frag6.c revision 165118
162587Sitojun/* $FreeBSD: head/sys/netinet6/frag6.c 165118 2006-12-12 12:17:58Z bz $ */ 295023Ssuz/* $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $ */ 362587Sitojun 4139826Simp/*- 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 3353541Sshin#include <sys/param.h> 3453541Sshin#include <sys/systm.h> 3553541Sshin#include <sys/malloc.h> 3653541Sshin#include <sys/mbuf.h> 3753541Sshin#include <sys/domain.h> 3853541Sshin#include <sys/protosw.h> 3953541Sshin#include <sys/socket.h> 4053541Sshin#include <sys/errno.h> 4153541Sshin#include <sys/time.h> 4253541Sshin#include <sys/kernel.h> 4353541Sshin#include <sys/syslog.h> 4453541Sshin 4553541Sshin#include <net/if.h> 4653541Sshin#include <net/route.h> 4753541Sshin 4853541Sshin#include <netinet/in.h> 4953541Sshin#include <netinet/in_var.h> 5062587Sitojun#include <netinet/ip6.h> 5153541Sshin#include <netinet6/ip6_var.h> 5262587Sitojun#include <netinet/icmp6.h> 53121684Sume#include <netinet/in_systm.h> /* for ECN definitions */ 54121684Sume#include <netinet/ip.h> /* for ECN definitions */ 5553541Sshin 5653541Sshin/* 5753541Sshin * Define it to get a correct behavior on per-interface statistics. 5853541Sshin * You will need to perform an extra routing table lookup, per fragment, 5953541Sshin * to do it. This may, or may not be, a performance hit. 6053541Sshin */ 6162587Sitojun#define IN6_IFSTAT_STRICT 6253541Sshin 6362587Sitojunstatic void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *)); 6462587Sitojunstatic void frag6_deq __P((struct ip6asfrag *)); 6562587Sitojunstatic void frag6_insque __P((struct ip6q *, struct ip6q *)); 6662587Sitojunstatic void frag6_remque __P((struct ip6q *)); 6762587Sitojunstatic void frag6_freef __P((struct ip6q *)); 6853541Sshin 69121346Sumestatic struct mtx ip6qlock; 70121346Sume/* 71121346Sume * These fields all protected by ip6qlock. 72121346Sume */ 73121346Sumestatic u_int frag6_nfragpackets; 74121346Sumestatic u_int frag6_nfrags; 75121346Sumestatic struct ip6q ip6q; /* ip6 reassemble queue */ 7653541Sshin 77121346Sume#define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF); 78121346Sume#define IP6Q_LOCK() mtx_lock(&ip6qlock) 79121346Sume#define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock) 80121355Sume#define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED) 81121346Sume#define IP6Q_UNLOCK() mtx_unlock(&ip6qlock) 82121345Sume 8369774Sphkstatic MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header"); 8462587Sitojun 8553541Sshin/* 8653541Sshin * Initialise reassembly queue and fragment identifier. 8753541Sshin */ 88157927Spsstatic void 89157927Spsfrag6_change(void *tag) 90157927Sps{ 91157927Sps 92157927Sps ip6_maxfragpackets = nmbclusters / 4; 93157927Sps ip6_maxfrags = nmbclusters / 4; 94157927Sps} 95157927Sps 9653541Sshinvoid 9753541Sshinfrag6_init() 9853541Sshin{ 9953541Sshin 10077969Sjesper ip6_maxfragpackets = nmbclusters / 4; 101121345Sume ip6_maxfrags = nmbclusters / 4; 102157927Sps EVENTHANDLER_REGISTER(nmbclusters_change, 103157927Sps frag6_change, NULL, EVENTHANDLER_PRI_ANY); 10477969Sjesper 105121346Sume IP6Q_LOCK_INIT(); 106121346Sume 10753541Sshin ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q; 10853541Sshin} 10953541Sshin 11053541Sshin/* 11162587Sitojun * In RFC2460, fragment and reassembly rule do not agree with each other, 11262587Sitojun * in terms of next header field handling in fragment header. 11362587Sitojun * While the sender will use the same value for all of the fragmented packets, 11462587Sitojun * receiver is suggested not to check the consistency. 11562587Sitojun * 11662587Sitojun * fragment rule (p20): 11762587Sitojun * (2) A Fragment header containing: 11862587Sitojun * The Next Header value that identifies the first header of 11962587Sitojun * the Fragmentable Part of the original packet. 12062587Sitojun * -> next header field is same for all fragments 12162587Sitojun * 12262587Sitojun * reassembly rule (p21): 12362587Sitojun * The Next Header field of the last header of the Unfragmentable 12462587Sitojun * Part is obtained from the Next Header field of the first 12562587Sitojun * fragment's Fragment header. 12662587Sitojun * -> should grab it from the first fragment only 12762587Sitojun * 12862587Sitojun * The following note also contradicts with fragment rule - noone is going to 12962587Sitojun * send different fragment with different next header field. 13062587Sitojun * 13162587Sitojun * additional note (p22): 13262587Sitojun * The Next Header values in the Fragment headers of different 13362587Sitojun * fragments of the same original packet may differ. Only the value 13462587Sitojun * from the Offset zero fragment packet is used for reassembly. 13562587Sitojun * -> should grab it from the first fragment only 13662587Sitojun * 13762587Sitojun * There is no explicit reason given in the RFC. Historical reason maybe? 13862587Sitojun */ 13962587Sitojun/* 14053541Sshin * Fragment input 14153541Sshin */ 14253541Sshinint 14353541Sshinfrag6_input(mp, offp, proto) 14453541Sshin struct mbuf **mp; 14553541Sshin int *offp, proto; 14653541Sshin{ 14753541Sshin struct mbuf *m = *mp, *t; 14853541Sshin struct ip6_hdr *ip6; 14953541Sshin struct ip6_frag *ip6f; 15053541Sshin struct ip6q *q6; 15162587Sitojun struct ip6asfrag *af6, *ip6af, *af6dwn; 152121630Sume#ifdef IN6_IFSTAT_STRICT 153121630Sume struct in6_ifaddr *ia; 154121630Sume#endif 15553541Sshin int offset = *offp, nxt, i, next; 15653541Sshin int first_frag = 0; 15762587Sitojun int fragoff, frgpartlen; /* must be larger than u_int16_t */ 15853541Sshin struct ifnet *dstifp; 159121684Sume u_int8_t ecn, ecn0; 160165118Sbz#if 0 161165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 162165118Sbz#endif 16353541Sshin 16462587Sitojun ip6 = mtod(m, struct ip6_hdr *); 16562587Sitojun#ifndef PULLDOWN_TEST 16653541Sshin IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE); 16753541Sshin ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset); 16862587Sitojun#else 16962587Sitojun IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f)); 17062587Sitojun if (ip6f == NULL) 171120856Sume return (IPPROTO_DONE); 17262587Sitojun#endif 17353541Sshin 17453541Sshin dstifp = NULL; 17553541Sshin#ifdef IN6_IFSTAT_STRICT 17653541Sshin /* find the destination interface of the packet. */ 177121630Sume if ((ia = ip6_getdstifaddr(m)) != NULL) 178121630Sume dstifp = ia->ia_ifp; 17953541Sshin#else 18053541Sshin /* we are violating the spec, this is not the destination interface */ 18153541Sshin if ((m->m_flags & M_PKTHDR) != 0) 18253541Sshin dstifp = m->m_pkthdr.rcvif; 18353541Sshin#endif 18453541Sshin 18553541Sshin /* jumbo payload can't contain a fragment header */ 18653541Sshin if (ip6->ip6_plen == 0) { 18753541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset); 18853541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 18953541Sshin return IPPROTO_DONE; 19053541Sshin } 19153541Sshin 19253541Sshin /* 19353541Sshin * check whether fragment packet's fragment length is 19453541Sshin * multiple of 8 octets. 19553541Sshin * sizeof(struct ip6_frag) == 8 19653541Sshin * sizeof(struct ip6_hdr) = 40 19753541Sshin */ 19853541Sshin if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && 19953541Sshin (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) { 200120891Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 201120891Sume offsetof(struct ip6_hdr, ip6_plen)); 20253541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 20353541Sshin return IPPROTO_DONE; 20453541Sshin } 20553541Sshin 20653541Sshin ip6stat.ip6s_fragments++; 20753541Sshin in6_ifstat_inc(dstifp, ifs6_reass_reqd); 208120891Sume 20962587Sitojun /* offset now points to data portion */ 21053541Sshin offset += sizeof(struct ip6_frag); 21153541Sshin 212121345Sume IP6Q_LOCK(); 21378064Sume 214121345Sume /* 215121345Sume * Enforce upper bound on number of fragments. 216121345Sume * If maxfrag is 0, never accept fragments. 217121345Sume * If maxfrag is -1, accept all fragments without limitation. 218121345Sume */ 219121345Sume if (ip6_maxfrags < 0) 220121345Sume ; 221121345Sume else if (frag6_nfrags >= (u_int)ip6_maxfrags) 222121345Sume goto dropfrag; 223121345Sume 22453541Sshin for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next) 22553541Sshin if (ip6f->ip6f_ident == q6->ip6q_ident && 22653541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && 22753541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)) 22853541Sshin break; 22953541Sshin 23053541Sshin if (q6 == &ip6q) { 23153541Sshin /* 23253541Sshin * the first fragment to arrive, create a reassembly queue. 23353541Sshin */ 23453541Sshin first_frag = 1; 23553541Sshin 23653541Sshin /* 23753541Sshin * Enforce upper bound on number of fragmented packets 23853541Sshin * for which we attempt reassembly; 239121345Sume * If maxfragpackets is 0, never accept fragments. 240121345Sume * If maxfragpackets is -1, accept all fragments without 241121345Sume * limitation. 24253541Sshin */ 24378064Sume if (ip6_maxfragpackets < 0) 24478064Sume ; 24578064Sume else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets) 24678064Sume goto dropfrag; 24778064Sume frag6_nfragpackets++; 24853541Sshin q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE, 249121607Sume M_NOWAIT); 25053541Sshin if (q6 == NULL) 25153541Sshin goto dropfrag; 25262587Sitojun bzero(q6, sizeof(*q6)); 25353541Sshin 25453541Sshin frag6_insque(q6, &ip6q); 25553541Sshin 25662587Sitojun /* ip6q_nxt will be filled afterwards, from 1st fragment */ 25753541Sshin q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6; 25862587Sitojun#ifdef notyet 25962587Sitojun q6->ip6q_nxtp = (u_char *)nxtp; 26062587Sitojun#endif 26153541Sshin q6->ip6q_ident = ip6f->ip6f_ident; 26253541Sshin q6->ip6q_arrive = 0; /* Is it used anywhere? */ 26353541Sshin q6->ip6q_ttl = IPV6_FRAGTTL; 26453541Sshin q6->ip6q_src = ip6->ip6_src; 26553541Sshin q6->ip6q_dst = ip6->ip6_dst; 26653541Sshin q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ 267121345Sume 268121345Sume q6->ip6q_nfrag = 0; 26953541Sshin } 27053541Sshin 27153541Sshin /* 27253541Sshin * If it's the 1st fragment, record the length of the 27353541Sshin * unfragmentable part and the next header of the fragment header. 27453541Sshin */ 27553541Sshin fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); 27653541Sshin if (fragoff == 0) { 277120891Sume q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - 278120891Sume sizeof(struct ip6_frag); 27953541Sshin q6->ip6q_nxt = ip6f->ip6f_nxt; 28053541Sshin } 28153541Sshin 28253541Sshin /* 28353541Sshin * Check that the reassembled packet would not exceed 65535 bytes 28453541Sshin * in size. 28553541Sshin * If it would exceed, discard the fragment and return an ICMP error. 28653541Sshin */ 28762587Sitojun frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; 28853541Sshin if (q6->ip6q_unfrglen >= 0) { 28953541Sshin /* The 1st fragment has already arrived. */ 29053541Sshin if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { 29153541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 292120891Sume offset - sizeof(struct ip6_frag) + 293120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 294121345Sume IP6Q_UNLOCK(); 295120856Sume return (IPPROTO_DONE); 29653541Sshin } 297120891Sume } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { 29853541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 299120891Sume offset - sizeof(struct ip6_frag) + 300120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 301121345Sume IP6Q_UNLOCK(); 302120856Sume return (IPPROTO_DONE); 30353541Sshin } 30453541Sshin /* 30553541Sshin * If it's the first fragment, do the above check for each 30653541Sshin * fragment already stored in the reassembly queue. 30753541Sshin */ 30853541Sshin if (fragoff == 0) { 30953541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 31053541Sshin af6 = af6dwn) { 31153541Sshin af6dwn = af6->ip6af_down; 31253541Sshin 31353541Sshin if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen > 31453541Sshin IPV6_MAXPACKET) { 31553541Sshin struct mbuf *merr = IP6_REASS_MBUF(af6); 31653541Sshin struct ip6_hdr *ip6err; 31753541Sshin int erroff = af6->ip6af_offset; 31853541Sshin 31953541Sshin /* dequeue the fragment. */ 32053541Sshin frag6_deq(af6); 32162587Sitojun free(af6, M_FTABLE); 32253541Sshin 32353541Sshin /* adjust pointer. */ 32453541Sshin ip6err = mtod(merr, struct ip6_hdr *); 32553541Sshin 32653541Sshin /* 32753541Sshin * Restore source and destination addresses 32853541Sshin * in the erroneous IPv6 header. 32953541Sshin */ 33053541Sshin ip6err->ip6_src = q6->ip6q_src; 33153541Sshin ip6err->ip6_dst = q6->ip6q_dst; 33253541Sshin 33353541Sshin icmp6_error(merr, ICMP6_PARAM_PROB, 334120891Sume ICMP6_PARAMPROB_HEADER, 335120891Sume erroff - sizeof(struct ip6_frag) + 336120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 33753541Sshin } 33853541Sshin } 33953541Sshin } 34053541Sshin 34162587Sitojun ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE, 342121607Sume M_NOWAIT); 34362587Sitojun if (ip6af == NULL) 34462587Sitojun goto dropfrag; 34562587Sitojun bzero(ip6af, sizeof(*ip6af)); 34662587Sitojun ip6af->ip6af_head = ip6->ip6_flow; 34762587Sitojun ip6af->ip6af_len = ip6->ip6_plen; 34862587Sitojun ip6af->ip6af_nxt = ip6->ip6_nxt; 34962587Sitojun ip6af->ip6af_hlim = ip6->ip6_hlim; 35053541Sshin ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG; 35153541Sshin ip6af->ip6af_off = fragoff; 35253541Sshin ip6af->ip6af_frglen = frgpartlen; 35353541Sshin ip6af->ip6af_offset = offset; 35453541Sshin IP6_REASS_MBUF(ip6af) = m; 35553541Sshin 35653541Sshin if (first_frag) { 35753541Sshin af6 = (struct ip6asfrag *)q6; 35853541Sshin goto insert; 35953541Sshin } 36053541Sshin 36153541Sshin /* 362121684Sume * Handle ECN by comparing this segment with the first one; 363121684Sume * if CE is set, do not lose CE. 364121684Sume * drop if CE and not-ECT are mixed for the same packet. 365121684Sume */ 366121684Sume ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; 367121684Sume ecn0 = (ntohl(q6->ip6q_down->ip6af_head) >> 20) & IPTOS_ECN_MASK; 368121684Sume if (ecn == IPTOS_ECN_CE) { 369121684Sume if (ecn0 == IPTOS_ECN_NOTECT) { 370121684Sume free(ip6af, M_FTABLE); 371121684Sume goto dropfrag; 372121684Sume } 373121684Sume if (ecn0 != IPTOS_ECN_CE) 374121684Sume q6->ip6q_down->ip6af_head |= htonl(IPTOS_ECN_CE << 20); 375121684Sume } 376121684Sume if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) { 377121684Sume free(ip6af, M_FTABLE); 378121684Sume goto dropfrag; 379121684Sume } 380121684Sume 381121684Sume /* 38253541Sshin * Find a segment which begins after this one does. 38353541Sshin */ 38453541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 38553541Sshin af6 = af6->ip6af_down) 38653541Sshin if (af6->ip6af_off > ip6af->ip6af_off) 38753541Sshin break; 38853541Sshin 38962587Sitojun#if 0 39053541Sshin /* 39162587Sitojun * If there is a preceding segment, it may provide some of 39262587Sitojun * our data already. If so, drop the data from the incoming 39362587Sitojun * segment. If it provides all of our data, drop us. 39462587Sitojun */ 39562587Sitojun if (af6->ip6af_up != (struct ip6asfrag *)q6) { 39662587Sitojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 39762587Sitojun - ip6af->ip6af_off; 39862587Sitojun if (i > 0) { 39962587Sitojun if (i >= ip6af->ip6af_frglen) 40062587Sitojun goto dropfrag; 40162587Sitojun m_adj(IP6_REASS_MBUF(ip6af), i); 40262587Sitojun ip6af->ip6af_off += i; 40362587Sitojun ip6af->ip6af_frglen -= i; 40462587Sitojun } 40562587Sitojun } 40662587Sitojun 40762587Sitojun /* 40862587Sitojun * While we overlap succeeding segments trim them or, 40962587Sitojun * if they are completely covered, dequeue them. 41062587Sitojun */ 41162587Sitojun while (af6 != (struct ip6asfrag *)q6 && 41262587Sitojun ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) { 41362587Sitojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 41462587Sitojun if (i < af6->ip6af_frglen) { 41562587Sitojun af6->ip6af_frglen -= i; 41662587Sitojun af6->ip6af_off += i; 41762587Sitojun m_adj(IP6_REASS_MBUF(af6), i); 41862587Sitojun break; 41962587Sitojun } 42062587Sitojun af6 = af6->ip6af_down; 42162587Sitojun m_freem(IP6_REASS_MBUF(af6->ip6af_up)); 42262587Sitojun frag6_deq(af6->ip6af_up); 42362587Sitojun } 42462587Sitojun#else 42562587Sitojun /* 42653541Sshin * If the incoming framgent overlaps some existing fragments in 42753541Sshin * the reassembly queue, drop it, since it is dangerous to override 42853541Sshin * existing fragments from a security point of view. 429121345Sume * We don't know which fragment is the bad guy - here we trust 430121345Sume * fragment that came in earlier, with no real reason. 43153541Sshin */ 43253541Sshin if (af6->ip6af_up != (struct ip6asfrag *)q6) { 43353541Sshin i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 43453541Sshin - ip6af->ip6af_off; 43553541Sshin if (i > 0) { 43676899Ssumikawa#if 0 /* suppress the noisy log */ 43753541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 43853541Sshin "overlaps the previous fragment\n", 439165118Sbz i, ip6_sprintf(ip6buf, &q6->ip6q_src)); 44076899Ssumikawa#endif 44176899Ssumikawa free(ip6af, M_FTABLE); 44253541Sshin goto dropfrag; 44353541Sshin } 44453541Sshin } 44553541Sshin if (af6 != (struct ip6asfrag *)q6) { 44653541Sshin i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 44753541Sshin if (i > 0) { 44876899Ssumikawa#if 0 /* suppress the noisy log */ 44953541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 45053541Sshin "overlaps the succeeding fragment", 451165118Sbz i, ip6_sprintf(ip6buf, &q6->ip6q_src)); 45276899Ssumikawa#endif 45376899Ssumikawa free(ip6af, M_FTABLE); 45453541Sshin goto dropfrag; 45553541Sshin } 45653541Sshin } 45762587Sitojun#endif 45853541Sshin 45953541Sshininsert: 46053541Sshin 46153541Sshin /* 46253541Sshin * Stick new segment in its place; 46353541Sshin * check for complete reassembly. 46453541Sshin * Move to front of packet queue, as we are 46553541Sshin * the most recently active fragmented packet. 46653541Sshin */ 46753541Sshin frag6_enq(ip6af, af6->ip6af_up); 468121345Sume frag6_nfrags++; 469121345Sume q6->ip6q_nfrag++; 47062587Sitojun#if 0 /* xxx */ 47162587Sitojun if (q6 != ip6q.ip6q_next) { 47262587Sitojun frag6_remque(q6); 47362587Sitojun frag6_insque(q6, &ip6q); 47462587Sitojun } 47562587Sitojun#endif 47653541Sshin next = 0; 47753541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 47853541Sshin af6 = af6->ip6af_down) { 47953541Sshin if (af6->ip6af_off != next) { 480121345Sume IP6Q_UNLOCK(); 48153541Sshin return IPPROTO_DONE; 48253541Sshin } 48353541Sshin next += af6->ip6af_frglen; 48453541Sshin } 48553541Sshin if (af6->ip6af_up->ip6af_mff) { 486121345Sume IP6Q_UNLOCK(); 48753541Sshin return IPPROTO_DONE; 48853541Sshin } 48953541Sshin 49053541Sshin /* 49153541Sshin * Reassembly is complete; concatenate fragments. 49253541Sshin */ 49353541Sshin ip6af = q6->ip6q_down; 49453541Sshin t = m = IP6_REASS_MBUF(ip6af); 49553541Sshin af6 = ip6af->ip6af_down; 49662587Sitojun frag6_deq(ip6af); 49753541Sshin while (af6 != (struct ip6asfrag *)q6) { 49862587Sitojun af6dwn = af6->ip6af_down; 49962587Sitojun frag6_deq(af6); 50053541Sshin while (t->m_next) 50153541Sshin t = t->m_next; 50253541Sshin t->m_next = IP6_REASS_MBUF(af6); 50362587Sitojun m_adj(t->m_next, af6->ip6af_offset); 50462587Sitojun free(af6, M_FTABLE); 50562587Sitojun af6 = af6dwn; 50653541Sshin } 50753541Sshin 50853541Sshin /* adjust offset to point where the original next header starts */ 50953541Sshin offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); 51062587Sitojun free(ip6af, M_FTABLE); 51162587Sitojun ip6 = mtod(m, struct ip6_hdr *); 51253541Sshin ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr)); 51353541Sshin ip6->ip6_src = q6->ip6q_src; 51453541Sshin ip6->ip6_dst = q6->ip6q_dst; 51553541Sshin nxt = q6->ip6q_nxt; 51662587Sitojun#ifdef notyet 51762587Sitojun *q6->ip6q_nxtp = (u_char)(nxt & 0xff); 51862587Sitojun#endif 51953541Sshin 52053541Sshin /* 52153541Sshin * Delete frag6 header with as a few cost as possible. 52253541Sshin */ 52362587Sitojun if (offset < m->m_len) { 52453541Sshin ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag), 52553541Sshin offset); 52662587Sitojun m->m_data += sizeof(struct ip6_frag); 52762587Sitojun m->m_len -= sizeof(struct ip6_frag); 52862587Sitojun } else { 52962587Sitojun /* this comes with no copy if the boundary is on cluster */ 530111119Simp if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) { 53162587Sitojun frag6_remque(q6); 532121345Sume frag6_nfrags -= q6->ip6q_nfrag; 53362587Sitojun free(q6, M_FTABLE); 53462587Sitojun frag6_nfragpackets--; 53562587Sitojun goto dropfrag; 53662587Sitojun } 53762587Sitojun m_adj(t, sizeof(struct ip6_frag)); 53862587Sitojun m_cat(m, t); 53953541Sshin } 54053541Sshin 54153541Sshin /* 54253541Sshin * Store NXT to the original. 54353541Sshin */ 54453541Sshin { 54553541Sshin char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ 54653541Sshin *prvnxtp = nxt; 54753541Sshin } 54853541Sshin 54953541Sshin frag6_remque(q6); 550121345Sume frag6_nfrags -= q6->ip6q_nfrag; 55153541Sshin free(q6, M_FTABLE); 55253541Sshin frag6_nfragpackets--; 55353541Sshin 55453541Sshin if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 55553541Sshin int plen = 0; 55653541Sshin for (t = m; t; t = t->m_next) 55753541Sshin plen += t->m_len; 55853541Sshin m->m_pkthdr.len = plen; 55953541Sshin } 560120891Sume 56153541Sshin ip6stat.ip6s_reassembled++; 56253541Sshin in6_ifstat_inc(dstifp, ifs6_reass_ok); 56353541Sshin 56453541Sshin /* 56553541Sshin * Tell launch routine the next header 56653541Sshin */ 56753541Sshin 56853541Sshin *mp = m; 56953541Sshin *offp = offset; 57053541Sshin 571121345Sume IP6Q_UNLOCK(); 57253541Sshin return nxt; 57353541Sshin 57453541Sshin dropfrag: 575121346Sume IP6Q_UNLOCK(); 57653541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 57753541Sshin ip6stat.ip6s_fragdropped++; 57853541Sshin m_freem(m); 57953541Sshin return IPPROTO_DONE; 58053541Sshin} 58153541Sshin 58253541Sshin/* 58353541Sshin * Free a fragment reassembly header and all 58453541Sshin * associated datagrams. 58553541Sshin */ 58653541Sshinvoid 58753541Sshinfrag6_freef(q6) 58853541Sshin struct ip6q *q6; 58953541Sshin{ 59053541Sshin struct ip6asfrag *af6, *down6; 59153541Sshin 592121355Sume IP6Q_LOCK_ASSERT(); 593121345Sume 59453541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 59553541Sshin af6 = down6) { 59653541Sshin struct mbuf *m = IP6_REASS_MBUF(af6); 59753541Sshin 59853541Sshin down6 = af6->ip6af_down; 59953541Sshin frag6_deq(af6); 60053541Sshin 60153541Sshin /* 60253541Sshin * Return ICMP time exceeded error for the 1st fragment. 60353541Sshin * Just free other fragments. 60453541Sshin */ 60553541Sshin if (af6->ip6af_off == 0) { 60653541Sshin struct ip6_hdr *ip6; 60753541Sshin 60853541Sshin /* adjust pointer */ 60953541Sshin ip6 = mtod(m, struct ip6_hdr *); 61053541Sshin 611120891Sume /* restore source and destination addresses */ 61253541Sshin ip6->ip6_src = q6->ip6q_src; 61353541Sshin ip6->ip6_dst = q6->ip6q_dst; 61453541Sshin 61553541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 61653541Sshin ICMP6_TIME_EXCEED_REASSEMBLY, 0); 61762587Sitojun } else 61853541Sshin m_freem(m); 61962587Sitojun free(af6, M_FTABLE); 62053541Sshin } 62153541Sshin frag6_remque(q6); 622121345Sume frag6_nfrags -= q6->ip6q_nfrag; 62353541Sshin free(q6, M_FTABLE); 62453541Sshin frag6_nfragpackets--; 62553541Sshin} 62653541Sshin 62753541Sshin/* 62853541Sshin * Put an ip fragment on a reassembly chain. 62953541Sshin * Like insque, but pointers in middle of structure. 63053541Sshin */ 63153541Sshinvoid 63253541Sshinfrag6_enq(af6, up6) 63353541Sshin struct ip6asfrag *af6, *up6; 63453541Sshin{ 635121345Sume 636121355Sume IP6Q_LOCK_ASSERT(); 637121345Sume 63853541Sshin af6->ip6af_up = up6; 63953541Sshin af6->ip6af_down = up6->ip6af_down; 64053541Sshin up6->ip6af_down->ip6af_up = af6; 64153541Sshin up6->ip6af_down = af6; 64253541Sshin} 64353541Sshin 64453541Sshin/* 64553541Sshin * To frag6_enq as remque is to insque. 64653541Sshin */ 64753541Sshinvoid 64853541Sshinfrag6_deq(af6) 64953541Sshin struct ip6asfrag *af6; 65053541Sshin{ 651121345Sume 652121355Sume IP6Q_LOCK_ASSERT(); 653121345Sume 65453541Sshin af6->ip6af_up->ip6af_down = af6->ip6af_down; 65553541Sshin af6->ip6af_down->ip6af_up = af6->ip6af_up; 65653541Sshin} 65753541Sshin 65853541Sshinvoid 65953541Sshinfrag6_insque(new, old) 66053541Sshin struct ip6q *new, *old; 66153541Sshin{ 662121345Sume 663121355Sume IP6Q_LOCK_ASSERT(); 664121345Sume 66553541Sshin new->ip6q_prev = old; 66653541Sshin new->ip6q_next = old->ip6q_next; 66753541Sshin old->ip6q_next->ip6q_prev= new; 66853541Sshin old->ip6q_next = new; 66953541Sshin} 67053541Sshin 67153541Sshinvoid 67253541Sshinfrag6_remque(p6) 67353541Sshin struct ip6q *p6; 67453541Sshin{ 675121345Sume 676121355Sume IP6Q_LOCK_ASSERT(); 677121345Sume 67853541Sshin p6->ip6q_prev->ip6q_next = p6->ip6q_next; 67953541Sshin p6->ip6q_next->ip6q_prev = p6->ip6q_prev; 68053541Sshin} 68153541Sshin 68253541Sshin/* 68378064Sume * IPv6 reassembling timer processing; 68453541Sshin * if a timer expires on a reassembly 68553541Sshin * queue, discard it. 68653541Sshin */ 68753541Sshinvoid 68853541Sshinfrag6_slowtimo() 68953541Sshin{ 69053541Sshin struct ip6q *q6; 69153541Sshin 692158295Sbz#if 0 693158295Sbz GIANT_REQUIRED; /* XXX bz: ip6_forward_rt */ 694158295Sbz#endif 695158295Sbz 696121345Sume IP6Q_LOCK(); 69753541Sshin q6 = ip6q.ip6q_next; 69853541Sshin if (q6) 69953541Sshin while (q6 != &ip6q) { 70053541Sshin --q6->ip6q_ttl; 70153541Sshin q6 = q6->ip6q_next; 70253541Sshin if (q6->ip6q_prev->ip6q_ttl == 0) { 70353541Sshin ip6stat.ip6s_fragtimeout++; 70453541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 70553541Sshin frag6_freef(q6->ip6q_prev); 70653541Sshin } 70753541Sshin } 70853541Sshin /* 70953541Sshin * If we are over the maximum number of fragments 71053541Sshin * (due to the limit being lowered), drain off 71153541Sshin * enough to get down to the new limit. 71253541Sshin */ 71378064Sume while (frag6_nfragpackets > (u_int)ip6_maxfragpackets && 71478064Sume ip6q.ip6q_prev) { 71553541Sshin ip6stat.ip6s_fragoverflow++; 71653541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 71753541Sshin frag6_freef(ip6q.ip6q_prev); 71853541Sshin } 719121345Sume IP6Q_UNLOCK(); 72062587Sitojun 72162587Sitojun#if 0 72262587Sitojun /* 72362587Sitojun * Routing changes might produce a better route than we last used; 72462587Sitojun * make sure we notice eventually, even if forwarding only for one 72562587Sitojun * destination and the cache is never replaced. 72662587Sitojun */ 72762587Sitojun if (ip6_forward_rt.ro_rt) { 72862587Sitojun RTFREE(ip6_forward_rt.ro_rt); 72962587Sitojun ip6_forward_rt.ro_rt = 0; 73062587Sitojun } 73162587Sitojun if (ipsrcchk_rt.ro_rt) { 73262587Sitojun RTFREE(ipsrcchk_rt.ro_rt); 73362587Sitojun ipsrcchk_rt.ro_rt = 0; 73462587Sitojun } 73562587Sitojun#endif 73653541Sshin} 73753541Sshin 73853541Sshin/* 73953541Sshin * Drain off all datagram fragments. 74053541Sshin */ 74153541Sshinvoid 74253541Sshinfrag6_drain() 74353541Sshin{ 744121345Sume 745121346Sume if (IP6Q_TRYLOCK() == 0) 74653541Sshin return; 74753541Sshin while (ip6q.ip6q_next != &ip6q) { 74853541Sshin ip6stat.ip6s_fragdropped++; 74953541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 75053541Sshin frag6_freef(ip6q.ip6q_next); 75153541Sshin } 752121345Sume IP6Q_UNLOCK(); 75353541Sshin} 754