npf_data.c revision 1.4 1 1.4 rmind /* $NetBSD: npf_data.c,v 1.4 2010/11/11 06:30:39 rmind Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2009-2010 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 /*
30 1.1 rmind * NPF proplib(9) dictionary producer.
31 1.1 rmind *
32 1.1 rmind * XXX: Needs some clean-up.
33 1.1 rmind */
34 1.1 rmind
35 1.4 rmind #include <sys/cdefs.h>
36 1.4 rmind __RCSID("$NetBSD: npf_data.c,v 1.4 2010/11/11 06:30:39 rmind Exp $");
37 1.4 rmind
38 1.1 rmind #include <sys/types.h>
39 1.1 rmind #include <sys/socket.h>
40 1.1 rmind #include <sys/ioctl.h>
41 1.1 rmind #include <net/if.h>
42 1.3 rmind #include <netinet/tcp.h>
43 1.1 rmind
44 1.1 rmind #include <arpa/inet.h>
45 1.1 rmind #include <prop/proplib.h>
46 1.1 rmind
47 1.1 rmind #include <stdlib.h>
48 1.1 rmind #include <string.h>
49 1.1 rmind #include <unistd.h>
50 1.1 rmind #include <ctype.h>
51 1.1 rmind #include <err.h>
52 1.1 rmind #include <ifaddrs.h>
53 1.1 rmind #include <netdb.h>
54 1.1 rmind #include <assert.h>
55 1.1 rmind
56 1.1 rmind #include "npfctl.h"
57 1.1 rmind
58 1.1 rmind static struct ifaddrs * ifs_list = NULL;
59 1.1 rmind
60 1.1 rmind static prop_dictionary_t npf_dict, settings_dict;
61 1.1 rmind static prop_array_t nat_arr, tables_arr, rules_arr;
62 1.1 rmind
63 1.1 rmind static pri_t gr_prio_counter = 1;
64 1.1 rmind static pri_t rl_prio_counter = 1;
65 1.1 rmind static pri_t nat_prio_counter = 1;
66 1.1 rmind
67 1.1 rmind void
68 1.1 rmind npfctl_init_data(void)
69 1.1 rmind {
70 1.1 rmind prop_number_t ver;
71 1.1 rmind
72 1.1 rmind if (getifaddrs(&ifs_list) == -1)
73 1.1 rmind err(EXIT_FAILURE, "getifaddrs");
74 1.1 rmind
75 1.1 rmind npf_dict = prop_dictionary_create();
76 1.1 rmind
77 1.1 rmind ver = prop_number_create_integer(NPF_VERSION);
78 1.1 rmind prop_dictionary_set(npf_dict, "version", ver);
79 1.1 rmind
80 1.1 rmind nat_arr = prop_array_create();
81 1.3 rmind prop_dictionary_set(npf_dict, "translation", nat_arr);
82 1.1 rmind
83 1.1 rmind settings_dict = prop_dictionary_create();
84 1.1 rmind prop_dictionary_set(npf_dict, "settings", settings_dict);
85 1.1 rmind
86 1.1 rmind tables_arr = prop_array_create();
87 1.1 rmind prop_dictionary_set(npf_dict, "tables", tables_arr);
88 1.1 rmind
89 1.1 rmind rules_arr = prop_array_create();
90 1.1 rmind prop_dictionary_set(npf_dict, "rules", rules_arr);
91 1.1 rmind }
92 1.1 rmind
93 1.1 rmind int
94 1.1 rmind npfctl_ioctl_send(int fd)
95 1.1 rmind {
96 1.1 rmind int ret = 0, errval;
97 1.1 rmind
98 1.1 rmind #ifdef DEBUG
99 1.3 rmind prop_dictionary_externalize_to_file(npf_dict, "./npf.plist");
100 1.1 rmind #else
101 1.1 rmind errval = prop_dictionary_send_ioctl(npf_dict, fd, IOC_NPF_RELOAD);
102 1.1 rmind if (errval) {
103 1.1 rmind errx(EXIT_FAILURE, "npf_ioctl_send: %s\n", strerror(errval));
104 1.1 rmind ret = -1;
105 1.1 rmind }
106 1.1 rmind #endif
107 1.1 rmind prop_object_release(npf_dict);
108 1.1 rmind return ret;
109 1.1 rmind }
110 1.1 rmind
111 1.1 rmind /*
112 1.1 rmind * Helper routines:
113 1.1 rmind *
114 1.1 rmind * npfctl_getif() - get interface addresses and index number from name.
115 1.1 rmind * npfctl_parse_v4mask() - parse address/mask integers from CIDR block.
116 1.3 rmind * npfctl_parse_port() - parse port number (which may be a service name).
117 1.3 rmind * npfctl_parse_tcpfl() - parse TCP flags.
118 1.1 rmind */
119 1.1 rmind
120 1.1 rmind static struct ifaddrs *
121 1.1 rmind npfctl_getif(char *ifname, unsigned int *if_idx)
122 1.1 rmind {
123 1.1 rmind struct ifaddrs *ifent;
124 1.1 rmind struct sockaddr_in *sin;
125 1.1 rmind
126 1.1 rmind for (ifent = ifs_list; ifent != NULL; ifent = ifent->ifa_next) {
127 1.1 rmind sin = (struct sockaddr_in *)ifent->ifa_addr;
128 1.1 rmind
129 1.1 rmind if (sin->sin_family != AF_INET)
130 1.1 rmind continue;
131 1.1 rmind if (strcmp(ifent->ifa_name, ifname) == 0)
132 1.1 rmind break;
133 1.1 rmind }
134 1.1 rmind if (ifent) {
135 1.1 rmind *if_idx = if_nametoindex(ifname);
136 1.1 rmind }
137 1.1 rmind return ifent;
138 1.1 rmind }
139 1.1 rmind
140 1.1 rmind bool
141 1.3 rmind npfctl_parse_v4mask(char *ostr, in_addr_t *addr, in_addr_t *mask)
142 1.1 rmind {
143 1.3 rmind char *str = xstrdup(ostr);
144 1.1 rmind char *p = strchr(str, '/');
145 1.1 rmind u_int bits;
146 1.3 rmind bool ret;
147 1.1 rmind
148 1.1 rmind /* In network byte order. */
149 1.1 rmind if (p) {
150 1.1 rmind *p++ = '\0';
151 1.1 rmind bits = (u_int)atoi(p);
152 1.1 rmind *mask = bits ? htonl(0xffffffff << (32 - bits)) : 0;
153 1.1 rmind } else {
154 1.1 rmind *mask = 0xffffffff;
155 1.1 rmind }
156 1.3 rmind ret = inet_aton(str, (struct in_addr *)addr) != 0;
157 1.3 rmind free(str);
158 1.3 rmind return ret;
159 1.3 rmind }
160 1.3 rmind
161 1.3 rmind static bool
162 1.3 rmind npfctl_parse_port(char *ostr, bool *range, in_port_t *fport, in_port_t *tport)
163 1.3 rmind {
164 1.3 rmind char *str = xstrdup(ostr), *sep;
165 1.3 rmind
166 1.3 rmind *range = false;
167 1.3 rmind if ((sep = strchr(str, ':')) != NULL) {
168 1.3 rmind /* Port range (only numeric). */
169 1.3 rmind *range = true;
170 1.3 rmind *sep = '\0';
171 1.3 rmind
172 1.3 rmind } else if (isalpha((unsigned char)*str)) {
173 1.3 rmind struct servent *se;
174 1.3 rmind
175 1.3 rmind se = getservbyname(str, NULL);
176 1.3 rmind if (se == NULL) {
177 1.3 rmind free(str);
178 1.3 rmind return false;
179 1.3 rmind }
180 1.3 rmind *fport = se->s_port;
181 1.3 rmind } else {
182 1.3 rmind *fport = htons(atoi(str));
183 1.3 rmind }
184 1.3 rmind *tport = sep ? htons(atoi(sep + 1)) : *fport;
185 1.3 rmind free(str);
186 1.3 rmind return true;
187 1.1 rmind }
188 1.1 rmind
189 1.1 rmind static void
190 1.1 rmind npfctl_parse_cidr(char *str, in_addr_t *addr, in_addr_t *mask)
191 1.1 rmind {
192 1.1 rmind
193 1.1 rmind if (isalpha((unsigned char)*str)) {
194 1.1 rmind struct ifaddrs *ifa;
195 1.1 rmind struct sockaddr_in *sin;
196 1.1 rmind u_int idx;
197 1.1 rmind
198 1.1 rmind if ((ifa = npfctl_getif(str, &idx)) == NULL) {
199 1.1 rmind errx(EXIT_FAILURE, "invalid interface '%s'", str);
200 1.1 rmind }
201 1.1 rmind /* Interface address. */
202 1.1 rmind sin = (struct sockaddr_in *)ifa->ifa_addr;
203 1.1 rmind *addr = sin->sin_addr.s_addr;
204 1.1 rmind *mask = 0xffffffff;
205 1.1 rmind
206 1.1 rmind } else if (!npfctl_parse_v4mask(str, addr, mask)) {
207 1.1 rmind errx(EXIT_FAILURE, "invalid CIDR '%s'\n", str);
208 1.1 rmind }
209 1.1 rmind }
210 1.1 rmind
211 1.3 rmind static bool
212 1.3 rmind npfctl_parse_tcpfl(char *s, uint8_t *tfl, uint8_t *tfl_mask)
213 1.3 rmind {
214 1.3 rmind uint8_t tcpfl = 0;
215 1.3 rmind bool mask = false;
216 1.3 rmind
217 1.3 rmind while (*s) {
218 1.3 rmind switch (*s) {
219 1.3 rmind case 'F': tcpfl |= TH_FIN; break;
220 1.3 rmind case 'S': tcpfl |= TH_SYN; break;
221 1.3 rmind case 'R': tcpfl |= TH_RST; break;
222 1.3 rmind case 'P': tcpfl |= TH_PUSH; break;
223 1.3 rmind case 'A': tcpfl |= TH_ACK; break;
224 1.3 rmind case 'U': tcpfl |= TH_URG; break;
225 1.3 rmind case 'E': tcpfl |= TH_ECE; break;
226 1.3 rmind case 'W': tcpfl |= TH_CWR; break;
227 1.3 rmind case '/':
228 1.3 rmind *s = '\0';
229 1.3 rmind *tfl = tcpfl;
230 1.3 rmind tcpfl = 0;
231 1.3 rmind mask = true;
232 1.3 rmind break;
233 1.3 rmind default:
234 1.3 rmind return false;
235 1.3 rmind }
236 1.3 rmind s++;
237 1.3 rmind }
238 1.3 rmind if (!mask) {
239 1.3 rmind *tfl = tcpfl;
240 1.3 rmind }
241 1.3 rmind *tfl_mask = tcpfl;
242 1.3 rmind return true;
243 1.3 rmind }
244 1.3 rmind
245 1.1 rmind /*
246 1.1 rmind * NPF table creation and construction routines.
247 1.1 rmind */
248 1.1 rmind
249 1.1 rmind prop_dictionary_t
250 1.1 rmind npfctl_lookup_table(char *tidstr)
251 1.1 rmind {
252 1.1 rmind prop_dictionary_t tl;
253 1.1 rmind prop_object_iterator_t it;
254 1.1 rmind prop_object_t obj;
255 1.1 rmind u_int tid;
256 1.1 rmind
257 1.1 rmind if ((it = prop_array_iterator(tables_arr)) == NULL)
258 1.1 rmind err(EXIT_FAILURE, "prop_array_iterator");
259 1.1 rmind
260 1.1 rmind tid = atoi(tidstr);
261 1.1 rmind while ((tl = prop_object_iterator_next(it)) != NULL) {
262 1.1 rmind obj = prop_dictionary_get(tl, "id");
263 1.1 rmind if (tid == prop_number_integer_value(obj))
264 1.1 rmind break;
265 1.1 rmind }
266 1.1 rmind return tl;
267 1.1 rmind }
268 1.1 rmind
269 1.1 rmind prop_dictionary_t
270 1.1 rmind npfctl_mk_table(void)
271 1.1 rmind {
272 1.1 rmind prop_dictionary_t tl;
273 1.1 rmind prop_array_t tlist;
274 1.1 rmind
275 1.1 rmind tl = prop_dictionary_create();
276 1.1 rmind tlist = prop_array_create();
277 1.1 rmind prop_dictionary_set(tl, "entries", tlist);
278 1.1 rmind
279 1.1 rmind return tl;
280 1.1 rmind }
281 1.1 rmind
282 1.1 rmind void
283 1.1 rmind npfctl_table_setup(prop_dictionary_t tl, char *idstr, char *typestr)
284 1.1 rmind {
285 1.1 rmind prop_number_t typenum;
286 1.1 rmind unsigned int id;
287 1.1 rmind
288 1.1 rmind id = atoi(idstr);
289 1.1 rmind /* TODO: 1. check ID range 2. check if not a duplicate */
290 1.1 rmind prop_dictionary_set(tl, "id", prop_number_create_integer(id));
291 1.1 rmind
292 1.1 rmind if (strcmp(typestr, "hash")) {
293 1.1 rmind typenum = prop_number_create_integer(NPF_TABLE_HASH);
294 1.1 rmind } else if (strcmp(typestr, "tree")) {
295 1.1 rmind typenum = prop_number_create_integer(NPF_TABLE_RBTREE);
296 1.1 rmind } else {
297 1.1 rmind errx(EXIT_FAILURE, "invalid table type '%s'\n", typestr);
298 1.1 rmind }
299 1.1 rmind prop_dictionary_set(tl, "type", typenum);
300 1.1 rmind }
301 1.1 rmind
302 1.1 rmind void
303 1.1 rmind npfctl_construct_table(prop_dictionary_t tl, char *fname)
304 1.1 rmind {
305 1.1 rmind prop_dictionary_t entdict;
306 1.1 rmind prop_array_t tblents;
307 1.1 rmind char *buf;
308 1.1 rmind FILE *fp;
309 1.1 rmind size_t n;
310 1.1 rmind int l;
311 1.1 rmind
312 1.1 rmind tblents = prop_dictionary_get(tl, "entries");
313 1.1 rmind assert(tblents != NULL);
314 1.1 rmind
315 1.1 rmind fp = fopen(fname, "r");
316 1.1 rmind if (fp == NULL) {
317 1.1 rmind err(EXIT_FAILURE, "fopen");
318 1.1 rmind }
319 1.1 rmind l = 1;
320 1.1 rmind buf = NULL;
321 1.1 rmind while (getline(&buf, &n, fp) != -1) {
322 1.1 rmind in_addr_t addr, mask;
323 1.1 rmind
324 1.1 rmind if (*buf == '\n' || *buf == '#')
325 1.1 rmind continue;
326 1.1 rmind
327 1.1 rmind /* IPv4 CIDR: a.b.c.d/mask */
328 1.1 rmind if (!npfctl_parse_v4mask(buf, &addr, &mask))
329 1.1 rmind errx(EXIT_FAILURE, "invalid table entry at line %d", l);
330 1.1 rmind
331 1.1 rmind /* Create and add table entry. */
332 1.1 rmind entdict = prop_dictionary_create();
333 1.1 rmind prop_dictionary_set(entdict, "addr",
334 1.1 rmind prop_number_create_integer(addr));
335 1.1 rmind prop_dictionary_set(entdict, "mask",
336 1.1 rmind prop_number_create_integer(mask));
337 1.1 rmind prop_array_add(tblents, entdict);
338 1.1 rmind l++;
339 1.1 rmind }
340 1.1 rmind if (buf != NULL) {
341 1.1 rmind free(buf);
342 1.1 rmind }
343 1.1 rmind }
344 1.1 rmind
345 1.1 rmind void
346 1.1 rmind npfctl_add_table(prop_dictionary_t tl)
347 1.1 rmind {
348 1.1 rmind
349 1.1 rmind prop_array_add(tables_arr, tl);
350 1.1 rmind }
351 1.1 rmind
352 1.1 rmind /*
353 1.1 rmind * npfctl_mk_rule: create a rule (or group) dictionary.
354 1.1 rmind *
355 1.1 rmind * Note: group is a rule containing subrules. It has no n-code, however.
356 1.1 rmind */
357 1.1 rmind prop_dictionary_t
358 1.1 rmind npfctl_mk_rule(bool group)
359 1.1 rmind {
360 1.1 rmind prop_dictionary_t rl;
361 1.1 rmind prop_array_t subrl;
362 1.1 rmind pri_t pri;
363 1.1 rmind
364 1.1 rmind rl = prop_dictionary_create();
365 1.1 rmind if (group) {
366 1.1 rmind subrl = prop_array_create();
367 1.1 rmind prop_dictionary_set(rl, "subrules", subrl);
368 1.1 rmind /* Give new priority, reset rule priority counter. */
369 1.1 rmind pri = gr_prio_counter++;
370 1.1 rmind rl_prio_counter = 1;
371 1.1 rmind } else {
372 1.1 rmind pri = rl_prio_counter++;
373 1.1 rmind }
374 1.1 rmind prop_dictionary_set(rl, "priority",
375 1.1 rmind prop_number_create_integer(pri));
376 1.1 rmind
377 1.1 rmind return rl;
378 1.1 rmind }
379 1.1 rmind
380 1.1 rmind void
381 1.1 rmind npfctl_add_rule(prop_dictionary_t rl, prop_dictionary_t parent)
382 1.1 rmind {
383 1.1 rmind prop_array_t rlset;
384 1.1 rmind
385 1.1 rmind if (parent) {
386 1.1 rmind rlset = prop_dictionary_get(parent, "subrules");
387 1.1 rmind assert(rlset != NULL);
388 1.1 rmind } else {
389 1.1 rmind rlset = rules_arr;
390 1.1 rmind }
391 1.1 rmind prop_array_add(rlset, rl);
392 1.1 rmind }
393 1.1 rmind
394 1.1 rmind void
395 1.4 rmind npfctl_rule_setattr(prop_dictionary_t rl, int attr, char *iface,
396 1.4 rmind bool ipid_rnd, int minttl, int maxmss)
397 1.1 rmind {
398 1.1 rmind prop_number_t attrnum;
399 1.1 rmind
400 1.1 rmind attrnum = prop_number_create_integer(attr);
401 1.1 rmind prop_dictionary_set(rl, "attributes", attrnum);
402 1.1 rmind if (iface) {
403 1.1 rmind prop_number_t ifnum;
404 1.1 rmind unsigned int if_idx;
405 1.1 rmind
406 1.1 rmind if (npfctl_getif(iface, &if_idx) == NULL) {
407 1.1 rmind errx(EXIT_FAILURE, "invalid interface '%s'", iface);
408 1.1 rmind }
409 1.1 rmind ifnum = prop_number_create_integer(if_idx);
410 1.1 rmind prop_dictionary_set(rl, "interface", ifnum);
411 1.1 rmind }
412 1.4 rmind if (attr & NPF_RULE_NORMALIZE) {
413 1.4 rmind prop_dictionary_set(rl, "randomize-id",
414 1.4 rmind prop_bool_create(ipid_rnd));
415 1.4 rmind prop_dictionary_set(rl, "min-ttl",
416 1.4 rmind prop_number_create_integer(minttl));
417 1.4 rmind prop_dictionary_set(rl, "max-mss",
418 1.4 rmind prop_number_create_integer(maxmss));
419 1.4 rmind }
420 1.1 rmind }
421 1.1 rmind
422 1.1 rmind /*
423 1.1 rmind * Main rule generation routines.
424 1.1 rmind */
425 1.1 rmind
426 1.1 rmind static void
427 1.1 rmind npfctl_rulenc_v4cidr(void **nc, int nblocks[], var_t *dat, bool sd)
428 1.1 rmind {
429 1.1 rmind element_t *el = dat->v_elements;
430 1.1 rmind int foff;
431 1.1 rmind
432 1.1 rmind /* If table, generate a single table matching block. */
433 1.1 rmind if (dat->v_type == VAR_TABLE) {
434 1.1 rmind u_int tid = atoi(el->e_data);
435 1.1 rmind
436 1.1 rmind nblocks[0]--;
437 1.1 rmind foff = npfctl_failure_offset(nblocks);
438 1.1 rmind npfctl_gennc_tbl(nc, foff, tid, sd);
439 1.1 rmind return;
440 1.1 rmind }
441 1.1 rmind
442 1.1 rmind /* Generate v4 CIDR matching blocks. */
443 1.1 rmind for (el = dat->v_elements; el != NULL; el = el->e_next) {
444 1.1 rmind in_addr_t addr, mask;
445 1.1 rmind
446 1.1 rmind npfctl_parse_cidr(el->e_data, &addr, &mask);
447 1.1 rmind
448 1.1 rmind nblocks[1]--;
449 1.1 rmind foff = npfctl_failure_offset(nblocks);
450 1.1 rmind npfctl_gennc_v4cidr(nc, foff, addr, mask, sd);
451 1.1 rmind }
452 1.1 rmind }
453 1.1 rmind
454 1.1 rmind static void
455 1.1 rmind npfctl_rulenc_ports(void **nc, int nblocks[], var_t *dat, bool tcpudp, bool sd)
456 1.1 rmind {
457 1.1 rmind element_t *el = dat->v_elements;
458 1.1 rmind int foff;
459 1.1 rmind
460 1.1 rmind assert(dat->v_type != VAR_TABLE);
461 1.1 rmind
462 1.1 rmind /* Generate TCP/UDP port matching blocks. */
463 1.1 rmind for (el = dat->v_elements; el != NULL; el = el->e_next) {
464 1.3 rmind in_port_t fport, tport;
465 1.3 rmind bool range;
466 1.1 rmind
467 1.3 rmind if (!npfctl_parse_port(el->e_data, &range, &fport, &tport)) {
468 1.3 rmind errx(EXIT_FAILURE, "invalid service '%s'", el->e_data);
469 1.1 rmind }
470 1.1 rmind nblocks[0]--;
471 1.1 rmind foff = npfctl_failure_offset(nblocks);
472 1.3 rmind npfctl_gennc_ports(nc, foff, fport, tport, tcpudp, sd);
473 1.1 rmind }
474 1.1 rmind }
475 1.1 rmind
476 1.1 rmind static void
477 1.1 rmind npfctl_rulenc_block(void **nc, int nblocks[], var_t *cidr, var_t *ports,
478 1.1 rmind bool both, bool tcpudp, bool sd)
479 1.1 rmind {
480 1.1 rmind
481 1.1 rmind npfctl_rulenc_v4cidr(nc, nblocks, cidr, sd);
482 1.1 rmind if (ports == NULL) {
483 1.1 rmind return;
484 1.1 rmind }
485 1.1 rmind npfctl_rulenc_ports(nc, nblocks, ports, tcpudp, sd);
486 1.1 rmind if (!both) {
487 1.1 rmind return;
488 1.1 rmind }
489 1.1 rmind npfctl_rulenc_ports(nc, nblocks, ports, !tcpudp, sd);
490 1.1 rmind }
491 1.1 rmind
492 1.1 rmind void
493 1.3 rmind npfctl_rule_protodata(prop_dictionary_t rl, char *proto, char *tcp_flags,
494 1.3 rmind int icmp_type, int icmp_code,
495 1.3 rmind var_t *from, var_t *fports, var_t *to, var_t *tports)
496 1.1 rmind {
497 1.1 rmind prop_data_t ncdata;
498 1.1 rmind bool icmp, tcpudp, both;
499 1.3 rmind int foff, nblocks[3] = { 0, 0, 0 };
500 1.1 rmind void *ncptr, *nc;
501 1.1 rmind size_t sz;
502 1.1 rmind
503 1.1 rmind /*
504 1.1 rmind * Default: both TCP and UDP.
505 1.1 rmind */
506 1.1 rmind icmp = false;
507 1.1 rmind tcpudp = true;
508 1.1 rmind both = false;
509 1.1 rmind if (proto == NULL) {
510 1.1 rmind goto skip_proto;
511 1.1 rmind }
512 1.1 rmind
513 1.1 rmind if (strcmp(proto, "icmp") == 0) {
514 1.1 rmind /* ICMP case. */
515 1.1 rmind fports = NULL;
516 1.1 rmind tports = NULL;
517 1.1 rmind icmp = true;
518 1.1 rmind
519 1.1 rmind } else if (strcmp(proto, "tcp") == 0) {
520 1.1 rmind /* Just TCP. */
521 1.1 rmind tcpudp = true;
522 1.1 rmind
523 1.1 rmind } else if (strcmp(proto, "udp") == 0) {
524 1.1 rmind /* Just UDP. */
525 1.1 rmind tcpudp = false;
526 1.1 rmind
527 1.1 rmind } else {
528 1.1 rmind /* Default. */
529 1.1 rmind }
530 1.1 rmind skip_proto:
531 1.3 rmind if (icmp_type != -1) {
532 1.3 rmind assert(tcp_flags == NULL);
533 1.3 rmind icmp = true;
534 1.3 rmind nblocks[2] += 1;
535 1.3 rmind }
536 1.3 rmind if (tcpudp && tcp_flags) {
537 1.3 rmind assert(icmp_type == -1 && icmp_code == -1);
538 1.3 rmind nblocks[2] += 1;
539 1.3 rmind }
540 1.1 rmind
541 1.1 rmind /* Calculate how blocks to determince n-code. */
542 1.1 rmind if (from && from->v_count) {
543 1.1 rmind if (from->v_type == VAR_TABLE)
544 1.1 rmind nblocks[0] += 1;
545 1.1 rmind else
546 1.1 rmind nblocks[1] += from->v_count;
547 1.1 rmind if (fports && fports->v_count)
548 1.1 rmind nblocks[0] += fports->v_count * (both ? 2 : 1);
549 1.1 rmind }
550 1.1 rmind if (to && to->v_count) {
551 1.1 rmind if (to->v_type == VAR_TABLE)
552 1.1 rmind nblocks[0] += 1;
553 1.1 rmind else
554 1.1 rmind nblocks[1] += to->v_count;
555 1.1 rmind if (tports && tports->v_count)
556 1.1 rmind nblocks[0] += tports->v_count * (both ? 2 : 1);
557 1.1 rmind }
558 1.1 rmind
559 1.3 rmind /* Any n-code to generate? */
560 1.3 rmind if ((nblocks[0] + nblocks[1] + nblocks[2]) == 0) {
561 1.3 rmind /* Done, if none. */
562 1.3 rmind return;
563 1.3 rmind }
564 1.3 rmind
565 1.1 rmind /* Allocate memory for the n-code. */
566 1.1 rmind sz = npfctl_calc_ncsize(nblocks);
567 1.1 rmind ncptr = malloc(sz);
568 1.1 rmind if (ncptr == NULL) {
569 1.1 rmind perror("malloc");
570 1.1 rmind exit(EXIT_FAILURE);
571 1.1 rmind }
572 1.1 rmind nc = ncptr;
573 1.1 rmind
574 1.3 rmind /*
575 1.3 rmind * Generate v4 CIDR matching blocks and TCP/UDP port matching.
576 1.3 rmind */
577 1.1 rmind if (from) {
578 1.1 rmind npfctl_rulenc_block(&nc, nblocks, from, fports,
579 1.1 rmind both, tcpudp, true);
580 1.1 rmind }
581 1.1 rmind if (to) {
582 1.1 rmind npfctl_rulenc_block(&nc, nblocks, to, tports,
583 1.1 rmind both, tcpudp, false);
584 1.1 rmind }
585 1.3 rmind
586 1.1 rmind if (icmp) {
587 1.3 rmind /*
588 1.3 rmind * ICMP case.
589 1.3 rmind */
590 1.3 rmind nblocks[2]--;
591 1.3 rmind foff = npfctl_failure_offset(nblocks);
592 1.3 rmind npfctl_gennc_icmp(&nc, foff, icmp_type, icmp_code);
593 1.3 rmind
594 1.3 rmind } else if (tcpudp && tcp_flags) {
595 1.3 rmind /*
596 1.3 rmind * TCP case, flags.
597 1.3 rmind */
598 1.3 rmind uint8_t tfl = 0, tfl_mask;
599 1.3 rmind
600 1.3 rmind nblocks[2]--;
601 1.3 rmind foff = npfctl_failure_offset(nblocks);
602 1.3 rmind if (!npfctl_parse_tcpfl(tcp_flags, &tfl, &tfl_mask)) {
603 1.3 rmind errx(EXIT_FAILURE, "invalid TCP flags '%s'", tcp_flags);
604 1.3 rmind }
605 1.3 rmind npfctl_gennc_tcpfl(&nc, foff, tfl, tfl_mask);
606 1.1 rmind }
607 1.1 rmind npfctl_gennc_complete(&nc);
608 1.1 rmind
609 1.3 rmind if ((uintptr_t)nc - (uintptr_t)ncptr != sz) {
610 1.2 jnemeth errx(EXIT_FAILURE, "n-code size got wrong (%tu != %zu)",
611 1.1 rmind (uintptr_t)nc - (uintptr_t)ncptr, sz);
612 1.3 rmind }
613 1.1 rmind
614 1.1 rmind #ifdef DEBUG
615 1.1 rmind uint32_t *op = ncptr;
616 1.1 rmind size_t n = sz;
617 1.1 rmind do {
618 1.1 rmind DPRINTF(("\t> |0x%02x|\n", (u_int)*op));
619 1.1 rmind op++;
620 1.1 rmind n -= sizeof(*op);
621 1.1 rmind } while (n);
622 1.1 rmind #endif
623 1.1 rmind
624 1.1 rmind /* Create a final memory block of data, ready to send. */
625 1.1 rmind ncdata = prop_data_create_data(ncptr, sz);
626 1.1 rmind if (ncdata == NULL) {
627 1.1 rmind perror("prop_data_create_data");
628 1.1 rmind exit(EXIT_FAILURE);
629 1.1 rmind }
630 1.1 rmind prop_dictionary_set(rl, "ncode", ncdata);
631 1.1 rmind free(ncptr);
632 1.1 rmind }
633 1.1 rmind
634 1.1 rmind /*
635 1.1 rmind * NAT policy construction routines.
636 1.1 rmind */
637 1.1 rmind
638 1.1 rmind prop_dictionary_t
639 1.1 rmind npfctl_mk_nat(void)
640 1.1 rmind {
641 1.1 rmind prop_dictionary_t rl;
642 1.1 rmind pri_t pri;
643 1.1 rmind
644 1.1 rmind /* NAT policy is rule with extra info. */
645 1.1 rmind rl = prop_dictionary_create();
646 1.1 rmind pri = nat_prio_counter++;
647 1.1 rmind prop_dictionary_set(rl, "priority",
648 1.1 rmind prop_number_create_integer(pri));
649 1.1 rmind return rl;
650 1.1 rmind }
651 1.1 rmind
652 1.1 rmind void
653 1.1 rmind npfctl_add_nat(prop_dictionary_t nat)
654 1.1 rmind {
655 1.1 rmind prop_array_add(nat_arr, nat);
656 1.1 rmind }
657 1.1 rmind
658 1.1 rmind void
659 1.3 rmind npfctl_nat_setup(prop_dictionary_t rl, int type, int flags,
660 1.3 rmind char *iface, char *taddr, char *rport)
661 1.1 rmind {
662 1.3 rmind int attr = NPF_RULE_PASS | NPF_RULE_FINAL;
663 1.1 rmind in_addr_t addr, mask;
664 1.4 rmind void *addrptr;
665 1.1 rmind
666 1.3 rmind /* Translation type and flags. */
667 1.3 rmind prop_dictionary_set(rl, "type",
668 1.3 rmind prop_number_create_integer(type));
669 1.3 rmind prop_dictionary_set(rl, "flags",
670 1.3 rmind prop_number_create_integer(flags));
671 1.3 rmind
672 1.1 rmind /* Interface and attributes. */
673 1.3 rmind attr |= (type == NPF_NATOUT) ? NPF_RULE_OUT : NPF_RULE_IN;
674 1.4 rmind npfctl_rule_setattr(rl, attr, iface, false, 0, 0);
675 1.1 rmind
676 1.3 rmind /* Translation IP, XXX should be no mask. */
677 1.3 rmind npfctl_parse_cidr(taddr, &addr, &mask);
678 1.4 rmind addrptr = prop_data_create_data(&addr, sizeof(in_addr_t));
679 1.4 rmind if (addrptr == NULL) {
680 1.4 rmind err(EXIT_FAILURE, "prop_data_create_data");
681 1.4 rmind }
682 1.4 rmind prop_dictionary_set(rl, "translation-ip", addrptr);
683 1.3 rmind
684 1.3 rmind /* Translation port (for redirect case). */
685 1.3 rmind if (rport) {
686 1.3 rmind in_port_t port;
687 1.3 rmind bool range;
688 1.3 rmind
689 1.3 rmind if (!npfctl_parse_port(rport, &range, &port, &port)) {
690 1.3 rmind errx(EXIT_FAILURE, "invalid service '%s'", rport);
691 1.3 rmind }
692 1.3 rmind if (range) {
693 1.3 rmind errx(EXIT_FAILURE, "range is not supported for 'rdr'");
694 1.3 rmind }
695 1.4 rmind prop_dictionary_set(rl, "translation-port",
696 1.3 rmind prop_number_create_integer(port));
697 1.3 rmind }
698 1.1 rmind }
699