npf.c revision 1.1 1 1.1 rmind /* $NetBSD: npf.c,v 1.1 2011/02/02 02:20:26 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*
4 1.1 rmind * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * Redistribution and use in source and binary forms, with or without
8 1.1 rmind * modification, are permitted provided that the following conditions
9 1.1 rmind * are met:
10 1.1 rmind * 1. Redistributions of source code must retain the above copyright
11 1.1 rmind * notice, this list of conditions and the following disclaimer.
12 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 rmind * notice, this list of conditions and the following disclaimer in the
14 1.1 rmind * documentation and/or other materials provided with the distribution.
15 1.1 rmind *
16 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 rmind * POSSIBILITY OF SUCH DAMAGE.
27 1.1 rmind */
28 1.1 rmind
29 1.1 rmind #include <sys/param.h>
30 1.1 rmind #include <sys/types.h>
31 1.1 rmind #include <sys/queue.h>
32 1.1 rmind
33 1.1 rmind #include <netinet/in.h>
34 1.1 rmind #include <netinet/in_systm.h>
35 1.1 rmind
36 1.1 rmind #include <arpa/inet.h>
37 1.1 rmind #include <net/if.h>
38 1.1 rmind #include <net/pfvar.h>
39 1.1 rmind #include <net/npf_ncode.h>
40 1.1 rmind #include <npf.h>
41 1.1 rmind
42 1.1 rmind #include <stdlib.h>
43 1.1 rmind #include <string.h>
44 1.1 rmind #include <fcntl.h>
45 1.1 rmind #include <errno.h>
46 1.1 rmind #include <err.h>
47 1.1 rmind #include <assert.h>
48 1.1 rmind
49 1.1 rmind #include "filter.h"
50 1.1 rmind
51 1.1 rmind static void npf_init_filter(char *, char *, int);
52 1.1 rmind static int npf_add_filter(uint32_t, uint8_t, struct sockaddr *,
53 1.1 rmind struct sockaddr *, uint16_t);
54 1.1 rmind static int npf_add_nat(uint32_t, struct sockaddr *, struct sockaddr *,
55 1.1 rmind uint16_t, struct sockaddr *, uint16_t, uint16_t);
56 1.1 rmind static int npf_add_rdr(uint32_t, struct sockaddr *, struct sockaddr *,
57 1.1 rmind uint16_t, struct sockaddr *, uint16_t);
58 1.1 rmind static int npf_server_lookup(struct sockaddr *, struct sockaddr *,
59 1.1 rmind struct sockaddr *);
60 1.1 rmind static int npf_prepare_commit(uint32_t);
61 1.1 rmind static int npf_do_commit(void);
62 1.1 rmind static int npf_do_rollback(void);
63 1.1 rmind
64 1.1 rmind const ftp_proxy_ops_t npf_fprx_ops = {
65 1.1 rmind .init_filter = npf_init_filter,
66 1.1 rmind .add_filter = npf_add_filter,
67 1.1 rmind .add_nat = npf_add_nat,
68 1.1 rmind .add_rdr = npf_add_rdr,
69 1.1 rmind .server_lookup = npf_server_lookup,
70 1.1 rmind .prepare_commit = npf_prepare_commit,
71 1.1 rmind .do_commit = npf_do_commit,
72 1.1 rmind .do_rollback = npf_do_rollback
73 1.1 rmind };
74 1.1 rmind
75 1.1 rmind #define sa_to_32(sa) (((struct sockaddr_in *)sa)->sin_addr.s_addr)
76 1.1 rmind
77 1.1 rmind #define NPF_DEV_PATH "/dev/npf"
78 1.1 rmind #define NPF_FP_RULE_TAG "ftp-proxy"
79 1.1 rmind
80 1.1 rmind typedef struct fp_ent {
81 1.1 rmind LIST_ENTRY(fp_ent) fpe_list;
82 1.1 rmind uint32_t fpe_id;
83 1.1 rmind nl_rule_t * fpe_rl;
84 1.1 rmind nl_rule_t * fpe_nat;
85 1.1 rmind nl_rule_t * fpe_rdr;
86 1.1 rmind } fp_ent_t;
87 1.1 rmind
88 1.1 rmind char * npfopts;
89 1.1 rmind
90 1.1 rmind static LIST_HEAD(, fp_ent) fp_ent_list;
91 1.1 rmind static fp_ent_t * fp_ent_hint;
92 1.1 rmind static struct sockaddr_in fp_server_sa;
93 1.1 rmind static u_int fp_if_idx;
94 1.1 rmind static int npf_fd;
95 1.1 rmind
96 1.1 rmind static uint32_t ncode[ ] = {
97 1.1 rmind /* from <shost> to <dhost> port <dport> */
98 1.1 rmind NPF_OPCODE_IP4MASK, 0x01, 0xdeadbeef, 0xffffffff,
99 1.1 rmind NPF_OPCODE_BNE, 15,
100 1.1 rmind NPF_OPCODE_IP4MASK, 0x00, 0xdeadbeef, 0xffffffff,
101 1.1 rmind NPF_OPCODE_BNE, 9,
102 1.1 rmind NPF_OPCODE_TCP_PORTS, 0x00, 0xdeadbeef,
103 1.1 rmind NPF_OPCODE_BNE, 4,
104 1.1 rmind /* Success (0x0) and failure (0xff) paths. */
105 1.1 rmind NPF_OPCODE_RET, 0x00,
106 1.1 rmind NPF_OPCODE_RET, 0xff
107 1.1 rmind };
108 1.1 rmind
109 1.1 rmind static void
110 1.1 rmind ftp_proxy_modify_nc(in_addr_t shost, in_addr_t dhost, in_port_t dport)
111 1.1 rmind {
112 1.1 rmind /* Source address to match. */
113 1.1 rmind ncode[2] = shost;
114 1.1 rmind /* Destination address to match. */
115 1.1 rmind ncode[8] = shost;
116 1.1 rmind /* Destination port to match. */
117 1.1 rmind ncode[14] = ((uint32_t)dport << 16) | dport;
118 1.1 rmind }
119 1.1 rmind
120 1.1 rmind static fp_ent_t *
121 1.1 rmind ftp_proxy_lookup(uint32_t id)
122 1.1 rmind {
123 1.1 rmind fp_ent_t *fpe;
124 1.1 rmind
125 1.1 rmind /* Look for FTP proxy entry. First, try hint (last used). */
126 1.1 rmind if (fp_ent_hint && fp_ent_hint->fpe_id == id) {
127 1.1 rmind return fp_ent_hint;
128 1.1 rmind }
129 1.1 rmind LIST_FOREACH(fpe, &fp_ent_list, fpe_list) {
130 1.1 rmind if (fpe->fpe_id == id)
131 1.1 rmind break;
132 1.1 rmind }
133 1.1 rmind return fpe;
134 1.1 rmind }
135 1.1 rmind
136 1.1 rmind static void
137 1.1 rmind npf_init_filter(char *opt_qname, char *opt_tagname, int opt_verbose)
138 1.1 rmind {
139 1.1 rmind char *netif = npfopts, *saddr, *port;
140 1.1 rmind
141 1.1 rmind /* XXX get rid of this */
142 1.1 rmind saddr = strchr(netif, ':');
143 1.1 rmind if (saddr == NULL) {
144 1.1 rmind errx(EXIT_FAILURE, "invalid -N option string: %s", npfopts);
145 1.1 rmind }
146 1.1 rmind *saddr++ = '\0';
147 1.1 rmind if (saddr == NULL || (port = strchr(saddr, ':')) == NULL) {
148 1.1 rmind errx(EXIT_FAILURE, "invalid -N option string: %s", npfopts);
149 1.1 rmind }
150 1.1 rmind *port++ = '\0';
151 1.1 rmind if (port == NULL) {
152 1.1 rmind errx(EXIT_FAILURE, "invalid -N option string: %s", npfopts);
153 1.1 rmind }
154 1.1 rmind
155 1.1 rmind fp_if_idx = if_nametoindex(netif);
156 1.1 rmind if (fp_if_idx == 0) {
157 1.1 rmind errx(EXIT_FAILURE, "invalid network interface '%s'", netif);
158 1.1 rmind }
159 1.1 rmind
160 1.1 rmind memset(&fp_server_sa, 0, sizeof(struct sockaddr_in));
161 1.1 rmind fp_server_sa.sin_len = sizeof(struct sockaddr_in);
162 1.1 rmind fp_server_sa.sin_family = AF_INET;
163 1.1 rmind fp_server_sa.sin_addr.s_addr = inet_addr(saddr);
164 1.1 rmind fp_server_sa.sin_port = htons(atoi(port));
165 1.1 rmind
166 1.1 rmind npf_fd = open(NPF_DEV_PATH, O_RDONLY);
167 1.1 rmind if (npf_fd == -1) {
168 1.1 rmind err(EXIT_FAILURE, "cannot open '%s'", NPF_DEV_PATH);
169 1.1 rmind }
170 1.1 rmind LIST_INIT(&fp_ent_list);
171 1.1 rmind fp_ent_hint = NULL;
172 1.1 rmind }
173 1.1 rmind
174 1.1 rmind static int
175 1.1 rmind npf_prepare_commit(uint32_t id)
176 1.1 rmind {
177 1.1 rmind fp_ent_t *fpe;
178 1.1 rmind
179 1.1 rmind /* Check if already exists. */
180 1.1 rmind fpe = ftp_proxy_lookup(id);
181 1.1 rmind if (fpe) {
182 1.1 rmind /* Destroy existing rules and reset the values. */
183 1.1 rmind npf_rule_destroy(fpe->fpe_rl);
184 1.1 rmind npf_rule_destroy(fpe->fpe_nat);
185 1.1 rmind npf_rule_destroy(fpe->fpe_rdr);
186 1.1 rmind goto reset;
187 1.1 rmind }
188 1.1 rmind /* Create a new one, if not found. */
189 1.1 rmind fpe = malloc(sizeof(fp_ent_t));
190 1.1 rmind if (fpe == NULL) {
191 1.1 rmind return -1;
192 1.1 rmind }
193 1.1 rmind LIST_INSERT_HEAD(&fp_ent_list, fpe, fpe_list);
194 1.1 rmind fpe->fpe_id = id;
195 1.1 rmind reset:
196 1.1 rmind fpe->fpe_rl = NULL;
197 1.1 rmind fpe->fpe_nat = NULL;
198 1.1 rmind fpe->fpe_rdr = NULL;
199 1.1 rmind return 0;
200 1.1 rmind }
201 1.1 rmind
202 1.1 rmind static int
203 1.1 rmind npf_add_filter(uint32_t id, uint8_t pf_dir, struct sockaddr *src,
204 1.1 rmind struct sockaddr *dst, uint16_t dport)
205 1.1 rmind {
206 1.1 rmind fp_ent_t *fpe;
207 1.1 rmind nl_rule_t *rl;
208 1.1 rmind int di;
209 1.1 rmind
210 1.1 rmind if (!src || !dst || !dport) {
211 1.1 rmind errno = EINVAL;
212 1.1 rmind return -1;
213 1.1 rmind }
214 1.1 rmind fpe = ftp_proxy_lookup(id);
215 1.1 rmind assert(fpe != NULL);
216 1.1 rmind
217 1.1 rmind di = (pf_dir == PF_OUT) ? NPF_RULE_OUT : NPF_RULE_IN;
218 1.1 rmind rl = npf_rule_create(NULL, di | NPF_RULE_PASS | NPF_RULE_FINAL, 0);
219 1.1 rmind if (rl == NULL) {
220 1.1 rmind errno = ENOMEM;
221 1.1 rmind return -1;
222 1.1 rmind }
223 1.1 rmind ftp_proxy_modify_nc(sa_to_32(src), sa_to_32(dst), htons(dport));
224 1.1 rmind errno = npf_rule_setcode(rl, NPF_CODE_NCODE, ncode, sizeof(ncode));
225 1.1 rmind if (errno) {
226 1.1 rmind npf_rule_destroy(rl);
227 1.1 rmind return -1;
228 1.1 rmind }
229 1.1 rmind assert(fpe->fpe_rl == NULL);
230 1.1 rmind fpe->fpe_rl = rl;
231 1.1 rmind return 0;
232 1.1 rmind }
233 1.1 rmind
234 1.1 rmind static int
235 1.1 rmind npf_add_nat(uint32_t id, struct sockaddr *src, struct sockaddr *dst,
236 1.1 rmind uint16_t dport, struct sockaddr *snat, uint16_t plow, uint16_t phigh)
237 1.1 rmind {
238 1.1 rmind fp_ent_t *fpe;
239 1.1 rmind nl_nat_t *nt;
240 1.1 rmind npf_addr_t addr;
241 1.1 rmind
242 1.1 rmind if (!src || !dst || !dport || !snat || !plow ||
243 1.1 rmind (src->sa_family != snat->sa_family)) {
244 1.1 rmind errno = EINVAL;
245 1.1 rmind return (-1);
246 1.1 rmind }
247 1.1 rmind fpe = ftp_proxy_lookup(id);
248 1.1 rmind assert(fpe != NULL);
249 1.1 rmind
250 1.1 rmind memcpy(&addr, &sa_to_32(snat), sizeof(struct in_addr));
251 1.1 rmind nt = npf_nat_create(NPF_NATOUT, NPF_NAT_PORTS | NPF_NAT_PORTMAP, 0,
252 1.1 rmind &addr, AF_INET, htons(plow));
253 1.1 rmind if (nt == NULL) {
254 1.1 rmind errno = ENOMEM;
255 1.1 rmind return -1;
256 1.1 rmind }
257 1.1 rmind ftp_proxy_modify_nc(sa_to_32(src), sa_to_32(dst), htons(dport));
258 1.1 rmind errno = npf_rule_setcode(nt, NPF_CODE_NCODE, ncode, sizeof(ncode));
259 1.1 rmind if (errno) {
260 1.1 rmind npf_rule_destroy(nt);
261 1.1 rmind return -1;
262 1.1 rmind }
263 1.1 rmind assert(fpe->fpe_nat == NULL);
264 1.1 rmind fpe->fpe_nat = nt;
265 1.1 rmind return 0;
266 1.1 rmind }
267 1.1 rmind
268 1.1 rmind static int
269 1.1 rmind npf_add_rdr(uint32_t id, struct sockaddr *src, struct sockaddr *dst,
270 1.1 rmind uint16_t dport, struct sockaddr *rdr, uint16_t rdr_port)
271 1.1 rmind {
272 1.1 rmind fp_ent_t *fpe;
273 1.1 rmind nl_nat_t *nt;
274 1.1 rmind npf_addr_t addr;
275 1.1 rmind
276 1.1 rmind if (!src || !dst || !dport || !rdr || !rdr_port ||
277 1.1 rmind (src->sa_family != rdr->sa_family)) {
278 1.1 rmind errno = EINVAL;
279 1.1 rmind return -1;
280 1.1 rmind }
281 1.1 rmind fpe = ftp_proxy_lookup(id);
282 1.1 rmind assert(fpe != NULL);
283 1.1 rmind
284 1.1 rmind memcpy(&addr, &sa_to_32(rdr), sizeof(struct in_addr));
285 1.1 rmind nt = npf_nat_create(NPF_NATIN, NPF_NAT_PORTS, 0,
286 1.1 rmind &addr, AF_INET, htons(rdr_port));
287 1.1 rmind if (nt == NULL) {
288 1.1 rmind errno = ENOMEM;
289 1.1 rmind return -1;
290 1.1 rmind }
291 1.1 rmind ftp_proxy_modify_nc(sa_to_32(src), sa_to_32(dst), htons(dport));
292 1.1 rmind errno = npf_rule_setcode(nt, NPF_CODE_NCODE, ncode, sizeof(ncode));
293 1.1 rmind if (errno) {
294 1.1 rmind npf_rule_destroy(nt);
295 1.1 rmind return -1;
296 1.1 rmind }
297 1.1 rmind assert(fpe->fpe_rdr == NULL);
298 1.1 rmind fpe->fpe_rdr = nt;
299 1.1 rmind return 0;
300 1.1 rmind }
301 1.1 rmind
302 1.1 rmind static int
303 1.1 rmind npf_server_lookup(struct sockaddr *c, struct sockaddr *proxy,
304 1.1 rmind struct sockaddr *server)
305 1.1 rmind {
306 1.1 rmind
307 1.1 rmind memcpy(server, &fp_server_sa, sizeof(struct sockaddr_in));
308 1.1 rmind return 0;
309 1.1 rmind }
310 1.1 rmind
311 1.1 rmind static int
312 1.1 rmind npf_do_commit(void)
313 1.1 rmind {
314 1.1 rmind nl_rule_t *group;
315 1.1 rmind fp_ent_t *fpe;
316 1.1 rmind pri_t pri;
317 1.1 rmind
318 1.1 rmind group = npf_rule_create(NPF_FP_RULE_TAG, NPF_RULE_PASS | NPF_RULE_IN |
319 1.1 rmind NPF_RULE_OUT | NPF_RULE_FINAL, fp_if_idx);
320 1.1 rmind if (group == NULL) {
321 1.1 rmind return -1;
322 1.1 rmind }
323 1.1 rmind pri = 1;
324 1.1 rmind LIST_FOREACH(fpe, &fp_ent_list, fpe_list) {
325 1.1 rmind npf_rule_insert(NULL, group, fpe->fpe_rl, pri++);
326 1.1 rmind }
327 1.1 rmind npf_update_rule(npf_fd, NPF_FP_RULE_TAG, group);
328 1.1 rmind npf_rule_destroy(group);
329 1.1 rmind return 0;
330 1.1 rmind }
331 1.1 rmind
332 1.1 rmind static int
333 1.1 rmind npf_do_rollback(void)
334 1.1 rmind {
335 1.1 rmind /* None. */
336 1.1 rmind return 0;
337 1.1 rmind }
338