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 if (rv == 0 && *mp == NULL)
137 return (PFIL_CONSUMED);
138 return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
143 ipf_check_wrapper6(struct mbuf **mp, struct ifnet *ifp, int flags,
144 void *ruleset __unused, struct inpcb *inp)
148 CURVNET_SET(ifp->if_vnet);
149 rv = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
150 sizeof(struct ip6_hdr), ifp, !!(flags & PFIL_OUT), mp);
152 if (rv == 0 && *mp == NULL)
153 return (PFIL_CONSUMED);
155 return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
158 #if defined(IPFILTER_LKM)
159 int ipf_identify(char *s)
161 if (strcmp(s, "ipl") == 0)
165 #endif /* IPFILTER_LKM */
169 ipf_timer_func(void *arg)
171 ipf_main_softc_t *softc = arg;
175 READ_ENTER(&softc->ipf_global);
177 if (softc->ipf_running > 0)
178 ipf_slowtimer(softc);
180 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
182 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
184 callout_init(&softc->ipf_slow_ch, 1);
185 callout_reset(&softc->ipf_slow_ch,
186 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
187 ipf_timer_func, softc);
189 RWLOCK_EXIT(&softc->ipf_global);
195 ipfattach(ipf_main_softc_t *softc)
202 if (softc->ipf_running > 0) {
207 if (ipf_init_all(softc) < 0) {
213 bzero((char *)V_ipfmain.ipf_selwait, sizeof(V_ipfmain.ipf_selwait));
214 softc->ipf_running = 1;
216 if (softc->ipf_control_forwarding & 1)
221 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
222 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
224 callout_init(&softc->ipf_slow_ch, 1);
225 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
226 ipf_timer_func, softc);
232 * Disable the filter by removing the hooks from the IP input/output
236 ipfdetach(ipf_main_softc_t *softc)
242 if (softc->ipf_control_forwarding & 2)
248 if (softc->ipf_slow_ch.callout != NULL)
249 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
250 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
252 callout_drain(&softc->ipf_slow_ch);
256 softc->ipf_running = -2;
265 * Filter ioctl interface.
268 ipfioctl(struct cdev *dev, ioctlcmd_t cmd, caddr_t data,
269 int mode, struct thread *p)
270 #define p_cred td_ucred
271 #define p_uid td_ucred->cr_ruid
273 int error = 0, unit = 0;
276 CURVNET_SET(TD_TO_VNET(p));
277 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
279 V_ipfmain.ipf_interror = 130001;
284 if (jailed_without_vnet(p->p_cred)) {
285 V_ipfmain.ipf_interror = 130018;
290 unit = GET_MINOR(dev);
291 if ((IPL_LOGMAX < unit) || (unit < 0)) {
292 V_ipfmain.ipf_interror = 130002;
297 if (V_ipfmain.ipf_running <= 0) {
298 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
299 V_ipfmain.ipf_interror = 130003;
303 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
304 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
305 cmd != SIOCGETFS && cmd != SIOCGETFF &&
306 cmd != SIOCIPFINTERROR) {
307 V_ipfmain.ipf_interror = 130004;
315 error = ipf_ioctlswitch(&V_ipfmain, unit, data, cmd, mode, p->p_uid, p);
329 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
330 * requires a large amount of setting up and isn't any more efficient.
333 ipf_send_reset(fr_info_t *fin)
335 struct tcphdr *tcp, *tcp2;
344 if (tcp->th_flags & TH_RST)
345 return (-1); /* feedback loop */
347 if (ipf_checkl4sum(fin) == -1)
350 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
351 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
352 ((tcp->th_flags & TH_FIN) ? 1 : 0);
355 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
360 MGETHDR(m, M_NOWAIT, MT_HEADER);
362 MGET(m, M_NOWAIT, MT_HEADER);
366 if (sizeof(*tcp2) + hlen > MLEN) {
367 if (!(MCLGET(m, M_NOWAIT))) {
373 m->m_len = sizeof(*tcp2) + hlen;
374 m->m_data += max_linkhdr;
375 m->m_pkthdr.len = m->m_len;
376 m->m_pkthdr.rcvif = (struct ifnet *)0;
377 ip = mtod(m, struct ip *);
378 bzero((char *)ip, hlen);
382 tcp2 = (struct tcphdr *)((char *)ip + hlen);
383 tcp2->th_sport = tcp->th_dport;
384 tcp2->th_dport = tcp->th_sport;
386 if (tcp_get_flags(tcp) & TH_ACK) {
387 tcp2->th_seq = tcp->th_ack;
388 tcp_set_flags(tcp2, TH_RST);
392 tcp2->th_ack = ntohl(tcp->th_seq);
393 tcp2->th_ack += tlen;
394 tcp2->th_ack = htonl(tcp2->th_ack);
395 tcp_set_flags(tcp2, TH_RST|TH_ACK);
397 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
398 tcp2->th_win = tcp->th_win;
403 if (fin->fin_v == 6) {
404 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
405 ip6->ip6_plen = htons(sizeof(struct tcphdr));
406 ip6->ip6_nxt = IPPROTO_TCP;
408 ip6->ip6_src = fin->fin_dst6.in6;
409 ip6->ip6_dst = fin->fin_src6.in6;
410 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
411 sizeof(*ip6), sizeof(*tcp2));
412 return (ipf_send_ip(fin, m));
415 ip->ip_p = IPPROTO_TCP;
416 ip->ip_len = htons(sizeof(struct tcphdr));
417 ip->ip_src.s_addr = fin->fin_daddr;
418 ip->ip_dst.s_addr = fin->fin_saddr;
419 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
420 ip->ip_len = htons(hlen + sizeof(*tcp2));
421 return (ipf_send_ip(fin, m));
426 * ip_len must be in network byte order when called.
429 ipf_send_ip(fr_info_t *fin, mb_t *m)
435 ip = mtod(m, ip_t *);
436 bzero((char *)&fnew, sizeof(fnew));
437 fnew.fin_main_soft = fin->fin_main_soft;
439 IP_V_A(ip, fin->fin_v);
446 fnew.fin_p = ip->ip_p;
447 fnew.fin_plen = ntohs(ip->ip_len);
448 IP_HL_A(ip, sizeof(*oip) >> 2);
449 ip->ip_tos = oip->ip_tos;
450 ip->ip_id = fin->fin_ip->ip_id;
451 ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
452 ip->ip_ttl = V_ip_defttl;
458 ip6_t *ip6 = (ip6_t *)ip;
461 ip6->ip6_hlim = IPDEFTTL;
464 fnew.fin_p = ip6->ip6_nxt;
466 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
474 m->m_pkthdr.rcvif = NULL;
477 fnew.fin_ifp = fin->fin_ifp;
478 fnew.fin_flx = FI_NOCKSUM;
482 fnew.fin_hlen = hlen;
483 fnew.fin_dp = (char *)ip + hlen;
484 (void) ipf_makefrip(hlen, ip, &fnew);
486 return (ipf_fastroute(m, &m, &fnew, NULL));
491 ipf_send_icmp_err(int type, fr_info_t *fin, int dst)
493 int err, hlen, xtra, iclen, ohlen, avail, code;
504 if ((type < 0) || (type >= ICMP_MAXTYPE))
507 code = fin->fin_icode;
509 /* See NetBSD ip_fil_netbsd.c r1.4: */
510 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
514 if (ipf_checkl4sum(fin) == -1)
517 MGETHDR(m, M_NOWAIT, MT_HEADER);
519 MGET(m, M_NOWAIT, MT_HEADER);
530 if (fin->fin_v == 4) {
531 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
532 switch (ntohs(fin->fin_data[0]) >> 8)
545 if (ipf_ifpaddr(&V_ipfmain, 4, FRI_NORMAL, ifp,
546 &dst6, NULL) == -1) {
552 dst4.s_addr = fin->fin_daddr;
555 ohlen = fin->fin_hlen;
556 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
557 if (fin->fin_hlen < fin->fin_plen)
558 xtra = MIN(fin->fin_dlen, 8);
564 else if (fin->fin_v == 6) {
565 hlen = sizeof(ip6_t);
566 ohlen = sizeof(ip6_t);
567 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
568 type = icmptoicmp6types[type];
569 if (type == ICMP6_DST_UNREACH)
570 code = icmptoicmp6unreach[code];
572 if (iclen + max_linkhdr + fin->fin_plen > avail) {
573 if (!(MCLGET(m, M_NOWAIT))) {
579 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
580 xtra = MIN(xtra, IPV6_MMTU - iclen);
582 if (ipf_ifpaddr(&V_ipfmain, 6, FRI_NORMAL, ifp,
583 &dst6, NULL) == -1) {
588 dst6 = fin->fin_dst6;
596 avail -= (max_linkhdr + iclen);
604 m->m_data += max_linkhdr;
605 m->m_pkthdr.rcvif = (struct ifnet *)0;
606 m->m_pkthdr.len = iclen;
608 ip = mtod(m, ip_t *);
609 icmp = (struct icmp *)((char *)ip + hlen);
610 ip2 = (ip_t *)&icmp->icmp_ip;
612 icmp->icmp_type = type;
613 icmp->icmp_code = fin->fin_icode;
614 icmp->icmp_cksum = 0;
616 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
617 if (fin->fin_mtu != 0) {
618 icmp->icmp_nextmtu = htons(fin->fin_mtu);
620 } else if (ifp != NULL) {
621 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
623 } else { /* make up a number... */
624 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
629 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
633 if (fin->fin_v == 6) {
634 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
635 ip6->ip6_plen = htons(iclen - hlen);
636 ip6->ip6_nxt = IPPROTO_ICMPV6;
638 ip6->ip6_src = dst6.in6;
639 ip6->ip6_dst = fin->fin_src6.in6;
641 bcopy((char *)fin->fin_ip + ohlen,
642 (char *)&icmp->icmp_ip + ohlen, xtra);
643 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
644 sizeof(*ip6), iclen - hlen);
648 ip->ip_p = IPPROTO_ICMP;
649 ip->ip_src.s_addr = dst4.s_addr;
650 ip->ip_dst.s_addr = fin->fin_saddr;
653 bcopy((char *)fin->fin_ip + ohlen,
654 (char *)&icmp->icmp_ip + ohlen, xtra);
655 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
657 ip->ip_len = htons(iclen);
658 ip->ip_p = IPPROTO_ICMP;
660 err = ipf_send_ip(fin, m);
668 * m0 - pointer to mbuf where the IP packet starts
669 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
672 ipf_fastroute(mb_t *m0, mb_t **mpp, fr_info_t *fin, frdest_t *fdp)
674 register struct ip *ip, *mhip;
675 register struct mbuf *m = *mpp;
676 int len, off, error = 0, hlen, code;
677 struct ifnet *ifp, *sifp;
679 struct sockaddr_in *dst;
680 const struct sockaddr *gw;
681 struct nhop_object *nh;
691 * If the mbuf we're about to send is not writable (because of
692 * a cluster reference, for example) we'll need to make a copy
693 * of it since this routine modifies the contents.
695 * If you have non-crappy network hardware that can transmit data
696 * from the mbuf, rather than making a copy, this is gonna be a
699 if (M_WRITABLE(m) == 0) {
700 m0 = m_dup(m, M_NOWAIT);
714 if (fin->fin_v == 6) {
716 * currently "to <if>" and "to <if>:ip#" are not supported
719 return (ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL));
723 hlen = fin->fin_hlen;
724 ip = mtod(m0, struct ip *);
730 bzero(&ro, sizeof (ro));
731 dst = (struct sockaddr_in *)&ro.ro_dst;
732 dst->sin_family = AF_INET;
733 dst->sin_addr = ip->ip_dst;
734 dst->sin_len = sizeof(dst);
735 gw = (const struct sockaddr *)dst;
738 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
739 (fdp->fd_type == FRD_DSTLIST)) {
740 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
749 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
754 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
755 dst->sin_addr = fdp->fd_ip;
757 fibnum = M_GETFIB(m0);
759 nh = fib4_lookup(fibnum, dst->sin_addr, 0, NHR_NONE, 0);
761 if (in_localaddr(ip->ip_dst))
762 error = EHOSTUNREACH;
770 if (nh->nh_flags & NHF_GATEWAY) {
772 ro.ro_flags |= RT_HAS_GW;
776 * For input packets which are being "fastrouted", they won't
777 * go back through output filtering and miss their chance to get
778 * NAT'd and counted. Duplicated packets aren't considered to be
779 * part of the normal packet stream, so do not NAT them or pass
780 * them through stateful checking, etc.
782 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
786 (void) ipf_acctpkt(fin, NULL);
788 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
791 (void) ipf_state_check(fin, &pass);
794 switch (ipf_nat_checkout(fin, NULL))
812 * If small enough for interface, can just send directly.
814 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
816 ip->ip_sum = in_cksum(m, hlen);
817 error = (*ifp->if_output)(ifp, m, gw, &ro);
821 * Too large for interface; fragment if possible.
822 * Must be able to put at least 8 bytes per fragment.
824 ip_off = ntohs(ip->ip_off);
825 if (ip_off & IP_DF) {
829 len = (ifp->if_mtu - hlen) &~ 7;
836 int mhlen, firstlen = len;
837 struct mbuf **mnext = &m->m_act;
840 * Loop through length of segment after first fragment,
841 * make new header and copy data of each part and link onto chain.
844 mhlen = sizeof (struct ip);
845 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
847 MGETHDR(m, M_NOWAIT, MT_HEADER);
849 MGET(m, M_NOWAIT, MT_HEADER);
856 m->m_data += max_linkhdr;
857 mhip = mtod(m, struct ip *);
858 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
859 if (hlen > sizeof (struct ip)) {
860 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
861 IP_HL_A(mhip, mhlen >> 2);
864 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
865 if (off + len >= ntohs(ip->ip_len))
866 len = ntohs(ip->ip_len) - off;
868 mhip->ip_off |= IP_MF;
869 mhip->ip_len = htons((u_short)(len + mhlen));
871 m->m_next = m_copym(m0, off, len, M_NOWAIT);
872 if (m->m_next == 0) {
873 error = ENOBUFS; /* ??? */
876 m->m_pkthdr.len = mhlen + len;
877 m->m_pkthdr.rcvif = NULL;
878 mhip->ip_off = htons((u_short)mhip->ip_off);
880 mhip->ip_sum = in_cksum(m, mhlen);
884 * Update first fragment by trimming what's been copied out
885 * and updating header, then send each fragment (in order).
887 m_adj(m0, hlen + firstlen - ip->ip_len);
888 ip->ip_len = htons((u_short)(hlen + firstlen));
889 ip->ip_off = htons((u_short)IP_MF);
891 ip->ip_sum = in_cksum(m0, hlen);
893 for (m = m0; m; m = m0) {
897 error = (*ifp->if_output)(ifp, m, gw, &ro);
904 V_ipfmain.ipf_frouteok[0]++;
906 V_ipfmain.ipf_frouteok[1]++;
910 if (error == EMSGSIZE) {
912 code = fin->fin_icode;
913 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
915 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
917 fin->fin_icode = code;
925 ipf_verifysrc(fr_info_t *fin)
927 struct nhop_object *nh;
930 nh = fib4_lookup(RT_DEFAULT_FIB, fin->fin_src, 0, NHR_NONE, 0);
933 return (fin->fin_ifp == nh->nh_ifp);
938 * return the first IP Address associated with an interface
941 ipf_ifpaddr(ipf_main_softc_t *softc, int v, int atype, void *ifptr,
942 i6addr_t *inp, i6addr_t *inpmask)
945 struct in6_addr *ia6 = NULL;
947 struct sockaddr *sock, *mask;
948 struct sockaddr_in *sin;
952 if ((ifptr == NULL) || (ifptr == (void *)-1))
962 bzero((char *)inp, sizeof(*inp));
964 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
966 sock = ifa->ifa_addr;
967 while (sock != NULL && ifa != NULL) {
968 sin = (struct sockaddr_in *)sock;
969 if ((v == 4) && (sin->sin_family == AF_INET))
972 if ((v == 6) && (sin->sin_family == AF_INET6)) {
973 ia6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
974 if (!IN6_IS_ADDR_LINKLOCAL(ia6) &&
975 !IN6_IS_ADDR_LOOPBACK(ia6))
979 ifa = CK_STAILQ_NEXT(ifa, ifa_link);
981 sock = ifa->ifa_addr;
984 if (ifa == NULL || sin == NULL)
987 mask = ifa->ifa_netmask;
988 if (atype == FRI_BROADCAST)
989 sock = ifa->ifa_broadaddr;
990 else if (atype == FRI_PEERADDR)
991 sock = ifa->ifa_dstaddr;
998 return (ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
999 (struct sockaddr_in6 *)mask,
1003 return (ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1004 (struct sockaddr_in *)mask,
1005 &inp->in4, &inpmask->in4));
1010 ipf_newisn(fr_info_t *fin)
1013 newiss = arc4random();
1019 ipf_checkv4sum(fr_info_t *fin)
1021 #ifdef CSUM_DATA_VALID
1027 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1030 if ((fin->fin_flx & FI_SHORT) != 0)
1033 if (fin->fin_cksum != FI_CK_NEEDED)
1034 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1043 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1045 fin->fin_cksum = FI_CK_BAD;
1046 fin->fin_flx |= FI_BAD;
1047 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));
1050 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1051 /* Depending on the driver, UDP may have zero checksum */
1052 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1053 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1054 udphdr_t *udp = fin->fin_dp;
1055 if (udp->uh_sum == 0) {
1057 * we're good no matter what the hardware
1058 * checksum flags and csum_data say (handling
1059 * of csum_data for zero UDP checksum is not
1060 * consistent across all drivers)
1067 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1068 sum = m->m_pkthdr.csum_data;
1070 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1071 htonl(m->m_pkthdr.csum_data +
1072 fin->fin_dlen + fin->fin_p));
1075 fin->fin_cksum = FI_CK_BAD;
1076 fin->fin_flx |= FI_BAD;
1077 DT2(ipf_fi_bad_checkv4sum_sum, fr_info_t *, fin, u_int, sum);
1079 fin->fin_cksum = FI_CK_SUMOK;
1083 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1084 fin->fin_cksum = FI_CK_L4FULL;
1086 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1087 m->m_pkthdr.csum_flags == CSUM_UDP ||
1088 m->m_pkthdr.csum_flags == CSUM_IP) {
1089 fin->fin_cksum = FI_CK_L4PART;
1097 if (ipf_checkl4sum(fin) == -1) {
1098 fin->fin_flx |= FI_BAD;
1099 DT2(ipf_fi_bad_checkv4sum_manual, fr_info_t *, fin, u_int, manual);
1104 if (ipf_checkl4sum(fin) == -1) {
1105 fin->fin_flx |= FI_BAD;
1106 DT2(ipf_fi_bad_checkv4sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1116 ipf_checkv6sum(fr_info_t *fin)
1118 if ((fin->fin_flx & FI_NOCKSUM) != 0) {
1119 DT(ipf_checkv6sum_fi_nocksum);
1123 if ((fin->fin_flx & FI_SHORT) != 0) {
1124 DT(ipf_checkv6sum_fi_short);
1128 if (fin->fin_cksum != FI_CK_NEEDED) {
1129 DT(ipf_checkv6sum_fi_ck_needed);
1130 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1133 if (ipf_checkl4sum(fin) == -1) {
1134 fin->fin_flx |= FI_BAD;
1135 DT2(ipf_fi_bad_checkv6sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1140 #endif /* USE_INET6 */
1144 mbufchainlen(struct mbuf *m0)
1148 if ((m0->m_flags & M_PKTHDR) != 0) {
1149 len = m0->m_pkthdr.len;
1153 for (m = m0, len = 0; m != NULL; m = m->m_next)
1160 /* ------------------------------------------------------------------------ */
1161 /* Function: ipf_pullup */
1162 /* Returns: NULL == pullup failed, else pointer to protocol header */
1163 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1164 /* fin(I) - pointer to packet information */
1165 /* len(I) - number of bytes to pullup */
1167 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1168 /* single buffer for ease of access. Operating system native functions are */
1169 /* used to manage buffers - if necessary. If the entire packet ends up in */
1170 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1171 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1172 /* and ONLY if the pullup succeeds. */
1174 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1175 /* of buffers that starts at *fin->fin_mp. */
1176 /* ------------------------------------------------------------------------ */
1178 ipf_pullup(mb_t *xmin, fr_info_t *fin, int len)
1188 if ((fin->fin_flx & FI_COALESCE) != 0)
1191 ipoff = fin->fin_ipoff;
1192 if (fin->fin_dp != NULL)
1193 dpoff = (char *)fin->fin_dp - (char *)ip;
1197 if (M_LEN(m) < len) {
1198 mb_t *n = *fin->fin_mp;
1200 * Assume that M_PKTHDR is set and just work with what is left
1201 * rather than check..
1202 * Should not make any real difference, anyway.
1206 * Record the mbuf that points to the mbuf that we're
1207 * about to go to work on so that we can update the
1208 * m_next appropriately later.
1210 for (; n->m_next != m; n = n->m_next)
1222 #ifdef HAVE_M_PULLDOWN
1223 if (m_pulldown(m, 0, len, NULL) == NULL)
1226 FREE_MB_T(*fin->fin_mp);
1233 m = m_pullup(m, len);
1239 * When n is non-NULL, it indicates that m pointed to
1240 * a sub-chain (tail) of the mbuf and that the head
1241 * of this chain has not yet been free'd.
1244 FREE_MB_T(*fin->fin_mp);
1247 *fin->fin_mp = NULL;
1255 while (M_LEN(m) == 0) {
1259 ip = MTOD(m, ip_t *) + ipoff;
1262 if (fin->fin_dp != NULL)
1263 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1264 if (fin->fin_fraghdr != NULL)
1265 fin->fin_fraghdr = (char *)ip +
1266 ((char *)fin->fin_fraghdr -
1267 (char *)fin->fin_ip);
1270 if (len == fin->fin_plen)
1271 fin->fin_flx |= FI_COALESCE;
1277 ipf_inject(fr_info_t *fin, mb_t *m)
1279 struct epoch_tracker et;
1282 NET_EPOCH_ENTER(et);
1283 if (fin->fin_out == 0) {
1284 netisr_dispatch(NETISR_IP, m);
1286 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1287 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1288 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1295 VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet_hook);
1296 VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet6_hook);
1297 #define V_ipf_inet_hook VNET(ipf_inet_hook)
1298 #define V_ipf_inet6_hook VNET(ipf_inet6_hook)
1300 int ipf_pfil_unhook(void) {
1302 pfil_remove_hook(V_ipf_inet_hook);
1305 pfil_remove_hook(V_ipf_inet6_hook);
1311 int ipf_pfil_hook(void) {
1314 struct pfil_hook_args pha = {
1315 .pa_version = PFIL_VERSION,
1316 .pa_flags = PFIL_IN | PFIL_OUT,
1317 .pa_modname = "ipfilter",
1318 .pa_rulname = "default-ip4",
1319 .pa_mbuf_chk = ipf_check_wrapper,
1320 .pa_type = PFIL_TYPE_IP4,
1322 V_ipf_inet_hook = pfil_add_hook(&pha);
1325 pha.pa_rulname = "default-ip6";
1326 pha.pa_mbuf_chk = ipf_check_wrapper6;
1327 pha.pa_type = PFIL_TYPE_IP6;
1328 V_ipf_inet6_hook = pfil_add_hook(&pha);
1331 struct pfil_link_args pla = {
1332 .pa_version = PFIL_VERSION,
1333 .pa_flags = PFIL_IN | PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR,
1334 .pa_head = V_inet_pfil_head,
1335 .pa_hook = V_ipf_inet_hook,
1337 error = pfil_link(&pla);
1341 pla.pa_head = V_inet6_pfil_head;
1342 pla.pa_hook = V_ipf_inet6_hook;
1343 error6 = pfil_link(&pla);
1346 if (error || error6)
1357 V_ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1358 ipf_ifevent, NULL, \
1359 EVENTHANDLER_PRI_ANY);
1360 V_ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1361 ipf_ifevent, NULL, \
1362 EVENTHANDLER_PRI_ANY);
1364 V_ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1365 NULL, EVENTHANDLER_PRI_ANY);
1370 ipf_event_dereg(void)
1372 if (V_ipf_arrivetag != NULL) {
1373 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, V_ipf_arrivetag);
1375 if (V_ipf_departtag != NULL) {
1376 EVENTHANDLER_DEREGISTER(ifnet_departure_event, V_ipf_departtag);
1379 if (V_ipf_clonetag != NULL) {
1380 EVENTHANDLER_DEREGISTER(if_clone_event, V_ipf_clonetag);
1389 return (arc4random());
1394 ipf_pcksum(fr_info_t *fin, int hlen, u_int sum)
1401 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1404 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1409 * Both sum and sum2 are partial sums, so combine them together.
1411 sum += ~sum2 & 0xffff;
1412 while (sum > 0xffff)
1413 sum = (sum & 0xffff) + (sum >> 16);
1414 sum2 = ~sum & 0xffff;
1420 ipf_pcksum6(struct mbuf *m, ip6_t *ip6, u_int32_t off, u_int32_t len)
1425 if (m->m_len < sizeof(struct ip6_hdr)) {
1429 sum = in6_cksum(m, ip6->ip6_nxt, off, len);
1435 sp = (u_short *)&ip6->ip6_src;
1436 sum = *sp++; /* ip6_src */
1444 sum += *sp++; /* ip6_dst */
1452 return (ipf_pcksum(fin, off, sum));
1458 ipf_fbsd_kenv_get(ipf_main_softc_t *softc)
1460 TUNABLE_INT_FETCH("net.inet.ipf.large_nat",
1461 &softc->ipf_large_nat);