1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_ipfw.h" 33 #include "opt_inet.h" 34 #include "opt_inet6.h" 35 #ifndef INET 36 #error IPFIREWALL requires INET. 37 #endif /* INET */ 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/module.h> 44 #include <sys/kernel.h> 45 #include <sys/lock.h> 46 #include <sys/rwlock.h> 47 #include <sys/socket.h> 48 #include <sys/sysctl.h> 49 50 #include <net/if.h> 51 #include <net/if_var.h> 52 #include <net/route.h> 53 #include <net/ethernet.h> 54 #include <net/pfil.h> 55 #include <net/vnet.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/ip.h> 60 #include <netinet/ip_var.h> 61 #include <netinet/ip_fw.h> 62 #ifdef INET6 63 #include <netinet/ip6.h> 64 #include <netinet6/ip6_var.h> 65 #include <netinet6/scope6_var.h> 66 #endif 67 68 #include <netgraph/ng_ipfw.h> 69 70 #include <netpfil/ipfw/ip_fw_private.h> 71 72 #include <machine/in_cksum.h> 73 74 VNET_DEFINE_STATIC(int, fw_enable) = 1; 75 #define V_fw_enable VNET(fw_enable) 76 77 #ifdef INET6 78 VNET_DEFINE_STATIC(int, fw6_enable) = 1; 79 #define V_fw6_enable VNET(fw6_enable) 80 #endif 81 82 VNET_DEFINE_STATIC(int, fwlink_enable) = 0; 83 #define V_fwlink_enable VNET(fwlink_enable) 84 85 int ipfw_chg_hook(SYSCTL_HANDLER_ARGS); 86 87 /* Forward declarations. */ 88 static int ipfw_divert(struct mbuf **, struct ip_fw_args *, bool); 89 90 #ifdef SYSCTL_NODE 91 92 SYSBEGIN(f1) 93 94 SYSCTL_DECL(_net_inet_ip_fw); 95 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, enable, 96 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3 | 97 CTLFLAG_NEEDGIANT, &VNET_NAME(fw_enable), 0, ipfw_chg_hook, "I", 98 "Enable ipfw"); 99 #ifdef INET6 100 SYSCTL_DECL(_net_inet6_ip6_fw); 101 SYSCTL_PROC(_net_inet6_ip6_fw, OID_AUTO, enable, 102 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3 | 103 CTLFLAG_NEEDGIANT, &VNET_NAME(fw6_enable), 0, ipfw_chg_hook, "I", 104 "Enable ipfw+6"); 105 #endif /* INET6 */ 106 107 SYSCTL_DECL(_net_link_ether); 108 SYSCTL_PROC(_net_link_ether, OID_AUTO, ipfw, 109 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3 | 110 CTLFLAG_NEEDGIANT, &VNET_NAME(fwlink_enable), 0, ipfw_chg_hook, "I", 111 "Pass ether pkts through firewall"); 112 113 SYSEND 114 115 #endif /* SYSCTL_NODE */ 116 117 /* 118 * The pfilter hook to pass packets to ipfw_chk and then to 119 * dummynet, divert, netgraph or other modules. 120 * The packet may be consumed. 121 */ 122 static pfil_return_t 123 ipfw_check_packet(struct mbuf **m0, struct ifnet *ifp, int flags, 124 void *ruleset __unused, struct inpcb *inp) 125 { 126 struct ip_fw_args args; 127 struct m_tag *tag; 128 pfil_return_t ret; 129 int ipfw; 130 131 args.flags = (flags & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT; 132 again: 133 /* 134 * extract and remove the tag if present. If we are left 135 * with onepass, optimize the outgoing path. 136 */ 137 tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL); 138 if (tag != NULL) { 139 args.rule = *((struct ipfw_rule_ref *)(tag+1)); 140 m_tag_delete(*m0, tag); 141 if (args.rule.info & IPFW_ONEPASS) 142 return (PFIL_PASS); 143 args.flags |= IPFW_ARGS_REF; 144 } 145 146 args.m = *m0; 147 args.ifp = ifp; 148 args.inp = inp; 149 args.rule.pkt_mark = 0; 150 151 ipfw = ipfw_chk(&args); 152 *m0 = args.m; 153 154 KASSERT(*m0 != NULL || ipfw == IP_FW_DENY || 155 ipfw == IP_FW_NAT64, ("%s: m0 is NULL", __func__)); 156 157 ret = PFIL_PASS; 158 switch (ipfw) { 159 case IP_FW_PASS: 160 /* next_hop may be set by ipfw_chk */ 161 if ((args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR | 162 IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) == 0) 163 break; 164 #if (!defined(INET6) && !defined(INET)) 165 ret = PFIL_DROPPED; 166 #else 167 { 168 void *psa; 169 size_t len; 170 #ifdef INET 171 if (args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR)) { 172 MPASS((args.flags & (IPFW_ARGS_NH4 | 173 IPFW_ARGS_NH4PTR)) != (IPFW_ARGS_NH4 | 174 IPFW_ARGS_NH4PTR)); 175 MPASS((args.flags & (IPFW_ARGS_NH6 | 176 IPFW_ARGS_NH6PTR)) == 0); 177 len = sizeof(struct sockaddr_in); 178 psa = (args.flags & IPFW_ARGS_NH4) ? 179 &args.hopstore : args.next_hop; 180 if (in_localip(satosin(psa)->sin_addr)) 181 (*m0)->m_flags |= M_FASTFWD_OURS; 182 (*m0)->m_flags |= M_IP_NEXTHOP; 183 } 184 #endif /* INET */ 185 #ifdef INET6 186 if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) { 187 MPASS((args.flags & (IPFW_ARGS_NH6 | 188 IPFW_ARGS_NH6PTR)) != (IPFW_ARGS_NH6 | 189 IPFW_ARGS_NH6PTR)); 190 MPASS((args.flags & (IPFW_ARGS_NH4 | 191 IPFW_ARGS_NH4PTR)) == 0); 192 len = sizeof(struct sockaddr_in6); 193 psa = args.next_hop6; 194 (*m0)->m_flags |= M_IP6_NEXTHOP; 195 } 196 #endif /* INET6 */ 197 /* 198 * Incoming packets should not be tagged so we do not 199 * m_tag_find. Outgoing packets may be tagged, so we 200 * reuse the tag if present. 201 */ 202 tag = (flags & PFIL_IN) ? NULL : 203 m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL); 204 if (tag != NULL) { 205 m_tag_unlink(*m0, tag); 206 } else { 207 tag = m_tag_get(PACKET_TAG_IPFORWARD, len, 208 M_NOWAIT); 209 if (tag == NULL) { 210 ret = PFIL_DROPPED; 211 break; 212 } 213 } 214 if ((args.flags & IPFW_ARGS_NH6) == 0) 215 bcopy(psa, tag + 1, len); 216 m_tag_prepend(*m0, tag); 217 ret = PFIL_PASS; 218 #ifdef INET6 219 /* IPv6 next hop needs additional handling */ 220 if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) { 221 struct sockaddr_in6 *sa6; 222 223 sa6 = satosin6(tag + 1); 224 if (args.flags & IPFW_ARGS_NH6) { 225 sa6->sin6_family = AF_INET6; 226 sa6->sin6_len = sizeof(*sa6); 227 sa6->sin6_addr = args.hopstore6.sin6_addr; 228 sa6->sin6_port = args.hopstore6.sin6_port; 229 sa6->sin6_scope_id = 230 args.hopstore6.sin6_scope_id; 231 } 232 /* 233 * If nh6 address is link-local we should convert 234 * it to kernel internal form before doing any 235 * comparisons. 236 */ 237 if (sa6_embedscope(sa6, V_ip6_use_defzone) != 0) { 238 ret = PFIL_DROPPED; 239 break; 240 } 241 if (in6_localip(&sa6->sin6_addr)) 242 (*m0)->m_flags |= M_FASTFWD_OURS; 243 } 244 #endif /* INET6 */ 245 } 246 #endif /* INET || INET6 */ 247 break; 248 249 case IP_FW_DENY: 250 ret = PFIL_DROPPED; 251 break; 252 253 case IP_FW_DUMMYNET: 254 if (ip_dn_io_ptr == NULL) { 255 ret = PFIL_DROPPED; 256 break; 257 } 258 MPASS(args.flags & IPFW_ARGS_REF); 259 if (args.flags & (IPFW_ARGS_IP4 | IPFW_ARGS_IP6)) 260 (void )ip_dn_io_ptr(m0, &args); 261 else { 262 ret = PFIL_DROPPED; 263 break; 264 } 265 /* 266 * XXX should read the return value. 267 * dummynet normally eats the packet and sets *m0=NULL 268 * unless the packet can be sent immediately. In this 269 * case args is updated and we should re-run the 270 * check without clearing args. 271 */ 272 if (*m0 != NULL) 273 goto again; 274 ret = PFIL_CONSUMED; 275 break; 276 277 case IP_FW_TEE: 278 case IP_FW_DIVERT: 279 if (ip_divert_ptr == NULL) { 280 ret = PFIL_DROPPED; 281 break; 282 } 283 MPASS(args.flags & IPFW_ARGS_REF); 284 (void )ipfw_divert(m0, &args, ipfw == IP_FW_TEE); 285 /* continue processing for the original packet (tee). */ 286 if (*m0) 287 goto again; 288 ret = PFIL_CONSUMED; 289 break; 290 291 case IP_FW_NGTEE: 292 case IP_FW_NETGRAPH: 293 if (ng_ipfw_input_p == NULL) { 294 ret = PFIL_DROPPED; 295 break; 296 } 297 MPASS(args.flags & IPFW_ARGS_REF); 298 (void )ng_ipfw_input_p(m0, &args, ipfw == IP_FW_NGTEE); 299 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */ 300 goto again; /* continue with packet */ 301 ret = PFIL_CONSUMED; 302 break; 303 304 case IP_FW_NAT: 305 /* honor one-pass in case of successful nat */ 306 if (V_fw_one_pass) 307 break; 308 goto again; 309 310 case IP_FW_REASS: 311 goto again; /* continue with packet */ 312 313 case IP_FW_NAT64: 314 ret = PFIL_CONSUMED; 315 break; 316 317 default: 318 KASSERT(0, ("%s: unknown retval", __func__)); 319 } 320 321 if (ret != PFIL_PASS) { 322 if (*m0) 323 FREE_PKT(*m0); 324 *m0 = NULL; 325 } 326 327 return (ret); 328 } 329 330 /* 331 * ipfw processing for ethernet packets (in and out), mbuf version. 332 */ 333 static pfil_return_t 334 ipfw_check_frame_mbuf(struct mbuf **m0, struct ifnet *ifp, const int flags, 335 void *ruleset __unused, struct inpcb *inp) 336 { 337 struct ip_fw_args args = { 338 .flags = IPFW_ARGS_ETHER | 339 ((flags & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT), 340 .ifp = ifp, 341 .inp = inp, 342 }; 343 struct m_tag *mtag; 344 pfil_return_t ret; 345 int ipfw; 346 347 again: 348 /* 349 * Fetch start point from rule, if any. 350 * Remove the tag if present. 351 */ 352 mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL); 353 if (mtag != NULL) { 354 args.rule = *((struct ipfw_rule_ref *)(mtag+1)); 355 m_tag_delete(*m0, mtag); 356 if (args.rule.info & IPFW_ONEPASS) 357 return (PFIL_PASS); 358 args.flags |= IPFW_ARGS_REF; 359 } 360 args.m = *m0; 361 362 ipfw = ipfw_chk(&args); 363 *m0 = args.m; 364 365 ret = PFIL_PASS; 366 switch (ipfw) { 367 case IP_FW_PASS: 368 break; 369 370 case IP_FW_DENY: 371 ret = PFIL_DROPPED; 372 break; 373 374 case IP_FW_DUMMYNET: 375 if (ip_dn_io_ptr == NULL) { 376 ret = PFIL_DROPPED; 377 break; 378 } 379 MPASS(args.flags & IPFW_ARGS_REF); 380 ip_dn_io_ptr(m0, &args); 381 return (PFIL_CONSUMED); 382 383 case IP_FW_NGTEE: 384 case IP_FW_NETGRAPH: 385 if (ng_ipfw_input_p == NULL) { 386 ret = PFIL_DROPPED; 387 break; 388 } 389 MPASS(args.flags & IPFW_ARGS_REF); 390 (void )ng_ipfw_input_p(m0, &args, ipfw == IP_FW_NGTEE); 391 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */ 392 goto again; /* continue with packet */ 393 ret = PFIL_CONSUMED; 394 break; 395 396 default: 397 KASSERT(0, ("%s: unknown retval", __func__)); 398 } 399 400 if (ret != PFIL_PASS) { 401 if (*m0) 402 FREE_PKT(*m0); 403 *m0 = NULL; 404 } 405 406 return (ret); 407 } 408 409 /* 410 * ipfw processing for ethernet packets (in and out), memory pointer version, 411 * two in/out accessors. 412 */ 413 static pfil_return_t 414 ipfw_check_frame_mem(void *mem, u_int len, int flags, struct ifnet *ifp, 415 void *ruleset __unused, struct mbuf **m) 416 { 417 struct ip_fw_args args = { 418 .flags = len | IPFW_ARGS_ETHER | 419 ((flags & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT), 420 .ifp = ifp, 421 .mem = mem, 422 }; 423 pfil_return_t ret; 424 int ipfw; 425 426 *m = NULL; 427 again: 428 ipfw = ipfw_chk(&args); 429 430 ret = PFIL_PASS; 431 switch (ipfw) { 432 case IP_FW_PASS: 433 break; 434 435 case IP_FW_DENY: 436 ret = PFIL_DROPPED; 437 break; 438 439 case IP_FW_DUMMYNET: 440 if (ip_dn_io_ptr == NULL) { 441 ret = PFIL_DROPPED; 442 break; 443 } 444 *m = m_devget(mem, len, 0, ifp, NULL); 445 if (*m == NULL) { 446 ret = PFIL_DROPPED; 447 break; 448 } 449 MPASS(args.flags & IPFW_ARGS_REF); 450 ip_dn_io_ptr(m, &args); 451 return (PFIL_CONSUMED); 452 453 case IP_FW_NGTEE: 454 case IP_FW_NETGRAPH: 455 if (ng_ipfw_input_p == NULL) { 456 ret = PFIL_DROPPED; 457 break; 458 } 459 *m = m_devget(mem, len, 0, ifp, NULL); 460 if (*m == NULL) { 461 ret = PFIL_DROPPED; 462 break; 463 } 464 MPASS(args.flags & IPFW_ARGS_REF); 465 (void )ng_ipfw_input_p(m, &args, ipfw == IP_FW_NGTEE); 466 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */ 467 goto again; /* continue with packet */ 468 ret = PFIL_CONSUMED; 469 break; 470 471 default: 472 KASSERT(0, ("%s: unknown retval", __func__)); 473 } 474 475 if (*m != NULL && ret == PFIL_PASS) 476 ret = PFIL_REALLOCED; 477 478 return (ret); 479 } 480 481 /* do the divert, return 1 on error 0 on success */ 482 static int 483 ipfw_divert(struct mbuf **m0, struct ip_fw_args *args, bool tee) 484 { 485 /* 486 * ipfw_chk() has already tagged the packet with the divert tag. 487 * If tee is set, copy packet and return original. 488 * If not tee, consume packet and send it to divert socket. 489 */ 490 struct mbuf *clone; 491 struct ip *ip = mtod(*m0, struct ip *); 492 struct m_tag *tag; 493 494 /* Cloning needed for tee? */ 495 if (tee == false) { 496 clone = *m0; /* use the original mbuf */ 497 *m0 = NULL; 498 } else { 499 clone = m_dup(*m0, M_NOWAIT); 500 /* If we cannot duplicate the mbuf, we sacrifice the divert 501 * chain and continue with the tee-ed packet. 502 */ 503 if (clone == NULL) 504 return 1; 505 } 506 507 /* 508 * Divert listeners can normally handle non-fragmented packets, 509 * but we can only reass in the non-tee case. 510 * This means that listeners on a tee rule may get fragments, 511 * and have to live with that. 512 * Note that we now have the 'reass' ipfw option so if we care 513 * we can do it before a 'tee'. 514 */ 515 if (tee == false) switch (ip->ip_v) { 516 case IPVERSION: 517 if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) { 518 int hlen; 519 struct mbuf *reass; 520 521 reass = ip_reass(clone); /* Reassemble packet. */ 522 if (reass == NULL) 523 return 0; /* not an error */ 524 /* if reass = NULL then it was consumed by ip_reass */ 525 /* 526 * IP header checksum fixup after reassembly and leave header 527 * in network byte order. 528 */ 529 ip = mtod(reass, struct ip *); 530 hlen = ip->ip_hl << 2; 531 ip->ip_sum = 0; 532 if (hlen == sizeof(struct ip)) 533 ip->ip_sum = in_cksum_hdr(ip); 534 else 535 ip->ip_sum = in_cksum(reass, hlen); 536 clone = reass; 537 } 538 break; 539 #ifdef INET6 540 case IPV6_VERSION >> 4: 541 { 542 struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *); 543 544 if (ip6->ip6_nxt == IPPROTO_FRAGMENT) { 545 int nxt, off; 546 547 off = sizeof(struct ip6_hdr); 548 nxt = frag6_input(&clone, &off, 0); 549 if (nxt == IPPROTO_DONE) 550 return (0); 551 } 552 break; 553 } 554 #endif 555 } 556 557 /* attach a tag to the packet with the reinject info */ 558 tag = m_tag_alloc(MTAG_IPFW_RULE, 0, 559 sizeof(struct ipfw_rule_ref), M_NOWAIT); 560 if (tag == NULL) { 561 FREE_PKT(clone); 562 return 1; 563 } 564 *((struct ipfw_rule_ref *)(tag+1)) = args->rule; 565 m_tag_prepend(clone, tag); 566 567 /* Do the dirty job... */ 568 ip_divert_ptr(clone, args->flags & IPFW_ARGS_IN); 569 return 0; 570 } 571 572 /* 573 * attach or detach hooks for a given protocol family 574 */ 575 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet_hook); 576 #define V_ipfw_inet_hook VNET(ipfw_inet_hook) 577 #ifdef INET6 578 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet6_hook); 579 #define V_ipfw_inet6_hook VNET(ipfw_inet6_hook) 580 #endif 581 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_link_hook); 582 #define V_ipfw_link_hook VNET(ipfw_link_hook) 583 584 static void 585 ipfw_hook(int pf) 586 { 587 struct pfil_hook_args pha = { 588 .pa_version = PFIL_VERSION, 589 .pa_flags = PFIL_IN | PFIL_OUT, 590 .pa_modname = "ipfw", 591 }; 592 pfil_hook_t *h; 593 594 switch (pf) { 595 case AF_INET: 596 pha.pa_mbuf_chk = ipfw_check_packet; 597 pha.pa_type = PFIL_TYPE_IP4; 598 pha.pa_rulname = "default"; 599 h = &V_ipfw_inet_hook; 600 break; 601 #ifdef INET6 602 case AF_INET6: 603 pha.pa_mbuf_chk = ipfw_check_packet; 604 pha.pa_type = PFIL_TYPE_IP6; 605 pha.pa_rulname = "default6"; 606 h = &V_ipfw_inet6_hook; 607 break; 608 #endif 609 case AF_LINK: 610 pha.pa_mbuf_chk = ipfw_check_frame_mbuf; 611 pha.pa_mem_chk = ipfw_check_frame_mem; 612 pha.pa_type = PFIL_TYPE_ETHERNET; 613 pha.pa_rulname = "default-link"; 614 h = &V_ipfw_link_hook; 615 break; 616 } 617 618 *h = pfil_add_hook(&pha); 619 } 620 621 static void 622 ipfw_unhook(int pf) 623 { 624 625 switch (pf) { 626 case AF_INET: 627 pfil_remove_hook(V_ipfw_inet_hook); 628 break; 629 #ifdef INET6 630 case AF_INET6: 631 pfil_remove_hook(V_ipfw_inet6_hook); 632 break; 633 #endif 634 case AF_LINK: 635 pfil_remove_hook(V_ipfw_link_hook); 636 break; 637 } 638 } 639 640 static int 641 ipfw_link(int pf, bool unlink) 642 { 643 struct pfil_link_args pla; 644 645 pla.pa_version = PFIL_VERSION; 646 pla.pa_flags = PFIL_IN | PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR; 647 if (unlink) 648 pla.pa_flags |= PFIL_UNLINK; 649 650 switch (pf) { 651 case AF_INET: 652 pla.pa_head = V_inet_pfil_head; 653 pla.pa_hook = V_ipfw_inet_hook; 654 break; 655 #ifdef INET6 656 case AF_INET6: 657 pla.pa_head = V_inet6_pfil_head; 658 pla.pa_hook = V_ipfw_inet6_hook; 659 break; 660 #endif 661 case AF_LINK: 662 pla.pa_head = V_link_pfil_head; 663 pla.pa_hook = V_ipfw_link_hook; 664 break; 665 } 666 667 return (pfil_link(&pla)); 668 } 669 670 int 671 ipfw_attach_hooks(void) 672 { 673 int error = 0; 674 675 ipfw_hook(AF_INET); 676 TUNABLE_INT_FETCH("net.inet.ip.fw.enable", &V_fw_enable); 677 if (V_fw_enable && (error = ipfw_link(AF_INET, false)) != 0) 678 printf("ipfw_hook() error\n"); 679 #ifdef INET6 680 ipfw_hook(AF_INET6); 681 TUNABLE_INT_FETCH("net.inet6.ip6.fw.enable", &V_fw6_enable); 682 if (V_fw6_enable && (error = ipfw_link(AF_INET6, false)) != 0) 683 printf("ipfw6_hook() error\n"); 684 #endif 685 ipfw_hook(AF_LINK); 686 TUNABLE_INT_FETCH("net.link.ether.ipfw", &V_fwlink_enable); 687 if (V_fwlink_enable && (error = ipfw_link(AF_LINK, false)) != 0) 688 printf("ipfw_link_hook() error\n"); 689 690 return (error); 691 } 692 693 void 694 ipfw_detach_hooks(void) 695 { 696 697 ipfw_unhook(AF_INET); 698 #ifdef INET6 699 ipfw_unhook(AF_INET6); 700 #endif 701 ipfw_unhook(AF_LINK); 702 } 703 704 int 705 ipfw_chg_hook(SYSCTL_HANDLER_ARGS) 706 { 707 int newval; 708 int error; 709 int af; 710 711 if (arg1 == &V_fw_enable) 712 af = AF_INET; 713 #ifdef INET6 714 else if (arg1 == &V_fw6_enable) 715 af = AF_INET6; 716 #endif 717 else if (arg1 == &V_fwlink_enable) 718 af = AF_LINK; 719 else 720 return (EINVAL); 721 722 newval = *(int *)arg1; 723 /* Handle sysctl change */ 724 error = sysctl_handle_int(oidp, &newval, 0, req); 725 726 if (error) 727 return (error); 728 729 /* Formalize new value */ 730 newval = (newval) ? 1 : 0; 731 732 if (*(int *)arg1 == newval) 733 return (0); 734 735 error = ipfw_link(af, newval == 0 ? true : false); 736 if (error) 737 return (error); 738 *(int *)arg1 = newval; 739 740 return (0); 741 } 742 /* end of file */ 743