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