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