3 * Copyright (C) 2012 by Darren Reed.
5 * See the IPFILTER.LICENCE file for details on licencing.
8 #if defined(KERNEL) || defined(_KERNEL)
14 #if defined(__FreeBSD__) && \
15 !defined(KLD_MODULE) && !defined(IPFILTER_LKM)
16 # include "opt_inet6.h"
18 #include <sys/param.h>
19 #include <sys/eventhandler.h>
21 #include <sys/errno.h>
22 #include <sys/types.h>
24 #include <sys/fcntl.h>
25 #include <sys/filio.h>
27 #include <sys/systm.h>
28 #include <sys/dirent.h>
29 #if defined(__FreeBSD__)
30 # include <sys/jail.h>
32 #include <sys/malloc.h>
34 #include <sys/sockopt.h>
35 #include <sys/socket.h>
36 #include <sys/selinfo.h>
38 #include <net/if_var.h>
39 #include <net/netisr.h>
40 #include <net/route.h>
41 #include <net/route/nhop.h>
42 #include <netinet/in.h>
43 #include <netinet/in_fib.h>
44 #include <netinet/in_pcb.h>
45 #include <netinet/in_var.h>
46 #include <netinet/in_systm.h>
47 #include <netinet/ip.h>
48 #include <netinet/ip_var.h>
49 #include <netinet/tcp.h>
50 #include <netinet/tcp_var.h>
52 #include <netinet/udp.h>
53 #include <netinet/tcpip.h>
54 #include <netinet/ip_icmp.h>
55 #include "netinet/ip_compat.h"
57 # include <netinet/icmp6.h>
59 #include "netinet/ip_fil.h"
60 #include "netinet/ip_nat.h"
61 #include "netinet/ip_frag.h"
62 #include "netinet/ip_state.h"
63 #include "netinet/ip_proxy.h"
64 #include "netinet/ip_auth.h"
65 #include "netinet/ip_sync.h"
66 #include "netinet/ip_lookup.h"
67 #include "netinet/ip_dstlist.h"
69 # include "netinet/ip_scan.h"
71 #include "netinet/ip_pool.h"
72 #include <sys/malloc.h>
73 #include <sys/kernel.h>
74 #ifdef CSUM_DATA_VALID
75 # include <machine/in_cksum.h>
77 extern int ip_optcopy(struct ip *, struct ip *);
79 #ifdef IPFILTER_M_IPFILTER
80 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
84 static int ipf_send_ip(fr_info_t *, mb_t *);
85 static void ipf_timer_func(void *arg);
87 VNET_DEFINE(ipf_main_softc_t, ipfmain) = {
90 #define V_ipfmain VNET(ipfmain)
95 VNET_DEFINE_STATIC(eventhandler_tag, ipf_arrivetag);
96 VNET_DEFINE_STATIC(eventhandler_tag, ipf_departtag);
97 #define V_ipf_arrivetag VNET(ipf_arrivetag)
98 #define V_ipf_departtag VNET(ipf_departtag)
101 * Disable the "cloner" event handler; we are getting interface
102 * events before the firewall is fully initiallized and also no vnet
103 * information thus leading to uninitialised memory accesses.
104 * In addition it is unclear why we need it in first place.
105 * If it turns out to be needed, well need a dedicated event handler
106 * for it to deal with the ifc and the correct vnet.
108 VNET_DEFINE_STATIC(eventhandler_tag, ipf_clonetag);
109 #define V_ipf_clonetag VNET(ipf_clonetag)
112 static void ipf_ifevent(void *arg, struct ifnet *ifp);
114 static void ipf_ifevent(void *arg, struct ifnet *ifp)
117 CURVNET_SET(ifp->if_vnet);
118 if (V_ipfmain.ipf_running > 0)
119 ipf_sync(&V_ipfmain, NULL);
126 ipf_check_wrapper(struct mbuf **mp, struct ifnet *ifp, int flags,
127 void *ruleset __unused, struct inpcb *inp)
129 struct ip *ip = mtod(*mp, struct ip *);
132 CURVNET_SET(ifp->if_vnet);
133 rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp,
134 !!(flags & PFIL_OUT), mp);
136 return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
141 ipf_check_wrapper6(struct mbuf **mp, struct ifnet *ifp, int flags,
142 void *ruleset __unused, struct inpcb *inp)
146 CURVNET_SET(ifp->if_vnet);
147 rv = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
148 sizeof(struct ip6_hdr), ifp, !!(flags & PFIL_OUT), mp);
151 return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
154 #if defined(IPFILTER_LKM)
155 int ipf_identify(char *s)
157 if (strcmp(s, "ipl") == 0)
161 #endif /* IPFILTER_LKM */
165 ipf_timer_func(void *arg)
167 ipf_main_softc_t *softc = arg;
171 READ_ENTER(&softc->ipf_global);
173 if (softc->ipf_running > 0)
174 ipf_slowtimer(softc);
176 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
178 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
180 callout_init(&softc->ipf_slow_ch, 1);
181 callout_reset(&softc->ipf_slow_ch,
182 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
183 ipf_timer_func, softc);
185 RWLOCK_EXIT(&softc->ipf_global);
191 ipfattach(ipf_main_softc_t *softc)
198 if (softc->ipf_running > 0) {
203 if (ipf_init_all(softc) < 0) {
209 bzero((char *)V_ipfmain.ipf_selwait, sizeof(V_ipfmain.ipf_selwait));
210 softc->ipf_running = 1;
212 if (softc->ipf_control_forwarding & 1)
217 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
218 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
220 callout_init(&softc->ipf_slow_ch, 1);
221 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
222 ipf_timer_func, softc);
228 * Disable the filter by removing the hooks from the IP input/output
232 ipfdetach(ipf_main_softc_t *softc)
238 if (softc->ipf_control_forwarding & 2)
244 if (softc->ipf_slow_ch.callout != NULL)
245 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
246 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
248 callout_drain(&softc->ipf_slow_ch);
252 softc->ipf_running = -2;
261 * Filter ioctl interface.
264 ipfioctl(struct cdev *dev, ioctlcmd_t cmd, caddr_t data,
265 int mode, struct thread *p)
266 #define p_cred td_ucred
267 #define p_uid td_ucred->cr_ruid
269 int error = 0, unit = 0;
272 CURVNET_SET(TD_TO_VNET(p));
273 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
275 V_ipfmain.ipf_interror = 130001;
280 if (jailed_without_vnet(p->p_cred)) {
281 V_ipfmain.ipf_interror = 130018;
286 unit = GET_MINOR(dev);
287 if ((IPL_LOGMAX < unit) || (unit < 0)) {
288 V_ipfmain.ipf_interror = 130002;
293 if (V_ipfmain.ipf_running <= 0) {
294 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
295 V_ipfmain.ipf_interror = 130003;
299 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
300 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
301 cmd != SIOCGETFS && cmd != SIOCGETFF &&
302 cmd != SIOCIPFINTERROR) {
303 V_ipfmain.ipf_interror = 130004;
311 error = ipf_ioctlswitch(&V_ipfmain, unit, data, cmd, mode, p->p_uid, p);
325 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
326 * requires a large amount of setting up and isn't any more efficient.
329 ipf_send_reset(fr_info_t *fin)
331 struct tcphdr *tcp, *tcp2;
340 if (tcp->th_flags & TH_RST)
341 return (-1); /* feedback loop */
343 if (ipf_checkl4sum(fin) == -1)
346 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
347 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
348 ((tcp->th_flags & TH_FIN) ? 1 : 0);
351 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
356 MGETHDR(m, M_NOWAIT, MT_HEADER);
358 MGET(m, M_NOWAIT, MT_HEADER);
362 if (sizeof(*tcp2) + hlen > MLEN) {
363 if (!(MCLGET(m, M_NOWAIT))) {
369 m->m_len = sizeof(*tcp2) + hlen;
370 m->m_data += max_linkhdr;
371 m->m_pkthdr.len = m->m_len;
372 m->m_pkthdr.rcvif = (struct ifnet *)0;
373 ip = mtod(m, struct ip *);
374 bzero((char *)ip, hlen);
378 tcp2 = (struct tcphdr *)((char *)ip + hlen);
379 tcp2->th_sport = tcp->th_dport;
380 tcp2->th_dport = tcp->th_sport;
382 if (tcp_get_flags(tcp) & TH_ACK) {
383 tcp2->th_seq = tcp->th_ack;
384 tcp_set_flags(tcp2, TH_RST);
388 tcp2->th_ack = ntohl(tcp->th_seq);
389 tcp2->th_ack += tlen;
390 tcp2->th_ack = htonl(tcp2->th_ack);
391 tcp_set_flags(tcp2, TH_RST|TH_ACK);
393 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
394 tcp2->th_win = tcp->th_win;
399 if (fin->fin_v == 6) {
400 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
401 ip6->ip6_plen = htons(sizeof(struct tcphdr));
402 ip6->ip6_nxt = IPPROTO_TCP;
404 ip6->ip6_src = fin->fin_dst6.in6;
405 ip6->ip6_dst = fin->fin_src6.in6;
406 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
407 sizeof(*ip6), sizeof(*tcp2));
408 return (ipf_send_ip(fin, m));
411 ip->ip_p = IPPROTO_TCP;
412 ip->ip_len = htons(sizeof(struct tcphdr));
413 ip->ip_src.s_addr = fin->fin_daddr;
414 ip->ip_dst.s_addr = fin->fin_saddr;
415 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
416 ip->ip_len = htons(hlen + sizeof(*tcp2));
417 return (ipf_send_ip(fin, m));
422 * ip_len must be in network byte order when called.
425 ipf_send_ip(fr_info_t *fin, mb_t *m)
431 ip = mtod(m, ip_t *);
432 bzero((char *)&fnew, sizeof(fnew));
433 fnew.fin_main_soft = fin->fin_main_soft;
435 IP_V_A(ip, fin->fin_v);
442 fnew.fin_p = ip->ip_p;
443 fnew.fin_plen = ntohs(ip->ip_len);
444 IP_HL_A(ip, sizeof(*oip) >> 2);
445 ip->ip_tos = oip->ip_tos;
446 ip->ip_id = fin->fin_ip->ip_id;
447 ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
448 ip->ip_ttl = V_ip_defttl;
454 ip6_t *ip6 = (ip6_t *)ip;
457 ip6->ip6_hlim = IPDEFTTL;
460 fnew.fin_p = ip6->ip6_nxt;
462 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
470 m->m_pkthdr.rcvif = NULL;
473 fnew.fin_ifp = fin->fin_ifp;
474 fnew.fin_flx = FI_NOCKSUM;
478 fnew.fin_hlen = hlen;
479 fnew.fin_dp = (char *)ip + hlen;
480 (void) ipf_makefrip(hlen, ip, &fnew);
482 return (ipf_fastroute(m, &m, &fnew, NULL));
487 ipf_send_icmp_err(int type, fr_info_t *fin, int dst)
489 int err, hlen, xtra, iclen, ohlen, avail, code;
500 if ((type < 0) || (type >= ICMP_MAXTYPE))
503 code = fin->fin_icode;
505 /* See NetBSD ip_fil_netbsd.c r1.4: */
506 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
510 if (ipf_checkl4sum(fin) == -1)
513 MGETHDR(m, M_NOWAIT, MT_HEADER);
515 MGET(m, M_NOWAIT, MT_HEADER);
526 if (fin->fin_v == 4) {
527 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
528 switch (ntohs(fin->fin_data[0]) >> 8)
541 if (ipf_ifpaddr(&V_ipfmain, 4, FRI_NORMAL, ifp,
542 &dst6, NULL) == -1) {
548 dst4.s_addr = fin->fin_daddr;
551 ohlen = fin->fin_hlen;
552 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
553 if (fin->fin_hlen < fin->fin_plen)
554 xtra = MIN(fin->fin_dlen, 8);
560 else if (fin->fin_v == 6) {
561 hlen = sizeof(ip6_t);
562 ohlen = sizeof(ip6_t);
563 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
564 type = icmptoicmp6types[type];
565 if (type == ICMP6_DST_UNREACH)
566 code = icmptoicmp6unreach[code];
568 if (iclen + max_linkhdr + fin->fin_plen > avail) {
569 if (!(MCLGET(m, M_NOWAIT))) {
575 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
576 xtra = MIN(xtra, IPV6_MMTU - iclen);
578 if (ipf_ifpaddr(&V_ipfmain, 6, FRI_NORMAL, ifp,
579 &dst6, NULL) == -1) {
584 dst6 = fin->fin_dst6;
592 avail -= (max_linkhdr + iclen);
600 m->m_data += max_linkhdr;
601 m->m_pkthdr.rcvif = (struct ifnet *)0;
602 m->m_pkthdr.len = iclen;
604 ip = mtod(m, ip_t *);
605 icmp = (struct icmp *)((char *)ip + hlen);
606 ip2 = (ip_t *)&icmp->icmp_ip;
608 icmp->icmp_type = type;
609 icmp->icmp_code = fin->fin_icode;
610 icmp->icmp_cksum = 0;
612 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
613 if (fin->fin_mtu != 0) {
614 icmp->icmp_nextmtu = htons(fin->fin_mtu);
616 } else if (ifp != NULL) {
617 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
619 } else { /* make up a number... */
620 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
625 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
629 if (fin->fin_v == 6) {
630 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
631 ip6->ip6_plen = htons(iclen - hlen);
632 ip6->ip6_nxt = IPPROTO_ICMPV6;
634 ip6->ip6_src = dst6.in6;
635 ip6->ip6_dst = fin->fin_src6.in6;
637 bcopy((char *)fin->fin_ip + ohlen,
638 (char *)&icmp->icmp_ip + ohlen, xtra);
639 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
640 sizeof(*ip6), iclen - hlen);
644 ip->ip_p = IPPROTO_ICMP;
645 ip->ip_src.s_addr = dst4.s_addr;
646 ip->ip_dst.s_addr = fin->fin_saddr;
649 bcopy((char *)fin->fin_ip + ohlen,
650 (char *)&icmp->icmp_ip + ohlen, xtra);
651 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
653 ip->ip_len = htons(iclen);
654 ip->ip_p = IPPROTO_ICMP;
656 err = ipf_send_ip(fin, m);
664 * m0 - pointer to mbuf where the IP packet starts
665 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
668 ipf_fastroute(mb_t *m0, mb_t **mpp, fr_info_t *fin, frdest_t *fdp)
670 register struct ip *ip, *mhip;
671 register struct mbuf *m = *mpp;
672 int len, off, error = 0, hlen, code;
673 struct ifnet *ifp, *sifp;
675 struct sockaddr_in *dst;
676 const struct sockaddr *gw;
677 struct nhop_object *nh;
687 * If the mbuf we're about to send is not writable (because of
688 * a cluster reference, for example) we'll need to make a copy
689 * of it since this routine modifies the contents.
691 * If you have non-crappy network hardware that can transmit data
692 * from the mbuf, rather than making a copy, this is gonna be a
695 if (M_WRITABLE(m) == 0) {
696 m0 = m_dup(m, M_NOWAIT);
710 if (fin->fin_v == 6) {
712 * currently "to <if>" and "to <if>:ip#" are not supported
715 return (ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL));
719 hlen = fin->fin_hlen;
720 ip = mtod(m0, struct ip *);
726 bzero(&ro, sizeof (ro));
727 dst = (struct sockaddr_in *)&ro.ro_dst;
728 dst->sin_family = AF_INET;
729 dst->sin_addr = ip->ip_dst;
730 dst->sin_len = sizeof(dst);
731 gw = (const struct sockaddr *)dst;
734 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
735 (fdp->fd_type == FRD_DSTLIST)) {
736 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
745 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
750 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
751 dst->sin_addr = fdp->fd_ip;
753 fibnum = M_GETFIB(m0);
755 nh = fib4_lookup(fibnum, dst->sin_addr, 0, NHR_NONE, 0);
757 if (in_localaddr(ip->ip_dst))
758 error = EHOSTUNREACH;
766 if (nh->nh_flags & NHF_GATEWAY) {
768 ro.ro_flags |= RT_HAS_GW;
772 * For input packets which are being "fastrouted", they won't
773 * go back through output filtering and miss their chance to get
774 * NAT'd and counted. Duplicated packets aren't considered to be
775 * part of the normal packet stream, so do not NAT them or pass
776 * them through stateful checking, etc.
778 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
782 (void) ipf_acctpkt(fin, NULL);
784 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
787 (void) ipf_state_check(fin, &pass);
790 switch (ipf_nat_checkout(fin, NULL))
808 * If small enough for interface, can just send directly.
810 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
812 ip->ip_sum = in_cksum(m, hlen);
813 error = (*ifp->if_output)(ifp, m, gw, &ro);
817 * Too large for interface; fragment if possible.
818 * Must be able to put at least 8 bytes per fragment.
820 ip_off = ntohs(ip->ip_off);
821 if (ip_off & IP_DF) {
825 len = (ifp->if_mtu - hlen) &~ 7;
832 int mhlen, firstlen = len;
833 struct mbuf **mnext = &m->m_act;
836 * Loop through length of segment after first fragment,
837 * make new header and copy data of each part and link onto chain.
840 mhlen = sizeof (struct ip);
841 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
843 MGETHDR(m, M_NOWAIT, MT_HEADER);
845 MGET(m, M_NOWAIT, MT_HEADER);
852 m->m_data += max_linkhdr;
853 mhip = mtod(m, struct ip *);
854 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
855 if (hlen > sizeof (struct ip)) {
856 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
857 IP_HL_A(mhip, mhlen >> 2);
860 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
861 if (off + len >= ntohs(ip->ip_len))
862 len = ntohs(ip->ip_len) - off;
864 mhip->ip_off |= IP_MF;
865 mhip->ip_len = htons((u_short)(len + mhlen));
867 m->m_next = m_copym(m0, off, len, M_NOWAIT);
868 if (m->m_next == 0) {
869 error = ENOBUFS; /* ??? */
872 m->m_pkthdr.len = mhlen + len;
873 m->m_pkthdr.rcvif = NULL;
874 mhip->ip_off = htons((u_short)mhip->ip_off);
876 mhip->ip_sum = in_cksum(m, mhlen);
880 * Update first fragment by trimming what's been copied out
881 * and updating header, then send each fragment (in order).
883 m_adj(m0, hlen + firstlen - ip->ip_len);
884 ip->ip_len = htons((u_short)(hlen + firstlen));
885 ip->ip_off = htons((u_short)IP_MF);
887 ip->ip_sum = in_cksum(m0, hlen);
889 for (m = m0; m; m = m0) {
893 error = (*ifp->if_output)(ifp, m, gw, &ro);
900 V_ipfmain.ipf_frouteok[0]++;
902 V_ipfmain.ipf_frouteok[1]++;
906 if (error == EMSGSIZE) {
908 code = fin->fin_icode;
909 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
911 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
913 fin->fin_icode = code;
921 ipf_verifysrc(fr_info_t *fin)
923 struct nhop_object *nh;
926 nh = fib4_lookup(RT_DEFAULT_FIB, fin->fin_src, 0, NHR_NONE, 0);
929 return (fin->fin_ifp == nh->nh_ifp);
934 * return the first IP Address associated with an interface
937 ipf_ifpaddr(ipf_main_softc_t *softc, int v, int atype, void *ifptr,
938 i6addr_t *inp, i6addr_t *inpmask)
941 struct in6_addr *ia6 = NULL;
943 struct sockaddr *sock, *mask;
944 struct sockaddr_in *sin;
948 if ((ifptr == NULL) || (ifptr == (void *)-1))
958 bzero((char *)inp, sizeof(*inp));
960 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
962 sock = ifa->ifa_addr;
963 while (sock != NULL && ifa != NULL) {
964 sin = (struct sockaddr_in *)sock;
965 if ((v == 4) && (sin->sin_family == AF_INET))
968 if ((v == 6) && (sin->sin_family == AF_INET6)) {
969 ia6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
970 if (!IN6_IS_ADDR_LINKLOCAL(ia6) &&
971 !IN6_IS_ADDR_LOOPBACK(ia6))
975 ifa = CK_STAILQ_NEXT(ifa, ifa_link);
977 sock = ifa->ifa_addr;
980 if (ifa == NULL || sin == NULL)
983 mask = ifa->ifa_netmask;
984 if (atype == FRI_BROADCAST)
985 sock = ifa->ifa_broadaddr;
986 else if (atype == FRI_PEERADDR)
987 sock = ifa->ifa_dstaddr;
994 return (ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
995 (struct sockaddr_in6 *)mask,
999 return (ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1000 (struct sockaddr_in *)mask,
1001 &inp->in4, &inpmask->in4));
1006 ipf_newisn(fr_info_t *fin)
1009 newiss = arc4random();
1015 ipf_checkv4sum(fr_info_t *fin)
1017 #ifdef CSUM_DATA_VALID
1023 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1026 if ((fin->fin_flx & FI_SHORT) != 0)
1029 if (fin->fin_cksum != FI_CK_NEEDED)
1030 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1039 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1041 fin->fin_cksum = FI_CK_BAD;
1042 fin->fin_flx |= FI_BAD;
1043 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));
1046 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1047 /* Depending on the driver, UDP may have zero checksum */
1048 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1049 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1050 udphdr_t *udp = fin->fin_dp;
1051 if (udp->uh_sum == 0) {
1053 * we're good no matter what the hardware
1054 * checksum flags and csum_data say (handling
1055 * of csum_data for zero UDP checksum is not
1056 * consistent across all drivers)
1063 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1064 sum = m->m_pkthdr.csum_data;
1066 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1067 htonl(m->m_pkthdr.csum_data +
1068 fin->fin_dlen + fin->fin_p));
1071 fin->fin_cksum = FI_CK_BAD;
1072 fin->fin_flx |= FI_BAD;
1073 DT2(ipf_fi_bad_checkv4sum_sum, fr_info_t *, fin, u_int, sum);
1075 fin->fin_cksum = FI_CK_SUMOK;
1079 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1080 fin->fin_cksum = FI_CK_L4FULL;
1082 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1083 m->m_pkthdr.csum_flags == CSUM_UDP ||
1084 m->m_pkthdr.csum_flags == CSUM_IP) {
1085 fin->fin_cksum = FI_CK_L4PART;
1093 if (ipf_checkl4sum(fin) == -1) {
1094 fin->fin_flx |= FI_BAD;
1095 DT2(ipf_fi_bad_checkv4sum_manual, fr_info_t *, fin, u_int, manual);
1100 if (ipf_checkl4sum(fin) == -1) {
1101 fin->fin_flx |= FI_BAD;
1102 DT2(ipf_fi_bad_checkv4sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1112 ipf_checkv6sum(fr_info_t *fin)
1114 if ((fin->fin_flx & FI_NOCKSUM) != 0) {
1115 DT(ipf_checkv6sum_fi_nocksum);
1119 if ((fin->fin_flx & FI_SHORT) != 0) {
1120 DT(ipf_checkv6sum_fi_short);
1124 if (fin->fin_cksum != FI_CK_NEEDED) {
1125 DT(ipf_checkv6sum_fi_ck_needed);
1126 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1129 if (ipf_checkl4sum(fin) == -1) {
1130 fin->fin_flx |= FI_BAD;
1131 DT2(ipf_fi_bad_checkv6sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1136 #endif /* USE_INET6 */
1140 mbufchainlen(struct mbuf *m0)
1144 if ((m0->m_flags & M_PKTHDR) != 0) {
1145 len = m0->m_pkthdr.len;
1149 for (m = m0, len = 0; m != NULL; m = m->m_next)
1156 /* ------------------------------------------------------------------------ */
1157 /* Function: ipf_pullup */
1158 /* Returns: NULL == pullup failed, else pointer to protocol header */
1159 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1160 /* fin(I) - pointer to packet information */
1161 /* len(I) - number of bytes to pullup */
1163 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1164 /* single buffer for ease of access. Operating system native functions are */
1165 /* used to manage buffers - if necessary. If the entire packet ends up in */
1166 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1167 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1168 /* and ONLY if the pullup succeeds. */
1170 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1171 /* of buffers that starts at *fin->fin_mp. */
1172 /* ------------------------------------------------------------------------ */
1174 ipf_pullup(mb_t *xmin, fr_info_t *fin, int len)
1184 if ((fin->fin_flx & FI_COALESCE) != 0)
1187 ipoff = fin->fin_ipoff;
1188 if (fin->fin_dp != NULL)
1189 dpoff = (char *)fin->fin_dp - (char *)ip;
1193 if (M_LEN(m) < len) {
1194 mb_t *n = *fin->fin_mp;
1196 * Assume that M_PKTHDR is set and just work with what is left
1197 * rather than check..
1198 * Should not make any real difference, anyway.
1202 * Record the mbuf that points to the mbuf that we're
1203 * about to go to work on so that we can update the
1204 * m_next appropriately later.
1206 for (; n->m_next != m; n = n->m_next)
1218 #ifdef HAVE_M_PULLDOWN
1219 if (m_pulldown(m, 0, len, NULL) == NULL)
1222 FREE_MB_T(*fin->fin_mp);
1229 m = m_pullup(m, len);
1235 * When n is non-NULL, it indicates that m pointed to
1236 * a sub-chain (tail) of the mbuf and that the head
1237 * of this chain has not yet been free'd.
1240 FREE_MB_T(*fin->fin_mp);
1243 *fin->fin_mp = NULL;
1251 while (M_LEN(m) == 0) {
1255 ip = MTOD(m, ip_t *) + ipoff;
1258 if (fin->fin_dp != NULL)
1259 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1260 if (fin->fin_fraghdr != NULL)
1261 fin->fin_fraghdr = (char *)ip +
1262 ((char *)fin->fin_fraghdr -
1263 (char *)fin->fin_ip);
1266 if (len == fin->fin_plen)
1267 fin->fin_flx |= FI_COALESCE;
1273 ipf_inject(fr_info_t *fin, mb_t *m)
1275 struct epoch_tracker et;
1278 NET_EPOCH_ENTER(et);
1279 if (fin->fin_out == 0) {
1280 netisr_dispatch(NETISR_IP, m);
1282 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1283 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1284 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1291 VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet_hook);
1292 VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet6_hook);
1293 #define V_ipf_inet_hook VNET(ipf_inet_hook)
1294 #define V_ipf_inet6_hook VNET(ipf_inet6_hook)
1296 int ipf_pfil_unhook(void) {
1298 pfil_remove_hook(V_ipf_inet_hook);
1301 pfil_remove_hook(V_ipf_inet6_hook);
1307 int ipf_pfil_hook(void) {
1310 struct pfil_hook_args pha = {
1311 .pa_version = PFIL_VERSION,
1312 .pa_flags = PFIL_IN | PFIL_OUT,
1313 .pa_modname = "ipfilter",
1314 .pa_rulname = "default-ip4",
1315 .pa_mbuf_chk = ipf_check_wrapper,
1316 .pa_type = PFIL_TYPE_IP4,
1318 V_ipf_inet_hook = pfil_add_hook(&pha);
1321 pha.pa_rulname = "default-ip6";
1322 pha.pa_mbuf_chk = ipf_check_wrapper6;
1323 pha.pa_type = PFIL_TYPE_IP6;
1324 V_ipf_inet6_hook = pfil_add_hook(&pha);
1327 struct pfil_link_args pla = {
1328 .pa_version = PFIL_VERSION,
1329 .pa_flags = PFIL_IN | PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR,
1330 .pa_head = V_inet_pfil_head,
1331 .pa_hook = V_ipf_inet_hook,
1333 error = pfil_link(&pla);
1337 pla.pa_head = V_inet6_pfil_head;
1338 pla.pa_hook = V_ipf_inet6_hook;
1339 error6 = pfil_link(&pla);
1342 if (error || error6)
1353 V_ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1354 ipf_ifevent, NULL, \
1355 EVENTHANDLER_PRI_ANY);
1356 V_ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1357 ipf_ifevent, NULL, \
1358 EVENTHANDLER_PRI_ANY);
1360 V_ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1361 NULL, EVENTHANDLER_PRI_ANY);
1366 ipf_event_dereg(void)
1368 if (V_ipf_arrivetag != NULL) {
1369 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, V_ipf_arrivetag);
1371 if (V_ipf_departtag != NULL) {
1372 EVENTHANDLER_DEREGISTER(ifnet_departure_event, V_ipf_departtag);
1375 if (V_ipf_clonetag != NULL) {
1376 EVENTHANDLER_DEREGISTER(if_clone_event, V_ipf_clonetag);
1385 return (arc4random());
1390 ipf_pcksum(fr_info_t *fin, int hlen, u_int sum)
1397 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1400 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1405 * Both sum and sum2 are partial sums, so combine them together.
1407 sum += ~sum2 & 0xffff;
1408 while (sum > 0xffff)
1409 sum = (sum & 0xffff) + (sum >> 16);
1410 sum2 = ~sum & 0xffff;
1416 ipf_pcksum6(struct mbuf *m, ip6_t *ip6, u_int32_t off, u_int32_t len)
1421 if (m->m_len < sizeof(struct ip6_hdr)) {
1425 sum = in6_cksum(m, ip6->ip6_nxt, off, len);
1431 sp = (u_short *)&ip6->ip6_src;
1432 sum = *sp++; /* ip6_src */
1440 sum += *sp++; /* ip6_dst */
1448 return (ipf_pcksum(fin, off, sum));
1454 ipf_fbsd_kenv_get(ipf_main_softc_t *softc)
1456 TUNABLE_INT_FETCH("net.inet.ipf.large_nat",
1457 &softc->ipf_large_nat);