xref: /onnv-gate/usr/src/uts/common/inet/ipf/ip_frag.c (revision 6518:b2f372728678)
12393Syz155240 /*
22393Syz155240  * Copyright (C) 1993-2003 by Darren Reed.
32393Syz155240  *
42393Syz155240  * See the IPFILTER.LICENCE file for details on licencing.
52393Syz155240  *
66254San207044  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
72393Syz155240  * Use is subject to license terms.
82393Syz155240  */
92393Syz155240 
102393Syz155240 #pragma ident	"%Z%%M%	%I%	%E% SMI"
112393Syz155240 
122393Syz155240 #if defined(KERNEL) || defined(_KERNEL)
132393Syz155240 # undef KERNEL
142393Syz155240 # undef _KERNEL
152393Syz155240 # define        KERNEL	1
162393Syz155240 # define        _KERNEL	1
172393Syz155240 #endif
182393Syz155240 #include <sys/errno.h>
192393Syz155240 #include <sys/types.h>
202393Syz155240 #include <sys/param.h>
212393Syz155240 #include <sys/time.h>
222393Syz155240 #include <sys/file.h>
232393Syz155240 #ifdef __hpux
242393Syz155240 # include <sys/timeout.h>
252393Syz155240 #endif
262393Syz155240 #if !defined(_KERNEL)
272393Syz155240 # include <stdio.h>
282393Syz155240 # include <string.h>
292393Syz155240 # include <stdlib.h>
302393Syz155240 # define _KERNEL
312393Syz155240 # ifdef __OpenBSD__
322393Syz155240 struct file;
332393Syz155240 # endif
342393Syz155240 # include <sys/uio.h>
352393Syz155240 # undef _KERNEL
362393Syz155240 #endif
372393Syz155240 #if defined(_KERNEL) && (__FreeBSD_version >= 220000)
382393Syz155240 # include <sys/filio.h>
392393Syz155240 # include <sys/fcntl.h>
402393Syz155240 #else
412393Syz155240 # include <sys/ioctl.h>
422393Syz155240 #endif
432393Syz155240 #if !defined(linux)
442393Syz155240 # include <sys/protosw.h>
452393Syz155240 #endif
462393Syz155240 #include <sys/socket.h>
472393Syz155240 #if defined(_KERNEL)
482393Syz155240 # include <sys/systm.h>
492393Syz155240 # if !defined(__SVR4) && !defined(__svr4__)
502393Syz155240 #  include <sys/mbuf.h>
512393Syz155240 # endif
522393Syz155240 #endif
532393Syz155240 #if !defined(__SVR4) && !defined(__svr4__)
542393Syz155240 # if defined(_KERNEL) && !defined(__sgi) && !defined(AIX)
552393Syz155240 #  include <sys/kernel.h>
562393Syz155240 # endif
572393Syz155240 #else
582393Syz155240 # include <sys/byteorder.h>
592393Syz155240 # ifdef _KERNEL
602393Syz155240 #  include <sys/dditypes.h>
612393Syz155240 # endif
622393Syz155240 # include <sys/stream.h>
632393Syz155240 # include <sys/kmem.h>
642393Syz155240 #endif
652393Syz155240 #include <net/if.h>
662393Syz155240 #ifdef sun
672393Syz155240 # include <net/af.h>
682393Syz155240 #endif
692393Syz155240 #include <net/route.h>
702393Syz155240 #include <netinet/in.h>
712393Syz155240 #include <netinet/in_systm.h>
722393Syz155240 #include <netinet/ip.h>
732393Syz155240 #if !defined(linux)
742393Syz155240 # include <netinet/ip_var.h>
752393Syz155240 #endif
762393Syz155240 #include <netinet/tcp.h>
772393Syz155240 #include <netinet/udp.h>
782393Syz155240 #include <netinet/ip_icmp.h>
792393Syz155240 #include "netinet/ip_compat.h"
802393Syz155240 #include <netinet/tcpip.h>
812393Syz155240 #include "netinet/ip_fil.h"
822393Syz155240 #include "netinet/ip_nat.h"
832393Syz155240 #include "netinet/ip_frag.h"
842393Syz155240 #include "netinet/ip_state.h"
852393Syz155240 #include "netinet/ip_auth.h"
863448Sdh155122 #include "netinet/ipf_stack.h"
872393Syz155240 #if (__FreeBSD_version >= 300000)
882393Syz155240 # include <sys/malloc.h>
892393Syz155240 # if defined(_KERNEL)
902393Syz155240 #  ifndef IPFILTER_LKM
912393Syz155240 #   include <sys/libkern.h>
922393Syz155240 #   include <sys/systm.h>
932393Syz155240 #  endif
942393Syz155240 extern struct callout_handle fr_slowtimer_ch;
952393Syz155240 # endif
962393Syz155240 #endif
972393Syz155240 #if defined(__NetBSD__) && (__NetBSD_Version__ >= 104230000)
982393Syz155240 # include <sys/callout.h>
992393Syz155240 extern struct callout fr_slowtimer_ch;
1002393Syz155240 #endif
1012393Syz155240 #if defined(__OpenBSD__)
1022393Syz155240 # include <sys/timeout.h>
1032393Syz155240 extern struct timeout fr_slowtimer_ch;
1042393Syz155240 #endif
1052393Syz155240 /* END OF INCLUDES */
1062393Syz155240 
1072393Syz155240 #if !defined(lint)
1082393Syz155240 static const char sccsid[] = "@(#)ip_frag.c	1.11 3/24/96 (C) 1993-2000 Darren Reed";
1092393Syz155240 static const char rcsid[] = "@(#)$Id: ip_frag.c,v 2.77.2.5 2005/08/11 14:33:10 darrenr Exp $";
1102393Syz155240 #endif
1112393Syz155240 
1122393Syz155240 static ipfr_t *ipfr_newfrag __P((fr_info_t *, u_32_t, ipfr_t **));
1132393Syz155240 static ipfr_t *fr_fraglookup __P((fr_info_t *, ipfr_t **));
1143448Sdh155122 static void fr_fragdelete __P((ipfr_t *, ipfr_t ***, ipf_stack_t *));
1152393Syz155240 
1162393Syz155240 /* ------------------------------------------------------------------------ */
1172393Syz155240 /* Function:    fr_fraginit                                                 */
1182393Syz155240 /* Returns:     int - 0 == success, -1 == error                             */
1192393Syz155240 /* Parameters:  Nil                                                         */
1202393Syz155240 /*                                                                          */
1212393Syz155240 /* Initialise the hash tables for the fragment cache lookups.               */
1222393Syz155240 /* ------------------------------------------------------------------------ */
1233448Sdh155122 int fr_fraginit(ifs)
1243448Sdh155122 ipf_stack_t *ifs;
1252393Syz155240 {
1263448Sdh155122 	ifs->ifs_ipfr_tail = &ifs->ifs_ipfr_list;
1273448Sdh155122 	ifs->ifs_ipfr_nattail = &ifs->ifs_ipfr_natlist;
1283448Sdh155122 	ifs->ifs_ipfr_ipidtail = &ifs->ifs_ipfr_ipidlist;
1294251San207044 	/* the IP frag related variables are set in ipftuneable_setdefs() to
1304251San207044 	 * their default values
1314251San207044 	 */
1323448Sdh155122 
1333448Sdh155122 	KMALLOCS(ifs->ifs_ipfr_heads, ipfr_t **,
1343448Sdh155122 	    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1353448Sdh155122 	if (ifs->ifs_ipfr_heads == NULL)
1362393Syz155240 		return -1;
1373448Sdh155122 	bzero((char *)ifs->ifs_ipfr_heads,
1383448Sdh155122 	    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1392393Syz155240 
1403448Sdh155122 	KMALLOCS(ifs->ifs_ipfr_nattab, ipfr_t **,
1413448Sdh155122 	    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1423448Sdh155122 	if (ifs->ifs_ipfr_nattab == NULL)
1432393Syz155240 		return -1;
1443448Sdh155122 	bzero((char *)ifs->ifs_ipfr_nattab,
1453448Sdh155122 	    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1462393Syz155240 
1473448Sdh155122 	KMALLOCS(ifs->ifs_ipfr_ipidtab, ipfr_t **,
1483448Sdh155122 	    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1493448Sdh155122 	if (ifs->ifs_ipfr_ipidtab == NULL)
1502393Syz155240 		return -1;
1513448Sdh155122 	bzero((char *)ifs->ifs_ipfr_ipidtab,
1523448Sdh155122 	    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1532393Syz155240 
1543448Sdh155122 	RWLOCK_INIT(&ifs->ifs_ipf_frag, "ipf fragment rwlock");
1552393Syz155240 
1562393Syz155240 	/* Initialise frblock with "block in all" */
1573448Sdh155122 	bzero((char *)&ifs->ifs_frblock, sizeof(ifs->ifs_frblock));
1583448Sdh155122 	ifs->ifs_frblock.fr_flags = FR_BLOCK|FR_INQUE;	/* block in */
1593448Sdh155122 	ifs->ifs_frblock.fr_ref = 1;
1602393Syz155240 
1613448Sdh155122 	ifs->ifs_fr_frag_init = 1;
1622393Syz155240 
1632393Syz155240 	return 0;
1642393Syz155240 }
1652393Syz155240 
1662393Syz155240 
1672393Syz155240 /* ------------------------------------------------------------------------ */
1682393Syz155240 /* Function:    fr_fragunload                                               */
1692393Syz155240 /* Returns:     Nil                                                         */
1702393Syz155240 /* Parameters:  Nil                                                         */
1712393Syz155240 /*                                                                          */
1722393Syz155240 /* Free all memory allocated whilst running and from initialisation.        */
1732393Syz155240 /* ------------------------------------------------------------------------ */
1743448Sdh155122 void fr_fragunload(ifs)
1753448Sdh155122 ipf_stack_t *ifs;
1762393Syz155240 {
1773448Sdh155122 	if (ifs->ifs_fr_frag_init == 1) {
1783448Sdh155122 		fr_fragclear(ifs);
1792393Syz155240 
1803448Sdh155122 		RW_DESTROY(&ifs->ifs_ipf_frag);
1813448Sdh155122 		ifs->ifs_fr_frag_init = 0;
1822393Syz155240 	}
1832393Syz155240 
1843448Sdh155122 	if (ifs->ifs_ipfr_heads != NULL) {
1853448Sdh155122 		KFREES(ifs->ifs_ipfr_heads,
1863448Sdh155122 		    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1873448Sdh155122 	}
1883448Sdh155122 	ifs->ifs_ipfr_heads = NULL;
1892393Syz155240 
1903448Sdh155122 	if (ifs->ifs_ipfr_nattab != NULL) {
1913448Sdh155122 		KFREES(ifs->ifs_ipfr_nattab,
1923448Sdh155122 		    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1933448Sdh155122 	}
1943448Sdh155122 	ifs->ifs_ipfr_nattab = NULL;
1952393Syz155240 
1963448Sdh155122 	if (ifs->ifs_ipfr_ipidtab != NULL) {
1973448Sdh155122 		KFREES(ifs->ifs_ipfr_ipidtab,
1983448Sdh155122 		    ifs->ifs_ipfr_size * sizeof(ipfr_t *));
1993448Sdh155122 	}
2003448Sdh155122 	ifs->ifs_ipfr_ipidtab = NULL;
2012393Syz155240 }
2022393Syz155240 
2032393Syz155240 
2042393Syz155240 /* ------------------------------------------------------------------------ */
2052393Syz155240 /* Function:    fr_fragstats                                                */
2062393Syz155240 /* Returns:     ipfrstat_t* - pointer to struct with current frag stats     */
2072393Syz155240 /* Parameters:  Nil                                                         */
2082393Syz155240 /*                                                                          */
2092393Syz155240 /* Updates ipfr_stats with current information and returns a pointer to it  */
2102393Syz155240 /* ------------------------------------------------------------------------ */
2113448Sdh155122 ipfrstat_t *fr_fragstats(ifs)
2123448Sdh155122 ipf_stack_t *ifs;
2132393Syz155240 {
2143448Sdh155122 	ifs->ifs_ipfr_stats.ifs_table = ifs->ifs_ipfr_heads;
2153448Sdh155122 	ifs->ifs_ipfr_stats.ifs_nattab = ifs->ifs_ipfr_nattab;
2163448Sdh155122 	ifs->ifs_ipfr_stats.ifs_inuse = ifs->ifs_ipfr_inuse;
2173448Sdh155122 	return &ifs->ifs_ipfr_stats;
2182393Syz155240 }
2192393Syz155240 
2202393Syz155240 
2212393Syz155240 /* ------------------------------------------------------------------------ */
2222393Syz155240 /* Function:    ipfr_newfrag                                                */
2232393Syz155240 /* Returns:     ipfr_t * - pointer to fragment cache state info or NULL     */
2242393Syz155240 /* Parameters:  fin(I)   - pointer to packet information                    */
2252393Syz155240 /*              table(I) - pointer to frag table to add to                  */
2262393Syz155240 /*                                                                          */
2272393Syz155240 /* Add a new entry to the fragment cache, registering it as having come     */
2282393Syz155240 /* through this box, with the result of the filter operation.               */
2292393Syz155240 /* ------------------------------------------------------------------------ */
2302393Syz155240 static ipfr_t *ipfr_newfrag(fin, pass, table)
2312393Syz155240 fr_info_t *fin;
2322393Syz155240 u_32_t pass;
2332393Syz155240 ipfr_t *table[];
2342393Syz155240 {
2352393Syz155240 	ipfr_t *fra, frag;
2362393Syz155240 	u_int idx, off;
2373448Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
2382393Syz155240 
2396254San207044 	if (ifs->ifs_ipfr_inuse >= ifs->ifs_ipfr_size)
2402393Syz155240 		return NULL;
2412393Syz155240 
2422393Syz155240 	if ((fin->fin_flx & (FI_FRAG|FI_BAD)) != FI_FRAG)
2432393Syz155240 		return NULL;
2442393Syz155240 
2452393Syz155240 	if (pass & FR_FRSTRICT)
2462393Syz155240 		if (fin->fin_off != 0)
2472393Syz155240 			return NULL;
2482393Syz155240 
2492393Syz155240 	frag.ipfr_p = fin->fin_p;
2502393Syz155240 	idx = fin->fin_p;
2512393Syz155240 	frag.ipfr_id = fin->fin_id;
2522393Syz155240 	idx += fin->fin_id;
2532393Syz155240 	frag.ipfr_source = fin->fin_fi.fi_src;
2542393Syz155240 	idx += frag.ipfr_src.s_addr;
2552393Syz155240 	frag.ipfr_dest = fin->fin_fi.fi_dst;
2562393Syz155240 	idx += frag.ipfr_dst.s_addr;
2572393Syz155240 	frag.ipfr_ifp = fin->fin_ifp;
2582393Syz155240 	idx *= 127;
2596254San207044 	idx %= ifs->ifs_ipfr_size;
2602393Syz155240 
2612393Syz155240 	frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
2622393Syz155240 	frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
2632393Syz155240 	frag.ipfr_auth = fin->fin_fi.fi_auth;
2642393Syz155240 
2652393Syz155240 	/*
2662393Syz155240 	 * first, make sure it isn't already there...
2672393Syz155240 	 */
2682393Syz155240 	for (fra = table[idx]; (fra != NULL); fra = fra->ipfr_hnext)
2692393Syz155240 		if (!bcmp((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp,
2702393Syz155240 			  IPFR_CMPSZ)) {
2713448Sdh155122 			ifs->ifs_ipfr_stats.ifs_exists++;
2722393Syz155240 			return NULL;
2732393Syz155240 		}
2742393Syz155240 
2752393Syz155240 	/*
2762393Syz155240 	 * allocate some memory, if possible, if not, just record that we
2772393Syz155240 	 * failed to do so.
2782393Syz155240 	 */
2792393Syz155240 	KMALLOC(fra, ipfr_t *);
2802393Syz155240 	if (fra == NULL) {
2813448Sdh155122 		ifs->ifs_ipfr_stats.ifs_nomem++;
2822393Syz155240 		return NULL;
2832393Syz155240 	}
2842393Syz155240 
2852393Syz155240 	fra->ipfr_rule = fin->fin_fr;
2862393Syz155240 	if (fra->ipfr_rule != NULL) {
2872393Syz155240 
2882393Syz155240 		frentry_t *fr;
2892393Syz155240 
2902393Syz155240 		fr = fin->fin_fr;
2912393Syz155240 		MUTEX_ENTER(&fr->fr_lock);
2922393Syz155240 		fr->fr_ref++;
2932393Syz155240 		MUTEX_EXIT(&fr->fr_lock);
2942393Syz155240 	}
2952393Syz155240 
2962393Syz155240 	/*
2972393Syz155240 	 * Insert the fragment into the fragment table, copy the struct used
2982393Syz155240 	 * in the search using bcopy rather than reassign each field.
2992393Syz155240 	 * Set the ttl to the default.
3002393Syz155240 	 */
3012393Syz155240 	if ((fra->ipfr_hnext = table[idx]) != NULL)
3022393Syz155240 		table[idx]->ipfr_hprev = &fra->ipfr_hnext;
3032393Syz155240 	fra->ipfr_hprev = table + idx;
3042393Syz155240 	fra->ipfr_data = NULL;
3052393Syz155240 	table[idx] = fra;
3062393Syz155240 	bcopy((char *)&frag.ipfr_ifp, (char *)&fra->ipfr_ifp, IPFR_CMPSZ);
3073448Sdh155122 	fra->ipfr_ttl = ifs->ifs_fr_ticks + ifs->ifs_fr_ipfrttl;
3082393Syz155240 
3092393Syz155240 	/*
3102393Syz155240 	 * Compute the offset of the expected start of the next packet.
3112393Syz155240 	 */
3122393Syz155240 	off = fin->fin_off;
3132393Syz155240 	if (off == 0) {
3142393Syz155240 		fra->ipfr_seen0 = 1;
3152393Syz155240 		fra->ipfr_firstend = fin->fin_flen;
3162393Syz155240 	} else {
3172393Syz155240 		fra->ipfr_seen0 = 0;
3182393Syz155240 		fra->ipfr_firstend = 0;
3192393Syz155240 	}
3202393Syz155240 	fra->ipfr_off = off + fin->fin_dlen;
3212393Syz155240 	fra->ipfr_pass = pass;
3223448Sdh155122 	fra->ipfr_ref = 1;
3233448Sdh155122 	ifs->ifs_ipfr_stats.ifs_new++;
3243448Sdh155122 	ifs->ifs_ipfr_inuse++;
3252393Syz155240 	return fra;
3262393Syz155240 }
3272393Syz155240 
3282393Syz155240 
3292393Syz155240 /* ------------------------------------------------------------------------ */
3302393Syz155240 /* Function:    fr_newfrag                                                  */
3312393Syz155240 /* Returns:     int - 0 == success, -1 == error                             */
3322393Syz155240 /* Parameters:  fin(I)  - pointer to packet information                     */
3332393Syz155240 /*                                                                          */
3342393Syz155240 /* Add a new entry to the fragment cache table based on the current packet  */
3352393Syz155240 /* ------------------------------------------------------------------------ */
3362393Syz155240 int fr_newfrag(fin, pass)
3372393Syz155240 u_32_t pass;
3382393Syz155240 fr_info_t *fin;
3392393Syz155240 {
3402393Syz155240 	ipfr_t	*fra;
3413448Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
3422393Syz155240 
3433448Sdh155122 	if (ifs->ifs_fr_frag_lock != 0)
3442393Syz155240 		return -1;
3452393Syz155240 
3463448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_frag);
3473448Sdh155122 	fra = ipfr_newfrag(fin, pass, ifs->ifs_ipfr_heads);
3482393Syz155240 	if (fra != NULL) {
3493448Sdh155122 		*ifs->ifs_ipfr_tail = fra;
3503448Sdh155122 		fra->ipfr_prev = ifs->ifs_ipfr_tail;
3513448Sdh155122 		ifs->ifs_ipfr_tail = &fra->ipfr_next;
3523448Sdh155122 		if (ifs->ifs_ipfr_list == NULL)
3533448Sdh155122 			ifs->ifs_ipfr_list = fra;
3542393Syz155240 		fra->ipfr_next = NULL;
3552393Syz155240 	}
3563448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_frag);
3572393Syz155240 	return fra ? 0 : -1;
3582393Syz155240 }
3592393Syz155240 
3602393Syz155240 
3612393Syz155240 /* ------------------------------------------------------------------------ */
3622393Syz155240 /* Function:    fr_nat_newfrag                                              */
3632393Syz155240 /* Returns:     int - 0 == success, -1 == error                             */
3642393Syz155240 /* Parameters:  fin(I)  - pointer to packet information                     */
3652393Syz155240 /*              nat(I)  - pointer to NAT structure                          */
3662393Syz155240 /*                                                                          */
3672393Syz155240 /* Create a new NAT fragment cache entry based on the current packet and    */
3682393Syz155240 /* the NAT structure for this "session".                                    */
3692393Syz155240 /* ------------------------------------------------------------------------ */
3702393Syz155240 int fr_nat_newfrag(fin, pass, nat)
3712393Syz155240 fr_info_t *fin;
3722393Syz155240 u_32_t pass;
3732393Syz155240 nat_t *nat;
3742393Syz155240 {
3752393Syz155240 	ipfr_t	*fra;
3763448Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
3772393Syz155240 
3783448Sdh155122 	if ((fin->fin_v != 4) || (ifs->ifs_fr_frag_lock != 0))
3792393Syz155240 		return 0;
3802393Syz155240 
3813448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_natfrag);
3823448Sdh155122 	fra = ipfr_newfrag(fin, pass, ifs->ifs_ipfr_nattab);
3832393Syz155240 	if (fra != NULL) {
3842393Syz155240 		fra->ipfr_data = nat;
3852393Syz155240 		nat->nat_data = fra;
3863448Sdh155122 		*ifs->ifs_ipfr_nattail = fra;
3873448Sdh155122 		fra->ipfr_prev = ifs->ifs_ipfr_nattail;
3883448Sdh155122 		ifs->ifs_ipfr_nattail = &fra->ipfr_next;
3892393Syz155240 		fra->ipfr_next = NULL;
3902393Syz155240 	}
3913448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_natfrag);
3922393Syz155240 	return fra ? 0 : -1;
3932393Syz155240 }
3942393Syz155240 
3952393Syz155240 
3962393Syz155240 /* ------------------------------------------------------------------------ */
3972393Syz155240 /* Function:    fr_ipid_newfrag                                             */
3982393Syz155240 /* Returns:     int - 0 == success, -1 == error                             */
3992393Syz155240 /* Parameters:  fin(I)  - pointer to packet information                     */
4002393Syz155240 /*              ipid(I) - new IP ID for this fragmented packet              */
4012393Syz155240 /*                                                                          */
4022393Syz155240 /* Create a new fragment cache entry for this packet and store, as a data   */
4032393Syz155240 /* pointer, the new IP ID value.                                            */
4042393Syz155240 /* ------------------------------------------------------------------------ */
4052393Syz155240 int fr_ipid_newfrag(fin, ipid)
4062393Syz155240 fr_info_t *fin;
4072393Syz155240 u_32_t ipid;
4082393Syz155240 {
4092393Syz155240 	ipfr_t	*fra;
4103448Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
4112393Syz155240 
4123448Sdh155122 	if (ifs->ifs_fr_frag_lock)
4132393Syz155240 		return 0;
4142393Syz155240 
4153448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_ipidfrag);
4163448Sdh155122 	fra = ipfr_newfrag(fin, 0, ifs->ifs_ipfr_ipidtab);
4172393Syz155240 	if (fra != NULL) {
4182393Syz155240 		fra->ipfr_data = (void *)(uintptr_t)ipid;
4193448Sdh155122 		*ifs->ifs_ipfr_ipidtail = fra;
4203448Sdh155122 		fra->ipfr_prev = ifs->ifs_ipfr_ipidtail;
4213448Sdh155122 		ifs->ifs_ipfr_ipidtail = &fra->ipfr_next;
4222393Syz155240 		fra->ipfr_next = NULL;
4232393Syz155240 	}
4243448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_ipidfrag);
4252393Syz155240 	return fra ? 0 : -1;
4262393Syz155240 }
4272393Syz155240 
4282393Syz155240 
4292393Syz155240 /* ------------------------------------------------------------------------ */
4302393Syz155240 /* Function:    fr_fraglookup                                               */
4312393Syz155240 /* Returns:     ipfr_t * - pointer to ipfr_t structure if there's a         */
4322393Syz155240 /*                         matching entry in the frag table, else NULL      */
4332393Syz155240 /* Parameters:  fin(I)   - pointer to packet information                    */
4342393Syz155240 /*              table(I) - pointer to fragment cache table to search        */
4352393Syz155240 /*                                                                          */
4362393Syz155240 /* Check the fragment cache to see if there is already a record of this     */
4372393Syz155240 /* packet with its filter result known.                                     */
4382393Syz155240 /* ------------------------------------------------------------------------ */
4392393Syz155240 static ipfr_t *fr_fraglookup(fin, table)
4402393Syz155240 fr_info_t *fin;
4412393Syz155240 ipfr_t *table[];
4422393Syz155240 {
4432393Syz155240 	ipfr_t *f, frag;
4442393Syz155240 	u_int idx;
4453448Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
4462393Syz155240 
4472393Syz155240 	if ((fin->fin_flx & (FI_FRAG|FI_BAD)) != FI_FRAG)
4482393Syz155240 		return NULL;
4492393Syz155240 
4502393Syz155240 	/*
4512393Syz155240 	 * For fragments, we record protocol, packet id, TOS and both IP#'s
4522393Syz155240 	 * (these should all be the same for all fragments of a packet).
4532393Syz155240 	 *
4542393Syz155240 	 * build up a hash value to index the table with.
4552393Syz155240 	 */
4562393Syz155240 	frag.ipfr_p = fin->fin_p;
4572393Syz155240 	idx = fin->fin_p;
4582393Syz155240 	frag.ipfr_id = fin->fin_id;
4592393Syz155240 	idx += fin->fin_id;
4602393Syz155240 	frag.ipfr_source = fin->fin_fi.fi_src;
4612393Syz155240 	idx += frag.ipfr_src.s_addr;
4622393Syz155240 	frag.ipfr_dest = fin->fin_fi.fi_dst;
4632393Syz155240 	idx += frag.ipfr_dst.s_addr;
4642393Syz155240 	frag.ipfr_ifp = fin->fin_ifp;
4652393Syz155240 	idx *= 127;
4662393Syz155240 	idx %= IPFT_SIZE;
4672393Syz155240 
4682393Syz155240 	frag.ipfr_optmsk = fin->fin_fi.fi_optmsk & IPF_OPTCOPY;
4692393Syz155240 	frag.ipfr_secmsk = fin->fin_fi.fi_secmsk;
4702393Syz155240 	frag.ipfr_auth = fin->fin_fi.fi_auth;
4712393Syz155240 
4722393Syz155240 	/*
4732393Syz155240 	 * check the table, careful to only compare the right amount of data
4742393Syz155240 	 */
4752393Syz155240 	for (f = table[idx]; f; f = f->ipfr_hnext)
4762393Syz155240 		if (!bcmp((char *)&frag.ipfr_ifp, (char *)&f->ipfr_ifp,
4772393Syz155240 			  IPFR_CMPSZ)) {
4782393Syz155240 			u_short	off;
4792393Syz155240 
4802393Syz155240 			/*
4812393Syz155240 			 * We don't want to let short packets match because
4822393Syz155240 			 * they could be compromising the security of other
4832393Syz155240 			 * rules that want to match on layer 4 fields (and
4842393Syz155240 			 * can't because they have been fragmented off.)
4852393Syz155240 			 * Why do this check here?  The counter acts as an
4862393Syz155240 			 * indicator of this kind of attack, whereas if it was
4872393Syz155240 			 * elsewhere, it wouldn't know if other matching
4882393Syz155240 			 * packets had been seen.
4892393Syz155240 			 */
4902393Syz155240 			if (fin->fin_flx & FI_SHORT) {
4913448Sdh155122 				ATOMIC_INCL(ifs->ifs_ipfr_stats.ifs_short);
4922393Syz155240 				continue;
4932393Syz155240 			}
4942393Syz155240 
4952393Syz155240 			/*
4962393Syz155240 			 * XXX - We really need to be guarding against the
4972393Syz155240 			 * retransmission of (src,dst,id,offset-range) here
4982393Syz155240 			 * because a fragmented packet is never resent with
4992393Syz155240 			 * the same IP ID# (or shouldn't).
5002393Syz155240 			 */
5012393Syz155240 			off = fin->fin_off; /* same as in ipfr_newfrag() */
5022393Syz155240 			if (f->ipfr_seen0) {
5032393Syz155240 				if (off == 0) {
5043448Sdh155122 					ATOMIC_INCL(ifs->ifs_ipfr_stats.ifs_retrans0);
5052393Syz155240 					continue;
5062393Syz155240 				}
5072393Syz155240 			} else if (off == 0) {
5082393Syz155240 				f->ipfr_seen0 = 1;
5092393Syz155240 				f->ipfr_firstend = fin->fin_flen;
5102393Syz155240 			}
5112393Syz155240 
5122393Syz155240 			if (f != table[idx]) {
5132393Syz155240 				ipfr_t **fp;
5142393Syz155240 
5152393Syz155240 				/*
5162393Syz155240 				 * Move fragment info. to the top of the list
5172393Syz155240 				 * to speed up searches.  First, delink...
5182393Syz155240 				 */
5192393Syz155240 				fp = f->ipfr_hprev;
5202393Syz155240 				(*fp) = f->ipfr_hnext;
5212393Syz155240 				if (f->ipfr_hnext != NULL)
5222393Syz155240 					f->ipfr_hnext->ipfr_hprev = fp;
5232393Syz155240 				/*
5242393Syz155240 				 * Then put back at the top of the chain.
5252393Syz155240 				 */
5262393Syz155240 				f->ipfr_hnext = table[idx];
5272393Syz155240 				table[idx]->ipfr_hprev = &f->ipfr_hnext;
5282393Syz155240 				f->ipfr_hprev = table + idx;
5292393Syz155240 				table[idx] = f;
5302393Syz155240 			}
5312393Syz155240 
5322393Syz155240 			if (fin->fin_v == 6) {
5332393Syz155240 				if (f->ipfr_seen0 && (off < f->ipfr_firstend))
5342393Syz155240 					fin->fin_flx |= FI_BAD;
5352393Syz155240 			}
5362393Syz155240 			/*
5372393Syz155240 			 * If we've follwed the fragments, and this is the
5382393Syz155240 			 * last (in order), shrink expiration time.
5392393Syz155240 			 */
5402393Syz155240 			if (off == f->ipfr_off) {
5412393Syz155240 				if (!(fin->fin_ip->ip_off & IP_MF))
5423448Sdh155122 					f->ipfr_ttl = ifs->ifs_fr_ticks + 1;
5432393Syz155240 				f->ipfr_off = fin->fin_dlen + off;
5442393Syz155240 			} else if (f->ipfr_pass & FR_FRSTRICT)
5452393Syz155240 				continue;
5463448Sdh155122 			ATOMIC_INCL(ifs->ifs_ipfr_stats.ifs_hits);
5472393Syz155240 			return f;
5482393Syz155240 		}
5492393Syz155240 	return NULL;
5502393Syz155240 }
5512393Syz155240 
5522393Syz155240 
5532393Syz155240 /* ------------------------------------------------------------------------ */
5542393Syz155240 /* Function:    fr_nat_knownfrag                                            */
5552393Syz155240 /* Returns:     nat_t* - pointer to 'parent' NAT structure if frag table    */
5562393Syz155240 /*                       match found, else NULL                             */
5572393Syz155240 /* Parameters:  fin(I)  - pointer to packet information                     */
5582393Syz155240 /*                                                                          */
5592393Syz155240 /* Functional interface for NAT lookups of the NAT fragment cache           */
5602393Syz155240 /* ------------------------------------------------------------------------ */
5612393Syz155240 nat_t *fr_nat_knownfrag(fin)
5622393Syz155240 fr_info_t *fin;
5632393Syz155240 {
5642393Syz155240 	nat_t	*nat;
5652393Syz155240 	ipfr_t	*ipf;
5663448Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
5672393Syz155240 
5683448Sdh155122 	if ((fin->fin_v != 4) || (ifs->ifs_fr_frag_lock) || !ifs->ifs_ipfr_natlist)
5692393Syz155240 		return NULL;
5703448Sdh155122 	READ_ENTER(&ifs->ifs_ipf_natfrag);
5713448Sdh155122 	ipf = fr_fraglookup(fin, ifs->ifs_ipfr_nattab);
5722393Syz155240 	if (ipf != NULL) {
5732393Syz155240 		nat = ipf->ipfr_data;
5742393Syz155240 		/*
5752393Syz155240 		 * This is the last fragment for this packet.
5762393Syz155240 		 */
5773448Sdh155122 		if ((ipf->ipfr_ttl == ifs->ifs_fr_ticks + 1) && (nat != NULL)) {
5782393Syz155240 			nat->nat_data = NULL;
5792393Syz155240 			ipf->ipfr_data = NULL;
5802393Syz155240 		}
5812393Syz155240 	} else
5822393Syz155240 		nat = NULL;
5833448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_natfrag);
5842393Syz155240 	return nat;
5852393Syz155240 }
5862393Syz155240 
5872393Syz155240 
5882393Syz155240 /* ------------------------------------------------------------------------ */
5892393Syz155240 /* Function:    fr_ipid_knownfrag                                           */
5902393Syz155240 /* Returns:     u_32_t - IPv4 ID for this packet if match found, else       */
5912393Syz155240 /*                       return 0xfffffff to indicate no match.             */
5922393Syz155240 /* Parameters:  fin(I) - pointer to packet information                      */
5932393Syz155240 /*                                                                          */
5942393Syz155240 /* Functional interface for IP ID lookups of the IP ID fragment cache       */
5952393Syz155240 /* ------------------------------------------------------------------------ */
5962393Syz155240 u_32_t fr_ipid_knownfrag(fin)
5972393Syz155240 fr_info_t *fin;
5982393Syz155240 {
5992393Syz155240 	ipfr_t	*ipf;
6002393Syz155240 	u_32_t	id;
6013448Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
6022393Syz155240 
6033448Sdh155122 	if ((fin->fin_v != 4) || (ifs->ifs_fr_frag_lock) || !ifs->ifs_ipfr_ipidlist)
6042393Syz155240 		return 0xffffffff;
6052393Syz155240 
6063448Sdh155122 	READ_ENTER(&ifs->ifs_ipf_ipidfrag);
6073448Sdh155122 	ipf = fr_fraglookup(fin, ifs->ifs_ipfr_ipidtab);
6082393Syz155240 	if (ipf != NULL)
6092393Syz155240 		id = (u_32_t)(uintptr_t)ipf->ipfr_data;
6102393Syz155240 	else
6112393Syz155240 		id = 0xffffffff;
6123448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_ipidfrag);
6132393Syz155240 	return id;
6142393Syz155240 }
6152393Syz155240 
6162393Syz155240 
6172393Syz155240 /* ------------------------------------------------------------------------ */
6182393Syz155240 /* Function:    fr_knownfrag                                                */
6192393Syz155240 /* Returns:     frentry_t* - pointer to filter rule if a match is found in  */
6202393Syz155240 /*                           the frag cache table, else NULL.               */
6212393Syz155240 /* Parameters:  fin(I)   - pointer to packet information                    */
6222393Syz155240 /*              passp(O) - pointer to where to store rule flags resturned   */
6232393Syz155240 /*                                                                          */
6242393Syz155240 /* Functional interface for normal lookups of the fragment cache.  If a     */
6252393Syz155240 /* match is found, return the rule pointer and flags from the rule, except  */
6262393Syz155240 /* that if FR_LOGFIRST is set, reset FR_LOG.                                */
6272393Syz155240 /* ------------------------------------------------------------------------ */
6282393Syz155240 frentry_t *fr_knownfrag(fin, passp)
6292393Syz155240 fr_info_t *fin;
6302393Syz155240 u_32_t *passp;
6312393Syz155240 {
6322393Syz155240 	frentry_t *fr = NULL;
6332393Syz155240 	ipfr_t	*fra;
6342393Syz155240 	u_32_t pass, oflx;
6353448Sdh155122 	ipf_stack_t *ifs = fin->fin_ifs;
6362393Syz155240 
6373448Sdh155122 	if ((ifs->ifs_fr_frag_lock) || (ifs->ifs_ipfr_list == NULL))
6382393Syz155240 		return NULL;
6392393Syz155240 
6403448Sdh155122 	READ_ENTER(&ifs->ifs_ipf_frag);
6412393Syz155240 	oflx = fin->fin_flx;
6423448Sdh155122 	fra = fr_fraglookup(fin, ifs->ifs_ipfr_heads);
6432393Syz155240 	if (fra != NULL) {
6442393Syz155240 		fr = fra->ipfr_rule;
6452393Syz155240 		fin->fin_fr = fr;
6462393Syz155240 		if (fr != NULL) {
6472393Syz155240 			pass = fr->fr_flags;
6482393Syz155240 			if ((pass & FR_LOGFIRST) != 0)
6492393Syz155240 				pass &= ~(FR_LOGFIRST|FR_LOG);
6502393Syz155240 			*passp = pass;
6512393Syz155240 		}
6522393Syz155240 	}
6532393Syz155240 	if (!(oflx & FI_BAD) && (fin->fin_flx & FI_BAD)) {
6542393Syz155240 		*passp &= ~FR_CMDMASK;
6552393Syz155240 		*passp |= FR_BLOCK;
6563448Sdh155122 		fr = &ifs->ifs_frblock;
6572393Syz155240 	}
6583448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_frag);
6592393Syz155240 	return fr;
6602393Syz155240 }
6612393Syz155240 
6622393Syz155240 
6632393Syz155240 /* ------------------------------------------------------------------------ */
6642393Syz155240 /* Function:    fr_forget                                                   */
6652393Syz155240 /* Returns:     Nil                                                         */
6662393Syz155240 /* Parameters:  ptr(I) - pointer to data structure                          */
6672393Syz155240 /*                                                                          */
6682393Syz155240 /* Search through all of the fragment cache entries and wherever a pointer  */
6692393Syz155240 /* is found to match ptr, reset it to NULL.                                 */
6702393Syz155240 /* ------------------------------------------------------------------------ */
6713448Sdh155122 void fr_forget(ptr, ifs)
6722393Syz155240 void *ptr;
6733448Sdh155122 ipf_stack_t *ifs;
6742393Syz155240 {
6752393Syz155240 	ipfr_t	*fr;
6762393Syz155240 
6773448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_frag);
6783448Sdh155122 	for (fr = ifs->ifs_ipfr_list; fr; fr = fr->ipfr_next)
6792393Syz155240 		if (fr->ipfr_data == ptr)
6802393Syz155240 			fr->ipfr_data = NULL;
6813448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_frag);
6822393Syz155240 }
6832393Syz155240 
6842393Syz155240 
6852393Syz155240 /* ------------------------------------------------------------------------ */
6862393Syz155240 /* Function:    fr_forgetnat                                                */
6872393Syz155240 /* Returns:     Nil                                                         */
6882393Syz155240 /* Parameters:  ptr(I) - pointer to data structure                          */
6892393Syz155240 /*                                                                          */
6902393Syz155240 /* Search through all of the fragment cache entries for NAT and wherever a  */
6912393Syz155240 /* pointer  is found to match ptr, reset it to NULL.                        */
6922393Syz155240 /* ------------------------------------------------------------------------ */
6933448Sdh155122 void fr_forgetnat(ptr, ifs)
6942393Syz155240 void *ptr;
6953448Sdh155122 ipf_stack_t *ifs;
6962393Syz155240 {
6972393Syz155240 	ipfr_t	*fr;
6982393Syz155240 
6993448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_natfrag);
7003448Sdh155122 	for (fr = ifs->ifs_ipfr_natlist; fr; fr = fr->ipfr_next)
7012393Syz155240 		if (fr->ipfr_data == ptr)
7022393Syz155240 			fr->ipfr_data = NULL;
7033448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_natfrag);
7042393Syz155240 }
7052393Syz155240 
7062393Syz155240 
7072393Syz155240 /* ------------------------------------------------------------------------ */
7082393Syz155240 /* Function:    fr_fragdelete                                               */
7092393Syz155240 /* Returns:     Nil                                                         */
7102393Syz155240 /* Parameters:  fra(I)   - pointer to fragment structure to delete          */
7112393Syz155240 /*              tail(IO) - pointer to the pointer to the tail of the frag   */
7122393Syz155240 /*                         list                                             */
7132393Syz155240 /*                                                                          */
7142393Syz155240 /* Remove a fragment cache table entry from the table & list.  Also free    */
7152393Syz155240 /* the filter rule it is associated with it if it is no longer used as a    */
7162393Syz155240 /* result of decreasing the reference count.                                */
7172393Syz155240 /* ------------------------------------------------------------------------ */
7183448Sdh155122 static void fr_fragdelete(fra, tail, ifs)
7192393Syz155240 ipfr_t *fra, ***tail;
7203448Sdh155122 ipf_stack_t *ifs;
7212393Syz155240 {
7222393Syz155240 	frentry_t *fr;
7232393Syz155240 
7242393Syz155240 	fr = fra->ipfr_rule;
7252393Syz155240 	if (fr != NULL)
7263448Sdh155122 	    (void)fr_derefrule(&fr, ifs);
7272393Syz155240 
7282393Syz155240 	if (fra->ipfr_next)
7292393Syz155240 		fra->ipfr_next->ipfr_prev = fra->ipfr_prev;
7302393Syz155240 	*fra->ipfr_prev = fra->ipfr_next;
7312393Syz155240 	if (*tail == &fra->ipfr_next)
7322393Syz155240 		*tail = fra->ipfr_prev;
7332393Syz155240 
7342393Syz155240 	if (fra->ipfr_hnext)
7352393Syz155240 		fra->ipfr_hnext->ipfr_hprev = fra->ipfr_hprev;
7362393Syz155240 	*fra->ipfr_hprev = fra->ipfr_hnext;
7373448Sdh155122 
7383448Sdh155122 	if (fra->ipfr_ref <= 0)
7393448Sdh155122 		KFREE(fra);
7402393Syz155240 }
7412393Syz155240 
7422393Syz155240 
7432393Syz155240 /* ------------------------------------------------------------------------ */
7442393Syz155240 /* Function:    fr_fragclear                                                */
7452393Syz155240 /* Returns:     Nil                                                         */
7462393Syz155240 /* Parameters:  Nil                                                         */
7472393Syz155240 /*                                                                          */
7482393Syz155240 /* Free memory in use by fragment state information kept.  Do the normal    */
7492393Syz155240 /* fragment state stuff first and then the NAT-fragment table.              */
7502393Syz155240 /* ------------------------------------------------------------------------ */
7513448Sdh155122 void fr_fragclear(ifs)
7523448Sdh155122 ipf_stack_t *ifs;
7532393Syz155240 {
7542393Syz155240 	ipfr_t	*fra;
7552393Syz155240 	nat_t	*nat;
7562393Syz155240 
7573448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_frag);
7583448Sdh155122 	while ((fra = ifs->ifs_ipfr_list) != NULL) {
7593448Sdh155122 		fra->ipfr_ref--;
7603448Sdh155122 		fr_fragdelete(fra, &ifs->ifs_ipfr_tail, ifs);
7613448Sdh155122 	}
7623448Sdh155122 	ifs->ifs_ipfr_tail = &ifs->ifs_ipfr_list;
7633448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_frag);
7642393Syz155240 
7653448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_nat);
7663448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_natfrag);
7673448Sdh155122 	while ((fra = ifs->ifs_ipfr_natlist) != NULL) {
7682393Syz155240 		nat = fra->ipfr_data;
7692393Syz155240 		if (nat != NULL) {
7702393Syz155240 			if (nat->nat_data == fra)
7712393Syz155240 				nat->nat_data = NULL;
7722393Syz155240 		}
7733448Sdh155122 		fra->ipfr_ref--;
7743448Sdh155122 		fr_fragdelete(fra, &ifs->ifs_ipfr_nattail, ifs);
7752393Syz155240 	}
7763448Sdh155122 	ifs->ifs_ipfr_nattail = &ifs->ifs_ipfr_natlist;
7773448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_natfrag);
7783448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_nat);
7792393Syz155240 }
7802393Syz155240 
7812393Syz155240 
7822393Syz155240 /* ------------------------------------------------------------------------ */
7832393Syz155240 /* Function:    fr_fragexpire                                               */
7842393Syz155240 /* Returns:     Nil                                                         */
7852393Syz155240 /* Parameters:  Nil                                                         */
7862393Syz155240 /*                                                                          */
7872393Syz155240 /* Expire entries in the fragment cache table that have been there too long */
7882393Syz155240 /* ------------------------------------------------------------------------ */
7893448Sdh155122 void fr_fragexpire(ifs)
7903448Sdh155122 ipf_stack_t *ifs;
7912393Syz155240 {
7922393Syz155240 	ipfr_t	**fp, *fra;
7932393Syz155240 	nat_t	*nat;
7942393Syz155240 	SPL_INT(s);
7952393Syz155240 
7963448Sdh155122 	if (ifs->ifs_fr_frag_lock)
7972393Syz155240 		return;
7982393Syz155240 
7992393Syz155240 	SPL_NET(s);
8003448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_frag);
8012393Syz155240 	/*
8022393Syz155240 	 * Go through the entire table, looking for entries to expire,
8033448Sdh155122 	 * which is indicated by the ttl being less than or equal to
8043448Sdh155122 	 * ifs_fr_ticks.
8052393Syz155240 	 */
8063448Sdh155122 	for (fp = &ifs->ifs_ipfr_list; ((fra = *fp) != NULL); ) {
8073448Sdh155122 		if (fra->ipfr_ttl > ifs->ifs_fr_ticks)
8082393Syz155240 			break;
8093448Sdh155122 		fra->ipfr_ref--;
8103448Sdh155122 		fr_fragdelete(fra, &ifs->ifs_ipfr_tail, ifs);
8113448Sdh155122 		ifs->ifs_ipfr_stats.ifs_expire++;
8123448Sdh155122 		ifs->ifs_ipfr_inuse--;
8132393Syz155240 	}
8143448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_frag);
8152393Syz155240 
8163448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_ipidfrag);
8173448Sdh155122 	for (fp = &ifs->ifs_ipfr_ipidlist; ((fra = *fp) != NULL); ) {
8183448Sdh155122 		if (fra->ipfr_ttl > ifs->ifs_fr_ticks)
8192393Syz155240 			break;
8203448Sdh155122 		fra->ipfr_ref--;
8213448Sdh155122 		fr_fragdelete(fra, &ifs->ifs_ipfr_ipidtail, ifs);
8223448Sdh155122 		ifs->ifs_ipfr_stats.ifs_expire++;
8233448Sdh155122 		ifs->ifs_ipfr_inuse--;
8242393Syz155240 	}
8253448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_ipidfrag);
8262393Syz155240 
8272393Syz155240 	/*
8282393Syz155240 	 * Same again for the NAT table, except that if the structure also
8292393Syz155240 	 * still points to a NAT structure, and the NAT structure points back
8302393Syz155240 	 * at the one to be free'd, NULL the reference from the NAT struct.
8312393Syz155240 	 * NOTE: We need to grab both mutex's early, and in this order so as
8322393Syz155240 	 * to prevent a deadlock if both try to expire at the same time.
8332393Syz155240 	 */
8343448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_nat);
8353448Sdh155122 	WRITE_ENTER(&ifs->ifs_ipf_natfrag);
8363448Sdh155122 	for (fp = &ifs->ifs_ipfr_natlist; ((fra = *fp) != NULL); ) {
8373448Sdh155122 		if (fra->ipfr_ttl > ifs->ifs_fr_ticks)
8382393Syz155240 			break;
8392393Syz155240 		nat = fra->ipfr_data;
8402393Syz155240 		if (nat != NULL) {
8412393Syz155240 			if (nat->nat_data == fra)
8422393Syz155240 				nat->nat_data = NULL;
8432393Syz155240 		}
8443448Sdh155122 		fra->ipfr_ref--;
8453448Sdh155122 		fr_fragdelete(fra, &ifs->ifs_ipfr_nattail, ifs);
8463448Sdh155122 		ifs->ifs_ipfr_stats.ifs_expire++;
8473448Sdh155122 		ifs->ifs_ipfr_inuse--;
8482393Syz155240 	}
8493448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_natfrag);
8503448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_nat);
8512393Syz155240 	SPL_X(s);
8522393Syz155240 }
8532393Syz155240 
8542393Syz155240 
8552393Syz155240 /* ------------------------------------------------------------------------ */
8562393Syz155240 /* Function:    fr_slowtimer                                                */
8572393Syz155240 /* Returns:     Nil                                                         */
8582393Syz155240 /* Parameters:  Nil                                                         */
8592393Syz155240 /*                                                                          */
8602393Syz155240 /* Slowly expire held state for fragments.  Timeouts are set * in           */
8612393Syz155240 /* expectation of this being called twice per second.                       */
8622393Syz155240 /* ------------------------------------------------------------------------ */
8632393Syz155240 #if !defined(_KERNEL) || (!SOLARIS && !defined(__hpux) && !defined(__sgi) && \
8642393Syz155240 			  !defined(__osf__) && !defined(linux))
8652393Syz155240 # if defined(_KERNEL) && ((BSD >= 199103) || defined(__sgi))
8663448Sdh155122 void fr_slowtimer __P((void *arg))
8672393Syz155240 # else
8683448Sdh155122 int fr_slowtimer(void *arg)
8692393Syz155240 # endif
8702393Syz155240 {
8713448Sdh155122 	ipf_stack_t *ifs = arg;
8723448Sdh155122 
8733448Sdh155122 	READ_ENTER(&ifs->ifs_ipf_global);
8742393Syz155240 
8753448Sdh155122 	fr_fragexpire(ifs);
8763448Sdh155122 	fr_timeoutstate(ifs);
8773448Sdh155122 	fr_natexpire(ifs);
8783448Sdh155122 	fr_authexpire(ifs);
8793448Sdh155122 	ifs->ifs_fr_ticks++;
8803448Sdh155122 	if (ifs->ifs_fr_running <= 0)
8812393Syz155240 		goto done;
8822393Syz155240 # ifdef _KERNEL
8832393Syz155240 #  if defined(__NetBSD__) && (__NetBSD_Version__ >= 104240000)
8842393Syz155240 	callout_reset(&fr_slowtimer_ch, hz / 2, fr_slowtimer, NULL);
8852393Syz155240 #  else
8862393Syz155240 #   if defined(__OpenBSD__)
8872393Syz155240 	timeout_add(&fr_slowtimer_ch, hz/2);
8882393Syz155240 #   else
8892393Syz155240 #    if (__FreeBSD_version >= 300000)
8902393Syz155240 	fr_slowtimer_ch = timeout(fr_slowtimer, NULL, hz/2);
8912393Syz155240 #    else
8922393Syz155240 #     ifdef linux
8932393Syz155240 	;
8942393Syz155240 #     else
8952393Syz155240 	timeout(fr_slowtimer, NULL, hz/2);
8962393Syz155240 #     endif
8972393Syz155240 #    endif /* FreeBSD */
8982393Syz155240 #   endif /* OpenBSD */
8992393Syz155240 #  endif /* NetBSD */
9002393Syz155240 # endif
9012393Syz155240 done:
9023448Sdh155122 	RWLOCK_EXIT(&ifs->ifs_ipf_global);
9032393Syz155240 # if (BSD < 199103) || !defined(_KERNEL)
9042393Syz155240 	return 0;
9052393Syz155240 # endif
9062393Syz155240 }
9072393Syz155240 #endif /* !SOLARIS && !defined(__hpux) && !defined(__sgi) */
9083448Sdh155122 
9093448Sdh155122 /*ARGSUSED*/
9103448Sdh155122 int fr_nextfrag(token, itp, top, tail, lock, ifs)
9113448Sdh155122 ipftoken_t *token;
9123448Sdh155122 ipfgeniter_t *itp;
9133448Sdh155122 ipfr_t **top, ***tail;
9143448Sdh155122 ipfrwlock_t *lock;
9153448Sdh155122 ipf_stack_t *ifs;
9163448Sdh155122 {
9173448Sdh155122 	ipfr_t *frag, *next, zero;
9183448Sdh155122 	int error = 0;
9193448Sdh155122 
920*6518Sjojemann 	READ_ENTER(lock);
921*6518Sjojemann 
922*6518Sjojemann 	/*
923*6518Sjojemann 	 * Retrieve "previous" entry from token and find the next entry.
924*6518Sjojemann 	 */
9253448Sdh155122 	frag = token->ipt_data;
9263448Sdh155122 	if (frag == NULL)
9273448Sdh155122 		next = *top;
9283448Sdh155122 	else
9293448Sdh155122 		next = frag->ipfr_next;
9303448Sdh155122 
931*6518Sjojemann 	/*
932*6518Sjojemann 	 * If we found an entry, add reference to it and update token.
933*6518Sjojemann 	 * Otherwise, zero out data to be returned and NULL out token.
934*6518Sjojemann 	 */
9353448Sdh155122 	if (next != NULL) {
9363448Sdh155122 		ATOMIC_INC(next->ipfr_ref);
9373448Sdh155122 		token->ipt_data = next;
9383448Sdh155122 	} else {
9393448Sdh155122 		bzero(&zero, sizeof(zero));
9403448Sdh155122 		next = &zero;
941*6518Sjojemann 		token->ipt_data = NULL;
9423448Sdh155122 	}
943*6518Sjojemann 
944*6518Sjojemann 	/*
945*6518Sjojemann 	 * Now that we have ref, it's save to give up lock.
946*6518Sjojemann 	 */
9473448Sdh155122 	RWLOCK_EXIT(lock);
9483448Sdh155122 
949*6518Sjojemann 	/*
950*6518Sjojemann 	 * Copy out data and clean up references and token as needed.
951*6518Sjojemann 	 */
9523448Sdh155122 	error = COPYOUT(next, itp->igi_data, sizeof(*next));
9533448Sdh155122 	if (error != 0)
9543448Sdh155122 		error = EFAULT;
955*6518Sjojemann 	if (token->ipt_data == NULL) {
956*6518Sjojemann 		ipf_freetoken(token, ifs);
957*6518Sjojemann 	} else {
958*6518Sjojemann 		if (frag != NULL)
959*6518Sjojemann 			fr_fragderef(&frag, lock, ifs);
960*6518Sjojemann 		if (next->ipfr_next == NULL)
961*6518Sjojemann 			ipf_freetoken(token, ifs);
962*6518Sjojemann 	}
9633448Sdh155122 	return error;
9643448Sdh155122 }
9653448Sdh155122 
9663448Sdh155122 
9673448Sdh155122 void fr_fragderef(frp, lock, ifs)
9683448Sdh155122 ipfr_t **frp;
9693448Sdh155122 ipfrwlock_t *lock;
9703448Sdh155122 ipf_stack_t *ifs;
9713448Sdh155122 {
9723448Sdh155122 	ipfr_t *fra;
9733448Sdh155122 
9743448Sdh155122 	fra = *frp;
9753448Sdh155122 	*frp = NULL;
9763448Sdh155122 
9773448Sdh155122 	WRITE_ENTER(lock);
9783448Sdh155122 	fra->ipfr_ref--;
9793448Sdh155122 	if (fra->ipfr_ref <= 0) {
9803448Sdh155122 		KFREE(fra);
9813448Sdh155122 		ifs->ifs_ipfr_stats.ifs_expire++;
9823448Sdh155122 		ifs->ifs_ipfr_inuse--;
9833448Sdh155122 	}
9843448Sdh155122 	RWLOCK_EXIT(lock);
9853448Sdh155122 }
986