1 /* $FreeBSD: src/sys/netinet6/frag6.c,v 1.2.2.5 2001/07/03 11:01:50 ume Exp $ */
2 /* $KAME: frag6.c,v 1.31 2001/05/17 13:45:34 jinmei 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 <kern/queue.h>
45 #include <kern/locks.h>
48 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_var.h>
52 #include <netinet/ip6.h>
53 #include <netinet6/ip6_var.h>
54 #include <netinet/icmp6.h>
56 #include <net/net_osdep.h>
59 * Define it to get a correct behavior on per-interface statistics.
60 * You will need to perform an extra routing table lookup, per fragment,
61 * to do it. This may, or may not be, a performance hit.
63 #define IN6_IFSTAT_STRICT
65 static void frag6_enq(struct ip6asfrag
*, struct ip6asfrag
*);
66 static void frag6_deq(struct ip6asfrag
*);
67 static void frag6_insque(struct ip6q
*, struct ip6q
*);
68 static void frag6_remque(struct ip6q
*);
69 static void frag6_freef(struct ip6q
*);
71 /* XXX we eventually need splreass6, or some real semaphore */
72 int frag6_doing_reass
;
73 u_int frag6_nfragpackets
;
74 static u_int frag6_nfrags
;
75 struct ip6q ip6q
; /* ip6 reassemble queue */
78 MALLOC_DEFINE(M_FTABLE
, "fragment", "fragment reassembly header");
81 extern lck_mtx_t
*inet6_domain_mutex
;
83 * Initialise reassembly queue and fragment identifier.
90 ip6_maxfragpackets
= nmbclusters
/ 32;
91 ip6_maxfrags
= nmbclusters
/ 4;
94 * in many cases, random() here does NOT return random number
95 * as initialization during bootstrap time occur in fixed order.
98 ip6_id
= random() ^ tv
.tv_usec
;
99 ip6q
.ip6q_next
= ip6q
.ip6q_prev
= &ip6q
;
103 * In RFC2460, fragment and reassembly rule do not agree with each other,
104 * in terms of next header field handling in fragment header.
105 * While the sender will use the same value for all of the fragmented packets,
106 * receiver is suggested not to check the consistency.
108 * fragment rule (p20):
109 * (2) A Fragment header containing:
110 * The Next Header value that identifies the first header of
111 * the Fragmentable Part of the original packet.
112 * -> next header field is same for all fragments
114 * reassembly rule (p21):
115 * The Next Header field of the last header of the Unfragmentable
116 * Part is obtained from the Next Header field of the first
117 * fragment's Fragment header.
118 * -> should grab it from the first fragment only
120 * The following note also contradicts with fragment rule - noone is going to
121 * send different fragment with different next header field.
123 * additional note (p22):
124 * The Next Header values in the Fragment headers of different
125 * fragments of the same original packet may differ. Only the value
126 * from the Offset zero fragment packet is used for reassembly.
127 * -> should grab it from the first fragment only
129 * There is no explicit reason given in the RFC. Historical reason maybe?
133 * NOTE: this function is called with the inet6_domain_mutex held from ip6_input.
134 * inet6_domain_mutex is protecting he frag6 queue manipulation.
137 frag6_input(mp
, offp
)
141 struct mbuf
*m
= *mp
, *t
;
143 struct ip6_frag
*ip6f
;
145 struct ip6asfrag
*af6
, *ip6af
, *af6dwn
;
146 int offset
= *offp
, nxt
, i
, next
;
148 int fragoff
, frgpartlen
; /* must be larger than u_int16_t */
149 struct ifnet
*dstifp
;
150 #ifdef IN6_IFSTAT_STRICT
151 static struct route_in6 ro
;
152 struct sockaddr_in6
*dst
;
155 ip6
= mtod(m
, struct ip6_hdr
*);
156 #ifndef PULLDOWN_TEST
157 IP6_EXTHDR_CHECK(m
, offset
, sizeof(struct ip6_frag
), return IPPROTO_DONE
);
158 ip6f
= (struct ip6_frag
*)((caddr_t
)ip6
+ offset
);
160 IP6_EXTHDR_GET(ip6f
, struct ip6_frag
*, m
, offset
, sizeof(*ip6f
));
166 #ifdef IN6_IFSTAT_STRICT
167 /* find the destination interface of the packet. */
168 dst
= (struct sockaddr_in6
*)&ro
.ro_dst
;
170 && ((ro
.ro_rt
->rt_flags
& RTF_UP
) == 0
171 || !IN6_ARE_ADDR_EQUAL(&dst
->sin6_addr
, &ip6
->ip6_dst
))) {
173 ro
.ro_rt
= (struct rtentry
*)0;
175 if (ro
.ro_rt
== NULL
) {
176 bzero(dst
, sizeof(*dst
));
177 dst
->sin6_family
= AF_INET6
;
178 dst
->sin6_len
= sizeof(struct sockaddr_in6
);
179 dst
->sin6_addr
= ip6
->ip6_dst
;
181 rtalloc((struct route
*)&ro
);
182 if (ro
.ro_rt
!= NULL
&& ro
.ro_rt
->rt_ifa
!= NULL
)
183 dstifp
= ((struct in6_ifaddr
*)ro
.ro_rt
->rt_ifa
)->ia_ifp
;
185 /* we are violating the spec, this is not the destination interface */
186 if ((m
->m_flags
& M_PKTHDR
) != 0)
187 dstifp
= m
->m_pkthdr
.rcvif
;
190 /* jumbo payload can't contain a fragment header */
191 if (ip6
->ip6_plen
== 0) {
192 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
, offset
);
193 in6_ifstat_inc(dstifp
, ifs6_reass_fail
);
198 * check whether fragment packet's fragment length is
199 * multiple of 8 octets.
200 * sizeof(struct ip6_frag) == 8
201 * sizeof(struct ip6_hdr) = 40
203 if ((ip6f
->ip6f_offlg
& IP6F_MORE_FRAG
) &&
204 (((ntohs(ip6
->ip6_plen
) - offset
) & 0x7) != 0)) {
205 icmp6_error(m
, ICMP6_PARAM_PROB
,
206 ICMP6_PARAMPROB_HEADER
,
207 offsetof(struct ip6_hdr
, ip6_plen
));
208 in6_ifstat_inc(dstifp
, ifs6_reass_fail
);
212 ip6stat
.ip6s_fragments
++;
213 in6_ifstat_inc(dstifp
, ifs6_reass_reqd
);
215 /* offset now points to data portion */
216 offset
+= sizeof(struct ip6_frag
);
218 frag6_doing_reass
= 1;
221 * Enforce upper bound on number of fragments.
222 * If maxfrag is 0, never accept fragments.
223 * If maxfrag is -1, accept all fragments without limitation.
225 if (ip6_maxfrags
< 0)
227 else if (frag6_nfrags
>= (u_int
)ip6_maxfrags
)
230 for (q6
= ip6q
.ip6q_next
; q6
!= &ip6q
; q6
= q6
->ip6q_next
)
231 if (ip6f
->ip6f_ident
== q6
->ip6q_ident
&&
232 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_src
, &q6
->ip6q_src
) &&
233 IN6_ARE_ADDR_EQUAL(&ip6
->ip6_dst
, &q6
->ip6q_dst
))
238 * the first fragment to arrive, create a reassembly queue.
243 * Enforce upper bound on number of fragmented packets
244 * for which we attempt reassembly;
245 * If maxfrag is 0, never accept fragments.
246 * If maxfrag is -1, accept all fragments without limitation.
248 if (ip6_maxfragpackets
< 0)
250 else if (frag6_nfragpackets
>= (u_int
)ip6_maxfragpackets
)
252 frag6_nfragpackets
++;
253 q6
= (struct ip6q
*)_MALLOC(sizeof(struct ip6q
), M_FTABLE
,
257 bzero(q6
, sizeof(*q6
));
259 frag6_insque(q6
, &ip6q
);
261 /* ip6q_nxt will be filled afterwards, from 1st fragment */
262 q6
->ip6q_down
= q6
->ip6q_up
= (struct ip6asfrag
*)q6
;
264 q6
->ip6q_nxtp
= (u_char
*)nxtp
;
266 q6
->ip6q_ident
= ip6f
->ip6f_ident
;
267 q6
->ip6q_arrive
= 0; /* Is it used anywhere? */
268 q6
->ip6q_ttl
= IPV6_FRAGTTL
;
269 q6
->ip6q_src
= ip6
->ip6_src
;
270 q6
->ip6q_dst
= ip6
->ip6_dst
;
271 q6
->ip6q_unfrglen
= -1; /* The 1st fragment has not arrived. */
277 * If it's the 1st fragment, record the length of the
278 * unfragmentable part and the next header of the fragment header.
280 fragoff
= ntohs(ip6f
->ip6f_offlg
& IP6F_OFF_MASK
);
282 q6
->ip6q_unfrglen
= offset
- sizeof(struct ip6_hdr
)
283 - sizeof(struct ip6_frag
);
284 q6
->ip6q_nxt
= ip6f
->ip6f_nxt
;
288 * Check that the reassembled packet would not exceed 65535 bytes
290 * If it would exceed, discard the fragment and return an ICMP error.
292 frgpartlen
= sizeof(struct ip6_hdr
) + ntohs(ip6
->ip6_plen
) - offset
;
293 if (q6
->ip6q_unfrglen
>= 0) {
294 /* The 1st fragment has already arrived. */
295 if (q6
->ip6q_unfrglen
+ fragoff
+ frgpartlen
> IPV6_MAXPACKET
) {
296 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
,
297 offset
- sizeof(struct ip6_frag
) +
298 offsetof(struct ip6_frag
, ip6f_offlg
));
299 frag6_doing_reass
= 0;
300 return(IPPROTO_DONE
);
303 else if (fragoff
+ frgpartlen
> IPV6_MAXPACKET
) {
304 icmp6_error(m
, ICMP6_PARAM_PROB
, ICMP6_PARAMPROB_HEADER
,
305 offset
- sizeof(struct ip6_frag
) +
306 offsetof(struct ip6_frag
, ip6f_offlg
));
307 frag6_doing_reass
= 0;
308 return(IPPROTO_DONE
);
311 * If it's the first fragment, do the above check for each
312 * fragment already stored in the reassembly queue.
315 for (af6
= q6
->ip6q_down
; af6
!= (struct ip6asfrag
*)q6
;
317 af6dwn
= af6
->ip6af_down
;
319 if (q6
->ip6q_unfrglen
+ af6
->ip6af_off
+ af6
->ip6af_frglen
>
321 struct mbuf
*merr
= IP6_REASS_MBUF(af6
);
322 struct ip6_hdr
*ip6err
;
323 int erroff
= af6
->ip6af_offset
;
325 /* dequeue the fragment. */
329 /* adjust pointer. */
330 ip6err
= mtod(merr
, struct ip6_hdr
*);
333 * Restore source and destination addresses
334 * in the erroneous IPv6 header.
336 ip6err
->ip6_src
= q6
->ip6q_src
;
337 ip6err
->ip6_dst
= q6
->ip6q_dst
;
339 icmp6_error(merr
, ICMP6_PARAM_PROB
,
340 ICMP6_PARAMPROB_HEADER
,
341 erroff
- sizeof(struct ip6_frag
) +
342 offsetof(struct ip6_frag
, ip6f_offlg
));
347 ip6af
= (struct ip6asfrag
*)_MALLOC(sizeof(struct ip6asfrag
), M_FTABLE
,
351 bzero(ip6af
, sizeof(*ip6af
));
352 ip6af
->ip6af_head
= ip6
->ip6_flow
;
353 ip6af
->ip6af_len
= ip6
->ip6_plen
;
354 ip6af
->ip6af_nxt
= ip6
->ip6_nxt
;
355 ip6af
->ip6af_hlim
= ip6
->ip6_hlim
;
356 ip6af
->ip6af_mff
= ip6f
->ip6f_offlg
& IP6F_MORE_FRAG
;
357 ip6af
->ip6af_off
= fragoff
;
358 ip6af
->ip6af_frglen
= frgpartlen
;
359 ip6af
->ip6af_offset
= offset
;
360 IP6_REASS_MBUF(ip6af
) = m
;
363 af6
= (struct ip6asfrag
*)q6
;
368 * Find a segment which begins after this one does.
370 for (af6
= q6
->ip6q_down
; af6
!= (struct ip6asfrag
*)q6
;
371 af6
= af6
->ip6af_down
)
372 if (af6
->ip6af_off
> ip6af
->ip6af_off
)
377 * If there is a preceding segment, it may provide some of
378 * our data already. If so, drop the data from the incoming
379 * segment. If it provides all of our data, drop us.
381 if (af6
->ip6af_up
!= (struct ip6asfrag
*)q6
) {
382 i
= af6
->ip6af_up
->ip6af_off
+ af6
->ip6af_up
->ip6af_frglen
385 if (i
>= ip6af
->ip6af_frglen
)
387 m_adj(IP6_REASS_MBUF(ip6af
), i
);
388 ip6af
->ip6af_off
+= i
;
389 ip6af
->ip6af_frglen
-= i
;
394 * While we overlap succeeding segments trim them or,
395 * if they are completely covered, dequeue them.
397 while (af6
!= (struct ip6asfrag
*)q6
&&
398 ip6af
->ip6af_off
+ ip6af
->ip6af_frglen
> af6
->ip6af_off
) {
399 i
= (ip6af
->ip6af_off
+ ip6af
->ip6af_frglen
) - af6
->ip6af_off
;
400 if (i
< af6
->ip6af_frglen
) {
401 af6
->ip6af_frglen
-= i
;
403 m_adj(IP6_REASS_MBUF(af6
), i
);
406 af6
= af6
->ip6af_down
;
407 m_freem(IP6_REASS_MBUF(af6
->ip6af_up
));
408 frag6_deq(af6
->ip6af_up
);
412 * If the incoming framgent overlaps some existing fragments in
413 * the reassembly queue, drop it, since it is dangerous to override
414 * existing fragments from a security point of view.
416 if (af6
->ip6af_up
!= (struct ip6asfrag
*)q6
) {
417 i
= af6
->ip6af_up
->ip6af_off
+ af6
->ip6af_up
->ip6af_frglen
420 #if 0 /* suppress the noisy log */
421 log(LOG_ERR
, "%d bytes of a fragment from %s "
422 "overlaps the previous fragment\n",
423 i
, ip6_sprintf(&q6
->ip6q_src
));
425 FREE(ip6af
, M_FTABLE
);
429 if (af6
!= (struct ip6asfrag
*)q6
) {
430 i
= (ip6af
->ip6af_off
+ ip6af
->ip6af_frglen
) - af6
->ip6af_off
;
432 #if 0 /* suppress the noisy log */
433 log(LOG_ERR
, "%d bytes of a fragment from %s "
434 "overlaps the succeeding fragment",
435 i
, ip6_sprintf(&q6
->ip6q_src
));
437 FREE(ip6af
, M_FTABLE
);
446 * Stick new segment in its place;
447 * check for complete reassembly.
448 * Move to front of packet queue, as we are
449 * the most recently active fragmented packet.
451 frag6_enq(ip6af
, af6
->ip6af_up
);
455 if (q6
!= ip6q
.ip6q_next
) {
457 frag6_insque(q6
, &ip6q
);
461 for (af6
= q6
->ip6q_down
; af6
!= (struct ip6asfrag
*)q6
;
462 af6
= af6
->ip6af_down
) {
463 if (af6
->ip6af_off
!= next
) {
464 frag6_doing_reass
= 0;
467 next
+= af6
->ip6af_frglen
;
469 if (af6
->ip6af_up
->ip6af_mff
) {
470 frag6_doing_reass
= 0;
475 * Reassembly is complete; concatenate fragments.
477 ip6af
= q6
->ip6q_down
;
478 t
= m
= IP6_REASS_MBUF(ip6af
);
479 af6
= ip6af
->ip6af_down
;
481 while (af6
!= (struct ip6asfrag
*)q6
) {
482 af6dwn
= af6
->ip6af_down
;
486 t
->m_next
= IP6_REASS_MBUF(af6
);
487 m_adj(t
->m_next
, af6
->ip6af_offset
);
492 /* adjust offset to point where the original next header starts */
493 offset
= ip6af
->ip6af_offset
- sizeof(struct ip6_frag
);
494 FREE(ip6af
, M_FTABLE
);
495 ip6
= mtod(m
, struct ip6_hdr
*);
496 ip6
->ip6_plen
= htons((u_short
)next
+ offset
- sizeof(struct ip6_hdr
));
497 ip6
->ip6_src
= q6
->ip6q_src
;
498 ip6
->ip6_dst
= q6
->ip6q_dst
;
501 *q6
->ip6q_nxtp
= (u_char
)(nxt
& 0xff);
505 * Delete frag6 header with as a few cost as possible.
507 if (offset
< m
->m_len
) {
508 ovbcopy((caddr_t
)ip6
, (caddr_t
)ip6
+ sizeof(struct ip6_frag
),
510 m
->m_data
+= sizeof(struct ip6_frag
);
511 m
->m_len
-= sizeof(struct ip6_frag
);
513 /* this comes with no copy if the boundary is on cluster */
514 if ((t
= m_split(m
, offset
, M_DONTWAIT
)) == NULL
) {
516 frag6_nfrags
-= q6
->ip6q_nfrag
;
518 frag6_nfragpackets
--;
521 m_adj(t
, sizeof(struct ip6_frag
));
526 * Store NXT to the original.
529 char *prvnxtp
= ip6_get_prevhdr(m
, offset
); /* XXX */
534 frag6_nfrags
-= q6
->ip6q_nfrag
;
536 frag6_nfragpackets
--;
538 if (m
->m_flags
& M_PKTHDR
) { /* Isn't it always true? */
540 for (t
= m
; t
; t
= t
->m_next
)
542 m
->m_pkthdr
.len
= plen
;
545 ip6stat
.ip6s_reassembled
++;
546 in6_ifstat_inc(dstifp
, ifs6_reass_ok
);
549 * Tell launch routine the next header
555 frag6_doing_reass
= 0;
559 in6_ifstat_inc(dstifp
, ifs6_reass_fail
);
560 ip6stat
.ip6s_fragdropped
++;
562 frag6_doing_reass
= 0;
567 * Free a fragment reassembly header and all
568 * associated datagrams.
574 struct ip6asfrag
*af6
, *down6
;
576 for (af6
= q6
->ip6q_down
; af6
!= (struct ip6asfrag
*)q6
;
578 struct mbuf
*m
= IP6_REASS_MBUF(af6
);
580 down6
= af6
->ip6af_down
;
584 * Return ICMP time exceeded error for the 1st fragment.
585 * Just free other fragments.
587 if (af6
->ip6af_off
== 0) {
591 ip6
= mtod(m
, struct ip6_hdr
*);
593 /* restoure source and destination addresses */
594 ip6
->ip6_src
= q6
->ip6q_src
;
595 ip6
->ip6_dst
= q6
->ip6q_dst
;
596 icmp6_error(m
, ICMP6_TIME_EXCEEDED
,
597 ICMP6_TIME_EXCEED_REASSEMBLY
, 0);
604 frag6_nfrags
-= q6
->ip6q_nfrag
;
606 frag6_nfragpackets
--;
610 * Put an ip fragment on a reassembly chain.
611 * Like insque, but pointers in middle of structure.
615 struct ip6asfrag
*af6
, *up6
;
618 af6
->ip6af_down
= up6
->ip6af_down
;
619 up6
->ip6af_down
->ip6af_up
= af6
;
620 up6
->ip6af_down
= af6
;
624 * To frag6_enq as remque is to insque.
628 struct ip6asfrag
*af6
;
630 af6
->ip6af_up
->ip6af_down
= af6
->ip6af_down
;
631 af6
->ip6af_down
->ip6af_up
= af6
->ip6af_up
;
635 frag6_insque(new, old
)
636 struct ip6q
*new, *old
;
638 new->ip6q_prev
= old
;
639 new->ip6q_next
= old
->ip6q_next
;
640 old
->ip6q_next
->ip6q_prev
= new;
641 old
->ip6q_next
= new;
648 p6
->ip6q_prev
->ip6q_next
= p6
->ip6q_next
;
649 p6
->ip6q_next
->ip6q_prev
= p6
->ip6q_prev
;
653 * IPv6 reassembling timer processing;
654 * if a timer expires on a reassembly
661 lck_mtx_lock(inet6_domain_mutex
);
663 frag6_doing_reass
= 1;
666 while (q6
!= &ip6q
) {
669 if (q6
->ip6q_prev
->ip6q_ttl
== 0) {
670 ip6stat
.ip6s_fragtimeout
++;
671 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
672 frag6_freef(q6
->ip6q_prev
);
676 * If we are over the maximum number of fragments
677 * (due to the limit being lowered), drain off
678 * enough to get down to the new limit.
680 while (frag6_nfragpackets
> (u_int
)ip6_maxfragpackets
&&
682 ip6stat
.ip6s_fragoverflow
++;
683 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
684 frag6_freef(ip6q
.ip6q_prev
);
686 frag6_doing_reass
= 0;
690 * Routing changes might produce a better route than we last used;
691 * make sure we notice eventually, even if forwarding only for one
692 * destination and the cache is never replaced.
694 if (ip6_forward_rt
.ro_rt
) {
695 rtfree(ip6_forward_rt
.ro_rt
);
696 ip6_forward_rt
.ro_rt
= 0;
698 if (ipsrcchk_rt
.ro_rt
) {
699 rtfree(ipsrcchk_rt
.ro_rt
);
700 ipsrcchk_rt
.ro_rt
= 0;
704 lck_mtx_unlock(inet6_domain_mutex
);
708 * Drain off all datagram fragments.
713 if (frag6_doing_reass
)
715 lck_mtx_lock(inet6_domain_mutex
);
716 while (ip6q
.ip6q_next
!= &ip6q
) {
717 ip6stat
.ip6s_fragdropped
++;
718 /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
719 frag6_freef(ip6q
.ip6q_next
);
721 lck_mtx_unlock(inet6_domain_mutex
);