npf_data.c revision 1.26 1 1.26 christos /* $NetBSD: npf_data.c,v 1.26 2016/12/26 23:05:05 christos Exp $ */
2 1.1 rmind
3 1.1 rmind /*-
4 1.25 rmind * Copyright (c) 2009-2014 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.10 rmind * npfctl(8) data manipulation and helper routines.
31 1.1 rmind */
32 1.1 rmind
33 1.4 rmind #include <sys/cdefs.h>
34 1.26 christos __RCSID("$NetBSD: npf_data.c,v 1.26 2016/12/26 23:05:05 christos Exp $");
35 1.4 rmind
36 1.1 rmind #include <sys/types.h>
37 1.10 rmind #include <sys/null.h>
38 1.10 rmind #include <netinet/in.h>
39 1.10 rmind #include <netinet/in_systm.h>
40 1.10 rmind #include <netinet/ip.h>
41 1.10 rmind #define ICMP_STRINGS
42 1.10 rmind #include <netinet/ip_icmp.h>
43 1.16 spz #define ICMP6_STRINGS
44 1.16 spz #include <netinet/icmp6.h>
45 1.26 christos #define __FAVOR_BSD
46 1.10 rmind #include <netinet/tcp.h>
47 1.1 rmind #include <net/if.h>
48 1.1 rmind
49 1.1 rmind #include <stdlib.h>
50 1.21 rmind #include <stddef.h>
51 1.1 rmind #include <string.h>
52 1.21 rmind #include <ctype.h>
53 1.1 rmind #include <err.h>
54 1.10 rmind #include <errno.h>
55 1.1 rmind #include <ifaddrs.h>
56 1.1 rmind #include <netdb.h>
57 1.1 rmind
58 1.1 rmind #include "npfctl.h"
59 1.1 rmind
60 1.1 rmind static struct ifaddrs * ifs_list = NULL;
61 1.1 rmind
62 1.21 rmind void
63 1.21 rmind npfctl_note_interface(const char *ifname)
64 1.21 rmind {
65 1.21 rmind unsigned long if_idx = if_nametoindex(ifname);
66 1.21 rmind bool testif = npfctl_debug_addif(ifname);
67 1.21 rmind const char *p = ifname;
68 1.21 rmind
69 1.21 rmind /* If such interface exists or if it is a test interface - done. */
70 1.21 rmind if (if_idx || testif) {
71 1.21 rmind return;
72 1.21 rmind }
73 1.21 rmind
74 1.21 rmind /*
75 1.21 rmind * Minimum sanity check. The interface name shall be non-empty
76 1.21 rmind * string shorter than IFNAMSIZ and alphanumeric only.
77 1.21 rmind */
78 1.21 rmind if (*p == '\0') {
79 1.21 rmind goto invalid;
80 1.21 rmind }
81 1.21 rmind while (*p) {
82 1.21 rmind const size_t len = (ptrdiff_t)p - (ptrdiff_t)ifname;
83 1.21 rmind
84 1.21 rmind if (!isalnum((unsigned char)*p) || len > IFNAMSIZ) {
85 1.21 rmind invalid: yyerror("illegitimate interface name '%s'", ifname);
86 1.21 rmind }
87 1.21 rmind p++;
88 1.21 rmind }
89 1.21 rmind
90 1.21 rmind /* Throw a warning, so that the user could double check. */
91 1.21 rmind warnx("warning - unknown interface '%s'", ifname);
92 1.21 rmind }
93 1.21 rmind
94 1.21 rmind static unsigned long
95 1.10 rmind npfctl_find_ifindex(const char *ifname)
96 1.1 rmind {
97 1.18 rmind unsigned long if_idx = if_nametoindex(ifname);
98 1.21 rmind bool testif = npfctl_debug_addif(ifname);
99 1.18 rmind
100 1.18 rmind if (!if_idx) {
101 1.21 rmind if (testif) {
102 1.21 rmind static u_int dummy_if_idx = (1 << 15);
103 1.21 rmind return ++dummy_if_idx;
104 1.18 rmind }
105 1.18 rmind yyerror("unknown interface '%s'", ifname);
106 1.18 rmind }
107 1.18 rmind return if_idx;
108 1.1 rmind }
109 1.1 rmind
110 1.10 rmind static bool
111 1.10 rmind npfctl_copy_address(sa_family_t fam, npf_addr_t *addr, const void *ptr)
112 1.1 rmind {
113 1.14 rmind memset(addr, 0, sizeof(npf_addr_t));
114 1.14 rmind
115 1.10 rmind switch (fam) {
116 1.10 rmind case AF_INET: {
117 1.10 rmind const struct sockaddr_in *sin = ptr;
118 1.10 rmind memcpy(addr, &sin->sin_addr, sizeof(sin->sin_addr));
119 1.10 rmind return true;
120 1.10 rmind }
121 1.10 rmind case AF_INET6: {
122 1.10 rmind const struct sockaddr_in6 *sin6 = ptr;
123 1.10 rmind memcpy(addr, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
124 1.10 rmind return true;
125 1.10 rmind }
126 1.10 rmind default:
127 1.10 rmind yyerror("unknown address family %u", fam);
128 1.10 rmind return false;
129 1.10 rmind }
130 1.5 rmind }
131 1.5 rmind
132 1.10 rmind static bool
133 1.10 rmind npfctl_parse_fam_addr(const char *name, sa_family_t *fam, npf_addr_t *addr)
134 1.1 rmind {
135 1.10 rmind static const struct addrinfo hint = {
136 1.10 rmind .ai_family = AF_UNSPEC,
137 1.10 rmind .ai_flags = AI_NUMERICHOST
138 1.10 rmind };
139 1.10 rmind struct addrinfo *ai;
140 1.10 rmind int ret;
141 1.1 rmind
142 1.10 rmind ret = getaddrinfo(name, NULL, &hint, &ai);
143 1.10 rmind if (ret) {
144 1.10 rmind yyerror("cannot parse '%s' (%s)", name, gai_strerror(ret));
145 1.10 rmind return false;
146 1.10 rmind }
147 1.10 rmind if (fam) {
148 1.10 rmind *fam = ai->ai_family;
149 1.1 rmind }
150 1.10 rmind if (!npfctl_copy_address(*fam, addr, ai->ai_addr)) {
151 1.10 rmind return false;
152 1.1 rmind }
153 1.10 rmind freeaddrinfo(ai);
154 1.10 rmind return true;
155 1.1 rmind }
156 1.1 rmind
157 1.10 rmind static bool
158 1.10 rmind npfctl_parse_mask(const char *s, sa_family_t fam, npf_netmask_t *mask)
159 1.3 rmind {
160 1.10 rmind char *ep = NULL;
161 1.10 rmind npf_addr_t addr;
162 1.10 rmind uint8_t *ap;
163 1.10 rmind
164 1.10 rmind if (s) {
165 1.10 rmind errno = 0;
166 1.10 rmind *mask = (npf_netmask_t)strtol(s, &ep, 0);
167 1.10 rmind if (*ep == '\0' && s != ep && errno != ERANGE)
168 1.10 rmind return true;
169 1.10 rmind if (!npfctl_parse_fam_addr(s, &fam, &addr))
170 1.10 rmind return false;
171 1.10 rmind }
172 1.3 rmind
173 1.15 rmind assert(fam == AF_INET || fam == AF_INET6);
174 1.15 rmind *mask = NPF_NO_NETMASK;
175 1.10 rmind if (ep == NULL) {
176 1.10 rmind return true;
177 1.10 rmind }
178 1.15 rmind
179 1.26 christos ap = addr.word8 + (*mask / 8) - 1;
180 1.26 christos while (ap >= addr.word8) {
181 1.10 rmind for (int j = 8; j > 0; j--) {
182 1.10 rmind if (*ap & 1)
183 1.10 rmind return true;
184 1.10 rmind *ap >>= 1;
185 1.10 rmind (*mask)--;
186 1.10 rmind if (*mask == 0)
187 1.10 rmind return true;
188 1.3 rmind }
189 1.10 rmind ap--;
190 1.3 rmind }
191 1.3 rmind return true;
192 1.1 rmind }
193 1.1 rmind
194 1.10 rmind /*
195 1.10 rmind * npfctl_parse_fam_addr_mask: return address family, address and mask.
196 1.10 rmind *
197 1.10 rmind * => Mask is optional and can be NULL.
198 1.10 rmind * => Returns true on success or false if unable to parse.
199 1.10 rmind */
200 1.10 rmind npfvar_t *
201 1.10 rmind npfctl_parse_fam_addr_mask(const char *addr, const char *mask,
202 1.10 rmind unsigned long *nummask)
203 1.8 zoltan {
204 1.10 rmind fam_addr_mask_t fam;
205 1.10 rmind
206 1.10 rmind memset(&fam, 0, sizeof(fam));
207 1.8 zoltan
208 1.10 rmind if (!npfctl_parse_fam_addr(addr, &fam.fam_family, &fam.fam_addr))
209 1.22 rmind return NULL;
210 1.10 rmind
211 1.10 rmind /*
212 1.10 rmind * Note: both mask and nummask may be NULL. In such case,
213 1.10 rmind * npfctl_parse_mask() will handle and will set full mask.
214 1.10 rmind */
215 1.10 rmind if (nummask) {
216 1.10 rmind fam.fam_mask = *nummask;
217 1.10 rmind } else if (!npfctl_parse_mask(mask, fam.fam_family, &fam.fam_mask)) {
218 1.22 rmind return NULL;
219 1.8 zoltan }
220 1.22 rmind return npfvar_create_element(NPFVAR_FAM, &fam, sizeof(fam));
221 1.1 rmind }
222 1.1 rmind
223 1.10 rmind npfvar_t *
224 1.23 rmind npfctl_parse_table_id(const char *name)
225 1.3 rmind {
226 1.24 rmind u_int tid;
227 1.24 rmind
228 1.24 rmind tid = npfctl_table_getid(name);
229 1.24 rmind if (tid == (unsigned)-1) {
230 1.23 rmind yyerror("table '%s' is not defined", name);
231 1.10 rmind return NULL;
232 1.3 rmind }
233 1.24 rmind return npfvar_create_element(NPFVAR_TABLE, &tid, sizeof(u_int));
234 1.3 rmind }
235 1.3 rmind
236 1.10 rmind /*
237 1.10 rmind * npfctl_parse_port_range: create a port-range variable. Note that the
238 1.12 rmind * passed port numbers should be in host byte order.
239 1.10 rmind */
240 1.10 rmind npfvar_t *
241 1.10 rmind npfctl_parse_port_range(in_port_t s, in_port_t e)
242 1.1 rmind {
243 1.10 rmind port_range_t pr;
244 1.1 rmind
245 1.12 rmind pr.pr_start = htons(s);
246 1.12 rmind pr.pr_end = htons(e);
247 1.1 rmind
248 1.22 rmind return npfvar_create_element(NPFVAR_PORT_RANGE, &pr, sizeof(pr));
249 1.1 rmind }
250 1.1 rmind
251 1.10 rmind npfvar_t *
252 1.11 christos npfctl_parse_port_range_variable(const char *v)
253 1.11 christos {
254 1.11 christos npfvar_t *vp = npfvar_lookup(v);
255 1.11 christos size_t count = npfvar_get_count(vp);
256 1.22 rmind npfvar_t *pvp = npfvar_create();
257 1.12 rmind port_range_t *pr;
258 1.12 rmind in_port_t p;
259 1.11 christos
260 1.11 christos for (size_t i = 0; i < count; i++) {
261 1.11 christos int type = npfvar_get_type(vp, i);
262 1.11 christos void *data = npfvar_get_data(vp, type, i);
263 1.12 rmind
264 1.11 christos switch (type) {
265 1.11 christos case NPFVAR_IDENTIFIER:
266 1.11 christos case NPFVAR_STRING:
267 1.11 christos p = npfctl_portno(data);
268 1.11 christos npfvar_add_elements(pvp, npfctl_parse_port_range(p, p));
269 1.11 christos break;
270 1.11 christos case NPFVAR_PORT_RANGE:
271 1.11 christos pr = data;
272 1.11 christos npfvar_add_element(pvp, NPFVAR_PORT_RANGE, pr,
273 1.11 christos sizeof(*pr));
274 1.11 christos break;
275 1.11 christos case NPFVAR_NUM:
276 1.11 christos p = *(unsigned long *)data;
277 1.11 christos npfvar_add_elements(pvp, npfctl_parse_port_range(p, p));
278 1.11 christos break;
279 1.11 christos default:
280 1.11 christos yyerror("wrong variable '%s' type '%s' for port range",
281 1.11 christos v, npfvar_type(type));
282 1.12 rmind npfvar_destroy(pvp);
283 1.12 rmind return NULL;
284 1.11 christos }
285 1.11 christos }
286 1.11 christos return pvp;
287 1.11 christos }
288 1.11 christos
289 1.11 christos npfvar_t *
290 1.19 rmind npfctl_parse_ifnet(const char *ifname, const int family)
291 1.10 rmind {
292 1.10 rmind struct ifaddrs *ifa;
293 1.19 rmind ifnet_addr_t ifna;
294 1.22 rmind npfvar_t *vpa;
295 1.1 rmind
296 1.10 rmind if (ifs_list == NULL && getifaddrs(&ifs_list) == -1) {
297 1.10 rmind err(EXIT_FAILURE, "getifaddrs");
298 1.10 rmind }
299 1.1 rmind
300 1.22 rmind vpa = npfvar_create();
301 1.21 rmind ifna.ifna_name = estrdup(ifname);
302 1.19 rmind ifna.ifna_addrs = vpa;
303 1.19 rmind ifna.ifna_index = npfctl_find_ifindex(ifname);
304 1.19 rmind assert(ifna.ifna_index != 0);
305 1.1 rmind
306 1.10 rmind for (ifa = ifs_list; ifa != NULL; ifa = ifa->ifa_next) {
307 1.19 rmind fam_addr_mask_t fam;
308 1.10 rmind struct sockaddr *sa;
309 1.1 rmind
310 1.10 rmind if (strcmp(ifa->ifa_name, ifname) != 0)
311 1.10 rmind continue;
312 1.1 rmind
313 1.10 rmind if ((ifa->ifa_flags & IFF_UP) == 0)
314 1.10 rmind warnx("interface '%s' is down", ifname);
315 1.10 rmind
316 1.10 rmind sa = ifa->ifa_addr;
317 1.19 rmind if (sa->sa_family != AF_INET && sa->sa_family != AF_INET6)
318 1.19 rmind continue;
319 1.19 rmind if (family != AF_UNSPEC && sa->sa_family != family)
320 1.10 rmind continue;
321 1.1 rmind
322 1.19 rmind memset(&fam, 0, sizeof(fam));
323 1.19 rmind fam.fam_family = sa->sa_family;
324 1.19 rmind fam.fam_ifindex = ifna.ifna_index;
325 1.1 rmind
326 1.19 rmind if (!npfctl_copy_address(sa->sa_family, &fam.fam_addr, sa))
327 1.10 rmind goto out;
328 1.1 rmind
329 1.10 rmind if (!npfctl_parse_mask(NULL, fam.fam_family, &fam.fam_mask))
330 1.10 rmind goto out;
331 1.1 rmind
332 1.19 rmind if (!npfvar_add_element(vpa, NPFVAR_FAM, &fam, sizeof(fam)))
333 1.10 rmind goto out;
334 1.10 rmind }
335 1.19 rmind if (npfvar_get_count(vpa) == 0) {
336 1.10 rmind yyerror("no addresses matched for interface '%s'", ifname);
337 1.10 rmind goto out;
338 1.1 rmind }
339 1.19 rmind
340 1.22 rmind return npfvar_create_element(NPFVAR_INTERFACE, &ifna, sizeof(ifna));
341 1.10 rmind out:
342 1.19 rmind npfvar_destroy(ifna.ifna_addrs);
343 1.10 rmind return NULL;
344 1.1 rmind }
345 1.1 rmind
346 1.15 rmind bool
347 1.15 rmind npfctl_parse_cidr(char *cidr, fam_addr_mask_t *fam, int *alen)
348 1.1 rmind {
349 1.15 rmind char *mask, *p;
350 1.1 rmind
351 1.15 rmind p = strchr(cidr, '\n');
352 1.10 rmind if (p) {
353 1.15 rmind *p = '\0';
354 1.15 rmind }
355 1.15 rmind mask = strchr(cidr, '/');
356 1.15 rmind if (mask) {
357 1.15 rmind *mask++ = '\0';
358 1.1 rmind }
359 1.15 rmind
360 1.15 rmind memset(fam, 0, sizeof(*fam));
361 1.15 rmind if (!npfctl_parse_fam_addr(cidr, &fam->fam_family, &fam->fam_addr)) {
362 1.15 rmind return false;
363 1.15 rmind }
364 1.15 rmind if (!npfctl_parse_mask(mask, fam->fam_family, &fam->fam_mask)) {
365 1.15 rmind return false;
366 1.15 rmind }
367 1.15 rmind switch (fam->fam_family) {
368 1.15 rmind case AF_INET:
369 1.15 rmind *alen = sizeof(struct in_addr);
370 1.15 rmind break;
371 1.15 rmind case AF_INET6:
372 1.15 rmind *alen = sizeof(struct in6_addr);
373 1.15 rmind break;
374 1.15 rmind default:
375 1.15 rmind return false;
376 1.1 rmind }
377 1.15 rmind return true;
378 1.1 rmind }
379 1.1 rmind
380 1.14 rmind int
381 1.14 rmind npfctl_protono(const char *proto)
382 1.14 rmind {
383 1.14 rmind struct protoent *pe;
384 1.14 rmind
385 1.14 rmind pe = getprotobyname(proto);
386 1.14 rmind if (pe == NULL) {
387 1.14 rmind yyerror("unknown protocol '%s'", proto);
388 1.14 rmind return -1;
389 1.14 rmind }
390 1.14 rmind return pe->p_proto;
391 1.14 rmind }
392 1.14 rmind
393 1.10 rmind /*
394 1.10 rmind * npfctl_portno: convert port identifier (string) to a number.
395 1.10 rmind *
396 1.12 rmind * => Returns port number in host byte order.
397 1.10 rmind */
398 1.10 rmind in_port_t
399 1.10 rmind npfctl_portno(const char *port)
400 1.1 rmind {
401 1.10 rmind struct addrinfo *ai, *rai;
402 1.10 rmind in_port_t p = 0;
403 1.10 rmind int e;
404 1.10 rmind
405 1.10 rmind e = getaddrinfo(NULL, port, NULL, &rai);
406 1.10 rmind if (e != 0) {
407 1.14 rmind yyerror("invalid port name '%s' (%s)", port, gai_strerror(e));
408 1.10 rmind return 0;
409 1.10 rmind }
410 1.10 rmind
411 1.10 rmind for (ai = rai; ai; ai = ai->ai_next) {
412 1.10 rmind switch (ai->ai_family) {
413 1.10 rmind case AF_INET: {
414 1.10 rmind struct sockaddr_in *sin = (void *)ai->ai_addr;
415 1.10 rmind p = sin->sin_port;
416 1.10 rmind goto out;
417 1.10 rmind }
418 1.10 rmind case AF_INET6: {
419 1.10 rmind struct sockaddr_in6 *sin6 = (void *)ai->ai_addr;
420 1.10 rmind p = sin6->sin6_port;
421 1.10 rmind goto out;
422 1.8 zoltan }
423 1.10 rmind default:
424 1.10 rmind break;
425 1.8 zoltan }
426 1.1 rmind }
427 1.10 rmind out:
428 1.10 rmind freeaddrinfo(rai);
429 1.12 rmind return ntohs(p);
430 1.10 rmind }
431 1.1 rmind
432 1.10 rmind npfvar_t *
433 1.10 rmind npfctl_parse_tcpflag(const char *s)
434 1.10 rmind {
435 1.10 rmind uint8_t tfl = 0;
436 1.3 rmind
437 1.10 rmind while (*s) {
438 1.10 rmind switch (*s) {
439 1.10 rmind case 'F': tfl |= TH_FIN; break;
440 1.10 rmind case 'S': tfl |= TH_SYN; break;
441 1.10 rmind case 'R': tfl |= TH_RST; break;
442 1.10 rmind case 'P': tfl |= TH_PUSH; break;
443 1.10 rmind case 'A': tfl |= TH_ACK; break;
444 1.10 rmind case 'U': tfl |= TH_URG; break;
445 1.10 rmind case 'E': tfl |= TH_ECE; break;
446 1.10 rmind case 'W': tfl |= TH_CWR; break;
447 1.10 rmind default:
448 1.10 rmind yyerror("invalid flag '%c'", *s);
449 1.10 rmind return NULL;
450 1.3 rmind }
451 1.10 rmind s++;
452 1.1 rmind }
453 1.22 rmind return npfvar_create_element(NPFVAR_TCPFLAG, &tfl, sizeof(tfl));
454 1.10 rmind }
455 1.10 rmind
456 1.10 rmind uint8_t
457 1.16 spz npfctl_icmptype(int proto, const char *type)
458 1.10 rmind {
459 1.26 christos #ifdef __NetBSD__
460 1.16 spz uint8_t ul;
461 1.16 spz
462 1.16 spz switch (proto) {
463 1.16 spz case IPPROTO_ICMP:
464 1.16 spz for (ul = 0; icmp_type[ul]; ul++)
465 1.16 spz if (strcmp(icmp_type[ul], type) == 0)
466 1.16 spz return ul;
467 1.16 spz break;
468 1.16 spz case IPPROTO_ICMPV6:
469 1.16 spz for (ul = 0; icmp6_type_err[ul]; ul++)
470 1.16 spz if (strcmp(icmp6_type_err[ul], type) == 0)
471 1.16 spz return ul;
472 1.16 spz for (ul = 0; icmp6_type_info[ul]; ul++)
473 1.16 spz if (strcmp(icmp6_type_info[ul], type) == 0)
474 1.22 rmind return ul + 128;
475 1.16 spz break;
476 1.16 spz default:
477 1.16 spz assert(false);
478 1.16 spz }
479 1.26 christos #endif
480 1.16 spz yyerror("unknown icmp-type %s", type);
481 1.10 rmind return ~0;
482 1.10 rmind }
483 1.10 rmind
484 1.10 rmind uint8_t
485 1.16 spz npfctl_icmpcode(int proto, uint8_t type, const char *code)
486 1.10 rmind {
487 1.26 christos #ifdef __NetBSD__
488 1.17 rmind const char * const *arr;
489 1.10 rmind
490 1.16 spz switch (proto) {
491 1.16 spz case IPPROTO_ICMP:
492 1.16 spz switch (type) {
493 1.16 spz case ICMP_ECHOREPLY:
494 1.16 spz case ICMP_SOURCEQUENCH:
495 1.16 spz case ICMP_ALTHOSTADDR:
496 1.16 spz case ICMP_ECHO:
497 1.16 spz case ICMP_ROUTERSOLICIT:
498 1.16 spz case ICMP_TSTAMP:
499 1.16 spz case ICMP_TSTAMPREPLY:
500 1.16 spz case ICMP_IREQ:
501 1.16 spz case ICMP_IREQREPLY:
502 1.16 spz case ICMP_MASKREQ:
503 1.16 spz case ICMP_MASKREPLY:
504 1.16 spz arr = icmp_code_none;
505 1.16 spz break;
506 1.16 spz case ICMP_ROUTERADVERT:
507 1.16 spz arr = icmp_code_routeradvert;
508 1.16 spz break;
509 1.16 spz case ICMP_UNREACH:
510 1.16 spz arr = icmp_code_unreach;
511 1.16 spz break;
512 1.16 spz case ICMP_REDIRECT:
513 1.16 spz arr = icmp_code_redirect;
514 1.16 spz break;
515 1.16 spz case ICMP_TIMXCEED:
516 1.16 spz arr = icmp_code_timxceed;
517 1.16 spz break;
518 1.16 spz case ICMP_PARAMPROB:
519 1.16 spz arr = icmp_code_paramprob;
520 1.16 spz break;
521 1.16 spz case ICMP_PHOTURIS:
522 1.16 spz arr = icmp_code_photuris;
523 1.16 spz break;
524 1.16 spz default:
525 1.16 spz yyerror("unknown icmp-type %d while parsing code %s",
526 1.16 spz type, code);
527 1.16 spz return ~0;
528 1.16 spz }
529 1.10 rmind break;
530 1.16 spz case IPPROTO_ICMPV6:
531 1.16 spz switch (type) {
532 1.16 spz case ICMP6_DST_UNREACH:
533 1.16 spz arr = icmp6_code_unreach;
534 1.16 spz break;
535 1.16 spz case ICMP6_TIME_EXCEEDED:
536 1.16 spz arr = icmp6_code_timxceed;
537 1.16 spz break;
538 1.16 spz case ICMP6_PARAM_PROB:
539 1.16 spz arr = icmp6_code_paramprob;
540 1.16 spz break;
541 1.16 spz case ICMP6_PACKET_TOO_BIG:
542 1.16 spz /* code-less info ICMPs */
543 1.16 spz case ICMP6_ECHO_REQUEST:
544 1.16 spz case ICMP6_ECHO_REPLY:
545 1.16 spz case MLD_LISTENER_QUERY:
546 1.16 spz case MLD_LISTENER_REPORT:
547 1.16 spz case MLD_LISTENER_DONE:
548 1.16 spz case ND_ROUTER_SOLICIT:
549 1.16 spz case ND_ROUTER_ADVERT:
550 1.16 spz case ND_NEIGHBOR_SOLICIT:
551 1.16 spz case ND_NEIGHBOR_ADVERT:
552 1.16 spz case ND_REDIRECT:
553 1.16 spz arr = icmp6_code_none;
554 1.16 spz break;
555 1.16 spz /* XXX TODO: info ICMPs with code values */
556 1.16 spz default:
557 1.16 spz yyerror("unknown icmp-type %d while parsing code %s",
558 1.16 spz type, code);
559 1.16 spz return ~0;
560 1.16 spz }
561 1.10 rmind break;
562 1.10 rmind default:
563 1.16 spz assert(false);
564 1.10 rmind }
565 1.10 rmind
566 1.10 rmind for (uint8_t ul = 0; arr[ul]; ul++) {
567 1.10 rmind if (strcmp(arr[ul], code) == 0)
568 1.10 rmind return ul;
569 1.10 rmind }
570 1.26 christos #endif
571 1.16 spz yyerror("unknown code %s for icmp-type %d", code, type);
572 1.10 rmind return ~0;
573 1.10 rmind }
574 1.10 rmind
575 1.10 rmind npfvar_t *
576 1.16 spz npfctl_parse_icmp(int proto, int type, int code)
577 1.10 rmind {
578 1.22 rmind npfvar_t *vp = npfvar_create();
579 1.16 spz
580 1.20 rmind if (!npfvar_add_element(vp, NPFVAR_ICMP, &type, sizeof(type)))
581 1.10 rmind goto out;
582 1.10 rmind
583 1.20 rmind if (!npfvar_add_element(vp, NPFVAR_ICMP, &code, sizeof(code)))
584 1.10 rmind goto out;
585 1.10 rmind
586 1.10 rmind return vp;
587 1.10 rmind out:
588 1.10 rmind npfvar_destroy(vp);
589 1.10 rmind return NULL;
590 1.1 rmind }
591 1.25 rmind
592 1.25 rmind /*
593 1.25 rmind * npfctl_npt66_calcadj: calculate the adjustment for NPTv6 as per RFC 6296.
594 1.25 rmind */
595 1.25 rmind uint16_t
596 1.25 rmind npfctl_npt66_calcadj(npf_netmask_t len, const npf_addr_t *pref_in,
597 1.25 rmind const npf_addr_t *pref_out)
598 1.25 rmind {
599 1.25 rmind const uint16_t *addr6_in = (const uint16_t *)pref_in;
600 1.25 rmind const uint16_t *addr6_out = (const uint16_t *)pref_out;
601 1.25 rmind unsigned i, remnant, wordmask, preflen = len >> 4;
602 1.25 rmind uint32_t adj, isum = 0, osum = 0;
603 1.25 rmind
604 1.25 rmind /*
605 1.25 rmind * Extract the bits within a 16-bit word (when prefix length is
606 1.25 rmind * not dividable by 16) and include them into the sum.
607 1.25 rmind */
608 1.25 rmind remnant = len - (preflen << 4);
609 1.25 rmind wordmask = (1U << remnant) - 1;
610 1.25 rmind assert(wordmask == 0 || (len % 16) != 0);
611 1.25 rmind
612 1.25 rmind /* Inner prefix - sum and fold. */
613 1.25 rmind for (i = 0; i < preflen; i++) {
614 1.25 rmind isum += addr6_in[i];
615 1.25 rmind }
616 1.25 rmind isum += addr6_in[i] & wordmask;
617 1.25 rmind while (isum >> 16) {
618 1.25 rmind isum = (isum >> 16) + (isum & 0xffff);
619 1.25 rmind }
620 1.25 rmind
621 1.25 rmind /* Outer prefix - sum and fold. */
622 1.25 rmind for (i = 0; i < preflen; i++) {
623 1.25 rmind osum += addr6_out[i];
624 1.25 rmind }
625 1.25 rmind osum += addr6_out[i] & wordmask;
626 1.25 rmind while (osum >> 16) {
627 1.25 rmind osum = (osum >> 16) + (osum & 0xffff);
628 1.25 rmind }
629 1.25 rmind
630 1.25 rmind /* Calculate 1's complement difference. */
631 1.25 rmind adj = isum + ~osum;
632 1.25 rmind while (adj >> 16) {
633 1.25 rmind adj = (adj >> 16) + (adj & 0xffff);
634 1.25 rmind }
635 1.25 rmind return (uint16_t)adj;
636 1.25 rmind }
637