Home | History | Annotate | Line # | Download | only in netinet
ip_reass.c revision 1.3
      1  1.3  rmind /*	$NetBSD: ip_reass.c,v 1.3 2010/08/25 00:05:14 rmind Exp $	*/
      2  1.1  rmind 
      3  1.1  rmind /*
      4  1.1  rmind  * Copyright (c) 1982, 1986, 1988, 1993
      5  1.1  rmind  *	The Regents of the University of California.  All rights reserved.
      6  1.1  rmind  *
      7  1.1  rmind  * Redistribution and use in source and binary forms, with or without
      8  1.1  rmind  * modification, are permitted provided that the following conditions
      9  1.1  rmind  * are met:
     10  1.1  rmind  * 1. Redistributions of source code must retain the above copyright
     11  1.1  rmind  *    notice, this list of conditions and the following disclaimer.
     12  1.1  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  rmind  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  rmind  *    documentation and/or other materials provided with the distribution.
     15  1.1  rmind  * 3. Neither the name of the University nor the names of its contributors
     16  1.1  rmind  *    may be used to endorse or promote products derived from this software
     17  1.1  rmind  *    without specific prior written permission.
     18  1.1  rmind  *
     19  1.1  rmind  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  1.1  rmind  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.1  rmind  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.1  rmind  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  1.1  rmind  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  1.1  rmind  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  1.1  rmind  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  1.1  rmind  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  1.1  rmind  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  1.1  rmind  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  1.1  rmind  * SUCH DAMAGE.
     30  1.1  rmind  *
     31  1.1  rmind  *	@(#)ip_input.c	8.2 (Berkeley) 1/4/94
     32  1.1  rmind  */
     33  1.1  rmind 
     34  1.1  rmind /*
     35  1.1  rmind  * IP reassembly.
     36  1.1  rmind  *
     37  1.1  rmind  * Additive-Increase/Multiplicative-Decrease (AIMD) strategy for IP
     38  1.1  rmind  * reassembly queue buffer managment.
     39  1.1  rmind  *
     40  1.1  rmind  * We keep a count of total IP fragments (NB: not fragmented packets),
     41  1.1  rmind  * awaiting reassembly (ip_nfrags) and a limit (ip_maxfrags) on fragments.
     42  1.1  rmind  * If ip_nfrags exceeds ip_maxfrags the limit, we drop half the total
     43  1.1  rmind  * fragments in reassembly queues.  This AIMD policy avoids repeatedly
     44  1.1  rmind  * deleting single packets under heavy fragmentation load (e.g., from lossy
     45  1.1  rmind  * NFS peers).
     46  1.1  rmind  */
     47  1.1  rmind 
     48  1.1  rmind #include <sys/cdefs.h>
     49  1.3  rmind __KERNEL_RCSID(0, "$NetBSD: ip_reass.c,v 1.3 2010/08/25 00:05:14 rmind Exp $");
     50  1.1  rmind 
     51  1.1  rmind #include <sys/param.h>
     52  1.2  rmind #include <sys/types.h>
     53  1.1  rmind 
     54  1.1  rmind #include <sys/malloc.h>
     55  1.1  rmind #include <sys/mbuf.h>
     56  1.1  rmind #include <sys/domain.h>
     57  1.1  rmind #include <sys/protosw.h>
     58  1.1  rmind #include <sys/pool.h>
     59  1.2  rmind #include <sys/queue.h>
     60  1.1  rmind #include <sys/sysctl.h>
     61  1.2  rmind #include <sys/systm.h>
     62  1.1  rmind 
     63  1.1  rmind #include <net/if.h>
     64  1.1  rmind #include <net/route.h>
     65  1.1  rmind 
     66  1.1  rmind #include <netinet/in.h>
     67  1.1  rmind #include <netinet/in_systm.h>
     68  1.1  rmind #include <netinet/ip.h>
     69  1.1  rmind #include <netinet/in_pcb.h>
     70  1.2  rmind #include <netinet/ip_var.h>
     71  1.1  rmind #include <netinet/in_proto.h>
     72  1.1  rmind #include <netinet/ip_private.h>
     73  1.1  rmind #include <netinet/in_var.h>
     74  1.1  rmind 
     75  1.1  rmind /*
     76  1.3  rmind  * IP reassembly queue structures.  Each fragment being reassembled is
     77  1.3  rmind  * attached to one of these structures.  They are timed out after TTL
     78  1.3  rmind  * drops to 0, and may also be reclaimed if memory becomes tight.
     79  1.3  rmind  */
     80  1.3  rmind 
     81  1.3  rmind typedef struct ipfr_qent {
     82  1.3  rmind 	TAILQ_ENTRY(ipfr_qent)	ipqe_q;
     83  1.3  rmind 	struct ip *		ipqe_ip;
     84  1.3  rmind 	struct mbuf *		ipqe_m;
     85  1.3  rmind 	bool			ipqe_mff;
     86  1.3  rmind } ipfr_qent_t;
     87  1.3  rmind 
     88  1.3  rmind typedef struct ipfr_queue {
     89  1.3  rmind 	LIST_ENTRY(ipfr_queue)	ipq_q;		/* to other reass headers */
     90  1.3  rmind 	TAILQ_HEAD(, ipfr_qent)	ipq_fragq;	/* queue of fragment entries */
     91  1.3  rmind 	uint8_t			ipq_ttl;	/* time for reass q to live */
     92  1.3  rmind 	uint8_t			ipq_p;		/* protocol of this fragment */
     93  1.3  rmind 	uint16_t		ipq_id;		/* sequence id for reassembly */
     94  1.3  rmind 	struct in_addr		ipq_src;
     95  1.3  rmind 	struct in_addr		ipq_dst;
     96  1.3  rmind 	uint16_t		ipq_nfrags;	/* frags in this queue entry */
     97  1.3  rmind 	uint8_t 		ipq_tos;	/* TOS of this fragment */
     98  1.3  rmind } ipfr_queue_t;
     99  1.3  rmind 
    100  1.3  rmind /*
    101  1.3  rmind  * Hash table of IP reassembly queues.
    102  1.1  rmind  */
    103  1.1  rmind #define	IPREASS_HASH_SHIFT	6
    104  1.1  rmind #define	IPREASS_HASH_SIZE	(1 << IPREASS_HASH_SHIFT)
    105  1.1  rmind #define	IPREASS_HASH_MASK	(IPREASS_HASH_SIZE - 1)
    106  1.1  rmind #define	IPREASS_HASH(x, y) \
    107  1.1  rmind 	(((((x) & 0xf) | ((((x) >> 8) & 0xf) << 4)) ^ (y)) & IPREASS_HASH_MASK)
    108  1.1  rmind 
    109  1.3  rmind static LIST_HEAD(, ipfr_queue)	ip_frags[IPREASS_HASH_SIZE];
    110  1.3  rmind static struct pool	ipqent_pool;
    111  1.3  rmind static int		ipq_locked;
    112  1.1  rmind 
    113  1.3  rmind /* Number of packets in reassembly queue and total number of fragments. */
    114  1.3  rmind static int		ip_nfragpackets;
    115  1.3  rmind static int		ip_nfrags;
    116  1.1  rmind 
    117  1.3  rmind /* Limits on packet and fragments. */
    118  1.3  rmind static int		ip_maxfragpackets;
    119  1.3  rmind static int		ip_maxfrags;
    120  1.1  rmind 
    121  1.1  rmind /*
    122  1.3  rmind  * Cached copy of nmbclusters.  If nbclusters is different, recalculate
    123  1.3  rmind  * IP parameters derived from nmbclusters.
    124  1.2  rmind  */
    125  1.3  rmind static int		ip_nmbclusters;
    126  1.1  rmind 
    127  1.1  rmind /*
    128  1.1  rmind  * IP reassembly TTL machinery for multiplicative drop.
    129  1.1  rmind  */
    130  1.3  rmind static u_int		fragttl_histo[IPFRAGTTL + 1];
    131  1.1  rmind 
    132  1.3  rmind void			sysctl_ip_reass_setup(void);
    133  1.3  rmind static void		ip_nmbclusters_changed(void);
    134  1.2  rmind 
    135  1.3  rmind static struct mbuf *	ip_reass(ipfr_qent_t *, ipfr_queue_t *, u_int);
    136  1.2  rmind static u_int		ip_reass_ttl_decr(u_int ticks);
    137  1.2  rmind static void		ip_reass_drophalf(void);
    138  1.3  rmind static void		ip_freef(ipfr_queue_t *);
    139  1.1  rmind 
    140  1.1  rmind /*
    141  1.1  rmind  * ip_reass_init:
    142  1.1  rmind  *
    143  1.1  rmind  *	Initialization of IP reassembly mechanism.
    144  1.1  rmind  */
    145  1.1  rmind void
    146  1.1  rmind ip_reass_init(void)
    147  1.1  rmind {
    148  1.1  rmind 	int i;
    149  1.1  rmind 
    150  1.3  rmind 	pool_init(&ipqent_pool, sizeof(ipfr_qent_t), 0, 0, 0, "ipqepl",
    151  1.1  rmind 	    NULL, IPL_VM);
    152  1.1  rmind 
    153  1.1  rmind 	for (i = 0; i < IPREASS_HASH_SIZE; i++) {
    154  1.3  rmind 		LIST_INIT(&ip_frags[i]);
    155  1.1  rmind 	}
    156  1.1  rmind 	ip_maxfragpackets = 200;
    157  1.1  rmind 	ip_maxfrags = 0;
    158  1.1  rmind 	ip_nmbclusters_changed();
    159  1.1  rmind 
    160  1.1  rmind 	sysctl_ip_reass_setup();
    161  1.1  rmind }
    162  1.1  rmind 
    163  1.1  rmind static struct sysctllog *ip_reass_sysctllog;
    164  1.1  rmind 
    165  1.1  rmind void
    166  1.1  rmind sysctl_ip_reass_setup(void)
    167  1.1  rmind {
    168  1.1  rmind 
    169  1.1  rmind 	sysctl_createv(&ip_reass_sysctllog, 0, NULL, NULL,
    170  1.1  rmind 		CTLFLAG_PERMANENT,
    171  1.1  rmind 		CTLTYPE_NODE, "net", NULL,
    172  1.1  rmind 		NULL, 0, NULL, 0,
    173  1.1  rmind 		CTL_NET, CTL_EOL);
    174  1.1  rmind 	sysctl_createv(&ip_reass_sysctllog, 0, NULL, NULL,
    175  1.1  rmind 		CTLFLAG_PERMANENT,
    176  1.1  rmind 		CTLTYPE_NODE, "inet",
    177  1.1  rmind 		SYSCTL_DESCR("PF_INET related settings"),
    178  1.1  rmind 		NULL, 0, NULL, 0,
    179  1.1  rmind 		CTL_NET, PF_INET, CTL_EOL);
    180  1.1  rmind 	sysctl_createv(&ip_reass_sysctllog, 0, NULL, NULL,
    181  1.1  rmind 		CTLFLAG_PERMANENT,
    182  1.1  rmind 		CTLTYPE_NODE, "ip",
    183  1.1  rmind 		SYSCTL_DESCR("IPv4 related settings"),
    184  1.1  rmind 		NULL, 0, NULL, 0,
    185  1.1  rmind 		CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
    186  1.1  rmind 
    187  1.1  rmind 	sysctl_createv(&ip_reass_sysctllog, 0, NULL, NULL,
    188  1.1  rmind 		CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    189  1.1  rmind 		CTLTYPE_INT, "maxfragpackets",
    190  1.1  rmind 		SYSCTL_DESCR("Maximum number of fragments to retain for "
    191  1.1  rmind 			     "possible reassembly"),
    192  1.1  rmind 		NULL, 0, &ip_maxfragpackets, 0,
    193  1.1  rmind 		CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MAXFRAGPACKETS, CTL_EOL);
    194  1.1  rmind }
    195  1.1  rmind 
    196  1.1  rmind #define CHECK_NMBCLUSTER_PARAMS()				\
    197  1.1  rmind do {								\
    198  1.1  rmind 	if (__predict_false(ip_nmbclusters != nmbclusters))	\
    199  1.1  rmind 		ip_nmbclusters_changed();			\
    200  1.1  rmind } while (/*CONSTCOND*/0)
    201  1.1  rmind 
    202  1.1  rmind /*
    203  1.1  rmind  * Compute IP limits derived from the value of nmbclusters.
    204  1.1  rmind  */
    205  1.1  rmind static void
    206  1.1  rmind ip_nmbclusters_changed(void)
    207  1.1  rmind {
    208  1.1  rmind 	ip_maxfrags = nmbclusters / 4;
    209  1.1  rmind 	ip_nmbclusters = nmbclusters;
    210  1.1  rmind }
    211  1.1  rmind 
    212  1.1  rmind static inline int	ipq_lock_try(void);
    213  1.1  rmind static inline void	ipq_unlock(void);
    214  1.1  rmind 
    215  1.1  rmind static inline int
    216  1.1  rmind ipq_lock_try(void)
    217  1.1  rmind {
    218  1.1  rmind 	int s;
    219  1.1  rmind 
    220  1.1  rmind 	/*
    221  1.1  rmind 	 * Use splvm() -- we're blocking things that would cause
    222  1.1  rmind 	 * mbuf allocation.
    223  1.1  rmind 	 */
    224  1.1  rmind 	s = splvm();
    225  1.1  rmind 	if (ipq_locked) {
    226  1.1  rmind 		splx(s);
    227  1.1  rmind 		return (0);
    228  1.1  rmind 	}
    229  1.1  rmind 	ipq_locked = 1;
    230  1.1  rmind 	splx(s);
    231  1.1  rmind 	return (1);
    232  1.1  rmind }
    233  1.1  rmind 
    234  1.1  rmind static inline void
    235  1.1  rmind ipq_unlock(void)
    236  1.1  rmind {
    237  1.1  rmind 	int s;
    238  1.1  rmind 
    239  1.1  rmind 	s = splvm();
    240  1.1  rmind 	ipq_locked = 0;
    241  1.1  rmind 	splx(s);
    242  1.1  rmind }
    243  1.1  rmind 
    244  1.1  rmind #ifdef DIAGNOSTIC
    245  1.1  rmind #define	IPQ_LOCK()							\
    246  1.1  rmind do {									\
    247  1.1  rmind 	if (ipq_lock_try() == 0) {					\
    248  1.1  rmind 		printf("%s:%d: ipq already locked\n", __FILE__, __LINE__); \
    249  1.1  rmind 		panic("ipq_lock");					\
    250  1.1  rmind 	}								\
    251  1.1  rmind } while (/*CONSTCOND*/ 0)
    252  1.1  rmind #define	IPQ_LOCK_CHECK()						\
    253  1.1  rmind do {									\
    254  1.1  rmind 	if (ipq_locked == 0) {						\
    255  1.1  rmind 		printf("%s:%d: ipq lock not held\n", __FILE__, __LINE__); \
    256  1.1  rmind 		panic("ipq lock check");				\
    257  1.1  rmind 	}								\
    258  1.1  rmind } while (/*CONSTCOND*/ 0)
    259  1.1  rmind #else
    260  1.1  rmind #define	IPQ_LOCK()		(void) ipq_lock_try()
    261  1.1  rmind #define	IPQ_LOCK_CHECK()	/* nothing */
    262  1.1  rmind #endif
    263  1.1  rmind 
    264  1.1  rmind #define	IPQ_UNLOCK()		ipq_unlock()
    265  1.1  rmind 
    266  1.1  rmind /*
    267  1.1  rmind  * ip_reass:
    268  1.1  rmind  *
    269  1.1  rmind  *	Take incoming datagram fragment and try to reassemble it into whole
    270  1.1  rmind  *	datagram.  If a chain for reassembly of this datagram already exists,
    271  1.1  rmind  *	then it is given as 'fp'; otherwise have to make a chain.
    272  1.1  rmind  */
    273  1.1  rmind struct mbuf *
    274  1.3  rmind ip_reass(ipfr_qent_t *ipqe, ipfr_queue_t *fp, const u_int hash)
    275  1.1  rmind {
    276  1.1  rmind 	const int hlen = ipqe->ipqe_ip->ip_hl << 2;
    277  1.1  rmind 	struct mbuf *m = ipqe->ipqe_m, *t;
    278  1.3  rmind 	ipfr_qent_t *nq, *p, *q;
    279  1.1  rmind 	struct ip *ip;
    280  1.1  rmind 	int i, next, s;
    281  1.1  rmind 
    282  1.1  rmind 	IPQ_LOCK_CHECK();
    283  1.1  rmind 
    284  1.1  rmind 	/*
    285  1.1  rmind 	 * Presence of header sizes in mbufs would confuse code below.
    286  1.1  rmind 	 */
    287  1.1  rmind 	m->m_data += hlen;
    288  1.1  rmind 	m->m_len -= hlen;
    289  1.1  rmind 
    290  1.1  rmind #ifdef	notyet
    291  1.1  rmind 	/* Make sure fragment limit is up-to-date. */
    292  1.1  rmind 	CHECK_NMBCLUSTER_PARAMS();
    293  1.1  rmind 
    294  1.1  rmind 	/* If we have too many fragments, drop the older half. */
    295  1.1  rmind 	if (ip_nfrags >= ip_maxfrags) {
    296  1.1  rmind 		ip_reass_drophalf(void);
    297  1.1  rmind 	}
    298  1.1  rmind #endif
    299  1.1  rmind 
    300  1.1  rmind 	/*
    301  1.1  rmind 	 * We are about to add a fragment; increment frag count.
    302  1.1  rmind 	 */
    303  1.1  rmind 	ip_nfrags++;
    304  1.1  rmind 
    305  1.1  rmind 	/*
    306  1.1  rmind 	 * If first fragment to arrive, create a reassembly queue.
    307  1.1  rmind 	 */
    308  1.1  rmind 	if (fp == NULL) {
    309  1.1  rmind 		/*
    310  1.1  rmind 		 * Enforce upper bound on number of fragmented packets
    311  1.1  rmind 		 * for which we attempt reassembly:  a) if maxfrag is 0,
    312  1.1  rmind 		 * never accept fragments  b) if maxfrag is -1, accept
    313  1.1  rmind 		 * all fragments without limitation.
    314  1.1  rmind 		 */
    315  1.1  rmind 		if (ip_maxfragpackets < 0)
    316  1.1  rmind 			;
    317  1.1  rmind 		else if (ip_nfragpackets >= ip_maxfragpackets) {
    318  1.1  rmind 			goto dropfrag;
    319  1.1  rmind 		}
    320  1.1  rmind 		ip_nfragpackets++;
    321  1.3  rmind 		fp = malloc(sizeof(ipfr_queue_t), M_FTABLE, M_NOWAIT);
    322  1.1  rmind 		if (fp == NULL) {
    323  1.1  rmind 			goto dropfrag;
    324  1.1  rmind 		}
    325  1.3  rmind 		LIST_INSERT_HEAD(&ip_frags[hash], fp, ipq_q);
    326  1.1  rmind 		fp->ipq_nfrags = 1;
    327  1.1  rmind 		fp->ipq_ttl = IPFRAGTTL;
    328  1.1  rmind 		fp->ipq_p = ipqe->ipqe_ip->ip_p;
    329  1.1  rmind 		fp->ipq_id = ipqe->ipqe_ip->ip_id;
    330  1.1  rmind 		fp->ipq_tos = ipqe->ipqe_ip->ip_tos;
    331  1.1  rmind 		TAILQ_INIT(&fp->ipq_fragq);
    332  1.1  rmind 		fp->ipq_src = ipqe->ipqe_ip->ip_src;
    333  1.1  rmind 		fp->ipq_dst = ipqe->ipqe_ip->ip_dst;
    334  1.1  rmind 		p = NULL;
    335  1.1  rmind 		goto insert;
    336  1.1  rmind 	} else {
    337  1.1  rmind 		fp->ipq_nfrags++;
    338  1.1  rmind 	}
    339  1.1  rmind 
    340  1.1  rmind 	/*
    341  1.1  rmind 	 * Find a segment which begins after this one does.
    342  1.1  rmind 	 */
    343  1.1  rmind 	for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
    344  1.1  rmind 	    p = q, q = TAILQ_NEXT(q, ipqe_q))
    345  1.1  rmind 		if (ntohs(q->ipqe_ip->ip_off) > ntohs(ipqe->ipqe_ip->ip_off))
    346  1.1  rmind 			break;
    347  1.1  rmind 
    348  1.1  rmind 	/*
    349  1.1  rmind 	 * If there is a preceding segment, it may provide some of our
    350  1.1  rmind 	 * data already.  If so, drop the data from the incoming segment.
    351  1.1  rmind 	 * If it provides all of our data, drop us.
    352  1.1  rmind 	 */
    353  1.1  rmind 	if (p != NULL) {
    354  1.1  rmind 		i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) -
    355  1.1  rmind 		    ntohs(ipqe->ipqe_ip->ip_off);
    356  1.1  rmind 		if (i > 0) {
    357  1.1  rmind 			if (i >= ntohs(ipqe->ipqe_ip->ip_len)) {
    358  1.1  rmind 				goto dropfrag;
    359  1.1  rmind 			}
    360  1.1  rmind 			m_adj(ipqe->ipqe_m, i);
    361  1.1  rmind 			ipqe->ipqe_ip->ip_off =
    362  1.1  rmind 			    htons(ntohs(ipqe->ipqe_ip->ip_off) + i);
    363  1.1  rmind 			ipqe->ipqe_ip->ip_len =
    364  1.1  rmind 			    htons(ntohs(ipqe->ipqe_ip->ip_len) - i);
    365  1.1  rmind 		}
    366  1.1  rmind 	}
    367  1.1  rmind 
    368  1.1  rmind 	/*
    369  1.1  rmind 	 * While we overlap succeeding segments trim them or, if they are
    370  1.1  rmind 	 * completely covered, dequeue them.
    371  1.1  rmind 	 */
    372  1.1  rmind 	for (; q != NULL &&
    373  1.1  rmind 	    ntohs(ipqe->ipqe_ip->ip_off) + ntohs(ipqe->ipqe_ip->ip_len) >
    374  1.1  rmind 	    ntohs(q->ipqe_ip->ip_off); q = nq) {
    375  1.1  rmind 		i = (ntohs(ipqe->ipqe_ip->ip_off) +
    376  1.1  rmind 		    ntohs(ipqe->ipqe_ip->ip_len)) - ntohs(q->ipqe_ip->ip_off);
    377  1.1  rmind 		if (i < ntohs(q->ipqe_ip->ip_len)) {
    378  1.1  rmind 			q->ipqe_ip->ip_len =
    379  1.1  rmind 			    htons(ntohs(q->ipqe_ip->ip_len) - i);
    380  1.1  rmind 			q->ipqe_ip->ip_off =
    381  1.1  rmind 			    htons(ntohs(q->ipqe_ip->ip_off) + i);
    382  1.1  rmind 			m_adj(q->ipqe_m, i);
    383  1.1  rmind 			break;
    384  1.1  rmind 		}
    385  1.1  rmind 		nq = TAILQ_NEXT(q, ipqe_q);
    386  1.1  rmind 		m_freem(q->ipqe_m);
    387  1.1  rmind 		TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
    388  1.1  rmind 		s = splvm();
    389  1.1  rmind 		pool_put(&ipqent_pool, q);
    390  1.1  rmind 		splx(s);
    391  1.1  rmind 		fp->ipq_nfrags--;
    392  1.1  rmind 		ip_nfrags--;
    393  1.1  rmind 	}
    394  1.1  rmind 
    395  1.1  rmind insert:
    396  1.1  rmind 	/*
    397  1.1  rmind 	 * Stick new segment in its place; check for complete reassembly.
    398  1.1  rmind 	 */
    399  1.1  rmind 	if (p == NULL) {
    400  1.1  rmind 		TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
    401  1.1  rmind 	} else {
    402  1.1  rmind 		TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q);
    403  1.1  rmind 	}
    404  1.1  rmind 	next = 0;
    405  1.1  rmind 	for (p = NULL, q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL;
    406  1.1  rmind 	    p = q, q = TAILQ_NEXT(q, ipqe_q)) {
    407  1.1  rmind 		if (ntohs(q->ipqe_ip->ip_off) != next) {
    408  1.1  rmind 			IPQ_UNLOCK();
    409  1.1  rmind 			return NULL;
    410  1.1  rmind 		}
    411  1.1  rmind 		next += ntohs(q->ipqe_ip->ip_len);
    412  1.1  rmind 	}
    413  1.1  rmind 	if (p->ipqe_mff) {
    414  1.1  rmind 		IPQ_UNLOCK();
    415  1.1  rmind 		return NULL;
    416  1.1  rmind 	}
    417  1.1  rmind 	/*
    418  1.1  rmind 	 * Reassembly is complete.  Check for a bogus message size and
    419  1.1  rmind 	 * concatenate fragments.
    420  1.1  rmind 	 */
    421  1.1  rmind 	q = TAILQ_FIRST(&fp->ipq_fragq);
    422  1.1  rmind 	ip = q->ipqe_ip;
    423  1.1  rmind 	if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
    424  1.1  rmind 		IP_STATINC(IP_STAT_TOOLONG);
    425  1.1  rmind 		ip_freef(fp);
    426  1.1  rmind 		IPQ_UNLOCK();
    427  1.1  rmind 		return NULL;
    428  1.1  rmind 	}
    429  1.1  rmind 	m = q->ipqe_m;
    430  1.1  rmind 	t = m->m_next;
    431  1.1  rmind 	m->m_next = NULL;
    432  1.1  rmind 	m_cat(m, t);
    433  1.1  rmind 	nq = TAILQ_NEXT(q, ipqe_q);
    434  1.1  rmind 	s = splvm();
    435  1.1  rmind 	pool_put(&ipqent_pool, q);
    436  1.1  rmind 	splx(s);
    437  1.1  rmind 	for (q = nq; q != NULL; q = nq) {
    438  1.1  rmind 		t = q->ipqe_m;
    439  1.1  rmind 		nq = TAILQ_NEXT(q, ipqe_q);
    440  1.1  rmind 		s = splvm();
    441  1.1  rmind 		pool_put(&ipqent_pool, q);
    442  1.1  rmind 		splx(s);
    443  1.1  rmind 		m_cat(m, t);
    444  1.1  rmind 	}
    445  1.1  rmind 	ip_nfrags -= fp->ipq_nfrags;
    446  1.1  rmind 
    447  1.1  rmind 	/*
    448  1.1  rmind 	 * Create header for new packet by modifying header of first
    449  1.1  rmind 	 * packet.  Dequeue and discard fragment reassembly header.  Make
    450  1.1  rmind 	 * header visible.
    451  1.1  rmind 	 */
    452  1.2  rmind 	ip->ip_len = htons((ip->ip_hl << 2) + next);
    453  1.1  rmind 	ip->ip_src = fp->ipq_src;
    454  1.1  rmind 	ip->ip_dst = fp->ipq_dst;
    455  1.2  rmind 
    456  1.1  rmind 	LIST_REMOVE(fp, ipq_q);
    457  1.1  rmind 	free(fp, M_FTABLE);
    458  1.1  rmind 	ip_nfragpackets--;
    459  1.1  rmind 	m->m_len += (ip->ip_hl << 2);
    460  1.1  rmind 	m->m_data -= (ip->ip_hl << 2);
    461  1.1  rmind 	/* some debugging cruft by sklower, below, will go away soon */
    462  1.1  rmind 	if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */
    463  1.1  rmind 		int plen = 0;
    464  1.1  rmind 		for (t = m; t; t = t->m_next) {
    465  1.1  rmind 			plen += t->m_len;
    466  1.1  rmind 		}
    467  1.1  rmind 		m->m_pkthdr.len = plen;
    468  1.1  rmind 		m->m_pkthdr.csum_flags = 0;
    469  1.1  rmind 	}
    470  1.1  rmind 	IPQ_UNLOCK();
    471  1.1  rmind 	return m;
    472  1.1  rmind 
    473  1.1  rmind dropfrag:
    474  1.1  rmind 	if (fp != NULL) {
    475  1.1  rmind 		fp->ipq_nfrags--;
    476  1.1  rmind 	}
    477  1.1  rmind 	ip_nfrags--;
    478  1.1  rmind 	IP_STATINC(IP_STAT_FRAGDROPPED);
    479  1.1  rmind 	m_freem(m);
    480  1.1  rmind 	s = splvm();
    481  1.1  rmind 	pool_put(&ipqent_pool, ipqe);
    482  1.1  rmind 	splx(s);
    483  1.1  rmind 	IPQ_UNLOCK();
    484  1.1  rmind 	return NULL;
    485  1.1  rmind }
    486  1.1  rmind 
    487  1.1  rmind /*
    488  1.1  rmind  * ip_freef:
    489  1.1  rmind  *
    490  1.1  rmind  *	Free a fragment reassembly header and all associated datagrams.
    491  1.1  rmind  */
    492  1.2  rmind static void
    493  1.3  rmind ip_freef(ipfr_queue_t *fp)
    494  1.1  rmind {
    495  1.3  rmind 	ipfr_qent_t *q, *p;
    496  1.1  rmind 	u_int nfrags = 0;
    497  1.1  rmind 	int s;
    498  1.1  rmind 
    499  1.1  rmind 	IPQ_LOCK_CHECK();
    500  1.1  rmind 
    501  1.1  rmind 	for (q = TAILQ_FIRST(&fp->ipq_fragq); q != NULL; q = p) {
    502  1.1  rmind 		p = TAILQ_NEXT(q, ipqe_q);
    503  1.1  rmind 		m_freem(q->ipqe_m);
    504  1.1  rmind 		nfrags++;
    505  1.1  rmind 		TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
    506  1.1  rmind 		s = splvm();
    507  1.1  rmind 		pool_put(&ipqent_pool, q);
    508  1.1  rmind 		splx(s);
    509  1.1  rmind 	}
    510  1.1  rmind 
    511  1.1  rmind 	if (nfrags != fp->ipq_nfrags) {
    512  1.1  rmind 		printf("ip_freef: nfrags %d != %d\n", fp->ipq_nfrags, nfrags);
    513  1.1  rmind 	}
    514  1.1  rmind 	ip_nfrags -= nfrags;
    515  1.1  rmind 	LIST_REMOVE(fp, ipq_q);
    516  1.1  rmind 	free(fp, M_FTABLE);
    517  1.1  rmind 	ip_nfragpackets--;
    518  1.1  rmind }
    519  1.1  rmind 
    520  1.1  rmind /*
    521  1.1  rmind  * ip_reass_ttl_decr:
    522  1.1  rmind  *
    523  1.1  rmind  *	Decrement TTL of all reasembly queue entries by `ticks'.  Count
    524  1.1  rmind  *	number of distinct fragments (as opposed to partial, fragmented
    525  1.1  rmind  *	datagrams) inthe reassembly queue.  While we  traverse the entire
    526  1.1  rmind  *	reassembly queue, compute and return the median TTL over all
    527  1.1  rmind  *	fragments.
    528  1.1  rmind  */
    529  1.1  rmind static u_int
    530  1.1  rmind ip_reass_ttl_decr(u_int ticks)
    531  1.1  rmind {
    532  1.1  rmind 	u_int nfrags, median, dropfraction, keepfraction;
    533  1.3  rmind 	ipfr_queue_t *fp, *nfp;
    534  1.1  rmind 	int i;
    535  1.1  rmind 
    536  1.1  rmind 	nfrags = 0;
    537  1.1  rmind 	memset(fragttl_histo, 0, sizeof(fragttl_histo));
    538  1.1  rmind 
    539  1.1  rmind 	for (i = 0; i < IPREASS_HASH_SIZE; i++) {
    540  1.3  rmind 		for (fp = LIST_FIRST(&ip_frags[i]); fp != NULL; fp = nfp) {
    541  1.1  rmind 			fp->ipq_ttl = ((fp->ipq_ttl <= ticks) ?
    542  1.1  rmind 			    0 : fp->ipq_ttl - ticks);
    543  1.1  rmind 			nfp = LIST_NEXT(fp, ipq_q);
    544  1.1  rmind 			if (fp->ipq_ttl == 0) {
    545  1.1  rmind 				IP_STATINC(IP_STAT_FRAGTIMEOUT);
    546  1.1  rmind 				ip_freef(fp);
    547  1.1  rmind 			} else {
    548  1.1  rmind 				nfrags += fp->ipq_nfrags;
    549  1.1  rmind 				fragttl_histo[fp->ipq_ttl] += fp->ipq_nfrags;
    550  1.1  rmind 			}
    551  1.1  rmind 		}
    552  1.1  rmind 	}
    553  1.1  rmind 
    554  1.1  rmind 	KASSERT(ip_nfrags == nfrags);
    555  1.1  rmind 
    556  1.1  rmind 	/* Find median (or other drop fraction) in histogram. */
    557  1.1  rmind 	dropfraction = (ip_nfrags / 2);
    558  1.1  rmind 	keepfraction = ip_nfrags - dropfraction;
    559  1.1  rmind 	for (i = IPFRAGTTL, median = 0; i >= 0; i--) {
    560  1.1  rmind 		median += fragttl_histo[i];
    561  1.1  rmind 		if (median >= keepfraction)
    562  1.1  rmind 			break;
    563  1.1  rmind 	}
    564  1.1  rmind 
    565  1.1  rmind 	/* Return TTL of median (or other fraction). */
    566  1.1  rmind 	return (u_int)i;
    567  1.1  rmind }
    568  1.1  rmind 
    569  1.1  rmind static void
    570  1.1  rmind ip_reass_drophalf(void)
    571  1.1  rmind {
    572  1.1  rmind 	u_int median_ticks;
    573  1.1  rmind 
    574  1.1  rmind 	/*
    575  1.1  rmind 	 * Compute median TTL of all fragments, and count frags
    576  1.1  rmind 	 * with that TTL or lower (roughly half of all fragments).
    577  1.1  rmind 	 */
    578  1.1  rmind 	median_ticks = ip_reass_ttl_decr(0);
    579  1.1  rmind 
    580  1.1  rmind 	/* Drop half. */
    581  1.1  rmind 	median_ticks = ip_reass_ttl_decr(median_ticks);
    582  1.1  rmind }
    583  1.1  rmind 
    584  1.1  rmind /*
    585  1.1  rmind  * ip_reass_drain: drain off all datagram fragments.  Do not acquire
    586  1.1  rmind  * softnet_lock as can be called from hardware interrupt context.
    587  1.1  rmind  */
    588  1.1  rmind void
    589  1.1  rmind ip_reass_drain(void)
    590  1.1  rmind {
    591  1.1  rmind 
    592  1.1  rmind 	/*
    593  1.1  rmind 	 * We may be called from a device's interrupt context.  If
    594  1.1  rmind 	 * the ipq is already busy, just bail out now.
    595  1.1  rmind 	 */
    596  1.1  rmind 	if (ipq_lock_try() != 0) {
    597  1.1  rmind 		/*
    598  1.1  rmind 		 * Drop half the total fragments now. If more mbufs are
    599  1.1  rmind 		 * needed, we will be called again soon.
    600  1.1  rmind 		 */
    601  1.1  rmind 		ip_reass_drophalf();
    602  1.1  rmind 		IPQ_UNLOCK();
    603  1.1  rmind 	}
    604  1.1  rmind }
    605  1.1  rmind 
    606  1.1  rmind /*
    607  1.1  rmind  * ip_reass_slowtimo:
    608  1.1  rmind  *
    609  1.1  rmind  *	If a timer expires on a reassembly queue, discard it.
    610  1.1  rmind  */
    611  1.1  rmind void
    612  1.1  rmind ip_reass_slowtimo(void)
    613  1.1  rmind {
    614  1.1  rmind 	static u_int dropscanidx = 0;
    615  1.1  rmind 	u_int i, median_ttl;
    616  1.1  rmind 
    617  1.1  rmind 	IPQ_LOCK();
    618  1.1  rmind 
    619  1.1  rmind 	/* Age TTL of all fragments by 1 tick .*/
    620  1.1  rmind 	median_ttl = ip_reass_ttl_decr(1);
    621  1.1  rmind 
    622  1.1  rmind 	/* Make sure fragment limit is up-to-date. */
    623  1.1  rmind 	CHECK_NMBCLUSTER_PARAMS();
    624  1.1  rmind 
    625  1.1  rmind 	/* If we have too many fragments, drop the older half. */
    626  1.1  rmind 	if (ip_nfrags > ip_maxfrags) {
    627  1.1  rmind 		ip_reass_ttl_decr(median_ttl);
    628  1.1  rmind 	}
    629  1.1  rmind 
    630  1.1  rmind 	/*
    631  1.1  rmind 	 * If we are over the maximum number of fragmented packets (due to
    632  1.1  rmind 	 * the limit being lowered), drain off enough to get down to the
    633  1.1  rmind 	 * new limit.  Start draining from the reassembly hashqueue most
    634  1.1  rmind 	 * recently drained.
    635  1.1  rmind 	 */
    636  1.1  rmind 	if (ip_maxfragpackets < 0)
    637  1.1  rmind 		;
    638  1.1  rmind 	else {
    639  1.1  rmind 		int wrapped = 0;
    640  1.1  rmind 
    641  1.1  rmind 		i = dropscanidx;
    642  1.1  rmind 		while (ip_nfragpackets > ip_maxfragpackets && wrapped == 0) {
    643  1.3  rmind 			while (LIST_FIRST(&ip_frags[i]) != NULL) {
    644  1.3  rmind 				ip_freef(LIST_FIRST(&ip_frags[i]));
    645  1.1  rmind 			}
    646  1.1  rmind 			if (++i >= IPREASS_HASH_SIZE) {
    647  1.1  rmind 				i = 0;
    648  1.1  rmind 			}
    649  1.1  rmind 			/*
    650  1.1  rmind 			 * Do not scan forever even if fragment counters are
    651  1.1  rmind 			 * wrong: stop after scanning entire reassembly queue.
    652  1.1  rmind 			 */
    653  1.1  rmind 			if (i == dropscanidx) {
    654  1.1  rmind 				wrapped = 1;
    655  1.1  rmind 			}
    656  1.1  rmind 		}
    657  1.1  rmind 		dropscanidx = i;
    658  1.1  rmind 	}
    659  1.1  rmind 	IPQ_UNLOCK();
    660  1.1  rmind }
    661  1.2  rmind 
    662  1.2  rmind /*
    663  1.2  rmind  * ip_reass_packet: generic routine to perform IP reassembly.
    664  1.2  rmind  *
    665  1.2  rmind  * => Passed fragment should have IP_MF flag and/or offset set.
    666  1.2  rmind  * => Fragment should not have other than IP_MF flags set.
    667  1.2  rmind  *
    668  1.2  rmind  * => Returns 0 on success or error otherwise.  When reassembly is complete,
    669  1.2  rmind  *    m_final representing a constructed final packet is set.
    670  1.2  rmind  */
    671  1.2  rmind int
    672  1.2  rmind ip_reass_packet(struct mbuf *m, struct ip *ip, bool mff, struct mbuf **m_final)
    673  1.2  rmind {
    674  1.3  rmind 	ipfr_queue_t *fp;
    675  1.3  rmind 	ipfr_qent_t *ipqe;
    676  1.2  rmind 	u_int hash;
    677  1.2  rmind 
    678  1.2  rmind 	/* Look for queue of fragments of this datagram. */
    679  1.3  rmind 	IPQ_LOCK();
    680  1.3  rmind 	hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
    681  1.3  rmind 	LIST_FOREACH(fp, &ip_frags[hash], ipq_q) {
    682  1.3  rmind 		if (ip->ip_id != fp->ipq_id)
    683  1.3  rmind 			continue;
    684  1.3  rmind 		if (!in_hosteq(ip->ip_src, fp->ipq_src))
    685  1.3  rmind 			continue;
    686  1.3  rmind 		if (!in_hosteq(ip->ip_dst, fp->ipq_dst))
    687  1.3  rmind 			continue;
    688  1.3  rmind 		if (ip->ip_p != fp->ipq_p)
    689  1.3  rmind 			continue;
    690  1.3  rmind 		break;
    691  1.3  rmind 	}
    692  1.2  rmind 
    693  1.2  rmind 	/* Make sure that TOS matches previous fragments. */
    694  1.2  rmind 	if (fp && fp->ipq_tos != ip->ip_tos) {
    695  1.2  rmind 		IP_STATINC(IP_STAT_BADFRAGS);
    696  1.2  rmind 		IPQ_UNLOCK();
    697  1.2  rmind 		return EINVAL;
    698  1.2  rmind 	}
    699  1.2  rmind 
    700  1.2  rmind 	/*
    701  1.2  rmind 	 * Create new entry and attempt to reassembly.
    702  1.2  rmind 	 */
    703  1.2  rmind 	IP_STATINC(IP_STAT_FRAGMENTS);
    704  1.2  rmind 	int s = splvm();
    705  1.2  rmind 	ipqe = pool_get(&ipqent_pool, PR_NOWAIT);
    706  1.2  rmind 	splx(s);
    707  1.2  rmind 	if (ipqe == NULL) {
    708  1.2  rmind 		IP_STATINC(IP_STAT_RCVMEMDROP);
    709  1.2  rmind 		IPQ_UNLOCK();
    710  1.2  rmind 		return ENOMEM;
    711  1.2  rmind 	}
    712  1.2  rmind 	ipqe->ipqe_mff = mff;
    713  1.2  rmind 	ipqe->ipqe_m = m;
    714  1.2  rmind 	ipqe->ipqe_ip = ip;
    715  1.2  rmind 
    716  1.2  rmind 	*m_final = ip_reass(ipqe, fp, hash);
    717  1.2  rmind 	if (*m_final) {
    718  1.2  rmind 		/* Note if finally reassembled. */
    719  1.2  rmind 		IP_STATINC(IP_STAT_REASSEMBLED);
    720  1.2  rmind 	}
    721  1.2  rmind 	return 0;
    722  1.2  rmind }
    723