2 * Copyright (C) 1993-2001 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * @(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed
7 * $FreeBSD: src/sys/contrib/ipfilter/netinet/fil.c,v 1.23.2.6 2003/03/01 03:55:54 darrenr Exp $
8 * $DragonFly: src/sys/contrib/ipfilter/netinet/fil.c,v 1.8 2004/06/02 14:42:48 eirikn Exp $
10 #if defined(__sgi) && (IRIX > 602)
11 # include <sys/ptimers.h>
13 #include <sys/errno.h>
14 #include <sys/types.h>
15 #include <sys/param.h>
18 #if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
20 # include "opt_ipfilter_log.h"
22 #if (defined(KERNEL) || defined(_KERNEL)) && (defined(__DragonFly__) || (defined(__FreeBSD_version) && \
23 (__FreeBSD_version >= 220000)))
24 # if defined(__DragonFly__) || (__FreeBSD_version >= 400000)
26 # include "opt_inet6.h"
28 # if defined(__FreeBSD__) && (__FreeBSD_version == 400019)
29 # define CSUM_DELAY_DATA
32 # include <sys/filio.h>
33 # include <sys/fcntl.h>
35 # include <sys/ioctl.h>
37 #if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
38 # include <sys/systm.h>
44 #if !defined(__SVR4) && !defined(__svr4__)
46 # include <sys/mbuf.h>
49 # include <sys/byteorder.h>
51 # include <sys/dditypes.h>
53 # include <sys/stream.h>
56 # include <sys/protosw.h>
57 # include <sys/socket.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
68 # include <netinet/ip_var.h>
70 #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
71 # include <sys/hashing.h>
72 # include <netinet/in_var.h>
74 #include <netinet/tcp.h>
75 #include <netinet/udp.h>
76 #include <netinet/ip_icmp.h>
77 #include "ip_compat.h"
79 # include <netinet/icmp6.h>
80 # if !SOLARIS && defined(_KERNEL)
81 # include <netinet6/in6_var.h>
84 #include <netinet/tcpip.h>
91 # if defined(__DragonFly__) || (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
92 # include <sys/malloc.h>
93 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
94 # include "opt_ipfilter.h"
98 # define MIN(a,b) (((a)<(b))?(a):(b))
102 #include <sys/in_cksum.h>
104 static const char sccsid[] = "@(#)fil.c 1.36 6/5/96 (C) 1993-2000 Darren Reed";
111 # define FR_VERBOSE(verb_pr) verbose verb_pr
112 # define FR_DEBUG(verb_pr) debug verb_pr
113 # define IPLLOG(a, c, d, e) ipflog(a, c, d, e)
114 #else /* #ifndef _KERNEL */
115 # define FR_VERBOSE(verb_pr)
116 # define FR_DEBUG(verb_pr)
117 # define IPLLOG(a, c, d, e) ipflog(a, c, d, e)
118 # if SOLARIS || defined(__sgi)
119 extern KRWLOCK_T ipf_mutex, ipf_auth, ipf_nat;
120 extern kmutex_t ipf_rw;
121 # endif /* SOLARIS || __sgi */
125 struct filterstats frstats[2] = {{0,0,0,0,0},{0,0,0,0,0}};
126 struct frentry *ipfilter[2][2] = { { NULL, NULL }, { NULL, NULL } },
128 *ipfilter6[2][2] = { { NULL, NULL }, { NULL, NULL } },
129 *ipacct6[2][2] = { { NULL, NULL }, { NULL, NULL } },
131 *ipacct[2][2] = { { NULL, NULL }, { NULL, NULL } };
132 struct frgroup *ipfgroups[3][2];
133 int fr_flags = IPF_LOGGING;
137 int fr_minttllog = 1;
138 #if defined(IPFILTER_DEFAULT_BLOCK)
139 int fr_pass = FR_NOMATCH|FR_BLOCK;
141 int fr_pass = (IPF_DEFAULT_PASS|FR_NOMATCH);
143 char ipfilter_version[] = IPL_VERSION;
145 fr_info_t frcache[2];
147 static int frflushlist (int, minor_t, int *, frentry_t **);
149 static void frsynclist (frentry_t *);
154 * bit values for identifying presence of individual IP options
156 struct optlist ipopts[20] = {
157 { IPOPT_NOP, 0x000001 },
158 { IPOPT_RR, 0x000002 },
159 { IPOPT_ZSU, 0x000004 },
160 { IPOPT_MTUP, 0x000008 },
161 { IPOPT_MTUR, 0x000010 },
162 { IPOPT_ENCODE, 0x000020 },
163 { IPOPT_TS, 0x000040 },
164 { IPOPT_TR, 0x000080 },
165 { IPOPT_SECURITY, 0x000100 },
166 { IPOPT_LSRR, 0x000200 },
167 { IPOPT_E_SEC, 0x000400 },
168 { IPOPT_CIPSO, 0x000800 },
169 { IPOPT_SATID, 0x001000 },
170 { IPOPT_SSRR, 0x002000 },
171 { IPOPT_ADDEXT, 0x004000 },
172 { IPOPT_VISA, 0x008000 },
173 { IPOPT_IMITD, 0x010000 },
174 { IPOPT_EIP, 0x020000 },
175 { IPOPT_FINN, 0x040000 },
180 * bit values for identifying presence of individual IP security options
182 struct optlist secopt[8] = {
183 { IPSO_CLASS_RES4, 0x01 },
184 { IPSO_CLASS_TOPS, 0x02 },
185 { IPSO_CLASS_SECR, 0x04 },
186 { IPSO_CLASS_RES3, 0x08 },
187 { IPSO_CLASS_CONF, 0x10 },
188 { IPSO_CLASS_UNCL, 0x20 },
189 { IPSO_CLASS_RES2, 0x40 },
190 { IPSO_CLASS_RES1, 0x80 }
195 * compact the IP header into a structure which contains just the info.
196 * which is useful for comparing IP headers with.
198 void fr_makefrip(hlen, ip, fin)
203 u_short optmsk = 0, secmsk = 0, auth = 0;
204 int i, mv, ol, off, p, plen, v;
205 fr_ip_t *fi = &fin->fin_fi;
213 fin->fin_data[0] = 0;
214 fin->fin_data[1] = 0;
217 fin->fin_icode = ipl_unreach;
220 fin->fin_hlen = hlen;
222 fin->fin_id = ip->ip_id;
223 fi->fi_tos = ip->ip_tos;
224 off = (ip->ip_off & IP_OFFMASK);
225 tcp = (tcphdr_t *)((char *)ip + hlen);
226 (*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4));
227 fi->fi_src.i6[1] = 0;
228 fi->fi_src.i6[2] = 0;
229 fi->fi_src.i6[3] = 0;
230 fi->fi_dst.i6[1] = 0;
231 fi->fi_dst.i6[2] = 0;
232 fi->fi_dst.i6[3] = 0;
233 fi->fi_saddr = ip->ip_src.s_addr;
234 fi->fi_daddr = ip->ip_dst.s_addr;
236 fi->fi_fl = (hlen > sizeof(ip_t)) ? FI_OPTIONS : 0;
237 if (ip->ip_off & (IP_MF|IP_OFFMASK))
238 fi->fi_fl |= FI_FRAG;
240 fin->fin_dlen = plen - hlen;
244 ip6_t *ip6 = (ip6_t *)ip;
249 fi->fi_ttl = ip6->ip6_hlim;
250 tcp = (tcphdr_t *)(ip6 + 1);
251 fi->fi_src.in6 = ip6->ip6_src;
252 fi->fi_dst.in6 = ip6->ip6_dst;
253 fin->fin_id = (u_short)(ip6->ip6_flow & 0xffff);
256 plen = ntohs(ip6->ip6_plen);
257 fin->fin_dlen = plen;
258 plen += sizeof(*ip6);
265 fin->fin_plen = plen;
266 fin->fin_dp = (char *)tcp;
273 case IPPROTO_ICMPV6 :
275 int minicmpsz = sizeof(struct icmp6_hdr);
276 struct icmp6_hdr *icmp6;
278 if (fin->fin_dlen > 1) {
279 fin->fin_data[0] = *(u_short *)tcp;
281 icmp6 = (struct icmp6_hdr *)tcp;
283 switch (icmp6->icmp6_type)
285 case ICMP6_ECHO_REPLY :
286 case ICMP6_ECHO_REQUEST :
287 minicmpsz = ICMP6_MINLEN;
289 case ICMP6_DST_UNREACH :
290 case ICMP6_PACKET_TOO_BIG :
291 case ICMP6_TIME_EXCEEDED :
292 case ICMP6_PARAM_PROB :
293 minicmpsz = ICMP6ERR_IPICMPHLEN;
300 if (!(plen >= minicmpsz))
301 fi->fi_fl |= FI_SHORT;
308 int minicmpsz = sizeof(struct icmp);
311 if (!off && (fin->fin_dlen > 1)) {
312 fin->fin_data[0] = *(u_short *)tcp;
314 icmp = (icmphdr_t *)tcp;
316 switch (icmp->icmp_type)
318 case ICMP_ECHOREPLY :
320 /* Router discovery messages - RFC 1256 */
321 case ICMP_ROUTERADVERT :
322 case ICMP_ROUTERSOLICIT :
323 minicmpsz = ICMP_MINLEN;
326 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
330 case ICMP_TSTAMPREPLY :
334 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
338 case ICMP_MASKREPLY :
346 if ((!(plen >= hlen + minicmpsz) && !off) ||
347 (off && off < sizeof(struct icmp)))
348 fi->fi_fl |= FI_SHORT;
353 fi->fi_fl |= FI_TCPUDP;
356 if (plen < sizeof(struct tcphdr))
357 fi->fi_fl |= FI_SHORT;
361 if ((!IPMINLEN(ip, tcphdr) && !off) ||
362 (off && off < sizeof(struct tcphdr)))
363 fi->fi_fl |= FI_SHORT;
365 if (!(fi->fi_fl & FI_SHORT) && !off)
366 fin->fin_tcpf = tcp->th_flags;
369 fi->fi_fl |= FI_TCPUDP;
372 if (plen < sizeof(struct udphdr))
373 fi->fi_fl |= FI_SHORT;
377 if ((!IPMINLEN(ip, udphdr) && !off) ||
378 (off && off < sizeof(struct udphdr)))
379 fi->fi_fl |= FI_SHORT;
382 if (!off && (fin->fin_dlen > 3)) {
383 fin->fin_data[0] = ntohs(tcp->th_sport);
384 fin->fin_data[1] = ntohs(tcp->th_dport);
391 fi->fi_fl |= FI_SHORT;
395 if (((ip->ip_len < hlen + 8) && !off) ||
397 fi->fi_fl |= FI_SHORT;
413 for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
417 else if (opt == IPOPT_NOP)
423 if (ol < 2 || ol > hlen)
426 for (i = 9, mv = 4; mv >= 0; ) {
428 if (opt == (u_char)op->ol_val) {
429 optmsk |= op->ol_bit;
430 if (opt == IPOPT_SECURITY) {
435 sec = *(s + 2); /* classification */
436 for (j = 3, m = 2; m >= 0; ) {
438 if (sec == sp->ol_val) {
439 secmsk |= sp->ol_bit;
445 if (sec < sp->ol_val)
453 if (opt < op->ol_val)
461 if (auth && !(auth & 0x0100))
463 fi->fi_optmsk = optmsk;
464 fi->fi_secmsk = secmsk;
470 * check an IP packet for TCP/UDP characteristics such as ports and flags.
472 int fr_tcpudpchk(ft, fin)
481 * Both ports should *always* be in the first fragment.
482 * So far, I cannot find any cases where they can not be.
484 * compare destination ports
486 if ((i = (int)ft->ftu_dcmp)) {
488 tup = fin->fin_data[1];
490 * Do opposite test to that required and
491 * continue if that succeeds.
493 if (!--i && tup != po) /* EQUAL */
495 else if (!--i && tup == po) /* NOTEQUAL */
497 else if (!--i && tup >= po) /* LESSTHAN */
499 else if (!--i && tup <= po) /* GREATERTHAN */
501 else if (!--i && tup > po) /* LT or EQ */
503 else if (!--i && tup < po) /* GT or EQ */
505 else if (!--i && /* Out of range */
506 (tup >= po && tup <= ft->ftu_dtop))
508 else if (!--i && /* In range */
509 (tup <= po || tup >= ft->ftu_dtop))
513 * compare source ports
515 if (err && (i = (int)ft->ftu_scmp)) {
517 tup = fin->fin_data[0];
518 if (!--i && tup != po)
520 else if (!--i && tup == po)
522 else if (!--i && tup >= po)
524 else if (!--i && tup <= po)
526 else if (!--i && tup > po)
528 else if (!--i && tup < po)
530 else if (!--i && /* Out of range */
531 (tup >= po && tup <= ft->ftu_stop))
533 else if (!--i && /* In range */
534 (tup <= po || tup >= ft->ftu_stop))
539 * If we don't have all the TCP/UDP header, then how can we
540 * expect to do any sort of match on it ? If we were looking for
541 * TCP flags, then NO match. If not, then match (which should
542 * satisfy the "short" class too).
544 if (err && (fin->fin_fi.fi_p == IPPROTO_TCP)) {
545 if (fin->fin_fl & FI_SHORT)
546 return !(ft->ftu_tcpf | ft->ftu_tcpfm);
548 * Match the flags ? If not, abort this match.
551 ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) {
552 FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf,
553 ft->ftu_tcpfm, ft->ftu_tcpf));
561 * Check the input/output list of rules for a match and result.
562 * Could be per interface, but this gets real nasty when you don't have
565 int fr_scanlist(passin, ip, fin, m)
572 fr_ip_t *fi = &fin->fin_fi;
573 int rulen, portcmp = 0, off, skip = 0, logged = 0;
574 u_32_t pass, passt, passl;
583 if ((fi->fi_fl & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
586 for (rulen = 0; fr; fr = fr->fr_next, rulen++) {
588 FR_VERBOSE(("%d (%#x)\n", skip, fr->fr_flags));
593 * In all checks below, a null (zero) value in the
594 * filter struture is taken to mean a wildcard.
596 * check that we are working for the right interface
600 if (fin->fin_out != 0) {
602 (fr->fr_oifa != ((mb_t *)m)->m_pkthdr.rcvif)))
607 if (opts & (OPT_VERBOSE|OPT_DEBUG))
611 FR_VERBOSE(("%c", fr->fr_skip ? 's' :
612 (pass & FR_PASS) ? 'p' :
613 (pass & FR_AUTH) ? 'a' :
614 (pass & FR_ACCOUNT) ? 'A' :
615 (pass & FR_NOMATCH) ? 'n' : 'b'));
617 if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
622 u_32_t *ld, *lm, *lip;
626 lm = (u_32_t *)&fr->fr_mip;
627 ld = (u_32_t *)&fr->fr_ip;
628 i = ((*lip & *lm) != *ld);
629 FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
634 * We now know whether the packet version and the
635 * rule version match, along with protocol, ttl and
640 * Unrolled loops (4 each, for 32 bits).
642 FR_DEBUG(("1a. %#08x & %#08x != %#08x\n",
644 i |= ((*lip++ & *lm++) != *ld++) << 5;
646 FR_DEBUG(("1b. %#08x & %#08x != %#08x\n",
648 i |= ((*lip++ & *lm++) != *ld++) << 5;
649 FR_DEBUG(("1c. %#08x & %#08x != %#08x\n",
651 i |= ((*lip++ & *lm++) != *ld++) << 5;
652 FR_DEBUG(("1d. %#08x & %#08x != %#08x\n",
654 i |= ((*lip++ & *lm++) != *ld++) << 5;
660 i ^= (fr->fr_flags & FR_NOTSRCIP);
663 FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
665 i |= ((*lip++ & *lm++) != *ld++) << 6;
667 FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
669 i |= ((*lip++ & *lm++) != *ld++) << 6;
670 FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
672 i |= ((*lip++ & *lm++) != *ld++) << 6;
673 FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
675 i |= ((*lip++ & *lm++) != *ld++) << 6;
681 i ^= (fr->fr_flags & FR_NOTDSTIP);
684 FR_DEBUG(("3. %#08x & %#08x != %#08x\n",
686 i |= ((*lip++ & *lm++) != *ld++);
687 FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
689 i |= ((*lip & *lm) != *ld);
695 * If a fragment, then only the first has what we're looking
698 if (!portcmp && (fr->fr_dcmp || fr->fr_scmp || fr->fr_tcpf ||
701 if (fi->fi_fl & FI_TCPUDP) {
702 if (!fr_tcpudpchk(&fr->fr_tuc, fin))
704 } else if (fr->fr_icmpm || fr->fr_icmp) {
705 if (((fi->fi_p != IPPROTO_ICMP) &&
706 (fi->fi_p != IPPROTO_ICMPV6)) || off ||
709 if ((fin->fin_data[0] & fr->fr_icmpm) != fr->fr_icmp) {
710 FR_DEBUG(("i. %#x & %#x != %#x\n",
711 fin->fin_data[0], fr->fr_icmpm,
718 if (fr->fr_flags & FR_NOMATCH) {
723 if (fr->fr_flags & FR_QUICK)
729 passt = fr->fr_flags;
732 #if (BSD >= 199306) && (defined(_KERNEL) || defined(KERNEL))
733 if (securelevel <= 0)
735 if ((passt & FR_CALLNOW) && fr->fr_func)
736 passt = (*fr->fr_func)(passt, ip, fin);
739 * Just log this packet...
741 if ((passt & FR_LOGMASK) == FR_LOG) {
742 if (!IPLLOG(passt, ip, fin, m)) {
743 if (passt & FR_LOGORBLOCK)
744 passt |= FR_BLOCK|FR_QUICK;
745 ATOMIC_INCL(frstats[fin->fin_out].fr_skip);
747 ATOMIC_INCL(frstats[fin->fin_out].fr_pkl);
750 #endif /* IPFILTER_LOG */
751 ATOMIC_INCL(fr->fr_hits);
752 if (passt & FR_ACCOUNT)
753 fr->fr_bytes += (U_QUAD_T)ip->ip_len;
755 fin->fin_icode = fr->fr_icode;
756 fin->fin_rule = rulen;
757 fin->fin_group = fr->fr_group;
758 if (fr->fr_grp != NULL) {
759 fin->fin_fr = fr->fr_grp;
760 passt = fr_scanlist(passt, ip, fin, m);
761 if (fin->fin_fr == NULL) {
762 fin->fin_rule = rulen;
763 fin->fin_group = fr->fr_group;
766 if (passt & FR_DONTCACHE)
769 if (!(skip = fr->fr_skip) && (passt & FR_LOGMASK) != FR_LOG)
771 FR_DEBUG(("pass %#x\n", pass));
772 if (passt & FR_QUICK)
776 pass |= FR_DONTCACHE;
777 pass |= (fi->fi_fl << 24);
783 * frcheck - filter check
784 * check using source and destination addresses/ports in a packet whether
785 * or not to pass it on or not.
787 int fr_check(ip, hlen, ifp, out
788 #if defined(_KERNEL) && SOLARIS
801 * The above really sucks, but short of writing a diff
803 fr_info_t frinfo, *fc;
804 fr_info_t *fin = &frinfo;
805 int changed, error = EHOSTUNREACH, v = ip->ip_v;
806 frentry_t *fr = NULL, *list;
808 #if !SOLARIS || !defined(_KERNEL)
813 int p, len, drop = 0, logit = 0;
815 # if !defined(__SVR4) && !defined(__svr4__)
821 # if !SOLARIS && !defined(NETBSD_PF) && \
822 (defined(__DragonFly__) || (defined(__FreeBSD__) && (__FreeBSD_version < 500011)) || \
823 defined(__OpenBSD__) || defined(_BSDI_VERSION))
824 if (fr_checkp != fr_check && fr_running > 0) {
825 static int counter = 0;
828 printf("WARNING: fr_checkp corrupt: value %lx\n",
830 printf("WARNING: fr_checkp should be %lx\n",
832 printf("WARNING: fixing fr_checkp\n");
834 fr_checkp = fr_check;
836 if (counter == 10000)
843 * XXX For now, IP Filter and fast-forwarding of cached flows
844 * XXX are mutually exclusive. Eventually, IP Filter should
845 * XXX get a "can-fast-forward" filter rule.
847 m->m_flags &= ~M_CANFASTFWD;
848 # endif /* M_CANFASTFWD */
849 # ifdef CSUM_DELAY_DATA
851 * disable delayed checksums.
853 if ((out != 0) && (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)) {
855 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
857 # endif /* CSUM_DELAY_DATA */
861 len = ntohs(((ip6_t*)ip)->ip6_plen);
863 return -1; /* potential jumbo gram */
864 len += sizeof(ip6_t);
865 p = ((ip6_t *)ip)->ip6_nxt;
873 if ((p == IPPROTO_TCP || p == IPPROTO_UDP ||
874 (v == 4 && p == IPPROTO_ICMP)
876 || (v == 6 && p == IPPROTO_ICMPV6)
881 if ((v == 6) || (ip->ip_off & IP_OFFMASK) == 0)
885 plen = sizeof(tcphdr_t);
888 plen = sizeof(udphdr_t);
890 /* 96 - enough for complete ICMP error IP header */
892 plen = ICMPERR_MAXPKTLEN - sizeof(ip_t);
898 case IPPROTO_ICMPV6 :
900 * XXX does not take intermediate header
903 plen = ICMP6ERR_MINPKTLEN + 8 - sizeof(ip6_t);
907 up = MIN(hlen + plen, len);
911 /* Under IRIX, avoid m_pullup as it makes ping <hostname> panic */
912 if ((up > sizeof(hbuf)) || (m_length(m) < up)) {
913 ATOMIC_INCL(frstats[out].fr_pull[1]);
916 m_copydata(m, 0, up, hbuf);
917 ATOMIC_INCL(frstats[out].fr_pull[0]);
921 if ((*mp = m_pullup(m, up)) == 0) {
922 ATOMIC_INCL(frstats[out].fr_pull[1]);
925 ATOMIC_INCL(frstats[out].fr_pull[0]);
927 ip = mtod(m, ip_t *);
935 # endif /* !defined(__SVR4) && !defined(__svr4__) */
951 fr_makefrip(hlen, ip, fin);
956 ATOMIC_INCL(frstats[0].fr_ipv6[out]);
957 if (((ip6_t *)ip)->ip6_hlim < fr_minttl) {
958 ATOMIC_INCL(frstats[0].fr_badttl);
959 if (fr_minttllog & 1)
961 if (fr_minttllog & 2)
967 if (fr_chksrc && !fr_verifysrc(ip->ip_src, ifp)) {
968 ATOMIC_INCL(frstats[0].fr_badsrc);
973 } else if (ip->ip_ttl < fr_minttl) {
974 ATOMIC_INCL(frstats[0].fr_badttl);
975 if (fr_minttllog & 1)
977 if (fr_minttllog & 2)
984 fin->fin_group = logit;
985 pass = FR_INQUE|FR_NOMATCH|FR_LOGB;
986 (void) IPLLOG(pass, ip, fin, m);
996 if (fin->fin_fl & FI_SHORT) {
997 ATOMIC_INCL(frstats[out].fr_short);
1000 READ_ENTER(&ipf_mutex);
1003 * Check auth now. This, combined with the check below to see if apass
1004 * is 0 is to ensure that we don't count the packet twice, which can
1005 * otherwise occur when we reprocess it. As it is, we only count it
1006 * after it has no auth. table matchup. This also stops NAT from
1007 * occuring until after the packet has been auth'd.
1009 apass = fr_checkauth(ip, fin);
1014 list = ipacct6[0][fr_active];
1017 list = ipacct[0][fr_active];
1018 changed = ip_natin(ip, fin);
1019 if (!apass && (fin->fin_fr = list) &&
1020 (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1021 ATOMIC_INCL(frstats[0].fr_acct);
1026 if ((fin->fin_fl & FI_FRAG) == FI_FRAG)
1027 fr = ipfr_knownfrag(ip, fin);
1028 if (!fr && !(fin->fin_fl & FI_SHORT))
1029 fr = fr_checkstate(ip, fin);
1031 pass = fr->fr_flags;
1032 if (fr && (pass & FR_LOGFIRST))
1033 pass &= ~(FR_LOGFIRST|FR_LOG);
1038 * If a packet is found in the auth table, then skip checking
1039 * the access lists for permission but we do need to consider
1040 * the result as if it were from the ACL's.
1044 if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) {
1046 * copy cached data so we can unlock the mutex
1049 bcopy((char *)fc, (char *)fin, FI_COPYSIZE);
1050 ATOMIC_INCL(frstats[out].fr_chit);
1051 if ((fr = fin->fin_fr)) {
1052 ATOMIC_INCL(fr->fr_hits);
1053 pass = fr->fr_flags;
1058 list = ipfilter6[out][fr_active];
1061 list = ipfilter[out][fr_active];
1062 if ((fin->fin_fr = list))
1063 pass = fr_scanlist(fr_pass, ip, fin, m);
1064 if (!(pass & (FR_KEEPSTATE|FR_DONTCACHE)))
1065 bcopy((char *)fin, (char *)fc,
1067 if (pass & FR_NOMATCH) {
1068 ATOMIC_INCL(frstats[out].fr_nom);
1077 * If we fail to add a packet to the authorization queue,
1078 * then we drop the packet later. However, if it was added
1079 * then pretend we've dropped it already.
1081 if ((pass & FR_AUTH)) {
1082 if (fr_newauth((mb_t *)m, fin, ip) != 0) {
1089 if (pass & FR_PREAUTH) {
1090 READ_ENTER(&ipf_auth);
1091 if ((fin->fin_fr = ipauth) &&
1092 (pass = fr_scanlist(0, ip, fin, m))) {
1093 ATOMIC_INCL(fr_authstats.fas_hits);
1095 ATOMIC_INCL(fr_authstats.fas_miss);
1097 RWLOCK_EXIT(&ipf_auth);
1101 if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) {
1102 if (fin->fin_fl & FI_FRAG) {
1103 if (ipfr_newfrag(ip, fin) == -1) {
1104 ATOMIC_INCL(frstats[out].fr_bnfr);
1106 ATOMIC_INCL(frstats[out].fr_nfr);
1109 ATOMIC_INCL(frstats[out].fr_cfr);
1112 if (pass & FR_KEEPSTATE) {
1113 if (fr_addstate(ip, fin, NULL, 0) == NULL) {
1114 ATOMIC_INCL(frstats[out].fr_bads);
1116 ATOMIC_INCL(frstats[out].fr_ads);
1119 } else if (fr != NULL) {
1120 pass = fr->fr_flags;
1121 if (pass & FR_LOGFIRST)
1122 pass &= ~(FR_LOGFIRST|FR_LOG);
1125 #if (BSD >= 199306) && (defined(_KERNEL) || defined(KERNEL))
1126 if (securelevel <= 0)
1128 if (fr && fr->fr_func && !(pass & FR_CALLNOW))
1129 pass = (*fr->fr_func)(pass, ip, fin);
1132 * Only count/translate packets which will be passed on, out the
1135 if (out && (pass & FR_PASS)) {
1138 list = ipacct6[1][fr_active];
1141 list = ipacct[1][fr_active];
1146 sg = fin->fin_group;
1148 if (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT) {
1149 ATOMIC_INCL(frstats[1].fr_acct);
1151 fin->fin_group = sg;
1155 changed = ip_natout(ip, fin);
1158 RWLOCK_EXIT(&ipf_mutex);
1161 if ((fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
1162 if ((fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
1163 pass |= FF_LOGNOMATCH;
1164 ATOMIC_INCL(frstats[out].fr_npkl);
1166 } else if (((pass & FR_LOGMASK) == FR_LOGP) ||
1167 ((pass & FR_PASS) && (fr_flags & FF_LOGPASS))) {
1168 if ((pass & FR_LOGMASK) != FR_LOGP)
1170 ATOMIC_INCL(frstats[out].fr_ppkl);
1172 } else if (((pass & FR_LOGMASK) == FR_LOGB) ||
1173 ((pass & FR_BLOCK) && (fr_flags & FF_LOGBLOCK))) {
1174 if ((pass & FR_LOGMASK) != FR_LOGB)
1175 pass |= FF_LOGBLOCK;
1176 ATOMIC_INCL(frstats[out].fr_bpkl);
1178 if (!IPLLOG(pass, ip, fin, m)) {
1179 ATOMIC_INCL(frstats[out].fr_skip);
1180 if ((pass & (FR_PASS|FR_LOGORBLOCK)) ==
1181 (FR_PASS|FR_LOGORBLOCK))
1182 pass ^= FR_PASS|FR_BLOCK;
1186 #endif /* IPFILTER_LOG */
1190 * Only allow FR_DUP to work if a rule matched - it makes no sense to
1191 * set FR_DUP as a "default" as there are no instructions about where
1192 * to send the packet.
1194 if (fr && (pass & FR_DUP))
1198 # if defined(__OpenBSD__) && (OpenBSD >= 199905)
1199 mc = m_copym2(m, 0, M_COPYALL, M_DONTWAIT);
1201 mc = m_copy(m, 0, M_COPYALL);
1205 if (pass & FR_PASS) {
1206 ATOMIC_INCL(frstats[out].fr_pass);
1207 } else if (pass & FR_BLOCK) {
1208 ATOMIC_INCL(frstats[out].fr_block);
1210 * Should we return an ICMP packet to indicate error
1211 * status passing through the packet filter ?
1212 * WARNING: ICMP error packets AND TCP RST packets should
1213 * ONLY be sent in repsonse to incoming packets. Sending them
1214 * in response to outbound packets can result in a panic on
1215 * some operating systems.
1220 * If a packet results in a NAT error, do not
1221 * send a reset or ICMP error as it may disrupt
1222 * an existing flow. This is the proxy saying
1223 * the content is bad so just drop the packet
1227 else if (pass & FR_RETICMP) {
1230 if ((pass & FR_RETMASK) == FR_FAKEICMP)
1234 send_icmp_err(ip, ICMP_UNREACH, fin, dst);
1235 ATOMIC_INCL(frstats[0].fr_ret);
1236 } else if (((pass & FR_RETMASK) == FR_RETRST) &&
1237 !(fin->fin_fl & FI_SHORT)) {
1238 if (send_reset(ip, fin) == 0) {
1239 ATOMIC_INCL(frstats[1].fr_ret);
1243 if (pass & FR_RETRST)
1249 * If we didn't drop off the bottom of the list of rules (and thus
1250 * the 'current' rule fr is not NULL), then we may have some extra
1251 * instructions about what to do with a packet.
1252 * Once we're finished return to our caller, freeing the packet if
1253 * we are dropping it (* BSD ONLY *).
1255 if ((changed == -1) && (pass & FR_PASS)) {
1259 #if defined(_KERNEL)
1261 # if !defined(linux)
1263 frdest_t *fdp = &fr->fr_tif;
1265 if (((pass & FR_FASTROUTE) && !out) ||
1266 (fdp->fd_ifp && fdp->fd_ifp != (struct ifnet *)-1)) {
1267 (void) ipfr_fastroute(m, mp, fin, fdp);
1272 (void) ipfr_fastroute(mc, &mc, fin, &fr->fr_dif);
1275 if (!(pass & FR_PASS) && m) {
1280 else if (changed && up && m)
1281 m_copyback(m, 0, up, hbuf);
1283 # endif /* !linux */
1284 # else /* !SOLARIS */
1286 frdest_t *fdp = &fr->fr_tif;
1288 if (((pass & FR_FASTROUTE) && !out) ||
1289 (fdp->fd_ifp && fdp->fd_ifp != (struct ifnet *)-1))
1290 (void) ipfr_fastroute(ip, m, mp, fin, fdp);
1293 (void) ipfr_fastroute(ip, mc, &mc, fin, &fr->fr_dif);
1295 # endif /* !SOLARIS */
1296 return (pass & FR_PASS) ? 0 : error;
1298 if (pass & FR_NOMATCH)
1304 if ((pass & FR_RETMASK) == FR_RETRST)
1306 if ((pass & FR_RETMASK) == FR_RETICMP)
1308 if ((pass & FR_RETMASK) == FR_FAKEICMP)
1311 #endif /* _KERNEL */
1317 * addr should be 16bit aligned and len is in bytes.
1318 * length is in bytes
1320 u_short ipf_cksum(addr, len)
1326 for (sum = 0; len > 1; len -= 2)
1329 /* mop up an odd byte, if necessary */
1331 sum += *(u_char *)addr;
1334 * add back carry outs from top 16 bits to low 16 bits
1336 sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
1337 sum += (sum >> 16); /* add carry */
1338 return (u_short)(~sum);
1343 * NB: This function assumes we've pullup'd enough for all of the IP header
1344 * and the TCP header. We also assume that data blocks aren't allocated in
1347 u_short fr_tcpsum(m, ip, tcp)
1352 u_short *sp, slen, ts;
1357 * Add up IP Header portion
1359 hlen = ip->ip_hl << 2;
1360 slen = ip->ip_len - hlen;
1361 sum = htons((u_short)ip->ip_p);
1363 sp = (u_short *)&ip->ip_src;
1364 sum += *sp++; /* ip_src */
1366 sum += *sp++; /* ip_dst */
1372 sum2 = ip_cksum(m, hlen, sum); /* hlen == offset */
1373 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
1374 sum2 = ~sum2 & 0xffff;
1375 # else /* SOLARIS */
1376 # if defined(BSD) || defined(sun)
1383 sum2 = in_cksum(m, slen);
1391 * Both sum and sum2 are partial sums, so combine them together.
1393 sum = (sum & 0xffff) + (sum >> 16);
1394 sum = ~sum & 0xffff;
1396 sum2 = (sum2 & 0xffff) + (sum2 >> 16);
1397 # else /* defined(BSD) || defined(sun) */
1403 u_short len = ip->ip_len;
1409 * Add up IP Header portion
1411 sp = (u_short *)&ip->ip_src;
1412 len -= (ip->ip_hl << 2);
1413 sum = ntohs(IPPROTO_TCP);
1415 sum += *sp++; /* ip_src */
1417 sum += *sp++; /* ip_dst */
1419 if (sp != (u_short *)tcp)
1420 sp = (u_short *)tcp;
1421 sum += *sp++; /* sport */
1422 sum += *sp++; /* dport */
1423 sum += *sp++; /* seq */
1425 sum += *sp++; /* ack */
1427 sum += *sp++; /* off */
1428 sum += *sp++; /* win */
1429 sum += *sp++; /* Skip over checksum */
1430 sum += *sp++; /* urp */
1434 * In case we had to copy the IP & TCP header out of mbufs,
1435 * skip over the mbuf bits which are the header
1437 if ((caddr_t)ip != mtod(m, caddr_t)) {
1438 hlen = (caddr_t)sp - (caddr_t)ip;
1440 add = MIN(hlen, m->m_len);
1441 sp = (u_short *)(mtod(m, caddr_t) + add);
1443 if (add == m->m_len) {
1448 sp = mtod(m, u_short *);
1450 PANIC((!m),("fr_tcpsum(1): not enough data"));
1456 if (!(len -= sizeof(*tcp)))
1459 if (((caddr_t)sp - mtod(m, caddr_t)) >= m->m_len) {
1461 PANIC((!m),("fr_tcpsum(2): not enough data"));
1462 sp = mtod(m, u_short *);
1464 if (((caddr_t)(sp + 1) - mtod(m, caddr_t)) > m->m_len) {
1465 bytes.c[0] = *(u_char *)sp;
1467 PANIC((!m),("fr_tcpsum(3): not enough data"));
1468 sp = mtod(m, u_short *);
1469 bytes.c[1] = *(u_char *)sp;
1471 sp = (u_short *)((u_char *)sp + 1);
1473 if ((u_long)sp & 1) {
1474 bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s));
1481 sum += ntohs(*(u_char *)sp << 8);
1483 while (sum > 0xffff)
1484 sum = (sum & 0xffff) + (sum >> 16);
1485 sum2 = (u_short)(~sum & 0xffff);
1487 # endif /* defined(BSD) || defined(sun) */
1488 # endif /* SOLARIS */
1490 for (; slen > 1; slen -= 2)
1493 sum += ntohs(*(u_char *)sp << 8);
1494 while (sum > 0xffff)
1495 sum = (sum & 0xffff) + (sum >> 16);
1496 sum2 = (u_short)(~sum & 0xffff);
1503 #if defined(_KERNEL) && ( ((BSD < 199306) && !SOLARIS) || defined(__sgi) )
1505 * Copyright (c) 1982, 1986, 1988, 1991, 1993
1506 * The Regents of the University of California. All rights reserved.
1508 * Redistribution and use in source and binary forms, with or without
1509 * modification, are permitted provided that the following conditions
1511 * 1. Redistributions of source code must retain the above copyright
1512 * notice, this list of conditions and the following disclaimer.
1513 * 2. Redistributions in binary form must reproduce the above copyright
1514 * notice, this list of conditions and the following disclaimer in the
1515 * documentation and/or other materials provided with the distribution.
1516 * 3. All advertising materials mentioning features or use of this software
1517 * must display the following acknowledgement:
1518 * This product includes software developed by the University of
1519 * California, Berkeley and its contributors.
1520 * 4. Neither the name of the University nor the names of its contributors
1521 * may be used to endorse or promote products derived from this software
1522 * without specific prior written permission.
1524 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1525 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1526 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1527 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1528 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1529 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1530 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1531 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1532 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1533 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1536 * @(#)uipc_mbuf.c 8.2 (Berkeley) 1/4/94
1537 * $Id: fil.c,v 2.35.2.67 2002/12/06 13:28:05 darrenr Exp $
1540 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1541 * continuing for "len" bytes, into the indicated buffer.
1544 m_copydata(m, off, len, cp)
1552 if (off < 0 || len < 0)
1553 panic("m_copydata");
1556 panic("m_copydata");
1564 panic("m_copydata");
1565 count = MIN(m->m_len - off, len);
1566 bcopy(mtod(m, caddr_t) + off, cp, count);
1577 * Copy data from a buffer back into the indicated mbuf chain,
1578 * starting "off" bytes from the beginning, extending the mbuf
1579 * chain if necessary.
1582 m_copyback(m0, off, len, cp)
1589 struct mbuf *m = m0, *n;
1594 while (off > (mlen = m->m_len)) {
1597 if (m->m_next == 0) {
1598 #ifdef __DragonFly__
1599 n = m_getclr(MB_DONTWAIT, m->m_type);
1601 n = m_getclr(M_DONTWAIT, m->m_type);
1605 n->m_len = min(MLEN, len + off);
1611 mlen = min (m->m_len - off, len);
1612 bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
1620 if (m->m_next == 0) {
1621 #ifdef __DragonFly__
1622 n = m_get(MB_DONTWAIT, m->m_type);
1624 n = m_get(M_DONTWAIT, m->m_type);
1628 n->m_len = min(MLEN, len);
1635 if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1636 m->m_pkthdr.len = totlen;
1641 #endif /* (_KERNEL) && ( ((BSD < 199306) && !SOLARIS) || __sgi) */
1644 frgroup_t *fr_findgroup(num, flags, which, set, fgpp)
1650 frgroup_t *fg, **fgp;
1652 if (which == IPL_LOGAUTH)
1653 fgp = &ipfgroups[2][set];
1654 else if (flags & FR_ACCOUNT)
1655 fgp = &ipfgroups[1][set];
1656 else if (flags & (FR_OUTQUE|FR_INQUE))
1657 fgp = &ipfgroups[0][set];
1662 if (fg->fg_num == num)
1672 frgroup_t *fr_addgroup(num, fp, which, set)
1678 frgroup_t *fg, **fgp;
1680 if ((fg = fr_findgroup(num, fp->fr_flags, which, set, &fgp)))
1683 KMALLOC(fg, frgroup_t *);
1688 fg->fg_start = &fp->fr_grp;
1695 void fr_delgroup(num, flags, which, set)
1700 frgroup_t *fg, **fgp;
1702 if (!(fg = fr_findgroup(num, flags, which, set, &fgp)))
1712 * recursively flush rules from the list, descending groups as they are
1713 * encountered. if a rule is the head of a group and it has lost all its
1714 * group members, then also delete the group reference.
1716 static int frflushlist(set, unit, nfreedp, listp)
1725 while ((fp = *listp)) {
1726 *listp = fp->fr_next;
1728 i = frflushlist(set, unit, nfreedp, &fp->fr_grp);
1729 MUTEX_ENTER(&ipf_rw);
1731 MUTEX_EXIT(&ipf_rw);
1734 ATOMIC_DEC32(fp->fr_ref);
1735 if (fp->fr_grhead) {
1736 fr_delgroup(fp->fr_grhead, fp->fr_flags,
1740 if (fp->fr_ref == 0) {
1751 int frflush(unit, proto, flags)
1755 int flushed = 0, set;
1757 if (unit != IPL_LOGIPF)
1759 WRITE_ENTER(&ipf_mutex);
1760 bzero((char *)frcache, sizeof(frcache[0]) * 2);
1763 if (flags & FR_INACTIVE)
1766 if (flags & FR_OUTQUE) {
1768 if (proto == 0 || proto == 6) {
1769 (void) frflushlist(set, unit,
1770 &flushed, &ipfilter6[1][set]);
1771 (void) frflushlist(set, unit,
1772 &flushed, &ipacct6[1][set]);
1775 if (proto == 0 || proto == 4) {
1776 (void) frflushlist(set, unit,
1777 &flushed, &ipfilter[1][set]);
1778 (void) frflushlist(set, unit,
1779 &flushed, &ipacct[1][set]);
1782 if (flags & FR_INQUE) {
1784 if (proto == 0 || proto == 6) {
1785 (void) frflushlist(set, unit,
1786 &flushed, &ipfilter6[0][set]);
1787 (void) frflushlist(set, unit,
1788 &flushed, &ipacct6[0][set]);
1791 if (proto == 0 || proto == 4) {
1792 (void) frflushlist(set, unit,
1793 &flushed, &ipfilter[0][set]);
1794 (void) frflushlist(set, unit,
1795 &flushed, &ipacct[0][set]);
1798 RWLOCK_EXIT(&ipf_mutex);
1803 char *memstr(src, dst, slen, dlen)
1809 while (dlen >= slen) {
1810 if (bcmp(src, dst, slen) == 0) {
1821 void fixskip(listp, rp, addremove)
1822 frentry_t **listp, *rp;
1826 int rules = 0, rn = 0;
1828 for (fp = *listp; fp && (fp != rp); fp = fp->fr_next, rules++)
1834 for (fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
1835 if (fp->fr_skip && (rn + fp->fr_skip >= rules))
1836 fp->fr_skip += addremove;
1842 * count consecutive 1's in bit mask. If the mask generated by counting
1843 * consecutive 1's is different to that passed, return -1, else return #
1852 ip = ipn = ntohl(ip);
1853 for (i = 32; i; i--, ipn *= 2)
1854 if (ipn & 0x80000000)
1859 for (i = 32, j = cnt; i; i--, j--) {
1871 * return the first IP Address associated with an interface
1873 int fr_ifpaddr(v, ifptr, inp)
1876 struct in_addr *inp;
1879 struct in6_addr *inp6 = NULL;
1884 struct ifnet *ifp = ifptr;
1891 struct in6_addr in6;
1894 * First is always link local.
1896 if (ill->ill_ipif->ipif_next)
1897 in6 = ill->ill_ipif->ipif_next->ipif_v6lcl_addr;
1899 bzero((char *)&in6, sizeof(in6));
1900 bcopy((char *)&in6, (char *)inp, sizeof(in6));
1904 in.s_addr = ill->ill_ipif->ipif_local_addr;
1907 # else /* SOLARIS */
1911 struct sockaddr_in *sin;
1914 # if defined(__DragonFly__) || (__FreeBSD_version >= 300000)
1915 ifa = TAILQ_FIRST(&ifp->if_addrhead);
1917 # if defined(__NetBSD__) || defined(__OpenBSD__)
1918 ifa = ifp->if_addrlist.tqh_first;
1920 # if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
1921 ifa = &((struct in_ifaddr *)ifp->in_ifaddr)->ia_ifa;
1923 ifa = ifp->if_addrlist;
1925 # endif /* __NetBSD__ || __OpenBSD__ */
1926 # endif /* __FreeBSD_version >= 300000 */
1927 # if (BSD < 199306) && !(/*IRIX6*/defined(__sgi) && defined(IFF_DRVRLOCK))
1928 sin = (struct sockaddr_in *)&ifa->ifa_addr;
1930 sin = (struct sockaddr_in *)ifa->ifa_addr;
1931 while (sin && ifa) {
1932 if ((v == 4) && (sin->sin_family == AF_INET))
1935 if ((v == 6) && (sin->sin_family == AF_INET6)) {
1936 inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1937 if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1938 !IN6_IS_ADDR_LOOPBACK(inp6))
1942 # if defined(__DragonFly__) || (__FreeBSD_version >= 300000)
1943 ifa = TAILQ_NEXT(ifa, ifa_link);
1945 # if defined(__NetBSD__) || defined(__OpenBSD__)
1946 ifa = ifa->ifa_list.tqe_next;
1948 ifa = ifa->ifa_next;
1950 # endif /* __FreeBSD_version >= 300000 */
1952 sin = (struct sockaddr_in *)ifa->ifa_addr;
1958 # endif /* (BSD < 199306) && (!__sgi && IFF_DRVLOCK) */
1961 bcopy((char *)inp6, (char *)inp, sizeof(*inp6));
1969 # endif /* SOLARIS */
1974 static void frsynclist(fr)
1977 for (; fr; fr = fr->fr_next) {
1978 if (fr->fr_ifa != NULL) {
1979 fr->fr_ifa = GETUNIT(fr->fr_ifname, fr->fr_ip.fi_v);
1980 if (fr->fr_ifa == NULL)
1981 fr->fr_ifa = (void *)-1;
1984 frsynclist(fr->fr_grp);
1994 # if defined(__DragonFly__) || defined(__OpenBSD__) || ((NetBSD >= 199511) && (NetBSD < 1991011)) || \
1995 (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
1996 # if (NetBSD >= 199905) || defined(__OpenBSD__)
1997 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
1999 for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
2002 for (ifp = ifnet; ifp; ifp = ifp->if_next)
2008 ip_natsync((struct ifnet *)-1);
2009 # endif /* !SOLARIS */
2011 WRITE_ENTER(&ipf_mutex);
2012 frsynclist(ipacct[0][fr_active]);
2013 frsynclist(ipacct[1][fr_active]);
2014 frsynclist(ipfilter[0][fr_active]);
2015 frsynclist(ipfilter[1][fr_active]);
2017 frsynclist(ipacct6[0][fr_active]);
2018 frsynclist(ipacct6[1][fr_active]);
2019 frsynclist(ipfilter6[0][fr_active]);
2020 frsynclist(ipfilter6[1][fr_active]);
2022 RWLOCK_EXIT(&ipf_mutex);
2027 * In the functions below, bcopy() is called because the pointer being
2028 * copied _from_ in this instance is a pointer to a char buf (which could
2029 * end up being unaligned) and on the kernel's local stack.
2031 int ircopyptr(a, b, c)
2039 if (copyin(a, (char *)&ca, sizeof(ca)))
2042 bcopy(a, &ca, sizeof(ca));
2044 err = copyin(ca, b, c);
2051 int iwcopyptr(a, b, c)
2059 if (copyin(b, (char *)&ca, sizeof(ca)))
2062 bcopy(b, &ca, sizeof(ca));
2064 err = copyout(a, ca, c);
2074 * return the first IP Address associated with an interface
2076 int fr_ifpaddr(v, ifptr, inp)
2079 struct in_addr *inp;
2085 int ircopyptr(a, b, c)
2091 bcopy(a, &ca, sizeof(ca));
2097 int iwcopyptr(a, b, c)
2103 bcopy(b, &ca, sizeof(ca));
2112 int fr_lock(data, lockp)
2118 error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
2120 error = IWCOPY((caddr_t)lockp, data, sizeof(*lockp));
2128 void fr_getstat(fiop)
2131 bcopy((char *)frstats, (char *)fiop->f_st, sizeof(filterstats_t) * 2);
2132 fiop->f_locks[0] = fr_state_lock;
2133 fiop->f_locks[1] = fr_nat_lock;
2134 fiop->f_locks[2] = fr_frag_lock;
2135 fiop->f_locks[3] = fr_auth_lock;
2136 fiop->f_fin[0] = ipfilter[0][0];
2137 fiop->f_fin[1] = ipfilter[0][1];
2138 fiop->f_fout[0] = ipfilter[1][0];
2139 fiop->f_fout[1] = ipfilter[1][1];
2140 fiop->f_acctin[0] = ipacct[0][0];
2141 fiop->f_acctin[1] = ipacct[0][1];
2142 fiop->f_acctout[0] = ipacct[1][0];
2143 fiop->f_acctout[1] = ipacct[1][1];
2145 fiop->f_fin6[0] = ipfilter6[0][0];
2146 fiop->f_fin6[1] = ipfilter6[0][1];
2147 fiop->f_fout6[0] = ipfilter6[1][0];
2148 fiop->f_fout6[1] = ipfilter6[1][1];
2149 fiop->f_acctin6[0] = ipacct6[0][0];
2150 fiop->f_acctin6[1] = ipacct6[0][1];
2151 fiop->f_acctout6[0] = ipacct6[1][0];
2152 fiop->f_acctout6[1] = ipacct6[1][1];
2154 fiop->f_fin6[0] = NULL;
2155 fiop->f_fin6[1] = NULL;
2156 fiop->f_fout6[0] = NULL;
2157 fiop->f_fout6[1] = NULL;
2158 fiop->f_acctin6[0] = NULL;
2159 fiop->f_acctin6[1] = NULL;
2160 fiop->f_acctout6[0] = NULL;
2161 fiop->f_acctout6[1] = NULL;
2163 fiop->f_active = fr_active;
2164 fiop->f_froute[0] = ipl_frouteok[0];
2165 fiop->f_froute[1] = ipl_frouteok[1];
2167 fiop->f_running = fr_running;
2168 fiop->f_groups[0][0] = ipfgroups[0][0];
2169 fiop->f_groups[0][1] = ipfgroups[0][1];
2170 fiop->f_groups[1][0] = ipfgroups[1][0];
2171 fiop->f_groups[1][1] = ipfgroups[1][1];
2172 fiop->f_groups[2][0] = ipfgroups[2][0];
2173 fiop->f_groups[2][1] = ipfgroups[2][1];
2175 fiop->f_logging = 1;
2177 fiop->f_logging = 0;
2179 fiop->f_defpass = fr_pass;
2180 strncpy(fiop->f_version, ipfilter_version, sizeof(fiop->f_version));
2185 int icmptoicmp6types[ICMP_MAXTYPE+1] = {
2186 ICMP6_ECHO_REPLY, /* 0: ICMP_ECHOREPLY */
2189 ICMP6_DST_UNREACH, /* 3: ICMP_UNREACH */
2190 -1, /* 4: ICMP_SOURCEQUENCH */
2191 ND_REDIRECT, /* 5: ICMP_REDIRECT */
2194 ICMP6_ECHO_REQUEST, /* 8: ICMP_ECHO */
2196 -1, /* 10: UNUSED */
2197 ICMP6_TIME_EXCEEDED, /* 11: ICMP_TIMXCEED */
2198 ICMP6_PARAM_PROB, /* 12: ICMP_PARAMPROB */
2199 -1, /* 13: ICMP_TSTAMP */
2200 -1, /* 14: ICMP_TSTAMPREPLY */
2201 -1, /* 15: ICMP_IREQ */
2202 -1, /* 16: ICMP_IREQREPLY */
2203 -1, /* 17: ICMP_MASKREQ */
2204 -1, /* 18: ICMP_MASKREPLY */
2208 int icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
2209 ICMP6_DST_UNREACH_ADDR, /* 0: ICMP_UNREACH_NET */
2210 ICMP6_DST_UNREACH_ADDR, /* 1: ICMP_UNREACH_HOST */
2211 -1, /* 2: ICMP_UNREACH_PROTOCOL */
2212 ICMP6_DST_UNREACH_NOPORT, /* 3: ICMP_UNREACH_PORT */
2213 -1, /* 4: ICMP_UNREACH_NEEDFRAG */
2214 ICMP6_DST_UNREACH_NOTNEIGHBOR, /* 5: ICMP_UNREACH_SRCFAIL */
2215 ICMP6_DST_UNREACH_ADDR, /* 6: ICMP_UNREACH_NET_UNKNOWN */
2216 ICMP6_DST_UNREACH_ADDR, /* 7: ICMP_UNREACH_HOST_UNKNOWN */
2217 -1, /* 8: ICMP_UNREACH_ISOLATED */
2218 ICMP6_DST_UNREACH_ADMIN, /* 9: ICMP_UNREACH_NET_PROHIB */
2219 ICMP6_DST_UNREACH_ADMIN, /* 10: ICMP_UNREACH_HOST_PROHIB */
2220 -1, /* 11: ICMP_UNREACH_TOSNET */
2221 -1, /* 12: ICMP_UNREACH_TOSHOST */
2222 ICMP6_DST_UNREACH_ADMIN, /* 13: ICMP_UNREACH_ADMIN_PROHIBIT */