2 * Copyright (c) 2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
31 * Copyright (c) 1994 Ugen J.S.Antsilevich
33 * Idea and grammar partially left from:
34 * Copyright (c) 1993 Daniel Boulet
36 * Redistribution and use in source forms, with and without modification,
37 * are permitted provided that this entire comment appears intact.
39 * Redistribution in binary form may occur without any restrictions.
40 * Obviously, it would be nice if you gave credit where credit is due
41 * but requiring it would be too onerous.
43 * This software is provided ``AS IS'' without any warranties of any kind.
45 * NEW command line interface for IP firewall facility
47 * $Id: ip6fw.c,v 1.3 2006/02/07 06:22:17 lindak Exp $
48 * $FreeBSD: src/sbin/ip6fw/ip6fw.c,v 1.1.2.6 2001/08/01 06:52:18 obrien Exp $
51 #include <sys/types.h>
52 #include <sys/queue.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
56 #include <sys/ioctl.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip6.h>
77 #include <netinet/icmp6.h>
78 #include <netinet6/ip6_fw.h>
79 #include <netinet/tcp.h>
80 #include <arpa/inet.h>
84 int s
; /* main RAW socket */
85 int do_resolv
=0; /* Would try to resolv all */
86 int do_acct
=0; /* Show packet/byte count */
87 int do_time
=0; /* Show time stamps */
88 int do_quiet
=0; /* Be quiet in add and flush */
89 int do_force
=0; /* Don't ask for confirmation */
96 static struct icmpcode icmp6codes
[] = {
97 { ICMP6_DST_UNREACH_NOROUTE
, "noroute" },
98 { ICMP6_DST_UNREACH_ADMIN
, "admin" },
99 { ICMP6_DST_UNREACH_NOTNEIGHBOR
, "notneighbor" },
100 { ICMP6_DST_UNREACH_ADDR
, "addr" },
101 { ICMP6_DST_UNREACH_NOPORT
, "noport" },
105 static char ntop_buf
[INET6_ADDRSTRLEN
];
107 static void show_usage(const char *fmt
, ...);
110 mask_bits(u_char
*m_ad
, int m_len
)
114 for (i
= 0; i
< m_len
; i
++, m_ad
++) {
121 #define MASKLEN(m, l) case m: h_num += l; break
135 static int pl2m
[9] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
137 struct in6_addr
*plen2mask(int n
)
139 static struct in6_addr ia
;
143 memset(&ia
, 0, sizeof(struct in6_addr
));
145 for (i
= 0; i
< 16; i
++, p
++, n
-= 8) {
157 print_port(prot
, port
, comma
)
164 const char *protocol
;
168 pe
= getprotobynumber(prot
);
170 protocol
= pe
->p_name
;
174 se
= getservbyport(htons(port
), protocol
);
176 printf("%s%s", comma
, se
->s_name
);
181 printf("%s%d",comma
,port
);
185 print_iface(char *key
, union ip6_fw_if
*un
, int byname
)
187 char ifnb
[IP6FW_IFNLEN
+1];
190 strncpy(ifnb
, un
->fu_via_if
.name
, IP6FW_IFNLEN
);
191 ifnb
[IP6FW_IFNLEN
]='\0';
192 if (un
->fu_via_if
.unit
== -1)
193 printf(" %s %s*", key
, ifnb
);
195 printf(" %s %s%d", key
, ifnb
, un
->fu_via_if
.unit
);
196 } else if (!IN6_IS_ADDR_UNSPECIFIED(&un
->fu_via_ip6
)) {
197 printf(" %s %s", key
, inet_ntop(AF_INET6
,&un
->fu_via_ip6
,ntop_buf
,sizeof(ntop_buf
)));
199 printf(" %s any", key
);
203 print_reject_code(int code
)
207 for (ic
= icmp6codes
; ic
->str
; ic
++)
208 if (ic
->code
== code
) {
209 printf("%s", ic
->str
);
216 show_ip6fw(struct ip6_fw
*chain
)
222 int nsp
= IPV6_FW_GETNSRCP(chain
);
223 int ndp
= IPV6_FW_GETNDSTP(chain
);
226 setservent(1/*stayopen*/);
228 printf("%05u ", chain
->fw_number
);
231 printf("%10u %10u ",chain
->fw_pcnt
,chain
->fw_bcnt
);
235 if (chain
->timestamp
)
239 strlcpy(timestr
, ctime((time_t *)&chain
->timestamp
), sizeof(timestr
));
240 *strchr(timestr
, '\n') = '\0';
241 printf("%s ", timestr
);
247 switch (chain
->fw_flg
& IPV6_FW_F_COMMAND
)
249 case IPV6_FW_F_ACCEPT
:
255 case IPV6_FW_F_COUNT
:
258 case IPV6_FW_F_DIVERT
:
259 printf("divert %u", chain
->fw_divert_port
);
262 printf("tee %u", chain
->fw_divert_port
);
264 case IPV6_FW_F_SKIPTO
:
265 printf("skipto %u", chain
->fw_skipto_rule
);
267 case IPV6_FW_F_REJECT
:
268 if (chain
->fw_reject_code
== IPV6_FW_REJECT_RST
)
272 print_reject_code(chain
->fw_reject_code
);
276 errx(EX_OSERR
, "impossible");
279 if (chain
->fw_flg
& IPV6_FW_F_PRN
)
282 pe
= getprotobynumber(chain
->fw_prot
);
284 printf(" %s", pe
->p_name
);
286 printf(" %u", chain
->fw_prot
);
288 printf(" from %s", chain
->fw_flg
& IPV6_FW_F_INVSRC
? "not " : "");
290 mb
=mask_bits((u_char
*)&chain
->fw_smsk
,sizeof(chain
->fw_smsk
));
291 if (mb
==128 && do_resolv
) {
292 he
=gethostbyaddr((char *)&(chain
->fw_src
),sizeof(chain
->fw_src
),AF_INET6
);
294 printf("%s", inet_ntop(AF_INET6
,&chain
->fw_src
,ntop_buf
,sizeof(ntop_buf
)));
296 printf("%s",he
->h_name
);
302 printf("%s", inet_ntop(AF_INET6
,&chain
->fw_src
,ntop_buf
,sizeof(ntop_buf
)));
306 printf("%s", inet_ntop(AF_INET6
,&chain
->fw_src
,ntop_buf
,sizeof(ntop_buf
)));
309 if (chain
->fw_prot
== IPPROTO_TCP
|| chain
->fw_prot
== IPPROTO_UDP
) {
311 for (i
= 0; i
< nsp
; i
++) {
312 print_port(chain
->fw_prot
, chain
->fw_pts
[i
], comma
);
313 if (i
==0 && (chain
->fw_flg
& IPV6_FW_F_SRNG
))
320 printf(" to %s", chain
->fw_flg
& IPV6_FW_F_INVDST
? "not " : "");
322 mb
=mask_bits((u_char
*)&chain
->fw_dmsk
,sizeof(chain
->fw_dmsk
));
323 if (mb
==128 && do_resolv
) {
324 he
=gethostbyaddr((char *)&(chain
->fw_dst
),sizeof(chain
->fw_dst
),AF_INET
);
326 printf("%s", inet_ntop(AF_INET6
,&chain
->fw_dst
,ntop_buf
,sizeof(ntop_buf
)));
328 printf("%s",he
->h_name
);
334 printf("%s", inet_ntop(AF_INET6
,&chain
->fw_dst
,ntop_buf
,sizeof(ntop_buf
)));
338 printf("%s", inet_ntop(AF_INET6
,&chain
->fw_dst
,ntop_buf
,sizeof(ntop_buf
)));
341 if (chain
->fw_prot
== IPPROTO_TCP
|| chain
->fw_prot
== IPPROTO_UDP
) {
343 for (i
= 0; i
< ndp
; i
++) {
344 print_port(chain
->fw_prot
, chain
->fw_pts
[nsp
+i
], comma
);
345 if (i
==0 && (chain
->fw_flg
& IPV6_FW_F_DRNG
))
353 if ((chain
->fw_flg
& IPV6_FW_F_IN
) && !(chain
->fw_flg
& IPV6_FW_F_OUT
))
355 if (!(chain
->fw_flg
& IPV6_FW_F_IN
) && (chain
->fw_flg
& IPV6_FW_F_OUT
))
358 /* Handle hack for "via" backwards compatibility */
359 if ((chain
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
361 &chain
->fw_in_if
, chain
->fw_flg
& IPV6_FW_F_IIFNAME
);
363 /* Receive interface specified */
364 if (chain
->fw_flg
& IPV6_FW_F_IIFACE
)
365 print_iface("recv", &chain
->fw_in_if
,
366 chain
->fw_flg
& IPV6_FW_F_IIFNAME
);
367 /* Transmit interface specified */
368 if (chain
->fw_flg
& IPV6_FW_F_OIFACE
)
369 print_iface("xmit", &chain
->fw_out_if
,
370 chain
->fw_flg
& IPV6_FW_F_OIFNAME
);
373 if (chain
->fw_flg
& IPV6_FW_F_FRAG
)
376 if (chain
->fw_ip6opt
|| chain
->fw_ip6nopt
) {
377 int _opt_printed
= 0;
378 #define PRINTOPT(x) {if (_opt_printed) printf(",");\
379 printf(x); _opt_printed = 1;}
382 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_HOPOPT
) PRINTOPT("hopopt");
383 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_HOPOPT
) PRINTOPT("!hopopt");
384 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_ROUTE
) PRINTOPT("route");
385 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_ROUTE
) PRINTOPT("!route");
386 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_FRAG
) PRINTOPT("frag");
387 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_FRAG
) PRINTOPT("!frag");
388 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_ESP
) PRINTOPT("esp");
389 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_ESP
) PRINTOPT("!esp");
390 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_AH
) PRINTOPT("ah");
391 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_AH
) PRINTOPT("!ah");
392 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_NONXT
) PRINTOPT("nonxt");
393 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_NONXT
) PRINTOPT("!nonxt");
394 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_OPTS
) PRINTOPT("opts");
395 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_OPTS
) PRINTOPT("!opts");
398 if (chain
->fw_ipflg
& IPV6_FW_IF_TCPEST
)
399 printf(" established");
400 else if (chain
->fw_tcpf
== IPV6_FW_TCPF_SYN
&&
401 chain
->fw_tcpnf
== IPV6_FW_TCPF_ACK
)
403 else if (chain
->fw_tcpf
|| chain
->fw_tcpnf
) {
404 int _flg_printed
= 0;
405 #define PRINTFLG(x) {if (_flg_printed) printf(",");\
406 printf(x); _flg_printed = 1;}
409 if (chain
->fw_tcpf
& IPV6_FW_TCPF_FIN
) PRINTFLG("fin");
410 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_FIN
) PRINTFLG("!fin");
411 if (chain
->fw_tcpf
& IPV6_FW_TCPF_SYN
) PRINTFLG("syn");
412 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_SYN
) PRINTFLG("!syn");
413 if (chain
->fw_tcpf
& IPV6_FW_TCPF_RST
) PRINTFLG("rst");
414 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_RST
) PRINTFLG("!rst");
415 if (chain
->fw_tcpf
& IPV6_FW_TCPF_PSH
) PRINTFLG("psh");
416 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_PSH
) PRINTFLG("!psh");
417 if (chain
->fw_tcpf
& IPV6_FW_TCPF_ACK
) PRINTFLG("ack");
418 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_ACK
) PRINTFLG("!ack");
419 if (chain
->fw_tcpf
& IPV6_FW_TCPF_URG
) PRINTFLG("urg");
420 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_URG
) PRINTFLG("!urg");
422 if (chain
->fw_flg
& IPV6_FW_F_ICMPBIT
) {
428 for (type_index
= 0; type_index
< IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8; ++type_index
)
429 if (chain
->fw_icmp6types
[type_index
/ (sizeof(unsigned) * 8)] &
430 (1U << (type_index
% (sizeof(unsigned) * 8)))) {
431 printf("%c%d", first
== 1 ? ' ' : ',', type_index
);
445 struct ip6_fw
*r
, *rules
;
447 unsigned long rulenum
;
448 int nalloc
, maxbytes
;
451 /* extract rules from kernel, resizing array as necessary */
453 nalloc
= sizeof *rules
;
455 maxbytes
= 65536 * sizeof *rules
;
456 while (bytes
>= nalloc
) {
457 nalloc
= nalloc
* 2 + 200;
459 if ((rules
= realloc(rules
, bytes
)) == NULL
)
460 err(EX_OSERR
, "realloc");
461 memset(rules
, 0, sizeof *rules
);
462 rules
->version
= IPV6_FW_CURRENT_API_VERSION
;
463 i
= getsockopt(s
, IPPROTO_IPV6
, IPV6_FW_GET
, rules
, &bytes
);
464 if ((i
< 0 && errno
!= EINVAL
) || nalloc
> maxbytes
)
465 err(EX_OSERR
, "getsockopt(IPV6_FW_GET)");
468 /* display all rules */
469 for (r
= rules
, l
= bytes
; l
>= sizeof rules
[0];
470 r
++, l
-=sizeof rules
[0])
474 /* display specific rules requested on command line */
481 /* convert command line rule # */
482 rulenum
= strtoul(*av
++, &endptr
, 10);
485 warn("invalid rule number: %s", av
[-1]);
489 for (r
= rules
, l
= bytes
;
490 l
>= sizeof rules
[0] && r
->fw_number
<= rulenum
;
491 r
++, l
-=sizeof rules
[0])
492 if (rulenum
== r
->fw_number
) {
498 warnx("rule %lu does not exist", rulenum
);
507 show_usage(const char *fmt
, ...)
514 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
516 warnx("error: %s", buf
);
518 fprintf(stderr
, "usage: ip6fw [options]\n"
520 " add [number] rule\n"
521 " delete number ...\n"
522 " list [number ...]\n"
523 " show [number ...]\n"
524 " zero [number ...]\n"
525 " rule: action proto src dst extras...\n"
527 " {allow|permit|accept|pass|deny|drop|reject|unreach code|\n"
528 " reset|count|skipto num} [log]\n"
529 " proto: {ipv6|tcp|udp|ipv6-icmp|<number>}\n"
530 " src: from [not] {any|ipv6[/prefixlen]} [{port|port-port},[port],...]\n"
531 " dst: to [not] {any|ipv6[/prefixlen]} [{port|port-port},[port],...]\n"
533 " fragment (may not be used with ports or tcpflags)\n"
536 " {xmit|recv|via} {iface|ipv6|any}\n"
537 " {established|setup}\n"
538 " tcpflags [!]{syn|fin|rst|ack|psh|urg},...\n"
539 " ipv6options [!]{hopopt|route|frag|esp|ah|nonxt|opts},...\n"
540 " icmptypes {type[,type]}...\n");
546 lookup_host (host
, addr
, family
)
550 struct hostent
*he
= gethostbyname2(host
, family
);
555 memcpy(addr
, he
->h_addr_list
[0], he
->h_length
);
561 fill_ip6(ipno
, mask
, acp
, avp
)
562 struct in6_addr
*ipno
, *mask
;
571 if (ac
&& !strncmp(*av
,"any",strlen(*av
))) {
572 *ipno
= *mask
= in6addr_any
; av
++; ac
--;
574 p
= strchr(*av
, '/');
580 if (lookup_host(*av
, (u_char
*)ipno
, AF_INET6
) != 0)
581 show_usage("hostname ``%s'' unknown", *av
);
586 } else if (atoi(p
) > 128) {
587 show_usage("bad width ``%s''", p
);
589 *mask
= *(plen2mask(atoi(p
)));
593 *mask
= *(plen2mask(128));
596 for (i
= 0; i
< sizeof(*ipno
); i
++)
597 ipno
->s6_addr
[i
] &= mask
->s6_addr
[i
];
606 fill_reject_code6(u_short
*codep
, char *str
)
612 val
= strtoul(str
, &s
, 0);
613 if (s
!= str
&& *s
== '\0' && val
< 0x100) {
617 for (ic
= icmp6codes
; ic
->str
; ic
++)
618 if (!strcasecmp(str
, ic
->str
)) {
622 show_usage("unknown ICMP6 unreachable code ``%s''", str
);
626 add_port(cnt
, ptr
, off
, port
)
627 u_short
*cnt
, *ptr
, off
, port
;
629 if (off
+ *cnt
>= IPV6_FW_MAX_PORTS
)
630 errx(1, "too many ports (max is %d)", IPV6_FW_MAX_PORTS
);
631 ptr
[off
+*cnt
] = port
;
636 lookup_port(const char *arg
, int test
, int nodash
)
642 snprintf(buf
, sizeof(buf
), "%s", arg
);
643 buf
[strcspn(arg
, nodash
? "-," : ",")] = 0;
644 val
= (int) strtoul(buf
, &earg
, 0);
645 if (!*buf
|| *earg
) {
647 if ((s
= getservbyname(buf
, NULL
))) {
648 val
= htons(s
->s_port
);
651 errx(1, "unknown port ``%s''", arg
);
656 if (val
< 0 || val
> 0xffff) {
658 errx(1, "port ``%s'' out of range", arg
);
667 fill_port(cnt
, ptr
, off
, arg
)
668 u_short
*cnt
, *ptr
, off
;
672 int initial_range
= 0;
674 s
= arg
+ strcspn(arg
, "-,"); /* first port name can't have a dash */
677 if (strchr(arg
, ','))
678 errx(1, "port range must be first in list");
679 add_port(cnt
, ptr
, off
, *arg
? lookup_port(arg
, 0, 0) : 0x0000);
684 add_port(cnt
, ptr
, off
, *arg
? lookup_port(arg
, 0, 0) : 0xffff);
688 while (arg
!= NULL
) {
692 add_port(cnt
, ptr
, off
, lookup_port(arg
, 0, 0));
695 return initial_range
;
699 fill_tcpflag(set
, reset
, vp
)
711 { "syn", IPV6_FW_TCPF_SYN
},
712 { "fin", IPV6_FW_TCPF_FIN
},
713 { "ack", IPV6_FW_TCPF_ACK
},
714 { "psh", IPV6_FW_TCPF_PSH
},
715 { "rst", IPV6_FW_TCPF_RST
},
716 { "urg", IPV6_FW_TCPF_URG
}
729 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); ++i
)
730 if (!strncmp(p
, flags
[i
].name
, strlen(p
))) {
731 *d
|= flags
[i
].value
;
734 if (i
== sizeof(flags
) / sizeof(flags
[0]))
735 show_usage("invalid tcp flag ``%s''", p
);
741 fill_ip6opt(u_char
*set
, u_char
*reset
, char **vp
)
756 if (!strncmp(p
,"hopopt",strlen(p
))) *d
|= IPV6_FW_IP6OPT_HOPOPT
;
757 if (!strncmp(p
,"route",strlen(p
))) *d
|= IPV6_FW_IP6OPT_ROUTE
;
758 if (!strncmp(p
,"frag",strlen(p
))) *d
|= IPV6_FW_IP6OPT_FRAG
;
759 if (!strncmp(p
,"esp",strlen(p
))) *d
|= IPV6_FW_IP6OPT_ESP
;
760 if (!strncmp(p
,"ah",strlen(p
))) *d
|= IPV6_FW_IP6OPT_AH
;
761 if (!strncmp(p
,"nonxt",strlen(p
))) *d
|= IPV6_FW_IP6OPT_NONXT
;
762 if (!strncmp(p
,"opts",strlen(p
))) *d
|= IPV6_FW_IP6OPT_OPTS
;
768 fill_icmptypes(types
, vp
, fw_flg
)
777 unsigned long icmptype
;
782 icmptype
= strtoul(c
, &c
, 0);
784 if ( *c
!= ',' && *c
!= '\0' )
785 show_usage("invalid ICMP6 type");
787 if (icmptype
>= IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8)
788 show_usage("ICMP6 type out of range");
790 types
[icmptype
/ (sizeof(unsigned) * 8)] |=
791 1U << (icmptype
% (sizeof(unsigned) * 8));
792 *fw_flg
|= IPV6_FW_F_ICMPBIT
;
805 memset(&rule
, 0, sizeof rule
);
806 rule
.version
= IPV6_FW_CURRENT_API_VERSION
;
811 while (ac
&& isdigit(**av
)) {
812 rule
.fw_number
= atoi(*av
); av
++; ac
--;
813 i
= setsockopt(s
, IPPROTO_IPV6
, IPV6_FW_DEL
, &rule
, sizeof rule
);
816 warn("rule %u: setsockopt(%s)", rule
.fw_number
, "IPV6_FW_DEL");
824 verify_interface(union ip6_fw_if
*ifu
)
829 * If a unit was specified, check for that exact interface.
830 * If a wildcard was specified, check for unit 0.
832 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d",
834 ifu
->fu_via_if
.unit
== -1 ? 0 : ifu
->fu_via_if
.unit
);
836 if (ioctl(s
, SIOCGIFFLAGS
, &ifr
) < 0)
837 warnx("warning: interface ``%s'' does not exist", ifr
.ifr_name
);
841 fill_iface(char *which
, union ip6_fw_if
*ifu
, int *byname
, int ac
, char *arg
)
844 show_usage("missing argument for ``%s''", which
);
846 /* Parse the interface or address */
847 if (!strcmp(arg
, "any")) {
848 ifu
->fu_via_ip6
= in6addr_any
;
850 } else if (!isdigit(*arg
)) {
854 strncpy(ifu
->fu_via_if
.name
, arg
, sizeof(ifu
->fu_via_if
.name
));
855 ifu
->fu_via_if
.name
[sizeof(ifu
->fu_via_if
.name
) - 1] = '\0';
856 for (q
= ifu
->fu_via_if
.name
;
857 *q
&& !isdigit(*q
) && *q
!= '*'; q
++)
859 ifu
->fu_via_if
.unit
= (*q
== '*') ? -1 : atoi(q
);
861 verify_interface(ifu
);
862 } else if (inet_pton(AF_INET6
, arg
, &ifu
->fu_via_ip6
) != 1) {
863 show_usage("bad ip6 address ``%s''", arg
);
877 int saw_xmrc
= 0, saw_via
= 0;
879 memset(&rule
, 0, sizeof rule
);
880 rule
.version
= IPV6_FW_CURRENT_API_VERSION
;
885 if (ac
&& isdigit(**av
)) {
886 rule
.fw_number
= atoi(*av
); av
++; ac
--;
891 show_usage("missing action");
892 if (!strncmp(*av
,"accept",strlen(*av
))
893 || !strncmp(*av
,"pass",strlen(*av
))
894 || !strncmp(*av
,"allow",strlen(*av
))
895 || !strncmp(*av
,"permit",strlen(*av
))) {
896 rule
.fw_flg
|= IPV6_FW_F_ACCEPT
; av
++; ac
--;
897 } else if (!strncmp(*av
,"count",strlen(*av
))) {
898 rule
.fw_flg
|= IPV6_FW_F_COUNT
; av
++; ac
--;
901 else if (!strncmp(*av
,"divert",strlen(*av
))) {
902 rule
.fw_flg
|= IPV6_FW_F_DIVERT
; av
++; ac
--;
904 show_usage("missing divert port");
905 rule
.fw_divert_port
= strtoul(*av
, NULL
, 0); av
++; ac
--;
906 if (rule
.fw_divert_port
== 0) {
909 s
= getservbyname(av
[-1], "divert");
911 rule
.fw_divert_port
= ntohs(s
->s_port
);
913 show_usage("illegal divert port");
915 } else if (!strncmp(*av
,"tee",strlen(*av
))) {
916 rule
.fw_flg
|= IPV6_FW_F_TEE
; av
++; ac
--;
918 show_usage("missing divert port");
919 rule
.fw_divert_port
= strtoul(*av
, NULL
, 0); av
++; ac
--;
920 if (rule
.fw_divert_port
== 0) {
923 s
= getservbyname(av
[-1], "divert");
925 rule
.fw_divert_port
= ntohs(s
->s_port
);
927 show_usage("illegal divert port");
931 else if (!strncmp(*av
,"skipto",strlen(*av
))) {
932 rule
.fw_flg
|= IPV6_FW_F_SKIPTO
; av
++; ac
--;
934 show_usage("missing skipto rule number");
935 rule
.fw_skipto_rule
= strtoul(*av
, NULL
, 0); av
++; ac
--;
936 } else if ((!strncmp(*av
,"deny",strlen(*av
))
937 || !strncmp(*av
,"drop",strlen(*av
)))) {
938 rule
.fw_flg
|= IPV6_FW_F_DENY
; av
++; ac
--;
939 } else if (!strncmp(*av
,"reject",strlen(*av
))) {
940 rule
.fw_flg
|= IPV6_FW_F_REJECT
; av
++; ac
--;
941 rule
.fw_reject_code
= ICMP6_DST_UNREACH_NOROUTE
;
942 } else if (!strncmp(*av
,"reset",strlen(*av
))) {
943 rule
.fw_flg
|= IPV6_FW_F_REJECT
; av
++; ac
--;
944 rule
.fw_reject_code
= IPV6_FW_REJECT_RST
; /* check TCP later */
945 } else if (!strncmp(*av
,"unreach",strlen(*av
))) {
946 rule
.fw_flg
|= IPV6_FW_F_REJECT
; av
++; ac
--;
947 fill_reject_code6(&rule
.fw_reject_code
, *av
); av
++; ac
--;
949 show_usage("invalid action ``%s''", *av
);
953 if (ac
&& !strncmp(*av
,"log",strlen(*av
))) {
954 rule
.fw_flg
|= IPV6_FW_F_PRN
; av
++; ac
--;
959 show_usage("missing protocol");
960 if ((proto
= atoi(*av
)) > 0) {
961 rule
.fw_prot
= proto
; av
++; ac
--;
962 } else if (!strncmp(*av
,"all",strlen(*av
))) {
963 rule
.fw_prot
= IPPROTO_IPV6
; av
++; ac
--;
964 } else if ((pe
= getprotobyname(*av
)) != NULL
) {
965 rule
.fw_prot
= pe
->p_proto
; av
++; ac
--;
967 show_usage("invalid protocol ``%s''", *av
);
970 if (rule
.fw_prot
!= IPPROTO_TCP
971 && (rule
.fw_flg
& IPV6_FW_F_COMMAND
) == IPV6_FW_F_REJECT
972 && rule
.fw_reject_code
== IPV6_FW_REJECT_RST
)
973 show_usage("``reset'' is only valid for tcp packets");
976 if (ac
&& !strncmp(*av
,"from",strlen(*av
))) { av
++; ac
--; }
978 show_usage("missing ``from''");
980 if (ac
&& !strncmp(*av
,"not",strlen(*av
))) {
981 rule
.fw_flg
|= IPV6_FW_F_INVSRC
;
985 show_usage("missing arguments");
987 fill_ip6(&rule
.fw_src
, &rule
.fw_smsk
, &ac
, &av
);
989 if (ac
&& (isdigit(**av
) || lookup_port(*av
, 1, 1) >= 0)) {
992 if (fill_port(&nports
, rule
.fw_pts
, 0, *av
))
993 rule
.fw_flg
|= IPV6_FW_F_SRNG
;
994 IPV6_FW_SETNSRCP(&rule
, nports
);
999 if (ac
&& !strncmp(*av
,"to",strlen(*av
))) { av
++; ac
--; }
1001 show_usage("missing ``to''");
1003 if (ac
&& !strncmp(*av
,"not",strlen(*av
))) {
1004 rule
.fw_flg
|= IPV6_FW_F_INVDST
;
1008 show_usage("missing arguments");
1010 fill_ip6(&rule
.fw_dst
, &rule
.fw_dmsk
, &ac
, &av
);
1012 if (ac
&& (isdigit(**av
) || lookup_port(*av
, 1, 1) >= 0)) {
1015 if (fill_port(&nports
,
1016 rule
.fw_pts
, IPV6_FW_GETNSRCP(&rule
), *av
))
1017 rule
.fw_flg
|= IPV6_FW_F_DRNG
;
1018 IPV6_FW_SETNDSTP(&rule
, nports
);
1022 if ((rule
.fw_prot
!= IPPROTO_TCP
) && (rule
.fw_prot
!= IPPROTO_UDP
)
1023 && (IPV6_FW_GETNSRCP(&rule
) || IPV6_FW_GETNDSTP(&rule
))) {
1024 show_usage("only TCP and UDP protocols are valid"
1025 " with port specifications");
1029 if (!strncmp(*av
,"in",strlen(*av
))) {
1030 rule
.fw_flg
|= IPV6_FW_F_IN
;
1031 av
++; ac
--; continue;
1033 if (!strncmp(*av
,"out",strlen(*av
))) {
1034 rule
.fw_flg
|= IPV6_FW_F_OUT
;
1035 av
++; ac
--; continue;
1037 if (ac
&& !strncmp(*av
,"xmit",strlen(*av
))) {
1038 union ip6_fw_if ifu
;
1043 show_usage("``via'' is incompatible"
1044 " with ``xmit'' and ``recv''");
1048 fill_iface("xmit", &ifu
, &byname
, ac
, *av
);
1049 rule
.fw_out_if
= ifu
;
1050 rule
.fw_flg
|= IPV6_FW_F_OIFACE
;
1052 rule
.fw_flg
|= IPV6_FW_F_OIFNAME
;
1053 av
++; ac
--; continue;
1055 if (ac
&& !strncmp(*av
,"recv",strlen(*av
))) {
1056 union ip6_fw_if ifu
;
1063 fill_iface("recv", &ifu
, &byname
, ac
, *av
);
1064 rule
.fw_in_if
= ifu
;
1065 rule
.fw_flg
|= IPV6_FW_F_IIFACE
;
1067 rule
.fw_flg
|= IPV6_FW_F_IIFNAME
;
1068 av
++; ac
--; continue;
1070 if (ac
&& !strncmp(*av
,"via",strlen(*av
))) {
1071 union ip6_fw_if ifu
;
1078 fill_iface("via", &ifu
, &byname
, ac
, *av
);
1079 rule
.fw_out_if
= rule
.fw_in_if
= ifu
;
1082 (IPV6_FW_F_IIFNAME
| IPV6_FW_F_OIFNAME
);
1083 av
++; ac
--; continue;
1085 if (!strncmp(*av
,"fragment",strlen(*av
))) {
1086 rule
.fw_flg
|= IPV6_FW_F_FRAG
;
1087 av
++; ac
--; continue;
1089 if (!strncmp(*av
,"ipv6options",strlen(*av
))) {
1092 show_usage("missing argument"
1093 " for ``ipv6options''");
1094 fill_ip6opt(&rule
.fw_ip6opt
, &rule
.fw_ip6nopt
, av
);
1095 av
++; ac
--; continue;
1097 if (rule
.fw_prot
== IPPROTO_TCP
) {
1098 if (!strncmp(*av
,"established",strlen(*av
))) {
1099 rule
.fw_ipflg
|= IPV6_FW_IF_TCPEST
;
1100 av
++; ac
--; continue;
1102 if (!strncmp(*av
,"setup",strlen(*av
))) {
1103 rule
.fw_tcpf
|= IPV6_FW_TCPF_SYN
;
1104 rule
.fw_tcpnf
|= IPV6_FW_TCPF_ACK
;
1105 av
++; ac
--; continue;
1107 if (!strncmp(*av
,"tcpflags",strlen(*av
))) {
1110 show_usage("missing argument"
1111 " for ``tcpflags''");
1112 fill_tcpflag(&rule
.fw_tcpf
, &rule
.fw_tcpnf
, av
);
1113 av
++; ac
--; continue;
1116 if (rule
.fw_prot
== IPPROTO_ICMPV6
) {
1117 if (!strncmp(*av
,"icmptypes",strlen(*av
))) {
1120 show_usage("missing argument"
1121 " for ``icmptypes''");
1122 fill_icmptypes(rule
.fw_icmp6types
,
1124 av
++; ac
--; continue;
1127 show_usage("unknown argument ``%s''", *av
);
1130 /* No direction specified -> do both directions */
1131 if (!(rule
.fw_flg
& (IPV6_FW_F_OUT
|IPV6_FW_F_IN
)))
1132 rule
.fw_flg
|= (IPV6_FW_F_OUT
|IPV6_FW_F_IN
);
1134 /* Sanity check interface check, but handle "via" case separately */
1136 if (rule
.fw_flg
& IPV6_FW_F_IN
)
1137 rule
.fw_flg
|= IPV6_FW_F_IIFACE
;
1138 if (rule
.fw_flg
& IPV6_FW_F_OUT
)
1139 rule
.fw_flg
|= IPV6_FW_F_OIFACE
;
1140 } else if ((rule
.fw_flg
& IPV6_FW_F_OIFACE
) && (rule
.fw_flg
& IPV6_FW_F_IN
))
1141 show_usage("can't check xmit interface of incoming packets");
1143 /* frag may not be used in conjunction with ports or TCP flags */
1144 if (rule
.fw_flg
& IPV6_FW_F_FRAG
) {
1145 if (rule
.fw_tcpf
|| rule
.fw_tcpnf
)
1146 show_usage("can't mix 'frag' and tcpflags");
1149 show_usage("can't mix 'frag' and port specifications");
1152 i
= setsockopt(s
, IPPROTO_IPV6
, IPV6_FW_ADD
, &rule
, sizeof rule
);
1154 err(EX_UNAVAILABLE
, "setsockopt(%s)", "IPV6_FW_ADD");
1167 memset(&rule
, 0, sizeof rule
);
1168 rule
.version
= IPV6_FW_CURRENT_API_VERSION
;
1171 /* clear all entries */
1172 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_FW_ZERO
, &rule
, sizeof rule
) < 0)
1173 err(EX_UNAVAILABLE
, "setsockopt(%s)", "IPV6_FW_ZERO");
1175 printf("Accounting cleared.\n");
1181 if (isdigit(**av
)) {
1182 rule
.fw_number
= atoi(*av
); av
++; ac
--;
1183 if (setsockopt(s
, IPPROTO_IPV6
,
1184 IPV6_FW_ZERO
, &rule
, sizeof rule
)) {
1185 warn("rule %u: setsockopt(%s)", rule
.fw_number
,
1190 printf("Entry %d cleared\n",
1193 show_usage("invalid rule number ``%s''", *av
);
1208 /* init optind to 1 */
1215 /* Set the force flag for non-interactive processes */
1216 do_force
= !isatty(STDIN_FILENO
);
1218 while ((ch
= getopt(ac
, av
,"afqtN")) != -1)
1240 if (*(av
+=optind
)==NULL
) {
1241 show_usage("Bad arguments");
1244 if (!strncmp(*av
, "add", strlen(*av
))) {
1246 } else if (!strncmp(*av
, "delete", strlen(*av
))) {
1248 } else if (!strncmp(*av
, "flush", strlen(*av
))) {
1251 if ( do_force
|| do_quiet
)
1257 printf("Are you sure? [yn] ");
1260 c
= toupper(getc(stdin
));
1261 while (c
!= '\n' && getc(stdin
) != '\n')
1264 } while (c
!= 'Y' && c
!= 'N');
1272 memset(&rule
, 0, sizeof rule
);
1273 rule
.version
= IPV6_FW_CURRENT_API_VERSION
;
1274 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_FW_FLUSH
, &rule
, sizeof rule
) < 0)
1275 err(EX_UNAVAILABLE
, "setsockopt(%s)", "IPV6_FW_FLUSH");
1277 printf("Flushed all rules.\n");
1279 } else if (!strncmp(*av
, "zero", strlen(*av
))) {
1281 } else if (!strncmp(*av
, "print", strlen(*av
))) {
1283 } else if (!strncmp(*av
, "list", strlen(*av
))) {
1285 } else if (!strncmp(*av
, "show", strlen(*av
))) {
1289 show_usage("Bad arguments");
1300 #define WHITESP " \t\f\v\n\r"
1302 char *a
, *p
, *args
[MAX_ARGS
], *cmd
= NULL
;
1304 int i
, c
, lineno
, qflag
, pflag
, status
;
1308 s
= socket( AF_INET6
, SOCK_RAW
, IPPROTO_RAW
);
1310 err(EX_UNAVAILABLE
, "socket");
1315 * Only interpret the last command line argument as a file to
1316 * be preprocessed if it is specified as an absolute pathname.
1319 if (ac
> 1 && av
[ac
- 1][0] == '/' && access(av
[ac
- 1], R_OK
) == 0) {
1320 qflag
= pflag
= i
= 0;
1323 while ((c
= getopt(ac
, av
, "D:U:p:q")) != -1)
1327 errx(EX_USAGE
, "-D requires -p");
1328 if (i
> MAX_ARGS
- 2)
1330 "too many -D or -U options");
1337 errx(EX_USAGE
, "-U requires -p");
1338 if (i
> MAX_ARGS
- 2)
1340 "too many -D or -U options");
1363 show_usage("extraneous filename arguments");
1365 if ((f
= fopen(av
[0], "r")) == NULL
)
1366 err(EX_UNAVAILABLE
, "fopen: %s", av
[0]);
1369 /* pipe through preprocessor (cpp or m4) */
1374 if (pipe(pipedes
) == -1)
1375 err(EX_OSERR
, "cannot create pipe");
1377 switch((preproc
= fork())) {
1379 err(EX_OSERR
, "cannot fork");
1383 if (dup2(fileno(f
), 0) == -1 ||
1384 dup2(pipedes
[1], 1) == -1)
1385 err(EX_OSERR
, "dup2()");
1390 err(EX_OSERR
, "execvp(%s) failed", cmd
);
1396 if ((f
= fdopen(pipedes
[0], "r")) == NULL
) {
1397 int savederrno
= errno
;
1399 (void)kill(preproc
, SIGTERM
);
1401 err(EX_OSERR
, "fdopen()");
1406 while (fgets(buf
, BUFSIZ
, f
)) {
1408 snprintf(linename
, sizeof(linename
), "Line %d", lineno
);
1413 if ((p
= strchr(buf
, '#')) != NULL
)
1416 if (qflag
) args
[i
++]="-q";
1417 for (a
= strtok(buf
, WHITESP
);
1418 a
&& i
< MAX_ARGS
; a
= strtok(NULL
, WHITESP
), i
++)
1420 if (i
== (qflag
? 2: 1))
1423 errx(EX_USAGE
, "%s: too many arguments", linename
);
1426 ip6fw_main(i
, args
);
1430 if (waitpid(preproc
, &status
, 0) != -1) {
1431 if (WIFEXITED(status
)) {
1432 if (WEXITSTATUS(status
) != EX_OK
)
1433 errx(EX_UNAVAILABLE
,
1434 "preprocessor exited with status %d",
1435 WEXITSTATUS(status
));
1436 } else if (WIFSIGNALED(status
)) {
1437 errx(EX_UNAVAILABLE
,
1438 "preprocessor exited with signal %d",