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