3 * Copyright (C) 2012 by Darren Reed.
5 * See the IPFILTER.LICENCE file for details on licencing.
8 static const char sccsid[] = "@(#)ip_fil.c 2.41 6/5/96 (C) 1993-2000 Darren Reed";
9 static const char rcsid[] = "@(#)$Id$";
12 #if defined(KERNEL) || defined(_KERNEL)
18 #if defined(__FreeBSD__) && \
19 !defined(KLD_MODULE) && !defined(IPFILTER_LKM)
20 # include "opt_inet6.h"
22 #include <sys/param.h>
23 #include <sys/eventhandler.h>
25 #include <sys/errno.h>
26 #include <sys/types.h>
28 #include <sys/fcntl.h>
29 #include <sys/filio.h>
31 #include <sys/systm.h>
32 #include <sys/dirent.h>
33 #if defined(__FreeBSD__)
34 # include <sys/jail.h>
36 #include <sys/malloc.h>
38 #include <sys/sockopt.h>
39 #include <sys/socket.h>
40 #include <sys/selinfo.h>
42 #include <net/if_var.h>
43 #include <net/netisr.h>
44 #include <net/route.h>
45 #include <net/route/nhop.h>
46 #include <netinet/in.h>
47 #include <netinet/in_fib.h>
48 #include <netinet/in_pcb.h>
49 #include <netinet/in_var.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_var.h>
53 #include <netinet/tcp.h>
54 #include <netinet/tcp_var.h>
56 #include <netinet/udp.h>
57 #include <netinet/tcpip.h>
58 #include <netinet/ip_icmp.h>
59 #include "netinet/ip_compat.h"
61 # include <netinet/icmp6.h>
63 #include "netinet/ip_fil.h"
64 #include "netinet/ip_nat.h"
65 #include "netinet/ip_frag.h"
66 #include "netinet/ip_state.h"
67 #include "netinet/ip_proxy.h"
68 #include "netinet/ip_auth.h"
69 #include "netinet/ip_sync.h"
70 #include "netinet/ip_lookup.h"
71 #include "netinet/ip_dstlist.h"
73 # include "netinet/ip_scan.h"
75 #include "netinet/ip_pool.h"
76 #include <sys/malloc.h>
77 #include <sys/kernel.h>
78 #ifdef CSUM_DATA_VALID
79 # include <machine/in_cksum.h>
81 extern int ip_optcopy(struct ip *, struct ip *);
83 #ifdef IPFILTER_M_IPFILTER
84 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
88 static int ipf_send_ip(fr_info_t *, mb_t *);
89 static void ipf_timer_func(void *arg);
91 VNET_DEFINE(ipf_main_softc_t, ipfmain) = {
94 #define V_ipfmain VNET(ipfmain)
99 VNET_DEFINE_STATIC(eventhandler_tag, ipf_arrivetag);
100 VNET_DEFINE_STATIC(eventhandler_tag, ipf_departtag);
101 #define V_ipf_arrivetag VNET(ipf_arrivetag)
102 #define V_ipf_departtag VNET(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 VNET_DEFINE_STATIC(eventhandler_tag, ipf_clonetag);
113 #define V_ipf_clonetag VNET(ipf_clonetag)
116 static void ipf_ifevent(void *arg, struct ifnet *ifp);
118 static void ipf_ifevent(void *arg, struct ifnet *ifp)
121 CURVNET_SET(ifp->if_vnet);
122 if (V_ipfmain.ipf_running > 0)
123 ipf_sync(&V_ipfmain, NULL);
130 ipf_check_wrapper(struct mbuf **mp, struct ifnet *ifp, int flags,
131 void *ruleset __unused, struct inpcb *inp)
133 struct ip *ip = mtod(*mp, struct ip *);
136 CURVNET_SET(ifp->if_vnet);
137 rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp,
138 !!(flags & PFIL_OUT), mp);
140 return (rv == 0 ? PFIL_PASS : PFIL_DROPPED);
145 ipf_check_wrapper6(struct mbuf **mp, struct ifnet *ifp, int flags,
146 void *ruleset __unused, struct inpcb *inp)
150 CURVNET_SET(ifp->if_vnet);
151 rv = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
152 sizeof(struct ip6_hdr), ifp, !!(flags & PFIL_OUT), mp);
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->th_flags & TH_ACK) {
387 tcp2->th_seq = tcp->th_ack;
388 tcp2->th_flags = TH_RST;
392 tcp2->th_ack = ntohl(tcp->th_seq);
393 tcp2->th_ack += tlen;
394 tcp2->th_ack = htonl(tcp2->th_ack);
395 tcp2->th_flags = TH_RST|TH_ACK;
398 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
399 tcp2->th_win = tcp->th_win;
404 if (fin->fin_v == 6) {
405 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
406 ip6->ip6_plen = htons(sizeof(struct tcphdr));
407 ip6->ip6_nxt = IPPROTO_TCP;
409 ip6->ip6_src = fin->fin_dst6.in6;
410 ip6->ip6_dst = fin->fin_src6.in6;
411 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
412 sizeof(*ip6), sizeof(*tcp2));
413 return (ipf_send_ip(fin, m));
416 ip->ip_p = IPPROTO_TCP;
417 ip->ip_len = htons(sizeof(struct tcphdr));
418 ip->ip_src.s_addr = fin->fin_daddr;
419 ip->ip_dst.s_addr = fin->fin_saddr;
420 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
421 ip->ip_len = htons(hlen + sizeof(*tcp2));
422 return (ipf_send_ip(fin, m));
427 * ip_len must be in network byte order when called.
430 ipf_send_ip(fr_info_t *fin, mb_t *m)
436 ip = mtod(m, ip_t *);
437 bzero((char *)&fnew, sizeof(fnew));
438 fnew.fin_main_soft = fin->fin_main_soft;
440 IP_V_A(ip, fin->fin_v);
447 fnew.fin_p = ip->ip_p;
448 fnew.fin_plen = ntohs(ip->ip_len);
449 IP_HL_A(ip, sizeof(*oip) >> 2);
450 ip->ip_tos = oip->ip_tos;
451 ip->ip_id = fin->fin_ip->ip_id;
452 ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
453 ip->ip_ttl = V_ip_defttl;
459 ip6_t *ip6 = (ip6_t *)ip;
462 ip6->ip6_hlim = IPDEFTTL;
465 fnew.fin_p = ip6->ip6_nxt;
467 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
475 m->m_pkthdr.rcvif = NULL;
478 fnew.fin_ifp = fin->fin_ifp;
479 fnew.fin_flx = FI_NOCKSUM;
483 fnew.fin_hlen = hlen;
484 fnew.fin_dp = (char *)ip + hlen;
485 (void) ipf_makefrip(hlen, ip, &fnew);
487 return (ipf_fastroute(m, &m, &fnew, NULL));
492 ipf_send_icmp_err(int type, fr_info_t *fin, int dst)
494 int err, hlen, xtra, iclen, ohlen, avail, code;
505 if ((type < 0) || (type >= ICMP_MAXTYPE))
508 code = fin->fin_icode;
510 /* See NetBSD ip_fil_netbsd.c r1.4: */
511 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
515 if (ipf_checkl4sum(fin) == -1)
518 MGETHDR(m, M_NOWAIT, MT_HEADER);
520 MGET(m, M_NOWAIT, MT_HEADER);
531 if (fin->fin_v == 4) {
532 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
533 switch (ntohs(fin->fin_data[0]) >> 8)
546 if (ipf_ifpaddr(&V_ipfmain, 4, FRI_NORMAL, ifp,
547 &dst6, NULL) == -1) {
553 dst4.s_addr = fin->fin_daddr;
556 ohlen = fin->fin_hlen;
557 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
558 if (fin->fin_hlen < fin->fin_plen)
559 xtra = MIN(fin->fin_dlen, 8);
565 else if (fin->fin_v == 6) {
566 hlen = sizeof(ip6_t);
567 ohlen = sizeof(ip6_t);
568 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
569 type = icmptoicmp6types[type];
570 if (type == ICMP6_DST_UNREACH)
571 code = icmptoicmp6unreach[code];
573 if (iclen + max_linkhdr + fin->fin_plen > avail) {
574 if (!(MCLGET(m, M_NOWAIT))) {
580 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
581 xtra = MIN(xtra, IPV6_MMTU - iclen);
583 if (ipf_ifpaddr(&V_ipfmain, 6, FRI_NORMAL, ifp,
584 &dst6, NULL) == -1) {
589 dst6 = fin->fin_dst6;
597 avail -= (max_linkhdr + iclen);
605 m->m_data += max_linkhdr;
606 m->m_pkthdr.rcvif = (struct ifnet *)0;
607 m->m_pkthdr.len = iclen;
609 ip = mtod(m, ip_t *);
610 icmp = (struct icmp *)((char *)ip + hlen);
611 ip2 = (ip_t *)&icmp->icmp_ip;
613 icmp->icmp_type = type;
614 icmp->icmp_code = fin->fin_icode;
615 icmp->icmp_cksum = 0;
617 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
618 if (fin->fin_mtu != 0) {
619 icmp->icmp_nextmtu = htons(fin->fin_mtu);
621 } else if (ifp != NULL) {
622 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
624 } else { /* make up a number... */
625 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
630 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
634 if (fin->fin_v == 6) {
635 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
636 ip6->ip6_plen = htons(iclen - hlen);
637 ip6->ip6_nxt = IPPROTO_ICMPV6;
639 ip6->ip6_src = dst6.in6;
640 ip6->ip6_dst = fin->fin_src6.in6;
642 bcopy((char *)fin->fin_ip + ohlen,
643 (char *)&icmp->icmp_ip + ohlen, xtra);
644 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
645 sizeof(*ip6), iclen - hlen);
649 ip->ip_p = IPPROTO_ICMP;
650 ip->ip_src.s_addr = dst4.s_addr;
651 ip->ip_dst.s_addr = fin->fin_saddr;
654 bcopy((char *)fin->fin_ip + ohlen,
655 (char *)&icmp->icmp_ip + ohlen, xtra);
656 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
658 ip->ip_len = htons(iclen);
659 ip->ip_p = IPPROTO_ICMP;
661 err = ipf_send_ip(fin, m);
669 * m0 - pointer to mbuf where the IP packet starts
670 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
673 ipf_fastroute(mb_t *m0, mb_t **mpp, fr_info_t *fin, frdest_t *fdp)
675 register struct ip *ip, *mhip;
676 register struct mbuf *m = *mpp;
677 int len, off, error = 0, hlen, code;
678 struct ifnet *ifp, *sifp;
680 struct sockaddr_in *dst;
681 const struct sockaddr *gw;
682 struct nhop_object *nh;
692 * If the mbuf we're about to send is not writable (because of
693 * a cluster reference, for example) we'll need to make a copy
694 * of it since this routine modifies the contents.
696 * If you have non-crappy network hardware that can transmit data
697 * from the mbuf, rather than making a copy, this is gonna be a
700 if (M_WRITABLE(m) == 0) {
701 m0 = m_dup(m, M_NOWAIT);
715 if (fin->fin_v == 6) {
717 * currently "to <if>" and "to <if>:ip#" are not supported
720 return (ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL));
724 hlen = fin->fin_hlen;
725 ip = mtod(m0, struct ip *);
731 bzero(&ro, sizeof (ro));
732 dst = (struct sockaddr_in *)&ro.ro_dst;
733 dst->sin_family = AF_INET;
734 dst->sin_addr = ip->ip_dst;
735 dst->sin_len = sizeof(dst);
736 gw = (const struct sockaddr *)dst;
739 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
740 (fdp->fd_type == FRD_DSTLIST)) {
741 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
750 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
755 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
756 dst->sin_addr = fdp->fd_ip;
758 fibnum = M_GETFIB(m0);
760 nh = fib4_lookup(fibnum, dst->sin_addr, 0, NHR_NONE, 0);
762 if (in_localaddr(ip->ip_dst))
763 error = EHOSTUNREACH;
771 if (nh->nh_flags & NHF_GATEWAY) {
773 ro.ro_flags |= RT_HAS_GW;
777 * For input packets which are being "fastrouted", they won't
778 * go back through output filtering and miss their chance to get
779 * NAT'd and counted. Duplicated packets aren't considered to be
780 * part of the normal packet stream, so do not NAT them or pass
781 * them through stateful checking, etc.
783 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
787 (void) ipf_acctpkt(fin, NULL);
789 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
792 (void) ipf_state_check(fin, &pass);
795 switch (ipf_nat_checkout(fin, NULL))
813 * If small enough for interface, can just send directly.
815 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
817 ip->ip_sum = in_cksum(m, hlen);
818 error = (*ifp->if_output)(ifp, m, gw, &ro);
822 * Too large for interface; fragment if possible.
823 * Must be able to put at least 8 bytes per fragment.
825 ip_off = ntohs(ip->ip_off);
826 if (ip_off & IP_DF) {
830 len = (ifp->if_mtu - hlen) &~ 7;
837 int mhlen, firstlen = len;
838 struct mbuf **mnext = &m->m_act;
841 * Loop through length of segment after first fragment,
842 * make new header and copy data of each part and link onto chain.
845 mhlen = sizeof (struct ip);
846 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
848 MGETHDR(m, M_NOWAIT, MT_HEADER);
850 MGET(m, M_NOWAIT, MT_HEADER);
857 m->m_data += max_linkhdr;
858 mhip = mtod(m, struct ip *);
859 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
860 if (hlen > sizeof (struct ip)) {
861 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
862 IP_HL_A(mhip, mhlen >> 2);
865 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
866 if (off + len >= ntohs(ip->ip_len))
867 len = ntohs(ip->ip_len) - off;
869 mhip->ip_off |= IP_MF;
870 mhip->ip_len = htons((u_short)(len + mhlen));
872 m->m_next = m_copym(m0, off, len, M_NOWAIT);
873 if (m->m_next == 0) {
874 error = ENOBUFS; /* ??? */
877 m->m_pkthdr.len = mhlen + len;
878 m->m_pkthdr.rcvif = NULL;
879 mhip->ip_off = htons((u_short)mhip->ip_off);
881 mhip->ip_sum = in_cksum(m, mhlen);
885 * Update first fragment by trimming what's been copied out
886 * and updating header, then send each fragment (in order).
888 m_adj(m0, hlen + firstlen - ip->ip_len);
889 ip->ip_len = htons((u_short)(hlen + firstlen));
890 ip->ip_off = htons((u_short)IP_MF);
892 ip->ip_sum = in_cksum(m0, hlen);
894 for (m = m0; m; m = m0) {
898 error = (*ifp->if_output)(ifp, m, gw, &ro);
905 V_ipfmain.ipf_frouteok[0]++;
907 V_ipfmain.ipf_frouteok[1]++;
911 if (error == EMSGSIZE) {
913 code = fin->fin_icode;
914 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
916 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
918 fin->fin_icode = code;
926 ipf_verifysrc(fr_info_t *fin)
928 struct nhop_object *nh;
931 nh = fib4_lookup(RT_DEFAULT_FIB, fin->fin_src, 0, NHR_NONE, 0);
934 return (fin->fin_ifp == nh->nh_ifp);
939 * return the first IP Address associated with an interface
942 ipf_ifpaddr(ipf_main_softc_t *softc, int v, int atype, void *ifptr,
943 i6addr_t *inp, i6addr_t *inpmask)
946 struct in6_addr *ia6 = NULL;
948 struct sockaddr *sock, *mask;
949 struct sockaddr_in *sin;
953 if ((ifptr == NULL) || (ifptr == (void *)-1))
963 bzero((char *)inp, sizeof(*inp));
965 ifa = CK_STAILQ_FIRST(&ifp->if_addrhead);
967 sock = ifa->ifa_addr;
968 while (sock != NULL && ifa != NULL) {
969 sin = (struct sockaddr_in *)sock;
970 if ((v == 4) && (sin->sin_family == AF_INET))
973 if ((v == 6) && (sin->sin_family == AF_INET6)) {
974 ia6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
975 if (!IN6_IS_ADDR_LINKLOCAL(ia6) &&
976 !IN6_IS_ADDR_LOOPBACK(ia6))
980 ifa = CK_STAILQ_NEXT(ifa, ifa_link);
982 sock = ifa->ifa_addr;
985 if (ifa == NULL || sin == NULL)
988 mask = ifa->ifa_netmask;
989 if (atype == FRI_BROADCAST)
990 sock = ifa->ifa_broadaddr;
991 else if (atype == FRI_PEERADDR)
992 sock = ifa->ifa_dstaddr;
999 return (ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1000 (struct sockaddr_in6 *)mask,
1004 return (ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1005 (struct sockaddr_in *)mask,
1006 &inp->in4, &inpmask->in4));
1011 ipf_newisn(fr_info_t *fin)
1014 newiss = arc4random();
1020 ipf_checkv4sum(fr_info_t *fin)
1022 #ifdef CSUM_DATA_VALID
1028 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1031 if ((fin->fin_flx & FI_SHORT) != 0)
1034 if (fin->fin_cksum != FI_CK_NEEDED)
1035 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1044 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1046 fin->fin_cksum = FI_CK_BAD;
1047 fin->fin_flx |= FI_BAD;
1048 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));
1051 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1052 /* Depending on the driver, UDP may have zero checksum */
1053 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1054 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1055 udphdr_t *udp = fin->fin_dp;
1056 if (udp->uh_sum == 0) {
1058 * we're good no matter what the hardware
1059 * checksum flags and csum_data say (handling
1060 * of csum_data for zero UDP checksum is not
1061 * consistent across all drivers)
1068 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1069 sum = m->m_pkthdr.csum_data;
1071 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1072 htonl(m->m_pkthdr.csum_data +
1073 fin->fin_dlen + fin->fin_p));
1076 fin->fin_cksum = FI_CK_BAD;
1077 fin->fin_flx |= FI_BAD;
1078 DT2(ipf_fi_bad_checkv4sum_sum, fr_info_t *, fin, u_int, sum);
1080 fin->fin_cksum = FI_CK_SUMOK;
1084 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1085 fin->fin_cksum = FI_CK_L4FULL;
1087 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1088 m->m_pkthdr.csum_flags == CSUM_UDP ||
1089 m->m_pkthdr.csum_flags == CSUM_IP) {
1090 fin->fin_cksum = FI_CK_L4PART;
1098 if (ipf_checkl4sum(fin) == -1) {
1099 fin->fin_flx |= FI_BAD;
1100 DT2(ipf_fi_bad_checkv4sum_manual, fr_info_t *, fin, u_int, manual);
1105 if (ipf_checkl4sum(fin) == -1) {
1106 fin->fin_flx |= FI_BAD;
1107 DT2(ipf_fi_bad_checkv4sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1117 ipf_checkv6sum(fr_info_t *fin)
1119 if ((fin->fin_flx & FI_NOCKSUM) != 0) {
1120 DT(ipf_checkv6sum_fi_nocksum);
1124 if ((fin->fin_flx & FI_SHORT) != 0) {
1125 DT(ipf_checkv6sum_fi_short);
1129 if (fin->fin_cksum != FI_CK_NEEDED) {
1130 DT(ipf_checkv6sum_fi_ck_needed);
1131 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1134 if (ipf_checkl4sum(fin) == -1) {
1135 fin->fin_flx |= FI_BAD;
1136 DT2(ipf_fi_bad_checkv6sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1141 #endif /* USE_INET6 */
1145 mbufchainlen(struct mbuf *m0)
1149 if ((m0->m_flags & M_PKTHDR) != 0) {
1150 len = m0->m_pkthdr.len;
1154 for (m = m0, len = 0; m != NULL; m = m->m_next)
1161 /* ------------------------------------------------------------------------ */
1162 /* Function: ipf_pullup */
1163 /* Returns: NULL == pullup failed, else pointer to protocol header */
1164 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1165 /* fin(I) - pointer to packet information */
1166 /* len(I) - number of bytes to pullup */
1168 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1169 /* single buffer for ease of access. Operating system native functions are */
1170 /* used to manage buffers - if necessary. If the entire packet ends up in */
1171 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1172 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1173 /* and ONLY if the pullup succeeds. */
1175 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1176 /* of buffers that starts at *fin->fin_mp. */
1177 /* ------------------------------------------------------------------------ */
1179 ipf_pullup(mb_t *xmin, fr_info_t *fin, int len)
1189 if ((fin->fin_flx & FI_COALESCE) != 0)
1192 ipoff = fin->fin_ipoff;
1193 if (fin->fin_dp != NULL)
1194 dpoff = (char *)fin->fin_dp - (char *)ip;
1198 if (M_LEN(m) < len) {
1199 mb_t *n = *fin->fin_mp;
1201 * Assume that M_PKTHDR is set and just work with what is left
1202 * rather than check..
1203 * Should not make any real difference, anyway.
1207 * Record the mbuf that points to the mbuf that we're
1208 * about to go to work on so that we can update the
1209 * m_next appropriately later.
1211 for (; n->m_next != m; n = n->m_next)
1223 #ifdef HAVE_M_PULLDOWN
1224 if (m_pulldown(m, 0, len, NULL) == NULL)
1227 FREE_MB_T(*fin->fin_mp);
1234 m = m_pullup(m, len);
1240 * When n is non-NULL, it indicates that m pointed to
1241 * a sub-chain (tail) of the mbuf and that the head
1242 * of this chain has not yet been free'd.
1245 FREE_MB_T(*fin->fin_mp);
1248 *fin->fin_mp = NULL;
1256 while (M_LEN(m) == 0) {
1260 ip = MTOD(m, ip_t *) + ipoff;
1263 if (fin->fin_dp != NULL)
1264 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1265 if (fin->fin_fraghdr != NULL)
1266 fin->fin_fraghdr = (char *)ip +
1267 ((char *)fin->fin_fraghdr -
1268 (char *)fin->fin_ip);
1271 if (len == fin->fin_plen)
1272 fin->fin_flx |= FI_COALESCE;
1278 ipf_inject(fr_info_t *fin, mb_t *m)
1280 struct epoch_tracker et;
1283 NET_EPOCH_ENTER(et);
1284 if (fin->fin_out == 0) {
1285 netisr_dispatch(NETISR_IP, m);
1287 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1288 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1289 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1296 VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet_hook);
1297 VNET_DEFINE_STATIC(pfil_hook_t, ipf_inet6_hook);
1298 #define V_ipf_inet_hook VNET(ipf_inet_hook)
1299 #define V_ipf_inet6_hook VNET(ipf_inet6_hook)
1301 int ipf_pfil_unhook(void) {
1303 pfil_remove_hook(V_ipf_inet_hook);
1306 pfil_remove_hook(V_ipf_inet6_hook);
1312 int ipf_pfil_hook(void) {
1315 struct pfil_hook_args pha = {
1316 .pa_version = PFIL_VERSION,
1317 .pa_flags = PFIL_IN | PFIL_OUT,
1318 .pa_modname = "ipfilter",
1319 .pa_rulname = "default-ip4",
1320 .pa_mbuf_chk = ipf_check_wrapper,
1321 .pa_type = PFIL_TYPE_IP4,
1323 V_ipf_inet_hook = pfil_add_hook(&pha);
1326 pha.pa_rulname = "default-ip6";
1327 pha.pa_mbuf_chk = ipf_check_wrapper6;
1328 pha.pa_type = PFIL_TYPE_IP6;
1329 V_ipf_inet6_hook = pfil_add_hook(&pha);
1332 struct pfil_link_args pla = {
1333 .pa_version = PFIL_VERSION,
1334 .pa_flags = PFIL_IN | PFIL_OUT | PFIL_HEADPTR | PFIL_HOOKPTR,
1335 .pa_head = V_inet_pfil_head,
1336 .pa_hook = V_ipf_inet_hook,
1338 error = pfil_link(&pla);
1342 pla.pa_head = V_inet6_pfil_head;
1343 pla.pa_hook = V_ipf_inet6_hook;
1344 error6 = pfil_link(&pla);
1347 if (error || error6)
1358 V_ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1359 ipf_ifevent, NULL, \
1360 EVENTHANDLER_PRI_ANY);
1361 V_ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1362 ipf_ifevent, NULL, \
1363 EVENTHANDLER_PRI_ANY);
1365 V_ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1366 NULL, EVENTHANDLER_PRI_ANY);
1371 ipf_event_dereg(void)
1373 if (V_ipf_arrivetag != NULL) {
1374 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, V_ipf_arrivetag);
1376 if (V_ipf_departtag != NULL) {
1377 EVENTHANDLER_DEREGISTER(ifnet_departure_event, V_ipf_departtag);
1380 if (V_ipf_clonetag != NULL) {
1381 EVENTHANDLER_DEREGISTER(if_clone_event, V_ipf_clonetag);
1390 return (arc4random());
1395 ipf_pcksum(fr_info_t *fin, int hlen, u_int sum)
1402 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1405 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1410 * Both sum and sum2 are partial sums, so combine them together.
1412 sum += ~sum2 & 0xffff;
1413 while (sum > 0xffff)
1414 sum = (sum & 0xffff) + (sum >> 16);
1415 sum2 = ~sum & 0xffff;
1421 ipf_pcksum6(struct mbuf *m, ip6_t *ip6, u_int32_t off, u_int32_t len)
1426 if (m->m_len < sizeof(struct ip6_hdr)) {
1430 sum = in6_cksum(m, ip6->ip6_nxt, off, len);
1436 sp = (u_short *)&ip6->ip6_src;
1437 sum = *sp++; /* ip6_src */
1445 sum += *sp++; /* ip6_dst */
1453 return (ipf_pcksum(fin, off, sum));
1459 ipf_fbsd_kenv_get(ipf_main_softc_t *softc)
1461 TUNABLE_INT_FETCH("net.inet.ipf.large_nat",
1462 &softc->ipf_large_nat);