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 # include <sys/mbuf.h>
37 # include <sys/sockopt.h>
39 # include <sys/mbuf.h>
41 #include <sys/socket.h>
42 # include <sys/selinfo.h>
43 # include <netinet/tcp_var.h>
46 # include <net/if_var.h>
47 # include <net/netisr.h>
48 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/tcp.h>
56 # include <netinet/tcp_timer.h>
58 #include <netinet/udp.h>
59 #include <netinet/tcpip.h>
60 #include <netinet/ip_icmp.h>
61 #include "netinet/ip_compat.h"
63 # include <netinet/icmp6.h>
65 #include "netinet/ip_fil.h"
66 #include "netinet/ip_nat.h"
67 #include "netinet/ip_frag.h"
68 #include "netinet/ip_state.h"
69 #include "netinet/ip_proxy.h"
70 #include "netinet/ip_auth.h"
71 #include "netinet/ip_sync.h"
72 #include "netinet/ip_lookup.h"
73 #include "netinet/ip_dstlist.h"
75 #include "netinet/ip_scan.h"
77 #include "netinet/ip_pool.h"
78 # include <sys/malloc.h>
79 #include <sys/kernel.h>
80 #ifdef CSUM_DATA_VALID
81 #include <machine/in_cksum.h>
83 extern int ip_optcopy __P((struct ip *, struct ip *));
86 # ifdef IPFILTER_M_IPFILTER
87 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
91 static u_short ipid = 0;
92 static int (*ipf_savep) __P((void *, ip_t *, int, void *, int, struct mbuf **));
93 static int ipf_send_ip __P((fr_info_t *, mb_t *));
94 static void ipf_timer_func __P((void *arg));
95 int ipf_locks_done = 0;
97 ipf_main_softc_t ipfmain;
99 # include <sys/conf.h>
100 # if defined(NETBSD_PF)
101 # include <net/pfil.h>
102 # endif /* NETBSD_PF */
104 * We provide the ipf_checkp name just to minimize changes later.
106 int (*ipf_checkp) __P((void *, ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
109 static eventhandler_tag ipf_arrivetag, ipf_departtag, ipf_clonetag;
111 static void ipf_ifevent(void *arg);
113 static void ipf_ifevent(arg)
122 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
124 struct ip *ip = mtod(*mp, struct ip *);
128 * IPFilter expects evreything in network byte order
130 #if (__FreeBSD_version < 1000019)
131 ip->ip_len = htons(ip->ip_len);
132 ip->ip_off = htons(ip->ip_off);
134 rv = ipf_check(&ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
136 #if (__FreeBSD_version < 1000019)
137 if ((rv == 0) && (*mp != NULL)) {
138 ip = mtod(*mp, struct ip *);
139 ip->ip_len = ntohs(ip->ip_len);
140 ip->ip_off = ntohs(ip->ip_off);
147 # include <netinet/ip6.h>
150 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
152 return (ipf_check(&ipfmain, mtod(*mp, struct ip *),
153 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp));
156 #if defined(IPFILTER_LKM)
160 if (strcmp(s, "ipl") == 0)
164 #endif /* IPFILTER_LKM */
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, CALLOUT_MPSAFE);
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);
196 ipf_main_softc_t *softc;
203 if (softc->ipf_running > 0) {
208 if (ipf_init_all(softc) < 0) {
214 if (ipf_checkp != ipf_check) {
215 ipf_savep = ipf_checkp;
216 ipf_checkp = ipf_check;
219 bzero((char *)ipfmain.ipf_selwait, sizeof(ipfmain.ipf_selwait));
220 softc->ipf_running = 1;
222 if (softc->ipf_control_forwarding & 1)
229 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
230 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
232 callout_init(&softc->ipf_slow_ch, CALLOUT_MPSAFE);
233 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
234 ipf_timer_func, softc);
240 * Disable the filter by removing the hooks from the IP input/output
245 ipf_main_softc_t *softc;
251 if (softc->ipf_control_forwarding & 2)
257 if (softc->ipf_slow_ch.callout != NULL)
258 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
259 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
261 callout_drain(&softc->ipf_slow_ch);
264 if (ipf_checkp != NULL)
265 ipf_checkp = ipf_savep;
271 softc->ipf_running = -2;
280 * Filter ioctl interface.
283 ipfioctl(dev, cmd, data, mode
286 # define p_cred td_ucred
287 # define p_uid td_ucred->cr_ruid
293 int error = 0, unit = 0;
297 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
299 ipfmain.ipf_interror = 130001;
304 unit = GET_MINOR(dev);
305 if ((IPL_LOGMAX < unit) || (unit < 0)) {
306 ipfmain.ipf_interror = 130002;
310 if (ipfmain.ipf_running <= 0) {
311 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
312 ipfmain.ipf_interror = 130003;
315 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
316 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
317 cmd != SIOCGETFS && cmd != SIOCGETFF &&
318 cmd != SIOCIPFINTERROR) {
319 ipfmain.ipf_interror = 130004;
326 error = ipf_ioctlswitch(&ipfmain, unit, data, cmd, mode, p->p_uid, p);
339 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
340 * requires a large amount of setting up and isn't any more efficient.
346 struct tcphdr *tcp, *tcp2;
355 if (tcp->th_flags & TH_RST)
356 return -1; /* feedback loop */
358 if (ipf_checkl4sum(fin) == -1)
361 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
362 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
363 ((tcp->th_flags & TH_FIN) ? 1 : 0);
366 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
371 MGETHDR(m, M_DONTWAIT, MT_HEADER);
373 MGET(m, M_DONTWAIT, MT_HEADER);
377 if (sizeof(*tcp2) + hlen > MLEN) {
378 MCLGET(m, M_DONTWAIT);
379 if ((m->m_flags & M_EXT) == 0) {
385 m->m_len = sizeof(*tcp2) + hlen;
387 m->m_data += max_linkhdr;
388 m->m_pkthdr.len = m->m_len;
389 m->m_pkthdr.rcvif = (struct ifnet *)0;
391 ip = mtod(m, struct ip *);
392 bzero((char *)ip, hlen);
396 tcp2 = (struct tcphdr *)((char *)ip + hlen);
397 tcp2->th_sport = tcp->th_dport;
398 tcp2->th_dport = tcp->th_sport;
400 if (tcp->th_flags & TH_ACK) {
401 tcp2->th_seq = tcp->th_ack;
402 tcp2->th_flags = TH_RST;
406 tcp2->th_ack = ntohl(tcp->th_seq);
407 tcp2->th_ack += tlen;
408 tcp2->th_ack = htonl(tcp2->th_ack);
409 tcp2->th_flags = TH_RST|TH_ACK;
412 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
413 tcp2->th_win = tcp->th_win;
418 if (fin->fin_v == 6) {
419 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
420 ip6->ip6_plen = htons(sizeof(struct tcphdr));
421 ip6->ip6_nxt = IPPROTO_TCP;
423 ip6->ip6_src = fin->fin_dst6.in6;
424 ip6->ip6_dst = fin->fin_src6.in6;
425 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
426 sizeof(*ip6), sizeof(*tcp2));
427 return ipf_send_ip(fin, m);
430 ip->ip_p = IPPROTO_TCP;
431 ip->ip_len = htons(sizeof(struct tcphdr));
432 ip->ip_src.s_addr = fin->fin_daddr;
433 ip->ip_dst.s_addr = fin->fin_saddr;
434 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
435 ip->ip_len = htons(hlen + sizeof(*tcp2));
436 return ipf_send_ip(fin, m);
441 * ip_len must be in network byte order when called.
452 ip = mtod(m, ip_t *);
453 bzero((char *)&fnew, sizeof(fnew));
454 fnew.fin_main_soft = fin->fin_main_soft;
456 IP_V_A(ip, fin->fin_v);
463 fnew.fin_p = ip->ip_p;
464 fnew.fin_plen = ntohs(ip->ip_len);
465 IP_HL_A(ip, sizeof(*oip) >> 2);
466 ip->ip_tos = oip->ip_tos;
467 ip->ip_id = fin->fin_ip->ip_id;
468 #if defined(FreeBSD) && (__FreeBSD_version > 460000)
469 ip->ip_off = htons(path_mtu_discovery ? IP_DF : 0);
473 ip->ip_ttl = V_ip_defttl;
479 ip6_t *ip6 = (ip6_t *)ip;
482 ip6->ip6_hlim = IPDEFTTL;
485 fnew.fin_p = ip6->ip6_nxt;
487 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
495 m->m_pkthdr.rcvif = NULL;
498 fnew.fin_ifp = fin->fin_ifp;
499 fnew.fin_flx = FI_NOCKSUM;
503 fnew.fin_hlen = hlen;
504 fnew.fin_dp = (char *)ip + hlen;
505 (void) ipf_makefrip(hlen, ip, &fnew);
507 return ipf_fastroute(m, &m, &fnew, NULL);
512 ipf_send_icmp_err(type, fin, dst)
517 int err, hlen, xtra, iclen, ohlen, avail, code;
528 if ((type < 0) || (type >= ICMP_MAXTYPE))
531 code = fin->fin_icode;
534 /* XXX Fix an off by one error: s/>/>=/
536 if ((code < 0) || (code > sizeof(icmptoicmp6unreach)/sizeof(int)))
537 Fix obtained from NetBSD ip_fil_netbsd.c r1.4: */
539 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
543 if (ipf_checkl4sum(fin) == -1)
546 MGETHDR(m, M_DONTWAIT, MT_HEADER);
548 MGET(m, M_DONTWAIT, MT_HEADER);
559 if (fin->fin_v == 4) {
560 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
561 switch (ntohs(fin->fin_data[0]) >> 8)
574 if (ipf_ifpaddr(&ipfmain, 4, FRI_NORMAL, ifp,
575 &dst6, NULL) == -1) {
581 dst4.s_addr = fin->fin_daddr;
584 ohlen = fin->fin_hlen;
585 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
586 if (fin->fin_hlen < fin->fin_plen)
587 xtra = MIN(fin->fin_dlen, 8);
593 else if (fin->fin_v == 6) {
594 hlen = sizeof(ip6_t);
595 ohlen = sizeof(ip6_t);
596 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
597 type = icmptoicmp6types[type];
598 if (type == ICMP6_DST_UNREACH)
599 code = icmptoicmp6unreach[code];
601 if (iclen + max_linkhdr + fin->fin_plen > avail) {
602 MCLGET(m, M_DONTWAIT);
603 if ((m->m_flags & M_EXT) == 0) {
609 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
610 xtra = MIN(xtra, IPV6_MMTU - iclen);
612 if (ipf_ifpaddr(&ipfmain, 6, FRI_NORMAL, ifp,
613 &dst6, NULL) == -1) {
618 dst6 = fin->fin_dst6;
626 avail -= (max_linkhdr + iclen);
634 m->m_data += max_linkhdr;
635 m->m_pkthdr.rcvif = (struct ifnet *)0;
636 m->m_pkthdr.len = iclen;
638 ip = mtod(m, ip_t *);
639 icmp = (struct icmp *)((char *)ip + hlen);
640 ip2 = (ip_t *)&icmp->icmp_ip;
642 icmp->icmp_type = type;
643 icmp->icmp_code = fin->fin_icode;
644 icmp->icmp_cksum = 0;
646 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
647 if (fin->fin_mtu != 0) {
648 icmp->icmp_nextmtu = htons(fin->fin_mtu);
650 } else if (ifp != NULL) {
651 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
653 } else { /* make up a number... */
654 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
659 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
663 if (fin->fin_v == 6) {
664 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
665 ip6->ip6_plen = htons(iclen - hlen);
666 ip6->ip6_nxt = IPPROTO_ICMPV6;
668 ip6->ip6_src = dst6.in6;
669 ip6->ip6_dst = fin->fin_src6.in6;
671 bcopy((char *)fin->fin_ip + ohlen,
672 (char *)&icmp->icmp_ip + ohlen, xtra);
673 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
674 sizeof(*ip6), iclen - hlen);
678 ip->ip_p = IPPROTO_ICMP;
679 ip->ip_src.s_addr = dst4.s_addr;
680 ip->ip_dst.s_addr = fin->fin_saddr;
683 bcopy((char *)fin->fin_ip + ohlen,
684 (char *)&icmp->icmp_ip + ohlen, xtra);
685 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
687 ip->ip_len = htons(iclen);
688 ip->ip_p = IPPROTO_ICMP;
690 err = ipf_send_ip(fin, m);
698 * m0 - pointer to mbuf where the IP packet starts
699 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
702 ipf_fastroute(m0, mpp, fin, fdp)
707 register struct ip *ip, *mhip;
708 register struct mbuf *m = *mpp;
709 register struct route *ro;
710 int len, off, error = 0, hlen, code;
711 struct ifnet *ifp, *sifp;
712 struct sockaddr_in *dst;
713 struct route iproute;
724 * If the mbuf we're about to send is not writable (because of
725 * a cluster reference, for example) we'll need to make a copy
726 * of it since this routine modifies the contents.
728 * If you have non-crappy network hardware that can transmit data
729 * from the mbuf, rather than making a copy, this is gonna be a
732 if (M_WRITABLE(m) == 0) {
733 m0 = m_dup(m, M_DONTWAIT);
747 if (fin->fin_v == 6) {
749 * currently "to <if>" and "to <if>:ip#" are not supported
752 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
756 hlen = fin->fin_hlen;
757 ip = mtod(m0, struct ip *);
764 bzero(ro, sizeof (*ro));
765 dst = (struct sockaddr_in *)&ro->ro_dst;
766 dst->sin_family = AF_INET;
767 dst->sin_addr = ip->ip_dst;
770 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
771 (fdp->fd_type == FRD_DSTLIST)) {
772 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
781 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
786 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
787 dst->sin_addr = fdp->fd_ip;
789 dst->sin_len = sizeof(*dst);
790 in_rtalloc(ro, M_GETFIB(m0));
792 if ((ifp == NULL) && (ro->ro_rt != NULL))
793 ifp = ro->ro_rt->rt_ifp;
795 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
796 if (in_localaddr(ip->ip_dst))
797 error = EHOSTUNREACH;
802 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
803 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
805 counter_u64_add(ro->ro_rt->rt_pksent, 1);
808 * For input packets which are being "fastrouted", they won't
809 * go back through output filtering and miss their chance to get
810 * NAT'd and counted. Duplicated packets aren't considered to be
811 * part of the normal packet stream, so do not NAT them or pass
812 * them through stateful checking, etc.
814 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
818 (void) ipf_acctpkt(fin, NULL);
820 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
823 (void) ipf_state_check(fin, &pass);
826 switch (ipf_nat_checkout(fin, NULL))
844 * If small enough for interface, can just send directly.
846 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
848 ip->ip_sum = in_cksum(m, hlen);
849 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
855 * Too large for interface; fragment if possible.
856 * Must be able to put at least 8 bytes per fragment.
858 ip_off = ntohs(ip->ip_off);
859 if (ip_off & IP_DF) {
863 len = (ifp->if_mtu - hlen) &~ 7;
870 int mhlen, firstlen = len;
871 struct mbuf **mnext = &m->m_act;
874 * Loop through length of segment after first fragment,
875 * make new header and copy data of each part and link onto chain.
878 mhlen = sizeof (struct ip);
879 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
881 MGETHDR(m, M_DONTWAIT, MT_HEADER);
883 MGET(m, M_DONTWAIT, MT_HEADER);
890 m->m_data += max_linkhdr;
891 mhip = mtod(m, struct ip *);
892 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
893 if (hlen > sizeof (struct ip)) {
894 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
895 IP_HL_A(mhip, mhlen >> 2);
898 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
899 if (off + len >= ntohs(ip->ip_len))
900 len = ntohs(ip->ip_len) - off;
902 mhip->ip_off |= IP_MF;
903 mhip->ip_len = htons((u_short)(len + mhlen));
905 m->m_next = m_copy(m0, off, len);
906 if (m->m_next == 0) {
907 error = ENOBUFS; /* ??? */
910 m->m_pkthdr.len = mhlen + len;
911 m->m_pkthdr.rcvif = NULL;
912 mhip->ip_off = htons((u_short)mhip->ip_off);
914 mhip->ip_sum = in_cksum(m, mhlen);
918 * Update first fragment by trimming what's been copied out
919 * and updating header, then send each fragment (in order).
921 m_adj(m0, hlen + firstlen - ip->ip_len);
922 ip->ip_len = htons((u_short)(hlen + firstlen));
923 ip->ip_off = htons((u_short)IP_MF);
925 ip->ip_sum = in_cksum(m0, hlen);
927 for (m = m0; m; m = m0) {
931 error = (*ifp->if_output)(ifp, m,
932 (struct sockaddr *)dst,
941 ipfmain.ipf_frouteok[0]++;
943 ipfmain.ipf_frouteok[1]++;
945 if ((ro != NULL) && (ro->ro_rt != NULL)) {
950 if (error == EMSGSIZE) {
952 code = fin->fin_icode;
953 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
955 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
957 fin->fin_icode = code;
968 struct sockaddr_in *dst;
969 struct route iproute;
971 bzero((char *)&iproute, sizeof(iproute));
972 dst = (struct sockaddr_in *)&iproute.ro_dst;
973 dst->sin_len = sizeof(*dst);
974 dst->sin_family = AF_INET;
975 dst->sin_addr = fin->fin_src;
976 in_rtalloc(&iproute, 0);
977 if (iproute.ro_rt == NULL)
979 return (fin->fin_ifp == iproute.ro_rt->rt_ifp);
984 * return the first IP Address associated with an interface
987 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
988 ipf_main_softc_t *softc;
991 i6addr_t *inp, *inpmask;
994 struct in6_addr *inp6 = NULL;
996 struct sockaddr *sock, *mask;
997 struct sockaddr_in *sin;
1001 if ((ifptr == NULL) || (ifptr == (void *)-1))
1008 inp->in4.s_addr = 0;
1011 bzero((char *)inp, sizeof(*inp));
1013 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1015 sock = ifa->ifa_addr;
1016 while (sock != NULL && ifa != NULL) {
1017 sin = (struct sockaddr_in *)sock;
1018 if ((v == 4) && (sin->sin_family == AF_INET))
1021 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1022 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1023 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1024 !IN6_IS_ADDR_LOOPBACK(inp6))
1028 ifa = TAILQ_NEXT(ifa, ifa_link);
1030 sock = ifa->ifa_addr;
1033 if (ifa == NULL || sin == NULL)
1036 mask = ifa->ifa_netmask;
1037 if (atype == FRI_BROADCAST)
1038 sock = ifa->ifa_broadaddr;
1039 else if (atype == FRI_PEERADDR)
1040 sock = ifa->ifa_dstaddr;
1047 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1048 (struct sockaddr_in6 *)mask,
1052 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1053 (struct sockaddr_in *)mask,
1054 &inp->in4, &inpmask->in4);
1063 newiss = arc4random();
1068 /* ------------------------------------------------------------------------ */
1069 /* Function: ipf_nextipid */
1070 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
1071 /* Parameters: fin(I) - pointer to packet information */
1073 /* Returns the next IPv4 ID to use for this packet. */
1074 /* ------------------------------------------------------------------------ */
1081 #ifndef RANDOM_IP_ID
1082 MUTEX_ENTER(&ipfmain.ipf_rw);
1084 MUTEX_EXIT(&ipfmain.ipf_rw);
1097 #ifdef CSUM_DATA_VALID
1103 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1106 if ((fin->fin_flx & FI_SHORT) != 0)
1109 if (fin->fin_cksum != FI_CK_NEEDED)
1110 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1119 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1121 fin->fin_cksum = FI_CK_BAD;
1122 fin->fin_flx |= FI_BAD;
1125 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1126 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1127 sum = m->m_pkthdr.csum_data;
1129 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1130 htonl(m->m_pkthdr.csum_data +
1131 fin->fin_dlen + fin->fin_p));
1134 fin->fin_cksum = FI_CK_BAD;
1135 fin->fin_flx |= FI_BAD;
1137 fin->fin_cksum = FI_CK_SUMOK;
1141 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1142 fin->fin_cksum = FI_CK_L4FULL;
1144 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1145 m->m_pkthdr.csum_flags == CSUM_UDP) {
1146 fin->fin_cksum = FI_CK_L4PART;
1148 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1149 fin->fin_cksum = FI_CK_L4PART;
1157 if (ipf_checkl4sum(fin) == -1) {
1158 fin->fin_flx |= FI_BAD;
1163 if (ipf_checkl4sum(fin) == -1) {
1164 fin->fin_flx |= FI_BAD;
1177 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1180 if ((fin->fin_flx & FI_SHORT) != 0)
1183 if (fin->fin_cksum != FI_CK_NEEDED)
1184 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1186 if (ipf_checkl4sum(fin) == -1) {
1187 fin->fin_flx |= FI_BAD;
1192 #endif /* USE_INET6 */
1201 if ((m0->m_flags & M_PKTHDR) != 0) {
1202 len = m0->m_pkthdr.len;
1206 for (m = m0, len = 0; m != NULL; m = m->m_next)
1213 /* ------------------------------------------------------------------------ */
1214 /* Function: ipf_pullup */
1215 /* Returns: NULL == pullup failed, else pointer to protocol header */
1216 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1217 /* fin(I) - pointer to packet information */
1218 /* len(I) - number of bytes to pullup */
1220 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1221 /* single buffer for ease of access. Operating system native functions are */
1222 /* used to manage buffers - if necessary. If the entire packet ends up in */
1223 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1224 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1225 /* and ONLY if the pullup succeeds. */
1227 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1228 /* of buffers that starts at *fin->fin_mp. */
1229 /* ------------------------------------------------------------------------ */
1231 ipf_pullup(xmin, fin, len)
1243 ip = (char *)fin->fin_ip;
1244 if ((fin->fin_flx & FI_COALESCE) != 0)
1247 ipoff = fin->fin_ipoff;
1248 if (fin->fin_dp != NULL)
1249 dpoff = (char *)fin->fin_dp - (char *)ip;
1253 if (M_LEN(m) < len) {
1254 mb_t *n = *fin->fin_mp;
1256 * Assume that M_PKTHDR is set and just work with what is left
1257 * rather than check..
1258 * Should not make any real difference, anyway.
1262 * Record the mbuf that points to the mbuf that we're
1263 * about to go to work on so that we can update the
1264 * m_next appropriately later.
1266 for (; n->m_next != m; n = n->m_next)
1278 #ifdef HAVE_M_PULLDOWN
1279 if (m_pulldown(m, 0, len, NULL) == NULL)
1282 FREE_MB_T(*fin->fin_mp);
1288 m = m_pullup(m, len);
1294 * When n is non-NULL, it indicates that m pointed to
1295 * a sub-chain (tail) of the mbuf and that the head
1296 * of this chain has not yet been free'd.
1299 FREE_MB_T(*fin->fin_mp);
1302 *fin->fin_mp = NULL;
1310 while (M_LEN(m) == 0) {
1314 ip = MTOD(m, char *) + ipoff;
1316 fin->fin_ip = (ip_t *)ip;
1317 if (fin->fin_dp != NULL)
1318 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1319 if (fin->fin_fraghdr != NULL)
1320 fin->fin_fraghdr = (char *)ip +
1321 ((char *)fin->fin_fraghdr -
1322 (char *)fin->fin_ip);
1325 if (len == fin->fin_plen)
1326 fin->fin_flx |= FI_COALESCE;
1338 if (fin->fin_out == 0) {
1339 netisr_dispatch(NETISR_IP, m);
1341 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1342 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1343 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1349 int ipf_pfil_unhook(void) {
1350 #if defined(NETBSD_PF) && (__FreeBSD_version >= 500011)
1351 struct pfil_head *ph_inet;
1353 struct pfil_head *ph_inet6;
1358 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1359 if (ph_inet != NULL)
1360 pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1361 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1363 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1364 if (ph_inet6 != NULL)
1365 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1366 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1373 int ipf_pfil_hook(void) {
1374 #if defined(NETBSD_PF) && (__FreeBSD_version >= 500011)
1375 struct pfil_head *ph_inet;
1377 struct pfil_head *ph_inet6;
1382 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1384 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1394 if (ph_inet != NULL)
1395 pfil_add_hook((void *)ipf_check_wrapper, NULL,
1396 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1398 if (ph_inet6 != NULL)
1399 pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1400 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1409 ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1410 ipf_ifevent, &ipfmain, \
1411 EVENTHANDLER_PRI_ANY);
1412 ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1413 ipf_ifevent, &ipfmain, \
1414 EVENTHANDLER_PRI_ANY);
1415 ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1416 &ipfmain, EVENTHANDLER_PRI_ANY);
1420 ipf_event_dereg(void)
1422 if (ipf_arrivetag != NULL) {
1423 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag);
1425 if (ipf_departtag != NULL) {
1426 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag);
1428 if (ipf_clonetag != NULL) {
1429 EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag);
1437 return arc4random();
1442 ipf_pcksum(fin, hlen, sum)
1452 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1455 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1460 * Both sum and sum2 are partial sums, so combine them together.
1462 sum += ~sum2 & 0xffff;
1463 while (sum > 0xffff)
1464 sum = (sum & 0xffff) + (sum >> 16);
1465 sum2 = ~sum & 0xffff;