1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 &VNET_NAME(fw_enable), 0, ipfw_chg_hook, "I", "Enable ipfw"); 98 #ifdef INET6 99 SYSCTL_DECL(_net_inet6_ip6_fw); 100 SYSCTL_PROC(_net_inet6_ip6_fw, OID_AUTO, enable, 101 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, 102 &VNET_NAME(fw6_enable), 0, ipfw_chg_hook, "I", "Enable ipfw+6"); 103 #endif /* INET6 */ 104 105 SYSCTL_DECL(_net_link_ether); 106 SYSCTL_PROC(_net_link_ether, OID_AUTO, ipfw, 107 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, 108 &VNET_NAME(fwlink_enable), 0, ipfw_chg_hook, "I", 109 "Pass ether pkts through firewall"); 110 111 SYSEND 112 113 #endif /* SYSCTL_NODE */ 114 115 /* 116 * The pfilter hook to pass packets to ipfw_chk and then to 117 * dummynet, divert, netgraph or other modules. 118 * The packet may be consumed. 119 */ 120 static pfil_return_t 121 ipfw_check_packet(struct mbuf **m0, struct ifnet *ifp, int flags, 122 void *ruleset __unused, struct inpcb *inp) 123 { 124 struct ip_fw_args args; 125 struct m_tag *tag; 126 pfil_return_t ret; 127 int ipfw; 128 129 args.flags = (flags & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT; 130 again: 131 /* 132 * extract and remove the tag if present. If we are left 133 * with onepass, optimize the outgoing path. 134 */ 135 tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL); 136 if (tag != NULL) { 137 args.rule = *((struct ipfw_rule_ref *)(tag+1)); 138 m_tag_delete(*m0, tag); 139 if (args.rule.info & IPFW_ONEPASS) 140 return (0); 141 args.flags |= IPFW_ARGS_REF; 142 } 143 144 args.m = *m0; 145 args.ifp = ifp; 146 args.inp = inp; 147 148 ipfw = ipfw_chk(&args); 149 *m0 = args.m; 150 151 KASSERT(*m0 != NULL || ipfw == IP_FW_DENY || 152 ipfw == IP_FW_NAT64, ("%s: m0 is NULL", __func__)); 153 154 ret = PFIL_PASS; 155 switch (ipfw) { 156 case IP_FW_PASS: 157 /* next_hop may be set by ipfw_chk */ 158 if ((args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR | 159 IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) == 0) 160 break; 161 #if (!defined(INET6) && !defined(INET)) 162 ret = PFIL_DROPPED; 163 #else 164 { 165 void *psa; 166 size_t len; 167 #ifdef INET 168 if (args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR)) { 169 MPASS((args.flags & (IPFW_ARGS_NH4 | 170 IPFW_ARGS_NH4PTR)) != (IPFW_ARGS_NH4 | 171 IPFW_ARGS_NH4PTR)); 172 MPASS((args.flags & (IPFW_ARGS_NH6 | 173 IPFW_ARGS_NH6PTR)) == 0); 174 len = sizeof(struct sockaddr_in); 175 psa = (args.flags & IPFW_ARGS_NH4) ? 176 &args.hopstore : args.next_hop; 177 if (in_localip(satosin(psa)->sin_addr)) 178 (*m0)->m_flags |= M_FASTFWD_OURS; 179 (*m0)->m_flags |= M_IP_NEXTHOP; 180 } 181 #endif /* INET */ 182 #ifdef INET6 183 if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) { 184 MPASS((args.flags & (IPFW_ARGS_NH6 | 185 IPFW_ARGS_NH6PTR)) != (IPFW_ARGS_NH6 | 186 IPFW_ARGS_NH6PTR)); 187 MPASS((args.flags & (IPFW_ARGS_NH4 | 188 IPFW_ARGS_NH4PTR)) == 0); 189 len = sizeof(struct sockaddr_in6); 190 psa = args.next_hop6; 191 (*m0)->m_flags |= M_IP6_NEXTHOP; 192 } 193 #endif /* INET6 */ 194 /* 195 * Incoming packets should not be tagged so we do not 196 * m_tag_find. Outgoing packets may be tagged, so we 197 * reuse the tag if present. 198 */ 199 tag = (flags & PFIL_IN) ? NULL : 200 m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL); 201 if (tag != NULL) { 202 m_tag_unlink(*m0, tag); 203 } else { 204 tag = m_tag_get(PACKET_TAG_IPFORWARD, len, 205 M_NOWAIT); 206 if (tag == NULL) { 207 ret = PFIL_DROPPED; 208 break; 209 } 210 } 211 if ((args.flags & IPFW_ARGS_NH6) == 0) 212 bcopy(psa, tag + 1, len); 213 m_tag_prepend(*m0, tag); 214 ret = 0; 215 #ifdef INET6 216 /* IPv6 next hop needs additional handling */ 217 if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) { 218 struct sockaddr_in6 *sa6; 219 220 sa6 = satosin6(tag + 1); 221 if (args.flags & IPFW_ARGS_NH6) { 222 sa6->sin6_family = AF_INET6; 223 sa6->sin6_len = sizeof(*sa6); 224 sa6->sin6_addr = args.hopstore6.sin6_addr; 225 sa6->sin6_port = args.hopstore6.sin6_port; 226 sa6->sin6_scope_id = 227 args.hopstore6.sin6_scope_id; 228 } 229 /* 230 * If nh6 address is link-local we should convert 231 * it to kernel internal form before doing any 232 * comparisons. 233 */ 234 if (sa6_embedscope(sa6, V_ip6_use_defzone) != 0) { 235 ret = PFIL_DROPPED; 236 break; 237 } 238 if (in6_localip(&sa6->sin6_addr)) 239 (*m0)->m_flags |= M_FASTFWD_OURS; 240 } 241 #endif /* INET6 */ 242 } 243 #endif /* INET || INET6 */ 244 break; 245 246 case IP_FW_DENY: 247 ret = PFIL_DROPPED; 248 break; 249 250 case IP_FW_DUMMYNET: 251 if (ip_dn_io_ptr == NULL) { 252 ret = PFIL_DROPPED; 253 break; 254 } 255 MPASS(args.flags & IPFW_ARGS_REF); 256 if (args.flags & (IPFW_ARGS_IP4 | IPFW_ARGS_IP6)) 257 (void )ip_dn_io_ptr(m0, &args); 258 else { 259 ret = PFIL_DROPPED; 260 break; 261 } 262 /* 263 * XXX should read the return value. 264 * dummynet normally eats the packet and sets *m0=NULL 265 * unless the packet can be sent immediately. In this 266 * case args is updated and we should re-run the 267 * check without clearing args. 268 */ 269 if (*m0 != NULL) 270 goto again; 271 ret = PFIL_CONSUMED; 272 break; 273 274 case IP_FW_TEE: 275 case IP_FW_DIVERT: 276 if (ip_divert_ptr == NULL) { 277 ret = PFIL_DROPPED; 278 break; 279 } 280 MPASS(args.flags & IPFW_ARGS_REF); 281 (void )ipfw_divert(m0, &args, ipfw == IP_FW_TEE); 282 /* continue processing for the original packet (tee). */ 283 if (*m0) 284 goto again; 285 ret = PFIL_CONSUMED; 286 break; 287 288 case IP_FW_NGTEE: 289 case IP_FW_NETGRAPH: 290 if (ng_ipfw_input_p == NULL) { 291 ret = PFIL_DROPPED; 292 break; 293 } 294 MPASS(args.flags & IPFW_ARGS_REF); 295 (void )ng_ipfw_input_p(m0, &args, ipfw == IP_FW_NGTEE); 296 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */ 297 goto again; /* continue with packet */ 298 ret = PFIL_CONSUMED; 299 break; 300 301 case IP_FW_NAT: 302 /* honor one-pass in case of successful nat */ 303 if (V_fw_one_pass) 304 break; 305 goto again; 306 307 case IP_FW_REASS: 308 goto again; /* continue with packet */ 309 310 case IP_FW_NAT64: 311 ret = PFIL_CONSUMED; 312 break; 313 314 default: 315 KASSERT(0, ("%s: unknown retval", __func__)); 316 } 317 318 if (ret != PFIL_PASS) { 319 if (*m0) 320 FREE_PKT(*m0); 321 *m0 = NULL; 322 } 323 324 return (ret); 325 } 326 327 /* 328 * ipfw processing for ethernet packets (in and out). 329 */ 330 static pfil_return_t 331 ipfw_check_frame(pfil_packet_t p, struct ifnet *ifp, int flags, 332 void *ruleset __unused, struct inpcb *inp) 333 { 334 struct ip_fw_args args; 335 pfil_return_t ret; 336 bool mem, realloc; 337 int ipfw; 338 339 if (flags & PFIL_MEMPTR) { 340 mem = true; 341 realloc = false; 342 args.flags = PFIL_LENGTH(flags) | IPFW_ARGS_ETHER; 343 args.mem = p.mem; 344 } else { 345 mem = realloc = false; 346 args.flags = IPFW_ARGS_ETHER; 347 } 348 args.flags |= (flags & PFIL_IN) ? IPFW_ARGS_IN : IPFW_ARGS_OUT; 349 args.ifp = ifp; 350 args.inp = inp; 351 352 again: 353 if (!mem) { 354 /* 355 * Fetch start point from rule, if any. 356 * Remove the tag if present. 357 */ 358 struct m_tag *mtag; 359 360 mtag = m_tag_locate(*p.m, MTAG_IPFW_RULE, 0, NULL); 361 if (mtag != NULL) { 362 args.rule = *((struct ipfw_rule_ref *)(mtag+1)); 363 m_tag_delete(*p.m, mtag); 364 if (args.rule.info & IPFW_ONEPASS) 365 return (PFIL_PASS); 366 args.flags |= IPFW_ARGS_REF; 367 } 368 args.m = *p.m; 369 } 370 371 ipfw = ipfw_chk(&args); 372 373 ret = PFIL_PASS; 374 switch (ipfw) { 375 case IP_FW_PASS: 376 break; 377 378 case IP_FW_DENY: 379 ret = PFIL_DROPPED; 380 break; 381 382 case IP_FW_DUMMYNET: 383 if (ip_dn_io_ptr == NULL) { 384 ret = PFIL_DROPPED; 385 break; 386 } 387 if (mem) { 388 if (pfil_realloc(&p, flags, ifp) != 0) { 389 ret = PFIL_DROPPED; 390 break; 391 } 392 mem = false; 393 realloc = true; 394 } 395 MPASS(args.flags & IPFW_ARGS_REF); 396 ip_dn_io_ptr(p.m, &args); 397 return (PFIL_CONSUMED); 398 399 case IP_FW_NGTEE: 400 case IP_FW_NETGRAPH: 401 if (ng_ipfw_input_p == NULL) { 402 ret = PFIL_DROPPED; 403 break; 404 } 405 if (mem) { 406 if (pfil_realloc(&p, flags, ifp) != 0) { 407 ret = PFIL_DROPPED; 408 break; 409 } 410 mem = false; 411 realloc = true; 412 } 413 MPASS(args.flags & IPFW_ARGS_REF); 414 (void )ng_ipfw_input_p(p.m, &args, ipfw == IP_FW_NGTEE); 415 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */ 416 goto again; /* continue with packet */ 417 ret = PFIL_CONSUMED; 418 break; 419 420 default: 421 KASSERT(0, ("%s: unknown retval", __func__)); 422 } 423 424 if (!mem && ret != PFIL_PASS) { 425 if (*p.m) 426 FREE_PKT(*p.m); 427 *p.m = NULL; 428 } 429 430 if (realloc && ret == PFIL_PASS) 431 ret = PFIL_REALLOCED; 432 433 return (ret); 434 } 435 436 /* do the divert, return 1 on error 0 on success */ 437 static int 438 ipfw_divert(struct mbuf **m0, struct ip_fw_args *args, bool tee) 439 { 440 /* 441 * ipfw_chk() has already tagged the packet with the divert tag. 442 * If tee is set, copy packet and return original. 443 * If not tee, consume packet and send it to divert socket. 444 */ 445 struct mbuf *clone; 446 struct ip *ip = mtod(*m0, struct ip *); 447 struct m_tag *tag; 448 449 /* Cloning needed for tee? */ 450 if (tee == false) { 451 clone = *m0; /* use the original mbuf */ 452 *m0 = NULL; 453 } else { 454 clone = m_dup(*m0, M_NOWAIT); 455 /* If we cannot duplicate the mbuf, we sacrifice the divert 456 * chain and continue with the tee-ed packet. 457 */ 458 if (clone == NULL) 459 return 1; 460 } 461 462 /* 463 * Divert listeners can normally handle non-fragmented packets, 464 * but we can only reass in the non-tee case. 465 * This means that listeners on a tee rule may get fragments, 466 * and have to live with that. 467 * Note that we now have the 'reass' ipfw option so if we care 468 * we can do it before a 'tee'. 469 */ 470 if (tee == false) switch (ip->ip_v) { 471 case IPVERSION: 472 if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) { 473 int hlen; 474 struct mbuf *reass; 475 476 reass = ip_reass(clone); /* Reassemble packet. */ 477 if (reass == NULL) 478 return 0; /* not an error */ 479 /* if reass = NULL then it was consumed by ip_reass */ 480 /* 481 * IP header checksum fixup after reassembly and leave header 482 * in network byte order. 483 */ 484 ip = mtod(reass, struct ip *); 485 hlen = ip->ip_hl << 2; 486 ip->ip_sum = 0; 487 if (hlen == sizeof(struct ip)) 488 ip->ip_sum = in_cksum_hdr(ip); 489 else 490 ip->ip_sum = in_cksum(reass, hlen); 491 clone = reass; 492 } 493 break; 494 #ifdef INET6 495 case IPV6_VERSION >> 4: 496 { 497 struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *); 498 499 if (ip6->ip6_nxt == IPPROTO_FRAGMENT) { 500 int nxt, off; 501 502 off = sizeof(struct ip6_hdr); 503 nxt = frag6_input(&clone, &off, 0); 504 if (nxt == IPPROTO_DONE) 505 return (0); 506 } 507 break; 508 } 509 #endif 510 } 511 512 /* attach a tag to the packet with the reinject info */ 513 tag = m_tag_alloc(MTAG_IPFW_RULE, 0, 514 sizeof(struct ipfw_rule_ref), M_NOWAIT); 515 if (tag == NULL) { 516 FREE_PKT(clone); 517 return 1; 518 } 519 *((struct ipfw_rule_ref *)(tag+1)) = args->rule; 520 m_tag_prepend(clone, tag); 521 522 /* Do the dirty job... */ 523 ip_divert_ptr(clone, args->flags & IPFW_ARGS_IN); 524 return 0; 525 } 526 527 /* 528 * attach or detach hooks for a given protocol family 529 */ 530 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet_hook); 531 #define V_ipfw_inet_hook VNET(ipfw_inet_hook) 532 #ifdef INET6 533 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_inet6_hook); 534 #define V_ipfw_inet6_hook VNET(ipfw_inet6_hook) 535 #endif 536 VNET_DEFINE_STATIC(pfil_hook_t, ipfw_link_hook); 537 #define V_ipfw_link_hook VNET(ipfw_link_hook) 538 539 static int 540 ipfw_hook(int onoff, int pf) 541 { 542 struct pfil_hook_args pha; 543 struct pfil_link_args pla; 544 pfil_hook_t *h; 545 546 pha.pa_version = PFIL_VERSION; 547 pha.pa_flags = PFIL_IN | PFIL_OUT | PFIL_MEMPTR; 548 pha.pa_modname = "ipfw"; 549 pha.pa_ruleset = NULL; 550 551 pla.pa_version = PFIL_VERSION; 552 pla.pa_flags = PFIL_IN | PFIL_OUT | 553 PFIL_HEADPTR | PFIL_HOOKPTR; 554 555 switch (pf) { 556 case AF_INET: 557 pha.pa_func = ipfw_check_packet; 558 pha.pa_type = PFIL_TYPE_IP4; 559 pha.pa_rulname = "default"; 560 h = &V_ipfw_inet_hook; 561 pla.pa_head = V_inet_pfil_head; 562 break; 563 #ifdef INET6 564 case AF_INET6: 565 pha.pa_func = ipfw_check_packet; 566 pha.pa_type = PFIL_TYPE_IP6; 567 pha.pa_rulname = "default6"; 568 h = &V_ipfw_inet6_hook; 569 pla.pa_head = V_inet6_pfil_head; 570 break; 571 #endif 572 case AF_LINK: 573 pha.pa_func = ipfw_check_frame; 574 pha.pa_type = PFIL_TYPE_ETHERNET; 575 pha.pa_rulname = "default-link"; 576 h = &V_ipfw_link_hook; 577 pla.pa_head = V_link_pfil_head; 578 break; 579 } 580 581 if (onoff) { 582 *h = pfil_add_hook(&pha); 583 pla.pa_hook = *h; 584 (void)pfil_link(&pla); 585 } else 586 if (*h != NULL) 587 pfil_remove_hook(*h); 588 589 return 0; 590 } 591 592 int 593 ipfw_attach_hooks(int arg) 594 { 595 int error = 0; 596 597 if (arg == 0) /* detach */ 598 ipfw_hook(0, AF_INET); 599 else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) { 600 error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */ 601 printf("ipfw_hook() error\n"); 602 } 603 #ifdef INET6 604 if (arg == 0) /* detach */ 605 ipfw_hook(0, AF_INET6); 606 else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) { 607 error = ENOENT; 608 printf("ipfw6_hook() error\n"); 609 } 610 #endif 611 if (arg == 0) /* detach */ 612 ipfw_hook(0, AF_LINK); 613 else if (V_fwlink_enable && ipfw_hook(1, AF_LINK) != 0) { 614 error = ENOENT; 615 printf("ipfw_link_hook() error\n"); 616 } 617 return error; 618 } 619 620 int 621 ipfw_chg_hook(SYSCTL_HANDLER_ARGS) 622 { 623 int newval; 624 int error; 625 int af; 626 627 if (arg1 == &V_fw_enable) 628 af = AF_INET; 629 #ifdef INET6 630 else if (arg1 == &V_fw6_enable) 631 af = AF_INET6; 632 #endif 633 else if (arg1 == &V_fwlink_enable) 634 af = AF_LINK; 635 else 636 return (EINVAL); 637 638 newval = *(int *)arg1; 639 /* Handle sysctl change */ 640 error = sysctl_handle_int(oidp, &newval, 0, req); 641 642 if (error) 643 return (error); 644 645 /* Formalize new value */ 646 newval = (newval) ? 1 : 0; 647 648 if (*(int *)arg1 == newval) 649 return (0); 650 651 error = ipfw_hook(newval, af); 652 if (error) 653 return (error); 654 *(int *)arg1 = newval; 655 656 return (0); 657 } 658 /* end of file */ 659