1caf43b02SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 482cd038dSYoshinobu Inoue * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 582cd038dSYoshinobu Inoue * All rights reserved. 621f08a07SBjoern A. Zeeb * Copyright (c) 2019 Netflix, Inc. 782cd038dSYoshinobu Inoue * 882cd038dSYoshinobu Inoue * Redistribution and use in source and binary forms, with or without 982cd038dSYoshinobu Inoue * modification, are permitted provided that the following conditions 1082cd038dSYoshinobu Inoue * are met: 1182cd038dSYoshinobu Inoue * 1. Redistributions of source code must retain the above copyright 1282cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer. 1382cd038dSYoshinobu Inoue * 2. Redistributions in binary form must reproduce the above copyright 1482cd038dSYoshinobu Inoue * notice, this list of conditions and the following disclaimer in the 1582cd038dSYoshinobu Inoue * documentation and/or other materials provided with the distribution. 1682cd038dSYoshinobu Inoue * 3. Neither the name of the project nor the names of its contributors 1782cd038dSYoshinobu Inoue * may be used to endorse or promote products derived from this software 1882cd038dSYoshinobu Inoue * without specific prior written permission. 1982cd038dSYoshinobu Inoue * 2082cd038dSYoshinobu Inoue * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 2182cd038dSYoshinobu Inoue * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2282cd038dSYoshinobu Inoue * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2382cd038dSYoshinobu Inoue * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 2482cd038dSYoshinobu Inoue * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2582cd038dSYoshinobu Inoue * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2682cd038dSYoshinobu Inoue * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2782cd038dSYoshinobu Inoue * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2882cd038dSYoshinobu Inoue * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2982cd038dSYoshinobu Inoue * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3082cd038dSYoshinobu Inoue * SUCH DAMAGE. 31b48287a3SDavid E. O'Brien * 32b48287a3SDavid E. O'Brien * $KAME: frag6.c,v 1.33 2002/01/07 11:34:48 kjc Exp $ 3382cd038dSYoshinobu Inoue */ 3482cd038dSYoshinobu Inoue 35b48287a3SDavid E. O'Brien #include <sys/cdefs.h> 36b48287a3SDavid E. O'Brien __FBSDID("$FreeBSD$"); 37b48287a3SDavid E. O'Brien 38aaa46574SAdrian Chadd #include "opt_rss.h" 39aaa46574SAdrian Chadd 4082cd038dSYoshinobu Inoue #include <sys/param.h> 41f349c821SBjoern A. Zeeb #include <sys/systm.h> 421a3044faSBjoern A. Zeeb #include <sys/domain.h> 431a3044faSBjoern A. Zeeb #include <sys/eventhandler.h> 4480d7a853SJonathan T. Looney #include <sys/hash.h> 451a3044faSBjoern A. Zeeb #include <sys/kernel.h> 4682cd038dSYoshinobu Inoue #include <sys/malloc.h> 4782cd038dSYoshinobu Inoue #include <sys/mbuf.h> 4882cd038dSYoshinobu Inoue #include <sys/protosw.h> 4921f08a07SBjoern A. Zeeb #include <sys/queue.h> 5082cd038dSYoshinobu Inoue #include <sys/socket.h> 51757cb678SBjoern A. Zeeb #include <sys/sysctl.h> 5282cd038dSYoshinobu Inoue #include <sys/syslog.h> 5382cd038dSYoshinobu Inoue 5482cd038dSYoshinobu Inoue #include <net/if.h> 5576039bc8SGleb Smirnoff #include <net/if_var.h> 563d0d5b21SJustin Hibbits #include <net/if_private.h> 57aaa46574SAdrian Chadd #include <net/netisr.h> 5882cd038dSYoshinobu Inoue #include <net/route.h> 59eddfbb76SRobert Watson #include <net/vnet.h> 6082cd038dSYoshinobu Inoue 6182cd038dSYoshinobu Inoue #include <netinet/in.h> 6282cd038dSYoshinobu Inoue #include <netinet/in_var.h> 63686cdd19SJun-ichiro itojun Hagino #include <netinet/ip6.h> 6482cd038dSYoshinobu Inoue #include <netinet6/ip6_var.h> 65686cdd19SJun-ichiro itojun Hagino #include <netinet/icmp6.h> 6623d374aaSBjoern A. Zeeb #include <netinet/in_systm.h> /* For ECN definitions. */ 6723d374aaSBjoern A. Zeeb #include <netinet/ip.h> /* For ECN definitions. */ 6882cd038dSYoshinobu Inoue 691a3044faSBjoern A. Zeeb #ifdef MAC 704b908c8bSRobert Watson #include <security/mac/mac_framework.h> 711a3044faSBjoern A. Zeeb #endif 724b908c8bSRobert Watson 73f1664f32SBjoern A. Zeeb /* 74f1664f32SBjoern A. Zeeb * A "big picture" of how IPv6 fragment queues are all linked together. 75f1664f32SBjoern A. Zeeb * 76f1664f32SBjoern A. Zeeb * struct ip6qbucket ip6qb[...]; hashed buckets 77f1664f32SBjoern A. Zeeb * |||||||| 78f1664f32SBjoern A. Zeeb * | 79f1664f32SBjoern A. Zeeb * +--- TAILQ(struct ip6q, packets) *q6; tailq entries holding 80f1664f32SBjoern A. Zeeb * |||||||| fragmented packets 81f1664f32SBjoern A. Zeeb * | (1 per original packet) 82f1664f32SBjoern A. Zeeb * | 83f1664f32SBjoern A. Zeeb * +--- TAILQ(struct ip6asfrag, ip6q_frags) *af6; tailq entries of IPv6 84f1664f32SBjoern A. Zeeb * | *ip6af;fragment packets 85f1664f32SBjoern A. Zeeb * | for one original packet 86f1664f32SBjoern A. Zeeb * + *mbuf 87f1664f32SBjoern A. Zeeb */ 88f1664f32SBjoern A. Zeeb 8923d374aaSBjoern A. Zeeb /* Reassembly headers are stored in hash buckets. */ 902ceeacbeSJonathan T. Looney #define IP6REASS_NHASH_LOG2 10 9180d7a853SJonathan T. Looney #define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2) 9280d7a853SJonathan T. Looney #define IP6REASS_HMASK (IP6REASS_NHASH - 1) 9380d7a853SJonathan T. Looney 9421f08a07SBjoern A. Zeeb TAILQ_HEAD(ip6qhead, ip6q); 9580d7a853SJonathan T. Looney struct ip6qbucket { 9621f08a07SBjoern A. Zeeb struct ip6qhead packets; 9780d7a853SJonathan T. Looney struct mtx lock; 981e9f3b73SJonathan T. Looney int count; 9980d7a853SJonathan T. Looney }; 10080d7a853SJonathan T. Looney 1011540a98eSBjoern A. Zeeb struct ip6asfrag { 10221f08a07SBjoern A. Zeeb TAILQ_ENTRY(ip6asfrag) ip6af_tq; 1031540a98eSBjoern A. Zeeb struct mbuf *ip6af_m; 104f1664f32SBjoern A. Zeeb int ip6af_offset; /* Offset in ip6af_m to next header. */ 105f1664f32SBjoern A. Zeeb int ip6af_frglen; /* Fragmentable part length. */ 106f1664f32SBjoern A. Zeeb int ip6af_off; /* Fragment offset. */ 107f1664f32SBjoern A. Zeeb bool ip6af_mff; /* More fragment bit in frag off. */ 1081540a98eSBjoern A. Zeeb }; 1091540a98eSBjoern A. Zeeb 110487a161cSBjoern A. Zeeb static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header"); 111487a161cSBjoern A. Zeeb 11267a10c46SBjoern A. Zeeb #ifdef VIMAGE 11367a10c46SBjoern A. Zeeb /* A flag to indicate if IPv6 fragmentation is initialized. */ 11467a10c46SBjoern A. Zeeb VNET_DEFINE_STATIC(bool, frag6_on); 11567a10c46SBjoern A. Zeeb #define V_frag6_on VNET(frag6_on) 11667a10c46SBjoern A. Zeeb #endif 11767a10c46SBjoern A. Zeeb 118757cb678SBjoern A. Zeeb /* System wide (global) maximum and count of packets in reassembly queues. */ 119757cb678SBjoern A. Zeeb static int ip6_maxfrags; 120c17ae180SMateusz Guzik static u_int __exclusive_cache_line frag6_nfrags; 121757cb678SBjoern A. Zeeb 122757cb678SBjoern A. Zeeb /* Maximum and current packets in per-VNET reassembly queue. */ 123757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int, ip6_maxfragpackets); 12480d7a853SJonathan T. Looney VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets); 125757cb678SBjoern A. Zeeb #define V_ip6_maxfragpackets VNET(ip6_maxfragpackets) 126757cb678SBjoern A. Zeeb #define V_frag6_nfragpackets VNET(frag6_nfragpackets) 127757cb678SBjoern A. Zeeb 128e32221a1SAlexander V. Chernikov /* Maximum per-VNET reassembly timeout (milliseconds) */ 129e32221a1SAlexander V. Chernikov VNET_DEFINE_STATIC(u_int, ip6_fraglifetime) = IPV6_DEFFRAGTTL; 130e32221a1SAlexander V. Chernikov #define V_ip6_fraglifetime VNET(ip6_fraglifetime) 131e32221a1SAlexander V. Chernikov 132757cb678SBjoern A. Zeeb /* Maximum per-VNET reassembly queues per bucket and fragments per packet. */ 133757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int, ip6_maxfragbucketsize); 134757cb678SBjoern A. Zeeb VNET_DEFINE_STATIC(int, ip6_maxfragsperpacket); 135757cb678SBjoern A. Zeeb #define V_ip6_maxfragbucketsize VNET(ip6_maxfragbucketsize) 136757cb678SBjoern A. Zeeb #define V_ip6_maxfragsperpacket VNET(ip6_maxfragsperpacket) 137757cb678SBjoern A. Zeeb 138757cb678SBjoern A. Zeeb /* Per-VNET reassembly queue buckets. */ 1399cb1a47aSBjoern A. Zeeb VNET_DEFINE_STATIC(struct ip6qbucket, ip6qb[IP6REASS_NHASH]); 1409cb1a47aSBjoern A. Zeeb VNET_DEFINE_STATIC(uint32_t, ip6qb_hashseed); 1419cb1a47aSBjoern A. Zeeb #define V_ip6qb VNET(ip6qb) 1429cb1a47aSBjoern A. Zeeb #define V_ip6qb_hashseed VNET(ip6qb_hashseed) 14382cd038dSYoshinobu Inoue 1449cb1a47aSBjoern A. Zeeb #define IP6QB_LOCK(_b) mtx_lock(&V_ip6qb[(_b)].lock) 1459cb1a47aSBjoern A. Zeeb #define IP6QB_TRYLOCK(_b) mtx_trylock(&V_ip6qb[(_b)].lock) 1469cb1a47aSBjoern A. Zeeb #define IP6QB_LOCK_ASSERT(_b) mtx_assert(&V_ip6qb[(_b)].lock, MA_OWNED) 1479cb1a47aSBjoern A. Zeeb #define IP6QB_UNLOCK(_b) mtx_unlock(&V_ip6qb[(_b)].lock) 14821f08a07SBjoern A. Zeeb #define IP6QB_HEAD(_b) (&V_ip6qb[(_b)].packets) 1499888c401SHajimu UMEMOTO 15082cd038dSYoshinobu Inoue /* 1512ceeacbeSJonathan T. Looney * By default, limit the number of IP6 fragments across all reassembly 1522ceeacbeSJonathan T. Looney * queues to 1/32 of the total number of mbuf clusters. 1532ceeacbeSJonathan T. Looney * 1542ceeacbeSJonathan T. Looney * Limit the total number of reassembly queues per VNET to the 1552ceeacbeSJonathan T. Looney * IP6 fragment limit, but ensure the limit will not allow any bucket 1562ceeacbeSJonathan T. Looney * to grow above 100 items. (The bucket limit is 1572ceeacbeSJonathan T. Looney * IP_MAXFRAGPACKETS / (IPREASS_NHASH / 2), so the 50 is the correct 1582ceeacbeSJonathan T. Looney * multiplier to reach a 100-item limit.) 1592ceeacbeSJonathan T. Looney * The 100-item limit was chosen as brief testing seems to show that 1602ceeacbeSJonathan T. Looney * this produces "reasonable" performance on some subset of systems 1612ceeacbeSJonathan T. Looney * under DoS attack. 1622ceeacbeSJonathan T. Looney */ 1632ceeacbeSJonathan T. Looney #define IP6_MAXFRAGS (nmbclusters / 32) 1642ceeacbeSJonathan T. Looney #define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50)) 1652ceeacbeSJonathan T. Looney 166e32221a1SAlexander V. Chernikov /* Interval between periodic reassembly queue inspections */ 167e32221a1SAlexander V. Chernikov #define IP6_CALLOUT_INTERVAL_MS 500 168e32221a1SAlexander V. Chernikov 1692ceeacbeSJonathan T. Looney /* 170757cb678SBjoern A. Zeeb * Sysctls and helper function. 17182cd038dSYoshinobu Inoue */ 172757cb678SBjoern A. Zeeb SYSCTL_DECL(_net_inet6_ip6); 173757cb678SBjoern A. Zeeb 17465456706SBjoern A. Zeeb SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfrags, 175c17ae180SMateusz Guzik CTLFLAG_RD, &frag6_nfrags, 0, 17665456706SBjoern A. Zeeb "Global number of IPv6 fragments across all reassembly queues."); 17765456706SBjoern A. Zeeb 178757cb678SBjoern A. Zeeb static void 17909b361c7SBjoern A. Zeeb frag6_set_bucketsize(void) 1801e9f3b73SJonathan T. Looney { 1811e9f3b73SJonathan T. Looney int i; 1821e9f3b73SJonathan T. Looney 1831e9f3b73SJonathan T. Looney if ((i = V_ip6_maxfragpackets) > 0) 1841e9f3b73SJonathan T. Looney V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1); 1851e9f3b73SJonathan T. Looney } 1861e9f3b73SJonathan T. Looney 187757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags, 188757cb678SBjoern A. Zeeb CTLFLAG_RW, &ip6_maxfrags, 0, 189757cb678SBjoern A. Zeeb "Maximum allowed number of outstanding IPv6 packet fragments. " 1903cf59750SGordon Bergling "A value of 0 means no fragmented packets will be accepted, while " 191757cb678SBjoern A. Zeeb "a value of -1 means no limit"); 192757cb678SBjoern A. Zeeb 193757cb678SBjoern A. Zeeb static int 194757cb678SBjoern A. Zeeb sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS) 195757cb678SBjoern A. Zeeb { 196757cb678SBjoern A. Zeeb int error, val; 197757cb678SBjoern A. Zeeb 198757cb678SBjoern A. Zeeb val = V_ip6_maxfragpackets; 199757cb678SBjoern A. Zeeb error = sysctl_handle_int(oidp, &val, 0, req); 200757cb678SBjoern A. Zeeb if (error != 0 || !req->newptr) 201757cb678SBjoern A. Zeeb return (error); 202757cb678SBjoern A. Zeeb V_ip6_maxfragpackets = val; 203757cb678SBjoern A. Zeeb frag6_set_bucketsize(); 204757cb678SBjoern A. Zeeb return (0); 205757cb678SBjoern A. Zeeb } 206757cb678SBjoern A. Zeeb SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets, 2077029da5cSPawel Biernacki CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 2087029da5cSPawel Biernacki NULL, 0, sysctl_ip6_maxfragpackets, "I", 209757cb678SBjoern A. Zeeb "Default maximum number of outstanding fragmented IPv6 packets. " 210757cb678SBjoern A. Zeeb "A value of 0 means no fragmented packets will be accepted, while a " 211757cb678SBjoern A. Zeeb "a value of -1 means no limit"); 21253707abdSBjoern A. Zeeb SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfragpackets, 21353707abdSBjoern A. Zeeb CTLFLAG_VNET | CTLFLAG_RD, 21453707abdSBjoern A. Zeeb __DEVOLATILE(u_int *, &VNET_NAME(frag6_nfragpackets)), 0, 21553707abdSBjoern A. Zeeb "Per-VNET number of IPv6 fragments across all reassembly queues."); 216757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket, 217757cb678SBjoern A. Zeeb CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0, 218757cb678SBjoern A. Zeeb "Maximum allowed number of fragments per packet"); 219757cb678SBjoern A. Zeeb SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize, 220757cb678SBjoern A. Zeeb CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0, 221757cb678SBjoern A. Zeeb "Maximum number of reassembly queues per hash bucket"); 222757cb678SBjoern A. Zeeb 223e32221a1SAlexander V. Chernikov static int 224e32221a1SAlexander V. Chernikov frag6_milli_to_callout_ticks(int ms) 225e32221a1SAlexander V. Chernikov { 226e32221a1SAlexander V. Chernikov return (ms / IP6_CALLOUT_INTERVAL_MS); 227e32221a1SAlexander V. Chernikov } 228e32221a1SAlexander V. Chernikov 229e32221a1SAlexander V. Chernikov static int 230e32221a1SAlexander V. Chernikov frag6_callout_ticks_to_milli(int ms) 231e32221a1SAlexander V. Chernikov { 232e32221a1SAlexander V. Chernikov return (ms * IP6_CALLOUT_INTERVAL_MS); 233e32221a1SAlexander V. Chernikov } 234e32221a1SAlexander V. Chernikov 235e32221a1SAlexander V. Chernikov _Static_assert(sizeof(((struct ip6q *)NULL)->ip6q_ttl) >= 2, 236e32221a1SAlexander V. Chernikov "ip6q_ttl field is not large enough"); 237e32221a1SAlexander V. Chernikov 238e32221a1SAlexander V. Chernikov static int 239e32221a1SAlexander V. Chernikov sysctl_ip6_fraglifetime(SYSCTL_HANDLER_ARGS) 240e32221a1SAlexander V. Chernikov { 241e32221a1SAlexander V. Chernikov int error, val; 242e32221a1SAlexander V. Chernikov 243e32221a1SAlexander V. Chernikov val = V_ip6_fraglifetime; 244e32221a1SAlexander V. Chernikov error = sysctl_handle_int(oidp, &val, 0, req); 245e32221a1SAlexander V. Chernikov if (error != 0 || !req->newptr) 246e32221a1SAlexander V. Chernikov return (error); 247e32221a1SAlexander V. Chernikov if (val <= 0) 248e32221a1SAlexander V. Chernikov val = IPV6_DEFFRAGTTL; 249e32221a1SAlexander V. Chernikov 250e32221a1SAlexander V. Chernikov if (frag6_milli_to_callout_ticks(val) >= 65536) 251e32221a1SAlexander V. Chernikov val = frag6_callout_ticks_to_milli(65535); 252e32221a1SAlexander V. Chernikov #ifdef VIMAGE 253e32221a1SAlexander V. Chernikov if (!IS_DEFAULT_VNET(curvnet)) { 254e32221a1SAlexander V. Chernikov CURVNET_SET(vnet0); 255e32221a1SAlexander V. Chernikov int host_val = V_ip6_fraglifetime; 256e32221a1SAlexander V. Chernikov CURVNET_RESTORE(); 257e32221a1SAlexander V. Chernikov 258e32221a1SAlexander V. Chernikov if (val > host_val) 259e32221a1SAlexander V. Chernikov val = host_val; 260e32221a1SAlexander V. Chernikov } 261e32221a1SAlexander V. Chernikov #endif 262e32221a1SAlexander V. Chernikov V_ip6_fraglifetime = val; 263e32221a1SAlexander V. Chernikov return (0); 264e32221a1SAlexander V. Chernikov } 265e32221a1SAlexander V. Chernikov SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, fraglifetime_ms, 266e32221a1SAlexander V. Chernikov CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, 267e32221a1SAlexander V. Chernikov NULL, 0, sysctl_ip6_fraglifetime, "I", 268e32221a1SAlexander V. Chernikov "Fragment lifetime, in milliseconds"); 269e32221a1SAlexander V. Chernikov 270757cb678SBjoern A. Zeeb /* 271c00464a2SBjoern A. Zeeb * Remove the IPv6 fragmentation header from the mbuf. 272c00464a2SBjoern A. Zeeb */ 273c00464a2SBjoern A. Zeeb int 274a61b5cfbSBjoern A. Zeeb ip6_deletefraghdr(struct mbuf *m, int offset, int wait __unused) 275c00464a2SBjoern A. Zeeb { 2765778b399SBjoern A. Zeeb struct ip6_hdr *ip6; 277a61b5cfbSBjoern A. Zeeb 278a61b5cfbSBjoern A. Zeeb KASSERT(m->m_len >= offset + sizeof(struct ip6_frag), 279a61b5cfbSBjoern A. Zeeb ("%s: ext headers not contigous in mbuf %p m_len %d >= " 280a61b5cfbSBjoern A. Zeeb "offset %d + %zu\n", __func__, m, m->m_len, offset, 281a61b5cfbSBjoern A. Zeeb sizeof(struct ip6_frag))); 282c00464a2SBjoern A. Zeeb 283c00464a2SBjoern A. Zeeb /* Delete frag6 header. */ 2845778b399SBjoern A. Zeeb ip6 = mtod(m, struct ip6_hdr *); 285a61b5cfbSBjoern A. Zeeb bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag), offset); 286c00464a2SBjoern A. Zeeb m->m_data += sizeof(struct ip6_frag); 287c00464a2SBjoern A. Zeeb m->m_len -= sizeof(struct ip6_frag); 288c00464a2SBjoern A. Zeeb m->m_flags |= M_FRAGMENTED; 289a61b5cfbSBjoern A. Zeeb 290c00464a2SBjoern A. Zeeb return (0); 291c00464a2SBjoern A. Zeeb } 292c00464a2SBjoern A. Zeeb 293c00464a2SBjoern A. Zeeb /* 29423d374aaSBjoern A. Zeeb * Free a fragment reassembly header and all associated datagrams. 295757cb678SBjoern A. Zeeb */ 2964f590175SPaul Saab static void 297c00464a2SBjoern A. Zeeb frag6_freef(struct ip6q *q6, uint32_t bucket) 2984f590175SPaul Saab { 2995778b399SBjoern A. Zeeb struct ip6_hdr *ip6; 30021f08a07SBjoern A. Zeeb struct ip6asfrag *af6; 3015778b399SBjoern A. Zeeb struct mbuf *m; 3024f590175SPaul Saab 3039cb1a47aSBjoern A. Zeeb IP6QB_LOCK_ASSERT(bucket); 304c00464a2SBjoern A. Zeeb 30521f08a07SBjoern A. Zeeb while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) { 306da89a0feSBjoern A. Zeeb m = af6->ip6af_m; 30721f08a07SBjoern A. Zeeb TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq); 308c00464a2SBjoern A. Zeeb 309c00464a2SBjoern A. Zeeb /* 310c00464a2SBjoern A. Zeeb * Return ICMP time exceeded error for the 1st fragment. 311c00464a2SBjoern A. Zeeb * Just free other fragments. 312c00464a2SBjoern A. Zeeb */ 313a55383e7SHans Petter Selasky if (af6->ip6af_off == 0 && m->m_pkthdr.rcvif != NULL) { 31423d374aaSBjoern A. Zeeb /* Adjust pointer. */ 315c00464a2SBjoern A. Zeeb ip6 = mtod(m, struct ip6_hdr *); 316c00464a2SBjoern A. Zeeb 31723d374aaSBjoern A. Zeeb /* Restore source and destination addresses. */ 318c00464a2SBjoern A. Zeeb ip6->ip6_src = q6->ip6q_src; 319c00464a2SBjoern A. Zeeb ip6->ip6_dst = q6->ip6q_dst; 320c00464a2SBjoern A. Zeeb 321c00464a2SBjoern A. Zeeb icmp6_error(m, ICMP6_TIME_EXCEEDED, 322c00464a2SBjoern A. Zeeb ICMP6_TIME_EXCEED_REASSEMBLY, 0); 323c00464a2SBjoern A. Zeeb } else 324c00464a2SBjoern A. Zeeb m_freem(m); 32523d374aaSBjoern A. Zeeb 326c00464a2SBjoern A. Zeeb free(af6, M_FRAG6); 3272adfd64fSJonathan T. Looney } 32821f08a07SBjoern A. Zeeb 32921f08a07SBjoern A. Zeeb TAILQ_REMOVE(IP6QB_HEAD(bucket), q6, ip6q_tq); 33021f08a07SBjoern A. Zeeb V_ip6qb[bucket].count--; 331c00464a2SBjoern A. Zeeb atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag); 332c00464a2SBjoern A. Zeeb #ifdef MAC 333c00464a2SBjoern A. Zeeb mac_ip6q_destroy(q6); 334c00464a2SBjoern A. Zeeb #endif 335c00464a2SBjoern A. Zeeb free(q6, M_FRAG6); 336c00464a2SBjoern A. Zeeb atomic_subtract_int(&V_frag6_nfragpackets, 1); 33782cd038dSYoshinobu Inoue } 33882cd038dSYoshinobu Inoue 33982cd038dSYoshinobu Inoue /* 340a55383e7SHans Petter Selasky * Drain off all datagram fragments belonging to 341a55383e7SHans Petter Selasky * the given network interface. 342a55383e7SHans Petter Selasky */ 343a55383e7SHans Petter Selasky static void 344a55383e7SHans Petter Selasky frag6_cleanup(void *arg __unused, struct ifnet *ifp) 345a55383e7SHans Petter Selasky { 34621f08a07SBjoern A. Zeeb struct ip6qhead *head; 34721f08a07SBjoern A. Zeeb struct ip6q *q6; 348a55383e7SHans Petter Selasky struct ip6asfrag *af6; 34921f08a07SBjoern A. Zeeb uint32_t bucket; 350a55383e7SHans Petter Selasky 351a55383e7SHans Petter Selasky KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__)); 352a55383e7SHans Petter Selasky 3536e6b5143SBjoern A. Zeeb CURVNET_SET_QUIET(ifp->if_vnet); 35467a10c46SBjoern A. Zeeb #ifdef VIMAGE 35567a10c46SBjoern A. Zeeb /* 35667a10c46SBjoern A. Zeeb * Skip processing if IPv6 reassembly is not initialised or 35767a10c46SBjoern A. Zeeb * torn down by frag6_destroy(). 35867a10c46SBjoern A. Zeeb */ 3596e6b5143SBjoern A. Zeeb if (!V_frag6_on) { 3606e6b5143SBjoern A. Zeeb CURVNET_RESTORE(); 36167a10c46SBjoern A. Zeeb return; 3626e6b5143SBjoern A. Zeeb } 36367a10c46SBjoern A. Zeeb #endif 36467a10c46SBjoern A. Zeeb 36521f08a07SBjoern A. Zeeb for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 36621f08a07SBjoern A. Zeeb IP6QB_LOCK(bucket); 36721f08a07SBjoern A. Zeeb head = IP6QB_HEAD(bucket); 368a55383e7SHans Petter Selasky /* Scan fragment list. */ 36921f08a07SBjoern A. Zeeb TAILQ_FOREACH(q6, head, ip6q_tq) { 37021f08a07SBjoern A. Zeeb TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) { 371f1664f32SBjoern A. Zeeb /* Clear no longer valid rcvif pointer. */ 372da89a0feSBjoern A. Zeeb if (af6->ip6af_m->m_pkthdr.rcvif == ifp) 373da89a0feSBjoern A. Zeeb af6->ip6af_m->m_pkthdr.rcvif = NULL; 374a55383e7SHans Petter Selasky } 375a55383e7SHans Petter Selasky } 37621f08a07SBjoern A. Zeeb IP6QB_UNLOCK(bucket); 377a55383e7SHans Petter Selasky } 378a55383e7SHans Petter Selasky CURVNET_RESTORE(); 379a55383e7SHans Petter Selasky } 380a55383e7SHans Petter Selasky EVENTHANDLER_DEFINE(ifnet_departure_event, frag6_cleanup, NULL, 0); 381a55383e7SHans Petter Selasky 382a55383e7SHans Petter Selasky /* 38323d374aaSBjoern A. Zeeb * Like in RFC2460, in RFC8200, fragment and reassembly rules do not agree with 38423d374aaSBjoern A. Zeeb * each other, in terms of next header field handling in fragment header. 385686cdd19SJun-ichiro itojun Hagino * While the sender will use the same value for all of the fragmented packets, 38623d374aaSBjoern A. Zeeb * receiver is suggested not to check for consistency. 387686cdd19SJun-ichiro itojun Hagino * 38823d374aaSBjoern A. Zeeb * Fragment rules (p18,p19): 389686cdd19SJun-ichiro itojun Hagino * (2) A Fragment header containing: 39023d374aaSBjoern A. Zeeb * The Next Header value that identifies the first header 39123d374aaSBjoern A. Zeeb * after the Per-Fragment headers of the original packet. 392686cdd19SJun-ichiro itojun Hagino * -> next header field is same for all fragments 393686cdd19SJun-ichiro itojun Hagino * 39423d374aaSBjoern A. Zeeb * Reassembly rule (p20): 39523d374aaSBjoern A. Zeeb * The Next Header field of the last header of the Per-Fragment 39623d374aaSBjoern A. Zeeb * headers is obtained from the Next Header field of the first 397686cdd19SJun-ichiro itojun Hagino * fragment's Fragment header. 398686cdd19SJun-ichiro itojun Hagino * -> should grab it from the first fragment only 399686cdd19SJun-ichiro itojun Hagino * 400686cdd19SJun-ichiro itojun Hagino * The following note also contradicts with fragment rule - no one is going to 401686cdd19SJun-ichiro itojun Hagino * send different fragment with different next header field. 402686cdd19SJun-ichiro itojun Hagino * 40323d374aaSBjoern A. Zeeb * Additional note (p22) [not an error]: 404686cdd19SJun-ichiro itojun Hagino * The Next Header values in the Fragment headers of different 405686cdd19SJun-ichiro itojun Hagino * fragments of the same original packet may differ. Only the value 406686cdd19SJun-ichiro itojun Hagino * from the Offset zero fragment packet is used for reassembly. 407686cdd19SJun-ichiro itojun Hagino * -> should grab it from the first fragment only 408686cdd19SJun-ichiro itojun Hagino * 409686cdd19SJun-ichiro itojun Hagino * There is no explicit reason given in the RFC. Historical reason maybe? 410686cdd19SJun-ichiro itojun Hagino */ 411686cdd19SJun-ichiro itojun Hagino /* 41223d374aaSBjoern A. Zeeb * Fragment input. 41382cd038dSYoshinobu Inoue */ 41482cd038dSYoshinobu Inoue int 4151272577eSXin LI frag6_input(struct mbuf **mp, int *offp, int proto) 41682cd038dSYoshinobu Inoue { 41721f08a07SBjoern A. Zeeb struct mbuf *m, *t; 41882cd038dSYoshinobu Inoue struct ip6_hdr *ip6; 41982cd038dSYoshinobu Inoue struct ip6_frag *ip6f; 42021f08a07SBjoern A. Zeeb struct ip6qhead *head; 42121f08a07SBjoern A. Zeeb struct ip6q *q6; 42221f08a07SBjoern A. Zeeb struct ip6asfrag *af6, *ip6af, *af6tmp; 42321f08a07SBjoern A. Zeeb struct in6_ifaddr *ia6; 42421f08a07SBjoern A. Zeeb struct ifnet *dstifp, *srcifp; 425505e91f5SKristof Provost uint32_t hashkey[(sizeof(struct in6_addr) * 2 + 426505e91f5SKristof Provost sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)]; 4279cb1a47aSBjoern A. Zeeb uint32_t bucket, *hashkeyp; 4285778b399SBjoern A. Zeeb int fragoff, frgpartlen; /* Must be larger than uint16_t. */ 4295778b399SBjoern A. Zeeb int nxt, offset, plen; 4305778b399SBjoern A. Zeeb uint8_t ecn, ecn0; 4315778b399SBjoern A. Zeeb bool only_frag; 432aaa46574SAdrian Chadd #ifdef RSS 433aaa46574SAdrian Chadd struct ip6_direct_ctx *ip6dc; 4345778b399SBjoern A. Zeeb struct m_tag *mtag; 435aaa46574SAdrian Chadd #endif 436aaa46574SAdrian Chadd 4375778b399SBjoern A. Zeeb m = *mp; 4385778b399SBjoern A. Zeeb offset = *offp; 4395778b399SBjoern A. Zeeb 440c1131de6SBjoern A. Zeeb M_ASSERTPKTHDR(m); 441c1131de6SBjoern A. Zeeb 442a4adf6ccSBjoern A. Zeeb if (m->m_len < offset + sizeof(struct ip6_frag)) { 443a61b5cfbSBjoern A. Zeeb m = m_pullup(m, offset + sizeof(struct ip6_frag)); 444a61b5cfbSBjoern A. Zeeb if (m == NULL) { 445a61b5cfbSBjoern A. Zeeb IP6STAT_INC(ip6s_exthdrtoolong); 446a61b5cfbSBjoern A. Zeeb *mp = NULL; 44740e39bbbSHajimu UMEMOTO return (IPPROTO_DONE); 448a61b5cfbSBjoern A. Zeeb } 449a4adf6ccSBjoern A. Zeeb } 450a61b5cfbSBjoern A. Zeeb ip6 = mtod(m, struct ip6_hdr *); 45182cd038dSYoshinobu Inoue 45282cd038dSYoshinobu Inoue dstifp = NULL; 45323d374aaSBjoern A. Zeeb /* Find the destination interface of the packet. */ 4548268d82cSAlexander V. Chernikov ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */, false); 4558268d82cSAlexander V. Chernikov if (ia6 != NULL) 4565778b399SBjoern A. Zeeb dstifp = ia6->ia_ifp; 45723d374aaSBjoern A. Zeeb 45823d374aaSBjoern A. Zeeb /* Jumbo payload cannot contain a fragment header. */ 45982cd038dSYoshinobu Inoue if (ip6->ip6_plen == 0) { 46082cd038dSYoshinobu Inoue icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset); 46182cd038dSYoshinobu Inoue in6_ifstat_inc(dstifp, ifs6_reass_fail); 462a8fe77d8SBjoern A. Zeeb *mp = NULL; 4635778b399SBjoern A. Zeeb return (IPPROTO_DONE); 46482cd038dSYoshinobu Inoue } 46582cd038dSYoshinobu Inoue 46682cd038dSYoshinobu Inoue /* 46723d374aaSBjoern A. Zeeb * Check whether fragment packet's fragment length is a 46823d374aaSBjoern A. Zeeb * multiple of 8 octets (unless it is the last one). 46982cd038dSYoshinobu Inoue * sizeof(struct ip6_frag) == 8 47082cd038dSYoshinobu Inoue * sizeof(struct ip6_hdr) = 40 47182cd038dSYoshinobu Inoue */ 472a61b5cfbSBjoern A. Zeeb ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset); 47382cd038dSYoshinobu Inoue if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && 47482cd038dSYoshinobu Inoue (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) { 47506cd0a3fSHajimu UMEMOTO icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 476686cdd19SJun-ichiro itojun Hagino offsetof(struct ip6_hdr, ip6_plen)); 47782cd038dSYoshinobu Inoue in6_ifstat_inc(dstifp, ifs6_reass_fail); 478a8fe77d8SBjoern A. Zeeb *mp = NULL; 4795778b399SBjoern A. Zeeb return (IPPROTO_DONE); 48082cd038dSYoshinobu Inoue } 48182cd038dSYoshinobu Inoue 4829cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_fragments); 48382cd038dSYoshinobu Inoue in6_ifstat_inc(dstifp, ifs6_reass_reqd); 48482cd038dSYoshinobu Inoue 4854018ea9aSBjoern A. Zeeb /* 4862946a941STom Jones * Handle "atomic" fragments (offset and m bit set to 0) upfront, 487c1131de6SBjoern A. Zeeb * unrelated to any reassembly. We need to remove the frag hdr 488c1131de6SBjoern A. Zeeb * which is ugly. 48923d374aaSBjoern A. Zeeb * See RFC 6946 and section 4.5 of RFC 8200. 4904018ea9aSBjoern A. Zeeb */ 4914018ea9aSBjoern A. Zeeb if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) { 4922946a941STom Jones IP6STAT_INC(ip6s_atomicfrags); 493c1131de6SBjoern A. Zeeb nxt = ip6f->ip6f_nxt; 494c1131de6SBjoern A. Zeeb /* 495c1131de6SBjoern A. Zeeb * Set nxt(-hdr field value) to the original value. 496c1131de6SBjoern A. Zeeb * We cannot just set ip6->ip6_nxt as there might be 497c1131de6SBjoern A. Zeeb * an unfragmentable part with extension headers and 498c1131de6SBjoern A. Zeeb * we must update the last one. 499c1131de6SBjoern A. Zeeb */ 500c1131de6SBjoern A. Zeeb m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t), 501c1131de6SBjoern A. Zeeb (caddr_t)&nxt); 502c1131de6SBjoern A. Zeeb ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - 503c1131de6SBjoern A. Zeeb sizeof(struct ip6_frag)); 504c1131de6SBjoern A. Zeeb if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) 505c1131de6SBjoern A. Zeeb goto dropfrag2; 506c1131de6SBjoern A. Zeeb m->m_pkthdr.len -= sizeof(struct ip6_frag); 5074018ea9aSBjoern A. Zeeb in6_ifstat_inc(dstifp, ifs6_reass_ok); 508c1131de6SBjoern A. Zeeb *mp = m; 509c1131de6SBjoern A. Zeeb return (nxt); 5104018ea9aSBjoern A. Zeeb } 5114018ea9aSBjoern A. Zeeb 512c1131de6SBjoern A. Zeeb /* Offset now points to data portion. */ 513c1131de6SBjoern A. Zeeb offset += sizeof(struct ip6_frag); 514c1131de6SBjoern A. Zeeb 5155f9f192dSJonathan T. Looney /* Get fragment length and discard 0-byte fragments. */ 5165f9f192dSJonathan T. Looney frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset; 5175f9f192dSJonathan T. Looney if (frgpartlen == 0) { 5185f9f192dSJonathan T. Looney icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 5195f9f192dSJonathan T. Looney offsetof(struct ip6_hdr, ip6_plen)); 5205f9f192dSJonathan T. Looney in6_ifstat_inc(dstifp, ifs6_reass_fail); 5215f9f192dSJonathan T. Looney IP6STAT_INC(ip6s_fragdropped); 522a8fe77d8SBjoern A. Zeeb *mp = NULL; 5235778b399SBjoern A. Zeeb return (IPPROTO_DONE); 5245f9f192dSJonathan T. Looney } 5255f9f192dSJonathan T. Looney 5267715d794SBjoern A. Zeeb /* 5277715d794SBjoern A. Zeeb * Enforce upper bound on number of fragments for the entire system. 5287715d794SBjoern A. Zeeb * If maxfrag is 0, never accept fragments. 5297715d794SBjoern A. Zeeb * If maxfrag is -1, accept all fragments without limitation. 5307715d794SBjoern A. Zeeb */ 5317715d794SBjoern A. Zeeb if (ip6_maxfrags < 0) 5327715d794SBjoern A. Zeeb ; 5337715d794SBjoern A. Zeeb else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags) 5347715d794SBjoern A. Zeeb goto dropfrag2; 5357715d794SBjoern A. Zeeb 53630809ba9SBjoern A. Zeeb /* 53730809ba9SBjoern A. Zeeb * Validate that a full header chain to the ULP is present in the 53830809ba9SBjoern A. Zeeb * packet containing the first fragment as per RFC RFC7112 and 53930809ba9SBjoern A. Zeeb * RFC 8200 pages 18,19: 54030809ba9SBjoern A. Zeeb * The first fragment packet is composed of: 54130809ba9SBjoern A. Zeeb * (3) Extension headers, if any, and the Upper-Layer header. These 54230809ba9SBjoern A. Zeeb * headers must be in the first fragment. ... 54330809ba9SBjoern A. Zeeb */ 54430809ba9SBjoern A. Zeeb fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK); 54530809ba9SBjoern A. Zeeb /* XXX TODO. thj has D16851 open for this. */ 54630809ba9SBjoern A. Zeeb /* Send ICMPv6 4,3 in case of violation. */ 54730809ba9SBjoern A. Zeeb 548efdfee93SBjoern A. Zeeb /* Store receive network interface pointer for later. */ 549efdfee93SBjoern A. Zeeb srcifp = m->m_pkthdr.rcvif; 550efdfee93SBjoern A. Zeeb 55123d374aaSBjoern A. Zeeb /* Generate a hash value for fragment bucket selection. */ 55280d7a853SJonathan T. Looney hashkeyp = hashkey; 55380d7a853SJonathan T. Looney memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr)); 55480d7a853SJonathan T. Looney hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp); 55580d7a853SJonathan T. Looney memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr)); 55680d7a853SJonathan T. Looney hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp); 55780d7a853SJonathan T. Looney *hashkeyp = ip6f->ip6f_ident; 5589cb1a47aSBjoern A. Zeeb bucket = jenkins_hash32(hashkey, nitems(hashkey), V_ip6qb_hashseed); 5599cb1a47aSBjoern A. Zeeb bucket &= IP6REASS_HMASK; 5609cb1a47aSBjoern A. Zeeb IP6QB_LOCK(bucket); 56121f08a07SBjoern A. Zeeb head = IP6QB_HEAD(bucket); 5629888c401SHajimu UMEMOTO 56321f08a07SBjoern A. Zeeb TAILQ_FOREACH(q6, head, ip6q_tq) 56482cd038dSYoshinobu Inoue if (ip6f->ip6f_ident == q6->ip6q_ident && 56582cd038dSYoshinobu Inoue IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) && 5664b908c8bSRobert Watson IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst) 5674b908c8bSRobert Watson #ifdef MAC 5684b908c8bSRobert Watson && mac_ip6q_match(m, q6) 5694b908c8bSRobert Watson #endif 5704b908c8bSRobert Watson ) 57182cd038dSYoshinobu Inoue break; 57282cd038dSYoshinobu Inoue 5735778b399SBjoern A. Zeeb only_frag = false; 57421f08a07SBjoern A. Zeeb if (q6 == NULL) { 5755778b399SBjoern A. Zeeb /* A first fragment to arrive creates a reassembly queue. */ 5765778b399SBjoern A. Zeeb only_frag = true; 57782cd038dSYoshinobu Inoue 57882cd038dSYoshinobu Inoue /* 57982cd038dSYoshinobu Inoue * Enforce upper bound on number of fragmented packets 58082cd038dSYoshinobu Inoue * for which we attempt reassembly; 5819888c401SHajimu UMEMOTO * If maxfragpackets is 0, never accept fragments. 5829888c401SHajimu UMEMOTO * If maxfragpackets is -1, accept all fragments without 5839888c401SHajimu UMEMOTO * limitation. 58482cd038dSYoshinobu Inoue */ 585603724d3SBjoern A. Zeeb if (V_ip6_maxfragpackets < 0) 58633841545SHajimu UMEMOTO ; 5879cb1a47aSBjoern A. Zeeb else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize || 5881e9f3b73SJonathan T. Looney atomic_load_int(&V_frag6_nfragpackets) >= 58980d7a853SJonathan T. Looney (u_int)V_ip6_maxfragpackets) 59033841545SHajimu UMEMOTO goto dropfrag; 59123d374aaSBjoern A. Zeeb 59223d374aaSBjoern A. Zeeb /* Allocate IPv6 fragement packet queue entry. */ 593487a161cSBjoern A. Zeeb q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FRAG6, 594487a161cSBjoern A. Zeeb M_NOWAIT | M_ZERO); 59582cd038dSYoshinobu Inoue if (q6 == NULL) 59682cd038dSYoshinobu Inoue goto dropfrag; 5974b908c8bSRobert Watson #ifdef MAC 5984b908c8bSRobert Watson if (mac_ip6q_init(q6, M_NOWAIT) != 0) { 599487a161cSBjoern A. Zeeb free(q6, M_FRAG6); 6004b908c8bSRobert Watson goto dropfrag; 6014b908c8bSRobert Watson } 6024b908c8bSRobert Watson mac_ip6q_create(m, q6); 6034b908c8bSRobert Watson #endif 604702828f6SBjoern A. Zeeb atomic_add_int(&V_frag6_nfragpackets, 1); 60582cd038dSYoshinobu Inoue 60623d374aaSBjoern A. Zeeb /* ip6q_nxt will be filled afterwards, from 1st fragment. */ 60721f08a07SBjoern A. Zeeb TAILQ_INIT(&q6->ip6q_frags); 60882cd038dSYoshinobu Inoue q6->ip6q_ident = ip6f->ip6f_ident; 609e32221a1SAlexander V. Chernikov q6->ip6q_ttl = frag6_milli_to_callout_ticks(V_ip6_fraglifetime); 61082cd038dSYoshinobu Inoue q6->ip6q_src = ip6->ip6_src; 61182cd038dSYoshinobu Inoue q6->ip6q_dst = ip6->ip6_dst; 612bb4a7d94SKristof Provost q6->ip6q_ecn = IPV6_ECN(ip6); 61382cd038dSYoshinobu Inoue q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */ 6149888c401SHajimu UMEMOTO 61521f08a07SBjoern A. Zeeb /* Add the fragemented packet to the bucket. */ 61621f08a07SBjoern A. Zeeb TAILQ_INSERT_HEAD(head, q6, ip6q_tq); 61721f08a07SBjoern A. Zeeb V_ip6qb[bucket].count++; 61882cd038dSYoshinobu Inoue } 61982cd038dSYoshinobu Inoue 62082cd038dSYoshinobu Inoue /* 62123d374aaSBjoern A. Zeeb * If it is the 1st fragment, record the length of the 62282cd038dSYoshinobu Inoue * unfragmentable part and the next header of the fragment header. 623619456bbSBjoern A. Zeeb * Assume the first 1st fragement to arrive will be correct. 624619456bbSBjoern A. Zeeb * We do not have any duplicate checks here yet so another packet 625619456bbSBjoern A. Zeeb * with fragoff == 0 could come and overwrite the ip6q_unfrglen 626619456bbSBjoern A. Zeeb * and worse, the next header, at any time. 62782cd038dSYoshinobu Inoue */ 628619456bbSBjoern A. Zeeb if (fragoff == 0 && q6->ip6q_unfrglen == -1) { 62906cd0a3fSHajimu UMEMOTO q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) - 63006cd0a3fSHajimu UMEMOTO sizeof(struct ip6_frag); 63182cd038dSYoshinobu Inoue q6->ip6q_nxt = ip6f->ip6f_nxt; 632619456bbSBjoern A. Zeeb /* XXX ECN? */ 63382cd038dSYoshinobu Inoue } 63482cd038dSYoshinobu Inoue 63582cd038dSYoshinobu Inoue /* 63682cd038dSYoshinobu Inoue * Check that the reassembled packet would not exceed 65535 bytes 63782cd038dSYoshinobu Inoue * in size. 63882cd038dSYoshinobu Inoue * If it would exceed, discard the fragment and return an ICMP error. 63982cd038dSYoshinobu Inoue */ 64082cd038dSYoshinobu Inoue if (q6->ip6q_unfrglen >= 0) { 64182cd038dSYoshinobu Inoue /* The 1st fragment has already arrived. */ 64282cd038dSYoshinobu Inoue if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) { 643e5fffe9aSBjoern A. Zeeb if (only_frag) { 644e5fffe9aSBjoern A. Zeeb TAILQ_REMOVE(head, q6, ip6q_tq); 645e5fffe9aSBjoern A. Zeeb V_ip6qb[bucket].count--; 646e5fffe9aSBjoern A. Zeeb atomic_subtract_int(&V_frag6_nfragpackets, 1); 647e5fffe9aSBjoern A. Zeeb #ifdef MAC 648e5fffe9aSBjoern A. Zeeb mac_ip6q_destroy(q6); 649e5fffe9aSBjoern A. Zeeb #endif 650e5fffe9aSBjoern A. Zeeb free(q6, M_FRAG6); 651e5fffe9aSBjoern A. Zeeb } 652e5fffe9aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 65382cd038dSYoshinobu Inoue icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 654686cdd19SJun-ichiro itojun Hagino offset - sizeof(struct ip6_frag) + 655686cdd19SJun-ichiro itojun Hagino offsetof(struct ip6_frag, ip6f_offlg)); 656a8fe77d8SBjoern A. Zeeb *mp = NULL; 65782cd038dSYoshinobu Inoue return (IPPROTO_DONE); 65882cd038dSYoshinobu Inoue } 65906cd0a3fSHajimu UMEMOTO } else if (fragoff + frgpartlen > IPV6_MAXPACKET) { 660e5fffe9aSBjoern A. Zeeb if (only_frag) { 661e5fffe9aSBjoern A. Zeeb TAILQ_REMOVE(head, q6, ip6q_tq); 662e5fffe9aSBjoern A. Zeeb V_ip6qb[bucket].count--; 663e5fffe9aSBjoern A. Zeeb atomic_subtract_int(&V_frag6_nfragpackets, 1); 664e5fffe9aSBjoern A. Zeeb #ifdef MAC 665e5fffe9aSBjoern A. Zeeb mac_ip6q_destroy(q6); 666e5fffe9aSBjoern A. Zeeb #endif 667e5fffe9aSBjoern A. Zeeb free(q6, M_FRAG6); 668e5fffe9aSBjoern A. Zeeb } 669e5fffe9aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 67082cd038dSYoshinobu Inoue icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, 671686cdd19SJun-ichiro itojun Hagino offset - sizeof(struct ip6_frag) + 672686cdd19SJun-ichiro itojun Hagino offsetof(struct ip6_frag, ip6f_offlg)); 673a8fe77d8SBjoern A. Zeeb *mp = NULL; 67482cd038dSYoshinobu Inoue return (IPPROTO_DONE); 67582cd038dSYoshinobu Inoue } 676f1664f32SBjoern A. Zeeb 67782cd038dSYoshinobu Inoue /* 67823d374aaSBjoern A. Zeeb * If it is the first fragment, do the above check for each 67982cd038dSYoshinobu Inoue * fragment already stored in the reassembly queue. 68082cd038dSYoshinobu Inoue */ 681dda02192SBjoern A. Zeeb if (fragoff == 0 && !only_frag) { 68221f08a07SBjoern A. Zeeb TAILQ_FOREACH_SAFE(af6, &q6->ip6q_frags, ip6af_tq, af6tmp) { 683dda02192SBjoern A. Zeeb if (q6->ip6q_unfrglen + af6->ip6af_off + 684dda02192SBjoern A. Zeeb af6->ip6af_frglen > IPV6_MAXPACKET) { 68582cd038dSYoshinobu Inoue struct ip6_hdr *ip6err; 6865778b399SBjoern A. Zeeb struct mbuf *merr; 6875778b399SBjoern A. Zeeb int erroff; 6885778b399SBjoern A. Zeeb 689da89a0feSBjoern A. Zeeb merr = af6->ip6af_m; 6905778b399SBjoern A. Zeeb erroff = af6->ip6af_offset; 69182cd038dSYoshinobu Inoue 69223d374aaSBjoern A. Zeeb /* Dequeue the fragment. */ 69321f08a07SBjoern A. Zeeb TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq); 694dda02192SBjoern A. Zeeb q6->ip6q_nfrag--; 695dda02192SBjoern A. Zeeb atomic_subtract_int(&frag6_nfrags, 1); 696487a161cSBjoern A. Zeeb free(af6, M_FRAG6); 69782cd038dSYoshinobu Inoue 698a55383e7SHans Petter Selasky /* Set a valid receive interface pointer. */ 699a55383e7SHans Petter Selasky merr->m_pkthdr.rcvif = srcifp; 700a55383e7SHans Petter Selasky 70123d374aaSBjoern A. Zeeb /* Adjust pointer. */ 70282cd038dSYoshinobu Inoue ip6err = mtod(merr, struct ip6_hdr *); 70382cd038dSYoshinobu Inoue 70482cd038dSYoshinobu Inoue /* 70582cd038dSYoshinobu Inoue * Restore source and destination addresses 70682cd038dSYoshinobu Inoue * in the erroneous IPv6 header. 70782cd038dSYoshinobu Inoue */ 70882cd038dSYoshinobu Inoue ip6err->ip6_src = q6->ip6q_src; 70982cd038dSYoshinobu Inoue ip6err->ip6_dst = q6->ip6q_dst; 71082cd038dSYoshinobu Inoue 71182cd038dSYoshinobu Inoue icmp6_error(merr, ICMP6_PARAM_PROB, 71282cd038dSYoshinobu Inoue ICMP6_PARAMPROB_HEADER, 713686cdd19SJun-ichiro itojun Hagino erroff - sizeof(struct ip6_frag) + 714686cdd19SJun-ichiro itojun Hagino offsetof(struct ip6_frag, ip6f_offlg)); 71582cd038dSYoshinobu Inoue } 71682cd038dSYoshinobu Inoue } 71782cd038dSYoshinobu Inoue } 71882cd038dSYoshinobu Inoue 71923d374aaSBjoern A. Zeeb /* Allocate an IPv6 fragement queue entry for this fragmented part. */ 720487a161cSBjoern A. Zeeb ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FRAG6, 721487a161cSBjoern A. Zeeb M_NOWAIT | M_ZERO); 722686cdd19SJun-ichiro itojun Hagino if (ip6af == NULL) 723686cdd19SJun-ichiro itojun Hagino goto dropfrag; 72421f08a07SBjoern A. Zeeb ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) ? true : false; 72582cd038dSYoshinobu Inoue ip6af->ip6af_off = fragoff; 72682cd038dSYoshinobu Inoue ip6af->ip6af_frglen = frgpartlen; 72782cd038dSYoshinobu Inoue ip6af->ip6af_offset = offset; 728da89a0feSBjoern A. Zeeb ip6af->ip6af_m = m; 72982cd038dSYoshinobu Inoue 7305778b399SBjoern A. Zeeb if (only_frag) { 73121f08a07SBjoern A. Zeeb /* 73221f08a07SBjoern A. Zeeb * Do a manual insert rather than a hard-to-understand cast 73321f08a07SBjoern A. Zeeb * to a different type relying on data structure order to work. 73421f08a07SBjoern A. Zeeb */ 73521f08a07SBjoern A. Zeeb TAILQ_INSERT_HEAD(&q6->ip6q_frags, ip6af, ip6af_tq); 73621f08a07SBjoern A. Zeeb goto postinsert; 73782cd038dSYoshinobu Inoue } 73882cd038dSYoshinobu Inoue 73923d374aaSBjoern A. Zeeb /* Do duplicate, condition, and boundry checks. */ 74082cd038dSYoshinobu Inoue /* 74159dfcba4SHajimu UMEMOTO * Handle ECN by comparing this segment with the first one; 74259dfcba4SHajimu UMEMOTO * if CE is set, do not lose CE. 74323d374aaSBjoern A. Zeeb * Drop if CE and not-ECT are mixed for the same packet. 74459dfcba4SHajimu UMEMOTO */ 745bb4a7d94SKristof Provost ecn = IPV6_ECN(ip6); 7465e9510e3SJINMEI Tatuya ecn0 = q6->ip6q_ecn; 74759dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_CE) { 74859dfcba4SHajimu UMEMOTO if (ecn0 == IPTOS_ECN_NOTECT) { 749487a161cSBjoern A. Zeeb free(ip6af, M_FRAG6); 75059dfcba4SHajimu UMEMOTO goto dropfrag; 75159dfcba4SHajimu UMEMOTO } 75259dfcba4SHajimu UMEMOTO if (ecn0 != IPTOS_ECN_CE) 7535e9510e3SJINMEI Tatuya q6->ip6q_ecn = IPTOS_ECN_CE; 75459dfcba4SHajimu UMEMOTO } 75559dfcba4SHajimu UMEMOTO if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) { 756487a161cSBjoern A. Zeeb free(ip6af, M_FRAG6); 75759dfcba4SHajimu UMEMOTO goto dropfrag; 75859dfcba4SHajimu UMEMOTO } 75959dfcba4SHajimu UMEMOTO 76023d374aaSBjoern A. Zeeb /* Find a fragmented part which begins after this one does. */ 76121f08a07SBjoern A. Zeeb TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) 76282cd038dSYoshinobu Inoue if (af6->ip6af_off > ip6af->ip6af_off) 76382cd038dSYoshinobu Inoue break; 76482cd038dSYoshinobu Inoue 76582cd038dSYoshinobu Inoue /* 76682cd038dSYoshinobu Inoue * If the incoming framgent overlaps some existing fragments in 76723d374aaSBjoern A. Zeeb * the reassembly queue, drop both the new fragment and the 76823d374aaSBjoern A. Zeeb * entire reassembly queue. However, if the new fragment 76923d374aaSBjoern A. Zeeb * is an exact duplicate of an existing fragment, only silently 77023d374aaSBjoern A. Zeeb * drop the existing fragment and leave the fragmentation queue 77123d374aaSBjoern A. Zeeb * unchanged, as allowed by the RFC. (RFC 8200, 4.5) 77282cd038dSYoshinobu Inoue */ 77321f08a07SBjoern A. Zeeb if (af6 != NULL) 77421f08a07SBjoern A. Zeeb af6tmp = TAILQ_PREV(af6, ip6fraghead, ip6af_tq); 77521f08a07SBjoern A. Zeeb else 77621f08a07SBjoern A. Zeeb af6tmp = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead); 77721f08a07SBjoern A. Zeeb if (af6tmp != NULL) { 77821f08a07SBjoern A. Zeeb if (af6tmp->ip6af_off + af6tmp->ip6af_frglen - 7795778b399SBjoern A. Zeeb ip6af->ip6af_off > 0) { 780cd188da2SBjoern A. Zeeb if (af6tmp->ip6af_off != ip6af->ip6af_off || 781cd188da2SBjoern A. Zeeb af6tmp->ip6af_frglen != ip6af->ip6af_frglen) 782cd188da2SBjoern A. Zeeb frag6_freef(q6, bucket); 783487a161cSBjoern A. Zeeb free(ip6af, M_FRAG6); 78482cd038dSYoshinobu Inoue goto dropfrag; 78582cd038dSYoshinobu Inoue } 78682cd038dSYoshinobu Inoue } 78721f08a07SBjoern A. Zeeb if (af6 != NULL) { 7885778b399SBjoern A. Zeeb if (ip6af->ip6af_off + ip6af->ip6af_frglen - 7895778b399SBjoern A. Zeeb af6->ip6af_off > 0) { 790cd188da2SBjoern A. Zeeb if (af6->ip6af_off != ip6af->ip6af_off || 791cd188da2SBjoern A. Zeeb af6->ip6af_frglen != ip6af->ip6af_frglen) 792cd188da2SBjoern A. Zeeb frag6_freef(q6, bucket); 793487a161cSBjoern A. Zeeb free(ip6af, M_FRAG6); 79482cd038dSYoshinobu Inoue goto dropfrag; 79582cd038dSYoshinobu Inoue } 79682cd038dSYoshinobu Inoue } 79782cd038dSYoshinobu Inoue 7984b908c8bSRobert Watson #ifdef MAC 7994b908c8bSRobert Watson mac_ip6q_update(m, q6); 8004b908c8bSRobert Watson #endif 80182cd038dSYoshinobu Inoue 80282cd038dSYoshinobu Inoue /* 80323d374aaSBjoern A. Zeeb * Stick new segment in its place; check for complete reassembly. 80423d374aaSBjoern A. Zeeb * If not complete, check fragment limit. Move to front of packet 80523d374aaSBjoern A. Zeeb * queue, as we are the most recently active fragmented packet. 80682cd038dSYoshinobu Inoue */ 80721f08a07SBjoern A. Zeeb if (af6 != NULL) 80821f08a07SBjoern A. Zeeb TAILQ_INSERT_BEFORE(af6, ip6af, ip6af_tq); 80921f08a07SBjoern A. Zeeb else 81021f08a07SBjoern A. Zeeb TAILQ_INSERT_TAIL(&q6->ip6q_frags, ip6af, ip6af_tq); 81121f08a07SBjoern A. Zeeb postinsert: 8122adfd64fSJonathan T. Looney atomic_add_int(&frag6_nfrags, 1); 8139888c401SHajimu UMEMOTO q6->ip6q_nfrag++; 8143c7165b3SBjoern A. Zeeb 8155778b399SBjoern A. Zeeb plen = 0; 81621f08a07SBjoern A. Zeeb TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) { 8175778b399SBjoern A. Zeeb if (af6->ip6af_off != plen) { 81803c99d76SJonathan T. Looney if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) { 819198fdaedSTom Jones IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag); 8209cb1a47aSBjoern A. Zeeb frag6_freef(q6, bucket); 82103c99d76SJonathan T. Looney } 8229cb1a47aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 823a8fe77d8SBjoern A. Zeeb *mp = NULL; 8245778b399SBjoern A. Zeeb return (IPPROTO_DONE); 82582cd038dSYoshinobu Inoue } 8265778b399SBjoern A. Zeeb plen += af6->ip6af_frglen; 82782cd038dSYoshinobu Inoue } 82821f08a07SBjoern A. Zeeb af6 = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead); 82921f08a07SBjoern A. Zeeb if (af6->ip6af_mff) { 83003c99d76SJonathan T. Looney if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) { 831198fdaedSTom Jones IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag); 8329cb1a47aSBjoern A. Zeeb frag6_freef(q6, bucket); 83303c99d76SJonathan T. Looney } 8349cb1a47aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 835a8fe77d8SBjoern A. Zeeb *mp = NULL; 8365778b399SBjoern A. Zeeb return (IPPROTO_DONE); 83782cd038dSYoshinobu Inoue } 83882cd038dSYoshinobu Inoue 83923d374aaSBjoern A. Zeeb /* Reassembly is complete; concatenate fragments. */ 84021f08a07SBjoern A. Zeeb ip6af = TAILQ_FIRST(&q6->ip6q_frags); 841da89a0feSBjoern A. Zeeb t = m = ip6af->ip6af_m; 84221f08a07SBjoern A. Zeeb TAILQ_REMOVE(&q6->ip6q_frags, ip6af, ip6af_tq); 84321f08a07SBjoern A. Zeeb while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) { 8449907aba3SAndrey V. Elsukov m->m_pkthdr.csum_flags &= 845da89a0feSBjoern A. Zeeb af6->ip6af_m->m_pkthdr.csum_flags; 8469907aba3SAndrey V. Elsukov m->m_pkthdr.csum_data += 847da89a0feSBjoern A. Zeeb af6->ip6af_m->m_pkthdr.csum_data; 8489907aba3SAndrey V. Elsukov 84921f08a07SBjoern A. Zeeb TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq); 850efdfee93SBjoern A. Zeeb t = m_last(t); 851da89a0feSBjoern A. Zeeb m_adj(af6->ip6af_m, af6->ip6af_offset); 852da89a0feSBjoern A. Zeeb m_demote_pkthdr(af6->ip6af_m); 853da89a0feSBjoern A. Zeeb m_cat(t, af6->ip6af_m); 854487a161cSBjoern A. Zeeb free(af6, M_FRAG6); 85582cd038dSYoshinobu Inoue } 85682cd038dSYoshinobu Inoue 8579907aba3SAndrey V. Elsukov while (m->m_pkthdr.csum_data & 0xffff0000) 8589907aba3SAndrey V. Elsukov m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) + 8599907aba3SAndrey V. Elsukov (m->m_pkthdr.csum_data >> 16); 8609907aba3SAndrey V. Elsukov 86123d374aaSBjoern A. Zeeb /* Adjust offset to point where the original next header starts. */ 86282cd038dSYoshinobu Inoue offset = ip6af->ip6af_offset - sizeof(struct ip6_frag); 863487a161cSBjoern A. Zeeb free(ip6af, M_FRAG6); 864*ff3d1a3fSJonathan T. Looney if ((u_int)plen + (u_int)offset - sizeof(struct ip6_hdr) > 865*ff3d1a3fSJonathan T. Looney IPV6_MAXPACKET) { 866*ff3d1a3fSJonathan T. Looney frag6_freef(q6, bucket); 867*ff3d1a3fSJonathan T. Looney goto dropfrag; 868*ff3d1a3fSJonathan T. Looney } 869686cdd19SJun-ichiro itojun Hagino ip6 = mtod(m, struct ip6_hdr *); 8705778b399SBjoern A. Zeeb ip6->ip6_plen = htons((u_short)plen + offset - sizeof(struct ip6_hdr)); 8715e9510e3SJINMEI Tatuya if (q6->ip6q_ecn == IPTOS_ECN_CE) 8725e9510e3SJINMEI Tatuya ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20); 87382cd038dSYoshinobu Inoue nxt = q6->ip6q_nxt; 87482cd038dSYoshinobu Inoue 87521f08a07SBjoern A. Zeeb TAILQ_REMOVE(head, q6, ip6q_tq); 87621f08a07SBjoern A. Zeeb V_ip6qb[bucket].count--; 8772adfd64fSJonathan T. Looney atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag); 87821f08a07SBjoern A. Zeeb 879a61b5cfbSBjoern A. Zeeb ip6_deletefraghdr(m, offset, M_NOWAIT); 88082cd038dSYoshinobu Inoue 88123d374aaSBjoern A. Zeeb /* Set nxt(-hdr field value) to the original value. */ 88268e0e5a6SAndrey V. Elsukov m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t), 88368e0e5a6SAndrey V. Elsukov (caddr_t)&nxt); 88482cd038dSYoshinobu Inoue 8854b908c8bSRobert Watson #ifdef MAC 8864b908c8bSRobert Watson mac_ip6q_reassemble(q6, m); 8874b908c8bSRobert Watson mac_ip6q_destroy(q6); 8884b908c8bSRobert Watson #endif 889487a161cSBjoern A. Zeeb free(q6, M_FRAG6); 89080d7a853SJonathan T. Looney atomic_subtract_int(&V_frag6_nfragpackets, 1); 89182cd038dSYoshinobu Inoue 89282cd038dSYoshinobu Inoue if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */ 8935778b399SBjoern A. Zeeb 8945778b399SBjoern A. Zeeb plen = 0; 89582cd038dSYoshinobu Inoue for (t = m; t; t = t->m_next) 89682cd038dSYoshinobu Inoue plen += t->m_len; 89782cd038dSYoshinobu Inoue m->m_pkthdr.len = plen; 898a55383e7SHans Petter Selasky /* Set a valid receive interface pointer. */ 899a55383e7SHans Petter Selasky m->m_pkthdr.rcvif = srcifp; 90082cd038dSYoshinobu Inoue } 90182cd038dSYoshinobu Inoue 902aaa46574SAdrian Chadd #ifdef RSS 903aaa46574SAdrian Chadd mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc), 904aaa46574SAdrian Chadd M_NOWAIT); 905aaa46574SAdrian Chadd if (mtag == NULL) 906aaa46574SAdrian Chadd goto dropfrag; 907aaa46574SAdrian Chadd 908aaa46574SAdrian Chadd ip6dc = (struct ip6_direct_ctx *)(mtag + 1); 909aaa46574SAdrian Chadd ip6dc->ip6dc_nxt = nxt; 910aaa46574SAdrian Chadd ip6dc->ip6dc_off = offset; 911aaa46574SAdrian Chadd 912aaa46574SAdrian Chadd m_tag_prepend(m, mtag); 913aaa46574SAdrian Chadd #endif 914aaa46574SAdrian Chadd 9159cb1a47aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 9169cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_reassembled); 91782cd038dSYoshinobu Inoue in6_ifstat_inc(dstifp, ifs6_reass_ok); 91882cd038dSYoshinobu Inoue 919aaa46574SAdrian Chadd #ifdef RSS 92023d374aaSBjoern A. Zeeb /* Queue/dispatch for reprocessing. */ 921aaa46574SAdrian Chadd netisr_dispatch(NETISR_IPV6_DIRECT, m); 922a8fe77d8SBjoern A. Zeeb *mp = NULL; 9235778b399SBjoern A. Zeeb return (IPPROTO_DONE); 924aaa46574SAdrian Chadd #endif 925aaa46574SAdrian Chadd 92623d374aaSBjoern A. Zeeb /* Tell launch routine the next header. */ 92782cd038dSYoshinobu Inoue *mp = m; 92882cd038dSYoshinobu Inoue *offp = offset; 92982cd038dSYoshinobu Inoue 9305778b399SBjoern A. Zeeb return (nxt); 93182cd038dSYoshinobu Inoue 93282cd038dSYoshinobu Inoue dropfrag: 9339cb1a47aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 9347715d794SBjoern A. Zeeb dropfrag2: 93582cd038dSYoshinobu Inoue in6_ifstat_inc(dstifp, ifs6_reass_fail); 9369cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_fragdropped); 93782cd038dSYoshinobu Inoue m_freem(m); 938a8fe77d8SBjoern A. Zeeb *mp = NULL; 9395778b399SBjoern A. Zeeb return (IPPROTO_DONE); 94082cd038dSYoshinobu Inoue } 94182cd038dSYoshinobu Inoue 94282cd038dSYoshinobu Inoue /* 94333841545SHajimu UMEMOTO * IPv6 reassembling timer processing; 94423d374aaSBjoern A. Zeeb * if a timer expires on a reassembly queue, discard it. 94582cd038dSYoshinobu Inoue */ 946a0d7d247SGleb Smirnoff static struct callout frag6_callout; 947a0d7d247SGleb Smirnoff static void 948a0d7d247SGleb Smirnoff frag6_slowtimo(void *arg __unused) 94982cd038dSYoshinobu Inoue { 9508b615593SMarko Zec VNET_ITERATOR_DECL(vnet_iter); 95121f08a07SBjoern A. Zeeb struct ip6qhead *head; 95221f08a07SBjoern A. Zeeb struct ip6q *q6, *q6tmp; 9539cb1a47aSBjoern A. Zeeb uint32_t bucket; 95482cd038dSYoshinobu Inoue 9558afe9481SMateusz Guzik if (atomic_load_int(&frag6_nfrags) == 0) 956a0d7d247SGleb Smirnoff goto done; 9578afe9481SMateusz Guzik 9585ee847d3SRobert Watson VNET_LIST_RLOCK_NOSLEEP(); 9598b615593SMarko Zec VNET_FOREACH(vnet_iter) { 9608b615593SMarko Zec CURVNET_SET(vnet_iter); 9619cb1a47aSBjoern A. Zeeb for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 9628afe9481SMateusz Guzik if (V_ip6qb[bucket].count == 0) 9638afe9481SMateusz Guzik continue; 9649cb1a47aSBjoern A. Zeeb IP6QB_LOCK(bucket); 9659cb1a47aSBjoern A. Zeeb head = IP6QB_HEAD(bucket); 96621f08a07SBjoern A. Zeeb TAILQ_FOREACH_SAFE(q6, head, ip6q_tq, q6tmp) 96721f08a07SBjoern A. Zeeb if (--q6->ip6q_ttl == 0) { 968198fdaedSTom Jones IP6STAT_ADD(ip6s_fragtimeout, 96921f08a07SBjoern A. Zeeb q6->ip6q_nfrag); 97082cd038dSYoshinobu Inoue /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 97121f08a07SBjoern A. Zeeb frag6_freef(q6, bucket); 97282cd038dSYoshinobu Inoue } 97382cd038dSYoshinobu Inoue /* 97482cd038dSYoshinobu Inoue * If we are over the maximum number of fragments 97582cd038dSYoshinobu Inoue * (due to the limit being lowered), drain off 97682cd038dSYoshinobu Inoue * enough to get down to the new limit. 9771e9f3b73SJonathan T. Looney * Note that we drain all reassembly queues if 9781e9f3b73SJonathan T. Looney * maxfragpackets is 0 (fragmentation is disabled), 97923d374aaSBjoern A. Zeeb * and do not enforce a limit when maxfragpackets 9801e9f3b73SJonathan T. Looney * is negative. 98182cd038dSYoshinobu Inoue */ 9821e9f3b73SJonathan T. Looney while ((V_ip6_maxfragpackets == 0 || 9831e9f3b73SJonathan T. Looney (V_ip6_maxfragpackets > 0 && 9849cb1a47aSBjoern A. Zeeb V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) && 98521f08a07SBjoern A. Zeeb (q6 = TAILQ_LAST(head, ip6qhead)) != NULL) { 98621f08a07SBjoern A. Zeeb IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag); 98782cd038dSYoshinobu Inoue /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 98821f08a07SBjoern A. Zeeb frag6_freef(q6, bucket); 98980d7a853SJonathan T. Looney } 9909cb1a47aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 99182cd038dSYoshinobu Inoue } 9921e9f3b73SJonathan T. Looney /* 9931e9f3b73SJonathan T. Looney * If we are still over the maximum number of fragmented 9941e9f3b73SJonathan T. Looney * packets, drain off enough to get down to the new limit. 9951e9f3b73SJonathan T. Looney */ 9969cb1a47aSBjoern A. Zeeb bucket = 0; 9971e9f3b73SJonathan T. Looney while (V_ip6_maxfragpackets >= 0 && 9981e9f3b73SJonathan T. Looney atomic_load_int(&V_frag6_nfragpackets) > 9991e9f3b73SJonathan T. Looney (u_int)V_ip6_maxfragpackets) { 10009cb1a47aSBjoern A. Zeeb IP6QB_LOCK(bucket); 100121f08a07SBjoern A. Zeeb q6 = TAILQ_LAST(IP6QB_HEAD(bucket), ip6qhead); 100221f08a07SBjoern A. Zeeb if (q6 != NULL) { 100321f08a07SBjoern A. Zeeb IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag); 10041e9f3b73SJonathan T. Looney /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 100521f08a07SBjoern A. Zeeb frag6_freef(q6, bucket); 10061e9f3b73SJonathan T. Looney } 10079cb1a47aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 10089cb1a47aSBjoern A. Zeeb bucket = (bucket + 1) % IP6REASS_NHASH; 10091e9f3b73SJonathan T. Looney } 10108b615593SMarko Zec CURVNET_RESTORE(); 10118b615593SMarko Zec } 10125ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 1013a0d7d247SGleb Smirnoff done: 1014e32221a1SAlexander V. Chernikov callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS, 1015e32221a1SAlexander V. Chernikov SBT_1MS * 10, frag6_slowtimo, NULL, 0); 101682cd038dSYoshinobu Inoue } 101782cd038dSYoshinobu Inoue 1018a0d7d247SGleb Smirnoff static void 1019a0d7d247SGleb Smirnoff frag6_slowtimo_init(void *arg __unused) 1020a0d7d247SGleb Smirnoff { 1021a0d7d247SGleb Smirnoff 1022a0d7d247SGleb Smirnoff callout_init(&frag6_callout, 1); 1023e32221a1SAlexander V. Chernikov callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS, 1024e32221a1SAlexander V. Chernikov SBT_1MS * 10, frag6_slowtimo, NULL, 0); 1025a0d7d247SGleb Smirnoff } 1026a0d7d247SGleb Smirnoff SYSINIT(frag6, SI_SUB_VNET_DONE, SI_ORDER_ANY, frag6_slowtimo_init, NULL); 1027a0d7d247SGleb Smirnoff 102823d374aaSBjoern A. Zeeb /* 102923d374aaSBjoern A. Zeeb * Eventhandler to adjust limits in case nmbclusters change. 103023d374aaSBjoern A. Zeeb */ 1031c00464a2SBjoern A. Zeeb static void 1032c00464a2SBjoern A. Zeeb frag6_change(void *tag) 1033c00464a2SBjoern A. Zeeb { 1034c00464a2SBjoern A. Zeeb VNET_ITERATOR_DECL(vnet_iter); 1035c00464a2SBjoern A. Zeeb 1036c00464a2SBjoern A. Zeeb ip6_maxfrags = IP6_MAXFRAGS; 1037c00464a2SBjoern A. Zeeb VNET_LIST_RLOCK_NOSLEEP(); 1038c00464a2SBjoern A. Zeeb VNET_FOREACH(vnet_iter) { 1039c00464a2SBjoern A. Zeeb CURVNET_SET(vnet_iter); 1040c00464a2SBjoern A. Zeeb V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS; 1041c00464a2SBjoern A. Zeeb frag6_set_bucketsize(); 1042c00464a2SBjoern A. Zeeb CURVNET_RESTORE(); 1043c00464a2SBjoern A. Zeeb } 1044c00464a2SBjoern A. Zeeb VNET_LIST_RUNLOCK_NOSLEEP(); 1045c00464a2SBjoern A. Zeeb } 1046c00464a2SBjoern A. Zeeb 1047c00464a2SBjoern A. Zeeb /* 1048c00464a2SBjoern A. Zeeb * Initialise reassembly queue and fragment identifier. 1049c00464a2SBjoern A. Zeeb */ 1050c00464a2SBjoern A. Zeeb void 1051c00464a2SBjoern A. Zeeb frag6_init(void) 1052c00464a2SBjoern A. Zeeb { 10539cb1a47aSBjoern A. Zeeb uint32_t bucket; 1054c00464a2SBjoern A. Zeeb 1055c00464a2SBjoern A. Zeeb V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS; 1056c00464a2SBjoern A. Zeeb frag6_set_bucketsize(); 10579cb1a47aSBjoern A. Zeeb for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 105821f08a07SBjoern A. Zeeb TAILQ_INIT(IP6QB_HEAD(bucket)); 1059efdfee93SBjoern A. Zeeb mtx_init(&V_ip6qb[bucket].lock, "ip6qb", NULL, MTX_DEF); 10609cb1a47aSBjoern A. Zeeb V_ip6qb[bucket].count = 0; 1061c00464a2SBjoern A. Zeeb } 10629cb1a47aSBjoern A. Zeeb V_ip6qb_hashseed = arc4random(); 1063c00464a2SBjoern A. Zeeb V_ip6_maxfragsperpacket = 64; 106467a10c46SBjoern A. Zeeb #ifdef VIMAGE 106567a10c46SBjoern A. Zeeb V_frag6_on = true; 106667a10c46SBjoern A. Zeeb #endif 1067c00464a2SBjoern A. Zeeb if (!IS_DEFAULT_VNET(curvnet)) 1068c00464a2SBjoern A. Zeeb return; 1069c00464a2SBjoern A. Zeeb 1070c00464a2SBjoern A. Zeeb ip6_maxfrags = IP6_MAXFRAGS; 1071c00464a2SBjoern A. Zeeb EVENTHANDLER_REGISTER(nmbclusters_change, 1072c00464a2SBjoern A. Zeeb frag6_change, NULL, EVENTHANDLER_PRI_ANY); 1073c00464a2SBjoern A. Zeeb } 1074c00464a2SBjoern A. Zeeb 107582cd038dSYoshinobu Inoue /* 107682cd038dSYoshinobu Inoue * Drain off all datagram fragments. 107782cd038dSYoshinobu Inoue */ 107867a10c46SBjoern A. Zeeb static void 107967a10c46SBjoern A. Zeeb frag6_drain_one(void) 108082cd038dSYoshinobu Inoue { 108121f08a07SBjoern A. Zeeb struct ip6q *q6; 10829cb1a47aSBjoern A. Zeeb uint32_t bucket; 10839888c401SHajimu UMEMOTO 10849cb1a47aSBjoern A. Zeeb for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 108567a10c46SBjoern A. Zeeb IP6QB_LOCK(bucket); 108621f08a07SBjoern A. Zeeb while ((q6 = TAILQ_FIRST(IP6QB_HEAD(bucket))) != NULL) { 10879cb8d207SAndrey V. Elsukov IP6STAT_INC(ip6s_fragdropped); 108882cd038dSYoshinobu Inoue /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */ 108921f08a07SBjoern A. Zeeb frag6_freef(q6, bucket); 109080d7a853SJonathan T. Looney } 10919cb1a47aSBjoern A. Zeeb IP6QB_UNLOCK(bucket); 109282cd038dSYoshinobu Inoue } 109367a10c46SBjoern A. Zeeb } 109467a10c46SBjoern A. Zeeb 109567a10c46SBjoern A. Zeeb void 109667a10c46SBjoern A. Zeeb frag6_drain(void) 109767a10c46SBjoern A. Zeeb { 109867a10c46SBjoern A. Zeeb VNET_ITERATOR_DECL(vnet_iter); 109967a10c46SBjoern A. Zeeb 110067a10c46SBjoern A. Zeeb VNET_LIST_RLOCK_NOSLEEP(); 110167a10c46SBjoern A. Zeeb VNET_FOREACH(vnet_iter) { 110267a10c46SBjoern A. Zeeb CURVNET_SET(vnet_iter); 110367a10c46SBjoern A. Zeeb frag6_drain_one(); 11048b615593SMarko Zec CURVNET_RESTORE(); 11058b615593SMarko Zec } 11065ee847d3SRobert Watson VNET_LIST_RUNLOCK_NOSLEEP(); 110782cd038dSYoshinobu Inoue } 1108e5ee7060SGleb Smirnoff 110967a10c46SBjoern A. Zeeb #ifdef VIMAGE 111067a10c46SBjoern A. Zeeb /* 111167a10c46SBjoern A. Zeeb * Clear up IPv6 reassembly structures. 111267a10c46SBjoern A. Zeeb */ 111367a10c46SBjoern A. Zeeb void 111467a10c46SBjoern A. Zeeb frag6_destroy(void) 111567a10c46SBjoern A. Zeeb { 111667a10c46SBjoern A. Zeeb uint32_t bucket; 111767a10c46SBjoern A. Zeeb 111867a10c46SBjoern A. Zeeb frag6_drain_one(); 111967a10c46SBjoern A. Zeeb V_frag6_on = false; 112067a10c46SBjoern A. Zeeb for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) { 112167a10c46SBjoern A. Zeeb KASSERT(V_ip6qb[bucket].count == 0, 112267a10c46SBjoern A. Zeeb ("%s: V_ip6qb[%d] (%p) count not 0 (%d)", __func__, 112367a10c46SBjoern A. Zeeb bucket, &V_ip6qb[bucket], V_ip6qb[bucket].count)); 112467a10c46SBjoern A. Zeeb mtx_destroy(&V_ip6qb[bucket].lock); 112567a10c46SBjoern A. Zeeb } 112667a10c46SBjoern A. Zeeb } 112767a10c46SBjoern A. Zeeb #endif 1128