frag6.c revision 238248
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 238248 2012-07-08 15:30:24Z 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 */ 78215701Sdimstatic VNET_DEFINE(u_int, frag6_nfragpackets); 79215701Sdimstatic VNET_DEFINE(u_int, frag6_nfrags); 80215701Sdimstatic 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 224238248Sbz /* 225238248Sbz * XXX-BZ RFC XXXX (draft-gont-6man-ipv6-atomic-fragments) 226238248Sbz * Handle "atomic" fragments (offset and m bit set to 0) upfront, 227238248Sbz * unrelated to any reassembly. Just skip the fragment header. 228238248Sbz */ 229238248Sbz if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) { 230238248Sbz /* XXX-BZ we want dedicated counters for this. */ 231238248Sbz V_ip6stat.ip6s_reassembled++; 232238248Sbz in6_ifstat_inc(dstifp, ifs6_reass_ok); 233238248Sbz *offp = offset; 234238248Sbz return (ip6f->ip6f_nxt); 235238248Sbz } 236238248Sbz 237121345Sume IP6Q_LOCK(); 23878064Sume 239121345Sume /* 240121345Sume * Enforce upper bound on number of fragments. 241121345Sume * If maxfrag is 0, never accept fragments. 242121345Sume * If maxfrag is -1, accept all fragments without limitation. 243121345Sume */ 244181803Sbz if (V_ip6_maxfrags < 0) 245121345Sume ; 246181803Sbz else if (V_frag6_nfrags >= (u_int)V_ip6_maxfrags) 247121345Sume goto dropfrag; 248121345Sume 249181803Sbz for (q6 = V_ip6q.ip6q_next; q6 != &V_ip6q; q6 = q6->ip6q_next) 25053541Sshin if (ip6f->ip6f_ident == q6->ip6q_ident && 25153541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && 252184307Srwatson IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst) 253184307Srwatson#ifdef MAC 254184307Srwatson && mac_ip6q_match(m, q6) 255184307Srwatson#endif 256184307Srwatson ) 25753541Sshin break; 25853541Sshin 259181803Sbz if (q6 == &V_ip6q) { 26053541Sshin /* 26153541Sshin * the first fragment to arrive, create a reassembly queue. 26253541Sshin */ 26353541Sshin first_frag = 1; 26453541Sshin 26553541Sshin /* 26653541Sshin * Enforce upper bound on number of fragmented packets 26753541Sshin * for which we attempt reassembly; 268121345Sume * If maxfragpackets is 0, never accept fragments. 269121345Sume * If maxfragpackets is -1, accept all fragments without 270121345Sume * limitation. 27153541Sshin */ 272181803Sbz if (V_ip6_maxfragpackets < 0) 27378064Sume ; 274181803Sbz else if (V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets) 27578064Sume goto dropfrag; 276181803Sbz V_frag6_nfragpackets++; 27753541Sshin q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE, 278121607Sume M_NOWAIT); 27953541Sshin if (q6 == NULL) 28053541Sshin goto dropfrag; 28162587Sitojun bzero(q6, sizeof(*q6)); 282184307Srwatson#ifdef MAC 283184307Srwatson if (mac_ip6q_init(q6, M_NOWAIT) != 0) { 284184307Srwatson free(q6, M_FTABLE); 285184307Srwatson goto dropfrag; 286184307Srwatson } 287184307Srwatson mac_ip6q_create(m, q6); 288184307Srwatson#endif 289181803Sbz frag6_insque(q6, &V_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; 297171260Sdelphij q6->ip6q_ttl = IPV6_FRAGTTL; 29853541Sshin q6->ip6q_src = ip6->ip6_src; 29953541Sshin q6->ip6q_dst = ip6->ip6_dst; 300170275Sjinmei q6->ip6q_ecn = 301170275Sjinmei (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; 30253541Sshin q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ 303121345Sume 304121345Sume q6->ip6q_nfrag = 0; 30553541Sshin } 30653541Sshin 30753541Sshin /* 30853541Sshin * If it's the 1st fragment, record the length of the 30953541Sshin * unfragmentable part and the next header of the fragment header. 31053541Sshin */ 31153541Sshin fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); 31253541Sshin if (fragoff == 0) { 313120891Sume q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - 314120891Sume sizeof(struct ip6_frag); 31553541Sshin q6->ip6q_nxt = ip6f->ip6f_nxt; 31653541Sshin } 31753541Sshin 31853541Sshin /* 31953541Sshin * Check that the reassembled packet would not exceed 65535 bytes 32053541Sshin * in size. 32153541Sshin * If it would exceed, discard the fragment and return an ICMP error. 32253541Sshin */ 32362587Sitojun frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; 32453541Sshin if (q6->ip6q_unfrglen >= 0) { 32553541Sshin /* The 1st fragment has already arrived. */ 32653541Sshin if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { 32753541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 328120891Sume offset - sizeof(struct ip6_frag) + 329120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 330121345Sume IP6Q_UNLOCK(); 331120856Sume return (IPPROTO_DONE); 33253541Sshin } 333120891Sume } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { 33453541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 335120891Sume offset - sizeof(struct ip6_frag) + 336120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 337121345Sume IP6Q_UNLOCK(); 338120856Sume return (IPPROTO_DONE); 33953541Sshin } 34053541Sshin /* 34153541Sshin * If it's the first fragment, do the above check for each 34253541Sshin * fragment already stored in the reassembly queue. 34353541Sshin */ 34453541Sshin if (fragoff == 0) { 34553541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 34653541Sshin af6 = af6dwn) { 34753541Sshin af6dwn = af6->ip6af_down; 34853541Sshin 34953541Sshin if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen > 35053541Sshin IPV6_MAXPACKET) { 35153541Sshin struct mbuf *merr = IP6_REASS_MBUF(af6); 35253541Sshin struct ip6_hdr *ip6err; 35353541Sshin int erroff = af6->ip6af_offset; 35453541Sshin 35553541Sshin /* dequeue the fragment. */ 35653541Sshin frag6_deq(af6); 35762587Sitojun free(af6, M_FTABLE); 35853541Sshin 35953541Sshin /* adjust pointer. */ 36053541Sshin ip6err = mtod(merr, struct ip6_hdr *); 36153541Sshin 36253541Sshin /* 36353541Sshin * Restore source and destination addresses 36453541Sshin * in the erroneous IPv6 header. 36553541Sshin */ 36653541Sshin ip6err->ip6_src = q6->ip6q_src; 36753541Sshin ip6err->ip6_dst = q6->ip6q_dst; 36853541Sshin 36953541Sshin icmp6_error(merr, ICMP6_PARAM_PROB, 370120891Sume ICMP6_PARAMPROB_HEADER, 371120891Sume erroff - sizeof(struct ip6_frag) + 372120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 37353541Sshin } 37453541Sshin } 37553541Sshin } 37653541Sshin 37762587Sitojun ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE, 378121607Sume M_NOWAIT); 37962587Sitojun if (ip6af == NULL) 38062587Sitojun goto dropfrag; 38162587Sitojun bzero(ip6af, sizeof(*ip6af)); 38253541Sshin ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG; 38353541Sshin ip6af->ip6af_off = fragoff; 38453541Sshin ip6af->ip6af_frglen = frgpartlen; 38553541Sshin ip6af->ip6af_offset = offset; 38653541Sshin IP6_REASS_MBUF(ip6af) = m; 38753541Sshin 38853541Sshin if (first_frag) { 38953541Sshin af6 = (struct ip6asfrag *)q6; 39053541Sshin goto insert; 39153541Sshin } 39253541Sshin 39353541Sshin /* 394121684Sume * Handle ECN by comparing this segment with the first one; 395121684Sume * if CE is set, do not lose CE. 396121684Sume * drop if CE and not-ECT are mixed for the same packet. 397121684Sume */ 398121684Sume ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; 399170275Sjinmei ecn0 = q6->ip6q_ecn; 400121684Sume if (ecn == IPTOS_ECN_CE) { 401121684Sume if (ecn0 == IPTOS_ECN_NOTECT) { 402121684Sume free(ip6af, M_FTABLE); 403121684Sume goto dropfrag; 404121684Sume } 405121684Sume if (ecn0 != IPTOS_ECN_CE) 406170275Sjinmei q6->ip6q_ecn = IPTOS_ECN_CE; 407121684Sume } 408121684Sume if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) { 409121684Sume free(ip6af, M_FTABLE); 410121684Sume goto dropfrag; 411121684Sume } 412121684Sume 413121684Sume /* 41453541Sshin * Find a segment which begins after this one does. 41553541Sshin */ 41653541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 41753541Sshin af6 = af6->ip6af_down) 41853541Sshin if (af6->ip6af_off > ip6af->ip6af_off) 41953541Sshin break; 42053541Sshin 42162587Sitojun#if 0 42253541Sshin /* 42362587Sitojun * If there is a preceding segment, it may provide some of 42462587Sitojun * our data already. If so, drop the data from the incoming 42562587Sitojun * segment. If it provides all of our data, drop us. 42662587Sitojun */ 42762587Sitojun if (af6->ip6af_up != (struct ip6asfrag *)q6) { 42862587Sitojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 42962587Sitojun - ip6af->ip6af_off; 43062587Sitojun if (i > 0) { 43162587Sitojun if (i >= ip6af->ip6af_frglen) 43262587Sitojun goto dropfrag; 43362587Sitojun m_adj(IP6_REASS_MBUF(ip6af), i); 43462587Sitojun ip6af->ip6af_off += i; 43562587Sitojun ip6af->ip6af_frglen -= i; 43662587Sitojun } 43762587Sitojun } 43862587Sitojun 43962587Sitojun /* 44062587Sitojun * While we overlap succeeding segments trim them or, 44162587Sitojun * if they are completely covered, dequeue them. 44262587Sitojun */ 44362587Sitojun while (af6 != (struct ip6asfrag *)q6 && 44462587Sitojun ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) { 44562587Sitojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 44662587Sitojun if (i < af6->ip6af_frglen) { 44762587Sitojun af6->ip6af_frglen -= i; 44862587Sitojun af6->ip6af_off += i; 44962587Sitojun m_adj(IP6_REASS_MBUF(af6), i); 45062587Sitojun break; 45162587Sitojun } 45262587Sitojun af6 = af6->ip6af_down; 45362587Sitojun m_freem(IP6_REASS_MBUF(af6->ip6af_up)); 45462587Sitojun frag6_deq(af6->ip6af_up); 45562587Sitojun } 45662587Sitojun#else 45762587Sitojun /* 45853541Sshin * If the incoming framgent overlaps some existing fragments in 45953541Sshin * the reassembly queue, drop it, since it is dangerous to override 46053541Sshin * existing fragments from a security point of view. 461121345Sume * We don't know which fragment is the bad guy - here we trust 462121345Sume * fragment that came in earlier, with no real reason. 463170275Sjinmei * 464170275Sjinmei * Note: due to changes after disabling this part, mbuf passed to 465170275Sjinmei * m_adj() below now does not meet the requirement. 46653541Sshin */ 46753541Sshin if (af6->ip6af_up != (struct ip6asfrag *)q6) { 46853541Sshin i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 46953541Sshin - ip6af->ip6af_off; 47053541Sshin if (i > 0) { 47176899Ssumikawa#if 0 /* suppress the noisy log */ 47253541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 47353541Sshin "overlaps the previous fragment\n", 474165118Sbz i, ip6_sprintf(ip6buf, &q6->ip6q_src)); 47576899Ssumikawa#endif 47676899Ssumikawa free(ip6af, M_FTABLE); 47753541Sshin goto dropfrag; 47853541Sshin } 47953541Sshin } 48053541Sshin if (af6 != (struct ip6asfrag *)q6) { 48153541Sshin i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 48253541Sshin if (i > 0) { 48376899Ssumikawa#if 0 /* suppress the noisy log */ 48453541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 48553541Sshin "overlaps the succeeding fragment", 486165118Sbz i, ip6_sprintf(ip6buf, &q6->ip6q_src)); 48776899Ssumikawa#endif 48876899Ssumikawa free(ip6af, M_FTABLE); 48953541Sshin goto dropfrag; 49053541Sshin } 49153541Sshin } 49262587Sitojun#endif 49353541Sshin 49453541Sshininsert: 495184307Srwatson#ifdef MAC 496184307Srwatson if (!first_frag) 497184307Srwatson mac_ip6q_update(m, q6); 498184307Srwatson#endif 49953541Sshin 50053541Sshin /* 50153541Sshin * Stick new segment in its place; 50253541Sshin * check for complete reassembly. 50353541Sshin * Move to front of packet queue, as we are 50453541Sshin * the most recently active fragmented packet. 50553541Sshin */ 50653541Sshin frag6_enq(ip6af, af6->ip6af_up); 507181803Sbz V_frag6_nfrags++; 508121345Sume q6->ip6q_nfrag++; 50962587Sitojun#if 0 /* xxx */ 510181803Sbz if (q6 != V_ip6q.ip6q_next) { 51162587Sitojun frag6_remque(q6); 512181803Sbz frag6_insque(q6, &V_ip6q); 51362587Sitojun } 51462587Sitojun#endif 51553541Sshin next = 0; 51653541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 51753541Sshin af6 = af6->ip6af_down) { 51853541Sshin if (af6->ip6af_off != next) { 519121345Sume IP6Q_UNLOCK(); 52053541Sshin return IPPROTO_DONE; 52153541Sshin } 52253541Sshin next += af6->ip6af_frglen; 52353541Sshin } 52453541Sshin if (af6->ip6af_up->ip6af_mff) { 525121345Sume IP6Q_UNLOCK(); 52653541Sshin return IPPROTO_DONE; 52753541Sshin } 52853541Sshin 52953541Sshin /* 53053541Sshin * Reassembly is complete; concatenate fragments. 53153541Sshin */ 53253541Sshin ip6af = q6->ip6q_down; 53353541Sshin t = m = IP6_REASS_MBUF(ip6af); 53453541Sshin af6 = ip6af->ip6af_down; 53562587Sitojun frag6_deq(ip6af); 53653541Sshin while (af6 != (struct ip6asfrag *)q6) { 53762587Sitojun af6dwn = af6->ip6af_down; 53862587Sitojun frag6_deq(af6); 53953541Sshin while (t->m_next) 54053541Sshin t = t->m_next; 54153541Sshin t->m_next = IP6_REASS_MBUF(af6); 54262587Sitojun m_adj(t->m_next, af6->ip6af_offset); 54362587Sitojun free(af6, M_FTABLE); 54462587Sitojun af6 = af6dwn; 54553541Sshin } 54653541Sshin 54753541Sshin /* adjust offset to point where the original next header starts */ 54853541Sshin offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); 54962587Sitojun free(ip6af, M_FTABLE); 55062587Sitojun ip6 = mtod(m, struct ip6_hdr *); 55153541Sshin ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr)); 552170275Sjinmei if (q6->ip6q_ecn == IPTOS_ECN_CE) 553170275Sjinmei ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20); 55453541Sshin nxt = q6->ip6q_nxt; 55562587Sitojun#ifdef notyet 55662587Sitojun *q6->ip6q_nxtp = (u_char)(nxt & 0xff); 55762587Sitojun#endif 55853541Sshin 559170275Sjinmei /* Delete frag6 header */ 560170275Sjinmei if (m->m_len >= offset + sizeof(struct ip6_frag)) { 561170275Sjinmei /* This is the only possible case with !PULLDOWN_TEST */ 56253541Sshin ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag), 563170275Sjinmei offset); 56462587Sitojun m->m_data += sizeof(struct ip6_frag); 56562587Sitojun m->m_len -= sizeof(struct ip6_frag); 56662587Sitojun } else { 56762587Sitojun /* this comes with no copy if the boundary is on cluster */ 568111119Simp if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) { 56962587Sitojun frag6_remque(q6); 570181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 571184307Srwatson#ifdef MAC 572184307Srwatson mac_ip6q_destroy(q6); 573184307Srwatson#endif 57462587Sitojun free(q6, M_FTABLE); 575181803Sbz V_frag6_nfragpackets--; 57662587Sitojun goto dropfrag; 57762587Sitojun } 57862587Sitojun m_adj(t, sizeof(struct ip6_frag)); 57962587Sitojun m_cat(m, t); 58053541Sshin } 58153541Sshin 58253541Sshin /* 58353541Sshin * Store NXT to the original. 58453541Sshin */ 58553541Sshin { 58653541Sshin char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ 58753541Sshin *prvnxtp = nxt; 58853541Sshin } 58953541Sshin 59053541Sshin frag6_remque(q6); 591181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 592184307Srwatson#ifdef MAC 593184307Srwatson mac_ip6q_reassemble(q6, m); 594184307Srwatson mac_ip6q_destroy(q6); 595184307Srwatson#endif 59653541Sshin free(q6, M_FTABLE); 597181803Sbz V_frag6_nfragpackets--; 59853541Sshin 59953541Sshin if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 60053541Sshin int plen = 0; 60153541Sshin for (t = m; t; t = t->m_next) 60253541Sshin plen += t->m_len; 60353541Sshin m->m_pkthdr.len = plen; 60453541Sshin } 605120891Sume 606181803Sbz V_ip6stat.ip6s_reassembled++; 60753541Sshin in6_ifstat_inc(dstifp, ifs6_reass_ok); 60853541Sshin 60953541Sshin /* 61053541Sshin * Tell launch routine the next header 61153541Sshin */ 61253541Sshin 61353541Sshin *mp = m; 61453541Sshin *offp = offset; 61553541Sshin 616121345Sume IP6Q_UNLOCK(); 61753541Sshin return nxt; 61853541Sshin 61953541Sshin dropfrag: 620121346Sume IP6Q_UNLOCK(); 62153541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 622181803Sbz V_ip6stat.ip6s_fragdropped++; 62353541Sshin m_freem(m); 62453541Sshin return IPPROTO_DONE; 62553541Sshin} 62653541Sshin 62753541Sshin/* 62853541Sshin * Free a fragment reassembly header and all 62953541Sshin * associated datagrams. 63053541Sshin */ 63153541Sshinvoid 632171259Sdelphijfrag6_freef(struct ip6q *q6) 63353541Sshin{ 63453541Sshin struct ip6asfrag *af6, *down6; 63553541Sshin 636121355Sume IP6Q_LOCK_ASSERT(); 637121345Sume 63853541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 63953541Sshin af6 = down6) { 64053541Sshin struct mbuf *m = IP6_REASS_MBUF(af6); 64153541Sshin 64253541Sshin down6 = af6->ip6af_down; 64353541Sshin frag6_deq(af6); 64453541Sshin 64553541Sshin /* 64653541Sshin * Return ICMP time exceeded error for the 1st fragment. 64753541Sshin * Just free other fragments. 64853541Sshin */ 64953541Sshin if (af6->ip6af_off == 0) { 65053541Sshin struct ip6_hdr *ip6; 65153541Sshin 65253541Sshin /* adjust pointer */ 65353541Sshin ip6 = mtod(m, struct ip6_hdr *); 65453541Sshin 655120891Sume /* restore source and destination addresses */ 65653541Sshin ip6->ip6_src = q6->ip6q_src; 65753541Sshin ip6->ip6_dst = q6->ip6q_dst; 65853541Sshin 65953541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 66053541Sshin ICMP6_TIME_EXCEED_REASSEMBLY, 0); 66162587Sitojun } else 66253541Sshin m_freem(m); 66362587Sitojun free(af6, M_FTABLE); 66453541Sshin } 66553541Sshin frag6_remque(q6); 666181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 667184307Srwatson#ifdef MAC 668184307Srwatson mac_ip6q_destroy(q6); 669184307Srwatson#endif 67053541Sshin free(q6, M_FTABLE); 671181803Sbz V_frag6_nfragpackets--; 67253541Sshin} 67353541Sshin 67453541Sshin/* 67553541Sshin * Put an ip fragment on a reassembly chain. 67653541Sshin * Like insque, but pointers in middle of structure. 67753541Sshin */ 67853541Sshinvoid 679171259Sdelphijfrag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6) 68053541Sshin{ 681121345Sume 682121355Sume IP6Q_LOCK_ASSERT(); 683121345Sume 68453541Sshin af6->ip6af_up = up6; 68553541Sshin af6->ip6af_down = up6->ip6af_down; 68653541Sshin up6->ip6af_down->ip6af_up = af6; 68753541Sshin up6->ip6af_down = af6; 68853541Sshin} 68953541Sshin 69053541Sshin/* 69153541Sshin * To frag6_enq as remque is to insque. 69253541Sshin */ 69353541Sshinvoid 694171259Sdelphijfrag6_deq(struct ip6asfrag *af6) 69553541Sshin{ 696121345Sume 697121355Sume IP6Q_LOCK_ASSERT(); 698121345Sume 69953541Sshin af6->ip6af_up->ip6af_down = af6->ip6af_down; 70053541Sshin af6->ip6af_down->ip6af_up = af6->ip6af_up; 70153541Sshin} 70253541Sshin 70353541Sshinvoid 704171259Sdelphijfrag6_insque(struct ip6q *new, struct ip6q *old) 70553541Sshin{ 706121345Sume 707121355Sume IP6Q_LOCK_ASSERT(); 708121345Sume 70953541Sshin new->ip6q_prev = old; 71053541Sshin new->ip6q_next = old->ip6q_next; 71153541Sshin old->ip6q_next->ip6q_prev= new; 71253541Sshin old->ip6q_next = new; 71353541Sshin} 71453541Sshin 71553541Sshinvoid 716171259Sdelphijfrag6_remque(struct ip6q *p6) 71753541Sshin{ 718121345Sume 719121355Sume IP6Q_LOCK_ASSERT(); 720121345Sume 72153541Sshin p6->ip6q_prev->ip6q_next = p6->ip6q_next; 72253541Sshin p6->ip6q_next->ip6q_prev = p6->ip6q_prev; 72353541Sshin} 72453541Sshin 72553541Sshin/* 72678064Sume * IPv6 reassembling timer processing; 72753541Sshin * if a timer expires on a reassembly 72853541Sshin * queue, discard it. 72953541Sshin */ 73053541Sshinvoid 731171259Sdelphijfrag6_slowtimo(void) 73253541Sshin{ 733183550Szec VNET_ITERATOR_DECL(vnet_iter); 73453541Sshin struct ip6q *q6; 73553541Sshin 736195760Srwatson VNET_LIST_RLOCK_NOSLEEP(); 737121345Sume IP6Q_LOCK(); 738183550Szec VNET_FOREACH(vnet_iter) { 739183550Szec CURVNET_SET(vnet_iter); 740183550Szec q6 = V_ip6q.ip6q_next; 741183550Szec if (q6) 742183550Szec while (q6 != &V_ip6q) { 743183550Szec --q6->ip6q_ttl; 744183550Szec q6 = q6->ip6q_next; 745183550Szec if (q6->ip6q_prev->ip6q_ttl == 0) { 746183550Szec V_ip6stat.ip6s_fragtimeout++; 747183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 748183550Szec frag6_freef(q6->ip6q_prev); 749183550Szec } 75053541Sshin } 751183550Szec /* 752183550Szec * If we are over the maximum number of fragments 753183550Szec * (due to the limit being lowered), drain off 754183550Szec * enough to get down to the new limit. 755183550Szec */ 756183550Szec while (V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets && 757183550Szec V_ip6q.ip6q_prev) { 758183550Szec V_ip6stat.ip6s_fragoverflow++; 759183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 760183550Szec frag6_freef(V_ip6q.ip6q_prev); 76153541Sshin } 762183550Szec CURVNET_RESTORE(); 76353541Sshin } 764121345Sume IP6Q_UNLOCK(); 765195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 76653541Sshin} 76753541Sshin 76853541Sshin/* 76953541Sshin * Drain off all datagram fragments. 77053541Sshin */ 77153541Sshinvoid 772171259Sdelphijfrag6_drain(void) 77353541Sshin{ 774183550Szec VNET_ITERATOR_DECL(vnet_iter); 775121345Sume 776195760Srwatson VNET_LIST_RLOCK_NOSLEEP(); 777195760Srwatson if (IP6Q_TRYLOCK() == 0) { 778195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 77953541Sshin return; 780195760Srwatson } 781183550Szec VNET_FOREACH(vnet_iter) { 782183550Szec CURVNET_SET(vnet_iter); 783183550Szec while (V_ip6q.ip6q_next != &V_ip6q) { 784183550Szec V_ip6stat.ip6s_fragdropped++; 785183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 786183550Szec frag6_freef(V_ip6q.ip6q_next); 787183550Szec } 788183550Szec CURVNET_RESTORE(); 78953541Sshin } 790121345Sume IP6Q_UNLOCK(); 791195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 79253541Sshin} 793