fil.c revision 67614
1/*
2 * Copyright (C) 1993-2000 by Darren Reed.
3 *
4 * Redistribution and use in source and binary forms are permitted
5 * provided that this notice is preserved and due credit is given
6 * to the original author and the contributors.
7 */
8#if !defined(lint)
9static const char sccsid[] = "@(#)fil.c	1.36 6/5/96 (C) 1993-1996 Darren Reed";
10/* static const char rcsid[] = "@(#)$Id: fil.c,v 2.3.2.16 2000/01/27 08:49:37 darrenr Exp $"; */
11static const char rcsid[] = "@(#)$FreeBSD: head/sys/contrib/ipfilter/netinet/fil.c 67614 2000-10-26 12:33:42Z darrenr $";
12#endif
13
14#include <sys/errno.h>
15#include <sys/types.h>
16#include <sys/param.h>
17#include <sys/time.h>
18#include <sys/file.h>
19#if defined(__NetBSD__) && (NetBSD >= 199905) && !defined(IPFILTER_LKM) && \
20    defined(_KERNEL)
21# include "opt_ipfilter_log.h"
22#endif
23#if (defined(KERNEL) || defined(_KERNEL)) && defined(__FreeBSD_version) && \
24    (__FreeBSD_version >= 220000)
25# if (__FreeBSD_version >= 400000)
26#  ifndef KLD_MODULE
27#   include "opt_inet6.h"
28#  endif
29#  if (__FreeBSD_version == 400019)
30#   define CSUM_DELAY_DATA
31#  endif
32# endif
33# include <sys/filio.h>
34# include <sys/fcntl.h>
35#else
36# include <sys/ioctl.h>
37#endif
38#if (defined(_KERNEL) || defined(KERNEL)) && !defined(linux)
39# include <sys/systm.h>
40#else
41# include <stdio.h>
42# include <string.h>
43# include <stdlib.h>
44#endif
45#include <sys/uio.h>
46#if !defined(__SVR4) && !defined(__svr4__)
47# ifndef linux
48#  include <sys/mbuf.h>
49# endif
50#else
51# include <sys/byteorder.h>
52# if SOLARIS2 < 5
53#  include <sys/dditypes.h>
54# endif
55#  include <sys/stream.h>
56#endif
57#ifndef linux
58# include <sys/protosw.h>
59# include <sys/socket.h>
60#endif
61#include <net/if.h>
62#ifdef sun
63# include <net/af.h>
64#endif
65#include <net/route.h>
66#include <netinet/in.h>
67#include <netinet/in_systm.h>
68#include <netinet/ip.h>
69#ifndef linux
70# include <netinet/ip_var.h>
71#endif
72#if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
73# include <sys/hashing.h>
74# include <netinet/in_var.h>
75#endif
76#include <netinet/tcp.h>
77#include <netinet/udp.h>
78#include <netinet/ip_icmp.h>
79#include "netinet/ip_compat.h"
80#ifdef	USE_INET6
81# include <netinet/icmp6.h>
82# if !SOLARIS && defined(_KERNEL)
83#  include <netinet6/in6_var.h>
84# endif
85#endif
86#include <netinet/tcpip.h>
87#include "netinet/ip_fil.h"
88#include "netinet/ip_proxy.h"
89#include "netinet/ip_nat.h"
90#include "netinet/ip_frag.h"
91#include "netinet/ip_state.h"
92#include "netinet/ip_auth.h"
93# if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
94#  include <sys/malloc.h>
95#  if defined(_KERNEL) && !defined(IPFILTER_LKM)
96#   include "opt_ipfilter.h"
97#  endif
98# endif
99#ifndef	MIN
100# define	MIN(a,b)	(((a)<(b))?(a):(b))
101#endif
102#include "netinet/ipl.h"
103
104#include <machine/in_cksum.h>
105
106#ifndef	_KERNEL
107# include "ipf.h"
108# include "ipt.h"
109extern	int	opts;
110
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)		ipllog()
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)
119extern	KRWLOCK_T	ipf_mutex, ipf_auth, ipf_nat;
120extern	kmutex_t	ipf_rw;
121# endif
122# if SOLARIS
123#  define	FR_NEWAUTH(m, fi, ip, qif)	fr_newauth((mb_t *)m, fi, \
124							   ip, qif)
125# else /* SOLARIS */
126#  define	FR_NEWAUTH(m, fi, ip, qif)	fr_newauth((mb_t *)m, fi, ip)
127# endif /* SOLARIS || __sgi */
128#endif /* _KERNEL */
129
130
131struct	filterstats frstats[2] = {{0,0,0,0,0},{0,0,0,0,0}};
132struct	frentry	*ipfilter[2][2] = { { NULL, NULL }, { NULL, NULL } },
133#ifdef	USE_INET6
134		*ipfilter6[2][2] = { { NULL, NULL }, { NULL, NULL } },
135		*ipacct6[2][2] = { { NULL, NULL }, { NULL, NULL } },
136#endif
137		*ipacct[2][2] = { { NULL, NULL }, { NULL, NULL } };
138struct	frgroup *ipfgroups[3][2];
139int	fr_flags = IPF_LOGGING;
140int	fr_active = 0;
141int	fr_chksrc = 0;
142#if defined(IPFILTER_DEFAULT_BLOCK)
143int	fr_pass = FR_NOMATCH|FR_BLOCK;
144#else
145int	fr_pass = (IPF_DEFAULT_PASS|FR_NOMATCH);
146#endif
147char	ipfilter_version[] = IPL_VERSION;
148
149fr_info_t	frcache[2];
150
151static	int	frflushlist __P((int, minor_t, int *, frentry_t **));
152#ifdef	_KERNEL
153static	void	frsynclist __P((frentry_t *));
154#endif
155
156
157/*
158 * bit values for identifying presence of individual IP options
159 */
160struct	optlist	ipopts[20] = {
161	{ IPOPT_NOP,	0x000001 },
162	{ IPOPT_RR,	0x000002 },
163	{ IPOPT_ZSU,	0x000004 },
164	{ IPOPT_MTUP,	0x000008 },
165	{ IPOPT_MTUR,	0x000010 },
166	{ IPOPT_ENCODE,	0x000020 },
167	{ IPOPT_TS,	0x000040 },
168	{ IPOPT_TR,	0x000080 },
169	{ IPOPT_SECURITY, 0x000100 },
170	{ IPOPT_LSRR,	0x000200 },
171	{ IPOPT_E_SEC,	0x000400 },
172	{ IPOPT_CIPSO,	0x000800 },
173	{ IPOPT_SATID,	0x001000 },
174	{ IPOPT_SSRR,	0x002000 },
175	{ IPOPT_ADDEXT,	0x004000 },
176	{ IPOPT_VISA,	0x008000 },
177	{ IPOPT_IMITD,	0x010000 },
178	{ IPOPT_EIP,	0x020000 },
179	{ IPOPT_FINN,	0x040000 },
180	{ 0,		0x000000 }
181};
182
183/*
184 * bit values for identifying presence of individual IP security options
185 */
186struct	optlist	secopt[8] = {
187	{ IPSO_CLASS_RES4,	0x01 },
188	{ IPSO_CLASS_TOPS,	0x02 },
189	{ IPSO_CLASS_SECR,	0x04 },
190	{ IPSO_CLASS_RES3,	0x08 },
191	{ IPSO_CLASS_CONF,	0x10 },
192	{ IPSO_CLASS_UNCL,	0x20 },
193	{ IPSO_CLASS_RES2,	0x40 },
194	{ IPSO_CLASS_RES1,	0x80 }
195};
196
197
198/*
199 * compact the IP header into a structure which contains just the info.
200 * which is useful for comparing IP headers with.
201 */
202void	fr_makefrip(hlen, ip, fin)
203int hlen;
204ip_t *ip;
205fr_info_t *fin;
206{
207	u_short optmsk = 0, secmsk = 0, auth = 0;
208	int i, mv, ol, off, p, plen, v;
209	fr_ip_t *fi = &fin->fin_fi;
210	struct optlist *op;
211	u_char *s, opt;
212	tcphdr_t *tcp;
213
214	fin->fin_rev = 0;
215	fin->fin_fr = NULL;
216	fin->fin_tcpf = 0;
217	fin->fin_data[0] = 0;
218	fin->fin_data[1] = 0;
219	fin->fin_rule = -1;
220	fin->fin_group = -1;
221#ifdef	_KERNEL
222	fin->fin_icode = ipl_unreach;
223#endif
224	v = fin->fin_v;
225	fi->fi_v = v;
226	fin->fin_hlen = hlen;
227	if (v == 4) {
228		fin->fin_id = ip->ip_id;
229		fi->fi_tos = ip->ip_tos;
230		off = (ip->ip_off & IP_OFFMASK) << 3;
231		tcp = (tcphdr_t *)((char *)ip + hlen);
232		(*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4));
233		fi->fi_src.i6[1] = 0;
234		fi->fi_src.i6[2] = 0;
235		fi->fi_src.i6[3] = 0;
236		fi->fi_dst.i6[1] = 0;
237		fi->fi_dst.i6[2] = 0;
238		fi->fi_dst.i6[3] = 0;
239		fi->fi_saddr = ip->ip_src.s_addr;
240		fi->fi_daddr = ip->ip_dst.s_addr;
241		p = ip->ip_p;
242		fi->fi_fl = (hlen > sizeof(ip_t)) ? FI_OPTIONS : 0;
243		if (ip->ip_off & 0x3fff)
244			fi->fi_fl |= FI_FRAG;
245		plen = ip->ip_len;
246		fin->fin_dlen = plen - hlen;
247	}
248#ifdef	USE_INET6
249	else if (v == 6) {
250		ip6_t *ip6 = (ip6_t *)ip;
251
252		off = 0;
253		p = ip6->ip6_nxt;
254		fi->fi_p = p;
255		fi->fi_ttl = ip6->ip6_hlim;
256		tcp = (tcphdr_t *)(ip6 + 1);
257		fi->fi_src.in6 = ip6->ip6_src;
258		fi->fi_dst.in6 = ip6->ip6_dst;
259		fin->fin_id = (u_short)(ip6->ip6_flow & 0xffff);
260		fi->fi_tos = 0;
261		fi->fi_fl = 0;
262		plen = ntohs(ip6->ip6_plen);
263		fin->fin_dlen = plen;
264	}
265#endif
266	else
267		return;
268
269	fin->fin_off = off;
270	fin->fin_plen = plen;
271	fin->fin_dp = (void *)tcp;
272
273	switch (p)
274	{
275	case IPPROTO_ICMP :
276	{
277		int minicmpsz = sizeof(struct icmp);
278		icmphdr_t *icmp;
279
280		if (fin->fin_dlen > 1)
281			fin->fin_data[0] = *(u_short *)tcp;
282
283		if ((!(plen >= hlen + minicmpsz) && !off) ||
284		    (off && off < sizeof(struct icmp))) {
285			fi->fi_fl |= FI_SHORT;
286			if (fin->fin_dlen < 2)
287				break;
288		}
289
290		icmp = (icmphdr_t *)tcp;
291
292		if (!off && (icmp->icmp_type == ICMP_ECHOREPLY ||
293		     icmp->icmp_type == ICMP_ECHO))
294			minicmpsz = ICMP_MINLEN;
295
296		/* type(1) + code(1) + cksum(2) + id(2) seq(2) +
297		 * 3*timestamp(3*4) */
298		else if (!off && (icmp->icmp_type == ICMP_TSTAMP ||
299		    icmp->icmp_type == ICMP_TSTAMPREPLY))
300			minicmpsz = 20;
301
302		/* type(1) + code(1) + cksum(2) + id(2) seq(2) + mask(4) */
303		else if (!off && (icmp->icmp_type == ICMP_MASKREQ ||
304		    icmp->icmp_type == ICMP_MASKREPLY))
305			minicmpsz = 12;
306
307		break;
308	}
309	case IPPROTO_TCP :
310		fi->fi_fl |= FI_TCPUDP;
311#ifdef	USE_INET6
312		if (v == 6) {
313			if (plen < sizeof(struct tcphdr))
314				fi->fi_fl |= FI_SHORT;
315		} else
316#endif
317		if (v == 4) {
318			if ((!IPMINLEN(ip, tcphdr) && !off) ||
319			     (off && off < sizeof(struct tcphdr)))
320				fi->fi_fl |= FI_SHORT;
321		}
322		if (!(fi->fi_fl & FI_SHORT) && !off)
323			fin->fin_tcpf = tcp->th_flags;
324		goto getports;
325	case IPPROTO_UDP :
326		fi->fi_fl |= FI_TCPUDP;
327#ifdef	USE_INET6
328		if (v == 6) {
329			if (plen < sizeof(struct udphdr))
330				fi->fi_fl |= FI_SHORT;
331		} else
332#endif
333		if (v == 4) {
334			if ((!IPMINLEN(ip, udphdr) && !off) ||
335			    (off && off < sizeof(struct udphdr)))
336				fi->fi_fl |= FI_SHORT;
337		}
338getports:
339		if (!off && (fin->fin_dlen > 3)) {
340			fin->fin_data[0] = ntohs(tcp->th_sport);
341			fin->fin_data[1] = ntohs(tcp->th_dport);
342		}
343		break;
344	default :
345		break;
346	}
347
348#ifdef	USE_INET6
349	if (v == 6) {
350		fi->fi_optmsk = 0;
351		fi->fi_secmsk = 0;
352		fi->fi_auth = 0;
353		return;
354	}
355#endif
356
357	for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
358		opt = *s;
359		if (opt == '\0')
360			break;
361		else if (opt == IPOPT_NOP)
362			ol = 1;
363		else {
364			if (hlen < 2)
365				break;
366			ol = (int)*(s + 1);
367			if (ol < 2 || ol > hlen)
368				break;
369		}
370		for (i = 9, mv = 4; mv >= 0; ) {
371			op = ipopts + i;
372			if (opt == (u_char)op->ol_val) {
373				optmsk |= op->ol_bit;
374				if (opt == IPOPT_SECURITY) {
375					struct optlist *sp;
376					u_char	sec;
377					int j, m;
378
379					sec = *(s + 2);	/* classification */
380					for (j = 3, m = 2; m >= 0; ) {
381						sp = secopt + j;
382						if (sec == sp->ol_val) {
383							secmsk |= sp->ol_bit;
384							auth = *(s + 3);
385							auth *= 256;
386							auth += *(s + 4);
387							break;
388						}
389						if (sec < sp->ol_val)
390							j -= m--;
391						else
392							j += m--;
393					}
394				}
395				break;
396			}
397			if (opt < op->ol_val)
398				i -= mv--;
399			else
400				i += mv--;
401		}
402		hlen -= ol;
403		s += ol;
404	}
405	if (auth && !(auth & 0x0100))
406		auth &= 0xff00;
407	fi->fi_optmsk = optmsk;
408	fi->fi_secmsk = secmsk;
409	fi->fi_auth = auth;
410}
411
412
413/*
414 * check an IP packet for TCP/UDP characteristics such as ports and flags.
415 */
416int fr_tcpudpchk(ft, fin)
417frtuc_t *ft;
418fr_info_t *fin;
419{
420	register u_short po, tup;
421	register char i;
422	register int err = 1;
423
424	/*
425	 * Both ports should *always* be in the first fragment.
426	 * So far, I cannot find any cases where they can not be.
427	 *
428	 * compare destination ports
429	 */
430	if ((i = (int)ft->ftu_dcmp)) {
431		po = ft->ftu_dport;
432		tup = fin->fin_data[1];
433		/*
434		 * Do opposite test to that required and
435		 * continue if that succeeds.
436		 */
437		if (!--i && tup != po) /* EQUAL */
438			err = 0;
439		else if (!--i && tup == po) /* NOTEQUAL */
440			err = 0;
441		else if (!--i && tup >= po) /* LESSTHAN */
442			err = 0;
443		else if (!--i && tup <= po) /* GREATERTHAN */
444			err = 0;
445		else if (!--i && tup > po) /* LT or EQ */
446			err = 0;
447		else if (!--i && tup < po) /* GT or EQ */
448			err = 0;
449		else if (!--i &&	   /* Out of range */
450			 (tup >= po && tup <= ft->ftu_dtop))
451			err = 0;
452		else if (!--i &&	   /* In range */
453			 (tup <= po || tup >= ft->ftu_dtop))
454			err = 0;
455	}
456	/*
457	 * compare source ports
458	 */
459	if (err && (i = (int)ft->ftu_scmp)) {
460		po = ft->ftu_sport;
461		tup = fin->fin_data[0];
462		if (!--i && tup != po)
463			err = 0;
464		else if (!--i && tup == po)
465			err = 0;
466		else if (!--i && tup >= po)
467			err = 0;
468		else if (!--i && tup <= po)
469			err = 0;
470		else if (!--i && tup > po)
471			err = 0;
472		else if (!--i && tup < po)
473			err = 0;
474		else if (!--i &&	   /* Out of range */
475			 (tup >= po && tup <= ft->ftu_stop))
476			err = 0;
477		else if (!--i &&	   /* In range */
478			 (tup <= po || tup >= ft->ftu_stop))
479			err = 0;
480	}
481
482	/*
483	 * If we don't have all the TCP/UDP header, then how can we
484	 * expect to do any sort of match on it ?  If we were looking for
485	 * TCP flags, then NO match.  If not, then match (which should
486	 * satisfy the "short" class too).
487	 */
488	if (err && (fin->fin_fi.fi_p == IPPROTO_TCP)) {
489		if (fin->fin_fi.fi_fl & FI_SHORT)
490			return !(ft->ftu_tcpf | ft->ftu_tcpfm);
491		/*
492		 * Match the flags ?  If not, abort this match.
493		 */
494		if (ft->ftu_tcpfm &&
495		    ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) {
496			FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf,
497				 ft->ftu_tcpfm, ft->ftu_tcpf));
498			err = 0;
499		}
500	}
501	return err;
502}
503
504/*
505 * Check the input/output list of rules for a match and result.
506 * Could be per interface, but this gets real nasty when you don't have
507 * kernel sauce.
508 */
509int fr_scanlist(pass, ip, fin, m)
510u_32_t pass;
511ip_t *ip;
512register fr_info_t *fin;
513void *m;
514{
515	register struct frentry *fr;
516	register fr_ip_t *fi = &fin->fin_fi;
517	int rulen, portcmp = 0, off, skip = 0, logged = 0;
518	u_32_t passt;
519
520	fr = fin->fin_fr;
521	fin->fin_fr = NULL;
522	fin->fin_rule = 0;
523	fin->fin_group = 0;
524	if (fin->fin_v == 4)
525		off = ip->ip_off & IP_OFFMASK;
526	else
527		off = 0;
528	pass |= (fi->fi_fl << 24);
529
530	if ((fi->fi_fl & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
531		portcmp = 1;
532
533	for (rulen = 0; fr; fr = fr->fr_next, rulen++) {
534		if (skip) {
535			skip--;
536			continue;
537		}
538		/*
539		 * In all checks below, a null (zero) value in the
540		 * filter struture is taken to mean a wildcard.
541		 *
542		 * check that we are working for the right interface
543		 */
544#ifdef	_KERNEL
545# if BSD >= 199306
546		if (fin->fin_out != 0) {
547			if ((fr->fr_oifa &&
548			     fr->fr_oifa != ((mb_t *)m)->m_pkthdr.rcvif) ||
549			    (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp))
550				continue;
551		} else
552# endif
553			if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
554				continue;
555#else
556		if (opts & (OPT_VERBOSE|OPT_DEBUG))
557			printf("\n");
558		FR_VERBOSE(("%c", (pass & FR_PASS) ? 'p' :
559				  (pass & FR_AUTH) ? 'a' : 'b'));
560		if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
561			continue;
562		FR_VERBOSE((":i"));
563#endif
564		{
565			register u_32_t	*ld, *lm, *lip;
566			register int i;
567
568			lip = (u_32_t *)fi;
569			lm = (u_32_t *)&fr->fr_mip;
570			ld = (u_32_t *)&fr->fr_ip;
571			i = ((*lip & *lm) != *ld);
572			FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
573				   *lip, *lm, *ld));
574			if (i)
575				continue;
576			/*
577			 * We now know whether the packet version and the
578			 * rule version match, along with protocol, ttl and
579			 * tos.
580			 */
581			lip++, lm++, ld++;
582			/*
583			 * Unrolled loops (4 each, for 32 bits).
584			 */
585			i |= ((*lip & *lm) != *ld) << 19;
586			FR_DEBUG(("1a. %#08x & %#08x != %#08x\n",
587				   *lip, *lm, *ld));
588			if (fi->fi_v == 6) {
589				lip++, lm++, ld++;
590				i |= ((*lip & *lm) != *ld) << 19;
591				FR_DEBUG(("1b. %#08x & %#08x != %#08x\n",
592					   *lip, *lm, *ld));
593				lip++, lm++, ld++;
594				i |= ((*lip & *lm) != *ld) << 19;
595				FR_DEBUG(("1c. %#08x & %#08x != %#08x\n",
596					   *lip, *lm, *ld));
597				lip++, lm++, ld++;
598				i |= ((*lip & *lm) != *ld) << 19;
599				FR_DEBUG(("1d. %#08x & %#08x != %#08x\n",
600					   *lip, *lm, *ld));
601			} else {
602				lip += 3;
603				lm += 3;
604				ld += 3;
605			}
606			i ^= (fr->fr_flags & FR_NOTSRCIP);
607			if (i)
608				continue;
609			lip++, lm++, ld++;
610			i |= ((*lip & *lm) != *ld) << 20;
611			FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
612				   *lip, *lm, *ld));
613			if (fi->fi_v == 6) {
614				lip++, lm++, ld++;
615				i |= ((*lip & *lm) != *ld) << 20;
616				FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
617					   *lip, *lm, *ld));
618				lip++, lm++, ld++;
619				i |= ((*lip & *lm) != *ld) << 20;
620				FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
621					   *lip, *lm, *ld));
622				lip++, lm++, ld++;
623				i |= ((*lip & *lm) != *ld) << 20;
624				FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
625					   *lip, *lm, *ld));
626			} else {
627				lip += 3;
628				lm += 3;
629				ld += 3;
630			}
631			i ^= (fr->fr_flags & FR_NOTDSTIP);
632			if (i)
633				continue;
634			lip++, lm++, ld++;
635			i |= ((*lip & *lm) != *ld);
636			FR_DEBUG(("3. %#08x & %#08x != %#08x\n",
637				   *lip, *lm, *ld));
638			lip++, lm++, ld++;
639			i |= ((*lip & *lm) != *ld);
640			FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
641				   *lip, *lm, *ld));
642			if (i)
643				continue;
644		}
645
646		/*
647		 * If a fragment, then only the first has what we're looking
648		 * for here...
649		 */
650		if (!portcmp && (fr->fr_dcmp || fr->fr_scmp || fr->fr_tcpf ||
651				 fr->fr_tcpfm))
652			continue;
653		if (fi->fi_fl & FI_TCPUDP) {
654			if (!fr_tcpudpchk(&fr->fr_tuc, fin))
655				continue;
656		} else if (fr->fr_icmpm || fr->fr_icmp) {
657			if ((fi->fi_p != IPPROTO_ICMP) || off ||
658			    (fin->fin_dlen < 2))
659				continue;
660			if ((fin->fin_data[0] & fr->fr_icmpm) != fr->fr_icmp) {
661				FR_DEBUG(("i. %#x & %#x != %#x\n",
662					 fin->fin_data[0], fr->fr_icmpm,
663					 fr->fr_icmp));
664				continue;
665			}
666		}
667		FR_VERBOSE(("*"));
668		/*
669		 * Just log this packet...
670		 */
671		passt = fr->fr_flags;
672#if (BSD >= 199306) && (defined(_KERNEL) || defined(KERNEL))
673		if (securelevel <= 0)
674#endif
675			if ((passt & FR_CALLNOW) && fr->fr_func)
676				passt = (*fr->fr_func)(passt, ip, fin);
677		fin->fin_fr = fr;
678#ifdef  IPFILTER_LOG
679		if ((passt & FR_LOGMASK) == FR_LOG) {
680			if (!IPLLOG(passt, ip, fin, m)) {
681				if (passt & FR_LOGORBLOCK)
682					passt |= FR_BLOCK|FR_QUICK;
683				ATOMIC_INCL(frstats[fin->fin_out].fr_skip);
684			}
685			ATOMIC_INCL(frstats[fin->fin_out].fr_pkl);
686			logged = 1;
687		}
688#endif /* IPFILTER_LOG */
689		if (!(skip = fr->fr_skip) && (passt & FR_LOGMASK) != FR_LOG)
690			pass = passt;
691		FR_DEBUG(("pass %#x\n", pass));
692		ATOMIC_INCL(fr->fr_hits);
693		if (pass & FR_ACCOUNT)
694			fr->fr_bytes += (U_QUAD_T)ip->ip_len;
695		else
696			fin->fin_icode = fr->fr_icode;
697		fin->fin_rule = rulen;
698		fin->fin_group = fr->fr_group;
699		if (fr->fr_grp) {
700			fin->fin_fr = fr->fr_grp;
701			pass = fr_scanlist(pass, ip, fin, m);
702			if (fin->fin_fr == NULL) {
703				fin->fin_rule = rulen;
704				fin->fin_group = fr->fr_group;
705				fin->fin_fr = fr;
706			}
707			if (pass & FR_DONTCACHE)
708				logged = 1;
709		}
710		if (pass & FR_QUICK)
711			break;
712	}
713	if (logged)
714		pass |= FR_DONTCACHE;
715	return pass;
716}
717
718
719/*
720 * frcheck - filter check
721 * check using source and destination addresses/ports in a packet whether
722 * or not to pass it on or not.
723 */
724int fr_check(ip, hlen, ifp, out
725#if defined(_KERNEL) && SOLARIS
726, qif, mp)
727qif_t *qif;
728#else
729, mp)
730#endif
731mb_t **mp;
732ip_t *ip;
733int hlen;
734void *ifp;
735int out;
736{
737	/*
738	 * The above really sucks, but short of writing a diff
739	 */
740	fr_info_t frinfo, *fc;
741	register fr_info_t *fin = &frinfo;
742	int changed, error = EHOSTUNREACH, v = ip->ip_v;
743	frentry_t *fr = NULL, *list;
744	u_32_t pass, apass;
745#if !SOLARIS || !defined(_KERNEL)
746	register mb_t *m = *mp;
747#endif
748
749#ifdef	_KERNEL
750	mb_t *mc = NULL;
751	int p, len;
752# if !defined(__SVR4) && !defined(__svr4__)
753#  ifdef __sgi
754	char hbuf[(0xf << 2) + sizeof(struct icmp) + sizeof(ip_t) + 8];
755#  endif
756	int up;
757
758#  ifdef M_CANFASTFWD
759	/*
760	 * XXX For now, IP Filter and fast-forwarding of cached flows
761	 * XXX are mutually exclusive.  Eventually, IP Filter should
762	 * XXX get a "can-fast-forward" filter rule.
763	 */
764	m->m_flags &= ~M_CANFASTFWD;
765#  endif /* M_CANFASTFWD */
766#  ifdef CSUM_DELAY_DATA
767	/*
768	 * disable delayed checksums.
769	 */
770	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
771		in_delayed_cksum(m);
772		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
773	}
774#  endif /* CSUM_DELAY_DATA */
775
776# ifdef	USE_INET6
777	if (v == 6) {
778		len = ntohs(((ip6_t*)ip)->ip6_plen);
779		p = ((ip6_t *)ip)->ip6_nxt;
780	} else
781# endif
782	{
783		p = ip->ip_p;
784		len = ip->ip_len;
785	}
786
787	if ((p == IPPROTO_TCP || p == IPPROTO_UDP || p == IPPROTO_ICMP
788# ifdef USE_INET6
789	    || (v == 6 && p == IPPROTO_ICMPV6)
790# endif
791	   )) {
792		int plen = 0;
793
794		if ((v == 6) || (ip->ip_off & IP_OFFMASK) == 0)
795			switch(p)
796			{
797			case IPPROTO_TCP:
798				plen = sizeof(tcphdr_t);
799				break;
800			case IPPROTO_UDP:
801				plen = sizeof(udphdr_t);
802				break;
803			/* 96 - enough for complete ICMP error IP header */
804			case IPPROTO_ICMP:
805# ifdef USE_INET6
806	    		case IPPROTO_ICMPV6 :
807# endif
808				plen = ICMPERR_MAXPKTLEN - sizeof(ip_t);
809				break;
810			}
811		up = MIN(hlen + plen, len);
812
813		if (up > m->m_len) {
814#  ifdef __sgi
815	/* Under IRIX, avoid m_pullup as it makes ping <hostname> panic */
816			if ((up > sizeof(hbuf)) || (m_length(m) < up)) {
817				ATOMIC_INCL(frstats[out].fr_pull[1]);
818				return -1;
819			}
820			m_copydata(m, 0, up, hbuf);
821			ATOMIC_INCL(frstats[out].fr_pull[0]);
822			ip = (ip_t *)hbuf;
823#  else /* __ sgi */
824#   ifndef linux
825			if ((*mp = m_pullup(m, up)) == 0) {
826				ATOMIC_INCL(frstats[out].fr_pull[1]);
827				return -1;
828			} else {
829				ATOMIC_INCL(frstats[out].fr_pull[0]);
830				m = *mp;
831				ip = mtod(m, ip_t *);
832			}
833#   endif /* !linux */
834#  endif /* __sgi */
835		} else
836			up = 0;
837	} else
838		up = 0;
839# endif /* !defined(__SVR4) && !defined(__svr4__) */
840# if SOLARIS
841	mb_t *m = qif->qf_m;
842
843	if ((u_int)ip & 0x3)
844		return 2;
845	fin->fin_qfm = m;
846	fin->fin_qif = qif;
847# endif
848#endif /* _KERNEL */
849
850#ifndef __FreeBSD__
851	/*
852	 * Be careful here: ip_id is in network byte order when called
853	 * from ip_output()
854	 */
855	if ((out) && (v == 4))
856		ip->ip_id = ntohs(ip->ip_id);
857#endif
858
859	changed = 0;
860	fin->fin_ifp = ifp;
861	fin->fin_v = v;
862	fin->fin_out = out;
863	fin->fin_mp = mp;
864	fr_makefrip(hlen, ip, fin);
865
866#ifdef _KERNEL
867# ifdef	USE_INET6
868	if (v == 6) {
869		ATOMIC_INCL(frstats[0].fr_ipv6[out]);
870	} else
871# endif
872		if (!out && fr_chksrc && !fr_verifysrc(ip->ip_src, ifp)) {
873			ATOMIC_INCL(frstats[0].fr_badsrc);
874# ifdef	IPFILTER_LOG
875			if (fr_chksrc == 2) {
876				fin->fin_group = -2;
877				pass = FR_INQUE|FR_NOMATCH|FR_LOGB;
878				(void) IPLLOG(pass, ip, fin, m);
879			}
880# endif
881# if !SOLARIS
882			m_freem(m);
883# endif
884			return error;
885		}
886#endif
887	pass = fr_pass;
888	if (fin->fin_fi.fi_fl & FI_SHORT) {
889		ATOMIC_INCL(frstats[out].fr_short);
890	}
891
892	READ_ENTER(&ipf_mutex);
893
894	if (fin->fin_fi.fi_fl & FI_SHORT)
895		ATOMIC_INCL(frstats[out].fr_short);
896
897	/*
898	 * Check auth now.  This, combined with the check below to see if apass
899	 * is 0 is to ensure that we don't count the packet twice, which can
900	 * otherwise occur when we reprocess it.  As it is, we only count it
901	 * after it has no auth. table matchup.  This also stops NAT from
902	 * occuring until after the packet has been auth'd.
903	 */
904	apass = fr_checkauth(ip, fin);
905
906	if (!out) {
907#ifdef	USE_INET6
908		if (v == 6)
909			list = ipacct6[0][fr_active];
910		else
911#endif
912			list = ipacct[0][fr_active];
913		changed = ip_natin(ip, fin);
914		if (!apass && (fin->fin_fr = list) &&
915		    (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
916			ATOMIC_INCL(frstats[0].fr_acct);
917		}
918	}
919
920	if (apass || (!(fr = ipfr_knownfrag(ip, fin)) &&
921	    !(fr = fr_checkstate(ip, fin)))) {
922		/*
923		 * If a packet is found in the auth table, then skip checking
924		 * the access lists for permission but we do need to consider
925		 * the result as if it were from the ACL's.
926		 */
927		if (!apass) {
928			fc = frcache + out;
929			if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) {
930				/*
931				 * copy cached data so we can unlock the mutex
932				 * earlier.
933				 */
934				bcopy((char *)fc, (char *)fin, FI_COPYSIZE);
935				ATOMIC_INCL(frstats[out].fr_chit);
936				if ((fr = fin->fin_fr)) {
937					ATOMIC_INCL(fr->fr_hits);
938					pass = fr->fr_flags;
939				}
940			} else {
941#ifdef	USE_INET6
942				if (v == 6)
943					list = ipfilter6[out][fr_active];
944				else
945#endif
946					list = ipfilter[out][fr_active];
947				if ((fin->fin_fr = list))
948					pass = fr_scanlist(fr_pass, ip, fin, m);
949				if (!(pass & (FR_KEEPSTATE|FR_DONTCACHE)))
950					bcopy((char *)fin, (char *)fc,
951					      FI_COPYSIZE);
952				if (pass & FR_NOMATCH) {
953					ATOMIC_INCL(frstats[out].fr_nom);
954				}
955			}
956			fr = fin->fin_fr;
957		} else
958			pass = apass;
959
960		/*
961		 * If we fail to add a packet to the authorization queue,
962		 * then we drop the packet later.  However, if it was added
963		 * then pretend we've dropped it already.
964		 */
965		if ((pass & FR_AUTH))
966			if (fr_newauth((mb_t *)m, fin, ip) != 0)
967#ifdef	_KERNEL
968				m = *mp = NULL;
969#else
970				;
971#endif
972
973		if (pass & FR_PREAUTH) {
974			READ_ENTER(&ipf_auth);
975			if ((fin->fin_fr = ipauth) &&
976			    (pass = fr_scanlist(0, ip, fin, m))) {
977				ATOMIC_INCL(fr_authstats.fas_hits);
978			} else {
979				ATOMIC_INCL(fr_authstats.fas_miss);
980			}
981			RWLOCK_EXIT(&ipf_auth);
982		}
983
984		fin->fin_fr = fr;
985		if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) {
986			if (fin->fin_fi.fi_fl & FI_FRAG) {
987				if (ipfr_newfrag(ip, fin, pass) == -1) {
988					ATOMIC_INCL(frstats[out].fr_bnfr);
989				} else {
990					ATOMIC_INCL(frstats[out].fr_nfr);
991				}
992			} else {
993				ATOMIC_INCL(frstats[out].fr_cfr);
994			}
995		}
996		if (pass & FR_KEEPSTATE) {
997			if (fr_addstate(ip, fin, 0) == NULL) {
998				ATOMIC_INCL(frstats[out].fr_bads);
999			} else {
1000				ATOMIC_INCL(frstats[out].fr_ads);
1001			}
1002		}
1003	} else if (fr != NULL) {
1004		pass = fr->fr_flags;
1005		if (pass & FR_LOGFIRST)
1006			pass &= ~(FR_LOGFIRST|FR_LOG);
1007	}
1008
1009#if (BSD >= 199306) && (defined(_KERNEL) || defined(KERNEL))
1010	if (securelevel <= 0)
1011#endif
1012		if (fr && fr->fr_func && !(pass & FR_CALLNOW))
1013			pass = (*fr->fr_func)(pass, ip, fin);
1014
1015	/*
1016	 * Only count/translate packets which will be passed on, out the
1017	 * interface.
1018	 */
1019	if (out && (pass & FR_PASS)) {
1020#ifdef	USE_INET6
1021		if (v == 6)
1022			list = ipacct6[1][fr_active];
1023		else
1024#endif
1025			list = ipacct[1][fr_active];
1026		if ((fin->fin_fr = list) &&
1027		    (fr_scanlist(FR_NOMATCH, ip, fin, m) & FR_ACCOUNT)) {
1028			ATOMIC_INCL(frstats[1].fr_acct);
1029		}
1030		fin->fin_fr = fr;
1031		changed = ip_natout(ip, fin);
1032	} else
1033		fin->fin_fr = fr;
1034	RWLOCK_EXIT(&ipf_mutex);
1035
1036#ifdef	IPFILTER_LOG
1037	if ((fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
1038		if ((fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
1039			pass |= FF_LOGNOMATCH;
1040			ATOMIC_INCL(frstats[out].fr_npkl);
1041			goto logit;
1042		} else if (((pass & FR_LOGMASK) == FR_LOGP) ||
1043		    ((pass & FR_PASS) && (fr_flags & FF_LOGPASS))) {
1044			if ((pass & FR_LOGMASK) != FR_LOGP)
1045				pass |= FF_LOGPASS;
1046			ATOMIC_INCL(frstats[out].fr_ppkl);
1047			goto logit;
1048		} else if (((pass & FR_LOGMASK) == FR_LOGB) ||
1049			   ((pass & FR_BLOCK) && (fr_flags & FF_LOGBLOCK))) {
1050			if ((pass & FR_LOGMASK) != FR_LOGB)
1051				pass |= FF_LOGBLOCK;
1052			ATOMIC_INCL(frstats[out].fr_bpkl);
1053logit:
1054			if (!IPLLOG(pass, ip, fin, m)) {
1055				ATOMIC_INCL(frstats[out].fr_skip);
1056				if ((pass & (FR_PASS|FR_LOGORBLOCK)) ==
1057				    (FR_PASS|FR_LOGORBLOCK))
1058					pass ^= FR_PASS|FR_BLOCK;
1059			}
1060		}
1061	}
1062#endif /* IPFILTER_LOG */
1063
1064#ifndef __FreeBSD__
1065	if ((out) && (v == 4))
1066		ip->ip_id = htons(ip->ip_id);
1067#endif
1068
1069#ifdef	_KERNEL
1070	/*
1071	 * Only allow FR_DUP to work if a rule matched - it makes no sense to
1072	 * set FR_DUP as a "default" as there are no instructions about where
1073	 * to send the packet.
1074	 */
1075	if (fr && (pass & FR_DUP))
1076# if	SOLARIS
1077		mc = dupmsg(m);
1078# else
1079#  ifndef linux
1080		mc = m_copy(m, 0, M_COPYALL);
1081#  else
1082		;
1083#  endif
1084# endif
1085#endif
1086	if (pass & FR_PASS) {
1087		ATOMIC_INCL(frstats[out].fr_pass);
1088	} else if (pass & FR_BLOCK) {
1089		ATOMIC_INCL(frstats[out].fr_block);
1090		/*
1091		 * Should we return an ICMP packet to indicate error
1092		 * status passing through the packet filter ?
1093		 * WARNING: ICMP error packets AND TCP RST packets should
1094		 * ONLY be sent in repsonse to incoming packets.  Sending them
1095		 * in response to outbound packets can result in a panic on
1096		 * some operating systems.
1097		 */
1098		if (!out) {
1099#ifdef	_KERNEL
1100			if (pass & FR_RETICMP) {
1101				int dst;
1102
1103				if ((pass & FR_RETMASK) == FR_FAKEICMP)
1104					dst = 1;
1105				else
1106					dst = 0;
1107				send_icmp_err(ip, ICMP_UNREACH, fin, dst);
1108				ATOMIC_INCL(frstats[0].fr_ret);
1109			} else if (((pass & FR_RETMASK) == FR_RETRST) &&
1110				   !(fin->fin_fi.fi_fl & FI_SHORT)) {
1111				if (send_reset(ip, fin) == 0) {
1112					ATOMIC_INCL(frstats[1].fr_ret);
1113				}
1114			}
1115#else
1116			if ((pass & FR_RETMASK) == FR_RETICMP) {
1117				verbose("- ICMP unreachable sent\n");
1118				ATOMIC_INCL(frstats[0].fr_ret);
1119			} else if ((pass & FR_RETMASK) == FR_FAKEICMP) {
1120				verbose("- forged ICMP unreachable sent\n");
1121				ATOMIC_INCL(frstats[0].fr_ret);
1122			} else if (((pass & FR_RETMASK) == FR_RETRST) &&
1123				   !(fin->fin_fi.fi_fl & FI_SHORT)) {
1124				verbose("- TCP RST sent\n");
1125				ATOMIC_INCL(frstats[1].fr_ret);
1126			}
1127#endif
1128		} else {
1129			if (pass & FR_RETRST)
1130				error = ECONNRESET;
1131		}
1132	}
1133
1134	/*
1135	 * If we didn't drop off the bottom of the list of rules (and thus
1136	 * the 'current' rule fr is not NULL), then we may have some extra
1137	 * instructions about what to do with a packet.
1138	 * Once we're finished return to our caller, freeing the packet if
1139	 * we are dropping it (* BSD ONLY *).
1140	 */
1141	if ((changed == -1) && (pass & FR_PASS)) {
1142		pass &= ~FR_PASS;
1143		pass |= FR_BLOCK;
1144	}
1145#if defined(_KERNEL)
1146# if !SOLARIS
1147#  if !defined(linux)
1148	if (fr) {
1149		frdest_t *fdp = &fr->fr_tif;
1150
1151		if (((pass & FR_FASTROUTE) && !out) ||
1152		    (fdp->fd_ifp && fdp->fd_ifp != (struct ifnet *)-1)) {
1153			if (ipfr_fastroute(m, fin, fdp) == 0)
1154				m = *mp = NULL;
1155		}
1156		if (mc)
1157			ipfr_fastroute(mc, fin, &fr->fr_dif);
1158	}
1159	if (!(pass & FR_PASS) && m)
1160		m_freem(m);
1161#   ifdef __sgi
1162	else if (changed && up && m)
1163		m_copyback(m, 0, up, hbuf);
1164#   endif
1165#  endif /* !linux */
1166# else /* !SOLARIS */
1167	if (fr) {
1168		frdest_t *fdp = &fr->fr_tif;
1169
1170		if (((pass & FR_FASTROUTE) && !out) ||
1171		    (fdp->fd_ifp && fdp->fd_ifp != (struct ifnet *)-1)) {
1172			if (ipfr_fastroute(ip, m, mp, fin, fdp) == 0)
1173				m = *mp = NULL;
1174		}
1175		if (mc)
1176			ipfr_fastroute(ip, mc, mp, fin, &fr->fr_dif);
1177	}
1178# endif /* !SOLARIS */
1179	return (pass & FR_PASS) ? 0 : error;
1180#else /* _KERNEL */
1181	if (pass & FR_NOMATCH)
1182		return 1;
1183	if (pass & FR_PASS)
1184		return 0;
1185	if (pass & FR_AUTH)
1186		return -2;
1187	return -1;
1188#endif /* _KERNEL */
1189}
1190
1191
1192/*
1193 * ipf_cksum
1194 * addr should be 16bit aligned and len is in bytes.
1195 * length is in bytes
1196 */
1197u_short ipf_cksum(addr, len)
1198register u_short *addr;
1199register int len;
1200{
1201	register u_32_t sum = 0;
1202
1203	for (sum = 0; len > 1; len -= 2)
1204		sum += *addr++;
1205
1206	/* mop up an odd byte, if necessary */
1207	if (len == 1)
1208		sum += *(u_char *)addr;
1209
1210	/*
1211	 * add back carry outs from top 16 bits to low 16 bits
1212	 */
1213	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
1214	sum += (sum >> 16);			/* add carry */
1215	return (u_short)(~sum);
1216}
1217
1218
1219/*
1220 * NB: This function assumes we've pullup'd enough for all of the IP header
1221 * and the TCP header.  We also assume that data blocks aren't allocated in
1222 * odd sizes.
1223 */
1224u_short fr_tcpsum(m, ip, tcp)
1225mb_t *m;
1226ip_t *ip;
1227tcphdr_t *tcp;
1228{
1229	u_short *sp, slen, ts;
1230	u_int sum, sum2;
1231	int hlen;
1232
1233	/*
1234	 * Add up IP Header portion
1235	 */
1236	hlen = ip->ip_hl << 2;
1237	slen = ip->ip_len - hlen;
1238	sum = htons((u_short)ip->ip_p);
1239	sum += htons(slen);
1240	sp = (u_short *)&ip->ip_src;
1241	sum += *sp++;	/* ip_src */
1242	sum += *sp++;
1243	sum += *sp++;	/* ip_dst */
1244	sum += *sp++;
1245	ts = tcp->th_sum;
1246	tcp->th_sum = 0;
1247#ifdef	KERNEL
1248# if SOLARIS
1249	sum2 = ip_cksum(m, hlen, sum);	/* hlen == offset */
1250	sum2 = (sum2 & 0xffff) + (sum2 >> 16);
1251	sum2 = ~sum2 & 0xffff;
1252# else /* SOLARIS */
1253#  if defined(BSD) || defined(sun)
1254#   if BSD >= 199306
1255	m->m_data += hlen;
1256#   else
1257	m->m_off += hlen;
1258#   endif
1259	m->m_len -= hlen;
1260	sum2 = in_cksum(m, slen);
1261	m->m_len += hlen;
1262#   if BSD >= 199306
1263	m->m_data -= hlen;
1264#   else
1265	m->m_off -= hlen;
1266#   endif
1267	/*
1268	 * Both sum and sum2 are partial sums, so combine them together.
1269	 */
1270	sum = (sum & 0xffff) + (sum >> 16);
1271	sum = ~sum & 0xffff;
1272	sum2 += sum;
1273	sum2 = (sum2 & 0xffff) + (sum2 >> 16);
1274#  else /* defined(BSD) || defined(sun) */
1275{
1276	union {
1277		u_char	c[2];
1278		u_short	s;
1279	} bytes;
1280	u_short len = ip->ip_len;
1281# if defined(__sgi)
1282	int add;
1283# endif
1284
1285	/*
1286	 * Add up IP Header portion
1287	 */
1288	sp = (u_short *)&ip->ip_src;
1289	len -= (ip->ip_hl << 2);
1290	sum = ntohs(IPPROTO_TCP);
1291	sum += htons(len);
1292	sum += *sp++;	/* ip_src */
1293	sum += *sp++;
1294	sum += *sp++;	/* ip_dst */
1295	sum += *sp++;
1296	if (sp != (u_short *)tcp)
1297		sp = (u_short *)tcp;
1298	sum += *sp++;	/* sport */
1299	sum += *sp++;	/* dport */
1300	sum += *sp++;	/* seq */
1301	sum += *sp++;
1302	sum += *sp++;	/* ack */
1303	sum += *sp++;
1304	sum += *sp++;	/* off */
1305	sum += *sp++;	/* win */
1306	sum += *sp++;	/* Skip over checksum */
1307	sum += *sp++;	/* urp */
1308
1309# ifdef	__sgi
1310	/*
1311	 * In case we had to copy the IP & TCP header out of mbufs,
1312	 * skip over the mbuf bits which are the header
1313	 */
1314	if ((caddr_t)ip != mtod(m, caddr_t)) {
1315		hlen = (caddr_t)sp - (caddr_t)ip;
1316		while (hlen) {
1317			add = MIN(hlen, m->m_len);
1318			sp = (u_short *)(mtod(m, caddr_t) + add);
1319			hlen -= add;
1320			if (add == m->m_len) {
1321				m = m->m_next;
1322				if (!hlen) {
1323					if (!m)
1324						break;
1325					sp = mtod(m, u_short *);
1326				}
1327				PANIC((!m),("fr_tcpsum(1): not enough data"));
1328			}
1329		}
1330	}
1331# endif
1332
1333	if (!(len -= sizeof(*tcp)))
1334		goto nodata;
1335	while (len > 1) {
1336		if (((caddr_t)sp - mtod(m, caddr_t)) >= m->m_len) {
1337			m = m->m_next;
1338			PANIC((!m),("fr_tcpsum(2): not enough data"));
1339			sp = mtod(m, u_short *);
1340		}
1341		if (((caddr_t)(sp + 1) - mtod(m, caddr_t)) > m->m_len) {
1342			bytes.c[0] = *(u_char *)sp;
1343			m = m->m_next;
1344			PANIC((!m),("fr_tcpsum(3): not enough data"));
1345			sp = mtod(m, u_short *);
1346			bytes.c[1] = *(u_char *)sp;
1347			sum += bytes.s;
1348			sp = (u_short *)((u_char *)sp + 1);
1349		}
1350		if ((u_long)sp & 1) {
1351			bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s));
1352			sum += bytes.s;
1353		} else
1354			sum += *sp++;
1355		len -= 2;
1356	}
1357	if (len)
1358		sum += ntohs(*(u_char *)sp << 8);
1359nodata:
1360	while (sum > 0xffff)
1361		sum = (sum & 0xffff) + (sum >> 16);
1362	sum2 = (u_short)(~sum & 0xffff);
1363}
1364#  endif /*  defined(BSD) || defined(sun) */
1365# endif /* SOLARIS */
1366#else /* KERNEL */
1367	sum2 = 0;
1368#endif /* KERNEL */
1369	tcp->th_sum = ts;
1370	return sum2;
1371}
1372
1373
1374#if defined(_KERNEL) && ( ((BSD < 199306) && !SOLARIS) || defined(__sgi) )
1375/*
1376 * Copyright (c) 1982, 1986, 1988, 1991, 1993
1377 *	The Regents of the University of California.  All rights reserved.
1378 *
1379 * Redistribution and use in source and binary forms, with or without
1380 * modification, are permitted provided that the following conditions
1381 * are met:
1382 * 1. Redistributions of source code must retain the above copyright
1383 *    notice, this list of conditions and the following disclaimer.
1384 * 2. Redistributions in binary form must reproduce the above copyright
1385 *    notice, this list of conditions and the following disclaimer in the
1386 *    documentation and/or other materials provided with the distribution.
1387 * 3. All advertising materials mentioning features or use of this software
1388 *    must display the following acknowledgement:
1389 *	This product includes software developed by the University of
1390 *	California, Berkeley and its contributors.
1391 * 4. Neither the name of the University nor the names of its contributors
1392 *    may be used to endorse or promote products derived from this software
1393 *    without specific prior written permission.
1394 *
1395 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1396 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1397 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1398 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1399 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1400 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1401 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1402 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1403 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1404 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1405 * SUCH DAMAGE.
1406 *
1407 *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
1408 * $Id: fil.c,v 2.35.2.26 2000/10/24 11:58:17 darrenr Exp $
1409 */
1410/*
1411 * Copy data from an mbuf chain starting "off" bytes from the beginning,
1412 * continuing for "len" bytes, into the indicated buffer.
1413 */
1414void
1415m_copydata(m, off, len, cp)
1416	register mb_t *m;
1417	register int off;
1418	register int len;
1419	caddr_t cp;
1420{
1421	register unsigned count;
1422
1423	if (off < 0 || len < 0)
1424		panic("m_copydata");
1425	while (off > 0) {
1426		if (m == 0)
1427			panic("m_copydata");
1428		if (off < m->m_len)
1429			break;
1430		off -= m->m_len;
1431		m = m->m_next;
1432	}
1433	while (len > 0) {
1434		if (m == 0)
1435			panic("m_copydata");
1436		count = MIN(m->m_len - off, len);
1437		bcopy(mtod(m, caddr_t) + off, cp, count);
1438		len -= count;
1439		cp += count;
1440		off = 0;
1441		m = m->m_next;
1442	}
1443}
1444
1445
1446# ifndef linux
1447/*
1448 * Copy data from a buffer back into the indicated mbuf chain,
1449 * starting "off" bytes from the beginning, extending the mbuf
1450 * chain if necessary.
1451 */
1452void
1453m_copyback(m0, off, len, cp)
1454	struct	mbuf *m0;
1455	register int off;
1456	register int len;
1457	caddr_t cp;
1458{
1459	register int mlen;
1460	register struct mbuf *m = m0, *n;
1461	int totlen = 0;
1462
1463	if (m0 == 0)
1464		return;
1465	while (off > (mlen = m->m_len)) {
1466		off -= mlen;
1467		totlen += mlen;
1468		if (m->m_next == 0) {
1469			n = m_getclr(M_DONTWAIT, m->m_type);
1470			if (n == 0)
1471				goto out;
1472			n->m_len = min(MLEN, len + off);
1473			m->m_next = n;
1474		}
1475		m = m->m_next;
1476	}
1477	while (len > 0) {
1478		mlen = min (m->m_len - off, len);
1479		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
1480		cp += mlen;
1481		len -= mlen;
1482		mlen += off;
1483		off = 0;
1484		totlen += mlen;
1485		if (len == 0)
1486			break;
1487		if (m->m_next == 0) {
1488			n = m_get(M_DONTWAIT, m->m_type);
1489			if (n == 0)
1490				break;
1491			n->m_len = min(MLEN, len);
1492			m->m_next = n;
1493		}
1494		m = m->m_next;
1495	}
1496out:
1497#if 0
1498	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
1499		m->m_pkthdr.len = totlen;
1500#endif
1501	return;
1502}
1503# endif /* linux */
1504#endif /* (_KERNEL) && ( ((BSD < 199306) && !SOLARIS) || __sgi) */
1505
1506
1507frgroup_t *fr_findgroup(num, flags, which, set, fgpp)
1508u_32_t num, flags;
1509minor_t which;
1510int set;
1511frgroup_t ***fgpp;
1512{
1513	frgroup_t *fg, **fgp;
1514
1515	if (which == IPL_LOGAUTH)
1516		fgp = &ipfgroups[2][set];
1517	else if (flags & FR_ACCOUNT)
1518		fgp = &ipfgroups[1][set];
1519	else if (flags & (FR_OUTQUE|FR_INQUE))
1520		fgp = &ipfgroups[0][set];
1521	else
1522		return NULL;
1523	num &= 0xffff;
1524
1525	while ((fg = *fgp))
1526		if (fg->fg_num == num)
1527			break;
1528		else
1529			fgp = &fg->fg_next;
1530	if (fgpp)
1531		*fgpp = fgp;
1532	return fg;
1533}
1534
1535
1536frgroup_t *fr_addgroup(num, fp, which, set)
1537u_32_t num;
1538frentry_t *fp;
1539minor_t which;
1540int set;
1541{
1542	frgroup_t *fg, **fgp;
1543
1544	if ((fg = fr_findgroup(num, fp->fr_flags, which, set, &fgp)))
1545		return fg;
1546
1547	KMALLOC(fg, frgroup_t *);
1548	if (fg) {
1549		fg->fg_num = num;
1550		fg->fg_next = *fgp;
1551		fg->fg_head = fp;
1552		fg->fg_start = &fp->fr_grp;
1553		*fgp = fg;
1554	}
1555	return fg;
1556}
1557
1558
1559void fr_delgroup(num, flags, which, set)
1560u_32_t num, flags;
1561minor_t which;
1562int set;
1563{
1564	frgroup_t *fg, **fgp;
1565
1566	if (!(fg = fr_findgroup(num, flags, which, set, &fgp)))
1567		return;
1568
1569	*fgp = fg->fg_next;
1570	KFREE(fg);
1571}
1572
1573
1574
1575/*
1576 * recursively flush rules from the list, descending groups as they are
1577 * encountered.  if a rule is the head of a group and it has lost all its
1578 * group members, then also delete the group reference.
1579 */
1580static int frflushlist(set, unit, nfreedp, listp)
1581int set;
1582minor_t unit;
1583int *nfreedp;
1584frentry_t **listp;
1585{
1586	register int freed = 0, i;
1587	register frentry_t *fp;
1588
1589	while ((fp = *listp)) {
1590		*listp = fp->fr_next;
1591		if (fp->fr_grp) {
1592			i = frflushlist(set, unit, nfreedp, &fp->fr_grp);
1593			MUTEX_ENTER(&ipf_rw);
1594			fp->fr_ref -= i;
1595			MUTEX_EXIT(&ipf_rw);
1596		}
1597
1598		ATOMIC_DEC32(fp->fr_ref);
1599		if (fp->fr_grhead) {
1600			fr_delgroup(fp->fr_grhead, fp->fr_flags,
1601				    unit, set);
1602			fp->fr_grhead = 0;
1603		}
1604		if (fp->fr_ref == 0) {
1605			KFREE(fp);
1606			freed++;
1607		} else
1608			fp->fr_next = NULL;
1609	}
1610	*nfreedp += freed;
1611	return freed;
1612}
1613
1614
1615int frflush(unit, flags)
1616minor_t unit;
1617int flags;
1618{
1619	int flushed = 0, set;
1620
1621	if (unit != IPL_LOGIPF)
1622		return 0;
1623	WRITE_ENTER(&ipf_mutex);
1624	bzero((char *)frcache, sizeof(frcache[0]) * 2);
1625
1626	set = fr_active;
1627	if (flags & FR_INACTIVE)
1628		set = 1 - set;
1629
1630	if (flags & FR_OUTQUE) {
1631#ifdef	USE_INET6
1632		(void) frflushlist(set, unit, &flushed, &ipfilter6[1][set]);
1633		(void) frflushlist(set, unit, &flushed, &ipacct6[1][set]);
1634#endif
1635		(void) frflushlist(set, unit, &flushed, &ipfilter[1][set]);
1636		(void) frflushlist(set, unit, &flushed, &ipacct[1][set]);
1637	}
1638	if (flags & FR_INQUE) {
1639#ifdef	USE_INET6
1640		(void) frflushlist(set, unit, &flushed, &ipfilter6[0][set]);
1641		(void) frflushlist(set, unit, &flushed, &ipacct6[0][set]);
1642#endif
1643		(void) frflushlist(set, unit, &flushed, &ipfilter[0][set]);
1644		(void) frflushlist(set, unit, &flushed, &ipacct[0][set]);
1645	}
1646	RWLOCK_EXIT(&ipf_mutex);
1647	return flushed;
1648}
1649
1650
1651char *memstr(src, dst, slen, dlen)
1652char *src, *dst;
1653int slen, dlen;
1654{
1655	char *s = NULL;
1656
1657	while (dlen >= slen) {
1658		if (bcmp(src, dst, slen) == 0) {
1659			s = dst;
1660			break;
1661		}
1662		dst++;
1663		dlen--;
1664	}
1665	return s;
1666}
1667
1668
1669void fixskip(listp, rp, addremove)
1670frentry_t **listp, *rp;
1671int addremove;
1672{
1673	frentry_t *fp;
1674	int rules = 0, rn = 0;
1675
1676	for (fp = *listp; fp && (fp != rp); fp = fp->fr_next, rules++)
1677		;
1678
1679	if (!fp)
1680		return;
1681
1682	for (fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
1683		if (fp->fr_skip && (rn + fp->fr_skip >= rules))
1684			fp->fr_skip += addremove;
1685}
1686
1687
1688#ifdef	_KERNEL
1689/*
1690 * count consecutive 1's in bit mask.  If the mask generated by counting
1691 * consecutive 1's is different to that passed, return -1, else return #
1692 * of bits.
1693 */
1694int	countbits(ip)
1695u_32_t	ip;
1696{
1697	u_32_t	ipn;
1698	int	cnt = 0, i, j;
1699
1700	ip = ipn = ntohl(ip);
1701	for (i = 32; i; i--, ipn *= 2)
1702		if (ipn & 0x80000000)
1703			cnt++;
1704		else
1705			break;
1706	ipn = 0;
1707	for (i = 32, j = cnt; i; i--, j--) {
1708		ipn *= 2;
1709		if (j > 0)
1710			ipn++;
1711	}
1712	if (ipn == ip)
1713		return cnt;
1714	return -1;
1715}
1716
1717
1718/*
1719 * return the first IP Address associated with an interface
1720 */
1721int fr_ifpaddr(v, ifptr, inp)
1722int v;
1723void *ifptr;
1724struct in_addr *inp;
1725{
1726# ifdef	USE_INET6
1727	struct in6_addr *inp6 = NULL;
1728# endif
1729# if SOLARIS
1730	ill_t *ill = ifptr;
1731# else
1732	struct ifnet *ifp = ifptr;
1733# endif
1734	struct in_addr in;
1735
1736# if SOLARIS
1737#  ifdef	USE_INET6
1738	if (v == 6) {
1739		struct in6_addr in6;
1740
1741		/*
1742		 * First is always link local.
1743		 */
1744		if (ill->ill_ipif->ipif_next)
1745			in6 = ill->ill_ipif->ipif_next->ipif_v6lcl_addr;
1746		else
1747			bzero((char *)&in6, sizeof(in6));
1748		bcopy((char *)&in6, (char *)inp, sizeof(in6));
1749	} else
1750#  endif
1751	{
1752		in.s_addr = ill->ill_ipif->ipif_local_addr;
1753		*inp = in;
1754	}
1755# else /* SOLARIS */
1756#  if linux
1757	;
1758#  else /* linux */
1759	struct sockaddr_in *sin;
1760	struct ifaddr *ifa;
1761
1762#   if	(__FreeBSD_version >= 300000)
1763	ifa = TAILQ_FIRST(&ifp->if_addrhead);
1764#   else
1765#    if defined(__NetBSD__) || defined(__OpenBSD__)
1766	ifa = ifp->if_addrlist.tqh_first;
1767#    else
1768#     if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
1769	ifa = &((struct in_ifaddr *)ifp->in_ifaddr)->ia_ifa;
1770#     else
1771	ifa = ifp->if_addrlist;
1772#     endif
1773#    endif /* __NetBSD__ || __OpenBSD__ */
1774#   endif /* __FreeBSD_version >= 300000 */
1775#   if (BSD < 199306) && !(/*IRIX6*/defined(__sgi) && defined(IFF_DRVRLOCK))
1776	sin = (struct sockaddr_in *)&ifa->ifa_addr;
1777#   else
1778	sin = (struct sockaddr_in *)ifa->ifa_addr;
1779	while (sin && ifa) {
1780		if ((v == 4) && (sin->sin_family == AF_INET))
1781			break;
1782#    ifdef USE_INET6
1783		if ((v == 6) && (sin->sin_family == AF_INET6)) {
1784			inp6 = &((struct sockaddr_in6 *)sin)->sin6_addr;
1785			if (!IN6_IS_ADDR_LINKLOCAL(inp6) &&
1786			    !IN6_IS_ADDR_LOOPBACK(inp6))
1787				break;
1788		}
1789#    endif
1790#    if	(__FreeBSD_version >= 300000)
1791		ifa = TAILQ_NEXT(ifa, ifa_link);
1792#    else
1793#     if defined(__NetBSD__) || defined(__OpenBSD__)
1794		ifa = ifa->ifa_list.tqe_next;
1795#     else
1796		ifa = ifa->ifa_next;
1797#     endif
1798#    endif /* __FreeBSD_version >= 300000 */
1799		if (ifa)
1800			sin = (struct sockaddr_in *)ifa->ifa_addr;
1801	}
1802	if (ifa == NULL)
1803		sin = NULL;
1804	if (sin == NULL)
1805		return -1;
1806#   endif /* (BSD < 199306) && (!__sgi && IFF_DRVLOCK) */
1807#    ifdef	USE_INET6
1808	if (v == 6)
1809		bcopy((char *)inp6, (char *)inp, sizeof(*inp6));
1810	else
1811#    endif
1812	{
1813		in = sin->sin_addr;
1814		*inp = in;
1815	}
1816#  endif /* linux */
1817# endif /* SOLARIS */
1818	return 0;
1819}
1820
1821
1822static void frsynclist(fr)
1823register frentry_t *fr;
1824{
1825	for (; fr; fr = fr->fr_next) {
1826		if (fr->fr_ifa != NULL) {
1827			fr->fr_ifa = GETUNIT(fr->fr_ifname, fr->fr_ip.fi_v);
1828			if (fr->fr_ifa == NULL)
1829				fr->fr_ifa = (void *)-1;
1830		}
1831		if (fr->fr_grp)
1832			frsynclist(fr->fr_grp);
1833	}
1834}
1835
1836
1837void frsync()
1838{
1839# if !SOLARIS
1840	register struct ifnet *ifp;
1841
1842#  if defined(__OpenBSD__) || ((NetBSD >= 199511) && (NetBSD < 1991011)) || \
1843     (defined(__FreeBSD_version) && (__FreeBSD_version >= 300000))
1844#   if (NetBSD >= 199905) || defined(__OpenBSD__)
1845	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_list.tqe_next)
1846#   else
1847	for (ifp = ifnet.tqh_first; ifp; ifp = ifp->if_link.tqe_next)
1848#   endif
1849#  else
1850	for (ifp = ifnet; ifp; ifp = ifp->if_next)
1851#  endif
1852	{
1853		ip_natsync(ifp);
1854		ip_statesync(ifp);
1855	}
1856	ip_natsync((struct ifnet *)-1);
1857# endif
1858
1859	WRITE_ENTER(&ipf_mutex);
1860	frsynclist(ipacct[0][fr_active]);
1861	frsynclist(ipacct[1][fr_active]);
1862	frsynclist(ipfilter[0][fr_active]);
1863	frsynclist(ipfilter[1][fr_active]);
1864#ifdef	USE_INET6
1865	frsynclist(ipacct6[0][fr_active]);
1866	frsynclist(ipacct6[1][fr_active]);
1867	frsynclist(ipfilter6[0][fr_active]);
1868	frsynclist(ipfilter6[1][fr_active]);
1869#endif
1870	RWLOCK_EXIT(&ipf_mutex);
1871}
1872
1873
1874/*
1875 * In the functions below, bcopy() is called because the pointer being
1876 * copied _from_ in this instance is a pointer to a char buf (which could
1877 * end up being unaligned) and on the kernel's local stack.
1878 */
1879int ircopyptr(a, b, c)
1880void *a, *b;
1881size_t c;
1882{
1883	caddr_t ca;
1884	int err;
1885
1886#if SOLARIS
1887	if (copyin(a, (char *)&ca, sizeof(ca)))
1888		return EFAULT;
1889#else
1890	bcopy(a, &ca, sizeof(ca));
1891#endif
1892	err = copyin(ca, b, c);
1893	if (err)
1894		err = EFAULT;
1895	return err;
1896}
1897
1898
1899int iwcopyptr(a, b, c)
1900void *a, *b;
1901size_t c;
1902{
1903	caddr_t ca;
1904	int err;
1905
1906#if SOLARIS
1907	if (copyin(b, (char *)&ca, sizeof(ca)))
1908		return EFAULT;
1909#else
1910	bcopy(b, &ca, sizeof(ca));
1911#endif
1912	err = copyout(a, ca, c);
1913	if (err)
1914		err = EFAULT;
1915	return err;
1916}
1917
1918#else /* _KERNEL */
1919
1920
1921/*
1922 * return the first IP Address associated with an interface
1923 */
1924int fr_ifpaddr(v, ifptr, inp)
1925int v;
1926void *ifptr;
1927struct in_addr *inp;
1928{
1929	return 0;
1930}
1931
1932
1933int ircopyptr(a, b, c)
1934void *a, *b;
1935size_t c;
1936{
1937	caddr_t ca;
1938
1939	bcopy(a, &ca, sizeof(ca));
1940	bcopy(ca, b, c);
1941	return 0;
1942}
1943
1944
1945int iwcopyptr(a, b, c)
1946void *a, *b;
1947size_t c;
1948{
1949	caddr_t ca;
1950
1951	bcopy(b, &ca, sizeof(ca));
1952	bcopy(a, ca, c);
1953	return 0;
1954}
1955
1956
1957#endif
1958
1959
1960int fr_lock(data, lockp)
1961caddr_t data;
1962int *lockp;
1963{
1964	int arg, error;
1965
1966	error = IRCOPY(data, (caddr_t)&arg, sizeof(arg));
1967	if (!error) {
1968		error = IWCOPY((caddr_t)lockp, data, sizeof(*lockp));
1969		if (!error)
1970			*lockp = arg;
1971	}
1972	return error;
1973}
1974
1975
1976void fr_getstat(fiop)
1977friostat_t *fiop;
1978{
1979	bcopy((char *)frstats, (char *)fiop->f_st, sizeof(filterstats_t) * 2);
1980	fiop->f_locks[0] = fr_state_lock;
1981	fiop->f_locks[1] = fr_nat_lock;
1982	fiop->f_locks[2] = fr_frag_lock;
1983	fiop->f_locks[3] = fr_auth_lock;
1984	fiop->f_fin[0] = ipfilter[0][0];
1985	fiop->f_fin[1] = ipfilter[0][1];
1986	fiop->f_fout[0] = ipfilter[1][0];
1987	fiop->f_fout[1] = ipfilter[1][1];
1988	fiop->f_acctin[0] = ipacct[0][0];
1989	fiop->f_acctin[1] = ipacct[0][1];
1990	fiop->f_acctout[0] = ipacct[1][0];
1991	fiop->f_acctout[1] = ipacct[1][1];
1992#ifdef	USE_INET6
1993	fiop->f_fin6[0] = ipfilter6[0][0];
1994	fiop->f_fin6[1] = ipfilter6[0][1];
1995	fiop->f_fout6[0] = ipfilter6[1][0];
1996	fiop->f_fout6[1] = ipfilter6[1][1];
1997	fiop->f_acctin6[0] = ipacct6[0][0];
1998	fiop->f_acctin6[1] = ipacct6[0][1];
1999	fiop->f_acctout6[0] = ipacct6[1][0];
2000	fiop->f_acctout6[1] = ipacct6[1][1];
2001#else
2002	fiop->f_fin6[0] = NULL;
2003	fiop->f_fin6[1] = NULL;
2004	fiop->f_fout6[0] = NULL;
2005	fiop->f_fout6[1] = NULL;
2006	fiop->f_acctin6[0] = NULL;
2007	fiop->f_acctin6[1] = NULL;
2008	fiop->f_acctout6[0] = NULL;
2009	fiop->f_acctout6[1] = NULL;
2010#endif
2011	fiop->f_active = fr_active;
2012	fiop->f_froute[0] = ipl_frouteok[0];
2013	fiop->f_froute[1] = ipl_frouteok[1];
2014
2015	fiop->f_running = fr_running;
2016	fiop->f_groups[0][0] = ipfgroups[0][0];
2017	fiop->f_groups[0][1] = ipfgroups[0][1];
2018	fiop->f_groups[1][0] = ipfgroups[1][0];
2019	fiop->f_groups[1][1] = ipfgroups[1][1];
2020	fiop->f_groups[2][0] = ipfgroups[2][0];
2021	fiop->f_groups[2][1] = ipfgroups[2][1];
2022#ifdef  IPFILTER_LOG
2023	fiop->f_logging = 1;
2024#else
2025	fiop->f_logging = 0;
2026#endif
2027	fiop->f_defpass = fr_pass;
2028	strncpy(fiop->f_version, ipfilter_version, sizeof(fiop->f_version));
2029}
2030
2031
2032#ifdef	USE_INET6
2033int icmptoicmp6types[ICMP_MAXTYPE+1] = {
2034	ICMP6_ECHO_REPLY,	/* 0: ICMP_ECHOREPLY */
2035	-1,			/* 1: UNUSED */
2036	-1,			/* 2: UNUSED */
2037	ICMP6_DST_UNREACH,	/* 3: ICMP_UNREACH */
2038	-1,			/* 4: ICMP_SOURCEQUENCH */
2039	ND_REDIRECT,		/* 5: ICMP_REDIRECT */
2040	-1,			/* 6: UNUSED */
2041	-1,			/* 7: UNUSED */
2042	ICMP6_ECHO_REQUEST,	/* 8: ICMP_ECHO */
2043	-1,			/* 9: UNUSED */
2044	-1,			/* 10: UNUSED */
2045	ICMP6_TIME_EXCEEDED,	/* 11: ICMP_TIMXCEED */
2046	ICMP6_PARAM_PROB,	/* 12: ICMP_PARAMPROB */
2047	-1,			/* 13: ICMP_TSTAMP */
2048	-1,			/* 14: ICMP_TSTAMPREPLY */
2049	-1,			/* 15: ICMP_IREQ */
2050	-1,			/* 16: ICMP_IREQREPLY */
2051	-1,			/* 17: ICMP_MASKREQ */
2052	-1,			/* 18: ICMP_MASKREPLY */
2053};
2054
2055
2056int	icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
2057	ICMP6_DST_UNREACH_ADDR,		/* 0: ICMP_UNREACH_NET */
2058	ICMP6_DST_UNREACH_ADDR,		/* 1: ICMP_UNREACH_HOST */
2059	-1,				/* 2: ICMP_UNREACH_PROTOCOL */
2060	ICMP6_DST_UNREACH_NOPORT,	/* 3: ICMP_UNREACH_PORT */
2061	-1,				/* 4: ICMP_UNREACH_NEEDFRAG */
2062	ICMP6_DST_UNREACH_NOTNEIGHBOR,	/* 5: ICMP_UNREACH_SRCFAIL */
2063	ICMP6_DST_UNREACH_ADDR,		/* 6: ICMP_UNREACH_NET_UNKNOWN */
2064	ICMP6_DST_UNREACH_ADDR,		/* 7: ICMP_UNREACH_HOST_UNKNOWN */
2065	-1,				/* 8: ICMP_UNREACH_ISOLATED */
2066	ICMP6_DST_UNREACH_ADMIN,	/* 9: ICMP_UNREACH_NET_PROHIB */
2067	ICMP6_DST_UNREACH_ADMIN,	/* 10: ICMP_UNREACH_HOST_PROHIB */
2068	-1,				/* 11: ICMP_UNREACH_TOSNET */
2069	-1,				/* 12: ICMP_UNREACH_TOSHOST */
2070	ICMP6_DST_UNREACH_ADMIN,	/* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
2071};
2072#endif
2073