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>
29 #include <sys/errno.h>
30 #include <sys/types.h>
32 # include <sys/fcntl.h>
33 # include <sys/filio.h>
35 #include <sys/systm.h>
36 # include <sys/dirent.h>
37 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000)
40 # include <sys/malloc.h>
41 # include <sys/mbuf.h>
42 # include <sys/sockopt.h>
43 #include <sys/socket.h>
44 # include <sys/selinfo.h>
45 # include <netinet/tcp_var.h>
48 # include <net/if_var.h>
49 # include <net/netisr.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_fib.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>
59 #include <netinet/udp.h>
60 #include <netinet/tcpip.h>
61 #include <netinet/ip_icmp.h>
62 #include "netinet/ip_compat.h"
64 # include <netinet/icmp6.h>
66 #include "netinet/ip_fil.h"
67 #include "netinet/ip_nat.h"
68 #include "netinet/ip_frag.h"
69 #include "netinet/ip_state.h"
70 #include "netinet/ip_proxy.h"
71 #include "netinet/ip_auth.h"
72 #include "netinet/ip_sync.h"
73 #include "netinet/ip_lookup.h"
74 #include "netinet/ip_dstlist.h"
76 #include "netinet/ip_scan.h"
78 #include "netinet/ip_pool.h"
79 # include <sys/malloc.h>
80 #include <sys/kernel.h>
81 #ifdef CSUM_DATA_VALID
82 #include <machine/in_cksum.h>
84 extern int ip_optcopy __P((struct ip *, struct ip *));
86 # ifdef IPFILTER_M_IPFILTER
87 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
91 static int ipf_send_ip __P((fr_info_t *, mb_t *));
92 static void ipf_timer_func __P((void *arg));
94 VNET_DEFINE(ipf_main_softc_t, ipfmain) = {
97 #define V_ipfmain VNET(ipfmain)
99 # include <sys/conf.h>
100 # include <net/pfil.h>
102 static eventhandler_tag ipf_arrivetag, ipf_departtag;
105 * Disable the "cloner" event handler; we are getting interface
106 * events before the firewall is fully initiallized and also no vnet
107 * information thus leading to uninitialised memory accesses.
108 * In addition it is unclear why we need it in first place.
109 * If it turns out to be needed, well need a dedicated event handler
110 * for it to deal with the ifc and the correct vnet.
112 static eventhandler_tag ipf_clonetag;
115 static void ipf_ifevent(void *arg, struct ifnet *ifp);
117 static void ipf_ifevent(arg, ifp)
122 CURVNET_SET(ifp->if_vnet);
123 if (V_ipfmain.ipf_running > 0)
124 ipf_sync(&V_ipfmain, NULL);
131 ipf_check_wrapper(struct mbuf **mp, struct ifnet *ifp, int flags,
132 void *ruleset __unused, struct inpcb *inp)
134 struct ip *ip = mtod(*mp, struct ip *);
137 CURVNET_SET(ifp->if_vnet);
138 rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp,
139 !!(flags & PFIL_OUT), mp);
141 return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
146 ipf_check_wrapper6(struct mbuf **mp, struct ifnet *ifp, int flags,
147 void *ruleset __unused, struct inpcb *inp)
151 CURVNET_SET(ifp->if_vnet);
152 rv = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
153 sizeof(struct ip6_hdr), ifp, !!(flags & PFIL_OUT), mp);
156 return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
159 #if defined(IPFILTER_LKM)
163 if (strcmp(s, "ipl") == 0)
167 #endif /* IPFILTER_LKM */
174 ipf_main_softc_t *softc = arg;
178 READ_ENTER(&softc->ipf_global);
180 if (softc->ipf_running > 0)
181 ipf_slowtimer(softc);
183 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
185 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
187 callout_init(&softc->ipf_slow_ch, 1);
188 callout_reset(&softc->ipf_slow_ch,
189 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
190 ipf_timer_func, softc);
192 RWLOCK_EXIT(&softc->ipf_global);
199 ipf_main_softc_t *softc;
206 if (softc->ipf_running > 0) {
211 if (ipf_init_all(softc) < 0) {
217 bzero((char *)V_ipfmain.ipf_selwait, sizeof(V_ipfmain.ipf_selwait));
218 softc->ipf_running = 1;
220 if (softc->ipf_control_forwarding & 1)
225 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
226 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
228 callout_init(&softc->ipf_slow_ch, 1);
229 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
230 ipf_timer_func, softc);
236 * Disable the filter by removing the hooks from the IP input/output
241 ipf_main_softc_t *softc;
247 if (softc->ipf_control_forwarding & 2)
253 if (softc->ipf_slow_ch.callout != NULL)
254 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
255 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
257 callout_drain(&softc->ipf_slow_ch);
261 softc->ipf_running = -2;
270 * Filter ioctl interface.
273 ipfioctl(dev, cmd, data, mode, p)
275 # define p_cred td_ucred
276 # define p_uid td_ucred->cr_ruid
282 int error = 0, unit = 0;
285 CURVNET_SET(TD_TO_VNET(p));
287 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
289 V_ipfmain.ipf_interror = 130001;
295 unit = GET_MINOR(dev);
296 if ((IPL_LOGMAX < unit) || (unit < 0)) {
297 V_ipfmain.ipf_interror = 130002;
302 if (V_ipfmain.ipf_running <= 0) {
303 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
304 V_ipfmain.ipf_interror = 130003;
308 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
309 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
310 cmd != SIOCGETFS && cmd != SIOCGETFF &&
311 cmd != SIOCIPFINTERROR) {
312 V_ipfmain.ipf_interror = 130004;
320 error = ipf_ioctlswitch(&V_ipfmain, unit, data, cmd, mode, p->p_uid, p);
334 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
335 * requires a large amount of setting up and isn't any more efficient.
341 struct tcphdr *tcp, *tcp2;
350 if (tcp->th_flags & TH_RST)
351 return -1; /* feedback loop */
353 if (ipf_checkl4sum(fin) == -1)
356 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
357 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
358 ((tcp->th_flags & TH_FIN) ? 1 : 0);
361 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
366 MGETHDR(m, M_NOWAIT, MT_HEADER);
368 MGET(m, M_NOWAIT, MT_HEADER);
372 if (sizeof(*tcp2) + hlen > MLEN) {
373 if (!(MCLGET(m, M_NOWAIT))) {
379 m->m_len = sizeof(*tcp2) + hlen;
381 m->m_data += max_linkhdr;
382 m->m_pkthdr.len = m->m_len;
383 m->m_pkthdr.rcvif = (struct ifnet *)0;
385 ip = mtod(m, struct ip *);
386 bzero((char *)ip, hlen);
390 tcp2 = (struct tcphdr *)((char *)ip + hlen);
391 tcp2->th_sport = tcp->th_dport;
392 tcp2->th_dport = tcp->th_sport;
394 if (tcp->th_flags & TH_ACK) {
395 tcp2->th_seq = tcp->th_ack;
396 tcp2->th_flags = TH_RST;
400 tcp2->th_ack = ntohl(tcp->th_seq);
401 tcp2->th_ack += tlen;
402 tcp2->th_ack = htonl(tcp2->th_ack);
403 tcp2->th_flags = TH_RST|TH_ACK;
406 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
407 tcp2->th_win = tcp->th_win;
412 if (fin->fin_v == 6) {
413 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
414 ip6->ip6_plen = htons(sizeof(struct tcphdr));
415 ip6->ip6_nxt = IPPROTO_TCP;
417 ip6->ip6_src = fin->fin_dst6.in6;
418 ip6->ip6_dst = fin->fin_src6.in6;
419 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
420 sizeof(*ip6), sizeof(*tcp2));
421 return ipf_send_ip(fin, m);
424 ip->ip_p = IPPROTO_TCP;
425 ip->ip_len = htons(sizeof(struct tcphdr));
426 ip->ip_src.s_addr = fin->fin_daddr;
427 ip->ip_dst.s_addr = fin->fin_saddr;
428 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
429 ip->ip_len = htons(hlen + sizeof(*tcp2));
430 return ipf_send_ip(fin, m);
435 * ip_len must be in network byte order when called.
446 ip = mtod(m, ip_t *);
447 bzero((char *)&fnew, sizeof(fnew));
448 fnew.fin_main_soft = fin->fin_main_soft;
450 IP_V_A(ip, fin->fin_v);
457 fnew.fin_p = ip->ip_p;
458 fnew.fin_plen = ntohs(ip->ip_len);
459 IP_HL_A(ip, sizeof(*oip) >> 2);
460 ip->ip_tos = oip->ip_tos;
461 ip->ip_id = fin->fin_ip->ip_id;
462 ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
463 ip->ip_ttl = V_ip_defttl;
469 ip6_t *ip6 = (ip6_t *)ip;
472 ip6->ip6_hlim = IPDEFTTL;
475 fnew.fin_p = ip6->ip6_nxt;
477 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
485 m->m_pkthdr.rcvif = NULL;
488 fnew.fin_ifp = fin->fin_ifp;
489 fnew.fin_flx = FI_NOCKSUM;
493 fnew.fin_hlen = hlen;
494 fnew.fin_dp = (char *)ip + hlen;
495 (void) ipf_makefrip(hlen, ip, &fnew);
497 return ipf_fastroute(m, &m, &fnew, NULL);
502 ipf_send_icmp_err(type, fin, dst)
507 int err, hlen, xtra, iclen, ohlen, avail, code;
518 if ((type < 0) || (type >= ICMP_MAXTYPE))
521 code = fin->fin_icode;
523 /* See NetBSD ip_fil_netbsd.c r1.4: */
524 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
528 if (ipf_checkl4sum(fin) == -1)
531 MGETHDR(m, M_NOWAIT, MT_HEADER);
533 MGET(m, M_NOWAIT, MT_HEADER);
544 if (fin->fin_v == 4) {
545 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
546 switch (ntohs(fin->fin_data[0]) >> 8)
559 if (ipf_ifpaddr(&V_ipfmain, 4, FRI_NORMAL, ifp,
560 &dst6, NULL) == -1) {
566 dst4.s_addr = fin->fin_daddr;
569 ohlen = fin->fin_hlen;
570 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
571 if (fin->fin_hlen < fin->fin_plen)
572 xtra = MIN(fin->fin_dlen, 8);
578 else if (fin->fin_v == 6) {
579 hlen = sizeof(ip6_t);
580 ohlen = sizeof(ip6_t);
581 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
582 type = icmptoicmp6types[type];
583 if (type == ICMP6_DST_UNREACH)
584 code = icmptoicmp6unreach[code];
586 if (iclen + max_linkhdr + fin->fin_plen > avail) {
587 if (!(MCLGET(m, M_NOWAIT))) {
593 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
594 xtra = MIN(xtra, IPV6_MMTU - iclen);
596 if (ipf_ifpaddr(&V_ipfmain, 6, FRI_NORMAL, ifp,
597 &dst6, NULL) == -1) {
602 dst6 = fin->fin_dst6;
610 avail -= (max_linkhdr + iclen);
618 m->m_data += max_linkhdr;
619 m->m_pkthdr.rcvif = (struct ifnet *)0;
620 m->m_pkthdr.len = iclen;
622 ip = mtod(m, ip_t *);
623 icmp = (struct icmp *)((char *)ip + hlen);
624 ip2 = (ip_t *)&icmp->icmp_ip;
626 icmp->icmp_type = type;
627 icmp->icmp_code = fin->fin_icode;
628 icmp->icmp_cksum = 0;
630 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
631 if (fin->fin_mtu != 0) {
632 icmp->icmp_nextmtu = htons(fin->fin_mtu);
634 } else if (ifp != NULL) {
635 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
637 } else { /* make up a number... */
638 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
643 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
647 if (fin->fin_v == 6) {
648 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
649 ip6->ip6_plen = htons(iclen - hlen);
650 ip6->ip6_nxt = IPPROTO_ICMPV6;
652 ip6->ip6_src = dst6.in6;
653 ip6->ip6_dst = fin->fin_src6.in6;
655 bcopy((char *)fin->fin_ip + ohlen,
656 (char *)&icmp->icmp_ip + ohlen, xtra);
657 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
658 sizeof(*ip6), iclen - hlen);
662 ip->ip_p = IPPROTO_ICMP;
663 ip->ip_src.s_addr = dst4.s_addr;
664 ip->ip_dst.s_addr = fin->fin_saddr;
667 bcopy((char *)fin->fin_ip + ohlen,
668 (char *)&icmp->icmp_ip + ohlen, xtra);
669 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
671 ip->ip_len = htons(iclen);
672 ip->ip_p = IPPROTO_ICMP;
674 err = ipf_send_ip(fin, m);
682 * m0 - pointer to mbuf where the IP packet starts
683 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
686 ipf_fastroute(m0, mpp, fin, fdp)
691 register struct ip *ip, *mhip;
692 register struct mbuf *m = *mpp;
693 int len, off, error = 0, hlen, code;
694 struct ifnet *ifp, *sifp;
695 struct sockaddr_in dst;
696 struct nhop4_extended nh4;
707 * If the mbuf we're about to send is not writable (because of
708 * a cluster reference, for example) we'll need to make a copy
709 * of it since this routine modifies the contents.
711 * If you have non-crappy network hardware that can transmit data
712 * from the mbuf, rather than making a copy, this is gonna be a
715 if (M_WRITABLE(m) == 0) {
716 m0 = m_dup(m, M_NOWAIT);
730 if (fin->fin_v == 6) {
732 * currently "to <if>" and "to <if>:ip#" are not supported
735 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
739 hlen = fin->fin_hlen;
740 ip = mtod(m0, struct ip *);
746 bzero(&dst, sizeof (dst));
747 dst.sin_family = AF_INET;
748 dst.sin_addr = ip->ip_dst;
749 dst.sin_len = sizeof(dst);
752 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
753 (fdp->fd_type == FRD_DSTLIST)) {
754 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
763 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
768 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
769 dst.sin_addr = fdp->fd_ip;
771 fibnum = M_GETFIB(m0);
772 if (fib4_lookup_nh_ext(fibnum, dst.sin_addr, NHR_REF, 0, &nh4) != 0) {
773 if (in_localaddr(ip->ip_dst))
774 error = EHOSTUNREACH;
783 if (nh4.nh_flags & NHF_GATEWAY)
784 dst.sin_addr = nh4.nh_addr;
787 * For input packets which are being "fastrouted", they won't
788 * go back through output filtering and miss their chance to get
789 * NAT'd and counted. Duplicated packets aren't considered to be
790 * part of the normal packet stream, so do not NAT them or pass
791 * them through stateful checking, etc.
793 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
797 (void) ipf_acctpkt(fin, NULL);
799 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
802 (void) ipf_state_check(fin, &pass);
805 switch (ipf_nat_checkout(fin, NULL))
823 * If small enough for interface, can just send directly.
825 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
827 ip->ip_sum = in_cksum(m, hlen);
828 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
834 * Too large for interface; fragment if possible.
835 * Must be able to put at least 8 bytes per fragment.
837 ip_off = ntohs(ip->ip_off);
838 if (ip_off & IP_DF) {
842 len = (ifp->if_mtu - hlen) &~ 7;
849 int mhlen, firstlen = len;
850 struct mbuf **mnext = &m->m_act;
853 * Loop through length of segment after first fragment,
854 * make new header and copy data of each part and link onto chain.
857 mhlen = sizeof (struct ip);
858 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
860 MGETHDR(m, M_NOWAIT, MT_HEADER);
862 MGET(m, M_NOWAIT, MT_HEADER);
869 m->m_data += max_linkhdr;
870 mhip = mtod(m, struct ip *);
871 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
872 if (hlen > sizeof (struct ip)) {
873 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
874 IP_HL_A(mhip, mhlen >> 2);
877 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
878 if (off + len >= ntohs(ip->ip_len))
879 len = ntohs(ip->ip_len) - off;
881 mhip->ip_off |= IP_MF;
882 mhip->ip_len = htons((u_short)(len + mhlen));
884 m->m_next = m_copym(m0, off, len, M_NOWAIT);
885 if (m->m_next == 0) {
886 error = ENOBUFS; /* ??? */
889 m->m_pkthdr.len = mhlen + len;
890 m->m_pkthdr.rcvif = NULL;
891 mhip->ip_off = htons((u_short)mhip->ip_off);
893 mhip->ip_sum = in_cksum(m, mhlen);
897 * Update first fragment by trimming what's been copied out
898 * and updating header, then send each fragment (in order).
900 m_adj(m0, hlen + firstlen - ip->ip_len);
901 ip->ip_len = htons((u_short)(hlen + firstlen));
902 ip->ip_off = htons((u_short)IP_MF);
904 ip->ip_sum = in_cksum(m0, hlen);
906 for (m = m0; m; m = m0) {
910 error = (*ifp->if_output)(ifp, m,
911 (struct sockaddr *)&dst,
920 V_ipfmain.ipf_frouteok[0]++;
922 V_ipfmain.ipf_frouteok[1]++;
925 fib4_free_nh_ext(fibnum, &nh4);
929 if (error == EMSGSIZE) {
931 code = fin->fin_icode;
932 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
934 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
936 fin->fin_icode = code;
947 struct nhop4_basic nh4;
949 if (fib4_lookup_nh_basic(0, fin->fin_src, 0, 0, &nh4) != 0)
951 return (fin->fin_ifp == nh4.nh_ifp);
956 * return the first IP Address associated with an interface
959 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
960 ipf_main_softc_t *softc;
963 i6addr_t *inp, *inpmask;
966 struct in6_addr *inp6 = NULL;
968 struct sockaddr *sock, *mask;
969 struct sockaddr_in *sin;
973 if ((ifptr == NULL) || (ifptr == (void *)-1))
983 bzero((char *)inp, sizeof(*inp));
985 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
987 sock = ifa->ifa_addr;
988 while (sock != NULL && ifa != NULL) {
989 sin = (struct sockaddr_in *)sock;
990 if ((v == 4) && (sin->sin_family == AF_INET))
993 if ((v == 6) && (sin->sin_family == AF_INET6)) {
994 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
995 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
996 !IN6_IS_ADDR_LOOPBACK(inp6))
1000 ifa = CK_STAILQ_NEXT(ifa, ifa_link);
1002 sock = ifa->ifa_addr;
1005 if (ifa == NULL || sin == NULL)
1008 mask = ifa->ifa_netmask;
1009 if (atype == FRI_BROADCAST)
1010 sock = ifa->ifa_broadaddr;
1011 else if (atype == FRI_PEERADDR)
1012 sock = ifa->ifa_dstaddr;
1019 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1020 (struct sockaddr_in6 *)mask,
1024 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1025 (struct sockaddr_in *)mask,
1026 &inp->in4, &inpmask->in4);
1035 newiss = arc4random();
1044 #ifdef CSUM_DATA_VALID
1050 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1053 if ((fin->fin_flx & FI_SHORT) != 0)
1056 if (fin->fin_cksum != FI_CK_NEEDED)
1057 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1066 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1068 fin->fin_cksum = FI_CK_BAD;
1069 fin->fin_flx |= FI_BAD;
1070 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));
1073 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1074 /* Depending on the driver, UDP may have zero checksum */
1075 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1076 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1077 udphdr_t *udp = fin->fin_dp;
1078 if (udp->uh_sum == 0) {
1080 * we're good no matter what the hardware
1081 * checksum flags and csum_data say (handling
1082 * of csum_data for zero UDP checksum is not
1083 * consistent across all drivers)
1090 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1091 sum = m->m_pkthdr.csum_data;
1093 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1094 htonl(m->m_pkthdr.csum_data +
1095 fin->fin_dlen + fin->fin_p));
1098 fin->fin_cksum = FI_CK_BAD;
1099 fin->fin_flx |= FI_BAD;
1100 DT2(ipf_fi_bad_checkv4sum_sum, fr_info_t *, fin, u_int, sum);
1102 fin->fin_cksum = FI_CK_SUMOK;
1106 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1107 fin->fin_cksum = FI_CK_L4FULL;
1109 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1110 m->m_pkthdr.csum_flags == CSUM_UDP) {
1111 fin->fin_cksum = FI_CK_L4PART;
1113 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1114 fin->fin_cksum = FI_CK_L4PART;
1122 if (ipf_checkl4sum(fin) == -1) {
1123 fin->fin_flx |= FI_BAD;
1124 DT2(ipf_fi_bad_checkv4sum_manual, fr_info_t *, fin, u_int, manual);
1129 if (ipf_checkl4sum(fin) == -1) {
1130 fin->fin_flx |= FI_BAD;
1131 DT2(ipf_fi_bad_checkv4sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1144 if ((fin->fin_flx & FI_NOCKSUM) != 0) {
1145 DT(ipf_checkv6sum_fi_nocksum);
1149 if ((fin->fin_flx & FI_SHORT) != 0) {
1150 DT(ipf_checkv6sum_fi_short);
1154 if (fin->fin_cksum != FI_CK_NEEDED) {
1155 DT(ipf_checkv6sum_fi_ck_needed);
1156 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1159 if (ipf_checkl4sum(fin) == -1) {
1160 fin->fin_flx |= FI_BAD;
1161 DT2(ipf_fi_bad_checkv6sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1166 #endif /* USE_INET6 */
1175 if ((m0->m_flags & M_PKTHDR) != 0) {
1176 len = m0->m_pkthdr.len;
1180 for (m = m0, len = 0; m != NULL; m = m->m_next)
1187 /* ------------------------------------------------------------------------ */
1188 /* Function: ipf_pullup */
1189 /* Returns: NULL == pullup failed, else pointer to protocol header */
1190 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1191 /* fin(I) - pointer to packet information */
1192 /* len(I) - number of bytes to pullup */
1194 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1195 /* single buffer for ease of access. Operating system native functions are */
1196 /* used to manage buffers - if necessary. If the entire packet ends up in */
1197 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1198 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1199 /* and ONLY if the pullup succeeds. */
1201 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1202 /* of buffers that starts at *fin->fin_mp. */
1203 /* ------------------------------------------------------------------------ */
1205 ipf_pullup(xmin, fin, len)
1217 ip = (char *)fin->fin_ip;
1218 if ((fin->fin_flx & FI_COALESCE) != 0)
1221 ipoff = fin->fin_ipoff;
1222 if (fin->fin_dp != NULL)
1223 dpoff = (char *)fin->fin_dp - (char *)ip;
1227 if (M_LEN(m) < len) {
1228 mb_t *n = *fin->fin_mp;
1230 * Assume that M_PKTHDR is set and just work with what is left
1231 * rather than check..
1232 * Should not make any real difference, anyway.
1236 * Record the mbuf that points to the mbuf that we're
1237 * about to go to work on so that we can update the
1238 * m_next appropriately later.
1240 for (; n->m_next != m; n = n->m_next)
1252 #ifdef HAVE_M_PULLDOWN
1253 if (m_pulldown(m, 0, len, NULL) == NULL)
1256 FREE_MB_T(*fin->fin_mp);
1262 m = m_pullup(m, len);
1268 * When n is non-NULL, it indicates that m pointed to
1269 * a sub-chain (tail) of the mbuf and that the head
1270 * of this chain has not yet been free'd.
1273 FREE_MB_T(*fin->fin_mp);
1276 *fin->fin_mp = NULL;
1284 while (M_LEN(m) == 0) {
1288 ip = MTOD(m, char *) + ipoff;
1290 fin->fin_ip = (ip_t *)ip;
1291 if (fin->fin_dp != NULL)
1292 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1293 if (fin->fin_fraghdr != NULL)
1294 fin->fin_fraghdr = (char *)ip +
1295 ((char *)fin->fin_fraghdr -
1296 (char *)fin->fin_ip);
1299 if (len == fin->fin_plen)
1300 fin->fin_flx |= FI_COALESCE;
1312 if (fin->fin_out == 0) {
1313 netisr_dispatch(NETISR_IP, m);
1315 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1316 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1317 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1323 VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet_hook);
1324 VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet6_hook);
1325 #define V_ipf_inet_hook VNET(ipf_inet_hook)
1326 #define V_ipf_inet6_hook VNET(ipf_inet6_hook)
1328 int ipf_pfil_unhook(void) {
1330 pfil_remove_hook(V_ipf_inet_hook);
1333 pfil_remove_hook(V_ipf_inet6_hook);
1339 int ipf_pfil_hook(void) {
1340 struct pfil_hook_args pha;
1341 struct pfil_link_args pla;
1344 pha.pa_version = PFIL_VERSION;
1345 pha.pa_flags = PFIL_IN | PFIL_OUT;
1346 pha.pa_modname = "ipfilter";
1347 pha.pa_rulname = "default";
1348 pha.pa_func = ipf_check_wrapper;
1349 pha.pa_ruleset = NULL;
1350 pha.pa_type = PFIL_TYPE_IP4;
1351 V_ipf_inet_hook = pfil_add_hook(&pha);
1354 pha.pa_func = ipf_check_wrapper6;
1355 pha.pa_type = PFIL_TYPE_IP6;
1356 V_ipf_inet6_hook = pfil_add_hook(&pha);
1359 pla.pa_version = PFIL_VERSION;
1360 pla.pa_flags = PFIL_IN | PFIL_OUT |
1361 PFIL_HEADPTR | PFIL_HOOKPTR;
1362 pla.pa_head = V_inet_pfil_head;
1363 pla.pa_hook = V_ipf_inet_hook;
1364 error = pfil_link(&pla);
1368 pla.pa_head = V_inet6_pfil_head;
1369 pla.pa_hook = V_ipf_inet6_hook;
1370 error6 = pfil_link(&pla);
1373 if (error || error6)
1384 ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1385 ipf_ifevent, NULL, \
1386 EVENTHANDLER_PRI_ANY);
1387 ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1388 ipf_ifevent, NULL, \
1389 EVENTHANDLER_PRI_ANY);
1391 ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1392 NULL, EVENTHANDLER_PRI_ANY);
1397 ipf_event_dereg(void)
1399 if (ipf_arrivetag != NULL) {
1400 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag);
1402 if (ipf_departtag != NULL) {
1403 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag);
1406 if (ipf_clonetag != NULL) {
1407 EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag);
1416 return arc4random();
1421 ipf_pcksum(fin, hlen, sum)
1431 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1434 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1439 * Both sum and sum2 are partial sums, so combine them together.
1441 sum += ~sum2 & 0xffff;
1442 while (sum > 0xffff)
1443 sum = (sum & 0xffff) + (sum >> 16);
1444 sum2 = ~sum & 0xffff;