Home | History | Annotate | Line # | Download | only in netinet
fil.c revision 1.15.4.3
      1  1.15.4.3     skrll /*	$NetBSD: fil.c,v 1.15.4.3 2017/08/28 17:52:34 skrll Exp $	*/
      2       1.1  christos 
      3       1.1  christos /*
      4       1.1  christos  * Copyright (C) 2012 by Darren Reed.
      5       1.1  christos  *
      6       1.1  christos  * See the IPFILTER.LICENCE file for details on licencing.
      7       1.1  christos  *
      8       1.3   darrenr  * Id: fil.c,v 1.1.1.2 2012/07/22 13:45:07 darrenr Exp $
      9       1.1  christos  *
     10       1.1  christos  */
     11       1.1  christos #if defined(KERNEL) || defined(_KERNEL)
     12       1.1  christos # undef KERNEL
     13       1.1  christos # undef _KERNEL
     14       1.1  christos # define        KERNEL	1
     15       1.1  christos # define        _KERNEL	1
     16       1.1  christos #endif
     17       1.1  christos #include <sys/errno.h>
     18       1.1  christos #include <sys/types.h>
     19       1.1  christos #include <sys/param.h>
     20       1.1  christos #include <sys/time.h>
     21       1.1  christos #if defined(_KERNEL) && defined(__FreeBSD_version) && \
     22       1.1  christos     (__FreeBSD_version >= 220000)
     23       1.1  christos # if (__FreeBSD_version >= 400000)
     24       1.1  christos #  if !defined(IPFILTER_LKM)
     25       1.1  christos #   include "opt_inet6.h"
     26       1.1  christos #  endif
     27       1.1  christos #  if (__FreeBSD_version == 400019)
     28       1.1  christos #   define CSUM_DELAY_DATA
     29       1.1  christos #  endif
     30       1.1  christos # endif
     31       1.1  christos # include <sys/filio.h>
     32       1.1  christos #else
     33       1.1  christos # include <sys/ioctl.h>
     34       1.1  christos #endif
     35       1.1  christos #if (defined(__SVR4) || defined(__svr4__)) && defined(sun)
     36       1.1  christos # include <sys/filio.h>
     37       1.1  christos #endif
     38       1.1  christos #if !defined(_AIX51)
     39       1.1  christos # include <sys/fcntl.h>
     40       1.1  christos #endif
     41       1.1  christos #if defined(_KERNEL)
     42       1.1  christos # include <sys/systm.h>
     43       1.1  christos # include <sys/file.h>
     44       1.1  christos #else
     45       1.1  christos # include <stdio.h>
     46       1.1  christos # include <string.h>
     47       1.1  christos # include <stdlib.h>
     48       1.1  christos # include <stddef.h>
     49       1.1  christos # include <sys/file.h>
     50       1.1  christos # define _KERNEL
     51       1.1  christos # ifdef __OpenBSD__
     52       1.1  christos struct file;
     53       1.1  christos # endif
     54       1.1  christos # include <sys/uio.h>
     55       1.1  christos # undef _KERNEL
     56       1.1  christos #endif
     57       1.1  christos #if !defined(__SVR4) && !defined(__svr4__) && !defined(__hpux) && \
     58       1.1  christos     !defined(linux)
     59       1.1  christos # include <sys/mbuf.h>
     60       1.1  christos #else
     61       1.1  christos # if !defined(linux)
     62       1.1  christos #  include <sys/byteorder.h>
     63       1.1  christos # endif
     64       1.1  christos # if (SOLARIS2 < 5) && defined(sun)
     65       1.1  christos #  include <sys/dditypes.h>
     66       1.1  christos # endif
     67       1.1  christos #endif
     68       1.1  christos #ifdef __hpux
     69       1.1  christos # define _NET_ROUTE_INCLUDED
     70       1.1  christos #endif
     71       1.1  christos #if !defined(linux)
     72       1.1  christos # include <sys/protosw.h>
     73       1.1  christos #endif
     74       1.1  christos #include <sys/socket.h>
     75       1.1  christos #include <net/if.h>
     76       1.1  christos #ifdef sun
     77       1.1  christos # include <net/af.h>
     78       1.1  christos #endif
     79       1.1  christos #include <netinet/in.h>
     80       1.1  christos #include <netinet/in_systm.h>
     81       1.1  christos #include <netinet/ip.h>
     82       1.1  christos #if defined(__sgi) && defined(IFF_DRVRLOCK) /* IRIX 6 */
     83       1.1  christos # include <sys/hashing.h>
     84       1.1  christos # include <netinet/in_var.h>
     85       1.1  christos #endif
     86       1.1  christos #include <netinet/tcp.h>
     87       1.1  christos #if (!defined(__sgi) && !defined(AIX)) || defined(_KERNEL)
     88       1.1  christos # include <netinet/udp.h>
     89       1.1  christos # include <netinet/ip_icmp.h>
     90       1.1  christos #endif
     91       1.1  christos #ifdef __hpux
     92       1.1  christos # undef _NET_ROUTE_INCLUDED
     93       1.1  christos #endif
     94       1.1  christos #ifdef __osf__
     95       1.1  christos # undef _RADIX_H_
     96       1.1  christos #endif
     97       1.1  christos #include "netinet/ip_compat.h"
     98       1.1  christos #ifdef	USE_INET6
     99       1.1  christos # include <netinet/icmp6.h>
    100       1.1  christos # if !SOLARIS && defined(_KERNEL) && !defined(__osf__) && !defined(__hpux)
    101       1.1  christos #  include <netinet6/in6_var.h>
    102       1.1  christos # endif
    103       1.1  christos #endif
    104       1.1  christos #include "netinet/ip_fil.h"
    105       1.1  christos #include "netinet/ip_nat.h"
    106       1.1  christos #include "netinet/ip_frag.h"
    107       1.1  christos #include "netinet/ip_state.h"
    108       1.1  christos #include "netinet/ip_proxy.h"
    109       1.1  christos #include "netinet/ip_auth.h"
    110       1.1  christos #ifdef IPFILTER_SCAN
    111       1.1  christos # include "netinet/ip_scan.h"
    112       1.1  christos #endif
    113       1.1  christos #include "netinet/ip_sync.h"
    114       1.1  christos #include "netinet/ip_lookup.h"
    115       1.1  christos #include "netinet/ip_pool.h"
    116       1.1  christos #include "netinet/ip_htable.h"
    117       1.1  christos #ifdef IPFILTER_COMPILED
    118       1.1  christos # include "netinet/ip_rules.h"
    119       1.1  christos #endif
    120       1.1  christos #if defined(IPFILTER_BPF) && defined(_KERNEL)
    121       1.1  christos # include <net/bpf.h>
    122       1.1  christos #endif
    123       1.1  christos #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
    124       1.1  christos # include <sys/malloc.h>
    125       1.1  christos #endif
    126       1.1  christos #include "netinet/ipl.h"
    127       1.1  christos 
    128       1.1  christos #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
    129       1.1  christos # include <sys/callout.h>
    130       1.1  christos extern struct callout ipf_slowtimer_ch;
    131       1.1  christos #endif
    132       1.1  christos #if defined(__OpenBSD__)
    133       1.1  christos # include <sys/timeout.h>
    134       1.1  christos extern struct timeout ipf_slowtimer_ch;
    135       1.1  christos #endif
    136       1.1  christos /* END OF INCLUDES */
    137       1.1  christos 
    138       1.1  christos #if !defined(lint)
    139       1.2  christos #if defined(__NetBSD__)
    140       1.2  christos #include <sys/cdefs.h>
    141  1.15.4.3     skrll __KERNEL_RCSID(0, "$NetBSD: fil.c,v 1.15.4.3 2017/08/28 17:52:34 skrll Exp $");
    142       1.2  christos #else
    143       1.1  christos static const char sccsid[] = "@(#)fil.c	1.36 6/5/96 (C) 1993-2000 Darren Reed";
    144       1.3   darrenr static const char rcsid[] = "@(#)Id: fil.c,v 1.1.1.2 2012/07/22 13:45:07 darrenr Exp $";
    145       1.2  christos #endif
    146       1.1  christos #endif
    147       1.1  christos 
    148       1.1  christos #ifndef	_KERNEL
    149       1.1  christos # include "ipf.h"
    150       1.1  christos # include "ipt.h"
    151       1.1  christos extern	int	opts;
    152       1.1  christos extern	int	blockreason;
    153       1.1  christos #endif /* _KERNEL */
    154       1.1  christos 
    155       1.1  christos #define	LBUMP(x)	softc->x++
    156       1.1  christos #define	LBUMPD(x, y)	do { softc->x.y++; DT(y); } while (0)
    157       1.1  christos 
    158       1.2  christos static	INLINE int	ipf_check_ipf(fr_info_t *, frentry_t *, int);
    159       1.2  christos static	u_32_t		ipf_checkcipso(fr_info_t *, u_char *, int);
    160       1.2  christos static	u_32_t		ipf_checkripso(u_char *);
    161       1.2  christos static	u_32_t		ipf_decaps(fr_info_t *, u_32_t, int);
    162       1.2  christos #ifdef	IPFILTER_LOG
    163       1.2  christos static	frentry_t	*ipf_dolog(fr_info_t *, u_32_t *);
    164       1.2  christos #endif
    165       1.3   darrenr static	int		ipf_flushlist(ipf_main_softc_t *, int *, frentry_t **);
    166       1.3   darrenr static	int		ipf_flush_groups(ipf_main_softc_t *, frgroup_t **, int);
    167       1.2  christos static	ipfunc_t	ipf_findfunc(ipfunc_t);
    168       1.2  christos static	void		*ipf_findlookup(ipf_main_softc_t *, int, frentry_t *,
    169       1.2  christos 					i6addr_t *, i6addr_t *);
    170       1.2  christos static	frentry_t	*ipf_firewall(fr_info_t *, u_32_t *);
    171       1.2  christos static	int		ipf_fr_matcharray(fr_info_t *, int *);
    172       1.2  christos static	int		ipf_frruleiter(ipf_main_softc_t *, void *, int, void *);
    173       1.2  christos static	void		ipf_funcfini(ipf_main_softc_t *, frentry_t *);;
    174       1.2  christos static	int		ipf_funcinit(ipf_main_softc_t *, frentry_t *);
    175       1.2  christos static	int		ipf_geniter(ipf_main_softc_t *, ipftoken_t *,
    176       1.2  christos 				    ipfgeniter_t *);
    177       1.2  christos static	void		ipf_getstat(ipf_main_softc_t *,
    178       1.2  christos 				    struct friostat *, int);
    179       1.3   darrenr static	int		ipf_group_flush(ipf_main_softc_t *, frgroup_t *);
    180       1.3   darrenr static	void		ipf_group_free(frgroup_t *);
    181       1.2  christos static	int		ipf_grpmapfini(struct ipf_main_softc_s *, frentry_t *);
    182       1.2  christos static	int		ipf_grpmapinit(struct ipf_main_softc_s *, frentry_t *);
    183       1.3   darrenr static	frentry_t	*ipf_nextrule(ipf_main_softc_t *, int, int,
    184       1.3   darrenr 					frentry_t *, int);
    185       1.2  christos static	int		ipf_portcheck(frpcmp_t *, u_32_t);
    186       1.2  christos static	INLINE int	ipf_pr_ah(fr_info_t *);
    187       1.2  christos static	INLINE void	ipf_pr_esp(fr_info_t *);
    188       1.2  christos static	INLINE void	ipf_pr_gre(fr_info_t *);
    189       1.2  christos static	INLINE void	ipf_pr_udp(fr_info_t *);
    190       1.2  christos static	INLINE void	ipf_pr_tcp(fr_info_t *);
    191       1.2  christos static	INLINE void	ipf_pr_icmp(fr_info_t *);
    192       1.2  christos static	INLINE void	ipf_pr_ipv4hdr(fr_info_t *);
    193       1.2  christos static	INLINE void	ipf_pr_short(fr_info_t *, int);
    194       1.2  christos static	INLINE int	ipf_pr_tcpcommon(fr_info_t *);
    195       1.2  christos static	INLINE int	ipf_pr_udpcommon(fr_info_t *);
    196       1.2  christos static	void		ipf_rule_delete(ipf_main_softc_t *, frentry_t *f,
    197       1.2  christos 					int, int);
    198       1.2  christos static	void		ipf_rule_expire_insert(ipf_main_softc_t *,
    199       1.2  christos 					       frentry_t *, int);
    200       1.2  christos static	int		ipf_synclist(ipf_main_softc_t *, frentry_t *, void *);
    201       1.3   darrenr static	void		ipf_token_flush(ipf_main_softc_t *);
    202       1.3   darrenr static	void		ipf_token_unlink(ipf_main_softc_t *, ipftoken_t *);
    203       1.2  christos static	ipftuneable_t	*ipf_tune_findbyname(ipftuneable_t *, const char *);
    204       1.2  christos static	ipftuneable_t	*ipf_tune_findbycookie(ipftuneable_t **, void *,
    205       1.2  christos 					       void **);
    206       1.2  christos static	int		ipf_updateipid(fr_info_t *);
    207       1.2  christos static	int		ipf_settimeout(struct ipf_main_softc_s *,
    208       1.2  christos 				       struct ipftuneable *, ipftuneval_t *);
    209       1.1  christos 
    210       1.1  christos 
    211       1.1  christos /*
    212       1.1  christos  * bit values for identifying presence of individual IP options
    213       1.1  christos  * All of these tables should be ordered by increasing key value on the left
    214       1.1  christos  * hand side to allow for binary searching of the array and include a trailer
    215       1.1  christos  * with a 0 for the bitmask for linear searches to easily find the end with.
    216       1.1  christos  */
    217       1.1  christos static const	struct	optlist	ipopts[20] = {
    218       1.1  christos 	{ IPOPT_NOP,	0x000001 },
    219       1.1  christos 	{ IPOPT_RR,	0x000002 },
    220       1.1  christos 	{ IPOPT_ZSU,	0x000004 },
    221       1.1  christos 	{ IPOPT_MTUP,	0x000008 },
    222       1.1  christos 	{ IPOPT_MTUR,	0x000010 },
    223       1.1  christos 	{ IPOPT_ENCODE,	0x000020 },
    224       1.1  christos 	{ IPOPT_TS,	0x000040 },
    225       1.1  christos 	{ IPOPT_TR,	0x000080 },
    226       1.1  christos 	{ IPOPT_SECURITY, 0x000100 },
    227       1.1  christos 	{ IPOPT_LSRR,	0x000200 },
    228       1.1  christos 	{ IPOPT_E_SEC,	0x000400 },
    229       1.1  christos 	{ IPOPT_CIPSO,	0x000800 },
    230       1.1  christos 	{ IPOPT_SATID,	0x001000 },
    231       1.1  christos 	{ IPOPT_SSRR,	0x002000 },
    232       1.1  christos 	{ IPOPT_ADDEXT,	0x004000 },
    233       1.1  christos 	{ IPOPT_VISA,	0x008000 },
    234       1.1  christos 	{ IPOPT_IMITD,	0x010000 },
    235       1.1  christos 	{ IPOPT_EIP,	0x020000 },
    236       1.1  christos 	{ IPOPT_FINN,	0x040000 },
    237       1.1  christos 	{ 0,		0x000000 }
    238       1.1  christos };
    239       1.1  christos 
    240       1.1  christos #ifdef USE_INET6
    241  1.15.4.2     skrll static const struct optlist ip6exthdr[] = {
    242       1.1  christos 	{ IPPROTO_HOPOPTS,		0x000001 },
    243       1.1  christos 	{ IPPROTO_IPV6,			0x000002 },
    244       1.1  christos 	{ IPPROTO_ROUTING,		0x000004 },
    245       1.1  christos 	{ IPPROTO_FRAGMENT,		0x000008 },
    246       1.1  christos 	{ IPPROTO_ESP,			0x000010 },
    247       1.1  christos 	{ IPPROTO_AH,			0x000020 },
    248       1.1  christos 	{ IPPROTO_NONE,			0x000040 },
    249       1.1  christos 	{ IPPROTO_DSTOPTS,		0x000080 },
    250       1.1  christos 	{ IPPROTO_MOBILITY,		0x000100 },
    251       1.1  christos 	{ 0,				0 }
    252       1.1  christos };
    253       1.1  christos #endif
    254       1.1  christos 
    255       1.1  christos /*
    256       1.1  christos  * bit values for identifying presence of individual IP security options
    257       1.1  christos  */
    258       1.1  christos static const	struct	optlist	secopt[8] = {
    259       1.1  christos 	{ IPSO_CLASS_RES4,	0x01 },
    260       1.1  christos 	{ IPSO_CLASS_TOPS,	0x02 },
    261       1.1  christos 	{ IPSO_CLASS_SECR,	0x04 },
    262       1.1  christos 	{ IPSO_CLASS_RES3,	0x08 },
    263       1.1  christos 	{ IPSO_CLASS_CONF,	0x10 },
    264       1.1  christos 	{ IPSO_CLASS_UNCL,	0x20 },
    265       1.1  christos 	{ IPSO_CLASS_RES2,	0x40 },
    266       1.1  christos 	{ IPSO_CLASS_RES1,	0x80 }
    267       1.1  christos };
    268       1.1  christos 
    269       1.1  christos char	ipfilter_version[] = IPL_VERSION;
    270       1.1  christos 
    271       1.1  christos int	ipf_features = 0
    272       1.1  christos #ifdef	IPFILTER_LKM
    273       1.1  christos 		| IPF_FEAT_LKM
    274       1.1  christos #endif
    275       1.1  christos #ifdef	IPFILTER_LOG
    276       1.1  christos 		| IPF_FEAT_LOG
    277       1.1  christos #endif
    278       1.1  christos 		| IPF_FEAT_LOOKUP
    279       1.1  christos #ifdef	IPFILTER_BPF
    280       1.1  christos 		| IPF_FEAT_BPF
    281       1.1  christos #endif
    282       1.1  christos #ifdef	IPFILTER_COMPILED
    283       1.1  christos 		| IPF_FEAT_COMPILED
    284       1.1  christos #endif
    285       1.1  christos #ifdef	IPFILTER_CKSUM
    286       1.1  christos 		| IPF_FEAT_CKSUM
    287       1.1  christos #endif
    288       1.1  christos 		| IPF_FEAT_SYNC
    289       1.1  christos #ifdef	IPFILTER_SCAN
    290       1.1  christos 		| IPF_FEAT_SCAN
    291       1.1  christos #endif
    292       1.1  christos #ifdef	USE_INET6
    293       1.1  christos 		| IPF_FEAT_IPV6
    294       1.1  christos #endif
    295       1.1  christos 	;
    296       1.1  christos 
    297       1.1  christos 
    298       1.1  christos /*
    299       1.1  christos  * Table of functions available for use with call rules.
    300       1.1  christos  */
    301       1.1  christos static ipfunc_resolve_t ipf_availfuncs[] = {
    302       1.1  christos 	{ "srcgrpmap", ipf_srcgrpmap, ipf_grpmapinit, ipf_grpmapfini },
    303       1.1  christos 	{ "dstgrpmap", ipf_dstgrpmap, ipf_grpmapinit, ipf_grpmapfini },
    304       1.2  christos 	{ "",	       NULL,	      NULL,	      NULL }
    305       1.1  christos };
    306       1.1  christos 
    307       1.1  christos static ipftuneable_t ipf_main_tuneables[] = {
    308       1.1  christos 	{ { (void *)offsetof(struct ipf_main_softc_s, ipf_flags) },
    309       1.1  christos 		"ipf_flags",		0,	0xffffffff,
    310       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_flags),
    311       1.1  christos 		0,			NULL,	NULL },
    312       1.1  christos 	{ { (void *)offsetof(struct ipf_main_softc_s, ipf_active) },
    313       1.1  christos 		"active",		0,	0,
    314       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_active),
    315       1.1  christos 		IPFT_RDONLY,		NULL,	NULL },
    316       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_control_forwarding) },
    317       1.1  christos 		"control_forwarding",	0, 1,
    318       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_control_forwarding),
    319       1.1  christos 		0,			NULL,	NULL },
    320       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_update_ipid) },
    321       1.1  christos 		"update_ipid",		0,	1,
    322       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_update_ipid),
    323       1.1  christos 		0,			NULL,	NULL },
    324       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_chksrc) },
    325       1.1  christos 		"chksrc",		0,	1,
    326       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_chksrc),
    327       1.1  christos 		0,			NULL,	NULL },
    328       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_minttl) },
    329       1.1  christos 		"min_ttl",		0,	1,
    330       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_minttl),
    331       1.1  christos 		0,			NULL,	NULL },
    332       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_icmpminfragmtu) },
    333       1.1  christos 		"icmp_minfragmtu",	0,	1,
    334       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_icmpminfragmtu),
    335       1.1  christos 		0,			NULL,	NULL },
    336       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_pass) },
    337       1.1  christos 		"default_pass",		0,	0xffffffff,
    338       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_pass),
    339       1.1  christos 		0,			NULL,	NULL },
    340       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpidletimeout) },
    341       1.1  christos 		"tcp_idle_timeout",	1,	0x7fffffff,
    342       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcpidletimeout),
    343       1.1  christos 		0,			NULL,	ipf_settimeout },
    344       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosewait) },
    345       1.1  christos 		"tcp_close_wait",	1,	0x7fffffff,
    346       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcpclosewait),
    347       1.1  christos 		0,			NULL,	ipf_settimeout },
    348       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcplastack) },
    349       1.1  christos 		"tcp_last_ack",		1,	0x7fffffff,
    350       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcplastack),
    351       1.1  christos 		0,			NULL,	ipf_settimeout },
    352       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcptimeout) },
    353       1.1  christos 		"tcp_timeout",		1,	0x7fffffff,
    354       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcptimeout),
    355       1.1  christos 		0,			NULL,	ipf_settimeout },
    356       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynsent) },
    357       1.1  christos 		"tcp_syn_sent",		1,	0x7fffffff,
    358       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcpsynsent),
    359       1.1  christos 		0,			NULL,	ipf_settimeout },
    360       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpsynrecv) },
    361       1.1  christos 		"tcp_syn_received",	1,	0x7fffffff,
    362       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcpsynrecv),
    363       1.1  christos 		0,			NULL,	ipf_settimeout },
    364       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcpclosed) },
    365       1.1  christos 		"tcp_closed",		1,	0x7fffffff,
    366       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcpclosed),
    367       1.1  christos 		0,			NULL,	ipf_settimeout },
    368       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcphalfclosed) },
    369       1.1  christos 		"tcp_half_closed",	1,	0x7fffffff,
    370       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcphalfclosed),
    371       1.1  christos 		0,			NULL,	ipf_settimeout },
    372       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_tcptimewait) },
    373       1.1  christos 		"tcp_time_wait",	1,	0x7fffffff,
    374       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_tcptimewait),
    375       1.1  christos 		0,			NULL,	ipf_settimeout },
    376       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_udptimeout) },
    377       1.1  christos 		"udp_timeout",		1,	0x7fffffff,
    378       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_udptimeout),
    379       1.1  christos 		0,			NULL,	ipf_settimeout },
    380       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_udpacktimeout) },
    381       1.1  christos 		"udp_ack_timeout",	1,	0x7fffffff,
    382       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_udpacktimeout),
    383       1.1  christos 		0,			NULL,	ipf_settimeout },
    384       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_icmptimeout) },
    385       1.1  christos 		"icmp_timeout",		1,	0x7fffffff,
    386       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_icmptimeout),
    387       1.1  christos 		0,			NULL,	ipf_settimeout },
    388       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_icmpacktimeout) },
    389       1.1  christos 		"icmp_ack_timeout",	1,	0x7fffffff,
    390       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_icmpacktimeout),
    391       1.1  christos 		0,			NULL,	ipf_settimeout },
    392       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_iptimeout) },
    393       1.1  christos 		"ip_timeout",		1,	0x7fffffff,
    394       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_iptimeout),
    395       1.1  christos 		0,			NULL,	ipf_settimeout },
    396       1.1  christos #if defined(INSTANCES) && defined(_KERNEL)
    397       1.1  christos 	{ { (void *)offsetof(ipf_main_softc_t, ipf_get_loopback) },
    398       1.1  christos 		"intercept_loopback",	0,	1,
    399       1.1  christos 		stsizeof(ipf_main_softc_t, ipf_get_loopback),
    400       1.1  christos 		0,			NULL,	ipf_set_loopback },
    401       1.1  christos #endif
    402       1.1  christos 	{ { 0 },
    403       1.1  christos 		NULL,			0,	0,
    404       1.1  christos 		0,
    405       1.1  christos 		0,			NULL,	NULL }
    406       1.1  christos };
    407       1.1  christos 
    408       1.1  christos 
    409       1.1  christos /*
    410       1.1  christos  * The next section of code is a a collection of small routines that set
    411       1.1  christos  * fields in the fr_info_t structure passed based on properties of the
    412       1.1  christos  * current packet.  There are different routines for the same protocol
    413       1.1  christos  * for each of IPv4 and IPv6.  Adding a new protocol, for which there
    414       1.1  christos  * will "special" inspection for setup, is now more easily done by adding
    415       1.1  christos  * a new routine and expanding the ipf_pr_ipinit*() function rather than by
    416       1.1  christos  * adding more code to a growing switch statement.
    417       1.1  christos  */
    418       1.1  christos #ifdef USE_INET6
    419       1.2  christos static	INLINE int	ipf_pr_ah6(fr_info_t *);
    420       1.2  christos static	INLINE void	ipf_pr_esp6(fr_info_t *);
    421       1.2  christos static	INLINE void	ipf_pr_gre6(fr_info_t *);
    422       1.2  christos static	INLINE void	ipf_pr_udp6(fr_info_t *);
    423       1.2  christos static	INLINE void	ipf_pr_tcp6(fr_info_t *);
    424       1.2  christos static	INLINE void	ipf_pr_icmp6(fr_info_t *);
    425       1.2  christos static	INLINE void	ipf_pr_ipv6hdr(fr_info_t *);
    426       1.2  christos static	INLINE void	ipf_pr_short6(fr_info_t *, int);
    427       1.2  christos static	INLINE int	ipf_pr_hopopts6(fr_info_t *);
    428       1.2  christos static	INLINE int	ipf_pr_mobility6(fr_info_t *);
    429       1.2  christos static	INLINE int	ipf_pr_routing6(fr_info_t *);
    430       1.2  christos static	INLINE int	ipf_pr_dstopts6(fr_info_t *);
    431       1.2  christos static	INLINE int	ipf_pr_fragment6(fr_info_t *);
    432       1.2  christos static	INLINE struct ip6_ext *ipf_pr_ipv6exthdr(fr_info_t *, int, int);
    433       1.1  christos 
    434       1.1  christos 
    435       1.1  christos /* ------------------------------------------------------------------------ */
    436       1.1  christos /* Function:    ipf_pr_short6                                               */
    437       1.1  christos /* Returns:     void                                                        */
    438       1.1  christos /* Parameters:  fin(I)  - pointer to packet information                     */
    439       1.1  christos /*              xmin(I) - minimum header size                               */
    440       1.1  christos /*                                                                          */
    441       1.1  christos /* IPv6 Only                                                                */
    442       1.1  christos /* This is function enforces the 'is a packet too short to be legit' rule   */
    443       1.1  christos /* for IPv6 and marks the packet with FI_SHORT if so.  See function comment */
    444       1.1  christos /* for ipf_pr_short() for more details.                                     */
    445       1.1  christos /* ------------------------------------------------------------------------ */
    446       1.1  christos static INLINE void
    447       1.2  christos ipf_pr_short6(fr_info_t *fin, int xmin)
    448       1.1  christos {
    449       1.1  christos 
    450       1.1  christos 	if (fin->fin_dlen < xmin)
    451       1.1  christos 		fin->fin_flx |= FI_SHORT;
    452       1.1  christos }
    453       1.1  christos 
    454       1.1  christos 
    455       1.1  christos /* ------------------------------------------------------------------------ */
    456       1.1  christos /* Function:    ipf_pr_ipv6hdr                                              */
    457       1.1  christos /* Returns:     void                                                        */
    458       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    459       1.1  christos /*                                                                          */
    460       1.1  christos /* IPv6 Only                                                                */
    461       1.1  christos /* Copy values from the IPv6 header into the fr_info_t struct and call the  */
    462       1.1  christos /* per-protocol analyzer if it exists.  In validating the packet, a protocol*/
    463       1.1  christos /* analyzer may pullup or free the packet itself so we need to be vigiliant */
    464       1.1  christos /* of that possibility arising.                                             */
    465       1.1  christos /* ------------------------------------------------------------------------ */
    466       1.1  christos static INLINE void
    467       1.2  christos ipf_pr_ipv6hdr(fr_info_t *fin)
    468       1.1  christos {
    469       1.1  christos 	ip6_t *ip6 = (ip6_t *)fin->fin_ip;
    470       1.1  christos 	int p, go = 1, i, hdrcount;
    471       1.1  christos 	fr_ip_t *fi = &fin->fin_fi;
    472       1.1  christos 
    473       1.1  christos 	fin->fin_off = 0;
    474       1.1  christos 
    475       1.1  christos 	fi->fi_tos = 0;
    476       1.1  christos 	fi->fi_optmsk = 0;
    477       1.1  christos 	fi->fi_secmsk = 0;
    478       1.1  christos 	fi->fi_auth = 0;
    479       1.1  christos 
    480       1.1  christos 	p = ip6->ip6_nxt;
    481       1.1  christos 	fin->fin_crc = p;
    482       1.1  christos 	fi->fi_ttl = ip6->ip6_hlim;
    483       1.1  christos 	fi->fi_src.in6 = ip6->ip6_src;
    484       1.1  christos 	fin->fin_crc += fi->fi_src.i6[0];
    485       1.1  christos 	fin->fin_crc += fi->fi_src.i6[1];
    486       1.1  christos 	fin->fin_crc += fi->fi_src.i6[2];
    487       1.1  christos 	fin->fin_crc += fi->fi_src.i6[3];
    488       1.1  christos 	fi->fi_dst.in6 = ip6->ip6_dst;
    489       1.1  christos 	fin->fin_crc += fi->fi_dst.i6[0];
    490       1.1  christos 	fin->fin_crc += fi->fi_dst.i6[1];
    491       1.1  christos 	fin->fin_crc += fi->fi_dst.i6[2];
    492       1.1  christos 	fin->fin_crc += fi->fi_dst.i6[3];
    493       1.1  christos 	fin->fin_id = 0;
    494       1.1  christos 	if (IN6_IS_ADDR_MULTICAST(&fi->fi_dst.in6))
    495       1.1  christos 		fin->fin_flx |= FI_MULTICAST|FI_MBCAST;
    496       1.1  christos 
    497       1.1  christos 	hdrcount = 0;
    498       1.1  christos 	while (go && !(fin->fin_flx & FI_SHORT)) {
    499       1.1  christos 		switch (p)
    500       1.1  christos 		{
    501       1.1  christos 		case IPPROTO_UDP :
    502       1.1  christos 			ipf_pr_udp6(fin);
    503       1.1  christos 			go = 0;
    504       1.1  christos 			break;
    505       1.1  christos 
    506       1.1  christos 		case IPPROTO_TCP :
    507       1.1  christos 			ipf_pr_tcp6(fin);
    508       1.1  christos 			go = 0;
    509       1.1  christos 			break;
    510       1.1  christos 
    511       1.1  christos 		case IPPROTO_ICMPV6 :
    512       1.1  christos 			ipf_pr_icmp6(fin);
    513       1.1  christos 			go = 0;
    514       1.1  christos 			break;
    515       1.1  christos 
    516       1.1  christos 		case IPPROTO_GRE :
    517       1.1  christos 			ipf_pr_gre6(fin);
    518       1.1  christos 			go = 0;
    519       1.1  christos 			break;
    520       1.1  christos 
    521       1.1  christos 		case IPPROTO_HOPOPTS :
    522       1.1  christos 			p = ipf_pr_hopopts6(fin);
    523       1.1  christos 			break;
    524       1.1  christos 
    525       1.1  christos 		case IPPROTO_MOBILITY :
    526       1.1  christos 			p = ipf_pr_mobility6(fin);
    527       1.1  christos 			break;
    528       1.1  christos 
    529       1.1  christos 		case IPPROTO_DSTOPTS :
    530       1.1  christos 			p = ipf_pr_dstopts6(fin);
    531       1.1  christos 			break;
    532       1.1  christos 
    533       1.1  christos 		case IPPROTO_ROUTING :
    534       1.1  christos 			p = ipf_pr_routing6(fin);
    535       1.1  christos 			break;
    536       1.1  christos 
    537       1.1  christos 		case IPPROTO_AH :
    538       1.1  christos 			p = ipf_pr_ah6(fin);
    539       1.1  christos 			break;
    540       1.1  christos 
    541       1.1  christos 		case IPPROTO_ESP :
    542       1.1  christos 			ipf_pr_esp6(fin);
    543       1.1  christos 			go = 0;
    544       1.1  christos 			break;
    545       1.1  christos 
    546       1.1  christos 		case IPPROTO_IPV6 :
    547       1.1  christos 			for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
    548       1.1  christos 				if (ip6exthdr[i].ol_val == p) {
    549       1.1  christos 					fin->fin_flx |= ip6exthdr[i].ol_bit;
    550       1.1  christos 					break;
    551       1.1  christos 				}
    552       1.1  christos 			go = 0;
    553       1.1  christos 			break;
    554       1.1  christos 
    555       1.1  christos 		case IPPROTO_NONE :
    556       1.1  christos 			go = 0;
    557       1.1  christos 			break;
    558       1.1  christos 
    559       1.1  christos 		case IPPROTO_FRAGMENT :
    560       1.1  christos 			p = ipf_pr_fragment6(fin);
    561       1.1  christos 			/*
    562       1.1  christos 			 * Given that the only fragments we want to let through
    563       1.1  christos 			 * (where fin_off != 0) are those where the non-first
    564       1.1  christos 			 * fragments only have data, we can safely stop looking
    565       1.1  christos 			 * at headers if this is a non-leading fragment.
    566       1.1  christos 			 */
    567       1.1  christos 			if (fin->fin_off != 0)
    568       1.1  christos 				go = 0;
    569       1.1  christos 			break;
    570       1.1  christos 
    571       1.1  christos 		default :
    572       1.1  christos 			go = 0;
    573       1.1  christos 			break;
    574       1.1  christos 		}
    575       1.1  christos 		hdrcount++;
    576       1.1  christos 
    577       1.1  christos 		/*
    578       1.1  christos 		 * It is important to note that at this point, for the
    579       1.1  christos 		 * extension headers (go != 0), the entire header may not have
    580       1.1  christos 		 * been pulled up when the code gets to this point.  This is
    581       1.1  christos 		 * only done for "go != 0" because the other header handlers
    582       1.1  christos 		 * will all pullup their complete header.  The other indicator
    583       1.1  christos 		 * of an incomplete packet is that this was just an extension
    584       1.1  christos 		 * header.
    585       1.1  christos 		 */
    586       1.1  christos 		if ((go != 0) && (p != IPPROTO_NONE) &&
    587       1.1  christos 		    (ipf_pr_pullup(fin, 0) == -1)) {
    588       1.1  christos 			p = IPPROTO_NONE;
    589       1.1  christos 			break;
    590       1.1  christos 		}
    591       1.1  christos 	}
    592       1.1  christos 
    593       1.1  christos 	/*
    594       1.1  christos 	 * Some of the above functions, like ipf_pr_esp6(), can call ipf_pullup
    595       1.1  christos 	 * and destroy whatever packet was here.  The caller of this function
    596       1.1  christos 	 * expects us to return if there is a problem with ipf_pullup.
    597       1.1  christos 	 */
    598       1.1  christos 	if (fin->fin_m == NULL) {
    599       1.1  christos 		ipf_main_softc_t *softc = fin->fin_main_soft;
    600       1.1  christos 
    601       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_bad);
    602       1.1  christos 		return;
    603       1.1  christos 	}
    604       1.1  christos 
    605       1.1  christos 	fi->fi_p = p;
    606       1.1  christos 
    607       1.1  christos 	/*
    608       1.1  christos 	 * IPv6 fragment case 1 - see comment for ipf_pr_fragment6().
    609       1.1  christos 	 * "go != 0" imples the above loop hasn't arrived at a layer 4 header.
    610       1.1  christos 	 */
    611       1.1  christos 	if ((go != 0) && (fin->fin_flx & FI_FRAG) && (fin->fin_off == 0)) {
    612       1.1  christos 		ipf_main_softc_t *softc = fin->fin_main_soft;
    613       1.1  christos 
    614       1.1  christos 		fin->fin_flx |= FI_BAD;
    615  1.15.4.2     skrll 		DT2(ipf_fi_bad_ipv6_frag_1, fr_info_t *, fin, int, go);
    616       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_badfrag);
    617       1.1  christos 		LBUMP(ipf_stats[fin->fin_out].fr_v6_bad);
    618       1.1  christos 	}
    619       1.1  christos }
    620       1.1  christos 
    621       1.1  christos 
    622       1.1  christos /* ------------------------------------------------------------------------ */
    623       1.1  christos /* Function:    ipf_pr_ipv6exthdr                                           */
    624       1.1  christos /* Returns:     struct ip6_ext * - pointer to the start of the next header  */
    625       1.1  christos /*                                 or NULL if there is a prolblem.          */
    626       1.1  christos /* Parameters:  fin(I)      - pointer to packet information                 */
    627       1.1  christos /*              multiple(I) - flag indicating yes/no if multiple occurances */
    628       1.1  christos /*                            of this extension header are allowed.         */
    629       1.1  christos /*              proto(I)    - protocol number for this extension header     */
    630       1.1  christos /*                                                                          */
    631       1.1  christos /* IPv6 Only                                                                */
    632       1.1  christos /* This function embodies a number of common checks that all IPv6 extension */
    633       1.1  christos /* headers must be subjected to.  For example, making sure the packet is    */
    634       1.1  christos /* big enough for it to be in, checking if it is repeated and setting a     */
    635       1.1  christos /* flag to indicate its presence.                                           */
    636       1.1  christos /* ------------------------------------------------------------------------ */
    637       1.1  christos static INLINE struct ip6_ext *
    638       1.2  christos ipf_pr_ipv6exthdr(fr_info_t *fin, int multiple, int proto)
    639       1.1  christos {
    640       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
    641       1.1  christos 	struct ip6_ext *hdr;
    642       1.1  christos 	u_short shift;
    643       1.1  christos 	int i;
    644       1.1  christos 
    645       1.1  christos 	fin->fin_flx |= FI_V6EXTHDR;
    646       1.1  christos 
    647       1.1  christos 				/* 8 is default length of extension hdr */
    648       1.1  christos 	if ((fin->fin_dlen - 8) < 0) {
    649       1.1  christos 		fin->fin_flx |= FI_SHORT;
    650       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_short);
    651       1.1  christos 		return NULL;
    652       1.1  christos 	}
    653       1.1  christos 
    654       1.1  christos 	if (ipf_pr_pullup(fin, 8) == -1) {
    655       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_pullup);
    656       1.1  christos 		return NULL;
    657       1.1  christos 	}
    658       1.1  christos 
    659       1.1  christos 	hdr = fin->fin_dp;
    660       1.1  christos 	switch (proto)
    661       1.1  christos 	{
    662       1.1  christos 	case IPPROTO_FRAGMENT :
    663       1.1  christos 		shift = 8;
    664       1.1  christos 		break;
    665       1.1  christos 	default :
    666       1.1  christos 		shift = 8 + (hdr->ip6e_len << 3);
    667       1.1  christos 		break;
    668       1.1  christos 	}
    669       1.1  christos 
    670       1.1  christos 	if (shift > fin->fin_dlen) {	/* Nasty extension header length? */
    671       1.1  christos 		fin->fin_flx |= FI_BAD;
    672  1.15.4.2     skrll 		DT3(ipf_fi_bad_pr_ipv6exthdr_len, fr_info_t *, fin, u_short, shift, u_short, fin->fin_dlen);
    673       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_ext_hlen);
    674       1.1  christos 		return NULL;
    675       1.1  christos 	}
    676       1.1  christos 
    677       1.1  christos 	fin->fin_dp = (char *)fin->fin_dp + shift;
    678       1.1  christos 	fin->fin_dlen -= shift;
    679       1.1  christos 
    680       1.1  christos 	/*
    681       1.1  christos 	 * If we have seen a fragment header, do not set any flags to indicate
    682       1.1  christos 	 * the presence of this extension header as it has no impact on the
    683       1.1  christos 	 * end result until after it has been defragmented.
    684       1.1  christos 	 */
    685       1.1  christos 	if (fin->fin_flx & FI_FRAG)
    686       1.1  christos 		return hdr;
    687       1.1  christos 
    688       1.1  christos 	for (i = 0; ip6exthdr[i].ol_bit != 0; i++)
    689       1.1  christos 		if (ip6exthdr[i].ol_val == proto) {
    690       1.1  christos 			/*
    691       1.1  christos 			 * Most IPv6 extension headers are only allowed once.
    692       1.1  christos 			 */
    693       1.1  christos 			if ((multiple == 0) &&
    694  1.15.4.2     skrll 			    ((fin->fin_optmsk & ip6exthdr[i].ol_bit) != 0)) {
    695       1.1  christos 				fin->fin_flx |= FI_BAD;
    696  1.15.4.2     skrll 				DT2(ipf_fi_bad_ipv6exthdr_once, fr_info_t *, fin, u_int, (fin->fin_optmsk & ip6exthdr[i].ol_bit));
    697  1.15.4.2     skrll 			} else
    698       1.1  christos 				fin->fin_optmsk |= ip6exthdr[i].ol_bit;
    699       1.1  christos 			break;
    700       1.1  christos 		}
    701       1.1  christos 
    702       1.1  christos 	return hdr;
    703       1.1  christos }
    704       1.1  christos 
    705       1.1  christos 
    706       1.1  christos /* ------------------------------------------------------------------------ */
    707       1.1  christos /* Function:    ipf_pr_hopopts6                                             */
    708       1.1  christos /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
    709       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    710       1.1  christos /*                                                                          */
    711       1.1  christos /* IPv6 Only                                                                */
    712       1.1  christos /* This is function checks pending hop by hop options extension header      */
    713       1.1  christos /* ------------------------------------------------------------------------ */
    714       1.1  christos static INLINE int
    715       1.2  christos ipf_pr_hopopts6(fr_info_t *fin)
    716       1.1  christos {
    717       1.1  christos 	struct ip6_ext *hdr;
    718       1.1  christos 
    719       1.1  christos 	hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
    720       1.1  christos 	if (hdr == NULL)
    721       1.1  christos 		return IPPROTO_NONE;
    722       1.1  christos 	return hdr->ip6e_nxt;
    723       1.1  christos }
    724       1.1  christos 
    725       1.1  christos 
    726       1.1  christos /* ------------------------------------------------------------------------ */
    727       1.1  christos /* Function:    ipf_pr_mobility6                                            */
    728       1.1  christos /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
    729       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    730       1.1  christos /*                                                                          */
    731       1.1  christos /* IPv6 Only                                                                */
    732       1.1  christos /* This is function checks the IPv6 mobility extension header               */
    733       1.1  christos /* ------------------------------------------------------------------------ */
    734       1.1  christos static INLINE int
    735       1.2  christos ipf_pr_mobility6(fr_info_t *fin)
    736       1.1  christos {
    737       1.1  christos 	struct ip6_ext *hdr;
    738       1.1  christos 
    739       1.1  christos 	hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_MOBILITY);
    740       1.1  christos 	if (hdr == NULL)
    741       1.1  christos 		return IPPROTO_NONE;
    742       1.1  christos 	return hdr->ip6e_nxt;
    743       1.1  christos }
    744       1.1  christos 
    745       1.1  christos 
    746       1.1  christos /* ------------------------------------------------------------------------ */
    747       1.1  christos /* Function:    ipf_pr_routing6                                             */
    748       1.1  christos /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
    749       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    750       1.1  christos /*                                                                          */
    751       1.1  christos /* IPv6 Only                                                                */
    752       1.1  christos /* This is function checks pending routing extension header                 */
    753       1.1  christos /* ------------------------------------------------------------------------ */
    754       1.1  christos static INLINE int
    755       1.2  christos ipf_pr_routing6(fr_info_t *fin)
    756       1.1  christos {
    757       1.1  christos 	struct ip6_routing *hdr;
    758       1.1  christos 
    759       1.1  christos 	hdr = (struct ip6_routing *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_ROUTING);
    760       1.1  christos 	if (hdr == NULL)
    761       1.1  christos 		return IPPROTO_NONE;
    762       1.1  christos 
    763       1.1  christos 	switch (hdr->ip6r_type)
    764       1.1  christos 	{
    765       1.1  christos 	case 0 :
    766       1.1  christos 		/*
    767       1.1  christos 		 * Nasty extension header length?
    768       1.1  christos 		 */
    769       1.1  christos 		if (((hdr->ip6r_len >> 1) < hdr->ip6r_segleft) ||
    770       1.1  christos 		    (hdr->ip6r_segleft && (hdr->ip6r_len & 1))) {
    771       1.1  christos 			ipf_main_softc_t *softc = fin->fin_main_soft;
    772       1.1  christos 
    773       1.1  christos 			fin->fin_flx |= FI_BAD;
    774  1.15.4.2     skrll 			DT1(ipf_fi_bad_routing6, fr_info_t *, fin);
    775       1.1  christos 			LBUMPD(ipf_stats[fin->fin_out], fr_v6_rh_bad);
    776       1.1  christos 			return IPPROTO_NONE;
    777       1.1  christos 		}
    778       1.1  christos 		break;
    779       1.1  christos 
    780       1.1  christos 	default :
    781       1.1  christos 		break;
    782       1.1  christos 	}
    783       1.1  christos 
    784       1.1  christos 	return hdr->ip6r_nxt;
    785       1.1  christos }
    786       1.1  christos 
    787       1.1  christos 
    788       1.1  christos /* ------------------------------------------------------------------------ */
    789       1.1  christos /* Function:    ipf_pr_fragment6                                            */
    790       1.1  christos /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
    791       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    792       1.1  christos /*                                                                          */
    793       1.1  christos /* IPv6 Only                                                                */
    794       1.1  christos /* Examine the IPv6 fragment header and extract fragment offset information.*/
    795       1.1  christos /*                                                                          */
    796       1.1  christos /* Fragments in IPv6 are extraordinarily difficult to deal with - much more */
    797       1.1  christos /* so than in IPv4.  There are 5 cases of fragments with IPv6 that all      */
    798       1.1  christos /* packets with a fragment header can fit into.  They are as follows:       */
    799       1.1  christos /*                                                                          */
    800       1.1  christos /* 1.  [IPv6][0-n EH][FH][0-n EH] (no L4HDR present)                        */
    801       1.1  christos /* 2.  [IPV6][0-n EH][FH][0-n EH][L4HDR part] (short)                       */
    802       1.1  christos /* 3.  [IPV6][0-n EH][FH][L4HDR part][0-n data] (short)                     */
    803       1.1  christos /* 4.  [IPV6][0-n EH][FH][0-n EH][L4HDR][0-n data]                          */
    804       1.1  christos /* 5.  [IPV6][0-n EH][FH][data]                                             */
    805       1.1  christos /*                                                                          */
    806       1.1  christos /* IPV6 = IPv6 header, FH = Fragment Header,                                */
    807       1.1  christos /* 0-n EH = 0 or more extension headers, 0-n data = 0 or more bytes of data */
    808       1.1  christos /*                                                                          */
    809       1.1  christos /* Packets that match 1, 2, 3 will be dropped as the only reasonable        */
    810       1.1  christos /* scenario in which they happen is in extreme circumstances that are most  */
    811       1.1  christos /* likely to be an indication of an attack rather than normal traffic.      */
    812       1.1  christos /* A type 3 packet may be sent by an attacked after a type 4 packet.  There */
    813       1.1  christos /* are two rules that can be used to guard against type 3 packets: L4       */
    814       1.1  christos /* headers must always be in a packet that has the offset field set to 0    */
    815       1.1  christos /* and no packet is allowed to overlay that where offset = 0.               */
    816       1.1  christos /* ------------------------------------------------------------------------ */
    817       1.1  christos static INLINE int
    818       1.2  christos ipf_pr_fragment6(fr_info_t *fin)
    819       1.1  christos {
    820       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
    821       1.1  christos 	struct ip6_frag *frag;
    822       1.1  christos 
    823       1.1  christos 	fin->fin_flx |= FI_FRAG;
    824       1.1  christos 
    825       1.1  christos 	frag = (struct ip6_frag *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_FRAGMENT);
    826       1.1  christos 	if (frag == NULL) {
    827       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_frag_bad);
    828       1.1  christos 		return IPPROTO_NONE;
    829       1.1  christos 	}
    830       1.1  christos 
    831       1.1  christos 	if ((frag->ip6f_offlg & IP6F_MORE_FRAG) != 0) {
    832       1.1  christos 		/*
    833       1.1  christos 		 * Any fragment that isn't the last fragment must have its
    834       1.1  christos 		 * length as a multiple of 8.
    835       1.1  christos 		 */
    836  1.15.4.2     skrll 		if ((fin->fin_plen & 7) != 0) {
    837       1.1  christos 			fin->fin_flx |= FI_BAD;
    838  1.15.4.2     skrll 			DT2(ipf_fi_bad_frag_not_8, fr_info_t *, fin, u_int, (fin->fin_plen & 7));
    839  1.15.4.2     skrll 		}
    840       1.1  christos 	}
    841       1.1  christos 
    842       1.1  christos 	fin->fin_fraghdr = frag;
    843       1.1  christos 	fin->fin_id = frag->ip6f_ident;
    844       1.1  christos 	fin->fin_off = ntohs(frag->ip6f_offlg & IP6F_OFF_MASK);
    845       1.1  christos 	if (fin->fin_off != 0)
    846       1.1  christos 		fin->fin_flx |= FI_FRAGBODY;
    847       1.1  christos 
    848       1.1  christos 	/*
    849       1.1  christos 	 * Jumbograms aren't handled, so the max. length is 64k
    850       1.1  christos 	 */
    851  1.15.4.2     skrll 	if ((fin->fin_off << 3) + fin->fin_dlen > 65535) {
    852       1.1  christos 		  fin->fin_flx |= FI_BAD;
    853  1.15.4.2     skrll 		  DT2(ipf_fi_bad_jumbogram, fr_info_t *, fin, u_int, ((fin->fin_off << 3) + fin->fin_dlen));
    854  1.15.4.2     skrll 	}
    855       1.1  christos 
    856       1.1  christos 	/*
    857       1.1  christos 	 * We don't know where the transport layer header (or whatever is next
    858       1.1  christos 	 * is), as it could be behind destination options (amongst others) so
    859       1.1  christos 	 * return the fragment header as the type of packet this is.  Note that
    860       1.1  christos 	 * this effectively disables the fragment cache for > 1 protocol at a
    861       1.1  christos 	 * time.
    862       1.1  christos 	 */
    863       1.1  christos 	return frag->ip6f_nxt;
    864       1.1  christos }
    865       1.1  christos 
    866       1.1  christos 
    867       1.1  christos /* ------------------------------------------------------------------------ */
    868       1.1  christos /* Function:    ipf_pr_dstopts6                                             */
    869       1.1  christos /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
    870       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    871       1.1  christos /*                                                                          */
    872       1.1  christos /* IPv6 Only                                                                */
    873       1.1  christos /* This is function checks pending destination options extension header     */
    874       1.1  christos /* ------------------------------------------------------------------------ */
    875       1.1  christos static INLINE int
    876       1.2  christos ipf_pr_dstopts6(fr_info_t *fin)
    877       1.1  christos {
    878       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
    879       1.1  christos 	struct ip6_ext *hdr;
    880       1.1  christos 
    881       1.1  christos 	hdr = ipf_pr_ipv6exthdr(fin, 0, IPPROTO_DSTOPTS);
    882       1.1  christos 	if (hdr == NULL) {
    883       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_dst_bad);
    884       1.1  christos 		return IPPROTO_NONE;
    885       1.1  christos 	}
    886       1.1  christos 	return hdr->ip6e_nxt;
    887       1.1  christos }
    888       1.1  christos 
    889       1.1  christos 
    890       1.1  christos /* ------------------------------------------------------------------------ */
    891       1.1  christos /* Function:    ipf_pr_icmp6                                                */
    892       1.1  christos /* Returns:     void                                                        */
    893       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    894       1.1  christos /*                                                                          */
    895       1.1  christos /* IPv6 Only                                                                */
    896       1.1  christos /* This routine is mainly concerned with determining the minimum valid size */
    897       1.1  christos /* for an ICMPv6 packet.                                                    */
    898       1.1  christos /* ------------------------------------------------------------------------ */
    899       1.1  christos static INLINE void
    900       1.2  christos ipf_pr_icmp6(fr_info_t *fin)
    901       1.1  christos {
    902       1.1  christos 	int minicmpsz = sizeof(struct icmp6_hdr);
    903       1.1  christos 	struct icmp6_hdr *icmp6;
    904       1.1  christos 
    905       1.1  christos 	if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN - sizeof(ip6_t)) == -1) {
    906       1.1  christos 		ipf_main_softc_t *softc = fin->fin_main_soft;
    907       1.1  christos 
    908       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_icmp6_pullup);
    909       1.1  christos 		return;
    910       1.1  christos 	}
    911       1.1  christos 
    912       1.1  christos 	if (fin->fin_dlen > 1) {
    913       1.1  christos 		ip6_t *ip6;
    914       1.1  christos 
    915       1.1  christos 		icmp6 = fin->fin_dp;
    916       1.1  christos 
    917       1.1  christos 		fin->fin_data[0] = *(u_short *)icmp6;
    918       1.1  christos 
    919       1.1  christos 		if ((icmp6->icmp6_type & ICMP6_INFOMSG_MASK) != 0)
    920       1.1  christos 			fin->fin_flx |= FI_ICMPQUERY;
    921       1.1  christos 
    922       1.1  christos 		switch (icmp6->icmp6_type)
    923       1.1  christos 		{
    924       1.1  christos 		case ICMP6_ECHO_REPLY :
    925       1.1  christos 		case ICMP6_ECHO_REQUEST :
    926       1.1  christos 			if (fin->fin_dlen >= 6)
    927       1.1  christos 				fin->fin_data[1] = icmp6->icmp6_id;
    928       1.1  christos 			minicmpsz = ICMP6ERR_MINPKTLEN - sizeof(ip6_t);
    929       1.1  christos 			break;
    930       1.1  christos 
    931       1.1  christos 		case ICMP6_DST_UNREACH :
    932       1.1  christos 		case ICMP6_PACKET_TOO_BIG :
    933       1.1  christos 		case ICMP6_TIME_EXCEEDED :
    934       1.1  christos 		case ICMP6_PARAM_PROB :
    935       1.1  christos 			fin->fin_flx |= FI_ICMPERR;
    936       1.1  christos 			minicmpsz = ICMP6ERR_IPICMPHLEN - sizeof(ip6_t);
    937       1.1  christos 			if (fin->fin_plen < ICMP6ERR_IPICMPHLEN)
    938       1.1  christos 				break;
    939       1.1  christos 
    940       1.1  christos 			if (M_LEN(fin->fin_m) < fin->fin_plen) {
    941       1.1  christos 				if (ipf_coalesce(fin) != 1)
    942       1.1  christos 					return;
    943       1.1  christos 			}
    944       1.1  christos 
    945       1.1  christos 			if (ipf_pr_pullup(fin, ICMP6ERR_MINPKTLEN) == -1)
    946       1.1  christos 				return;
    947       1.1  christos 
    948       1.1  christos 			/*
    949       1.1  christos 			 * If the destination of this packet doesn't match the
    950       1.1  christos 			 * source of the original packet then this packet is
    951       1.1  christos 			 * not correct.
    952       1.1  christos 			 */
    953       1.1  christos 			icmp6 = fin->fin_dp;
    954       1.1  christos 			ip6 = (ip6_t *)((char *)icmp6 + ICMPERR_ICMPHLEN);
    955       1.1  christos 			if (IP6_NEQ(&fin->fin_fi.fi_dst,
    956  1.15.4.2     skrll 				    &ip6->ip6_src)) {
    957       1.1  christos 				fin->fin_flx |= FI_BAD;
    958  1.15.4.2     skrll 				DT1(ipf_fi_bad_icmp6, fr_info_t *, fin);
    959  1.15.4.2     skrll 			}
    960       1.1  christos 			break;
    961       1.1  christos 		default :
    962       1.1  christos 			break;
    963       1.1  christos 		}
    964       1.1  christos 	}
    965       1.1  christos 
    966       1.1  christos 	ipf_pr_short6(fin, minicmpsz);
    967       1.3   darrenr 	if ((fin->fin_flx & (FI_SHORT|FI_BAD)) == 0) {
    968       1.3   darrenr 		u_char p = fin->fin_p;
    969       1.3   darrenr 
    970       1.3   darrenr 		fin->fin_p = IPPROTO_ICMPV6;
    971       1.3   darrenr 		ipf_checkv6sum(fin);
    972       1.3   darrenr 		fin->fin_p = p;
    973       1.3   darrenr 	}
    974       1.1  christos }
    975       1.1  christos 
    976       1.1  christos 
    977       1.1  christos /* ------------------------------------------------------------------------ */
    978       1.1  christos /* Function:    ipf_pr_udp6                                                 */
    979       1.1  christos /* Returns:     void                                                        */
    980       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
    981       1.1  christos /*                                                                          */
    982       1.1  christos /* IPv6 Only                                                                */
    983       1.1  christos /* Analyse the packet for IPv6/UDP properties.                              */
    984       1.1  christos /* Is not expected to be called for fragmented packets.                     */
    985       1.1  christos /* ------------------------------------------------------------------------ */
    986       1.1  christos static INLINE void
    987       1.2  christos ipf_pr_udp6(fr_info_t *fin)
    988       1.1  christos {
    989       1.1  christos 
    990       1.1  christos 	if (ipf_pr_udpcommon(fin) == 0) {
    991       1.1  christos 		u_char p = fin->fin_p;
    992       1.1  christos 
    993       1.1  christos 		fin->fin_p = IPPROTO_UDP;
    994       1.1  christos 		ipf_checkv6sum(fin);
    995       1.1  christos 		fin->fin_p = p;
    996       1.1  christos 	}
    997       1.1  christos }
    998       1.1  christos 
    999       1.1  christos 
   1000       1.1  christos /* ------------------------------------------------------------------------ */
   1001       1.1  christos /* Function:    ipf_pr_tcp6                                                 */
   1002       1.1  christos /* Returns:     void                                                        */
   1003       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1004       1.1  christos /*                                                                          */
   1005       1.1  christos /* IPv6 Only                                                                */
   1006       1.1  christos /* Analyse the packet for IPv6/TCP properties.                              */
   1007       1.1  christos /* Is not expected to be called for fragmented packets.                     */
   1008       1.1  christos /* ------------------------------------------------------------------------ */
   1009       1.1  christos static INLINE void
   1010       1.2  christos ipf_pr_tcp6(fr_info_t *fin)
   1011       1.1  christos {
   1012       1.1  christos 
   1013       1.1  christos 	if (ipf_pr_tcpcommon(fin) == 0) {
   1014       1.1  christos 		u_char p = fin->fin_p;
   1015       1.1  christos 
   1016       1.3   darrenr 		fin->fin_p = IPPROTO_TCP;
   1017       1.1  christos 		ipf_checkv6sum(fin);
   1018       1.1  christos 		fin->fin_p = p;
   1019       1.1  christos 	}
   1020       1.1  christos }
   1021       1.1  christos 
   1022       1.1  christos 
   1023       1.1  christos /* ------------------------------------------------------------------------ */
   1024       1.1  christos /* Function:    ipf_pr_esp6                                                 */
   1025       1.1  christos /* Returns:     void                                                        */
   1026       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1027       1.1  christos /*                                                                          */
   1028       1.1  christos /* IPv6 Only                                                                */
   1029       1.1  christos /* Analyse the packet for ESP properties.                                   */
   1030       1.1  christos /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits)  */
   1031       1.1  christos /* even though the newer ESP packets must also have a sequence number that  */
   1032       1.1  christos /* is 32bits as well, it is not possible(?) to determine the version from a */
   1033       1.1  christos /* simple packet header.                                                    */
   1034       1.1  christos /* ------------------------------------------------------------------------ */
   1035       1.1  christos static INLINE void
   1036       1.2  christos ipf_pr_esp6(fr_info_t *fin)
   1037       1.1  christos {
   1038       1.1  christos 
   1039       1.1  christos 	if ((fin->fin_off == 0) && (ipf_pr_pullup(fin, 8) == -1)) {
   1040       1.1  christos 		ipf_main_softc_t *softc = fin->fin_main_soft;
   1041       1.1  christos 
   1042       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_esp_pullup);
   1043       1.1  christos 		return;
   1044       1.1  christos 	}
   1045       1.1  christos }
   1046       1.1  christos 
   1047       1.1  christos 
   1048       1.1  christos /* ------------------------------------------------------------------------ */
   1049       1.1  christos /* Function:    ipf_pr_ah6                                                  */
   1050       1.1  christos /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
   1051       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1052       1.1  christos /*                                                                          */
   1053       1.1  christos /* IPv6 Only                                                                */
   1054       1.1  christos /* Analyse the packet for AH properties.                                    */
   1055       1.1  christos /* The minimum length is taken to be the combination of all fields in the   */
   1056       1.1  christos /* header being present and no authentication data (null algorithm used.)   */
   1057       1.1  christos /* ------------------------------------------------------------------------ */
   1058       1.1  christos static INLINE int
   1059       1.2  christos ipf_pr_ah6(fr_info_t *fin)
   1060       1.1  christos {
   1061       1.1  christos 	authhdr_t *ah;
   1062       1.1  christos 
   1063       1.1  christos 	fin->fin_flx |= FI_AH;
   1064       1.1  christos 
   1065       1.1  christos 	ah = (authhdr_t *)ipf_pr_ipv6exthdr(fin, 0, IPPROTO_HOPOPTS);
   1066       1.1  christos 	if (ah == NULL) {
   1067       1.1  christos 		ipf_main_softc_t *softc = fin->fin_main_soft;
   1068       1.1  christos 
   1069       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_ah_bad);
   1070       1.1  christos 		return IPPROTO_NONE;
   1071       1.1  christos 	}
   1072       1.1  christos 
   1073       1.1  christos 	ipf_pr_short6(fin, sizeof(*ah));
   1074       1.1  christos 
   1075       1.1  christos 	/*
   1076       1.1  christos 	 * No need for another pullup, ipf_pr_ipv6exthdr() will pullup
   1077       1.1  christos 	 * enough data to satisfy ah_next (the very first one.)
   1078       1.1  christos 	 */
   1079       1.1  christos 	return ah->ah_next;
   1080       1.1  christos }
   1081       1.1  christos 
   1082       1.1  christos 
   1083       1.1  christos /* ------------------------------------------------------------------------ */
   1084       1.1  christos /* Function:    ipf_pr_gre6                                                 */
   1085       1.1  christos /* Returns:     void                                                        */
   1086       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1087       1.1  christos /*                                                                          */
   1088       1.1  christos /* Analyse the packet for GRE properties.                                   */
   1089       1.1  christos /* ------------------------------------------------------------------------ */
   1090       1.1  christos static INLINE void
   1091       1.2  christos ipf_pr_gre6(fr_info_t *fin)
   1092       1.1  christos {
   1093       1.1  christos 	grehdr_t *gre;
   1094       1.1  christos 
   1095       1.1  christos 	if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) {
   1096       1.1  christos 		ipf_main_softc_t *softc = fin->fin_main_soft;
   1097       1.1  christos 
   1098       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v6_gre_pullup);
   1099       1.1  christos 		return;
   1100       1.1  christos 	}
   1101       1.1  christos 
   1102       1.1  christos 	gre = fin->fin_dp;
   1103       1.1  christos 	if (GRE_REV(gre->gr_flags) == 1)
   1104       1.1  christos 		fin->fin_data[0] = gre->gr_call;
   1105       1.1  christos }
   1106       1.1  christos #endif	/* USE_INET6 */
   1107       1.1  christos 
   1108       1.1  christos 
   1109       1.1  christos /* ------------------------------------------------------------------------ */
   1110       1.1  christos /* Function:    ipf_pr_pullup                                               */
   1111       1.1  christos /* Returns:     int     - 0 == pullup succeeded, -1 == failure              */
   1112       1.1  christos /* Parameters:  fin(I)  - pointer to packet information                     */
   1113       1.1  christos /*              plen(I) - length (excluding L3 header) to pullup            */
   1114       1.1  christos /*                                                                          */
   1115       1.1  christos /* Short inline function to cut down on code duplication to perform a call  */
   1116       1.1  christos /* to ipf_pullup to ensure there is the required amount of data,            */
   1117       1.1  christos /* consecutively in the packet buffer.                                      */
   1118       1.1  christos /*                                                                          */
   1119       1.1  christos /* This function pulls up 'extra' data at the location of fin_dp.  fin_dp   */
   1120       1.1  christos /* points to the first byte after the complete layer 3 header, which will   */
   1121       1.1  christos /* include all of the known extension headers for IPv6 or options for IPv4. */
   1122       1.1  christos /*                                                                          */
   1123       1.1  christos /* Since fr_pullup() expects the total length of bytes to be pulled up, it  */
   1124       1.1  christos /* is necessary to add those we can already assume to be pulled up (fin_dp  */
   1125       1.1  christos /* - fin_ip) to what is passed through.                                     */
   1126       1.1  christos /* ------------------------------------------------------------------------ */
   1127       1.1  christos int
   1128       1.2  christos ipf_pr_pullup(fr_info_t *fin, int plen)
   1129       1.1  christos {
   1130       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1131       1.1  christos 
   1132       1.1  christos 	if (fin->fin_m != NULL) {
   1133       1.1  christos 		if (fin->fin_dp != NULL)
   1134       1.1  christos 			plen += (char *)fin->fin_dp -
   1135       1.1  christos 				((char *)fin->fin_ip + fin->fin_hlen);
   1136       1.1  christos 		plen += fin->fin_hlen;
   1137       1.3   darrenr 		if (M_LEN(fin->fin_m) < plen + fin->fin_ipoff) {
   1138       1.1  christos #if defined(_KERNEL)
   1139       1.1  christos 			if (ipf_pullup(fin->fin_m, fin, plen) == NULL) {
   1140       1.1  christos 				DT(ipf_pullup_fail);
   1141       1.1  christos 				LBUMP(ipf_stats[fin->fin_out].fr_pull[1]);
   1142       1.1  christos 				return -1;
   1143       1.1  christos 			}
   1144       1.1  christos 			LBUMP(ipf_stats[fin->fin_out].fr_pull[0]);
   1145       1.1  christos #else
   1146       1.1  christos 			LBUMP(ipf_stats[fin->fin_out].fr_pull[1]);
   1147       1.1  christos 			/*
   1148       1.1  christos 			 * Fake ipf_pullup failing
   1149       1.1  christos 			 */
   1150       1.1  christos 			fin->fin_reason = FRB_PULLUP;
   1151       1.1  christos 			*fin->fin_mp = NULL;
   1152       1.1  christos 			fin->fin_m = NULL;
   1153       1.1  christos 			fin->fin_ip = NULL;
   1154       1.1  christos 			return -1;
   1155       1.1  christos #endif
   1156       1.1  christos 		}
   1157       1.1  christos 	}
   1158       1.1  christos 	return 0;
   1159       1.1  christos }
   1160       1.1  christos 
   1161       1.1  christos 
   1162       1.1  christos /* ------------------------------------------------------------------------ */
   1163       1.1  christos /* Function:    ipf_pr_short                                                */
   1164       1.1  christos /* Returns:     void                                                        */
   1165       1.1  christos /* Parameters:  fin(I)  - pointer to packet information                     */
   1166       1.1  christos /*              xmin(I) - minimum header size                               */
   1167       1.1  christos /*                                                                          */
   1168       1.1  christos /* Check if a packet is "short" as defined by xmin.  The rule we are        */
   1169       1.1  christos /* applying here is that the packet must not be fragmented within the layer */
   1170       1.1  christos /* 4 header.  That is, it must not be a fragment that has its offset set to */
   1171       1.1  christos /* start within the layer 4 header (hdrmin) or if it is at offset 0, the    */
   1172       1.1  christos /* entire layer 4 header must be present (min).                             */
   1173       1.1  christos /* ------------------------------------------------------------------------ */
   1174       1.1  christos static INLINE void
   1175       1.2  christos ipf_pr_short(fr_info_t *fin, int xmin)
   1176       1.1  christos {
   1177       1.1  christos 
   1178       1.1  christos 	if (fin->fin_off == 0) {
   1179       1.1  christos 		if (fin->fin_dlen < xmin)
   1180       1.1  christos 			fin->fin_flx |= FI_SHORT;
   1181       1.1  christos 	} else if (fin->fin_off < xmin) {
   1182       1.1  christos 		fin->fin_flx |= FI_SHORT;
   1183       1.1  christos 	}
   1184       1.1  christos }
   1185       1.1  christos 
   1186       1.1  christos 
   1187       1.1  christos /* ------------------------------------------------------------------------ */
   1188       1.1  christos /* Function:    ipf_pr_icmp                                                 */
   1189       1.1  christos /* Returns:     void                                                        */
   1190       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1191       1.1  christos /*                                                                          */
   1192       1.1  christos /* IPv4 Only                                                                */
   1193       1.1  christos /* Do a sanity check on the packet for ICMP (v4).  In nearly all cases,     */
   1194       1.1  christos /* except extrememly bad packets, both type and code will be present.       */
   1195       1.1  christos /* The expected minimum size of an ICMP packet is very much dependent on    */
   1196       1.1  christos /* the type of it.                                                          */
   1197       1.1  christos /*                                                                          */
   1198       1.1  christos /* XXX - other ICMP sanity checks?                                          */
   1199       1.1  christos /* ------------------------------------------------------------------------ */
   1200       1.1  christos static INLINE void
   1201       1.2  christos ipf_pr_icmp(fr_info_t *fin)
   1202       1.1  christos {
   1203       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1204       1.1  christos 	int minicmpsz = sizeof(struct icmp);
   1205       1.1  christos 	icmphdr_t *icmp;
   1206       1.1  christos 	ip_t *oip;
   1207       1.1  christos 
   1208       1.1  christos 	ipf_pr_short(fin, ICMPERR_ICMPHLEN);
   1209       1.1  christos 
   1210       1.1  christos 	if (fin->fin_off != 0) {
   1211       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_frag);
   1212       1.1  christos 		return;
   1213       1.1  christos 	}
   1214       1.1  christos 
   1215       1.1  christos 	if (ipf_pr_pullup(fin, ICMPERR_ICMPHLEN) == -1) {
   1216       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v4_icmp_pullup);
   1217       1.1  christos 		return;
   1218       1.1  christos 	}
   1219       1.1  christos 
   1220       1.1  christos 	icmp = fin->fin_dp;
   1221       1.1  christos 
   1222       1.1  christos 	fin->fin_data[0] = *(u_short *)icmp;
   1223       1.1  christos 	fin->fin_data[1] = icmp->icmp_id;
   1224       1.1  christos 
   1225       1.1  christos 	switch (icmp->icmp_type)
   1226       1.1  christos 	{
   1227       1.1  christos 	case ICMP_ECHOREPLY :
   1228       1.1  christos 	case ICMP_ECHO :
   1229       1.1  christos 	/* Router discovery messaes - RFC 1256 */
   1230       1.1  christos 	case ICMP_ROUTERADVERT :
   1231       1.1  christos 	case ICMP_ROUTERSOLICIT :
   1232       1.1  christos 		fin->fin_flx |= FI_ICMPQUERY;
   1233       1.1  christos 		minicmpsz = ICMP_MINLEN;
   1234       1.1  christos 		break;
   1235       1.1  christos 	/*
   1236       1.1  christos 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
   1237       1.1  christos 	 * 3 * timestamp(3 * 4)
   1238       1.1  christos 	 */
   1239       1.1  christos 	case ICMP_TSTAMP :
   1240       1.1  christos 	case ICMP_TSTAMPREPLY :
   1241       1.1  christos 		fin->fin_flx |= FI_ICMPQUERY;
   1242       1.1  christos 		minicmpsz = 20;
   1243       1.1  christos 		break;
   1244       1.1  christos 	/*
   1245       1.1  christos 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) +
   1246       1.1  christos 	 * mask(4)
   1247       1.1  christos 	 */
   1248       1.1  christos 	case ICMP_IREQ :
   1249       1.1  christos 	case ICMP_IREQREPLY :
   1250       1.1  christos 	case ICMP_MASKREQ :
   1251       1.1  christos 	case ICMP_MASKREPLY :
   1252       1.1  christos 		fin->fin_flx |= FI_ICMPQUERY;
   1253       1.1  christos 		minicmpsz = 12;
   1254       1.1  christos 		break;
   1255       1.1  christos 	/*
   1256       1.1  christos 	 * type(1) + code(1) + cksum(2) + id(2) seq(2) + ip(20+)
   1257       1.1  christos 	 */
   1258       1.1  christos 	case ICMP_UNREACH :
   1259       1.1  christos #ifdef icmp_nextmtu
   1260       1.1  christos 		if (icmp->icmp_code == ICMP_UNREACH_NEEDFRAG) {
   1261  1.15.4.2     skrll 			if (icmp->icmp_nextmtu < softc->ipf_icmpminfragmtu) {
   1262       1.1  christos 				fin->fin_flx |= FI_BAD;
   1263  1.15.4.2     skrll 				DT3(ipf_fi_bad_icmp_nextmtu, fr_info_t *, fin, u_int, icmp->icmp_nextmtu, u_int, softc->ipf_icmpminfragmtu);
   1264  1.15.4.2     skrll 			}
   1265       1.1  christos 		}
   1266       1.1  christos #endif
   1267       1.1  christos 	case ICMP_SOURCEQUENCH :
   1268       1.1  christos 	case ICMP_REDIRECT :
   1269       1.1  christos 	case ICMP_TIMXCEED :
   1270       1.1  christos 	case ICMP_PARAMPROB :
   1271       1.1  christos 		fin->fin_flx |= FI_ICMPERR;
   1272       1.1  christos 		if (ipf_coalesce(fin) != 1) {
   1273       1.1  christos 			LBUMPD(ipf_stats[fin->fin_out], fr_icmp_coalesce);
   1274       1.1  christos 			return;
   1275       1.1  christos 		}
   1276       1.1  christos 
   1277       1.1  christos 		/*
   1278       1.1  christos 		 * ICMP error packets should not be generated for IP
   1279       1.1  christos 		 * packets that are a fragment that isn't the first
   1280       1.1  christos 		 * fragment.
   1281       1.1  christos 		 */
   1282       1.1  christos 		oip = (ip_t *)((char *)fin->fin_dp + ICMPERR_ICMPHLEN);
   1283  1.15.4.2     skrll 		if ((ntohs(oip->ip_off) & IP_OFFMASK) != 0) {
   1284       1.1  christos 			fin->fin_flx |= FI_BAD;
   1285  1.15.4.2     skrll 			DT2(ipf_fi_bad_icmp_err, fr_info_t, fin, u_int, (ntohs(oip->ip_off) & IP_OFFMASK));
   1286  1.15.4.2     skrll 		}
   1287       1.1  christos 
   1288       1.1  christos 		/*
   1289       1.1  christos 		 * If the destination of this packet doesn't match the
   1290       1.1  christos 		 * source of the original packet then this packet is
   1291       1.1  christos 		 * not correct.
   1292       1.1  christos 		 */
   1293  1.15.4.2     skrll 		if (oip->ip_src.s_addr != fin->fin_daddr) {
   1294       1.1  christos 			fin->fin_flx |= FI_BAD;
   1295  1.15.4.2     skrll 			DT1(ipf_fi_bad_src_ne_dst, fr_info_t *, fin);
   1296  1.15.4.2     skrll 		}
   1297       1.1  christos 		break;
   1298       1.1  christos 	default :
   1299       1.1  christos 		break;
   1300       1.1  christos 	}
   1301       1.1  christos 
   1302       1.1  christos 	ipf_pr_short(fin, minicmpsz);
   1303       1.1  christos 
   1304       1.1  christos 	ipf_checkv4sum(fin);
   1305       1.1  christos }
   1306       1.1  christos 
   1307       1.1  christos 
   1308       1.1  christos /* ------------------------------------------------------------------------ */
   1309       1.1  christos /* Function:    ipf_pr_tcpcommon                                            */
   1310       1.1  christos /* Returns:     int    - 0 = header ok, 1 = bad packet, -1 = buffer error   */
   1311       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1312       1.1  christos /*                                                                          */
   1313       1.1  christos /* TCP header sanity checking.  Look for bad combinations of TCP flags,     */
   1314       1.1  christos /* and make some checks with how they interact with other fields.           */
   1315       1.1  christos /* If compiled with IPFILTER_CKSUM, check to see if the TCP checksum is     */
   1316       1.1  christos /* valid and mark the packet as bad if not.                                 */
   1317       1.1  christos /* ------------------------------------------------------------------------ */
   1318       1.1  christos static INLINE int
   1319       1.2  christos ipf_pr_tcpcommon(fr_info_t *fin)
   1320       1.1  christos {
   1321       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1322       1.1  christos 	int flags, tlen;
   1323       1.1  christos 	tcphdr_t *tcp;
   1324       1.1  christos 
   1325       1.1  christos 	fin->fin_flx |= FI_TCPUDP;
   1326       1.1  christos 	if (fin->fin_off != 0) {
   1327       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_tcp_frag);
   1328       1.1  christos 		return 0;
   1329       1.1  christos 	}
   1330       1.1  christos 
   1331       1.1  christos 	if (ipf_pr_pullup(fin, sizeof(*tcp)) == -1) {
   1332       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup);
   1333       1.1  christos 		return -1;
   1334       1.1  christos 	}
   1335       1.1  christos 
   1336       1.1  christos 	tcp = fin->fin_dp;
   1337       1.1  christos 	if (fin->fin_dlen > 3) {
   1338       1.1  christos 		fin->fin_sport = ntohs(tcp->th_sport);
   1339       1.1  christos 		fin->fin_dport = ntohs(tcp->th_dport);
   1340       1.1  christos 	}
   1341       1.1  christos 
   1342       1.1  christos 	if ((fin->fin_flx & FI_SHORT) != 0) {
   1343       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_tcp_short);
   1344       1.1  christos 		return 1;
   1345       1.1  christos 	}
   1346       1.1  christos 
   1347       1.1  christos 	/*
   1348       1.1  christos 	 * Use of the TCP data offset *must* result in a value that is at
   1349       1.1  christos 	 * least the same size as the TCP header.
   1350       1.1  christos 	 */
   1351       1.1  christos 	tlen = TCP_OFF(tcp) << 2;
   1352       1.1  christos 	if (tlen < sizeof(tcphdr_t)) {
   1353       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_tcp_small);
   1354       1.1  christos 		fin->fin_flx |= FI_BAD;
   1355  1.15.4.2     skrll 		DT3(ipf_fi_bad_tlen, fr_info_t, fin, u_int, tlen, u_int, sizeof(tcphdr_t));
   1356       1.1  christos 		return 1;
   1357       1.1  christos 	}
   1358       1.1  christos 
   1359       1.1  christos 	flags = tcp->th_flags;
   1360       1.1  christos 	fin->fin_tcpf = tcp->th_flags;
   1361       1.1  christos 
   1362       1.1  christos 	/*
   1363       1.1  christos 	 * If the urgent flag is set, then the urgent pointer must
   1364       1.1  christos 	 * also be set and vice versa.  Good TCP packets do not have
   1365       1.1  christos 	 * just one of these set.
   1366       1.1  christos 	 */
   1367       1.1  christos 	if ((flags & TH_URG) != 0 && (tcp->th_urp == 0)) {
   1368       1.1  christos 		fin->fin_flx |= FI_BAD;
   1369  1.15.4.2     skrll 		DT3(ipf_fi_bad_th_urg, fr_info_t*, fin, u_int, (flags & TH_URG), u_int, tcp->th_urp);
   1370       1.1  christos #if 0
   1371       1.1  christos 	} else if ((flags & TH_URG) == 0 && (tcp->th_urp != 0)) {
   1372       1.1  christos 		/*
   1373       1.1  christos 		 * Ignore this case (#if 0) as it shows up in "real"
   1374       1.1  christos 		 * traffic with bogus values in the urgent pointer field.
   1375       1.1  christos 		 */
   1376       1.1  christos 		fin->fin_flx |= FI_BAD;
   1377  1.15.4.2     skrll 		DT3(ipf_fi_bad_th_urg0, fr_info_t *, fin, u_int, (flags & TH_URG), u_int, tcp->th_urp);
   1378       1.1  christos #endif
   1379       1.1  christos 	} else if (((flags & (TH_SYN|TH_FIN)) != 0) &&
   1380       1.1  christos 		   ((flags & (TH_RST|TH_ACK)) == TH_RST)) {
   1381       1.1  christos 		/* TH_FIN|TH_RST|TH_ACK seems to appear "naturally" */
   1382       1.1  christos 		fin->fin_flx |= FI_BAD;
   1383  1.15.4.2     skrll 		DT1(ipf_fi_bad_th_fin_rst_ack, fr_info_t, fin);
   1384       1.1  christos #if 1
   1385       1.1  christos 	} else if (((flags & TH_SYN) != 0) &&
   1386       1.1  christos 		   ((flags & (TH_URG|TH_PUSH)) != 0)) {
   1387       1.1  christos 		/*
   1388       1.1  christos 		 * SYN with URG and PUSH set is not for normal TCP but it is
   1389       1.1  christos 		 * possible(?) with T/TCP...but who uses T/TCP?
   1390       1.1  christos 		 */
   1391       1.1  christos 		fin->fin_flx |= FI_BAD;
   1392  1.15.4.2     skrll 		DT1(ipf_fi_bad_th_syn_urg_psh, fr_info_t *, fin);
   1393       1.1  christos #endif
   1394       1.1  christos 	} else if (!(flags & TH_ACK)) {
   1395       1.1  christos 		/*
   1396       1.1  christos 		 * If the ack bit isn't set, then either the SYN or
   1397       1.1  christos 		 * RST bit must be set.  If the SYN bit is set, then
   1398       1.1  christos 		 * we expect the ACK field to be 0.  If the ACK is
   1399       1.1  christos 		 * not set and if URG, PSH or FIN are set, consdier
   1400       1.1  christos 		 * that to indicate a bad TCP packet.
   1401       1.1  christos 		 */
   1402       1.1  christos 		if ((flags == TH_SYN) && (tcp->th_ack != 0)) {
   1403       1.1  christos 			/*
   1404       1.1  christos 			 * Cisco PIX sets the ACK field to a random value.
   1405       1.1  christos 			 * In light of this, do not set FI_BAD until a patch
   1406       1.1  christos 			 * is available from Cisco to ensure that
   1407       1.1  christos 			 * interoperability between existing systems is
   1408       1.1  christos 			 * achieved.
   1409       1.1  christos 			 */
   1410       1.1  christos 			/*fin->fin_flx |= FI_BAD*/;
   1411  1.15.4.2     skrll 			/*DT1(ipf_fi_bad_th_syn_ack, fr_info_t *, fin);*/
   1412       1.1  christos 		} else if (!(flags & (TH_RST|TH_SYN))) {
   1413       1.1  christos 			fin->fin_flx |= FI_BAD;
   1414  1.15.4.2     skrll 			DT1(ipf_fi_bad_th_rst_syn, fr_info_t *, fin);
   1415       1.1  christos 		} else if ((flags & (TH_URG|TH_PUSH|TH_FIN)) != 0) {
   1416       1.1  christos 			fin->fin_flx |= FI_BAD;
   1417  1.15.4.2     skrll 			DT1(ipf_fi_bad_th_urg_push_fin, fr_info_t *, fin);
   1418       1.1  christos 		}
   1419       1.1  christos 	}
   1420       1.1  christos 	if (fin->fin_flx & FI_BAD) {
   1421       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_tcp_bad_flags);
   1422       1.1  christos 		return 1;
   1423       1.1  christos 	}
   1424       1.1  christos 
   1425       1.1  christos 	/*
   1426       1.1  christos 	 * At this point, it's not exactly clear what is to be gained by
   1427       1.1  christos 	 * marking up which TCP options are and are not present.  The one we
   1428       1.1  christos 	 * are most interested in is the TCP window scale.  This is only in
   1429       1.1  christos 	 * a SYN packet [RFC1323] so we don't need this here...?
   1430       1.1  christos 	 * Now if we were to analyse the header for passive fingerprinting,
   1431       1.1  christos 	 * then that might add some weight to adding this...
   1432       1.1  christos 	 */
   1433       1.1  christos 	if (tlen == sizeof(tcphdr_t)) {
   1434       1.1  christos 		return 0;
   1435       1.1  christos 	}
   1436       1.1  christos 
   1437       1.1  christos 	if (ipf_pr_pullup(fin, tlen) == -1) {
   1438       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_tcp_pullup);
   1439       1.1  christos 		return -1;
   1440       1.1  christos 	}
   1441       1.1  christos 
   1442       1.1  christos #if 0
   1443       1.1  christos 	tcp = fin->fin_dp;
   1444       1.1  christos 	ip = fin->fin_ip;
   1445       1.1  christos 	s = (u_char *)(tcp + 1);
   1446       1.1  christos 	off = IP_HL(ip) << 2;
   1447       1.1  christos # ifdef _KERNEL
   1448       1.1  christos 	if (fin->fin_mp != NULL) {
   1449       1.1  christos 		mb_t *m = *fin->fin_mp;
   1450       1.1  christos 
   1451       1.1  christos 		if (off + tlen > M_LEN(m))
   1452       1.1  christos 			return;
   1453       1.1  christos 	}
   1454       1.1  christos # endif
   1455       1.1  christos 	for (tlen -= (int)sizeof(*tcp); tlen > 0; ) {
   1456       1.1  christos 		opt = *s;
   1457       1.1  christos 		if (opt == '\0')
   1458       1.1  christos 			break;
   1459       1.1  christos 		else if (opt == TCPOPT_NOP)
   1460       1.1  christos 			ol = 1;
   1461       1.1  christos 		else {
   1462       1.1  christos 			if (tlen < 2)
   1463       1.1  christos 				break;
   1464       1.1  christos 			ol = (int)*(s + 1);
   1465       1.1  christos 			if (ol < 2 || ol > tlen)
   1466       1.1  christos 				break;
   1467       1.1  christos 		}
   1468       1.1  christos 
   1469       1.1  christos 		for (i = 9, mv = 4; mv >= 0; ) {
   1470       1.1  christos 			op = ipopts + i;
   1471       1.1  christos 			if (opt == (u_char)op->ol_val) {
   1472       1.1  christos 				optmsk |= op->ol_bit;
   1473       1.1  christos 				break;
   1474       1.1  christos 			}
   1475       1.1  christos 		}
   1476       1.1  christos 		tlen -= ol;
   1477       1.1  christos 		s += ol;
   1478       1.1  christos 	}
   1479       1.1  christos #endif /* 0 */
   1480       1.1  christos 
   1481       1.1  christos 	return 0;
   1482       1.1  christos }
   1483       1.1  christos 
   1484       1.1  christos 
   1485       1.1  christos 
   1486       1.1  christos /* ------------------------------------------------------------------------ */
   1487       1.1  christos /* Function:    ipf_pr_udpcommon                                            */
   1488       1.1  christos /* Returns:     int    - 0 = header ok, 1 = bad packet                      */
   1489       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1490       1.1  christos /*                                                                          */
   1491       1.1  christos /* Extract the UDP source and destination ports, if present.  If compiled   */
   1492       1.1  christos /* with IPFILTER_CKSUM, check to see if the UDP checksum is valid.          */
   1493       1.1  christos /* ------------------------------------------------------------------------ */
   1494       1.1  christos static INLINE int
   1495       1.2  christos ipf_pr_udpcommon(fr_info_t *fin)
   1496       1.1  christos {
   1497       1.1  christos 	udphdr_t *udp;
   1498       1.1  christos 
   1499       1.1  christos 	fin->fin_flx |= FI_TCPUDP;
   1500       1.1  christos 
   1501       1.1  christos 	if (!fin->fin_off && (fin->fin_dlen > 3)) {
   1502       1.1  christos 		if (ipf_pr_pullup(fin, sizeof(*udp)) == -1) {
   1503       1.1  christos 			ipf_main_softc_t *softc = fin->fin_main_soft;
   1504       1.1  christos 
   1505       1.1  christos 			fin->fin_flx |= FI_SHORT;
   1506       1.1  christos 			LBUMPD(ipf_stats[fin->fin_out], fr_udp_pullup);
   1507       1.1  christos 			return 1;
   1508       1.1  christos 		}
   1509       1.1  christos 
   1510       1.1  christos 		udp = fin->fin_dp;
   1511       1.1  christos 
   1512       1.1  christos 		fin->fin_sport = ntohs(udp->uh_sport);
   1513       1.1  christos 		fin->fin_dport = ntohs(udp->uh_dport);
   1514       1.1  christos 	}
   1515       1.1  christos 
   1516       1.1  christos 	return 0;
   1517       1.1  christos }
   1518       1.1  christos 
   1519       1.1  christos 
   1520       1.1  christos /* ------------------------------------------------------------------------ */
   1521       1.1  christos /* Function:    ipf_pr_tcp                                                  */
   1522       1.1  christos /* Returns:     void                                                        */
   1523       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1524       1.1  christos /*                                                                          */
   1525       1.1  christos /* IPv4 Only                                                                */
   1526       1.1  christos /* Analyse the packet for IPv4/TCP properties.                              */
   1527       1.1  christos /* ------------------------------------------------------------------------ */
   1528       1.1  christos static INLINE void
   1529       1.2  christos ipf_pr_tcp(fr_info_t *fin)
   1530       1.1  christos {
   1531       1.1  christos 
   1532       1.1  christos 	ipf_pr_short(fin, sizeof(tcphdr_t));
   1533       1.1  christos 
   1534       1.1  christos 	if (ipf_pr_tcpcommon(fin) == 0)
   1535       1.1  christos 		ipf_checkv4sum(fin);
   1536       1.1  christos }
   1537       1.1  christos 
   1538       1.1  christos 
   1539       1.1  christos /* ------------------------------------------------------------------------ */
   1540       1.1  christos /* Function:    ipf_pr_udp                                                  */
   1541       1.1  christos /* Returns:     void                                                        */
   1542       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1543       1.1  christos /*                                                                          */
   1544       1.1  christos /* IPv4 Only                                                                */
   1545       1.1  christos /* Analyse the packet for IPv4/UDP properties.                              */
   1546       1.1  christos /* ------------------------------------------------------------------------ */
   1547       1.1  christos static INLINE void
   1548       1.2  christos ipf_pr_udp(fr_info_t *fin)
   1549       1.1  christos {
   1550       1.1  christos 
   1551       1.1  christos 	ipf_pr_short(fin, sizeof(udphdr_t));
   1552       1.1  christos 
   1553       1.1  christos 	if (ipf_pr_udpcommon(fin) == 0)
   1554       1.1  christos 		ipf_checkv4sum(fin);
   1555       1.1  christos }
   1556       1.1  christos 
   1557       1.1  christos 
   1558       1.1  christos /* ------------------------------------------------------------------------ */
   1559       1.1  christos /* Function:    ipf_pr_esp                                                  */
   1560       1.1  christos /* Returns:     void                                                        */
   1561       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1562       1.1  christos /*                                                                          */
   1563       1.1  christos /* Analyse the packet for ESP properties.                                   */
   1564       1.1  christos /* The minimum length is taken to be the SPI (32bits) plus a tail (32bits)  */
   1565       1.1  christos /* even though the newer ESP packets must also have a sequence number that  */
   1566       1.1  christos /* is 32bits as well, it is not possible(?) to determine the version from a */
   1567       1.1  christos /* simple packet header.                                                    */
   1568       1.1  christos /* ------------------------------------------------------------------------ */
   1569       1.1  christos static INLINE void
   1570       1.2  christos ipf_pr_esp(fr_info_t *fin)
   1571       1.1  christos {
   1572       1.1  christos 
   1573       1.1  christos 	if (fin->fin_off == 0) {
   1574       1.1  christos 		ipf_pr_short(fin, 8);
   1575       1.1  christos 		if (ipf_pr_pullup(fin, 8) == -1) {
   1576       1.1  christos 			ipf_main_softc_t *softc = fin->fin_main_soft;
   1577       1.1  christos 
   1578       1.1  christos 			LBUMPD(ipf_stats[fin->fin_out], fr_v4_esp_pullup);
   1579       1.1  christos 		}
   1580       1.1  christos 	}
   1581       1.1  christos }
   1582       1.1  christos 
   1583       1.1  christos 
   1584       1.1  christos /* ------------------------------------------------------------------------ */
   1585       1.1  christos /* Function:    ipf_pr_ah                                                   */
   1586       1.1  christos /* Returns:     int    - value of the next header or IPPROTO_NONE if error  */
   1587       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1588       1.1  christos /*                                                                          */
   1589       1.1  christos /* Analyse the packet for AH properties.                                    */
   1590       1.1  christos /* The minimum length is taken to be the combination of all fields in the   */
   1591       1.1  christos /* header being present and no authentication data (null algorithm used.)   */
   1592       1.1  christos /* ------------------------------------------------------------------------ */
   1593       1.1  christos static INLINE int
   1594       1.2  christos ipf_pr_ah(fr_info_t *fin)
   1595       1.1  christos {
   1596       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1597       1.1  christos 	authhdr_t *ah;
   1598       1.1  christos 	int len;
   1599       1.1  christos 
   1600       1.1  christos 	fin->fin_flx |= FI_AH;
   1601       1.1  christos 	ipf_pr_short(fin, sizeof(*ah));
   1602       1.1  christos 
   1603       1.1  christos 	if (((fin->fin_flx & FI_SHORT) != 0) || (fin->fin_off != 0)) {
   1604       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v4_ah_bad);
   1605       1.1  christos 		return IPPROTO_NONE;
   1606       1.1  christos 	}
   1607       1.1  christos 
   1608       1.1  christos 	if (ipf_pr_pullup(fin, sizeof(*ah)) == -1) {
   1609       1.1  christos 		DT(fr_v4_ah_pullup_1);
   1610       1.1  christos 		LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup);
   1611       1.1  christos 		return IPPROTO_NONE;
   1612       1.1  christos 	}
   1613       1.1  christos 
   1614       1.1  christos 	ah = (authhdr_t *)fin->fin_dp;
   1615       1.1  christos 
   1616       1.1  christos 	len = (ah->ah_plen + 2) << 2;
   1617       1.1  christos 	ipf_pr_short(fin, len);
   1618       1.1  christos 	if (ipf_pr_pullup(fin, len) == -1) {
   1619       1.1  christos 		DT(fr_v4_ah_pullup_2);
   1620       1.1  christos 		LBUMP(ipf_stats[fin->fin_out].fr_v4_ah_pullup);
   1621       1.1  christos 		return IPPROTO_NONE;
   1622       1.1  christos 	}
   1623       1.1  christos 
   1624       1.1  christos 	/*
   1625       1.1  christos 	 * Adjust fin_dp and fin_dlen for skipping over the authentication
   1626       1.1  christos 	 * header.
   1627       1.1  christos 	 */
   1628       1.1  christos 	fin->fin_dp = (char *)fin->fin_dp + len;
   1629       1.1  christos 	fin->fin_dlen -= len;
   1630       1.1  christos 	return ah->ah_next;
   1631       1.1  christos }
   1632       1.1  christos 
   1633       1.1  christos 
   1634       1.1  christos /* ------------------------------------------------------------------------ */
   1635       1.1  christos /* Function:    ipf_pr_gre                                                  */
   1636       1.1  christos /* Returns:     void                                                        */
   1637       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1638       1.1  christos /*                                                                          */
   1639       1.1  christos /* Analyse the packet for GRE properties.                                   */
   1640       1.1  christos /* ------------------------------------------------------------------------ */
   1641       1.1  christos static INLINE void
   1642       1.2  christos ipf_pr_gre(fr_info_t *fin)
   1643       1.1  christos {
   1644       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1645       1.1  christos 	grehdr_t *gre;
   1646       1.1  christos 
   1647       1.1  christos 	ipf_pr_short(fin, sizeof(grehdr_t));
   1648       1.1  christos 
   1649       1.1  christos 	if (fin->fin_off != 0) {
   1650       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_frag);
   1651       1.1  christos 		return;
   1652       1.1  christos 	}
   1653       1.1  christos 
   1654       1.1  christos 	if (ipf_pr_pullup(fin, sizeof(grehdr_t)) == -1) {
   1655       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v4_gre_pullup);
   1656       1.1  christos 		return;
   1657       1.1  christos 	}
   1658       1.1  christos 
   1659       1.1  christos 	gre = fin->fin_dp;
   1660       1.1  christos 	if (GRE_REV(gre->gr_flags) == 1)
   1661       1.1  christos 		fin->fin_data[0] = gre->gr_call;
   1662       1.1  christos }
   1663       1.1  christos 
   1664       1.1  christos 
   1665       1.1  christos /* ------------------------------------------------------------------------ */
   1666       1.1  christos /* Function:    ipf_pr_ipv4hdr                                              */
   1667       1.1  christos /* Returns:     void                                                        */
   1668       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   1669       1.1  christos /*                                                                          */
   1670       1.1  christos /* IPv4 Only                                                                */
   1671       1.1  christos /* Analyze the IPv4 header and set fields in the fr_info_t structure.       */
   1672       1.1  christos /* Check all options present and flag their presence if any exist.          */
   1673       1.1  christos /* ------------------------------------------------------------------------ */
   1674       1.1  christos static INLINE void
   1675       1.2  christos ipf_pr_ipv4hdr(fr_info_t *fin)
   1676       1.1  christos {
   1677       1.1  christos 	u_short optmsk = 0, secmsk = 0, auth = 0;
   1678       1.1  christos 	int hlen, ol, mv, p, i;
   1679       1.1  christos 	const struct optlist *op;
   1680       1.1  christos 	u_char *s, opt;
   1681       1.1  christos 	u_short off;
   1682       1.1  christos 	fr_ip_t *fi;
   1683       1.1  christos 	ip_t *ip;
   1684       1.1  christos 
   1685       1.1  christos 	fi = &fin->fin_fi;
   1686       1.1  christos 	hlen = fin->fin_hlen;
   1687       1.1  christos 
   1688       1.1  christos 	ip = fin->fin_ip;
   1689       1.1  christos 	p = ip->ip_p;
   1690       1.1  christos 	fi->fi_p = p;
   1691       1.1  christos 	fin->fin_crc = p;
   1692       1.1  christos 	fi->fi_tos = ip->ip_tos;
   1693       1.1  christos 	fin->fin_id = ip->ip_id;
   1694       1.1  christos 	off = ntohs(ip->ip_off);
   1695       1.1  christos 
   1696       1.1  christos 	/* Get both TTL and protocol */
   1697       1.1  christos 	fi->fi_p = ip->ip_p;
   1698       1.1  christos 	fi->fi_ttl = ip->ip_ttl;
   1699       1.1  christos 
   1700       1.1  christos 	/* Zero out bits not used in IPv6 address */
   1701       1.1  christos 	fi->fi_src.i6[1] = 0;
   1702       1.1  christos 	fi->fi_src.i6[2] = 0;
   1703       1.1  christos 	fi->fi_src.i6[3] = 0;
   1704       1.1  christos 	fi->fi_dst.i6[1] = 0;
   1705       1.1  christos 	fi->fi_dst.i6[2] = 0;
   1706       1.1  christos 	fi->fi_dst.i6[3] = 0;
   1707       1.1  christos 
   1708       1.1  christos 	fi->fi_saddr = ip->ip_src.s_addr;
   1709       1.1  christos 	fin->fin_crc += fi->fi_saddr;
   1710       1.1  christos 	fi->fi_daddr = ip->ip_dst.s_addr;
   1711       1.1  christos 	fin->fin_crc += fi->fi_daddr;
   1712  1.15.4.2     skrll 	if (IN_CLASSD(ntohl(fi->fi_daddr)))
   1713       1.1  christos 		fin->fin_flx |= FI_MULTICAST|FI_MBCAST;
   1714       1.1  christos 
   1715       1.1  christos 	/*
   1716       1.1  christos 	 * set packet attribute flags based on the offset and
   1717       1.1  christos 	 * calculate the byte offset that it represents.
   1718       1.1  christos 	 */
   1719       1.1  christos 	off &= IP_MF|IP_OFFMASK;
   1720       1.1  christos 	if (off != 0) {
   1721       1.1  christos 		int morefrag = off & IP_MF;
   1722       1.1  christos 
   1723       1.1  christos 		fi->fi_flx |= FI_FRAG;
   1724       1.1  christos 		off &= IP_OFFMASK;
   1725       1.1  christos 		if (off != 0) {
   1726       1.1  christos 			fin->fin_flx |= FI_FRAGBODY;
   1727       1.1  christos 			off <<= 3;
   1728       1.1  christos 			if ((off + fin->fin_dlen > 65535) ||
   1729       1.1  christos 			    (fin->fin_dlen == 0) ||
   1730       1.1  christos 			    ((morefrag != 0) && ((fin->fin_dlen & 7) != 0))) {
   1731       1.1  christos 				/*
   1732       1.1  christos 				 * The length of the packet, starting at its
   1733       1.1  christos 				 * offset cannot exceed 65535 (0xffff) as the
   1734       1.1  christos 				 * length of an IP packet is only 16 bits.
   1735       1.1  christos 				 *
   1736       1.1  christos 				 * Any fragment that isn't the last fragment
   1737       1.1  christos 				 * must have a length greater than 0 and it
   1738       1.1  christos 				 * must be an even multiple of 8.
   1739       1.1  christos 				 */
   1740       1.1  christos 				fi->fi_flx |= FI_BAD;
   1741  1.15.4.2     skrll 				DT1(ipf_fi_bad_fragbody_gt_65535, fr_info_t *, fin);
   1742       1.1  christos 			}
   1743       1.1  christos 		}
   1744       1.1  christos 	}
   1745       1.1  christos 	fin->fin_off = off;
   1746       1.1  christos 
   1747       1.1  christos 	/*
   1748       1.1  christos 	 * Call per-protocol setup and checking
   1749       1.1  christos 	 */
   1750       1.1  christos 	if (p == IPPROTO_AH) {
   1751       1.1  christos 		/*
   1752       1.1  christos 		 * Treat AH differently because we expect there to be another
   1753       1.1  christos 		 * layer 4 header after it.
   1754       1.1  christos 		 */
   1755       1.1  christos 		p = ipf_pr_ah(fin);
   1756       1.1  christos 	}
   1757       1.1  christos 
   1758       1.1  christos 	switch (p)
   1759       1.1  christos 	{
   1760       1.1  christos 	case IPPROTO_UDP :
   1761       1.1  christos 		ipf_pr_udp(fin);
   1762       1.1  christos 		break;
   1763       1.1  christos 	case IPPROTO_TCP :
   1764       1.1  christos 		ipf_pr_tcp(fin);
   1765       1.1  christos 		break;
   1766       1.1  christos 	case IPPROTO_ICMP :
   1767       1.1  christos 		ipf_pr_icmp(fin);
   1768       1.1  christos 		break;
   1769       1.1  christos 	case IPPROTO_ESP :
   1770       1.1  christos 		ipf_pr_esp(fin);
   1771       1.1  christos 		break;
   1772       1.1  christos 	case IPPROTO_GRE :
   1773       1.1  christos 		ipf_pr_gre(fin);
   1774       1.1  christos 		break;
   1775       1.1  christos 	}
   1776       1.1  christos 
   1777       1.1  christos 	ip = fin->fin_ip;
   1778       1.1  christos 	if (ip == NULL)
   1779       1.1  christos 		return;
   1780       1.1  christos 
   1781       1.1  christos 	/*
   1782       1.1  christos 	 * If it is a standard IP header (no options), set the flag fields
   1783       1.1  christos 	 * which relate to options to 0.
   1784       1.1  christos 	 */
   1785       1.1  christos 	if (hlen == sizeof(*ip)) {
   1786       1.1  christos 		fi->fi_optmsk = 0;
   1787       1.1  christos 		fi->fi_secmsk = 0;
   1788       1.1  christos 		fi->fi_auth = 0;
   1789       1.1  christos 		return;
   1790       1.1  christos 	}
   1791       1.1  christos 
   1792       1.1  christos 	/*
   1793       1.1  christos 	 * So the IP header has some IP options attached.  Walk the entire
   1794       1.1  christos 	 * list of options present with this packet and set flags to indicate
   1795       1.1  christos 	 * which ones are here and which ones are not.  For the somewhat out
   1796       1.1  christos 	 * of date and obscure security classification options, set a flag to
   1797       1.1  christos 	 * represent which classification is present.
   1798       1.1  christos 	 */
   1799       1.1  christos 	fi->fi_flx |= FI_OPTIONS;
   1800       1.1  christos 
   1801       1.1  christos 	for (s = (u_char *)(ip + 1), hlen -= (int)sizeof(*ip); hlen > 0; ) {
   1802       1.1  christos 		opt = *s;
   1803       1.1  christos 		if (opt == '\0')
   1804       1.1  christos 			break;
   1805       1.1  christos 		else if (opt == IPOPT_NOP)
   1806       1.1  christos 			ol = 1;
   1807       1.1  christos 		else {
   1808       1.1  christos 			if (hlen < 2)
   1809       1.1  christos 				break;
   1810       1.1  christos 			ol = (int)*(s + 1);
   1811       1.1  christos 			if (ol < 2 || ol > hlen)
   1812       1.1  christos 				break;
   1813       1.1  christos 		}
   1814       1.1  christos 		for (i = 9, mv = 4; mv >= 0; ) {
   1815       1.1  christos 			op = ipopts + i;
   1816       1.1  christos 
   1817       1.1  christos 			if ((opt == (u_char)op->ol_val) && (ol > 4)) {
   1818       1.1  christos 				u_32_t doi;
   1819       1.1  christos 
   1820       1.1  christos 				switch (opt)
   1821       1.1  christos 				{
   1822       1.1  christos 				case IPOPT_SECURITY :
   1823       1.1  christos 					if (optmsk & op->ol_bit) {
   1824       1.1  christos 						fin->fin_flx |= FI_BAD;
   1825  1.15.4.2     skrll 						DT2(ipf_fi_bad_ipopt_security, fr_info_t *, fin, u_short, (optmsk & op->ol_bit));
   1826       1.1  christos 					} else {
   1827       1.1  christos 						doi = ipf_checkripso(s);
   1828       1.1  christos 						secmsk = doi >> 16;
   1829       1.1  christos 						auth = doi & 0xffff;
   1830       1.1  christos 					}
   1831       1.1  christos 					break;
   1832       1.1  christos 
   1833       1.1  christos 				case IPOPT_CIPSO :
   1834       1.1  christos 
   1835       1.1  christos 					if (optmsk & op->ol_bit) {
   1836       1.1  christos 						fin->fin_flx |= FI_BAD;
   1837  1.15.4.2     skrll 						DT2(ipf_fi_bad_ipopt_cipso, fr_info_t *, fin, u_short, (optmsk & op->ol_bit));
   1838       1.1  christos 					} else {
   1839       1.1  christos 						doi = ipf_checkcipso(fin,
   1840       1.1  christos 								     s, ol);
   1841       1.1  christos 						secmsk = doi >> 16;
   1842       1.1  christos 						auth = doi & 0xffff;
   1843       1.1  christos 					}
   1844       1.1  christos 					break;
   1845       1.1  christos 				}
   1846       1.1  christos 				optmsk |= op->ol_bit;
   1847       1.1  christos 			}
   1848       1.1  christos 
   1849       1.1  christos 			if (opt < op->ol_val)
   1850       1.1  christos 				i -= mv;
   1851       1.1  christos 			else
   1852       1.1  christos 				i += mv;
   1853       1.1  christos 			mv--;
   1854       1.1  christos 		}
   1855       1.1  christos 		hlen -= ol;
   1856       1.1  christos 		s += ol;
   1857       1.1  christos 	}
   1858       1.1  christos 
   1859       1.1  christos 	/*
   1860       1.1  christos 	 *
   1861       1.1  christos 	 */
   1862       1.1  christos 	if (auth && !(auth & 0x0100))
   1863       1.1  christos 		auth &= 0xff00;
   1864       1.1  christos 	fi->fi_optmsk = optmsk;
   1865       1.1  christos 	fi->fi_secmsk = secmsk;
   1866       1.1  christos 	fi->fi_auth = auth;
   1867       1.1  christos }
   1868       1.1  christos 
   1869       1.1  christos 
   1870       1.1  christos /* ------------------------------------------------------------------------ */
   1871       1.1  christos /* Function:    ipf_checkripso                                              */
   1872       1.1  christos /* Returns:     void                                                        */
   1873       1.1  christos /* Parameters:  s(I)   - pointer to start of RIPSO option                   */
   1874       1.1  christos /*                                                                          */
   1875       1.1  christos /* ------------------------------------------------------------------------ */
   1876       1.1  christos static u_32_t
   1877       1.2  christos ipf_checkripso(u_char *s)
   1878       1.1  christos {
   1879       1.1  christos 	const struct optlist *sp;
   1880       1.1  christos 	u_short secmsk = 0, auth = 0;
   1881       1.1  christos 	u_char sec;
   1882       1.1  christos 	int j, m;
   1883       1.1  christos 
   1884       1.1  christos 	sec = *(s + 2);	/* classification */
   1885       1.1  christos 	for (j = 3, m = 2; m >= 0; ) {
   1886       1.1  christos 		sp = secopt + j;
   1887       1.1  christos 		if (sec == sp->ol_val) {
   1888       1.1  christos 			secmsk |= sp->ol_bit;
   1889       1.1  christos 			auth = *(s + 3);
   1890       1.1  christos 			auth *= 256;
   1891       1.1  christos 			auth += *(s + 4);
   1892       1.1  christos 			break;
   1893       1.1  christos 		}
   1894       1.1  christos 		if (sec < sp->ol_val)
   1895       1.1  christos 			j -= m;
   1896       1.1  christos 		else
   1897       1.1  christos 			j += m;
   1898       1.1  christos 		m--;
   1899       1.1  christos 	}
   1900       1.1  christos 
   1901       1.1  christos 	return (secmsk << 16) | auth;
   1902       1.1  christos }
   1903       1.1  christos 
   1904       1.1  christos 
   1905       1.1  christos /* ------------------------------------------------------------------------ */
   1906       1.1  christos /* Function:    ipf_checkcipso                                              */
   1907       1.1  christos /* Returns:     u_32_t  - 0 = failure, else the doi from the header         */
   1908       1.1  christos /* Parameters:  fin(IO) - pointer to packet information                     */
   1909       1.1  christos /*              s(I)    - pointer to start of CIPSO option                  */
   1910       1.1  christos /*              ol(I)   - length of CIPSO option field                      */
   1911       1.1  christos /*                                                                          */
   1912       1.1  christos /* This function returns the domain of integrity (DOI) field from the CIPSO */
   1913       1.1  christos /* header and returns that whilst also storing the highest sensitivity      */
   1914       1.1  christos /* value found in the fr_info_t structure.                                  */
   1915       1.1  christos /*                                                                          */
   1916       1.1  christos /* No attempt is made to extract the category bitmaps as these are defined  */
   1917       1.1  christos /* by the user (rather than the protocol) and can be rather numerous on the */
   1918       1.1  christos /* end nodes.                                                               */
   1919       1.1  christos /* ------------------------------------------------------------------------ */
   1920       1.1  christos static u_32_t
   1921       1.2  christos ipf_checkcipso(fr_info_t *fin, u_char *s, int ol)
   1922       1.1  christos {
   1923       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   1924       1.1  christos 	fr_ip_t *fi;
   1925       1.1  christos 	u_32_t doi;
   1926       1.1  christos 	u_char *t, tag, tlen, sensitivity;
   1927       1.1  christos 	int len;
   1928       1.1  christos 
   1929       1.1  christos 	if (ol < 6 || ol > 40) {
   1930       1.1  christos 		LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_bad);
   1931       1.1  christos 		fin->fin_flx |= FI_BAD;
   1932  1.15.4.2     skrll 		DT2(ipf_fi_bad_checkcipso_ol, fr_info_t *, fin, u_int, ol);
   1933       1.1  christos 		return 0;
   1934       1.1  christos 	}
   1935       1.1  christos 
   1936       1.1  christos 	fi = &fin->fin_fi;
   1937       1.1  christos 	fi->fi_sensitivity = 0;
   1938       1.1  christos 	/*
   1939       1.1  christos 	 * The DOI field MUST be there.
   1940       1.1  christos 	 */
   1941       1.1  christos 	bcopy(s + 2, &doi, sizeof(doi));
   1942       1.1  christos 
   1943       1.1  christos 	t = (u_char *)s + 6;
   1944       1.1  christos 	for (len = ol - 6; len >= 2; len -= tlen, t+= tlen) {
   1945       1.1  christos 		tag = *t;
   1946       1.1  christos 		tlen = *(t + 1);
   1947       1.1  christos 		if (tlen > len || tlen < 4 || tlen > 34) {
   1948       1.1  christos 			LBUMPD(ipf_stats[fin->fin_out], fr_v4_cipso_tlen);
   1949       1.1  christos 			fin->fin_flx |= FI_BAD;
   1950  1.15.4.2     skrll 			DT2(ipf_fi_bad_checkcipso_tlen, fr_info_t *, fin, u_int, tlen);
   1951       1.1  christos 			return 0;
   1952       1.1  christos 		}
   1953       1.1  christos 
   1954       1.1  christos 		sensitivity = 0;
   1955       1.1  christos 		/*
   1956       1.1  christos 		 * Tag numbers 0, 1, 2, 5 are laid out in the CIPSO Internet
   1957       1.1  christos 		 * draft (16 July 1992) that has expired.
   1958       1.1  christos 		 */
   1959       1.1  christos 		if (tag == 0) {
   1960       1.1  christos 			fin->fin_flx |= FI_BAD;
   1961  1.15.4.2     skrll 			DT2(ipf_fi_bad_checkcipso_tag, fr_info_t *, fin, u_int, tag);
   1962       1.1  christos 			continue;
   1963       1.1  christos 		} else if (tag == 1) {
   1964       1.1  christos 			if (*(t + 2) != 0) {
   1965       1.1  christos 				fin->fin_flx |= FI_BAD;
   1966  1.15.4.2     skrll 				DT2(ipf_fi_bad_checkcipso_tag1_t2, fr_info_t *, fin, u_int, (*t + 2));
   1967       1.1  christos 				continue;
   1968       1.1  christos 			}
   1969       1.1  christos 			sensitivity = *(t + 3);
   1970       1.1  christos 			/* Category bitmap for categories 0-239 */
   1971       1.1  christos 
   1972       1.1  christos 		} else if (tag == 4) {
   1973       1.1  christos 			if (*(t + 2) != 0) {
   1974       1.1  christos 				fin->fin_flx |= FI_BAD;
   1975  1.15.4.2     skrll 				DT2(ipf_fi_bad_checkcipso_tag4_t2, fr_info_t *, fin, u_int, (*t + 2));
   1976       1.1  christos 				continue;
   1977       1.1  christos 			}
   1978       1.1  christos 			sensitivity = *(t + 3);
   1979       1.1  christos 			/* Enumerated categories, 16bits each, upto 15 */
   1980       1.1  christos 
   1981       1.1  christos 		} else if (tag == 5) {
   1982       1.1  christos 			if (*(t + 2) != 0) {
   1983       1.1  christos 				fin->fin_flx |= FI_BAD;
   1984  1.15.4.2     skrll 				DT2(ipf_fi_bad_checkcipso_tag5_t2, fr_info_t *, fin, u_int, (*t + 2));
   1985       1.1  christos 				continue;
   1986       1.1  christos 			}
   1987       1.1  christos 			sensitivity = *(t + 3);
   1988       1.1  christos 			/* Range of categories (2*16bits), up to 7 pairs */
   1989       1.1  christos 
   1990       1.1  christos 		} else if (tag > 127) {
   1991       1.1  christos 			/* Custom defined DOI */
   1992       1.1  christos 			;
   1993       1.1  christos 		} else {
   1994  1.15.4.2     skrll 			DT2(ipf_fi_bad_checkcipso_tag127, fr_info_t *, fin, u_int, tag);
   1995       1.1  christos 			fin->fin_flx |= FI_BAD;
   1996       1.1  christos 			continue;
   1997       1.1  christos 		}
   1998       1.1  christos 
   1999       1.1  christos 		if (sensitivity > fi->fi_sensitivity)
   2000       1.1  christos 			fi->fi_sensitivity = sensitivity;
   2001       1.1  christos 	}
   2002       1.1  christos 
   2003       1.1  christos 	return doi;
   2004       1.1  christos }
   2005       1.1  christos 
   2006       1.1  christos 
   2007       1.1  christos /* ------------------------------------------------------------------------ */
   2008       1.1  christos /* Function:    ipf_makefrip                                                */
   2009       1.1  christos /* Returns:     int     - 0 == packet ok, -1 == packet freed                */
   2010       1.1  christos /* Parameters:  hlen(I) - length of IP packet header                        */
   2011       1.1  christos /*              ip(I)   - pointer to the IP header                          */
   2012       1.1  christos /*              fin(IO) - pointer to packet information                     */
   2013       1.1  christos /*                                                                          */
   2014       1.1  christos /* Compact the IP header into a structure which contains just the info.     */
   2015       1.1  christos /* which is useful for comparing IP headers with and store this information */
   2016       1.1  christos /* in the fr_info_t structure pointer to by fin.  At present, it is assumed */
   2017       1.1  christos /* this function will be called with either an IPv4 or IPv6 packet.         */
   2018       1.1  christos /* ------------------------------------------------------------------------ */
   2019       1.1  christos int
   2020       1.2  christos ipf_makefrip(int hlen, ip_t *ip, fr_info_t *fin)
   2021       1.1  christos {
   2022       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2023       1.1  christos 	int v;
   2024       1.1  christos 
   2025       1.1  christos 	fin->fin_depth = 0;
   2026       1.1  christos 	fin->fin_hlen = (u_short)hlen;
   2027       1.1  christos 	fin->fin_ip = ip;
   2028       1.1  christos 	fin->fin_rule = 0xffffffff;
   2029       1.1  christos 	fin->fin_group[0] = -1;
   2030       1.1  christos 	fin->fin_group[1] = '\0';
   2031       1.1  christos 	fin->fin_dp = (char *)ip + hlen;
   2032       1.1  christos 
   2033       1.1  christos 	v = fin->fin_v;
   2034       1.1  christos 	if (v == 4) {
   2035       1.1  christos 		fin->fin_plen = ntohs(ip->ip_len);
   2036       1.1  christos 		fin->fin_dlen = fin->fin_plen - hlen;
   2037       1.1  christos 		ipf_pr_ipv4hdr(fin);
   2038       1.1  christos #ifdef	USE_INET6
   2039       1.1  christos 	} else if (v == 6) {
   2040       1.1  christos 		fin->fin_plen = ntohs(((ip6_t *)ip)->ip6_plen);
   2041       1.1  christos 		fin->fin_dlen = fin->fin_plen;
   2042       1.1  christos 		fin->fin_plen += hlen;
   2043       1.1  christos 
   2044       1.1  christos 		ipf_pr_ipv6hdr(fin);
   2045       1.1  christos #endif
   2046       1.1  christos 	}
   2047       1.1  christos 	if (fin->fin_ip == NULL) {
   2048       1.1  christos 		LBUMP(ipf_stats[fin->fin_out].fr_ip_freed);
   2049       1.1  christos 		return -1;
   2050       1.1  christos 	}
   2051       1.1  christos 	return 0;
   2052       1.1  christos }
   2053       1.1  christos 
   2054       1.1  christos 
   2055       1.1  christos /* ------------------------------------------------------------------------ */
   2056       1.1  christos /* Function:    ipf_portcheck                                               */
   2057       1.1  christos /* Returns:     int - 1 == port matched, 0 == port match failed             */
   2058       1.1  christos /* Parameters:  frp(I) - pointer to port check `expression'                 */
   2059       1.1  christos /*              pop(I) - port number to evaluate                            */
   2060       1.1  christos /*                                                                          */
   2061       1.1  christos /* Perform a comparison of a port number against some other(s), using a     */
   2062       1.1  christos /* structure with compare information stored in it.                         */
   2063       1.1  christos /* ------------------------------------------------------------------------ */
   2064       1.1  christos static INLINE int
   2065       1.2  christos ipf_portcheck(frpcmp_t *frp, u_32_t pop)
   2066       1.1  christos {
   2067       1.1  christos 	int err = 1;
   2068       1.1  christos 	u_32_t po;
   2069       1.1  christos 
   2070       1.1  christos 	po = frp->frp_port;
   2071       1.1  christos 
   2072       1.1  christos 	/*
   2073       1.1  christos 	 * Do opposite test to that required and continue if that succeeds.
   2074       1.1  christos 	 */
   2075       1.1  christos 	switch (frp->frp_cmp)
   2076       1.1  christos 	{
   2077       1.1  christos 	case FR_EQUAL :
   2078       1.1  christos 		if (pop != po) /* EQUAL */
   2079       1.1  christos 			err = 0;
   2080       1.1  christos 		break;
   2081       1.1  christos 	case FR_NEQUAL :
   2082       1.1  christos 		if (pop == po) /* NOTEQUAL */
   2083       1.1  christos 			err = 0;
   2084       1.1  christos 		break;
   2085       1.1  christos 	case FR_LESST :
   2086       1.1  christos 		if (pop >= po) /* LESSTHAN */
   2087       1.1  christos 			err = 0;
   2088       1.1  christos 		break;
   2089       1.1  christos 	case FR_GREATERT :
   2090       1.1  christos 		if (pop <= po) /* GREATERTHAN */
   2091       1.1  christos 			err = 0;
   2092       1.1  christos 		break;
   2093       1.1  christos 	case FR_LESSTE :
   2094       1.1  christos 		if (pop > po) /* LT or EQ */
   2095       1.1  christos 			err = 0;
   2096       1.1  christos 		break;
   2097       1.1  christos 	case FR_GREATERTE :
   2098       1.1  christos 		if (pop < po) /* GT or EQ */
   2099       1.1  christos 			err = 0;
   2100       1.1  christos 		break;
   2101       1.1  christos 	case FR_OUTRANGE :
   2102       1.1  christos 		if (pop >= po && pop <= frp->frp_top) /* Out of range */
   2103       1.1  christos 			err = 0;
   2104       1.1  christos 		break;
   2105       1.1  christos 	case FR_INRANGE :
   2106       1.1  christos 		if (pop <= po || pop >= frp->frp_top) /* In range */
   2107       1.1  christos 			err = 0;
   2108       1.1  christos 		break;
   2109       1.1  christos 	case FR_INCRANGE :
   2110       1.1  christos 		if (pop < po || pop > frp->frp_top) /* Inclusive range */
   2111       1.1  christos 			err = 0;
   2112       1.1  christos 		break;
   2113       1.1  christos 	default :
   2114       1.1  christos 		break;
   2115       1.1  christos 	}
   2116       1.1  christos 	return err;
   2117       1.1  christos }
   2118       1.1  christos 
   2119       1.1  christos 
   2120       1.1  christos /* ------------------------------------------------------------------------ */
   2121       1.1  christos /* Function:    ipf_tcpudpchk                                               */
   2122       1.1  christos /* Returns:     int - 1 == protocol matched, 0 == check failed              */
   2123       1.1  christos /* Parameters:  fda(I) - pointer to packet information                      */
   2124       1.1  christos /*              ft(I)  - pointer to structure with comparison data          */
   2125       1.1  christos /*                                                                          */
   2126       1.1  christos /* Compares the current pcket (assuming it is TCP/UDP) information with a   */
   2127       1.1  christos /* structure containing information that we want to match against.          */
   2128       1.1  christos /* ------------------------------------------------------------------------ */
   2129       1.1  christos int
   2130       1.2  christos ipf_tcpudpchk(fr_ip_t *fi, frtuc_t *ft)
   2131       1.1  christos {
   2132       1.1  christos 	int err = 1;
   2133       1.1  christos 
   2134       1.1  christos 	/*
   2135       1.1  christos 	 * Both ports should *always* be in the first fragment.
   2136       1.1  christos 	 * So far, I cannot find any cases where they can not be.
   2137       1.1  christos 	 *
   2138       1.1  christos 	 * compare destination ports
   2139       1.1  christos 	 */
   2140       1.1  christos 	if (ft->ftu_dcmp)
   2141       1.1  christos 		err = ipf_portcheck(&ft->ftu_dst, fi->fi_ports[1]);
   2142       1.1  christos 
   2143       1.1  christos 	/*
   2144       1.1  christos 	 * compare source ports
   2145       1.1  christos 	 */
   2146       1.1  christos 	if (err && ft->ftu_scmp)
   2147       1.1  christos 		err = ipf_portcheck(&ft->ftu_src, fi->fi_ports[0]);
   2148       1.1  christos 
   2149       1.1  christos 	/*
   2150       1.1  christos 	 * If we don't have all the TCP/UDP header, then how can we
   2151       1.1  christos 	 * expect to do any sort of match on it ?  If we were looking for
   2152       1.1  christos 	 * TCP flags, then NO match.  If not, then match (which should
   2153       1.1  christos 	 * satisfy the "short" class too).
   2154       1.1  christos 	 */
   2155       1.1  christos 	if (err && (fi->fi_p == IPPROTO_TCP)) {
   2156       1.1  christos 		if (fi->fi_flx & FI_SHORT)
   2157       1.1  christos 			return !(ft->ftu_tcpf | ft->ftu_tcpfm);
   2158       1.1  christos 		/*
   2159       1.1  christos 		 * Match the flags ?  If not, abort this match.
   2160       1.1  christos 		 */
   2161       1.1  christos 		if (ft->ftu_tcpfm &&
   2162       1.1  christos 		    ft->ftu_tcpf != (fi->fi_tcpf & ft->ftu_tcpfm)) {
   2163       1.1  christos 			FR_DEBUG(("f. %#x & %#x != %#x\n", fi->fi_tcpf,
   2164       1.1  christos 				 ft->ftu_tcpfm, ft->ftu_tcpf));
   2165       1.1  christos 			err = 0;
   2166       1.1  christos 		}
   2167       1.1  christos 	}
   2168       1.1  christos 	return err;
   2169       1.1  christos }
   2170       1.1  christos 
   2171       1.1  christos 
   2172       1.1  christos /* ------------------------------------------------------------------------ */
   2173       1.1  christos /* Function:    ipf_check_ipf                                               */
   2174       1.1  christos /* Returns:     int - 0 == match, else no match                             */
   2175       1.1  christos /* Parameters:  fin(I)     - pointer to packet information                  */
   2176       1.1  christos /*              fr(I)      - pointer to filter rule                         */
   2177       1.1  christos /*              portcmp(I) - flag indicating whether to attempt matching on */
   2178       1.1  christos /*                           TCP/UDP port data.                             */
   2179       1.1  christos /*                                                                          */
   2180       1.1  christos /* Check to see if a packet matches an IPFilter rule.  Checks of addresses, */
   2181       1.1  christos /* port numbers, etc, for "standard" IPFilter rules are all orchestrated in */
   2182       1.1  christos /* this function.                                                           */
   2183       1.1  christos /* ------------------------------------------------------------------------ */
   2184       1.1  christos static INLINE int
   2185       1.2  christos ipf_check_ipf(fr_info_t *fin, frentry_t *fr, int portcmp)
   2186       1.1  christos {
   2187       1.1  christos 	u_32_t	*ld, *lm, *lip;
   2188       1.1  christos 	fripf_t *fri;
   2189       1.1  christos 	fr_ip_t *fi;
   2190       1.1  christos 	int i;
   2191       1.1  christos 
   2192       1.1  christos 	fi = &fin->fin_fi;
   2193       1.1  christos 	fri = fr->fr_ipf;
   2194       1.1  christos 	lip = (u_32_t *)fi;
   2195       1.1  christos 	lm = (u_32_t *)&fri->fri_mip;
   2196       1.1  christos 	ld = (u_32_t *)&fri->fri_ip;
   2197       1.1  christos 
   2198       1.1  christos 	/*
   2199       1.1  christos 	 * first 32 bits to check coversion:
   2200       1.1  christos 	 * IP version, TOS, TTL, protocol
   2201       1.1  christos 	 */
   2202       1.1  christos 	i = ((*lip & *lm) != *ld);
   2203       1.1  christos 	FR_DEBUG(("0. %#08x & %#08x != %#08x\n",
   2204       1.1  christos 		   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2205       1.1  christos 	if (i)
   2206       1.1  christos 		return 1;
   2207       1.1  christos 
   2208       1.1  christos 	/*
   2209       1.1  christos 	 * Next 32 bits is a constructed bitmask indicating which IP options
   2210       1.1  christos 	 * are present (if any) in this packet.
   2211       1.1  christos 	 */
   2212       1.1  christos 	lip++, lm++, ld++;
   2213       1.1  christos 	i = ((*lip & *lm) != *ld);
   2214       1.1  christos 	FR_DEBUG(("1. %#08x & %#08x != %#08x\n",
   2215       1.1  christos 		   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2216       1.1  christos 	if (i != 0)
   2217       1.1  christos 		return 1;
   2218       1.1  christos 
   2219       1.1  christos 	lip++, lm++, ld++;
   2220       1.1  christos 	/*
   2221       1.1  christos 	 * Unrolled loops (4 each, for 32 bits) for address checks.
   2222       1.1  christos 	 */
   2223       1.1  christos 	/*
   2224       1.1  christos 	 * Check the source address.
   2225       1.1  christos 	 */
   2226       1.1  christos 	if (fr->fr_satype == FRI_LOOKUP) {
   2227       1.1  christos 		i = (*fr->fr_srcfunc)(fin->fin_main_soft, fr->fr_srcptr,
   2228       1.1  christos 				      fi->fi_v, lip, fin->fin_plen);
   2229       1.1  christos 		if (i == -1)
   2230       1.1  christos 			return 1;
   2231       1.1  christos 		lip += 3;
   2232       1.1  christos 		lm += 3;
   2233       1.1  christos 		ld += 3;
   2234       1.1  christos 	} else {
   2235       1.1  christos 		i = ((*lip & *lm) != *ld);
   2236       1.1  christos 		FR_DEBUG(("2a. %#08x & %#08x != %#08x\n",
   2237       1.1  christos 			   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2238       1.1  christos 		if (fi->fi_v == 6) {
   2239       1.1  christos 			lip++, lm++, ld++;
   2240       1.1  christos 			i |= ((*lip & *lm) != *ld);
   2241       1.1  christos 			FR_DEBUG(("2b. %#08x & %#08x != %#08x\n",
   2242       1.1  christos 				   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2243       1.1  christos 			lip++, lm++, ld++;
   2244       1.1  christos 			i |= ((*lip & *lm) != *ld);
   2245       1.1  christos 			FR_DEBUG(("2c. %#08x & %#08x != %#08x\n",
   2246       1.1  christos 				   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2247       1.1  christos 			lip++, lm++, ld++;
   2248       1.1  christos 			i |= ((*lip & *lm) != *ld);
   2249       1.1  christos 			FR_DEBUG(("2d. %#08x & %#08x != %#08x\n",
   2250       1.1  christos 				   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2251       1.1  christos 		} else {
   2252       1.1  christos 			lip += 3;
   2253       1.1  christos 			lm += 3;
   2254       1.1  christos 			ld += 3;
   2255       1.1  christos 		}
   2256       1.1  christos 	}
   2257       1.1  christos 	i ^= (fr->fr_flags & FR_NOTSRCIP) >> 6;
   2258       1.1  christos 	if (i != 0)
   2259       1.1  christos 		return 1;
   2260       1.1  christos 
   2261       1.1  christos 	/*
   2262       1.1  christos 	 * Check the destination address.
   2263       1.1  christos 	 */
   2264       1.1  christos 	lip++, lm++, ld++;
   2265       1.1  christos 	if (fr->fr_datype == FRI_LOOKUP) {
   2266       1.1  christos 		i = (*fr->fr_dstfunc)(fin->fin_main_soft, fr->fr_dstptr,
   2267       1.1  christos 				      fi->fi_v, lip, fin->fin_plen);
   2268       1.1  christos 		if (i == -1)
   2269       1.1  christos 			return 1;
   2270       1.1  christos 		lip += 3;
   2271       1.1  christos 		lm += 3;
   2272       1.1  christos 		ld += 3;
   2273       1.1  christos 	} else {
   2274       1.1  christos 		i = ((*lip & *lm) != *ld);
   2275       1.1  christos 		FR_DEBUG(("3a. %#08x & %#08x != %#08x\n",
   2276       1.1  christos 			   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2277       1.1  christos 		if (fi->fi_v == 6) {
   2278       1.1  christos 			lip++, lm++, ld++;
   2279       1.1  christos 			i |= ((*lip & *lm) != *ld);
   2280       1.1  christos 			FR_DEBUG(("3b. %#08x & %#08x != %#08x\n",
   2281       1.1  christos 				   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2282       1.1  christos 			lip++, lm++, ld++;
   2283       1.1  christos 			i |= ((*lip & *lm) != *ld);
   2284       1.1  christos 			FR_DEBUG(("3c. %#08x & %#08x != %#08x\n",
   2285       1.1  christos 				   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2286       1.1  christos 			lip++, lm++, ld++;
   2287       1.1  christos 			i |= ((*lip & *lm) != *ld);
   2288       1.1  christos 			FR_DEBUG(("3d. %#08x & %#08x != %#08x\n",
   2289       1.1  christos 				   ntohl(*lip), ntohl(*lm), ntohl(*ld)));
   2290       1.1  christos 		} else {
   2291       1.1  christos 			lip += 3;
   2292       1.1  christos 			lm += 3;
   2293       1.1  christos 			ld += 3;
   2294       1.1  christos 		}
   2295       1.1  christos 	}
   2296       1.1  christos 	i ^= (fr->fr_flags & FR_NOTDSTIP) >> 7;
   2297       1.1  christos 	if (i != 0)
   2298       1.1  christos 		return 1;
   2299       1.1  christos 	/*
   2300       1.1  christos 	 * IP addresses matched.  The next 32bits contains:
   2301       1.1  christos 	 * mast of old IP header security & authentication bits.
   2302       1.1  christos 	 */
   2303       1.1  christos 	lip++, lm++, ld++;
   2304       1.1  christos 	i = (*ld - (*lip & *lm));
   2305       1.1  christos 	FR_DEBUG(("4. %#08x & %#08x != %#08x\n", *lip, *lm, *ld));
   2306       1.1  christos 
   2307       1.1  christos 	/*
   2308       1.1  christos 	 * Next we have 32 bits of packet flags.
   2309       1.1  christos 	 */
   2310       1.1  christos 	lip++, lm++, ld++;
   2311       1.1  christos 	i |= (*ld - (*lip & *lm));
   2312       1.1  christos 	FR_DEBUG(("5. %#08x & %#08x != %#08x\n", *lip, *lm, *ld));
   2313       1.1  christos 
   2314       1.1  christos 	if (i == 0) {
   2315       1.1  christos 		/*
   2316       1.1  christos 		 * If a fragment, then only the first has what we're
   2317       1.1  christos 		 * looking for here...
   2318       1.1  christos 		 */
   2319       1.1  christos 		if (portcmp) {
   2320       1.1  christos 			if (!ipf_tcpudpchk(&fin->fin_fi, &fr->fr_tuc))
   2321       1.1  christos 				i = 1;
   2322       1.1  christos 		} else {
   2323       1.1  christos 			if (fr->fr_dcmp || fr->fr_scmp ||
   2324       1.1  christos 			    fr->fr_tcpf || fr->fr_tcpfm)
   2325       1.1  christos 				i = 1;
   2326       1.1  christos 			if (fr->fr_icmpm || fr->fr_icmp) {
   2327       1.1  christos 				if (((fi->fi_p != IPPROTO_ICMP) &&
   2328       1.1  christos 				     (fi->fi_p != IPPROTO_ICMPV6)) ||
   2329       1.1  christos 				    fin->fin_off || (fin->fin_dlen < 2))
   2330       1.1  christos 					i = 1;
   2331       1.1  christos 				else if ((fin->fin_data[0] & fr->fr_icmpm) !=
   2332       1.1  christos 					 fr->fr_icmp) {
   2333       1.1  christos 					FR_DEBUG(("i. %#x & %#x != %#x\n",
   2334       1.1  christos 						 fin->fin_data[0],
   2335       1.1  christos 						 fr->fr_icmpm, fr->fr_icmp));
   2336       1.1  christos 					i = 1;
   2337       1.1  christos 				}
   2338       1.1  christos 			}
   2339       1.1  christos 		}
   2340       1.1  christos 	}
   2341       1.1  christos 	return i;
   2342       1.1  christos }
   2343       1.1  christos 
   2344       1.1  christos 
   2345       1.1  christos /* ------------------------------------------------------------------------ */
   2346       1.1  christos /* Function:    ipf_scanlist                                                */
   2347       1.1  christos /* Returns:     int - result flags of scanning filter list                  */
   2348       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   2349       1.1  christos /*              pass(I) - default result to return for filtering            */
   2350       1.1  christos /*                                                                          */
   2351       1.1  christos /* Check the input/output list of rules for a match to the current packet.  */
   2352       1.1  christos /* If a match is found, the value of fr_flags from the rule becomes the     */
   2353       1.1  christos /* return value and fin->fin_fr points to the matched rule.                 */
   2354       1.1  christos /*                                                                          */
   2355       1.1  christos /* This function may be called recusively upto 16 times (limit inbuilt.)    */
   2356       1.1  christos /* When unwinding, it should finish up with fin_depth as 0.                 */
   2357       1.1  christos /*                                                                          */
   2358       1.1  christos /* Could be per interface, but this gets real nasty when you don't have,    */
   2359       1.1  christos /* or can't easily change, the kernel source code to .                      */
   2360       1.1  christos /* ------------------------------------------------------------------------ */
   2361       1.1  christos int
   2362       1.2  christos ipf_scanlist(fr_info_t *fin, u_32_t pass)
   2363       1.1  christos {
   2364       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2365       1.1  christos 	int rulen, portcmp, off, skip;
   2366       1.1  christos 	struct frentry *fr, *fnext;
   2367       1.1  christos 	u_32_t passt, passo;
   2368       1.1  christos 
   2369       1.1  christos 	/*
   2370       1.1  christos 	 * Do not allow nesting deeper than 16 levels.
   2371       1.1  christos 	 */
   2372       1.1  christos 	if (fin->fin_depth >= 16)
   2373       1.1  christos 		return pass;
   2374       1.1  christos 
   2375       1.1  christos 	fr = fin->fin_fr;
   2376       1.1  christos 
   2377       1.1  christos 	/*
   2378       1.1  christos 	 * If there are no rules in this list, return now.
   2379       1.1  christos 	 */
   2380       1.1  christos 	if (fr == NULL)
   2381       1.1  christos 		return pass;
   2382       1.1  christos 
   2383       1.1  christos 	skip = 0;
   2384       1.1  christos 	portcmp = 0;
   2385       1.1  christos 	fin->fin_depth++;
   2386       1.1  christos 	fin->fin_fr = NULL;
   2387       1.1  christos 	off = fin->fin_off;
   2388       1.1  christos 
   2389       1.1  christos 	if ((fin->fin_flx & FI_TCPUDP) && (fin->fin_dlen > 3) && !off)
   2390       1.1  christos 		portcmp = 1;
   2391       1.1  christos 
   2392       1.1  christos 	for (rulen = 0; fr; fr = fnext, rulen++) {
   2393       1.1  christos 		fnext = fr->fr_next;
   2394       1.1  christos 		if (skip != 0) {
   2395       1.1  christos 			FR_VERBOSE(("SKIP %d (%#x)\n", skip, fr->fr_flags));
   2396       1.1  christos 			skip--;
   2397       1.1  christos 			continue;
   2398       1.1  christos 		}
   2399       1.1  christos 
   2400       1.1  christos 		/*
   2401       1.1  christos 		 * In all checks below, a null (zero) value in the
   2402       1.1  christos 		 * filter struture is taken to mean a wildcard.
   2403       1.1  christos 		 *
   2404       1.1  christos 		 * check that we are working for the right interface
   2405       1.1  christos 		 */
   2406       1.1  christos #ifdef	_KERNEL
   2407       1.1  christos 		if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
   2408       1.1  christos 			continue;
   2409       1.1  christos #else
   2410       1.1  christos 		if (opts & (OPT_VERBOSE|OPT_DEBUG))
   2411       1.1  christos 			printf("\n");
   2412       1.1  christos 		FR_VERBOSE(("%c", FR_ISSKIP(pass) ? 's' :
   2413       1.1  christos 				  FR_ISPASS(pass) ? 'p' :
   2414       1.1  christos 				  FR_ISACCOUNT(pass) ? 'A' :
   2415       1.1  christos 				  FR_ISAUTH(pass) ? 'a' :
   2416       1.1  christos 				  (pass & FR_NOMATCH) ? 'n' :'b'));
   2417       1.1  christos 		if (fr->fr_ifa && fr->fr_ifa != fin->fin_ifp)
   2418       1.1  christos 			continue;
   2419       1.1  christos 		FR_VERBOSE((":i"));
   2420       1.1  christos #endif
   2421       1.1  christos 
   2422       1.1  christos 		switch (fr->fr_type)
   2423       1.1  christos 		{
   2424       1.1  christos 		case FR_T_IPF :
   2425       1.1  christos 		case FR_T_IPF_BUILTIN :
   2426       1.1  christos 			if (ipf_check_ipf(fin, fr, portcmp))
   2427       1.1  christos 				continue;
   2428       1.1  christos 			break;
   2429       1.1  christos #if defined(IPFILTER_BPF)
   2430       1.1  christos 		case FR_T_BPFOPC :
   2431       1.1  christos 		case FR_T_BPFOPC_BUILTIN :
   2432       1.1  christos 		    {
   2433       1.1  christos 			u_char *mc;
   2434       1.1  christos 			int wlen;
   2435       1.1  christos 
   2436       1.1  christos 			if (*fin->fin_mp == NULL)
   2437       1.1  christos 				continue;
   2438       1.1  christos 			if (fin->fin_family != fr->fr_family)
   2439       1.1  christos 				continue;
   2440       1.1  christos 			mc = (u_char *)fin->fin_m;
   2441       1.1  christos 			wlen = fin->fin_dlen + fin->fin_hlen;
   2442       1.1  christos 			if (!bpf_filter(fr->fr_data, mc, wlen, 0))
   2443       1.1  christos 				continue;
   2444       1.1  christos 			break;
   2445       1.1  christos 		    }
   2446       1.1  christos #endif
   2447       1.1  christos 		case FR_T_CALLFUNC_BUILTIN :
   2448       1.1  christos 		    {
   2449       1.1  christos 			frentry_t *f;
   2450       1.1  christos 
   2451       1.1  christos 			f = (*fr->fr_func)(fin, &pass);
   2452       1.1  christos 			if (f != NULL)
   2453       1.1  christos 				fr = f;
   2454       1.1  christos 			else
   2455       1.1  christos 				continue;
   2456       1.1  christos 			break;
   2457       1.1  christos 		    }
   2458       1.1  christos 
   2459       1.1  christos 		case FR_T_IPFEXPR :
   2460       1.1  christos 		case FR_T_IPFEXPR_BUILTIN :
   2461       1.1  christos 			if (fin->fin_family != fr->fr_family)
   2462       1.1  christos 				continue;
   2463       1.1  christos 			if (ipf_fr_matcharray(fin, fr->fr_data) == 0)
   2464       1.1  christos 				continue;
   2465       1.1  christos 			break;
   2466       1.1  christos 
   2467       1.1  christos 		default :
   2468       1.1  christos 			break;
   2469       1.1  christos 		}
   2470       1.1  christos 
   2471       1.1  christos 		if ((fin->fin_out == 0) && (fr->fr_nattag.ipt_num[0] != 0)) {
   2472       1.1  christos 			if (fin->fin_nattag == NULL)
   2473       1.1  christos 				continue;
   2474       1.1  christos 			if (ipf_matchtag(&fr->fr_nattag, fin->fin_nattag) == 0)
   2475       1.1  christos 				continue;
   2476       1.1  christos 		}
   2477       1.1  christos 		FR_VERBOSE(("=%d/%d.%d *", fr->fr_grhead, fr->fr_group, rulen));
   2478       1.1  christos 
   2479       1.1  christos 		passt = fr->fr_flags;
   2480       1.1  christos 
   2481       1.1  christos 		/*
   2482       1.1  christos 		 * If the rule is a "call now" rule, then call the function
   2483       1.1  christos 		 * in the rule, if it exists and use the results from that.
   2484       1.1  christos 		 * If the function pointer is bad, just make like we ignore
   2485       1.1  christos 		 * it, except for increasing the hit counter.
   2486       1.1  christos 		 */
   2487       1.1  christos 		if ((passt & FR_CALLNOW) != 0) {
   2488       1.1  christos 			frentry_t *frs;
   2489       1.1  christos 
   2490       1.1  christos 			ATOMIC_INC64(fr->fr_hits);
   2491       1.1  christos 			if ((fr->fr_func == NULL) ||
   2492       1.1  christos 			    (fr->fr_func == (ipfunc_t)-1))
   2493       1.1  christos 				continue;
   2494       1.1  christos 
   2495       1.1  christos 			frs = fin->fin_fr;
   2496       1.1  christos 			fin->fin_fr = fr;
   2497       1.1  christos 			fr = (*fr->fr_func)(fin, &passt);
   2498       1.1  christos 			if (fr == NULL) {
   2499       1.1  christos 				fin->fin_fr = frs;
   2500       1.1  christos 				continue;
   2501       1.1  christos 			}
   2502       1.1  christos 			passt = fr->fr_flags;
   2503       1.1  christos 		}
   2504       1.1  christos 		fin->fin_fr = fr;
   2505       1.1  christos 
   2506       1.1  christos #ifdef  IPFILTER_LOG
   2507       1.1  christos 		/*
   2508       1.1  christos 		 * Just log this packet...
   2509       1.1  christos 		 */
   2510       1.1  christos 		if ((passt & FR_LOGMASK) == FR_LOG) {
   2511       1.1  christos 			if (ipf_log_pkt(fin, passt) == -1) {
   2512       1.1  christos 				if (passt & FR_LOGORBLOCK) {
   2513       1.1  christos 					DT(frb_logfail);
   2514       1.1  christos 					passt &= ~FR_CMDMASK;
   2515       1.1  christos 					passt |= FR_BLOCK|FR_QUICK;
   2516       1.1  christos 					fin->fin_reason = FRB_LOGFAIL;
   2517       1.1  christos 				}
   2518       1.1  christos 			}
   2519       1.1  christos 		}
   2520       1.1  christos #endif /* IPFILTER_LOG */
   2521       1.1  christos 
   2522       1.1  christos 		MUTEX_ENTER(&fr->fr_lock);
   2523       1.1  christos 		fr->fr_bytes += (U_QUAD_T)fin->fin_plen;
   2524       1.1  christos 		fr->fr_hits++;
   2525       1.1  christos 		MUTEX_EXIT(&fr->fr_lock);
   2526       1.1  christos 		fin->fin_rule = rulen;
   2527       1.1  christos 
   2528       1.1  christos 		passo = pass;
   2529       1.1  christos 		if (FR_ISSKIP(passt)) {
   2530       1.1  christos 			skip = fr->fr_arg;
   2531       1.1  christos 			continue;
   2532       1.3   darrenr 		} else if (((passt & FR_LOGMASK) != FR_LOG) &&
   2533       1.3   darrenr 			   ((passt & FR_LOGMASK) != FR_DECAPSULATE)) {
   2534       1.1  christos 			pass = passt;
   2535       1.1  christos 		}
   2536       1.1  christos 
   2537       1.1  christos 		if (passt & (FR_RETICMP|FR_FAKEICMP))
   2538       1.1  christos 			fin->fin_icode = fr->fr_icode;
   2539       1.1  christos 
   2540       1.1  christos 		if (fr->fr_group != -1) {
   2541       1.1  christos 			(void) strncpy(fin->fin_group,
   2542       1.1  christos 				       FR_NAME(fr, fr_group),
   2543       1.1  christos 				       strlen(FR_NAME(fr, fr_group)));
   2544       1.1  christos 		} else {
   2545       1.1  christos 			fin->fin_group[0] = '\0';
   2546       1.1  christos 		}
   2547       1.1  christos 
   2548       1.3   darrenr 		FR_DEBUG(("pass %#x/%#x/%x\n", passo, pass, passt));
   2549       1.1  christos 
   2550       1.3   darrenr 		if (fr->fr_grphead != NULL) {
   2551       1.3   darrenr 			fin->fin_fr = fr->fr_grphead->fg_start;
   2552       1.1  christos 			FR_VERBOSE(("group %s\n", FR_NAME(fr, fr_grhead)));
   2553       1.1  christos 
   2554       1.3   darrenr 			if (FR_ISDECAPS(passt))
   2555       1.1  christos 				passt = ipf_decaps(fin, pass, fr->fr_icode);
   2556       1.1  christos 			else
   2557       1.1  christos 				passt = ipf_scanlist(fin, pass);
   2558       1.1  christos 
   2559       1.1  christos 			if (fin->fin_fr == NULL) {
   2560       1.1  christos 				fin->fin_rule = rulen;
   2561       1.1  christos 				if (fr->fr_group != -1)
   2562       1.1  christos 					(void) strncpy(fin->fin_group,
   2563       1.1  christos 						       fr->fr_names +
   2564       1.1  christos 						       fr->fr_group,
   2565       1.1  christos 						       strlen(fr->fr_names +
   2566       1.1  christos 							      fr->fr_group));
   2567       1.1  christos 				fin->fin_fr = fr;
   2568       1.1  christos 				passt = pass;
   2569       1.1  christos 			}
   2570       1.1  christos 			pass = passt;
   2571       1.1  christos 		}
   2572       1.1  christos 
   2573       1.1  christos 		if (pass & FR_QUICK) {
   2574       1.1  christos 			/*
   2575       1.1  christos 			 * Finally, if we've asked to track state for this
   2576       1.1  christos 			 * packet, set it up.  Add state for "quick" rules
   2577       1.1  christos 			 * here so that if the action fails we can consider
   2578       1.1  christos 			 * the rule to "not match" and keep on processing
   2579       1.1  christos 			 * filter rules.
   2580       1.1  christos 			 */
   2581       1.1  christos 			if ((pass & FR_KEEPSTATE) && !FR_ISAUTH(pass) &&
   2582       1.1  christos 			    !(fin->fin_flx & FI_STATE)) {
   2583       1.1  christos 				int out = fin->fin_out;
   2584       1.1  christos 
   2585       1.1  christos 				fin->fin_fr = fr;
   2586       1.1  christos 				if (ipf_state_add(softc, fin, NULL, 0) == 0) {
   2587       1.1  christos 					LBUMPD(ipf_stats[out], fr_ads);
   2588       1.1  christos 				} else {
   2589       1.1  christos 					LBUMPD(ipf_stats[out], fr_bads);
   2590       1.1  christos 					pass = passo;
   2591       1.1  christos 					continue;
   2592       1.1  christos 				}
   2593       1.1  christos 			}
   2594       1.1  christos 			break;
   2595       1.1  christos 		}
   2596       1.1  christos 	}
   2597       1.1  christos 	fin->fin_depth--;
   2598       1.1  christos 	return pass;
   2599       1.1  christos }
   2600       1.1  christos 
   2601       1.1  christos 
   2602       1.1  christos /* ------------------------------------------------------------------------ */
   2603       1.1  christos /* Function:    ipf_acctpkt                                                 */
   2604       1.1  christos /* Returns:     frentry_t* - always returns NULL                            */
   2605       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   2606       1.1  christos /*              passp(IO) - pointer to current/new filter decision (unused) */
   2607       1.1  christos /*                                                                          */
   2608       1.1  christos /* Checks a packet against accounting rules, if there are any for the given */
   2609       1.1  christos /* IP protocol version.                                                     */
   2610       1.1  christos /*                                                                          */
   2611       1.1  christos /* N.B.: this function returns NULL to match the prototype used by other    */
   2612       1.1  christos /* functions called from the IPFilter "mainline" in ipf_check().            */
   2613       1.1  christos /* ------------------------------------------------------------------------ */
   2614       1.1  christos frentry_t *
   2615       1.2  christos ipf_acctpkt(fr_info_t *fin, u_32_t *passp)
   2616       1.1  christos {
   2617       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2618       1.1  christos 	char group[FR_GROUPLEN];
   2619       1.1  christos 	frentry_t *fr, *frsave;
   2620       1.1  christos 	u_32_t pass, rulen;
   2621       1.1  christos 
   2622       1.1  christos 	passp = passp;
   2623       1.1  christos 	fr = softc->ipf_acct[fin->fin_out][softc->ipf_active];
   2624       1.1  christos 
   2625       1.1  christos 	if (fr != NULL) {
   2626       1.1  christos 		frsave = fin->fin_fr;
   2627       1.1  christos 		bcopy(fin->fin_group, group, FR_GROUPLEN);
   2628       1.1  christos 		rulen = fin->fin_rule;
   2629       1.1  christos 		fin->fin_fr = fr;
   2630       1.1  christos 		pass = ipf_scanlist(fin, FR_NOMATCH);
   2631       1.1  christos 		if (FR_ISACCOUNT(pass)) {
   2632       1.1  christos 			LBUMPD(ipf_stats[0], fr_acct);
   2633       1.1  christos 		}
   2634       1.1  christos 		fin->fin_fr = frsave;
   2635       1.1  christos 		bcopy(group, fin->fin_group, FR_GROUPLEN);
   2636       1.1  christos 		fin->fin_rule = rulen;
   2637       1.1  christos 	}
   2638       1.1  christos 	return NULL;
   2639       1.1  christos }
   2640       1.1  christos 
   2641       1.1  christos 
   2642       1.1  christos /* ------------------------------------------------------------------------ */
   2643       1.1  christos /* Function:    ipf_firewall                                                */
   2644       1.1  christos /* Returns:     frentry_t* - returns pointer to matched rule, if no matches */
   2645       1.1  christos /*                           were found, returns NULL.                      */
   2646       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   2647       1.1  christos /*              passp(IO) - pointer to current/new filter decision (unused) */
   2648       1.1  christos /*                                                                          */
   2649       1.1  christos /* Applies an appropriate set of firewall rules to the packet, to see if    */
   2650       1.1  christos /* there are any matches.  The first check is to see if a match can be seen */
   2651       1.1  christos /* in the cache.  If not, then search an appropriate list of rules.  Once a */
   2652       1.1  christos /* matching rule is found, take any appropriate actions as defined by the   */
   2653       1.1  christos /* rule - except logging.                                                   */
   2654       1.1  christos /* ------------------------------------------------------------------------ */
   2655       1.1  christos static frentry_t *
   2656       1.2  christos ipf_firewall(fr_info_t *fin, u_32_t *passp)
   2657       1.1  christos {
   2658       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   2659       1.1  christos 	frentry_t *fr;
   2660       1.1  christos 	u_32_t pass;
   2661       1.1  christos 	int out;
   2662       1.1  christos 
   2663       1.1  christos 	out = fin->fin_out;
   2664       1.1  christos 	pass = *passp;
   2665       1.1  christos 
   2666       1.1  christos 	/*
   2667       1.1  christos 	 * This rule cache will only affect packets that are not being
   2668       1.1  christos 	 * statefully filtered.
   2669       1.1  christos 	 */
   2670       1.1  christos 	fin->fin_fr = softc->ipf_rules[out][softc->ipf_active];
   2671       1.1  christos 	if (fin->fin_fr != NULL)
   2672       1.1  christos 		pass = ipf_scanlist(fin, softc->ipf_pass);
   2673       1.1  christos 
   2674       1.1  christos 	if ((pass & FR_NOMATCH)) {
   2675       1.1  christos 		LBUMPD(ipf_stats[out], fr_nom);
   2676       1.1  christos 	}
   2677       1.1  christos 	fr = fin->fin_fr;
   2678       1.1  christos 
   2679       1.1  christos 	/*
   2680       1.1  christos 	 * Apply packets per second rate-limiting to a rule as required.
   2681       1.1  christos 	 */
   2682       1.1  christos 	if ((fr != NULL) && (fr->fr_pps != 0) &&
   2683       1.1  christos 	    !ppsratecheck(&fr->fr_lastpkt, &fr->fr_curpps, fr->fr_pps)) {
   2684       1.1  christos 		DT2(frb_ppsrate, fr_info_t *, fin, frentry_t *, fr);
   2685       1.1  christos 		pass &= ~(FR_CMDMASK|FR_RETICMP|FR_RETRST);
   2686       1.1  christos 		pass |= FR_BLOCK;
   2687       1.1  christos 		LBUMPD(ipf_stats[out], fr_ppshit);
   2688       1.1  christos 		fin->fin_reason = FRB_PPSRATE;
   2689       1.1  christos 	}
   2690       1.1  christos 
   2691       1.1  christos 	/*
   2692       1.1  christos 	 * If we fail to add a packet to the authorization queue, then we
   2693       1.1  christos 	 * drop the packet later.  However, if it was added then pretend
   2694       1.1  christos 	 * we've dropped it already.
   2695       1.1  christos 	 */
   2696       1.1  christos 	if (FR_ISAUTH(pass)) {
   2697       1.1  christos 		if (ipf_auth_new(fin->fin_m, fin) != 0) {
   2698       1.1  christos 			DT1(frb_authnew, fr_info_t *, fin);
   2699       1.1  christos 			fin->fin_m = *fin->fin_mp = NULL;
   2700       1.1  christos 			fin->fin_reason = FRB_AUTHNEW;
   2701       1.1  christos 			fin->fin_error = 0;
   2702       1.1  christos 		} else {
   2703       1.1  christos 			IPFERROR(1);
   2704       1.1  christos 			fin->fin_error = ENOSPC;
   2705       1.1  christos 		}
   2706       1.1  christos 	}
   2707       1.1  christos 
   2708       1.1  christos 	if ((fr != NULL) && (fr->fr_func != NULL) &&
   2709       1.1  christos 	    (fr->fr_func != (ipfunc_t)-1) && !(pass & FR_CALLNOW))
   2710       1.1  christos 		(void) (*fr->fr_func)(fin, &pass);
   2711       1.1  christos 
   2712       1.1  christos 	/*
   2713       1.1  christos 	 * If a rule is a pre-auth rule, check again in the list of rules
   2714       1.1  christos 	 * loaded for authenticated use.  It does not particulary matter
   2715       1.1  christos 	 * if this search fails because a "preauth" result, from a rule,
   2716       1.1  christos 	 * is treated as "not a pass", hence the packet is blocked.
   2717       1.1  christos 	 */
   2718       1.1  christos 	if (FR_ISPREAUTH(pass)) {
   2719       1.1  christos 		pass = ipf_auth_pre_scanlist(softc, fin, pass);
   2720       1.1  christos 	}
   2721       1.1  christos 
   2722       1.1  christos 	/*
   2723       1.1  christos 	 * If the rule has "keep frag" and the packet is actually a fragment,
   2724       1.1  christos 	 * then create a fragment state entry.
   2725       1.1  christos 	 */
   2726  1.15.4.3     skrll 	if (pass & FR_KEEPFRAG) {
   2727       1.1  christos 		if (fin->fin_flx & FI_FRAG) {
   2728       1.1  christos 			if (ipf_frag_new(softc, fin, pass) == -1) {
   2729       1.1  christos 				LBUMP(ipf_stats[out].fr_bnfr);
   2730       1.1  christos 			} else {
   2731       1.1  christos 				LBUMP(ipf_stats[out].fr_nfr);
   2732       1.1  christos 			}
   2733       1.1  christos 		} else {
   2734       1.1  christos 			LBUMP(ipf_stats[out].fr_cfr);
   2735       1.1  christos 		}
   2736       1.1  christos 	}
   2737       1.1  christos 
   2738       1.1  christos 	fr = fin->fin_fr;
   2739       1.1  christos 	*passp = pass;
   2740       1.1  christos 
   2741       1.1  christos 	return fr;
   2742       1.1  christos }
   2743       1.1  christos 
   2744       1.1  christos 
   2745       1.1  christos /* ------------------------------------------------------------------------ */
   2746       1.1  christos /* Function:    ipf_check                                                   */
   2747       1.1  christos /* Returns:     int -  0 == packet allowed through,                         */
   2748       1.1  christos /*              User space:                                                 */
   2749       1.1  christos /*                    -1 == packet blocked                                  */
   2750       1.1  christos /*                     1 == packet not matched                              */
   2751       1.1  christos /*                    -2 == requires authentication                         */
   2752       1.1  christos /*              Kernel:                                                     */
   2753       1.1  christos /*                   > 0 == filter error # for packet                       */
   2754       1.1  christos /* Parameters: ip(I)   - pointer to start of IPv4/6 packet                  */
   2755       1.1  christos /*             hlen(I) - length of header                                   */
   2756       1.1  christos /*             ifp(I)  - pointer to interface this packet is on             */
   2757       1.1  christos /*             out(I)  - 0 == packet going in, 1 == packet going out        */
   2758       1.1  christos /*             mp(IO)  - pointer to caller's buffer pointer that holds this */
   2759       1.1  christos /*                       IP packet.                                         */
   2760       1.1  christos /* Solaris & HP-UX ONLY :                                                   */
   2761       1.1  christos /*             qpi(I)  - pointer to STREAMS queue information for this      */
   2762       1.1  christos /*                       interface & direction.                             */
   2763       1.1  christos /*                                                                          */
   2764       1.1  christos /* ipf_check() is the master function for all IPFilter packet processing.   */
   2765       1.1  christos /* It orchestrates: Network Address Translation (NAT), checking for packet  */
   2766       1.1  christos /* authorisation (or pre-authorisation), presence of related state info.,   */
   2767       1.1  christos /* generating log entries, IP packet accounting, routing of packets as      */
   2768       1.1  christos /* directed by firewall rules and of course whether or not to allow the     */
   2769       1.1  christos /* packet to be further processed by the kernel.                            */
   2770       1.1  christos /*                                                                          */
   2771       1.1  christos /* For packets blocked, the contents of "mp" will be NULL'd and the buffer  */
   2772       1.1  christos /* freed.  Packets passed may be returned with the pointer pointed to by    */
   2773       1.1  christos /* by "mp" changed to a new buffer.                                         */
   2774       1.1  christos /* ------------------------------------------------------------------------ */
   2775       1.1  christos int
   2776       1.2  christos ipf_check(void *ctx, ip_t *ip, int hlen, void *ifp, int out,
   2777       1.1  christos #if defined(_KERNEL) && defined(MENTAT)
   2778       1.2  christos     void *qif,
   2779       1.1  christos #endif
   2780       1.2  christos     mb_t **mp)
   2781       1.1  christos {
   2782       1.1  christos 	/*
   2783       1.1  christos 	 * The above really sucks, but short of writing a diff
   2784       1.1  christos 	 */
   2785       1.1  christos 	ipf_main_softc_t *softc = ctx;
   2786       1.1  christos 	fr_info_t frinfo;
   2787       1.1  christos 	fr_info_t *fin = &frinfo;
   2788       1.1  christos 	u_32_t pass = softc->ipf_pass;
   2789       1.1  christos 	frentry_t *fr = NULL;
   2790       1.1  christos 	int v = IP_V(ip);
   2791       1.1  christos 	mb_t *mc = NULL;
   2792       1.1  christos 	mb_t *m;
   2793       1.1  christos 	/*
   2794       1.1  christos 	 * The first part of ipf_check() deals with making sure that what goes
   2795       1.1  christos 	 * into the filtering engine makes some sense.  Information about the
   2796       1.1  christos 	 * the packet is distilled, collected into a fr_info_t structure and
   2797       1.1  christos 	 * the an attempt to ensure the buffer the packet is in is big enough
   2798       1.1  christos 	 * to hold all the required packet headers.
   2799       1.1  christos 	 */
   2800       1.1  christos #ifdef	_KERNEL
   2801       1.1  christos # ifdef MENTAT
   2802       1.1  christos 	qpktinfo_t *qpi = qif;
   2803       1.1  christos 
   2804       1.1  christos #  ifdef __sparc
   2805       1.1  christos 	if ((u_int)ip & 0x3)
   2806       1.1  christos 		return 2;
   2807       1.1  christos #  endif
   2808       1.1  christos # else
   2809       1.1  christos 	SPL_INT(s);
   2810       1.1  christos # endif
   2811       1.1  christos 
   2812       1.1  christos 	if (softc->ipf_running <= 0) {
   2813       1.1  christos 		return 0;
   2814       1.1  christos 	}
   2815       1.1  christos 
   2816       1.1  christos 	bzero((char *)fin, sizeof(*fin));
   2817       1.1  christos 
   2818       1.1  christos # ifdef MENTAT
   2819       1.3   darrenr 	if (qpi->qpi_flags & QF_BROADCAST)
   2820       1.3   darrenr 		fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
   2821       1.3   darrenr 	if (qpi->qpi_flags & QF_MULTICAST)
   2822       1.3   darrenr 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
   2823       1.1  christos 	m = qpi->qpi_m;
   2824       1.1  christos 	fin->fin_qfm = m;
   2825       1.1  christos 	fin->fin_qpi = qpi;
   2826       1.1  christos # else /* MENTAT */
   2827       1.1  christos 
   2828       1.1  christos 	m = *mp;
   2829       1.1  christos 
   2830       1.1  christos #  if defined(M_MCAST)
   2831       1.1  christos 	if ((m->m_flags & M_MCAST) != 0)
   2832       1.1  christos 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
   2833       1.1  christos #  endif
   2834       1.1  christos #  if defined(M_MLOOP)
   2835       1.1  christos 	if ((m->m_flags & M_MLOOP) != 0)
   2836       1.1  christos 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
   2837       1.1  christos #  endif
   2838       1.1  christos #  if defined(M_BCAST)
   2839       1.1  christos 	if ((m->m_flags & M_BCAST) != 0)
   2840       1.1  christos 		fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
   2841       1.1  christos #  endif
   2842       1.1  christos #  ifdef M_CANFASTFWD
   2843       1.1  christos 	/*
   2844       1.1  christos 	 * XXX For now, IP Filter and fast-forwarding of cached flows
   2845       1.1  christos 	 * XXX are mutually exclusive.  Eventually, IP Filter should
   2846       1.1  christos 	 * XXX get a "can-fast-forward" filter rule.
   2847       1.1  christos 	 */
   2848       1.1  christos 	m->m_flags &= ~M_CANFASTFWD;
   2849       1.1  christos #  endif /* M_CANFASTFWD */
   2850       1.1  christos #  if defined(CSUM_DELAY_DATA) && (!defined(__FreeBSD_version) || \
   2851       1.1  christos 				   (__FreeBSD_version < 501108))
   2852       1.1  christos 	/*
   2853       1.1  christos 	 * disable delayed checksums.
   2854       1.1  christos 	 */
   2855       1.1  christos 	if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
   2856       1.1  christos 		in_delayed_cksum(m);
   2857       1.1  christos 		m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA;
   2858       1.1  christos 	}
   2859       1.1  christos #  endif /* CSUM_DELAY_DATA */
   2860       1.1  christos # endif /* MENTAT */
   2861       1.1  christos #else
   2862       1.1  christos 	bzero((char *)fin, sizeof(*fin));
   2863       1.1  christos 	m = *mp;
   2864       1.1  christos # if defined(M_MCAST)
   2865       1.1  christos 	if ((m->m_flags & M_MCAST) != 0)
   2866       1.1  christos 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
   2867       1.1  christos # endif
   2868       1.1  christos # if defined(M_MLOOP)
   2869       1.1  christos 	if ((m->m_flags & M_MLOOP) != 0)
   2870       1.1  christos 		fin->fin_flx |= FI_MBCAST|FI_MULTICAST;
   2871       1.1  christos # endif
   2872       1.1  christos # if defined(M_BCAST)
   2873       1.1  christos 	if ((m->m_flags & M_BCAST) != 0)
   2874       1.1  christos 		fin->fin_flx |= FI_MBCAST|FI_BROADCAST;
   2875       1.1  christos # endif
   2876       1.1  christos #endif /* _KERNEL */
   2877       1.1  christos 
   2878       1.1  christos 	fin->fin_v = v;
   2879       1.1  christos 	fin->fin_m = m;
   2880       1.1  christos 	fin->fin_ip = ip;
   2881       1.1  christos 	fin->fin_mp = mp;
   2882       1.1  christos 	fin->fin_out = out;
   2883       1.1  christos 	fin->fin_ifp = ifp;
   2884       1.1  christos 	fin->fin_error = ENETUNREACH;
   2885       1.1  christos 	fin->fin_hlen = (u_short)hlen;
   2886       1.1  christos 	fin->fin_dp = (char *)ip + hlen;
   2887       1.1  christos 	fin->fin_main_soft = softc;
   2888       1.1  christos 
   2889       1.1  christos 	fin->fin_ipoff = (char *)ip - MTOD(m, char *);
   2890       1.1  christos 
   2891       1.1  christos 	SPL_NET(s);
   2892       1.1  christos 
   2893       1.1  christos #ifdef	USE_INET6
   2894       1.1  christos 	if (v == 6) {
   2895       1.1  christos 		LBUMP(ipf_stats[out].fr_ipv6);
   2896       1.1  christos 		/*
   2897       1.1  christos 		 * Jumbo grams are quite likely too big for internal buffer
   2898       1.1  christos 		 * structures to handle comfortably, for now, so just drop
   2899       1.1  christos 		 * them.
   2900       1.1  christos 		 */
   2901       1.1  christos 		if (((ip6_t *)ip)->ip6_plen == 0) {
   2902       1.1  christos 			DT1(frb_jumbo, ip6_t *, (ip6_t *)ip);
   2903       1.1  christos 			pass = FR_BLOCK|FR_NOMATCH;
   2904       1.1  christos 			fin->fin_reason = FRB_JUMBO;
   2905       1.1  christos 			goto finished;
   2906       1.1  christos 		}
   2907       1.1  christos 		fin->fin_family = AF_INET6;
   2908       1.1  christos 	} else
   2909       1.1  christos #endif
   2910       1.1  christos 	{
   2911       1.1  christos 		fin->fin_family = AF_INET;
   2912       1.1  christos 	}
   2913       1.1  christos 
   2914       1.1  christos 	if (ipf_makefrip(hlen, ip, fin) == -1) {
   2915       1.1  christos 		DT1(frb_makefrip, fr_info_t *, fin);
   2916       1.1  christos 		pass = FR_BLOCK|FR_NOMATCH;
   2917       1.1  christos 		fin->fin_reason = FRB_MAKEFRIP;
   2918       1.1  christos 		goto finished;
   2919       1.1  christos 	}
   2920       1.1  christos 
   2921       1.1  christos 	/*
   2922       1.1  christos 	 * For at least IPv6 packets, if a m_pullup() fails then this pointer
   2923       1.1  christos 	 * becomes NULL and so we have no packet to free.
   2924       1.1  christos 	 */
   2925       1.1  christos 	if (*fin->fin_mp == NULL)
   2926       1.1  christos 		goto finished;
   2927       1.1  christos 
   2928       1.1  christos 	if (!out) {
   2929       1.1  christos 		if (v == 4) {
   2930       1.1  christos 			if (softc->ipf_chksrc && !ipf_verifysrc(fin)) {
   2931       1.1  christos 				LBUMPD(ipf_stats[0], fr_v4_badsrc);
   2932       1.1  christos 				fin->fin_flx |= FI_BADSRC;
   2933       1.1  christos 			}
   2934       1.1  christos 			if (fin->fin_ip->ip_ttl < softc->ipf_minttl) {
   2935       1.1  christos 				LBUMPD(ipf_stats[0], fr_v4_badttl);
   2936       1.1  christos 				fin->fin_flx |= FI_LOWTTL;
   2937       1.1  christos 			}
   2938       1.1  christos 		}
   2939       1.1  christos #ifdef USE_INET6
   2940       1.1  christos 		else  if (v == 6) {
   2941       1.1  christos 			if (((ip6_t *)ip)->ip6_hlim < softc->ipf_minttl) {
   2942       1.1  christos 				LBUMPD(ipf_stats[0], fr_v6_badttl);
   2943       1.1  christos 				fin->fin_flx |= FI_LOWTTL;
   2944       1.1  christos 			}
   2945       1.1  christos 		}
   2946       1.1  christos #endif
   2947       1.1  christos 	}
   2948       1.1  christos 
   2949       1.1  christos 	if (fin->fin_flx & FI_SHORT) {
   2950       1.1  christos 		LBUMPD(ipf_stats[out], fr_short);
   2951       1.1  christos 	}
   2952       1.1  christos 
   2953       1.1  christos 	READ_ENTER(&softc->ipf_mutex);
   2954       1.1  christos 
   2955       1.1  christos 	if (!out) {
   2956       1.1  christos 		switch (fin->fin_v)
   2957       1.1  christos 		{
   2958       1.1  christos 		case 4 :
   2959       1.1  christos 			if (ipf_nat_checkin(fin, &pass) == -1) {
   2960       1.1  christos 				goto filterdone;
   2961       1.1  christos 			}
   2962       1.1  christos 			break;
   2963       1.1  christos #ifdef USE_INET6
   2964       1.1  christos 		case 6 :
   2965       1.1  christos 			if (ipf_nat6_checkin(fin, &pass) == -1) {
   2966       1.1  christos 				goto filterdone;
   2967       1.1  christos 			}
   2968       1.1  christos 			break;
   2969       1.1  christos #endif
   2970       1.1  christos 		default :
   2971       1.1  christos 			break;
   2972       1.1  christos 		}
   2973       1.1  christos 	}
   2974       1.1  christos 	/*
   2975       1.1  christos 	 * Check auth now.
   2976       1.1  christos 	 * If a packet is found in the auth table, then skip checking
   2977       1.1  christos 	 * the access lists for permission but we do need to consider
   2978       1.1  christos 	 * the result as if it were from the ACL's.  In addition, being
   2979       1.1  christos 	 * found in the auth table means it has been seen before, so do
   2980       1.1  christos 	 * not pass it through accounting (again), lest it be counted twice.
   2981       1.1  christos 	 */
   2982       1.1  christos 	fr = ipf_auth_check(fin, &pass);
   2983       1.1  christos 	if (!out && (fr == NULL))
   2984       1.1  christos 		(void) ipf_acctpkt(fin, NULL);
   2985       1.1  christos 
   2986       1.1  christos 	if (fr == NULL) {
   2987       1.1  christos 		if ((fin->fin_flx & FI_FRAG) != 0)
   2988       1.1  christos 			fr = ipf_frag_known(fin, &pass);
   2989       1.1  christos 
   2990       1.1  christos 		if (fr == NULL)
   2991       1.1  christos 			fr = ipf_state_check(fin, &pass);
   2992       1.1  christos 	}
   2993       1.1  christos 
   2994       1.1  christos 	if ((pass & FR_NOMATCH) || (fr == NULL))
   2995       1.1  christos 		fr = ipf_firewall(fin, &pass);
   2996       1.1  christos 
   2997       1.1  christos 	/*
   2998       1.1  christos 	 * If we've asked to track state for this packet, set it up.
   2999       1.1  christos 	 * Here rather than ipf_firewall because ipf_checkauth may decide
   3000       1.1  christos 	 * to return a packet for "keep state"
   3001       1.1  christos 	 */
   3002       1.1  christos 	if ((pass & FR_KEEPSTATE) && (fin->fin_m != NULL) &&
   3003       1.1  christos 	    !(fin->fin_flx & FI_STATE)) {
   3004       1.1  christos 		if (ipf_state_add(softc, fin, NULL, 0) == 0) {
   3005       1.1  christos 			LBUMP(ipf_stats[out].fr_ads);
   3006       1.1  christos 		} else {
   3007       1.1  christos 			LBUMP(ipf_stats[out].fr_bads);
   3008       1.1  christos 			if (FR_ISPASS(pass)) {
   3009       1.1  christos 				DT(frb_stateadd);
   3010       1.1  christos 				pass &= ~FR_CMDMASK;
   3011       1.1  christos 				pass |= FR_BLOCK;
   3012       1.1  christos 				fin->fin_reason = FRB_STATEADD;
   3013       1.1  christos 			}
   3014       1.1  christos 		}
   3015       1.1  christos 	}
   3016       1.1  christos 
   3017       1.1  christos 	fin->fin_fr = fr;
   3018       1.1  christos 	if ((fr != NULL) && !(fin->fin_flx & FI_STATE)) {
   3019       1.1  christos 		fin->fin_dif = &fr->fr_dif;
   3020       1.1  christos 		fin->fin_tif = &fr->fr_tifs[fin->fin_rev];
   3021       1.1  christos 	}
   3022       1.1  christos 
   3023       1.1  christos 	/*
   3024       1.1  christos 	 * Only count/translate packets which will be passed on, out the
   3025       1.1  christos 	 * interface.
   3026       1.1  christos 	 */
   3027       1.1  christos 	if (out && FR_ISPASS(pass)) {
   3028       1.1  christos 		(void) ipf_acctpkt(fin, NULL);
   3029       1.1  christos 
   3030       1.1  christos 		switch (fin->fin_v)
   3031       1.1  christos 		{
   3032       1.1  christos 		case 4 :
   3033       1.1  christos 			if (ipf_nat_checkout(fin, &pass) == -1) {
   3034       1.1  christos 				;
   3035       1.1  christos 			} else if ((softc->ipf_update_ipid != 0) && (v == 4)) {
   3036       1.1  christos 				if (ipf_updateipid(fin) == -1) {
   3037       1.1  christos 					DT(frb_updateipid);
   3038       1.1  christos 					LBUMP(ipf_stats[1].fr_ipud);
   3039       1.1  christos 					pass &= ~FR_CMDMASK;
   3040       1.1  christos 					pass |= FR_BLOCK;
   3041       1.1  christos 					fin->fin_reason = FRB_UPDATEIPID;
   3042       1.1  christos 				} else {
   3043       1.1  christos 					LBUMP(ipf_stats[0].fr_ipud);
   3044       1.1  christos 				}
   3045       1.1  christos 			}
   3046       1.1  christos 			break;
   3047       1.1  christos #ifdef USE_INET6
   3048       1.1  christos 		case 6 :
   3049       1.1  christos 			(void) ipf_nat6_checkout(fin, &pass);
   3050       1.1  christos 			break;
   3051       1.1  christos #endif
   3052       1.1  christos 		default :
   3053       1.1  christos 			break;
   3054       1.1  christos 		}
   3055       1.1  christos 	}
   3056       1.1  christos 
   3057       1.1  christos filterdone:
   3058       1.1  christos #ifdef	IPFILTER_LOG
   3059       1.1  christos 	if ((softc->ipf_flags & FF_LOGGING) || (pass & FR_LOGMASK)) {
   3060       1.1  christos 		(void) ipf_dolog(fin, &pass);
   3061       1.1  christos 	}
   3062       1.1  christos #endif
   3063       1.1  christos 
   3064       1.1  christos 	/*
   3065       1.1  christos 	 * The FI_STATE flag is cleared here so that calling ipf_state_check
   3066       1.1  christos 	 * will work when called from inside of fr_fastroute.  Although
   3067       1.1  christos 	 * there is a similar flag, FI_NATED, for NAT, it does have the same
   3068       1.1  christos 	 * impact on code execution.
   3069       1.1  christos 	 */
   3070       1.1  christos 	fin->fin_flx &= ~FI_STATE;
   3071       1.1  christos 
   3072       1.1  christos #if defined(FASTROUTE_RECURSION)
   3073       1.1  christos 	/*
   3074       1.1  christos 	 * Up the reference on fr_lock and exit ipf_mutex. The generation of
   3075       1.1  christos 	 * a packet below can sometimes cause a recursive call into IPFilter.
   3076       1.1  christos 	 * On those platforms where that does happen, we need to hang onto
   3077       1.1  christos 	 * the filter rule just in case someone decides to remove or flush it
   3078       1.1  christos 	 * in the meantime.
   3079       1.1  christos 	 */
   3080       1.1  christos 	if (fr != NULL) {
   3081       1.1  christos 		MUTEX_ENTER(&fr->fr_lock);
   3082       1.1  christos 		fr->fr_ref++;
   3083       1.1  christos 		MUTEX_EXIT(&fr->fr_lock);
   3084       1.1  christos 	}
   3085       1.1  christos 
   3086       1.1  christos 	RWLOCK_EXIT(&softc->ipf_mutex);
   3087       1.1  christos #endif
   3088       1.1  christos 
   3089       1.1  christos 	if ((pass & FR_RETMASK) != 0) {
   3090       1.1  christos 		/*
   3091       1.1  christos 		 * Should we return an ICMP packet to indicate error
   3092       1.1  christos 		 * status passing through the packet filter ?
   3093       1.1  christos 		 * WARNING: ICMP error packets AND TCP RST packets should
   3094       1.1  christos 		 * ONLY be sent in repsonse to incoming packets.  Sending
   3095       1.1  christos 		 * them in response to outbound packets can result in a
   3096       1.1  christos 		 * panic on some operating systems.
   3097       1.1  christos 		 */
   3098       1.1  christos 		if (!out) {
   3099       1.1  christos 			if (pass & FR_RETICMP) {
   3100       1.1  christos 				int dst;
   3101       1.1  christos 
   3102       1.1  christos 				if ((pass & FR_RETMASK) == FR_FAKEICMP)
   3103       1.1  christos 					dst = 1;
   3104       1.1  christos 				else
   3105       1.1  christos 					dst = 0;
   3106       1.1  christos 				(void) ipf_send_icmp_err(ICMP_UNREACH, fin,
   3107       1.1  christos 							 dst);
   3108       1.1  christos 				LBUMP(ipf_stats[0].fr_ret);
   3109       1.1  christos 			} else if (((pass & FR_RETMASK) == FR_RETRST) &&
   3110       1.1  christos 				   !(fin->fin_flx & FI_SHORT)) {
   3111       1.1  christos 				if (((fin->fin_flx & FI_OOW) != 0) ||
   3112       1.1  christos 				    (ipf_send_reset(fin) == 0)) {
   3113       1.1  christos 					LBUMP(ipf_stats[1].fr_ret);
   3114       1.1  christos 				}
   3115       1.1  christos 			}
   3116       1.1  christos 
   3117       1.1  christos 			/*
   3118       1.1  christos 			 * When using return-* with auth rules, the auth code
   3119       1.1  christos 			 * takes over disposing of this packet.
   3120       1.1  christos 			 */
   3121       1.1  christos 			if (FR_ISAUTH(pass) && (fin->fin_m != NULL)) {
   3122       1.1  christos 				DT1(frb_authcapture, fr_info_t *, fin);
   3123       1.1  christos 				fin->fin_m = *fin->fin_mp = NULL;
   3124       1.1  christos 				fin->fin_reason = FRB_AUTHCAPTURE;
   3125       1.1  christos 				m = NULL;
   3126       1.1  christos 			}
   3127       1.1  christos 		} else {
   3128       1.1  christos 			if (pass & FR_RETRST) {
   3129       1.1  christos 				fin->fin_error = ECONNRESET;
   3130       1.1  christos 			}
   3131       1.1  christos 		}
   3132       1.1  christos 	}
   3133       1.1  christos 
   3134       1.1  christos 	/*
   3135       1.1  christos 	 * After the above so that ICMP unreachables and TCP RSTs get
   3136       1.1  christos 	 * created properly.
   3137       1.1  christos 	 */
   3138       1.1  christos 	if (FR_ISBLOCK(pass) && (fin->fin_flx & FI_NEWNAT))
   3139       1.1  christos 		ipf_nat_uncreate(fin);
   3140       1.1  christos 
   3141       1.1  christos 	/*
   3142       1.1  christos 	 * If we didn't drop off the bottom of the list of rules (and thus
   3143       1.1  christos 	 * the 'current' rule fr is not NULL), then we may have some extra
   3144       1.1  christos 	 * instructions about what to do with a packet.
   3145       1.1  christos 	 * Once we're finished return to our caller, freeing the packet if
   3146       1.1  christos 	 * we are dropping it.
   3147       1.1  christos 	 */
   3148       1.1  christos 	if (fr != NULL) {
   3149       1.1  christos 		frdest_t *fdp;
   3150       1.1  christos 
   3151       1.1  christos 		/*
   3152       1.1  christos 		 * Generate a duplicated packet first because ipf_fastroute
   3153       1.1  christos 		 * can lead to fin_m being free'd... not good.
   3154       1.1  christos 		 */
   3155       1.1  christos 		fdp = fin->fin_dif;
   3156       1.1  christos 		if ((fdp != NULL) && (fdp->fd_ptr != NULL) &&
   3157      1.13  christos 		    (fdp->fd_ptr != (void *)-1) && (fin->fin_m != NULL)) {
   3158       1.1  christos 			mc = M_COPY(fin->fin_m);
   3159       1.1  christos 			if (mc != NULL)
   3160       1.1  christos 				ipf_fastroute(mc, &mc, fin, fdp);
   3161       1.1  christos 		}
   3162       1.1  christos 
   3163       1.1  christos 		fdp = fin->fin_tif;
   3164       1.1  christos 		if (!out && (pass & FR_FASTROUTE)) {
   3165       1.1  christos 			/*
   3166       1.1  christos 			 * For fastroute rule, no destination interface defined
   3167       1.1  christos 			 * so pass NULL as the frdest_t parameter
   3168       1.1  christos 			 */
   3169       1.1  christos 			(void) ipf_fastroute(fin->fin_m, mp, fin, NULL);
   3170       1.1  christos 			m = *mp = NULL;
   3171       1.1  christos 		} else if ((fdp != NULL) && (fdp->fd_ptr != NULL) &&
   3172       1.1  christos 			   (fdp->fd_ptr != (struct ifnet *)-1)) {
   3173       1.1  christos 			/* this is for to rules: */
   3174       1.1  christos 			ipf_fastroute(fin->fin_m, mp, fin, fdp);
   3175       1.1  christos 			m = *mp = NULL;
   3176       1.1  christos 		}
   3177       1.1  christos 
   3178       1.1  christos #if defined(FASTROUTE_RECURSION)
   3179       1.1  christos 		(void) ipf_derefrule(softc, &fr);
   3180       1.1  christos #endif
   3181       1.1  christos 	}
   3182       1.1  christos #if !defined(FASTROUTE_RECURSION)
   3183       1.1  christos 	RWLOCK_EXIT(&softc->ipf_mutex);
   3184       1.1  christos #endif
   3185       1.1  christos 
   3186       1.1  christos finished:
   3187       1.1  christos 	if (!FR_ISPASS(pass)) {
   3188       1.1  christos 		LBUMP(ipf_stats[out].fr_block);
   3189       1.1  christos 		if (*mp != NULL) {
   3190       1.2  christos #ifdef _KERNEL
   3191       1.1  christos 			FREE_MB_T(*mp);
   3192       1.2  christos #endif
   3193       1.1  christos 			m = *mp = NULL;
   3194       1.1  christos 		}
   3195       1.1  christos 	} else {
   3196       1.1  christos 		LBUMP(ipf_stats[out].fr_pass);
   3197       1.1  christos #if defined(_KERNEL) && defined(__sgi)
   3198       1.1  christos 		if ((fin->fin_hbuf != NULL) &&
   3199       1.1  christos 		    (mtod(fin->fin_m, struct ip *) != fin->fin_ip)) {
   3200       1.1  christos 			COPYBACK(fin->fin_m, 0, fin->fin_plen, fin->fin_hbuf);
   3201       1.1  christos 		}
   3202       1.1  christos #endif
   3203       1.1  christos 	}
   3204       1.1  christos 
   3205       1.1  christos 	SPL_X(s);
   3206       1.1  christos 
   3207       1.1  christos #ifdef _KERNEL
   3208       1.3   darrenr 	if (FR_ISPASS(pass))
   3209       1.3   darrenr 		return 0;
   3210       1.3   darrenr 	LBUMP(ipf_stats[out].fr_blocked[fin->fin_reason]);
   3211       1.3   darrenr 	return fin->fin_error;
   3212       1.1  christos #else /* _KERNEL */
   3213       1.1  christos 	if (*mp != NULL)
   3214       1.1  christos 		(*mp)->mb_ifp = fin->fin_ifp;
   3215       1.1  christos 	blockreason = fin->fin_reason;
   3216       1.1  christos 	FR_VERBOSE(("fin_flx %#x pass %#x ", fin->fin_flx, pass));
   3217       1.1  christos 	/*if ((pass & FR_CMDMASK) == (softc->ipf_pass & FR_CMDMASK))*/
   3218       1.1  christos 		if ((pass & FR_NOMATCH) != 0)
   3219       1.1  christos 			return 1;
   3220       1.1  christos 
   3221       1.1  christos 	if ((pass & FR_RETMASK) != 0)
   3222       1.1  christos 		switch (pass & FR_RETMASK)
   3223       1.1  christos 		{
   3224       1.1  christos 		case FR_RETRST :
   3225       1.1  christos 			return 3;
   3226       1.1  christos 		case FR_RETICMP :
   3227       1.1  christos 			return 4;
   3228       1.1  christos 		case FR_FAKEICMP :
   3229       1.1  christos 			return 5;
   3230       1.1  christos 		}
   3231       1.1  christos 
   3232       1.1  christos 	switch (pass & FR_CMDMASK)
   3233       1.1  christos 	{
   3234       1.1  christos 	case FR_PASS :
   3235       1.1  christos 		return 0;
   3236       1.1  christos 	case FR_BLOCK :
   3237       1.1  christos 		return -1;
   3238       1.1  christos 	case FR_AUTH :
   3239       1.1  christos 		return -2;
   3240       1.1  christos 	case FR_ACCOUNT :
   3241       1.1  christos 		return -3;
   3242       1.1  christos 	case FR_PREAUTH :
   3243       1.1  christos 		return -4;
   3244       1.1  christos 	}
   3245       1.1  christos 	return 2;
   3246       1.1  christos #endif /* _KERNEL */
   3247       1.1  christos }
   3248       1.1  christos 
   3249       1.1  christos 
   3250       1.1  christos #ifdef	IPFILTER_LOG
   3251       1.1  christos /* ------------------------------------------------------------------------ */
   3252       1.1  christos /* Function:    ipf_dolog                                                   */
   3253       1.1  christos /* Returns:     frentry_t* - returns contents of fin_fr (no change made)    */
   3254       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   3255       1.1  christos /*              passp(IO) - pointer to current/new filter decision (unused) */
   3256       1.1  christos /*                                                                          */
   3257       1.1  christos /* Checks flags set to see how a packet should be logged, if it is to be    */
   3258       1.1  christos /* logged.  Adjust statistics based on its success or not.                  */
   3259       1.1  christos /* ------------------------------------------------------------------------ */
   3260       1.1  christos frentry_t *
   3261       1.2  christos ipf_dolog(fr_info_t *fin, u_32_t *passp)
   3262       1.1  christos {
   3263       1.1  christos 	ipf_main_softc_t *softc = fin->fin_main_soft;
   3264       1.1  christos 	u_32_t pass;
   3265       1.1  christos 	int out;
   3266       1.1  christos 
   3267       1.1  christos 	out = fin->fin_out;
   3268       1.1  christos 	pass = *passp;
   3269       1.1  christos 
   3270       1.1  christos 	if ((softc->ipf_flags & FF_LOGNOMATCH) && (pass & FR_NOMATCH)) {
   3271       1.1  christos 		pass |= FF_LOGNOMATCH;
   3272       1.1  christos 		LBUMPD(ipf_stats[out], fr_npkl);
   3273       1.1  christos 		goto logit;
   3274       1.1  christos 
   3275       1.1  christos 	} else if (((pass & FR_LOGMASK) == FR_LOGP) ||
   3276       1.1  christos 	    (FR_ISPASS(pass) && (softc->ipf_flags & FF_LOGPASS))) {
   3277       1.1  christos 		if ((pass & FR_LOGMASK) != FR_LOGP)
   3278       1.1  christos 			pass |= FF_LOGPASS;
   3279       1.1  christos 		LBUMPD(ipf_stats[out], fr_ppkl);
   3280       1.1  christos 		goto logit;
   3281       1.1  christos 
   3282       1.1  christos 	} else if (((pass & FR_LOGMASK) == FR_LOGB) ||
   3283       1.1  christos 		   (FR_ISBLOCK(pass) && (softc->ipf_flags & FF_LOGBLOCK))) {
   3284       1.1  christos 		if ((pass & FR_LOGMASK) != FR_LOGB)
   3285       1.1  christos 			pass |= FF_LOGBLOCK;
   3286       1.1  christos 		LBUMPD(ipf_stats[out], fr_bpkl);
   3287       1.1  christos 
   3288       1.1  christos logit:
   3289       1.1  christos 		if (ipf_log_pkt(fin, pass) == -1) {
   3290       1.1  christos 			/*
   3291       1.1  christos 			 * If the "or-block" option has been used then
   3292       1.1  christos 			 * block the packet if we failed to log it.
   3293       1.1  christos 			 */
   3294       1.1  christos 			if ((pass & FR_LOGORBLOCK) && FR_ISPASS(pass)) {
   3295       1.1  christos 				DT1(frb_logfail2, u_int, pass);
   3296       1.1  christos 				pass &= ~FR_CMDMASK;
   3297       1.1  christos 				pass |= FR_BLOCK;
   3298       1.1  christos 				fin->fin_reason = FRB_LOGFAIL2;
   3299       1.1  christos 			}
   3300       1.1  christos 		}
   3301       1.1  christos 		*passp = pass;
   3302       1.1  christos 	}
   3303       1.1  christos 
   3304       1.1  christos 	return fin->fin_fr;
   3305       1.1  christos }
   3306       1.1  christos #endif /* IPFILTER_LOG */
   3307       1.1  christos 
   3308       1.1  christos 
   3309       1.1  christos /* ------------------------------------------------------------------------ */
   3310       1.1  christos /* Function:    ipf_cksum                                                   */
   3311       1.1  christos /* Returns:     u_short - IP header checksum                                */
   3312       1.1  christos /* Parameters:  addr(I) - pointer to start of buffer to checksum            */
   3313       1.1  christos /*              len(I)  - length of buffer in bytes                         */
   3314       1.1  christos /*                                                                          */
   3315       1.1  christos /* Calculate the two's complement 16 bit checksum of the buffer passed.     */
   3316       1.1  christos /*                                                                          */
   3317       1.1  christos /* N.B.: addr should be 16bit aligned.                                      */
   3318       1.1  christos /* ------------------------------------------------------------------------ */
   3319       1.1  christos u_short
   3320       1.2  christos ipf_cksum(u_short *addr, int len)
   3321       1.1  christos {
   3322       1.1  christos 	u_32_t sum = 0;
   3323       1.1  christos 
   3324       1.1  christos 	for (sum = 0; len > 1; len -= 2)
   3325       1.1  christos 		sum += *addr++;
   3326       1.1  christos 
   3327       1.1  christos 	/* mop up an odd byte, if necessary */
   3328       1.1  christos 	if (len == 1)
   3329       1.1  christos 		sum += *(u_char *)addr;
   3330       1.1  christos 
   3331       1.1  christos 	/*
   3332       1.1  christos 	 * add back carry outs from top 16 bits to low 16 bits
   3333       1.1  christos 	 */
   3334       1.1  christos 	sum = (sum >> 16) + (sum & 0xffff);	/* add hi 16 to low 16 */
   3335       1.1  christos 	sum += (sum >> 16);			/* add carry */
   3336       1.1  christos 	return (u_short)(~sum);
   3337       1.1  christos }
   3338       1.1  christos 
   3339       1.1  christos 
   3340       1.1  christos /* ------------------------------------------------------------------------ */
   3341       1.1  christos /* Function:    fr_cksum                                                    */
   3342       1.1  christos /* Returns:     u_short - layer 4 checksum                                  */
   3343       1.1  christos /* Parameters:  fin(I)     - pointer to packet information                  */
   3344       1.1  christos /*              ip(I)      - pointer to IP header                           */
   3345       1.1  christos /*              l4proto(I) - protocol to caclulate checksum for             */
   3346       1.1  christos /*              l4hdr(I)   - pointer to layer 4 header                      */
   3347       1.1  christos /*                                                                          */
   3348       1.1  christos /* Calculates the TCP checksum for the packet held in "m", using the data   */
   3349       1.1  christos /* in the IP header "ip" to seed it.                                        */
   3350       1.1  christos /*                                                                          */
   3351       1.1  christos /* NB: This function assumes we've pullup'd enough for all of the IP header */
   3352       1.1  christos /* and the TCP header.  We also assume that data blocks aren't allocated in */
   3353       1.1  christos /* odd sizes.                                                               */
   3354       1.1  christos /*                                                                          */
   3355       1.1  christos /* Expects ip_len and ip_off to be in network byte order when called.       */
   3356       1.1  christos /* ------------------------------------------------------------------------ */
   3357       1.1  christos u_short
   3358       1.2  christos fr_cksum(fr_info_t *fin, ip_t *ip, int l4proto, void *l4hdr)
   3359       1.1  christos {
   3360       1.1  christos 	u_short *sp, slen, sumsave, *csump;
   3361       1.1  christos 	u_int sum, sum2;
   3362       1.1  christos 	int hlen;
   3363       1.1  christos 	int off;
   3364       1.1  christos #ifdef	USE_INET6
   3365       1.1  christos 	ip6_t *ip6;
   3366       1.1  christos #endif
   3367       1.1  christos 
   3368       1.1  christos 	csump = NULL;
   3369       1.1  christos 	sumsave = 0;
   3370       1.1  christos 	sp = NULL;
   3371       1.1  christos 	slen = 0;
   3372       1.1  christos 	hlen = 0;
   3373       1.1  christos 	sum = 0;
   3374       1.1  christos 
   3375       1.1  christos 	sum = htons((u_short)l4proto);
   3376       1.1  christos 	/*
   3377       1.1  christos 	 * Add up IP Header portion
   3378       1.1  christos 	 */
   3379       1.1  christos #ifdef	USE_INET6
   3380       1.1  christos 	if (IP_V(ip) == 4) {
   3381       1.1  christos #endif
   3382       1.1  christos 		hlen = IP_HL(ip) << 2;
   3383       1.1  christos 		off = hlen;
   3384       1.1  christos 		sp = (u_short *)&ip->ip_src;
   3385       1.1  christos 		sum += *sp++;	/* ip_src */
   3386       1.1  christos 		sum += *sp++;
   3387       1.1  christos 		sum += *sp++;	/* ip_dst */
   3388       1.1  christos 		sum += *sp++;
   3389       1.1  christos #ifdef	USE_INET6
   3390       1.1  christos 	} else if (IP_V(ip) == 6) {
   3391       1.1  christos 		ip6 = (ip6_t *)ip;
   3392       1.1  christos 		hlen = sizeof(*ip6);
   3393       1.1  christos 		off = ((char *)fin->fin_dp - (char *)fin->fin_ip);
   3394       1.1  christos 		sp = (u_short *)&ip6->ip6_src;
   3395       1.1  christos 		sum += *sp++;	/* ip6_src */
   3396       1.1  christos 		sum += *sp++;
   3397       1.1  christos 		sum += *sp++;
   3398       1.1  christos 		sum += *sp++;
   3399       1.1  christos 		sum += *sp++;
   3400       1.1  christos 		sum += *sp++;
   3401       1.1  christos 		sum += *sp++;
   3402       1.1  christos 		sum += *sp++;
   3403       1.3   darrenr 		/* This needs to be routing header aware. */
   3404       1.1  christos 		sum += *sp++;	/* ip6_dst */
   3405       1.1  christos 		sum += *sp++;
   3406       1.1  christos 		sum += *sp++;
   3407       1.1  christos 		sum += *sp++;
   3408       1.1  christos 		sum += *sp++;
   3409       1.1  christos 		sum += *sp++;
   3410       1.1  christos 		sum += *sp++;
   3411       1.1  christos 		sum += *sp++;
   3412       1.1  christos 	} else {
   3413       1.1  christos 		return 0xffff;
   3414       1.1  christos 	}
   3415       1.1  christos #endif
   3416       1.1  christos 	slen = fin->fin_plen - off;
   3417       1.1  christos 	sum += htons(slen);
   3418       1.1  christos 
   3419       1.1  christos 	switch (l4proto)
   3420       1.1  christos 	{
   3421       1.1  christos 	case IPPROTO_UDP :
   3422       1.1  christos 		csump = &((udphdr_t *)l4hdr)->uh_sum;
   3423       1.1  christos 		break;
   3424       1.1  christos 
   3425       1.1  christos 	case IPPROTO_TCP :
   3426       1.1  christos 		csump = &((tcphdr_t *)l4hdr)->th_sum;
   3427       1.1  christos 		break;
   3428       1.1  christos 	case IPPROTO_ICMP :
   3429       1.1  christos 		csump = &((icmphdr_t *)l4hdr)->icmp_cksum;
   3430       1.1  christos 		sum = 0;	/* Pseudo-checksum is not included */
   3431       1.1  christos 		break;
   3432       1.3   darrenr #ifdef USE_INET6
   3433       1.3   darrenr 	case IPPROTO_ICMPV6 :
   3434       1.3   darrenr 		csump = &((struct icmp6_hdr *)l4hdr)->icmp6_cksum;
   3435       1.3   darrenr 		break;
   3436       1.3   darrenr #endif
   3437       1.1  christos 	default :
   3438       1.1  christos 		break;
   3439       1.1  christos 	}
   3440       1.1  christos 
   3441       1.1  christos 	if (csump != NULL) {
   3442       1.1  christos 		sumsave = *csump;
   3443       1.1  christos 		*csump = 0;
   3444       1.1  christos 	}
   3445       1.1  christos 
   3446       1.1  christos 	sum2 = ipf_pcksum(fin, off, sum);
   3447       1.1  christos 	if (csump != NULL)
   3448       1.1  christos 		*csump = sumsave;
   3449       1.1  christos 	return sum2;
   3450       1.1  christos }
   3451       1.1  christos 
   3452       1.1  christos 
   3453       1.1  christos /* ------------------------------------------------------------------------ */
   3454       1.1  christos /* Function:    ipf_findgroup                                               */
   3455       1.1  christos /* Returns:     frgroup_t * - NULL = group not found, else pointer to group */
   3456       1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   3457       1.3   darrenr /*              group(I) - group name to search for                         */
   3458       1.1  christos /*              unit(I)  - device to which this group belongs               */
   3459       1.1  christos /*              set(I)   - which set of rules (inactive/inactive) this is   */
   3460       1.1  christos /*              fgpp(O)  - pointer to place to store pointer to the pointer */
   3461       1.1  christos /*                         to where to add the next (last) group or where   */
   3462       1.1  christos /*                         to delete group from.                            */
   3463       1.1  christos /*                                                                          */
   3464       1.1  christos /* Search amongst the defined groups for a particular group number.         */
   3465       1.1  christos /* ------------------------------------------------------------------------ */
   3466       1.1  christos frgroup_t *
   3467       1.2  christos ipf_findgroup(ipf_main_softc_t *softc, char *group, minor_t unit, int set,
   3468       1.2  christos     frgroup_t ***fgpp)
   3469       1.1  christos {
   3470       1.1  christos 	frgroup_t *fg, **fgp;
   3471       1.1  christos 
   3472       1.1  christos 	/*
   3473       1.1  christos 	 * Which list of groups to search in is dependent on which list of
   3474       1.1  christos 	 * rules are being operated on.
   3475       1.1  christos 	 */
   3476       1.1  christos 	fgp = &softc->ipf_groups[unit][set];
   3477       1.1  christos 
   3478       1.1  christos 	while ((fg = *fgp) != NULL) {
   3479       1.1  christos 		if (strncmp(group, fg->fg_name, FR_GROUPLEN) == 0)
   3480       1.1  christos 			break;
   3481       1.1  christos 		else
   3482       1.1  christos 			fgp = &fg->fg_next;
   3483       1.1  christos 	}
   3484       1.1  christos 	if (fgpp != NULL)
   3485       1.1  christos 		*fgpp = fgp;
   3486       1.1  christos 	return fg;
   3487       1.1  christos }
   3488       1.1  christos 
   3489       1.1  christos 
   3490       1.1  christos /* ------------------------------------------------------------------------ */
   3491       1.1  christos /* Function:    ipf_group_add                                               */
   3492       1.1  christos /* Returns:     frgroup_t * - NULL == did not create group,                 */
   3493       1.1  christos /*                            != NULL == pointer to the group               */
   3494       1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   3495       1.3   darrenr /*              num(I)   - group number to add                              */
   3496       1.1  christos /*              head(I)  - rule pointer that is using this as the head      */
   3497       1.1  christos /*              flags(I) - rule flags which describe the type of rule it is */
   3498       1.1  christos /*              unit(I)  - device to which this group will belong to        */
   3499       1.1  christos /*              set(I)   - which set of rules (inactive/inactive) this is   */
   3500       1.1  christos /* Write Locks: ipf_mutex                                                   */
   3501       1.1  christos /*                                                                          */
   3502       1.1  christos /* Add a new group head, or if it already exists, increase the reference    */
   3503       1.1  christos /* count to it.                                                             */
   3504       1.1  christos /* ------------------------------------------------------------------------ */
   3505       1.1  christos frgroup_t *
   3506       1.2  christos ipf_group_add(ipf_main_softc_t *softc, char *group, void *head, u_32_t flags,
   3507       1.2  christos     minor_t unit, int set)
   3508       1.1  christos {
   3509       1.1  christos 	frgroup_t *fg, **fgp;
   3510       1.1  christos 	u_32_t gflags;
   3511       1.1  christos 
   3512       1.1  christos 	if (group == NULL)
   3513       1.1  christos 		return NULL;
   3514       1.1  christos 
   3515       1.1  christos 	if (unit == IPL_LOGIPF && *group == '\0')
   3516       1.1  christos 		return NULL;
   3517       1.1  christos 
   3518       1.1  christos 	fgp = NULL;
   3519       1.1  christos 	gflags = flags & FR_INOUT;
   3520       1.1  christos 
   3521       1.1  christos 	fg = ipf_findgroup(softc, group, unit, set, &fgp);
   3522       1.1  christos 	if (fg != NULL) {
   3523       1.3   darrenr 		if (fg->fg_head == NULL && head != NULL)
   3524       1.3   darrenr 			fg->fg_head = head;
   3525       1.1  christos 		if (fg->fg_flags == 0)
   3526       1.1  christos 			fg->fg_flags = gflags;
   3527       1.1  christos 		else if (gflags != fg->fg_flags)
   3528       1.1  christos 			return NULL;
   3529       1.1  christos 		fg->fg_ref++;
   3530       1.1  christos 		return fg;
   3531       1.1  christos 	}
   3532       1.1  christos 
   3533       1.1  christos 	KMALLOC(fg, frgroup_t *);
   3534       1.1  christos 	if (fg != NULL) {
   3535       1.1  christos 		fg->fg_head = head;
   3536       1.1  christos 		fg->fg_start = NULL;
   3537       1.1  christos 		fg->fg_next = *fgp;
   3538       1.1  christos 		bcopy(group, fg->fg_name, strlen(group) + 1);
   3539       1.1  christos 		fg->fg_flags = gflags;
   3540       1.1  christos 		fg->fg_ref = 1;
   3541       1.3   darrenr 		fg->fg_set = &softc->ipf_groups[unit][set];
   3542       1.1  christos 		*fgp = fg;
   3543       1.1  christos 	}
   3544       1.1  christos 	return fg;
   3545       1.1  christos }
   3546       1.1  christos 
   3547       1.1  christos 
   3548       1.1  christos /* ------------------------------------------------------------------------ */
   3549       1.1  christos /* Function:    ipf_group_del                                               */
   3550       1.1  christos /* Returns:     int      - number of rules deleted                          */
   3551       1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   3552       1.3   darrenr /*              group(I) - group name to delete                             */
   3553       1.3   darrenr /*              fr(I)    - filter rule from which group is referenced       */
   3554       1.1  christos /* Write Locks: ipf_mutex                                                   */
   3555       1.1  christos /*                                                                          */
   3556       1.3   darrenr /* This function is called whenever a reference to a group is to be dropped */
   3557       1.3   darrenr /* and thus its reference count needs to be lowered and the group free'd if */
   3558       1.3   darrenr /* the reference count reaches zero. Passing in fr is really for the sole   */
   3559       1.3   darrenr /* purpose of knowing when the head rule is being deleted.                  */
   3560       1.1  christos /* ------------------------------------------------------------------------ */
   3561       1.3   darrenr void
   3562       1.3   darrenr ipf_group_del(ipf_main_softc_t *softc, frgroup_t *group, frentry_t *fr)
   3563       1.1  christos {
   3564       1.1  christos 
   3565       1.3   darrenr 	if (group->fg_head == fr)
   3566       1.3   darrenr 		group->fg_head = NULL;
   3567       1.3   darrenr 
   3568       1.3   darrenr 	group->fg_ref--;
   3569       1.3   darrenr 	if ((group->fg_ref == 0) && (group->fg_start == NULL))
   3570       1.3   darrenr 		ipf_group_free(group);
   3571       1.3   darrenr }
   3572       1.1  christos 
   3573       1.1  christos 
   3574       1.3   darrenr /* ------------------------------------------------------------------------ */
   3575       1.3   darrenr /* Function:    ipf_group_free                                              */
   3576       1.3   darrenr /* Returns:     Nil                                                         */
   3577       1.3   darrenr /* Parameters:  group(I) - pointer to filter rule group                     */
   3578       1.3   darrenr /*                                                                          */
   3579       1.3   darrenr /* Remove the group from the list of groups and free it.                    */
   3580       1.3   darrenr /* ------------------------------------------------------------------------ */
   3581       1.3   darrenr static void
   3582       1.3   darrenr ipf_group_free(frgroup_t *group)
   3583       1.3   darrenr {
   3584       1.3   darrenr 	frgroup_t **gp;
   3585       1.3   darrenr 
   3586       1.3   darrenr 	for (gp = group->fg_set; *gp != NULL; gp = &(*gp)->fg_next) {
   3587       1.3   darrenr 		if (*gp == group) {
   3588       1.3   darrenr 			*gp = group->fg_next;
   3589       1.3   darrenr 			break;
   3590       1.3   darrenr 		}
   3591       1.1  christos 	}
   3592       1.3   darrenr 	KFREE(group);
   3593       1.3   darrenr }
   3594       1.3   darrenr 
   3595       1.3   darrenr 
   3596       1.3   darrenr /* ------------------------------------------------------------------------ */
   3597       1.3   darrenr /* Function:    ipf_group_flush                                             */
   3598       1.3   darrenr /* Returns:     int      - number of rules flush from group                 */
   3599       1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   3600       1.3   darrenr /* Parameters:  group(I) - pointer to filter rule group                     */
   3601       1.3   darrenr /*                                                                          */
   3602       1.3   darrenr /* Remove all of the rules that currently are listed under the given group. */
   3603       1.3   darrenr /* ------------------------------------------------------------------------ */
   3604       1.3   darrenr static int
   3605       1.3   darrenr ipf_group_flush(ipf_main_softc_t *softc, frgroup_t *group)
   3606       1.3   darrenr {
   3607       1.3   darrenr 	int gone = 0;
   3608       1.3   darrenr 
   3609       1.3   darrenr 	(void) ipf_flushlist(softc, &gone, &group->fg_start);
   3610       1.1  christos 
   3611       1.1  christos 	return gone;
   3612       1.1  christos }
   3613       1.1  christos 
   3614       1.1  christos 
   3615       1.1  christos /* ------------------------------------------------------------------------ */
   3616       1.1  christos /* Function:    ipf_getrulen                                                */
   3617       1.1  christos /* Returns:     frentry_t * - NULL == not found, else pointer to rule n     */
   3618       1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   3619       1.1  christos /* Parameters:  unit(I)  - device for which to count the rule's number      */
   3620       1.1  christos /*              flags(I) - which set of rules to find the rule in           */
   3621       1.1  christos /*              group(I) - group name                                       */
   3622       1.1  christos /*              n(I)     - rule number to find                              */
   3623       1.1  christos /*                                                                          */
   3624       1.1  christos /* Find rule # n in group # g and return a pointer to it.  Return NULl if   */
   3625       1.1  christos /* group # g doesn't exist or there are less than n rules in the group.     */
   3626       1.1  christos /* ------------------------------------------------------------------------ */
   3627       1.1  christos frentry_t *
   3628       1.2  christos ipf_getrulen(ipf_main_softc_t *softc, int unit, char *group, u_32_t n)
   3629       1.1  christos {
   3630       1.1  christos 	frentry_t *fr;
   3631       1.1  christos 	frgroup_t *fg;
   3632       1.1  christos 
   3633       1.1  christos 	fg = ipf_findgroup(softc, group, unit, softc->ipf_active, NULL);
   3634       1.1  christos 	if (fg == NULL)
   3635       1.1  christos 		return NULL;
   3636       1.1  christos 	for (fr = fg->fg_start; fr && n; fr = fr->fr_next, n--)
   3637       1.1  christos 		;
   3638       1.1  christos 	if (n != 0)
   3639       1.1  christos 		return NULL;
   3640       1.1  christos 	return fr;
   3641       1.1  christos }
   3642       1.1  christos 
   3643       1.1  christos 
   3644       1.1  christos /* ------------------------------------------------------------------------ */
   3645       1.1  christos /* Function:    ipf_flushlist                                               */
   3646       1.1  christos /* Returns:     int - >= 0 - number of flushed rules                        */
   3647       1.3   darrenr /* Parameters:  softc(I)   - pointer to soft context main structure         */
   3648       1.1  christos /*              nfreedp(O) - pointer to int where flush count is stored     */
   3649       1.1  christos /*              listp(I)   - pointer to list to flush pointer               */
   3650       1.1  christos /* Write Locks: ipf_mutex                                                   */
   3651       1.1  christos /*                                                                          */
   3652       1.1  christos /* Recursively flush rules from the list, descending groups as they are     */
   3653       1.1  christos /* encountered.  if a rule is the head of a group and it has lost all its   */
   3654       1.1  christos /* group members, then also delete the group reference.  nfreedp is needed  */
   3655       1.1  christos /* to store the accumulating count of rules removed, whereas the returned   */
   3656       1.1  christos /* value is just the number removed from the current list.  The latter is   */
   3657       1.1  christos /* needed to correctly adjust reference counts on rules that define groups. */
   3658       1.1  christos /*                                                                          */
   3659       1.1  christos /* NOTE: Rules not loaded from user space cannot be flushed.                */
   3660       1.1  christos /* ------------------------------------------------------------------------ */
   3661       1.1  christos static int
   3662       1.3   darrenr ipf_flushlist(ipf_main_softc_t *softc, int *nfreedp, frentry_t **listp)
   3663       1.1  christos {
   3664       1.1  christos 	int freed = 0;
   3665       1.1  christos 	frentry_t *fp;
   3666       1.1  christos 
   3667       1.1  christos 	while ((fp = *listp) != NULL) {
   3668       1.1  christos 		if ((fp->fr_type & FR_T_BUILTIN) ||
   3669       1.1  christos 		    !(fp->fr_flags & FR_COPIED)) {
   3670       1.1  christos 			listp = &fp->fr_next;
   3671       1.1  christos 			continue;
   3672       1.1  christos 		}
   3673       1.1  christos 		*listp = fp->fr_next;
   3674       1.1  christos 		if (fp->fr_next != NULL)
   3675       1.1  christos 			fp->fr_next->fr_pnext = fp->fr_pnext;
   3676       1.1  christos 		fp->fr_pnext = NULL;
   3677       1.1  christos 
   3678       1.3   darrenr 		if (fp->fr_grphead != NULL) {
   3679       1.3   darrenr 			freed += ipf_group_flush(softc, fp->fr_grphead);
   3680       1.1  christos 			fp->fr_names[fp->fr_grhead] = '\0';
   3681       1.1  christos 		}
   3682       1.1  christos 
   3683       1.3   darrenr 		if (fp->fr_icmpgrp != NULL) {
   3684       1.3   darrenr 			freed += ipf_group_flush(softc, fp->fr_icmpgrp);
   3685       1.1  christos 			fp->fr_names[fp->fr_icmphead] = '\0';
   3686       1.1  christos 		}
   3687       1.1  christos 
   3688       1.1  christos 		if (fp->fr_srctrack.ht_max_nodes)
   3689       1.1  christos 			ipf_rb_ht_flush(&fp->fr_srctrack);
   3690       1.1  christos 
   3691       1.1  christos 		fp->fr_next = NULL;
   3692       1.1  christos 
   3693       1.1  christos 		ASSERT(fp->fr_ref > 0);
   3694       1.1  christos 		if (ipf_derefrule(softc, &fp) == 0)
   3695       1.1  christos 			freed++;
   3696       1.1  christos 	}
   3697       1.1  christos 	*nfreedp += freed;
   3698       1.1  christos 	return freed;
   3699       1.1  christos }
   3700       1.1  christos 
   3701       1.1  christos 
   3702       1.1  christos /* ------------------------------------------------------------------------ */
   3703       1.1  christos /* Function:    ipf_flush                                                   */
   3704       1.1  christos /* Returns:     int - >= 0 - number of flushed rules                        */
   3705       1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   3706       1.3   darrenr /*              unit(I)  - device for which to flush rules                  */
   3707       1.1  christos /*              flags(I) - which set of rules to flush                      */
   3708       1.1  christos /*                                                                          */
   3709       1.1  christos /* Calls flushlist() for all filter rules (accounting, firewall - both IPv4 */
   3710       1.1  christos /* and IPv6) as defined by the value of flags.                              */
   3711       1.1  christos /* ------------------------------------------------------------------------ */
   3712       1.1  christos int
   3713       1.2  christos ipf_flush(ipf_main_softc_t *softc, minor_t unit, int flags)
   3714       1.1  christos {
   3715       1.1  christos 	int flushed = 0, set;
   3716       1.1  christos 
   3717       1.1  christos 	WRITE_ENTER(&softc->ipf_mutex);
   3718       1.1  christos 
   3719       1.1  christos 	set = softc->ipf_active;
   3720       1.1  christos 	if ((flags & FR_INACTIVE) == FR_INACTIVE)
   3721       1.1  christos 		set = 1 - set;
   3722       1.1  christos 
   3723       1.1  christos 	if (flags & FR_OUTQUE) {
   3724       1.3   darrenr 		ipf_flushlist(softc, &flushed, &softc->ipf_rules[1][set]);
   3725       1.3   darrenr 		ipf_flushlist(softc, &flushed, &softc->ipf_acct[1][set]);
   3726       1.1  christos 	}
   3727       1.1  christos 	if (flags & FR_INQUE) {
   3728       1.3   darrenr 		ipf_flushlist(softc, &flushed, &softc->ipf_rules[0][set]);
   3729       1.3   darrenr 		ipf_flushlist(softc, &flushed, &softc->ipf_acct[0][set]);
   3730       1.1  christos 	}
   3731       1.1  christos 
   3732       1.3   darrenr 	flushed += ipf_flush_groups(softc, &softc->ipf_groups[unit][set],
   3733       1.1  christos 				    flags & (FR_INQUE|FR_OUTQUE));
   3734       1.1  christos 
   3735       1.1  christos 	RWLOCK_EXIT(&softc->ipf_mutex);
   3736       1.1  christos 
   3737       1.1  christos 	if (unit == IPL_LOGIPF) {
   3738       1.1  christos 		int tmp;
   3739       1.1  christos 
   3740       1.1  christos 		tmp = ipf_flush(softc, IPL_LOGCOUNT, flags);
   3741       1.1  christos 		if (tmp >= 0)
   3742       1.1  christos 			flushed += tmp;
   3743       1.1  christos 	}
   3744       1.1  christos 	return flushed;
   3745       1.1  christos }
   3746       1.1  christos 
   3747       1.1  christos 
   3748       1.1  christos /* ------------------------------------------------------------------------ */
   3749       1.1  christos /* Function:    ipf_flush_groups                                            */
   3750       1.1  christos /* Returns:     int - >= 0 - number of flushed rules                        */
   3751       1.3   darrenr /* Parameters:  softc(I)  - soft context pointerto work with                */
   3752       1.3   darrenr /*              grhead(I) - pointer to the start of the group list to flush */
   3753       1.3   darrenr /*              flags(I)  - which set of rules to flush                     */
   3754       1.3   darrenr /*                                                                          */
   3755       1.3   darrenr /* Walk through all of the groups under the given group head and remove all */
   3756       1.3   darrenr /* of those that match the flags passed in. The for loop here is bit more   */
   3757       1.3   darrenr /* complicated than usual because the removal of a rule with ipf_derefrule  */
   3758       1.3   darrenr /* may end up removing not only the structure pointed to by "fg" but also   */
   3759       1.3   darrenr /* what is fg_next and fg_next after that. So if a filter rule is actually  */
   3760       1.3   darrenr /* removed from the group then it is necessary to start again.              */
   3761       1.1  christos /* ------------------------------------------------------------------------ */
   3762       1.1  christos static int
   3763       1.3   darrenr ipf_flush_groups( ipf_main_softc_t *softc, frgroup_t **grhead, int flags)
   3764       1.1  christos {
   3765       1.3   darrenr 	frentry_t *fr, **frp;
   3766       1.1  christos 	frgroup_t *fg, **fgp;
   3767       1.1  christos 	int flushed = 0;
   3768       1.3   darrenr 	int removed = 0;
   3769       1.1  christos 
   3770       1.3   darrenr 	for (fgp = grhead; (fg = *fgp) != NULL; ) {
   3771       1.3   darrenr 		while ((fg != NULL) && ((fg->fg_flags & flags) == 0))
   3772       1.3   darrenr 			fg = fg->fg_next;
   3773       1.3   darrenr 		if (fg == NULL)
   3774       1.3   darrenr 			break;
   3775       1.3   darrenr 		removed = 0;
   3776       1.1  christos 		frp = &fg->fg_start;
   3777       1.3   darrenr 		while ((removed == 0) && ((fr = *frp) != NULL)) {
   3778       1.1  christos 			if ((fr->fr_flags & flags) == 0) {
   3779       1.1  christos 				frp = &fr->fr_next;
   3780       1.3   darrenr 			} else {
   3781       1.3   darrenr 				if (fr->fr_next != NULL)
   3782       1.3   darrenr 					fr->fr_next->fr_pnext = fr->fr_pnext;
   3783       1.3   darrenr 				*frp = fr->fr_next;
   3784       1.3   darrenr 				fr->fr_pnext = NULL;
   3785       1.3   darrenr 				fr->fr_next = NULL;
   3786       1.3   darrenr 				(void) ipf_derefrule(softc, &fr);
   3787       1.3   darrenr 				flushed++;
   3788       1.3   darrenr 				removed++;
   3789       1.1  christos 			}
   3790       1.1  christos 		}
   3791       1.3   darrenr 		if (removed == 0)
   3792       1.3   darrenr 			fgp = &fg->fg_next;
   3793       1.1  christos 	}
   3794       1.1  christos 	return flushed;
   3795       1.1  christos }
   3796       1.1  christos 
   3797       1.1  christos 
   3798       1.1  christos /* ------------------------------------------------------------------------ */
   3799       1.1  christos /* Function:    memstr                                                      */
   3800       1.1  christos /* Returns:     char *  - NULL if failed, != NULL pointer to matching bytes */
   3801       1.1  christos /* Parameters:  src(I)  - pointer to byte sequence to match                 */
   3802       1.1  christos /*              dst(I)  - pointer to byte sequence to search                */
   3803       1.1  christos /*              slen(I) - match length                                      */
   3804       1.1  christos /*              dlen(I) - length available to search in                     */
   3805       1.1  christos /*                                                                          */
   3806       1.1  christos /* Search dst for a sequence of bytes matching those at src and extend for  */
   3807       1.1  christos /* slen bytes.                                                              */
   3808       1.1  christos /* ------------------------------------------------------------------------ */
   3809       1.1  christos char *
   3810       1.2  christos memstr(const char *src, char *dst, size_t slen, size_t dlen)
   3811       1.1  christos {
   3812       1.1  christos 	char *s = NULL;
   3813       1.1  christos 
   3814       1.1  christos 	while (dlen >= slen) {
   3815       1.2  christos 		if (memcmp(src, dst, slen) == 0) {
   3816       1.1  christos 			s = dst;
   3817       1.1  christos 			break;
   3818       1.1  christos 		}
   3819       1.1  christos 		dst++;
   3820       1.1  christos 		dlen--;
   3821       1.1  christos 	}
   3822       1.1  christos 	return s;
   3823       1.1  christos }
   3824      1.15  christos 
   3825      1.15  christos 
   3826       1.1  christos /* ------------------------------------------------------------------------ */
   3827       1.1  christos /* Function:    ipf_fixskip                                                 */
   3828       1.1  christos /* Returns:     Nil                                                         */
   3829       1.1  christos /* Parameters:  listp(IO)    - pointer to start of list with skip rule      */
   3830       1.1  christos /*              rp(I)        - rule added/removed with skip in it.          */
   3831       1.1  christos /*              addremove(I) - adjustment (-1/+1) to make to skip count,    */
   3832       1.1  christos /*                             depending on whether a rule was just added   */
   3833       1.1  christos /*                             or removed.                                  */
   3834       1.1  christos /*                                                                          */
   3835       1.1  christos /* Adjust all the rules in a list which would have skip'd past the position */
   3836       1.1  christos /* where we are inserting to skip to the right place given the change.      */
   3837       1.1  christos /* ------------------------------------------------------------------------ */
   3838       1.1  christos void
   3839       1.2  christos ipf_fixskip(frentry_t **listp, frentry_t *rp, int addremove)
   3840       1.1  christos {
   3841       1.1  christos 	int rules, rn;
   3842       1.1  christos 	frentry_t *fp;
   3843       1.1  christos 
   3844       1.1  christos 	rules = 0;
   3845       1.1  christos 	for (fp = *listp; (fp != NULL) && (fp != rp); fp = fp->fr_next)
   3846       1.1  christos 		rules++;
   3847       1.1  christos 
   3848       1.1  christos 	if (!fp)
   3849       1.1  christos 		return;
   3850       1.1  christos 
   3851       1.1  christos 	for (rn = 0, fp = *listp; fp && (fp != rp); fp = fp->fr_next, rn++)
   3852       1.1  christos 		if (FR_ISSKIP(fp->fr_flags) && (rn + fp->fr_arg >= rules))
   3853       1.1  christos 			fp->fr_arg += addremove;
   3854       1.1  christos }
   3855       1.1  christos 
   3856       1.1  christos 
   3857       1.1  christos #ifdef	_KERNEL
   3858       1.1  christos /* ------------------------------------------------------------------------ */
   3859       1.1  christos /* Function:    count4bits                                                  */
   3860       1.1  christos /* Returns:     int - >= 0 - number of consecutive bits in input            */
   3861       1.1  christos /* Parameters:  ip(I) - 32bit IP address                                    */
   3862       1.1  christos /*                                                                          */
   3863       1.1  christos /* IPv4 ONLY                                                                */
   3864       1.1  christos /* count consecutive 1's in bit mask.  If the mask generated by counting    */
   3865       1.1  christos /* consecutive 1's is different to that passed, return -1, else return #    */
   3866       1.1  christos /* of bits.                                                                 */
   3867       1.1  christos /* ------------------------------------------------------------------------ */
   3868       1.1  christos int
   3869       1.2  christos count4bits(u_32_t ip)
   3870       1.1  christos {
   3871       1.1  christos 	u_32_t	ipn;
   3872       1.1  christos 	int	cnt = 0, i, j;
   3873       1.1  christos 
   3874       1.1  christos 	ip = ipn = ntohl(ip);
   3875       1.1  christos 	for (i = 32; i; i--, ipn *= 2)
   3876       1.1  christos 		if (ipn & 0x80000000)
   3877       1.1  christos 			cnt++;
   3878       1.1  christos 		else
   3879       1.1  christos 			break;
   3880       1.1  christos 	ipn = 0;
   3881       1.1  christos 	for (i = 32, j = cnt; i; i--, j--) {
   3882       1.1  christos 		ipn *= 2;
   3883       1.1  christos 		if (j > 0)
   3884       1.1  christos 			ipn++;
   3885       1.1  christos 	}
   3886       1.1  christos 	if (ipn == ip)
   3887       1.1  christos 		return cnt;
   3888       1.1  christos 	return -1;
   3889       1.1  christos }
   3890       1.1  christos 
   3891       1.1  christos 
   3892       1.1  christos /* ------------------------------------------------------------------------ */
   3893       1.1  christos /* Function:    count6bits                                                  */
   3894       1.1  christos /* Returns:     int - >= 0 - number of consecutive bits in input            */
   3895       1.1  christos /* Parameters:  msk(I) - pointer to start of IPv6 bitmask                   */
   3896       1.1  christos /*                                                                          */
   3897       1.1  christos /* IPv6 ONLY                                                                */
   3898       1.1  christos /* count consecutive 1's in bit mask.                                       */
   3899       1.1  christos /* ------------------------------------------------------------------------ */
   3900       1.1  christos # ifdef USE_INET6
   3901       1.1  christos int
   3902       1.2  christos count6bits(u_32_t *msk)
   3903       1.1  christos {
   3904       1.1  christos 	int i = 0, k;
   3905       1.1  christos 	u_32_t j;
   3906       1.1  christos 
   3907       1.1  christos 	for (k = 3; k >= 0; k--)
   3908       1.1  christos 		if (msk[k] == 0xffffffff)
   3909       1.1  christos 			i += 32;
   3910       1.1  christos 		else {
   3911       1.1  christos 			for (j = msk[k]; j; j <<= 1)
   3912       1.1  christos 				if (j & 0x80000000)
   3913       1.1  christos 					i++;
   3914       1.1  christos 		}
   3915       1.1  christos 	return i;
   3916       1.1  christos }
   3917       1.1  christos # endif
   3918       1.1  christos #endif /* _KERNEL */
   3919       1.1  christos 
   3920       1.1  christos 
   3921       1.1  christos /* ------------------------------------------------------------------------ */
   3922       1.1  christos /* Function:    ipf_synclist                                                */
   3923       1.1  christos /* Returns:     int    - 0 = no failures, else indication of first failure  */
   3924       1.1  christos /* Parameters:  fr(I)  - start of filter list to sync interface names for   */
   3925       1.1  christos /*              ifp(I) - interface pointer for limiting sync lookups        */
   3926       1.1  christos /* Write Locks: ipf_mutex                                                   */
   3927       1.1  christos /*                                                                          */
   3928       1.1  christos /* Walk through a list of filter rules and resolve any interface names into */
   3929       1.1  christos /* pointers.  Where dynamic addresses are used, also update the IP address  */
   3930       1.1  christos /* used in the rule.  The interface pointer is used to limit the lookups to */
   3931       1.1  christos /* a specific set of matching names if it is non-NULL.                      */
   3932       1.1  christos /* Errors can occur when resolving the destination name of to/dup-to fields */
   3933       1.1  christos /* when the name points to a pool and that pool doest not exist. If this    */
   3934       1.1  christos /* does happen then it is necessary to check if there are any lookup refs   */
   3935       1.1  christos /* that need to be dropped before returning with an error.                  */
   3936       1.1  christos /* ------------------------------------------------------------------------ */
   3937       1.1  christos static int
   3938       1.2  christos ipf_synclist(ipf_main_softc_t *softc, frentry_t *fr, void *ifp)
   3939       1.1  christos {
   3940       1.1  christos 	frentry_t *frt, *start = fr;
   3941       1.1  christos 	frdest_t *fdp;
   3942       1.1  christos 	char *name;
   3943       1.1  christos 	int error;
   3944       1.1  christos 	void *ifa;
   3945       1.1  christos 	int v, i;
   3946       1.1  christos 
   3947       1.1  christos 	error = 0;
   3948       1.1  christos 
   3949       1.1  christos 	for (; fr; fr = fr->fr_next) {
   3950       1.1  christos 		if (fr->fr_family == AF_INET)
   3951       1.1  christos 			v = 4;
   3952       1.1  christos 		else if (fr->fr_family == AF_INET6)
   3953       1.1  christos 			v = 6;
   3954       1.1  christos 		else
   3955       1.1  christos 			v = 0;
   3956       1.1  christos 
   3957       1.1  christos 		/*
   3958       1.1  christos 		 * Lookup all the interface names that are part of the rule.
   3959       1.1  christos 		 */
   3960       1.1  christos 		for (i = 0; i < 4; i++) {
   3961       1.1  christos 			if ((ifp != NULL) && (fr->fr_ifas[i] != ifp))
   3962       1.1  christos 				continue;
   3963       1.1  christos 			if (fr->fr_ifnames[i] == -1)
   3964       1.1  christos 				continue;
   3965       1.1  christos 			name = FR_NAME(fr, fr_ifnames[i]);
   3966       1.1  christos 			fr->fr_ifas[i] = ipf_resolvenic(softc, name, v);
   3967       1.1  christos 		}
   3968       1.1  christos 
   3969       1.1  christos 		if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) {
   3970       1.1  christos 			if (fr->fr_satype != FRI_NORMAL &&
   3971       1.1  christos 			    fr->fr_satype != FRI_LOOKUP) {
   3972       1.1  christos 				ifa = ipf_resolvenic(softc, fr->fr_names +
   3973       1.1  christos 						     fr->fr_sifpidx, v);
   3974       1.1  christos 				ipf_ifpaddr(softc, v, fr->fr_satype, ifa,
   3975       1.1  christos 					    &fr->fr_src6, &fr->fr_smsk6);
   3976       1.1  christos 			}
   3977       1.1  christos 			if (fr->fr_datype != FRI_NORMAL &&
   3978       1.1  christos 			    fr->fr_datype != FRI_LOOKUP) {
   3979       1.1  christos 				ifa = ipf_resolvenic(softc, fr->fr_names +
   3980       1.1  christos 						     fr->fr_sifpidx, v);
   3981       1.1  christos 				ipf_ifpaddr(softc, v, fr->fr_datype, ifa,
   3982       1.1  christos 					    &fr->fr_dst6, &fr->fr_dmsk6);
   3983       1.1  christos 			}
   3984       1.1  christos 		}
   3985       1.1  christos 
   3986       1.1  christos 		fdp = &fr->fr_tifs[0];
   3987       1.1  christos 		if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
   3988       1.1  christos 			error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
   3989       1.1  christos 			if (error != 0)
   3990       1.1  christos 				goto unwind;
   3991       1.1  christos 		}
   3992       1.1  christos 
   3993       1.1  christos 		fdp = &fr->fr_tifs[1];
   3994       1.1  christos 		if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
   3995       1.1  christos 			error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
   3996       1.1  christos 			if (error != 0)
   3997       1.1  christos 				goto unwind;
   3998       1.1  christos 		}
   3999       1.1  christos 
   4000       1.1  christos 		fdp = &fr->fr_dif;
   4001       1.1  christos 		if ((ifp == NULL) || (fdp->fd_ptr == ifp)) {
   4002       1.1  christos 			error = ipf_resolvedest(softc, fr->fr_names, fdp, v);
   4003       1.1  christos 			if (error != 0)
   4004       1.1  christos 				goto unwind;
   4005       1.1  christos 		}
   4006       1.1  christos 
   4007       1.1  christos 		if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
   4008       1.1  christos 		    (fr->fr_satype == FRI_LOOKUP) && (fr->fr_srcptr == NULL)) {
   4009       1.1  christos 			fr->fr_srcptr = ipf_lookup_res_num(softc,
   4010       1.1  christos 							   fr->fr_srctype,
   4011       1.1  christos 							   IPL_LOGIPF,
   4012       1.1  christos 							   fr->fr_srcnum,
   4013       1.1  christos 							   &fr->fr_srcfunc);
   4014       1.1  christos 		}
   4015       1.1  christos 		if (((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
   4016       1.1  christos 		    (fr->fr_datype == FRI_LOOKUP) && (fr->fr_dstptr == NULL)) {
   4017       1.1  christos 			fr->fr_dstptr = ipf_lookup_res_num(softc,
   4018       1.1  christos 							   fr->fr_dsttype,
   4019       1.1  christos 							   IPL_LOGIPF,
   4020       1.1  christos 							   fr->fr_dstnum,
   4021       1.1  christos 							   &fr->fr_dstfunc);
   4022       1.1  christos 		}
   4023       1.1  christos 	}
   4024       1.1  christos 	return 0;
   4025       1.1  christos 
   4026       1.1  christos unwind:
   4027       1.1  christos 	for (frt = start; frt != fr; fr = fr->fr_next) {
   4028       1.1  christos 		if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
   4029       1.1  christos 		    (frt->fr_satype == FRI_LOOKUP) && (frt->fr_srcptr != NULL))
   4030       1.1  christos 				ipf_lookup_deref(softc, frt->fr_srctype,
   4031       1.1  christos 						 frt->fr_srcptr);
   4032       1.1  christos 		if (((frt->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) &&
   4033       1.1  christos 		    (frt->fr_datype == FRI_LOOKUP) && (frt->fr_dstptr != NULL))
   4034       1.1  christos 				ipf_lookup_deref(softc, frt->fr_dsttype,
   4035       1.1  christos 						 frt->fr_dstptr);
   4036       1.1  christos 	}
   4037       1.1  christos 	return error;
   4038       1.1  christos }
   4039       1.1  christos 
   4040       1.1  christos 
   4041       1.1  christos /* ------------------------------------------------------------------------ */
   4042       1.1  christos /* Function:    ipf_sync                                                    */
   4043       1.1  christos /* Returns:     void                                                        */
   4044       1.1  christos /* Parameters:  Nil                                                         */
   4045       1.1  christos /*                                                                          */
   4046       1.1  christos /* ipf_sync() is called when we suspect that the interface list or          */
   4047       1.1  christos /* information about interfaces (like IP#) has changed.  Go through all     */
   4048       1.1  christos /* filter rules, NAT entries and the state table and check if anything      */
   4049       1.1  christos /* needs to be changed/updated.                                             */
   4050       1.1  christos /* ------------------------------------------------------------------------ */
   4051       1.1  christos int
   4052       1.2  christos ipf_sync(ipf_main_softc_t *softc, void *ifp)
   4053       1.1  christos {
   4054       1.1  christos 	int i;
   4055       1.1  christos 
   4056       1.1  christos # if !SOLARIS
   4057       1.1  christos 	ipf_nat_sync(softc, ifp);
   4058       1.1  christos 	ipf_state_sync(softc, ifp);
   4059       1.1  christos 	ipf_lookup_sync(softc, ifp);
   4060       1.1  christos # endif
   4061       1.1  christos 
   4062       1.1  christos 	WRITE_ENTER(&softc->ipf_mutex);
   4063       1.1  christos 	(void) ipf_synclist(softc, softc->ipf_acct[0][softc->ipf_active], ifp);
   4064       1.1  christos 	(void) ipf_synclist(softc, softc->ipf_acct[1][softc->ipf_active], ifp);
   4065       1.1  christos 	(void) ipf_synclist(softc, softc->ipf_rules[0][softc->ipf_active], ifp);
   4066       1.1  christos 	(void) ipf_synclist(softc, softc->ipf_rules[1][softc->ipf_active], ifp);
   4067       1.1  christos 
   4068       1.1  christos 	for (i = 0; i < IPL_LOGSIZE; i++) {
   4069       1.1  christos 		frgroup_t *g;
   4070       1.1  christos 
   4071       1.1  christos 		for (g = softc->ipf_groups[i][0]; g != NULL; g = g->fg_next)
   4072       1.1  christos 			(void) ipf_synclist(softc, g->fg_start, ifp);
   4073       1.1  christos 		for (g = softc->ipf_groups[i][1]; g != NULL; g = g->fg_next)
   4074       1.1  christos 			(void) ipf_synclist(softc, g->fg_start, ifp);
   4075       1.1  christos 	}
   4076       1.1  christos 	RWLOCK_EXIT(&softc->ipf_mutex);
   4077       1.1  christos 
   4078       1.1  christos 	return 0;
   4079       1.1  christos }
   4080       1.1  christos 
   4081       1.1  christos 
   4082       1.1  christos /*
   4083       1.1  christos  * In the functions below, bcopy() is called because the pointer being
   4084       1.1  christos  * copied _from_ in this instance is a pointer to a char buf (which could
   4085       1.1  christos  * end up being unaligned) and on the kernel's local stack.
   4086       1.1  christos  */
   4087       1.1  christos /* ------------------------------------------------------------------------ */
   4088       1.1  christos /* Function:    copyinptr                                                   */
   4089       1.1  christos /* Returns:     int - 0 = success, else failure                             */
   4090       1.1  christos /* Parameters:  src(I)  - pointer to the source address                     */
   4091       1.1  christos /*              dst(I)  - destination address                               */
   4092       1.1  christos /*              size(I) - number of bytes to copy                           */
   4093       1.1  christos /*                                                                          */
   4094       1.1  christos /* Copy a block of data in from user space, given a pointer to the pointer  */
   4095       1.1  christos /* to start copying from (src) and a pointer to where to store it (dst).    */
   4096       1.1  christos /* NB: src - pointer to user space pointer, dst - kernel space pointer      */
   4097       1.1  christos /* ------------------------------------------------------------------------ */
   4098       1.1  christos int
   4099       1.2  christos copyinptr(ipf_main_softc_t *softc, void *src, void *dst, size_t size)
   4100       1.1  christos {
   4101       1.2  christos 	void *ca;
   4102       1.1  christos 	int error;
   4103       1.1  christos 
   4104       1.1  christos # if SOLARIS
   4105       1.1  christos 	error = COPYIN(src, &ca, sizeof(ca));
   4106       1.1  christos 	if (error != 0)
   4107       1.1  christos 		return error;
   4108       1.1  christos # else
   4109       1.2  christos 	bcopy(src, (void *)&ca, sizeof(ca));
   4110       1.1  christos # endif
   4111       1.1  christos 	error = COPYIN(ca, dst, size);
   4112       1.1  christos 	if (error != 0) {
   4113       1.1  christos 		IPFERROR(3);
   4114       1.1  christos 		error = EFAULT;
   4115       1.1  christos 	}
   4116       1.1  christos 	return error;
   4117       1.1  christos }
   4118       1.1  christos 
   4119       1.1  christos 
   4120       1.1  christos /* ------------------------------------------------------------------------ */
   4121       1.1  christos /* Function:    copyoutptr                                                  */
   4122       1.1  christos /* Returns:     int - 0 = success, else failure                             */
   4123       1.1  christos /* Parameters:  src(I)  - pointer to the source address                     */
   4124       1.1  christos /*              dst(I)  - destination address                               */
   4125       1.1  christos /*              size(I) - number of bytes to copy                           */
   4126       1.1  christos /*                                                                          */
   4127       1.1  christos /* Copy a block of data out to user space, given a pointer to the pointer   */
   4128       1.1  christos /* to start copying from (src) and a pointer to where to store it (dst).    */
   4129       1.1  christos /* NB: src - kernel space pointer, dst - pointer to user space pointer.     */
   4130       1.1  christos /* ------------------------------------------------------------------------ */
   4131       1.1  christos int
   4132       1.2  christos copyoutptr(ipf_main_softc_t *softc, void *src, void *dst, size_t size)
   4133       1.1  christos {
   4134       1.2  christos 	void *ca;
   4135       1.1  christos 	int error;
   4136       1.1  christos 
   4137       1.2  christos 	bcopy(dst, &ca, sizeof(ca));
   4138       1.1  christos 	error = COPYOUT(src, ca, size);
   4139       1.1  christos 	if (error != 0) {
   4140       1.1  christos 		IPFERROR(4);
   4141       1.1  christos 		error = EFAULT;
   4142       1.1  christos 	}
   4143       1.1  christos 	return error;
   4144       1.1  christos }
   4145       1.1  christos #ifdef	_KERNEL
   4146       1.1  christos #endif
   4147       1.1  christos 
   4148       1.1  christos 
   4149       1.1  christos /* ------------------------------------------------------------------------ */
   4150       1.1  christos /* Function:    ipf_lock                                                    */
   4151       1.1  christos /* Returns:     int      - 0 = success, else error                          */
   4152       1.1  christos /* Parameters:  data(I)  - pointer to lock value to set                     */
   4153       1.1  christos /*              lockp(O) - pointer to location to store old lock value      */
   4154       1.1  christos /*                                                                          */
   4155       1.1  christos /* Get the new value for the lock integer, set it and return the old value  */
   4156       1.1  christos /* in *lockp.                                                               */
   4157       1.1  christos /* ------------------------------------------------------------------------ */
   4158       1.1  christos int
   4159       1.2  christos ipf_lock(void *data, int *lockp)
   4160       1.1  christos {
   4161       1.1  christos 	int arg, err;
   4162       1.1  christos 
   4163       1.1  christos 	err = BCOPYIN(data, &arg, sizeof(arg));
   4164       1.1  christos 	if (err != 0)
   4165       1.1  christos 		return EFAULT;
   4166       1.1  christos 	err = BCOPYOUT(lockp, data, sizeof(*lockp));
   4167       1.1  christos 	if (err != 0)
   4168       1.1  christos 		return EFAULT;
   4169       1.1  christos 	*lockp = arg;
   4170       1.1  christos 	return 0;
   4171       1.1  christos }
   4172       1.1  christos 
   4173       1.1  christos 
   4174       1.1  christos /* ------------------------------------------------------------------------ */
   4175       1.1  christos /* Function:    ipf_getstat                                                 */
   4176       1.1  christos /* Returns:     Nil                                                         */
   4177       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   4178       1.1  christos /*              fiop(I)  - pointer to ipfilter stats structure              */
   4179       1.1  christos /*              rev(I)   - version claim by program doing ioctl             */
   4180       1.1  christos /*                                                                          */
   4181       1.1  christos /* Stores a copy of current pointers, counters, etc, in the friostat        */
   4182       1.1  christos /* structure.                                                               */
   4183       1.1  christos /* If IPFILTER_COMPAT is compiled, we pretend to be whatever version the    */
   4184       1.1  christos /* program is looking for. This ensure that validation of the version it    */
   4185       1.1  christos /* expects will always succeed. Thus kernels with IPFILTER_COMPAT will      */
   4186       1.1  christos /* allow older binaries to work but kernels without it will not.            */
   4187       1.1  christos /* ------------------------------------------------------------------------ */
   4188       1.1  christos /*ARGSUSED*/
   4189       1.1  christos static void
   4190       1.2  christos ipf_getstat(ipf_main_softc_t *softc, friostat_t *fiop, int rev)
   4191       1.1  christos {
   4192       1.1  christos 	int i;
   4193       1.1  christos 
   4194       1.1  christos 	bcopy((char *)softc->ipf_stats, (char *)fiop->f_st,
   4195       1.1  christos 	      sizeof(ipf_statistics_t) * 2);
   4196       1.1  christos 	fiop->f_locks[IPL_LOGSTATE] = -1;
   4197       1.1  christos 	fiop->f_locks[IPL_LOGNAT] = -1;
   4198       1.1  christos 	fiop->f_locks[IPL_LOGIPF] = -1;
   4199       1.1  christos 	fiop->f_locks[IPL_LOGAUTH] = -1;
   4200       1.1  christos 
   4201       1.1  christos 	fiop->f_ipf[0][0] = softc->ipf_rules[0][0];
   4202       1.1  christos 	fiop->f_acct[0][0] = softc->ipf_acct[0][0];
   4203       1.1  christos 	fiop->f_ipf[0][1] = softc->ipf_rules[0][1];
   4204       1.1  christos 	fiop->f_acct[0][1] = softc->ipf_acct[0][1];
   4205       1.1  christos 	fiop->f_ipf[1][0] = softc->ipf_rules[1][0];
   4206       1.1  christos 	fiop->f_acct[1][0] = softc->ipf_acct[1][0];
   4207       1.1  christos 	fiop->f_ipf[1][1] = softc->ipf_rules[1][1];
   4208       1.1  christos 	fiop->f_acct[1][1] = softc->ipf_acct[1][1];
   4209       1.1  christos 
   4210       1.1  christos 	fiop->f_ticks = softc->ipf_ticks;
   4211       1.1  christos 	fiop->f_active = softc->ipf_active;
   4212       1.1  christos 	fiop->f_froute[0] = softc->ipf_frouteok[0];
   4213       1.1  christos 	fiop->f_froute[1] = softc->ipf_frouteok[1];
   4214       1.1  christos 	fiop->f_rb_no_mem = softc->ipf_rb_no_mem;
   4215       1.1  christos 	fiop->f_rb_node_max = softc->ipf_rb_node_max;
   4216       1.1  christos 
   4217       1.1  christos 	fiop->f_running = softc->ipf_running;
   4218       1.1  christos 	for (i = 0; i < IPL_LOGSIZE; i++) {
   4219       1.1  christos 		fiop->f_groups[i][0] = softc->ipf_groups[i][0];
   4220       1.1  christos 		fiop->f_groups[i][1] = softc->ipf_groups[i][1];
   4221       1.1  christos 	}
   4222       1.1  christos #ifdef  IPFILTER_LOG
   4223       1.1  christos 	fiop->f_log_ok = ipf_log_logok(softc, IPL_LOGIPF);
   4224       1.1  christos 	fiop->f_log_fail = ipf_log_failures(softc, IPL_LOGIPF);
   4225       1.1  christos 	fiop->f_logging = 1;
   4226       1.1  christos #else
   4227       1.1  christos 	fiop->f_log_ok = 0;
   4228       1.1  christos 	fiop->f_log_fail = 0;
   4229       1.1  christos 	fiop->f_logging = 0;
   4230       1.1  christos #endif
   4231       1.1  christos 	fiop->f_defpass = softc->ipf_pass;
   4232       1.1  christos 	fiop->f_features = ipf_features;
   4233       1.1  christos 
   4234       1.1  christos #ifdef IPFILTER_COMPAT
   4235      1.14  christos 	snprintf(fiop->f_version, sizeof(fiop->f_version),
   4236      1.14  christos 		 "IP Filter: v%d.%d.%d", (rev / 1000000) % 100,
   4237      1.14  christos 		 (rev / 10000) % 100, (rev / 100) % 100);
   4238       1.1  christos #else
   4239       1.1  christos 	rev = rev;
   4240       1.1  christos 	(void) strncpy(fiop->f_version, ipfilter_version,
   4241       1.1  christos 		       sizeof(fiop->f_version));
   4242      1.14  christos         fiop->f_version[sizeof(fiop->f_version) - 1] = '\0';
   4243       1.1  christos #endif
   4244       1.1  christos }
   4245       1.1  christos 
   4246       1.1  christos 
   4247       1.1  christos #ifdef	USE_INET6
   4248       1.1  christos int icmptoicmp6types[ICMP_MAXTYPE+1] = {
   4249       1.1  christos 	ICMP6_ECHO_REPLY,	/* 0: ICMP_ECHOREPLY */
   4250       1.1  christos 	-1,			/* 1: UNUSED */
   4251       1.1  christos 	-1,			/* 2: UNUSED */
   4252       1.1  christos 	ICMP6_DST_UNREACH,	/* 3: ICMP_UNREACH */
   4253       1.1  christos 	-1,			/* 4: ICMP_SOURCEQUENCH */
   4254       1.1  christos 	ND_REDIRECT,		/* 5: ICMP_REDIRECT */
   4255       1.1  christos 	-1,			/* 6: UNUSED */
   4256       1.1  christos 	-1,			/* 7: UNUSED */
   4257       1.1  christos 	ICMP6_ECHO_REQUEST,	/* 8: ICMP_ECHO */
   4258       1.1  christos 	-1,			/* 9: UNUSED */
   4259       1.1  christos 	-1,			/* 10: UNUSED */
   4260       1.1  christos 	ICMP6_TIME_EXCEEDED,	/* 11: ICMP_TIMXCEED */
   4261       1.1  christos 	ICMP6_PARAM_PROB,	/* 12: ICMP_PARAMPROB */
   4262       1.1  christos 	-1,			/* 13: ICMP_TSTAMP */
   4263       1.1  christos 	-1,			/* 14: ICMP_TSTAMPREPLY */
   4264       1.1  christos 	-1,			/* 15: ICMP_IREQ */
   4265       1.1  christos 	-1,			/* 16: ICMP_IREQREPLY */
   4266       1.1  christos 	-1,			/* 17: ICMP_MASKREQ */
   4267       1.1  christos 	-1,			/* 18: ICMP_MASKREPLY */
   4268       1.1  christos };
   4269       1.1  christos 
   4270       1.1  christos 
   4271       1.1  christos int	icmptoicmp6unreach[ICMP_MAX_UNREACH] = {
   4272       1.1  christos 	ICMP6_DST_UNREACH_ADDR,		/* 0: ICMP_UNREACH_NET */
   4273       1.1  christos 	ICMP6_DST_UNREACH_ADDR,		/* 1: ICMP_UNREACH_HOST */
   4274       1.1  christos 	-1,				/* 2: ICMP_UNREACH_PROTOCOL */
   4275       1.1  christos 	ICMP6_DST_UNREACH_NOPORT,	/* 3: ICMP_UNREACH_PORT */
   4276       1.1  christos 	-1,				/* 4: ICMP_UNREACH_NEEDFRAG */
   4277       1.1  christos 	ICMP6_DST_UNREACH_NOTNEIGHBOR,	/* 5: ICMP_UNREACH_SRCFAIL */
   4278       1.1  christos 	ICMP6_DST_UNREACH_ADDR,		/* 6: ICMP_UNREACH_NET_UNKNOWN */
   4279       1.1  christos 	ICMP6_DST_UNREACH_ADDR,		/* 7: ICMP_UNREACH_HOST_UNKNOWN */
   4280       1.1  christos 	-1,				/* 8: ICMP_UNREACH_ISOLATED */
   4281       1.1  christos 	ICMP6_DST_UNREACH_ADMIN,	/* 9: ICMP_UNREACH_NET_PROHIB */
   4282       1.1  christos 	ICMP6_DST_UNREACH_ADMIN,	/* 10: ICMP_UNREACH_HOST_PROHIB */
   4283       1.1  christos 	-1,				/* 11: ICMP_UNREACH_TOSNET */
   4284       1.1  christos 	-1,				/* 12: ICMP_UNREACH_TOSHOST */
   4285       1.1  christos 	ICMP6_DST_UNREACH_ADMIN,	/* 13: ICMP_UNREACH_ADMIN_PROHIBIT */
   4286       1.1  christos };
   4287       1.1  christos int	icmpreplytype6[ICMP6_MAXTYPE + 1];
   4288       1.1  christos #endif
   4289       1.1  christos 
   4290       1.1  christos int	icmpreplytype4[ICMP_MAXTYPE + 1];
   4291       1.1  christos 
   4292       1.1  christos 
   4293       1.1  christos /* ------------------------------------------------------------------------ */
   4294       1.1  christos /* Function:    ipf_matchicmpqueryreply                                     */
   4295       1.1  christos /* Returns:     int - 1 if "icmp" is a valid reply to "ic" else 0.          */
   4296       1.1  christos /* Parameters:  v(I)    - IP protocol version (4 or 6)                      */
   4297       1.1  christos /*              ic(I)   - ICMP information                                  */
   4298       1.1  christos /*              icmp(I) - ICMP packet header                                */
   4299       1.1  christos /*              rev(I)  - direction (0 = forward/1 = reverse) of packet     */
   4300       1.1  christos /*                                                                          */
   4301       1.1  christos /* Check if the ICMP packet defined by the header pointed to by icmp is a   */
   4302       1.1  christos /* reply to one as described by what's in ic.  If it is a match, return 1,  */
   4303       1.1  christos /* else return 0 for no match.                                              */
   4304       1.1  christos /* ------------------------------------------------------------------------ */
   4305       1.1  christos int
   4306       1.2  christos ipf_matchicmpqueryreply(int v, icmpinfo_t *ic, icmphdr_t *icmp, int rev)
   4307       1.1  christos {
   4308       1.1  christos 	int ictype;
   4309       1.1  christos 
   4310       1.1  christos 	ictype = ic->ici_type;
   4311       1.1  christos 
   4312       1.1  christos 	if (v == 4) {
   4313       1.1  christos 		/*
   4314       1.1  christos 		 * If we matched its type on the way in, then when going out
   4315       1.1  christos 		 * it will still be the same type.
   4316       1.1  christos 		 */
   4317       1.1  christos 		if ((!rev && (icmp->icmp_type == ictype)) ||
   4318       1.1  christos 		    (rev && (icmpreplytype4[ictype] == icmp->icmp_type))) {
   4319       1.1  christos 			if (icmp->icmp_type != ICMP_ECHOREPLY)
   4320       1.1  christos 				return 1;
   4321       1.1  christos 			if (icmp->icmp_id == ic->ici_id)
   4322       1.1  christos 				return 1;
   4323       1.1  christos 		}
   4324       1.1  christos 	}
   4325       1.1  christos #ifdef	USE_INET6
   4326       1.1  christos 	else if (v == 6) {
   4327       1.1  christos 		if ((!rev && (icmp->icmp_type == ictype)) ||
   4328       1.1  christos 		    (rev && (icmpreplytype6[ictype] == icmp->icmp_type))) {
   4329       1.1  christos 			if (icmp->icmp_type != ICMP6_ECHO_REPLY)
   4330       1.1  christos 				return 1;
   4331       1.1  christos 			if (icmp->icmp_id == ic->ici_id)
   4332       1.1  christos 				return 1;
   4333       1.1  christos 		}
   4334       1.1  christos 	}
   4335       1.1  christos #endif
   4336       1.1  christos 	return 0;
   4337       1.1  christos }
   4338       1.1  christos 
   4339  1.15.4.2     skrll /* ------------------------------------------------------------------------ */
   4340  1.15.4.2     skrll /* Function:    ipf_rule_compare                                            */
   4341  1.15.4.2     skrll /* Parameters:  fr1(I) - first rule structure to compare                    */
   4342  1.15.4.2     skrll /*              fr2(I) - second rule structure to compare                   */
   4343  1.15.4.2     skrll /* Returns:     int    - 0 == rules are the same, else mismatch             */
   4344  1.15.4.2     skrll /*                                                                          */
   4345  1.15.4.2     skrll /* Compare two rules and return 0 if they match or a number indicating      */
   4346  1.15.4.2     skrll /* which of the individual checks failed.                                   */
   4347  1.15.4.2     skrll /* ------------------------------------------------------------------------ */
   4348  1.15.4.2     skrll static int
   4349  1.15.4.2     skrll ipf_rule_compare(frentry_t *fr1, frentry_t *fr2)
   4350  1.15.4.2     skrll {
   4351  1.15.4.2     skrll 	if (fr1->fr_cksum != fr2->fr_cksum)
   4352  1.15.4.2     skrll 		return 1;
   4353  1.15.4.2     skrll 	if (fr1->fr_size != fr2->fr_size)
   4354  1.15.4.2     skrll 		return 2;
   4355  1.15.4.2     skrll 	if (fr1->fr_dsize != fr2->fr_dsize)
   4356  1.15.4.2     skrll 		return 3;
   4357  1.15.4.2     skrll 	if (memcmp(&fr1->fr_func, &fr2->fr_func,
   4358  1.15.4.2     skrll 		 fr1->fr_size - offsetof(struct frentry, fr_func)) != 0)
   4359  1.15.4.2     skrll 		return 4;
   4360  1.15.4.2     skrll 	if (fr1->fr_data && !fr2->fr_data)
   4361  1.15.4.2     skrll 		return 5;
   4362  1.15.4.2     skrll 	if (!fr1->fr_data && fr2->fr_data)
   4363  1.15.4.2     skrll 		return 6;
   4364  1.15.4.2     skrll 	if (fr1->fr_data) {
   4365  1.15.4.2     skrll 		if (memcmp(fr1->fr_caddr, fr2->fr_caddr, fr1->fr_dsize))
   4366  1.15.4.2     skrll 			return 7;
   4367  1.15.4.2     skrll 	}
   4368  1.15.4.2     skrll 	return 0;
   4369  1.15.4.2     skrll }
   4370  1.15.4.2     skrll 
   4371       1.1  christos 
   4372       1.1  christos /* ------------------------------------------------------------------------ */
   4373       1.1  christos /* Function:    frrequest                                                   */
   4374       1.1  christos /* Returns:     int - 0 == success, > 0 == errno value                      */
   4375       1.1  christos /* Parameters:  unit(I)     - device for which this is for                  */
   4376       1.1  christos /*              req(I)      - ioctl command (SIOC*)                         */
   4377       1.1  christos /*              data(I)     - pointr to ioctl data                          */
   4378       1.1  christos /*              set(I)      - 1 or 0 (filter set)                           */
   4379       1.1  christos /*              makecopy(I) - flag indicating whether data points to a rule */
   4380       1.1  christos /*                            in kernel space & hence doesn't need copying. */
   4381       1.1  christos /*                                                                          */
   4382       1.1  christos /* This function handles all the requests which operate on the list of      */
   4383       1.1  christos /* filter rules.  This includes adding, deleting, insertion.  It is also    */
   4384       1.1  christos /* responsible for creating groups when a "head" rule is loaded.  Interface */
   4385       1.1  christos /* names are resolved here and other sanity checks are made on the content  */
   4386       1.1  christos /* of the rule structure being loaded.  If a rule has user defined timeouts */
   4387       1.1  christos /* then make sure they are created and initialised before exiting.          */
   4388       1.1  christos /* ------------------------------------------------------------------------ */
   4389       1.1  christos int
   4390       1.2  christos frrequest(ipf_main_softc_t *softc, int unit, ioctlcmd_t req, void *data,
   4391       1.2  christos     int set, int makecopy)
   4392       1.1  christos {
   4393       1.1  christos 	int error = 0, in, family, addrem, need_free = 0;
   4394       1.1  christos 	frentry_t frd, *fp, *f, **fprev, **ftail;
   4395      1.11    martin 	void *ptr, *uptr;
   4396       1.1  christos 	u_int *p, *pp;
   4397       1.1  christos 	frgroup_t *fg;
   4398       1.1  christos 	char *group;
   4399       1.1  christos 
   4400       1.1  christos 	ptr = NULL;
   4401       1.1  christos 	fg = NULL;
   4402       1.1  christos 	fp = &frd;
   4403       1.1  christos 	if (makecopy != 0) {
   4404       1.1  christos 		bzero(fp, sizeof(frd));
   4405       1.1  christos 		error = ipf_inobj(softc, data, NULL, fp, IPFOBJ_FRENTRY);
   4406       1.1  christos 		if (error) {
   4407       1.1  christos 			return error;
   4408       1.1  christos 		}
   4409       1.1  christos 		if ((fp->fr_type & FR_T_BUILTIN) != 0) {
   4410       1.1  christos 			IPFERROR(6);
   4411       1.1  christos 			return EINVAL;
   4412       1.1  christos 		}
   4413       1.1  christos 		KMALLOCS(f, frentry_t *, fp->fr_size);
   4414       1.1  christos 		if (f == NULL) {
   4415       1.1  christos 			IPFERROR(131);
   4416       1.1  christos 			return ENOMEM;
   4417       1.1  christos 		}
   4418       1.1  christos 		bzero(f, fp->fr_size);
   4419       1.1  christos 		error = ipf_inobjsz(softc, data, f, IPFOBJ_FRENTRY,
   4420       1.1  christos 				    fp->fr_size);
   4421       1.1  christos 		if (error) {
   4422       1.1  christos 			KFREES(f, fp->fr_size);
   4423       1.1  christos 			return error;
   4424       1.1  christos 		}
   4425       1.1  christos 
   4426       1.1  christos 		fp = f;
   4427       1.1  christos 		f = NULL;
   4428      1.15  christos 		fp->fr_next = NULL;
   4429       1.1  christos 		fp->fr_dnext = NULL;
   4430      1.15  christos 		fp->fr_pnext = NULL;
   4431      1.15  christos 		fp->fr_pdnext = NULL;
   4432      1.15  christos 		fp->fr_grp = NULL;
   4433      1.15  christos 		fp->fr_grphead = NULL;
   4434      1.15  christos 		fp->fr_icmpgrp = NULL;
   4435      1.15  christos 		fp->fr_isc = (void *)-1;
   4436      1.15  christos 		fp->fr_ptr = NULL;
   4437       1.1  christos 		fp->fr_ref = 0;
   4438       1.1  christos 		fp->fr_flags |= FR_COPIED;
   4439       1.1  christos 	} else {
   4440       1.1  christos 		fp = (frentry_t *)data;
   4441       1.1  christos 		if ((fp->fr_type & FR_T_BUILTIN) == 0) {
   4442       1.1  christos 			IPFERROR(7);
   4443       1.1  christos 			return EINVAL;
   4444       1.1  christos 		}
   4445       1.1  christos 		fp->fr_flags &= ~FR_COPIED;
   4446       1.1  christos 	}
   4447       1.1  christos 
   4448       1.1  christos 	if (((fp->fr_dsize == 0) && (fp->fr_data != NULL)) ||
   4449       1.1  christos 	    ((fp->fr_dsize != 0) && (fp->fr_data == NULL))) {
   4450       1.1  christos 		IPFERROR(8);
   4451       1.1  christos 		error = EINVAL;
   4452       1.1  christos 		goto donenolock;
   4453       1.1  christos 	}
   4454       1.1  christos 
   4455       1.1  christos 	family = fp->fr_family;
   4456       1.1  christos 	uptr = fp->fr_data;
   4457       1.1  christos 
   4458       1.1  christos 	if (req == (ioctlcmd_t)SIOCINAFR || req == (ioctlcmd_t)SIOCINIFR ||
   4459       1.1  christos 	    req == (ioctlcmd_t)SIOCADAFR || req == (ioctlcmd_t)SIOCADIFR)
   4460       1.1  christos 		addrem = 0;
   4461       1.1  christos 	else if (req == (ioctlcmd_t)SIOCRMAFR || req == (ioctlcmd_t)SIOCRMIFR)
   4462       1.1  christos 		addrem = 1;
   4463       1.1  christos 	else if (req == (ioctlcmd_t)SIOCZRLST)
   4464       1.1  christos 		addrem = 2;
   4465       1.1  christos 	else {
   4466       1.1  christos 		IPFERROR(9);
   4467       1.1  christos 		error = EINVAL;
   4468       1.1  christos 		goto donenolock;
   4469       1.1  christos 	}
   4470       1.1  christos 
   4471       1.1  christos 	/*
   4472       1.1  christos 	 * Only filter rules for IPv4 or IPv6 are accepted.
   4473       1.1  christos 	 */
   4474       1.1  christos 	if (family == AF_INET) {
   4475       1.1  christos 		/*EMPTY*/;
   4476       1.1  christos #ifdef	USE_INET6
   4477       1.1  christos 	} else if (family == AF_INET6) {
   4478       1.1  christos 		/*EMPTY*/;
   4479       1.1  christos #endif
   4480       1.1  christos 	} else if (family != 0) {
   4481       1.1  christos 		IPFERROR(10);
   4482       1.1  christos 		error = EINVAL;
   4483       1.1  christos 		goto donenolock;
   4484       1.1  christos 	}
   4485       1.1  christos 
   4486       1.1  christos 	/*
   4487       1.1  christos 	 * If the rule is being loaded from user space, i.e. we had to copy it
   4488       1.1  christos 	 * into kernel space, then do not trust the function pointer in the
   4489       1.1  christos 	 * rule.
   4490       1.1  christos 	 */
   4491       1.1  christos 	if ((makecopy == 1) && (fp->fr_func != NULL)) {
   4492       1.1  christos 		if (ipf_findfunc(fp->fr_func) == NULL) {
   4493       1.1  christos 			IPFERROR(11);
   4494       1.1  christos 			error = ESRCH;
   4495       1.1  christos 			goto donenolock;
   4496       1.1  christos 		}
   4497       1.1  christos 
   4498       1.1  christos 		if (addrem == 0) {
   4499       1.1  christos 			error = ipf_funcinit(softc, fp);
   4500       1.1  christos 			if (error != 0)
   4501       1.1  christos 				goto donenolock;
   4502       1.1  christos 		}
   4503       1.1  christos 	}
   4504       1.1  christos 	if ((fp->fr_flags & FR_CALLNOW) &&
   4505       1.3   darrenr 	    ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) {
   4506       1.1  christos 		IPFERROR(142);
   4507       1.1  christos 		error = ESRCH;
   4508       1.1  christos 		goto donenolock;
   4509       1.1  christos 	}
   4510       1.1  christos 	if (((fp->fr_flags & FR_CMDMASK) == FR_CALL) &&
   4511       1.3   darrenr 	    ((fp->fr_func == NULL) || (fp->fr_func == (ipfunc_t)-1))) {
   4512       1.1  christos 		IPFERROR(143);
   4513       1.1  christos 		error = ESRCH;
   4514       1.1  christos 		goto donenolock;
   4515       1.1  christos 	}
   4516       1.1  christos 
   4517       1.1  christos 	ptr = NULL;
   4518       1.1  christos 
   4519       1.1  christos 	if (FR_ISACCOUNT(fp->fr_flags))
   4520       1.1  christos 		unit = IPL_LOGCOUNT;
   4521       1.1  christos 
   4522       1.1  christos 	/*
   4523       1.1  christos 	 * Check that each group name in the rule has a start index that
   4524       1.1  christos 	 * is valid.
   4525       1.1  christos 	 */
   4526       1.1  christos 	if (fp->fr_icmphead != -1) {
   4527       1.1  christos 		if ((fp->fr_icmphead < 0) ||
   4528       1.1  christos 		    (fp->fr_icmphead >= fp->fr_namelen)) {
   4529       1.1  christos 			IPFERROR(136);
   4530       1.1  christos 			error = EINVAL;
   4531       1.1  christos 			goto donenolock;
   4532       1.1  christos 		}
   4533       1.1  christos 		if (!strcmp(FR_NAME(fp, fr_icmphead), "0"))
   4534       1.1  christos 			fp->fr_names[fp->fr_icmphead] = '\0';
   4535       1.1  christos 	}
   4536       1.1  christos 
   4537       1.1  christos 	if (fp->fr_grhead != -1) {
   4538       1.1  christos 		if ((fp->fr_grhead < 0) ||
   4539       1.1  christos 		    (fp->fr_grhead >= fp->fr_namelen)) {
   4540       1.1  christos 			IPFERROR(137);
   4541       1.1  christos 			error = EINVAL;
   4542       1.1  christos 			goto donenolock;
   4543       1.1  christos 		}
   4544       1.1  christos 		if (!strcmp(FR_NAME(fp, fr_grhead), "0"))
   4545       1.1  christos 			fp->fr_names[fp->fr_grhead] = '\0';
   4546       1.1  christos 	}
   4547       1.1  christos 
   4548       1.1  christos 	if (fp->fr_group != -1) {
   4549       1.1  christos 		if ((fp->fr_group < 0) ||
   4550       1.1  christos 		    (fp->fr_group >= fp->fr_namelen)) {
   4551       1.1  christos 			IPFERROR(138);
   4552       1.1  christos 			error = EINVAL;
   4553       1.1  christos 			goto donenolock;
   4554       1.1  christos 		}
   4555       1.1  christos 		if ((req != (int)SIOCZRLST) && (fp->fr_group != -1)) {
   4556       1.1  christos 			/*
   4557       1.1  christos 			 * Allow loading rules that are in groups to cause
   4558       1.1  christos 			 * them to be created if they don't already exit.
   4559       1.1  christos 			 */
   4560       1.1  christos 			group = FR_NAME(fp, fr_group);
   4561       1.3   darrenr 			if (addrem == 0) {
   4562       1.3   darrenr 				fg = ipf_group_add(softc, group, NULL,
   4563       1.3   darrenr 						   fp->fr_flags, unit, set);
   4564  1.15.4.1     skrll 				if (fg == NULL) {
   4565  1.15.4.1     skrll 					IPFERROR(152);
   4566  1.15.4.1     skrll 					error = ESRCH;
   4567  1.15.4.1     skrll 					goto donenolock;
   4568  1.15.4.1     skrll 				}
   4569       1.3   darrenr 				fp->fr_grp = fg;
   4570       1.3   darrenr 			} else {
   4571       1.3   darrenr 				fg = ipf_findgroup(softc, group, unit,
   4572       1.3   darrenr 						   set, NULL);
   4573       1.1  christos 				if (fg == NULL) {
   4574       1.1  christos 					IPFERROR(12);
   4575       1.1  christos 					error = ESRCH;
   4576       1.1  christos 					goto donenolock;
   4577       1.1  christos 				}
   4578       1.1  christos 			}
   4579       1.3   darrenr 
   4580       1.3   darrenr 			if (fg->fg_flags == 0) {
   4581       1.1  christos 				fg->fg_flags = fp->fr_flags & FR_INOUT;
   4582       1.3   darrenr 			} else if (fg->fg_flags != (fp->fr_flags & FR_INOUT)) {
   4583       1.1  christos 				IPFERROR(13);
   4584       1.1  christos 				error = ESRCH;
   4585       1.1  christos 				goto donenolock;
   4586       1.1  christos 			}
   4587       1.1  christos 		}
   4588       1.1  christos 	} else {
   4589       1.1  christos 		/*
   4590       1.1  christos 		 * If a rule is going to be part of a group then it does
   4591       1.1  christos 		 * not matter whether it is an in or out rule, but if it
   4592       1.1  christos 		 * isn't in a group, then it does...
   4593       1.1  christos 		 */
   4594       1.1  christos 		if ((fp->fr_flags & (FR_INQUE|FR_OUTQUE)) == 0) {
   4595       1.1  christos 			IPFERROR(14);
   4596       1.1  christos 			error = EINVAL;
   4597       1.1  christos 			goto donenolock;
   4598       1.1  christos 		}
   4599       1.1  christos 	}
   4600       1.1  christos 	in = (fp->fr_flags & FR_INQUE) ? 0 : 1;
   4601       1.1  christos 
   4602       1.1  christos 	/*
   4603       1.1  christos 	 * Work out which rule list this change is being applied to.
   4604       1.1  christos 	 */
   4605       1.1  christos 	ftail = NULL;
   4606       1.1  christos 	fprev = NULL;
   4607  1.15.4.2     skrll 	if (unit == IPL_LOGAUTH) {
   4608       1.2  christos 		if ((fp->fr_tifs[0].fd_ptr != NULL) ||
   4609       1.1  christos 		    (fp->fr_tifs[1].fd_ptr != NULL) ||
   4610  1.15.4.2     skrll 		    (fp->fr_dif.fd_ptr != NULL) ||
   4611       1.1  christos 		    (fp->fr_flags & FR_FASTROUTE)) {
   4612  1.15.4.2     skrll 			IPFERROR(145);
   4613       1.1  christos 			error = EINVAL;
   4614       1.1  christos 			goto donenolock;
   4615       1.1  christos 		}
   4616  1.15.4.2     skrll 		fprev = ipf_auth_rulehead(softc);
   4617       1.1  christos 	} else {
   4618       1.1  christos 		if (FR_ISACCOUNT(fp->fr_flags))
   4619       1.1  christos 			fprev = &softc->ipf_acct[in][set];
   4620       1.1  christos 		else if ((fp->fr_flags & (FR_OUTQUE|FR_INQUE)) != 0)
   4621       1.1  christos 			fprev = &softc->ipf_rules[in][set];
   4622       1.1  christos 	}
   4623       1.1  christos 	if (fprev == NULL) {
   4624       1.1  christos 		IPFERROR(15);
   4625       1.1  christos 		error = ESRCH;
   4626       1.1  christos 		goto donenolock;
   4627       1.1  christos 	}
   4628       1.1  christos 
   4629       1.1  christos 	if (fg != NULL)
   4630       1.1  christos 		fprev = &fg->fg_start;
   4631       1.1  christos 
   4632       1.1  christos 	/*
   4633       1.1  christos 	 * Copy in extra data for the rule.
   4634       1.1  christos 	 */
   4635       1.1  christos 	if (fp->fr_dsize != 0) {
   4636       1.1  christos 		if (makecopy != 0) {
   4637       1.1  christos 			KMALLOCS(ptr, void *, fp->fr_dsize);
   4638       1.1  christos 			if (ptr == NULL) {
   4639       1.1  christos 				IPFERROR(16);
   4640       1.1  christos 				error = ENOMEM;
   4641       1.1  christos 				goto donenolock;
   4642       1.1  christos 			}
   4643       1.1  christos 
   4644       1.1  christos 			/*
   4645       1.1  christos 			 * The bcopy case is for when the data is appended
   4646       1.1  christos 			 * to the rule by ipf_in_compat().
   4647       1.1  christos 			 */
   4648       1.1  christos 			if (uptr >= (void *)fp &&
   4649       1.1  christos 			    uptr < (void *)((char *)fp + fp->fr_size)) {
   4650       1.1  christos 				bcopy(uptr, ptr, fp->fr_dsize);
   4651       1.1  christos 				error = 0;
   4652       1.1  christos 			} else {
   4653       1.1  christos 				error = COPYIN(uptr, ptr, fp->fr_dsize);
   4654       1.1  christos 				if (error != 0) {
   4655       1.1  christos 					IPFERROR(17);
   4656       1.1  christos 					error = EFAULT;
   4657       1.1  christos 					goto donenolock;
   4658       1.1  christos 				}
   4659       1.1  christos 			}
   4660       1.1  christos 		} else {
   4661       1.1  christos 			ptr = uptr;
   4662       1.1  christos 		}
   4663       1.1  christos 		fp->fr_data = ptr;
   4664       1.1  christos 	} else {
   4665       1.1  christos 		fp->fr_data = NULL;
   4666       1.1  christos 	}
   4667       1.1  christos 
   4668       1.1  christos 	/*
   4669       1.1  christos 	 * Perform per-rule type sanity checks of their members.
   4670       1.1  christos 	 * All code after this needs to be aware that allocated memory
   4671       1.1  christos 	 * may need to be free'd before exiting.
   4672       1.1  christos 	 */
   4673       1.1  christos 	switch (fp->fr_type & ~FR_T_BUILTIN)
   4674       1.1  christos 	{
   4675       1.1  christos #if defined(IPFILTER_BPF)
   4676       1.1  christos 	case FR_T_BPFOPC :
   4677       1.1  christos 		if (fp->fr_dsize == 0) {
   4678       1.1  christos 			IPFERROR(19);
   4679       1.1  christos 			error = EINVAL;
   4680       1.1  christos 			break;
   4681       1.1  christos 		}
   4682       1.1  christos 		if (!bpf_validate(ptr, fp->fr_dsize/sizeof(struct bpf_insn))) {
   4683       1.1  christos 			IPFERROR(20);
   4684       1.1  christos 			error = EINVAL;
   4685       1.1  christos 			break;
   4686       1.1  christos 		}
   4687       1.1  christos 		break;
   4688       1.1  christos #endif
   4689       1.1  christos 	case FR_T_IPF :
   4690       1.1  christos 		/*
   4691       1.1  christos 		 * Preparation for error case at the bottom of this function.
   4692       1.1  christos 		 */
   4693       1.1  christos 		if (fp->fr_datype == FRI_LOOKUP)
   4694       1.1  christos 			fp->fr_dstptr = NULL;
   4695       1.1  christos 		if (fp->fr_satype == FRI_LOOKUP)
   4696       1.1  christos 			fp->fr_srcptr = NULL;
   4697       1.1  christos 
   4698       1.1  christos 		if (fp->fr_dsize != sizeof(fripf_t)) {
   4699       1.1  christos 			IPFERROR(21);
   4700       1.1  christos 			error = EINVAL;
   4701       1.1  christos 			break;
   4702       1.1  christos 		}
   4703       1.1  christos 
   4704       1.1  christos 		/*
   4705       1.1  christos 		 * Allowing a rule with both "keep state" and "with oow" is
   4706       1.1  christos 		 * pointless because adding a state entry to the table will
   4707       1.1  christos 		 * fail with the out of window (oow) flag set.
   4708       1.1  christos 		 */
   4709       1.1  christos 		if ((fp->fr_flags & FR_KEEPSTATE) && (fp->fr_flx & FI_OOW)) {
   4710       1.1  christos 			IPFERROR(22);
   4711       1.1  christos 			error = EINVAL;
   4712       1.1  christos 			break;
   4713       1.1  christos 		}
   4714       1.1  christos 
   4715       1.1  christos 		switch (fp->fr_satype)
   4716       1.1  christos 		{
   4717       1.1  christos 		case FRI_BROADCAST :
   4718       1.1  christos 		case FRI_DYNAMIC :
   4719       1.1  christos 		case FRI_NETWORK :
   4720       1.1  christos 		case FRI_NETMASKED :
   4721       1.1  christos 		case FRI_PEERADDR :
   4722       1.1  christos 			if (fp->fr_sifpidx < 0) {
   4723       1.1  christos 				IPFERROR(23);
   4724       1.1  christos 				error = EINVAL;
   4725       1.1  christos 			}
   4726       1.1  christos 			break;
   4727       1.1  christos 		case FRI_LOOKUP :
   4728       1.1  christos 			fp->fr_srcptr = ipf_findlookup(softc, unit, fp,
   4729       1.1  christos 						       &fp->fr_src6,
   4730       1.1  christos 						       &fp->fr_smsk6);
   4731       1.1  christos 			if (fp->fr_srcfunc == NULL) {
   4732       1.1  christos 				IPFERROR(132);
   4733       1.1  christos 				error = ESRCH;
   4734       1.1  christos 				break;
   4735       1.1  christos 			}
   4736       1.1  christos 			break;
   4737       1.1  christos 		case FRI_NORMAL :
   4738       1.1  christos 			break;
   4739       1.1  christos 		default :
   4740       1.1  christos 			IPFERROR(133);
   4741       1.1  christos 			error = EINVAL;
   4742       1.1  christos 			break;
   4743       1.1  christos 		}
   4744       1.1  christos 		if (error != 0)
   4745       1.1  christos 			break;
   4746       1.1  christos 
   4747       1.1  christos 		switch (fp->fr_datype)
   4748       1.1  christos 		{
   4749       1.1  christos 		case FRI_BROADCAST :
   4750       1.1  christos 		case FRI_DYNAMIC :
   4751       1.1  christos 		case FRI_NETWORK :
   4752       1.1  christos 		case FRI_NETMASKED :
   4753       1.1  christos 		case FRI_PEERADDR :
   4754       1.1  christos 			if (fp->fr_difpidx < 0) {
   4755       1.1  christos 				IPFERROR(24);
   4756       1.1  christos 				error = EINVAL;
   4757       1.1  christos 			}
   4758       1.1  christos 			break;
   4759       1.1  christos 		case FRI_LOOKUP :
   4760       1.1  christos 			fp->fr_dstptr = ipf_findlookup(softc, unit, fp,
   4761       1.1  christos 						       &fp->fr_dst6,
   4762       1.1  christos 						       &fp->fr_dmsk6);
   4763       1.1  christos 			if (fp->fr_dstfunc == NULL) {
   4764       1.1  christos 				IPFERROR(134);
   4765       1.1  christos 				error = ESRCH;
   4766       1.1  christos 			}
   4767       1.1  christos 			break;
   4768       1.1  christos 		case FRI_NORMAL :
   4769       1.1  christos 			break;
   4770       1.1  christos 		default :
   4771       1.1  christos 			IPFERROR(135);
   4772       1.1  christos 			error = EINVAL;
   4773       1.1  christos 		}
   4774       1.1  christos 		break;
   4775       1.1  christos 
   4776       1.1  christos 	case FR_T_NONE :
   4777       1.1  christos 	case FR_T_CALLFUNC :
   4778       1.1  christos 	case FR_T_COMPIPF :
   4779       1.1  christos 		break;
   4780       1.1  christos 
   4781       1.1  christos 	case FR_T_IPFEXPR :
   4782       1.1  christos 		if (ipf_matcharray_verify(fp->fr_data, fp->fr_dsize) == -1) {
   4783       1.1  christos 			IPFERROR(25);
   4784       1.1  christos 			error = EINVAL;
   4785       1.1  christos 		}
   4786       1.1  christos 		break;
   4787       1.1  christos 
   4788       1.1  christos 	default :
   4789       1.1  christos 		IPFERROR(26);
   4790       1.1  christos 		error = EINVAL;
   4791       1.1  christos 		break;
   4792       1.1  christos 	}
   4793       1.1  christos 	if (error != 0)
   4794       1.1  christos 		goto donenolock;
   4795       1.1  christos 
   4796       1.1  christos 	if (fp->fr_tif.fd_name != -1) {
   4797       1.1  christos 		if ((fp->fr_tif.fd_name < 0) ||
   4798       1.1  christos 		    (fp->fr_tif.fd_name >= fp->fr_namelen)) {
   4799       1.1  christos 			IPFERROR(139);
   4800       1.1  christos 			error = EINVAL;
   4801       1.1  christos 			goto donenolock;
   4802       1.1  christos 		}
   4803       1.1  christos 	}
   4804       1.1  christos 
   4805       1.1  christos 	if (fp->fr_dif.fd_name != -1) {
   4806       1.1  christos 		if ((fp->fr_dif.fd_name < 0) ||
   4807       1.1  christos 		    (fp->fr_dif.fd_name >= fp->fr_namelen)) {
   4808       1.1  christos 			IPFERROR(140);
   4809       1.1  christos 			error = EINVAL;
   4810       1.1  christos 			goto donenolock;
   4811       1.1  christos 		}
   4812       1.1  christos 	}
   4813       1.1  christos 
   4814       1.1  christos 	if (fp->fr_rif.fd_name != -1) {
   4815       1.1  christos 		if ((fp->fr_rif.fd_name < 0) ||
   4816       1.1  christos 		    (fp->fr_rif.fd_name >= fp->fr_namelen)) {
   4817       1.1  christos 			IPFERROR(141);
   4818       1.1  christos 			error = EINVAL;
   4819       1.1  christos 			goto donenolock;
   4820       1.1  christos 		}
   4821       1.1  christos 	}
   4822       1.1  christos 
   4823       1.1  christos 	/*
   4824       1.1  christos 	 * Lookup all the interface names that are part of the rule.
   4825       1.1  christos 	 */
   4826       1.1  christos 	error = ipf_synclist(softc, fp, NULL);
   4827       1.1  christos 	if (error != 0)
   4828       1.1  christos 		goto donenolock;
   4829       1.1  christos 	fp->fr_statecnt = 0;
   4830       1.1  christos 	if (fp->fr_srctrack.ht_max_nodes != 0)
   4831       1.1  christos 		ipf_rb_ht_init(&fp->fr_srctrack);
   4832       1.1  christos 
   4833       1.1  christos 	/*
   4834       1.1  christos 	 * Look for an existing matching filter rule, but don't include the
   4835       1.1  christos 	 * next or interface pointer in the comparison (fr_next, fr_ifa).
   4836       1.1  christos 	 * This elminates rules which are indentical being loaded.  Checksum
   4837       1.1  christos 	 * the constant part of the filter rule to make comparisons quicker
   4838       1.1  christos 	 * (this meaning no pointers are included).
   4839       1.1  christos 	 */
   4840       1.1  christos 	for (fp->fr_cksum = 0, p = (u_int *)&fp->fr_func, pp = &fp->fr_cksum;
   4841       1.1  christos 	     p < pp; p++)
   4842       1.1  christos 		fp->fr_cksum += *p;
   4843       1.2  christos 	pp = (u_int *)((char *)fp->fr_caddr + fp->fr_dsize);
   4844       1.1  christos 	for (p = (u_int *)fp->fr_data; p < pp; p++)
   4845       1.1  christos 		fp->fr_cksum += *p;
   4846       1.1  christos 
   4847       1.1  christos 	WRITE_ENTER(&softc->ipf_mutex);
   4848       1.1  christos 
   4849       1.1  christos 	/*
   4850       1.1  christos 	 * Now that the filter rule lists are locked, we can walk the
   4851       1.1  christos 	 * chain of them without fear.
   4852       1.1  christos 	 */
   4853       1.1  christos 	ftail = fprev;
   4854       1.1  christos 	for (f = *ftail; (f = *ftail) != NULL; ftail = &f->fr_next) {
   4855       1.1  christos 		if (fp->fr_collect <= f->fr_collect) {
   4856       1.1  christos 			ftail = fprev;
   4857       1.1  christos 			f = NULL;
   4858       1.1  christos 			break;
   4859       1.1  christos 		}
   4860       1.1  christos 		fprev = ftail;
   4861       1.1  christos 	}
   4862       1.1  christos 
   4863       1.1  christos 	for (; (f = *ftail) != NULL; ftail = &f->fr_next) {
   4864       1.3   darrenr 		DT2(rule_cmp, frentry_t *, fp, frentry_t *, f);
   4865  1.15.4.2     skrll 		if (ipf_rule_compare(fp, f) == 0)
   4866       1.1  christos 			break;
   4867       1.1  christos 	}
   4868       1.1  christos 
   4869       1.1  christos 	/*
   4870       1.1  christos 	 * If zero'ing statistics, copy current to caller and zero.
   4871       1.1  christos 	 */
   4872       1.1  christos 	if (addrem == 2) {
   4873       1.1  christos 		if (f == NULL) {
   4874       1.1  christos 			IPFERROR(27);
   4875       1.1  christos 			error = ESRCH;
   4876       1.1  christos 		} else {
   4877       1.1  christos 			/*
   4878       1.1  christos 			 * Copy and reduce lock because of impending copyout.
   4879       1.1  christos 			 * Well we should, but if we do then the atomicity of
   4880       1.1  christos 			 * this call and the correctness of fr_hits and
   4881       1.1  christos 			 * fr_bytes cannot be guaranteed.  As it is, this code
   4882       1.1  christos 			 * only resets them to 0 if they are successfully
   4883       1.1  christos 			 * copied out into user space.
   4884       1.1  christos 			 */
   4885       1.1  christos 			bcopy((char *)f, (char *)fp, f->fr_size);
   4886       1.1  christos 			/* MUTEX_DOWNGRADE(&softc->ipf_mutex); */
   4887       1.1  christos 
   4888       1.1  christos 			/*
   4889       1.1  christos 			 * When we copy this rule back out, set the data
   4890       1.1  christos 			 * pointer to be what it was in user space.
   4891       1.1  christos 			 */
   4892       1.1  christos 			fp->fr_data = uptr;
   4893       1.1  christos 			error = ipf_outobj(softc, data, fp, IPFOBJ_FRENTRY);
   4894       1.1  christos 
   4895       1.1  christos 			if (error == 0) {
   4896       1.1  christos 				if ((f->fr_dsize != 0) && (uptr != NULL))
   4897       1.1  christos 					error = COPYOUT(f->fr_data, uptr,
   4898       1.1  christos 							f->fr_dsize);
   4899       1.1  christos 					if (error != 0) {
   4900       1.1  christos 						IPFERROR(28);
   4901       1.1  christos 						error = EFAULT;
   4902       1.1  christos 					}
   4903       1.1  christos 				if (error == 0) {
   4904       1.1  christos 					f->fr_hits = 0;
   4905       1.1  christos 					f->fr_bytes = 0;
   4906       1.1  christos 				}
   4907       1.1  christos 			}
   4908       1.1  christos 		}
   4909       1.1  christos 
   4910       1.1  christos 		if (makecopy != 0) {
   4911       1.1  christos 			if (ptr != NULL) {
   4912       1.1  christos 				KFREES(ptr, fp->fr_dsize);
   4913       1.1  christos 			}
   4914       1.1  christos 			KFREES(fp, fp->fr_size);
   4915       1.1  christos 		}
   4916       1.1  christos 		RWLOCK_EXIT(&softc->ipf_mutex);
   4917       1.1  christos 		return error;
   4918       1.1  christos 	}
   4919       1.1  christos 
   4920       1.1  christos   	if (!f) {
   4921       1.1  christos 		/*
   4922       1.1  christos 		 * At the end of this, ftail must point to the place where the
   4923       1.1  christos 		 * new rule is to be saved/inserted/added.
   4924       1.1  christos 		 * For SIOCAD*FR, this should be the last rule in the group of
   4925       1.1  christos 		 * rules that have equal fr_collect fields.
   4926       1.1  christos 		 * For SIOCIN*FR, ...
   4927       1.1  christos 		 */
   4928       1.1  christos 		if (req == (ioctlcmd_t)SIOCADAFR ||
   4929       1.1  christos 		    req == (ioctlcmd_t)SIOCADIFR) {
   4930       1.1  christos 
   4931       1.1  christos 			for (ftail = fprev; (f = *ftail) != NULL; ) {
   4932       1.1  christos 				if (f->fr_collect > fp->fr_collect)
   4933       1.1  christos 					break;
   4934       1.1  christos 				ftail = &f->fr_next;
   4935      1.15  christos 				fprev = ftail;
   4936       1.1  christos 			}
   4937      1.15  christos 			ftail = fprev;
   4938       1.1  christos 			f = NULL;
   4939       1.1  christos 			ptr = NULL;
   4940       1.1  christos 		} else if (req == (ioctlcmd_t)SIOCINAFR ||
   4941       1.1  christos 			   req == (ioctlcmd_t)SIOCINIFR) {
   4942       1.1  christos 			while ((f = *fprev) != NULL) {
   4943       1.1  christos 				if (f->fr_collect >= fp->fr_collect)
   4944       1.1  christos 					break;
   4945       1.1  christos 				fprev = &f->fr_next;
   4946       1.1  christos 			}
   4947       1.1  christos   			ftail = fprev;
   4948       1.1  christos   			if (fp->fr_hits != 0) {
   4949       1.1  christos 				while (fp->fr_hits && (f = *ftail)) {
   4950       1.1  christos 					if (f->fr_collect != fp->fr_collect)
   4951       1.1  christos 						break;
   4952       1.1  christos 					fprev = ftail;
   4953       1.1  christos   					ftail = &f->fr_next;
   4954       1.1  christos 					fp->fr_hits--;
   4955       1.1  christos 				}
   4956       1.1  christos   			}
   4957       1.1  christos   			f = NULL;
   4958       1.1  christos   			ptr = NULL;
   4959       1.1  christos 		}
   4960       1.1  christos 	}
   4961       1.1  christos 
   4962       1.1  christos 	/*
   4963       1.1  christos 	 * Request to remove a rule.
   4964       1.1  christos 	 */
   4965       1.1  christos 	if (addrem == 1) {
   4966       1.1  christos 		if (!f) {
   4967       1.1  christos 			IPFERROR(29);
   4968       1.1  christos 			error = ESRCH;
   4969       1.1  christos 		} else {
   4970       1.1  christos 			/*
   4971       1.1  christos 			 * Do not allow activity from user space to interfere
   4972       1.1  christos 			 * with rules not loaded that way.
   4973       1.1  christos 			 */
   4974       1.1  christos 			if ((makecopy == 1) && !(f->fr_flags & FR_COPIED)) {
   4975       1.1  christos 				IPFERROR(30);
   4976       1.1  christos 				error = EPERM;
   4977       1.1  christos 				goto done;
   4978       1.1  christos 			}
   4979       1.1  christos 
   4980       1.1  christos 			/*
   4981       1.1  christos 			 * Return EBUSY if the rule is being reference by
   4982       1.1  christos 			 * something else (eg state information.)
   4983       1.1  christos 			 */
   4984       1.1  christos 			if (f->fr_ref > 1) {
   4985       1.1  christos 				IPFERROR(31);
   4986       1.1  christos 				error = EBUSY;
   4987       1.1  christos 				goto done;
   4988       1.1  christos 			}
   4989       1.1  christos #ifdef	IPFILTER_SCAN
   4990       1.1  christos 			if (f->fr_isctag != -1 &&
   4991       1.1  christos 			    (f->fr_isc != (struct ipscan *)-1))
   4992       1.1  christos 				ipf_scan_detachfr(f);
   4993       1.1  christos #endif
   4994       1.1  christos 
   4995       1.1  christos 			if (unit == IPL_LOGAUTH) {
   4996       1.1  christos 				error = ipf_auth_precmd(softc, req, f, ftail);
   4997       1.1  christos 				goto done;
   4998       1.1  christos 			}
   4999       1.1  christos 
   5000       1.1  christos 			ipf_rule_delete(softc, f, unit, set);
   5001       1.1  christos 
   5002       1.1  christos 			need_free = makecopy;
   5003       1.1  christos 		}
   5004       1.1  christos 	} else {
   5005       1.1  christos 		/*
   5006       1.1  christos 		 * Not removing, so we must be adding/inserting a rule.
   5007       1.1  christos 		 */
   5008       1.1  christos 		if (f != NULL) {
   5009       1.1  christos 			IPFERROR(32);
   5010       1.1  christos 			error = EEXIST;
   5011       1.1  christos 			goto done;
   5012       1.1  christos 		}
   5013       1.1  christos 		if (unit == IPL_LOGAUTH) {
   5014       1.1  christos 			error = ipf_auth_precmd(softc, req, fp, ftail);
   5015       1.1  christos 			goto done;
   5016       1.1  christos 		}
   5017       1.1  christos 
   5018       1.1  christos 		MUTEX_NUKE(&fp->fr_lock);
   5019       1.1  christos 		MUTEX_INIT(&fp->fr_lock, "filter rule lock");
   5020       1.1  christos 		if (fp->fr_die != 0)
   5021       1.1  christos 			ipf_rule_expire_insert(softc, fp, set);
   5022       1.1  christos 
   5023       1.1  christos 		fp->fr_hits = 0;
   5024       1.1  christos 		if (makecopy != 0)
   5025       1.1  christos 			fp->fr_ref = 1;
   5026       1.1  christos 		fp->fr_pnext = ftail;
   5027       1.1  christos 		fp->fr_next = *ftail;
   5028      1.15  christos 		if (fp->fr_next != NULL)
   5029      1.15  christos 			fp->fr_next->fr_pnext = &fp->fr_next;
   5030       1.1  christos 		*ftail = fp;
   5031       1.1  christos 		if (addrem == 0)
   5032       1.1  christos 			ipf_fixskip(ftail, fp, 1);
   5033       1.1  christos 
   5034       1.1  christos 		fp->fr_icmpgrp = NULL;
   5035       1.1  christos 		if (fp->fr_icmphead != -1) {
   5036       1.1  christos 			group = FR_NAME(fp, fr_icmphead);
   5037       1.1  christos 			fg = ipf_group_add(softc, group, fp, 0, unit, set);
   5038       1.3   darrenr 			fp->fr_icmpgrp = fg;
   5039       1.1  christos 		}
   5040       1.1  christos 
   5041       1.3   darrenr 		fp->fr_grphead = NULL;
   5042       1.1  christos 		if (fp->fr_grhead != -1) {
   5043       1.1  christos 			group = FR_NAME(fp, fr_grhead);
   5044       1.1  christos 			fg = ipf_group_add(softc, group, fp, fp->fr_flags,
   5045       1.1  christos 					   unit, set);
   5046       1.3   darrenr 			fp->fr_grphead = fg;
   5047       1.1  christos 		}
   5048       1.1  christos 	}
   5049       1.1  christos done:
   5050       1.1  christos 	RWLOCK_EXIT(&softc->ipf_mutex);
   5051       1.1  christos donenolock:
   5052       1.1  christos 	if (need_free || (error != 0)) {
   5053       1.1  christos 		if ((fp->fr_type & ~FR_T_BUILTIN) == FR_T_IPF) {
   5054       1.1  christos 			if ((fp->fr_satype == FRI_LOOKUP) &&
   5055       1.1  christos 			    (fp->fr_srcptr != NULL))
   5056       1.1  christos 				ipf_lookup_deref(softc, fp->fr_srctype,
   5057       1.1  christos 						 fp->fr_srcptr);
   5058       1.1  christos 			if ((fp->fr_datype == FRI_LOOKUP) &&
   5059       1.1  christos 			    (fp->fr_dstptr != NULL))
   5060       1.1  christos 				ipf_lookup_deref(softc, fp->fr_dsttype,
   5061       1.1  christos 						 fp->fr_dstptr);
   5062       1.1  christos 		}
   5063       1.3   darrenr 		if (fp->fr_grp != NULL) {
   5064       1.3   darrenr 			WRITE_ENTER(&softc->ipf_mutex);
   5065       1.3   darrenr 			ipf_group_del(softc, fp->fr_grp, fp);
   5066       1.3   darrenr 			RWLOCK_EXIT(&softc->ipf_mutex);
   5067       1.3   darrenr 		}
   5068       1.1  christos 		if ((ptr != NULL) && (makecopy != 0)) {
   5069       1.1  christos 			KFREES(ptr, fp->fr_dsize);
   5070       1.1  christos 		}
   5071       1.1  christos 		KFREES(fp, fp->fr_size);
   5072       1.1  christos 	}
   5073       1.1  christos 	return (error);
   5074       1.1  christos }
   5075       1.1  christos 
   5076       1.1  christos 
   5077       1.1  christos /* ------------------------------------------------------------------------ */
   5078       1.1  christos /* Function:   ipf_rule_delete                                              */
   5079       1.1  christos /* Returns:    Nil                                                          */
   5080       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   5081       1.1  christos /*             f(I)     - pointer to the rule being deleted                 */
   5082       1.1  christos /*             ftail(I) - pointer to the pointer to f                       */
   5083       1.1  christos /*             unit(I)  - device for which this is for                      */
   5084       1.1  christos /*             set(I)   - 1 or 0 (filter set)                               */
   5085       1.1  christos /*                                                                          */
   5086       1.1  christos /* This function attempts to do what it can to delete a filter rule: remove */
   5087       1.1  christos /* it from any linked lists and remove any groups it is responsible for.    */
   5088       1.1  christos /* But in the end, removing a rule can only drop the reference count - we   */
   5089       1.1  christos /* must use that as the guide for whether or not it can be freed.           */
   5090       1.1  christos /* ------------------------------------------------------------------------ */
   5091       1.1  christos static void
   5092       1.2  christos ipf_rule_delete(ipf_main_softc_t *softc, frentry_t *f, int unit, int set)
   5093       1.1  christos {
   5094       1.1  christos 
   5095       1.1  christos 	/*
   5096       1.1  christos 	 * If fr_pdnext is set, then the rule is on the expire list, so
   5097       1.1  christos 	 * remove it from there.
   5098       1.1  christos 	 */
   5099       1.1  christos 	if (f->fr_pdnext != NULL) {
   5100       1.1  christos 		*f->fr_pdnext = f->fr_dnext;
   5101       1.1  christos 		if (f->fr_dnext != NULL)
   5102       1.1  christos 			f->fr_dnext->fr_pdnext = f->fr_pdnext;
   5103       1.1  christos 		f->fr_pdnext = NULL;
   5104       1.1  christos 		f->fr_dnext = NULL;
   5105       1.1  christos 	}
   5106       1.1  christos 
   5107       1.1  christos 	ipf_fixskip(f->fr_pnext, f, -1);
   5108       1.1  christos 	if (f->fr_pnext != NULL)
   5109       1.1  christos 		*f->fr_pnext = f->fr_next;
   5110       1.1  christos 	if (f->fr_next != NULL)
   5111       1.1  christos 		f->fr_next->fr_pnext = f->fr_pnext;
   5112       1.1  christos 	f->fr_pnext = NULL;
   5113       1.1  christos 	f->fr_next = NULL;
   5114       1.1  christos 
   5115       1.1  christos 	(void) ipf_derefrule(softc, &f);
   5116       1.1  christos }
   5117       1.1  christos 
   5118       1.1  christos /* ------------------------------------------------------------------------ */
   5119       1.1  christos /* Function:   ipf_rule_expire_insert                                       */
   5120       1.1  christos /* Returns:    Nil                                                          */
   5121       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   5122       1.1  christos /*             f(I)     - pointer to rule to be added to expire list        */
   5123       1.1  christos /*             set(I)   - 1 or 0 (filter set)                               */
   5124       1.1  christos /*                                                                          */
   5125       1.1  christos /* If the new rule has a given expiration time, insert it into the list of  */
   5126       1.1  christos /* expiring rules with the ones to be removed first added to the front of   */
   5127       1.1  christos /* the list. The insertion is O(n) but it is kept sorted for quick scans at */
   5128       1.1  christos /* expiration interval checks.                                              */
   5129       1.1  christos /* ------------------------------------------------------------------------ */
   5130       1.1  christos static void
   5131       1.2  christos ipf_rule_expire_insert(ipf_main_softc_t *softc, frentry_t *f, int set)
   5132       1.1  christos {
   5133       1.1  christos 	frentry_t *fr;
   5134       1.1  christos 
   5135       1.1  christos 	/*
   5136       1.1  christos 	 */
   5137       1.1  christos 
   5138       1.1  christos 	f->fr_die = softc->ipf_ticks + IPF_TTLVAL(f->fr_die);
   5139       1.1  christos 	for (fr = softc->ipf_rule_explist[set]; fr != NULL;
   5140       1.1  christos 	     fr = fr->fr_dnext) {
   5141       1.1  christos 		if (f->fr_die < fr->fr_die)
   5142       1.1  christos 			break;
   5143       1.1  christos 		if (fr->fr_dnext == NULL) {
   5144       1.1  christos 			/*
   5145       1.1  christos 			 * We've got to the last rule and everything
   5146       1.1  christos 			 * wanted to be expired before this new node,
   5147       1.1  christos 			 * so we have to tack it on the end...
   5148       1.1  christos 			 */
   5149       1.1  christos 			fr->fr_dnext = f;
   5150       1.1  christos 			f->fr_pdnext = &fr->fr_dnext;
   5151       1.1  christos 			fr = NULL;
   5152       1.1  christos 			break;
   5153       1.1  christos 		}
   5154       1.1  christos 	}
   5155       1.1  christos 
   5156       1.1  christos 	if (softc->ipf_rule_explist[set] == NULL) {
   5157       1.1  christos 		softc->ipf_rule_explist[set] = f;
   5158       1.1  christos 		f->fr_pdnext = &softc->ipf_rule_explist[set];
   5159       1.1  christos 	} else if (fr != NULL) {
   5160       1.1  christos 		f->fr_dnext = fr;
   5161       1.1  christos 		f->fr_pdnext = fr->fr_pdnext;
   5162       1.1  christos 		fr->fr_pdnext = &f->fr_dnext;
   5163       1.1  christos 	}
   5164       1.1  christos }
   5165       1.1  christos 
   5166       1.1  christos 
   5167       1.1  christos /* ------------------------------------------------------------------------ */
   5168       1.1  christos /* Function:   ipf_findlookup                                               */
   5169       1.1  christos /* Returns:    NULL = failure, else success                                 */
   5170       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   5171       1.1  christos /*             unit(I)  - ipf device we want to find match for              */
   5172       1.1  christos /*             fp(I)    - rule for which lookup is for                      */
   5173       1.1  christos /*             addrp(I) - pointer to lookup information in address struct   */
   5174       1.1  christos /*             maskp(O) - pointer to lookup information for storage         */
   5175       1.1  christos /*                                                                          */
   5176       1.1  christos /* When using pools and hash tables to store addresses for matching in      */
   5177       1.1  christos /* rules, it is necessary to resolve both the object referred to by the     */
   5178       1.1  christos /* name or address (and return that pointer) and also provide the means by  */
   5179       1.1  christos /* which to determine if an address belongs to that object to make the      */
   5180       1.1  christos /* packet matching quicker.                                                 */
   5181       1.1  christos /* ------------------------------------------------------------------------ */
   5182       1.1  christos static void *
   5183       1.2  christos ipf_findlookup(ipf_main_softc_t *softc, int unit, frentry_t *fr,
   5184       1.2  christos     i6addr_t *addrp, i6addr_t *maskp)
   5185       1.1  christos {
   5186       1.1  christos 	void *ptr = NULL;
   5187       1.1  christos 
   5188       1.1  christos 	switch (addrp->iplookupsubtype)
   5189       1.1  christos 	{
   5190       1.1  christos 	case 0 :
   5191       1.1  christos 		ptr = ipf_lookup_res_num(softc, unit, addrp->iplookuptype,
   5192       1.1  christos 					 addrp->iplookupnum,
   5193       1.1  christos 					 &maskp->iplookupfunc);
   5194       1.1  christos 		break;
   5195       1.1  christos 	case 1 :
   5196       1.1  christos 		if (addrp->iplookupname < 0)
   5197       1.1  christos 			break;
   5198       1.1  christos 		if (addrp->iplookupname >= fr->fr_namelen)
   5199       1.1  christos 			break;
   5200       1.1  christos 		ptr = ipf_lookup_res_name(softc, unit, addrp->iplookuptype,
   5201       1.1  christos 					  fr->fr_names + addrp->iplookupname,
   5202       1.1  christos 					  &maskp->iplookupfunc);
   5203       1.1  christos 		break;
   5204       1.1  christos 	default :
   5205       1.1  christos 		break;
   5206       1.1  christos 	}
   5207       1.1  christos 
   5208       1.1  christos 	return ptr;
   5209       1.1  christos }
   5210       1.1  christos 
   5211       1.1  christos 
   5212       1.1  christos /* ------------------------------------------------------------------------ */
   5213       1.1  christos /* Function:    ipf_funcinit                                                */
   5214       1.1  christos /* Returns:     int - 0 == success, else ESRCH: cannot resolve rule details */
   5215       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   5216       1.1  christos /*              fr(I)    - pointer to filter rule                           */
   5217       1.1  christos /*                                                                          */
   5218       1.1  christos /* If a rule is a call rule, then check if the function it points to needs  */
   5219       1.1  christos /* an init function to be called now the rule has been loaded.              */
   5220       1.1  christos /* ------------------------------------------------------------------------ */
   5221       1.1  christos static int
   5222       1.2  christos ipf_funcinit(ipf_main_softc_t *softc, frentry_t *fr)
   5223       1.1  christos {
   5224       1.1  christos 	ipfunc_resolve_t *ft;
   5225       1.1  christos 	int err;
   5226       1.1  christos 
   5227       1.1  christos 	IPFERROR(34);
   5228       1.1  christos 	err = ESRCH;
   5229       1.1  christos 
   5230       1.1  christos 	for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
   5231       1.1  christos 		if (ft->ipfu_addr == fr->fr_func) {
   5232       1.1  christos 			err = 0;
   5233       1.1  christos 			if (ft->ipfu_init != NULL)
   5234       1.1  christos 				err = (*ft->ipfu_init)(softc, fr);
   5235       1.1  christos 			break;
   5236       1.1  christos 		}
   5237       1.1  christos 	return err;
   5238       1.1  christos }
   5239       1.1  christos 
   5240       1.1  christos 
   5241       1.1  christos /* ------------------------------------------------------------------------ */
   5242       1.1  christos /* Function:    ipf_funcfini                                                */
   5243       1.1  christos /* Returns:     Nil                                                         */
   5244       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   5245       1.1  christos /*              fr(I)    - pointer to filter rule                           */
   5246       1.1  christos /*                                                                          */
   5247       1.1  christos /* For a given filter rule, call the matching "fini" function if the rule   */
   5248       1.1  christos /* is using a known function that would have resulted in the "init" being   */
   5249       1.1  christos /* called for ealier.                                                       */
   5250       1.1  christos /* ------------------------------------------------------------------------ */
   5251       1.1  christos static void
   5252       1.2  christos ipf_funcfini(ipf_main_softc_t *softc, frentry_t *fr)
   5253       1.1  christos {
   5254       1.1  christos 	ipfunc_resolve_t *ft;
   5255       1.1  christos 
   5256       1.1  christos 	for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
   5257       1.1  christos 		if (ft->ipfu_addr == fr->fr_func) {
   5258       1.1  christos 			if (ft->ipfu_fini != NULL)
   5259       1.1  christos 				(void) (*ft->ipfu_fini)(softc, fr);
   5260       1.1  christos 			break;
   5261       1.1  christos 		}
   5262       1.1  christos }
   5263       1.1  christos 
   5264       1.1  christos 
   5265       1.1  christos /* ------------------------------------------------------------------------ */
   5266       1.1  christos /* Function:    ipf_findfunc                                                */
   5267       1.1  christos /* Returns:     ipfunc_t - pointer to function if found, else NULL          */
   5268       1.1  christos /* Parameters:  funcptr(I) - function pointer to lookup                     */
   5269       1.1  christos /*                                                                          */
   5270       1.1  christos /* Look for a function in the table of known functions.                     */
   5271       1.1  christos /* ------------------------------------------------------------------------ */
   5272       1.1  christos static ipfunc_t
   5273       1.2  christos ipf_findfunc(ipfunc_t funcptr)
   5274       1.1  christos {
   5275       1.1  christos 	ipfunc_resolve_t *ft;
   5276       1.1  christos 
   5277       1.1  christos 	for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
   5278       1.1  christos 		if (ft->ipfu_addr == funcptr)
   5279       1.1  christos 			return funcptr;
   5280       1.1  christos 	return NULL;
   5281       1.1  christos }
   5282       1.1  christos 
   5283       1.1  christos 
   5284       1.1  christos /* ------------------------------------------------------------------------ */
   5285       1.1  christos /* Function:    ipf_resolvefunc                                             */
   5286       1.1  christos /* Returns:     int - 0 == success, else error                              */
   5287       1.1  christos /* Parameters:  data(IO) - ioctl data pointer to ipfunc_resolve_t struct    */
   5288       1.1  christos /*                                                                          */
   5289       1.1  christos /* Copy in a ipfunc_resolve_t structure and then fill in the missing field. */
   5290       1.1  christos /* This will either be the function name (if the pointer is set) or the     */
   5291       1.1  christos /* function pointer if the name is set.  When found, fill in the other one  */
   5292       1.1  christos /* so that the entire, complete, structure can be copied back to user space.*/
   5293       1.1  christos /* ------------------------------------------------------------------------ */
   5294       1.1  christos int
   5295       1.2  christos ipf_resolvefunc(ipf_main_softc_t *softc, void *data)
   5296       1.1  christos {
   5297       1.1  christos 	ipfunc_resolve_t res, *ft;
   5298       1.1  christos 	int error;
   5299       1.1  christos 
   5300       1.1  christos 	error = BCOPYIN(data, &res, sizeof(res));
   5301       1.1  christos 	if (error != 0) {
   5302       1.1  christos 		IPFERROR(123);
   5303       1.1  christos 		return EFAULT;
   5304       1.1  christos 	}
   5305       1.1  christos 
   5306       1.1  christos 	if (res.ipfu_addr == NULL && res.ipfu_name[0] != '\0') {
   5307       1.1  christos 		for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
   5308       1.1  christos 			if (strncmp(res.ipfu_name, ft->ipfu_name,
   5309       1.1  christos 				    sizeof(res.ipfu_name)) == 0) {
   5310       1.1  christos 				res.ipfu_addr = ft->ipfu_addr;
   5311       1.1  christos 				res.ipfu_init = ft->ipfu_init;
   5312       1.1  christos 				if (COPYOUT(&res, data, sizeof(res)) != 0) {
   5313       1.1  christos 					IPFERROR(35);
   5314       1.1  christos 					return EFAULT;
   5315       1.1  christos 				}
   5316       1.1  christos 				return 0;
   5317       1.1  christos 			}
   5318       1.1  christos 	}
   5319       1.1  christos 	if (res.ipfu_addr != NULL && res.ipfu_name[0] == '\0') {
   5320       1.1  christos 		for (ft = ipf_availfuncs; ft->ipfu_addr != NULL; ft++)
   5321       1.1  christos 			if (ft->ipfu_addr == res.ipfu_addr) {
   5322       1.1  christos 				(void) strncpy(res.ipfu_name, ft->ipfu_name,
   5323       1.1  christos 					       sizeof(res.ipfu_name));
   5324       1.1  christos 				res.ipfu_init = ft->ipfu_init;
   5325       1.1  christos 				if (COPYOUT(&res, data, sizeof(res)) != 0) {
   5326       1.1  christos 					IPFERROR(36);
   5327       1.1  christos 					return EFAULT;
   5328       1.1  christos 				}
   5329       1.1  christos 				return 0;
   5330       1.1  christos 			}
   5331       1.1  christos 	}
   5332       1.1  christos 	IPFERROR(37);
   5333       1.1  christos 	return ESRCH;
   5334       1.1  christos }
   5335       1.1  christos 
   5336       1.1  christos 
   5337       1.1  christos #if !defined(_KERNEL) || (!defined(__NetBSD__) && !defined(__OpenBSD__) && \
   5338       1.1  christos      !defined(__FreeBSD__)) || \
   5339       1.1  christos     FREEBSD_LT_REV(501000) || NETBSD_LT_REV(105000000) || \
   5340       1.1  christos     OPENBSD_LT_REV(200006)
   5341       1.1  christos /*
   5342       1.1  christos  * From: NetBSD
   5343       1.1  christos  * ppsratecheck(): packets (or events) per second limitation.
   5344       1.1  christos  */
   5345       1.1  christos int
   5346       1.1  christos ppsratecheck(lasttime, curpps, maxpps)
   5347       1.1  christos 	struct timeval *lasttime;
   5348       1.1  christos 	int *curpps;
   5349       1.1  christos 	int maxpps;	/* maximum pps allowed */
   5350       1.1  christos {
   5351       1.1  christos 	struct timeval tv, delta;
   5352       1.1  christos 	int rv;
   5353       1.1  christos 
   5354       1.1  christos 	GETKTIME(&tv);
   5355       1.1  christos 
   5356       1.1  christos 	delta.tv_sec = tv.tv_sec - lasttime->tv_sec;
   5357       1.1  christos 	delta.tv_usec = tv.tv_usec - lasttime->tv_usec;
   5358       1.1  christos 	if (delta.tv_usec < 0) {
   5359       1.1  christos 		delta.tv_sec--;
   5360       1.1  christos 		delta.tv_usec += 1000000;
   5361       1.1  christos 	}
   5362       1.1  christos 
   5363       1.1  christos 	/*
   5364       1.1  christos 	 * check for 0,0 is so that the message will be seen at least once.
   5365       1.1  christos 	 * if more than one second have passed since the last update of
   5366       1.1  christos 	 * lasttime, reset the counter.
   5367       1.1  christos 	 *
   5368       1.1  christos 	 * we do increment *curpps even in *curpps < maxpps case, as some may
   5369       1.1  christos 	 * try to use *curpps for stat purposes as well.
   5370       1.1  christos 	 */
   5371       1.1  christos 	if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) ||
   5372       1.1  christos 	    delta.tv_sec >= 1) {
   5373       1.1  christos 		*lasttime = tv;
   5374       1.1  christos 		*curpps = 0;
   5375       1.1  christos 		rv = 1;
   5376       1.1  christos 	} else if (maxpps < 0)
   5377       1.1  christos 		rv = 1;
   5378       1.1  christos 	else if (*curpps < maxpps)
   5379       1.1  christos 		rv = 1;
   5380       1.1  christos 	else
   5381       1.1  christos 		rv = 0;
   5382       1.1  christos 	*curpps = *curpps + 1;
   5383       1.1  christos 
   5384       1.1  christos 	return (rv);
   5385       1.1  christos }
   5386       1.1  christos #endif
   5387       1.1  christos 
   5388       1.1  christos 
   5389       1.1  christos /* ------------------------------------------------------------------------ */
   5390       1.1  christos /* Function:    ipf_derefrule                                               */
   5391       1.1  christos /* Returns:     int   - 0 == rule freed up, else rule not freed             */
   5392       1.1  christos /* Parameters:  fr(I) - pointer to filter rule                              */
   5393       1.1  christos /*                                                                          */
   5394       1.1  christos /* Decrement the reference counter to a rule by one.  If it reaches zero,   */
   5395       1.1  christos /* free it and any associated storage space being used by it.               */
   5396       1.1  christos /* ------------------------------------------------------------------------ */
   5397       1.1  christos int
   5398       1.2  christos ipf_derefrule(ipf_main_softc_t *softc, frentry_t **frp)
   5399       1.1  christos {
   5400       1.1  christos 	frentry_t *fr;
   5401       1.1  christos 	frdest_t *fdp;
   5402       1.1  christos 
   5403       1.1  christos 	fr = *frp;
   5404       1.1  christos 	*frp = NULL;
   5405       1.1  christos 
   5406       1.1  christos 	MUTEX_ENTER(&fr->fr_lock);
   5407       1.1  christos 	fr->fr_ref--;
   5408       1.1  christos 	if (fr->fr_ref == 0) {
   5409       1.1  christos 		MUTEX_EXIT(&fr->fr_lock);
   5410       1.1  christos 		MUTEX_DESTROY(&fr->fr_lock);
   5411       1.1  christos 
   5412       1.1  christos 		ipf_funcfini(softc, fr);
   5413       1.1  christos 
   5414       1.1  christos 		fdp = &fr->fr_tif;
   5415       1.1  christos 		if (fdp->fd_type == FRD_DSTLIST)
   5416       1.1  christos 			ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
   5417       1.1  christos 
   5418       1.1  christos 		fdp = &fr->fr_rif;
   5419       1.1  christos 		if (fdp->fd_type == FRD_DSTLIST)
   5420       1.1  christos 			ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
   5421       1.1  christos 
   5422       1.1  christos 		fdp = &fr->fr_dif;
   5423       1.1  christos 		if (fdp->fd_type == FRD_DSTLIST)
   5424       1.1  christos 			ipf_lookup_deref(softc, IPLT_DSTLIST, fdp->fd_ptr);
   5425       1.1  christos 
   5426       1.1  christos 		if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF &&
   5427       1.1  christos 		    fr->fr_satype == FRI_LOOKUP)
   5428       1.1  christos 			ipf_lookup_deref(softc, fr->fr_srctype, fr->fr_srcptr);
   5429       1.1  christos 		if ((fr->fr_type & ~FR_T_BUILTIN) == FR_T_IPF &&
   5430       1.1  christos 		    fr->fr_datype == FRI_LOOKUP)
   5431       1.1  christos 			ipf_lookup_deref(softc, fr->fr_dsttype, fr->fr_dstptr);
   5432       1.1  christos 
   5433       1.3   darrenr 		if (fr->fr_grp != NULL)
   5434       1.3   darrenr 			ipf_group_del(softc, fr->fr_grp, fr);
   5435       1.3   darrenr 
   5436       1.3   darrenr 		if (fr->fr_grphead != NULL)
   5437       1.3   darrenr 			ipf_group_del(softc, fr->fr_grphead, fr);
   5438       1.3   darrenr 
   5439       1.3   darrenr 		if (fr->fr_icmpgrp != NULL)
   5440       1.3   darrenr 			ipf_group_del(softc, fr->fr_icmpgrp, fr);
   5441       1.3   darrenr 
   5442       1.1  christos 		if ((fr->fr_flags & FR_COPIED) != 0) {
   5443       1.1  christos 			if (fr->fr_dsize) {
   5444       1.1  christos 				KFREES(fr->fr_data, fr->fr_dsize);
   5445       1.1  christos 			}
   5446       1.1  christos 			KFREES(fr, fr->fr_size);
   5447       1.1  christos 			return 0;
   5448       1.1  christos 		}
   5449       1.1  christos 		return 1;
   5450       1.1  christos 	} else {
   5451       1.1  christos 		MUTEX_EXIT(&fr->fr_lock);
   5452       1.1  christos 	}
   5453       1.1  christos 	return -1;
   5454       1.1  christos }
   5455       1.1  christos 
   5456       1.1  christos 
   5457       1.1  christos /* ------------------------------------------------------------------------ */
   5458       1.1  christos /* Function:    ipf_grpmapinit                                              */
   5459       1.1  christos /* Returns:     int - 0 == success, else ESRCH because table entry not found*/
   5460       1.1  christos /* Parameters:  fr(I) - pointer to rule to find hash table for              */
   5461       1.1  christos /*                                                                          */
   5462       1.1  christos /* Looks for group hash table fr_arg and stores a pointer to it in fr_ptr.  */
   5463       1.1  christos /* fr_ptr is later used by ipf_srcgrpmap and ipf_dstgrpmap.                 */
   5464       1.1  christos /* ------------------------------------------------------------------------ */
   5465       1.1  christos static int
   5466       1.2  christos ipf_grpmapinit(ipf_main_softc_t *softc, frentry_t *fr)
   5467       1.1  christos {
   5468       1.1  christos 	char name[FR_GROUPLEN];
   5469       1.1  christos 	iphtable_t *iph;
   5470       1.1  christos 
   5471      1.14  christos 	(void) snprintf(name, sizeof(name), "%d", fr->fr_arg);
   5472       1.1  christos 	iph = ipf_lookup_find_htable(softc, IPL_LOGIPF, name);
   5473       1.1  christos 	if (iph == NULL) {
   5474       1.1  christos 		IPFERROR(38);
   5475       1.1  christos 		return ESRCH;
   5476       1.1  christos 	}
   5477       1.1  christos 	if ((iph->iph_flags & FR_INOUT) != (fr->fr_flags & FR_INOUT)) {
   5478       1.1  christos 		IPFERROR(39);
   5479       1.1  christos 		return ESRCH;
   5480       1.1  christos 	}
   5481       1.1  christos 	iph->iph_ref++;
   5482       1.1  christos 	fr->fr_ptr = iph;
   5483       1.1  christos 	return 0;
   5484       1.1  christos }
   5485       1.1  christos 
   5486       1.1  christos 
   5487       1.1  christos /* ------------------------------------------------------------------------ */
   5488       1.1  christos /* Function:    ipf_grpmapfini                                              */
   5489       1.1  christos /* Returns:     int - 0 == success, else ESRCH because table entry not found*/
   5490       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   5491       1.1  christos /*              fr(I)    - pointer to rule to release hash table for        */
   5492       1.1  christos /*                                                                          */
   5493       1.1  christos /* For rules that have had ipf_grpmapinit called, ipf_lookup_deref needs to */
   5494       1.1  christos /* be called to undo what ipf_grpmapinit caused to be done.                 */
   5495       1.1  christos /* ------------------------------------------------------------------------ */
   5496       1.1  christos static int
   5497       1.2  christos ipf_grpmapfini(ipf_main_softc_t *softc, frentry_t *fr)
   5498       1.1  christos {
   5499       1.1  christos 	iphtable_t *iph;
   5500       1.1  christos 	iph = fr->fr_ptr;
   5501       1.1  christos 	if (iph != NULL)
   5502       1.1  christos 		ipf_lookup_deref(softc, IPLT_HASH, iph);
   5503       1.1  christos 	return 0;
   5504       1.1  christos }
   5505       1.1  christos 
   5506       1.1  christos 
   5507       1.1  christos /* ------------------------------------------------------------------------ */
   5508       1.1  christos /* Function:    ipf_srcgrpmap                                               */
   5509       1.1  christos /* Returns:     frentry_t * - pointer to "new last matching" rule or NULL   */
   5510       1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   5511       1.1  christos /*              passp(IO) - pointer to current/new filter decision (unused) */
   5512       1.1  christos /*                                                                          */
   5513       1.1  christos /* Look for a rule group head in a hash table, using the source address as  */
   5514       1.1  christos /* the key, and descend into that group and continue matching rules against */
   5515       1.1  christos /* the packet.                                                              */
   5516       1.1  christos /* ------------------------------------------------------------------------ */
   5517       1.1  christos frentry_t *
   5518       1.2  christos ipf_srcgrpmap(fr_info_t *fin, u_32_t *passp)
   5519       1.1  christos {
   5520       1.1  christos 	frgroup_t *fg;
   5521       1.1  christos 	void *rval;
   5522       1.1  christos 
   5523       1.1  christos 	rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr,
   5524       1.1  christos 				 &fin->fin_src);
   5525       1.1  christos 	if (rval == NULL)
   5526       1.1  christos 		return NULL;
   5527       1.1  christos 
   5528       1.1  christos 	fg = rval;
   5529       1.1  christos 	fin->fin_fr = fg->fg_start;
   5530       1.1  christos 	(void) ipf_scanlist(fin, *passp);
   5531       1.1  christos 	return fin->fin_fr;
   5532       1.1  christos }
   5533       1.1  christos 
   5534       1.1  christos 
   5535       1.1  christos /* ------------------------------------------------------------------------ */
   5536       1.1  christos /* Function:    ipf_dstgrpmap                                               */
   5537       1.1  christos /* Returns:     frentry_t * - pointer to "new last matching" rule or NULL   */
   5538       1.1  christos /* Parameters:  fin(I)    - pointer to packet information                   */
   5539       1.1  christos /*              passp(IO) - pointer to current/new filter decision (unused) */
   5540       1.1  christos /*                                                                          */
   5541       1.1  christos /* Look for a rule group head in a hash table, using the destination        */
   5542       1.1  christos /* address as the key, and descend into that group and continue matching    */
   5543       1.1  christos /* rules against  the packet.                                               */
   5544       1.1  christos /* ------------------------------------------------------------------------ */
   5545       1.1  christos frentry_t *
   5546       1.2  christos ipf_dstgrpmap(fr_info_t *fin, u_32_t *passp)
   5547       1.1  christos {
   5548       1.1  christos 	frgroup_t *fg;
   5549       1.1  christos 	void *rval;
   5550       1.1  christos 
   5551       1.1  christos 	rval = ipf_iphmfindgroup(fin->fin_main_soft, fin->fin_fr->fr_ptr,
   5552       1.1  christos 				 &fin->fin_dst);
   5553       1.1  christos 	if (rval == NULL)
   5554       1.1  christos 		return NULL;
   5555       1.1  christos 
   5556       1.1  christos 	fg = rval;
   5557       1.1  christos 	fin->fin_fr = fg->fg_start;
   5558       1.1  christos 	(void) ipf_scanlist(fin, *passp);
   5559       1.1  christos 	return fin->fin_fr;
   5560       1.1  christos }
   5561       1.1  christos 
   5562       1.1  christos /*
   5563       1.1  christos  * Queue functions
   5564       1.1  christos  * ===============
   5565       1.1  christos  * These functions manage objects on queues for efficient timeouts.  There
   5566       1.1  christos  * are a number of system defined queues as well as user defined timeouts.
   5567       1.1  christos  * It is expected that a lock is held in the domain in which the queue
   5568       1.1  christos  * belongs (i.e. either state or NAT) when calling any of these functions
   5569       1.1  christos  * that prevents ipf_freetimeoutqueue() from being called at the same time
   5570       1.1  christos  * as any other.
   5571       1.1  christos  */
   5572       1.1  christos 
   5573       1.1  christos 
   5574       1.1  christos /* ------------------------------------------------------------------------ */
   5575       1.1  christos /* Function:    ipf_addtimeoutqueue                                         */
   5576       1.1  christos /* Returns:     struct ifqtq * - NULL if malloc fails, else pointer to      */
   5577       1.1  christos /*                               timeout queue with given interval.         */
   5578       1.1  christos /* Parameters:  parent(I)  - pointer to pointer to parent node of this list */
   5579       1.1  christos /*                           of interface queues.                           */
   5580       1.1  christos /*              seconds(I) - timeout value in seconds for this queue.       */
   5581       1.1  christos /*                                                                          */
   5582       1.1  christos /* This routine first looks for a timeout queue that matches the interval   */
   5583       1.1  christos /* being requested.  If it finds one, increments the reference counter and  */
   5584       1.1  christos /* returns a pointer to it.  If none are found, it allocates a new one and  */
   5585       1.1  christos /* inserts it at the top of the list.                                       */
   5586       1.1  christos /*                                                                          */
   5587       1.1  christos /* Locking.                                                                 */
   5588       1.1  christos /* It is assumed that the caller of this function has an appropriate lock   */
   5589       1.1  christos /* held (exclusively) in the domain that encompases 'parent'.               */
   5590       1.1  christos /* ------------------------------------------------------------------------ */
   5591       1.1  christos ipftq_t *
   5592       1.2  christos ipf_addtimeoutqueue(ipf_main_softc_t *softc, ipftq_t **parent, u_int seconds)
   5593       1.1  christos {
   5594       1.1  christos 	ipftq_t *ifq;
   5595       1.1  christos 	u_int period;
   5596       1.1  christos 
   5597       1.1  christos 	period = seconds * IPF_HZ_DIVIDE;
   5598       1.1  christos 
   5599       1.1  christos 	MUTEX_ENTER(&softc->ipf_timeoutlock);
   5600       1.1  christos 	for (ifq = *parent; ifq != NULL; ifq = ifq->ifq_next) {
   5601       1.1  christos 		if (ifq->ifq_ttl == period) {
   5602       1.1  christos 			/*
   5603       1.1  christos 			 * Reset the delete flag, if set, so the structure
   5604       1.1  christos 			 * gets reused rather than freed and reallocated.
   5605       1.1  christos 			 */
   5606       1.1  christos 			MUTEX_ENTER(&ifq->ifq_lock);
   5607       1.1  christos 			ifq->ifq_flags &= ~IFQF_DELETE;
   5608       1.1  christos 			ifq->ifq_ref++;
   5609       1.1  christos 			MUTEX_EXIT(&ifq->ifq_lock);
   5610       1.1  christos 			MUTEX_EXIT(&softc->ipf_timeoutlock);
   5611       1.1  christos 
   5612       1.1  christos 			return ifq;
   5613       1.1  christos 		}
   5614       1.1  christos 	}
   5615       1.1  christos 
   5616       1.1  christos 	KMALLOC(ifq, ipftq_t *);
   5617       1.1  christos 	if (ifq != NULL) {
   5618       1.1  christos 		MUTEX_NUKE(&ifq->ifq_lock);
   5619       1.1  christos 		IPFTQ_INIT(ifq, period, "ipftq mutex");
   5620       1.1  christos 		ifq->ifq_next = *parent;
   5621       1.1  christos 		ifq->ifq_pnext = parent;
   5622       1.1  christos 		ifq->ifq_flags = IFQF_USER;
   5623       1.1  christos 		ifq->ifq_ref++;
   5624       1.1  christos 		*parent = ifq;
   5625       1.1  christos 		softc->ipf_userifqs++;
   5626       1.1  christos 	}
   5627       1.1  christos 	MUTEX_EXIT(&softc->ipf_timeoutlock);
   5628       1.1  christos 	return ifq;
   5629       1.1  christos }
   5630       1.1  christos 
   5631       1.1  christos 
   5632       1.1  christos /* ------------------------------------------------------------------------ */
   5633       1.1  christos /* Function:    ipf_deletetimeoutqueue                                      */
   5634       1.1  christos /* Returns:     int    - new reference count value of the timeout queue     */
   5635       1.1  christos /* Parameters:  ifq(I) - timeout queue which is losing a reference.         */
   5636       1.1  christos /* Locks:       ifq->ifq_lock                                               */
   5637       1.1  christos /*                                                                          */
   5638       1.1  christos /* This routine must be called when we're discarding a pointer to a timeout */
   5639       1.1  christos /* queue object, taking care of the reference counter.                      */
   5640       1.1  christos /*                                                                          */
   5641       1.1  christos /* Now that this just sets a DELETE flag, it requires the expire code to    */
   5642       1.1  christos /* check the list of user defined timeout queues and call the free function */
   5643       1.1  christos /* below (currently commented out) to stop memory leaking.  It is done this */
   5644       1.1  christos /* way because the locking may not be sufficient to safely do a free when   */
   5645       1.1  christos /* this function is called.                                                 */
   5646       1.1  christos /* ------------------------------------------------------------------------ */
   5647       1.1  christos int
   5648       1.2  christos ipf_deletetimeoutqueue(ipftq_t *ifq)
   5649       1.1  christos {
   5650       1.1  christos 
   5651       1.1  christos 	ifq->ifq_ref--;
   5652       1.1  christos 	if ((ifq->ifq_ref == 0) && ((ifq->ifq_flags & IFQF_USER) != 0)) {
   5653       1.1  christos 		ifq->ifq_flags |= IFQF_DELETE;
   5654       1.1  christos 	}
   5655       1.1  christos 
   5656       1.1  christos 	return ifq->ifq_ref;
   5657       1.1  christos }
   5658       1.1  christos 
   5659       1.1  christos 
   5660       1.1  christos /* ------------------------------------------------------------------------ */
   5661       1.1  christos /* Function:    ipf_freetimeoutqueue                                        */
   5662       1.1  christos /* Parameters:  ifq(I) - timeout queue which is losing a reference.         */
   5663       1.1  christos /* Returns:     Nil                                                         */
   5664       1.1  christos /*                                                                          */
   5665       1.1  christos /* Locking:                                                                 */
   5666       1.1  christos /* It is assumed that the caller of this function has an appropriate lock   */
   5667       1.1  christos /* held (exclusively) in the domain that encompases the callers "domain".   */
   5668       1.1  christos /* The ifq_lock for this structure should not be held.                      */
   5669       1.1  christos /*                                                                          */
   5670       1.1  christos /* Remove a user defined timeout queue from the list of queues it is in and */
   5671       1.1  christos /* tidy up after this is done.                                              */
   5672       1.1  christos /* ------------------------------------------------------------------------ */
   5673       1.1  christos void
   5674       1.2  christos ipf_freetimeoutqueue(ipf_main_softc_t *softc, ipftq_t *ifq)
   5675       1.1  christos {
   5676       1.1  christos 
   5677       1.1  christos 	if (((ifq->ifq_flags & IFQF_DELETE) == 0) || (ifq->ifq_ref != 0) ||
   5678       1.1  christos 	    ((ifq->ifq_flags & IFQF_USER) == 0)) {
   5679       1.1  christos 		printf("ipf_freetimeoutqueue(%lx) flags 0x%x ttl %d ref %d\n",
   5680       1.1  christos 		       (u_long)ifq, ifq->ifq_flags, ifq->ifq_ttl,
   5681       1.1  christos 		       ifq->ifq_ref);
   5682       1.1  christos 		return;
   5683       1.1  christos 	}
   5684       1.1  christos 
   5685       1.1  christos 	/*
   5686       1.1  christos 	 * Remove from its position in the list.
   5687       1.1  christos 	 */
   5688       1.1  christos 	*ifq->ifq_pnext = ifq->ifq_next;
   5689       1.1  christos 	if (ifq->ifq_next != NULL)
   5690       1.1  christos 		ifq->ifq_next->ifq_pnext = ifq->ifq_pnext;
   5691       1.1  christos 	ifq->ifq_next = NULL;
   5692       1.1  christos 	ifq->ifq_pnext = NULL;
   5693       1.1  christos 
   5694       1.1  christos 	MUTEX_DESTROY(&ifq->ifq_lock);
   5695       1.1  christos 	ATOMIC_DEC(softc->ipf_userifqs);
   5696       1.1  christos 	KFREE(ifq);
   5697       1.1  christos }
   5698       1.1  christos 
   5699       1.1  christos 
   5700       1.1  christos /* ------------------------------------------------------------------------ */
   5701       1.1  christos /* Function:    ipf_deletequeueentry                                        */
   5702       1.1  christos /* Returns:     Nil                                                         */
   5703       1.1  christos /* Parameters:  tqe(I) - timeout queue entry to delete                      */
   5704       1.1  christos /*                                                                          */
   5705       1.1  christos /* Remove a tail queue entry from its queue and make it an orphan.          */
   5706       1.1  christos /* ipf_deletetimeoutqueue is called to make sure the reference count on the */
   5707       1.1  christos /* queue is correct.  We can't, however, call ipf_freetimeoutqueue because  */
   5708       1.1  christos /* the correct lock(s) may not be held that would make it safe to do so.    */
   5709       1.1  christos /* ------------------------------------------------------------------------ */
   5710       1.1  christos void
   5711       1.2  christos ipf_deletequeueentry(ipftqent_t *tqe)
   5712       1.1  christos {
   5713       1.1  christos 	ipftq_t *ifq;
   5714       1.1  christos 
   5715       1.1  christos 	ifq = tqe->tqe_ifq;
   5716       1.1  christos 
   5717       1.1  christos 	MUTEX_ENTER(&ifq->ifq_lock);
   5718       1.1  christos 
   5719       1.1  christos 	if (tqe->tqe_pnext != NULL) {
   5720       1.1  christos 		*tqe->tqe_pnext = tqe->tqe_next;
   5721       1.1  christos 		if (tqe->tqe_next != NULL)
   5722       1.1  christos 			tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
   5723       1.1  christos 		else    /* we must be the tail anyway */
   5724       1.1  christos 			ifq->ifq_tail = tqe->tqe_pnext;
   5725       1.1  christos 
   5726       1.1  christos 		tqe->tqe_pnext = NULL;
   5727       1.1  christos 		tqe->tqe_ifq = NULL;
   5728       1.1  christos 	}
   5729       1.1  christos 
   5730       1.1  christos 	(void) ipf_deletetimeoutqueue(ifq);
   5731       1.1  christos 	ASSERT(ifq->ifq_ref > 0);
   5732       1.1  christos 
   5733       1.1  christos 	MUTEX_EXIT(&ifq->ifq_lock);
   5734       1.1  christos }
   5735       1.1  christos 
   5736       1.1  christos 
   5737       1.1  christos /* ------------------------------------------------------------------------ */
   5738       1.1  christos /* Function:    ipf_queuefront                                              */
   5739       1.1  christos /* Returns:     Nil                                                         */
   5740       1.1  christos /* Parameters:  tqe(I) - pointer to timeout queue entry                     */
   5741       1.1  christos /*                                                                          */
   5742       1.1  christos /* Move a queue entry to the front of the queue, if it isn't already there. */
   5743       1.1  christos /* ------------------------------------------------------------------------ */
   5744       1.1  christos void
   5745       1.2  christos ipf_queuefront(ipftqent_t *tqe)
   5746       1.1  christos {
   5747       1.1  christos 	ipftq_t *ifq;
   5748       1.1  christos 
   5749       1.1  christos 	ifq = tqe->tqe_ifq;
   5750       1.1  christos 	if (ifq == NULL)
   5751       1.1  christos 		return;
   5752       1.1  christos 
   5753       1.1  christos 	MUTEX_ENTER(&ifq->ifq_lock);
   5754       1.1  christos 	if (ifq->ifq_head != tqe) {
   5755       1.1  christos 		*tqe->tqe_pnext = tqe->tqe_next;
   5756       1.1  christos 		if (tqe->tqe_next)
   5757       1.1  christos 			tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
   5758       1.1  christos 		else
   5759       1.1  christos 			ifq->ifq_tail = tqe->tqe_pnext;
   5760       1.1  christos 
   5761       1.1  christos 		tqe->tqe_next = ifq->ifq_head;
   5762       1.1  christos 		ifq->ifq_head->tqe_pnext = &tqe->tqe_next;
   5763       1.1  christos 		ifq->ifq_head = tqe;
   5764       1.1  christos 		tqe->tqe_pnext = &ifq->ifq_head;
   5765       1.1  christos 	}
   5766       1.1  christos 	MUTEX_EXIT(&ifq->ifq_lock);
   5767       1.1  christos }
   5768       1.1  christos 
   5769       1.1  christos 
   5770       1.1  christos /* ------------------------------------------------------------------------ */
   5771       1.1  christos /* Function:    ipf_queueback                                               */
   5772       1.1  christos /* Returns:     Nil                                                         */
   5773       1.1  christos /* Parameters:  ticks(I) - ipf tick time to use with this call              */
   5774       1.1  christos /*              tqe(I)   - pointer to timeout queue entry                   */
   5775       1.1  christos /*                                                                          */
   5776       1.1  christos /* Move a queue entry to the back of the queue, if it isn't already there.  */
   5777       1.1  christos /* We use use ticks to calculate the expiration and mark for when we last   */
   5778       1.1  christos /* touched the structure.                                                   */
   5779       1.1  christos /* ------------------------------------------------------------------------ */
   5780       1.1  christos void
   5781       1.2  christos ipf_queueback(u_long ticks, ipftqent_t *tqe)
   5782       1.1  christos {
   5783       1.1  christos 	ipftq_t *ifq;
   5784       1.1  christos 
   5785       1.1  christos 	ifq = tqe->tqe_ifq;
   5786       1.1  christos 	if (ifq == NULL)
   5787       1.1  christos 		return;
   5788       1.1  christos 	tqe->tqe_die = ticks + ifq->ifq_ttl;
   5789       1.1  christos 	tqe->tqe_touched = ticks;
   5790       1.1  christos 
   5791       1.1  christos 	MUTEX_ENTER(&ifq->ifq_lock);
   5792       1.1  christos 	if (tqe->tqe_next != NULL) {		/* at the end already ? */
   5793       1.1  christos 		/*
   5794       1.1  christos 		 * Remove from list
   5795       1.1  christos 		 */
   5796       1.1  christos 		*tqe->tqe_pnext = tqe->tqe_next;
   5797       1.1  christos 		tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
   5798       1.1  christos 
   5799       1.1  christos 		/*
   5800       1.1  christos 		 * Make it the last entry.
   5801       1.1  christos 		 */
   5802       1.1  christos 		tqe->tqe_next = NULL;
   5803       1.1  christos 		tqe->tqe_pnext = ifq->ifq_tail;
   5804       1.1  christos 		*ifq->ifq_tail = tqe;
   5805       1.1  christos 		ifq->ifq_tail = &tqe->tqe_next;
   5806       1.1  christos 	}
   5807       1.1  christos 	MUTEX_EXIT(&ifq->ifq_lock);
   5808       1.1  christos }
   5809       1.1  christos 
   5810       1.1  christos 
   5811       1.1  christos /* ------------------------------------------------------------------------ */
   5812       1.1  christos /* Function:    ipf_queueappend                                             */
   5813       1.1  christos /* Returns:     Nil                                                         */
   5814       1.1  christos /* Parameters:  ticks(I)  - ipf tick time to use with this call             */
   5815       1.1  christos /*              tqe(I)    - pointer to timeout queue entry                  */
   5816       1.1  christos /*              ifq(I)    - pointer to timeout queue                        */
   5817       1.1  christos /*              parent(I) - owing object pointer                            */
   5818       1.1  christos /*                                                                          */
   5819       1.1  christos /* Add a new item to this queue and put it on the very end.                 */
   5820       1.1  christos /* We use use ticks to calculate the expiration and mark for when we last   */
   5821       1.1  christos /* touched the structure.                                                   */
   5822       1.1  christos /* ------------------------------------------------------------------------ */
   5823       1.1  christos void
   5824       1.2  christos ipf_queueappend(u_long ticks, ipftqent_t *tqe, ipftq_t *ifq, void *parent)
   5825       1.1  christos {
   5826       1.1  christos 
   5827       1.1  christos 	MUTEX_ENTER(&ifq->ifq_lock);
   5828       1.1  christos 	tqe->tqe_parent = parent;
   5829       1.1  christos 	tqe->tqe_pnext = ifq->ifq_tail;
   5830       1.1  christos 	*ifq->ifq_tail = tqe;
   5831       1.1  christos 	ifq->ifq_tail = &tqe->tqe_next;
   5832       1.1  christos 	tqe->tqe_next = NULL;
   5833       1.1  christos 	tqe->tqe_ifq = ifq;
   5834       1.1  christos 	tqe->tqe_die = ticks + ifq->ifq_ttl;
   5835       1.1  christos 	tqe->tqe_touched = ticks;
   5836       1.1  christos 	ifq->ifq_ref++;
   5837       1.1  christos 	MUTEX_EXIT(&ifq->ifq_lock);
   5838       1.1  christos }
   5839       1.1  christos 
   5840       1.1  christos 
   5841       1.1  christos /* ------------------------------------------------------------------------ */
   5842       1.1  christos /* Function:    ipf_movequeue                                               */
   5843       1.1  christos /* Returns:     Nil                                                         */
   5844       1.1  christos /* Parameters:  tq(I)   - pointer to timeout queue information              */
   5845       1.1  christos /*              oifp(I) - old timeout queue entry was on                    */
   5846       1.1  christos /*              nifp(I) - new timeout queue to put entry on                 */
   5847       1.1  christos /*                                                                          */
   5848       1.1  christos /* Move a queue entry from one timeout queue to another timeout queue.      */
   5849       1.1  christos /* If it notices that the current entry is already last and does not need   */
   5850       1.1  christos /* to move queue, the return.                                               */
   5851       1.1  christos /* ------------------------------------------------------------------------ */
   5852       1.1  christos void
   5853       1.2  christos ipf_movequeue(u_long ticks, ipftqent_t *tqe, ipftq_t *oifq, ipftq_t *nifq)
   5854       1.1  christos {
   5855       1.1  christos 
   5856       1.1  christos 	/*
   5857       1.1  christos 	 * If the queue hasn't changed and we last touched this entry at the
   5858       1.1  christos 	 * same ipf time, then we're not going to achieve anything by either
   5859       1.1  christos 	 * changing the ttl or moving it on the queue.
   5860       1.1  christos 	 */
   5861       1.1  christos 	if (oifq == nifq && tqe->tqe_touched == ticks)
   5862       1.1  christos 		return;
   5863       1.1  christos 
   5864       1.1  christos 	/*
   5865       1.1  christos 	 * For any of this to be outside the lock, there is a risk that two
   5866       1.1  christos 	 * packets entering simultaneously, with one changing to a different
   5867       1.1  christos 	 * queue and one not, could end up with things in a bizarre state.
   5868       1.1  christos 	 */
   5869       1.1  christos 	MUTEX_ENTER(&oifq->ifq_lock);
   5870       1.1  christos 
   5871       1.1  christos 	tqe->tqe_touched = ticks;
   5872       1.1  christos 	tqe->tqe_die = ticks + nifq->ifq_ttl;
   5873       1.1  christos 	/*
   5874       1.1  christos 	 * Is the operation here going to be a no-op ?
   5875       1.1  christos 	 */
   5876       1.1  christos 	if (oifq == nifq) {
   5877       1.1  christos 		if ((tqe->tqe_next == NULL) ||
   5878       1.1  christos 		    (tqe->tqe_next->tqe_die == tqe->tqe_die)) {
   5879       1.1  christos 			MUTEX_EXIT(&oifq->ifq_lock);
   5880       1.1  christos 			return;
   5881       1.1  christos 		}
   5882       1.1  christos 	}
   5883       1.1  christos 
   5884       1.1  christos 	/*
   5885       1.1  christos 	 * Remove from the old queue
   5886       1.1  christos 	 */
   5887       1.1  christos 	*tqe->tqe_pnext = tqe->tqe_next;
   5888       1.1  christos 	if (tqe->tqe_next)
   5889       1.1  christos 		tqe->tqe_next->tqe_pnext = tqe->tqe_pnext;
   5890       1.1  christos 	else
   5891       1.1  christos 		oifq->ifq_tail = tqe->tqe_pnext;
   5892       1.1  christos 	tqe->tqe_next = NULL;
   5893       1.1  christos 
   5894       1.1  christos 	/*
   5895       1.1  christos 	 * If we're moving from one queue to another, release the
   5896       1.1  christos 	 * lock on the old queue and get a lock on the new queue.
   5897       1.1  christos 	 * For user defined queues, if we're moving off it, call
   5898       1.1  christos 	 * delete in case it can now be freed.
   5899       1.1  christos 	 */
   5900       1.1  christos 	if (oifq != nifq) {
   5901       1.1  christos 		tqe->tqe_ifq = NULL;
   5902       1.1  christos 
   5903       1.1  christos 		(void) ipf_deletetimeoutqueue(oifq);
   5904       1.1  christos 
   5905       1.1  christos 		MUTEX_EXIT(&oifq->ifq_lock);
   5906       1.1  christos 
   5907       1.1  christos 		MUTEX_ENTER(&nifq->ifq_lock);
   5908       1.1  christos 
   5909       1.1  christos 		tqe->tqe_ifq = nifq;
   5910       1.1  christos 		nifq->ifq_ref++;
   5911       1.1  christos 	}
   5912       1.1  christos 
   5913       1.1  christos 	/*
   5914       1.1  christos 	 * Add to the bottom of the new queue
   5915       1.1  christos 	 */
   5916       1.1  christos 	tqe->tqe_pnext = nifq->ifq_tail;
   5917       1.1  christos 	*nifq->ifq_tail = tqe;
   5918       1.1  christos 	nifq->ifq_tail = &tqe->tqe_next;
   5919       1.1  christos 	MUTEX_EXIT(&nifq->ifq_lock);
   5920       1.1  christos }
   5921       1.1  christos 
   5922       1.1  christos 
   5923       1.1  christos /* ------------------------------------------------------------------------ */
   5924       1.1  christos /* Function:    ipf_updateipid                                              */
   5925       1.1  christos /* Returns:     int - 0 == success, -1 == error (packet should be droppped) */
   5926       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   5927       1.1  christos /*                                                                          */
   5928       1.1  christos /* When we are doing NAT, change the IP of every packet to represent a      */
   5929       1.1  christos /* single sequence of packets coming from the host, hiding any host         */
   5930       1.1  christos /* specific sequencing that might otherwise be revealed.  If the packet is  */
   5931       1.1  christos /* a fragment, then store the 'new' IPid in the fragment cache and look up  */
   5932       1.1  christos /* the fragment cache for non-leading fragments.  If a non-leading fragment */
   5933       1.1  christos /* has no match in the cache, return an error.                              */
   5934       1.1  christos /* ------------------------------------------------------------------------ */
   5935       1.1  christos static int
   5936       1.2  christos ipf_updateipid(fr_info_t *fin)
   5937       1.1  christos {
   5938       1.1  christos 	u_short id, ido, sums;
   5939       1.1  christos 	u_32_t sumd, sum;
   5940       1.1  christos 	ip_t *ip;
   5941       1.1  christos 
   5942       1.1  christos 	if (fin->fin_off != 0) {
   5943       1.1  christos 		sum = ipf_frag_ipidknown(fin);
   5944       1.1  christos 		if (sum == 0xffffffff)
   5945       1.1  christos 			return -1;
   5946       1.1  christos 		sum &= 0xffff;
   5947       1.1  christos 		id = (u_short)sum;
   5948       1.1  christos 	} else {
   5949       1.1  christos 		id = ipf_nextipid(fin);
   5950       1.1  christos 		if (fin->fin_off == 0 && (fin->fin_flx & FI_FRAG) != 0)
   5951       1.1  christos 			(void) ipf_frag_ipidnew(fin, (u_32_t)id);
   5952       1.1  christos 	}
   5953       1.1  christos 
   5954       1.1  christos 	ip = fin->fin_ip;
   5955       1.1  christos 	ido = ntohs(ip->ip_id);
   5956       1.1  christos 	if (id == ido)
   5957       1.1  christos 		return 0;
   5958       1.1  christos 	ip->ip_id = htons(id);
   5959       1.1  christos 	CALC_SUMD(ido, id, sumd);	/* DESTRUCTIVE MACRO! id,ido change */
   5960       1.1  christos 	sum = (~ntohs(ip->ip_sum)) & 0xffff;
   5961       1.1  christos 	sum += sumd;
   5962       1.1  christos 	sum = (sum >> 16) + (sum & 0xffff);
   5963       1.1  christos 	sum = (sum >> 16) + (sum & 0xffff);
   5964       1.1  christos 	sums = ~(u_short)sum;
   5965       1.1  christos 	ip->ip_sum = htons(sums);
   5966       1.1  christos 	return 0;
   5967       1.1  christos }
   5968       1.1  christos 
   5969       1.1  christos 
   5970       1.1  christos #ifdef	NEED_FRGETIFNAME
   5971       1.1  christos /* ------------------------------------------------------------------------ */
   5972       1.1  christos /* Function:    ipf_getifname                                               */
   5973       1.1  christos /* Returns:     char *    - pointer to interface name                       */
   5974       1.1  christos /* Parameters:  ifp(I)    - pointer to network interface                    */
   5975       1.1  christos /*              buffer(O) - pointer to where to store interface name        */
   5976       1.1  christos /*                                                                          */
   5977       1.1  christos /* Constructs an interface name in the buffer passed.  The buffer passed is */
   5978       1.1  christos /* expected to be at least LIFNAMSIZ in bytes big.  If buffer is passed in  */
   5979       1.1  christos /* as a NULL pointer then return a pointer to a static array.               */
   5980       1.1  christos /* ------------------------------------------------------------------------ */
   5981       1.1  christos char *
   5982       1.1  christos ipf_getifname(ifp, buffer)
   5983       1.1  christos 	struct ifnet *ifp;
   5984       1.1  christos 	char *buffer;
   5985       1.1  christos {
   5986       1.1  christos 	static char namebuf[LIFNAMSIZ];
   5987       1.1  christos # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
   5988       1.1  christos      defined(__sgi) || defined(linux) || defined(_AIX51) || \
   5989       1.1  christos      (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
   5990       1.1  christos 	int unit, space;
   5991       1.1  christos 	char temp[20];
   5992       1.1  christos 	char *s;
   5993       1.1  christos # endif
   5994       1.1  christos 
   5995       1.1  christos 	if (buffer == NULL)
   5996       1.1  christos 		buffer = namebuf;
   5997       1.1  christos 	(void) strncpy(buffer, ifp->if_name, LIFNAMSIZ);
   5998       1.1  christos 	buffer[LIFNAMSIZ - 1] = '\0';
   5999       1.1  christos # if defined(MENTAT) || defined(__FreeBSD__) || defined(__osf__) || \
   6000       1.1  christos      defined(__sgi) || defined(_AIX51) || \
   6001       1.1  christos      (defined(sun) && !defined(__SVR4) && !defined(__svr4__))
   6002       1.1  christos 	for (s = buffer; *s; s++)
   6003       1.1  christos 		;
   6004       1.1  christos 	unit = ifp->if_unit;
   6005       1.1  christos 	space = LIFNAMSIZ - (s - buffer);
   6006       1.1  christos 	if ((space > 0) && (unit >= 0)) {
   6007      1.14  christos 		snprintf(temp, sizeof(temp), "%d", unit);
   6008       1.1  christos 		(void) strncpy(s, temp, space);
   6009      1.14  christos 		s[space - 1] = '\0';
   6010       1.1  christos 	}
   6011       1.1  christos # endif
   6012       1.1  christos 	return buffer;
   6013       1.1  christos }
   6014       1.1  christos #endif
   6015       1.1  christos 
   6016       1.1  christos 
   6017       1.1  christos /* ------------------------------------------------------------------------ */
   6018       1.1  christos /* Function:    ipf_ioctlswitch                                             */
   6019       1.1  christos /* Returns:     int     - -1 continue processing, else ioctl return value   */
   6020       1.1  christos /* Parameters:  unit(I) - device unit opened                                */
   6021       1.1  christos /*              data(I) - pointer to ioctl data                             */
   6022       1.1  christos /*              cmd(I)  - ioctl command                                     */
   6023       1.1  christos /*              mode(I) - mode value                                        */
   6024       1.1  christos /*              uid(I)  - uid making the ioctl call                         */
   6025       1.1  christos /*              ctx(I)  - pointer to context data                           */
   6026       1.1  christos /*                                                                          */
   6027       1.1  christos /* Based on the value of unit, call the appropriate ioctl handler or return */
   6028       1.1  christos /* EIO if ipfilter is not running.   Also checks if write perms are req'd   */
   6029       1.1  christos /* for the device in order to execute the ioctl.  A special case is made    */
   6030       1.1  christos /* SIOCIPFINTERROR so that the same code isn't required in every handler.   */
   6031       1.3   darrenr /* The context data pointer is passed through as this is used as the key    */
   6032       1.3   darrenr /* for locating a matching token for continued access for walking lists,    */
   6033       1.3   darrenr /* etc.                                                                     */
   6034       1.1  christos /* ------------------------------------------------------------------------ */
   6035       1.1  christos int
   6036       1.2  christos ipf_ioctlswitch(ipf_main_softc_t *softc, int unit, void *data, ioctlcmd_t cmd,
   6037       1.2  christos     int mode, int uid, void *ctx)
   6038       1.1  christos {
   6039       1.1  christos 	int error = 0;
   6040       1.1  christos 
   6041       1.1  christos 	switch (cmd)
   6042       1.1  christos 	{
   6043       1.1  christos 	case SIOCIPFINTERROR :
   6044       1.1  christos 		error = BCOPYOUT(&softc->ipf_interror, data,
   6045       1.1  christos 				 sizeof(softc->ipf_interror));
   6046       1.1  christos 		if (error != 0) {
   6047       1.1  christos 			IPFERROR(40);
   6048       1.1  christos 			error = EFAULT;
   6049       1.1  christos 		}
   6050       1.1  christos 		return error;
   6051       1.1  christos 	default :
   6052       1.1  christos 		break;
   6053       1.1  christos 	}
   6054       1.1  christos 
   6055       1.1  christos 	switch (unit)
   6056       1.1  christos 	{
   6057       1.1  christos 	case IPL_LOGIPF :
   6058       1.1  christos 		error = ipf_ipf_ioctl(softc, data, cmd, mode, uid, ctx);
   6059       1.1  christos 		break;
   6060       1.1  christos 	case IPL_LOGNAT :
   6061       1.1  christos 		if (softc->ipf_running > 0) {
   6062       1.1  christos 			error = ipf_nat_ioctl(softc, data, cmd, mode,
   6063       1.1  christos 					      uid, ctx);
   6064       1.1  christos 		} else {
   6065       1.1  christos 			IPFERROR(42);
   6066       1.1  christos 			error = EIO;
   6067       1.1  christos 		}
   6068       1.1  christos 		break;
   6069       1.1  christos 	case IPL_LOGSTATE :
   6070       1.1  christos 		if (softc->ipf_running > 0) {
   6071       1.1  christos 			error = ipf_state_ioctl(softc, data, cmd, mode,
   6072       1.1  christos 						uid, ctx);
   6073       1.1  christos 		} else {
   6074       1.1  christos 			IPFERROR(43);
   6075       1.1  christos 			error = EIO;
   6076       1.1  christos 		}
   6077       1.1  christos 		break;
   6078       1.1  christos 	case IPL_LOGAUTH :
   6079       1.1  christos 		if (softc->ipf_running > 0) {
   6080       1.1  christos 			error = ipf_auth_ioctl(softc, data, cmd, mode,
   6081       1.1  christos 					       uid, ctx);
   6082       1.1  christos 		} else {
   6083       1.1  christos 			IPFERROR(44);
   6084       1.1  christos 			error = EIO;
   6085       1.1  christos 		}
   6086       1.1  christos 		break;
   6087       1.1  christos 	case IPL_LOGSYNC :
   6088       1.1  christos 		if (softc->ipf_running > 0) {
   6089       1.1  christos 			error = ipf_sync_ioctl(softc, data, cmd, mode,
   6090       1.1  christos 					       uid, ctx);
   6091       1.1  christos 		} else {
   6092       1.1  christos 			error = EIO;
   6093       1.1  christos 			IPFERROR(45);
   6094       1.1  christos 		}
   6095       1.1  christos 		break;
   6096       1.1  christos 	case IPL_LOGSCAN :
   6097       1.1  christos #ifdef IPFILTER_SCAN
   6098       1.1  christos 		if (softc->ipf_running > 0)
   6099       1.1  christos 			error = ipf_scan_ioctl(softc, data, cmd, mode,
   6100       1.1  christos 					       uid, ctx);
   6101       1.1  christos 		else
   6102       1.1  christos #endif
   6103       1.1  christos 		{
   6104       1.1  christos 			error = EIO;
   6105       1.1  christos 			IPFERROR(46);
   6106       1.1  christos 		}
   6107       1.1  christos 		break;
   6108       1.1  christos 	case IPL_LOGLOOKUP :
   6109       1.1  christos 		if (softc->ipf_running > 0) {
   6110       1.1  christos 			error = ipf_lookup_ioctl(softc, data, cmd, mode,
   6111       1.1  christos 						 uid, ctx);
   6112       1.1  christos 		} else {
   6113       1.1  christos 			error = EIO;
   6114       1.1  christos 			IPFERROR(47);
   6115       1.1  christos 		}
   6116       1.1  christos 		break;
   6117       1.1  christos 	default :
   6118       1.1  christos 		IPFERROR(48);
   6119       1.1  christos 		error = EIO;
   6120       1.1  christos 		break;
   6121       1.1  christos 	}
   6122       1.1  christos 
   6123       1.1  christos 	return error;
   6124       1.1  christos }
   6125       1.1  christos 
   6126       1.1  christos 
   6127       1.1  christos /*
   6128       1.1  christos  * This array defines the expected size of objects coming into the kernel
   6129       1.1  christos  * for the various recognised object types. The first column is flags (see
   6130       1.1  christos  * below), 2nd column is current size, 3rd column is the version number of
   6131       1.1  christos  * when the current size became current.
   6132       1.1  christos  * Flags:
   6133       1.1  christos  * 1 = minimum size, not absolute size
   6134       1.1  christos  */
   6135       1.1  christos static	int	ipf_objbytes[IPFOBJ_COUNT][3] = {
   6136       1.3   darrenr 	{ 1,	sizeof(struct frentry),		5010000 },	/* 0 */
   6137       1.1  christos 	{ 1,	sizeof(struct friostat),	5010000 },
   6138       1.1  christos 	{ 0,	sizeof(struct fr_info),		5010000 },
   6139       1.1  christos 	{ 0,	sizeof(struct ipf_authstat),	4010100 },
   6140       1.1  christos 	{ 0,	sizeof(struct ipfrstat),	5010000 },
   6141       1.3   darrenr 	{ 1,	sizeof(struct ipnat),		5010000 },	/* 5 */
   6142       1.1  christos 	{ 0,	sizeof(struct natstat),		5010000 },
   6143       1.1  christos 	{ 0,	sizeof(struct ipstate_save),	5010000 },
   6144       1.1  christos 	{ 1,	sizeof(struct nat_save),	5010000 },
   6145       1.1  christos 	{ 0,	sizeof(struct natlookup),	5010000 },
   6146       1.3   darrenr 	{ 1,	sizeof(struct ipstate),		5010000 },	/* 10 */
   6147       1.1  christos 	{ 0,	sizeof(struct ips_stat),	5010000 },
   6148       1.1  christos 	{ 0,	sizeof(struct frauth),		5010000 },
   6149       1.1  christos 	{ 0,	sizeof(struct ipftune),		4010100 },
   6150       1.1  christos 	{ 0,	sizeof(struct nat),		5010000 },
   6151       1.3   darrenr 	{ 0,	sizeof(struct ipfruleiter),	4011400 },	/* 15 */
   6152       1.1  christos 	{ 0,	sizeof(struct ipfgeniter),	4011400 },
   6153       1.1  christos 	{ 0,	sizeof(struct ipftable),	4011400 },
   6154       1.1  christos 	{ 0,	sizeof(struct ipflookupiter),	4011400 },
   6155       1.1  christos 	{ 0,	sizeof(struct ipftq) * IPF_TCP_NSTATES },
   6156       1.3   darrenr 	{ 1,	0,				0	}, /* IPFEXPR */
   6157       1.1  christos 	{ 0,	0,				0	}, /* PROXYCTL */
   6158       1.1  christos 	{ 0,	sizeof (struct fripf),		5010000	}
   6159       1.1  christos };
   6160       1.1  christos 
   6161       1.1  christos 
   6162       1.1  christos /* ------------------------------------------------------------------------ */
   6163       1.1  christos /* Function:    ipf_inobj                                                   */
   6164       1.1  christos /* Returns:     int     - 0 = success, else failure                         */
   6165       1.1  christos /* Parameters:  softc(I) - soft context pointerto work with                 */
   6166       1.1  christos /*              data(I)  - pointer to ioctl data                            */
   6167       1.1  christos /*              objp(O)  - where to store ipfobj structure                  */
   6168       1.1  christos /*              ptr(I)   - pointer to data to copy out                      */
   6169       1.1  christos /*              type(I)  - type of structure being moved                    */
   6170       1.1  christos /*                                                                          */
   6171       1.1  christos /* Copy in the contents of what the ipfobj_t points to.  In future, we      */
   6172       1.1  christos /* add things to check for version numbers, sizes, etc, to make it backward */
   6173       1.1  christos /* compatible at the ABI for user land.                                     */
   6174       1.1  christos /* If objp is not NULL then we assume that the caller wants to see what is  */
   6175       1.1  christos /* in the ipfobj_t structure being copied in. As an example, this can tell  */
   6176       1.1  christos /* the caller what version of ipfilter the ioctl program was written to.    */
   6177       1.1  christos /* ------------------------------------------------------------------------ */
   6178       1.1  christos int
   6179       1.2  christos ipf_inobj(ipf_main_softc_t *softc, void *data, ipfobj_t *objp, void *ptr,
   6180       1.2  christos     int type)
   6181       1.1  christos {
   6182       1.1  christos 	ipfobj_t obj;
   6183       1.1  christos 	int error;
   6184       1.1  christos 	int size;
   6185       1.1  christos 
   6186       1.1  christos 	if ((type < 0) || (type >= IPFOBJ_COUNT)) {
   6187       1.1  christos 		IPFERROR(49);
   6188       1.1  christos 		return EINVAL;
   6189       1.1  christos 	}
   6190       1.1  christos 
   6191       1.1  christos 	if (objp == NULL)
   6192       1.1  christos 		objp = &obj;
   6193       1.1  christos 	error = BCOPYIN(data, objp, sizeof(*objp));
   6194       1.1  christos 	if (error != 0) {
   6195       1.1  christos 		IPFERROR(124);
   6196       1.1  christos 		return EFAULT;
   6197       1.1  christos 	}
   6198       1.1  christos 
   6199       1.1  christos 	if (objp->ipfo_type != type) {
   6200       1.1  christos 		IPFERROR(50);
   6201       1.1  christos 		return EINVAL;
   6202       1.1  christos 	}
   6203       1.1  christos 
   6204       1.1  christos 	if (objp->ipfo_rev >= ipf_objbytes[type][2]) {
   6205       1.1  christos 		if ((ipf_objbytes[type][0] & 1) != 0) {
   6206       1.1  christos 			if (objp->ipfo_size < ipf_objbytes[type][1]) {
   6207       1.1  christos 				IPFERROR(51);
   6208       1.1  christos 				return EINVAL;
   6209       1.1  christos 			}
   6210       1.1  christos 			size =  ipf_objbytes[type][1];
   6211       1.1  christos 		} else if (objp->ipfo_size == ipf_objbytes[type][1]) {
   6212       1.1  christos 			size =  objp->ipfo_size;
   6213       1.1  christos 		} else {
   6214       1.1  christos 			IPFERROR(52);
   6215       1.1  christos 			return EINVAL;
   6216       1.1  christos 		}
   6217       1.1  christos 		error = COPYIN(objp->ipfo_ptr, ptr, size);
   6218       1.1  christos 		if (error != 0) {
   6219       1.1  christos 			IPFERROR(55);
   6220       1.1  christos 			error = EFAULT;
   6221       1.1  christos 		}
   6222       1.1  christos 	} else {
   6223       1.1  christos #ifdef  IPFILTER_COMPAT
   6224       1.1  christos 		error = ipf_in_compat(softc, objp, ptr, 0);
   6225       1.1  christos #else
   6226       1.1  christos 		IPFERROR(54);
   6227       1.1  christos 		error = EINVAL;
   6228       1.1  christos #endif
   6229       1.1  christos 	}
   6230       1.1  christos 	return error;
   6231       1.1  christos }
   6232       1.1  christos 
   6233       1.1  christos 
   6234       1.1  christos /* ------------------------------------------------------------------------ */
   6235       1.1  christos /* Function:    ipf_inobjsz                                                 */
   6236       1.1  christos /* Returns:     int     - 0 = success, else failure                         */
   6237       1.1  christos /* Parameters:  softc(I) - soft context pointerto work with                 */
   6238       1.1  christos /*              data(I)  - pointer to ioctl data                            */
   6239       1.1  christos /*              ptr(I)   - pointer to store real data in                    */
   6240       1.1  christos /*              type(I)  - type of structure being moved                    */
   6241       1.1  christos /*              sz(I)    - size of data to copy                             */
   6242       1.1  christos /*                                                                          */
   6243       1.1  christos /* As per ipf_inobj, except the size of the object to copy in is passed in  */
   6244       1.1  christos /* but it must not be smaller than the size defined for the type and the    */
   6245       1.1  christos /* type must allow for varied sized objects.  The extra requirement here is */
   6246       1.1  christos /* that sz must match the size of the object being passed in - this is not  */
   6247       1.1  christos /* not possible nor required in ipf_inobj().                                */
   6248       1.1  christos /* ------------------------------------------------------------------------ */
   6249       1.1  christos int
   6250       1.2  christos ipf_inobjsz(ipf_main_softc_t *softc, void *data, void *ptr, int type, int sz)
   6251       1.1  christos {
   6252       1.1  christos 	ipfobj_t obj;
   6253       1.1  christos 	int error;
   6254       1.1  christos 
   6255       1.1  christos 	if ((type < 0) || (type >= IPFOBJ_COUNT)) {
   6256       1.1  christos 		IPFERROR(56);
   6257       1.1  christos 		return EINVAL;
   6258       1.1  christos 	}
   6259       1.1  christos 
   6260       1.1  christos 	error = BCOPYIN(data, &obj, sizeof(obj));
   6261       1.1  christos 	if (error != 0) {
   6262       1.1  christos 		IPFERROR(125);
   6263       1.1  christos 		return EFAULT;
   6264       1.1  christos 	}
   6265       1.1  christos 
   6266       1.1  christos 	if (obj.ipfo_type != type) {
   6267       1.1  christos 		IPFERROR(58);
   6268       1.1  christos 		return EINVAL;
   6269       1.1  christos 	}
   6270       1.1  christos 
   6271       1.1  christos 	if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
   6272       1.1  christos 		if (((ipf_objbytes[type][0] & 1) == 0) ||
   6273       1.1  christos 		    (sz < ipf_objbytes[type][1])) {
   6274       1.1  christos 			IPFERROR(57);
   6275       1.1  christos 			return EINVAL;
   6276       1.1  christos 		}
   6277       1.1  christos 		error = COPYIN(obj.ipfo_ptr, ptr, sz);
   6278       1.1  christos 		if (error != 0) {
   6279       1.1  christos 			IPFERROR(61);
   6280       1.1  christos 			error = EFAULT;
   6281       1.1  christos 		}
   6282       1.1  christos 	} else {
   6283       1.1  christos #ifdef	IPFILTER_COMPAT
   6284       1.1  christos 		error = ipf_in_compat(softc, &obj, ptr, sz);
   6285       1.1  christos #else
   6286       1.1  christos 		IPFERROR(60);
   6287       1.1  christos 		error = EINVAL;
   6288       1.1  christos #endif
   6289       1.1  christos 	}
   6290       1.1  christos 	return error;
   6291       1.1  christos }
   6292       1.1  christos 
   6293       1.1  christos 
   6294       1.1  christos /* ------------------------------------------------------------------------ */
   6295       1.1  christos /* Function:    ipf_outobjsz                                                */
   6296       1.1  christos /* Returns:     int     - 0 = success, else failure                         */
   6297       1.1  christos /* Parameters:  data(I) - pointer to ioctl data                             */
   6298       1.1  christos /*              ptr(I)  - pointer to store real data in                     */
   6299       1.1  christos /*              type(I) - type of structure being moved                     */
   6300       1.1  christos /*              sz(I)   - size of data to copy                              */
   6301       1.1  christos /*                                                                          */
   6302       1.1  christos /* As per ipf_outobj, except the size of the object to copy out is passed in*/
   6303       1.1  christos /* but it must not be smaller than the size defined for the type and the    */
   6304       1.1  christos /* type must allow for varied sized objects.  The extra requirement here is */
   6305       1.1  christos /* that sz must match the size of the object being passed in - this is not  */
   6306       1.1  christos /* not possible nor required in ipf_outobj().                               */
   6307       1.1  christos /* ------------------------------------------------------------------------ */
   6308       1.1  christos int
   6309       1.2  christos ipf_outobjsz(ipf_main_softc_t *softc, void *data, void *ptr, int type, int sz)
   6310       1.1  christos {
   6311       1.1  christos 	ipfobj_t obj;
   6312       1.1  christos 	int error;
   6313       1.1  christos 
   6314       1.1  christos 	if ((type < 0) || (type >= IPFOBJ_COUNT)) {
   6315       1.1  christos 		IPFERROR(62);
   6316       1.1  christos 		return EINVAL;
   6317       1.1  christos 	}
   6318       1.1  christos 
   6319       1.1  christos 	error = BCOPYIN(data, &obj, sizeof(obj));
   6320       1.1  christos 	if (error != 0) {
   6321       1.1  christos 		IPFERROR(127);
   6322       1.1  christos 		return EFAULT;
   6323       1.1  christos 	}
   6324       1.1  christos 
   6325       1.1  christos 	if (obj.ipfo_type != type) {
   6326       1.1  christos 		IPFERROR(63);
   6327       1.1  christos 		return EINVAL;
   6328       1.1  christos 	}
   6329       1.1  christos 
   6330       1.1  christos 	if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
   6331       1.1  christos 		if (((ipf_objbytes[type][0] & 1) == 0) ||
   6332       1.1  christos 		    (sz < ipf_objbytes[type][1])) {
   6333       1.1  christos 			IPFERROR(146);
   6334       1.1  christos 			return EINVAL;
   6335       1.1  christos 		}
   6336       1.1  christos 		error = COPYOUT(ptr, obj.ipfo_ptr, sz);
   6337       1.1  christos 		if (error != 0) {
   6338       1.1  christos 			IPFERROR(66);
   6339       1.1  christos 			error = EFAULT;
   6340       1.1  christos 		}
   6341       1.1  christos 	} else {
   6342       1.1  christos #ifdef	IPFILTER_COMPAT
   6343       1.1  christos 		error = ipf_out_compat(softc, &obj, ptr);
   6344       1.1  christos #else
   6345       1.1  christos 		IPFERROR(65);
   6346       1.1  christos 		error = EINVAL;
   6347       1.1  christos #endif
   6348       1.1  christos 	}
   6349       1.1  christos 	return error;
   6350       1.1  christos }
   6351       1.1  christos 
   6352       1.1  christos 
   6353       1.1  christos /* ------------------------------------------------------------------------ */
   6354       1.1  christos /* Function:    ipf_outobj                                                  */
   6355       1.1  christos /* Returns:     int     - 0 = success, else failure                         */
   6356       1.1  christos /* Parameters:  data(I) - pointer to ioctl data                             */
   6357       1.1  christos /*              ptr(I)  - pointer to store real data in                     */
   6358       1.1  christos /*              type(I) - type of structure being moved                     */
   6359       1.1  christos /*                                                                          */
   6360       1.1  christos /* Copy out the contents of what ptr is to where ipfobj points to.  In      */
   6361       1.1  christos /* future, we add things to check for version numbers, sizes, etc, to make  */
   6362       1.1  christos /* it backward  compatible at the ABI for user land.                        */
   6363       1.1  christos /* ------------------------------------------------------------------------ */
   6364       1.1  christos int
   6365       1.2  christos ipf_outobj(ipf_main_softc_t *softc, void *data, void *ptr, int type)
   6366       1.1  christos {
   6367       1.1  christos 	ipfobj_t obj;
   6368       1.1  christos 	int error;
   6369       1.1  christos 
   6370       1.1  christos 	if ((type < 0) || (type >= IPFOBJ_COUNT)) {
   6371       1.1  christos 		IPFERROR(67);
   6372       1.1  christos 		return EINVAL;
   6373       1.1  christos 	}
   6374       1.1  christos 
   6375       1.1  christos 	error = BCOPYIN(data, &obj, sizeof(obj));
   6376       1.1  christos 	if (error != 0) {
   6377       1.1  christos 		IPFERROR(126);
   6378       1.1  christos 		return EFAULT;
   6379       1.1  christos 	}
   6380       1.1  christos 
   6381       1.1  christos 	if (obj.ipfo_type != type) {
   6382       1.1  christos 		IPFERROR(68);
   6383       1.1  christos 		return EINVAL;
   6384       1.1  christos 	}
   6385       1.1  christos 
   6386       1.1  christos 	if (obj.ipfo_rev >= ipf_objbytes[type][2]) {
   6387       1.1  christos 		if ((ipf_objbytes[type][0] & 1) != 0) {
   6388       1.1  christos 			if (obj.ipfo_size < ipf_objbytes[type][1]) {
   6389       1.1  christos 				IPFERROR(69);
   6390       1.1  christos 				return EINVAL;
   6391       1.1  christos 			}
   6392       1.1  christos 		} else if (obj.ipfo_size != ipf_objbytes[type][1]) {
   6393       1.1  christos 			IPFERROR(70);
   6394       1.1  christos 			return EINVAL;
   6395       1.1  christos 		}
   6396       1.1  christos 
   6397       1.1  christos 		error = COPYOUT(ptr, obj.ipfo_ptr, obj.ipfo_size);
   6398       1.1  christos 		if (error != 0) {
   6399       1.1  christos 			IPFERROR(73);
   6400       1.1  christos 			error = EFAULT;
   6401       1.1  christos 		}
   6402       1.1  christos 	} else {
   6403       1.1  christos #ifdef	IPFILTER_COMPAT
   6404       1.1  christos 		error = ipf_out_compat(softc, &obj, ptr);
   6405       1.1  christos #else
   6406       1.1  christos 		IPFERROR(72);
   6407       1.1  christos 		error = EINVAL;
   6408       1.1  christos #endif
   6409       1.1  christos 	}
   6410       1.1  christos 	return error;
   6411       1.1  christos }
   6412       1.1  christos 
   6413       1.1  christos 
   6414       1.1  christos /* ------------------------------------------------------------------------ */
   6415       1.1  christos /* Function:    ipf_outobjk                                                 */
   6416       1.1  christos /* Returns:     int     - 0 = success, else failure                         */
   6417       1.1  christos /* Parameters:  obj(I)  - pointer to data description structure             */
   6418       1.1  christos /*              ptr(I)  - pointer to kernel data to copy out                */
   6419       1.1  christos /*                                                                          */
   6420       1.1  christos /* In the above functions, the ipfobj_t structure is copied into the kernel,*/
   6421       1.1  christos /* telling ipfilter how to copy out data. In this instance, the ipfobj_t is */
   6422       1.1  christos /* already populated with information and now we just need to use it.       */
   6423       1.1  christos /* There is no need for this function to have a "type" parameter as there   */
   6424       1.1  christos /* is no point in validating information that comes from the kernel with    */
   6425       1.1  christos /* itself.                                                                  */
   6426       1.1  christos /* ------------------------------------------------------------------------ */
   6427       1.3   darrenr int
   6428       1.3   darrenr ipf_outobjk(ipf_main_softc_t *softc, ipfobj_t *obj, void *ptr)
   6429       1.1  christos {
   6430       1.1  christos 	int type = obj->ipfo_type;
   6431       1.1  christos 	int error;
   6432       1.1  christos 
   6433       1.1  christos 	if ((type < 0) || (type >= IPFOBJ_COUNT)) {
   6434       1.1  christos 		IPFERROR(147);
   6435       1.1  christos 		return EINVAL;
   6436       1.1  christos 	}
   6437       1.1  christos 
   6438       1.1  christos 	if (obj->ipfo_rev >= ipf_objbytes[type][2]) {
   6439       1.1  christos 		if ((ipf_objbytes[type][0] & 1) != 0) {
   6440       1.1  christos 			if (obj->ipfo_size < ipf_objbytes[type][1]) {
   6441       1.1  christos 				IPFERROR(148);
   6442       1.1  christos 				return EINVAL;
   6443       1.1  christos 			}
   6444       1.1  christos 
   6445       1.1  christos 		} else if (obj->ipfo_size != ipf_objbytes[type][1]) {
   6446       1.1  christos 			IPFERROR(149);
   6447       1.1  christos 			return EINVAL;
   6448       1.1  christos 		}
   6449       1.1  christos 
   6450       1.1  christos 		error = COPYOUT(ptr, obj->ipfo_ptr, obj->ipfo_size);
   6451       1.1  christos 		if (error != 0) {
   6452       1.1  christos 			IPFERROR(150);
   6453       1.1  christos 			error = EFAULT;
   6454       1.1  christos 		}
   6455       1.1  christos 	} else {
   6456       1.1  christos #ifdef  IPFILTER_COMPAT
   6457       1.1  christos 		error = ipf_out_compat(softc, obj, ptr);
   6458       1.1  christos #else
   6459       1.1  christos 		IPFERROR(151);
   6460       1.1  christos 		error = EINVAL;
   6461       1.1  christos #endif
   6462       1.1  christos 	}
   6463       1.1  christos 	return error;
   6464       1.1  christos }
   6465       1.1  christos 
   6466       1.1  christos 
   6467       1.1  christos /* ------------------------------------------------------------------------ */
   6468       1.1  christos /* Function:    ipf_checkl4sum                                              */
   6469       1.1  christos /* Returns:     int     - 0 = good, -1 = bad, 1 = cannot check              */
   6470       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   6471       1.1  christos /*                                                                          */
   6472       1.1  christos /* If possible, calculate the layer 4 checksum for the packet.  If this is  */
   6473       1.1  christos /* not possible, return without indicating a failure or success but in a    */
   6474       1.3   darrenr /* way that is ditinguishable. This function should only be called by the   */
   6475       1.3   darrenr /* ipf_checkv6sum() for each platform.                                      */
   6476       1.1  christos /* ------------------------------------------------------------------------ */
   6477       1.1  christos int
   6478       1.2  christos ipf_checkl4sum(fr_info_t *fin)
   6479       1.1  christos {
   6480       1.1  christos 	u_short sum, hdrsum, *csump;
   6481       1.1  christos 	udphdr_t *udp;
   6482       1.1  christos 	int dosum;
   6483       1.1  christos 
   6484       1.1  christos 	/*
   6485       1.1  christos 	 * If the TCP packet isn't a fragment, isn't too short and otherwise
   6486       1.1  christos 	 * isn't already considered "bad", then validate the checksum.  If
   6487       1.1  christos 	 * this check fails then considered the packet to be "bad".
   6488       1.1  christos 	 */
   6489       1.1  christos 	if ((fin->fin_flx & (FI_FRAG|FI_SHORT|FI_BAD)) != 0)
   6490       1.1  christos 		return 1;
   6491       1.1  christos 
   6492       1.1  christos 	csump = NULL;
   6493       1.1  christos 	hdrsum = 0;
   6494       1.1  christos 	dosum = 0;
   6495       1.1  christos 	sum = 0;
   6496       1.1  christos 
   6497       1.3   darrenr 	switch (fin->fin_p)
   6498       1.3   darrenr 	{
   6499       1.3   darrenr 	case IPPROTO_TCP :
   6500       1.3   darrenr 		csump = &((tcphdr_t *)fin->fin_dp)->th_sum;
   6501       1.3   darrenr 		dosum = 1;
   6502       1.3   darrenr 		break;
   6503       1.3   darrenr 
   6504       1.3   darrenr 	case IPPROTO_UDP :
   6505       1.3   darrenr 		udp = fin->fin_dp;
   6506       1.3   darrenr 		if (udp->uh_sum != 0) {
   6507       1.3   darrenr 			csump = &udp->uh_sum;
   6508       1.1  christos 			dosum = 1;
   6509       1.3   darrenr 		}
   6510       1.3   darrenr 		break;
   6511       1.1  christos 
   6512       1.3   darrenr #ifdef USE_INET6
   6513       1.3   darrenr 	case IPPROTO_ICMPV6 :
   6514       1.3   darrenr 		csump = &((struct icmp6_hdr *)fin->fin_dp)->icmp6_cksum;
   6515       1.3   darrenr 		dosum = 1;
   6516       1.3   darrenr 		break;
   6517       1.3   darrenr #endif
   6518       1.1  christos 
   6519       1.3   darrenr 	case IPPROTO_ICMP :
   6520       1.3   darrenr 		csump = &((struct icmp *)fin->fin_dp)->icmp_cksum;
   6521       1.3   darrenr 		dosum = 1;
   6522       1.3   darrenr 		break;
   6523       1.1  christos 
   6524       1.3   darrenr 	default :
   6525       1.3   darrenr 		return 1;
   6526       1.3   darrenr 		/*NOTREACHED*/
   6527       1.3   darrenr 	}
   6528       1.1  christos 
   6529       1.3   darrenr 	if (csump != NULL)
   6530       1.3   darrenr 		hdrsum = *csump;
   6531       1.1  christos 
   6532       1.3   darrenr 	if (dosum) {
   6533       1.3   darrenr 		sum = fr_cksum(fin, fin->fin_ip, fin->fin_p, fin->fin_dp);
   6534       1.1  christos 	}
   6535       1.1  christos #if !defined(_KERNEL)
   6536       1.1  christos 	if (sum == hdrsum) {
   6537       1.1  christos 		FR_DEBUG(("checkl4sum: %hx == %hx\n", sum, hdrsum));
   6538       1.1  christos 	} else {
   6539       1.1  christos 		FR_DEBUG(("checkl4sum: %hx != %hx\n", sum, hdrsum));
   6540       1.1  christos 	}
   6541       1.1  christos #endif
   6542       1.3   darrenr 	DT2(l4sums, u_short, hdrsum, u_short, sum);
   6543       1.1  christos 	if (hdrsum == sum) {
   6544       1.3   darrenr 		fin->fin_cksum = FI_CK_SUMOK;
   6545       1.1  christos 		return 0;
   6546       1.1  christos 	}
   6547       1.3   darrenr 	fin->fin_cksum = FI_CK_BAD;
   6548       1.1  christos 	return -1;
   6549       1.1  christos }
   6550       1.1  christos 
   6551       1.1  christos 
   6552       1.1  christos /* ------------------------------------------------------------------------ */
   6553       1.1  christos /* Function:    ipf_ifpfillv4addr                                           */
   6554       1.1  christos /* Returns:     int     - 0 = address update, -1 = address not updated      */
   6555       1.1  christos /* Parameters:  atype(I)   - type of network address update to perform      */
   6556       1.1  christos /*              sin(I)     - pointer to source of address information       */
   6557       1.1  christos /*              mask(I)    - pointer to source of netmask information       */
   6558       1.1  christos /*              inp(I)     - pointer to destination address store           */
   6559       1.1  christos /*              inpmask(I) - pointer to destination netmask store           */
   6560       1.1  christos /*                                                                          */
   6561       1.1  christos /* Given a type of network address update (atype) to perform, copy          */
   6562       1.1  christos /* information from sin/mask into inp/inpmask.  If ipnmask is NULL then no  */
   6563       1.1  christos /* netmask update is performed unless FRI_NETMASKED is passed as atype, in  */
   6564       1.1  christos /* which case the operation fails.  For all values of atype other than      */
   6565       1.1  christos /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s  */
   6566       1.1  christos /* value.                                                                   */
   6567       1.1  christos /* ------------------------------------------------------------------------ */
   6568       1.1  christos int
   6569       1.2  christos ipf_ifpfillv4addr(int atype, struct sockaddr_in *sin, struct sockaddr_in *mask,
   6570       1.2  christos     struct in_addr *inp, struct in_addr *inpmask)
   6571       1.1  christos {
   6572       1.1  christos 	if (inpmask != NULL && atype != FRI_NETMASKED)
   6573       1.1  christos 		inpmask->s_addr = 0xffffffff;
   6574       1.1  christos 
   6575       1.1  christos 	if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
   6576       1.1  christos 		if (atype == FRI_NETMASKED) {
   6577       1.1  christos 			if (inpmask == NULL)
   6578       1.1  christos 				return -1;
   6579       1.1  christos 			inpmask->s_addr = mask->sin_addr.s_addr;
   6580       1.1  christos 		}
   6581       1.1  christos 		inp->s_addr = sin->sin_addr.s_addr & mask->sin_addr.s_addr;
   6582       1.1  christos 	} else {
   6583       1.1  christos 		inp->s_addr = sin->sin_addr.s_addr;
   6584       1.1  christos 	}
   6585       1.1  christos 	return 0;
   6586       1.1  christos }
   6587       1.1  christos 
   6588       1.1  christos 
   6589       1.1  christos #ifdef	USE_INET6
   6590       1.1  christos /* ------------------------------------------------------------------------ */
   6591       1.1  christos /* Function:    ipf_ifpfillv6addr                                           */
   6592       1.1  christos /* Returns:     int     - 0 = address update, -1 = address not updated      */
   6593       1.1  christos /* Parameters:  atype(I)   - type of network address update to perform      */
   6594       1.1  christos /*              sin(I)     - pointer to source of address information       */
   6595       1.1  christos /*              mask(I)    - pointer to source of netmask information       */
   6596       1.1  christos /*              inp(I)     - pointer to destination address store           */
   6597       1.1  christos /*              inpmask(I) - pointer to destination netmask store           */
   6598       1.1  christos /*                                                                          */
   6599       1.1  christos /* Given a type of network address update (atype) to perform, copy          */
   6600       1.1  christos /* information from sin/mask into inp/inpmask.  If ipnmask is NULL then no  */
   6601       1.1  christos /* netmask update is performed unless FRI_NETMASKED is passed as atype, in  */
   6602       1.1  christos /* which case the operation fails.  For all values of atype other than      */
   6603       1.1  christos /* FRI_NETMASKED, if inpmask is non-NULL then the mask is set to an all 1s  */
   6604       1.1  christos /* value.                                                                   */
   6605       1.1  christos /* ------------------------------------------------------------------------ */
   6606       1.1  christos int
   6607       1.2  christos ipf_ifpfillv6addr(int atype, struct sockaddr_in6 *sin,
   6608       1.2  christos     struct sockaddr_in6 *mask, i6addr_t *inp, i6addr_t *inpmask)
   6609       1.1  christos {
   6610       1.1  christos 	i6addr_t *src, *and;
   6611       1.1  christos 
   6612       1.1  christos 	src = (i6addr_t *)&sin->sin6_addr;
   6613       1.1  christos 	and = (i6addr_t *)&mask->sin6_addr;
   6614       1.1  christos 
   6615       1.1  christos 	if (inpmask != NULL && atype != FRI_NETMASKED) {
   6616       1.1  christos 		inpmask->i6[0] = 0xffffffff;
   6617       1.1  christos 		inpmask->i6[1] = 0xffffffff;
   6618       1.1  christos 		inpmask->i6[2] = 0xffffffff;
   6619       1.1  christos 		inpmask->i6[3] = 0xffffffff;
   6620       1.1  christos 	}
   6621       1.1  christos 
   6622       1.1  christos 	if (atype == FRI_NETWORK || atype == FRI_NETMASKED) {
   6623       1.1  christos 		if (atype == FRI_NETMASKED) {
   6624       1.1  christos 			if (inpmask == NULL)
   6625       1.1  christos 				return -1;
   6626       1.1  christos 			inpmask->i6[0] = and->i6[0];
   6627       1.1  christos 			inpmask->i6[1] = and->i6[1];
   6628       1.1  christos 			inpmask->i6[2] = and->i6[2];
   6629       1.1  christos 			inpmask->i6[3] = and->i6[3];
   6630       1.1  christos 		}
   6631       1.1  christos 
   6632       1.1  christos 		inp->i6[0] = src->i6[0] & and->i6[0];
   6633       1.1  christos 		inp->i6[1] = src->i6[1] & and->i6[1];
   6634       1.1  christos 		inp->i6[2] = src->i6[2] & and->i6[2];
   6635       1.1  christos 		inp->i6[3] = src->i6[3] & and->i6[3];
   6636       1.1  christos 	} else {
   6637       1.1  christos 		inp->i6[0] = src->i6[0];
   6638       1.1  christos 		inp->i6[1] = src->i6[1];
   6639       1.1  christos 		inp->i6[2] = src->i6[2];
   6640       1.1  christos 		inp->i6[3] = src->i6[3];
   6641       1.1  christos 	}
   6642       1.1  christos 	return 0;
   6643       1.1  christos }
   6644       1.1  christos #endif
   6645       1.1  christos 
   6646       1.1  christos 
   6647       1.1  christos /* ------------------------------------------------------------------------ */
   6648       1.1  christos /* Function:    ipf_matchtag                                                */
   6649       1.1  christos /* Returns:     0 == mismatch, 1 == match.                                  */
   6650       1.1  christos /* Parameters:  tag1(I) - pointer to first tag to compare                   */
   6651       1.1  christos /*              tag2(I) - pointer to second tag to compare                  */
   6652       1.1  christos /*                                                                          */
   6653       1.1  christos /* Returns true (non-zero) or false(0) if the two tag structures can be     */
   6654       1.1  christos /* considered to be a match or not match, respectively.  The tag is 16      */
   6655       1.1  christos /* bytes long (16 characters) but that is overlayed with 4 32bit ints so    */
   6656       1.1  christos /* compare the ints instead, for speed. tag1 is the master of the           */
   6657       1.1  christos /* comparison.  This function should only be called with both tag1 and tag2 */
   6658       1.1  christos /* as non-NULL pointers.                                                    */
   6659       1.1  christos /* ------------------------------------------------------------------------ */
   6660       1.1  christos int
   6661       1.2  christos ipf_matchtag(ipftag_t *tag1, ipftag_t *tag2)
   6662       1.1  christos {
   6663       1.1  christos 	if (tag1 == tag2)
   6664       1.1  christos 		return 1;
   6665       1.1  christos 
   6666       1.1  christos 	if ((tag1->ipt_num[0] == 0) && (tag2->ipt_num[0] == 0))
   6667       1.1  christos 		return 1;
   6668       1.1  christos 
   6669       1.1  christos 	if ((tag1->ipt_num[0] == tag2->ipt_num[0]) &&
   6670       1.1  christos 	    (tag1->ipt_num[1] == tag2->ipt_num[1]) &&
   6671       1.1  christos 	    (tag1->ipt_num[2] == tag2->ipt_num[2]) &&
   6672       1.1  christos 	    (tag1->ipt_num[3] == tag2->ipt_num[3]))
   6673       1.1  christos 		return 1;
   6674       1.1  christos 	return 0;
   6675       1.1  christos }
   6676       1.1  christos 
   6677       1.1  christos 
   6678       1.1  christos /* ------------------------------------------------------------------------ */
   6679       1.1  christos /* Function:    ipf_coalesce                                                */
   6680       1.1  christos /* Returns:     1 == success, -1 == failure, 0 == no change                 */
   6681       1.1  christos /* Parameters:  fin(I) - pointer to packet information                      */
   6682       1.1  christos /*                                                                          */
   6683       1.1  christos /* Attempt to get all of the packet data into a single, contiguous buffer.  */
   6684       1.1  christos /* If this call returns a failure then the buffers have also been freed.    */
   6685       1.1  christos /* ------------------------------------------------------------------------ */
   6686       1.1  christos int
   6687       1.2  christos ipf_coalesce(fr_info_t *fin)
   6688       1.1  christos {
   6689       1.1  christos 
   6690       1.1  christos 	if ((fin->fin_flx & FI_COALESCE) != 0)
   6691       1.1  christos 		return 1;
   6692       1.1  christos 
   6693       1.1  christos 	/*
   6694       1.1  christos 	 * If the mbuf pointers indicate that there is no mbuf to work with,
   6695       1.1  christos 	 * return but do not indicate success or failure.
   6696       1.1  christos 	 */
   6697       1.1  christos 	if (fin->fin_m == NULL || fin->fin_mp == NULL)
   6698       1.1  christos 		return 0;
   6699       1.1  christos 
   6700       1.1  christos #if defined(_KERNEL)
   6701       1.1  christos 	if (ipf_pullup(fin->fin_m, fin, fin->fin_plen) == NULL) {
   6702       1.1  christos 		ipf_main_softc_t *softc = fin->fin_main_soft;
   6703       1.1  christos 
   6704       1.1  christos 		DT1(frb_coalesce, fr_info_t *, fin);
   6705       1.1  christos 		LBUMP(ipf_stats[fin->fin_out].fr_badcoalesces);
   6706       1.1  christos # ifdef MENTAT
   6707       1.1  christos 		FREE_MB_T(*fin->fin_mp);
   6708       1.1  christos # endif
   6709       1.1  christos 		fin->fin_reason = FRB_COALESCE;
   6710       1.1  christos 		*fin->fin_mp = NULL;
   6711       1.1  christos 		fin->fin_m = NULL;
   6712       1.1  christos 		return -1;
   6713       1.1  christos 	}
   6714       1.1  christos #else
   6715       1.1  christos 	fin = fin;	/* LINT */
   6716       1.1  christos #endif
   6717       1.1  christos 	return 1;
   6718       1.1  christos }
   6719       1.1  christos 
   6720       1.1  christos 
   6721       1.1  christos /*
   6722       1.1  christos  * The following table lists all of the tunable variables that can be
   6723       1.1  christos  * accessed via SIOCIPFGET/SIOCIPFSET/SIOCIPFGETNEXt.  The format of each row
   6724       1.1  christos  * in the table below is as follows:
   6725       1.1  christos  *
   6726       1.1  christos  * pointer to value, name of value, minimum, maximum, size of the value's
   6727       1.1  christos  *     container, value attribute flags
   6728       1.1  christos  *
   6729       1.1  christos  * For convienience, IPFT_RDONLY means the value is read-only, IPFT_WRDISABLED
   6730       1.1  christos  * means the value can only be written to when IPFilter is loaded but disabled.
   6731       1.1  christos  * The obvious implication is if neither of these are set then the value can be
   6732       1.1  christos  * changed at any time without harm.
   6733       1.1  christos  */
   6734       1.1  christos 
   6735       1.1  christos 
   6736       1.1  christos /* ------------------------------------------------------------------------ */
   6737       1.1  christos /* Function:    ipf_tune_findbycookie                                       */
   6738       1.1  christos /* Returns:     NULL = search failed, else pointer to tune struct           */
   6739       1.1  christos /* Parameters:  cookie(I) - cookie value to search for amongst tuneables    */
   6740       1.1  christos /*              next(O)   - pointer to place to store the cookie for the    */
   6741       1.1  christos /*                          "next" tuneable, if it is desired.              */
   6742       1.1  christos /*                                                                          */
   6743       1.1  christos /* This function is used to walk through all of the existing tunables with  */
   6744       1.1  christos /* successive calls.  It searches the known tunables for the one which has  */
   6745       1.1  christos /* a matching value for "cookie" - ie its address.  When returning a match, */
   6746       1.1  christos /* the next one to be found may be returned inside next.                    */
   6747       1.1  christos /* ------------------------------------------------------------------------ */
   6748       1.1  christos static ipftuneable_t *
   6749       1.2  christos ipf_tune_findbycookie(ipftuneable_t **ptop, void *cookie, void **next)
   6750       1.1  christos {
   6751       1.1  christos 	ipftuneable_t *ta, **tap;
   6752       1.1  christos 
   6753       1.1  christos 	for (ta = *ptop; ta->ipft_name != NULL; ta++)
   6754       1.1  christos 		if (ta == cookie) {
   6755       1.1  christos 			if (next != NULL) {
   6756       1.1  christos 				/*
   6757       1.1  christos 				 * If the next entry in the array has a name
   6758       1.1  christos 				 * present, then return a pointer to it for
   6759       1.1  christos 				 * where to go next, else return a pointer to
   6760       1.1  christos 				 * the dynaminc list as a key to search there
   6761       1.1  christos 				 * next.  This facilitates a weak linking of
   6762       1.1  christos 				 * the two "lists" together.
   6763       1.1  christos 				 */
   6764       1.1  christos 				if ((ta + 1)->ipft_name != NULL)
   6765       1.1  christos 					*next = ta + 1;
   6766       1.1  christos 				else
   6767       1.1  christos 					*next = ptop;
   6768       1.1  christos 			}
   6769       1.1  christos 			return ta;
   6770       1.1  christos 		}
   6771       1.1  christos 
   6772       1.1  christos 	for (tap = ptop; (ta = *tap) != NULL; tap = &ta->ipft_next)
   6773       1.1  christos 		if (tap == cookie) {
   6774       1.1  christos 			if (next != NULL)
   6775       1.1  christos 				*next = &ta->ipft_next;
   6776       1.1  christos 			return ta;
   6777       1.1  christos 		}
   6778       1.1  christos 
   6779       1.1  christos 	if (next != NULL)
   6780       1.1  christos 		*next = NULL;
   6781       1.1  christos 	return NULL;
   6782       1.1  christos }
   6783       1.1  christos 
   6784       1.1  christos 
   6785       1.1  christos /* ------------------------------------------------------------------------ */
   6786       1.1  christos /* Function:    ipf_tune_findbyname                                         */
   6787       1.1  christos /* Returns:     NULL = search failed, else pointer to tune struct           */
   6788       1.1  christos /* Parameters:  name(I) - name of the tuneable entry to find.               */
   6789       1.1  christos /*                                                                          */
   6790       1.1  christos /* Search the static array of tuneables and the list of dynamic tuneables   */
   6791       1.1  christos /* for an entry with a matching name.  If we can find one, return a pointer */
   6792       1.1  christos /* to the matching structure.                                               */
   6793       1.1  christos /* ------------------------------------------------------------------------ */
   6794       1.1  christos static ipftuneable_t *
   6795       1.2  christos ipf_tune_findbyname(ipftuneable_t *top, const char *name)
   6796       1.1  christos {
   6797       1.1  christos 	ipftuneable_t *ta;
   6798       1.1  christos 
   6799       1.1  christos 	for (ta = top; ta != NULL; ta = ta->ipft_next)
   6800       1.1  christos 		if (!strcmp(ta->ipft_name, name)) {
   6801       1.1  christos 			return ta;
   6802       1.1  christos 		}
   6803       1.1  christos 
   6804       1.1  christos 	return NULL;
   6805       1.1  christos }
   6806       1.1  christos 
   6807       1.1  christos 
   6808       1.1  christos /* ------------------------------------------------------------------------ */
   6809       1.1  christos /* Function:    ipf_tune_add_array                                          */
   6810       1.1  christos /* Returns:     int - 0 == success, else failure                            */
   6811       1.1  christos /* Parameters:  newtune - pointer to new tune array to add to tuneables     */
   6812       1.1  christos /*                                                                          */
   6813       1.1  christos /* Appends tune structures from the array passed in (newtune) to the end of */
   6814       1.1  christos /* the current list of "dynamic" tuneable parameters.                       */
   6815       1.1  christos /* If any entry to be added is already present (by name) then the operation */
   6816       1.1  christos /* is aborted - entries that have been added are removed before returning.  */
   6817       1.1  christos /* An entry with no name (NULL) is used as the indication that the end of   */
   6818       1.1  christos /* the array has been reached.                                              */
   6819       1.1  christos /* ------------------------------------------------------------------------ */
   6820       1.1  christos int
   6821       1.2  christos ipf_tune_add_array(ipf_main_softc_t *softc, ipftuneable_t *newtune)
   6822       1.1  christos {
   6823       1.1  christos 	ipftuneable_t *nt, *dt;
   6824       1.1  christos 	int error = 0;
   6825       1.1  christos 
   6826       1.1  christos 	for (nt = newtune; nt->ipft_name != NULL; nt++) {
   6827       1.1  christos 		error = ipf_tune_add(softc, nt);
   6828       1.1  christos 		if (error != 0) {
   6829       1.1  christos 			for (dt = newtune; dt != nt; dt++) {
   6830       1.1  christos 				(void) ipf_tune_del(softc, dt);
   6831       1.1  christos 			}
   6832       1.1  christos 		}
   6833       1.1  christos 	}
   6834       1.1  christos 
   6835       1.1  christos 	return error;
   6836       1.1  christos }
   6837       1.1  christos 
   6838       1.1  christos 
   6839       1.1  christos /* ------------------------------------------------------------------------ */
   6840       1.1  christos /* Function:    ipf_tune_array_link                                         */
   6841       1.1  christos /* Returns:     0 == success, -1 == failure                                 */
   6842       1.1  christos /* Parameters:  softc(I) - soft context pointerto work with                 */
   6843       1.1  christos /*              array(I) - pointer to an array of tuneables                 */
   6844       1.1  christos /*                                                                          */
   6845       1.1  christos /* Given an array of tunables (array), append them to the current list of   */
   6846       1.1  christos /* tuneables for this context (softc->ipf_tuners.) To properly prepare the  */
   6847       1.1  christos /* the array for being appended to the list, initialise all of the next     */
   6848       1.1  christos /* pointers so we don't need to walk parts of it with ++ and others with    */
   6849       1.1  christos /* next. The array is expected to have an entry with a NULL name as the     */
   6850       1.1  christos /* terminator. Trying to add an array with no non-NULL names will return as */
   6851       1.1  christos /* a failure.                                                               */
   6852       1.1  christos /* ------------------------------------------------------------------------ */
   6853       1.1  christos int
   6854       1.2  christos ipf_tune_array_link(ipf_main_softc_t *softc, ipftuneable_t *array)
   6855       1.1  christos {
   6856       1.1  christos 	ipftuneable_t *t, **p;
   6857       1.1  christos 
   6858       1.1  christos 	t = array;
   6859       1.1  christos 	if (t->ipft_name == NULL)
   6860       1.1  christos 		return -1;
   6861       1.1  christos 
   6862       1.1  christos 	for (; t[1].ipft_name != NULL; t++)
   6863       1.1  christos 		t[0].ipft_next = &t[1];
   6864       1.1  christos 	t->ipft_next = NULL;
   6865       1.1  christos 
   6866       1.1  christos 	/*
   6867       1.1  christos 	 * Since a pointer to the last entry isn't kept, we need to find it
   6868       1.1  christos 	 * each time we want to add new variables to the list.
   6869       1.1  christos 	 */
   6870       1.1  christos 	for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next)
   6871       1.1  christos 		if (t->ipft_name == NULL)
   6872       1.1  christos 			break;
   6873       1.1  christos 	*p = array;
   6874       1.1  christos 
   6875       1.1  christos 	return 0;
   6876       1.1  christos }
   6877       1.1  christos 
   6878       1.1  christos 
   6879       1.1  christos /* ------------------------------------------------------------------------ */
   6880       1.1  christos /* Function:    ipf_tune_array_unlink                                       */
   6881       1.1  christos /* Returns:     0 == success, -1 == failure                                 */
   6882       1.1  christos /* Parameters:  softc(I) - soft context pointerto work with                 */
   6883       1.1  christos /*              array(I) - pointer to an array of tuneables                 */
   6884       1.1  christos /*                                                                          */
   6885       1.1  christos /* ------------------------------------------------------------------------ */
   6886       1.1  christos int
   6887       1.2  christos ipf_tune_array_unlink(ipf_main_softc_t *softc, ipftuneable_t *array)
   6888       1.1  christos {
   6889       1.1  christos 	ipftuneable_t *t, **p;
   6890       1.1  christos 
   6891       1.1  christos 	for (p = &softc->ipf_tuners; (t = *p) != NULL; p = &t->ipft_next)
   6892       1.1  christos 		if (t == array)
   6893       1.1  christos 			break;
   6894       1.1  christos 	if (t == NULL)
   6895       1.1  christos 		return -1;
   6896       1.1  christos 
   6897       1.1  christos 	for (; t[1].ipft_name != NULL; t++)
   6898       1.1  christos 		;
   6899       1.1  christos 
   6900       1.1  christos 	*p = t->ipft_next;
   6901       1.1  christos 
   6902       1.1  christos 	return 0;
   6903       1.1  christos }
   6904       1.1  christos 
   6905       1.1  christos 
   6906       1.1  christos /* ------------------------------------------------------------------------ */
   6907       1.1  christos /* Function:   ipf_tune_array_copy                                          */
   6908       1.1  christos /* Returns:    NULL = failure, else pointer to new array                    */
   6909       1.1  christos /* Parameters: base(I)     - pointer to structure base                      */
   6910       1.1  christos /*             size(I)     - size of the array at template                  */
   6911       1.1  christos /*             template(I) - original array to copy                         */
   6912       1.1  christos /*                                                                          */
   6913       1.1  christos /* Allocate memory for a new set of tuneable values and copy everything     */
   6914       1.1  christos /* from template into the new region of memory.  The new region is full of  */
   6915       1.1  christos /* uninitialised pointers (ipft_next) so set them up.  Now, ipftp_offset... */
   6916       1.1  christos /*                                                                          */
   6917       1.1  christos /* NOTE: the following assumes that sizeof(long) == sizeof(void *)          */
   6918       1.1  christos /* In the array template, ipftp_offset is the offset (in bytes) of the      */
   6919       1.1  christos /* location of the tuneable value inside the structure pointed to by base.  */
   6920       1.1  christos /* As ipftp_offset is a union over the pointers to the tuneable values, if  */
   6921       1.1  christos /* we add base to the copy's ipftp_offset, copy ends up with a pointer in   */
   6922       1.1  christos /* ipftp_void that points to the stored value.                              */
   6923       1.1  christos /* ------------------------------------------------------------------------ */
   6924       1.1  christos ipftuneable_t *
   6925       1.2  christos ipf_tune_array_copy(void *base, size_t size, ipftuneable_t *template)
   6926       1.1  christos {
   6927       1.1  christos 	ipftuneable_t *copy;
   6928       1.1  christos 	int i;
   6929       1.1  christos 
   6930       1.1  christos 
   6931       1.1  christos 	KMALLOCS(copy, ipftuneable_t *, size);
   6932       1.1  christos 	if (copy == NULL) {
   6933       1.1  christos 		return NULL;
   6934       1.1  christos 	}
   6935       1.1  christos 	bcopy(template, copy, size);
   6936       1.1  christos 
   6937       1.1  christos 	for (i = 0; copy[i].ipft_name; i++) {
   6938       1.1  christos 		copy[i].ipft_una.ipftp_offset += (u_long)base;
   6939       1.1  christos 		copy[i].ipft_next = copy + i + 1;
   6940       1.1  christos 	}
   6941       1.1  christos 
   6942       1.1  christos 	return copy;
   6943       1.1  christos }
   6944       1.1  christos 
   6945       1.1  christos 
   6946       1.1  christos /* ------------------------------------------------------------------------ */
   6947       1.1  christos /* Function:    ipf_tune_add                                                */
   6948       1.1  christos /* Returns:     int - 0 == success, else failure                            */
   6949       1.1  christos /* Parameters:  newtune - pointer to new tune entry to add to tuneables     */
   6950       1.1  christos /*                                                                          */
   6951       1.1  christos /* Appends tune structures from the array passed in (newtune) to the end of */
   6952       1.1  christos /* the current list of "dynamic" tuneable parameters.  Once added, the      */
   6953       1.1  christos /* owner of the object is not expected to ever change "ipft_next".          */
   6954       1.1  christos /* ------------------------------------------------------------------------ */
   6955       1.1  christos int
   6956       1.2  christos ipf_tune_add(ipf_main_softc_t *softc, ipftuneable_t *newtune)
   6957       1.1  christos {
   6958       1.1  christos 	ipftuneable_t *ta, **tap;
   6959       1.1  christos 
   6960       1.1  christos 	ta = ipf_tune_findbyname(softc->ipf_tuners, newtune->ipft_name);
   6961       1.1  christos 	if (ta != NULL) {
   6962       1.1  christos 		IPFERROR(74);
   6963       1.1  christos 		return EEXIST;
   6964       1.1  christos 	}
   6965       1.1  christos 
   6966       1.1  christos 	for (tap = &softc->ipf_tuners; *tap != NULL; tap = &(*tap)->ipft_next)
   6967       1.1  christos 		;
   6968       1.1  christos 
   6969       1.1  christos 	newtune->ipft_next = NULL;
   6970       1.1  christos 	*tap = newtune;
   6971       1.1  christos 	return 0;
   6972       1.1  christos }
   6973       1.1  christos 
   6974       1.1  christos 
   6975       1.1  christos /* ------------------------------------------------------------------------ */
   6976       1.1  christos /* Function:    ipf_tune_del                                                */
   6977       1.1  christos /* Returns:     int - 0 == success, else failure                            */
   6978       1.1  christos /* Parameters:  oldtune - pointer to tune entry to remove from the list of  */
   6979       1.1  christos /*                        current dynamic tuneables                         */
   6980       1.1  christos /*                                                                          */
   6981       1.1  christos /* Search for the tune structure, by pointer, in the list of those that are */
   6982       1.1  christos /* dynamically added at run time.  If found, adjust the list so that this   */
   6983       1.1  christos /* structure is no longer part of it.                                       */
   6984       1.1  christos /* ------------------------------------------------------------------------ */
   6985       1.1  christos int
   6986       1.2  christos ipf_tune_del(ipf_main_softc_t *softc, ipftuneable_t *oldtune)
   6987       1.1  christos {
   6988       1.1  christos 	ipftuneable_t *ta, **tap;
   6989       1.1  christos 	int error = 0;
   6990       1.1  christos 
   6991       1.1  christos 	for (tap = &softc->ipf_tuners; (ta = *tap) != NULL;
   6992       1.1  christos 	     tap = &ta->ipft_next) {
   6993       1.1  christos 		if (ta == oldtune) {
   6994       1.1  christos 			*tap = oldtune->ipft_next;
   6995       1.1  christos 			oldtune->ipft_next = NULL;
   6996       1.1  christos 			break;
   6997       1.1  christos 		}
   6998       1.1  christos 	}
   6999       1.1  christos 
   7000       1.1  christos 	if (ta == NULL) {
   7001       1.1  christos 		error = ESRCH;
   7002       1.1  christos 		IPFERROR(75);
   7003       1.1  christos 	}
   7004       1.1  christos 	return error;
   7005       1.1  christos }
   7006       1.1  christos 
   7007       1.1  christos 
   7008       1.1  christos /* ------------------------------------------------------------------------ */
   7009       1.1  christos /* Function:    ipf_tune_del_array                                          */
   7010       1.1  christos /* Returns:     int - 0 == success, else failure                            */
   7011       1.1  christos /* Parameters:  oldtune - pointer to tuneables array                        */
   7012       1.1  christos /*                                                                          */
   7013       1.1  christos /* Remove each tuneable entry in the array from the list of "dynamic"       */
   7014       1.1  christos /* tunables.  If one entry should fail to be found, an error will be        */
   7015       1.1  christos /* returned and no further ones removed.                                    */
   7016       1.1  christos /* An entry with a NULL name is used as the indicator of the last entry in  */
   7017       1.1  christos /* the array.                                                               */
   7018       1.1  christos /* ------------------------------------------------------------------------ */
   7019       1.1  christos int
   7020       1.2  christos ipf_tune_del_array(ipf_main_softc_t *softc, ipftuneable_t *oldtune)
   7021       1.1  christos {
   7022       1.1  christos 	ipftuneable_t *ot;
   7023       1.1  christos 	int error = 0;
   7024       1.1  christos 
   7025       1.1  christos 	for (ot = oldtune; ot->ipft_name != NULL; ot++) {
   7026       1.1  christos 		error = ipf_tune_del(softc, ot);
   7027       1.1  christos 		if (error != 0)
   7028       1.1  christos 			break;
   7029       1.1  christos 	}
   7030       1.1  christos 
   7031       1.1  christos 	return error;
   7032       1.1  christos 
   7033       1.1  christos }
   7034       1.1  christos 
   7035       1.1  christos 
   7036       1.1  christos /* ------------------------------------------------------------------------ */
   7037       1.1  christos /* Function:    ipf_tune                                                    */
   7038       1.1  christos /* Returns:     int - 0 == success, else failure                            */
   7039       1.1  christos /* Parameters:  cmd(I)  - ioctl command number                              */
   7040       1.1  christos /*              data(I) - pointer to ioctl data structure                   */
   7041       1.1  christos /*                                                                          */
   7042       1.1  christos /* Implement handling of SIOCIPFGETNEXT, SIOCIPFGET and SIOCIPFSET.  These  */
   7043       1.1  christos /* three ioctls provide the means to access and control global variables    */
   7044       1.1  christos /* within IPFilter, allowing (for example) timeouts and table sizes to be   */
   7045       1.1  christos /* changed without rebooting, reloading or recompiling.  The initialisation */
   7046       1.1  christos /* and 'destruction' routines of the various components of ipfilter are all */
   7047       1.1  christos /* each responsible for handling their own values being too big.            */
   7048       1.1  christos /* ------------------------------------------------------------------------ */
   7049       1.1  christos int
   7050       1.2  christos ipf_ipftune(ipf_main_softc_t *softc, ioctlcmd_t cmd, void *data)
   7051       1.1  christos {
   7052       1.1  christos 	ipftuneable_t *ta;
   7053       1.1  christos 	ipftune_t tu;
   7054       1.1  christos 	void *cookie;
   7055       1.1  christos 	int error;
   7056       1.1  christos 
   7057       1.1  christos 	error = ipf_inobj(softc, data, NULL, &tu, IPFOBJ_TUNEABLE);
   7058       1.1  christos 	if (error != 0)
   7059       1.1  christos 		return error;
   7060       1.1  christos 
   7061       1.1  christos 	tu.ipft_name[sizeof(tu.ipft_name) - 1] = '\0';
   7062       1.1  christos 	cookie = tu.ipft_cookie;
   7063       1.1  christos 	ta = NULL;
   7064       1.1  christos 
   7065       1.1  christos 	switch (cmd)
   7066       1.1  christos 	{
   7067       1.1  christos 	case SIOCIPFGETNEXT :
   7068       1.1  christos 		/*
   7069       1.1  christos 		 * If cookie is non-NULL, assume it to be a pointer to the last
   7070       1.1  christos 		 * entry we looked at, so find it (if possible) and return a
   7071       1.1  christos 		 * pointer to the next one after it.  The last entry in the
   7072       1.1  christos 		 * the table is a NULL entry, so when we get to it, set cookie
   7073       1.1  christos 		 * to NULL and return that, indicating end of list, erstwhile
   7074       1.1  christos 		 * if we come in with cookie set to NULL, we are starting anew
   7075       1.1  christos 		 * at the front of the list.
   7076       1.1  christos 		 */
   7077       1.1  christos 		if (cookie != NULL) {
   7078       1.1  christos 			ta = ipf_tune_findbycookie(&softc->ipf_tuners,
   7079       1.1  christos 						   cookie, &tu.ipft_cookie);
   7080       1.1  christos 		} else {
   7081       1.1  christos 			ta = softc->ipf_tuners;
   7082       1.1  christos 			tu.ipft_cookie = ta + 1;
   7083       1.1  christos 		}
   7084       1.1  christos 		if (ta != NULL) {
   7085       1.1  christos 			/*
   7086       1.1  christos 			 * Entry found, but does the data pointed to by that
   7087       1.1  christos 			 * row fit in what we can return?
   7088       1.1  christos 			 */
   7089       1.1  christos 			if (ta->ipft_sz > sizeof(tu.ipft_un)) {
   7090       1.1  christos 				IPFERROR(76);
   7091       1.1  christos 				return EINVAL;
   7092       1.1  christos 			}
   7093       1.1  christos 
   7094       1.1  christos 			tu.ipft_vlong = 0;
   7095       1.1  christos 			if (ta->ipft_sz == sizeof(u_long))
   7096       1.1  christos 				tu.ipft_vlong = *ta->ipft_plong;
   7097       1.1  christos 			else if (ta->ipft_sz == sizeof(u_int))
   7098       1.1  christos 				tu.ipft_vint = *ta->ipft_pint;
   7099       1.1  christos 			else if (ta->ipft_sz == sizeof(u_short))
   7100       1.1  christos 				tu.ipft_vshort = *ta->ipft_pshort;
   7101       1.1  christos 			else if (ta->ipft_sz == sizeof(u_char))
   7102       1.1  christos 				tu.ipft_vchar = *ta->ipft_pchar;
   7103       1.1  christos 
   7104       1.1  christos 			tu.ipft_sz = ta->ipft_sz;
   7105       1.1  christos 			tu.ipft_min = ta->ipft_min;
   7106       1.1  christos 			tu.ipft_max = ta->ipft_max;
   7107       1.1  christos 			tu.ipft_flags = ta->ipft_flags;
   7108       1.1  christos 			bcopy(ta->ipft_name, tu.ipft_name,
   7109       1.1  christos 			      MIN(sizeof(tu.ipft_name),
   7110       1.1  christos 				  strlen(ta->ipft_name) + 1));
   7111       1.1  christos 		}
   7112       1.1  christos 		error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
   7113       1.1  christos 		break;
   7114       1.1  christos 
   7115       1.1  christos 	case SIOCIPFGET :
   7116       1.1  christos 	case SIOCIPFSET :
   7117       1.1  christos 		/*
   7118       1.1  christos 		 * Search by name or by cookie value for a particular entry
   7119       1.1  christos 		 * in the tuning paramter table.
   7120       1.1  christos 		 */
   7121       1.1  christos 		IPFERROR(77);
   7122       1.1  christos 		error = ESRCH;
   7123       1.1  christos 		if (cookie != NULL) {
   7124       1.1  christos 			ta = ipf_tune_findbycookie(&softc->ipf_tuners,
   7125       1.1  christos 						   cookie, NULL);
   7126       1.1  christos 			if (ta != NULL)
   7127       1.1  christos 				error = 0;
   7128       1.1  christos 		} else if (tu.ipft_name[0] != '\0') {
   7129       1.1  christos 			ta = ipf_tune_findbyname(softc->ipf_tuners,
   7130       1.1  christos 						 tu.ipft_name);
   7131       1.1  christos 			if (ta != NULL)
   7132       1.1  christos 				error = 0;
   7133       1.1  christos 		}
   7134       1.1  christos 		if (error != 0)
   7135       1.1  christos 			break;
   7136       1.1  christos 
   7137       1.1  christos 		if (cmd == (ioctlcmd_t)SIOCIPFGET) {
   7138       1.1  christos 			/*
   7139       1.1  christos 			 * Fetch the tuning parameters for a particular value
   7140       1.1  christos 			 */
   7141       1.1  christos 			tu.ipft_vlong = 0;
   7142       1.1  christos 			if (ta->ipft_sz == sizeof(u_long))
   7143       1.1  christos 				tu.ipft_vlong = *ta->ipft_plong;
   7144       1.1  christos 			else if (ta->ipft_sz == sizeof(u_int))
   7145       1.1  christos 				tu.ipft_vint = *ta->ipft_pint;
   7146       1.1  christos 			else if (ta->ipft_sz == sizeof(u_short))
   7147       1.1  christos 				tu.ipft_vshort = *ta->ipft_pshort;
   7148       1.1  christos 			else if (ta->ipft_sz == sizeof(u_char))
   7149       1.1  christos 				tu.ipft_vchar = *ta->ipft_pchar;
   7150       1.1  christos 			tu.ipft_cookie = ta;
   7151       1.1  christos 			tu.ipft_sz = ta->ipft_sz;
   7152       1.1  christos 			tu.ipft_min = ta->ipft_min;
   7153       1.1  christos 			tu.ipft_max = ta->ipft_max;
   7154       1.1  christos 			tu.ipft_flags = ta->ipft_flags;
   7155       1.1  christos 			error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
   7156       1.1  christos 
   7157       1.1  christos 		} else if (cmd == (ioctlcmd_t)SIOCIPFSET) {
   7158       1.1  christos 			/*
   7159       1.1  christos 			 * Set an internal parameter.  The hard part here is
   7160       1.1  christos 			 * getting the new value safely and correctly out of
   7161       1.1  christos 			 * the kernel (given we only know its size, not type.)
   7162       1.1  christos 			 */
   7163       1.1  christos 			u_long in;
   7164       1.1  christos 
   7165       1.1  christos 			if (((ta->ipft_flags & IPFT_WRDISABLED) != 0) &&
   7166       1.1  christos 			    (softc->ipf_running > 0)) {
   7167       1.1  christos 				IPFERROR(78);
   7168       1.1  christos 				error = EBUSY;
   7169       1.1  christos 				break;
   7170       1.1  christos 			}
   7171       1.1  christos 
   7172       1.1  christos 			in = tu.ipft_vlong;
   7173       1.1  christos 			if (in < ta->ipft_min || in > ta->ipft_max) {
   7174       1.1  christos 				IPFERROR(79);
   7175       1.1  christos 				error = EINVAL;
   7176       1.1  christos 				break;
   7177       1.1  christos 			}
   7178       1.1  christos 
   7179       1.1  christos 			if (ta->ipft_func != NULL) {
   7180       1.1  christos 				SPL_INT(s);
   7181       1.1  christos 
   7182       1.1  christos 				SPL_NET(s);
   7183       1.1  christos 				error = (*ta->ipft_func)(softc, ta,
   7184       1.1  christos 							 &tu.ipft_un);
   7185       1.1  christos 				SPL_X(s);
   7186       1.1  christos 
   7187       1.1  christos 			} else if (ta->ipft_sz == sizeof(u_long)) {
   7188       1.1  christos 				tu.ipft_vlong = *ta->ipft_plong;
   7189       1.1  christos 				*ta->ipft_plong = in;
   7190       1.1  christos 
   7191       1.1  christos 			} else if (ta->ipft_sz == sizeof(u_int)) {
   7192       1.1  christos 				tu.ipft_vint = *ta->ipft_pint;
   7193       1.1  christos 				*ta->ipft_pint = (u_int)(in & 0xffffffff);
   7194       1.1  christos 
   7195       1.1  christos 			} else if (ta->ipft_sz == sizeof(u_short)) {
   7196       1.1  christos 				tu.ipft_vshort = *ta->ipft_pshort;
   7197       1.1  christos 				*ta->ipft_pshort = (u_short)(in & 0xffff);
   7198       1.1  christos 
   7199       1.1  christos 			} else if (ta->ipft_sz == sizeof(u_char)) {
   7200       1.1  christos 				tu.ipft_vchar = *ta->ipft_pchar;
   7201       1.1  christos 				*ta->ipft_pchar = (u_char)(in & 0xff);
   7202       1.1  christos 			}
   7203       1.1  christos 			error = ipf_outobj(softc, data, &tu, IPFOBJ_TUNEABLE);
   7204       1.1  christos 		}
   7205       1.1  christos 		break;
   7206       1.1  christos 
   7207       1.1  christos 	default :
   7208       1.1  christos 		IPFERROR(80);
   7209       1.1  christos 		error = EINVAL;
   7210       1.1  christos 		break;
   7211       1.1  christos 	}
   7212       1.1  christos 
   7213       1.1  christos 	return error;
   7214       1.1  christos }
   7215       1.1  christos 
   7216       1.1  christos 
   7217       1.1  christos /* ------------------------------------------------------------------------ */
   7218       1.1  christos /* Function:    ipf_zerostats                                               */
   7219       1.1  christos /* Returns:     int - 0 = success, else failure                             */
   7220       1.1  christos /* Parameters:  data(O) - pointer to pointer for copying data back to       */
   7221       1.1  christos /*                                                                          */
   7222       1.1  christos /* Copies the current statistics out to userspace and then zero's the       */
   7223       1.1  christos /* current ones in the kernel. The lock is only held across the bzero() as  */
   7224       1.1  christos /* the copyout may result in paging (ie network activity.)                  */
   7225       1.1  christos /* ------------------------------------------------------------------------ */
   7226       1.1  christos int
   7227       1.2  christos ipf_zerostats(ipf_main_softc_t *softc, void *data)
   7228       1.1  christos {
   7229       1.1  christos 	friostat_t fio;
   7230       1.1  christos 	ipfobj_t obj;
   7231       1.1  christos 	int error;
   7232       1.1  christos 
   7233       1.1  christos 	error = ipf_inobj(softc, data, &obj, &fio, IPFOBJ_IPFSTAT);
   7234       1.1  christos 	if (error != 0)
   7235       1.1  christos 		return error;
   7236       1.1  christos 	ipf_getstat(softc, &fio, obj.ipfo_rev);
   7237       1.1  christos 	error = ipf_outobj(softc, data, &fio, IPFOBJ_IPFSTAT);
   7238       1.1  christos 	if (error != 0)
   7239       1.1  christos 		return error;
   7240       1.1  christos 
   7241       1.1  christos 	WRITE_ENTER(&softc->ipf_mutex);
   7242       1.1  christos 	bzero(&softc->ipf_stats, sizeof(softc->ipf_stats));
   7243       1.1  christos 	RWLOCK_EXIT(&softc->ipf_mutex);
   7244       1.1  christos 
   7245       1.1  christos 	return 0;
   7246       1.1  christos }
   7247       1.1  christos 
   7248       1.1  christos 
   7249       1.1  christos /* ------------------------------------------------------------------------ */
   7250       1.1  christos /* Function:    ipf_resolvedest                                             */
   7251       1.1  christos /* Returns:     Nil                                                         */
   7252       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   7253       1.1  christos /*              base(I)  - where strings are stored                         */
   7254       1.1  christos /*              fdp(IO)  - pointer to destination information to resolve    */
   7255       1.1  christos /*              v(I)     - IP protocol version to match                     */
   7256       1.1  christos /*                                                                          */
   7257       1.1  christos /* Looks up an interface name in the frdest structure pointed to by fdp and */
   7258       1.1  christos /* if a matching name can be found for the particular IP protocol version   */
   7259       1.1  christos /* then store the interface pointer in the frdest struct.  If no match is   */
   7260       1.1  christos /* found, then set the interface pointer to be -1 as NULL is considered to  */
   7261       1.1  christos /* indicate there is no information at all in the structure.                */
   7262       1.1  christos /* ------------------------------------------------------------------------ */
   7263       1.1  christos int
   7264       1.2  christos ipf_resolvedest(ipf_main_softc_t *softc, char *base, frdest_t *fdp, int v)
   7265       1.1  christos {
   7266       1.1  christos 	int errval = 0;
   7267       1.1  christos 	void *ifp;
   7268       1.1  christos 
   7269       1.1  christos 	ifp = NULL;
   7270       1.1  christos 
   7271       1.1  christos 	if (fdp->fd_name != -1) {
   7272       1.1  christos 		if (fdp->fd_type == FRD_DSTLIST) {
   7273       1.1  christos 			ifp = ipf_lookup_res_name(softc, IPL_LOGIPF,
   7274       1.1  christos 						  IPLT_DSTLIST,
   7275       1.1  christos 						  base + fdp->fd_name,
   7276       1.1  christos 						  NULL);
   7277       1.1  christos 			if (ifp == NULL) {
   7278       1.1  christos 				IPFERROR(144);
   7279       1.1  christos 				errval = ESRCH;
   7280       1.1  christos 			}
   7281       1.1  christos 		} else {
   7282       1.1  christos 			ifp = GETIFP(base + fdp->fd_name, v);
   7283       1.1  christos 			if (ifp == NULL)
   7284       1.1  christos 				ifp = (void *)-1;
   7285       1.2  christos 			if ((ifp != NULL) && (ifp != (void *)-1))
   7286       1.2  christos 				fdp->fd_local = ipf_deliverlocal(softc, v, ifp,
   7287       1.2  christos 								 &fdp->fd_ip6);
   7288       1.1  christos 		}
   7289       1.1  christos 	}
   7290       1.1  christos 	fdp->fd_ptr = ifp;
   7291       1.1  christos 
   7292       1.1  christos 	return errval;
   7293       1.1  christos }
   7294       1.1  christos 
   7295       1.1  christos 
   7296       1.1  christos /* ------------------------------------------------------------------------ */
   7297       1.1  christos /* Function:    ipf_resolvenic                                              */
   7298       1.1  christos /* Returns:     void* - NULL = wildcard name, -1 = failed to find NIC, else */
   7299       1.1  christos /*                      pointer to interface structure for NIC              */
   7300       1.1  christos /* Parameters:  softc(I)- pointer to soft context main structure            */
   7301       1.1  christos /*              name(I) - complete interface name                           */
   7302       1.1  christos /*              v(I)    - IP protocol version                               */
   7303       1.1  christos /*                                                                          */
   7304       1.1  christos /* Look for a network interface structure that firstly has a matching name  */
   7305       1.1  christos /* to that passed in and that is also being used for that IP protocol       */
   7306       1.1  christos /* version (necessary on some platforms where there are separate listings   */
   7307       1.1  christos /* for both IPv4 and IPv6 on the same physical NIC.                         */
   7308       1.2  christos /*                                                                          */
   7309       1.1  christos /* ------------------------------------------------------------------------ */
   7310       1.1  christos void *
   7311       1.2  christos ipf_resolvenic(ipf_main_softc_t *softc, char *name, int v)
   7312       1.1  christos {
   7313       1.1  christos 	void *nic;
   7314       1.1  christos 
   7315       1.3   darrenr 	softc = softc;	/* gcc -Wextra */
   7316       1.1  christos 	if (name[0] == '\0')
   7317       1.1  christos 		return NULL;
   7318       1.1  christos 
   7319       1.1  christos 	if ((name[1] == '\0') && ((name[0] == '-') || (name[0] == '*'))) {
   7320       1.1  christos 		return NULL;
   7321       1.1  christos 	}
   7322       1.1  christos 
   7323       1.1  christos 	nic = GETIFP(name, v);
   7324       1.1  christos 	if (nic == NULL)
   7325       1.1  christos 		nic = (void *)-1;
   7326       1.1  christos 	return nic;
   7327       1.1  christos }
   7328       1.1  christos 
   7329       1.1  christos 
   7330       1.1  christos /* ------------------------------------------------------------------------ */
   7331       1.1  christos /* Function:    ipf_token_expire                                            */
   7332       1.1  christos /* Returns:     None.                                                       */
   7333       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   7334       1.1  christos /*                                                                          */
   7335       1.1  christos /* This function is run every ipf tick to see if there are any tokens that  */
   7336       1.1  christos /* have been held for too long and need to be freed up.                     */
   7337       1.1  christos /* ------------------------------------------------------------------------ */
   7338       1.1  christos void
   7339       1.2  christos ipf_token_expire(ipf_main_softc_t *softc)
   7340       1.1  christos {
   7341       1.1  christos 	ipftoken_t *it;
   7342       1.1  christos 
   7343       1.1  christos 	WRITE_ENTER(&softc->ipf_tokens);
   7344       1.1  christos 	while ((it = softc->ipf_token_head) != NULL) {
   7345       1.1  christos 		if (it->ipt_die > softc->ipf_ticks)
   7346       1.1  christos 			break;
   7347       1.1  christos 
   7348       1.3   darrenr 		ipf_token_deref(softc, it);
   7349       1.3   darrenr 	}
   7350       1.3   darrenr 	RWLOCK_EXIT(&softc->ipf_tokens);
   7351       1.3   darrenr }
   7352       1.3   darrenr 
   7353       1.3   darrenr 
   7354       1.3   darrenr /* ------------------------------------------------------------------------ */
   7355       1.3   darrenr /* Function:    ipf_token_flush                                             */
   7356       1.3   darrenr /* Returns:     None.                                                       */
   7357       1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   7358       1.3   darrenr /*                                                                          */
   7359       1.3   darrenr /* Loop through all of the existing tokens and call deref to see if they    */
   7360       1.3   darrenr /* can be freed. Normally a function like this might just loop on           */
   7361       1.3   darrenr /* ipf_token_head but there is a chance that a token might have a ref count */
   7362       1.3   darrenr /* of greater than one and in that case the the reference would drop twice  */
   7363       1.3   darrenr /* by code that is only entitled to drop it once.                           */
   7364       1.3   darrenr /* ------------------------------------------------------------------------ */
   7365       1.3   darrenr static void
   7366       1.4   darrenr ipf_token_flush(ipf_main_softc_t *softc)
   7367       1.3   darrenr {
   7368       1.3   darrenr 	ipftoken_t *it, *next;
   7369       1.3   darrenr 
   7370       1.3   darrenr 	WRITE_ENTER(&softc->ipf_tokens);
   7371       1.3   darrenr 	for (it = softc->ipf_token_head; it != NULL; it = next) {
   7372       1.3   darrenr 		next = it->ipt_next;
   7373       1.3   darrenr 		(void) ipf_token_deref(softc, it);
   7374       1.1  christos 	}
   7375       1.1  christos 	RWLOCK_EXIT(&softc->ipf_tokens);
   7376       1.1  christos }
   7377       1.1  christos 
   7378       1.1  christos 
   7379       1.1  christos /* ------------------------------------------------------------------------ */
   7380       1.3   darrenr /* Function:    ipf_token_del                                               */
   7381       1.1  christos /* Returns:     int     - 0 = success, else error                           */
   7382       1.3   darrenr /* Parameters:  softc(I)- pointer to soft context main structure            */
   7383       1.1  christos /*              type(I) - the token type to match                           */
   7384       1.1  christos /*              uid(I)  - uid owning the token                              */
   7385       1.1  christos /*              ptr(I)  - context pointer for the token                     */
   7386       1.1  christos /*                                                                          */
   7387       1.1  christos /* This function looks for a a token in the current list that matches up    */
   7388       1.1  christos /* the fields (type, uid, ptr).  If none is found, ESRCH is returned, else  */
   7389       1.3   darrenr /* call ipf_token_dewref() to remove it from the list. In the event that    */
   7390       1.3   darrenr /* the token has a reference held elsewhere, setting ipt_complete to 2      */
   7391       1.3   darrenr /* enables debugging to distinguish between the two paths that ultimately   */
   7392       1.3   darrenr /* lead to a token to be deleted.                                           */
   7393       1.1  christos /* ------------------------------------------------------------------------ */
   7394       1.1  christos int
   7395       1.2  christos ipf_token_del(ipf_main_softc_t *softc, int type, int uid, void *ptr)
   7396       1.1  christos {
   7397       1.1  christos 	ipftoken_t *it;
   7398       1.1  christos 	int error;
   7399       1.1  christos 
   7400       1.1  christos 	IPFERROR(82);
   7401       1.1  christos 	error = ESRCH;
   7402       1.1  christos 
   7403       1.1  christos 	WRITE_ENTER(&softc->ipf_tokens);
   7404       1.3   darrenr 	for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) {
   7405       1.1  christos 		if (ptr == it->ipt_ctx && type == it->ipt_type &&
   7406       1.1  christos 		    uid == it->ipt_uid) {
   7407       1.3   darrenr 			it->ipt_complete = 2;
   7408       1.3   darrenr 			ipf_token_deref(softc, it);
   7409       1.1  christos 			error = 0;
   7410       1.1  christos 			break;
   7411       1.3   darrenr 		}
   7412       1.1  christos 	}
   7413       1.1  christos 	RWLOCK_EXIT(&softc->ipf_tokens);
   7414       1.1  christos 
   7415       1.1  christos 	return error;
   7416       1.1  christos }
   7417       1.1  christos 
   7418       1.1  christos 
   7419       1.1  christos /* ------------------------------------------------------------------------ */
   7420       1.1  christos /* Function:    ipf_token_mark_complete                                     */
   7421       1.1  christos /* Returns:     None.                                                       */
   7422       1.1  christos /* Parameters:  token(I) - pointer to token structure                       */
   7423       1.1  christos /*                                                                          */
   7424       1.3   darrenr /* Mark a token as being ineligable for being found with ipf_token_find.    */
   7425       1.1  christos /* ------------------------------------------------------------------------ */
   7426       1.1  christos void
   7427       1.2  christos ipf_token_mark_complete(ipftoken_t *token)
   7428       1.1  christos {
   7429       1.3   darrenr 	if (token->ipt_complete == 0)
   7430       1.3   darrenr 		token->ipt_complete = 1;
   7431       1.1  christos }
   7432       1.1  christos 
   7433       1.1  christos 
   7434       1.1  christos /* ------------------------------------------------------------------------ */
   7435       1.1  christos /* Function:    ipf_token_find                                               */
   7436       1.1  christos /* Returns:     ipftoken_t * - NULL if no memory, else pointer to token     */
   7437       1.1  christos /* Parameters:  softc(I)- pointer to soft context main structure            */
   7438       1.1  christos /*              type(I) - the token type to match                           */
   7439       1.1  christos /*              uid(I)  - uid owning the token                              */
   7440       1.1  christos /*              ptr(I)  - context pointer for the token                     */
   7441       1.1  christos /*                                                                          */
   7442       1.1  christos /* This function looks for a live token in the list of current tokens that  */
   7443       1.1  christos /* matches the tuple (type, uid, ptr).  If one cannot be found then one is  */
   7444       1.1  christos /* allocated.  If one is found then it is moved to the top of the list of   */
   7445       1.1  christos /* currently active tokens.                                                 */
   7446       1.1  christos /* ------------------------------------------------------------------------ */
   7447       1.1  christos ipftoken_t *
   7448       1.2  christos ipf_token_find(ipf_main_softc_t *softc, int type, int uid, void *ptr)
   7449       1.1  christos {
   7450       1.1  christos 	ipftoken_t *it, *new;
   7451       1.1  christos 
   7452       1.1  christos 	KMALLOC(new, ipftoken_t *);
   7453       1.3   darrenr 	if (new != NULL)
   7454       1.3   darrenr 		bzero((char *)new, sizeof(*new));
   7455       1.1  christos 
   7456       1.1  christos 	WRITE_ENTER(&softc->ipf_tokens);
   7457       1.1  christos 	for (it = softc->ipf_token_head; it != NULL; it = it->ipt_next) {
   7458       1.3   darrenr 		if ((ptr == it->ipt_ctx) && (type == it->ipt_type) &&
   7459       1.3   darrenr 		    (uid == it->ipt_uid) && (it->ipt_complete < 2))
   7460       1.1  christos 			break;
   7461       1.1  christos 	}
   7462       1.1  christos 
   7463       1.1  christos 	if (it == NULL) {
   7464       1.1  christos 		it = new;
   7465       1.1  christos 		new = NULL;
   7466       1.1  christos 		if (it == NULL) {
   7467       1.1  christos 			RWLOCK_EXIT(&softc->ipf_tokens);
   7468       1.1  christos 			return NULL;
   7469       1.1  christos 		}
   7470       1.1  christos 		it->ipt_ctx = ptr;
   7471       1.1  christos 		it->ipt_uid = uid;
   7472       1.1  christos 		it->ipt_type = type;
   7473       1.3   darrenr 		it->ipt_ref = 1;
   7474       1.1  christos 	} else {
   7475       1.1  christos 		if (new != NULL) {
   7476       1.1  christos 			KFREE(new);
   7477       1.1  christos 			new = NULL;
   7478       1.1  christos 		}
   7479       1.1  christos 
   7480       1.3   darrenr 		if (it->ipt_complete > 0)
   7481       1.3   darrenr 			it = NULL;
   7482       1.3   darrenr 		else
   7483       1.3   darrenr 			ipf_token_unlink(softc, it);
   7484       1.1  christos 	}
   7485       1.1  christos 
   7486       1.3   darrenr 	if (it != NULL) {
   7487       1.1  christos 		it->ipt_pnext = softc->ipf_token_tail;
   7488       1.1  christos 		*softc->ipf_token_tail = it;
   7489       1.1  christos 		softc->ipf_token_tail = &it->ipt_next;
   7490       1.1  christos 		it->ipt_next = NULL;
   7491       1.3   darrenr 		it->ipt_ref++;
   7492       1.1  christos 
   7493       1.1  christos 		it->ipt_die = softc->ipf_ticks + 20;
   7494       1.1  christos 	}
   7495       1.1  christos 
   7496       1.1  christos 	RWLOCK_EXIT(&softc->ipf_tokens);
   7497       1.1  christos 
   7498       1.1  christos 	return it;
   7499       1.1  christos }
   7500       1.1  christos 
   7501       1.1  christos 
   7502       1.1  christos /* ------------------------------------------------------------------------ */
   7503       1.1  christos /* Function:    ipf_token_unlink                                            */
   7504       1.1  christos /* Returns:     None.                                                       */
   7505       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   7506       1.1  christos /*              token(I) - pointer to token structure                       */
   7507       1.1  christos /* Write Locks: ipf_tokens                                                  */
   7508       1.1  christos /*                                                                          */
   7509       1.1  christos /* This function unlinks a token structure from the linked list of tokens   */
   7510       1.1  christos /* that "own" it.  The head pointer never needs to be explicitly adjusted   */
   7511       1.1  christos /* but the tail does due to the linked list implementation.                 */
   7512       1.1  christos /* ------------------------------------------------------------------------ */
   7513       1.1  christos static void
   7514       1.2  christos ipf_token_unlink(ipf_main_softc_t *softc, ipftoken_t *token)
   7515       1.1  christos {
   7516       1.1  christos 
   7517       1.1  christos 	if (softc->ipf_token_tail == &token->ipt_next)
   7518       1.1  christos 		softc->ipf_token_tail = token->ipt_pnext;
   7519       1.1  christos 
   7520       1.1  christos 	*token->ipt_pnext = token->ipt_next;
   7521       1.1  christos 	if (token->ipt_next != NULL)
   7522       1.1  christos 		token->ipt_next->ipt_pnext = token->ipt_pnext;
   7523       1.3   darrenr 	token->ipt_next = NULL;
   7524       1.3   darrenr 	token->ipt_pnext = NULL;
   7525       1.1  christos }
   7526       1.1  christos 
   7527       1.1  christos 
   7528       1.1  christos /* ------------------------------------------------------------------------ */
   7529       1.1  christos /* Function:    ipf_token_deref                                             */
   7530       1.3   darrenr /* Returns:     int      - 0 == token freed, else reference count           */
   7531       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   7532       1.1  christos /*              token(I) - pointer to token structure                       */
   7533       1.1  christos /* Write Locks: ipf_tokens                                                  */
   7534       1.1  christos /*                                                                          */
   7535       1.1  christos /* Drop the reference count on the token structure and if it drops to zero, */
   7536       1.1  christos /* call the dereference function for the token type because it is then      */
   7537       1.1  christos /* possible to free the token data structure.                               */
   7538       1.1  christos /* ------------------------------------------------------------------------ */
   7539       1.3   darrenr int
   7540       1.2  christos ipf_token_deref(ipf_main_softc_t *softc, ipftoken_t *token)
   7541       1.1  christos {
   7542       1.1  christos 	void *data, **datap;
   7543       1.1  christos 
   7544       1.3   darrenr 	ASSERT(token->ipt_ref > 0);
   7545       1.1  christos 	token->ipt_ref--;
   7546       1.1  christos 	if (token->ipt_ref > 0)
   7547       1.3   darrenr 		return token->ipt_ref;
   7548       1.1  christos 
   7549       1.1  christos 	data = token->ipt_data;
   7550       1.1  christos 	datap = &data;
   7551       1.1  christos 
   7552       1.1  christos 	if ((data != NULL) && (data != (void *)-1)) {
   7553       1.1  christos 		switch (token->ipt_type)
   7554       1.1  christos 		{
   7555       1.1  christos 		case IPFGENITER_IPF :
   7556       1.1  christos 			(void) ipf_derefrule(softc, (frentry_t **)datap);
   7557       1.1  christos 			break;
   7558       1.1  christos 		case IPFGENITER_IPNAT :
   7559       1.1  christos 			WRITE_ENTER(&softc->ipf_nat);
   7560       1.3   darrenr 			ipf_nat_rule_deref(softc, (ipnat_t **)datap);
   7561       1.1  christos 			RWLOCK_EXIT(&softc->ipf_nat);
   7562       1.1  christos 			break;
   7563       1.1  christos 		case IPFGENITER_NAT :
   7564       1.1  christos 			ipf_nat_deref(softc, (nat_t **)datap);
   7565       1.1  christos 			break;
   7566       1.1  christos 		case IPFGENITER_STATE :
   7567       1.1  christos 			ipf_state_deref(softc, (ipstate_t **)datap);
   7568       1.1  christos 			break;
   7569       1.1  christos 		case IPFGENITER_FRAG :
   7570       1.1  christos 			ipf_frag_pkt_deref(softc, (ipfr_t **)datap);
   7571       1.1  christos 			break;
   7572       1.1  christos 		case IPFGENITER_NATFRAG :
   7573       1.1  christos 			ipf_frag_nat_deref(softc, (ipfr_t **)datap);
   7574       1.1  christos 			break;
   7575       1.1  christos 		case IPFGENITER_HOSTMAP :
   7576       1.1  christos 			WRITE_ENTER(&softc->ipf_nat);
   7577       1.3   darrenr 			ipf_nat_hostmapdel(softc, (hostmap_t **)datap);
   7578       1.1  christos 			RWLOCK_EXIT(&softc->ipf_nat);
   7579       1.1  christos 			break;
   7580       1.1  christos 		default :
   7581       1.1  christos 			ipf_lookup_iterderef(softc, token->ipt_type, data);
   7582       1.1  christos 			break;
   7583       1.1  christos 		}
   7584       1.1  christos 	}
   7585       1.1  christos 
   7586       1.3   darrenr 	ipf_token_unlink(softc, token);
   7587       1.1  christos 	KFREE(token);
   7588       1.3   darrenr 	return 0;
   7589       1.1  christos }
   7590       1.1  christos 
   7591       1.1  christos 
   7592       1.1  christos /* ------------------------------------------------------------------------ */
   7593       1.3   darrenr /* Function:    ipf_nextrule                                                */
   7594       1.3   darrenr /* Returns:     frentry_t * - NULL == no more rules, else pointer to next   */
   7595       1.3   darrenr /* Parameters:  softc(I)    - pointer to soft context main structure        */
   7596       1.3   darrenr /*              fr(I)       - pointer to filter rule                        */
   7597       1.3   darrenr /*              out(I)      - 1 == out rules, 0 == input rules              */
   7598       1.1  christos /*                                                                          */
   7599       1.3   darrenr /* Starting with "fr", find the next rule to visit. This includes visiting  */
   7600       1.3   darrenr /* the list of rule groups if either fr is NULL (empty list) or it is the   */
   7601       1.3   darrenr /* last rule in the list. When walking rule lists, it is either input or    */
   7602       1.3   darrenr /* output rules that are returned, never both.                              */
   7603       1.1  christos /* ------------------------------------------------------------------------ */
   7604       1.3   darrenr static frentry_t *
   7605       1.3   darrenr ipf_nextrule(ipf_main_softc_t *softc, int active, int unit,
   7606       1.3   darrenr     frentry_t *fr, int out)
   7607       1.1  christos {
   7608       1.3   darrenr 	frentry_t *next;
   7609       1.3   darrenr 	frgroup_t *fg;
   7610       1.3   darrenr 
   7611       1.3   darrenr 	if (fr != NULL && fr->fr_group != -1) {
   7612       1.3   darrenr 		fg = ipf_findgroup(softc, fr->fr_names + fr->fr_group,
   7613       1.3   darrenr 				   unit, active, NULL);
   7614       1.3   darrenr 		if (fg != NULL)
   7615       1.3   darrenr 			fg = fg->fg_next;
   7616       1.3   darrenr 	} else {
   7617       1.3   darrenr 		fg = softc->ipf_groups[unit][active];
   7618       1.3   darrenr 	}
   7619       1.1  christos 
   7620       1.3   darrenr 	while (fg != NULL) {
   7621       1.3   darrenr 		next = fg->fg_start;
   7622       1.3   darrenr 		while (next != NULL) {
   7623       1.3   darrenr 			if (out) {
   7624       1.3   darrenr 				if (next->fr_flags & FR_OUTQUE)
   7625       1.3   darrenr 					return next;
   7626       1.3   darrenr 			} else if (next->fr_flags & FR_INQUE) {
   7627       1.3   darrenr 				return next;
   7628       1.3   darrenr 			}
   7629       1.3   darrenr 			next = next->fr_next;
   7630       1.3   darrenr 		}
   7631       1.3   darrenr 		if (next == NULL)
   7632       1.3   darrenr 			fg = fg->fg_next;
   7633       1.3   darrenr 	}
   7634       1.1  christos 
   7635       1.3   darrenr 	return NULL;
   7636       1.1  christos }
   7637       1.1  christos 
   7638       1.1  christos /* ------------------------------------------------------------------------ */
   7639       1.1  christos /* Function:    ipf_getnextrule                                             */
   7640       1.1  christos /* Returns:     int - 0 = success, else error                               */
   7641       1.1  christos /* Parameters:  softc(I)- pointer to soft context main structure            */
   7642       1.1  christos /*              t(I)   - pointer to destination information to resolve      */
   7643       1.1  christos /*              ptr(I) - pointer to ipfobj_t to copyin from user space      */
   7644       1.1  christos /*                                                                          */
   7645       1.1  christos /* This function's first job is to bring in the ipfruleiter_t structure via */
   7646       1.1  christos /* the ipfobj_t structure to determine what should be the next rule to      */
   7647       1.1  christos /* return. Once the ipfruleiter_t has been brought in, it then tries to     */
   7648       1.1  christos /* find the 'next rule'.  This may include searching rule group lists or    */
   7649       1.1  christos /* just be as simple as looking at the 'next' field in the rule structure.  */
   7650       1.1  christos /* When we have found the rule to return, increase its reference count and  */
   7651       1.1  christos /* if we used an existing rule to get here, decrease its reference count.   */
   7652       1.1  christos /* ------------------------------------------------------------------------ */
   7653       1.1  christos int
   7654       1.2  christos ipf_getnextrule(ipf_main_softc_t *softc, ipftoken_t *t, void *ptr)
   7655       1.1  christos {
   7656       1.1  christos 	frentry_t *fr, *next, zero;
   7657       1.1  christos 	ipfruleiter_t it;
   7658       1.1  christos 	int error, out;
   7659       1.1  christos 	frgroup_t *fg;
   7660       1.1  christos 	ipfobj_t obj;
   7661       1.3   darrenr 	int predict;
   7662       1.1  christos 	char *dst;
   7663       1.3   darrenr 	int unit;
   7664       1.1  christos 
   7665       1.1  christos 	if (t == NULL || ptr == NULL) {
   7666       1.1  christos 		IPFERROR(84);
   7667       1.1  christos 		return EFAULT;
   7668       1.1  christos 	}
   7669       1.1  christos 
   7670       1.1  christos 	error = ipf_inobj(softc, ptr, &obj, &it, IPFOBJ_IPFITER);
   7671       1.1  christos 	if (error != 0)
   7672       1.1  christos 		return error;
   7673       1.1  christos 
   7674       1.1  christos 	if ((it.iri_inout < 0) || (it.iri_inout > 3)) {
   7675       1.1  christos 		IPFERROR(85);
   7676       1.1  christos 		return EINVAL;
   7677       1.1  christos 	}
   7678       1.1  christos 	if ((it.iri_active != 0) && (it.iri_active != 1)) {
   7679       1.1  christos 		IPFERROR(86);
   7680       1.1  christos 		return EINVAL;
   7681       1.1  christos 	}
   7682       1.1  christos 	if (it.iri_nrules == 0) {
   7683       1.1  christos 		IPFERROR(87);
   7684       1.1  christos 		return ENOSPC;
   7685       1.1  christos 	}
   7686       1.1  christos 	if (it.iri_rule == NULL) {
   7687       1.1  christos 		IPFERROR(88);
   7688       1.1  christos 		return EFAULT;
   7689       1.1  christos 	}
   7690       1.1  christos 
   7691       1.1  christos 	fg = NULL;
   7692       1.1  christos 	fr = t->ipt_data;
   7693       1.3   darrenr 	if ((it.iri_inout & F_OUT) != 0)
   7694       1.3   darrenr 		out = 1;
   7695       1.3   darrenr 	else
   7696       1.3   darrenr 		out = 0;
   7697       1.3   darrenr 	if ((it.iri_inout & F_ACIN) != 0)
   7698       1.3   darrenr 		unit = IPL_LOGCOUNT;
   7699       1.3   darrenr 	else
   7700       1.3   darrenr 		unit = IPL_LOGIPF;
   7701       1.1  christos 
   7702       1.1  christos 	READ_ENTER(&softc->ipf_mutex);
   7703       1.1  christos 	if (fr == NULL) {
   7704       1.1  christos 		if (*it.iri_group == '\0') {
   7705       1.3   darrenr 			if (unit == IPL_LOGCOUNT) {
   7706       1.1  christos 				next = softc->ipf_acct[out][it.iri_active];
   7707       1.3   darrenr 			} else {
   7708       1.1  christos 				next = softc->ipf_rules[out][it.iri_active];
   7709       1.3   darrenr 			}
   7710       1.3   darrenr 			if (next == NULL)
   7711       1.3   darrenr 				next = ipf_nextrule(softc, it.iri_active,
   7712       1.3   darrenr 						    unit, NULL, out);
   7713       1.1  christos 		} else {
   7714       1.3   darrenr 			fg = ipf_findgroup(softc, it.iri_group, unit,
   7715       1.3   darrenr 					   it.iri_active, NULL);
   7716       1.1  christos 			if (fg != NULL)
   7717       1.1  christos 				next = fg->fg_start;
   7718       1.1  christos 			else
   7719       1.1  christos 				next = NULL;
   7720       1.1  christos 		}
   7721       1.1  christos 	} else {
   7722       1.1  christos 		next = fr->fr_next;
   7723       1.3   darrenr 		if (next == NULL)
   7724       1.3   darrenr 			next = ipf_nextrule(softc, it.iri_active, unit,
   7725       1.3   darrenr 					    fr, out);
   7726       1.1  christos 	}
   7727       1.1  christos 
   7728       1.3   darrenr 	if (next != NULL && next->fr_next != NULL)
   7729       1.3   darrenr 		predict = 1;
   7730       1.3   darrenr 	else if (ipf_nextrule(softc, it.iri_active, unit, next, out) != NULL)
   7731       1.3   darrenr 		predict = 1;
   7732       1.3   darrenr 	else
   7733       1.3   darrenr 		predict = 0;
   7734       1.3   darrenr 
   7735       1.3   darrenr 	if (fr != NULL)
   7736       1.3   darrenr 		(void) ipf_derefrule(softc, &fr);
   7737       1.3   darrenr 
   7738       1.1  christos 	obj.ipfo_type = IPFOBJ_FRENTRY;
   7739       1.1  christos 	dst = (char *)it.iri_rule;
   7740       1.1  christos 
   7741       1.1  christos 	if (next != NULL) {
   7742       1.1  christos 		obj.ipfo_size = next->fr_size;
   7743       1.1  christos 		MUTEX_ENTER(&next->fr_lock);
   7744       1.1  christos 		next->fr_ref++;
   7745       1.1  christos 		MUTEX_EXIT(&next->fr_lock);
   7746       1.1  christos 		t->ipt_data = next;
   7747       1.1  christos 	} else {
   7748       1.1  christos 		obj.ipfo_size = sizeof(frentry_t);
   7749       1.1  christos 		bzero(&zero, sizeof(zero));
   7750       1.1  christos 		next = &zero;
   7751       1.1  christos 		t->ipt_data = NULL;
   7752       1.1  christos 	}
   7753       1.3   darrenr 	it.iri_rule = predict ? next : NULL;
   7754       1.3   darrenr 	if (predict == 0)
   7755       1.1  christos 		ipf_token_mark_complete(t);
   7756       1.1  christos 
   7757       1.1  christos 	RWLOCK_EXIT(&softc->ipf_mutex);
   7758       1.1  christos 
   7759       1.1  christos 	obj.ipfo_ptr = dst;
   7760       1.1  christos 	error = ipf_outobjk(softc, &obj, next);
   7761       1.1  christos 	if (error == 0 && t->ipt_data != NULL) {
   7762       1.1  christos 		dst += obj.ipfo_size;
   7763       1.1  christos 		if (next->fr_data != NULL) {
   7764       1.1  christos 			ipfobj_t dobj;
   7765       1.1  christos 
   7766       1.3   darrenr 			if (next->fr_type == FR_T_IPFEXPR)
   7767       1.3   darrenr 				dobj.ipfo_type = IPFOBJ_IPFEXPR;
   7768       1.3   darrenr 			else
   7769       1.3   darrenr 				dobj.ipfo_type = IPFOBJ_FRIPF;
   7770       1.1  christos 			dobj.ipfo_size = next->fr_dsize;
   7771       1.1  christos 			dobj.ipfo_rev = obj.ipfo_rev;
   7772       1.1  christos 			dobj.ipfo_ptr = dst;
   7773       1.1  christos 			error = ipf_outobjk(softc, &dobj, next->fr_data);
   7774       1.1  christos 		}
   7775       1.1  christos 	}
   7776       1.1  christos 
   7777       1.1  christos 	if ((fr != NULL) && (next == &zero))
   7778       1.1  christos 		(void) ipf_derefrule(softc, &fr);
   7779       1.1  christos 
   7780       1.1  christos 	return error;
   7781       1.1  christos }
   7782       1.1  christos 
   7783       1.1  christos 
   7784       1.1  christos /* ------------------------------------------------------------------------ */
   7785       1.1  christos /* Function:    ipf_frruleiter                                              */
   7786       1.1  christos /* Returns:     int - 0 = success, else error                               */
   7787       1.1  christos /* Parameters:  softc(I)- pointer to soft context main structure            */
   7788       1.1  christos /*              data(I) - the token type to match                           */
   7789       1.1  christos /*              uid(I)  - uid owning the token                              */
   7790       1.1  christos /*              ptr(I)  - context pointer for the token                     */
   7791       1.1  christos /*                                                                          */
   7792       1.1  christos /* This function serves as a stepping stone between ipf_ipf_ioctl and       */
   7793       1.1  christos /* ipf_getnextrule.  It's role is to find the right token in the kernel for */
   7794       1.1  christos /* the process doing the ioctl and use that to ask for the next rule.       */
   7795       1.1  christos /* ------------------------------------------------------------------------ */
   7796       1.1  christos static int
   7797       1.2  christos ipf_frruleiter(ipf_main_softc_t *softc, void *data, int uid, void *ctx)
   7798       1.1  christos {
   7799       1.1  christos 	ipftoken_t *token;
   7800       1.3   darrenr 	ipfruleiter_t it;
   7801       1.3   darrenr 	ipfobj_t obj;
   7802       1.1  christos 	int error;
   7803       1.1  christos 
   7804       1.1  christos 	token = ipf_token_find(softc, IPFGENITER_IPF, uid, ctx);
   7805       1.1  christos 	if (token != NULL) {
   7806       1.1  christos 		error = ipf_getnextrule(softc, token, data);
   7807       1.1  christos 		WRITE_ENTER(&softc->ipf_tokens);
   7808       1.3   darrenr 		ipf_token_deref(softc, token);
   7809       1.1  christos 		RWLOCK_EXIT(&softc->ipf_tokens);
   7810       1.1  christos 	} else {
   7811       1.3   darrenr 		error = ipf_inobj(softc, data, &obj, &it, IPFOBJ_IPFITER);
   7812       1.3   darrenr 		if (error != 0)
   7813       1.3   darrenr 			return error;
   7814       1.3   darrenr 		it.iri_rule = NULL;
   7815       1.3   darrenr 		error = ipf_outobj(softc, data, &it, IPFOBJ_IPFITER);
   7816       1.1  christos 	}
   7817       1.1  christos 
   7818       1.1  christos 	return error;
   7819       1.1  christos }
   7820       1.1  christos 
   7821       1.1  christos 
   7822       1.1  christos /* ------------------------------------------------------------------------ */
   7823       1.1  christos /* Function:    ipf_geniter                                                 */
   7824       1.1  christos /* Returns:     int - 0 = success, else error                               */
   7825       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   7826       1.1  christos /*              token(I) - pointer to ipftoken_t structure                  */
   7827       1.1  christos /*              itp(I)   - pointer to iterator data                         */
   7828       1.1  christos /*                                                                          */
   7829       1.1  christos /* Decide which iterator function to call using information passed through  */
   7830       1.1  christos /* the ipfgeniter_t structure at itp.                                       */
   7831       1.1  christos /* ------------------------------------------------------------------------ */
   7832       1.1  christos static int
   7833       1.2  christos ipf_geniter(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp)
   7834       1.1  christos {
   7835       1.1  christos 	int error;
   7836       1.1  christos 
   7837       1.1  christos 	switch (itp->igi_type)
   7838       1.1  christos 	{
   7839       1.1  christos 	case IPFGENITER_FRAG :
   7840       1.1  christos 		error = ipf_frag_pkt_next(softc, token, itp);
   7841       1.1  christos 		break;
   7842       1.1  christos 	default :
   7843       1.1  christos 		IPFERROR(92);
   7844       1.1  christos 		error = EINVAL;
   7845       1.1  christos 		break;
   7846       1.1  christos 	}
   7847       1.1  christos 
   7848       1.1  christos 	return error;
   7849       1.1  christos }
   7850       1.1  christos 
   7851       1.1  christos 
   7852       1.1  christos /* ------------------------------------------------------------------------ */
   7853       1.1  christos /* Function:    ipf_genericiter                                             */
   7854       1.1  christos /* Returns:     int - 0 = success, else error                               */
   7855       1.1  christos /* Parameters:  softc(I)- pointer to soft context main structure            */
   7856       1.1  christos /*              data(I) - the token type to match                           */
   7857       1.1  christos /*              uid(I)  - uid owning the token                              */
   7858       1.1  christos /*              ptr(I)  - context pointer for the token                     */
   7859       1.1  christos /*                                                                          */
   7860       1.1  christos /* Handle the SIOCGENITER ioctl for the ipfilter device. The primary role   */
   7861       1.1  christos /* ------------------------------------------------------------------------ */
   7862       1.1  christos int
   7863       1.2  christos ipf_genericiter(ipf_main_softc_t *softc, void *data, int uid, void *ctx)
   7864       1.1  christos {
   7865       1.1  christos 	ipftoken_t *token;
   7866       1.1  christos 	ipfgeniter_t iter;
   7867       1.1  christos 	int error;
   7868       1.1  christos 
   7869       1.1  christos 	error = ipf_inobj(softc, data, NULL, &iter, IPFOBJ_GENITER);
   7870       1.1  christos 	if (error != 0)
   7871       1.1  christos 		return error;
   7872       1.1  christos 
   7873       1.1  christos 	token = ipf_token_find(softc, iter.igi_type, uid, ctx);
   7874       1.1  christos 	if (token != NULL) {
   7875       1.1  christos 		token->ipt_subtype = iter.igi_type;
   7876       1.1  christos 		error = ipf_geniter(softc, token, &iter);
   7877       1.1  christos 		WRITE_ENTER(&softc->ipf_tokens);
   7878       1.3   darrenr 		ipf_token_deref(softc, token);
   7879       1.1  christos 		RWLOCK_EXIT(&softc->ipf_tokens);
   7880       1.1  christos 	} else {
   7881       1.1  christos 		IPFERROR(93);
   7882       1.1  christos 		error = 0;
   7883       1.1  christos 	}
   7884       1.1  christos 
   7885       1.1  christos 	return error;
   7886       1.1  christos }
   7887       1.1  christos 
   7888       1.1  christos 
   7889       1.1  christos /* ------------------------------------------------------------------------ */
   7890       1.1  christos /* Function:    ipf_ipf_ioctl                                               */
   7891       1.1  christos /* Returns:     int - 0 = success, else error                               */
   7892       1.1  christos /* Parameters:  softc(I)- pointer to soft context main structure           */
   7893       1.1  christos /*              data(I) - the token type to match                           */
   7894       1.1  christos /*              cmd(I)  - the ioctl command number                          */
   7895       1.1  christos /*              mode(I) - mode flags for the ioctl                          */
   7896       1.1  christos /*              uid(I)  - uid owning the token                              */
   7897       1.1  christos /*              ptr(I)  - context pointer for the token                     */
   7898       1.1  christos /*                                                                          */
   7899       1.1  christos /* This function handles all of the ioctl command that are actually isssued */
   7900       1.1  christos /* to the /dev/ipl device.                                                  */
   7901       1.1  christos /* ------------------------------------------------------------------------ */
   7902       1.1  christos int
   7903       1.2  christos ipf_ipf_ioctl(ipf_main_softc_t *softc, void *data, ioctlcmd_t cmd, int mode,
   7904       1.2  christos     int uid, void *ctx)
   7905       1.1  christos {
   7906       1.1  christos 	friostat_t fio;
   7907       1.1  christos 	int error, tmp;
   7908       1.1  christos 	ipfobj_t obj;
   7909       1.1  christos 	SPL_INT(s);
   7910       1.1  christos 
   7911       1.1  christos 	switch (cmd)
   7912       1.1  christos 	{
   7913       1.1  christos 	case SIOCFRENB :
   7914       1.1  christos 		if (!(mode & FWRITE)) {
   7915       1.1  christos 			IPFERROR(94);
   7916       1.1  christos 			error = EPERM;
   7917       1.1  christos 		} else {
   7918       1.1  christos 			error = BCOPYIN(data, &tmp, sizeof(tmp));
   7919       1.1  christos 			if (error != 0) {
   7920       1.1  christos 				IPFERROR(95);
   7921       1.1  christos 				error = EFAULT;
   7922       1.1  christos 				break;
   7923       1.1  christos 			}
   7924       1.1  christos 
   7925       1.1  christos 			WRITE_ENTER(&softc->ipf_global);
   7926       1.1  christos 			if (tmp) {
   7927       1.1  christos 				if (softc->ipf_running > 0)
   7928       1.1  christos 					error = 0;
   7929       1.1  christos 				else
   7930       1.1  christos 					error = ipfattach(softc);
   7931       1.1  christos 				if (error == 0)
   7932       1.1  christos 					softc->ipf_running = 1;
   7933       1.1  christos 				else
   7934       1.1  christos 					(void) ipfdetach(softc);
   7935       1.1  christos 			} else {
   7936       1.1  christos 				if (softc->ipf_running == 1)
   7937       1.1  christos 					error = ipfdetach(softc);
   7938       1.1  christos 				else
   7939       1.1  christos 					error = 0;
   7940       1.1  christos 				if (error == 0)
   7941       1.1  christos 					softc->ipf_running = -1;
   7942       1.1  christos 			}
   7943       1.1  christos 			RWLOCK_EXIT(&softc->ipf_global);
   7944       1.1  christos 		}
   7945       1.1  christos 		break;
   7946       1.1  christos 
   7947       1.1  christos 	case SIOCIPFSET :
   7948       1.1  christos 		if (!(mode & FWRITE)) {
   7949       1.1  christos 			IPFERROR(96);
   7950       1.1  christos 			error = EPERM;
   7951       1.1  christos 			break;
   7952       1.1  christos 		}
   7953       1.1  christos 		/* FALLTHRU */
   7954       1.1  christos 	case SIOCIPFGETNEXT :
   7955       1.1  christos 	case SIOCIPFGET :
   7956       1.1  christos 		error = ipf_ipftune(softc, cmd, (void *)data);
   7957       1.1  christos 		break;
   7958       1.1  christos 
   7959       1.1  christos 	case SIOCSETFF :
   7960       1.1  christos 		if (!(mode & FWRITE)) {
   7961       1.1  christos 			IPFERROR(97);
   7962       1.1  christos 			error = EPERM;
   7963       1.1  christos 		} else {
   7964       1.1  christos 			error = BCOPYIN(data, &softc->ipf_flags,
   7965       1.1  christos 					sizeof(softc->ipf_flags));
   7966       1.1  christos 			if (error != 0) {
   7967       1.1  christos 				IPFERROR(98);
   7968       1.1  christos 				error = EFAULT;
   7969       1.1  christos 			}
   7970       1.1  christos 		}
   7971       1.1  christos 		break;
   7972       1.1  christos 
   7973       1.1  christos 	case SIOCGETFF :
   7974       1.1  christos 		error = BCOPYOUT(&softc->ipf_flags, data,
   7975       1.1  christos 				 sizeof(softc->ipf_flags));
   7976       1.1  christos 		if (error != 0) {
   7977       1.1  christos 			IPFERROR(99);
   7978       1.1  christos 			error = EFAULT;
   7979       1.1  christos 		}
   7980       1.1  christos 		break;
   7981       1.1  christos 
   7982       1.1  christos 	case SIOCFUNCL :
   7983       1.1  christos 		error = ipf_resolvefunc(softc, (void *)data);
   7984       1.1  christos 		break;
   7985       1.1  christos 
   7986       1.1  christos 	case SIOCINAFR :
   7987       1.1  christos 	case SIOCRMAFR :
   7988       1.1  christos 	case SIOCADAFR :
   7989       1.1  christos 	case SIOCZRLST :
   7990       1.1  christos 		if (!(mode & FWRITE)) {
   7991       1.1  christos 			IPFERROR(100);
   7992       1.1  christos 			error = EPERM;
   7993       1.1  christos 		} else {
   7994       1.2  christos 			error = frrequest(softc, IPL_LOGIPF, cmd, data,
   7995       1.1  christos 					  softc->ipf_active, 1);
   7996       1.1  christos 		}
   7997       1.1  christos 		break;
   7998       1.1  christos 
   7999       1.1  christos 	case SIOCINIFR :
   8000       1.1  christos 	case SIOCRMIFR :
   8001       1.1  christos 	case SIOCADIFR :
   8002       1.1  christos 		if (!(mode & FWRITE)) {
   8003       1.1  christos 			IPFERROR(101);
   8004       1.1  christos 			error = EPERM;
   8005       1.1  christos 		} else {
   8006       1.2  christos 			error = frrequest(softc, IPL_LOGIPF, cmd, data,
   8007       1.1  christos 					  1 - softc->ipf_active, 1);
   8008       1.1  christos 		}
   8009       1.1  christos 		break;
   8010       1.1  christos 
   8011       1.1  christos 	case SIOCSWAPA :
   8012       1.1  christos 		if (!(mode & FWRITE)) {
   8013       1.1  christos 			IPFERROR(102);
   8014       1.1  christos 			error = EPERM;
   8015       1.1  christos 		} else {
   8016       1.1  christos 			WRITE_ENTER(&softc->ipf_mutex);
   8017       1.1  christos 			error = BCOPYOUT(&softc->ipf_active, data,
   8018       1.1  christos 					 sizeof(softc->ipf_active));
   8019       1.1  christos 			if (error != 0) {
   8020       1.1  christos 				IPFERROR(103);
   8021       1.1  christos 				error = EFAULT;
   8022       1.1  christos 			} else {
   8023       1.1  christos 				softc->ipf_active = 1 - softc->ipf_active;
   8024       1.1  christos 			}
   8025       1.1  christos 			RWLOCK_EXIT(&softc->ipf_mutex);
   8026       1.1  christos 		}
   8027       1.1  christos 		break;
   8028       1.1  christos 
   8029       1.1  christos 	case SIOCGETFS :
   8030       1.1  christos 		error = ipf_inobj(softc, (void *)data, &obj, &fio,
   8031       1.1  christos 				  IPFOBJ_IPFSTAT);
   8032       1.1  christos 		if (error != 0)
   8033       1.1  christos 			break;
   8034       1.1  christos 		ipf_getstat(softc, &fio, obj.ipfo_rev);
   8035       1.1  christos 		error = ipf_outobj(softc, (void *)data, &fio, IPFOBJ_IPFSTAT);
   8036       1.1  christos 		break;
   8037       1.1  christos 
   8038       1.1  christos 	case SIOCFRZST :
   8039       1.1  christos 		if (!(mode & FWRITE)) {
   8040       1.1  christos 			IPFERROR(104);
   8041       1.1  christos 			error = EPERM;
   8042       1.1  christos 		} else
   8043       1.2  christos 			error = ipf_zerostats(softc, data);
   8044       1.1  christos 		break;
   8045       1.1  christos 
   8046       1.1  christos 	case SIOCIPFFL :
   8047       1.1  christos 		if (!(mode & FWRITE)) {
   8048       1.1  christos 			IPFERROR(105);
   8049       1.1  christos 			error = EPERM;
   8050       1.1  christos 		} else {
   8051       1.1  christos 			error = BCOPYIN(data, &tmp, sizeof(tmp));
   8052       1.1  christos 			if (!error) {
   8053       1.1  christos 				tmp = ipf_flush(softc, IPL_LOGIPF, tmp);
   8054       1.1  christos 				error = BCOPYOUT(&tmp, data, sizeof(tmp));
   8055       1.1  christos 				if (error != 0) {
   8056       1.1  christos 					IPFERROR(106);
   8057       1.1  christos 					error = EFAULT;
   8058       1.1  christos 				}
   8059       1.1  christos 			} else {
   8060       1.1  christos 				IPFERROR(107);
   8061       1.1  christos 				error = EFAULT;
   8062       1.1  christos 			}
   8063       1.1  christos 		}
   8064       1.1  christos 		break;
   8065       1.1  christos 
   8066       1.1  christos #ifdef USE_INET6
   8067       1.1  christos 	case SIOCIPFL6 :
   8068       1.1  christos 		if (!(mode & FWRITE)) {
   8069       1.1  christos 			IPFERROR(108);
   8070       1.1  christos 			error = EPERM;
   8071       1.1  christos 		} else {
   8072       1.1  christos 			error = BCOPYIN(data, &tmp, sizeof(tmp));
   8073       1.1  christos 			if (!error) {
   8074       1.1  christos 				tmp = ipf_flush(softc, IPL_LOGIPF, tmp);
   8075       1.1  christos 				error = BCOPYOUT(&tmp, data, sizeof(tmp));
   8076       1.1  christos 				if (error != 0) {
   8077       1.1  christos 					IPFERROR(109);
   8078       1.1  christos 					error = EFAULT;
   8079       1.1  christos 				}
   8080       1.1  christos 			} else {
   8081       1.1  christos 				IPFERROR(110);
   8082       1.1  christos 				error = EFAULT;
   8083       1.1  christos 			}
   8084       1.1  christos 		}
   8085       1.1  christos 		break;
   8086       1.1  christos #endif
   8087       1.1  christos 
   8088       1.1  christos 	case SIOCSTLCK :
   8089       1.1  christos 		if (!(mode & FWRITE)) {
   8090       1.1  christos 			IPFERROR(122);
   8091       1.1  christos 			error = EPERM;
   8092       1.1  christos 		} else {
   8093       1.1  christos 			error = BCOPYIN(data, &tmp, sizeof(tmp));
   8094       1.1  christos 			if (error == 0) {
   8095       1.1  christos 				ipf_state_setlock(softc->ipf_state_soft, tmp);
   8096       1.1  christos 				ipf_nat_setlock(softc->ipf_nat_soft, tmp);
   8097       1.1  christos 				ipf_frag_setlock(softc->ipf_frag_soft, tmp);
   8098       1.1  christos 				ipf_auth_setlock(softc->ipf_auth_soft, tmp);
   8099       1.1  christos 			} else {
   8100       1.1  christos 				IPFERROR(111);
   8101       1.1  christos 				error = EFAULT;
   8102       1.1  christos 			}
   8103       1.1  christos 		}
   8104       1.1  christos 		break;
   8105       1.1  christos 
   8106       1.1  christos #ifdef	IPFILTER_LOG
   8107       1.1  christos 	case SIOCIPFFB :
   8108       1.1  christos 		if (!(mode & FWRITE)) {
   8109       1.1  christos 			IPFERROR(112);
   8110       1.1  christos 			error = EPERM;
   8111       1.1  christos 		} else {
   8112       1.1  christos 			tmp = ipf_log_clear(softc, IPL_LOGIPF);
   8113       1.1  christos 			error = BCOPYOUT(&tmp, data, sizeof(tmp));
   8114       1.1  christos 			if (error) {
   8115       1.1  christos 				IPFERROR(113);
   8116       1.1  christos 				error = EFAULT;
   8117       1.1  christos 			}
   8118       1.1  christos 		}
   8119       1.1  christos 		break;
   8120       1.1  christos #endif /* IPFILTER_LOG */
   8121       1.1  christos 
   8122       1.1  christos 	case SIOCFRSYN :
   8123       1.1  christos 		if (!(mode & FWRITE)) {
   8124       1.1  christos 			IPFERROR(114);
   8125       1.1  christos 			error = EPERM;
   8126       1.1  christos 		} else {
   8127       1.1  christos 			WRITE_ENTER(&softc->ipf_global);
   8128       1.1  christos #if (defined(MENTAT) && defined(_KERNEL)) && !defined(INSTANCES)
   8129       1.1  christos 			error = ipfsync();
   8130       1.1  christos #else
   8131       1.1  christos 			ipf_sync(softc, NULL);
   8132       1.1  christos 			error = 0;
   8133       1.1  christos #endif
   8134       1.1  christos 			RWLOCK_EXIT(&softc->ipf_global);
   8135       1.1  christos 
   8136       1.1  christos 		}
   8137       1.1  christos 		break;
   8138       1.1  christos 
   8139       1.1  christos 	case SIOCGFRST :
   8140       1.1  christos 		error = ipf_outobj(softc, (void *)data,
   8141       1.1  christos 				   ipf_frag_stats(softc->ipf_frag_soft),
   8142       1.1  christos 				   IPFOBJ_FRAGSTAT);
   8143       1.1  christos 		break;
   8144       1.1  christos 
   8145       1.1  christos #ifdef	IPFILTER_LOG
   8146       1.1  christos 	case FIONREAD :
   8147       1.1  christos 		tmp = ipf_log_bytesused(softc, IPL_LOGIPF);
   8148       1.1  christos 		error = BCOPYOUT(&tmp, data, sizeof(tmp));
   8149       1.1  christos 		break;
   8150       1.1  christos #endif
   8151       1.1  christos 
   8152       1.1  christos 	case SIOCIPFITER :
   8153       1.1  christos 		SPL_SCHED(s);
   8154       1.1  christos 		error = ipf_frruleiter(softc, data, uid, ctx);
   8155       1.1  christos 		SPL_X(s);
   8156       1.1  christos 		break;
   8157       1.1  christos 
   8158       1.1  christos 	case SIOCGENITER :
   8159       1.1  christos 		SPL_SCHED(s);
   8160       1.1  christos 		error = ipf_genericiter(softc, data, uid, ctx);
   8161       1.1  christos 		SPL_X(s);
   8162       1.1  christos 		break;
   8163       1.1  christos 
   8164       1.1  christos 	case SIOCIPFDELTOK :
   8165       1.1  christos 		error = BCOPYIN(data, &tmp, sizeof(tmp));
   8166       1.1  christos 		if (error == 0) {
   8167       1.1  christos 			SPL_SCHED(s);
   8168       1.1  christos 			error = ipf_token_del(softc, tmp, uid, ctx);
   8169       1.1  christos 			SPL_X(s);
   8170       1.1  christos 		}
   8171       1.1  christos 		break;
   8172       1.1  christos 
   8173       1.1  christos 	default :
   8174       1.1  christos 		IPFERROR(115);
   8175       1.1  christos 		error = EINVAL;
   8176       1.1  christos 		break;
   8177       1.1  christos 	}
   8178       1.1  christos 
   8179       1.1  christos 	return error;
   8180       1.1  christos }
   8181       1.1  christos 
   8182       1.1  christos 
   8183       1.1  christos /* ------------------------------------------------------------------------ */
   8184       1.1  christos /* Function:    ipf_decaps                                                  */
   8185       1.1  christos /* Returns:     int        - -1 == decapsulation failed, else bit mask of   */
   8186       1.1  christos /*                           flags indicating packet filtering decision.    */
   8187       1.1  christos /* Parameters:  fin(I)     - pointer to packet information                  */
   8188       1.1  christos /*              pass(I)    - IP protocol version to match                   */
   8189       1.1  christos /*              l5proto(I) - layer 5 protocol to decode UDP data as.        */
   8190       1.1  christos /*                                                                          */
   8191       1.1  christos /* This function is called for packets that are wrapt up in other packets,  */
   8192       1.1  christos /* for example, an IP packet that is the entire data segment for another IP */
   8193       1.1  christos /* packet.  If the basic constraints for this are satisfied, change the     */
   8194       1.1  christos /* buffer to point to the start of the inner packet and start processing    */
   8195       1.1  christos /* rules belonging to the head group this rule specifies.                   */
   8196       1.1  christos /* ------------------------------------------------------------------------ */
   8197       1.1  christos u_32_t
   8198       1.2  christos ipf_decaps(fr_info_t *fin, u_32_t pass, int l5proto)
   8199       1.1  christos {
   8200       1.1  christos 	fr_info_t fin2, *fino = NULL;
   8201       1.1  christos 	int elen, hlen, nh;
   8202       1.1  christos 	grehdr_t gre;
   8203       1.1  christos 	ip_t *ip;
   8204       1.1  christos 	mb_t *m;
   8205       1.1  christos 
   8206       1.1  christos 	if ((fin->fin_flx & FI_COALESCE) == 0)
   8207       1.1  christos 		if (ipf_coalesce(fin) == -1)
   8208       1.1  christos 			goto cantdecaps;
   8209       1.1  christos 
   8210       1.1  christos 	m = fin->fin_m;
   8211       1.1  christos 	hlen = fin->fin_hlen;
   8212       1.1  christos 
   8213       1.1  christos 	switch (fin->fin_p)
   8214       1.1  christos 	{
   8215       1.1  christos 	case IPPROTO_UDP :
   8216       1.1  christos 		/*
   8217       1.1  christos 		 * In this case, the specific protocol being decapsulated
   8218       1.1  christos 		 * inside UDP frames comes from the rule.
   8219       1.1  christos 		 */
   8220       1.1  christos 		nh = fin->fin_fr->fr_icode;
   8221       1.1  christos 		break;
   8222       1.1  christos 
   8223       1.1  christos 	case IPPROTO_GRE :	/* 47 */
   8224       1.1  christos 		bcopy(fin->fin_dp, (char *)&gre, sizeof(gre));
   8225       1.1  christos 		hlen += sizeof(grehdr_t);
   8226       1.1  christos 		if (gre.gr_R|gre.gr_s)
   8227       1.1  christos 			goto cantdecaps;
   8228       1.1  christos 		if (gre.gr_C)
   8229       1.1  christos 			hlen += 4;
   8230       1.1  christos 		if (gre.gr_K)
   8231       1.1  christos 			hlen += 4;
   8232       1.1  christos 		if (gre.gr_S)
   8233       1.1  christos 			hlen += 4;
   8234       1.1  christos 
   8235       1.1  christos 		nh = IPPROTO_IP;
   8236       1.1  christos 
   8237       1.1  christos 		/*
   8238       1.1  christos 		 * If the routing options flag is set, validate that it is
   8239       1.1  christos 		 * there and bounce over it.
   8240       1.1  christos 		 */
   8241       1.1  christos #if 0
   8242       1.1  christos 		/* This is really heavy weight and lots of room for error, */
   8243       1.1  christos 		/* so for now, put it off and get the simple stuff right.  */
   8244       1.1  christos 		if (gre.gr_R) {
   8245       1.1  christos 			u_char off, len, *s;
   8246       1.1  christos 			u_short af;
   8247       1.1  christos 			int end;
   8248       1.1  christos 
   8249       1.1  christos 			end = 0;
   8250       1.1  christos 			s = fin->fin_dp;
   8251       1.1  christos 			s += hlen;
   8252       1.1  christos 			aplen = fin->fin_plen - hlen;
   8253       1.1  christos 			while (aplen > 3) {
   8254       1.1  christos 				af = (s[0] << 8) | s[1];
   8255       1.1  christos 				off = s[2];
   8256       1.1  christos 				len = s[3];
   8257       1.1  christos 				aplen -= 4;
   8258       1.1  christos 				s += 4;
   8259       1.1  christos 				if (af == 0 && len == 0) {
   8260       1.1  christos 					end = 1;
   8261       1.1  christos 					break;
   8262       1.1  christos 				}
   8263       1.1  christos 				if (aplen < len)
   8264       1.1  christos 					break;
   8265       1.1  christos 				s += len;
   8266       1.1  christos 				aplen -= len;
   8267       1.1  christos 			}
   8268       1.1  christos 			if (end != 1)
   8269       1.1  christos 				goto cantdecaps;
   8270       1.1  christos 			hlen = s - (u_char *)fin->fin_dp;
   8271       1.1  christos 		}
   8272       1.1  christos #endif
   8273       1.1  christos 		break;
   8274       1.1  christos 
   8275       1.1  christos #ifdef IPPROTO_IPIP
   8276       1.1  christos 	case IPPROTO_IPIP :	/* 4 */
   8277       1.1  christos #endif
   8278       1.1  christos 		nh = IPPROTO_IP;
   8279       1.1  christos 		break;
   8280       1.1  christos 
   8281       1.1  christos 	default :	/* Includes ESP, AH is special for IPv4 */
   8282       1.1  christos 		goto cantdecaps;
   8283       1.1  christos 	}
   8284       1.1  christos 
   8285       1.1  christos 	switch (nh)
   8286       1.1  christos 	{
   8287       1.1  christos 	case IPPROTO_IP :
   8288       1.1  christos 	case IPPROTO_IPV6 :
   8289       1.1  christos 		break;
   8290       1.1  christos 	default :
   8291       1.1  christos 		goto cantdecaps;
   8292       1.1  christos 	}
   8293       1.1  christos 
   8294       1.1  christos 	bcopy((char *)fin, (char *)&fin2, sizeof(fin2));
   8295       1.1  christos 	fino = fin;
   8296       1.1  christos 	fin = &fin2;
   8297       1.1  christos 	elen = hlen;
   8298       1.1  christos #if defined(MENTAT) && defined(_KERNEL)
   8299       1.1  christos 	m->b_rptr += elen;
   8300       1.1  christos #else
   8301       1.1  christos 	m->m_data += elen;
   8302       1.1  christos 	m->m_len -= elen;
   8303       1.1  christos #endif
   8304       1.1  christos 	fin->fin_plen -= elen;
   8305       1.1  christos 
   8306       1.1  christos 	ip = (ip_t *)((char *)fin->fin_ip + elen);
   8307       1.1  christos 
   8308       1.1  christos 	/*
   8309       1.1  christos 	 * Make sure we have at least enough data for the network layer
   8310       1.1  christos 	 * header.
   8311       1.1  christos 	 */
   8312       1.1  christos 	if (IP_V(ip) == 4)
   8313       1.1  christos 		hlen = IP_HL(ip) << 2;
   8314       1.1  christos #ifdef USE_INET6
   8315       1.1  christos 	else if (IP_V(ip) == 6)
   8316       1.1  christos 		hlen = sizeof(ip6_t);
   8317       1.1  christos #endif
   8318       1.1  christos 	else
   8319       1.1  christos 		goto cantdecaps2;
   8320       1.1  christos 
   8321       1.1  christos 	if (fin->fin_plen < hlen)
   8322       1.1  christos 		goto cantdecaps2;
   8323       1.1  christos 
   8324       1.1  christos 	fin->fin_dp = (char *)ip + hlen;
   8325       1.1  christos 
   8326       1.1  christos 	if (IP_V(ip) == 4) {
   8327       1.1  christos 		/*
   8328       1.1  christos 		 * Perform IPv4 header checksum validation.
   8329       1.1  christos 		 */
   8330       1.1  christos 		if (ipf_cksum((u_short *)ip, hlen))
   8331       1.1  christos 			goto cantdecaps2;
   8332       1.1  christos 	}
   8333       1.1  christos 
   8334       1.1  christos 	if (ipf_makefrip(hlen, ip, fin) == -1) {
   8335       1.1  christos cantdecaps2:
   8336       1.1  christos 		if (m != NULL) {
   8337       1.1  christos #if defined(MENTAT) && defined(_KERNEL)
   8338       1.1  christos 			m->b_rptr -= elen;
   8339       1.1  christos #else
   8340       1.1  christos 			m->m_data -= elen;
   8341       1.1  christos 			m->m_len += elen;
   8342       1.1  christos #endif
   8343       1.1  christos 		}
   8344       1.1  christos cantdecaps:
   8345       1.1  christos 		DT1(frb_decapfrip, fr_info_t *, fin);
   8346       1.1  christos 		pass &= ~FR_CMDMASK;
   8347       1.1  christos 		pass |= FR_BLOCK|FR_QUICK;
   8348       1.1  christos 		fin->fin_reason = FRB_DECAPFRIP;
   8349       1.1  christos 		return -1;
   8350       1.1  christos 	}
   8351       1.1  christos 
   8352       1.1  christos 	pass = ipf_scanlist(fin, pass);
   8353       1.1  christos 
   8354       1.1  christos 	/*
   8355       1.1  christos 	 * Copy the packet filter "result" fields out of the fr_info_t struct
   8356       1.1  christos 	 * that is local to the decapsulation processing and back into the
   8357       1.1  christos 	 * one we were called with.
   8358       1.1  christos 	 */
   8359       1.1  christos 	fino->fin_flx = fin->fin_flx;
   8360       1.1  christos 	fino->fin_rev = fin->fin_rev;
   8361       1.1  christos 	fino->fin_icode = fin->fin_icode;
   8362       1.1  christos 	fino->fin_rule = fin->fin_rule;
   8363       1.1  christos 	(void) strncpy(fino->fin_group, fin->fin_group, FR_GROUPLEN);
   8364       1.1  christos 	fino->fin_fr = fin->fin_fr;
   8365       1.1  christos 	fino->fin_error = fin->fin_error;
   8366       1.1  christos 	fino->fin_mp = fin->fin_mp;
   8367       1.1  christos 	fino->fin_m = fin->fin_m;
   8368       1.1  christos 	m = fin->fin_m;
   8369       1.1  christos 	if (m != NULL) {
   8370       1.1  christos #if defined(MENTAT) && defined(_KERNEL)
   8371       1.1  christos 		m->b_rptr -= elen;
   8372       1.1  christos #else
   8373       1.1  christos 		m->m_data -= elen;
   8374       1.1  christos 		m->m_len += elen;
   8375       1.1  christos #endif
   8376       1.1  christos 	}
   8377       1.1  christos 	return pass;
   8378       1.1  christos }
   8379       1.1  christos 
   8380       1.1  christos 
   8381       1.1  christos /* ------------------------------------------------------------------------ */
   8382       1.1  christos /* Function:    ipf_matcharray_load                                         */
   8383       1.1  christos /* Returns:     int         - 0 = success, else error                       */
   8384       1.1  christos /* Parameters:  softc(I)    - pointer to soft context main structure        */
   8385       1.1  christos /*              data(I)     - pointer to ioctl data                         */
   8386       1.1  christos /*              objp(I)     - ipfobj_t structure to load data into          */
   8387       1.1  christos /*              arrayptr(I) - pointer to location to store array pointer    */
   8388       1.1  christos /*                                                                          */
   8389       1.1  christos /* This function loads in a mathing array through the ipfobj_t struct that  */
   8390       1.1  christos /* describes it.  Sanity checking and array size limitations are enforced   */
   8391       1.1  christos /* in this function to prevent userspace from trying to load in something   */
   8392       1.1  christos /* that is insanely big.  Once the size of the array is known, the memory   */
   8393       1.1  christos /* required is malloc'd and returned through changing *arrayptr.  The       */
   8394       1.1  christos /* contents of the array are verified before returning.  Only in the event  */
   8395       1.1  christos /* of a successful call is the caller required to free up the malloc area.  */
   8396       1.1  christos /* ------------------------------------------------------------------------ */
   8397       1.1  christos int
   8398       1.2  christos ipf_matcharray_load(ipf_main_softc_t *softc, void *data, ipfobj_t *objp,
   8399       1.2  christos     int **arrayptr)
   8400       1.1  christos {
   8401       1.1  christos 	int arraysize, *array, error;
   8402       1.1  christos 
   8403       1.1  christos 	*arrayptr = NULL;
   8404       1.1  christos 
   8405       1.1  christos 	error = BCOPYIN(data, objp, sizeof(*objp));
   8406       1.1  christos 	if (error != 0) {
   8407       1.1  christos 		IPFERROR(116);
   8408       1.1  christos 		return EFAULT;
   8409       1.1  christos 	}
   8410       1.1  christos 
   8411       1.1  christos 	if (objp->ipfo_type != IPFOBJ_IPFEXPR) {
   8412       1.1  christos 		IPFERROR(117);
   8413       1.1  christos 		return EINVAL;
   8414       1.1  christos 	}
   8415       1.1  christos 
   8416       1.1  christos 	if (((objp->ipfo_size & 3) != 0) || (objp->ipfo_size == 0) ||
   8417       1.1  christos 	    (objp->ipfo_size > 1024)) {
   8418       1.1  christos 		IPFERROR(118);
   8419       1.1  christos 		return EINVAL;
   8420       1.1  christos 	}
   8421       1.1  christos 
   8422       1.1  christos 	arraysize = objp->ipfo_size * sizeof(*array);
   8423       1.1  christos 	KMALLOCS(array, int *, arraysize);
   8424       1.1  christos 	if (array == NULL) {
   8425       1.1  christos 		IPFERROR(119);
   8426       1.1  christos 		return ENOMEM;
   8427       1.1  christos 	}
   8428       1.1  christos 
   8429       1.1  christos 	error = COPYIN(objp->ipfo_ptr, array, arraysize);
   8430       1.1  christos 	if (error != 0) {
   8431       1.1  christos 		KFREES(array, arraysize);
   8432       1.1  christos 		IPFERROR(120);
   8433       1.1  christos 		return EFAULT;
   8434       1.1  christos 	}
   8435       1.1  christos 
   8436       1.1  christos 	if (ipf_matcharray_verify(array, arraysize) != 0) {
   8437       1.1  christos 		KFREES(array, arraysize);
   8438       1.1  christos 		IPFERROR(121);
   8439       1.1  christos 		return EINVAL;
   8440       1.1  christos 	}
   8441       1.1  christos 
   8442       1.1  christos 	*arrayptr = array;
   8443       1.1  christos 	return 0;
   8444       1.1  christos }
   8445       1.1  christos 
   8446       1.1  christos 
   8447       1.1  christos /* ------------------------------------------------------------------------ */
   8448       1.1  christos /* Function:    ipf_matcharray_verify                                       */
   8449       1.1  christos /* Returns:     Nil                                                         */
   8450       1.1  christos /* Parameters:  array(I)     - pointer to matching array                    */
   8451       1.1  christos /*              arraysize(I) - number of elements in the array              */
   8452       1.1  christos /*                                                                          */
   8453       1.1  christos /* Verify the contents of a matching array by stepping through each element */
   8454       1.1  christos /* in it.  The actual commands in the array are not verified for            */
   8455       1.1  christos /* correctness, only that all of the sizes are correctly within limits.     */
   8456       1.1  christos /* ------------------------------------------------------------------------ */
   8457       1.1  christos int
   8458       1.2  christos ipf_matcharray_verify(int *array, int arraysize)
   8459       1.1  christos {
   8460       1.3   darrenr 	int i, nelem, maxidx;
   8461       1.3   darrenr 	ipfexp_t *e;
   8462       1.1  christos 
   8463       1.1  christos 	nelem = arraysize / sizeof(*array);
   8464       1.1  christos 
   8465       1.1  christos 	/*
   8466       1.1  christos 	 * Currently, it makes no sense to have an array less than 6
   8467       1.1  christos 	 * elements long - the initial size at the from, a single operation
   8468       1.1  christos 	 * (minimum 4 in length) and a trailer, for a total of 6.
   8469       1.1  christos 	 */
   8470       1.1  christos 	if ((array[0] < 6) || (arraysize < 24) || (arraysize > 4096)) {
   8471       1.1  christos 		return -1;
   8472       1.1  christos 	}
   8473       1.1  christos 
   8474       1.1  christos 	/*
   8475       1.1  christos 	 * Verify the size of data pointed to by array with how long
   8476       1.1  christos 	 * the array claims to be itself.
   8477       1.1  christos 	 */
   8478       1.1  christos 	if (array[0] * sizeof(*array) != arraysize) {
   8479       1.1  christos 		return -1;
   8480       1.1  christos 	}
   8481       1.1  christos 
   8482       1.1  christos 	maxidx = nelem - 1;
   8483       1.1  christos 	/*
   8484       1.1  christos 	 * The last opcode in this array should be an IPF_EXP_END.
   8485       1.1  christos 	 */
   8486       1.1  christos 	if (array[maxidx] != IPF_EXP_END) {
   8487       1.1  christos 		return -1;
   8488       1.1  christos 	}
   8489       1.1  christos 
   8490       1.1  christos 	for (i = 1; i < maxidx; ) {
   8491       1.3   darrenr 		e = (ipfexp_t *)(array + i);
   8492       1.1  christos 
   8493       1.1  christos 		/*
   8494       1.1  christos 		 * The length of the bits to check must be at least 1
   8495       1.1  christos 		 * (or else there is nothing to comapre with!) and it
   8496       1.1  christos 		 * cannot exceed the length of the data present.
   8497       1.1  christos 		 */
   8498       1.3   darrenr 		if ((e->ipfe_size < 1 ) ||
   8499       1.3   darrenr 		    (e->ipfe_size + i > maxidx)) {
   8500       1.1  christos 			return -1;
   8501       1.1  christos 		}
   8502       1.3   darrenr 		i += e->ipfe_size;
   8503       1.1  christos 	}
   8504       1.1  christos 	return 0;
   8505       1.1  christos }
   8506       1.1  christos 
   8507       1.1  christos 
   8508       1.1  christos /* ------------------------------------------------------------------------ */
   8509       1.1  christos /* Function:    ipf_fr_matcharray                                           */
   8510       1.1  christos /* Returns:     int      - 0 = match failed, else positive match            */
   8511       1.1  christos /* Parameters:  fin(I)   - pointer to packet information                    */
   8512       1.1  christos /*              array(I) - pointer to matching array                        */
   8513       1.1  christos /*                                                                          */
   8514       1.1  christos /* This function is used to apply a matching array against a packet and     */
   8515       1.1  christos /* return an indication of whether or not the packet successfully matches   */
   8516       1.1  christos /* all of the commands in it.                                               */
   8517       1.1  christos /* ------------------------------------------------------------------------ */
   8518       1.1  christos static int
   8519       1.2  christos ipf_fr_matcharray(fr_info_t *fin, int *array)
   8520       1.1  christos {
   8521       1.3   darrenr 	int i, n, *x, rv, p;
   8522       1.3   darrenr 	ipfexp_t *e;
   8523       1.1  christos 
   8524       1.3   darrenr 	rv = 0;
   8525       1.1  christos 	n = array[0];
   8526       1.1  christos 	x = array + 1;
   8527       1.1  christos 
   8528       1.3   darrenr 	for (; n > 0; x += 3 + x[3], rv = 0) {
   8529       1.3   darrenr 		e = (ipfexp_t *)x;
   8530       1.3   darrenr 		if (e->ipfe_cmd == IPF_EXP_END)
   8531       1.3   darrenr 			break;
   8532       1.3   darrenr 		n -= e->ipfe_size;
   8533       1.1  christos 
   8534       1.1  christos 		/*
   8535       1.1  christos 		 * The upper 16 bits currently store the protocol value.
   8536       1.1  christos 		 * This is currently used with TCP and UDP port compares and
   8537       1.1  christos 		 * allows "tcp.port = 80" without requiring an explicit
   8538       1.1  christos 		 " "ip.pr = tcp" first.
   8539       1.1  christos 		 */
   8540       1.3   darrenr 		p = e->ipfe_cmd >> 16;
   8541       1.1  christos 		if ((p != 0) && (p != fin->fin_p))
   8542       1.1  christos 			break;
   8543       1.1  christos 
   8544       1.3   darrenr 		switch (e->ipfe_cmd)
   8545       1.1  christos 		{
   8546       1.1  christos 		case IPF_EXP_IP_PR :
   8547       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8548       1.3   darrenr 				rv |= (fin->fin_p == e->ipfe_arg0[i]);
   8549       1.1  christos 			}
   8550       1.1  christos 			break;
   8551       1.1  christos 
   8552       1.1  christos 		case IPF_EXP_IP_SRCADDR :
   8553       1.1  christos 			if (fin->fin_v != 4)
   8554       1.1  christos 				break;
   8555       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8556       1.3   darrenr 				rv |= ((fin->fin_saddr &
   8557       1.3   darrenr 					e->ipfe_arg0[i * 2 + 1]) ==
   8558       1.3   darrenr 				       e->ipfe_arg0[i * 2]);
   8559       1.1  christos 			}
   8560       1.1  christos 			break;
   8561       1.1  christos 
   8562       1.1  christos 		case IPF_EXP_IP_DSTADDR :
   8563       1.1  christos 			if (fin->fin_v != 4)
   8564       1.1  christos 				break;
   8565       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8566       1.3   darrenr 				rv |= ((fin->fin_daddr &
   8567       1.3   darrenr 					e->ipfe_arg0[i * 2 + 1]) ==
   8568       1.3   darrenr 				       e->ipfe_arg0[i * 2]);
   8569       1.1  christos 			}
   8570       1.1  christos 			break;
   8571       1.1  christos 
   8572       1.1  christos 		case IPF_EXP_IP_ADDR :
   8573       1.1  christos 			if (fin->fin_v != 4)
   8574       1.1  christos 				break;
   8575       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8576       1.3   darrenr 				rv |= ((fin->fin_saddr &
   8577       1.3   darrenr 					e->ipfe_arg0[i * 2 + 1]) ==
   8578       1.3   darrenr 				       e->ipfe_arg0[i * 2]) ||
   8579       1.3   darrenr 				      ((fin->fin_daddr &
   8580       1.3   darrenr 					e->ipfe_arg0[i * 2 + 1]) ==
   8581       1.3   darrenr 				       e->ipfe_arg0[i * 2]);
   8582       1.1  christos 			}
   8583       1.1  christos 			break;
   8584       1.1  christos 
   8585       1.1  christos #ifdef USE_INET6
   8586       1.1  christos 		case IPF_EXP_IP6_SRCADDR :
   8587       1.1  christos 			if (fin->fin_v != 6)
   8588       1.1  christos 				break;
   8589       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8590       1.3   darrenr 				rv |= IP6_MASKEQ(&fin->fin_src6,
   8591       1.3   darrenr 						 &e->ipfe_arg0[i * 8 + 4],
   8592       1.3   darrenr 						 &e->ipfe_arg0[i * 8]);
   8593       1.1  christos 			}
   8594       1.1  christos 			break;
   8595       1.1  christos 
   8596       1.1  christos 		case IPF_EXP_IP6_DSTADDR :
   8597       1.1  christos 			if (fin->fin_v != 6)
   8598       1.1  christos 				break;
   8599       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8600       1.3   darrenr 				rv |= IP6_MASKEQ(&fin->fin_dst6,
   8601       1.3   darrenr 						 &e->ipfe_arg0[i * 8 + 4],
   8602       1.3   darrenr 						 &e->ipfe_arg0[i * 8]);
   8603       1.1  christos 			}
   8604       1.1  christos 			break;
   8605       1.1  christos 
   8606       1.1  christos 		case IPF_EXP_IP6_ADDR :
   8607       1.1  christos 			if (fin->fin_v != 6)
   8608       1.1  christos 				break;
   8609       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8610       1.3   darrenr 				rv |= IP6_MASKEQ(&fin->fin_src6,
   8611       1.3   darrenr 						 &e->ipfe_arg0[i * 8 + 4],
   8612       1.3   darrenr 						 &e->ipfe_arg0[i * 8]) ||
   8613       1.3   darrenr 				      IP6_MASKEQ(&fin->fin_dst6,
   8614       1.3   darrenr 						 &e->ipfe_arg0[i * 8 + 4],
   8615       1.3   darrenr 						 &e->ipfe_arg0[i * 8]);
   8616       1.1  christos 			}
   8617       1.1  christos 			break;
   8618       1.1  christos #endif
   8619       1.1  christos 
   8620       1.1  christos 		case IPF_EXP_UDP_PORT :
   8621       1.1  christos 		case IPF_EXP_TCP_PORT :
   8622       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8623       1.3   darrenr 				rv |= (fin->fin_sport == e->ipfe_arg0[i]) ||
   8624       1.3   darrenr 				      (fin->fin_dport == e->ipfe_arg0[i]);
   8625       1.1  christos 			}
   8626       1.1  christos 			break;
   8627       1.1  christos 
   8628       1.1  christos 		case IPF_EXP_UDP_SPORT :
   8629       1.1  christos 		case IPF_EXP_TCP_SPORT :
   8630       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8631       1.3   darrenr 				rv |= (fin->fin_sport == e->ipfe_arg0[i]);
   8632       1.1  christos 			}
   8633       1.1  christos 			break;
   8634       1.1  christos 
   8635       1.1  christos 		case IPF_EXP_UDP_DPORT :
   8636       1.1  christos 		case IPF_EXP_TCP_DPORT :
   8637       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8638       1.3   darrenr 				rv |= (fin->fin_dport == e->ipfe_arg0[i]);
   8639       1.1  christos 			}
   8640       1.1  christos 			break;
   8641       1.1  christos 
   8642       1.1  christos 		case IPF_EXP_TCP_FLAGS :
   8643       1.3   darrenr 			for (i = 0; !rv && i < e->ipfe_narg; i++) {
   8644       1.3   darrenr 				rv |= ((fin->fin_tcpf &
   8645       1.3   darrenr 					e->ipfe_arg0[i * 2 + 1]) ==
   8646       1.3   darrenr 				       e->ipfe_arg0[i * 2]);
   8647       1.1  christos 			}
   8648       1.1  christos 			break;
   8649       1.1  christos 		}
   8650       1.3   darrenr 		rv ^= e->ipfe_not;
   8651       1.1  christos 
   8652       1.3   darrenr 		if (rv == 0)
   8653       1.1  christos 			break;
   8654       1.1  christos 	}
   8655       1.1  christos 
   8656       1.3   darrenr 	return rv;
   8657       1.1  christos }
   8658       1.1  christos 
   8659       1.1  christos 
   8660       1.1  christos /* ------------------------------------------------------------------------ */
   8661       1.1  christos /* Function:    ipf_queueflush                                              */
   8662       1.1  christos /* Returns:     int - number of entries flushed (0 = none)                  */
   8663       1.1  christos /* Parameters:  softc(I)    - pointer to soft context main structure        */
   8664       1.1  christos /*              deletefn(I) - function to call to delete entry              */
   8665       1.1  christos /*              ipfqs(I)    - top of the list of ipf internal queues        */
   8666       1.1  christos /*              userqs(I)   - top of the list of user defined timeouts      */
   8667       1.1  christos /*                                                                          */
   8668       1.1  christos /* This fucntion gets called when the state/NAT hash tables fill up and we  */
   8669       1.1  christos /* need to try a bit harder to free up some space.  The algorithm used here */
   8670       1.1  christos /* split into two parts but both halves have the same goal: to reduce the   */
   8671       1.1  christos /* number of connections considered to be "active" to the low watermark.    */
   8672       1.1  christos /* There are two steps in doing this:                                       */
   8673       1.1  christos /* 1) Remove any TCP connections that are already considered to be "closed" */
   8674       1.1  christos /*    but have not yet been removed from the state table.  The two states   */
   8675       1.1  christos /*    TCPS_TIME_WAIT and TCPS_CLOSED are considered to be the perfect       */
   8676       1.1  christos /*    candidates for this style of removal.  If freeing up entries in       */
   8677       1.1  christos /*    CLOSED or both CLOSED and TIME_WAIT brings us to the low watermark,   */
   8678       1.1  christos /*    we do not go on to step 2.                                            */
   8679       1.1  christos /*                                                                          */
   8680       1.1  christos /* 2) Look for the oldest entries on each timeout queue and free them if    */
   8681       1.1  christos /*    they are within the given window we are considering.  Where the       */
   8682       1.1  christos /*    window starts and the steps taken to increase its size depend upon    */
   8683       1.1  christos /*    how long ipf has been running (ipf_ticks.)  Anything modified in the  */
   8684       1.1  christos /*    last 30 seconds is not touched.                                       */
   8685       1.1  christos /*                                              touched                     */
   8686       1.1  christos /*         die     ipf_ticks  30*1.5    1800*1.5   |  43200*1.5             */
   8687       1.1  christos /*           |          |        |           |     |     |                  */
   8688       1.1  christos /* future <--+----------+--------+-----------+-----+-----+-----------> past */
   8689       1.1  christos /*                     now        \_int=30s_/ \_int=1hr_/ \_int=12hr        */
   8690       1.1  christos /*                                                                          */
   8691       1.1  christos /* Points to note:                                                          */
   8692       1.1  christos /* - tqe_die is the time, in the future, when entries die.                  */
   8693       1.1  christos /* - tqe_die - ipf_ticks is how long left the connection has to live in ipf */
   8694       1.1  christos /*   ticks.                                                                 */
   8695       1.1  christos /* - tqe_touched is when the entry was last used by NAT/state               */
   8696       1.1  christos /* - the closer tqe_touched is to ipf_ticks, the further tqe_die will be    */
   8697       1.1  christos /*   ipf_ticks any given timeout queue and vice versa.                      */
   8698       1.1  christos /* - both tqe_die and tqe_touched increase over time                        */
   8699       1.1  christos /* - timeout queues are sorted with the highest value of tqe_die at the     */
   8700       1.1  christos /*   bottom and therefore the smallest values of each are at the top        */
   8701       1.1  christos /* - the pointer passed in as ipfqs should point to an array of timeout     */
   8702       1.1  christos /*   queues representing each of the TCP states                             */
   8703       1.1  christos /*                                                                          */
   8704       1.1  christos /* We start by setting up a maximum range to scan for things to move of     */
   8705       1.1  christos /* iend (newest) to istart (oldest) in chunks of "interval".  If nothing is */
   8706       1.1  christos /* found in that range, "interval" is adjusted (so long as it isn't 30) and */
   8707       1.1  christos /* we start again with a new value for "iend" and "istart".  This is        */
   8708       1.1  christos /* continued until we either finish the scan of 30 second intervals or the  */
   8709       1.1  christos /* low water mark is reached.                                               */
   8710       1.1  christos /* ------------------------------------------------------------------------ */
   8711       1.1  christos int
   8712       1.2  christos ipf_queueflush(ipf_main_softc_t *softc, ipftq_delete_fn_t deletefn,
   8713       1.2  christos     ipftq_t *ipfqs, ipftq_t *userqs, u_int *activep, int size, int low)
   8714       1.1  christos {
   8715       1.1  christos 	u_long interval, istart, iend;
   8716       1.1  christos 	ipftq_t *ifq, *ifqnext;
   8717       1.1  christos 	ipftqent_t *tqe, *tqn;
   8718       1.1  christos 	int removed = 0;
   8719       1.1  christos 
   8720       1.1  christos 	for (tqn = ipfqs[IPF_TCPS_CLOSED].ifq_head; ((tqe = tqn) != NULL); ) {
   8721       1.1  christos 		tqn = tqe->tqe_next;
   8722       1.1  christos 		if ((*deletefn)(softc, tqe->tqe_parent) == 0)
   8723       1.1  christos 			removed++;
   8724       1.1  christos 	}
   8725       1.1  christos 	if ((*activep * 100 / size) > low) {
   8726       1.1  christos 		for (tqn = ipfqs[IPF_TCPS_TIME_WAIT].ifq_head;
   8727       1.1  christos 		     ((tqe = tqn) != NULL); ) {
   8728       1.1  christos 			tqn = tqe->tqe_next;
   8729       1.1  christos 			if ((*deletefn)(softc, tqe->tqe_parent) == 0)
   8730       1.1  christos 				removed++;
   8731       1.1  christos 		}
   8732       1.1  christos 	}
   8733       1.1  christos 
   8734       1.1  christos 	if ((*activep * 100 / size) <= low) {
   8735       1.1  christos 		return removed;
   8736       1.1  christos 	}
   8737       1.1  christos 
   8738       1.1  christos 	/*
   8739       1.1  christos 	 * NOTE: Use of "* 15 / 10" is required here because if "* 1.5" is
   8740       1.1  christos 	 *       used then the operations are upgraded to floating point
   8741       1.1  christos 	 *       and kernels don't like floating point...
   8742       1.1  christos 	 */
   8743       1.1  christos 	if (softc->ipf_ticks > IPF_TTLVAL(43200 * 15 / 10)) {
   8744       1.1  christos 		istart = IPF_TTLVAL(86400 * 4);
   8745       1.1  christos 		interval = IPF_TTLVAL(43200);
   8746       1.1  christos 	} else if (softc->ipf_ticks > IPF_TTLVAL(1800 * 15 / 10)) {
   8747       1.1  christos 		istart = IPF_TTLVAL(43200);
   8748       1.1  christos 		interval = IPF_TTLVAL(1800);
   8749       1.1  christos 	} else if (softc->ipf_ticks > IPF_TTLVAL(30 * 15 / 10)) {
   8750       1.1  christos 		istart = IPF_TTLVAL(1800);
   8751       1.1  christos 		interval = IPF_TTLVAL(30);
   8752       1.1  christos 	} else {
   8753       1.1  christos 		return 0;
   8754       1.1  christos 	}
   8755       1.1  christos 	if (istart > softc->ipf_ticks) {
   8756       1.1  christos 		if (softc->ipf_ticks - interval < interval)
   8757       1.1  christos 			istart = interval;
   8758       1.1  christos 		else
   8759       1.1  christos 			istart = (softc->ipf_ticks / interval) * interval;
   8760       1.1  christos 	}
   8761       1.1  christos 
   8762       1.1  christos 	iend = softc->ipf_ticks - interval;
   8763       1.1  christos 
   8764       1.1  christos 	while ((*activep * 100 / size) > low) {
   8765       1.1  christos 		u_long try;
   8766       1.1  christos 
   8767       1.1  christos 		try = softc->ipf_ticks - istart;
   8768       1.1  christos 
   8769       1.1  christos 		for (ifq = ipfqs; ifq != NULL; ifq = ifq->ifq_next) {
   8770       1.1  christos 			for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
   8771       1.1  christos 				if (try < tqe->tqe_touched)
   8772       1.1  christos 					break;
   8773       1.1  christos 				tqn = tqe->tqe_next;
   8774       1.1  christos 				if ((*deletefn)(softc, tqe->tqe_parent) == 0)
   8775       1.1  christos 					removed++;
   8776       1.1  christos 			}
   8777       1.1  christos 		}
   8778       1.1  christos 
   8779       1.1  christos 		for (ifq = userqs; ifq != NULL; ifq = ifqnext) {
   8780       1.1  christos 			ifqnext = ifq->ifq_next;
   8781       1.1  christos 
   8782       1.1  christos 			for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); ) {
   8783       1.1  christos 				if (try < tqe->tqe_touched)
   8784       1.1  christos 					break;
   8785       1.1  christos 				tqn = tqe->tqe_next;
   8786       1.1  christos 				if ((*deletefn)(softc, tqe->tqe_parent) == 0)
   8787       1.1  christos 					removed++;
   8788       1.1  christos 			}
   8789       1.1  christos 		}
   8790       1.1  christos 
   8791       1.1  christos 		if (try >= iend) {
   8792       1.1  christos 			if (interval == IPF_TTLVAL(43200)) {
   8793       1.1  christos 				interval = IPF_TTLVAL(1800);
   8794       1.1  christos 			} else if (interval == IPF_TTLVAL(1800)) {
   8795       1.1  christos 				interval = IPF_TTLVAL(30);
   8796       1.1  christos 			} else {
   8797       1.1  christos 				break;
   8798       1.1  christos 			}
   8799       1.1  christos 			if (interval >= softc->ipf_ticks)
   8800       1.1  christos 				break;
   8801       1.1  christos 
   8802       1.1  christos 			iend = softc->ipf_ticks - interval;
   8803       1.1  christos 		}
   8804       1.1  christos 		istart -= interval;
   8805       1.1  christos 	}
   8806       1.1  christos 
   8807       1.1  christos 	return removed;
   8808       1.1  christos }
   8809       1.1  christos 
   8810       1.1  christos 
   8811       1.1  christos /* ------------------------------------------------------------------------ */
   8812       1.1  christos /* Function:    ipf_deliverlocal                                            */
   8813       1.1  christos /* Returns:     int - 1 = local address, 0 = non-local address              */
   8814       1.1  christos /* Parameters:  softc(I)     - pointer to soft context main structure       */
   8815       1.1  christos /*              ipversion(I) - IP protocol version (4 or 6)                 */
   8816       1.1  christos /*              ifp(I)       - network interface pointer                    */
   8817       1.1  christos /*              ipaddr(I)    - IPv4/6 destination address                   */
   8818       1.1  christos /*                                                                          */
   8819       1.1  christos /* This fucntion is used to determine in the address "ipaddr" belongs to    */
   8820       1.1  christos /* the network interface represented by ifp.                                */
   8821       1.1  christos /* ------------------------------------------------------------------------ */
   8822       1.1  christos int
   8823       1.2  christos ipf_deliverlocal(ipf_main_softc_t *softc, int ipversion, void *ifp,
   8824       1.2  christos     i6addr_t *ipaddr)
   8825       1.1  christos {
   8826       1.1  christos 	i6addr_t addr;
   8827       1.1  christos 	int islocal = 0;
   8828       1.1  christos 
   8829       1.1  christos 	if (ipversion == 4) {
   8830       1.1  christos 		if (ipf_ifpaddr(softc, 4, FRI_NORMAL, ifp, &addr, NULL) == 0) {
   8831       1.1  christos 			if (addr.in4.s_addr == ipaddr->in4.s_addr)
   8832       1.1  christos 				islocal = 1;
   8833       1.1  christos 		}
   8834       1.1  christos 
   8835       1.1  christos #ifdef USE_INET6
   8836       1.1  christos 	} else if (ipversion == 6) {
   8837       1.1  christos 		if (ipf_ifpaddr(softc, 6, FRI_NORMAL, ifp, &addr, NULL) == 0) {
   8838       1.1  christos 			if (IP6_EQ(&addr, ipaddr))
   8839       1.1  christos 				islocal = 1;
   8840       1.1  christos 		}
   8841       1.1  christos #endif
   8842       1.1  christos 	}
   8843       1.1  christos 
   8844       1.1  christos 	return islocal;
   8845       1.1  christos }
   8846       1.1  christos 
   8847       1.1  christos 
   8848       1.1  christos /* ------------------------------------------------------------------------ */
   8849       1.1  christos /* Function:    ipf_settimeout                                              */
   8850       1.1  christos /* Returns:     int - 0 = success, -1 = failure                             */
   8851       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   8852       1.1  christos /*              t(I)     - pointer to tuneable array entry                  */
   8853       1.1  christos /*              p(I)     - pointer to values passed in to apply             */
   8854       1.1  christos /*                                                                          */
   8855       1.1  christos /* This function is called to set the timeout values for each distinct      */
   8856       1.1  christos /* queue timeout that is available.  When called, it calls into both the    */
   8857       1.1  christos /* state and NAT code, telling them to update their timeout queues.         */
   8858       1.1  christos /* ------------------------------------------------------------------------ */
   8859       1.1  christos static int
   8860       1.2  christos ipf_settimeout(struct ipf_main_softc_s *softc, ipftuneable_t *t,
   8861       1.2  christos     ipftuneval_t *p)
   8862       1.1  christos {
   8863       1.1  christos 
   8864       1.1  christos 	/*
   8865       1.1  christos 	 * ipf_interror should be set by the functions called here, not
   8866       1.1  christos 	 * by this function - it's just a middle man.
   8867       1.1  christos 	 */
   8868       1.1  christos 	if (ipf_state_settimeout(softc, t, p) == -1)
   8869       1.1  christos 		return -1;
   8870       1.1  christos 	if (ipf_nat_settimeout(softc, t, p) == -1)
   8871       1.1  christos 		return -1;
   8872       1.1  christos 	return 0;
   8873       1.1  christos }
   8874       1.1  christos 
   8875       1.1  christos 
   8876       1.1  christos /* ------------------------------------------------------------------------ */
   8877       1.1  christos /* Function:    ipf_apply_timeout                                           */
   8878       1.1  christos /* Returns:     int - 0 = success, -1 = failure                             */
   8879       1.1  christos /* Parameters:  head(I)    - pointer to tuneable array entry                */
   8880       1.1  christos /*              seconds(I) - pointer to values passed in to apply           */
   8881       1.1  christos /*                                                                          */
   8882       1.1  christos /* This function applies a timeout of "seconds" to the timeout queue that   */
   8883       1.1  christos /* is pointed to by "head".  All entries on this list have an expiration    */
   8884       1.1  christos /* set to be the current tick value of ipf plus the ttl.  Given that this   */
   8885       1.1  christos /* function should only be called when the delta is non-zero, the task is   */
   8886       1.1  christos /* to walk the entire list and apply the change.  The sort order will not   */
   8887       1.1  christos /* change.  The only catch is that this is O(n) across the list, so if the  */
   8888       1.1  christos /* queue has lots of entries (10s of thousands or 100s of thousands), it    */
   8889       1.1  christos /* could take a relatively long time to work through them all.              */
   8890       1.1  christos /* ------------------------------------------------------------------------ */
   8891       1.1  christos void
   8892       1.2  christos ipf_apply_timeout(ipftq_t *head, u_int seconds)
   8893       1.1  christos {
   8894       1.1  christos 	u_int oldtimeout, newtimeout;
   8895       1.1  christos 	ipftqent_t *tqe;
   8896       1.1  christos 	int delta;
   8897       1.1  christos 
   8898       1.1  christos 	MUTEX_ENTER(&head->ifq_lock);
   8899       1.1  christos 	oldtimeout = head->ifq_ttl;
   8900       1.1  christos 	newtimeout = IPF_TTLVAL(seconds);
   8901       1.1  christos 	delta = oldtimeout - newtimeout;
   8902       1.1  christos 
   8903       1.1  christos 	head->ifq_ttl = newtimeout;
   8904       1.1  christos 
   8905       1.1  christos 	for (tqe = head->ifq_head; tqe != NULL; tqe = tqe->tqe_next) {
   8906       1.1  christos 		tqe->tqe_die += delta;
   8907       1.1  christos 	}
   8908       1.1  christos 	MUTEX_EXIT(&head->ifq_lock);
   8909       1.1  christos }
   8910       1.1  christos 
   8911       1.1  christos 
   8912       1.1  christos /* ------------------------------------------------------------------------ */
   8913       1.1  christos /* Function:   ipf_settimeout_tcp                                           */
   8914       1.1  christos /* Returns:    int - 0 = successfully applied, -1 = failed                  */
   8915       1.1  christos /* Parameters: t(I)   - pointer to tuneable to change                       */
   8916       1.1  christos /*             p(I)   - pointer to new timeout information                  */
   8917       1.1  christos /*             tab(I) - pointer to table of TCP queues                      */
   8918       1.1  christos /*                                                                          */
   8919       1.1  christos /* This function applies the new timeout (p) to the TCP tunable (t) and     */
   8920       1.1  christos /* updates all of the entries on the relevant timeout queue by calling      */
   8921       1.1  christos /* ipf_apply_timeout().                                                     */
   8922       1.1  christos /* ------------------------------------------------------------------------ */
   8923       1.1  christos int
   8924       1.2  christos ipf_settimeout_tcp(ipftuneable_t *t, ipftuneval_t *p, ipftq_t *tab)
   8925       1.1  christos {
   8926       1.1  christos 	if (!strcmp(t->ipft_name, "tcp_idle_timeout") ||
   8927       1.1  christos 	    !strcmp(t->ipft_name, "tcp_established")) {
   8928       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_ESTABLISHED], p->ipftu_int);
   8929       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_close_wait")) {
   8930       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_CLOSE_WAIT], p->ipftu_int);
   8931       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_last_ack")) {
   8932       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_LAST_ACK], p->ipftu_int);
   8933       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_timeout")) {
   8934       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int);
   8935       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int);
   8936       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int);
   8937       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_listen")) {
   8938       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_LISTEN], p->ipftu_int);
   8939       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_half_established")) {
   8940       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_HALF_ESTAB], p->ipftu_int);
   8941       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_closing")) {
   8942       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_CLOSING], p->ipftu_int);
   8943       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_syn_received")) {
   8944       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_SYN_RECEIVED], p->ipftu_int);
   8945       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_syn_sent")) {
   8946       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_SYN_SENT], p->ipftu_int);
   8947       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_closed")) {
   8948       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int);
   8949       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_half_closed")) {
   8950       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_CLOSED], p->ipftu_int);
   8951       1.1  christos 	} else if (!strcmp(t->ipft_name, "tcp_time_wait")) {
   8952       1.1  christos 		ipf_apply_timeout(&tab[IPF_TCPS_TIME_WAIT], p->ipftu_int);
   8953       1.1  christos 	} else {
   8954       1.1  christos 		/*
   8955       1.1  christos 		 * ipf_interror isn't set here because it should be set
   8956       1.1  christos 		 * by whatever called this function.
   8957       1.1  christos 		 */
   8958       1.1  christos 		return -1;
   8959       1.1  christos 	}
   8960       1.1  christos 	return 0;
   8961       1.1  christos }
   8962       1.1  christos 
   8963       1.1  christos 
   8964       1.1  christos /* ------------------------------------------------------------------------ */
   8965       1.1  christos /* Function:   ipf_main_soft_create                                         */
   8966       1.1  christos /* Returns:    NULL = failure, else success                                 */
   8967       1.1  christos /* Parameters: arg(I) - pointer to soft context structure if already allocd */
   8968       1.1  christos /*                                                                          */
   8969       1.1  christos /* Create the foundation soft context structure. In circumstances where it  */
   8970       1.1  christos /* is not required to dynamically allocate the context, a pointer can be    */
   8971       1.1  christos /* passed in (rather than NULL) to a structure to be initialised.           */
   8972       1.1  christos /* The main thing of interest is that a number of locks are initialised     */
   8973       1.1  christos /* here instead of in the where might be expected - in the relevant create  */
   8974       1.1  christos /* function elsewhere.  This is done because the current locking design has */
   8975       1.1  christos /* some areas where these locks are used outside of their module.           */
   8976       1.1  christos /* Possibly the most important exercise that is done here is setting of all */
   8977       1.1  christos /* the timeout values, allowing them to be changed before init().           */
   8978       1.1  christos /* ------------------------------------------------------------------------ */
   8979       1.1  christos void *
   8980       1.2  christos ipf_main_soft_create(void *arg)
   8981       1.1  christos {
   8982       1.1  christos 	ipf_main_softc_t *softc;
   8983       1.1  christos 
   8984       1.1  christos 	if (arg == NULL) {
   8985       1.1  christos 		KMALLOC(softc, ipf_main_softc_t *);
   8986       1.1  christos 		if (softc == NULL)
   8987       1.1  christos 			return NULL;
   8988       1.1  christos 	} else {
   8989       1.1  christos 		softc = arg;
   8990       1.1  christos 	}
   8991       1.1  christos 
   8992       1.1  christos 	bzero((char *)softc, sizeof(*softc));
   8993       1.1  christos 
   8994       1.1  christos 	/*
   8995       1.1  christos 	 * This serves as a flag as to whether or not the softc should be
   8996       1.1  christos 	 * free'd when _destroy is called.
   8997       1.1  christos 	 */
   8998       1.1  christos 	softc->ipf_dynamic_softc = (arg == NULL) ? 1 : 0;
   8999       1.1  christos 
   9000       1.1  christos 	softc->ipf_tuners = ipf_tune_array_copy(softc,
   9001       1.1  christos 						sizeof(ipf_main_tuneables),
   9002       1.1  christos 						ipf_main_tuneables);
   9003       1.1  christos 	if (softc->ipf_tuners == NULL) {
   9004       1.3   darrenr 		ipf_main_soft_destroy(softc);
   9005       1.1  christos 		return NULL;
   9006       1.1  christos 	}
   9007       1.1  christos 
   9008       1.1  christos 	MUTEX_INIT(&softc->ipf_rw, "ipf rw mutex");
   9009       1.1  christos 	MUTEX_INIT(&softc->ipf_timeoutlock, "ipf timeout lock");
   9010       1.1  christos 	RWLOCK_INIT(&softc->ipf_global, "ipf filter load/unload mutex");
   9011       1.1  christos 	RWLOCK_INIT(&softc->ipf_mutex, "ipf filter rwlock");
   9012       1.1  christos 	RWLOCK_INIT(&softc->ipf_tokens, "ipf token rwlock");
   9013       1.1  christos 	RWLOCK_INIT(&softc->ipf_state, "ipf state rwlock");
   9014       1.1  christos 	RWLOCK_INIT(&softc->ipf_nat, "ipf IP NAT rwlock");
   9015       1.1  christos 	RWLOCK_INIT(&softc->ipf_poolrw, "ipf pool rwlock");
   9016       1.1  christos 	RWLOCK_INIT(&softc->ipf_frag, "ipf frag rwlock");
   9017       1.1  christos 
   9018       1.1  christos 	softc->ipf_token_head = NULL;
   9019       1.1  christos 	softc->ipf_token_tail = &softc->ipf_token_head;
   9020       1.1  christos 
   9021       1.1  christos 	softc->ipf_tcpidletimeout = FIVE_DAYS;
   9022       1.1  christos 	softc->ipf_tcpclosewait = IPF_TTLVAL(2 * TCP_MSL);
   9023       1.1  christos 	softc->ipf_tcplastack = IPF_TTLVAL(30);
   9024       1.1  christos 	softc->ipf_tcptimewait = IPF_TTLVAL(2 * TCP_MSL);
   9025       1.1  christos 	softc->ipf_tcptimeout = IPF_TTLVAL(2 * TCP_MSL);
   9026       1.1  christos 	softc->ipf_tcpsynsent = IPF_TTLVAL(2 * TCP_MSL);
   9027       1.1  christos 	softc->ipf_tcpsynrecv = IPF_TTLVAL(2 * TCP_MSL);
   9028       1.1  christos 	softc->ipf_tcpclosed = IPF_TTLVAL(30);
   9029       1.1  christos 	softc->ipf_tcphalfclosed = IPF_TTLVAL(2 * 3600);
   9030       1.1  christos 	softc->ipf_udptimeout = IPF_TTLVAL(120);
   9031       1.1  christos 	softc->ipf_udpacktimeout = IPF_TTLVAL(12);
   9032       1.1  christos 	softc->ipf_icmptimeout = IPF_TTLVAL(60);
   9033       1.1  christos 	softc->ipf_icmpacktimeout = IPF_TTLVAL(6);
   9034       1.1  christos 	softc->ipf_iptimeout = IPF_TTLVAL(60);
   9035       1.1  christos 
   9036       1.1  christos #if defined(IPFILTER_DEFAULT_BLOCK)
   9037       1.1  christos 	softc->ipf_pass = FR_BLOCK|FR_NOMATCH;
   9038       1.1  christos #else
   9039       1.1  christos 	softc->ipf_pass = (IPF_DEFAULT_PASS)|FR_NOMATCH;
   9040       1.1  christos #endif
   9041       1.1  christos 	softc->ipf_minttl = 4;
   9042       1.1  christos 	softc->ipf_icmpminfragmtu = 68;
   9043       1.1  christos 	softc->ipf_flags = IPF_LOGGING;
   9044       1.1  christos 
   9045       1.1  christos 	return softc;
   9046       1.1  christos }
   9047       1.1  christos 
   9048       1.1  christos /* ------------------------------------------------------------------------ */
   9049       1.1  christos /* Function:   ipf_main_soft_init                                           */
   9050       1.1  christos /* Returns:    0 = success, -1 = failure                                    */
   9051       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   9052       1.1  christos /*                                                                          */
   9053       1.1  christos /* A null-op function that exists as a placeholder so that the flow in      */
   9054       1.1  christos /* other functions is obvious.                                              */
   9055       1.1  christos /* ------------------------------------------------------------------------ */
   9056       1.1  christos /*ARGSUSED*/
   9057       1.1  christos int
   9058       1.2  christos ipf_main_soft_init(ipf_main_softc_t *softc)
   9059       1.1  christos {
   9060       1.1  christos 	return 0;
   9061       1.1  christos }
   9062       1.1  christos 
   9063       1.1  christos 
   9064       1.1  christos /* ------------------------------------------------------------------------ */
   9065       1.1  christos /* Function:   ipf_main_soft_destroy                                        */
   9066       1.1  christos /* Returns:    void                                                         */
   9067       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   9068       1.1  christos /*                                                                          */
   9069       1.1  christos /* Undo everything that we did in ipf_main_soft_create.                     */
   9070       1.1  christos /*                                                                          */
   9071       1.1  christos /* The most important check that needs to be made here is whether or not    */
   9072       1.1  christos /* the structure was allocated by ipf_main_soft_create() by checking what   */
   9073       1.1  christos /* value is stored in ipf_dynamic_main.                                     */
   9074       1.1  christos /* ------------------------------------------------------------------------ */
   9075       1.1  christos /*ARGSUSED*/
   9076       1.1  christos void
   9077       1.3   darrenr ipf_main_soft_destroy(ipf_main_softc_t *softc)
   9078       1.1  christos {
   9079       1.1  christos 
   9080       1.1  christos 	RW_DESTROY(&softc->ipf_frag);
   9081       1.1  christos 	RW_DESTROY(&softc->ipf_poolrw);
   9082       1.1  christos 	RW_DESTROY(&softc->ipf_nat);
   9083       1.1  christos 	RW_DESTROY(&softc->ipf_state);
   9084       1.1  christos 	RW_DESTROY(&softc->ipf_tokens);
   9085       1.1  christos 	RW_DESTROY(&softc->ipf_mutex);
   9086       1.1  christos 	RW_DESTROY(&softc->ipf_global);
   9087       1.1  christos 	MUTEX_DESTROY(&softc->ipf_timeoutlock);
   9088       1.1  christos 	MUTEX_DESTROY(&softc->ipf_rw);
   9089       1.1  christos 
   9090       1.1  christos 	if (softc->ipf_tuners != NULL) {
   9091       1.1  christos 		KFREES(softc->ipf_tuners, sizeof(ipf_main_tuneables));
   9092       1.1  christos 	}
   9093       1.1  christos 	if (softc->ipf_dynamic_softc == 1) {
   9094       1.1  christos 		KFREE(softc);
   9095       1.1  christos 	}
   9096       1.1  christos }
   9097       1.1  christos 
   9098       1.1  christos 
   9099       1.1  christos /* ------------------------------------------------------------------------ */
   9100       1.1  christos /* Function:   ipf_main_soft_fini                                           */
   9101       1.1  christos /* Returns:    0 = success, -1 = failure                                    */
   9102       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   9103       1.1  christos /*                                                                          */
   9104       1.1  christos /* Clean out the rules which have been added since _init was last called,   */
   9105       1.1  christos /* the only dynamic part of the mainline.                                   */
   9106       1.1  christos /* ------------------------------------------------------------------------ */
   9107       1.1  christos int
   9108       1.2  christos ipf_main_soft_fini(ipf_main_softc_t *softc)
   9109       1.1  christos {
   9110       1.1  christos 	(void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
   9111       1.1  christos 	(void) ipf_flush(softc, IPL_LOGIPF, FR_INQUE|FR_OUTQUE);
   9112       1.1  christos 	(void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE|FR_INACTIVE);
   9113       1.1  christos 	(void) ipf_flush(softc, IPL_LOGCOUNT, FR_INQUE|FR_OUTQUE);
   9114       1.1  christos 
   9115       1.1  christos 	return 0;
   9116       1.1  christos }
   9117       1.1  christos 
   9118       1.1  christos 
   9119       1.1  christos /* ------------------------------------------------------------------------ */
   9120       1.1  christos /* Function:   ipf_main_load                                                */
   9121       1.1  christos /* Returns:    0 = success, -1 = failure                                    */
   9122       1.1  christos /* Parameters: none                                                         */
   9123       1.1  christos /*                                                                          */
   9124       1.1  christos /* Handle global initialisation that needs to be done for the base part of  */
   9125       1.1  christos /* IPFilter. At present this just amounts to initialising some ICMP lookup  */
   9126       1.1  christos /* arrays that get used by the state/NAT code.                              */
   9127       1.1  christos /* ------------------------------------------------------------------------ */
   9128       1.1  christos int
   9129       1.2  christos ipf_main_load(void)
   9130       1.1  christos {
   9131       1.1  christos 	int i;
   9132       1.1  christos 
   9133       1.1  christos 	/* fill icmp reply type table */
   9134       1.1  christos 	for (i = 0; i <= ICMP_MAXTYPE; i++)
   9135       1.1  christos 		icmpreplytype4[i] = -1;
   9136       1.1  christos 	icmpreplytype4[ICMP_ECHO] = ICMP_ECHOREPLY;
   9137       1.1  christos 	icmpreplytype4[ICMP_TSTAMP] = ICMP_TSTAMPREPLY;
   9138       1.1  christos 	icmpreplytype4[ICMP_IREQ] = ICMP_IREQREPLY;
   9139       1.1  christos 	icmpreplytype4[ICMP_MASKREQ] = ICMP_MASKREPLY;
   9140       1.1  christos 
   9141       1.1  christos #ifdef  USE_INET6
   9142       1.1  christos 	/* fill icmp reply type table */
   9143       1.1  christos 	for (i = 0; i <= ICMP6_MAXTYPE; i++)
   9144       1.1  christos 		icmpreplytype6[i] = -1;
   9145       1.1  christos 	icmpreplytype6[ICMP6_ECHO_REQUEST] = ICMP6_ECHO_REPLY;
   9146       1.1  christos 	icmpreplytype6[ICMP6_MEMBERSHIP_QUERY] = ICMP6_MEMBERSHIP_REPORT;
   9147       1.1  christos 	icmpreplytype6[ICMP6_NI_QUERY] = ICMP6_NI_REPLY;
   9148       1.1  christos 	icmpreplytype6[ND_ROUTER_SOLICIT] = ND_ROUTER_ADVERT;
   9149       1.1  christos 	icmpreplytype6[ND_NEIGHBOR_SOLICIT] = ND_NEIGHBOR_ADVERT;
   9150       1.1  christos #endif
   9151       1.1  christos 
   9152       1.1  christos 	return 0;
   9153       1.1  christos }
   9154       1.1  christos 
   9155       1.1  christos 
   9156       1.1  christos /* ------------------------------------------------------------------------ */
   9157       1.1  christos /* Function:   ipf_main_unload                                              */
   9158       1.1  christos /* Returns:    0 = success, -1 = failure                                    */
   9159       1.1  christos /* Parameters: none                                                         */
   9160       1.1  christos /*                                                                          */
   9161       1.1  christos /* A null-op function that exists as a placeholder so that the flow in      */
   9162       1.1  christos /* other functions is obvious.                                              */
   9163       1.1  christos /* ------------------------------------------------------------------------ */
   9164       1.1  christos int
   9165       1.2  christos ipf_main_unload(void)
   9166       1.1  christos {
   9167       1.1  christos 	return 0;
   9168       1.1  christos }
   9169       1.1  christos 
   9170       1.1  christos 
   9171       1.1  christos /* ------------------------------------------------------------------------ */
   9172       1.1  christos /* Function:   ipf_load_all                                                 */
   9173       1.1  christos /* Returns:    0 = success, -1 = failure                                    */
   9174       1.1  christos /* Parameters: none                                                         */
   9175       1.1  christos /*                                                                          */
   9176       1.1  christos /* Work through all of the subsystems inside IPFilter and call the load     */
   9177       1.1  christos /* function for each in an order that won't lead to a crash :)              */
   9178       1.1  christos /* ------------------------------------------------------------------------ */
   9179       1.1  christos int
   9180       1.2  christos ipf_load_all(void)
   9181       1.1  christos {
   9182       1.1  christos 	if (ipf_main_load() == -1)
   9183       1.1  christos 		return -1;
   9184       1.1  christos 
   9185       1.1  christos 	if (ipf_state_main_load() == -1)
   9186       1.1  christos 		return -1;
   9187       1.1  christos 
   9188       1.1  christos 	if (ipf_nat_main_load() == -1)
   9189       1.1  christos 		return -1;
   9190       1.1  christos 
   9191       1.1  christos 	if (ipf_frag_main_load() == -1)
   9192       1.1  christos 		return -1;
   9193       1.1  christos 
   9194       1.1  christos 	if (ipf_auth_main_load() == -1)
   9195       1.1  christos 		return -1;
   9196       1.1  christos 
   9197       1.1  christos 	if (ipf_proxy_main_load() == -1)
   9198       1.1  christos 		return -1;
   9199       1.1  christos 
   9200       1.1  christos 	return 0;
   9201       1.1  christos }
   9202       1.1  christos 
   9203       1.1  christos 
   9204       1.1  christos /* ------------------------------------------------------------------------ */
   9205       1.1  christos /* Function:   ipf_unload_all                                               */
   9206       1.1  christos /* Returns:    0 = success, -1 = failure                                    */
   9207       1.1  christos /* Parameters: none                                                         */
   9208       1.1  christos /*                                                                          */
   9209       1.1  christos /* Work through all of the subsystems inside IPFilter and call the unload   */
   9210       1.1  christos /* function for each in an order that won't lead to a crash :)              */
   9211       1.1  christos /* ------------------------------------------------------------------------ */
   9212       1.1  christos int
   9213       1.2  christos ipf_unload_all(void)
   9214       1.1  christos {
   9215       1.1  christos 	if (ipf_proxy_main_unload() == -1)
   9216       1.1  christos 		return -1;
   9217       1.1  christos 
   9218       1.1  christos 	if (ipf_auth_main_unload() == -1)
   9219       1.1  christos 		return -1;
   9220       1.1  christos 
   9221       1.1  christos 	if (ipf_frag_main_unload() == -1)
   9222       1.1  christos 		return -1;
   9223       1.1  christos 
   9224       1.1  christos 	if (ipf_nat_main_unload() == -1)
   9225       1.1  christos 		return -1;
   9226       1.1  christos 
   9227       1.1  christos 	if (ipf_state_main_unload() == -1)
   9228       1.1  christos 		return -1;
   9229       1.1  christos 
   9230       1.1  christos 	if (ipf_main_unload() == -1)
   9231       1.1  christos 		return -1;
   9232       1.1  christos 
   9233       1.1  christos 	return 0;
   9234       1.1  christos }
   9235       1.1  christos 
   9236       1.1  christos 
   9237       1.1  christos /* ------------------------------------------------------------------------ */
   9238       1.1  christos /* Function:   ipf_create_all                                               */
   9239       1.1  christos /* Returns:    NULL = failure, else success                                 */
   9240       1.1  christos /* Parameters: arg(I) - pointer to soft context main structure              */
   9241       1.1  christos /*                                                                          */
   9242       1.1  christos /* Work through all of the subsystems inside IPFilter and call the create   */
   9243       1.1  christos /* function for each in an order that won't lead to a crash :)              */
   9244       1.1  christos /* ------------------------------------------------------------------------ */
   9245       1.1  christos ipf_main_softc_t *
   9246       1.2  christos ipf_create_all(void *arg)
   9247       1.1  christos {
   9248       1.1  christos 	ipf_main_softc_t *softc;
   9249       1.1  christos 
   9250       1.1  christos 	softc = ipf_main_soft_create(arg);
   9251       1.1  christos 	if (softc == NULL)
   9252       1.1  christos 		return NULL;
   9253       1.1  christos 
   9254       1.2  christos #ifdef IPFILTER_LOG
   9255       1.1  christos 	softc->ipf_log_soft = ipf_log_soft_create(softc);
   9256       1.1  christos 	if (softc->ipf_log_soft == NULL) {
   9257       1.1  christos 		ipf_destroy_all(softc);
   9258       1.1  christos 		return NULL;
   9259       1.1  christos 	}
   9260       1.2  christos #endif
   9261       1.1  christos 
   9262       1.1  christos 	softc->ipf_lookup_soft = ipf_lookup_soft_create(softc);
   9263       1.1  christos 	if (softc->ipf_lookup_soft == NULL) {
   9264       1.1  christos 		ipf_destroy_all(softc);
   9265       1.1  christos 		return NULL;
   9266       1.1  christos 	}
   9267       1.1  christos 
   9268       1.1  christos 	softc->ipf_sync_soft = ipf_sync_soft_create(softc);
   9269       1.1  christos 	if (softc->ipf_sync_soft == NULL) {
   9270       1.1  christos 		ipf_destroy_all(softc);
   9271       1.1  christos 		return NULL;
   9272       1.1  christos 	}
   9273       1.1  christos 
   9274       1.1  christos 	softc->ipf_state_soft = ipf_state_soft_create(softc);
   9275       1.1  christos 	if (softc->ipf_state_soft == NULL) {
   9276       1.1  christos 		ipf_destroy_all(softc);
   9277       1.1  christos 		return NULL;
   9278       1.1  christos 	}
   9279       1.1  christos 
   9280       1.1  christos 	softc->ipf_nat_soft = ipf_nat_soft_create(softc);
   9281       1.1  christos 	if (softc->ipf_nat_soft == NULL) {
   9282       1.1  christos 		ipf_destroy_all(softc);
   9283       1.1  christos 		return NULL;
   9284       1.1  christos 	}
   9285       1.1  christos 
   9286       1.1  christos 	softc->ipf_frag_soft = ipf_frag_soft_create(softc);
   9287       1.1  christos 	if (softc->ipf_frag_soft == NULL) {
   9288       1.1  christos 		ipf_destroy_all(softc);
   9289       1.1  christos 		return NULL;
   9290       1.1  christos 	}
   9291       1.1  christos 
   9292       1.1  christos 	softc->ipf_auth_soft = ipf_auth_soft_create(softc);
   9293       1.1  christos 	if (softc->ipf_auth_soft == NULL) {
   9294       1.1  christos 		ipf_destroy_all(softc);
   9295       1.1  christos 		return NULL;
   9296       1.1  christos 	}
   9297       1.1  christos 
   9298       1.1  christos 	softc->ipf_proxy_soft = ipf_proxy_soft_create(softc);
   9299       1.1  christos 	if (softc->ipf_proxy_soft == NULL) {
   9300       1.1  christos 		ipf_destroy_all(softc);
   9301       1.1  christos 		return NULL;
   9302       1.1  christos 	}
   9303       1.1  christos 
   9304       1.1  christos 	return softc;
   9305       1.1  christos }
   9306       1.1  christos 
   9307       1.1  christos 
   9308       1.1  christos /* ------------------------------------------------------------------------ */
   9309       1.1  christos /* Function:   ipf_destroy_all                                              */
   9310       1.1  christos /* Returns:    void                                                         */
   9311       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   9312       1.1  christos /*                                                                          */
   9313       1.1  christos /* Work through all of the subsystems inside IPFilter and call the destroy  */
   9314       1.1  christos /* function for each in an order that won't lead to a crash :)              */
   9315       1.1  christos /*                                                                          */
   9316       1.1  christos /* Every one of these functions is expected to succeed, so there is no      */
   9317       1.1  christos /* checking of return values.                                               */
   9318       1.1  christos /* ------------------------------------------------------------------------ */
   9319       1.1  christos void
   9320       1.2  christos ipf_destroy_all(ipf_main_softc_t *softc)
   9321       1.1  christos {
   9322       1.1  christos 
   9323       1.1  christos 	if (softc->ipf_state_soft != NULL) {
   9324       1.1  christos 		ipf_state_soft_destroy(softc, softc->ipf_state_soft);
   9325       1.1  christos 		softc->ipf_state_soft = NULL;
   9326       1.1  christos 	}
   9327       1.1  christos 
   9328       1.1  christos 	if (softc->ipf_nat_soft != NULL) {
   9329       1.1  christos 		ipf_nat_soft_destroy(softc, softc->ipf_nat_soft);
   9330       1.1  christos 		softc->ipf_nat_soft = NULL;
   9331       1.1  christos 	}
   9332       1.1  christos 
   9333       1.1  christos 	if (softc->ipf_frag_soft != NULL) {
   9334       1.1  christos 		ipf_frag_soft_destroy(softc, softc->ipf_frag_soft);
   9335       1.1  christos 		softc->ipf_frag_soft = NULL;
   9336       1.1  christos 	}
   9337       1.1  christos 
   9338       1.1  christos 	if (softc->ipf_auth_soft != NULL) {
   9339       1.1  christos 		ipf_auth_soft_destroy(softc, softc->ipf_auth_soft);
   9340       1.1  christos 		softc->ipf_auth_soft = NULL;
   9341       1.1  christos 	}
   9342       1.1  christos 
   9343       1.1  christos 	if (softc->ipf_proxy_soft != NULL) {
   9344       1.1  christos 		ipf_proxy_soft_destroy(softc, softc->ipf_proxy_soft);
   9345       1.1  christos 		softc->ipf_proxy_soft = NULL;
   9346       1.1  christos 	}
   9347       1.1  christos 
   9348       1.1  christos 	if (softc->ipf_sync_soft != NULL) {
   9349       1.1  christos 		ipf_sync_soft_destroy(softc, softc->ipf_sync_soft);
   9350       1.1  christos 		softc->ipf_sync_soft = NULL;
   9351       1.1  christos 	}
   9352       1.1  christos 
   9353       1.1  christos 	if (softc->ipf_lookup_soft != NULL) {
   9354       1.1  christos 		ipf_lookup_soft_destroy(softc, softc->ipf_lookup_soft);
   9355       1.1  christos 		softc->ipf_lookup_soft = NULL;
   9356       1.1  christos 	}
   9357       1.1  christos 
   9358       1.2  christos #ifdef IPFILTER_LOG
   9359       1.1  christos 	if (softc->ipf_log_soft != NULL) {
   9360       1.1  christos 		ipf_log_soft_destroy(softc, softc->ipf_log_soft);
   9361       1.1  christos 		softc->ipf_log_soft = NULL;
   9362       1.1  christos 	}
   9363       1.2  christos #endif
   9364       1.1  christos 
   9365       1.3   darrenr 	ipf_main_soft_destroy(softc);
   9366       1.1  christos }
   9367       1.1  christos 
   9368       1.1  christos 
   9369       1.1  christos /* ------------------------------------------------------------------------ */
   9370       1.1  christos /* Function:   ipf_init_all                                                 */
   9371       1.1  christos /* Returns:    0 = success, -1 = failure                                    */
   9372       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   9373       1.1  christos /*                                                                          */
   9374       1.1  christos /* Work through all of the subsystems inside IPFilter and call the init     */
   9375       1.1  christos /* function for each in an order that won't lead to a crash :)              */
   9376       1.1  christos /* ------------------------------------------------------------------------ */
   9377       1.1  christos int
   9378       1.2  christos ipf_init_all(ipf_main_softc_t *softc)
   9379       1.1  christos {
   9380       1.1  christos 
   9381       1.1  christos 	if (ipf_main_soft_init(softc) == -1)
   9382       1.1  christos 		return -1;
   9383       1.1  christos 
   9384       1.2  christos #ifdef IPFILTER_LOG
   9385       1.1  christos 	if (ipf_log_soft_init(softc, softc->ipf_log_soft) == -1)
   9386       1.1  christos 		return -1;
   9387       1.2  christos #endif
   9388       1.1  christos 
   9389       1.1  christos 	if (ipf_lookup_soft_init(softc, softc->ipf_lookup_soft) == -1)
   9390       1.1  christos 		return -1;
   9391       1.1  christos 
   9392       1.1  christos 	if (ipf_sync_soft_init(softc, softc->ipf_sync_soft) == -1)
   9393       1.1  christos 		return -1;
   9394       1.1  christos 
   9395       1.1  christos 	if (ipf_state_soft_init(softc, softc->ipf_state_soft) == -1)
   9396       1.1  christos 		return -1;
   9397       1.1  christos 
   9398       1.1  christos 	if (ipf_nat_soft_init(softc, softc->ipf_nat_soft) == -1)
   9399       1.1  christos 		return -1;
   9400       1.1  christos 
   9401       1.1  christos 	if (ipf_frag_soft_init(softc, softc->ipf_frag_soft) == -1)
   9402       1.1  christos 		return -1;
   9403       1.1  christos 
   9404       1.1  christos 	if (ipf_auth_soft_init(softc, softc->ipf_auth_soft) == -1)
   9405       1.1  christos 		return -1;
   9406       1.1  christos 
   9407       1.1  christos 	if (ipf_proxy_soft_init(softc, softc->ipf_proxy_soft) == -1)
   9408       1.1  christos 		return -1;
   9409       1.1  christos 
   9410       1.1  christos 	return 0;
   9411       1.1  christos }
   9412       1.1  christos 
   9413       1.1  christos 
   9414       1.1  christos /* ------------------------------------------------------------------------ */
   9415       1.1  christos /* Function:   ipf_fini_all                                                 */
   9416       1.1  christos /* Returns:    0 = success, -1 = failure                                    */
   9417       1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
   9418       1.1  christos /*                                                                          */
   9419       1.1  christos /* Work through all of the subsystems inside IPFilter and call the fini     */
   9420       1.1  christos /* function for each in an order that won't lead to a crash :)              */
   9421       1.1  christos /* ------------------------------------------------------------------------ */
   9422       1.1  christos int
   9423       1.2  christos ipf_fini_all(ipf_main_softc_t *softc)
   9424       1.1  christos {
   9425       1.1  christos 
   9426       1.3   darrenr 	ipf_token_flush(softc);
   9427       1.3   darrenr 
   9428       1.1  christos 	if (ipf_proxy_soft_fini(softc, softc->ipf_proxy_soft) == -1)
   9429       1.1  christos 		return -1;
   9430       1.1  christos 
   9431       1.1  christos 	if (ipf_auth_soft_fini(softc, softc->ipf_auth_soft) == -1)
   9432       1.1  christos 		return -1;
   9433       1.1  christos 
   9434       1.1  christos 	if (ipf_frag_soft_fini(softc, softc->ipf_frag_soft) == -1)
   9435       1.1  christos 		return -1;
   9436       1.1  christos 
   9437       1.1  christos 	if (ipf_nat_soft_fini(softc, softc->ipf_nat_soft) == -1)
   9438       1.1  christos 		return -1;
   9439       1.1  christos 
   9440       1.1  christos 	if (ipf_state_soft_fini(softc, softc->ipf_state_soft) == -1)
   9441       1.1  christos 		return -1;
   9442       1.1  christos 
   9443       1.1  christos 	if (ipf_sync_soft_fini(softc, softc->ipf_sync_soft) == -1)
   9444       1.1  christos 		return -1;
   9445       1.1  christos 
   9446       1.1  christos 	if (ipf_lookup_soft_fini(softc, softc->ipf_lookup_soft) == -1)
   9447       1.1  christos 		return -1;
   9448       1.1  christos 
   9449       1.2  christos #ifdef IPFILTER_LOG
   9450       1.1  christos 	if (ipf_log_soft_fini(softc, softc->ipf_log_soft) == -1)
   9451       1.1  christos 		return -1;
   9452       1.2  christos #endif
   9453       1.1  christos 
   9454       1.1  christos 	if (ipf_main_soft_fini(softc) == -1)
   9455       1.1  christos 		return -1;
   9456       1.1  christos 
   9457       1.1  christos 	return 0;
   9458       1.1  christos }
   9459       1.1  christos 
   9460       1.1  christos 
   9461       1.1  christos /* ------------------------------------------------------------------------ */
   9462       1.1  christos /* Function:    ipf_rule_expire                                             */
   9463       1.1  christos /* Returns:     Nil                                                         */
   9464       1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   9465       1.1  christos /*                                                                          */
   9466       1.1  christos /* At present this function exists just to support temporary addition of    */
   9467       1.1  christos /* firewall rules. Both inactive and active lists are scanned for items to  */
   9468       1.1  christos /* purge, as by rights, the expiration is computed as soon as the rule is   */
   9469       1.1  christos /* loaded in.                                                               */
   9470       1.1  christos /* ------------------------------------------------------------------------ */
   9471       1.1  christos void
   9472       1.2  christos ipf_rule_expire(ipf_main_softc_t *softc)
   9473       1.1  christos {
   9474       1.1  christos 	frentry_t *fr;
   9475       1.1  christos 
   9476       1.1  christos 	if ((softc->ipf_rule_explist[0] == NULL) &&
   9477       1.1  christos 	    (softc->ipf_rule_explist[1] == NULL))
   9478       1.1  christos 		return;
   9479       1.1  christos 
   9480       1.1  christos 	WRITE_ENTER(&softc->ipf_mutex);
   9481       1.1  christos 
   9482       1.1  christos 	while ((fr = softc->ipf_rule_explist[0]) != NULL) {
   9483       1.1  christos 		/*
   9484       1.1  christos 		 * Because the list is kept sorted on insertion, the fist
   9485       1.1  christos 		 * one that dies in the future means no more work to do.
   9486       1.1  christos 		 */
   9487       1.1  christos 		if (fr->fr_die > softc->ipf_ticks)
   9488       1.1  christos 			break;
   9489       1.1  christos 		ipf_rule_delete(softc, fr, IPL_LOGIPF, 0);
   9490       1.1  christos 	}
   9491       1.1  christos 
   9492       1.1  christos 	while ((fr = softc->ipf_rule_explist[1]) != NULL) {
   9493       1.1  christos 		/*
   9494       1.1  christos 		 * Because the list is kept sorted on insertion, the fist
   9495       1.1  christos 		 * one that dies in the future means no more work to do.
   9496       1.1  christos 		 */
   9497       1.1  christos 		if (fr->fr_die > softc->ipf_ticks)
   9498       1.1  christos 			break;
   9499       1.1  christos 		ipf_rule_delete(softc, fr, IPL_LOGIPF, 1);
   9500       1.1  christos 	}
   9501       1.1  christos 
   9502       1.1  christos 	RWLOCK_EXIT(&softc->ipf_mutex);
   9503       1.1  christos }
   9504       1.1  christos 
   9505       1.1  christos 
   9506       1.7  christos static int ipf_ht_node_cmp(const struct host_node_s *, const struct host_node_s *);
   9507       1.2  christos static void ipf_ht_node_make_key(host_track_t *, host_node_t *, int,
   9508       1.2  christos 				 i6addr_t *);
   9509       1.1  christos 
   9510       1.3   darrenr RBI_CODE(ipf_rb, host_node_t, hn_entry, ipf_ht_node_cmp)
   9511       1.1  christos 
   9512       1.1  christos 
   9513       1.1  christos /* ------------------------------------------------------------------------ */
   9514       1.1  christos /* Function:    ipf_ht_node_cmp                                             */
   9515       1.1  christos /* Returns:     int   - 0 == nodes are the same, ..                         */
   9516       1.1  christos /* Parameters:  k1(I) - pointer to first key to compare                     */
   9517       1.1  christos /*              k2(I) - pointer to second key to compare                    */
   9518       1.1  christos /*                                                                          */
   9519       1.1  christos /* The "key" for the node is a combination of two fields: the address       */
   9520       1.1  christos /* family and the address itself.                                           */
   9521       1.1  christos /*                                                                          */
   9522       1.1  christos /* Because we're not actually interpreting the address data, it isn't       */
   9523       1.1  christos /* necessary to convert them to/from network/host byte order. The mask is   */
   9524       1.1  christos /* just used to remove bits that aren't significant - it doesn't matter     */
   9525       1.1  christos /* where they are, as long as they're always in the same place.             */
   9526       1.1  christos /*                                                                          */
   9527       1.1  christos /* As with IP6_EQ, comparing IPv6 addresses starts at the bottom because    */
   9528       1.1  christos /* this is where individual ones will differ the most - but not true for    */
   9529       1.1  christos /* for /48's, etc.                                                          */
   9530       1.1  christos /* ------------------------------------------------------------------------ */
   9531       1.1  christos static int
   9532       1.7  christos ipf_ht_node_cmp(const struct host_node_s *k1, const struct host_node_s *k2)
   9533       1.1  christos {
   9534       1.1  christos 	int i;
   9535       1.1  christos 
   9536       1.1  christos 	i = (k2->hn_addr.adf_family - k1->hn_addr.adf_family);
   9537       1.1  christos 	if (i != 0)
   9538       1.1  christos 		return i;
   9539       1.1  christos 
   9540       1.1  christos 	if (k1->hn_addr.adf_family == AF_INET)
   9541       1.1  christos 		return (k2->hn_addr.adf_addr.in4.s_addr -
   9542       1.1  christos 			k1->hn_addr.adf_addr.in4.s_addr);
   9543       1.1  christos 
   9544       1.1  christos 	i = k2->hn_addr.adf_addr.i6[3] - k1->hn_addr.adf_addr.i6[3];
   9545       1.1  christos 	if (i != 0)
   9546       1.1  christos 		return i;
   9547       1.1  christos 	i = k2->hn_addr.adf_addr.i6[2] - k1->hn_addr.adf_addr.i6[2];
   9548       1.1  christos 	if (i != 0)
   9549       1.1  christos 		return i;
   9550       1.1  christos 	i = k2->hn_addr.adf_addr.i6[1] - k1->hn_addr.adf_addr.i6[1];
   9551       1.1  christos 	if (i != 0)
   9552       1.1  christos 		return i;
   9553       1.1  christos 	i = k2->hn_addr.adf_addr.i6[0] - k1->hn_addr.adf_addr.i6[0];
   9554       1.1  christos 	return i;
   9555       1.1  christos }
   9556       1.1  christos 
   9557       1.1  christos 
   9558       1.1  christos /* ------------------------------------------------------------------------ */
   9559       1.1  christos /* Function:    ipf_ht_node_make_key                                        */
   9560       1.1  christos /* Returns:     Nil                                                         */
   9561       1.1  christos /* parameters:  htp(I)    - pointer to address tracking structure           */
   9562       1.1  christos /*              key(I)    - where to store masked address for lookup        */
   9563       1.1  christos /*              family(I) - protocol family of address                      */
   9564       1.1  christos /*              addr(I)   - pointer to network address                      */
   9565       1.1  christos /*                                                                          */
   9566       1.1  christos /* Using the "netmask" (number of bits) stored parent host tracking struct, */
   9567       1.1  christos /* copy the address passed in into the key structure whilst masking out the */
   9568       1.1  christos /* bits that we don't want.                                                 */
   9569       1.1  christos /*                                                                          */
   9570       1.1  christos /* Because the parser will set ht_netmask to 128 if there is no protocol    */
   9571       1.1  christos /* specified (the parser doesn't know if it should be a v4 or v6 rule), we  */
   9572       1.1  christos /* have to be wary of that and not allow 32-128 to happen.                  */
   9573       1.1  christos /* ------------------------------------------------------------------------ */
   9574       1.1  christos static void
   9575       1.2  christos ipf_ht_node_make_key(host_track_t *htp, host_node_t *key, int family,
   9576       1.2  christos     i6addr_t *addr)
   9577       1.1  christos {
   9578       1.1  christos 	key->hn_addr.adf_family = family;
   9579       1.1  christos 	if (family == AF_INET) {
   9580       1.1  christos 		u_32_t mask;
   9581       1.1  christos 		int bits;
   9582       1.1  christos 
   9583       1.1  christos 		key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in4);
   9584       1.1  christos 		bits = htp->ht_netmask;
   9585       1.1  christos 		if (bits >= 32) {
   9586       1.1  christos 			mask = 0xffffffff;
   9587       1.1  christos 		} else {
   9588       1.1  christos 			mask = htonl(0xffffffff << (32 - bits));
   9589       1.1  christos 		}
   9590       1.1  christos 		key->hn_addr.adf_addr.in4.s_addr = addr->in4.s_addr & mask;
   9591       1.2  christos #ifdef USE_INET6
   9592       1.1  christos 	} else {
   9593       1.1  christos 		int bits = htp->ht_netmask;
   9594       1.1  christos 
   9595       1.1  christos 		key->hn_addr.adf_len = sizeof(key->hn_addr.adf_addr.in6);
   9596       1.1  christos 		if (bits > 96) {
   9597       1.1  christos 			key->hn_addr.adf_addr.i6[3] = addr->i6[3] &
   9598       1.1  christos 					     htonl(0xffffffff << (128 - bits));
   9599       1.1  christos 			key->hn_addr.adf_addr.i6[2] = addr->i6[2];
   9600       1.1  christos 			key->hn_addr.adf_addr.i6[1] = addr->i6[2];
   9601       1.1  christos 			key->hn_addr.adf_addr.i6[0] = addr->i6[2];
   9602       1.1  christos 		} else if (bits > 64) {
   9603       1.1  christos 			key->hn_addr.adf_addr.i6[3] = 0;
   9604       1.1  christos 			key->hn_addr.adf_addr.i6[2] = addr->i6[2] &
   9605       1.1  christos 					     htonl(0xffffffff << (96 - bits));
   9606       1.1  christos 			key->hn_addr.adf_addr.i6[1] = addr->i6[1];
   9607       1.1  christos 			key->hn_addr.adf_addr.i6[0] = addr->i6[0];
   9608       1.1  christos 		} else if (bits > 32) {
   9609       1.1  christos 			key->hn_addr.adf_addr.i6[3] = 0;
   9610       1.1  christos 			key->hn_addr.adf_addr.i6[2] = 0;
   9611       1.1  christos 			key->hn_addr.adf_addr.i6[1] = addr->i6[1] &
   9612       1.1  christos 					     htonl(0xffffffff << (64 - bits));
   9613       1.1  christos 			key->hn_addr.adf_addr.i6[0] = addr->i6[0];
   9614       1.1  christos 		} else {
   9615       1.1  christos 			key->hn_addr.adf_addr.i6[3] = 0;
   9616       1.1  christos 			key->hn_addr.adf_addr.i6[2] = 0;
   9617       1.1  christos 			key->hn_addr.adf_addr.i6[1] = 0;
   9618       1.1  christos 			key->hn_addr.adf_addr.i6[0] = addr->i6[0] &
   9619       1.1  christos 					     htonl(0xffffffff << (32 - bits));
   9620       1.1  christos 		}
   9621       1.2  christos #endif
   9622       1.1  christos 	}
   9623       1.1  christos }
   9624       1.1  christos 
   9625       1.1  christos 
   9626       1.1  christos /* ------------------------------------------------------------------------ */
   9627       1.1  christos /* Function:    ipf_ht_node_add                                             */
   9628       1.1  christos /* Returns:     int       - 0 == success,  -1 == failure                    */
   9629       1.1  christos /* Parameters:  softc(I)  - pointer to soft context main structure          */
   9630       1.1  christos /*              htp(I)    - pointer to address tracking structure           */
   9631       1.1  christos /*              family(I) - protocol family of address                      */
   9632       1.1  christos /*              addr(I)   - pointer to network address                      */
   9633       1.1  christos /*                                                                          */
   9634       1.1  christos /* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS  */
   9635       1.1  christos /*       ipf_ht_node_del FROM RUNNING CONCURRENTLY ON THE SAME htp.         */
   9636       1.1  christos /*                                                                          */
   9637       1.1  christos /* After preparing the key with the address information to find, look in    */
   9638       1.1  christos /* the red-black tree to see if the address is known. A successful call to  */
   9639       1.1  christos /* this function can mean one of two things: a new node was added to the    */
   9640       1.1  christos /* tree or a matching node exists and we're able to bump up its activity.   */
   9641       1.1  christos /* ------------------------------------------------------------------------ */
   9642       1.1  christos int
   9643       1.2  christos ipf_ht_node_add(ipf_main_softc_t *softc, host_track_t *htp, int family,
   9644       1.2  christos     i6addr_t *addr)
   9645       1.1  christos {
   9646       1.1  christos 	host_node_t *h;
   9647       1.1  christos 	host_node_t k;
   9648       1.1  christos 
   9649       1.1  christos 	ipf_ht_node_make_key(htp, &k, family, addr);
   9650       1.1  christos 
   9651       1.1  christos 	h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k);
   9652       1.1  christos 	if (h == NULL) {
   9653       1.1  christos 		if (htp->ht_cur_nodes >= htp->ht_max_nodes)
   9654       1.1  christos 			return -1;
   9655       1.1  christos 		KMALLOC(h, host_node_t *);
   9656       1.1  christos 		if (h == NULL) {
   9657       1.1  christos 			DT(ipf_rb_no_mem);
   9658       1.1  christos 			LBUMP(ipf_rb_no_mem);
   9659       1.1  christos 			return -1;
   9660       1.1  christos 		}
   9661       1.1  christos 
   9662       1.1  christos 		/*
   9663       1.1  christos 		 * If there was a macro to initialise the RB node then that
   9664       1.1  christos 		 * would get used here, but there isn't...
   9665       1.1  christos 		 */
   9666       1.1  christos 		bzero((char *)h, sizeof(*h));
   9667       1.1  christos 		h->hn_addr = k.hn_addr;
   9668       1.1  christos 		h->hn_addr.adf_family = k.hn_addr.adf_family;
   9669       1.1  christos 		RBI_INSERT(ipf_rb, &htp->ht_root, h);
   9670       1.1  christos 		htp->ht_cur_nodes++;
   9671       1.1  christos 	} else {
   9672       1.1  christos 		if ((htp->ht_max_per_node != 0) &&
   9673       1.1  christos 		    (h->hn_active >= htp->ht_max_per_node)) {
   9674       1.1  christos 			DT(ipf_rb_node_max);
   9675       1.1  christos 			LBUMP(ipf_rb_node_max);
   9676       1.1  christos 			return -1;
   9677       1.1  christos 		}
   9678       1.1  christos 	}
   9679       1.1  christos 
   9680       1.1  christos 	h->hn_active++;
   9681       1.1  christos 
   9682       1.1  christos 	return 0;
   9683       1.1  christos }
   9684       1.1  christos 
   9685       1.1  christos 
   9686       1.1  christos /* ------------------------------------------------------------------------ */
   9687       1.1  christos /* Function:    ipf_ht_node_del                                             */
   9688       1.1  christos /* Returns:     int       - 0 == success,  -1 == failure                    */
   9689       1.1  christos /* parameters:  htp(I)    - pointer to address tracking structure           */
   9690       1.1  christos /*              family(I) - protocol family of address                      */
   9691       1.1  christos /*              addr(I)   - pointer to network address                      */
   9692       1.1  christos /*                                                                          */
   9693       1.1  christos /* NOTE: THIS FUNCTION MUST BE CALLED WITH AN EXCLUSIVE LOCK THAT PREVENTS  */
   9694       1.1  christos /*       ipf_ht_node_add FROM RUNNING CONCURRENTLY ON THE SAME htp.         */
   9695       1.1  christos /*                                                                          */
   9696       1.7  christos /* Try and find the address passed in amongst the leaves on this tree to    */
   9697       1.1  christos /* be friend. If found then drop the active account for that node drops by  */
   9698       1.1  christos /* one. If that count reaches 0, it is time to free it all up.              */
   9699       1.1  christos /* ------------------------------------------------------------------------ */
   9700       1.1  christos int
   9701       1.2  christos ipf_ht_node_del(host_track_t *htp, int family, i6addr_t *addr)
   9702       1.1  christos {
   9703       1.1  christos 	host_node_t *h;
   9704       1.1  christos 	host_node_t k;
   9705       1.1  christos 
   9706       1.1  christos 	ipf_ht_node_make_key(htp, &k, family, addr);
   9707       1.1  christos 
   9708       1.1  christos 	h = RBI_SEARCH(ipf_rb, &htp->ht_root, &k);
   9709       1.1  christos 	if (h == NULL) {
   9710       1.1  christos 		return -1;
   9711       1.1  christos 	} else {
   9712       1.1  christos 		h->hn_active--;
   9713       1.1  christos 		if (h->hn_active == 0) {
   9714       1.1  christos 			(void) RBI_DELETE(ipf_rb, &htp->ht_root, h);
   9715       1.1  christos 			htp->ht_cur_nodes--;
   9716       1.1  christos 			KFREE(h);
   9717       1.1  christos 		}
   9718       1.1  christos 	}
   9719       1.1  christos 
   9720       1.1  christos 	return 0;
   9721       1.1  christos }
   9722       1.1  christos 
   9723       1.1  christos 
   9724       1.1  christos /* ------------------------------------------------------------------------ */
   9725       1.1  christos /* Function:    ipf_rb_ht_init                                              */
   9726       1.1  christos /* Returns:     Nil                                                         */
   9727       1.1  christos /* Parameters:  head(I) - pointer to host tracking structure                */
   9728       1.1  christos /*                                                                          */
   9729       1.1  christos /* Initialise the host tracking structure to be ready for use above.        */
   9730       1.1  christos /* ------------------------------------------------------------------------ */
   9731       1.1  christos void
   9732       1.2  christos ipf_rb_ht_init(host_track_t *head)
   9733       1.1  christos {
   9734       1.8  christos 	memset(head, 0, sizeof(*head));
   9735       1.1  christos 	RBI_INIT(ipf_rb, &head->ht_root);
   9736       1.1  christos }
   9737       1.1  christos 
   9738       1.1  christos 
   9739       1.1  christos /* ------------------------------------------------------------------------ */
   9740       1.1  christos /* Function:    ipf_rb_ht_freenode                                          */
   9741       1.1  christos /* Returns:     Nil                                                         */
   9742       1.1  christos /* Parameters:  head(I) - pointer to host tracking structure                */
   9743       1.1  christos /*              arg(I)  - additional argument from walk caller              */
   9744       1.1  christos /*                                                                          */
   9745       1.1  christos /* Free an actual host_node_t structure.                                    */
   9746       1.1  christos /* ------------------------------------------------------------------------ */
   9747       1.1  christos void
   9748       1.2  christos ipf_rb_ht_freenode(host_node_t *node, void *arg)
   9749       1.1  christos {
   9750       1.1  christos 	KFREE(node);
   9751       1.1  christos }
   9752       1.1  christos 
   9753       1.1  christos 
   9754       1.1  christos /* ------------------------------------------------------------------------ */
   9755       1.1  christos /* Function:    ipf_rb_ht_flush                                             */
   9756       1.1  christos /* Returns:     Nil                                                         */
   9757       1.1  christos /* Parameters:  head(I) - pointer to host tracking structure                */
   9758       1.1  christos /*                                                                          */
   9759       1.1  christos /* Remove all of the nodes in the tree tracking hosts by calling a walker   */
   9760       1.1  christos /* and free'ing each one.                                                   */
   9761       1.1  christos /* ------------------------------------------------------------------------ */
   9762       1.1  christos void
   9763       1.2  christos ipf_rb_ht_flush(host_track_t *head)
   9764       1.1  christos {
   9765       1.7  christos 	/* XXX - May use node members after freeing the node. */
   9766       1.1  christos 	RBI_WALK(ipf_rb, &head->ht_root, ipf_rb_ht_freenode, NULL);
   9767       1.1  christos }
   9768       1.1  christos 
   9769       1.1  christos 
   9770       1.1  christos /* ------------------------------------------------------------------------ */
   9771       1.1  christos /* Function:    ipf_slowtimer                                               */
   9772       1.1  christos /* Returns:     Nil                                                         */
   9773       1.1  christos /* Parameters:  ptr(I) - pointer to main ipf soft context structure         */
   9774       1.1  christos /*                                                                          */
   9775       1.1  christos /* Slowly expire held state for fragments.  Timeouts are set * in           */
   9776       1.1  christos /* expectation of this being called twice per second.                       */
   9777       1.1  christos /* ------------------------------------------------------------------------ */
   9778       1.1  christos void
   9779       1.2  christos ipf_slowtimer(ipf_main_softc_t *softc)
   9780       1.1  christos {
   9781       1.1  christos 
   9782       1.1  christos 	ipf_token_expire(softc);
   9783       1.1  christos 	ipf_frag_expire(softc);
   9784       1.1  christos 	ipf_state_expire(softc);
   9785       1.1  christos 	ipf_nat_expire(softc);
   9786       1.1  christos 	ipf_auth_expire(softc);
   9787       1.1  christos 	ipf_lookup_expire(softc);
   9788       1.1  christos 	ipf_rule_expire(softc);
   9789       1.1  christos 	ipf_sync_expire(softc);
   9790       1.1  christos 	softc->ipf_ticks++;
   9791       1.1  christos #   if defined(__OpenBSD__)
   9792       1.1  christos 	timeout_add(&ipf_slowtimer_ch, hz/2);
   9793       1.1  christos #   endif
   9794       1.1  christos }
   9795       1.3   darrenr 
   9796       1.3   darrenr 
   9797       1.3   darrenr /* ------------------------------------------------------------------------ */
   9798       1.3   darrenr /* Function:    ipf_inet_mask_add                                           */
   9799       1.3   darrenr /* Returns:     Nil                                                         */
   9800       1.3   darrenr /* Parameters:  bits(I) - pointer to nat context information                */
   9801       1.3   darrenr /*              mtab(I) - pointer to mask hash table structure              */
   9802       1.3   darrenr /*                                                                          */
   9803       1.3   darrenr /* When called, bits represents the mask of a new NAT rule that has just    */
   9804       1.3   darrenr /* been added. This function inserts a bitmask into the array of masks to   */
   9805       1.3   darrenr /* search when searching for a matching NAT rule for a packet.              */
   9806       1.3   darrenr /* Prevention of duplicate masks is achieved by checking the use count for  */
   9807       1.3   darrenr /* a given netmask.                                                         */
   9808       1.3   darrenr /* ------------------------------------------------------------------------ */
   9809       1.3   darrenr void
   9810       1.4   darrenr ipf_inet_mask_add(int bits, ipf_v4_masktab_t *mtab)
   9811       1.3   darrenr {
   9812       1.3   darrenr 	u_32_t mask;
   9813       1.3   darrenr 	int i, j;
   9814       1.3   darrenr 
   9815       1.3   darrenr 	mtab->imt4_masks[bits]++;
   9816       1.3   darrenr 	if (mtab->imt4_masks[bits] > 1)
   9817       1.3   darrenr 		return;
   9818       1.3   darrenr 
   9819       1.3   darrenr 	if (bits == 0)
   9820       1.3   darrenr 		mask = 0;
   9821       1.3   darrenr 	else
   9822       1.3   darrenr 		mask = 0xffffffff << (32 - bits);
   9823       1.3   darrenr 
   9824       1.3   darrenr 	for (i = 0; i < 33; i++) {
   9825       1.3   darrenr 		if (ntohl(mtab->imt4_active[i]) < mask) {
   9826       1.3   darrenr 			for (j = 32; j > i; j--)
   9827       1.3   darrenr 				mtab->imt4_active[j] = mtab->imt4_active[j - 1];
   9828       1.3   darrenr 			mtab->imt4_active[i] = htonl(mask);
   9829       1.3   darrenr 			break;
   9830       1.3   darrenr 		}
   9831       1.3   darrenr 	}
   9832       1.3   darrenr 	mtab->imt4_max++;
   9833       1.3   darrenr }
   9834       1.3   darrenr 
   9835       1.3   darrenr 
   9836       1.3   darrenr /* ------------------------------------------------------------------------ */
   9837       1.3   darrenr /* Function:    ipf_inet_mask_del                                           */
   9838       1.3   darrenr /* Returns:     Nil                                                         */
   9839       1.3   darrenr /* Parameters:  bits(I) - number of bits set in the netmask                 */
   9840       1.3   darrenr /*              mtab(I) - pointer to mask hash table structure              */
   9841       1.3   darrenr /*                                                                          */
   9842       1.3   darrenr /* Remove the 32bit bitmask represented by "bits" from the collection of    */
   9843       1.3   darrenr /* netmasks stored inside of mtab.                                          */
   9844       1.3   darrenr /* ------------------------------------------------------------------------ */
   9845       1.3   darrenr void
   9846       1.5   darrenr ipf_inet_mask_del(int bits, ipf_v4_masktab_t *mtab)
   9847       1.3   darrenr {
   9848       1.3   darrenr 	u_32_t mask;
   9849       1.3   darrenr 	int i, j;
   9850       1.3   darrenr 
   9851       1.3   darrenr 	mtab->imt4_masks[bits]--;
   9852       1.3   darrenr 	if (mtab->imt4_masks[bits] > 0)
   9853       1.3   darrenr 		return;
   9854       1.3   darrenr 
   9855       1.3   darrenr 	mask = htonl(0xffffffff << (32 - bits));
   9856       1.3   darrenr 	for (i = 0; i < 33; i++) {
   9857       1.3   darrenr 		if (mtab->imt4_active[i] == mask) {
   9858       1.3   darrenr 			for (j = i + 1; j < 33; j++)
   9859       1.3   darrenr 				mtab->imt4_active[j - 1] = mtab->imt4_active[j];
   9860       1.3   darrenr 			break;
   9861       1.3   darrenr 		}
   9862       1.3   darrenr 	}
   9863       1.3   darrenr 	mtab->imt4_max--;
   9864       1.3   darrenr 	ASSERT(mtab->imt4_max >= 0);
   9865       1.3   darrenr }
   9866       1.3   darrenr 
   9867       1.3   darrenr 
   9868       1.3   darrenr #ifdef USE_INET6
   9869       1.3   darrenr /* ------------------------------------------------------------------------ */
   9870       1.3   darrenr /* Function:    ipf_inet6_mask_add                                          */
   9871       1.3   darrenr /* Returns:     Nil                                                         */
   9872       1.3   darrenr /* Parameters:  bits(I) - number of bits set in mask                        */
   9873       1.3   darrenr /*              mask(I) - pointer to mask to add                            */
   9874       1.3   darrenr /*              mtab(I) - pointer to mask hash table structure              */
   9875       1.3   darrenr /*                                                                          */
   9876       1.3   darrenr /* When called, bitcount represents the mask of a IPv6 NAT map rule that    */
   9877       1.3   darrenr /* has just been added. This function inserts a bitmask into the array of   */
   9878       1.3   darrenr /* masks to search when searching for a matching NAT rule for a packet.     */
   9879       1.3   darrenr /* Prevention of duplicate masks is achieved by checking the use count for  */
   9880       1.3   darrenr /* a given netmask.                                                         */
   9881       1.3   darrenr /* ------------------------------------------------------------------------ */
   9882       1.3   darrenr void
   9883       1.4   darrenr ipf_inet6_mask_add(int bits, i6addr_t *mask, ipf_v6_masktab_t *mtab)
   9884       1.3   darrenr {
   9885       1.3   darrenr 	i6addr_t zero;
   9886       1.3   darrenr 	int i, j;
   9887       1.3   darrenr 
   9888       1.3   darrenr 	mtab->imt6_masks[bits]++;
   9889       1.3   darrenr 	if (mtab->imt6_masks[bits] > 1)
   9890       1.3   darrenr 		return;
   9891       1.3   darrenr 
   9892       1.3   darrenr 	if (bits == 0) {
   9893       1.3   darrenr 		mask = &zero;
   9894       1.3   darrenr 		zero.i6[0] = 0;
   9895       1.3   darrenr 		zero.i6[1] = 0;
   9896       1.3   darrenr 		zero.i6[2] = 0;
   9897       1.3   darrenr 		zero.i6[3] = 0;
   9898       1.3   darrenr 	}
   9899       1.3   darrenr 
   9900       1.3   darrenr 	for (i = 0; i < 129; i++) {
   9901       1.3   darrenr 		if (IP6_LT(&mtab->imt6_active[i], mask)) {
   9902       1.3   darrenr 			for (j = 128; j > i; j--)
   9903       1.3   darrenr 				mtab->imt6_active[j] = mtab->imt6_active[j - 1];
   9904       1.3   darrenr 			mtab->imt6_active[i] = *mask;
   9905       1.3   darrenr 			break;
   9906       1.3   darrenr 		}
   9907       1.3   darrenr 	}
   9908       1.3   darrenr 	mtab->imt6_max++;
   9909       1.3   darrenr }
   9910       1.3   darrenr 
   9911       1.3   darrenr 
   9912       1.3   darrenr /* ------------------------------------------------------------------------ */
   9913       1.3   darrenr /* Function:    ipf_inet6_mask_del                                          */
   9914       1.3   darrenr /* Returns:     Nil                                                         */
   9915       1.3   darrenr /* Parameters:  bits(I) - number of bits set in mask                        */
   9916       1.3   darrenr /*              mask(I) - pointer to mask to remove                         */
   9917       1.3   darrenr /*              mtab(I) - pointer to mask hash table structure              */
   9918       1.3   darrenr /*                                                                          */
   9919       1.3   darrenr /* Remove the 128bit bitmask represented by "bits" from the collection of   */
   9920       1.3   darrenr /* netmasks stored inside of mtab.                                          */
   9921       1.3   darrenr /* ------------------------------------------------------------------------ */
   9922       1.3   darrenr void
   9923       1.4   darrenr ipf_inet6_mask_del(int bits, i6addr_t *mask, ipf_v6_masktab_t *mtab)
   9924       1.3   darrenr {
   9925       1.3   darrenr 	i6addr_t zero;
   9926       1.3   darrenr 	int i, j;
   9927       1.3   darrenr 
   9928       1.3   darrenr 	mtab->imt6_masks[bits]--;
   9929       1.3   darrenr 	if (mtab->imt6_masks[bits] > 0)
   9930       1.3   darrenr 		return;
   9931       1.3   darrenr 
   9932       1.3   darrenr 	if (bits == 0)
   9933       1.3   darrenr 		mask = &zero;
   9934       1.3   darrenr 	zero.i6[0] = 0;
   9935       1.3   darrenr 	zero.i6[1] = 0;
   9936       1.3   darrenr 	zero.i6[2] = 0;
   9937       1.3   darrenr 	zero.i6[3] = 0;
   9938       1.3   darrenr 
   9939       1.3   darrenr 	for (i = 0; i < 129; i++) {
   9940       1.3   darrenr 		if (IP6_EQ(&mtab->imt6_active[i], mask)) {
   9941       1.3   darrenr 			for (j = i + 1; j < 129; j++) {
   9942       1.3   darrenr 				mtab->imt6_active[j - 1] = mtab->imt6_active[j];
   9943       1.3   darrenr 				if (IP6_EQ(&mtab->imt6_active[j - 1], &zero))
   9944       1.3   darrenr 					break;
   9945       1.3   darrenr 			}
   9946       1.3   darrenr 			break;
   9947       1.3   darrenr 		}
   9948       1.3   darrenr 	}
   9949       1.3   darrenr 	mtab->imt6_max--;
   9950       1.3   darrenr 	ASSERT(mtab->imt6_max >= 0);
   9951       1.3   darrenr }
   9952       1.3   darrenr #endif
   9953