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/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>
55 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 800000)
58 #define CURVNET_SET(arg)
59 #define CURVNET_RESTORE()
61 #include <netinet/udp.h>
62 #include <netinet/tcpip.h>
63 #include <netinet/ip_icmp.h>
64 #include "netinet/ip_compat.h"
66 # include <netinet/icmp6.h>
68 #include "netinet/ip_fil.h"
69 #include "netinet/ip_nat.h"
70 #include "netinet/ip_frag.h"
71 #include "netinet/ip_state.h"
72 #include "netinet/ip_proxy.h"
73 #include "netinet/ip_auth.h"
74 #include "netinet/ip_sync.h"
75 #include "netinet/ip_lookup.h"
76 #include "netinet/ip_dstlist.h"
78 #include "netinet/ip_scan.h"
80 #include "netinet/ip_pool.h"
81 # include <sys/malloc.h>
82 #include <sys/kernel.h>
83 #ifdef CSUM_DATA_VALID
84 #include <machine/in_cksum.h>
86 extern int ip_optcopy __P((struct ip *, struct ip *));
88 # ifdef IPFILTER_M_IPFILTER
89 MALLOC_DEFINE(M_IPFILTER, "ipfilter", "IP Filter packet filter data structures");
93 static u_short ipid = 0;
94 static int (*ipf_savep) __P((void *, ip_t *, int, void *, int, struct mbuf **));
95 static int ipf_send_ip __P((fr_info_t *, mb_t *));
96 static void ipf_timer_func __P((void *arg));
97 int ipf_locks_done = 0;
99 ipf_main_softc_t ipfmain;
101 # include <sys/conf.h>
102 # include <net/pfil.h>
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, 1);
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, 1);
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, p)
285 # define p_cred td_ucred
286 # define p_uid td_ucred->cr_ruid
292 int error = 0, unit = 0;
296 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
298 ipfmain.ipf_interror = 130001;
303 unit = GET_MINOR(dev);
304 if ((IPL_LOGMAX < unit) || (unit < 0)) {
305 ipfmain.ipf_interror = 130002;
309 if (ipfmain.ipf_running <= 0) {
310 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
311 ipfmain.ipf_interror = 130003;
314 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
315 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
316 cmd != SIOCGETFS && cmd != SIOCGETFF &&
317 cmd != SIOCIPFINTERROR) {
318 ipfmain.ipf_interror = 130004;
325 CURVNET_SET(TD_TO_VNET(p));
326 error = ipf_ioctlswitch(&ipfmain, unit, data, cmd, mode, p->p_uid, p);
340 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
341 * requires a large amount of setting up and isn't any more efficient.
347 struct tcphdr *tcp, *tcp2;
356 if (tcp->th_flags & TH_RST)
357 return -1; /* feedback loop */
359 if (ipf_checkl4sum(fin) == -1)
362 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
363 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
364 ((tcp->th_flags & TH_FIN) ? 1 : 0);
367 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
372 MGETHDR(m, M_DONTWAIT, MT_HEADER);
374 MGET(m, M_DONTWAIT, MT_HEADER);
378 if (sizeof(*tcp2) + hlen > MLEN) {
379 MCLGET(m, M_DONTWAIT);
380 if ((m->m_flags & M_EXT) == 0) {
386 m->m_len = sizeof(*tcp2) + hlen;
388 m->m_data += max_linkhdr;
389 m->m_pkthdr.len = m->m_len;
390 m->m_pkthdr.rcvif = (struct ifnet *)0;
392 ip = mtod(m, struct ip *);
393 bzero((char *)ip, hlen);
397 tcp2 = (struct tcphdr *)((char *)ip + hlen);
398 tcp2->th_sport = tcp->th_dport;
399 tcp2->th_dport = tcp->th_sport;
401 if (tcp->th_flags & TH_ACK) {
402 tcp2->th_seq = tcp->th_ack;
403 tcp2->th_flags = TH_RST;
407 tcp2->th_ack = ntohl(tcp->th_seq);
408 tcp2->th_ack += tlen;
409 tcp2->th_ack = htonl(tcp2->th_ack);
410 tcp2->th_flags = TH_RST|TH_ACK;
413 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
414 tcp2->th_win = tcp->th_win;
419 if (fin->fin_v == 6) {
420 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
421 ip6->ip6_plen = htons(sizeof(struct tcphdr));
422 ip6->ip6_nxt = IPPROTO_TCP;
424 ip6->ip6_src = fin->fin_dst6.in6;
425 ip6->ip6_dst = fin->fin_src6.in6;
426 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
427 sizeof(*ip6), sizeof(*tcp2));
428 return ipf_send_ip(fin, m);
431 ip->ip_p = IPPROTO_TCP;
432 ip->ip_len = htons(sizeof(struct tcphdr));
433 ip->ip_src.s_addr = fin->fin_daddr;
434 ip->ip_dst.s_addr = fin->fin_saddr;
435 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
436 ip->ip_len = htons(hlen + sizeof(*tcp2));
437 return ipf_send_ip(fin, m);
442 * ip_len must be in network byte order when called.
453 ip = mtod(m, ip_t *);
454 bzero((char *)&fnew, sizeof(fnew));
455 fnew.fin_main_soft = fin->fin_main_soft;
457 IP_V_A(ip, fin->fin_v);
464 fnew.fin_p = ip->ip_p;
465 fnew.fin_plen = ntohs(ip->ip_len);
466 IP_HL_A(ip, sizeof(*oip) >> 2);
467 ip->ip_tos = oip->ip_tos;
468 ip->ip_id = fin->fin_ip->ip_id;
469 ip->ip_off = htons(V_path_mtu_discovery ? IP_DF : 0);
470 ip->ip_ttl = V_ip_defttl;
476 ip6_t *ip6 = (ip6_t *)ip;
479 ip6->ip6_hlim = IPDEFTTL;
482 fnew.fin_p = ip6->ip6_nxt;
484 fnew.fin_plen = ntohs(ip6->ip6_plen) + hlen;
492 m->m_pkthdr.rcvif = NULL;
495 fnew.fin_ifp = fin->fin_ifp;
496 fnew.fin_flx = FI_NOCKSUM;
500 fnew.fin_hlen = hlen;
501 fnew.fin_dp = (char *)ip + hlen;
502 (void) ipf_makefrip(hlen, ip, &fnew);
504 return ipf_fastroute(m, &m, &fnew, NULL);
509 ipf_send_icmp_err(type, fin, dst)
514 int err, hlen, xtra, iclen, ohlen, avail, code;
525 if ((type < 0) || (type >= ICMP_MAXTYPE))
528 code = fin->fin_icode;
530 /* See NetBSD ip_fil_netbsd.c r1.4: */
531 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
535 if (ipf_checkl4sum(fin) == -1)
538 MGETHDR(m, M_DONTWAIT, MT_HEADER);
540 MGET(m, M_DONTWAIT, MT_HEADER);
551 if (fin->fin_v == 4) {
552 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
553 switch (ntohs(fin->fin_data[0]) >> 8)
566 if (ipf_ifpaddr(&ipfmain, 4, FRI_NORMAL, ifp,
567 &dst6, NULL) == -1) {
573 dst4.s_addr = fin->fin_daddr;
576 ohlen = fin->fin_hlen;
577 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
578 if (fin->fin_hlen < fin->fin_plen)
579 xtra = MIN(fin->fin_dlen, 8);
585 else if (fin->fin_v == 6) {
586 hlen = sizeof(ip6_t);
587 ohlen = sizeof(ip6_t);
588 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
589 type = icmptoicmp6types[type];
590 if (type == ICMP6_DST_UNREACH)
591 code = icmptoicmp6unreach[code];
593 if (iclen + max_linkhdr + fin->fin_plen > avail) {
594 MCLGET(m, M_DONTWAIT);
595 if ((m->m_flags & M_EXT) == 0) {
601 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
602 xtra = MIN(xtra, IPV6_MMTU - iclen);
604 if (ipf_ifpaddr(&ipfmain, 6, FRI_NORMAL, ifp,
605 &dst6, NULL) == -1) {
610 dst6 = fin->fin_dst6;
618 avail -= (max_linkhdr + iclen);
626 m->m_data += max_linkhdr;
627 m->m_pkthdr.rcvif = (struct ifnet *)0;
628 m->m_pkthdr.len = iclen;
630 ip = mtod(m, ip_t *);
631 icmp = (struct icmp *)((char *)ip + hlen);
632 ip2 = (ip_t *)&icmp->icmp_ip;
634 icmp->icmp_type = type;
635 icmp->icmp_code = fin->fin_icode;
636 icmp->icmp_cksum = 0;
638 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
639 if (fin->fin_mtu != 0) {
640 icmp->icmp_nextmtu = htons(fin->fin_mtu);
642 } else if (ifp != NULL) {
643 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
645 } else { /* make up a number... */
646 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
651 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
655 if (fin->fin_v == 6) {
656 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
657 ip6->ip6_plen = htons(iclen - hlen);
658 ip6->ip6_nxt = IPPROTO_ICMPV6;
660 ip6->ip6_src = dst6.in6;
661 ip6->ip6_dst = fin->fin_src6.in6;
663 bcopy((char *)fin->fin_ip + ohlen,
664 (char *)&icmp->icmp_ip + ohlen, xtra);
665 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
666 sizeof(*ip6), iclen - hlen);
670 ip->ip_p = IPPROTO_ICMP;
671 ip->ip_src.s_addr = dst4.s_addr;
672 ip->ip_dst.s_addr = fin->fin_saddr;
675 bcopy((char *)fin->fin_ip + ohlen,
676 (char *)&icmp->icmp_ip + ohlen, xtra);
677 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
679 ip->ip_len = htons(iclen);
680 ip->ip_p = IPPROTO_ICMP;
682 err = ipf_send_ip(fin, m);
690 * m0 - pointer to mbuf where the IP packet starts
691 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
694 ipf_fastroute(m0, mpp, fin, fdp)
699 register struct ip *ip, *mhip;
700 register struct mbuf *m = *mpp;
701 register struct route *ro;
702 int len, off, error = 0, hlen, code;
703 struct ifnet *ifp, *sifp;
704 struct sockaddr_in *dst;
705 struct route iproute;
716 * If the mbuf we're about to send is not writable (because of
717 * a cluster reference, for example) we'll need to make a copy
718 * of it since this routine modifies the contents.
720 * If you have non-crappy network hardware that can transmit data
721 * from the mbuf, rather than making a copy, this is gonna be a
724 if (M_WRITABLE(m) == 0) {
725 m0 = m_dup(m, M_DONTWAIT);
739 if (fin->fin_v == 6) {
741 * currently "to <if>" and "to <if>:ip#" are not supported
744 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
748 hlen = fin->fin_hlen;
749 ip = mtod(m0, struct ip *);
756 bzero(ro, sizeof (*ro));
757 dst = (struct sockaddr_in *)&ro->ro_dst;
758 dst->sin_family = AF_INET;
759 dst->sin_addr = ip->ip_dst;
762 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
763 (fdp->fd_type == FRD_DSTLIST)) {
764 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
773 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
778 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
779 dst->sin_addr = fdp->fd_ip;
781 dst->sin_len = sizeof(*dst);
782 in_rtalloc(ro, M_GETFIB(m0));
784 if ((ifp == NULL) && (ro->ro_rt != NULL))
785 ifp = ro->ro_rt->rt_ifp;
787 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
788 if (in_localaddr(ip->ip_dst))
789 error = EHOSTUNREACH;
794 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
795 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
797 counter_u64_add(ro->ro_rt->rt_pksent, 1);
800 * For input packets which are being "fastrouted", they won't
801 * go back through output filtering and miss their chance to get
802 * NAT'd and counted. Duplicated packets aren't considered to be
803 * part of the normal packet stream, so do not NAT them or pass
804 * them through stateful checking, etc.
806 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
810 (void) ipf_acctpkt(fin, NULL);
812 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
815 (void) ipf_state_check(fin, &pass);
818 switch (ipf_nat_checkout(fin, NULL))
836 * If small enough for interface, can just send directly.
838 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
840 ip->ip_sum = in_cksum(m, hlen);
841 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
847 * Too large for interface; fragment if possible.
848 * Must be able to put at least 8 bytes per fragment.
850 ip_off = ntohs(ip->ip_off);
851 if (ip_off & IP_DF) {
855 len = (ifp->if_mtu - hlen) &~ 7;
862 int mhlen, firstlen = len;
863 struct mbuf **mnext = &m->m_act;
866 * Loop through length of segment after first fragment,
867 * make new header and copy data of each part and link onto chain.
870 mhlen = sizeof (struct ip);
871 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
873 MGETHDR(m, M_DONTWAIT, MT_HEADER);
875 MGET(m, M_DONTWAIT, MT_HEADER);
882 m->m_data += max_linkhdr;
883 mhip = mtod(m, struct ip *);
884 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
885 if (hlen > sizeof (struct ip)) {
886 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
887 IP_HL_A(mhip, mhlen >> 2);
890 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
891 if (off + len >= ntohs(ip->ip_len))
892 len = ntohs(ip->ip_len) - off;
894 mhip->ip_off |= IP_MF;
895 mhip->ip_len = htons((u_short)(len + mhlen));
897 m->m_next = m_copy(m0, off, len);
898 if (m->m_next == 0) {
899 error = ENOBUFS; /* ??? */
902 m->m_pkthdr.len = mhlen + len;
903 m->m_pkthdr.rcvif = NULL;
904 mhip->ip_off = htons((u_short)mhip->ip_off);
906 mhip->ip_sum = in_cksum(m, mhlen);
910 * Update first fragment by trimming what's been copied out
911 * and updating header, then send each fragment (in order).
913 m_adj(m0, hlen + firstlen - ip->ip_len);
914 ip->ip_len = htons((u_short)(hlen + firstlen));
915 ip->ip_off = htons((u_short)IP_MF);
917 ip->ip_sum = in_cksum(m0, hlen);
919 for (m = m0; m; m = m0) {
923 error = (*ifp->if_output)(ifp, m,
924 (struct sockaddr *)dst,
933 ipfmain.ipf_frouteok[0]++;
935 ipfmain.ipf_frouteok[1]++;
937 if ((ro != NULL) && (ro->ro_rt != NULL)) {
942 if (error == EMSGSIZE) {
944 code = fin->fin_icode;
945 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
947 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
949 fin->fin_icode = code;
960 struct sockaddr_in *dst;
961 struct route iproute;
963 bzero((char *)&iproute, sizeof(iproute));
964 dst = (struct sockaddr_in *)&iproute.ro_dst;
965 dst->sin_len = sizeof(*dst);
966 dst->sin_family = AF_INET;
967 dst->sin_addr = fin->fin_src;
968 in_rtalloc(&iproute, 0);
969 if (iproute.ro_rt == NULL)
971 return (fin->fin_ifp == iproute.ro_rt->rt_ifp);
976 * return the first IP Address associated with an interface
979 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
980 ipf_main_softc_t *softc;
983 i6addr_t *inp, *inpmask;
986 struct in6_addr *inp6 = NULL;
988 struct sockaddr *sock, *mask;
989 struct sockaddr_in *sin;
993 if ((ifptr == NULL) || (ifptr == (void *)-1))
1000 inp->in4.s_addr = 0;
1003 bzero((char *)inp, sizeof(*inp));
1005 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1007 sock = ifa->ifa_addr;
1008 while (sock != NULL && ifa != NULL) {
1009 sin = (struct sockaddr_in *)sock;
1010 if ((v == 4) && (sin->sin_family == AF_INET))
1013 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1014 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1015 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1016 !IN6_IS_ADDR_LOOPBACK(inp6))
1020 ifa = TAILQ_NEXT(ifa, ifa_link);
1022 sock = ifa->ifa_addr;
1025 if (ifa == NULL || sin == NULL)
1028 mask = ifa->ifa_netmask;
1029 if (atype == FRI_BROADCAST)
1030 sock = ifa->ifa_broadaddr;
1031 else if (atype == FRI_PEERADDR)
1032 sock = ifa->ifa_dstaddr;
1039 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1040 (struct sockaddr_in6 *)mask,
1044 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1045 (struct sockaddr_in *)mask,
1046 &inp->in4, &inpmask->in4);
1055 newiss = arc4random();
1060 /* ------------------------------------------------------------------------ */
1061 /* Function: ipf_nextipid */
1062 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
1063 /* Parameters: fin(I) - pointer to packet information */
1065 /* Returns the next IPv4 ID to use for this packet. */
1066 /* ------------------------------------------------------------------------ */
1073 #ifndef RANDOM_IP_ID
1074 MUTEX_ENTER(&ipfmain.ipf_rw);
1076 MUTEX_EXIT(&ipfmain.ipf_rw);
1089 #ifdef CSUM_DATA_VALID
1095 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1098 if ((fin->fin_flx & FI_SHORT) != 0)
1101 if (fin->fin_cksum != FI_CK_NEEDED)
1102 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1111 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1113 fin->fin_cksum = FI_CK_BAD;
1114 fin->fin_flx |= FI_BAD;
1117 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1118 /* Depending on the driver, UDP may have zero checksum */
1119 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1120 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1121 udphdr_t *udp = fin->fin_dp;
1122 if (udp->uh_sum == 0) {
1124 * we're good no matter what the hardware
1125 * checksum flags and csum_data say (handling
1126 * of csum_data for zero UDP checksum is not
1127 * consistent across all drivers)
1134 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
1135 sum = m->m_pkthdr.csum_data;
1137 sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
1138 htonl(m->m_pkthdr.csum_data +
1139 fin->fin_dlen + fin->fin_p));
1142 fin->fin_cksum = FI_CK_BAD;
1143 fin->fin_flx |= FI_BAD;
1145 fin->fin_cksum = FI_CK_SUMOK;
1149 if (m->m_pkthdr.csum_flags == CSUM_DELAY_DATA) {
1150 fin->fin_cksum = FI_CK_L4FULL;
1152 } else if (m->m_pkthdr.csum_flags == CSUM_TCP ||
1153 m->m_pkthdr.csum_flags == CSUM_UDP) {
1154 fin->fin_cksum = FI_CK_L4PART;
1156 } else if (m->m_pkthdr.csum_flags == CSUM_IP) {
1157 fin->fin_cksum = FI_CK_L4PART;
1165 if (ipf_checkl4sum(fin) == -1) {
1166 fin->fin_flx |= FI_BAD;
1171 if (ipf_checkl4sum(fin) == -1) {
1172 fin->fin_flx |= FI_BAD;
1185 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1188 if ((fin->fin_flx & FI_SHORT) != 0)
1191 if (fin->fin_cksum != FI_CK_NEEDED)
1192 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1194 if (ipf_checkl4sum(fin) == -1) {
1195 fin->fin_flx |= FI_BAD;
1200 #endif /* USE_INET6 */
1209 if ((m0->m_flags & M_PKTHDR) != 0) {
1210 len = m0->m_pkthdr.len;
1214 for (m = m0, len = 0; m != NULL; m = m->m_next)
1221 /* ------------------------------------------------------------------------ */
1222 /* Function: ipf_pullup */
1223 /* Returns: NULL == pullup failed, else pointer to protocol header */
1224 /* Parameters: xmin(I)- pointer to buffer where data packet starts */
1225 /* fin(I) - pointer to packet information */
1226 /* len(I) - number of bytes to pullup */
1228 /* Attempt to move at least len bytes (from the start of the buffer) into a */
1229 /* single buffer for ease of access. Operating system native functions are */
1230 /* used to manage buffers - if necessary. If the entire packet ends up in */
1231 /* a single buffer, set the FI_COALESCE flag even though ipf_coalesce() has */
1232 /* not been called. Both fin_ip and fin_dp are updated before exiting _IF_ */
1233 /* and ONLY if the pullup succeeds. */
1235 /* We assume that 'xmin' is a pointer to a buffer that is part of the chain */
1236 /* of buffers that starts at *fin->fin_mp. */
1237 /* ------------------------------------------------------------------------ */
1239 ipf_pullup(xmin, fin, len)
1251 ip = (char *)fin->fin_ip;
1252 if ((fin->fin_flx & FI_COALESCE) != 0)
1255 ipoff = fin->fin_ipoff;
1256 if (fin->fin_dp != NULL)
1257 dpoff = (char *)fin->fin_dp - (char *)ip;
1261 if (M_LEN(m) < len) {
1262 mb_t *n = *fin->fin_mp;
1264 * Assume that M_PKTHDR is set and just work with what is left
1265 * rather than check..
1266 * Should not make any real difference, anyway.
1270 * Record the mbuf that points to the mbuf that we're
1271 * about to go to work on so that we can update the
1272 * m_next appropriately later.
1274 for (; n->m_next != m; n = n->m_next)
1286 #ifdef HAVE_M_PULLDOWN
1287 if (m_pulldown(m, 0, len, NULL) == NULL)
1290 FREE_MB_T(*fin->fin_mp);
1296 m = m_pullup(m, len);
1302 * When n is non-NULL, it indicates that m pointed to
1303 * a sub-chain (tail) of the mbuf and that the head
1304 * of this chain has not yet been free'd.
1307 FREE_MB_T(*fin->fin_mp);
1310 *fin->fin_mp = NULL;
1318 while (M_LEN(m) == 0) {
1322 ip = MTOD(m, char *) + ipoff;
1324 fin->fin_ip = (ip_t *)ip;
1325 if (fin->fin_dp != NULL)
1326 fin->fin_dp = (char *)fin->fin_ip + dpoff;
1327 if (fin->fin_fraghdr != NULL)
1328 fin->fin_fraghdr = (char *)ip +
1329 ((char *)fin->fin_fraghdr -
1330 (char *)fin->fin_ip);
1333 if (len == fin->fin_plen)
1334 fin->fin_flx |= FI_COALESCE;
1346 if (fin->fin_out == 0) {
1347 netisr_dispatch(NETISR_IP, m);
1349 fin->fin_ip->ip_len = ntohs(fin->fin_ip->ip_len);
1350 fin->fin_ip->ip_off = ntohs(fin->fin_ip->ip_off);
1351 error = ip_output(m, NULL, NULL, IP_FORWARDING, NULL, NULL);
1357 int ipf_pfil_unhook(void) {
1358 struct pfil_head *ph_inet;
1360 struct pfil_head *ph_inet6;
1363 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1364 if (ph_inet != NULL)
1365 pfil_remove_hook((void *)ipf_check_wrapper, NULL,
1366 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1368 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1369 if (ph_inet6 != NULL)
1370 pfil_remove_hook((void *)ipf_check_wrapper6, NULL,
1371 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet6);
1377 int ipf_pfil_hook(void) {
1378 struct pfil_head *ph_inet;
1380 struct pfil_head *ph_inet6;
1383 ph_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
1385 ph_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
1395 if (ph_inet != NULL)
1396 pfil_add_hook((void *)ipf_check_wrapper, NULL,
1397 PFIL_IN|PFIL_OUT|PFIL_WAITOK, ph_inet);
1399 if (ph_inet6 != NULL)
1400 pfil_add_hook((void *)ipf_check_wrapper6, NULL,
1401 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;
1472 ipf_pcksum6(fin, ip6, off, len)
1482 if (m->m_len < sizeof(struct ip6_hdr)) {
1486 sum = in6_cksum(m, ip6->ip6_nxt, off, len);
1491 ipf_pcksum6(fin, ip6, off, len)
1500 sp = (u_short *)&ip6->ip6_src;
1501 sum = *sp++; /* ip6_src */
1509 sum += *sp++; /* ip6_dst */
1517 return(ipf_pcksum(fin, off, sum));