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