frag6.c revision 207369
1139826Simp/*- 253541Sshin * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 353541Sshin * All rights reserved. 453541Sshin * 553541Sshin * Redistribution and use in source and binary forms, with or without 653541Sshin * modification, are permitted provided that the following conditions 753541Sshin * are met: 853541Sshin * 1. Redistributions of source code must retain the above copyright 953541Sshin * notice, this list of conditions and the following disclaimer. 1053541Sshin * 2. Redistributions in binary form must reproduce the above copyright 1153541Sshin * notice, this list of conditions and the following disclaimer in the 1253541Sshin * documentation and/or other materials provided with the distribution. 1353541Sshin * 3. Neither the name of the project nor the names of its contributors 1453541Sshin * may be used to endorse or promote products derived from this software 1553541Sshin * without specific prior written permission. 1653541Sshin * 1753541Sshin * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 1853541Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1953541Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2053541Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2153541Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2253541Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2353541Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2453541Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2553541Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2653541Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2753541Sshin * SUCH DAMAGE. 28174510Sobrien * 29174510Sobrien * $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $ 3053541Sshin */ 3153541Sshin 32174510Sobrien#include <sys/cdefs.h> 33174510Sobrien__FBSDID("$FreeBSD: head/sys/netinet6/frag6.c 207369 2010-04-29 11:52:42Z bz $"); 34174510Sobrien 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> 49195699Srwatson#include <net/vnet.h> 5053541Sshin 5153541Sshin#include <netinet/in.h> 5253541Sshin#include <netinet/in_var.h> 5362587Sitojun#include <netinet/ip6.h> 5453541Sshin#include <netinet6/ip6_var.h> 5562587Sitojun#include <netinet/icmp6.h> 56121684Sume#include <netinet/in_systm.h> /* for ECN definitions */ 57121684Sume#include <netinet/ip.h> /* for ECN definitions */ 5853541Sshin 59184307Srwatson#include <security/mac/mac_framework.h> 60184307Srwatson 6153541Sshin/* 6253541Sshin * Define it to get a correct behavior on per-interface statistics. 6353541Sshin * You will need to perform an extra routing table lookup, per fragment, 6453541Sshin * to do it. This may, or may not be, a performance hit. 6553541Sshin */ 6662587Sitojun#define IN6_IFSTAT_STRICT 6753541Sshin 68175162Sobrienstatic void frag6_enq(struct ip6asfrag *, struct ip6asfrag *); 69175162Sobrienstatic void frag6_deq(struct ip6asfrag *); 70175162Sobrienstatic void frag6_insque(struct ip6q *, struct ip6q *); 71175162Sobrienstatic void frag6_remque(struct ip6q *); 72175162Sobrienstatic void frag6_freef(struct ip6q *); 7353541Sshin 74121346Sumestatic struct mtx ip6qlock; 75121346Sume/* 76121346Sume * These fields all protected by ip6qlock. 77121346Sume */ 78195699Srwatsonstatic VNET_DEFINE(u_int, frag6_nfragpackets); 79195699Srwatsonstatic VNET_DEFINE(u_int, frag6_nfrags); 80195699Srwatsonstatic VNET_DEFINE(struct ip6q, ip6q); /* ip6 reassemble queue */ 8153541Sshin 82195727Srwatson#define V_frag6_nfragpackets VNET(frag6_nfragpackets) 83195727Srwatson#define V_frag6_nfrags VNET(frag6_nfrags) 84195727Srwatson#define V_ip6q VNET(ip6q) 85195699Srwatson 86121346Sume#define IP6Q_LOCK_INIT() mtx_init(&ip6qlock, "ip6qlock", NULL, MTX_DEF); 87121346Sume#define IP6Q_LOCK() mtx_lock(&ip6qlock) 88121346Sume#define IP6Q_TRYLOCK() mtx_trylock(&ip6qlock) 89121355Sume#define IP6Q_LOCK_ASSERT() mtx_assert(&ip6qlock, MA_OWNED) 90121346Sume#define IP6Q_UNLOCK() mtx_unlock(&ip6qlock) 91121345Sume 9269774Sphkstatic MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header"); 9362587Sitojun 9453541Sshin/* 9553541Sshin * Initialise reassembly queue and fragment identifier. 9653541Sshin */ 97157927Spsstatic void 98157927Spsfrag6_change(void *tag) 99157927Sps{ 100157927Sps 101181803Sbz V_ip6_maxfragpackets = nmbclusters / 4; 102181803Sbz V_ip6_maxfrags = nmbclusters / 4; 103157927Sps} 104157927Sps 10553541Sshinvoid 106171259Sdelphijfrag6_init(void) 10753541Sshin{ 10853541Sshin 109181803Sbz V_ip6_maxfragpackets = nmbclusters / 4; 110181803Sbz V_ip6_maxfrags = nmbclusters / 4; 111207369Sbz V_ip6q.ip6q_next = V_ip6q.ip6q_prev = &V_ip6q; 112190787Szec 113190787Szec if (!IS_DEFAULT_VNET(curvnet)) 114190787Szec return; 115190787Szec 116157927Sps EVENTHANDLER_REGISTER(nmbclusters_change, 117157927Sps frag6_change, NULL, EVENTHANDLER_PRI_ANY); 118207369Sbz 119207369Sbz IP6Q_LOCK_INIT(); 12053541Sshin} 12153541Sshin 12253541Sshin/* 12362587Sitojun * In RFC2460, fragment and reassembly rule do not agree with each other, 12462587Sitojun * in terms of next header field handling in fragment header. 12562587Sitojun * While the sender will use the same value for all of the fragmented packets, 12662587Sitojun * receiver is suggested not to check the consistency. 12762587Sitojun * 12862587Sitojun * fragment rule (p20): 12962587Sitojun * (2) A Fragment header containing: 13062587Sitojun * The Next Header value that identifies the first header of 13162587Sitojun * the Fragmentable Part of the original packet. 13262587Sitojun * -> next header field is same for all fragments 13362587Sitojun * 13462587Sitojun * reassembly rule (p21): 13562587Sitojun * The Next Header field of the last header of the Unfragmentable 13662587Sitojun * Part is obtained from the Next Header field of the first 13762587Sitojun * fragment's Fragment header. 13862587Sitojun * -> should grab it from the first fragment only 13962587Sitojun * 14062587Sitojun * The following note also contradicts with fragment rule - noone is going to 14162587Sitojun * send different fragment with different next header field. 14262587Sitojun * 14362587Sitojun * additional note (p22): 14462587Sitojun * The Next Header values in the Fragment headers of different 14562587Sitojun * fragments of the same original packet may differ. Only the value 14662587Sitojun * from the Offset zero fragment packet is used for reassembly. 14762587Sitojun * -> should grab it from the first fragment only 14862587Sitojun * 14962587Sitojun * There is no explicit reason given in the RFC. Historical reason maybe? 15062587Sitojun */ 15162587Sitojun/* 15253541Sshin * Fragment input 15353541Sshin */ 15453541Sshinint 155171259Sdelphijfrag6_input(struct mbuf **mp, int *offp, int proto) 15653541Sshin{ 15753541Sshin struct mbuf *m = *mp, *t; 15853541Sshin struct ip6_hdr *ip6; 15953541Sshin struct ip6_frag *ip6f; 16053541Sshin struct ip6q *q6; 16162587Sitojun struct ip6asfrag *af6, *ip6af, *af6dwn; 162121630Sume#ifdef IN6_IFSTAT_STRICT 163121630Sume struct in6_ifaddr *ia; 164121630Sume#endif 16553541Sshin int offset = *offp, nxt, i, next; 16653541Sshin int first_frag = 0; 16762587Sitojun int fragoff, frgpartlen; /* must be larger than u_int16_t */ 16853541Sshin struct ifnet *dstifp; 169121684Sume u_int8_t ecn, ecn0; 170165118Sbz#if 0 171165118Sbz char ip6buf[INET6_ADDRSTRLEN]; 172165118Sbz#endif 17353541Sshin 17462587Sitojun ip6 = mtod(m, struct ip6_hdr *); 17562587Sitojun#ifndef PULLDOWN_TEST 17653541Sshin IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE); 17753541Sshin ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset); 17862587Sitojun#else 17962587Sitojun IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f)); 18062587Sitojun if (ip6f == NULL) 181120856Sume return (IPPROTO_DONE); 18262587Sitojun#endif 18353541Sshin 18453541Sshin dstifp = NULL; 18553541Sshin#ifdef IN6_IFSTAT_STRICT 18653541Sshin /* find the destination interface of the packet. */ 187194760Srwatson if ((ia = ip6_getdstifaddr(m)) != NULL) { 188121630Sume dstifp = ia->ia_ifp; 189194760Srwatson ifa_free(&ia->ia_ifa); 190194760Srwatson } 19153541Sshin#else 19253541Sshin /* we are violating the spec, this is not the destination interface */ 19353541Sshin if ((m->m_flags & M_PKTHDR) != 0) 19453541Sshin dstifp = m->m_pkthdr.rcvif; 19553541Sshin#endif 19653541Sshin 19753541Sshin /* jumbo payload can't contain a fragment header */ 19853541Sshin if (ip6->ip6_plen == 0) { 19953541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset); 20053541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 20153541Sshin return IPPROTO_DONE; 20253541Sshin } 20353541Sshin 20453541Sshin /* 20553541Sshin * check whether fragment packet's fragment length is 20653541Sshin * multiple of 8 octets. 20753541Sshin * sizeof(struct ip6_frag) == 8 20853541Sshin * sizeof(struct ip6_hdr) = 40 20953541Sshin */ 21053541Sshin if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && 21153541Sshin (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) { 212120891Sume icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 213120891Sume offsetof(struct ip6_hdr, ip6_plen)); 21453541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 21553541Sshin return IPPROTO_DONE; 21653541Sshin } 21753541Sshin 218181803Sbz V_ip6stat.ip6s_fragments++; 21953541Sshin in6_ifstat_inc(dstifp, ifs6_reass_reqd); 220120891Sume 22162587Sitojun /* offset now points to data portion */ 22253541Sshin offset += sizeof(struct ip6_frag); 22353541Sshin 224121345Sume IP6Q_LOCK(); 22578064Sume 226121345Sume /* 227121345Sume * Enforce upper bound on number of fragments. 228121345Sume * If maxfrag is 0, never accept fragments. 229121345Sume * If maxfrag is -1, accept all fragments without limitation. 230121345Sume */ 231181803Sbz if (V_ip6_maxfrags < 0) 232121345Sume ; 233181803Sbz else if (V_frag6_nfrags >= (u_int)V_ip6_maxfrags) 234121345Sume goto dropfrag; 235121345Sume 236181803Sbz for (q6 = V_ip6q.ip6q_next; q6 != &V_ip6q; q6 = q6->ip6q_next) 23753541Sshin if (ip6f->ip6f_ident == q6->ip6q_ident && 23853541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && 239184307Srwatson IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst) 240184307Srwatson#ifdef MAC 241184307Srwatson && mac_ip6q_match(m, q6) 242184307Srwatson#endif 243184307Srwatson ) 24453541Sshin break; 24553541Sshin 246181803Sbz if (q6 == &V_ip6q) { 24753541Sshin /* 24853541Sshin * the first fragment to arrive, create a reassembly queue. 24953541Sshin */ 25053541Sshin first_frag = 1; 25153541Sshin 25253541Sshin /* 25353541Sshin * Enforce upper bound on number of fragmented packets 25453541Sshin * for which we attempt reassembly; 255121345Sume * If maxfragpackets is 0, never accept fragments. 256121345Sume * If maxfragpackets is -1, accept all fragments without 257121345Sume * limitation. 25853541Sshin */ 259181803Sbz if (V_ip6_maxfragpackets < 0) 26078064Sume ; 261181803Sbz else if (V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets) 26278064Sume goto dropfrag; 263181803Sbz V_frag6_nfragpackets++; 26453541Sshin q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE, 265121607Sume M_NOWAIT); 26653541Sshin if (q6 == NULL) 26753541Sshin goto dropfrag; 26862587Sitojun bzero(q6, sizeof(*q6)); 269184307Srwatson#ifdef MAC 270184307Srwatson if (mac_ip6q_init(q6, M_NOWAIT) != 0) { 271184307Srwatson free(q6, M_FTABLE); 272184307Srwatson goto dropfrag; 273184307Srwatson } 274184307Srwatson mac_ip6q_create(m, q6); 275184307Srwatson#endif 276181803Sbz frag6_insque(q6, &V_ip6q); 27753541Sshin 27862587Sitojun /* ip6q_nxt will be filled afterwards, from 1st fragment */ 27953541Sshin q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6; 28062587Sitojun#ifdef notyet 28162587Sitojun q6->ip6q_nxtp = (u_char *)nxtp; 28262587Sitojun#endif 28353541Sshin q6->ip6q_ident = ip6f->ip6f_ident; 284171260Sdelphij q6->ip6q_ttl = IPV6_FRAGTTL; 28553541Sshin q6->ip6q_src = ip6->ip6_src; 28653541Sshin q6->ip6q_dst = ip6->ip6_dst; 287170275Sjinmei q6->ip6q_ecn = 288170275Sjinmei (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; 28953541Sshin q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ 290121345Sume 291121345Sume q6->ip6q_nfrag = 0; 29253541Sshin } 29353541Sshin 29453541Sshin /* 29553541Sshin * If it's the 1st fragment, record the length of the 29653541Sshin * unfragmentable part and the next header of the fragment header. 29753541Sshin */ 29853541Sshin fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); 29953541Sshin if (fragoff == 0) { 300120891Sume q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - 301120891Sume sizeof(struct ip6_frag); 30253541Sshin q6->ip6q_nxt = ip6f->ip6f_nxt; 30353541Sshin } 30453541Sshin 30553541Sshin /* 30653541Sshin * Check that the reassembled packet would not exceed 65535 bytes 30753541Sshin * in size. 30853541Sshin * If it would exceed, discard the fragment and return an ICMP error. 30953541Sshin */ 31062587Sitojun frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; 31153541Sshin if (q6->ip6q_unfrglen >= 0) { 31253541Sshin /* The 1st fragment has already arrived. */ 31353541Sshin if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { 31453541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 315120891Sume offset - sizeof(struct ip6_frag) + 316120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 317121345Sume IP6Q_UNLOCK(); 318120856Sume return (IPPROTO_DONE); 31953541Sshin } 320120891Sume } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { 32153541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 322120891Sume offset - sizeof(struct ip6_frag) + 323120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 324121345Sume IP6Q_UNLOCK(); 325120856Sume return (IPPROTO_DONE); 32653541Sshin } 32753541Sshin /* 32853541Sshin * If it's the first fragment, do the above check for each 32953541Sshin * fragment already stored in the reassembly queue. 33053541Sshin */ 33153541Sshin if (fragoff == 0) { 33253541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 33353541Sshin af6 = af6dwn) { 33453541Sshin af6dwn = af6->ip6af_down; 33553541Sshin 33653541Sshin if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen > 33753541Sshin IPV6_MAXPACKET) { 33853541Sshin struct mbuf *merr = IP6_REASS_MBUF(af6); 33953541Sshin struct ip6_hdr *ip6err; 34053541Sshin int erroff = af6->ip6af_offset; 34153541Sshin 34253541Sshin /* dequeue the fragment. */ 34353541Sshin frag6_deq(af6); 34462587Sitojun free(af6, M_FTABLE); 34553541Sshin 34653541Sshin /* adjust pointer. */ 34753541Sshin ip6err = mtod(merr, struct ip6_hdr *); 34853541Sshin 34953541Sshin /* 35053541Sshin * Restore source and destination addresses 35153541Sshin * in the erroneous IPv6 header. 35253541Sshin */ 35353541Sshin ip6err->ip6_src = q6->ip6q_src; 35453541Sshin ip6err->ip6_dst = q6->ip6q_dst; 35553541Sshin 35653541Sshin icmp6_error(merr, ICMP6_PARAM_PROB, 357120891Sume ICMP6_PARAMPROB_HEADER, 358120891Sume erroff - sizeof(struct ip6_frag) + 359120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 36053541Sshin } 36153541Sshin } 36253541Sshin } 36353541Sshin 36462587Sitojun ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE, 365121607Sume M_NOWAIT); 36662587Sitojun if (ip6af == NULL) 36762587Sitojun goto dropfrag; 36862587Sitojun bzero(ip6af, sizeof(*ip6af)); 36953541Sshin ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG; 37053541Sshin ip6af->ip6af_off = fragoff; 37153541Sshin ip6af->ip6af_frglen = frgpartlen; 37253541Sshin ip6af->ip6af_offset = offset; 37353541Sshin IP6_REASS_MBUF(ip6af) = m; 37453541Sshin 37553541Sshin if (first_frag) { 37653541Sshin af6 = (struct ip6asfrag *)q6; 37753541Sshin goto insert; 37853541Sshin } 37953541Sshin 38053541Sshin /* 381121684Sume * Handle ECN by comparing this segment with the first one; 382121684Sume * if CE is set, do not lose CE. 383121684Sume * drop if CE and not-ECT are mixed for the same packet. 384121684Sume */ 385121684Sume ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; 386170275Sjinmei ecn0 = q6->ip6q_ecn; 387121684Sume if (ecn == IPTOS_ECN_CE) { 388121684Sume if (ecn0 == IPTOS_ECN_NOTECT) { 389121684Sume free(ip6af, M_FTABLE); 390121684Sume goto dropfrag; 391121684Sume } 392121684Sume if (ecn0 != IPTOS_ECN_CE) 393170275Sjinmei q6->ip6q_ecn = IPTOS_ECN_CE; 394121684Sume } 395121684Sume if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) { 396121684Sume free(ip6af, M_FTABLE); 397121684Sume goto dropfrag; 398121684Sume } 399121684Sume 400121684Sume /* 40153541Sshin * Find a segment which begins after this one does. 40253541Sshin */ 40353541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 40453541Sshin af6 = af6->ip6af_down) 40553541Sshin if (af6->ip6af_off > ip6af->ip6af_off) 40653541Sshin break; 40753541Sshin 40862587Sitojun#if 0 40953541Sshin /* 41062587Sitojun * If there is a preceding segment, it may provide some of 41162587Sitojun * our data already. If so, drop the data from the incoming 41262587Sitojun * segment. If it provides all of our data, drop us. 41362587Sitojun */ 41462587Sitojun if (af6->ip6af_up != (struct ip6asfrag *)q6) { 41562587Sitojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 41662587Sitojun - ip6af->ip6af_off; 41762587Sitojun if (i > 0) { 41862587Sitojun if (i >= ip6af->ip6af_frglen) 41962587Sitojun goto dropfrag; 42062587Sitojun m_adj(IP6_REASS_MBUF(ip6af), i); 42162587Sitojun ip6af->ip6af_off += i; 42262587Sitojun ip6af->ip6af_frglen -= i; 42362587Sitojun } 42462587Sitojun } 42562587Sitojun 42662587Sitojun /* 42762587Sitojun * While we overlap succeeding segments trim them or, 42862587Sitojun * if they are completely covered, dequeue them. 42962587Sitojun */ 43062587Sitojun while (af6 != (struct ip6asfrag *)q6 && 43162587Sitojun ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) { 43262587Sitojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 43362587Sitojun if (i < af6->ip6af_frglen) { 43462587Sitojun af6->ip6af_frglen -= i; 43562587Sitojun af6->ip6af_off += i; 43662587Sitojun m_adj(IP6_REASS_MBUF(af6), i); 43762587Sitojun break; 43862587Sitojun } 43962587Sitojun af6 = af6->ip6af_down; 44062587Sitojun m_freem(IP6_REASS_MBUF(af6->ip6af_up)); 44162587Sitojun frag6_deq(af6->ip6af_up); 44262587Sitojun } 44362587Sitojun#else 44462587Sitojun /* 44553541Sshin * If the incoming framgent overlaps some existing fragments in 44653541Sshin * the reassembly queue, drop it, since it is dangerous to override 44753541Sshin * existing fragments from a security point of view. 448121345Sume * We don't know which fragment is the bad guy - here we trust 449121345Sume * fragment that came in earlier, with no real reason. 450170275Sjinmei * 451170275Sjinmei * Note: due to changes after disabling this part, mbuf passed to 452170275Sjinmei * m_adj() below now does not meet the requirement. 45353541Sshin */ 45453541Sshin if (af6->ip6af_up != (struct ip6asfrag *)q6) { 45553541Sshin i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 45653541Sshin - ip6af->ip6af_off; 45753541Sshin if (i > 0) { 45876899Ssumikawa#if 0 /* suppress the noisy log */ 45953541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 46053541Sshin "overlaps the previous fragment\n", 461165118Sbz i, ip6_sprintf(ip6buf, &q6->ip6q_src)); 46276899Ssumikawa#endif 46376899Ssumikawa free(ip6af, M_FTABLE); 46453541Sshin goto dropfrag; 46553541Sshin } 46653541Sshin } 46753541Sshin if (af6 != (struct ip6asfrag *)q6) { 46853541Sshin i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 46953541Sshin if (i > 0) { 47076899Ssumikawa#if 0 /* suppress the noisy log */ 47153541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 47253541Sshin "overlaps the succeeding fragment", 473165118Sbz i, ip6_sprintf(ip6buf, &q6->ip6q_src)); 47476899Ssumikawa#endif 47576899Ssumikawa free(ip6af, M_FTABLE); 47653541Sshin goto dropfrag; 47753541Sshin } 47853541Sshin } 47962587Sitojun#endif 48053541Sshin 48153541Sshininsert: 482184307Srwatson#ifdef MAC 483184307Srwatson if (!first_frag) 484184307Srwatson mac_ip6q_update(m, q6); 485184307Srwatson#endif 48653541Sshin 48753541Sshin /* 48853541Sshin * Stick new segment in its place; 48953541Sshin * check for complete reassembly. 49053541Sshin * Move to front of packet queue, as we are 49153541Sshin * the most recently active fragmented packet. 49253541Sshin */ 49353541Sshin frag6_enq(ip6af, af6->ip6af_up); 494181803Sbz V_frag6_nfrags++; 495121345Sume q6->ip6q_nfrag++; 49662587Sitojun#if 0 /* xxx */ 497181803Sbz if (q6 != V_ip6q.ip6q_next) { 49862587Sitojun frag6_remque(q6); 499181803Sbz frag6_insque(q6, &V_ip6q); 50062587Sitojun } 50162587Sitojun#endif 50253541Sshin next = 0; 50353541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 50453541Sshin af6 = af6->ip6af_down) { 50553541Sshin if (af6->ip6af_off != next) { 506121345Sume IP6Q_UNLOCK(); 50753541Sshin return IPPROTO_DONE; 50853541Sshin } 50953541Sshin next += af6->ip6af_frglen; 51053541Sshin } 51153541Sshin if (af6->ip6af_up->ip6af_mff) { 512121345Sume IP6Q_UNLOCK(); 51353541Sshin return IPPROTO_DONE; 51453541Sshin } 51553541Sshin 51653541Sshin /* 51753541Sshin * Reassembly is complete; concatenate fragments. 51853541Sshin */ 51953541Sshin ip6af = q6->ip6q_down; 52053541Sshin t = m = IP6_REASS_MBUF(ip6af); 52153541Sshin af6 = ip6af->ip6af_down; 52262587Sitojun frag6_deq(ip6af); 52353541Sshin while (af6 != (struct ip6asfrag *)q6) { 52462587Sitojun af6dwn = af6->ip6af_down; 52562587Sitojun frag6_deq(af6); 52653541Sshin while (t->m_next) 52753541Sshin t = t->m_next; 52853541Sshin t->m_next = IP6_REASS_MBUF(af6); 52962587Sitojun m_adj(t->m_next, af6->ip6af_offset); 53062587Sitojun free(af6, M_FTABLE); 53162587Sitojun af6 = af6dwn; 53253541Sshin } 53353541Sshin 53453541Sshin /* adjust offset to point where the original next header starts */ 53553541Sshin offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); 53662587Sitojun free(ip6af, M_FTABLE); 53762587Sitojun ip6 = mtod(m, struct ip6_hdr *); 53853541Sshin ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr)); 539170275Sjinmei if (q6->ip6q_ecn == IPTOS_ECN_CE) 540170275Sjinmei ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20); 54153541Sshin nxt = q6->ip6q_nxt; 54262587Sitojun#ifdef notyet 54362587Sitojun *q6->ip6q_nxtp = (u_char)(nxt & 0xff); 54462587Sitojun#endif 54553541Sshin 546170275Sjinmei /* Delete frag6 header */ 547170275Sjinmei if (m->m_len >= offset + sizeof(struct ip6_frag)) { 548170275Sjinmei /* This is the only possible case with !PULLDOWN_TEST */ 54953541Sshin ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag), 550170275Sjinmei offset); 55162587Sitojun m->m_data += sizeof(struct ip6_frag); 55262587Sitojun m->m_len -= sizeof(struct ip6_frag); 55362587Sitojun } else { 55462587Sitojun /* this comes with no copy if the boundary is on cluster */ 555111119Simp if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) { 55662587Sitojun frag6_remque(q6); 557181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 558184307Srwatson#ifdef MAC 559184307Srwatson mac_ip6q_destroy(q6); 560184307Srwatson#endif 56162587Sitojun free(q6, M_FTABLE); 562181803Sbz V_frag6_nfragpackets--; 56362587Sitojun goto dropfrag; 56462587Sitojun } 56562587Sitojun m_adj(t, sizeof(struct ip6_frag)); 56662587Sitojun m_cat(m, t); 56753541Sshin } 56853541Sshin 56953541Sshin /* 57053541Sshin * Store NXT to the original. 57153541Sshin */ 57253541Sshin { 57353541Sshin char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ 57453541Sshin *prvnxtp = nxt; 57553541Sshin } 57653541Sshin 57753541Sshin frag6_remque(q6); 578181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 579184307Srwatson#ifdef MAC 580184307Srwatson mac_ip6q_reassemble(q6, m); 581184307Srwatson mac_ip6q_destroy(q6); 582184307Srwatson#endif 58353541Sshin free(q6, M_FTABLE); 584181803Sbz V_frag6_nfragpackets--; 58553541Sshin 58653541Sshin if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 58753541Sshin int plen = 0; 58853541Sshin for (t = m; t; t = t->m_next) 58953541Sshin plen += t->m_len; 59053541Sshin m->m_pkthdr.len = plen; 59153541Sshin } 592120891Sume 593181803Sbz V_ip6stat.ip6s_reassembled++; 59453541Sshin in6_ifstat_inc(dstifp, ifs6_reass_ok); 59553541Sshin 59653541Sshin /* 59753541Sshin * Tell launch routine the next header 59853541Sshin */ 59953541Sshin 60053541Sshin *mp = m; 60153541Sshin *offp = offset; 60253541Sshin 603121345Sume IP6Q_UNLOCK(); 60453541Sshin return nxt; 60553541Sshin 60653541Sshin dropfrag: 607121346Sume IP6Q_UNLOCK(); 60853541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 609181803Sbz V_ip6stat.ip6s_fragdropped++; 61053541Sshin m_freem(m); 61153541Sshin return IPPROTO_DONE; 61253541Sshin} 61353541Sshin 61453541Sshin/* 61553541Sshin * Free a fragment reassembly header and all 61653541Sshin * associated datagrams. 61753541Sshin */ 61853541Sshinvoid 619171259Sdelphijfrag6_freef(struct ip6q *q6) 62053541Sshin{ 62153541Sshin struct ip6asfrag *af6, *down6; 62253541Sshin 623121355Sume IP6Q_LOCK_ASSERT(); 624121345Sume 62553541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 62653541Sshin af6 = down6) { 62753541Sshin struct mbuf *m = IP6_REASS_MBUF(af6); 62853541Sshin 62953541Sshin down6 = af6->ip6af_down; 63053541Sshin frag6_deq(af6); 63153541Sshin 63253541Sshin /* 63353541Sshin * Return ICMP time exceeded error for the 1st fragment. 63453541Sshin * Just free other fragments. 63553541Sshin */ 63653541Sshin if (af6->ip6af_off == 0) { 63753541Sshin struct ip6_hdr *ip6; 63853541Sshin 63953541Sshin /* adjust pointer */ 64053541Sshin ip6 = mtod(m, struct ip6_hdr *); 64153541Sshin 642120891Sume /* restore source and destination addresses */ 64353541Sshin ip6->ip6_src = q6->ip6q_src; 64453541Sshin ip6->ip6_dst = q6->ip6q_dst; 64553541Sshin 64653541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 64753541Sshin ICMP6_TIME_EXCEED_REASSEMBLY, 0); 64862587Sitojun } else 64953541Sshin m_freem(m); 65062587Sitojun free(af6, M_FTABLE); 65153541Sshin } 65253541Sshin frag6_remque(q6); 653181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 654184307Srwatson#ifdef MAC 655184307Srwatson mac_ip6q_destroy(q6); 656184307Srwatson#endif 65753541Sshin free(q6, M_FTABLE); 658181803Sbz V_frag6_nfragpackets--; 65953541Sshin} 66053541Sshin 66153541Sshin/* 66253541Sshin * Put an ip fragment on a reassembly chain. 66353541Sshin * Like insque, but pointers in middle of structure. 66453541Sshin */ 66553541Sshinvoid 666171259Sdelphijfrag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6) 66753541Sshin{ 668121345Sume 669121355Sume IP6Q_LOCK_ASSERT(); 670121345Sume 67153541Sshin af6->ip6af_up = up6; 67253541Sshin af6->ip6af_down = up6->ip6af_down; 67353541Sshin up6->ip6af_down->ip6af_up = af6; 67453541Sshin up6->ip6af_down = af6; 67553541Sshin} 67653541Sshin 67753541Sshin/* 67853541Sshin * To frag6_enq as remque is to insque. 67953541Sshin */ 68053541Sshinvoid 681171259Sdelphijfrag6_deq(struct ip6asfrag *af6) 68253541Sshin{ 683121345Sume 684121355Sume IP6Q_LOCK_ASSERT(); 685121345Sume 68653541Sshin af6->ip6af_up->ip6af_down = af6->ip6af_down; 68753541Sshin af6->ip6af_down->ip6af_up = af6->ip6af_up; 68853541Sshin} 68953541Sshin 69053541Sshinvoid 691171259Sdelphijfrag6_insque(struct ip6q *new, struct ip6q *old) 69253541Sshin{ 693121345Sume 694121355Sume IP6Q_LOCK_ASSERT(); 695121345Sume 69653541Sshin new->ip6q_prev = old; 69753541Sshin new->ip6q_next = old->ip6q_next; 69853541Sshin old->ip6q_next->ip6q_prev= new; 69953541Sshin old->ip6q_next = new; 70053541Sshin} 70153541Sshin 70253541Sshinvoid 703171259Sdelphijfrag6_remque(struct ip6q *p6) 70453541Sshin{ 705121345Sume 706121355Sume IP6Q_LOCK_ASSERT(); 707121345Sume 70853541Sshin p6->ip6q_prev->ip6q_next = p6->ip6q_next; 70953541Sshin p6->ip6q_next->ip6q_prev = p6->ip6q_prev; 71053541Sshin} 71153541Sshin 71253541Sshin/* 71378064Sume * IPv6 reassembling timer processing; 71453541Sshin * if a timer expires on a reassembly 71553541Sshin * queue, discard it. 71653541Sshin */ 71753541Sshinvoid 718171259Sdelphijfrag6_slowtimo(void) 71953541Sshin{ 720183550Szec VNET_ITERATOR_DECL(vnet_iter); 72153541Sshin struct ip6q *q6; 72253541Sshin 723195760Srwatson VNET_LIST_RLOCK_NOSLEEP(); 724121345Sume IP6Q_LOCK(); 725183550Szec VNET_FOREACH(vnet_iter) { 726183550Szec CURVNET_SET(vnet_iter); 727183550Szec q6 = V_ip6q.ip6q_next; 728183550Szec if (q6) 729183550Szec while (q6 != &V_ip6q) { 730183550Szec --q6->ip6q_ttl; 731183550Szec q6 = q6->ip6q_next; 732183550Szec if (q6->ip6q_prev->ip6q_ttl == 0) { 733183550Szec V_ip6stat.ip6s_fragtimeout++; 734183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 735183550Szec frag6_freef(q6->ip6q_prev); 736183550Szec } 73753541Sshin } 738183550Szec /* 739183550Szec * If we are over the maximum number of fragments 740183550Szec * (due to the limit being lowered), drain off 741183550Szec * enough to get down to the new limit. 742183550Szec */ 743183550Szec while (V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets && 744183550Szec V_ip6q.ip6q_prev) { 745183550Szec V_ip6stat.ip6s_fragoverflow++; 746183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 747183550Szec frag6_freef(V_ip6q.ip6q_prev); 74853541Sshin } 749183550Szec CURVNET_RESTORE(); 75053541Sshin } 751121345Sume IP6Q_UNLOCK(); 752195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 75353541Sshin} 75453541Sshin 75553541Sshin/* 75653541Sshin * Drain off all datagram fragments. 75753541Sshin */ 75853541Sshinvoid 759171259Sdelphijfrag6_drain(void) 76053541Sshin{ 761183550Szec VNET_ITERATOR_DECL(vnet_iter); 762121345Sume 763195760Srwatson VNET_LIST_RLOCK_NOSLEEP(); 764195760Srwatson if (IP6Q_TRYLOCK() == 0) { 765195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 76653541Sshin return; 767195760Srwatson } 768183550Szec VNET_FOREACH(vnet_iter) { 769183550Szec CURVNET_SET(vnet_iter); 770183550Szec while (V_ip6q.ip6q_next != &V_ip6q) { 771183550Szec V_ip6stat.ip6s_fragdropped++; 772183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 773183550Szec frag6_freef(V_ip6q.ip6q_next); 774183550Szec } 775183550Szec CURVNET_RESTORE(); 77653541Sshin } 777121345Sume IP6Q_UNLOCK(); 778195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 77953541Sshin} 780