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/mbuf.h>
40 # include <sys/sockopt.h>
42 # include <sys/mbuf.h>
44 #include <sys/socket.h>
45 # include <sys/selinfo.h>
46 # include <netinet/tcp_var.h>
49 # include <net/if_var.h>
50 # include <net/netisr.h>
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/tcp.h>
58 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000)
61 #define CURVNET_SET(arg)
62 #define CURVNET_RESTORE()
64 #include <netinet/udp.h>
65 #include <netinet/tcpip.h>
66 #include <netinet/ip_icmp.h>
67 #include "netinet/ip_compat.h"
69 # include <netinet/icmp6.h>
71 #include "netinet/ip_fil.h"
72 #include "netinet/ip_nat.h"
73 #include "netinet/ip_frag.h"
74 #include "netinet/ip_state.h"
75 #include "netinet/ip_proxy.h"
76 #include "netinet/ip_auth.h"
77 #include "netinet/ip_sync.h"
78 #include "netinet/ip_lookup.h"
79 #include "netinet/ip_dstlist.h"
81 #include "netinet/ip_scan.h"
83 #include "netinet/ip_pool.h"
84 # include <sys/malloc.h>
85 #include <sys/kernel.h>
86 #ifdef CSUM_DATA_VALID
87 #include <machine/in_cksum.h>
89 extern int ip_optcopy __P((struct ip *, struct ip *));
92 # ifdef IPFILTER_M_IPFILTER
93 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
97 static u_short ipid = 0;
98 static int (*ipf_savep) __P((void *, ip_t *, int, void *, int, struct mbuf **));
99 static int ipf_send_ip __P((fr_info_t *, mb_t *));
100 static void ipf_timer_func __P((void *arg));
101 int ipf_locks_done = 0;
103 ipf_main_softc_t ipfmain;
105 # include <sys/conf.h>
106 # if defined(NETBSD_PF)
107 # include <net/pfil.h>
108 # endif /* NETBSD_PF */
110 * We provide the ipf_checkp name just to minimize changes later.
112 int (*ipf_checkp) __P((void *, ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
115 static eventhandler_tag ipf_arrivetag, ipf_departtag, ipf_clonetag;
117 static void ipf_ifevent(void *arg);
119 static void ipf_ifevent(arg)
128 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
130 struct ip *ip = mtod(*mp, struct ip *);
134 * IPFilter expects evreything in network byte order
136 #if (__FreeBSD_version < 1000019)
137 ip->ip_len = htons(ip->ip_len);
138 ip->ip_off = htons(ip->ip_off);
140 rv = ipf_check(&ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
142 #if (__FreeBSD_version < 1000019)
143 if ((rv == 0) && (*mp != NULL)) {
144 ip = mtod(*mp, struct ip *);
145 ip->ip_len = ntohs(ip->ip_len);
146 ip->ip_off = ntohs(ip->ip_off);
153 # include <netinet/ip6.h>
156 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
158 return (ipf_check(&ipfmain, mtod(*mp, struct ip *),
159 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp));
162 #if defined(IPFILTER_LKM)
166 if (strcmp(s, "ipl") == 0)
170 #endif /* IPFILTER_LKM */
177 ipf_main_softc_t *softc = arg;
181 READ_ENTER(&softc->ipf_global);
183 if (softc->ipf_running > 0)
184 ipf_slowtimer(softc);
186 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
188 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
190 callout_init(&softc->ipf_slow_ch, 1);
191 callout_reset(&softc->ipf_slow_ch,
192 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
193 ipf_timer_func, softc);
195 RWLOCK_EXIT(&softc->ipf_global);
202 ipf_main_softc_t *softc;
209 if (softc->ipf_running > 0) {
214 if (ipf_init_all(softc) < 0) {
220 if (ipf_checkp != ipf_check) {
221 ipf_savep = ipf_checkp;
222 ipf_checkp = ipf_check;
225 bzero((char *)ipfmain.ipf_selwait, sizeof(ipfmain.ipf_selwait));
226 softc->ipf_running = 1;
228 if (softc->ipf_control_forwarding & 1)
235 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
236 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
238 callout_init(&softc->ipf_slow_ch, 1);
239 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
240 ipf_timer_func, softc);
246 * Disable the filter by removing the hooks from the IP input/output
251 ipf_main_softc_t *softc;
257 if (softc->ipf_control_forwarding & 2)
263 if (softc->ipf_slow_ch.callout != NULL)
264 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
265 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
267 callout_drain(&softc->ipf_slow_ch);
270 if (ipf_checkp != NULL)
271 ipf_checkp = ipf_savep;
277 softc->ipf_running = -2;
286 * Filter ioctl interface.
289 ipfioctl(dev, cmd, data, mode, p)
291 # define p_cred td_ucred
292 # define p_uid td_ucred->cr_ruid
298 int error = 0, unit = 0;
302 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
304 ipfmain.ipf_interror = 130001;
309 unit = GET_MINOR(dev);
310 if ((IPL_LOGMAX < unit) || (unit < 0)) {
311 ipfmain.ipf_interror = 130002;
315 if (ipfmain.ipf_running <= 0) {
316 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
317 ipfmain.ipf_interror = 130003;
320 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
321 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
322 cmd != SIOCGETFS && cmd != SIOCGETFF &&
323 cmd != SIOCIPFINTERROR) {
324 ipfmain.ipf_interror = 130004;
331 CURVNET_SET(TD_TO_VNET(p));
332 error = ipf_ioctlswitch(&ipfmain, unit, data, cmd, mode, p->p_uid, p);
346 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
347 * requires a large amount of setting up and isn't any more efficient.
353 struct tcphdr *tcp, *tcp2;
362 if (tcp->th_flags & TH_RST)
363 return -1; /* feedback loop */
365 if (ipf_checkl4sum(fin) == -1)
368 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
369 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
370 ((tcp->th_flags & TH_FIN) ? 1 : 0);
373 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
378 MGETHDR(m, M_DONTWAIT, MT_HEADER);
380 MGET(m, M_DONTWAIT, MT_HEADER);
384 if (sizeof(*tcp2) + hlen > MLEN) {
385 MCLGET(m, M_DONTWAIT);
386 if ((m->m_flags & M_EXT) == 0) {
392 m->m_len = sizeof(*tcp2) + hlen;
394 m->m_data += max_linkhdr;
395 m->m_pkthdr.len = m->m_len;
396 m->m_pkthdr.rcvif = (struct ifnet *)0;
398 ip = mtod(m, struct ip *);
399 bzero((char *)ip, hlen);
403 tcp2 = (struct tcphdr *)((char *)ip + hlen);
404 tcp2->th_sport = tcp->th_dport;
405 tcp2->th_dport = tcp->th_sport;
407 if (tcp->th_flags & TH_ACK) {
408 tcp2->th_seq = tcp->th_ack;
409 tcp2->th_flags = TH_RST;
413 tcp2->th_ack = ntohl(tcp->th_seq);
414 tcp2->th_ack += tlen;
415 tcp2->th_ack = htonl(tcp2->th_ack);
416 tcp2->th_flags = TH_RST|TH_ACK;
419 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
420 tcp2->th_win = tcp->th_win;
425 if (fin->fin_v == 6) {
426 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
427 ip6->ip6_plen = htons(sizeof(struct tcphdr));
428 ip6->ip6_nxt = IPPROTO_TCP;
430 ip6->ip6_src = fin->fin_dst6.in6;
431 ip6->ip6_dst = fin->fin_src6.in6;
432 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
433 sizeof(*ip6), sizeof(*tcp2));
434 return ipf_send_ip(fin, m);
437 ip->ip_p = IPPROTO_TCP;
438 ip->ip_len = htons(sizeof(struct tcphdr));
439 ip->ip_src.s_addr = fin->fin_daddr;
440 ip->ip_dst.s_addr = fin->fin_saddr;
441 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
442 ip->ip_len = htons(hlen + sizeof(*tcp2));
443 return ipf_send_ip(fin, m);
448 * ip_len must be in network byte order when called.
459 ip = mtod(m, ip_t *);
460 bzero((char *)&fnew, sizeof(fnew));
461 fnew.fin_main_soft = fin->fin_main_soft;
463 IP_V_A(ip, fin->fin_v);
470 fnew.fin_p = ip->ip_p;
471 fnew.fin_plen = ntohs(ip->ip_len);
472 IP_HL_A(ip, sizeof(*oip) >> 2);
473 ip->ip_tos = oip->ip_tos;
474 ip->ip_id = fin->fin_ip->ip_id;
475 #if defined(FreeBSD) && (__FreeBSD_version > 460000)
476 ip->ip_off = htons(path_mtu_discovery ? IP_DF : 0);
480 ip->ip_ttl = V_ip_defttl;
486 ip6_t *ip6 = (ip6_t *)ip;
489 ip6->ip6_hlim = IPDEFTTL;
492 fnew.fin_p = ip6->ip6_nxt;
494 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
502 m->m_pkthdr.rcvif = NULL;
505 fnew.fin_ifp = fin->fin_ifp;
506 fnew.fin_flx = FI_NOCKSUM;
510 fnew.fin_hlen = hlen;
511 fnew.fin_dp = (char *)ip + hlen;
512 (void) ipf_makefrip(hlen, ip, &fnew);
514 return ipf_fastroute(m, &m, &fnew, NULL);
519 ipf_send_icmp_err(type, fin, dst)
524 int err, hlen, xtra, iclen, ohlen, avail, code;
535 if ((type < 0) || (type >= ICMP_MAXTYPE))
538 code = fin->fin_icode;
540 /* See NetBSD ip_fil_netbsd.c r1.4: */
541 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
545 if (ipf_checkl4sum(fin) == -1)
548 MGETHDR(m, M_DONTWAIT, MT_HEADER);
550 MGET(m, M_DONTWAIT, MT_HEADER);
561 if (fin->fin_v == 4) {
562 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
563 switch (ntohs(fin->fin_data[0]) >> 8)
576 if (ipf_ifpaddr(&ipfmain, 4, FRI_NORMAL, ifp,
577 &dst6, NULL) == -1) {
583 dst4.s_addr = fin->fin_daddr;
586 ohlen = fin->fin_hlen;
587 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
588 if (fin->fin_hlen < fin->fin_plen)
589 xtra = MIN(fin->fin_dlen, 8);
595 else if (fin->fin_v == 6) {
596 hlen = sizeof(ip6_t);
597 ohlen = sizeof(ip6_t);
598 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
599 type = icmptoicmp6types[type];
600 if (type == ICMP6_DST_UNREACH)
601 code = icmptoicmp6unreach[code];
603 if (iclen + max_linkhdr + fin->fin_plen > avail) {
604 MCLGET(m, M_DONTWAIT);
605 if ((m->m_flags & M_EXT) == 0) {
611 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
612 xtra = MIN(xtra, IPV6_MMTU - iclen);
614 if (ipf_ifpaddr(&ipfmain, 6, FRI_NORMAL, ifp,
615 &dst6, NULL) == -1) {
620 dst6 = fin->fin_dst6;
628 avail -= (max_linkhdr + iclen);
636 m->m_data += max_linkhdr;
637 m->m_pkthdr.rcvif = (struct ifnet *)0;
638 m->m_pkthdr.len = iclen;
640 ip = mtod(m, ip_t *);
641 icmp = (struct icmp *)((char *)ip + hlen);
642 ip2 = (ip_t *)&icmp->icmp_ip;
644 icmp->icmp_type = type;
645 icmp->icmp_code = fin->fin_icode;
646 icmp->icmp_cksum = 0;
648 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
649 if (fin->fin_mtu != 0) {
650 icmp->icmp_nextmtu = htons(fin->fin_mtu);
652 } else if (ifp != NULL) {
653 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
655 } else { /* make up a number... */
656 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
661 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
665 if (fin->fin_v == 6) {
666 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
667 ip6->ip6_plen = htons(iclen - hlen);
668 ip6->ip6_nxt = IPPROTO_ICMPV6;
670 ip6->ip6_src = dst6.in6;
671 ip6->ip6_dst = fin->fin_src6.in6;
673 bcopy((char *)fin->fin_ip + ohlen,
674 (char *)&icmp->icmp_ip + ohlen, xtra);
675 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
676 sizeof(*ip6), iclen - hlen);
680 ip->ip_p = IPPROTO_ICMP;
681 ip->ip_src.s_addr = dst4.s_addr;
682 ip->ip_dst.s_addr = fin->fin_saddr;
685 bcopy((char *)fin->fin_ip + ohlen,
686 (char *)&icmp->icmp_ip + ohlen, xtra);
687 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
689 ip->ip_len = htons(iclen);
690 ip->ip_p = IPPROTO_ICMP;
692 err = ipf_send_ip(fin, m);
700 * m0 - pointer to mbuf where the IP packet starts
701 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
704 ipf_fastroute(m0, mpp, fin, fdp)
709 register struct ip *ip, *mhip;
710 register struct mbuf *m = *mpp;
711 register struct route *ro;
712 int len, off, error = 0, hlen, code;
713 struct ifnet *ifp, *sifp;
714 struct sockaddr_in *dst;
715 struct route iproute;
726 * If the mbuf we're about to send is not writable (because of
727 * a cluster reference, for example) we'll need to make a copy
728 * of it since this routine modifies the contents.
730 * If you have non-crappy network hardware that can transmit data
731 * from the mbuf, rather than making a copy, this is gonna be a
734 if (M_WRITABLE(m) == 0) {
735 m0 = m_dup(m, M_DONTWAIT);
749 if (fin->fin_v == 6) {
751 * currently "to <if>" and "to <if>:ip#" are not supported
754 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
758 hlen = fin->fin_hlen;
759 ip = mtod(m0, struct ip *);
766 bzero(ro, sizeof (*ro));
767 dst = (struct sockaddr_in *)&ro->ro_dst;
768 dst->sin_family = AF_INET;
769 dst->sin_addr = ip->ip_dst;
772 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
773 (fdp->fd_type == FRD_DSTLIST)) {
774 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
783 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
788 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
789 dst->sin_addr = fdp->fd_ip;
791 dst->sin_len = sizeof(*dst);
792 in_rtalloc(ro, M_GETFIB(m0));
794 if ((ifp == NULL) && (ro->ro_rt != NULL))
795 ifp = ro->ro_rt->rt_ifp;
797 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
798 if (in_localaddr(ip->ip_dst))
799 error = EHOSTUNREACH;
804 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
805 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
807 counter_u64_add(ro->ro_rt->rt_pksent, 1);
810 * For input packets which are being "fastrouted", they won't
811 * go back through output filtering and miss their chance to get
812 * NAT'd and counted. Duplicated packets aren't considered to be
813 * part of the normal packet stream, so do not NAT them or pass
814 * them through stateful checking, etc.
816 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
820 (void) ipf_acctpkt(fin, NULL);
822 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
825 (void) ipf_state_check(fin, &pass);
828 switch (ipf_nat_checkout(fin, NULL))
846 * If small enough for interface, can just send directly.
848 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
850 ip->ip_sum = in_cksum(m, hlen);
851 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
857 * Too large for interface; fragment if possible.
858 * Must be able to put at least 8 bytes per fragment.
860 ip_off = ntohs(ip->ip_off);
861 if (ip_off & IP_DF) {
865 len = (ifp->if_mtu - hlen) &~ 7;
872 int mhlen, firstlen = len;
873 struct mbuf **mnext = &m->m_act;
876 * Loop through length of segment after first fragment,
877 * make new header and copy data of each part and link onto chain.
880 mhlen = sizeof (struct ip);
881 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
883 MGETHDR(m, M_DONTWAIT, MT_HEADER);
885 MGET(m, M_DONTWAIT, MT_HEADER);
892 m->m_data += max_linkhdr;
893 mhip = mtod(m, struct ip *);
894 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
895 if (hlen > sizeof (struct ip)) {
896 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
897 IP_HL_A(mhip, mhlen >> 2);
900 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
901 if (off + len >= ntohs(ip->ip_len))
902 len = ntohs(ip->ip_len) - off;
904 mhip->ip_off |= IP_MF;
905 mhip->ip_len = htons((u_short)(len + mhlen));
907 m->m_next = m_copy(m0, off, len);
908 if (m->m_next == 0) {
909 error = ENOBUFS; /* ??? */
912 m->m_pkthdr.len = mhlen + len;
913 m->m_pkthdr.rcvif = NULL;
914 mhip->ip_off = htons((u_short)mhip->ip_off);
916 mhip->ip_sum = in_cksum(m, mhlen);
920 * Update first fragment by trimming what's been copied out
921 * and updating header, then send each fragment (in order).
923 m_adj(m0, hlen + firstlen - ip->ip_len);
924 ip->ip_len = htons((u_short)(hlen + firstlen));
925 ip->ip_off = htons((u_short)IP_MF);
927 ip->ip_sum = in_cksum(m0, hlen);
929 for (m = m0; m; m = m0) {
933 error = (*ifp->if_output)(ifp, m,
934 (struct sockaddr *)dst,
943 ipfmain.ipf_frouteok[0]++;
945 ipfmain.ipf_frouteok[1]++;
947 if ((ro != NULL) && (ro->ro_rt != NULL)) {
952 if (error == EMSGSIZE) {
954 code = fin->fin_icode;
955 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
957 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
959 fin->fin_icode = code;
970 struct sockaddr_in *dst;
971 struct route iproute;
973 bzero((char *)&iproute, sizeof(iproute));
974 dst = (struct sockaddr_in *)&iproute.ro_dst;
975 dst->sin_len = sizeof(*dst);
976 dst->sin_family = AF_INET;
977 dst->sin_addr = fin->fin_src;
978 in_rtalloc(&iproute, 0);
979 if (iproute.ro_rt == NULL)
981 return (fin->fin_ifp == iproute.ro_rt->rt_ifp);
986 * return the first IP Address associated with an interface
989 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
990 ipf_main_softc_t *softc;
993 i6addr_t *inp, *inpmask;
996 struct in6_addr *inp6 = NULL;
998 struct sockaddr *sock, *mask;
999 struct sockaddr_in *sin;
1003 if ((ifptr == NULL) || (ifptr == (void *)-1))
1010 inp->in4.s_addr = 0;
1013 bzero((char *)inp, sizeof(*inp));
1015 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1017 sock = ifa->ifa_addr;
1018 while (sock != NULL && ifa != NULL) {
1019 sin = (struct sockaddr_in *)sock;
1020 if ((v == 4) && (sin->sin_family == AF_INET))
1023 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1024 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1025 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1026 !IN6_IS_ADDR_LOOPBACK(inp6))
1030 ifa = TAILQ_NEXT(ifa, ifa_link);
1032 sock = ifa->ifa_addr;
1035 if (ifa == NULL || sin == NULL)
1038 mask = ifa->ifa_netmask;
1039 if (atype == FRI_BROADCAST)
1040 sock = ifa->ifa_broadaddr;
1041 else if (atype == FRI_PEERADDR)
1042 sock = ifa->ifa_dstaddr;
1049 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1050 (struct sockaddr_in6 *)mask,
1054 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1055 (struct sockaddr_in *)mask,
1056 &inp->in4, &inpmask->in4);
1065 newiss = arc4random();
1070 /* ------------------------------------------------------------------------ */
1071 /* Function: ipf_nextipid */
1072 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
1073 /* Parameters: fin(I) - pointer to packet information */
1075 /* Returns the next IPv4 ID to use for this packet. */
1076 /* ------------------------------------------------------------------------ */
1083 #ifndef RANDOM_IP_ID
1084 MUTEX_ENTER(&ipfmain.ipf_rw);
1086 MUTEX_EXIT(&ipfmain.ipf_rw);
1099 #ifdef CSUM_DATA_VALID
1105 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1108 if ((fin->fin_flx & FI_SHORT) != 0)
1111 if (fin->fin_cksum != FI_CK_NEEDED)
1112 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1121 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1123 fin->fin_cksum = FI_CK_BAD;
1124 fin->fin_flx |= FI_BAD;
1127 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1128 /* Depending on the driver, UDP may have zero checksum */
1129 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1130 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1131 udphdr_t *udp = fin->fin_dp;
1132 if (udp->uh_sum == 0) {
1134 * we're good no matter what the hardware
1135 * checksum flags and csum_data say (handling
1136 * of csum_data for zero UDP checksum is not
1137 * consistent across all drivers)
1144 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1145 sum = m->m_pkthdr.csum_data;
1147 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1148 htonl(m->m_pkthdr.csum_data +
1149 fin->fin_dlen + fin->fin_p));
1152 fin->fin_cksum = FI_CK_BAD;
1153 fin->fin_flx |= FI_BAD;
1155 fin->fin_cksum = FI_CK_SUMOK;
1159 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1160 fin->fin_cksum = FI_CK_L4FULL;
1162 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1163 m->m_pkthdr.csum_flags == CSUM_UDP) {
1164 fin->fin_cksum = FI_CK_L4PART;
1166 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1167 fin->fin_cksum = FI_CK_L4PART;
1175 if (ipf_checkl4sum(fin) == -1) {
1176 fin->fin_flx |= FI_BAD;
1181 if (ipf_checkl4sum(fin) == -1) {
1182 fin->fin_flx |= FI_BAD;
1195 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1198 if ((fin->fin_flx & FI_SHORT) != 0)
1201 if (fin->fin_cksum != FI_CK_NEEDED)
1202 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1204 if (ipf_checkl4sum(fin) == -1) {
1205 fin->fin_flx |= FI_BAD;
1210 #endif /* USE_INET6 */
1219 if ((m0->m_flags & M_PKTHDR) != 0) {
1220 len = m0->m_pkthdr.len;
1224 for (m = m0, len = 0; m != NULL; m = m->m_next)
1231 /* ------------------------------------------------------------------------ */
1232 /* Function: ipf_pullup */
1233 /* Returns: NULL == pullup failed, else pointer to protocol header */
1234 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1235 /* fin(I) - pointer to packet information */
1236 /* len(I) - number of bytes to pullup */
1238 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1239 /* single buffer for ease of access. Operating system native functions are */
1240 /* used to manage buffers - if necessary. If the entire packet ends up in */
1241 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1242 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1243 /* and ONLY if the pullup succeeds. */
1245 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1246 /* of buffers that starts at *fin->fin_mp. */
1247 /* ------------------------------------------------------------------------ */
1249 ipf_pullup(xmin, fin, len)
1261 ip = (char *)fin->fin_ip;
1262 if ((fin->fin_flx & FI_COALESCE) != 0)
1265 ipoff = fin->fin_ipoff;
1266 if (fin->fin_dp != NULL)
1267 dpoff = (char *)fin->fin_dp - (char *)ip;
1271 if (M_LEN(m) < len) {
1272 mb_t *n = *fin->fin_mp;
1274 * Assume that M_PKTHDR is set and just work with what is left
1275 * rather than check..
1276 * Should not make any real difference, anyway.
1280 * Record the mbuf that points to the mbuf that we're
1281 * about to go to work on so that we can update the
1282 * m_next appropriately later.
1284 for (; n->m_next != m; n = n->m_next)
1296 #ifdef HAVE_M_PULLDOWN
1297 if (m_pulldown(m, 0, len, NULL) == NULL)
1300 FREE_MB_T(*fin->fin_mp);
1306 m = m_pullup(m, len);
1312 * When n is non-NULL, it indicates that m pointed to
1313 * a sub-chain (tail) of the mbuf and that the head
1314 * of this chain has not yet been free'd.
1317 FREE_MB_T(*fin->fin_mp);
1320 *fin->fin_mp = NULL;
1328 while (M_LEN(m) == 0) {
1332 ip = MTOD(m, char *) + ipoff;
1334 fin->fin_ip = (ip_t *)ip;
1335 if (fin->fin_dp != NULL)
1336 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1337 if (fin->fin_fraghdr != NULL)
1338 fin->fin_fraghdr = (char *)ip +
1339 ((char *)fin->fin_fraghdr -
1340 (char *)fin->fin_ip);
1343 if (len == fin->fin_plen)
1344 fin->fin_flx |= FI_COALESCE;
1356 if (fin->fin_out == 0) {
1357 netisr_dispatch(NETISR_IP, m);
1359 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1360 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1361 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1367 int ipf_pfil_unhook(void) {
1368 #if defined(NETBSD_PF) && (__FreeBSD_version >= 500011)
1369 struct pfil_head *ph_inet;
1371 struct pfil_head *ph_inet6;
1376 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1377 if (ph_inet != NULL)
1378 pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1379 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1381 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1382 if (ph_inet6 != NULL)
1383 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1384 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1391 int ipf_pfil_hook(void) {
1392 #if defined(NETBSD_PF) && (__FreeBSD_version >= 500011)
1393 struct pfil_head *ph_inet;
1395 struct pfil_head *ph_inet6;
1400 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1402 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1412 if (ph_inet != NULL)
1413 pfil_add_hook((void *)ipf_check_wrapper, NULL,
1414 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1416 if (ph_inet6 != NULL)
1417 pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1418 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1427 ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1428 ipf_ifevent, &ipfmain, \
1429 EVENTHANDLER_PRI_ANY);
1430 ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1431 ipf_ifevent, &ipfmain, \
1432 EVENTHANDLER_PRI_ANY);
1433 ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1434 &ipfmain, EVENTHANDLER_PRI_ANY);
1438 ipf_event_dereg(void)
1440 if (ipf_arrivetag != NULL) {
1441 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag);
1443 if (ipf_departtag != NULL) {
1444 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag);
1446 if (ipf_clonetag != NULL) {
1447 EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag);
1455 return arc4random();
1460 ipf_pcksum(fin, hlen, sum)
1470 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1473 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1478 * Both sum and sum2 are partial sums, so combine them together.
1480 sum += ~sum2 & 0xffff;
1481 while (sum > 0xffff)
1482 sum = (sum & 0xffff) + (sum >> 16);
1483 sum2 = ~sum & 0xffff;