4 * Copyright (C) 2012 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
9 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
10 static const char rcsid[] = "@(#)$Id$";
13 #if defined(KERNEL) || defined(_KERNEL)
19 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 400000) && \
20 !defined(KLD_MODULE) && !defined(IPFILTER_LKM)
21 # include "opt_inet6.h"
23 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 440000) && \
24 !defined(KLD_MODULE) && !defined(IPFILTER_LKM)
25 # include "opt_random_ip_id.h"
27 #include <sys/param.h>
28 #include <sys/errno.h>
29 #include <sys/types.h>
31 # include <sys/fcntl.h>
32 # include <sys/filio.h>
34 #include <sys/systm.h>
35 # include <sys/dirent.h>
36 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000)
39 # include <sys/malloc.h>
40 # include <sys/mbuf.h>
41 # include <sys/sockopt.h>
42 #include <sys/socket.h>
43 # include <sys/selinfo.h>
44 # include <netinet/tcp_var.h>
47 # include <net/if_var.h>
48 # include <net/netisr.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_fib.h>
52 #include <netinet/in_var.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/tcp.h>
58 #include <netinet/udp.h>
59 #include <netinet/tcpip.h>
60 #include <netinet/ip_icmp.h>
61 #include "netinet/ip_compat.h"
63 # include <netinet/icmp6.h>
65 #include "netinet/ip_fil.h"
66 #include "netinet/ip_nat.h"
67 #include "netinet/ip_frag.h"
68 #include "netinet/ip_state.h"
69 #include "netinet/ip_proxy.h"
70 #include "netinet/ip_auth.h"
71 #include "netinet/ip_sync.h"
72 #include "netinet/ip_lookup.h"
73 #include "netinet/ip_dstlist.h"
75 #include "netinet/ip_scan.h"
77 #include "netinet/ip_pool.h"
78 # include <sys/malloc.h>
79 #include <sys/kernel.h>
80 #ifdef CSUM_DATA_VALID
81 #include <machine/in_cksum.h>
83 extern int ip_optcopy __P((struct ip *, struct ip *));
85 # ifdef IPFILTER_M_IPFILTER
86 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
90 static int ipf_send_ip __P((fr_info_t *, mb_t *));
91 static void ipf_timer_func __P((void *arg));
93 VNET_DEFINE(ipf_main_softc_t, ipfmain) = {
96 #define V_ipfmain VNET(ipfmain)
98 # include <sys/conf.h>
99 # include <net/pfil.h>
101 static eventhandler_tag ipf_arrivetag, ipf_departtag;
104 * Disable the "cloner" event handler; we are getting interface
105 * events before the firewall is fully initiallized and also no vnet
106 * information thus leading to uninitialised memory accesses.
107 * In addition it is unclear why we need it in first place.
108 * If it turns out to be needed, well need a dedicated event handler
109 * for it to deal with the ifc and the correct vnet.
111 static eventhandler_tag ipf_clonetag;
114 static void ipf_ifevent(void *arg, struct ifnet *ifp);
116 static void ipf_ifevent(arg, ifp)
121 CURVNET_SET(ifp->if_vnet);
122 if (V_ipfmain.ipf_running > 0)
123 ipf_sync(&V_ipfmain, NULL);
130 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
132 struct ip *ip = mtod(*mp, struct ip *);
135 CURVNET_SET(ifp->if_vnet);
136 rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
143 # include <netinet/ip6.h>
146 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
150 CURVNET_SET(ifp->if_vnet);
151 error = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
152 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp);
157 #if defined(IPFILTER_LKM)
161 if (strcmp(s, "ipl") == 0)
165 #endif /* IPFILTER_LKM */
172 ipf_main_softc_t *softc = arg;
176 READ_ENTER(&softc->ipf_global);
178 if (softc->ipf_running > 0)
179 ipf_slowtimer(softc);
181 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
183 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
185 callout_init(&softc->ipf_slow_ch, 1);
186 callout_reset(&softc->ipf_slow_ch,
187 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
188 ipf_timer_func, softc);
190 RWLOCK_EXIT(&softc->ipf_global);
197 ipf_main_softc_t *softc;
204 if (softc->ipf_running > 0) {
209 if (ipf_init_all(softc) < 0) {
215 bzero((char *)V_ipfmain.ipf_selwait, sizeof(V_ipfmain.ipf_selwait));
216 softc->ipf_running = 1;
218 if (softc->ipf_control_forwarding & 1)
223 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
224 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
226 callout_init(&softc->ipf_slow_ch, 1);
227 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
228 ipf_timer_func, softc);
234 * Disable the filter by removing the hooks from the IP input/output
239 ipf_main_softc_t *softc;
245 if (softc->ipf_control_forwarding & 2)
251 if (softc->ipf_slow_ch.callout != NULL)
252 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
253 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
255 callout_drain(&softc->ipf_slow_ch);
259 softc->ipf_running = -2;
268 * Filter ioctl interface.
271 ipfioctl(dev, cmd, data, mode, p)
273 # define p_cred td_ucred
274 # define p_uid td_ucred->cr_ruid
280 int error = 0, unit = 0;
283 CURVNET_SET(TD_TO_VNET(p));
285 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
287 V_ipfmain.ipf_interror = 130001;
293 unit = GET_MINOR(dev);
294 if ((IPL_LOGMAX < unit) || (unit < 0)) {
295 V_ipfmain.ipf_interror = 130002;
300 if (V_ipfmain.ipf_running <= 0) {
301 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
302 V_ipfmain.ipf_interror = 130003;
306 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
307 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
308 cmd != SIOCGETFS && cmd != SIOCGETFF &&
309 cmd != SIOCIPFINTERROR) {
310 V_ipfmain.ipf_interror = 130004;
318 error = ipf_ioctlswitch(&V_ipfmain, unit, data, cmd, mode, p->p_uid, p);
332 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
333 * requires a large amount of setting up and isn't any more efficient.
339 struct tcphdr *tcp, *tcp2;
348 if (tcp->th_flags & TH_RST)
349 return -1; /* feedback loop */
351 if (ipf_checkl4sum(fin) == -1)
354 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
355 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
356 ((tcp->th_flags & TH_FIN) ? 1 : 0);
359 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
364 MGETHDR(m, M_NOWAIT, MT_HEADER);
366 MGET(m, M_NOWAIT, MT_HEADER);
370 if (sizeof(*tcp2) + hlen > MLEN) {
371 if (!(MCLGET(m, M_NOWAIT))) {
377 m->m_len = sizeof(*tcp2) + hlen;
379 m->m_data += max_linkhdr;
380 m->m_pkthdr.len = m->m_len;
381 m->m_pkthdr.rcvif = (struct ifnet *)0;
383 ip = mtod(m, struct ip *);
384 bzero((char *)ip, hlen);
388 tcp2 = (struct tcphdr *)((char *)ip + hlen);
389 tcp2->th_sport = tcp->th_dport;
390 tcp2->th_dport = tcp->th_sport;
392 if (tcp->th_flags & TH_ACK) {
393 tcp2->th_seq = tcp->th_ack;
394 tcp2->th_flags = TH_RST;
398 tcp2->th_ack = ntohl(tcp->th_seq);
399 tcp2->th_ack += tlen;
400 tcp2->th_ack = htonl(tcp2->th_ack);
401 tcp2->th_flags = TH_RST|TH_ACK;
404 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
405 tcp2->th_win = tcp->th_win;
410 if (fin->fin_v == 6) {
411 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
412 ip6->ip6_plen = htons(sizeof(struct tcphdr));
413 ip6->ip6_nxt = IPPROTO_TCP;
415 ip6->ip6_src = fin->fin_dst6.in6;
416 ip6->ip6_dst = fin->fin_src6.in6;
417 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
418 sizeof(*ip6), sizeof(*tcp2));
419 return ipf_send_ip(fin, m);
422 ip->ip_p = IPPROTO_TCP;
423 ip->ip_len = htons(sizeof(struct tcphdr));
424 ip->ip_src.s_addr = fin->fin_daddr;
425 ip->ip_dst.s_addr = fin->fin_saddr;
426 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
427 ip->ip_len = htons(hlen + sizeof(*tcp2));
428 return ipf_send_ip(fin, m);
433 * ip_len must be in network byte order when called.
444 ip = mtod(m, ip_t *);
445 bzero((char *)&fnew, sizeof(fnew));
446 fnew.fin_main_soft = fin->fin_main_soft;
448 IP_V_A(ip, fin->fin_v);
455 fnew.fin_p = ip->ip_p;
456 fnew.fin_plen = ntohs(ip->ip_len);
457 IP_HL_A(ip, sizeof(*oip) >> 2);
458 ip->ip_tos = oip->ip_tos;
459 ip->ip_id = fin->fin_ip->ip_id;
460 ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
461 ip->ip_ttl = V_ip_defttl;
467 ip6_t *ip6 = (ip6_t *)ip;
470 ip6->ip6_hlim = IPDEFTTL;
473 fnew.fin_p = ip6->ip6_nxt;
475 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
483 m->m_pkthdr.rcvif = NULL;
486 fnew.fin_ifp = fin->fin_ifp;
487 fnew.fin_flx = FI_NOCKSUM;
491 fnew.fin_hlen = hlen;
492 fnew.fin_dp = (char *)ip + hlen;
493 (void) ipf_makefrip(hlen, ip, &fnew);
495 return ipf_fastroute(m, &m, &fnew, NULL);
500 ipf_send_icmp_err(type, fin, dst)
505 int err, hlen, xtra, iclen, ohlen, avail, code;
516 if ((type < 0) || (type >= ICMP_MAXTYPE))
519 code = fin->fin_icode;
521 /* See NetBSD ip_fil_netbsd.c r1.4: */
522 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
526 if (ipf_checkl4sum(fin) == -1)
529 MGETHDR(m, M_NOWAIT, MT_HEADER);
531 MGET(m, M_NOWAIT, MT_HEADER);
542 if (fin->fin_v == 4) {
543 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
544 switch (ntohs(fin->fin_data[0]) >> 8)
557 if (ipf_ifpaddr(&V_ipfmain, 4, FRI_NORMAL, ifp,
558 &dst6, NULL) == -1) {
564 dst4.s_addr = fin->fin_daddr;
567 ohlen = fin->fin_hlen;
568 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
569 if (fin->fin_hlen < fin->fin_plen)
570 xtra = MIN(fin->fin_dlen, 8);
576 else if (fin->fin_v == 6) {
577 hlen = sizeof(ip6_t);
578 ohlen = sizeof(ip6_t);
579 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
580 type = icmptoicmp6types[type];
581 if (type == ICMP6_DST_UNREACH)
582 code = icmptoicmp6unreach[code];
584 if (iclen + max_linkhdr + fin->fin_plen > avail) {
585 if (!(MCLGET(m, M_NOWAIT))) {
591 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
592 xtra = MIN(xtra, IPV6_MMTU - iclen);
594 if (ipf_ifpaddr(&V_ipfmain, 6, FRI_NORMAL, ifp,
595 &dst6, NULL) == -1) {
600 dst6 = fin->fin_dst6;
608 avail -= (max_linkhdr + iclen);
616 m->m_data += max_linkhdr;
617 m->m_pkthdr.rcvif = (struct ifnet *)0;
618 m->m_pkthdr.len = iclen;
620 ip = mtod(m, ip_t *);
621 icmp = (struct icmp *)((char *)ip + hlen);
622 ip2 = (ip_t *)&icmp->icmp_ip;
624 icmp->icmp_type = type;
625 icmp->icmp_code = fin->fin_icode;
626 icmp->icmp_cksum = 0;
628 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
629 if (fin->fin_mtu != 0) {
630 icmp->icmp_nextmtu = htons(fin->fin_mtu);
632 } else if (ifp != NULL) {
633 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
635 } else { /* make up a number... */
636 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
641 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
645 if (fin->fin_v == 6) {
646 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
647 ip6->ip6_plen = htons(iclen - hlen);
648 ip6->ip6_nxt = IPPROTO_ICMPV6;
650 ip6->ip6_src = dst6.in6;
651 ip6->ip6_dst = fin->fin_src6.in6;
653 bcopy((char *)fin->fin_ip + ohlen,
654 (char *)&icmp->icmp_ip + ohlen, xtra);
655 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
656 sizeof(*ip6), iclen - hlen);
660 ip->ip_p = IPPROTO_ICMP;
661 ip->ip_src.s_addr = dst4.s_addr;
662 ip->ip_dst.s_addr = fin->fin_saddr;
665 bcopy((char *)fin->fin_ip + ohlen,
666 (char *)&icmp->icmp_ip + ohlen, xtra);
667 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
669 ip->ip_len = htons(iclen);
670 ip->ip_p = IPPROTO_ICMP;
672 err = ipf_send_ip(fin, m);
680 * m0 - pointer to mbuf where the IP packet starts
681 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
684 ipf_fastroute(m0, mpp, fin, fdp)
689 register struct ip *ip, *mhip;
690 register struct mbuf *m = *mpp;
691 int len, off, error = 0, hlen, code;
692 struct ifnet *ifp, *sifp;
693 struct sockaddr_in dst;
694 struct nhop4_extended nh4;
705 * If the mbuf we're about to send is not writable (because of
706 * a cluster reference, for example) we'll need to make a copy
707 * of it since this routine modifies the contents.
709 * If you have non-crappy network hardware that can transmit data
710 * from the mbuf, rather than making a copy, this is gonna be a
713 if (M_WRITABLE(m) == 0) {
714 m0 = m_dup(m, M_NOWAIT);
728 if (fin->fin_v == 6) {
730 * currently "to <if>" and "to <if>:ip#" are not supported
733 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
737 hlen = fin->fin_hlen;
738 ip = mtod(m0, struct ip *);
744 bzero(&dst, sizeof (dst));
745 dst.sin_family = AF_INET;
746 dst.sin_addr = ip->ip_dst;
747 dst.sin_len = sizeof(dst);
750 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
751 (fdp->fd_type == FRD_DSTLIST)) {
752 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
761 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
766 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
767 dst.sin_addr = fdp->fd_ip;
769 fibnum = M_GETFIB(m0);
770 if (fib4_lookup_nh_ext(fibnum, dst.sin_addr, NHR_REF, 0, &nh4) != 0) {
771 if (in_localaddr(ip->ip_dst))
772 error = EHOSTUNREACH;
781 if (nh4.nh_flags & NHF_GATEWAY)
782 dst.sin_addr = nh4.nh_addr;
785 * For input packets which are being "fastrouted", they won't
786 * go back through output filtering and miss their chance to get
787 * NAT'd and counted. Duplicated packets aren't considered to be
788 * part of the normal packet stream, so do not NAT them or pass
789 * them through stateful checking, etc.
791 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
795 (void) ipf_acctpkt(fin, NULL);
797 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
800 (void) ipf_state_check(fin, &pass);
803 switch (ipf_nat_checkout(fin, NULL))
821 * If small enough for interface, can just send directly.
823 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
825 ip->ip_sum = in_cksum(m, hlen);
826 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
832 * Too large for interface; fragment if possible.
833 * Must be able to put at least 8 bytes per fragment.
835 ip_off = ntohs(ip->ip_off);
836 if (ip_off & IP_DF) {
840 len = (ifp->if_mtu - hlen) &~ 7;
847 int mhlen, firstlen = len;
848 struct mbuf **mnext = &m->m_act;
851 * Loop through length of segment after first fragment,
852 * make new header and copy data of each part and link onto chain.
855 mhlen = sizeof (struct ip);
856 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
858 MGETHDR(m, M_NOWAIT, MT_HEADER);
860 MGET(m, M_NOWAIT, MT_HEADER);
867 m->m_data += max_linkhdr;
868 mhip = mtod(m, struct ip *);
869 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
870 if (hlen > sizeof (struct ip)) {
871 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
872 IP_HL_A(mhip, mhlen >> 2);
875 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
876 if (off + len >= ntohs(ip->ip_len))
877 len = ntohs(ip->ip_len) - off;
879 mhip->ip_off |= IP_MF;
880 mhip->ip_len = htons((u_short)(len + mhlen));
882 m->m_next = m_copym(m0, off, len, M_NOWAIT);
883 if (m->m_next == 0) {
884 error = ENOBUFS; /* ??? */
887 m->m_pkthdr.len = mhlen + len;
888 m->m_pkthdr.rcvif = NULL;
889 mhip->ip_off = htons((u_short)mhip->ip_off);
891 mhip->ip_sum = in_cksum(m, mhlen);
895 * Update first fragment by trimming what's been copied out
896 * and updating header, then send each fragment (in order).
898 m_adj(m0, hlen + firstlen - ip->ip_len);
899 ip->ip_len = htons((u_short)(hlen + firstlen));
900 ip->ip_off = htons((u_short)IP_MF);
902 ip->ip_sum = in_cksum(m0, hlen);
904 for (m = m0; m; m = m0) {
908 error = (*ifp->if_output)(ifp, m,
909 (struct sockaddr *)&dst,
918 V_ipfmain.ipf_frouteok[0]++;
920 V_ipfmain.ipf_frouteok[1]++;
923 fib4_free_nh_ext(fibnum, &nh4);
927 if (error == EMSGSIZE) {
929 code = fin->fin_icode;
930 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
932 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
934 fin->fin_icode = code;
945 struct nhop4_basic nh4;
947 if (fib4_lookup_nh_basic(0, fin->fin_src, 0, 0, &nh4) != 0)
949 return (fin->fin_ifp == nh4.nh_ifp);
954 * return the first IP Address associated with an interface
957 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
958 ipf_main_softc_t *softc;
961 i6addr_t *inp, *inpmask;
964 struct in6_addr *inp6 = NULL;
966 struct sockaddr *sock, *mask;
967 struct sockaddr_in *sin;
971 if ((ifptr == NULL) || (ifptr == (void *)-1))
981 bzero((char *)inp, sizeof(*inp));
983 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
985 sock = ifa->ifa_addr;
986 while (sock != NULL && ifa != NULL) {
987 sin = (struct sockaddr_in *)sock;
988 if ((v == 4) && (sin->sin_family == AF_INET))
991 if ((v == 6) && (sin->sin_family == AF_INET6)) {
992 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
993 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
994 !IN6_IS_ADDR_LOOPBACK(inp6))
998 ifa = CK_STAILQ_NEXT(ifa, ifa_link);
1000 sock = ifa->ifa_addr;
1003 if (ifa == NULL || sin == NULL)
1006 mask = ifa->ifa_netmask;
1007 if (atype == FRI_BROADCAST)
1008 sock = ifa->ifa_broadaddr;
1009 else if (atype == FRI_PEERADDR)
1010 sock = ifa->ifa_dstaddr;
1017 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1018 (struct sockaddr_in6 *)mask,
1022 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1023 (struct sockaddr_in *)mask,
1024 &inp->in4, &inpmask->in4);
1033 newiss = arc4random();
1042 #ifdef CSUM_DATA_VALID
1048 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1051 if ((fin->fin_flx & FI_SHORT) != 0)
1054 if (fin->fin_cksum != FI_CK_NEEDED)
1055 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1064 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1066 fin->fin_cksum = FI_CK_BAD;
1067 fin->fin_flx |= FI_BAD;
1068 DT2(ipf_fi_bad_checkv4sum_csum_ip_checked, fr_info_t *, fin, u_int, m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID));
1071 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1072 /* Depending on the driver, UDP may have zero checksum */
1073 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1074 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1075 udphdr_t *udp = fin->fin_dp;
1076 if (udp->uh_sum == 0) {
1078 * we're good no matter what the hardware
1079 * checksum flags and csum_data say (handling
1080 * of csum_data for zero UDP checksum is not
1081 * consistent across all drivers)
1088 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1089 sum = m->m_pkthdr.csum_data;
1091 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1092 htonl(m->m_pkthdr.csum_data +
1093 fin->fin_dlen + fin->fin_p));
1096 fin->fin_cksum = FI_CK_BAD;
1097 fin->fin_flx |= FI_BAD;
1098 DT2(ipf_fi_bad_checkv4sum_sum, fr_info_t *, fin, u_int, sum);
1100 fin->fin_cksum = FI_CK_SUMOK;
1104 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1105 fin->fin_cksum = FI_CK_L4FULL;
1107 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1108 m->m_pkthdr.csum_flags == CSUM_UDP) {
1109 fin->fin_cksum = FI_CK_L4PART;
1111 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1112 fin->fin_cksum = FI_CK_L4PART;
1120 if (ipf_checkl4sum(fin) == -1) {
1121 fin->fin_flx |= FI_BAD;
1122 DT2(ipf_fi_bad_checkv4sum_manual, fr_info_t *, fin, u_int, manual);
1127 if (ipf_checkl4sum(fin) == -1) {
1128 fin->fin_flx |= FI_BAD;
1129 DT2(ipf_fi_bad_checkv4sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1142 if ((fin->fin_flx & FI_NOCKSUM) != 0) {
1143 DT(ipf_checkv6sum_fi_nocksum);
1147 if ((fin->fin_flx & FI_SHORT) != 0) {
1148 DT(ipf_checkv6sum_fi_short);
1152 if (fin->fin_cksum != FI_CK_NEEDED) {
1153 DT(ipf_checkv6sum_fi_ck_needed);
1154 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1157 if (ipf_checkl4sum(fin) == -1) {
1158 fin->fin_flx |= FI_BAD;
1159 DT2(ipf_fi_bad_checkv6sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1164 #endif /* USE_INET6 */
1173 if ((m0->m_flags & M_PKTHDR) != 0) {
1174 len = m0->m_pkthdr.len;
1178 for (m = m0, len = 0; m != NULL; m = m->m_next)
1185 /* ------------------------------------------------------------------------ */
1186 /* Function: ipf_pullup */
1187 /* Returns: NULL == pullup failed, else pointer to protocol header */
1188 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1189 /* fin(I) - pointer to packet information */
1190 /* len(I) - number of bytes to pullup */
1192 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1193 /* single buffer for ease of access. Operating system native functions are */
1194 /* used to manage buffers - if necessary. If the entire packet ends up in */
1195 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1196 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1197 /* and ONLY if the pullup succeeds. */
1199 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1200 /* of buffers that starts at *fin->fin_mp. */
1201 /* ------------------------------------------------------------------------ */
1203 ipf_pullup(xmin, fin, len)
1215 ip = (char *)fin->fin_ip;
1216 if ((fin->fin_flx & FI_COALESCE) != 0)
1219 ipoff = fin->fin_ipoff;
1220 if (fin->fin_dp != NULL)
1221 dpoff = (char *)fin->fin_dp - (char *)ip;
1225 if (M_LEN(m) < len) {
1226 mb_t *n = *fin->fin_mp;
1228 * Assume that M_PKTHDR is set and just work with what is left
1229 * rather than check..
1230 * Should not make any real difference, anyway.
1234 * Record the mbuf that points to the mbuf that we're
1235 * about to go to work on so that we can update the
1236 * m_next appropriately later.
1238 for (; n->m_next != m; n = n->m_next)
1250 #ifdef HAVE_M_PULLDOWN
1251 if (m_pulldown(m, 0, len, NULL) == NULL)
1254 FREE_MB_T(*fin->fin_mp);
1260 m = m_pullup(m, len);
1266 * When n is non-NULL, it indicates that m pointed to
1267 * a sub-chain (tail) of the mbuf and that the head
1268 * of this chain has not yet been free'd.
1271 FREE_MB_T(*fin->fin_mp);
1274 *fin->fin_mp = NULL;
1282 while (M_LEN(m) == 0) {
1286 ip = MTOD(m, char *) + ipoff;
1288 fin->fin_ip = (ip_t *)ip;
1289 if (fin->fin_dp != NULL)
1290 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1291 if (fin->fin_fraghdr != NULL)
1292 fin->fin_fraghdr = (char *)ip +
1293 ((char *)fin->fin_fraghdr -
1294 (char *)fin->fin_ip);
1297 if (len == fin->fin_plen)
1298 fin->fin_flx |= FI_COALESCE;
1310 if (fin->fin_out == 0) {
1311 netisr_dispatch(NETISR_IP, m);
1313 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1314 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1315 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1321 int ipf_pfil_unhook(void) {
1322 struct pfil_head *ph_inet;
1324 struct pfil_head *ph_inet6;
1327 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1328 if (ph_inet != NULL)
1329 pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1330 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1332 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1333 if (ph_inet6 != NULL)
1334 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1335 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1341 int ipf_pfil_hook(void) {
1342 struct pfil_head *ph_inet;
1344 struct pfil_head *ph_inet6;
1347 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1349 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1359 if (ph_inet != NULL)
1360 pfil_add_hook((void *)ipf_check_wrapper, NULL,
1361 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1363 if (ph_inet6 != NULL)
1364 pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1365 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1373 ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1374 ipf_ifevent, NULL, \
1375 EVENTHANDLER_PRI_ANY);
1376 ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1377 ipf_ifevent, NULL, \
1378 EVENTHANDLER_PRI_ANY);
1380 ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1381 NULL, EVENTHANDLER_PRI_ANY);
1386 ipf_event_dereg(void)
1388 if (ipf_arrivetag != NULL) {
1389 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag);
1391 if (ipf_departtag != NULL) {
1392 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag);
1395 if (ipf_clonetag != NULL) {
1396 EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag);
1405 return arc4random();
1410 ipf_pcksum(fin, hlen, sum)
1420 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1423 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1428 * Both sum and sum2 are partial sums, so combine them together.
1430 sum += ~sum2 & 0xffff;
1431 while (sum > 0xffff)
1432 sum = (sum & 0xffff) + (sum >> 16);
1433 sum2 = ~sum & 0xffff;