2 /* $KAME: ip6_fw.c,v 1.21 2001/01/24 01:25:32 itojun Exp $ */
5 * Copyright (C) 1998, 1999, 2000 and 2001 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * Copyright (c) 1993 Daniel Boulet
35 * Copyright (c) 1994 Ugen J.S.Antsilevich
36 * Copyright (c) 1996 Alex Nash
38 * Redistribution and use in source forms, with and without modification,
39 * are permitted provided that this entire comment appears intact.
41 * Redistribution in binary form may occur without any restrictions.
42 * Obviously, it would be nice if you gave credit where credit is due
43 * but requiring it would be too onerous.
45 * This software is provided ``AS IS'' without any warranties of any kind.
49 * Implement IPv6 packet firewall
52 #if !defined(KLD_MODULE)
53 #include "opt_ip6fw.h"
55 #include "opt_inet6.h"
59 #error "NOT SUPPORTED IPV6 DIVERT"
61 #ifdef IP6FW_DIVERT_RESTART
62 #error "NOT SUPPORTED IPV6 DIVERT"
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/malloc.h>
69 #include <sys/queue.h>
70 #include <sys/kernel.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/syslog.h>
76 #include <net/route.h>
77 #include <netinet/in_systm.h>
78 #include <netinet/in.h>
79 #include <netinet/ip.h>
81 #include <netinet/ip6.h>
82 #include <netinet6/ip6_var.h>
83 #include <netinet6/in6_var.h>
84 #include <netinet/icmp6.h>
86 #include <netinet/in_pcb.h>
88 #include <netinet6/ip6_fw.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/tcp.h>
91 #include <netinet/tcp_seq.h>
92 #include <netinet/tcp_timer.h>
93 #include <netinet/tcp_var.h>
94 #include <netinet/udp.h>
96 #include <sys/sysctl.h>
98 #include <net/net_osdep.h>
100 MALLOC_DEFINE(M_IP6FW, "Ip6Fw/Ip6Acct", "Ip6Fw/Ip6Acct chain's");
102 static int fw6_debug = 1;
103 #ifdef IPV6FIREWALL_VERBOSE
104 static int fw6_verbose = 1;
106 static int fw6_verbose = 0;
108 #ifdef IPV6FIREWALL_VERBOSE_LIMIT
109 static int fw6_verbose_limit = IPV6FIREWALL_VERBOSE_LIMIT;
111 static int fw6_verbose_limit = 0;
114 static LIST_HEAD (ip6_fw_head, ip6_fw_chain) ip6_fw_chain;
117 SYSCTL_DECL(_net_inet6_ip6);
118 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW | CTLFLAG_SECURE,
120 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, enable, CTLFLAG_RW | CTLFLAG_SECURE,
121 &ip6_fw_enable, 0, "Enable ip6fw");
122 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, debug, CTLFLAG_RW, &fw6_debug, 0, "");
123 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, verbose, CTLFLAG_RW | CTLFLAG_SECURE,
124 &fw6_verbose, 0, "");
125 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, verbose_limit, CTLFLAG_RW, &fw6_verbose_limit, 0, "");
128 #define dprintf(a) do { \
132 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
134 static int add_entry6 __P((struct ip6_fw_head *chainptr, struct ip6_fw *frwl));
135 static int del_entry6 __P((struct ip6_fw_head *chainptr, u_short number));
136 static int zero_entry6 __P((struct mbuf *m));
137 static struct ip6_fw *check_ip6fw_struct __P((struct ip6_fw *m));
138 static struct ip6_fw *check_ip6fw_mbuf __P((struct mbuf *fw));
139 static int ip6opts_match __P((struct ip6_hdr **ip6, struct ip6_fw *f,
141 int *off, int *nxt, u_short *offset));
142 static int port_match6 __P((u_short *portptr, int nports, u_short port,
144 static int tcp6flg_match __P((struct tcphdr *tcp6, struct ip6_fw *f));
145 static int icmp6type_match __P((struct icmp6_hdr * icmp, struct ip6_fw * f));
146 static void ip6fw_report __P((struct ip6_fw *f, struct ip6_hdr *ip6,
147 struct ifnet *rif, struct ifnet *oif, int off, int nxt));
149 static int ip6_fw_chk __P((struct ip6_hdr **pip6,
150 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m));
151 static int ip6_fw_ctl __P((int stage, struct mbuf **mm));
153 static char err_prefix[] = "ip6_fw_ctl:";
156 * Returns 1 if the port is matched by the vector, 0 otherwise
160 port_match6(u_short *portptr, int nports, u_short port, int range_flag)
165 if (portptr[0] <= port && port <= portptr[1]) {
171 while (nports-- > 0) {
172 if (*portptr++ == port) {
180 tcp6flg_match(struct tcphdr *tcp6, struct ip6_fw *f)
182 u_char flg_set, flg_clr;
185 * If an established connection is required, reject packets that
186 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
187 * other flag requirements.
189 if ((f->fw_ipflg & IPV6_FW_IF_TCPEST) &&
190 ((tcp6->th_flags & (IPV6_FW_TCPF_RST | IPV6_FW_TCPF_ACK |
191 IPV6_FW_TCPF_SYN)) == IPV6_FW_TCPF_SYN))
194 flg_set = tcp6->th_flags & f->fw_tcpf;
195 flg_clr = tcp6->th_flags & f->fw_tcpnf;
197 if (flg_set != f->fw_tcpf)
206 icmp6type_match(struct icmp6_hdr *icmp6, struct ip6_fw *f)
210 if (!(f->fw_flg & IPV6_FW_F_ICMPBIT))
213 type = icmp6->icmp6_type;
215 /* check for matching type in the bitmap */
216 if (type < IPV6_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8 &&
217 (f->fw_icmp6types[type / (sizeof(unsigned) * 8)] &
218 (1U << (type % (8 * sizeof(unsigned))))))
221 return(0); /* no match */
225 is_icmp6_query(struct ip6_hdr *ip6, int off)
227 const struct icmp6_hdr *icmp6;
230 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
231 icmp6_type = icmp6->icmp6_type;
233 if (icmp6_type == ICMP6_ECHO_REQUEST ||
234 icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
235 icmp6_type == ICMP6_WRUREQUEST ||
236 icmp6_type == ICMP6_FQDN_QUERY ||
237 icmp6_type == ICMP6_NI_QUERY)
244 ip6opts_match(struct ip6_hdr **pip6, struct ip6_fw *f, struct mbuf **m,
245 int *off, int *nxt, u_short *offset)
248 struct ip6_hdr *ip6 = *pip6;
249 struct ip6_ext *ip6e;
250 u_char opts, nopts, nopts_sve;
253 nopts = nopts_sve = f->fw_ip6nopt;
256 *off = sizeof(struct ip6_hdr);
257 len = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr);
259 ip6e = (struct ip6_ext *)((caddr_t) ip6 + *off);
260 if ((*m)->m_len < *off + sizeof(*ip6e))
261 goto opts_check; /* XXX */
264 case IPPROTO_FRAGMENT:
265 if ((*m)->m_len >= *off + sizeof(struct ip6_frag)) {
266 struct ip6_frag *ip6f;
268 ip6f = (struct ip6_frag *) ((caddr_t)ip6 + *off);
269 *offset = ip6f->ip6f_offlg & IP6F_OFF_MASK;
271 opts &= ~IPV6_FW_IP6OPT_FRAG;
272 nopts &= ~IPV6_FW_IP6OPT_FRAG;
273 *off += sizeof(struct ip6_frag);
276 opts &= ~IPV6_FW_IP6OPT_AH;
277 nopts &= ~IPV6_FW_IP6OPT_AH;
278 *off += (ip6e->ip6e_len + 2) << 2;
282 case IPPROTO_HOPOPTS:
283 opts &= ~IPV6_FW_IP6OPT_HOPOPT;
284 nopts &= ~IPV6_FW_IP6OPT_HOPOPT;
286 case IPPROTO_ROUTING:
287 opts &= ~IPV6_FW_IP6OPT_ROUTE;
288 nopts &= ~IPV6_FW_IP6OPT_ROUTE;
291 opts &= ~IPV6_FW_IP6OPT_ESP;
292 nopts &= ~IPV6_FW_IP6OPT_ESP;
295 opts &= ~IPV6_FW_IP6OPT_NONXT;
296 nopts &= ~IPV6_FW_IP6OPT_NONXT;
298 case IPPROTO_DSTOPTS:
299 opts &= ~IPV6_FW_IP6OPT_OPTS;
300 nopts &= ~IPV6_FW_IP6OPT_OPTS;
305 *off += (ip6e->ip6e_len + 1) << 3;
308 *nxt = ip6e->ip6e_nxt;
312 if (f->fw_ip6opt == f->fw_ip6nopt) /* XXX */
315 if (opts == 0 && nopts == nopts_sve)
323 iface_match(struct ifnet *ifp, union ip6_fw_if *ifu, int byname)
325 /* Check by name or by IP address */
327 /* Check unit number (-1 is wildcard) */
328 if (ifu->fu_via_if.unit != -1
329 && ifp->if_unit != ifu->fu_via_if.unit)
332 if (strncmp(ifp->if_name, ifu->fu_via_if.name, IP6FW_IFNLEN))
335 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu->fu_via_ip6)) { /* Zero == wildcard */
338 for (ia = ifp->if_addrlist.tqh_first; ia; ia = ia->ifa_list.tqe_next)
341 if (ia->ifa_addr == NULL)
343 if (ia->ifa_addr->sa_family != AF_INET6)
345 if (!IN6_ARE_ADDR_EQUAL(&ifu->fu_via_ip6,
346 &(((struct sockaddr_in6 *)
347 (ia->ifa_addr))->sin6_addr)))
357 ip6fw_report(struct ip6_fw *f, struct ip6_hdr *ip6,
358 struct ifnet *rif, struct ifnet *oif, int off, int nxt)
361 struct tcphdr *const tcp6 = (struct tcphdr *) ((caddr_t) ip6+ off);
362 struct udphdr *const udp = (struct udphdr *) ((caddr_t) ip6+ off);
363 struct icmp6_hdr *const icmp6 = (struct icmp6_hdr *) ((caddr_t) ip6+ off);
366 char action2[32], proto[102], name[18];
369 count = f ? f->fw_pcnt : ++counter;
370 if (fw6_verbose_limit != 0 && count > fw6_verbose_limit)
373 /* Print command name */
374 snprintf(SNPARGS(name, 0), "ip6fw: %d", f ? f->fw_number : -1);
380 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
384 case IPV6_FW_F_REJECT:
385 if (f->fw_reject_code == IPV6_FW_REJECT_RST)
390 case IPV6_FW_F_ACCEPT:
393 case IPV6_FW_F_COUNT:
396 case IPV6_FW_F_DIVERT:
397 snprintf(SNPARGS(action2, 0), "Divert %d",
401 snprintf(SNPARGS(action2, 0), "Tee %d",
404 case IPV6_FW_F_SKIPTO:
405 snprintf(SNPARGS(action2, 0), "SkipTo %d",
416 len = snprintf(SNPARGS(proto, 0), "TCP [%s]",
417 ip6_sprintf(&ip6->ip6_src));
419 len += snprintf(SNPARGS(proto, len), ":%d ",
420 ntohs(tcp6->th_sport));
422 len += snprintf(SNPARGS(proto, len), " ");
423 len += snprintf(SNPARGS(proto, len), "[%s]",
424 ip6_sprintf(&ip6->ip6_dst));
426 snprintf(SNPARGS(proto, len), ":%d",
427 ntohs(tcp6->th_dport));
430 len = snprintf(SNPARGS(proto, 0), "UDP [%s]",
431 ip6_sprintf(&ip6->ip6_src));
433 len += snprintf(SNPARGS(proto, len), ":%d ",
434 ntohs(udp->uh_sport));
436 len += snprintf(SNPARGS(proto, len), " ");
437 len += snprintf(SNPARGS(proto, len), "[%s]",
438 ip6_sprintf(&ip6->ip6_dst));
440 snprintf(SNPARGS(proto, len), ":%d",
441 ntohs(udp->uh_dport));
445 len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP:%u.%u ",
446 icmp6->icmp6_type, icmp6->icmp6_code);
448 len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP ");
449 len += snprintf(SNPARGS(proto, len), "[%s]",
450 ip6_sprintf(&ip6->ip6_src));
451 snprintf(SNPARGS(proto, len), " [%s]",
452 ip6_sprintf(&ip6->ip6_dst));
455 len = snprintf(SNPARGS(proto, 0), "P:%d [%s]", nxt,
456 ip6_sprintf(&ip6->ip6_src));
457 snprintf(SNPARGS(proto, len), " [%s]",
458 ip6_sprintf(&ip6->ip6_dst));
463 log(LOG_SECURITY | LOG_INFO, "%s %s %s out via %s\n",
464 name, action, proto, if_name(oif));
466 log(LOG_SECURITY | LOG_INFO, "%s %s %s in via %s\n",
467 name, action, proto, if_name(rif));
469 log(LOG_SECURITY | LOG_INFO, "%s %s %s",
470 name, action, proto);
471 if (fw6_verbose_limit != 0 && count == fw6_verbose_limit)
472 log(LOG_SECURITY | LOG_INFO, "ip6fw: limit reached on entry %d\n",
473 f ? f->fw_number : -1);
479 * ip Pointer to packet header (struct ip6_hdr *)
480 * hlen Packet header length
481 * oif Outgoing interface, or NULL if packet is incoming
482 * #ifndef IP6FW_DIVERT_RESTART
483 * *cookie Ignore all divert/tee rules to this port (if non-zero)
485 * *cookie Skip up to the first rule past this rule number;
487 * *m The packet; we set to NULL when/if we nuke it.
491 * 0 The packet is to be accepted and routed normally OR
492 * the packet was denied/rejected and has been dropped;
493 * in the latter case, *m is equal to NULL upon return.
494 * port Divert the packet to port.
498 ip6_fw_chk(struct ip6_hdr **pip6,
499 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m)
501 struct ip6_fw_chain *chain;
502 struct ip6_fw *rule = NULL;
503 struct ip6_hdr *ip6 = *pip6;
504 struct ifnet *const rif = (*m)->m_pkthdr.rcvif;
506 int off = sizeof(struct ip6_hdr), nxt = ip6->ip6_nxt;
507 u_short src_port, dst_port;
508 #ifdef IP6FW_DIVERT_RESTART
509 u_int16_t skipto = *cookie;
511 u_int16_t ignport = ntohs(*cookie);
516 * Go down the chain, looking for enlightment
517 * #ifdef IP6FW_DIVERT_RESTART
518 * If we've been asked to start at a given rule immediatly, do so.
521 chain = LIST_FIRST(&ip6_fw_chain);
522 #ifdef IP6FW_DIVERT_RESTART
526 while (chain && (chain->rule->fw_number <= skipto)) {
527 chain = LIST_NEXT(chain, chain);
529 if (! chain) goto dropit;
531 #endif /* IP6FW_DIVERT_RESTART */
532 for (; chain; chain = LIST_NEXT(chain, chain)) {
533 struct ip6_fw *const f = chain->rule;
536 /* Check direction outbound */
537 if (!(f->fw_flg & IPV6_FW_F_OUT))
540 /* Check direction inbound */
541 if (!(f->fw_flg & IPV6_FW_F_IN))
545 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
546 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
547 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
548 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
549 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
551 /* If src-addr doesn't match, not this rule. */
552 if (((f->fw_flg & IPV6_FW_F_INVSRC) != 0) ^
553 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_src,&f->fw_smsk,&f->fw_src)))
556 /* If dest-addr doesn't match, not this rule. */
557 if (((f->fw_flg & IPV6_FW_F_INVDST) != 0) ^
558 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_dst,&f->fw_dmsk,&f->fw_dst)))
561 #undef IN6_ARE_ADDR_MASKEQUAL
562 /* Interface check */
563 if ((f->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
564 struct ifnet *const iface = oif ? oif : rif;
566 /* Backwards compatibility hack for "via" */
567 if (!iface || !iface_match(iface,
568 &f->fw_in_if, f->fw_flg & IPV6_FW_F_OIFNAME))
571 /* Check receive interface */
572 if ((f->fw_flg & IPV6_FW_F_IIFACE)
573 && (!rif || !iface_match(rif,
574 &f->fw_in_if, f->fw_flg & IPV6_FW_F_IIFNAME)))
576 /* Check outgoing interface */
577 if ((f->fw_flg & IPV6_FW_F_OIFACE)
578 && (!oif || !iface_match(oif,
579 &f->fw_out_if, f->fw_flg & IPV6_FW_F_OIFNAME)))
583 /* Check IP options */
584 if (!ip6opts_match(&ip6, f, m, &off, &nxt, &offset))
588 if ((f->fw_flg & IPV6_FW_F_FRAG) && !offset)
591 /* Check protocol; if wildcard, match */
592 if (f->fw_prot == IPPROTO_IPV6)
595 /* If different, don't match */
596 if (nxt != f->fw_prot)
599 #define PULLUP_TO(len) do { \
600 if ((*m)->m_len < (len) \
601 && (*m = m_pullup(*m, (len))) == 0) { \
604 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
607 /* Protocol specific checks */
613 if (offset == 1) { /* cf. RFC 1858 */
614 PULLUP_TO(off + 4); /* XXX ? */
619 * TCP flags and ports aren't available in this
620 * packet -- if this rule specified either one,
621 * we consider the rule a non-match.
623 if (f->fw_nports != 0 ||
624 f->fw_tcpf != f->fw_tcpnf)
630 tcp6 = (struct tcphdr *) ((caddr_t)ip6 + off);
631 if (((f->fw_tcpf != f->fw_tcpnf) ||
632 (f->fw_ipflg & IPV6_FW_IF_TCPEST)) &&
633 !tcp6flg_match(tcp6, f))
635 src_port = ntohs(tcp6->th_sport);
636 dst_port = ntohs(tcp6->th_dport);
646 * Port specification is unavailable -- if this
647 * rule specifies a port, we consider the rule
650 if (f->fw_nports != 0)
656 udp = (struct udphdr *) ((caddr_t)ip6 + off);
657 src_port = ntohs(udp->uh_sport);
658 dst_port = ntohs(udp->uh_dport);
660 if (!port_match6(&f->fw_pts[0],
661 IPV6_FW_GETNSRCP(f), src_port,
662 f->fw_flg & IPV6_FW_F_SRNG))
664 if (!port_match6(&f->fw_pts[IPV6_FW_GETNSRCP(f)],
665 IPV6_FW_GETNDSTP(f), dst_port,
666 f->fw_flg & IPV6_FW_F_DRNG))
673 struct icmp6_hdr *icmp;
675 if (offset != 0) /* Type isn't valid */
678 icmp = (struct icmp6_hdr *) ((caddr_t)ip6 + off);
679 if (!icmp6type_match(icmp, f))
687 ip6fw_report(NULL, ip6, rif, oif, off, nxt);
692 #ifndef IP6FW_DIVERT_RESTART
693 /* Ignore divert/tee rule if socket port is "ignport" */
694 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
695 case IPV6_FW_F_DIVERT:
697 if (f->fw_divert_port == ignport)
698 continue; /* ignore this rule */
702 #endif /* IP6FW_DIVERT_RESTART */
703 /* Update statistics */
705 f->fw_bcnt += ntohs(ip6->ip6_plen);
706 f->timestamp = time_second;
708 /* Log to console if desired */
709 if ((f->fw_flg & IPV6_FW_F_PRN) && fw6_verbose)
710 ip6fw_report(f, ip6, rif, oif, off, nxt);
712 /* Take appropriate action */
713 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
714 case IPV6_FW_F_ACCEPT:
716 case IPV6_FW_F_COUNT:
718 case IPV6_FW_F_DIVERT:
719 #ifdef IP6FW_DIVERT_RESTART
720 *cookie = f->fw_number;
722 *cookie = htons(f->fw_divert_port);
723 #endif /* IP6FW_DIVERT_RESTART */
724 return(f->fw_divert_port);
727 * XXX someday tee packet here, but beware that you
728 * can't use m_copym() or m_copypacket() because
729 * the divert input routine modifies the mbuf
730 * (and these routines only increment reference
731 * counts in the case of mbuf clusters), so need
732 * to write custom routine.
735 case IPV6_FW_F_SKIPTO:
737 while (chain->chain.le_next
738 && chain->chain.le_next->rule->fw_number
741 while (chain->chain.le_next->rule->fw_number
744 chain = chain->chain.le_next;
748 /* Deny/reject this packet using this rule */
754 /* Rule 65535 should always be there and should always match */
756 panic("ip6_fw: chain");
760 * At this point, we're going to drop the packet.
761 * Send a reject notice if all of the following are true:
763 * - The packet matched a reject rule
764 * - The packet is not an ICMP packet, or is an ICMP query packet
765 * - The packet is not a multicast or broadcast packet
767 if ((rule->fw_flg & IPV6_FW_F_COMMAND) == IPV6_FW_F_REJECT
768 && (nxt != IPPROTO_ICMPV6 || is_icmp6_query(ip6, off))
769 && !((*m)->m_flags & (M_BCAST|M_MCAST))
770 && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
771 switch (rule->fw_reject_code) {
772 case IPV6_FW_REJECT_RST:
774 struct tcphdr *const tcp =
775 (struct tcphdr *) ((caddr_t)ip6 + off);
783 if (offset != 0 || (tcp->th_flags & TH_RST))
788 ti.th.th_seq = ntohl(ti.th.th_seq);
789 ti.th.th_ack = ntohl(ti.th.th_ack);
790 ti.ip6.ip6_nxt = IPPROTO_TCP;
791 if (ti.th.th_flags & TH_ACK) {
797 if (((*m)->m_flags & M_PKTHDR) != 0) {
798 ack += (*m)->m_pkthdr.len - off
799 - (ti.th.th_off << 2);
800 } else if (ip6->ip6_plen) {
801 ack += ntohs(ip6->ip6_plen) + sizeof(*ip6)
802 - off - (ti.th.th_off << 2);
809 flags = TH_RST|TH_ACK;
811 bcopy(&ti, ip6, sizeof(ti));
812 tcp_respond(NULL, ip6, (struct tcphdr *)(ip6 + 1),
813 *m, ack, seq, flags);
817 default: /* Send an ICMP unreachable using code */
819 (*m)->m_pkthdr.rcvif = oif;
820 icmp6_error(*m, ICMP6_DST_UNREACH,
821 rule->fw_reject_code, 0);
829 * Finally, drop the packet.
839 add_entry6(struct ip6_fw_head *chainptr, struct ip6_fw *frwl)
841 struct ip6_fw *ftmp = 0;
842 struct ip6_fw_chain *fwc = 0, *fcp, *fcpl = 0;
846 fwc = malloc(sizeof *fwc, M_IP6FW, M_NOWAIT);
847 ftmp = malloc(sizeof *ftmp, M_IP6FW, M_NOWAIT);
849 dprintf(("%s malloc said no\n", err_prefix));
850 if (fwc) free(fwc, M_IP6FW);
851 if (ftmp) free(ftmp, M_IP6FW);
855 bcopy(frwl, ftmp, sizeof(struct ip6_fw));
856 ftmp->fw_in_if.fu_via_if.name[IP6FW_IFNLEN - 1] = '\0';
863 if (!chainptr->lh_first) {
864 LIST_INSERT_HEAD(chainptr, fwc, chain);
867 } else if (ftmp->fw_number == (u_short)-1) {
868 if (fwc) free(fwc, M_IP6FW);
869 if (ftmp) free(ftmp, M_IP6FW);
871 dprintf(("%s bad rule number\n", err_prefix));
875 /* If entry number is 0, find highest numbered rule and add 100 */
876 if (ftmp->fw_number == 0) {
877 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
878 if (fcp->rule->fw_number != (u_short)-1)
879 nbr = fcp->rule->fw_number;
883 if (nbr < (u_short)-1 - 100)
885 ftmp->fw_number = nbr;
888 /* Got a valid number; now insert it, keeping the list ordered */
889 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
890 if (fcp->rule->fw_number > ftmp->fw_number) {
892 LIST_INSERT_AFTER(fcpl, fwc, chain);
894 LIST_INSERT_HEAD(chainptr, fwc, chain);
907 del_entry6(struct ip6_fw_head *chainptr, u_short number)
909 struct ip6_fw_chain *fcp;
914 fcp = chainptr->lh_first;
915 if (number != (u_short)-1) {
916 for (; fcp; fcp = fcp->chain.le_next) {
917 if (fcp->rule->fw_number == number) {
918 LIST_REMOVE(fcp, chain);
920 free(fcp->rule, M_IP6FW);
932 zero_entry6(struct mbuf *m)
935 struct ip6_fw_chain *fcp;
938 if (m && m->m_len != 0) {
939 if (m->m_len != sizeof(struct ip6_fw))
941 frwl = mtod(m, struct ip6_fw *);
947 * It's possible to insert multiple chain entries with the
948 * same number, so we don't stop after finding the first
949 * match if zeroing a specific entry.
952 for (fcp = ip6_fw_chain.lh_first; fcp; fcp = fcp->chain.le_next)
953 if (!frwl || frwl->fw_number == fcp->rule->fw_number) {
954 fcp->rule->fw_bcnt = fcp->rule->fw_pcnt = 0;
955 fcp->rule->timestamp = 0;
961 log(LOG_SECURITY | LOG_NOTICE,
962 "ip6fw: Entry %d cleared.\n", frwl->fw_number);
964 log(LOG_SECURITY | LOG_NOTICE,
965 "ip6fw: Accounting cleared.\n");
971 static struct ip6_fw *
972 check_ip6fw_mbuf(struct mbuf *m)
975 if (m->m_len != sizeof(struct ip6_fw)) {
976 dprintf(("%s len=%d, want %zu\n", err_prefix, m->m_len,
977 sizeof(struct ip6_fw)));
980 return(check_ip6fw_struct(mtod(m, struct ip6_fw *)));
983 static struct ip6_fw *
984 check_ip6fw_struct(struct ip6_fw *frwl)
986 /* Check for invalid flag bits */
987 if ((frwl->fw_flg & ~IPV6_FW_F_MASK) != 0) {
988 dprintf(("%s undefined flag bits set (flags=%x)\n",
989 err_prefix, frwl->fw_flg));
992 /* Must apply to incoming or outgoing (or both) */
993 if (!(frwl->fw_flg & (IPV6_FW_F_IN | IPV6_FW_F_OUT))) {
994 dprintf(("%s neither in nor out\n", err_prefix));
997 /* Empty interface name is no good */
998 if (((frwl->fw_flg & IPV6_FW_F_IIFNAME)
999 && !*frwl->fw_in_if.fu_via_if.name)
1000 || ((frwl->fw_flg & IPV6_FW_F_OIFNAME)
1001 && !*frwl->fw_out_if.fu_via_if.name)) {
1002 dprintf(("%s empty interface name\n", err_prefix));
1005 /* Sanity check interface matching */
1006 if ((frwl->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
1007 ; /* allow "via" backwards compatibility */
1008 } else if ((frwl->fw_flg & IPV6_FW_F_IN)
1009 && (frwl->fw_flg & IPV6_FW_F_OIFACE)) {
1010 dprintf(("%s outgoing interface check on incoming\n",
1014 /* Sanity check port ranges */
1015 if ((frwl->fw_flg & IPV6_FW_F_SRNG) && IPV6_FW_GETNSRCP(frwl) < 2) {
1016 dprintf(("%s src range set but n_src_p=%d\n",
1017 err_prefix, IPV6_FW_GETNSRCP(frwl)));
1020 if ((frwl->fw_flg & IPV6_FW_F_DRNG) && IPV6_FW_GETNDSTP(frwl) < 2) {
1021 dprintf(("%s dst range set but n_dst_p=%d\n",
1022 err_prefix, IPV6_FW_GETNDSTP(frwl)));
1025 if (IPV6_FW_GETNSRCP(frwl) + IPV6_FW_GETNDSTP(frwl) > IPV6_FW_MAX_PORTS) {
1026 dprintf(("%s too many ports (%d+%d)\n",
1027 err_prefix, IPV6_FW_GETNSRCP(frwl), IPV6_FW_GETNDSTP(frwl)));
1031 * Protocols other than TCP/UDP don't use port range
1033 if ((frwl->fw_prot != IPPROTO_TCP) &&
1034 (frwl->fw_prot != IPPROTO_UDP) &&
1035 (IPV6_FW_GETNSRCP(frwl) || IPV6_FW_GETNDSTP(frwl))) {
1036 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1042 * Rather than modify the entry to make such entries work,
1043 * we reject this rule and require user level utilities
1044 * to enforce whatever policy they deem appropriate.
1046 if ((frwl->fw_src.s6_addr32[0] & (~frwl->fw_smsk.s6_addr32[0])) ||
1047 (frwl->fw_src.s6_addr32[1] & (~frwl->fw_smsk.s6_addr32[1])) ||
1048 (frwl->fw_src.s6_addr32[2] & (~frwl->fw_smsk.s6_addr32[2])) ||
1049 (frwl->fw_src.s6_addr32[3] & (~frwl->fw_smsk.s6_addr32[3])) ||
1050 (frwl->fw_dst.s6_addr32[0] & (~frwl->fw_dmsk.s6_addr32[0])) ||
1051 (frwl->fw_dst.s6_addr32[1] & (~frwl->fw_dmsk.s6_addr32[1])) ||
1052 (frwl->fw_dst.s6_addr32[2] & (~frwl->fw_dmsk.s6_addr32[2])) ||
1053 (frwl->fw_dst.s6_addr32[3] & (~frwl->fw_dmsk.s6_addr32[3]))) {
1054 dprintf(("%s rule never matches\n", err_prefix));
1058 if ((frwl->fw_flg & IPV6_FW_F_FRAG) &&
1059 (frwl->fw_prot == IPPROTO_UDP || frwl->fw_prot == IPPROTO_TCP)) {
1060 if (frwl->fw_nports) {
1061 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix));
1064 if (frwl->fw_prot == IPPROTO_TCP &&
1065 frwl->fw_tcpf != frwl->fw_tcpnf) {
1066 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix));
1071 /* Check command specific stuff */
1072 switch (frwl->fw_flg & IPV6_FW_F_COMMAND)
1074 case IPV6_FW_F_REJECT:
1075 if (frwl->fw_reject_code >= 0x100
1076 && !(frwl->fw_prot == IPPROTO_TCP
1077 && frwl->fw_reject_code == IPV6_FW_REJECT_RST)) {
1078 dprintf(("%s unknown reject code\n", err_prefix));
1082 case IPV6_FW_F_DIVERT: /* Diverting to port zero is invalid */
1084 if (frwl->fw_divert_port == 0) {
1085 dprintf(("%s can't divert to port 0\n", err_prefix));
1089 case IPV6_FW_F_DENY:
1090 case IPV6_FW_F_ACCEPT:
1091 case IPV6_FW_F_COUNT:
1092 case IPV6_FW_F_SKIPTO:
1095 dprintf(("%s invalid command\n", err_prefix));
1103 ip6_fw_ctl(int stage, struct mbuf **mm)
1108 if (stage == IPV6_FW_GET) {
1109 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1110 *mm = m = m_get(M_TRYWAIT, MT_DATA); /* XXX */
1113 if (sizeof *(fcp->rule) > MLEN) {
1114 MCLGET(m, M_TRYWAIT);
1115 if ((m->m_flags & M_EXT) == 0) {
1120 for (; fcp; fcp = fcp->chain.le_next) {
1121 bcopy(fcp->rule, m->m_data, sizeof *(fcp->rule));
1122 m->m_len = sizeof *(fcp->rule);
1123 m->m_next = m_get(M_TRYWAIT, MT_DATA); /* XXX */
1129 if (sizeof *(fcp->rule) > MLEN) {
1130 MCLGET(m, M_TRYWAIT);
1131 if ((m->m_flags & M_EXT) == 0) {
1141 /* only allow get calls if secure mode > 2 */
1142 if (securelevel > 2) {
1149 if (stage == IPV6_FW_FLUSH) {
1150 while (ip6_fw_chain.lh_first != NULL &&
1151 ip6_fw_chain.lh_first->rule->fw_number != (u_short)-1) {
1152 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1154 LIST_REMOVE(ip6_fw_chain.lh_first, chain);
1156 free(fcp->rule, M_IP6FW);
1165 if (stage == IPV6_FW_ZERO) {
1166 error = zero_entry6(m);
1174 printf("%s NULL mbuf ptr\n", err_prefix);
1178 if (stage == IPV6_FW_ADD) {
1179 struct ip6_fw *frwl = check_ip6fw_mbuf(m);
1184 error = add_entry6(&ip6_fw_chain, frwl);
1191 if (stage == IPV6_FW_DEL) {
1192 if (m->m_len != sizeof(struct ip6_fw)) {
1193 dprintf(("%s len=%d, want %zu\n", err_prefix, m->m_len,
1194 sizeof(struct ip6_fw)));
1196 } else if (mtod(m, struct ip6_fw *)->fw_number == (u_short)-1) {
1197 dprintf(("%s can't delete rule 65535\n", err_prefix));
1200 error = del_entry6(&ip6_fw_chain,
1201 mtod(m, struct ip6_fw *)->fw_number);
1209 dprintf(("%s unknown request %d\n", err_prefix, stage));
1220 struct ip6_fw default_rule;
1222 ip6_fw_chk_ptr = ip6_fw_chk;
1223 ip6_fw_ctl_ptr = ip6_fw_ctl;
1224 LIST_INIT(&ip6_fw_chain);
1226 bzero(&default_rule, sizeof default_rule);
1227 default_rule.fw_prot = IPPROTO_IPV6;
1228 default_rule.fw_number = (u_short)-1;
1229 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1230 default_rule.fw_flg |= IPV6_FW_F_ACCEPT;
1232 default_rule.fw_flg |= IPV6_FW_F_DENY;
1234 default_rule.fw_flg |= IPV6_FW_F_IN | IPV6_FW_F_OUT;
1235 if (check_ip6fw_struct(&default_rule) == NULL ||
1236 add_entry6(&ip6_fw_chain, &default_rule))
1239 printf("IPv6 packet filtering initialized, ");
1240 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1241 printf("default to accept, ");
1243 #ifndef IPV6FIREWALL_VERBOSE
1244 printf("logging disabled\n");
1246 if (fw6_verbose_limit == 0)
1247 printf("unlimited logging\n");
1249 printf("logging limited to %d packets/entry\n",
1254 static ip6_fw_chk_t *old_chk_ptr;
1255 static ip6_fw_ctl_t *old_ctl_ptr;
1258 ip6fw_modevent(module_t mod, int type, void *unused)
1266 old_chk_ptr = ip6_fw_chk_ptr;
1267 old_ctl_ptr = ip6_fw_ctl_ptr;
1274 ip6_fw_chk_ptr = old_chk_ptr;
1275 ip6_fw_ctl_ptr = old_ctl_ptr;
1276 while (LIST_FIRST(&ip6_fw_chain) != NULL) {
1277 struct ip6_fw_chain *fcp = LIST_FIRST(&ip6_fw_chain);
1278 LIST_REMOVE(LIST_FIRST(&ip6_fw_chain), chain);
1279 free(fcp->rule, M_IP6FW);
1284 printf("IPv6 firewall unloaded\n");
1292 static moduledata_t ip6fwmod = {
1297 DECLARE_MODULE(ip6fw, ip6fwmod, SI_SUB_PSEUDO, SI_ORDER_ANY);