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 # if defined(NETBSD_PF)
104 # include <net/pfil.h>
105 # endif /* NETBSD_PF */
107 * We provide the ipf_checkp name just to minimize changes later.
109 int (*ipf_checkp) __P((void *, ip_t *ip, int hlen, void *ifp, int out, mb_t **mp));
112 static eventhandler_tag ipf_arrivetag, ipf_departtag, ipf_clonetag;
114 static void ipf_ifevent(void *arg);
116 static void ipf_ifevent(arg)
125 ipf_check_wrapper(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
127 struct ip *ip = mtod(*mp, struct ip *);
131 * IPFilter expects evreything in network byte order
133 #if (__FreeBSD_version < 1000019)
134 ip->ip_len = htons(ip->ip_len);
135 ip->ip_off = htons(ip->ip_off);
137 rv = ipf_check(&ipfmain, ip, ip->ip_hl << 2, ifp, (dir == PFIL_OUT),
139 #if (__FreeBSD_version < 1000019)
140 if ((rv == 0) && (*mp != NULL)) {
141 ip = mtod(*mp, struct ip *);
142 ip->ip_len = ntohs(ip->ip_len);
143 ip->ip_off = ntohs(ip->ip_off);
150 # include <netinet/ip6.h>
153 ipf_check_wrapper6(void *arg, struct mbuf **mp, struct ifnet *ifp, int dir)
155 return (ipf_check(&ipfmain, mtod(*mp, struct ip *),
156 sizeof(struct ip6_hdr), ifp, (dir == PFIL_OUT), mp));
159 #if defined(IPFILTER_LKM)
163 if (strcmp(s, "ipl") == 0)
167 #endif /* IPFILTER_LKM */
174 ipf_main_softc_t *softc = arg;
178 READ_ENTER(&softc->ipf_global);
180 if (softc->ipf_running > 0)
181 ipf_slowtimer(softc);
183 if (softc->ipf_running == -1 || softc->ipf_running == 1) {
185 softc->ipf_slow_ch = timeout(ipf_timer_func, softc, hz/2);
187 callout_init(&softc->ipf_slow_ch, 1);
188 callout_reset(&softc->ipf_slow_ch,
189 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
190 ipf_timer_func, softc);
192 RWLOCK_EXIT(&softc->ipf_global);
199 ipf_main_softc_t *softc;
206 if (softc->ipf_running > 0) {
211 if (ipf_init_all(softc) < 0) {
217 if (ipf_checkp != ipf_check) {
218 ipf_savep = ipf_checkp;
219 ipf_checkp = ipf_check;
222 bzero((char *)ipfmain.ipf_selwait, sizeof(ipfmain.ipf_selwait));
223 softc->ipf_running = 1;
225 if (softc->ipf_control_forwarding & 1)
232 softc->ipf_slow_ch = timeout(ipf_timer_func, softc,
233 (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT);
235 callout_init(&softc->ipf_slow_ch, 1);
236 callout_reset(&softc->ipf_slow_ch, (hz / IPF_HZ_DIVIDE) * IPF_HZ_MULT,
237 ipf_timer_func, softc);
243 * Disable the filter by removing the hooks from the IP input/output
248 ipf_main_softc_t *softc;
254 if (softc->ipf_control_forwarding & 2)
260 if (softc->ipf_slow_ch.callout != NULL)
261 untimeout(ipf_timer_func, softc, softc->ipf_slow_ch);
262 bzero(&softc->ipf_slow, sizeof(softc->ipf_slow));
264 callout_drain(&softc->ipf_slow_ch);
267 if (ipf_checkp != NULL)
268 ipf_checkp = ipf_savep;
274 softc->ipf_running = -2;
283 * Filter ioctl interface.
286 ipfioctl(dev, cmd, data, mode, p)
288 # define p_cred td_ucred
289 # define p_uid td_ucred->cr_ruid
295 int error = 0, unit = 0;
299 if (securelevel_ge(p->p_cred, 3) && (mode & FWRITE))
301 ipfmain.ipf_interror = 130001;
306 unit = GET_MINOR(dev);
307 if ((IPL_LOGMAX < unit) || (unit < 0)) {
308 ipfmain.ipf_interror = 130002;
312 if (ipfmain.ipf_running <= 0) {
313 if (unit != IPL_LOGIPF && cmd != SIOCIPFINTERROR) {
314 ipfmain.ipf_interror = 130003;
317 if (cmd != SIOCIPFGETNEXT && cmd != SIOCIPFGET &&
318 cmd != SIOCIPFSET && cmd != SIOCFRENB &&
319 cmd != SIOCGETFS && cmd != SIOCGETFF &&
320 cmd != SIOCIPFINTERROR) {
321 ipfmain.ipf_interror = 130004;
328 CURVNET_SET(TD_TO_VNET(p));
329 error = ipf_ioctlswitch(&ipfmain, unit, data, cmd, mode, p->p_uid, p);
343 * ipf_send_reset - this could conceivably be a call to tcp_respond(), but that
344 * requires a large amount of setting up and isn't any more efficient.
350 struct tcphdr *tcp, *tcp2;
359 if (tcp->th_flags & TH_RST)
360 return -1; /* feedback loop */
362 if (ipf_checkl4sum(fin) == -1)
365 tlen = fin->fin_dlen - (TCP_OFF(tcp) << 2) +
366 ((tcp->th_flags & TH_SYN) ? 1 : 0) +
367 ((tcp->th_flags & TH_FIN) ? 1 : 0);
370 hlen = (fin->fin_v == 6) ? sizeof(ip6_t) : sizeof(ip_t);
375 MGETHDR(m, M_DONTWAIT, MT_HEADER);
377 MGET(m, M_DONTWAIT, MT_HEADER);
381 if (sizeof(*tcp2) + hlen > MLEN) {
382 MCLGET(m, M_DONTWAIT);
383 if ((m->m_flags & M_EXT) == 0) {
389 m->m_len = sizeof(*tcp2) + hlen;
391 m->m_data += max_linkhdr;
392 m->m_pkthdr.len = m->m_len;
393 m->m_pkthdr.rcvif = (struct ifnet *)0;
395 ip = mtod(m, struct ip *);
396 bzero((char *)ip, hlen);
400 tcp2 = (struct tcphdr *)((char *)ip + hlen);
401 tcp2->th_sport = tcp->th_dport;
402 tcp2->th_dport = tcp->th_sport;
404 if (tcp->th_flags & TH_ACK) {
405 tcp2->th_seq = tcp->th_ack;
406 tcp2->th_flags = TH_RST;
410 tcp2->th_ack = ntohl(tcp->th_seq);
411 tcp2->th_ack += tlen;
412 tcp2->th_ack = htonl(tcp2->th_ack);
413 tcp2->th_flags = TH_RST|TH_ACK;
416 TCP_OFF_A(tcp2, sizeof(*tcp2) >> 2);
417 tcp2->th_win = tcp->th_win;
422 if (fin->fin_v == 6) {
423 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
424 ip6->ip6_plen = htons(sizeof(struct tcphdr));
425 ip6->ip6_nxt = IPPROTO_TCP;
427 ip6->ip6_src = fin->fin_dst6.in6;
428 ip6->ip6_dst = fin->fin_src6.in6;
429 tcp2->th_sum = in6_cksum(m, IPPROTO_TCP,
430 sizeof(*ip6), sizeof(*tcp2));
431 return ipf_send_ip(fin, m);
434 ip->ip_p = IPPROTO_TCP;
435 ip->ip_len = htons(sizeof(struct tcphdr));
436 ip->ip_src.s_addr = fin->fin_daddr;
437 ip->ip_dst.s_addr = fin->fin_saddr;
438 tcp2->th_sum = in_cksum(m, hlen + sizeof(*tcp2));
439 ip->ip_len = htons(hlen + sizeof(*tcp2));
440 return ipf_send_ip(fin, m);
445 * ip_len must be in network byte order when called.
456 ip = mtod(m, ip_t *);
457 bzero((char *)&fnew, sizeof(fnew));
458 fnew.fin_main_soft = fin->fin_main_soft;
460 IP_V_A(ip, fin->fin_v);
467 fnew.fin_p = ip->ip_p;
468 fnew.fin_plen = ntohs(ip->ip_len);
469 IP_HL_A(ip, sizeof(*oip) >> 2);
470 ip->ip_tos = oip->ip_tos;
471 ip->ip_id = fin->fin_ip->ip_id;
472 ip->ip_off = htons(V_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;
533 /* See NetBSD ip_fil_netbsd.c r1.4: */
534 if ((code < 0) || (code >= sizeof(icmptoicmp6unreach)/sizeof(int)))
538 if (ipf_checkl4sum(fin) == -1)
541 MGETHDR(m, M_DONTWAIT, MT_HEADER);
543 MGET(m, M_DONTWAIT, MT_HEADER);
554 if (fin->fin_v == 4) {
555 if ((fin->fin_p == IPPROTO_ICMP) && !(fin->fin_flx & FI_SHORT))
556 switch (ntohs(fin->fin_data[0]) >> 8)
569 if (ipf_ifpaddr(&ipfmain, 4, FRI_NORMAL, ifp,
570 &dst6, NULL) == -1) {
576 dst4.s_addr = fin->fin_daddr;
579 ohlen = fin->fin_hlen;
580 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
581 if (fin->fin_hlen < fin->fin_plen)
582 xtra = MIN(fin->fin_dlen, 8);
588 else if (fin->fin_v == 6) {
589 hlen = sizeof(ip6_t);
590 ohlen = sizeof(ip6_t);
591 iclen = hlen + offsetof(struct icmp, icmp_ip) + ohlen;
592 type = icmptoicmp6types[type];
593 if (type == ICMP6_DST_UNREACH)
594 code = icmptoicmp6unreach[code];
596 if (iclen + max_linkhdr + fin->fin_plen > avail) {
597 MCLGET(m, M_DONTWAIT);
598 if ((m->m_flags & M_EXT) == 0) {
604 xtra = MIN(fin->fin_plen, avail - iclen - max_linkhdr);
605 xtra = MIN(xtra, IPV6_MMTU - iclen);
607 if (ipf_ifpaddr(&ipfmain, 6, FRI_NORMAL, ifp,
608 &dst6, NULL) == -1) {
613 dst6 = fin->fin_dst6;
621 avail -= (max_linkhdr + iclen);
629 m->m_data += max_linkhdr;
630 m->m_pkthdr.rcvif = (struct ifnet *)0;
631 m->m_pkthdr.len = iclen;
633 ip = mtod(m, ip_t *);
634 icmp = (struct icmp *)((char *)ip + hlen);
635 ip2 = (ip_t *)&icmp->icmp_ip;
637 icmp->icmp_type = type;
638 icmp->icmp_code = fin->fin_icode;
639 icmp->icmp_cksum = 0;
641 if (type == ICMP_UNREACH && fin->fin_icode == ICMP_UNREACH_NEEDFRAG) {
642 if (fin->fin_mtu != 0) {
643 icmp->icmp_nextmtu = htons(fin->fin_mtu);
645 } else if (ifp != NULL) {
646 icmp->icmp_nextmtu = htons(GETIFMTU_4(ifp));
648 } else { /* make up a number... */
649 icmp->icmp_nextmtu = htons(fin->fin_plen - 20);
654 bcopy((char *)fin->fin_ip, (char *)ip2, ohlen);
658 if (fin->fin_v == 6) {
659 ip6->ip6_flow = ((ip6_t *)fin->fin_ip)->ip6_flow;
660 ip6->ip6_plen = htons(iclen - hlen);
661 ip6->ip6_nxt = IPPROTO_ICMPV6;
663 ip6->ip6_src = dst6.in6;
664 ip6->ip6_dst = fin->fin_src6.in6;
666 bcopy((char *)fin->fin_ip + ohlen,
667 (char *)&icmp->icmp_ip + ohlen, xtra);
668 icmp->icmp_cksum = in6_cksum(m, IPPROTO_ICMPV6,
669 sizeof(*ip6), iclen - hlen);
673 ip->ip_p = IPPROTO_ICMP;
674 ip->ip_src.s_addr = dst4.s_addr;
675 ip->ip_dst.s_addr = fin->fin_saddr;
678 bcopy((char *)fin->fin_ip + ohlen,
679 (char *)&icmp->icmp_ip + ohlen, xtra);
680 icmp->icmp_cksum = ipf_cksum((u_short *)icmp,
682 ip->ip_len = htons(iclen);
683 ip->ip_p = IPPROTO_ICMP;
685 err = ipf_send_ip(fin, m);
693 * m0 - pointer to mbuf where the IP packet starts
694 * mpp - pointer to the mbuf pointer that is the start of the mbuf chain
697 ipf_fastroute(m0, mpp, fin, fdp)
702 register struct ip *ip, *mhip;
703 register struct mbuf *m = *mpp;
704 register struct route *ro;
705 int len, off, error = 0, hlen, code;
706 struct ifnet *ifp, *sifp;
707 struct sockaddr_in *dst;
708 struct route iproute;
719 * If the mbuf we're about to send is not writable (because of
720 * a cluster reference, for example) we'll need to make a copy
721 * of it since this routine modifies the contents.
723 * If you have non-crappy network hardware that can transmit data
724 * from the mbuf, rather than making a copy, this is gonna be a
727 if (M_WRITABLE(m) == 0) {
728 m0 = m_dup(m, M_DONTWAIT);
742 if (fin->fin_v == 6) {
744 * currently "to <if>" and "to <if>:ip#" are not supported
747 return ip6_output(m, NULL, NULL, 0, NULL, NULL, NULL);
751 hlen = fin->fin_hlen;
752 ip = mtod(m0, struct ip *);
759 bzero(ro, sizeof (*ro));
760 dst = (struct sockaddr_in *)&ro->ro_dst;
761 dst->sin_family = AF_INET;
762 dst->sin_addr = ip->ip_dst;
765 if ((fr != NULL) && !(fr->fr_flags & FR_KEEPSTATE) && (fdp != NULL) &&
766 (fdp->fd_type == FRD_DSTLIST)) {
767 if (ipf_dstlist_select_node(fin, fdp->fd_ptr, NULL, &node) == 0)
776 if ((ifp == NULL) && ((fr == NULL) || !(fr->fr_flags & FR_FASTROUTE))) {
781 if ((fdp != NULL) && (fdp->fd_ip.s_addr != 0))
782 dst->sin_addr = fdp->fd_ip;
784 dst->sin_len = sizeof(*dst);
785 in_rtalloc(ro, M_GETFIB(m0));
787 if ((ifp == NULL) && (ro->ro_rt != NULL))
788 ifp = ro->ro_rt->rt_ifp;
790 if ((ro->ro_rt == NULL) || (ifp == NULL)) {
791 if (in_localaddr(ip->ip_dst))
792 error = EHOSTUNREACH;
797 if (ro->ro_rt->rt_flags & RTF_GATEWAY)
798 dst = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
800 counter_u64_add(ro->ro_rt->rt_pksent, 1);
803 * For input packets which are being "fastrouted", they won't
804 * go back through output filtering and miss their chance to get
805 * NAT'd and counted. Duplicated packets aren't considered to be
806 * part of the normal packet stream, so do not NAT them or pass
807 * them through stateful checking, etc.
809 if ((fdp != &fr->fr_dif) && (fin->fin_out == 0)) {
813 (void) ipf_acctpkt(fin, NULL);
815 if (!fr || !(fr->fr_flags & FR_RETMASK)) {
818 (void) ipf_state_check(fin, &pass);
821 switch (ipf_nat_checkout(fin, NULL))
839 * If small enough for interface, can just send directly.
841 if (ntohs(ip->ip_len) <= ifp->if_mtu) {
843 ip->ip_sum = in_cksum(m, hlen);
844 error = (*ifp->if_output)(ifp, m, (struct sockaddr *)dst,
850 * Too large for interface; fragment if possible.
851 * Must be able to put at least 8 bytes per fragment.
853 ip_off = ntohs(ip->ip_off);
854 if (ip_off & IP_DF) {
858 len = (ifp->if_mtu - hlen) &~ 7;
865 int mhlen, firstlen = len;
866 struct mbuf **mnext = &m->m_act;
869 * Loop through length of segment after first fragment,
870 * make new header and copy data of each part and link onto chain.
873 mhlen = sizeof (struct ip);
874 for (off = hlen + len; off < ntohs(ip->ip_len); off += len) {
876 MGETHDR(m, M_DONTWAIT, MT_HEADER);
878 MGET(m, M_DONTWAIT, MT_HEADER);
885 m->m_data += max_linkhdr;
886 mhip = mtod(m, struct ip *);
887 bcopy((char *)ip, (char *)mhip, sizeof(*ip));
888 if (hlen > sizeof (struct ip)) {
889 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
890 IP_HL_A(mhip, mhlen >> 2);
893 mhip->ip_off = ((off - hlen) >> 3) + ip_off;
894 if (off + len >= ntohs(ip->ip_len))
895 len = ntohs(ip->ip_len) - off;
897 mhip->ip_off |= IP_MF;
898 mhip->ip_len = htons((u_short)(len + mhlen));
900 m->m_next = m_copy(m0, off, len);
901 if (m->m_next == 0) {
902 error = ENOBUFS; /* ??? */
905 m->m_pkthdr.len = mhlen + len;
906 m->m_pkthdr.rcvif = NULL;
907 mhip->ip_off = htons((u_short)mhip->ip_off);
909 mhip->ip_sum = in_cksum(m, mhlen);
913 * Update first fragment by trimming what's been copied out
914 * and updating header, then send each fragment (in order).
916 m_adj(m0, hlen + firstlen - ip->ip_len);
917 ip->ip_len = htons((u_short)(hlen + firstlen));
918 ip->ip_off = htons((u_short)IP_MF);
920 ip->ip_sum = in_cksum(m0, hlen);
922 for (m = m0; m; m = m0) {
926 error = (*ifp->if_output)(ifp, m,
927 (struct sockaddr *)dst,
936 ipfmain.ipf_frouteok[0]++;
938 ipfmain.ipf_frouteok[1]++;
940 if ((ro != NULL) && (ro->ro_rt != NULL)) {
945 if (error == EMSGSIZE) {
947 code = fin->fin_icode;
948 fin->fin_icode = ICMP_UNREACH_NEEDFRAG;
950 (void) ipf_send_icmp_err(ICMP_UNREACH, fin, 1);
952 fin->fin_icode = code;
963 struct sockaddr_in *dst;
964 struct route iproute;
966 bzero((char *)&iproute, sizeof(iproute));
967 dst = (struct sockaddr_in *)&iproute.ro_dst;
968 dst->sin_len = sizeof(*dst);
969 dst->sin_family = AF_INET;
970 dst->sin_addr = fin->fin_src;
971 in_rtalloc(&iproute, 0);
972 if (iproute.ro_rt == NULL)
974 return (fin->fin_ifp == iproute.ro_rt->rt_ifp);
979 * return the first IP Address associated with an interface
982 ipf_ifpaddr(softc, v, atype, ifptr, inp, inpmask)
983 ipf_main_softc_t *softc;
986 i6addr_t *inp, *inpmask;
989 struct in6_addr *inp6 = NULL;
991 struct sockaddr *sock, *mask;
992 struct sockaddr_in *sin;
996 if ((ifptr == NULL) || (ifptr == (void *)-1))
1003 inp->in4.s_addr = 0;
1006 bzero((char *)inp, sizeof(*inp));
1008 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1010 sock = ifa->ifa_addr;
1011 while (sock != NULL && ifa != NULL) {
1012 sin = (struct sockaddr_in *)sock;
1013 if ((v == 4) && (sin->sin_family == AF_INET))
1016 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1017 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1018 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1019 !IN6_IS_ADDR_LOOPBACK(inp6))
1023 ifa = TAILQ_NEXT(ifa, ifa_link);
1025 sock = ifa->ifa_addr;
1028 if (ifa == NULL || sin == NULL)
1031 mask = ifa->ifa_netmask;
1032 if (atype == FRI_BROADCAST)
1033 sock = ifa->ifa_broadaddr;
1034 else if (atype == FRI_PEERADDR)
1035 sock = ifa->ifa_dstaddr;
1042 return ipf_ifpfillv6addr(atype, (struct sockaddr_in6 *)sock,
1043 (struct sockaddr_in6 *)mask,
1047 return ipf_ifpfillv4addr(atype, (struct sockaddr_in *)sock,
1048 (struct sockaddr_in *)mask,
1049 &inp->in4, &inpmask->in4);
1058 newiss = arc4random();
1063 /* ------------------------------------------------------------------------ */
1064 /* Function: ipf_nextipid */
1065 /* Returns: int - 0 == success, -1 == error (packet should be droppped) */
1066 /* Parameters: fin(I) - pointer to packet information */
1068 /* Returns the next IPv4 ID to use for this packet. */
1069 /* ------------------------------------------------------------------------ */
1076 #ifndef RANDOM_IP_ID
1077 MUTEX_ENTER(&ipfmain.ipf_rw);
1079 MUTEX_EXIT(&ipfmain.ipf_rw);
1092 #ifdef CSUM_DATA_VALID
1098 if ((fin->fin_flx & FI_NOCKSUM) != 0)
1101 if ((fin->fin_flx & FI_SHORT) != 0)
1104 if (fin->fin_cksum != FI_CK_NEEDED)
1105 return (fin->fin_cksum > FI_CK_NEEDED) ? 0 : -1;
1114 if ((m->m_pkthdr.csum_flags & (CSUM_IP_CHECKED|CSUM_IP_VALID)) ==
1116 fin->fin_cksum = FI_CK_BAD;
1117 fin->fin_flx |= FI_BAD;
1120 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
1121 /* Depending on the driver, UDP may have zero checksum */
1122 if (fin->fin_p == IPPROTO_UDP && (fin->fin_flx &
1123 (FI_FRAG|FI_SHORT|FI_BAD)) == 0) {
1124 udphdr_t *udp = fin->fin_dp;
1125 if (udp->uh_sum == 0) {
1127 * we're good no matter what the hardware
1128 * checksum flags and csum_data say (handling
1129 * of csum_data for zero UDP checksum is not
1130 * consistent across all drivers)
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;