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()
65 # include <netinet/tcp_timer.h>
67 #include <netinet/udp.h>
68 #include <netinet/tcpip.h>
69 #include <netinet/ip_icmp.h>
70 #include "netinet/ip_compat.h"
72 # include <netinet/icmp6.h>
74 #include "netinet/ip_fil.h"
75 #include "netinet/ip_nat.h"
76 #include "netinet/ip_frag.h"
77 #include "netinet/ip_state.h"
78 #include "netinet/ip_proxy.h"
79 #include "netinet/ip_auth.h"
80 #include "netinet/ip_sync.h"
81 #include "netinet/ip_lookup.h"
82 #include "netinet/ip_dstlist.h"
84 #include "netinet/ip_scan.h"
86 #include "netinet/ip_pool.h"
87 # include <sys/malloc.h>
88 #include <sys/kernel.h>
89 #ifdef CSUM_DATA_VALID
90 #include <machine/in_cksum.h>
92 extern int ip_optcopy __P((struct ip *, struct ip *));
95 # ifdef IPFILTER_M_IPFILTER
96 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
100 static u_short ipid = 0;
101 static int (*ipf_savep) __P((void *, ip_t *, int, void *, int, struct mbuf **));
102 static int ipf_send_ip __P((fr_info_t *, mb_t *));
103 static void ipf_timer_func __P((void *arg));
104 int ipf_locks_done = 0;
106 ipf_main_softc_t ipfmain;
108 # include <sys/conf.h>
109 # if defined(NETBSD_PF)
110 # include <net/pfil.h>
111 # endif /* NETBSD_PF */
113 * We provide the ipf_checkp name just to minimize changes later.
115 int (*ipf_checkp) __P((void *, ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
118 static eventhandler_tag ipf_arrivetag, ipf_departtag, ipf_clonetag;
120 static void ipf_ifevent(void *arg);
122 static void ipf_ifevent(arg)
131 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
133 struct ip *ip = mtod(*mp, struct ip *);
137 * IPFilter expects evreything in network byte order
139 #if (__FreeBSD_version < 1000019)
140 ip->ip_len = htons(ip->ip_len);
141 ip->ip_off = htons(ip->ip_off);
143 rv = ipf_check(&ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
145 #if (__FreeBSD_version < 1000019)
146 if ((rv == 0) && (*mp != NULL)) {
147 ip = mtod(*mp, struct ip *);
148 ip->ip_len = ntohs(ip->ip_len);
149 ip->ip_off = ntohs(ip->ip_off);
156 # include <netinet/ip6.h>
159 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
161 return (ipf_check(&ipfmain, mtod(*mp, struct ip *),
162 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp));
165 #if defined(IPFILTER_LKM)
169 if (strcmp(s, "ipl") == 0)
173 #endif /* IPFILTER_LKM */
180 ipf_main_softc_t *softc = arg;
184 READ_ENTER(&softc->ipf_global);
186 if (softc->ipf_running > 0)
187 ipf_slowtimer(softc);
189 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
191 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
193 callout_init(&softc->ipf_slow_ch, CALLOUT_MPSAFE);
194 callout_reset(&softc->ipf_slow_ch,
195 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
196 ipf_timer_func, softc);
198 RWLOCK_EXIT(&softc->ipf_global);
205 ipf_main_softc_t *softc;
212 if (softc->ipf_running > 0) {
217 if (ipf_init_all(softc) < 0) {
223 if (ipf_checkp != ipf_check) {
224 ipf_savep = ipf_checkp;
225 ipf_checkp = ipf_check;
228 bzero((char *)ipfmain.ipf_selwait, sizeof(ipfmain.ipf_selwait));
229 softc->ipf_running = 1;
231 if (softc->ipf_control_forwarding & 1)
238 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
239 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
241 callout_init(&softc->ipf_slow_ch, CALLOUT_MPSAFE);
242 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
243 ipf_timer_func, softc);
249 * Disable the filter by removing the hooks from the IP input/output
254 ipf_main_softc_t *softc;
260 if (softc->ipf_control_forwarding & 2)
266 if (softc->ipf_slow_ch.callout != NULL)
267 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
268 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
270 callout_drain(&softc->ipf_slow_ch);
273 if (ipf_checkp != NULL)
274 ipf_checkp = ipf_savep;
280 softc->ipf_running = -2;
289 * Filter ioctl interface.
292 ipfioctl(dev, cmd, data, mode
295 # define p_cred td_ucred
296 # define p_uid td_ucred->cr_ruid
302 int error = 0, unit = 0;
306 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
308 ipfmain.ipf_interror = 130001;
313 unit = GET_MINOR(dev);
314 if ((IPL_LOGMAX < unit) || (unit < 0)) {
315 ipfmain.ipf_interror = 130002;
319 if (ipfmain.ipf_running <= 0) {
320 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
321 ipfmain.ipf_interror = 130003;
324 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
325 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
326 cmd != SIOCGETFS && cmd != SIOCGETFF &&
327 cmd != SIOCIPFINTERROR) {
328 ipfmain.ipf_interror = 130004;
335 CURVNET_SET(TD_TO_VNET(p));
336 error = ipf_ioctlswitch(&ipfmain, unit, data, cmd, mode, p->p_uid, p);
350 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
351 * requires a large amount of setting up and isn't any more efficient.
357 struct tcphdr *tcp, *tcp2;
366 if (tcp->th_flags & TH_RST)
367 return -1; /* feedback loop */
369 if (ipf_checkl4sum(fin) == -1)
372 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
373 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
374 ((tcp->th_flags & TH_FIN) ? 1 : 0);
377 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
382 MGETHDR(m, M_DONTWAIT, MT_HEADER);
384 MGET(m, M_DONTWAIT, MT_HEADER);
388 if (sizeof(*tcp2) + hlen > MLEN) {
389 MCLGET(m, M_DONTWAIT);
390 if ((m->m_flags & M_EXT) == 0) {
396 m->m_len = sizeof(*tcp2) + hlen;
398 m->m_data += max_linkhdr;
399 m->m_pkthdr.len = m->m_len;
400 m->m_pkthdr.rcvif = (struct ifnet *)0;
402 ip = mtod(m, struct ip *);
403 bzero((char *)ip, hlen);
407 tcp2 = (struct tcphdr *)((char *)ip + hlen);
408 tcp2->th_sport = tcp->th_dport;
409 tcp2->th_dport = tcp->th_sport;
411 if (tcp->th_flags & TH_ACK) {
412 tcp2->th_seq = tcp->th_ack;
413 tcp2->th_flags = TH_RST;
417 tcp2->th_ack = ntohl(tcp->th_seq);
418 tcp2->th_ack += tlen;
419 tcp2->th_ack = htonl(tcp2->th_ack);
420 tcp2->th_flags = TH_RST|TH_ACK;
423 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
424 tcp2->th_win = tcp->th_win;
429 if (fin->fin_v == 6) {
430 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
431 ip6->ip6_plen = htons(sizeof(struct tcphdr));
432 ip6->ip6_nxt = IPPROTO_TCP;
434 ip6->ip6_src = fin->fin_dst6.in6;
435 ip6->ip6_dst = fin->fin_src6.in6;
436 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
437 sizeof(*ip6), sizeof(*tcp2));
438 return ipf_send_ip(fin, m);
441 ip->ip_p = IPPROTO_TCP;
442 ip->ip_len = htons(sizeof(struct tcphdr));
443 ip->ip_src.s_addr = fin->fin_daddr;
444 ip->ip_dst.s_addr = fin->fin_saddr;
445 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
446 ip->ip_len = htons(hlen + sizeof(*tcp2));
447 return ipf_send_ip(fin, m);
452 * ip_len must be in network byte order when called.
463 ip = mtod(m, ip_t *);
464 bzero((char *)&fnew, sizeof(fnew));
465 fnew.fin_main_soft = fin->fin_main_soft;
467 IP_V_A(ip, fin->fin_v);
474 fnew.fin_p = ip->ip_p;
475 fnew.fin_plen = ntohs(ip->ip_len);
476 IP_HL_A(ip, sizeof(*oip) >> 2);
477 ip->ip_tos = oip->ip_tos;
478 ip->ip_id = fin->fin_ip->ip_id;
479 #if defined(FreeBSD) && (__FreeBSD_version > 460000)
480 ip->ip_off = htons(path_mtu_discovery ? IP_DF : 0);
484 ip->ip_ttl = V_ip_defttl;
490 ip6_t *ip6 = (ip6_t *)ip;
493 ip6->ip6_hlim = IPDEFTTL;
496 fnew.fin_p = ip6->ip6_nxt;
498 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
506 m->m_pkthdr.rcvif = NULL;
509 fnew.fin_ifp = fin->fin_ifp;
510 fnew.fin_flx = FI_NOCKSUM;
514 fnew.fin_hlen = hlen;
515 fnew.fin_dp = (char *)ip + hlen;
516 (void) ipf_makefrip(hlen, ip, &fnew);
518 return ipf_fastroute(m, &m, &fnew, NULL);
523 ipf_send_icmp_err(type, fin, dst)
528 int err, hlen, xtra, iclen, ohlen, avail, code;
539 if ((type < 0) || (type >= ICMP_MAXTYPE))
542 code = fin->fin_icode;
545 /* XXX Fix an off by one error: s/>/>=/
547 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
548 Fix obtained from NetBSD ip_fil_netbsd.c r1.4: */
550 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
554 if (ipf_checkl4sum(fin) == -1)
557 MGETHDR(m, M_DONTWAIT, MT_HEADER);
559 MGET(m, M_DONTWAIT, MT_HEADER);
570 if (fin->fin_v == 4) {
571 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
572 switch (ntohs(fin->fin_data[0]) >> 8)
585 if (ipf_ifpaddr(&ipfmain, 4, FRI_NORMAL, ifp,
586 &dst6, NULL) == -1) {
592 dst4.s_addr = fin->fin_daddr;
595 ohlen = fin->fin_hlen;
596 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
597 if (fin->fin_hlen < fin->fin_plen)
598 xtra = MIN(fin->fin_dlen, 8);
604 else if (fin->fin_v == 6) {
605 hlen = sizeof(ip6_t);
606 ohlen = sizeof(ip6_t);
607 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
608 type = icmptoicmp6types[type];
609 if (type == ICMP6_DST_UNREACH)
610 code = icmptoicmp6unreach[code];
612 if (iclen + max_linkhdr + fin->fin_plen > avail) {
613 MCLGET(m, M_DONTWAIT);
614 if ((m->m_flags & M_EXT) == 0) {
620 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
621 xtra = MIN(xtra, IPV6_MMTU - iclen);
623 if (ipf_ifpaddr(&ipfmain, 6, FRI_NORMAL, ifp,
624 &dst6, NULL) == -1) {
629 dst6 = fin->fin_dst6;
637 avail -= (max_linkhdr + iclen);
645 m->m_data += max_linkhdr;
646 m->m_pkthdr.rcvif = (struct ifnet *)0;
647 m->m_pkthdr.len = iclen;
649 ip = mtod(m, ip_t *);
650 icmp = (struct icmp *)((char *)ip + hlen);
651 ip2 = (ip_t *)&icmp->icmp_ip;
653 icmp->icmp_type = type;
654 icmp->icmp_code = fin->fin_icode;
655 icmp->icmp_cksum = 0;
657 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
658 if (fin->fin_mtu != 0) {
659 icmp->icmp_nextmtu = htons(fin->fin_mtu);
661 } else if (ifp != NULL) {
662 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
664 } else { /* make up a number... */
665 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
670 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
674 if (fin->fin_v == 6) {
675 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
676 ip6->ip6_plen = htons(iclen - hlen);
677 ip6->ip6_nxt = IPPROTO_ICMPV6;
679 ip6->ip6_src = dst6.in6;
680 ip6->ip6_dst = fin->fin_src6.in6;
682 bcopy((char *)fin->fin_ip + ohlen,
683 (char *)&icmp->icmp_ip + ohlen, xtra);
684 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
685 sizeof(*ip6), iclen - hlen);
689 ip->ip_p = IPPROTO_ICMP;
690 ip->ip_src.s_addr = dst4.s_addr;
691 ip->ip_dst.s_addr = fin->fin_saddr;
694 bcopy((char *)fin->fin_ip + ohlen,
695 (char *)&icmp->icmp_ip + ohlen, xtra);
696 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
698 ip->ip_len = htons(iclen);
699 ip->ip_p = IPPROTO_ICMP;
701 err = ipf_send_ip(fin, m);
709 * m0 - pointer to mbuf where the IP packet starts
710 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
713 ipf_fastroute(m0, mpp, fin, fdp)
718 register struct ip *ip, *mhip;
719 register struct mbuf *m = *mpp;
720 register struct route *ro;
721 int len, off, error = 0, hlen, code;
722 struct ifnet *ifp, *sifp;
723 struct sockaddr_in *dst;
724 struct route iproute;
735 * If the mbuf we're about to send is not writable (because of
736 * a cluster reference, for example) we'll need to make a copy
737 * of it since this routine modifies the contents.
739 * If you have non-crappy network hardware that can transmit data
740 * from the mbuf, rather than making a copy, this is gonna be a
743 if (M_WRITABLE(m) == 0) {
744 m0 = m_dup(m, M_DONTWAIT);
758 if (fin->fin_v == 6) {
760 * currently "to <if>" and "to <if>:ip#" are not supported
763 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
767 hlen = fin->fin_hlen;
768 ip = mtod(m0, struct ip *);
775 bzero(ro, sizeof (*ro));
776 dst = (struct sockaddr_in *)&ro->ro_dst;
777 dst->sin_family = AF_INET;
778 dst->sin_addr = ip->ip_dst;
781 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
782 (fdp->fd_type == FRD_DSTLIST)) {
783 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
792 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
797 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
798 dst->sin_addr = fdp->fd_ip;
800 dst->sin_len = sizeof(*dst);
801 in_rtalloc(ro, M_GETFIB(m0));
803 if ((ifp == NULL) && (ro->ro_rt != NULL))
804 ifp = ro->ro_rt->rt_ifp;
806 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
807 if (in_localaddr(ip->ip_dst))
808 error = EHOSTUNREACH;
813 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
814 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
816 counter_u64_add(ro->ro_rt->rt_pksent, 1);
819 * For input packets which are being "fastrouted", they won't
820 * go back through output filtering and miss their chance to get
821 * NAT'd and counted. Duplicated packets aren't considered to be
822 * part of the normal packet stream, so do not NAT them or pass
823 * them through stateful checking, etc.
825 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
829 (void) ipf_acctpkt(fin, NULL);
831 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
834 (void) ipf_state_check(fin, &pass);
837 switch (ipf_nat_checkout(fin, NULL))
855 * If small enough for interface, can just send directly.
857 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
859 ip->ip_sum = in_cksum(m, hlen);
860 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
866 * Too large for interface; fragment if possible.
867 * Must be able to put at least 8 bytes per fragment.
869 ip_off = ntohs(ip->ip_off);
870 if (ip_off & IP_DF) {
874 len = (ifp->if_mtu - hlen) &~ 7;
881 int mhlen, firstlen = len;
882 struct mbuf **mnext = &m->m_act;
885 * Loop through length of segment after first fragment,
886 * make new header and copy data of each part and link onto chain.
889 mhlen = sizeof (struct ip);
890 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
892 MGETHDR(m, M_DONTWAIT, MT_HEADER);
894 MGET(m, M_DONTWAIT, MT_HEADER);
901 m->m_data += max_linkhdr;
902 mhip = mtod(m, struct ip *);
903 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
904 if (hlen > sizeof (struct ip)) {
905 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
906 IP_HL_A(mhip, mhlen >> 2);
909 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
910 if (off + len >= ntohs(ip->ip_len))
911 len = ntohs(ip->ip_len) - off;
913 mhip->ip_off |= IP_MF;
914 mhip->ip_len = htons((u_short)(len + mhlen));
916 m->m_next = m_copy(m0, off, len);
917 if (m->m_next == 0) {
918 error = ENOBUFS; /* ??? */
921 m->m_pkthdr.len = mhlen + len;
922 m->m_pkthdr.rcvif = NULL;
923 mhip->ip_off = htons((u_short)mhip->ip_off);
925 mhip->ip_sum = in_cksum(m, mhlen);
929 * Update first fragment by trimming what's been copied out
930 * and updating header, then send each fragment (in order).
932 m_adj(m0, hlen + firstlen - ip->ip_len);
933 ip->ip_len = htons((u_short)(hlen + firstlen));
934 ip->ip_off = htons((u_short)IP_MF);
936 ip->ip_sum = in_cksum(m0, hlen);
938 for (m = m0; m; m = m0) {
942 error = (*ifp->if_output)(ifp, m,
943 (struct sockaddr *)dst,
952 ipfmain.ipf_frouteok[0]++;
954 ipfmain.ipf_frouteok[1]++;
956 if ((ro != NULL) && (ro->ro_rt != NULL)) {
961 if (error == EMSGSIZE) {
963 code = fin->fin_icode;
964 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
966 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
968 fin->fin_icode = code;
979 struct sockaddr_in *dst;
980 struct route iproute;
982 bzero((char *)&iproute, sizeof(iproute));
983 dst = (struct sockaddr_in *)&iproute.ro_dst;
984 dst->sin_len = sizeof(*dst);
985 dst->sin_family = AF_INET;
986 dst->sin_addr = fin->fin_src;
987 in_rtalloc(&iproute, 0);
988 if (iproute.ro_rt == NULL)
990 return (fin->fin_ifp == iproute.ro_rt->rt_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();
1079 /* ------------------------------------------------------------------------ */
1080 /* Function: ipf_nextipid */
1081 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
1082 /* Parameters: fin(I) - pointer to packet information */
1084 /* Returns the next IPv4 ID to use for this packet. */
1085 /* ------------------------------------------------------------------------ */
1092 #ifndef RANDOM_IP_ID
1093 MUTEX_ENTER(&ipfmain.ipf_rw);
1095 MUTEX_EXIT(&ipfmain.ipf_rw);
1108 #ifdef CSUM_DATA_VALID
1114 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1117 if ((fin->fin_flx & FI_SHORT) != 0)
1120 if (fin->fin_cksum != FI_CK_NEEDED)
1121 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1130 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1132 fin->fin_cksum = FI_CK_BAD;
1133 fin->fin_flx |= FI_BAD;
1136 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1137 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1138 sum = m->m_pkthdr.csum_data;
1140 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1141 htonl(m->m_pkthdr.csum_data +
1142 fin->fin_dlen + fin->fin_p));
1145 fin->fin_cksum = FI_CK_BAD;
1146 fin->fin_flx |= FI_BAD;
1148 fin->fin_cksum = FI_CK_SUMOK;
1152 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1153 fin->fin_cksum = FI_CK_L4FULL;
1155 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1156 m->m_pkthdr.csum_flags == CSUM_UDP) {
1157 fin->fin_cksum = FI_CK_L4PART;
1159 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1160 fin->fin_cksum = FI_CK_L4PART;
1168 if (ipf_checkl4sum(fin) == -1) {
1169 fin->fin_flx |= FI_BAD;
1174 if (ipf_checkl4sum(fin) == -1) {
1175 fin->fin_flx |= FI_BAD;
1188 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1191 if ((fin->fin_flx & FI_SHORT) != 0)
1194 if (fin->fin_cksum != FI_CK_NEEDED)
1195 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1197 if (ipf_checkl4sum(fin) == -1) {
1198 fin->fin_flx |= FI_BAD;
1203 #endif /* USE_INET6 */
1212 if ((m0->m_flags & M_PKTHDR) != 0) {
1213 len = m0->m_pkthdr.len;
1217 for (m = m0, len = 0; m != NULL; m = m->m_next)
1224 /* ------------------------------------------------------------------------ */
1225 /* Function: ipf_pullup */
1226 /* Returns: NULL == pullup failed, else pointer to protocol header */
1227 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1228 /* fin(I) - pointer to packet information */
1229 /* len(I) - number of bytes to pullup */
1231 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1232 /* single buffer for ease of access. Operating system native functions are */
1233 /* used to manage buffers - if necessary. If the entire packet ends up in */
1234 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1235 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1236 /* and ONLY if the pullup succeeds. */
1238 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1239 /* of buffers that starts at *fin->fin_mp. */
1240 /* ------------------------------------------------------------------------ */
1242 ipf_pullup(xmin, fin, len)
1254 ip = (char *)fin->fin_ip;
1255 if ((fin->fin_flx & FI_COALESCE) != 0)
1258 ipoff = fin->fin_ipoff;
1259 if (fin->fin_dp != NULL)
1260 dpoff = (char *)fin->fin_dp - (char *)ip;
1264 if (M_LEN(m) < len) {
1265 mb_t *n = *fin->fin_mp;
1267 * Assume that M_PKTHDR is set and just work with what is left
1268 * rather than check..
1269 * Should not make any real difference, anyway.
1273 * Record the mbuf that points to the mbuf that we're
1274 * about to go to work on so that we can update the
1275 * m_next appropriately later.
1277 for (; n->m_next != m; n = n->m_next)
1289 #ifdef HAVE_M_PULLDOWN
1290 if (m_pulldown(m, 0, len, NULL) == NULL)
1293 FREE_MB_T(*fin->fin_mp);
1299 m = m_pullup(m, len);
1305 * When n is non-NULL, it indicates that m pointed to
1306 * a sub-chain (tail) of the mbuf and that the head
1307 * of this chain has not yet been free'd.
1310 FREE_MB_T(*fin->fin_mp);
1313 *fin->fin_mp = NULL;
1321 while (M_LEN(m) == 0) {
1325 ip = MTOD(m, char *) + ipoff;
1327 fin->fin_ip = (ip_t *)ip;
1328 if (fin->fin_dp != NULL)
1329 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1330 if (fin->fin_fraghdr != NULL)
1331 fin->fin_fraghdr = (char *)ip +
1332 ((char *)fin->fin_fraghdr -
1333 (char *)fin->fin_ip);
1336 if (len == fin->fin_plen)
1337 fin->fin_flx |= FI_COALESCE;
1349 if (fin->fin_out == 0) {
1350 netisr_dispatch(NETISR_IP, m);
1352 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1353 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1354 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1360 int ipf_pfil_unhook(void) {
1361 #if defined(NETBSD_PF) && (__FreeBSD_version >= 500011)
1362 struct pfil_head *ph_inet;
1364 struct pfil_head *ph_inet6;
1369 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1370 if (ph_inet != NULL)
1371 pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1372 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1374 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1375 if (ph_inet6 != NULL)
1376 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1377 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1384 int ipf_pfil_hook(void) {
1385 #if defined(NETBSD_PF) && (__FreeBSD_version >= 500011)
1386 struct pfil_head *ph_inet;
1388 struct pfil_head *ph_inet6;
1393 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1395 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1405 if (ph_inet != NULL)
1406 pfil_add_hook((void *)ipf_check_wrapper, NULL,
1407 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1409 if (ph_inet6 != NULL)
1410 pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1411 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1420 ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1421 ipf_ifevent, &ipfmain, \
1422 EVENTHANDLER_PRI_ANY);
1423 ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1424 ipf_ifevent, &ipfmain, \
1425 EVENTHANDLER_PRI_ANY);
1426 ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1427 &ipfmain, EVENTHANDLER_PRI_ANY);
1431 ipf_event_dereg(void)
1433 if (ipf_arrivetag != NULL) {
1434 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag);
1436 if (ipf_departtag != NULL) {
1437 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag);
1439 if (ipf_clonetag != NULL) {
1440 EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag);
1448 return arc4random();
1453 ipf_pcksum(fin, hlen, sum)
1463 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1466 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1471 * Both sum and sum2 are partial sums, so combine them together.
1473 sum += ~sum2 & 0xffff;
1474 while (sum > 0xffff)
1475 sum = (sum & 0xffff) + (sum >> 16);
1476 sum2 = ~sum & 0xffff;