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) && \
20 !defined(KLD_MODULE) && !defined(IPFILTER_LKM)
21 # include "opt_inet6.h"
23 #if defined(__FreeBSD_version) && \
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)
39 #include <sys/malloc.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 VNET_DEFINE_STATIC(eventhandler_tag, ipf_arrivetag);
102 VNET_DEFINE_STATIC(eventhandler_tag, ipf_departtag);
103 #define V_ipf_arrivetag VNET(ipf_arrivetag)
104 #define V_ipf_departtag VNET(ipf_departtag)
107 * Disable the "cloner" event handler; we are getting interface
108 * events before the firewall is fully initiallized and also no vnet
109 * information thus leading to uninitialised memory accesses.
110 * In addition it is unclear why we need it in first place.
111 * If it turns out to be needed, well need a dedicated event handler
112 * for it to deal with the ifc and the correct vnet.
114 VNET_DEFINE_STATIC(eventhandler_tag, ipf_clonetag);
115 #define V_ipf_clonetag VNET(ipf_clonetag)
118 static void ipf_ifevent(void *arg, struct ifnet *ifp);
120 static void ipf_ifevent(arg, ifp)
125 CURVNET_SET(ifp->if_vnet);
126 if (V_ipfmain.ipf_running > 0)
127 ipf_sync(&V_ipfmain, NULL);
134 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
136 struct ip *ip = mtod(*mp, struct ip *);
140 * IPFilter expects evreything in network byte order
142 #if (__FreeBSD_version < 1000019)
143 ip->ip_len = htons(ip->ip_len);
144 ip->ip_off = htons(ip->ip_off);
146 CURVNET_SET(ifp->if_vnet);
147 rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
150 #if (__FreeBSD_version < 1000019)
151 if ((rv == 0) && (*mp != NULL)) {
152 ip = mtod(*mp, struct ip *);
153 ip->ip_len = ntohs(ip->ip_len);
154 ip->ip_off = ntohs(ip->ip_off);
161 # include <netinet/ip6.h>
164 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
168 CURVNET_SET(ifp->if_vnet);
169 error = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
170 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp);
175 #if defined(IPFILTER_LKM)
179 if (strcmp(s, "ipl") == 0)
183 #endif /* IPFILTER_LKM */
190 ipf_main_softc_t *softc = arg;
194 READ_ENTER(&softc->ipf_global);
196 if (softc->ipf_running > 0)
197 ipf_slowtimer(softc);
199 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
201 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
203 callout_init(&softc->ipf_slow_ch, 1);
204 callout_reset(&softc->ipf_slow_ch,
205 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
206 ipf_timer_func, softc);
208 RWLOCK_EXIT(&softc->ipf_global);
215 ipf_main_softc_t *softc;
222 if (softc->ipf_running > 0) {
227 if (ipf_init_all(softc) < 0) {
233 bzero((char *)V_ipfmain.ipf_selwait, sizeof(V_ipfmain.ipf_selwait));
234 softc->ipf_running = 1;
236 if (softc->ipf_control_forwarding & 1)
241 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
242 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
244 callout_init(&softc->ipf_slow_ch, 1);
245 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
246 ipf_timer_func, softc);
252 * Disable the filter by removing the hooks from the IP input/output
257 ipf_main_softc_t *softc;
263 if (softc->ipf_control_forwarding & 2)
269 if (softc->ipf_slow_ch.callout != NULL)
270 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
271 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
273 callout_drain(&softc->ipf_slow_ch);
277 softc->ipf_running = -2;
286 * Filter ioctl interface.
289 ipfioctl(dev, cmd, data, mode, p)
291 # define p_cred td_ucred
292 # define p_uid td_ucred->cr_ruid
298 int error = 0, unit = 0;
301 CURVNET_SET(TD_TO_VNET(p));
303 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
305 V_ipfmain.ipf_interror = 130001;
311 unit = GET_MINOR(dev);
312 if ((IPL_LOGMAX < unit) || (unit < 0)) {
313 V_ipfmain.ipf_interror = 130002;
318 if (V_ipfmain.ipf_running <= 0) {
319 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
320 V_ipfmain.ipf_interror = 130003;
324 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
325 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
326 cmd != SIOCGETFS && cmd != SIOCGETFF &&
327 cmd != SIOCIPFINTERROR) {
328 V_ipfmain.ipf_interror = 130004;
336 error = ipf_ioctlswitch(&V_ipfmain, unit, data, cmd, mode, p->p_uid, p);
350 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
351 * requires a large amount of setting up and isn't any more efficient.
357 struct tcphdr *tcp, *tcp2;
366 if (tcp->th_flags & TH_RST)
367 return -1; /* feedback loop */
369 if (ipf_checkl4sum(fin) == -1)
372 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
373 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
374 ((tcp->th_flags & TH_FIN) ? 1 : 0);
377 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
382 MGETHDR(m, M_NOWAIT, MT_HEADER);
384 MGET(m, M_NOWAIT, MT_HEADER);
388 if (sizeof(*tcp2) + hlen > MLEN) {
389 if (!(MCLGET(m, M_NOWAIT))) {
395 m->m_len = sizeof(*tcp2) + hlen;
397 m->m_data += max_linkhdr;
398 m->m_pkthdr.len = m->m_len;
399 m->m_pkthdr.rcvif = (struct ifnet *)0;
401 ip = mtod(m, struct ip *);
402 bzero((char *)ip, hlen);
406 tcp2 = (struct tcphdr *)((char *)ip + hlen);
407 tcp2->th_sport = tcp->th_dport;
408 tcp2->th_dport = tcp->th_sport;
410 if (tcp->th_flags & TH_ACK) {
411 tcp2->th_seq = tcp->th_ack;
412 tcp2->th_flags = TH_RST;
416 tcp2->th_ack = ntohl(tcp->th_seq);
417 tcp2->th_ack += tlen;
418 tcp2->th_ack = htonl(tcp2->th_ack);
419 tcp2->th_flags = TH_RST|TH_ACK;
422 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
423 tcp2->th_win = tcp->th_win;
428 if (fin->fin_v == 6) {
429 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
430 ip6->ip6_plen = htons(sizeof(struct tcphdr));
431 ip6->ip6_nxt = IPPROTO_TCP;
433 ip6->ip6_src = fin->fin_dst6.in6;
434 ip6->ip6_dst = fin->fin_src6.in6;
435 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
436 sizeof(*ip6), sizeof(*tcp2));
437 return ipf_send_ip(fin, m);
440 ip->ip_p = IPPROTO_TCP;
441 ip->ip_len = htons(sizeof(struct tcphdr));
442 ip->ip_src.s_addr = fin->fin_daddr;
443 ip->ip_dst.s_addr = fin->fin_saddr;
444 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
445 ip->ip_len = htons(hlen + sizeof(*tcp2));
446 return ipf_send_ip(fin, m);
451 * ip_len must be in network byte order when called.
462 ip = mtod(m, ip_t *);
463 bzero((char *)&fnew, sizeof(fnew));
464 fnew.fin_main_soft = fin->fin_main_soft;
466 IP_V_A(ip, fin->fin_v);
473 fnew.fin_p = ip->ip_p;
474 fnew.fin_plen = ntohs(ip->ip_len);
475 IP_HL_A(ip, sizeof(*oip) >> 2);
476 ip->ip_tos = oip->ip_tos;
477 ip->ip_id = fin->fin_ip->ip_id;
478 ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
479 ip->ip_ttl = V_ip_defttl;
485 ip6_t *ip6 = (ip6_t *)ip;
488 ip6->ip6_hlim = IPDEFTTL;
491 fnew.fin_p = ip6->ip6_nxt;
493 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
501 m->m_pkthdr.rcvif = NULL;
504 fnew.fin_ifp = fin->fin_ifp;
505 fnew.fin_flx = FI_NOCKSUM;
509 fnew.fin_hlen = hlen;
510 fnew.fin_dp = (char *)ip + hlen;
511 (void) ipf_makefrip(hlen, ip, &fnew);
513 return ipf_fastroute(m, &m, &fnew, NULL);
518 ipf_send_icmp_err(type, fin, dst)
523 int err, hlen, xtra, iclen, ohlen, avail, code;
534 if ((type < 0) || (type >= ICMP_MAXTYPE))
537 code = fin->fin_icode;
539 /* See NetBSD ip_fil_netbsd.c r1.4: */
540 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
544 if (ipf_checkl4sum(fin) == -1)
547 MGETHDR(m, M_NOWAIT, MT_HEADER);
549 MGET(m, M_NOWAIT, MT_HEADER);
560 if (fin->fin_v == 4) {
561 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
562 switch (ntohs(fin->fin_data[0]) >> 8)
575 if (ipf_ifpaddr(&V_ipfmain, 4, FRI_NORMAL, ifp,
576 &dst6, NULL) == -1) {
582 dst4.s_addr = fin->fin_daddr;
585 ohlen = fin->fin_hlen;
586 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
587 if (fin->fin_hlen < fin->fin_plen)
588 xtra = MIN(fin->fin_dlen, 8);
594 else if (fin->fin_v == 6) {
595 hlen = sizeof(ip6_t);
596 ohlen = sizeof(ip6_t);
597 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
598 type = icmptoicmp6types[type];
599 if (type == ICMP6_DST_UNREACH)
600 code = icmptoicmp6unreach[code];
602 if (iclen + max_linkhdr + fin->fin_plen > avail) {
603 if (!(MCLGET(m, M_NOWAIT))) {
609 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
610 xtra = MIN(xtra, IPV6_MMTU - iclen);
612 if (ipf_ifpaddr(&V_ipfmain, 6, FRI_NORMAL, ifp,
613 &dst6, NULL) == -1) {
618 dst6 = fin->fin_dst6;
626 avail -= (max_linkhdr + iclen);
634 m->m_data += max_linkhdr;
635 m->m_pkthdr.rcvif = (struct ifnet *)0;
636 m->m_pkthdr.len = iclen;
638 ip = mtod(m, ip_t *);
639 icmp = (struct icmp *)((char *)ip + hlen);
640 ip2 = (ip_t *)&icmp->icmp_ip;
642 icmp->icmp_type = type;
643 icmp->icmp_code = fin->fin_icode;
644 icmp->icmp_cksum = 0;
646 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
647 if (fin->fin_mtu != 0) {
648 icmp->icmp_nextmtu = htons(fin->fin_mtu);
650 } else if (ifp != NULL) {
651 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
653 } else { /* make up a number... */
654 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
659 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
663 if (fin->fin_v == 6) {
664 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
665 ip6->ip6_plen = htons(iclen - hlen);
666 ip6->ip6_nxt = IPPROTO_ICMPV6;
668 ip6->ip6_src = dst6.in6;
669 ip6->ip6_dst = fin->fin_src6.in6;
671 bcopy((char *)fin->fin_ip + ohlen,
672 (char *)&icmp->icmp_ip + ohlen, xtra);
673 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
674 sizeof(*ip6), iclen - hlen);
678 ip->ip_p = IPPROTO_ICMP;
679 ip->ip_src.s_addr = dst4.s_addr;
680 ip->ip_dst.s_addr = fin->fin_saddr;
683 bcopy((char *)fin->fin_ip + ohlen,
684 (char *)&icmp->icmp_ip + ohlen, xtra);
685 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
687 ip->ip_len = htons(iclen);
688 ip->ip_p = IPPROTO_ICMP;
690 err = ipf_send_ip(fin, m);
698 * m0 - pointer to mbuf where the IP packet starts
699 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
702 ipf_fastroute(m0, mpp, fin, fdp)
707 register struct ip *ip, *mhip;
708 register struct mbuf *m = *mpp;
709 int len, off, error = 0, hlen, code;
710 struct ifnet *ifp, *sifp;
711 struct sockaddr_in dst;
712 struct nhop4_extended nh4;
722 * If the mbuf we're about to send is not writable (because of
723 * a cluster reference, for example) we'll need to make a copy
724 * of it since this routine modifies the contents.
726 * If you have non-crappy network hardware that can transmit data
727 * from the mbuf, rather than making a copy, this is gonna be a
730 if (M_WRITABLE(m) == 0) {
731 m0 = m_dup(m, M_NOWAIT);
745 if (fin->fin_v == 6) {
747 * currently "to <if>" and "to <if>:ip#" are not supported
750 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
754 hlen = fin->fin_hlen;
755 ip = mtod(m0, struct ip *);
761 bzero(&dst, sizeof (dst));
762 dst.sin_family = AF_INET;
763 dst.sin_addr = ip->ip_dst;
764 dst.sin_len = sizeof(dst);
767 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
768 (fdp->fd_type == FRD_DSTLIST)) {
769 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
778 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
783 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
784 dst.sin_addr = fdp->fd_ip;
786 fibnum = M_GETFIB(m0);
787 if (fib4_lookup_nh_ext(fibnum, dst.sin_addr, NHR_REF, 0, &nh4) != 0) {
788 if (in_localaddr(ip->ip_dst))
789 error = EHOSTUNREACH;
797 if (nh4.nh_flags & NHF_GATEWAY)
798 dst.sin_addr = nh4.nh_addr;
801 * For input packets which are being "fastrouted", they won't
802 * go back through output filtering and miss their chance to get
803 * NAT'd and counted. Duplicated packets aren't considered to be
804 * part of the normal packet stream, so do not NAT them or pass
805 * them through stateful checking, etc.
807 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
811 (void) ipf_acctpkt(fin, NULL);
813 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
816 (void) ipf_state_check(fin, &pass);
819 switch (ipf_nat_checkout(fin, NULL))
837 * If small enough for interface, can just send directly.
839 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
841 ip->ip_sum = in_cksum(m, hlen);
842 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
848 * Too large for interface; fragment if possible.
849 * Must be able to put at least 8 bytes per fragment.
851 ip_off = ntohs(ip->ip_off);
852 if (ip_off & IP_DF) {
856 len = (ifp->if_mtu - hlen) &~ 7;
863 int mhlen, firstlen = len;
864 struct mbuf **mnext = &m->m_act;
867 * Loop through length of segment after first fragment,
868 * make new header and copy data of each part and link onto chain.
871 mhlen = sizeof (struct ip);
872 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
874 MGETHDR(m, M_NOWAIT, MT_HEADER);
876 MGET(m, M_NOWAIT, MT_HEADER);
883 m->m_data += max_linkhdr;
884 mhip = mtod(m, struct ip *);
885 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
886 if (hlen > sizeof (struct ip)) {
887 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
888 IP_HL_A(mhip, mhlen >> 2);
891 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
892 if (off + len >= ntohs(ip->ip_len))
893 len = ntohs(ip->ip_len) - off;
895 mhip->ip_off |= IP_MF;
896 mhip->ip_len = htons((u_short)(len + mhlen));
898 m->m_next = m_copym(m0, off, len, M_NOWAIT);
899 if (m->m_next == 0) {
900 error = ENOBUFS; /* ??? */
903 m->m_pkthdr.len = mhlen + len;
904 m->m_pkthdr.rcvif = NULL;
905 mhip->ip_off = htons((u_short)mhip->ip_off);
907 mhip->ip_sum = in_cksum(m, mhlen);
911 * Update first fragment by trimming what's been copied out
912 * and updating header, then send each fragment (in order).
914 m_adj(m0, hlen + firstlen - ip->ip_len);
915 ip->ip_len = htons((u_short)(hlen + firstlen));
916 ip->ip_off = htons((u_short)IP_MF);
918 ip->ip_sum = in_cksum(m0, hlen);
920 for (m = m0; m; m = m0) {
924 error = (*ifp->if_output)(ifp, m,
925 (struct sockaddr *)&dst,
934 V_ipfmain.ipf_frouteok[0]++;
936 V_ipfmain.ipf_frouteok[1]++;
940 if (error == EMSGSIZE) {
942 code = fin->fin_icode;
943 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
945 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
947 fin->fin_icode = code;
958 struct nhop4_basic nh4;
960 if (fib4_lookup_nh_basic(0, fin->fin_src, 0, 0, &nh4) != 0)
962 return (fin->fin_ifp == nh4.nh_ifp);
967 * return the first IP Address associated with an interface
970 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
971 ipf_main_softc_t *softc;
974 i6addr_t *inp, *inpmask;
977 struct in6_addr *ia6 = NULL;
979 struct sockaddr *sock, *mask;
980 struct sockaddr_in *sin;
984 if ((ifptr == NULL) || (ifptr == (void *)-1))
994 bzero((char *)inp, sizeof(*inp));
996 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
998 sock = ifa->ifa_addr;
999 while (sock != NULL && ifa != NULL) {
1000 sin = (struct sockaddr_in *)sock;
1001 if ((v == 4) && (sin->sin_family == AF_INET))
1004 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1005 ia6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1006 if (!IN6_IS_ADDR_LINKLOCAL(ia6) &&
1007 !IN6_IS_ADDR_LOOPBACK(ia6))
1011 ifa = CK_STAILQ_NEXT(ifa, ifa_link);
1013 sock = ifa->ifa_addr;
1016 if (ifa == NULL || sin == NULL)
1019 mask = ifa->ifa_netmask;
1020 if (atype == FRI_BROADCAST)
1021 sock = ifa->ifa_broadaddr;
1022 else if (atype == FRI_PEERADDR)
1023 sock = ifa->ifa_dstaddr;
1030 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1031 (struct sockaddr_in6 *)mask,
1035 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1036 (struct sockaddr_in *)mask,
1037 &inp->in4, &inpmask->in4);
1046 newiss = arc4random();
1055 #ifdef CSUM_DATA_VALID
1061 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1064 if ((fin->fin_flx & FI_SHORT) != 0)
1067 if (fin->fin_cksum != FI_CK_NEEDED)
1068 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1077 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1079 fin->fin_cksum = FI_CK_BAD;
1080 fin->fin_flx |= FI_BAD;
1081 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));
1084 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1085 /* Depending on the driver, UDP may have zero checksum */
1086 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1087 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1088 udphdr_t *udp = fin->fin_dp;
1089 if (udp->uh_sum == 0) {
1091 * we're good no matter what the hardware
1092 * checksum flags and csum_data say (handling
1093 * of csum_data for zero UDP checksum is not
1094 * consistent across all drivers)
1101 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1102 sum = m->m_pkthdr.csum_data;
1104 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1105 htonl(m->m_pkthdr.csum_data +
1106 fin->fin_dlen + fin->fin_p));
1109 fin->fin_cksum = FI_CK_BAD;
1110 fin->fin_flx |= FI_BAD;
1111 DT2(ipf_fi_bad_checkv4sum_sum, fr_info_t *, fin, u_int, sum);
1113 fin->fin_cksum = FI_CK_SUMOK;
1117 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1118 fin->fin_cksum = FI_CK_L4FULL;
1120 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1121 m->m_pkthdr.csum_flags == CSUM_UDP) {
1122 fin->fin_cksum = FI_CK_L4PART;
1124 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1125 fin->fin_cksum = FI_CK_L4PART;
1133 if (ipf_checkl4sum(fin) == -1) {
1134 fin->fin_flx |= FI_BAD;
1135 DT2(ipf_fi_bad_checkv4sum_manual, fr_info_t *, fin, u_int, manual);
1140 if (ipf_checkl4sum(fin) == -1) {
1141 fin->fin_flx |= FI_BAD;
1142 DT2(ipf_fi_bad_checkv4sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1155 if ((fin->fin_flx & FI_NOCKSUM) != 0) {
1156 DT(ipf_checkv6sum_fi_nocksum);
1160 if ((fin->fin_flx & FI_SHORT) != 0) {
1161 DT(ipf_checkv6sum_fi_short);
1165 if (fin->fin_cksum != FI_CK_NEEDED) {
1166 DT(ipf_checkv6sum_fi_ck_needed);
1167 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1170 if (ipf_checkl4sum(fin) == -1) {
1171 fin->fin_flx |= FI_BAD;
1172 DT2(ipf_fi_bad_checkv6sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1177 #endif /* USE_INET6 */
1186 if ((m0->m_flags & M_PKTHDR) != 0) {
1187 len = m0->m_pkthdr.len;
1191 for (m = m0, len = 0; m != NULL; m = m->m_next)
1198 /* ------------------------------------------------------------------------ */
1199 /* Function: ipf_pullup */
1200 /* Returns: NULL == pullup failed, else pointer to protocol header */
1201 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1202 /* fin(I) - pointer to packet information */
1203 /* len(I) - number of bytes to pullup */
1205 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1206 /* single buffer for ease of access. Operating system native functions are */
1207 /* used to manage buffers - if necessary. If the entire packet ends up in */
1208 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1209 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1210 /* and ONLY if the pullup succeeds. */
1212 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1213 /* of buffers that starts at *fin->fin_mp. */
1214 /* ------------------------------------------------------------------------ */
1216 ipf_pullup(xmin, fin, len)
1228 ip = (char *)fin->fin_ip;
1229 if ((fin->fin_flx & FI_COALESCE) != 0)
1232 ipoff = fin->fin_ipoff;
1233 if (fin->fin_dp != NULL)
1234 dpoff = (char *)fin->fin_dp - (char *)ip;
1238 if (M_LEN(m) < len) {
1239 mb_t *n = *fin->fin_mp;
1241 * Assume that M_PKTHDR is set and just work with what is left
1242 * rather than check..
1243 * Should not make any real difference, anyway.
1247 * Record the mbuf that points to the mbuf that we're
1248 * about to go to work on so that we can update the
1249 * m_next appropriately later.
1251 for (; n->m_next != m; n = n->m_next)
1263 #ifdef HAVE_M_PULLDOWN
1264 if (m_pulldown(m, 0, len, NULL) == NULL)
1267 FREE_MB_T(*fin->fin_mp);
1273 m = m_pullup(m, len);
1279 * When n is non-NULL, it indicates that m pointed to
1280 * a sub-chain (tail) of the mbuf and that the head
1281 * of this chain has not yet been free'd.
1284 FREE_MB_T(*fin->fin_mp);
1287 *fin->fin_mp = NULL;
1295 while (M_LEN(m) == 0) {
1299 ip = MTOD(m, char *) + ipoff;
1301 fin->fin_ip = (ip_t *)ip;
1302 if (fin->fin_dp != NULL)
1303 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1304 if (fin->fin_fraghdr != NULL)
1305 fin->fin_fraghdr = (char *)ip +
1306 ((char *)fin->fin_fraghdr -
1307 (char *)fin->fin_ip);
1310 if (len == fin->fin_plen)
1311 fin->fin_flx |= FI_COALESCE;
1323 if (fin->fin_out == 0) {
1324 netisr_dispatch(NETISR_IP, m);
1326 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1327 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1328 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1334 int ipf_pfil_unhook(void) {
1335 struct pfil_head *ph_inet;
1337 struct pfil_head *ph_inet6;
1340 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1341 if (ph_inet != NULL)
1342 pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1343 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1345 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1346 if (ph_inet6 != NULL)
1347 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1348 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1354 int ipf_pfil_hook(void) {
1355 struct pfil_head *ph_inet;
1357 struct pfil_head *ph_inet6;
1360 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1362 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1372 if (ph_inet != NULL)
1373 pfil_add_hook((void *)ipf_check_wrapper, NULL,
1374 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1376 if (ph_inet6 != NULL)
1377 pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1378 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1386 V_ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1387 ipf_ifevent, NULL, \
1388 EVENTHANDLER_PRI_ANY);
1389 V_ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1390 ipf_ifevent, NULL, \
1391 EVENTHANDLER_PRI_ANY);
1393 V_ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1394 NULL, EVENTHANDLER_PRI_ANY);
1399 ipf_event_dereg(void)
1401 if (V_ipf_arrivetag != NULL) {
1402 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, V_ipf_arrivetag);
1404 if (V_ipf_departtag != NULL) {
1405 EVENTHANDLER_DEREGISTER(ifnet_departure_event, V_ipf_departtag);
1408 if (V_ipf_clonetag != NULL) {
1409 EVENTHANDLER_DEREGISTER(if_clone_event, V_ipf_clonetag);
1418 return arc4random();
1423 ipf_pcksum(fin, hlen, sum)
1433 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1436 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1441 * Both sum and sum2 are partial sums, so combine them together.
1443 sum += ~sum2 & 0xffff;
1444 while (sum > 0xffff)
1445 sum = (sum & 0xffff) + (sum >> 16);
1446 sum2 = ~sum & 0xffff;
1452 ipf_pcksum6(m, ip6, off, len)
1461 if (m->m_len < sizeof(struct ip6_hdr)) {
1465 sum = in6_cksum(m, ip6->ip6_nxt, off, len);
1471 sp = (u_short *)&ip6->ip6_src;
1472 sum = *sp++; /* ip6_src */
1480 sum += *sp++; /* ip6_dst */
1488 return(ipf_pcksum(fin, off, sum));