ip_fw_pfil.c revision 241913
1/*- 2 * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/netpfil/ipfw/ip_fw_pfil.c 241913 2012-10-22 21:09:03Z glebius $"); 29 30#include "opt_ipfw.h" 31#include "opt_inet.h" 32#include "opt_inet6.h" 33#ifndef INET 34#error IPFIREWALL requires INET. 35#endif /* INET */ 36 37#include <sys/param.h> 38#include <sys/systm.h> 39#include <sys/malloc.h> 40#include <sys/mbuf.h> 41#include <sys/module.h> 42#include <sys/kernel.h> 43#include <sys/lock.h> 44#include <sys/rwlock.h> 45#include <sys/socket.h> 46#include <sys/sysctl.h> 47 48#include <net/if.h> 49#include <net/route.h> 50#include <net/ethernet.h> 51#include <net/pfil.h> 52#include <net/vnet.h> 53 54#include <netinet/in.h> 55#include <netinet/in_systm.h> 56#include <netinet/ip.h> 57#include <netinet/ip_var.h> 58#include <netinet/ip_fw.h> 59#ifdef INET6 60#include <netinet/ip6.h> 61#include <netinet6/ip6_var.h> 62#endif 63 64#include <netgraph/ng_ipfw.h> 65 66#include <netpfil/ipfw/ip_fw_private.h> 67 68#include <machine/in_cksum.h> 69 70static VNET_DEFINE(int, fw_enable) = 1; 71#define V_fw_enable VNET(fw_enable) 72 73#ifdef INET6 74static VNET_DEFINE(int, fw6_enable) = 1; 75#define V_fw6_enable VNET(fw6_enable) 76#endif 77 78static VNET_DEFINE(int, fwlink_enable) = 0; 79#define V_fwlink_enable VNET(fwlink_enable) 80 81int ipfw_chg_hook(SYSCTL_HANDLER_ARGS); 82 83/* Forward declarations. */ 84static int ipfw_divert(struct mbuf **, int, struct ipfw_rule_ref *, int); 85static int ipfw_check_packet(void *, struct mbuf **, struct ifnet *, int, 86 struct inpcb *); 87static int ipfw_check_frame(void *, struct mbuf **, struct ifnet *, int, 88 struct inpcb *); 89 90#ifdef SYSCTL_NODE 91 92SYSBEGIN(f1) 93 94SYSCTL_DECL(_net_inet_ip_fw); 95SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, enable, 96 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_enable), 0, 97 ipfw_chg_hook, "I", "Enable ipfw"); 98#ifdef INET6 99SYSCTL_DECL(_net_inet6_ip6_fw); 100SYSCTL_VNET_PROC(_net_inet6_ip6_fw, OID_AUTO, enable, 101 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw6_enable), 0, 102 ipfw_chg_hook, "I", "Enable ipfw+6"); 103#endif /* INET6 */ 104 105SYSCTL_DECL(_net_link_ether); 106SYSCTL_VNET_PROC(_net_link_ether, OID_AUTO, ipfw, 107 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fwlink_enable), 0, 108 ipfw_chg_hook, "I", "Pass ether pkts through firewall"); 109 110SYSEND 111 112#endif /* SYSCTL_NODE */ 113 114/* 115 * The pfilter hook to pass packets to ipfw_chk and then to 116 * dummynet, divert, netgraph or other modules. 117 * The packet may be consumed. 118 */ 119static int 120ipfw_check_packet(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir, 121 struct inpcb *inp) 122{ 123 struct ip_fw_args args; 124 struct m_tag *tag; 125 int ipfw; 126 int ret; 127 128 /* convert dir to IPFW values */ 129 dir = (dir == PFIL_IN) ? DIR_IN : DIR_OUT; 130 bzero(&args, sizeof(args)); 131 132again: 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 (0); 143 } 144 145 args.m = *m0; 146 args.oif = dir == DIR_OUT ? ifp : NULL; 147 args.inp = inp; 148 149 ipfw = ipfw_chk(&args); 150 *m0 = args.m; 151 152 KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL", 153 __func__)); 154 155 /* breaking out of the switch means drop */ 156 ret = 0; /* default return value for pass */ 157 switch (ipfw) { 158 case IP_FW_PASS: 159 /* next_hop may be set by ipfw_chk */ 160 if (args.next_hop == NULL && args.next_hop6 == NULL) 161 break; /* pass */ 162#if !defined(IPFIREWALL_FORWARD) || (!defined(INET6) && !defined(INET)) 163 ret = EACCES; 164#else 165 { 166 struct m_tag *fwd_tag; 167 size_t len; 168 169 KASSERT(args.next_hop == NULL || args.next_hop6 == NULL, 170 ("%s: both next_hop=%p and next_hop6=%p not NULL", __func__, 171 args.next_hop, args.next_hop6)); 172#ifdef INET6 173 if (args.next_hop6 != NULL) 174 len = sizeof(struct sockaddr_in6); 175#endif 176#ifdef INET 177 if (args.next_hop != NULL) 178 len = sizeof(struct sockaddr_in); 179#endif 180 181 /* Incoming packets should not be tagged so we do not 182 * m_tag_find. Outgoing packets may be tagged, so we 183 * reuse the tag if present. 184 */ 185 fwd_tag = (dir == DIR_IN) ? NULL : 186 m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL); 187 if (fwd_tag != NULL) { 188 m_tag_unlink(*m0, fwd_tag); 189 } else { 190 fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD, len, 191 M_NOWAIT); 192 if (fwd_tag == NULL) { 193 ret = EACCES; 194 break; /* i.e. drop */ 195 } 196 } 197#ifdef INET6 198 if (args.next_hop6 != NULL) { 199 bcopy(args.next_hop6, (fwd_tag+1), len); 200 if (in6_localip(&args.next_hop6->sin6_addr)) 201 (*m0)->m_flags |= M_FASTFWD_OURS; 202 } 203#endif 204#ifdef INET 205 if (args.next_hop != NULL) { 206 bcopy(args.next_hop, (fwd_tag+1), len); 207 if (in_localip(args.next_hop->sin_addr)) 208 (*m0)->m_flags |= M_FASTFWD_OURS; 209 } 210#endif 211 m_tag_prepend(*m0, fwd_tag); 212 } 213#endif /* IPFIREWALL_FORWARD */ 214 break; 215 216 case IP_FW_DENY: 217 ret = EACCES; 218 break; /* i.e. drop */ 219 220 case IP_FW_DUMMYNET: 221 ret = EACCES; 222 if (ip_dn_io_ptr == NULL) 223 break; /* i.e. drop */ 224 if (mtod(*m0, struct ip *)->ip_v == 4) 225 ret = ip_dn_io_ptr(m0, dir, &args); 226 else if (mtod(*m0, struct ip *)->ip_v == 6) 227 ret = ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args); 228 else 229 break; /* drop it */ 230 /* 231 * XXX should read the return value. 232 * dummynet normally eats the packet and sets *m0=NULL 233 * unless the packet can be sent immediately. In this 234 * case args is updated and we should re-run the 235 * check without clearing args. 236 */ 237 if (*m0 != NULL) 238 goto again; 239 break; 240 241 case IP_FW_TEE: 242 case IP_FW_DIVERT: 243 if (ip_divert_ptr == NULL) { 244 ret = EACCES; 245 break; /* i.e. drop */ 246 } 247 ret = ipfw_divert(m0, dir, &args.rule, 248 (ipfw == IP_FW_TEE) ? 1 : 0); 249 /* continue processing for the original packet (tee). */ 250 if (*m0) 251 goto again; 252 break; 253 254 case IP_FW_NGTEE: 255 case IP_FW_NETGRAPH: 256 if (ng_ipfw_input_p == NULL) { 257 ret = EACCES; 258 break; /* i.e. drop */ 259 } 260 ret = ng_ipfw_input_p(m0, dir, &args, 261 (ipfw == IP_FW_NGTEE) ? 1 : 0); 262 if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */ 263 goto again; /* continue with packet */ 264 break; 265 266 case IP_FW_NAT: 267 /* honor one-pass in case of successful nat */ 268 if (V_fw_one_pass) 269 break; /* ret is already 0 */ 270 goto again; 271 272 case IP_FW_REASS: 273 goto again; /* continue with packet */ 274 275 default: 276 KASSERT(0, ("%s: unknown retval", __func__)); 277 } 278 279 if (ret != 0) { 280 if (*m0) 281 FREE_PKT(*m0); 282 *m0 = NULL; 283 } 284 285 return ret; 286} 287 288/* 289 * ipfw processing for ethernet packets (in and out). 290 * Inteface is NULL from ether_demux, and ifp from 291 * ether_output_frame. 292 */ 293static int 294ipfw_check_frame(void *arg, struct mbuf **m0, struct ifnet *dst, int dir, 295 struct inpcb *inp) 296{ 297 struct ether_header *eh; 298 struct ether_header save_eh; 299 struct mbuf *m; 300 int i, ret; 301 struct ip_fw_args args; 302 struct m_tag *mtag; 303 304 /* fetch start point from rule, if any */ 305 mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL); 306 if (mtag == NULL) { 307 args.rule.slot = 0; 308 } else { 309 /* dummynet packet, already partially processed */ 310 struct ipfw_rule_ref *r; 311 312 /* XXX can we free it after use ? */ 313 mtag->m_tag_id = PACKET_TAG_NONE; 314 r = (struct ipfw_rule_ref *)(mtag + 1); 315 if (r->info & IPFW_ONEPASS) 316 return (0); 317 args.rule = *r; 318 } 319 320 /* I need some amt of data to be contiguous */ 321 m = *m0; 322 i = min(m->m_pkthdr.len, max_protohdr); 323 if (m->m_len < i) { 324 m = m_pullup(m, i); 325 if (m == NULL) { 326 *m0 = m; 327 return (0); 328 } 329 } 330 eh = mtod(m, struct ether_header *); 331 save_eh = *eh; /* save copy for restore below */ 332 m_adj(m, ETHER_HDR_LEN); /* strip ethernet header */ 333 334 args.m = m; /* the packet we are looking at */ 335 args.oif = dst; /* destination, if any */ 336 args.next_hop = NULL; /* we do not support forward yet */ 337 args.next_hop6 = NULL; /* we do not support forward yet */ 338 args.eh = &save_eh; /* MAC header for bridged/MAC packets */ 339 args.inp = NULL; /* used by ipfw uid/gid/jail rules */ 340 i = ipfw_chk(&args); 341 m = args.m; 342 if (m != NULL) { 343 /* 344 * Restore Ethernet header, as needed, in case the 345 * mbuf chain was replaced by ipfw. 346 */ 347 M_PREPEND(m, ETHER_HDR_LEN, M_DONTWAIT); 348 if (m == NULL) { 349 *m0 = NULL; 350 return (0); 351 } 352 if (eh != mtod(m, struct ether_header *)) 353 bcopy(&save_eh, mtod(m, struct ether_header *), 354 ETHER_HDR_LEN); 355 } 356 *m0 = m; 357 358 ret = 0; 359 /* Check result of ipfw_chk() */ 360 switch (i) { 361 case IP_FW_PASS: 362 break; 363 364 case IP_FW_DENY: 365 ret = EACCES; 366 break; /* i.e. drop */ 367 368 case IP_FW_DUMMYNET: 369 ret = EACCES; 370 int dir; 371 372 if (ip_dn_io_ptr == NULL) 373 break; /* i.e. drop */ 374 375 *m0 = NULL; 376 dir = PROTO_LAYER2 | (dst ? DIR_OUT : DIR_IN); 377 ip_dn_io_ptr(&m, dir, &args); 378 return 0; 379 380 default: 381 KASSERT(0, ("%s: unknown retval", __func__)); 382 } 383 384 if (ret != 0) { 385 if (*m0) 386 FREE_PKT(*m0); 387 *m0 = NULL; 388 } 389 390 return ret; 391} 392 393/* do the divert, return 1 on error 0 on success */ 394static int 395ipfw_divert(struct mbuf **m0, int incoming, struct ipfw_rule_ref *rule, 396 int tee) 397{ 398 /* 399 * ipfw_chk() has already tagged the packet with the divert tag. 400 * If tee is set, copy packet and return original. 401 * If not tee, consume packet and send it to divert socket. 402 */ 403 struct mbuf *clone; 404 struct ip *ip = mtod(*m0, struct ip *); 405 struct m_tag *tag; 406 407 /* Cloning needed for tee? */ 408 if (tee == 0) { 409 clone = *m0; /* use the original mbuf */ 410 *m0 = NULL; 411 } else { 412 clone = m_dup(*m0, M_DONTWAIT); 413 /* If we cannot duplicate the mbuf, we sacrifice the divert 414 * chain and continue with the tee-ed packet. 415 */ 416 if (clone == NULL) 417 return 1; 418 } 419 420 /* 421 * Divert listeners can normally handle non-fragmented packets, 422 * but we can only reass in the non-tee case. 423 * This means that listeners on a tee rule may get fragments, 424 * and have to live with that. 425 * Note that we now have the 'reass' ipfw option so if we care 426 * we can do it before a 'tee'. 427 */ 428 if (!tee) switch (ip->ip_v) { 429 case IPVERSION: 430 if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) { 431 int hlen; 432 struct mbuf *reass; 433 434 reass = ip_reass(clone); /* Reassemble packet. */ 435 if (reass == NULL) 436 return 0; /* not an error */ 437 /* if reass = NULL then it was consumed by ip_reass */ 438 /* 439 * IP header checksum fixup after reassembly and leave header 440 * in network byte order. 441 */ 442 ip = mtod(reass, struct ip *); 443 hlen = ip->ip_hl << 2; 444 ip->ip_sum = 0; 445 if (hlen == sizeof(struct ip)) 446 ip->ip_sum = in_cksum_hdr(ip); 447 else 448 ip->ip_sum = in_cksum(reass, hlen); 449 clone = reass; 450 } 451 break; 452#ifdef INET6 453 case IPV6_VERSION >> 4: 454 { 455 struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *); 456 457 if (ip6->ip6_nxt == IPPROTO_FRAGMENT) { 458 int nxt, off; 459 460 off = sizeof(struct ip6_hdr); 461 nxt = frag6_input(&clone, &off, 0); 462 if (nxt == IPPROTO_DONE) 463 return (0); 464 } 465 break; 466 } 467#endif 468 } 469 470 /* attach a tag to the packet with the reinject info */ 471 tag = m_tag_alloc(MTAG_IPFW_RULE, 0, 472 sizeof(struct ipfw_rule_ref), M_NOWAIT); 473 if (tag == NULL) { 474 FREE_PKT(clone); 475 return 1; 476 } 477 *((struct ipfw_rule_ref *)(tag+1)) = *rule; 478 m_tag_prepend(clone, tag); 479 480 /* Do the dirty job... */ 481 ip_divert_ptr(clone, incoming); 482 return 0; 483} 484 485/* 486 * attach or detach hooks for a given protocol family 487 */ 488static int 489ipfw_hook(int onoff, int pf) 490{ 491 struct pfil_head *pfh; 492 void *hook_func; 493 494 pfh = pfil_head_get(PFIL_TYPE_AF, pf); 495 if (pfh == NULL) 496 return ENOENT; 497 498 hook_func = (pf == AF_LINK) ? ipfw_check_frame : ipfw_check_packet; 499 500 (void) (onoff ? pfil_add_hook : pfil_remove_hook) 501 (hook_func, NULL, PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh); 502 503 return 0; 504} 505 506int 507ipfw_attach_hooks(int arg) 508{ 509 int error = 0; 510 511 if (arg == 0) /* detach */ 512 ipfw_hook(0, AF_INET); 513 else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) { 514 error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */ 515 printf("ipfw_hook() error\n"); 516 } 517#ifdef INET6 518 if (arg == 0) /* detach */ 519 ipfw_hook(0, AF_INET6); 520 else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) { 521 error = ENOENT; 522 printf("ipfw6_hook() error\n"); 523 } 524#endif 525 if (arg == 0) /* detach */ 526 ipfw_hook(0, AF_LINK); 527 else if (V_fwlink_enable && ipfw_hook(1, AF_LINK) != 0) { 528 error = ENOENT; 529 printf("ipfw_link_hook() error\n"); 530 } 531 return error; 532} 533 534int 535ipfw_chg_hook(SYSCTL_HANDLER_ARGS) 536{ 537 int *enable; 538 int newval; 539 int error; 540 int af; 541 542 if (arg1 == &VNET_NAME(fw_enable)) { 543 enable = &V_fw_enable; 544 af = AF_INET; 545 } 546#ifdef INET6 547 else if (arg1 == &VNET_NAME(fw6_enable)) { 548 enable = &V_fw6_enable; 549 af = AF_INET6; 550 } 551#endif 552 else if (arg1 == &VNET_NAME(fwlink_enable)) { 553 enable = &V_fwlink_enable; 554 af = AF_LINK; 555 } 556 else 557 return (EINVAL); 558 559 newval = *enable; 560 561 /* Handle sysctl change */ 562 error = sysctl_handle_int(oidp, &newval, 0, req); 563 564 if (error) 565 return (error); 566 567 /* Formalize new value */ 568 newval = (newval) ? 1 : 0; 569 570 if (*enable == newval) 571 return (0); 572 573 error = ipfw_hook(newval, af); 574 if (error) 575 return (error); 576 *enable = newval; 577 578 return (0); 579} 580/* end of file */ 581