1 /* $FreeBSD: src/sys/netinet6/ip6_fw.c,v 1.2.2.10 2003/08/03 17:52:54 ume Exp $ */
2 /* $DragonFly: src/sys/net/ip6fw/ip6_fw.c,v 1.17 2006/12/20 18:14:42 dillon 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>
75 #include <sys/thread2.h>
78 #include <net/route.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in.h>
81 #include <netinet/ip.h>
83 #include <netinet/ip6.h>
84 #include <netinet6/ip6_var.h>
85 #include <netinet6/in6_var.h>
86 #include <netinet/icmp6.h>
88 #include <netinet/in_pcb.h>
91 #include <netinet/ip_var.h>
92 #include <netinet/tcp.h>
93 #include <netinet/tcp_seq.h>
94 #include <netinet/tcp_timer.h>
95 #include <netinet/tcp_var.h>
96 #include <netinet/udp.h>
98 #include <sys/sysctl.h>
100 #include <net/net_osdep.h>
102 MALLOC_DEFINE(M_IP6FW, "Ip6Fw/Ip6Acct", "Ip6Fw/Ip6Acct chain's");
104 static int fw6_debug = 1;
105 #ifdef IPV6FIREWALL_VERBOSE
106 static int fw6_verbose = 1;
108 static int fw6_verbose = 0;
110 #ifdef IPV6FIREWALL_VERBOSE_LIMIT
111 static int fw6_verbose_limit = IPV6FIREWALL_VERBOSE_LIMIT;
113 static int fw6_verbose_limit = 0;
116 LIST_HEAD (ip6_fw_head, ip6_fw_chain) ip6_fw_chain;
119 SYSCTL_DECL(_net_inet6_ip6);
120 SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, fw, CTLFLAG_RW, 0, "Firewall");
121 SYSCTL_INT(_net_inet6_ip6_fw, OID_AUTO, enable, CTLFLAG_RW,
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, &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 (struct ip6_fw_head *chainptr, struct ip6_fw *frwl);
135 static int del_entry6 (struct ip6_fw_head *chainptr, u_short number);
136 static int zero_entry6 (struct mbuf *m);
137 static struct ip6_fw *check_ip6fw_struct (struct ip6_fw *m);
138 static struct ip6_fw *check_ip6fw_mbuf (struct mbuf *fw);
139 static int ip6opts_match (struct ip6_hdr **ip6, struct ip6_fw *f,
141 int *off, int *nxt, u_short *offset);
142 static int port_match6 (u_short *portptr, int nports, u_short port,
144 static int tcp6flg_match (struct tcphdr *tcp6, struct ip6_fw *f);
145 static int icmp6type_match (struct icmp6_hdr * icmp, struct ip6_fw * f);
146 static void ip6fw_report (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 (struct ip6_hdr **pip6,
150 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m);
151 static int ip6_fw_ctl (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 */
328 if (ifu->fu_via_if.glob) {
329 if (kfnmatch(ifu->fu_via_if.name, ifp->if_xname, 0)
333 if (strncmp(ifp->if_xname, ifu->fu_via_if.name,
338 } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu->fu_via_ip6)) { /* Zero == wildcard */
341 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
342 if (ifa->ifa_addr == NULL)
344 if (ifa->ifa_addr->sa_family != AF_INET6)
346 if (!IN6_ARE_ADDR_EQUAL(&ifu->fu_via_ip6,
347 &(((struct sockaddr_in6 *)
348 (ifa->ifa_addr))->sin6_addr)))
358 ip6fw_report(struct ip6_fw *f, struct ip6_hdr *ip6,
359 struct ifnet *rif, struct ifnet *oif, int off, int nxt)
362 struct tcphdr *const tcp6 = (struct tcphdr *) ((caddr_t) ip6+ off);
363 struct udphdr *const udp = (struct udphdr *) ((caddr_t) ip6+ off);
364 struct icmp6_hdr *const icmp6 = (struct icmp6_hdr *) ((caddr_t) ip6+ off);
367 char action2[32], proto[102], name[18];
370 count = f ? f->fw_pcnt : ++counter;
371 if (fw6_verbose_limit != 0 && count > fw6_verbose_limit)
374 /* Print command name */
375 ksnprintf(SNPARGS(name, 0), "ip6fw: %d", f ? f->fw_number : -1);
381 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
385 case IPV6_FW_F_REJECT:
386 if (f->fw_reject_code == IPV6_FW_REJECT_RST)
391 case IPV6_FW_F_ACCEPT:
394 case IPV6_FW_F_COUNT:
397 case IPV6_FW_F_DIVERT:
398 ksnprintf(SNPARGS(action2, 0), "Divert %d",
402 ksnprintf(SNPARGS(action2, 0), "Tee %d",
405 case IPV6_FW_F_SKIPTO:
406 ksnprintf(SNPARGS(action2, 0), "SkipTo %d",
417 len = ksnprintf(SNPARGS(proto, 0), "TCP [%s]",
418 ip6_sprintf(&ip6->ip6_src));
420 len += ksnprintf(SNPARGS(proto, len), ":%d ",
421 ntohs(tcp6->th_sport));
423 len += ksnprintf(SNPARGS(proto, len), " ");
424 len += ksnprintf(SNPARGS(proto, len), "[%s]",
425 ip6_sprintf(&ip6->ip6_dst));
427 ksnprintf(SNPARGS(proto, len), ":%d",
428 ntohs(tcp6->th_dport));
431 len = ksnprintf(SNPARGS(proto, 0), "UDP [%s]",
432 ip6_sprintf(&ip6->ip6_src));
434 len += ksnprintf(SNPARGS(proto, len), ":%d ",
435 ntohs(udp->uh_sport));
437 len += ksnprintf(SNPARGS(proto, len), " ");
438 len += ksnprintf(SNPARGS(proto, len), "[%s]",
439 ip6_sprintf(&ip6->ip6_dst));
441 ksnprintf(SNPARGS(proto, len), ":%d",
442 ntohs(udp->uh_dport));
446 len = ksnprintf(SNPARGS(proto, 0), "IPV6-ICMP:%u.%u ",
447 icmp6->icmp6_type, icmp6->icmp6_code);
449 len = ksnprintf(SNPARGS(proto, 0), "IPV6-ICMP ");
450 len += ksnprintf(SNPARGS(proto, len), "[%s]",
451 ip6_sprintf(&ip6->ip6_src));
452 ksnprintf(SNPARGS(proto, len), " [%s]",
453 ip6_sprintf(&ip6->ip6_dst));
456 len = ksnprintf(SNPARGS(proto, 0), "P:%d [%s]", nxt,
457 ip6_sprintf(&ip6->ip6_src));
458 ksnprintf(SNPARGS(proto, len), " [%s]",
459 ip6_sprintf(&ip6->ip6_dst));
464 log(LOG_SECURITY | LOG_INFO, "%s %s %s out via %s\n",
465 name, action, proto, if_name(oif));
467 log(LOG_SECURITY | LOG_INFO, "%s %s %s in via %s\n",
468 name, action, proto, if_name(rif));
470 log(LOG_SECURITY | LOG_INFO, "%s %s %s",
471 name, action, proto);
472 if (fw6_verbose_limit != 0 && count == fw6_verbose_limit)
473 log(LOG_SECURITY | LOG_INFO, "ip6fw: limit reached on entry %d\n",
474 f ? f->fw_number : -1);
480 * ip Pointer to packet header (struct ip6_hdr *)
481 * hlen Packet header length
482 * oif Outgoing interface, or NULL if packet is incoming
483 * #ifndef IP6FW_DIVERT_RESTART
484 * *cookie Ignore all divert/tee rules to this port (if non-zero)
486 * *cookie Skip up to the first rule past this rule number;
488 * *m The packet; we set to NULL when/if we nuke it.
492 * 0 The packet is to be accepted and routed normally OR
493 * the packet was denied/rejected and has been dropped;
494 * in the latter case, *m is equal to NULL upon return.
495 * port Divert the packet to port.
499 ip6_fw_chk(struct ip6_hdr **pip6,
500 struct ifnet *oif, u_int16_t *cookie, struct mbuf **m)
502 struct ip6_fw_chain *chain;
503 struct ip6_fw *rule = NULL;
504 struct ip6_hdr *ip6 = *pip6;
505 struct ifnet *const rif = (*m)->m_pkthdr.rcvif;
507 int off = sizeof(struct ip6_hdr), nxt = ip6->ip6_nxt;
508 u_short src_port, dst_port;
509 #ifdef IP6FW_DIVERT_RESTART
510 u_int16_t skipto = *cookie;
512 u_int16_t ignport = ntohs(*cookie);
517 * Go down the chain, looking for enlightment
518 * #ifdef IP6FW_DIVERT_RESTART
519 * If we've been asked to start at a given rule immediatly, do so.
522 chain = LIST_FIRST(&ip6_fw_chain);
523 #ifdef IP6FW_DIVERT_RESTART
527 while (chain && (chain->rule->fw_number <= skipto)) {
528 chain = LIST_NEXT(chain, chain);
530 if (! chain) goto dropit;
532 #endif /* IP6FW_DIVERT_RESTART */
533 for (; chain; chain = LIST_NEXT(chain, chain)) {
534 struct ip6_fw *const f = chain->rule;
537 /* Check direction outbound */
538 if (!(f->fw_flg & IPV6_FW_F_OUT))
541 /* Check direction inbound */
542 if (!(f->fw_flg & IPV6_FW_F_IN))
546 #define IN6_ARE_ADDR_MASKEQUAL(x,y,z) (\
547 (((x)->s6_addr32[0] & (y)->s6_addr32[0]) == (z)->s6_addr32[0]) && \
548 (((x)->s6_addr32[1] & (y)->s6_addr32[1]) == (z)->s6_addr32[1]) && \
549 (((x)->s6_addr32[2] & (y)->s6_addr32[2]) == (z)->s6_addr32[2]) && \
550 (((x)->s6_addr32[3] & (y)->s6_addr32[3]) == (z)->s6_addr32[3]))
552 /* If src-addr doesn't match, not this rule. */
553 if (((f->fw_flg & IPV6_FW_F_INVSRC) != 0) ^
554 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_src,&f->fw_smsk,&f->fw_src)))
557 /* If dest-addr doesn't match, not this rule. */
558 if (((f->fw_flg & IPV6_FW_F_INVDST) != 0) ^
559 (!IN6_ARE_ADDR_MASKEQUAL(&ip6->ip6_dst,&f->fw_dmsk,&f->fw_dst)))
562 #undef IN6_ARE_ADDR_MASKEQUAL
563 /* Interface check */
564 if ((f->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
565 struct ifnet *const iface = oif ? oif : rif;
567 /* Backwards compatibility hack for "via" */
568 if (!iface || !iface_match(iface,
569 &f->fw_in_if, f->fw_flg & IPV6_FW_F_OIFNAME))
572 /* Check receive interface */
573 if ((f->fw_flg & IPV6_FW_F_IIFACE)
574 && (!rif || !iface_match(rif,
575 &f->fw_in_if, f->fw_flg & IPV6_FW_F_IIFNAME)))
577 /* Check outgoing interface */
578 if ((f->fw_flg & IPV6_FW_F_OIFACE)
579 && (!oif || !iface_match(oif,
580 &f->fw_out_if, f->fw_flg & IPV6_FW_F_OIFNAME)))
584 /* Check IP options */
585 if (!ip6opts_match(&ip6, f, m, &off, &nxt, &offset))
589 if ((f->fw_flg & IPV6_FW_F_FRAG) && !offset)
592 /* Check protocol; if wildcard, match */
593 if (f->fw_prot == IPPROTO_IPV6)
596 /* If different, don't match */
597 if (nxt != f->fw_prot)
600 #define PULLUP_TO(len) do { \
601 if ((*m)->m_len < (len) \
602 && (*m = m_pullup(*m, (len))) == 0) { \
605 *pip6 = ip6 = mtod(*m, struct ip6_hdr *); \
608 /* Protocol specific checks */
614 if (offset == 1) { /* cf. RFC 1858 */
615 PULLUP_TO(off + 4); /* XXX ? */
620 * TCP flags and ports aren't available in this
621 * packet -- if this rule specified either one,
622 * we consider the rule a non-match.
624 if (f->fw_nports != 0 ||
625 f->fw_tcpf != f->fw_tcpnf)
631 tcp6 = (struct tcphdr *) ((caddr_t)ip6 + off);
632 if (((f->fw_tcpf != f->fw_tcpnf) ||
633 (f->fw_ipflg & IPV6_FW_IF_TCPEST)) &&
634 !tcp6flg_match(tcp6, f))
636 src_port = ntohs(tcp6->th_sport);
637 dst_port = ntohs(tcp6->th_dport);
647 * Port specification is unavailable -- if this
648 * rule specifies a port, we consider the rule
651 if (f->fw_nports != 0)
657 udp = (struct udphdr *) ((caddr_t)ip6 + off);
658 src_port = ntohs(udp->uh_sport);
659 dst_port = ntohs(udp->uh_dport);
661 if (!port_match6(&f->fw_pts[0],
662 IPV6_FW_GETNSRCP(f), src_port,
663 f->fw_flg & IPV6_FW_F_SRNG))
665 if (!port_match6(&f->fw_pts[IPV6_FW_GETNSRCP(f)],
666 IPV6_FW_GETNDSTP(f), dst_port,
667 f->fw_flg & IPV6_FW_F_DRNG))
674 struct icmp6_hdr *icmp;
676 if (offset != 0) /* Type isn't valid */
679 icmp = (struct icmp6_hdr *) ((caddr_t)ip6 + off);
680 if (!icmp6type_match(icmp, f))
688 ip6fw_report(NULL, ip6, rif, oif, off, nxt);
693 #ifndef IP6FW_DIVERT_RESTART
694 /* Ignore divert/tee rule if socket port is "ignport" */
695 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
696 case IPV6_FW_F_DIVERT:
698 if (f->fw_divert_port == ignport)
699 continue; /* ignore this rule */
703 #endif /* IP6FW_DIVERT_RESTART */
704 /* Update statistics */
706 f->fw_bcnt += ntohs(ip6->ip6_plen);
707 f->timestamp = time_second;
709 /* Log to console if desired */
710 if ((f->fw_flg & IPV6_FW_F_PRN) && fw6_verbose)
711 ip6fw_report(f, ip6, rif, oif, off, nxt);
713 /* Take appropriate action */
714 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
715 case IPV6_FW_F_ACCEPT:
717 case IPV6_FW_F_COUNT:
719 case IPV6_FW_F_DIVERT:
720 #ifdef IP6FW_DIVERT_RESTART
721 *cookie = f->fw_number;
723 *cookie = htons(f->fw_divert_port);
724 #endif /* IP6FW_DIVERT_RESTART */
725 return(f->fw_divert_port);
728 * XXX someday tee packet here, but beware that you
729 * can't use m_copym() or m_copypacket() because
730 * the divert input routine modifies the mbuf
731 * (and these routines only increment reference
732 * counts in the case of mbuf clusters), so need
733 * to write custom routine.
736 case IPV6_FW_F_SKIPTO:
738 while (chain->chain.le_next
739 && chain->chain.le_next->rule->fw_number
742 while (chain->chain.le_next->rule->fw_number
745 chain = chain->chain.le_next;
749 /* Deny/reject this packet using this rule */
755 /* Rule 65535 should always be there and should always match */
757 panic("ip6_fw: chain");
761 * At this point, we're going to drop the packet.
762 * Send a reject notice if all of the following are true:
764 * - The packet matched a reject rule
765 * - The packet is not an ICMP packet, or is an ICMP query packet
766 * - The packet is not a multicast or broadcast packet
768 if ((rule->fw_flg & IPV6_FW_F_COMMAND) == IPV6_FW_F_REJECT
769 && (nxt != IPPROTO_ICMPV6 || is_icmp6_query(ip6, off))
770 && !((*m)->m_flags & (M_BCAST|M_MCAST))
771 && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
772 switch (rule->fw_reject_code) {
773 case IPV6_FW_REJECT_RST:
775 struct tcphdr *const tcp =
776 (struct tcphdr *) ((caddr_t)ip6 + off);
784 if (offset != 0 || (tcp->th_flags & TH_RST))
789 ti.th.th_seq = ntohl(ti.th.th_seq);
790 ti.th.th_ack = ntohl(ti.th.th_ack);
791 ti.ip6.ip6_nxt = IPPROTO_TCP;
792 if (ti.th.th_flags & TH_ACK) {
798 if (((*m)->m_flags & M_PKTHDR) != 0) {
799 ack += (*m)->m_pkthdr.len - off
800 - (ti.th.th_off << 2);
801 } else if (ip6->ip6_plen) {
802 ack += ntohs(ip6->ip6_plen) + sizeof(*ip6)
803 - off - (ti.th.th_off << 2);
810 flags = TH_RST|TH_ACK;
812 bcopy(&ti, ip6, sizeof(ti));
813 tcp_respond(NULL, ip6, (struct tcphdr *)(ip6 + 1),
814 *m, ack, seq, flags);
818 default: /* Send an ICMP unreachable using code */
820 (*m)->m_pkthdr.rcvif = oif;
821 icmp6_error(*m, ICMP6_DST_UNREACH,
822 rule->fw_reject_code, 0);
830 * Finally, drop the packet.
840 add_entry6(struct ip6_fw_head *chainptr, struct ip6_fw *frwl)
842 struct ip6_fw *ftmp = 0;
843 struct ip6_fw_chain *fwc = 0, *fcp, *fcpl = 0;
846 fwc = kmalloc(sizeof *fwc, M_IP6FW, M_INTWAIT);
847 ftmp = kmalloc(sizeof *ftmp, M_IP6FW, M_INTWAIT);
849 bcopy(frwl, ftmp, sizeof(struct ip6_fw));
850 ftmp->fw_in_if.fu_via_if.name[IP6FW_IFNLEN - 1] = '\0';
857 if (!chainptr->lh_first) {
858 LIST_INSERT_HEAD(chainptr, fwc, chain);
861 } else if (ftmp->fw_number == (u_short)-1) {
862 if (fwc) kfree(fwc, M_IP6FW);
863 if (ftmp) kfree(ftmp, M_IP6FW);
865 dprintf(("%s bad rule number\n", err_prefix));
869 /* If entry number is 0, find highest numbered rule and add 100 */
870 if (ftmp->fw_number == 0) {
871 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
872 if (fcp->rule->fw_number != (u_short)-1)
873 nbr = fcp->rule->fw_number;
877 if (nbr < (u_short)-1 - 100)
879 ftmp->fw_number = nbr;
882 /* Got a valid number; now insert it, keeping the list ordered */
883 for (fcp = chainptr->lh_first; fcp; fcp = fcp->chain.le_next) {
884 if (fcp->rule->fw_number > ftmp->fw_number) {
886 LIST_INSERT_AFTER(fcpl, fwc, chain);
888 LIST_INSERT_HEAD(chainptr, fwc, chain);
901 del_entry6(struct ip6_fw_head *chainptr, u_short number)
903 struct ip6_fw_chain *fcp;
907 fcp = chainptr->lh_first;
908 if (number != (u_short)-1) {
909 for (; fcp; fcp = fcp->chain.le_next) {
910 if (fcp->rule->fw_number == number) {
911 LIST_REMOVE(fcp, chain);
913 kfree(fcp->rule, M_IP6FW);
925 zero_entry6(struct mbuf *m)
928 struct ip6_fw_chain *fcp;
930 if (m && m->m_len != 0) {
931 if (m->m_len != sizeof(struct ip6_fw))
933 frwl = mtod(m, struct ip6_fw *);
939 * It's possible to insert multiple chain entries with the
940 * same number, so we don't stop after finding the first
941 * match if zeroing a specific entry.
944 for (fcp = ip6_fw_chain.lh_first; fcp; fcp = fcp->chain.le_next)
945 if (!frwl || frwl->fw_number == fcp->rule->fw_number) {
946 fcp->rule->fw_bcnt = fcp->rule->fw_pcnt = 0;
947 fcp->rule->timestamp = 0;
953 log(LOG_SECURITY | LOG_NOTICE,
954 "ip6fw: Entry %d cleared.\n", frwl->fw_number);
956 log(LOG_SECURITY | LOG_NOTICE,
957 "ip6fw: Accounting cleared.\n");
963 static struct ip6_fw *
964 check_ip6fw_mbuf(struct mbuf *m)
967 if (m->m_len != sizeof(struct ip6_fw)) {
968 dprintf(("%s len=%d, want %d\n", err_prefix, m->m_len,
969 sizeof(struct ip6_fw)));
972 return(check_ip6fw_struct(mtod(m, struct ip6_fw *)));
975 static struct ip6_fw *
976 check_ip6fw_struct(struct ip6_fw *frwl)
978 /* Check for invalid flag bits */
979 if ((frwl->fw_flg & ~IPV6_FW_F_MASK) != 0) {
980 dprintf(("%s undefined flag bits set (flags=%x)\n",
981 err_prefix, frwl->fw_flg));
984 /* Must apply to incoming or outgoing (or both) */
985 if (!(frwl->fw_flg & (IPV6_FW_F_IN | IPV6_FW_F_OUT))) {
986 dprintf(("%s neither in nor out\n", err_prefix));
989 /* Empty interface name is no good */
990 if (((frwl->fw_flg & IPV6_FW_F_IIFNAME)
991 && !*frwl->fw_in_if.fu_via_if.name)
992 || ((frwl->fw_flg & IPV6_FW_F_OIFNAME)
993 && !*frwl->fw_out_if.fu_via_if.name)) {
994 dprintf(("%s empty interface name\n", err_prefix));
997 /* Sanity check interface matching */
998 if ((frwl->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
999 ; /* allow "via" backwards compatibility */
1000 } else if ((frwl->fw_flg & IPV6_FW_F_IN)
1001 && (frwl->fw_flg & IPV6_FW_F_OIFACE)) {
1002 dprintf(("%s outgoing interface check on incoming\n",
1006 /* Sanity check port ranges */
1007 if ((frwl->fw_flg & IPV6_FW_F_SRNG) && IPV6_FW_GETNSRCP(frwl) < 2) {
1008 dprintf(("%s src range set but n_src_p=%d\n",
1009 err_prefix, IPV6_FW_GETNSRCP(frwl)));
1012 if ((frwl->fw_flg & IPV6_FW_F_DRNG) && IPV6_FW_GETNDSTP(frwl) < 2) {
1013 dprintf(("%s dst range set but n_dst_p=%d\n",
1014 err_prefix, IPV6_FW_GETNDSTP(frwl)));
1017 if (IPV6_FW_GETNSRCP(frwl) + IPV6_FW_GETNDSTP(frwl) > IPV6_FW_MAX_PORTS) {
1018 dprintf(("%s too many ports (%d+%d)\n",
1019 err_prefix, IPV6_FW_GETNSRCP(frwl), IPV6_FW_GETNDSTP(frwl)));
1023 * Protocols other than TCP/UDP don't use port range
1025 if ((frwl->fw_prot != IPPROTO_TCP) &&
1026 (frwl->fw_prot != IPPROTO_UDP) &&
1027 (IPV6_FW_GETNSRCP(frwl) || IPV6_FW_GETNDSTP(frwl))) {
1028 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1034 * Rather than modify the entry to make such entries work,
1035 * we reject this rule and require user level utilities
1036 * to enforce whatever policy they deem appropriate.
1038 if ((frwl->fw_src.s6_addr32[0] & (~frwl->fw_smsk.s6_addr32[0])) ||
1039 (frwl->fw_src.s6_addr32[1] & (~frwl->fw_smsk.s6_addr32[1])) ||
1040 (frwl->fw_src.s6_addr32[2] & (~frwl->fw_smsk.s6_addr32[2])) ||
1041 (frwl->fw_src.s6_addr32[3] & (~frwl->fw_smsk.s6_addr32[3])) ||
1042 (frwl->fw_dst.s6_addr32[0] & (~frwl->fw_dmsk.s6_addr32[0])) ||
1043 (frwl->fw_dst.s6_addr32[1] & (~frwl->fw_dmsk.s6_addr32[1])) ||
1044 (frwl->fw_dst.s6_addr32[2] & (~frwl->fw_dmsk.s6_addr32[2])) ||
1045 (frwl->fw_dst.s6_addr32[3] & (~frwl->fw_dmsk.s6_addr32[3]))) {
1046 dprintf(("%s rule never matches\n", err_prefix));
1050 if ((frwl->fw_flg & IPV6_FW_F_FRAG) &&
1051 (frwl->fw_prot == IPPROTO_UDP || frwl->fw_prot == IPPROTO_TCP)) {
1052 if (frwl->fw_nports) {
1053 dprintf(("%s cannot mix 'frag' and ports\n", err_prefix));
1056 if (frwl->fw_prot == IPPROTO_TCP &&
1057 frwl->fw_tcpf != frwl->fw_tcpnf) {
1058 dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix));
1063 /* Check command specific stuff */
1064 switch (frwl->fw_flg & IPV6_FW_F_COMMAND)
1066 case IPV6_FW_F_REJECT:
1067 if (frwl->fw_reject_code >= 0x100
1068 && !(frwl->fw_prot == IPPROTO_TCP
1069 && frwl->fw_reject_code == IPV6_FW_REJECT_RST)) {
1070 dprintf(("%s unknown reject code\n", err_prefix));
1074 case IPV6_FW_F_DIVERT: /* Diverting to port zero is invalid */
1076 if (frwl->fw_divert_port == 0) {
1077 dprintf(("%s can't divert to port 0\n", err_prefix));
1081 case IPV6_FW_F_DENY:
1082 case IPV6_FW_F_ACCEPT:
1083 case IPV6_FW_F_COUNT:
1084 case IPV6_FW_F_SKIPTO:
1087 dprintf(("%s invalid command\n", err_prefix));
1095 ip6_fw_ctl(int stage, struct mbuf **mm)
1100 if (stage == IPV6_FW_GET) {
1101 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1102 *mm = m = m_get(MB_WAIT, MT_DATA); /* XXX */
1105 if (sizeof *(fcp->rule) > MLEN) {
1107 if ((m->m_flags & M_EXT) == 0) {
1112 for (; fcp; fcp = fcp->chain.le_next) {
1113 bcopy(fcp->rule, m->m_data, sizeof *(fcp->rule));
1114 m->m_len = sizeof *(fcp->rule);
1115 m->m_next = m_get(MB_WAIT, MT_DATA); /* XXX */
1121 if (sizeof *(fcp->rule) > MLEN) {
1123 if ((m->m_flags & M_EXT) == 0) {
1133 /* only allow get calls if secure mode > 2 */
1134 if (securelevel > 2) {
1141 if (stage == IPV6_FW_FLUSH) {
1142 while (ip6_fw_chain.lh_first != NULL &&
1143 ip6_fw_chain.lh_first->rule->fw_number != (u_short)-1) {
1144 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1146 LIST_REMOVE(ip6_fw_chain.lh_first, chain);
1148 kfree(fcp->rule, M_IP6FW);
1149 kfree(fcp, M_IP6FW);
1157 if (stage == IPV6_FW_ZERO) {
1158 error = zero_entry6(m);
1166 printf("%s NULL mbuf ptr\n", err_prefix);
1170 if (stage == IPV6_FW_ADD) {
1171 struct ip6_fw *frwl = check_ip6fw_mbuf(m);
1176 error = add_entry6(&ip6_fw_chain, frwl);
1183 if (stage == IPV6_FW_DEL) {
1184 if (m->m_len != sizeof(struct ip6_fw)) {
1185 dprintf(("%s len=%d, want %d\n", err_prefix, m->m_len,
1186 sizeof(struct ip6_fw)));
1188 } else if (mtod(m, struct ip6_fw *)->fw_number == (u_short)-1) {
1189 dprintf(("%s can't delete rule 65535\n", err_prefix));
1192 error = del_entry6(&ip6_fw_chain,
1193 mtod(m, struct ip6_fw *)->fw_number);
1201 dprintf(("%s unknown request %d\n", err_prefix, stage));
1212 struct ip6_fw default_rule;
1214 ip6_fw_chk_ptr = ip6_fw_chk;
1215 ip6_fw_ctl_ptr = ip6_fw_ctl;
1216 LIST_INIT(&ip6_fw_chain);
1218 bzero(&default_rule, sizeof default_rule);
1219 default_rule.fw_prot = IPPROTO_IPV6;
1220 default_rule.fw_number = (u_short)-1;
1221 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1222 default_rule.fw_flg |= IPV6_FW_F_ACCEPT;
1224 default_rule.fw_flg |= IPV6_FW_F_DENY;
1226 default_rule.fw_flg |= IPV6_FW_F_IN | IPV6_FW_F_OUT;
1227 if (check_ip6fw_struct(&default_rule) == NULL ||
1228 add_entry6(&ip6_fw_chain, &default_rule))
1231 printf("IPv6 packet filtering initialized, ");
1232 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1233 printf("default to accept, ");
1235 #ifndef IPV6FIREWALL_VERBOSE
1236 printf("logging disabled\n");
1238 if (fw6_verbose_limit == 0)
1239 printf("unlimited logging\n");
1241 printf("logging limited to %d packets/entry\n",
1246 static ip6_fw_chk_t *old_chk_ptr;
1247 static ip6_fw_ctl_t *old_ctl_ptr;
1250 ip6fw_modevent(module_t mod, int type, void *unused)
1256 old_chk_ptr = ip6_fw_chk_ptr;
1257 old_ctl_ptr = ip6_fw_ctl_ptr;
1264 ip6_fw_chk_ptr = old_chk_ptr;
1265 ip6_fw_ctl_ptr = old_ctl_ptr;
1266 while (LIST_FIRST(&ip6_fw_chain) != NULL) {
1267 struct ip6_fw_chain *fcp = LIST_FIRST(&ip6_fw_chain);
1268 LIST_REMOVE(LIST_FIRST(&ip6_fw_chain), chain);
1269 kfree(fcp->rule, M_IP6FW);
1270 kfree(fcp, M_IP6FW);
1274 printf("IPv6 firewall unloaded\n");
1282 static moduledata_t ip6fwmod = {
1287 DECLARE_MODULE(ip6fw, ip6fwmod, SI_SUB_PSEUDO, SI_ORDER_ANY);