summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/netinet6/frag6.c
diff options
context:
space:
mode:
Diffstat (limited to 'freebsd/sys/netinet6/frag6.c')
-rw-r--r--freebsd/sys/netinet6/frag6.c1107
1 files changed, 601 insertions, 506 deletions
diff --git a/freebsd/sys/netinet6/frag6.c b/freebsd/sys/netinet6/frag6.c
index 0b0c7b91..443c684a 100644
--- a/freebsd/sys/netinet6/frag6.c
+++ b/freebsd/sys/netinet6/frag6.c
@@ -5,6 +5,7 @@
*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
+ * Copyright (c) 2019 Netflix, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -40,20 +41,18 @@ __FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
+#include <sys/domain.h>
+#include <sys/eventhandler.h>
#include <sys/hash.h>
+#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
-#include <sys/domain.h>
-#include <sys/eventhandler.h>
#include <sys/protosw.h>
+#include <sys/queue.h>
#include <sys/socket.h>
-#include <sys/errno.h>
-#include <sys/time.h>
-#include <sys/kernel.h>
+#include <sys/sysctl.h>
#include <sys/syslog.h>
-#include <machine/atomic.h>
-
#include <net/if.h>
#include <net/if_var.h>
#include <net/netisr.h>
@@ -65,48 +64,85 @@ __FBSDID("$FreeBSD$");
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet/icmp6.h>
-#include <netinet/in_systm.h> /* for ECN definitions */
-#include <netinet/ip.h> /* for ECN definitions */
+#include <netinet/in_systm.h> /* For ECN definitions. */
+#include <netinet/ip.h> /* For ECN definitions. */
+#ifdef MAC
#include <security/mac/mac_framework.h>
+#endif
/*
- * Reassembly headers are stored in hash buckets.
+ * A "big picture" of how IPv6 fragment queues are all linked together.
+ *
+ * struct ip6qbucket ip6qb[...]; hashed buckets
+ * ||||||||
+ * |
+ * +--- TAILQ(struct ip6q, packets) *q6; tailq entries holding
+ * |||||||| fragmented packets
+ * | (1 per original packet)
+ * |
+ * +--- TAILQ(struct ip6asfrag, ip6q_frags) *af6; tailq entries of IPv6
+ * | *ip6af;fragment packets
+ * | for one original packet
+ * + *mbuf
*/
+
+/* Reassembly headers are stored in hash buckets. */
#define IP6REASS_NHASH_LOG2 10
#define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2)
#define IP6REASS_HMASK (IP6REASS_NHASH - 1)
-static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *,
- uint32_t bucket __unused);
-static void frag6_deq(struct ip6asfrag *, uint32_t bucket __unused);
-static void frag6_insque_head(struct ip6q *, struct ip6q *,
- uint32_t bucket);
-static void frag6_remque(struct ip6q *, uint32_t bucket);
-static void frag6_freef(struct ip6q *, uint32_t bucket);
-
+TAILQ_HEAD(ip6qhead, ip6q);
struct ip6qbucket {
- struct ip6q ip6q;
+ struct ip6qhead packets;
struct mtx lock;
int count;
};
-VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets);
-volatile u_int frag6_nfrags = 0;
-VNET_DEFINE_STATIC(struct ip6qbucket, ip6q[IP6REASS_NHASH]);
-VNET_DEFINE_STATIC(uint32_t, ip6q_hashseed);
+struct ip6asfrag {
+ TAILQ_ENTRY(ip6asfrag) ip6af_tq;
+ struct mbuf *ip6af_m;
+ int ip6af_offset; /* Offset in ip6af_m to next header. */
+ int ip6af_frglen; /* Fragmentable part length. */
+ int ip6af_off; /* Fragment offset. */
+ bool ip6af_mff; /* More fragment bit in frag off. */
+};
+
+static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header");
+
+#ifdef VIMAGE
+/* A flag to indicate if IPv6 fragmentation is initialized. */
+VNET_DEFINE_STATIC(bool, frag6_on);
+#define V_frag6_on VNET(frag6_on)
+#endif
+
+/* System wide (global) maximum and count of packets in reassembly queues. */
+static int ip6_maxfrags;
+static volatile u_int frag6_nfrags = 0;
+/* Maximum and current packets in per-VNET reassembly queue. */
+VNET_DEFINE_STATIC(int, ip6_maxfragpackets);
+VNET_DEFINE_STATIC(volatile u_int, frag6_nfragpackets);
+#define V_ip6_maxfragpackets VNET(ip6_maxfragpackets)
#define V_frag6_nfragpackets VNET(frag6_nfragpackets)
-#define V_ip6q VNET(ip6q)
-#define V_ip6q_hashseed VNET(ip6q_hashseed)
-#define IP6Q_LOCK(i) mtx_lock(&V_ip6q[(i)].lock)
-#define IP6Q_TRYLOCK(i) mtx_trylock(&V_ip6q[(i)].lock)
-#define IP6Q_LOCK_ASSERT(i) mtx_assert(&V_ip6q[(i)].lock, MA_OWNED)
-#define IP6Q_UNLOCK(i) mtx_unlock(&V_ip6q[(i)].lock)
-#define IP6Q_HEAD(i) (&V_ip6q[(i)].ip6q)
+/* Maximum per-VNET reassembly queues per bucket and fragments per packet. */
+VNET_DEFINE_STATIC(int, ip6_maxfragbucketsize);
+VNET_DEFINE_STATIC(int, ip6_maxfragsperpacket);
+#define V_ip6_maxfragbucketsize VNET(ip6_maxfragbucketsize)
+#define V_ip6_maxfragsperpacket VNET(ip6_maxfragsperpacket)
+
+/* Per-VNET reassembly queue buckets. */
+VNET_DEFINE_STATIC(struct ip6qbucket, ip6qb[IP6REASS_NHASH]);
+VNET_DEFINE_STATIC(uint32_t, ip6qb_hashseed);
+#define V_ip6qb VNET(ip6qb)
+#define V_ip6qb_hashseed VNET(ip6qb_hashseed)
-static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
+#define IP6QB_LOCK(_b) mtx_lock(&V_ip6qb[(_b)].lock)
+#define IP6QB_TRYLOCK(_b) mtx_trylock(&V_ip6qb[(_b)].lock)
+#define IP6QB_LOCK_ASSERT(_b) mtx_assert(&V_ip6qb[(_b)].lock, MA_OWNED)
+#define IP6QB_UNLOCK(_b) mtx_unlock(&V_ip6qb[(_b)].lock)
+#define IP6QB_HEAD(_b) (&V_ip6qb[(_b)].packets)
/*
* By default, limit the number of IP6 fragments across all reassembly
@@ -124,11 +160,18 @@ static MALLOC_DEFINE(M_FTABLE, "fragment", "fragment reassembly header");
#define IP6_MAXFRAGS (nmbclusters / 32)
#define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
+
/*
- * Initialise reassembly queue and fragment identifier.
+ * Sysctls and helper function.
*/
-void
-frag6_set_bucketsize()
+SYSCTL_DECL(_net_inet6_ip6);
+
+SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfrags,
+ CTLFLAG_RD, __DEVOLATILE(u_int *, &frag6_nfrags), 0,
+ "Global number of IPv6 fragments across all reassembly queues.");
+
+static void
+frag6_set_bucketsize(void)
{
int i;
@@ -136,68 +179,180 @@ frag6_set_bucketsize()
V_ip6_maxfragbucketsize = imax(i / (IP6REASS_NHASH / 2), 1);
}
+SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGS, maxfrags,
+ CTLFLAG_RW, &ip6_maxfrags, 0,
+ "Maximum allowed number of outstanding IPv6 packet fragments. "
+ "A value of 0 means no fragmented packets will be accepted, while a "
+ "a value of -1 means no limit");
+
+static int
+sysctl_ip6_maxfragpackets(SYSCTL_HANDLER_ARGS)
+{
+ int error, val;
+
+ val = V_ip6_maxfragpackets;
+ error = sysctl_handle_int(oidp, &val, 0, req);
+ if (error != 0 || !req->newptr)
+ return (error);
+ V_ip6_maxfragpackets = val;
+ frag6_set_bucketsize();
+ return (0);
+}
+SYSCTL_PROC(_net_inet6_ip6, IPV6CTL_MAXFRAGPACKETS, maxfragpackets,
+ CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW, NULL, 0,
+ sysctl_ip6_maxfragpackets, "I",
+ "Default maximum number of outstanding fragmented IPv6 packets. "
+ "A value of 0 means no fragmented packets will be accepted, while a "
+ "a value of -1 means no limit");
+SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfragpackets,
+ CTLFLAG_VNET | CTLFLAG_RD,
+ __DEVOLATILE(u_int *, &VNET_NAME(frag6_nfragpackets)), 0,
+ "Per-VNET number of IPv6 fragments across all reassembly queues.");
+SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGSPERPACKET, maxfragsperpacket,
+ CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragsperpacket), 0,
+ "Maximum allowed number of fragments per packet");
+SYSCTL_INT(_net_inet6_ip6, IPV6CTL_MAXFRAGBUCKETSIZE, maxfragbucketsize,
+ CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip6_maxfragbucketsize), 0,
+ "Maximum number of reassembly queues per hash bucket");
+
+
+/*
+ * Remove the IPv6 fragmentation header from the mbuf.
+ */
+int
+ip6_deletefraghdr(struct mbuf *m, int offset, int wait __unused)
+{
+ struct ip6_hdr *ip6;
+
+ KASSERT(m->m_len >= offset + sizeof(struct ip6_frag),
+ ("%s: ext headers not contigous in mbuf %p m_len %d >= "
+ "offset %d + %zu\n", __func__, m, m->m_len, offset,
+ sizeof(struct ip6_frag)));
+
+ /* Delete frag6 header. */
+ ip6 = mtod(m, struct ip6_hdr *);
+ bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag), offset);
+ m->m_data += sizeof(struct ip6_frag);
+ m->m_len -= sizeof(struct ip6_frag);
+ m->m_flags |= M_FRAGMENTED;
+
+ return (0);
+}
+
+/*
+ * Free a fragment reassembly header and all associated datagrams.
+ */
static void
-frag6_change(void *tag)
+frag6_freef(struct ip6q *q6, uint32_t bucket)
{
- VNET_ITERATOR_DECL(vnet_iter);
+ struct ip6_hdr *ip6;
+ struct ip6asfrag *af6;
+ struct mbuf *m;
- ip6_maxfrags = IP6_MAXFRAGS;
- VNET_LIST_RLOCK_NOSLEEP();
- VNET_FOREACH(vnet_iter) {
- CURVNET_SET(vnet_iter);
- V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
- frag6_set_bucketsize();
- CURVNET_RESTORE();
+ IP6QB_LOCK_ASSERT(bucket);
+
+ while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
+
+ m = af6->ip6af_m;
+ TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
+
+ /*
+ * Return ICMP time exceeded error for the 1st fragment.
+ * Just free other fragments.
+ */
+ if (af6->ip6af_off == 0 && m->m_pkthdr.rcvif != NULL) {
+
+ /* Adjust pointer. */
+ ip6 = mtod(m, struct ip6_hdr *);
+
+ /* Restore source and destination addresses. */
+ ip6->ip6_src = q6->ip6q_src;
+ ip6->ip6_dst = q6->ip6q_dst;
+
+ icmp6_error(m, ICMP6_TIME_EXCEEDED,
+ ICMP6_TIME_EXCEED_REASSEMBLY, 0);
+ } else
+ m_freem(m);
+
+ free(af6, M_FRAG6);
}
- VNET_LIST_RUNLOCK_NOSLEEP();
+
+ TAILQ_REMOVE(IP6QB_HEAD(bucket), q6, ip6q_tq);
+ V_ip6qb[bucket].count--;
+ atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
+#ifdef MAC
+ mac_ip6q_destroy(q6);
+#endif
+ free(q6, M_FRAG6);
+ atomic_subtract_int(&V_frag6_nfragpackets, 1);
}
-void
-frag6_init(void)
+/*
+ * Drain off all datagram fragments belonging to
+ * the given network interface.
+ */
+static void
+frag6_cleanup(void *arg __unused, struct ifnet *ifp)
{
+ struct ip6qhead *head;
struct ip6q *q6;
- int i;
+ struct ip6asfrag *af6;
+ uint32_t bucket;
- V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
- frag6_set_bucketsize();
- for (i = 0; i < IP6REASS_NHASH; i++) {
- q6 = IP6Q_HEAD(i);
- q6->ip6q_next = q6->ip6q_prev = q6;
- mtx_init(&V_ip6q[i].lock, "ip6qlock", NULL, MTX_DEF);
- V_ip6q[i].count = 0;
- }
- V_ip6q_hashseed = arc4random();
- V_ip6_maxfragsperpacket = 64;
- if (!IS_DEFAULT_VNET(curvnet))
+ KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
+
+ CURVNET_SET_QUIET(ifp->if_vnet);
+#ifdef VIMAGE
+ /*
+ * Skip processing if IPv6 reassembly is not initialised or
+ * torn down by frag6_destroy().
+ */
+ if (!V_frag6_on) {
+ CURVNET_RESTORE();
return;
+ }
+#endif
- ip6_maxfrags = IP6_MAXFRAGS;
- EVENTHANDLER_REGISTER(nmbclusters_change,
- frag6_change, NULL, EVENTHANDLER_PRI_ANY);
+ for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
+ IP6QB_LOCK(bucket);
+ head = IP6QB_HEAD(bucket);
+ /* Scan fragment list. */
+ TAILQ_FOREACH(q6, head, ip6q_tq) {
+ TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
+
+ /* Clear no longer valid rcvif pointer. */
+ if (af6->ip6af_m->m_pkthdr.rcvif == ifp)
+ af6->ip6af_m->m_pkthdr.rcvif = NULL;
+ }
+ }
+ IP6QB_UNLOCK(bucket);
+ }
+ CURVNET_RESTORE();
}
+EVENTHANDLER_DEFINE(ifnet_departure_event, frag6_cleanup, NULL, 0);
/*
- * In RFC2460, fragment and reassembly rule do not agree with each other,
- * in terms of next header field handling in fragment header.
+ * Like in RFC2460, in RFC8200, fragment and reassembly rules do not agree with
+ * each other, in terms of next header field handling in fragment header.
* While the sender will use the same value for all of the fragmented packets,
- * receiver is suggested not to check the consistency.
+ * receiver is suggested not to check for consistency.
*
- * fragment rule (p20):
- * (2) A Fragment header containing:
- * The Next Header value that identifies the first header of
- * the Fragmentable Part of the original packet.
+ * Fragment rules (p18,p19):
+ * (2) A Fragment header containing:
+ * The Next Header value that identifies the first header
+ * after the Per-Fragment headers of the original packet.
* -> next header field is same for all fragments
*
- * reassembly rule (p21):
- * The Next Header field of the last header of the Unfragmentable
- * Part is obtained from the Next Header field of the first
+ * Reassembly rule (p20):
+ * The Next Header field of the last header of the Per-Fragment
+ * headers is obtained from the Next Header field of the first
* fragment's Fragment header.
* -> should grab it from the first fragment only
*
* The following note also contradicts with fragment rule - no one is going to
* send different fragment with different next header field.
*
- * additional note (p22):
+ * Additional note (p22) [not an error]:
* The Next Header values in the Fragment headers of different
* fragments of the same original packet may differ. Only the value
* from the Offset zero fragment packet is used for reassembly.
@@ -206,91 +361,111 @@ frag6_init(void)
* There is no explicit reason given in the RFC. Historical reason maybe?
*/
/*
- * Fragment input
+ * Fragment input.
*/
int
frag6_input(struct mbuf **mp, int *offp, int proto)
{
- struct mbuf *m = *mp, *t;
+ struct mbuf *m, *t;
struct ip6_hdr *ip6;
struct ip6_frag *ip6f;
- struct ip6q *head, *q6;
- struct ip6asfrag *af6, *ip6af, *af6dwn;
- struct in6_ifaddr *ia;
- int offset = *offp, nxt, i, next;
- int first_frag = 0;
- int fragoff, frgpartlen; /* must be larger than u_int16_t */
+ struct ip6qhead *head;
+ struct ip6q *q6;
+ struct ip6asfrag *af6, *ip6af, *af6tmp;
+ struct in6_ifaddr *ia6;
+ struct ifnet *dstifp, *srcifp;
uint32_t hashkey[(sizeof(struct in6_addr) * 2 +
sizeof(ip6f->ip6f_ident)) / sizeof(uint32_t)];
- uint32_t hash, *hashkeyp;
- struct ifnet *dstifp;
- u_int8_t ecn, ecn0;
+ uint32_t bucket, *hashkeyp;
+ int fragoff, frgpartlen; /* Must be larger than uint16_t. */
+ int nxt, offset, plen;
+ uint8_t ecn, ecn0;
+ bool only_frag;
#ifdef RSS
- struct m_tag *mtag;
struct ip6_direct_ctx *ip6dc;
+ struct m_tag *mtag;
#endif
-#if 0
- char ip6buf[INET6_ADDRSTRLEN];
-#endif
+ m = *mp;
+ offset = *offp;
+
+ M_ASSERTPKTHDR(m);
+ if (m->m_len < offset + sizeof(struct ip6_frag)) {
+ m = m_pullup(m, offset + sizeof(struct ip6_frag));
+ if (m == NULL) {
+ IP6STAT_INC(ip6s_exthdrtoolong);
+ *mp = NULL;
+ return (IPPROTO_DONE);
+ }
+ }
ip6 = mtod(m, struct ip6_hdr *);
-#ifndef PULLDOWN_TEST
- IP6_EXTHDR_CHECK(m, offset, sizeof(struct ip6_frag), IPPROTO_DONE);
- ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
-#else
- IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
- if (ip6f == NULL)
- return (IPPROTO_DONE);
-#endif
dstifp = NULL;
- /* find the destination interface of the packet. */
- ia = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
- if (ia != NULL) {
- dstifp = ia->ia_ifp;
- ifa_free(&ia->ia_ifa);
+ /* Find the destination interface of the packet. */
+ ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 /* XXX */);
+ if (ia6 != NULL) {
+ dstifp = ia6->ia_ifp;
+ ifa_free(&ia6->ia_ifa);
}
- /* jumbo payload can't contain a fragment header */
+
+ /* Jumbo payload cannot contain a fragment header. */
if (ip6->ip6_plen == 0) {
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
in6_ifstat_inc(dstifp, ifs6_reass_fail);
- return IPPROTO_DONE;
+ *mp = NULL;
+ return (IPPROTO_DONE);
}
/*
- * check whether fragment packet's fragment length is
- * multiple of 8 octets.
+ * Check whether fragment packet's fragment length is a
+ * multiple of 8 octets (unless it is the last one).
* sizeof(struct ip6_frag) == 8
* sizeof(struct ip6_hdr) = 40
*/
+ ip6f = (struct ip6_frag *)((caddr_t)ip6 + offset);
if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
(((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offsetof(struct ip6_hdr, ip6_plen));
in6_ifstat_inc(dstifp, ifs6_reass_fail);
- return IPPROTO_DONE;
+ *mp = NULL;
+ return (IPPROTO_DONE);
}
IP6STAT_INC(ip6s_fragments);
in6_ifstat_inc(dstifp, ifs6_reass_reqd);
- /* offset now points to data portion */
- offset += sizeof(struct ip6_frag);
-
/*
- * RFC 6946: Handle "atomic" fragments (offset and m bit set to 0)
- * upfront, unrelated to any reassembly. Just skip the fragment header.
+ * Handle "atomic" fragments (offset and m bit set to 0) upfront,
+ * unrelated to any reassembly. We need to remove the frag hdr
+ * which is ugly.
+ * See RFC 6946 and section 4.5 of RFC 8200.
*/
if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
- /* XXX-BZ we want dedicated counters for this. */
- IP6STAT_INC(ip6s_reassembled);
+ IP6STAT_INC(ip6s_atomicfrags);
+ nxt = ip6f->ip6f_nxt;
+ /*
+ * Set nxt(-hdr field value) to the original value.
+ * We cannot just set ip6->ip6_nxt as there might be
+ * an unfragmentable part with extension headers and
+ * we must update the last one.
+ */
+ m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
+ (caddr_t)&nxt);
+ ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) -
+ sizeof(struct ip6_frag));
+ if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0)
+ goto dropfrag2;
+ m->m_pkthdr.len -= sizeof(struct ip6_frag);
in6_ifstat_inc(dstifp, ifs6_reass_ok);
- *offp = offset;
- m->m_flags |= M_FRAGMENTED;
- return (ip6f->ip6f_nxt);
+ *mp = m;
+ return (nxt);
}
+ /* Offset now points to data portion. */
+ offset += sizeof(struct ip6_frag);
+
/* Get fragment length and discard 0-byte fragments. */
frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
if (frgpartlen == 0) {
@@ -298,31 +473,48 @@ frag6_input(struct mbuf **mp, int *offp, int proto)
offsetof(struct ip6_hdr, ip6_plen));
in6_ifstat_inc(dstifp, ifs6_reass_fail);
IP6STAT_INC(ip6s_fragdropped);
- return IPPROTO_DONE;
+ *mp = NULL;
+ return (IPPROTO_DONE);
}
- hashkeyp = hashkey;
- memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
- hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
- memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
- hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
- *hashkeyp = ip6f->ip6f_ident;
- hash = jenkins_hash32(hashkey, nitems(hashkey), V_ip6q_hashseed);
- hash &= IP6REASS_HMASK;
- head = IP6Q_HEAD(hash);
- IP6Q_LOCK(hash);
-
/*
- * Enforce upper bound on number of fragments.
+ * Enforce upper bound on number of fragments for the entire system.
* If maxfrag is 0, never accept fragments.
* If maxfrag is -1, accept all fragments without limitation.
*/
if (ip6_maxfrags < 0)
;
else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
- goto dropfrag;
+ goto dropfrag2;
+
+ /*
+ * Validate that a full header chain to the ULP is present in the
+ * packet containing the first fragment as per RFC RFC7112 and
+ * RFC 8200 pages 18,19:
+ * The first fragment packet is composed of:
+ * (3) Extension headers, if any, and the Upper-Layer header. These
+ * headers must be in the first fragment. ...
+ */
+ fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
+ /* XXX TODO. thj has D16851 open for this. */
+ /* Send ICMPv6 4,3 in case of violation. */
- for (q6 = head->ip6q_next; q6 != head; q6 = q6->ip6q_next)
+ /* Store receive network interface pointer for later. */
+ srcifp = m->m_pkthdr.rcvif;
+
+ /* Generate a hash value for fragment bucket selection. */
+ hashkeyp = hashkey;
+ memcpy(hashkeyp, &ip6->ip6_src, sizeof(struct in6_addr));
+ hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
+ memcpy(hashkeyp, &ip6->ip6_dst, sizeof(struct in6_addr));
+ hashkeyp += sizeof(struct in6_addr) / sizeof(*hashkeyp);
+ *hashkeyp = ip6f->ip6f_ident;
+ bucket = jenkins_hash32(hashkey, nitems(hashkey), V_ip6qb_hashseed);
+ bucket &= IP6REASS_HMASK;
+ IP6QB_LOCK(bucket);
+ head = IP6QB_HEAD(bucket);
+
+ TAILQ_FOREACH(q6, head, ip6q_tq)
if (ip6f->ip6f_ident == q6->ip6q_ident &&
IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst)
@@ -332,11 +524,11 @@ frag6_input(struct mbuf **mp, int *offp, int proto)
)
break;
- if (q6 == head) {
- /*
- * the first fragment to arrive, create a reassembly queue.
- */
- first_frag = 1;
+ only_frag = false;
+ if (q6 == NULL) {
+
+ /* A first fragment to arrive creates a reassembly queue. */
+ only_frag = true;
/*
* Enforce upper bound on number of fragmented packets
@@ -347,30 +539,27 @@ frag6_input(struct mbuf **mp, int *offp, int proto)
*/
if (V_ip6_maxfragpackets < 0)
;
- else if (V_ip6q[hash].count >= V_ip6_maxfragbucketsize ||
+ else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize ||
atomic_load_int(&V_frag6_nfragpackets) >=
(u_int)V_ip6_maxfragpackets)
goto dropfrag;
- atomic_add_int(&V_frag6_nfragpackets, 1);
- q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
- M_NOWAIT);
+
+ /* Allocate IPv6 fragement packet queue entry. */
+ q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FRAG6,
+ M_NOWAIT | M_ZERO);
if (q6 == NULL)
goto dropfrag;
- bzero(q6, sizeof(*q6));
#ifdef MAC
if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
- free(q6, M_FTABLE);
+ free(q6, M_FRAG6);
goto dropfrag;
}
mac_ip6q_create(m, q6);
#endif
- frag6_insque_head(q6, head, hash);
+ atomic_add_int(&V_frag6_nfragpackets, 1);
- /* ip6q_nxt will be filled afterwards, from 1st fragment */
- q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
-#ifdef notyet
- q6->ip6q_nxtp = (u_char *)nxtp;
-#endif
+ /* ip6q_nxt will be filled afterwards, from 1st fragment. */
+ TAILQ_INIT(&q6->ip6q_frags);
q6->ip6q_ident = ip6f->ip6f_ident;
q6->ip6q_ttl = IPV6_FRAGTTL;
q6->ip6q_src = ip6->ip6_src;
@@ -379,18 +568,24 @@ frag6_input(struct mbuf **mp, int *offp, int proto)
(ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
- q6->ip6q_nfrag = 0;
+ /* Add the fragemented packet to the bucket. */
+ TAILQ_INSERT_HEAD(head, q6, ip6q_tq);
+ V_ip6qb[bucket].count++;
}
/*
- * If it's the 1st fragment, record the length of the
+ * If it is the 1st fragment, record the length of the
* unfragmentable part and the next header of the fragment header.
+ * Assume the first 1st fragement to arrive will be correct.
+ * We do not have any duplicate checks here yet so another packet
+ * with fragoff == 0 could come and overwrite the ip6q_unfrglen
+ * and worse, the next header, at any time.
*/
- fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
- if (fragoff == 0) {
+ if (fragoff == 0 && q6->ip6q_unfrglen == -1) {
q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
sizeof(struct ip6_frag);
q6->ip6q_nxt = ip6f->ip6f_nxt;
+ /* XXX ECN? */
}
/*
@@ -401,39 +596,66 @@ frag6_input(struct mbuf **mp, int *offp, int proto)
if (q6->ip6q_unfrglen >= 0) {
/* The 1st fragment has already arrived. */
if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
+ if (only_frag) {
+ TAILQ_REMOVE(head, q6, ip6q_tq);
+ V_ip6qb[bucket].count--;
+ atomic_subtract_int(&V_frag6_nfragpackets, 1);
+#ifdef MAC
+ mac_ip6q_destroy(q6);
+#endif
+ free(q6, M_FRAG6);
+ }
+ IP6QB_UNLOCK(bucket);
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offset - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
- IP6Q_UNLOCK(hash);
+ *mp = NULL;
return (IPPROTO_DONE);
}
} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
+ if (only_frag) {
+ TAILQ_REMOVE(head, q6, ip6q_tq);
+ V_ip6qb[bucket].count--;
+ atomic_subtract_int(&V_frag6_nfragpackets, 1);
+#ifdef MAC
+ mac_ip6q_destroy(q6);
+#endif
+ free(q6, M_FRAG6);
+ }
+ IP6QB_UNLOCK(bucket);
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offset - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
- IP6Q_UNLOCK(hash);
+ *mp = NULL;
return (IPPROTO_DONE);
}
+
/*
- * If it's the first fragment, do the above check for each
+ * If it is the first fragment, do the above check for each
* fragment already stored in the reassembly queue.
*/
- if (fragoff == 0) {
- for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
- af6 = af6dwn) {
- af6dwn = af6->ip6af_down;
-
- if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
- IPV6_MAXPACKET) {
- struct mbuf *merr = IP6_REASS_MBUF(af6);
+ if (fragoff == 0 && !only_frag) {
+ TAILQ_FOREACH_SAFE(af6, &q6->ip6q_frags, ip6af_tq, af6tmp) {
+
+ if (q6->ip6q_unfrglen + af6->ip6af_off +
+ af6->ip6af_frglen > IPV6_MAXPACKET) {
struct ip6_hdr *ip6err;
- int erroff = af6->ip6af_offset;
+ struct mbuf *merr;
+ int erroff;
+
+ merr = af6->ip6af_m;
+ erroff = af6->ip6af_offset;
- /* dequeue the fragment. */
- frag6_deq(af6, hash);
- free(af6, M_FTABLE);
+ /* Dequeue the fragment. */
+ TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
+ q6->ip6q_nfrag--;
+ atomic_subtract_int(&frag6_nfrags, 1);
+ free(af6, M_FRAG6);
- /* adjust pointer. */
+ /* Set a valid receive interface pointer. */
+ merr->m_pkthdr.rcvif = srcifp;
+
+ /* Adjust pointer. */
ip6err = mtod(merr, struct ip6_hdr *);
/*
@@ -451,239 +673,182 @@ frag6_input(struct mbuf **mp, int *offp, int proto)
}
}
- ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
- M_NOWAIT);
+ /* Allocate an IPv6 fragement queue entry for this fragmented part. */
+ ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FRAG6,
+ M_NOWAIT | M_ZERO);
if (ip6af == NULL)
goto dropfrag;
- bzero(ip6af, sizeof(*ip6af));
- ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
+ ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) ? true : false;
ip6af->ip6af_off = fragoff;
ip6af->ip6af_frglen = frgpartlen;
ip6af->ip6af_offset = offset;
- IP6_REASS_MBUF(ip6af) = m;
+ ip6af->ip6af_m = m;
- if (first_frag) {
- af6 = (struct ip6asfrag *)q6;
- goto insert;
+ if (only_frag) {
+ /*
+ * Do a manual insert rather than a hard-to-understand cast
+ * to a different type relying on data structure order to work.
+ */
+ TAILQ_INSERT_HEAD(&q6->ip6q_frags, ip6af, ip6af_tq);
+ goto postinsert;
}
+ /* Do duplicate, condition, and boundry checks. */
/*
* Handle ECN by comparing this segment with the first one;
* if CE is set, do not lose CE.
- * drop if CE and not-ECT are mixed for the same packet.
+ * Drop if CE and not-ECT are mixed for the same packet.
*/
ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
ecn0 = q6->ip6q_ecn;
if (ecn == IPTOS_ECN_CE) {
if (ecn0 == IPTOS_ECN_NOTECT) {
- free(ip6af, M_FTABLE);
+ free(ip6af, M_FRAG6);
goto dropfrag;
}
if (ecn0 != IPTOS_ECN_CE)
q6->ip6q_ecn = IPTOS_ECN_CE;
}
if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
- free(ip6af, M_FTABLE);
+ free(ip6af, M_FRAG6);
goto dropfrag;
}
- /*
- * Find a segment which begins after this one does.
- */
- for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
- af6 = af6->ip6af_down)
+ /* Find a fragmented part which begins after this one does. */
+ TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq)
if (af6->ip6af_off > ip6af->ip6af_off)
break;
-#if 0
- /*
- * If there is a preceding segment, it may provide some of
- * our data already. If so, drop the data from the incoming
- * segment. If it provides all of our data, drop us.
- */
- if (af6->ip6af_up != (struct ip6asfrag *)q6) {
- i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
- - ip6af->ip6af_off;
- if (i > 0) {
- if (i >= ip6af->ip6af_frglen)
- goto dropfrag;
- m_adj(IP6_REASS_MBUF(ip6af), i);
- ip6af->ip6af_off += i;
- ip6af->ip6af_frglen -= i;
- }
- }
-
- /*
- * While we overlap succeeding segments trim them or,
- * if they are completely covered, dequeue them.
- */
- while (af6 != (struct ip6asfrag *)q6 &&
- ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
- i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
- if (i < af6->ip6af_frglen) {
- af6->ip6af_frglen -= i;
- af6->ip6af_off += i;
- m_adj(IP6_REASS_MBUF(af6), i);
- break;
- }
- af6 = af6->ip6af_down;
- m_freem(IP6_REASS_MBUF(af6->ip6af_up));
- frag6_deq(af6->ip6af_up, hash);
- }
-#else
/*
* If the incoming framgent overlaps some existing fragments in
- * the reassembly queue, drop it, since it is dangerous to override
- * existing fragments from a security point of view.
- * We don't know which fragment is the bad guy - here we trust
- * fragment that came in earlier, with no real reason.
- *
- * Note: due to changes after disabling this part, mbuf passed to
- * m_adj() below now does not meet the requirement.
+ * the reassembly queue, drop both the new fragment and the
+ * entire reassembly queue. However, if the new fragment
+ * is an exact duplicate of an existing fragment, only silently
+ * drop the existing fragment and leave the fragmentation queue
+ * unchanged, as allowed by the RFC. (RFC 8200, 4.5)
*/
- if (af6->ip6af_up != (struct ip6asfrag *)q6) {
- i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
- - ip6af->ip6af_off;
- if (i > 0) {
-#if 0 /* suppress the noisy log */
- log(LOG_ERR, "%d bytes of a fragment from %s "
- "overlaps the previous fragment\n",
- i, ip6_sprintf(ip6buf, &q6->ip6q_src));
-#endif
- free(ip6af, M_FTABLE);
+ if (af6 != NULL)
+ af6tmp = TAILQ_PREV(af6, ip6fraghead, ip6af_tq);
+ else
+ af6tmp = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
+ if (af6tmp != NULL) {
+ if (af6tmp->ip6af_off + af6tmp->ip6af_frglen -
+ ip6af->ip6af_off > 0) {
+ if (af6tmp->ip6af_off != ip6af->ip6af_off ||
+ af6tmp->ip6af_frglen != ip6af->ip6af_frglen)
+ frag6_freef(q6, bucket);
+ free(ip6af, M_FRAG6);
goto dropfrag;
}
}
- if (af6 != (struct ip6asfrag *)q6) {
- i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
- if (i > 0) {
-#if 0 /* suppress the noisy log */
- log(LOG_ERR, "%d bytes of a fragment from %s "
- "overlaps the succeeding fragment",
- i, ip6_sprintf(ip6buf, &q6->ip6q_src));
-#endif
- free(ip6af, M_FTABLE);
+ if (af6 != NULL) {
+ if (ip6af->ip6af_off + ip6af->ip6af_frglen -
+ af6->ip6af_off > 0) {
+ if (af6->ip6af_off != ip6af->ip6af_off ||
+ af6->ip6af_frglen != ip6af->ip6af_frglen)
+ frag6_freef(q6, bucket);
+ free(ip6af, M_FRAG6);
goto dropfrag;
}
}
-#endif
-insert:
#ifdef MAC
- if (!first_frag)
- mac_ip6q_update(m, q6);
+ mac_ip6q_update(m, q6);
#endif
/*
- * Stick new segment in its place;
- * check for complete reassembly.
- * If not complete, check fragment limit.
- * Move to front of packet queue, as we are
- * the most recently active fragmented packet.
+ * Stick new segment in its place; check for complete reassembly.
+ * If not complete, check fragment limit. Move to front of packet
+ * queue, as we are the most recently active fragmented packet.
*/
- frag6_enq(ip6af, af6->ip6af_up, hash);
+ if (af6 != NULL)
+ TAILQ_INSERT_BEFORE(af6, ip6af, ip6af_tq);
+ else
+ TAILQ_INSERT_TAIL(&q6->ip6q_frags, ip6af, ip6af_tq);
+postinsert:
atomic_add_int(&frag6_nfrags, 1);
q6->ip6q_nfrag++;
-#if 0 /* xxx */
- if (q6 != head->ip6q_next) {
- frag6_remque(q6, hash);
- frag6_insque_head(q6, head, hash);
- }
-#endif
- next = 0;
- for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
- af6 = af6->ip6af_down) {
- if (af6->ip6af_off != next) {
+
+ plen = 0;
+ TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
+ if (af6->ip6af_off != plen) {
if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
- IP6STAT_INC(ip6s_fragdropped);
- frag6_freef(q6, hash);
+ IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
+ frag6_freef(q6, bucket);
}
- IP6Q_UNLOCK(hash);
- return IPPROTO_DONE;
+ IP6QB_UNLOCK(bucket);
+ *mp = NULL;
+ return (IPPROTO_DONE);
}
- next += af6->ip6af_frglen;
+ plen += af6->ip6af_frglen;
}
- if (af6->ip6af_up->ip6af_mff) {
+ af6 = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
+ if (af6->ip6af_mff) {
if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
- IP6STAT_INC(ip6s_fragdropped);
- frag6_freef(q6, hash);
+ IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
+ frag6_freef(q6, bucket);
}
- IP6Q_UNLOCK(hash);
- return IPPROTO_DONE;
+ IP6QB_UNLOCK(bucket);
+ *mp = NULL;
+ return (IPPROTO_DONE);
}
- /*
- * Reassembly is complete; concatenate fragments.
- */
- ip6af = q6->ip6q_down;
- t = m = IP6_REASS_MBUF(ip6af);
- af6 = ip6af->ip6af_down;
- frag6_deq(ip6af, hash);
- while (af6 != (struct ip6asfrag *)q6) {
+ /* Reassembly is complete; concatenate fragments. */
+ ip6af = TAILQ_FIRST(&q6->ip6q_frags);
+ t = m = ip6af->ip6af_m;
+ TAILQ_REMOVE(&q6->ip6q_frags, ip6af, ip6af_tq);
+ while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
m->m_pkthdr.csum_flags &=
- IP6_REASS_MBUF(af6)->m_pkthdr.csum_flags;
+ af6->ip6af_m->m_pkthdr.csum_flags;
m->m_pkthdr.csum_data +=
- IP6_REASS_MBUF(af6)->m_pkthdr.csum_data;
-
- af6dwn = af6->ip6af_down;
- frag6_deq(af6, hash);
- while (t->m_next)
- t = t->m_next;
- m_adj(IP6_REASS_MBUF(af6), af6->ip6af_offset);
- m_demote_pkthdr(IP6_REASS_MBUF(af6));
- m_cat(t, IP6_REASS_MBUF(af6));
- free(af6, M_FTABLE);
- af6 = af6dwn;
+ af6->ip6af_m->m_pkthdr.csum_data;
+
+ TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
+ t = m_last(t);
+ m_adj(af6->ip6af_m, af6->ip6af_offset);
+ m_demote_pkthdr(af6->ip6af_m);
+ m_cat(t, af6->ip6af_m);
+ free(af6, M_FRAG6);
}
while (m->m_pkthdr.csum_data & 0xffff0000)
m->m_pkthdr.csum_data = (m->m_pkthdr.csum_data & 0xffff) +
(m->m_pkthdr.csum_data >> 16);
- /* adjust offset to point where the original next header starts */
+ /* Adjust offset to point where the original next header starts. */
offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
- free(ip6af, M_FTABLE);
+ free(ip6af, M_FRAG6);
ip6 = mtod(m, struct ip6_hdr *);
- ip6->ip6_plen = htons((u_short)next + offset - sizeof(struct ip6_hdr));
+ ip6->ip6_plen = htons((u_short)plen + offset - sizeof(struct ip6_hdr));
if (q6->ip6q_ecn == IPTOS_ECN_CE)
ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
nxt = q6->ip6q_nxt;
-#ifdef notyet
- *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
-#endif
- if (ip6_deletefraghdr(m, offset, M_NOWAIT) != 0) {
- frag6_remque(q6, hash);
- atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
-#ifdef MAC
- mac_ip6q_destroy(q6);
-#endif
- free(q6, M_FTABLE);
- atomic_subtract_int(&V_frag6_nfragpackets, 1);
+ TAILQ_REMOVE(head, q6, ip6q_tq);
+ V_ip6qb[bucket].count--;
+ atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
- goto dropfrag;
- }
+ ip6_deletefraghdr(m, offset, M_NOWAIT);
- /*
- * Store NXT to the original.
- */
+ /* Set nxt(-hdr field value) to the original value. */
m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
(caddr_t)&nxt);
- frag6_remque(q6, hash);
- atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
#ifdef MAC
mac_ip6q_reassemble(q6, m);
mac_ip6q_destroy(q6);
#endif
- free(q6, M_FTABLE);
+ free(q6, M_FRAG6);
atomic_subtract_int(&V_frag6_nfragpackets, 1);
if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
- int plen = 0;
+
+ plen = 0;
for (t = m; t; t = t->m_next)
plen += t->m_len;
m->m_pkthdr.len = plen;
+ /* Set a valid receive interface pointer. */
+ m->m_pkthdr.rcvif = srcifp;
}
#ifdef RSS
@@ -699,211 +864,94 @@ insert:
m_tag_prepend(m, mtag);
#endif
- IP6Q_UNLOCK(hash);
+ IP6QB_UNLOCK(bucket);
IP6STAT_INC(ip6s_reassembled);
in6_ifstat_inc(dstifp, ifs6_reass_ok);
#ifdef RSS
- /*
- * Queue/dispatch for reprocessing.
- */
+ /* Queue/dispatch for reprocessing. */
netisr_dispatch(NETISR_IPV6_DIRECT, m);
- return IPPROTO_DONE;
+ *mp = NULL;
+ return (IPPROTO_DONE);
#endif
- /*
- * Tell launch routine the next header
- */
-
+ /* Tell launch routine the next header. */
*mp = m;
*offp = offset;
- return nxt;
+ return (nxt);
- dropfrag:
- IP6Q_UNLOCK(hash);
+dropfrag:
+ IP6QB_UNLOCK(bucket);
+dropfrag2:
in6_ifstat_inc(dstifp, ifs6_reass_fail);
IP6STAT_INC(ip6s_fragdropped);
m_freem(m);
- return IPPROTO_DONE;
-}
-
-/*
- * Free a fragment reassembly header and all
- * associated datagrams.
- */
-static void
-frag6_freef(struct ip6q *q6, uint32_t bucket)
-{
- struct ip6asfrag *af6, *down6;
-
- IP6Q_LOCK_ASSERT(bucket);
-
- for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
- af6 = down6) {
- struct mbuf *m = IP6_REASS_MBUF(af6);
-
- down6 = af6->ip6af_down;
- frag6_deq(af6, bucket);
-
- /*
- * Return ICMP time exceeded error for the 1st fragment.
- * Just free other fragments.
- */
- if (af6->ip6af_off == 0) {
- struct ip6_hdr *ip6;
-
- /* adjust pointer */
- ip6 = mtod(m, struct ip6_hdr *);
-
- /* restore source and destination addresses */
- ip6->ip6_src = q6->ip6q_src;
- ip6->ip6_dst = q6->ip6q_dst;
-
- icmp6_error(m, ICMP6_TIME_EXCEEDED,
- ICMP6_TIME_EXCEED_REASSEMBLY, 0);
- } else
- m_freem(m);
- free(af6, M_FTABLE);
- }
- frag6_remque(q6, bucket);
- atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
-#ifdef MAC
- mac_ip6q_destroy(q6);
-#endif
- free(q6, M_FTABLE);
- atomic_subtract_int(&V_frag6_nfragpackets, 1);
-}
-
-/*
- * Put an ip fragment on a reassembly chain.
- * Like insque, but pointers in middle of structure.
- */
-static void
-frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6,
- uint32_t bucket __unused)
-{
-
- IP6Q_LOCK_ASSERT(bucket);
-
- af6->ip6af_up = up6;
- af6->ip6af_down = up6->ip6af_down;
- up6->ip6af_down->ip6af_up = af6;
- up6->ip6af_down = af6;
-}
-
-/*
- * To frag6_enq as remque is to insque.
- */
-static void
-frag6_deq(struct ip6asfrag *af6, uint32_t bucket __unused)
-{
-
- IP6Q_LOCK_ASSERT(bucket);
-
- af6->ip6af_up->ip6af_down = af6->ip6af_down;
- af6->ip6af_down->ip6af_up = af6->ip6af_up;
-}
-
-static void
-frag6_insque_head(struct ip6q *new, struct ip6q *old, uint32_t bucket)
-{
-
- IP6Q_LOCK_ASSERT(bucket);
- KASSERT(IP6Q_HEAD(bucket) == old,
- ("%s: attempt to insert at head of wrong bucket"
- " (bucket=%u, old=%p)", __func__, bucket, old));
-
- new->ip6q_prev = old;
- new->ip6q_next = old->ip6q_next;
- old->ip6q_next->ip6q_prev= new;
- old->ip6q_next = new;
- V_ip6q[bucket].count++;
-}
-
-static void
-frag6_remque(struct ip6q *p6, uint32_t bucket)
-{
-
- IP6Q_LOCK_ASSERT(bucket);
-
- p6->ip6q_prev->ip6q_next = p6->ip6q_next;
- p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
- V_ip6q[bucket].count--;
+ *mp = NULL;
+ return (IPPROTO_DONE);
}
/*
* IPv6 reassembling timer processing;
- * if a timer expires on a reassembly
- * queue, discard it.
+ * if a timer expires on a reassembly queue, discard it.
*/
void
frag6_slowtimo(void)
{
VNET_ITERATOR_DECL(vnet_iter);
- struct ip6q *head, *q6;
- int i;
+ struct ip6qhead *head;
+ struct ip6q *q6, *q6tmp;
+ uint32_t bucket;
VNET_LIST_RLOCK_NOSLEEP();
VNET_FOREACH(vnet_iter) {
CURVNET_SET(vnet_iter);
- for (i = 0; i < IP6REASS_NHASH; i++) {
- IP6Q_LOCK(i);
- head = IP6Q_HEAD(i);
- q6 = head->ip6q_next;
- if (q6 == NULL) {
- /*
- * XXXJTL: This should never happen. This
- * should turn into an assertion.
- */
- IP6Q_UNLOCK(i);
- continue;
- }
- while (q6 != head) {
- --q6->ip6q_ttl;
- q6 = q6->ip6q_next;
- if (q6->ip6q_prev->ip6q_ttl == 0) {
- IP6STAT_INC(ip6s_fragtimeout);
+ for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
+ IP6QB_LOCK(bucket);
+ head = IP6QB_HEAD(bucket);
+ TAILQ_FOREACH_SAFE(q6, head, ip6q_tq, q6tmp)
+ if (--q6->ip6q_ttl == 0) {
+ IP6STAT_ADD(ip6s_fragtimeout,
+ q6->ip6q_nfrag);
/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
- frag6_freef(q6->ip6q_prev, i);
+ frag6_freef(q6, bucket);
}
- }
/*
* If we are over the maximum number of fragments
* (due to the limit being lowered), drain off
* enough to get down to the new limit.
* Note that we drain all reassembly queues if
* maxfragpackets is 0 (fragmentation is disabled),
- * and don't enforce a limit when maxfragpackets
+ * and do not enforce a limit when maxfragpackets
* is negative.
*/
while ((V_ip6_maxfragpackets == 0 ||
(V_ip6_maxfragpackets > 0 &&
- V_ip6q[i].count > V_ip6_maxfragbucketsize)) &&
- head->ip6q_prev != head) {
- IP6STAT_INC(ip6s_fragoverflow);
+ V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) &&
+ (q6 = TAILQ_LAST(head, ip6qhead)) != NULL) {
+ IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
- frag6_freef(head->ip6q_prev, i);
+ frag6_freef(q6, bucket);
}
- IP6Q_UNLOCK(i);
+ IP6QB_UNLOCK(bucket);
}
/*
* If we are still over the maximum number of fragmented
* packets, drain off enough to get down to the new limit.
*/
- i = 0;
+ bucket = 0;
while (V_ip6_maxfragpackets >= 0 &&
atomic_load_int(&V_frag6_nfragpackets) >
(u_int)V_ip6_maxfragpackets) {
- IP6Q_LOCK(i);
- head = IP6Q_HEAD(i);
- if (head->ip6q_prev != head) {
- IP6STAT_INC(ip6s_fragoverflow);
+ IP6QB_LOCK(bucket);
+ q6 = TAILQ_LAST(IP6QB_HEAD(bucket), ip6qhead);
+ if (q6 != NULL) {
+ IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
/* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
- frag6_freef(head->ip6q_prev, i);
+ frag6_freef(q6, bucket);
}
- IP6Q_UNLOCK(i);
- i = (i + 1) % IP6REASS_NHASH;
+ IP6QB_UNLOCK(bucket);
+ bucket = (bucket + 1) % IP6REASS_NHASH;
}
CURVNET_RESTORE();
}
@@ -911,55 +959,102 @@ frag6_slowtimo(void)
}
/*
+ * Eventhandler to adjust limits in case nmbclusters change.
+ */
+static void
+frag6_change(void *tag)
+{
+ VNET_ITERATOR_DECL(vnet_iter);
+
+ ip6_maxfrags = IP6_MAXFRAGS;
+ VNET_LIST_RLOCK_NOSLEEP();
+ VNET_FOREACH(vnet_iter) {
+ CURVNET_SET(vnet_iter);
+ V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
+ frag6_set_bucketsize();
+ CURVNET_RESTORE();
+ }
+ VNET_LIST_RUNLOCK_NOSLEEP();
+}
+
+/*
+ * Initialise reassembly queue and fragment identifier.
+ */
+void
+frag6_init(void)
+{
+ uint32_t bucket;
+
+ V_ip6_maxfragpackets = IP6_MAXFRAGPACKETS;
+ frag6_set_bucketsize();
+ for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
+ TAILQ_INIT(IP6QB_HEAD(bucket));
+ mtx_init(&V_ip6qb[bucket].lock, "ip6qb", NULL, MTX_DEF);
+ V_ip6qb[bucket].count = 0;
+ }
+ V_ip6qb_hashseed = arc4random();
+ V_ip6_maxfragsperpacket = 64;
+#ifdef VIMAGE
+ V_frag6_on = true;
+#endif
+ if (!IS_DEFAULT_VNET(curvnet))
+ return;
+
+ ip6_maxfrags = IP6_MAXFRAGS;
+ EVENTHANDLER_REGISTER(nmbclusters_change,
+ frag6_change, NULL, EVENTHANDLER_PRI_ANY);
+}
+
+/*
* Drain off all datagram fragments.
*/
+static void
+frag6_drain_one(void)
+{
+ struct ip6q *q6;
+ uint32_t bucket;
+
+ for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
+ IP6QB_LOCK(bucket);
+ while ((q6 = TAILQ_FIRST(IP6QB_HEAD(bucket))) != NULL) {
+ IP6STAT_INC(ip6s_fragdropped);
+ /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
+ frag6_freef(q6, bucket);
+ }
+ IP6QB_UNLOCK(bucket);
+ }
+}
+
void
frag6_drain(void)
{
VNET_ITERATOR_DECL(vnet_iter);
- struct ip6q *head;
- int i;
VNET_LIST_RLOCK_NOSLEEP();
VNET_FOREACH(vnet_iter) {
CURVNET_SET(vnet_iter);
- for (i = 0; i < IP6REASS_NHASH; i++) {
- if (IP6Q_TRYLOCK(i) == 0)
- continue;
- head = IP6Q_HEAD(i);
- while (head->ip6q_next != head) {
- IP6STAT_INC(ip6s_fragdropped);
- /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
- frag6_freef(head->ip6q_next, i);
- }
- IP6Q_UNLOCK(i);
- }
+ frag6_drain_one();
CURVNET_RESTORE();
}
VNET_LIST_RUNLOCK_NOSLEEP();
}
-int
-ip6_deletefraghdr(struct mbuf *m, int offset, int wait)
+#ifdef VIMAGE
+/*
+ * Clear up IPv6 reassembly structures.
+ */
+void
+frag6_destroy(void)
{
- struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
- struct mbuf *t;
-
- /* Delete frag6 header. */
- if (m->m_len >= offset + sizeof(struct ip6_frag)) {
- /* This is the only possible case with !PULLDOWN_TEST. */
- bcopy(ip6, (char *)ip6 + sizeof(struct ip6_frag),
- offset);
- m->m_data += sizeof(struct ip6_frag);
- m->m_len -= sizeof(struct ip6_frag);
- } else {
- /* This comes with no copy if the boundary is on cluster. */
- if ((t = m_split(m, offset, wait)) == NULL)
- return (ENOMEM);
- m_adj(t, sizeof(struct ip6_frag));
- m_cat(m, t);
+ uint32_t bucket;
+
+ frag6_drain_one();
+ V_frag6_on = false;
+ for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
+ KASSERT(V_ip6qb[bucket].count == 0,
+ ("%s: V_ip6qb[%d] (%p) count not 0 (%d)", __func__,
+ bucket, &V_ip6qb[bucket], V_ip6qb[bucket].count));
+ mtx_destroy(&V_ip6qb[bucket].lock);
}
-
- m->m_flags |= M_FRAGMENTED;
- return (0);
}
+#endif