ip_fil_freebsd.c revision 341379
1/* $FreeBSD: stable/10/sys/contrib/ipfilter/netinet/ip_fil_freebsd.c 341379 2018-12-01 18:17:51Z cy $ */ 2 3/* 4 * Copyright (C) 2012 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 */ 8#if !defined(lint) 9static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed"; 10static const char rcsid[] = "@(#)$Id$"; 11#endif 12 13#if defined(KERNEL) || defined(_KERNEL) 14# undef KERNEL 15# undef _KERNEL 16# define KERNEL 1 17# define _KERNEL 1 18#endif 19#if defined(__FreeBSD_version) && (__FreeBSD_version >= 400000) && \ 20 !defined(KLD_MODULE) && !defined(IPFILTER_LKM) 21# include "opt_inet6.h" 22#endif 23#if defined(__FreeBSD_version) && (__FreeBSD_version >= 440000) && \ 24 !defined(KLD_MODULE) && !defined(IPFILTER_LKM) 25# include "opt_random_ip_id.h" 26#endif 27#include <sys/param.h> 28#include <sys/errno.h> 29#include <sys/types.h> 30#include <sys/file.h> 31# include <sys/fcntl.h> 32# include <sys/filio.h> 33#include <sys/time.h> 34#include <sys/systm.h> 35# include <sys/dirent.h> 36#if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000) 37#include <sys/jail.h> 38#endif 39# include <sys/mbuf.h> 40# include <sys/sockopt.h> 41#if !defined(__hpux) 42# include <sys/mbuf.h> 43#endif 44#include <sys/socket.h> 45# include <sys/selinfo.h> 46# include <netinet/tcp_var.h> 47 48#include <net/if.h> 49# include <net/if_var.h> 50# include <net/netisr.h> 51#include <net/route.h> 52#include <netinet/in.h> 53#include <netinet/in_var.h> 54#include <netinet/in_systm.h> 55#include <netinet/ip.h> 56#include <netinet/ip_var.h> 57#include <netinet/tcp.h> 58#if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000) 59#include <net/vnet.h> 60#else 61#define CURVNET_SET(arg) 62#define CURVNET_RESTORE() 63#endif 64#include <netinet/udp.h> 65#include <netinet/tcpip.h> 66#include <netinet/ip_icmp.h> 67#include "netinet/ip_compat.h" 68#ifdef USE_INET6 69# include <netinet/icmp6.h> 70#endif 71#include "netinet/ip_fil.h" 72#include "netinet/ip_nat.h" 73#include "netinet/ip_frag.h" 74#include "netinet/ip_state.h" 75#include "netinet/ip_proxy.h" 76#include "netinet/ip_auth.h" 77#include "netinet/ip_sync.h" 78#include "netinet/ip_lookup.h" 79#include "netinet/ip_dstlist.h" 80#ifdef IPFILTER_SCAN 81#include "netinet/ip_scan.h" 82#endif 83#include "netinet/ip_pool.h" 84# include <sys/malloc.h> 85#include <sys/kernel.h> 86#ifdef CSUM_DATA_VALID 87#include <machine/in_cksum.h> 88#endif 89extern int ip_optcopy __P((struct ip *, struct ip *)); 90 91 92# ifdef IPFILTER_M_IPFILTER 93MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures"); 94# endif 95 96 97static u_short ipid = 0; 98static int (*ipf_savep) __P((void *, ip_t *, int, void *, int, struct mbuf **)); 99static int ipf_send_ip __P((fr_info_t *, mb_t *)); 100static void ipf_timer_func __P((void *arg)); 101int ipf_locks_done = 0; 102 103ipf_main_softc_t ipfmain; 104 105# include <sys/conf.h> 106# if defined(NETBSD_PF) 107# include <net/pfil.h> 108# endif /* NETBSD_PF */ 109/* 110 * We provide the ipf_checkp name just to minimize changes later. 111 */ 112int (*ipf_checkp) __P((void *, ip_t *ip, int hlen, void *ifp, int out, mb_t **mp)); 113 114 115static eventhandler_tag ipf_arrivetag, ipf_departtag, ipf_clonetag; 116 117static void ipf_ifevent(void *arg); 118 119static void ipf_ifevent(arg) 120 void *arg; 121{ 122 ipf_sync(arg, NULL); 123} 124 125 126 127static int 128ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir) 129{ 130 struct ip *ip = mtod(*mp, struct ip *); 131 int rv; 132 133 /* 134 * IPFilter expects evreything in network byte order 135 */ 136#if (__FreeBSD_version < 1000019) 137 ip->ip_len = htons(ip->ip_len); 138 ip->ip_off = htons(ip->ip_off); 139#endif 140 rv = ipf_check(&ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT), 141 mp); 142#if (__FreeBSD_version < 1000019) 143 if ((rv == 0) && (*mp != NULL)) { 144 ip = mtod(*mp, struct ip *); 145 ip->ip_len = ntohs(ip->ip_len); 146 ip->ip_off = ntohs(ip->ip_off); 147 } 148#endif 149 return rv; 150} 151 152# ifdef USE_INET6 153# include <netinet/ip6.h> 154 155static int 156ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir) 157{ 158 return (ipf_check(&ipfmain, mtod(*mp, struct ip *), 159 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp)); 160} 161# endif 162#if defined(IPFILTER_LKM) 163int ipf_identify(s) 164 char *s; 165{ 166 if (strcmp(s, "ipl") == 0) 167 return 1; 168 return 0; 169} 170#endif /* IPFILTER_LKM */ 171 172 173static void 174ipf_timer_func(arg) 175 void *arg; 176{ 177 ipf_main_softc_t *softc = arg; 178 SPL_INT(s); 179 180 SPL_NET(s); 181 READ_ENTER(&softc->ipf_global); 182 183 if (softc->ipf_running > 0) 184 ipf_slowtimer(softc); 185 186 if (softc->ipf_running == -1 || softc->ipf_running == 1) { 187#if 0 188 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2); 189#endif 190 callout_init(&softc->ipf_slow_ch, 1); 191 callout_reset(&softc->ipf_slow_ch, 192 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT, 193 ipf_timer_func, softc); 194 } 195 RWLOCK_EXIT(&softc->ipf_global); 196 SPL_X(s); 197} 198 199 200int 201ipfattach(softc) 202 ipf_main_softc_t *softc; 203{ 204#ifdef USE_SPL 205 int s; 206#endif 207 208 SPL_NET(s); 209 if (softc->ipf_running > 0) { 210 SPL_X(s); 211 return EBUSY; 212 } 213 214 if (ipf_init_all(softc) < 0) { 215 SPL_X(s); 216 return EIO; 217 } 218 219 220 if (ipf_checkp != ipf_check) { 221 ipf_savep = ipf_checkp; 222 ipf_checkp = ipf_check; 223 } 224 225 bzero((char *)ipfmain.ipf_selwait, sizeof(ipfmain.ipf_selwait)); 226 softc->ipf_running = 1; 227 228 if (softc->ipf_control_forwarding & 1) 229 V_ipforwarding = 1; 230 231 ipid = 0; 232 233 SPL_X(s); 234#if 0 235 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, 236 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT); 237#endif 238 callout_init(&softc->ipf_slow_ch, 1); 239 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT, 240 ipf_timer_func, softc); 241 return 0; 242} 243 244 245/* 246 * Disable the filter by removing the hooks from the IP input/output 247 * stream. 248 */ 249int 250ipfdetach(softc) 251 ipf_main_softc_t *softc; 252{ 253#ifdef USE_SPL 254 int s; 255#endif 256 257 if (softc->ipf_control_forwarding & 2) 258 V_ipforwarding = 0; 259 260 SPL_NET(s); 261 262#if 0 263 if (softc->ipf_slow_ch.callout != NULL) 264 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch); 265 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow)); 266#endif 267 callout_drain(&softc->ipf_slow_ch); 268 269#ifndef NETBSD_PF 270 if (ipf_checkp != NULL) 271 ipf_checkp = ipf_savep; 272 ipf_savep = NULL; 273#endif 274 275 ipf_fini_all(softc); 276 277 softc->ipf_running = -2; 278 279 SPL_X(s); 280 281 return 0; 282} 283 284 285/* 286 * Filter ioctl interface. 287 */ 288int 289ipfioctl(dev, cmd, data, mode, p) 290 struct thread *p; 291# define p_cred td_ucred 292# define p_uid td_ucred->cr_ruid 293 struct cdev *dev; 294 ioctlcmd_t cmd; 295 caddr_t data; 296 int mode; 297{ 298 int error = 0, unit = 0; 299 SPL_INT(s); 300 301#if (BSD >= 199306) 302 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE)) 303 { 304 ipfmain.ipf_interror = 130001; 305 return EPERM; 306 } 307#endif 308 309 unit = GET_MINOR(dev); 310 if ((IPL_LOGMAX < unit) || (unit < 0)) { 311 ipfmain.ipf_interror = 130002; 312 return ENXIO; 313 } 314 315 if (ipfmain.ipf_running <= 0) { 316 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) { 317 ipfmain.ipf_interror = 130003; 318 return EIO; 319 } 320 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET && 321 cmd != SIOCIPFSET && cmd != SIOCFRENB && 322 cmd != SIOCGETFS && cmd != SIOCGETFF && 323 cmd != SIOCIPFINTERROR) { 324 ipfmain.ipf_interror = 130004; 325 return EIO; 326 } 327 } 328 329 SPL_NET(s); 330 331 CURVNET_SET(TD_TO_VNET(p)); 332 error = ipf_ioctlswitch(&ipfmain, unit, data, cmd, mode, p->p_uid, p); 333 CURVNET_RESTORE(); 334 if (error != -1) { 335 SPL_X(s); 336 return error; 337 } 338 339 SPL_X(s); 340 341 return error; 342} 343 344 345/* 346 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that 347 * requires a large amount of setting up and isn't any more efficient. 348 */ 349int 350ipf_send_reset(fin) 351 fr_info_t *fin; 352{ 353 struct tcphdr *tcp, *tcp2; 354 int tlen = 0, hlen; 355 struct mbuf *m; 356#ifdef USE_INET6 357 ip6_t *ip6; 358#endif 359 ip_t *ip; 360 361 tcp = fin->fin_dp; 362 if (tcp->th_flags & TH_RST) 363 return -1; /* feedback loop */ 364 365 if (ipf_checkl4sum(fin) == -1) 366 return -1; 367 368 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) + 369 ((tcp->th_flags & TH_SYN) ? 1 : 0) + 370 ((tcp->th_flags & TH_FIN) ? 1 : 0); 371 372#ifdef USE_INET6 373 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t); 374#else 375 hlen = sizeof(ip_t); 376#endif 377#ifdef MGETHDR 378 MGETHDR(m, M_DONTWAIT, MT_HEADER); 379#else 380 MGET(m, M_DONTWAIT, MT_HEADER); 381#endif 382 if (m == NULL) 383 return -1; 384 if (sizeof(*tcp2) + hlen > MLEN) { 385 MCLGET(m, M_DONTWAIT); 386 if ((m->m_flags & M_EXT) == 0) { 387 FREE_MB_T(m); 388 return -1; 389 } 390 } 391 392 m->m_len = sizeof(*tcp2) + hlen; 393#if (BSD >= 199103) 394 m->m_data += max_linkhdr; 395 m->m_pkthdr.len = m->m_len; 396 m->m_pkthdr.rcvif = (struct ifnet *)0; 397#endif 398 ip = mtod(m, struct ip *); 399 bzero((char *)ip, hlen); 400#ifdef USE_INET6 401 ip6 = (ip6_t *)ip; 402#endif 403 tcp2 = (struct tcphdr *)((char *)ip + hlen); 404 tcp2->th_sport = tcp->th_dport; 405 tcp2->th_dport = tcp->th_sport; 406 407 if (tcp->th_flags & TH_ACK) { 408 tcp2->th_seq = tcp->th_ack; 409 tcp2->th_flags = TH_RST; 410 tcp2->th_ack = 0; 411 } else { 412 tcp2->th_seq = 0; 413 tcp2->th_ack = ntohl(tcp->th_seq); 414 tcp2->th_ack += tlen; 415 tcp2->th_ack = htonl(tcp2->th_ack); 416 tcp2->th_flags = TH_RST|TH_ACK; 417 } 418 TCP_X2_A(tcp2, 0); 419 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2); 420 tcp2->th_win = tcp->th_win; 421 tcp2->th_sum = 0; 422 tcp2->th_urp = 0; 423 424#ifdef USE_INET6 425 if (fin->fin_v == 6) { 426 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow; 427 ip6->ip6_plen = htons(sizeof(struct tcphdr)); 428 ip6->ip6_nxt = IPPROTO_TCP; 429 ip6->ip6_hlim = 0; 430 ip6->ip6_src = fin->fin_dst6.in6; 431 ip6->ip6_dst = fin->fin_src6.in6; 432 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP, 433 sizeof(*ip6), sizeof(*tcp2)); 434 return ipf_send_ip(fin, m); 435 } 436#endif 437 ip->ip_p = IPPROTO_TCP; 438 ip->ip_len = htons(sizeof(struct tcphdr)); 439 ip->ip_src.s_addr = fin->fin_daddr; 440 ip->ip_dst.s_addr = fin->fin_saddr; 441 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2)); 442 ip->ip_len = htons(hlen + sizeof(*tcp2)); 443 return ipf_send_ip(fin, m); 444} 445 446 447/* 448 * ip_len must be in network byte order when called. 449 */ 450static int 451ipf_send_ip(fin, m) 452 fr_info_t *fin; 453 mb_t *m; 454{ 455 fr_info_t fnew; 456 ip_t *ip, *oip; 457 int hlen; 458 459 ip = mtod(m, ip_t *); 460 bzero((char *)&fnew, sizeof(fnew)); 461 fnew.fin_main_soft = fin->fin_main_soft; 462 463 IP_V_A(ip, fin->fin_v); 464 switch (fin->fin_v) 465 { 466 case 4 : 467 oip = fin->fin_ip; 468 hlen = sizeof(*oip); 469 fnew.fin_v = 4; 470 fnew.fin_p = ip->ip_p; 471 fnew.fin_plen = ntohs(ip->ip_len); 472 IP_HL_A(ip, sizeof(*oip) >> 2); 473 ip->ip_tos = oip->ip_tos; 474 ip->ip_id = fin->fin_ip->ip_id; 475#if defined(FreeBSD) && (__FreeBSD_version > 460000) 476 ip->ip_off = htons(path_mtu_discovery ? IP_DF : 0); 477#else 478 ip->ip_off = 0; 479#endif 480 ip->ip_ttl = V_ip_defttl; 481 ip->ip_sum = 0; 482 break; 483#ifdef USE_INET6 484 case 6 : 485 { 486 ip6_t *ip6 = (ip6_t *)ip; 487 488 ip6->ip6_vfc = 0x60; 489 ip6->ip6_hlim = IPDEFTTL; 490 491 hlen = sizeof(*ip6); 492 fnew.fin_p = ip6->ip6_nxt; 493 fnew.fin_v = 6; 494 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen; 495 break; 496 } 497#endif 498 default : 499 return EINVAL; 500 } 501#ifdef IPSEC 502 m->m_pkthdr.rcvif = NULL; 503#endif 504 505 fnew.fin_ifp = fin->fin_ifp; 506 fnew.fin_flx = FI_NOCKSUM; 507 fnew.fin_m = m; 508 fnew.fin_ip = ip; 509 fnew.fin_mp = &m; 510 fnew.fin_hlen = hlen; 511 fnew.fin_dp = (char *)ip + hlen; 512 (void) ipf_makefrip(hlen, ip, &fnew); 513 514 return ipf_fastroute(m, &m, &fnew, NULL); 515} 516 517 518int 519ipf_send_icmp_err(type, fin, dst) 520 int type; 521 fr_info_t *fin; 522 int dst; 523{ 524 int err, hlen, xtra, iclen, ohlen, avail, code; 525 struct in_addr dst4; 526 struct icmp *icmp; 527 struct mbuf *m; 528 i6addr_t dst6; 529 void *ifp; 530#ifdef USE_INET6 531 ip6_t *ip6; 532#endif 533 ip_t *ip, *ip2; 534 535 if ((type < 0) || (type >= ICMP_MAXTYPE)) 536 return -1; 537 538 code = fin->fin_icode; 539#ifdef USE_INET6 540#if 0 541 /* XXX Fix an off by one error: s/>/>=/ 542 was: 543 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int))) 544 Fix obtained from NetBSD ip_fil_netbsd.c r1.4: */ 545#endif 546 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int))) 547 return -1; 548#endif 549 550 if (ipf_checkl4sum(fin) == -1) 551 return -1; 552#ifdef MGETHDR 553 MGETHDR(m, M_DONTWAIT, MT_HEADER); 554#else 555 MGET(m, M_DONTWAIT, MT_HEADER); 556#endif 557 if (m == NULL) 558 return -1; 559 avail = MHLEN; 560 561 xtra = 0; 562 hlen = 0; 563 ohlen = 0; 564 dst4.s_addr = 0; 565 ifp = fin->fin_ifp; 566 if (fin->fin_v == 4) { 567 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT)) 568 switch (ntohs(fin->fin_data[0]) >> 8) 569 { 570 case ICMP_ECHO : 571 case ICMP_TSTAMP : 572 case ICMP_IREQ : 573 case ICMP_MASKREQ : 574 break; 575 default : 576 FREE_MB_T(m); 577 return 0; 578 } 579 580 if (dst == 0) { 581 if (ipf_ifpaddr(&ipfmain, 4, FRI_NORMAL, ifp, 582 &dst6, NULL) == -1) { 583 FREE_MB_T(m); 584 return -1; 585 } 586 dst4 = dst6.in4; 587 } else 588 dst4.s_addr = fin->fin_daddr; 589 590 hlen = sizeof(ip_t); 591 ohlen = fin->fin_hlen; 592 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen; 593 if (fin->fin_hlen < fin->fin_plen) 594 xtra = MIN(fin->fin_dlen, 8); 595 else 596 xtra = 0; 597 } 598 599#ifdef USE_INET6 600 else if (fin->fin_v == 6) { 601 hlen = sizeof(ip6_t); 602 ohlen = sizeof(ip6_t); 603 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen; 604 type = icmptoicmp6types[type]; 605 if (type == ICMP6_DST_UNREACH) 606 code = icmptoicmp6unreach[code]; 607 608 if (iclen + max_linkhdr + fin->fin_plen > avail) { 609 MCLGET(m, M_DONTWAIT); 610 if ((m->m_flags & M_EXT) == 0) { 611 FREE_MB_T(m); 612 return -1; 613 } 614 avail = MCLBYTES; 615 } 616 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr); 617 xtra = MIN(xtra, IPV6_MMTU - iclen); 618 if (dst == 0) { 619 if (ipf_ifpaddr(&ipfmain, 6, FRI_NORMAL, ifp, 620 &dst6, NULL) == -1) { 621 FREE_MB_T(m); 622 return -1; 623 } 624 } else 625 dst6 = fin->fin_dst6; 626 } 627#endif 628 else { 629 FREE_MB_T(m); 630 return -1; 631 } 632 633 avail -= (max_linkhdr + iclen); 634 if (avail < 0) { 635 FREE_MB_T(m); 636 return -1; 637 } 638 if (xtra > avail) 639 xtra = avail; 640 iclen += xtra; 641 m->m_data += max_linkhdr; 642 m->m_pkthdr.rcvif = (struct ifnet *)0; 643 m->m_pkthdr.len = iclen; 644 m->m_len = iclen; 645 ip = mtod(m, ip_t *); 646 icmp = (struct icmp *)((char *)ip + hlen); 647 ip2 = (ip_t *)&icmp->icmp_ip; 648 649 icmp->icmp_type = type; 650 icmp->icmp_code = fin->fin_icode; 651 icmp->icmp_cksum = 0; 652#ifdef icmp_nextmtu 653 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) { 654 if (fin->fin_mtu != 0) { 655 icmp->icmp_nextmtu = htons(fin->fin_mtu); 656 657 } else if (ifp != NULL) { 658 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp)); 659 660 } else { /* make up a number... */ 661 icmp->icmp_nextmtu = htons(fin->fin_plen - 20); 662 } 663 } 664#endif 665 666 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen); 667 668#ifdef USE_INET6 669 ip6 = (ip6_t *)ip; 670 if (fin->fin_v == 6) { 671 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow; 672 ip6->ip6_plen = htons(iclen - hlen); 673 ip6->ip6_nxt = IPPROTO_ICMPV6; 674 ip6->ip6_hlim = 0; 675 ip6->ip6_src = dst6.in6; 676 ip6->ip6_dst = fin->fin_src6.in6; 677 if (xtra > 0) 678 bcopy((char *)fin->fin_ip + ohlen, 679 (char *)&icmp->icmp_ip + ohlen, xtra); 680 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6, 681 sizeof(*ip6), iclen - hlen); 682 } else 683#endif 684 { 685 ip->ip_p = IPPROTO_ICMP; 686 ip->ip_src.s_addr = dst4.s_addr; 687 ip->ip_dst.s_addr = fin->fin_saddr; 688 689 if (xtra > 0) 690 bcopy((char *)fin->fin_ip + ohlen, 691 (char *)&icmp->icmp_ip + ohlen, xtra); 692 icmp->icmp_cksum = ipf_cksum((u_short *)icmp, 693 sizeof(*icmp) + 8); 694 ip->ip_len = htons(iclen); 695 ip->ip_p = IPPROTO_ICMP; 696 } 697 err = ipf_send_ip(fin, m); 698 return err; 699} 700 701 702 703 704/* 705 * m0 - pointer to mbuf where the IP packet starts 706 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain 707 */ 708int 709ipf_fastroute(m0, mpp, fin, fdp) 710 mb_t *m0, **mpp; 711 fr_info_t *fin; 712 frdest_t *fdp; 713{ 714 register struct ip *ip, *mhip; 715 register struct mbuf *m = *mpp; 716 register struct route *ro; 717 int len, off, error = 0, hlen, code; 718 struct ifnet *ifp, *sifp; 719 struct sockaddr_in *dst; 720 struct route iproute; 721 u_short ip_off; 722 frdest_t node; 723 frentry_t *fr; 724 725 ro = NULL; 726 727#ifdef M_WRITABLE 728 /* 729 * HOT FIX/KLUDGE: 730 * 731 * If the mbuf we're about to send is not writable (because of 732 * a cluster reference, for example) we'll need to make a copy 733 * of it since this routine modifies the contents. 734 * 735 * If you have non-crappy network hardware that can transmit data 736 * from the mbuf, rather than making a copy, this is gonna be a 737 * problem. 738 */ 739 if (M_WRITABLE(m) == 0) { 740 m0 = m_dup(m, M_DONTWAIT); 741 if (m0 != NULL) { 742 FREE_MB_T(m); 743 m = m0; 744 *mpp = m; 745 } else { 746 error = ENOBUFS; 747 FREE_MB_T(m); 748 goto done; 749 } 750 } 751#endif 752 753#ifdef USE_INET6 754 if (fin->fin_v == 6) { 755 /* 756 * currently "to <if>" and "to <if>:ip#" are not supported 757 * for IPv6 758 */ 759 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL); 760 } 761#endif 762 763 hlen = fin->fin_hlen; 764 ip = mtod(m0, struct ip *); 765 ifp = NULL; 766 767 /* 768 * Route packet. 769 */ 770 ro = &iproute; 771 bzero(ro, sizeof (*ro)); 772 dst = (struct sockaddr_in *)&ro->ro_dst; 773 dst->sin_family = AF_INET; 774 dst->sin_addr = ip->ip_dst; 775 776 fr = fin->fin_fr; 777 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) && 778 (fdp->fd_type == FRD_DSTLIST)) { 779 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0) 780 fdp = &node; 781 } 782 783 if (fdp != NULL) 784 ifp = fdp->fd_ptr; 785 else 786 ifp = fin->fin_ifp; 787 788 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) { 789 error = -2; 790 goto bad; 791 } 792 793 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0)) 794 dst->sin_addr = fdp->fd_ip; 795 796 dst->sin_len = sizeof(*dst); 797 in_rtalloc(ro, M_GETFIB(m0)); 798 799 if ((ifp == NULL) && (ro->ro_rt != NULL)) 800 ifp = ro->ro_rt->rt_ifp; 801 802 if ((ro->ro_rt == NULL) || (ifp == NULL)) { 803 if (in_localaddr(ip->ip_dst)) 804 error = EHOSTUNREACH; 805 else 806 error = ENETUNREACH; 807 goto bad; 808 } 809 if (ro->ro_rt->rt_flags & RTF_GATEWAY) 810 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway; 811 if (ro->ro_rt) 812 counter_u64_add(ro->ro_rt->rt_pksent, 1); 813 814 /* 815 * For input packets which are being "fastrouted", they won't 816 * go back through output filtering and miss their chance to get 817 * NAT'd and counted. Duplicated packets aren't considered to be 818 * part of the normal packet stream, so do not NAT them or pass 819 * them through stateful checking, etc. 820 */ 821 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) { 822 sifp = fin->fin_ifp; 823 fin->fin_ifp = ifp; 824 fin->fin_out = 1; 825 (void) ipf_acctpkt(fin, NULL); 826 fin->fin_fr = NULL; 827 if (!fr || !(fr->fr_flags & FR_RETMASK)) { 828 u_32_t pass; 829 830 (void) ipf_state_check(fin, &pass); 831 } 832 833 switch (ipf_nat_checkout(fin, NULL)) 834 { 835 case 0 : 836 break; 837 case 1 : 838 ip->ip_sum = 0; 839 break; 840 case -1 : 841 error = -1; 842 goto bad; 843 break; 844 } 845 846 fin->fin_ifp = sifp; 847 fin->fin_out = 0; 848 } else 849 ip->ip_sum = 0; 850 /* 851 * If small enough for interface, can just send directly. 852 */ 853 if (ntohs(ip->ip_len) <= ifp->if_mtu) { 854 if (!ip->ip_sum) 855 ip->ip_sum = in_cksum(m, hlen); 856 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst, 857 ro 858 ); 859 goto done; 860 } 861 /* 862 * Too large for interface; fragment if possible. 863 * Must be able to put at least 8 bytes per fragment. 864 */ 865 ip_off = ntohs(ip->ip_off); 866 if (ip_off & IP_DF) { 867 error = EMSGSIZE; 868 goto bad; 869 } 870 len = (ifp->if_mtu - hlen) &~ 7; 871 if (len < 8) { 872 error = EMSGSIZE; 873 goto bad; 874 } 875 876 { 877 int mhlen, firstlen = len; 878 struct mbuf **mnext = &m->m_act; 879 880 /* 881 * Loop through length of segment after first fragment, 882 * make new header and copy data of each part and link onto chain. 883 */ 884 m0 = m; 885 mhlen = sizeof (struct ip); 886 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) { 887#ifdef MGETHDR 888 MGETHDR(m, M_DONTWAIT, MT_HEADER); 889#else 890 MGET(m, M_DONTWAIT, MT_HEADER); 891#endif 892 if (m == NULL) { 893 m = m0; 894 error = ENOBUFS; 895 goto bad; 896 } 897 m->m_data += max_linkhdr; 898 mhip = mtod(m, struct ip *); 899 bcopy((char *)ip, (char *)mhip, sizeof(*ip)); 900 if (hlen > sizeof (struct ip)) { 901 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip); 902 IP_HL_A(mhip, mhlen >> 2); 903 } 904 m->m_len = mhlen; 905 mhip->ip_off = ((off - hlen) >> 3) + ip_off; 906 if (off + len >= ntohs(ip->ip_len)) 907 len = ntohs(ip->ip_len) - off; 908 else 909 mhip->ip_off |= IP_MF; 910 mhip->ip_len = htons((u_short)(len + mhlen)); 911 *mnext = m; 912 m->m_next = m_copy(m0, off, len); 913 if (m->m_next == 0) { 914 error = ENOBUFS; /* ??? */ 915 goto sendorfree; 916 } 917 m->m_pkthdr.len = mhlen + len; 918 m->m_pkthdr.rcvif = NULL; 919 mhip->ip_off = htons((u_short)mhip->ip_off); 920 mhip->ip_sum = 0; 921 mhip->ip_sum = in_cksum(m, mhlen); 922 mnext = &m->m_act; 923 } 924 /* 925 * Update first fragment by trimming what's been copied out 926 * and updating header, then send each fragment (in order). 927 */ 928 m_adj(m0, hlen + firstlen - ip->ip_len); 929 ip->ip_len = htons((u_short)(hlen + firstlen)); 930 ip->ip_off = htons((u_short)IP_MF); 931 ip->ip_sum = 0; 932 ip->ip_sum = in_cksum(m0, hlen); 933sendorfree: 934 for (m = m0; m; m = m0) { 935 m0 = m->m_act; 936 m->m_act = 0; 937 if (error == 0) 938 error = (*ifp->if_output)(ifp, m, 939 (struct sockaddr *)dst, 940 ro 941 ); 942 else 943 FREE_MB_T(m); 944 } 945 } 946done: 947 if (!error) 948 ipfmain.ipf_frouteok[0]++; 949 else 950 ipfmain.ipf_frouteok[1]++; 951 952 if ((ro != NULL) && (ro->ro_rt != NULL)) { 953 RTFREE(ro->ro_rt); 954 } 955 return 0; 956bad: 957 if (error == EMSGSIZE) { 958 sifp = fin->fin_ifp; 959 code = fin->fin_icode; 960 fin->fin_icode = ICMP_UNREACH_NEEDFRAG; 961 fin->fin_ifp = ifp; 962 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1); 963 fin->fin_ifp = sifp; 964 fin->fin_icode = code; 965 } 966 FREE_MB_T(m); 967 goto done; 968} 969 970 971int 972ipf_verifysrc(fin) 973 fr_info_t *fin; 974{ 975 struct sockaddr_in *dst; 976 struct route iproute; 977 978 bzero((char *)&iproute, sizeof(iproute)); 979 dst = (struct sockaddr_in *)&iproute.ro_dst; 980 dst->sin_len = sizeof(*dst); 981 dst->sin_family = AF_INET; 982 dst->sin_addr = fin->fin_src; 983 in_rtalloc(&iproute, 0); 984 if (iproute.ro_rt == NULL) 985 return 0; 986 return (fin->fin_ifp == iproute.ro_rt->rt_ifp); 987} 988 989 990/* 991 * return the first IP Address associated with an interface 992 */ 993int 994ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask) 995 ipf_main_softc_t *softc; 996 int v, atype; 997 void *ifptr; 998 i6addr_t *inp, *inpmask; 999{ 1000#ifdef USE_INET6 1001 struct in6_addr *inp6 = NULL; 1002#endif 1003 struct sockaddr *sock, *mask; 1004 struct sockaddr_in *sin; 1005 struct ifaddr *ifa; 1006 struct ifnet *ifp; 1007 1008 if ((ifptr == NULL) || (ifptr == (void *)-1)) 1009 return -1; 1010 1011 sin = NULL; 1012 ifp = ifptr; 1013 1014 if (v == 4) 1015 inp->in4.s_addr = 0; 1016#ifdef USE_INET6 1017 else if (v == 6) 1018 bzero((char *)inp, sizeof(*inp)); 1019#endif 1020 ifa = TAILQ_FIRST(&ifp->if_addrhead); 1021 1022 sock = ifa->ifa_addr; 1023 while (sock != NULL && ifa != NULL) { 1024 sin = (struct sockaddr_in *)sock; 1025 if ((v == 4) && (sin->sin_family == AF_INET)) 1026 break; 1027#ifdef USE_INET6 1028 if ((v == 6) && (sin->sin_family == AF_INET6)) { 1029 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr; 1030 if (!IN6_IS_ADDR_LINKLOCAL(inp6) && 1031 !IN6_IS_ADDR_LOOPBACK(inp6)) 1032 break; 1033 } 1034#endif 1035 ifa = TAILQ_NEXT(ifa, ifa_link); 1036 if (ifa != NULL) 1037 sock = ifa->ifa_addr; 1038 } 1039 1040 if (ifa == NULL || sin == NULL) 1041 return -1; 1042 1043 mask = ifa->ifa_netmask; 1044 if (atype == FRI_BROADCAST) 1045 sock = ifa->ifa_broadaddr; 1046 else if (atype == FRI_PEERADDR) 1047 sock = ifa->ifa_dstaddr; 1048 1049 if (sock == NULL) 1050 return -1; 1051 1052#ifdef USE_INET6 1053 if (v == 6) { 1054 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock, 1055 (struct sockaddr_in6 *)mask, 1056 inp, inpmask); 1057 } 1058#endif 1059 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock, 1060 (struct sockaddr_in *)mask, 1061 &inp->in4, &inpmask->in4); 1062} 1063 1064 1065u_32_t 1066ipf_newisn(fin) 1067 fr_info_t *fin; 1068{ 1069 u_32_t newiss; 1070 newiss = arc4random(); 1071 return newiss; 1072} 1073 1074 1075/* ------------------------------------------------------------------------ */ 1076/* Function: ipf_nextipid */ 1077/* Returns: int - 0 == success, -1 == error (packet should be droppped) */ 1078/* Parameters: fin(I) - pointer to packet information */ 1079/* */ 1080/* Returns the next IPv4 ID to use for this packet. */ 1081/* ------------------------------------------------------------------------ */ 1082u_short 1083ipf_nextipid(fin) 1084 fr_info_t *fin; 1085{ 1086 u_short id; 1087 1088#ifndef RANDOM_IP_ID 1089 MUTEX_ENTER(&ipfmain.ipf_rw); 1090 id = ipid++; 1091 MUTEX_EXIT(&ipfmain.ipf_rw); 1092#else 1093 id = ip_randomid(); 1094#endif 1095 1096 return id; 1097} 1098 1099 1100INLINE int 1101ipf_checkv4sum(fin) 1102 fr_info_t *fin; 1103{ 1104#ifdef CSUM_DATA_VALID 1105 int manual = 0; 1106 u_short sum; 1107 ip_t *ip; 1108 mb_t *m; 1109 1110 if ((fin->fin_flx & FI_NOCKSUM) != 0) 1111 return 0; 1112 1113 if ((fin->fin_flx & FI_SHORT) != 0) 1114 return 1; 1115 1116 if (fin->fin_cksum != FI_CK_NEEDED) 1117 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1; 1118 1119 m = fin->fin_m; 1120 if (m == NULL) { 1121 manual = 1; 1122 goto skipauto; 1123 } 1124 ip = fin->fin_ip; 1125 1126 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) == 1127 CSUM_IP_CHECKED) { 1128 fin->fin_cksum = FI_CK_BAD; 1129 fin->fin_flx |= FI_BAD; 1130 return -1; 1131 } 1132 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 1133 /* Depending on the driver, UDP may have zero checksum */ 1134 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx & 1135 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) { 1136 udphdr_t *udp = fin->fin_dp; 1137 if (udp->uh_sum == 0) { 1138 /* 1139 * we're good no matter what the hardware 1140 * checksum flags and csum_data say (handling 1141 * of csum_data for zero UDP checksum is not 1142 * consistent across all drivers) 1143 */ 1144 fin->fin_cksum = 1; 1145 return 0; 1146 } 1147 } 1148 1149 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 1150 sum = m->m_pkthdr.csum_data; 1151 else 1152 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 1153 htonl(m->m_pkthdr.csum_data + 1154 fin->fin_dlen + fin->fin_p)); 1155 sum ^= 0xffff; 1156 if (sum != 0) { 1157 fin->fin_cksum = FI_CK_BAD; 1158 fin->fin_flx |= FI_BAD; 1159 } else { 1160 fin->fin_cksum = FI_CK_SUMOK; 1161 return 0; 1162 } 1163 } else { 1164 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) { 1165 fin->fin_cksum = FI_CK_L4FULL; 1166 return 0; 1167 } else if (m->m_pkthdr.csum_flags == CSUM_TCP || 1168 m->m_pkthdr.csum_flags == CSUM_UDP) { 1169 fin->fin_cksum = FI_CK_L4PART; 1170 return 0; 1171 } else if (m->m_pkthdr.csum_flags == CSUM_IP) { 1172 fin->fin_cksum = FI_CK_L4PART; 1173 return 0; 1174 } else { 1175 manual = 1; 1176 } 1177 } 1178skipauto: 1179 if (manual != 0) { 1180 if (ipf_checkl4sum(fin) == -1) { 1181 fin->fin_flx |= FI_BAD; 1182 return -1; 1183 } 1184 } 1185#else 1186 if (ipf_checkl4sum(fin) == -1) { 1187 fin->fin_flx |= FI_BAD; 1188 return -1; 1189 } 1190#endif 1191 return 0; 1192} 1193 1194 1195#ifdef USE_INET6 1196INLINE int 1197ipf_checkv6sum(fin) 1198 fr_info_t *fin; 1199{ 1200 if ((fin->fin_flx & FI_NOCKSUM) != 0) 1201 return 0; 1202 1203 if ((fin->fin_flx & FI_SHORT) != 0) 1204 return 1; 1205 1206 if (fin->fin_cksum != FI_CK_NEEDED) 1207 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1; 1208 1209 if (ipf_checkl4sum(fin) == -1) { 1210 fin->fin_flx |= FI_BAD; 1211 return -1; 1212 } 1213 return 0; 1214} 1215#endif /* USE_INET6 */ 1216 1217 1218size_t 1219mbufchainlen(m0) 1220 struct mbuf *m0; 1221 { 1222 size_t len; 1223 1224 if ((m0->m_flags & M_PKTHDR) != 0) { 1225 len = m0->m_pkthdr.len; 1226 } else { 1227 struct mbuf *m; 1228 1229 for (m = m0, len = 0; m != NULL; m = m->m_next) 1230 len += m->m_len; 1231 } 1232 return len; 1233} 1234 1235 1236/* ------------------------------------------------------------------------ */ 1237/* Function: ipf_pullup */ 1238/* Returns: NULL == pullup failed, else pointer to protocol header */ 1239/* Parameters: xmin(I)- pointer to buffer where data packet starts */ 1240/* fin(I) - pointer to packet information */ 1241/* len(I) - number of bytes to pullup */ 1242/* */ 1243/* Attempt to move at least len bytes (from the start of the buffer) into a */ 1244/* single buffer for ease of access. Operating system native functions are */ 1245/* used to manage buffers - if necessary. If the entire packet ends up in */ 1246/* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */ 1247/* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */ 1248/* and ONLY if the pullup succeeds. */ 1249/* */ 1250/* We assume that 'xmin' is a pointer to a buffer that is part of the chain */ 1251/* of buffers that starts at *fin->fin_mp. */ 1252/* ------------------------------------------------------------------------ */ 1253void * 1254ipf_pullup(xmin, fin, len) 1255 mb_t *xmin; 1256 fr_info_t *fin; 1257 int len; 1258{ 1259 int dpoff, ipoff; 1260 mb_t *m = xmin; 1261 char *ip; 1262 1263 if (m == NULL) 1264 return NULL; 1265 1266 ip = (char *)fin->fin_ip; 1267 if ((fin->fin_flx & FI_COALESCE) != 0) 1268 return ip; 1269 1270 ipoff = fin->fin_ipoff; 1271 if (fin->fin_dp != NULL) 1272 dpoff = (char *)fin->fin_dp - (char *)ip; 1273 else 1274 dpoff = 0; 1275 1276 if (M_LEN(m) < len) { 1277 mb_t *n = *fin->fin_mp; 1278 /* 1279 * Assume that M_PKTHDR is set and just work with what is left 1280 * rather than check.. 1281 * Should not make any real difference, anyway. 1282 */ 1283 if (m != n) { 1284 /* 1285 * Record the mbuf that points to the mbuf that we're 1286 * about to go to work on so that we can update the 1287 * m_next appropriately later. 1288 */ 1289 for (; n->m_next != m; n = n->m_next) 1290 ; 1291 } else { 1292 n = NULL; 1293 } 1294 1295#ifdef MHLEN 1296 if (len > MHLEN) 1297#else 1298 if (len > MLEN) 1299#endif 1300 { 1301#ifdef HAVE_M_PULLDOWN 1302 if (m_pulldown(m, 0, len, NULL) == NULL) 1303 m = NULL; 1304#else 1305 FREE_MB_T(*fin->fin_mp); 1306 m = NULL; 1307 n = NULL; 1308#endif 1309 } else 1310 { 1311 m = m_pullup(m, len); 1312 } 1313 if (n != NULL) 1314 n->m_next = m; 1315 if (m == NULL) { 1316 /* 1317 * When n is non-NULL, it indicates that m pointed to 1318 * a sub-chain (tail) of the mbuf and that the head 1319 * of this chain has not yet been free'd. 1320 */ 1321 if (n != NULL) { 1322 FREE_MB_T(*fin->fin_mp); 1323 } 1324 1325 *fin->fin_mp = NULL; 1326 fin->fin_m = NULL; 1327 return NULL; 1328 } 1329 1330 if (n == NULL) 1331 *fin->fin_mp = m; 1332 1333 while (M_LEN(m) == 0) { 1334 m = m->m_next; 1335 } 1336 fin->fin_m = m; 1337 ip = MTOD(m, char *) + ipoff; 1338 1339 fin->fin_ip = (ip_t *)ip; 1340 if (fin->fin_dp != NULL) 1341 fin->fin_dp = (char *)fin->fin_ip + dpoff; 1342 if (fin->fin_fraghdr != NULL) 1343 fin->fin_fraghdr = (char *)ip + 1344 ((char *)fin->fin_fraghdr - 1345 (char *)fin->fin_ip); 1346 } 1347 1348 if (len == fin->fin_plen) 1349 fin->fin_flx |= FI_COALESCE; 1350 return ip; 1351} 1352 1353 1354int 1355ipf_inject(fin, m) 1356 fr_info_t *fin; 1357 mb_t *m; 1358{ 1359 int error = 0; 1360 1361 if (fin->fin_out == 0) { 1362 netisr_dispatch(NETISR_IP, m); 1363 } else { 1364 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len); 1365 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off); 1366 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL); 1367 } 1368 1369 return error; 1370} 1371 1372int ipf_pfil_unhook(void) { 1373#if defined(NETBSD_PF) && (__FreeBSD_version >= 500011) 1374 struct pfil_head *ph_inet; 1375# ifdef USE_INET6 1376 struct pfil_head *ph_inet6; 1377# endif 1378#endif 1379 1380#ifdef NETBSD_PF 1381 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET); 1382 if (ph_inet != NULL) 1383 pfil_remove_hook((void *)ipf_check_wrapper, NULL, 1384 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet); 1385# ifdef USE_INET6 1386 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6); 1387 if (ph_inet6 != NULL) 1388 pfil_remove_hook((void *)ipf_check_wrapper6, NULL, 1389 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6); 1390# endif 1391#endif 1392 1393 return (0); 1394} 1395 1396int ipf_pfil_hook(void) { 1397#if defined(NETBSD_PF) && (__FreeBSD_version >= 500011) 1398 struct pfil_head *ph_inet; 1399# ifdef USE_INET6 1400 struct pfil_head *ph_inet6; 1401# endif 1402#endif 1403 1404# ifdef NETBSD_PF 1405 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET); 1406# ifdef USE_INET6 1407 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6); 1408# endif 1409 if (ph_inet == NULL 1410# ifdef USE_INET6 1411 && ph_inet6 == NULL 1412# endif 1413 ) { 1414 return ENODEV; 1415 } 1416 1417 if (ph_inet != NULL) 1418 pfil_add_hook((void *)ipf_check_wrapper, NULL, 1419 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet); 1420# ifdef USE_INET6 1421 if (ph_inet6 != NULL) 1422 pfil_add_hook((void *)ipf_check_wrapper6, NULL, 1423 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6); 1424# endif 1425# endif 1426 return (0); 1427} 1428 1429void 1430ipf_event_reg(void) 1431{ 1432 ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \ 1433 ipf_ifevent, &ipfmain, \ 1434 EVENTHANDLER_PRI_ANY); 1435 ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \ 1436 ipf_ifevent, &ipfmain, \ 1437 EVENTHANDLER_PRI_ANY); 1438 ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \ 1439 &ipfmain, EVENTHANDLER_PRI_ANY); 1440} 1441 1442void 1443ipf_event_dereg(void) 1444{ 1445 if (ipf_arrivetag != NULL) { 1446 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag); 1447 } 1448 if (ipf_departtag != NULL) { 1449 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag); 1450 } 1451 if (ipf_clonetag != NULL) { 1452 EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag); 1453 } 1454} 1455 1456 1457u_32_t 1458ipf_random() 1459{ 1460 return arc4random(); 1461} 1462 1463 1464u_int 1465ipf_pcksum(fin, hlen, sum) 1466 fr_info_t *fin; 1467 int hlen; 1468 u_int sum; 1469{ 1470 struct mbuf *m; 1471 u_int sum2; 1472 int off; 1473 1474 m = fin->fin_m; 1475 off = (char *)fin->fin_dp - (char *)fin->fin_ip; 1476 m->m_data += hlen; 1477 m->m_len -= hlen; 1478 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off); 1479 m->m_len += hlen; 1480 m->m_data -= hlen; 1481 1482 /* 1483 * Both sum and sum2 are partial sums, so combine them together. 1484 */ 1485 sum += ~sum2 & 0xffff; 1486 while (sum > 0xffff) 1487 sum = (sum & 0xffff) + (sum >> 16); 1488 sum2 = ~sum & 0xffff; 1489 return sum2; 1490} 1491