gdb - Local mods (compile)
[dragonfly.git] / sys / net / ip6fw / ip6_fw.c
1 /*      $FreeBSD: src/sys/netinet6/ip6_fw.c,v 1.2.2.10 2003/08/03 17:52:54 ume Exp $    */
2 /*      $KAME: ip6_fw.c,v 1.21 2001/01/24 01:25:32 itojun Exp $ */
3
4 /*
5  * Copyright (C) 1998, 1999, 2000 and 2001 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * Copyright (c) 1993 Daniel Boulet
35  * Copyright (c) 1994 Ugen J.S.Antsilevich
36  * Copyright (c) 1996 Alex Nash
37  *
38  * Redistribution and use in source forms, with and without modification,
39  * are permitted provided that this entire comment appears intact.
40  *
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.
44  *
45  * This software is provided ``AS IS'' without any warranties of any kind.
46  */
47
48 /*
49  * Implement IPv6 packet firewall
50  */
51
52 #if !defined(KLD_MODULE)
53 #include "opt_ip6fw.h"
54 #include "opt_inet.h"
55 #include "opt_inet6.h"
56 #endif
57
58 #ifdef IP6DIVERT
59 #error "NOT SUPPORTED IPV6 DIVERT"
60 #endif
61 #ifdef IP6FW_DIVERT_RESTART
62 #error "NOT SUPPORTED IPV6 DIVERT"
63 #endif
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/malloc.h>
68 #include <sys/mbuf.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>
74 #include <sys/thread2.h>
75 #include <sys/time.h>
76 #include <net/if.h>
77 #include <net/route.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/in.h>
80 #include <netinet/ip.h>
81
82 #include <netinet/ip6.h>
83 #include <netinet6/ip6_var.h>
84 #include <netinet6/in6_var.h>
85 #include <netinet/icmp6.h>
86
87 #include <netinet/in_pcb.h>
88
89 #include "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>
96
97 #include <sys/sysctl.h>
98
99 #include <net/net_osdep.h>
100
101 MALLOC_DEFINE(M_IP6FW, "Ip6Fw/Ip6Acct", "Ip6Fw/Ip6Acct chain's");
102
103 static int fw6_debug = 1;
104 #ifdef IPV6FIREWALL_VERBOSE
105 static int fw6_verbose = 1;
106 #else
107 static int fw6_verbose = 0;
108 #endif
109 #ifdef IPV6FIREWALL_VERBOSE_LIMIT
110 static int fw6_verbose_limit = IPV6FIREWALL_VERBOSE_LIMIT;
111 #else
112 static int fw6_verbose_limit = 0;
113 #endif
114
115 LIST_HEAD (ip6_fw_head, ip6_fw_chain) ip6_fw_chain;
116
117 #ifdef SYSCTL_NODE
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, "");
125 #endif
126
127 #define dprintf(a)      do {                                            \
128                                 if (fw6_debug)                          \
129                                         kprintf a;                      \
130                         } while (0)
131 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
132
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,
139                                    struct mbuf **m,
140                                    int *off, int *nxt, u_short *offset);
141 static int      port_match6 (u_short *portptr, int nports, u_short port,
142                                 int range_flag);
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);
147
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);
151
152 static char err_prefix[] = "ip6_fw_ctl:";
153
154 /*
155  * Returns 1 if the port is matched by the vector, 0 otherwise
156  */
157 static __inline int
158 port_match6(u_short *portptr, int nports, u_short port, int range_flag)
159 {
160         if (!nports)
161                 return 1;
162         if (range_flag) {
163                 if (portptr[0] <= port && port <= portptr[1]) {
164                         return 1;
165                 }
166                 nports -= 2;
167                 portptr += 2;
168         }
169         while (nports-- > 0) {
170                 if (*portptr++ == port) {
171                         return 1;
172                 }
173         }
174         return 0;
175 }
176
177 static int
178 tcp6flg_match(struct tcphdr *tcp6, struct ip6_fw *f)
179 {
180         u_char          flg_set, flg_clr;
181
182         /*
183          * If an established connection is required, reject packets that
184          * have only SYN of RST|ACK|SYN set.  Otherwise, fall through to
185          * other flag requirements.
186          */
187         if ((f->fw_ipflg & IPV6_FW_IF_TCPEST) &&
188             ((tcp6->th_flags & (IPV6_FW_TCPF_RST | IPV6_FW_TCPF_ACK |
189             IPV6_FW_TCPF_SYN)) == IPV6_FW_TCPF_SYN))
190                 return 0;
191
192         flg_set = tcp6->th_flags & f->fw_tcpf;
193         flg_clr = tcp6->th_flags & f->fw_tcpnf;
194
195         if (flg_set != f->fw_tcpf)
196                 return 0;
197         if (flg_clr)
198                 return 0;
199
200         return 1;
201 }
202
203 static int
204 icmp6type_match(struct icmp6_hdr *icmp6, struct ip6_fw *f)
205 {
206         int type;
207
208         if (!(f->fw_flg & IPV6_FW_F_ICMPBIT))
209                 return(1);
210
211         type = icmp6->icmp6_type;
212
213         /* check for matching type in the bitmap */
214         if (type < IPV6_FW_ICMPTYPES_DIM * sizeof(unsigned) * 8 &&
215                 (f->fw_icmp6types[type / (sizeof(unsigned) * 8)] &
216                 (1U << (type % (8 * sizeof(unsigned))))))
217                 return(1);
218
219         return(0); /* no match */
220 }
221
222 static int
223 is_icmp6_query(struct ip6_hdr *ip6, int off)
224 {
225         const struct icmp6_hdr *icmp6;
226         int icmp6_type;
227
228         icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off);
229         icmp6_type = icmp6->icmp6_type;
230
231         if (icmp6_type == ICMP6_ECHO_REQUEST ||
232             icmp6_type == ICMP6_MEMBERSHIP_QUERY ||
233             icmp6_type == ICMP6_WRUREQUEST ||
234             icmp6_type == ICMP6_FQDN_QUERY ||
235             icmp6_type == ICMP6_NI_QUERY)
236                 return(1);
237
238         return(0);
239 }
240
241 static int
242 ip6opts_match(struct ip6_hdr **pip6, struct ip6_fw *f, struct mbuf **m,
243               int *off, int *nxt, u_short *offset)
244 {
245         int len;
246         struct ip6_hdr *ip6 = *pip6;
247         struct ip6_ext *ip6e;
248         u_char  opts, nopts, nopts_sve;
249
250         opts = f->fw_ip6opt;
251         nopts = nopts_sve = f->fw_ip6nopt;
252
253         *nxt = ip6->ip6_nxt;
254         *off = sizeof(struct ip6_hdr);
255         len = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr);
256         while (*off < len) {
257                 ip6e = (struct ip6_ext *)((caddr_t) ip6 + *off);
258                 if ((*m)->m_len < *off + sizeof(*ip6e))
259                         goto opts_check;        /* XXX */
260
261                 switch (*nxt) {
262                 case IPPROTO_FRAGMENT:
263                         if ((*m)->m_len >= *off + sizeof(struct ip6_frag)) {
264                                 struct ip6_frag *ip6f;
265
266                                 ip6f = (struct ip6_frag *) ((caddr_t)ip6 + *off);
267                                 *offset = ip6f->ip6f_offlg & IP6F_OFF_MASK;
268                         }
269                         opts &= ~IPV6_FW_IP6OPT_FRAG;
270                         nopts &= ~IPV6_FW_IP6OPT_FRAG;
271                         *off += sizeof(struct ip6_frag);
272                         break;
273                 case IPPROTO_AH:
274                         opts &= ~IPV6_FW_IP6OPT_AH;
275                         nopts &= ~IPV6_FW_IP6OPT_AH;
276                         *off += (ip6e->ip6e_len + 2) << 2;
277                         break;
278                 default:
279                         switch (*nxt) {
280                         case IPPROTO_HOPOPTS:
281                                 opts &= ~IPV6_FW_IP6OPT_HOPOPT;
282                                 nopts &= ~IPV6_FW_IP6OPT_HOPOPT;
283                                 break;
284                         case IPPROTO_ROUTING:
285                                 opts &= ~IPV6_FW_IP6OPT_ROUTE;
286                                 nopts &= ~IPV6_FW_IP6OPT_ROUTE;
287                                 break;
288                         case IPPROTO_ESP:
289                                 opts &= ~IPV6_FW_IP6OPT_ESP;
290                                 nopts &= ~IPV6_FW_IP6OPT_ESP;
291                                 goto opts_check;
292                         case IPPROTO_NONE:
293                                 opts &= ~IPV6_FW_IP6OPT_NONXT;
294                                 nopts &= ~IPV6_FW_IP6OPT_NONXT;
295                                 goto opts_check;
296                         case IPPROTO_DSTOPTS:
297                                 opts &= ~IPV6_FW_IP6OPT_OPTS;
298                                 nopts &= ~IPV6_FW_IP6OPT_OPTS;
299                                 break;
300                         default:
301                                 goto opts_check;
302                         }
303                         *off += (ip6e->ip6e_len + 1) << 3;
304                         break;
305                 }
306                 *nxt = ip6e->ip6e_nxt;
307
308         }
309  opts_check:
310         if (f->fw_ip6opt == f->fw_ip6nopt)      /* XXX */
311                 return 1;
312
313         if (opts == 0 && nopts == nopts_sve)
314                 return 1;
315         else
316                 return 0;
317 }
318
319 static __inline int
320 iface_match(struct ifnet *ifp, union ip6_fw_if *ifu, int byname)
321 {
322         /* Check by name or by IP address */
323         if (byname) {
324                 /* Check name */
325                 if (ifu->fu_via_if.glob) {
326                         if (kfnmatch(ifu->fu_via_if.name, ifp->if_xname, 0)
327                             == FNM_NOMATCH)
328                                 return(0);
329                 } else {
330                         if (strncmp(ifp->if_xname, ifu->fu_via_if.name,
331                             IP6FW_IFNLEN) != 0)
332                                 return(0);
333                 }
334                 return(1);
335         } else if (!IN6_IS_ADDR_UNSPECIFIED(&ifu->fu_via_ip6)) {        /* Zero == wildcard */
336                 struct ifaddr_container *ifac;
337
338                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
339                         struct ifaddr *ifa = ifac->ifa;
340
341                         if (ifa->ifa_addr == NULL)
342                                 continue;
343                         if (ifa->ifa_addr->sa_family != AF_INET6)
344                                 continue;
345                         if (!IN6_ARE_ADDR_EQUAL(&ifu->fu_via_ip6,
346                             &(((struct sockaddr_in6 *)
347                             (ifa->ifa_addr))->sin6_addr)))
348                                 continue;
349                         return(1);
350                 }
351                 return(0);
352         }
353         return(1);
354 }
355
356 static void
357 ip6fw_report(struct ip6_fw *f, struct ip6_hdr *ip6,
358         struct ifnet *rif, struct ifnet *oif, int off, int nxt)
359 {
360         static int counter;
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);
364         int count;
365         char *action;
366         char action2[32], proto[102], name[18];
367         int len;
368
369         count = f ? f->fw_pcnt : ++counter;
370         if (fw6_verbose_limit != 0 && count > fw6_verbose_limit)
371                 return;
372
373         /* Print command name */
374         ksnprintf(SNPARGS(name, 0), "ip6fw: %d", f ? f->fw_number : -1);
375
376         action = action2;
377         if (!f)
378                 action = "Refuse";
379         else {
380                 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
381                 case IPV6_FW_F_DENY:
382                         action = "Deny";
383                         break;
384                 case IPV6_FW_F_REJECT:
385                         if (f->fw_reject_code == IPV6_FW_REJECT_RST)
386                                 action = "Reset";
387                         else
388                                 action = "Unreach";
389                         break;
390                 case IPV6_FW_F_ACCEPT:
391                         action = "Accept";
392                         break;
393                 case IPV6_FW_F_COUNT:
394                         action = "Count";
395                         break;
396                 case IPV6_FW_F_DIVERT:
397                         ksnprintf(SNPARGS(action2, 0), "Divert %d",
398                             f->fw_divert_port);
399                         break;
400                 case IPV6_FW_F_TEE:
401                         ksnprintf(SNPARGS(action2, 0), "Tee %d",
402                             f->fw_divert_port);
403                         break;
404                 case IPV6_FW_F_SKIPTO:
405                         ksnprintf(SNPARGS(action2, 0), "SkipTo %d",
406                             f->fw_skipto_rule);
407                         break;
408                 default:
409                         action = "UNKNOWN";
410                         break;
411                 }
412         }
413
414         switch (nxt) {
415         case IPPROTO_TCP:
416                 len = ksnprintf(SNPARGS(proto, 0), "TCP [%s]",
417                     ip6_sprintf(&ip6->ip6_src));
418                 if (off > 0)
419                         len += ksnprintf(SNPARGS(proto, len), ":%d ",
420                             ntohs(tcp6->th_sport));
421                 else
422                         len += ksnprintf(SNPARGS(proto, len), " ");
423                 len += ksnprintf(SNPARGS(proto, len), "[%s]",
424                     ip6_sprintf(&ip6->ip6_dst));
425                 if (off > 0)
426                         ksnprintf(SNPARGS(proto, len), ":%d",
427                             ntohs(tcp6->th_dport));
428                 break;
429         case IPPROTO_UDP:
430                 len = ksnprintf(SNPARGS(proto, 0), "UDP [%s]",
431                     ip6_sprintf(&ip6->ip6_src));
432                 if (off > 0)
433                         len += ksnprintf(SNPARGS(proto, len), ":%d ",
434                             ntohs(udp->uh_sport));
435                 else
436                     len += ksnprintf(SNPARGS(proto, len), " ");
437                 len += ksnprintf(SNPARGS(proto, len), "[%s]",
438                     ip6_sprintf(&ip6->ip6_dst));
439                 if (off > 0)
440                         ksnprintf(SNPARGS(proto, len), ":%d",
441                             ntohs(udp->uh_dport));
442                 break;
443         case IPPROTO_ICMPV6:
444                 if (off > 0)
445                         len = ksnprintf(SNPARGS(proto, 0), "IPV6-ICMP:%u.%u ",
446                             icmp6->icmp6_type, icmp6->icmp6_code);
447                 else
448                         len = ksnprintf(SNPARGS(proto, 0), "IPV6-ICMP ");
449                 len += ksnprintf(SNPARGS(proto, len), "[%s]",
450                     ip6_sprintf(&ip6->ip6_src));
451                 ksnprintf(SNPARGS(proto, len), " [%s]",
452                     ip6_sprintf(&ip6->ip6_dst));
453                 break;
454         default:
455                 len = ksnprintf(SNPARGS(proto, 0), "P:%d [%s]", nxt,
456                     ip6_sprintf(&ip6->ip6_src));
457                 ksnprintf(SNPARGS(proto, len), " [%s]",
458                     ip6_sprintf(&ip6->ip6_dst));
459                 break;
460         }
461
462         if (oif)
463                 log(LOG_SECURITY | LOG_INFO, "%s %s %s out via %s\n",
464                     name, action, proto, if_name(oif));
465         else if (rif)
466                 log(LOG_SECURITY | LOG_INFO, "%s %s %s in via %s\n",
467                     name, action, proto, if_name(rif));
468         else
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);
474 }
475
476 /*
477  * Parameters:
478  *
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)
484  * #else
485  *      *cookie Skip up to the first rule past this rule number;
486  * #endif
487  *      *m      The packet; we set to NULL when/if we nuke it.
488  *
489  * Return value:
490  *
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.
495  */
496
497 static int
498 ip6_fw_chk(struct ip6_hdr **pip6,
499         struct ifnet *oif, u_int16_t *cookie, struct mbuf **m)
500 {
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;
505         u_short offset = 0;
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;
510 #else
511         u_int16_t ignport = ntohs(*cookie);
512 #endif
513
514         *cookie = 0;
515         /*
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.
519          * #endif
520          */
521         chain = LIST_FIRST(&ip6_fw_chain);
522 #ifdef IP6FW_DIVERT_RESTART
523         if (skipto) {
524                 if (skipto >= 65535)
525                         goto dropit;
526                 while (chain && (chain->rule->fw_number <= skipto)) {
527                         chain = LIST_NEXT(chain, chain);
528                 }
529                 if (! chain) goto dropit;
530         }
531 #endif /* IP6FW_DIVERT_RESTART */
532         for (; chain; chain = LIST_NEXT(chain, chain)) {
533                 struct ip6_fw *const f = chain->rule;
534
535                 if (oif) {
536                         /* Check direction outbound */
537                         if (!(f->fw_flg & IPV6_FW_F_OUT))
538                                 continue;
539                 } else {
540                         /* Check direction inbound */
541                         if (!(f->fw_flg & IPV6_FW_F_IN))
542                                 continue;
543                 }
544
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]))
550
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)))
554                         continue;
555
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)))
559                         continue;
560
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;
565
566                         /* Backwards compatibility hack for "via" */
567                         if (!iface || !iface_match(iface,
568                             &f->fw_in_if, f->fw_flg & IPV6_FW_F_OIFNAME))
569                                 continue;
570                 } else {
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)))
575                                 continue;
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)))
580                                 continue;
581                 }
582
583                 /* Check IP options */
584                 if (!ip6opts_match(&ip6, f, m, &off, &nxt, &offset))
585                         continue;
586
587                 /* Fragments */
588                 if ((f->fw_flg & IPV6_FW_F_FRAG) && !offset)
589                         continue;
590
591                 /* Check protocol; if wildcard, match */
592                 if (f->fw_prot == IPPROTO_IPV6)
593                         goto got_match;
594
595                 /* If different, don't match */
596                 if (nxt != f->fw_prot)
597                         continue;
598
599 #define PULLUP_TO(len)  do {                                            \
600                             if ((*m)->m_len < (len)                     \
601                                 && (*m = m_pullup(*m, (len))) == NULL) {\
602                                     goto dropit;                        \
603                             }                                           \
604                             *pip6 = ip6 = mtod(*m, struct ip6_hdr *);   \
605                         } while (0)
606
607                 /* Protocol specific checks */
608                 switch (nxt) {
609                 case IPPROTO_TCP:
610                     {
611                         struct tcphdr *tcp6;
612
613                         if (offset == 1) {      /* cf. RFC 1858 */
614                                 PULLUP_TO(off + 4); /* XXX ? */
615                                 goto bogusfrag;
616                         }
617                         if (offset != 0) {
618                                 /*
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.
622                                  */
623                                 if (f->fw_nports != 0 ||
624                                     f->fw_tcpf != f->fw_tcpnf)
625                                         continue;
626
627                                 break;
628                         }
629                         PULLUP_TO(off + 14);
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))
634                                 continue;
635                         src_port = ntohs(tcp6->th_sport);
636                         dst_port = ntohs(tcp6->th_dport);
637                         goto check_ports;
638                     }
639
640                 case IPPROTO_UDP:
641                     {
642                         struct udphdr *udp;
643
644                         if (offset != 0) {
645                                 /*
646                                  * Port specification is unavailable -- if this
647                                  * rule specifies a port, we consider the rule
648                                  * a non-match.
649                                  */
650                                 if (f->fw_nports != 0)
651                                         continue;
652
653                                 break;
654                         }
655                         PULLUP_TO(off + 4);
656                         udp = (struct udphdr *) ((caddr_t)ip6 + off);
657                         src_port = ntohs(udp->uh_sport);
658                         dst_port = ntohs(udp->uh_dport);
659 check_ports:
660                         if (!port_match6(&f->fw_pts[0],
661                             IPV6_FW_GETNSRCP(f), src_port,
662                             f->fw_flg & IPV6_FW_F_SRNG))
663                                 continue;
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))
667                                 continue;
668                         break;
669                     }
670
671                 case IPPROTO_ICMPV6:
672                     {
673                         struct icmp6_hdr *icmp;
674
675                         if (offset != 0)        /* Type isn't valid */
676                                 break;
677                         PULLUP_TO(off + 2);
678                         icmp = (struct icmp6_hdr *) ((caddr_t)ip6 + off);
679                         if (!icmp6type_match(icmp, f))
680                                 continue;
681                         break;
682                     }
683 #undef PULLUP_TO
684
685 bogusfrag:
686                         if (fw6_verbose)
687                                 ip6fw_report(NULL, ip6, rif, oif, off, nxt);
688                         goto dropit;
689                 }
690
691 got_match:
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:
696                 case IPV6_FW_F_TEE:
697                         if (f->fw_divert_port == ignport)
698                                 continue;       /* ignore this rule */
699                         break;
700                 }
701
702 #endif /* IP6FW_DIVERT_RESTART */
703                 /* Update statistics */
704                 f->fw_pcnt += 1;
705                 f->fw_bcnt += ntohs(ip6->ip6_plen);
706                 f->timestamp = time_second;
707
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);
711
712                 /* Take appropriate action */
713                 switch (f->fw_flg & IPV6_FW_F_COMMAND) {
714                 case IPV6_FW_F_ACCEPT:
715                         return(0);
716                 case IPV6_FW_F_COUNT:
717                         continue;
718                 case IPV6_FW_F_DIVERT:
719 #ifdef IP6FW_DIVERT_RESTART
720                         *cookie = f->fw_number;
721 #else
722                         *cookie = htons(f->fw_divert_port);
723 #endif /* IP6FW_DIVERT_RESTART */
724                         return(f->fw_divert_port);
725                 case IPV6_FW_F_TEE:
726                         /*
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.
733                          */
734                         continue;
735                 case IPV6_FW_F_SKIPTO:
736 #ifdef DIAGNOSTIC
737                         while (chain->chain.le_next
738                             && chain->chain.le_next->rule->fw_number
739                                 < f->fw_skipto_rule)
740 #else
741                         while (chain->chain.le_next->rule->fw_number
742                             < f->fw_skipto_rule)
743 #endif
744                                 chain = chain->chain.le_next;
745                         continue;
746                 }
747
748                 /* Deny/reject this packet using this rule */
749                 rule = f;
750                 break;
751         }
752
753 #ifdef DIAGNOSTIC
754         /* Rule 65535 should always be there and should always match */
755         if (!chain)
756                 panic("ip6_fw: chain");
757 #endif
758
759         /*
760          * At this point, we're going to drop the packet.
761          * Send a reject notice if all of the following are true:
762          *
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
766          */
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:
773 #if 1   /* not tested */
774                   {
775                         struct tcphdr *const tcp =
776                                 (struct tcphdr *) ((caddr_t)ip6 + off);
777                         struct {
778                                 struct ip6_hdr ip6;
779                                 struct tcphdr th;
780                         } ti;
781                         tcp_seq ack, seq;
782                         int flags;
783
784                         if (offset != 0 || (tcp->th_flags & TH_RST))
785                                 break;
786
787                         ti.ip6 = *ip6;
788                         ti.th = *tcp;
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) {
793                                 ack = 0;
794                                 seq = ti.th.th_ack;
795                                 flags = TH_RST;
796                         } else {
797                                 ack = ti.th.th_seq;
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);
804                                 } else {
805                                         m_freem(*m);
806                                         *m = NULL;
807                                         break;
808                                 }
809                                 seq = 0;
810                                 flags = TH_RST|TH_ACK;
811                         }
812                         bcopy(&ti, ip6, sizeof(ti));
813                         tcp_respond(NULL, ip6, (struct tcphdr *)(ip6 + 1),
814                                 *m, ack, seq, flags);
815                         *m = NULL;
816                         break;
817                   }
818 #endif
819                 default:        /* Send an ICMP unreachable using code */
820                         if (oif)
821                                 (*m)->m_pkthdr.rcvif = oif;
822                         icmp6_error(*m, ICMP6_DST_UNREACH,
823                             rule->fw_reject_code, 0);
824                         *m = NULL;
825                         break;
826                 }
827         }
828
829 dropit:
830         /*
831          * Finally, drop the packet.
832          */
833         if (*m) {
834                 m_freem(*m);
835                 *m = NULL;
836         }
837         return(0);
838 }
839
840 static int
841 add_entry6(struct ip6_fw_head *chainptr, struct ip6_fw *frwl)
842 {
843         struct ip6_fw *ftmp = NULL;
844         struct ip6_fw_chain *fwc = NULL, *fcp, *fcpl = NULL;
845         u_short nbr = 0;
846
847         fwc = kmalloc(sizeof *fwc, M_IP6FW, M_INTWAIT);
848         ftmp = kmalloc(sizeof *ftmp, M_IP6FW, M_INTWAIT);
849
850         bcopy(frwl, ftmp, sizeof(struct ip6_fw));
851         ftmp->fw_in_if.fu_via_if.name[IP6FW_IFNLEN - 1] = '\0';
852         ftmp->fw_pcnt = 0L;
853         ftmp->fw_bcnt = 0L;
854         fwc->rule = ftmp;
855
856         crit_enter();
857
858         if (!chainptr->lh_first) {
859                 LIST_INSERT_HEAD(chainptr, fwc, chain);
860                 crit_exit();
861                 return(0);
862         } else if (ftmp->fw_number == (u_short)-1) {
863                 if (fwc)  kfree(fwc, M_IP6FW);
864                 if (ftmp) kfree(ftmp, M_IP6FW);
865                 crit_exit();
866                 dprintf(("%s bad rule number\n", err_prefix));
867                 return (EINVAL);
868         }
869
870         /* If entry number is 0, find highest numbered rule and add 100 */
871         if (ftmp->fw_number == 0) {
872                 LIST_FOREACH(fcp, chainptr, chain) {
873                         if (fcp->rule->fw_number != (u_short)-1)
874                                 nbr = fcp->rule->fw_number;
875                         else
876                                 break;
877                 }
878                 if (nbr < (u_short)-1 - 100)
879                         nbr += 100;
880                 ftmp->fw_number = nbr;
881         }
882
883         /* Got a valid number; now insert it, keeping the list ordered */
884         LIST_FOREACH(fcp, chainptr, chain) {
885                 if (fcp->rule->fw_number > ftmp->fw_number) {
886                         if (fcpl) {
887                                 LIST_INSERT_AFTER(fcpl, fwc, chain);
888                         } else {
889                                 LIST_INSERT_HEAD(chainptr, fwc, chain);
890                         }
891                         break;
892                 } else {
893                         fcpl = fcp;
894                 }
895         }
896
897         crit_exit();
898         return (0);
899 }
900
901 static int
902 del_entry6(struct ip6_fw_head *chainptr, u_short number)
903 {
904         struct ip6_fw_chain *fcp;
905
906         crit_enter();
907
908         fcp = chainptr->lh_first;
909         if (number != (u_short)-1) {
910                 for (; fcp; fcp = fcp->chain.le_next) {
911                         if (fcp->rule->fw_number == number) {
912                                 LIST_REMOVE(fcp, chain);
913                                 crit_exit();
914                                 kfree(fcp->rule, M_IP6FW);
915                                 kfree(fcp, M_IP6FW);
916                                 return 0;
917                         }
918                 }
919         }
920
921         crit_exit();
922         return (EINVAL);
923 }
924
925 static int
926 zero_entry6(struct mbuf *m)
927 {
928         struct ip6_fw *frwl;
929         struct ip6_fw_chain *fcp;
930
931         if (m && m->m_len != 0) {
932                 if (m->m_len != sizeof(struct ip6_fw))
933                         return(EINVAL);
934                 frwl = mtod(m, struct ip6_fw *);
935         }
936         else
937                 frwl = NULL;
938
939         /*
940          *      It's possible to insert multiple chain entries with the
941          *      same number, so we don't stop after finding the first
942          *      match if zeroing a specific entry.
943          */
944         crit_enter();
945         for (fcp = ip6_fw_chain.lh_first; fcp; fcp = fcp->chain.le_next)
946                 if (!frwl || frwl->fw_number == fcp->rule->fw_number) {
947                         fcp->rule->fw_bcnt = fcp->rule->fw_pcnt = 0;
948                         fcp->rule->timestamp = 0;
949                 }
950         crit_exit();
951
952         if (fw6_verbose) {
953                 if (frwl)
954                         log(LOG_SECURITY | LOG_NOTICE,
955                             "ip6fw: Entry %d cleared.\n", frwl->fw_number);
956                 else
957                         log(LOG_SECURITY | LOG_NOTICE,
958                             "ip6fw: Accounting cleared.\n");
959         }
960
961         return(0);
962 }
963
964 static struct ip6_fw *
965 check_ip6fw_mbuf(struct mbuf *m)
966 {
967         /* Check length */
968         if (m->m_len != sizeof(struct ip6_fw)) {
969                 dprintf(("%s len=%d, want %d\n", err_prefix, m->m_len,
970                         (int)sizeof(struct ip6_fw)));
971                 return (NULL);
972         }
973         return(check_ip6fw_struct(mtod(m, struct ip6_fw *)));
974 }
975
976 static struct ip6_fw *
977 check_ip6fw_struct(struct ip6_fw *frwl)
978 {
979         /* Check for invalid flag bits */
980         if ((frwl->fw_flg & ~IPV6_FW_F_MASK) != 0) {
981                 dprintf(("%s undefined flag bits set (flags=%x)\n",
982                     err_prefix, frwl->fw_flg));
983                 return (NULL);
984         }
985         /* Must apply to incoming or outgoing (or both) */
986         if (!(frwl->fw_flg & (IPV6_FW_F_IN | IPV6_FW_F_OUT))) {
987                 dprintf(("%s neither in nor out\n", err_prefix));
988                 return (NULL);
989         }
990         /* Empty interface name is no good */
991         if (((frwl->fw_flg & IPV6_FW_F_IIFNAME)
992               && !*frwl->fw_in_if.fu_via_if.name)
993             || ((frwl->fw_flg & IPV6_FW_F_OIFNAME)
994               && !*frwl->fw_out_if.fu_via_if.name)) {
995                 dprintf(("%s empty interface name\n", err_prefix));
996                 return (NULL);
997         }
998         /* Sanity check interface matching */
999         if ((frwl->fw_flg & IF6_FW_F_VIAHACK) == IF6_FW_F_VIAHACK) {
1000                 ;               /* allow "via" backwards compatibility */
1001         } else if ((frwl->fw_flg & IPV6_FW_F_IN)
1002             && (frwl->fw_flg & IPV6_FW_F_OIFACE)) {
1003                 dprintf(("%s outgoing interface check on incoming\n",
1004                     err_prefix));
1005                 return (NULL);
1006         }
1007         /* Sanity check port ranges */
1008         if ((frwl->fw_flg & IPV6_FW_F_SRNG) && IPV6_FW_GETNSRCP(frwl) < 2) {
1009                 dprintf(("%s src range set but n_src_p=%d\n",
1010                     err_prefix, IPV6_FW_GETNSRCP(frwl)));
1011                 return (NULL);
1012         }
1013         if ((frwl->fw_flg & IPV6_FW_F_DRNG) && IPV6_FW_GETNDSTP(frwl) < 2) {
1014                 dprintf(("%s dst range set but n_dst_p=%d\n",
1015                     err_prefix, IPV6_FW_GETNDSTP(frwl)));
1016                 return (NULL);
1017         }
1018         if (IPV6_FW_GETNSRCP(frwl) + IPV6_FW_GETNDSTP(frwl) > IPV6_FW_MAX_PORTS) {
1019                 dprintf(("%s too many ports (%d+%d)\n",
1020                     err_prefix, IPV6_FW_GETNSRCP(frwl), IPV6_FW_GETNDSTP(frwl)));
1021                 return (NULL);
1022         }
1023         /*
1024          *      Protocols other than TCP/UDP don't use port range
1025          */
1026         if ((frwl->fw_prot != IPPROTO_TCP) &&
1027             (frwl->fw_prot != IPPROTO_UDP) &&
1028             (IPV6_FW_GETNSRCP(frwl) || IPV6_FW_GETNDSTP(frwl))) {
1029                 dprintf(("%s port(s) specified for non TCP/UDP rule\n",
1030                     err_prefix));
1031                 return(NULL);
1032         }
1033
1034         /*
1035          *      Rather than modify the entry to make such entries work,
1036          *      we reject this rule and require user level utilities
1037          *      to enforce whatever policy they deem appropriate.
1038          */
1039         if ((frwl->fw_src.s6_addr32[0] & (~frwl->fw_smsk.s6_addr32[0])) ||
1040                 (frwl->fw_src.s6_addr32[1] & (~frwl->fw_smsk.s6_addr32[1])) ||
1041                 (frwl->fw_src.s6_addr32[2] & (~frwl->fw_smsk.s6_addr32[2])) ||
1042                 (frwl->fw_src.s6_addr32[3] & (~frwl->fw_smsk.s6_addr32[3])) ||
1043                 (frwl->fw_dst.s6_addr32[0] & (~frwl->fw_dmsk.s6_addr32[0])) ||
1044                 (frwl->fw_dst.s6_addr32[1] & (~frwl->fw_dmsk.s6_addr32[1])) ||
1045                 (frwl->fw_dst.s6_addr32[2] & (~frwl->fw_dmsk.s6_addr32[2])) ||
1046                 (frwl->fw_dst.s6_addr32[3] & (~frwl->fw_dmsk.s6_addr32[3]))) {
1047                 dprintf(("%s rule never matches\n", err_prefix));
1048                 return(NULL);
1049         }
1050
1051         if ((frwl->fw_flg & IPV6_FW_F_FRAG) &&
1052                 (frwl->fw_prot == IPPROTO_UDP || frwl->fw_prot == IPPROTO_TCP)) {
1053                 if (frwl->fw_nports) {
1054                         dprintf(("%s cannot mix 'frag' and ports\n", err_prefix));
1055                         return(NULL);
1056                 }
1057                 if (frwl->fw_prot == IPPROTO_TCP &&
1058                         frwl->fw_tcpf != frwl->fw_tcpnf) {
1059                         dprintf(("%s cannot mix 'frag' with TCP flags\n", err_prefix));
1060                         return(NULL);
1061                 }
1062         }
1063
1064         /* Check command specific stuff */
1065         switch (frwl->fw_flg & IPV6_FW_F_COMMAND)
1066         {
1067         case IPV6_FW_F_REJECT:
1068                 if (frwl->fw_reject_code >= 0x100
1069                     && !(frwl->fw_prot == IPPROTO_TCP
1070                       && frwl->fw_reject_code == IPV6_FW_REJECT_RST)) {
1071                         dprintf(("%s unknown reject code\n", err_prefix));
1072                         return(NULL);
1073                 }
1074                 break;
1075         case IPV6_FW_F_DIVERT:          /* Diverting to port zero is invalid */
1076         case IPV6_FW_F_TEE:
1077                 if (frwl->fw_divert_port == 0) {
1078                         dprintf(("%s can't divert to port 0\n", err_prefix));
1079                         return (NULL);
1080                 }
1081                 break;
1082         case IPV6_FW_F_DENY:
1083         case IPV6_FW_F_ACCEPT:
1084         case IPV6_FW_F_COUNT:
1085         case IPV6_FW_F_SKIPTO:
1086                 break;
1087         default:
1088                 dprintf(("%s invalid command\n", err_prefix));
1089                 return(NULL);
1090         }
1091
1092         return frwl;
1093 }
1094
1095 static int
1096 ip6_fw_ctl(int stage, struct mbuf **mm)
1097 {
1098         int error;
1099         struct mbuf *m;
1100
1101         if (stage == IPV6_FW_GET) {
1102                 struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1103                 *mm = m = m_get(M_WAITOK, MT_DATA); /* XXX */
1104                 if (!m)
1105                         return(ENOBUFS);
1106                 if (sizeof *(fcp->rule) > MLEN) {
1107                         MCLGET(m, M_WAITOK);
1108                         if ((m->m_flags & M_EXT) == 0) {
1109                                 m_free(m);
1110                                 return(ENOBUFS);
1111                         }
1112                 }
1113                 for (; fcp; fcp = fcp->chain.le_next) {
1114                         bcopy(fcp->rule, m->m_data, sizeof *(fcp->rule));
1115                         m->m_len = sizeof *(fcp->rule);
1116                         m->m_next = m_get(M_WAITOK, MT_DATA); /* XXX */
1117                         if (!m->m_next) {
1118                                 m_freem(*mm);
1119                                 return(ENOBUFS);
1120                         }
1121                         m = m->m_next;
1122                         if (sizeof *(fcp->rule) > MLEN) {
1123                                 MCLGET(m, M_WAITOK);
1124                                 if ((m->m_flags & M_EXT) == 0) {
1125                                         m_freem(*mm);
1126                                         return(ENOBUFS);
1127                                 }
1128                         }
1129                         m->m_len = 0;
1130                 }
1131                 return (0);
1132         }
1133         m = *mm;
1134         /* only allow get calls if secure mode > 2 */
1135         if (securelevel > 2) {
1136                 if (m) {
1137                         m_freem(m);
1138                         *mm = NULL;
1139                 }
1140                 return(EPERM);
1141         }
1142         if (stage == IPV6_FW_FLUSH) {
1143                 while (ip6_fw_chain.lh_first != NULL &&
1144                     ip6_fw_chain.lh_first->rule->fw_number != (u_short)-1) {
1145                         struct ip6_fw_chain *fcp = ip6_fw_chain.lh_first;
1146                         crit_enter();
1147                         LIST_REMOVE(ip6_fw_chain.lh_first, chain);
1148                         crit_exit();
1149                         kfree(fcp->rule, M_IP6FW);
1150                         kfree(fcp, M_IP6FW);
1151                 }
1152                 if (m) {
1153                         m_freem(m);
1154                         *mm = NULL;
1155                 }
1156                 return (0);
1157         }
1158         if (stage == IPV6_FW_ZERO) {
1159                 error = zero_entry6(m);
1160                 if (m) {
1161                         m_freem(m);
1162                         *mm = NULL;
1163                 }
1164                 return (error);
1165         }
1166         if (m == NULL) {
1167                 kprintf("%s NULL mbuf ptr\n", err_prefix);
1168                 return (EINVAL);
1169         }
1170
1171         if (stage == IPV6_FW_ADD) {
1172                 struct ip6_fw *frwl = check_ip6fw_mbuf(m);
1173
1174                 if (!frwl)
1175                         error = EINVAL;
1176                 else
1177                         error = add_entry6(&ip6_fw_chain, frwl);
1178                 if (m) {
1179                         m_freem(m);
1180                         *mm = NULL;
1181                 }
1182                 return error;
1183         }
1184         if (stage == IPV6_FW_DEL) {
1185                 if (m->m_len != sizeof(struct ip6_fw)) {
1186                         dprintf(("%s len=%d, want %d\n", err_prefix, m->m_len,
1187                                 (int)sizeof(struct ip6_fw)));
1188                         error = EINVAL;
1189                 } else if (mtod(m, struct ip6_fw *)->fw_number == (u_short)-1) {
1190                         dprintf(("%s can't delete rule 65535\n", err_prefix));
1191                         error = EINVAL;
1192                 } else
1193                         error = del_entry6(&ip6_fw_chain,
1194                             mtod(m, struct ip6_fw *)->fw_number);
1195                 if (m) {
1196                         m_freem(m);
1197                         *mm = NULL;
1198                 }
1199                 return error;
1200         }
1201
1202         dprintf(("%s unknown request %d\n", err_prefix, stage));
1203         if (m) {
1204                 m_freem(m);
1205                 *mm = NULL;
1206         }
1207         return (EINVAL);
1208 }
1209
1210 void
1211 ip6_fw_init(void)
1212 {
1213         struct ip6_fw default_rule;
1214
1215         ip6_fw_chk_ptr = ip6_fw_chk;
1216         ip6_fw_ctl_ptr = ip6_fw_ctl;
1217         LIST_INIT(&ip6_fw_chain);
1218
1219         bzero(&default_rule, sizeof default_rule);
1220         default_rule.fw_prot = IPPROTO_IPV6;
1221         default_rule.fw_number = (u_short)-1;
1222 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1223         default_rule.fw_flg |= IPV6_FW_F_ACCEPT;
1224 #else
1225         default_rule.fw_flg |= IPV6_FW_F_DENY;
1226 #endif
1227         default_rule.fw_flg |= IPV6_FW_F_IN | IPV6_FW_F_OUT;
1228         if (check_ip6fw_struct(&default_rule) == NULL ||
1229                 add_entry6(&ip6_fw_chain, &default_rule))
1230                 panic(__func__);
1231
1232         kprintf("IPv6 packet filtering initialized, ");
1233 #ifdef IPV6FIREWALL_DEFAULT_TO_ACCEPT
1234         kprintf("default to accept, ");
1235 #endif
1236 #ifndef IPV6FIREWALL_VERBOSE
1237         kprintf("logging disabled\n");
1238 #else
1239         if (fw6_verbose_limit == 0)
1240                 kprintf("unlimited logging\n");
1241         else
1242                 kprintf("logging limited to %d packets/entry\n",
1243                     fw6_verbose_limit);
1244 #endif
1245 }
1246
1247 static ip6_fw_chk_t *old_chk_ptr;
1248 static ip6_fw_ctl_t *old_ctl_ptr;
1249
1250 static int
1251 ip6fw_modevent(module_t mod, int type, void *unused)
1252 {
1253         switch (type) {
1254         case MOD_LOAD:
1255                 crit_enter();
1256
1257                 old_chk_ptr = ip6_fw_chk_ptr;
1258                 old_ctl_ptr = ip6_fw_ctl_ptr;
1259
1260                 ip6_fw_init();
1261                 crit_exit();
1262                 return 0;
1263         case MOD_UNLOAD:
1264                 crit_enter();
1265                 ip6_fw_chk_ptr =  old_chk_ptr;
1266                 ip6_fw_ctl_ptr =  old_ctl_ptr;
1267                 while (LIST_FIRST(&ip6_fw_chain) != NULL) {
1268                         struct ip6_fw_chain *fcp = LIST_FIRST(&ip6_fw_chain);
1269                         LIST_REMOVE(LIST_FIRST(&ip6_fw_chain), chain);
1270                         kfree(fcp->rule, M_IP6FW);
1271                         kfree(fcp, M_IP6FW);
1272                 }
1273
1274                 crit_exit();
1275                 kprintf("IPv6 firewall unloaded\n");
1276                 return 0;
1277         default:
1278                 break;
1279         }
1280         return 0;
1281 }
1282
1283 static moduledata_t ip6fwmod = {
1284         "ip6fw",
1285         ip6fw_modevent,
1286         0
1287 };
1288 DECLARE_MODULE(ip6fw, ip6fwmod, SI_SUB_PSEUDO, SI_ORDER_ANY);