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