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$"); 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 558170275Sjinmei /* Delete frag6 header */ 559170275Sjinmei if (m->m_len >= offset + sizeof(struct ip6_frag)) { 560170275Sjinmei /* This is the only possible case with !PULLDOWN_TEST */ 56153541Sshin ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag), 562170275Sjinmei offset); 56362587Sitojun m->m_data += sizeof(struct ip6_frag); 56462587Sitojun m->m_len -= sizeof(struct ip6_frag); 56562587Sitojun } else { 56662587Sitojun /* this comes with no copy if the boundary is on cluster */ 567243882Sglebius if ((t = m_split(m, offset, M_NOWAIT)) == NULL) { 56862587Sitojun frag6_remque(q6); 569181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 570184307Srwatson#ifdef MAC 571184307Srwatson mac_ip6q_destroy(q6); 572184307Srwatson#endif 57362587Sitojun free(q6, M_FTABLE); 574181803Sbz V_frag6_nfragpackets--; 57562587Sitojun goto dropfrag; 57662587Sitojun } 57762587Sitojun m_adj(t, sizeof(struct ip6_frag)); 57862587Sitojun m_cat(m, t); 57953541Sshin } 58053541Sshin 58153541Sshin /* 58253541Sshin * Store NXT to the original. 58353541Sshin */ 58453541Sshin { 58553541Sshin char *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */ 58653541Sshin *prvnxtp = nxt; 58753541Sshin } 58853541Sshin 58953541Sshin frag6_remque(q6); 590181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 591184307Srwatson#ifdef MAC 592184307Srwatson mac_ip6q_reassemble(q6, m); 593184307Srwatson mac_ip6q_destroy(q6); 594184307Srwatson#endif 59553541Sshin free(q6, M_FTABLE); 596181803Sbz V_frag6_nfragpackets--; 59753541Sshin 59853541Sshin if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 59953541Sshin int plen = 0; 60053541Sshin for (t = m; t; t = t->m_next) 60153541Sshin plen += t->m_len; 60253541Sshin m->m_pkthdr.len = plen; 60353541Sshin } 604120891Sume 605249294Sae IP6STAT_INC(ip6s_reassembled); 60653541Sshin in6_ifstat_inc(dstifp, ifs6_reass_ok); 60753541Sshin 60853541Sshin /* 60953541Sshin * Tell launch routine the next header 61053541Sshin */ 61153541Sshin 61253541Sshin *mp = m; 61353541Sshin *offp = offset; 61453541Sshin 615121345Sume IP6Q_UNLOCK(); 61653541Sshin return nxt; 61753541Sshin 61853541Sshin dropfrag: 619121346Sume IP6Q_UNLOCK(); 62053541Sshin in6_ifstat_inc(dstifp, ifs6_reass_fail); 621249294Sae IP6STAT_INC(ip6s_fragdropped); 62253541Sshin m_freem(m); 62353541Sshin return IPPROTO_DONE; 62453541Sshin} 62553541Sshin 62653541Sshin/* 62753541Sshin * Free a fragment reassembly header and all 62853541Sshin * associated datagrams. 62953541Sshin */ 63053541Sshinvoid 631171259Sdelphijfrag6_freef(struct ip6q *q6) 63253541Sshin{ 63353541Sshin struct ip6asfrag *af6, *down6; 63453541Sshin 635121355Sume IP6Q_LOCK_ASSERT(); 636121345Sume 63753541Sshin for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6; 63853541Sshin af6 = down6) { 63953541Sshin struct mbuf *m = IP6_REASS_MBUF(af6); 64053541Sshin 64153541Sshin down6 = af6->ip6af_down; 64253541Sshin frag6_deq(af6); 64353541Sshin 64453541Sshin /* 64553541Sshin * Return ICMP time exceeded error for the 1st fragment. 64653541Sshin * Just free other fragments. 64753541Sshin */ 64853541Sshin if (af6->ip6af_off == 0) { 64953541Sshin struct ip6_hdr *ip6; 65053541Sshin 65153541Sshin /* adjust pointer */ 65253541Sshin ip6 = mtod(m, struct ip6_hdr *); 65353541Sshin 654120891Sume /* restore source and destination addresses */ 65553541Sshin ip6->ip6_src = q6->ip6q_src; 65653541Sshin ip6->ip6_dst = q6->ip6q_dst; 65753541Sshin 65853541Sshin icmp6_error(m, ICMP6_TIME_EXCEEDED, 65953541Sshin ICMP6_TIME_EXCEED_REASSEMBLY, 0); 66062587Sitojun } else 66153541Sshin m_freem(m); 66262587Sitojun free(af6, M_FTABLE); 66353541Sshin } 66453541Sshin frag6_remque(q6); 665181803Sbz V_frag6_nfrags -= q6->ip6q_nfrag; 666184307Srwatson#ifdef MAC 667184307Srwatson mac_ip6q_destroy(q6); 668184307Srwatson#endif 66953541Sshin free(q6, M_FTABLE); 670181803Sbz V_frag6_nfragpackets--; 67153541Sshin} 67253541Sshin 67353541Sshin/* 67453541Sshin * Put an ip fragment on a reassembly chain. 67553541Sshin * Like insque, but pointers in middle of structure. 67653541Sshin */ 67753541Sshinvoid 678171259Sdelphijfrag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6) 67953541Sshin{ 680121345Sume 681121355Sume IP6Q_LOCK_ASSERT(); 682121345Sume 68353541Sshin af6->ip6af_up = up6; 68453541Sshin af6->ip6af_down = up6->ip6af_down; 68553541Sshin up6->ip6af_down->ip6af_up = af6; 68653541Sshin up6->ip6af_down = af6; 68753541Sshin} 68853541Sshin 68953541Sshin/* 69053541Sshin * To frag6_enq as remque is to insque. 69153541Sshin */ 69253541Sshinvoid 693171259Sdelphijfrag6_deq(struct ip6asfrag *af6) 69453541Sshin{ 695121345Sume 696121355Sume IP6Q_LOCK_ASSERT(); 697121345Sume 69853541Sshin af6->ip6af_up->ip6af_down = af6->ip6af_down; 69953541Sshin af6->ip6af_down->ip6af_up = af6->ip6af_up; 70053541Sshin} 70153541Sshin 70253541Sshinvoid 703171259Sdelphijfrag6_insque(struct ip6q *new, struct ip6q *old) 70453541Sshin{ 705121345Sume 706121355Sume IP6Q_LOCK_ASSERT(); 707121345Sume 70853541Sshin new->ip6q_prev = old; 70953541Sshin new->ip6q_next = old->ip6q_next; 71053541Sshin old->ip6q_next->ip6q_prev= new; 71153541Sshin old->ip6q_next = new; 71253541Sshin} 71353541Sshin 71453541Sshinvoid 715171259Sdelphijfrag6_remque(struct ip6q *p6) 71653541Sshin{ 717121345Sume 718121355Sume IP6Q_LOCK_ASSERT(); 719121345Sume 72053541Sshin p6->ip6q_prev->ip6q_next = p6->ip6q_next; 72153541Sshin p6->ip6q_next->ip6q_prev = p6->ip6q_prev; 72253541Sshin} 72353541Sshin 72453541Sshin/* 72578064Sume * IPv6 reassembling timer processing; 72653541Sshin * if a timer expires on a reassembly 72753541Sshin * queue, discard it. 72853541Sshin */ 72953541Sshinvoid 730171259Sdelphijfrag6_slowtimo(void) 73153541Sshin{ 732183550Szec VNET_ITERATOR_DECL(vnet_iter); 73353541Sshin struct ip6q *q6; 73453541Sshin 735195760Srwatson VNET_LIST_RLOCK_NOSLEEP(); 736121345Sume IP6Q_LOCK(); 737183550Szec VNET_FOREACH(vnet_iter) { 738183550Szec CURVNET_SET(vnet_iter); 739183550Szec q6 = V_ip6q.ip6q_next; 740183550Szec if (q6) 741183550Szec while (q6 != &V_ip6q) { 742183550Szec --q6->ip6q_ttl; 743183550Szec q6 = q6->ip6q_next; 744183550Szec if (q6->ip6q_prev->ip6q_ttl == 0) { 745249294Sae IP6STAT_INC(ip6s_fragtimeout); 746183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 747183550Szec frag6_freef(q6->ip6q_prev); 748183550Szec } 74953541Sshin } 750183550Szec /* 751183550Szec * If we are over the maximum number of fragments 752183550Szec * (due to the limit being lowered), drain off 753183550Szec * enough to get down to the new limit. 754183550Szec */ 755183550Szec while (V_frag6_nfragpackets > (u_int)V_ip6_maxfragpackets && 756183550Szec V_ip6q.ip6q_prev) { 757249294Sae IP6STAT_INC(ip6s_fragoverflow); 758183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 759183550Szec frag6_freef(V_ip6q.ip6q_prev); 76053541Sshin } 761183550Szec CURVNET_RESTORE(); 76253541Sshin } 763121345Sume IP6Q_UNLOCK(); 764195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 76553541Sshin} 76653541Sshin 76753541Sshin/* 76853541Sshin * Drain off all datagram fragments. 76953541Sshin */ 77053541Sshinvoid 771171259Sdelphijfrag6_drain(void) 77253541Sshin{ 773183550Szec VNET_ITERATOR_DECL(vnet_iter); 774121345Sume 775195760Srwatson VNET_LIST_RLOCK_NOSLEEP(); 776195760Srwatson if (IP6Q_TRYLOCK() == 0) { 777195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 77853541Sshin return; 779195760Srwatson } 780183550Szec VNET_FOREACH(vnet_iter) { 781183550Szec CURVNET_SET(vnet_iter); 782183550Szec while (V_ip6q.ip6q_next != &V_ip6q) { 783249294Sae IP6STAT_INC(ip6s_fragdropped); 784183550Szec /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 785183550Szec frag6_freef(V_ip6q.ip6q_next); 786183550Szec } 787183550Szec CURVNET_RESTORE(); 78853541Sshin } 789121345Sume IP6Q_UNLOCK(); 790195760Srwatson VNET_LIST_RUNLOCK_NOSLEEP(); 79153541Sshin} 792