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