1 /*-
2 * Copyright (c) 2002-2003 Luigi Rizzo
3 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4 * Copyright (c) 1994 Ugen J.S.Antsilevich
5 *
6 * Idea and grammar partially left from:
7 * Copyright (c) 1993 Daniel Boulet
8 *
9 * Redistribution and use in source forms, with and without modification,
10 * are permitted provided that this entire comment appears intact.
11 *
12 * Redistribution in binary form may occur without any restrictions.
13 * Obviously, it would be nice if you gave credit where credit is due
14 * but requiring it would be too onerous.
15 *
16 * This software is provided ``AS IS'' without any warranties of any kind.
17 *
18 * NEW command line interface for IP firewall facility
19 *
20 * ipv6 support
21 */
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25
26 #include "ipfw2.h"
27
28 #include <err.h>
29 #include <netdb.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sysexits.h>
34
35 #include <net/if.h>
36 #include <netinet/in.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/ip.h>
39 #include <netinet/icmp6.h>
40 #include <netinet/ip_fw.h>
41 #include <arpa/inet.h>
42
43 #define CHECK_LENGTH(v, len) do { \
44 if ((v) < (len)) \
45 errx(EX_DATAERR, "Rule too long"); \
46 } while (0)
47
48 static struct _s_x icmp6codes[] = {
49 { "no-route", ICMP6_DST_UNREACH_NOROUTE },
50 { "admin-prohib", ICMP6_DST_UNREACH_ADMIN },
51 { "address", ICMP6_DST_UNREACH_ADDR },
52 { "port", ICMP6_DST_UNREACH_NOPORT },
53 { NULL, 0 }
54 };
55
56 void
fill_unreach6_code(u_short * codep,char * str)57 fill_unreach6_code(u_short *codep, char *str)
58 {
59 int val;
60 char *s;
61
62 val = strtoul(str, &s, 0);
63 if (s == str || *s != '\0' || val >= 0x100)
64 val = match_token(icmp6codes, str);
65 if (val < 0)
66 errx(EX_DATAERR, "unknown ICMPv6 unreachable code ``%s''", str);
67 *codep = val;
68 return;
69 }
70
71 void
print_unreach6_code(struct buf_pr * bp,uint16_t code)72 print_unreach6_code(struct buf_pr *bp, uint16_t code)
73 {
74 char const *s = match_value(icmp6codes, code);
75
76 if (s != NULL)
77 bprintf(bp, "unreach6 %s", s);
78 else
79 bprintf(bp, "unreach6 %u", code);
80 }
81
82 /*
83 * Print the ip address contained in a command.
84 */
85 void
print_ip6(struct buf_pr * bp,const ipfw_insn_ip6 * cmd)86 print_ip6(struct buf_pr *bp, const ipfw_insn_ip6 *cmd)
87 {
88 char trad[255];
89 struct hostent *he = NULL;
90 const struct in6_addr *a = &(cmd->addr6);
91 int len, mb;
92
93 len = F_LEN((const ipfw_insn *)cmd) - 1;
94 if (cmd->o.opcode == O_IP6_SRC_ME || cmd->o.opcode == O_IP6_DST_ME) {
95 bprintf(bp, " me6");
96 return;
97 }
98 if (cmd->o.opcode == O_IP6) {
99 bprintf(bp, " ip6");
100 return;
101 }
102
103 /*
104 * len == 4 indicates a single IP, whereas lists of 1 or more
105 * addr/mask pairs have len = (2n+1). We convert len to n so we
106 * use that to count the number of entries.
107 */
108 bprintf(bp, " ");
109 for (len = len / 4; len > 0; len -= 2, a += 2) {
110 /* mask length */
111 mb = (cmd->o.opcode == O_IP6_SRC ||
112 cmd->o.opcode == O_IP6_DST) ? 128:
113 contigmask((const uint8_t *)&(a[1]), 128);
114
115 if (mb == 128 && g_co.do_resolv)
116 he = gethostbyaddr((const char *)a, sizeof(*a),
117 AF_INET6);
118
119 if (he != NULL) /* resolved to name */
120 bprintf(bp, "%s", he->h_name);
121 else if (mb == 0) /* any */
122 bprintf(bp, "any");
123 else { /* numeric IP followed by some kind of mask */
124 if (inet_ntop(AF_INET6, a, trad,
125 sizeof(trad)) == NULL)
126 bprintf(bp, "Error ntop in print_ip6\n");
127 bprintf(bp, "%s", trad );
128 if (mb < 0) /* mask not contiguous */
129 bprintf(bp, "/%s", inet_ntop(AF_INET6, &a[1],
130 trad, sizeof(trad)));
131 else if (mb < 128)
132 bprintf(bp, "/%d", mb);
133 }
134 if (len > 2)
135 bprintf(bp, ",");
136 }
137 }
138
139 void
fill_icmp6types(ipfw_insn_icmp6 * cmd,char * av,int cblen)140 fill_icmp6types(ipfw_insn_icmp6 *cmd, char *av, int cblen)
141 {
142 uint8_t type;
143
144 CHECK_LENGTH(cblen, (int)F_INSN_SIZE(ipfw_insn_icmp6));
145 memset(cmd, 0, sizeof(*cmd));
146 while (*av) {
147 if (*av == ',')
148 av++;
149 type = strtoul(av, &av, 0);
150 if (*av != ',' && *av != '\0')
151 errx(EX_DATAERR, "invalid ICMP6 type");
152 /*
153 * XXX: shouldn't this be 0xFF? I can't see any reason why
154 * we shouldn't be able to filter all possiable values
155 * regardless of the ability of the rest of the kernel to do
156 * anything useful with them.
157 */
158 if (type > ICMP6_MAXTYPE)
159 errx(EX_DATAERR, "ICMP6 type out of range");
160 cmd->d[type / 32] |= ( 1 << (type % 32));
161 }
162 cmd->o.opcode = O_ICMP6TYPE;
163 cmd->o.len |= F_INSN_SIZE(ipfw_insn_icmp6);
164 }
165
166 void
print_icmp6types(struct buf_pr * bp,const ipfw_insn_u32 * cmd)167 print_icmp6types(struct buf_pr *bp, const ipfw_insn_u32 *cmd)
168 {
169 int i, j;
170 char sep= ' ';
171
172 bprintf(bp, " icmp6types");
173 for (i = 0; i < 7; i++)
174 for (j=0; j < 32; ++j) {
175 if ( (cmd->d[i] & (1 << (j))) == 0)
176 continue;
177 bprintf(bp, "%c%d", sep, (i*32 + j));
178 sep = ',';
179 }
180 }
181
182 void
print_flow6id(struct buf_pr * bp,const ipfw_insn_u32 * cmd)183 print_flow6id(struct buf_pr *bp, const ipfw_insn_u32 *cmd)
184 {
185 uint16_t i, limit = cmd->o.arg1;
186 char sep = ',';
187
188 bprintf(bp, " flow-id ");
189 for( i=0; i < limit; ++i) {
190 if (i == limit - 1)
191 sep = ' ';
192 bprintf(bp, "%d%c", cmd->d[i], sep);
193 }
194 }
195
196 /* structure and define for the extension header in ipv6 */
197 static struct _s_x ext6hdrcodes[] = {
198 { "frag", EXT_FRAGMENT },
199 { "hopopt", EXT_HOPOPTS },
200 { "route", EXT_ROUTING },
201 { "dstopt", EXT_DSTOPTS },
202 { "ah", EXT_AH },
203 { "esp", EXT_ESP },
204 { "rthdr0", EXT_RTHDR0 },
205 { "rthdr2", EXT_RTHDR2 },
206 { NULL, 0 }
207 };
208
209 /* fills command for the extension header filtering */
210 int
fill_ext6hdr(ipfw_insn * cmd,char * av)211 fill_ext6hdr( ipfw_insn *cmd, char *av)
212 {
213 int tok;
214 char *s = av;
215
216 cmd->arg1 = 0;
217 while(s) {
218 av = strsep( &s, ",") ;
219 tok = match_token(ext6hdrcodes, av);
220 switch (tok) {
221 case EXT_FRAGMENT:
222 cmd->arg1 |= EXT_FRAGMENT;
223 break;
224 case EXT_HOPOPTS:
225 cmd->arg1 |= EXT_HOPOPTS;
226 break;
227 case EXT_ROUTING:
228 cmd->arg1 |= EXT_ROUTING;
229 break;
230 case EXT_DSTOPTS:
231 cmd->arg1 |= EXT_DSTOPTS;
232 break;
233 case EXT_AH:
234 cmd->arg1 |= EXT_AH;
235 break;
236 case EXT_ESP:
237 cmd->arg1 |= EXT_ESP;
238 break;
239 case EXT_RTHDR0:
240 cmd->arg1 |= EXT_RTHDR0;
241 break;
242 case EXT_RTHDR2:
243 cmd->arg1 |= EXT_RTHDR2;
244 break;
245 default:
246 errx(EX_DATAERR,
247 "invalid option for ipv6 exten header");
248 break;
249 }
250 }
251 if (cmd->arg1 == 0)
252 return (0);
253 cmd->opcode = O_EXT_HDR;
254 cmd->len |= F_INSN_SIZE(ipfw_insn);
255 return (1);
256 }
257
258 void
print_ext6hdr(struct buf_pr * bp,const ipfw_insn * cmd)259 print_ext6hdr(struct buf_pr *bp, const ipfw_insn *cmd )
260 {
261 char sep = ' ';
262
263 bprintf(bp, " extension header:");
264 if (cmd->arg1 & EXT_FRAGMENT) {
265 bprintf(bp, "%cfragmentation", sep);
266 sep = ',';
267 }
268 if (cmd->arg1 & EXT_HOPOPTS) {
269 bprintf(bp, "%chop options", sep);
270 sep = ',';
271 }
272 if (cmd->arg1 & EXT_ROUTING) {
273 bprintf(bp, "%crouting options", sep);
274 sep = ',';
275 }
276 if (cmd->arg1 & EXT_RTHDR0) {
277 bprintf(bp, "%crthdr0", sep);
278 sep = ',';
279 }
280 if (cmd->arg1 & EXT_RTHDR2) {
281 bprintf(bp, "%crthdr2", sep);
282 sep = ',';
283 }
284 if (cmd->arg1 & EXT_DSTOPTS) {
285 bprintf(bp, "%cdestination options", sep);
286 sep = ',';
287 }
288 if (cmd->arg1 & EXT_AH) {
289 bprintf(bp, "%cauthentication header", sep);
290 sep = ',';
291 }
292 if (cmd->arg1 & EXT_ESP) {
293 bprintf(bp, "%cencapsulated security payload", sep);
294 }
295 }
296
297 /* Try to find ipv6 address by hostname */
298 static int
lookup_host6(char * host,struct in6_addr * ip6addr)299 lookup_host6 (char *host, struct in6_addr *ip6addr)
300 {
301 struct hostent *he;
302
303 if (!inet_pton(AF_INET6, host, ip6addr)) {
304 if ((he = gethostbyname2(host, AF_INET6)) == NULL)
305 return(-1);
306 memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr));
307 }
308 return (0);
309 }
310
311
312 /*
313 * fill the addr and mask fields in the instruction as appropriate from av.
314 * Update length as appropriate.
315 * The following formats are allowed:
316 * any matches any IP6. Actually returns an empty instruction.
317 * me returns O_IP6_*_ME
318 *
319 * 03f1::234:123:0342 single IP6 address
320 * 03f1::234:123:0342/24 address/masklen
321 * 03f1::234:123:0342/ffff::ffff:ffff address/mask
322 * 03f1::234:123:0342/24,03f1::234:123:0343/ List of address
323 *
324 * Set of address (as in ipv6) not supported because ipv6 address
325 * are typically random past the initial prefix.
326 * Return 1 on success, 0 on failure.
327 */
328 static int
fill_ip6(ipfw_insn_ip6 * cmd,char * av,int cblen,struct tidx * tstate)329 fill_ip6(ipfw_insn_ip6 *cmd, char *av, int cblen, struct tidx *tstate)
330 {
331 int len = 0;
332 struct in6_addr *d = &(cmd->addr6);
333 char *oav;
334 /*
335 * Needed for multiple address.
336 * Note d[1] points to struct in6_add r mask6 of cmd
337 */
338
339 cmd->o.len &= ~F_LEN_MASK; /* zero len */
340
341 if (strcmp(av, "any") == 0)
342 return (1);
343
344 /* Set the data for "me" opt */
345 if (strcmp(av, "me") == 0 || strcmp(av, "me6") == 0) {
346 cmd->o.len |= F_INSN_SIZE(ipfw_insn);
347 return (1);
348 }
349
350 if (strncmp(av, "table(", 6) == 0) {
351 fill_table(&cmd->o, av, O_IP_DST_LOOKUP, tstate);
352 return (1);
353 }
354
355 oav = av = strdup(av);
356 while (av) {
357 /*
358 * After the address we can have '/' indicating a mask,
359 * or ',' indicating another address follows.
360 */
361
362 char *p, *q;
363 int masklen;
364 char md = '\0';
365
366 CHECK_LENGTH(cblen,
367 1 + len + 2 * (int)F_INSN_SIZE(struct in6_addr));
368
369 if ((q = strchr(av, ',')) ) {
370 *q = '\0';
371 q++;
372 }
373
374 if ((p = strchr(av, '/')) ) {
375 md = *p; /* save the separator */
376 *p = '\0'; /* terminate address string */
377 p++; /* and skip past it */
378 }
379 /* now p points to NULL, mask or next entry */
380
381 /* lookup stores address in *d as a side effect */
382 if (lookup_host6(av, d) != 0) {
383 /* XXX: failed. Free memory and go */
384 errx(EX_DATAERR, "bad address \"%s\"", av);
385 }
386 /* next, look at the mask, if any */
387 if (md == '/' && strchr(p, ':')) {
388 if (!inet_pton(AF_INET6, p, &d[1]))
389 errx(EX_DATAERR, "bad mask \"%s\"", p);
390
391 masklen = contigmask((uint8_t *)&(d[1]), 128);
392 } else {
393 masklen = (md == '/') ? atoi(p) : 128;
394 if (masklen > 128 || masklen < 0)
395 errx(EX_DATAERR, "bad width \"%s\''", p);
396 else
397 n2mask(&d[1], masklen);
398 }
399
400 APPLY_MASK(d, &d[1]); /* mask base address with mask */
401
402 av = q;
403
404 /* Check this entry */
405 if (masklen == 0) {
406 /*
407 * 'any' turns the entire list into a NOP.
408 * 'not any' never matches, so it is removed from the
409 * list unless it is the only item, in which case we
410 * report an error.
411 */
412 if (cmd->o.len & F_NOT && av == NULL && len == 0)
413 errx(EX_DATAERR, "not any never matches");
414 continue;
415 }
416
417 /*
418 * A single IP can be stored alone
419 */
420 if (masklen == 128 && av == NULL && len == 0) {
421 len = F_INSN_SIZE(struct in6_addr);
422 break;
423 }
424
425 /* Update length and pointer to arguments */
426 len += F_INSN_SIZE(struct in6_addr)*2;
427 d += 2;
428 } /* end while */
429
430 /*
431 * Total length of the command, remember that 1 is the size of
432 * the base command.
433 */
434 if (len + 1 > F_LEN_MASK)
435 errx(EX_DATAERR, "address list too long");
436 cmd->o.len |= len+1;
437 free(oav);
438 return (1);
439 }
440
441 /*
442 * fills command for ipv6 flow-id filtering
443 * note that the 20 bit flow number is stored in a array of u_int32_t
444 * it's supported lists of flow-id, so in the o.arg1 we store how many
445 * additional flow-id we want to filter, the basic is 1
446 */
447 void
fill_flow6(ipfw_insn_u32 * cmd,char * av,int cblen)448 fill_flow6( ipfw_insn_u32 *cmd, char *av, int cblen)
449 {
450 u_int32_t type; /* Current flow number */
451 u_int16_t nflow = 0; /* Current flow index */
452 char *s = av;
453 cmd->d[0] = 0; /* Initializing the base number*/
454
455 while (s) {
456 CHECK_LENGTH(cblen,
457 (int)F_INSN_SIZE(ipfw_insn_u32) + nflow + 1);
458
459 av = strsep( &s, ",") ;
460 type = strtoul(av, &av, 0);
461 if (*av != ',' && *av != '\0')
462 errx(EX_DATAERR, "invalid ipv6 flow number %s", av);
463 if (type > 0xfffff)
464 errx(EX_DATAERR, "flow number out of range %s", av);
465 cmd->d[nflow] |= type;
466 nflow++;
467 }
468 if( nflow > 0 ) {
469 cmd->o.opcode = O_FLOW6ID;
470 cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + nflow;
471 cmd->o.arg1 = nflow;
472 }
473 else {
474 errx(EX_DATAERR, "invalid ipv6 flow number %s", av);
475 }
476 }
477
478 ipfw_insn *
add_srcip6(ipfw_insn * cmd,char * av,int cblen,struct tidx * tstate)479 add_srcip6(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate)
480 {
481
482 fill_ip6((ipfw_insn_ip6 *)cmd, av, cblen, tstate);
483 if (cmd->opcode == O_IP_DST_SET) /* set */
484 cmd->opcode = O_IP_SRC_SET;
485 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */
486 cmd->opcode = O_IP_SRC_LOOKUP;
487 else if (F_LEN(cmd) == 0) { /* any */
488 } else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) { /* "me" */
489 cmd->opcode = O_IP6_SRC_ME;
490 } else if (F_LEN(cmd) ==
491 (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) {
492 /* single IP, no mask*/
493 cmd->opcode = O_IP6_SRC;
494 } else { /* addr/mask opt */
495 cmd->opcode = O_IP6_SRC_MASK;
496 }
497 return cmd;
498 }
499
500 ipfw_insn *
add_dstip6(ipfw_insn * cmd,char * av,int cblen,struct tidx * tstate)501 add_dstip6(ipfw_insn *cmd, char *av, int cblen, struct tidx *tstate)
502 {
503
504 fill_ip6((ipfw_insn_ip6 *)cmd, av, cblen, tstate);
505 if (cmd->opcode == O_IP_DST_SET) /* set */
506 ;
507 else if (cmd->opcode == O_IP_DST_LOOKUP) /* table */
508 ;
509 else if (F_LEN(cmd) == 0) { /* any */
510 } else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) { /* "me" */
511 cmd->opcode = O_IP6_DST_ME;
512 } else if (F_LEN(cmd) ==
513 (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) {
514 /* single IP, no mask*/
515 cmd->opcode = O_IP6_DST;
516 } else { /* addr/mask opt */
517 cmd->opcode = O_IP6_DST_MASK;
518 }
519 return cmd;
520 }
521