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>
28 #include <sys/errno.h>
29 #include <sys/types.h>
31 # include <sys/fcntl.h>
32 # include <sys/filio.h>
34 #include <sys/systm.h>
35 # include <sys/dirent.h>
36 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000)
39 # include <sys/malloc.h>
40 # include <sys/mbuf.h>
41 # include <sys/sockopt.h>
43 # include <sys/mbuf.h>
45 #include <sys/socket.h>
46 # include <sys/selinfo.h>
47 # include <netinet/tcp_var.h>
50 # include <net/if_var.h>
51 # include <net/netisr.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_fib.h>
55 #include <netinet/in_var.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/tcp.h>
60 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000)
63 #define CURVNET_SET(arg)
64 #define CURVNET_RESTORE()
65 #define VNET_DEFINE(_t, _v) _t _v
66 #define VNET_DECLARE(_t, _v) extern _t _v
70 # include <netinet/tcp_timer.h>
72 #include <netinet/udp.h>
73 #include <netinet/tcpip.h>
74 #include <netinet/ip_icmp.h>
75 #include "netinet/ip_compat.h"
77 # include <netinet/icmp6.h>
79 #include "netinet/ip_fil.h"
80 #include "netinet/ip_nat.h"
81 #include "netinet/ip_frag.h"
82 #include "netinet/ip_state.h"
83 #include "netinet/ip_proxy.h"
84 #include "netinet/ip_auth.h"
85 #include "netinet/ip_sync.h"
86 #include "netinet/ip_lookup.h"
87 #include "netinet/ip_dstlist.h"
89 #include "netinet/ip_scan.h"
91 #include "netinet/ip_pool.h"
92 # include <sys/malloc.h>
93 #include <sys/kernel.h>
94 #ifdef CSUM_DATA_VALID
95 #include <machine/in_cksum.h>
97 extern int ip_optcopy __P((struct ip *, struct ip *));
100 # ifdef IPFILTER_M_IPFILTER
101 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
105 static int ipf_send_ip __P((fr_info_t *, mb_t *));
106 static void ipf_timer_func __P((void *arg));
108 VNET_DEFINE(ipf_main_softc_t, ipfmain) = {
111 #define V_ipfmain VNET(ipfmain)
113 # include <sys/conf.h>
114 # if defined(NETBSD_PF)
115 # include <net/pfil.h>
116 # endif /* NETBSD_PF */
118 static eventhandler_tag ipf_arrivetag, ipf_departtag;
121 * Disable the "cloner" event handler; we are getting interface
122 * events before the firewall is fully initiallized and also no vnet
123 * information thus leading to uninitialised memory accesses.
124 * In addition it is unclear why we need it in first place.
125 * If it turns out to be needed, well need a dedicated event handler
126 * for it to deal with the ifc and the correct vnet.
128 static eventhandler_tag ipf_clonetag;
131 static void ipf_ifevent(void *arg, struct ifnet *ifp);
133 static void ipf_ifevent(arg, ifp)
138 CURVNET_SET(ifp->if_vnet);
139 if (V_ipfmain.ipf_running > 0)
140 ipf_sync(&V_ipfmain, NULL);
147 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
149 struct ip *ip = mtod(*mp, struct ip *);
153 * IPFilter expects evreything in network byte order
155 #if (__FreeBSD_version < 1000019)
156 ip->ip_len = htons(ip->ip_len);
157 ip->ip_off = htons(ip->ip_off);
159 CURVNET_SET(ifp->if_vnet);
160 rv = ipf_check(&V_ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
163 #if (__FreeBSD_version < 1000019)
164 if ((rv == 0) && (*mp != NULL)) {
165 ip = mtod(*mp, struct ip *);
166 ip->ip_len = ntohs(ip->ip_len);
167 ip->ip_off = ntohs(ip->ip_off);
174 # include <netinet/ip6.h>
177 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
181 CURVNET_SET(ifp->if_vnet);
182 error = ipf_check(&V_ipfmain, mtod(*mp, struct ip *),
183 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp);
188 #if defined(IPFILTER_LKM)
192 if (strcmp(s, "ipl") == 0)
196 #endif /* IPFILTER_LKM */
203 ipf_main_softc_t *softc = arg;
207 READ_ENTER(&softc->ipf_global);
209 if (softc->ipf_running > 0)
210 ipf_slowtimer(softc);
212 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
214 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
216 callout_init(&softc->ipf_slow_ch, 1);
217 callout_reset(&softc->ipf_slow_ch,
218 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
219 ipf_timer_func, softc);
221 RWLOCK_EXIT(&softc->ipf_global);
228 ipf_main_softc_t *softc;
235 if (softc->ipf_running > 0) {
240 if (ipf_init_all(softc) < 0) {
246 bzero((char *)V_ipfmain.ipf_selwait, sizeof(V_ipfmain.ipf_selwait));
247 softc->ipf_running = 1;
249 if (softc->ipf_control_forwarding & 1)
254 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
255 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
257 callout_init(&softc->ipf_slow_ch, 1);
258 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
259 ipf_timer_func, softc);
265 * Disable the filter by removing the hooks from the IP input/output
270 ipf_main_softc_t *softc;
276 if (softc->ipf_control_forwarding & 2)
282 if (softc->ipf_slow_ch.callout != NULL)
283 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
284 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
286 callout_drain(&softc->ipf_slow_ch);
290 softc->ipf_running = -2;
299 * Filter ioctl interface.
302 ipfioctl(dev, cmd, data, mode
305 # define p_cred td_ucred
306 # define p_uid td_ucred->cr_ruid
312 int error = 0, unit = 0;
315 CURVNET_SET(TD_TO_VNET(p));
317 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
319 V_ipfmain.ipf_interror = 130001;
325 unit = GET_MINOR(dev);
326 if ((IPL_LOGMAX < unit) || (unit < 0)) {
327 V_ipfmain.ipf_interror = 130002;
332 if (V_ipfmain.ipf_running <= 0) {
333 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
334 V_ipfmain.ipf_interror = 130003;
338 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
339 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
340 cmd != SIOCGETFS && cmd != SIOCGETFF &&
341 cmd != SIOCIPFINTERROR) {
342 V_ipfmain.ipf_interror = 130004;
350 error = ipf_ioctlswitch(&V_ipfmain, unit, data, cmd, mode, p->p_uid, p);
364 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
365 * requires a large amount of setting up and isn't any more efficient.
371 struct tcphdr *tcp, *tcp2;
380 if (tcp->th_flags & TH_RST)
381 return -1; /* feedback loop */
383 if (ipf_checkl4sum(fin) == -1)
386 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
387 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
388 ((tcp->th_flags & TH_FIN) ? 1 : 0);
391 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
396 MGETHDR(m, M_NOWAIT, MT_HEADER);
398 MGET(m, M_NOWAIT, MT_HEADER);
402 if (sizeof(*tcp2) + hlen > MLEN) {
403 if (!(MCLGET(m, M_NOWAIT))) {
409 m->m_len = sizeof(*tcp2) + hlen;
411 m->m_data += max_linkhdr;
412 m->m_pkthdr.len = m->m_len;
413 m->m_pkthdr.rcvif = (struct ifnet *)0;
415 ip = mtod(m, struct ip *);
416 bzero((char *)ip, hlen);
420 tcp2 = (struct tcphdr *)((char *)ip + hlen);
421 tcp2->th_sport = tcp->th_dport;
422 tcp2->th_dport = tcp->th_sport;
424 if (tcp->th_flags & TH_ACK) {
425 tcp2->th_seq = tcp->th_ack;
426 tcp2->th_flags = TH_RST;
430 tcp2->th_ack = ntohl(tcp->th_seq);
431 tcp2->th_ack += tlen;
432 tcp2->th_ack = htonl(tcp2->th_ack);
433 tcp2->th_flags = TH_RST|TH_ACK;
436 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
437 tcp2->th_win = tcp->th_win;
442 if (fin->fin_v == 6) {
443 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
444 ip6->ip6_plen = htons(sizeof(struct tcphdr));
445 ip6->ip6_nxt = IPPROTO_TCP;
447 ip6->ip6_src = fin->fin_dst6.in6;
448 ip6->ip6_dst = fin->fin_src6.in6;
449 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
450 sizeof(*ip6), sizeof(*tcp2));
451 return ipf_send_ip(fin, m);
454 ip->ip_p = IPPROTO_TCP;
455 ip->ip_len = htons(sizeof(struct tcphdr));
456 ip->ip_src.s_addr = fin->fin_daddr;
457 ip->ip_dst.s_addr = fin->fin_saddr;
458 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
459 ip->ip_len = htons(hlen + sizeof(*tcp2));
460 return ipf_send_ip(fin, m);
465 * ip_len must be in network byte order when called.
476 ip = mtod(m, ip_t *);
477 bzero((char *)&fnew, sizeof(fnew));
478 fnew.fin_main_soft = fin->fin_main_soft;
480 IP_V_A(ip, fin->fin_v);
487 fnew.fin_p = ip->ip_p;
488 fnew.fin_plen = ntohs(ip->ip_len);
489 IP_HL_A(ip, sizeof(*oip) >> 2);
490 ip->ip_tos = oip->ip_tos;
491 ip->ip_id = fin->fin_ip->ip_id;
492 #if defined(FreeBSD) && (__FreeBSD_version > 460000)
493 ip->ip_off = htons(path_mtu_discovery ? IP_DF : 0);
497 ip->ip_ttl = V_ip_defttl;
503 ip6_t *ip6 = (ip6_t *)ip;
506 ip6->ip6_hlim = IPDEFTTL;
509 fnew.fin_p = ip6->ip6_nxt;
511 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
519 m->m_pkthdr.rcvif = NULL;
522 fnew.fin_ifp = fin->fin_ifp;
523 fnew.fin_flx = FI_NOCKSUM;
527 fnew.fin_hlen = hlen;
528 fnew.fin_dp = (char *)ip + hlen;
529 (void) ipf_makefrip(hlen, ip, &fnew);
531 return ipf_fastroute(m, &m, &fnew, NULL);
536 ipf_send_icmp_err(type, fin, dst)
541 int err, hlen, xtra, iclen, ohlen, avail, code;
552 if ((type < 0) || (type >= ICMP_MAXTYPE))
555 code = fin->fin_icode;
558 /* XXX Fix an off by one error: s/>/>=/
560 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
561 Fix obtained from NetBSD ip_fil_netbsd.c r1.4: */
563 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
567 if (ipf_checkl4sum(fin) == -1)
570 MGETHDR(m, M_NOWAIT, MT_HEADER);
572 MGET(m, M_NOWAIT, MT_HEADER);
583 if (fin->fin_v == 4) {
584 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
585 switch (ntohs(fin->fin_data[0]) >> 8)
598 if (ipf_ifpaddr(&V_ipfmain, 4, FRI_NORMAL, ifp,
599 &dst6, NULL) == -1) {
605 dst4.s_addr = fin->fin_daddr;
608 ohlen = fin->fin_hlen;
609 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
610 if (fin->fin_hlen < fin->fin_plen)
611 xtra = MIN(fin->fin_dlen, 8);
617 else if (fin->fin_v == 6) {
618 hlen = sizeof(ip6_t);
619 ohlen = sizeof(ip6_t);
620 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
621 type = icmptoicmp6types[type];
622 if (type == ICMP6_DST_UNREACH)
623 code = icmptoicmp6unreach[code];
625 if (iclen + max_linkhdr + fin->fin_plen > avail) {
626 if (!(MCLGET(m, M_NOWAIT))) {
632 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
633 xtra = MIN(xtra, IPV6_MMTU - iclen);
635 if (ipf_ifpaddr(&V_ipfmain, 6, FRI_NORMAL, ifp,
636 &dst6, NULL) == -1) {
641 dst6 = fin->fin_dst6;
649 avail -= (max_linkhdr + iclen);
657 m->m_data += max_linkhdr;
658 m->m_pkthdr.rcvif = (struct ifnet *)0;
659 m->m_pkthdr.len = iclen;
661 ip = mtod(m, ip_t *);
662 icmp = (struct icmp *)((char *)ip + hlen);
663 ip2 = (ip_t *)&icmp->icmp_ip;
665 icmp->icmp_type = type;
666 icmp->icmp_code = fin->fin_icode;
667 icmp->icmp_cksum = 0;
669 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
670 if (fin->fin_mtu != 0) {
671 icmp->icmp_nextmtu = htons(fin->fin_mtu);
673 } else if (ifp != NULL) {
674 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
676 } else { /* make up a number... */
677 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
682 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
686 if (fin->fin_v == 6) {
687 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
688 ip6->ip6_plen = htons(iclen - hlen);
689 ip6->ip6_nxt = IPPROTO_ICMPV6;
691 ip6->ip6_src = dst6.in6;
692 ip6->ip6_dst = fin->fin_src6.in6;
694 bcopy((char *)fin->fin_ip + ohlen,
695 (char *)&icmp->icmp_ip + ohlen, xtra);
696 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
697 sizeof(*ip6), iclen - hlen);
701 ip->ip_p = IPPROTO_ICMP;
702 ip->ip_src.s_addr = dst4.s_addr;
703 ip->ip_dst.s_addr = fin->fin_saddr;
706 bcopy((char *)fin->fin_ip + ohlen,
707 (char *)&icmp->icmp_ip + ohlen, xtra);
708 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
710 ip->ip_len = htons(iclen);
711 ip->ip_p = IPPROTO_ICMP;
713 err = ipf_send_ip(fin, m);
721 * m0 - pointer to mbuf where the IP packet starts
722 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
725 ipf_fastroute(m0, mpp, fin, fdp)
730 register struct ip *ip, *mhip;
731 register struct mbuf *m = *mpp;
732 int len, off, error = 0, hlen, code;
733 struct ifnet *ifp, *sifp;
734 struct sockaddr_in dst;
735 struct nhop4_extended nh4;
746 * If the mbuf we're about to send is not writable (because of
747 * a cluster reference, for example) we'll need to make a copy
748 * of it since this routine modifies the contents.
750 * If you have non-crappy network hardware that can transmit data
751 * from the mbuf, rather than making a copy, this is gonna be a
754 if (M_WRITABLE(m) == 0) {
755 m0 = m_dup(m, M_NOWAIT);
769 if (fin->fin_v == 6) {
771 * currently "to <if>" and "to <if>:ip#" are not supported
774 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
778 hlen = fin->fin_hlen;
779 ip = mtod(m0, struct ip *);
785 bzero(&dst, sizeof (dst));
786 dst.sin_family = AF_INET;
787 dst.sin_addr = ip->ip_dst;
788 dst.sin_len = sizeof(dst);
791 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
792 (fdp->fd_type == FRD_DSTLIST)) {
793 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
802 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
807 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
808 dst.sin_addr = fdp->fd_ip;
810 fibnum = M_GETFIB(m0);
811 if (fib4_lookup_nh_ext(fibnum, dst.sin_addr, NHR_REF, 0, &nh4) != 0) {
812 if (in_localaddr(ip->ip_dst))
813 error = EHOSTUNREACH;
822 if (nh4.nh_flags & NHF_GATEWAY)
823 dst.sin_addr = nh4.nh_addr;
826 * For input packets which are being "fastrouted", they won't
827 * go back through output filtering and miss their chance to get
828 * NAT'd and counted. Duplicated packets aren't considered to be
829 * part of the normal packet stream, so do not NAT them or pass
830 * them through stateful checking, etc.
832 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
836 (void) ipf_acctpkt(fin, NULL);
838 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
841 (void) ipf_state_check(fin, &pass);
844 switch (ipf_nat_checkout(fin, NULL))
862 * If small enough for interface, can just send directly.
864 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
866 ip->ip_sum = in_cksum(m, hlen);
867 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)&dst,
873 * Too large for interface; fragment if possible.
874 * Must be able to put at least 8 bytes per fragment.
876 ip_off = ntohs(ip->ip_off);
877 if (ip_off & IP_DF) {
881 len = (ifp->if_mtu - hlen) &~ 7;
888 int mhlen, firstlen = len;
889 struct mbuf **mnext = &m->m_act;
892 * Loop through length of segment after first fragment,
893 * make new header and copy data of each part and link onto chain.
896 mhlen = sizeof (struct ip);
897 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
899 MGETHDR(m, M_NOWAIT, MT_HEADER);
901 MGET(m, M_NOWAIT, MT_HEADER);
908 m->m_data += max_linkhdr;
909 mhip = mtod(m, struct ip *);
910 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
911 if (hlen > sizeof (struct ip)) {
912 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
913 IP_HL_A(mhip, mhlen >> 2);
916 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
917 if (off + len >= ntohs(ip->ip_len))
918 len = ntohs(ip->ip_len) - off;
920 mhip->ip_off |= IP_MF;
921 mhip->ip_len = htons((u_short)(len + mhlen));
923 m->m_next = m_copym(m0, off, len, M_NOWAIT);
924 if (m->m_next == 0) {
925 error = ENOBUFS; /* ??? */
928 m->m_pkthdr.len = mhlen + len;
929 m->m_pkthdr.rcvif = NULL;
930 mhip->ip_off = htons((u_short)mhip->ip_off);
932 mhip->ip_sum = in_cksum(m, mhlen);
936 * Update first fragment by trimming what's been copied out
937 * and updating header, then send each fragment (in order).
939 m_adj(m0, hlen + firstlen - ip->ip_len);
940 ip->ip_len = htons((u_short)(hlen + firstlen));
941 ip->ip_off = htons((u_short)IP_MF);
943 ip->ip_sum = in_cksum(m0, hlen);
945 for (m = m0; m; m = m0) {
949 error = (*ifp->if_output)(ifp, m,
950 (struct sockaddr *)&dst,
959 V_ipfmain.ipf_frouteok[0]++;
961 V_ipfmain.ipf_frouteok[1]++;
964 fib4_free_nh_ext(fibnum, &nh4);
968 if (error == EMSGSIZE) {
970 code = fin->fin_icode;
971 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
973 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
975 fin->fin_icode = code;
986 struct nhop4_basic nh4;
988 if (fib4_lookup_nh_basic(0, fin->fin_src, 0, 0, &nh4) != 0)
990 return (fin->fin_ifp == nh4.nh_ifp);
995 * return the first IP Address associated with an interface
998 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
999 ipf_main_softc_t *softc;
1002 i6addr_t *inp, *inpmask;
1005 struct in6_addr *inp6 = NULL;
1007 struct sockaddr *sock, *mask;
1008 struct sockaddr_in *sin;
1012 if ((ifptr == NULL) || (ifptr == (void *)-1))
1019 inp->in4.s_addr = 0;
1022 bzero((char *)inp, sizeof(*inp));
1024 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1026 sock = ifa->ifa_addr;
1027 while (sock != NULL && ifa != NULL) {
1028 sin = (struct sockaddr_in *)sock;
1029 if ((v == 4) && (sin->sin_family == AF_INET))
1032 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1033 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1034 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1035 !IN6_IS_ADDR_LOOPBACK(inp6))
1039 ifa = TAILQ_NEXT(ifa, ifa_link);
1041 sock = ifa->ifa_addr;
1044 if (ifa == NULL || sin == NULL)
1047 mask = ifa->ifa_netmask;
1048 if (atype == FRI_BROADCAST)
1049 sock = ifa->ifa_broadaddr;
1050 else if (atype == FRI_PEERADDR)
1051 sock = ifa->ifa_dstaddr;
1058 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1059 (struct sockaddr_in6 *)mask,
1063 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1064 (struct sockaddr_in *)mask,
1065 &inp->in4, &inpmask->in4);
1074 newiss = arc4random();
1083 #ifdef CSUM_DATA_VALID
1089 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1092 if ((fin->fin_flx & FI_SHORT) != 0)
1095 if (fin->fin_cksum != FI_CK_NEEDED)
1096 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1105 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1107 fin->fin_cksum = FI_CK_BAD;
1108 fin->fin_flx |= FI_BAD;
1109 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));
1112 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1113 /* Depending on the driver, UDP may have zero checksum */
1114 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1115 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1116 udphdr_t *udp = fin->fin_dp;
1117 if (udp->uh_sum == 0) {
1119 * we're good no matter what the hardware
1120 * checksum flags and csum_data say (handling
1121 * of csum_data for zero UDP checksum is not
1122 * consistent across all drivers)
1129 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1130 sum = m->m_pkthdr.csum_data;
1132 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1133 htonl(m->m_pkthdr.csum_data +
1134 fin->fin_dlen + fin->fin_p));
1137 fin->fin_cksum = FI_CK_BAD;
1138 fin->fin_flx |= FI_BAD;
1139 DT2(ipf_fi_bad_checkv4sum_sum, fr_info_t *, fin, u_int, sum);
1141 fin->fin_cksum = FI_CK_SUMOK;
1145 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1146 fin->fin_cksum = FI_CK_L4FULL;
1148 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1149 m->m_pkthdr.csum_flags == CSUM_UDP) {
1150 fin->fin_cksum = FI_CK_L4PART;
1152 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1153 fin->fin_cksum = FI_CK_L4PART;
1161 if (ipf_checkl4sum(fin) == -1) {
1162 fin->fin_flx |= FI_BAD;
1163 DT2(ipf_fi_bad_checkv4sum_manual, fr_info_t *, fin, u_int, manual);
1168 if (ipf_checkl4sum(fin) == -1) {
1169 fin->fin_flx |= FI_BAD;
1170 DT2(ipf_fi_bad_checkv4sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1183 if ((fin->fin_flx & FI_NOCKSUM) != 0) {
1184 DT(ipf_checkv6sum_fi_nocksum);
1188 if ((fin->fin_flx & FI_SHORT) != 0) {
1189 DT(ipf_checkv6sum_fi_short);
1193 if (fin->fin_cksum != FI_CK_NEEDED) {
1194 DT(ipf_checkv6sum_fi_ck_needed);
1195 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1198 if (ipf_checkl4sum(fin) == -1) {
1199 fin->fin_flx |= FI_BAD;
1200 DT2(ipf_fi_bad_checkv6sum_checkl4sum, fr_info_t *, fin, u_int, -1);
1205 #endif /* USE_INET6 */
1214 if ((m0->m_flags & M_PKTHDR) != 0) {
1215 len = m0->m_pkthdr.len;
1219 for (m = m0, len = 0; m != NULL; m = m->m_next)
1226 /* ------------------------------------------------------------------------ */
1227 /* Function: ipf_pullup */
1228 /* Returns: NULL == pullup failed, else pointer to protocol header */
1229 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1230 /* fin(I) - pointer to packet information */
1231 /* len(I) - number of bytes to pullup */
1233 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1234 /* single buffer for ease of access. Operating system native functions are */
1235 /* used to manage buffers - if necessary. If the entire packet ends up in */
1236 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1237 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1238 /* and ONLY if the pullup succeeds. */
1240 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1241 /* of buffers that starts at *fin->fin_mp. */
1242 /* ------------------------------------------------------------------------ */
1244 ipf_pullup(xmin, fin, len)
1256 ip = (char *)fin->fin_ip;
1257 if ((fin->fin_flx & FI_COALESCE) != 0)
1260 ipoff = fin->fin_ipoff;
1261 if (fin->fin_dp != NULL)
1262 dpoff = (char *)fin->fin_dp - (char *)ip;
1266 if (M_LEN(m) < len) {
1267 mb_t *n = *fin->fin_mp;
1269 * Assume that M_PKTHDR is set and just work with what is left
1270 * rather than check..
1271 * Should not make any real difference, anyway.
1275 * Record the mbuf that points to the mbuf that we're
1276 * about to go to work on so that we can update the
1277 * m_next appropriately later.
1279 for (; n->m_next != m; n = n->m_next)
1291 #ifdef HAVE_M_PULLDOWN
1292 if (m_pulldown(m, 0, len, NULL) == NULL)
1295 FREE_MB_T(*fin->fin_mp);
1301 m = m_pullup(m, len);
1307 * When n is non-NULL, it indicates that m pointed to
1308 * a sub-chain (tail) of the mbuf and that the head
1309 * of this chain has not yet been free'd.
1312 FREE_MB_T(*fin->fin_mp);
1315 *fin->fin_mp = NULL;
1323 while (M_LEN(m) == 0) {
1327 ip = MTOD(m, char *) + ipoff;
1329 fin->fin_ip = (ip_t *)ip;
1330 if (fin->fin_dp != NULL)
1331 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1332 if (fin->fin_fraghdr != NULL)
1333 fin->fin_fraghdr = (char *)ip +
1334 ((char *)fin->fin_fraghdr -
1335 (char *)fin->fin_ip);
1338 if (len == fin->fin_plen)
1339 fin->fin_flx |= FI_COALESCE;
1351 if (fin->fin_out == 0) {
1352 netisr_dispatch(NETISR_IP, m);
1354 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1355 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1356 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1362 int ipf_pfil_unhook(void) {
1363 #if defined(NETBSD_PF) && (__FreeBSD_version >= 500011)
1364 struct pfil_head *ph_inet;
1366 struct pfil_head *ph_inet6;
1371 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1372 if (ph_inet != NULL)
1373 pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1374 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1376 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1377 if (ph_inet6 != NULL)
1378 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1379 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1386 int ipf_pfil_hook(void) {
1387 #if defined(NETBSD_PF) && (__FreeBSD_version >= 500011)
1388 struct pfil_head *ph_inet;
1390 struct pfil_head *ph_inet6;
1395 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1397 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1407 if (ph_inet != NULL)
1408 pfil_add_hook((void *)ipf_check_wrapper, NULL,
1409 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1411 if (ph_inet6 != NULL)
1412 pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1413 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1422 ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1423 ipf_ifevent, NULL, \
1424 EVENTHANDLER_PRI_ANY);
1425 ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1426 ipf_ifevent, NULL, \
1427 EVENTHANDLER_PRI_ANY);
1429 ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1430 NULL, EVENTHANDLER_PRI_ANY);
1435 ipf_event_dereg(void)
1437 if (ipf_arrivetag != NULL) {
1438 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag);
1440 if (ipf_departtag != NULL) {
1441 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag);
1444 if (ipf_clonetag != NULL) {
1445 EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag);
1454 return arc4random();
1459 ipf_pcksum(fin, hlen, sum)
1469 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1472 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1477 * Both sum and sum2 are partial sums, so combine them together.
1479 sum += ~sum2 & 0xffff;
1480 while (sum > 0xffff)
1481 sum = (sum & 0xffff) + (sum >> 16);
1482 sum2 = ~sum & 0xffff;