xref: /illumos-gate/usr/src/uts/common/inet/ipf/fil.c (revision de22af4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (C) 1993-2003 by Darren Reed.
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
5ab25eeb5Syz  *
6*de22af4eSJohn Ojemann  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
77c478bd9Sstevel@tonic-gate  */
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #if defined(KERNEL) || defined(_KERNEL)
107c478bd9Sstevel@tonic-gate # undef KERNEL
117c478bd9Sstevel@tonic-gate # undef _KERNEL
127c478bd9Sstevel@tonic-gate # define        KERNEL	1
137c478bd9Sstevel@tonic-gate # define        _KERNEL	1
147c478bd9Sstevel@tonic-gate #endif
157c478bd9Sstevel@tonic-gate #include <sys/errno.h>
167c478bd9Sstevel@tonic-gate #include <sys/types.h>
177c478bd9Sstevel@tonic-gate #include <sys/param.h>
187c478bd9Sstevel@tonic-gate #include <sys/time.h>
197c478bd9Sstevel@tonic-gate #if defined(__NetBSD__)
207c478bd9Sstevel@tonic-gate # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
217c478bd9Sstevel@tonic-gate #  include "opt_ipfilter_log.h"
227c478bd9Sstevel@tonic-gate # endif
237c478bd9Sstevel@tonic-gate #endif
247c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && defined(__FreeBSD_version) && \
257c478bd9Sstevel@tonic-gate     (__FreeBSD_version >= 220000)
267c478bd9Sstevel@tonic-gate # if (__FreeBSD_version >= 400000)
277c478bd9Sstevel@tonic-gate #  if !defined(IPFILTER_LKM)
287c478bd9Sstevel@tonic-gate #   include "opt_inet6.h"
297c478bd9Sstevel@tonic-gate #  endif
307c478bd9Sstevel@tonic-gate #  if (__FreeBSD_version == 400019)
317c478bd9Sstevel@tonic-gate #   define CSUM_DELAY_DATA
327c478bd9Sstevel@tonic-gate #  endif
337c478bd9Sstevel@tonic-gate # endif
347c478bd9Sstevel@tonic-gate # include <sys/filio.h>
357c478bd9Sstevel@tonic-gate #else
367c478bd9Sstevel@tonic-gate # include <sys/ioctl.h>
377c478bd9Sstevel@tonic-gate #endif
38ab25eeb5Syz #if !defined(_AIX51)
39ab25eeb5Syz # include <sys/fcntl.h>
40ab25eeb5Syz #endif
417c478bd9Sstevel@tonic-gate #if defined(_KERNEL)
427c478bd9Sstevel@tonic-gate # include <sys/systm.h>
437c478bd9Sstevel@tonic-gate # include <sys/file.h>
447c478bd9Sstevel@tonic-gate #else
457c478bd9Sstevel@tonic-gate # include <stdio.h>
467c478bd9Sstevel@tonic-gate # include <string.h>
477c478bd9Sstevel@tonic-gate # include <stdlib.h>
48ab25eeb5Syz # include <stddef.h>
497c478bd9Sstevel@tonic-gate # include <sys/file.h>
507c478bd9Sstevel@tonic-gate # define _KERNEL
517c478bd9Sstevel@tonic-gate # ifdef __OpenBSD__
527c478bd9Sstevel@tonic-gate struct file;
537c478bd9Sstevel@tonic-gate # endif
547c478bd9Sstevel@tonic-gate # include <sys/uio.h>
557c478bd9Sstevel@tonic-gate # undef _KERNEL
567c478bd9Sstevel@tonic-gate #endif
57ab25eeb5Syz #if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \
58ab25eeb5Syz     !defined(linux)
597c478bd9Sstevel@tonic-gate # include <sys/mbuf.h>
607c478bd9Sstevel@tonic-gate #else
61ab25eeb5Syz # if !defined(linux)
62ab25eeb5Syz #  include <sys/byteorder.h>
63ab25eeb5Syz # endif
647c478bd9Sstevel@tonic-gate # if (SOLARIS2 < 5) && defined(sun)
657c478bd9Sstevel@tonic-gate #  include <sys/dditypes.h>
667c478bd9Sstevel@tonic-gate # endif
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate #ifdef __hpux
697c478bd9Sstevel@tonic-gate # define _NET_ROUTE_INCLUDED
707c478bd9Sstevel@tonic-gate #endif
71ab25eeb5Syz #if !defined(linux)
72ab25eeb5Syz # include <sys/protosw.h>
73ab25eeb5Syz #endif
747c478bd9Sstevel@tonic-gate #include <sys/socket.h>
757c478bd9Sstevel@tonic-gate #include <net/if.h>
767c478bd9Sstevel@tonic-gate #ifdef sun
777c478bd9Sstevel@tonic-gate # include <net/af.h>
787c478bd9Sstevel@tonic-gate #endif
79ab25eeb5Syz #if !defined(_KERNEL) && defined(__FreeBSD__)
80ab25eeb5Syz # include "radix_ipf.h"
81ab25eeb5Syz #endif
827c478bd9Sstevel@tonic-gate #include <net/route.h>
837c478bd9Sstevel@tonic-gate #include <netinet/in.h>
847c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
857c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
86ab25eeb5Syz #if !defined(linux)
87ab25eeb5Syz # include <netinet/ip_var.h>
88ab25eeb5Syz #endif
897c478bd9Sstevel@tonic-gate #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
907c478bd9Sstevel@tonic-gate # include <sys/hashing.h>
917c478bd9Sstevel@tonic-gate # include <netinet/in_var.h>
927c478bd9Sstevel@tonic-gate #endif
937c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
94ab25eeb5Syz #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL)
95ab25eeb5Syz # include <netinet/udp.h>
96ab25eeb5Syz # include <netinet/ip_icmp.h>
97ab25eeb5Syz #endif
987c478bd9Sstevel@tonic-gate #ifdef __hpux
997c478bd9Sstevel@tonic-gate # undef _NET_ROUTE_INCLUDED
1007c478bd9Sstevel@tonic-gate #endif
101ab25eeb5Syz #include "netinet/ip_compat.h"
1027c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
1037c478bd9Sstevel@tonic-gate # include <netinet/icmp6.h>
104ab25eeb5Syz # if !SOLARIS && defined(_KERNEL) && !defined(__osf__) && !defined(__hpux)
1057c478bd9Sstevel@tonic-gate #  include <netinet6/in6_var.h>
1067c478bd9Sstevel@tonic-gate # endif
1077c478bd9Sstevel@tonic-gate #endif
1087c478bd9Sstevel@tonic-gate #include <netinet/tcpip.h>
1097c478bd9Sstevel@tonic-gate #include "netinet/ip_fil.h"
1107c478bd9Sstevel@tonic-gate #include "netinet/ip_nat.h"
1117c478bd9Sstevel@tonic-gate #include "netinet/ip_frag.h"
1127c478bd9Sstevel@tonic-gate #include "netinet/ip_state.h"
1137c478bd9Sstevel@tonic-gate #include "netinet/ip_proxy.h"
1147c478bd9Sstevel@tonic-gate #include "netinet/ip_auth.h"
115f4b3ec61Sdh #include "netinet/ipf_stack.h"
1167c478bd9Sstevel@tonic-gate #ifdef IPFILTER_SCAN
1177c478bd9Sstevel@tonic-gate # include "netinet/ip_scan.h"
1187c478bd9Sstevel@tonic-gate #endif
119ab25eeb5Syz #ifdef IPFILTER_SYNC
120ab25eeb5Syz # include "netinet/ip_sync.h"
121ab25eeb5Syz #endif
1227c478bd9Sstevel@tonic-gate #include "netinet/ip_pool.h"
1237c478bd9Sstevel@tonic-gate #include "netinet/ip_htable.h"
124ab25eeb5Syz #ifdef IPFILTER_COMPILED
125ab25eeb5Syz # include "netinet/ip_rules.h"
1267c478bd9Sstevel@tonic-gate #endif
127ab25eeb5Syz #if defined(IPFILTER_BPF) && defined(_KERNEL)
1287c478bd9Sstevel@tonic-gate # include <net/bpf.h>
1297c478bd9Sstevel@tonic-gate #endif
1307c478bd9Sstevel@tonic-gate #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
1317c478bd9Sstevel@tonic-gate # include <sys/malloc.h>
1327c478bd9Sstevel@tonic-gate # if defined(_KERNEL) && !defined(IPFILTER_LKM)
1337c478bd9Sstevel@tonic-gate #  include "opt_ipfilter.h"
1347c478bd9Sstevel@tonic-gate # endif
1357c478bd9Sstevel@tonic-gate #endif
1367c478bd9Sstevel@tonic-gate #include "netinet/ipl.h"
137ab25eeb5Syz /* END OF INCLUDES */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate #if !defined(lint)
1407c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)fil.c	1.36 6/5/96 (C) 1993-2000 Darren Reed";
141ab25eeb5Syz static const char rcsid[] = "@(#)$Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $";
1427c478bd9Sstevel@tonic-gate #endif
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #ifndef	_KERNEL
1457c478bd9Sstevel@tonic-gate # include "ipf.h"
1467c478bd9Sstevel@tonic-gate # include "ipt.h"
147ab25eeb5Syz # include "bpf-ipf.h"
1487c478bd9Sstevel@tonic-gate extern	int	opts;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate # define	FR_VERBOSE(verb_pr)			verbose verb_pr
1517c478bd9Sstevel@tonic-gate # define	FR_DEBUG(verb_pr)			debug verb_pr
1527c478bd9Sstevel@tonic-gate #else /* #ifndef _KERNEL */
1537c478bd9Sstevel@tonic-gate # define	FR_VERBOSE(verb_pr)
1547c478bd9Sstevel@tonic-gate # define	FR_DEBUG(verb_pr)
1557c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 
158ab25eeb5Syz char	ipfilter_version[] = IPL_VERSION;
159ab25eeb5Syz int	fr_features = 0
160ab25eeb5Syz #ifdef	IPFILTER_LKM
161ab25eeb5Syz 		| IPF_FEAT_LKM
1627c478bd9Sstevel@tonic-gate #endif
163ab25eeb5Syz #ifdef	IPFILTER_LOG
164ab25eeb5Syz 		| IPF_FEAT_LOG
165ab25eeb5Syz #endif
166ab25eeb5Syz #ifdef	IPFILTER_LOOKUP
167ab25eeb5Syz 		| IPF_FEAT_LOOKUP
168ab25eeb5Syz #endif
169ab25eeb5Syz #ifdef	IPFILTER_BPF
170ab25eeb5Syz 		| IPF_FEAT_BPF
171ab25eeb5Syz #endif
172ab25eeb5Syz #ifdef	IPFILTER_COMPILED
173ab25eeb5Syz 		| IPF_FEAT_COMPILED
174ab25eeb5Syz #endif
175ab25eeb5Syz #ifdef	IPFILTER_CKSUM
176ab25eeb5Syz 		| IPF_FEAT_CKSUM
177ab25eeb5Syz #endif
178ab25eeb5Syz #ifdef	IPFILTER_SYNC
179ab25eeb5Syz 		| IPF_FEAT_SYNC
180ab25eeb5Syz #endif
181ab25eeb5Syz #ifdef	IPFILTER_SCAN
182ab25eeb5Syz 		| IPF_FEAT_SCAN
183ab25eeb5Syz #endif
184ab25eeb5Syz #ifdef	USE_INET6
185ab25eeb5Syz 		| IPF_FEAT_IPV6
186ab25eeb5Syz #endif
187ab25eeb5Syz 	;
1887c478bd9Sstevel@tonic-gate 
189cbded9aeSdr #define	IPF_BUMP(x)	(x)++
190cbded9aeSdr 
191cbded9aeSdr static	INLINE int	fr_ipfcheck __P((fr_info_t *, frentry_t *, int));
1927c478bd9Sstevel@tonic-gate static	INLINE int	fr_ipfcheck __P((fr_info_t *, frentry_t *, int));
1937c478bd9Sstevel@tonic-gate static	int		fr_portcheck __P((frpcmp_t *, u_short *));
194f4b3ec61Sdh static	int		frflushlist __P((int, minor_t, int *, frentry_t **,
195f4b3ec61Sdh 					 ipf_stack_t *));
1967c478bd9Sstevel@tonic-gate static	ipfunc_t	fr_findfunc __P((ipfunc_t));
1977c478bd9Sstevel@tonic-gate static	frentry_t	*fr_firewall __P((fr_info_t *, u_32_t *));
198f4b3ec61Sdh static	int		fr_funcinit __P((frentry_t *fr, ipf_stack_t *));
199ab25eeb5Syz static	INLINE void	frpr_ah __P((fr_info_t *));
2007663b816Sml static	INLINE void	frpr_esp __P((fr_info_t *));
2017663b816Sml static	INLINE void	frpr_gre __P((fr_info_t *));
2027c478bd9Sstevel@tonic-gate static	INLINE void	frpr_udp __P((fr_info_t *));
2037c478bd9Sstevel@tonic-gate static	INLINE void	frpr_tcp __P((fr_info_t *));
2047c478bd9Sstevel@tonic-gate static	INLINE void	frpr_icmp __P((fr_info_t *));
2057c478bd9Sstevel@tonic-gate static	INLINE void	frpr_ipv4hdr __P((fr_info_t *));
2067663b816Sml static	INLINE int	frpr_pullup __P((fr_info_t *, int));
2077663b816Sml static	INLINE void	frpr_short __P((fr_info_t *, int));
2087c478bd9Sstevel@tonic-gate static	INLINE void	frpr_tcpcommon __P((fr_info_t *));
2097c478bd9Sstevel@tonic-gate static	INLINE void	frpr_udpcommon __P((fr_info_t *));
2107c478bd9Sstevel@tonic-gate static	INLINE int	fr_updateipid __P((fr_info_t *));
2117c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
212f4b3ec61Sdh static	int		fr_grpmapinit __P((frentry_t *fr, ipf_stack_t *));
213f4b3ec61Sdh static	INLINE void	*fr_resolvelookup __P((u_int, u_int, lookupfunc_t *,
214f4b3ec61Sdh 					       ipf_stack_t *));
2157c478bd9Sstevel@tonic-gate #endif
216f4b3ec61Sdh static	void		frsynclist __P((int, int, void *, char *, frentry_t *,
217f4b3ec61Sdh     ipf_stack_t *));
218381a2a9aSdr static	void		*fr_ifsync __P((int, int, char *, char *,
219f4b3ec61Sdh 					void *, void *, ipf_stack_t *));
220f4b3ec61Sdh static	ipftuneable_t	*fr_findtunebyname __P((const char *, ipf_stack_t *));
221f4b3ec61Sdh static	ipftuneable_t	*fr_findtunebycookie __P((void *, void **, ipf_stack_t *));
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate /*
2247c478bd9Sstevel@tonic-gate  * bit values for identifying presence of individual IP options
225ab25eeb5Syz  * All of these tables should be ordered by increasing key value on the left
226ab25eeb5Syz  * hand side to allow for binary searching of the array and include a trailer
227ab25eeb5Syz  * with a 0 for the bitmask for linear searches to easily find the end with.
2287c478bd9Sstevel@tonic-gate  */
229ab25eeb5Syz const	struct	optlist	ipopts[20] = {
2307c478bd9Sstevel@tonic-gate 	{ IPOPT_NOP,	0x000001 },
2317c478bd9Sstevel@tonic-gate 	{ IPOPT_RR,	0x000002 },
2327c478bd9Sstevel@tonic-gate 	{ IPOPT_ZSU,	0x000004 },
2337c478bd9Sstevel@tonic-gate 	{ IPOPT_MTUP,	0x000008 },
2347c478bd9Sstevel@tonic-gate 	{ IPOPT_MTUR,	0x000010 },
2357c478bd9Sstevel@tonic-gate 	{ IPOPT_ENCODE,	0x000020 },
2367c478bd9Sstevel@tonic-gate 	{ IPOPT_TS,	0x000040 },
2377c478bd9Sstevel@tonic-gate 	{ IPOPT_TR,	0x000080 },
2387c478bd9Sstevel@tonic-gate 	{ IPOPT_SECURITY, 0x000100 },
2397c478bd9Sstevel@tonic-gate 	{ IPOPT_LSRR,	0x000200 },
2407c478bd9Sstevel@tonic-gate 	{ IPOPT_E_SEC,	0x000400 },
2417c478bd9Sstevel@tonic-gate 	{ IPOPT_CIPSO,	0x000800 },
2427c478bd9Sstevel@tonic-gate 	{ IPOPT_SATID,	0x001000 },
2437c478bd9Sstevel@tonic-gate 	{ IPOPT_SSRR,	0x002000 },
2447c478bd9Sstevel@tonic-gate 	{ IPOPT_ADDEXT,	0x004000 },
2457c478bd9Sstevel@tonic-gate 	{ IPOPT_VISA,	0x008000 },
2467c478bd9Sstevel@tonic-gate 	{ IPOPT_IMITD,	0x010000 },
2477c478bd9Sstevel@tonic-gate 	{ IPOPT_EIP,	0x020000 },
2487c478bd9Sstevel@tonic-gate 	{ IPOPT_FINN,	0x040000 },
2497c478bd9Sstevel@tonic-gate 	{ 0,		0x000000 }
2507c478bd9Sstevel@tonic-gate };
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate #ifdef USE_INET6
2537c478bd9Sstevel@tonic-gate struct optlist ip6exthdr[] = {
2547c478bd9Sstevel@tonic-gate 	{ IPPROTO_HOPOPTS,		0x000001 },
2557663b816Sml 	{ IPPROTO_IPV6,			0x000002 },
2567663b816Sml 	{ IPPROTO_ROUTING,		0x000004 },
2577663b816Sml 	{ IPPROTO_FRAGMENT,		0x000008 },
2587663b816Sml 	{ IPPROTO_ESP,			0x000010 },
2597663b816Sml 	{ IPPROTO_AH,			0x000020 },
2607663b816Sml 	{ IPPROTO_NONE,			0x000040 },
2617663b816Sml 	{ IPPROTO_DSTOPTS,		0x000080 },
2627663b816Sml 	{ 0,				0 }
2637c478bd9Sstevel@tonic-gate };
2647c478bd9Sstevel@tonic-gate #endif
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate struct optlist tcpopts[] = {
2677c478bd9Sstevel@tonic-gate 	{ TCPOPT_NOP,			0x000001 },
2687c478bd9Sstevel@tonic-gate 	{ TCPOPT_MAXSEG,		0x000002 },
2697c478bd9Sstevel@tonic-gate 	{ TCPOPT_WINDOW,		0x000004 },
2707c478bd9Sstevel@tonic-gate 	{ TCPOPT_SACK_PERMITTED,	0x000008 },
2717c478bd9Sstevel@tonic-gate 	{ TCPOPT_SACK,			0x000010 },
2727c478bd9Sstevel@tonic-gate 	{ TCPOPT_TIMESTAMP,		0x000020 },
2737c478bd9Sstevel@tonic-gate 	{ 0,				0x000000 }
2747c478bd9Sstevel@tonic-gate };
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * bit values for identifying presence of individual IP security options
2787c478bd9Sstevel@tonic-gate  */
279ab25eeb5Syz const	struct	optlist	secopt[8] = {
2807c478bd9Sstevel@tonic-gate 	{ IPSO_CLASS_RES4,	0x01 },
2817c478bd9Sstevel@tonic-gate 	{ IPSO_CLASS_TOPS,	0x02 },
2827c478bd9Sstevel@tonic-gate 	{ IPSO_CLASS_SECR,	0x04 },
2837c478bd9Sstevel@tonic-gate 	{ IPSO_CLASS_RES3,	0x08 },
2847c478bd9Sstevel@tonic-gate 	{ IPSO_CLASS_CONF,	0x10 },
2857c478bd9Sstevel@tonic-gate 	{ IPSO_CLASS_UNCL,	0x20 },
2867c478bd9Sstevel@tonic-gate 	{ IPSO_CLASS_RES2,	0x40 },
2877c478bd9Sstevel@tonic-gate 	{ IPSO_CLASS_RES1,	0x80 }
2887c478bd9Sstevel@tonic-gate };
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate /*
2927c478bd9Sstevel@tonic-gate  * Table of functions available for use with call rules.
2937c478bd9Sstevel@tonic-gate  */
2947c478bd9Sstevel@tonic-gate static ipfunc_resolve_t fr_availfuncs[] = {
2957c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
2967c478bd9Sstevel@tonic-gate 	{ "fr_srcgrpmap", fr_srcgrpmap, fr_grpmapinit },
2977c478bd9Sstevel@tonic-gate 	{ "fr_dstgrpmap", fr_dstgrpmap, fr_grpmapinit },
2987c478bd9Sstevel@tonic-gate #endif
2997c478bd9Sstevel@tonic-gate 	{ "", NULL }
3007c478bd9Sstevel@tonic-gate };
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 
303ea8244dcSJohn Ojemann /*
304ea8244dcSJohn Ojemann  * Below we declare a list of constants used only by the ipf_extraflush()
305ea8244dcSJohn Ojemann  * routine.  We are placing it here, instead of in ipf_extraflush() itself,
306ea8244dcSJohn Ojemann  * because we want to make it visible to tools such as mdb, nm etc., so the
307ea8244dcSJohn Ojemann  * values can easily be altered during debugging.
308ea8244dcSJohn Ojemann  */
309ea8244dcSJohn Ojemann static	const	int	idletime_tab[] = {
310ea8244dcSJohn Ojemann 	IPF_TTLVAL(30),		/* 30 seconds */
311ea8244dcSJohn Ojemann 	IPF_TTLVAL(1800),	/* 30 minutes */
312ea8244dcSJohn Ojemann 	IPF_TTLVAL(43200),	/* 12 hours */
313ea8244dcSJohn Ojemann 	IPF_TTLVAL(345600),	/* 4 days */
314ea8244dcSJohn Ojemann };
315ea8244dcSJohn Ojemann 
316ea8244dcSJohn Ojemann 
3177c478bd9Sstevel@tonic-gate /*
3187c478bd9Sstevel@tonic-gate  * The next section of code is a a collection of small routines that set
3197c478bd9Sstevel@tonic-gate  * fields in the fr_info_t structure passed based on properties of the
3207c478bd9Sstevel@tonic-gate  * current packet.  There are different routines for the same protocol
3217c478bd9Sstevel@tonic-gate  * for each of IPv4 and IPv6.  Adding a new protocol, for which there
3227c478bd9Sstevel@tonic-gate  * will "special" inspection for setup, is now more easily done by adding
3237c478bd9Sstevel@tonic-gate  * a new routine and expanding the frpr_ipinit*() function rather than by
3247c478bd9Sstevel@tonic-gate  * adding more code to a growing switch statement.
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate #ifdef USE_INET6
327ab25eeb5Syz static	INLINE int	frpr_ah6 __P((fr_info_t *));
328ab25eeb5Syz static	INLINE void	frpr_esp6 __P((fr_info_t *));
329ab25eeb5Syz static	INLINE void	frpr_gre6 __P((fr_info_t *));
3307c478bd9Sstevel@tonic-gate static	INLINE void	frpr_udp6 __P((fr_info_t *));
3317c478bd9Sstevel@tonic-gate static	INLINE void	frpr_tcp6 __P((fr_info_t *));
3327c478bd9Sstevel@tonic-gate static	INLINE void	frpr_icmp6 __P((fr_info_t *));
33333f2fefdSDarren Reed static	INLINE void	frpr_ipv6hdr __P((fr_info_t *));
3347c478bd9Sstevel@tonic-gate static	INLINE void	frpr_short6 __P((fr_info_t *, int));
3357663b816Sml static	INLINE int	frpr_hopopts6 __P((fr_info_t *));
3367663b816Sml static	INLINE int	frpr_routing6 __P((fr_info_t *));
3377663b816Sml static	INLINE int	frpr_dstopts6 __P((fr_info_t *));
3387663b816Sml static	INLINE int	frpr_fragment6 __P((fr_info_t *));
339ab25eeb5Syz static	INLINE int	frpr_ipv6exthdr __P((fr_info_t *, int, int));
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3437c478bd9Sstevel@tonic-gate /* Function:    frpr_short6                                                 */
3447c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
3457c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
3467c478bd9Sstevel@tonic-gate /*                                                                          */
3477c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
3487c478bd9Sstevel@tonic-gate /* This is function enforces the 'is a packet too short to be legit' rule   */
349ab25eeb5Syz /* for IPv6 and marks the packet with FI_SHORT if so.  See function comment */
350ab25eeb5Syz /* for frpr_short() for more details.                                       */
3517c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
352ab25eeb5Syz static INLINE void frpr_short6(fin, xmin)
3537c478bd9Sstevel@tonic-gate fr_info_t *fin;
354ab25eeb5Syz int xmin;
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 
357ab25eeb5Syz 	if (fin->fin_dlen < xmin)
358ab25eeb5Syz 		fin->fin_flx |= FI_SHORT;
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3637c478bd9Sstevel@tonic-gate /* Function:    frpr_ipv6hdr                                                */
36433f2fefdSDarren Reed /* Returns:     Nil                                                         */
3657c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
3667c478bd9Sstevel@tonic-gate /*                                                                          */
3677c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
3687c478bd9Sstevel@tonic-gate /* Copy values from the IPv6 header into the fr_info_t struct and call the  */
3697c478bd9Sstevel@tonic-gate /* per-protocol analyzer if it exists.                                      */
3707c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
37133f2fefdSDarren Reed static INLINE void frpr_ipv6hdr(fin)
3727c478bd9Sstevel@tonic-gate fr_info_t *fin;
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	ip6_t *ip6 = (ip6_t *)fin->fin_ip;
375ab25eeb5Syz 	int p, go = 1, i, hdrcount;
3767c478bd9Sstevel@tonic-gate 	fr_ip_t *fi = &fin->fin_fi;
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 	fin->fin_off = 0;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	fi->fi_tos = 0;
3817c478bd9Sstevel@tonic-gate 	fi->fi_optmsk = 0;
3827c478bd9Sstevel@tonic-gate 	fi->fi_secmsk = 0;
3837c478bd9Sstevel@tonic-gate 	fi->fi_auth = 0;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	p = ip6->ip6_nxt;
3867c478bd9Sstevel@tonic-gate 	fi->fi_ttl = ip6->ip6_hlim;
3877c478bd9Sstevel@tonic-gate 	fi->fi_src.in6 = ip6->ip6_src;
3887c478bd9Sstevel@tonic-gate 	fi->fi_dst.in6 = ip6->ip6_dst;
3897663b816Sml 	fin->fin_id = 0;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	hdrcount = 0;
3927663b816Sml 	while (go && !(fin->fin_flx & (FI_BAD|FI_SHORT))) {
3937c478bd9Sstevel@tonic-gate 		switch (p)
3947c478bd9Sstevel@tonic-gate 		{
3957c478bd9Sstevel@tonic-gate 		case IPPROTO_UDP :
3967c478bd9Sstevel@tonic-gate 			frpr_udp6(fin);
3977c478bd9Sstevel@tonic-gate 			go = 0;
3987c478bd9Sstevel@tonic-gate 			break;
3997663b816Sml 
4007c478bd9Sstevel@tonic-gate 		case IPPROTO_TCP :
4017c478bd9Sstevel@tonic-gate 			frpr_tcp6(fin);
4027c478bd9Sstevel@tonic-gate 			go = 0;
4037c478bd9Sstevel@tonic-gate 			break;
4047663b816Sml 
4057c478bd9Sstevel@tonic-gate 		case IPPROTO_ICMPV6 :
4067c478bd9Sstevel@tonic-gate 			frpr_icmp6(fin);
4077c478bd9Sstevel@tonic-gate 			go = 0;
4087c478bd9Sstevel@tonic-gate 			break;
4097663b816Sml 
4107663b816Sml 		case IPPROTO_GRE :
411ab25eeb5Syz 			frpr_gre6(fin);
4127663b816Sml 			go = 0;
4137663b816Sml 			break;
4147663b816Sml 
4157c478bd9Sstevel@tonic-gate 		case IPPROTO_HOPOPTS :
4167c478bd9Sstevel@tonic-gate 			/*
417ab25eeb5Syz 			 * hop by hop ext header is only allowed
418ab25eeb5Syz 			 * right after IPv6 header.
4197c478bd9Sstevel@tonic-gate 			 */
420ab25eeb5Syz 			if (hdrcount != 0) {
4217663b816Sml 				fin->fin_flx |= FI_BAD;
422ab25eeb5Syz 				p = IPPROTO_NONE;
423ab25eeb5Syz 			} else {
424ab25eeb5Syz 				p = frpr_hopopts6(fin);
4257c478bd9Sstevel@tonic-gate 			}
4267c478bd9Sstevel@tonic-gate 			break;
4277663b816Sml 
4287c478bd9Sstevel@tonic-gate 		case IPPROTO_DSTOPTS :
4297663b816Sml 			p = frpr_dstopts6(fin);
4307c478bd9Sstevel@tonic-gate 			break;
4317663b816Sml 
4327c478bd9Sstevel@tonic-gate 		case IPPROTO_ROUTING :
4337663b816Sml 			p = frpr_routing6(fin);
4347c478bd9Sstevel@tonic-gate 			break;
4357663b816Sml 
436ab25eeb5Syz 		case IPPROTO_AH :
437ab25eeb5Syz 			p = frpr_ah6(fin);
438ab25eeb5Syz 			break;
439ab25eeb5Syz 
4407c478bd9Sstevel@tonic-gate 		case IPPROTO_ESP :
441ab25eeb5Syz 			frpr_esp6(fin);
4427663b816Sml 			go = 0;
4437663b816Sml 			break;
4447663b816Sml 
445ab25eeb5Syz 		case IPPROTO_IPV6 :
4467663b816Sml 			for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
4477663b816Sml 				if (ip6exthdr[i].ol_val == p) {
448ab25eeb5Syz 					fin->fin_flx |= ip6exthdr[i].ol_bit;
4497c478bd9Sstevel@tonic-gate 					break;
4507663b816Sml 				}
4517c478bd9Sstevel@tonic-gate 			go = 0;
4527c478bd9Sstevel@tonic-gate 			break;
4537663b816Sml 
454ab25eeb5Syz 		case IPPROTO_NONE :
4557c478bd9Sstevel@tonic-gate 			go = 0;
4567c478bd9Sstevel@tonic-gate 			break;
4577663b816Sml 
4587c478bd9Sstevel@tonic-gate 		case IPPROTO_FRAGMENT :
459ab25eeb5Syz 			p = frpr_fragment6(fin);
460ab25eeb5Syz 			if (fin->fin_off != 0)  /* Not the first frag */
4617663b816Sml 				go = 0;
4627663b816Sml 			break;
4637663b816Sml 
4647c478bd9Sstevel@tonic-gate 		default :
4657c478bd9Sstevel@tonic-gate 			go = 0;
4667c478bd9Sstevel@tonic-gate 			break;
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 		hdrcount++;
4697663b816Sml 
470ab25eeb5Syz 		/*
471ab25eeb5Syz 		 * It is important to note that at this point, for the
472ab25eeb5Syz 		 * extension headers (go != 0), the entire header may not have
473ab25eeb5Syz 		 * been pulled up when the code gets to this point.  This is
474ab25eeb5Syz 		 * only done for "go != 0" because the other header handlers
475ab25eeb5Syz 		 * will all pullup their complete header.  The other indicator
476ab25eeb5Syz 		 * of an incomplete packet is that this was just an extension
477ab25eeb5Syz 		 * header.
478ab25eeb5Syz 		 */
479ab25eeb5Syz 		if ((go != 0) && (p != IPPROTO_NONE) &&
480ab25eeb5Syz 		    (frpr_pullup(fin, 0) == -1)) {
481ab25eeb5Syz 			p = IPPROTO_NONE;
482ab25eeb5Syz 			go = 0;
483ab25eeb5Syz 		}
484ab25eeb5Syz 	}
4857663b816Sml 	fi->fi_p = p;
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
490ab25eeb5Syz /* Function:    frpr_ipv6exthdr                                             */
4917663b816Sml /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
492ab25eeb5Syz /* Parameters:  fin(I)      - pointer to packet information                 */
493ab25eeb5Syz /*              multiple(I) - flag indicating yes/no if multiple occurances */
494ab25eeb5Syz /*                            of this extension header are allowed.         */
495ab25eeb5Syz /*              proto(I)    - protocol number for this extension header     */
4967c478bd9Sstevel@tonic-gate /*                                                                          */
4977c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
49872680cf5SDarren Reed /* This function expects to find an IPv6 extension header at fin_dp.        */
49972680cf5SDarren Reed /* There must be at least 8 bytes of data at fin_dp for there to be a valid */
50072680cf5SDarren Reed /* extension header present. If a good one is found, fin_dp is advanced to  */
50172680cf5SDarren Reed /* point at the first piece of data after the extension header, fin_exthdr  */
50272680cf5SDarren Reed /* points to the start of the extension header and the "protocol" of the    */
50372680cf5SDarren Reed /* *NEXT* header is returned.                                               */
5047c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
505ab25eeb5Syz static INLINE int frpr_ipv6exthdr(fin, multiple, proto)
5067c478bd9Sstevel@tonic-gate fr_info_t *fin;
507ab25eeb5Syz int multiple, proto;
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	struct ip6_ext *hdr;
5107c478bd9Sstevel@tonic-gate 	u_short shift;
5117c478bd9Sstevel@tonic-gate 	int i;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	fin->fin_flx |= FI_V6EXTHDR;
5147c478bd9Sstevel@tonic-gate 
515ab25eeb5Syz 				/* 8 is default length of extension hdr */
516ab25eeb5Syz 	if ((fin->fin_dlen - 8) < 0) {
517ab25eeb5Syz 		fin->fin_flx |= FI_SHORT;
5187663b816Sml 		return IPPROTO_NONE;
519ab25eeb5Syz 	}
5207663b816Sml 
5217663b816Sml 	if (frpr_pullup(fin, 8) == -1)
5227663b816Sml 		return IPPROTO_NONE;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	hdr = fin->fin_dp;
5257c478bd9Sstevel@tonic-gate 	shift = 8 + (hdr->ip6e_len << 3);
5267c478bd9Sstevel@tonic-gate 	if (shift > fin->fin_dlen) {	/* Nasty extension header length? */
5277c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_BAD;
5287663b816Sml 		return IPPROTO_NONE;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317663b816Sml 	for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
532ab25eeb5Syz 		if (ip6exthdr[i].ol_val == proto) {
533ab25eeb5Syz 			/*
534ab25eeb5Syz 			 * Most IPv6 extension headers are only allowed once.
535ab25eeb5Syz 			 */
536ab25eeb5Syz 			if ((multiple == 0) &&
537ab25eeb5Syz 			    ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0))
538ab25eeb5Syz 				fin->fin_flx |= FI_BAD;
539ab25eeb5Syz 			else
540ab25eeb5Syz 				fin->fin_optmsk |= ip6exthdr[i].ol_bit;
5417c478bd9Sstevel@tonic-gate 			break;
5427663b816Sml 		}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	fin->fin_dp = (char *)fin->fin_dp + shift;
5457c478bd9Sstevel@tonic-gate 	fin->fin_dlen -= shift;
5467c478bd9Sstevel@tonic-gate 
5477663b816Sml 	return hdr->ip6e_nxt;
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 
551ab25eeb5Syz /* ------------------------------------------------------------------------ */
552ab25eeb5Syz /* Function:    frpr_hopopts6                                               */
553ab25eeb5Syz /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
554ab25eeb5Syz /* Parameters:  fin(I) - pointer to packet information                      */
555ab25eeb5Syz /*                                                                          */
556ab25eeb5Syz /* IPv6 Only                                                                */
557ab25eeb5Syz /* This is function checks pending hop by hop options extension header      */
558ab25eeb5Syz /* ------------------------------------------------------------------------ */
559ab25eeb5Syz static INLINE int frpr_hopopts6(fin)
560ab25eeb5Syz fr_info_t *fin;
561ab25eeb5Syz {
562ab25eeb5Syz 	return frpr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
563ab25eeb5Syz }
564ab25eeb5Syz 
565ab25eeb5Syz 
5667c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5677c478bd9Sstevel@tonic-gate /* Function:    frpr_routing6                                               */
5687663b816Sml /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
5697c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
5707c478bd9Sstevel@tonic-gate /*                                                                          */
5717c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
5727c478bd9Sstevel@tonic-gate /* This is function checks pending routing extension header                 */
5737c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5747663b816Sml static INLINE int frpr_routing6(fin)
5757c478bd9Sstevel@tonic-gate fr_info_t *fin;
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	struct ip6_ext *hdr;
578ab25eeb5Syz 	int shift;
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 	hdr = fin->fin_dp;
581ab25eeb5Syz 	if (frpr_ipv6exthdr(fin, 0, IPPROTO_ROUTING) == IPPROTO_NONE)
582ab25eeb5Syz 		return IPPROTO_NONE;
5837663b816Sml 
5847c478bd9Sstevel@tonic-gate 	shift = 8 + (hdr->ip6e_len << 3);
5857c478bd9Sstevel@tonic-gate 	/*
5867c478bd9Sstevel@tonic-gate 	 * Nasty extension header length?
5877c478bd9Sstevel@tonic-gate 	 */
588ab25eeb5Syz 	if ((hdr->ip6e_len << 3) & 15) {
5897c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_BAD;
590ab25eeb5Syz 		/*
591ab25eeb5Syz 		 * Compensate for the changes made in frpr_ipv6exthdr()
592ab25eeb5Syz 		 */
593ab25eeb5Syz 		fin->fin_dlen += shift;
594ab25eeb5Syz 		fin->fin_dp = (char *)fin->fin_dp - shift;
5957663b816Sml 		return IPPROTO_NONE;
5967c478bd9Sstevel@tonic-gate 	}
5977c478bd9Sstevel@tonic-gate 
5987663b816Sml 	return hdr->ip6e_nxt;
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
6037c478bd9Sstevel@tonic-gate /* Function:    frpr_fragment6                                              */
6047663b816Sml /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
6057c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
6067c478bd9Sstevel@tonic-gate /*                                                                          */
6077c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
6087c478bd9Sstevel@tonic-gate /* Examine the IPv6 fragment header and extract fragment offset information.*/
609ab25eeb5Syz /*                                                                          */
610ab25eeb5Syz /* We don't know where the transport layer header (or whatever is next is), */
611ab25eeb5Syz /* as it could be behind destination options (amongst others).  Because     */
612ab25eeb5Syz /* there is no fragment cache, there is no knowledge about whether or not an*/
613ab25eeb5Syz /* upper layer header has been seen (or where it ends) and thus we are not  */
614ab25eeb5Syz /* able to continue processing beyond this header with any confidence.      */
6157c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
6167663b816Sml static INLINE int frpr_fragment6(fin)
6177c478bd9Sstevel@tonic-gate fr_info_t *fin;
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	struct ip6_frag *frag;
6207663b816Sml 
621ab25eeb5Syz 	fin->fin_flx |= FI_FRAG;
6227c478bd9Sstevel@tonic-gate 
62372680cf5SDarren Reed 	/*
62472680cf5SDarren Reed 	 * A fragmented IPv6 packet implies that there must be something
62572680cf5SDarren Reed 	 * else after the fragment.
62672680cf5SDarren Reed 	 */
627ab25eeb5Syz 	if (frpr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT) == IPPROTO_NONE)
628b7fbedc2SDarren Reed 		return IPPROTO_NONE;
6297c478bd9Sstevel@tonic-gate 
630ab25eeb5Syz 	frag = (struct ip6_frag *)((char *)fin->fin_dp - sizeof(*frag));
6317663b816Sml 
632ab25eeb5Syz 	/*
633b7fbedc2SDarren Reed 	 * If this fragment isn't the last then the packet length must
634b7fbedc2SDarren Reed 	 * be a multiple of 8.
635ab25eeb5Syz 	 */
636b7fbedc2SDarren Reed 	if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) {
637b7fbedc2SDarren Reed 		fin->fin_flx |= FI_MOREFRAG;
638b7fbedc2SDarren Reed 
639b7fbedc2SDarren Reed 		if ((fin->fin_plen & 0x7) != 0)
640b7fbedc2SDarren Reed 			fin->fin_flx |= FI_BAD;
641ab25eeb5Syz 	}
642ab25eeb5Syz 
643b7fbedc2SDarren Reed 	fin->fin_id = frag->ip6f_ident;
64472680cf5SDarren Reed 	fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
64572680cf5SDarren Reed 	if (fin->fin_off != 0)
64672680cf5SDarren Reed 		fin->fin_flx |= FI_FRAGBODY;
64772680cf5SDarren Reed 
6487663b816Sml 	return frag->ip6f_nxt;
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
6537c478bd9Sstevel@tonic-gate /* Function:    frpr_dstopts6                                               */
6547663b816Sml /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
6557c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
6567c478bd9Sstevel@tonic-gate /*              nextheader(I) - stores next header value                    */
6577c478bd9Sstevel@tonic-gate /*                                                                          */
6587c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
6597c478bd9Sstevel@tonic-gate /* This is function checks pending destination options extension header     */
6607c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
6617663b816Sml static INLINE int frpr_dstopts6(fin)
6627c478bd9Sstevel@tonic-gate fr_info_t *fin;
6637c478bd9Sstevel@tonic-gate {
664ab25eeb5Syz 	return frpr_ipv6exthdr(fin, 1, IPPROTO_DSTOPTS);
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
6697c478bd9Sstevel@tonic-gate /* Function:    frpr_icmp6                                                  */
6707c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
6717c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
6727c478bd9Sstevel@tonic-gate /*                                                                          */
6737c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
6747c478bd9Sstevel@tonic-gate /* This routine is mainly concerned with determining the minimum valid size */
6757c478bd9Sstevel@tonic-gate /* for an ICMPv6 packet.                                                    */
6767c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
6777c478bd9Sstevel@tonic-gate static INLINE void frpr_icmp6(fin)
6787c478bd9Sstevel@tonic-gate fr_info_t *fin;
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	int minicmpsz = sizeof(struct icmp6_hdr);
6817c478bd9Sstevel@tonic-gate 	struct icmp6_hdr *icmp6;
6827c478bd9Sstevel@tonic-gate 
683ab25eeb5Syz 	if (frpr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1)
6847663b816Sml 		return;
6857663b816Sml 
6867c478bd9Sstevel@tonic-gate 	if (fin->fin_dlen > 1) {
6877c478bd9Sstevel@tonic-gate 		icmp6 = fin->fin_dp;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 		fin->fin_data[0] = *(u_short *)icmp6;
6907c478bd9Sstevel@tonic-gate 
691d6c23f6fSyx 		if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0)
692d6c23f6fSyx 			fin->fin_flx |= FI_ICMPQUERY;
693d6c23f6fSyx 
6947c478bd9Sstevel@tonic-gate 		switch (icmp6->icmp6_type)
6957c478bd9Sstevel@tonic-gate 		{
6967c478bd9Sstevel@tonic-gate 		case ICMP6_ECHO_REPLY :
6977c478bd9Sstevel@tonic-gate 		case ICMP6_ECHO_REQUEST :
698d6c23f6fSyx 			if (fin->fin_dlen >= 6)
699d6c23f6fSyx 				fin->fin_data[1] = icmp6->icmp6_id;
7007c478bd9Sstevel@tonic-gate 			minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t);
7017c478bd9Sstevel@tonic-gate 			break;
7027c478bd9Sstevel@tonic-gate 		case ICMP6_DST_UNREACH :
7037c478bd9Sstevel@tonic-gate 		case ICMP6_PACKET_TOO_BIG :
7047c478bd9Sstevel@tonic-gate 		case ICMP6_TIME_EXCEEDED :
7057c478bd9Sstevel@tonic-gate 		case ICMP6_PARAM_PROB :
7067c478bd9Sstevel@tonic-gate 			if ((fin->fin_m != NULL) &&
7077c478bd9Sstevel@tonic-gate 			    (M_LEN(fin->fin_m) < fin->fin_plen)) {
7087663b816Sml 				if (fr_coalesce(fin) != 1)
7097c478bd9Sstevel@tonic-gate 					return;
7107c478bd9Sstevel@tonic-gate 			}
7117c478bd9Sstevel@tonic-gate 			fin->fin_flx |= FI_ICMPERR;
7127c478bd9Sstevel@tonic-gate 			minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t);
7137c478bd9Sstevel@tonic-gate 			break;
7147c478bd9Sstevel@tonic-gate 		default :
7157c478bd9Sstevel@tonic-gate 			break;
7167c478bd9Sstevel@tonic-gate 		}
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	frpr_short6(fin, minicmpsz);
7207c478bd9Sstevel@tonic-gate }
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
7247c478bd9Sstevel@tonic-gate /* Function:    frpr_udp6                                                   */
7257c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
7267c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
7277c478bd9Sstevel@tonic-gate /*                                                                          */
7287c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
7297c478bd9Sstevel@tonic-gate /* Analyse the packet for IPv6/UDP properties.                              */
730ab25eeb5Syz /* Is not expected to be called for fragmented packets.                     */
7317c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
7327c478bd9Sstevel@tonic-gate static INLINE void frpr_udp6(fin)
7337c478bd9Sstevel@tonic-gate fr_info_t *fin;
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	fr_checkv6sum(fin);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	frpr_short6(fin, sizeof(struct udphdr));
73908254dd3Syz 	if (frpr_pullup(fin, sizeof(struct udphdr)) == -1)
74008254dd3Syz 		return;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	frpr_udpcommon(fin);
7437c478bd9Sstevel@tonic-gate }
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
7477c478bd9Sstevel@tonic-gate /* Function:    frpr_tcp6                                                   */
7487c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
7497c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
7507c478bd9Sstevel@tonic-gate /*                                                                          */
7517c478bd9Sstevel@tonic-gate /* IPv6 Only                                                                */
7527c478bd9Sstevel@tonic-gate /* Analyse the packet for IPv6/TCP properties.                              */
753ab25eeb5Syz /* Is not expected to be called for fragmented packets.                     */
7547c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
7557c478bd9Sstevel@tonic-gate static INLINE void frpr_tcp6(fin)
7567c478bd9Sstevel@tonic-gate fr_info_t *fin;
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	fr_checkv6sum(fin);
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	frpr_short6(fin, sizeof(struct tcphdr));
76208254dd3Syz 	if (frpr_pullup(fin, sizeof(struct tcphdr)) == -1)
76308254dd3Syz 		return;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	frpr_tcpcommon(fin);
7667c478bd9Sstevel@tonic-gate }
767ab25eeb5Syz 
768ab25eeb5Syz 
769ab25eeb5Syz /* ------------------------------------------------------------------------ */
770ab25eeb5Syz /* Function:    frpr_esp6                                                   */
771ab25eeb5Syz /* Returns:     void                                                        */
772ab25eeb5Syz /* Parameters:  fin(I) - pointer to packet information                      */
773ab25eeb5Syz /*                                                                          */
774ab25eeb5Syz /* IPv6 Only                                                                */
775ab25eeb5Syz /* Analyse the packet for ESP properties.                                   */
776ab25eeb5Syz /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits)  */
777ab25eeb5Syz /* even though the newer ESP packets must also have a sequence number that  */
778ab25eeb5Syz /* is 32bits as well, it is not possible(?) to determine the version from a */
779ab25eeb5Syz /* simple packet header.                                                    */
780ab25eeb5Syz /* ------------------------------------------------------------------------ */
781ab25eeb5Syz static INLINE void frpr_esp6(fin)
782ab25eeb5Syz fr_info_t *fin;
783ab25eeb5Syz {
784ab25eeb5Syz 	int i;
785ab25eeb5Syz 	frpr_short6(fin, sizeof(grehdr_t));
786ab25eeb5Syz 
787ab25eeb5Syz 	(void) frpr_pullup(fin, 8);
788ab25eeb5Syz 
789ab25eeb5Syz 	for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
790ab25eeb5Syz 		if (ip6exthdr[i].ol_val == IPPROTO_ESP) {
791ab25eeb5Syz 			fin->fin_optmsk |= ip6exthdr[i].ol_bit;
792ab25eeb5Syz 			break;
793ab25eeb5Syz 		}
794ab25eeb5Syz }
795ab25eeb5Syz 
796ab25eeb5Syz 
797ab25eeb5Syz /* ------------------------------------------------------------------------ */
798ab25eeb5Syz /* Function:    frpr_ah6                                                    */
799ab25eeb5Syz /* Returns:     void                                                        */
800ab25eeb5Syz /* Parameters:  fin(I) - pointer to packet information                      */
801ab25eeb5Syz /*                                                                          */
802ab25eeb5Syz /* IPv6 Only                                                                */
803ab25eeb5Syz /* Analyse the packet for AH properties.                                    */
804ab25eeb5Syz /* The minimum length is taken to be the combination of all fields in the   */
805ab25eeb5Syz /* header being present and no authentication data (null algorithm used.)   */
806ab25eeb5Syz /* ------------------------------------------------------------------------ */
807ab25eeb5Syz static INLINE int frpr_ah6(fin)
808ab25eeb5Syz fr_info_t *fin;
809ab25eeb5Syz {
810ab25eeb5Syz 	authhdr_t *ah;
811ab25eeb5Syz 	int i, shift;
812ab25eeb5Syz 
813ab25eeb5Syz 	frpr_short6(fin, 12);
814ab25eeb5Syz 
815ab25eeb5Syz 	if (frpr_pullup(fin, sizeof(*ah)) == -1)
816ab25eeb5Syz 		return IPPROTO_NONE;
817ab25eeb5Syz 
818ab25eeb5Syz 	for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
819ab25eeb5Syz 		if (ip6exthdr[i].ol_val == IPPROTO_AH) {
820ab25eeb5Syz 			fin->fin_optmsk |= ip6exthdr[i].ol_bit;
821ab25eeb5Syz 			break;
822ab25eeb5Syz 		}
823ab25eeb5Syz 
824ab25eeb5Syz 	ah = (authhdr_t *)fin->fin_dp;
825ab25eeb5Syz 
826ab25eeb5Syz 	shift = (ah->ah_plen + 2) * 4;
827ab25eeb5Syz 	fin->fin_dlen -= shift;
828ab25eeb5Syz 	fin->fin_dp = (char*)fin->fin_dp + shift;
829ab25eeb5Syz 
830ab25eeb5Syz 	return ah->ah_next;
831ab25eeb5Syz }
832ab25eeb5Syz 
833ab25eeb5Syz 
834ab25eeb5Syz /* ------------------------------------------------------------------------ */
835ab25eeb5Syz /* Function:    frpr_gre6                                                   */
836ab25eeb5Syz /* Returns:     void                                                        */
837ab25eeb5Syz /* Parameters:  fin(I) - pointer to packet information                      */
838ab25eeb5Syz /*                                                                          */
839ab25eeb5Syz /* Analyse the packet for GRE properties.                                   */
840ab25eeb5Syz /* ------------------------------------------------------------------------ */
841ab25eeb5Syz static INLINE void frpr_gre6(fin)
842ab25eeb5Syz fr_info_t *fin;
843ab25eeb5Syz {
844ab25eeb5Syz 	grehdr_t *gre;
845ab25eeb5Syz 
846ab25eeb5Syz 	frpr_short6(fin, sizeof(grehdr_t));
847ab25eeb5Syz 
848ab25eeb5Syz 	if (frpr_pullup(fin, sizeof(grehdr_t)) == -1)
849ab25eeb5Syz 		return;
850ab25eeb5Syz 
851ab25eeb5Syz 	gre = fin->fin_dp;
852ab25eeb5Syz 	if (GRE_REV(gre->gr_flags) == 1)
853ab25eeb5Syz 		fin->fin_data[0] = gre->gr_call;
854ab25eeb5Syz }
8557c478bd9Sstevel@tonic-gate #endif	/* USE_INET6 */
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 
8587663b816Sml /* ------------------------------------------------------------------------ */
8597663b816Sml /* Function:    frpr_pullup                                                 */
8607663b816Sml /* Returns:     int     - 0 == pullup succeeded, -1 == failure              */
8617663b816Sml /* Parameters:  fin(I)  - pointer to packet information                     */
8627663b816Sml /*              plen(I) - length (excluding L3 header) to pullup            */
8637663b816Sml /*                                                                          */
8647663b816Sml /* Short inline function to cut down on code duplication to perform a call  */
8657663b816Sml /* to fr_pullup to ensure there is the required amount of data,             */
8667663b816Sml /* consecutively in the packet buffer.                                      */
8677663b816Sml /* ------------------------------------------------------------------------ */
8687663b816Sml static INLINE int frpr_pullup(fin, plen)
8697663b816Sml fr_info_t *fin;
8707663b816Sml int plen;
8717663b816Sml {
872ab25eeb5Syz #if defined(_KERNEL)
8737663b816Sml 	if (fin->fin_m != NULL) {
87440cdc2e8SAlexandr Nedvedicky 		int ipoff;
87540cdc2e8SAlexandr Nedvedicky 
87640cdc2e8SAlexandr Nedvedicky 		ipoff = (char *)fin->fin_ip - MTOD(fin->fin_m, char *);
87740cdc2e8SAlexandr Nedvedicky 
8787663b816Sml 		if (fin->fin_dp != NULL)
8797663b816Sml 			plen += (char *)fin->fin_dp -
8807663b816Sml 				((char *)fin->fin_ip + fin->fin_hlen);
88140cdc2e8SAlexandr Nedvedicky 		plen += fin->fin_hlen;
88240cdc2e8SAlexandr Nedvedicky 		/*
88340cdc2e8SAlexandr Nedvedicky 		 * We don't do 'plen += ipoff;' here. The fr_pullup() will
88440cdc2e8SAlexandr Nedvedicky 		 * do it for us.
88540cdc2e8SAlexandr Nedvedicky 		 */
88640cdc2e8SAlexandr Nedvedicky 		if (M_LEN(fin->fin_m) < plen + ipoff) {
8877663b816Sml 			if (fr_pullup(fin->fin_m, fin, plen) == NULL)
8887663b816Sml 				return -1;
8897663b816Sml 		}
8907663b816Sml 	}
8917663b816Sml #endif
8927663b816Sml 	return 0;
8937663b816Sml }
8947663b816Sml 
8957663b816Sml 
8967c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
8977c478bd9Sstevel@tonic-gate /* Function:    frpr_short                                                  */
8987c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
899ab25eeb5Syz /* Parameters:  fin(I)  - pointer to packet information                     */
900ab25eeb5Syz /*              xmin(I) - minimum header size                               */
9017c478bd9Sstevel@tonic-gate /*                                                                          */
902ab25eeb5Syz /* Check if a packet is "short" as defined by xmin.  The rule we are        */
903ab25eeb5Syz /* applying here is that the packet must not be fragmented within the layer */
904ab25eeb5Syz /* 4 header.  That is, it must not be a fragment that has its offset set to */
905ab25eeb5Syz /* start within the layer 4 header (hdrmin) or if it is at offset 0, the    */
906ab25eeb5Syz /* entire layer 4 header must be present (min).                             */
9077c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
908ab25eeb5Syz static INLINE void frpr_short(fin, xmin)
9097c478bd9Sstevel@tonic-gate fr_info_t *fin;
910ab25eeb5Syz int xmin;
9117c478bd9Sstevel@tonic-gate {
9127c478bd9Sstevel@tonic-gate 
913ab25eeb5Syz 	if (fin->fin_off == 0) {
914ab25eeb5Syz 		if (fin->fin_dlen < xmin)
915ab25eeb5Syz 			fin->fin_flx |= FI_SHORT;
916ab25eeb5Syz 	} else if (fin->fin_off < xmin) {
917ab25eeb5Syz 		fin->fin_flx |= FI_SHORT;
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate }
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
9237c478bd9Sstevel@tonic-gate /* Function:    frpr_icmp                                                   */
9247c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
9257c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
9267c478bd9Sstevel@tonic-gate /*                                                                          */
9277c478bd9Sstevel@tonic-gate /* IPv4 Only                                                                */
9287c478bd9Sstevel@tonic-gate /* Do a sanity check on the packet for ICMP (v4).  In nearly all cases,     */
9297c478bd9Sstevel@tonic-gate /* except extrememly bad packets, both type and code will be present.       */
930ab25eeb5Syz /* The expected minimum size of an ICMP packet is very much dependent on    */
9317c478bd9Sstevel@tonic-gate /* the type of it.                                                          */
9327c478bd9Sstevel@tonic-gate /*                                                                          */
9337c478bd9Sstevel@tonic-gate /* XXX - other ICMP sanity checks?                                          */
9347c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
9357c478bd9Sstevel@tonic-gate static INLINE void frpr_icmp(fin)
9367c478bd9Sstevel@tonic-gate fr_info_t *fin;
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	int minicmpsz = sizeof(struct icmp);
9397c478bd9Sstevel@tonic-gate 	icmphdr_t *icmp;
940ab25eeb5Syz 	ip_t *oip;
941f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
9427c478bd9Sstevel@tonic-gate 
94362685e53Sml 	if (fin->fin_off != 0) {
94462685e53Sml 		frpr_short(fin, ICMPERR_ICMPHLEN);
94562685e53Sml 		return;
94662685e53Sml 	}
94762685e53Sml 
948ab25eeb5Syz 	if (frpr_pullup(fin, ICMPERR_ICMPHLEN) == -1)
949ab25eeb5Syz 		return;
950ab25eeb5Syz 
9517c478bd9Sstevel@tonic-gate 	fr_checkv4sum(fin);
9527c478bd9Sstevel@tonic-gate 
953f524e99bSAlexandr Nedvedicky 	/*
954f524e99bSAlexandr Nedvedicky 	 * This is a right place to set icmp pointer, since the memory
955f524e99bSAlexandr Nedvedicky 	 * referenced by fin_dp could get reallocated. The code down below can
956f524e99bSAlexandr Nedvedicky 	 * rely on fact icmp variable always points to ICMP header.
957f524e99bSAlexandr Nedvedicky 	 */
958f524e99bSAlexandr Nedvedicky 	icmp = fin->fin_dp;
959f524e99bSAlexandr Nedvedicky 	fin->fin_data[0] = *(u_short *)icmp;
960f524e99bSAlexandr Nedvedicky 	fin->fin_data[1] = icmp->icmp_id;
961f524e99bSAlexandr Nedvedicky 
962f524e99bSAlexandr Nedvedicky 	switch (icmp->icmp_type)
963f524e99bSAlexandr Nedvedicky 	{
964f524e99bSAlexandr Nedvedicky 	case ICMP_ECHOREPLY :
965f524e99bSAlexandr Nedvedicky 	case ICMP_ECHO :
966f524e99bSAlexandr Nedvedicky 	/* Router discovery messaes - RFC 1256 */
967f524e99bSAlexandr Nedvedicky 	case ICMP_ROUTERADVERT :
968f524e99bSAlexandr Nedvedicky 	case ICMP_ROUTERSOLICIT :
969f524e99bSAlexandr Nedvedicky 		minicmpsz = ICMP_MINLEN;
970f524e99bSAlexandr Nedvedicky 		break;
971f524e99bSAlexandr Nedvedicky 	/*
972f524e99bSAlexandr Nedvedicky 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
973f524e99bSAlexandr Nedvedicky 	 * 3 * timestamp(3 * 4)
974f524e99bSAlexandr Nedvedicky 	 */
975f524e99bSAlexandr Nedvedicky 	case ICMP_TSTAMP :
976f524e99bSAlexandr Nedvedicky 	case ICMP_TSTAMPREPLY :
977f524e99bSAlexandr Nedvedicky 		minicmpsz = 20;
978f524e99bSAlexandr Nedvedicky 		break;
979f524e99bSAlexandr Nedvedicky 	/*
980f524e99bSAlexandr Nedvedicky 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
981f524e99bSAlexandr Nedvedicky 	 * mask(4)
982f524e99bSAlexandr Nedvedicky 	 */
983f524e99bSAlexandr Nedvedicky 	case ICMP_MASKREQ :
984f524e99bSAlexandr Nedvedicky 	case ICMP_MASKREPLY :
9857c478bd9Sstevel@tonic-gate 			minicmpsz = 12;
9867c478bd9Sstevel@tonic-gate 			break;
987f524e99bSAlexandr Nedvedicky 	/*
988f524e99bSAlexandr Nedvedicky 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
989f524e99bSAlexandr Nedvedicky 	 */
990f524e99bSAlexandr Nedvedicky 	case ICMP_UNREACH :
991f524e99bSAlexandr Nedvedicky 		if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) {
992f524e99bSAlexandr Nedvedicky 			if (icmp->icmp_nextmtu < ifs->ifs_fr_icmpminfragmtu)
993ab25eeb5Syz 				fin->fin_flx |= FI_BAD;
9947c478bd9Sstevel@tonic-gate 		}
995f524e99bSAlexandr Nedvedicky 		/* FALLTHRU */
996f524e99bSAlexandr Nedvedicky 	case ICMP_SOURCEQUENCH :
997f524e99bSAlexandr Nedvedicky 	case ICMP_REDIRECT :
998f524e99bSAlexandr Nedvedicky 	case ICMP_TIMXCEED :
999f524e99bSAlexandr Nedvedicky 	case ICMP_PARAMPROB :
1000f524e99bSAlexandr Nedvedicky 		fin->fin_flx |= FI_ICMPERR;
1001f524e99bSAlexandr Nedvedicky 		if (fr_coalesce(fin) != 1)
1002f524e99bSAlexandr Nedvedicky 			return;
1003f524e99bSAlexandr Nedvedicky 		/*
1004f524e99bSAlexandr Nedvedicky 		 * ICMP error packets should not be generated for IP
1005f524e99bSAlexandr Nedvedicky 		 * packets that are a fragment that isn't the first
1006f524e99bSAlexandr Nedvedicky 		 * fragment.
1007f524e99bSAlexandr Nedvedicky 		 */
1008f524e99bSAlexandr Nedvedicky 		oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN);
1009f524e99bSAlexandr Nedvedicky 		if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0)
1010f524e99bSAlexandr Nedvedicky 			fin->fin_flx |= FI_BAD;
1011f524e99bSAlexandr Nedvedicky 		break;
1012f524e99bSAlexandr Nedvedicky 	default :
1013f524e99bSAlexandr Nedvedicky 		break;
10147c478bd9Sstevel@tonic-gate 	}
10157c478bd9Sstevel@tonic-gate 
10167663b816Sml 	frpr_short(fin, minicmpsz);
10177c478bd9Sstevel@tonic-gate }
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
10217c478bd9Sstevel@tonic-gate /* Function:    frpr_tcpcommon                                              */
10227c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
10237c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
10247c478bd9Sstevel@tonic-gate /*                                                                          */
10257c478bd9Sstevel@tonic-gate /* TCP header sanity checking.  Look for bad combinations of TCP flags,     */
10267c478bd9Sstevel@tonic-gate /* and make some checks with how they interact with other fields.           */
10277c478bd9Sstevel@tonic-gate /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is     */
10287c478bd9Sstevel@tonic-gate /* valid and mark the packet as bad if not.                                 */
10297c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
10307c478bd9Sstevel@tonic-gate static INLINE void frpr_tcpcommon(fin)
10317c478bd9Sstevel@tonic-gate fr_info_t *fin;
10327c478bd9Sstevel@tonic-gate {
10337c478bd9Sstevel@tonic-gate 	int flags, tlen;
10347c478bd9Sstevel@tonic-gate 	tcphdr_t *tcp;
10357c478bd9Sstevel@tonic-gate 
103662685e53Sml 	fin->fin_flx |= FI_TCPUDP;
103762685e53Sml 	if (fin->fin_off != 0)
103862685e53Sml 		return;
103962685e53Sml 
1040ab25eeb5Syz 	if (frpr_pullup(fin, sizeof(*tcp)) == -1)
1041ab25eeb5Syz 		return;
10427c478bd9Sstevel@tonic-gate 	tcp = fin->fin_dp;
10437c478bd9Sstevel@tonic-gate 
1044ab25eeb5Syz 	if (fin->fin_dlen > 3) {
1045ab25eeb5Syz 		fin->fin_sport = ntohs(tcp->th_sport);
1046ab25eeb5Syz 		fin->fin_dport = ntohs(tcp->th_dport);
10477c478bd9Sstevel@tonic-gate 	}
1048ab25eeb5Syz 
1049ab25eeb5Syz 	if ((fin->fin_flx & FI_SHORT) != 0)
1050ab25eeb5Syz 		return;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	/*
10537c478bd9Sstevel@tonic-gate 	 * Use of the TCP data offset *must* result in a value that is at
10547c478bd9Sstevel@tonic-gate 	 * least the same size as the TCP header.
10557c478bd9Sstevel@tonic-gate 	 */
1056ab25eeb5Syz 	tlen = TCP_OFF(tcp) << 2;
1057ab25eeb5Syz 	if (tlen < sizeof(tcphdr_t)) {
10587c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_BAD;
1059ab25eeb5Syz 		return;
1060ab25eeb5Syz 	}
1061ab25eeb5Syz 
1062ab25eeb5Syz 	flags = tcp->th_flags;
1063ab25eeb5Syz 	fin->fin_tcpf = tcp->th_flags;
10647c478bd9Sstevel@tonic-gate 
1065ab25eeb5Syz 	/*
1066ab25eeb5Syz 	 * If the urgent flag is set, then the urgent pointer must
1067ab25eeb5Syz 	 * also be set and vice versa.  Good TCP packets do not have
1068ab25eeb5Syz 	 * just one of these set.
1069ab25eeb5Syz 	 */
1070ab25eeb5Syz 	if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) {
1071ab25eeb5Syz 		fin->fin_flx |= FI_BAD;
1072ab25eeb5Syz 	} else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) {
1073ab25eeb5Syz 		/* Ignore this case, it shows up in "real" traffic with */
1074ab25eeb5Syz 		/* bogus values in the urgent pointer field. */
1075ab25eeb5Syz 		flags = flags; /* LINT */
1076ab25eeb5Syz 	} else if (((flags & (TH_SYN|TH_FIN)) != 0) &&
1077ab25eeb5Syz 		   ((flags & (TH_RST|TH_ACK)) == TH_RST)) {
1078ab25eeb5Syz 		/* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
1079ab25eeb5Syz 		fin->fin_flx |= FI_BAD;
1080ab25eeb5Syz 	} else if (!(flags & TH_ACK)) {
10817c478bd9Sstevel@tonic-gate 		/*
1082ab25eeb5Syz 		 * If the ack bit isn't set, then either the SYN or
1083ab25eeb5Syz 		 * RST bit must be set.  If the SYN bit is set, then
1084ab25eeb5Syz 		 * we expect the ACK field to be 0.  If the ACK is
1085ab25eeb5Syz 		 * not set and if URG, PSH or FIN are set, consdier
1086ab25eeb5Syz 		 * that to indicate a bad TCP packet.
10877c478bd9Sstevel@tonic-gate 		 */
1088ab25eeb5Syz 		if ((flags == TH_SYN) && (tcp->th_ack != 0)) {
10897c478bd9Sstevel@tonic-gate 			/*
1090ab25eeb5Syz 			 * Cisco PIX sets the ACK field to a random value.
1091ab25eeb5Syz 			 * In light of this, do not set FI_BAD until a patch
1092ab25eeb5Syz 			 * is available from Cisco to ensure that
1093ab25eeb5Syz 			 * interoperability between existing systems is
1094ab25eeb5Syz 			 * achieved.
10957c478bd9Sstevel@tonic-gate 			 */
1096ab25eeb5Syz 			/*fin->fin_flx |= FI_BAD*/;
1097ab25eeb5Syz 			flags = flags; /* LINT */
1098ab25eeb5Syz 		} else if (!(flags & (TH_RST|TH_SYN))) {
1099ab25eeb5Syz 			fin->fin_flx |= FI_BAD;
1100ab25eeb5Syz 		} else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) {
1101ab25eeb5Syz 			fin->fin_flx |= FI_BAD;
11027c478bd9Sstevel@tonic-gate 		}
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	/*
11067c478bd9Sstevel@tonic-gate 	 * At this point, it's not exactly clear what is to be gained by
11077c478bd9Sstevel@tonic-gate 	 * marking up which TCP options are and are not present.  The one we
11087c478bd9Sstevel@tonic-gate 	 * are most interested in is the TCP window scale.  This is only in
11097c478bd9Sstevel@tonic-gate 	 * a SYN packet [RFC1323] so we don't need this here...?
11107c478bd9Sstevel@tonic-gate 	 * Now if we were to analyse the header for passive fingerprinting,
11117c478bd9Sstevel@tonic-gate 	 * then that might add some weight to adding this...
11127c478bd9Sstevel@tonic-gate 	 */
1113ab25eeb5Syz 	if (tlen == sizeof(tcphdr_t))
1114ab25eeb5Syz 		return;
1115ab25eeb5Syz 
1116ab25eeb5Syz 	if (frpr_pullup(fin, tlen) == -1)
11177c478bd9Sstevel@tonic-gate 		return;
1118ab25eeb5Syz 
1119ab25eeb5Syz #if 0
1120ab25eeb5Syz 	ip = fin->fin_ip;
1121ab25eeb5Syz 	s = (u_char *)(tcp + 1);
11227c478bd9Sstevel@tonic-gate 	off = IP_HL(ip) << 2;
11237c478bd9Sstevel@tonic-gate # ifdef _KERNEL
11247c478bd9Sstevel@tonic-gate 	if (fin->fin_mp != NULL) {
11257c478bd9Sstevel@tonic-gate 		mb_t *m = *fin->fin_mp;
11267c478bd9Sstevel@tonic-gate 
1127ab25eeb5Syz 		if (off + tlen > M_LEN(m))
11287c478bd9Sstevel@tonic-gate 			return;
11297c478bd9Sstevel@tonic-gate 	}
11307c478bd9Sstevel@tonic-gate # endif
1131ab25eeb5Syz 	for (tlen -= (int)sizeof(*tcp); tlen > 0; ) {
11327c478bd9Sstevel@tonic-gate 		opt = *s;
11337c478bd9Sstevel@tonic-gate 		if (opt == '\0')
11347c478bd9Sstevel@tonic-gate 			break;
11357c478bd9Sstevel@tonic-gate 		else if (opt == TCPOPT_NOP)
11367c478bd9Sstevel@tonic-gate 			ol = 1;
11377c478bd9Sstevel@tonic-gate 		else {
1138ab25eeb5Syz 			if (tlen < 2)
11397c478bd9Sstevel@tonic-gate 				break;
11407c478bd9Sstevel@tonic-gate 			ol = (int)*(s + 1);
1141ab25eeb5Syz 			if (ol < 2 || ol > tlen)
11427c478bd9Sstevel@tonic-gate 				break;
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 		for (i = 9, mv = 4; mv >= 0; ) {
11467c478bd9Sstevel@tonic-gate 			op = ipopts + i;
11477c478bd9Sstevel@tonic-gate 			if (opt == (u_char)op->ol_val) {
11487c478bd9Sstevel@tonic-gate 				optmsk |= op->ol_bit;
11497c478bd9Sstevel@tonic-gate 				break;
11507c478bd9Sstevel@tonic-gate 			}
11517c478bd9Sstevel@tonic-gate 		}
1152ab25eeb5Syz 		tlen -= ol;
11537c478bd9Sstevel@tonic-gate 		s += ol;
11547c478bd9Sstevel@tonic-gate 	}
11557c478bd9Sstevel@tonic-gate #endif /* 0 */
11567c478bd9Sstevel@tonic-gate }
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
11617c478bd9Sstevel@tonic-gate /* Function:    frpr_udpcommon                                              */
11627c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
11637c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
11647c478bd9Sstevel@tonic-gate /*                                                                          */
11657c478bd9Sstevel@tonic-gate /* Extract the UDP source and destination ports, if present.  If compiled   */
11667c478bd9Sstevel@tonic-gate /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid.          */
11677c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
11687c478bd9Sstevel@tonic-gate static INLINE void frpr_udpcommon(fin)
11697c478bd9Sstevel@tonic-gate fr_info_t *fin;
11707c478bd9Sstevel@tonic-gate {
11717c478bd9Sstevel@tonic-gate 	udphdr_t *udp;
11727c478bd9Sstevel@tonic-gate 
117362685e53Sml 	fin->fin_flx |= FI_TCPUDP;
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if (!fin->fin_off && (fin->fin_dlen > 3)) {
1176ab25eeb5Syz 		if (frpr_pullup(fin, sizeof(*udp)) == -1) {
1177ab25eeb5Syz 			fin->fin_flx |= FI_SHORT;
1178ab25eeb5Syz 			return;
1179ab25eeb5Syz 		}
1180ab25eeb5Syz 
118162685e53Sml 		udp = fin->fin_dp;
118262685e53Sml 
11837c478bd9Sstevel@tonic-gate 		fin->fin_sport = ntohs(udp->uh_sport);
11847c478bd9Sstevel@tonic-gate 		fin->fin_dport = ntohs(udp->uh_dport);
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate }
11877c478bd9Sstevel@tonic-gate 
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
11907c478bd9Sstevel@tonic-gate /* Function:    frpr_tcp                                                    */
11917c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
11927c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
11937c478bd9Sstevel@tonic-gate /*                                                                          */
11947c478bd9Sstevel@tonic-gate /* IPv4 Only                                                                */
11957c478bd9Sstevel@tonic-gate /* Analyse the packet for IPv4/TCP properties.                              */
11967c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
11977c478bd9Sstevel@tonic-gate static INLINE void frpr_tcp(fin)
11987c478bd9Sstevel@tonic-gate fr_info_t *fin;
11997c478bd9Sstevel@tonic-gate {
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate 	fr_checkv4sum(fin);
12027c478bd9Sstevel@tonic-gate 
1203ab25eeb5Syz 	frpr_short(fin, sizeof(tcphdr_t));
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	frpr_tcpcommon(fin);
12067c478bd9Sstevel@tonic-gate }
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
12107c478bd9Sstevel@tonic-gate /* Function:    frpr_udp                                                    */
12117c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
12127c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
12137c478bd9Sstevel@tonic-gate /*                                                                          */
12147c478bd9Sstevel@tonic-gate /* IPv4 Only                                                                */
12157c478bd9Sstevel@tonic-gate /* Analyse the packet for IPv4/UDP properties.                              */
12167c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
12177c478bd9Sstevel@tonic-gate static INLINE void frpr_udp(fin)
12187c478bd9Sstevel@tonic-gate fr_info_t *fin;
12197c478bd9Sstevel@tonic-gate {
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 	fr_checkv4sum(fin);
12227c478bd9Sstevel@tonic-gate 
1223ab25eeb5Syz 	frpr_short(fin, sizeof(udphdr_t));
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	frpr_udpcommon(fin);
12267c478bd9Sstevel@tonic-gate }
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 
12297663b816Sml /* ------------------------------------------------------------------------ */
12307663b816Sml /* Function:    frpr_esp                                                    */
12317663b816Sml /* Returns:     void                                                        */
12327663b816Sml /* Parameters:  fin(I) - pointer to packet information                      */
12337663b816Sml /*                                                                          */
12347663b816Sml /* Analyse the packet for ESP properties.                                   */
12357663b816Sml /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits)  */
12367663b816Sml /* even though the newer ESP packets must also have a sequence number that  */
12377663b816Sml /* is 32bits as well, it is not possible(?) to determine the version from a */
12387663b816Sml /* simple packet header.                                                    */
12397663b816Sml /* ------------------------------------------------------------------------ */
12407663b816Sml static INLINE void frpr_esp(fin)
12417663b816Sml fr_info_t *fin;
12427663b816Sml {
1243ab25eeb5Syz 	if ((fin->fin_off == 0) && (frpr_pullup(fin, 8) == -1))
12447663b816Sml 		return;
12457663b816Sml 
1246ab25eeb5Syz 	frpr_short(fin, 8);
1247ab25eeb5Syz }
1248ab25eeb5Syz 
1249ab25eeb5Syz 
1250ab25eeb5Syz /* ------------------------------------------------------------------------ */
1251ab25eeb5Syz /* Function:    frpr_ah                                                     */
1252ab25eeb5Syz /* Returns:     void                                                        */
1253ab25eeb5Syz /* Parameters:  fin(I) - pointer to packet information                      */
1254ab25eeb5Syz /*                                                                          */
1255ab25eeb5Syz /* Analyse the packet for AH properties.                                    */
1256ab25eeb5Syz /* The minimum length is taken to be the combination of all fields in the   */
1257ab25eeb5Syz /* header being present and no authentication data (null algorithm used.)   */
1258ab25eeb5Syz /* ------------------------------------------------------------------------ */
1259ab25eeb5Syz static INLINE void frpr_ah(fin)
1260ab25eeb5Syz fr_info_t *fin;
1261ab25eeb5Syz {
1262ab25eeb5Syz 	authhdr_t *ah;
1263ab25eeb5Syz 	int len;
1264ab25eeb5Syz 
1265ab25eeb5Syz 	if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(*ah)) == -1))
1266ab25eeb5Syz 		return;
1267ab25eeb5Syz 
1268ab25eeb5Syz 	ah = (authhdr_t *)fin->fin_dp;
1269ab25eeb5Syz 
1270ab25eeb5Syz 	len = (ah->ah_plen + 2) << 2;
1271ab25eeb5Syz 	frpr_short(fin, len);
12727663b816Sml }
12737663b816Sml 
12747663b816Sml 
12757663b816Sml /* ------------------------------------------------------------------------ */
12767663b816Sml /* Function:    frpr_gre                                                    */
12777663b816Sml /* Returns:     void                                                        */
12787663b816Sml /* Parameters:  fin(I) - pointer to packet information                      */
12797663b816Sml /*                                                                          */
12807663b816Sml /* Analyse the packet for GRE properties.                                   */
12817663b816Sml /* ------------------------------------------------------------------------ */
12827663b816Sml static INLINE void frpr_gre(fin)
12837663b816Sml fr_info_t *fin;
12847663b816Sml {
1285ab25eeb5Syz 	grehdr_t *gre;
1286ab25eeb5Syz 
1287ab25eeb5Syz 	if ((fin->fin_off == 0) && (frpr_pullup(fin, sizeof(grehdr_t)) == -1))
12887663b816Sml 		return;
12897663b816Sml 
1290ab25eeb5Syz 	frpr_short(fin, sizeof(grehdr_t));
1291ab25eeb5Syz 
1292ab25eeb5Syz 	if (fin->fin_off == 0) {
1293ab25eeb5Syz 		gre = fin->fin_dp;
1294ab25eeb5Syz 		if (GRE_REV(gre->gr_flags) == 1)
1295ab25eeb5Syz 			fin->fin_data[0] = gre->gr_call;
1296ab25eeb5Syz 	}
12977663b816Sml }
12987663b816Sml 
12997663b816Sml 
13007c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
13017c478bd9Sstevel@tonic-gate /* Function:    frpr_ipv4hdr                                                */
13027c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
13037c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
13047c478bd9Sstevel@tonic-gate /*                                                                          */
13057c478bd9Sstevel@tonic-gate /* IPv4 Only                                                                */
13067c478bd9Sstevel@tonic-gate /* Analyze the IPv4 header and set fields in the fr_info_t structure.       */
13077c478bd9Sstevel@tonic-gate /* Check all options present and flag their presence if any exist.          */
13087c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
13097c478bd9Sstevel@tonic-gate static INLINE void frpr_ipv4hdr(fin)
13107c478bd9Sstevel@tonic-gate fr_info_t *fin;
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	u_short optmsk = 0, secmsk = 0, auth = 0;
13137c478bd9Sstevel@tonic-gate 	int hlen, ol, mv, p, i;
1314ab25eeb5Syz 	const struct optlist *op;
13157c478bd9Sstevel@tonic-gate 	u_char *s, opt;
13167c478bd9Sstevel@tonic-gate 	u_short off;
13177c478bd9Sstevel@tonic-gate 	fr_ip_t *fi;
13187c478bd9Sstevel@tonic-gate 	ip_t *ip;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	fi = &fin->fin_fi;
13217c478bd9Sstevel@tonic-gate 	hlen = fin->fin_hlen;
13227c478bd9Sstevel@tonic-gate 
13237c478bd9Sstevel@tonic-gate 	ip = fin->fin_ip;
13247c478bd9Sstevel@tonic-gate 	p = ip->ip_p;
13257c478bd9Sstevel@tonic-gate 	fi->fi_p = p;
13267c478bd9Sstevel@tonic-gate 	fi->fi_tos = ip->ip_tos;
13277c478bd9Sstevel@tonic-gate 	fin->fin_id = ip->ip_id;
13287c478bd9Sstevel@tonic-gate 	off = ip->ip_off;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 	/* Get both TTL and protocol */
13317c478bd9Sstevel@tonic-gate 	fi->fi_p = ip->ip_p;
13327c478bd9Sstevel@tonic-gate 	fi->fi_ttl = ip->ip_ttl;
13337c478bd9Sstevel@tonic-gate #if 0
13347c478bd9Sstevel@tonic-gate 	(*(((u_short *)fi) + 1)) = (*(((u_short *)ip) + 4));
13357c478bd9Sstevel@tonic-gate #endif
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	/* Zero out bits not used in IPv6 address */
13387c478bd9Sstevel@tonic-gate 	fi->fi_src.i6[1] = 0;
13397c478bd9Sstevel@tonic-gate 	fi->fi_src.i6[2] = 0;
13407c478bd9Sstevel@tonic-gate 	fi->fi_src.i6[3] = 0;
13417c478bd9Sstevel@tonic-gate 	fi->fi_dst.i6[1] = 0;
13427c478bd9Sstevel@tonic-gate 	fi->fi_dst.i6[2] = 0;
13437c478bd9Sstevel@tonic-gate 	fi->fi_dst.i6[3] = 0;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	fi->fi_saddr = ip->ip_src.s_addr;
13467c478bd9Sstevel@tonic-gate 	fi->fi_daddr = ip->ip_dst.s_addr;
13477c478bd9Sstevel@tonic-gate 
13487c478bd9Sstevel@tonic-gate 	/*
13497c478bd9Sstevel@tonic-gate 	 * set packet attribute flags based on the offset and
13507c478bd9Sstevel@tonic-gate 	 * calculate the byte offset that it represents.
13517c478bd9Sstevel@tonic-gate 	 */
13527c478bd9Sstevel@tonic-gate 	off &= IP_MF|IP_OFFMASK;
13537c478bd9Sstevel@tonic-gate 	if (off != 0) {
135472680cf5SDarren Reed 		int morefrag = off & IP_MF;
135572680cf5SDarren Reed 
13567c478bd9Sstevel@tonic-gate 		fi->fi_flx |= FI_FRAG;
135772680cf5SDarren Reed 		if (morefrag)
135872680cf5SDarren Reed 			fi->fi_flx |= FI_MOREFRAG;
13597c478bd9Sstevel@tonic-gate 		off &= IP_OFFMASK;
13607c478bd9Sstevel@tonic-gate 		if (off != 0) {
1361ab25eeb5Syz 			fin->fin_flx |= FI_FRAGBODY;
13627c478bd9Sstevel@tonic-gate 			off <<= 3;
1363ab25eeb5Syz 			if ((off + fin->fin_dlen > 65535) ||
1364ab25eeb5Syz 			    (fin->fin_dlen == 0) ||
136572680cf5SDarren Reed 			    ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) {
1366ab25eeb5Syz 				/*
1367ab25eeb5Syz 				 * The length of the packet, starting at its
1368ab25eeb5Syz 				 * offset cannot exceed 65535 (0xffff) as the
1369ab25eeb5Syz 				 * length of an IP packet is only 16 bits.
1370ab25eeb5Syz 				 *
1371ab25eeb5Syz 				 * Any fragment that isn't the last fragment
1372ab25eeb5Syz 				 * must have a length greater than 0 and it
1373ab25eeb5Syz 				 * must be an even multiple of 8.
1374ab25eeb5Syz 				 */
13757c478bd9Sstevel@tonic-gate 				fi->fi_flx |= FI_BAD;
1376ab25eeb5Syz 			}
13777c478bd9Sstevel@tonic-gate 		}
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 	fin->fin_off = off;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 	/*
13827c478bd9Sstevel@tonic-gate 	 * Call per-protocol setup and checking
13837c478bd9Sstevel@tonic-gate 	 */
13847c478bd9Sstevel@tonic-gate 	switch (p)
13857c478bd9Sstevel@tonic-gate 	{
13867c478bd9Sstevel@tonic-gate 	case IPPROTO_UDP :
13877c478bd9Sstevel@tonic-gate 		frpr_udp(fin);
13887c478bd9Sstevel@tonic-gate 		break;
13897c478bd9Sstevel@tonic-gate 	case IPPROTO_TCP :
13907c478bd9Sstevel@tonic-gate 		frpr_tcp(fin);
13917c478bd9Sstevel@tonic-gate 		break;
13927c478bd9Sstevel@tonic-gate 	case IPPROTO_ICMP :
13937c478bd9Sstevel@tonic-gate 		frpr_icmp(fin);
13947c478bd9Sstevel@tonic-gate 		break;
1395ab25eeb5Syz 	case IPPROTO_AH :
1396ab25eeb5Syz 		frpr_ah(fin);
1397ab25eeb5Syz 		break;
1398ab25eeb5Syz 	case IPPROTO_ESP :
1399ab25eeb5Syz 		frpr_esp(fin);
1400ab25eeb5Syz 		break;
1401ab25eeb5Syz 	case IPPROTO_GRE :
1402ab25eeb5Syz 		frpr_gre(fin);
1403ab25eeb5Syz 		break;
14047c478bd9Sstevel@tonic-gate 	}
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	ip = fin->fin_ip;
14077c478bd9Sstevel@tonic-gate 	if (ip == NULL)
14087c478bd9Sstevel@tonic-gate 		return;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/*
14117c478bd9Sstevel@tonic-gate 	 * If it is a standard IP header (no options), set the flag fields
14127c478bd9Sstevel@tonic-gate 	 * which relate to options to 0.
14137c478bd9Sstevel@tonic-gate 	 */
14147c478bd9Sstevel@tonic-gate 	if (hlen == sizeof(*ip)) {
14157c478bd9Sstevel@tonic-gate 		fi->fi_optmsk = 0;
14167c478bd9Sstevel@tonic-gate 		fi->fi_secmsk = 0;
14177c478bd9Sstevel@tonic-gate 		fi->fi_auth = 0;
14187c478bd9Sstevel@tonic-gate 		return;
14197c478bd9Sstevel@tonic-gate 	}
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 	/*
14227c478bd9Sstevel@tonic-gate 	 * So the IP header has some IP options attached.  Walk the entire
14237c478bd9Sstevel@tonic-gate 	 * list of options present with this packet and set flags to indicate
14247c478bd9Sstevel@tonic-gate 	 * which ones are here and which ones are not.  For the somewhat out
14257c478bd9Sstevel@tonic-gate 	 * of date and obscure security classification options, set a flag to
14267c478bd9Sstevel@tonic-gate 	 * represent which classification is present.
14277c478bd9Sstevel@tonic-gate 	 */
14287c478bd9Sstevel@tonic-gate 	fi->fi_flx |= FI_OPTIONS;
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate 	for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
14317c478bd9Sstevel@tonic-gate 		opt = *s;
14327c478bd9Sstevel@tonic-gate 		if (opt == '\0')
14337c478bd9Sstevel@tonic-gate 			break;
14347c478bd9Sstevel@tonic-gate 		else if (opt == IPOPT_NOP)
14357c478bd9Sstevel@tonic-gate 			ol = 1;
14367c478bd9Sstevel@tonic-gate 		else {
14377c478bd9Sstevel@tonic-gate 			if (hlen < 2)
14387c478bd9Sstevel@tonic-gate 				break;
14397c478bd9Sstevel@tonic-gate 			ol = (int)*(s + 1);
14407c478bd9Sstevel@tonic-gate 			if (ol < 2 || ol > hlen)
14417c478bd9Sstevel@tonic-gate 				break;
14427c478bd9Sstevel@tonic-gate 		}
14437c478bd9Sstevel@tonic-gate 		for (i = 9, mv = 4; mv >= 0; ) {
14447c478bd9Sstevel@tonic-gate 			op = ipopts + i;
14457c478bd9Sstevel@tonic-gate 			if ((opt == (u_char)op->ol_val) && (ol > 4)) {
14467c478bd9Sstevel@tonic-gate 				optmsk |= op->ol_bit;
14477c478bd9Sstevel@tonic-gate 				if (opt == IPOPT_SECURITY) {
1448ab25eeb5Syz 					const struct optlist *sp;
14497c478bd9Sstevel@tonic-gate 					u_char	sec;
14507c478bd9Sstevel@tonic-gate 					int j, m;
14517c478bd9Sstevel@tonic-gate 
14527c478bd9Sstevel@tonic-gate 					sec = *(s + 2);	/* classification */
14537c478bd9Sstevel@tonic-gate 					for (j = 3, m = 2; m >= 0; ) {
14547c478bd9Sstevel@tonic-gate 						sp = secopt + j;
14557c478bd9Sstevel@tonic-gate 						if (sec == sp->ol_val) {
14567c478bd9Sstevel@tonic-gate 							secmsk |= sp->ol_bit;
14577c478bd9Sstevel@tonic-gate 							auth = *(s + 3);
14587c478bd9Sstevel@tonic-gate 							auth *= 256;
14597c478bd9Sstevel@tonic-gate 							auth += *(s + 4);
14607c478bd9Sstevel@tonic-gate 							break;
14617c478bd9Sstevel@tonic-gate 						}
14627c478bd9Sstevel@tonic-gate 						if (sec < sp->ol_val)
14637c478bd9Sstevel@tonic-gate 							j -= m;
14647c478bd9Sstevel@tonic-gate 						else
14657c478bd9Sstevel@tonic-gate 							j += m;
14667c478bd9Sstevel@tonic-gate 						m--;
14677c478bd9Sstevel@tonic-gate 					}
14687c478bd9Sstevel@tonic-gate 				}
14697c478bd9Sstevel@tonic-gate 				break;
14707c478bd9Sstevel@tonic-gate 			}
14717c478bd9Sstevel@tonic-gate 			if (opt < op->ol_val)
14727c478bd9Sstevel@tonic-gate 				i -= mv;
14737c478bd9Sstevel@tonic-gate 			else
14747c478bd9Sstevel@tonic-gate 				i += mv;
14757c478bd9Sstevel@tonic-gate 			mv--;
14767c478bd9Sstevel@tonic-gate 		}
14777c478bd9Sstevel@tonic-gate 		hlen -= ol;
14787c478bd9Sstevel@tonic-gate 		s += ol;
14797c478bd9Sstevel@tonic-gate 	}
14807c478bd9Sstevel@tonic-gate 
14817c478bd9Sstevel@tonic-gate 	/*
1482ab25eeb5Syz 	 *
14837c478bd9Sstevel@tonic-gate 	 */
14847c478bd9Sstevel@tonic-gate 	if (auth && !(auth & 0x0100))
14857c478bd9Sstevel@tonic-gate 		auth &= 0xff00;
14867c478bd9Sstevel@tonic-gate 	fi->fi_optmsk = optmsk;
14877c478bd9Sstevel@tonic-gate 	fi->fi_secmsk = secmsk;
14887c478bd9Sstevel@tonic-gate 	fi->fi_auth = auth;
14897c478bd9Sstevel@tonic-gate }
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
14937c478bd9Sstevel@tonic-gate /* Function:    fr_makefrip                                                 */
149408254dd3Syz /* Returns:     int - 1 == hdr checking error, 0 == OK                      */
14957c478bd9Sstevel@tonic-gate /* Parameters:  hlen(I) - length of IP packet header                        */
14967c478bd9Sstevel@tonic-gate /*              ip(I)   - pointer to the IP header                          */
1497ab25eeb5Syz /*              fin(IO) - pointer to packet information                     */
14987c478bd9Sstevel@tonic-gate /*                                                                          */
14997c478bd9Sstevel@tonic-gate /* Compact the IP header into a structure which contains just the info.     */
15007c478bd9Sstevel@tonic-gate /* which is useful for comparing IP headers with and store this information */
15017c478bd9Sstevel@tonic-gate /* in the fr_info_t structure pointer to by fin.  At present, it is assumed */
15027c478bd9Sstevel@tonic-gate /* this function will be called with either an IPv4 or IPv6 packet.         */
15037c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
15047c478bd9Sstevel@tonic-gate int	fr_makefrip(hlen, ip, fin)
15057c478bd9Sstevel@tonic-gate int hlen;
15067c478bd9Sstevel@tonic-gate ip_t *ip;
15077c478bd9Sstevel@tonic-gate fr_info_t *fin;
15087c478bd9Sstevel@tonic-gate {
15097c478bd9Sstevel@tonic-gate 	int v;
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	fin->fin_depth = 0;
15127c478bd9Sstevel@tonic-gate 	fin->fin_hlen = (u_short)hlen;
15137c478bd9Sstevel@tonic-gate 	fin->fin_ip = ip;
15147c478bd9Sstevel@tonic-gate 	fin->fin_rule = 0xffffffff;
15157c478bd9Sstevel@tonic-gate 	fin->fin_group[0] = -1;
15167c478bd9Sstevel@tonic-gate 	fin->fin_group[1] = '\0';
15177c478bd9Sstevel@tonic-gate 	fin->fin_dlen = fin->fin_plen - hlen;
15187c478bd9Sstevel@tonic-gate 	fin->fin_dp = (char *)ip + hlen;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	v = fin->fin_v;
15217c478bd9Sstevel@tonic-gate 	if (v == 4)
15227c478bd9Sstevel@tonic-gate 		frpr_ipv4hdr(fin);
15237c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
152433f2fefdSDarren Reed 	else if (v == 6)
152533f2fefdSDarren Reed 		frpr_ipv6hdr(fin);
15267c478bd9Sstevel@tonic-gate #endif
15277c478bd9Sstevel@tonic-gate 	if (fin->fin_ip == NULL)
15287c478bd9Sstevel@tonic-gate 		return -1;
15297c478bd9Sstevel@tonic-gate 	return 0;
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
15347c478bd9Sstevel@tonic-gate /* Function:    fr_portcheck                                                */
15357c478bd9Sstevel@tonic-gate /* Returns:     int - 1 == port matched, 0 == port match failed             */
15367c478bd9Sstevel@tonic-gate /* Parameters:  frp(I) - pointer to port check `expression'                 */
15377c478bd9Sstevel@tonic-gate /*              pop(I) - pointer to port number to evaluate                 */
15387c478bd9Sstevel@tonic-gate /*                                                                          */
15397c478bd9Sstevel@tonic-gate /* Perform a comparison of a port number against some other(s), using a     */
15407c478bd9Sstevel@tonic-gate /* structure with compare information stored in it.                         */
15417c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
15427c478bd9Sstevel@tonic-gate static INLINE int fr_portcheck(frp, pop)
15437c478bd9Sstevel@tonic-gate frpcmp_t *frp;
15447c478bd9Sstevel@tonic-gate u_short *pop;
15457c478bd9Sstevel@tonic-gate {
15467c478bd9Sstevel@tonic-gate 	u_short tup, po;
15477c478bd9Sstevel@tonic-gate 	int err = 1;
15487c478bd9Sstevel@tonic-gate 
15497c478bd9Sstevel@tonic-gate 	tup = *pop;
15507c478bd9Sstevel@tonic-gate 	po = frp->frp_port;
15517c478bd9Sstevel@tonic-gate 
15527c478bd9Sstevel@tonic-gate 	/*
15537c478bd9Sstevel@tonic-gate 	 * Do opposite test to that required and continue if that succeeds.
15547c478bd9Sstevel@tonic-gate 	 */
15557c478bd9Sstevel@tonic-gate 	switch (frp->frp_cmp)
15567c478bd9Sstevel@tonic-gate 	{
15577c478bd9Sstevel@tonic-gate 	case FR_EQUAL :
15587c478bd9Sstevel@tonic-gate 		if (tup != po) /* EQUAL */
15597c478bd9Sstevel@tonic-gate 			err = 0;
15607c478bd9Sstevel@tonic-gate 		break;
15617c478bd9Sstevel@tonic-gate 	case FR_NEQUAL :
15627c478bd9Sstevel@tonic-gate 		if (tup == po) /* NOTEQUAL */
15637c478bd9Sstevel@tonic-gate 			err = 0;
15647c478bd9Sstevel@tonic-gate 		break;
15657c478bd9Sstevel@tonic-gate 	case FR_LESST :
15667c478bd9Sstevel@tonic-gate 		if (tup >= po) /* LESSTHAN */
15677c478bd9Sstevel@tonic-gate 			err = 0;
15687c478bd9Sstevel@tonic-gate 		break;
15697c478bd9Sstevel@tonic-gate 	case FR_GREATERT :
15707c478bd9Sstevel@tonic-gate 		if (tup <= po) /* GREATERTHAN */
15717c478bd9Sstevel@tonic-gate 			err = 0;
15727c478bd9Sstevel@tonic-gate 		break;
15737c478bd9Sstevel@tonic-gate 	case FR_LESSTE :
15747c478bd9Sstevel@tonic-gate 		if (tup > po) /* LT or EQ */
15757c478bd9Sstevel@tonic-gate 			err = 0;
15767c478bd9Sstevel@tonic-gate 		break;
15777c478bd9Sstevel@tonic-gate 	case FR_GREATERTE :
15787c478bd9Sstevel@tonic-gate 		if (tup < po) /* GT or EQ */
15797c478bd9Sstevel@tonic-gate 			err = 0;
15807c478bd9Sstevel@tonic-gate 		break;
15817c478bd9Sstevel@tonic-gate 	case FR_OUTRANGE :
15827c478bd9Sstevel@tonic-gate 		if (tup >= po && tup <= frp->frp_top) /* Out of range */
15837c478bd9Sstevel@tonic-gate 			err = 0;
15847c478bd9Sstevel@tonic-gate 		break;
15857c478bd9Sstevel@tonic-gate 	case FR_INRANGE :
15867c478bd9Sstevel@tonic-gate 		if (tup <= po || tup >= frp->frp_top) /* In range */
15877c478bd9Sstevel@tonic-gate 			err = 0;
15887c478bd9Sstevel@tonic-gate 		break;
15897c478bd9Sstevel@tonic-gate 	case FR_INCRANGE :
15907c478bd9Sstevel@tonic-gate 		if (tup < po || tup > frp->frp_top) /* Inclusive range */
15917c478bd9Sstevel@tonic-gate 			err = 0;
15927c478bd9Sstevel@tonic-gate 		break;
15937c478bd9Sstevel@tonic-gate 	default :
15947c478bd9Sstevel@tonic-gate 		break;
15957c478bd9Sstevel@tonic-gate 	}
15967c478bd9Sstevel@tonic-gate 	return err;
15977c478bd9Sstevel@tonic-gate }
15987c478bd9Sstevel@tonic-gate 
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
16017c478bd9Sstevel@tonic-gate /* Function:    fr_tcpudpchk                                                */
16027c478bd9Sstevel@tonic-gate /* Returns:     int - 1 == protocol matched, 0 == check failed              */
16037c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
16047c478bd9Sstevel@tonic-gate /*              ft(I)  - pointer to structure with comparison data          */
16057c478bd9Sstevel@tonic-gate /*                                                                          */
16067c478bd9Sstevel@tonic-gate /* Compares the current pcket (assuming it is TCP/UDP) information with a   */
16077c478bd9Sstevel@tonic-gate /* structure containing information that we want to match against.          */
16087c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
16097c478bd9Sstevel@tonic-gate int fr_tcpudpchk(fin, ft)
16107c478bd9Sstevel@tonic-gate fr_info_t *fin;
16117c478bd9Sstevel@tonic-gate frtuc_t *ft;
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	int err = 1;
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	/*
16167c478bd9Sstevel@tonic-gate 	 * Both ports should *always* be in the first fragment.
16177c478bd9Sstevel@tonic-gate 	 * So far, I cannot find any cases where they can not be.
16187c478bd9Sstevel@tonic-gate 	 *
16197c478bd9Sstevel@tonic-gate 	 * compare destination ports
16207c478bd9Sstevel@tonic-gate 	 */
16217c478bd9Sstevel@tonic-gate 	if (ft->ftu_dcmp)
16227c478bd9Sstevel@tonic-gate 		err = fr_portcheck(&ft->ftu_dst, &fin->fin_dport);
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate 	/*
16257c478bd9Sstevel@tonic-gate 	 * compare source ports
16267c478bd9Sstevel@tonic-gate 	 */
16277c478bd9Sstevel@tonic-gate 	if (err && ft->ftu_scmp)
16287c478bd9Sstevel@tonic-gate 		err = fr_portcheck(&ft->ftu_src, &fin->fin_sport);
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	/*
16317c478bd9Sstevel@tonic-gate 	 * If we don't have all the TCP/UDP header, then how can we
16327c478bd9Sstevel@tonic-gate 	 * expect to do any sort of match on it ?  If we were looking for
16337c478bd9Sstevel@tonic-gate 	 * TCP flags, then NO match.  If not, then match (which should
16347c478bd9Sstevel@tonic-gate 	 * satisfy the "short" class too).
16357c478bd9Sstevel@tonic-gate 	 */
16367c478bd9Sstevel@tonic-gate 	if (err && (fin->fin_p == IPPROTO_TCP)) {
16377c478bd9Sstevel@tonic-gate 		if (fin->fin_flx & FI_SHORT)
16387c478bd9Sstevel@tonic-gate 			return !(ft->ftu_tcpf | ft->ftu_tcpfm);
16397c478bd9Sstevel@tonic-gate 		/*
16407c478bd9Sstevel@tonic-gate 		 * Match the flags ?  If not, abort this match.
16417c478bd9Sstevel@tonic-gate 		 */
16427c478bd9Sstevel@tonic-gate 		if (ft->ftu_tcpfm &&
16437c478bd9Sstevel@tonic-gate 		    ft->ftu_tcpf != (fin->fin_tcpf & ft->ftu_tcpfm)) {
16447c478bd9Sstevel@tonic-gate 			FR_DEBUG(("f. %#x & %#x != %#x\n", fin->fin_tcpf,
16457c478bd9Sstevel@tonic-gate 				 ft->ftu_tcpfm, ft->ftu_tcpf));
16467c478bd9Sstevel@tonic-gate 			err = 0;
16477c478bd9Sstevel@tonic-gate 		}
16487c478bd9Sstevel@tonic-gate 	}
16497c478bd9Sstevel@tonic-gate 	return err;
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
16547c478bd9Sstevel@tonic-gate /* Function:    fr_ipfcheck                                                 */
16557c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == match, 1 == no match                             */
16567c478bd9Sstevel@tonic-gate /* Parameters:  fin(I)     - pointer to packet information                  */
16577c478bd9Sstevel@tonic-gate /*              fr(I)      - pointer to filter rule                         */
16587c478bd9Sstevel@tonic-gate /*              portcmp(I) - flag indicating whether to attempt matching on */
16597c478bd9Sstevel@tonic-gate /*                           TCP/UDP port data.                             */
16607c478bd9Sstevel@tonic-gate /*                                                                          */
16617c478bd9Sstevel@tonic-gate /* Check to see if a packet matches an IPFilter rule.  Checks of addresses, */
16627c478bd9Sstevel@tonic-gate /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
16637c478bd9Sstevel@tonic-gate /* this function.                                                           */
16647c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
16657c478bd9Sstevel@tonic-gate static INLINE int fr_ipfcheck(fin, fr, portcmp)
16667c478bd9Sstevel@tonic-gate fr_info_t *fin;
16677c478bd9Sstevel@tonic-gate frentry_t *fr;
16687c478bd9Sstevel@tonic-gate int portcmp;
16697c478bd9Sstevel@tonic-gate {
16707c478bd9Sstevel@tonic-gate 	u_32_t	*ld, *lm, *lip;
16717c478bd9Sstevel@tonic-gate 	fripf_t *fri;
16727c478bd9Sstevel@tonic-gate 	fr_ip_t *fi;
16737c478bd9Sstevel@tonic-gate 	int i;
1674f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	fi = &fin->fin_fi;
16777c478bd9Sstevel@tonic-gate 	fri = fr->fr_ipf;
16787c478bd9Sstevel@tonic-gate 	lip = (u_32_t *)fi;
16797c478bd9Sstevel@tonic-gate 	lm = (u_32_t *)&fri->fri_mip;
16807c478bd9Sstevel@tonic-gate 	ld = (u_32_t *)&fri->fri_ip;
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate 	/*
16837c478bd9Sstevel@tonic-gate 	 * first 32 bits to check coversion:
16847c478bd9Sstevel@tonic-gate 	 * IP version, TOS, TTL, protocol
16857c478bd9Sstevel@tonic-gate 	 */
16867c478bd9Sstevel@tonic-gate 	i = ((*lip & *lm) != *ld);
16877c478bd9Sstevel@tonic-gate 	FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
16887c478bd9Sstevel@tonic-gate 		   *lip, *lm, *ld));
16897c478bd9Sstevel@tonic-gate 	if (i)
16907c478bd9Sstevel@tonic-gate 		return 1;
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	/*
16937c478bd9Sstevel@tonic-gate 	 * Next 32 bits is a constructed bitmask indicating which IP options
16947c478bd9Sstevel@tonic-gate 	 * are present (if any) in this packet.
16957c478bd9Sstevel@tonic-gate 	 */
16967c478bd9Sstevel@tonic-gate 	lip++, lm++, ld++;
16977c478bd9Sstevel@tonic-gate 	i |= ((*lip & *lm) != *ld);
16987c478bd9Sstevel@tonic-gate 	FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
16997c478bd9Sstevel@tonic-gate 		   *lip, *lm, *ld));
17007c478bd9Sstevel@tonic-gate 	if (i)
17017c478bd9Sstevel@tonic-gate 		return 1;
17027c478bd9Sstevel@tonic-gate 
17037c478bd9Sstevel@tonic-gate 	lip++, lm++, ld++;
17047c478bd9Sstevel@tonic-gate 	/*
17057c478bd9Sstevel@tonic-gate 	 * Unrolled loops (4 each, for 32 bits) for address checks.
17067c478bd9Sstevel@tonic-gate 	 */
17077c478bd9Sstevel@tonic-gate 	/*
17087c478bd9Sstevel@tonic-gate 	 * Check the source address.
17097c478bd9Sstevel@tonic-gate 	 */
17107c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
17117c478bd9Sstevel@tonic-gate 	if (fr->fr_satype == FRI_LOOKUP) {
1712*de22af4eSJohn Ojemann 		fin->fin_flx |= FI_DONTCACHE;
1713*de22af4eSJohn Ojemann 		i = (*fr->fr_srcfunc)(fr->fr_srcptr, fi->fi_v, lip, fin, ifs);
17147c478bd9Sstevel@tonic-gate 		if (i == -1)
17157c478bd9Sstevel@tonic-gate 			return 1;
17167c478bd9Sstevel@tonic-gate 		lip += 3;
17177c478bd9Sstevel@tonic-gate 		lm += 3;
17187c478bd9Sstevel@tonic-gate 		ld += 3;
17197c478bd9Sstevel@tonic-gate 	} else {
17207c478bd9Sstevel@tonic-gate #endif
17217c478bd9Sstevel@tonic-gate 		i = ((*lip & *lm) != *ld);
17227c478bd9Sstevel@tonic-gate 		FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
17237c478bd9Sstevel@tonic-gate 			   *lip, *lm, *ld));
17247c478bd9Sstevel@tonic-gate 		if (fi->fi_v == 6) {
17257c478bd9Sstevel@tonic-gate 			lip++, lm++, ld++;
17267c478bd9Sstevel@tonic-gate 			i |= ((*lip & *lm) != *ld);
17277c478bd9Sstevel@tonic-gate 			FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
17287c478bd9Sstevel@tonic-gate 				   *lip, *lm, *ld));
17297c478bd9Sstevel@tonic-gate 			lip++, lm++, ld++;
17307c478bd9Sstevel@tonic-gate 			i |= ((*lip & *lm) != *ld);
17317c478bd9Sstevel@tonic-gate 			FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
17327c478bd9Sstevel@tonic-gate 				   *lip, *lm, *ld));
17337c478bd9Sstevel@tonic-gate 			lip++, lm++, ld++;
17347c478bd9Sstevel@tonic-gate 			i |= ((*lip & *lm) != *ld);
17357c478bd9Sstevel@tonic-gate 			FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
17367c478bd9Sstevel@tonic-gate 				   *lip, *lm, *ld));
17377c478bd9Sstevel@tonic-gate 		} else {
17387c478bd9Sstevel@tonic-gate 			lip += 3;
17397c478bd9Sstevel@tonic-gate 			lm += 3;
17407c478bd9Sstevel@tonic-gate 			ld += 3;
17417c478bd9Sstevel@tonic-gate 		}
17427c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
17437c478bd9Sstevel@tonic-gate 	}
17447c478bd9Sstevel@tonic-gate #endif
17457c478bd9Sstevel@tonic-gate 	i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6;
17467c478bd9Sstevel@tonic-gate 	if (i)
17477c478bd9Sstevel@tonic-gate 		return 1;
17487c478bd9Sstevel@tonic-gate 
17497c478bd9Sstevel@tonic-gate 	/*
17507c478bd9Sstevel@tonic-gate 	 * Check the destination address.
17517c478bd9Sstevel@tonic-gate 	 */
17527c478bd9Sstevel@tonic-gate 	lip++, lm++, ld++;
17537c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
17547c478bd9Sstevel@tonic-gate 	if (fr->fr_datype == FRI_LOOKUP) {
1755*de22af4eSJohn Ojemann 		fin->fin_flx |= FI_DONTCACHE;
1756*de22af4eSJohn Ojemann 		i = (*fr->fr_dstfunc)(fr->fr_dstptr, fi->fi_v, lip, fin, ifs);
17577c478bd9Sstevel@tonic-gate 		if (i == -1)
17587c478bd9Sstevel@tonic-gate 			return 1;
17597c478bd9Sstevel@tonic-gate 		lip += 3;
17607c478bd9Sstevel@tonic-gate 		lm += 3;
17617c478bd9Sstevel@tonic-gate 		ld += 3;
17627c478bd9Sstevel@tonic-gate 	} else {
17637c478bd9Sstevel@tonic-gate #endif
17647c478bd9Sstevel@tonic-gate 		i = ((*lip & *lm) != *ld);
17657c478bd9Sstevel@tonic-gate 		FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
17667c478bd9Sstevel@tonic-gate 			   *lip, *lm, *ld));
17677c478bd9Sstevel@tonic-gate 		if (fi->fi_v == 6) {
17687c478bd9Sstevel@tonic-gate 			lip++, lm++, ld++;
17697c478bd9Sstevel@tonic-gate 			i |= ((*lip & *lm) != *ld);
17707c478bd9Sstevel@tonic-gate 			FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
17717c478bd9Sstevel@tonic-gate 				   *lip, *lm, *ld));
17727c478bd9Sstevel@tonic-gate 			lip++, lm++, ld++;
17737c478bd9Sstevel@tonic-gate 			i |= ((*lip & *lm) != *ld);
17747c478bd9Sstevel@tonic-gate 			FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
17757c478bd9Sstevel@tonic-gate 				   *lip, *lm, *ld));
17767c478bd9Sstevel@tonic-gate 			lip++, lm++, ld++;
17777c478bd9Sstevel@tonic-gate 			i |= ((*lip & *lm) != *ld);
17787c478bd9Sstevel@tonic-gate 			FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
17797c478bd9Sstevel@tonic-gate 				   *lip, *lm, *ld));
17807c478bd9Sstevel@tonic-gate 		} else {
17817c478bd9Sstevel@tonic-gate 			lip += 3;
17827c478bd9Sstevel@tonic-gate 			lm += 3;
17837c478bd9Sstevel@tonic-gate 			ld += 3;
17847c478bd9Sstevel@tonic-gate 		}
17857c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
17867c478bd9Sstevel@tonic-gate 	}
17877c478bd9Sstevel@tonic-gate #endif
17887c478bd9Sstevel@tonic-gate 	i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7;
17897c478bd9Sstevel@tonic-gate 	if (i)
17907c478bd9Sstevel@tonic-gate 		return 1;
17917c478bd9Sstevel@tonic-gate 	/*
17927c478bd9Sstevel@tonic-gate 	 * IP addresses matched.  The next 32bits contains:
17937c478bd9Sstevel@tonic-gate 	 * mast of old IP header security & authentication bits.
17947c478bd9Sstevel@tonic-gate 	 */
17957c478bd9Sstevel@tonic-gate 	lip++, lm++, ld++;
17967c478bd9Sstevel@tonic-gate 	i |= ((*lip & *lm) != *ld);
17977c478bd9Sstevel@tonic-gate 	FR_DEBUG(("4. %#08x & %#08x != %#08x\n",
17987c478bd9Sstevel@tonic-gate 		   *lip, *lm, *ld));
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate 	/*
18017c478bd9Sstevel@tonic-gate 	 * Next we have 32 bits of packet flags.
18027c478bd9Sstevel@tonic-gate 	 */
18037c478bd9Sstevel@tonic-gate 	lip++, lm++, ld++;
18047c478bd9Sstevel@tonic-gate 	i |= ((*lip & *lm) != *ld);
18057c478bd9Sstevel@tonic-gate 	FR_DEBUG(("5. %#08x & %#08x != %#08x\n",
18067c478bd9Sstevel@tonic-gate 		   *lip, *lm, *ld));
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate 	if (i == 0) {
18097c478bd9Sstevel@tonic-gate 		/*
18107c478bd9Sstevel@tonic-gate 		 * If a fragment, then only the first has what we're
18117c478bd9Sstevel@tonic-gate 		 * looking for here...
18127c478bd9Sstevel@tonic-gate 		 */
18137c478bd9Sstevel@tonic-gate 		if (portcmp) {
18147c478bd9Sstevel@tonic-gate 			if (!fr_tcpudpchk(fin, &fr->fr_tuc))
18157c478bd9Sstevel@tonic-gate 				i = 1;
18167c478bd9Sstevel@tonic-gate 		} else {
18177c478bd9Sstevel@tonic-gate 			if (fr->fr_dcmp || fr->fr_scmp ||
18187c478bd9Sstevel@tonic-gate 			    fr->fr_tcpf || fr->fr_tcpfm)
18197c478bd9Sstevel@tonic-gate 				i = 1;
18207c478bd9Sstevel@tonic-gate 			if (fr->fr_icmpm || fr->fr_icmp) {
18217c478bd9Sstevel@tonic-gate 				if (((fi->fi_p != IPPROTO_ICMP) &&
18227c478bd9Sstevel@tonic-gate 				     (fi->fi_p != IPPROTO_ICMPV6)) ||
18237c478bd9Sstevel@tonic-gate 				    fin->fin_off || (fin->fin_dlen < 2))
18247c478bd9Sstevel@tonic-gate 					i = 1;
18257c478bd9Sstevel@tonic-gate 				else if ((fin->fin_data[0] & fr->fr_icmpm) !=
18267c478bd9Sstevel@tonic-gate 					 fr->fr_icmp) {
18277c478bd9Sstevel@tonic-gate 					FR_DEBUG(("i. %#x & %#x != %#x\n",
18287c478bd9Sstevel@tonic-gate 						 fin->fin_data[0],
18297c478bd9Sstevel@tonic-gate 						 fr->fr_icmpm, fr->fr_icmp));
18307c478bd9Sstevel@tonic-gate 					i = 1;
18317c478bd9Sstevel@tonic-gate 				}
18327c478bd9Sstevel@tonic-gate 			}
18337c478bd9Sstevel@tonic-gate 		}
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 	return i;
18367c478bd9Sstevel@tonic-gate }
18377c478bd9Sstevel@tonic-gate 
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
18407c478bd9Sstevel@tonic-gate /* Function:    fr_scanlist                                                 */
18417c478bd9Sstevel@tonic-gate /* Returns:     int - result flags of scanning filter list                  */
18427c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
18437c478bd9Sstevel@tonic-gate /*              pass(I) - default result to return for filtering            */
18447c478bd9Sstevel@tonic-gate /*                                                                          */
18457c478bd9Sstevel@tonic-gate /* Check the input/output list of rules for a match to the current packet.  */
18467c478bd9Sstevel@tonic-gate /* If a match is found, the value of fr_flags from the rule becomes the     */
18477c478bd9Sstevel@tonic-gate /* return value and fin->fin_fr points to the matched rule.                 */
18487c478bd9Sstevel@tonic-gate /*                                                                          */
18497c478bd9Sstevel@tonic-gate /* This function may be called recusively upto 16 times (limit inbuilt.)    */
18507c478bd9Sstevel@tonic-gate /* When unwinding, it should finish up with fin_depth as 0.                 */
18517c478bd9Sstevel@tonic-gate /*                                                                          */
18527c478bd9Sstevel@tonic-gate /* Could be per interface, but this gets real nasty when you don't have,    */
18537c478bd9Sstevel@tonic-gate /* or can't easily change, the kernel source code to .                      */
18547c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
18557c478bd9Sstevel@tonic-gate int fr_scanlist(fin, pass)
18567c478bd9Sstevel@tonic-gate fr_info_t *fin;
18577c478bd9Sstevel@tonic-gate u_32_t pass;
18587c478bd9Sstevel@tonic-gate {
18597c478bd9Sstevel@tonic-gate 	int rulen, portcmp, off, logged, skip;
18607c478bd9Sstevel@tonic-gate 	struct frentry *fr, *fnext;
1861ab25eeb5Syz 	u_32_t passt, passo;
1862f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 	/*
18657c478bd9Sstevel@tonic-gate 	 * Do not allow nesting deeper than 16 levels.
18667c478bd9Sstevel@tonic-gate 	 */
18677c478bd9Sstevel@tonic-gate 	if (fin->fin_depth >= 16)
18687c478bd9Sstevel@tonic-gate 		return pass;
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate 	fr = fin->fin_fr;
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	/*
18737c478bd9Sstevel@tonic-gate 	 * If there are no rules in this list, return now.
18747c478bd9Sstevel@tonic-gate 	 */
18757c478bd9Sstevel@tonic-gate 	if (fr == NULL)
18767c478bd9Sstevel@tonic-gate 		return pass;
18777c478bd9Sstevel@tonic-gate 
18787c478bd9Sstevel@tonic-gate 	skip = 0;
18797c478bd9Sstevel@tonic-gate 	logged = 0;
18807c478bd9Sstevel@tonic-gate 	portcmp = 0;
18817c478bd9Sstevel@tonic-gate 	fin->fin_depth++;
18827c478bd9Sstevel@tonic-gate 	fin->fin_fr = NULL;
18837c478bd9Sstevel@tonic-gate 	off = fin->fin_off;
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate 	if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
18867c478bd9Sstevel@tonic-gate 		portcmp = 1;
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	for (rulen = 0; fr; fr = fnext, rulen++) {
18897c478bd9Sstevel@tonic-gate 		fnext = fr->fr_next;
18907c478bd9Sstevel@tonic-gate 		if (skip != 0) {
18917c478bd9Sstevel@tonic-gate 			FR_VERBOSE(("%d (%#x)\n", skip, fr->fr_flags));
18927c478bd9Sstevel@tonic-gate 			skip--;
18937c478bd9Sstevel@tonic-gate 			continue;
18947c478bd9Sstevel@tonic-gate 		}
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 		/*
18977c478bd9Sstevel@tonic-gate 		 * In all checks below, a null (zero) value in the
18987c478bd9Sstevel@tonic-gate 		 * filter struture is taken to mean a wildcard.
18997c478bd9Sstevel@tonic-gate 		 *
19007c478bd9Sstevel@tonic-gate 		 * check that we are working for the right interface
19017c478bd9Sstevel@tonic-gate 		 */
19027c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
19037c478bd9Sstevel@tonic-gate 		if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
19047c478bd9Sstevel@tonic-gate 			continue;
19057c478bd9Sstevel@tonic-gate #else
19067c478bd9Sstevel@tonic-gate 		if (opts & (OPT_VERBOSE|OPT_DEBUG))
19077c478bd9Sstevel@tonic-gate 			printf("\n");
19087c478bd9Sstevel@tonic-gate 		FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' :
1909ab25eeb5Syz 				  FR_ISPASS(pass) ? 'p' :
19107c478bd9Sstevel@tonic-gate 				  FR_ISACCOUNT(pass) ? 'A' :
19117c478bd9Sstevel@tonic-gate 				  FR_ISAUTH(pass) ? 'a' :
19127c478bd9Sstevel@tonic-gate 				  (pass & FR_NOMATCH) ? 'n' :'b'));
19137c478bd9Sstevel@tonic-gate 		if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
19147c478bd9Sstevel@tonic-gate 			continue;
19157c478bd9Sstevel@tonic-gate 		FR_VERBOSE((":i"));
19167c478bd9Sstevel@tonic-gate #endif
19177c478bd9Sstevel@tonic-gate 
19187c478bd9Sstevel@tonic-gate 		switch (fr->fr_type)
19197c478bd9Sstevel@tonic-gate 		{
19207c478bd9Sstevel@tonic-gate 		case FR_T_IPF :
19217c478bd9Sstevel@tonic-gate 		case FR_T_IPF|FR_T_BUILTIN :
19227c478bd9Sstevel@tonic-gate 			if (fr_ipfcheck(fin, fr, portcmp))
19237c478bd9Sstevel@tonic-gate 				continue;
19247c478bd9Sstevel@tonic-gate 			break;
1925ab25eeb5Syz #if defined(IPFILTER_BPF)
19267c478bd9Sstevel@tonic-gate 		case FR_T_BPFOPC :
19277c478bd9Sstevel@tonic-gate 		case FR_T_BPFOPC|FR_T_BUILTIN :
19287c478bd9Sstevel@tonic-gate 		    {
19297c478bd9Sstevel@tonic-gate 			u_char *mc;
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 			if (*fin->fin_mp == NULL)
19327c478bd9Sstevel@tonic-gate 				continue;
19337c478bd9Sstevel@tonic-gate 			if (fin->fin_v != fr->fr_v)
19347c478bd9Sstevel@tonic-gate 				continue;
19357c478bd9Sstevel@tonic-gate 			mc = (u_char *)fin->fin_m;
1936ab25eeb5Syz 			if (!bpf_filter(fr->fr_data, mc, fin->fin_plen, 0))
19377c478bd9Sstevel@tonic-gate 				continue;
19387c478bd9Sstevel@tonic-gate 			break;
19397c478bd9Sstevel@tonic-gate 		    }
19407c478bd9Sstevel@tonic-gate #endif
19417c478bd9Sstevel@tonic-gate 		case FR_T_CALLFUNC|FR_T_BUILTIN :
19427c478bd9Sstevel@tonic-gate 		    {
19437c478bd9Sstevel@tonic-gate 			frentry_t *f;
19447c478bd9Sstevel@tonic-gate 
19457c478bd9Sstevel@tonic-gate 			f = (*fr->fr_func)(fin, &pass);
19467c478bd9Sstevel@tonic-gate 			if (f != NULL)
19477c478bd9Sstevel@tonic-gate 				fr = f;
19487c478bd9Sstevel@tonic-gate 			else
19497c478bd9Sstevel@tonic-gate 				continue;
19507c478bd9Sstevel@tonic-gate 			break;
19517c478bd9Sstevel@tonic-gate 		    }
19527c478bd9Sstevel@tonic-gate 		default :
19537c478bd9Sstevel@tonic-gate 			break;
19547c478bd9Sstevel@tonic-gate 		}
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 		if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) {
19577c478bd9Sstevel@tonic-gate 			if (fin->fin_nattag == NULL)
19587c478bd9Sstevel@tonic-gate 				continue;
19597c478bd9Sstevel@tonic-gate 			if (fr_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0)
19607c478bd9Sstevel@tonic-gate 				continue;
19617c478bd9Sstevel@tonic-gate 		}
19627c478bd9Sstevel@tonic-gate 		FR_VERBOSE(("=%s.%d *", fr->fr_group, rulen));
19637c478bd9Sstevel@tonic-gate 
19647c478bd9Sstevel@tonic-gate 		passt = fr->fr_flags;
19657c478bd9Sstevel@tonic-gate 
1966ab25eeb5Syz 		/*
1967ab25eeb5Syz 		 * Allowing a rule with the "keep state" flag set to match
1968ab25eeb5Syz 		 * packets that have been tagged "out of window" by the TCP
1969ab25eeb5Syz 		 * state tracking is foolish as the attempt to add a new
1970ab25eeb5Syz 		 * state entry to the table will fail.
1971ab25eeb5Syz 		 */
1972ab25eeb5Syz 		if ((passt & FR_KEEPSTATE) && (fin->fin_flx & FI_OOW))
1973ab25eeb5Syz 			continue;
1974ab25eeb5Syz 
19757c478bd9Sstevel@tonic-gate 		/*
19767c478bd9Sstevel@tonic-gate 		 * If the rule is a "call now" rule, then call the function
19777c478bd9Sstevel@tonic-gate 		 * in the rule, if it exists and use the results from that.
19787c478bd9Sstevel@tonic-gate 		 * If the function pointer is bad, just make like we ignore
19797c478bd9Sstevel@tonic-gate 		 * it, except for increasing the hit counter.
19807c478bd9Sstevel@tonic-gate 		 */
1981*de22af4eSJohn Ojemann 		IPF_BUMP(fr->fr_hits);
1982*de22af4eSJohn Ojemann 		fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
19837c478bd9Sstevel@tonic-gate 		if ((passt & FR_CALLNOW) != 0) {
19847c478bd9Sstevel@tonic-gate 			if ((fr->fr_func != NULL) &&
19857c478bd9Sstevel@tonic-gate 			    (fr->fr_func != (ipfunc_t)-1)) {
19867c478bd9Sstevel@tonic-gate 				frentry_t *frs;
19877c478bd9Sstevel@tonic-gate 
19887c478bd9Sstevel@tonic-gate 				frs = fin->fin_fr;
19897c478bd9Sstevel@tonic-gate 				fin->fin_fr = fr;
19907c478bd9Sstevel@tonic-gate 				fr = (*fr->fr_func)(fin, &passt);
19917c478bd9Sstevel@tonic-gate 				if (fr == NULL) {
19927c478bd9Sstevel@tonic-gate 					fin->fin_fr = frs;
19937c478bd9Sstevel@tonic-gate 					continue;
19947c478bd9Sstevel@tonic-gate 				}
19957c478bd9Sstevel@tonic-gate 				passt = fr->fr_flags;
19967c478bd9Sstevel@tonic-gate 				fin->fin_fr = fr;
19977c478bd9Sstevel@tonic-gate 			}
19987c478bd9Sstevel@tonic-gate 		} else {
19997c478bd9Sstevel@tonic-gate 			fin->fin_fr = fr;
20007c478bd9Sstevel@tonic-gate 		}
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate #ifdef  IPFILTER_LOG
20037c478bd9Sstevel@tonic-gate 		/*
20047c478bd9Sstevel@tonic-gate 		 * Just log this packet...
20057c478bd9Sstevel@tonic-gate 		 */
20067c478bd9Sstevel@tonic-gate 		if ((passt & FR_LOGMASK) == FR_LOG) {
20077c478bd9Sstevel@tonic-gate 			if (ipflog(fin, passt) == -1) {
20087c478bd9Sstevel@tonic-gate 				if (passt & FR_LOGORBLOCK) {
20097c478bd9Sstevel@tonic-gate 					passt &= ~FR_CMDMASK;
20107c478bd9Sstevel@tonic-gate 					passt |= FR_BLOCK|FR_QUICK;
20117c478bd9Sstevel@tonic-gate 				}
2012cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[fin->fin_out].fr_skip);
20137c478bd9Sstevel@tonic-gate 			}
2014cbded9aeSdr 			IPF_BUMP(ifs->ifs_frstats[fin->fin_out].fr_pkl);
20157c478bd9Sstevel@tonic-gate 			logged = 1;
20167c478bd9Sstevel@tonic-gate 		}
20177c478bd9Sstevel@tonic-gate #endif /* IPFILTER_LOG */
2018ab25eeb5Syz 		passo = pass;
20197c478bd9Sstevel@tonic-gate 		if (FR_ISSKIP(passt))
20207c478bd9Sstevel@tonic-gate 			skip = fr->fr_arg;
20217c478bd9Sstevel@tonic-gate 		else if ((passt & FR_LOGMASK) != FR_LOG)
20227c478bd9Sstevel@tonic-gate 			pass = passt;
20237c478bd9Sstevel@tonic-gate 		if (passt & (FR_RETICMP|FR_FAKEICMP))
20247c478bd9Sstevel@tonic-gate 			fin->fin_icode = fr->fr_icode;
20257c478bd9Sstevel@tonic-gate 		FR_DEBUG(("pass %#x\n", pass));
20267c478bd9Sstevel@tonic-gate 		fin->fin_rule = rulen;
20277c478bd9Sstevel@tonic-gate 		(void) strncpy(fin->fin_group, fr->fr_group, FR_GROUPLEN);
20287c478bd9Sstevel@tonic-gate 		if (fr->fr_grp != NULL) {
20297c478bd9Sstevel@tonic-gate 			fin->fin_fr = *fr->fr_grp;
20307c478bd9Sstevel@tonic-gate 			pass = fr_scanlist(fin, pass);
20317c478bd9Sstevel@tonic-gate 			if (fin->fin_fr == NULL) {
20327c478bd9Sstevel@tonic-gate 				fin->fin_rule = rulen;
20337c478bd9Sstevel@tonic-gate 				(void) strncpy(fin->fin_group, fr->fr_group,
20347c478bd9Sstevel@tonic-gate 					       FR_GROUPLEN);
20357c478bd9Sstevel@tonic-gate 				fin->fin_fr = fr;
20367c478bd9Sstevel@tonic-gate 			}
20377c478bd9Sstevel@tonic-gate 			if (fin->fin_flx & FI_DONTCACHE)
20387c478bd9Sstevel@tonic-gate 				logged = 1;
20397c478bd9Sstevel@tonic-gate 		}
2040ab25eeb5Syz 
2041ab25eeb5Syz 		if (pass & FR_QUICK) {
2042ab25eeb5Syz 			/*
2043ab25eeb5Syz 			 * Finally, if we've asked to track state for this
2044ab25eeb5Syz 			 * packet, set it up.  Add state for "quick" rules
2045ab25eeb5Syz 			 * here so that if the action fails we can consider
2046ab25eeb5Syz 			 * the rule to "not match" and keep on processing
2047ab25eeb5Syz 			 * filter rules.
2048ab25eeb5Syz 			 */
2049ab25eeb5Syz 			if ((pass & FR_KEEPSTATE) &&
2050ab25eeb5Syz 			    !(fin->fin_flx & FI_STATE)) {
2051ab25eeb5Syz 				int out = fin->fin_out;
2052ab25eeb5Syz 
2053ab25eeb5Syz 				if (fr_addstate(fin, NULL, 0) != NULL) {
2054cbded9aeSdr 					IPF_BUMP(ifs->ifs_frstats[out].fr_ads);
2055ab25eeb5Syz 				} else {
2056cbded9aeSdr 					IPF_BUMP(ifs->ifs_frstats[out].fr_bads);
2057ab25eeb5Syz 					pass = passo;
2058ab25eeb5Syz 					continue;
2059ab25eeb5Syz 				}
2060ab25eeb5Syz 			}
20617c478bd9Sstevel@tonic-gate 			break;
2062ab25eeb5Syz 		}
20637c478bd9Sstevel@tonic-gate 	}
20647c478bd9Sstevel@tonic-gate 	if (logged)
20657c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_DONTCACHE;
20667c478bd9Sstevel@tonic-gate 	fin->fin_depth--;
20677c478bd9Sstevel@tonic-gate 	return pass;
20687c478bd9Sstevel@tonic-gate }
20697c478bd9Sstevel@tonic-gate 
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
20727c478bd9Sstevel@tonic-gate /* Function:    fr_acctpkt                                                  */
20737c478bd9Sstevel@tonic-gate /* Returns:     frentry_t* - always returns NULL                            */
20747c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
20757c478bd9Sstevel@tonic-gate /*              passp(IO) - pointer to current/new filter decision (unused) */
20767c478bd9Sstevel@tonic-gate /*                                                                          */
20777c478bd9Sstevel@tonic-gate /* Checks a packet against accounting rules, if there are any for the given */
20787c478bd9Sstevel@tonic-gate /* IP protocol version.                                                     */
20797c478bd9Sstevel@tonic-gate /*                                                                          */
20807c478bd9Sstevel@tonic-gate /* N.B.: this function returns NULL to match the prototype used by other    */
20817c478bd9Sstevel@tonic-gate /* functions called from the IPFilter "mainline" in fr_check().             */
20827c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
20837c478bd9Sstevel@tonic-gate frentry_t *fr_acctpkt(fin, passp)
20847c478bd9Sstevel@tonic-gate fr_info_t *fin;
20857c478bd9Sstevel@tonic-gate u_32_t *passp;
20867c478bd9Sstevel@tonic-gate {
20877c478bd9Sstevel@tonic-gate 	char group[FR_GROUPLEN];
20887c478bd9Sstevel@tonic-gate 	frentry_t *fr, *frsave;
20897c478bd9Sstevel@tonic-gate 	u_32_t pass, rulen;
2090f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	passp = passp;
20937c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
20947c478bd9Sstevel@tonic-gate 	if (fin->fin_v == 6)
2095f4b3ec61Sdh 		fr = ifs->ifs_ipacct6[fin->fin_out][ifs->ifs_fr_active];
20967c478bd9Sstevel@tonic-gate 	else
20977c478bd9Sstevel@tonic-gate #endif
2098f4b3ec61Sdh 		fr = ifs->ifs_ipacct[fin->fin_out][ifs->ifs_fr_active];
20997c478bd9Sstevel@tonic-gate 
21007c478bd9Sstevel@tonic-gate 	if (fr != NULL) {
21017c478bd9Sstevel@tonic-gate 		frsave = fin->fin_fr;
21027c478bd9Sstevel@tonic-gate 		bcopy(fin->fin_group, group, FR_GROUPLEN);
21037c478bd9Sstevel@tonic-gate 		rulen = fin->fin_rule;
21047c478bd9Sstevel@tonic-gate 		fin->fin_fr = fr;
21057c478bd9Sstevel@tonic-gate 		pass = fr_scanlist(fin, FR_NOMATCH);
21067c478bd9Sstevel@tonic-gate 		if (FR_ISACCOUNT(pass)) {
2107cbded9aeSdr 			IPF_BUMP(ifs->ifs_frstats[0].fr_acct);
21087c478bd9Sstevel@tonic-gate 		}
21097c478bd9Sstevel@tonic-gate 		fin->fin_fr = frsave;
21107c478bd9Sstevel@tonic-gate 		bcopy(group, fin->fin_group, FR_GROUPLEN);
21117c478bd9Sstevel@tonic-gate 		fin->fin_rule = rulen;
21127c478bd9Sstevel@tonic-gate 	}
21137c478bd9Sstevel@tonic-gate 	return NULL;
21147c478bd9Sstevel@tonic-gate }
21157c478bd9Sstevel@tonic-gate 
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
21187c478bd9Sstevel@tonic-gate /* Function:    fr_firewall                                                 */
21197c478bd9Sstevel@tonic-gate /* Returns:     frentry_t* - returns pointer to matched rule, if no matches */
21207c478bd9Sstevel@tonic-gate /*                           were found, returns NULL.                      */
21217c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
21227c478bd9Sstevel@tonic-gate /*              passp(IO) - pointer to current/new filter decision (unused) */
21237c478bd9Sstevel@tonic-gate /*                                                                          */
21247c478bd9Sstevel@tonic-gate /* Applies an appropriate set of firewall rules to the packet, to see if    */
21257c478bd9Sstevel@tonic-gate /* there are any matches.  The first check is to see if a match can be seen */
21267c478bd9Sstevel@tonic-gate /* in the cache.  If not, then search an appropriate list of rules.  Once a */
21277c478bd9Sstevel@tonic-gate /* matching rule is found, take any appropriate actions as defined by the   */
21287c478bd9Sstevel@tonic-gate /* rule - except logging.                                                   */
21297c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
21307c478bd9Sstevel@tonic-gate static frentry_t *fr_firewall(fin, passp)
21317c478bd9Sstevel@tonic-gate fr_info_t *fin;
21327c478bd9Sstevel@tonic-gate u_32_t *passp;
21337c478bd9Sstevel@tonic-gate {
21347c478bd9Sstevel@tonic-gate 	frentry_t *fr;
213514d3298eSAlexandr Nedvedicky 	fr_info_t *fc;
21367c478bd9Sstevel@tonic-gate 	u_32_t pass;
21377c478bd9Sstevel@tonic-gate 	int out;
2138f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	out = fin->fin_out;
21417c478bd9Sstevel@tonic-gate 	pass = *passp;
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
2144cbded9aeSdr 	if (fin->fin_v == 6)
2145cbded9aeSdr 		fin->fin_fr = ifs->ifs_ipfilter6[out][ifs->ifs_fr_active];
2146cbded9aeSdr 	else
21477c478bd9Sstevel@tonic-gate #endif
2148cbded9aeSdr 		fin->fin_fr = ifs->ifs_ipfilter[out][ifs->ifs_fr_active];
214914d3298eSAlexandr Nedvedicky 
215014d3298eSAlexandr Nedvedicky 	/*
215114d3298eSAlexandr Nedvedicky 	 * If there are no rules loaded skip all checks and return.
215214d3298eSAlexandr Nedvedicky 	 */
215314d3298eSAlexandr Nedvedicky 	if (fin->fin_fr == NULL) {
215414d3298eSAlexandr Nedvedicky 
215514d3298eSAlexandr Nedvedicky 		if ((pass & FR_NOMATCH)) {
215614d3298eSAlexandr Nedvedicky 			IPF_BUMP(ifs->ifs_frstats[out].fr_nom);
215714d3298eSAlexandr Nedvedicky 		}
215814d3298eSAlexandr Nedvedicky 
215914d3298eSAlexandr Nedvedicky 		return (NULL);
216014d3298eSAlexandr Nedvedicky 	}
216114d3298eSAlexandr Nedvedicky 
216214d3298eSAlexandr Nedvedicky 	fc = &ifs->ifs_frcache[out][CACHE_HASH(fin)];
216314d3298eSAlexandr Nedvedicky 	READ_ENTER(&ifs->ifs_ipf_frcache);
216414d3298eSAlexandr Nedvedicky 	if (!bcmp((char *)fin, (char *)fc, FI_CSIZE)) {
216514d3298eSAlexandr Nedvedicky 		/*
216614d3298eSAlexandr Nedvedicky 		 * copy cached data so we can unlock the mutexes earlier.
216714d3298eSAlexandr Nedvedicky 		 */
216814d3298eSAlexandr Nedvedicky 		bcopy((char *)fc, (char *)fin, FI_COPYSIZE);
216914d3298eSAlexandr Nedvedicky 		RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
217014d3298eSAlexandr Nedvedicky 		IPF_BUMP(ifs->ifs_frstats[out].fr_chit);
217114d3298eSAlexandr Nedvedicky 
217214d3298eSAlexandr Nedvedicky 		if ((fr = fin->fin_fr) != NULL) {
217314d3298eSAlexandr Nedvedicky 			IPF_BUMP(fr->fr_hits);
2174*de22af4eSJohn Ojemann 			fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
217514d3298eSAlexandr Nedvedicky 			pass = fr->fr_flags;
217614d3298eSAlexandr Nedvedicky 		}
217714d3298eSAlexandr Nedvedicky 	} else {
217814d3298eSAlexandr Nedvedicky 		RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
217914d3298eSAlexandr Nedvedicky 
2180cbded9aeSdr 		pass = fr_scanlist(fin, ifs->ifs_fr_pass);
2181ab25eeb5Syz 
218214d3298eSAlexandr Nedvedicky 		if (((pass & FR_KEEPSTATE) == 0) &&
218314d3298eSAlexandr Nedvedicky 		    ((fin->fin_flx & FI_DONTCACHE) == 0)) {
218414d3298eSAlexandr Nedvedicky 			WRITE_ENTER(&ifs->ifs_ipf_frcache);
218514d3298eSAlexandr Nedvedicky 			bcopy((char *)fin, (char *)fc, FI_COPYSIZE);
218614d3298eSAlexandr Nedvedicky 			RWLOCK_EXIT(&ifs->ifs_ipf_frcache);
218714d3298eSAlexandr Nedvedicky 		}
218814d3298eSAlexandr Nedvedicky 
218914d3298eSAlexandr Nedvedicky 		fr = fin->fin_fr;
219014d3298eSAlexandr Nedvedicky 	}
219114d3298eSAlexandr Nedvedicky 
2192cbded9aeSdr 	if ((pass & FR_NOMATCH)) {
2193cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_nom);
21947c478bd9Sstevel@tonic-gate 	}
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	/*
21977c478bd9Sstevel@tonic-gate 	 * Apply packets per second rate-limiting to a rule as required.
21987c478bd9Sstevel@tonic-gate 	 */
21997c478bd9Sstevel@tonic-gate 	if ((fr != NULL) && (fr->fr_pps != 0) &&
22007c478bd9Sstevel@tonic-gate 	    !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) {
22017c478bd9Sstevel@tonic-gate 		pass &= ~(FR_CMDMASK|FR_DUP|FR_RETICMP|FR_RETRST);
22027c478bd9Sstevel@tonic-gate 		pass |= FR_BLOCK;
2203cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_ppshit);
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	/*
22077c478bd9Sstevel@tonic-gate 	 * If we fail to add a packet to the authorization queue, then we
22087c478bd9Sstevel@tonic-gate 	 * drop the packet later.  However, if it was added then pretend
22097c478bd9Sstevel@tonic-gate 	 * we've dropped it already.
22107c478bd9Sstevel@tonic-gate 	 */
22117c478bd9Sstevel@tonic-gate 	if (FR_ISAUTH(pass)) {
22127c478bd9Sstevel@tonic-gate 		if (fr_newauth(fin->fin_m, fin) != 0) {
22137c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
22147c478bd9Sstevel@tonic-gate 			fin->fin_m = *fin->fin_mp = NULL;
22157c478bd9Sstevel@tonic-gate #else
22167c478bd9Sstevel@tonic-gate 			;
22177c478bd9Sstevel@tonic-gate #endif
22187c478bd9Sstevel@tonic-gate 			fin->fin_error = 0;
22197c478bd9Sstevel@tonic-gate 		} else
22207c478bd9Sstevel@tonic-gate 			fin->fin_error = ENOSPC;
22217c478bd9Sstevel@tonic-gate 	}
22227c478bd9Sstevel@tonic-gate 
22237c478bd9Sstevel@tonic-gate 	if ((fr != NULL) && (fr->fr_func != NULL) &&
22247c478bd9Sstevel@tonic-gate 	    (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW))
22257c478bd9Sstevel@tonic-gate 		(void) (*fr->fr_func)(fin, &pass);
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	/*
22287c478bd9Sstevel@tonic-gate 	 * If a rule is a pre-auth rule, check again in the list of rules
22297c478bd9Sstevel@tonic-gate 	 * loaded for authenticated use.  It does not particulary matter
22307c478bd9Sstevel@tonic-gate 	 * if this search fails because a "preauth" result, from a rule,
22317c478bd9Sstevel@tonic-gate 	 * is treated as "not a pass", hence the packet is blocked.
22327c478bd9Sstevel@tonic-gate 	 */
22337c478bd9Sstevel@tonic-gate 	if (FR_ISPREAUTH(pass)) {
2234f4b3ec61Sdh 		if ((fin->fin_fr = ifs->ifs_ipauth) != NULL)
2235f4b3ec61Sdh 			pass = fr_scanlist(fin, ifs->ifs_fr_pass);
22367c478bd9Sstevel@tonic-gate 	}
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate 	/*
22397c478bd9Sstevel@tonic-gate 	 * If the rule has "keep frag" and the packet is actually a fragment,
22407c478bd9Sstevel@tonic-gate 	 * then create a fragment state entry.
22417c478bd9Sstevel@tonic-gate 	 */
22427c478bd9Sstevel@tonic-gate 	if ((pass & (FR_KEEPFRAG|FR_KEEPSTATE)) == FR_KEEPFRAG) {
22437c478bd9Sstevel@tonic-gate 		if (fin->fin_flx & FI_FRAG) {
22447c478bd9Sstevel@tonic-gate 			if (fr_newfrag(fin, pass) == -1) {
2245cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[out].fr_bnfr);
22467c478bd9Sstevel@tonic-gate 			} else {
2247cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[out].fr_nfr);
22487c478bd9Sstevel@tonic-gate 			}
22497c478bd9Sstevel@tonic-gate 		} else {
2250cbded9aeSdr 			IPF_BUMP(ifs->ifs_frstats[out].fr_cfr);
22517c478bd9Sstevel@tonic-gate 		}
22527c478bd9Sstevel@tonic-gate 	}
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate 	/*
22557c478bd9Sstevel@tonic-gate 	 * Finally, if we've asked to track state for this packet, set it up.
22567c478bd9Sstevel@tonic-gate 	 */
2257ab25eeb5Syz 	if ((pass & FR_KEEPSTATE) && !(fin->fin_flx & FI_STATE)) {
22587c478bd9Sstevel@tonic-gate 		if (fr_addstate(fin, NULL, 0) != NULL) {
2259cbded9aeSdr 			IPF_BUMP(ifs->ifs_frstats[out].fr_ads);
22607c478bd9Sstevel@tonic-gate 		} else {
2261cbded9aeSdr 			IPF_BUMP(ifs->ifs_frstats[out].fr_bads);
2262ab25eeb5Syz 			if (FR_ISPASS(pass)) {
2263ab25eeb5Syz 				pass &= ~FR_CMDMASK;
2264ab25eeb5Syz 				pass |= FR_BLOCK;
2265ab25eeb5Syz 			}
22667c478bd9Sstevel@tonic-gate 		}
22677c478bd9Sstevel@tonic-gate 	}
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 	fr = fin->fin_fr;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 	if (passp != NULL)
22727c478bd9Sstevel@tonic-gate 		*passp = pass;
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 	return fr;
22757c478bd9Sstevel@tonic-gate }
22767c478bd9Sstevel@tonic-gate 
22777c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
22787c478bd9Sstevel@tonic-gate /* Function:    fr_check                                                    */
22797c478bd9Sstevel@tonic-gate /* Returns:     int -  0 == packet allowed through,                         */
22807c478bd9Sstevel@tonic-gate /*              User space:                                                 */
22817c478bd9Sstevel@tonic-gate /*                    -1 == packet blocked                                  */
22827c478bd9Sstevel@tonic-gate /*                     1 == packet not matched                              */
2283ab25eeb5Syz /*                    -2 == requires authentication                         */
22847c478bd9Sstevel@tonic-gate /*              Kernel:                                                     */
22857c478bd9Sstevel@tonic-gate /*                   > 0 == filter error # for packet                       */
22867c478bd9Sstevel@tonic-gate /* Parameters: ip(I)   - pointer to start of IPv4/6 packet                  */
22877c478bd9Sstevel@tonic-gate /*             hlen(I) - length of header                                   */
22887c478bd9Sstevel@tonic-gate /*             ifp(I)  - pointer to interface this packet is on             */
22897c478bd9Sstevel@tonic-gate /*             out(I)  - 0 == packet going in, 1 == packet going out        */
22907c478bd9Sstevel@tonic-gate /*             mp(IO)  - pointer to caller's buffer pointer that holds this */
22917c478bd9Sstevel@tonic-gate /*                       IP packet.                                         */
22927c478bd9Sstevel@tonic-gate /* Solaris & HP-UX ONLY :                                                   */
2293ab25eeb5Syz /*             qpi(I)  - pointer to STREAMS queue information for this      */
22947c478bd9Sstevel@tonic-gate /*                       interface & direction.                             */
22957c478bd9Sstevel@tonic-gate /*                                                                          */
22967c478bd9Sstevel@tonic-gate /* fr_check() is the master function for all IPFilter packet processing.    */
22977c478bd9Sstevel@tonic-gate /* It orchestrates: Network Address Translation (NAT), checking for packet  */
22987c478bd9Sstevel@tonic-gate /* authorisation (or pre-authorisation), presence of related state info.,   */
22997c478bd9Sstevel@tonic-gate /* generating log entries, IP packet accounting, routing of packets as      */
23007c478bd9Sstevel@tonic-gate /* directed by firewall rules and of course whether or not to allow the     */
23017c478bd9Sstevel@tonic-gate /* packet to be further processed by the kernel.                            */
23027c478bd9Sstevel@tonic-gate /*                                                                          */
23037c478bd9Sstevel@tonic-gate /* For packets blocked, the contents of "mp" will be NULL'd and the buffer  */
23047c478bd9Sstevel@tonic-gate /* freed.  Packets passed may be returned with the pointer pointed to by    */
23057c478bd9Sstevel@tonic-gate /* by "mp" changed to a new buffer.                                         */
23067c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
23077c478bd9Sstevel@tonic-gate int fr_check(ip, hlen, ifp, out
23087c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && defined(MENTAT)
2309f4b3ec61Sdh , qif, mp, ifs)
2310ab25eeb5Syz void *qif;
23117c478bd9Sstevel@tonic-gate #else
2312f4b3ec61Sdh , mp, ifs)
23137c478bd9Sstevel@tonic-gate #endif
23147c478bd9Sstevel@tonic-gate mb_t **mp;
23157c478bd9Sstevel@tonic-gate ip_t *ip;
23167c478bd9Sstevel@tonic-gate int hlen;
23177c478bd9Sstevel@tonic-gate void *ifp;
23187c478bd9Sstevel@tonic-gate int out;
2319f4b3ec61Sdh ipf_stack_t *ifs;
23207c478bd9Sstevel@tonic-gate {
23217c478bd9Sstevel@tonic-gate 	/*
23227c478bd9Sstevel@tonic-gate 	 * The above really sucks, but short of writing a diff
23237c478bd9Sstevel@tonic-gate 	 */
23247c478bd9Sstevel@tonic-gate 	fr_info_t frinfo;
23257c478bd9Sstevel@tonic-gate 	fr_info_t *fin = &frinfo;
2326f4b3ec61Sdh 	u_32_t pass;
23277c478bd9Sstevel@tonic-gate 	frentry_t *fr = NULL;
2328ab25eeb5Syz 	int v = IP_V(ip);
23297c478bd9Sstevel@tonic-gate 	mb_t *mc = NULL;
23307c478bd9Sstevel@tonic-gate 	mb_t *m;
23317663b816Sml #ifdef USE_INET6
23327663b816Sml 	ip6_t *ip6;
23337663b816Sml #endif
2334ab25eeb5Syz #ifdef	_KERNEL
2335ab25eeb5Syz # ifdef MENTAT
2336ab25eeb5Syz 	qpktinfo_t *qpi = qif;
2337ab25eeb5Syz #endif
2338ab25eeb5Syz #endif
2339f4b3ec61Sdh 
2340ab25eeb5Syz 	SPL_INT(s);
2341f4b3ec61Sdh 	pass = ifs->ifs_fr_pass;
23427c478bd9Sstevel@tonic-gate 
23437c478bd9Sstevel@tonic-gate 	/*
23447c478bd9Sstevel@tonic-gate 	 * The first part of fr_check() deals with making sure that what goes
23457c478bd9Sstevel@tonic-gate 	 * into the filtering engine makes some sense.  Information about the
23467c478bd9Sstevel@tonic-gate 	 * the packet is distilled, collected into a fr_info_t structure and
23477c478bd9Sstevel@tonic-gate 	 * the an attempt to ensure the buffer the packet is in is big enough
23487c478bd9Sstevel@tonic-gate 	 * to hold all the required packet headers.
23497c478bd9Sstevel@tonic-gate 	 */
23507c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
23517c478bd9Sstevel@tonic-gate # ifdef MENTAT
2352ab25eeb5Syz 	if (!OK_32PTR(ip))
23537c478bd9Sstevel@tonic-gate 		return 2;
23547c478bd9Sstevel@tonic-gate # endif
2355ab25eeb5Syz 
23567c478bd9Sstevel@tonic-gate 
2357f4b3ec61Sdh 	if (ifs->ifs_fr_running <= 0) {
23587c478bd9Sstevel@tonic-gate 		return 0;
23597c478bd9Sstevel@tonic-gate 	}
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate 	bzero((char *)fin, sizeof(*fin));
23627c478bd9Sstevel@tonic-gate 
23637c478bd9Sstevel@tonic-gate # ifdef MENTAT
23641b47e080Sdr 	fin->fin_flx = qpi->qpi_flags & (FI_NOCKSUM|FI_MBCAST|FI_MULTICAST|
23651b47e080Sdr 					 FI_BROADCAST);
2366ab25eeb5Syz 	m = qpi->qpi_m;
23677c478bd9Sstevel@tonic-gate 	fin->fin_qfm = m;
2368ab25eeb5Syz 	fin->fin_qpi = qpi;
23697c478bd9Sstevel@tonic-gate # else /* MENTAT */
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 	m = *mp;
2372ab25eeb5Syz 
23737c478bd9Sstevel@tonic-gate #  if defined(M_MCAST)
23747c478bd9Sstevel@tonic-gate 	if ((m->m_flags & M_MCAST) != 0)
23757c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
23767c478bd9Sstevel@tonic-gate #  endif
2377ab25eeb5Syz #  if defined(M_MLOOP)
2378ab25eeb5Syz 	if ((m->m_flags & M_MLOOP) != 0)
2379ab25eeb5Syz 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
2380ab25eeb5Syz #  endif
23817c478bd9Sstevel@tonic-gate #  if defined(M_BCAST)
23827c478bd9Sstevel@tonic-gate 	if ((m->m_flags & M_BCAST) != 0)
23837c478bd9Sstevel@tonic-gate 		fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
23847c478bd9Sstevel@tonic-gate #  endif
23857c478bd9Sstevel@tonic-gate #  ifdef M_CANFASTFWD
23867c478bd9Sstevel@tonic-gate 	/*
23877c478bd9Sstevel@tonic-gate 	 * XXX For now, IP Filter and fast-forwarding of cached flows
23887c478bd9Sstevel@tonic-gate 	 * XXX are mutually exclusive.  Eventually, IP Filter should
23897c478bd9Sstevel@tonic-gate 	 * XXX get a "can-fast-forward" filter rule.
23907c478bd9Sstevel@tonic-gate 	 */
23917c478bd9Sstevel@tonic-gate 	m->m_flags &= ~M_CANFASTFWD;
23927c478bd9Sstevel@tonic-gate #  endif /* M_CANFASTFWD */
23937c478bd9Sstevel@tonic-gate #  ifdef CSUM_DELAY_DATA
23947c478bd9Sstevel@tonic-gate 	/*
23957c478bd9Sstevel@tonic-gate 	 * disable delayed checksums.
23967c478bd9Sstevel@tonic-gate 	 */
23977c478bd9Sstevel@tonic-gate 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
23987c478bd9Sstevel@tonic-gate 		in_delayed_cksum(m);
23997c478bd9Sstevel@tonic-gate 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
24007c478bd9Sstevel@tonic-gate 	}
24017c478bd9Sstevel@tonic-gate #  endif /* CSUM_DELAY_DATA */
24027c478bd9Sstevel@tonic-gate # endif /* MENTAT */
2403ab25eeb5Syz #else
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate 	bzero((char *)fin, sizeof(*fin));
24067c478bd9Sstevel@tonic-gate 	m = *mp;
24077c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
24087c478bd9Sstevel@tonic-gate 
2409ab25eeb5Syz 	fin->fin_v = v;
2410ab25eeb5Syz 	fin->fin_m = m;
2411ab25eeb5Syz 	fin->fin_ip = ip;
2412ab25eeb5Syz 	fin->fin_mp = mp;
2413ab25eeb5Syz 	fin->fin_out = out;
2414ab25eeb5Syz 	fin->fin_ifp = ifp;
2415ab25eeb5Syz 	fin->fin_error = ENETUNREACH;
2416ab25eeb5Syz 	fin->fin_hlen = (u_short)hlen;
2417ab25eeb5Syz 	fin->fin_dp = (char *)ip + hlen;
2418ab25eeb5Syz 	fin->fin_ipoff = (char *)ip - MTOD(m, char *);
2419f4b3ec61Sdh 	fin->fin_ifs = ifs;
2420ab25eeb5Syz 
2421ab25eeb5Syz 	SPL_NET(s);
2422ab25eeb5Syz 
24237c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
24247c478bd9Sstevel@tonic-gate 	if (v == 6) {
2425cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_ipv6);
24267c478bd9Sstevel@tonic-gate 		/*
24277c478bd9Sstevel@tonic-gate 		 * Jumbo grams are quite likely too big for internal buffer
24287c478bd9Sstevel@tonic-gate 		 * structures to handle comfortably, for now, so just drop
2429ab25eeb5Syz 		 * them.
24307c478bd9Sstevel@tonic-gate 		 */
2431ab25eeb5Syz 		ip6 = (ip6_t *)ip;
2432ab25eeb5Syz 		fin->fin_plen = ntohs(ip6->ip6_plen);
2433ab25eeb5Syz 		if (fin->fin_plen == 0) {
2434f4b3ec61Sdh 			READ_ENTER(&ifs->ifs_ipf_mutex);
24357c478bd9Sstevel@tonic-gate 			pass = FR_BLOCK|FR_NOMATCH;
24367c478bd9Sstevel@tonic-gate 			goto filtered;
24377c478bd9Sstevel@tonic-gate 		}
2438ab25eeb5Syz 		fin->fin_plen += sizeof(ip6_t);
24397c478bd9Sstevel@tonic-gate 	} else
24407c478bd9Sstevel@tonic-gate #endif
24417c478bd9Sstevel@tonic-gate 	{
2442ab25eeb5Syz #if (OpenBSD >= 200311) && defined(_KERNEL)
2443ab25eeb5Syz 		ip->ip_len = ntohs(ip->ip_len);
2444ab25eeb5Syz 		ip->ip_off = ntohs(ip->ip_off);
24457c478bd9Sstevel@tonic-gate #endif
2446ab25eeb5Syz 		fin->fin_plen = ip->ip_len;
24477c478bd9Sstevel@tonic-gate 	}
24487c478bd9Sstevel@tonic-gate 
24497663b816Sml 	if (fr_makefrip(hlen, ip, fin) == -1) {
2450f4b3ec61Sdh 		READ_ENTER(&ifs->ifs_ipf_mutex);
24517663b816Sml 		pass = FR_BLOCK;
24527663b816Sml 		goto filtered;
24537663b816Sml 	}
24547c478bd9Sstevel@tonic-gate 
24557c478bd9Sstevel@tonic-gate 	/*
24567c478bd9Sstevel@tonic-gate 	 * For at least IPv6 packets, if a m_pullup() fails then this pointer
24577c478bd9Sstevel@tonic-gate 	 * becomes NULL and so we have no packet to free.
24587c478bd9Sstevel@tonic-gate 	 */
24597c478bd9Sstevel@tonic-gate 	if (*fin->fin_mp == NULL)
24607c478bd9Sstevel@tonic-gate 		goto finished;
24617c478bd9Sstevel@tonic-gate 
24627c478bd9Sstevel@tonic-gate 	if (!out) {
24637c478bd9Sstevel@tonic-gate 		if (v == 4) {
24647c478bd9Sstevel@tonic-gate #ifdef _KERNEL
2465f4b3ec61Sdh 			if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) {
2466cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[0].fr_badsrc);
24677c478bd9Sstevel@tonic-gate 				fin->fin_flx |= FI_BADSRC;
24687c478bd9Sstevel@tonic-gate 			}
24697c478bd9Sstevel@tonic-gate #endif
2470f4b3ec61Sdh 			if (fin->fin_ip->ip_ttl < ifs->ifs_fr_minttl) {
2471cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[0].fr_badttl);
24727c478bd9Sstevel@tonic-gate 				fin->fin_flx |= FI_LOWTTL;
24737c478bd9Sstevel@tonic-gate 			}
24747c478bd9Sstevel@tonic-gate 		}
24757c478bd9Sstevel@tonic-gate #ifdef USE_INET6
24767c478bd9Sstevel@tonic-gate 		else  if (v == 6) {
24777663b816Sml 			ip6 = (ip6_t *)ip;
24787663b816Sml #ifdef _KERNEL
2479f4b3ec61Sdh 			if (ifs->ifs_fr_chksrc && !fr_verifysrc(fin)) {
2480cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[0].fr_badsrc);
24817663b816Sml 				fin->fin_flx |= FI_BADSRC;
24827663b816Sml 			}
24837663b816Sml #endif
2484f4b3ec61Sdh 			if (ip6->ip6_hlim < ifs->ifs_fr_minttl) {
2485cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[0].fr_badttl);
24867c478bd9Sstevel@tonic-gate 				fin->fin_flx |= FI_LOWTTL;
24877c478bd9Sstevel@tonic-gate 			}
24887c478bd9Sstevel@tonic-gate 		}
24897c478bd9Sstevel@tonic-gate #endif
24907c478bd9Sstevel@tonic-gate 	}
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	if (fin->fin_flx & FI_SHORT) {
2493cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_short);
24947c478bd9Sstevel@tonic-gate 	}
24957c478bd9Sstevel@tonic-gate 
2496f4b3ec61Sdh 	READ_ENTER(&ifs->ifs_ipf_mutex);
24977c478bd9Sstevel@tonic-gate 
24987c478bd9Sstevel@tonic-gate 	/*
24997c478bd9Sstevel@tonic-gate 	 * Check auth now.  This, combined with the check below to see if apass
25007c478bd9Sstevel@tonic-gate 	 * is 0 is to ensure that we don't count the packet twice, which can
25017c478bd9Sstevel@tonic-gate 	 * otherwise occur when we reprocess it.  As it is, we only count it
25027c478bd9Sstevel@tonic-gate 	 * after it has no auth. table matchup.  This also stops NAT from
25037c478bd9Sstevel@tonic-gate 	 * occuring until after the packet has been auth'd.
25047c478bd9Sstevel@tonic-gate 	 */
25057c478bd9Sstevel@tonic-gate 	fr = fr_checkauth(fin, &pass);
2506ab25eeb5Syz 	if (!out) {
2507d6c23f6fSyx 		switch (fin->fin_v)
2508d6c23f6fSyx 		{
2509d6c23f6fSyx 		case 4 :
2510d6c23f6fSyx 			if (fr_checknatin(fin, &pass) == -1) {
2511d6c23f6fSyx 				RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2512d6c23f6fSyx 				goto finished;
2513d6c23f6fSyx 			}
2514d6c23f6fSyx 			break;
2515d6c23f6fSyx #ifdef	USE_INET6
2516d6c23f6fSyx 		case 6 :
2517d6c23f6fSyx 			if (fr_checknat6in(fin, &pass) == -1) {
2518d6c23f6fSyx 				RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2519d6c23f6fSyx 				goto finished;
2520d6c23f6fSyx 			}
2521d6c23f6fSyx 			break;
2522d6c23f6fSyx #endif
2523d6c23f6fSyx 		default :
2524d6c23f6fSyx 			break;
2525ab25eeb5Syz 		}
2526ab25eeb5Syz 	}
25277c478bd9Sstevel@tonic-gate 	if (!out)
25287c478bd9Sstevel@tonic-gate 		(void) fr_acctpkt(fin, NULL);
25297c478bd9Sstevel@tonic-gate 
25307c478bd9Sstevel@tonic-gate 	if (fr == NULL)
25317c478bd9Sstevel@tonic-gate 		if ((fin->fin_flx & (FI_FRAG|FI_BAD)) == FI_FRAG)
25327c478bd9Sstevel@tonic-gate 			fr = fr_knownfrag(fin, &pass);
25337c478bd9Sstevel@tonic-gate 	if (fr == NULL)
25347c478bd9Sstevel@tonic-gate 		fr = fr_checkstate(fin, &pass);
25357c478bd9Sstevel@tonic-gate 
25367c478bd9Sstevel@tonic-gate 	if ((pass & FR_NOMATCH) || (fr == NULL))
25377c478bd9Sstevel@tonic-gate 		fr = fr_firewall(fin, &pass);
25387c478bd9Sstevel@tonic-gate 
25397c478bd9Sstevel@tonic-gate 	fin->fin_fr = fr;
25407c478bd9Sstevel@tonic-gate 
25417c478bd9Sstevel@tonic-gate 	/*
25427c478bd9Sstevel@tonic-gate 	 * Only count/translate packets which will be passed on, out the
25437c478bd9Sstevel@tonic-gate 	 * interface.
25447c478bd9Sstevel@tonic-gate 	 */
25457c478bd9Sstevel@tonic-gate 	if (out && FR_ISPASS(pass)) {
25467c478bd9Sstevel@tonic-gate 		(void) fr_acctpkt(fin, NULL);
25477c478bd9Sstevel@tonic-gate 
2548d6c23f6fSyx 		switch (fin->fin_v)
2549d6c23f6fSyx 		{
2550d6c23f6fSyx 		case 4 :
2551d6c23f6fSyx 			if (fr_checknatout(fin, &pass) == -1) {
2552d6c23f6fSyx 				RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2553d6c23f6fSyx 				goto finished;
2554d6c23f6fSyx 			}
2555d6c23f6fSyx 			break;
2556d6c23f6fSyx #ifdef	USE_INET6
2557d6c23f6fSyx 		case 6 :
2558d6c23f6fSyx 			if (fr_checknat6out(fin, &pass) == -1) {
2559d6c23f6fSyx 				RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2560d6c23f6fSyx 				goto finished;
2561d6c23f6fSyx 			}
2562d6c23f6fSyx 			break;
2563d6c23f6fSyx #endif
2564d6c23f6fSyx 		default :
2565d6c23f6fSyx 			break;
2566d6c23f6fSyx 		}
2567d6c23f6fSyx 
2568d6c23f6fSyx 		if ((ifs->ifs_fr_update_ipid != 0) && (v == 4)) {
25697c478bd9Sstevel@tonic-gate 			if (fr_updateipid(fin) == -1) {
2570cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[1].fr_ipud);
25717c478bd9Sstevel@tonic-gate 				pass &= ~FR_CMDMASK;
25727c478bd9Sstevel@tonic-gate 				pass |= FR_BLOCK;
25737c478bd9Sstevel@tonic-gate 			} else {
2574cbded9aeSdr 				IPF_BUMP(ifs->ifs_frstats[0].fr_ipud);
25757c478bd9Sstevel@tonic-gate 			}
25767c478bd9Sstevel@tonic-gate 		}
25777c478bd9Sstevel@tonic-gate 	}
25787c478bd9Sstevel@tonic-gate 
25797c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOG
2580f4b3ec61Sdh 	if ((ifs->ifs_fr_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
25817c478bd9Sstevel@tonic-gate 		(void) fr_dolog(fin, &pass);
2582ab25eeb5Syz 	}
25837c478bd9Sstevel@tonic-gate #endif
25847c478bd9Sstevel@tonic-gate 
258533f2fefdSDarren Reed 	/*
258633f2fefdSDarren Reed 	 * The FI_STATE flag is cleared here so that calling fr_checkstate
258733f2fefdSDarren Reed 	 * will work when called from inside of fr_fastroute.  Although
258833f2fefdSDarren Reed 	 * there is a similar flag, FI_NATED, for NAT, it does have the same
258933f2fefdSDarren Reed 	 * impact on code execution.
259033f2fefdSDarren Reed 	 */
259133f2fefdSDarren Reed 	fin->fin_flx &= ~FI_STATE;
25927c478bd9Sstevel@tonic-gate 
25937c478bd9Sstevel@tonic-gate 	/*
25947c478bd9Sstevel@tonic-gate 	 * Only allow FR_DUP to work if a rule matched - it makes no sense to
25957c478bd9Sstevel@tonic-gate 	 * set FR_DUP as a "default" as there are no instructions about where
2596ab25eeb5Syz 	 * to send the packet.  Use fin_m here because it may have changed
2597ab25eeb5Syz 	 * (without an update of 'm') in prior processing.
25987c478bd9Sstevel@tonic-gate 	 */
25997c478bd9Sstevel@tonic-gate 	if ((fr != NULL) && (pass & FR_DUP)) {
2600ab25eeb5Syz 		mc = M_DUPLICATE(fin->fin_m);
2601201a9dc8SAlexandr Nedvedicky #ifdef _KERNEL
2602201a9dc8SAlexandr Nedvedicky 		mc->b_rptr += fin->fin_ipoff;
2603201a9dc8SAlexandr Nedvedicky #endif
26047c478bd9Sstevel@tonic-gate 	}
26057c478bd9Sstevel@tonic-gate 
26067c478bd9Sstevel@tonic-gate 	if (pass & (FR_RETRST|FR_RETICMP)) {
26077c478bd9Sstevel@tonic-gate 		/*
26087c478bd9Sstevel@tonic-gate 		 * Should we return an ICMP packet to indicate error
26097c478bd9Sstevel@tonic-gate 		 * status passing through the packet filter ?
26107c478bd9Sstevel@tonic-gate 		 * WARNING: ICMP error packets AND TCP RST packets should
26117c478bd9Sstevel@tonic-gate 		 * ONLY be sent in repsonse to incoming packets.  Sending them
26127c478bd9Sstevel@tonic-gate 		 * in response to outbound packets can result in a panic on
26137c478bd9Sstevel@tonic-gate 		 * some operating systems.
26147c478bd9Sstevel@tonic-gate 		 */
26157c478bd9Sstevel@tonic-gate 		if (!out) {
26167c478bd9Sstevel@tonic-gate 			if (pass & FR_RETICMP) {
26177c478bd9Sstevel@tonic-gate 				int dst;
26187c478bd9Sstevel@tonic-gate 
26197c478bd9Sstevel@tonic-gate 				if ((pass & FR_RETMASK) == FR_FAKEICMP)
26207c478bd9Sstevel@tonic-gate 					dst = 1;
26217c478bd9Sstevel@tonic-gate 				else
26227c478bd9Sstevel@tonic-gate 					dst = 0;
2623a1173273SAlexandr Nedvedicky #if defined(_KERNEL) && (SOLARIS2 >= 10)
2624a1173273SAlexandr Nedvedicky 				/*
2625a1173273SAlexandr Nedvedicky 				 * Assume it's possible to enter insane rule:
2626a1173273SAlexandr Nedvedicky 				 * 	pass return-icmp in proto udp ...
2627a1173273SAlexandr Nedvedicky 				 * then we have no other option than to forward
2628a1173273SAlexandr Nedvedicky 				 * packet on loopback and give up any attempt
2629a1173273SAlexandr Nedvedicky 				 * to create a fake response.
2630a1173273SAlexandr Nedvedicky 				 */
2631a1173273SAlexandr Nedvedicky 				if (IPF_IS_LOOPBACK(qpi->qpi_flags) &&
2632a1173273SAlexandr Nedvedicky 				    FR_ISBLOCK(pass)) {
2633a1173273SAlexandr Nedvedicky 
2634a1173273SAlexandr Nedvedicky 					if (fr_make_icmp(fin) == 0) {
2635a1173273SAlexandr Nedvedicky 						IPF_BUMP(
2636a1173273SAlexandr Nedvedicky 						ifs->ifs_frstats[out].fr_ret);
2637a1173273SAlexandr Nedvedicky 					}
2638a1173273SAlexandr Nedvedicky 					/*
2639a1173273SAlexandr Nedvedicky 					 * we drop packet silently in case we
2640a1173273SAlexandr Nedvedicky 					 * failed assemble fake response for it
2641a1173273SAlexandr Nedvedicky 					 */
2642a1173273SAlexandr Nedvedicky 					else if (*mp != NULL) {
2643a1173273SAlexandr Nedvedicky 						FREE_MB_T(*mp);
2644a1173273SAlexandr Nedvedicky 						m = *mp = NULL;
2645a1173273SAlexandr Nedvedicky 					}
2646a1173273SAlexandr Nedvedicky 
2647a1173273SAlexandr Nedvedicky 					IPF_BUMP(
2648a1173273SAlexandr Nedvedicky 					    ifs->ifs_frstats[out].fr_block);
2649a1173273SAlexandr Nedvedicky 					RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2650a1173273SAlexandr Nedvedicky 
2651a1173273SAlexandr Nedvedicky 					return (0);
2652a1173273SAlexandr Nedvedicky 				}
2653a1173273SAlexandr Nedvedicky #endif	/* _KERNEL && SOLARIS2 >= 10 */
2654a1173273SAlexandr Nedvedicky 
26557c478bd9Sstevel@tonic-gate 				(void) fr_send_icmp_err(ICMP_UNREACH, fin, dst);
2656a1173273SAlexandr Nedvedicky 				IPF_BUMP(ifs->ifs_frstats[out].fr_ret);
2657a1173273SAlexandr Nedvedicky 
26587c478bd9Sstevel@tonic-gate 			} else if (((pass & FR_RETMASK) == FR_RETRST) &&
26597c478bd9Sstevel@tonic-gate 				   !(fin->fin_flx & FI_SHORT)) {
2660a1173273SAlexandr Nedvedicky 
2661a1173273SAlexandr Nedvedicky #if defined(_KERNEL) && (SOLARIS2 >= 10)
2662a1173273SAlexandr Nedvedicky 				/*
2663a1173273SAlexandr Nedvedicky 				 * Assume it's possible to enter insane rule:
2664a1173273SAlexandr Nedvedicky 				 * 	pass return-rst in proto tcp ...
2665a1173273SAlexandr Nedvedicky 				 * then we have no other option than to forward
2666a1173273SAlexandr Nedvedicky 				 * packet on loopback and give up any attempt
2667a1173273SAlexandr Nedvedicky 				 * to create a fake response.
2668a1173273SAlexandr Nedvedicky 				 */
2669a1173273SAlexandr Nedvedicky 				if (IPF_IS_LOOPBACK(qpi->qpi_flags) &&
2670a1173273SAlexandr Nedvedicky 				    FR_ISBLOCK(pass)) {
2671a1173273SAlexandr Nedvedicky 					if (fr_make_rst(fin) == 0) {
2672a1173273SAlexandr Nedvedicky 						IPF_BUMP(
2673a1173273SAlexandr Nedvedicky 						ifs->ifs_frstats[out].fr_ret);
2674a1173273SAlexandr Nedvedicky 					}
2675a1173273SAlexandr Nedvedicky 					else if (mp != NULL) {
2676a1173273SAlexandr Nedvedicky 					/*
2677a1173273SAlexandr Nedvedicky 					 * we drop packet silently in case we
2678a1173273SAlexandr Nedvedicky 					 * failed assemble fake response for it
2679a1173273SAlexandr Nedvedicky 					 */
2680a1173273SAlexandr Nedvedicky 						FREE_MB_T(*mp);
2681a1173273SAlexandr Nedvedicky 						m = *mp = NULL;
2682a1173273SAlexandr Nedvedicky 					}
2683a1173273SAlexandr Nedvedicky 
2684a1173273SAlexandr Nedvedicky 					IPF_BUMP(
2685a1173273SAlexandr Nedvedicky 					    ifs->ifs_frstats[out].fr_block);
2686a1173273SAlexandr Nedvedicky 					RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
2687a1173273SAlexandr Nedvedicky 
2688a1173273SAlexandr Nedvedicky 					return (0);
2689a1173273SAlexandr Nedvedicky 				 }
2690a1173273SAlexandr Nedvedicky #endif /* _KERNEL && _SOLARIS2 >= 10 */
26917c478bd9Sstevel@tonic-gate 				if (fr_send_reset(fin) == 0) {
2692cbded9aeSdr 					IPF_BUMP(ifs->ifs_frstats[1].fr_ret);
26937c478bd9Sstevel@tonic-gate 				}
26947c478bd9Sstevel@tonic-gate 			}
26957c478bd9Sstevel@tonic-gate 		} else {
26967c478bd9Sstevel@tonic-gate 			if (pass & FR_RETRST)
26977c478bd9Sstevel@tonic-gate 				fin->fin_error = ECONNRESET;
26987c478bd9Sstevel@tonic-gate 		}
26997c478bd9Sstevel@tonic-gate 	}
27007c478bd9Sstevel@tonic-gate 
27017c478bd9Sstevel@tonic-gate 	/*
27027c478bd9Sstevel@tonic-gate 	 * If we didn't drop off the bottom of the list of rules (and thus
27037c478bd9Sstevel@tonic-gate 	 * the 'current' rule fr is not NULL), then we may have some extra
27047c478bd9Sstevel@tonic-gate 	 * instructions about what to do with a packet.
27057c478bd9Sstevel@tonic-gate 	 * Once we're finished return to our caller, freeing the packet if
27067c478bd9Sstevel@tonic-gate 	 * we are dropping it (* BSD ONLY *).
2707ab25eeb5Syz 	 * Reassign m from fin_m as we may have a new buffer, now.
27087c478bd9Sstevel@tonic-gate 	 */
27097c478bd9Sstevel@tonic-gate filtered:
2710ab25eeb5Syz 	m = fin->fin_m;
27117c478bd9Sstevel@tonic-gate 
27127c478bd9Sstevel@tonic-gate 	if (fr != NULL) {
27137c478bd9Sstevel@tonic-gate 		frdest_t *fdp;
27147c478bd9Sstevel@tonic-gate 
27157c478bd9Sstevel@tonic-gate 		fdp = &fr->fr_tifs[fin->fin_rev];
27167c478bd9Sstevel@tonic-gate 
27177c478bd9Sstevel@tonic-gate 		if (!out && (pass & FR_FASTROUTE)) {
27187c478bd9Sstevel@tonic-gate 			/*
27197c478bd9Sstevel@tonic-gate 			 * For fastroute rule, no destioation interface defined
27207c478bd9Sstevel@tonic-gate 			 * so pass NULL as the frdest_t parameter
27217c478bd9Sstevel@tonic-gate 			 */
27227c478bd9Sstevel@tonic-gate 			(void) fr_fastroute(m, mp, fin, NULL);
27237c478bd9Sstevel@tonic-gate 			m = *mp = NULL;
27247c478bd9Sstevel@tonic-gate 		} else if ((fdp->fd_ifp != NULL) &&
27257c478bd9Sstevel@tonic-gate 			   (fdp->fd_ifp != (struct ifnet *)-1)) {
27267c478bd9Sstevel@tonic-gate 			/* this is for to rules: */
27277c478bd9Sstevel@tonic-gate 			(void) fr_fastroute(m, mp, fin, fdp);
27287c478bd9Sstevel@tonic-gate 			m = *mp = NULL;
27297c478bd9Sstevel@tonic-gate 		}
27307c478bd9Sstevel@tonic-gate 
27317c478bd9Sstevel@tonic-gate 		/*
27327c478bd9Sstevel@tonic-gate 		 * Generate a duplicated packet.
27337c478bd9Sstevel@tonic-gate 		 */
27347c478bd9Sstevel@tonic-gate 		if (mc != NULL)
27357c478bd9Sstevel@tonic-gate 			(void) fr_fastroute(mc, &mc, fin, &fr->fr_dif);
27367c478bd9Sstevel@tonic-gate 	}
27377c478bd9Sstevel@tonic-gate 
273833f2fefdSDarren Reed 	if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT))
273933f2fefdSDarren Reed 		nat_uncreate(fin);
274033f2fefdSDarren Reed 
27417c478bd9Sstevel@tonic-gate 	/*
27427c478bd9Sstevel@tonic-gate 	 * This late because the likes of fr_fastroute() use fin_fr.
27437c478bd9Sstevel@tonic-gate 	 */
2744f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
27457c478bd9Sstevel@tonic-gate 
2746ab25eeb5Syz finished:
27477c478bd9Sstevel@tonic-gate 	if (!FR_ISPASS(pass)) {
2748cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_block);
2749ab25eeb5Syz 		if (*mp != NULL) {
27507c478bd9Sstevel@tonic-gate 			FREE_MB_T(*mp);
27517c478bd9Sstevel@tonic-gate 			m = *mp = NULL;
27527c478bd9Sstevel@tonic-gate 		}
2753ab25eeb5Syz 	} else {
2754cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_pass);
27557c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && defined(__sgi)
2756ab25eeb5Syz 		if ((fin->fin_hbuf != NULL) &&
2757ab25eeb5Syz 		    (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) {
2758ab25eeb5Syz 			COPYBACK(m, 0, fin->fin_plen, fin->fin_hbuf);
27597c478bd9Sstevel@tonic-gate 		}
27607c478bd9Sstevel@tonic-gate #endif
2761ab25eeb5Syz 	}
2762ab25eeb5Syz 
2763ab25eeb5Syz 	SPL_X(s);
2764ab25eeb5Syz 
27657c478bd9Sstevel@tonic-gate #ifdef _KERNEL
2766ab25eeb5Syz # if OpenBSD >= 200311
2767ab25eeb5Syz 	if (FR_ISPASS(pass) && (v == 4)) {
2768ab25eeb5Syz 		ip = fin->fin_ip;
2769ab25eeb5Syz 		ip->ip_len = ntohs(ip->ip_len);
2770ab25eeb5Syz 		ip->ip_off = ntohs(ip->ip_off);
2771ab25eeb5Syz 	}
2772ab25eeb5Syz # endif
27737c478bd9Sstevel@tonic-gate 	return (FR_ISPASS(pass)) ? 0 : fin->fin_error;
27747c478bd9Sstevel@tonic-gate #else /* _KERNEL */
2775ab25eeb5Syz 	FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass));
27767c478bd9Sstevel@tonic-gate 	if ((pass & FR_NOMATCH) != 0)
27777c478bd9Sstevel@tonic-gate 		return 1;
27787c478bd9Sstevel@tonic-gate 
27797c478bd9Sstevel@tonic-gate 	if ((pass & FR_RETMASK) != 0)
27807c478bd9Sstevel@tonic-gate 		switch (pass & FR_RETMASK)
27817c478bd9Sstevel@tonic-gate 		{
27827c478bd9Sstevel@tonic-gate 		case FR_RETRST :
27837c478bd9Sstevel@tonic-gate 			return 3;
27847c478bd9Sstevel@tonic-gate 		case FR_RETICMP :
27857c478bd9Sstevel@tonic-gate 			return 4;
27867c478bd9Sstevel@tonic-gate 		case FR_FAKEICMP :
27877c478bd9Sstevel@tonic-gate 			return 5;
27887c478bd9Sstevel@tonic-gate 		}
27897c478bd9Sstevel@tonic-gate 
27907c478bd9Sstevel@tonic-gate 	switch (pass & FR_CMDMASK)
27917c478bd9Sstevel@tonic-gate 	{
27927c478bd9Sstevel@tonic-gate 	case FR_PASS :
27937c478bd9Sstevel@tonic-gate 		return 0;
27947c478bd9Sstevel@tonic-gate 	case FR_BLOCK :
27957c478bd9Sstevel@tonic-gate 		return -1;
27967c478bd9Sstevel@tonic-gate 	case FR_AUTH :
27977c478bd9Sstevel@tonic-gate 		return -2;
27987c478bd9Sstevel@tonic-gate 	case FR_ACCOUNT :
27997c478bd9Sstevel@tonic-gate 		return -3;
28007c478bd9Sstevel@tonic-gate 	case FR_PREAUTH :
28017c478bd9Sstevel@tonic-gate 		return -4;
28027c478bd9Sstevel@tonic-gate 	}
28037c478bd9Sstevel@tonic-gate 	return 2;
28047c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
28057c478bd9Sstevel@tonic-gate }
28067c478bd9Sstevel@tonic-gate 
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOG
28097c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
28107c478bd9Sstevel@tonic-gate /* Function:    fr_dolog                                                    */
28117c478bd9Sstevel@tonic-gate /* Returns:     frentry_t* - returns contents of fin_fr (no change made)    */
28127c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
28137c478bd9Sstevel@tonic-gate /*              passp(IO) - pointer to current/new filter decision (unused) */
28147c478bd9Sstevel@tonic-gate /*                                                                          */
28157c478bd9Sstevel@tonic-gate /* Checks flags set to see how a packet should be logged, if it is to be    */
28167c478bd9Sstevel@tonic-gate /* logged.  Adjust statistics based on its success or not.                  */
28177c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
28187c478bd9Sstevel@tonic-gate frentry_t *fr_dolog(fin, passp)
28197c478bd9Sstevel@tonic-gate fr_info_t *fin;
28207c478bd9Sstevel@tonic-gate u_32_t *passp;
28217c478bd9Sstevel@tonic-gate {
28227c478bd9Sstevel@tonic-gate 	u_32_t pass;
28237c478bd9Sstevel@tonic-gate 	int out;
2824f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
28257c478bd9Sstevel@tonic-gate 
28267c478bd9Sstevel@tonic-gate 	out = fin->fin_out;
28277c478bd9Sstevel@tonic-gate 	pass = *passp;
28287c478bd9Sstevel@tonic-gate 
2829f4b3ec61Sdh 	if ((ifs->ifs_fr_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
28307c478bd9Sstevel@tonic-gate 		pass |= FF_LOGNOMATCH;
2831cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_npkl);
28327c478bd9Sstevel@tonic-gate 		goto logit;
28337c478bd9Sstevel@tonic-gate 	} else if (((pass & FR_LOGMASK) == FR_LOGP) ||
2834f4b3ec61Sdh 	    (FR_ISPASS(pass) && (ifs->ifs_fr_flags & FF_LOGPASS))) {
28357c478bd9Sstevel@tonic-gate 		if ((pass & FR_LOGMASK) != FR_LOGP)
28367c478bd9Sstevel@tonic-gate 			pass |= FF_LOGPASS;
2837cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_ppkl);
28387c478bd9Sstevel@tonic-gate 		goto logit;
28397c478bd9Sstevel@tonic-gate 	} else if (((pass & FR_LOGMASK) == FR_LOGB) ||
2840f4b3ec61Sdh 		   (FR_ISBLOCK(pass) && (ifs->ifs_fr_flags & FF_LOGBLOCK))) {
28417c478bd9Sstevel@tonic-gate 		if ((pass & FR_LOGMASK) != FR_LOGB)
28427c478bd9Sstevel@tonic-gate 			pass |= FF_LOGBLOCK;
2843cbded9aeSdr 		IPF_BUMP(ifs->ifs_frstats[out].fr_bpkl);
28447c478bd9Sstevel@tonic-gate logit:
28457c478bd9Sstevel@tonic-gate 		if (ipflog(fin, pass) == -1) {
2846cbded9aeSdr 			IPF_BUMP(ifs->ifs_frstats[out].fr_skip);
28477c478bd9Sstevel@tonic-gate 
28487c478bd9Sstevel@tonic-gate 			/*
28497c478bd9Sstevel@tonic-gate 			 * If the "or-block" option has been used then
28507c478bd9Sstevel@tonic-gate 			 * block the packet if we failed to log it.
28517c478bd9Sstevel@tonic-gate 			 */
28527c478bd9Sstevel@tonic-gate 			if ((pass & FR_LOGORBLOCK) &&
28537c478bd9Sstevel@tonic-gate 			    FR_ISPASS(pass)) {
28547c478bd9Sstevel@tonic-gate 				pass &= ~FR_CMDMASK;
28557c478bd9Sstevel@tonic-gate 				pass |= FR_BLOCK;
28567c478bd9Sstevel@tonic-gate 			}
28577c478bd9Sstevel@tonic-gate 		}
28587c478bd9Sstevel@tonic-gate 		*passp = pass;
28597c478bd9Sstevel@tonic-gate 	}
28607c478bd9Sstevel@tonic-gate 
28617c478bd9Sstevel@tonic-gate 	return fin->fin_fr;
28627c478bd9Sstevel@tonic-gate }
28637c478bd9Sstevel@tonic-gate #endif /* IPFILTER_LOG */
28647c478bd9Sstevel@tonic-gate 
28657c478bd9Sstevel@tonic-gate 
28667c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
28677c478bd9Sstevel@tonic-gate /* Function:    ipf_cksum                                                   */
28687c478bd9Sstevel@tonic-gate /* Returns:     u_short - IP header checksum                                */
28697c478bd9Sstevel@tonic-gate /* Parameters:  addr(I) - pointer to start of buffer to checksum            */
28707c478bd9Sstevel@tonic-gate /*              len(I)  - length of buffer in bytes                         */
28717c478bd9Sstevel@tonic-gate /*                                                                          */
28727c478bd9Sstevel@tonic-gate /* Calculate the two's complement 16 bit checksum of the buffer passed.     */
28737c478bd9Sstevel@tonic-gate /*                                                                          */
28747c478bd9Sstevel@tonic-gate /* N.B.: addr should be 16bit aligned.                                      */
28757c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
28767c478bd9Sstevel@tonic-gate u_short ipf_cksum(addr, len)
28777c478bd9Sstevel@tonic-gate u_short *addr;
28787c478bd9Sstevel@tonic-gate int len;
28797c478bd9Sstevel@tonic-gate {
28807c478bd9Sstevel@tonic-gate 	u_32_t sum = 0;
28817c478bd9Sstevel@tonic-gate 
28827c478bd9Sstevel@tonic-gate 	for (sum = 0; len > 1; len -= 2)
28837c478bd9Sstevel@tonic-gate 		sum += *addr++;
28847c478bd9Sstevel@tonic-gate 
28857c478bd9Sstevel@tonic-gate 	/* mop up an odd byte, if necessary */
28867c478bd9Sstevel@tonic-gate 	if (len == 1)
28877c478bd9Sstevel@tonic-gate 		sum += *(u_char *)addr;
28887c478bd9Sstevel@tonic-gate 
28897c478bd9Sstevel@tonic-gate 	/*
28907c478bd9Sstevel@tonic-gate 	 * add back carry outs from top 16 bits to low 16 bits
28917c478bd9Sstevel@tonic-gate 	 */
28927c478bd9Sstevel@tonic-gate 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
28937c478bd9Sstevel@tonic-gate 	sum += (sum >> 16);			/* add carry */
28947c478bd9Sstevel@tonic-gate 	return (u_short)(~sum);
28957c478bd9Sstevel@tonic-gate }
28967c478bd9Sstevel@tonic-gate 
28977c478bd9Sstevel@tonic-gate 
28987c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
28997c478bd9Sstevel@tonic-gate /* Function:    fr_cksum                                                    */
29007c478bd9Sstevel@tonic-gate /* Returns:     u_short - layer 4 checksum                                  */
2901ab25eeb5Syz /* Parameters:  m(I  )     - pointer to buffer holding packet               */
2902ab25eeb5Syz /*              ip(I)      - pointer to IP header                           */
2903ab25eeb5Syz /*              l4proto(I) - protocol to caclulate checksum for             */
2904ab25eeb5Syz /*              l4hdr(I)   - pointer to layer 4 header                      */
29057c478bd9Sstevel@tonic-gate /*                                                                          */
29067c478bd9Sstevel@tonic-gate /* Calculates the TCP checksum for the packet held in "m", using the data   */
29077c478bd9Sstevel@tonic-gate /* in the IP header "ip" to seed it.                                        */
29087c478bd9Sstevel@tonic-gate /*                                                                          */
29097c478bd9Sstevel@tonic-gate /* NB: This function assumes we've pullup'd enough for all of the IP header */
29107c478bd9Sstevel@tonic-gate /* and the TCP header.  We also assume that data blocks aren't allocated in */
29117c478bd9Sstevel@tonic-gate /* odd sizes.                                                               */
2912ab25eeb5Syz /*                                                                          */
2913ab25eeb5Syz /* Expects ip_len to be in host byte order when called.                     */
29147c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
29157c478bd9Sstevel@tonic-gate u_short fr_cksum(m, ip, l4proto, l4hdr)
29167c478bd9Sstevel@tonic-gate mb_t *m;
29177c478bd9Sstevel@tonic-gate ip_t *ip;
29187c478bd9Sstevel@tonic-gate int l4proto;
29197c478bd9Sstevel@tonic-gate void *l4hdr;
29207c478bd9Sstevel@tonic-gate {
29217c478bd9Sstevel@tonic-gate 	u_short *sp, slen, sumsave, l4hlen, *csump;
29227c478bd9Sstevel@tonic-gate 	u_int sum, sum2;
29237c478bd9Sstevel@tonic-gate 	int hlen;
29247c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
29257c478bd9Sstevel@tonic-gate 	ip6_t *ip6;
29267c478bd9Sstevel@tonic-gate #endif
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	csump = NULL;
29297c478bd9Sstevel@tonic-gate 	sumsave = 0;
29307c478bd9Sstevel@tonic-gate 	l4hlen = 0;
29317c478bd9Sstevel@tonic-gate 	sp = NULL;
29327c478bd9Sstevel@tonic-gate 	slen = 0;
29337c478bd9Sstevel@tonic-gate 	hlen = 0;
29347c478bd9Sstevel@tonic-gate 	sum = 0;
29357c478bd9Sstevel@tonic-gate 
29367c478bd9Sstevel@tonic-gate 	/*
29377c478bd9Sstevel@tonic-gate 	 * Add up IP Header portion
29387c478bd9Sstevel@tonic-gate 	 */
29397c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
29407c478bd9Sstevel@tonic-gate 	if (IP_V(ip) == 4) {
29417c478bd9Sstevel@tonic-gate #endif
29427c478bd9Sstevel@tonic-gate 		hlen = IP_HL(ip) << 2;
29437c478bd9Sstevel@tonic-gate 		slen = ip->ip_len - hlen;
2944ab25eeb5Syz 		sum = htons((u_short)l4proto);
29457c478bd9Sstevel@tonic-gate 		sum += htons(slen);
29467c478bd9Sstevel@tonic-gate 		sp = (u_short *)&ip->ip_src;
29477c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* ip_src */
29487c478bd9Sstevel@tonic-gate 		sum += *sp++;
29497c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* ip_dst */
29507c478bd9Sstevel@tonic-gate 		sum += *sp++;
29517c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
29527c478bd9Sstevel@tonic-gate 	} else if (IP_V(ip) == 6) {
29537c478bd9Sstevel@tonic-gate 		ip6 = (ip6_t *)ip;
29547c478bd9Sstevel@tonic-gate 		hlen = sizeof(*ip6);
29557c478bd9Sstevel@tonic-gate 		slen = ntohs(ip6->ip6_plen);
2956ab25eeb5Syz 		sum = htons((u_short)l4proto);
29577c478bd9Sstevel@tonic-gate 		sum += htons(slen);
29587c478bd9Sstevel@tonic-gate 		sp = (u_short *)&ip6->ip6_src;
29597c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* ip6_src */
29607c478bd9Sstevel@tonic-gate 		sum += *sp++;
29617c478bd9Sstevel@tonic-gate 		sum += *sp++;
29627c478bd9Sstevel@tonic-gate 		sum += *sp++;
29637c478bd9Sstevel@tonic-gate 		sum += *sp++;
29647c478bd9Sstevel@tonic-gate 		sum += *sp++;
29657c478bd9Sstevel@tonic-gate 		sum += *sp++;
29667c478bd9Sstevel@tonic-gate 		sum += *sp++;
29677c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* ip6_dst */
29687c478bd9Sstevel@tonic-gate 		sum += *sp++;
29697c478bd9Sstevel@tonic-gate 		sum += *sp++;
29707c478bd9Sstevel@tonic-gate 		sum += *sp++;
29717c478bd9Sstevel@tonic-gate 		sum += *sp++;
29727c478bd9Sstevel@tonic-gate 		sum += *sp++;
29737c478bd9Sstevel@tonic-gate 		sum += *sp++;
29747663b816Sml 		sum += *sp++;
29757c478bd9Sstevel@tonic-gate 	}
29767c478bd9Sstevel@tonic-gate #endif
29777c478bd9Sstevel@tonic-gate 
29787c478bd9Sstevel@tonic-gate 	switch (l4proto)
29797c478bd9Sstevel@tonic-gate 	{
29807c478bd9Sstevel@tonic-gate 	case IPPROTO_UDP :
29817c478bd9Sstevel@tonic-gate 		csump = &((udphdr_t *)l4hdr)->uh_sum;
29827c478bd9Sstevel@tonic-gate 		l4hlen = sizeof(udphdr_t);
29837c478bd9Sstevel@tonic-gate 		break;
29847c478bd9Sstevel@tonic-gate 
29857c478bd9Sstevel@tonic-gate 	case IPPROTO_TCP :
29867c478bd9Sstevel@tonic-gate 		csump = &((tcphdr_t *)l4hdr)->th_sum;
29877c478bd9Sstevel@tonic-gate 		l4hlen = sizeof(tcphdr_t);
29887c478bd9Sstevel@tonic-gate 		break;
29897c478bd9Sstevel@tonic-gate 	case IPPROTO_ICMP :
29907c478bd9Sstevel@tonic-gate 		csump = &((icmphdr_t *)l4hdr)->icmp_cksum;
29917c478bd9Sstevel@tonic-gate 		l4hlen = 4;
2992ab25eeb5Syz 		sum = 0;
29937c478bd9Sstevel@tonic-gate 		break;
29947c478bd9Sstevel@tonic-gate 	default :
29957c478bd9Sstevel@tonic-gate 		break;
29967c478bd9Sstevel@tonic-gate 	}
29977c478bd9Sstevel@tonic-gate 
29987c478bd9Sstevel@tonic-gate 	if (csump != NULL) {
29997c478bd9Sstevel@tonic-gate 		sumsave = *csump;
30007c478bd9Sstevel@tonic-gate 		*csump = 0;
30017c478bd9Sstevel@tonic-gate 	}
30027c478bd9Sstevel@tonic-gate 
30037c478bd9Sstevel@tonic-gate 	l4hlen = l4hlen;	/* LINT */
30047c478bd9Sstevel@tonic-gate 
30057c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
30067c478bd9Sstevel@tonic-gate # ifdef MENTAT
30077c478bd9Sstevel@tonic-gate 	{
30087c478bd9Sstevel@tonic-gate 	void *rp = m->b_rptr;
30097c478bd9Sstevel@tonic-gate 
30107c478bd9Sstevel@tonic-gate 	if ((unsigned char *)ip > m->b_rptr && (unsigned char *)ip < m->b_wptr)
30117c478bd9Sstevel@tonic-gate 		m->b_rptr = (u_char *)ip;
30127c478bd9Sstevel@tonic-gate 	sum2 = ip_cksum(m, hlen, sum);	/* hlen == offset */
30137c478bd9Sstevel@tonic-gate 	m->b_rptr = rp;
30147c478bd9Sstevel@tonic-gate 	sum2 = (sum2 & 0xffff) + (sum2 >> 16);
30157c478bd9Sstevel@tonic-gate 	sum2 = ~sum2 & 0xffff;
3016ab25eeb5Syz 	}
30177c478bd9Sstevel@tonic-gate # else /* MENTAT */
30187c478bd9Sstevel@tonic-gate #  if defined(BSD) || defined(sun)
30197c478bd9Sstevel@tonic-gate #   if BSD >= 199103
30207c478bd9Sstevel@tonic-gate 	m->m_data += hlen;
30217c478bd9Sstevel@tonic-gate #   else
30227c478bd9Sstevel@tonic-gate 	m->m_off += hlen;
30237c478bd9Sstevel@tonic-gate #   endif
30247c478bd9Sstevel@tonic-gate 	m->m_len -= hlen;
30257c478bd9Sstevel@tonic-gate 	sum2 = in_cksum(m, slen);
30267c478bd9Sstevel@tonic-gate 	m->m_len += hlen;
30277c478bd9Sstevel@tonic-gate #   if BSD >= 199103
30287c478bd9Sstevel@tonic-gate 	m->m_data -= hlen;
30297c478bd9Sstevel@tonic-gate #   else
30307c478bd9Sstevel@tonic-gate 	m->m_off -= hlen;
30317c478bd9Sstevel@tonic-gate #   endif
30327c478bd9Sstevel@tonic-gate 	/*
30337c478bd9Sstevel@tonic-gate 	 * Both sum and sum2 are partial sums, so combine them together.
30347c478bd9Sstevel@tonic-gate 	 */
30357c478bd9Sstevel@tonic-gate 	sum += ~sum2 & 0xffff;
30367c478bd9Sstevel@tonic-gate 	while (sum > 0xffff)
30377c478bd9Sstevel@tonic-gate 		sum = (sum & 0xffff) + (sum >> 16);
30387c478bd9Sstevel@tonic-gate 	sum2 = ~sum & 0xffff;
30397c478bd9Sstevel@tonic-gate #  else /* defined(BSD) || defined(sun) */
30407c478bd9Sstevel@tonic-gate {
30417c478bd9Sstevel@tonic-gate 	union {
30427c478bd9Sstevel@tonic-gate 		u_char	c[2];
30437c478bd9Sstevel@tonic-gate 		u_short	s;
30447c478bd9Sstevel@tonic-gate 	} bytes;
30457c478bd9Sstevel@tonic-gate 	u_short len = ip->ip_len;
30467c478bd9Sstevel@tonic-gate #   if defined(__sgi)
30477c478bd9Sstevel@tonic-gate 	int add;
30487c478bd9Sstevel@tonic-gate #   endif
30497c478bd9Sstevel@tonic-gate 
30507c478bd9Sstevel@tonic-gate 	/*
30517c478bd9Sstevel@tonic-gate 	 * Add up IP Header portion
30527c478bd9Sstevel@tonic-gate 	 */
30537c478bd9Sstevel@tonic-gate 	if (sp != (u_short *)l4hdr)
30547c478bd9Sstevel@tonic-gate 		sp = (u_short *)l4hdr;
30557c478bd9Sstevel@tonic-gate 
3056ab25eeb5Syz 	switch (l4proto)
30577c478bd9Sstevel@tonic-gate 	{
30587c478bd9Sstevel@tonic-gate 	case IPPROTO_UDP :
30597c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* sport */
30607c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* dport */
30617c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* udp length */
30627c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* checksum */
30637c478bd9Sstevel@tonic-gate 		break;
30647c478bd9Sstevel@tonic-gate 
30657c478bd9Sstevel@tonic-gate 	case IPPROTO_TCP :
30667c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* sport */
30677c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* dport */
30687c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* seq */
30697c478bd9Sstevel@tonic-gate 		sum += *sp++;
30707c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* ack */
30717c478bd9Sstevel@tonic-gate 		sum += *sp++;
30727c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* off */
30737c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* win */
30747c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* checksum */
30757c478bd9Sstevel@tonic-gate 		sum += *sp++;	/* urp */
30767c478bd9Sstevel@tonic-gate 		break;
3077ab25eeb5Syz 	case IPPROTO_ICMP :
3078ab25eeb5Syz 		sum = *sp++;	/* type/code */
3079ab25eeb5Syz 		sum += *sp++;	/* checksum */
3080ab25eeb5Syz 		break;
30817c478bd9Sstevel@tonic-gate 	}
30827c478bd9Sstevel@tonic-gate 
30837c478bd9Sstevel@tonic-gate #   ifdef	__sgi
30847c478bd9Sstevel@tonic-gate 	/*
30857c478bd9Sstevel@tonic-gate 	 * In case we had to copy the IP & TCP header out of mbufs,
30867c478bd9Sstevel@tonic-gate 	 * skip over the mbuf bits which are the header
30877c478bd9Sstevel@tonic-gate 	 */
30887c478bd9Sstevel@tonic-gate 	if ((caddr_t)ip != mtod(m, caddr_t)) {
30897c478bd9Sstevel@tonic-gate 		hlen = (caddr_t)sp - (caddr_t)ip;
30907c478bd9Sstevel@tonic-gate 		while (hlen) {
30917c478bd9Sstevel@tonic-gate 			add = MIN(hlen, m->m_len);
30927c478bd9Sstevel@tonic-gate 			sp = (u_short *)(mtod(m, caddr_t) + add);
30937c478bd9Sstevel@tonic-gate 			hlen -= add;
30947c478bd9Sstevel@tonic-gate 			if (add == m->m_len) {
30957c478bd9Sstevel@tonic-gate 				m = m->m_next;
30967c478bd9Sstevel@tonic-gate 				if (!hlen) {
30977c478bd9Sstevel@tonic-gate 					if (!m)
30987c478bd9Sstevel@tonic-gate 						break;
30997c478bd9Sstevel@tonic-gate 					sp = mtod(m, u_short *);
31007c478bd9Sstevel@tonic-gate 				}
31017c478bd9Sstevel@tonic-gate 				PANIC((!m),("fr_cksum(1): not enough data"));
31027c478bd9Sstevel@tonic-gate 			}
31037c478bd9Sstevel@tonic-gate 		}
31047c478bd9Sstevel@tonic-gate 	}
31057c478bd9Sstevel@tonic-gate #   endif
31067c478bd9Sstevel@tonic-gate 
3107ab25eeb5Syz 	len -= (l4hlen + hlen);
31087c478bd9Sstevel@tonic-gate 	if (len <= 0)
31097c478bd9Sstevel@tonic-gate 		goto nodata;
31107c478bd9Sstevel@tonic-gate 
31117c478bd9Sstevel@tonic-gate 	while (len > 1) {
31127c478bd9Sstevel@tonic-gate 		if (((caddr_t)sp - mtod(m, caddr_t)) >= m->m_len) {
31137c478bd9Sstevel@tonic-gate 			m = m->m_next;
31147c478bd9Sstevel@tonic-gate 			PANIC((!m),("fr_cksum(2): not enough data"));
31157c478bd9Sstevel@tonic-gate 			sp = mtod(m, u_short *);
31167c478bd9Sstevel@tonic-gate 		}
31177c478bd9Sstevel@tonic-gate 		if (((caddr_t)(sp + 1) - mtod(m, caddr_t)) > m->m_len) {
31187c478bd9Sstevel@tonic-gate 			bytes.c[0] = *(u_char *)sp;
31197c478bd9Sstevel@tonic-gate 			m = m->m_next;
31207c478bd9Sstevel@tonic-gate 			PANIC((!m),("fr_cksum(3): not enough data"));
31217c478bd9Sstevel@tonic-gate 			sp = mtod(m, u_short *);
31227c478bd9Sstevel@tonic-gate 			bytes.c[1] = *(u_char *)sp;
31237c478bd9Sstevel@tonic-gate 			sum += bytes.s;
31247c478bd9Sstevel@tonic-gate 			sp = (u_short *)((u_char *)sp + 1);
31257c478bd9Sstevel@tonic-gate 		}
31267c478bd9Sstevel@tonic-gate 		if ((u_long)sp & 1) {
31277c478bd9Sstevel@tonic-gate 			bcopy((char *)sp++, (char *)&bytes.s, sizeof(bytes.s));
31287c478bd9Sstevel@tonic-gate 			sum += bytes.s;
31297c478bd9Sstevel@tonic-gate 		} else
31307c478bd9Sstevel@tonic-gate 			sum += *sp++;
31317c478bd9Sstevel@tonic-gate 		len -= 2;
31327c478bd9Sstevel@tonic-gate 	}
31337c478bd9Sstevel@tonic-gate 
31347c478bd9Sstevel@tonic-gate 	if (len != 0)
31357c478bd9Sstevel@tonic-gate 		sum += ntohs(*(u_char *)sp << 8);
31367c478bd9Sstevel@tonic-gate nodata:
31377c478bd9Sstevel@tonic-gate 	while (sum > 0xffff)
31387c478bd9Sstevel@tonic-gate 		sum = (sum & 0xffff) + (sum >> 16);
31397c478bd9Sstevel@tonic-gate 	sum2 = (u_short)(~sum & 0xffff);
31407c478bd9Sstevel@tonic-gate }
31417c478bd9Sstevel@tonic-gate #  endif /*  defined(BSD) || defined(sun) */
31427c478bd9Sstevel@tonic-gate # endif /* MENTAT */
31437c478bd9Sstevel@tonic-gate #else /* _KERNEL */
31447c478bd9Sstevel@tonic-gate 	for (; slen > 1; slen -= 2)
31457c478bd9Sstevel@tonic-gate 	        sum += *sp++;
31467c478bd9Sstevel@tonic-gate 	if (slen)
31477c478bd9Sstevel@tonic-gate 		sum += ntohs(*(u_char *)sp << 8);
31487c478bd9Sstevel@tonic-gate 	while (sum > 0xffff)
31497c478bd9Sstevel@tonic-gate 		sum = (sum & 0xffff) + (sum >> 16);
31507c478bd9Sstevel@tonic-gate 	sum2 = (u_short)(~sum & 0xffff);
31517c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
31527c478bd9Sstevel@tonic-gate 	if (csump != NULL)
31537c478bd9Sstevel@tonic-gate 		*csump = sumsave;
31547c478bd9Sstevel@tonic-gate 	return sum2;
31557c478bd9Sstevel@tonic-gate }
31567c478bd9Sstevel@tonic-gate 
31577c478bd9Sstevel@tonic-gate 
31587c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && ( ((BSD < 199103) && !defined(MENTAT)) || \
3159ab25eeb5Syz     defined(__sgi) ) && !defined(linux) && !defined(_AIX51)
31607c478bd9Sstevel@tonic-gate /*
31617c478bd9Sstevel@tonic-gate  * Copyright (c) 1982, 1986, 1988, 1991, 1993
31627c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
31637c478bd9Sstevel@tonic-gate  *
31647c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
31657c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
31667c478bd9Sstevel@tonic-gate  * are met:
31677c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
31687c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
31697c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
31707c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
31717c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31727c478bd9Sstevel@tonic-gate  * 3. Neither the name of the University nor the names of its contributors
31737c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
31747c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
31757c478bd9Sstevel@tonic-gate  *
31767c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31777c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31787c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31797c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31807c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31817c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31827c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31837c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31847c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31857c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31867c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
31877c478bd9Sstevel@tonic-gate  *
31887c478bd9Sstevel@tonic-gate  *	@(#)uipc_mbuf.c	8.2 (Berkeley) 1/4/94
3189ab25eeb5Syz  * $Id: fil.c,v 2.243.2.64 2005/08/13 05:19:59 darrenr Exp $
31907c478bd9Sstevel@tonic-gate  */
31917c478bd9Sstevel@tonic-gate /*
31927c478bd9Sstevel@tonic-gate  * Copy data from an mbuf chain starting "off" bytes from the beginning,
31937c478bd9Sstevel@tonic-gate  * continuing for "len" bytes, into the indicated buffer.
31947c478bd9Sstevel@tonic-gate  */
31957c478bd9Sstevel@tonic-gate void
31967c478bd9Sstevel@tonic-gate m_copydata(m, off, len, cp)
31977c478bd9Sstevel@tonic-gate 	mb_t *m;
31987c478bd9Sstevel@tonic-gate 	int off;
31997c478bd9Sstevel@tonic-gate 	int len;
32007c478bd9Sstevel@tonic-gate 	caddr_t cp;
32017c478bd9Sstevel@tonic-gate {
32027c478bd9Sstevel@tonic-gate 	unsigned count;
32037c478bd9Sstevel@tonic-gate 
32047c478bd9Sstevel@tonic-gate 	if (off < 0 || len < 0)
32057c478bd9Sstevel@tonic-gate 		panic("m_copydata");
32067c478bd9Sstevel@tonic-gate 	while (off > 0) {
32077c478bd9Sstevel@tonic-gate 		if (m == 0)
32087c478bd9Sstevel@tonic-gate 			panic("m_copydata");
32097c478bd9Sstevel@tonic-gate 		if (off < m->m_len)
32107c478bd9Sstevel@tonic-gate 			break;
32117c478bd9Sstevel@tonic-gate 		off -= m->m_len;
32127c478bd9Sstevel@tonic-gate 		m = m->m_next;
32137c478bd9Sstevel@tonic-gate 	}
32147c478bd9Sstevel@tonic-gate 	while (len > 0) {
32157c478bd9Sstevel@tonic-gate 		if (m == 0)
32167c478bd9Sstevel@tonic-gate 			panic("m_copydata");
32177c478bd9Sstevel@tonic-gate 		count = MIN(m->m_len - off, len);
32187c478bd9Sstevel@tonic-gate 		bcopy(mtod(m, caddr_t) + off, cp, count);
32197c478bd9Sstevel@tonic-gate 		len -= count;
32207c478bd9Sstevel@tonic-gate 		cp += count;
32217c478bd9Sstevel@tonic-gate 		off = 0;
32227c478bd9Sstevel@tonic-gate 		m = m->m_next;
32237c478bd9Sstevel@tonic-gate 	}
32247c478bd9Sstevel@tonic-gate }
32257c478bd9Sstevel@tonic-gate 
32267c478bd9Sstevel@tonic-gate 
32277c478bd9Sstevel@tonic-gate /*
32287c478bd9Sstevel@tonic-gate  * Copy data from a buffer back into the indicated mbuf chain,
32297c478bd9Sstevel@tonic-gate  * starting "off" bytes from the beginning, extending the mbuf
32307c478bd9Sstevel@tonic-gate  * chain if necessary.
32317c478bd9Sstevel@tonic-gate  */
32327c478bd9Sstevel@tonic-gate void
32337c478bd9Sstevel@tonic-gate m_copyback(m0, off, len, cp)
32347c478bd9Sstevel@tonic-gate 	struct	mbuf *m0;
32357c478bd9Sstevel@tonic-gate 	int off;
32367c478bd9Sstevel@tonic-gate 	int len;
32377c478bd9Sstevel@tonic-gate 	caddr_t cp;
32387c478bd9Sstevel@tonic-gate {
32397c478bd9Sstevel@tonic-gate 	int mlen;
32407c478bd9Sstevel@tonic-gate 	struct mbuf *m = m0, *n;
32417c478bd9Sstevel@tonic-gate 	int totlen = 0;
32427c478bd9Sstevel@tonic-gate 
32437c478bd9Sstevel@tonic-gate 	if (m0 == 0)
32447c478bd9Sstevel@tonic-gate 		return;
32457c478bd9Sstevel@tonic-gate 	while (off > (mlen = m->m_len)) {
32467c478bd9Sstevel@tonic-gate 		off -= mlen;
32477c478bd9Sstevel@tonic-gate 		totlen += mlen;
32487c478bd9Sstevel@tonic-gate 		if (m->m_next == 0) {
32497c478bd9Sstevel@tonic-gate 			n = m_getclr(M_DONTWAIT, m->m_type);
32507c478bd9Sstevel@tonic-gate 			if (n == 0)
32517c478bd9Sstevel@tonic-gate 				goto out;
32527c478bd9Sstevel@tonic-gate 			n->m_len = min(MLEN, len + off);
32537c478bd9Sstevel@tonic-gate 			m->m_next = n;
32547c478bd9Sstevel@tonic-gate 		}
32557c478bd9Sstevel@tonic-gate 		m = m->m_next;
32567c478bd9Sstevel@tonic-gate 	}
32577c478bd9Sstevel@tonic-gate 	while (len > 0) {
3258ab25eeb5Syz 		mlen = min(m->m_len - off, len);
32597c478bd9Sstevel@tonic-gate 		bcopy(cp, off + mtod(m, caddr_t), (unsigned)mlen);
32607c478bd9Sstevel@tonic-gate 		cp += mlen;
32617c478bd9Sstevel@tonic-gate 		len -= mlen;
32627c478bd9Sstevel@tonic-gate 		mlen += off;
32637c478bd9Sstevel@tonic-gate 		off = 0;
32647c478bd9Sstevel@tonic-gate 		totlen += mlen;
32657c478bd9Sstevel@tonic-gate 		if (len == 0)
32667c478bd9Sstevel@tonic-gate 			break;
32677c478bd9Sstevel@tonic-gate 		if (m->m_next == 0) {
32687c478bd9Sstevel@tonic-gate 			n = m_get(M_DONTWAIT, m->m_type);
32697c478bd9Sstevel@tonic-gate 			if (n == 0)
32707c478bd9Sstevel@tonic-gate 				break;
32717c478bd9Sstevel@tonic-gate 			n->m_len = min(MLEN, len);
32727c478bd9Sstevel@tonic-gate 			m->m_next = n;
32737c478bd9Sstevel@tonic-gate 		}
32747c478bd9Sstevel@tonic-gate 		m = m->m_next;
32757c478bd9Sstevel@tonic-gate 	}
32767c478bd9Sstevel@tonic-gate out:
32777c478bd9Sstevel@tonic-gate #if 0
32787c478bd9Sstevel@tonic-gate 	if (((m = m0)->m_flags & M_PKTHDR) && (m->m_pkthdr.len < totlen))
32797c478bd9Sstevel@tonic-gate 		m->m_pkthdr.len = totlen;
32807c478bd9Sstevel@tonic-gate #endif
32817c478bd9Sstevel@tonic-gate 	return;
32827c478bd9Sstevel@tonic-gate }
32837c478bd9Sstevel@tonic-gate #endif /* (_KERNEL) && ( ((BSD < 199103) && !MENTAT) || __sgi) */
32847c478bd9Sstevel@tonic-gate 
32857c478bd9Sstevel@tonic-gate 
32867c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
32877c478bd9Sstevel@tonic-gate /* Function:    fr_findgroup                                                */
32887c478bd9Sstevel@tonic-gate /* Returns:     frgroup_t * - NULL = group not found, else pointer to group */
32897c478bd9Sstevel@tonic-gate /* Parameters:  group(I) - group name to search for                         */
32907c478bd9Sstevel@tonic-gate /*              unit(I)  - device to which this group belongs               */
32917c478bd9Sstevel@tonic-gate /*              set(I)   - which set of rules (inactive/inactive) this is   */
32927c478bd9Sstevel@tonic-gate /*              fgpp(O)  - pointer to place to store pointer to the pointer */
32937c478bd9Sstevel@tonic-gate /*                         to where to add the next (last) group or where   */
32947c478bd9Sstevel@tonic-gate /*                         to delete group from.                            */
32957c478bd9Sstevel@tonic-gate /*                                                                          */
32967c478bd9Sstevel@tonic-gate /* Search amongst the defined groups for a particular group number.         */
32977c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3298f4b3ec61Sdh frgroup_t *fr_findgroup(group, unit, set, fgpp, ifs)
32997c478bd9Sstevel@tonic-gate char *group;
33007c478bd9Sstevel@tonic-gate minor_t unit;
33017c478bd9Sstevel@tonic-gate int set;
33027c478bd9Sstevel@tonic-gate frgroup_t ***fgpp;
3303f4b3ec61Sdh ipf_stack_t *ifs;
33047c478bd9Sstevel@tonic-gate {
33057c478bd9Sstevel@tonic-gate 	frgroup_t *fg, **fgp;
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 	/*
3308ab25eeb5Syz 	 * Which list of groups to search in is dependent on which list of
33097c478bd9Sstevel@tonic-gate 	 * rules are being operated on.
33107c478bd9Sstevel@tonic-gate 	 */
3311f4b3ec61Sdh 	fgp = &ifs->ifs_ipfgroups[unit][set];
33127c478bd9Sstevel@tonic-gate 
33137c478bd9Sstevel@tonic-gate 	while ((fg = *fgp) != NULL) {
33147c478bd9Sstevel@tonic-gate 		if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0)
33157c478bd9Sstevel@tonic-gate 			break;
33167c478bd9Sstevel@tonic-gate 		else
33177c478bd9Sstevel@tonic-gate 			fgp = &fg->fg_next;
33187c478bd9Sstevel@tonic-gate 	}
33197c478bd9Sstevel@tonic-gate 	if (fgpp != NULL)
33207c478bd9Sstevel@tonic-gate 		*fgpp = fgp;
33217c478bd9Sstevel@tonic-gate 	return fg;
33227c478bd9Sstevel@tonic-gate }
33237c478bd9Sstevel@tonic-gate 
33247c478bd9Sstevel@tonic-gate 
33257c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
33267c478bd9Sstevel@tonic-gate /* Function:    fr_addgroup                                                 */
33277c478bd9Sstevel@tonic-gate /* Returns:     frgroup_t * - NULL == did not create group,                 */
33287c478bd9Sstevel@tonic-gate /*                            != NULL == pointer to the group               */
33297c478bd9Sstevel@tonic-gate /* Parameters:  num(I)   - group number to add                              */
33307c478bd9Sstevel@tonic-gate /*              head(I)  - rule pointer that is using this as the head      */
33317c478bd9Sstevel@tonic-gate /*              flags(I) - rule flags which describe the type of rule it is */
33327c478bd9Sstevel@tonic-gate /*              unit(I)  - device to which this group will belong to        */
33337c478bd9Sstevel@tonic-gate /*              set(I)   - which set of rules (inactive/inactive) this is   */
33347c478bd9Sstevel@tonic-gate /* Write Locks: ipf_mutex                                                   */
33357c478bd9Sstevel@tonic-gate /*                                                                          */
33367c478bd9Sstevel@tonic-gate /* Add a new group head, or if it already exists, increase the reference    */
33377c478bd9Sstevel@tonic-gate /* count to it.                                                             */
33387c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3339f4b3ec61Sdh frgroup_t *fr_addgroup(group, head, flags, unit, set, ifs)
33407c478bd9Sstevel@tonic-gate char *group;
33417c478bd9Sstevel@tonic-gate void *head;
33427c478bd9Sstevel@tonic-gate u_32_t flags;
33437c478bd9Sstevel@tonic-gate minor_t unit;
33447c478bd9Sstevel@tonic-gate int set;
3345f4b3ec61Sdh ipf_stack_t *ifs;
33467c478bd9Sstevel@tonic-gate {
33477c478bd9Sstevel@tonic-gate 	frgroup_t *fg, **fgp;
33487c478bd9Sstevel@tonic-gate 	u_32_t gflags;
33497c478bd9Sstevel@tonic-gate 
33507c478bd9Sstevel@tonic-gate 	if (group == NULL)
33517c478bd9Sstevel@tonic-gate 		return NULL;
33527c478bd9Sstevel@tonic-gate 
33537c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGIPF && *group == '\0')
33547c478bd9Sstevel@tonic-gate 		return NULL;
33557c478bd9Sstevel@tonic-gate 
33567c478bd9Sstevel@tonic-gate 	fgp = NULL;
33577c478bd9Sstevel@tonic-gate 	gflags = flags & FR_INOUT;
33587c478bd9Sstevel@tonic-gate 
3359f4b3ec61Sdh 	fg = fr_findgroup(group, unit, set, &fgp, ifs);
33607c478bd9Sstevel@tonic-gate 	if (fg != NULL) {
33617c478bd9Sstevel@tonic-gate 		if (fg->fg_flags == 0)
33627c478bd9Sstevel@tonic-gate 			fg->fg_flags = gflags;
33637c478bd9Sstevel@tonic-gate 		else if (gflags != fg->fg_flags)
33647c478bd9Sstevel@tonic-gate 			return NULL;
33657c478bd9Sstevel@tonic-gate 		fg->fg_ref++;
33667c478bd9Sstevel@tonic-gate 		return fg;
33677c478bd9Sstevel@tonic-gate 	}
33687c478bd9Sstevel@tonic-gate 	KMALLOC(fg, frgroup_t *);
33697c478bd9Sstevel@tonic-gate 	if (fg != NULL) {
33707c478bd9Sstevel@tonic-gate 		fg->fg_head = head;
33717c478bd9Sstevel@tonic-gate 		fg->fg_start = NULL;
33727c478bd9Sstevel@tonic-gate 		fg->fg_next = *fgp;
33737c478bd9Sstevel@tonic-gate 		bcopy(group, fg->fg_name, FR_GROUPLEN);
33747c478bd9Sstevel@tonic-gate 		fg->fg_flags = gflags;
33757c478bd9Sstevel@tonic-gate 		fg->fg_ref = 1;
33767c478bd9Sstevel@tonic-gate 		*fgp = fg;
33777c478bd9Sstevel@tonic-gate 	}
33787c478bd9Sstevel@tonic-gate 	return fg;
33797c478bd9Sstevel@tonic-gate }
33807c478bd9Sstevel@tonic-gate 
33817c478bd9Sstevel@tonic-gate 
33827c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
33837c478bd9Sstevel@tonic-gate /* Function:    fr_delgroup                                                 */
33847c478bd9Sstevel@tonic-gate /* Returns:     Nil                                                         */
33857c478bd9Sstevel@tonic-gate /* Parameters:  group(I) - group name to delete                             */
33867c478bd9Sstevel@tonic-gate /*              unit(I)  - device to which this group belongs               */
33877c478bd9Sstevel@tonic-gate /*              set(I)   - which set of rules (inactive/inactive) this is   */
33887c478bd9Sstevel@tonic-gate /* Write Locks: ipf_mutex                                                   */
33897c478bd9Sstevel@tonic-gate /*                                                                          */
33907c478bd9Sstevel@tonic-gate /* Attempt to delete a group head.                                          */
33917c478bd9Sstevel@tonic-gate /* Only do this when its reference count reaches 0.                         */
33927c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3393f4b3ec61Sdh void fr_delgroup(group, unit, set, ifs)
33947c478bd9Sstevel@tonic-gate char *group;
33957c478bd9Sstevel@tonic-gate minor_t unit;
33967c478bd9Sstevel@tonic-gate int set;
3397f4b3ec61Sdh ipf_stack_t *ifs;
33987c478bd9Sstevel@tonic-gate {
33997c478bd9Sstevel@tonic-gate 	frgroup_t *fg, **fgp;
3400ab25eeb5Syz 
3401f4b3ec61Sdh 	fg = fr_findgroup(group, unit, set, &fgp, ifs);
34027c478bd9Sstevel@tonic-gate 	if (fg == NULL)
34037c478bd9Sstevel@tonic-gate 		return;
3404ab25eeb5Syz 
34057c478bd9Sstevel@tonic-gate 	fg->fg_ref--;
34067c478bd9Sstevel@tonic-gate 	if (fg->fg_ref == 0) {
34077c478bd9Sstevel@tonic-gate 		*fgp = fg->fg_next;
34087c478bd9Sstevel@tonic-gate 		KFREE(fg);
34097c478bd9Sstevel@tonic-gate 	}
34107c478bd9Sstevel@tonic-gate }
34117c478bd9Sstevel@tonic-gate 
34127c478bd9Sstevel@tonic-gate 
34137c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
34147c478bd9Sstevel@tonic-gate /* Function:    fr_getrulen                                                 */
34157c478bd9Sstevel@tonic-gate /* Returns:     frentry_t * - NULL == not found, else pointer to rule n     */
34167c478bd9Sstevel@tonic-gate /* Parameters:  unit(I)  - device for which to count the rule's number      */
34177c478bd9Sstevel@tonic-gate /*              flags(I) - which set of rules to find the rule in           */
34187c478bd9Sstevel@tonic-gate /*              group(I) - group name                                       */
34197c478bd9Sstevel@tonic-gate /*              n(I)     - rule number to find                              */
34207c478bd9Sstevel@tonic-gate /*                                                                          */
34217c478bd9Sstevel@tonic-gate /* Find rule # n in group # g and return a pointer to it.  Return NULl if   */
34227c478bd9Sstevel@tonic-gate /* group # g doesn't exist or there are less than n rules in the group.     */
34237c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3424f4b3ec61Sdh frentry_t *fr_getrulen(unit, group, n, ifs)
34257c478bd9Sstevel@tonic-gate int unit;
34267c478bd9Sstevel@tonic-gate char *group;
34277c478bd9Sstevel@tonic-gate u_32_t n;
3428f4b3ec61Sdh ipf_stack_t *ifs;
34297c478bd9Sstevel@tonic-gate {
34307c478bd9Sstevel@tonic-gate 	frentry_t *fr;
34317c478bd9Sstevel@tonic-gate 	frgroup_t *fg;
34327c478bd9Sstevel@tonic-gate 
3433f4b3ec61Sdh 	fg = fr_findgroup(group, unit, ifs->ifs_fr_active, NULL, ifs);
34347c478bd9Sstevel@tonic-gate 	if (fg == NULL)
34357c478bd9Sstevel@tonic-gate 		return NULL;
34367c478bd9Sstevel@tonic-gate 	for (fr = fg->fg_head; fr && n; fr = fr->fr_next, n--)
34377c478bd9Sstevel@tonic-gate 		;
34387c478bd9Sstevel@tonic-gate 	if (n != 0)
34397c478bd9Sstevel@tonic-gate 		return NULL;
34407c478bd9Sstevel@tonic-gate 	return fr;
34417c478bd9Sstevel@tonic-gate }
34427c478bd9Sstevel@tonic-gate 
34437c478bd9Sstevel@tonic-gate 
34447c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
34457c478bd9Sstevel@tonic-gate /* Function:    fr_rulen                                                    */
34467c478bd9Sstevel@tonic-gate /* Returns:     int - >= 0 - rule number, -1 == search failed               */
34477c478bd9Sstevel@tonic-gate /* Parameters:  unit(I) - device for which to count the rule's number       */
34487c478bd9Sstevel@tonic-gate /*              fr(I)   - pointer to rule to match                          */
34497c478bd9Sstevel@tonic-gate /*                                                                          */
34507c478bd9Sstevel@tonic-gate /* Return the number for a rule on a specific filtering device.             */
34517c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3452f4b3ec61Sdh int fr_rulen(unit, fr, ifs)
34537c478bd9Sstevel@tonic-gate int unit;
34547c478bd9Sstevel@tonic-gate frentry_t *fr;
3455f4b3ec61Sdh ipf_stack_t *ifs;
34567c478bd9Sstevel@tonic-gate {
34577c478bd9Sstevel@tonic-gate 	frentry_t *fh;
34587c478bd9Sstevel@tonic-gate 	frgroup_t *fg;
34597c478bd9Sstevel@tonic-gate 	u_32_t n = 0;
34607c478bd9Sstevel@tonic-gate 
34617c478bd9Sstevel@tonic-gate 	if (fr == NULL)
34627c478bd9Sstevel@tonic-gate 		return -1;
3463f4b3ec61Sdh 	fg = fr_findgroup(fr->fr_group, unit, ifs->ifs_fr_active, NULL, ifs);
34647c478bd9Sstevel@tonic-gate 	if (fg == NULL)
34657c478bd9Sstevel@tonic-gate 		return -1;
34667c478bd9Sstevel@tonic-gate 	for (fh = fg->fg_head; fh; n++, fh = fh->fr_next)
34677c478bd9Sstevel@tonic-gate 		if (fh == fr)
34687c478bd9Sstevel@tonic-gate 			break;
34697c478bd9Sstevel@tonic-gate 	if (fh == NULL)
34707c478bd9Sstevel@tonic-gate 		return -1;
34717c478bd9Sstevel@tonic-gate 	return n;
34727c478bd9Sstevel@tonic-gate }
34737c478bd9Sstevel@tonic-gate 
34747c478bd9Sstevel@tonic-gate 
34757c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
34767c478bd9Sstevel@tonic-gate /* Function:    frflushlist                                                 */
34777c478bd9Sstevel@tonic-gate /* Returns:     int - >= 0 - number of flushed rules                        */
34787c478bd9Sstevel@tonic-gate /* Parameters:  set(I)   - which set of rules (inactive/inactive) this is   */
34797c478bd9Sstevel@tonic-gate /*              unit(I)  - device for which to flush rules                  */
34807c478bd9Sstevel@tonic-gate /*              flags(I) - which set of rules to flush                      */
34817c478bd9Sstevel@tonic-gate /*              nfreedp(O) - pointer to int where flush count is stored     */
34827c478bd9Sstevel@tonic-gate /*              listp(I)   - pointer to list to flush pointer               */
34837c478bd9Sstevel@tonic-gate /* Write Locks: ipf_mutex                                                   */
34847c478bd9Sstevel@tonic-gate /*                                                                          */
34857c478bd9Sstevel@tonic-gate /* Recursively flush rules from the list, descending groups as they are     */
34867c478bd9Sstevel@tonic-gate /* encountered.  if a rule is the head of a group and it has lost all its   */
34877c478bd9Sstevel@tonic-gate /* group members, then also delete the group reference.  nfreedp is needed  */
34887c478bd9Sstevel@tonic-gate /* to store the accumulating count of rules removed, whereas the returned   */
34897c478bd9Sstevel@tonic-gate /* value is just the number removed from the current list.  The latter is   */
34907c478bd9Sstevel@tonic-gate /* needed to correctly adjust reference counts on rules that define groups. */
34917c478bd9Sstevel@tonic-gate /*                                                                          */
34927c478bd9Sstevel@tonic-gate /* NOTE: Rules not loaded from user space cannot be flushed.                */
34937c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3494f4b3ec61Sdh static int frflushlist(set, unit, nfreedp, listp, ifs)
34957c478bd9Sstevel@tonic-gate int set;
34967c478bd9Sstevel@tonic-gate minor_t unit;
34977c478bd9Sstevel@tonic-gate int *nfreedp;
34987c478bd9Sstevel@tonic-gate frentry_t **listp;
3499f4b3ec61Sdh ipf_stack_t *ifs;
35007c478bd9Sstevel@tonic-gate {
350124109627Syx 	int freed = 0;
35027c478bd9Sstevel@tonic-gate 	frentry_t *fp;
35037c478bd9Sstevel@tonic-gate 
35047c478bd9Sstevel@tonic-gate 	while ((fp = *listp) != NULL) {
35057c478bd9Sstevel@tonic-gate 		if ((fp->fr_type & FR_T_BUILTIN) ||
35067c478bd9Sstevel@tonic-gate 		    !(fp->fr_flags & FR_COPIED)) {
35077c478bd9Sstevel@tonic-gate 			listp = &fp->fr_next;
35087c478bd9Sstevel@tonic-gate 			continue;
35097c478bd9Sstevel@tonic-gate 		}
35107c478bd9Sstevel@tonic-gate 		*listp = fp->fr_next;
35117c478bd9Sstevel@tonic-gate 		if (fp->fr_grp != NULL) {
351224109627Syx 			(void) frflushlist(set, unit, nfreedp, fp->fr_grp, ifs);
35137c478bd9Sstevel@tonic-gate 		}
35147c478bd9Sstevel@tonic-gate 
35157c478bd9Sstevel@tonic-gate 		if (fp->fr_grhead != NULL) {
3516f4b3ec61Sdh 			fr_delgroup(fp->fr_grhead, unit, set, ifs);
35177c478bd9Sstevel@tonic-gate 			*fp->fr_grhead = '\0';
35187c478bd9Sstevel@tonic-gate 		}
35197c478bd9Sstevel@tonic-gate 
35207c478bd9Sstevel@tonic-gate 		ASSERT(fp->fr_ref > 0);
35217c478bd9Sstevel@tonic-gate 		fp->fr_next = NULL;
3522f4b3ec61Sdh 		if (fr_derefrule(&fp, ifs) == 0)
35237c478bd9Sstevel@tonic-gate 			freed++;
35247c478bd9Sstevel@tonic-gate 	}
35257c478bd9Sstevel@tonic-gate 	*nfreedp += freed;
35267c478bd9Sstevel@tonic-gate 	return freed;
35277c478bd9Sstevel@tonic-gate }
35287c478bd9Sstevel@tonic-gate 
35297c478bd9Sstevel@tonic-gate 
35307c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
35317c478bd9Sstevel@tonic-gate /* Function:    frflush                                                     */
35327c478bd9Sstevel@tonic-gate /* Returns:     int - >= 0 - number of flushed rules                        */
35337c478bd9Sstevel@tonic-gate /* Parameters:  unit(I)  - device for which to flush rules                  */
35347c478bd9Sstevel@tonic-gate /*              flags(I) - which set of rules to flush                      */
35357c478bd9Sstevel@tonic-gate /*                                                                          */
35367c478bd9Sstevel@tonic-gate /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
35377c478bd9Sstevel@tonic-gate /* and IPv6) as defined by the value of flags.                              */
35387c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3539f4b3ec61Sdh int frflush(unit, proto, flags, ifs)
35407c478bd9Sstevel@tonic-gate minor_t unit;
35417663b816Sml int proto, flags;
3542f4b3ec61Sdh ipf_stack_t *ifs;
35437c478bd9Sstevel@tonic-gate {
35447c478bd9Sstevel@tonic-gate 	int flushed = 0, set;
35457c478bd9Sstevel@tonic-gate 
3546f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
354714d3298eSAlexandr Nedvedicky 	bzero((char *)ifs->ifs_frcache, sizeof (ifs->ifs_frcache));
35487c478bd9Sstevel@tonic-gate 
3549f4b3ec61Sdh 	set = ifs->ifs_fr_active;
35507c478bd9Sstevel@tonic-gate 	if ((flags & FR_INACTIVE) == FR_INACTIVE)
35517c478bd9Sstevel@tonic-gate 		set = 1 - set;
35527c478bd9Sstevel@tonic-gate 
35537c478bd9Sstevel@tonic-gate 	if (flags & FR_OUTQUE) {
35547663b816Sml 		if (proto == 0 || proto == 6) {
35557663b816Sml 			(void) frflushlist(set, unit,
3556f4b3ec61Sdh 			    &flushed, &ifs->ifs_ipfilter6[1][set], ifs);
35577663b816Sml 			(void) frflushlist(set, unit,
3558f4b3ec61Sdh 			    &flushed, &ifs->ifs_ipacct6[1][set], ifs);
35597663b816Sml 		}
35607663b816Sml 		if (proto == 0 || proto == 4) {
35617663b816Sml 			(void) frflushlist(set, unit,
3562f4b3ec61Sdh 			    &flushed, &ifs->ifs_ipfilter[1][set], ifs);
35637663b816Sml 			(void) frflushlist(set, unit,
3564f4b3ec61Sdh 			    &flushed, &ifs->ifs_ipacct[1][set], ifs);
35657663b816Sml 		}
35667c478bd9Sstevel@tonic-gate 	}
35677c478bd9Sstevel@tonic-gate 	if (flags & FR_INQUE) {
35687663b816Sml 		if (proto == 0 || proto == 6) {
35697663b816Sml 			(void) frflushlist(set, unit,
3570f4b3ec61Sdh 			    &flushed, &ifs->ifs_ipfilter6[0][set], ifs);
35717663b816Sml 			(void) frflushlist(set, unit,
3572f4b3ec61Sdh 			    &flushed, &ifs->ifs_ipacct6[0][set], ifs);
35737663b816Sml 		}
35747663b816Sml 		if (proto == 0 || proto == 4) {
35757663b816Sml 			(void) frflushlist(set, unit,
3576f4b3ec61Sdh 			    &flushed, &ifs->ifs_ipfilter[0][set], ifs);
35777663b816Sml 			(void) frflushlist(set, unit,
3578f4b3ec61Sdh 			    &flushed, &ifs->ifs_ipacct[0][set], ifs);
35797663b816Sml 		}
35807c478bd9Sstevel@tonic-gate 	}
3581f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
35827663b816Sml 
35837663b816Sml 	if (unit == IPL_LOGIPF) {
35847663b816Sml 		int tmp;
35857663b816Sml 
3586f4b3ec61Sdh 		tmp = frflush(IPL_LOGCOUNT, proto, flags, ifs);
35877663b816Sml 		if (tmp >= 0)
35887663b816Sml 			flushed += tmp;
35897663b816Sml 	}
35907c478bd9Sstevel@tonic-gate 	return flushed;
35917c478bd9Sstevel@tonic-gate }
35927c478bd9Sstevel@tonic-gate 
35937c478bd9Sstevel@tonic-gate 
35947c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
35957c478bd9Sstevel@tonic-gate /* Function:    memstr                                                      */
35967c478bd9Sstevel@tonic-gate /* Returns:     char *  - NULL if failed, != NULL pointer to matching bytes */
35977c478bd9Sstevel@tonic-gate /* Parameters:  src(I)  - pointer to byte sequence to match                 */
35987c478bd9Sstevel@tonic-gate /*              dst(I)  - pointer to byte sequence to search                */
35997c478bd9Sstevel@tonic-gate /*              slen(I) - match length                                      */
36007c478bd9Sstevel@tonic-gate /*              dlen(I) - length available to search in                     */
36017c478bd9Sstevel@tonic-gate /*                                                                          */
36027c478bd9Sstevel@tonic-gate /* Search dst for a sequence of bytes matching those at src and extend for  */
36037c478bd9Sstevel@tonic-gate /* slen bytes.                                                              */
36047c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
36057c478bd9Sstevel@tonic-gate char *memstr(src, dst, slen, dlen)
36067c478bd9Sstevel@tonic-gate char *src, *dst;
36077c478bd9Sstevel@tonic-gate int slen, dlen;
36087c478bd9Sstevel@tonic-gate {
36097c478bd9Sstevel@tonic-gate 	char *s = NULL;
36107c478bd9Sstevel@tonic-gate 
36117c478bd9Sstevel@tonic-gate 	while (dlen >= slen) {
36127c478bd9Sstevel@tonic-gate 		if (bcmp(src, dst, slen) == 0) {
36137c478bd9Sstevel@tonic-gate 			s = dst;
36147c478bd9Sstevel@tonic-gate 			break;
36157c478bd9Sstevel@tonic-gate 		}
36167c478bd9Sstevel@tonic-gate 		dst++;
36177c478bd9Sstevel@tonic-gate 		dlen--;
36187c478bd9Sstevel@tonic-gate 	}
36197c478bd9Sstevel@tonic-gate 	return s;
36207c478bd9Sstevel@tonic-gate }
36217c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
36227c478bd9Sstevel@tonic-gate /* Function:    fr_fixskip                                                  */
36237c478bd9Sstevel@tonic-gate /* Returns:     Nil                                                         */
36247c478bd9Sstevel@tonic-gate /* Parameters:  listp(IO)    - pointer to start of list with skip rule      */
36257c478bd9Sstevel@tonic-gate /*              rp(I)        - rule added/removed with skip in it.          */
36267c478bd9Sstevel@tonic-gate /*              addremove(I) - adjustment (-1/+1) to make to skip count,    */
36277c478bd9Sstevel@tonic-gate /*                             depending on whether a rule was just added   */
36287c478bd9Sstevel@tonic-gate /*                             or removed.                                  */
36297c478bd9Sstevel@tonic-gate /*                                                                          */
36307c478bd9Sstevel@tonic-gate /* Adjust all the rules in a list which would have skip'd past the position */
36317c478bd9Sstevel@tonic-gate /* where we are inserting to skip to the right place given the change.      */
36327c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
36337c478bd9Sstevel@tonic-gate void fr_fixskip(listp, rp, addremove)
36347c478bd9Sstevel@tonic-gate frentry_t **listp, *rp;
36357c478bd9Sstevel@tonic-gate int addremove;
36367c478bd9Sstevel@tonic-gate {
36377c478bd9Sstevel@tonic-gate 	int rules, rn;
36387c478bd9Sstevel@tonic-gate 	frentry_t *fp;
36397c478bd9Sstevel@tonic-gate 
36407c478bd9Sstevel@tonic-gate 	rules = 0;
36417c478bd9Sstevel@tonic-gate 	for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next)
36427c478bd9Sstevel@tonic-gate 		rules++;
36437c478bd9Sstevel@tonic-gate 
36447c478bd9Sstevel@tonic-gate 	if (!fp)
36457c478bd9Sstevel@tonic-gate 		return;
36467c478bd9Sstevel@tonic-gate 
36477c478bd9Sstevel@tonic-gate 	for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
36487c478bd9Sstevel@tonic-gate 		if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules))
36497c478bd9Sstevel@tonic-gate 			fp->fr_arg += addremove;
36507c478bd9Sstevel@tonic-gate }
36517c478bd9Sstevel@tonic-gate 
36527c478bd9Sstevel@tonic-gate 
36537c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
36547c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
36557c478bd9Sstevel@tonic-gate /* Function:    count4bits                                                  */
36567c478bd9Sstevel@tonic-gate /* Returns:     int - >= 0 - number of consecutive bits in input            */
36577c478bd9Sstevel@tonic-gate /* Parameters:  ip(I) - 32bit IP address                                    */
36587c478bd9Sstevel@tonic-gate /*                                                                          */
36597c478bd9Sstevel@tonic-gate /* IPv4 ONLY                                                                */
36607c478bd9Sstevel@tonic-gate /* count consecutive 1's in bit mask.  If the mask generated by counting    */
36617c478bd9Sstevel@tonic-gate /* consecutive 1's is different to that passed, return -1, else return #    */
36627c478bd9Sstevel@tonic-gate /* of bits.                                                                 */
36637c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
36647c478bd9Sstevel@tonic-gate int	count4bits(ip)
36657c478bd9Sstevel@tonic-gate u_32_t	ip;
36667c478bd9Sstevel@tonic-gate {
36677c478bd9Sstevel@tonic-gate 	u_32_t	ipn;
36687c478bd9Sstevel@tonic-gate 	int	cnt = 0, i, j;
36697c478bd9Sstevel@tonic-gate 
36707c478bd9Sstevel@tonic-gate 	ip = ipn = ntohl(ip);
36717c478bd9Sstevel@tonic-gate 	for (i = 32; i; i--, ipn *= 2)
36727c478bd9Sstevel@tonic-gate 		if (ipn & 0x80000000)
36737c478bd9Sstevel@tonic-gate 			cnt++;
36747c478bd9Sstevel@tonic-gate 		else
36757c478bd9Sstevel@tonic-gate 			break;
36767c478bd9Sstevel@tonic-gate 	ipn = 0;
36777c478bd9Sstevel@tonic-gate 	for (i = 32, j = cnt; i; i--, j--) {
36787c478bd9Sstevel@tonic-gate 		ipn *= 2;
36797c478bd9Sstevel@tonic-gate 		if (j > 0)
36807c478bd9Sstevel@tonic-gate 			ipn++;
36817c478bd9Sstevel@tonic-gate 	}
36827c478bd9Sstevel@tonic-gate 	if (ipn == ip)
36837c478bd9Sstevel@tonic-gate 		return cnt;
36847c478bd9Sstevel@tonic-gate 	return -1;
36857c478bd9Sstevel@tonic-gate }
36867c478bd9Sstevel@tonic-gate 
3687ab25eeb5Syz 
36887663b816Sml #ifdef USE_INET6
36897c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
36907c478bd9Sstevel@tonic-gate /* Function:    count6bits                                                  */
36917c478bd9Sstevel@tonic-gate /* Returns:     int - >= 0 - number of consecutive bits in input            */
36927c478bd9Sstevel@tonic-gate /* Parameters:  msk(I) - pointer to start of IPv6 bitmask                   */
36937c478bd9Sstevel@tonic-gate /*                                                                          */
36947c478bd9Sstevel@tonic-gate /* IPv6 ONLY                                                                */
36957c478bd9Sstevel@tonic-gate /* count consecutive 1's in bit mask.                                       */
36967c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
36977c478bd9Sstevel@tonic-gate int count6bits(msk)
36987c478bd9Sstevel@tonic-gate u_32_t *msk;
36997c478bd9Sstevel@tonic-gate {
37007c478bd9Sstevel@tonic-gate 	int i = 0, k;
37017c478bd9Sstevel@tonic-gate 	u_32_t j;
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate 	for (k = 3; k >= 0; k--)
37047c478bd9Sstevel@tonic-gate 		if (msk[k] == 0xffffffff)
37057c478bd9Sstevel@tonic-gate 			i += 32;
37067c478bd9Sstevel@tonic-gate 		else {
37077c478bd9Sstevel@tonic-gate 			for (j = msk[k]; j; j <<= 1)
37087c478bd9Sstevel@tonic-gate 				if (j & 0x80000000)
37097c478bd9Sstevel@tonic-gate 					i++;
37107c478bd9Sstevel@tonic-gate 		}
37117c478bd9Sstevel@tonic-gate 	return i;
37127c478bd9Sstevel@tonic-gate }
3713ab25eeb5Syz # endif
37147c478bd9Sstevel@tonic-gate #endif /* _KERNEL */
37157c478bd9Sstevel@tonic-gate 
37167c478bd9Sstevel@tonic-gate 
3717381a2a9aSdr /* ------------------------------------------------------------------------ */
3718381a2a9aSdr /* Function:    fr_ifsync                                                   */
3719381a2a9aSdr /* Returns:     void *    - new interface identifier                        */
3720381a2a9aSdr /* Parameters:  action(I)  - type of synchronisation to do                  */
3721381a2a9aSdr /*              v(I)       - IP version being sync'd (v4 or v6)             */
3722381a2a9aSdr /*              newifp(I)  - interface identifier being introduced/removed  */
3723381a2a9aSdr /*              oldifp(I)  - interface identifier in a filter rule          */
372440cdc2e8SAlexandr Nedvedicky /*              newname(I) - name associated with newifp interface          */
372540cdc2e8SAlexandr Nedvedicky /*              oldname(I) - name associated with oldifp interface          */
372640cdc2e8SAlexandr Nedvedicky /*		ifs       - pointer to IPF stack instance		    */
3727381a2a9aSdr /*                                                                          */
3728381a2a9aSdr /* This function returns what the new value for "oldifp" should be for its  */
3729381a2a9aSdr /* caller.  In some cases it will not change, in some it will.              */
3730381a2a9aSdr /* action == IPFSYNC_RESYNC                                                 */
3731381a2a9aSdr /*   a new value for oldifp will always be looked up, according to oldname, */
3732381a2a9aSdr /*   the values of newname and newifp are ignored.                          */
3733381a2a9aSdr /* action == IPFSYNC_NEWIFP                                                 */
3734381a2a9aSdr /*   if oldname matches newname then we are doing a sync for the matching   */
3735381a2a9aSdr /*   interface, so we return newifp to be used in place of oldifp.  If the  */
3736381a2a9aSdr /*   the names don't match, just return oldifp.                             */
3737381a2a9aSdr /* action == IPFSYNC_OLDIFP                                                 */
3738381a2a9aSdr /*   if oldifp matches newifp then we are are doing a sync to remove any    */
3739381a2a9aSdr /*   references to oldifp, so we return "-1".                               */
374040cdc2e8SAlexandr Nedvedicky /* -----								    */
374140cdc2e8SAlexandr Nedvedicky /* NOTE:								    */
374240cdc2e8SAlexandr Nedvedicky /* This function processes NIC event from PF_HOOKS. The action parameter    */
374340cdc2e8SAlexandr Nedvedicky /* is set in ipf_nic_event_v4()/ipf_nic_event_v6() function. There is	    */
374440cdc2e8SAlexandr Nedvedicky /* one single switch statement() in ipf_nic_event_vx() function, which	    */
374540cdc2e8SAlexandr Nedvedicky /* translates the HOOK event type to action parameter passed to fr_ifsync.  */
374640cdc2e8SAlexandr Nedvedicky /* The translation table looks as follows:				    */
374740cdc2e8SAlexandr Nedvedicky /*	event		| action					    */
374840cdc2e8SAlexandr Nedvedicky /*	----------------+-------------					    */
374940cdc2e8SAlexandr Nedvedicky /*	NE_PLUMB	| IPFSYNC_NEWIFP				    */
375040cdc2e8SAlexandr Nedvedicky /*	NE_UNPLUMB	| IPFSYNC_OLDIFP				    */
375140cdc2e8SAlexandr Nedvedicky /*    NE_ADDRESS_CHANGE	| IPFSYNC_RESYNC				    */
375240cdc2e8SAlexandr Nedvedicky /*									    */
375340cdc2e8SAlexandr Nedvedicky /* The oldname and oldifp parameters are taken from IPF entry (rule, state  */
375440cdc2e8SAlexandr Nedvedicky /* table entry, NAT table entry, fragment ...). The newname and newifp	    */
375540cdc2e8SAlexandr Nedvedicky /* parameters come from hook event data, parameters are taken from event    */
375640cdc2e8SAlexandr Nedvedicky /* in ipf_nic_event_vx() functions. Any time NIC changes, the IPF is	    */
375740cdc2e8SAlexandr Nedvedicky /* notified by hook function.						    */
375840cdc2e8SAlexandr Nedvedicky /*									    */
375940cdc2e8SAlexandr Nedvedicky /* We get NE_UNPLUMB event from PF_HOOKS even if someone coincidently tries */
376040cdc2e8SAlexandr Nedvedicky /* to plumb the interface, which is already plumbed. In such case we always */
376140cdc2e8SAlexandr Nedvedicky /* get the event from PF_HOOKS as follows:				    */
376240cdc2e8SAlexandr Nedvedicky /*	event:	NE_PLUMB						    */
376340cdc2e8SAlexandr Nedvedicky /*	NIC:	0x0							    */
3764381a2a9aSdr /* ------------------------------------------------------------------------ */
3765f4b3ec61Sdh static void *fr_ifsync(action, v, newname, oldname, newifp, oldifp, ifs)
3766381a2a9aSdr int action, v;
3767381a2a9aSdr char *newname, *oldname;
3768381a2a9aSdr void *newifp, *oldifp;
3769f4b3ec61Sdh ipf_stack_t *ifs;
3770381a2a9aSdr {
3771381a2a9aSdr 	void *rval = oldifp;
3772381a2a9aSdr 
3773381a2a9aSdr 	switch (action)
3774381a2a9aSdr 	{
3775381a2a9aSdr 	case IPFSYNC_RESYNC :
3776381a2a9aSdr 		if (oldname[0] != '\0') {
3777f4b3ec61Sdh 			rval = fr_resolvenic(oldname, v, ifs);
3778381a2a9aSdr 		}
3779381a2a9aSdr 		break;
3780381a2a9aSdr 	case IPFSYNC_NEWIFP :
3781381a2a9aSdr 		if (!strncmp(newname, oldname, LIFNAMSIZ))
3782381a2a9aSdr 			rval = newifp;
3783381a2a9aSdr 		break;
3784381a2a9aSdr 	case IPFSYNC_OLDIFP :
378540cdc2e8SAlexandr Nedvedicky 		/*
378640cdc2e8SAlexandr Nedvedicky 		 * If interface gets unplumbed it must be invalidated, which
378740cdc2e8SAlexandr Nedvedicky 		 * means set all existing references to the interface to -1.
378840cdc2e8SAlexandr Nedvedicky 		 * We don't want to invalidate references for wildcard
378940cdc2e8SAlexandr Nedvedicky 		 * (unbound) rules (entries).
379040cdc2e8SAlexandr Nedvedicky 		 */
3791381a2a9aSdr 		if (newifp == oldifp)
379224ed9a9fSan 			rval = (oldifp) ? (void *)-1 : NULL;
3793381a2a9aSdr 		break;
3794381a2a9aSdr 	}
3795381a2a9aSdr 
3796381a2a9aSdr 	return rval;
3797381a2a9aSdr }
3798381a2a9aSdr 
3799381a2a9aSdr 
38007c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
38017c478bd9Sstevel@tonic-gate /* Function:    frsynclist                                                  */
38027c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
3803381a2a9aSdr /* Parameters:  action(I) - type of synchronisation to do                   */
3804381a2a9aSdr /*              v(I)      - IP version being sync'd (v4 or v6)              */
3805381a2a9aSdr /*              ifp(I)    - interface identifier associated with action     */
380640cdc2e8SAlexandr Nedvedicky /*              ifname(I) - name associated with ifp parameter              */
380740cdc2e8SAlexandr Nedvedicky /*              fr(I)     - pointer to filter rule                          */
380840cdc2e8SAlexandr Nedvedicky /*		ifs       - pointer to IPF stack instance		    */
38097c478bd9Sstevel@tonic-gate /* Write Locks: ipf_mutex                                                   */
38107c478bd9Sstevel@tonic-gate /*                                                                          */
38117c478bd9Sstevel@tonic-gate /* Walk through a list of filter rules and resolve any interface names into */
38127c478bd9Sstevel@tonic-gate /* pointers.  Where dynamic addresses are used, also update the IP address  */
3813ab25eeb5Syz /* used in the rule.  The interface pointer is used to limit the lookups to */
3814ab25eeb5Syz /* a specific set of matching names if it is non-NULL.                      */
38157c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
3816f4b3ec61Sdh static void frsynclist(action, v, ifp, ifname, fr, ifs)
3817381a2a9aSdr int action, v;
3818ab25eeb5Syz void *ifp;
3819381a2a9aSdr char *ifname;
3820381a2a9aSdr frentry_t *fr;
3821f4b3ec61Sdh ipf_stack_t *ifs;
38227c478bd9Sstevel@tonic-gate {
38237c478bd9Sstevel@tonic-gate 	frdest_t *fdp;
3824381a2a9aSdr 	int rv, i;
38257c478bd9Sstevel@tonic-gate 
38267c478bd9Sstevel@tonic-gate 	for (; fr; fr = fr->fr_next) {
3827381a2a9aSdr 		rv = fr->fr_v;
3828381a2a9aSdr 		if (v != 0 && v != rv)
3829381a2a9aSdr 			continue;
38307c478bd9Sstevel@tonic-gate 
38317c478bd9Sstevel@tonic-gate 		/*
38327c478bd9Sstevel@tonic-gate 		 * Lookup all the interface names that are part of the rule.
38337c478bd9Sstevel@tonic-gate 		 */
38347c478bd9Sstevel@tonic-gate 		for (i = 0; i < 4; i++) {
3835381a2a9aSdr 			fr->fr_ifas[i] = fr_ifsync(action, rv, ifname,
3836381a2a9aSdr 						   fr->fr_ifnames[i],
3837f4b3ec61Sdh 						   ifp, fr->fr_ifas[i],
3838f4b3ec61Sdh 						   ifs);
38397c478bd9Sstevel@tonic-gate 		}
38407c478bd9Sstevel@tonic-gate 
3841381a2a9aSdr 		fdp = &fr->fr_tifs[0];
3842381a2a9aSdr 		fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname,
3843f4b3ec61Sdh 					   ifp, fdp->fd_ifp, ifs);
3844381a2a9aSdr 
3845381a2a9aSdr 		fdp = &fr->fr_tifs[1];
3846381a2a9aSdr 		fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname,
3847f4b3ec61Sdh 					   ifp, fdp->fd_ifp, ifs);
3848381a2a9aSdr 
3849381a2a9aSdr 		fdp = &fr->fr_dif;
3850381a2a9aSdr 		fdp->fd_ifp = fr_ifsync(action, rv, ifname, fdp->fd_ifname,
3851f4b3ec61Sdh 					   ifp, fdp->fd_ifp, ifs);
3852381a2a9aSdr 
3853381a2a9aSdr 		if (action != IPFSYNC_RESYNC)
3854a4cf92b0Sdr 			continue;
3855381a2a9aSdr 
38567c478bd9Sstevel@tonic-gate 		if (fr->fr_type == FR_T_IPF) {
38577c478bd9Sstevel@tonic-gate 			if (fr->fr_satype != FRI_NORMAL &&
38587c478bd9Sstevel@tonic-gate 			    fr->fr_satype != FRI_LOOKUP) {
3859381a2a9aSdr 				(void)fr_ifpaddr(rv, fr->fr_satype,
38607c478bd9Sstevel@tonic-gate 						 fr->fr_ifas[fr->fr_sifpidx],
3861f4b3ec61Sdh 						 &fr->fr_src, &fr->fr_smsk,
3862f4b3ec61Sdh 						 ifs);
38637c478bd9Sstevel@tonic-gate 			}
38647c478bd9Sstevel@tonic-gate 			if (fr->fr_datype != FRI_NORMAL &&
38657c478bd9Sstevel@tonic-gate 			    fr->fr_datype != FRI_LOOKUP) {
3866381a2a9aSdr 				(void)fr_ifpaddr(rv, fr->fr_datype,
38677c478bd9Sstevel@tonic-gate 						 fr->fr_ifas[fr->fr_difpidx],
3868f4b3ec61Sdh 						 &fr->fr_dst, &fr->fr_dmsk,
3869f4b3ec61Sdh 						 ifs);
38707c478bd9Sstevel@tonic-gate 			}
38717c478bd9Sstevel@tonic-gate 		}
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
38747c478bd9Sstevel@tonic-gate 		if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP &&
38757c478bd9Sstevel@tonic-gate 		    fr->fr_srcptr == NULL) {
38767c478bd9Sstevel@tonic-gate 			fr->fr_srcptr = fr_resolvelookup(fr->fr_srctype,
38777c478bd9Sstevel@tonic-gate 							 fr->fr_srcnum,
3878f4b3ec61Sdh 							 &fr->fr_srcfunc, ifs);
38797c478bd9Sstevel@tonic-gate 		}
38807c478bd9Sstevel@tonic-gate 		if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP &&
38817c478bd9Sstevel@tonic-gate 		    fr->fr_dstptr == NULL) {
38827c478bd9Sstevel@tonic-gate 			fr->fr_dstptr = fr_resolvelookup(fr->fr_dsttype,
38837c478bd9Sstevel@tonic-gate 							 fr->fr_dstnum,
3884f4b3ec61Sdh 							 &fr->fr_dstfunc, ifs);
38857c478bd9Sstevel@tonic-gate 		}
38867c478bd9Sstevel@tonic-gate #endif
38877c478bd9Sstevel@tonic-gate 	}
38887c478bd9Sstevel@tonic-gate }
38897c478bd9Sstevel@tonic-gate 
38907c478bd9Sstevel@tonic-gate 
38917c478bd9Sstevel@tonic-gate #ifdef	_KERNEL
38927c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
38937c478bd9Sstevel@tonic-gate /* Function:    frsync                                                      */
38947c478bd9Sstevel@tonic-gate /* Returns:     void                                                        */
3895381a2a9aSdr /* Parameters:  action(I) - type of synchronisation to do                   */
3896381a2a9aSdr /*              v(I)      - IP version being sync'd (v4 or v6)              */
3897381a2a9aSdr /*              ifp(I)    - interface identifier associated with action     */
3898381a2a9aSdr /*              name(I)   - name associated with ifp parameter              */
38997c478bd9Sstevel@tonic-gate /*                                                                          */
39007c478bd9Sstevel@tonic-gate /* frsync() is called when we suspect that the interface list or            */
39017c478bd9Sstevel@tonic-gate /* information about interfaces (like IP#) has changed.  Go through all     */
39027c478bd9Sstevel@tonic-gate /* filter rules, NAT entries and the state table and check if anything      */
39037c478bd9Sstevel@tonic-gate /* needs to be changed/updated.                                             */
3904381a2a9aSdr /* With the filtering hooks added to Solaris, we needed to change the manner*/
3905381a2a9aSdr /* in which this was done to support three different types of sync:         */
3906381a2a9aSdr /* - complete resync of all interface name/identifiers                      */
3907381a2a9aSdr /* - new interface being announced with its name and identifier             */
3908381a2a9aSdr /* - interface removal being announced by only its identifier               */
3909381a2a9aSdr /* ------------------------------------------------------------------------ */
3910f4b3ec61Sdh void frsync(action, v, ifp, name, ifs)
3911381a2a9aSdr int action, v;
3912ab25eeb5Syz void *ifp;
3913381a2a9aSdr char *name;
3914f4b3ec61Sdh ipf_stack_t *ifs;
39157c478bd9Sstevel@tonic-gate {
39167c478bd9Sstevel@tonic-gate 	int i;
3917ab25eeb5Syz 
3918f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
3919f4b3ec61Sdh 	frsynclist(action, v, ifp, name, ifs->ifs_ipacct[0][ifs->ifs_fr_active], ifs);
3920f4b3ec61Sdh 	frsynclist(action, v, ifp, name, ifs->ifs_ipacct[1][ifs->ifs_fr_active], ifs);
3921f4b3ec61Sdh 	frsynclist(action, v, ifp, name, ifs->ifs_ipfilter[0][ifs->ifs_fr_active], ifs);
3922f4b3ec61Sdh 	frsynclist(action, v, ifp, name, ifs->ifs_ipfilter[1][ifs->ifs_fr_active], ifs);
3923f4b3ec61Sdh 	frsynclist(action, v, ifp, name, ifs->ifs_ipacct6[0][ifs->ifs_fr_active], ifs);
3924f4b3ec61Sdh 	frsynclist(action, v, ifp, name, ifs->ifs_ipacct6[1][ifs->ifs_fr_active], ifs);
3925f4b3ec61Sdh 	frsynclist(action, v, ifp, name, ifs->ifs_ipfilter6[0][ifs->ifs_fr_active], ifs);
3926f4b3ec61Sdh 	frsynclist(action, v, ifp, name, ifs->ifs_ipfilter6[1][ifs->ifs_fr_active], ifs);
39277c478bd9Sstevel@tonic-gate 
39287c478bd9Sstevel@tonic-gate 	for (i = 0; i < IPL_LOGSIZE; i++) {
39297c478bd9Sstevel@tonic-gate 		frgroup_t *g;
39307c478bd9Sstevel@tonic-gate 
3931f4b3ec61Sdh 		for (g = ifs->ifs_ipfgroups[i][0]; g != NULL; g = g->fg_next)
3932f4b3ec61Sdh 			frsynclist(action, v, ifp, name, g->fg_start, ifs);
3933f4b3ec61Sdh 		for (g = ifs->ifs_ipfgroups[i][1]; g != NULL; g = g->fg_next)
3934f4b3ec61Sdh 			frsynclist(action, v, ifp, name, g->fg_start, ifs);
39357c478bd9Sstevel@tonic-gate 	}
3936f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
39377c478bd9Sstevel@tonic-gate }
39387c478bd9Sstevel@tonic-gate 
3939e8d569f4SAlexandr Nedvedicky #if SOLARIS2 >= 10
3940e8d569f4SAlexandr Nedvedicky /* ------------------------------------------------------------------------ */
3941e8d569f4SAlexandr Nedvedicky /* Function:    fr_syncindex						    */
3942e8d569f4SAlexandr Nedvedicky /* Returns:     void							    */
3943e8d569f4SAlexandr Nedvedicky /* Parameters:  rules	  - list of rules to be sync'd			    */
3944e8d569f4SAlexandr Nedvedicky /*		ifp	  - interface, which is being sync'd		    */
3945e8d569f4SAlexandr Nedvedicky /*		newifp	  - new ifindex value for interface		    */
3946e8d569f4SAlexandr Nedvedicky /*                                                                          */
3947e8d569f4SAlexandr Nedvedicky /* Function updates all NIC indecis, which match ifp, in every rule. Every  */
3948e8d569f4SAlexandr Nedvedicky /* NIC index matching ifp, will be updated to newifp.			    */
3949e8d569f4SAlexandr Nedvedicky /* ------------------------------------------------------------------------ */
3950e8d569f4SAlexandr Nedvedicky static void fr_syncindex(rules, ifp, newifp)
3951e8d569f4SAlexandr Nedvedicky frentry_t *rules;
3952e8d569f4SAlexandr Nedvedicky void *ifp;
3953e8d569f4SAlexandr Nedvedicky void *newifp;
3954e8d569f4SAlexandr Nedvedicky {
3955e8d569f4SAlexandr Nedvedicky 	int i;
3956e8d569f4SAlexandr Nedvedicky 	frentry_t *fr;
3957e8d569f4SAlexandr Nedvedicky 
3958e8d569f4SAlexandr Nedvedicky 	for (fr = rules; fr != NULL; fr = fr->fr_next) {
3959e8d569f4SAlexandr Nedvedicky 		/*
3960e8d569f4SAlexandr Nedvedicky 		 * Lookup all the interface names that are part of the rule.
3961e8d569f4SAlexandr Nedvedicky 		 */
3962e8d569f4SAlexandr Nedvedicky 		for (i = 0; i < 4; i++)
3963e8d569f4SAlexandr Nedvedicky 			if (fr->fr_ifas[i] == ifp)
3964e8d569f4SAlexandr Nedvedicky 				fr->fr_ifas[i] = newifp;
3965e8d569f4SAlexandr Nedvedicky 
3966e8d569f4SAlexandr Nedvedicky 		for (i = 0; i < 2; i++) {
3967e8d569f4SAlexandr Nedvedicky 			if (fr->fr_tifs[i].fd_ifp == ifp)
3968e8d569f4SAlexandr Nedvedicky 				fr->fr_tifs[i].fd_ifp = newifp;
3969e8d569f4SAlexandr Nedvedicky 		}
3970e8d569f4SAlexandr Nedvedicky 
3971e8d569f4SAlexandr Nedvedicky 		if (fr->fr_dif.fd_ifp == ifp)
3972e8d569f4SAlexandr Nedvedicky 			fr->fr_dif.fd_ifp = newifp;
3973e8d569f4SAlexandr Nedvedicky 	}
3974e8d569f4SAlexandr Nedvedicky }
3975e8d569f4SAlexandr Nedvedicky 
3976e8d569f4SAlexandr Nedvedicky /* ------------------------------------------------------------------------ */
3977e8d569f4SAlexandr Nedvedicky /* Function:    fr_ifindexsync						    */
3978e8d569f4SAlexandr Nedvedicky /* Returns:     void							    */
3979e8d569f4SAlexandr Nedvedicky /* Parameters:	ifp	  - interface, which is being sync'd		    */
3980e8d569f4SAlexandr Nedvedicky /*		newifp	  - new ifindex value for interface		    */
3981e8d569f4SAlexandr Nedvedicky /*              ifs	  - IPF's stack					    */
3982e8d569f4SAlexandr Nedvedicky /*                                                                          */
3983e8d569f4SAlexandr Nedvedicky /* Function assumes ipf_mutex is locked exclusively.			    */
3984e8d569f4SAlexandr Nedvedicky /* 									    */
3985e8d569f4SAlexandr Nedvedicky /* Function updates the NIC references in rules with new interfaces index   */
3986e8d569f4SAlexandr Nedvedicky /* (newifp). Function must process active lists:			    */
3987e8d569f4SAlexandr Nedvedicky /*	with accounting rules (IPv6 and IPv4)				    */
3988e8d569f4SAlexandr Nedvedicky /*	with inbound rules (IPv6 and IPv4)				    */
3989e8d569f4SAlexandr Nedvedicky /*	with outbound rules (IPv6 and IPv4)				    */
3990e8d569f4SAlexandr Nedvedicky /* Function also has to take care of rule groups.			    */
3991e8d569f4SAlexandr Nedvedicky /*                                                                          */
3992e8d569f4SAlexandr Nedvedicky /* NOTE: The ipf_mutex is grabbed exclusively by caller (which is always    */
3993e8d569f4SAlexandr Nedvedicky /* nic_event_hook). The hook function also updates state entries, NAT rules */
3994e8d569f4SAlexandr Nedvedicky /* and NAT entries. We want to do all these update atomically to keep the   */
3995e8d569f4SAlexandr Nedvedicky /* NIC references consistent. The ipf_mutex will synchronize event with	    */
3996e8d569f4SAlexandr Nedvedicky /* fr_check(), which processes packets,	so no packet will enter fr_check(), */
3997e8d569f4SAlexandr Nedvedicky /* while NIC references will be synchronized.				    */
3998e8d569f4SAlexandr Nedvedicky /* ------------------------------------------------------------------------ */
3999e8d569f4SAlexandr Nedvedicky void fr_ifindexsync(ifp, newifp, ifs)
4000e8d569f4SAlexandr Nedvedicky void *ifp;
4001e8d569f4SAlexandr Nedvedicky void *newifp;
4002e8d569f4SAlexandr Nedvedicky ipf_stack_t *ifs;
4003e8d569f4SAlexandr Nedvedicky {
4004e8d569f4SAlexandr Nedvedicky 	unsigned int	i;
4005e8d569f4SAlexandr Nedvedicky 	frentry_t *rule_lists[8];
4006e8d569f4SAlexandr Nedvedicky 	unsigned int	rules = sizeof (rule_lists) / sizeof (frentry_t *);
4007e8d569f4SAlexandr Nedvedicky 
4008e8d569f4SAlexandr Nedvedicky 	rule_lists[0] = ifs->ifs_ipacct[0][ifs->ifs_fr_active];
4009e8d569f4SAlexandr Nedvedicky 	rule_lists[1] =	ifs->ifs_ipacct[1][ifs->ifs_fr_active];
4010e8d569f4SAlexandr Nedvedicky 	rule_lists[2] =	ifs->ifs_ipfilter[0][ifs->ifs_fr_active];
4011e8d569f4SAlexandr Nedvedicky 	rule_lists[3] =	ifs->ifs_ipfilter[1][ifs->ifs_fr_active];
4012e8d569f4SAlexandr Nedvedicky 	rule_lists[4] =	ifs->ifs_ipacct6[0][ifs->ifs_fr_active];
4013e8d569f4SAlexandr Nedvedicky 	rule_lists[5] =	ifs->ifs_ipacct6[1][ifs->ifs_fr_active];
4014e8d569f4SAlexandr Nedvedicky 	rule_lists[6] =	ifs->ifs_ipfilter6[0][ifs->ifs_fr_active];
4015e8d569f4SAlexandr Nedvedicky 	rule_lists[7] =	ifs->ifs_ipfilter6[1][ifs->ifs_fr_active];
4016e8d569f4SAlexandr Nedvedicky 
4017e8d569f4SAlexandr Nedvedicky 	for (i = 0; i < rules; i++) {
4018e8d569f4SAlexandr Nedvedicky 		fr_syncindex(rule_lists[i], ifp, newifp);
4019e8d569f4SAlexandr Nedvedicky 	}
4020e8d569f4SAlexandr Nedvedicky 
4021e8d569f4SAlexandr Nedvedicky 	/*
4022e8d569f4SAlexandr Nedvedicky 	 * Update rule groups.
4023e8d569f4SAlexandr Nedvedicky 	 */
4024e8d569f4SAlexandr Nedvedicky 	for (i = 0; i < IPL_LOGSIZE; i++) {
4025e8d569f4SAlexandr Nedvedicky 		frgroup_t *g;
4026e8d569f4SAlexandr Nedvedicky 
4027e8d569f4SAlexandr Nedvedicky 		for (g = ifs->ifs_ipfgroups[i][0]; g != NULL; g = g->fg_next)
4028e8d569f4SAlexandr Nedvedicky 			fr_syncindex(g->fg_start, ifp, newifp);
4029e8d569f4SAlexandr Nedvedicky 		for (g = ifs->ifs_ipfgroups[i][1]; g != NULL; g = g->fg_next)
4030e8d569f4SAlexandr Nedvedicky 			fr_syncindex(g->fg_start, ifp, newifp);
4031e8d569f4SAlexandr Nedvedicky 	}
4032e8d569f4SAlexandr Nedvedicky }
4033e8d569f4SAlexandr Nedvedicky #endif
40347c478bd9Sstevel@tonic-gate 
40357c478bd9Sstevel@tonic-gate /*
40367c478bd9Sstevel@tonic-gate  * In the functions below, bcopy() is called because the pointer being
40377c478bd9Sstevel@tonic-gate  * copied _from_ in this instance is a pointer to a char buf (which could
40387c478bd9Sstevel@tonic-gate  * end up being unaligned) and on the kernel's local stack.
40397c478bd9Sstevel@tonic-gate  */
40407c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
40417c478bd9Sstevel@tonic-gate /* Function:    copyinptr                                                   */
40427c478bd9Sstevel@tonic-gate /* Returns:     int - 0 = success, else failure                             */
40437c478bd9Sstevel@tonic-gate /* Parameters:  src(I)  - pointer to the source address                     */
40447c478bd9Sstevel@tonic-gate /*              dst(I)  - destination address                               */
40457c478bd9Sstevel@tonic-gate /*              size(I) - number of bytes to copy                           */
40467c478bd9Sstevel@tonic-gate /*                                                                          */
40477c478bd9Sstevel@tonic-gate /* Copy a block of data in from user space, given a pointer to the pointer  */
40487c478bd9Sstevel@tonic-gate /* to start copying from (src) and a pointer to where to store it (dst).    */
40497c478bd9Sstevel@tonic-gate /* NB: src - pointer to user space pointer, dst - kernel space pointer      */
40507c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
40517c478bd9Sstevel@tonic-gate int copyinptr(src, dst, size)
40527c478bd9Sstevel@tonic-gate void *src, *dst;
40537c478bd9Sstevel@tonic-gate size_t size;
40547c478bd9Sstevel@tonic-gate {
40557c478bd9Sstevel@tonic-gate 	caddr_t ca;
40567c478bd9Sstevel@tonic-gate 	int err;
40577c478bd9Sstevel@tonic-gate 
4058ab25eeb5Syz # if SOLARIS
4059ab25eeb5Syz 	err = COPYIN(src, (caddr_t)&ca, sizeof(ca));
40607c478bd9Sstevel@tonic-gate 	if (err != 0)
40617c478bd9Sstevel@tonic-gate 		return err;
4062ab25eeb5Syz # else
40637c478bd9Sstevel@tonic-gate 	bcopy(src, (caddr_t)&ca, sizeof(ca));
4064ab25eeb5Syz # endif
4065ab25eeb5Syz 	err = COPYIN(ca, dst, size);
40667c478bd9Sstevel@tonic-gate 	return err;
40677c478bd9Sstevel@tonic-gate }
40687c478bd9Sstevel@tonic-gate 
40697c478bd9Sstevel@tonic-gate 
40707c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
40717c478bd9Sstevel@tonic-gate /* Function:    copyoutptr                                                  */
40727c478bd9Sstevel@tonic-gate /* Returns:     int - 0 = success, else failure                             */
40737c478bd9Sstevel@tonic-gate /* Parameters:  src(I)  - pointer to the source address                     */
40747c478bd9Sstevel@tonic-gate /*              dst(I)  - destination address                               */
40757c478bd9Sstevel@tonic-gate /*              size(I) - number of bytes to copy                           */
40767c478bd9Sstevel@tonic-gate /*                                                                          */
40777c478bd9Sstevel@tonic-gate /* Copy a block of data out to user space, given a pointer to the pointer   */
40787c478bd9Sstevel@tonic-gate /* to start copying from (src) and a pointer to where to store it (dst).    */
40797c478bd9Sstevel@tonic-gate /* NB: src - kernel space pointer, dst - pointer to user space pointer.     */
40807c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
40817c478bd9Sstevel@tonic-gate int copyoutptr(src, dst, size)
40827c478bd9Sstevel@tonic-gate void *src, *dst;
40837c478bd9Sstevel@tonic-gate size_t size;
40847c478bd9Sstevel@tonic-gate {
40857c478bd9Sstevel@tonic-gate 	caddr_t ca;
40867c478bd9Sstevel@tonic-gate 	int err;
40877c478bd9Sstevel@tonic-gate 
4088ab25eeb5Syz # if SOLARIS
4089ab25eeb5Syz 	err = COPYIN(dst, (caddr_t)&ca, sizeof(ca));
40907c478bd9Sstevel@tonic-gate 	if (err != 0)
40917c478bd9Sstevel@tonic-gate 		return err;
4092ab25eeb5Syz # else
40937c478bd9Sstevel@tonic-gate 	bcopy(dst, (caddr_t)&ca, sizeof(ca));
4094ab25eeb5Syz # endif
4095ab25eeb5Syz 	err = COPYOUT(src, ca, size);
40967c478bd9Sstevel@tonic-gate 	return err;
40977c478bd9Sstevel@tonic-gate }
4098ab25eeb5Syz #endif
40997c478bd9Sstevel@tonic-gate 
41007c478bd9Sstevel@tonic-gate 
4101ab25eeb5Syz /* ------------------------------------------------------------------------ */
4102ab25eeb5Syz /* Function:    fr_lock                                                     */
4103bb1d9de5SJohn Ojemann /* Returns:	int - 0 = success, else error				    */
4104ab25eeb5Syz /* Parameters:  data(I)  - pointer to lock value to set                     */
4105ab25eeb5Syz /*              lockp(O) - pointer to location to store old lock value      */
4106ab25eeb5Syz /*                                                                          */
4107ab25eeb5Syz /* Get the new value for the lock integer, set it and return the old value  */
4108ab25eeb5Syz /* in *lockp.                                                               */
4109ab25eeb5Syz /* ------------------------------------------------------------------------ */
4110bb1d9de5SJohn Ojemann int fr_lock(data, lockp)
4111ab25eeb5Syz caddr_t data;
4112ab25eeb5Syz int *lockp;
41137c478bd9Sstevel@tonic-gate {
4114bb1d9de5SJohn Ojemann 	int arg, err;
41157c478bd9Sstevel@tonic-gate 
4116bb1d9de5SJohn Ojemann 	err = BCOPYIN(data, (caddr_t)&arg, sizeof(arg));
4117bb1d9de5SJohn Ojemann 	if (err != 0)
4118bb1d9de5SJohn Ojemann 		return (EFAULT);
4119bb1d9de5SJohn Ojemann 	err = BCOPYOUT((caddr_t)lockp, data, sizeof(*lockp));
4120bb1d9de5SJohn Ojemann 	if (err != 0)
4121bb1d9de5SJohn Ojemann 		return (EFAULT);
4122ab25eeb5Syz 	*lockp = arg;
4123bb1d9de5SJohn Ojemann 	return (0);
41247c478bd9Sstevel@tonic-gate }
41257c478bd9Sstevel@tonic-gate 
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
41287c478bd9Sstevel@tonic-gate /* Function:    fr_getstat                                                  */
41297c478bd9Sstevel@tonic-gate /* Returns:     Nil                                                         */
41307c478bd9Sstevel@tonic-gate /* Parameters:  fiop(I)  - pointer to ipfilter stats structure              */
41317c478bd9Sstevel@tonic-gate /*                                                                          */
41327c478bd9Sstevel@tonic-gate /* Stores a copy of current pointers, counters, etc, in the friostat        */
41337c478bd9Sstevel@tonic-gate /* structure.                                                               */
41347c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
4135f4b3ec61Sdh void fr_getstat(fiop, ifs)
41367c478bd9Sstevel@tonic-gate friostat_t *fiop;
4137f4b3ec61Sdh ipf_stack_t *ifs;
41387c478bd9Sstevel@tonic-gate {
41397c478bd9Sstevel@tonic-gate 	int i, j;
41407c478bd9Sstevel@tonic-gate 
4141f4b3ec61Sdh 	bcopy((char *)&ifs->ifs_frstats, (char *)fiop->f_st,
4142f4b3ec61Sdh 	    sizeof(filterstats_t) * 2);
4143f4b3ec61Sdh 	fiop->f_locks[IPL_LOGSTATE] = ifs->ifs_fr_state_lock;
4144f4b3ec61Sdh 	fiop->f_locks[IPL_LOGNAT] = ifs->ifs_fr_nat_lock;
4145f4b3ec61Sdh 	fiop->f_locks[IPL_LOGIPF] = ifs->ifs_fr_frag_lock;
4146f4b3ec61Sdh 	fiop->f_locks[IPL_LOGAUTH] = ifs->ifs_fr_auth_lock;
41477c478bd9Sstevel@tonic-gate 
41487c478bd9Sstevel@tonic-gate 	for (i = 0; i < 2; i++)
41497c478bd9Sstevel@tonic-gate 		for (j = 0; j < 2; j++) {
4150f4b3ec61Sdh 			fiop->f_ipf[i][j] = ifs->ifs_ipfilter[i][j];
4151f4b3ec61Sdh 			fiop->f_acct[i][j] = ifs->ifs_ipacct[i][j];
4152f4b3ec61Sdh 			fiop->f_ipf6[i][j] = ifs->ifs_ipfilter6[i][j];
4153f4b3ec61Sdh 			fiop->f_acct6[i][j] = ifs->ifs_ipacct6[i][j];
41547c478bd9Sstevel@tonic-gate 		}
41557c478bd9Sstevel@tonic-gate 
4156f4b3ec61Sdh 	fiop->f_ticks = ifs->ifs_fr_ticks;
4157f4b3ec61Sdh 	fiop->f_active = ifs->ifs_fr_active;
4158f4b3ec61Sdh 	fiop->f_froute[0] = ifs->ifs_fr_frouteok[0];
4159f4b3ec61Sdh 	fiop->f_froute[1] = ifs->ifs_fr_frouteok[1];
41607c478bd9Sstevel@tonic-gate 
4161f4b3ec61Sdh 	fiop->f_running = ifs->ifs_fr_running;
41627c478bd9Sstevel@tonic-gate 	for (i = 0; i < IPL_LOGSIZE; i++) {
4163f4b3ec61Sdh 		fiop->f_groups[i][0] = ifs->ifs_ipfgroups[i][0];
4164f4b3ec61Sdh 		fiop->f_groups[i][1] = ifs->ifs_ipfgroups[i][1];
41657c478bd9Sstevel@tonic-gate 	}
41667c478bd9Sstevel@tonic-gate #ifdef  IPFILTER_LOG
41677c478bd9Sstevel@tonic-gate 	fiop->f_logging = 1;
41687c478bd9Sstevel@tonic-gate #else
41697c478bd9Sstevel@tonic-gate 	fiop->f_logging = 0;
41707c478bd9Sstevel@tonic-gate #endif
4171f4b3ec61Sdh 	fiop->f_defpass = ifs->ifs_fr_pass;
4172ab25eeb5Syz 	fiop->f_features = fr_features;
4173ab25eeb5Syz 	(void) strncpy(fiop->f_version, ipfilter_version,
4174ab25eeb5Syz 		       sizeof(fiop->f_version));
41757c478bd9Sstevel@tonic-gate }
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate 
41787c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
41797c478bd9Sstevel@tonic-gate int icmptoicmp6types[ICMP_MAXTYPE+1] = {
41807c478bd9Sstevel@tonic-gate 	ICMP6_ECHO_REPLY,	/* 0: ICMP_ECHOREPLY */
41817c478bd9Sstevel@tonic-gate 	-1,			/* 1: UNUSED */
41827c478bd9Sstevel@tonic-gate 	-1,			/* 2: UNUSED */
41837c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH,	/* 3: ICMP_UNREACH */
41847c478bd9Sstevel@tonic-gate 	-1,			/* 4: ICMP_SOURCEQUENCH */
41857c478bd9Sstevel@tonic-gate 	ND_REDIRECT,		/* 5: ICMP_REDIRECT */
41867c478bd9Sstevel@tonic-gate 	-1,			/* 6: UNUSED */
41877c478bd9Sstevel@tonic-gate 	-1,			/* 7: UNUSED */
41887c478bd9Sstevel@tonic-gate 	ICMP6_ECHO_REQUEST,	/* 8: ICMP_ECHO */
41897c478bd9Sstevel@tonic-gate 	-1,			/* 9: UNUSED */
41907c478bd9Sstevel@tonic-gate 	-1,			/* 10: UNUSED */
41917c478bd9Sstevel@tonic-gate 	ICMP6_TIME_EXCEEDED,	/* 11: ICMP_TIMXCEED */
41927c478bd9Sstevel@tonic-gate 	ICMP6_PARAM_PROB,	/* 12: ICMP_PARAMPROB */
41937c478bd9Sstevel@tonic-gate 	-1,			/* 13: ICMP_TSTAMP */
41947c478bd9Sstevel@tonic-gate 	-1,			/* 14: ICMP_TSTAMPREPLY */
41957c478bd9Sstevel@tonic-gate 	-1,			/* 15: ICMP_IREQ */
41967c478bd9Sstevel@tonic-gate 	-1,			/* 16: ICMP_IREQREPLY */
41977c478bd9Sstevel@tonic-gate 	-1,			/* 17: ICMP_MASKREQ */
41987c478bd9Sstevel@tonic-gate 	-1,			/* 18: ICMP_MASKREPLY */
41997c478bd9Sstevel@tonic-gate };
42007c478bd9Sstevel@tonic-gate 
42017c478bd9Sstevel@tonic-gate 
42027c478bd9Sstevel@tonic-gate int	icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
42037c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_ADDR,		/* 0: ICMP_UNREACH_NET */
42047c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_ADDR,		/* 1: ICMP_UNREACH_HOST */
42057c478bd9Sstevel@tonic-gate 	-1,				/* 2: ICMP_UNREACH_PROTOCOL */
42067c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_NOPORT,	/* 3: ICMP_UNREACH_PORT */
42077c478bd9Sstevel@tonic-gate 	-1,				/* 4: ICMP_UNREACH_NEEDFRAG */
42087c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_NOTNEIGHBOR,	/* 5: ICMP_UNREACH_SRCFAIL */
42097c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_ADDR,		/* 6: ICMP_UNREACH_NET_UNKNOWN */
42107c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_ADDR,		/* 7: ICMP_UNREACH_HOST_UNKNOWN */
42117c478bd9Sstevel@tonic-gate 	-1,				/* 8: ICMP_UNREACH_ISOLATED */
42127c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_ADMIN,	/* 9: ICMP_UNREACH_NET_PROHIB */
42137c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_ADMIN,	/* 10: ICMP_UNREACH_HOST_PROHIB */
42147c478bd9Sstevel@tonic-gate 	-1,				/* 11: ICMP_UNREACH_TOSNET */
42157c478bd9Sstevel@tonic-gate 	-1,				/* 12: ICMP_UNREACH_TOSHOST */
42167c478bd9Sstevel@tonic-gate 	ICMP6_DST_UNREACH_ADMIN,	/* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
42177c478bd9Sstevel@tonic-gate };
42187c478bd9Sstevel@tonic-gate int	icmpreplytype6[ICMP6_MAXTYPE + 1];
42197c478bd9Sstevel@tonic-gate #endif
42207c478bd9Sstevel@tonic-gate 
42217c478bd9Sstevel@tonic-gate int	icmpreplytype4[ICMP_MAXTYPE + 1];
42227c478bd9Sstevel@tonic-gate 
42237c478bd9Sstevel@tonic-gate 
42247c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
42257c478bd9Sstevel@tonic-gate /* Function:    fr_matchicmpqueryreply                                      */
42267c478bd9Sstevel@tonic-gate /* Returns:     int - 1 if "icmp" is a valid reply to "ic" else 0.          */
42277c478bd9Sstevel@tonic-gate /* Parameters:  v(I)    - IP protocol version (4 or 6)                      */
42287c478bd9Sstevel@tonic-gate /*              ic(I)   - ICMP information                                  */
42297c478bd9Sstevel@tonic-gate /*              icmp(I) - ICMP packet header                                */
42307c478bd9Sstevel@tonic-gate /*              rev(I)  - direction (0 = forward/1 = reverse) of packet     */
42317c478bd9Sstevel@tonic-gate /*                                                                          */
42327c478bd9Sstevel@tonic-gate /* Check if the ICMP packet defined by the header pointed to by icmp is a   */
42337c478bd9Sstevel@tonic-gate /* reply to one as described by what's in ic.  If it is a match, return 1,  */
42347c478bd9Sstevel@tonic-gate /* else return 0 for no match.                                              */
42357c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
42367c478bd9Sstevel@tonic-gate int fr_matchicmpqueryreply(v, ic, icmp, rev)
42377c478bd9Sstevel@tonic-gate int v;
42387c478bd9Sstevel@tonic-gate icmpinfo_t *ic;
42397c478bd9Sstevel@tonic-gate icmphdr_t *icmp;
42407c478bd9Sstevel@tonic-gate int rev;
42417c478bd9Sstevel@tonic-gate {
42427c478bd9Sstevel@tonic-gate 	int ictype;
42437c478bd9Sstevel@tonic-gate 
42447c478bd9Sstevel@tonic-gate 	ictype = ic->ici_type;
42457c478bd9Sstevel@tonic-gate 
42467c478bd9Sstevel@tonic-gate 	if (v == 4) {
42477c478bd9Sstevel@tonic-gate 		/*
42487c478bd9Sstevel@tonic-gate 		 * If we matched its type on the way in, then when going out
42497c478bd9Sstevel@tonic-gate 		 * it will still be the same type.
42507c478bd9Sstevel@tonic-gate 		 */
42517c478bd9Sstevel@tonic-gate 		if ((!rev && (icmp->icmp_type == ictype)) ||
42527c478bd9Sstevel@tonic-gate 		    (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) {
42537c478bd9Sstevel@tonic-gate 			if (icmp->icmp_type != ICMP_ECHOREPLY)
42547c478bd9Sstevel@tonic-gate 				return 1;
4255ab25eeb5Syz 			if (icmp->icmp_id == ic->ici_id)
42567c478bd9Sstevel@tonic-gate 				return 1;
42577c478bd9Sstevel@tonic-gate 		}
42587c478bd9Sstevel@tonic-gate 	}
42597c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
42607c478bd9Sstevel@tonic-gate 	else if (v == 6) {
42617c478bd9Sstevel@tonic-gate 		if ((!rev && (icmp->icmp_type == ictype)) ||
42627c478bd9Sstevel@tonic-gate 		    (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) {
42637c478bd9Sstevel@tonic-gate 			if (icmp->icmp_type != ICMP6_ECHO_REPLY)
42647c478bd9Sstevel@tonic-gate 				return 1;
4265ab25eeb5Syz 			if (icmp->icmp_id == ic->ici_id)
42667c478bd9Sstevel@tonic-gate 				return 1;
42677c478bd9Sstevel@tonic-gate 		}
42687c478bd9Sstevel@tonic-gate 	}
42697c478bd9Sstevel@tonic-gate #endif
42707c478bd9Sstevel@tonic-gate 	return 0;
42717c478bd9Sstevel@tonic-gate }
42727c478bd9Sstevel@tonic-gate 
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
42757c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
42767c478bd9Sstevel@tonic-gate /* Function:    fr_resolvelookup                                            */
42777c478bd9Sstevel@tonic-gate /* Returns:     void * - NULL = failure, else success.                      */
42787c478bd9Sstevel@tonic-gate /* Parameters:  type(I)     - type of lookup these parameters are for.      */
42797c478bd9Sstevel@tonic-gate /*              number(I)   - table number to use when searching            */
42807c478bd9Sstevel@tonic-gate /*              funcptr(IO) - pointer to pointer for storing IP address     */
4281*de22af4eSJohn Ojemann /*			      searching function.			    */
4282*de22af4eSJohn Ojemann /*		ifs	    - ipf stack instance			    */
42837c478bd9Sstevel@tonic-gate /*                                                                          */
42847c478bd9Sstevel@tonic-gate /* Search for the "table" number passed in amongst those configured for     */
42857c478bd9Sstevel@tonic-gate /* that particular type.  If the type is recognised then the function to    */
42867c478bd9Sstevel@tonic-gate /* call to do the IP address search will be change, regardless of whether   */
42877c478bd9Sstevel@tonic-gate /* or not the "table" number exists.                                        */
42887c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
4289f4b3ec61Sdh static void *fr_resolvelookup(type, number, funcptr, ifs)
42907c478bd9Sstevel@tonic-gate u_int type, number;
42917c478bd9Sstevel@tonic-gate lookupfunc_t *funcptr;
4292f4b3ec61Sdh ipf_stack_t *ifs;
42937c478bd9Sstevel@tonic-gate {
42947c478bd9Sstevel@tonic-gate 	char name[FR_GROUPLEN];
42957c478bd9Sstevel@tonic-gate 	iphtable_t *iph;
42967c478bd9Sstevel@tonic-gate 	ip_pool_t *ipo;
42977c478bd9Sstevel@tonic-gate 	void *ptr;
42987c478bd9Sstevel@tonic-gate 
4299ab25eeb5Syz #if defined(SNPRINTF) && defined(_KERNEL)
4300ab25eeb5Syz 	(void) SNPRINTF(name, sizeof(name), "%u", number);
4301ab25eeb5Syz #else
43027c478bd9Sstevel@tonic-gate 	(void) sprintf(name, "%u", number);
4303ab25eeb5Syz #endif
43047c478bd9Sstevel@tonic-gate 
4305f4b3ec61Sdh 	READ_ENTER(&ifs->ifs_ip_poolrw);
43067c478bd9Sstevel@tonic-gate 
43077c478bd9Sstevel@tonic-gate 	switch (type)
43087c478bd9Sstevel@tonic-gate 	{
43097c478bd9Sstevel@tonic-gate 	case IPLT_POOL :
43107c478bd9Sstevel@tonic-gate # if (defined(__osf__) && defined(_KERNEL))
43117c478bd9Sstevel@tonic-gate 		ptr = NULL;
43127c478bd9Sstevel@tonic-gate 		*funcptr = NULL;
43137c478bd9Sstevel@tonic-gate # else
4314f4b3ec61Sdh 		ipo = ip_pool_find(IPL_LOGIPF, name, ifs);
43157c478bd9Sstevel@tonic-gate 		ptr = ipo;
43167c478bd9Sstevel@tonic-gate 		if (ipo != NULL) {
43177c478bd9Sstevel@tonic-gate 			ATOMIC_INC32(ipo->ipo_ref);
43187c478bd9Sstevel@tonic-gate 		}
43197c478bd9Sstevel@tonic-gate 		*funcptr = ip_pool_search;
43207c478bd9Sstevel@tonic-gate # endif
43217c478bd9Sstevel@tonic-gate 		break;
43227c478bd9Sstevel@tonic-gate 	case IPLT_HASH :
4323f4b3ec61Sdh 		iph = fr_findhtable(IPL_LOGIPF, name, ifs);
43247c478bd9Sstevel@tonic-gate 		ptr = iph;
43257c478bd9Sstevel@tonic-gate 		if (iph != NULL) {
43267c478bd9Sstevel@tonic-gate 			ATOMIC_INC32(iph->iph_ref);
43277c478bd9Sstevel@tonic-gate 		}
43287c478bd9Sstevel@tonic-gate 		*funcptr = fr_iphmfindip;
43297c478bd9Sstevel@tonic-gate 		break;
43307c478bd9Sstevel@tonic-gate 	default:
43317c478bd9Sstevel@tonic-gate 		ptr = NULL;
43327c478bd9Sstevel@tonic-gate 		*funcptr = NULL;
43337c478bd9Sstevel@tonic-gate 		break;
43347c478bd9Sstevel@tonic-gate 	}
4335f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ip_poolrw);
43367c478bd9Sstevel@tonic-gate 
43377c478bd9Sstevel@tonic-gate 	return ptr;
43387c478bd9Sstevel@tonic-gate }
43397c478bd9Sstevel@tonic-gate #endif
43407c478bd9Sstevel@tonic-gate 
43417c478bd9Sstevel@tonic-gate 
43427c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
43437c478bd9Sstevel@tonic-gate /* Function:    frrequest                                                   */
43447c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == success, > 0 == errno value                      */
43457c478bd9Sstevel@tonic-gate /* Parameters:  unit(I)     - device for which this is for                  */
43467c478bd9Sstevel@tonic-gate /*              req(I)      - ioctl command (SIOC*)                         */
43477c478bd9Sstevel@tonic-gate /*              data(I)     - pointr to ioctl data                          */
43487c478bd9Sstevel@tonic-gate /*              set(I)      - 1 or 0 (filter set)                           */
43497c478bd9Sstevel@tonic-gate /*              makecopy(I) - flag indicating whether data points to a rule */
43507c478bd9Sstevel@tonic-gate /*                            in kernel space & hence doesn't need copying. */
43517c478bd9Sstevel@tonic-gate /*                                                                          */
43527c478bd9Sstevel@tonic-gate /* This function handles all the requests which operate on the list of      */
43537c478bd9Sstevel@tonic-gate /* filter rules.  This includes adding, deleting, insertion.  It is also    */
43547c478bd9Sstevel@tonic-gate /* responsible for creating groups when a "head" rule is loaded.  Interface */
43557c478bd9Sstevel@tonic-gate /* names are resolved here and other sanity checks are made on the content  */
43567c478bd9Sstevel@tonic-gate /* of the rule structure being loaded.  If a rule has user defined timeouts */
43577c478bd9Sstevel@tonic-gate /* then make sure they are created and initialised before exiting.          */
43587c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
4359f4b3ec61Sdh int frrequest(unit, req, data, set, makecopy, ifs)
43607c478bd9Sstevel@tonic-gate int unit;
4361ab25eeb5Syz ioctlcmd_t req;
43627c478bd9Sstevel@tonic-gate int set, makecopy;
43637c478bd9Sstevel@tonic-gate caddr_t data;
4364f4b3ec61Sdh ipf_stack_t *ifs;
43657c478bd9Sstevel@tonic-gate {
43667c478bd9Sstevel@tonic-gate 	frentry_t frd, *fp, *f, **fprev, **ftail;
43677c478bd9Sstevel@tonic-gate 	int error = 0, in, v;
4368ab25eeb5Syz 	void *ptr, *uptr;
43697c478bd9Sstevel@tonic-gate 	u_int *p, *pp;
43707c478bd9Sstevel@tonic-gate 	frgroup_t *fg;
43717c478bd9Sstevel@tonic-gate 	char *group;
43727c478bd9Sstevel@tonic-gate 
43737c478bd9Sstevel@tonic-gate 	fg = NULL;
43747c478bd9Sstevel@tonic-gate 	fp = &frd;
43757c478bd9Sstevel@tonic-gate 	if (makecopy != 0) {
43767c478bd9Sstevel@tonic-gate 		error = fr_inobj(data, fp, IPFOBJ_FRENTRY);
43777c478bd9Sstevel@tonic-gate 		if (error)
43787c478bd9Sstevel@tonic-gate 			return EFAULT;
43797c478bd9Sstevel@tonic-gate 		if ((fp->fr_flags & FR_T_BUILTIN) != 0)
43807c478bd9Sstevel@tonic-gate 			return EINVAL;
43817c478bd9Sstevel@tonic-gate 		fp->fr_ref = 0;
43827c478bd9Sstevel@tonic-gate 		fp->fr_flags |= FR_COPIED;
43837c478bd9Sstevel@tonic-gate 	} else {
43847c478bd9Sstevel@tonic-gate 		fp = (frentry_t *)data;
43857c478bd9Sstevel@tonic-gate 		if ((fp->fr_type & FR_T_BUILTIN) == 0)
43867c478bd9Sstevel@tonic-gate 			return EINVAL;
43877c478bd9Sstevel@tonic-gate 		fp->fr_flags &= ~FR_COPIED;
43887c478bd9Sstevel@tonic-gate 	}
43897c478bd9Sstevel@tonic-gate 
43907c478bd9Sstevel@tonic-gate 	if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) ||
43917c478bd9Sstevel@tonic-gate 	    ((fp->fr_dsize != 0) && (fp->fr_data == NULL)))
43927c478bd9Sstevel@tonic-gate 		return EINVAL;
43937c478bd9Sstevel@tonic-gate 
43947c478bd9Sstevel@tonic-gate 	v = fp->fr_v;
4395ab25eeb5Syz 	uptr = fp->fr_data;
43967c478bd9Sstevel@tonic-gate 
43977c478bd9Sstevel@tonic-gate 	/*
43987c478bd9Sstevel@tonic-gate 	 * Only filter rules for IPv4 or IPv6 are accepted.
43997c478bd9Sstevel@tonic-gate 	 */
44007c478bd9Sstevel@tonic-gate 	if (v == 4)
44017c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
44027c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
44037c478bd9Sstevel@tonic-gate 	else if (v == 6)
44047c478bd9Sstevel@tonic-gate 		/*EMPTY*/;
44057c478bd9Sstevel@tonic-gate #endif
44067c478bd9Sstevel@tonic-gate 	else {
44077c478bd9Sstevel@tonic-gate 		return EINVAL;
44087c478bd9Sstevel@tonic-gate 	}
44097c478bd9Sstevel@tonic-gate 
44107c478bd9Sstevel@tonic-gate 	/*
44117c478bd9Sstevel@tonic-gate 	 * If the rule is being loaded from user space, i.e. we had to copy it
44127c478bd9Sstevel@tonic-gate 	 * into kernel space, then do not trust the function pointer in the
44137c478bd9Sstevel@tonic-gate 	 * rule.
44147c478bd9Sstevel@tonic-gate 	 */
44157c478bd9Sstevel@tonic-gate 	if ((makecopy == 1) && (fp->fr_func != NULL)) {
44167c478bd9Sstevel@tonic-gate 		if (fr_findfunc(fp->fr_func) == NULL)
44177c478bd9Sstevel@tonic-gate 			return ESRCH;
4418f4b3ec61Sdh 		error = fr_funcinit(fp, ifs);
44197c478bd9Sstevel@tonic-gate 		if (error != 0)
44207c478bd9Sstevel@tonic-gate 			return error;
44217c478bd9Sstevel@tonic-gate 	}
44227c478bd9Sstevel@tonic-gate 
44237c478bd9Sstevel@tonic-gate 	ptr = NULL;
44247c478bd9Sstevel@tonic-gate 	/*
44257c478bd9Sstevel@tonic-gate 	 * Check that the group number does exist and that its use (in/out)
44267c478bd9Sstevel@tonic-gate 	 * matches what the rule is.
44277c478bd9Sstevel@tonic-gate 	 */
4428ab25eeb5Syz 	if (!strncmp(fp->fr_grhead, "0", FR_GROUPLEN))
4429ab25eeb5Syz 		*fp->fr_grhead = '\0';
44307c478bd9Sstevel@tonic-gate 	group = fp->fr_group;
4431ab25eeb5Syz 	if (!strncmp(group, "0", FR_GROUPLEN))
44327c478bd9Sstevel@tonic-gate 		*group = '\0';
44336d512983Sdr 
44346d512983Sdr 	if (FR_ISACCOUNT(fp->fr_flags))
44356d512983Sdr 		unit = IPL_LOGCOUNT;
44366d512983Sdr 
44377c478bd9Sstevel@tonic-gate 	if ((req != (int)SIOCZRLST) && (*group != '\0')) {
4438f4b3ec61Sdh 		fg = fr_findgroup(group, unit, set, NULL, ifs);
44397c478bd9Sstevel@tonic-gate 		if (fg == NULL)
44407c478bd9Sstevel@tonic-gate 			return ESRCH;
44417c478bd9Sstevel@tonic-gate 		if (fg->fg_flags == 0)
44427c478bd9Sstevel@tonic-gate 			fg->fg_flags = fp->fr_flags & FR_INOUT;
44437c478bd9Sstevel@tonic-gate 		else if (fg->fg_flags != (fp->fr_flags & FR_INOUT))
44447c478bd9Sstevel@tonic-gate 			return ESRCH;
44457c478bd9Sstevel@tonic-gate 	}
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
44487c478bd9Sstevel@tonic-gate 
44497c478bd9Sstevel@tonic-gate 	/*
44507c478bd9Sstevel@tonic-gate 	 * Work out which rule list this change is being applied to.
44517c478bd9Sstevel@tonic-gate 	 */
44527c478bd9Sstevel@tonic-gate 	ftail = NULL;
44537c478bd9Sstevel@tonic-gate 	fprev = NULL;
44547c478bd9Sstevel@tonic-gate 	if (unit == IPL_LOGAUTH)
4455f4b3ec61Sdh 		fprev = &ifs->ifs_ipauth;
44567c478bd9Sstevel@tonic-gate 	else if (v == 4) {
44577c478bd9Sstevel@tonic-gate 		if (FR_ISACCOUNT(fp->fr_flags))
4458f4b3ec61Sdh 			fprev = &ifs->ifs_ipacct[in][set];
44596d512983Sdr 		else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
4460f4b3ec61Sdh 			fprev = &ifs->ifs_ipfilter[in][set];
44617c478bd9Sstevel@tonic-gate 	} else if (v == 6) {
44627c478bd9Sstevel@tonic-gate 		if (FR_ISACCOUNT(fp->fr_flags))
4463f4b3ec61Sdh 			fprev = &ifs->ifs_ipacct6[in][set];
44646d512983Sdr 		else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
4465f4b3ec61Sdh 			fprev = &ifs->ifs_ipfilter6[in][set];
44667c478bd9Sstevel@tonic-gate 	}
44677c478bd9Sstevel@tonic-gate 	if (fprev == NULL)
44687c478bd9Sstevel@tonic-gate 		return ESRCH;
44697c478bd9Sstevel@tonic-gate 
44707c478bd9Sstevel@tonic-gate 	if (*group != '\0') {
4471f4b3ec61Sdh 	    if (!fg && !(fg = fr_findgroup(group, unit, set, NULL, ifs)))
44727c478bd9Sstevel@tonic-gate 			return ESRCH;
44737c478bd9Sstevel@tonic-gate 		fprev = &fg->fg_start;
44747c478bd9Sstevel@tonic-gate 	}
44757c478bd9Sstevel@tonic-gate 
44767c478bd9Sstevel@tonic-gate 	ftail = fprev;
4477ab25eeb5Syz 	for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) {
4478ab25eeb5Syz 		if (fp->fr_collect <= f->fr_collect) {
4479ab25eeb5Syz 			ftail = fprev;
4480ab25eeb5Syz 			f = NULL;
4481ab25eeb5Syz 			break;
4482ab25eeb5Syz 		}
4483ab25eeb5Syz 		fprev = ftail;
4484ab25eeb5Syz 	}
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	/*
44877c478bd9Sstevel@tonic-gate 	 * Copy in extra data for the rule.
44887c478bd9Sstevel@tonic-gate 	 */
44897c478bd9Sstevel@tonic-gate 	if (fp->fr_dsize != 0) {
44907c478bd9Sstevel@tonic-gate 		if (makecopy != 0) {
44917c478bd9Sstevel@tonic-gate 			KMALLOCS(ptr, void *, fp->fr_dsize);
44927c478bd9Sstevel@tonic-gate 			if (!ptr)
44937c478bd9Sstevel@tonic-gate 				return ENOMEM;
4494ab25eeb5Syz 			error = COPYIN(uptr, ptr, fp->fr_dsize);
44957c478bd9Sstevel@tonic-gate 		} else {
4496ab25eeb5Syz 			ptr = uptr;
44977c478bd9Sstevel@tonic-gate 			error = 0;
44987c478bd9Sstevel@tonic-gate 		}
44997c478bd9Sstevel@tonic-gate 		if (error != 0) {
45007c478bd9Sstevel@tonic-gate 			KFREES(ptr, fp->fr_dsize);
450172680cf5SDarren Reed 			return EFAULT;
45027c478bd9Sstevel@tonic-gate 		}
45037c478bd9Sstevel@tonic-gate 		fp->fr_data = ptr;
45047c478bd9Sstevel@tonic-gate 	} else
45057c478bd9Sstevel@tonic-gate 		fp->fr_data = NULL;
45067c478bd9Sstevel@tonic-gate 
45077c478bd9Sstevel@tonic-gate 	/*
45087c478bd9Sstevel@tonic-gate 	 * Perform per-rule type sanity checks of their members.
45097c478bd9Sstevel@tonic-gate 	 */
45107c478bd9Sstevel@tonic-gate 	switch (fp->fr_type & ~FR_T_BUILTIN)
45117c478bd9Sstevel@tonic-gate 	{
4512ab25eeb5Syz #if defined(IPFILTER_BPF)
45137c478bd9Sstevel@tonic-gate 	case FR_T_BPFOPC :
45147c478bd9Sstevel@tonic-gate 		if (fp->fr_dsize == 0)
45157c478bd9Sstevel@tonic-gate 			return EINVAL;
45167c478bd9Sstevel@tonic-gate 		if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) {
45177c478bd9Sstevel@tonic-gate 			if (makecopy && fp->fr_data != NULL) {
45187c478bd9Sstevel@tonic-gate 				KFREES(fp->fr_data, fp->fr_dsize);
45197c478bd9Sstevel@tonic-gate 			}
45207c478bd9Sstevel@tonic-gate 			return EINVAL;
45217c478bd9Sstevel@tonic-gate 		}
45227c478bd9Sstevel@tonic-gate 		break;
45237c478bd9Sstevel@tonic-gate #endif
45247c478bd9Sstevel@tonic-gate 	case FR_T_IPF :
452572680cf5SDarren Reed 		if (fp->fr_dsize != sizeof(fripf_t)) {
452672680cf5SDarren Reed 			if (makecopy && fp->fr_data != NULL) {
452772680cf5SDarren Reed 				KFREES(fp->fr_data, fp->fr_dsize);
452872680cf5SDarren Reed 			}
4529ab25eeb5Syz 			return EINVAL;
453072680cf5SDarren Reed 		}
4531ab25eeb5Syz 
4532ab25eeb5Syz 		/*
4533ab25eeb5Syz 		 * Allowing a rule with both "keep state" and "with oow" is
4534ab25eeb5Syz 		 * pointless because adding a state entry to the table will
4535ab25eeb5Syz 		 * fail with the out of window (oow) flag set.
4536ab25eeb5Syz 		 */
453772680cf5SDarren Reed 		if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) {
453872680cf5SDarren Reed 			if (makecopy && fp->fr_data != NULL) {
453972680cf5SDarren Reed 				KFREES(fp->fr_data, fp->fr_dsize);
454072680cf5SDarren Reed 			}
45417c478bd9Sstevel@tonic-gate 			return EINVAL;
454272680cf5SDarren Reed 		}
4543ab25eeb5Syz 
45447c478bd9Sstevel@tonic-gate 		switch (fp->fr_satype)
45457c478bd9Sstevel@tonic-gate 		{
45467c478bd9Sstevel@tonic-gate 		case FRI_BROADCAST :
45477c478bd9Sstevel@tonic-gate 		case FRI_DYNAMIC :
45487c478bd9Sstevel@tonic-gate 		case FRI_NETWORK :
45497c478bd9Sstevel@tonic-gate 		case FRI_NETMASKED :
45507c478bd9Sstevel@tonic-gate 		case FRI_PEERADDR :
45517c478bd9Sstevel@tonic-gate 			if (fp->fr_sifpidx < 0 || fp->fr_sifpidx > 3) {
45527c478bd9Sstevel@tonic-gate 				if (makecopy && fp->fr_data != NULL) {
45537c478bd9Sstevel@tonic-gate 					KFREES(fp->fr_data, fp->fr_dsize);
45547c478bd9Sstevel@tonic-gate 				}
45557c478bd9Sstevel@tonic-gate 				return EINVAL;
45567c478bd9Sstevel@tonic-gate 			}
45577c478bd9Sstevel@tonic-gate 			break;
45587c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
45597c478bd9Sstevel@tonic-gate 		case FRI_LOOKUP :
45607c478bd9Sstevel@tonic-gate 			fp->fr_srcptr = fr_resolvelookup(fp->fr_srctype,
45617c478bd9Sstevel@tonic-gate 							 fp->fr_srcnum,
4562f4b3ec61Sdh 							 &fp->fr_srcfunc, ifs);
45637c478bd9Sstevel@tonic-gate 			break;
45647c478bd9Sstevel@tonic-gate #endif
45657c478bd9Sstevel@tonic-gate 		default :
45667c478bd9Sstevel@tonic-gate 			break;
45677c478bd9Sstevel@tonic-gate 		}
45687c478bd9Sstevel@tonic-gate 
45697c478bd9Sstevel@tonic-gate 		switch (fp->fr_datype)
45707c478bd9Sstevel@tonic-gate 		{
45717c478bd9Sstevel@tonic-gate 		case FRI_BROADCAST :
45727c478bd9Sstevel@tonic-gate 		case FRI_DYNAMIC :
45737c478bd9Sstevel@tonic-gate 		case FRI_NETWORK :
45747c478bd9Sstevel@tonic-gate 		case FRI_NETMASKED :
45757c478bd9Sstevel@tonic-gate 		case FRI_PEERADDR :
45767c478bd9Sstevel@tonic-gate 			if (fp->fr_difpidx < 0 || fp->fr_difpidx > 3) {
45777c478bd9Sstevel@tonic-gate 				if (makecopy && fp->fr_data != NULL) {
45787c478bd9Sstevel@tonic-gate 					KFREES(fp->fr_data, fp->fr_dsize);
45797c478bd9Sstevel@tonic-gate 				}
45807c478bd9Sstevel@tonic-gate 				return EINVAL;
45817c478bd9Sstevel@tonic-gate 			}
45827c478bd9Sstevel@tonic-gate 			break;
45837c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
45847c478bd9Sstevel@tonic-gate 		case FRI_LOOKUP :
45857c478bd9Sstevel@tonic-gate 			fp->fr_dstptr = fr_resolvelookup(fp->fr_dsttype,
45867c478bd9Sstevel@tonic-gate 							 fp->fr_dstnum,
4587f4b3ec61Sdh 							 &fp->fr_dstfunc, ifs);
45887c478bd9Sstevel@tonic-gate 			break;
45897c478bd9Sstevel@tonic-gate #endif
45907c478bd9Sstevel@tonic-gate 		default :
45917c478bd9Sstevel@tonic-gate 			break;
45927c478bd9Sstevel@tonic-gate 		}
45937c478bd9Sstevel@tonic-gate 		break;
45947c478bd9Sstevel@tonic-gate 	case FR_T_NONE :
45957c478bd9Sstevel@tonic-gate 		break;
45967c478bd9Sstevel@tonic-gate 	case FR_T_CALLFUNC :
45977c478bd9Sstevel@tonic-gate 		break;
45987c478bd9Sstevel@tonic-gate 	case FR_T_COMPIPF :
45997c478bd9Sstevel@tonic-gate 		break;
46007c478bd9Sstevel@tonic-gate 	default :
46017c478bd9Sstevel@tonic-gate 		if (makecopy && fp->fr_data != NULL) {
46027c478bd9Sstevel@tonic-gate 			KFREES(fp->fr_data, fp->fr_dsize);
46037c478bd9Sstevel@tonic-gate 		}
46047c478bd9Sstevel@tonic-gate 		return EINVAL;
46057c478bd9Sstevel@tonic-gate 	}
46067c478bd9Sstevel@tonic-gate 
46077c478bd9Sstevel@tonic-gate 	/*
46087c478bd9Sstevel@tonic-gate 	 * Lookup all the interface names that are part of the rule.
46097c478bd9Sstevel@tonic-gate 	 */
4610f4b3ec61Sdh 	frsynclist(0, 0, NULL, NULL, fp, ifs);
46117c478bd9Sstevel@tonic-gate 	fp->fr_statecnt = 0;
46127c478bd9Sstevel@tonic-gate 
46137c478bd9Sstevel@tonic-gate 	/*
46147c478bd9Sstevel@tonic-gate 	 * Look for an existing matching filter rule, but don't include the
46157c478bd9Sstevel@tonic-gate 	 * next or interface pointer in the comparison (fr_next, fr_ifa).
46167c478bd9Sstevel@tonic-gate 	 * This elminates rules which are indentical being loaded.  Checksum
46177c478bd9Sstevel@tonic-gate 	 * the constant part of the filter rule to make comparisons quicker
46187c478bd9Sstevel@tonic-gate 	 * (this meaning no pointers are included).
46197c478bd9Sstevel@tonic-gate 	 */
46207c478bd9Sstevel@tonic-gate 	for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum;
46217c478bd9Sstevel@tonic-gate 	     p < pp; p++)
46227c478bd9Sstevel@tonic-gate 		fp->fr_cksum += *p;
46237c478bd9Sstevel@tonic-gate 	pp = (u_int *)(fp->fr_caddr + fp->fr_dsize);
46247c478bd9Sstevel@tonic-gate 	for (p = (u_int *)fp->fr_data; p < pp; p++)
46257c478bd9Sstevel@tonic-gate 		fp->fr_cksum += *p;
46267c478bd9Sstevel@tonic-gate 
4627f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
462814d3298eSAlexandr Nedvedicky 	bzero((char *)ifs->ifs_frcache, sizeof (ifs->ifs_frcache));
46297c478bd9Sstevel@tonic-gate 
4630ab25eeb5Syz 	for (; (f = *ftail) != NULL; ftail = &f->fr_next) {
4631ab25eeb5Syz 		if ((fp->fr_cksum != f->fr_cksum) ||
4632ab25eeb5Syz 		    (f->fr_dsize != fp->fr_dsize))
4633ab25eeb5Syz 			continue;
4634ab25eeb5Syz 		if (bcmp((char *)&f->fr_func, (char *)&fp->fr_func, FR_CMPSIZ))
4635ab25eeb5Syz 			continue;
4636ab25eeb5Syz 		if ((!ptr && !f->fr_data) ||
4637ab25eeb5Syz 		    (ptr && f->fr_data &&
46387c478bd9Sstevel@tonic-gate 		     !bcmp((char *)ptr, (char *)f->fr_data, f->fr_dsize)))
46397c478bd9Sstevel@tonic-gate 			break;
4640ab25eeb5Syz 	}
46417c478bd9Sstevel@tonic-gate 
46427c478bd9Sstevel@tonic-gate 	/*
46437c478bd9Sstevel@tonic-gate 	 * If zero'ing statistics, copy current to caller and zero.
46447c478bd9Sstevel@tonic-gate 	 */
4645ab25eeb5Syz 	if (req == (ioctlcmd_t)SIOCZRLST) {
46467c478bd9Sstevel@tonic-gate 		if (f == NULL)
46477c478bd9Sstevel@tonic-gate 			error = ESRCH;
46487c478bd9Sstevel@tonic-gate 		else {
4649ab25eeb5Syz 			/*
4650ab25eeb5Syz 			 * Copy and reduce lock because of impending copyout.
4651ab25eeb5Syz 			 * Well we should, but if we do then the atomicity of
4652ab25eeb5Syz 			 * this call and the correctness of fr_hits and
4653ab25eeb5Syz 			 * fr_bytes cannot be guaranteed.  As it is, this code
4654ab25eeb5Syz 			 * only resets them to 0 if they are successfully
4655ab25eeb5Syz 			 * copied out into user space.
4656ab25eeb5Syz 			 */
4657ab25eeb5Syz 			bcopy((char *)f, (char *)fp, sizeof(*f));
4658ab25eeb5Syz 
4659ab25eeb5Syz 			/*
4660ab25eeb5Syz 			 * When we copy this rule back out, set the data
4661ab25eeb5Syz 			 * pointer to be what it was in user space.
4662ab25eeb5Syz 			 */
4663ab25eeb5Syz 			fp->fr_data = uptr;
4664ab25eeb5Syz 			error = fr_outobj(data, fp, IPFOBJ_FRENTRY);
4665ab25eeb5Syz 
46667c478bd9Sstevel@tonic-gate 			if (error == 0) {
4667ab25eeb5Syz 				if ((f->fr_dsize != 0) && (uptr != NULL))
4668ab25eeb5Syz 					error = COPYOUT(f->fr_data, uptr,
46697c478bd9Sstevel@tonic-gate 							f->fr_dsize);
46707c478bd9Sstevel@tonic-gate 				if (error == 0) {
46717c478bd9Sstevel@tonic-gate 					f->fr_hits = 0;
46727c478bd9Sstevel@tonic-gate 					f->fr_bytes = 0;
46737c478bd9Sstevel@tonic-gate 				}
46747c478bd9Sstevel@tonic-gate 			}
46757c478bd9Sstevel@tonic-gate 		}
46767c478bd9Sstevel@tonic-gate 
4677ab25eeb5Syz 		if ((ptr != NULL) && (makecopy != 0)) {
46787c478bd9Sstevel@tonic-gate 			KFREES(ptr, fp->fr_dsize);
46797c478bd9Sstevel@tonic-gate 		}
4680f4b3ec61Sdh 		RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
46817c478bd9Sstevel@tonic-gate 		return error;
46827c478bd9Sstevel@tonic-gate 	}
46837c478bd9Sstevel@tonic-gate 
46847c478bd9Sstevel@tonic-gate 	if (!f) {
4685ab25eeb5Syz 		/*
4686ab25eeb5Syz 		 * At the end of this, ftail must point to the place where the
4687ab25eeb5Syz 		 * new rule is to be saved/inserted/added.
4688ab25eeb5Syz 		 * For SIOCAD*FR, this should be the last rule in the group of
4689ab25eeb5Syz 		 * rules that have equal fr_collect fields.
4690ab25eeb5Syz 		 * For SIOCIN*FR, ...
4691ab25eeb5Syz 		 */
4692ab25eeb5Syz 		if (req == (ioctlcmd_t)SIOCADAFR ||
4693ab25eeb5Syz 		    req == (ioctlcmd_t)SIOCADIFR) {
4694ab25eeb5Syz 
4695ab25eeb5Syz 			for (ftail = fprev; (f = *ftail) != NULL; ) {
4696ab25eeb5Syz 				if (f->fr_collect > fp->fr_collect)
4697ab25eeb5Syz 					break;
4698ab25eeb5Syz 				ftail = &f->fr_next;
4699ab25eeb5Syz 			}
4700ab25eeb5Syz 			f = NULL;
4701ab25eeb5Syz 			ptr = NULL;
4702ab25eeb5Syz 			error = 0;
4703ab25eeb5Syz 		} else if (req == (ioctlcmd_t)SIOCINAFR ||
4704ab25eeb5Syz 			   req == (ioctlcmd_t)SIOCINIFR) {
4705ab25eeb5Syz 			while ((f = *fprev) != NULL) {
4706ab25eeb5Syz 				if (f->fr_collect >= fp->fr_collect)
4707ab25eeb5Syz 					break;
4708ab25eeb5Syz 				fprev = &f->fr_next;
4709ab25eeb5Syz 			}
47107c478bd9Sstevel@tonic-gate 			ftail = fprev;
47117c478bd9Sstevel@tonic-gate 			if (fp->fr_hits != 0) {
4712ab25eeb5Syz 				while (fp->fr_hits && (f = *ftail)) {
4713ab25eeb5Syz 					if (f->fr_collect != fp->fr_collect)
4714ab25eeb5Syz 						break;
4715ab25eeb5Syz 					fprev = ftail;
47167c478bd9Sstevel@tonic-gate 					ftail = &f->fr_next;
4717ab25eeb5Syz 					fp->fr_hits--;
4718ab25eeb5Syz 				}
47197c478bd9Sstevel@tonic-gate 			}
47207c478bd9Sstevel@tonic-gate 			f = NULL;
47217c478bd9Sstevel@tonic-gate 			ptr = NULL;
47227c478bd9Sstevel@tonic-gate 			error = 0;
47237c478bd9Sstevel@tonic-gate 		}
47247c478bd9Sstevel@tonic-gate 	}
47257c478bd9Sstevel@tonic-gate 
47267c478bd9Sstevel@tonic-gate 	/*
47277c478bd9Sstevel@tonic-gate 	 * Request to remove a rule.
47287c478bd9Sstevel@tonic-gate 	 */
4729ab25eeb5Syz 	if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR) {
47307c478bd9Sstevel@tonic-gate 		if (!f)
47317c478bd9Sstevel@tonic-gate 			error = ESRCH;
47327c478bd9Sstevel@tonic-gate 		else {
47337c478bd9Sstevel@tonic-gate 			/*
47347c478bd9Sstevel@tonic-gate 			 * Do not allow activity from user space to interfere
47357c478bd9Sstevel@tonic-gate 			 * with rules not loaded that way.
47367c478bd9Sstevel@tonic-gate 			 */
47377c478bd9Sstevel@tonic-gate 			if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) {
47387c478bd9Sstevel@tonic-gate 				error = EPERM;
47397c478bd9Sstevel@tonic-gate 				goto done;
47407c478bd9Sstevel@tonic-gate 			}
47417c478bd9Sstevel@tonic-gate 
47427c478bd9Sstevel@tonic-gate 			/*
47437c478bd9Sstevel@tonic-gate 			 * Return EBUSY if the rule is being reference by
47447c478bd9Sstevel@tonic-gate 			 * something else (eg state information.
47457c478bd9Sstevel@tonic-gate 			 */
47467c478bd9Sstevel@tonic-gate 			if (f->fr_ref > 1) {
47477c478bd9Sstevel@tonic-gate 				error = EBUSY;
47487c478bd9Sstevel@tonic-gate 				goto done;
47497c478bd9Sstevel@tonic-gate 			}
47507c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SCAN
47517c478bd9Sstevel@tonic-gate 			if (f->fr_isctag[0] != '\0' &&
47527c478bd9Sstevel@tonic-gate 			    (f->fr_isc != (struct ipscan *)-1))
4753ab25eeb5Syz 				ipsc_detachfr(f);
47547c478bd9Sstevel@tonic-gate #endif
47557c478bd9Sstevel@tonic-gate 			if (unit == IPL_LOGAUTH) {
4756f4b3ec61Sdh 				error = fr_preauthcmd(req, f, ftail, ifs);
47577c478bd9Sstevel@tonic-gate 				goto done;
47587c478bd9Sstevel@tonic-gate 			}
47597c478bd9Sstevel@tonic-gate 			if (*f->fr_grhead != '\0')
4760f4b3ec61Sdh 				fr_delgroup(f->fr_grhead, unit, set, ifs);
4761ab25eeb5Syz 			fr_fixskip(ftail, f, -1);
47627c478bd9Sstevel@tonic-gate 			*ftail = f->fr_next;
47637c478bd9Sstevel@tonic-gate 			f->fr_next = NULL;
4764f4b3ec61Sdh 			(void)fr_derefrule(&f, ifs);
47657c478bd9Sstevel@tonic-gate 		}
47667c478bd9Sstevel@tonic-gate 	} else {
47677c478bd9Sstevel@tonic-gate 		/*
47687c478bd9Sstevel@tonic-gate 		 * Not removing, so we must be adding/inserting a rule.
47697c478bd9Sstevel@tonic-gate 		 */
47707c478bd9Sstevel@tonic-gate 		if (f)
47717c478bd9Sstevel@tonic-gate 			error = EEXIST;
47727c478bd9Sstevel@tonic-gate 		else {
47737c478bd9Sstevel@tonic-gate 			if (unit == IPL_LOGAUTH) {
4774f4b3ec61Sdh 				error = fr_preauthcmd(req, fp, ftail, ifs);
47757c478bd9Sstevel@tonic-gate 				goto done;
47767c478bd9Sstevel@tonic-gate 			}
47777c478bd9Sstevel@tonic-gate 			if (makecopy) {
47787c478bd9Sstevel@tonic-gate 				KMALLOC(f, frentry_t *);
47797c478bd9Sstevel@tonic-gate 			} else
47807c478bd9Sstevel@tonic-gate 				f = fp;
47817c478bd9Sstevel@tonic-gate 			if (f != NULL) {
47827c478bd9Sstevel@tonic-gate 				if (fp != f)
47837c478bd9Sstevel@tonic-gate 					bcopy((char *)fp, (char *)f,
47847c478bd9Sstevel@tonic-gate 					      sizeof(*f));
47857c478bd9Sstevel@tonic-gate 				MUTEX_NUKE(&f->fr_lock);
47867c478bd9Sstevel@tonic-gate 				MUTEX_INIT(&f->fr_lock, "filter rule lock");
47877c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_SCAN
4788ab25eeb5Syz 				if (f->fr_isctag[0] != '\0' &&
4789ab25eeb5Syz 				    ipsc_attachfr(f))
47907c478bd9Sstevel@tonic-gate 					f->fr_isc = (struct ipscan *)-1;
47917c478bd9Sstevel@tonic-gate #endif
47927c478bd9Sstevel@tonic-gate 				f->fr_hits = 0;
47937c478bd9Sstevel@tonic-gate 				if (makecopy != 0)
47947c478bd9Sstevel@tonic-gate 					f->fr_ref = 1;
47957c478bd9Sstevel@tonic-gate 				f->fr_next = *ftail;
47967c478bd9Sstevel@tonic-gate 				*ftail = f;
4797ab25eeb5Syz 				if (req == (ioctlcmd_t)SIOCINIFR ||
4798ab25eeb5Syz 				    req == (ioctlcmd_t)SIOCINAFR)
4799ab25eeb5Syz 					fr_fixskip(ftail, f, 1);
48007c478bd9Sstevel@tonic-gate 				f->fr_grp = NULL;
48017c478bd9Sstevel@tonic-gate 				group = f->fr_grhead;
48027c478bd9Sstevel@tonic-gate 				if (*group != '\0') {
48037c478bd9Sstevel@tonic-gate 					fg = fr_addgroup(group, f, f->fr_flags,
4804f4b3ec61Sdh 							 unit, set, ifs);
48057c478bd9Sstevel@tonic-gate 					if (fg != NULL)
48067c478bd9Sstevel@tonic-gate 						f->fr_grp = &fg->fg_start;
48077c478bd9Sstevel@tonic-gate 				}
48087c478bd9Sstevel@tonic-gate 			} else
48097c478bd9Sstevel@tonic-gate 				error = ENOMEM;
48107c478bd9Sstevel@tonic-gate 		}
48117c478bd9Sstevel@tonic-gate 	}
48127c478bd9Sstevel@tonic-gate done:
4813f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
48147c478bd9Sstevel@tonic-gate 	if ((ptr != NULL) && (error != 0) && (makecopy != 0)) {
48157c478bd9Sstevel@tonic-gate 		KFREES(ptr, fp->fr_dsize);
48167c478bd9Sstevel@tonic-gate 	}
48177c478bd9Sstevel@tonic-gate 	return (error);
48187c478bd9Sstevel@tonic-gate }
48197c478bd9Sstevel@tonic-gate 
48207c478bd9Sstevel@tonic-gate 
48217c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
48227c478bd9Sstevel@tonic-gate /* Function:    fr_funcinit                                                 */
48237c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == success, else ESRCH: cannot resolve rule details */
48247c478bd9Sstevel@tonic-gate /* Parameters:  fr(I) - pointer to filter rule                              */
48257c478bd9Sstevel@tonic-gate /*                                                                          */
48267c478bd9Sstevel@tonic-gate /* If a rule is a call rule, then check if the function it points to needs  */
48277c478bd9Sstevel@tonic-gate /* an init function to be called now the rule has been loaded.              */
48287c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
4829f4b3ec61Sdh static int fr_funcinit(fr, ifs)
48307c478bd9Sstevel@tonic-gate frentry_t *fr;
4831f4b3ec61Sdh ipf_stack_t *ifs;
48327c478bd9Sstevel@tonic-gate {
48337c478bd9Sstevel@tonic-gate 	ipfunc_resolve_t *ft;
48347c478bd9Sstevel@tonic-gate 	int err;
48357c478bd9Sstevel@tonic-gate 
48367c478bd9Sstevel@tonic-gate 	err = ESRCH;
48377c478bd9Sstevel@tonic-gate 
48387c478bd9Sstevel@tonic-gate 	for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
48397c478bd9Sstevel@tonic-gate 		if (ft->ipfu_addr == fr->fr_func) {
48407c478bd9Sstevel@tonic-gate 			err = 0;
48417c478bd9Sstevel@tonic-gate 			if (ft->ipfu_init != NULL)
4842f4b3ec61Sdh 				err = (*ft->ipfu_init)(fr, ifs);
48437c478bd9Sstevel@tonic-gate 			break;
48447c478bd9Sstevel@tonic-gate 		}
48457c478bd9Sstevel@tonic-gate 	return err;
48467c478bd9Sstevel@tonic-gate }
48477c478bd9Sstevel@tonic-gate 
48487c478bd9Sstevel@tonic-gate 
48497c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
48507c478bd9Sstevel@tonic-gate /* Function:    fr_findfunc                                                 */
48517c478bd9Sstevel@tonic-gate /* Returns:     ipfunc_t - pointer to function if found, else NULL          */
48527c478bd9Sstevel@tonic-gate /* Parameters:  funcptr(I) - function pointer to lookup                     */
48537c478bd9Sstevel@tonic-gate /*                                                                          */
48547c478bd9Sstevel@tonic-gate /* Look for a function in the table of known functions.                     */
48557c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
48567c478bd9Sstevel@tonic-gate static ipfunc_t fr_findfunc(funcptr)
48577c478bd9Sstevel@tonic-gate ipfunc_t funcptr;
48587c478bd9Sstevel@tonic-gate {
48597c478bd9Sstevel@tonic-gate 	ipfunc_resolve_t *ft;
48607c478bd9Sstevel@tonic-gate 
48617c478bd9Sstevel@tonic-gate 	for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
48627c478bd9Sstevel@tonic-gate 		if (ft->ipfu_addr == funcptr)
48637c478bd9Sstevel@tonic-gate 			return funcptr;
48647c478bd9Sstevel@tonic-gate 	return NULL;
48657c478bd9Sstevel@tonic-gate }
48667c478bd9Sstevel@tonic-gate 
48677c478bd9Sstevel@tonic-gate 
48687c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
48697c478bd9Sstevel@tonic-gate /* Function:    fr_resolvefunc                                              */
48707c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == success, else error                              */
48717c478bd9Sstevel@tonic-gate /* Parameters:  data(IO) - ioctl data pointer to ipfunc_resolve_t struct    */
48727c478bd9Sstevel@tonic-gate /*                                                                          */
48737c478bd9Sstevel@tonic-gate /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */
48747c478bd9Sstevel@tonic-gate /* This will either be the function name (if the pointer is set) or the     */
4875ab25eeb5Syz /* function pointer if the name is set.  When found, fill in the other one  */
4876ab25eeb5Syz /* so that the entire, complete, structure can be copied back to user space.*/
48777c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
48787c478bd9Sstevel@tonic-gate int fr_resolvefunc(data)
48797c478bd9Sstevel@tonic-gate void *data;
48807c478bd9Sstevel@tonic-gate {
48817c478bd9Sstevel@tonic-gate 	ipfunc_resolve_t res, *ft;
4882bb1d9de5SJohn Ojemann 	int err;
48837c478bd9Sstevel@tonic-gate 
4884bb1d9de5SJohn Ojemann 	err = BCOPYIN(data, &res, sizeof(res));
4885bb1d9de5SJohn Ojemann 	if (err != 0)
4886bb1d9de5SJohn Ojemann 		return EFAULT;
48877c478bd9Sstevel@tonic-gate 
48887c478bd9Sstevel@tonic-gate 	if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') {
48897c478bd9Sstevel@tonic-gate 		for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
48907c478bd9Sstevel@tonic-gate 			if (strncmp(res.ipfu_name, ft->ipfu_name,
48917c478bd9Sstevel@tonic-gate 				    sizeof(res.ipfu_name)) == 0) {
48927c478bd9Sstevel@tonic-gate 				res.ipfu_addr = ft->ipfu_addr;
48937c478bd9Sstevel@tonic-gate 				res.ipfu_init = ft->ipfu_init;
48947c478bd9Sstevel@tonic-gate 				if (COPYOUT(&res, data, sizeof(res)) != 0)
48957c478bd9Sstevel@tonic-gate 					return EFAULT;
48967c478bd9Sstevel@tonic-gate 				return 0;
48977c478bd9Sstevel@tonic-gate 			}
48987c478bd9Sstevel@tonic-gate 	}
48997c478bd9Sstevel@tonic-gate 	if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') {
49007c478bd9Sstevel@tonic-gate 		for (ft = fr_availfuncs; ft->ipfu_addr != NULL; ft++)
49017c478bd9Sstevel@tonic-gate 			if (ft->ipfu_addr == res.ipfu_addr) {
49027c478bd9Sstevel@tonic-gate 				(void) strncpy(res.ipfu_name, ft->ipfu_name,
4903ab25eeb5Syz 					       sizeof(res.ipfu_name));
49047c478bd9Sstevel@tonic-gate 				res.ipfu_init = ft->ipfu_init;
49057c478bd9Sstevel@tonic-gate 				if (COPYOUT(&res, data, sizeof(res)) != 0)
49067c478bd9Sstevel@tonic-gate 					return EFAULT;
49077c478bd9Sstevel@tonic-gate 				return 0;
49087c478bd9Sstevel@tonic-gate 			}
49097c478bd9Sstevel@tonic-gate 	}
49107c478bd9Sstevel@tonic-gate 	return ESRCH;
49117c478bd9Sstevel@tonic-gate }
49127c478bd9Sstevel@tonic-gate 
49137c478bd9Sstevel@tonic-gate 
4914ab25eeb5Syz #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && !defined(__FreeBSD__)) || \
4915ab25eeb5Syz     (defined(__FreeBSD__) && (__FreeBSD_version < 490000)) || \
4916ab25eeb5Syz     (defined(__NetBSD__) && (__NetBSD_Version__ < 105000000)) || \
4917ab25eeb5Syz     (defined(__OpenBSD__) && (OpenBSD < 200006))
49187c478bd9Sstevel@tonic-gate /*
49197c478bd9Sstevel@tonic-gate  * From: NetBSD
49207c478bd9Sstevel@tonic-gate  * ppsratecheck(): packets (or events) per second limitation.
49217c478bd9Sstevel@tonic-gate  */
49227c478bd9Sstevel@tonic-gate int
49237c478bd9Sstevel@tonic-gate ppsratecheck(lasttime, curpps, maxpps)
49247c478bd9Sstevel@tonic-gate 	struct timeval *lasttime;
49257c478bd9Sstevel@tonic-gate 	int *curpps;
49267c478bd9Sstevel@tonic-gate 	int maxpps;	/* maximum pps allowed */
49277c478bd9Sstevel@tonic-gate {
49287c478bd9Sstevel@tonic-gate 	struct timeval tv, delta;
49297c478bd9Sstevel@tonic-gate 	int rv;
49307c478bd9Sstevel@tonic-gate 
49317c478bd9Sstevel@tonic-gate 	GETKTIME(&tv);
49327c478bd9Sstevel@tonic-gate 
49337c478bd9Sstevel@tonic-gate 	delta.tv_sec = tv.tv_sec - lasttime->tv_sec;
49347c478bd9Sstevel@tonic-gate 	delta.tv_usec = tv.tv_usec - lasttime->tv_usec;
49357c478bd9Sstevel@tonic-gate 	if (delta.tv_usec < 0) {
49367c478bd9Sstevel@tonic-gate 		delta.tv_sec--;
49377c478bd9Sstevel@tonic-gate 		delta.tv_usec += 1000000;
49387c478bd9Sstevel@tonic-gate 	}
49397c478bd9Sstevel@tonic-gate 
49407c478bd9Sstevel@tonic-gate 	/*
49417c478bd9Sstevel@tonic-gate 	 * check for 0,0 is so that the message will be seen at least once.
49427c478bd9Sstevel@tonic-gate 	 * if more than one second have passed since the last update of
49437c478bd9Sstevel@tonic-gate 	 * lasttime, reset the counter.
49447c478bd9Sstevel@tonic-gate 	 *
49457c478bd9Sstevel@tonic-gate 	 * we do increment *curpps even in *curpps < maxpps case, as some may
49467c478bd9Sstevel@tonic-gate 	 * try to use *curpps for stat purposes as well.
49477c478bd9Sstevel@tonic-gate 	 */
49487c478bd9Sstevel@tonic-gate 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
49497c478bd9Sstevel@tonic-gate 	    delta.tv_sec >= 1) {
49507c478bd9Sstevel@tonic-gate 		*lasttime = tv;
49517c478bd9Sstevel@tonic-gate 		*curpps = 0;
49527c478bd9Sstevel@tonic-gate 		rv = 1;
49537c478bd9Sstevel@tonic-gate 	} else if (maxpps < 0)
49547c478bd9Sstevel@tonic-gate 		rv = 1;
49557c478bd9Sstevel@tonic-gate 	else if (*curpps < maxpps)
49567c478bd9Sstevel@tonic-gate 		rv = 1;
49577c478bd9Sstevel@tonic-gate 	else
49587c478bd9Sstevel@tonic-gate 		rv = 0;
49597c478bd9Sstevel@tonic-gate 	*curpps = *curpps + 1;
49607c478bd9Sstevel@tonic-gate 
49617c478bd9Sstevel@tonic-gate 	return (rv);
49627c478bd9Sstevel@tonic-gate }
49637c478bd9Sstevel@tonic-gate #endif
49647c478bd9Sstevel@tonic-gate 
49657c478bd9Sstevel@tonic-gate 
49667c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
49677c478bd9Sstevel@tonic-gate /* Function:    fr_derefrule                                                */
49687c478bd9Sstevel@tonic-gate /* Returns:     int   - 0 == rule freed up, else rule not freed             */
49697c478bd9Sstevel@tonic-gate /* Parameters:  fr(I) - pointer to filter rule                              */
49707c478bd9Sstevel@tonic-gate /*                                                                          */
49717c478bd9Sstevel@tonic-gate /* Decrement the reference counter to a rule by one.  If it reaches zero,   */
49727c478bd9Sstevel@tonic-gate /* free it and any associated storage space being used by it.               */
49737c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
4974f4b3ec61Sdh int fr_derefrule(frp, ifs)
49757c478bd9Sstevel@tonic-gate frentry_t **frp;
4976f4b3ec61Sdh ipf_stack_t *ifs;
49777c478bd9Sstevel@tonic-gate {
49787c478bd9Sstevel@tonic-gate 	frentry_t *fr;
49797c478bd9Sstevel@tonic-gate 
49807c478bd9Sstevel@tonic-gate 	fr = *frp;
49817c478bd9Sstevel@tonic-gate 
49827c478bd9Sstevel@tonic-gate 	MUTEX_ENTER(&fr->fr_lock);
49837c478bd9Sstevel@tonic-gate 	fr->fr_ref--;
49847c478bd9Sstevel@tonic-gate 	if (fr->fr_ref == 0) {
49857c478bd9Sstevel@tonic-gate 		MUTEX_EXIT(&fr->fr_lock);
49867c478bd9Sstevel@tonic-gate 		MUTEX_DESTROY(&fr->fr_lock);
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate #ifdef IPFILTER_LOOKUP
49897c478bd9Sstevel@tonic-gate 		if (fr->fr_type == FR_T_IPF && fr->fr_satype == FRI_LOOKUP)
4990f4b3ec61Sdh 		    ip_lookup_deref(fr->fr_srctype, fr->fr_srcptr, ifs);
49917c478bd9Sstevel@tonic-gate 		if (fr->fr_type == FR_T_IPF && fr->fr_datype == FRI_LOOKUP)
4992f4b3ec61Sdh 		    ip_lookup_deref(fr->fr_dsttype, fr->fr_dstptr, ifs);
49937c478bd9Sstevel@tonic-gate #endif
49947c478bd9Sstevel@tonic-gate 
49957c478bd9Sstevel@tonic-gate 		if (fr->fr_dsize) {
49967c478bd9Sstevel@tonic-gate 			KFREES(fr->fr_data, fr->fr_dsize);
49977c478bd9Sstevel@tonic-gate 		}
49987c478bd9Sstevel@tonic-gate 		if ((fr->fr_flags & FR_COPIED) != 0) {
49997c478bd9Sstevel@tonic-gate 			KFREE(fr);
50007c478bd9Sstevel@tonic-gate 			return 0;
50017c478bd9Sstevel@tonic-gate 		}
50027c478bd9Sstevel@tonic-gate 		return 1;
50037c478bd9Sstevel@tonic-gate 	} else {
50047c478bd9Sstevel@tonic-gate 		MUTEX_EXIT(&fr->fr_lock);
50057c478bd9Sstevel@tonic-gate 	}
50067c478bd9Sstevel@tonic-gate 	*frp = NULL;
50077c478bd9Sstevel@tonic-gate 	return -1;
50087c478bd9Sstevel@tonic-gate }
50097c478bd9Sstevel@tonic-gate 
50107c478bd9Sstevel@tonic-gate 
50117c478bd9Sstevel@tonic-gate #ifdef	IPFILTER_LOOKUP
50127c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
50137c478bd9Sstevel@tonic-gate /* Function:    fr_grpmapinit                                               */
50147c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == success, else ESRCH because table entry not found*/
50157c478bd9Sstevel@tonic-gate /* Parameters:  fr(I) - pointer to rule to find hash table for              */
50167c478bd9Sstevel@tonic-gate /*                                                                          */
50177c478bd9Sstevel@tonic-gate /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr.  */
50187c478bd9Sstevel@tonic-gate /* fr_ptr is later used by fr_srcgrpmap and fr_dstgrpmap.                   */
50197c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5020f4b3ec61Sdh static int fr_grpmapinit(fr, ifs)
50217c478bd9Sstevel@tonic-gate frentry_t *fr;
5022f4b3ec61Sdh ipf_stack_t *ifs;
50237c478bd9Sstevel@tonic-gate {
50247c478bd9Sstevel@tonic-gate 	char name[FR_GROUPLEN];
50257c478bd9Sstevel@tonic-gate 	iphtable_t *iph;
50267c478bd9Sstevel@tonic-gate 
5027ab25eeb5Syz #if defined(SNPRINTF) && defined(_KERNEL)
5028ab25eeb5Syz 	(void) SNPRINTF(name, sizeof(name), "%d", fr->fr_arg);
5029ab25eeb5Syz #else
50307c478bd9Sstevel@tonic-gate 	(void) sprintf(name, "%d", fr->fr_arg);
5031ab25eeb5Syz #endif
5032f4b3ec61Sdh 	iph = fr_findhtable(IPL_LOGIPF, name, ifs);
50337c478bd9Sstevel@tonic-gate 	if (iph == NULL)
50347c478bd9Sstevel@tonic-gate 		return ESRCH;
50357c478bd9Sstevel@tonic-gate 	if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT))
50367c478bd9Sstevel@tonic-gate 		return ESRCH;
50377c478bd9Sstevel@tonic-gate 	fr->fr_ptr = iph;
50387c478bd9Sstevel@tonic-gate 	return 0;
50397c478bd9Sstevel@tonic-gate }
50407c478bd9Sstevel@tonic-gate 
50417c478bd9Sstevel@tonic-gate 
50427c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
50437c478bd9Sstevel@tonic-gate /* Function:    fr_srcgrpmap                                                */
50447c478bd9Sstevel@tonic-gate /* Returns:     frentry_t * - pointer to "new last matching" rule or NULL   */
50457c478bd9Sstevel@tonic-gate /* Parameters:  fin(I)    - pointer to packet information                   */
50467c478bd9Sstevel@tonic-gate /*              passp(IO) - pointer to current/new filter decision (unused) */
50477c478bd9Sstevel@tonic-gate /*                                                                          */
50487c478bd9Sstevel@tonic-gate /* Look for a rule group head in a hash table, using the source address as  */
50497c478bd9Sstevel@tonic-gate /* the key, and descend into that group and continue matching rules against */
50507c478bd9Sstevel@tonic-gate /* the packet.                                                              */
50517c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
50527c478bd9Sstevel@tonic-gate frentry_t *fr_srcgrpmap(fin, passp)
50537c478bd9Sstevel@tonic-gate fr_info_t *fin;
50547c478bd9Sstevel@tonic-gate u_32_t *passp;
50557c478bd9Sstevel@tonic-gate {
50567c478bd9Sstevel@tonic-gate 	frgroup_t *fg;
50577c478bd9Sstevel@tonic-gate 	void *rval;
5058f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
50597c478bd9Sstevel@tonic-gate 
5060f4b3ec61Sdh 	rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, fin->fin_v, &fin->fin_src, ifs);
50617c478bd9Sstevel@tonic-gate 	if (rval == NULL)
50627c478bd9Sstevel@tonic-gate 		return NULL;
50637c478bd9Sstevel@tonic-gate 
50647c478bd9Sstevel@tonic-gate 	fg = rval;
50657c478bd9Sstevel@tonic-gate 	fin->fin_fr = fg->fg_start;
5066ab25eeb5Syz 	(void) fr_scanlist(fin, *passp);
50677c478bd9Sstevel@tonic-gate 	return fin->fin_fr;
50687c478bd9Sstevel@tonic-gate }
50697c478bd9Sstevel@tonic-gate 
50707c478bd9Sstevel@tonic-gate 
50717c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
50727c478bd9Sstevel@tonic-gate /* Function:    fr_dstgrpmap                                                */
50737c478bd9Sstevel@tonic-gate /* Returns:     frentry_t * - pointer to "new last matching" rule or NULL   */
50747c478bd9Sstevel@tonic-gate /* Parameters:  fin(I)    - pointer to packet information                   */
50757c478bd9Sstevel@tonic-gate /*              passp(IO) - pointer to current/new filter decision (unused) */
50767c478bd9Sstevel@tonic-gate /*                                                                          */
50777c478bd9Sstevel@tonic-gate /* Look for a rule group head in a hash table, using the destination        */
50787c478bd9Sstevel@tonic-gate /* address as the key, and descend into that group and continue matching    */
50797c478bd9Sstevel@tonic-gate /* rules against  the packet.                                               */
50807c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
50817c478bd9Sstevel@tonic-gate frentry_t *fr_dstgrpmap(fin, passp)
50827c478bd9Sstevel@tonic-gate fr_info_t *fin;
50837c478bd9Sstevel@tonic-gate u_32_t *passp;
50847c478bd9Sstevel@tonic-gate {
50857c478bd9Sstevel@tonic-gate 	frgroup_t *fg;
50867c478bd9Sstevel@tonic-gate 	void *rval;
5087f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
50887c478bd9Sstevel@tonic-gate 
5089f4b3ec61Sdh 	rval = fr_iphmfindgroup(fin->fin_fr->fr_ptr, fin->fin_v, &fin->fin_dst, ifs);
50907c478bd9Sstevel@tonic-gate 	if (rval == NULL)
50917c478bd9Sstevel@tonic-gate 		return NULL;
50927c478bd9Sstevel@tonic-gate 
50937c478bd9Sstevel@tonic-gate 	fg = rval;
50947c478bd9Sstevel@tonic-gate 	fin->fin_fr = fg->fg_start;
5095ab25eeb5Syz 	(void) fr_scanlist(fin, *passp);
50967c478bd9Sstevel@tonic-gate 	return fin->fin_fr;
50977c478bd9Sstevel@tonic-gate }
50987c478bd9Sstevel@tonic-gate #endif /* IPFILTER_LOOKUP */
50997c478bd9Sstevel@tonic-gate 
5100ab25eeb5Syz /*
5101ab25eeb5Syz  * Queue functions
5102ab25eeb5Syz  * ===============
5103ab25eeb5Syz  * These functions manage objects on queues for efficient timeouts.  There are
5104ab25eeb5Syz  * a number of system defined queues as well as user defined timeouts.  It is
5105ab25eeb5Syz  * expected that a lock is held in the domain in which the queue belongs
5106ab25eeb5Syz  * (i.e. either state or NAT) when calling any of these functions that prevents
5107ab25eeb5Syz  * fr_freetimeoutqueue() from being called at the same time as any other.
5108ab25eeb5Syz  */
5109ab25eeb5Syz 
51107c478bd9Sstevel@tonic-gate 
51117c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
51127c478bd9Sstevel@tonic-gate /* Function:    fr_addtimeoutqueue                                          */
51137c478bd9Sstevel@tonic-gate /* Returns:     struct ifqtq * - NULL if malloc fails, else pointer to      */
51147c478bd9Sstevel@tonic-gate /*                               timeout queue with given interval.         */
51157c478bd9Sstevel@tonic-gate /* Parameters:  parent(I)  - pointer to pointer to parent node of this list */
51167c478bd9Sstevel@tonic-gate /*                           of interface queues.                           */
51177c478bd9Sstevel@tonic-gate /*              seconds(I) - timeout value in seconds for this queue.       */
51187c478bd9Sstevel@tonic-gate /*                                                                          */
51197c478bd9Sstevel@tonic-gate /* This routine first looks for a timeout queue that matches the interval   */
51207c478bd9Sstevel@tonic-gate /* being requested.  If it finds one, increments the reference counter and  */
51217c478bd9Sstevel@tonic-gate /* returns a pointer to it.  If none are found, it allocates a new one and  */
51227c478bd9Sstevel@tonic-gate /* inserts it at the top of the list.                                       */
5123ab25eeb5Syz /*                                                                          */
5124ab25eeb5Syz /* Locking.                                                                 */
5125ab25eeb5Syz /* It is assumed that the caller of this function has an appropriate lock   */
5126ab25eeb5Syz /* held (exclusively) in the domain that encompases 'parent'.               */
51277c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5128f4b3ec61Sdh ipftq_t *fr_addtimeoutqueue(parent, seconds, ifs)
51297c478bd9Sstevel@tonic-gate ipftq_t **parent;
51307c478bd9Sstevel@tonic-gate u_int seconds;
5131f4b3ec61Sdh ipf_stack_t *ifs;
51327c478bd9Sstevel@tonic-gate {
51337c478bd9Sstevel@tonic-gate 	ipftq_t *ifq;
5134ab25eeb5Syz 	u_int period;
51357c478bd9Sstevel@tonic-gate 
51367c478bd9Sstevel@tonic-gate 	period = seconds * IPF_HZ_DIVIDE;
5137ab25eeb5Syz 
5138f4b3ec61Sdh 	MUTEX_ENTER(&ifs->ifs_ipf_timeoutlock);
5139ab25eeb5Syz 	for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) {
5140ab25eeb5Syz 		if (ifq->ifq_ttl == period) {
5141ab25eeb5Syz 			/*
5142ab25eeb5Syz 			 * Reset the delete flag, if set, so the structure
5143ab25eeb5Syz 			 * gets reused rather than freed and reallocated.
5144ab25eeb5Syz 			 */
5145ab25eeb5Syz 			MUTEX_ENTER(&ifq->ifq_lock);
5146ab25eeb5Syz 			ifq->ifq_flags &= ~IFQF_DELETE;
5147ab25eeb5Syz 			ifq->ifq_ref++;
5148ab25eeb5Syz 			MUTEX_EXIT(&ifq->ifq_lock);
5149f4b3ec61Sdh 			MUTEX_EXIT(&ifs->ifs_ipf_timeoutlock);
51507c478bd9Sstevel@tonic-gate 
5151ab25eeb5Syz 			return ifq;
5152ab25eeb5Syz 		}
51537c478bd9Sstevel@tonic-gate 	}
51547c478bd9Sstevel@tonic-gate 
51557c478bd9Sstevel@tonic-gate 	KMALLOC(ifq, ipftq_t *);
51567c478bd9Sstevel@tonic-gate 	if (ifq != NULL) {
51577c478bd9Sstevel@tonic-gate 		ifq->ifq_ttl = period;
51587c478bd9Sstevel@tonic-gate 		ifq->ifq_head = NULL;
51597c478bd9Sstevel@tonic-gate 		ifq->ifq_tail = &ifq->ifq_head;
51607c478bd9Sstevel@tonic-gate 		ifq->ifq_next = *parent;
51617c478bd9Sstevel@tonic-gate 		ifq->ifq_pnext = parent;
51627c478bd9Sstevel@tonic-gate 		ifq->ifq_ref = 1;
51637c478bd9Sstevel@tonic-gate 		ifq->ifq_flags = IFQF_USER;
51647c478bd9Sstevel@tonic-gate 		*parent = ifq;
5165f4b3ec61Sdh 		ifs->ifs_fr_userifqs++;
51667c478bd9Sstevel@tonic-gate 		MUTEX_NUKE(&ifq->ifq_lock);
51677c478bd9Sstevel@tonic-gate 		MUTEX_INIT(&ifq->ifq_lock, "ipftq mutex");
51687c478bd9Sstevel@tonic-gate 	}
5169f4b3ec61Sdh 	MUTEX_EXIT(&ifs->ifs_ipf_timeoutlock);
51707c478bd9Sstevel@tonic-gate 	return ifq;
51717c478bd9Sstevel@tonic-gate }
51727c478bd9Sstevel@tonic-gate 
51737c478bd9Sstevel@tonic-gate 
51747c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
51757c478bd9Sstevel@tonic-gate /* Function:    fr_deletetimeoutqueue                                       */
5176ab25eeb5Syz /* Returns:     int    - new reference count value of the timeout queue     */
5177ab25eeb5Syz /* Parameters:  ifq(I) - timeout queue which is losing a reference.         */
5178ab25eeb5Syz /* Locks:       ifq->ifq_lock                                               */
51797c478bd9Sstevel@tonic-gate /*                                                                          */
51807c478bd9Sstevel@tonic-gate /* This routine must be called when we're discarding a pointer to a timeout */
5181ab25eeb5Syz /* queue object, taking care of the reference counter.                      */
5182ab25eeb5Syz /*                                                                          */
5183ab25eeb5Syz /* Now that this just sets a DELETE flag, it requires the expire code to    */
5184ab25eeb5Syz /* check the list of user defined timeout queues and call the free function */
5185ab25eeb5Syz /* below (currently commented out) to stop memory leaking.  It is done this */
5186ab25eeb5Syz /* way because the locking may not be sufficient to safely do a free when   */
5187ab25eeb5Syz /* this function is called.                                                 */
51887c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5189ab25eeb5Syz int fr_deletetimeoutqueue(ifq)
51907c478bd9Sstevel@tonic-gate ipftq_t *ifq;
51917c478bd9Sstevel@tonic-gate {
51927c478bd9Sstevel@tonic-gate 
51937c478bd9Sstevel@tonic-gate 	ifq->ifq_ref--;
5194ab25eeb5Syz 	if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) {
5195ab25eeb5Syz 		ifq->ifq_flags |= IFQF_DELETE;
5196ab25eeb5Syz 	}
51977c478bd9Sstevel@tonic-gate 
5198ab25eeb5Syz 	return ifq->ifq_ref;
5199ab25eeb5Syz }
5200ab25eeb5Syz 
5201ab25eeb5Syz 
5202ab25eeb5Syz /* ------------------------------------------------------------------------ */
5203ab25eeb5Syz /* Function:    fr_freetimeoutqueue                                         */
5204ab25eeb5Syz /* Parameters:  ifq(I) - timeout queue which is losing a reference.         */
5205ab25eeb5Syz /* Returns:     Nil                                                         */
5206ab25eeb5Syz /*                                                                          */
5207ab25eeb5Syz /* Locking:                                                                 */
5208ab25eeb5Syz /* It is assumed that the caller of this function has an appropriate lock   */
5209ab25eeb5Syz /* held (exclusively) in the domain that encompases the callers "domain".   */
5210ab25eeb5Syz /* The ifq_lock for this structure should not be held.                      */
5211ab25eeb5Syz /*                                                                          */
5212ab25eeb5Syz /* Remove a user definde timeout queue from the list of queues it is in and */
5213ab25eeb5Syz /* tidy up after this is done.                                              */
5214ab25eeb5Syz /* ------------------------------------------------------------------------ */
5215f4b3ec61Sdh void fr_freetimeoutqueue(ifq, ifs)
5216ab25eeb5Syz ipftq_t *ifq;
5217f4b3ec61Sdh ipf_stack_t *ifs;
5218ab25eeb5Syz {
5219ab25eeb5Syz 
5220ab25eeb5Syz 
5221ab25eeb5Syz 	if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) ||
5222ab25eeb5Syz 	    ((ifq->ifq_flags & IFQF_USER) == 0)) {
5223ab25eeb5Syz 		printf("fr_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n",
5224ab25eeb5Syz 		       (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl,
5225ab25eeb5Syz 		       ifq->ifq_ref);
5226ab25eeb5Syz 		return;
52277c478bd9Sstevel@tonic-gate 	}
5228ab25eeb5Syz 
5229ab25eeb5Syz 	/*
5230ab25eeb5Syz 	 * Remove from its position in the list.
5231ab25eeb5Syz 	 */
5232ab25eeb5Syz 	*ifq->ifq_pnext = ifq->ifq_next;
5233ab25eeb5Syz 	if (ifq->ifq_next != NULL)
5234ab25eeb5Syz 		ifq->ifq_next->ifq_pnext = ifq->ifq_pnext;
5235ab25eeb5Syz 
5236ab25eeb5Syz 	MUTEX_DESTROY(&ifq->ifq_lock);
5237f4b3ec61Sdh 	ifs->ifs_fr_userifqs--;
5238ab25eeb5Syz 	KFREE(ifq);
5239ab25eeb5Syz }
5240ab25eeb5Syz 
5241ab25eeb5Syz 
5242ab25eeb5Syz /* ------------------------------------------------------------------------ */
5243ab25eeb5Syz /* Function:    fr_deletequeueentry                                         */
5244ab25eeb5Syz /* Returns:     Nil                                                         */
5245ab25eeb5Syz /* Parameters:  tqe(I) - timeout queue entry to delete                      */
5246ab25eeb5Syz /*              ifq(I) - timeout queue to remove entry from                 */
5247ab25eeb5Syz /*                                                                          */
5248ab25eeb5Syz /* Remove a tail queue entry from its queue and make it an orphan.          */
5249ab25eeb5Syz /* fr_deletetimeoutqueue is called to make sure the reference count on the  */
5250ab25eeb5Syz /* queue is correct.  We can't, however, call fr_freetimeoutqueue because   */
5251ab25eeb5Syz /* the correct lock(s) may not be held that would make it safe to do so.    */
5252ab25eeb5Syz /* ------------------------------------------------------------------------ */
5253ab25eeb5Syz void fr_deletequeueentry(tqe)
5254ab25eeb5Syz ipftqent_t *tqe;
5255ab25eeb5Syz {
5256ab25eeb5Syz 	ipftq_t *ifq;
5257ab25eeb5Syz 
5258ab25eeb5Syz 	ifq = tqe->tqe_ifq;
5259ab25eeb5Syz 	if (ifq == NULL)
5260ab25eeb5Syz 		return;
5261ab25eeb5Syz 
5262ab25eeb5Syz 	MUTEX_ENTER(&ifq->ifq_lock);
5263ab25eeb5Syz 
5264ab25eeb5Syz 	if (tqe->tqe_pnext != NULL) {
5265ab25eeb5Syz 		*tqe->tqe_pnext = tqe->tqe_next;
5266ab25eeb5Syz 		if (tqe->tqe_next != NULL)
5267ab25eeb5Syz 			tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5268ab25eeb5Syz 		else    /* we must be the tail anyway */
5269ab25eeb5Syz 			ifq->ifq_tail = tqe->tqe_pnext;
5270ab25eeb5Syz 
5271ab25eeb5Syz 		tqe->tqe_pnext = NULL;
5272ab25eeb5Syz 		tqe->tqe_ifq = NULL;
5273ab25eeb5Syz 	}
5274ab25eeb5Syz 
5275ab25eeb5Syz 	(void) fr_deletetimeoutqueue(ifq);
5276ab25eeb5Syz 
5277ab25eeb5Syz 	MUTEX_EXIT(&ifq->ifq_lock);
52787c478bd9Sstevel@tonic-gate }
52797c478bd9Sstevel@tonic-gate 
52807c478bd9Sstevel@tonic-gate 
52817c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
52827c478bd9Sstevel@tonic-gate /* Function:    fr_queuefront                                               */
52837c478bd9Sstevel@tonic-gate /* Returns:     Nil                                                         */
5284ab25eeb5Syz /* Parameters:  tqe(I) - pointer to timeout queue entry                     */
52857c478bd9Sstevel@tonic-gate /*                                                                          */
52867c478bd9Sstevel@tonic-gate /* Move a queue entry to the front of the queue, if it isn't already there. */
52877c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
52887c478bd9Sstevel@tonic-gate void fr_queuefront(tqe)
52897c478bd9Sstevel@tonic-gate ipftqent_t *tqe;
52907c478bd9Sstevel@tonic-gate {
52917c478bd9Sstevel@tonic-gate 	ipftq_t *ifq;
52927c478bd9Sstevel@tonic-gate 
52937c478bd9Sstevel@tonic-gate 	ifq = tqe->tqe_ifq;
5294ab25eeb5Syz 	if (ifq == NULL)
5295ab25eeb5Syz 		return;
52967c478bd9Sstevel@tonic-gate 
5297ab25eeb5Syz 	MUTEX_ENTER(&ifq->ifq_lock);
52987c478bd9Sstevel@tonic-gate 	if (ifq->ifq_head != tqe) {
52997c478bd9Sstevel@tonic-gate 		*tqe->tqe_pnext = tqe->tqe_next;
53007c478bd9Sstevel@tonic-gate 		if (tqe->tqe_next)
53017c478bd9Sstevel@tonic-gate 			tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
53027c478bd9Sstevel@tonic-gate 		else
53037c478bd9Sstevel@tonic-gate 			ifq->ifq_tail = tqe->tqe_pnext;
53047c478bd9Sstevel@tonic-gate 
53057c478bd9Sstevel@tonic-gate 		tqe->tqe_next = ifq->ifq_head;
53067c478bd9Sstevel@tonic-gate 		ifq->ifq_head->tqe_pnext = &tqe->tqe_next;
53077c478bd9Sstevel@tonic-gate 		ifq->ifq_head = tqe;
53087c478bd9Sstevel@tonic-gate 		tqe->tqe_pnext = &ifq->ifq_head;
53097c478bd9Sstevel@tonic-gate 	}
5310ab25eeb5Syz 	MUTEX_EXIT(&ifq->ifq_lock);
53117c478bd9Sstevel@tonic-gate }
53127c478bd9Sstevel@tonic-gate 
53137c478bd9Sstevel@tonic-gate 
53147c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
53157c478bd9Sstevel@tonic-gate /* Function:    fr_queueback                                                */
53167c478bd9Sstevel@tonic-gate /* Returns:     Nil                                                         */
5317ab25eeb5Syz /* Parameters:  tqe(I) - pointer to timeout queue entry                     */
53187c478bd9Sstevel@tonic-gate /*                                                                          */
53197c478bd9Sstevel@tonic-gate /* Move a queue entry to the back of the queue, if it isn't already there.  */
53207c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5321f4b3ec61Sdh void fr_queueback(tqe, ifs)
53227c478bd9Sstevel@tonic-gate ipftqent_t *tqe;
5323f4b3ec61Sdh ipf_stack_t *ifs;
53247c478bd9Sstevel@tonic-gate {
53257c478bd9Sstevel@tonic-gate 	ipftq_t *ifq;
53267c478bd9Sstevel@tonic-gate 
53277c478bd9Sstevel@tonic-gate 	ifq = tqe->tqe_ifq;
53287663b816Sml 	if (ifq == NULL)
53297663b816Sml 		return;
5330f4b3ec61Sdh 	tqe->tqe_die = ifs->ifs_fr_ticks + ifq->ifq_ttl;
53317c478bd9Sstevel@tonic-gate 
5332ab25eeb5Syz 	MUTEX_ENTER(&ifq->ifq_lock);
5333ab25eeb5Syz 	if (tqe->tqe_next == NULL) {		/* at the end already ? */
53347c478bd9Sstevel@tonic-gate 		MUTEX_EXIT(&ifq->ifq_lock);
5335ab25eeb5Syz 		return;
53367c478bd9Sstevel@tonic-gate 	}
5337ab25eeb5Syz 
5338ab25eeb5Syz 	/*
5339ab25eeb5Syz 	 * Remove from list
5340ab25eeb5Syz 	 */
5341ab25eeb5Syz 	*tqe->tqe_pnext = tqe->tqe_next;
5342ab25eeb5Syz 	tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
5343ab25eeb5Syz 
5344ab25eeb5Syz 	/*
5345ab25eeb5Syz 	 * Make it the last entry.
5346ab25eeb5Syz 	 */
5347ab25eeb5Syz 	tqe->tqe_next = NULL;
5348ab25eeb5Syz 	tqe->tqe_pnext = ifq->ifq_tail;
5349ab25eeb5Syz 	*ifq->ifq_tail = tqe;
5350ab25eeb5Syz 	ifq->ifq_tail = &tqe->tqe_next;
5351ab25eeb5Syz 	MUTEX_EXIT(&ifq->ifq_lock);
5352ab25eeb5Syz }
5353ab25eeb5Syz 
5354ab25eeb5Syz 
5355ab25eeb5Syz /* ------------------------------------------------------------------------ */
5356ab25eeb5Syz /* Function:    fr_queueappend                                              */
5357ab25eeb5Syz /* Returns:     Nil                                                         */
5358ab25eeb5Syz /* Parameters:  tqe(I)    - pointer to timeout queue entry                  */
5359ab25eeb5Syz /*              ifq(I)    - pointer to timeout queue                        */
5360ab25eeb5Syz /*              parent(I) - owing object pointer                            */
5361ab25eeb5Syz /*                                                                          */
5362ab25eeb5Syz /* Add a new item to this queue and put it on the very end.                 */
5363ab25eeb5Syz /* ------------------------------------------------------------------------ */
5364f4b3ec61Sdh void fr_queueappend(tqe, ifq, parent, ifs)
5365ab25eeb5Syz ipftqent_t *tqe;
5366ab25eeb5Syz ipftq_t *ifq;
5367ab25eeb5Syz void *parent;
5368f4b3ec61Sdh ipf_stack_t *ifs;
5369ab25eeb5Syz {
5370ab25eeb5Syz 
5371ab25eeb5Syz 	MUTEX_ENTER(&ifq->ifq_lock);
5372ab25eeb5Syz 	tqe->tqe_parent = parent;
5373ab25eeb5Syz 	tqe->tqe_pnext = ifq->ifq_tail;
5374ab25eeb5Syz 	*ifq->ifq_tail = tqe;
5375ab25eeb5Syz 	ifq->ifq_tail = &tqe->tqe_next;
5376ab25eeb5Syz 	tqe->tqe_next = NULL;
5377ab25eeb5Syz 	tqe->tqe_ifq = ifq;
5378f4b3ec61Sdh 	tqe->tqe_die = ifs->ifs_fr_ticks + ifq->ifq_ttl;
5379ab25eeb5Syz 	ifq->ifq_ref++;
5380ab25eeb5Syz 	MUTEX_EXIT(&ifq->ifq_lock);
53817c478bd9Sstevel@tonic-gate }
53827c478bd9Sstevel@tonic-gate 
53837c478bd9Sstevel@tonic-gate 
53847c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
53857c478bd9Sstevel@tonic-gate /* Function:    fr_movequeue                                                */
53867c478bd9Sstevel@tonic-gate /* Returns:     Nil                                                         */
53877c478bd9Sstevel@tonic-gate /* Parameters:  tq(I)   - pointer to timeout queue information              */
53887c478bd9Sstevel@tonic-gate /*              oifp(I) - old timeout queue entry was on                    */
53897c478bd9Sstevel@tonic-gate /*              nifp(I) - new timeout queue to put entry on                 */
5390dc0749f3SJohn Ojemann /*		ifs	- ipf stack instance				    */
53917c478bd9Sstevel@tonic-gate /*                                                                          */
53927c478bd9Sstevel@tonic-gate /* Move a queue entry from one timeout queue to another timeout queue.      */
53937c478bd9Sstevel@tonic-gate /* If it notices that the current entry is already last and does not need   */
53947c478bd9Sstevel@tonic-gate /* to move queue, the return.                                               */
53957c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5396f4b3ec61Sdh void fr_movequeue(tqe, oifq, nifq, ifs)
53977c478bd9Sstevel@tonic-gate ipftqent_t *tqe;
53987c478bd9Sstevel@tonic-gate ipftq_t *oifq, *nifq;
5399f4b3ec61Sdh ipf_stack_t *ifs;
54007c478bd9Sstevel@tonic-gate {
54017c478bd9Sstevel@tonic-gate 	/*
5402dc0749f3SJohn Ojemann 	 * If the queue isn't changing, and the clock hasn't ticked
5403dc0749f3SJohn Ojemann 	 * since the last update, the operation will be a no-op.
54047c478bd9Sstevel@tonic-gate 	 */
5405dc0749f3SJohn Ojemann 	if (oifq == nifq && tqe->tqe_touched == ifs->ifs_fr_ticks)
5406dc0749f3SJohn Ojemann 		return;
5407dc0749f3SJohn Ojemann 
5408dc0749f3SJohn Ojemann 	/*
5409dc0749f3SJohn Ojemann 	 * Grab the lock and update the timers.
5410dc0749f3SJohn Ojemann 	 */
5411dc0749f3SJohn Ojemann 	MUTEX_ENTER(&oifq->ifq_lock);
5412dc0749f3SJohn Ojemann 	tqe->tqe_touched = ifs->ifs_fr_ticks;
5413cbded9aeSdr 	tqe->tqe_die = ifs->ifs_fr_ticks + nifq->ifq_ttl;
5414dc0749f3SJohn Ojemann 
5415dc0749f3SJohn Ojemann 	/*
5416dc0749f3SJohn Ojemann 	 * The remainder of the operation can still be a no-op.
5417dc0749f3SJohn Ojemann 	 *
5418dc0749f3SJohn Ojemann 	 * If the queue isn't changing, check to see if
5419dc0749f3SJohn Ojemann 	 * an update would be meaningless.
5420dc0749f3SJohn Ojemann 	 */
5421cbded9aeSdr 	if (oifq == nifq) {
5422dc0749f3SJohn Ojemann 		if ((tqe->tqe_next == NULL) ||
5423dc0749f3SJohn Ojemann 		    (tqe->tqe_next->tqe_die == tqe->tqe_die)) {
5424dc0749f3SJohn Ojemann 			MUTEX_EXIT(&oifq->ifq_lock);
5425cbded9aeSdr 			return;
5426dc0749f3SJohn Ojemann 		}
5427ab25eeb5Syz 	}
54287c478bd9Sstevel@tonic-gate 
54297c478bd9Sstevel@tonic-gate 	/*
54307c478bd9Sstevel@tonic-gate 	 * Remove from the old queue
54317c478bd9Sstevel@tonic-gate 	 */
54327c478bd9Sstevel@tonic-gate 	*tqe->tqe_pnext = tqe->tqe_next;
54337c478bd9Sstevel@tonic-gate 	if (tqe->tqe_next)
54347c478bd9Sstevel@tonic-gate 		tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
54357c478bd9Sstevel@tonic-gate 	else
54367c478bd9Sstevel@tonic-gate 		oifq->ifq_tail = tqe->tqe_pnext;
54377c478bd9Sstevel@tonic-gate 	tqe->tqe_next = NULL;
54387c478bd9Sstevel@tonic-gate 
54397c478bd9Sstevel@tonic-gate 	/*
54407c478bd9Sstevel@tonic-gate 	 * If we're moving from one queue to another, release the lock on the
54417c478bd9Sstevel@tonic-gate 	 * old queue and get a lock on the new queue.  For user defined queues,
54427c478bd9Sstevel@tonic-gate 	 * if we're moving off it, call delete in case it can now be freed.
54437c478bd9Sstevel@tonic-gate 	 */
54447c478bd9Sstevel@tonic-gate 	if (oifq != nifq) {
54457c478bd9Sstevel@tonic-gate 		tqe->tqe_ifq = NULL;
5446ab25eeb5Syz 
5447ab25eeb5Syz 		(void) fr_deletetimeoutqueue(oifq);
5448ab25eeb5Syz 
54497c478bd9Sstevel@tonic-gate 		MUTEX_EXIT(&oifq->ifq_lock);
54507c478bd9Sstevel@tonic-gate 
54517c478bd9Sstevel@tonic-gate 		MUTEX_ENTER(&nifq->ifq_lock);
5452ab25eeb5Syz 
54537c478bd9Sstevel@tonic-gate 		tqe->tqe_ifq = nifq;
54547c478bd9Sstevel@tonic-gate 		nifq->ifq_ref++;
54557c478bd9Sstevel@tonic-gate 	}
54567c478bd9Sstevel@tonic-gate 
54577c478bd9Sstevel@tonic-gate 	/*
54587c478bd9Sstevel@tonic-gate 	 * Add to the bottom of the new queue
54597c478bd9Sstevel@tonic-gate 	 */
54607c478bd9Sstevel@tonic-gate 	tqe->tqe_pnext = nifq->ifq_tail;
54617c478bd9Sstevel@tonic-gate 	*nifq->ifq_tail = tqe;
54627c478bd9Sstevel@tonic-gate 	nifq->ifq_tail = &tqe->tqe_next;
54637c478bd9Sstevel@tonic-gate 	MUTEX_EXIT(&nifq->ifq_lock);
54647c478bd9Sstevel@tonic-gate }
54657c478bd9Sstevel@tonic-gate 
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
54687c478bd9Sstevel@tonic-gate /* Function:    fr_updateipid                                               */
54697c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == success, -1 == error (packet should be droppped) */
54707c478bd9Sstevel@tonic-gate /* Parameters:  fin(I) - pointer to packet information                      */
54717c478bd9Sstevel@tonic-gate /*                                                                          */
54727c478bd9Sstevel@tonic-gate /* When we are doing NAT, change the IP of every packet to represent a      */
54737c478bd9Sstevel@tonic-gate /* single sequence of packets coming from the host, hiding any host         */
54747c478bd9Sstevel@tonic-gate /* specific sequencing that might otherwise be revealed.  If the packet is  */
54757c478bd9Sstevel@tonic-gate /* a fragment, then store the 'new' IPid in the fragment cache and look up  */
54767c478bd9Sstevel@tonic-gate /* the fragment cache for non-leading fragments.  If a non-leading fragment */
54777c478bd9Sstevel@tonic-gate /* has no match in the cache, return an error.                              */
54787c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
54797c478bd9Sstevel@tonic-gate static INLINE int fr_updateipid(fin)
54807c478bd9Sstevel@tonic-gate fr_info_t *fin;
54817c478bd9Sstevel@tonic-gate {
54827c478bd9Sstevel@tonic-gate 	u_short id, ido, sums;
54837c478bd9Sstevel@tonic-gate 	u_32_t sumd, sum;
54847c478bd9Sstevel@tonic-gate 	ip_t *ip;
54857c478bd9Sstevel@tonic-gate 
54867c478bd9Sstevel@tonic-gate 	if (fin->fin_off != 0) {
54877c478bd9Sstevel@tonic-gate 		sum = fr_ipid_knownfrag(fin);
54887c478bd9Sstevel@tonic-gate 		if (sum == 0xffffffff)
54897c478bd9Sstevel@tonic-gate 			return -1;
54907c478bd9Sstevel@tonic-gate 		sum &= 0xffff;
54917c478bd9Sstevel@tonic-gate 		id = (u_short)sum;
54927c478bd9Sstevel@tonic-gate 	} else {
54937c478bd9Sstevel@tonic-gate 		id = fr_nextipid(fin);
54947c478bd9Sstevel@tonic-gate 		if (fin->fin_off == 0 && (fin->fin_flx & FI_FRAG) != 0)
54957c478bd9Sstevel@tonic-gate 			(void) fr_ipid_newfrag(fin, (u_32_t)id);
54967c478bd9Sstevel@tonic-gate 	}
54977c478bd9Sstevel@tonic-gate 
54987c478bd9Sstevel@tonic-gate 	ip = fin->fin_ip;
54997c478bd9Sstevel@tonic-gate 	ido = ntohs(ip->ip_id);
55007c478bd9Sstevel@tonic-gate 	if (id == ido)
55017c478bd9Sstevel@tonic-gate 		return 0;
55027c478bd9Sstevel@tonic-gate 	ip->ip_id = htons(id);
55037c478bd9Sstevel@tonic-gate 	CALC_SUMD(ido, id, sumd);	/* DESTRUCTIVE MACRO! id,ido change */
55047c478bd9Sstevel@tonic-gate 	sum = (~ntohs(ip->ip_sum)) & 0xffff;
55057c478bd9Sstevel@tonic-gate 	sum += sumd;
55067c478bd9Sstevel@tonic-gate 	sum = (sum >> 16) + (sum & 0xffff);
55077c478bd9Sstevel@tonic-gate 	sum = (sum >> 16) + (sum & 0xffff);
55087c478bd9Sstevel@tonic-gate 	sums = ~(u_short)sum;
55097c478bd9Sstevel@tonic-gate 	ip->ip_sum = htons(sums);
55107c478bd9Sstevel@tonic-gate 	return 0;
55117c478bd9Sstevel@tonic-gate }
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate 
55147c478bd9Sstevel@tonic-gate #ifdef	NEED_FRGETIFNAME
55157c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
55167c478bd9Sstevel@tonic-gate /* Function:    fr_getifname                                                */
5517ab25eeb5Syz /* Returns:     char *    - pointer to interface name                       */
5518ab25eeb5Syz /* Parameters:  ifp(I)    - pointer to network interface                    */
5519ab25eeb5Syz /*              buffer(O) - pointer to where to store interface name        */
55207c478bd9Sstevel@tonic-gate /*                                                                          */
5521ab25eeb5Syz /* Constructs an interface name in the buffer passed.  The buffer passed is */
5522ab25eeb5Syz /* expected to be at least LIFNAMSIZ in bytes big.  If buffer is passed in  */
5523ab25eeb5Syz /* as a NULL pointer then return a pointer to a static array.               */
55247c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
55257c478bd9Sstevel@tonic-gate char *fr_getifname(ifp, buffer)
55267c478bd9Sstevel@tonic-gate struct ifnet *ifp;
55277c478bd9Sstevel@tonic-gate char *buffer;
55287c478bd9Sstevel@tonic-gate {
5529ab25eeb5Syz 	static char namebuf[LIFNAMSIZ];
5530ab25eeb5Syz # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
5531ab25eeb5Syz      defined(__sgi) || defined(linux) || defined(_AIX51) || \
5532ab25eeb5Syz      (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
55337c478bd9Sstevel@tonic-gate 	int unit, space;
55347c478bd9Sstevel@tonic-gate 	char temp[20];
55357c478bd9Sstevel@tonic-gate 	char *s;
5536ab25eeb5Syz # endif
55377c478bd9Sstevel@tonic-gate 
5538f4b3ec61Sdh 	ASSERT(buffer != NULL);
5539f4b3ec61Sdh #ifdef notdef
55407c478bd9Sstevel@tonic-gate 	if (buffer == NULL)
55417c478bd9Sstevel@tonic-gate 		buffer = namebuf;
5542f4b3ec61Sdh #endif
55437c478bd9Sstevel@tonic-gate 	(void) strncpy(buffer, ifp->if_name, LIFNAMSIZ);
5544ab25eeb5Syz 	buffer[LIFNAMSIZ - 1] = '\0';
5545ab25eeb5Syz # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
5546ab25eeb5Syz      defined(__sgi) || defined(_AIX51) || \
5547ab25eeb5Syz      (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
55487c478bd9Sstevel@tonic-gate 	for (s = buffer; *s; s++)
55497c478bd9Sstevel@tonic-gate 		;
55507c478bd9Sstevel@tonic-gate 	unit = ifp->if_unit;
55517c478bd9Sstevel@tonic-gate 	space = LIFNAMSIZ - (s - buffer);
55527c478bd9Sstevel@tonic-gate 	if (space > 0) {
5553ab25eeb5Syz #  if defined(SNPRINTF) && defined(_KERNEL)
5554ab25eeb5Syz 		(void) SNPRINTF(temp, sizeof(temp), "%d", unit);
5555ab25eeb5Syz #  else
55567c478bd9Sstevel@tonic-gate 		(void) sprintf(temp, "%d", unit);
5557ab25eeb5Syz #  endif
55587c478bd9Sstevel@tonic-gate 		(void) strncpy(s, temp, space);
55597c478bd9Sstevel@tonic-gate 	}
5560ab25eeb5Syz # endif
55617c478bd9Sstevel@tonic-gate 	return buffer;
55627c478bd9Sstevel@tonic-gate }
55637c478bd9Sstevel@tonic-gate #endif
55647c478bd9Sstevel@tonic-gate 
55657c478bd9Sstevel@tonic-gate 
55667c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
55677c478bd9Sstevel@tonic-gate /* Function:    fr_ioctlswitch                                              */
55687c478bd9Sstevel@tonic-gate /* Returns:     int     - -1 continue processing, else ioctl return value   */
55697c478bd9Sstevel@tonic-gate /* Parameters:  unit(I) - device unit opened                                */
55707c478bd9Sstevel@tonic-gate /*              data(I) - pointer to ioctl data                             */
55717c478bd9Sstevel@tonic-gate /*              cmd(I)  - ioctl command                                     */
55727c478bd9Sstevel@tonic-gate /*              mode(I) - mode value                                        */
55737c478bd9Sstevel@tonic-gate /*                                                                          */
55747c478bd9Sstevel@tonic-gate /* Based on the value of unit, call the appropriate ioctl handler or return */
55757c478bd9Sstevel@tonic-gate /* EIO if ipfilter is not running.   Also checks if write perms are req'd   */
55767c478bd9Sstevel@tonic-gate /* for the device in order to execute the ioctl.                            */
55777c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5578f4b3ec61Sdh INLINE int fr_ioctlswitch(unit, data, cmd, mode, uid, ctx, ifs)
5579f4b3ec61Sdh int unit, mode, uid;
5580ab25eeb5Syz ioctlcmd_t cmd;
5581f4b3ec61Sdh void *data, *ctx;
5582f4b3ec61Sdh ipf_stack_t *ifs;
55837c478bd9Sstevel@tonic-gate {
55847c478bd9Sstevel@tonic-gate 	int error = 0;
55857c478bd9Sstevel@tonic-gate 
55867c478bd9Sstevel@tonic-gate 	switch (unit)
55877c478bd9Sstevel@tonic-gate 	{
55887c478bd9Sstevel@tonic-gate 	case IPL_LOGIPF :
55897c478bd9Sstevel@tonic-gate 		error = -1;
55907c478bd9Sstevel@tonic-gate 		break;
55917c478bd9Sstevel@tonic-gate 	case IPL_LOGNAT :
5592f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
5593f4b3ec61Sdh 			error = fr_nat_ioctl(data, cmd, mode, uid, ctx, ifs);
55947c478bd9Sstevel@tonic-gate 		else
55957c478bd9Sstevel@tonic-gate 			error = EIO;
55967c478bd9Sstevel@tonic-gate 		break;
55977c478bd9Sstevel@tonic-gate 	case IPL_LOGSTATE :
5598f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
5599f4b3ec61Sdh 			error = fr_state_ioctl(data, cmd, mode, uid, ctx, ifs);
56007c478bd9Sstevel@tonic-gate 		else
56017c478bd9Sstevel@tonic-gate 			error = EIO;
56027c478bd9Sstevel@tonic-gate 		break;
56037c478bd9Sstevel@tonic-gate 	case IPL_LOGAUTH :
5604f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0) {
5605ab25eeb5Syz 			if ((cmd == (ioctlcmd_t)SIOCADAFR) ||
5606ab25eeb5Syz 			    (cmd == (ioctlcmd_t)SIOCRMAFR)) {
56077c478bd9Sstevel@tonic-gate 				if (!(mode & FWRITE)) {
56087c478bd9Sstevel@tonic-gate 					error = EPERM;
56097c478bd9Sstevel@tonic-gate 				} else {
56107c478bd9Sstevel@tonic-gate 					error = frrequest(unit, cmd, data,
5611f4b3ec61Sdh 						  ifs->ifs_fr_active, 1, ifs);
56127c478bd9Sstevel@tonic-gate 				}
56137c478bd9Sstevel@tonic-gate 			} else {
5614f4b3ec61Sdh 				error = fr_auth_ioctl(data, cmd, mode, uid, ctx, ifs);
56157c478bd9Sstevel@tonic-gate 			}
56167c478bd9Sstevel@tonic-gate 		} else
56177c478bd9Sstevel@tonic-gate 			error = EIO;
56187c478bd9Sstevel@tonic-gate 		break;
56197c478bd9Sstevel@tonic-gate 	case IPL_LOGSYNC :
56207c478bd9Sstevel@tonic-gate #ifdef IPFILTER_SYNC
5621f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
5622f4b3ec61Sdh 			error = fr_sync_ioctl(data, cmd, mode, ifs);
56237c478bd9Sstevel@tonic-gate 		else
56247c478bd9Sstevel@tonic-gate #endif
56257c478bd9Sstevel@tonic-gate 			error = EIO;
56267c478bd9Sstevel@tonic-gate 		break;
56277c478bd9Sstevel@tonic-gate 	case IPL_LOGSCAN :
56287c478bd9Sstevel@tonic-gate #ifdef IPFILTER_SCAN
5629f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
5630f4b3ec61Sdh 			error = fr_scan_ioctl(data, cmd, mode, ifs);
56317c478bd9Sstevel@tonic-gate 		else
56327c478bd9Sstevel@tonic-gate #endif
56337c478bd9Sstevel@tonic-gate 			error = EIO;
56347c478bd9Sstevel@tonic-gate 		break;
56357c478bd9Sstevel@tonic-gate 	case IPL_LOGLOOKUP :
56367c478bd9Sstevel@tonic-gate #ifdef IPFILTER_LOOKUP
5637f4b3ec61Sdh 		if (ifs->ifs_fr_running > 0)
5638f4b3ec61Sdh 			error = ip_lookup_ioctl(data, cmd, mode, uid, ctx, ifs);
56397c478bd9Sstevel@tonic-gate 		else
56407c478bd9Sstevel@tonic-gate #endif
56417c478bd9Sstevel@tonic-gate 			error = EIO;
56427c478bd9Sstevel@tonic-gate 		break;
56437c478bd9Sstevel@tonic-gate 	default :
56447c478bd9Sstevel@tonic-gate 		error = EIO;
56457c478bd9Sstevel@tonic-gate 		break;
56467c478bd9Sstevel@tonic-gate 	}
56477c478bd9Sstevel@tonic-gate 
56487c478bd9Sstevel@tonic-gate 	return error;
56497c478bd9Sstevel@tonic-gate }
56507c478bd9Sstevel@tonic-gate 
56517c478bd9Sstevel@tonic-gate 
56527c478bd9Sstevel@tonic-gate /*
56537c478bd9Sstevel@tonic-gate  * This array defines the expected size of objects coming into the kernel
56547c478bd9Sstevel@tonic-gate  * for the various recognised object types.
56557c478bd9Sstevel@tonic-gate  */
5656f4b3ec61Sdh #define	NUM_OBJ_TYPES	19
5657ab25eeb5Syz 
5658ab25eeb5Syz static	int	fr_objbytes[NUM_OBJ_TYPES][2] = {
5659ab25eeb5Syz 	{ 1,	sizeof(struct frentry) },		/* frentry */
5660ab25eeb5Syz 	{ 0,	sizeof(struct friostat) },
5661ab25eeb5Syz 	{ 0,	sizeof(struct fr_info) },
5662ab25eeb5Syz 	{ 0,	sizeof(struct fr_authstat) },
5663ab25eeb5Syz 	{ 0,	sizeof(struct ipfrstat) },
5664ab25eeb5Syz 	{ 0,	sizeof(struct ipnat) },
5665ab25eeb5Syz 	{ 0,	sizeof(struct natstat) },
5666ab25eeb5Syz 	{ 0,	sizeof(struct ipstate_save) },
5667ab25eeb5Syz 	{ 1,	sizeof(struct nat_save) },		/* nat_save */
5668ab25eeb5Syz 	{ 0,	sizeof(struct natlookup) },
5669ab25eeb5Syz 	{ 1,	sizeof(struct ipstate) },		/* ipstate */
5670ab25eeb5Syz 	{ 0,	sizeof(struct ips_stat) },
5671ab25eeb5Syz 	{ 0,	sizeof(struct frauth) },
5672f4b3ec61Sdh 	{ 0,	sizeof(struct ipftune) },
5673f4b3ec61Sdh 	{ 0,	sizeof(struct nat) },                   /* nat_t */
5674f4b3ec61Sdh 	{ 0,	sizeof(struct ipfruleiter) },
5675f4b3ec61Sdh 	{ 0,	sizeof(struct ipfgeniter) },
5676f4b3ec61Sdh 	{ 0,	sizeof(struct ipftable) },
5677f4b3ec61Sdh 	{ 0,	sizeof(struct ipflookupiter) }
56787c478bd9Sstevel@tonic-gate };
56797c478bd9Sstevel@tonic-gate 
56807c478bd9Sstevel@tonic-gate 
56817c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
56827c478bd9Sstevel@tonic-gate /* Function:    fr_inobj                                                    */
56837c478bd9Sstevel@tonic-gate /* Returns:     int     - 0 = success, else failure                         */
56847c478bd9Sstevel@tonic-gate /* Parameters:  data(I) - pointer to ioctl data                             */
56857c478bd9Sstevel@tonic-gate /*              ptr(I)  - pointer to store real data in                     */
56867c478bd9Sstevel@tonic-gate /*              type(I) - type of structure being moved                     */
56877c478bd9Sstevel@tonic-gate /*                                                                          */
56887c478bd9Sstevel@tonic-gate /* Copy in the contents of what the ipfobj_t points to.  In future, we      */
56897c478bd9Sstevel@tonic-gate /* add things to check for version numbers, sizes, etc, to make it backward */
56907c478bd9Sstevel@tonic-gate /* compatible at the ABI for user land.                                     */
56917c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
56927c478bd9Sstevel@tonic-gate int fr_inobj(data, ptr, type)
56937c478bd9Sstevel@tonic-gate void *data;
56947c478bd9Sstevel@tonic-gate void *ptr;
56957c478bd9Sstevel@tonic-gate int type;
56967c478bd9Sstevel@tonic-gate {
56977c478bd9Sstevel@tonic-gate 	ipfobj_t obj;
56987c478bd9Sstevel@tonic-gate 	int error = 0;
56997c478bd9Sstevel@tonic-gate 
5700ab25eeb5Syz 	if ((type < 0) || (type > NUM_OBJ_TYPES-1))
57017c478bd9Sstevel@tonic-gate 		return EINVAL;
57027c478bd9Sstevel@tonic-gate 
5703bb1d9de5SJohn Ojemann 	error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj));
5704bb1d9de5SJohn Ojemann 	if (error != 0)
5705bb1d9de5SJohn Ojemann 		return EFAULT;
57067c478bd9Sstevel@tonic-gate 
57077c478bd9Sstevel@tonic-gate 	if (obj.ipfo_type != type)
57087c478bd9Sstevel@tonic-gate 		return EINVAL;
57097c478bd9Sstevel@tonic-gate 
57107c478bd9Sstevel@tonic-gate #ifndef	IPFILTER_COMPAT
5711ab25eeb5Syz 	if ((fr_objbytes[type][0] & 1) != 0) {
5712ab25eeb5Syz 		if (obj.ipfo_size < fr_objbytes[type][1])
5713ab25eeb5Syz 			return EINVAL;
5714ab25eeb5Syz 	} else if (obj.ipfo_size != fr_objbytes[type][1])
57157c478bd9Sstevel@tonic-gate 		return EINVAL;
57167c478bd9Sstevel@tonic-gate #else
5717d6c23f6fSyx 	if (obj.ipfo_rev != IPFILTER_VERSION) {
5718d6c23f6fSyx 		error = fr_incomptrans(&obj, ptr);
5719d6c23f6fSyx 		return error;
5720d6c23f6fSyx 	}
5721d6c23f6fSyx 
5722d6c23f6fSyx 	if ((fr_objbytes[type][0] & 1) != 0 &&
5723d6c23f6fSyx 	    obj.ipfo_size < fr_objbytes[type][1] ||
5724d6c23f6fSyx 	    obj.ipfo_size != fr_objbytes[type][1])
57257c478bd9Sstevel@tonic-gate 		return EINVAL;
57267c478bd9Sstevel@tonic-gate #endif
57277c478bd9Sstevel@tonic-gate 
5728ab25eeb5Syz 	if ((fr_objbytes[type][0] & 1) != 0) {
5729ab25eeb5Syz 		error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr,
5730ab25eeb5Syz 				fr_objbytes[type][1]);
5731ab25eeb5Syz 	} else {
5732ab25eeb5Syz 		error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr,
5733ab25eeb5Syz 				obj.ipfo_size);
5734ab25eeb5Syz 	}
57357c478bd9Sstevel@tonic-gate 	return error;
57367c478bd9Sstevel@tonic-gate }
57377c478bd9Sstevel@tonic-gate 
57387c478bd9Sstevel@tonic-gate 
57397c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5740ab25eeb5Syz /* Function:    fr_inobjsz                                                  */
57417c478bd9Sstevel@tonic-gate /* Returns:     int     - 0 = success, else failure                         */
57427c478bd9Sstevel@tonic-gate /* Parameters:  data(I) - pointer to ioctl data                             */
57437c478bd9Sstevel@tonic-gate /*              ptr(I)  - pointer to store real data in                     */
57447c478bd9Sstevel@tonic-gate /*              type(I) - type of structure being moved                     */
5745ab25eeb5Syz /*              sz(I)   - size of data to copy                              */
57467c478bd9Sstevel@tonic-gate /*                                                                          */
5747ab25eeb5Syz /* As per fr_inobj, except the size of the object to copy in is passed in   */
5748ab25eeb5Syz /* but it must not be smaller than the size defined for the type and the    */
5749ab25eeb5Syz /* type must allow for varied sized objects.  The extra requirement here is */
5750ab25eeb5Syz /* that sz must match the size of the object being passed in - this is not  */
5751ab25eeb5Syz /* not possible nor required in fr_inobj().                                 */
57527c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5753ab25eeb5Syz int fr_inobjsz(data, ptr, type, sz)
57547c478bd9Sstevel@tonic-gate void *data;
57557c478bd9Sstevel@tonic-gate void *ptr;
5756ab25eeb5Syz int type, sz;
57577c478bd9Sstevel@tonic-gate {
57587c478bd9Sstevel@tonic-gate 	ipfobj_t obj;
57597c478bd9Sstevel@tonic-gate 	int error;
57607c478bd9Sstevel@tonic-gate 
5761ab25eeb5Syz 	if ((type < 0) || (type > NUM_OBJ_TYPES-1))
5762ab25eeb5Syz 		return EINVAL;
5763ab25eeb5Syz 	if (((fr_objbytes[type][0] & 1) == 0) || (sz < fr_objbytes[type][1]))
5764ab25eeb5Syz 		return EINVAL;
5765ab25eeb5Syz 
5766bb1d9de5SJohn Ojemann 	error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj));
5767bb1d9de5SJohn Ojemann 	if (error != 0)
5768bb1d9de5SJohn Ojemann 		return EFAULT;
57697c478bd9Sstevel@tonic-gate 
57707c478bd9Sstevel@tonic-gate 	if (obj.ipfo_type != type)
57717c478bd9Sstevel@tonic-gate 		return EINVAL;
57727c478bd9Sstevel@tonic-gate 
57737c478bd9Sstevel@tonic-gate #ifndef	IPFILTER_COMPAT
5774ab25eeb5Syz 	if (obj.ipfo_size != sz)
57757c478bd9Sstevel@tonic-gate 		return EINVAL;
57767c478bd9Sstevel@tonic-gate #else
57777c478bd9Sstevel@tonic-gate 	if (obj.ipfo_rev != IPFILTER_VERSION)
5778d6c23f6fSyx 		/*XXX compatibility hook here */
5779d6c23f6fSyx 		/*EMPTY*/;
5780ab25eeb5Syz 	if (obj.ipfo_size != sz)
57817c478bd9Sstevel@tonic-gate 		/* XXX compatibility hook here */
57827c478bd9Sstevel@tonic-gate 		return EINVAL;
57837c478bd9Sstevel@tonic-gate #endif
57847c478bd9Sstevel@tonic-gate 
5785ab25eeb5Syz 	error = COPYIN((caddr_t)obj.ipfo_ptr, (caddr_t)ptr, sz);
57867c478bd9Sstevel@tonic-gate 	return error;
57877c478bd9Sstevel@tonic-gate }
57887c478bd9Sstevel@tonic-gate 
57897c478bd9Sstevel@tonic-gate 
57907c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5791ab25eeb5Syz /* Function:    fr_outobjsz                                                 */
5792ab25eeb5Syz /* Returns:     int     - 0 = success, else failure                         */
5793ab25eeb5Syz /* Parameters:  data(I) - pointer to ioctl data                             */
5794ab25eeb5Syz /*              ptr(I)  - pointer to store real data in                     */
5795ab25eeb5Syz /*              type(I) - type of structure being moved                     */
5796ab25eeb5Syz /*              sz(I)   - size of data to copy                              */
57977c478bd9Sstevel@tonic-gate /*                                                                          */
5798ab25eeb5Syz /* As per fr_outobj, except the size of the object to copy out is passed in */
5799ab25eeb5Syz /* but it must not be smaller than the size defined for the type and the    */
5800ab25eeb5Syz /* type must allow for varied sized objects.  The extra requirement here is */
5801ab25eeb5Syz /* that sz must match the size of the object being passed in - this is not  */
5802ab25eeb5Syz /* not possible nor required in fr_outobj().                                */
58037c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
5804ab25eeb5Syz int fr_outobjsz(data, ptr, type, sz)
5805ab25eeb5Syz void *data;
5806ab25eeb5Syz void *ptr;
5807ab25eeb5Syz int type, sz;
58087c478bd9Sstevel@tonic-gate {
5809ab25eeb5Syz 	ipfobj_t obj;
5810ab25eeb5Syz 	int error;
58117c478bd9Sstevel@tonic-gate 
5812ab25eeb5Syz 	if ((type < 0) || (type > NUM_OBJ_TYPES-1) ||
5813ab25eeb5Syz 	    ((fr_objbytes[type][0] & 1) == 0) ||
5814ab25eeb5Syz 	    (sz < fr_objbytes[type][1]))
5815ab25eeb5Syz 		return EINVAL;
58167c478bd9Sstevel@tonic-gate 
5817bb1d9de5SJohn Ojemann 	error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj));
5818bb1d9de5SJohn Ojemann 	if (error != 0)
5819bb1d9de5SJohn Ojemann 		return EFAULT;
58207c478bd9Sstevel@tonic-gate 
5821ab25eeb5Syz 	if (obj.ipfo_type != type)
5822ab25eeb5Syz 		return EINVAL;
58237c478bd9Sstevel@tonic-gate 
5824ab25eeb5Syz #ifndef	IPFILTER_COMPAT
5825ab25eeb5Syz 	if (obj.ipfo_size != sz)
5826ab25eeb5Syz 		return EINVAL;
5827ab25eeb5Syz #else
5828ab25eeb5Syz 	if (obj.ipfo_rev != IPFILTER_VERSION)
5829ab25eeb5Syz 		/* XXX compatibility hook here */
5830d6c23f6fSyx 		/*EMPTY*/;
5831ab25eeb5Syz 	if (obj.ipfo_size != sz)
5832ab25eeb5Syz 		/* XXX compatibility hook here */
5833ab25eeb5Syz 		return EINVAL;
58347c478bd9Sstevel@tonic-gate #endif
5835ab25eeb5Syz 
5836ab25eeb5Syz 	error = COPYOUT((caddr_t)ptr, (caddr_t)obj.ipfo_ptr, sz);
5837ab25eeb5Syz 	return error;
5838ab25eeb5Syz }
5839ab25eeb5Syz 
5840ab25eeb5Syz 
5841ab25eeb5Syz /* ------------------------------------------------------------------------ */
5842ab25eeb5Syz /* Function:    fr_outobj                                                   */
5843ab25eeb5Syz /* Returns:     int     - 0 = success, else failure                         */
5844ab25eeb5Syz /* Parameters:  data(I) - pointer to ioctl data                             */
5845ab25eeb5Syz /*              ptr(I)  - pointer to store real data in                     */
5846ab25eeb5Syz /*              type(I) - type of structure being moved                     */
5847ab25eeb5Syz /*                                                                          */
5848ab25eeb5Syz /* Copy out the contents of what ptr is to where ipfobj points to.  In      */
5849ab25eeb5Syz /* future, we add things to check for version numbers, sizes, etc, to make  */
5850ab25eeb5Syz /* it backward  compatible at the ABI for user land.                        */
5851ab25eeb5Syz /* ------------------------------------------------------------------------ */
5852ab25eeb5Syz int fr_outobj(data, ptr, type)
5853ab25eeb5Syz void *data;
5854ab25eeb5Syz void *ptr;
5855ab25eeb5Syz int type;
5856ab25eeb5Syz {
5857ab25eeb5Syz 	ipfobj_t obj;
5858ab25eeb5Syz 	int error;
5859ab25eeb5Syz 
5860ab25eeb5Syz 	if ((type < 0) || (type > NUM_OBJ_TYPES-1))
5861ab25eeb5Syz 		return EINVAL;
5862ab25eeb5Syz 
5863bb1d9de5SJohn Ojemann 	error = BCOPYIN((caddr_t)data, (caddr_t)&obj, sizeof(obj));
5864bb1d9de5SJohn Ojemann 	if (error != 0)
5865bb1d9de5SJohn Ojemann 		return EFAULT;
5866ab25eeb5Syz 
5867ab25eeb5Syz 	if (obj.ipfo_type != type)
5868ab25eeb5Syz 		return EINVAL;
5869ab25eeb5Syz 
5870ab25eeb5Syz #ifndef	IPFILTER_COMPAT
5871ab25eeb5Syz 	if ((fr_objbytes[type][0] & 1) != 0) {
5872ab25eeb5Syz 		if (obj.ipfo_size < fr_objbytes[type][1])
5873ab25eeb5Syz 			return EINVAL;
5874ab25eeb5Syz 	} else if (obj.ipfo_size != fr_objbytes[type][1])
5875ab25eeb5Syz 		return EINVAL;
5876ab25eeb5Syz #else
5877d6c23f6fSyx 	if (obj.ipfo_rev != IPFILTER_VERSION) {
5878d6c23f6fSyx 		error = fr_outcomptrans(&obj, ptr);
5879d6c23f6fSyx 		return error;
5880d6c23f6fSyx 	}
5881d6c23f6fSyx 
5882d6c23f6fSyx 	if ((fr_objbytes[type][0] & 1) != 0 &&
5883d6c23f6fSyx 	    obj.ipfo_size < fr_objbytes[type][1] ||
5884d6c23f6fSyx 	    obj.ipfo_size != fr_objbytes[type][1])
5885ab25eeb5Syz 		return EINVAL;
5886ab25eeb5Syz #endif
5887ab25eeb5Syz 
5888ab25eeb5Syz 	error = COPYOUT((caddr_t)ptr, (caddr_t)obj.ipfo_ptr, obj.ipfo_size);
5889ab25eeb5Syz 	return error;
5890ab25eeb5Syz }
5891ab25eeb5Syz 
5892ab25eeb5Syz 
5893ab25eeb5Syz /* ------------------------------------------------------------------------ */
5894ab25eeb5Syz /* Function:    fr_checkl4sum                                               */
5895ab25eeb5Syz /* Returns:     int     - 0 = good, -1 = bad, 1 = cannot check              */
5896ab25eeb5Syz /* Parameters:  fin(I) - pointer to packet information                      */
5897ab25eeb5Syz /*                                                                          */
5898ab25eeb5Syz /* If possible, calculate the layer 4 checksum for the packet.  If this is  */
5899ab25eeb5Syz /* not possible, return without indicating a failure or success but in a    */
5900ab25eeb5Syz /* way that is ditinguishable.                                              */
5901ab25eeb5Syz /* ------------------------------------------------------------------------ */
5902ab25eeb5Syz int fr_checkl4sum(fin)
5903ab25eeb5Syz fr_info_t *fin;
5904ab25eeb5Syz {
5905ab25eeb5Syz 	u_short sum, hdrsum, *csump;
5906ab25eeb5Syz 	udphdr_t *udp;
5907ab25eeb5Syz 	int dosum;
5908f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
5909ab25eeb5Syz 
5910381a2a9aSdr #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
59117ddc9b1aSDarren Reed 	net_handle_t net_data_p;
5912381a2a9aSdr 	if (fin->fin_v == 4)
5913f4b3ec61Sdh 		net_data_p = ifs->ifs_ipf_ipv4;
5914381a2a9aSdr 	else
5915f4b3ec61Sdh 		net_data_p = ifs->ifs_ipf_ipv6;
5916381a2a9aSdr #endif
5917381a2a9aSdr 
5918ab25eeb5Syz 	if ((fin->fin_flx & FI_NOCKSUM) != 0)
5919ab25eeb5Syz 		return 0;
5920ab25eeb5Syz 
5921ab25eeb5Syz 	/*
5922ab25eeb5Syz 	 * If the TCP packet isn't a fragment, isn't too short and otherwise
5923ab25eeb5Syz 	 * isn't already considered "bad", then validate the checksum.  If
5924ab25eeb5Syz 	 * this check fails then considered the packet to be "bad".
5925ab25eeb5Syz 	 */
5926ab25eeb5Syz 	if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0)
5927ab25eeb5Syz 		return 1;
5928ab25eeb5Syz 
5929ab25eeb5Syz 	csump = NULL;
5930ab25eeb5Syz 	hdrsum = 0;
5931ab25eeb5Syz 	dosum = 0;
5932ab25eeb5Syz 	sum = 0;
5933ab25eeb5Syz 
5934381a2a9aSdr #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
5935381a2a9aSdr 	ASSERT(fin->fin_m != NULL);
5936381a2a9aSdr 	if (NET_IS_HCK_L4_FULL(net_data_p, fin->fin_m) ||
5937381a2a9aSdr 	    NET_IS_HCK_L4_PART(net_data_p, fin->fin_m)) {
5938381a2a9aSdr 			hdrsum = 0;
5939381a2a9aSdr 			sum = 0;
5940ab25eeb5Syz 	} else {
5941ab25eeb5Syz #endif
5942ab25eeb5Syz 		switch (fin->fin_p)
5943ab25eeb5Syz 		{
59447c478bd9Sstevel@tonic-gate 		case IPPROTO_TCP :
59457c478bd9Sstevel@tonic-gate 			csump = &((tcphdr_t *)fin->fin_dp)->th_sum;
59467c478bd9Sstevel@tonic-gate 			dosum = 1;
59477c478bd9Sstevel@tonic-gate 			break;
59487c478bd9Sstevel@tonic-gate 
59497c478bd9Sstevel@tonic-gate 		case IPPROTO_UDP :
59507c478bd9Sstevel@tonic-gate 			udp = fin->fin_dp;
59517c478bd9Sstevel@tonic-gate 			if (udp->uh_sum != 0) {
59527c478bd9Sstevel@tonic-gate 				csump = &udp->uh_sum;
59537c478bd9Sstevel@tonic-gate 				dosum = 1;
59547c478bd9Sstevel@tonic-gate 			}
59557c478bd9Sstevel@tonic-gate 			break;
59567c478bd9Sstevel@tonic-gate 
59577c478bd9Sstevel@tonic-gate 		case IPPROTO_ICMP :
59587c478bd9Sstevel@tonic-gate 			csump = &((struct icmp *)fin->fin_dp)->icmp_cksum;
59597c478bd9Sstevel@tonic-gate 			dosum = 1;
59607c478bd9Sstevel@tonic-gate 			break;
59617c478bd9Sstevel@tonic-gate 
59627c478bd9Sstevel@tonic-gate 		default :
59637c478bd9Sstevel@tonic-gate 			return 1;
59647c478bd9Sstevel@tonic-gate 			/*NOTREACHED*/
59657c478bd9Sstevel@tonic-gate 		}
59667c478bd9Sstevel@tonic-gate 
59677c478bd9Sstevel@tonic-gate 		if (csump != NULL)
59687c478bd9Sstevel@tonic-gate 			hdrsum = *csump;
59697c478bd9Sstevel@tonic-gate 
59707c478bd9Sstevel@tonic-gate 		if (dosum)
59717c478bd9Sstevel@tonic-gate 			sum = fr_cksum(fin->fin_m, fin->fin_ip,
59727c478bd9Sstevel@tonic-gate 				       fin->fin_p, fin->fin_dp);
5973381a2a9aSdr #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6)
59747c478bd9Sstevel@tonic-gate 	}
59757c478bd9Sstevel@tonic-gate #endif
59767c478bd9Sstevel@tonic-gate #if !defined(_KERNEL)
5977ab25eeb5Syz 	if (sum == hdrsum) {
5978ab25eeb5Syz 		FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum));
5979ab25eeb5Syz 	} else {
5980ab25eeb5Syz 		FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum));
5981ab25eeb5Syz 	}
59827c478bd9Sstevel@tonic-gate #endif
59837c478bd9Sstevel@tonic-gate 	if (hdrsum == sum)
59847c478bd9Sstevel@tonic-gate 		return 0;
59857c478bd9Sstevel@tonic-gate 	return -1;
59867c478bd9Sstevel@tonic-gate }
59877c478bd9Sstevel@tonic-gate 
59887c478bd9Sstevel@tonic-gate 
59897c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
59907c478bd9Sstevel@tonic-gate /* Function:    fr_ifpfillv4addr                                            */
59917c478bd9Sstevel@tonic-gate /* Returns:     int     - 0 = address update, -1 = address not updated      */
59927c478bd9Sstevel@tonic-gate /* Parameters:  atype(I)   - type of network address update to perform      */
59937c478bd9Sstevel@tonic-gate /*              sin(I)     - pointer to source of address information       */
59947c478bd9Sstevel@tonic-gate /*              mask(I)    - pointer to source of netmask information       */
59957c478bd9Sstevel@tonic-gate /*              inp(I)     - pointer to destination address store           */
59967c478bd9Sstevel@tonic-gate /*              inpmask(I) - pointer to destination netmask store           */
59977c478bd9Sstevel@tonic-gate /*                                                                          */
59987c478bd9Sstevel@tonic-gate /* Given a type of network address update (atype) to perform, copy          */
59997c478bd9Sstevel@tonic-gate /* information from sin/mask into inp/inpmask.  If ipnmask is NULL then no  */
60007c478bd9Sstevel@tonic-gate /* netmask update is performed unless FRI_NETMASKED is passed as atype, in  */
60017c478bd9Sstevel@tonic-gate /* which case the operation fails.  For all values of atype other than      */
60027c478bd9Sstevel@tonic-gate /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s  */
60037c478bd9Sstevel@tonic-gate /* value.                                                                   */
60047c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
60057c478bd9Sstevel@tonic-gate int fr_ifpfillv4addr(atype, sin, mask, inp, inpmask)
60067c478bd9Sstevel@tonic-gate int atype;
60077c478bd9Sstevel@tonic-gate struct sockaddr_in *sin, *mask;
60087c478bd9Sstevel@tonic-gate struct in_addr *inp, *inpmask;
60097c478bd9Sstevel@tonic-gate {
60107c478bd9Sstevel@tonic-gate 	if (inpmask != NULL && atype != FRI_NETMASKED)
60117c478bd9Sstevel@tonic-gate 		inpmask->s_addr = 0xffffffff;
60127c478bd9Sstevel@tonic-gate 
60137c478bd9Sstevel@tonic-gate 	if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
60147c478bd9Sstevel@tonic-gate 		if (atype == FRI_NETMASKED) {
60157c478bd9Sstevel@tonic-gate 			if (inpmask == NULL)
60167c478bd9Sstevel@tonic-gate 				return -1;
60177c478bd9Sstevel@tonic-gate 			inpmask->s_addr = mask->sin_addr.s_addr;
60187c478bd9Sstevel@tonic-gate 		}
60197c478bd9Sstevel@tonic-gate 		inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr;
60207c478bd9Sstevel@tonic-gate 	} else {
60217c478bd9Sstevel@tonic-gate 		inp->s_addr = sin->sin_addr.s_addr;
60227c478bd9Sstevel@tonic-gate 	}
60237c478bd9Sstevel@tonic-gate 	return 0;
60247c478bd9Sstevel@tonic-gate }
60257c478bd9Sstevel@tonic-gate 
60267c478bd9Sstevel@tonic-gate 
60277c478bd9Sstevel@tonic-gate #ifdef	USE_INET6
60287c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
60297c478bd9Sstevel@tonic-gate /* Function:    fr_ifpfillv6addr                                            */
60307c478bd9Sstevel@tonic-gate /* Returns:     int     - 0 = address update, -1 = address not updated      */
60317c478bd9Sstevel@tonic-gate /* Parameters:  atype(I)   - type of network address update to perform      */
60327c478bd9Sstevel@tonic-gate /*              sin(I)     - pointer to source of address information       */
60337c478bd9Sstevel@tonic-gate /*              mask(I)    - pointer to source of netmask information       */
60347c478bd9Sstevel@tonic-gate /*              inp(I)     - pointer to destination address store           */
60357c478bd9Sstevel@tonic-gate /*              inpmask(I) - pointer to destination netmask store           */
60367c478bd9Sstevel@tonic-gate /*                                                                          */
60377c478bd9Sstevel@tonic-gate /* Given a type of network address update (atype) to perform, copy          */
60387c478bd9Sstevel@tonic-gate /* information from sin/mask into inp/inpmask.  If ipnmask is NULL then no  */
60397c478bd9Sstevel@tonic-gate /* netmask update is performed unless FRI_NETMASKED is passed as atype, in  */
60407c478bd9Sstevel@tonic-gate /* which case the operation fails.  For all values of atype other than      */
60417c478bd9Sstevel@tonic-gate /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s  */
60427c478bd9Sstevel@tonic-gate /* value.                                                                   */
60437c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
60447c478bd9Sstevel@tonic-gate int fr_ifpfillv6addr(atype, sin, mask, inp, inpmask)
60457c478bd9Sstevel@tonic-gate int atype;
60467c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin, *mask;
60477c478bd9Sstevel@tonic-gate struct in_addr *inp, *inpmask;
60487c478bd9Sstevel@tonic-gate {
60497c478bd9Sstevel@tonic-gate 	i6addr_t *src, *dst, *and, *dmask;
60507c478bd9Sstevel@tonic-gate 
60517c478bd9Sstevel@tonic-gate 	src = (i6addr_t *)&sin->sin6_addr;
60527c478bd9Sstevel@tonic-gate 	and = (i6addr_t *)&mask->sin6_addr;
60537c478bd9Sstevel@tonic-gate 	dst = (i6addr_t *)inp;
60547c478bd9Sstevel@tonic-gate 	dmask = (i6addr_t *)inpmask;
60557c478bd9Sstevel@tonic-gate 
60567c478bd9Sstevel@tonic-gate 	if (inpmask != NULL && atype != FRI_NETMASKED) {
60577c478bd9Sstevel@tonic-gate 		dmask->i6[0] = 0xffffffff;
60587c478bd9Sstevel@tonic-gate 		dmask->i6[1] = 0xffffffff;
60597c478bd9Sstevel@tonic-gate 		dmask->i6[2] = 0xffffffff;
60607c478bd9Sstevel@tonic-gate 		dmask->i6[3] = 0xffffffff;
60617c478bd9Sstevel@tonic-gate 	}
60627c478bd9Sstevel@tonic-gate 
60637c478bd9Sstevel@tonic-gate 	if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
60647c478bd9Sstevel@tonic-gate 		if (atype == FRI_NETMASKED) {
60657c478bd9Sstevel@tonic-gate 			if (inpmask == NULL)
60667c478bd9Sstevel@tonic-gate 				return -1;
60677c478bd9Sstevel@tonic-gate 			dmask->i6[0] = and->i6[0];
60687c478bd9Sstevel@tonic-gate 			dmask->i6[1] = and->i6[1];
60697c478bd9Sstevel@tonic-gate 			dmask->i6[2] = and->i6[2];
60707c478bd9Sstevel@tonic-gate 			dmask->i6[3] = and->i6[3];
60717c478bd9Sstevel@tonic-gate 		}
60727c478bd9Sstevel@tonic-gate 
60737c478bd9Sstevel@tonic-gate 		dst->i6[0] = src->i6[0] & and->i6[0];
60747c478bd9Sstevel@tonic-gate 		dst->i6[1] = src->i6[1] & and->i6[1];
60757c478bd9Sstevel@tonic-gate 		dst->i6[2] = src->i6[2] & and->i6[2];
60767c478bd9Sstevel@tonic-gate 		dst->i6[3] = src->i6[3] & and->i6[3];
60777c478bd9Sstevel@tonic-gate 	} else {
60787c478bd9Sstevel@tonic-gate 		dst->i6[0] = src->i6[0];
60797c478bd9Sstevel@tonic-gate 		dst->i6[1] = src->i6[1];
60807c478bd9Sstevel@tonic-gate 		dst->i6[2] = src->i6[2];
60817c478bd9Sstevel@tonic-gate 		dst->i6[3] = src->i6[3];
60827c478bd9Sstevel@tonic-gate 	}
60837c478bd9Sstevel@tonic-gate 	return 0;
60847c478bd9Sstevel@tonic-gate }
60857c478bd9Sstevel@tonic-gate #endif
60867c478bd9Sstevel@tonic-gate 
60877c478bd9Sstevel@tonic-gate 
60887c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
60897c478bd9Sstevel@tonic-gate /* Function:    fr_matchtag                                                 */
60907c478bd9Sstevel@tonic-gate /* Returns:     0 == mismatch, 1 == match.                                  */
60917c478bd9Sstevel@tonic-gate /* Parameters:  tag1(I) - pointer to first tag to compare                   */
60927c478bd9Sstevel@tonic-gate /*              tag2(I) - pointer to second tag to compare                  */
60937c478bd9Sstevel@tonic-gate /*                                                                          */
60947c478bd9Sstevel@tonic-gate /* Returns true (non-zero) or false(0) if the two tag structures can be     */
60957c478bd9Sstevel@tonic-gate /* considered to be a match or not match, respectively.  The tag is 16      */
60967c478bd9Sstevel@tonic-gate /* bytes long (16 characters) but that is overlayed with 4 32bit ints so    */
60977c478bd9Sstevel@tonic-gate /* compare the ints instead, for speed. tag1 is the master of the           */
60987c478bd9Sstevel@tonic-gate /* comparison.  This function should only be called with both tag1 and tag2 */
60997c478bd9Sstevel@tonic-gate /* as non-NULL pointers.                                                    */
61007c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
61017c478bd9Sstevel@tonic-gate int fr_matchtag(tag1, tag2)
61027c478bd9Sstevel@tonic-gate ipftag_t *tag1, *tag2;
61037c478bd9Sstevel@tonic-gate {
61047c478bd9Sstevel@tonic-gate 	if (tag1 == tag2)
61057c478bd9Sstevel@tonic-gate 		return 1;
61067c478bd9Sstevel@tonic-gate 
61077c478bd9Sstevel@tonic-gate 	if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0))
61087c478bd9Sstevel@tonic-gate 		return 1;
61097c478bd9Sstevel@tonic-gate 
61107c478bd9Sstevel@tonic-gate 	if ((tag1->ipt_num[0] == tag2->ipt_num[0]) &&
61117c478bd9Sstevel@tonic-gate 	    (tag1->ipt_num[1] == tag2->ipt_num[1]) &&
61127c478bd9Sstevel@tonic-gate 	    (tag1->ipt_num[2] == tag2->ipt_num[2]) &&
61137c478bd9Sstevel@tonic-gate 	    (tag1->ipt_num[3] == tag2->ipt_num[3]))
61147c478bd9Sstevel@tonic-gate 		return 1;
61157c478bd9Sstevel@tonic-gate 	return 0;
61167c478bd9Sstevel@tonic-gate }
61177c478bd9Sstevel@tonic-gate 
61187c478bd9Sstevel@tonic-gate 
61197c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
6120ab25eeb5Syz /* Function:    fr_coalesce                                                 */
6121ab25eeb5Syz /* Returns:     1 == success, -1 == failure, 0 == no change                 */
6122ab25eeb5Syz /* Parameters:  fin(I) - pointer to packet information                      */
61237c478bd9Sstevel@tonic-gate /*                                                                          */
6124ab25eeb5Syz /* Attempt to get all of the packet data into a single, contiguous buffer.  */
6125ab25eeb5Syz /* If this call returns a failure then the buffers have also been freed.    */
6126ab25eeb5Syz /* ------------------------------------------------------------------------ */
6127ab25eeb5Syz int fr_coalesce(fin)
61287c478bd9Sstevel@tonic-gate fr_info_t *fin;
61297c478bd9Sstevel@tonic-gate {
6130f4b3ec61Sdh 	ipf_stack_t *ifs = fin->fin_ifs;
61317c478bd9Sstevel@tonic-gate 	if ((fin->fin_flx & FI_COALESCE) != 0)
6132ab25eeb5Syz 		return 1;
61337c478bd9Sstevel@tonic-gate 
6134ab25eeb5Syz 	/*
6135ab25eeb5Syz 	 * If the mbuf pointers indicate that there is no mbuf to work with,
6136ab25eeb5Syz 	 * return but do not indicate success or failure.
6137ab25eeb5Syz 	 */
6138ab25eeb5Syz 	if (fin->fin_m == NULL || fin->fin_mp == NULL)
6139ab25eeb5Syz 		return 0;
61407c478bd9Sstevel@tonic-gate 
6141ab25eeb5Syz #if defined(_KERNEL)
6142ab25eeb5Syz 	if (fr_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) {
6143cbded9aeSdr 		IPF_BUMP(ifs->ifs_fr_badcoalesces[fin->fin_out]);
61447c478bd9Sstevel@tonic-gate # ifdef MENTAT
6145ab25eeb5Syz 		FREE_MB_T(*fin->fin_mp);
6146ab25eeb5Syz # endif
6147ab25eeb5Syz 		*fin->fin_mp = NULL;
6148ab25eeb5Syz 		fin->fin_m = NULL;
6149ab25eeb5Syz 		return -1;
61507c478bd9Sstevel@tonic-gate 	}
6151ab25eeb5Syz #else
6152ab25eeb5Syz 	fin = fin;	/* LINT */
6153ab25eeb5Syz #endif
6154ab25eeb5Syz 	return 1;
61557c478bd9Sstevel@tonic-gate }
61567c478bd9Sstevel@tonic-gate 
61577c478bd9Sstevel@tonic-gate 
61587c478bd9Sstevel@tonic-gate /*
61597c478bd9Sstevel@tonic-gate  * The following table lists all of the tunable variables that can be
6160f4b3ec61Sdh  * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXT.  The format of each row
61617c478bd9Sstevel@tonic-gate  * in the table below is as follows:
61627c478bd9Sstevel@tonic-gate  *
61637c478bd9Sstevel@tonic-gate  * pointer to value, name of value, minimum, maximum, size of the value's
61647c478bd9Sstevel@tonic-gate  *     container, value attribute flags
61657c478bd9Sstevel@tonic-gate  *
61667c478bd9Sstevel@tonic-gate  * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED
61677c478bd9Sstevel@tonic-gate  * means the value can only be written to when IPFilter is loaded but disabled.
61687c478bd9Sstevel@tonic-gate  * The obvious implication is if neither of these are set then the value can be
61697c478bd9Sstevel@tonic-gate  * changed at any time without harm.
61707c478bd9Sstevel@tonic-gate  */
6171f4b3ec61Sdh ipftuneable_t lcl_ipf_tuneables[] = {
61727c478bd9Sstevel@tonic-gate 	/* filtering */
6173f4b3ec61Sdh 	{ { NULL },	"fr_flags",		0,	0xffffffff,
6174f4b3ec61Sdh 			0,		0 },
6175f4b3ec61Sdh 	{ { NULL },	"fr_active",		0,	0,
6176f4b3ec61Sdh 			0,		IPFT_RDONLY },
6177f4b3ec61Sdh 	{ { NULL },	"fr_control_forwarding",	0, 1,
6178f4b3ec61Sdh 			0,		0 },
6179f4b3ec61Sdh 	{ { NULL },	"fr_update_ipid",	0,	1,
6180f4b3ec61Sdh 			0,		0 },
6181f4b3ec61Sdh 	{ { NULL },	"fr_chksrc",		0,	1,
6182f4b3ec61Sdh 			0,		0 },
6183f4b3ec61Sdh 	{ { NULL },	"fr_minttl",		0,	1,
6184f4b3ec61Sdh 			0,		0 },
6185f4b3ec61Sdh 	{ { NULL }, 	"fr_icmpminfragmtu",	0,	1,
6186f4b3ec61Sdh 			0,		0 },
6187f4b3ec61Sdh 	{ { NULL },		"fr_pass",		0,	0xffffffff,
6188f4b3ec61Sdh 			0,		0 },
6189381a2a9aSdr #if SOLARIS2 >= 10
6190f4b3ec61Sdh 	{ { NULL },	"ipf_loopback",		0,	1,
6191f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6192381a2a9aSdr #endif
61937c478bd9Sstevel@tonic-gate 	/* state */
6194f4b3ec61Sdh 	{ { NULL }, "fr_tcpidletimeout",	1,	0x7fffffff,
6195f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6196f4b3ec61Sdh 	{ { NULL },	"fr_tcpclosewait",	1,	0x7fffffff,
6197f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6198f4b3ec61Sdh 	{ { NULL },	"fr_tcplastack",	1,	0x7fffffff,
6199f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6200f4b3ec61Sdh 	{ { NULL },	"fr_tcptimeout",	1,	0x7fffffff,
6201f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6202f4b3ec61Sdh 	{ { NULL },	"fr_tcpclosed",		1,	0x7fffffff,
6203f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6204f4b3ec61Sdh 	{ { NULL },	"fr_tcphalfclosed",	1,	0x7fffffff,
6205f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6206f4b3ec61Sdh 	{ { NULL },	"fr_udptimeout",	1,	0x7fffffff,
6207f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6208f4b3ec61Sdh 	{ { NULL }, "fr_udpacktimeout",	1,	0x7fffffff,
6209f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6210f4b3ec61Sdh 	{ { NULL },	"fr_icmptimeout",	1,	0x7fffffff,
6211f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6212f4b3ec61Sdh 	{ { NULL }, "fr_icmpacktimeout",	1,	0x7fffffff,
6213f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6214f4b3ec61Sdh 	{ { NULL }, "fr_iptimeout",		1,	0x7fffffff,
6215f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6216f4b3ec61Sdh 	{ { NULL },	"fr_statemax",		1,	0x7fffffff,
6217f4b3ec61Sdh 			0,		0 },
6218f4b3ec61Sdh 	{ { NULL },	"fr_statesize",		1,	0x7fffffff,
6219f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6220f4b3ec61Sdh 	{ { NULL },	"fr_state_lock",	0,	1,
6221f4b3ec61Sdh 			0,		IPFT_RDONLY },
6222f4b3ec61Sdh 	{ { NULL }, "fr_state_maxbucket", 1,	0x7fffffff,
6223f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6224f4b3ec61Sdh 	{ { NULL }, "fr_state_maxbucket_reset",	0, 1,
6225f4b3ec61Sdh 			0, IPFT_WRDISABLED },
6226f4b3ec61Sdh 	{ { NULL },	"ipstate_logging",	0,	1,
6227f4b3ec61Sdh 			0,	0 },
6228ea8244dcSJohn Ojemann 	{ { NULL },	"state_flush_level_hi",	1,	100,
6229ea8244dcSJohn Ojemann 			0,		0 },
6230ea8244dcSJohn Ojemann 	{ { NULL },	"state_flush_level_lo",	1,	100,
6231ea8244dcSJohn Ojemann 			0,		0 },
62327c478bd9Sstevel@tonic-gate 	/* nat */
6233f4b3ec61Sdh 	{ { NULL },		"fr_nat_lock",		0,	1,
6234f4b3ec61Sdh 			0,		IPFT_RDONLY },
6235f4b3ec61Sdh 	{ { NULL },	"ipf_nattable_sz",	1,	0x7fffffff,
6236f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6237f4b3ec61Sdh 	{ { NULL }, "ipf_nattable_max",	1,	0x7fffffff,
6238f4b3ec61Sdh 			0,	0 },
6239f4b3ec61Sdh 	{ { NULL },	"ipf_natrules_sz",	1,	0x7fffffff,
6240f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6241f4b3ec61Sdh 	{ { NULL },	"ipf_rdrrules_sz",	1,	0x7fffffff,
6242f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6243f4b3ec61Sdh 	{ { NULL },	"ipf_hostmap_sz",	1,	0x7fffffff,
6244f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6245f4b3ec61Sdh 	{ { NULL }, "fr_nat_maxbucket",	1,	0x7fffffff,
6246f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6247f4b3ec61Sdh 	{ { NULL },	"fr_nat_maxbucket_reset",	0, 1,
6248f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6249f4b3ec61Sdh 	{ { NULL },		"nat_logging",		0,	1,
6250f4b3ec61Sdh 			0,		0 },
6251f4b3ec61Sdh 	{ { NULL },	"fr_defnatage",		1,	0x7fffffff,
6252f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6253f4b3ec61Sdh 	{ { NULL },	"fr_defnatipage",	1,	0x7fffffff,
6254f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6255f4b3ec61Sdh 	{ { NULL }, "fr_defnaticmpage",	1,	0x7fffffff,
6256f4b3ec61Sdh 			0,	IPFT_WRDISABLED },
6257ea8244dcSJohn Ojemann 	{ { NULL },	"nat_flush_level_hi",	1,	100,
62583805c50fSan 			0,		0 },
6259ea8244dcSJohn Ojemann 	{ { NULL },	"nat_flush_level_lo",	1,	100,
62603805c50fSan 			0,		0 },
62617c478bd9Sstevel@tonic-gate 	/* frag */
6262f4b3ec61Sdh 	{ { NULL },	"ipfr_size",		1,	0x7fffffff,
6263f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6264f4b3ec61Sdh 	{ { NULL },	"fr_ipfrttl",		1,	0x7fffffff,
6265f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
62667c478bd9Sstevel@tonic-gate #ifdef IPFILTER_LOG
62677c478bd9Sstevel@tonic-gate 	/* log */
6268f4b3ec61Sdh 	{ { NULL },	"ipl_suppress",		0,	1,
6269f4b3ec61Sdh 			0,		0 },
6270f4b3ec61Sdh 	{ { NULL },	"ipl_buffer_sz",	0,	0,
6271f4b3ec61Sdh 			0,		IPFT_RDONLY },
6272f4b3ec61Sdh 	{ { NULL },	"ipl_logmax",		0,	0x7fffffff,
6273f4b3ec61Sdh 			0,		IPFT_WRDISABLED },
6274f4b3ec61Sdh 	{ { NULL },	"ipl_logall",		0,	1,
6275f4b3ec61Sdh 			0,		0 },
6276f4b3ec61Sdh 	{ { NULL },	"ipl_logsize",		0,	0x80000,
6277f4b3ec61Sdh 			0,		0 },
62787c478bd9Sstevel@tonic-gate #endif
62797c478bd9Sstevel@tonic-gate 	{ { NULL },		NULL,			0,	0 }
62807c478bd9Sstevel@tonic-gate };
62817c478bd9Sstevel@tonic-gate 
6282f4b3ec61Sdh static ipftuneable_t *
6283f4b3ec61Sdh tune_lookup(ipf_stack_t *ifs, char *name)
6284f4b3ec61Sdh {
6285f4b3ec61Sdh     int i;
6286ab25eeb5Syz 
6287f4b3ec61Sdh     for (i = 0; ifs->ifs_ipf_tuneables[i].ipft_name != NULL; i++) {
6288f4b3ec61Sdh 	if (strcmp(ifs->ifs_ipf_tuneables[i].ipft_name, name) == 0)
6289f4b3ec61Sdh 	    return (&ifs->ifs_ipf_tuneables[i]);
6290f4b3ec61Sdh     }
6291f4b3ec61Sdh     return (NULL);
6292f4b3ec61Sdh }
6293f4b3ec61Sdh 
6294f4b3ec61Sdh #ifdef _KERNEL
6295f4b3ec61Sdh extern dev_info_t *ipf_dev_info;
6296f4b3ec61Sdh extern int ipf_property_update __P((dev_info_t *, ipf_stack_t *));
6297f4b3ec61Sdh #endif
62988128a42dSan 
62998128a42dSan /* -------------------------------------------------------------------- */
63008128a42dSan /* Function:	ipftuneable_setdefs()					*/
63018128a42dSan /* Returns:		void						*/
63028128a42dSan /* Parameters:	ifs - pointer to newly allocated IPF instance		*/
63038128a42dSan /*				assigned to	IP instance		*/
63048128a42dSan /*									*/
63058128a42dSan /* Function initializes IPF instance variables. Function is invoked	*/
63068128a42dSan /* from	ipftuneable_alloc(). ipftuneable_alloc() is called only one	*/
63078128a42dSan /* time during IP instance lifetime - at the time of IP instance	*/
63088128a42dSan /* creation. Anytime IP	instance is being created new private IPF	*/
63098128a42dSan /* instance is allocated and assigned to it. The moment of IP 		*/
63108128a42dSan /* instance creation is the right time to initialize those IPF 		*/
63118128a42dSan /* variables.								*/
63128128a42dSan /*									*/
63138128a42dSan /* -------------------------------------------------------------------- */
63148128a42dSan static void ipftuneable_setdefs(ipf_stack_t *ifs)
63158128a42dSan {
63168128a42dSan 	ifs->ifs_ipfr_size = IPFT_SIZE;
63178128a42dSan 	ifs->ifs_fr_ipfrttl = 120;	/* 60 seconds */
63188128a42dSan 
63198128a42dSan 	/* it comes from fr_authinit() in IPF auth */
63208128a42dSan 	ifs->ifs_fr_authsize = FR_NUMAUTH;
63218128a42dSan 	ifs->ifs_fr_defaultauthage = 600;
63228128a42dSan 
63238128a42dSan 	/* it comes from fr_stateinit() in IPF state */
63248128a42dSan 	ifs->ifs_fr_tcpidletimeout = IPF_TTLVAL(3600 * 24 * 5);	/* five days */
63258128a42dSan 	ifs->ifs_fr_tcpclosewait = IPF_TTLVAL(TCP_MSL);
63268128a42dSan 	ifs->ifs_fr_tcplastack = IPF_TTLVAL(TCP_MSL);
63278128a42dSan 	ifs->ifs_fr_tcptimeout = IPF_TTLVAL(TCP_MSL);
63288128a42dSan 	ifs->ifs_fr_tcpclosed = IPF_TTLVAL(60);
63298128a42dSan 	ifs->ifs_fr_tcphalfclosed = IPF_TTLVAL(2 * 3600);	/* 2 hours */
63308128a42dSan 	ifs->ifs_fr_udptimeout = IPF_TTLVAL(120);
63318128a42dSan 	ifs->ifs_fr_udpacktimeout = IPF_TTLVAL(12);
63328128a42dSan 	ifs->ifs_fr_icmptimeout = IPF_TTLVAL(60);
63338128a42dSan 	ifs->ifs_fr_icmpacktimeout = IPF_TTLVAL(6);
63348128a42dSan 	ifs->ifs_fr_iptimeout = IPF_TTLVAL(60);
63358128a42dSan 	ifs->ifs_fr_statemax = IPSTATE_MAX;
63368128a42dSan 	ifs->ifs_fr_statesize = IPSTATE_SIZE;
63378128a42dSan 	ifs->ifs_fr_state_maxbucket_reset = 1;
6338ea8244dcSJohn Ojemann 	ifs->ifs_state_flush_level_hi = ST_FLUSH_HI;
6339ea8244dcSJohn Ojemann 	ifs->ifs_state_flush_level_lo = ST_FLUSH_LO;
63408128a42dSan 
63418128a42dSan 	/* it comes from fr_natinit() in ipnat */
63428128a42dSan 	ifs->ifs_ipf_nattable_sz = NAT_TABLE_SZ;
63438128a42dSan 	ifs->ifs_ipf_nattable_max = NAT_TABLE_MAX;
63448128a42dSan 	ifs->ifs_ipf_natrules_sz = NAT_SIZE;
63458128a42dSan 	ifs->ifs_ipf_rdrrules_sz = RDR_SIZE;
63468128a42dSan 	ifs->ifs_ipf_hostmap_sz = HOSTMAP_SIZE;
63478128a42dSan 	ifs->ifs_fr_nat_maxbucket_reset = 1;
63488128a42dSan 	ifs->ifs_fr_defnatage = DEF_NAT_AGE;
63498128a42dSan 	ifs->ifs_fr_defnatipage = 120;		/* 60 seconds */
63508128a42dSan 	ifs->ifs_fr_defnaticmpage = 6;		/* 3 seconds */
6351ea8244dcSJohn Ojemann 	ifs->ifs_nat_flush_level_hi = NAT_FLUSH_HI;
6352ea8244dcSJohn Ojemann 	ifs->ifs_nat_flush_level_lo = NAT_FLUSH_LO;
63538128a42dSan 
63548128a42dSan #ifdef IPFILTER_LOG
63558128a42dSan 	/* it comes from fr_loginit() in IPF log */
63568128a42dSan 	ifs->ifs_ipl_suppress = 1;
63578128a42dSan 	ifs->ifs_ipl_logmax = IPL_LOGMAX;
63588128a42dSan 	ifs->ifs_ipl_logsize = IPFILTER_LOGSIZE;
63598128a42dSan 
63608128a42dSan 	/* from fr_natinit() */
63618128a42dSan 	ifs->ifs_nat_logging = 1;
63628128a42dSan 
63638128a42dSan 	/* from fr_stateinit() */
63648128a42dSan 	ifs->ifs_ipstate_logging = 1;
63658128a42dSan #else
63668128a42dSan 	/* from fr_natinit() */
63678128a42dSan 	ifs->ifs_nat_logging = 0;
63688128a42dSan 
63698128a42dSan 	/* from fr_stateinit() */
63708128a42dSan 	ifs->ifs_ipstate_logging = 0;
63718128a42dSan #endif
6372fd636508Szf 	ifs->ifs_ipf_loopback = 0;
63738128a42dSan 
63748128a42dSan }
6375f4b3ec61Sdh /*
6376f4b3ec61Sdh  * Allocate a per-stack tuneable and copy in the names. Then
6377f4b3ec61Sdh  * set it to point to each of the per-stack tunables.
6378f4b3ec61Sdh  */
6379f4b3ec61Sdh void
6380f4b3ec61Sdh ipftuneable_alloc(ipf_stack_t *ifs)
6381f4b3ec61Sdh {
6382f4b3ec61Sdh     ipftuneable_t *item;
6383f4b3ec61Sdh 
6384f4b3ec61Sdh     KMALLOCS(ifs->ifs_ipf_tuneables, ipftuneable_t *,
6385f4b3ec61Sdh 	sizeof (lcl_ipf_tuneables));
6386f4b3ec61Sdh     bcopy(lcl_ipf_tuneables, ifs->ifs_ipf_tuneables,
6387f4b3ec61Sdh 	sizeof (lcl_ipf_tuneables));
6388f4b3ec61Sdh 
6389f4b3ec61Sdh #define TUNE_SET(_ifs, _name, _field)			\
6390f4b3ec61Sdh     item = tune_lookup((_ifs), (_name));		\
6391f4b3ec61Sdh     if (item != NULL) {					\
6392f4b3ec61Sdh 	item->ipft_una.ipftp_int = (unsigned int *)&((_ifs)->_field);	\
6393f4b3ec61Sdh 	item->ipft_sz = sizeof ((_ifs)->_field);	\
6394f4b3ec61Sdh     }
6395f4b3ec61Sdh 
6396f4b3ec61Sdh     TUNE_SET(ifs, "fr_flags", ifs_fr_flags);
6397f4b3ec61Sdh     TUNE_SET(ifs, "fr_active", ifs_fr_active);
6398f4b3ec61Sdh     TUNE_SET(ifs, "fr_control_forwarding", ifs_fr_control_forwarding);
6399f4b3ec61Sdh     TUNE_SET(ifs, "fr_update_ipid", ifs_fr_update_ipid);
6400f4b3ec61Sdh     TUNE_SET(ifs, "fr_chksrc", ifs_fr_chksrc);
6401f4b3ec61Sdh     TUNE_SET(ifs, "fr_minttl", ifs_fr_minttl);
6402f4b3ec61Sdh     TUNE_SET(ifs, "fr_icmpminfragmtu", ifs_fr_icmpminfragmtu);
6403f4b3ec61Sdh     TUNE_SET(ifs, "fr_pass", ifs_fr_pass);
6404f4b3ec61Sdh     TUNE_SET(ifs, "fr_tcpidletimeout", ifs_fr_tcpidletimeout);
6405f4b3ec61Sdh     TUNE_SET(ifs, "fr_tcpclosewait", ifs_fr_tcpclosewait);
6406f4b3ec61Sdh     TUNE_SET(ifs, "fr_tcplastack", ifs_fr_tcplastack);
6407f4b3ec61Sdh     TUNE_SET(ifs, "fr_tcptimeout", ifs_fr_tcptimeout);
6408f4b3ec61Sdh     TUNE_SET(ifs, "fr_tcpclosed", ifs_fr_tcpclosed);
6409f4b3ec61Sdh     TUNE_SET(ifs, "fr_tcphalfclosed", ifs_fr_tcphalfclosed);
6410f4b3ec61Sdh     TUNE_SET(ifs, "fr_udptimeout", ifs_fr_udptimeout);
6411f4b3ec61Sdh     TUNE_SET(ifs, "fr_udpacktimeout", ifs_fr_udpacktimeout);
6412f4b3ec61Sdh     TUNE_SET(ifs, "fr_icmptimeout", ifs_fr_icmptimeout);
6413f4b3ec61Sdh     TUNE_SET(ifs, "fr_icmpacktimeout", ifs_fr_icmpacktimeout);
6414f4b3ec61Sdh     TUNE_SET(ifs, "fr_iptimeout", ifs_fr_iptimeout);
6415f4b3ec61Sdh     TUNE_SET(ifs, "fr_statemax", ifs_fr_statemax);
6416f4b3ec61Sdh     TUNE_SET(ifs, "fr_statesize", ifs_fr_statesize);
6417f4b3ec61Sdh     TUNE_SET(ifs, "fr_state_lock", ifs_fr_state_lock);
6418f4b3ec61Sdh     TUNE_SET(ifs, "fr_state_maxbucket", ifs_fr_state_maxbucket);
6419f4b3ec61Sdh     TUNE_SET(ifs, "fr_state_maxbucket_reset", ifs_fr_state_maxbucket_reset);
6420f4b3ec61Sdh     TUNE_SET(ifs, "ipstate_logging", ifs_ipstate_logging);
6421f4b3ec61Sdh     TUNE_SET(ifs, "fr_nat_lock", ifs_fr_nat_lock);
6422f4b3ec61Sdh     TUNE_SET(ifs, "ipf_nattable_sz", ifs_ipf_nattable_sz);
6423f4b3ec61Sdh     TUNE_SET(ifs, "ipf_nattable_max", ifs_ipf_nattable_max);
6424f4b3ec61Sdh     TUNE_SET(ifs, "ipf_natrules_sz", ifs_ipf_natrules_sz);
6425f4b3ec61Sdh     TUNE_SET(ifs, "ipf_rdrrules_sz", ifs_ipf_rdrrules_sz);
6426f4b3ec61Sdh     TUNE_SET(ifs, "ipf_hostmap_sz", ifs_ipf_hostmap_sz);
6427f4b3ec61Sdh     TUNE_SET(ifs, "fr_nat_maxbucket", ifs_fr_nat_maxbucket);
6428f4b3ec61Sdh     TUNE_SET(ifs, "fr_nat_maxbucket_reset", ifs_fr_nat_maxbucket_reset);
6429f4b3ec61Sdh     TUNE_SET(ifs, "nat_logging", ifs_nat_logging);
6430f4b3ec61Sdh     TUNE_SET(ifs, "fr_defnatage", ifs_fr_defnatage);
6431fd636508Szf     TUNE_SET(ifs, "fr_defnatipage", ifs_fr_defnatipage);
6432f4b3ec61Sdh     TUNE_SET(ifs, "fr_defnaticmpage", ifs_fr_defnaticmpage);
6433ea8244dcSJohn Ojemann     TUNE_SET(ifs, "nat_flush_level_hi", ifs_nat_flush_level_hi);
6434ea8244dcSJohn Ojemann     TUNE_SET(ifs, "nat_flush_level_lo", ifs_nat_flush_level_lo);
6435ea8244dcSJohn Ojemann     TUNE_SET(ifs, "state_flush_level_hi", ifs_state_flush_level_hi);
6436ea8244dcSJohn Ojemann     TUNE_SET(ifs, "state_flush_level_lo", ifs_state_flush_level_lo);
6437f4b3ec61Sdh     TUNE_SET(ifs, "ipfr_size", ifs_ipfr_size);
6438f4b3ec61Sdh     TUNE_SET(ifs, "fr_ipfrttl", ifs_fr_ipfrttl);
6439fd636508Szf     TUNE_SET(ifs, "ipf_loopback", ifs_ipf_loopback);
6440f4b3ec61Sdh #ifdef IPFILTER_LOG
6441f4b3ec61Sdh     TUNE_SET(ifs, "ipl_suppress", ifs_ipl_suppress);
6442f4b3ec61Sdh     TUNE_SET(ifs, "ipl_buffer_sz", ifs_ipl_buffer_sz);
6443f4b3ec61Sdh     TUNE_SET(ifs, "ipl_logmax", ifs_ipl_logmax);
6444f4b3ec61Sdh     TUNE_SET(ifs, "ipl_logall", ifs_ipl_logall);
6445f4b3ec61Sdh     TUNE_SET(ifs, "ipl_logsize", ifs_ipl_logsize);
6446f4b3ec61Sdh #endif
6447f4b3ec61Sdh #undef TUNE_SET
6448f4b3ec61Sdh 
64498128a42dSan 	ipftuneable_setdefs(ifs);
64508128a42dSan 
6451f4b3ec61Sdh #ifdef _KERNEL
6452f4b3ec61Sdh     (void) ipf_property_update(ipf_dev_info, ifs);
6453f4b3ec61Sdh #endif
6454f4b3ec61Sdh }
6455f4b3ec61Sdh 
6456f4b3ec61Sdh void
6457f4b3ec61Sdh ipftuneable_free(ipf_stack_t *ifs)
6458f4b3ec61Sdh {
6459f4b3ec61Sdh 	KFREES(ifs->ifs_ipf_tuneables, sizeof (lcl_ipf_tuneables));
6460f4b3ec61Sdh 	ifs->ifs_ipf_tuneables = NULL;
6461f4b3ec61Sdh }
6462ab25eeb5Syz 
6463ab25eeb5Syz /* ------------------------------------------------------------------------ */
6464ab25eeb5Syz /* Function:    fr_findtunebycookie                                         */
6465ab25eeb5Syz /* Returns:     NULL = search failed, else pointer to tune struct           */
6466ab25eeb5Syz /* Parameters:  cookie(I) - cookie value to search for amongst tuneables    */
6467ab25eeb5Syz /*              next(O)   - pointer to place to store the cookie for the    */
6468ab25eeb5Syz /*                          "next" tuneable, if it is desired.              */
6469ab25eeb5Syz /*                                                                          */
6470ab25eeb5Syz /* This function is used to walk through all of the existing tunables with  */
6471ab25eeb5Syz /* successive calls.  It searches the known tunables for the one which has  */
6472ab25eeb5Syz /* a matching value for "cookie" - ie its address.  When returning a match, */
6473ab25eeb5Syz /* the next one to be found may be returned inside next.                    */
6474ab25eeb5Syz /* ------------------------------------------------------------------------ */
6475f4b3ec61Sdh static ipftuneable_t *fr_findtunebycookie(cookie, next, ifs)
6476ab25eeb5Syz void *cookie, **next;
6477f4b3ec61Sdh ipf_stack_t * ifs;
6478ab25eeb5Syz {
6479ab25eeb5Syz 	ipftuneable_t *ta, **tap;
6480ab25eeb5Syz 
6481f4b3ec61Sdh 	for (ta = ifs->ifs_ipf_tuneables; ta->ipft_name != NULL; ta++)
6482ab25eeb5Syz 		if (ta == cookie) {
6483ab25eeb5Syz 			if (next != NULL) {
6484ab25eeb5Syz 				/*
6485ab25eeb5Syz 				 * If the next entry in the array has a name
6486ab25eeb5Syz 				 * present, then return a pointer to it for
6487ab25eeb5Syz 				 * where to go next, else return a pointer to
6488ab25eeb5Syz 				 * the dynaminc list as a key to search there
6489ab25eeb5Syz 				 * next.  This facilitates a weak linking of
6490ab25eeb5Syz 				 * the two "lists" together.
6491ab25eeb5Syz 				 */
6492ab25eeb5Syz 				if ((ta + 1)->ipft_name != NULL)
6493ab25eeb5Syz 					*next = ta + 1;
6494ab25eeb5Syz 				else
6495f4b3ec61Sdh 					*next = &ifs->ifs_ipf_tunelist;
6496ab25eeb5Syz 			}
6497ab25eeb5Syz 			return ta;
6498ab25eeb5Syz 		}
6499ab25eeb5Syz 
6500f4b3ec61Sdh 	for (tap = &ifs->ifs_ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next)
6501ab25eeb5Syz 		if (tap == cookie) {
6502ab25eeb5Syz 			if (next != NULL)
6503ab25eeb5Syz 				*next = &ta->ipft_next;
6504ab25eeb5Syz 			return ta;
6505ab25eeb5Syz 		}
6506ab25eeb5Syz 
6507ab25eeb5Syz 	if (next != NULL)
6508ab25eeb5Syz 		*next = NULL;
6509ab25eeb5Syz 	return NULL;
6510ab25eeb5Syz }
6511ab25eeb5Syz 
6512ab25eeb5Syz 
6513ab25eeb5Syz /* ------------------------------------------------------------------------ */
6514ab25eeb5Syz /* Function:    fr_findtunebyname                                           */
6515ab25eeb5Syz /* Returns:     NULL = search failed, else pointer to tune struct           */
6516ab25eeb5Syz /* Parameters:  name(I) - name of the tuneable entry to find.               */
6517ab25eeb5Syz /*                                                                          */
6518ab25eeb5Syz /* Search the static array of tuneables and the list of dynamic tuneables   */
6519ab25eeb5Syz /* for an entry with a matching name.  If we can find one, return a pointer */
6520ab25eeb5Syz /* to the matching structure.                                               */
6521ab25eeb5Syz /* ------------------------------------------------------------------------ */
6522f4b3ec61Sdh static ipftuneable_t *fr_findtunebyname(name, ifs)
6523ab25eeb5Syz const char *name;
6524f4b3ec61Sdh ipf_stack_t *ifs;
6525ab25eeb5Syz {
6526ab25eeb5Syz 	ipftuneable_t *ta;
6527ab25eeb5Syz 
6528f4b3ec61Sdh 	for (ta = ifs->ifs_ipf_tuneables; ta->ipft_name != NULL; ta++)
6529ab25eeb5Syz 		if (!strcmp(ta->ipft_name, name)) {
6530ab25eeb5Syz 			return ta;
6531ab25eeb5Syz 		}
6532ab25eeb5Syz 
6533f4b3ec61Sdh 	for (ta = ifs->ifs_ipf_tunelist; ta != NULL; ta = ta->ipft_next)
6534ab25eeb5Syz 		if (!strcmp(ta->ipft_name, name)) {
6535ab25eeb5Syz 			return ta;
6536ab25eeb5Syz 		}
6537ab25eeb5Syz 
6538ab25eeb5Syz 	return NULL;
6539ab25eeb5Syz }
6540ab25eeb5Syz 
6541ab25eeb5Syz 
6542ab25eeb5Syz /* ------------------------------------------------------------------------ */
6543ab25eeb5Syz /* Function:    fr_addipftune                                               */
6544ab25eeb5Syz /* Returns:     int - 0 == success, else failure                            */
6545ab25eeb5Syz /* Parameters:  newtune - pointer to new tune struct to add to tuneables    */
6546ab25eeb5Syz /*                                                                          */
6547ab25eeb5Syz /* Appends the tune structure pointer to by "newtune" to the end of the     */
6548ab25eeb5Syz /* current list of "dynamic" tuneable parameters.  Once added, the owner    */
6549ab25eeb5Syz /* of the object is not expected to ever change "ipft_next".                */
6550ab25eeb5Syz /* ------------------------------------------------------------------------ */
6551f4b3ec61Sdh int fr_addipftune(newtune, ifs)
6552ab25eeb5Syz ipftuneable_t *newtune;
6553f4b3ec61Sdh ipf_stack_t *ifs;
6554ab25eeb5Syz {
6555ab25eeb5Syz 	ipftuneable_t *ta, **tap;
6556ab25eeb5Syz 
6557f4b3ec61Sdh 	ta = fr_findtunebyname(newtune->ipft_name, ifs);
6558ab25eeb5Syz 	if (ta != NULL)
6559ab25eeb5Syz 		return EEXIST;
6560ab25eeb5Syz 
6561f4b3ec61Sdh 	for (tap = &ifs->ifs_ipf_tunelist; *tap != NULL; tap = &(*tap)->ipft_next)
6562ab25eeb5Syz 		;
6563ab25eeb5Syz 
6564ab25eeb5Syz 	newtune->ipft_next = NULL;
6565ab25eeb5Syz 	*tap = newtune;
6566ab25eeb5Syz 	return 0;
6567ab25eeb5Syz }
6568ab25eeb5Syz 
6569ab25eeb5Syz 
6570ab25eeb5Syz /* ------------------------------------------------------------------------ */
6571ab25eeb5Syz /* Function:    fr_delipftune                                               */
6572ab25eeb5Syz /* Returns:     int - 0 == success, else failure                            */
6573ab25eeb5Syz /* Parameters:  oldtune - pointer to tune struct to remove from the list of */
6574ab25eeb5Syz /*                        current dynamic tuneables                         */
6575ab25eeb5Syz /*                                                                          */
6576ab25eeb5Syz /* Search for the tune structure, by pointer, in the list of those that are */
6577ab25eeb5Syz /* dynamically added at run time.  If found, adjust the list so that this   */
6578ab25eeb5Syz /* structure is no longer part of it.                                       */
6579ab25eeb5Syz /* ------------------------------------------------------------------------ */
6580f4b3ec61Sdh int fr_delipftune(oldtune, ifs)
6581ab25eeb5Syz ipftuneable_t *oldtune;
6582f4b3ec61Sdh ipf_stack_t *ifs;
6583ab25eeb5Syz {
6584ab25eeb5Syz 	ipftuneable_t *ta, **tap;
6585ab25eeb5Syz 
6586f4b3ec61Sdh 	for (tap = &ifs->ifs_ipf_tunelist; (ta = *tap) != NULL; tap = &ta->ipft_next)
6587ab25eeb5Syz 		if (ta == oldtune) {
6588ab25eeb5Syz 			*tap = oldtune->ipft_next;
6589ab25eeb5Syz 			oldtune->ipft_next = NULL;
6590ab25eeb5Syz 			return 0;
6591ab25eeb5Syz 		}
6592ab25eeb5Syz 
6593ab25eeb5Syz 	return ESRCH;
6594ab25eeb5Syz }
6595ab25eeb5Syz 
65967c478bd9Sstevel@tonic-gate 
65977c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
65987c478bd9Sstevel@tonic-gate /* Function:    fr_ipftune                                                  */
65997c478bd9Sstevel@tonic-gate /* Returns:     int - 0 == success, else failure                            */
66007c478bd9Sstevel@tonic-gate /* Parameters:  cmd(I)  - ioctl command number                              */
66017c478bd9Sstevel@tonic-gate /*              data(I) - pointer to ioctl data structure                   */
66027c478bd9Sstevel@tonic-gate /*                                                                          */
66037c478bd9Sstevel@tonic-gate /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET.  These  */
66047c478bd9Sstevel@tonic-gate /* three ioctls provide the means to access and control global variables    */
66057c478bd9Sstevel@tonic-gate /* within IPFilter, allowing (for example) timeouts and table sizes to be   */
66067c478bd9Sstevel@tonic-gate /* changed without rebooting, reloading or recompiling.  The initialisation */
66077c478bd9Sstevel@tonic-gate /* and 'destruction' routines of the various components of ipfilter are all */
66087c478bd9Sstevel@tonic-gate /* each responsible for handling their own values being too big.            */
66097c478bd9Sstevel@tonic-gate /* ------------------------------------------------------------------------ */
6610f4b3ec61Sdh int fr_ipftune(cmd, data, ifs)
6611ab25eeb5Syz ioctlcmd_t cmd;
6612ab25eeb5Syz void *data;
6613f4b3ec61Sdh ipf_stack_t *ifs;
66147c478bd9Sstevel@tonic-gate {
66157c478bd9Sstevel@tonic-gate 	ipftuneable_t *ta;
66167c478bd9Sstevel@tonic-gate 	ipftune_t tu;
66177c478bd9Sstevel@tonic-gate 	void *cookie;
66187c478bd9Sstevel@tonic-gate 	int error;
66197c478bd9Sstevel@tonic-gate 
66207c478bd9Sstevel@tonic-gate 	error = fr_inobj(data, &tu, IPFOBJ_TUNEABLE);
66217c478bd9Sstevel@tonic-gate 	if (error != 0)
66227c478bd9Sstevel@tonic-gate 		return error;
66237c478bd9Sstevel@tonic-gate 
66247c478bd9Sstevel@tonic-gate 	tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
66257c478bd9Sstevel@tonic-gate 	cookie = tu.ipft_cookie;
6626ab25eeb5Syz 	ta = NULL;
66277c478bd9Sstevel@tonic-gate 
66287c478bd9Sstevel@tonic-gate 	switch (cmd)
66297c478bd9Sstevel@tonic-gate 	{
66307c478bd9Sstevel@tonic-gate 	case SIOCIPFGETNEXT :
66317c478bd9Sstevel@tonic-gate 		/*
66327c478bd9Sstevel@tonic-gate 		 * If cookie is non-NULL, assume it to be a pointer to the last
66337c478bd9Sstevel@tonic-gate 		 * entry we looked at, so find it (if possible) and return a
66347c478bd9Sstevel@tonic-gate 		 * pointer to the next one after it.  The last entry in the
66357c478bd9Sstevel@tonic-gate 		 * the table is a NULL entry, so when we get to it, set cookie
66367c478bd9Sstevel@tonic-gate 		 * to NULL and return that, indicating end of list, erstwhile
66377c478bd9Sstevel@tonic-gate 		 * if we come in with cookie set to NULL, we are starting anew
66387c478bd9Sstevel@tonic-gate 		 * at the front of the list.
66397c478bd9Sstevel@tonic-gate 		 */
66407c478bd9Sstevel@tonic-gate 		if (cookie != NULL) {
6641f4b3ec61Sdh 			ta = fr_findtunebycookie(cookie, &tu.ipft_cookie, ifs);
6642ab25eeb5Syz 		} else {
6643f4b3ec61Sdh 			ta = ifs->ifs_ipf_tuneables;
6644ab25eeb5Syz 			tu.ipft_cookie = ta + 1;
66457c478bd9Sstevel@tonic-gate 		}
66467c478bd9Sstevel@tonic-gate 		if (ta != NULL) {
66477c478bd9Sstevel@tonic-gate 			/*
66487c478bd9Sstevel@tonic-gate 			 * Entry found, but does the data pointed to by that
66497c478bd9Sstevel@tonic-gate 			 * row fit in what we can return?
66507c478bd9Sstevel@tonic-gate 			 */
66517c478bd9Sstevel@tonic-gate 			if (ta->ipft_sz > sizeof(tu.ipft_un))
66527c478bd9Sstevel@tonic-gate 				return EINVAL;
66537c478bd9Sstevel@tonic-gate 
66547c478bd9Sstevel@tonic-gate 			tu.ipft_vlong = 0;
66557c478bd9Sstevel@tonic-gate 			if (ta->ipft_sz == sizeof(u_long))
66567c478bd9Sstevel@tonic-gate 				tu.ipft_vlong = *ta->ipft_plong;
66577c478bd9Sstevel@tonic-gate 			else if (ta->ipft_sz == sizeof(u_int))
66587c478bd9Sstevel@tonic-gate 				tu.ipft_vint = *ta->ipft_pint;
66597c478bd9Sstevel@tonic-gate 			else if (ta->ipft_sz == sizeof(u_short))
66607c478bd9Sstevel@tonic-gate 				tu.ipft_vshort = *ta->ipft_pshort;
66617c478bd9Sstevel@tonic-gate 			else if (ta->ipft_sz == sizeof(u_char))
66627c478bd9Sstevel@tonic-gate 				tu.ipft_vchar = *ta->ipft_pchar;
66637c478bd9Sstevel@tonic-gate 
66647c478bd9Sstevel@tonic-gate 			tu.ipft_sz = ta->ipft_sz;
66657c478bd9Sstevel@tonic-gate 			tu.ipft_min = ta->ipft_min;
66667c478bd9Sstevel@tonic-gate 			tu.ipft_max = ta->ipft_max;
66677c478bd9Sstevel@tonic-gate 			tu.ipft_flags = ta->ipft_flags;
66687c478bd9Sstevel@tonic-gate 			bcopy(ta->ipft_name, tu.ipft_name,
66697c478bd9Sstevel@tonic-gate 			      MIN(sizeof(tu.ipft_name),
66707c478bd9Sstevel@tonic-gate 				  strlen(ta->ipft_name) + 1));
66717c478bd9Sstevel@tonic-gate 		}
66727c478bd9Sstevel@tonic-gate 		error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
6673ab25eeb5Syz 		break;
66747c478bd9Sstevel@tonic-gate 
66757c478bd9Sstevel@tonic-gate 	case SIOCIPFGET :
66767c478bd9Sstevel@tonic-gate 	case SIOCIPFSET :
66777c478bd9Sstevel@tonic-gate 		/*
66787c478bd9Sstevel@tonic-gate 		 * Search by name or by cookie value for a particular entry
66797c478bd9Sstevel@tonic-gate 		 * in the tuning paramter table.
66807c478bd9Sstevel@tonic-gate 		 */
6681ab25eeb5Syz 		error = ESRCH;
66827c478bd9Sstevel@tonic-gate 		if (cookie != NULL) {
6683f4b3ec61Sdh 			ta = fr_findtunebycookie(cookie, NULL, ifs);
6684ab25eeb5Syz 			if (ta != NULL)
6685ab25eeb5Syz 				error = 0;
66867c478bd9Sstevel@tonic-gate 		} else if (tu.ipft_name[0] != '\0') {
6687f4b3ec61Sdh 			ta = fr_findtunebyname(tu.ipft_name, ifs);
6688ab25eeb5Syz 			if (ta != NULL)
6689ab25eeb5Syz 				error = 0;
6690ab25eeb5Syz 		}
6691ab25eeb5Syz 		if (error != 0)
6692ab25eeb5Syz 			break;
66937c478bd9Sstevel@tonic-gate 
6694ab25eeb5Syz 		if (cmd == (ioctlcmd_t)SIOCIPFGET) {
66957c478bd9Sstevel@tonic-gate 			/*
66967c478bd9Sstevel@tonic-gate 			 * Fetch the tuning parameters for a particular value
66977c478bd9Sstevel@tonic-gate 			 */
66987c478bd9Sstevel@tonic-gate 			tu.ipft_vlong = 0;
66997c478bd9Sstevel@tonic-gate 			if (ta->ipft_sz == sizeof(u_long))
67007c478bd9Sstevel@tonic-gate 				tu.ipft_vlong = *ta->ipft_plong;
67017c478bd9Sstevel@tonic-gate 			else if (ta->ipft_sz == sizeof(u_int))
67027c478bd9Sstevel@tonic-gate 				tu.ipft_vint = *ta->ipft_pint;
67037c478bd9Sstevel@tonic-gate 			else if (ta->ipft_sz == sizeof(u_short))
67047c478bd9Sstevel@tonic-gate 				tu.ipft_vshort = *ta->ipft_pshort;
67057c478bd9Sstevel@tonic-gate 			else if (ta->ipft_sz == sizeof(u_char))
67067c478bd9Sstevel@tonic-gate 				tu.ipft_vchar = *ta->ipft_pchar;
6707ab25eeb5Syz 			tu.ipft_cookie = ta;
67087c478bd9Sstevel@tonic-gate 			tu.ipft_sz = ta->ipft_sz;
67097c478bd9Sstevel@tonic-gate 			tu.ipft_min = ta->ipft_min;
67107c478bd9Sstevel@tonic-gate 			tu.ipft_max = ta->ipft_max;
67117c478bd9Sstevel@tonic-gate 			tu.ipft_flags = ta->ipft_flags;
67127c478bd9Sstevel@tonic-gate 			error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
6713ab25eeb5Syz 
6714ab25eeb5Syz 		} else if (cmd == (ioctlcmd_t)SIOCIPFSET) {
67157c478bd9Sstevel@tonic-gate 			/*
67167c478bd9Sstevel@tonic-gate 			 * Set an internal parameter.  The hard part here is
67177c478bd9Sstevel@tonic-gate 			 * getting the new value safely and correctly out of
67187c478bd9Sstevel@tonic-gate 			 * the kernel (given we only know its size, not type.)
67197c478bd9Sstevel@tonic-gate 			 */
67207c478bd9Sstevel@tonic-gate 			u_long in;
67217c478bd9Sstevel@tonic-gate 
67227c478bd9Sstevel@tonic-gate 			if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) &&
6723f4b3ec61Sdh 			    (ifs->ifs_fr_running > 0)) {
6724ab25eeb5Syz 				error = EBUSY;
6725ab25eeb5Syz 				break;
6726ab25eeb5Syz 			}
67277c478bd9Sstevel@tonic-gate 
67287c478bd9Sstevel@tonic-gate 			in = tu.ipft_vlong;
6729ab25eeb5Syz 			if (in < ta->ipft_min || in > ta->ipft_max) {
6730ab25eeb5Syz 				error = EINVAL;
6731ab25eeb5Syz 				break;
6732ab25eeb5Syz 			}
67337c478bd9Sstevel@tonic-gate 
67347c478bd9Sstevel@tonic-gate 			if (ta->ipft_sz == sizeof(u_long)) {
67357c478bd9Sstevel@tonic-gate 				tu.ipft_vlong = *ta->ipft_plong;
67367c478bd9Sstevel@tonic-gate 				*ta->ipft_plong = in;
67377c478bd9Sstevel@tonic-gate 			} else if (ta->ipft_sz == sizeof(u_int)) {
67387c478bd9Sstevel@tonic-gate 				tu.ipft_vint = *ta->ipft_pint;
67397c478bd9Sstevel@tonic-gate 				*ta->ipft_pint = (u_int)(in & 0xffffffff);
67407c478bd9Sstevel@tonic-gate 			} else if (ta->ipft_sz == sizeof(u_short)) {
67417c478bd9Sstevel@tonic-gate 				tu.ipft_vshort = *ta->ipft_pshort;
67427c478bd9Sstevel@tonic-gate 				*ta->ipft_pshort = (u_short)(in & 0xffff);
67437c478bd9Sstevel@tonic-gate 			} else if (ta->ipft_sz == sizeof(u_char)) {
67447c478bd9Sstevel@tonic-gate 				tu.ipft_vchar = *ta->ipft_pchar;
67457c478bd9Sstevel@tonic-gate 				*ta->ipft_pchar = (u_char)(in & 0xff);
67467c478bd9Sstevel@tonic-gate 			}
67477c478bd9Sstevel@tonic-gate 			error = fr_outobj(data, &tu, IPFOBJ_TUNEABLE);
67487c478bd9Sstevel@tonic-gate 		}
6749ab25eeb5Syz 		break;
6750ab25eeb5Syz 
67517c478bd9Sstevel@tonic-gate 	default :
6752ab25eeb5Syz 		error = EINVAL;
67537c478bd9Sstevel@tonic-gate 		break;
67547c478bd9Sstevel@tonic-gate 	}
67557c478bd9Sstevel@tonic-gate 
6756ab25eeb5Syz 	return error;
6757ab25eeb5Syz }
6758ab25eeb5Syz 
6759ab25eeb5Syz 
6760ab25eeb5Syz /* ------------------------------------------------------------------------ */
6761ab25eeb5Syz /* Function:    fr_initialise                                               */
6762ab25eeb5Syz /* Returns:     int - 0 == success,  < 0 == failure                         */
6763ab25eeb5Syz /* Parameters:  None.                                                       */
6764ab25eeb5Syz /*                                                                          */
6765ab25eeb5Syz /* Call of the initialise functions for all the various subsystems inside   */
6766ab25eeb5Syz /* of IPFilter.  If any of them should fail, return immeadiately a failure  */
6767ab25eeb5Syz /* BUT do not try to recover from the error here.                           */
6768ab25eeb5Syz /* ------------------------------------------------------------------------ */
6769f4b3ec61Sdh int fr_initialise(ifs)
6770f4b3ec61Sdh ipf_stack_t *ifs;
6771ab25eeb5Syz {
6772ab25eeb5Syz 	int i;
6773ab25eeb5Syz 
6774ab25eeb5Syz #ifdef IPFILTER_LOG
6775f4b3ec61Sdh 	i = fr_loginit(ifs);
6776ab25eeb5Syz 	if (i < 0)
6777ab25eeb5Syz 		return -10 + i;
6778ab25eeb5Syz #endif
6779f4b3ec61Sdh 	i = fr_natinit(ifs);
6780ab25eeb5Syz 	if (i < 0)
6781ab25eeb5Syz 		return -20 + i;
6782ab25eeb5Syz 
6783f4b3ec61Sdh 	i = fr_stateinit(ifs);
6784ab25eeb5Syz 	if (i < 0)
6785ab25eeb5Syz 		return -30 + i;
6786ab25eeb5Syz 
6787f4b3ec61Sdh 	i = fr_authinit(ifs);
6788ab25eeb5Syz 	if (i < 0)
6789ab25eeb5Syz 		return -40 + i;
6790ab25eeb5Syz 
6791f4b3ec61Sdh 	i = fr_fraginit(ifs);
6792ab25eeb5Syz 	if (i < 0)
6793ab25eeb5Syz 		return -50 + i;
6794ab25eeb5Syz 
6795f4b3ec61Sdh 	i = appr_init(ifs);
6796ab25eeb5Syz 	if (i < 0)
6797ab25eeb5Syz 		return -60 + i;
6798ab25eeb5Syz 
6799ab25eeb5Syz #ifdef IPFILTER_SYNC
6800f4b3ec61Sdh 	i = ipfsync_init(ifs);
6801ab25eeb5Syz 	if (i < 0)
6802ab25eeb5Syz 		return -70 + i;
6803ab25eeb5Syz #endif
6804ab25eeb5Syz #ifdef IPFILTER_SCAN
6805f4b3ec61Sdh 	i = ipsc_init(ifs);
6806ab25eeb5Syz 	if (i < 0)
6807ab25eeb5Syz 		return -80 + i;
6808ab25eeb5Syz #endif
6809ab25eeb5Syz #ifdef IPFILTER_LOOKUP
6810f4b3ec61Sdh 	i = ip_lookup_init(ifs);
6811ab25eeb5Syz 	if (i < 0)
6812ab25eeb5Syz 		return -90 + i;
6813ab25eeb5Syz #endif
6814ab25eeb5Syz #ifdef IPFILTER_COMPILED
6815f4b3ec61Sdh 	ipfrule_add(ifs);
6816ab25eeb5Syz #endif
6817ab25eeb5Syz 	return 0;
6818ab25eeb5Syz }
6819ab25eeb5Syz 
6820ab25eeb5Syz 
6821ab25eeb5Syz /* ------------------------------------------------------------------------ */
6822ab25eeb5Syz /* Function:    fr_deinitialise                                             */
6823ab25eeb5Syz /* Returns:     None.                                                       */
6824ab25eeb5Syz /* Parameters:  None.                                                       */
6825ab25eeb5Syz /*                                                                          */
6826ab25eeb5Syz /* Call all the various subsystem cleanup routines to deallocate memory or  */
6827ab25eeb5Syz /* destroy locks or whatever they've done that they need to now undo.       */
6828ab25eeb5Syz /* The order here IS important as there are some cross references of        */
6829ab25eeb5Syz /* internal data structures.                                                */
6830ab25eeb5Syz /* ------------------------------------------------------------------------ */
6831f4b3ec61Sdh void fr_deinitialise(ifs)
6832f4b3ec61Sdh ipf_stack_t *ifs;
6833ab25eeb5Syz {
6834f4b3ec61Sdh 	fr_fragunload(ifs);
6835f4b3ec61Sdh 	fr_authunload(ifs);
6836f4b3ec61Sdh 	fr_natunload(ifs);
6837f4b3ec61Sdh 	fr_stateunload(ifs);
6838ab25eeb5Syz #ifdef IPFILTER_SCAN
6839f4b3ec61Sdh 	fr_scanunload(ifs);
6840ab25eeb5Syz #endif
6841f4b3ec61Sdh 	appr_unload(ifs);
6842ab25eeb5Syz 
6843ab25eeb5Syz #ifdef IPFILTER_COMPILED
6844f4b3ec61Sdh 	ipfrule_remove(ifs);
6845ab25eeb5Syz #endif
6846ab25eeb5Syz 
6847f4b3ec61Sdh 	(void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE, ifs);
6848f4b3ec61Sdh 	(void) frflush(IPL_LOGIPF, 0, FR_INQUE|FR_OUTQUE, ifs);
6849f4b3ec61Sdh 	(void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE|FR_INACTIVE, ifs);
6850f4b3ec61Sdh 	(void) frflush(IPL_LOGCOUNT, 0, FR_INQUE|FR_OUTQUE, ifs);
6851ab25eeb5Syz 
6852ab25eeb5Syz #ifdef IPFILTER_LOOKUP
6853f4b3ec61Sdh 	ip_lookup_unload(ifs);
6854ab25eeb5Syz #endif
6855ab25eeb5Syz 
6856ab25eeb5Syz #ifdef IPFILTER_LOG
6857f4b3ec61Sdh 	fr_logunload(ifs);
6858ab25eeb5Syz #endif
6859ab25eeb5Syz }
6860ab25eeb5Syz 
6861ab25eeb5Syz 
6862ab25eeb5Syz /* ------------------------------------------------------------------------ */
6863ab25eeb5Syz /* Function:    fr_zerostats                                                */
6864ab25eeb5Syz /* Returns:     int - 0 = success, else failure                             */
6865ab25eeb5Syz /* Parameters:  data(O) - pointer to pointer for copying data back to       */
6866ab25eeb5Syz /*                                                                          */
6867ab25eeb5Syz /* Copies the current statistics out to userspace and then zero's the       */
6868ab25eeb5Syz /* current ones in the kernel. The lock is only held across the bzero() as  */
6869ab25eeb5Syz /* the copyout may result in paging (ie network activity.)                  */
6870ab25eeb5Syz /* ------------------------------------------------------------------------ */
6871f4b3ec61Sdh int	fr_zerostats(data, ifs)
6872ab25eeb5Syz caddr_t	data;
6873f4b3ec61Sdh ipf_stack_t *ifs;
6874ab25eeb5Syz {
6875ab25eeb5Syz 	friostat_t fio;
6876ab25eeb5Syz 	int error;
6877ab25eeb5Syz 
6878f4b3ec61Sdh 	fr_getstat(&fio, ifs);
6879ab25eeb5Syz 	error = copyoutptr(&fio, data, sizeof(fio));
6880ab25eeb5Syz 	if (error)
6881ab25eeb5Syz 		return EFAULT;
6882ab25eeb5Syz 
6883f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_mutex);
6884f4b3ec61Sdh 	bzero((char *)ifs->ifs_frstats, sizeof(*ifs->ifs_frstats) * 2);
6885f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
6886ab25eeb5Syz 
6887ab25eeb5Syz 	return 0;
6888ab25eeb5Syz }
6889ab25eeb5Syz 
6890ab25eeb5Syz 
6891ab25eeb5Syz #ifdef _KERNEL
6892ab25eeb5Syz /* ------------------------------------------------------------------------ */
6893ab25eeb5Syz /* Function:    fr_resolvedest                                              */
6894ab25eeb5Syz /* Returns:     Nil                                                         */
6895ab25eeb5Syz /* Parameters:  fdp(IO) - pointer to destination information to resolve     */
6896ab25eeb5Syz /*              v(I)    - IP protocol version to match                      */
6897ab25eeb5Syz /*                                                                          */
6898ab25eeb5Syz /* Looks up an interface name in the frdest structure pointed to by fdp and */
6899ab25eeb5Syz /* if a matching name can be found for the particular IP protocol version   */
6900ab25eeb5Syz /* then store the interface pointer in the frdest struct.  If no match is   */
6901ab25eeb5Syz /* found, then set the interface pointer to be -1 as NULL is considered to  */
6902ab25eeb5Syz /* indicate there is no information at all in the structure.                */
6903ab25eeb5Syz /* ------------------------------------------------------------------------ */
6904f4b3ec61Sdh void fr_resolvedest(fdp, v, ifs)
6905ab25eeb5Syz frdest_t *fdp;
6906ab25eeb5Syz int v;
6907f4b3ec61Sdh ipf_stack_t *ifs;
6908ab25eeb5Syz {
6909381a2a9aSdr 	fdp->fd_ifp = NULL;
6910ab25eeb5Syz 
6911381a2a9aSdr   	if (*fdp->fd_ifname != '\0') {
6912f4b3ec61Sdh  		fdp->fd_ifp = GETIFP(fdp->fd_ifname, v, ifs);
6913381a2a9aSdr 		if (fdp->fd_ifp == NULL)
6914381a2a9aSdr 			fdp->fd_ifp = (void *)-1;
6915ab25eeb5Syz 	}
6916ab25eeb5Syz }
6917ab25eeb5Syz #endif /* _KERNEL */
6918ab25eeb5Syz 
6919ab25eeb5Syz 
6920ab25eeb5Syz /* ------------------------------------------------------------------------ */
6921ab25eeb5Syz /* Function:    fr_resolvenic                                               */
6922ab25eeb5Syz /* Returns:     void* - NULL = wildcard name, -1 = failed to find NIC, else */
6923ab25eeb5Syz /*                      pointer to interface structure for NIC              */
6924ab25eeb5Syz /* Parameters:  name(I) - complete interface name                           */
6925ab25eeb5Syz /*              v(I)    - IP protocol version                               */
6926ab25eeb5Syz /*                                                                          */
6927ab25eeb5Syz /* Look for a network interface structure that firstly has a matching name  */
6928ab25eeb5Syz /* to that passed in and that is also being used for that IP protocol       */
6929ab25eeb5Syz /* version (necessary on some platforms where there are separate listings   */
6930ab25eeb5Syz /* for both IPv4 and IPv6 on the same physical NIC.                         */
6931ab25eeb5Syz /*                                                                          */
6932ab25eeb5Syz /* One might wonder why name gets terminated with a \0 byte in here.  The   */
6933ab25eeb5Syz /* reason is an interface name could get into the kernel structures of ipf  */
6934ab25eeb5Syz /* in any number of ways and so long as they all use the same sized array   */
6935ab25eeb5Syz /* to put the name in, it makes sense to ensure it gets null terminated     */
6936ab25eeb5Syz /* before it is used for its intended purpose - finding its match in the    */
6937ab25eeb5Syz /* kernel's list of configured interfaces.                                  */
6938ab25eeb5Syz /*                                                                          */
6939ab25eeb5Syz /* NOTE: This SHOULD ONLY be used with IPFilter structures that have an     */
6940ab25eeb5Syz /*       array for the name that is LIFNAMSIZ bytes (at least) in length.   */
6941ab25eeb5Syz /* ------------------------------------------------------------------------ */
6942f4b3ec61Sdh void *fr_resolvenic(name, v, ifs)
6943ab25eeb5Syz char *name;
6944ab25eeb5Syz int v;
6945f4b3ec61Sdh ipf_stack_t *ifs;
6946ab25eeb5Syz {
6947ab25eeb5Syz 	void *nic;
6948ab25eeb5Syz 
6949ab25eeb5Syz 	if (name[0] == '\0')
6950ab25eeb5Syz 		return NULL;
6951ab25eeb5Syz 
6952ab25eeb5Syz 	if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) {
6953ab25eeb5Syz 		return NULL;
6954ab25eeb5Syz 	}
6955ab25eeb5Syz 
6956ab25eeb5Syz 	name[LIFNAMSIZ - 1] = '\0';
6957ab25eeb5Syz 
6958f4b3ec61Sdh 	nic = GETIFP(name, v, ifs);
6959ab25eeb5Syz 	if (nic == NULL)
6960ab25eeb5Syz 		nic = (void *)-1;
6961ab25eeb5Syz 	return nic;
69627c478bd9Sstevel@tonic-gate }
6963f4b3ec61Sdh 
696490b0a856Sjojemann 
696590b0a856Sjojemann /* ------------------------------------------------------------------------ */
696690b0a856Sjojemann /* Function:    ipf_expiretokens                                            */
696790b0a856Sjojemann /* Returns:     None.                                                       */
696890b0a856Sjojemann /* Parameters:  ifs - ipf stack instance                                    */
696990b0a856Sjojemann /*                                                                          */
697090b0a856Sjojemann /* This function is run every ipf tick to see if there are any tokens that  */
697190b0a856Sjojemann /* have been held for too long and need to be freed up.                     */
697290b0a856Sjojemann /* ------------------------------------------------------------------------ */
6973f4b3ec61Sdh void ipf_expiretokens(ifs)
6974f4b3ec61Sdh ipf_stack_t *ifs;
6975f4b3ec61Sdh {
6976f4b3ec61Sdh 	ipftoken_t *it;
6977f4b3ec61Sdh 
6978f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_tokens);
6979f4b3ec61Sdh 	while ((it = ifs->ifs_ipftokenhead) != NULL) {
6980f4b3ec61Sdh 		if (it->ipt_die > ifs->ifs_fr_ticks)
6981f4b3ec61Sdh 			break;
6982f4b3ec61Sdh 
6983f4b3ec61Sdh 		ipf_freetoken(it, ifs);
6984f4b3ec61Sdh 	}
6985f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
6986f4b3ec61Sdh }
6987f4b3ec61Sdh 
6988f4b3ec61Sdh 
698990b0a856Sjojemann /* ------------------------------------------------------------------------ */
699090b0a856Sjojemann /* Function:    ipf_deltoken                                                */
699190b0a856Sjojemann /* Returns:     int - 0 = success, else error                               */
699290b0a856Sjojemann /* Parameters:  type(I) - the token type to match                           */
699390b0a856Sjojemann /*              uid(I)  - uid owning the token                              */
699490b0a856Sjojemann /*              ptr(I)  - context pointer for the token                     */
699590b0a856Sjojemann /*              ifs - ipf stack instance                                    */
699690b0a856Sjojemann /*                                                                          */
699790b0a856Sjojemann /* This function looks for a a token in the current list that matches up    */
699890b0a856Sjojemann /* the fields (type, uid, ptr).  If none is found, ESRCH is returned, else  */
699990b0a856Sjojemann /* call ipf_freetoken() to remove it from the list.                         */
700090b0a856Sjojemann /* ------------------------------------------------------------------------ */
7001f4b3ec61Sdh int ipf_deltoken(type, uid, ptr, ifs)
7002f4b3ec61Sdh int type, uid;
7003f4b3ec61Sdh void *ptr;
7004f4b3ec61Sdh ipf_stack_t *ifs;
7005f4b3ec61Sdh {
7006f4b3ec61Sdh 	ipftoken_t *it;
7007f4b3ec61Sdh 	int error = ESRCH;
7008f4b3ec61Sdh 
7009f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_tokens);
7010f4b3ec61Sdh 	for (it = ifs->ifs_ipftokenhead; it != NULL; it = it->ipt_next)
7011f4b3ec61Sdh 		if (ptr == it->ipt_ctx && type == it->ipt_type &&
7012f4b3ec61Sdh 		    uid == it->ipt_uid) {
7013f4b3ec61Sdh 			ipf_freetoken(it, ifs);
7014f4b3ec61Sdh 			error = 0;
7015f4b3ec61Sdh 			break;
7016f4b3ec61Sdh 	}
7017f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
7018f4b3ec61Sdh 
7019f4b3ec61Sdh 	return error;
7020f4b3ec61Sdh }
7021f4b3ec61Sdh 
702290b0a856Sjojemann 
702390b0a856Sjojemann /* ------------------------------------------------------------------------ */
702490b0a856Sjojemann /* Function:    ipf_unlinktoken                                             */
702590b0a856Sjojemann /* Returns:     None.                                                       */
702690b0a856Sjojemann /* Parameters:  token(I) - pointer to token structure                       */
702790b0a856Sjojemann /*              ifs - ipf stack instance                                    */
702890b0a856Sjojemann /*                                                                          */
702990b0a856Sjojemann /* This function unlinks a token structure from the linked list of tokens   */
703090b0a856Sjojemann /* that it belongs to.  The head pointer never needs to be explicitly       */
703190b0a856Sjojemann /* adjusted, but the tail does due to the linked list implementation.       */
703290b0a856Sjojemann /* ------------------------------------------------------------------------ */
7033786c7074Sjojemann static void ipf_unlinktoken(token, ifs)
7034f4b3ec61Sdh ipftoken_t *token;
7035f4b3ec61Sdh ipf_stack_t *ifs;
7036f4b3ec61Sdh {
7037f4b3ec61Sdh 
7038f4b3ec61Sdh 	if (ifs->ifs_ipftokentail == &token->ipt_next)
7039f4b3ec61Sdh 		ifs->ifs_ipftokentail = token->ipt_pnext;
7040f4b3ec61Sdh 
7041f4b3ec61Sdh 	*token->ipt_pnext = token->ipt_next;
7042f4b3ec61Sdh 	if (token->ipt_next != NULL)
7043f4b3ec61Sdh 		token->ipt_next->ipt_pnext = token->ipt_pnext;
7044f4b3ec61Sdh }
7045f4b3ec61Sdh 
7046f4b3ec61Sdh 
704790b0a856Sjojemann /* ------------------------------------------------------------------------ */
704890b0a856Sjojemann /* Function:    ipf_findtoken                                               */
704990b0a856Sjojemann /* Returns:     ipftoken_t * - NULL if no memory, else pointer to token     */
705090b0a856Sjojemann /* Parameters:  type(I) - the token type to match                           */
705190b0a856Sjojemann /*              uid(I) - uid owning the token                               */
705290b0a856Sjojemann /*              ptr(I) - context pointer for the token                      */
705390b0a856Sjojemann /*              ifs - ipf stack instance                                    */
705490b0a856Sjojemann /*                                                                          */
705590b0a856Sjojemann /* This function looks for a live token in the list of current tokens that  */
705690b0a856Sjojemann /* matches the tuple (type, uid, ptr).  If one cannot be found then one is  */
705790b0a856Sjojemann /* allocated.  If one is found then it is moved to the top of the list of   */
705890b0a856Sjojemann /* currently active tokens.                                                 */
705990b0a856Sjojemann /*                                                                          */
706090b0a856Sjojemann /* NOTE: It is by design that this function returns holding a read lock on  */
706190b0a856Sjojemann /*       ipf_tokens.  Callers must make sure they release it!               */
706290b0a856Sjojemann /* ------------------------------------------------------------------------ */
7063f4b3ec61Sdh ipftoken_t *ipf_findtoken(type, uid, ptr, ifs)
7064f4b3ec61Sdh int type, uid;
7065f4b3ec61Sdh void *ptr;
7066f4b3ec61Sdh ipf_stack_t *ifs;
7067f4b3ec61Sdh {
7068f4b3ec61Sdh 	ipftoken_t *it, *new;
7069f4b3ec61Sdh 
7070f4b3ec61Sdh 	KMALLOC(new, ipftoken_t *);
7071f4b3ec61Sdh 
7072f4b3ec61Sdh 	WRITE_ENTER(&ifs->ifs_ipf_tokens);
7073f4b3ec61Sdh 	for (it = ifs->ifs_ipftokenhead; it != NULL; it = it->ipt_next) {
7074f4b3ec61Sdh 		if (it->ipt_alive == 0)
7075f4b3ec61Sdh 			continue;
7076f4b3ec61Sdh 		if (ptr == it->ipt_ctx && type == it->ipt_type &&
7077f4b3ec61Sdh 		    uid == it->ipt_uid)
7078f4b3ec61Sdh 			break;
7079f4b3ec61Sdh 	}
7080f4b3ec61Sdh 
7081f4b3ec61Sdh 	if (it == NULL) {
7082f4b3ec61Sdh 		it = new;
7083f4b3ec61Sdh 		new = NULL;
7084f4b3ec61Sdh 		if (it == NULL)
7085f4b3ec61Sdh 			return NULL;
7086f4b3ec61Sdh 		it->ipt_data = NULL;
7087f4b3ec61Sdh 		it->ipt_ctx = ptr;
7088f4b3ec61Sdh 		it->ipt_uid = uid;
7089f4b3ec61Sdh 		it->ipt_type = type;
7090f4b3ec61Sdh 		it->ipt_next = NULL;
7091f4b3ec61Sdh 		it->ipt_alive = 1;
7092f4b3ec61Sdh 	} else {
7093f4b3ec61Sdh 		if (new != NULL) {
7094f4b3ec61Sdh 			KFREE(new);
7095f4b3ec61Sdh 			new = NULL;
7096f4b3ec61Sdh 		}
7097f4b3ec61Sdh 
7098f4b3ec61Sdh 		ipf_unlinktoken(it, ifs);
7099f4b3ec61Sdh 	}
7100f4b3ec61Sdh 	it->ipt_pnext = ifs->ifs_ipftokentail;
7101f4b3ec61Sdh 	*ifs->ifs_ipftokentail = it;
7102f4b3ec61Sdh 	ifs->ifs_ipftokentail = &it->ipt_next;
7103f4b3ec61Sdh 	it->ipt_next = NULL;
7104f4b3ec61Sdh 
7105f4b3ec61Sdh 	it->ipt_die = ifs->ifs_fr_ticks + 2;
7106f4b3ec61Sdh 
7107f4b3ec61Sdh 	MUTEX_DOWNGRADE(&ifs->ifs_ipf_tokens);
7108f4b3ec61Sdh 
7109f4b3ec61Sdh 	return it;
7110f4b3ec61Sdh }
7111f4b3ec61Sdh 
7112f4b3ec61Sdh 
711390b0a856Sjojemann /* ------------------------------------------------------------------------ */
711490b0a856Sjojemann /* Function:    ipf_freetoken                                               */
711590b0a856Sjojemann /* Returns:     None.                                                       */
711690b0a856Sjojemann /* Parameters:  token(I) - pointer to token structure                       */
711790b0a856Sjojemann /*              ifs - ipf stack instance                                    */
711890b0a856Sjojemann /*                                                                          */
711990b0a856Sjojemann /* This function unlinks a token from the linked list and on the path to    */
712090b0a856Sjojemann /* free'ing the data, it calls the dereference function that is associated  */
712190b0a856Sjojemann /* with the type of data pointed to by the token as it is considered to     */
712290b0a856Sjojemann /* hold a reference to it.                                                  */
712390b0a856Sjojemann /* ------------------------------------------------------------------------ */
7124f4b3ec61Sdh void ipf_freetoken(token, ifs)
7125f4b3ec61Sdh ipftoken_t *token;
7126f4b3ec61Sdh ipf_stack_t *ifs;
7127f4b3ec61Sdh {
712890b0a856Sjojemann 	void *data, **datap;
7129f4b3ec61Sdh 
7130f4b3ec61Sdh 	ipf_unlinktoken(token, ifs);
7131f4b3ec61Sdh 
7132f4b3ec61Sdh 	data = token->ipt_data;
713390b0a856Sjojemann 	datap = &data;
7134f4b3ec61Sdh 
7135f4b3ec61Sdh 	if ((data != NULL) && (data != (void *)-1)) {
7136f4b3ec61Sdh 		switch (token->ipt_type)
7137f4b3ec61Sdh 		{
7138f4b3ec61Sdh 		case IPFGENITER_IPF :
713990b0a856Sjojemann 			(void)fr_derefrule((frentry_t **)datap, ifs);
7140f4b3ec61Sdh 			break;
7141f4b3ec61Sdh 		case IPFGENITER_IPNAT :
7142f4b3ec61Sdh 			WRITE_ENTER(&ifs->ifs_ipf_nat);
714390b0a856Sjojemann 			fr_ipnatderef((ipnat_t **)datap, ifs);
7144f4b3ec61Sdh 			RWLOCK_EXIT(&ifs->ifs_ipf_nat);
7145f4b3ec61Sdh 			break;
7146f4b3ec61Sdh 		case IPFGENITER_NAT :
714790b0a856Sjojemann 			fr_natderef((nat_t **)datap, ifs);
7148f4b3ec61Sdh 			break;
7149f4b3ec61Sdh 		case IPFGENITER_STATE :
715090b0a856Sjojemann 			fr_statederef((ipstate_t **)datap, ifs);
7151f4b3ec61Sdh 			break;
7152f4b3ec61Sdh 		case IPFGENITER_FRAG :
715390b0a856Sjojemann 			fr_fragderef((ipfr_t **)datap, &ifs->ifs_ipf_frag, ifs);
7154f4b3ec61Sdh 			break;
7155f4b3ec61Sdh 		case IPFGENITER_NATFRAG :
715690b0a856Sjojemann  			fr_fragderef((ipfr_t **)datap,
715790b0a856Sjojemann 				     &ifs->ifs_ipf_natfrag, ifs);
7158f4b3ec61Sdh 			break;
7159f4b3ec61Sdh 		case IPFGENITER_HOSTMAP :
716090b0a856Sjojemann 			WRITE_ENTER(&ifs->ifs_ipf_nat);
716190b0a856Sjojemann 			fr_hostmapdel((hostmap_t **)datap);
716290b0a856Sjojemann 			RWLOCK_EXIT(&ifs->ifs_ipf_nat);
7163f4b3ec61Sdh 			break;
7164f4b3ec61Sdh 		default :
7165f4b3ec61Sdh 			(void) ip_lookup_iterderef(token->ipt_type, data, ifs);
7166f4b3ec61Sdh 			break;
7167f4b3ec61Sdh 		}
7168f4b3ec61Sdh 	}
7169f4b3ec61Sdh 
7170f4b3ec61Sdh 	KFREE(token);
7171f4b3ec61Sdh }
7172f4b3ec61Sdh 
717390b0a856Sjojemann 
717490b0a856Sjojemann /* ------------------------------------------------------------------------ */
717590b0a856Sjojemann /* Function:    ipf_getnextrule                                             */
717690b0a856Sjojemann /* Returns:     int - 0 = success, else error                               */
717790b0a856Sjojemann /* Parameters:  t(I)   - pointer to destination information to resolve      */
717890b0a856Sjojemann /*              ptr(I) - pointer to ipfobj_t to copyin from user space      */
717990b0a856Sjojemann /*              ifs - ipf stack instance                                    */
718090b0a856Sjojemann /*                                                                          */
718190b0a856Sjojemann /* This function's first job is to bring in the ipfruleiter_t structure via */
718290b0a856Sjojemann /* the ipfobj_t structure to determine what should be the next rule to      */
718390b0a856Sjojemann /* return. Once the ipfruleiter_t has been brought in, it then tries to     */
718490b0a856Sjojemann /* find the 'next rule'.  This may include searching rule group lists or    */
718590b0a856Sjojemann /* just be as simple as looking at the 'next' field in the rule structure.  */
718690b0a856Sjojemann /* When we have found the rule to return, increase its reference count and  */
718790b0a856Sjojemann /* if we used an existing rule to get here, decrease its reference count.   */
718890b0a856Sjojemann /* ------------------------------------------------------------------------ */
718990b0a856Sjojemann int ipf_getnextrule(t, ptr, ifs)
719090b0a856Sjojemann ipftoken_t *t;
719190b0a856Sjojemann void *ptr;
719290b0a856Sjojemann ipf_stack_t *ifs;
7193f4b3ec61Sdh {
7194f4b3ec61Sdh 	frentry_t *fr, *next, zero;
719590b0a856Sjojemann 	int error, out, count;
7196f4b3ec61Sdh 	ipfruleiter_t it;
7197f4b3ec61Sdh 	frgroup_t *fg;
719890b0a856Sjojemann 	char *dst;
7199f4b3ec61Sdh 
7200f4b3ec61Sdh 	if (t == NULL || ptr == NULL)
7201f4b3ec61Sdh 		return EFAULT;
7202f4b3ec61Sdh 	error = fr_inobj(ptr, &it, IPFOBJ_IPFITER);
7203f4b3ec61Sdh 	if (error != 0)
7204f4b3ec61Sdh 		return error;
7205f4b3ec61Sdh 	if ((it.iri_ver != AF_INET) && (it.iri_ver != AF_INET6))
7206f4b3ec61Sdh 		return EINVAL;
720790b0a856Sjojemann 	if ((it.iri_inout < 0) || (it.iri_inout > 3))
720890b0a856Sjojemann 		return EINVAL;
720990b0a856Sjojemann 	if (it.iri_nrules == 0)
7210f4b3ec61Sdh 		return EINVAL;
7211f4b3ec61Sdh 	if ((it.iri_active != 0) && (it.iri_active != 1))
7212f4b3ec61Sdh 		return EINVAL;
7213f4b3ec61Sdh 	if (it.iri_rule == NULL)
7214f4b3ec61Sdh 		return EFAULT;
7215f4b3ec61Sdh 
721690b0a856Sjojemann 	/*
721790b0a856Sjojemann 	 * Use bitmask on it.iri_inout to determine direction.
721890b0a856Sjojemann 	 * F_OUT (1) and F_ACOUT (3) mask to out = 1, while
721990b0a856Sjojemann 	 * F_IN (0) and F_ACIN (2) mask to out = 0.
722090b0a856Sjojemann 	 */
722190b0a856Sjojemann 	out = it.iri_inout & F_OUT;
7222f4b3ec61Sdh 	READ_ENTER(&ifs->ifs_ipf_mutex);
7223786c7074Sjojemann 
7224786c7074Sjojemann 	/*
7225786c7074Sjojemann 	 * Retrieve "previous" entry from token and find the next entry.
7226786c7074Sjojemann 	 */
7227786c7074Sjojemann 	fr = t->ipt_data;
7228f4b3ec61Sdh 	if (fr == NULL) {
7229f4b3ec61Sdh 		if (*it.iri_group == '\0') {
723090b0a856Sjojemann 			/*
723190b0a856Sjojemann 			 * Use bitmask again to determine accounting or not.
723290b0a856Sjojemann 			 * F_ACIN will mask to accounting cases F_ACIN (2)
723390b0a856Sjojemann 			 * or F_ACOUT (3), but not F_IN or F_OUT.
723490b0a856Sjojemann 			 */
723590b0a856Sjojemann 			if ((it.iri_inout & F_ACIN) != 0) {
723690b0a856Sjojemann 				if (it.iri_ver == AF_INET)
723790b0a856Sjojemann 					next = ifs->ifs_ipacct
723890b0a856Sjojemann 					    [out][it.iri_active];
723990b0a856Sjojemann 				else
724090b0a856Sjojemann 					next = ifs->ifs_ipacct6
724190b0a856Sjojemann 					    [out][it.iri_active];
724290b0a856Sjojemann 			} else {
724390b0a856Sjojemann 				if (it.iri_ver == AF_INET)
724490b0a856Sjojemann 					next = ifs->ifs_ipfilter
724590b0a856Sjojemann 					    [out][it.iri_active];
724690b0a856Sjojemann 				else
724790b0a856Sjojemann 					next = ifs->ifs_ipfilter6
724890b0a856Sjojemann 					    [out][it.iri_active];
724990b0a856Sjojemann 			}
7250f4b3ec61Sdh 		} else {
7251f4b3ec61Sdh 			fg = fr_findgroup(it.iri_group, IPL_LOGIPF,
7252f4b3ec61Sdh 					  it.iri_active, NULL, ifs);
7253f4b3ec61Sdh 			if (fg != NULL)
7254f4b3ec61Sdh 				next = fg->fg_start;
7255f4b3ec61Sdh 			else
7256f4b3ec61Sdh 				next = NULL;
7257f4b3ec61Sdh 		}
7258f4b3ec61Sdh 	} else {
7259f4b3ec61Sdh 		next = fr->fr_next;
7260f4b3ec61Sdh 	}
7261f4b3ec61Sdh 
726290b0a856Sjojemann 	dst = (char *)it.iri_rule;
726390b0a856Sjojemann 	/*
726490b0a856Sjojemann 	 * The ipfruleiter may ask for more than 1 rule at a time to be
726590b0a856Sjojemann 	 * copied out, so long as that many exist in the list to start with!
726690b0a856Sjojemann 	 */
726790b0a856Sjojemann 	for (count = it.iri_nrules; count > 0; count--) {
7268786c7074Sjojemann 		/*
7269786c7074Sjojemann 		 * If we found an entry, add reference to it and update token.
7270786c7074Sjojemann 		 * Otherwise, zero out data to be returned and NULL out token.
7271786c7074Sjojemann 		 */
727290b0a856Sjojemann 		if (next != NULL) {
7273f4b3ec61Sdh 			MUTEX_ENTER(&next->fr_lock);
7274f4b3ec61Sdh 			next->fr_ref++;
7275f4b3ec61Sdh 			MUTEX_EXIT(&next->fr_lock);
7276966f126dSzf 			t->ipt_data = next;
727790b0a856Sjojemann 		} else {
727890b0a856Sjojemann 			bzero(&zero, sizeof(zero));
727990b0a856Sjojemann 			next = &zero;
728090b0a856Sjojemann 			t->ipt_data = NULL;
728190b0a856Sjojemann 		}
7282786c7074Sjojemann 
7283786c7074Sjojemann 		/*
7284786c7074Sjojemann 		 * Now that we have ref, it's save to give up lock.
7285786c7074Sjojemann 		 */
728690b0a856Sjojemann 		RWLOCK_EXIT(&ifs->ifs_ipf_mutex);
728790b0a856Sjojemann 
7288786c7074Sjojemann 		/*
7289786c7074Sjojemann 		 * Copy out data and clean up references and token as needed.
7290786c7074Sjojemann 		 */
729190b0a856Sjojemann 		error = COPYOUT(next, dst, sizeof(*next));
7292f4b3ec61Sdh 		if (error != 0)
7293786c7074Sjojemann 			error = EFAULT;
7294786c7074Sjojemann 		if (t->ipt_data == NULL) {
7295786c7074Sjojemann 			ipf_freetoken(t, ifs);
7296786c7074Sjojemann 			break;
7297786c7074Sjojemann 		} else {
7298786c7074Sjojemann 			if (fr != NULL)
7299786c7074Sjojemann 				(void) fr_derefrule(&fr, ifs);
7300786c7074Sjojemann 			if (next->fr_data != NULL) {
7301786c7074Sjojemann 				dst += sizeof(*next);
7302786c7074Sjojemann 				error = COPYOUT(next->fr_data, dst,
7303786c7074Sjojemann 						next->fr_dsize);
7304786c7074Sjojemann 				if (error != 0)
7305786c7074Sjojemann 					error = EFAULT;
7306786c7074Sjojemann 				else
7307786c7074Sjojemann 					dst += next->fr_dsize;
7308786c7074Sjojemann 			}
7309786c7074Sjojemann 			if (next->fr_next == NULL) {
7310786c7074Sjojemann 				ipf_freetoken(t, ifs);
7311786c7074Sjojemann 				break;
7312786c7074Sjojemann 			}
731390b0a856Sjojemann 		}
731490b0a856Sjojemann 
731590b0a856Sjojemann 		if ((count == 1) || (error != 0))
731690b0a856Sjojemann 			break;
7317f4b3ec61Sdh 
731890b0a856Sjojemann 		READ_ENTER(&ifs->ifs_ipf_mutex);
731990b0a856Sjojemann 		fr = next;
732090b0a856Sjojemann 		next = fr->fr_next;
732190b0a856Sjojemann 	}
7322786c7074Sjojemann 
7323f4b3ec61Sdh 	return error;
7324f4b3ec61Sdh }
7325f4b3ec61Sdh 
7326f4b3ec61Sdh 
732790b0a856Sjojemann /* ------------------------------------------------------------------------ */
732890b0a856Sjojemann /* Function:    fr_frruleiter                                               */
732990b0a856Sjojemann /* Returns:     int - 0 = success, else error                               */
733090b0a856Sjojemann /* Parameters:  data(I) - the token type to match                           */
733190b0a856Sjojemann /*              uid(I) - uid owning the token                               */
733290b0a856Sjojemann /*              ptr(I) - context pointer for the token                      */
733390b0a856Sjojemann /*              ifs - ipf stack instance                                    */
733490b0a856Sjojemann /*                                                                          */
733590b0a856Sjojemann /* This function serves as a stepping stone between fr_ipf_ioctl and        */
733690b0a856Sjojemann /* ipf_getnextrule.  It's role is to find the right token in the kernel for */
733790b0a856Sjojemann /* the process doing the ioctl and use that to ask for the next rule.       */
733890b0a856Sjojemann /* ------------------------------------------------------------------------ */
7339f4b3ec61Sdh int ipf_frruleiter(data, uid, ctx, ifs)
7340f4b3ec61Sdh void *data, *ctx;
7341f4b3ec61Sdh int uid;
7342f4b3ec61Sdh ipf_stack_t *ifs;
7343f4b3ec61Sdh {
7344f4b3ec61Sdh 	ipftoken_t *token;
7345f4b3ec61Sdh 	int error;
7346f4b3ec61Sdh 
7347f4b3ec61Sdh 	token = ipf_findtoken(IPFGENITER_IPF, uid, ctx, ifs);
7348f4b3ec61Sdh 	if (token != NULL)
7349f4b3ec61Sdh 		error = ipf_getnextrule(token, data, ifs);
7350f4b3ec61Sdh 	else
7351f4b3ec61Sdh 		error = EFAULT;
7352f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
7353f4b3ec61Sdh 
7354f4b3ec61Sdh 	return error;
7355f4b3ec61Sdh }
7356f4b3ec61Sdh 
7357f4b3ec61Sdh 
735890b0a856Sjojemann /* ------------------------------------------------------------------------ */
735990b0a856Sjojemann /* Function:    ipf_geniter                                                 */
736090b0a856Sjojemann /* Returns:     int - 0 = success, else error                               */
736190b0a856Sjojemann /* Parameters:  token(I) - pointer to ipftoken structure                    */
736290b0a856Sjojemann /*              itp(I) - pointer to ipfgeniter structure                    */
736390b0a856Sjojemann /*              ifs - ipf stack instance                                    */
736490b0a856Sjojemann /*                                                                          */
736590b0a856Sjojemann /* Generic iterator called from ipf_genericiter.  Currently only used for   */
736690b0a856Sjojemann /* walking through list of fragments.                                       */
736790b0a856Sjojemann /* ------------------------------------------------------------------------ */
7368f4b3ec61Sdh int ipf_geniter(token, itp, ifs)
7369f4b3ec61Sdh ipftoken_t *token;
7370f4b3ec61Sdh ipfgeniter_t *itp;
7371f4b3ec61Sdh ipf_stack_t *ifs;
7372f4b3ec61Sdh {
7373f4b3ec61Sdh 	int error;
7374f4b3ec61Sdh 
7375f4b3ec61Sdh 	switch (itp->igi_type)
7376f4b3ec61Sdh 	{
7377f4b3ec61Sdh 	case IPFGENITER_FRAG :
7378f4b3ec61Sdh 		error = fr_nextfrag(token, itp, &ifs->ifs_ipfr_list,
737990b0a856Sjojemann 				    &ifs->ifs_ipfr_tail, &ifs->ifs_ipf_frag,
738090b0a856Sjojemann 				    ifs);
7381f4b3ec61Sdh 		break;
7382f4b3ec61Sdh 	default :
7383f4b3ec61Sdh 		error = EINVAL;
7384f4b3ec61Sdh 		break;
7385f4b3ec61Sdh 	}
7386f4b3ec61Sdh 
7387f4b3ec61Sdh 	return error;
7388f4b3ec61Sdh }
7389f4b3ec61Sdh 
7390f4b3ec61Sdh 
739190b0a856Sjojemann /* ------------------------------------------------------------------------ */
739290b0a856Sjojemann /* Function:    ipf_genericiter                                             */
739390b0a856Sjojemann /* Returns:     int - 0 = success, else error                               */
739490b0a856Sjojemann /* Parameters:  data(I) - the token type to match                           */
739590b0a856Sjojemann /*              uid(I) - uid owning the token                               */
739690b0a856Sjojemann /*              ptr(I) - context pointer for the token                      */
739790b0a856Sjojemann /*              ifs - ipf stack instance                                    */
739890b0a856Sjojemann /*                                                                          */
739990b0a856Sjojemann /* This function serves as a stepping stone between fr_ipf_ioctl and        */
740090b0a856Sjojemann /* ipf_geniter when handling SIOCGENITER.  It's role is to find the right   */
740190b0a856Sjojemann /* token in the kernel for the process using the ioctl, and to use that     */
740290b0a856Sjojemann /* token when calling ipf_geniter.                                          */
740390b0a856Sjojemann /* ------------------------------------------------------------------------ */
7404f4b3ec61Sdh int ipf_genericiter(data, uid, ctx, ifs)
7405f4b3ec61Sdh void *data, *ctx;
7406f4b3ec61Sdh int uid;
7407f4b3ec61Sdh ipf_stack_t *ifs;
7408f4b3ec61Sdh {
7409f4b3ec61Sdh 	ipftoken_t *token;
7410f4b3ec61Sdh 	ipfgeniter_t iter;
7411f4b3ec61Sdh 	int error;
7412f4b3ec61Sdh 
7413f4b3ec61Sdh 	error = fr_inobj(data, &iter, IPFOBJ_GENITER);
7414f4b3ec61Sdh 	if (error != 0)
7415f4b3ec61Sdh 		return error;
7416f4b3ec61Sdh 
7417f4b3ec61Sdh 	token = ipf_findtoken(iter.igi_type, uid, ctx, ifs);
7418f4b3ec61Sdh 	if (token != NULL) {
7419f4b3ec61Sdh 		token->ipt_subtype = iter.igi_type;
7420f4b3ec61Sdh 		error = ipf_geniter(token, &iter, ifs);
7421f4b3ec61Sdh 	} else
7422f4b3ec61Sdh 		error = EFAULT;
7423f4b3ec61Sdh 	RWLOCK_EXIT(&ifs->ifs_ipf_tokens);
7424f4b3ec61Sdh 
7425f4b3ec61Sdh 	return error;
7426f4b3ec61Sdh }
7427ea8244dcSJohn Ojemann 
7428ea8244dcSJohn Ojemann 
7429ea8244dcSJohn Ojemann /* --------------------------------------------------------------------- */
7430ea8244dcSJohn Ojemann /* Function:    ipf_earlydrop                                            */
7431ea8244dcSJohn Ojemann /* Returns:     number of dropped/removed entries from the queue         */
7432ea8244dcSJohn Ojemann /* Parameters:	flushtype - which table we're cleaning (NAT or State)	 */
7433ea8244dcSJohn Ojemann /*              ifq	- pointer to queue with entries to be deleted    */
7434ea8244dcSJohn Ojemann /*              idletime - entry must be idle this long to be deleted    */
7435ea8244dcSJohn Ojemann /*              ifs     - ipf stack instance                             */
7436ea8244dcSJohn Ojemann /*                                                                       */
7437ea8244dcSJohn Ojemann /* Function is invoked from state/NAT flush routines to remove entries   */
7438ea8244dcSJohn Ojemann /* from specified timeout queue, based on how long they've sat idle,     */
7439ea8244dcSJohn Ojemann /* without waiting for it to happen on its own.                          */
7440ea8244dcSJohn Ojemann /* --------------------------------------------------------------------- */
7441ea8244dcSJohn Ojemann int ipf_earlydrop(flushtype, ifq, idletime, ifs)
7442ea8244dcSJohn Ojemann int flushtype;
7443ea8244dcSJohn Ojemann ipftq_t *ifq;
7444ea8244dcSJohn Ojemann int idletime;
7445ea8244dcSJohn Ojemann ipf_stack_t *ifs;
7446ea8244dcSJohn Ojemann {
7447ea8244dcSJohn Ojemann         ipftqent_t *tqe, *tqn;
7448ea8244dcSJohn Ojemann         unsigned int dropped;
7449ea8244dcSJohn Ojemann         int droptick;
7450ea8244dcSJohn Ojemann 	void *ent;
7451ea8244dcSJohn Ojemann 
7452ea8244dcSJohn Ojemann         if (ifq == NULL)
7453ea8244dcSJohn Ojemann                 return (0);
7454ea8244dcSJohn Ojemann 
7455ea8244dcSJohn Ojemann         dropped = 0;
7456ea8244dcSJohn Ojemann 
7457ea8244dcSJohn Ojemann         /*
7458ea8244dcSJohn Ojemann          * Determine the tick representing the idle time we're interested
7459ea8244dcSJohn Ojemann          * in.  If an entry exists in the queue, and it was touched before
7460ea8244dcSJohn Ojemann          * that tick, then it's been idle longer than idletime, so it should
7461ea8244dcSJohn Ojemann 	 * be deleted.
7462ea8244dcSJohn Ojemann          */
7463ea8244dcSJohn Ojemann         droptick = ifs->ifs_fr_ticks - idletime;
7464ea8244dcSJohn Ojemann         tqn = ifq->ifq_head;
7465ea8244dcSJohn Ojemann         while ((tqe = tqn) != NULL && tqe->tqe_touched < droptick) {
7466ea8244dcSJohn Ojemann                 tqn = tqe->tqe_next;
7467ea8244dcSJohn Ojemann 		ent = tqe->tqe_parent;
7468ea8244dcSJohn Ojemann 		switch (flushtype)
7469ea8244dcSJohn Ojemann 		{
7470ea8244dcSJohn Ojemann 		case NAT_FLUSH:
7471ea8244dcSJohn Ojemann 			if (nat_delete((nat_t *)ent, NL_FLUSH, ifs) == 0)
7472ea8244dcSJohn Ojemann 				dropped++;
7473ea8244dcSJohn Ojemann 			break;
7474ea8244dcSJohn Ojemann 		case STATE_FLUSH:
7475ea8244dcSJohn Ojemann 			if (fr_delstate((ipstate_t *)ent, ISL_FLUSH, ifs) == 0)
7476ea8244dcSJohn Ojemann 				dropped++;
7477ea8244dcSJohn Ojemann 			break;
7478ea8244dcSJohn Ojemann 		default:
7479ea8244dcSJohn Ojemann 			return (0);
7480ea8244dcSJohn Ojemann 		}
7481ea8244dcSJohn Ojemann         }
7482ea8244dcSJohn Ojemann         return (dropped);
7483ea8244dcSJohn Ojemann }
7484ea8244dcSJohn Ojemann 
7485ea8244dcSJohn Ojemann 
7486ea8244dcSJohn Ojemann /* --------------------------------------------------------------------- */
7487ea8244dcSJohn Ojemann /* Function:    ipf_flushclosing                                         */
7488ea8244dcSJohn Ojemann /* Returns:     int - number of entries deleted                          */
7489ea8244dcSJohn Ojemann /* Parameters:	flushtype - which table we're cleaning (NAT or State)	 */
7490ea8244dcSJohn Ojemann /*              stateval - TCP state at which to start removing entries  */
7491ea8244dcSJohn Ojemann /*              ipfqs - pointer to timeout queues                        */
7492ea8244dcSJohn Ojemann /*              userqs - pointer to user defined queues                  */
7493ea8244dcSJohn Ojemann /*              ifs  - ipf stack instance                                */
7494ea8244dcSJohn Ojemann /*                                                                       */
7495ea8244dcSJohn Ojemann /* Remove state/NAT table entries for TCP connections which are in the   */
7496ea8244dcSJohn Ojemann /* process of closing, and have at least reached the state specified by  */
7497ea8244dcSJohn Ojemann /* the 'stateval' parameter.                                             */
7498ea8244dcSJohn Ojemann /* --------------------------------------------------------------------- */
7499ea8244dcSJohn Ojemann int ipf_flushclosing(flushtype, stateval, ipfqs, userqs, ifs)
7500ea8244dcSJohn Ojemann int flushtype, stateval;
7501ea8244dcSJohn Ojemann ipftq_t *ipfqs, *userqs;
7502ea8244dcSJohn Ojemann ipf_stack_t *ifs;
7503ea8244dcSJohn Ojemann {
7504ea8244dcSJohn Ojemann 	ipftq_t *ifq, *ifqn;
7505ea8244dcSJohn Ojemann         ipftqent_t *tqe, *tqn;
7506ea8244dcSJohn Ojemann         int dropped;
7507ea8244dcSJohn Ojemann 	void *ent;
7508ea8244dcSJohn Ojemann 	nat_t *nat;
7509ea8244dcSJohn Ojemann 	ipstate_t *is;
7510ea8244dcSJohn Ojemann 
7511ea8244dcSJohn Ojemann         dropped = 0;
7512ea8244dcSJohn Ojemann 
7513ea8244dcSJohn Ojemann         /*
7514ea8244dcSJohn Ojemann          * Start by deleting any entries in specific timeout queues.
7515ea8244dcSJohn Ojemann          */
7516ea8244dcSJohn Ojemann 	ifqn = &ipfqs[stateval];
7517ea8244dcSJohn Ojemann         while ((ifq = ifqn) != NULL) {
7518ea8244dcSJohn Ojemann                 ifqn = ifq->ifq_next;
7519ea8244dcSJohn Ojemann                 dropped += ipf_earlydrop(flushtype, ifq, (int)0, ifs);
7520ea8244dcSJohn Ojemann         }
7521ea8244dcSJohn Ojemann 
7522ea8244dcSJohn Ojemann         /*
7523ea8244dcSJohn Ojemann          * Next, look through user defined queues for closing entries.
7524ea8244dcSJohn Ojemann          */
7525ea8244dcSJohn Ojemann 	ifqn = userqs;
7526ea8244dcSJohn Ojemann         while ((ifq = ifqn) != NULL) {
7527ea8244dcSJohn Ojemann                 ifqn = ifq->ifq_next;
7528ea8244dcSJohn Ojemann                 tqn = ifq->ifq_head;
7529ea8244dcSJohn Ojemann                 while ((tqe = tqn) != NULL) {
7530ea8244dcSJohn Ojemann                         tqn = tqe->tqe_next;
7531ea8244dcSJohn Ojemann 			ent = tqe->tqe_parent;
7532ea8244dcSJohn Ojemann 			switch (flushtype)
7533ea8244dcSJohn Ojemann 			{
7534ea8244dcSJohn Ojemann 			case NAT_FLUSH:
7535ea8244dcSJohn Ojemann 				nat = (nat_t *)ent;
7536ea8244dcSJohn Ojemann 				if ((nat->nat_p == IPPROTO_TCP) &&
7537ea8244dcSJohn Ojemann 				    (nat->nat_tcpstate[0] >= stateval) &&
7538ea8244dcSJohn Ojemann 				    (nat->nat_tcpstate[1] >= stateval) &&
7539ea8244dcSJohn Ojemann 				    (nat_delete(nat, NL_EXPIRE, ifs) == 0))
7540ea8244dcSJohn Ojemann 					dropped++;
7541ea8244dcSJohn Ojemann 				break;
7542ea8244dcSJohn Ojemann 			case STATE_FLUSH:
7543ea8244dcSJohn Ojemann 				is = (ipstate_t *)ent;
7544ea8244dcSJohn Ojemann 				if ((is->is_p == IPPROTO_TCP) &&
7545ea8244dcSJohn Ojemann 				    (is->is_state[0] >= stateval) &&
7546ea8244dcSJohn Ojemann 				    (is->is_state[1] >= stateval) &&
7547ea8244dcSJohn Ojemann 				    (fr_delstate(is, ISL_EXPIRE, ifs) == 0))
7548ea8244dcSJohn Ojemann 					dropped++;
7549ea8244dcSJohn Ojemann 				break;
7550ea8244dcSJohn Ojemann 			default:
7551ea8244dcSJohn Ojemann 				return (0);
7552ea8244dcSJohn Ojemann 			}
7553ea8244dcSJohn Ojemann                 }
7554ea8244dcSJohn Ojemann         }
7555ea8244dcSJohn Ojemann         return (dropped);
7556ea8244dcSJohn Ojemann }
7557ea8244dcSJohn Ojemann 
7558ea8244dcSJohn Ojemann 
7559ea8244dcSJohn Ojemann /* --------------------------------------------------------------------- */
7560ea8244dcSJohn Ojemann /* Function:    ipf_extraflush                                           */
7561ea8244dcSJohn Ojemann /* Returns:     int - number of entries flushed (0 = none)               */
7562ea8244dcSJohn Ojemann /* Parameters:	flushtype - which table we're cleaning (NAT or State)	 */
7563ea8244dcSJohn Ojemann /*              ipfqs - pointer to 'established' timeout queue           */
7564ea8244dcSJohn Ojemann /*              userqs - pointer to user defined queues                  */
7565ea8244dcSJohn Ojemann /*              ifs  - ipf stack instance                                */
7566ea8244dcSJohn Ojemann /*                                                                       */
7567ea8244dcSJohn Ojemann /* This function gets called when either NAT or state tables fill up.    */
7568ea8244dcSJohn Ojemann /* We need to try a bit harder to free up some space.  The function will */
7569ea8244dcSJohn Ojemann /* flush entries for TCP connections which have been idle a long time.   */
7570ea8244dcSJohn Ojemann /*                                                                       */
7571ea8244dcSJohn Ojemann /* Currently, the idle time is checked using values from ideltime_tab[]	 */
7572ea8244dcSJohn Ojemann /* --------------------------------------------------------------------- */
7573ea8244dcSJohn Ojemann int ipf_extraflush(flushtype, ipfqs, userqs, ifs)
7574ea8244dcSJohn Ojemann int flushtype;
7575ea8244dcSJohn Ojemann ipftq_t *ipfqs, *userqs;
7576ea8244dcSJohn Ojemann ipf_stack_t *ifs;
7577ea8244dcSJohn Ojemann {
7578ea8244dcSJohn Ojemann 	ipftq_t *ifq, *ifqn;
7579ea8244dcSJohn Ojemann 	int idletime, removed, idle_idx;
7580ea8244dcSJohn Ojemann 
7581ea8244dcSJohn Ojemann 	removed = 0;
7582ea8244dcSJohn Ojemann 
7583ea8244dcSJohn Ojemann 	/*
7584ea8244dcSJohn Ojemann   	 * Determine initial threshold for minimum idle time based on
7585ea8244dcSJohn Ojemann 	 * how long ipfilter has been running.  Ipfilter needs to have
7586ea8244dcSJohn Ojemann 	 * been up as long as the smallest interval to continue on.
7587ea8244dcSJohn Ojemann 	 *
7588ea8244dcSJohn Ojemann 	 * Minimum idle times stored in idletime_tab and indexed by
7589ea8244dcSJohn Ojemann 	 * idle_idx.  Start at upper end of array and work backwards.
7590ea8244dcSJohn Ojemann 	 *
7591ea8244dcSJohn Ojemann 	 * Once the index is found, set the initial idle time to the
7592ea8244dcSJohn Ojemann 	 * first interval before the current ipfilter run time.
7593ea8244dcSJohn Ojemann 	 */
7594ea8244dcSJohn Ojemann 	if (ifs->ifs_fr_ticks < idletime_tab[0])
7595ea8244dcSJohn Ojemann 		return (0);
7596ea8244dcSJohn Ojemann 	idle_idx = (sizeof (idletime_tab) / sizeof (int)) - 1;
7597ea8244dcSJohn Ojemann 	if (ifs->ifs_fr_ticks > idletime_tab[idle_idx]) {
7598ea8244dcSJohn Ojemann 		idletime = idletime_tab[idle_idx];
7599ea8244dcSJohn Ojemann 	} else {
7600ea8244dcSJohn Ojemann 		while ((idle_idx > 0) &&
7601ea8244dcSJohn Ojemann 		    (ifs->ifs_fr_ticks < idletime_tab[idle_idx]))
7602ea8244dcSJohn Ojemann 			idle_idx--;
7603ea8244dcSJohn Ojemann 
7604ea8244dcSJohn Ojemann 		idletime = (ifs->ifs_fr_ticks /
7605ea8244dcSJohn Ojemann 			    idletime_tab[idle_idx]) *
7606ea8244dcSJohn Ojemann 			    idletime_tab[idle_idx];
7607ea8244dcSJohn Ojemann 	}
7608ea8244dcSJohn Ojemann 
7609ea8244dcSJohn Ojemann 	while (idle_idx >= 0) {
7610ea8244dcSJohn Ojemann 		/*
7611ea8244dcSJohn Ojemann 		 * Check to see if we need to delete more entries.
7612ea8244dcSJohn Ojemann 		 * If we do, start with appropriate timeout queue.
7613ea8244dcSJohn Ojemann 		 */
7614ea8244dcSJohn Ojemann 		if (flushtype == NAT_FLUSH) {
7615ea8244dcSJohn Ojemann 			if (NAT_TAB_WATER_LEVEL(ifs) <=
7616ea8244dcSJohn Ojemann 			    ifs->ifs_nat_flush_level_lo)
7617ea8244dcSJohn Ojemann 				break;
7618ea8244dcSJohn Ojemann 		} else if (flushtype == STATE_FLUSH) {
7619ea8244dcSJohn Ojemann 			if (ST_TAB_WATER_LEVEL(ifs) <=
7620ea8244dcSJohn Ojemann 			    ifs->ifs_state_flush_level_lo)
7621ea8244dcSJohn Ojemann 				break;
7622ea8244dcSJohn Ojemann 		} else {
7623ea8244dcSJohn Ojemann 			break;
7624ea8244dcSJohn Ojemann 		}
7625ea8244dcSJohn Ojemann 
7626ea8244dcSJohn Ojemann 		removed += ipf_earlydrop(flushtype, ipfqs, idletime, ifs);
7627ea8244dcSJohn Ojemann 
7628ea8244dcSJohn Ojemann 		/*
7629ea8244dcSJohn Ojemann 		 * Next, check the user defined queues.  But first, make
7630ea8244dcSJohn Ojemann 		 * certain that timeout queue deletions didn't do enough.
7631ea8244dcSJohn Ojemann 		 */
7632ea8244dcSJohn Ojemann 		if (flushtype == NAT_FLUSH) {
7633ea8244dcSJohn Ojemann 			if (NAT_TAB_WATER_LEVEL(ifs) <=
7634ea8244dcSJohn Ojemann 			    ifs->ifs_nat_flush_level_lo)
7635ea8244dcSJohn Ojemann 				break;
7636ea8244dcSJohn Ojemann 		} else {
7637ea8244dcSJohn Ojemann 			if (ST_TAB_WATER_LEVEL(ifs) <=
7638ea8244dcSJohn Ojemann 			    ifs->ifs_state_flush_level_lo)
7639ea8244dcSJohn Ojemann 				break;
7640ea8244dcSJohn Ojemann 		}
7641ea8244dcSJohn Ojemann 		ifqn = userqs;
7642ea8244dcSJohn Ojemann 		while ((ifq = ifqn) != NULL) {
7643ea8244dcSJohn Ojemann 			ifqn = ifq->ifq_next;
7644ea8244dcSJohn Ojemann 			removed += ipf_earlydrop(flushtype, ifq, idletime, ifs);
7645ea8244dcSJohn Ojemann 		}
7646ea8244dcSJohn Ojemann 
7647ea8244dcSJohn Ojemann 		/*
7648ea8244dcSJohn Ojemann 		 * Adjust the granularity of idle time.
7649ea8244dcSJohn Ojemann 		 *
7650ea8244dcSJohn Ojemann 		 * If we reach an interval boundary, we need to
7651ea8244dcSJohn Ojemann 		 * either adjust the idle time accordingly or exit
7652ea8244dcSJohn Ojemann 		 * the loop altogether (if this is very last check).
7653ea8244dcSJohn Ojemann 		 */
7654ea8244dcSJohn Ojemann 		idletime -= idletime_tab[idle_idx];
7655ea8244dcSJohn Ojemann 		if (idletime < idletime_tab[idle_idx]) {
7656ea8244dcSJohn Ojemann 			if (idle_idx != 0) {
7657ea8244dcSJohn Ojemann 				idletime = idletime_tab[idle_idx] -
7658ea8244dcSJohn Ojemann 				idletime_tab[idle_idx - 1];
7659ea8244dcSJohn Ojemann 				idle_idx--;
7660ea8244dcSJohn Ojemann 			} else {
7661ea8244dcSJohn Ojemann 				break;
7662ea8244dcSJohn Ojemann 			}
7663ea8244dcSJohn Ojemann 		}
7664ea8244dcSJohn Ojemann 	}
7665ea8244dcSJohn Ojemann 
7666ea8244dcSJohn Ojemann 	return (removed);
7667ea8244dcSJohn Ojemann }
7668