npf_data.c revision 1.1 1 1.1 rmind /* $NetBSD: npf_data.c,v 1.1 2010/08/22 18:56:23 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.1 rmind #include <sys/types.h>
36 1.1 rmind #include <sys/socket.h>
37 1.1 rmind #include <sys/ioctl.h>
38 1.1 rmind #include <net/if.h>
39 1.1 rmind
40 1.1 rmind #include <arpa/inet.h>
41 1.1 rmind #include <prop/proplib.h>
42 1.1 rmind
43 1.1 rmind #include <stdlib.h>
44 1.1 rmind #include <string.h>
45 1.1 rmind #include <unistd.h>
46 1.1 rmind #include <ctype.h>
47 1.1 rmind #include <err.h>
48 1.1 rmind #include <ifaddrs.h>
49 1.1 rmind #include <netdb.h>
50 1.1 rmind #include <assert.h>
51 1.1 rmind
52 1.1 rmind #include "npfctl.h"
53 1.1 rmind
54 1.1 rmind static struct ifaddrs * ifs_list = NULL;
55 1.1 rmind
56 1.1 rmind static prop_dictionary_t npf_dict, settings_dict;
57 1.1 rmind static prop_array_t nat_arr, tables_arr, rules_arr;
58 1.1 rmind
59 1.1 rmind static pri_t gr_prio_counter = 1;
60 1.1 rmind static pri_t rl_prio_counter = 1;
61 1.1 rmind static pri_t nat_prio_counter = 1;
62 1.1 rmind
63 1.1 rmind void
64 1.1 rmind npfctl_init_data(void)
65 1.1 rmind {
66 1.1 rmind prop_number_t ver;
67 1.1 rmind
68 1.1 rmind if (getifaddrs(&ifs_list) == -1)
69 1.1 rmind err(EXIT_FAILURE, "getifaddrs");
70 1.1 rmind
71 1.1 rmind npf_dict = prop_dictionary_create();
72 1.1 rmind
73 1.1 rmind ver = prop_number_create_integer(NPF_VERSION);
74 1.1 rmind prop_dictionary_set(npf_dict, "version", ver);
75 1.1 rmind
76 1.1 rmind nat_arr = prop_array_create();
77 1.1 rmind prop_dictionary_set(npf_dict, "nat", 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.1 rmind prop_dictionary_externalize_to_file(npf_dict, "/tmp/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.1 rmind /*
108 1.1 rmind * Helper routines:
109 1.1 rmind *
110 1.1 rmind * npfctl_getif() - get interface addresses and index number from name.
111 1.1 rmind * npfctl_servname2port() - get service ports from name.
112 1.1 rmind * npfctl_parse_v4mask() - parse address/mask integers from CIDR block.
113 1.1 rmind */
114 1.1 rmind
115 1.1 rmind static struct ifaddrs *
116 1.1 rmind npfctl_getif(char *ifname, unsigned int *if_idx)
117 1.1 rmind {
118 1.1 rmind struct ifaddrs *ifent;
119 1.1 rmind struct sockaddr_in *sin;
120 1.1 rmind
121 1.1 rmind for (ifent = ifs_list; ifent != NULL; ifent = ifent->ifa_next) {
122 1.1 rmind sin = (struct sockaddr_in *)ifent->ifa_addr;
123 1.1 rmind
124 1.1 rmind if (sin->sin_family != AF_INET)
125 1.1 rmind continue;
126 1.1 rmind if (strcmp(ifent->ifa_name, ifname) == 0)
127 1.1 rmind break;
128 1.1 rmind }
129 1.1 rmind if (ifent) {
130 1.1 rmind *if_idx = if_nametoindex(ifname);
131 1.1 rmind }
132 1.1 rmind return ifent;
133 1.1 rmind }
134 1.1 rmind
135 1.1 rmind static int
136 1.1 rmind npfctl_servname2port(char *name)
137 1.1 rmind {
138 1.1 rmind struct servent *se;
139 1.1 rmind
140 1.1 rmind se = getservbyname(name, NULL);
141 1.1 rmind return se ? se->s_port : -1;
142 1.1 rmind }
143 1.1 rmind
144 1.1 rmind bool
145 1.1 rmind npfctl_parse_v4mask(char *str, in_addr_t *addr, in_addr_t *mask)
146 1.1 rmind {
147 1.1 rmind char *p = strchr(str, '/');
148 1.1 rmind u_int bits;
149 1.1 rmind
150 1.1 rmind /* In network byte order. */
151 1.1 rmind if (p) {
152 1.1 rmind *p++ = '\0';
153 1.1 rmind bits = (u_int)atoi(p);
154 1.1 rmind *mask = bits ? htonl(0xffffffff << (32 - bits)) : 0;
155 1.1 rmind } else {
156 1.1 rmind *mask = 0xffffffff;
157 1.1 rmind }
158 1.1 rmind return inet_aton(str, (struct in_addr *)addr) != 0;
159 1.1 rmind }
160 1.1 rmind
161 1.1 rmind static void
162 1.1 rmind npfctl_parse_cidr(char *str, in_addr_t *addr, in_addr_t *mask)
163 1.1 rmind {
164 1.1 rmind
165 1.1 rmind if (isalpha((unsigned char)*str)) {
166 1.1 rmind struct ifaddrs *ifa;
167 1.1 rmind struct sockaddr_in *sin;
168 1.1 rmind u_int idx;
169 1.1 rmind
170 1.1 rmind if ((ifa = npfctl_getif(str, &idx)) == NULL) {
171 1.1 rmind errx(EXIT_FAILURE, "invalid interface '%s'", str);
172 1.1 rmind }
173 1.1 rmind /* Interface address. */
174 1.1 rmind sin = (struct sockaddr_in *)ifa->ifa_addr;
175 1.1 rmind *addr = sin->sin_addr.s_addr;
176 1.1 rmind *mask = 0xffffffff;
177 1.1 rmind
178 1.1 rmind } else if (!npfctl_parse_v4mask(str, addr, mask)) {
179 1.1 rmind errx(EXIT_FAILURE, "invalid CIDR '%s'\n", str);
180 1.1 rmind }
181 1.1 rmind }
182 1.1 rmind
183 1.1 rmind /*
184 1.1 rmind * NPF table creation and construction routines.
185 1.1 rmind */
186 1.1 rmind
187 1.1 rmind prop_dictionary_t
188 1.1 rmind npfctl_lookup_table(char *tidstr)
189 1.1 rmind {
190 1.1 rmind prop_dictionary_t tl;
191 1.1 rmind prop_object_iterator_t it;
192 1.1 rmind prop_object_t obj;
193 1.1 rmind u_int tid;
194 1.1 rmind
195 1.1 rmind if ((it = prop_array_iterator(tables_arr)) == NULL)
196 1.1 rmind err(EXIT_FAILURE, "prop_array_iterator");
197 1.1 rmind
198 1.1 rmind tid = atoi(tidstr);
199 1.1 rmind while ((tl = prop_object_iterator_next(it)) != NULL) {
200 1.1 rmind obj = prop_dictionary_get(tl, "id");
201 1.1 rmind if (tid == prop_number_integer_value(obj))
202 1.1 rmind break;
203 1.1 rmind }
204 1.1 rmind return tl;
205 1.1 rmind }
206 1.1 rmind
207 1.1 rmind prop_dictionary_t
208 1.1 rmind npfctl_mk_table(void)
209 1.1 rmind {
210 1.1 rmind prop_dictionary_t tl;
211 1.1 rmind prop_array_t tlist;
212 1.1 rmind
213 1.1 rmind tl = prop_dictionary_create();
214 1.1 rmind tlist = prop_array_create();
215 1.1 rmind prop_dictionary_set(tl, "entries", tlist);
216 1.1 rmind
217 1.1 rmind return tl;
218 1.1 rmind }
219 1.1 rmind
220 1.1 rmind void
221 1.1 rmind npfctl_table_setup(prop_dictionary_t tl, char *idstr, char *typestr)
222 1.1 rmind {
223 1.1 rmind prop_number_t typenum;
224 1.1 rmind unsigned int id;
225 1.1 rmind
226 1.1 rmind id = atoi(idstr);
227 1.1 rmind /* TODO: 1. check ID range 2. check if not a duplicate */
228 1.1 rmind prop_dictionary_set(tl, "id", prop_number_create_integer(id));
229 1.1 rmind
230 1.1 rmind if (strcmp(typestr, "hash")) {
231 1.1 rmind typenum = prop_number_create_integer(NPF_TABLE_HASH);
232 1.1 rmind } else if (strcmp(typestr, "tree")) {
233 1.1 rmind typenum = prop_number_create_integer(NPF_TABLE_RBTREE);
234 1.1 rmind } else {
235 1.1 rmind errx(EXIT_FAILURE, "invalid table type '%s'\n", typestr);
236 1.1 rmind }
237 1.1 rmind prop_dictionary_set(tl, "type", typenum);
238 1.1 rmind }
239 1.1 rmind
240 1.1 rmind void
241 1.1 rmind npfctl_construct_table(prop_dictionary_t tl, char *fname)
242 1.1 rmind {
243 1.1 rmind prop_dictionary_t entdict;
244 1.1 rmind prop_array_t tblents;
245 1.1 rmind char *buf;
246 1.1 rmind FILE *fp;
247 1.1 rmind size_t n;
248 1.1 rmind int l;
249 1.1 rmind
250 1.1 rmind tblents = prop_dictionary_get(tl, "entries");
251 1.1 rmind assert(tblents != NULL);
252 1.1 rmind
253 1.1 rmind fp = fopen(fname, "r");
254 1.1 rmind if (fp == NULL) {
255 1.1 rmind err(EXIT_FAILURE, "fopen");
256 1.1 rmind }
257 1.1 rmind l = 1;
258 1.1 rmind buf = NULL;
259 1.1 rmind while (getline(&buf, &n, fp) != -1) {
260 1.1 rmind in_addr_t addr, mask;
261 1.1 rmind
262 1.1 rmind if (*buf == '\n' || *buf == '#')
263 1.1 rmind continue;
264 1.1 rmind
265 1.1 rmind /* IPv4 CIDR: a.b.c.d/mask */
266 1.1 rmind if (!npfctl_parse_v4mask(buf, &addr, &mask))
267 1.1 rmind errx(EXIT_FAILURE, "invalid table entry at line %d", l);
268 1.1 rmind
269 1.1 rmind /* Create and add table entry. */
270 1.1 rmind entdict = prop_dictionary_create();
271 1.1 rmind prop_dictionary_set(entdict, "addr",
272 1.1 rmind prop_number_create_integer(addr));
273 1.1 rmind prop_dictionary_set(entdict, "mask",
274 1.1 rmind prop_number_create_integer(mask));
275 1.1 rmind prop_array_add(tblents, entdict);
276 1.1 rmind l++;
277 1.1 rmind }
278 1.1 rmind if (buf != NULL) {
279 1.1 rmind free(buf);
280 1.1 rmind }
281 1.1 rmind }
282 1.1 rmind
283 1.1 rmind void
284 1.1 rmind npfctl_add_table(prop_dictionary_t tl)
285 1.1 rmind {
286 1.1 rmind
287 1.1 rmind prop_array_add(tables_arr, tl);
288 1.1 rmind }
289 1.1 rmind
290 1.1 rmind /*
291 1.1 rmind * npfctl_mk_rule: create a rule (or group) dictionary.
292 1.1 rmind *
293 1.1 rmind * Note: group is a rule containing subrules. It has no n-code, however.
294 1.1 rmind */
295 1.1 rmind prop_dictionary_t
296 1.1 rmind npfctl_mk_rule(bool group)
297 1.1 rmind {
298 1.1 rmind prop_dictionary_t rl;
299 1.1 rmind prop_array_t subrl;
300 1.1 rmind pri_t pri;
301 1.1 rmind
302 1.1 rmind rl = prop_dictionary_create();
303 1.1 rmind if (group) {
304 1.1 rmind subrl = prop_array_create();
305 1.1 rmind prop_dictionary_set(rl, "subrules", subrl);
306 1.1 rmind /* Give new priority, reset rule priority counter. */
307 1.1 rmind pri = gr_prio_counter++;
308 1.1 rmind rl_prio_counter = 1;
309 1.1 rmind } else {
310 1.1 rmind pri = rl_prio_counter++;
311 1.1 rmind }
312 1.1 rmind prop_dictionary_set(rl, "priority",
313 1.1 rmind prop_number_create_integer(pri));
314 1.1 rmind
315 1.1 rmind return rl;
316 1.1 rmind }
317 1.1 rmind
318 1.1 rmind void
319 1.1 rmind npfctl_add_rule(prop_dictionary_t rl, prop_dictionary_t parent)
320 1.1 rmind {
321 1.1 rmind prop_array_t rlset;
322 1.1 rmind
323 1.1 rmind if (parent) {
324 1.1 rmind rlset = prop_dictionary_get(parent, "subrules");
325 1.1 rmind assert(rlset != NULL);
326 1.1 rmind } else {
327 1.1 rmind rlset = rules_arr;
328 1.1 rmind }
329 1.1 rmind prop_array_add(rlset, rl);
330 1.1 rmind }
331 1.1 rmind
332 1.1 rmind void
333 1.1 rmind npfctl_rule_setattr(prop_dictionary_t rl, int attr, char *iface)
334 1.1 rmind {
335 1.1 rmind prop_number_t attrnum;
336 1.1 rmind
337 1.1 rmind attrnum = prop_number_create_integer(attr);
338 1.1 rmind prop_dictionary_set(rl, "attributes", attrnum);
339 1.1 rmind if (iface) {
340 1.1 rmind prop_number_t ifnum;
341 1.1 rmind unsigned int if_idx;
342 1.1 rmind
343 1.1 rmind if (npfctl_getif(iface, &if_idx) == NULL) {
344 1.1 rmind errx(EXIT_FAILURE, "invalid interface '%s'", iface);
345 1.1 rmind }
346 1.1 rmind ifnum = prop_number_create_integer(if_idx);
347 1.1 rmind prop_dictionary_set(rl, "interface", ifnum);
348 1.1 rmind }
349 1.1 rmind }
350 1.1 rmind
351 1.1 rmind /*
352 1.1 rmind * Main rule generation routines.
353 1.1 rmind */
354 1.1 rmind
355 1.1 rmind static void
356 1.1 rmind npfctl_rulenc_v4cidr(void **nc, int nblocks[], var_t *dat, bool sd)
357 1.1 rmind {
358 1.1 rmind element_t *el = dat->v_elements;
359 1.1 rmind int foff;
360 1.1 rmind
361 1.1 rmind /* If table, generate a single table matching block. */
362 1.1 rmind if (dat->v_type == VAR_TABLE) {
363 1.1 rmind u_int tid = atoi(el->e_data);
364 1.1 rmind
365 1.1 rmind nblocks[0]--;
366 1.1 rmind foff = npfctl_failure_offset(nblocks);
367 1.1 rmind npfctl_gennc_tbl(nc, foff, tid, sd);
368 1.1 rmind return;
369 1.1 rmind }
370 1.1 rmind
371 1.1 rmind /* Generate v4 CIDR matching blocks. */
372 1.1 rmind for (el = dat->v_elements; el != NULL; el = el->e_next) {
373 1.1 rmind in_addr_t addr, mask;
374 1.1 rmind
375 1.1 rmind npfctl_parse_cidr(el->e_data, &addr, &mask);
376 1.1 rmind
377 1.1 rmind nblocks[1]--;
378 1.1 rmind foff = npfctl_failure_offset(nblocks);
379 1.1 rmind npfctl_gennc_v4cidr(nc, foff, addr, mask, sd);
380 1.1 rmind }
381 1.1 rmind }
382 1.1 rmind
383 1.1 rmind static void
384 1.1 rmind npfctl_rulenc_ports(void **nc, int nblocks[], var_t *dat, bool tcpudp, bool sd)
385 1.1 rmind {
386 1.1 rmind element_t *el = dat->v_elements;
387 1.1 rmind int foff;
388 1.1 rmind
389 1.1 rmind assert(dat->v_type != VAR_TABLE);
390 1.1 rmind
391 1.1 rmind /* Generate TCP/UDP port matching blocks. */
392 1.1 rmind for (el = dat->v_elements; el != NULL; el = el->e_next) {
393 1.1 rmind int pfrom, pto;
394 1.1 rmind char *sep;
395 1.1 rmind
396 1.1 rmind if ((sep = strchr(el->e_data, ':')) != NULL) {
397 1.1 rmind /* Port range (only numeric). */
398 1.1 rmind *sep = '\0';
399 1.1 rmind }
400 1.1 rmind if (isalpha((unsigned char)*el->e_data)) {
401 1.1 rmind pfrom = npfctl_servname2port(el->e_data);
402 1.1 rmind if (pfrom == -1) {
403 1.1 rmind errx(EXIT_FAILURE, "invalid service '%s'",
404 1.1 rmind el->e_data);
405 1.1 rmind }
406 1.1 rmind } else {
407 1.1 rmind pfrom = htons(atoi(el->e_data));
408 1.1 rmind }
409 1.1 rmind pto = sep ? htons(atoi(sep + 1)) : pfrom;
410 1.1 rmind
411 1.1 rmind nblocks[0]--;
412 1.1 rmind foff = npfctl_failure_offset(nblocks);
413 1.1 rmind npfctl_gennc_ports(nc, foff, pfrom, pto, tcpudp, sd);
414 1.1 rmind }
415 1.1 rmind }
416 1.1 rmind
417 1.1 rmind static void
418 1.1 rmind npfctl_rulenc_block(void **nc, int nblocks[], var_t *cidr, var_t *ports,
419 1.1 rmind bool both, bool tcpudp, bool sd)
420 1.1 rmind {
421 1.1 rmind
422 1.1 rmind npfctl_rulenc_v4cidr(nc, nblocks, cidr, sd);
423 1.1 rmind if (ports == NULL) {
424 1.1 rmind return;
425 1.1 rmind }
426 1.1 rmind npfctl_rulenc_ports(nc, nblocks, ports, tcpudp, sd);
427 1.1 rmind if (!both) {
428 1.1 rmind return;
429 1.1 rmind }
430 1.1 rmind npfctl_rulenc_ports(nc, nblocks, ports, !tcpudp, sd);
431 1.1 rmind }
432 1.1 rmind
433 1.1 rmind void
434 1.1 rmind npfctl_rule_protodata(prop_dictionary_t rl, char *proto, var_t *from,
435 1.1 rmind var_t *fports, var_t *to, var_t *tports)
436 1.1 rmind {
437 1.1 rmind prop_data_t ncdata;
438 1.1 rmind bool icmp, tcpudp, both;
439 1.1 rmind int nblocks[2] = { 0, 0 };
440 1.1 rmind void *ncptr, *nc;
441 1.1 rmind size_t sz;
442 1.1 rmind
443 1.1 rmind /*
444 1.1 rmind * Default: both TCP and UDP.
445 1.1 rmind */
446 1.1 rmind icmp = false;
447 1.1 rmind tcpudp = true;
448 1.1 rmind both = false;
449 1.1 rmind if (proto == NULL) {
450 1.1 rmind goto skip_proto;
451 1.1 rmind }
452 1.1 rmind
453 1.1 rmind if (strcmp(proto, "icmp") == 0) {
454 1.1 rmind /* ICMP case. */
455 1.1 rmind fports = NULL;
456 1.1 rmind tports = NULL;
457 1.1 rmind icmp = true;
458 1.1 rmind nblocks[0] += 1;
459 1.1 rmind
460 1.1 rmind } else if (strcmp(proto, "tcp") == 0) {
461 1.1 rmind /* Just TCP. */
462 1.1 rmind tcpudp = true;
463 1.1 rmind
464 1.1 rmind } else if (strcmp(proto, "udp") == 0) {
465 1.1 rmind /* Just UDP. */
466 1.1 rmind tcpudp = false;
467 1.1 rmind
468 1.1 rmind } else {
469 1.1 rmind /* Default. */
470 1.1 rmind }
471 1.1 rmind skip_proto:
472 1.1 rmind
473 1.1 rmind /* Calculate how blocks to determince n-code. */
474 1.1 rmind if (from && from->v_count) {
475 1.1 rmind if (from->v_type == VAR_TABLE)
476 1.1 rmind nblocks[0] += 1;
477 1.1 rmind else
478 1.1 rmind nblocks[1] += from->v_count;
479 1.1 rmind if (fports && fports->v_count)
480 1.1 rmind nblocks[0] += fports->v_count * (both ? 2 : 1);
481 1.1 rmind }
482 1.1 rmind if (to && to->v_count) {
483 1.1 rmind if (to->v_type == VAR_TABLE)
484 1.1 rmind nblocks[0] += 1;
485 1.1 rmind else
486 1.1 rmind nblocks[1] += to->v_count;
487 1.1 rmind if (tports && tports->v_count)
488 1.1 rmind nblocks[0] += tports->v_count * (both ? 2 : 1);
489 1.1 rmind }
490 1.1 rmind
491 1.1 rmind /* Allocate memory for the n-code. */
492 1.1 rmind sz = npfctl_calc_ncsize(nblocks);
493 1.1 rmind ncptr = malloc(sz);
494 1.1 rmind if (ncptr == NULL) {
495 1.1 rmind perror("malloc");
496 1.1 rmind exit(EXIT_FAILURE);
497 1.1 rmind }
498 1.1 rmind nc = ncptr;
499 1.1 rmind
500 1.1 rmind /* Ethernet fragment (ETHERTYPE_IP), XXX. */
501 1.1 rmind npfctl_gennc_ether(&nc, npfctl_failure_offset(nblocks), htons(0x0800));
502 1.1 rmind
503 1.1 rmind /* Generate v4 CIDR matching blocks and TCP/UDP port matching. */
504 1.1 rmind if (from) {
505 1.1 rmind npfctl_rulenc_block(&nc, nblocks, from, fports,
506 1.1 rmind both, tcpudp, true);
507 1.1 rmind }
508 1.1 rmind if (to) {
509 1.1 rmind npfctl_rulenc_block(&nc, nblocks, to, tports,
510 1.1 rmind both, tcpudp, false);
511 1.1 rmind }
512 1.1 rmind /* ICMP case. */
513 1.1 rmind if (icmp) {
514 1.1 rmind const int foff = npfctl_failure_offset(nblocks);
515 1.1 rmind npfctl_gennc_icmp(&nc, foff, -1, -1);
516 1.1 rmind }
517 1.1 rmind npfctl_gennc_complete(&nc);
518 1.1 rmind
519 1.1 rmind if ((uintptr_t)nc - (uintptr_t)ncptr != sz)
520 1.1 rmind errx(EXIT_FAILURE, "n-code size got wrong (%lu != %lu)",
521 1.1 rmind (uintptr_t)nc - (uintptr_t)ncptr, sz);
522 1.1 rmind
523 1.1 rmind #ifdef DEBUG
524 1.1 rmind uint32_t *op = ncptr;
525 1.1 rmind size_t n = sz;
526 1.1 rmind do {
527 1.1 rmind DPRINTF(("\t> |0x%02x|\n", (u_int)*op));
528 1.1 rmind op++;
529 1.1 rmind n -= sizeof(*op);
530 1.1 rmind } while (n);
531 1.1 rmind #endif
532 1.1 rmind
533 1.1 rmind /* Create a final memory block of data, ready to send. */
534 1.1 rmind ncdata = prop_data_create_data(ncptr, sz);
535 1.1 rmind if (ncdata == NULL) {
536 1.1 rmind perror("prop_data_create_data");
537 1.1 rmind exit(EXIT_FAILURE);
538 1.1 rmind }
539 1.1 rmind prop_dictionary_set(rl, "ncode", ncdata);
540 1.1 rmind free(ncptr);
541 1.1 rmind }
542 1.1 rmind
543 1.1 rmind /*
544 1.1 rmind * NAT policy construction routines.
545 1.1 rmind */
546 1.1 rmind
547 1.1 rmind prop_dictionary_t
548 1.1 rmind npfctl_mk_nat(void)
549 1.1 rmind {
550 1.1 rmind prop_dictionary_t rl;
551 1.1 rmind pri_t pri;
552 1.1 rmind
553 1.1 rmind /* NAT policy is rule with extra info. */
554 1.1 rmind rl = prop_dictionary_create();
555 1.1 rmind pri = nat_prio_counter++;
556 1.1 rmind prop_dictionary_set(rl, "priority",
557 1.1 rmind prop_number_create_integer(pri));
558 1.1 rmind return rl;
559 1.1 rmind }
560 1.1 rmind
561 1.1 rmind void
562 1.1 rmind npfctl_add_nat(prop_dictionary_t nat)
563 1.1 rmind {
564 1.1 rmind prop_array_add(nat_arr, nat);
565 1.1 rmind }
566 1.1 rmind
567 1.1 rmind void
568 1.1 rmind npfctl_nat_setup(prop_dictionary_t rl, char *iface, char *gwip)
569 1.1 rmind {
570 1.1 rmind const int attr = NPF_RULE_PASS | NPF_RULE_OUT | NPF_RULE_FINAL;
571 1.1 rmind in_addr_t addr, mask;
572 1.1 rmind
573 1.1 rmind /* Interface and attributes. */
574 1.1 rmind npfctl_rule_setattr(rl, attr, iface);
575 1.1 rmind
576 1.1 rmind /* Gateway IP, XXX should be no mask. */
577 1.1 rmind npfctl_parse_cidr(gwip, &addr, &mask);
578 1.1 rmind prop_dictionary_set(rl, "gateway_ip", prop_number_create_integer(addr));
579 1.1 rmind }
580