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