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