npf_data.c revision 1.8 1 1.8 zoltan /* $NetBSD: npf_data.c,v 1.8 2011/11/04 01:00:28 zoltan Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.6 rmind * Copyright (c) 2009-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 /*
30 1.7 rmind * npfctl(8) helper routines.
31 1.1 rmind */
32 1.1 rmind
33 1.4 rmind #include <sys/cdefs.h>
34 1.8 zoltan __RCSID("$NetBSD: npf_data.c,v 1.8 2011/11/04 01:00:28 zoltan Exp $");
35 1.4 rmind
36 1.1 rmind #include <sys/types.h>
37 1.1 rmind #include <sys/socket.h>
38 1.1 rmind #include <sys/ioctl.h>
39 1.1 rmind #include <net/if.h>
40 1.3 rmind #include <netinet/tcp.h>
41 1.1 rmind
42 1.1 rmind #include <arpa/inet.h>
43 1.1 rmind
44 1.1 rmind #include <stdlib.h>
45 1.1 rmind #include <string.h>
46 1.1 rmind #include <unistd.h>
47 1.1 rmind #include <ctype.h>
48 1.1 rmind #include <err.h>
49 1.1 rmind #include <ifaddrs.h>
50 1.1 rmind #include <netdb.h>
51 1.1 rmind #include <assert.h>
52 1.1 rmind
53 1.1 rmind #include "npfctl.h"
54 1.1 rmind
55 1.1 rmind static struct ifaddrs * ifs_list = NULL;
56 1.7 rmind nl_config_t * npf_conf = NULL;
57 1.1 rmind
58 1.1 rmind void
59 1.1 rmind npfctl_init_data(void)
60 1.1 rmind {
61 1.1 rmind
62 1.7 rmind npf_conf = npf_config_create();
63 1.7 rmind if (npf_conf == NULL) {
64 1.7 rmind errx(EXIT_FAILURE, "npf_config_create");
65 1.7 rmind }
66 1.7 rmind if (getifaddrs(&ifs_list) == -1) {
67 1.1 rmind err(EXIT_FAILURE, "getifaddrs");
68 1.7 rmind }
69 1.1 rmind }
70 1.1 rmind
71 1.1 rmind int
72 1.1 rmind npfctl_ioctl_send(int fd)
73 1.1 rmind {
74 1.7 rmind int error = npf_config_submit(npf_conf, fd);
75 1.7 rmind npf_config_destroy(npf_conf);
76 1.7 rmind return error;
77 1.5 rmind }
78 1.5 rmind
79 1.1 rmind /*
80 1.1 rmind * Helper routines:
81 1.1 rmind *
82 1.1 rmind * npfctl_getif() - get interface addresses and index number from name.
83 1.1 rmind * npfctl_parse_v4mask() - parse address/mask integers from CIDR block.
84 1.3 rmind * npfctl_parse_port() - parse port number (which may be a service name).
85 1.3 rmind * npfctl_parse_tcpfl() - parse TCP flags.
86 1.1 rmind */
87 1.1 rmind
88 1.6 rmind struct ifaddrs *
89 1.8 zoltan npfctl_getif(char *ifname, unsigned int *if_idx, bool reqaddr, sa_family_t addrtype)
90 1.1 rmind {
91 1.1 rmind struct ifaddrs *ifent;
92 1.1 rmind struct sockaddr_in *sin;
93 1.1 rmind
94 1.1 rmind for (ifent = ifs_list; ifent != NULL; ifent = ifent->ifa_next) {
95 1.1 rmind sin = (struct sockaddr_in *)ifent->ifa_addr;
96 1.8 zoltan if (sin->sin_family != addrtype && reqaddr)
97 1.1 rmind continue;
98 1.1 rmind if (strcmp(ifent->ifa_name, ifname) == 0)
99 1.1 rmind break;
100 1.1 rmind }
101 1.1 rmind if (ifent) {
102 1.1 rmind *if_idx = if_nametoindex(ifname);
103 1.1 rmind }
104 1.1 rmind return ifent;
105 1.1 rmind }
106 1.1 rmind
107 1.1 rmind bool
108 1.3 rmind npfctl_parse_port(char *ostr, bool *range, in_port_t *fport, in_port_t *tport)
109 1.3 rmind {
110 1.3 rmind char *str = xstrdup(ostr), *sep;
111 1.3 rmind
112 1.3 rmind *range = false;
113 1.3 rmind if ((sep = strchr(str, ':')) != NULL) {
114 1.3 rmind /* Port range (only numeric). */
115 1.3 rmind *range = true;
116 1.3 rmind *sep = '\0';
117 1.3 rmind
118 1.3 rmind } else if (isalpha((unsigned char)*str)) {
119 1.3 rmind struct servent *se;
120 1.3 rmind
121 1.3 rmind se = getservbyname(str, NULL);
122 1.3 rmind if (se == NULL) {
123 1.3 rmind free(str);
124 1.3 rmind return false;
125 1.3 rmind }
126 1.3 rmind *fport = se->s_port;
127 1.3 rmind } else {
128 1.3 rmind *fport = htons(atoi(str));
129 1.3 rmind }
130 1.3 rmind *tport = sep ? htons(atoi(sep + 1)) : *fport;
131 1.3 rmind free(str);
132 1.3 rmind return true;
133 1.1 rmind }
134 1.1 rmind
135 1.7 rmind void
136 1.8 zoltan npfctl_create_mask(sa_family_t family, u_int length, npf_addr_t *omask)
137 1.8 zoltan {
138 1.8 zoltan uint32_t part;
139 1.8 zoltan uint32_t *mask = (uint32_t*)omask;
140 1.8 zoltan
141 1.8 zoltan memset(omask, 0, sizeof(npf_addr_t));
142 1.8 zoltan if (family == AF_INET) {
143 1.8 zoltan part = htonl(0xffffffff << (32 - length));
144 1.8 zoltan memcpy(mask, &part, 4);
145 1.8 zoltan } else if (family == AF_INET6) {
146 1.8 zoltan while (length > 32) {
147 1.8 zoltan part = htonl(0xffffffff);
148 1.8 zoltan memcpy(mask, &part, 4);
149 1.8 zoltan mask += 1;
150 1.8 zoltan length -= 32;
151 1.8 zoltan }
152 1.8 zoltan part = htonl(0xffffffff << (32 - length));
153 1.8 zoltan memcpy(mask, &part, 4);
154 1.8 zoltan }
155 1.8 zoltan }
156 1.8 zoltan
157 1.8 zoltan sa_family_t
158 1.8 zoltan npfctl_get_addrfamily(const char *ostr)
159 1.8 zoltan {
160 1.8 zoltan struct addrinfo hint, *res = NULL;
161 1.8 zoltan int ret;
162 1.8 zoltan char *str = xstrdup(ostr);
163 1.8 zoltan char *p = strchr(str, '/');
164 1.8 zoltan sa_family_t family;
165 1.8 zoltan
166 1.8 zoltan if (p)
167 1.8 zoltan *p = '\0';
168 1.8 zoltan memset(&hint, '\0', sizeof(hint));
169 1.8 zoltan hint.ai_family = PF_UNSPEC;
170 1.8 zoltan hint.ai_flags = AI_NUMERICHOST;
171 1.8 zoltan ret = getaddrinfo(str, NULL, &hint, &res);
172 1.8 zoltan if (ret) {
173 1.8 zoltan family = AF_UNSPEC;
174 1.8 zoltan } else {
175 1.8 zoltan family = res->ai_family;
176 1.8 zoltan }
177 1.8 zoltan freeaddrinfo(res);
178 1.8 zoltan free(str);
179 1.8 zoltan return family;
180 1.8 zoltan }
181 1.8 zoltan
182 1.8 zoltan sa_family_t
183 1.8 zoltan npfctl_parse_cidr(char *str, sa_family_t addrfamily, npf_addr_t *addr, npf_netmask_t *mask)
184 1.1 rmind {
185 1.1 rmind
186 1.6 rmind if (strcmp(str, "any") == 0) {
187 1.8 zoltan memset(addr, 0, sizeof(npf_addr_t));
188 1.8 zoltan memset(mask, 0, sizeof(npf_netmask_t));
189 1.6 rmind } else if (isalpha((unsigned char)*str)) {
190 1.8 zoltan /* TODO: handle multiple addresses per interface */
191 1.1 rmind struct ifaddrs *ifa;
192 1.1 rmind struct sockaddr_in *sin;
193 1.1 rmind u_int idx;
194 1.8 zoltan if ((ifa = npfctl_getif(str, &idx, true, AF_INET)) == NULL) {
195 1.1 rmind errx(EXIT_FAILURE, "invalid interface '%s'", str);
196 1.1 rmind }
197 1.1 rmind /* Interface address. */
198 1.1 rmind sin = (struct sockaddr_in *)ifa->ifa_addr;
199 1.8 zoltan memcpy(addr, &(sin->sin_addr.s_addr), sizeof(struct in_addr));
200 1.8 zoltan //v4mask = 0xffffffff; - TODO!
201 1.8 zoltan } else {
202 1.8 zoltan char *p = strchr(str, '/');
203 1.8 zoltan if (p != NULL) {
204 1.8 zoltan *p++ = '\0';
205 1.8 zoltan *mask = atoi(p);
206 1.8 zoltan } else {
207 1.8 zoltan if (addrfamily == AF_INET)
208 1.8 zoltan *mask = 32;
209 1.8 zoltan else
210 1.8 zoltan *mask = 128;
211 1.8 zoltan }
212 1.8 zoltan memset(addr, 0, sizeof(npf_addr_t));
213 1.8 zoltan int ret = inet_pton(addrfamily, str, addr);
214 1.8 zoltan if (ret != 1) {
215 1.8 zoltan printf("TODO: error");
216 1.8 zoltan }
217 1.8 zoltan }
218 1.1 rmind
219 1.8 zoltan return addrfamily;
220 1.1 rmind }
221 1.1 rmind
222 1.3 rmind static bool
223 1.3 rmind npfctl_parse_tcpfl(char *s, uint8_t *tfl, uint8_t *tfl_mask)
224 1.3 rmind {
225 1.3 rmind uint8_t tcpfl = 0;
226 1.3 rmind bool mask = false;
227 1.3 rmind
228 1.3 rmind while (*s) {
229 1.3 rmind switch (*s) {
230 1.3 rmind case 'F': tcpfl |= TH_FIN; break;
231 1.3 rmind case 'S': tcpfl |= TH_SYN; break;
232 1.3 rmind case 'R': tcpfl |= TH_RST; break;
233 1.3 rmind case 'P': tcpfl |= TH_PUSH; break;
234 1.3 rmind case 'A': tcpfl |= TH_ACK; break;
235 1.3 rmind case 'U': tcpfl |= TH_URG; break;
236 1.3 rmind case 'E': tcpfl |= TH_ECE; break;
237 1.3 rmind case 'W': tcpfl |= TH_CWR; break;
238 1.3 rmind case '/':
239 1.3 rmind *s = '\0';
240 1.3 rmind *tfl = tcpfl;
241 1.3 rmind tcpfl = 0;
242 1.3 rmind mask = true;
243 1.3 rmind break;
244 1.3 rmind default:
245 1.3 rmind return false;
246 1.3 rmind }
247 1.3 rmind s++;
248 1.3 rmind }
249 1.3 rmind if (!mask) {
250 1.3 rmind *tfl = tcpfl;
251 1.3 rmind }
252 1.3 rmind *tfl_mask = tcpfl;
253 1.3 rmind return true;
254 1.3 rmind }
255 1.3 rmind
256 1.1 rmind void
257 1.7 rmind npfctl_fill_table(nl_table_t *tl, char *fname)
258 1.1 rmind {
259 1.1 rmind char *buf;
260 1.1 rmind FILE *fp;
261 1.1 rmind size_t n;
262 1.1 rmind int l;
263 1.1 rmind
264 1.1 rmind fp = fopen(fname, "r");
265 1.1 rmind if (fp == NULL) {
266 1.6 rmind err(EXIT_FAILURE, "open '%s'", fname);
267 1.1 rmind }
268 1.1 rmind l = 1;
269 1.1 rmind buf = NULL;
270 1.1 rmind while (getline(&buf, &n, fp) != -1) {
271 1.8 zoltan npf_addr_t addr;
272 1.8 zoltan npf_netmask_t mask;
273 1.1 rmind
274 1.1 rmind if (*buf == '\n' || *buf == '#')
275 1.1 rmind continue;
276 1.1 rmind
277 1.8 zoltan if (!npfctl_parse_cidr(buf, npfctl_get_addrfamily(buf), &addr, &mask)) {
278 1.1 rmind errx(EXIT_FAILURE, "invalid table entry at line %d", l);
279 1.7 rmind }
280 1.1 rmind
281 1.1 rmind /* Create and add table entry. */
282 1.8 zoltan npf_table_add_entry(tl, &addr, mask);
283 1.1 rmind l++;
284 1.1 rmind }
285 1.1 rmind if (buf != NULL) {
286 1.1 rmind free(buf);
287 1.1 rmind }
288 1.1 rmind }
289 1.1 rmind
290 1.1 rmind /*
291 1.7 rmind * N-code generation helpers.
292 1.1 rmind */
293 1.1 rmind
294 1.1 rmind static void
295 1.8 zoltan npfctl_rulenc_cidr(void **nc, int nblocks[], var_t *dat, bool sd, sa_family_t addrfamily)
296 1.1 rmind {
297 1.1 rmind element_t *el = dat->v_elements;
298 1.1 rmind int foff;
299 1.1 rmind
300 1.1 rmind /* If table, generate a single table matching block. */
301 1.1 rmind if (dat->v_type == VAR_TABLE) {
302 1.1 rmind u_int tid = atoi(el->e_data);
303 1.1 rmind
304 1.1 rmind nblocks[0]--;
305 1.1 rmind foff = npfctl_failure_offset(nblocks);
306 1.1 rmind npfctl_gennc_tbl(nc, foff, tid, sd);
307 1.1 rmind return;
308 1.1 rmind }
309 1.1 rmind
310 1.8 zoltan /* Generate v4/v6 CIDR matching blocks. */
311 1.1 rmind for (el = dat->v_elements; el != NULL; el = el->e_next) {
312 1.8 zoltan npf_addr_t addr;
313 1.8 zoltan npf_netmask_t mask;
314 1.1 rmind
315 1.8 zoltan npfctl_parse_cidr(el->e_data, addrfamily, &addr, &mask);
316 1.8 zoltan if (addrfamily == AF_INET)
317 1.8 zoltan nblocks[1]--;
318 1.8 zoltan else if (addrfamily == AF_INET6)
319 1.8 zoltan nblocks[3]--;
320 1.1 rmind foff = npfctl_failure_offset(nblocks);
321 1.8 zoltan if (addrfamily == AF_INET)
322 1.8 zoltan npfctl_gennc_v4cidr(nc, foff, &addr, mask, sd);
323 1.8 zoltan else if (addrfamily == AF_INET6)
324 1.8 zoltan npfctl_gennc_v6cidr(nc, foff, &addr, mask, sd);
325 1.1 rmind }
326 1.1 rmind }
327 1.1 rmind
328 1.1 rmind static void
329 1.5 rmind npfctl_rulenc_ports(void **nc, int nblocks[], var_t *dat, bool tcpudp,
330 1.5 rmind bool both, bool sd)
331 1.1 rmind {
332 1.1 rmind element_t *el = dat->v_elements;
333 1.1 rmind int foff;
334 1.1 rmind
335 1.1 rmind assert(dat->v_type != VAR_TABLE);
336 1.1 rmind
337 1.1 rmind /* Generate TCP/UDP port matching blocks. */
338 1.1 rmind for (el = dat->v_elements; el != NULL; el = el->e_next) {
339 1.3 rmind in_port_t fport, tport;
340 1.3 rmind bool range;
341 1.1 rmind
342 1.3 rmind if (!npfctl_parse_port(el->e_data, &range, &fport, &tport)) {
343 1.3 rmind errx(EXIT_FAILURE, "invalid service '%s'", el->e_data);
344 1.1 rmind }
345 1.1 rmind nblocks[0]--;
346 1.5 rmind foff = both ? 0 : npfctl_failure_offset(nblocks);
347 1.3 rmind npfctl_gennc_ports(nc, foff, fport, tport, tcpudp, sd);
348 1.1 rmind }
349 1.1 rmind }
350 1.1 rmind
351 1.1 rmind static void
352 1.1 rmind npfctl_rulenc_block(void **nc, int nblocks[], var_t *cidr, var_t *ports,
353 1.8 zoltan bool both, bool tcpudp, bool sd, sa_family_t addrfamily)
354 1.1 rmind {
355 1.1 rmind
356 1.8 zoltan npfctl_rulenc_cidr(nc, nblocks, cidr, sd, addrfamily);
357 1.1 rmind if (ports == NULL) {
358 1.1 rmind return;
359 1.1 rmind }
360 1.5 rmind npfctl_rulenc_ports(nc, nblocks, ports, tcpudp, both, sd);
361 1.1 rmind if (!both) {
362 1.1 rmind return;
363 1.1 rmind }
364 1.5 rmind npfctl_rulenc_ports(nc, nblocks, ports, !tcpudp, false, sd);
365 1.1 rmind }
366 1.1 rmind
367 1.1 rmind void
368 1.7 rmind npfctl_rule_ncode(nl_rule_t *rl, char *proto, char *tcpfl, int icmp_type,
369 1.8 zoltan int icmp_code, var_t *from, sa_family_t addrfamily, var_t *fports, var_t *to, var_t *tports)
370 1.1 rmind {
371 1.8 zoltan int nblocks[4] = { 0, 0, 0, 0 };
372 1.1 rmind bool icmp, tcpudp, both;
373 1.1 rmind void *ncptr, *nc;
374 1.7 rmind size_t sz, foff;
375 1.1 rmind
376 1.1 rmind /*
377 1.1 rmind * Default: both TCP and UDP.
378 1.1 rmind */
379 1.1 rmind icmp = false;
380 1.1 rmind tcpudp = true;
381 1.1 rmind if (proto == NULL) {
382 1.5 rmind both = true;
383 1.1 rmind goto skip_proto;
384 1.1 rmind }
385 1.5 rmind both = false;
386 1.1 rmind
387 1.1 rmind if (strcmp(proto, "icmp") == 0) {
388 1.1 rmind /* ICMP case. */
389 1.1 rmind fports = NULL;
390 1.1 rmind tports = NULL;
391 1.1 rmind icmp = true;
392 1.1 rmind
393 1.1 rmind } else if (strcmp(proto, "tcp") == 0) {
394 1.1 rmind /* Just TCP. */
395 1.1 rmind tcpudp = true;
396 1.1 rmind
397 1.1 rmind } else if (strcmp(proto, "udp") == 0) {
398 1.1 rmind /* Just UDP. */
399 1.1 rmind tcpudp = false;
400 1.1 rmind
401 1.1 rmind } else {
402 1.1 rmind /* Default. */
403 1.1 rmind }
404 1.1 rmind skip_proto:
405 1.6 rmind if (icmp || icmp_type != -1) {
406 1.7 rmind assert(tcpfl == NULL);
407 1.3 rmind icmp = true;
408 1.3 rmind nblocks[2] += 1;
409 1.3 rmind }
410 1.7 rmind if (tcpudp && tcpfl) {
411 1.3 rmind assert(icmp_type == -1 && icmp_code == -1);
412 1.3 rmind nblocks[2] += 1;
413 1.3 rmind }
414 1.1 rmind
415 1.1 rmind /* Calculate how blocks to determince n-code. */
416 1.1 rmind if (from && from->v_count) {
417 1.1 rmind if (from->v_type == VAR_TABLE)
418 1.1 rmind nblocks[0] += 1;
419 1.8 zoltan else {
420 1.8 zoltan if (addrfamily == AF_INET)
421 1.8 zoltan nblocks[1] += from->v_count;
422 1.8 zoltan else
423 1.8 zoltan nblocks[3] += from->v_count;
424 1.8 zoltan }
425 1.1 rmind if (fports && fports->v_count)
426 1.1 rmind nblocks[0] += fports->v_count * (both ? 2 : 1);
427 1.1 rmind }
428 1.1 rmind if (to && to->v_count) {
429 1.1 rmind if (to->v_type == VAR_TABLE)
430 1.1 rmind nblocks[0] += 1;
431 1.8 zoltan else {
432 1.8 zoltan if (addrfamily == AF_INET)
433 1.8 zoltan nblocks[1] += to->v_count;
434 1.8 zoltan else
435 1.8 zoltan nblocks[3] += to->v_count;
436 1.8 zoltan }
437 1.1 rmind if (tports && tports->v_count)
438 1.1 rmind nblocks[0] += tports->v_count * (both ? 2 : 1);
439 1.1 rmind }
440 1.1 rmind
441 1.3 rmind /* Any n-code to generate? */
442 1.8 zoltan if (!icmp && (nblocks[0] + nblocks[1] + nblocks[2] + nblocks[3]) == 0) {
443 1.3 rmind /* Done, if none. */
444 1.3 rmind return;
445 1.3 rmind }
446 1.3 rmind
447 1.1 rmind /* Allocate memory for the n-code. */
448 1.1 rmind sz = npfctl_calc_ncsize(nblocks);
449 1.1 rmind ncptr = malloc(sz);
450 1.1 rmind if (ncptr == NULL) {
451 1.7 rmind err(EXIT_FAILURE, "malloc");
452 1.1 rmind }
453 1.1 rmind nc = ncptr;
454 1.1 rmind
455 1.3 rmind /*
456 1.8 zoltan * Generate v4/v6 CIDR matching blocks and TCP/UDP port matching.
457 1.3 rmind */
458 1.1 rmind if (from) {
459 1.1 rmind npfctl_rulenc_block(&nc, nblocks, from, fports,
460 1.8 zoltan both, tcpudp, true, addrfamily);
461 1.1 rmind }
462 1.1 rmind if (to) {
463 1.1 rmind npfctl_rulenc_block(&nc, nblocks, to, tports,
464 1.8 zoltan both, tcpudp, false, addrfamily);
465 1.1 rmind }
466 1.3 rmind
467 1.1 rmind if (icmp) {
468 1.3 rmind /*
469 1.3 rmind * ICMP case.
470 1.3 rmind */
471 1.3 rmind nblocks[2]--;
472 1.3 rmind foff = npfctl_failure_offset(nblocks);
473 1.3 rmind npfctl_gennc_icmp(&nc, foff, icmp_type, icmp_code);
474 1.3 rmind
475 1.7 rmind } else if (tcpudp && tcpfl) {
476 1.3 rmind /*
477 1.3 rmind * TCP case, flags.
478 1.3 rmind */
479 1.3 rmind uint8_t tfl = 0, tfl_mask;
480 1.3 rmind
481 1.3 rmind nblocks[2]--;
482 1.3 rmind foff = npfctl_failure_offset(nblocks);
483 1.7 rmind if (!npfctl_parse_tcpfl(tcpfl, &tfl, &tfl_mask)) {
484 1.7 rmind errx(EXIT_FAILURE, "invalid TCP flags '%s'", tcpfl);
485 1.3 rmind }
486 1.3 rmind npfctl_gennc_tcpfl(&nc, foff, tfl, tfl_mask);
487 1.1 rmind }
488 1.1 rmind npfctl_gennc_complete(&nc);
489 1.1 rmind
490 1.3 rmind if ((uintptr_t)nc - (uintptr_t)ncptr != sz) {
491 1.2 jnemeth errx(EXIT_FAILURE, "n-code size got wrong (%tu != %zu)",
492 1.1 rmind (uintptr_t)nc - (uintptr_t)ncptr, sz);
493 1.3 rmind }
494 1.1 rmind
495 1.1 rmind #ifdef DEBUG
496 1.1 rmind uint32_t *op = ncptr;
497 1.1 rmind size_t n = sz;
498 1.1 rmind do {
499 1.1 rmind DPRINTF(("\t> |0x%02x|\n", (u_int)*op));
500 1.1 rmind op++;
501 1.1 rmind n -= sizeof(*op);
502 1.1 rmind } while (n);
503 1.1 rmind #endif
504 1.1 rmind
505 1.1 rmind /* Create a final memory block of data, ready to send. */
506 1.7 rmind if (npf_rule_setcode(rl, NPF_CODE_NCODE, ncptr, sz) == -1) {
507 1.7 rmind errx(EXIT_FAILURE, "npf_rule_setcode");
508 1.1 rmind }
509 1.1 rmind free(ncptr);
510 1.1 rmind }
511