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