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: releng/10.3/sys/netinet6/frag6.c 284568 2015-06-18 20:21:02Z kp $"); 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 218249294Sae IP6STAT_INC(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 /* 225255792Sbz * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0) 226255792Sbz * upfront, unrelated to any reassembly. Just skip the fragment header. 227238248Sbz */ 228238248Sbz if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) { 229238248Sbz /* XXX-BZ we want dedicated counters for this. */ 230249294Sae IP6STAT_INC(ip6s_reassembled); 231238248Sbz in6_ifstat_inc(dstifp, ifs6_reass_ok); 232238248Sbz *offp = offset; 233238248Sbz return (ip6f->ip6f_nxt); 234238248Sbz } 235238248Sbz 236121345Sume IP6Q_LOCK(); 23778064Sume 238121345Sume /* 239121345Sume * Enforce upper bound on number of fragments. 240121345Sume * If maxfrag is 0, never accept fragments. 241121345Sume * If maxfrag is -1, accept all fragments without limitation. 242121345Sume */ 243181803Sbz if (V_ip6_maxfrags < 0) 244121345Sume ; 245181803Sbz else if (V_frag6_nfrags >= (u_int)V_ip6_maxfrags) 246121345Sume goto dropfrag; 247121345Sume 248181803Sbz for (q6 = V_ip6q.ip6q_next; q6 != &V_ip6q; q6 = q6->ip6q_next) 24953541Sshin if (ip6f->ip6f_ident == q6->ip6q_ident && 25053541Sshin IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && 251184307Srwatson IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst) 252184307Srwatson#ifdef MAC 253184307Srwatson && mac_ip6q_match(m, q6) 254184307Srwatson#endif 255184307Srwatson ) 25653541Sshin break; 25753541Sshin 258181803Sbz if (q6 == &V_ip6q) { 25953541Sshin /* 26053541Sshin * the first fragment to arrive, create a reassembly queue. 26153541Sshin */ 26253541Sshin first_frag = 1; 26353541Sshin 26453541Sshin /* 26553541Sshin * Enforce upper bound on number of fragmented packets 26653541Sshin * for which we attempt reassembly; 267121345Sume * If maxfragpackets is 0, never accept fragments. 268121345Sume * If maxfragpackets is -1, accept all fragments without 269121345Sume * limitation. 27053541Sshin */ 271181803Sbz if (V_ip6_maxfragpackets < 0) 27278064Sume ; 273181803Sbz else if (V_frag6_nfragpackets >= (u_int)V_ip6_maxfragpackets) 27478064Sume goto dropfrag; 275181803Sbz V_frag6_nfragpackets++; 27653541Sshin q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE, 277121607Sume M_NOWAIT); 27853541Sshin if (q6 == NULL) 27953541Sshin goto dropfrag; 28062587Sitojun bzero(q6, sizeof(*q6)); 281184307Srwatson#ifdef MAC 282184307Srwatson if (mac_ip6q_init(q6, M_NOWAIT) != 0) { 283184307Srwatson free(q6, M_FTABLE); 284184307Srwatson goto dropfrag; 285184307Srwatson } 286184307Srwatson mac_ip6q_create(m, q6); 287184307Srwatson#endif 288181803Sbz frag6_insque(q6, &V_ip6q); 28953541Sshin 29062587Sitojun /* ip6q_nxt will be filled afterwards, from 1st fragment */ 29153541Sshin q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6; 29262587Sitojun#ifdef notyet 29362587Sitojun q6->ip6q_nxtp = (u_char *)nxtp; 29462587Sitojun#endif 29553541Sshin q6->ip6q_ident = ip6f->ip6f_ident; 296171260Sdelphij q6->ip6q_ttl = IPV6_FRAGTTL; 29753541Sshin q6->ip6q_src = ip6->ip6_src; 29853541Sshin q6->ip6q_dst = ip6->ip6_dst; 299170275Sjinmei q6->ip6q_ecn = 300170275Sjinmei (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; 30153541Sshin q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ 302121345Sume 303121345Sume q6->ip6q_nfrag = 0; 30453541Sshin } 30553541Sshin 30653541Sshin /* 30753541Sshin * If it's the 1st fragment, record the length of the 30853541Sshin * unfragmentable part and the next header of the fragment header. 30953541Sshin */ 31053541Sshin fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); 31153541Sshin if (fragoff == 0) { 312120891Sume q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - 313120891Sume sizeof(struct ip6_frag); 31453541Sshin q6->ip6q_nxt = ip6f->ip6f_nxt; 31553541Sshin } 31653541Sshin 31753541Sshin /* 31853541Sshin * Check that the reassembled packet would not exceed 65535 bytes 31953541Sshin * in size. 32053541Sshin * If it would exceed, discard the fragment and return an ICMP error. 32153541Sshin */ 32262587Sitojun frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; 32353541Sshin if (q6->ip6q_unfrglen >= 0) { 32453541Sshin /* The 1st fragment has already arrived. */ 32553541Sshin if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { 32653541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 327120891Sume offset - sizeof(struct ip6_frag) + 328120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 329121345Sume IP6Q_UNLOCK(); 330120856Sume return (IPPROTO_DONE); 33153541Sshin } 332120891Sume } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { 33353541Sshin icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 334120891Sume offset - sizeof(struct ip6_frag) + 335120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 336121345Sume IP6Q_UNLOCK(); 337120856Sume return (IPPROTO_DONE); 33853541Sshin } 33953541Sshin /* 34053541Sshin * If it's the first fragment, do the above check for each 34153541Sshin * fragment already stored in the reassembly queue. 34253541Sshin */ 34353541Sshin if (fragoff == 0) { 34453541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 34553541Sshin af6 = af6dwn) { 34653541Sshin af6dwn = af6->ip6af_down; 34753541Sshin 34853541Sshin if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen > 34953541Sshin IPV6_MAXPACKET) { 35053541Sshin struct mbuf *merr = IP6_REASS_MBUF(af6); 35153541Sshin struct ip6_hdr *ip6err; 35253541Sshin int erroff = af6->ip6af_offset; 35353541Sshin 35453541Sshin /* dequeue the fragment. */ 35553541Sshin frag6_deq(af6); 35662587Sitojun free(af6, M_FTABLE); 35753541Sshin 35853541Sshin /* adjust pointer. */ 35953541Sshin ip6err = mtod(merr, struct ip6_hdr *); 36053541Sshin 36153541Sshin /* 36253541Sshin * Restore source and destination addresses 36353541Sshin * in the erroneous IPv6 header. 36453541Sshin */ 36553541Sshin ip6err->ip6_src = q6->ip6q_src; 36653541Sshin ip6err->ip6_dst = q6->ip6q_dst; 36753541Sshin 36853541Sshin icmp6_error(merr, ICMP6_PARAM_PROB, 369120891Sume ICMP6_PARAMPROB_HEADER, 370120891Sume erroff - sizeof(struct ip6_frag) + 371120891Sume offsetof(struct ip6_frag, ip6f_offlg)); 37253541Sshin } 37353541Sshin } 37453541Sshin } 37553541Sshin 37662587Sitojun ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE, 377121607Sume M_NOWAIT); 37862587Sitojun if (ip6af == NULL) 37962587Sitojun goto dropfrag; 38062587Sitojun bzero(ip6af, sizeof(*ip6af)); 38153541Sshin ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG; 38253541Sshin ip6af->ip6af_off = fragoff; 38353541Sshin ip6af->ip6af_frglen = frgpartlen; 38453541Sshin ip6af->ip6af_offset = offset; 38553541Sshin IP6_REASS_MBUF(ip6af) = m; 38653541Sshin 38753541Sshin if (first_frag) { 38853541Sshin af6 = (struct ip6asfrag *)q6; 38953541Sshin goto insert; 39053541Sshin } 39153541Sshin 39253541Sshin /* 393121684Sume * Handle ECN by comparing this segment with the first one; 394121684Sume * if CE is set, do not lose CE. 395121684Sume * drop if CE and not-ECT are mixed for the same packet. 396121684Sume */ 397121684Sume ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK; 398170275Sjinmei ecn0 = q6->ip6q_ecn; 399121684Sume if (ecn == IPTOS_ECN_CE) { 400121684Sume if (ecn0 == IPTOS_ECN_NOTECT) { 401121684Sume free(ip6af, M_FTABLE); 402121684Sume goto dropfrag; 403121684Sume } 404121684Sume if (ecn0 != IPTOS_ECN_CE) 405170275Sjinmei q6->ip6q_ecn = IPTOS_ECN_CE; 406121684Sume } 407121684Sume if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) { 408121684Sume free(ip6af, M_FTABLE); 409121684Sume goto dropfrag; 410121684Sume } 411121684Sume 412121684Sume /* 41353541Sshin * Find a segment which begins after this one does. 41453541Sshin */ 41553541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 41653541Sshin af6 = af6->ip6af_down) 41753541Sshin if (af6->ip6af_off > ip6af->ip6af_off) 41853541Sshin break; 41953541Sshin 42062587Sitojun#if 0 42153541Sshin /* 42262587Sitojun * If there is a preceding segment, it may provide some of 42362587Sitojun * our data already. If so, drop the data from the incoming 42462587Sitojun * segment. If it provides all of our data, drop us. 42562587Sitojun */ 42662587Sitojun if (af6->ip6af_up != (struct ip6asfrag *)q6) { 42762587Sitojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 42862587Sitojun - ip6af->ip6af_off; 42962587Sitojun if (i > 0) { 43062587Sitojun if (i >= ip6af->ip6af_frglen) 43162587Sitojun goto dropfrag; 43262587Sitojun m_adj(IP6_REASS_MBUF(ip6af), i); 43362587Sitojun ip6af->ip6af_off += i; 43462587Sitojun ip6af->ip6af_frglen -= i; 43562587Sitojun } 43662587Sitojun } 43762587Sitojun 43862587Sitojun /* 43962587Sitojun * While we overlap succeeding segments trim them or, 44062587Sitojun * if they are completely covered, dequeue them. 44162587Sitojun */ 44262587Sitojun while (af6 != (struct ip6asfrag *)q6 && 44362587Sitojun ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) { 44462587Sitojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 44562587Sitojun if (i < af6->ip6af_frglen) { 44662587Sitojun af6->ip6af_frglen -= i; 44762587Sitojun af6->ip6af_off += i; 44862587Sitojun m_adj(IP6_REASS_MBUF(af6), i); 44962587Sitojun break; 45062587Sitojun } 45162587Sitojun af6 = af6->ip6af_down; 45262587Sitojun m_freem(IP6_REASS_MBUF(af6->ip6af_up)); 45362587Sitojun frag6_deq(af6->ip6af_up); 45462587Sitojun } 45562587Sitojun#else 45662587Sitojun /* 45753541Sshin * If the incoming framgent overlaps some existing fragments in 45853541Sshin * the reassembly queue, drop it, since it is dangerous to override 45953541Sshin * existing fragments from a security point of view. 460121345Sume * We don't know which fragment is the bad guy - here we trust 461121345Sume * fragment that came in earlier, with no real reason. 462170275Sjinmei * 463170275Sjinmei * Note: due to changes after disabling this part, mbuf passed to 464170275Sjinmei * m_adj() below now does not meet the requirement. 46553541Sshin */ 46653541Sshin if (af6->ip6af_up != (struct ip6asfrag *)q6) { 46753541Sshin i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen 46853541Sshin - ip6af->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 previous fragment\n", 473165118Sbz i, ip6_sprintf(ip6buf, &q6->ip6q_src)); 47476899Ssumikawa#endif 47576899Ssumikawa free(ip6af, M_FTABLE); 47653541Sshin goto dropfrag; 47753541Sshin } 47853541Sshin } 47953541Sshin if (af6 != (struct ip6asfrag *)q6) { 48053541Sshin i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off; 48153541Sshin if (i > 0) { 48276899Ssumikawa#if 0 /* suppress the noisy log */ 48353541Sshin log(LOG_ERR, "%d bytes of a fragment from %s " 48453541Sshin "overlaps the succeeding fragment", 485165118Sbz i, ip6_sprintf(ip6buf, &q6->ip6q_src)); 48676899Ssumikawa#endif 48776899Ssumikawa free(ip6af, M_FTABLE); 48853541Sshin goto dropfrag; 48953541Sshin } 49053541Sshin } 49162587Sitojun#endif 49253541Sshin 49353541Sshininsert: 494184307Srwatson#ifdef MAC 495184307Srwatson if (!first_frag) 496184307Srwatson mac_ip6q_update(m, q6); 497184307Srwatson#endif 49853541Sshin 49953541Sshin /* 50053541Sshin * Stick new segment in its place; 50153541Sshin * check for complete reassembly. 50253541Sshin * Move to front of packet queue, as we are 50353541Sshin * the most recently active fragmented packet. 50453541Sshin */ 50553541Sshin frag6_enq(ip6af, af6->ip6af_up); 506181803Sbz V_frag6_nfrags++; 507121345Sume q6->ip6q_nfrag++; 50862587Sitojun#if 0 /* xxx */ 509181803Sbz if (q6 != V_ip6q.ip6q_next) { 51062587Sitojun frag6_remque(q6); 511181803Sbz frag6_insque(q6, &V_ip6q); 51262587Sitojun } 51362587Sitojun#endif 51453541Sshin next = 0; 51553541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 51653541Sshin af6 = af6->ip6af_down) { 51753541Sshin if (af6->ip6af_off != next) { 518121345Sume IP6Q_UNLOCK(); 51953541Sshin return IPPROTO_DONE; 52053541Sshin } 52153541Sshin next += af6->ip6af_frglen; 52253541Sshin } 52353541Sshin if (af6->ip6af_up->ip6af_mff) { 524121345Sume IP6Q_UNLOCK(); 52553541Sshin return IPPROTO_DONE; 52653541Sshin } 52753541Sshin 52853541Sshin /* 52953541Sshin * Reassembly is complete; concatenate fragments. 53053541Sshin */ 53153541Sshin ip6af = q6->ip6q_down; 53253541Sshin t = m = IP6_REASS_MBUF(ip6af); 53353541Sshin af6 = ip6af->ip6af_down; 53462587Sitojun frag6_deq(ip6af); 53553541Sshin while (af6 != (struct ip6asfrag *)q6) { 53662587Sitojun af6dwn = af6->ip6af_down; 53762587Sitojun frag6_deq(af6); 53853541Sshin while (t->m_next) 53953541Sshin t = t->m_next; 54053541Sshin t->m_next = IP6_REASS_MBUF(af6); 54162587Sitojun m_adj(t->m_next, af6->ip6af_offset); 54262587Sitojun free(af6, M_FTABLE); 54362587Sitojun af6 = af6dwn; 54453541Sshin } 54553541Sshin 54653541Sshin /* adjust offset to point where the original next header starts */ 54753541Sshin offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); 54862587Sitojun free(ip6af, M_FTABLE); 54962587Sitojun ip6 = mtod(m, struct ip6_hdr *); 55053541Sshin ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr)); 551170275Sjinmei if (q6->ip6q_ecn == IPTOS_ECN_CE) 552170275Sjinmei ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20); 55353541Sshin nxt = q6->ip6q_nxt; 55462587Sitojun#ifdef notyet 55562587Sitojun *q6->ip6q_nxtp = (u_char)(nxt & 0xff); 55662587Sitojun#endif 55753541Sshin 558284568Skp if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) { 559284568Skp frag6_remque(q6); 560284568Skp V_frag6_nfrags -= q6->ip6q_nfrag; 561184307Srwatson#ifdef MAC 562284568Skp mac_ip6q_destroy(q6); 563184307Srwatson#endif 564284568Skp free(q6, M_FTABLE); 565284568Skp V_frag6_nfragpackets--; 566284568Skp 567284568Skp goto dropfrag; 56853541Sshin } 56953541Sshin 57053541Sshin /* 57153541Sshin * Store NXT to the original. 57253541Sshin */ 57353541Sshin { 57453541Sshin char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ 57553541Sshin *prvnxtp = nxt; 57653541Sshin } 57753541Sshin 57853541Sshin frag6_remque(q6); 579181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 580184307Srwatson#ifdef MAC 581184307Srwatson mac_ip6q_reassemble(q6, m); 582184307Srwatson mac_ip6q_destroy(q6); 583184307Srwatson#endif 58453541Sshin free(q6, M_FTABLE); 585181803Sbz V_frag6_nfragpackets--; 58653541Sshin 58753541Sshin if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 58853541Sshin int plen = 0; 58953541Sshin for (t = m; t; t = t->m_next) 59053541Sshin plen += t->m_len; 59153541Sshin m->m_pkthdr.len = plen; 59253541Sshin } 593120891Sume 594249294Sae IP6STAT_INC(ip6s_reassembled); 59553541Sshin in6_ifstat_inc(dstifp, ifs6_reass_ok); 59653541Sshin 59753541Sshin /* 59853541Sshin * Tell launch routine the next header 59953541Sshin */ 60053541Sshin 60153541Sshin *mp = m; 60253541Sshin *offp = offset; 60353541Sshin 604121345Sume IP6Q_UNLOCK(); 60553541Sshin return nxt; 60653541Sshin 60753541Sshin dropfrag: 608121346Sume IP6Q_UNLOCK(); 60953541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 610249294Sae IP6STAT_INC(ip6s_fragdropped); 61153541Sshin m_freem(m); 61253541Sshin return IPPROTO_DONE; 61353541Sshin} 61453541Sshin 61553541Sshin/* 61653541Sshin * Free a fragment reassembly header and all 61753541Sshin * associated datagrams. 61853541Sshin */ 61953541Sshinvoid 620171259Sdelphijfrag6_freef(struct ip6q *q6) 62153541Sshin{ 62253541Sshin struct ip6asfrag *af6, *down6; 62353541Sshin 624121355Sume IP6Q_LOCK_ASSERT(); 625121345Sume 62653541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 62753541Sshin af6 = down6) { 62853541Sshin struct mbuf *m = IP6_REASS_MBUF(af6); 62953541Sshin 63053541Sshin down6 = af6->ip6af_down; 63153541Sshin frag6_deq(af6); 63253541Sshin 63353541Sshin /* 63453541Sshin * Return ICMP time exceeded error for the 1st fragment. 63553541Sshin * Just free other fragments. 63653541Sshin */ 63753541Sshin if (af6->ip6af_off == 0) { 63853541Sshin struct ip6_hdr *ip6; 63953541Sshin 64053541Sshin /* adjust pointer */ 64153541Sshin ip6 = mtod(m, struct ip6_hdr *); 64253541Sshin 643120891Sume /* restore source and destination addresses */ 64453541Sshin ip6->ip6_src = q6->ip6q_src; 64553541Sshin ip6->ip6_dst = q6->ip6q_dst; 64653541Sshin 64753541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 64853541Sshin ICMP6_TIME_EXCEED_REASSEMBLY, 0); 64962587Sitojun } else 65053541Sshin m_freem(m); 65162587Sitojun free(af6, M_FTABLE); 65253541Sshin } 65353541Sshin frag6_remque(q6); 654181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 655184307Srwatson#ifdef MAC 656184307Srwatson mac_ip6q_destroy(q6); 657184307Srwatson#endif 65853541Sshin free(q6, M_FTABLE); 659181803Sbz V_frag6_nfragpackets--; 66053541Sshin} 66153541Sshin 66253541Sshin/* 66353541Sshin * Put an ip fragment on a reassembly chain. 66453541Sshin * Like insque, but pointers in middle of structure. 66553541Sshin */ 66653541Sshinvoid 667171259Sdelphijfrag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6) 66853541Sshin{ 669121345Sume 670121355Sume IP6Q_LOCK_ASSERT(); 671121345Sume 67253541Sshin af6->ip6af_up = up6; 67353541Sshin af6->ip6af_down = up6->ip6af_down; 67453541Sshin up6->ip6af_down->ip6af_up = af6; 67553541Sshin up6->ip6af_down = af6; 67653541Sshin} 67753541Sshin 67853541Sshin/* 67953541Sshin * To frag6_enq as remque is to insque. 68053541Sshin */ 68153541Sshinvoid 682171259Sdelphijfrag6_deq(struct ip6asfrag *af6) 68353541Sshin{ 684121345Sume 685121355Sume IP6Q_LOCK_ASSERT(); 686121345Sume 68753541Sshin af6->ip6af_up->ip6af_down = af6->ip6af_down; 68853541Sshin af6->ip6af_down->ip6af_up = af6->ip6af_up; 68953541Sshin} 69053541Sshin 69153541Sshinvoid 692171259Sdelphijfrag6_insque(struct ip6q *new, struct ip6q *old) 69353541Sshin{ 694121345Sume 695121355Sume IP6Q_LOCK_ASSERT(); 696121345Sume 69753541Sshin new->ip6q_prev = old; 69853541Sshin new->ip6q_next = old->ip6q_next; 69953541Sshin old->ip6q_next->ip6q_prev= new; 70053541Sshin old->ip6q_next = new; 70153541Sshin} 70253541Sshin 70353541Sshinvoid 704171259Sdelphijfrag6_remque(struct ip6q *p6) 70553541Sshin{ 706121345Sume 707121355Sume IP6Q_LOCK_ASSERT(); 708121345Sume 70953541Sshin p6->ip6q_prev->ip6q_next = p6->ip6q_next; 71053541Sshin p6->ip6q_next->ip6q_prev = p6->ip6q_prev; 71153541Sshin} 71253541Sshin 71353541Sshin/* 71478064Sume * IPv6 reassembling timer processing; 71553541Sshin * if a timer expires on a reassembly 71653541Sshin * queue, discard it. 71753541Sshin */ 71853541Sshinvoid 719171259Sdelphijfrag6_slowtimo(void) 72053541Sshin{ 721183550Szec VNET_ITERATOR_DECL(vnet_iter); 72253541Sshin struct ip6q *q6; 72353541Sshin 724195760Srwatson VNET_LIST_RLOCK_NOSLEEP(); 725121345Sume IP6Q_LOCK(); 726183550Szec VNET_FOREACH(vnet_iter) { 727183550Szec CURVNET_SET(vnet_iter); 728183550Szec q6 = V_ip6q.ip6q_next; 729183550Szec if (q6) 730183550Szec while (q6 != &V_ip6q) { 731183550Szec --q6->ip6q_ttl; 732183550Szec q6 = q6->ip6q_next; 733183550Szec if (q6->ip6q_prev->ip6q_ttl == 0) { 734249294Sae IP6STAT_INC(ip6s_fragtimeout); 735183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 736183550Szec frag6_freef(q6->ip6q_prev); 737183550Szec } 73853541Sshin } 739183550Szec /* 740183550Szec * If we are over the maximum number of fragments 741183550Szec * (due to the limit being lowered), drain off 742183550Szec * enough to get down to the new limit. 743183550Szec */ 744183550Szec while (V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets && 745183550Szec V_ip6q.ip6q_prev) { 746249294Sae IP6STAT_INC(ip6s_fragoverflow); 747183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 748183550Szec frag6_freef(V_ip6q.ip6q_prev); 74953541Sshin } 750183550Szec CURVNET_RESTORE(); 75153541Sshin } 752121345Sume IP6Q_UNLOCK(); 753195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 75453541Sshin} 75553541Sshin 75653541Sshin/* 75753541Sshin * Drain off all datagram fragments. 75853541Sshin */ 75953541Sshinvoid 760171259Sdelphijfrag6_drain(void) 76153541Sshin{ 762183550Szec VNET_ITERATOR_DECL(vnet_iter); 763121345Sume 764195760Srwatson VNET_LIST_RLOCK_NOSLEEP(); 765195760Srwatson if (IP6Q_TRYLOCK() == 0) { 766195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 76753541Sshin return; 768195760Srwatson } 769183550Szec VNET_FOREACH(vnet_iter) { 770183550Szec CURVNET_SET(vnet_iter); 771183550Szec while (V_ip6q.ip6q_next != &V_ip6q) { 772249294Sae IP6STAT_INC(ip6s_fragdropped); 773183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 774183550Szec frag6_freef(V_ip6q.ip6q_next); 775183550Szec } 776183550Szec CURVNET_RESTORE(); 77753541Sshin } 778121345Sume IP6Q_UNLOCK(); 779195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 78053541Sshin} 781284568Skp 782284568Skpint 783284568Skpip6_deletefraghdr(struct mbuf *m, int offset, int wait) 784284568Skp{ 785284568Skp struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); 786284568Skp struct mbuf *t; 787284568Skp 788284568Skp /* Delete frag6 header. */ 789284568Skp if (m->m_len >= offset + sizeof(struct ip6_frag)) { 790284568Skp /* This is the only possible case with !PULLDOWN_TEST. */ 791284568Skp bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag), 792284568Skp offset); 793284568Skp m->m_data += sizeof(struct ip6_frag); 794284568Skp m->m_len -= sizeof(struct ip6_frag); 795284568Skp } else { 796284568Skp /* This comes with no copy if the boundary is on cluster. */ 797284568Skp if ((t = m_split(m, offset, wait)) == NULL) 798284568Skp return (ENOMEM); 799284568Skp m_adj(t, sizeof(struct ip6_frag)); 800284568Skp m_cat(m, t); 801284568Skp } 802284568Skp 803284568Skp return (0); 804284568Skp} 805