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