1 /* $FreeBSD: src/sys/netinet6/ip6_fw.c,v 1.2.2.9 2002/04/28 05:40:27 suz Exp $ */
2 /* $DragonFly: src/sys/net/ip6fw/ip6_fw.c,v 1.8 2004/06/02 14:42:58 eirikn Exp $ */
3 /* $KAME: ip6_fw.c,v 1.21 2001/01/24 01:25:32 itojun Exp $ */
6 * Copyright (C) 1998, 1999, 2000 and 2001 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1993 Daniel Boulet
36 * Copyright (c) 1994 Ugen J.S.Antsilevich
37 * Copyright (c) 1996 Alex Nash
39 * Redistribution and use in source forms, with and without modification,
40 * are permitted provided that this entire comment appears intact.
42 * Redistribution in binary form may occur without any restrictions.
43 * Obviously, it would be nice if you gave credit where credit is due
44 * but requiring it would be too onerous.
46 * This software is provided ``AS IS'' without any warranties of any kind.
50 * Implement IPv6 packet firewall
53 #if !defined(KLD_MODULE)
54 #include "opt_ip6fw.h"
56 #include "opt_inet6.h"
60 #error "NOT SUPPORTED IPV6 DIVERT"
62 #ifdef IP6FW_DIVERT_RESTART
63 #error "NOT SUPPORTED IPV6 DIVERT"
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
70 #include <sys/queue.h>
71 #include <sys/kernel.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/syslog.h>
77 #include <net/route.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in.h>
80 #include <netinet/ip.h>
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/in6_var.h>
85 #include <netinet/icmp6.h>
87 #include <netinet/in_pcb.h>
90 #include <netinet/ip_var.h>
91 #include <netinet/tcp.h>
92 #include <netinet/tcp_seq.h>
93 #include <netinet/tcp_timer.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet/udp.h>
97 #include <sys/sysctl.h>
99 #include <net/net_osdep.h>
101 MALLOC_DEFINE(M_IP6FW, "Ip6Fw/Ip6Acct", "Ip6Fw/Ip6Acct chain's");
103 static int fw6_debug = 1;
104 #ifdef IPV6FIREWALL_VERBOSE
105 static int fw6_verbose = 1;
107 static int fw6_verbose = 0;
109 #ifdef IPV6FIREWALL_VERBOSE_LIMIT
110 static int fw6_verbose_limit = IPV6FIREWALL_VERBOSE_LIMIT;
112 static int fw6_verbose_limit = 0;
115 LIST_HEAD (ip6_fw_head, ip6_fw_chain) ip6_fw_chain;
118 SYSCTL_DECL(_net_inet6_ip6);
119 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
120 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, enable, CTLFLAG_RW,
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, &fw6_verbose, 0, "");
124 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, verbose_limit, CTLFLAG_RW, &fw6_verbose_limit, 0, "");
127 #define dprintf(a) do { \
131 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
133 static int add_entry6 (struct ip6_fw_head *chainptr, struct ip6_fw *frwl);
134 static int del_entry6 (struct ip6_fw_head *chainptr, u_short number);
135 static int zero_entry6 (struct mbuf *m);
136 static struct ip6_fw *check_ip6fw_struct (struct ip6_fw *m);
137 static struct ip6_fw *check_ip6fw_mbuf (struct mbuf *fw);
138 static int ip6opts_match (struct ip6_hdr **ip6, struct ip6_fw *f,
140 int *off, int *nxt, u_short *offset);
141 static int port_match6 (u_short *portptr, int nports, u_short port,
143 static int tcp6flg_match (struct tcphdr *tcp6, struct ip6_fw *f);
144 static int icmp6type_match (struct icmp6_hdr * icmp, struct ip6_fw * f);
145 static void ip6fw_report (struct ip6_fw *f, struct ip6_hdr *ip6,
146 struct ifnet *rif, struct ifnet *oif, int off, int nxt);
148 static int ip6_fw_chk (struct ip6_hdr **pip6,
149 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m);
150 static int ip6_fw_ctl (int stage, struct mbuf **mm);
152 static char err_prefix[] = "ip6_fw_ctl:";
155 * Returns 1 if the port is matched by the vector, 0 otherwise
159 port_match6(u_short *portptr, int nports, u_short port, int range_flag)
164 if (portptr[0] <= port && port <= portptr[1]) {
170 while (nports-- > 0) {
171 if (*portptr++ == port) {
179 tcp6flg_match(struct tcphdr *tcp6, struct ip6_fw *f)
181 u_char flg_set, flg_clr;
184 * If an established connection is required, reject packets that
185 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
186 * other flag requirements.
188 if ((f->fw_ipflg & IPV6_FW_IF_TCPEST) &&
189 ((tcp6->th_flags & (IPV6_FW_TCPF_RST | IPV6_FW_TCPF_ACK |
190 IPV6_FW_TCPF_SYN)) == IPV6_FW_TCPF_SYN))
193 flg_set = tcp6->th_flags & f->fw_tcpf;
194 flg_clr = tcp6->th_flags & f->fw_tcpnf;
196 if (flg_set != f->fw_tcpf)
205 icmp6type_match(struct icmp6_hdr *icmp6, struct ip6_fw *f)
209 if (!(f->fw_flg & IPV6_FW_F_ICMPBIT))
212 type = icmp6->icmp6_type;
214 /* check for matching type in the bitmap */
215 if (type < IPV6_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8 &&
216 (f->fw_icmp6types[type / (sizeof(unsigned) * 8)] &
217 (1U << (type % (8 * sizeof(unsigned))))))
220 return(0); /* no match */
224 is_icmp6_query(struct ip6_hdr *ip6, int off)
226 const struct icmp6_hdr *icmp6;
229 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
230 icmp6_type = icmp6->icmp6_type;
232 if (icmp6_type == ICMP6_ECHO_REQUEST ||
233 icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
234 icmp6_type == ICMP6_WRUREQUEST ||
235 icmp6_type == ICMP6_FQDN_QUERY ||
236 icmp6_type == ICMP6_NI_QUERY)
243 ip6opts_match(struct ip6_hdr **pip6, struct ip6_fw *f, struct mbuf **m,
244 int *off, int *nxt, u_short *offset)
247 struct ip6_hdr *ip6 = *pip6;
248 struct ip6_ext *ip6e;
249 u_char opts, nopts, nopts_sve;
252 nopts = nopts_sve = f->fw_ip6nopt;
255 *off = sizeof(struct ip6_hdr);
256 len = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr);
258 ip6e = (struct ip6_ext *)((caddr_t) ip6 + *off);
259 if ((*m)->m_len < *off + sizeof(*ip6e))
260 goto opts_check; /* XXX */
263 case IPPROTO_FRAGMENT:
264 if ((*m)->m_len >= *off + sizeof(struct ip6_frag)) {
265 struct ip6_frag *ip6f;
267 ip6f = (struct ip6_frag *) ((caddr_t)ip6 + *off);
268 *offset = ip6f->ip6f_offlg & IP6F_OFF_MASK;
270 opts &= ~IPV6_FW_IP6OPT_FRAG;
271 nopts &= ~IPV6_FW_IP6OPT_FRAG;
272 *off += sizeof(struct ip6_frag);
275 opts &= ~IPV6_FW_IP6OPT_AH;
276 nopts &= ~IPV6_FW_IP6OPT_AH;
277 *off += (ip6e->ip6e_len + 2) << 2;
281 case IPPROTO_HOPOPTS:
282 opts &= ~IPV6_FW_IP6OPT_HOPOPT;
283 nopts &= ~IPV6_FW_IP6OPT_HOPOPT;
285 case IPPROTO_ROUTING:
286 opts &= ~IPV6_FW_IP6OPT_ROUTE;
287 nopts &= ~IPV6_FW_IP6OPT_ROUTE;
290 opts &= ~IPV6_FW_IP6OPT_ESP;
291 nopts &= ~IPV6_FW_IP6OPT_ESP;
294 opts &= ~IPV6_FW_IP6OPT_NONXT;
295 nopts &= ~IPV6_FW_IP6OPT_NONXT;
298 case IPPROTO_DSTOPTS:
299 opts &= ~IPV6_FW_IP6OPT_OPTS;
300 nopts &= ~IPV6_FW_IP6OPT_OPTS;
306 *off += (ip6e->ip6e_len + 1) << 3;
309 *nxt = ip6e->ip6e_nxt;
313 if (f->fw_ip6opt == f->fw_ip6nopt) /* XXX */
316 if (opts == 0 && nopts == nopts_sve)
324 iface_match(struct ifnet *ifp, union ip6_fw_if *ifu, int byname)
326 /* Check by name or by IP address */
329 if (ifu->fu_via_if.glob) {
330 if (fnmatch(ifu->fu_via_if.name, ifp->if_xname, 0)
334 if (strncmp(ifp->if_xname, ifu->fu_via_if.name,
339 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu->fu_via_ip6)) { /* Zero == wildcard */
342 for (ia = ifp->if_addrlist.tqh_first; ia; ia = ia->ifa_list.tqe_next)
345 if (ia->ifa_addr == NULL)
347 if (ia->ifa_addr->sa_family != AF_INET6)
349 if (!IN6_ARE_ADDR_EQUAL(&ifu->fu_via_ip6,
350 &(((struct sockaddr_in6 *)
351 (ia->ifa_addr))->sin6_addr)))
361 ip6fw_report(struct ip6_fw *f, struct ip6_hdr *ip6,
362 struct ifnet *rif, struct ifnet *oif, int off, int nxt)
365 struct tcphdr *const tcp6 = (struct tcphdr *) ((caddr_t) ip6+ off);
366 struct udphdr *const udp = (struct udphdr *) ((caddr_t) ip6+ off);
367 struct icmp6_hdr *const icmp6 = (struct icmp6_hdr *) ((caddr_t) ip6+ off);
370 char action2[32], proto[102], name[18];
373 count = f ? f->fw_pcnt : ++counter;
374 if (fw6_verbose_limit != 0 && count > fw6_verbose_limit)
377 /* Print command name */
378 snprintf(SNPARGS(name, 0), "ip6fw: %d", f ? f->fw_number : -1);
384 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
388 case IPV6_FW_F_REJECT:
389 if (f->fw_reject_code == IPV6_FW_REJECT_RST)
394 case IPV6_FW_F_ACCEPT:
397 case IPV6_FW_F_COUNT:
400 case IPV6_FW_F_DIVERT:
401 snprintf(SNPARGS(action2, 0), "Divert %d",
405 snprintf(SNPARGS(action2, 0), "Tee %d",
408 case IPV6_FW_F_SKIPTO:
409 snprintf(SNPARGS(action2, 0), "SkipTo %d",
420 len = snprintf(SNPARGS(proto, 0), "TCP [%s]",
421 ip6_sprintf(&ip6->ip6_src));
423 len += snprintf(SNPARGS(proto, len), ":%d ",
424 ntohs(tcp6->th_sport));
426 len += snprintf(SNPARGS(proto, len), " ");
427 len += snprintf(SNPARGS(proto, len), "[%s]",
428 ip6_sprintf(&ip6->ip6_dst));
430 snprintf(SNPARGS(proto, len), ":%d",
431 ntohs(tcp6->th_dport));
434 len = snprintf(SNPARGS(proto, 0), "UDP [%s]",
435 ip6_sprintf(&ip6->ip6_src));
437 len += snprintf(SNPARGS(proto, len), ":%d ",
438 ntohs(udp->uh_sport));
440 len += snprintf(SNPARGS(proto, len), " ");
441 len += snprintf(SNPARGS(proto, len), "[%s]",
442 ip6_sprintf(&ip6->ip6_dst));
444 snprintf(SNPARGS(proto, len), ":%d",
445 ntohs(udp->uh_dport));
449 len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP:%u.%u ",
450 icmp6->icmp6_type, icmp6->icmp6_code);
452 len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP ");
453 len += snprintf(SNPARGS(proto, len), "[%s]",
454 ip6_sprintf(&ip6->ip6_src));
455 snprintf(SNPARGS(proto, len), " [%s]",
456 ip6_sprintf(&ip6->ip6_dst));
459 len = snprintf(SNPARGS(proto, 0), "P:%d [%s]", nxt,
460 ip6_sprintf(&ip6->ip6_src));
461 snprintf(SNPARGS(proto, len), " [%s]",
462 ip6_sprintf(&ip6->ip6_dst));
467 log(LOG_SECURITY | LOG_INFO, "%s %s %s out via %s\n",
468 name, action, proto, if_name(oif));
470 log(LOG_SECURITY | LOG_INFO, "%s %s %s in via %s\n",
471 name, action, proto, if_name(rif));
473 log(LOG_SECURITY | LOG_INFO, "%s %s %s",
474 name, action, proto);
475 if (fw6_verbose_limit != 0 && count == fw6_verbose_limit)
476 log(LOG_SECURITY | LOG_INFO, "ip6fw: limit reached on entry %d\n",
477 f ? f->fw_number : -1);
483 * ip Pointer to packet header (struct ip6_hdr *)
484 * hlen Packet header length
485 * oif Outgoing interface, or NULL if packet is incoming
486 * #ifndef IP6FW_DIVERT_RESTART
487 * *cookie Ignore all divert/tee rules to this port (if non-zero)
489 * *cookie Skip up to the first rule past this rule number;
491 * *m The packet; we set to NULL when/if we nuke it.
495 * 0 The packet is to be accepted and routed normally OR
496 * the packet was denied/rejected and has been dropped;
497 * in the latter case, *m is equal to NULL upon return.
498 * port Divert the packet to port.
502 ip6_fw_chk(struct ip6_hdr **pip6,
503 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m)
505 struct ip6_fw_chain *chain;
506 struct ip6_fw *rule = NULL;
507 struct ip6_hdr *ip6 = *pip6;
508 struct ifnet *const rif = (*m)->m_pkthdr.rcvif;
510 int off = sizeof(struct ip6_hdr), nxt = ip6->ip6_nxt;
511 u_short src_port, dst_port;
512 #ifdef IP6FW_DIVERT_RESTART
513 u_int16_t skipto = *cookie;
515 u_int16_t ignport = ntohs(*cookie);
520 * Go down the chain, looking for enlightment
521 * #ifdef IP6FW_DIVERT_RESTART
522 * If we've been asked to start at a given rule immediatly, do so.
525 chain = LIST_FIRST(&ip6_fw_chain);
526 #ifdef IP6FW_DIVERT_RESTART
530 while (chain && (chain->rule->fw_number <= skipto)) {
531 chain = LIST_NEXT(chain, chain);
533 if (! chain) goto dropit;
535 #endif /* IP6FW_DIVERT_RESTART */
536 for (; chain; chain = LIST_NEXT(chain, chain)) {
537 struct ip6_fw *const f = chain->rule;
540 /* Check direction outbound */
541 if (!(f->fw_flg & IPV6_FW_F_OUT))
544 /* Check direction inbound */
545 if (!(f->fw_flg & IPV6_FW_F_IN))
549 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
550 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
551 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
552 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
553 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
555 /* If src-addr doesn't match, not this rule. */
556 if (((f->fw_flg & IPV6_FW_F_INVSRC) != 0) ^
557 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_src,&f->fw_smsk,&f->fw_src)))
560 /* If dest-addr doesn't match, not this rule. */
561 if (((f->fw_flg & IPV6_FW_F_INVDST) != 0) ^
562 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_dst,&f->fw_dmsk,&f->fw_dst)))
565 #undef IN6_ARE_ADDR_MASKEQUAL
566 /* Interface check */
567 if ((f->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
568 struct ifnet *const iface = oif ? oif : rif;
570 /* Backwards compatibility hack for "via" */
571 if (!iface || !iface_match(iface,
572 &f->fw_in_if, f->fw_flg & IPV6_FW_F_OIFNAME))
575 /* Check receive interface */
576 if ((f->fw_flg & IPV6_FW_F_IIFACE)
577 && (!rif || !iface_match(rif,
578 &f->fw_in_if, f->fw_flg & IPV6_FW_F_IIFNAME)))
580 /* Check outgoing interface */
581 if ((f->fw_flg & IPV6_FW_F_OIFACE)
582 && (!oif || !iface_match(oif,
583 &f->fw_out_if, f->fw_flg & IPV6_FW_F_OIFNAME)))
587 /* Check IP options */
588 if (!ip6opts_match(&ip6, f, m, &off, &nxt, &offset))
592 if ((f->fw_flg & IPV6_FW_F_FRAG) && !offset)
595 /* Check protocol; if wildcard, match */
596 if (f->fw_prot == IPPROTO_IPV6)
599 /* If different, don't match */
600 if (nxt != f->fw_prot)
603 #define PULLUP_TO(len) do { \
604 if ((*m)->m_len < (len) \
605 && (*m = m_pullup(*m, (len))) == 0) { \
608 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
611 /* Protocol specific checks */
617 if (offset == 1) { /* cf. RFC 1858 */
618 PULLUP_TO(off + 4); /* XXX ? */
623 * TCP flags and ports aren't available in this
624 * packet -- if this rule specified either one,
625 * we consider the rule a non-match.
627 if (f->fw_nports != 0 ||
628 f->fw_tcpf != f->fw_tcpnf)
634 tcp6 = (struct tcphdr *) ((caddr_t)ip6 + off);
635 if (((f->fw_tcpf != f->fw_tcpnf) ||
636 (f->fw_ipflg & IPV6_FW_IF_TCPEST)) &&
637 !tcp6flg_match(tcp6, f))
639 src_port = ntohs(tcp6->th_sport);
640 dst_port = ntohs(tcp6->th_dport);
650 * Port specification is unavailable -- if this
651 * rule specifies a port, we consider the rule
654 if (f->fw_nports != 0)
660 udp = (struct udphdr *) ((caddr_t)ip6 + off);
661 src_port = ntohs(udp->uh_sport);
662 dst_port = ntohs(udp->uh_dport);
664 if (!port_match6(&f->fw_pts[0],
665 IPV6_FW_GETNSRCP(f), src_port,
666 f->fw_flg & IPV6_FW_F_SRNG))
668 if (!port_match6(&f->fw_pts[IPV6_FW_GETNSRCP(f)],
669 IPV6_FW_GETNDSTP(f), dst_port,
670 f->fw_flg & IPV6_FW_F_DRNG))
677 struct icmp6_hdr *icmp;
679 if (offset != 0) /* Type isn't valid */
682 icmp = (struct icmp6_hdr *) ((caddr_t)ip6 + off);
683 if (!icmp6type_match(icmp, f))
691 ip6fw_report(NULL, ip6, rif, oif, off, nxt);
696 #ifndef IP6FW_DIVERT_RESTART
697 /* Ignore divert/tee rule if socket port is "ignport" */
698 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
699 case IPV6_FW_F_DIVERT:
701 if (f->fw_divert_port == ignport)
702 continue; /* ignore this rule */
706 #endif /* IP6FW_DIVERT_RESTART */
707 /* Update statistics */
709 f->fw_bcnt += ntohs(ip6->ip6_plen);
710 f->timestamp = time_second;
712 /* Log to console if desired */
713 if ((f->fw_flg & IPV6_FW_F_PRN) && fw6_verbose)
714 ip6fw_report(f, ip6, rif, oif, off, nxt);
716 /* Take appropriate action */
717 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
718 case IPV6_FW_F_ACCEPT:
720 case IPV6_FW_F_COUNT:
722 case IPV6_FW_F_DIVERT:
723 #ifdef IP6FW_DIVERT_RESTART
724 *cookie = f->fw_number;
726 *cookie = htons(f->fw_divert_port);
727 #endif /* IP6FW_DIVERT_RESTART */
728 return(f->fw_divert_port);
731 * XXX someday tee packet here, but beware that you
732 * can't use m_copym() or m_copypacket() because
733 * the divert input routine modifies the mbuf
734 * (and these routines only increment reference
735 * counts in the case of mbuf clusters), so need
736 * to write custom routine.
739 case IPV6_FW_F_SKIPTO:
741 while (chain->chain.le_next
742 && chain->chain.le_next->rule->fw_number
745 while (chain->chain.le_next->rule->fw_number
748 chain = chain->chain.le_next;
752 /* Deny/reject this packet using this rule */
758 /* Rule 65535 should always be there and should always match */
760 panic("ip6_fw: chain");
764 * At this point, we're going to drop the packet.
765 * Send a reject notice if all of the following are true:
767 * - The packet matched a reject rule
768 * - The packet is not an ICMP packet, or is an ICMP query packet
769 * - The packet is not a multicast or broadcast packet
771 if ((rule->fw_flg & IPV6_FW_F_COMMAND) == IPV6_FW_F_REJECT
772 && (nxt != IPPROTO_ICMPV6 || is_icmp6_query(ip6, off))
773 && !((*m)->m_flags & (M_BCAST|M_MCAST))
774 && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
775 switch (rule->fw_reject_code) {
776 case IPV6_FW_REJECT_RST:
778 struct tcphdr *const tcp =
779 (struct tcphdr *) ((caddr_t)ip6 + off);
787 if (offset != 0 || (tcp->th_flags & TH_RST))
794 ti.ip6.ip6_nxt = IPPROTO_TCP;
795 if (ti.th.th_flags & TH_ACK) {
801 if (((*m)->m_flags & M_PKTHDR) != 0) {
802 ack += (*m)->m_pkthdr.len - off
803 - (ti.th.th_off << 2);
804 } else if (ip6->ip6_plen) {
805 ack += ntohs(ip6->ip6_plen) + sizeof(*ip6)
806 - off - (ti.th.th_off << 2);
813 flags = TH_RST|TH_ACK;
815 bcopy(&ti, ip6, sizeof(ti));
816 tcp_respond(NULL, ip6, (struct tcphdr *)(ip6 + 1),
817 *m, ack, seq, flags);
821 default: /* Send an ICMP unreachable using code */
823 (*m)->m_pkthdr.rcvif = oif;
824 icmp6_error(*m, ICMP6_DST_UNREACH,
825 rule->fw_reject_code, 0);
833 * Finally, drop the packet.
843 add_entry6(struct ip6_fw_head *chainptr, struct ip6_fw *frwl)
845 struct ip6_fw *ftmp = 0;
846 struct ip6_fw_chain *fwc = 0, *fcp, *fcpl = 0;
850 fwc = malloc(sizeof *fwc, M_IP6FW, M_INTWAIT);
851 ftmp = malloc(sizeof *ftmp, M_IP6FW, M_INTWAIT);
853 bcopy(frwl, ftmp, sizeof(struct ip6_fw));
854 ftmp->fw_in_if.fu_via_if.name[IP6FW_IFNLEN - 1] = '\0';
861 if (!chainptr->lh_first) {
862 LIST_INSERT_HEAD(chainptr, fwc, chain);
865 } else if (ftmp->fw_number == (u_short)-1) {
866 if (fwc) free(fwc, M_IP6FW);
867 if (ftmp) free(ftmp, M_IP6FW);
869 dprintf(("%s bad rule number\n", err_prefix));
873 /* If entry number is 0, find highest numbered rule and add 100 */
874 if (ftmp->fw_number == 0) {
875 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
876 if (fcp->rule->fw_number != (u_short)-1)
877 nbr = fcp->rule->fw_number;
881 if (nbr < (u_short)-1 - 100)
883 ftmp->fw_number = nbr;
886 /* Got a valid number; now insert it, keeping the list ordered */
887 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
888 if (fcp->rule->fw_number > ftmp->fw_number) {
890 LIST_INSERT_AFTER(fcpl, fwc, chain);
892 LIST_INSERT_HEAD(chainptr, fwc, chain);
905 del_entry6(struct ip6_fw_head *chainptr, u_short number)
907 struct ip6_fw_chain *fcp;
912 fcp = chainptr->lh_first;
913 if (number != (u_short)-1) {
914 for (; fcp; fcp = fcp->chain.le_next) {
915 if (fcp->rule->fw_number == number) {
916 LIST_REMOVE(fcp, chain);
918 free(fcp->rule, M_IP6FW);
930 zero_entry6(struct mbuf *m)
933 struct ip6_fw_chain *fcp;
936 if (m && m->m_len != 0) {
937 if (m->m_len != sizeof(struct ip6_fw))
939 frwl = mtod(m, struct ip6_fw *);
945 * It's possible to insert multiple chain entries with the
946 * same number, so we don't stop after finding the first
947 * match if zeroing a specific entry.
950 for (fcp = ip6_fw_chain.lh_first; fcp; fcp = fcp->chain.le_next)
951 if (!frwl || frwl->fw_number == fcp->rule->fw_number) {
952 fcp->rule->fw_bcnt = fcp->rule->fw_pcnt = 0;
953 fcp->rule->timestamp = 0;
959 log(LOG_SECURITY | LOG_NOTICE,
960 "ip6fw: Entry %d cleared.\n", frwl->fw_number);
962 log(LOG_SECURITY | LOG_NOTICE,
963 "ip6fw: Accounting cleared.\n");
969 static struct ip6_fw *
970 check_ip6fw_mbuf(struct mbuf *m)
973 if (m->m_len != sizeof(struct ip6_fw)) {
974 dprintf(("%s len=%d, want %d\n", err_prefix, m->m_len,
975 sizeof(struct ip6_fw)));
978 return(check_ip6fw_struct(mtod(m, struct ip6_fw *)));
981 static struct ip6_fw *
982 check_ip6fw_struct(struct ip6_fw *frwl)
984 /* Check for invalid flag bits */
985 if ((frwl->fw_flg & ~IPV6_FW_F_MASK) != 0) {
986 dprintf(("%s undefined flag bits set (flags=%x)\n",
987 err_prefix, frwl->fw_flg));
990 /* Must apply to incoming or outgoing (or both) */
991 if (!(frwl->fw_flg & (IPV6_FW_F_IN | IPV6_FW_F_OUT))) {
992 dprintf(("%s neither in nor out\n", err_prefix));
995 /* Empty interface name is no good */
996 if (((frwl->fw_flg & IPV6_FW_F_IIFNAME)
997 && !*frwl->fw_in_if.fu_via_if.name)
998 || ((frwl->fw_flg & IPV6_FW_F_OIFNAME)
999 && !*frwl->fw_out_if.fu_via_if.name)) {
1000 dprintf(("%s empty interface name\n", err_prefix));
1003 /* Sanity check interface matching */
1004 if ((frwl->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
1005 ; /* allow "via" backwards compatibility */
1006 } else if ((frwl->fw_flg & IPV6_FW_F_IN)
1007 && (frwl->fw_flg & IPV6_FW_F_OIFACE)) {
1008 dprintf(("%s outgoing interface check on incoming\n",
1012 /* Sanity check port ranges */
1013 if ((frwl->fw_flg & IPV6_FW_F_SRNG) && IPV6_FW_GETNSRCP(frwl) < 2) {
1014 dprintf(("%s src range set but n_src_p=%d\n",
1015 err_prefix, IPV6_FW_GETNSRCP(frwl)));
1018 if ((frwl->fw_flg & IPV6_FW_F_DRNG) && IPV6_FW_GETNDSTP(frwl) < 2) {
1019 dprintf(("%s dst range set but n_dst_p=%d\n",
1020 err_prefix, IPV6_FW_GETNDSTP(frwl)));
1023 if (IPV6_FW_GETNSRCP(frwl) + IPV6_FW_GETNDSTP(frwl) > IPV6_FW_MAX_PORTS) {
1024 dprintf(("%s too many ports (%d+%d)\n",
1025 err_prefix, IPV6_FW_GETNSRCP(frwl), IPV6_FW_GETNDSTP(frwl)));
1029 * Protocols other than TCP/UDP don't use port range
1031 if ((frwl->fw_prot != IPPROTO_TCP) &&
1032 (frwl->fw_prot != IPPROTO_UDP) &&
1033 (IPV6_FW_GETNSRCP(frwl) || IPV6_FW_GETNDSTP(frwl))) {
1034 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1040 * Rather than modify the entry to make such entries work,
1041 * we reject this rule and require user level utilities
1042 * to enforce whatever policy they deem appropriate.
1044 if ((frwl->fw_src.s6_addr32[0] & (~frwl->fw_smsk.s6_addr32[0])) ||
1045 (frwl->fw_src.s6_addr32[1] & (~frwl->fw_smsk.s6_addr32[1])) ||
1046 (frwl->fw_src.s6_addr32[2] & (~frwl->fw_smsk.s6_addr32[2])) ||
1047 (frwl->fw_src.s6_addr32[3] & (~frwl->fw_smsk.s6_addr32[3])) ||
1048 (frwl->fw_dst.s6_addr32[0] & (~frwl->fw_dmsk.s6_addr32[0])) ||
1049 (frwl->fw_dst.s6_addr32[1] & (~frwl->fw_dmsk.s6_addr32[1])) ||
1050 (frwl->fw_dst.s6_addr32[2] & (~frwl->fw_dmsk.s6_addr32[2])) ||
1051 (frwl->fw_dst.s6_addr32[3] & (~frwl->fw_dmsk.s6_addr32[3]))) {
1052 dprintf(("%s rule never matches\n", err_prefix));
1056 if ((frwl->fw_flg & IPV6_FW_F_FRAG) &&
1057 (frwl->fw_prot == IPPROTO_UDP || frwl->fw_prot == IPPROTO_TCP)) {
1058 if (frwl->fw_nports) {
1059 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix));
1062 if (frwl->fw_prot == IPPROTO_TCP &&
1063 frwl->fw_tcpf != frwl->fw_tcpnf) {
1064 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix));
1069 /* Check command specific stuff */
1070 switch (frwl->fw_flg & IPV6_FW_F_COMMAND)
1072 case IPV6_FW_F_REJECT:
1073 if (frwl->fw_reject_code >= 0x100
1074 && !(frwl->fw_prot == IPPROTO_TCP
1075 && frwl->fw_reject_code == IPV6_FW_REJECT_RST)) {
1076 dprintf(("%s unknown reject code\n", err_prefix));
1080 case IPV6_FW_F_DIVERT: /* Diverting to port zero is invalid */
1082 if (frwl->fw_divert_port == 0) {
1083 dprintf(("%s can't divert to port 0\n", err_prefix));
1087 case IPV6_FW_F_DENY:
1088 case IPV6_FW_F_ACCEPT:
1089 case IPV6_FW_F_COUNT:
1090 case IPV6_FW_F_SKIPTO:
1093 dprintf(("%s invalid command\n", err_prefix));
1101 ip6_fw_ctl(int stage, struct mbuf **mm)
1106 if (stage == IPV6_FW_GET) {
1107 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1108 *mm = m = m_get(MB_WAIT, MT_DATA); /* XXX */
1111 if (sizeof *(fcp->rule) > MLEN) {
1113 if ((m->m_flags & M_EXT) == 0) {
1118 for (; fcp; fcp = fcp->chain.le_next) {
1119 bcopy(fcp->rule, m->m_data, sizeof *(fcp->rule));
1120 m->m_len = sizeof *(fcp->rule);
1121 m->m_next = m_get(MB_WAIT, MT_DATA); /* XXX */
1127 if (sizeof *(fcp->rule) > MLEN) {
1129 if ((m->m_flags & M_EXT) == 0) {
1139 /* only allow get calls if secure mode > 2 */
1140 if (securelevel > 2) {
1147 if (stage == IPV6_FW_FLUSH) {
1148 while (ip6_fw_chain.lh_first != NULL &&
1149 ip6_fw_chain.lh_first->rule->fw_number != (u_short)-1) {
1150 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1152 LIST_REMOVE(ip6_fw_chain.lh_first, chain);
1154 free(fcp->rule, M_IP6FW);
1163 if (stage == IPV6_FW_ZERO) {
1164 error = zero_entry6(m);
1172 printf("%s NULL mbuf ptr\n", err_prefix);
1176 if (stage == IPV6_FW_ADD) {
1177 struct ip6_fw *frwl = check_ip6fw_mbuf(m);
1182 error = add_entry6(&ip6_fw_chain, frwl);
1189 if (stage == IPV6_FW_DEL) {
1190 if (m->m_len != sizeof(struct ip6_fw)) {
1191 dprintf(("%s len=%d, want %d\n", err_prefix, m->m_len,
1192 sizeof(struct ip6_fw)));
1194 } else if (mtod(m, struct ip6_fw *)->fw_number == (u_short)-1) {
1195 dprintf(("%s can't delete rule 65535\n", err_prefix));
1198 error = del_entry6(&ip6_fw_chain,
1199 mtod(m, struct ip6_fw *)->fw_number);
1207 dprintf(("%s unknown request %d\n", err_prefix, stage));
1218 struct ip6_fw default_rule;
1220 ip6_fw_chk_ptr = ip6_fw_chk;
1221 ip6_fw_ctl_ptr = ip6_fw_ctl;
1222 LIST_INIT(&ip6_fw_chain);
1224 bzero(&default_rule, sizeof default_rule);
1225 default_rule.fw_prot = IPPROTO_IPV6;
1226 default_rule.fw_number = (u_short)-1;
1227 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1228 default_rule.fw_flg |= IPV6_FW_F_ACCEPT;
1230 default_rule.fw_flg |= IPV6_FW_F_DENY;
1232 default_rule.fw_flg |= IPV6_FW_F_IN | IPV6_FW_F_OUT;
1233 if (check_ip6fw_struct(&default_rule) == NULL ||
1234 add_entry6(&ip6_fw_chain, &default_rule))
1235 panic(__FUNCTION__);
1237 printf("IPv6 packet filtering initialized, ");
1238 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1239 printf("default to accept, ");
1241 #ifndef IPV6FIREWALL_VERBOSE
1242 printf("logging disabled\n");
1244 if (fw6_verbose_limit == 0)
1245 printf("unlimited logging\n");
1247 printf("logging limited to %d packets/entry\n",
1252 static ip6_fw_chk_t *old_chk_ptr;
1253 static ip6_fw_ctl_t *old_ctl_ptr;
1256 ip6fw_modevent(module_t mod, int type, void *unused)
1264 old_chk_ptr = ip6_fw_chk_ptr;
1265 old_ctl_ptr = ip6_fw_ctl_ptr;
1272 ip6_fw_chk_ptr = old_chk_ptr;
1273 ip6_fw_ctl_ptr = old_ctl_ptr;
1274 while (LIST_FIRST(&ip6_fw_chain) != NULL) {
1275 struct ip6_fw_chain *fcp = LIST_FIRST(&ip6_fw_chain);
1276 LIST_REMOVE(LIST_FIRST(&ip6_fw_chain), chain);
1277 free(fcp->rule, M_IP6FW);
1282 printf("IPv6 firewall unloaded\n");
1290 static moduledata_t ip6fwmod = {
1295 DECLARE_MODULE(ip6fw, ip6fwmod, SI_SUB_PSEUDO, SI_ORDER_ANY);