ip_reass.c revision 1.13 1 /* $NetBSD: ip_reass.c,v 1.13 2018/02/08 10:03:52 maxv 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.13 2018/02/08 10:03:52 maxv 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/pool.h>
58 #include <sys/queue.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61
62 #include <net/if.h>
63
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/in_pcb.h>
68 #include <netinet/ip_var.h>
69 #include <netinet/ip_private.h>
70 #include <netinet/in_var.h>
71
72 /*
73 * IP reassembly queue structures. Each fragment being reassembled is
74 * attached to one of these structures. They are timed out after TTL
75 * drops to 0, and may also be reclaimed if memory becomes tight.
76 */
77
78 typedef struct ipfr_qent {
79 TAILQ_ENTRY(ipfr_qent) ipqe_q;
80 struct ip * ipqe_ip;
81 struct mbuf * ipqe_m;
82 bool ipqe_mff;
83 } ipfr_qent_t;
84
85 TAILQ_HEAD(ipfr_qent_head, ipfr_qent);
86
87 typedef struct ipfr_queue {
88 LIST_ENTRY(ipfr_queue) ipq_q; /* to other reass headers */
89 struct ipfr_qent_head ipq_fragq; /* queue of fragment entries */
90 uint8_t ipq_ttl; /* time for reass q to live */
91 uint8_t ipq_p; /* protocol of this fragment */
92 uint16_t ipq_id; /* sequence id for reassembly */
93 struct in_addr ipq_src;
94 struct in_addr ipq_dst;
95 uint16_t ipq_nfrags; /* frags in this queue entry */
96 uint8_t ipq_tos; /* TOS of this fragment */
97 } ipfr_queue_t;
98
99 /*
100 * Hash table of IP reassembly queues.
101 */
102 #define IPREASS_HASH_SHIFT 6
103 #define IPREASS_HASH_SIZE (1 << IPREASS_HASH_SHIFT)
104 #define IPREASS_HASH_MASK (IPREASS_HASH_SIZE - 1)
105 #define IPREASS_HASH(x, y) \
106 (((((x) & 0xf) | ((((x) >> 8) & 0xf) << 4)) ^ (y)) & IPREASS_HASH_MASK)
107
108 static LIST_HEAD(, ipfr_queue) ip_frags[IPREASS_HASH_SIZE];
109 static pool_cache_t ipfren_cache;
110 static kmutex_t ipfr_lock;
111
112 /* Number of packets in reassembly queue and total number of fragments. */
113 static int ip_nfragpackets;
114 static int ip_nfrags;
115
116 /* Limits on packet and fragments. */
117 static int ip_maxfragpackets;
118 static int ip_maxfrags;
119
120 /*
121 * Cached copy of nmbclusters. If nbclusters is different, recalculate
122 * IP parameters derived from nmbclusters.
123 */
124 static int ip_nmbclusters;
125
126 /*
127 * IP reassembly TTL machinery for multiplicative drop.
128 */
129 static u_int fragttl_histo[IPFRAGTTL + 1];
130
131 static struct sysctllog *ip_reass_sysctllog;
132
133 void sysctl_ip_reass_setup(void);
134 static void ip_nmbclusters_changed(void);
135
136 static struct mbuf * ip_reass(ipfr_qent_t *, ipfr_queue_t *, u_int);
137 static u_int ip_reass_ttl_decr(u_int ticks);
138 static void ip_reass_drophalf(void);
139 static void ip_freef(ipfr_queue_t *);
140
141 /*
142 * ip_reass_init:
143 *
144 * Initialization of IP reassembly mechanism.
145 */
146 void
147 ip_reass_init(void)
148 {
149 int i;
150
151 ipfren_cache = pool_cache_init(sizeof(ipfr_qent_t), coherency_unit,
152 0, 0, "ipfrenpl", NULL, IPL_NET, NULL, NULL, NULL);
153 mutex_init(&ipfr_lock, MUTEX_DEFAULT, IPL_VM);
154
155 for (i = 0; i < IPREASS_HASH_SIZE; i++) {
156 LIST_INIT(&ip_frags[i]);
157 }
158 ip_maxfragpackets = 200;
159 ip_maxfrags = 0;
160 ip_nmbclusters_changed();
161
162 sysctl_ip_reass_setup();
163 }
164
165 void
166 sysctl_ip_reass_setup(void)
167 {
168
169 sysctl_createv(&ip_reass_sysctllog, 0, NULL, NULL,
170 CTLFLAG_PERMANENT,
171 CTLTYPE_NODE, "inet",
172 SYSCTL_DESCR("PF_INET related settings"),
173 NULL, 0, NULL, 0,
174 CTL_NET, PF_INET, CTL_EOL);
175 sysctl_createv(&ip_reass_sysctllog, 0, NULL, NULL,
176 CTLFLAG_PERMANENT,
177 CTLTYPE_NODE, "ip",
178 SYSCTL_DESCR("IPv4 related settings"),
179 NULL, 0, NULL, 0,
180 CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
181
182 sysctl_createv(&ip_reass_sysctllog, 0, NULL, NULL,
183 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
184 CTLTYPE_INT, "maxfragpackets",
185 SYSCTL_DESCR("Maximum number of fragments to retain for "
186 "possible reassembly"),
187 NULL, 0, &ip_maxfragpackets, 0,
188 CTL_NET, PF_INET, IPPROTO_IP, IPCTL_MAXFRAGPACKETS, CTL_EOL);
189 }
190
191 #define CHECK_NMBCLUSTER_PARAMS() \
192 do { \
193 if (__predict_false(ip_nmbclusters != nmbclusters)) \
194 ip_nmbclusters_changed(); \
195 } while (/*CONSTCOND*/0)
196
197 /*
198 * Compute IP limits derived from the value of nmbclusters.
199 */
200 static void
201 ip_nmbclusters_changed(void)
202 {
203 ip_maxfrags = nmbclusters / 4;
204 ip_nmbclusters = nmbclusters;
205 }
206
207 /*
208 * ip_reass:
209 *
210 * Take incoming datagram fragment and try to reassemble it into whole
211 * datagram. If a chain for reassembly of this datagram already exists,
212 * then it is given as 'fp'; otherwise have to make a chain.
213 */
214 struct mbuf *
215 ip_reass(ipfr_qent_t *ipqe, ipfr_queue_t *fp, const u_int hash)
216 {
217 struct ip *ip = ipqe->ipqe_ip, *qip;
218 const int hlen = ip->ip_hl << 2;
219 struct mbuf *m = ipqe->ipqe_m, *t;
220 ipfr_qent_t *nq, *p, *q;
221 int i, next;
222
223 KASSERT(mutex_owned(&ipfr_lock));
224
225 /*
226 * Presence of header sizes in mbufs would confuse code below.
227 */
228 m->m_data += hlen;
229 m->m_len -= hlen;
230
231 #ifdef notyet
232 /* Make sure fragment limit is up-to-date. */
233 CHECK_NMBCLUSTER_PARAMS();
234
235 /* If we have too many fragments, drop the older half. */
236 if (ip_nfrags >= ip_maxfrags) {
237 ip_reass_drophalf(void);
238 }
239 #endif
240
241 /*
242 * We are about to add a fragment; increment frag count.
243 */
244 ip_nfrags++;
245
246 /*
247 * If first fragment to arrive, create a reassembly queue.
248 */
249 if (fp == NULL) {
250 /*
251 * Enforce upper bound on number of fragmented packets
252 * for which we attempt reassembly: a) if maxfrag is 0,
253 * never accept fragments b) if maxfrag is -1, accept
254 * all fragments without limitation.
255 */
256 if (ip_maxfragpackets < 0)
257 ;
258 else if (ip_nfragpackets >= ip_maxfragpackets) {
259 goto dropfrag;
260 }
261 fp = malloc(sizeof(ipfr_queue_t), M_FTABLE, M_NOWAIT);
262 if (fp == NULL) {
263 goto dropfrag;
264 }
265 ip_nfragpackets++;
266 TAILQ_INIT(&fp->ipq_fragq);
267 fp->ipq_nfrags = 1;
268 fp->ipq_ttl = IPFRAGTTL;
269 fp->ipq_p = ip->ip_p;
270 fp->ipq_id = ip->ip_id;
271 fp->ipq_tos = ip->ip_tos;
272 fp->ipq_src = ip->ip_src;
273 fp->ipq_dst = ip->ip_dst;
274 LIST_INSERT_HEAD(&ip_frags[hash], fp, ipq_q);
275 p = NULL;
276 goto insert;
277 } else {
278 fp->ipq_nfrags++;
279 }
280
281 /*
282 * Find a segment which begins after this one does.
283 */
284 TAILQ_FOREACH(q, &fp->ipq_fragq, ipqe_q) {
285 if (ntohs(q->ipqe_ip->ip_off) > ntohs(ip->ip_off))
286 break;
287 }
288 if (q != NULL) {
289 p = TAILQ_PREV(q, ipfr_qent_head, ipqe_q);
290 } else {
291 p = TAILQ_LAST(&fp->ipq_fragq, ipfr_qent_head);
292 }
293
294 /*
295 * If there is a preceding segment, it may provide some of our
296 * data already. If so, drop the data from the incoming segment.
297 * If it provides all of our data, drop us.
298 */
299 if (p != NULL) {
300 i = ntohs(p->ipqe_ip->ip_off) + ntohs(p->ipqe_ip->ip_len) -
301 ntohs(ip->ip_off);
302 if (i > 0) {
303 if (i >= ntohs(ip->ip_len)) {
304 goto dropfrag;
305 }
306 m_adj(ipqe->ipqe_m, i);
307 ip->ip_off = htons(ntohs(ip->ip_off) + i);
308 ip->ip_len = htons(ntohs(ip->ip_len) - i);
309 }
310 }
311
312 /*
313 * While we overlap succeeding segments trim them or, if they are
314 * completely covered, dequeue them.
315 */
316 while (q != NULL) {
317 size_t end;
318
319 qip = q->ipqe_ip;
320 end = ntohs(ip->ip_off) + ntohs(ip->ip_len);
321 if (end <= ntohs(qip->ip_off)) {
322 break;
323 }
324 i = end - ntohs(qip->ip_off);
325 if (i < ntohs(qip->ip_len)) {
326 qip->ip_len = htons(ntohs(qip->ip_len) - i);
327 qip->ip_off = htons(ntohs(qip->ip_off) + i);
328 m_adj(q->ipqe_m, i);
329 break;
330 }
331 nq = TAILQ_NEXT(q, ipqe_q);
332 m_freem(q->ipqe_m);
333 TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
334 pool_cache_put(ipfren_cache, q);
335 fp->ipq_nfrags--;
336 ip_nfrags--;
337 q = nq;
338 }
339
340 insert:
341 /*
342 * Stick new segment in its place; check for complete reassembly.
343 */
344 if (p == NULL) {
345 TAILQ_INSERT_HEAD(&fp->ipq_fragq, ipqe, ipqe_q);
346 } else {
347 TAILQ_INSERT_AFTER(&fp->ipq_fragq, p, ipqe, ipqe_q);
348 }
349 next = 0;
350 TAILQ_FOREACH(q, &fp->ipq_fragq, ipqe_q) {
351 qip = q->ipqe_ip;
352 if (ntohs(qip->ip_off) != next) {
353 mutex_exit(&ipfr_lock);
354 return NULL;
355 }
356 next += ntohs(qip->ip_len);
357 }
358 p = TAILQ_LAST(&fp->ipq_fragq, ipfr_qent_head);
359 if (p->ipqe_mff) {
360 mutex_exit(&ipfr_lock);
361 return NULL;
362 }
363
364 /*
365 * Reassembly is complete. Check for a bogus message size.
366 */
367 q = TAILQ_FIRST(&fp->ipq_fragq);
368 ip = q->ipqe_ip;
369 if ((next + (ip->ip_hl << 2)) > IP_MAXPACKET) {
370 IP_STATINC(IP_STAT_TOOLONG);
371 ip_freef(fp);
372 mutex_exit(&ipfr_lock);
373 return NULL;
374 }
375 LIST_REMOVE(fp, ipq_q);
376 ip_nfrags -= fp->ipq_nfrags;
377 ip_nfragpackets--;
378 mutex_exit(&ipfr_lock);
379
380 /* Concatenate all fragments. */
381 m = q->ipqe_m;
382 t = m->m_next;
383 m->m_next = NULL;
384 m_cat(m, t);
385 nq = TAILQ_NEXT(q, ipqe_q);
386 pool_cache_put(ipfren_cache, q);
387
388 for (q = nq; q != NULL; q = nq) {
389 t = q->ipqe_m;
390 nq = TAILQ_NEXT(q, ipqe_q);
391 pool_cache_put(ipfren_cache, q);
392 m_cat(m, t);
393 }
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 free(fp, M_FTABLE);
404
405 m->m_len += (ip->ip_hl << 2);
406 m->m_data -= (ip->ip_hl << 2);
407
408 /* Fix up mbuf. XXX This should be done elsewhere. */
409 if (m->m_flags & M_PKTHDR) {
410 int plen = 0;
411 for (t = m; t; t = t->m_next) {
412 plen += t->m_len;
413 }
414 m->m_pkthdr.len = plen;
415 m->m_pkthdr.csum_flags = 0;
416 }
417 return m;
418
419 dropfrag:
420 if (fp != NULL) {
421 fp->ipq_nfrags--;
422 }
423 ip_nfrags--;
424 IP_STATINC(IP_STAT_FRAGDROPPED);
425 mutex_exit(&ipfr_lock);
426
427 pool_cache_put(ipfren_cache, ipqe);
428 m_freem(m);
429 return NULL;
430 }
431
432 /*
433 * ip_freef:
434 *
435 * Free a fragment reassembly header and all associated datagrams.
436 */
437 static void
438 ip_freef(ipfr_queue_t *fp)
439 {
440 ipfr_qent_t *q;
441
442 KASSERT(mutex_owned(&ipfr_lock));
443
444 LIST_REMOVE(fp, ipq_q);
445 ip_nfrags -= fp->ipq_nfrags;
446 ip_nfragpackets--;
447
448 while ((q = TAILQ_FIRST(&fp->ipq_fragq)) != NULL) {
449 TAILQ_REMOVE(&fp->ipq_fragq, q, ipqe_q);
450 m_freem(q->ipqe_m);
451 pool_cache_put(ipfren_cache, q);
452 }
453 free(fp, M_FTABLE);
454 }
455
456 /*
457 * ip_reass_ttl_decr:
458 *
459 * Decrement TTL of all reasembly queue entries by `ticks'. Count
460 * number of distinct fragments (as opposed to partial, fragmented
461 * datagrams) inthe reassembly queue. While we traverse the entire
462 * reassembly queue, compute and return the median TTL over all
463 * fragments.
464 */
465 static u_int
466 ip_reass_ttl_decr(u_int ticks)
467 {
468 u_int nfrags, median, dropfraction, keepfraction;
469 ipfr_queue_t *fp, *nfp;
470 int i;
471
472 nfrags = 0;
473 memset(fragttl_histo, 0, sizeof(fragttl_histo));
474
475 for (i = 0; i < IPREASS_HASH_SIZE; i++) {
476 for (fp = LIST_FIRST(&ip_frags[i]); fp != NULL; fp = nfp) {
477 fp->ipq_ttl = ((fp->ipq_ttl <= ticks) ?
478 0 : fp->ipq_ttl - ticks);
479 nfp = LIST_NEXT(fp, ipq_q);
480 if (fp->ipq_ttl == 0) {
481 IP_STATINC(IP_STAT_FRAGTIMEOUT);
482 ip_freef(fp);
483 } else {
484 nfrags += fp->ipq_nfrags;
485 fragttl_histo[fp->ipq_ttl] += fp->ipq_nfrags;
486 }
487 }
488 }
489
490 KASSERT(ip_nfrags == nfrags);
491
492 /* Find median (or other drop fraction) in histogram. */
493 dropfraction = (ip_nfrags / 2);
494 keepfraction = ip_nfrags - dropfraction;
495 for (i = IPFRAGTTL, median = 0; i >= 0; i--) {
496 median += fragttl_histo[i];
497 if (median >= keepfraction)
498 break;
499 }
500
501 /* Return TTL of median (or other fraction). */
502 return (u_int)i;
503 }
504
505 static void
506 ip_reass_drophalf(void)
507 {
508 u_int median_ticks;
509
510 KASSERT(mutex_owned(&ipfr_lock));
511
512 /*
513 * Compute median TTL of all fragments, and count frags
514 * with that TTL or lower (roughly half of all fragments).
515 */
516 median_ticks = ip_reass_ttl_decr(0);
517
518 /* Drop half. */
519 median_ticks = ip_reass_ttl_decr(median_ticks);
520 }
521
522 /*
523 * ip_reass_drain: drain off all datagram fragments. Do not acquire
524 * softnet_lock as can be called from hardware interrupt context.
525 */
526 void
527 ip_reass_drain(void)
528 {
529
530 /*
531 * We may be called from a device's interrupt context. If
532 * the ipq is already busy, just bail out now.
533 */
534 if (mutex_tryenter(&ipfr_lock)) {
535 /*
536 * Drop half the total fragments now. If more mbufs are
537 * needed, we will be called again soon.
538 */
539 ip_reass_drophalf();
540 mutex_exit(&ipfr_lock);
541 }
542 }
543
544 /*
545 * ip_reass_slowtimo:
546 *
547 * If a timer expires on a reassembly queue, discard it.
548 */
549 void
550 ip_reass_slowtimo(void)
551 {
552 static u_int dropscanidx = 0;
553 u_int i, median_ttl;
554
555 mutex_enter(&ipfr_lock);
556
557 /* Age TTL of all fragments by 1 tick .*/
558 median_ttl = ip_reass_ttl_decr(1);
559
560 /* Make sure fragment limit is up-to-date. */
561 CHECK_NMBCLUSTER_PARAMS();
562
563 /* If we have too many fragments, drop the older half. */
564 if (ip_nfrags > ip_maxfrags) {
565 ip_reass_ttl_decr(median_ttl);
566 }
567
568 /*
569 * If we are over the maximum number of fragmented packets (due to
570 * the limit being lowered), drain off enough to get down to the
571 * new limit. Start draining from the reassembly hashqueue most
572 * recently drained.
573 */
574 if (ip_maxfragpackets < 0)
575 ;
576 else {
577 int wrapped = 0;
578
579 i = dropscanidx;
580 while (ip_nfragpackets > ip_maxfragpackets && wrapped == 0) {
581 while (LIST_FIRST(&ip_frags[i]) != NULL) {
582 ip_freef(LIST_FIRST(&ip_frags[i]));
583 }
584 if (++i >= IPREASS_HASH_SIZE) {
585 i = 0;
586 }
587 /*
588 * Do not scan forever even if fragment counters are
589 * wrong: stop after scanning entire reassembly queue.
590 */
591 if (i == dropscanidx) {
592 wrapped = 1;
593 }
594 }
595 dropscanidx = i;
596 }
597 mutex_exit(&ipfr_lock);
598 }
599
600 /*
601 * ip_reass_packet: generic routine to perform IP reassembly.
602 *
603 * => Passed fragment should have IP_MF flag and/or offset set.
604 * => Fragment should not have other than IP_MF flags set.
605 *
606 * => Returns 0 on success or error otherwise.
607 * => On complete, m0 represents a constructed final packet.
608 */
609 int
610 ip_reass_packet(struct mbuf **m0, struct ip *ip)
611 {
612 const int hlen = ip->ip_hl << 2;
613 const int len = ntohs(ip->ip_len);
614 struct mbuf *m = *m0;
615 ipfr_queue_t *fp;
616 ipfr_qent_t *ipqe;
617 u_int hash, off, flen;
618 bool mff;
619
620 /*
621 * Prevent TCP blind data attacks by not allowing non-initial
622 * fragments to start at less than 68 bytes (minimal fragment
623 * size) and making sure the first fragment is at least 68
624 * bytes.
625 */
626 off = (ntohs(ip->ip_off) & IP_OFFMASK) << 3;
627 if ((off > 0 ? off + hlen : len) < IP_MINFRAGSIZE - 1) {
628 IP_STATINC(IP_STAT_BADFRAGS);
629 return EINVAL;
630 }
631
632 if (off + len > IP_MAXPACKET) {
633 IP_STATINC(IP_STAT_TOOLONG);
634 return EINVAL;
635 }
636
637 /*
638 * Fragment length and MF flag. Make sure that fragments have
639 * a data length which is non-zero and multiple of 8 bytes.
640 */
641 flen = ntohs(ip->ip_len) - hlen;
642 mff = (ip->ip_off & htons(IP_MF)) != 0;
643 if (mff && (flen == 0 || (flen & 0x7) != 0)) {
644 IP_STATINC(IP_STAT_BADFRAGS);
645 return EINVAL;
646 }
647
648 /*
649 * Adjust total IP length to not reflect header and convert
650 * offset of this to bytes. XXX: clobbers struct ip.
651 */
652 ip->ip_len = htons(flen);
653 ip->ip_off = htons(off);
654
655 /* Look for queue of fragments of this datagram. */
656 mutex_enter(&ipfr_lock);
657 hash = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id);
658 LIST_FOREACH(fp, &ip_frags[hash], ipq_q) {
659 if (ip->ip_id != fp->ipq_id)
660 continue;
661 if (!in_hosteq(ip->ip_src, fp->ipq_src))
662 continue;
663 if (!in_hosteq(ip->ip_dst, fp->ipq_dst))
664 continue;
665 if (ip->ip_p != fp->ipq_p)
666 continue;
667 break;
668 }
669
670 /* Make sure that TOS matches previous fragments. */
671 if (fp && fp->ipq_tos != ip->ip_tos) {
672 IP_STATINC(IP_STAT_BADFRAGS);
673 mutex_exit(&ipfr_lock);
674 return EINVAL;
675 }
676
677 /*
678 * Create new entry and attempt to reassembly.
679 */
680 IP_STATINC(IP_STAT_FRAGMENTS);
681 ipqe = pool_cache_get(ipfren_cache, PR_NOWAIT);
682 if (ipqe == NULL) {
683 IP_STATINC(IP_STAT_RCVMEMDROP);
684 mutex_exit(&ipfr_lock);
685 return ENOMEM;
686 }
687 ipqe->ipqe_mff = mff;
688 ipqe->ipqe_m = m;
689 ipqe->ipqe_ip = ip;
690
691 *m0 = ip_reass(ipqe, fp, hash);
692 if (*m0) {
693 /* Note that finally reassembled. */
694 IP_STATINC(IP_STAT_REASSEMBLED);
695 }
696 return 0;
697 }
698