frag6.c revision 121345
162587Sitojun/* $FreeBSD: head/sys/netinet6/frag6.c 121345 2003-10-22 15:29:42Z ume $ */ 295023Ssuz/* $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $ */ 362587Sitojun 453541Sshin/* 553541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 653541Sshin * All rights reserved. 753541Sshin * 853541Sshin * Redistribution and use in source and binary forms, with or without 953541Sshin * modification, are permitted provided that the following conditions 1053541Sshin * are met: 1153541Sshin * 1. Redistributions of source code must retain the above copyright 1253541Sshin * notice, this list of conditions and the following disclaimer. 1353541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1453541Sshin * notice, this list of conditions and the following disclaimer in the 1553541Sshin * documentation and/or other materials provided with the distribution. 1653541Sshin * 3. Neither the name of the project nor the names of its contributors 1753541Sshin * may be used to endorse or promote products derived from this software 1853541Sshin * without specific prior written permission. 1953541Sshin * 2053541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2153541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2253541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2353541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2453541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2553541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2653541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2753541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2853541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2953541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3053541Sshin * SUCH DAMAGE. 3153541Sshin */ 3253541Sshin 33120645Sume#include "opt_random_ip_id.h" 34120645Sume 3553541Sshin#include <sys/param.h> 3653541Sshin#include <sys/systm.h> 3753541Sshin#include <sys/malloc.h> 3853541Sshin#include <sys/mbuf.h> 3953541Sshin#include <sys/domain.h> 4053541Sshin#include <sys/protosw.h> 4153541Sshin#include <sys/socket.h> 4253541Sshin#include <sys/errno.h> 4353541Sshin#include <sys/time.h> 4453541Sshin#include <sys/kernel.h> 4553541Sshin#include <sys/syslog.h> 4653541Sshin 4753541Sshin#include <net/if.h> 4853541Sshin#include <net/route.h> 4953541Sshin 5053541Sshin#include <netinet/in.h> 5153541Sshin#include <netinet/in_var.h> 5262587Sitojun#include <netinet/ip6.h> 5353541Sshin#include <netinet6/ip6_var.h> 5462587Sitojun#include <netinet/icmp6.h> 5553541Sshin 5653541Sshin#include <net/net_osdep.h> 5753541Sshin 5853541Sshin/* 5953541Sshin * Define it to get a correct behavior on per-interface statistics. 6053541Sshin * You will need to perform an extra routing table lookup, per fragment, 6153541Sshin * to do it. This may, or may not be, a performance hit. 6253541Sshin */ 6362587Sitojun#define IN6_IFSTAT_STRICT 6453541Sshin 6562587Sitojunstatic void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *)); 6662587Sitojunstatic void frag6_deq __P((struct ip6asfrag *)); 6762587Sitojunstatic void frag6_insque __P((struct ip6q *, struct ip6q *)); 6862587Sitojunstatic void frag6_remque __P((struct ip6q *)); 6962587Sitojunstatic void frag6_freef __P((struct ip6q *)); 7053541Sshin 71121345Sumestatic int ip6q_locked; 7262587Sitojunu_int frag6_nfragpackets; 73121345Sumeu_int frag6_nfrags; 7462587Sitojunstruct ip6q ip6q; /* ip6 reassemble queue */ 7553541Sshin 76121345Sumestatic __inline int ip6q_lock_try __P((void)); 77121345Sumestatic __inline void ip6q_unlock __P((void)); 78121345Sume 79121345Sumestatic __inline int 80121345Sumeip6q_lock_try() 81121345Sume{ 82121345Sume if (ip6q_locked) 83121345Sume return (0); 84121345Sume ip6q_locked = 1; 85121345Sume return (1); 86121345Sume} 87121345Sume 88121345Sumestatic __inline void 89121345Sumeip6q_unlock() 90121345Sume{ 91121345Sume ip6q_locked = 0; 92121345Sume} 93121345Sume 94121345Sume#ifdef DIAGNOSTIC 95121345Sume#define IP6Q_LOCK() \ 96121345Sumedo { \ 97121345Sume if (ip6q_lock_try() == 0) { \ 98121345Sume printf("%s:%d: ip6q already locked\n", __FILE__, __LINE__); \ 99121345Sume panic("ip6q_lock"); \ 100121345Sume } \ 101121345Sume} while (/*CONSTCOND*/ 0) 102121345Sume#define IP6Q_LOCK_CHECK() \ 103121345Sumedo { \ 104121345Sume if (ip6q_locked == 0) { \ 105121345Sume printf("%s:%d: ip6q lock not held\n", __FILE__, __LINE__); \ 106121345Sume panic("ip6q lock check"); \ 107121345Sume } \ 108121345Sume} while (/*CONSTCOND*/ 0) 109121345Sume#else 110121345Sume#define IP6Q_LOCK() (void) ip6q_lock_try() 111121345Sume#define IP6Q_LOCK_CHECK() /* nothing */ 112121345Sume#endif 113121345Sume 114121345Sume#define IP6Q_UNLOCK() ip6q_unlock() 115121345Sume 11669774Sphkstatic MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header"); 11762587Sitojun 11853541Sshin/* 11953541Sshin * Initialise reassembly queue and fragment identifier. 12053541Sshin */ 12153541Sshinvoid 12253541Sshinfrag6_init() 12353541Sshin{ 12453541Sshin 12577969Sjesper ip6_maxfragpackets = nmbclusters / 4; 126121345Sume ip6_maxfrags = nmbclusters / 4; 12777969Sjesper 128120645Sume#ifndef RANDOM_IP_ID 129120648Sume ip6_id = arc4random(); 130120643Sume#endif 13153541Sshin ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q; 13253541Sshin} 13353541Sshin 13453541Sshin/* 13562587Sitojun * In RFC2460, fragment and reassembly rule do not agree with each other, 13662587Sitojun * in terms of next header field handling in fragment header. 13762587Sitojun * While the sender will use the same value for all of the fragmented packets, 13862587Sitojun * receiver is suggested not to check the consistency. 13962587Sitojun * 14062587Sitojun * fragment rule (p20): 14162587Sitojun * (2) A Fragment header containing: 14262587Sitojun * The Next Header value that identifies the first header of 14362587Sitojun * the Fragmentable Part of the original packet. 14462587Sitojun * -> next header field is same for all fragments 14562587Sitojun * 14662587Sitojun * reassembly rule (p21): 14762587Sitojun * The Next Header field of the last header of the Unfragmentable 14862587Sitojun * Part is obtained from the Next Header field of the first 14962587Sitojun * fragment's Fragment header. 15062587Sitojun * -> should grab it from the first fragment only 15162587Sitojun * 15262587Sitojun * The following note also contradicts with fragment rule - noone is going to 15362587Sitojun * send different fragment with different next header field. 15462587Sitojun * 15562587Sitojun * additional note (p22): 15662587Sitojun * The Next Header values in the Fragment headers of different 15762587Sitojun * fragments of the same original packet may differ. Only the value 15862587Sitojun * from the Offset zero fragment packet is used for reassembly. 15962587Sitojun * -> should grab it from the first fragment only 16062587Sitojun * 16162587Sitojun * There is no explicit reason given in the RFC. Historical reason maybe? 16262587Sitojun */ 16362587Sitojun/* 16453541Sshin * Fragment input 16553541Sshin */ 16653541Sshinint 16753541Sshinfrag6_input(mp, offp, proto) 16853541Sshin struct mbuf **mp; 16953541Sshin int *offp, proto; 17053541Sshin{ 17153541Sshin struct mbuf *m = *mp, *t; 17253541Sshin struct ip6_hdr *ip6; 17353541Sshin struct ip6_frag *ip6f; 17453541Sshin struct ip6q *q6; 17562587Sitojun struct ip6asfrag *af6, *ip6af, *af6dwn; 17653541Sshin int offset = *offp, nxt, i, next; 17753541Sshin int first_frag = 0; 17862587Sitojun int fragoff, frgpartlen; /* must be larger than u_int16_t */ 17953541Sshin struct ifnet *dstifp; 18053541Sshin#ifdef IN6_IFSTAT_STRICT 18153541Sshin static struct route_in6 ro; 18253541Sshin struct sockaddr_in6 *dst; 18353541Sshin#endif 18453541Sshin 18562587Sitojun ip6 = mtod(m, struct ip6_hdr *); 18662587Sitojun#ifndef PULLDOWN_TEST 18753541Sshin IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE); 18853541Sshin ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset); 18962587Sitojun#else 19062587Sitojun IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f)); 19162587Sitojun if (ip6f == NULL) 192120856Sume return (IPPROTO_DONE); 19362587Sitojun#endif 19453541Sshin 19553541Sshin dstifp = NULL; 19653541Sshin#ifdef IN6_IFSTAT_STRICT 19753541Sshin /* find the destination interface of the packet. */ 19853541Sshin dst = (struct sockaddr_in6 *)&ro.ro_dst; 19953541Sshin if (ro.ro_rt 20053541Sshin && ((ro.ro_rt->rt_flags & RTF_UP) == 0 20153541Sshin || !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) { 20253541Sshin RTFREE(ro.ro_rt); 20353541Sshin ro.ro_rt = (struct rtentry *)0; 20453541Sshin } 20553541Sshin if (ro.ro_rt == NULL) { 20653541Sshin bzero(dst, sizeof(*dst)); 20753541Sshin dst->sin6_family = AF_INET6; 20853541Sshin dst->sin6_len = sizeof(struct sockaddr_in6); 20953541Sshin dst->sin6_addr = ip6->ip6_dst; 21053541Sshin } 21154350Sshin rtalloc((struct route *)&ro); 21253541Sshin if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL) 21353541Sshin dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp; 21453541Sshin#else 21553541Sshin /* we are violating the spec, this is not the destination interface */ 21653541Sshin if ((m->m_flags & M_PKTHDR) != 0) 21753541Sshin dstifp = m->m_pkthdr.rcvif; 21853541Sshin#endif 21953541Sshin 22053541Sshin /* jumbo payload can't contain a fragment header */ 22153541Sshin if (ip6->ip6_plen == 0) { 22253541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset); 22353541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 22453541Sshin return IPPROTO_DONE; 22553541Sshin } 22653541Sshin 22753541Sshin /* 22853541Sshin * check whether fragment packet's fragment length is 22953541Sshin * multiple of 8 octets. 23053541Sshin * sizeof(struct ip6_frag) == 8 23153541Sshin * sizeof(struct ip6_hdr) = 40 23253541Sshin */ 23353541Sshin if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && 23453541Sshin (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) { 235120891Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 236120891Sume offsetof(struct ip6_hdr, ip6_plen)); 23753541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 23853541Sshin return IPPROTO_DONE; 23953541Sshin } 24053541Sshin 24153541Sshin ip6stat.ip6s_fragments++; 24253541Sshin in6_ifstat_inc(dstifp, ifs6_reass_reqd); 243120891Sume 24462587Sitojun /* offset now points to data portion */ 24553541Sshin offset += sizeof(struct ip6_frag); 24653541Sshin 247121345Sume IP6Q_LOCK(); 24878064Sume 249121345Sume /* 250121345Sume * Enforce upper bound on number of fragments. 251121345Sume * If maxfrag is 0, never accept fragments. 252121345Sume * If maxfrag is -1, accept all fragments without limitation. 253121345Sume */ 254121345Sume if (ip6_maxfrags < 0) 255121345Sume ; 256121345Sume else if (frag6_nfrags >= (u_int)ip6_maxfrags) 257121345Sume goto dropfrag; 258121345Sume 25953541Sshin for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next) 26053541Sshin if (ip6f->ip6f_ident == q6->ip6q_ident && 26153541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && 26253541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)) 26353541Sshin break; 26453541Sshin 26553541Sshin if (q6 == &ip6q) { 26653541Sshin /* 26753541Sshin * the first fragment to arrive, create a reassembly queue. 26853541Sshin */ 26953541Sshin first_frag = 1; 27053541Sshin 27153541Sshin /* 27253541Sshin * Enforce upper bound on number of fragmented packets 27353541Sshin * for which we attempt reassembly; 274121345Sume * If maxfragpackets is 0, never accept fragments. 275121345Sume * If maxfragpackets is -1, accept all fragments without 276121345Sume * limitation. 27753541Sshin */ 27878064Sume if (ip6_maxfragpackets < 0) 27978064Sume ; 28078064Sume else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets) 28178064Sume goto dropfrag; 28278064Sume frag6_nfragpackets++; 28353541Sshin q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE, 284120891Sume M_DONTWAIT); 28553541Sshin if (q6 == NULL) 28653541Sshin goto dropfrag; 28762587Sitojun bzero(q6, sizeof(*q6)); 28853541Sshin 28953541Sshin frag6_insque(q6, &ip6q); 29053541Sshin 29162587Sitojun /* ip6q_nxt will be filled afterwards, from 1st fragment */ 29253541Sshin q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6; 29362587Sitojun#ifdef notyet 29462587Sitojun q6->ip6q_nxtp = (u_char *)nxtp; 29562587Sitojun#endif 29653541Sshin q6->ip6q_ident = ip6f->ip6f_ident; 29753541Sshin q6->ip6q_arrive = 0; /* Is it used anywhere? */ 29853541Sshin q6->ip6q_ttl = IPV6_FRAGTTL; 29953541Sshin q6->ip6q_src = ip6->ip6_src; 30053541Sshin q6->ip6q_dst = ip6->ip6_dst; 30153541Sshin q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ 302121345Sume 303121345Sume q6->ip6q_nfrag = 0; 30453541Sshin } 30553541Sshin 30653541Sshin /* 30753541Sshin * If it's the 1st fragment, record the length of the 30853541Sshin * unfragmentable part and the next header of the fragment header. 30953541Sshin */ 31053541Sshin fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); 31153541Sshin if (fragoff == 0) { 312120891Sume q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - 313120891Sume sizeof(struct ip6_frag); 31453541Sshin q6->ip6q_nxt = ip6f->ip6f_nxt; 31553541Sshin } 31653541Sshin 31753541Sshin /* 31853541Sshin * Check that the reassembled packet would not exceed 65535 bytes 31953541Sshin * in size. 32053541Sshin * If it would exceed, discard the fragment and return an ICMP error. 32153541Sshin */ 32262587Sitojun frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; 32353541Sshin if (q6->ip6q_unfrglen >= 0) { 32453541Sshin /* The 1st fragment has already arrived. */ 32553541Sshin if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { 32653541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 327120891Sume offset - sizeof(struct ip6_frag) + 328120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 329121345Sume IP6Q_UNLOCK(); 330120856Sume return (IPPROTO_DONE); 33153541Sshin } 332120891Sume } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { 33353541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 334120891Sume offset - sizeof(struct ip6_frag) + 335120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 336121345Sume IP6Q_UNLOCK(); 337120856Sume return (IPPROTO_DONE); 33853541Sshin } 33953541Sshin /* 34053541Sshin * If it's the first fragment, do the above check for each 34153541Sshin * fragment already stored in the reassembly queue. 34253541Sshin */ 34353541Sshin if (fragoff == 0) { 34453541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 34553541Sshin af6 = af6dwn) { 34653541Sshin af6dwn = af6->ip6af_down; 34753541Sshin 34853541Sshin if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen > 34953541Sshin IPV6_MAXPACKET) { 35053541Sshin struct mbuf *merr = IP6_REASS_MBUF(af6); 35153541Sshin struct ip6_hdr *ip6err; 35253541Sshin int erroff = af6->ip6af_offset; 35353541Sshin 35453541Sshin /* dequeue the fragment. */ 35553541Sshin frag6_deq(af6); 35662587Sitojun free(af6, M_FTABLE); 35753541Sshin 35853541Sshin /* adjust pointer. */ 35953541Sshin ip6err = mtod(merr, struct ip6_hdr *); 36053541Sshin 36153541Sshin /* 36253541Sshin * Restore source and destination addresses 36353541Sshin * in the erroneous IPv6 header. 36453541Sshin */ 36553541Sshin ip6err->ip6_src = q6->ip6q_src; 36653541Sshin ip6err->ip6_dst = q6->ip6q_dst; 36753541Sshin 36853541Sshin icmp6_error(merr, ICMP6_PARAM_PROB, 369120891Sume ICMP6_PARAMPROB_HEADER, 370120891Sume erroff - sizeof(struct ip6_frag) + 371120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 37253541Sshin } 37353541Sshin } 37453541Sshin } 37553541Sshin 37662587Sitojun ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE, 377111119Simp M_DONTWAIT); 37862587Sitojun if (ip6af == NULL) 37962587Sitojun goto dropfrag; 38062587Sitojun bzero(ip6af, sizeof(*ip6af)); 38162587Sitojun ip6af->ip6af_head = ip6->ip6_flow; 38262587Sitojun ip6af->ip6af_len = ip6->ip6_plen; 38362587Sitojun ip6af->ip6af_nxt = ip6->ip6_nxt; 38462587Sitojun ip6af->ip6af_hlim = ip6->ip6_hlim; 38553541Sshin ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG; 38653541Sshin ip6af->ip6af_off = fragoff; 38753541Sshin ip6af->ip6af_frglen = frgpartlen; 38853541Sshin ip6af->ip6af_offset = offset; 38953541Sshin IP6_REASS_MBUF(ip6af) = m; 39053541Sshin 39153541Sshin if (first_frag) { 39253541Sshin af6 = (struct ip6asfrag *)q6; 39353541Sshin goto insert; 39453541Sshin } 39553541Sshin 39653541Sshin /* 39753541Sshin * Find a segment which begins after this one does. 39853541Sshin */ 39953541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 40053541Sshin af6 = af6->ip6af_down) 40153541Sshin if (af6->ip6af_off > ip6af->ip6af_off) 40253541Sshin break; 40353541Sshin 40462587Sitojun#if 0 40553541Sshin /* 40662587Sitojun * If there is a preceding segment, it may provide some of 40762587Sitojun * our data already. If so, drop the data from the incoming 40862587Sitojun * segment. If it provides all of our data, drop us. 40962587Sitojun */ 41062587Sitojun if (af6->ip6af_up != (struct ip6asfrag *)q6) { 41162587Sitojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 41262587Sitojun - ip6af->ip6af_off; 41362587Sitojun if (i > 0) { 41462587Sitojun if (i >= ip6af->ip6af_frglen) 41562587Sitojun goto dropfrag; 41662587Sitojun m_adj(IP6_REASS_MBUF(ip6af), i); 41762587Sitojun ip6af->ip6af_off += i; 41862587Sitojun ip6af->ip6af_frglen -= i; 41962587Sitojun } 42062587Sitojun } 42162587Sitojun 42262587Sitojun /* 42362587Sitojun * While we overlap succeeding segments trim them or, 42462587Sitojun * if they are completely covered, dequeue them. 42562587Sitojun */ 42662587Sitojun while (af6 != (struct ip6asfrag *)q6 && 42762587Sitojun ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) { 42862587Sitojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 42962587Sitojun if (i < af6->ip6af_frglen) { 43062587Sitojun af6->ip6af_frglen -= i; 43162587Sitojun af6->ip6af_off += i; 43262587Sitojun m_adj(IP6_REASS_MBUF(af6), i); 43362587Sitojun break; 43462587Sitojun } 43562587Sitojun af6 = af6->ip6af_down; 43662587Sitojun m_freem(IP6_REASS_MBUF(af6->ip6af_up)); 43762587Sitojun frag6_deq(af6->ip6af_up); 43862587Sitojun } 43962587Sitojun#else 44062587Sitojun /* 44153541Sshin * If the incoming framgent overlaps some existing fragments in 44253541Sshin * the reassembly queue, drop it, since it is dangerous to override 44353541Sshin * existing fragments from a security point of view. 444121345Sume * We don't know which fragment is the bad guy - here we trust 445121345Sume * fragment that came in earlier, with no real reason. 44653541Sshin */ 44753541Sshin if (af6->ip6af_up != (struct ip6asfrag *)q6) { 44853541Sshin i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 44953541Sshin - ip6af->ip6af_off; 45053541Sshin if (i > 0) { 45176899Ssumikawa#if 0 /* suppress the noisy log */ 45253541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 45353541Sshin "overlaps the previous fragment\n", 45453541Sshin i, ip6_sprintf(&q6->ip6q_src)); 45576899Ssumikawa#endif 45676899Ssumikawa free(ip6af, M_FTABLE); 45753541Sshin goto dropfrag; 45853541Sshin } 45953541Sshin } 46053541Sshin if (af6 != (struct ip6asfrag *)q6) { 46153541Sshin i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 46253541Sshin if (i > 0) { 46376899Ssumikawa#if 0 /* suppress the noisy log */ 46453541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 46553541Sshin "overlaps the succeeding fragment", 46653541Sshin i, ip6_sprintf(&q6->ip6q_src)); 46776899Ssumikawa#endif 46876899Ssumikawa free(ip6af, M_FTABLE); 46953541Sshin goto dropfrag; 47053541Sshin } 47153541Sshin } 47262587Sitojun#endif 47353541Sshin 47453541Sshininsert: 47553541Sshin 47653541Sshin /* 47753541Sshin * Stick new segment in its place; 47853541Sshin * check for complete reassembly. 47953541Sshin * Move to front of packet queue, as we are 48053541Sshin * the most recently active fragmented packet. 48153541Sshin */ 48253541Sshin frag6_enq(ip6af, af6->ip6af_up); 483121345Sume frag6_nfrags++; 484121345Sume q6->ip6q_nfrag++; 48562587Sitojun#if 0 /* xxx */ 48662587Sitojun if (q6 != ip6q.ip6q_next) { 48762587Sitojun frag6_remque(q6); 48862587Sitojun frag6_insque(q6, &ip6q); 48962587Sitojun } 49062587Sitojun#endif 49153541Sshin next = 0; 49253541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 49353541Sshin af6 = af6->ip6af_down) { 49453541Sshin if (af6->ip6af_off != next) { 495121345Sume IP6Q_UNLOCK(); 49653541Sshin return IPPROTO_DONE; 49753541Sshin } 49853541Sshin next += af6->ip6af_frglen; 49953541Sshin } 50053541Sshin if (af6->ip6af_up->ip6af_mff) { 501121345Sume IP6Q_UNLOCK(); 50253541Sshin return IPPROTO_DONE; 50353541Sshin } 50453541Sshin 50553541Sshin /* 50653541Sshin * Reassembly is complete; concatenate fragments. 50753541Sshin */ 50853541Sshin ip6af = q6->ip6q_down; 50953541Sshin t = m = IP6_REASS_MBUF(ip6af); 51053541Sshin af6 = ip6af->ip6af_down; 51162587Sitojun frag6_deq(ip6af); 51253541Sshin while (af6 != (struct ip6asfrag *)q6) { 51362587Sitojun af6dwn = af6->ip6af_down; 51462587Sitojun frag6_deq(af6); 51553541Sshin while (t->m_next) 51653541Sshin t = t->m_next; 51753541Sshin t->m_next = IP6_REASS_MBUF(af6); 51862587Sitojun m_adj(t->m_next, af6->ip6af_offset); 51962587Sitojun free(af6, M_FTABLE); 52062587Sitojun af6 = af6dwn; 52153541Sshin } 52253541Sshin 52353541Sshin /* adjust offset to point where the original next header starts */ 52453541Sshin offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); 52562587Sitojun free(ip6af, M_FTABLE); 52662587Sitojun ip6 = mtod(m, struct ip6_hdr *); 52753541Sshin ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr)); 52853541Sshin ip6->ip6_src = q6->ip6q_src; 52953541Sshin ip6->ip6_dst = q6->ip6q_dst; 53053541Sshin nxt = q6->ip6q_nxt; 53162587Sitojun#ifdef notyet 53262587Sitojun *q6->ip6q_nxtp = (u_char)(nxt & 0xff); 53362587Sitojun#endif 53453541Sshin 53553541Sshin /* 53653541Sshin * Delete frag6 header with as a few cost as possible. 53753541Sshin */ 53862587Sitojun if (offset < m->m_len) { 53953541Sshin ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag), 54053541Sshin offset); 54162587Sitojun m->m_data += sizeof(struct ip6_frag); 54262587Sitojun m->m_len -= sizeof(struct ip6_frag); 54362587Sitojun } else { 54462587Sitojun /* this comes with no copy if the boundary is on cluster */ 545111119Simp if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) { 54662587Sitojun frag6_remque(q6); 547121345Sume frag6_nfrags -= q6->ip6q_nfrag; 54862587Sitojun free(q6, M_FTABLE); 54962587Sitojun frag6_nfragpackets--; 55062587Sitojun goto dropfrag; 55162587Sitojun } 55262587Sitojun m_adj(t, sizeof(struct ip6_frag)); 55362587Sitojun m_cat(m, t); 55453541Sshin } 55553541Sshin 55653541Sshin /* 55753541Sshin * Store NXT to the original. 55853541Sshin */ 55953541Sshin { 56053541Sshin char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ 56153541Sshin *prvnxtp = nxt; 56253541Sshin } 56353541Sshin 56453541Sshin frag6_remque(q6); 565121345Sume frag6_nfrags -= q6->ip6q_nfrag; 56653541Sshin free(q6, M_FTABLE); 56753541Sshin frag6_nfragpackets--; 56853541Sshin 56953541Sshin if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 57053541Sshin int plen = 0; 57153541Sshin for (t = m; t; t = t->m_next) 57253541Sshin plen += t->m_len; 57353541Sshin m->m_pkthdr.len = plen; 57453541Sshin } 575120891Sume 57653541Sshin ip6stat.ip6s_reassembled++; 57753541Sshin in6_ifstat_inc(dstifp, ifs6_reass_ok); 57853541Sshin 57953541Sshin /* 58053541Sshin * Tell launch routine the next header 58153541Sshin */ 58253541Sshin 58353541Sshin *mp = m; 58453541Sshin *offp = offset; 58553541Sshin 586121345Sume IP6Q_UNLOCK(); 58753541Sshin return nxt; 58853541Sshin 58953541Sshin dropfrag: 59053541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 59153541Sshin ip6stat.ip6s_fragdropped++; 59253541Sshin m_freem(m); 593121345Sume IP6Q_UNLOCK(); 59453541Sshin return IPPROTO_DONE; 59553541Sshin} 59653541Sshin 59753541Sshin/* 59853541Sshin * Free a fragment reassembly header and all 59953541Sshin * associated datagrams. 60053541Sshin */ 60153541Sshinvoid 60253541Sshinfrag6_freef(q6) 60353541Sshin struct ip6q *q6; 60453541Sshin{ 60553541Sshin struct ip6asfrag *af6, *down6; 60653541Sshin 607121345Sume IP6Q_LOCK_CHECK(); 608121345Sume 60953541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 61053541Sshin af6 = down6) { 61153541Sshin struct mbuf *m = IP6_REASS_MBUF(af6); 61253541Sshin 61353541Sshin down6 = af6->ip6af_down; 61453541Sshin frag6_deq(af6); 61553541Sshin 61653541Sshin /* 61753541Sshin * Return ICMP time exceeded error for the 1st fragment. 61853541Sshin * Just free other fragments. 61953541Sshin */ 62053541Sshin if (af6->ip6af_off == 0) { 62153541Sshin struct ip6_hdr *ip6; 62253541Sshin 62353541Sshin /* adjust pointer */ 62453541Sshin ip6 = mtod(m, struct ip6_hdr *); 62553541Sshin 626120891Sume /* restore source and destination addresses */ 62753541Sshin ip6->ip6_src = q6->ip6q_src; 62853541Sshin ip6->ip6_dst = q6->ip6q_dst; 62953541Sshin 63053541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 63153541Sshin ICMP6_TIME_EXCEED_REASSEMBLY, 0); 63262587Sitojun } else 63353541Sshin m_freem(m); 63462587Sitojun free(af6, M_FTABLE); 63553541Sshin } 63653541Sshin frag6_remque(q6); 637121345Sume frag6_nfrags -= q6->ip6q_nfrag; 63853541Sshin free(q6, M_FTABLE); 63953541Sshin frag6_nfragpackets--; 64053541Sshin} 64153541Sshin 64253541Sshin/* 64353541Sshin * Put an ip fragment on a reassembly chain. 64453541Sshin * Like insque, but pointers in middle of structure. 64553541Sshin */ 64653541Sshinvoid 64753541Sshinfrag6_enq(af6, up6) 64853541Sshin struct ip6asfrag *af6, *up6; 64953541Sshin{ 650121345Sume 651121345Sume IP6Q_LOCK_CHECK(); 652121345Sume 65353541Sshin af6->ip6af_up = up6; 65453541Sshin af6->ip6af_down = up6->ip6af_down; 65553541Sshin up6->ip6af_down->ip6af_up = af6; 65653541Sshin up6->ip6af_down = af6; 65753541Sshin} 65853541Sshin 65953541Sshin/* 66053541Sshin * To frag6_enq as remque is to insque. 66153541Sshin */ 66253541Sshinvoid 66353541Sshinfrag6_deq(af6) 66453541Sshin struct ip6asfrag *af6; 66553541Sshin{ 666121345Sume 667121345Sume IP6Q_LOCK_CHECK(); 668121345Sume 66953541Sshin af6->ip6af_up->ip6af_down = af6->ip6af_down; 67053541Sshin af6->ip6af_down->ip6af_up = af6->ip6af_up; 67153541Sshin} 67253541Sshin 67353541Sshinvoid 67453541Sshinfrag6_insque(new, old) 67553541Sshin struct ip6q *new, *old; 67653541Sshin{ 677121345Sume 678121345Sume IP6Q_LOCK_CHECK(); 679121345Sume 68053541Sshin new->ip6q_prev = old; 68153541Sshin new->ip6q_next = old->ip6q_next; 68253541Sshin old->ip6q_next->ip6q_prev= new; 68353541Sshin old->ip6q_next = new; 68453541Sshin} 68553541Sshin 68653541Sshinvoid 68753541Sshinfrag6_remque(p6) 68853541Sshin struct ip6q *p6; 68953541Sshin{ 690121345Sume 691121345Sume IP6Q_LOCK_CHECK(); 692121345Sume 69353541Sshin p6->ip6q_prev->ip6q_next = p6->ip6q_next; 69453541Sshin p6->ip6q_next->ip6q_prev = p6->ip6q_prev; 69553541Sshin} 69653541Sshin 69753541Sshin/* 69878064Sume * IPv6 reassembling timer processing; 69953541Sshin * if a timer expires on a reassembly 70053541Sshin * queue, discard it. 70153541Sshin */ 70253541Sshinvoid 70353541Sshinfrag6_slowtimo() 70453541Sshin{ 70553541Sshin struct ip6q *q6; 70653541Sshin int s = splnet(); 70753541Sshin 708121345Sume IP6Q_LOCK(); 70953541Sshin q6 = ip6q.ip6q_next; 71053541Sshin if (q6) 71153541Sshin while (q6 != &ip6q) { 71253541Sshin --q6->ip6q_ttl; 71353541Sshin q6 = q6->ip6q_next; 71453541Sshin if (q6->ip6q_prev->ip6q_ttl == 0) { 71553541Sshin ip6stat.ip6s_fragtimeout++; 71653541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 71753541Sshin frag6_freef(q6->ip6q_prev); 71853541Sshin } 71953541Sshin } 72053541Sshin /* 72153541Sshin * If we are over the maximum number of fragments 72253541Sshin * (due to the limit being lowered), drain off 72353541Sshin * enough to get down to the new limit. 72453541Sshin */ 72578064Sume while (frag6_nfragpackets > (u_int)ip6_maxfragpackets && 72678064Sume ip6q.ip6q_prev) { 72753541Sshin ip6stat.ip6s_fragoverflow++; 72853541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 72953541Sshin frag6_freef(ip6q.ip6q_prev); 73053541Sshin } 731121345Sume IP6Q_UNLOCK(); 73262587Sitojun 73362587Sitojun#if 0 73462587Sitojun /* 73562587Sitojun * Routing changes might produce a better route than we last used; 73662587Sitojun * make sure we notice eventually, even if forwarding only for one 73762587Sitojun * destination and the cache is never replaced. 73862587Sitojun */ 73962587Sitojun if (ip6_forward_rt.ro_rt) { 74062587Sitojun RTFREE(ip6_forward_rt.ro_rt); 74162587Sitojun ip6_forward_rt.ro_rt = 0; 74262587Sitojun } 74362587Sitojun if (ipsrcchk_rt.ro_rt) { 74462587Sitojun RTFREE(ipsrcchk_rt.ro_rt); 74562587Sitojun ipsrcchk_rt.ro_rt = 0; 74662587Sitojun } 74762587Sitojun#endif 74862587Sitojun 74953541Sshin splx(s); 75053541Sshin} 75153541Sshin 75253541Sshin/* 75353541Sshin * Drain off all datagram fragments. 75453541Sshin */ 75553541Sshinvoid 75653541Sshinfrag6_drain() 75753541Sshin{ 758121345Sume 759121345Sume if (ip6q_lock_try() == 0) 76053541Sshin return; 76153541Sshin while (ip6q.ip6q_next != &ip6q) { 76253541Sshin ip6stat.ip6s_fragdropped++; 76353541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 76453541Sshin frag6_freef(ip6q.ip6q_next); 76553541Sshin } 766121345Sume IP6Q_UNLOCK(); 76753541Sshin} 768