1 /* $FreeBSD: src/sys/netinet6/frag6.c,v 1.2.2.6 2002/04/28 05:40:26 suz Exp $ */
2 /* $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $ */
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
37 #include <sys/domain.h>
38 #include <sys/protosw.h>
39 #include <sys/socket.h>
40 #include <sys/errno.h>
42 #include <sys/kernel.h>
43 #include <sys/syslog.h>
44 #include <sys/thread2.h>
47 #include <net/route.h>
48 #include <net/netisr2.h>
49 #include <net/netmsg2.h>
51 #include <netinet/in.h>
52 #include <netinet/in_var.h>
53 #include <netinet/ip6.h>
54 #include <netinet6/ip6_var.h>
55 #include <netinet/icmp6.h>
57 #include <net/net_osdep.h>
59 #define FRAG6_SLOWTIMO (hz / 2)
62 * Define it to get a correct behavior on per-interface statistics.
63 * You will need to perform an extra routing table lookup, per fragment,
64 * to do it. This may, or may not be, a performance hit.
66 #define IN6_IFSTAT_STRICT
68 static void frag6_enq (struct ip6asfrag *, struct ip6asfrag *);
69 static void frag6_deq (struct ip6asfrag *);
70 static void frag6_insque (struct ip6q *, struct ip6q *);
71 static void frag6_remque (struct ip6q *);
72 static void frag6_freef (struct ip6q *);
73 static void frag6_slowtimo_dispatch (netmsg_t);
74 static void frag6_slowtimo (void *);
75 static void frag6_drain_dispatch (netmsg_t);
77 /* XXX we eventually need splreass6, or some real semaphore */
78 int frag6_doing_reass;
79 u_int frag6_nfragpackets;
81 struct ip6q ip6q; /* ip6 reassemble queue */
84 MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
86 static struct callout frag6_slowtimo_ch;
87 static struct netmsg_base frag6_slowtimo_nmsg;
88 static struct netmsg_base frag6_drain_nmsg;
91 * Initialise reassembly queue and fragment identifier.
98 ip6_maxfragpackets = nmbclusters / 4;
99 ip6_maxfrags = nmbclusters / 4;
102 * in many cases, random() here does NOT return random number
103 * as initialization during bootstrap time occur in fixed order.
106 ip6_id = krandom() ^ tv.tv_usec;
107 ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
109 netmsg_init(&frag6_drain_nmsg, NULL, &netisr_adone_rport,
110 MSGF_PRIORITY, frag6_drain_dispatch);
112 callout_init_mp(&frag6_slowtimo_ch);
113 netmsg_init(&frag6_slowtimo_nmsg, NULL, &netisr_adone_rport,
114 MSGF_PRIORITY, frag6_slowtimo_dispatch);
116 callout_reset_bycpu(&frag6_slowtimo_ch, FRAG6_SLOWTIMO,
117 frag6_slowtimo, NULL, 0);
121 * In RFC2460, fragment and reassembly rule do not agree with each other,
122 * in terms of next header field handling in fragment header.
123 * While the sender will use the same value for all of the fragmented packets,
124 * receiver is suggested not to check the consistency.
126 * fragment rule (p20):
127 * (2) A Fragment header containing:
128 * The Next Header value that identifies the first header of
129 * the Fragmentable Part of the original packet.
130 * -> next header field is same for all fragments
132 * reassembly rule (p21):
133 * The Next Header field of the last header of the Unfragmentable
134 * Part is obtained from the Next Header field of the first
135 * fragment's Fragment header.
136 * -> should grab it from the first fragment only
138 * The following note also contradicts with fragment rule - noone is going to
139 * send different fragment with different next header field.
141 * additional note (p22):
142 * The Next Header values in the Fragment headers of different
143 * fragments of the same original packet may differ. Only the value
144 * from the Offset zero fragment packet is used for reassembly.
145 * -> should grab it from the first fragment only
147 * There is no explicit reason given in the RFC. Historical reason maybe?
153 frag6_input(struct mbuf **mp, int *offp, int proto)
155 struct mbuf *m = *mp, *t;
157 struct ip6_frag *ip6f;
159 struct ip6asfrag *af6, *ip6af, *af6dwn;
160 int offset = *offp, nxt, i, next;
162 int fragoff, frgpartlen; /* must be larger than u_int16_t */
163 struct ifnet *dstifp;
164 #ifdef IN6_IFSTAT_STRICT
165 static struct route_in6 ro;
166 struct sockaddr_in6 *dst;
169 ip6 = mtod(m, struct ip6_hdr *);
170 #ifndef PULLDOWN_TEST
171 IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
172 ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
174 IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
180 #ifdef IN6_IFSTAT_STRICT
181 /* find the destination interface of the packet. */
182 dst = (struct sockaddr_in6 *)&ro.ro_dst;
184 (!(ro.ro_rt->rt_flags & RTF_UP) ||
185 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
189 if (ro.ro_rt == NULL) {
190 bzero(dst, sizeof(*dst));
191 dst->sin6_family = AF_INET6;
192 dst->sin6_len = sizeof(struct sockaddr_in6);
193 dst->sin6_addr = ip6->ip6_dst;
195 rtalloc((struct route *)&ro);
196 if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
197 dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
199 /* we are violating the spec, this is not the destination interface */
200 if (m->m_flags & M_PKTHDR)
201 dstifp = m->m_pkthdr.rcvif;
204 /* jumbo payload can't contain a fragment header */
205 if (ip6->ip6_plen == 0) {
206 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
207 in6_ifstat_inc(dstifp, ifs6_reass_fail);
212 * check whether fragment packet's fragment length is
213 * multiple of 8 octets.
214 * sizeof(struct ip6_frag) == 8
215 * sizeof(struct ip6_hdr) = 40
217 if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
218 (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
219 icmp6_error(m, ICMP6_PARAM_PROB,
220 ICMP6_PARAMPROB_HEADER,
221 offsetof(struct ip6_hdr, ip6_plen));
222 in6_ifstat_inc(dstifp, ifs6_reass_fail);
226 ip6stat.ip6s_fragments++;
227 in6_ifstat_inc(dstifp, ifs6_reass_reqd);
229 /* offset now points to data portion */
230 offset += sizeof(struct ip6_frag);
232 frag6_doing_reass = 1;
235 * Enforce upper bound on number of fragments.
236 * If maxfrag is 0, never accept fragments.
237 * If maxfrag is -1, accept all fragments without limitation.
239 if (ip6_maxfrags < 0)
241 else if (frag6_nfrags >= (u_int)ip6_maxfrags)
244 for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
245 if (ip6f->ip6f_ident == q6->ip6q_ident &&
246 IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
247 IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
252 * the first fragment to arrive, create a reassembly queue.
257 * Enforce upper bound on number of fragmented packets
258 * for which we attempt reassembly;
259 * If maxfrag is 0, never accept fragments.
260 * If maxfrag is -1, accept all fragments without limitation.
262 if (ip6_maxfragpackets < 0)
264 else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
266 frag6_nfragpackets++;
267 q6 = (struct ip6q *)kmalloc(sizeof(struct ip6q), M_FTABLE,
272 frag6_insque(q6, &ip6q);
274 /* ip6q_nxt will be filled afterwards, from 1st fragment */
275 q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
277 q6->ip6q_nxtp = (u_char *)nxtp;
279 q6->ip6q_ident = ip6f->ip6f_ident;
280 q6->ip6q_arrive = 0; /* Is it used anywhere? */
281 q6->ip6q_ttl = IPV6_FRAGTTL;
282 q6->ip6q_src = ip6->ip6_src;
283 q6->ip6q_dst = ip6->ip6_dst;
284 q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
289 * If it's the 1st fragment, record the length of the
290 * unfragmentable part and the next header of the fragment header.
292 fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
294 q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr)
295 - sizeof(struct ip6_frag);
296 q6->ip6q_nxt = ip6f->ip6f_nxt;
300 * Check that the reassembled packet would not exceed 65535 bytes
302 * If it would exceed, discard the fragment and return an ICMP error.
304 frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
305 if (q6->ip6q_unfrglen >= 0) {
306 /* The 1st fragment has already arrived. */
307 if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
308 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
309 offset - sizeof(struct ip6_frag) +
310 offsetof(struct ip6_frag, ip6f_offlg));
311 frag6_doing_reass = 0;
312 return (IPPROTO_DONE);
315 else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
316 icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
317 offset - sizeof(struct ip6_frag) +
318 offsetof(struct ip6_frag, ip6f_offlg));
319 frag6_doing_reass = 0;
320 return (IPPROTO_DONE);
323 * If it's the first fragment, do the above check for each
324 * fragment already stored in the reassembly queue.
327 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
329 af6dwn = af6->ip6af_down;
331 if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
333 struct mbuf *merr = IP6_REASS_MBUF(af6);
334 struct ip6_hdr *ip6err;
335 int erroff = af6->ip6af_offset;
337 /* dequeue the fragment. */
339 kfree(af6, M_FTABLE);
341 /* adjust pointer. */
342 ip6err = mtod(merr, struct ip6_hdr *);
345 * Restore source and destination addresses
346 * in the erroneous IPv6 header.
348 ip6err->ip6_src = q6->ip6q_src;
349 ip6err->ip6_dst = q6->ip6q_dst;
351 icmp6_error(merr, ICMP6_PARAM_PROB,
352 ICMP6_PARAMPROB_HEADER,
353 erroff - sizeof(struct ip6_frag) +
354 offsetof(struct ip6_frag, ip6f_offlg));
359 ip6af = (struct ip6asfrag *)kmalloc(sizeof(struct ip6asfrag), M_FTABLE,
363 ip6af->ip6af_head = ip6->ip6_flow;
364 ip6af->ip6af_len = ip6->ip6_plen;
365 ip6af->ip6af_nxt = ip6->ip6_nxt;
366 ip6af->ip6af_hlim = ip6->ip6_hlim;
367 ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
368 ip6af->ip6af_off = fragoff;
369 ip6af->ip6af_frglen = frgpartlen;
370 ip6af->ip6af_offset = offset;
371 IP6_REASS_MBUF(ip6af) = m;
374 af6 = (struct ip6asfrag *)q6;
379 * Find a segment which begins after this one does.
381 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
382 af6 = af6->ip6af_down)
383 if (af6->ip6af_off > ip6af->ip6af_off)
387 * RFC 5722: Drop overlapping fragments
389 if (af6->ip6af_up != (struct ip6asfrag *)q6) {
390 i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
393 kfree(ip6af, M_FTABLE);
397 if (af6 != (struct ip6asfrag *)q6) {
398 i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
400 kfree(ip6af, M_FTABLE);
408 * Stick new segment in its place;
409 * check for complete reassembly.
410 * Move to front of packet queue, as we are
411 * the most recently active fragmented packet.
413 frag6_enq(ip6af, af6->ip6af_up);
417 if (q6 != ip6q.ip6q_next) {
419 frag6_insque(q6, &ip6q);
423 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
424 af6 = af6->ip6af_down) {
425 if (af6->ip6af_off != next) {
426 frag6_doing_reass = 0;
429 next += af6->ip6af_frglen;
431 if (af6->ip6af_up->ip6af_mff) {
432 frag6_doing_reass = 0;
437 * Reassembly is complete; concatenate fragments.
439 ip6af = q6->ip6q_down;
440 t = m = IP6_REASS_MBUF(ip6af);
441 af6 = ip6af->ip6af_down;
443 while (af6 != (struct ip6asfrag *)q6) {
444 af6dwn = af6->ip6af_down;
448 t->m_next = IP6_REASS_MBUF(af6);
449 m_adj(t->m_next, af6->ip6af_offset);
450 kfree(af6, M_FTABLE);
454 /* adjust offset to point where the original next header starts */
455 offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
456 kfree(ip6af, M_FTABLE);
457 ip6 = mtod(m, struct ip6_hdr *);
458 ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
459 ip6->ip6_src = q6->ip6q_src;
460 ip6->ip6_dst = q6->ip6q_dst;
463 *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
467 * Delete frag6 header with as a few cost as possible.
469 if (offset < m->m_len) {
470 bcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
472 m->m_data += sizeof(struct ip6_frag);
473 m->m_len -= sizeof(struct ip6_frag);
475 /* this comes with no copy if the boundary is on cluster */
476 if ((t = m_split(m, offset, M_NOWAIT)) == NULL) {
478 frag6_nfrags -= q6->ip6q_nfrag;
480 frag6_nfragpackets--;
483 m_adj(t, sizeof(struct ip6_frag));
488 * Store NXT to the original.
491 char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
496 frag6_nfrags -= q6->ip6q_nfrag;
498 frag6_nfragpackets--;
500 if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
502 for (t = m; t; t = t->m_next)
504 m->m_pkthdr.len = plen;
507 ip6stat.ip6s_reassembled++;
508 in6_ifstat_inc(dstifp, ifs6_reass_ok);
511 * Reassembly complete, return the next protocol.
512 * Be sure to clear M_HASH to force the packet
513 * to be re-characterized.
515 m->m_flags &= ~M_HASH;
520 frag6_doing_reass = 0;
524 in6_ifstat_inc(dstifp, ifs6_reass_fail);
525 ip6stat.ip6s_fragdropped++;
527 frag6_doing_reass = 0;
532 * Free a fragment reassembly header and all
533 * associated datagrams.
536 frag6_freef(struct ip6q *q6)
538 struct ip6asfrag *af6, *down6;
540 for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
542 struct mbuf *m = IP6_REASS_MBUF(af6);
544 down6 = af6->ip6af_down;
548 * Return ICMP time exceeded error for the 1st fragment.
549 * Just free other fragments.
551 if (af6->ip6af_off == 0) {
555 ip6 = mtod(m, struct ip6_hdr *);
557 /* restoure source and destination addresses */
558 ip6->ip6_src = q6->ip6q_src;
559 ip6->ip6_dst = q6->ip6q_dst;
561 icmp6_error(m, ICMP6_TIME_EXCEEDED,
562 ICMP6_TIME_EXCEED_REASSEMBLY, 0);
565 kfree(af6, M_FTABLE);
568 frag6_nfrags -= q6->ip6q_nfrag;
570 frag6_nfragpackets--;
574 * Put an ip fragment on a reassembly chain.
575 * Like insque, but pointers in middle of structure.
578 frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
581 af6->ip6af_down = up6->ip6af_down;
582 up6->ip6af_down->ip6af_up = af6;
583 up6->ip6af_down = af6;
587 * To frag6_enq as remque is to insque.
590 frag6_deq(struct ip6asfrag *af6)
592 af6->ip6af_up->ip6af_down = af6->ip6af_down;
593 af6->ip6af_down->ip6af_up = af6->ip6af_up;
597 frag6_insque(struct ip6q *new, struct ip6q *old)
599 new->ip6q_prev = old;
600 new->ip6q_next = old->ip6q_next;
601 old->ip6q_next->ip6q_prev= new;
602 old->ip6q_next = new;
606 frag6_remque(struct ip6q *p6)
608 p6->ip6q_prev->ip6q_next = p6->ip6q_next;
609 p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
613 * IPv6 reassembling timer processing;
614 * if a timer expires on a reassembly
618 frag6_slowtimo_dispatch(netmsg_t nmsg)
622 ASSERT_NETISR_NCPUS(curthread, 0);
626 netisr_replymsg(&nmsg->base, 0);
629 frag6_doing_reass = 1;
632 while (q6 != &ip6q) {
635 if (q6->ip6q_prev->ip6q_ttl == 0) {
636 ip6stat.ip6s_fragtimeout++;
637 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
638 frag6_freef(q6->ip6q_prev);
642 * If we are over the maximum number of fragments
643 * (due to the limit being lowered), drain off
644 * enough to get down to the new limit.
646 while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
648 ip6stat.ip6s_fragoverflow++;
649 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
650 frag6_freef(ip6q.ip6q_prev);
652 frag6_doing_reass = 0;
656 * Routing changes might produce a better route than we last used;
657 * make sure we notice eventually, even if forwarding only for one
658 * destination and the cache is never replaced.
660 if (ip6_forward_rt.ro_rt) {
661 RTFREE(ip6_forward_rt.ro_rt);
662 ip6_forward_rt.ro_rt = NULL;
664 if (ipsrcchk_rt.ro_rt) {
665 RTFREE(ipsrcchk_rt.ro_rt);
666 ipsrcchk_rt.ro_rt = NULL;
669 callout_reset(&frag6_slowtimo_ch, FRAG6_SLOWTIMO, frag6_slowtimo, NULL);
673 frag6_slowtimo(void *dummy __unused)
675 struct netmsg_base *nmsg = &frag6_slowtimo_nmsg;
677 KKASSERT(mycpuid == 0);
680 if (nmsg->lmsg.ms_flags & MSGF_DONE)
681 netisr_sendmsg_oncpu(nmsg);
686 * Drain off all datagram fragments.
689 frag6_drain_oncpu(void)
692 ASSERT_NETISR_NCPUS(curthread, 0);
694 if (frag6_doing_reass)
696 while (ip6q.ip6q_next != &ip6q) {
697 ip6stat.ip6s_fragdropped++;
698 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
699 frag6_freef(ip6q.ip6q_next);
704 frag6_drain_dispatch(netmsg_t nmsg)
707 ASSERT_NETISR_NCPUS(curthread, 0);
710 netisr_replymsg(&nmsg->base, 0);
717 frag6_drain_ipi(void *dummy __unused)
719 struct netmsg_base *nmsg = &frag6_drain_nmsg;
721 KKASSERT(mycpuid == 0);
724 if (nmsg->lmsg.ms_flags & MSGF_DONE)
725 netisr_sendmsg_oncpu(nmsg);
733 if (IS_NETISR(curthread, 0)) {
739 lwkt_send_ipiq_bycpu(0, frag6_drain_ipi, NULL);