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>
41 # include <sys/mbuf.h>
42 #include <sys/socket.h>
43 # include <sys/selinfo.h>
44 # include <netinet/tcp_var.h>
47 # include <net/if_var.h>
48 # include <net/netisr.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/tcp.h>
56 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000)
59 #define CURVNET_SET(arg)
60 #define CURVNET_RESTORE()
62 #include <netinet/udp.h>
63 #include <netinet/tcpip.h>
64 #include <netinet/ip_icmp.h>
65 #include "netinet/ip_compat.h"
67 # include <netinet/icmp6.h>
69 #include "netinet/ip_fil.h"
70 #include "netinet/ip_nat.h"
71 #include "netinet/ip_frag.h"
72 #include "netinet/ip_state.h"
73 #include "netinet/ip_proxy.h"
74 #include "netinet/ip_auth.h"
75 #include "netinet/ip_sync.h"
76 #include "netinet/ip_lookup.h"
77 #include "netinet/ip_dstlist.h"
79 #include "netinet/ip_scan.h"
81 #include "netinet/ip_pool.h"
82 # include <sys/malloc.h>
83 #include <sys/kernel.h>
84 #ifdef CSUM_DATA_VALID
85 #include <machine/in_cksum.h>
87 extern int ip_optcopy __P((struct ip *, struct ip *));
89 # ifdef IPFILTER_M_IPFILTER
90 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
94 static u_short ipid = 0;
95 static int (*ipf_savep) __P((void *, ip_t *, int, void *, int, struct mbuf **));
96 static int ipf_send_ip __P((fr_info_t *, mb_t *));
97 static void ipf_timer_func __P((void *arg));
98 int ipf_locks_done = 0;
100 ipf_main_softc_t ipfmain;
102 # include <sys/conf.h>
103 # include <net/pfil.h>
105 * We provide the ipf_checkp name just to minimize changes later.
107 int (*ipf_checkp) __P((void *, ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
110 static eventhandler_tag ipf_arrivetag, ipf_departtag, ipf_clonetag;
112 static void ipf_ifevent(void *arg);
114 static void ipf_ifevent(arg)
123 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
125 struct ip *ip = mtod(*mp, struct ip *);
129 * IPFilter expects evreything in network byte order
131 #if (__FreeBSD_version < 1000019)
132 ip->ip_len = htons(ip->ip_len);
133 ip->ip_off = htons(ip->ip_off);
135 rv = ipf_check(&ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
137 #if (__FreeBSD_version < 1000019)
138 if ((rv == 0) && (*mp != NULL)) {
139 ip = mtod(*mp, struct ip *);
140 ip->ip_len = ntohs(ip->ip_len);
141 ip->ip_off = ntohs(ip->ip_off);
148 # include <netinet/ip6.h>
151 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
153 return (ipf_check(&ipfmain, mtod(*mp, struct ip *),
154 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp));
157 #if defined(IPFILTER_LKM)
161 if (strcmp(s, "ipl") == 0)
165 #endif /* IPFILTER_LKM */
172 ipf_main_softc_t *softc = arg;
176 READ_ENTER(&softc->ipf_global);
178 if (softc->ipf_running > 0)
179 ipf_slowtimer(softc);
181 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
183 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
185 callout_init(&softc->ipf_slow_ch, 1);
186 callout_reset(&softc->ipf_slow_ch,
187 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
188 ipf_timer_func, softc);
190 RWLOCK_EXIT(&softc->ipf_global);
197 ipf_main_softc_t *softc;
204 if (softc->ipf_running > 0) {
209 if (ipf_init_all(softc) < 0) {
215 if (ipf_checkp != ipf_check) {
216 ipf_savep = ipf_checkp;
217 ipf_checkp = ipf_check;
220 bzero((char *)ipfmain.ipf_selwait, sizeof(ipfmain.ipf_selwait));
221 softc->ipf_running = 1;
223 if (softc->ipf_control_forwarding & 1)
230 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
231 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
233 callout_init(&softc->ipf_slow_ch, 1);
234 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
235 ipf_timer_func, softc);
241 * Disable the filter by removing the hooks from the IP input/output
246 ipf_main_softc_t *softc;
252 if (softc->ipf_control_forwarding & 2)
258 if (softc->ipf_slow_ch.callout != NULL)
259 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
260 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
262 callout_drain(&softc->ipf_slow_ch);
265 if (ipf_checkp != NULL)
266 ipf_checkp = ipf_savep;
272 softc->ipf_running = -2;
281 * Filter ioctl interface.
284 ipfioctl(dev, cmd, data, mode, p)
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 CURVNET_SET(TD_TO_VNET(p));
327 error = ipf_ioctlswitch(&ipfmain, unit, data, cmd, mode, p->p_uid, p);
341 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
342 * requires a large amount of setting up and isn't any more efficient.
348 struct tcphdr *tcp, *tcp2;
357 if (tcp->th_flags & TH_RST)
358 return -1; /* feedback loop */
360 if (ipf_checkl4sum(fin) == -1)
363 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
364 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
365 ((tcp->th_flags & TH_FIN) ? 1 : 0);
368 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
373 MGETHDR(m, M_DONTWAIT, MT_HEADER);
375 MGET(m, M_DONTWAIT, MT_HEADER);
379 if (sizeof(*tcp2) + hlen > MLEN) {
380 MCLGET(m, M_DONTWAIT);
381 if ((m->m_flags & M_EXT) == 0) {
387 m->m_len = sizeof(*tcp2) + hlen;
389 m->m_data += max_linkhdr;
390 m->m_pkthdr.len = m->m_len;
391 m->m_pkthdr.rcvif = (struct ifnet *)0;
393 ip = mtod(m, struct ip *);
394 bzero((char *)ip, hlen);
398 tcp2 = (struct tcphdr *)((char *)ip + hlen);
399 tcp2->th_sport = tcp->th_dport;
400 tcp2->th_dport = tcp->th_sport;
402 if (tcp->th_flags & TH_ACK) {
403 tcp2->th_seq = tcp->th_ack;
404 tcp2->th_flags = TH_RST;
408 tcp2->th_ack = ntohl(tcp->th_seq);
409 tcp2->th_ack += tlen;
410 tcp2->th_ack = htonl(tcp2->th_ack);
411 tcp2->th_flags = TH_RST|TH_ACK;
414 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
415 tcp2->th_win = tcp->th_win;
420 if (fin->fin_v == 6) {
421 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
422 ip6->ip6_plen = htons(sizeof(struct tcphdr));
423 ip6->ip6_nxt = IPPROTO_TCP;
425 ip6->ip6_src = fin->fin_dst6.in6;
426 ip6->ip6_dst = fin->fin_src6.in6;
427 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
428 sizeof(*ip6), sizeof(*tcp2));
429 return ipf_send_ip(fin, m);
432 ip->ip_p = IPPROTO_TCP;
433 ip->ip_len = htons(sizeof(struct tcphdr));
434 ip->ip_src.s_addr = fin->fin_daddr;
435 ip->ip_dst.s_addr = fin->fin_saddr;
436 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
437 ip->ip_len = htons(hlen + sizeof(*tcp2));
438 return ipf_send_ip(fin, m);
443 * ip_len must be in network byte order when called.
454 ip = mtod(m, ip_t *);
455 bzero((char *)&fnew, sizeof(fnew));
456 fnew.fin_main_soft = fin->fin_main_soft;
458 IP_V_A(ip, fin->fin_v);
465 fnew.fin_p = ip->ip_p;
466 fnew.fin_plen = ntohs(ip->ip_len);
467 IP_HL_A(ip, sizeof(*oip) >> 2);
468 ip->ip_tos = oip->ip_tos;
469 ip->ip_id = fin->fin_ip->ip_id;
470 ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
471 ip->ip_ttl = V_ip_defttl;
477 ip6_t *ip6 = (ip6_t *)ip;
480 ip6->ip6_hlim = IPDEFTTL;
483 fnew.fin_p = ip6->ip6_nxt;
485 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
493 m->m_pkthdr.rcvif = NULL;
496 fnew.fin_ifp = fin->fin_ifp;
497 fnew.fin_flx = FI_NOCKSUM;
501 fnew.fin_hlen = hlen;
502 fnew.fin_dp = (char *)ip + hlen;
503 (void) ipf_makefrip(hlen, ip, &fnew);
505 return ipf_fastroute(m, &m, &fnew, NULL);
510 ipf_send_icmp_err(type, fin, dst)
515 int err, hlen, xtra, iclen, ohlen, avail, code;
526 if ((type < 0) || (type >= ICMP_MAXTYPE))
529 code = fin->fin_icode;
531 /* See NetBSD ip_fil_netbsd.c r1.4: */
532 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
536 if (ipf_checkl4sum(fin) == -1)
539 MGETHDR(m, M_DONTWAIT, MT_HEADER);
541 MGET(m, M_DONTWAIT, MT_HEADER);
552 if (fin->fin_v == 4) {
553 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
554 switch (ntohs(fin->fin_data[0]) >> 8)
567 if (ipf_ifpaddr(&ipfmain, 4, FRI_NORMAL, ifp,
568 &dst6, NULL) == -1) {
574 dst4.s_addr = fin->fin_daddr;
577 ohlen = fin->fin_hlen;
578 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
579 if (fin->fin_hlen < fin->fin_plen)
580 xtra = MIN(fin->fin_dlen, 8);
586 else if (fin->fin_v == 6) {
587 hlen = sizeof(ip6_t);
588 ohlen = sizeof(ip6_t);
589 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
590 type = icmptoicmp6types[type];
591 if (type == ICMP6_DST_UNREACH)
592 code = icmptoicmp6unreach[code];
594 if (iclen + max_linkhdr + fin->fin_plen > avail) {
595 MCLGET(m, M_DONTWAIT);
596 if ((m->m_flags & M_EXT) == 0) {
602 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
603 xtra = MIN(xtra, IPV6_MMTU - iclen);
605 if (ipf_ifpaddr(&ipfmain, 6, FRI_NORMAL, ifp,
606 &dst6, NULL) == -1) {
611 dst6 = fin->fin_dst6;
619 avail -= (max_linkhdr + iclen);
627 m->m_data += max_linkhdr;
628 m->m_pkthdr.rcvif = (struct ifnet *)0;
629 m->m_pkthdr.len = iclen;
631 ip = mtod(m, ip_t *);
632 icmp = (struct icmp *)((char *)ip + hlen);
633 ip2 = (ip_t *)&icmp->icmp_ip;
635 icmp->icmp_type = type;
636 icmp->icmp_code = fin->fin_icode;
637 icmp->icmp_cksum = 0;
639 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
640 if (fin->fin_mtu != 0) {
641 icmp->icmp_nextmtu = htons(fin->fin_mtu);
643 } else if (ifp != NULL) {
644 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
646 } else { /* make up a number... */
647 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
652 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
656 if (fin->fin_v == 6) {
657 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
658 ip6->ip6_plen = htons(iclen - hlen);
659 ip6->ip6_nxt = IPPROTO_ICMPV6;
661 ip6->ip6_src = dst6.in6;
662 ip6->ip6_dst = fin->fin_src6.in6;
664 bcopy((char *)fin->fin_ip + ohlen,
665 (char *)&icmp->icmp_ip + ohlen, xtra);
666 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
667 sizeof(*ip6), iclen - hlen);
671 ip->ip_p = IPPROTO_ICMP;
672 ip->ip_src.s_addr = dst4.s_addr;
673 ip->ip_dst.s_addr = fin->fin_saddr;
676 bcopy((char *)fin->fin_ip + ohlen,
677 (char *)&icmp->icmp_ip + ohlen, xtra);
678 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
680 ip->ip_len = htons(iclen);
681 ip->ip_p = IPPROTO_ICMP;
683 err = ipf_send_ip(fin, m);
691 * m0 - pointer to mbuf where the IP packet starts
692 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
695 ipf_fastroute(m0, mpp, fin, fdp)
700 register struct ip *ip, *mhip;
701 register struct mbuf *m = *mpp;
702 register struct route *ro;
703 int len, off, error = 0, hlen, code;
704 struct ifnet *ifp, *sifp;
705 struct sockaddr_in *dst;
706 struct route iproute;
717 * If the mbuf we're about to send is not writable (because of
718 * a cluster reference, for example) we'll need to make a copy
719 * of it since this routine modifies the contents.
721 * If you have non-crappy network hardware that can transmit data
722 * from the mbuf, rather than making a copy, this is gonna be a
725 if (M_WRITABLE(m) == 0) {
726 m0 = m_dup(m, M_DONTWAIT);
740 if (fin->fin_v == 6) {
742 * currently "to <if>" and "to <if>:ip#" are not supported
745 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
749 hlen = fin->fin_hlen;
750 ip = mtod(m0, struct ip *);
757 bzero(ro, sizeof (*ro));
758 dst = (struct sockaddr_in *)&ro->ro_dst;
759 dst->sin_family = AF_INET;
760 dst->sin_addr = ip->ip_dst;
763 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
764 (fdp->fd_type == FRD_DSTLIST)) {
765 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
774 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
779 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
780 dst->sin_addr = fdp->fd_ip;
782 dst->sin_len = sizeof(*dst);
783 in_rtalloc(ro, M_GETFIB(m0));
785 if ((ifp == NULL) && (ro->ro_rt != NULL))
786 ifp = ro->ro_rt->rt_ifp;
788 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
789 if (in_localaddr(ip->ip_dst))
790 error = EHOSTUNREACH;
795 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
796 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
798 counter_u64_add(ro->ro_rt->rt_pksent, 1);
801 * For input packets which are being "fastrouted", they won't
802 * go back through output filtering and miss their chance to get
803 * NAT'd and counted. Duplicated packets aren't considered to be
804 * part of the normal packet stream, so do not NAT them or pass
805 * them through stateful checking, etc.
807 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
811 (void) ipf_acctpkt(fin, NULL);
813 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
816 (void) ipf_state_check(fin, &pass);
819 switch (ipf_nat_checkout(fin, NULL))
837 * If small enough for interface, can just send directly.
839 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
841 ip->ip_sum = in_cksum(m, hlen);
842 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
848 * Too large for interface; fragment if possible.
849 * Must be able to put at least 8 bytes per fragment.
851 ip_off = ntohs(ip->ip_off);
852 if (ip_off & IP_DF) {
856 len = (ifp->if_mtu - hlen) &~ 7;
863 int mhlen, firstlen = len;
864 struct mbuf **mnext = &m->m_act;
867 * Loop through length of segment after first fragment,
868 * make new header and copy data of each part and link onto chain.
871 mhlen = sizeof (struct ip);
872 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
874 MGETHDR(m, M_DONTWAIT, MT_HEADER);
876 MGET(m, M_DONTWAIT, MT_HEADER);
883 m->m_data += max_linkhdr;
884 mhip = mtod(m, struct ip *);
885 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
886 if (hlen > sizeof (struct ip)) {
887 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
888 IP_HL_A(mhip, mhlen >> 2);
891 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
892 if (off + len >= ntohs(ip->ip_len))
893 len = ntohs(ip->ip_len) - off;
895 mhip->ip_off |= IP_MF;
896 mhip->ip_len = htons((u_short)(len + mhlen));
898 m->m_next = m_copy(m0, off, len);
899 if (m->m_next == 0) {
900 error = ENOBUFS; /* ??? */
903 m->m_pkthdr.len = mhlen + len;
904 m->m_pkthdr.rcvif = NULL;
905 mhip->ip_off = htons((u_short)mhip->ip_off);
907 mhip->ip_sum = in_cksum(m, mhlen);
911 * Update first fragment by trimming what's been copied out
912 * and updating header, then send each fragment (in order).
914 m_adj(m0, hlen + firstlen - ip->ip_len);
915 ip->ip_len = htons((u_short)(hlen + firstlen));
916 ip->ip_off = htons((u_short)IP_MF);
918 ip->ip_sum = in_cksum(m0, hlen);
920 for (m = m0; m; m = m0) {
924 error = (*ifp->if_output)(ifp, m,
925 (struct sockaddr *)dst,
934 ipfmain.ipf_frouteok[0]++;
936 ipfmain.ipf_frouteok[1]++;
938 if ((ro != NULL) && (ro->ro_rt != NULL)) {
943 if (error == EMSGSIZE) {
945 code = fin->fin_icode;
946 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
948 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
950 fin->fin_icode = code;
961 struct sockaddr_in *dst;
962 struct route iproute;
964 bzero((char *)&iproute, sizeof(iproute));
965 dst = (struct sockaddr_in *)&iproute.ro_dst;
966 dst->sin_len = sizeof(*dst);
967 dst->sin_family = AF_INET;
968 dst->sin_addr = fin->fin_src;
969 in_rtalloc(&iproute, 0);
970 if (iproute.ro_rt == NULL)
972 return (fin->fin_ifp == iproute.ro_rt->rt_ifp);
977 * return the first IP Address associated with an interface
980 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
981 ipf_main_softc_t *softc;
984 i6addr_t *inp, *inpmask;
987 struct in6_addr *inp6 = NULL;
989 struct sockaddr *sock, *mask;
990 struct sockaddr_in *sin;
994 if ((ifptr == NULL) || (ifptr == (void *)-1))
1001 inp->in4.s_addr = 0;
1004 bzero((char *)inp, sizeof(*inp));
1006 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1008 sock = ifa->ifa_addr;
1009 while (sock != NULL && ifa != NULL) {
1010 sin = (struct sockaddr_in *)sock;
1011 if ((v == 4) && (sin->sin_family == AF_INET))
1014 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1015 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1016 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1017 !IN6_IS_ADDR_LOOPBACK(inp6))
1021 ifa = TAILQ_NEXT(ifa, ifa_link);
1023 sock = ifa->ifa_addr;
1026 if (ifa == NULL || sin == NULL)
1029 mask = ifa->ifa_netmask;
1030 if (atype == FRI_BROADCAST)
1031 sock = ifa->ifa_broadaddr;
1032 else if (atype == FRI_PEERADDR)
1033 sock = ifa->ifa_dstaddr;
1040 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1041 (struct sockaddr_in6 *)mask,
1045 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1046 (struct sockaddr_in *)mask,
1047 &inp->in4, &inpmask->in4);
1056 newiss = arc4random();
1061 /* ------------------------------------------------------------------------ */
1062 /* Function: ipf_nextipid */
1063 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
1064 /* Parameters: fin(I) - pointer to packet information */
1066 /* Returns the next IPv4 ID to use for this packet. */
1067 /* ------------------------------------------------------------------------ */
1074 #ifndef RANDOM_IP_ID
1075 MUTEX_ENTER(&ipfmain.ipf_rw);
1077 MUTEX_EXIT(&ipfmain.ipf_rw);
1090 #ifdef CSUM_DATA_VALID
1096 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1099 if ((fin->fin_flx & FI_SHORT) != 0)
1102 if (fin->fin_cksum != FI_CK_NEEDED)
1103 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1112 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1114 fin->fin_cksum = FI_CK_BAD;
1115 fin->fin_flx |= FI_BAD;
1118 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1119 /* Depending on the driver, UDP may have zero checksum */
1120 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1121 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1122 udphdr_t *udp = fin->fin_dp;
1123 if (udp->uh_sum == 0) {
1125 * we're good no matter what the hardware
1126 * checksum flags and csum_data say (handling
1127 * of csum_data for zero UDP checksum is not
1128 * consistent across all drivers)
1135 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1136 sum = m->m_pkthdr.csum_data;
1138 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1139 htonl(m->m_pkthdr.csum_data +
1140 fin->fin_dlen + fin->fin_p));
1143 fin->fin_cksum = FI_CK_BAD;
1144 fin->fin_flx |= FI_BAD;
1146 fin->fin_cksum = FI_CK_SUMOK;
1150 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1151 fin->fin_cksum = FI_CK_L4FULL;
1153 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1154 m->m_pkthdr.csum_flags == CSUM_UDP) {
1155 fin->fin_cksum = FI_CK_L4PART;
1157 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1158 fin->fin_cksum = FI_CK_L4PART;
1166 if (ipf_checkl4sum(fin) == -1) {
1167 fin->fin_flx |= FI_BAD;
1172 if (ipf_checkl4sum(fin) == -1) {
1173 fin->fin_flx |= FI_BAD;
1186 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1189 if ((fin->fin_flx & FI_SHORT) != 0)
1192 if (fin->fin_cksum != FI_CK_NEEDED)
1193 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1195 if (ipf_checkl4sum(fin) == -1) {
1196 fin->fin_flx |= FI_BAD;
1201 #endif /* USE_INET6 */
1210 if ((m0->m_flags & M_PKTHDR) != 0) {
1211 len = m0->m_pkthdr.len;
1215 for (m = m0, len = 0; m != NULL; m = m->m_next)
1222 /* ------------------------------------------------------------------------ */
1223 /* Function: ipf_pullup */
1224 /* Returns: NULL == pullup failed, else pointer to protocol header */
1225 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1226 /* fin(I) - pointer to packet information */
1227 /* len(I) - number of bytes to pullup */
1229 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1230 /* single buffer for ease of access. Operating system native functions are */
1231 /* used to manage buffers - if necessary. If the entire packet ends up in */
1232 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1233 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1234 /* and ONLY if the pullup succeeds. */
1236 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1237 /* of buffers that starts at *fin->fin_mp. */
1238 /* ------------------------------------------------------------------------ */
1240 ipf_pullup(xmin, fin, len)
1252 ip = (char *)fin->fin_ip;
1253 if ((fin->fin_flx & FI_COALESCE) != 0)
1256 ipoff = fin->fin_ipoff;
1257 if (fin->fin_dp != NULL)
1258 dpoff = (char *)fin->fin_dp - (char *)ip;
1262 if (M_LEN(m) < len) {
1263 mb_t *n = *fin->fin_mp;
1265 * Assume that M_PKTHDR is set and just work with what is left
1266 * rather than check..
1267 * Should not make any real difference, anyway.
1271 * Record the mbuf that points to the mbuf that we're
1272 * about to go to work on so that we can update the
1273 * m_next appropriately later.
1275 for (; n->m_next != m; n = n->m_next)
1287 #ifdef HAVE_M_PULLDOWN
1288 if (m_pulldown(m, 0, len, NULL) == NULL)
1291 FREE_MB_T(*fin->fin_mp);
1297 m = m_pullup(m, len);
1303 * When n is non-NULL, it indicates that m pointed to
1304 * a sub-chain (tail) of the mbuf and that the head
1305 * of this chain has not yet been free'd.
1308 FREE_MB_T(*fin->fin_mp);
1311 *fin->fin_mp = NULL;
1319 while (M_LEN(m) == 0) {
1323 ip = MTOD(m, char *) + ipoff;
1325 fin->fin_ip = (ip_t *)ip;
1326 if (fin->fin_dp != NULL)
1327 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1328 if (fin->fin_fraghdr != NULL)
1329 fin->fin_fraghdr = (char *)ip +
1330 ((char *)fin->fin_fraghdr -
1331 (char *)fin->fin_ip);
1334 if (len == fin->fin_plen)
1335 fin->fin_flx |= FI_COALESCE;
1347 if (fin->fin_out == 0) {
1348 netisr_dispatch(NETISR_IP, m);
1350 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1351 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1352 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1358 int ipf_pfil_unhook(void) {
1359 struct pfil_head *ph_inet;
1361 struct pfil_head *ph_inet6;
1364 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1365 if (ph_inet != NULL)
1366 pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1367 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1369 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1370 if (ph_inet6 != NULL)
1371 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1372 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1378 int ipf_pfil_hook(void) {
1379 struct pfil_head *ph_inet;
1381 struct pfil_head *ph_inet6;
1384 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1386 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1396 if (ph_inet != NULL)
1397 pfil_add_hook((void *)ipf_check_wrapper, NULL,
1398 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1400 if (ph_inet6 != NULL)
1401 pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1402 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1410 ipf_arrivetag = EVENTHANDLER_REGISTER(ifnet_arrival_event, \
1411 ipf_ifevent, &ipfmain, \
1412 EVENTHANDLER_PRI_ANY);
1413 ipf_departtag = EVENTHANDLER_REGISTER(ifnet_departure_event, \
1414 ipf_ifevent, &ipfmain, \
1415 EVENTHANDLER_PRI_ANY);
1416 ipf_clonetag = EVENTHANDLER_REGISTER(if_clone_event, ipf_ifevent, \
1417 &ipfmain, EVENTHANDLER_PRI_ANY);
1421 ipf_event_dereg(void)
1423 if (ipf_arrivetag != NULL) {
1424 EVENTHANDLER_DEREGISTER(ifnet_arrival_event, ipf_arrivetag);
1426 if (ipf_departtag != NULL) {
1427 EVENTHANDLER_DEREGISTER(ifnet_departure_event, ipf_departtag);
1429 if (ipf_clonetag != NULL) {
1430 EVENTHANDLER_DEREGISTER(if_clone_event, ipf_clonetag);
1438 return arc4random();
1443 ipf_pcksum(fin, hlen, sum)
1453 off = (char *)fin->fin_dp - (char *)fin->fin_ip;
1456 sum2 = in_cksum(fin->fin_m, fin->fin_plen - off);
1461 * Both sum and sum2 are partial sums, so combine them together.
1463 sum += ~sum2 & 0xffff;
1464 while (sum > 0xffff)
1465 sum = (sum & 0xffff) + (sum >> 16);
1466 sum2 = ~sum & 0xffff;