xref: /titanic_50/usr/src/uts/common/inet/ipf/fil.c (revision 8f1d104bfc09ff778b56ecf8ca2d4e0998e7c909)
1 /*
2  * Copyright (C) 1993-2003 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
7  */
8 
9 #if defined(KERNEL) || defined(_KERNEL)
10 # undef KERNEL
11 # undef _KERNEL
12 # define        KERNEL	1
13 # define        _KERNEL	1
14 #endif
15 #include <sys/errno.h>
16 #include <sys/types.h>
17 #include <sys/param.h>
18 #include <sys/time.h>
19 #if defined(__NetBSD__)
20 # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
21 #  include "opt_ipfilter_log.h"
22 # endif
23 #endif
24 #if defined(_KERNEL) && defined(__FreeBSD_version) && \
25     (__FreeBSD_version >= 220000)
26 # if (__FreeBSD_version >= 400000)
27 #  if !defined(IPFILTER_LKM)
28 #   include "opt_inet6.h"
29 #  endif
30 #  if (__FreeBSD_version == 400019)
31 #   define CSUM_DELAY_DATA
32 #  endif
33 # endif
34 # include <sys/filio.h>
35 #else
36 # include <sys/ioctl.h>
37 #endif
38 #if !defined(_AIX51)
39 # include <sys/fcntl.h>
40 #endif
41 #if defined(_KERNEL)
42 # include <sys/systm.h>
43 # include <sys/file.h>
44 #else
45 # include <stdio.h>
46 # include <string.h>
47 # include <stdlib.h>
48 # include <stddef.h>
49 # include <sys/file.h>
50 # define _KERNEL
51 # ifdef __OpenBSD__
52 struct file;
53 # endif
54 # include <sys/uio.h>
55 # undef _KERNEL
56 #endif
57 #if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \
58     !defined(linux)
59 # include <sys/mbuf.h>
60 #else
61 # if !defined(linux)
62 #  include <sys/byteorder.h>
63 # endif
64 # if (SOLARIS2 < 5) && defined(sun)
65 #  include <sys/dditypes.h>
66 # endif
67 #endif
68 #ifdef __hpux
69 # define _NET_ROUTE_INCLUDED
70 #endif
71 #if !defined(linux)
72 # include <sys/protosw.h>
73 #endif
74 #include <sys/socket.h>
75 #include <net/if.h>
76 #ifdef sun
77 # include <net/af.h>
78 #endif
79 #if !defined(_KERNEL) && defined(__FreeBSD__)
80 # include "radix_ipf.h"
81 #endif
82 #include <net/route.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #if !defined(linux)
87 # include <netinet/ip_var.h>
88 #endif
89 #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
90 # include <sys/hashing.h>
91 # include <netinet/in_var.h>
92 #endif
93 #include <netinet/tcp.h>
94 #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL)
95 # include <netinet/udp.h>
96 # include <netinet/ip_icmp.h>
97 #endif
98 #ifdef __hpux
99 # undef _NET_ROUTE_INCLUDED
100 #endif
101 #include "netinet/ip_compat.h"
102 #ifdef	USE_INET6
103 # include <netinet/icmp6.h>
104 # if !SOLARIS && defined(_KERNEL) && !defined(__osf__) && !defined(__hpux)
105 #  include <netinet6/in6_var.h>
106 # endif
107 #endif
108 #include <netinet/tcpip.h>
109 #include "netinet/ip_fil.h"
110 #include "netinet/ip_nat.h"
111 #include "netinet/ip_frag.h"
112 #include "netinet/ip_state.h"
113 #include "netinet/ip_proxy.h"
114 #include "netinet/ip_auth.h"
115 #include "netinet/ipf_stack.h"
116 #ifdef IPFILTER_SCAN
117 # include "netinet/ip_scan.h"
118 #endif
119 #ifdef IPFILTER_SYNC
120 # include "netinet/ip_sync.h"
121 #endif
122 #include "netinet/ip_pool.h"
123 #include "netinet/ip_htable.h"
124 #ifdef IPFILTER_COMPILED
125 # include "netinet/ip_rules.h"
126 #endif
127 #if defined(IPFILTER_BPF) && defined(_KERNEL)
128 # include <net/bpf.h>
129 #endif
130 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
131 # include <sys/malloc.h>
132 # if defined(_KERNEL) && !defined(IPFILTER_LKM)
133 #  include "opt_ipfilter.h"
134 # endif
135 #endif
136 #include "netinet/ipl.h"
137 /* END OF INCLUDES */
138 
139 #if !defined(lint)
140 static const char sccsid[] = "@(#)fil.c	1.36 6/5/96 (C) 1993-2000 Darren Reed";
141 static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $";
142 #endif
143 
144 #ifndef	_KERNEL
145 # include "ipf.h"
146 # include "ipt.h"
147 # include "bpf-ipf.h"
148 extern	int	opts;
149 
150 # define	FR_VERBOSE(verb_pr)			verbose verb_pr
151 # define	FR_DEBUG(verb_pr)			debug verb_pr
152 #else /* #ifndef _KERNEL */
153 # define	FR_VERBOSE(verb_pr)
154 # define	FR_DEBUG(verb_pr)
155 #endif /* _KERNEL */
156 
157 
158 char	ipfilter_version[] = IPL_VERSION;
159 int	fr_features = 0
160 #ifdef	IPFILTER_LKM
161 		| IPF_FEAT_LKM
162 #endif
163 #ifdef	IPFILTER_LOG
164 		| IPF_FEAT_LOG
165 #endif
166 #ifdef	IPFILTER_LOOKUP
167 		| IPF_FEAT_LOOKUP
168 #endif
169 #ifdef	IPFILTER_BPF
170 		| IPF_FEAT_BPF
171 #endif
172 #ifdef	IPFILTER_COMPILED
173 		| IPF_FEAT_COMPILED
174 #endif
175 #ifdef	IPFILTER_CKSUM
176 		| IPF_FEAT_CKSUM
177 #endif
178 #ifdef	IPFILTER_SYNC
179 		| IPF_FEAT_SYNC
180 #endif
181 #ifdef	IPFILTER_SCAN
182 		| IPF_FEAT_SCAN
183 #endif
184 #ifdef	USE_INET6
185 		| IPF_FEAT_IPV6
186 #endif
187 	;
188 
189 #define	IPF_BUMP(x)	(x)++
190 
191 static	INLINE int	fr_ipfcheck __P((fr_info_t *, frentry_t *, int));
192 static	INLINE int	fr_ipfcheck __P((fr_info_t *, frentry_t *, int));
193 static	int		fr_portcheck __P((frpcmp_t *, u_short *));
194 static	int		frflushlist __P((int, minor_t, int *, frentry_t **,
195 					 ipf_stack_t *));
196 static	ipfunc_t	fr_findfunc __P((ipfunc_t));
197 static	frentry_t	*fr_firewall __P((fr_info_t *, u_32_t *));
198 static	int		fr_funcinit __P((frentry_t *fr, ipf_stack_t *));
199 static	INLINE void	frpr_ah __P((fr_info_t *));
200 static	INLINE void	frpr_esp __P((fr_info_t *));
201 static	INLINE void	frpr_gre __P((fr_info_t *));
202 static	INLINE void	frpr_udp __P((fr_info_t *));
203 static	INLINE void	frpr_tcp __P((fr_info_t *));
204 static	INLINE void	frpr_icmp __P((fr_info_t *));
205 static	INLINE void	frpr_ipv4hdr __P((fr_info_t *));
206 static	INLINE int	frpr_pullup __P((fr_info_t *, int));
207 static	INLINE void	frpr_short __P((fr_info_t *, int));
208 static	INLINE void	frpr_tcpcommon __P((fr_info_t *));
209 static	INLINE void	frpr_udpcommon __P((fr_info_t *));
210 static	INLINE int	fr_updateipid __P((fr_info_t *));
211 #ifdef	IPFILTER_LOOKUP
212 static	int		fr_grpmapinit __P((frentry_t *fr, ipf_stack_t *));
213 static	INLINE void	*fr_resolvelookup __P((u_int, u_int, lookupfunc_t *,
214 					       ipf_stack_t *));
215 #endif
216 static	void		frsynclist __P((int, int, void *, char *, frentry_t *,
217     ipf_stack_t *));
218 static	void		*fr_ifsync __P((int, int, char *, char *,
219 					void *, void *, ipf_stack_t *));
220 static	ipftuneable_t	*fr_findtunebyname __P((const char *, ipf_stack_t *));
221 static	ipftuneable_t	*fr_findtunebycookie __P((void *, void **, ipf_stack_t *));
222 
223 /*
224  * bit values for identifying presence of individual IP options
225  * All of these tables should be ordered by increasing key value on the left
226  * hand side to allow for binary searching of the array and include a trailer
227  * with a 0 for the bitmask for linear searches to easily find the end with.
228  */
229 const	struct	optlist	ipopts[20] = {
230 	{ IPOPT_NOP,	0x000001 },
231 	{ IPOPT_RR,	0x000002 },
232 	{ IPOPT_ZSU,	0x000004 },
233 	{ IPOPT_MTUP,	0x000008 },
234 	{ IPOPT_MTUR,	0x000010 },
235 	{ IPOPT_ENCODE,	0x000020 },
236 	{ IPOPT_TS,	0x000040 },
237 	{ IPOPT_TR,	0x000080 },
238 	{ IPOPT_SECURITY, 0x000100 },
239 	{ IPOPT_LSRR,	0x000200 },
240 	{ IPOPT_E_SEC,	0x000400 },
241 	{ IPOPT_CIPSO,	0x000800 },
242 	{ IPOPT_SATID,	0x001000 },
243 	{ IPOPT_SSRR,	0x002000 },
244 	{ IPOPT_ADDEXT,	0x004000 },
245 	{ IPOPT_VISA,	0x008000 },
246 	{ IPOPT_IMITD,	0x010000 },
247 	{ IPOPT_EIP,	0x020000 },
248 	{ IPOPT_FINN,	0x040000 },
249 	{ 0,		0x000000 }
250 };
251 
252 #ifdef USE_INET6
253 struct optlist ip6exthdr[] = {
254 	{ IPPROTO_HOPOPTS,		0x000001 },
255 	{ IPPROTO_IPV6,			0x000002 },
256 	{ IPPROTO_ROUTING,		0x000004 },
257 	{ IPPROTO_FRAGMENT,		0x000008 },
258 	{ IPPROTO_ESP,			0x000010 },
259 	{ IPPROTO_AH,			0x000020 },
260 	{ IPPROTO_NONE,			0x000040 },
261 	{ IPPROTO_DSTOPTS,		0x000080 },
262 	{ 0,				0 }
263 };
264 #endif
265 
266 struct optlist tcpopts[] = {
267 	{ TCPOPT_NOP,			0x000001 },
268 	{ TCPOPT_MAXSEG,		0x000002 },
269 	{ TCPOPT_WINDOW,		0x000004 },
270 	{ TCPOPT_SACK_PERMITTED,	0x000008 },
271 	{ TCPOPT_SACK,			0x000010 },
272 	{ TCPOPT_TIMESTAMP,		0x000020 },
273 	{ 0,				0x000000 }
274 };
275 
276 /*
277  * bit values for identifying presence of individual IP security options
278  */
279 const	struct	optlist	secopt[8] = {
280 	{ IPSO_CLASS_RES4,	0x01 },
281 	{ IPSO_CLASS_TOPS,	0x02 },
282 	{ IPSO_CLASS_SECR,	0x04 },
283 	{ IPSO_CLASS_RES3,	0x08 },
284 	{ IPSO_CLASS_CONF,	0x10 },
285 	{ IPSO_CLASS_UNCL,	0x20 },
286 	{ IPSO_CLASS_RES2,	0x40 },
287 	{ IPSO_CLASS_RES1,	0x80 }
288 };
289 
290 
291 /*
292  * Table of functions available for use with call rules.
293  */
294 static ipfunc_resolve_t fr_availfuncs[] = {
295 #ifdef	IPFILTER_LOOKUP
296 	{ "fr_srcgrpmap", fr_srcgrpmap, fr_grpmapinit },
297 	{ "fr_dstgrpmap", fr_dstgrpmap, fr_grpmapinit },
298 #endif
299 	{ "", NULL }
300 };
301 
302 
303 /*
304  * Below we declare a list of constants used only by the ipf_extraflush()
305  * routine.  We are placing it here, instead of in ipf_extraflush() itself,
306  * because we want to make it visible to tools such as mdb, nm etc., so the
307  * values can easily be altered during debugging.
308  */
309 static	const	int	idletime_tab[] = {
310 	IPF_TTLVAL(30),		/* 30 seconds */
311 	IPF_TTLVAL(1800),	/* 30 minutes */
312 	IPF_TTLVAL(43200),	/* 12 hours */
313 	IPF_TTLVAL(345600),	/* 4 days */
314 };
315 
316 
317 /*
318  * The next section of code is a a collection of small routines that set
319  * fields in the fr_info_t structure passed based on properties of the
320  * current packet.  There are different routines for the same protocol
321  * for each of IPv4 and IPv6.  Adding a new protocol, for which there
322  * will "special" inspection for setup, is now more easily done by adding
323  * a new routine and expanding the frpr_ipinit*() function rather than by
324  * adding more code to a growing switch statement.
325  */
326 #ifdef USE_INET6
327 static	INLINE int	frpr_ah6 __P((fr_info_t *));
328 static	INLINE void	frpr_esp6 __P((fr_info_t *));
329 static	INLINE void	frpr_gre6 __P((fr_info_t *));
330 static	INLINE void	frpr_udp6 __P((fr_info_t *));
331 static	INLINE void	frpr_tcp6 __P((fr_info_t *));
332 static	INLINE void	frpr_icmp6 __P((fr_info_t *));
333 static	INLINE void	frpr_ipv6hdr __P((fr_info_t *));
334 static	INLINE void	frpr_short6 __P((fr_info_t *, int));
335 static	INLINE int	frpr_hopopts6 __P((fr_info_t *));
336 static	INLINE int	frpr_routing6 __P((fr_info_t *));
337 static	INLINE int	frpr_dstopts6 __P((fr_info_t *));
338 static	INLINE int	frpr_fragment6 __P((fr_info_t *));
339 static	INLINE int	frpr_ipv6exthdr __P((fr_info_t *, int, int));
340 
341 
342 /* ------------------------------------------------------------------------ */
343 /* Function:    frpr_short6                                                 */
344 /* Returns:     void                                                        */
345 /* Parameters:  fin(I) - pointer to packet information                      */
346 /*                                                                          */
347 /* IPv6 Only                                                                */
348 /* This is function enforces the 'is a packet too short to be legit' rule   */
349 /* for IPv6 and marks the packet with FI_SHORT if so.  See function comment */
350 /* for frpr_short() for more details.                                       */
351 /* ------------------------------------------------------------------------ */
352 static INLINE void frpr_short6(fin, xmin)
353 fr_info_t *fin;
354 int xmin;
355 {
356 
357 	if (fin->fin_dlen < xmin)
358 		fin->fin_flx |= FI_SHORT;
359 }
360 
361 
362 /* ------------------------------------------------------------------------ */
363 /* Function:    frpr_ipv6hdr                                                */
364 /* Returns:     Nil                                                         */
365 /* Parameters:  fin(I) - pointer to packet information                      */
366 /*                                                                          */
367 /* IPv6 Only                                                                */
368 /* Copy values from the IPv6 header into the fr_info_t struct and call the  */
369 /* per-protocol analyzer if it exists.                                      */
370 /* ------------------------------------------------------------------------ */
371 static INLINE void frpr_ipv6hdr(fin)
372 fr_info_t *fin;
373 {
374 	ip6_t *ip6 = (ip6_t *)fin->fin_ip;
375 	int p, go = 1, i, hdrcount;
376 	fr_ip_t *fi = &fin->fin_fi;
377 
378 	fin->fin_off = 0;
379 
380 	fi->fi_tos = 0;
381 	fi->fi_optmsk = 0;
382 	fi->fi_secmsk = 0;
383 	fi->fi_auth = 0;
384 
385 	p = ip6->ip6_nxt;
386 	fi->fi_ttl = ip6->ip6_hlim;
387 	fi->fi_src.in6 = ip6->ip6_src;
388 	fi->fi_dst.in6 = ip6->ip6_dst;
389 	fin->fin_id = 0;
390 
391 	hdrcount = 0;
392 	while (go && !(fin->fin_flx & (FI_BAD|FI_SHORT))) {
393 		switch (p)
394 		{
395 		case IPPROTO_UDP :
396 			frpr_udp6(fin);
397 			go = 0;
398 			break;
399 
400 		case IPPROTO_TCP :
401 			frpr_tcp6(fin);
402 			go = 0;
403 			break;
404 
405 		case IPPROTO_ICMPV6 :
406 			frpr_icmp6(fin);
407 			go = 0;
408 			break;
409 
410 		case IPPROTO_GRE :
411 			frpr_gre6(fin);
412 			go = 0;
413 			break;
414 
415 		case IPPROTO_HOPOPTS :
416 			/*
417 			 * hop by hop ext header is only allowed
418 			 * right after IPv6 header.
419 			 */
420 			if (hdrcount != 0) {
421 				fin->fin_flx |= FI_BAD;
422 				p = IPPROTO_NONE;
423 			} else {
424 				p = frpr_hopopts6(fin);
425 			}
426 			break;
427 
428 		case IPPROTO_DSTOPTS :
429 			p = frpr_dstopts6(fin);
430 			break;
431 
432 		case IPPROTO_ROUTING :
433 			p = frpr_routing6(fin);
434 			break;
435 
436 		case IPPROTO_AH :
437 			p = frpr_ah6(fin);
438 			break;
439 
440 		case IPPROTO_ESP :
441 			frpr_esp6(fin);
442 			go = 0;
443 			break;
444 
445 		case IPPROTO_IPV6 :
446 			for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
447 				if (ip6exthdr[i].ol_val == p) {
448 					fin->fin_flx |= ip6exthdr[i].ol_bit;
449 					break;
450 				}
451 			go = 0;
452 			break;
453 
454 		case IPPROTO_NONE :
455 			go = 0;
456 			break;
457 
458 		case IPPROTO_FRAGMENT :
459 			p = frpr_fragment6(fin);
460 			if (fin->fin_off != 0)  /* Not the first frag */
461 				go = 0;
462 			break;
463 
464 		default :
465 			go = 0;
466 			break;
467 		}
468 		hdrcount++;
469 
470 		/*
471 		 * It is important to note that at this point, for the
472 		 * extension headers (go != 0), the entire header may not have
473 		 * been pulled up when the code gets to this point.  This is
474 		 * only done for "go != 0" because the other header handlers
475 		 * will all pullup their complete header.  The other indicator
476 		 * of an incomplete packet is that this was just an extension
477 		 * header.
478 		 */
479 		if ((go != 0) && (p != IPPROTO_NONE) &&
480 		    (frpr_pullup(fin, 0) == -1)) {
481 			p = IPPROTO_NONE;
482 			go = 0;
483 		}
484 	}
485 	fi->fi_p = p;
486 }
487 
488 
489 /* ------------------------------------------------------------------------ */
490 /* Function:    frpr_ipv6exthdr                                             */
491 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
492 /* Parameters:  fin(I)      - pointer to packet information                 */
493 /*              multiple(I) - flag indicating yes/no if multiple occurances */
494 /*                            of this extension header are allowed.         */
495 /*              proto(I)    - protocol number for this extension header     */
496 /*                                                                          */
497 /* IPv6 Only                                                                */
498 /* This function expects to find an IPv6 extension header at fin_dp.        */
499 /* There must be at least 8 bytes of data at fin_dp for there to be a valid */
500 /* extension header present. If a good one is found, fin_dp is advanced to  */
501 /* point at the first piece of data after the extension header, fin_exthdr  */
502 /* points to the start of the extension header and the "protocol" of the    */
503 /* *NEXT* header is returned.                                               */
504 /* ------------------------------------------------------------------------ */
505 static INLINE int frpr_ipv6exthdr(fin, multiple, proto)
506 fr_info_t *fin;
507 int multiple, proto;
508 {
509 	struct ip6_ext *hdr;
510 	u_short shift;
511 	int i;
512 
513 	fin->fin_flx |= FI_V6EXTHDR;
514 
515 				/* 8 is default length of extension hdr */
516 	if ((fin->fin_dlen - 8) < 0) {
517 		fin->fin_flx |= FI_SHORT;
518 		return IPPROTO_NONE;
519 	}
520 
521 	if (frpr_pullup(fin, 8) == -1)
522 		return IPPROTO_NONE;
523 
524 	hdr = fin->fin_dp;
525 	shift = 8 + (hdr->ip6e_len << 3);
526 	if (shift > fin->fin_dlen) {	/* Nasty extension header length? */
527 		fin->fin_flx |= FI_BAD;
528 		return IPPROTO_NONE;
529 	}
530 
531 	for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
532 		if (ip6exthdr[i].ol_val == proto) {
533 			/*
534 			 * Most IPv6 extension headers are only allowed once.
535 			 */
536 			if ((multiple == 0) &&
537 			    ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0))
538 				fin->fin_flx |= FI_BAD;
539 			else
540 				fin->fin_optmsk |= ip6exthdr[i].ol_bit;
541 			break;
542 		}
543 
544 	fin->fin_dp = (char *)fin->fin_dp + shift;
545 	fin->fin_dlen -= shift;
546 
547 	return hdr->ip6e_nxt;
548 }
549 
550 
551 /* ------------------------------------------------------------------------ */
552 /* Function:    frpr_hopopts6                                               */
553 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
554 /* Parameters:  fin(I) - pointer to packet information                      */
555 /*                                                                          */
556 /* IPv6 Only                                                                */
557 /* This is function checks pending hop by hop options extension header      */
558 /* ------------------------------------------------------------------------ */
559 static INLINE int frpr_hopopts6(fin)
560 fr_info_t *fin;
561 {
562 	return frpr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
563 }
564 
565 
566 /* ------------------------------------------------------------------------ */
567 /* Function:    frpr_routing6                                               */
568 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
569 /* Parameters:  fin(I) - pointer to packet information                      */
570 /*                                                                          */
571 /* IPv6 Only                                                                */
572 /* This is function checks pending routing extension header                 */
573 /* ------------------------------------------------------------------------ */
574 static INLINE int frpr_routing6(fin)
575 fr_info_t *fin;
576 {
577 	struct ip6_ext *hdr;
578 	int shift;
579 
580 	hdr = fin->fin_dp;
581 	if (frpr_ipv6exthdr(fin, 0, IPPROTO_ROUTING) == IPPROTO_NONE)
582 		return IPPROTO_NONE;
583 
584 	shift = 8 + (hdr->ip6e_len << 3);
585 	/*
586 	 * Nasty extension header length?
587 	 */
588 	if ((hdr->ip6e_len << 3) & 15) {
589 		fin->fin_flx |= FI_BAD;
590 		/*
591 		 * Compensate for the changes made in frpr_ipv6exthdr()
592 		 */
593 		fin->fin_dlen += shift;
594 		fin->fin_dp = (char *)fin->fin_dp - shift;
595 		return IPPROTO_NONE;
596 	}
597 
598 	return hdr->ip6e_nxt;
599 }
600 
601 
602 /* ------------------------------------------------------------------------ */
603 /* Function:    frpr_fragment6                                              */
604 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
605 /* Parameters:  fin(I) - pointer to packet information                      */
606 /*                                                                          */
607 /* IPv6 Only                                                                */
608 /* Examine the IPv6 fragment header and extract fragment offset information.*/
609 /*                                                                          */
610 /* We don't know where the transport layer header (or whatever is next is), */
611 /* as it could be behind destination options (amongst others).  Because     */
612 /* there is no fragment cache, there is no knowledge about whether or not an*/
613 /* upper layer header has been seen (or where it ends) and thus we are not  */
614 /* able to continue processing beyond this header with any confidence.      */
615 /* ------------------------------------------------------------------------ */
616 static INLINE int frpr_fragment6(fin)
617 fr_info_t *fin;
618 {
619 	struct ip6_frag *frag;
620 
621 	fin->fin_flx |= FI_FRAG;
622 
623 	/*
624 	 * A fragmented IPv6 packet implies that there must be something
625 	 * else after the fragment.
626 	 */
627 	if (frpr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT) == IPPROTO_NONE)
628 		return IPPROTO_NONE;
629 
630 	frag = (struct ip6_frag *)((char *)fin->fin_dp - sizeof(*frag));
631 
632 	/*
633 	 * If this fragment isn't the last then the packet length must
634 	 * be a multiple of 8.
635 	 */
636 	if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) {
637 		fin->fin_flx |= FI_MOREFRAG;
638 
639 		if ((fin->fin_plen & 0x7) != 0)
640 			fin->fin_flx |= FI_BAD;
641 	}
642 
643 	fin->fin_id = frag->ip6f_ident;
644 	fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
645 	if (fin->fin_off != 0)
646 		fin->fin_flx |= FI_FRAGBODY;
647 
648 	return frag->ip6f_nxt;
649 }
650 
651 
652 /* ------------------------------------------------------------------------ */
653 /* Function:    frpr_dstopts6                                               */
654 /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
655 /* Parameters:  fin(I) - pointer to packet information                      */
656 /*              nextheader(I) - stores next header value                    */
657 /*                                                                          */
658 /* IPv6 Only                                                                */
659 /* This is function checks pending destination options extension header     */
660 /* ------------------------------------------------------------------------ */
661 static INLINE int frpr_dstopts6(fin)
662 fr_info_t *fin;
663 {
664 	return frpr_ipv6exthdr(fin, 1, IPPROTO_DSTOPTS);
665 }
666 
667 
668 /* ------------------------------------------------------------------------ */
669 /* Function:    frpr_icmp6                                                  */
670 /* Returns:     void                                                        */
671 /* Parameters:  fin(I) - pointer to packet information                      */
672 /*                                                                          */
673 /* IPv6 Only                                                                */
674 /* This routine is mainly concerned with determining the minimum valid size */
675 /* for an ICMPv6 packet.                                                    */
676 /* ------------------------------------------------------------------------ */
677 static INLINE void frpr_icmp6(fin)
678 fr_info_t *fin;
679 {
680 	int minicmpsz = sizeof(struct icmp6_hdr);
681 	struct icmp6_hdr *icmp6;
682 
683 	if (frpr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1)
684 		return;
685 
686 	if (fin->fin_dlen > 1) {
687 		icmp6 = fin->fin_dp;
688 
689 		fin->fin_data[0] = *(u_short *)icmp6;
690 
691 		if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0)
692 			fin->fin_flx |= FI_ICMPQUERY;
693 
694 		switch (icmp6->icmp6_type)
695 		{
696 		case ICMP6_ECHO_REPLY :
697 		case ICMP6_ECHO_REQUEST :
698 			if (fin->fin_dlen >= 6)
699 				fin->fin_data[1] = icmp6->icmp6_id;
700 			minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t);
701 			break;
702 		case ICMP6_DST_UNREACH :
703 		case ICMP6_PACKET_TOO_BIG :
704 		case ICMP6_TIME_EXCEEDED :
705 		case ICMP6_PARAM_PROB :
706 			if ((fin->fin_m != NULL) &&
707 			    (M_LEN(fin->fin_m) < fin->fin_plen)) {
708 				if (fr_coalesce(fin) != 1)
709 					return;
710 			}
711 			fin->fin_flx |= FI_ICMPERR;
712 			minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t);
713 			break;
714 		default :
715 			break;
716 		}
717 	}
718 
719 	frpr_short6(fin, minicmpsz);
720 }
721 
722 
723 /* ------------------------------------------------------------------------ */
724 /* Function:    frpr_udp6                                                   */
725 /* Returns:     void                                                        */
726 /* Parameters:  fin(I) - pointer to packet information                      */
727 /*                                                                          */
728 /* IPv6 Only                                                                */
729 /* Analyse the packet for IPv6/UDP properties.                              */
730 /* Is not expected to be called for fragmented packets.                     */
731 /* ------------------------------------------------------------------------ */
732 static INLINE void frpr_udp6(fin)
733 fr_info_t *fin;
734 {
735 
736 	fr_checkv6sum(fin);
737 
738 	frpr_short6(fin, sizeof(struct udphdr));
739 	if (frpr_pullup(fin, sizeof(struct udphdr)) == -1)
740 		return;
741 
742 	frpr_udpcommon(fin);
743 }
744 
745 
746 /* ------------------------------------------------------------------------ */
747 /* Function:    frpr_tcp6                                                   */
748 /* Returns:     void                                                        */
749 /* Parameters:  fin(I) - pointer to packet information                      */
750 /*                                                                          */
751 /* IPv6 Only                                                                */
752 /* Analyse the packet for IPv6/TCP properties.                              */
753 /* Is not expected to be called for fragmented packets.                     */
754 /* ------------------------------------------------------------------------ */
755 static INLINE void frpr_tcp6(fin)
756 fr_info_t *fin;
757 {
758 
759 	fr_checkv6sum(fin);
760 
761 	frpr_short6(fin, sizeof(struct tcphdr));
762 	if (frpr_pullup(fin, sizeof(struct tcphdr)) == -1)
763 		return;
764 
765 	frpr_tcpcommon(fin);
766 }
767 
768 
769 /* ------------------------------------------------------------------------ */
770 /* Function:    frpr_esp6                                                   */
771 /* Returns:     void                                                        */
772 /* Parameters:  fin(I) - pointer to packet information                      */
773 /*                                                                          */
774 /* IPv6 Only                                                                */
775 /* Analyse the packet for ESP properties.                                   */
776 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits)  */
777 /* even though the newer ESP packets must also have a sequence number that  */
778 /* is 32bits as well, it is not possible(?) to determine the version from a */
779 /* simple packet header.                                                    */
780 /* ------------------------------------------------------------------------ */
781 static INLINE void frpr_esp6(fin)
782 fr_info_t *fin;
783 {
784 	int i;
785 	frpr_short6(fin, sizeof(grehdr_t));
786 
787 	(void) frpr_pullup(fin, 8);
788 
789 	for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
790 		if (ip6exthdr[i].ol_val == IPPROTO_ESP) {
791 			fin->fin_optmsk |= ip6exthdr[i].ol_bit;
792 			break;
793 		}
794 }
795 
796 
797 /* ------------------------------------------------------------------------ */
798 /* Function:    frpr_ah6                                                    */
799 /* Returns:     void                                                        */
800 /* Parameters:  fin(I) - pointer to packet information                      */
801 /*                                                                          */
802 /* IPv6 Only                                                                */
803 /* Analyse the packet for AH properties.                                    */
804 /* The minimum length is taken to be the combination of all fields in the   */
805 /* header being present and no authentication data (null algorithm used.)   */
806 /* ------------------------------------------------------------------------ */
807 static INLINE int frpr_ah6(fin)
808 fr_info_t *fin;
809 {
810 	authhdr_t *ah;
811 	int i, shift;
812 
813 	frpr_short6(fin, 12);
814 
815 	if (frpr_pullup(fin, sizeof(*ah)) == -1)
816 		return IPPROTO_NONE;
817 
818 	for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
819 		if (ip6exthdr[i].ol_val == IPPROTO_AH) {
820 			fin->fin_optmsk |= ip6exthdr[i].ol_bit;
821 			break;
822 		}
823 
824 	ah = (authhdr_t *)fin->fin_dp;
825 
826 	shift = (ah->ah_plen + 2) * 4;
827 	fin->fin_dlen -= shift;
828 	fin->fin_dp = (char*)fin->fin_dp + shift;
829 
830 	return ah->ah_next;
831 }
832 
833 
834 /* ------------------------------------------------------------------------ */
835 /* Function:    frpr_gre6                                                   */
836 /* Returns:     void                                                        */
837 /* Parameters:  fin(I) - pointer to packet information                      */
838 /*                                                                          */
839 /* Analyse the packet for GRE properties.                                   */
840 /* ------------------------------------------------------------------------ */
841 static INLINE void frpr_gre6(fin)
842 fr_info_t *fin;
843 {
844 	grehdr_t *gre;
845 
846 	frpr_short6(fin, sizeof(grehdr_t));
847 
848 	if (frpr_pullup(fin, sizeof(grehdr_t)) == -1)
849 		return;
850 
851 	gre = fin->fin_dp;
852 	if (GRE_REV(gre->gr_flags) == 1)
853 		fin->fin_data[0] = gre->gr_call;
854 }
855 #endif	/* USE_INET6 */
856 
857 
858 /* ------------------------------------------------------------------------ */
859 /* Function:    frpr_pullup                                                 */
860 /* Returns:     int     - 0 == pullup succeeded, -1 == failure              */
861 /* Parameters:  fin(I)  - pointer to packet information                     */
862 /*              plen(I) - length (excluding L3 header) to pullup            */
863 /*                                                                          */
864 /* Short inline function to cut down on code duplication to perform a call  */
865 /* to fr_pullup to ensure there is the required amount of data,             */
866 /* consecutively in the packet buffer.                                      */
867 /* ------------------------------------------------------------------------ */
868 static INLINE int frpr_pullup(fin, plen)
869 fr_info_t *fin;
870 int plen;
871 {
872 #if defined(_KERNEL)
873 	if (fin->fin_m != NULL) {
874 		int ipoff;
875 
876 		ipoff = (char *)fin->fin_ip - MTOD(fin->fin_m, char *);
877 
878 		if (fin->fin_dp != NULL)
879 			plen += (char *)fin->fin_dp -
880 				((char *)fin->fin_ip + fin->fin_hlen);
881 		plen += fin->fin_hlen;
882 		/*
883 		 * We don't do 'plen += ipoff;' here. The fr_pullup() will
884 		 * do it for us.
885 		 */
886 		if (M_LEN(fin->fin_m) < plen + ipoff) {
887 			if (fr_pullup(fin->fin_m, fin, plen) == NULL)
888 				return -1;
889 		}
890 	}
891 #endif
892 	return 0;
893 }
894 
895 
896 /* ------------------------------------------------------------------------ */
897 /* Function:    frpr_short                                                  */
898 /* Returns:     void                                                        */
899 /* Parameters:  fin(I)  - pointer to packet information                     */
900 /*              xmin(I) - minimum header size                               */
901 /*                                                                          */
902 /* Check if a packet is "short" as defined by xmin.  The rule we are        */
903 /* applying here is that the packet must not be fragmented within the layer */
904 /* 4 header.  That is, it must not be a fragment that has its offset set to */
905 /* start within the layer 4 header (hdrmin) or if it is at offset 0, the    */
906 /* entire layer 4 header must be present (min).                             */
907 /* ------------------------------------------------------------------------ */
908 static INLINE void frpr_short(fin, xmin)
909 fr_info_t *fin;
910 int xmin;
911 {
912 
913 	if (fin->fin_off == 0) {
914 		if (fin->fin_dlen < xmin)
915 			fin->fin_flx |= FI_SHORT;
916 	} else if (fin->fin_off < xmin) {
917 		fin->fin_flx |= FI_SHORT;
918 	}
919 }
920 
921 
922 /* ------------------------------------------------------------------------ */
923 /* Function:    frpr_icmp                                                   */
924 /* Returns:     void                                                        */
925 /* Parameters:  fin(I) - pointer to packet information                      */
926 /*                                                                          */
927 /* IPv4 Only                                                                */
928 /* Do a sanity check on the packet for ICMP (v4).  In nearly all cases,     */
929 /* except extrememly bad packets, both type and code will be present.       */
930 /* The expected minimum size of an ICMP packet is very much dependent on    */
931 /* the type of it.                                                          */
932 /*                                                                          */
933 /* XXX - other ICMP sanity checks?                                          */
934 /* ------------------------------------------------------------------------ */
935 static INLINE void frpr_icmp(fin)
936 fr_info_t *fin;
937 {
938 	int minicmpsz = sizeof(struct icmp);
939 	icmphdr_t *icmp;
940 	ip_t *oip;
941 	ipf_stack_t *ifs = fin->fin_ifs;
942 
943 	if (fin->fin_off != 0) {
944 		frpr_short(fin, ICMPERR_ICMPHLEN);
945 		return;
946 	}
947 
948 	if (frpr_pullup(fin, ICMPERR_ICMPHLEN) == -1)
949 		return;
950 
951 	fr_checkv4sum(fin);
952 
953 	/*
954 	 * This is a right place to set icmp pointer, since the memory
955 	 * referenced by fin_dp could get reallocated. The code down below can
956 	 * rely on fact icmp variable always points to ICMP header.
957 	 */
958 	icmp = fin->fin_dp;
959 	fin->fin_data[0] = *(u_short *)icmp;
960 	fin->fin_data[1] = icmp->icmp_id;
961 
962 	switch (icmp->icmp_type)
963 	{
964 	case ICMP_ECHOREPLY :
965 	case ICMP_ECHO :
966 	/* Router discovery messaes - RFC 1256 */
967 	case ICMP_ROUTERADVERT :
968 	case ICMP_ROUTERSOLICIT :
969 		minicmpsz = ICMP_MINLEN;
970 		break;
971 	/*
972 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
973 	 * 3 * timestamp(3 * 4)
974 	 */
975 	case ICMP_TSTAMP :
976 	case ICMP_TSTAMPREPLY :
977 		minicmpsz = 20;
978 		break;
979 	/*
980 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
981 	 * mask(4)
982 	 */
983 	case ICMP_MASKREQ :
984 	case ICMP_MASKREPLY :
985 			minicmpsz = 12;
986 			break;
987 	/*
988 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
989 	 */
990 	case ICMP_UNREACH :
991 		if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) {
992 			if (icmp->icmp_nextmtu < ifs->ifs_fr_icmpminfragmtu)
993 				fin->fin_flx |= FI_BAD;
994 		}
995 		/* FALLTHRU */
996 	case ICMP_SOURCEQUENCH :
997 	case ICMP_REDIRECT :
998 	case ICMP_TIMXCEED :
999 	case ICMP_PARAMPROB :
1000 		fin->fin_flx |= FI_ICMPERR;
1001 		if (fr_coalesce(fin) != 1)
1002 			return;
1003 		/*
1004 		 * ICMP error packets should not be generated for IP
1005 		 * packets that are a fragment that isn't the first
1006 		 * fragment.
1007 		 */
1008 		oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN);
1009 		if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0)
1010 			fin->fin_flx |= FI_BAD;
1011 		break;
1012 	default :
1013 		break;
1014 	}
1015 
1016 	frpr_short(fin, minicmpsz);
1017 }
1018 
1019 
1020 /* ------------------------------------------------------------------------ */
1021 /* Function:    frpr_tcpcommon                                              */
1022 /* Returns:     void                                                        */
1023 /* Parameters:  fin(I) - pointer to packet information                      */
1024 /*                                                                          */
1025 /* TCP header sanity checking.  Look for bad combinations of TCP flags,     */
1026 /* and make some checks with how they interact with other fields.           */
1027 /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is     */
1028 /* valid and mark the packet as bad if not.                                 */
1029 /* ------------------------------------------------------------------------ */
1030 static INLINE void frpr_tcpcommon(fin)
1031 fr_info_t *fin;
1032 {
1033 	int flags, tlen;
1034 	tcphdr_t *tcp;
1035 
1036 	fin->fin_flx |= FI_TCPUDP;
1037 	if (fin->fin_off != 0)
1038 		return;
1039 
1040 	if (frpr_pullup(fin, sizeof(*tcp)) == -1)
1041 		return;
1042 	tcp = fin->fin_dp;
1043 
1044 	if (fin->fin_dlen > 3) {
1045 		fin->fin_sport = ntohs(tcp->th_sport);
1046 		fin->fin_dport = ntohs(tcp->th_dport);
1047 	}
1048 
1049 	if ((fin->fin_flx & FI_SHORT) != 0)
1050 		return;
1051 
1052 	/*
1053 	 * Use of the TCP data offset *must* result in a value that is at
1054 	 * least the same size as the TCP header.
1055 	 */
1056 	tlen = TCP_OFF(tcp) << 2;
1057 	if (tlen < sizeof(tcphdr_t)) {
1058 		fin->fin_flx |= FI_BAD;
1059 		return;
1060 	}
1061 
1062 	flags = tcp->th_flags;
1063 	fin->fin_tcpf = tcp->th_flags;
1064 
1065 	/*
1066 	 * If the urgent flag is set, then the urgent pointer must
1067 	 * also be set and vice versa.  Good TCP packets do not have
1068 	 * just one of these set.
1069 	 */
1070 	if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) {
1071 		fin->fin_flx |= FI_BAD;
1072 	} else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) {
1073 		/* Ignore this case, it shows up in "real" traffic with */
1074 		/* bogus values in the urgent pointer field. */
1075 		flags = flags; /* LINT */
1076 	} else if (((flags & (TH_SYN|TH_FIN)) != 0) &&
1077 		   ((flags & (TH_RST|TH_ACK)) == TH_RST)) {
1078 		/* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
1079 		fin->fin_flx |= FI_BAD;
1080 	} else if (!(flags & TH_ACK)) {
1081 		/*
1082 		 * If the ack bit isn't set, then either the SYN or
1083 		 * RST bit must be set.  If the SYN bit is set, then
1084 		 * we expect the ACK field to be 0.  If the ACK is
1085 		 * not set and if URG, PSH or FIN are set, consdier
1086 		 * that to indicate a bad TCP packet.
1087 		 */
1088 		if ((flags == TH_SYN) && (tcp->th_ack != 0)) {
1089 			/*
1090 			 * Cisco PIX sets the ACK field to a random value.
1091 			 * In light of this, do not set FI_BAD until a patch
1092 			 * is available from Cisco to ensure that
1093 			 * interoperability between existing systems is
1094 			 * achieved.
1095 			 */
1096 			/*fin->fin_flx |= FI_BAD*/;
1097 			flags = flags; /* LINT */
1098 		} else if (!(flags & (TH_RST|TH_SYN))) {
1099 			fin->fin_flx |= FI_BAD;
1100 		} else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) {
1101 			fin->fin_flx |= FI_BAD;
1102 		}
1103 	}
1104 
1105 	/*
1106 	 * At this point, it's not exactly clear what is to be gained by
1107 	 * marking up which TCP options are and are not present.  The one we
1108 	 * are most interested in is the TCP window scale.  This is only in
1109 	 * a SYN packet [RFC1323] so we don't need this here...?
1110 	 * Now if we were to analyse the header for passive fingerprinting,
1111 	 * then that might add some weight to adding this...
1112 	 */
1113 	if (tlen == sizeof(tcphdr_t))
1114 		return;
1115 
1116 	if (frpr_pullup(fin, tlen) == -1)
1117 		return;
1118 
1119 #if 0
1120 	ip = fin->fin_ip;
1121 	s = (u_char *)(tcp + 1);
1122 	off = IP_HL(ip) << 2;
1123 # ifdef _KERNEL
1124 	if (fin->fin_mp != NULL) {
1125 		mb_t *m = *fin->fin_mp;
1126 
1127 		if (off + tlen > M_LEN(m))
1128 			return;
1129 	}
1130 # endif
1131 	for (tlen -= (int)sizeof(*tcp); tlen > 0; ) {
1132 		opt = *s;
1133 		if (opt == '\0')
1134 			break;
1135 		else if (opt == TCPOPT_NOP)
1136 			ol = 1;
1137 		else {
1138 			if (tlen < 2)
1139 				break;
1140 			ol = (int)*(s + 1);
1141 			if (ol < 2 || ol > tlen)
1142 				break;
1143 		}
1144 
1145 		for (i = 9, mv = 4; mv >= 0; ) {
1146 			op = ipopts + i;
1147 			if (opt == (u_char)op->ol_val) {
1148 				optmsk |= op->ol_bit;
1149 				break;
1150 			}
1151 		}
1152 		tlen -= ol;
1153 		s += ol;
1154 	}
1155 #endif /* 0 */
1156 }
1157 
1158 
1159 
1160 /* ------------------------------------------------------------------------ */
1161 /* Function:    frpr_udpcommon                                              */
1162 /* Returns:     void                                                        */
1163 /* Parameters:  fin(I) - pointer to packet information                      */
1164 /*                                                                          */
1165 /* Extract the UDP source and destination ports, if present.  If compiled   */
1166 /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid.          */
1167 /* ------------------------------------------------------------------------ */
1168 static INLINE void frpr_udpcommon(fin)
1169 fr_info_t *fin;
1170 {
1171 	udphdr_t *udp;
1172 
1173 	fin->fin_flx |= FI_TCPUDP;
1174 
1175 	if (!fin->fin_off && (fin->fin_dlen > 3)) {
1176 		if (frpr_pullup(fin, sizeof(*udp)) == -1) {
1177 			fin->fin_flx |= FI_SHORT;
1178 			return;
1179 		}
1180 
1181 		udp = fin->fin_dp;
1182 
1183 		fin->fin_sport = ntohs(udp->uh_sport);
1184 		fin->fin_dport = ntohs(udp->uh_dport);
1185 	}
1186 }
1187 
1188 
1189 /* ------------------------------------------------------------------------ */
1190 /* Function:    frpr_tcp                                                    */
1191 /* Returns:     void                                                        */
1192 /* Parameters:  fin(I) - pointer to packet information                      */
1193 /*                                                                          */
1194 /* IPv4 Only                                                                */
1195 /* Analyse the packet for IPv4/TCP properties.                              */
1196 /* ------------------------------------------------------------------------ */
1197 static INLINE void frpr_tcp(fin)
1198 fr_info_t *fin;
1199 {
1200 
1201 	fr_checkv4sum(fin);
1202 
1203 	frpr_short(fin, sizeof(tcphdr_t));
1204 
1205 	frpr_tcpcommon(fin);
1206 }
1207 
1208 
1209 /* ------------------------------------------------------------------------ */
1210 /* Function:    frpr_udp                                                    */
1211 /* Returns:     void                                                        */
1212 /* Parameters:  fin(I) - pointer to packet information                      */
1213 /*                                                                          */
1214 /* IPv4 Only                                                                */
1215 /* Analyse the packet for IPv4/UDP properties.                              */
1216 /* ------------------------------------------------------------------------ */
1217 static INLINE void frpr_udp(fin)
1218 fr_info_t *fin;
1219 {
1220 
1221 	fr_checkv4sum(fin);
1222 
1223 	frpr_short(fin, sizeof(udphdr_t));
1224 
1225 	frpr_udpcommon(fin);
1226 }
1227 
1228 
1229 /* ------------------------------------------------------------------------ */
1230 /* Function:    frpr_esp                                                    */
1231 /* Returns:     void                                                        */
1232 /* Parameters:  fin(I) - pointer to packet information                      */
1233 /*                                                                          */
1234 /* Analyse the packet for ESP properties.                                   */
1235 /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits)  */
1236 /* even though the newer ESP packets must also have a sequence number that  */
1237 /* is 32bits as well, it is not possible(?) to determine the version from a */
1238 /* simple packet header.                                                    */
1239 /* ------------------------------------------------------------------------ */
1240 static INLINE void frpr_esp(fin)
1241 fr_info_t *fin;
1242 {
1243 	if ((fin->fin_off == 0) && (frpr_pullup(fin, 8) == -1))
1244 		return;
1245 
1246 	frpr_short(fin, 8);
1247 }
1248 
1249 
1250 /* ------------------------------------------------------------------------ */
1251 /* Function:    frpr_ah                                                     */
1252 /* Returns:     void                                                        */
1253 /* Parameters:  fin(I) - pointer to packet information                      */
1254 /*                                                                          */
1255 /* Analyse the packet for AH properties.                                    */
1256 /* The minimum length is taken to be the combination of all fields in the   */
1257 /* header being present and no authentication data (null algorithm used.)   */
1258 /* ------------------------------------------------------------------------ */
1259 static INLINE void frpr_ah(fin)
1260 fr_info_t *fin;
1261 {
1262 	authhdr_t *ah;
1263 	int len;
1264 
1265 	if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(*ah)) == -1))
1266 		return;
1267 
1268 	ah = (authhdr_t *)fin->fin_dp;
1269 
1270 	len = (ah->ah_plen + 2) << 2;
1271 	frpr_short(fin, len);
1272 }
1273 
1274 
1275 /* ------------------------------------------------------------------------ */
1276 /* Function:    frpr_gre                                                    */
1277 /* Returns:     void                                                        */
1278 /* Parameters:  fin(I) - pointer to packet information                      */
1279 /*                                                                          */
1280 /* Analyse the packet for GRE properties.                                   */
1281 /* ------------------------------------------------------------------------ */
1282 static INLINE void frpr_gre(fin)
1283 fr_info_t *fin;
1284 {
1285 	grehdr_t *gre;
1286 
1287 	if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(grehdr_t)) == -1))
1288 		return;
1289 
1290 	frpr_short(fin, sizeof(grehdr_t));
1291 
1292 	if (fin->fin_off == 0) {
1293 		gre = fin->fin_dp;
1294 		if (GRE_REV(gre->gr_flags) == 1)
1295 			fin->fin_data[0] = gre->gr_call;
1296 	}
1297 }
1298 
1299 
1300 /* ------------------------------------------------------------------------ */
1301 /* Function:    frpr_ipv4hdr                                                */
1302 /* Returns:     void                                                        */
1303 /* Parameters:  fin(I) - pointer to packet information                      */
1304 /*                                                                          */
1305 /* IPv4 Only                                                                */
1306 /* Analyze the IPv4 header and set fields in the fr_info_t structure.       */
1307 /* Check all options present and flag their presence if any exist.          */
1308 /* ------------------------------------------------------------------------ */
1309 static INLINE void frpr_ipv4hdr(fin)
1310 fr_info_t *fin;
1311 {
1312 	u_short optmsk = 0, secmsk = 0, auth = 0;
1313 	int hlen, ol, mv, p, i;
1314 	const struct optlist *op;
1315 	u_char *s, opt;
1316 	u_short off;
1317 	fr_ip_t *fi;
1318 	ip_t *ip;
1319 
1320 	fi = &fin->fin_fi;
1321 	hlen = fin->fin_hlen;
1322 
1323 	ip = fin->fin_ip;
1324 	p = ip->ip_p;
1325 	fi->fi_p = p;
1326 	fi->fi_tos = ip->ip_tos;
1327 	fin->fin_id = ip->ip_id;
1328 	off = ip->ip_off;
1329 
1330 	/* Get both TTL and protocol */
1331 	fi->fi_p = ip->ip_p;
1332 	fi->fi_ttl = ip->ip_ttl;
1333 #if 0
1334 	(*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4));
1335 #endif
1336 
1337 	/* Zero out bits not used in IPv6 address */
1338 	fi->fi_src.i6[1] = 0;
1339 	fi->fi_src.i6[2] = 0;
1340 	fi->fi_src.i6[3] = 0;
1341 	fi->fi_dst.i6[1] = 0;
1342 	fi->fi_dst.i6[2] = 0;
1343 	fi->fi_dst.i6[3] = 0;
1344 
1345 	fi->fi_saddr = ip->ip_src.s_addr;
1346 	fi->fi_daddr = ip->ip_dst.s_addr;
1347 
1348 	/*
1349 	 * set packet attribute flags based on the offset and
1350 	 * calculate the byte offset that it represents.
1351 	 */
1352 	off &= IP_MF|IP_OFFMASK;
1353 	if (off != 0) {
1354 		int morefrag = off & IP_MF;
1355 
1356 		fi->fi_flx |= FI_FRAG;
1357 		if (morefrag)
1358 			fi->fi_flx |= FI_MOREFRAG;
1359 		off &= IP_OFFMASK;
1360 		if (off != 0) {
1361 			fin->fin_flx |= FI_FRAGBODY;
1362 			off <<= 3;
1363 			if ((off + fin->fin_dlen > 65535) ||
1364 			    (fin->fin_dlen == 0) ||
1365 			    ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) {
1366 				/*
1367 				 * The length of the packet, starting at its
1368 				 * offset cannot exceed 65535 (0xffff) as the
1369 				 * length of an IP packet is only 16 bits.
1370 				 *
1371 				 * Any fragment that isn't the last fragment
1372 				 * must have a length greater than 0 and it
1373 				 * must be an even multiple of 8.
1374 				 */
1375 				fi->fi_flx |= FI_BAD;
1376 			}
1377 		}
1378 	}
1379 	fin->fin_off = off;
1380 
1381 	/*
1382 	 * Call per-protocol setup and checking
1383 	 */
1384 	switch (p)
1385 	{
1386 	case IPPROTO_UDP :
1387 		frpr_udp(fin);
1388 		break;
1389 	case IPPROTO_TCP :
1390 		frpr_tcp(fin);
1391 		break;
1392 	case IPPROTO_ICMP :
1393 		frpr_icmp(fin);
1394 		break;
1395 	case IPPROTO_AH :
1396 		frpr_ah(fin);
1397 		break;
1398 	case IPPROTO_ESP :
1399 		frpr_esp(fin);
1400 		break;
1401 	case IPPROTO_GRE :
1402 		frpr_gre(fin);
1403 		break;
1404 	}
1405 
1406 	ip = fin->fin_ip;
1407 	if (ip == NULL)
1408 		return;
1409 
1410 	/*
1411 	 * If it is a standard IP header (no options), set the flag fields
1412 	 * which relate to options to 0.
1413 	 */
1414 	if (hlen == sizeof(*ip)) {
1415 		fi->fi_optmsk = 0;
1416 		fi->fi_secmsk = 0;
1417 		fi->fi_auth = 0;
1418 		return;
1419 	}
1420 
1421 	/*
1422 	 * So the IP header has some IP options attached.  Walk the entire
1423 	 * list of options present with this packet and set flags to indicate
1424 	 * which ones are here and which ones are not.  For the somewhat out
1425 	 * of date and obscure security classification options, set a flag to
1426 	 * represent which classification is present.
1427 	 */
1428 	fi->fi_flx |= FI_OPTIONS;
1429 
1430 	for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
1431 		opt = *s;
1432 		if (opt == '\0')
1433 			break;
1434 		else if (opt == IPOPT_NOP)
1435 			ol = 1;
1436 		else {
1437 			if (hlen < 2)
1438 				break;
1439 			ol = (int)*(s + 1);
1440 			if (ol < 2 || ol > hlen)
1441 				break;
1442 		}
1443 		for (i = 9, mv = 4; mv >= 0; ) {
1444 			op = ipopts + i;
1445 			if ((opt == (u_char)op->ol_val) && (ol > 4)) {
1446 				optmsk |= op->ol_bit;
1447 				if (opt == IPOPT_SECURITY) {
1448 					const struct optlist *sp;
1449 					u_char	sec;
1450 					int j, m;
1451 
1452 					sec = *(s + 2);	/* classification */
1453 					for (j = 3, m = 2; m >= 0; ) {
1454 						sp = secopt + j;
1455 						if (sec == sp->ol_val) {
1456 							secmsk |= sp->ol_bit;
1457 							auth = *(s + 3);
1458 							auth *= 256;
1459 							auth += *(s + 4);
1460 							break;
1461 						}
1462 						if (sec < sp->ol_val)
1463 							j -= m;
1464 						else
1465 							j += m;
1466 						m--;
1467 					}
1468 				}
1469 				break;
1470 			}
1471 			if (opt < op->ol_val)
1472 				i -= mv;
1473 			else
1474 				i += mv;
1475 			mv--;
1476 		}
1477 		hlen -= ol;
1478 		s += ol;
1479 	}
1480 
1481 	/*
1482 	 *
1483 	 */
1484 	if (auth && !(auth & 0x0100))
1485 		auth &= 0xff00;
1486 	fi->fi_optmsk = optmsk;
1487 	fi->fi_secmsk = secmsk;
1488 	fi->fi_auth = auth;
1489 }
1490 
1491 
1492 /* ------------------------------------------------------------------------ */
1493 /* Function:    fr_makefrip                                                 */
1494 /* Returns:     int - 1 == hdr checking error, 0 == OK                      */
1495 /* Parameters:  hlen(I) - length of IP packet header                        */
1496 /*              ip(I)   - pointer to the IP header                          */
1497 /*              fin(IO) - pointer to packet information                     */
1498 /*                                                                          */
1499 /* Compact the IP header into a structure which contains just the info.     */
1500 /* which is useful for comparing IP headers with and store this information */
1501 /* in the fr_info_t structure pointer to by fin.  At present, it is assumed */
1502 /* this function will be called with either an IPv4 or IPv6 packet.         */
1503 /* ------------------------------------------------------------------------ */
1504 int	fr_makefrip(hlen, ip, fin)
1505 int hlen;
1506 ip_t *ip;
1507 fr_info_t *fin;
1508 {
1509 	int v;
1510 
1511 	fin->fin_depth = 0;
1512 	fin->fin_hlen = (u_short)hlen;
1513 	fin->fin_ip = ip;
1514 	fin->fin_rule = 0xffffffff;
1515 	fin->fin_group[0] = -1;
1516 	fin->fin_group[1] = '\0';
1517 	fin->fin_dlen = fin->fin_plen - hlen;
1518 	fin->fin_dp = (char *)ip + hlen;
1519 
1520 	v = fin->fin_v;
1521 	if (v == 4)
1522 		frpr_ipv4hdr(fin);
1523 #ifdef	USE_INET6
1524 	else if (v == 6)
1525 		frpr_ipv6hdr(fin);
1526 #endif
1527 	if (fin->fin_ip == NULL)
1528 		return -1;
1529 	return 0;
1530 }
1531 
1532 
1533 /* ------------------------------------------------------------------------ */
1534 /* Function:    fr_portcheck                                                */
1535 /* Returns:     int - 1 == port matched, 0 == port match failed             */
1536 /* Parameters:  frp(I) - pointer to port check `expression'                 */
1537 /*              pop(I) - pointer to port number to evaluate                 */
1538 /*                                                                          */
1539 /* Perform a comparison of a port number against some other(s), using a     */
1540 /* structure with compare information stored in it.                         */
1541 /* ------------------------------------------------------------------------ */
1542 static INLINE int fr_portcheck(frp, pop)
1543 frpcmp_t *frp;
1544 u_short *pop;
1545 {
1546 	u_short tup, po;
1547 	int err = 1;
1548 
1549 	tup = *pop;
1550 	po = frp->frp_port;
1551 
1552 	/*
1553 	 * Do opposite test to that required and continue if that succeeds.
1554 	 */
1555 	switch (frp->frp_cmp)
1556 	{
1557 	case FR_EQUAL :
1558 		if (tup != po) /* EQUAL */
1559 			err = 0;
1560 		break;
1561 	case FR_NEQUAL :
1562 		if (tup == po) /* NOTEQUAL */
1563 			err = 0;
1564 		break;
1565 	case FR_LESST :
1566 		if (tup >= po) /* LESSTHAN */
1567 			err = 0;
1568 		break;
1569 	case FR_GREATERT :
1570 		if (tup <= po) /* GREATERTHAN */
1571 			err = 0;
1572 		break;
1573 	case FR_LESSTE :
1574 		if (tup > po) /* LT or EQ */
1575 			err = 0;
1576 		break;
1577 	case FR_GREATERTE :
1578 		if (tup < po) /* GT or EQ */
1579 			err = 0;
1580 		break;
1581 	case FR_OUTRANGE :
1582 		if (tup >= po && tup <= frp->frp_top) /* Out of range */
1583 			err = 0;
1584 		break;
1585 	case FR_INRANGE :
1586 		if (tup <= po || tup >= frp->frp_top) /* In range */
1587 			err = 0;
1588 		break;
1589 	case FR_INCRANGE :
1590 		if (tup < po || tup > frp->frp_top) /* Inclusive range */
1591 			err = 0;
1592 		break;
1593 	default :
1594 		break;
1595 	}
1596 	return err;
1597 }
1598 
1599 
1600 /* ------------------------------------------------------------------------ */
1601 /* Function:    fr_tcpudpchk                                                */
1602 /* Returns:     int - 1 == protocol matched, 0 == check failed              */
1603 /* Parameters:  fin(I) - pointer to packet information                      */
1604 /*              ft(I)  - pointer to structure with comparison data          */
1605 /*                                                                          */
1606 /* Compares the current pcket (assuming it is TCP/UDP) information with a   */
1607 /* structure containing information that we want to match against.          */
1608 /* ------------------------------------------------------------------------ */
1609 int fr_tcpudpchk(fin, ft)
1610 fr_info_t *fin;
1611 frtuc_t *ft;
1612 {
1613 	int err = 1;
1614 
1615 	/*
1616 	 * Both ports should *always* be in the first fragment.
1617 	 * So far, I cannot find any cases where they can not be.
1618 	 *
1619 	 * compare destination ports
1620 	 */
1621 	if (ft->ftu_dcmp)
1622 		err = fr_portcheck(&ft->ftu_dst, &fin->fin_dport);
1623 
1624 	/*
1625 	 * compare source ports
1626 	 */
1627 	if (err && ft->ftu_scmp)
1628 		err = fr_portcheck(&ft->ftu_src, &fin->fin_sport);
1629 
1630 	/*
1631 	 * If we don't have all the TCP/UDP header, then how can we
1632 	 * expect to do any sort of match on it ?  If we were looking for
1633 	 * TCP flags, then NO match.  If not, then match (which should
1634 	 * satisfy the "short" class too).
1635 	 */
1636 	if (err && (fin->fin_p == IPPROTO_TCP)) {
1637 		if (fin->fin_flx & FI_SHORT)
1638 			return !(ft->ftu_tcpf | ft->ftu_tcpfm);
1639 		/*
1640 		 * Match the flags ?  If not, abort this match.
1641 		 */
1642 		if (ft->ftu_tcpfm &&
1643 		    ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) {
1644 			FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf,
1645 				 ft->ftu_tcpfm, ft->ftu_tcpf));
1646 			err = 0;
1647 		}
1648 	}
1649 	return err;
1650 }
1651 
1652 
1653 /* ------------------------------------------------------------------------ */
1654 /* Function:    fr_ipfcheck                                                 */
1655 /* Returns:     int - 0 == match, 1 == no match                             */
1656 /* Parameters:  fin(I)     - pointer to packet information                  */
1657 /*              fr(I)      - pointer to filter rule                         */
1658 /*              portcmp(I) - flag indicating whether to attempt matching on */
1659 /*                           TCP/UDP port data.                             */
1660 /*                                                                          */
1661 /* Check to see if a packet matches an IPFilter rule.  Checks of addresses, */
1662 /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
1663 /* this function.                                                           */
1664 /* ------------------------------------------------------------------------ */
1665 static INLINE int fr_ipfcheck(fin, fr, portcmp)
1666 fr_info_t *fin;
1667 frentry_t *fr;
1668 int portcmp;
1669 {
1670 	u_32_t	*ld, *lm, *lip;
1671 	fripf_t *fri;
1672 	fr_ip_t *fi;
1673 	int i;
1674 	ipf_stack_t *ifs = fin->fin_ifs;
1675 
1676 	fi = &fin->fin_fi;
1677 	fri = fr->fr_ipf;
1678 	lip = (u_32_t *)fi;
1679 	lm = (u_32_t *)&fri->fri_mip;
1680 	ld = (u_32_t *)&fri->fri_ip;
1681 
1682 	/*
1683 	 * first 32 bits to check coversion:
1684 	 * IP version, TOS, TTL, protocol
1685 	 */
1686 	i = ((*lip & *lm) != *ld);
1687 	FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
1688 		   *lip, *lm, *ld));
1689 	if (i)
1690 		return 1;
1691 
1692 	/*
1693 	 * Next 32 bits is a constructed bitmask indicating which IP options
1694 	 * are present (if any) in this packet.
1695 	 */
1696 	lip++, lm++, ld++;
1697 	i |= ((*lip & *lm) != *ld);
1698 	FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
1699 		   *lip, *lm, *ld));
1700 	if (i)
1701 		return 1;
1702 
1703 	lip++, lm++, ld++;
1704 	/*
1705 	 * Unrolled loops (4 each, for 32 bits) for address checks.
1706 	 */
1707 	/*
1708 	 * Check the source address.
1709 	 */
1710 #ifdef	IPFILTER_LOOKUP
1711 	if (fr->fr_satype == FRI_LOOKUP) {
1712 		fin->fin_flx |= FI_DONTCACHE;
1713 		i = (*fr->fr_srcfunc)(fr->fr_srcptr, fi->fi_v, lip, fin, ifs);
1714 		if (i == -1)
1715 			return 1;
1716 		lip += 3;
1717 		lm += 3;
1718 		ld += 3;
1719 	} else {
1720 #endif
1721 		i = ((*lip & *lm) != *ld);
1722 		FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
1723 			   *lip, *lm, *ld));
1724 		if (fi->fi_v == 6) {
1725 			lip++, lm++, ld++;
1726 			i |= ((*lip & *lm) != *ld);
1727 			FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
1728 				   *lip, *lm, *ld));
1729 			lip++, lm++, ld++;
1730 			i |= ((*lip & *lm) != *ld);
1731 			FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
1732 				   *lip, *lm, *ld));
1733 			lip++, lm++, ld++;
1734 			i |= ((*lip & *lm) != *ld);
1735 			FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
1736 				   *lip, *lm, *ld));
1737 		} else {
1738 			lip += 3;
1739 			lm += 3;
1740 			ld += 3;
1741 		}
1742 #ifdef	IPFILTER_LOOKUP
1743 	}
1744 #endif
1745 	i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6;
1746 	if (i)
1747 		return 1;
1748 
1749 	/*
1750 	 * Check the destination address.
1751 	 */
1752 	lip++, lm++, ld++;
1753 #ifdef	IPFILTER_LOOKUP
1754 	if (fr->fr_datype == FRI_LOOKUP) {
1755 		fin->fin_flx |= FI_DONTCACHE;
1756 		i = (*fr->fr_dstfunc)(fr->fr_dstptr, fi->fi_v, lip, fin, ifs);
1757 		if (i == -1)
1758 			return 1;
1759 		lip += 3;
1760 		lm += 3;
1761 		ld += 3;
1762 	} else {
1763 #endif
1764 		i = ((*lip & *lm) != *ld);
1765 		FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
1766 			   *lip, *lm, *ld));
1767 		if (fi->fi_v == 6) {
1768 			lip++, lm++, ld++;
1769 			i |= ((*lip & *lm) != *ld);
1770 			FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
1771 				   *lip, *lm, *ld));
1772 			lip++, lm++, ld++;
1773 			i |= ((*lip & *lm) != *ld);
1774 			FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
1775 				   *lip, *lm, *ld));
1776 			lip++, lm++, ld++;
1777 			i |= ((*lip & *lm) != *ld);
1778 			FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
1779 				   *lip, *lm, *ld));
1780 		} else {
1781 			lip += 3;
1782 			lm += 3;
1783 			ld += 3;
1784 		}
1785 #ifdef	IPFILTER_LOOKUP
1786 	}
1787 #endif
1788 	i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7;
1789 	if (i)
1790 		return 1;
1791 	/*
1792 	 * IP addresses matched.  The next 32bits contains:
1793 	 * mast of old IP header security & authentication bits.
1794 	 */
1795 	lip++, lm++, ld++;
1796 	i |= ((*lip & *lm) != *ld);
1797 	FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
1798 		   *lip, *lm, *ld));
1799 
1800 	/*
1801 	 * Next we have 32 bits of packet flags.
1802 	 */
1803 	lip++, lm++, ld++;
1804 	i |= ((*lip & *lm) != *ld);
1805 	FR_DEBUG(("5. %#08x & %#08x != %#08x\n",
1806 		   *lip, *lm, *ld));
1807 
1808 	if (i == 0) {
1809 		/*
1810 		 * If a fragment, then only the first has what we're
1811 		 * looking for here...
1812 		 */
1813 		if (portcmp) {
1814 			if (!fr_tcpudpchk(fin, &fr->fr_tuc))
1815 				i = 1;
1816 		} else {
1817 			if (fr->fr_dcmp || fr->fr_scmp ||
1818 			    fr->fr_tcpf || fr->fr_tcpfm)
1819 				i = 1;
1820 			if (fr->fr_icmpm || fr->fr_icmp) {
1821 				if (((fi->fi_p != IPPROTO_ICMP) &&
1822 				     (fi->fi_p != IPPROTO_ICMPV6)) ||
1823 				    fin->fin_off || (fin->fin_dlen < 2))
1824 					i = 1;
1825 				else if ((fin->fin_data[0] & fr->fr_icmpm) !=
1826 					 fr->fr_icmp) {
1827 					FR_DEBUG(("i. %#x & %#x != %#x\n",
1828 						 fin->fin_data[0],
1829 						 fr->fr_icmpm, fr->fr_icmp));
1830 					i = 1;
1831 				}
1832 			}
1833 		}
1834 	}
1835 	return i;
1836 }
1837 
1838 
1839 /* ------------------------------------------------------------------------ */
1840 /* Function:    fr_scanlist                                                 */
1841 /* Returns:     int - result flags of scanning filter list                  */
1842 /* Parameters:  fin(I) - pointer to packet information                      */
1843 /*              pass(I) - default result to return for filtering            */
1844 /*                                                                          */
1845 /* Check the input/output list of rules for a match to the current packet.  */
1846 /* If a match is found, the value of fr_flags from the rule becomes the     */
1847 /* return value and fin->fin_fr points to the matched rule.                 */
1848 /*                                                                          */
1849 /* This function may be called recusively upto 16 times (limit inbuilt.)    */
1850 /* When unwinding, it should finish up with fin_depth as 0.                 */
1851 /*                                                                          */
1852 /* Could be per interface, but this gets real nasty when you don't have,    */
1853 /* or can't easily change, the kernel source code to .                      */
1854 /* ------------------------------------------------------------------------ */
1855 int fr_scanlist(fin, pass)
1856 fr_info_t *fin;
1857 u_32_t pass;
1858 {
1859 	int rulen, portcmp, off, logged, skip;
1860 	struct frentry *fr, *fnext;
1861 	u_32_t passt, passo;
1862 	ipf_stack_t *ifs = fin->fin_ifs;
1863 
1864 	/*
1865 	 * Do not allow nesting deeper than 16 levels.
1866 	 */
1867 	if (fin->fin_depth >= 16)
1868 		return pass;
1869 
1870 	fr = fin->fin_fr;
1871 
1872 	/*
1873 	 * If there are no rules in this list, return now.
1874 	 */
1875 	if (fr == NULL)
1876 		return pass;
1877 
1878 	skip = 0;
1879 	logged = 0;
1880 	portcmp = 0;
1881 	fin->fin_depth++;
1882 	fin->fin_fr = NULL;
1883 	off = fin->fin_off;
1884 
1885 	if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
1886 		portcmp = 1;
1887 
1888 	for (rulen = 0; fr; fr = fnext, rulen++) {
1889 		fnext = fr->fr_next;
1890 		if (skip != 0) {
1891 			FR_VERBOSE(("%d (%#x)\n", skip, fr->fr_flags));
1892 			skip--;
1893 			continue;
1894 		}
1895 
1896 		/*
1897 		 * In all checks below, a null (zero) value in the
1898 		 * filter struture is taken to mean a wildcard.
1899 		 *
1900 		 * check that we are working for the right interface
1901 		 */
1902 #ifdef	_KERNEL
1903 		if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
1904 			continue;
1905 #else
1906 		if (opts & (OPT_VERBOSE|OPT_DEBUG))
1907 			printf("\n");
1908 		FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' :
1909 				  FR_ISPASS(pass) ? 'p' :
1910 				  FR_ISACCOUNT(pass) ? 'A' :
1911 				  FR_ISAUTH(pass) ? 'a' :
1912 				  (pass & FR_NOMATCH) ? 'n' :'b'));
1913 		if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
1914 			continue;
1915 		FR_VERBOSE((":i"));
1916 #endif
1917 
1918 		switch (fr->fr_type)
1919 		{
1920 		case FR_T_IPF :
1921 		case FR_T_IPF|FR_T_BUILTIN :
1922 			if (fr_ipfcheck(fin, fr, portcmp))
1923 				continue;
1924 			break;
1925 #if defined(IPFILTER_BPF)
1926 		case FR_T_BPFOPC :
1927 		case FR_T_BPFOPC|FR_T_BUILTIN :
1928 		    {
1929 			u_char *mc;
1930 
1931 			if (*fin->fin_mp == NULL)
1932 				continue;
1933 			if (fin->fin_v != fr->fr_v)
1934 				continue;
1935 			mc = (u_char *)fin->fin_m;
1936 			if (!bpf_filter(fr->fr_data, mc, fin->fin_plen, 0))
1937 				continue;
1938 			break;
1939 		    }
1940 #endif
1941 		case FR_T_CALLFUNC|FR_T_BUILTIN :
1942 		    {
1943 			frentry_t *f;
1944 
1945 			f = (*fr->fr_func)(fin, &pass);
1946 			if (f != NULL)
1947 				fr = f;
1948 			else
1949 				continue;
1950 			break;
1951 		    }
1952 		default :
1953 			break;
1954 		}
1955 
1956 		if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) {
1957 			if (fin->fin_nattag == NULL)
1958 				continue;
1959 			if (fr_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0)
1960 				continue;
1961 		}
1962 		FR_VERBOSE(("=%s.%d *", fr->fr_group, rulen));
1963 
1964 		passt = fr->fr_flags;
1965 
1966 		/*
1967 		 * Allowing a rule with the "keep state" flag set to match
1968 		 * packets that have been tagged "out of window" by the TCP
1969 		 * state tracking is foolish as the attempt to add a new
1970 		 * state entry to the table will fail.
1971 		 */
1972 		if ((passt & FR_KEEPSTATE) && (fin->fin_flx & FI_OOW))
1973 			continue;
1974 
1975 		/*
1976 		 * If the rule is a "call now" rule, then call the function
1977 		 * in the rule, if it exists and use the results from that.
1978 		 * If the function pointer is bad, just make like we ignore
1979 		 * it, except for increasing the hit counter.
1980 		 */
1981 		IPF_BUMP(fr->fr_hits);
1982 		fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
1983 		if ((passt & FR_CALLNOW) != 0) {
1984 			if ((fr->fr_func != NULL) &&
1985 			    (fr->fr_func != (ipfunc_t)-1)) {
1986 				frentry_t *frs;
1987 
1988 				frs = fin->fin_fr;
1989 				fin->fin_fr = fr;
1990 				fr = (*fr->fr_func)(fin, &passt);
1991 				if (fr == NULL) {
1992 					fin->fin_fr = frs;
1993 					continue;
1994 				}
1995 				passt = fr->fr_flags;
1996 				fin->fin_fr = fr;
1997 			}
1998 		} else {
1999 			fin->fin_fr = fr;
2000 		}
2001 
2002 #ifdef  IPFILTER_LOG
2003 		/*
2004 		 * Just log this packet...
2005 		 */
2006 		if ((passt & FR_LOGMASK) == FR_LOG) {
2007 			if (ipflog(fin, passt) == -1) {
2008 				if (passt & FR_LOGORBLOCK) {
2009 					passt &= ~FR_CMDMASK;
2010 					passt |= FR_BLOCK|FR_QUICK;
2011 				}
2012 				IPF_BUMP(ifs->ifs_frstats[fin->fin_out].fr_skip);
2013 			}
2014 			IPF_BUMP(ifs->ifs_frstats[fin->fin_out].fr_pkl);
2015 			logged = 1;
2016 		}
2017 #endif /* IPFILTER_LOG */
2018 		passo = pass;
2019 		if (FR_ISSKIP(passt))
2020 			skip = fr->fr_arg;
2021 		else if ((passt & FR_LOGMASK) != FR_LOG)
2022 			pass = passt;
2023 		if (passt & (FR_RETICMP|FR_FAKEICMP))
2024 			fin->fin_icode = fr->fr_icode;
2025 		FR_DEBUG(("pass %#x\n", pass));
2026 		fin->fin_rule = rulen;
2027 		(void) strncpy(fin->fin_group, fr->fr_group, FR_GROUPLEN);
2028 		if (fr->fr_grp != NULL) {
2029 			fin->fin_fr = *fr->fr_grp;
2030 			pass = fr_scanlist(fin, pass);
2031 			if (fin->fin_fr == NULL) {
2032 				fin->fin_rule = rulen;
2033 				(void) strncpy(fin->fin_group, fr->fr_group,
2034 					       FR_GROUPLEN);
2035 				fin->fin_fr = fr;
2036 			}
2037 			if (fin->fin_flx & FI_DONTCACHE)
2038 				logged = 1;
2039 		}
2040 
2041 		if (pass & FR_QUICK) {
2042 			/*
2043 			 * Finally, if we've asked to track state for this
2044 			 * packet, set it up.  Add state for "quick" rules
2045 			 * here so that if the action fails we can consider
2046 			 * the rule to "not match" and keep on processing
2047 			 * filter rules.
2048 			 */
2049 			if ((pass & FR_KEEPSTATE) &&
2050 			    !(fin->fin_flx & FI_STATE)) {
2051 				int out = fin->fin_out;
2052 
2053 				if (fr_addstate(fin, NULL, 0) != NULL) {
2054 					IPF_BUMP(ifs->ifs_frstats[out].fr_ads);
2055 				} else {
2056 					IPF_BUMP(ifs->ifs_frstats[out].fr_bads);
2057 					pass = passo;
2058 					continue;
2059 				}
2060 			}
2061 			break;
2062 		}
2063 	}
2064 	if (logged)
2065 		fin->fin_flx |= FI_DONTCACHE;
2066 	fin->fin_depth--;
2067 	return pass;
2068 }
2069 
2070 
2071 /* ------------------------------------------------------------------------ */
2072 /* Function:    fr_acctpkt                                                  */
2073 /* Returns:     frentry_t* - always returns NULL                            */
2074 /* Parameters:  fin(I) - pointer to packet information                      */
2075 /*              passp(IO) - pointer to current/new filter decision (unused) */
2076 /*                                                                          */
2077 /* Checks a packet against accounting rules, if there are any for the given */
2078 /* IP protocol version.                                                     */
2079 /*                                                                          */
2080 /* N.B.: this function returns NULL to match the prototype used by other    */
2081 /* functions called from the IPFilter "mainline" in fr_check().             */
2082 /* ------------------------------------------------------------------------ */
2083 frentry_t *fr_acctpkt(fin, passp)
2084 fr_info_t *fin;
2085 u_32_t *passp;
2086 {
2087 	char group[FR_GROUPLEN];
2088 	frentry_t *fr, *frsave;
2089 	u_32_t pass, rulen;
2090 	ipf_stack_t *ifs = fin->fin_ifs;
2091 
2092 	passp = passp;
2093 #ifdef	USE_INET6
2094 	if (fin->fin_v == 6)
2095 		fr = ifs->ifs_ipacct6[fin->fin_out][ifs->ifs_fr_active];
2096 	else
2097 #endif
2098 		fr = ifs->ifs_ipacct[fin->fin_out][ifs->ifs_fr_active];
2099 
2100 	if (fr != NULL) {
2101 		frsave = fin->fin_fr;
2102 		bcopy(fin->fin_group, group, FR_GROUPLEN);
2103 		rulen = fin->fin_rule;
2104 		fin->fin_fr = fr;
2105 		pass = fr_scanlist(fin, FR_NOMATCH);
2106 		if (FR_ISACCOUNT(pass)) {
2107 			IPF_BUMP(ifs->ifs_frstats[0].fr_acct);
2108 		}
2109 		fin->fin_fr = frsave;
2110 		bcopy(group, fin->fin_group, FR_GROUPLEN);
2111 		fin->fin_rule = rulen;
2112 	}
2113 	return NULL;
2114 }
2115 
2116 
2117 /* ------------------------------------------------------------------------ */
2118 /* Function:    fr_firewall                                                 */
2119 /* Returns:     frentry_t* - returns pointer to matched rule, if no matches */
2120 /*                           were found, returns NULL.                      */
2121 /* Parameters:  fin(I) - pointer to packet information                      */
2122 /*              passp(IO) - pointer to current/new filter decision (unused) */
2123 /*                                                                          */
2124 /* Applies an appropriate set of firewall rules to the packet, to see if    */
2125 /* there are any matches.  The first check is to see if a match can be seen */
2126 /* in the cache.  If not, then search an appropriate list of rules.  Once a */
2127 /* matching rule is found, take any appropriate actions as defined by the   */
2128 /* rule - except logging.                                                   */
2129 /* ------------------------------------------------------------------------ */
2130 static frentry_t *fr_firewall(fin, passp)
2131 fr_info_t *fin;
2132 u_32_t *passp;
2133 {
2134 	frentry_t *fr;
2135 	fr_info_t *fc;
2136 	u_32_t pass;
2137 	int out;
2138 	ipf_stack_t *ifs = fin->fin_ifs;
2139 
2140 	out = fin->fin_out;
2141 	pass = *passp;
2142 
2143 #ifdef	USE_INET6
2144 	if (fin->fin_v == 6)
2145 		fin->fin_fr = ifs->ifs_ipfilter6[out][ifs->ifs_fr_active];
2146 	else
2147 #endif
2148 		fin->fin_fr = ifs->ifs_ipfilter[out][ifs->ifs_fr_active];
2149 
2150 	/*
2151 	 * If there are no rules loaded skip all checks and return.
2152 	 */
2153 	if (fin->fin_fr == NULL) {
2154 
2155 		if ((pass & FR_NOMATCH)) {
2156 			IPF_BUMP(ifs->ifs_frstats[out].fr_nom);
2157 		}
2158 
2159 		return (NULL);
2160 	}
2161 
2162 	fc = &ifs->ifs_frcache[out][CACHE_HASH(fin)];
2163 	READ_ENTER(&ifs->ifs_ipf_frcache);
2164 	if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) {
2165 		/*
2166 		 * copy cached data so we can unlock the mutexes earlier.
2167 		 */
2168 		bcopy((char *)fc, (char *)fin, FI_COPYSIZE);
2169 		RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
2170 		IPF_BUMP(ifs->ifs_frstats[out].fr_chit);
2171 
2172 		if ((fr = fin->fin_fr) != NULL) {
2173 			IPF_BUMP(fr->fr_hits);
2174 			fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
2175 			pass = fr->fr_flags;
2176 		}
2177 	} else {
2178 		RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
2179 
2180 		pass = fr_scanlist(fin, ifs->ifs_fr_pass);
2181 
2182 		if (((pass & FR_KEEPSTATE) == 0) &&
2183 		    ((fin->fin_flx & FI_DONTCACHE) == 0)) {
2184 			WRITE_ENTER(&ifs->ifs_ipf_frcache);
2185 			bcopy((char *)fin, (char *)fc, FI_COPYSIZE);
2186 			RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
2187 		}
2188 
2189 		fr = fin->fin_fr;
2190 	}
2191 
2192 	if ((pass & FR_NOMATCH)) {
2193 		IPF_BUMP(ifs->ifs_frstats[out].fr_nom);
2194 	}
2195 
2196 	/*
2197 	 * Apply packets per second rate-limiting to a rule as required.
2198 	 */
2199 	if ((fr != NULL) && (fr->fr_pps != 0) &&
2200 	    !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) {
2201 		pass &= ~(FR_CMDMASK|FR_DUP|FR_RETICMP|FR_RETRST);
2202 		pass |= FR_BLOCK;
2203 		IPF_BUMP(ifs->ifs_frstats[out].fr_ppshit);
2204 	}
2205 
2206 	/*
2207 	 * If we fail to add a packet to the authorization queue, then we
2208 	 * drop the packet later.  However, if it was added then pretend
2209 	 * we've dropped it already.
2210 	 */
2211 	if (FR_ISAUTH(pass)) {
2212 		if (fr_newauth(fin->fin_m, fin) != 0) {
2213 #ifdef	_KERNEL
2214 			fin->fin_m = *fin->fin_mp = NULL;
2215 #else
2216 			;
2217 #endif
2218 			fin->fin_error = 0;
2219 		} else
2220 			fin->fin_error = ENOSPC;
2221 	}
2222 
2223 	if ((fr != NULL) && (fr->fr_func != NULL) &&
2224 	    (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW))
2225 		(void) (*fr->fr_func)(fin, &pass);
2226 
2227 	/*
2228 	 * If a rule is a pre-auth rule, check again in the list of rules
2229 	 * loaded for authenticated use.  It does not particulary matter
2230 	 * if this search fails because a "preauth" result, from a rule,
2231 	 * is treated as "not a pass", hence the packet is blocked.
2232 	 */
2233 	if (FR_ISPREAUTH(pass)) {
2234 		if ((fin->fin_fr = ifs->ifs_ipauth) != NULL)
2235 			pass = fr_scanlist(fin, ifs->ifs_fr_pass);
2236 	}
2237 
2238 	/*
2239 	 * If the rule has "keep frag" and the packet is actually a fragment,
2240 	 * then create a fragment state entry.
2241 	 */
2242 	if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) {
2243 		if (fin->fin_flx & FI_FRAG) {
2244 			if (fr_newfrag(fin, pass) == -1) {
2245 				IPF_BUMP(ifs->ifs_frstats[out].fr_bnfr);
2246 			} else {
2247 				IPF_BUMP(ifs->ifs_frstats[out].fr_nfr);
2248 			}
2249 		} else {
2250 			IPF_BUMP(ifs->ifs_frstats[out].fr_cfr);
2251 		}
2252 	}
2253 
2254 	/*
2255 	 * Finally, if we've asked to track state for this packet, set it up.
2256 	 */
2257 	if ((pass & FR_KEEPSTATE) && !(fin->fin_flx & FI_STATE)) {
2258 		if (fr_addstate(fin, NULL, 0) != NULL) {
2259 			IPF_BUMP(ifs->ifs_frstats[out].fr_ads);
2260 		} else {
2261 			IPF_BUMP(ifs->ifs_frstats[out].fr_bads);
2262 			if (FR_ISPASS(pass)) {
2263 				pass &= ~FR_CMDMASK;
2264 				pass |= FR_BLOCK;
2265 			}
2266 		}
2267 	}
2268 
2269 	fr = fin->fin_fr;
2270 
2271 	if (passp != NULL)
2272 		*passp = pass;
2273 
2274 	return fr;
2275 }
2276 
2277 /* ------------------------------------------------------------------------ */
2278 /* Function:    fr_check                                                    */
2279 /* Returns:     int -  0 == packet allowed through,                         */
2280 /*              User space:                                                 */
2281 /*                    -1 == packet blocked                                  */
2282 /*                     1 == packet not matched                              */
2283 /*                    -2 == requires authentication                         */
2284 /*              Kernel:                                                     */
2285 /*                   > 0 == filter error # for packet                       */
2286 /* Parameters: ip(I)   - pointer to start of IPv4/6 packet                  */
2287 /*             hlen(I) - length of header                                   */
2288 /*             ifp(I)  - pointer to interface this packet is on             */
2289 /*             out(I)  - 0 == packet going in, 1 == packet going out        */
2290 /*             mp(IO)  - pointer to caller's buffer pointer that holds this */
2291 /*                       IP packet.                                         */
2292 /* Solaris & HP-UX ONLY :                                                   */
2293 /*             qpi(I)  - pointer to STREAMS queue information for this      */
2294 /*                       interface & direction.                             */
2295 /*                                                                          */
2296 /* fr_check() is the master function for all IPFilter packet processing.    */
2297 /* It orchestrates: Network Address Translation (NAT), checking for packet  */
2298 /* authorisation (or pre-authorisation), presence of related state info.,   */
2299 /* generating log entries, IP packet accounting, routing of packets as      */
2300 /* directed by firewall rules and of course whether or not to allow the     */
2301 /* packet to be further processed by the kernel.                            */
2302 /*                                                                          */
2303 /* For packets blocked, the contents of "mp" will be NULL'd and the buffer  */
2304 /* freed.  Packets passed may be returned with the pointer pointed to by    */
2305 /* by "mp" changed to a new buffer.                                         */
2306 /* ------------------------------------------------------------------------ */
2307 int fr_check(ip, hlen, ifp, out
2308 #if defined(_KERNEL) && defined(MENTAT)
2309 , qif, mp, ifs)
2310 void *qif;
2311 #else
2312 , mp, ifs)
2313 #endif
2314 mb_t **mp;
2315 ip_t *ip;
2316 int hlen;
2317 void *ifp;
2318 int out;
2319 ipf_stack_t *ifs;
2320 {
2321 	/*
2322 	 * The above really sucks, but short of writing a diff
2323 	 */
2324 	fr_info_t frinfo;
2325 	fr_info_t *fin = &frinfo;
2326 	u_32_t pass;
2327 	frentry_t *fr = NULL;
2328 	int v = IP_V(ip);
2329 	mb_t *mc = NULL;
2330 	mb_t *m;
2331 #ifdef USE_INET6
2332 	ip6_t *ip6;
2333 #endif
2334 #ifdef	_KERNEL
2335 # ifdef MENTAT
2336 	qpktinfo_t *qpi = qif;
2337 #endif
2338 #endif
2339 
2340 	SPL_INT(s);
2341 	pass = ifs->ifs_fr_pass;
2342 
2343 	/*
2344 	 * The first part of fr_check() deals with making sure that what goes
2345 	 * into the filtering engine makes some sense.  Information about the
2346 	 * the packet is distilled, collected into a fr_info_t structure and
2347 	 * the an attempt to ensure the buffer the packet is in is big enough
2348 	 * to hold all the required packet headers.
2349 	 */
2350 #ifdef	_KERNEL
2351 # ifdef MENTAT
2352 	if (!OK_32PTR(ip))
2353 		return 2;
2354 # endif
2355 
2356 
2357 	if (ifs->ifs_fr_running <= 0) {
2358 		return 0;
2359 	}
2360 
2361 	bzero((char *)fin, sizeof(*fin));
2362 
2363 # ifdef MENTAT
2364 	fin->fin_flx = qpi->qpi_flags & (FI_NOCKSUM|FI_MBCAST|FI_MULTICAST|
2365 					 FI_BROADCAST);
2366 	m = qpi->qpi_m;
2367 	fin->fin_qfm = m;
2368 	fin->fin_qpi = qpi;
2369 # else /* MENTAT */
2370 
2371 	m = *mp;
2372 
2373 #  if defined(M_MCAST)
2374 	if ((m->m_flags & M_MCAST) != 0)
2375 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2376 #  endif
2377 #  if defined(M_MLOOP)
2378 	if ((m->m_flags & M_MLOOP) != 0)
2379 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2380 #  endif
2381 #  if defined(M_BCAST)
2382 	if ((m->m_flags & M_BCAST) != 0)
2383 		fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
2384 #  endif
2385 #  ifdef M_CANFASTFWD
2386 	/*
2387 	 * XXX For now, IP Filter and fast-forwarding of cached flows
2388 	 * XXX are mutually exclusive.  Eventually, IP Filter should
2389 	 * XXX get a "can-fast-forward" filter rule.
2390 	 */
2391 	m->m_flags &= ~M_CANFASTFWD;
2392 #  endif /* M_CANFASTFWD */
2393 #  ifdef CSUM_DELAY_DATA
2394 	/*
2395 	 * disable delayed checksums.
2396 	 */
2397 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
2398 		in_delayed_cksum(m);
2399 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
2400 	}
2401 #  endif /* CSUM_DELAY_DATA */
2402 # endif /* MENTAT */
2403 #else
2404 
2405 	bzero((char *)fin, sizeof(*fin));
2406 	m = *mp;
2407 #endif /* _KERNEL */
2408 
2409 	fin->fin_v = v;
2410 	fin->fin_m = m;
2411 	fin->fin_ip = ip;
2412 	fin->fin_mp = mp;
2413 	fin->fin_out = out;
2414 	fin->fin_ifp = ifp;
2415 	fin->fin_error = ENETUNREACH;
2416 	fin->fin_hlen = (u_short)hlen;
2417 	fin->fin_dp = (char *)ip + hlen;
2418 	fin->fin_ipoff = (char *)ip - MTOD(m, char *);
2419 	fin->fin_ifs = ifs;
2420 
2421 	SPL_NET(s);
2422 
2423 #ifdef	USE_INET6
2424 	if (v == 6) {
2425 		IPF_BUMP(ifs->ifs_frstats[out].fr_ipv6);
2426 		/*
2427 		 * Jumbo grams are quite likely too big for internal buffer
2428 		 * structures to handle comfortably, for now, so just drop
2429 		 * them.
2430 		 */
2431 		ip6 = (ip6_t *)ip;
2432 		fin->fin_plen = ntohs(ip6->ip6_plen);
2433 		if (fin->fin_plen == 0) {
2434 			READ_ENTER(&ifs->ifs_ipf_mutex);
2435 			pass = FR_BLOCK|FR_NOMATCH;
2436 			goto filtered;
2437 		}
2438 		fin->fin_plen += sizeof(ip6_t);
2439 	} else
2440 #endif
2441 	{
2442 #if (OpenBSD >= 200311) && defined(_KERNEL)
2443 		ip->ip_len = ntohs(ip->ip_len);
2444 		ip->ip_off = ntohs(ip->ip_off);
2445 #endif
2446 		fin->fin_plen = ip->ip_len;
2447 	}
2448 
2449 	if (fr_makefrip(hlen, ip, fin) == -1) {
2450 		READ_ENTER(&ifs->ifs_ipf_mutex);
2451 		pass = FR_BLOCK;
2452 		goto filtered;
2453 	}
2454 
2455 	/*
2456 	 * For at least IPv6 packets, if a m_pullup() fails then this pointer
2457 	 * becomes NULL and so we have no packet to free.
2458 	 */
2459 	if (*fin->fin_mp == NULL)
2460 		goto finished;
2461 
2462 	if (!out) {
2463 		if (v == 4) {
2464 #ifdef _KERNEL
2465 			if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) {
2466 				IPF_BUMP(ifs->ifs_frstats[0].fr_badsrc);
2467 				fin->fin_flx |= FI_BADSRC;
2468 			}
2469 #endif
2470 			if (fin->fin_ip->ip_ttl < ifs->ifs_fr_minttl) {
2471 				IPF_BUMP(ifs->ifs_frstats[0].fr_badttl);
2472 				fin->fin_flx |= FI_LOWTTL;
2473 			}
2474 		}
2475 #ifdef USE_INET6
2476 		else  if (v == 6) {
2477 			ip6 = (ip6_t *)ip;
2478 #ifdef _KERNEL
2479 			if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) {
2480 				IPF_BUMP(ifs->ifs_frstats[0].fr_badsrc);
2481 				fin->fin_flx |= FI_BADSRC;
2482 			}
2483 #endif
2484 			if (ip6->ip6_hlim < ifs->ifs_fr_minttl) {
2485 				IPF_BUMP(ifs->ifs_frstats[0].fr_badttl);
2486 				fin->fin_flx |= FI_LOWTTL;
2487 			}
2488 		}
2489 #endif
2490 	}
2491 
2492 	if (fin->fin_flx & FI_SHORT) {
2493 		IPF_BUMP(ifs->ifs_frstats[out].fr_short);
2494 	}
2495 
2496 	READ_ENTER(&ifs->ifs_ipf_mutex);
2497 
2498 	/*
2499 	 * Check auth now.  This, combined with the check below to see if apass
2500 	 * is 0 is to ensure that we don't count the packet twice, which can
2501 	 * otherwise occur when we reprocess it.  As it is, we only count it
2502 	 * after it has no auth. table matchup.  This also stops NAT from
2503 	 * occuring until after the packet has been auth'd.
2504 	 */
2505 	fr = fr_checkauth(fin, &pass);
2506 	if (!out) {
2507 		switch (fin->fin_v)
2508 		{
2509 		case 4 :
2510 			if (fr_checknatin(fin, &pass) == -1) {
2511 				RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2512 				goto finished;
2513 			}
2514 			break;
2515 #ifdef	USE_INET6
2516 		case 6 :
2517 			if (fr_checknat6in(fin, &pass) == -1) {
2518 				RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2519 				goto finished;
2520 			}
2521 			break;
2522 #endif
2523 		default :
2524 			break;
2525 		}
2526 	}
2527 	if (!out)
2528 		(void) fr_acctpkt(fin, NULL);
2529 
2530 	if (fr == NULL)
2531 		if ((fin->fin_flx & (FI_FRAG|FI_BAD)) == FI_FRAG)
2532 			fr = fr_knownfrag(fin, &pass);
2533 	if (fr == NULL)
2534 		fr = fr_checkstate(fin, &pass);
2535 
2536 	if ((pass & FR_NOMATCH) || (fr == NULL))
2537 		fr = fr_firewall(fin, &pass);
2538 
2539 	fin->fin_fr = fr;
2540 
2541 	/*
2542 	 * Only count/translate packets which will be passed on, out the
2543 	 * interface.
2544 	 */
2545 	if (out && FR_ISPASS(pass)) {
2546 		(void) fr_acctpkt(fin, NULL);
2547 
2548 		switch (fin->fin_v)
2549 		{
2550 		case 4 :
2551 			if (fr_checknatout(fin, &pass) == -1) {
2552 				RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2553 				goto finished;
2554 			}
2555 			break;
2556 #ifdef	USE_INET6
2557 		case 6 :
2558 			if (fr_checknat6out(fin, &pass) == -1) {
2559 				RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2560 				goto finished;
2561 			}
2562 			break;
2563 #endif
2564 		default :
2565 			break;
2566 		}
2567 
2568 		if ((ifs->ifs_fr_update_ipid != 0) && (v == 4)) {
2569 			if (fr_updateipid(fin) == -1) {
2570 				IPF_BUMP(ifs->ifs_frstats[1].fr_ipud);
2571 				pass &= ~FR_CMDMASK;
2572 				pass |= FR_BLOCK;
2573 			} else {
2574 				IPF_BUMP(ifs->ifs_frstats[0].fr_ipud);
2575 			}
2576 		}
2577 	}
2578 
2579 #ifdef	IPFILTER_LOG
2580 	if ((ifs->ifs_fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
2581 		(void) fr_dolog(fin, &pass);
2582 	}
2583 #endif
2584 
2585 	/*
2586 	 * The FI_STATE flag is cleared here so that calling fr_checkstate
2587 	 * will work when called from inside of fr_fastroute.  Although
2588 	 * there is a similar flag, FI_NATED, for NAT, it does have the same
2589 	 * impact on code execution.
2590 	 */
2591 	fin->fin_flx &= ~FI_STATE;
2592 
2593 	/*
2594 	 * Only allow FR_DUP to work if a rule matched - it makes no sense to
2595 	 * set FR_DUP as a "default" as there are no instructions about where
2596 	 * to send the packet.  Use fin_m here because it may have changed
2597 	 * (without an update of 'm') in prior processing.
2598 	 */
2599 	if ((fr != NULL) && (pass & FR_DUP)) {
2600 		mc = M_DUPLICATE(fin->fin_m);
2601 #ifdef _KERNEL
2602 		mc->b_rptr += fin->fin_ipoff;
2603 #endif
2604 	}
2605 
2606 	if (pass & (FR_RETRST|FR_RETICMP)) {
2607 		/*
2608 		 * Should we return an ICMP packet to indicate error
2609 		 * status passing through the packet filter ?
2610 		 * WARNING: ICMP error packets AND TCP RST packets should
2611 		 * ONLY be sent in repsonse to incoming packets.  Sending them
2612 		 * in response to outbound packets can result in a panic on
2613 		 * some operating systems.
2614 		 */
2615 		if (!out) {
2616 			if (pass & FR_RETICMP) {
2617 				int dst;
2618 
2619 				if ((pass & FR_RETMASK) == FR_FAKEICMP)
2620 					dst = 1;
2621 				else
2622 					dst = 0;
2623 #if defined(_KERNEL) && (SOLARIS2 >= 10)
2624 				/*
2625 				 * Assume it's possible to enter insane rule:
2626 				 * 	pass return-icmp in proto udp ...
2627 				 * then we have no other option than to forward
2628 				 * packet on loopback and give up any attempt
2629 				 * to create a fake response.
2630 				 */
2631 				if (IPF_IS_LOOPBACK(qpi->qpi_flags) &&
2632 				    FR_ISBLOCK(pass)) {
2633 
2634 					if (fr_make_icmp(fin) == 0) {
2635 						IPF_BUMP(
2636 						ifs->ifs_frstats[out].fr_ret);
2637 					}
2638 					/*
2639 					 * we drop packet silently in case we
2640 					 * failed assemble fake response for it
2641 					 */
2642 					else if (*mp != NULL) {
2643 						FREE_MB_T(*mp);
2644 						m = *mp = NULL;
2645 					}
2646 
2647 					IPF_BUMP(
2648 					    ifs->ifs_frstats[out].fr_block);
2649 					RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2650 
2651 					return (0);
2652 				}
2653 #endif	/* _KERNEL && SOLARIS2 >= 10 */
2654 
2655 				(void) fr_send_icmp_err(ICMP_UNREACH, fin, dst);
2656 				IPF_BUMP(ifs->ifs_frstats[out].fr_ret);
2657 
2658 			} else if (((pass & FR_RETMASK) == FR_RETRST) &&
2659 				   !(fin->fin_flx & FI_SHORT)) {
2660 
2661 #if defined(_KERNEL) && (SOLARIS2 >= 10)
2662 				/*
2663 				 * Assume it's possible to enter insane rule:
2664 				 * 	pass return-rst in proto tcp ...
2665 				 * then we have no other option than to forward
2666 				 * packet on loopback and give up any attempt
2667 				 * to create a fake response.
2668 				 */
2669 				if (IPF_IS_LOOPBACK(qpi->qpi_flags) &&
2670 				    FR_ISBLOCK(pass)) {
2671 					if (fr_make_rst(fin) == 0) {
2672 						IPF_BUMP(
2673 						ifs->ifs_frstats[out].fr_ret);
2674 					}
2675 					else if (mp != NULL) {
2676 					/*
2677 					 * we drop packet silently in case we
2678 					 * failed assemble fake response for it
2679 					 */
2680 						FREE_MB_T(*mp);
2681 						m = *mp = NULL;
2682 					}
2683 
2684 					IPF_BUMP(
2685 					    ifs->ifs_frstats[out].fr_block);
2686 					RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2687 
2688 					return (0);
2689 				 }
2690 #endif /* _KERNEL && _SOLARIS2 >= 10 */
2691 				if (fr_send_reset(fin) == 0) {
2692 					IPF_BUMP(ifs->ifs_frstats[1].fr_ret);
2693 				}
2694 			}
2695 		} else {
2696 			if (pass & FR_RETRST)
2697 				fin->fin_error = ECONNRESET;
2698 		}
2699 	}
2700 
2701 	/*
2702 	 * If we didn't drop off the bottom of the list of rules (and thus
2703 	 * the 'current' rule fr is not NULL), then we may have some extra
2704 	 * instructions about what to do with a packet.
2705 	 * Once we're finished return to our caller, freeing the packet if
2706 	 * we are dropping it (* BSD ONLY *).
2707 	 * Reassign m from fin_m as we may have a new buffer, now.
2708 	 */
2709 filtered:
2710 	m = fin->fin_m;
2711 
2712 	if (fr != NULL) {
2713 		frdest_t *fdp;
2714 
2715 		fdp = &fr->fr_tifs[fin->fin_rev];
2716 
2717 		if (!out && (pass & FR_FASTROUTE)) {
2718 			/*
2719 			 * For fastroute rule, no destioation interface defined
2720 			 * so pass NULL as the frdest_t parameter
2721 			 */
2722 			(void) fr_fastroute(m, mp, fin, NULL);
2723 			m = *mp = NULL;
2724 		} else if ((fdp->fd_ifp != NULL) &&
2725 			   (fdp->fd_ifp != (struct ifnet *)-1)) {
2726 			/* this is for to rules: */
2727 			(void) fr_fastroute(m, mp, fin, fdp);
2728 			m = *mp = NULL;
2729 		}
2730 
2731 		/*
2732 		 * Generate a duplicated packet.
2733 		 */
2734 		if (mc != NULL)
2735 			(void) fr_fastroute(mc, &mc, fin, &fr->fr_dif);
2736 	}
2737 
2738 	if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT))
2739 		nat_uncreate(fin);
2740 
2741 	/*
2742 	 * This late because the likes of fr_fastroute() use fin_fr.
2743 	 */
2744 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2745 
2746 finished:
2747 	if (!FR_ISPASS(pass)) {
2748 		IPF_BUMP(ifs->ifs_frstats[out].fr_block);
2749 		if (*mp != NULL) {
2750 			FREE_MB_T(*mp);
2751 			m = *mp = NULL;
2752 		}
2753 	} else {
2754 		IPF_BUMP(ifs->ifs_frstats[out].fr_pass);
2755 #if defined(_KERNEL) && defined(__sgi)
2756 		if ((fin->fin_hbuf != NULL) &&
2757 		    (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) {
2758 			COPYBACK(m, 0, fin->fin_plen, fin->fin_hbuf);
2759 		}
2760 #endif
2761 	}
2762 
2763 	SPL_X(s);
2764 
2765 #ifdef _KERNEL
2766 # if OpenBSD >= 200311
2767 	if (FR_ISPASS(pass) && (v == 4)) {
2768 		ip = fin->fin_ip;
2769 		ip->ip_len = ntohs(ip->ip_len);
2770 		ip->ip_off = ntohs(ip->ip_off);
2771 	}
2772 # endif
2773 	return (FR_ISPASS(pass)) ? 0 : fin->fin_error;
2774 #else /* _KERNEL */
2775 	FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass));
2776 	if ((pass & FR_NOMATCH) != 0)
2777 		return 1;
2778 
2779 	if ((pass & FR_RETMASK) != 0)
2780 		switch (pass & FR_RETMASK)
2781 		{
2782 		case FR_RETRST :
2783 			return 3;
2784 		case FR_RETICMP :
2785 			return 4;
2786 		case FR_FAKEICMP :
2787 			return 5;
2788 		}
2789 
2790 	switch (pass & FR_CMDMASK)
2791 	{
2792 	case FR_PASS :
2793 		return 0;
2794 	case FR_BLOCK :
2795 		return -1;
2796 	case FR_AUTH :
2797 		return -2;
2798 	case FR_ACCOUNT :
2799 		return -3;
2800 	case FR_PREAUTH :
2801 		return -4;
2802 	}
2803 	return 2;
2804 #endif /* _KERNEL */
2805 }
2806 
2807 
2808 #ifdef	IPFILTER_LOG
2809 /* ------------------------------------------------------------------------ */
2810 /* Function:    fr_dolog                                                    */
2811 /* Returns:     frentry_t* - returns contents of fin_fr (no change made)    */
2812 /* Parameters:  fin(I) - pointer to packet information                      */
2813 /*              passp(IO) - pointer to current/new filter decision (unused) */
2814 /*                                                                          */
2815 /* Checks flags set to see how a packet should be logged, if it is to be    */
2816 /* logged.  Adjust statistics based on its success or not.                  */
2817 /* ------------------------------------------------------------------------ */
2818 frentry_t *fr_dolog(fin, passp)
2819 fr_info_t *fin;
2820 u_32_t *passp;
2821 {
2822 	u_32_t pass;
2823 	int out;
2824 	ipf_stack_t *ifs = fin->fin_ifs;
2825 
2826 	out = fin->fin_out;
2827 	pass = *passp;
2828 
2829 	if ((ifs->ifs_fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
2830 		pass |= FF_LOGNOMATCH;
2831 		IPF_BUMP(ifs->ifs_frstats[out].fr_npkl);
2832 		goto logit;
2833 	} else if (((pass & FR_LOGMASK) == FR_LOGP) ||
2834 	    (FR_ISPASS(pass) && (ifs->ifs_fr_flags & FF_LOGPASS))) {
2835 		if ((pass & FR_LOGMASK) != FR_LOGP)
2836 			pass |= FF_LOGPASS;
2837 		IPF_BUMP(ifs->ifs_frstats[out].fr_ppkl);
2838 		goto logit;
2839 	} else if (((pass & FR_LOGMASK) == FR_LOGB) ||
2840 		   (FR_ISBLOCK(pass) && (ifs->ifs_fr_flags & FF_LOGBLOCK))) {
2841 		if ((pass & FR_LOGMASK) != FR_LOGB)
2842 			pass |= FF_LOGBLOCK;
2843 		IPF_BUMP(ifs->ifs_frstats[out].fr_bpkl);
2844 logit:
2845 		if (ipflog(fin, pass) == -1) {
2846 			IPF_BUMP(ifs->ifs_frstats[out].fr_skip);
2847 
2848 			/*
2849 			 * If the "or-block" option has been used then
2850 			 * block the packet if we failed to log it.
2851 			 */
2852 			if ((pass & FR_LOGORBLOCK) &&
2853 			    FR_ISPASS(pass)) {
2854 				pass &= ~FR_CMDMASK;
2855 				pass |= FR_BLOCK;
2856 			}
2857 		}
2858 		*passp = pass;
2859 	}
2860 
2861 	return fin->fin_fr;
2862 }
2863 #endif /* IPFILTER_LOG */
2864 
2865 
2866 /* ------------------------------------------------------------------------ */
2867 /* Function:    ipf_cksum                                                   */
2868 /* Returns:     u_short - IP header checksum                                */
2869 /* Parameters:  addr(I) - pointer to start of buffer to checksum            */
2870 /*              len(I)  - length of buffer in bytes                         */
2871 /*                                                                          */
2872 /* Calculate the two's complement 16 bit checksum of the buffer passed.     */
2873 /*                                                                          */
2874 /* N.B.: addr should be 16bit aligned.                                      */
2875 /* ------------------------------------------------------------------------ */
2876 u_short ipf_cksum(addr, len)
2877 u_short *addr;
2878 int len;
2879 {
2880 	u_32_t sum = 0;
2881 
2882 	for (sum = 0; len > 1; len -= 2)
2883 		sum += *addr++;
2884 
2885 	/* mop up an odd byte, if necessary */
2886 	if (len == 1)
2887 		sum += *(u_char *)addr;
2888 
2889 	/*
2890 	 * add back carry outs from top 16 bits to low 16 bits
2891 	 */
2892 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
2893 	sum += (sum >> 16);			/* add carry */
2894 	return (u_short)(~sum);
2895 }
2896 
2897 
2898 /* ------------------------------------------------------------------------ */
2899 /* Function:    fr_cksum                                                    */
2900 /* Returns:     u_short - layer 4 checksum                                  */
2901 /* Parameters:  m(I  )     - pointer to buffer holding packet               */
2902 /*              ip(I)      - pointer to IP header                           */
2903 /*              l4proto(I) - protocol to caclulate checksum for             */
2904 /*              l4hdr(I)   - pointer to layer 4 header                      */
2905 /*                                                                          */
2906 /* Calculates the TCP checksum for the packet held in "m", using the data   */
2907 /* in the IP header "ip" to seed it.                                        */
2908 /*                                                                          */
2909 /* NB: This function assumes we've pullup'd enough for all of the IP header */
2910 /* and the TCP header.  We also assume that data blocks aren't allocated in */
2911 /* odd sizes.                                                               */
2912 /*                                                                          */
2913 /* Expects ip_len to be in host byte order when called.                     */
2914 /* ------------------------------------------------------------------------ */
2915 u_short fr_cksum(m, ip, l4proto, l4hdr)
2916 mb_t *m;
2917 ip_t *ip;
2918 int l4proto;
2919 void *l4hdr;
2920 {
2921 	u_short *sp, slen, sumsave, l4hlen, *csump;
2922 	u_int sum, sum2;
2923 	int hlen;
2924 #ifdef	USE_INET6
2925 	ip6_t *ip6;
2926 #endif
2927 
2928 	csump = NULL;
2929 	sumsave = 0;
2930 	l4hlen = 0;
2931 	sp = NULL;
2932 	slen = 0;
2933 	hlen = 0;
2934 	sum = 0;
2935 
2936 	/*
2937 	 * Add up IP Header portion
2938 	 */
2939 #ifdef	USE_INET6
2940 	if (IP_V(ip) == 4) {
2941 #endif
2942 		hlen = IP_HL(ip) << 2;
2943 		slen = ip->ip_len - hlen;
2944 		sum = htons((u_short)l4proto);
2945 		sum += htons(slen);
2946 		sp = (u_short *)&ip->ip_src;
2947 		sum += *sp++;	/* ip_src */
2948 		sum += *sp++;
2949 		sum += *sp++;	/* ip_dst */
2950 		sum += *sp++;
2951 #ifdef	USE_INET6
2952 	} else if (IP_V(ip) == 6) {
2953 		ip6 = (ip6_t *)ip;
2954 		hlen = sizeof(*ip6);
2955 		slen = ntohs(ip6->ip6_plen);
2956 		sum = htons((u_short)l4proto);
2957 		sum += htons(slen);
2958 		sp = (u_short *)&ip6->ip6_src;
2959 		sum += *sp++;	/* ip6_src */
2960 		sum += *sp++;
2961 		sum += *sp++;
2962 		sum += *sp++;
2963 		sum += *sp++;
2964 		sum += *sp++;
2965 		sum += *sp++;
2966 		sum += *sp++;
2967 		sum += *sp++;	/* ip6_dst */
2968 		sum += *sp++;
2969 		sum += *sp++;
2970 		sum += *sp++;
2971 		sum += *sp++;
2972 		sum += *sp++;
2973 		sum += *sp++;
2974 		sum += *sp++;
2975 	}
2976 #endif
2977 
2978 	switch (l4proto)
2979 	{
2980 	case IPPROTO_UDP :
2981 		csump = &((udphdr_t *)l4hdr)->uh_sum;
2982 		l4hlen = sizeof(udphdr_t);
2983 		break;
2984 
2985 	case IPPROTO_TCP :
2986 		csump = &((tcphdr_t *)l4hdr)->th_sum;
2987 		l4hlen = sizeof(tcphdr_t);
2988 		break;
2989 	case IPPROTO_ICMP :
2990 		csump = &((icmphdr_t *)l4hdr)->icmp_cksum;
2991 		l4hlen = 4;
2992 		sum = 0;
2993 		break;
2994 	default :
2995 		break;
2996 	}
2997 
2998 	if (csump != NULL) {
2999 		sumsave = *csump;
3000 		*csump = 0;
3001 	}
3002 
3003 	l4hlen = l4hlen;	/* LINT */
3004 
3005 #ifdef	_KERNEL
3006 # ifdef MENTAT
3007 	{
3008 	void *rp = m->b_rptr;
3009 
3010 	if ((unsigned char *)ip > m->b_rptr && (unsigned char *)ip < m->b_wptr)
3011 		m->b_rptr = (u_char *)ip;
3012 	sum2 = ip_cksum(m, hlen, sum);	/* hlen == offset */
3013 	m->b_rptr = rp;
3014 	sum2 = (sum2 & 0xffff) + (sum2 >> 16);
3015 	sum2 = ~sum2 & 0xffff;
3016 	}
3017 # else /* MENTAT */
3018 #  if defined(BSD) || defined(sun)
3019 #   if BSD >= 199103
3020 	m->m_data += hlen;
3021 #   else
3022 	m->m_off += hlen;
3023 #   endif
3024 	m->m_len -= hlen;
3025 	sum2 = in_cksum(m, slen);
3026 	m->m_len += hlen;
3027 #   if BSD >= 199103
3028 	m->m_data -= hlen;
3029 #   else
3030 	m->m_off -= hlen;
3031 #   endif
3032 	/*
3033 	 * Both sum and sum2 are partial sums, so combine them together.
3034 	 */
3035 	sum += ~sum2 & 0xffff;
3036 	while (sum > 0xffff)
3037 		sum = (sum & 0xffff) + (sum >> 16);
3038 	sum2 = ~sum & 0xffff;
3039 #  else /* defined(BSD) || defined(sun) */
3040 {
3041 	union {
3042 		u_char	c[2];
3043 		u_short	s;
3044 	} bytes;
3045 	u_short len = ip->ip_len;
3046 #   if defined(__sgi)
3047 	int add;
3048 #   endif
3049 
3050 	/*
3051 	 * Add up IP Header portion
3052 	 */
3053 	if (sp != (u_short *)l4hdr)
3054 		sp = (u_short *)l4hdr;
3055 
3056 	switch (l4proto)
3057 	{
3058 	case IPPROTO_UDP :
3059 		sum += *sp++;	/* sport */
3060 		sum += *sp++;	/* dport */
3061 		sum += *sp++;	/* udp length */
3062 		sum += *sp++;	/* checksum */
3063 		break;
3064 
3065 	case IPPROTO_TCP :
3066 		sum += *sp++;	/* sport */
3067 		sum += *sp++;	/* dport */
3068 		sum += *sp++;	/* seq */
3069 		sum += *sp++;
3070 		sum += *sp++;	/* ack */
3071 		sum += *sp++;
3072 		sum += *sp++;	/* off */
3073 		sum += *sp++;	/* win */
3074 		sum += *sp++;	/* checksum */
3075 		sum += *sp++;	/* urp */
3076 		break;
3077 	case IPPROTO_ICMP :
3078 		sum = *sp++;	/* type/code */
3079 		sum += *sp++;	/* checksum */
3080 		break;
3081 	}
3082 
3083 #   ifdef	__sgi
3084 	/*
3085 	 * In case we had to copy the IP & TCP header out of mbufs,
3086 	 * skip over the mbuf bits which are the header
3087 	 */
3088 	if ((caddr_t)ip != mtod(m, caddr_t)) {
3089 		hlen = (caddr_t)sp - (caddr_t)ip;
3090 		while (hlen) {
3091 			add = MIN(hlen, m->m_len);
3092 			sp = (u_short *)(mtod(m, caddr_t) + add);
3093 			hlen -= add;
3094 			if (add == m->m_len) {
3095 				m = m->m_next;
3096 				if (!hlen) {
3097 					if (!m)
3098 						break;
3099 					sp = mtod(m, u_short *);
3100 				}
3101 				PANIC((!m),("fr_cksum(1): not enough data"));
3102 			}
3103 		}
3104 	}
3105 #   endif
3106 
3107 	len -= (l4hlen + hlen);
3108 	if (len <= 0)
3109 		goto nodata;
3110 
3111 	while (len > 1) {
3112 		if (((caddr_t)sp - mtod(m, caddr_t)) >= m->m_len) {
3113 			m = m->m_next;
3114 			PANIC((!m),("fr_cksum(2): not enough data"));
3115 			sp = mtod(m, u_short *);
3116 		}
3117 		if (((caddr_t)(sp + 1) - mtod(m, caddr_t)) > m->m_len) {
3118 			bytes.c[0] = *(u_char *)sp;
3119 			m = m->m_next;
3120 			PANIC((!m),("fr_cksum(3): not enough data"));
3121 			sp = mtod(m, u_short *);
3122 			bytes.c[1] = *(u_char *)sp;
3123 			sum += bytes.s;
3124 			sp = (u_short *)((u_char *)sp + 1);
3125 		}
3126 		if ((u_long)sp & 1) {
3127 			bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s));
3128 			sum += bytes.s;
3129 		} else
3130 			sum += *sp++;
3131 		len -= 2;
3132 	}
3133 
3134 	if (len != 0)
3135 		sum += ntohs(*(u_char *)sp << 8);
3136 nodata:
3137 	while (sum > 0xffff)
3138 		sum = (sum & 0xffff) + (sum >> 16);
3139 	sum2 = (u_short)(~sum & 0xffff);
3140 }
3141 #  endif /*  defined(BSD) || defined(sun) */
3142 # endif /* MENTAT */
3143 #else /* _KERNEL */
3144 	for (; slen > 1; slen -= 2)
3145 	        sum += *sp++;
3146 	if (slen)
3147 		sum += ntohs(*(u_char *)sp << 8);
3148 	while (sum > 0xffff)
3149 		sum = (sum & 0xffff) + (sum >> 16);
3150 	sum2 = (u_short)(~sum & 0xffff);
3151 #endif /* _KERNEL */
3152 	if (csump != NULL)
3153 		*csump = sumsave;
3154 	return sum2;
3155 }
3156 
3157 
3158 #if defined(_KERNEL) && ( ((BSD < 199103) && !defined(MENTAT)) || \
3159     defined(__sgi) ) && !defined(linux) && !defined(_AIX51)
3160 /*
3161  * Copyright (c) 1982, 1986, 1988, 1991, 1993
3162  *	The Regents of the University of California.  All rights reserved.
3163  *
3164  * Redistribution and use in source and binary forms, with or without
3165  * modification, are permitted provided that the following conditions
3166  * are met:
3167  * 1. Redistributions of source code must retain the above copyright
3168  *    notice, this list of conditions and the following disclaimer.
3169  * 2. Redistributions in binary form must reproduce the above copyright
3170  *    notice, this list of conditions and the following disclaimer in the
3171  *    documentation and/or other materials provided with the distribution.
3172  * 3. Neither the name of the University nor the names of its contributors
3173  *    may be used to endorse or promote products derived from this software
3174  *    without specific prior written permission.
3175  *
3176  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3177  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3178  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3179  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3180  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3181  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3182  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3183  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3184  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3185  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3186  * SUCH DAMAGE.
3187  *
3188  *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
3189  * $Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $
3190  */
3191 /*
3192  * Copy data from an mbuf chain starting "off" bytes from the beginning,
3193  * continuing for "len" bytes, into the indicated buffer.
3194  */
3195 void
3196 m_copydata(m, off, len, cp)
3197 	mb_t *m;
3198 	int off;
3199 	int len;
3200 	caddr_t cp;
3201 {
3202 	unsigned count;
3203 
3204 	if (off < 0 || len < 0)
3205 		panic("m_copydata");
3206 	while (off > 0) {
3207 		if (m == 0)
3208 			panic("m_copydata");
3209 		if (off < m->m_len)
3210 			break;
3211 		off -= m->m_len;
3212 		m = m->m_next;
3213 	}
3214 	while (len > 0) {
3215 		if (m == 0)
3216 			panic("m_copydata");
3217 		count = MIN(m->m_len - off, len);
3218 		bcopy(mtod(m, caddr_t) + off, cp, count);
3219 		len -= count;
3220 		cp += count;
3221 		off = 0;
3222 		m = m->m_next;
3223 	}
3224 }
3225 
3226 
3227 /*
3228  * Copy data from a buffer back into the indicated mbuf chain,
3229  * starting "off" bytes from the beginning, extending the mbuf
3230  * chain if necessary.
3231  */
3232 void
3233 m_copyback(m0, off, len, cp)
3234 	struct	mbuf *m0;
3235 	int off;
3236 	int len;
3237 	caddr_t cp;
3238 {
3239 	int mlen;
3240 	struct mbuf *m = m0, *n;
3241 	int totlen = 0;
3242 
3243 	if (m0 == 0)
3244 		return;
3245 	while (off > (mlen = m->m_len)) {
3246 		off -= mlen;
3247 		totlen += mlen;
3248 		if (m->m_next == 0) {
3249 			n = m_getclr(M_DONTWAIT, m->m_type);
3250 			if (n == 0)
3251 				goto out;
3252 			n->m_len = min(MLEN, len + off);
3253 			m->m_next = n;
3254 		}
3255 		m = m->m_next;
3256 	}
3257 	while (len > 0) {
3258 		mlen = min(m->m_len - off, len);
3259 		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
3260 		cp += mlen;
3261 		len -= mlen;
3262 		mlen += off;
3263 		off = 0;
3264 		totlen += mlen;
3265 		if (len == 0)
3266 			break;
3267 		if (m->m_next == 0) {
3268 			n = m_get(M_DONTWAIT, m->m_type);
3269 			if (n == 0)
3270 				break;
3271 			n->m_len = min(MLEN, len);
3272 			m->m_next = n;
3273 		}
3274 		m = m->m_next;
3275 	}
3276 out:
3277 #if 0
3278 	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
3279 		m->m_pkthdr.len = totlen;
3280 #endif
3281 	return;
3282 }
3283 #endif /* (_KERNEL) && ( ((BSD < 199103) && !MENTAT) || __sgi) */
3284 
3285 
3286 /* ------------------------------------------------------------------------ */
3287 /* Function:    fr_findgroup                                                */
3288 /* Returns:     frgroup_t * - NULL = group not found, else pointer to group */
3289 /* Parameters:  group(I) - group name to search for                         */
3290 /*              unit(I)  - device to which this group belongs               */
3291 /*              set(I)   - which set of rules (inactive/inactive) this is   */
3292 /*              fgpp(O)  - pointer to place to store pointer to the pointer */
3293 /*                         to where to add the next (last) group or where   */
3294 /*                         to delete group from.                            */
3295 /*                                                                          */
3296 /* Search amongst the defined groups for a particular group number.         */
3297 /* ------------------------------------------------------------------------ */
3298 frgroup_t *fr_findgroup(group, unit, set, fgpp, ifs)
3299 char *group;
3300 minor_t unit;
3301 int set;
3302 frgroup_t ***fgpp;
3303 ipf_stack_t *ifs;
3304 {
3305 	frgroup_t *fg, **fgp;
3306 
3307 	/*
3308 	 * Which list of groups to search in is dependent on which list of
3309 	 * rules are being operated on.
3310 	 */
3311 	fgp = &ifs->ifs_ipfgroups[unit][set];
3312 
3313 	while ((fg = *fgp) != NULL) {
3314 		if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0)
3315 			break;
3316 		else
3317 			fgp = &fg->fg_next;
3318 	}
3319 	if (fgpp != NULL)
3320 		*fgpp = fgp;
3321 	return fg;
3322 }
3323 
3324 
3325 /* ------------------------------------------------------------------------ */
3326 /* Function:    fr_addgroup                                                 */
3327 /* Returns:     frgroup_t * - NULL == did not create group,                 */
3328 /*                            != NULL == pointer to the group               */
3329 /* Parameters:  num(I)   - group number to add                              */
3330 /*              head(I)  - rule pointer that is using this as the head      */
3331 /*              flags(I) - rule flags which describe the type of rule it is */
3332 /*              unit(I)  - device to which this group will belong to        */
3333 /*              set(I)   - which set of rules (inactive/inactive) this is   */
3334 /* Write Locks: ipf_mutex                                                   */
3335 /*                                                                          */
3336 /* Add a new group head, or if it already exists, increase the reference    */
3337 /* count to it.                                                             */
3338 /* ------------------------------------------------------------------------ */
3339 frgroup_t *fr_addgroup(group, head, flags, unit, set, ifs)
3340 char *group;
3341 void *head;
3342 u_32_t flags;
3343 minor_t unit;
3344 int set;
3345 ipf_stack_t *ifs;
3346 {
3347 	frgroup_t *fg, **fgp;
3348 	u_32_t gflags;
3349 
3350 	if (group == NULL)
3351 		return NULL;
3352 
3353 	if (unit == IPL_LOGIPF && *group == '\0')
3354 		return NULL;
3355 
3356 	fgp = NULL;
3357 	gflags = flags & FR_INOUT;
3358 
3359 	fg = fr_findgroup(group, unit, set, &fgp, ifs);
3360 	if (fg != NULL) {
3361 		if (fg->fg_flags == 0)
3362 			fg->fg_flags = gflags;
3363 		else if (gflags != fg->fg_flags)
3364 			return NULL;
3365 		fg->fg_ref++;
3366 		return fg;
3367 	}
3368 	KMALLOC(fg, frgroup_t *);
3369 	if (fg != NULL) {
3370 		fg->fg_head = head;
3371 		fg->fg_start = NULL;
3372 		fg->fg_next = *fgp;
3373 		bcopy(group, fg->fg_name, FR_GROUPLEN);
3374 		fg->fg_flags = gflags;
3375 		fg->fg_ref = 1;
3376 		*fgp = fg;
3377 	}
3378 	return fg;
3379 }
3380 
3381 
3382 /* ------------------------------------------------------------------------ */
3383 /* Function:    fr_delgroup                                                 */
3384 /* Returns:     Nil                                                         */
3385 /* Parameters:  group(I) - group name to delete                             */
3386 /*              unit(I)  - device to which this group belongs               */
3387 /*              set(I)   - which set of rules (inactive/inactive) this is   */
3388 /* Write Locks: ipf_mutex                                                   */
3389 /*                                                                          */
3390 /* Attempt to delete a group head.                                          */
3391 /* Only do this when its reference count reaches 0.                         */
3392 /* ------------------------------------------------------------------------ */
3393 void fr_delgroup(group, unit, set, ifs)
3394 char *group;
3395 minor_t unit;
3396 int set;
3397 ipf_stack_t *ifs;
3398 {
3399 	frgroup_t *fg, **fgp;
3400 
3401 	fg = fr_findgroup(group, unit, set, &fgp, ifs);
3402 	if (fg == NULL)
3403 		return;
3404 
3405 	fg->fg_ref--;
3406 	if (fg->fg_ref == 0) {
3407 		*fgp = fg->fg_next;
3408 		KFREE(fg);
3409 	}
3410 }
3411 
3412 
3413 /* ------------------------------------------------------------------------ */
3414 /* Function:    fr_getrulen                                                 */
3415 /* Returns:     frentry_t * - NULL == not found, else pointer to rule n     */
3416 /* Parameters:  unit(I)  - device for which to count the rule's number      */
3417 /*              flags(I) - which set of rules to find the rule in           */
3418 /*              group(I) - group name                                       */
3419 /*              n(I)     - rule number to find                              */
3420 /*                                                                          */
3421 /* Find rule # n in group # g and return a pointer to it.  Return NULl if   */
3422 /* group # g doesn't exist or there are less than n rules in the group.     */
3423 /* ------------------------------------------------------------------------ */
3424 frentry_t *fr_getrulen(unit, group, n, ifs)
3425 int unit;
3426 char *group;
3427 u_32_t n;
3428 ipf_stack_t *ifs;
3429 {
3430 	frentry_t *fr;
3431 	frgroup_t *fg;
3432 
3433 	fg = fr_findgroup(group, unit, ifs->ifs_fr_active, NULL, ifs);
3434 	if (fg == NULL)
3435 		return NULL;
3436 	for (fr = fg->fg_head; fr && n; fr = fr->fr_next, n--)
3437 		;
3438 	if (n != 0)
3439 		return NULL;
3440 	return fr;
3441 }
3442 
3443 
3444 /* ------------------------------------------------------------------------ */
3445 /* Function:    fr_rulen                                                    */
3446 /* Returns:     int - >= 0 - rule number, -1 == search failed               */
3447 /* Parameters:  unit(I) - device for which to count the rule's number       */
3448 /*              fr(I)   - pointer to rule to match                          */
3449 /*                                                                          */
3450 /* Return the number for a rule on a specific filtering device.             */
3451 /* ------------------------------------------------------------------------ */
3452 int fr_rulen(unit, fr, ifs)
3453 int unit;
3454 frentry_t *fr;
3455 ipf_stack_t *ifs;
3456 {
3457 	frentry_t *fh;
3458 	frgroup_t *fg;
3459 	u_32_t n = 0;
3460 
3461 	if (fr == NULL)
3462 		return -1;
3463 	fg = fr_findgroup(fr->fr_group, unit, ifs->ifs_fr_active, NULL, ifs);
3464 	if (fg == NULL)
3465 		return -1;
3466 	for (fh = fg->fg_head; fh; n++, fh = fh->fr_next)
3467 		if (fh == fr)
3468 			break;
3469 	if (fh == NULL)
3470 		return -1;
3471 	return n;
3472 }
3473 
3474 
3475 /* ------------------------------------------------------------------------ */
3476 /* Function:    frflushlist                                                 */
3477 /* Returns:     int - >= 0 - number of flushed rules                        */
3478 /* Parameters:  set(I)   - which set of rules (inactive/inactive) this is   */
3479 /*              unit(I)  - device for which to flush rules                  */
3480 /*              flags(I) - which set of rules to flush                      */
3481 /*              nfreedp(O) - pointer to int where flush count is stored     */
3482 /*              listp(I)   - pointer to list to flush pointer               */
3483 /* Write Locks: ipf_mutex                                                   */
3484 /*                                                                          */
3485 /* Recursively flush rules from the list, descending groups as they are     */
3486 /* encountered.  if a rule is the head of a group and it has lost all its   */
3487 /* group members, then also delete the group reference.  nfreedp is needed  */
3488 /* to store the accumulating count of rules removed, whereas the returned   */
3489 /* value is just the number removed from the current list.  The latter is   */
3490 /* needed to correctly adjust reference counts on rules that define groups. */
3491 /*                                                                          */
3492 /* NOTE: Rules not loaded from user space cannot be flushed.                */
3493 /* ------------------------------------------------------------------------ */
3494 static int frflushlist(set, unit, nfreedp, listp, ifs)
3495 int set;
3496 minor_t unit;
3497 int *nfreedp;
3498 frentry_t **listp;
3499 ipf_stack_t *ifs;
3500 {
3501 	int freed = 0;
3502 	frentry_t *fp;
3503 
3504 	while ((fp = *listp) != NULL) {
3505 		if ((fp->fr_type & FR_T_BUILTIN) ||
3506 		    !(fp->fr_flags & FR_COPIED)) {
3507 			listp = &fp->fr_next;
3508 			continue;
3509 		}
3510 		*listp = fp->fr_next;
3511 		if (fp->fr_grp != NULL) {
3512 			(void) frflushlist(set, unit, nfreedp, fp->fr_grp, ifs);
3513 		}
3514 
3515 		if (fp->fr_grhead != NULL) {
3516 			fr_delgroup(fp->fr_grhead, unit, set, ifs);
3517 			*fp->fr_grhead = '\0';
3518 		}
3519 
3520 		ASSERT(fp->fr_ref > 0);
3521 		fp->fr_next = NULL;
3522 		if (fr_derefrule(&fp, ifs) == 0)
3523 			freed++;
3524 	}
3525 	*nfreedp += freed;
3526 	return freed;
3527 }
3528 
3529 
3530 /* ------------------------------------------------------------------------ */
3531 /* Function:    frflush                                                     */
3532 /* Returns:     int - >= 0 - number of flushed rules                        */
3533 /* Parameters:  unit(I)  - device for which to flush rules                  */
3534 /*              flags(I) - which set of rules to flush                      */
3535 /*                                                                          */
3536 /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
3537 /* and IPv6) as defined by the value of flags.                              */
3538 /* ------------------------------------------------------------------------ */
3539 int frflush(unit, proto, flags, ifs)
3540 minor_t unit;
3541 int proto, flags;
3542 ipf_stack_t *ifs;
3543 {
3544 	int flushed = 0, set;
3545 
3546 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
3547 	bzero((char *)ifs->ifs_frcache, sizeof (ifs->ifs_frcache));
3548 
3549 	set = ifs->ifs_fr_active;
3550 	if ((flags & FR_INACTIVE) == FR_INACTIVE)
3551 		set = 1 - set;
3552 
3553 	if (flags & FR_OUTQUE) {
3554 		if (proto == 0 || proto == 6) {
3555 			(void) frflushlist(set, unit,
3556 			    &flushed, &ifs->ifs_ipfilter6[1][set], ifs);
3557 			(void) frflushlist(set, unit,
3558 			    &flushed, &ifs->ifs_ipacct6[1][set], ifs);
3559 		}
3560 		if (proto == 0 || proto == 4) {
3561 			(void) frflushlist(set, unit,
3562 			    &flushed, &ifs->ifs_ipfilter[1][set], ifs);
3563 			(void) frflushlist(set, unit,
3564 			    &flushed, &ifs->ifs_ipacct[1][set], ifs);
3565 		}
3566 	}
3567 	if (flags & FR_INQUE) {
3568 		if (proto == 0 || proto == 6) {
3569 			(void) frflushlist(set, unit,
3570 			    &flushed, &ifs->ifs_ipfilter6[0][set], ifs);
3571 			(void) frflushlist(set, unit,
3572 			    &flushed, &ifs->ifs_ipacct6[0][set], ifs);
3573 		}
3574 		if (proto == 0 || proto == 4) {
3575 			(void) frflushlist(set, unit,
3576 			    &flushed, &ifs->ifs_ipfilter[0][set], ifs);
3577 			(void) frflushlist(set, unit,
3578 			    &flushed, &ifs->ifs_ipacct[0][set], ifs);
3579 		}
3580 	}
3581 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
3582 
3583 	if (unit == IPL_LOGIPF) {
3584 		int tmp;
3585 
3586 		tmp = frflush(IPL_LOGCOUNT, proto, flags, ifs);
3587 		if (tmp >= 0)
3588 			flushed += tmp;
3589 	}
3590 	return flushed;
3591 }
3592 
3593 
3594 /* ------------------------------------------------------------------------ */
3595 /* Function:    memstr                                                      */
3596 /* Returns:     char *  - NULL if failed, != NULL pointer to matching bytes */
3597 /* Parameters:  src(I)  - pointer to byte sequence to match                 */
3598 /*              dst(I)  - pointer to byte sequence to search                */
3599 /*              slen(I) - match length                                      */
3600 /*              dlen(I) - length available to search in                     */
3601 /*                                                                          */
3602 /* Search dst for a sequence of bytes matching those at src and extend for  */
3603 /* slen bytes.                                                              */
3604 /* ------------------------------------------------------------------------ */
3605 char *memstr(src, dst, slen, dlen)
3606 char *src, *dst;
3607 int slen, dlen;
3608 {
3609 	char *s = NULL;
3610 
3611 	while (dlen >= slen) {
3612 		if (bcmp(src, dst, slen) == 0) {
3613 			s = dst;
3614 			break;
3615 		}
3616 		dst++;
3617 		dlen--;
3618 	}
3619 	return s;
3620 }
3621 /* ------------------------------------------------------------------------ */
3622 /* Function:    fr_fixskip                                                  */
3623 /* Returns:     Nil                                                         */
3624 /* Parameters:  listp(IO)    - pointer to start of list with skip rule      */
3625 /*              rp(I)        - rule added/removed with skip in it.          */
3626 /*              addremove(I) - adjustment (-1/+1) to make to skip count,    */
3627 /*                             depending on whether a rule was just added   */
3628 /*                             or removed.                                  */
3629 /*                                                                          */
3630 /* Adjust all the rules in a list which would have skip'd past the position */
3631 /* where we are inserting to skip to the right place given the change.      */
3632 /* ------------------------------------------------------------------------ */
3633 void fr_fixskip(listp, rp, addremove)
3634 frentry_t **listp, *rp;
3635 int addremove;
3636 {
3637 	int rules, rn;
3638 	frentry_t *fp;
3639 
3640 	rules = 0;
3641 	for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next)
3642 		rules++;
3643 
3644 	if (!fp)
3645 		return;
3646 
3647 	for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
3648 		if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules))
3649 			fp->fr_arg += addremove;
3650 }
3651 
3652 
3653 #ifdef	_KERNEL
3654 /* ------------------------------------------------------------------------ */
3655 /* Function:    count4bits                                                  */
3656 /* Returns:     int - >= 0 - number of consecutive bits in input            */
3657 /* Parameters:  ip(I) - 32bit IP address                                    */
3658 /*                                                                          */
3659 /* IPv4 ONLY                                                                */
3660 /* count consecutive 1's in bit mask.  If the mask generated by counting    */
3661 /* consecutive 1's is different to that passed, return -1, else return #    */
3662 /* of bits.                                                                 */
3663 /* ------------------------------------------------------------------------ */
3664 int	count4bits(ip)
3665 u_32_t	ip;
3666 {
3667 	u_32_t	ipn;
3668 	int	cnt = 0, i, j;
3669 
3670 	ip = ipn = ntohl(ip);
3671 	for (i = 32; i; i--, ipn *= 2)
3672 		if (ipn & 0x80000000)
3673 			cnt++;
3674 		else
3675 			break;
3676 	ipn = 0;
3677 	for (i = 32, j = cnt; i; i--, j--) {
3678 		ipn *= 2;
3679 		if (j > 0)
3680 			ipn++;
3681 	}
3682 	if (ipn == ip)
3683 		return cnt;
3684 	return -1;
3685 }
3686 
3687 
3688 #ifdef USE_INET6
3689 /* ------------------------------------------------------------------------ */
3690 /* Function:    count6bits                                                  */
3691 /* Returns:     int - >= 0 - number of consecutive bits in input            */
3692 /* Parameters:  msk(I) - pointer to start of IPv6 bitmask                   */
3693 /*                                                                          */
3694 /* IPv6 ONLY                                                                */
3695 /* count consecutive 1's in bit mask.                                       */
3696 /* ------------------------------------------------------------------------ */
3697 int count6bits(msk)
3698 u_32_t *msk;
3699 {
3700 	int i = 0, k;
3701 	u_32_t j;
3702 
3703 	for (k = 3; k >= 0; k--)
3704 		if (msk[k] == 0xffffffff)
3705 			i += 32;
3706 		else {
3707 			for (j = msk[k]; j; j <<= 1)
3708 				if (j & 0x80000000)
3709 					i++;
3710 		}
3711 	return i;
3712 }
3713 # endif
3714 #endif /* _KERNEL */
3715 
3716 
3717 /* ------------------------------------------------------------------------ */
3718 /* Function:    fr_ifsync                                                   */
3719 /* Returns:     void *    - new interface identifier                        */
3720 /* Parameters:  action(I)  - type of synchronisation to do                  */
3721 /*              v(I)       - IP version being sync'd (v4 or v6)             */
3722 /*              newifp(I)  - interface identifier being introduced/removed  */
3723 /*              oldifp(I)  - interface identifier in a filter rule          */
3724 /*              newname(I) - name associated with newifp interface          */
3725 /*              oldname(I) - name associated with oldifp interface          */
3726 /*		ifs       - pointer to IPF stack instance		    */
3727 /*                                                                          */
3728 /* This function returns what the new value for "oldifp" should be for its  */
3729 /* caller.  In some cases it will not change, in some it will.              */
3730 /* action == IPFSYNC_RESYNC                                                 */
3731 /*   a new value for oldifp will always be looked up, according to oldname, */
3732 /*   the values of newname and newifp are ignored.                          */
3733 /* action == IPFSYNC_NEWIFP                                                 */
3734 /*   if oldname matches newname then we are doing a sync for the matching   */
3735 /*   interface, so we return newifp to be used in place of oldifp.  If the  */
3736 /*   the names don't match, just return oldifp.                             */
3737 /* action == IPFSYNC_OLDIFP                                                 */
3738 /*   if oldifp matches newifp then we are are doing a sync to remove any    */
3739 /*   references to oldifp, so we return "-1".                               */
3740 /* -----								    */
3741 /* NOTE:								    */
3742 /* This function processes NIC event from PF_HOOKS. The action parameter    */
3743 /* is set in ipf_nic_event_v4()/ipf_nic_event_v6() function. There is	    */
3744 /* one single switch statement() in ipf_nic_event_vx() function, which	    */
3745 /* translates the HOOK event type to action parameter passed to fr_ifsync.  */
3746 /* The translation table looks as follows:				    */
3747 /*	event		| action					    */
3748 /*	----------------+-------------					    */
3749 /*	NE_PLUMB	| IPFSYNC_NEWIFP				    */
3750 /*	NE_UNPLUMB	| IPFSYNC_OLDIFP				    */
3751 /*    NE_ADDRESS_CHANGE	| IPFSYNC_RESYNC				    */
3752 /*									    */
3753 /* The oldname and oldifp parameters are taken from IPF entry (rule, state  */
3754 /* table entry, NAT table entry, fragment ...). The newname and newifp	    */
3755 /* parameters come from hook event data, parameters are taken from event    */
3756 /* in ipf_nic_event_vx() functions. Any time NIC changes, the IPF is	    */
3757 /* notified by hook function.						    */
3758 /*									    */
3759 /* We get NE_UNPLUMB event from PF_HOOKS even if someone coincidently tries */
3760 /* to plumb the interface, which is already plumbed. In such case we always */
3761 /* get the event from PF_HOOKS as follows:				    */
3762 /*	event:	NE_PLUMB						    */
3763 /*	NIC:	0x0							    */
3764 /* ------------------------------------------------------------------------ */
3765 static void *fr_ifsync(action, v, newname, oldname, newifp, oldifp, ifs)
3766 int action, v;
3767 char *newname, *oldname;
3768 void *newifp, *oldifp;
3769 ipf_stack_t *ifs;
3770 {
3771 	void *rval = oldifp;
3772 
3773 	switch (action)
3774 	{
3775 	case IPFSYNC_RESYNC :
3776 		if (oldname[0] != '\0') {
3777 			rval = fr_resolvenic(oldname, v, ifs);
3778 		}
3779 		break;
3780 	case IPFSYNC_NEWIFP :
3781 		if (!strncmp(newname, oldname, LIFNAMSIZ))
3782 			rval = newifp;
3783 		break;
3784 	case IPFSYNC_OLDIFP :
3785 		/*
3786 		 * If interface gets unplumbed it must be invalidated, which
3787 		 * means set all existing references to the interface to -1.
3788 		 * We don't want to invalidate references for wildcard
3789 		 * (unbound) rules (entries).
3790 		 */
3791 		if (newifp == oldifp)
3792 			rval = (oldifp) ? (void *)-1 : NULL;
3793 		break;
3794 	}
3795 
3796 	return rval;
3797 }
3798 
3799 
3800 /* ------------------------------------------------------------------------ */
3801 /* Function:    frsynclist                                                  */
3802 /* Returns:     void                                                        */
3803 /* Parameters:  action(I) - type of synchronisation to do                   */
3804 /*              v(I)      - IP version being sync'd (v4 or v6)              */
3805 /*              ifp(I)    - interface identifier associated with action     */
3806 /*              ifname(I) - name associated with ifp parameter              */
3807 /*              fr(I)     - pointer to filter rule                          */
3808 /*		ifs       - pointer to IPF stack instance		    */
3809 /* Write Locks: ipf_mutex                                                   */
3810 /*                                                                          */
3811 /* Walk through a list of filter rules and resolve any interface names into */
3812 /* pointers.  Where dynamic addresses are used, also update the IP address  */
3813 /* used in the rule.  The interface pointer is used to limit the lookups to */
3814 /* a specific set of matching names if it is non-NULL.                      */
3815 /* ------------------------------------------------------------------------ */
3816 static void frsynclist(action, v, ifp, ifname, fr, ifs)
3817 int action, v;
3818 void *ifp;
3819 char *ifname;
3820 frentry_t *fr;
3821 ipf_stack_t *ifs;
3822 {
3823 	frdest_t *fdp;
3824 	int rv, i;
3825 
3826 	for (; fr; fr = fr->fr_next) {
3827 		rv = fr->fr_v;
3828 		if (v != 0 && v != rv)
3829 			continue;
3830 
3831 		/*
3832 		 * Lookup all the interface names that are part of the rule.
3833 		 */
3834 		for (i = 0; i < 4; i++) {
3835 			fr->fr_ifas[i] = fr_ifsync(action, rv, ifname,
3836 						   fr->fr_ifnames[i],
3837 						   ifp, fr->fr_ifas[i],
3838 						   ifs);
3839 		}
3840 
3841 		fdp = &fr->fr_tifs[0];
3842 		fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname,
3843 					   ifp, fdp->fd_ifp, ifs);
3844 
3845 		fdp = &fr->fr_tifs[1];
3846 		fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname,
3847 					   ifp, fdp->fd_ifp, ifs);
3848 
3849 		fdp = &fr->fr_dif;
3850 		fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname,
3851 					   ifp, fdp->fd_ifp, ifs);
3852 
3853 		if (action != IPFSYNC_RESYNC)
3854 			continue;
3855 
3856 		if (fr->fr_type == FR_T_IPF) {
3857 			if (fr->fr_satype != FRI_NORMAL &&
3858 			    fr->fr_satype != FRI_LOOKUP) {
3859 				(void)fr_ifpaddr(rv, fr->fr_satype,
3860 						 fr->fr_ifas[fr->fr_sifpidx],
3861 						 &fr->fr_src, &fr->fr_smsk,
3862 						 ifs);
3863 			}
3864 			if (fr->fr_datype != FRI_NORMAL &&
3865 			    fr->fr_datype != FRI_LOOKUP) {
3866 				(void)fr_ifpaddr(rv, fr->fr_datype,
3867 						 fr->fr_ifas[fr->fr_difpidx],
3868 						 &fr->fr_dst, &fr->fr_dmsk,
3869 						 ifs);
3870 			}
3871 		}
3872 
3873 #ifdef	IPFILTER_LOOKUP
3874 		if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP &&
3875 		    fr->fr_srcptr == NULL) {
3876 			fr->fr_srcptr = fr_resolvelookup(fr->fr_srctype,
3877 							 fr->fr_srcnum,
3878 							 &fr->fr_srcfunc, ifs);
3879 		}
3880 		if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP &&
3881 		    fr->fr_dstptr == NULL) {
3882 			fr->fr_dstptr = fr_resolvelookup(fr->fr_dsttype,
3883 							 fr->fr_dstnum,
3884 							 &fr->fr_dstfunc, ifs);
3885 		}
3886 #endif
3887 	}
3888 }
3889 
3890 
3891 #ifdef	_KERNEL
3892 /* ------------------------------------------------------------------------ */
3893 /* Function:    frsync                                                      */
3894 /* Returns:     void                                                        */
3895 /* Parameters:  action(I) - type of synchronisation to do                   */
3896 /*              v(I)      - IP version being sync'd (v4 or v6)              */
3897 /*              ifp(I)    - interface identifier associated with action     */
3898 /*              name(I)   - name associated with ifp parameter              */
3899 /*                                                                          */
3900 /* frsync() is called when we suspect that the interface list or            */
3901 /* information about interfaces (like IP#) has changed.  Go through all     */
3902 /* filter rules, NAT entries and the state table and check if anything      */
3903 /* needs to be changed/updated.                                             */
3904 /* With the filtering hooks added to Solaris, we needed to change the manner*/
3905 /* in which this was done to support three different types of sync:         */
3906 /* - complete resync of all interface name/identifiers                      */
3907 /* - new interface being announced with its name and identifier             */
3908 /* - interface removal being announced by only its identifier               */
3909 /* ------------------------------------------------------------------------ */
3910 void frsync(action, v, ifp, name, ifs)
3911 int action, v;
3912 void *ifp;
3913 char *name;
3914 ipf_stack_t *ifs;
3915 {
3916 	int i;
3917 
3918 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
3919 	frsynclist(action, v, ifp, name, ifs->ifs_ipacct[0][ifs->ifs_fr_active], ifs);
3920 	frsynclist(action, v, ifp, name, ifs->ifs_ipacct[1][ifs->ifs_fr_active], ifs);
3921 	frsynclist(action, v, ifp, name, ifs->ifs_ipfilter[0][ifs->ifs_fr_active], ifs);
3922 	frsynclist(action, v, ifp, name, ifs->ifs_ipfilter[1][ifs->ifs_fr_active], ifs);
3923 	frsynclist(action, v, ifp, name, ifs->ifs_ipacct6[0][ifs->ifs_fr_active], ifs);
3924 	frsynclist(action, v, ifp, name, ifs->ifs_ipacct6[1][ifs->ifs_fr_active], ifs);
3925 	frsynclist(action, v, ifp, name, ifs->ifs_ipfilter6[0][ifs->ifs_fr_active], ifs);
3926 	frsynclist(action, v, ifp, name, ifs->ifs_ipfilter6[1][ifs->ifs_fr_active], ifs);
3927 
3928 	for (i = 0; i < IPL_LOGSIZE; i++) {
3929 		frgroup_t *g;
3930 
3931 		for (g = ifs->ifs_ipfgroups[i][0]; g != NULL; g = g->fg_next)
3932 			frsynclist(action, v, ifp, name, g->fg_start, ifs);
3933 		for (g = ifs->ifs_ipfgroups[i][1]; g != NULL; g = g->fg_next)
3934 			frsynclist(action, v, ifp, name, g->fg_start, ifs);
3935 	}
3936 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
3937 }
3938 
3939 #if SOLARIS2 >= 10
3940 /* ------------------------------------------------------------------------ */
3941 /* Function:    fr_syncindex						    */
3942 /* Returns:     void							    */
3943 /* Parameters:  rules	  - list of rules to be sync'd			    */
3944 /*		ifp	  - interface, which is being sync'd		    */
3945 /*		newifp	  - new ifindex value for interface		    */
3946 /*                                                                          */
3947 /* Function updates all NIC indecis, which match ifp, in every rule. Every  */
3948 /* NIC index matching ifp, will be updated to newifp.			    */
3949 /* ------------------------------------------------------------------------ */
3950 static void fr_syncindex(rules, ifp, newifp)
3951 frentry_t *rules;
3952 void *ifp;
3953 void *newifp;
3954 {
3955 	int i;
3956 	frentry_t *fr;
3957 
3958 	for (fr = rules; fr != NULL; fr = fr->fr_next) {
3959 		/*
3960 		 * Lookup all the interface names that are part of the rule.
3961 		 */
3962 		for (i = 0; i < 4; i++)
3963 			if (fr->fr_ifas[i] == ifp)
3964 				fr->fr_ifas[i] = newifp;
3965 
3966 		for (i = 0; i < 2; i++) {
3967 			if (fr->fr_tifs[i].fd_ifp == ifp)
3968 				fr->fr_tifs[i].fd_ifp = newifp;
3969 		}
3970 
3971 		if (fr->fr_dif.fd_ifp == ifp)
3972 			fr->fr_dif.fd_ifp = newifp;
3973 	}
3974 }
3975 
3976 /* ------------------------------------------------------------------------ */
3977 /* Function:    fr_ifindexsync						    */
3978 /* Returns:     void							    */
3979 /* Parameters:	ifp	  - interface, which is being sync'd		    */
3980 /*		newifp	  - new ifindex value for interface		    */
3981 /*              ifs	  - IPF's stack					    */
3982 /*                                                                          */
3983 /* Function assumes ipf_mutex is locked exclusively.			    */
3984 /* 									    */
3985 /* Function updates the NIC references in rules with new interfaces index   */
3986 /* (newifp). Function must process active lists:			    */
3987 /*	with accounting rules (IPv6 and IPv4)				    */
3988 /*	with inbound rules (IPv6 and IPv4)				    */
3989 /*	with outbound rules (IPv6 and IPv4)				    */
3990 /* Function also has to take care of rule groups.			    */
3991 /*                                                                          */
3992 /* NOTE: The ipf_mutex is grabbed exclusively by caller (which is always    */
3993 /* nic_event_hook). The hook function also updates state entries, NAT rules */
3994 /* and NAT entries. We want to do all these update atomically to keep the   */
3995 /* NIC references consistent. The ipf_mutex will synchronize event with	    */
3996 /* fr_check(), which processes packets,	so no packet will enter fr_check(), */
3997 /* while NIC references will be synchronized.				    */
3998 /* ------------------------------------------------------------------------ */
3999 void fr_ifindexsync(ifp, newifp, ifs)
4000 void *ifp;
4001 void *newifp;
4002 ipf_stack_t *ifs;
4003 {
4004 	unsigned int	i;
4005 	frentry_t *rule_lists[8];
4006 	unsigned int	rules = sizeof (rule_lists) / sizeof (frentry_t *);
4007 
4008 	rule_lists[0] = ifs->ifs_ipacct[0][ifs->ifs_fr_active];
4009 	rule_lists[1] =	ifs->ifs_ipacct[1][ifs->ifs_fr_active];
4010 	rule_lists[2] =	ifs->ifs_ipfilter[0][ifs->ifs_fr_active];
4011 	rule_lists[3] =	ifs->ifs_ipfilter[1][ifs->ifs_fr_active];
4012 	rule_lists[4] =	ifs->ifs_ipacct6[0][ifs->ifs_fr_active];
4013 	rule_lists[5] =	ifs->ifs_ipacct6[1][ifs->ifs_fr_active];
4014 	rule_lists[6] =	ifs->ifs_ipfilter6[0][ifs->ifs_fr_active];
4015 	rule_lists[7] =	ifs->ifs_ipfilter6[1][ifs->ifs_fr_active];
4016 
4017 	for (i = 0; i < rules; i++) {
4018 		fr_syncindex(rule_lists[i], ifp, newifp);
4019 	}
4020 
4021 	/*
4022 	 * Update rule groups.
4023 	 */
4024 	for (i = 0; i < IPL_LOGSIZE; i++) {
4025 		frgroup_t *g;
4026 
4027 		for (g = ifs->ifs_ipfgroups[i][0]; g != NULL; g = g->fg_next)
4028 			fr_syncindex(g->fg_start, ifp, newifp);
4029 		for (g = ifs->ifs_ipfgroups[i][1]; g != NULL; g = g->fg_next)
4030 			fr_syncindex(g->fg_start, ifp, newifp);
4031 	}
4032 }
4033 #endif
4034 
4035 /*
4036  * In the functions below, bcopy() is called because the pointer being
4037  * copied _from_ in this instance is a pointer to a char buf (which could
4038  * end up being unaligned) and on the kernel's local stack.
4039  */
4040 /* ------------------------------------------------------------------------ */
4041 /* Function:    copyinptr                                                   */
4042 /* Returns:     int - 0 = success, else failure                             */
4043 /* Parameters:  src(I)  - pointer to the source address                     */
4044 /*              dst(I)  - destination address                               */
4045 /*              size(I) - number of bytes to copy                           */
4046 /*                                                                          */
4047 /* Copy a block of data in from user space, given a pointer to the pointer  */
4048 /* to start copying from (src) and a pointer to where to store it (dst).    */
4049 /* NB: src - pointer to user space pointer, dst - kernel space pointer      */
4050 /* ------------------------------------------------------------------------ */
4051 int copyinptr(src, dst, size)
4052 void *src, *dst;
4053 size_t size;
4054 {
4055 	caddr_t ca;
4056 	int err;
4057 
4058 # if SOLARIS
4059 	err = COPYIN(src, (caddr_t)&ca, sizeof(ca));
4060 	if (err != 0)
4061 		return err;
4062 # else
4063 	bcopy(src, (caddr_t)&ca, sizeof(ca));
4064 # endif
4065 	err = COPYIN(ca, dst, size);
4066 	return err;
4067 }
4068 
4069 
4070 /* ------------------------------------------------------------------------ */
4071 /* Function:    copyoutptr                                                  */
4072 /* Returns:     int - 0 = success, else failure                             */
4073 /* Parameters:  src(I)  - pointer to the source address                     */
4074 /*              dst(I)  - destination address                               */
4075 /*              size(I) - number of bytes to copy                           */
4076 /*                                                                          */
4077 /* Copy a block of data out to user space, given a pointer to the pointer   */
4078 /* to start copying from (src) and a pointer to where to store it (dst).    */
4079 /* NB: src - kernel space pointer, dst - pointer to user space pointer.     */
4080 /* ------------------------------------------------------------------------ */
4081 int copyoutptr(src, dst, size)
4082 void *src, *dst;
4083 size_t size;
4084 {
4085 	caddr_t ca;
4086 	int err;
4087 
4088 # if SOLARIS
4089 	err = COPYIN(dst, (caddr_t)&ca, sizeof(ca));
4090 	if (err != 0)
4091 		return err;
4092 # else
4093 	bcopy(dst, (caddr_t)&ca, sizeof(ca));
4094 # endif
4095 	err = COPYOUT(src, ca, size);
4096 	return err;
4097 }
4098 #endif
4099 
4100 
4101 /* ------------------------------------------------------------------------ */
4102 /* Function:    fr_lock                                                     */
4103 /* Returns:	int - 0 = success, else error				    */
4104 /* Parameters:  data(I)  - pointer to lock value to set                     */
4105 /*              lockp(O) - pointer to location to store old lock value      */
4106 /*                                                                          */
4107 /* Get the new value for the lock integer, set it and return the old value  */
4108 /* in *lockp.                                                               */
4109 /* ------------------------------------------------------------------------ */
4110 int fr_lock(data, lockp)
4111 caddr_t data;
4112 int *lockp;
4113 {
4114 	int arg, err;
4115 
4116 	err = BCOPYIN(data, (caddr_t)&arg, sizeof(arg));
4117 	if (err != 0)
4118 		return (EFAULT);
4119 	err = BCOPYOUT((caddr_t)lockp, data, sizeof(*lockp));
4120 	if (err != 0)
4121 		return (EFAULT);
4122 	*lockp = arg;
4123 	return (0);
4124 }
4125 
4126 
4127 /* ------------------------------------------------------------------------ */
4128 /* Function:    fr_getstat                                                  */
4129 /* Returns:     Nil                                                         */
4130 /* Parameters:  fiop(I)  - pointer to ipfilter stats structure              */
4131 /*                                                                          */
4132 /* Stores a copy of current pointers, counters, etc, in the friostat        */
4133 /* structure.                                                               */
4134 /* ------------------------------------------------------------------------ */
4135 void fr_getstat(fiop, ifs)
4136 friostat_t *fiop;
4137 ipf_stack_t *ifs;
4138 {
4139 	int i, j;
4140 
4141 	bcopy((char *)&ifs->ifs_frstats, (char *)fiop->f_st,
4142 	    sizeof(filterstats_t) * 2);
4143 	fiop->f_locks[IPL_LOGSTATE] = ifs->ifs_fr_state_lock;
4144 	fiop->f_locks[IPL_LOGNAT] = ifs->ifs_fr_nat_lock;
4145 	fiop->f_locks[IPL_LOGIPF] = ifs->ifs_fr_frag_lock;
4146 	fiop->f_locks[IPL_LOGAUTH] = ifs->ifs_fr_auth_lock;
4147 
4148 	for (i = 0; i < 2; i++)
4149 		for (j = 0; j < 2; j++) {
4150 			fiop->f_ipf[i][j] = ifs->ifs_ipfilter[i][j];
4151 			fiop->f_acct[i][j] = ifs->ifs_ipacct[i][j];
4152 			fiop->f_ipf6[i][j] = ifs->ifs_ipfilter6[i][j];
4153 			fiop->f_acct6[i][j] = ifs->ifs_ipacct6[i][j];
4154 		}
4155 
4156 	fiop->f_ticks = ifs->ifs_fr_ticks;
4157 	fiop->f_active = ifs->ifs_fr_active;
4158 	fiop->f_froute[0] = ifs->ifs_fr_frouteok[0];
4159 	fiop->f_froute[1] = ifs->ifs_fr_frouteok[1];
4160 
4161 	fiop->f_running = ifs->ifs_fr_running;
4162 	for (i = 0; i < IPL_LOGSIZE; i++) {
4163 		fiop->f_groups[i][0] = ifs->ifs_ipfgroups[i][0];
4164 		fiop->f_groups[i][1] = ifs->ifs_ipfgroups[i][1];
4165 	}
4166 #ifdef  IPFILTER_LOG
4167 	fiop->f_logging = 1;
4168 #else
4169 	fiop->f_logging = 0;
4170 #endif
4171 	fiop->f_defpass = ifs->ifs_fr_pass;
4172 	fiop->f_features = fr_features;
4173 	(void) strncpy(fiop->f_version, ipfilter_version,
4174 		       sizeof(fiop->f_version));
4175 }
4176 
4177 
4178 #ifdef	USE_INET6
4179 int icmptoicmp6types[ICMP_MAXTYPE+1] = {
4180 	ICMP6_ECHO_REPLY,	/* 0: ICMP_ECHOREPLY */
4181 	-1,			/* 1: UNUSED */
4182 	-1,			/* 2: UNUSED */
4183 	ICMP6_DST_UNREACH,	/* 3: ICMP_UNREACH */
4184 	-1,			/* 4: ICMP_SOURCEQUENCH */
4185 	ND_REDIRECT,		/* 5: ICMP_REDIRECT */
4186 	-1,			/* 6: UNUSED */
4187 	-1,			/* 7: UNUSED */
4188 	ICMP6_ECHO_REQUEST,	/* 8: ICMP_ECHO */
4189 	-1,			/* 9: UNUSED */
4190 	-1,			/* 10: UNUSED */
4191 	ICMP6_TIME_EXCEEDED,	/* 11: ICMP_TIMXCEED */
4192 	ICMP6_PARAM_PROB,	/* 12: ICMP_PARAMPROB */
4193 	-1,			/* 13: ICMP_TSTAMP */
4194 	-1,			/* 14: ICMP_TSTAMPREPLY */
4195 	-1,			/* 15: ICMP_IREQ */
4196 	-1,			/* 16: ICMP_IREQREPLY */
4197 	-1,			/* 17: ICMP_MASKREQ */
4198 	-1,			/* 18: ICMP_MASKREPLY */
4199 };
4200 
4201 
4202 int	icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
4203 	ICMP6_DST_UNREACH_ADDR,		/* 0: ICMP_UNREACH_NET */
4204 	ICMP6_DST_UNREACH_ADDR,		/* 1: ICMP_UNREACH_HOST */
4205 	-1,				/* 2: ICMP_UNREACH_PROTOCOL */
4206 	ICMP6_DST_UNREACH_NOPORT,	/* 3: ICMP_UNREACH_PORT */
4207 	-1,				/* 4: ICMP_UNREACH_NEEDFRAG */
4208 	ICMP6_DST_UNREACH_NOTNEIGHBOR,	/* 5: ICMP_UNREACH_SRCFAIL */
4209 	ICMP6_DST_UNREACH_ADDR,		/* 6: ICMP_UNREACH_NET_UNKNOWN */
4210 	ICMP6_DST_UNREACH_ADDR,		/* 7: ICMP_UNREACH_HOST_UNKNOWN */
4211 	-1,				/* 8: ICMP_UNREACH_ISOLATED */
4212 	ICMP6_DST_UNREACH_ADMIN,	/* 9: ICMP_UNREACH_NET_PROHIB */
4213 	ICMP6_DST_UNREACH_ADMIN,	/* 10: ICMP_UNREACH_HOST_PROHIB */
4214 	-1,				/* 11: ICMP_UNREACH_TOSNET */
4215 	-1,				/* 12: ICMP_UNREACH_TOSHOST */
4216 	ICMP6_DST_UNREACH_ADMIN,	/* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
4217 };
4218 int	icmpreplytype6[ICMP6_MAXTYPE + 1];
4219 #endif
4220 
4221 int	icmpreplytype4[ICMP_MAXTYPE + 1];
4222 
4223 
4224 /* ------------------------------------------------------------------------ */
4225 /* Function:    fr_matchicmpqueryreply                                      */
4226 /* Returns:     int - 1 if "icmp" is a valid reply to "ic" else 0.          */
4227 /* Parameters:  v(I)    - IP protocol version (4 or 6)                      */
4228 /*              ic(I)   - ICMP information                                  */
4229 /*              icmp(I) - ICMP packet header                                */
4230 /*              rev(I)  - direction (0 = forward/1 = reverse) of packet     */
4231 /*                                                                          */
4232 /* Check if the ICMP packet defined by the header pointed to by icmp is a   */
4233 /* reply to one as described by what's in ic.  If it is a match, return 1,  */
4234 /* else return 0 for no match.                                              */
4235 /* ------------------------------------------------------------------------ */
4236 int fr_matchicmpqueryreply(v, ic, icmp, rev)
4237 int v;
4238 icmpinfo_t *ic;
4239 icmphdr_t *icmp;
4240 int rev;
4241 {
4242 	int ictype;
4243 
4244 	ictype = ic->ici_type;
4245 
4246 	if (v == 4) {
4247 		/*
4248 		 * If we matched its type on the way in, then when going out
4249 		 * it will still be the same type.
4250 		 */
4251 		if ((!rev && (icmp->icmp_type == ictype)) ||
4252 		    (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) {
4253 			if (icmp->icmp_type != ICMP_ECHOREPLY)
4254 				return 1;
4255 			if (icmp->icmp_id == ic->ici_id)
4256 				return 1;
4257 		}
4258 	}
4259 #ifdef	USE_INET6
4260 	else if (v == 6) {
4261 		if ((!rev && (icmp->icmp_type == ictype)) ||
4262 		    (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) {
4263 			if (icmp->icmp_type != ICMP6_ECHO_REPLY)
4264 				return 1;
4265 			if (icmp->icmp_id == ic->ici_id)
4266 				return 1;
4267 		}
4268 	}
4269 #endif
4270 	return 0;
4271 }
4272 
4273 
4274 #ifdef	IPFILTER_LOOKUP
4275 /* ------------------------------------------------------------------------ */
4276 /* Function:    fr_resolvelookup                                            */
4277 /* Returns:     void * - NULL = failure, else success.                      */
4278 /* Parameters:  type(I)     - type of lookup these parameters are for.      */
4279 /*              number(I)   - table number to use when searching            */
4280 /*              funcptr(IO) - pointer to pointer for storing IP address     */
4281 /*			      searching function.			    */
4282 /*		ifs	    - ipf stack instance			    */
4283 /*                                                                          */
4284 /* Search for the "table" number passed in amongst those configured for     */
4285 /* that particular type.  If the type is recognised then the function to    */
4286 /* call to do the IP address search will be change, regardless of whether   */
4287 /* or not the "table" number exists.                                        */
4288 /* ------------------------------------------------------------------------ */
4289 static void *fr_resolvelookup(type, number, funcptr, ifs)
4290 u_int type, number;
4291 lookupfunc_t *funcptr;
4292 ipf_stack_t *ifs;
4293 {
4294 	char name[FR_GROUPLEN];
4295 	iphtable_t *iph;
4296 	ip_pool_t *ipo;
4297 	void *ptr;
4298 
4299 #if defined(SNPRINTF) && defined(_KERNEL)
4300 	(void) SNPRINTF(name, sizeof(name), "%u", number);
4301 #else
4302 	(void) sprintf(name, "%u", number);
4303 #endif
4304 
4305 	READ_ENTER(&ifs->ifs_ip_poolrw);
4306 
4307 	switch (type)
4308 	{
4309 	case IPLT_POOL :
4310 # if (defined(__osf__) && defined(_KERNEL))
4311 		ptr = NULL;
4312 		*funcptr = NULL;
4313 # else
4314 		ipo = ip_pool_find(IPL_LOGIPF, name, ifs);
4315 		ptr = ipo;
4316 		if (ipo != NULL) {
4317 			ATOMIC_INC32(ipo->ipo_ref);
4318 		}
4319 		*funcptr = ip_pool_search;
4320 # endif
4321 		break;
4322 	case IPLT_HASH :
4323 		iph = fr_findhtable(IPL_LOGIPF, name, ifs);
4324 		ptr = iph;
4325 		if (iph != NULL) {
4326 			ATOMIC_INC32(iph->iph_ref);
4327 		}
4328 		*funcptr = fr_iphmfindip;
4329 		break;
4330 	default:
4331 		ptr = NULL;
4332 		*funcptr = NULL;
4333 		break;
4334 	}
4335 	RWLOCK_EXIT(&ifs->ifs_ip_poolrw);
4336 
4337 	return ptr;
4338 }
4339 #endif
4340 
4341 
4342 /* ------------------------------------------------------------------------ */
4343 /* Function:    frrequest                                                   */
4344 /* Returns:     int - 0 == success, > 0 == errno value                      */
4345 /* Parameters:  unit(I)     - device for which this is for                  */
4346 /*              req(I)      - ioctl command (SIOC*)                         */
4347 /*              data(I)     - pointr to ioctl data                          */
4348 /*              set(I)      - 1 or 0 (filter set)                           */
4349 /*              makecopy(I) - flag indicating whether data points to a rule */
4350 /*                            in kernel space & hence doesn't need copying. */
4351 /*                                                                          */
4352 /* This function handles all the requests which operate on the list of      */
4353 /* filter rules.  This includes adding, deleting, insertion.  It is also    */
4354 /* responsible for creating groups when a "head" rule is loaded.  Interface */
4355 /* names are resolved here and other sanity checks are made on the content  */
4356 /* of the rule structure being loaded.  If a rule has user defined timeouts */
4357 /* then make sure they are created and initialised before exiting.          */
4358 /* ------------------------------------------------------------------------ */
4359 int frrequest(unit, req, data, set, makecopy, ifs)
4360 int unit;
4361 ioctlcmd_t req;
4362 int set, makecopy;
4363 caddr_t data;
4364 ipf_stack_t *ifs;
4365 {
4366 	frentry_t frd, *fp, *f, **fprev, **ftail;
4367 	int error = 0, in, v;
4368 	void *ptr, *uptr;
4369 	u_int *p, *pp;
4370 	frgroup_t *fg;
4371 	char *group;
4372 
4373 	fg = NULL;
4374 	fp = &frd;
4375 	if (makecopy != 0) {
4376 		error = fr_inobj(data, fp, IPFOBJ_FRENTRY);
4377 		if (error)
4378 			return EFAULT;
4379 		if ((fp->fr_flags & FR_T_BUILTIN) != 0)
4380 			return EINVAL;
4381 		fp->fr_ref = 0;
4382 		fp->fr_flags |= FR_COPIED;
4383 	} else {
4384 		fp = (frentry_t *)data;
4385 		if ((fp->fr_type & FR_T_BUILTIN) == 0)
4386 			return EINVAL;
4387 		fp->fr_flags &= ~FR_COPIED;
4388 	}
4389 
4390 	if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) ||
4391 	    ((fp->fr_dsize != 0) && (fp->fr_data == NULL)))
4392 		return EINVAL;
4393 
4394 	v = fp->fr_v;
4395 	uptr = fp->fr_data;
4396 
4397 	/*
4398 	 * Only filter rules for IPv4 or IPv6 are accepted.
4399 	 */
4400 	if (v == 4)
4401 		/*EMPTY*/;
4402 #ifdef	USE_INET6
4403 	else if (v == 6)
4404 		/*EMPTY*/;
4405 #endif
4406 	else {
4407 		return EINVAL;
4408 	}
4409 
4410 	/*
4411 	 * If the rule is being loaded from user space, i.e. we had to copy it
4412 	 * into kernel space, then do not trust the function pointer in the
4413 	 * rule.
4414 	 */
4415 	if ((makecopy == 1) && (fp->fr_func != NULL)) {
4416 		if (fr_findfunc(fp->fr_func) == NULL)
4417 			return ESRCH;
4418 		error = fr_funcinit(fp, ifs);
4419 		if (error != 0)
4420 			return error;
4421 	}
4422 
4423 	ptr = NULL;
4424 	/*
4425 	 * Check that the group number does exist and that its use (in/out)
4426 	 * matches what the rule is.
4427 	 */
4428 	if (!strncmp(fp->fr_grhead, "0", FR_GROUPLEN))
4429 		*fp->fr_grhead = '\0';
4430 	group = fp->fr_group;
4431 	if (!strncmp(group, "0", FR_GROUPLEN))
4432 		*group = '\0';
4433 
4434 	if (FR_ISACCOUNT(fp->fr_flags))
4435 		unit = IPL_LOGCOUNT;
4436 
4437 	if ((req != (int)SIOCZRLST) && (*group != '\0')) {
4438 		fg = fr_findgroup(group, unit, set, NULL, ifs);
4439 		if (fg == NULL)
4440 			return ESRCH;
4441 		if (fg->fg_flags == 0)
4442 			fg->fg_flags = fp->fr_flags & FR_INOUT;
4443 		else if (fg->fg_flags != (fp->fr_flags & FR_INOUT))
4444 			return ESRCH;
4445 	}
4446 
4447 	in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
4448 
4449 	/*
4450 	 * Work out which rule list this change is being applied to.
4451 	 */
4452 	ftail = NULL;
4453 	fprev = NULL;
4454 	if (unit == IPL_LOGAUTH)
4455 		fprev = &ifs->ifs_ipauth;
4456 	else if (v == 4) {
4457 		if (FR_ISACCOUNT(fp->fr_flags))
4458 			fprev = &ifs->ifs_ipacct[in][set];
4459 		else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
4460 			fprev = &ifs->ifs_ipfilter[in][set];
4461 	} else if (v == 6) {
4462 		if (FR_ISACCOUNT(fp->fr_flags))
4463 			fprev = &ifs->ifs_ipacct6[in][set];
4464 		else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
4465 			fprev = &ifs->ifs_ipfilter6[in][set];
4466 	}
4467 	if (fprev == NULL)
4468 		return ESRCH;
4469 
4470 	if (*group != '\0') {
4471 	    if (!fg && !(fg = fr_findgroup(group, unit, set, NULL, ifs)))
4472 			return ESRCH;
4473 		fprev = &fg->fg_start;
4474 	}
4475 
4476 	ftail = fprev;
4477 	for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) {
4478 		if (fp->fr_collect <= f->fr_collect) {
4479 			ftail = fprev;
4480 			f = NULL;
4481 			break;
4482 		}
4483 		fprev = ftail;
4484 	}
4485 
4486 	/*
4487 	 * Copy in extra data for the rule.
4488 	 */
4489 	if (fp->fr_dsize != 0) {
4490 		if (makecopy != 0) {
4491 			KMALLOCS(ptr, void *, fp->fr_dsize);
4492 			if (!ptr)
4493 				return ENOMEM;
4494 			error = COPYIN(uptr, ptr, fp->fr_dsize);
4495 		} else {
4496 			ptr = uptr;
4497 			error = 0;
4498 		}
4499 		if (error != 0) {
4500 			KFREES(ptr, fp->fr_dsize);
4501 			return EFAULT;
4502 		}
4503 		fp->fr_data = ptr;
4504 	} else
4505 		fp->fr_data = NULL;
4506 
4507 	/*
4508 	 * Perform per-rule type sanity checks of their members.
4509 	 */
4510 	switch (fp->fr_type & ~FR_T_BUILTIN)
4511 	{
4512 #if defined(IPFILTER_BPF)
4513 	case FR_T_BPFOPC :
4514 		if (fp->fr_dsize == 0)
4515 			return EINVAL;
4516 		if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) {
4517 			if (makecopy && fp->fr_data != NULL) {
4518 				KFREES(fp->fr_data, fp->fr_dsize);
4519 			}
4520 			return EINVAL;
4521 		}
4522 		break;
4523 #endif
4524 	case FR_T_IPF :
4525 		if (fp->fr_dsize != sizeof(fripf_t)) {
4526 			if (makecopy && fp->fr_data != NULL) {
4527 				KFREES(fp->fr_data, fp->fr_dsize);
4528 			}
4529 			return EINVAL;
4530 		}
4531 
4532 		/*
4533 		 * Allowing a rule with both "keep state" and "with oow" is
4534 		 * pointless because adding a state entry to the table will
4535 		 * fail with the out of window (oow) flag set.
4536 		 */
4537 		if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) {
4538 			if (makecopy && fp->fr_data != NULL) {
4539 				KFREES(fp->fr_data, fp->fr_dsize);
4540 			}
4541 			return EINVAL;
4542 		}
4543 
4544 		switch (fp->fr_satype)
4545 		{
4546 		case FRI_BROADCAST :
4547 		case FRI_DYNAMIC :
4548 		case FRI_NETWORK :
4549 		case FRI_NETMASKED :
4550 		case FRI_PEERADDR :
4551 			if (fp->fr_sifpidx < 0 || fp->fr_sifpidx > 3) {
4552 				if (makecopy && fp->fr_data != NULL) {
4553 					KFREES(fp->fr_data, fp->fr_dsize);
4554 				}
4555 				return EINVAL;
4556 			}
4557 			break;
4558 #ifdef	IPFILTER_LOOKUP
4559 		case FRI_LOOKUP :
4560 			fp->fr_srcptr = fr_resolvelookup(fp->fr_srctype,
4561 							 fp->fr_srcnum,
4562 							 &fp->fr_srcfunc, ifs);
4563 			break;
4564 #endif
4565 		default :
4566 			break;
4567 		}
4568 
4569 		switch (fp->fr_datype)
4570 		{
4571 		case FRI_BROADCAST :
4572 		case FRI_DYNAMIC :
4573 		case FRI_NETWORK :
4574 		case FRI_NETMASKED :
4575 		case FRI_PEERADDR :
4576 			if (fp->fr_difpidx < 0 || fp->fr_difpidx > 3) {
4577 				if (makecopy && fp->fr_data != NULL) {
4578 					KFREES(fp->fr_data, fp->fr_dsize);
4579 				}
4580 				return EINVAL;
4581 			}
4582 			break;
4583 #ifdef	IPFILTER_LOOKUP
4584 		case FRI_LOOKUP :
4585 			fp->fr_dstptr = fr_resolvelookup(fp->fr_dsttype,
4586 							 fp->fr_dstnum,
4587 							 &fp->fr_dstfunc, ifs);
4588 			break;
4589 #endif
4590 		default :
4591 			break;
4592 		}
4593 		break;
4594 	case FR_T_NONE :
4595 		break;
4596 	case FR_T_CALLFUNC :
4597 		break;
4598 	case FR_T_COMPIPF :
4599 		break;
4600 	default :
4601 		if (makecopy && fp->fr_data != NULL) {
4602 			KFREES(fp->fr_data, fp->fr_dsize);
4603 		}
4604 		return EINVAL;
4605 	}
4606 
4607 	/*
4608 	 * Lookup all the interface names that are part of the rule.
4609 	 */
4610 	frsynclist(0, 0, NULL, NULL, fp, ifs);
4611 	fp->fr_statecnt = 0;
4612 
4613 	/*
4614 	 * Look for an existing matching filter rule, but don't include the
4615 	 * next or interface pointer in the comparison (fr_next, fr_ifa).
4616 	 * This elminates rules which are indentical being loaded.  Checksum
4617 	 * the constant part of the filter rule to make comparisons quicker
4618 	 * (this meaning no pointers are included).
4619 	 */
4620 	for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum;
4621 	     p < pp; p++)
4622 		fp->fr_cksum += *p;
4623 	pp = (u_int *)(fp->fr_caddr + fp->fr_dsize);
4624 	for (p = (u_int *)fp->fr_data; p < pp; p++)
4625 		fp->fr_cksum += *p;
4626 
4627 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
4628 	bzero((char *)ifs->ifs_frcache, sizeof (ifs->ifs_frcache));
4629 
4630 	for (; (f = *ftail) != NULL; ftail = &f->fr_next) {
4631 		if ((fp->fr_cksum != f->fr_cksum) ||
4632 		    (f->fr_dsize != fp->fr_dsize))
4633 			continue;
4634 		if (bcmp((char *)&f->fr_func, (char *)&fp->fr_func, FR_CMPSIZ))
4635 			continue;
4636 		if ((!ptr && !f->fr_data) ||
4637 		    (ptr && f->fr_data &&
4638 		     !bcmp((char *)ptr, (char *)f->fr_data, f->fr_dsize)))
4639 			break;
4640 	}
4641 
4642 	/*
4643 	 * If zero'ing statistics, copy current to caller and zero.
4644 	 */
4645 	if (req == (ioctlcmd_t)SIOCZRLST) {
4646 		if (f == NULL)
4647 			error = ESRCH;
4648 		else {
4649 			/*
4650 			 * Copy and reduce lock because of impending copyout.
4651 			 * Well we should, but if we do then the atomicity of
4652 			 * this call and the correctness of fr_hits and
4653 			 * fr_bytes cannot be guaranteed.  As it is, this code
4654 			 * only resets them to 0 if they are successfully
4655 			 * copied out into user space.
4656 			 */
4657 			bcopy((char *)f, (char *)fp, sizeof(*f));
4658 
4659 			/*
4660 			 * When we copy this rule back out, set the data
4661 			 * pointer to be what it was in user space.
4662 			 */
4663 			fp->fr_data = uptr;
4664 			error = fr_outobj(data, fp, IPFOBJ_FRENTRY);
4665 
4666 			if (error == 0) {
4667 				if ((f->fr_dsize != 0) && (uptr != NULL))
4668 					error = COPYOUT(f->fr_data, uptr,
4669 							f->fr_dsize);
4670 				if (error == 0) {
4671 					f->fr_hits = 0;
4672 					f->fr_bytes = 0;
4673 				}
4674 			}
4675 		}
4676 
4677 		if ((ptr != NULL) && (makecopy != 0)) {
4678 			KFREES(ptr, fp->fr_dsize);
4679 		}
4680 		RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
4681 		return error;
4682 	}
4683 
4684 	if (!f) {
4685 		/*
4686 		 * At the end of this, ftail must point to the place where the
4687 		 * new rule is to be saved/inserted/added.
4688 		 * For SIOCAD*FR, this should be the last rule in the group of
4689 		 * rules that have equal fr_collect fields.
4690 		 * For SIOCIN*FR, ...
4691 		 */
4692 		if (req == (ioctlcmd_t)SIOCADAFR ||
4693 		    req == (ioctlcmd_t)SIOCADIFR) {
4694 
4695 			for (ftail = fprev; (f = *ftail) != NULL; ) {
4696 				if (f->fr_collect > fp->fr_collect)
4697 					break;
4698 				ftail = &f->fr_next;
4699 			}
4700 			f = NULL;
4701 			ptr = NULL;
4702 			error = 0;
4703 		} else if (req == (ioctlcmd_t)SIOCINAFR ||
4704 			   req == (ioctlcmd_t)SIOCINIFR) {
4705 			while ((f = *fprev) != NULL) {
4706 				if (f->fr_collect >= fp->fr_collect)
4707 					break;
4708 				fprev = &f->fr_next;
4709 			}
4710 			ftail = fprev;
4711 			if (fp->fr_hits != 0) {
4712 				while (fp->fr_hits && (f = *ftail)) {
4713 					if (f->fr_collect != fp->fr_collect)
4714 						break;
4715 					fprev = ftail;
4716 					ftail = &f->fr_next;
4717 					fp->fr_hits--;
4718 				}
4719 			}
4720 			f = NULL;
4721 			ptr = NULL;
4722 			error = 0;
4723 		}
4724 	}
4725 
4726 	/*
4727 	 * Request to remove a rule.
4728 	 */
4729 	if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR) {
4730 		if (!f)
4731 			error = ESRCH;
4732 		else {
4733 			/*
4734 			 * Do not allow activity from user space to interfere
4735 			 * with rules not loaded that way.
4736 			 */
4737 			if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) {
4738 				error = EPERM;
4739 				goto done;
4740 			}
4741 
4742 			/*
4743 			 * Return EBUSY if the rule is being reference by
4744 			 * something else (eg state information.
4745 			 */
4746 			if (f->fr_ref > 1) {
4747 				error = EBUSY;
4748 				goto done;
4749 			}
4750 #ifdef	IPFILTER_SCAN
4751 			if (f->fr_isctag[0] != '\0' &&
4752 			    (f->fr_isc != (struct ipscan *)-1))
4753 				ipsc_detachfr(f);
4754 #endif
4755 			if (unit == IPL_LOGAUTH) {
4756 				error = fr_preauthcmd(req, f, ftail, ifs);
4757 				goto done;
4758 			}
4759 			if (*f->fr_grhead != '\0')
4760 				fr_delgroup(f->fr_grhead, unit, set, ifs);
4761 			fr_fixskip(ftail, f, -1);
4762 			*ftail = f->fr_next;
4763 			f->fr_next = NULL;
4764 			(void)fr_derefrule(&f, ifs);
4765 		}
4766 	} else {
4767 		/*
4768 		 * Not removing, so we must be adding/inserting a rule.
4769 		 */
4770 		if (f)
4771 			error = EEXIST;
4772 		else {
4773 			if (unit == IPL_LOGAUTH) {
4774 				error = fr_preauthcmd(req, fp, ftail, ifs);
4775 				goto done;
4776 			}
4777 			if (makecopy) {
4778 				KMALLOC(f, frentry_t *);
4779 			} else
4780 				f = fp;
4781 			if (f != NULL) {
4782 				if (fp != f)
4783 					bcopy((char *)fp, (char *)f,
4784 					      sizeof(*f));
4785 				MUTEX_NUKE(&f->fr_lock);
4786 				MUTEX_INIT(&f->fr_lock, "filter rule lock");
4787 #ifdef	IPFILTER_SCAN
4788 				if (f->fr_isctag[0] != '\0' &&
4789 				    ipsc_attachfr(f))
4790 					f->fr_isc = (struct ipscan *)-1;
4791 #endif
4792 				f->fr_hits = 0;
4793 				if (makecopy != 0)
4794 					f->fr_ref = 1;
4795 				f->fr_next = *ftail;
4796 				*ftail = f;
4797 				if (req == (ioctlcmd_t)SIOCINIFR ||
4798 				    req == (ioctlcmd_t)SIOCINAFR)
4799 					fr_fixskip(ftail, f, 1);
4800 				f->fr_grp = NULL;
4801 				group = f->fr_grhead;
4802 				if (*group != '\0') {
4803 					fg = fr_addgroup(group, f, f->fr_flags,
4804 							 unit, set, ifs);
4805 					if (fg != NULL)
4806 						f->fr_grp = &fg->fg_start;
4807 				}
4808 			} else
4809 				error = ENOMEM;
4810 		}
4811 	}
4812 done:
4813 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
4814 	if ((ptr != NULL) && (error != 0) && (makecopy != 0)) {
4815 		KFREES(ptr, fp->fr_dsize);
4816 	}
4817 	return (error);
4818 }
4819 
4820 
4821 /* ------------------------------------------------------------------------ */
4822 /* Function:    fr_funcinit                                                 */
4823 /* Returns:     int - 0 == success, else ESRCH: cannot resolve rule details */
4824 /* Parameters:  fr(I) - pointer to filter rule                              */
4825 /*                                                                          */
4826 /* If a rule is a call rule, then check if the function it points to needs  */
4827 /* an init function to be called now the rule has been loaded.              */
4828 /* ------------------------------------------------------------------------ */
4829 static int fr_funcinit(fr, ifs)
4830 frentry_t *fr;
4831 ipf_stack_t *ifs;
4832 {
4833 	ipfunc_resolve_t *ft;
4834 	int err;
4835 
4836 	err = ESRCH;
4837 
4838 	for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
4839 		if (ft->ipfu_addr == fr->fr_func) {
4840 			err = 0;
4841 			if (ft->ipfu_init != NULL)
4842 				err = (*ft->ipfu_init)(fr, ifs);
4843 			break;
4844 		}
4845 	return err;
4846 }
4847 
4848 
4849 /* ------------------------------------------------------------------------ */
4850 /* Function:    fr_findfunc                                                 */
4851 /* Returns:     ipfunc_t - pointer to function if found, else NULL          */
4852 /* Parameters:  funcptr(I) - function pointer to lookup                     */
4853 /*                                                                          */
4854 /* Look for a function in the table of known functions.                     */
4855 /* ------------------------------------------------------------------------ */
4856 static ipfunc_t fr_findfunc(funcptr)
4857 ipfunc_t funcptr;
4858 {
4859 	ipfunc_resolve_t *ft;
4860 
4861 	for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
4862 		if (ft->ipfu_addr == funcptr)
4863 			return funcptr;
4864 	return NULL;
4865 }
4866 
4867 
4868 /* ------------------------------------------------------------------------ */
4869 /* Function:    fr_resolvefunc                                              */
4870 /* Returns:     int - 0 == success, else error                              */
4871 /* Parameters:  data(IO) - ioctl data pointer to ipfunc_resolve_t struct    */
4872 /*                                                                          */
4873 /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */
4874 /* This will either be the function name (if the pointer is set) or the     */
4875 /* function pointer if the name is set.  When found, fill in the other one  */
4876 /* so that the entire, complete, structure can be copied back to user space.*/
4877 /* ------------------------------------------------------------------------ */
4878 int fr_resolvefunc(data)
4879 void *data;
4880 {
4881 	ipfunc_resolve_t res, *ft;
4882 	int err;
4883 
4884 	err = BCOPYIN(data, &res, sizeof(res));
4885 	if (err != 0)
4886 		return EFAULT;
4887 
4888 	if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') {
4889 		for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
4890 			if (strncmp(res.ipfu_name, ft->ipfu_name,
4891 				    sizeof(res.ipfu_name)) == 0) {
4892 				res.ipfu_addr = ft->ipfu_addr;
4893 				res.ipfu_init = ft->ipfu_init;
4894 				if (COPYOUT(&res, data, sizeof(res)) != 0)
4895 					return EFAULT;
4896 				return 0;
4897 			}
4898 	}
4899 	if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') {
4900 		for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
4901 			if (ft->ipfu_addr == res.ipfu_addr) {
4902 				(void) strncpy(res.ipfu_name, ft->ipfu_name,
4903 					       sizeof(res.ipfu_name));
4904 				res.ipfu_init = ft->ipfu_init;
4905 				if (COPYOUT(&res, data, sizeof(res)) != 0)
4906 					return EFAULT;
4907 				return 0;
4908 			}
4909 	}
4910 	return ESRCH;
4911 }
4912 
4913 
4914 #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__FreeBSD__)) || \
4915     (defined(__FreeBSD__) && (__FreeBSD_version < 490000)) || \
4916     (defined(__NetBSD__) && (__NetBSD_Version__ < 105000000)) || \
4917     (defined(__OpenBSD__) && (OpenBSD < 200006))
4918 /*
4919  * From: NetBSD
4920  * ppsratecheck(): packets (or events) per second limitation.
4921  */
4922 int
4923 ppsratecheck(lasttime, curpps, maxpps)
4924 	struct timeval *lasttime;
4925 	int *curpps;
4926 	int maxpps;	/* maximum pps allowed */
4927 {
4928 	struct timeval tv, delta;
4929 	int rv;
4930 
4931 	GETKTIME(&tv);
4932 
4933 	delta.tv_sec = tv.tv_sec - lasttime->tv_sec;
4934 	delta.tv_usec = tv.tv_usec - lasttime->tv_usec;
4935 	if (delta.tv_usec < 0) {
4936 		delta.tv_sec--;
4937 		delta.tv_usec += 1000000;
4938 	}
4939 
4940 	/*
4941 	 * check for 0,0 is so that the message will be seen at least once.
4942 	 * if more than one second have passed since the last update of
4943 	 * lasttime, reset the counter.
4944 	 *
4945 	 * we do increment *curpps even in *curpps < maxpps case, as some may
4946 	 * try to use *curpps for stat purposes as well.
4947 	 */
4948 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
4949 	    delta.tv_sec >= 1) {
4950 		*lasttime = tv;
4951 		*curpps = 0;
4952 		rv = 1;
4953 	} else if (maxpps < 0)
4954 		rv = 1;
4955 	else if (*curpps < maxpps)
4956 		rv = 1;
4957 	else
4958 		rv = 0;
4959 	*curpps = *curpps + 1;
4960 
4961 	return (rv);
4962 }
4963 #endif
4964 
4965 
4966 /* ------------------------------------------------------------------------ */
4967 /* Function:    fr_derefrule                                                */
4968 /* Returns:     int   - 0 == rule freed up, else rule not freed             */
4969 /* Parameters:  fr(I) - pointer to filter rule                              */
4970 /*                                                                          */
4971 /* Decrement the reference counter to a rule by one.  If it reaches zero,   */
4972 /* free it and any associated storage space being used by it.               */
4973 /* ------------------------------------------------------------------------ */
4974 int fr_derefrule(frp, ifs)
4975 frentry_t **frp;
4976 ipf_stack_t *ifs;
4977 {
4978 	frentry_t *fr;
4979 
4980 	fr = *frp;
4981 
4982 	MUTEX_ENTER(&fr->fr_lock);
4983 	fr->fr_ref--;
4984 	if (fr->fr_ref == 0) {
4985 		MUTEX_EXIT(&fr->fr_lock);
4986 		MUTEX_DESTROY(&fr->fr_lock);
4987 
4988 #ifdef IPFILTER_LOOKUP
4989 		if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP)
4990 		    ip_lookup_deref(fr->fr_srctype, fr->fr_srcptr, ifs);
4991 		if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP)
4992 		    ip_lookup_deref(fr->fr_dsttype, fr->fr_dstptr, ifs);
4993 #endif
4994 
4995 		if (fr->fr_dsize) {
4996 			KFREES(fr->fr_data, fr->fr_dsize);
4997 		}
4998 		if ((fr->fr_flags & FR_COPIED) != 0) {
4999 			KFREE(fr);
5000 			return 0;
5001 		}
5002 		return 1;
5003 	} else {
5004 		MUTEX_EXIT(&fr->fr_lock);
5005 	}
5006 	*frp = NULL;
5007 	return -1;
5008 }
5009 
5010 
5011 #ifdef	IPFILTER_LOOKUP
5012 /* ------------------------------------------------------------------------ */
5013 /* Function:    fr_grpmapinit                                               */
5014 /* Returns:     int - 0 == success, else ESRCH because table entry not found*/
5015 /* Parameters:  fr(I) - pointer to rule to find hash table for              */
5016 /*                                                                          */
5017 /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr.  */
5018 /* fr_ptr is later used by fr_srcgrpmap and fr_dstgrpmap.                   */
5019 /* ------------------------------------------------------------------------ */
5020 static int fr_grpmapinit(fr, ifs)
5021 frentry_t *fr;
5022 ipf_stack_t *ifs;
5023 {
5024 	char name[FR_GROUPLEN];
5025 	iphtable_t *iph;
5026 
5027 #if defined(SNPRINTF) && defined(_KERNEL)
5028 	(void) SNPRINTF(name, sizeof(name), "%d", fr->fr_arg);
5029 #else
5030 	(void) sprintf(name, "%d", fr->fr_arg);
5031 #endif
5032 	iph = fr_findhtable(IPL_LOGIPF, name, ifs);
5033 	if (iph == NULL)
5034 		return ESRCH;
5035 	if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT))
5036 		return ESRCH;
5037 	fr->fr_ptr = iph;
5038 	return 0;
5039 }
5040 
5041 
5042 /* ------------------------------------------------------------------------ */
5043 /* Function:    fr_srcgrpmap                                                */
5044 /* Returns:     frentry_t * - pointer to "new last matching" rule or NULL   */
5045 /* Parameters:  fin(I)    - pointer to packet information                   */
5046 /*              passp(IO) - pointer to current/new filter decision (unused) */
5047 /*                                                                          */
5048 /* Look for a rule group head in a hash table, using the source address as  */
5049 /* the key, and descend into that group and continue matching rules against */
5050 /* the packet.                                                              */
5051 /* ------------------------------------------------------------------------ */
5052 frentry_t *fr_srcgrpmap(fin, passp)
5053 fr_info_t *fin;
5054 u_32_t *passp;
5055 {
5056 	frgroup_t *fg;
5057 	void *rval;
5058 	ipf_stack_t *ifs = fin->fin_ifs;
5059 
5060 	rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, fin->fin_v, &fin->fin_src, ifs);
5061 	if (rval == NULL)
5062 		return NULL;
5063 
5064 	fg = rval;
5065 	fin->fin_fr = fg->fg_start;
5066 	(void) fr_scanlist(fin, *passp);
5067 	return fin->fin_fr;
5068 }
5069 
5070 
5071 /* ------------------------------------------------------------------------ */
5072 /* Function:    fr_dstgrpmap                                                */
5073 /* Returns:     frentry_t * - pointer to "new last matching" rule or NULL   */
5074 /* Parameters:  fin(I)    - pointer to packet information                   */
5075 /*              passp(IO) - pointer to current/new filter decision (unused) */
5076 /*                                                                          */
5077 /* Look for a rule group head in a hash table, using the destination        */
5078 /* address as the key, and descend into that group and continue matching    */
5079 /* rules against  the packet.                                               */
5080 /* ------------------------------------------------------------------------ */
5081 frentry_t *fr_dstgrpmap(fin, passp)
5082 fr_info_t *fin;
5083 u_32_t *passp;
5084 {
5085 	frgroup_t *fg;
5086 	void *rval;
5087 	ipf_stack_t *ifs = fin->fin_ifs;
5088 
5089 	rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, fin->fin_v, &fin->fin_dst, ifs);
5090 	if (rval == NULL)
5091 		return NULL;
5092 
5093 	fg = rval;
5094 	fin->fin_fr = fg->fg_start;
5095 	(void) fr_scanlist(fin, *passp);
5096 	return fin->fin_fr;
5097 }
5098 #endif /* IPFILTER_LOOKUP */
5099 
5100 /*
5101  * Queue functions
5102  * ===============
5103  * These functions manage objects on queues for efficient timeouts.  There are
5104  * a number of system defined queues as well as user defined timeouts.  It is
5105  * expected that a lock is held in the domain in which the queue belongs
5106  * (i.e. either state or NAT) when calling any of these functions that prevents
5107  * fr_freetimeoutqueue() from being called at the same time as any other.
5108  */
5109 
5110 
5111 /* ------------------------------------------------------------------------ */
5112 /* Function:    fr_addtimeoutqueue                                          */
5113 /* Returns:     struct ifqtq * - NULL if malloc fails, else pointer to      */
5114 /*                               timeout queue with given interval.         */
5115 /* Parameters:  parent(I)  - pointer to pointer to parent node of this list */
5116 /*                           of interface queues.                           */
5117 /*              seconds(I) - timeout value in seconds for this queue.       */
5118 /*                                                                          */
5119 /* This routine first looks for a timeout queue that matches the interval   */
5120 /* being requested.  If it finds one, increments the reference counter and  */
5121 /* returns a pointer to it.  If none are found, it allocates a new one and  */
5122 /* inserts it at the top of the list.                                       */
5123 /*                                                                          */
5124 /* Locking.                                                                 */
5125 /* It is assumed that the caller of this function has an appropriate lock   */
5126 /* held (exclusively) in the domain that encompases 'parent'.               */
5127 /* ------------------------------------------------------------------------ */
5128 ipftq_t *fr_addtimeoutqueue(parent, seconds, ifs)
5129 ipftq_t **parent;
5130 u_int seconds;
5131 ipf_stack_t *ifs;
5132 {
5133 	ipftq_t *ifq;
5134 	u_int period;
5135 
5136 	period = seconds * IPF_HZ_DIVIDE;
5137 
5138 	MUTEX_ENTER(&ifs->ifs_ipf_timeoutlock);
5139 	for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) {
5140 		if (ifq->ifq_ttl == period) {
5141 			/*
5142 			 * Reset the delete flag, if set, so the structure
5143 			 * gets reused rather than freed and reallocated.
5144 			 */
5145 			MUTEX_ENTER(&ifq->ifq_lock);
5146 			ifq->ifq_flags &= ~IFQF_DELETE;
5147 			ifq->ifq_ref++;
5148 			MUTEX_EXIT(&ifq->ifq_lock);
5149 			MUTEX_EXIT(&ifs->ifs_ipf_timeoutlock);
5150 
5151 			return ifq;
5152 		}
5153 	}
5154 
5155 	KMALLOC(ifq, ipftq_t *);
5156 	if (ifq != NULL) {
5157 		ifq->ifq_ttl = period;
5158 		ifq->ifq_head = NULL;
5159 		ifq->ifq_tail = &ifq->ifq_head;
5160 		ifq->ifq_next = *parent;
5161 		ifq->ifq_pnext = parent;
5162 		ifq->ifq_ref = 1;
5163 		ifq->ifq_flags = IFQF_USER;
5164 		*parent = ifq;
5165 		ifs->ifs_fr_userifqs++;
5166 		MUTEX_NUKE(&ifq->ifq_lock);
5167 		MUTEX_INIT(&ifq->ifq_lock, "ipftq mutex");
5168 	}
5169 	MUTEX_EXIT(&ifs->ifs_ipf_timeoutlock);
5170 	return ifq;
5171 }
5172 
5173 
5174 /* ------------------------------------------------------------------------ */
5175 /* Function:    fr_deletetimeoutqueue                                       */
5176 /* Returns:     int    - new reference count value of the timeout queue     */
5177 /* Parameters:  ifq(I) - timeout queue which is losing a reference.         */
5178 /* Locks:       ifq->ifq_lock                                               */
5179 /*                                                                          */
5180 /* This routine must be called when we're discarding a pointer to a timeout */
5181 /* queue object, taking care of the reference counter.                      */
5182 /*                                                                          */
5183 /* Now that this just sets a DELETE flag, it requires the expire code to    */
5184 /* check the list of user defined timeout queues and call the free function */
5185 /* below (currently commented out) to stop memory leaking.  It is done this */
5186 /* way because the locking may not be sufficient to safely do a free when   */
5187 /* this function is called.                                                 */
5188 /* ------------------------------------------------------------------------ */
5189 int fr_deletetimeoutqueue(ifq)
5190 ipftq_t *ifq;
5191 {
5192 
5193 	ifq->ifq_ref--;
5194 	if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) {
5195 		ifq->ifq_flags |= IFQF_DELETE;
5196 	}
5197 
5198 	return ifq->ifq_ref;
5199 }
5200 
5201 
5202 /* ------------------------------------------------------------------------ */
5203 /* Function:    fr_freetimeoutqueue                                         */
5204 /* Parameters:  ifq(I) - timeout queue which is losing a reference.         */
5205 /* Returns:     Nil                                                         */
5206 /*                                                                          */
5207 /* Locking:                                                                 */
5208 /* It is assumed that the caller of this function has an appropriate lock   */
5209 /* held (exclusively) in the domain that encompases the callers "domain".   */
5210 /* The ifq_lock for this structure should not be held.                      */
5211 /*                                                                          */
5212 /* Remove a user definde timeout queue from the list of queues it is in and */
5213 /* tidy up after this is done.                                              */
5214 /* ------------------------------------------------------------------------ */
5215 void fr_freetimeoutqueue(ifq, ifs)
5216 ipftq_t *ifq;
5217 ipf_stack_t *ifs;
5218 {
5219 
5220 
5221 	if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) ||
5222 	    ((ifq->ifq_flags & IFQF_USER) == 0)) {
5223 		printf("fr_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n",
5224 		       (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl,
5225 		       ifq->ifq_ref);
5226 		return;
5227 	}
5228 
5229 	/*
5230 	 * Remove from its position in the list.
5231 	 */
5232 	*ifq->ifq_pnext = ifq->ifq_next;
5233 	if (ifq->ifq_next != NULL)
5234 		ifq->ifq_next->ifq_pnext = ifq->ifq_pnext;
5235 
5236 	MUTEX_DESTROY(&ifq->ifq_lock);
5237 	ifs->ifs_fr_userifqs--;
5238 	KFREE(ifq);
5239 }
5240 
5241 
5242 /* ------------------------------------------------------------------------ */
5243 /* Function:    fr_deletequeueentry                                         */
5244 /* Returns:     Nil                                                         */
5245 /* Parameters:  tqe(I) - timeout queue entry to delete                      */
5246 /*              ifq(I) - timeout queue to remove entry from                 */
5247 /*                                                                          */
5248 /* Remove a tail queue entry from its queue and make it an orphan.          */
5249 /* fr_deletetimeoutqueue is called to make sure the reference count on the  */
5250 /* queue is correct.  We can't, however, call fr_freetimeoutqueue because   */
5251 /* the correct lock(s) may not be held that would make it safe to do so.    */
5252 /* ------------------------------------------------------------------------ */
5253 void fr_deletequeueentry(tqe)
5254 ipftqent_t *tqe;
5255 {
5256 	ipftq_t *ifq;
5257 
5258 	ifq = tqe->tqe_ifq;
5259 	if (ifq == NULL)
5260 		return;
5261 
5262 	MUTEX_ENTER(&ifq->ifq_lock);
5263 
5264 	if (tqe->tqe_pnext != NULL) {
5265 		*tqe->tqe_pnext = tqe->tqe_next;
5266 		if (tqe->tqe_next != NULL)
5267 			tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5268 		else    /* we must be the tail anyway */
5269 			ifq->ifq_tail = tqe->tqe_pnext;
5270 
5271 		tqe->tqe_pnext = NULL;
5272 		tqe->tqe_ifq = NULL;
5273 	}
5274 
5275 	(void) fr_deletetimeoutqueue(ifq);
5276 
5277 	MUTEX_EXIT(&ifq->ifq_lock);
5278 }
5279 
5280 
5281 /* ------------------------------------------------------------------------ */
5282 /* Function:    fr_queuefront                                               */
5283 /* Returns:     Nil                                                         */
5284 /* Parameters:  tqe(I) - pointer to timeout queue entry                     */
5285 /*                                                                          */
5286 /* Move a queue entry to the front of the queue, if it isn't already there. */
5287 /* ------------------------------------------------------------------------ */
5288 void fr_queuefront(tqe)
5289 ipftqent_t *tqe;
5290 {
5291 	ipftq_t *ifq;
5292 
5293 	ifq = tqe->tqe_ifq;
5294 	if (ifq == NULL)
5295 		return;
5296 
5297 	MUTEX_ENTER(&ifq->ifq_lock);
5298 	if (ifq->ifq_head != tqe) {
5299 		*tqe->tqe_pnext = tqe->tqe_next;
5300 		if (tqe->tqe_next)
5301 			tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5302 		else
5303 			ifq->ifq_tail = tqe->tqe_pnext;
5304 
5305 		tqe->tqe_next = ifq->ifq_head;
5306 		ifq->ifq_head->tqe_pnext = &tqe->tqe_next;
5307 		ifq->ifq_head = tqe;
5308 		tqe->tqe_pnext = &ifq->ifq_head;
5309 	}
5310 	MUTEX_EXIT(&ifq->ifq_lock);
5311 }
5312 
5313 
5314 /* ------------------------------------------------------------------------ */
5315 /* Function:    fr_queueback                                                */
5316 /* Returns:     Nil                                                         */
5317 /* Parameters:  tqe(I) - pointer to timeout queue entry                     */
5318 /*                                                                          */
5319 /* Move a queue entry to the back of the queue, if it isn't already there.  */
5320 /* ------------------------------------------------------------------------ */
5321 void fr_queueback(tqe, ifs)
5322 ipftqent_t *tqe;
5323 ipf_stack_t *ifs;
5324 {
5325 	ipftq_t *ifq;
5326 
5327 	ifq = tqe->tqe_ifq;
5328 	if (ifq == NULL)
5329 		return;
5330 	tqe->tqe_die = ifs->ifs_fr_ticks + ifq->ifq_ttl;
5331 
5332 	MUTEX_ENTER(&ifq->ifq_lock);
5333 	if (tqe->tqe_next == NULL) {		/* at the end already ? */
5334 		MUTEX_EXIT(&ifq->ifq_lock);
5335 		return;
5336 	}
5337 
5338 	/*
5339 	 * Remove from list
5340 	 */
5341 	*tqe->tqe_pnext = tqe->tqe_next;
5342 	tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5343 
5344 	/*
5345 	 * Make it the last entry.
5346 	 */
5347 	tqe->tqe_next = NULL;
5348 	tqe->tqe_pnext = ifq->ifq_tail;
5349 	*ifq->ifq_tail = tqe;
5350 	ifq->ifq_tail = &tqe->tqe_next;
5351 	MUTEX_EXIT(&ifq->ifq_lock);
5352 }
5353 
5354 
5355 /* ------------------------------------------------------------------------ */
5356 /* Function:    fr_queueappend                                              */
5357 /* Returns:     Nil                                                         */
5358 /* Parameters:  tqe(I)    - pointer to timeout queue entry                  */
5359 /*              ifq(I)    - pointer to timeout queue                        */
5360 /*              parent(I) - owing object pointer                            */
5361 /*                                                                          */
5362 /* Add a new item to this queue and put it on the very end.                 */
5363 /* ------------------------------------------------------------------------ */
5364 void fr_queueappend(tqe, ifq, parent, ifs)
5365 ipftqent_t *tqe;
5366 ipftq_t *ifq;
5367 void *parent;
5368 ipf_stack_t *ifs;
5369 {
5370 
5371 	MUTEX_ENTER(&ifq->ifq_lock);
5372 	tqe->tqe_parent = parent;
5373 	tqe->tqe_pnext = ifq->ifq_tail;
5374 	*ifq->ifq_tail = tqe;
5375 	ifq->ifq_tail = &tqe->tqe_next;
5376 	tqe->tqe_next = NULL;
5377 	tqe->tqe_ifq = ifq;
5378 	tqe->tqe_die = ifs->ifs_fr_ticks + ifq->ifq_ttl;
5379 	ifq->ifq_ref++;
5380 	MUTEX_EXIT(&ifq->ifq_lock);
5381 }
5382 
5383 
5384 /* ------------------------------------------------------------------------ */
5385 /* Function:    fr_movequeue                                                */
5386 /* Returns:     Nil                                                         */
5387 /* Parameters:  tq(I)   - pointer to timeout queue information              */
5388 /*              oifp(I) - old timeout queue entry was on                    */
5389 /*              nifp(I) - new timeout queue to put entry on                 */
5390 /*		ifs	- ipf stack instance				    */
5391 /*                                                                          */
5392 /* Move a queue entry from one timeout queue to another timeout queue.      */
5393 /* If it notices that the current entry is already last and does not need   */
5394 /* to move queue, the return.                                               */
5395 /* ------------------------------------------------------------------------ */
5396 void fr_movequeue(tqe, oifq, nifq, ifs)
5397 ipftqent_t *tqe;
5398 ipftq_t *oifq, *nifq;
5399 ipf_stack_t *ifs;
5400 {
5401 	/*
5402 	 * If the queue isn't changing, and the clock hasn't ticked
5403 	 * since the last update, the operation will be a no-op.
5404 	 */
5405 	if (oifq == nifq && tqe->tqe_touched == ifs->ifs_fr_ticks)
5406 		return;
5407 
5408 	/*
5409 	 * Grab the lock and update the timers.
5410 	 */
5411 	MUTEX_ENTER(&oifq->ifq_lock);
5412 	tqe->tqe_touched = ifs->ifs_fr_ticks;
5413 	tqe->tqe_die = ifs->ifs_fr_ticks + nifq->ifq_ttl;
5414 
5415 	/*
5416 	 * The remainder of the operation can still be a no-op.
5417 	 *
5418 	 * If the queue isn't changing, check to see if
5419 	 * an update would be meaningless.
5420 	 */
5421 	if (oifq == nifq) {
5422 		if ((tqe->tqe_next == NULL) ||
5423 		    (tqe->tqe_next->tqe_die == tqe->tqe_die)) {
5424 			MUTEX_EXIT(&oifq->ifq_lock);
5425 			return;
5426 		}
5427 	}
5428 
5429 	/*
5430 	 * Remove from the old queue
5431 	 */
5432 	*tqe->tqe_pnext = tqe->tqe_next;
5433 	if (tqe->tqe_next)
5434 		tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5435 	else
5436 		oifq->ifq_tail = tqe->tqe_pnext;
5437 	tqe->tqe_next = NULL;
5438 
5439 	/*
5440 	 * If we're moving from one queue to another, release the lock on the
5441 	 * old queue and get a lock on the new queue.  For user defined queues,
5442 	 * if we're moving off it, call delete in case it can now be freed.
5443 	 */
5444 	if (oifq != nifq) {
5445 		tqe->tqe_ifq = NULL;
5446 
5447 		(void) fr_deletetimeoutqueue(oifq);
5448 
5449 		MUTEX_EXIT(&oifq->ifq_lock);
5450 
5451 		MUTEX_ENTER(&nifq->ifq_lock);
5452 
5453 		tqe->tqe_ifq = nifq;
5454 		nifq->ifq_ref++;
5455 	}
5456 
5457 	/*
5458 	 * Add to the bottom of the new queue
5459 	 */
5460 	tqe->tqe_pnext = nifq->ifq_tail;
5461 	*nifq->ifq_tail = tqe;
5462 	nifq->ifq_tail = &tqe->tqe_next;
5463 	MUTEX_EXIT(&nifq->ifq_lock);
5464 }
5465 
5466 
5467 /* ------------------------------------------------------------------------ */
5468 /* Function:    fr_updateipid                                               */
5469 /* Returns:     int - 0 == success, -1 == error (packet should be droppped) */
5470 /* Parameters:  fin(I) - pointer to packet information                      */
5471 /*                                                                          */
5472 /* When we are doing NAT, change the IP of every packet to represent a      */
5473 /* single sequence of packets coming from the host, hiding any host         */
5474 /* specific sequencing that might otherwise be revealed.  If the packet is  */
5475 /* a fragment, then store the 'new' IPid in the fragment cache and look up  */
5476 /* the fragment cache for non-leading fragments.  If a non-leading fragment */
5477 /* has no match in the cache, return an error.                              */
5478 /* ------------------------------------------------------------------------ */
5479 static INLINE int fr_updateipid(fin)
5480 fr_info_t *fin;
5481 {
5482 	u_short id, ido, sums;
5483 	u_32_t sumd, sum;
5484 	ip_t *ip;
5485 
5486 	if (fin->fin_off != 0) {
5487 		sum = fr_ipid_knownfrag(fin);
5488 		if (sum == 0xffffffff)
5489 			return -1;
5490 		sum &= 0xffff;
5491 		id = (u_short)sum;
5492 	} else {
5493 		id = fr_nextipid(fin);
5494 		if (fin->fin_off == 0 && (fin->fin_flx & FI_FRAG) != 0)
5495 			(void) fr_ipid_newfrag(fin, (u_32_t)id);
5496 	}
5497 
5498 	ip = fin->fin_ip;
5499 	ido = ntohs(ip->ip_id);
5500 	if (id == ido)
5501 		return 0;
5502 	ip->ip_id = htons(id);
5503 	CALC_SUMD(ido, id, sumd);	/* DESTRUCTIVE MACRO! id,ido change */
5504 	sum = (~ntohs(ip->ip_sum)) & 0xffff;
5505 	sum += sumd;
5506 	sum = (sum >> 16) + (sum & 0xffff);
5507 	sum = (sum >> 16) + (sum & 0xffff);
5508 	sums = ~(u_short)sum;
5509 	ip->ip_sum = htons(sums);
5510 	return 0;
5511 }
5512 
5513 
5514 #ifdef	NEED_FRGETIFNAME
5515 /* ------------------------------------------------------------------------ */
5516 /* Function:    fr_getifname                                                */
5517 /* Returns:     char *    - pointer to interface name                       */
5518 /* Parameters:  ifp(I)    - pointer to network interface                    */
5519 /*              buffer(O) - pointer to where to store interface name        */
5520 /*                                                                          */
5521 /* Constructs an interface name in the buffer passed.  The buffer passed is */
5522 /* expected to be at least LIFNAMSIZ in bytes big.  If buffer is passed in  */
5523 /* as a NULL pointer then return a pointer to a static array.               */
5524 /* ------------------------------------------------------------------------ */
5525 char *fr_getifname(ifp, buffer)
5526 struct ifnet *ifp;
5527 char *buffer;
5528 {
5529 	static char namebuf[LIFNAMSIZ];
5530 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
5531      defined(__sgi) || defined(linux) || defined(_AIX51) || \
5532      (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
5533 	int unit, space;
5534 	char temp[20];
5535 	char *s;
5536 # endif
5537 
5538 	ASSERT(buffer != NULL);
5539 #ifdef notdef
5540 	if (buffer == NULL)
5541 		buffer = namebuf;
5542 #endif
5543 	(void) strncpy(buffer, ifp->if_name, LIFNAMSIZ);
5544 	buffer[LIFNAMSIZ - 1] = '\0';
5545 # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
5546      defined(__sgi) || defined(_AIX51) || \
5547      (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
5548 	for (s = buffer; *s; s++)
5549 		;
5550 	unit = ifp->if_unit;
5551 	space = LIFNAMSIZ - (s - buffer);
5552 	if (space > 0) {
5553 #  if defined(SNPRINTF) && defined(_KERNEL)
5554 		(void) SNPRINTF(temp, sizeof(temp), "%d", unit);
5555 #  else
5556 		(void) sprintf(temp, "%d", unit);
5557 #  endif
5558 		(void) strncpy(s, temp, space);
5559 	}
5560 # endif
5561 	return buffer;
5562 }
5563 #endif
5564 
5565 
5566 /* ------------------------------------------------------------------------ */
5567 /* Function:    fr_ioctlswitch                                              */
5568 /* Returns:     int     - -1 continue processing, else ioctl return value   */
5569 /* Parameters:  unit(I) - device unit opened                                */
5570 /*              data(I) - pointer to ioctl data                             */
5571 /*              cmd(I)  - ioctl command                                     */
5572 /*              mode(I) - mode value                                        */
5573 /*                                                                          */
5574 /* Based on the value of unit, call the appropriate ioctl handler or return */
5575 /* EIO if ipfilter is not running.   Also checks if write perms are req'd   */
5576 /* for the device in order to execute the ioctl.                            */
5577 /* ------------------------------------------------------------------------ */
5578 INLINE int fr_ioctlswitch(unit, data, cmd, mode, uid, ctx, ifs)
5579 int unit, mode, uid;
5580 ioctlcmd_t cmd;
5581 void *data, *ctx;
5582 ipf_stack_t *ifs;
5583 {
5584 	int error = 0;
5585 
5586 	switch (unit)
5587 	{
5588 	case IPL_LOGIPF :
5589 		error = -1;
5590 		break;
5591 	case IPL_LOGNAT :
5592 		if (ifs->ifs_fr_running > 0)
5593 			error = fr_nat_ioctl(data, cmd, mode, uid, ctx, ifs);
5594 		else
5595 			error = EIO;
5596 		break;
5597 	case IPL_LOGSTATE :
5598 		if (ifs->ifs_fr_running > 0)
5599 			error = fr_state_ioctl(data, cmd, mode, uid, ctx, ifs);
5600 		else
5601 			error = EIO;
5602 		break;
5603 	case IPL_LOGAUTH :
5604 		if (ifs->ifs_fr_running > 0) {
5605 			if ((cmd == (ioctlcmd_t)SIOCADAFR) ||
5606 			    (cmd == (ioctlcmd_t)SIOCRMAFR)) {
5607 				if (!(mode & FWRITE)) {
5608 					error = EPERM;
5609 				} else {
5610 					error = frrequest(unit, cmd, data,
5611 						  ifs->ifs_fr_active, 1, ifs);
5612 				}
5613 			} else {
5614 				error = fr_auth_ioctl(data, cmd, mode, uid, ctx, ifs);
5615 			}
5616 		} else
5617 			error = EIO;
5618 		break;
5619 	case IPL_LOGSYNC :
5620 #ifdef IPFILTER_SYNC
5621 		if (ifs->ifs_fr_running > 0)
5622 			error = fr_sync_ioctl(data, cmd, mode, ifs);
5623 		else
5624 #endif
5625 			error = EIO;
5626 		break;
5627 	case IPL_LOGSCAN :
5628 #ifdef IPFILTER_SCAN
5629 		if (ifs->ifs_fr_running > 0)
5630 			error = fr_scan_ioctl(data, cmd, mode, ifs);
5631 		else
5632 #endif
5633 			error = EIO;
5634 		break;
5635 	case IPL_LOGLOOKUP :
5636 #ifdef IPFILTER_LOOKUP
5637 		if (ifs->ifs_fr_running > 0)
5638 			error = ip_lookup_ioctl(data, cmd, mode, uid, ctx, ifs);
5639 		else
5640 #endif
5641 			error = EIO;
5642 		break;
5643 	default :
5644 		error = EIO;
5645 		break;
5646 	}
5647 
5648 	return error;
5649 }
5650 
5651 
5652 /*
5653  * This array defines the expected size of objects coming into the kernel
5654  * for the various recognised object types.
5655  */
5656 #define	NUM_OBJ_TYPES	19
5657 
5658 static	int	fr_objbytes[NUM_OBJ_TYPES][2] = {
5659 	{ 1,	sizeof(struct frentry) },		/* frentry */
5660 	{ 0,	sizeof(struct friostat) },
5661 	{ 0,	sizeof(struct fr_info) },
5662 	{ 0,	sizeof(struct fr_authstat) },
5663 	{ 0,	sizeof(struct ipfrstat) },
5664 	{ 0,	sizeof(struct ipnat) },
5665 	{ 0,	sizeof(struct natstat) },
5666 	{ 0,	sizeof(struct ipstate_save) },
5667 	{ 1,	sizeof(struct nat_save) },		/* nat_save */
5668 	{ 0,	sizeof(struct natlookup) },
5669 	{ 1,	sizeof(struct ipstate) },		/* ipstate */
5670 	{ 0,	sizeof(struct ips_stat) },
5671 	{ 0,	sizeof(struct frauth) },
5672 	{ 0,	sizeof(struct ipftune) },
5673 	{ 0,	sizeof(struct nat) },                   /* nat_t */
5674 	{ 0,	sizeof(struct ipfruleiter) },
5675 	{ 0,	sizeof(struct ipfgeniter) },
5676 	{ 0,	sizeof(struct ipftable) },
5677 	{ 0,	sizeof(struct ipflookupiter) }
5678 };
5679 
5680 
5681 /* ------------------------------------------------------------------------ */
5682 /* Function:    fr_inobj                                                    */
5683 /* Returns:     int     - 0 = success, else failure                         */
5684 /* Parameters:  data(I) - pointer to ioctl data                             */
5685 /*              ptr(I)  - pointer to store real data in                     */
5686 /*              type(I) - type of structure being moved                     */
5687 /*                                                                          */
5688 /* Copy in the contents of what the ipfobj_t points to.  In future, we      */
5689 /* add things to check for version numbers, sizes, etc, to make it backward */
5690 /* compatible at the ABI for user land.                                     */
5691 /* ------------------------------------------------------------------------ */
5692 int fr_inobj(data, ptr, type)
5693 void *data;
5694 void *ptr;
5695 int type;
5696 {
5697 	ipfobj_t obj;
5698 	int error = 0;
5699 
5700 	if ((type < 0) || (type > NUM_OBJ_TYPES-1))
5701 		return EINVAL;
5702 
5703 	error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj));
5704 	if (error != 0)
5705 		return EFAULT;
5706 
5707 	if (obj.ipfo_type != type)
5708 		return EINVAL;
5709 
5710 #ifndef	IPFILTER_COMPAT
5711 	if ((fr_objbytes[type][0] & 1) != 0) {
5712 		if (obj.ipfo_size < fr_objbytes[type][1])
5713 			return EINVAL;
5714 	} else if (obj.ipfo_size != fr_objbytes[type][1])
5715 		return EINVAL;
5716 #else
5717 	if (obj.ipfo_rev != IPFILTER_VERSION) {
5718 		error = fr_incomptrans(&obj, ptr);
5719 		return error;
5720 	}
5721 
5722 	if ((fr_objbytes[type][0] & 1) != 0 &&
5723 	    obj.ipfo_size < fr_objbytes[type][1] ||
5724 	    obj.ipfo_size != fr_objbytes[type][1])
5725 		return EINVAL;
5726 #endif
5727 
5728 	if ((fr_objbytes[type][0] & 1) != 0) {
5729 		error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr,
5730 				fr_objbytes[type][1]);
5731 	} else {
5732 		error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr,
5733 				obj.ipfo_size);
5734 	}
5735 	return error;
5736 }
5737 
5738 
5739 /* ------------------------------------------------------------------------ */
5740 /* Function:    fr_inobjsz                                                  */
5741 /* Returns:     int     - 0 = success, else failure                         */
5742 /* Parameters:  data(I) - pointer to ioctl data                             */
5743 /*              ptr(I)  - pointer to store real data in                     */
5744 /*              type(I) - type of structure being moved                     */
5745 /*              sz(I)   - size of data to copy                              */
5746 /*                                                                          */
5747 /* As per fr_inobj, except the size of the object to copy in is passed in   */
5748 /* but it must not be smaller than the size defined for the type and the    */
5749 /* type must allow for varied sized objects.  The extra requirement here is */
5750 /* that sz must match the size of the object being passed in - this is not  */
5751 /* not possible nor required in fr_inobj().                                 */
5752 /* ------------------------------------------------------------------------ */
5753 int fr_inobjsz(data, ptr, type, sz)
5754 void *data;
5755 void *ptr;
5756 int type, sz;
5757 {
5758 	ipfobj_t obj;
5759 	int error;
5760 
5761 	if ((type < 0) || (type > NUM_OBJ_TYPES-1))
5762 		return EINVAL;
5763 	if (((fr_objbytes[type][0] & 1) == 0) || (sz < fr_objbytes[type][1]))
5764 		return EINVAL;
5765 
5766 	error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj));
5767 	if (error != 0)
5768 		return EFAULT;
5769 
5770 	if (obj.ipfo_type != type)
5771 		return EINVAL;
5772 
5773 #ifndef	IPFILTER_COMPAT
5774 	if (obj.ipfo_size != sz)
5775 		return EINVAL;
5776 #else
5777 	if (obj.ipfo_rev != IPFILTER_VERSION)
5778 		/*XXX compatibility hook here */
5779 		/*EMPTY*/;
5780 	if (obj.ipfo_size != sz)
5781 		/* XXX compatibility hook here */
5782 		return EINVAL;
5783 #endif
5784 
5785 	error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr, sz);
5786 	return error;
5787 }
5788 
5789 
5790 /* ------------------------------------------------------------------------ */
5791 /* Function:    fr_outobjsz                                                 */
5792 /* Returns:     int     - 0 = success, else failure                         */
5793 /* Parameters:  data(I) - pointer to ioctl data                             */
5794 /*              ptr(I)  - pointer to store real data in                     */
5795 /*              type(I) - type of structure being moved                     */
5796 /*              sz(I)   - size of data to copy                              */
5797 /*                                                                          */
5798 /* As per fr_outobj, except the size of the object to copy out is passed in */
5799 /* but it must not be smaller than the size defined for the type and the    */
5800 /* type must allow for varied sized objects.  The extra requirement here is */
5801 /* that sz must match the size of the object being passed in - this is not  */
5802 /* not possible nor required in fr_outobj().                                */
5803 /* ------------------------------------------------------------------------ */
5804 int fr_outobjsz(data, ptr, type, sz)
5805 void *data;
5806 void *ptr;
5807 int type, sz;
5808 {
5809 	ipfobj_t obj;
5810 	int error;
5811 
5812 	if ((type < 0) || (type > NUM_OBJ_TYPES-1) ||
5813 	    ((fr_objbytes[type][0] & 1) == 0) ||
5814 	    (sz < fr_objbytes[type][1]))
5815 		return EINVAL;
5816 
5817 	error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj));
5818 	if (error != 0)
5819 		return EFAULT;
5820 
5821 	if (obj.ipfo_type != type)
5822 		return EINVAL;
5823 
5824 #ifndef	IPFILTER_COMPAT
5825 	if (obj.ipfo_size != sz)
5826 		return EINVAL;
5827 #else
5828 	if (obj.ipfo_rev != IPFILTER_VERSION)
5829 		/* XXX compatibility hook here */
5830 		/*EMPTY*/;
5831 	if (obj.ipfo_size != sz)
5832 		/* XXX compatibility hook here */
5833 		return EINVAL;
5834 #endif
5835 
5836 	error = COPYOUT((caddr_t)ptr, (caddr_t)obj.ipfo_ptr, sz);
5837 	return error;
5838 }
5839 
5840 
5841 /* ------------------------------------------------------------------------ */
5842 /* Function:    fr_outobj                                                   */
5843 /* Returns:     int     - 0 = success, else failure                         */
5844 /* Parameters:  data(I) - pointer to ioctl data                             */
5845 /*              ptr(I)  - pointer to store real data in                     */
5846 /*              type(I) - type of structure being moved                     */
5847 /*                                                                          */
5848 /* Copy out the contents of what ptr is to where ipfobj points to.  In      */
5849 /* future, we add things to check for version numbers, sizes, etc, to make  */
5850 /* it backward  compatible at the ABI for user land.                        */
5851 /* ------------------------------------------------------------------------ */
5852 int fr_outobj(data, ptr, type)
5853 void *data;
5854 void *ptr;
5855 int type;
5856 {
5857 	ipfobj_t obj;
5858 	int error;
5859 
5860 	if ((type < 0) || (type > NUM_OBJ_TYPES-1))
5861 		return EINVAL;
5862 
5863 	error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj));
5864 	if (error != 0)
5865 		return EFAULT;
5866 
5867 	if (obj.ipfo_type != type)
5868 		return EINVAL;
5869 
5870 #ifndef	IPFILTER_COMPAT
5871 	if ((fr_objbytes[type][0] & 1) != 0) {
5872 		if (obj.ipfo_size < fr_objbytes[type][1])
5873 			return EINVAL;
5874 	} else if (obj.ipfo_size != fr_objbytes[type][1])
5875 		return EINVAL;
5876 #else
5877 	if (obj.ipfo_rev != IPFILTER_VERSION) {
5878 		error = fr_outcomptrans(&obj, ptr);
5879 		return error;
5880 	}
5881 
5882 	if ((fr_objbytes[type][0] & 1) != 0 &&
5883 	    obj.ipfo_size < fr_objbytes[type][1] ||
5884 	    obj.ipfo_size != fr_objbytes[type][1])
5885 		return EINVAL;
5886 #endif
5887 
5888 	error = COPYOUT((caddr_t)ptr, (caddr_t)obj.ipfo_ptr, obj.ipfo_size);
5889 	return error;
5890 }
5891 
5892 
5893 /* ------------------------------------------------------------------------ */
5894 /* Function:    fr_checkl4sum                                               */
5895 /* Returns:     int     - 0 = good, -1 = bad, 1 = cannot check              */
5896 /* Parameters:  fin(I) - pointer to packet information                      */
5897 /*                                                                          */
5898 /* If possible, calculate the layer 4 checksum for the packet.  If this is  */
5899 /* not possible, return without indicating a failure or success but in a    */
5900 /* way that is ditinguishable.                                              */
5901 /* ------------------------------------------------------------------------ */
5902 int fr_checkl4sum(fin)
5903 fr_info_t *fin;
5904 {
5905 	u_short sum, hdrsum, *csump;
5906 	udphdr_t *udp;
5907 	int dosum;
5908 	ipf_stack_t *ifs = fin->fin_ifs;
5909 
5910 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
5911 	net_handle_t net_data_p;
5912 	if (fin->fin_v == 4)
5913 		net_data_p = ifs->ifs_ipf_ipv4;
5914 	else
5915 		net_data_p = ifs->ifs_ipf_ipv6;
5916 #endif
5917 
5918 	if ((fin->fin_flx & FI_NOCKSUM) != 0)
5919 		return 0;
5920 
5921 	/*
5922 	 * If the TCP packet isn't a fragment, isn't too short and otherwise
5923 	 * isn't already considered "bad", then validate the checksum.  If
5924 	 * this check fails then considered the packet to be "bad".
5925 	 */
5926 	if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0)
5927 		return 1;
5928 
5929 	csump = NULL;
5930 	hdrsum = 0;
5931 	dosum = 0;
5932 	sum = 0;
5933 
5934 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
5935 	ASSERT(fin->fin_m != NULL);
5936 	if (NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) ||
5937 	    NET_IS_HCK_L4_PART(net_data_p, fin->fin_m)) {
5938 			hdrsum = 0;
5939 			sum = 0;
5940 	} else {
5941 #endif
5942 		switch (fin->fin_p)
5943 		{
5944 		case IPPROTO_TCP :
5945 			csump = &((tcphdr_t *)fin->fin_dp)->th_sum;
5946 			dosum = 1;
5947 			break;
5948 
5949 		case IPPROTO_UDP :
5950 			udp = fin->fin_dp;
5951 			if (udp->uh_sum != 0) {
5952 				csump = &udp->uh_sum;
5953 				dosum = 1;
5954 			}
5955 			break;
5956 
5957 		case IPPROTO_ICMP :
5958 			csump = &((struct icmp *)fin->fin_dp)->icmp_cksum;
5959 			dosum = 1;
5960 			break;
5961 
5962 		default :
5963 			return 1;
5964 			/*NOTREACHED*/
5965 		}
5966 
5967 		if (csump != NULL)
5968 			hdrsum = *csump;
5969 
5970 		if (dosum)
5971 			sum = fr_cksum(fin->fin_m, fin->fin_ip,
5972 				       fin->fin_p, fin->fin_dp);
5973 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
5974 	}
5975 #endif
5976 #if !defined(_KERNEL)
5977 	if (sum == hdrsum) {
5978 		FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum));
5979 	} else {
5980 		FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum));
5981 	}
5982 #endif
5983 	if (hdrsum == sum)
5984 		return 0;
5985 	return -1;
5986 }
5987 
5988 
5989 /* ------------------------------------------------------------------------ */
5990 /* Function:    fr_ifpfillv4addr                                            */
5991 /* Returns:     int     - 0 = address update, -1 = address not updated      */
5992 /* Parameters:  atype(I)   - type of network address update to perform      */
5993 /*              sin(I)     - pointer to source of address information       */
5994 /*              mask(I)    - pointer to source of netmask information       */
5995 /*              inp(I)     - pointer to destination address store           */
5996 /*              inpmask(I) - pointer to destination netmask store           */
5997 /*                                                                          */
5998 /* Given a type of network address update (atype) to perform, copy          */
5999 /* information from sin/mask into inp/inpmask.  If ipnmask is NULL then no  */
6000 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in  */
6001 /* which case the operation fails.  For all values of atype other than      */
6002 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s  */
6003 /* value.                                                                   */
6004 /* ------------------------------------------------------------------------ */
6005 int fr_ifpfillv4addr(atype, sin, mask, inp, inpmask)
6006 int atype;
6007 struct sockaddr_in *sin, *mask;
6008 struct in_addr *inp, *inpmask;
6009 {
6010 	if (inpmask != NULL && atype != FRI_NETMASKED)
6011 		inpmask->s_addr = 0xffffffff;
6012 
6013 	if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
6014 		if (atype == FRI_NETMASKED) {
6015 			if (inpmask == NULL)
6016 				return -1;
6017 			inpmask->s_addr = mask->sin_addr.s_addr;
6018 		}
6019 		inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr;
6020 	} else {
6021 		inp->s_addr = sin->sin_addr.s_addr;
6022 	}
6023 	return 0;
6024 }
6025 
6026 
6027 #ifdef	USE_INET6
6028 /* ------------------------------------------------------------------------ */
6029 /* Function:    fr_ifpfillv6addr                                            */
6030 /* Returns:     int     - 0 = address update, -1 = address not updated      */
6031 /* Parameters:  atype(I)   - type of network address update to perform      */
6032 /*              sin(I)     - pointer to source of address information       */
6033 /*              mask(I)    - pointer to source of netmask information       */
6034 /*              inp(I)     - pointer to destination address store           */
6035 /*              inpmask(I) - pointer to destination netmask store           */
6036 /*                                                                          */
6037 /* Given a type of network address update (atype) to perform, copy          */
6038 /* information from sin/mask into inp/inpmask.  If ipnmask is NULL then no  */
6039 /* netmask update is performed unless FRI_NETMASKED is passed as atype, in  */
6040 /* which case the operation fails.  For all values of atype other than      */
6041 /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s  */
6042 /* value.                                                                   */
6043 /* ------------------------------------------------------------------------ */
6044 int fr_ifpfillv6addr(atype, sin, mask, inp, inpmask)
6045 int atype;
6046 struct sockaddr_in6 *sin, *mask;
6047 struct in_addr *inp, *inpmask;
6048 {
6049 	i6addr_t *src, *dst, *and, *dmask;
6050 
6051 	src = (i6addr_t *)&sin->sin6_addr;
6052 	and = (i6addr_t *)&mask->sin6_addr;
6053 	dst = (i6addr_t *)inp;
6054 	dmask = (i6addr_t *)inpmask;
6055 
6056 	if (inpmask != NULL && atype != FRI_NETMASKED) {
6057 		dmask->i6[0] = 0xffffffff;
6058 		dmask->i6[1] = 0xffffffff;
6059 		dmask->i6[2] = 0xffffffff;
6060 		dmask->i6[3] = 0xffffffff;
6061 	}
6062 
6063 	if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
6064 		if (atype == FRI_NETMASKED) {
6065 			if (inpmask == NULL)
6066 				return -1;
6067 			dmask->i6[0] = and->i6[0];
6068 			dmask->i6[1] = and->i6[1];
6069 			dmask->i6[2] = and->i6[2];
6070 			dmask->i6[3] = and->i6[3];
6071 		}
6072 
6073 		dst->i6[0] = src->i6[0] & and->i6[0];
6074 		dst->i6[1] = src->i6[1] & and->i6[1];
6075 		dst->i6[2] = src->i6[2] & and->i6[2];
6076 		dst->i6[3] = src->i6[3] & and->i6[3];
6077 	} else {
6078 		dst->i6[0] = src->i6[0];
6079 		dst->i6[1] = src->i6[1];
6080 		dst->i6[2] = src->i6[2];
6081 		dst->i6[3] = src->i6[3];
6082 	}
6083 	return 0;
6084 }
6085 #endif
6086 
6087 
6088 /* ------------------------------------------------------------------------ */
6089 /* Function:    fr_matchtag                                                 */
6090 /* Returns:     0 == mismatch, 1 == match.                                  */
6091 /* Parameters:  tag1(I) - pointer to first tag to compare                   */
6092 /*              tag2(I) - pointer to second tag to compare                  */
6093 /*                                                                          */
6094 /* Returns true (non-zero) or false(0) if the two tag structures can be     */
6095 /* considered to be a match or not match, respectively.  The tag is 16      */
6096 /* bytes long (16 characters) but that is overlayed with 4 32bit ints so    */
6097 /* compare the ints instead, for speed. tag1 is the master of the           */
6098 /* comparison.  This function should only be called with both tag1 and tag2 */
6099 /* as non-NULL pointers.                                                    */
6100 /* ------------------------------------------------------------------------ */
6101 int fr_matchtag(tag1, tag2)
6102 ipftag_t *tag1, *tag2;
6103 {
6104 	if (tag1 == tag2)
6105 		return 1;
6106 
6107 	if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0))
6108 		return 1;
6109 
6110 	if ((tag1->ipt_num[0] == tag2->ipt_num[0]) &&
6111 	    (tag1->ipt_num[1] == tag2->ipt_num[1]) &&
6112 	    (tag1->ipt_num[2] == tag2->ipt_num[2]) &&
6113 	    (tag1->ipt_num[3] == tag2->ipt_num[3]))
6114 		return 1;
6115 	return 0;
6116 }
6117 
6118 
6119 /* ------------------------------------------------------------------------ */
6120 /* Function:    fr_coalesce                                                 */
6121 /* Returns:     1 == success, -1 == failure, 0 == no change                 */
6122 /* Parameters:  fin(I) - pointer to packet information                      */
6123 /*                                                                          */
6124 /* Attempt to get all of the packet data into a single, contiguous buffer.  */
6125 /* If this call returns a failure then the buffers have also been freed.    */
6126 /* ------------------------------------------------------------------------ */
6127 int fr_coalesce(fin)
6128 fr_info_t *fin;
6129 {
6130 	ipf_stack_t *ifs = fin->fin_ifs;
6131 	if ((fin->fin_flx & FI_COALESCE) != 0)
6132 		return 1;
6133 
6134 	/*
6135 	 * If the mbuf pointers indicate that there is no mbuf to work with,
6136 	 * return but do not indicate success or failure.
6137 	 */
6138 	if (fin->fin_m == NULL || fin->fin_mp == NULL)
6139 		return 0;
6140 
6141 #if defined(_KERNEL)
6142 	if (fr_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) {
6143 		IPF_BUMP(ifs->ifs_fr_badcoalesces[fin->fin_out]);
6144 # ifdef MENTAT
6145 		FREE_MB_T(*fin->fin_mp);
6146 # endif
6147 		*fin->fin_mp = NULL;
6148 		fin->fin_m = NULL;
6149 		return -1;
6150 	}
6151 #else
6152 	fin = fin;	/* LINT */
6153 #endif
6154 	return 1;
6155 }
6156 
6157 
6158 /*
6159  * The following table lists all of the tunable variables that can be
6160  * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXT.  The format of each row
6161  * in the table below is as follows:
6162  *
6163  * pointer to value, name of value, minimum, maximum, size of the value's
6164  *     container, value attribute flags
6165  *
6166  * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED
6167  * means the value can only be written to when IPFilter is loaded but disabled.
6168  * The obvious implication is if neither of these are set then the value can be
6169  * changed at any time without harm.
6170  */
6171 ipftuneable_t lcl_ipf_tuneables[] = {
6172 	/* filtering */
6173 	{ { NULL },	"fr_flags",		0,	0xffffffff,
6174 			0,		0 },
6175 	{ { NULL },	"fr_active",		0,	0,
6176 			0,		IPFT_RDONLY },
6177 	{ { NULL },	"fr_control_forwarding",	0, 1,
6178 			0,		0 },
6179 	{ { NULL },	"fr_update_ipid",	0,	1,
6180 			0,		0 },
6181 	{ { NULL },	"fr_chksrc",		0,	1,
6182 			0,		0 },
6183 	{ { NULL },	"fr_minttl",		0,	1,
6184 			0,		0 },
6185 	{ { NULL }, 	"fr_icmpminfragmtu",	0,	1,
6186 			0,		0 },
6187 	{ { NULL },		"fr_pass",		0,	0xffffffff,
6188 			0,		0 },
6189 #if SOLARIS2 >= 10
6190 	{ { NULL },	"ipf_loopback",		0,	1,
6191 			0,		IPFT_WRDISABLED },
6192 #endif
6193 	/* state */
6194 	{ { NULL }, "fr_tcpidletimeout",	1,	0x7fffffff,
6195 			0,	IPFT_WRDISABLED },
6196 	{ { NULL },	"fr_tcpclosewait",	1,	0x7fffffff,
6197 			0,	IPFT_WRDISABLED },
6198 	{ { NULL },	"fr_tcplastack",	1,	0x7fffffff,
6199 			0,		IPFT_WRDISABLED },
6200 	{ { NULL },	"fr_tcptimeout",	1,	0x7fffffff,
6201 			0,		IPFT_WRDISABLED },
6202 	{ { NULL },	"fr_tcpclosed",		1,	0x7fffffff,
6203 			0,		IPFT_WRDISABLED },
6204 	{ { NULL },	"fr_tcphalfclosed",	1,	0x7fffffff,
6205 			0,	IPFT_WRDISABLED },
6206 	{ { NULL },	"fr_udptimeout",	1,	0x7fffffff,
6207 			0,		IPFT_WRDISABLED },
6208 	{ { NULL }, "fr_udpacktimeout",	1,	0x7fffffff,
6209 			0,	IPFT_WRDISABLED },
6210 	{ { NULL },	"fr_icmptimeout",	1,	0x7fffffff,
6211 			0,		IPFT_WRDISABLED },
6212 	{ { NULL }, "fr_icmpacktimeout",	1,	0x7fffffff,
6213 			0,	IPFT_WRDISABLED },
6214 	{ { NULL }, "fr_iptimeout",		1,	0x7fffffff,
6215 			0,		IPFT_WRDISABLED },
6216 	{ { NULL },	"fr_statemax",		1,	0x7fffffff,
6217 			0,		0 },
6218 	{ { NULL },	"fr_statesize",		1,	0x7fffffff,
6219 			0,		IPFT_WRDISABLED },
6220 	{ { NULL },	"fr_state_lock",	0,	1,
6221 			0,		IPFT_RDONLY },
6222 	{ { NULL }, "fr_state_maxbucket", 1,	0x7fffffff,
6223 			0,	IPFT_WRDISABLED },
6224 	{ { NULL }, "fr_state_maxbucket_reset",	0, 1,
6225 			0, IPFT_WRDISABLED },
6226 	{ { NULL },	"ipstate_logging",	0,	1,
6227 			0,	0 },
6228 	{ { NULL },	"state_flush_level_hi",	1,	100,
6229 			0,		0 },
6230 	{ { NULL },	"state_flush_level_lo",	1,	100,
6231 			0,		0 },
6232 	/* nat */
6233 	{ { NULL },		"fr_nat_lock",		0,	1,
6234 			0,		IPFT_RDONLY },
6235 	{ { NULL },	"ipf_nattable_sz",	1,	0x7fffffff,
6236 			0,	IPFT_WRDISABLED },
6237 	{ { NULL }, "ipf_nattable_max",	1,	0x7fffffff,
6238 			0,	0 },
6239 	{ { NULL },	"ipf_natrules_sz",	1,	0x7fffffff,
6240 			0,	IPFT_WRDISABLED },
6241 	{ { NULL },	"ipf_rdrrules_sz",	1,	0x7fffffff,
6242 			0,	IPFT_WRDISABLED },
6243 	{ { NULL },	"ipf_hostmap_sz",	1,	0x7fffffff,
6244 			0,		IPFT_WRDISABLED },
6245 	{ { NULL }, "fr_nat_maxbucket",	1,	0x7fffffff,
6246 			0,	IPFT_WRDISABLED },
6247 	{ { NULL },	"fr_nat_maxbucket_reset",	0, 1,
6248 			0,	IPFT_WRDISABLED },
6249 	{ { NULL },		"nat_logging",		0,	1,
6250 			0,		0 },
6251 	{ { NULL },	"fr_defnatage",		1,	0x7fffffff,
6252 			0,		IPFT_WRDISABLED },
6253 	{ { NULL },	"fr_defnatipage",	1,	0x7fffffff,
6254 			0,		IPFT_WRDISABLED },
6255 	{ { NULL }, "fr_defnaticmpage",	1,	0x7fffffff,
6256 			0,	IPFT_WRDISABLED },
6257 	{ { NULL },	"nat_flush_level_hi",	1,	100,
6258 			0,		0 },
6259 	{ { NULL },	"nat_flush_level_lo",	1,	100,
6260 			0,		0 },
6261 	/* frag */
6262 	{ { NULL },	"ipfr_size",		1,	0x7fffffff,
6263 			0,		IPFT_WRDISABLED },
6264 	{ { NULL },	"fr_ipfrttl",		1,	0x7fffffff,
6265 			0,		IPFT_WRDISABLED },
6266 #ifdef IPFILTER_LOG
6267 	/* log */
6268 	{ { NULL },	"ipl_suppress",		0,	1,
6269 			0,		0 },
6270 	{ { NULL },	"ipl_buffer_sz",	0,	0,
6271 			0,		IPFT_RDONLY },
6272 	{ { NULL },	"ipl_logmax",		0,	0x7fffffff,
6273 			0,		IPFT_WRDISABLED },
6274 	{ { NULL },	"ipl_logall",		0,	1,
6275 			0,		0 },
6276 	{ { NULL },	"ipl_logsize",		0,	0x80000,
6277 			0,		0 },
6278 #endif
6279 	{ { NULL },		NULL,			0,	0 }
6280 };
6281 
6282 static ipftuneable_t *
6283 tune_lookup(ipf_stack_t *ifs, char *name)
6284 {
6285     int i;
6286 
6287     for (i = 0; ifs->ifs_ipf_tuneables[i].ipft_name != NULL; i++) {
6288 	if (strcmp(ifs->ifs_ipf_tuneables[i].ipft_name, name) == 0)
6289 	    return (&ifs->ifs_ipf_tuneables[i]);
6290     }
6291     return (NULL);
6292 }
6293 
6294 #ifdef _KERNEL
6295 extern dev_info_t *ipf_dev_info;
6296 extern int ipf_property_update __P((dev_info_t *, ipf_stack_t *));
6297 #endif
6298 
6299 /* -------------------------------------------------------------------- */
6300 /* Function:	ipftuneable_setdefs()					*/
6301 /* Returns:		void						*/
6302 /* Parameters:	ifs - pointer to newly allocated IPF instance		*/
6303 /*				assigned to	IP instance		*/
6304 /*									*/
6305 /* Function initializes IPF instance variables. Function is invoked	*/
6306 /* from	ipftuneable_alloc(). ipftuneable_alloc() is called only one	*/
6307 /* time during IP instance lifetime - at the time of IP instance	*/
6308 /* creation. Anytime IP	instance is being created new private IPF	*/
6309 /* instance is allocated and assigned to it. The moment of IP 		*/
6310 /* instance creation is the right time to initialize those IPF 		*/
6311 /* variables.								*/
6312 /*									*/
6313 /* -------------------------------------------------------------------- */
6314 static void ipftuneable_setdefs(ipf_stack_t *ifs)
6315 {
6316 	ifs->ifs_ipfr_size = IPFT_SIZE;
6317 	ifs->ifs_fr_ipfrttl = 120;	/* 60 seconds */
6318 
6319 	/* it comes from fr_authinit() in IPF auth */
6320 	ifs->ifs_fr_authsize = FR_NUMAUTH;
6321 	ifs->ifs_fr_defaultauthage = 600;
6322 
6323 	/* it comes from fr_stateinit() in IPF state */
6324 	ifs->ifs_fr_tcpidletimeout = IPF_TTLVAL(3600 * 24 * 5);	/* five days */
6325 	ifs->ifs_fr_tcpclosewait = IPF_TTLVAL(TCP_MSL);
6326 	ifs->ifs_fr_tcplastack = IPF_TTLVAL(TCP_MSL);
6327 	ifs->ifs_fr_tcptimeout = IPF_TTLVAL(TCP_MSL);
6328 	ifs->ifs_fr_tcpclosed = IPF_TTLVAL(60);
6329 	ifs->ifs_fr_tcphalfclosed = IPF_TTLVAL(2 * 3600);	/* 2 hours */
6330 	ifs->ifs_fr_udptimeout = IPF_TTLVAL(120);
6331 	ifs->ifs_fr_udpacktimeout = IPF_TTLVAL(12);
6332 	ifs->ifs_fr_icmptimeout = IPF_TTLVAL(60);
6333 	ifs->ifs_fr_icmpacktimeout = IPF_TTLVAL(6);
6334 	ifs->ifs_fr_iptimeout = IPF_TTLVAL(60);
6335 	ifs->ifs_fr_statemax = IPSTATE_MAX;
6336 	ifs->ifs_fr_statesize = IPSTATE_SIZE;
6337 	ifs->ifs_fr_state_maxbucket_reset = 1;
6338 	ifs->ifs_state_flush_level_hi = ST_FLUSH_HI;
6339 	ifs->ifs_state_flush_level_lo = ST_FLUSH_LO;
6340 
6341 	/* it comes from fr_natinit() in ipnat */
6342 	ifs->ifs_ipf_nattable_sz = NAT_TABLE_SZ;
6343 	ifs->ifs_ipf_nattable_max = NAT_TABLE_MAX;
6344 	ifs->ifs_ipf_natrules_sz = NAT_SIZE;
6345 	ifs->ifs_ipf_rdrrules_sz = RDR_SIZE;
6346 	ifs->ifs_ipf_hostmap_sz = HOSTMAP_SIZE;
6347 	ifs->ifs_fr_nat_maxbucket_reset = 1;
6348 	ifs->ifs_fr_defnatage = DEF_NAT_AGE;
6349 	ifs->ifs_fr_defnatipage = 120;		/* 60 seconds */
6350 	ifs->ifs_fr_defnaticmpage = 6;		/* 3 seconds */
6351 	ifs->ifs_nat_flush_level_hi = NAT_FLUSH_HI;
6352 	ifs->ifs_nat_flush_level_lo = NAT_FLUSH_LO;
6353 
6354 #ifdef IPFILTER_LOG
6355 	/* it comes from fr_loginit() in IPF log */
6356 	ifs->ifs_ipl_suppress = 1;
6357 	ifs->ifs_ipl_logmax = IPL_LOGMAX;
6358 	ifs->ifs_ipl_logsize = IPFILTER_LOGSIZE;
6359 
6360 	/* from fr_natinit() */
6361 	ifs->ifs_nat_logging = 1;
6362 
6363 	/* from fr_stateinit() */
6364 	ifs->ifs_ipstate_logging = 1;
6365 #else
6366 	/* from fr_natinit() */
6367 	ifs->ifs_nat_logging = 0;
6368 
6369 	/* from fr_stateinit() */
6370 	ifs->ifs_ipstate_logging = 0;
6371 #endif
6372 	ifs->ifs_ipf_loopback = 0;
6373 
6374 }
6375 /*
6376  * Allocate a per-stack tuneable and copy in the names. Then
6377  * set it to point to each of the per-stack tunables.
6378  */
6379 void
6380 ipftuneable_alloc(ipf_stack_t *ifs)
6381 {
6382     ipftuneable_t *item;
6383 
6384     KMALLOCS(ifs->ifs_ipf_tuneables, ipftuneable_t *,
6385 	sizeof (lcl_ipf_tuneables));
6386     bcopy(lcl_ipf_tuneables, ifs->ifs_ipf_tuneables,
6387 	sizeof (lcl_ipf_tuneables));
6388 
6389 #define TUNE_SET(_ifs, _name, _field)			\
6390     item = tune_lookup((_ifs), (_name));		\
6391     if (item != NULL) {					\
6392 	item->ipft_una.ipftp_int = (unsigned int *)&((_ifs)->_field);	\
6393 	item->ipft_sz = sizeof ((_ifs)->_field);	\
6394     }
6395 
6396     TUNE_SET(ifs, "fr_flags", ifs_fr_flags);
6397     TUNE_SET(ifs, "fr_active", ifs_fr_active);
6398     TUNE_SET(ifs, "fr_control_forwarding", ifs_fr_control_forwarding);
6399     TUNE_SET(ifs, "fr_update_ipid", ifs_fr_update_ipid);
6400     TUNE_SET(ifs, "fr_chksrc", ifs_fr_chksrc);
6401     TUNE_SET(ifs, "fr_minttl", ifs_fr_minttl);
6402     TUNE_SET(ifs, "fr_icmpminfragmtu", ifs_fr_icmpminfragmtu);
6403     TUNE_SET(ifs, "fr_pass", ifs_fr_pass);
6404     TUNE_SET(ifs, "fr_tcpidletimeout", ifs_fr_tcpidletimeout);
6405     TUNE_SET(ifs, "fr_tcpclosewait", ifs_fr_tcpclosewait);
6406     TUNE_SET(ifs, "fr_tcplastack", ifs_fr_tcplastack);
6407     TUNE_SET(ifs, "fr_tcptimeout", ifs_fr_tcptimeout);
6408     TUNE_SET(ifs, "fr_tcpclosed", ifs_fr_tcpclosed);
6409     TUNE_SET(ifs, "fr_tcphalfclosed", ifs_fr_tcphalfclosed);
6410     TUNE_SET(ifs, "fr_udptimeout", ifs_fr_udptimeout);
6411     TUNE_SET(ifs, "fr_udpacktimeout", ifs_fr_udpacktimeout);
6412     TUNE_SET(ifs, "fr_icmptimeout", ifs_fr_icmptimeout);
6413     TUNE_SET(ifs, "fr_icmpacktimeout", ifs_fr_icmpacktimeout);
6414     TUNE_SET(ifs, "fr_iptimeout", ifs_fr_iptimeout);
6415     TUNE_SET(ifs, "fr_statemax", ifs_fr_statemax);
6416     TUNE_SET(ifs, "fr_statesize", ifs_fr_statesize);
6417     TUNE_SET(ifs, "fr_state_lock", ifs_fr_state_lock);
6418     TUNE_SET(ifs, "fr_state_maxbucket", ifs_fr_state_maxbucket);
6419     TUNE_SET(ifs, "fr_state_maxbucket_reset", ifs_fr_state_maxbucket_reset);
6420     TUNE_SET(ifs, "ipstate_logging", ifs_ipstate_logging);
6421     TUNE_SET(ifs, "fr_nat_lock", ifs_fr_nat_lock);
6422     TUNE_SET(ifs, "ipf_nattable_sz", ifs_ipf_nattable_sz);
6423     TUNE_SET(ifs, "ipf_nattable_max", ifs_ipf_nattable_max);
6424     TUNE_SET(ifs, "ipf_natrules_sz", ifs_ipf_natrules_sz);
6425     TUNE_SET(ifs, "ipf_rdrrules_sz", ifs_ipf_rdrrules_sz);
6426     TUNE_SET(ifs, "ipf_hostmap_sz", ifs_ipf_hostmap_sz);
6427     TUNE_SET(ifs, "fr_nat_maxbucket", ifs_fr_nat_maxbucket);
6428     TUNE_SET(ifs, "fr_nat_maxbucket_reset", ifs_fr_nat_maxbucket_reset);
6429     TUNE_SET(ifs, "nat_logging", ifs_nat_logging);
6430     TUNE_SET(ifs, "fr_defnatage", ifs_fr_defnatage);
6431     TUNE_SET(ifs, "fr_defnatipage", ifs_fr_defnatipage);
6432     TUNE_SET(ifs, "fr_defnaticmpage", ifs_fr_defnaticmpage);
6433     TUNE_SET(ifs, "nat_flush_level_hi", ifs_nat_flush_level_hi);
6434     TUNE_SET(ifs, "nat_flush_level_lo", ifs_nat_flush_level_lo);
6435     TUNE_SET(ifs, "state_flush_level_hi", ifs_state_flush_level_hi);
6436     TUNE_SET(ifs, "state_flush_level_lo", ifs_state_flush_level_lo);
6437     TUNE_SET(ifs, "ipfr_size", ifs_ipfr_size);
6438     TUNE_SET(ifs, "fr_ipfrttl", ifs_fr_ipfrttl);
6439     TUNE_SET(ifs, "ipf_loopback", ifs_ipf_loopback);
6440 #ifdef IPFILTER_LOG
6441     TUNE_SET(ifs, "ipl_suppress", ifs_ipl_suppress);
6442     TUNE_SET(ifs, "ipl_buffer_sz", ifs_ipl_buffer_sz);
6443     TUNE_SET(ifs, "ipl_logmax", ifs_ipl_logmax);
6444     TUNE_SET(ifs, "ipl_logall", ifs_ipl_logall);
6445     TUNE_SET(ifs, "ipl_logsize", ifs_ipl_logsize);
6446 #endif
6447 #undef TUNE_SET
6448 
6449 	ipftuneable_setdefs(ifs);
6450 
6451 #ifdef _KERNEL
6452     (void) ipf_property_update(ipf_dev_info, ifs);
6453 #endif
6454 }
6455 
6456 void
6457 ipftuneable_free(ipf_stack_t *ifs)
6458 {
6459 	KFREES(ifs->ifs_ipf_tuneables, sizeof (lcl_ipf_tuneables));
6460 	ifs->ifs_ipf_tuneables = NULL;
6461 }
6462 
6463 /* ------------------------------------------------------------------------ */
6464 /* Function:    fr_findtunebycookie                                         */
6465 /* Returns:     NULL = search failed, else pointer to tune struct           */
6466 /* Parameters:  cookie(I) - cookie value to search for amongst tuneables    */
6467 /*              next(O)   - pointer to place to store the cookie for the    */
6468 /*                          "next" tuneable, if it is desired.              */
6469 /*                                                                          */
6470 /* This function is used to walk through all of the existing tunables with  */
6471 /* successive calls.  It searches the known tunables for the one which has  */
6472 /* a matching value for "cookie" - ie its address.  When returning a match, */
6473 /* the next one to be found may be returned inside next.                    */
6474 /* ------------------------------------------------------------------------ */
6475 static ipftuneable_t *fr_findtunebycookie(cookie, next, ifs)
6476 void *cookie, **next;
6477 ipf_stack_t * ifs;
6478 {
6479 	ipftuneable_t *ta, **tap;
6480 
6481 	for (ta = ifs->ifs_ipf_tuneables; ta->ipft_name != NULL; ta++)
6482 		if (ta == cookie) {
6483 			if (next != NULL) {
6484 				/*
6485 				 * If the next entry in the array has a name
6486 				 * present, then return a pointer to it for
6487 				 * where to go next, else return a pointer to
6488 				 * the dynaminc list as a key to search there
6489 				 * next.  This facilitates a weak linking of
6490 				 * the two "lists" together.
6491 				 */
6492 				if ((ta + 1)->ipft_name != NULL)
6493 					*next = ta + 1;
6494 				else
6495 					*next = &ifs->ifs_ipf_tunelist;
6496 			}
6497 			return ta;
6498 		}
6499 
6500 	for (tap = &ifs->ifs_ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next)
6501 		if (tap == cookie) {
6502 			if (next != NULL)
6503 				*next = &ta->ipft_next;
6504 			return ta;
6505 		}
6506 
6507 	if (next != NULL)
6508 		*next = NULL;
6509 	return NULL;
6510 }
6511 
6512 
6513 /* ------------------------------------------------------------------------ */
6514 /* Function:    fr_findtunebyname                                           */
6515 /* Returns:     NULL = search failed, else pointer to tune struct           */
6516 /* Parameters:  name(I) - name of the tuneable entry to find.               */
6517 /*                                                                          */
6518 /* Search the static array of tuneables and the list of dynamic tuneables   */
6519 /* for an entry with a matching name.  If we can find one, return a pointer */
6520 /* to the matching structure.                                               */
6521 /* ------------------------------------------------------------------------ */
6522 static ipftuneable_t *fr_findtunebyname(name, ifs)
6523 const char *name;
6524 ipf_stack_t *ifs;
6525 {
6526 	ipftuneable_t *ta;
6527 
6528 	for (ta = ifs->ifs_ipf_tuneables; ta->ipft_name != NULL; ta++)
6529 		if (!strcmp(ta->ipft_name, name)) {
6530 			return ta;
6531 		}
6532 
6533 	for (ta = ifs->ifs_ipf_tunelist; ta != NULL; ta = ta->ipft_next)
6534 		if (!strcmp(ta->ipft_name, name)) {
6535 			return ta;
6536 		}
6537 
6538 	return NULL;
6539 }
6540 
6541 
6542 /* ------------------------------------------------------------------------ */
6543 /* Function:    fr_addipftune                                               */
6544 /* Returns:     int - 0 == success, else failure                            */
6545 /* Parameters:  newtune - pointer to new tune struct to add to tuneables    */
6546 /*                                                                          */
6547 /* Appends the tune structure pointer to by "newtune" to the end of the     */
6548 /* current list of "dynamic" tuneable parameters.  Once added, the owner    */
6549 /* of the object is not expected to ever change "ipft_next".                */
6550 /* ------------------------------------------------------------------------ */
6551 int fr_addipftune(newtune, ifs)
6552 ipftuneable_t *newtune;
6553 ipf_stack_t *ifs;
6554 {
6555 	ipftuneable_t *ta, **tap;
6556 
6557 	ta = fr_findtunebyname(newtune->ipft_name, ifs);
6558 	if (ta != NULL)
6559 		return EEXIST;
6560 
6561 	for (tap = &ifs->ifs_ipf_tunelist; *tap != NULL; tap = &(*tap)->ipft_next)
6562 		;
6563 
6564 	newtune->ipft_next = NULL;
6565 	*tap = newtune;
6566 	return 0;
6567 }
6568 
6569 
6570 /* ------------------------------------------------------------------------ */
6571 /* Function:    fr_delipftune                                               */
6572 /* Returns:     int - 0 == success, else failure                            */
6573 /* Parameters:  oldtune - pointer to tune struct to remove from the list of */
6574 /*                        current dynamic tuneables                         */
6575 /*                                                                          */
6576 /* Search for the tune structure, by pointer, in the list of those that are */
6577 /* dynamically added at run time.  If found, adjust the list so that this   */
6578 /* structure is no longer part of it.                                       */
6579 /* ------------------------------------------------------------------------ */
6580 int fr_delipftune(oldtune, ifs)
6581 ipftuneable_t *oldtune;
6582 ipf_stack_t *ifs;
6583 {
6584 	ipftuneable_t *ta, **tap;
6585 
6586 	for (tap = &ifs->ifs_ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next)
6587 		if (ta == oldtune) {
6588 			*tap = oldtune->ipft_next;
6589 			oldtune->ipft_next = NULL;
6590 			return 0;
6591 		}
6592 
6593 	return ESRCH;
6594 }
6595 
6596 
6597 /* ------------------------------------------------------------------------ */
6598 /* Function:    fr_ipftune                                                  */
6599 /* Returns:     int - 0 == success, else failure                            */
6600 /* Parameters:  cmd(I)  - ioctl command number                              */
6601 /*              data(I) - pointer to ioctl data structure                   */
6602 /*                                                                          */
6603 /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET.  These  */
6604 /* three ioctls provide the means to access and control global variables    */
6605 /* within IPFilter, allowing (for example) timeouts and table sizes to be   */
6606 /* changed without rebooting, reloading or recompiling.  The initialisation */
6607 /* and 'destruction' routines of the various components of ipfilter are all */
6608 /* each responsible for handling their own values being too big.            */
6609 /* ------------------------------------------------------------------------ */
6610 int fr_ipftune(cmd, data, ifs)
6611 ioctlcmd_t cmd;
6612 void *data;
6613 ipf_stack_t *ifs;
6614 {
6615 	ipftuneable_t *ta;
6616 	ipftune_t tu;
6617 	void *cookie;
6618 	int error;
6619 
6620 	error = fr_inobj(data, &tu, IPFOBJ_TUNEABLE);
6621 	if (error != 0)
6622 		return error;
6623 
6624 	tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
6625 	cookie = tu.ipft_cookie;
6626 	ta = NULL;
6627 
6628 	switch (cmd)
6629 	{
6630 	case SIOCIPFGETNEXT :
6631 		/*
6632 		 * If cookie is non-NULL, assume it to be a pointer to the last
6633 		 * entry we looked at, so find it (if possible) and return a
6634 		 * pointer to the next one after it.  The last entry in the
6635 		 * the table is a NULL entry, so when we get to it, set cookie
6636 		 * to NULL and return that, indicating end of list, erstwhile
6637 		 * if we come in with cookie set to NULL, we are starting anew
6638 		 * at the front of the list.
6639 		 */
6640 		if (cookie != NULL) {
6641 			ta = fr_findtunebycookie(cookie, &tu.ipft_cookie, ifs);
6642 		} else {
6643 			ta = ifs->ifs_ipf_tuneables;
6644 			tu.ipft_cookie = ta + 1;
6645 		}
6646 		if (ta != NULL) {
6647 			/*
6648 			 * Entry found, but does the data pointed to by that
6649 			 * row fit in what we can return?
6650 			 */
6651 			if (ta->ipft_sz > sizeof(tu.ipft_un))
6652 				return EINVAL;
6653 
6654 			tu.ipft_vlong = 0;
6655 			if (ta->ipft_sz == sizeof(u_long))
6656 				tu.ipft_vlong = *ta->ipft_plong;
6657 			else if (ta->ipft_sz == sizeof(u_int))
6658 				tu.ipft_vint = *ta->ipft_pint;
6659 			else if (ta->ipft_sz == sizeof(u_short))
6660 				tu.ipft_vshort = *ta->ipft_pshort;
6661 			else if (ta->ipft_sz == sizeof(u_char))
6662 				tu.ipft_vchar = *ta->ipft_pchar;
6663 
6664 			tu.ipft_sz = ta->ipft_sz;
6665 			tu.ipft_min = ta->ipft_min;
6666 			tu.ipft_max = ta->ipft_max;
6667 			tu.ipft_flags = ta->ipft_flags;
6668 			bcopy(ta->ipft_name, tu.ipft_name,
6669 			      MIN(sizeof(tu.ipft_name),
6670 				  strlen(ta->ipft_name) + 1));
6671 		}
6672 		error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
6673 		break;
6674 
6675 	case SIOCIPFGET :
6676 	case SIOCIPFSET :
6677 		/*
6678 		 * Search by name or by cookie value for a particular entry
6679 		 * in the tuning paramter table.
6680 		 */
6681 		error = ESRCH;
6682 		if (cookie != NULL) {
6683 			ta = fr_findtunebycookie(cookie, NULL, ifs);
6684 			if (ta != NULL)
6685 				error = 0;
6686 		} else if (tu.ipft_name[0] != '\0') {
6687 			ta = fr_findtunebyname(tu.ipft_name, ifs);
6688 			if (ta != NULL)
6689 				error = 0;
6690 		}
6691 		if (error != 0)
6692 			break;
6693 
6694 		if (cmd == (ioctlcmd_t)SIOCIPFGET) {
6695 			/*
6696 			 * Fetch the tuning parameters for a particular value
6697 			 */
6698 			tu.ipft_vlong = 0;
6699 			if (ta->ipft_sz == sizeof(u_long))
6700 				tu.ipft_vlong = *ta->ipft_plong;
6701 			else if (ta->ipft_sz == sizeof(u_int))
6702 				tu.ipft_vint = *ta->ipft_pint;
6703 			else if (ta->ipft_sz == sizeof(u_short))
6704 				tu.ipft_vshort = *ta->ipft_pshort;
6705 			else if (ta->ipft_sz == sizeof(u_char))
6706 				tu.ipft_vchar = *ta->ipft_pchar;
6707 			tu.ipft_cookie = ta;
6708 			tu.ipft_sz = ta->ipft_sz;
6709 			tu.ipft_min = ta->ipft_min;
6710 			tu.ipft_max = ta->ipft_max;
6711 			tu.ipft_flags = ta->ipft_flags;
6712 			error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
6713 
6714 		} else if (cmd == (ioctlcmd_t)SIOCIPFSET) {
6715 			/*
6716 			 * Set an internal parameter.  The hard part here is
6717 			 * getting the new value safely and correctly out of
6718 			 * the kernel (given we only know its size, not type.)
6719 			 */
6720 			u_long in;
6721 
6722 			if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) &&
6723 			    (ifs->ifs_fr_running > 0)) {
6724 				error = EBUSY;
6725 				break;
6726 			}
6727 
6728 			in = tu.ipft_vlong;
6729 			if (in < ta->ipft_min || in > ta->ipft_max) {
6730 				error = EINVAL;
6731 				break;
6732 			}
6733 
6734 			if (ta->ipft_sz == sizeof(u_long)) {
6735 				tu.ipft_vlong = *ta->ipft_plong;
6736 				*ta->ipft_plong = in;
6737 			} else if (ta->ipft_sz == sizeof(u_int)) {
6738 				tu.ipft_vint = *ta->ipft_pint;
6739 				*ta->ipft_pint = (u_int)(in & 0xffffffff);
6740 			} else if (ta->ipft_sz == sizeof(u_short)) {
6741 				tu.ipft_vshort = *ta->ipft_pshort;
6742 				*ta->ipft_pshort = (u_short)(in & 0xffff);
6743 			} else if (ta->ipft_sz == sizeof(u_char)) {
6744 				tu.ipft_vchar = *ta->ipft_pchar;
6745 				*ta->ipft_pchar = (u_char)(in & 0xff);
6746 			}
6747 			error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
6748 		}
6749 		break;
6750 
6751 	default :
6752 		error = EINVAL;
6753 		break;
6754 	}
6755 
6756 	return error;
6757 }
6758 
6759 
6760 /* ------------------------------------------------------------------------ */
6761 /* Function:    fr_initialise                                               */
6762 /* Returns:     int - 0 == success,  < 0 == failure                         */
6763 /* Parameters:  None.                                                       */
6764 /*                                                                          */
6765 /* Call of the initialise functions for all the various subsystems inside   */
6766 /* of IPFilter.  If any of them should fail, return immeadiately a failure  */
6767 /* BUT do not try to recover from the error here.                           */
6768 /* ------------------------------------------------------------------------ */
6769 int fr_initialise(ifs)
6770 ipf_stack_t *ifs;
6771 {
6772 	int i;
6773 
6774 #ifdef IPFILTER_LOG
6775 	i = fr_loginit(ifs);
6776 	if (i < 0)
6777 		return -10 + i;
6778 #endif
6779 	i = fr_natinit(ifs);
6780 	if (i < 0)
6781 		return -20 + i;
6782 
6783 	i = fr_stateinit(ifs);
6784 	if (i < 0)
6785 		return -30 + i;
6786 
6787 	i = fr_authinit(ifs);
6788 	if (i < 0)
6789 		return -40 + i;
6790 
6791 	i = fr_fraginit(ifs);
6792 	if (i < 0)
6793 		return -50 + i;
6794 
6795 	i = appr_init(ifs);
6796 	if (i < 0)
6797 		return -60 + i;
6798 
6799 #ifdef IPFILTER_SYNC
6800 	i = ipfsync_init(ifs);
6801 	if (i < 0)
6802 		return -70 + i;
6803 #endif
6804 #ifdef IPFILTER_SCAN
6805 	i = ipsc_init(ifs);
6806 	if (i < 0)
6807 		return -80 + i;
6808 #endif
6809 #ifdef IPFILTER_LOOKUP
6810 	i = ip_lookup_init(ifs);
6811 	if (i < 0)
6812 		return -90 + i;
6813 #endif
6814 #ifdef IPFILTER_COMPILED
6815 	ipfrule_add(ifs);
6816 #endif
6817 	return 0;
6818 }
6819 
6820 
6821 /* ------------------------------------------------------------------------ */
6822 /* Function:    fr_deinitialise                                             */
6823 /* Returns:     None.                                                       */
6824 /* Parameters:  None.                                                       */
6825 /*                                                                          */
6826 /* Call all the various subsystem cleanup routines to deallocate memory or  */
6827 /* destroy locks or whatever they've done that they need to now undo.       */
6828 /* The order here IS important as there are some cross references of        */
6829 /* internal data structures.                                                */
6830 /* ------------------------------------------------------------------------ */
6831 void fr_deinitialise(ifs)
6832 ipf_stack_t *ifs;
6833 {
6834 	fr_fragunload(ifs);
6835 	fr_authunload(ifs);
6836 	fr_natunload(ifs);
6837 	fr_stateunload(ifs);
6838 #ifdef IPFILTER_SCAN
6839 	fr_scanunload(ifs);
6840 #endif
6841 	appr_unload(ifs);
6842 
6843 #ifdef IPFILTER_COMPILED
6844 	ipfrule_remove(ifs);
6845 #endif
6846 
6847 	(void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE, ifs);
6848 	(void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE, ifs);
6849 	(void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE, ifs);
6850 	(void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE, ifs);
6851 
6852 #ifdef IPFILTER_LOOKUP
6853 	ip_lookup_unload(ifs);
6854 #endif
6855 
6856 #ifdef IPFILTER_LOG
6857 	fr_logunload(ifs);
6858 #endif
6859 }
6860 
6861 
6862 /* ------------------------------------------------------------------------ */
6863 /* Function:    fr_zerostats                                                */
6864 /* Returns:     int - 0 = success, else failure                             */
6865 /* Parameters:  data(O) - pointer to pointer for copying data back to       */
6866 /*                                                                          */
6867 /* Copies the current statistics out to userspace and then zero's the       */
6868 /* current ones in the kernel. The lock is only held across the bzero() as  */
6869 /* the copyout may result in paging (ie network activity.)                  */
6870 /* ------------------------------------------------------------------------ */
6871 int	fr_zerostats(data, ifs)
6872 caddr_t	data;
6873 ipf_stack_t *ifs;
6874 {
6875 	friostat_t fio;
6876 	int error;
6877 
6878 	fr_getstat(&fio, ifs);
6879 	error = copyoutptr(&fio, data, sizeof(fio));
6880 	if (error)
6881 		return EFAULT;
6882 
6883 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
6884 	bzero((char *)ifs->ifs_frstats, sizeof(*ifs->ifs_frstats) * 2);
6885 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
6886 
6887 	return 0;
6888 }
6889 
6890 
6891 #ifdef _KERNEL
6892 /* ------------------------------------------------------------------------ */
6893 /* Function:    fr_resolvedest                                              */
6894 /* Returns:     Nil                                                         */
6895 /* Parameters:  fdp(IO) - pointer to destination information to resolve     */
6896 /*              v(I)    - IP protocol version to match                      */
6897 /*                                                                          */
6898 /* Looks up an interface name in the frdest structure pointed to by fdp and */
6899 /* if a matching name can be found for the particular IP protocol version   */
6900 /* then store the interface pointer in the frdest struct.  If no match is   */
6901 /* found, then set the interface pointer to be -1 as NULL is considered to  */
6902 /* indicate there is no information at all in the structure.                */
6903 /* ------------------------------------------------------------------------ */
6904 void fr_resolvedest(fdp, v, ifs)
6905 frdest_t *fdp;
6906 int v;
6907 ipf_stack_t *ifs;
6908 {
6909 	fdp->fd_ifp = NULL;
6910 
6911   	if (*fdp->fd_ifname != '\0') {
6912  		fdp->fd_ifp = GETIFP(fdp->fd_ifname, v, ifs);
6913 		if (fdp->fd_ifp == NULL)
6914 			fdp->fd_ifp = (void *)-1;
6915 	}
6916 }
6917 #endif /* _KERNEL */
6918 
6919 
6920 /* ------------------------------------------------------------------------ */
6921 /* Function:    fr_resolvenic                                               */
6922 /* Returns:     void* - NULL = wildcard name, -1 = failed to find NIC, else */
6923 /*                      pointer to interface structure for NIC              */
6924 /* Parameters:  name(I) - complete interface name                           */
6925 /*              v(I)    - IP protocol version                               */
6926 /*                                                                          */
6927 /* Look for a network interface structure that firstly has a matching name  */
6928 /* to that passed in and that is also being used for that IP protocol       */
6929 /* version (necessary on some platforms where there are separate listings   */
6930 /* for both IPv4 and IPv6 on the same physical NIC.                         */
6931 /*                                                                          */
6932 /* One might wonder why name gets terminated with a \0 byte in here.  The   */
6933 /* reason is an interface name could get into the kernel structures of ipf  */
6934 /* in any number of ways and so long as they all use the same sized array   */
6935 /* to put the name in, it makes sense to ensure it gets null terminated     */
6936 /* before it is used for its intended purpose - finding its match in the    */
6937 /* kernel's list of configured interfaces.                                  */
6938 /*                                                                          */
6939 /* NOTE: This SHOULD ONLY be used with IPFilter structures that have an     */
6940 /*       array for the name that is LIFNAMSIZ bytes (at least) in length.   */
6941 /* ------------------------------------------------------------------------ */
6942 void *fr_resolvenic(name, v, ifs)
6943 char *name;
6944 int v;
6945 ipf_stack_t *ifs;
6946 {
6947 	void *nic;
6948 
6949 	if (name[0] == '\0')
6950 		return NULL;
6951 
6952 	if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) {
6953 		return NULL;
6954 	}
6955 
6956 	name[LIFNAMSIZ - 1] = '\0';
6957 
6958 	nic = GETIFP(name, v, ifs);
6959 	if (nic == NULL)
6960 		nic = (void *)-1;
6961 	return nic;
6962 }
6963 
6964 
6965 /* ------------------------------------------------------------------------ */
6966 /* Function:    ipf_expiretokens                                            */
6967 /* Returns:     None.                                                       */
6968 /* Parameters:  ifs - ipf stack instance                                    */
6969 /*                                                                          */
6970 /* This function is run every ipf tick to see if there are any tokens that  */
6971 /* have been held for too long and need to be freed up.                     */
6972 /* ------------------------------------------------------------------------ */
6973 void ipf_expiretokens(ifs)
6974 ipf_stack_t *ifs;
6975 {
6976 	ipftoken_t *it;
6977 
6978 	WRITE_ENTER(&ifs->ifs_ipf_tokens);
6979 	while ((it = ifs->ifs_ipftokenhead) != NULL) {
6980 		if (it->ipt_die > ifs->ifs_fr_ticks)
6981 			break;
6982 
6983 		ipf_freetoken(it, ifs);
6984 	}
6985 	RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
6986 }
6987 
6988 
6989 /* ------------------------------------------------------------------------ */
6990 /* Function:    ipf_deltoken                                                */
6991 /* Returns:     int - 0 = success, else error                               */
6992 /* Parameters:  type(I) - the token type to match                           */
6993 /*              uid(I)  - uid owning the token                              */
6994 /*              ptr(I)  - context pointer for the token                     */
6995 /*              ifs - ipf stack instance                                    */
6996 /*                                                                          */
6997 /* This function looks for a a token in the current list that matches up    */
6998 /* the fields (type, uid, ptr).  If none is found, ESRCH is returned, else  */
6999 /* call ipf_freetoken() to remove it from the list.                         */
7000 /* ------------------------------------------------------------------------ */
7001 int ipf_deltoken(type, uid, ptr, ifs)
7002 int type, uid;
7003 void *ptr;
7004 ipf_stack_t *ifs;
7005 {
7006 	ipftoken_t *it;
7007 	int error = ESRCH;
7008 
7009 	WRITE_ENTER(&ifs->ifs_ipf_tokens);
7010 	for (it = ifs->ifs_ipftokenhead; it != NULL; it = it->ipt_next)
7011 		if (ptr == it->ipt_ctx && type == it->ipt_type &&
7012 		    uid == it->ipt_uid) {
7013 			ipf_freetoken(it, ifs);
7014 			error = 0;
7015 			break;
7016 	}
7017 	RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
7018 
7019 	return error;
7020 }
7021 
7022 
7023 /* ------------------------------------------------------------------------ */
7024 /* Function:    ipf_unlinktoken                                             */
7025 /* Returns:     None.                                                       */
7026 /* Parameters:  token(I) - pointer to token structure                       */
7027 /*              ifs - ipf stack instance                                    */
7028 /*                                                                          */
7029 /* This function unlinks a token structure from the linked list of tokens   */
7030 /* that it belongs to.  The head pointer never needs to be explicitly       */
7031 /* adjusted, but the tail does due to the linked list implementation.       */
7032 /* ------------------------------------------------------------------------ */
7033 static void ipf_unlinktoken(token, ifs)
7034 ipftoken_t *token;
7035 ipf_stack_t *ifs;
7036 {
7037 
7038 	if (ifs->ifs_ipftokentail == &token->ipt_next)
7039 		ifs->ifs_ipftokentail = token->ipt_pnext;
7040 
7041 	*token->ipt_pnext = token->ipt_next;
7042 	if (token->ipt_next != NULL)
7043 		token->ipt_next->ipt_pnext = token->ipt_pnext;
7044 }
7045 
7046 
7047 /* ------------------------------------------------------------------------ */
7048 /* Function:    ipf_findtoken                                               */
7049 /* Returns:     ipftoken_t * - NULL if no memory, else pointer to token     */
7050 /* Parameters:  type(I) - the token type to match                           */
7051 /*              uid(I) - uid owning the token                               */
7052 /*              ptr(I) - context pointer for the token                      */
7053 /*              ifs - ipf stack instance                                    */
7054 /*                                                                          */
7055 /* This function looks for a live token in the list of current tokens that  */
7056 /* matches the tuple (type, uid, ptr).  If one cannot be found then one is  */
7057 /* allocated.  If one is found then it is moved to the top of the list of   */
7058 /* currently active tokens.                                                 */
7059 /*                                                                          */
7060 /* NOTE: It is by design that this function returns holding a read lock on  */
7061 /*       ipf_tokens.  Callers must make sure they release it!               */
7062 /* ------------------------------------------------------------------------ */
7063 ipftoken_t *ipf_findtoken(type, uid, ptr, ifs)
7064 int type, uid;
7065 void *ptr;
7066 ipf_stack_t *ifs;
7067 {
7068 	ipftoken_t *it, *new;
7069 
7070 	KMALLOC(new, ipftoken_t *);
7071 
7072 	WRITE_ENTER(&ifs->ifs_ipf_tokens);
7073 	for (it = ifs->ifs_ipftokenhead; it != NULL; it = it->ipt_next) {
7074 		if (it->ipt_alive == 0)
7075 			continue;
7076 		if (ptr == it->ipt_ctx && type == it->ipt_type &&
7077 		    uid == it->ipt_uid)
7078 			break;
7079 	}
7080 
7081 	if (it == NULL) {
7082 		it = new;
7083 		new = NULL;
7084 		if (it == NULL)
7085 			return NULL;
7086 		it->ipt_data = NULL;
7087 		it->ipt_ctx = ptr;
7088 		it->ipt_uid = uid;
7089 		it->ipt_type = type;
7090 		it->ipt_next = NULL;
7091 		it->ipt_alive = 1;
7092 	} else {
7093 		if (new != NULL) {
7094 			KFREE(new);
7095 			new = NULL;
7096 		}
7097 
7098 		ipf_unlinktoken(it, ifs);
7099 	}
7100 	it->ipt_pnext = ifs->ifs_ipftokentail;
7101 	*ifs->ifs_ipftokentail = it;
7102 	ifs->ifs_ipftokentail = &it->ipt_next;
7103 	it->ipt_next = NULL;
7104 
7105 	it->ipt_die = ifs->ifs_fr_ticks + 2;
7106 
7107 	MUTEX_DOWNGRADE(&ifs->ifs_ipf_tokens);
7108 
7109 	return it;
7110 }
7111 
7112 
7113 /* ------------------------------------------------------------------------ */
7114 /* Function:    ipf_freetoken                                               */
7115 /* Returns:     None.                                                       */
7116 /* Parameters:  token(I) - pointer to token structure                       */
7117 /*              ifs - ipf stack instance                                    */
7118 /*                                                                          */
7119 /* This function unlinks a token from the linked list and on the path to    */
7120 /* free'ing the data, it calls the dereference function that is associated  */
7121 /* with the type of data pointed to by the token as it is considered to     */
7122 /* hold a reference to it.                                                  */
7123 /* ------------------------------------------------------------------------ */
7124 void ipf_freetoken(token, ifs)
7125 ipftoken_t *token;
7126 ipf_stack_t *ifs;
7127 {
7128 	void *data, **datap;
7129 
7130 	ipf_unlinktoken(token, ifs);
7131 
7132 	data = token->ipt_data;
7133 	datap = &data;
7134 
7135 	if ((data != NULL) && (data != (void *)-1)) {
7136 		switch (token->ipt_type)
7137 		{
7138 		case IPFGENITER_IPF :
7139 			(void)fr_derefrule((frentry_t **)datap, ifs);
7140 			break;
7141 		case IPFGENITER_IPNAT :
7142 			WRITE_ENTER(&ifs->ifs_ipf_nat);
7143 			fr_ipnatderef((ipnat_t **)datap, ifs);
7144 			RWLOCK_EXIT(&ifs->ifs_ipf_nat);
7145 			break;
7146 		case IPFGENITER_NAT :
7147 			fr_natderef((nat_t **)datap, ifs);
7148 			break;
7149 		case IPFGENITER_STATE :
7150 			fr_statederef((ipstate_t **)datap, ifs);
7151 			break;
7152 		case IPFGENITER_FRAG :
7153 			fr_fragderef((ipfr_t **)datap, &ifs->ifs_ipf_frag, ifs);
7154 			break;
7155 		case IPFGENITER_NATFRAG :
7156  			fr_fragderef((ipfr_t **)datap,
7157 				     &ifs->ifs_ipf_natfrag, ifs);
7158 			break;
7159 		case IPFGENITER_HOSTMAP :
7160 			WRITE_ENTER(&ifs->ifs_ipf_nat);
7161 			fr_hostmapdel((hostmap_t **)datap);
7162 			RWLOCK_EXIT(&ifs->ifs_ipf_nat);
7163 			break;
7164 		default :
7165 			(void) ip_lookup_iterderef(token->ipt_type, data, ifs);
7166 			break;
7167 		}
7168 	}
7169 
7170 	KFREE(token);
7171 }
7172 
7173 
7174 /* ------------------------------------------------------------------------ */
7175 /* Function:    ipf_getnextrule                                             */
7176 /* Returns:     int - 0 = success, else error                               */
7177 /* Parameters:  t(I)   - pointer to destination information to resolve      */
7178 /*              ptr(I) - pointer to ipfobj_t to copyin from user space      */
7179 /*              ifs - ipf stack instance                                    */
7180 /*                                                                          */
7181 /* This function's first job is to bring in the ipfruleiter_t structure via */
7182 /* the ipfobj_t structure to determine what should be the next rule to      */
7183 /* return. Once the ipfruleiter_t has been brought in, it then tries to     */
7184 /* find the 'next rule'.  This may include searching rule group lists or    */
7185 /* just be as simple as looking at the 'next' field in the rule structure.  */
7186 /* When we have found the rule to return, increase its reference count and  */
7187 /* if we used an existing rule to get here, decrease its reference count.   */
7188 /* ------------------------------------------------------------------------ */
7189 int ipf_getnextrule(t, ptr, ifs)
7190 ipftoken_t *t;
7191 void *ptr;
7192 ipf_stack_t *ifs;
7193 {
7194 	frentry_t *fr, *next, zero;
7195 	int error, out, count;
7196 	ipfruleiter_t it;
7197 	frgroup_t *fg;
7198 	char *dst;
7199 
7200 	if (t == NULL || ptr == NULL)
7201 		return EFAULT;
7202 	error = fr_inobj(ptr, &it, IPFOBJ_IPFITER);
7203 	if (error != 0)
7204 		return error;
7205 	if ((it.iri_ver != AF_INET) && (it.iri_ver != AF_INET6))
7206 		return EINVAL;
7207 	if ((it.iri_inout < 0) || (it.iri_inout > 3))
7208 		return EINVAL;
7209 	if (it.iri_nrules == 0)
7210 		return EINVAL;
7211 	if ((it.iri_active != 0) && (it.iri_active != 1))
7212 		return EINVAL;
7213 	if (it.iri_rule == NULL)
7214 		return EFAULT;
7215 
7216 	/*
7217 	 * Use bitmask on it.iri_inout to determine direction.
7218 	 * F_OUT (1) and F_ACOUT (3) mask to out = 1, while
7219 	 * F_IN (0) and F_ACIN (2) mask to out = 0.
7220 	 */
7221 	out = it.iri_inout & F_OUT;
7222 	READ_ENTER(&ifs->ifs_ipf_mutex);
7223 
7224 	/*
7225 	 * Retrieve "previous" entry from token and find the next entry.
7226 	 */
7227 	fr = t->ipt_data;
7228 	if (fr == NULL) {
7229 		if (*it.iri_group == '\0') {
7230 			/*
7231 			 * Use bitmask again to determine accounting or not.
7232 			 * F_ACIN will mask to accounting cases F_ACIN (2)
7233 			 * or F_ACOUT (3), but not F_IN or F_OUT.
7234 			 */
7235 			if ((it.iri_inout & F_ACIN) != 0) {
7236 				if (it.iri_ver == AF_INET)
7237 					next = ifs->ifs_ipacct
7238 					    [out][it.iri_active];
7239 				else
7240 					next = ifs->ifs_ipacct6
7241 					    [out][it.iri_active];
7242 			} else {
7243 				if (it.iri_ver == AF_INET)
7244 					next = ifs->ifs_ipfilter
7245 					    [out][it.iri_active];
7246 				else
7247 					next = ifs->ifs_ipfilter6
7248 					    [out][it.iri_active];
7249 			}
7250 		} else {
7251 			fg = fr_findgroup(it.iri_group, IPL_LOGIPF,
7252 					  it.iri_active, NULL, ifs);
7253 			if (fg != NULL)
7254 				next = fg->fg_start;
7255 			else
7256 				next = NULL;
7257 		}
7258 	} else {
7259 		next = fr->fr_next;
7260 	}
7261 
7262 	dst = (char *)it.iri_rule;
7263 	/*
7264 	 * The ipfruleiter may ask for more than 1 rule at a time to be
7265 	 * copied out, so long as that many exist in the list to start with!
7266 	 */
7267 	for (count = it.iri_nrules; count > 0; count--) {
7268 		/*
7269 		 * If we found an entry, add reference to it and update token.
7270 		 * Otherwise, zero out data to be returned and NULL out token.
7271 		 */
7272 		if (next != NULL) {
7273 			MUTEX_ENTER(&next->fr_lock);
7274 			next->fr_ref++;
7275 			MUTEX_EXIT(&next->fr_lock);
7276 			t->ipt_data = next;
7277 		} else {
7278 			bzero(&zero, sizeof(zero));
7279 			next = &zero;
7280 			t->ipt_data = NULL;
7281 		}
7282 
7283 		/*
7284 		 * Now that we have ref, it's save to give up lock.
7285 		 */
7286 		RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
7287 
7288 		/*
7289 		 * Copy out data and clean up references and token as needed.
7290 		 */
7291 		error = COPYOUT(next, dst, sizeof(*next));
7292 		if (error != 0)
7293 			error = EFAULT;
7294 		if (t->ipt_data == NULL) {
7295 			ipf_freetoken(t, ifs);
7296 			break;
7297 		} else {
7298 			if (fr != NULL)
7299 				(void) fr_derefrule(&fr, ifs);
7300 			if (next->fr_data != NULL) {
7301 				dst += sizeof(*next);
7302 				error = COPYOUT(next->fr_data, dst,
7303 						next->fr_dsize);
7304 				if (error != 0)
7305 					error = EFAULT;
7306 				else
7307 					dst += next->fr_dsize;
7308 			}
7309 			if (next->fr_next == NULL) {
7310 				ipf_freetoken(t, ifs);
7311 				break;
7312 			}
7313 		}
7314 
7315 		if ((count == 1) || (error != 0))
7316 			break;
7317 
7318 		READ_ENTER(&ifs->ifs_ipf_mutex);
7319 		fr = next;
7320 		next = fr->fr_next;
7321 	}
7322 
7323 	return error;
7324 }
7325 
7326 
7327 /* ------------------------------------------------------------------------ */
7328 /* Function:    fr_frruleiter                                               */
7329 /* Returns:     int - 0 = success, else error                               */
7330 /* Parameters:  data(I) - the token type to match                           */
7331 /*              uid(I) - uid owning the token                               */
7332 /*              ptr(I) - context pointer for the token                      */
7333 /*              ifs - ipf stack instance                                    */
7334 /*                                                                          */
7335 /* This function serves as a stepping stone between fr_ipf_ioctl and        */
7336 /* ipf_getnextrule.  It's role is to find the right token in the kernel for */
7337 /* the process doing the ioctl and use that to ask for the next rule.       */
7338 /* ------------------------------------------------------------------------ */
7339 int ipf_frruleiter(data, uid, ctx, ifs)
7340 void *data, *ctx;
7341 int uid;
7342 ipf_stack_t *ifs;
7343 {
7344 	ipftoken_t *token;
7345 	int error;
7346 
7347 	token = ipf_findtoken(IPFGENITER_IPF, uid, ctx, ifs);
7348 	if (token != NULL)
7349 		error = ipf_getnextrule(token, data, ifs);
7350 	else
7351 		error = EFAULT;
7352 	RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
7353 
7354 	return error;
7355 }
7356 
7357 
7358 /* ------------------------------------------------------------------------ */
7359 /* Function:    ipf_geniter                                                 */
7360 /* Returns:     int - 0 = success, else error                               */
7361 /* Parameters:  token(I) - pointer to ipftoken structure                    */
7362 /*              itp(I) - pointer to ipfgeniter structure                    */
7363 /*              ifs - ipf stack instance                                    */
7364 /*                                                                          */
7365 /* Generic iterator called from ipf_genericiter.  Currently only used for   */
7366 /* walking through list of fragments.                                       */
7367 /* ------------------------------------------------------------------------ */
7368 int ipf_geniter(token, itp, ifs)
7369 ipftoken_t *token;
7370 ipfgeniter_t *itp;
7371 ipf_stack_t *ifs;
7372 {
7373 	int error;
7374 
7375 	switch (itp->igi_type)
7376 	{
7377 	case IPFGENITER_FRAG :
7378 		error = fr_nextfrag(token, itp, &ifs->ifs_ipfr_list,
7379 				    &ifs->ifs_ipfr_tail, &ifs->ifs_ipf_frag,
7380 				    ifs);
7381 		break;
7382 	default :
7383 		error = EINVAL;
7384 		break;
7385 	}
7386 
7387 	return error;
7388 }
7389 
7390 
7391 /* ------------------------------------------------------------------------ */
7392 /* Function:    ipf_genericiter                                             */
7393 /* Returns:     int - 0 = success, else error                               */
7394 /* Parameters:  data(I) - the token type to match                           */
7395 /*              uid(I) - uid owning the token                               */
7396 /*              ptr(I) - context pointer for the token                      */
7397 /*              ifs - ipf stack instance                                    */
7398 /*                                                                          */
7399 /* This function serves as a stepping stone between fr_ipf_ioctl and        */
7400 /* ipf_geniter when handling SIOCGENITER.  It's role is to find the right   */
7401 /* token in the kernel for the process using the ioctl, and to use that     */
7402 /* token when calling ipf_geniter.                                          */
7403 /* ------------------------------------------------------------------------ */
7404 int ipf_genericiter(data, uid, ctx, ifs)
7405 void *data, *ctx;
7406 int uid;
7407 ipf_stack_t *ifs;
7408 {
7409 	ipftoken_t *token;
7410 	ipfgeniter_t iter;
7411 	int error;
7412 
7413 	error = fr_inobj(data, &iter, IPFOBJ_GENITER);
7414 	if (error != 0)
7415 		return error;
7416 
7417 	token = ipf_findtoken(iter.igi_type, uid, ctx, ifs);
7418 	if (token != NULL) {
7419 		token->ipt_subtype = iter.igi_type;
7420 		error = ipf_geniter(token, &iter, ifs);
7421 	} else
7422 		error = EFAULT;
7423 	RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
7424 
7425 	return error;
7426 }
7427 
7428 
7429 /* --------------------------------------------------------------------- */
7430 /* Function:    ipf_earlydrop                                            */
7431 /* Returns:     number of dropped/removed entries from the queue         */
7432 /* Parameters:	flushtype - which table we're cleaning (NAT or State)	 */
7433 /*              ifq	- pointer to queue with entries to be deleted    */
7434 /*              idletime - entry must be idle this long to be deleted    */
7435 /*              ifs     - ipf stack instance                             */
7436 /*                                                                       */
7437 /* Function is invoked from state/NAT flush routines to remove entries   */
7438 /* from specified timeout queue, based on how long they've sat idle,     */
7439 /* without waiting for it to happen on its own.                          */
7440 /* --------------------------------------------------------------------- */
7441 int ipf_earlydrop(flushtype, ifq, idletime, ifs)
7442 int flushtype;
7443 ipftq_t *ifq;
7444 int idletime;
7445 ipf_stack_t *ifs;
7446 {
7447         ipftqent_t *tqe, *tqn;
7448         unsigned int dropped;
7449         int droptick;
7450 	void *ent;
7451 
7452         if (ifq == NULL)
7453                 return (0);
7454 
7455         dropped = 0;
7456 
7457         /*
7458          * Determine the tick representing the idle time we're interested
7459          * in.  If an entry exists in the queue, and it was touched before
7460          * that tick, then it's been idle longer than idletime, so it should
7461 	 * be deleted.
7462          */
7463         droptick = ifs->ifs_fr_ticks - idletime;
7464         tqn = ifq->ifq_head;
7465         while ((tqe = tqn) != NULL && tqe->tqe_touched < droptick) {
7466                 tqn = tqe->tqe_next;
7467 		ent = tqe->tqe_parent;
7468 		switch (flushtype)
7469 		{
7470 		case NAT_FLUSH:
7471 			if (nat_delete((nat_t *)ent, NL_FLUSH, ifs) == 0)
7472 				dropped++;
7473 			break;
7474 		case STATE_FLUSH:
7475 			if (fr_delstate((ipstate_t *)ent, ISL_FLUSH, ifs) == 0)
7476 				dropped++;
7477 			break;
7478 		default:
7479 			return (0);
7480 		}
7481         }
7482         return (dropped);
7483 }
7484 
7485 
7486 /* --------------------------------------------------------------------- */
7487 /* Function:    ipf_flushclosing                                         */
7488 /* Returns:     int - number of entries deleted                          */
7489 /* Parameters:	flushtype - which table we're cleaning (NAT or State)	 */
7490 /*              stateval - TCP state at which to start removing entries  */
7491 /*              ipfqs - pointer to timeout queues                        */
7492 /*              userqs - pointer to user defined queues                  */
7493 /*              ifs  - ipf stack instance                                */
7494 /*                                                                       */
7495 /* Remove state/NAT table entries for TCP connections which are in the   */
7496 /* process of closing, and have at least reached the state specified by  */
7497 /* the 'stateval' parameter.                                             */
7498 /* --------------------------------------------------------------------- */
7499 int ipf_flushclosing(flushtype, stateval, ipfqs, userqs, ifs)
7500 int flushtype, stateval;
7501 ipftq_t *ipfqs, *userqs;
7502 ipf_stack_t *ifs;
7503 {
7504 	ipftq_t *ifq, *ifqn;
7505         ipftqent_t *tqe, *tqn;
7506         int dropped;
7507 	void *ent;
7508 	nat_t *nat;
7509 	ipstate_t *is;
7510 
7511         dropped = 0;
7512 
7513         /*
7514          * Start by deleting any entries in specific timeout queues.
7515          */
7516 	ifqn = &ipfqs[stateval];
7517         while ((ifq = ifqn) != NULL) {
7518                 ifqn = ifq->ifq_next;
7519                 dropped += ipf_earlydrop(flushtype, ifq, (int)0, ifs);
7520         }
7521 
7522         /*
7523          * Next, look through user defined queues for closing entries.
7524          */
7525 	ifqn = userqs;
7526         while ((ifq = ifqn) != NULL) {
7527                 ifqn = ifq->ifq_next;
7528                 tqn = ifq->ifq_head;
7529                 while ((tqe = tqn) != NULL) {
7530                         tqn = tqe->tqe_next;
7531 			ent = tqe->tqe_parent;
7532 			switch (flushtype)
7533 			{
7534 			case NAT_FLUSH:
7535 				nat = (nat_t *)ent;
7536 				if ((nat->nat_p == IPPROTO_TCP) &&
7537 				    (nat->nat_tcpstate[0] >= stateval) &&
7538 				    (nat->nat_tcpstate[1] >= stateval) &&
7539 				    (nat_delete(nat, NL_EXPIRE, ifs) == 0))
7540 					dropped++;
7541 				break;
7542 			case STATE_FLUSH:
7543 				is = (ipstate_t *)ent;
7544 				if ((is->is_p == IPPROTO_TCP) &&
7545 				    (is->is_state[0] >= stateval) &&
7546 				    (is->is_state[1] >= stateval) &&
7547 				    (fr_delstate(is, ISL_EXPIRE, ifs) == 0))
7548 					dropped++;
7549 				break;
7550 			default:
7551 				return (0);
7552 			}
7553                 }
7554         }
7555         return (dropped);
7556 }
7557 
7558 
7559 /* --------------------------------------------------------------------- */
7560 /* Function:    ipf_extraflush                                           */
7561 /* Returns:     int - number of entries flushed (0 = none)               */
7562 /* Parameters:	flushtype - which table we're cleaning (NAT or State)	 */
7563 /*              ipfqs - pointer to 'established' timeout queue           */
7564 /*              userqs - pointer to user defined queues                  */
7565 /*              ifs  - ipf stack instance                                */
7566 /*                                                                       */
7567 /* This function gets called when either NAT or state tables fill up.    */
7568 /* We need to try a bit harder to free up some space.  The function will */
7569 /* flush entries for TCP connections which have been idle a long time.   */
7570 /*                                                                       */
7571 /* Currently, the idle time is checked using values from ideltime_tab[]	 */
7572 /* --------------------------------------------------------------------- */
7573 int ipf_extraflush(flushtype, ipfqs, userqs, ifs)
7574 int flushtype;
7575 ipftq_t *ipfqs, *userqs;
7576 ipf_stack_t *ifs;
7577 {
7578 	ipftq_t *ifq, *ifqn;
7579 	int idletime, removed, idle_idx;
7580 
7581 	removed = 0;
7582 
7583 	/*
7584   	 * Determine initial threshold for minimum idle time based on
7585 	 * how long ipfilter has been running.  Ipfilter needs to have
7586 	 * been up as long as the smallest interval to continue on.
7587 	 *
7588 	 * Minimum idle times stored in idletime_tab and indexed by
7589 	 * idle_idx.  Start at upper end of array and work backwards.
7590 	 *
7591 	 * Once the index is found, set the initial idle time to the
7592 	 * first interval before the current ipfilter run time.
7593 	 */
7594 	if (ifs->ifs_fr_ticks < idletime_tab[0])
7595 		return (0);
7596 	idle_idx = (sizeof (idletime_tab) / sizeof (int)) - 1;
7597 	if (ifs->ifs_fr_ticks > idletime_tab[idle_idx]) {
7598 		idletime = idletime_tab[idle_idx];
7599 	} else {
7600 		while ((idle_idx > 0) &&
7601 		    (ifs->ifs_fr_ticks < idletime_tab[idle_idx]))
7602 			idle_idx--;
7603 
7604 		idletime = (ifs->ifs_fr_ticks /
7605 			    idletime_tab[idle_idx]) *
7606 			    idletime_tab[idle_idx];
7607 	}
7608 
7609 	while (idle_idx >= 0) {
7610 		/*
7611 		 * Check to see if we need to delete more entries.
7612 		 * If we do, start with appropriate timeout queue.
7613 		 */
7614 		if (flushtype == NAT_FLUSH) {
7615 			if (NAT_TAB_WATER_LEVEL(ifs) <=
7616 			    ifs->ifs_nat_flush_level_lo)
7617 				break;
7618 		} else if (flushtype == STATE_FLUSH) {
7619 			if (ST_TAB_WATER_LEVEL(ifs) <=
7620 			    ifs->ifs_state_flush_level_lo)
7621 				break;
7622 		} else {
7623 			break;
7624 		}
7625 
7626 		removed += ipf_earlydrop(flushtype, ipfqs, idletime, ifs);
7627 
7628 		/*
7629 		 * Next, check the user defined queues.  But first, make
7630 		 * certain that timeout queue deletions didn't do enough.
7631 		 */
7632 		if (flushtype == NAT_FLUSH) {
7633 			if (NAT_TAB_WATER_LEVEL(ifs) <=
7634 			    ifs->ifs_nat_flush_level_lo)
7635 				break;
7636 		} else {
7637 			if (ST_TAB_WATER_LEVEL(ifs) <=
7638 			    ifs->ifs_state_flush_level_lo)
7639 				break;
7640 		}
7641 		ifqn = userqs;
7642 		while ((ifq = ifqn) != NULL) {
7643 			ifqn = ifq->ifq_next;
7644 			removed += ipf_earlydrop(flushtype, ifq, idletime, ifs);
7645 		}
7646 
7647 		/*
7648 		 * Adjust the granularity of idle time.
7649 		 *
7650 		 * If we reach an interval boundary, we need to
7651 		 * either adjust the idle time accordingly or exit
7652 		 * the loop altogether (if this is very last check).
7653 		 */
7654 		idletime -= idletime_tab[idle_idx];
7655 		if (idletime < idletime_tab[idle_idx]) {
7656 			if (idle_idx != 0) {
7657 				idletime = idletime_tab[idle_idx] -
7658 				idletime_tab[idle_idx - 1];
7659 				idle_idx--;
7660 			} else {
7661 				break;
7662 			}
7663 		}
7664 	}
7665 
7666 	return (removed);
7667 }
7668