2 * Copyright (c) 1996 Alex Nash, Paul Traina, Poul-Henning Kamp
3 * Copyright (c) 1994 Ugen J.S.Antsilevich
5 * Idea and grammar partially left from:
6 * Copyright (c) 1993 Daniel Boulet
8 * Redistribution and use in source forms, with and without modification,
9 * are permitted provided that this entire comment appears intact.
11 * Redistribution in binary form may occur without any restrictions.
12 * Obviously, it would be nice if you gave credit where credit is due
13 * but requiring it would be too onerous.
15 * This software is provided ``AS IS'' without any warranties of any kind.
17 * NEW command line interface for IP firewall facility
19 * $Id: ip6fw.c,v 1.3 2006/02/07 06:22:17 lindak Exp $
20 * $FreeBSD: src/sbin/ip6fw/ip6fw.c,v 1.1.2.6 2001/08/01 06:52:18 obrien Exp $
23 #include <sys/types.h>
24 #include <sys/queue.h>
25 #include <sys/socket.h>
26 #include <sys/sockio.h>
28 #include <sys/ioctl.h>
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip6.h>
49 #include <netinet/icmp6.h>
50 #include <netinet6/ip6_fw.h>
51 #include <netinet/tcp.h>
52 #include <arpa/inet.h>
56 int s
; /* main RAW socket */
57 int do_resolv
=0; /* Would try to resolv all */
58 int do_acct
=0; /* Show packet/byte count */
59 int do_time
=0; /* Show time stamps */
60 int do_quiet
=0; /* Be quiet in add and flush */
61 int do_force
=0; /* Don't ask for confirmation */
68 static struct icmpcode icmp6codes
[] = {
69 { ICMP6_DST_UNREACH_NOROUTE
, "noroute" },
70 { ICMP6_DST_UNREACH_ADMIN
, "admin" },
71 { ICMP6_DST_UNREACH_NOTNEIGHBOR
, "notneighbor" },
72 { ICMP6_DST_UNREACH_ADDR
, "addr" },
73 { ICMP6_DST_UNREACH_NOPORT
, "noport" },
77 static char ntop_buf
[INET6_ADDRSTRLEN
];
79 static void show_usage(const char *fmt
, ...);
82 mask_bits(u_char
*m_ad
, int m_len
)
86 for (i
= 0; i
< m_len
; i
++, m_ad
++) {
93 #define MASKLEN(m, l) case m: h_num += l; break
107 static int pl2m
[9] = { 0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff };
109 struct in6_addr
*plen2mask(int n
)
111 static struct in6_addr ia
;
115 memset(&ia
, 0, sizeof(struct in6_addr
));
117 for (i
= 0; i
< 16; i
++, p
++, n
-= 8) {
129 print_port(prot
, port
, comma
)
136 const char *protocol
;
140 pe
= getprotobynumber(prot
);
142 protocol
= pe
->p_name
;
146 se
= getservbyport(htons(port
), protocol
);
148 printf("%s%s", comma
, se
->s_name
);
153 printf("%s%d",comma
,port
);
157 print_iface(char *key
, union ip6_fw_if
*un
, int byname
)
159 char ifnb
[IP6FW_IFNLEN
+1];
162 strncpy(ifnb
, un
->fu_via_if
.name
, IP6FW_IFNLEN
);
163 ifnb
[IP6FW_IFNLEN
]='\0';
164 if (un
->fu_via_if
.unit
== -1)
165 printf(" %s %s*", key
, ifnb
);
167 printf(" %s %s%d", key
, ifnb
, un
->fu_via_if
.unit
);
168 } else if (!IN6_IS_ADDR_UNSPECIFIED(&un
->fu_via_ip6
)) {
169 printf(" %s %s", key
, inet_ntop(AF_INET6
,&un
->fu_via_ip6
,ntop_buf
,sizeof(ntop_buf
)));
171 printf(" %s any", key
);
175 print_reject_code(int code
)
179 for (ic
= icmp6codes
; ic
->str
; ic
++)
180 if (ic
->code
== code
) {
181 printf("%s", ic
->str
);
188 show_ip6fw(struct ip6_fw
*chain
)
194 int nsp
= IPV6_FW_GETNSRCP(chain
);
195 int ndp
= IPV6_FW_GETNDSTP(chain
);
198 setservent(1/*stayopen*/);
200 printf("%05u ", chain
->fw_number
);
203 printf("%10lu %10lu ",chain
->fw_pcnt
,chain
->fw_bcnt
);
207 if (chain
->timestamp
)
211 strcpy(timestr
, ctime((time_t *)&chain
->timestamp
));
212 *strchr(timestr
, '\n') = '\0';
213 printf("%s ", timestr
);
219 switch (chain
->fw_flg
& IPV6_FW_F_COMMAND
)
221 case IPV6_FW_F_ACCEPT
:
227 case IPV6_FW_F_COUNT
:
230 case IPV6_FW_F_DIVERT
:
231 printf("divert %u", chain
->fw_divert_port
);
234 printf("tee %u", chain
->fw_divert_port
);
236 case IPV6_FW_F_SKIPTO
:
237 printf("skipto %u", chain
->fw_skipto_rule
);
239 case IPV6_FW_F_REJECT
:
240 if (chain
->fw_reject_code
== IPV6_FW_REJECT_RST
)
244 print_reject_code(chain
->fw_reject_code
);
248 errx(EX_OSERR
, "impossible");
251 if (chain
->fw_flg
& IPV6_FW_F_PRN
)
254 pe
= getprotobynumber(chain
->fw_prot
);
256 printf(" %s", pe
->p_name
);
258 printf(" %u", chain
->fw_prot
);
260 printf(" from %s", chain
->fw_flg
& IPV6_FW_F_INVSRC
? "not " : "");
262 mb
=mask_bits((u_char
*)&chain
->fw_smsk
,sizeof(chain
->fw_smsk
));
263 if (mb
==128 && do_resolv
) {
264 he
=gethostbyaddr((char *)&(chain
->fw_src
),sizeof(chain
->fw_src
),AF_INET6
);
266 printf(inet_ntop(AF_INET6
,&chain
->fw_src
,ntop_buf
,sizeof(ntop_buf
)));
268 printf("%s",he
->h_name
);
274 printf(inet_ntop(AF_INET6
,&chain
->fw_src
,ntop_buf
,sizeof(ntop_buf
)));
278 printf(inet_ntop(AF_INET6
,&chain
->fw_src
,ntop_buf
,sizeof(ntop_buf
)));
281 if (chain
->fw_prot
== IPPROTO_TCP
|| chain
->fw_prot
== IPPROTO_UDP
) {
283 for (i
= 0; i
< nsp
; i
++) {
284 print_port(chain
->fw_prot
, chain
->fw_pts
[i
], comma
);
285 if (i
==0 && (chain
->fw_flg
& IPV6_FW_F_SRNG
))
292 printf(" to %s", chain
->fw_flg
& IPV6_FW_F_INVDST
? "not " : "");
294 mb
=mask_bits((u_char
*)&chain
->fw_dmsk
,sizeof(chain
->fw_dmsk
));
295 if (mb
==128 && do_resolv
) {
296 he
=gethostbyaddr((char *)&(chain
->fw_dst
),sizeof(chain
->fw_dst
),AF_INET
);
298 printf(inet_ntop(AF_INET6
,&chain
->fw_dst
,ntop_buf
,sizeof(ntop_buf
)));
300 printf("%s",he
->h_name
);
306 printf(inet_ntop(AF_INET6
,&chain
->fw_dst
,ntop_buf
,sizeof(ntop_buf
)));
310 printf(inet_ntop(AF_INET6
,&chain
->fw_dst
,ntop_buf
,sizeof(ntop_buf
)));
313 if (chain
->fw_prot
== IPPROTO_TCP
|| chain
->fw_prot
== IPPROTO_UDP
) {
315 for (i
= 0; i
< ndp
; i
++) {
316 print_port(chain
->fw_prot
, chain
->fw_pts
[nsp
+i
], comma
);
317 if (i
==0 && (chain
->fw_flg
& IPV6_FW_F_DRNG
))
325 if ((chain
->fw_flg
& IPV6_FW_F_IN
) && !(chain
->fw_flg
& IPV6_FW_F_OUT
))
327 if (!(chain
->fw_flg
& IPV6_FW_F_IN
) && (chain
->fw_flg
& IPV6_FW_F_OUT
))
330 /* Handle hack for "via" backwards compatibility */
331 if ((chain
->fw_flg
& IF6_FW_F_VIAHACK
) == IF6_FW_F_VIAHACK
) {
333 &chain
->fw_in_if
, chain
->fw_flg
& IPV6_FW_F_IIFNAME
);
335 /* Receive interface specified */
336 if (chain
->fw_flg
& IPV6_FW_F_IIFACE
)
337 print_iface("recv", &chain
->fw_in_if
,
338 chain
->fw_flg
& IPV6_FW_F_IIFNAME
);
339 /* Transmit interface specified */
340 if (chain
->fw_flg
& IPV6_FW_F_OIFACE
)
341 print_iface("xmit", &chain
->fw_out_if
,
342 chain
->fw_flg
& IPV6_FW_F_OIFNAME
);
345 if (chain
->fw_flg
& IPV6_FW_F_FRAG
)
348 if (chain
->fw_ip6opt
|| chain
->fw_ip6nopt
) {
349 int _opt_printed
= 0;
350 #define PRINTOPT(x) {if (_opt_printed) printf(",");\
351 printf(x); _opt_printed = 1;}
354 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_HOPOPT
) PRINTOPT("hopopt");
355 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_HOPOPT
) PRINTOPT("!hopopt");
356 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_ROUTE
) PRINTOPT("route");
357 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_ROUTE
) PRINTOPT("!route");
358 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_FRAG
) PRINTOPT("frag");
359 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_FRAG
) PRINTOPT("!frag");
360 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_ESP
) PRINTOPT("esp");
361 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_ESP
) PRINTOPT("!esp");
362 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_AH
) PRINTOPT("ah");
363 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_AH
) PRINTOPT("!ah");
364 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_NONXT
) PRINTOPT("nonxt");
365 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_NONXT
) PRINTOPT("!nonxt");
366 if (chain
->fw_ip6opt
& IPV6_FW_IP6OPT_OPTS
) PRINTOPT("opts");
367 if (chain
->fw_ip6nopt
& IPV6_FW_IP6OPT_OPTS
) PRINTOPT("!opts");
370 if (chain
->fw_ipflg
& IPV6_FW_IF_TCPEST
)
371 printf(" established");
372 else if (chain
->fw_tcpf
== IPV6_FW_TCPF_SYN
&&
373 chain
->fw_tcpnf
== IPV6_FW_TCPF_ACK
)
375 else if (chain
->fw_tcpf
|| chain
->fw_tcpnf
) {
376 int _flg_printed
= 0;
377 #define PRINTFLG(x) {if (_flg_printed) printf(",");\
378 printf(x); _flg_printed = 1;}
381 if (chain
->fw_tcpf
& IPV6_FW_TCPF_FIN
) PRINTFLG("fin");
382 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_FIN
) PRINTFLG("!fin");
383 if (chain
->fw_tcpf
& IPV6_FW_TCPF_SYN
) PRINTFLG("syn");
384 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_SYN
) PRINTFLG("!syn");
385 if (chain
->fw_tcpf
& IPV6_FW_TCPF_RST
) PRINTFLG("rst");
386 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_RST
) PRINTFLG("!rst");
387 if (chain
->fw_tcpf
& IPV6_FW_TCPF_PSH
) PRINTFLG("psh");
388 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_PSH
) PRINTFLG("!psh");
389 if (chain
->fw_tcpf
& IPV6_FW_TCPF_ACK
) PRINTFLG("ack");
390 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_ACK
) PRINTFLG("!ack");
391 if (chain
->fw_tcpf
& IPV6_FW_TCPF_URG
) PRINTFLG("urg");
392 if (chain
->fw_tcpnf
& IPV6_FW_TCPF_URG
) PRINTFLG("!urg");
394 if (chain
->fw_flg
& IPV6_FW_F_ICMPBIT
) {
400 for (type_index
= 0; type_index
< IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8; ++type_index
)
401 if (chain
->fw_icmp6types
[type_index
/ (sizeof(unsigned) * 8)] &
402 (1U << (type_index
% (sizeof(unsigned) * 8)))) {
403 printf("%c%d", first
== 1 ? ' ' : ',', type_index
);
417 struct ip6_fw
*r
, *rules
;
419 unsigned long rulenum
;
420 int nalloc
, maxbytes
;
423 /* extract rules from kernel, resizing array as necessary */
425 nalloc
= sizeof *rules
;
427 maxbytes
= 65536 * sizeof *rules
;
428 while (bytes
>= nalloc
) {
429 nalloc
= nalloc
* 2 + 200;
431 if ((rules
= realloc(rules
, bytes
)) == NULL
)
432 err(EX_OSERR
, "realloc");
433 memset(rules
, 0, sizeof *rules
);
434 rules
->version
= IPV6_FW_CURRENT_API_VERSION
;
435 i
= getsockopt(s
, IPPROTO_IPV6
, IPV6_FW_GET
, rules
, &bytes
);
436 if ((i
< 0 && errno
!= EINVAL
) || nalloc
> maxbytes
)
437 err(EX_OSERR
, "getsockopt(IPV6_FW_GET)");
440 /* display all rules */
441 for (r
= rules
, l
= bytes
; l
>= sizeof rules
[0];
442 r
++, l
-=sizeof rules
[0])
446 /* display specific rules requested on command line */
453 /* convert command line rule # */
454 rulenum
= strtoul(*av
++, &endptr
, 10);
457 warn("invalid rule number: %s", av
[-1]);
461 for (r
= rules
, l
= bytes
;
462 l
>= sizeof rules
[0] && r
->fw_number
<= rulenum
;
463 r
++, l
-=sizeof rules
[0])
464 if (rulenum
== r
->fw_number
) {
470 warnx("rule %lu does not exist", rulenum
);
479 show_usage(const char *fmt
, ...)
486 vsnprintf(buf
, sizeof(buf
), fmt
, args
);
488 warnx("error: %s", buf
);
490 fprintf(stderr
, "usage: ip6fw [options]\n"
492 " add [number] rule\n"
493 " delete number ...\n"
494 " list [number ...]\n"
495 " show [number ...]\n"
496 " zero [number ...]\n"
497 " rule: action proto src dst extras...\n"
499 " {allow|permit|accept|pass|deny|drop|reject|unreach code|\n"
500 " reset|count|skipto num} [log]\n"
501 " proto: {ipv6|tcp|udp|ipv6-icmp|<number>}\n"
502 " src: from [not] {any|ipv6[/prefixlen]} [{port|port-port},[port],...]\n"
503 " dst: to [not] {any|ipv6[/prefixlen]} [{port|port-port},[port],...]\n"
505 " fragment (may not be used with ports or tcpflags)\n"
508 " {xmit|recv|via} {iface|ipv6|any}\n"
509 " {established|setup}\n"
510 " tcpflags [!]{syn|fin|rst|ack|psh|urg},...\n"
511 " ipv6options [!]{hopopt|route|frag|esp|ah|nonxt|opts},...\n"
512 " icmptypes {type[,type]}...\n");
518 lookup_host (host
, addr
, family
)
522 struct hostent
*he
= gethostbyname2(host
, family
);
527 memcpy(addr
, he
->h_addr_list
[0], he
->h_length
);
533 fill_ip6(ipno
, mask
, acp
, avp
)
534 struct in6_addr
*ipno
, *mask
;
543 if (ac
&& !strncmp(*av
,"any",strlen(*av
))) {
544 *ipno
= *mask
= in6addr_any
; av
++; ac
--;
546 p
= strchr(*av
, '/');
552 if (lookup_host(*av
, (u_char
*)ipno
, AF_INET6
) != 0)
553 show_usage("hostname ``%s'' unknown", *av
);
558 } else if (atoi(p
) > 128) {
559 show_usage("bad width ``%s''", p
);
561 *mask
= *(plen2mask(atoi(p
)));
565 *mask
= *(plen2mask(128));
568 for (i
= 0; i
< sizeof(*ipno
); i
++)
569 ipno
->s6_addr
[i
] &= mask
->s6_addr
[i
];
578 fill_reject_code6(u_short
*codep
, char *str
)
584 val
= strtoul(str
, &s
, 0);
585 if (s
!= str
&& *s
== '\0' && val
< 0x100) {
589 for (ic
= icmp6codes
; ic
->str
; ic
++)
590 if (!strcasecmp(str
, ic
->str
)) {
594 show_usage("unknown ICMP6 unreachable code ``%s''", str
);
598 add_port(cnt
, ptr
, off
, port
)
599 u_short
*cnt
, *ptr
, off
, port
;
601 if (off
+ *cnt
>= IPV6_FW_MAX_PORTS
)
602 errx(1, "too many ports (max is %d)", IPV6_FW_MAX_PORTS
);
603 ptr
[off
+*cnt
] = port
;
608 lookup_port(const char *arg
, int test
, int nodash
)
614 snprintf(buf
, sizeof(buf
), "%s", arg
);
615 buf
[strcspn(arg
, nodash
? "-," : ",")] = 0;
616 val
= (int) strtoul(buf
, &earg
, 0);
617 if (!*buf
|| *earg
) {
619 if ((s
= getservbyname(buf
, NULL
))) {
620 val
= htons(s
->s_port
);
623 errx(1, "unknown port ``%s''", arg
);
628 if (val
< 0 || val
> 0xffff) {
630 errx(1, "port ``%s'' out of range", arg
);
639 fill_port(cnt
, ptr
, off
, arg
)
640 u_short
*cnt
, *ptr
, off
;
644 int initial_range
= 0;
646 s
= arg
+ strcspn(arg
, "-,"); /* first port name can't have a dash */
649 if (strchr(arg
, ','))
650 errx(1, "port range must be first in list");
651 add_port(cnt
, ptr
, off
, *arg
? lookup_port(arg
, 0, 0) : 0x0000);
656 add_port(cnt
, ptr
, off
, *arg
? lookup_port(arg
, 0, 0) : 0xffff);
660 while (arg
!= NULL
) {
664 add_port(cnt
, ptr
, off
, lookup_port(arg
, 0, 0));
667 return initial_range
;
671 fill_tcpflag(set
, reset
, vp
)
683 { "syn", IPV6_FW_TCPF_SYN
},
684 { "fin", IPV6_FW_TCPF_FIN
},
685 { "ack", IPV6_FW_TCPF_ACK
},
686 { "psh", IPV6_FW_TCPF_PSH
},
687 { "rst", IPV6_FW_TCPF_RST
},
688 { "urg", IPV6_FW_TCPF_URG
}
701 for (i
= 0; i
< sizeof(flags
) / sizeof(flags
[0]); ++i
)
702 if (!strncmp(p
, flags
[i
].name
, strlen(p
))) {
703 *d
|= flags
[i
].value
;
706 if (i
== sizeof(flags
) / sizeof(flags
[0]))
707 show_usage("invalid tcp flag ``%s''", p
);
713 fill_ip6opt(u_char
*set
, u_char
*reset
, char **vp
)
728 if (!strncmp(p
,"hopopt",strlen(p
))) *d
|= IPV6_FW_IP6OPT_HOPOPT
;
729 if (!strncmp(p
,"route",strlen(p
))) *d
|= IPV6_FW_IP6OPT_ROUTE
;
730 if (!strncmp(p
,"frag",strlen(p
))) *d
|= IPV6_FW_IP6OPT_FRAG
;
731 if (!strncmp(p
,"esp",strlen(p
))) *d
|= IPV6_FW_IP6OPT_ESP
;
732 if (!strncmp(p
,"ah",strlen(p
))) *d
|= IPV6_FW_IP6OPT_AH
;
733 if (!strncmp(p
,"nonxt",strlen(p
))) *d
|= IPV6_FW_IP6OPT_NONXT
;
734 if (!strncmp(p
,"opts",strlen(p
))) *d
|= IPV6_FW_IP6OPT_OPTS
;
740 fill_icmptypes(types
, vp
, fw_flg
)
749 unsigned long icmptype
;
754 icmptype
= strtoul(c
, &c
, 0);
756 if ( *c
!= ',' && *c
!= '\0' )
757 show_usage("invalid ICMP6 type");
759 if (icmptype
>= IPV6_FW_ICMPTYPES_DIM
* sizeof(unsigned) * 8)
760 show_usage("ICMP6 type out of range");
762 types
[icmptype
/ (sizeof(unsigned) * 8)] |=
763 1 << (icmptype
% (sizeof(unsigned) * 8));
764 *fw_flg
|= IPV6_FW_F_ICMPBIT
;
777 memset(&rule
, 0, sizeof rule
);
778 rule
.version
= IPV6_FW_CURRENT_API_VERSION
;
783 while (ac
&& isdigit(**av
)) {
784 rule
.fw_number
= atoi(*av
); av
++; ac
--;
785 i
= setsockopt(s
, IPPROTO_IPV6
, IPV6_FW_DEL
, &rule
, sizeof rule
);
788 warn("rule %u: setsockopt(%s)", rule
.fw_number
, "IPV6_FW_DEL");
796 verify_interface(union ip6_fw_if
*ifu
)
801 * If a unit was specified, check for that exact interface.
802 * If a wildcard was specified, check for unit 0.
804 snprintf(ifr
.ifr_name
, sizeof(ifr
.ifr_name
), "%s%d",
806 ifu
->fu_via_if
.unit
== -1 ? 0 : ifu
->fu_via_if
.unit
);
808 if (ioctl(s
, SIOCGIFFLAGS
, &ifr
) < 0)
809 warnx("warning: interface ``%s'' does not exist", ifr
.ifr_name
);
813 fill_iface(char *which
, union ip6_fw_if
*ifu
, int *byname
, int ac
, char *arg
)
816 show_usage("missing argument for ``%s''", which
);
818 /* Parse the interface or address */
819 if (!strcmp(arg
, "any")) {
820 ifu
->fu_via_ip6
= in6addr_any
;
822 } else if (!isdigit(*arg
)) {
826 strncpy(ifu
->fu_via_if
.name
, arg
, sizeof(ifu
->fu_via_if
.name
));
827 ifu
->fu_via_if
.name
[sizeof(ifu
->fu_via_if
.name
) - 1] = '\0';
828 for (q
= ifu
->fu_via_if
.name
;
829 *q
&& !isdigit(*q
) && *q
!= '*'; q
++)
831 ifu
->fu_via_if
.unit
= (*q
== '*') ? -1 : atoi(q
);
833 verify_interface(ifu
);
834 } else if (inet_pton(AF_INET6
, arg
, &ifu
->fu_via_ip6
) != 1) {
835 show_usage("bad ip6 address ``%s''", arg
);
849 int saw_xmrc
= 0, saw_via
= 0;
851 memset(&rule
, 0, sizeof rule
);
852 rule
.version
= IPV6_FW_CURRENT_API_VERSION
;
857 if (ac
&& isdigit(**av
)) {
858 rule
.fw_number
= atoi(*av
); av
++; ac
--;
863 show_usage("missing action");
864 if (!strncmp(*av
,"accept",strlen(*av
))
865 || !strncmp(*av
,"pass",strlen(*av
))
866 || !strncmp(*av
,"allow",strlen(*av
))
867 || !strncmp(*av
,"permit",strlen(*av
))) {
868 rule
.fw_flg
|= IPV6_FW_F_ACCEPT
; av
++; ac
--;
869 } else if (!strncmp(*av
,"count",strlen(*av
))) {
870 rule
.fw_flg
|= IPV6_FW_F_COUNT
; av
++; ac
--;
873 else if (!strncmp(*av
,"divert",strlen(*av
))) {
874 rule
.fw_flg
|= IPV6_FW_F_DIVERT
; av
++; ac
--;
876 show_usage("missing divert port");
877 rule
.fw_divert_port
= strtoul(*av
, NULL
, 0); av
++; ac
--;
878 if (rule
.fw_divert_port
== 0) {
881 s
= getservbyname(av
[-1], "divert");
883 rule
.fw_divert_port
= ntohs(s
->s_port
);
885 show_usage("illegal divert port");
887 } else if (!strncmp(*av
,"tee",strlen(*av
))) {
888 rule
.fw_flg
|= IPV6_FW_F_TEE
; av
++; ac
--;
890 show_usage("missing divert port");
891 rule
.fw_divert_port
= strtoul(*av
, NULL
, 0); av
++; ac
--;
892 if (rule
.fw_divert_port
== 0) {
895 s
= getservbyname(av
[-1], "divert");
897 rule
.fw_divert_port
= ntohs(s
->s_port
);
899 show_usage("illegal divert port");
903 else if (!strncmp(*av
,"skipto",strlen(*av
))) {
904 rule
.fw_flg
|= IPV6_FW_F_SKIPTO
; av
++; ac
--;
906 show_usage("missing skipto rule number");
907 rule
.fw_skipto_rule
= strtoul(*av
, NULL
, 0); av
++; ac
--;
908 } else if ((!strncmp(*av
,"deny",strlen(*av
))
909 || !strncmp(*av
,"drop",strlen(*av
)))) {
910 rule
.fw_flg
|= IPV6_FW_F_DENY
; av
++; ac
--;
911 } else if (!strncmp(*av
,"reject",strlen(*av
))) {
912 rule
.fw_flg
|= IPV6_FW_F_REJECT
; av
++; ac
--;
913 rule
.fw_reject_code
= ICMP6_DST_UNREACH_NOROUTE
;
914 } else if (!strncmp(*av
,"reset",strlen(*av
))) {
915 rule
.fw_flg
|= IPV6_FW_F_REJECT
; av
++; ac
--;
916 rule
.fw_reject_code
= IPV6_FW_REJECT_RST
; /* check TCP later */
917 } else if (!strncmp(*av
,"unreach",strlen(*av
))) {
918 rule
.fw_flg
|= IPV6_FW_F_REJECT
; av
++; ac
--;
919 fill_reject_code6(&rule
.fw_reject_code
, *av
); av
++; ac
--;
921 show_usage("invalid action ``%s''", *av
);
925 if (ac
&& !strncmp(*av
,"log",strlen(*av
))) {
926 rule
.fw_flg
|= IPV6_FW_F_PRN
; av
++; ac
--;
931 show_usage("missing protocol");
932 if ((proto
= atoi(*av
)) > 0) {
933 rule
.fw_prot
= proto
; av
++; ac
--;
934 } else if (!strncmp(*av
,"all",strlen(*av
))) {
935 rule
.fw_prot
= IPPROTO_IPV6
; av
++; ac
--;
936 } else if ((pe
= getprotobyname(*av
)) != NULL
) {
937 rule
.fw_prot
= pe
->p_proto
; av
++; ac
--;
939 show_usage("invalid protocol ``%s''", *av
);
942 if (rule
.fw_prot
!= IPPROTO_TCP
943 && (rule
.fw_flg
& IPV6_FW_F_COMMAND
) == IPV6_FW_F_REJECT
944 && rule
.fw_reject_code
== IPV6_FW_REJECT_RST
)
945 show_usage("``reset'' is only valid for tcp packets");
948 if (ac
&& !strncmp(*av
,"from",strlen(*av
))) { av
++; ac
--; }
950 show_usage("missing ``from''");
952 if (ac
&& !strncmp(*av
,"not",strlen(*av
))) {
953 rule
.fw_flg
|= IPV6_FW_F_INVSRC
;
957 show_usage("missing arguments");
959 fill_ip6(&rule
.fw_src
, &rule
.fw_smsk
, &ac
, &av
);
961 if (ac
&& (isdigit(**av
) || lookup_port(*av
, 1, 1) >= 0)) {
964 if (fill_port(&nports
, rule
.fw_pts
, 0, *av
))
965 rule
.fw_flg
|= IPV6_FW_F_SRNG
;
966 IPV6_FW_SETNSRCP(&rule
, nports
);
971 if (ac
&& !strncmp(*av
,"to",strlen(*av
))) { av
++; ac
--; }
973 show_usage("missing ``to''");
975 if (ac
&& !strncmp(*av
,"not",strlen(*av
))) {
976 rule
.fw_flg
|= IPV6_FW_F_INVDST
;
980 show_usage("missing arguments");
982 fill_ip6(&rule
.fw_dst
, &rule
.fw_dmsk
, &ac
, &av
);
984 if (ac
&& (isdigit(**av
) || lookup_port(*av
, 1, 1) >= 0)) {
987 if (fill_port(&nports
,
988 rule
.fw_pts
, IPV6_FW_GETNSRCP(&rule
), *av
))
989 rule
.fw_flg
|= IPV6_FW_F_DRNG
;
990 IPV6_FW_SETNDSTP(&rule
, nports
);
994 if ((rule
.fw_prot
!= IPPROTO_TCP
) && (rule
.fw_prot
!= IPPROTO_UDP
)
995 && (IPV6_FW_GETNSRCP(&rule
) || IPV6_FW_GETNDSTP(&rule
))) {
996 show_usage("only TCP and UDP protocols are valid"
997 " with port specifications");
1001 if (!strncmp(*av
,"in",strlen(*av
))) {
1002 rule
.fw_flg
|= IPV6_FW_F_IN
;
1003 av
++; ac
--; continue;
1005 if (!strncmp(*av
,"out",strlen(*av
))) {
1006 rule
.fw_flg
|= IPV6_FW_F_OUT
;
1007 av
++; ac
--; continue;
1009 if (ac
&& !strncmp(*av
,"xmit",strlen(*av
))) {
1010 union ip6_fw_if ifu
;
1015 show_usage("``via'' is incompatible"
1016 " with ``xmit'' and ``recv''");
1020 fill_iface("xmit", &ifu
, &byname
, ac
, *av
);
1021 rule
.fw_out_if
= ifu
;
1022 rule
.fw_flg
|= IPV6_FW_F_OIFACE
;
1024 rule
.fw_flg
|= IPV6_FW_F_OIFNAME
;
1025 av
++; ac
--; continue;
1027 if (ac
&& !strncmp(*av
,"recv",strlen(*av
))) {
1028 union ip6_fw_if ifu
;
1035 fill_iface("recv", &ifu
, &byname
, ac
, *av
);
1036 rule
.fw_in_if
= ifu
;
1037 rule
.fw_flg
|= IPV6_FW_F_IIFACE
;
1039 rule
.fw_flg
|= IPV6_FW_F_IIFNAME
;
1040 av
++; ac
--; continue;
1042 if (ac
&& !strncmp(*av
,"via",strlen(*av
))) {
1043 union ip6_fw_if ifu
;
1050 fill_iface("via", &ifu
, &byname
, ac
, *av
);
1051 rule
.fw_out_if
= rule
.fw_in_if
= ifu
;
1054 (IPV6_FW_F_IIFNAME
| IPV6_FW_F_OIFNAME
);
1055 av
++; ac
--; continue;
1057 if (!strncmp(*av
,"fragment",strlen(*av
))) {
1058 rule
.fw_flg
|= IPV6_FW_F_FRAG
;
1059 av
++; ac
--; continue;
1061 if (!strncmp(*av
,"ipv6options",strlen(*av
))) {
1064 show_usage("missing argument"
1065 " for ``ipv6options''");
1066 fill_ip6opt(&rule
.fw_ip6opt
, &rule
.fw_ip6nopt
, av
);
1067 av
++; ac
--; continue;
1069 if (rule
.fw_prot
== IPPROTO_TCP
) {
1070 if (!strncmp(*av
,"established",strlen(*av
))) {
1071 rule
.fw_ipflg
|= IPV6_FW_IF_TCPEST
;
1072 av
++; ac
--; continue;
1074 if (!strncmp(*av
,"setup",strlen(*av
))) {
1075 rule
.fw_tcpf
|= IPV6_FW_TCPF_SYN
;
1076 rule
.fw_tcpnf
|= IPV6_FW_TCPF_ACK
;
1077 av
++; ac
--; continue;
1079 if (!strncmp(*av
,"tcpflags",strlen(*av
))) {
1082 show_usage("missing argument"
1083 " for ``tcpflags''");
1084 fill_tcpflag(&rule
.fw_tcpf
, &rule
.fw_tcpnf
, av
);
1085 av
++; ac
--; continue;
1088 if (rule
.fw_prot
== IPPROTO_ICMPV6
) {
1089 if (!strncmp(*av
,"icmptypes",strlen(*av
))) {
1092 show_usage("missing argument"
1093 " for ``icmptypes''");
1094 fill_icmptypes(rule
.fw_icmp6types
,
1096 av
++; ac
--; continue;
1099 show_usage("unknown argument ``%s''", *av
);
1102 /* No direction specified -> do both directions */
1103 if (!(rule
.fw_flg
& (IPV6_FW_F_OUT
|IPV6_FW_F_IN
)))
1104 rule
.fw_flg
|= (IPV6_FW_F_OUT
|IPV6_FW_F_IN
);
1106 /* Sanity check interface check, but handle "via" case separately */
1108 if (rule
.fw_flg
& IPV6_FW_F_IN
)
1109 rule
.fw_flg
|= IPV6_FW_F_IIFACE
;
1110 if (rule
.fw_flg
& IPV6_FW_F_OUT
)
1111 rule
.fw_flg
|= IPV6_FW_F_OIFACE
;
1112 } else if ((rule
.fw_flg
& IPV6_FW_F_OIFACE
) && (rule
.fw_flg
& IPV6_FW_F_IN
))
1113 show_usage("can't check xmit interface of incoming packets");
1115 /* frag may not be used in conjunction with ports or TCP flags */
1116 if (rule
.fw_flg
& IPV6_FW_F_FRAG
) {
1117 if (rule
.fw_tcpf
|| rule
.fw_tcpnf
)
1118 show_usage("can't mix 'frag' and tcpflags");
1121 show_usage("can't mix 'frag' and port specifications");
1126 i
= setsockopt(s
, IPPROTO_IPV6
, IPV6_FW_ADD
, &rule
, sizeof rule
);
1128 err(EX_UNAVAILABLE
, "setsockopt(%s)", "IPV6_FW_ADD");
1139 memset(&rule
, 0, sizeof rule
);
1140 rule
.version
= IPV6_FW_CURRENT_API_VERSION
;
1143 /* clear all entries */
1144 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_FW_ZERO
, &rule
, sizeof rule
) < 0)
1145 err(EX_UNAVAILABLE
, "setsockopt(%s)", "IPV6_FW_ZERO");
1147 printf("Accounting cleared.\n");
1153 if (isdigit(**av
)) {
1154 rule
.fw_number
= atoi(*av
); av
++; ac
--;
1155 if (setsockopt(s
, IPPROTO_IPV6
,
1156 IPV6_FW_ZERO
, &rule
, sizeof rule
)) {
1157 warn("rule %u: setsockopt(%s)", rule
.fw_number
,
1162 printf("Entry %d cleared\n",
1165 show_usage("invalid rule number ``%s''", *av
);
1180 /* init optind to 1 */
1187 /* Set the force flag for non-interactive processes */
1188 do_force
= !isatty(STDIN_FILENO
);
1190 while ((ch
= getopt(ac
, av
,"afqtN")) != -1)
1212 if (*(av
+=optind
)==NULL
) {
1213 show_usage("Bad arguments");
1216 if (!strncmp(*av
, "add", strlen(*av
))) {
1218 } else if (!strncmp(*av
, "delete", strlen(*av
))) {
1220 } else if (!strncmp(*av
, "flush", strlen(*av
))) {
1223 if ( do_force
|| do_quiet
)
1229 printf("Are you sure? [yn] ");
1232 c
= toupper(getc(stdin
));
1233 while (c
!= '\n' && getc(stdin
) != '\n')
1236 } while (c
!= 'Y' && c
!= 'N');
1244 memset(&rule
, 0, sizeof rule
);
1245 rule
.version
= IPV6_FW_CURRENT_API_VERSION
;
1246 if (setsockopt(s
, IPPROTO_IPV6
, IPV6_FW_FLUSH
, &rule
, sizeof rule
) < 0)
1247 err(EX_UNAVAILABLE
, "setsockopt(%s)", "IPV6_FW_FLUSH");
1249 printf("Flushed all rules.\n");
1251 } else if (!strncmp(*av
, "zero", strlen(*av
))) {
1253 } else if (!strncmp(*av
, "print", strlen(*av
))) {
1255 } else if (!strncmp(*av
, "list", strlen(*av
))) {
1257 } else if (!strncmp(*av
, "show", strlen(*av
))) {
1261 show_usage("Bad arguments");
1272 #define WHITESP " \t\f\v\n\r"
1274 char *a
, *p
, *args
[MAX_ARGS
], *cmd
= NULL
;
1276 int i
, c
, lineno
, qflag
, pflag
, status
;
1280 s
= socket( AF_INET6
, SOCK_RAW
, IPPROTO_RAW
);
1282 err(EX_UNAVAILABLE
, "socket");
1287 * Only interpret the last command line argument as a file to
1288 * be preprocessed if it is specified as an absolute pathname.
1291 if (ac
> 1 && av
[ac
- 1][0] == '/' && access(av
[ac
- 1], R_OK
) == 0) {
1292 qflag
= pflag
= i
= 0;
1295 while ((c
= getopt(ac
, av
, "D:U:p:q")) != -1)
1299 errx(EX_USAGE
, "-D requires -p");
1300 if (i
> MAX_ARGS
- 2)
1302 "too many -D or -U options");
1309 errx(EX_USAGE
, "-U requires -p");
1310 if (i
> MAX_ARGS
- 2)
1312 "too many -D or -U options");
1335 show_usage("extraneous filename arguments");
1337 if ((f
= fopen(av
[0], "r")) == NULL
)
1338 err(EX_UNAVAILABLE
, "fopen: %s", av
[0]);
1341 /* pipe through preprocessor (cpp or m4) */
1346 if (pipe(pipedes
) == -1)
1347 err(EX_OSERR
, "cannot create pipe");
1349 switch((preproc
= fork())) {
1351 err(EX_OSERR
, "cannot fork");
1355 if (dup2(fileno(f
), 0) == -1 ||
1356 dup2(pipedes
[1], 1) == -1)
1357 err(EX_OSERR
, "dup2()");
1362 err(EX_OSERR
, "execvp(%s) failed", cmd
);
1368 if ((f
= fdopen(pipedes
[0], "r")) == NULL
) {
1369 int savederrno
= errno
;
1371 (void)kill(preproc
, SIGTERM
);
1373 err(EX_OSERR
, "fdopen()");
1378 while (fgets(buf
, BUFSIZ
, f
)) {
1380 sprintf(linename
, "Line %d", lineno
);
1385 if ((p
= strchr(buf
, '#')) != NULL
)
1388 if (qflag
) args
[i
++]="-q";
1389 for (a
= strtok(buf
, WHITESP
);
1390 a
&& i
< MAX_ARGS
; a
= strtok(NULL
, WHITESP
), i
++)
1392 if (i
== (qflag
? 2: 1))
1395 errx(EX_USAGE
, "%s: too many arguments", linename
);
1398 ip6fw_main(i
, args
);
1402 if (waitpid(preproc
, &status
, 0) != -1) {
1403 if (WIFEXITED(status
)) {
1404 if (WEXITSTATUS(status
) != EX_OK
)
1405 errx(EX_UNAVAILABLE
,
1406 "preprocessor exited with status %d",
1407 WEXITSTATUS(status
));
1408 } else if (WIFSIGNALED(status
)) {
1409 errx(EX_UNAVAILABLE
,
1410 "preprocessor exited with signal %d",