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