ipv6.c revision 248505
1238384Sjkim/*
2238384Sjkim * Copyright (c) 2002-2003 Luigi Rizzo
3238384Sjkim * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
4238384Sjkim * Copyright (c) 1994 Ugen J.S.Antsilevich
5238384Sjkim *
6238384Sjkim * Idea and grammar partially left from:
7238384Sjkim * Copyright (c) 1993 Daniel Boulet
8238384Sjkim *
9280304Sjkim * Redistribution and use in source forms, with and without modification,
10238384Sjkim * are permitted provided that this entire comment appears intact.
11238384Sjkim *
12238384Sjkim * Redistribution in binary form may occur without any restrictions.
13238384Sjkim * Obviously, it would be nice if you gave credit where credit is due
14238384Sjkim * but requiring it would be too onerous.
15238384Sjkim *
16238384Sjkim * This software is provided ``AS IS'' without any warranties of any kind.
17238384Sjkim *
18238384Sjkim * NEW command line interface for IP firewall facility
19238384Sjkim *
20238384Sjkim * $FreeBSD: stable/9/sbin/ipfw/ipv6.c 248505 2013-03-19 13:29:01Z melifaro $
21238384Sjkim *
22238384Sjkim * ipv6 support
23238384Sjkim */
24238384Sjkim
25238384Sjkim#include <sys/types.h>
26238384Sjkim#include <sys/socket.h>
27238384Sjkim
28238384Sjkim#include "ipfw2.h"
29238384Sjkim
30238384Sjkim#include <err.h>
31238384Sjkim#include <netdb.h>
32238384Sjkim#include <stdio.h>
33238384Sjkim#include <stdlib.h>
34238384Sjkim#include <string.h>
35238384Sjkim#include <sysexits.h>
36238384Sjkim
37238384Sjkim#include <net/if.h>
38238384Sjkim#include <netinet/in.h>
39238384Sjkim#include <netinet/in_systm.h>
40238384Sjkim#include <netinet/ip.h>
41238384Sjkim#include <netinet/icmp6.h>
42238384Sjkim#include <netinet/ip_fw.h>
43238384Sjkim#include <arpa/inet.h>
44238384Sjkim
45238384Sjkim#define	CHECK_LENGTH(v, len) do {			\
46238384Sjkim	if ((v) < (len))				\
47238384Sjkim		errx(EX_DATAERR, "Rule too long");	\
48238384Sjkim	} while (0)
49238384Sjkim
50238384Sjkimstatic struct _s_x icmp6codes[] = {
51238384Sjkim      { "no-route",		ICMP6_DST_UNREACH_NOROUTE },
52238384Sjkim      { "admin-prohib",		ICMP6_DST_UNREACH_ADMIN },
53238384Sjkim      { "address",		ICMP6_DST_UNREACH_ADDR },
54238384Sjkim      { "port",			ICMP6_DST_UNREACH_NOPORT },
55238384Sjkim      { NULL, 0 }
56238384Sjkim};
57238384Sjkim
58238384Sjkimvoid
59238384Sjkimfill_unreach6_code(u_short *codep, char *str)
60238384Sjkim{
61280304Sjkim	int val;
62280304Sjkim	char *s;
63280304Sjkim
64280304Sjkim	val = strtoul(str, &s, 0);
65238384Sjkim	if (s == str || *s != '\0' || val >= 0x100)
66280304Sjkim		val = match_token(icmp6codes, str);
67280304Sjkim	if (val < 0)
68280304Sjkim		errx(EX_DATAERR, "unknown ICMPv6 unreachable code ``%s''", str);
69280304Sjkim	*codep = val;
70280304Sjkim	return;
71280304Sjkim}
72280304Sjkim
73280304Sjkimvoid
74280304Sjkimprint_unreach6_code(uint16_t code)
75280304Sjkim{
76280304Sjkim	char const *s = match_value(icmp6codes, code);
77280304Sjkim
78238384Sjkim	if (s != NULL)
79280304Sjkim		printf("unreach6 %s", s);
80280304Sjkim	else
81238384Sjkim		printf("unreach6 %u", code);
82280304Sjkim}
83238384Sjkim
84280304Sjkim/*
85238384Sjkim * Print the ip address contained in a command.
86280304Sjkim */
87280304Sjkimvoid
88238384Sjkimprint_ip6(ipfw_insn_ip6 *cmd, char const *s)
89280304Sjkim{
90238384Sjkim       struct hostent *he = NULL;
91280304Sjkim       int len = F_LEN((ipfw_insn *) cmd) - 1;
92280304Sjkim       struct in6_addr *a = &(cmd->addr6);
93280304Sjkim       char trad[255];
94238384Sjkim
95280304Sjkim       printf("%s%s ", cmd->o.len & F_NOT ? " not": "", s);
96280304Sjkim
97238384Sjkim       if (cmd->o.opcode == O_IP6_SRC_ME || cmd->o.opcode == O_IP6_DST_ME) {
98280304Sjkim	       printf("me6");
99238384Sjkim	       return;
100280304Sjkim       }
101280304Sjkim       if (cmd->o.opcode == O_IP6) {
102280304Sjkim	       printf(" ip6");
103238384Sjkim	       return;
104280304Sjkim       }
105280304Sjkim
106238384Sjkim       /*
107280304Sjkim	* len == 4 indicates a single IP, whereas lists of 1 or more
108280304Sjkim	* addr/mask pairs have len = (2n+1). We convert len to n so we
109280304Sjkim	* use that to count the number of entries.
110238384Sjkim	*/
111280304Sjkim
112280304Sjkim       for (len = len / 4; len > 0; len -= 2, a += 2) {
113238384Sjkim	   int mb =	/* mask length */
114280304Sjkim	       (cmd->o.opcode == O_IP6_SRC || cmd->o.opcode == O_IP6_DST) ?
115280304Sjkim	       128 : contigmask((uint8_t *)&(a[1]), 128);
116238384Sjkim
117280304Sjkim	   if (mb == 128 && co.do_resolv)
118280304Sjkim	       he = gethostbyaddr((char *)a, sizeof(*a), AF_INET6);
119280304Sjkim	   if (he != NULL)	     /* resolved to name */
120280304Sjkim	       printf("%s", he->h_name);
121280304Sjkim	   else if (mb == 0)	   /* any */
122280304Sjkim	       printf("any");
123238384Sjkim	   else {	  /* numeric IP followed by some kind of mask */
124280304Sjkim	       if (inet_ntop(AF_INET6,  a, trad, sizeof( trad ) ) == NULL)
125280304Sjkim		   printf("Error ntop in print_ip6\n");
126280304Sjkim	       printf("%s",  trad );
127280304Sjkim	       if (mb < 0)     /* XXX not really legal... */
128280304Sjkim		   printf(":%s",
129280304Sjkim		       inet_ntop(AF_INET6, &a[1], trad, sizeof(trad)));
130280304Sjkim	       else if (mb < 128)
131280304Sjkim		   printf("/%d", mb);
132280304Sjkim	   }
133280304Sjkim	   if (len > 2)
134280304Sjkim	       printf(",");
135280304Sjkim       }
136280304Sjkim}
137280304Sjkim
138280304Sjkimvoid
139280304Sjkimfill_icmp6types(ipfw_insn_icmp6 *cmd, char *av, int cblen)
140280304Sjkim{
141280304Sjkim       uint8_t type;
142280304Sjkim
143280304Sjkim       CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn_icmp6));
144280304Sjkim
145280304Sjkim       bzero(cmd, sizeof(*cmd));
146280304Sjkim       while (*av) {
147280304Sjkim	   if (*av == ',')
148280304Sjkim	       av++;
149280304Sjkim	   type = strtoul(av, &av, 0);
150280304Sjkim	   if (*av != ',' && *av != '\0')
151280304Sjkim	       errx(EX_DATAERR, "invalid ICMP6 type");
152238384Sjkim	   /*
153280304Sjkim	    * XXX: shouldn't this be 0xFF?  I can't see any reason why
154280304Sjkim	    * we shouldn't be able to filter all possiable values
155238384Sjkim	    * regardless of the ability of the rest of the kernel to do
156280304Sjkim	    * anything useful with them.
157280304Sjkim	    */
158280304Sjkim	   if (type > ICMP6_MAXTYPE)
159280304Sjkim	       errx(EX_DATAERR, "ICMP6 type out of range");
160280304Sjkim	   cmd->d[type / 32] |= ( 1 << (type % 32));
161280304Sjkim       }
162280304Sjkim       cmd->o.opcode = O_ICMP6TYPE;
163280304Sjkim       cmd->o.len |= F_INSN_SIZE(ipfw_insn_icmp6);
164280304Sjkim}
165280304Sjkim
166280304Sjkim
167280304Sjkimvoid
168280304Sjkimprint_icmp6types(ipfw_insn_u32 *cmd)
169280304Sjkim{
170280304Sjkim       int i, j;
171280304Sjkim       char sep= ' ';
172280304Sjkim
173238384Sjkim       printf(" ip6 icmp6types");
174280304Sjkim       for (i = 0; i < 7; i++)
175280304Sjkim	       for (j=0; j < 32; ++j) {
176280304Sjkim		       if ( (cmd->d[i] & (1 << (j))) == 0)
177238384Sjkim			       continue;
178280304Sjkim		       printf("%c%d", sep, (i*32 + j));
179280304Sjkim		       sep = ',';
180238384Sjkim	       }
181280304Sjkim}
182280304Sjkim
183280304Sjkimvoid
184238384Sjkimprint_flow6id( ipfw_insn_u32 *cmd)
185280304Sjkim{
186280304Sjkim       uint16_t i, limit = cmd->o.arg1;
187280304Sjkim       char sep = ',';
188280304Sjkim
189280304Sjkim       printf(" flow-id ");
190280304Sjkim       for( i=0; i < limit; ++i) {
191280304Sjkim	       if (i == limit - 1)
192280304Sjkim		       sep = ' ';
193238384Sjkim	       printf("%d%c", cmd->d[i], sep);
194280304Sjkim       }
195280304Sjkim}
196280304Sjkim
197238384Sjkim/* structure and define for the extension header in ipv6 */
198280304Sjkimstatic struct _s_x ext6hdrcodes[] = {
199280304Sjkim       { "frag",       EXT_FRAGMENT },
200238384Sjkim       { "hopopt",     EXT_HOPOPTS },
201280304Sjkim       { "route",      EXT_ROUTING },
202238384Sjkim       { "dstopt",     EXT_DSTOPTS },
203280304Sjkim       { "ah",	 EXT_AH },
204238384Sjkim       { "esp",	EXT_ESP },
205       { "rthdr0",     EXT_RTHDR0 },
206       { "rthdr2",     EXT_RTHDR2 },
207       { NULL,	 0 }
208};
209
210/* fills command for the extension header filtering */
211int
212fill_ext6hdr( ipfw_insn *cmd, char *av)
213{
214       int tok;
215       char *s = av;
216
217       cmd->arg1 = 0;
218
219       while(s) {
220	   av = strsep( &s, ",") ;
221	   tok = match_token(ext6hdrcodes, av);
222	   switch (tok) {
223	   case EXT_FRAGMENT:
224	       cmd->arg1 |= EXT_FRAGMENT;
225	       break;
226
227	   case EXT_HOPOPTS:
228	       cmd->arg1 |= EXT_HOPOPTS;
229	       break;
230
231	   case EXT_ROUTING:
232	       cmd->arg1 |= EXT_ROUTING;
233	       break;
234
235	   case EXT_DSTOPTS:
236	       cmd->arg1 |= EXT_DSTOPTS;
237	       break;
238
239	   case EXT_AH:
240	       cmd->arg1 |= EXT_AH;
241	       break;
242
243	   case EXT_ESP:
244	       cmd->arg1 |= EXT_ESP;
245	       break;
246
247	   case EXT_RTHDR0:
248	       cmd->arg1 |= EXT_RTHDR0;
249	       break;
250
251	   case EXT_RTHDR2:
252	       cmd->arg1 |= EXT_RTHDR2;
253	       break;
254
255	   default:
256	       errx( EX_DATAERR, "invalid option for ipv6 exten header" );
257	       break;
258	   }
259       }
260       if (cmd->arg1 == 0 )
261	   return 0;
262       cmd->opcode = O_EXT_HDR;
263       cmd->len |= F_INSN_SIZE( ipfw_insn );
264       return 1;
265}
266
267void
268print_ext6hdr( ipfw_insn *cmd )
269{
270       char sep = ' ';
271
272       printf(" extension header:");
273       if (cmd->arg1 & EXT_FRAGMENT ) {
274	   printf("%cfragmentation", sep);
275	   sep = ',';
276       }
277       if (cmd->arg1 & EXT_HOPOPTS ) {
278	   printf("%chop options", sep);
279	   sep = ',';
280       }
281       if (cmd->arg1 & EXT_ROUTING ) {
282	   printf("%crouting options", sep);
283	   sep = ',';
284       }
285       if (cmd->arg1 & EXT_RTHDR0 ) {
286	   printf("%crthdr0", sep);
287	   sep = ',';
288       }
289       if (cmd->arg1 & EXT_RTHDR2 ) {
290	   printf("%crthdr2", sep);
291	   sep = ',';
292       }
293       if (cmd->arg1 & EXT_DSTOPTS ) {
294	   printf("%cdestination options", sep);
295	   sep = ',';
296       }
297       if (cmd->arg1 & EXT_AH ) {
298	   printf("%cauthentication header", sep);
299	   sep = ',';
300       }
301       if (cmd->arg1 & EXT_ESP ) {
302	   printf("%cencapsulated security payload", sep);
303       }
304}
305
306/* Try to find ipv6 address by hostname */
307static int
308lookup_host6 (char *host, struct in6_addr *ip6addr)
309{
310	struct hostent *he;
311
312	if (!inet_pton(AF_INET6, host, ip6addr)) {
313		if ((he = gethostbyname2(host, AF_INET6)) == NULL)
314			return(-1);
315		memcpy(ip6addr, he->h_addr_list[0], sizeof( struct in6_addr));
316	}
317	return(0);
318}
319
320
321/*
322 * fill the addr and mask fields in the instruction as appropriate from av.
323 * Update length as appropriate.
324 * The following formats are allowed:
325 *     any     matches any IP6. Actually returns an empty instruction.
326 *     me      returns O_IP6_*_ME
327 *
328 *     03f1::234:123:0342		single IP6 addres
329 *     03f1::234:123:0342/24	    address/mask
330 *     03f1::234:123:0342/24,03f1::234:123:0343/	       List of address
331 *
332 * Set of address (as in ipv6) not supported because ipv6 address
333 * are typically random past the initial prefix.
334 * Return 1 on success, 0 on failure.
335 */
336static int
337fill_ip6(ipfw_insn_ip6 *cmd, char *av, int cblen)
338{
339	int len = 0;
340	struct in6_addr *d = &(cmd->addr6);
341	/*
342	 * Needed for multiple address.
343	 * Note d[1] points to struct in6_add r mask6 of cmd
344	 */
345
346	cmd->o.len &= ~F_LEN_MASK;	/* zero len */
347
348	if (strcmp(av, "any") == 0)
349		return (1);
350
351
352	if (strcmp(av, "me") == 0) {	/* Set the data for "me" opt*/
353		cmd->o.len |= F_INSN_SIZE(ipfw_insn);
354		return (1);
355	}
356
357	if (strcmp(av, "me6") == 0) {	/* Set the data for "me" opt*/
358		cmd->o.len |= F_INSN_SIZE(ipfw_insn);
359		return (1);
360	}
361
362	if (strncmp(av, "table(", 6) == 0) {
363		char *p = strchr(av + 6, ',');
364		uint32_t *dm = ((ipfw_insn_u32 *)cmd)->d;
365
366		if (p)
367			*p++ = '\0';
368		cmd->o.opcode = O_IP_DST_LOOKUP;
369		cmd->o.arg1 = strtoul(av + 6, NULL, 0);
370		if (p) {
371			cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32);
372			dm[0] = strtoul(p, NULL, 0);
373		} else
374			cmd->o.len |= F_INSN_SIZE(ipfw_insn);
375		return (1);
376	}
377
378	av = strdup(av);
379	while (av) {
380		/*
381		 * After the address we can have '/' indicating a mask,
382		 * or ',' indicating another address follows.
383		 */
384
385		char *p;
386		int masklen;
387		char md = '\0';
388
389		CHECK_LENGTH(cblen, 1 + len + 2 * F_INSN_SIZE(struct in6_addr));
390
391		if ((p = strpbrk(av, "/,")) ) {
392			md = *p;	/* save the separator */
393			*p = '\0';	/* terminate address string */
394			p++;		/* and skip past it */
395		}
396		/* now p points to NULL, mask or next entry */
397
398		/* lookup stores address in *d as a side effect */
399		if (lookup_host6(av, d) != 0) {
400			/* XXX: failed. Free memory and go */
401			errx(EX_DATAERR, "bad address \"%s\"", av);
402		}
403		/* next, look at the mask, if any */
404		masklen = (md == '/') ? atoi(p) : 128;
405		if (masklen > 128 || masklen < 0)
406			errx(EX_DATAERR, "bad width \"%s\''", p);
407		else
408			n2mask(&d[1], masklen);
409
410		APPLY_MASK(d, &d[1])   /* mask base address with mask */
411
412		/* find next separator */
413
414		if (md == '/') {	/* find separator past the mask */
415			p = strpbrk(p, ",");
416			if (p != NULL)
417				p++;
418		}
419		av = p;
420
421		/* Check this entry */
422		if (masklen == 0) {
423			/*
424			 * 'any' turns the entire list into a NOP.
425			 * 'not any' never matches, so it is removed from the
426			 * list unless it is the only item, in which case we
427			 * report an error.
428			 */
429			if (cmd->o.len & F_NOT && av == NULL && len == 0)
430				errx(EX_DATAERR, "not any never matches");
431			continue;
432		}
433
434		/*
435		 * A single IP can be stored alone
436		 */
437		if (masklen == 128 && av == NULL && len == 0) {
438			len = F_INSN_SIZE(struct in6_addr);
439			break;
440		}
441
442		/* Update length and pointer to arguments */
443		len += F_INSN_SIZE(struct in6_addr)*2;
444		d += 2;
445	} /* end while */
446
447	/*
448	 * Total length of the command, remember that 1 is the size of
449	 * the base command.
450	 */
451	if (len + 1 > F_LEN_MASK)
452		errx(EX_DATAERR, "address list too long");
453	cmd->o.len |= len+1;
454	free(av);
455	return (1);
456}
457
458/*
459 * fills command for ipv6 flow-id filtering
460 * note that the 20 bit flow number is stored in a array of u_int32_t
461 * it's supported lists of flow-id, so in the o.arg1 we store how many
462 * additional flow-id we want to filter, the basic is 1
463 */
464void
465fill_flow6( ipfw_insn_u32 *cmd, char *av, int cblen)
466{
467	u_int32_t type;	 /* Current flow number */
468	u_int16_t nflow = 0;    /* Current flow index */
469	char *s = av;
470	cmd->d[0] = 0;	  /* Initializing the base number*/
471
472	while (s) {
473		CHECK_LENGTH(cblen, F_INSN_SIZE(ipfw_insn_u32) + nflow + 1);
474
475		av = strsep( &s, ",") ;
476		type = strtoul(av, &av, 0);
477		if (*av != ',' && *av != '\0')
478			errx(EX_DATAERR, "invalid ipv6 flow number %s", av);
479		if (type > 0xfffff)
480			errx(EX_DATAERR, "flow number out of range %s", av);
481		cmd->d[nflow] |= type;
482		nflow++;
483	}
484	if( nflow > 0 ) {
485		cmd->o.opcode = O_FLOW6ID;
486		cmd->o.len |= F_INSN_SIZE(ipfw_insn_u32) + nflow;
487		cmd->o.arg1 = nflow;
488	}
489	else {
490		errx(EX_DATAERR, "invalid ipv6 flow number %s", av);
491	}
492}
493
494ipfw_insn *
495add_srcip6(ipfw_insn *cmd, char *av, int cblen)
496{
497
498	fill_ip6((ipfw_insn_ip6 *)cmd, av, cblen);
499	if (cmd->opcode == O_IP_DST_SET)			/* set */
500		cmd->opcode = O_IP_SRC_SET;
501	else if (cmd->opcode == O_IP_DST_LOOKUP)		/* table */
502		cmd->opcode = O_IP_SRC_LOOKUP;
503	else if (F_LEN(cmd) == 0) {				/* any */
504	} else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) {	/* "me" */
505		cmd->opcode = O_IP6_SRC_ME;
506	} else if (F_LEN(cmd) ==
507	    (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) {
508		/* single IP, no mask*/
509		cmd->opcode = O_IP6_SRC;
510	} else {					/* addr/mask opt */
511		cmd->opcode = O_IP6_SRC_MASK;
512	}
513	return cmd;
514}
515
516ipfw_insn *
517add_dstip6(ipfw_insn *cmd, char *av, int cblen)
518{
519
520	fill_ip6((ipfw_insn_ip6 *)cmd, av, cblen);
521	if (cmd->opcode == O_IP_DST_SET)			/* set */
522		;
523	else if (cmd->opcode == O_IP_DST_LOOKUP)		/* table */
524		;
525	else if (F_LEN(cmd) == 0) {				/* any */
526	} else if (F_LEN(cmd) == F_INSN_SIZE(ipfw_insn)) {	/* "me" */
527		cmd->opcode = O_IP6_DST_ME;
528	} else if (F_LEN(cmd) ==
529	    (F_INSN_SIZE(struct in6_addr) + F_INSN_SIZE(ipfw_insn))) {
530		/* single IP, no mask*/
531		cmd->opcode = O_IP6_DST;
532	} else {					/* addr/mask opt */
533		cmd->opcode = O_IP6_DST_MASK;
534	}
535	return cmd;
536}
537