frag6.c revision 157927
162587Sitojun/* $FreeBSD: head/sys/netinet6/frag6.c 157927 2006-04-21 09:25:40Z ps $ */ 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#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 71121346Sumestatic struct mtx ip6qlock; 72121346Sume/* 73121346Sume * These fields all protected by ip6qlock. 74121346Sume */ 75121346Sumestatic u_int frag6_nfragpackets; 76121346Sumestatic u_int frag6_nfrags; 77121346Sumestatic struct ip6q ip6q; /* ip6 reassemble queue */ 7853541Sshin 79121346Sume#define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF); 80121346Sume#define IP6Q_LOCK() mtx_lock(&ip6qlock) 81121346Sume#define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock) 82121355Sume#define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED) 83121346Sume#define IP6Q_UNLOCK() mtx_unlock(&ip6qlock) 84121345Sume 8569774Sphkstatic MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header"); 8662587Sitojun 8753541Sshin/* 8853541Sshin * Initialise reassembly queue and fragment identifier. 8953541Sshin */ 90157927Spsstatic void 91157927Spsfrag6_change(void *tag) 92157927Sps{ 93157927Sps 94157927Sps ip6_maxfragpackets = nmbclusters / 4; 95157927Sps ip6_maxfrags = nmbclusters / 4; 96157927Sps} 97157927Sps 9853541Sshinvoid 9953541Sshinfrag6_init() 10053541Sshin{ 10153541Sshin 10277969Sjesper ip6_maxfragpackets = nmbclusters / 4; 103121345Sume ip6_maxfrags = nmbclusters / 4; 104157927Sps EVENTHANDLER_REGISTER(nmbclusters_change, 105157927Sps frag6_change, NULL, EVENTHANDLER_PRI_ANY); 10677969Sjesper 107121346Sume IP6Q_LOCK_INIT(); 108121346Sume 10953541Sshin ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q; 11053541Sshin} 11153541Sshin 11253541Sshin/* 11362587Sitojun * In RFC2460, fragment and reassembly rule do not agree with each other, 11462587Sitojun * in terms of next header field handling in fragment header. 11562587Sitojun * While the sender will use the same value for all of the fragmented packets, 11662587Sitojun * receiver is suggested not to check the consistency. 11762587Sitojun * 11862587Sitojun * fragment rule (p20): 11962587Sitojun * (2) A Fragment header containing: 12062587Sitojun * The Next Header value that identifies the first header of 12162587Sitojun * the Fragmentable Part of the original packet. 12262587Sitojun * -> next header field is same for all fragments 12362587Sitojun * 12462587Sitojun * reassembly rule (p21): 12562587Sitojun * The Next Header field of the last header of the Unfragmentable 12662587Sitojun * Part is obtained from the Next Header field of the first 12762587Sitojun * fragment's Fragment header. 12862587Sitojun * -> should grab it from the first fragment only 12962587Sitojun * 13062587Sitojun * The following note also contradicts with fragment rule - noone is going to 13162587Sitojun * send different fragment with different next header field. 13262587Sitojun * 13362587Sitojun * additional note (p22): 13462587Sitojun * The Next Header values in the Fragment headers of different 13562587Sitojun * fragments of the same original packet may differ. Only the value 13662587Sitojun * from the Offset zero fragment packet is used for reassembly. 13762587Sitojun * -> should grab it from the first fragment only 13862587Sitojun * 13962587Sitojun * There is no explicit reason given in the RFC. Historical reason maybe? 14062587Sitojun */ 14162587Sitojun/* 14253541Sshin * Fragment input 14353541Sshin */ 14453541Sshinint 14553541Sshinfrag6_input(mp, offp, proto) 14653541Sshin struct mbuf **mp; 14753541Sshin int *offp, proto; 14853541Sshin{ 14953541Sshin struct mbuf *m = *mp, *t; 15053541Sshin struct ip6_hdr *ip6; 15153541Sshin struct ip6_frag *ip6f; 15253541Sshin struct ip6q *q6; 15362587Sitojun struct ip6asfrag *af6, *ip6af, *af6dwn; 154121630Sume#ifdef IN6_IFSTAT_STRICT 155121630Sume struct in6_ifaddr *ia; 156121630Sume#endif 15753541Sshin int offset = *offp, nxt, i, next; 15853541Sshin int first_frag = 0; 15962587Sitojun int fragoff, frgpartlen; /* must be larger than u_int16_t */ 16053541Sshin struct ifnet *dstifp; 161121684Sume u_int8_t ecn, ecn0; 16253541Sshin 16362587Sitojun ip6 = mtod(m, struct ip6_hdr *); 16462587Sitojun#ifndef PULLDOWN_TEST 16553541Sshin IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE); 16653541Sshin ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset); 16762587Sitojun#else 16862587Sitojun IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f)); 16962587Sitojun if (ip6f == NULL) 170120856Sume return (IPPROTO_DONE); 17162587Sitojun#endif 17253541Sshin 17353541Sshin dstifp = NULL; 17453541Sshin#ifdef IN6_IFSTAT_STRICT 17553541Sshin /* find the destination interface of the packet. */ 176121630Sume if ((ia = ip6_getdstifaddr(m)) != NULL) 177121630Sume dstifp = ia->ia_ifp; 17853541Sshin#else 17953541Sshin /* we are violating the spec, this is not the destination interface */ 18053541Sshin if ((m->m_flags & M_PKTHDR) != 0) 18153541Sshin dstifp = m->m_pkthdr.rcvif; 18253541Sshin#endif 18353541Sshin 18453541Sshin /* jumbo payload can't contain a fragment header */ 18553541Sshin if (ip6->ip6_plen == 0) { 18653541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset); 18753541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 18853541Sshin return IPPROTO_DONE; 18953541Sshin } 19053541Sshin 19153541Sshin /* 19253541Sshin * check whether fragment packet's fragment length is 19353541Sshin * multiple of 8 octets. 19453541Sshin * sizeof(struct ip6_frag) == 8 19553541Sshin * sizeof(struct ip6_hdr) = 40 19653541Sshin */ 19753541Sshin if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && 19853541Sshin (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) { 199120891Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 200120891Sume offsetof(struct ip6_hdr, ip6_plen)); 20153541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 20253541Sshin return IPPROTO_DONE; 20353541Sshin } 20453541Sshin 20553541Sshin ip6stat.ip6s_fragments++; 20653541Sshin in6_ifstat_inc(dstifp, ifs6_reass_reqd); 207120891Sume 20862587Sitojun /* offset now points to data portion */ 20953541Sshin offset += sizeof(struct ip6_frag); 21053541Sshin 211121345Sume IP6Q_LOCK(); 21278064Sume 213121345Sume /* 214121345Sume * Enforce upper bound on number of fragments. 215121345Sume * If maxfrag is 0, never accept fragments. 216121345Sume * If maxfrag is -1, accept all fragments without limitation. 217121345Sume */ 218121345Sume if (ip6_maxfrags < 0) 219121345Sume ; 220121345Sume else if (frag6_nfrags >= (u_int)ip6_maxfrags) 221121345Sume goto dropfrag; 222121345Sume 22353541Sshin for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next) 22453541Sshin if (ip6f->ip6f_ident == q6->ip6q_ident && 22553541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && 22653541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)) 22753541Sshin break; 22853541Sshin 22953541Sshin if (q6 == &ip6q) { 23053541Sshin /* 23153541Sshin * the first fragment to arrive, create a reassembly queue. 23253541Sshin */ 23353541Sshin first_frag = 1; 23453541Sshin 23553541Sshin /* 23653541Sshin * Enforce upper bound on number of fragmented packets 23753541Sshin * for which we attempt reassembly; 238121345Sume * If maxfragpackets is 0, never accept fragments. 239121345Sume * If maxfragpackets is -1, accept all fragments without 240121345Sume * limitation. 24153541Sshin */ 24278064Sume if (ip6_maxfragpackets < 0) 24378064Sume ; 24478064Sume else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets) 24578064Sume goto dropfrag; 24678064Sume frag6_nfragpackets++; 24753541Sshin q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE, 248121607Sume M_NOWAIT); 24953541Sshin if (q6 == NULL) 25053541Sshin goto dropfrag; 25162587Sitojun bzero(q6, sizeof(*q6)); 25253541Sshin 25353541Sshin frag6_insque(q6, &ip6q); 25453541Sshin 25562587Sitojun /* ip6q_nxt will be filled afterwards, from 1st fragment */ 25653541Sshin q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6; 25762587Sitojun#ifdef notyet 25862587Sitojun q6->ip6q_nxtp = (u_char *)nxtp; 25962587Sitojun#endif 26053541Sshin q6->ip6q_ident = ip6f->ip6f_ident; 26153541Sshin q6->ip6q_arrive = 0; /* Is it used anywhere? */ 26253541Sshin q6->ip6q_ttl = IPV6_FRAGTTL; 26353541Sshin q6->ip6q_src = ip6->ip6_src; 26453541Sshin q6->ip6q_dst = ip6->ip6_dst; 26553541Sshin q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ 266121345Sume 267121345Sume q6->ip6q_nfrag = 0; 26853541Sshin } 26953541Sshin 27053541Sshin /* 27153541Sshin * If it's the 1st fragment, record the length of the 27253541Sshin * unfragmentable part and the next header of the fragment header. 27353541Sshin */ 27453541Sshin fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); 27553541Sshin if (fragoff == 0) { 276120891Sume q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - 277120891Sume sizeof(struct ip6_frag); 27853541Sshin q6->ip6q_nxt = ip6f->ip6f_nxt; 27953541Sshin } 28053541Sshin 28153541Sshin /* 28253541Sshin * Check that the reassembled packet would not exceed 65535 bytes 28353541Sshin * in size. 28453541Sshin * If it would exceed, discard the fragment and return an ICMP error. 28553541Sshin */ 28662587Sitojun frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; 28753541Sshin if (q6->ip6q_unfrglen >= 0) { 28853541Sshin /* The 1st fragment has already arrived. */ 28953541Sshin if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { 29053541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 291120891Sume offset - sizeof(struct ip6_frag) + 292120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 293121345Sume IP6Q_UNLOCK(); 294120856Sume return (IPPROTO_DONE); 29553541Sshin } 296120891Sume } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { 29753541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 298120891Sume offset - sizeof(struct ip6_frag) + 299120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 300121345Sume IP6Q_UNLOCK(); 301120856Sume return (IPPROTO_DONE); 30253541Sshin } 30353541Sshin /* 30453541Sshin * If it's the first fragment, do the above check for each 30553541Sshin * fragment already stored in the reassembly queue. 30653541Sshin */ 30753541Sshin if (fragoff == 0) { 30853541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 30953541Sshin af6 = af6dwn) { 31053541Sshin af6dwn = af6->ip6af_down; 31153541Sshin 31253541Sshin if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen > 31353541Sshin IPV6_MAXPACKET) { 31453541Sshin struct mbuf *merr = IP6_REASS_MBUF(af6); 31553541Sshin struct ip6_hdr *ip6err; 31653541Sshin int erroff = af6->ip6af_offset; 31753541Sshin 31853541Sshin /* dequeue the fragment. */ 31953541Sshin frag6_deq(af6); 32062587Sitojun free(af6, M_FTABLE); 32153541Sshin 32253541Sshin /* adjust pointer. */ 32353541Sshin ip6err = mtod(merr, struct ip6_hdr *); 32453541Sshin 32553541Sshin /* 32653541Sshin * Restore source and destination addresses 32753541Sshin * in the erroneous IPv6 header. 32853541Sshin */ 32953541Sshin ip6err->ip6_src = q6->ip6q_src; 33053541Sshin ip6err->ip6_dst = q6->ip6q_dst; 33153541Sshin 33253541Sshin icmp6_error(merr, ICMP6_PARAM_PROB, 333120891Sume ICMP6_PARAMPROB_HEADER, 334120891Sume erroff - sizeof(struct ip6_frag) + 335120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 33653541Sshin } 33753541Sshin } 33853541Sshin } 33953541Sshin 34062587Sitojun ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE, 341121607Sume M_NOWAIT); 34262587Sitojun if (ip6af == NULL) 34362587Sitojun goto dropfrag; 34462587Sitojun bzero(ip6af, sizeof(*ip6af)); 34562587Sitojun ip6af->ip6af_head = ip6->ip6_flow; 34662587Sitojun ip6af->ip6af_len = ip6->ip6_plen; 34762587Sitojun ip6af->ip6af_nxt = ip6->ip6_nxt; 34862587Sitojun ip6af->ip6af_hlim = ip6->ip6_hlim; 34953541Sshin ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG; 35053541Sshin ip6af->ip6af_off = fragoff; 35153541Sshin ip6af->ip6af_frglen = frgpartlen; 35253541Sshin ip6af->ip6af_offset = offset; 35353541Sshin IP6_REASS_MBUF(ip6af) = m; 35453541Sshin 35553541Sshin if (first_frag) { 35653541Sshin af6 = (struct ip6asfrag *)q6; 35753541Sshin goto insert; 35853541Sshin } 35953541Sshin 36053541Sshin /* 361121684Sume * Handle ECN by comparing this segment with the first one; 362121684Sume * if CE is set, do not lose CE. 363121684Sume * drop if CE and not-ECT are mixed for the same packet. 364121684Sume */ 365121684Sume ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; 366121684Sume ecn0 = (ntohl(q6->ip6q_down->ip6af_head) >> 20) & IPTOS_ECN_MASK; 367121684Sume if (ecn == IPTOS_ECN_CE) { 368121684Sume if (ecn0 == IPTOS_ECN_NOTECT) { 369121684Sume free(ip6af, M_FTABLE); 370121684Sume goto dropfrag; 371121684Sume } 372121684Sume if (ecn0 != IPTOS_ECN_CE) 373121684Sume q6->ip6q_down->ip6af_head |= htonl(IPTOS_ECN_CE << 20); 374121684Sume } 375121684Sume if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) { 376121684Sume free(ip6af, M_FTABLE); 377121684Sume goto dropfrag; 378121684Sume } 379121684Sume 380121684Sume /* 38153541Sshin * Find a segment which begins after this one does. 38253541Sshin */ 38353541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 38453541Sshin af6 = af6->ip6af_down) 38553541Sshin if (af6->ip6af_off > ip6af->ip6af_off) 38653541Sshin break; 38753541Sshin 38862587Sitojun#if 0 38953541Sshin /* 39062587Sitojun * If there is a preceding segment, it may provide some of 39162587Sitojun * our data already. If so, drop the data from the incoming 39262587Sitojun * segment. If it provides all of our data, drop us. 39362587Sitojun */ 39462587Sitojun if (af6->ip6af_up != (struct ip6asfrag *)q6) { 39562587Sitojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 39662587Sitojun - ip6af->ip6af_off; 39762587Sitojun if (i > 0) { 39862587Sitojun if (i >= ip6af->ip6af_frglen) 39962587Sitojun goto dropfrag; 40062587Sitojun m_adj(IP6_REASS_MBUF(ip6af), i); 40162587Sitojun ip6af->ip6af_off += i; 40262587Sitojun ip6af->ip6af_frglen -= i; 40362587Sitojun } 40462587Sitojun } 40562587Sitojun 40662587Sitojun /* 40762587Sitojun * While we overlap succeeding segments trim them or, 40862587Sitojun * if they are completely covered, dequeue them. 40962587Sitojun */ 41062587Sitojun while (af6 != (struct ip6asfrag *)q6 && 41162587Sitojun ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) { 41262587Sitojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 41362587Sitojun if (i < af6->ip6af_frglen) { 41462587Sitojun af6->ip6af_frglen -= i; 41562587Sitojun af6->ip6af_off += i; 41662587Sitojun m_adj(IP6_REASS_MBUF(af6), i); 41762587Sitojun break; 41862587Sitojun } 41962587Sitojun af6 = af6->ip6af_down; 42062587Sitojun m_freem(IP6_REASS_MBUF(af6->ip6af_up)); 42162587Sitojun frag6_deq(af6->ip6af_up); 42262587Sitojun } 42362587Sitojun#else 42462587Sitojun /* 42553541Sshin * If the incoming framgent overlaps some existing fragments in 42653541Sshin * the reassembly queue, drop it, since it is dangerous to override 42753541Sshin * existing fragments from a security point of view. 428121345Sume * We don't know which fragment is the bad guy - here we trust 429121345Sume * fragment that came in earlier, with no real reason. 43053541Sshin */ 43153541Sshin if (af6->ip6af_up != (struct ip6asfrag *)q6) { 43253541Sshin i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 43353541Sshin - ip6af->ip6af_off; 43453541Sshin if (i > 0) { 43576899Ssumikawa#if 0 /* suppress the noisy log */ 43653541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 43753541Sshin "overlaps the previous fragment\n", 43853541Sshin i, ip6_sprintf(&q6->ip6q_src)); 43976899Ssumikawa#endif 44076899Ssumikawa free(ip6af, M_FTABLE); 44153541Sshin goto dropfrag; 44253541Sshin } 44353541Sshin } 44453541Sshin if (af6 != (struct ip6asfrag *)q6) { 44553541Sshin i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 44653541Sshin if (i > 0) { 44776899Ssumikawa#if 0 /* suppress the noisy log */ 44853541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 44953541Sshin "overlaps the succeeding fragment", 45053541Sshin i, ip6_sprintf(&q6->ip6q_src)); 45176899Ssumikawa#endif 45276899Ssumikawa free(ip6af, M_FTABLE); 45353541Sshin goto dropfrag; 45453541Sshin } 45553541Sshin } 45662587Sitojun#endif 45753541Sshin 45853541Sshininsert: 45953541Sshin 46053541Sshin /* 46153541Sshin * Stick new segment in its place; 46253541Sshin * check for complete reassembly. 46353541Sshin * Move to front of packet queue, as we are 46453541Sshin * the most recently active fragmented packet. 46553541Sshin */ 46653541Sshin frag6_enq(ip6af, af6->ip6af_up); 467121345Sume frag6_nfrags++; 468121345Sume q6->ip6q_nfrag++; 46962587Sitojun#if 0 /* xxx */ 47062587Sitojun if (q6 != ip6q.ip6q_next) { 47162587Sitojun frag6_remque(q6); 47262587Sitojun frag6_insque(q6, &ip6q); 47362587Sitojun } 47462587Sitojun#endif 47553541Sshin next = 0; 47653541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 47753541Sshin af6 = af6->ip6af_down) { 47853541Sshin if (af6->ip6af_off != next) { 479121345Sume IP6Q_UNLOCK(); 48053541Sshin return IPPROTO_DONE; 48153541Sshin } 48253541Sshin next += af6->ip6af_frglen; 48353541Sshin } 48453541Sshin if (af6->ip6af_up->ip6af_mff) { 485121345Sume IP6Q_UNLOCK(); 48653541Sshin return IPPROTO_DONE; 48753541Sshin } 48853541Sshin 48953541Sshin /* 49053541Sshin * Reassembly is complete; concatenate fragments. 49153541Sshin */ 49253541Sshin ip6af = q6->ip6q_down; 49353541Sshin t = m = IP6_REASS_MBUF(ip6af); 49453541Sshin af6 = ip6af->ip6af_down; 49562587Sitojun frag6_deq(ip6af); 49653541Sshin while (af6 != (struct ip6asfrag *)q6) { 49762587Sitojun af6dwn = af6->ip6af_down; 49862587Sitojun frag6_deq(af6); 49953541Sshin while (t->m_next) 50053541Sshin t = t->m_next; 50153541Sshin t->m_next = IP6_REASS_MBUF(af6); 50262587Sitojun m_adj(t->m_next, af6->ip6af_offset); 50362587Sitojun free(af6, M_FTABLE); 50462587Sitojun af6 = af6dwn; 50553541Sshin } 50653541Sshin 50753541Sshin /* adjust offset to point where the original next header starts */ 50853541Sshin offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); 50962587Sitojun free(ip6af, M_FTABLE); 51062587Sitojun ip6 = mtod(m, struct ip6_hdr *); 51153541Sshin ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr)); 51253541Sshin ip6->ip6_src = q6->ip6q_src; 51353541Sshin ip6->ip6_dst = q6->ip6q_dst; 51453541Sshin nxt = q6->ip6q_nxt; 51562587Sitojun#ifdef notyet 51662587Sitojun *q6->ip6q_nxtp = (u_char)(nxt & 0xff); 51762587Sitojun#endif 51853541Sshin 51953541Sshin /* 52053541Sshin * Delete frag6 header with as a few cost as possible. 52153541Sshin */ 52262587Sitojun if (offset < m->m_len) { 52353541Sshin ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag), 52453541Sshin offset); 52562587Sitojun m->m_data += sizeof(struct ip6_frag); 52662587Sitojun m->m_len -= sizeof(struct ip6_frag); 52762587Sitojun } else { 52862587Sitojun /* this comes with no copy if the boundary is on cluster */ 529111119Simp if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) { 53062587Sitojun frag6_remque(q6); 531121345Sume frag6_nfrags -= q6->ip6q_nfrag; 53262587Sitojun free(q6, M_FTABLE); 53362587Sitojun frag6_nfragpackets--; 53462587Sitojun goto dropfrag; 53562587Sitojun } 53662587Sitojun m_adj(t, sizeof(struct ip6_frag)); 53762587Sitojun m_cat(m, t); 53853541Sshin } 53953541Sshin 54053541Sshin /* 54153541Sshin * Store NXT to the original. 54253541Sshin */ 54353541Sshin { 54453541Sshin char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ 54553541Sshin *prvnxtp = nxt; 54653541Sshin } 54753541Sshin 54853541Sshin frag6_remque(q6); 549121345Sume frag6_nfrags -= q6->ip6q_nfrag; 55053541Sshin free(q6, M_FTABLE); 55153541Sshin frag6_nfragpackets--; 55253541Sshin 55353541Sshin if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 55453541Sshin int plen = 0; 55553541Sshin for (t = m; t; t = t->m_next) 55653541Sshin plen += t->m_len; 55753541Sshin m->m_pkthdr.len = plen; 55853541Sshin } 559120891Sume 56053541Sshin ip6stat.ip6s_reassembled++; 56153541Sshin in6_ifstat_inc(dstifp, ifs6_reass_ok); 56253541Sshin 56353541Sshin /* 56453541Sshin * Tell launch routine the next header 56553541Sshin */ 56653541Sshin 56753541Sshin *mp = m; 56853541Sshin *offp = offset; 56953541Sshin 570121345Sume IP6Q_UNLOCK(); 57153541Sshin return nxt; 57253541Sshin 57353541Sshin dropfrag: 574121346Sume IP6Q_UNLOCK(); 57553541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 57653541Sshin ip6stat.ip6s_fragdropped++; 57753541Sshin m_freem(m); 57853541Sshin return IPPROTO_DONE; 57953541Sshin} 58053541Sshin 58153541Sshin/* 58253541Sshin * Free a fragment reassembly header and all 58353541Sshin * associated datagrams. 58453541Sshin */ 58553541Sshinvoid 58653541Sshinfrag6_freef(q6) 58753541Sshin struct ip6q *q6; 58853541Sshin{ 58953541Sshin struct ip6asfrag *af6, *down6; 59053541Sshin 591121355Sume IP6Q_LOCK_ASSERT(); 592121345Sume 59353541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 59453541Sshin af6 = down6) { 59553541Sshin struct mbuf *m = IP6_REASS_MBUF(af6); 59653541Sshin 59753541Sshin down6 = af6->ip6af_down; 59853541Sshin frag6_deq(af6); 59953541Sshin 60053541Sshin /* 60153541Sshin * Return ICMP time exceeded error for the 1st fragment. 60253541Sshin * Just free other fragments. 60353541Sshin */ 60453541Sshin if (af6->ip6af_off == 0) { 60553541Sshin struct ip6_hdr *ip6; 60653541Sshin 60753541Sshin /* adjust pointer */ 60853541Sshin ip6 = mtod(m, struct ip6_hdr *); 60953541Sshin 610120891Sume /* restore source and destination addresses */ 61153541Sshin ip6->ip6_src = q6->ip6q_src; 61253541Sshin ip6->ip6_dst = q6->ip6q_dst; 61353541Sshin 61453541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 61553541Sshin ICMP6_TIME_EXCEED_REASSEMBLY, 0); 61662587Sitojun } else 61753541Sshin m_freem(m); 61862587Sitojun free(af6, M_FTABLE); 61953541Sshin } 62053541Sshin frag6_remque(q6); 621121345Sume frag6_nfrags -= q6->ip6q_nfrag; 62253541Sshin free(q6, M_FTABLE); 62353541Sshin frag6_nfragpackets--; 62453541Sshin} 62553541Sshin 62653541Sshin/* 62753541Sshin * Put an ip fragment on a reassembly chain. 62853541Sshin * Like insque, but pointers in middle of structure. 62953541Sshin */ 63053541Sshinvoid 63153541Sshinfrag6_enq(af6, up6) 63253541Sshin struct ip6asfrag *af6, *up6; 63353541Sshin{ 634121345Sume 635121355Sume IP6Q_LOCK_ASSERT(); 636121345Sume 63753541Sshin af6->ip6af_up = up6; 63853541Sshin af6->ip6af_down = up6->ip6af_down; 63953541Sshin up6->ip6af_down->ip6af_up = af6; 64053541Sshin up6->ip6af_down = af6; 64153541Sshin} 64253541Sshin 64353541Sshin/* 64453541Sshin * To frag6_enq as remque is to insque. 64553541Sshin */ 64653541Sshinvoid 64753541Sshinfrag6_deq(af6) 64853541Sshin struct ip6asfrag *af6; 64953541Sshin{ 650121345Sume 651121355Sume IP6Q_LOCK_ASSERT(); 652121345Sume 65353541Sshin af6->ip6af_up->ip6af_down = af6->ip6af_down; 65453541Sshin af6->ip6af_down->ip6af_up = af6->ip6af_up; 65553541Sshin} 65653541Sshin 65753541Sshinvoid 65853541Sshinfrag6_insque(new, old) 65953541Sshin struct ip6q *new, *old; 66053541Sshin{ 661121345Sume 662121355Sume IP6Q_LOCK_ASSERT(); 663121345Sume 66453541Sshin new->ip6q_prev = old; 66553541Sshin new->ip6q_next = old->ip6q_next; 66653541Sshin old->ip6q_next->ip6q_prev= new; 66753541Sshin old->ip6q_next = new; 66853541Sshin} 66953541Sshin 67053541Sshinvoid 67153541Sshinfrag6_remque(p6) 67253541Sshin struct ip6q *p6; 67353541Sshin{ 674121345Sume 675121355Sume IP6Q_LOCK_ASSERT(); 676121345Sume 67753541Sshin p6->ip6q_prev->ip6q_next = p6->ip6q_next; 67853541Sshin p6->ip6q_next->ip6q_prev = p6->ip6q_prev; 67953541Sshin} 68053541Sshin 68153541Sshin/* 68278064Sume * IPv6 reassembling timer processing; 68353541Sshin * if a timer expires on a reassembly 68453541Sshin * queue, discard it. 68553541Sshin */ 68653541Sshinvoid 68753541Sshinfrag6_slowtimo() 68853541Sshin{ 68953541Sshin struct ip6q *q6; 69053541Sshin 691121345Sume IP6Q_LOCK(); 69253541Sshin q6 = ip6q.ip6q_next; 69353541Sshin if (q6) 69453541Sshin while (q6 != &ip6q) { 69553541Sshin --q6->ip6q_ttl; 69653541Sshin q6 = q6->ip6q_next; 69753541Sshin if (q6->ip6q_prev->ip6q_ttl == 0) { 69853541Sshin ip6stat.ip6s_fragtimeout++; 69953541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 70053541Sshin frag6_freef(q6->ip6q_prev); 70153541Sshin } 70253541Sshin } 70353541Sshin /* 70453541Sshin * If we are over the maximum number of fragments 70553541Sshin * (due to the limit being lowered), drain off 70653541Sshin * enough to get down to the new limit. 70753541Sshin */ 70878064Sume while (frag6_nfragpackets > (u_int)ip6_maxfragpackets && 70978064Sume ip6q.ip6q_prev) { 71053541Sshin ip6stat.ip6s_fragoverflow++; 71153541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 71253541Sshin frag6_freef(ip6q.ip6q_prev); 71353541Sshin } 714121345Sume IP6Q_UNLOCK(); 71562587Sitojun 71662587Sitojun#if 0 71762587Sitojun /* 71862587Sitojun * Routing changes might produce a better route than we last used; 71962587Sitojun * make sure we notice eventually, even if forwarding only for one 72062587Sitojun * destination and the cache is never replaced. 72162587Sitojun */ 72262587Sitojun if (ip6_forward_rt.ro_rt) { 72362587Sitojun RTFREE(ip6_forward_rt.ro_rt); 72462587Sitojun ip6_forward_rt.ro_rt = 0; 72562587Sitojun } 72662587Sitojun if (ipsrcchk_rt.ro_rt) { 72762587Sitojun RTFREE(ipsrcchk_rt.ro_rt); 72862587Sitojun ipsrcchk_rt.ro_rt = 0; 72962587Sitojun } 73062587Sitojun#endif 73153541Sshin} 73253541Sshin 73353541Sshin/* 73453541Sshin * Drain off all datagram fragments. 73553541Sshin */ 73653541Sshinvoid 73753541Sshinfrag6_drain() 73853541Sshin{ 739121345Sume 740121346Sume if (IP6Q_TRYLOCK() == 0) 74153541Sshin return; 74253541Sshin while (ip6q.ip6q_next != &ip6q) { 74353541Sshin ip6stat.ip6s_fragdropped++; 74453541Sshin /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 74553541Sshin frag6_freef(ip6q.ip6q_next); 74653541Sshin } 747121345Sume IP6Q_UNLOCK(); 74853541Sshin} 749