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