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/module.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>
89 #include <netinet6/ip6_fw.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 static 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 | CTLFLAG_SECURE,
121 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, enable, CTLFLAG_RW | CTLFLAG_SECURE,
122 &ip6_fw_enable, 0, "Enable ip6fw");
123 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, debug, CTLFLAG_RW, &fw6_debug, 0, "");
124 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, verbose, CTLFLAG_RW | CTLFLAG_SECURE,
125 &fw6_verbose, 0, "");
126 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, verbose_limit, CTLFLAG_RW, &fw6_verbose_limit, 0, "");
129 #define dprintf(a) do { \
132 } while (/*CONSTCOND*/ 0)
133 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
135 static int add_entry6 __P((struct ip6_fw_head *chainptr, struct ip6_fw *frwl));
136 static int del_entry6 __P((struct ip6_fw_head *chainptr, u_short number));
137 static int zero_entry6 __P((struct mbuf *m));
138 static struct ip6_fw *check_ip6fw_struct __P((struct ip6_fw *m));
139 static struct ip6_fw *check_ip6fw_mbuf __P((struct mbuf *fw));
140 static int ip6opts_match __P((struct ip6_hdr **ip6, struct ip6_fw *f,
142 int *off, int *nxt, u_short *offset));
143 static int port_match6 __P((u_short *portptr, int nports, u_short port,
145 static int tcp6flg_match __P((struct tcphdr *tcp6, struct ip6_fw *f));
146 static int icmp6type_match __P((struct icmp6_hdr * icmp, struct ip6_fw * f));
147 static void ip6fw_report __P((struct ip6_fw *f, struct ip6_hdr *ip6,
148 struct ifnet *rif, struct ifnet *oif, int off, int nxt));
150 static int ip6_fw_chk __P((struct ip6_hdr **pip6,
151 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m));
152 static int ip6_fw_ctl __P((int stage, struct mbuf **mm));
154 static char err_prefix[] = "ip6_fw_ctl:";
157 * Returns 1 if the port is matched by the vector, 0 otherwise
162 port_match6(u_short *portptr, int nports, u_short port, int range_flag)
167 if (portptr[0] <= port && port <= portptr[1]) {
173 while (nports-- > 0) {
174 if (*portptr++ == port) {
182 tcp6flg_match(struct tcphdr *tcp6, struct ip6_fw *f)
184 u_char flg_set, flg_clr;
187 * If an established connection is required, reject packets that
188 * have only SYN of RST|ACK|SYN set. Otherwise, fall through to
189 * other flag requirements.
191 if ((f->fw_ipflg & IPV6_FW_IF_TCPEST) &&
192 ((tcp6->th_flags & (IPV6_FW_TCPF_RST | IPV6_FW_TCPF_ACK |
193 IPV6_FW_TCPF_SYN)) == IPV6_FW_TCPF_SYN))
196 flg_set = tcp6->th_flags & f->fw_tcpf;
197 flg_clr = tcp6->th_flags & f->fw_tcpnf;
199 if (flg_set != f->fw_tcpf)
208 icmp6type_match(struct icmp6_hdr *icmp6, struct ip6_fw *f)
212 if (!(f->fw_flg & IPV6_FW_F_ICMPBIT))
215 type = icmp6->icmp6_type;
217 /* check for matching type in the bitmap */
218 if (type < IPV6_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8 &&
219 (f->fw_icmp6types[type / (sizeof(unsigned) * 8)] &
220 (1U << (type % (8 * sizeof(unsigned))))))
223 return (0); /* no match */
227 is_icmp6_query(struct ip6_hdr *ip6, int off)
229 const struct icmp6_hdr *icmp6;
232 icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
233 icmp6_type = icmp6->icmp6_type;
235 if (icmp6_type == ICMP6_ECHO_REQUEST ||
236 icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
237 icmp6_type == ICMP6_WRUREQUEST ||
238 icmp6_type == ICMP6_FQDN_QUERY ||
239 icmp6_type == ICMP6_NI_QUERY)
246 ip6opts_match(struct ip6_hdr **pip6, struct ip6_fw *f, struct mbuf **m,
247 int *off, int *nxt, u_short *offset)
250 struct ip6_hdr *ip6 = *pip6;
251 struct ip6_ext *ip6e;
252 u_char opts, nopts, nopts_sve;
255 nopts = nopts_sve = f->fw_ip6nopt;
258 *off = sizeof(struct ip6_hdr);
259 len = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr);
261 ip6e = (struct ip6_ext *)((caddr_t) ip6 + *off);
262 if ((*m)->m_len < *off + sizeof(*ip6e))
263 goto opts_check; /* XXX */
266 case IPPROTO_FRAGMENT:
267 if ((*m)->m_len >= *off + sizeof(struct ip6_frag)) {
268 struct ip6_frag *ip6f;
270 ip6f = (struct ip6_frag *) ((caddr_t)ip6 + *off);
271 *offset = ip6f->ip6f_offlg & IP6F_OFF_MASK;
273 opts &= ~IPV6_FW_IP6OPT_FRAG;
274 nopts &= ~IPV6_FW_IP6OPT_FRAG;
275 *off += sizeof(struct ip6_frag);
278 opts &= ~IPV6_FW_IP6OPT_AH;
279 nopts &= ~IPV6_FW_IP6OPT_AH;
280 *off += (ip6e->ip6e_len + 2) << 2;
284 case IPPROTO_HOPOPTS:
285 opts &= ~IPV6_FW_IP6OPT_HOPOPT;
286 nopts &= ~IPV6_FW_IP6OPT_HOPOPT;
288 case IPPROTO_ROUTING:
289 opts &= ~IPV6_FW_IP6OPT_ROUTE;
290 nopts &= ~IPV6_FW_IP6OPT_ROUTE;
293 opts &= ~IPV6_FW_IP6OPT_ESP;
294 nopts &= ~IPV6_FW_IP6OPT_ESP;
297 opts &= ~IPV6_FW_IP6OPT_NONXT;
298 nopts &= ~IPV6_FW_IP6OPT_NONXT;
300 case IPPROTO_DSTOPTS:
301 opts &= ~IPV6_FW_IP6OPT_OPTS;
302 nopts &= ~IPV6_FW_IP6OPT_OPTS;
307 *off += (ip6e->ip6e_len + 1) << 3;
310 *nxt = ip6e->ip6e_nxt;
314 if (f->fw_ip6opt == f->fw_ip6nopt) /* XXX */
317 if (opts == 0 && nopts == nopts_sve)
326 iface_match(struct ifnet *ifp, union ip6_fw_if *ifu, int byname)
328 /* Check by name or by IP address */
331 if (ifu->fu_via_if.glob) {
332 if (fnmatch(ifu->fu_via_if.name, ifp->if_xname, 0)
336 if (strncmp(ifp->if_xname, ifu->fu_via_if.name,
341 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu->fu_via_ip6)) { /* Zero == wildcard */
344 for (ia = ifp->if_addrlist.tqh_first; ia; ia = ia->ifa_list.tqe_next) {
346 if (ia->ifa_addr == NULL)
348 if (ia->ifa_addr->sa_family != AF_INET6)
350 if (!IN6_ARE_ADDR_EQUAL(&ifu->fu_via_ip6,
351 &(((struct sockaddr_in6 *)
352 (ia->ifa_addr))->sin6_addr)))
362 ip6fw_report(struct ip6_fw *f, struct ip6_hdr *ip6,
363 struct ifnet *rif, struct ifnet *oif, int off, int nxt)
366 struct tcphdr *const tcp6 = (struct tcphdr *) ((caddr_t) ip6+ off);
367 struct udphdr *const udp = (struct udphdr *) ((caddr_t) ip6+ off);
368 struct icmp6_hdr *const icmp6 = (struct icmp6_hdr *) ((caddr_t) ip6+ off);
371 char action2[32], proto[102], name[18];
374 count = f ? f->fw_pcnt : ++counter;
375 if (fw6_verbose_limit != 0 && count > fw6_verbose_limit)
378 /* Print command name */
379 snprintf(SNPARGS(name, 0), "ip6fw: %d", f ? f->fw_number : -1);
385 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
389 case IPV6_FW_F_REJECT:
390 if (f->fw_reject_code == IPV6_FW_REJECT_RST)
395 case IPV6_FW_F_ACCEPT:
398 case IPV6_FW_F_COUNT:
401 case IPV6_FW_F_DIVERT:
402 snprintf(SNPARGS(action2, 0), "Divert %d",
406 snprintf(SNPARGS(action2, 0), "Tee %d",
409 case IPV6_FW_F_SKIPTO:
410 snprintf(SNPARGS(action2, 0), "SkipTo %d",
421 len = snprintf(SNPARGS(proto, 0), "TCP [%s]",
422 ip6_sprintf(&ip6->ip6_src));
424 len += snprintf(SNPARGS(proto, len), ":%d ",
425 ntohs(tcp6->th_sport));
427 len += snprintf(SNPARGS(proto, len), " ");
428 len += snprintf(SNPARGS(proto, len), "[%s]",
429 ip6_sprintf(&ip6->ip6_dst));
431 snprintf(SNPARGS(proto, len), ":%d",
432 ntohs(tcp6->th_dport));
435 len = snprintf(SNPARGS(proto, 0), "UDP [%s]",
436 ip6_sprintf(&ip6->ip6_src));
438 len += snprintf(SNPARGS(proto, len), ":%d ",
439 ntohs(udp->uh_sport));
441 len += snprintf(SNPARGS(proto, len), " ");
442 len += snprintf(SNPARGS(proto, len), "[%s]",
443 ip6_sprintf(&ip6->ip6_dst));
445 snprintf(SNPARGS(proto, len), ":%d",
446 ntohs(udp->uh_dport));
450 len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP:%u.%u ",
451 icmp6->icmp6_type, icmp6->icmp6_code);
453 len = snprintf(SNPARGS(proto, 0), "IPV6-ICMP ");
454 len += snprintf(SNPARGS(proto, len), "[%s]",
455 ip6_sprintf(&ip6->ip6_src));
456 snprintf(SNPARGS(proto, len), " [%s]",
457 ip6_sprintf(&ip6->ip6_dst));
460 len = snprintf(SNPARGS(proto, 0), "P:%d [%s]", nxt,
461 ip6_sprintf(&ip6->ip6_src));
462 snprintf(SNPARGS(proto, len), " [%s]",
463 ip6_sprintf(&ip6->ip6_dst));
468 log(LOG_SECURITY | LOG_INFO, "%s %s %s out via %s\n",
469 name, action, proto, if_name(oif));
471 log(LOG_SECURITY | LOG_INFO, "%s %s %s in via %s\n",
472 name, action, proto, if_name(rif));
474 log(LOG_SECURITY | LOG_INFO, "%s %s %s",
475 name, action, proto);
476 if (fw6_verbose_limit != 0 && count == fw6_verbose_limit)
477 log(LOG_SECURITY | LOG_INFO, "ip6fw: limit reached on entry %d\n",
478 f ? f->fw_number : -1);
484 * ip Pointer to packet header (struct ip6_hdr *)
485 * hlen Packet header length
486 * oif Outgoing interface, or NULL if packet is incoming
487 * #ifndef IP6FW_DIVERT_RESTART
488 * *cookie Ignore all divert/tee rules to this port (if non-zero)
490 * *cookie Skip up to the first rule past this rule number;
492 * *m The packet; we set to NULL when/if we nuke it.
496 * 0 The packet is to be accepted and routed normally OR
497 * the packet was denied/rejected and has been dropped;
498 * in the latter case, *m is equal to NULL upon return.
499 * port Divert the packet to port.
503 ip6_fw_chk(struct ip6_hdr **pip6,
504 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m)
506 struct ip6_fw_chain *chain;
507 struct ip6_fw *rule = NULL;
508 struct ip6_hdr *ip6 = *pip6;
509 struct ifnet *const rif = (*m)->m_pkthdr.rcvif;
511 int off = sizeof(struct ip6_hdr), nxt = ip6->ip6_nxt;
512 u_short src_port, dst_port;
513 #ifdef IP6FW_DIVERT_RESTART
514 u_int16_t skipto = *cookie;
516 u_int16_t ignport = ntohs(*cookie);
521 * Go down the chain, looking for enlightment
522 * #ifdef IP6FW_DIVERT_RESTART
523 * If we've been asked to start at a given rule immediatly, do so.
526 chain = LIST_FIRST(&ip6_fw_chain);
527 #ifdef IP6FW_DIVERT_RESTART
531 while (chain && (chain->rule->fw_number <= skipto)) {
532 chain = LIST_NEXT(chain, chain);
534 if (! chain) goto dropit;
536 #endif /* IP6FW_DIVERT_RESTART */
537 for (; chain; chain = LIST_NEXT(chain, chain)) {
538 struct ip6_fw *const f = chain->rule;
541 /* Check direction outbound */
542 if (!(f->fw_flg & IPV6_FW_F_OUT))
545 /* Check direction inbound */
546 if (!(f->fw_flg & IPV6_FW_F_IN))
550 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
551 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
552 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
553 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
554 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
556 /* If src-addr doesn't match, not this rule. */
557 if (((f->fw_flg & IPV6_FW_F_INVSRC) != 0) ^
558 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_src,&f->fw_smsk,&f->fw_src)))
561 /* If dest-addr doesn't match, not this rule. */
562 if (((f->fw_flg & IPV6_FW_F_INVDST) != 0) ^
563 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_dst,&f->fw_dmsk,&f->fw_dst)))
566 #undef IN6_ARE_ADDR_MASKEQUAL
567 /* Interface check */
568 if ((f->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
569 struct ifnet *const iface = oif ? oif : rif;
571 /* Backwards compatibility hack for "via" */
572 if (!iface || !iface_match(iface,
573 &f->fw_in_if, f->fw_flg & IPV6_FW_F_OIFNAME))
576 /* Check receive interface */
577 if ((f->fw_flg & IPV6_FW_F_IIFACE)
578 && (!rif || !iface_match(rif,
579 &f->fw_in_if, f->fw_flg & IPV6_FW_F_IIFNAME)))
581 /* Check outgoing interface */
582 if ((f->fw_flg & IPV6_FW_F_OIFACE)
583 && (!oif || !iface_match(oif,
584 &f->fw_out_if, f->fw_flg & IPV6_FW_F_OIFNAME)))
588 /* Check IP options */
589 if (!ip6opts_match(&ip6, f, m, &off, &nxt, &offset))
593 if ((f->fw_flg & IPV6_FW_F_FRAG) && !offset)
596 /* Check protocol; if wildcard, match */
597 if (f->fw_prot == IPPROTO_IPV6)
600 /* If different, don't match */
601 if (nxt != f->fw_prot)
604 #define PULLUP_TO(len) do { \
605 if ((*m)->m_len < (len) \
606 && (*m = m_pullup(*m, (len))) == 0) { \
609 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
610 } while (/*CONSTCOND*/ 0)
612 /* Protocol specific checks */
618 if (offset == 1) { /* cf. RFC 1858 */
619 PULLUP_TO(off + 4); /* XXX ? */
624 * TCP flags and ports aren't available in this
625 * packet -- if this rule specified either one,
626 * we consider the rule a non-match.
628 if (f->fw_nports != 0 ||
629 f->fw_tcpf != f->fw_tcpnf)
635 tcp6 = (struct tcphdr *) ((caddr_t)ip6 + off);
636 if (((f->fw_tcpf != f->fw_tcpnf) ||
637 (f->fw_ipflg & IPV6_FW_IF_TCPEST)) &&
638 !tcp6flg_match(tcp6, f))
640 src_port = ntohs(tcp6->th_sport);
641 dst_port = ntohs(tcp6->th_dport);
651 * Port specification is unavailable -- if this
652 * rule specifies a port, we consider the rule
655 if (f->fw_nports != 0)
661 udp = (struct udphdr *) ((caddr_t)ip6 + off);
662 src_port = ntohs(udp->uh_sport);
663 dst_port = ntohs(udp->uh_dport);
665 if (!port_match6(&f->fw_pts[0],
666 IPV6_FW_GETNSRCP(f), src_port,
667 f->fw_flg & IPV6_FW_F_SRNG))
669 if (!port_match6(&f->fw_pts[IPV6_FW_GETNSRCP(f)],
670 IPV6_FW_GETNDSTP(f), dst_port,
671 f->fw_flg & IPV6_FW_F_DRNG))
678 struct icmp6_hdr *icmp;
680 if (offset != 0) /* Type isn't valid */
683 icmp = (struct icmp6_hdr *) ((caddr_t)ip6 + off);
684 if (!icmp6type_match(icmp, f))
692 ip6fw_report(NULL, ip6, rif, oif, off, nxt);
697 #ifndef IP6FW_DIVERT_RESTART
698 /* Ignore divert/tee rule if socket port is "ignport" */
699 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
700 case IPV6_FW_F_DIVERT:
702 if (f->fw_divert_port == ignport)
703 continue; /* ignore this rule */
707 #endif /* IP6FW_DIVERT_RESTART */
708 /* Update statistics */
710 f->fw_bcnt += ntohs(ip6->ip6_plen);
711 f->timestamp = time_second;
713 /* Log to console if desired */
714 if ((f->fw_flg & IPV6_FW_F_PRN) && fw6_verbose)
715 ip6fw_report(f, ip6, rif, oif, off, nxt);
717 /* Take appropriate action */
718 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
719 case IPV6_FW_F_ACCEPT:
721 case IPV6_FW_F_COUNT:
723 case IPV6_FW_F_DIVERT:
724 #ifdef IP6FW_DIVERT_RESTART
725 *cookie = f->fw_number;
727 *cookie = htons(f->fw_divert_port);
728 #endif /* IP6FW_DIVERT_RESTART */
729 return (f->fw_divert_port);
732 * XXX someday tee packet here, but beware that you
733 * can't use m_copym() or m_copypacket() because
734 * the divert input routine modifies the mbuf
735 * (and these routines only increment reference
736 * counts in the case of mbuf clusters), so need
737 * to write custom routine.
740 case IPV6_FW_F_SKIPTO:
742 while (chain->chain.le_next
743 && chain->chain.le_next->rule->fw_number
746 while (chain->chain.le_next->rule->fw_number
749 chain = chain->chain.le_next;
753 /* Deny/reject this packet using this rule */
759 /* Rule 65535 should always be there and should always match */
761 panic("ip6_fw: chain");
765 * At this point, we're going to drop the packet.
766 * Send a reject notice if all of the following are true:
768 * - The packet matched a reject rule
769 * - The packet is not an ICMP packet, or is an ICMP query packet
770 * - The packet is not a multicast or broadcast packet
772 if ((rule->fw_flg & IPV6_FW_F_COMMAND) == IPV6_FW_F_REJECT
773 && (nxt != IPPROTO_ICMPV6 || is_icmp6_query(ip6, off))
774 && !((*m)->m_flags & (M_BCAST|M_MCAST))
775 && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
776 switch (rule->fw_reject_code) {
777 case IPV6_FW_REJECT_RST:
778 #if 1 /* not tested */
780 struct tcphdr *const tcp =
781 (struct tcphdr *) ((caddr_t)ip6 + off);
789 if (offset != 0 || (tcp->th_flags & TH_RST))
794 ti.th.th_seq = ntohl(ti.th.th_seq);
795 ti.th.th_ack = ntohl(ti.th.th_ack);
796 ti.ip6.ip6_nxt = IPPROTO_TCP;
797 if (ti.th.th_flags & TH_ACK) {
803 if (((*m)->m_flags & M_PKTHDR) != 0) {
804 ack += (*m)->m_pkthdr.len - off
805 - (ti.th.th_off << 2);
806 } else if (ip6->ip6_plen) {
807 ack += ntohs(ip6->ip6_plen) + sizeof(*ip6)
808 - off - (ti.th.th_off << 2);
814 if (tcp->th_flags & TH_SYN)
817 flags = TH_RST|TH_ACK;
819 bcopy(&ti, ip6, sizeof(ti));
820 tcp_respond(NULL, ip6, (struct tcphdr *)(ip6 + 1),
821 *m, ack, seq, flags);
826 default: /* Send an ICMP unreachable using code */
828 (*m)->m_pkthdr.rcvif = oif;
829 icmp6_error(*m, ICMP6_DST_UNREACH,
830 rule->fw_reject_code, 0);
838 * Finally, drop the packet.
848 add_entry6(struct ip6_fw_head *chainptr, struct ip6_fw *frwl)
850 struct ip6_fw *ftmp = 0;
851 struct ip6_fw_chain *fwc = 0, *fcp, *fcpl = 0;
855 fwc = malloc(sizeof *fwc, M_IP6FW, M_NOWAIT);
856 ftmp = malloc(sizeof *ftmp, M_IP6FW, M_NOWAIT);
858 dprintf(("%s malloc said no\n", err_prefix));
859 if (fwc) free(fwc, M_IP6FW);
860 if (ftmp) free(ftmp, M_IP6FW);
864 bcopy(frwl, ftmp, sizeof(struct ip6_fw));
865 ftmp->fw_in_if.fu_via_if.name[IP6FW_IFNLEN - 1] = '\0';
872 if (!chainptr->lh_first) {
873 LIST_INSERT_HEAD(chainptr, fwc, chain);
876 } else if (ftmp->fw_number == (u_short)-1) {
877 if (fwc) free(fwc, M_IP6FW);
878 if (ftmp) free(ftmp, M_IP6FW);
880 dprintf(("%s bad rule number\n", err_prefix));
884 /* If entry number is 0, find highest numbered rule and add 100 */
885 if (ftmp->fw_number == 0) {
886 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
887 if (fcp->rule->fw_number != (u_short)-1)
888 nbr = fcp->rule->fw_number;
892 if (nbr < (u_short)-1 - 100)
894 ftmp->fw_number = nbr;
897 /* Got a valid number; now insert it, keeping the list ordered */
898 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
899 if (fcp->rule->fw_number > ftmp->fw_number) {
901 LIST_INSERT_AFTER(fcpl, fwc, chain);
903 LIST_INSERT_HEAD(chainptr, fwc, chain);
916 del_entry6(struct ip6_fw_head *chainptr, u_short number)
918 struct ip6_fw_chain *fcp;
923 fcp = chainptr->lh_first;
924 if (number != (u_short)-1) {
925 for (; fcp; fcp = fcp->chain.le_next) {
926 if (fcp->rule->fw_number == number) {
927 LIST_REMOVE(fcp, chain);
929 free(fcp->rule, M_IP6FW);
941 zero_entry6(struct mbuf *m)
944 struct ip6_fw_chain *fcp;
947 if (m && m->m_len != 0) {
948 if (m->m_len != sizeof(struct ip6_fw))
950 frwl = mtod(m, struct ip6_fw *);
956 * It's possible to insert multiple chain entries with the
957 * same number, so we don't stop after finding the first
958 * match if zeroing a specific entry.
961 for (fcp = ip6_fw_chain.lh_first; fcp; fcp = fcp->chain.le_next)
962 if (!frwl || frwl->fw_number == fcp->rule->fw_number) {
963 fcp->rule->fw_bcnt = fcp->rule->fw_pcnt = 0;
964 fcp->rule->timestamp = 0;
970 log(LOG_SECURITY | LOG_NOTICE,
971 "ip6fw: Entry %d cleared.\n", frwl->fw_number);
973 log(LOG_SECURITY | LOG_NOTICE,
974 "ip6fw: Accounting cleared.\n");
980 static struct ip6_fw *
981 check_ip6fw_mbuf(struct mbuf *m)
984 if (m->m_len != sizeof(struct ip6_fw)) {
985 dprintf(("%s len=%d, want %zu\n", err_prefix, m->m_len,
986 sizeof(struct ip6_fw)));
989 return (check_ip6fw_struct(mtod(m, struct ip6_fw *)));
992 static struct ip6_fw *
993 check_ip6fw_struct(struct ip6_fw *frwl)
995 /* Check for invalid flag bits */
996 if ((frwl->fw_flg & ~IPV6_FW_F_MASK) != 0) {
997 dprintf(("%s undefined flag bits set (flags=%x)\n",
998 err_prefix, frwl->fw_flg));
1001 /* Must apply to incoming or outgoing (or both) */
1002 if (!(frwl->fw_flg & (IPV6_FW_F_IN | IPV6_FW_F_OUT))) {
1003 dprintf(("%s neither in nor out\n", err_prefix));
1006 /* Empty interface name is no good */
1007 if (((frwl->fw_flg & IPV6_FW_F_IIFNAME)
1008 && !*frwl->fw_in_if.fu_via_if.name)
1009 || ((frwl->fw_flg & IPV6_FW_F_OIFNAME)
1010 && !*frwl->fw_out_if.fu_via_if.name)) {
1011 dprintf(("%s empty interface name\n", err_prefix));
1014 /* Sanity check interface matching */
1015 if ((frwl->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
1016 ; /* allow "via" backwards compatibility */
1017 } else if ((frwl->fw_flg & IPV6_FW_F_IN)
1018 && (frwl->fw_flg & IPV6_FW_F_OIFACE)) {
1019 dprintf(("%s outgoing interface check on incoming\n",
1023 /* Sanity check port ranges */
1024 if ((frwl->fw_flg & IPV6_FW_F_SRNG) && IPV6_FW_GETNSRCP(frwl) < 2) {
1025 dprintf(("%s src range set but n_src_p=%d\n",
1026 err_prefix, IPV6_FW_GETNSRCP(frwl)));
1029 if ((frwl->fw_flg & IPV6_FW_F_DRNG) && IPV6_FW_GETNDSTP(frwl) < 2) {
1030 dprintf(("%s dst range set but n_dst_p=%d\n",
1031 err_prefix, IPV6_FW_GETNDSTP(frwl)));
1034 if (IPV6_FW_GETNSRCP(frwl) + IPV6_FW_GETNDSTP(frwl) > IPV6_FW_MAX_PORTS) {
1035 dprintf(("%s too many ports (%d+%d)\n",
1036 err_prefix, IPV6_FW_GETNSRCP(frwl), IPV6_FW_GETNDSTP(frwl)));
1040 * Protocols other than TCP/UDP don't use port range
1042 if ((frwl->fw_prot != IPPROTO_TCP) &&
1043 (frwl->fw_prot != IPPROTO_UDP) &&
1044 (IPV6_FW_GETNSRCP(frwl) || IPV6_FW_GETNDSTP(frwl))) {
1045 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1051 * Rather than modify the entry to make such entries work,
1052 * we reject this rule and require user level utilities
1053 * to enforce whatever policy they deem appropriate.
1055 if ((frwl->fw_src.s6_addr32[0] & (~frwl->fw_smsk.s6_addr32[0])) ||
1056 (frwl->fw_src.s6_addr32[1] & (~frwl->fw_smsk.s6_addr32[1])) ||
1057 (frwl->fw_src.s6_addr32[2] & (~frwl->fw_smsk.s6_addr32[2])) ||
1058 (frwl->fw_src.s6_addr32[3] & (~frwl->fw_smsk.s6_addr32[3])) ||
1059 (frwl->fw_dst.s6_addr32[0] & (~frwl->fw_dmsk.s6_addr32[0])) ||
1060 (frwl->fw_dst.s6_addr32[1] & (~frwl->fw_dmsk.s6_addr32[1])) ||
1061 (frwl->fw_dst.s6_addr32[2] & (~frwl->fw_dmsk.s6_addr32[2])) ||
1062 (frwl->fw_dst.s6_addr32[3] & (~frwl->fw_dmsk.s6_addr32[3]))) {
1063 dprintf(("%s rule never matches\n", err_prefix));
1067 if ((frwl->fw_flg & IPV6_FW_F_FRAG) &&
1068 (frwl->fw_prot == IPPROTO_UDP || frwl->fw_prot == IPPROTO_TCP)) {
1069 if (frwl->fw_nports) {
1070 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix));
1073 if (frwl->fw_prot == IPPROTO_TCP &&
1074 frwl->fw_tcpf != frwl->fw_tcpnf) {
1075 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix));
1080 /* Check command specific stuff */
1081 switch (frwl->fw_flg & IPV6_FW_F_COMMAND)
1083 case IPV6_FW_F_REJECT:
1084 if (frwl->fw_reject_code >= 0x100
1085 && !(frwl->fw_prot == IPPROTO_TCP
1086 && frwl->fw_reject_code == IPV6_FW_REJECT_RST)) {
1087 dprintf(("%s unknown reject code\n", err_prefix));
1091 case IPV6_FW_F_DIVERT: /* Diverting to port zero is invalid */
1093 if (frwl->fw_divert_port == 0) {
1094 dprintf(("%s can't divert to port 0\n", err_prefix));
1098 case IPV6_FW_F_DENY:
1099 case IPV6_FW_F_ACCEPT:
1100 case IPV6_FW_F_COUNT:
1101 case IPV6_FW_F_SKIPTO:
1104 dprintf(("%s invalid command\n", err_prefix));
1112 ip6_fw_ctl(int stage, struct mbuf **mm)
1117 if (stage == IPV6_FW_GET) {
1118 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1119 *mm = m = m_get(M_TRYWAIT, MT_DATA); /* XXX */
1122 if (sizeof *(fcp->rule) > MLEN) {
1123 MCLGET(m, M_TRYWAIT);
1124 if ((m->m_flags & M_EXT) == 0) {
1129 for (; fcp; fcp = fcp->chain.le_next) {
1130 bcopy(fcp->rule, m->m_data, sizeof *(fcp->rule));
1131 m->m_len = sizeof *(fcp->rule);
1132 m->m_next = m_get(M_TRYWAIT, MT_DATA); /* XXX */
1138 if (sizeof *(fcp->rule) > MLEN) {
1139 MCLGET(m, M_TRYWAIT);
1140 if ((m->m_flags & M_EXT) == 0) {
1150 /* only allow get calls if secure mode > 2 */
1151 if (securelevel > 2) {
1158 if (stage == IPV6_FW_FLUSH) {
1159 while (ip6_fw_chain.lh_first != NULL &&
1160 ip6_fw_chain.lh_first->rule->fw_number != (u_short)-1) {
1161 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1163 LIST_REMOVE(ip6_fw_chain.lh_first, chain);
1165 free(fcp->rule, M_IP6FW);
1174 if (stage == IPV6_FW_ZERO) {
1175 error = zero_entry6(m);
1183 printf("%s NULL mbuf ptr\n", err_prefix);
1187 if (stage == IPV6_FW_ADD) {
1188 struct ip6_fw *frwl = check_ip6fw_mbuf(m);
1193 error = add_entry6(&ip6_fw_chain, frwl);
1200 if (stage == IPV6_FW_DEL) {
1201 if (m->m_len != sizeof(struct ip6_fw)) {
1202 dprintf(("%s len=%d, want %zu\n", err_prefix, m->m_len,
1203 sizeof(struct ip6_fw)));
1205 } else if (mtod(m, struct ip6_fw *)->fw_number == (u_short)-1) {
1206 dprintf(("%s can't delete rule 65535\n", err_prefix));
1209 error = del_entry6(&ip6_fw_chain,
1210 mtod(m, struct ip6_fw *)->fw_number);
1218 dprintf(("%s unknown request %d\n", err_prefix, stage));
1229 struct ip6_fw default_rule;
1231 ip6_fw_chk_ptr = ip6_fw_chk;
1232 ip6_fw_ctl_ptr = ip6_fw_ctl;
1233 LIST_INIT(&ip6_fw_chain);
1235 bzero(&default_rule, sizeof default_rule);
1236 default_rule.fw_prot = IPPROTO_IPV6;
1237 default_rule.fw_number = (u_short)-1;
1238 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1239 default_rule.fw_flg |= IPV6_FW_F_ACCEPT;
1241 default_rule.fw_flg |= IPV6_FW_F_DENY;
1243 default_rule.fw_flg |= IPV6_FW_F_IN | IPV6_FW_F_OUT;
1244 if (check_ip6fw_struct(&default_rule) == NULL ||
1245 add_entry6(&ip6_fw_chain, &default_rule))
1248 printf("IPv6 packet filtering initialized, ");
1249 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1250 printf("default to accept, ");
1252 #ifndef IPV6FIREWALL_VERBOSE
1253 printf("logging disabled\n");
1255 if (fw6_verbose_limit == 0)
1256 printf("unlimited logging\n");
1258 printf("logging limited to %d packets/entry\n",
1263 static ip6_fw_chk_t *old_chk_ptr;
1264 static ip6_fw_ctl_t *old_ctl_ptr;
1267 ip6fw_modevent(module_t mod, int type, void *unused)
1275 old_chk_ptr = ip6_fw_chk_ptr;
1276 old_ctl_ptr = ip6_fw_ctl_ptr;
1283 ip6_fw_chk_ptr = old_chk_ptr;
1284 ip6_fw_ctl_ptr = old_ctl_ptr;
1285 while (LIST_FIRST(&ip6_fw_chain) != NULL) {
1286 struct ip6_fw_chain *fcp = LIST_FIRST(&ip6_fw_chain);
1287 LIST_REMOVE(LIST_FIRST(&ip6_fw_chain), chain);
1288 free(fcp->rule, M_IP6FW);
1293 printf("IPv6 firewall unloaded\n");
1302 static moduledata_t ip6fwmod = {
1307 DECLARE_MODULE(ip6fw, ip6fwmod, SI_SUB_PSEUDO, SI_ORDER_ANY);