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