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