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