#include <sys/cdefs.h>
#include "opt_rss.h"
#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/protosw.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_private.h>
#include <net/netisr.h>
#include <net/route.h>
#include <net/vnet.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet/icmp6.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#ifdef MAC
#include <security/mac/mac_framework.h>
#endif
#define IP6REASS_NHASH_LOG2 10
#define IP6REASS_NHASH (1 << IP6REASS_NHASH_LOG2)
#define IP6REASS_HMASK (IP6REASS_NHASH - 1)
TAILQ_HEAD(ip6qhead, ip6q);
struct ip6qbucket {
struct ip6qhead packets;
struct mtx lock;
int count;
};
struct ip6asfrag {
TAILQ_ENTRY(ip6asfrag) ip6af_tq;
struct mbuf *ip6af_m;
int ip6af_offset;
int ip6af_frglen;
int ip6af_off;
bool ip6af_mff;
};
static MALLOC_DEFINE(M_FRAG6, "frag6", "IPv6 fragment reassembly header");
#ifdef VIMAGE
VNET_DEFINE_STATIC(bool, frag6_on);
#define V_frag6_on VNET(frag6_on)
#endif
static int ip6_maxfrags;
static u_int __exclusive_cache_line frag6_nfrags;
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)
VNET_DEFINE_STATIC(u_int, ip6_fraglifetime) = IPV6_DEFFRAGTTL;
#define V_ip6_fraglifetime VNET(ip6_fraglifetime)
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)
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)
#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)
#define IP6_MAXFRAGS (nmbclusters / 32)
#define IP6_MAXFRAGPACKETS (imin(IP6_MAXFRAGS, IP6REASS_NHASH * 50))
#define IP6_CALLOUT_INTERVAL_MS 500
SYSCTL_DECL(_net_inet6_ip6);
SYSCTL_UINT(_net_inet6_ip6, OID_AUTO, frag6_nfrags,
CTLFLAG_RD, &frag6_nfrags, 0,
"Global number of IPv6 fragments across all reassembly queues.");
static void
frag6_set_bucketsize(void)
{
int i;
if ((i = V_ip6_maxfragpackets) > 0)
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 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 | CTLFLAG_NEEDGIANT,
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");
static int
frag6_milli_to_callout_ticks(int ms)
{
return (ms / IP6_CALLOUT_INTERVAL_MS);
}
static int
frag6_callout_ticks_to_milli(int ms)
{
return (ms * IP6_CALLOUT_INTERVAL_MS);
}
_Static_assert(sizeof(((struct ip6q *)NULL)->ip6q_ttl) >= 2,
"ip6q_ttl field is not large enough");
static int
sysctl_ip6_fraglifetime(SYSCTL_HANDLER_ARGS)
{
int error, val;
val = V_ip6_fraglifetime;
error = sysctl_handle_int(oidp, &val, 0, req);
if (error != 0 || !req->newptr)
return (error);
if (val <= 0)
val = IPV6_DEFFRAGTTL;
if (frag6_milli_to_callout_ticks(val) >= 65536)
val = frag6_callout_ticks_to_milli(65535);
#ifdef VIMAGE
if (!IS_DEFAULT_VNET(curvnet)) {
CURVNET_SET(vnet0);
int host_val = V_ip6_fraglifetime;
CURVNET_RESTORE();
if (val > host_val)
val = host_val;
}
#endif
V_ip6_fraglifetime = val;
return (0);
}
SYSCTL_PROC(_net_inet6_ip6, OID_AUTO, fraglifetime_ms,
CTLFLAG_VNET | CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
NULL, 0, sysctl_ip6_fraglifetime, "I",
"Fragment lifetime, in milliseconds");
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)));
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);
}
static void
frag6_rmqueue(struct ip6q *q6, uint32_t bucket)
{
IP6QB_LOCK_ASSERT(bucket);
TAILQ_REMOVE(IP6QB_HEAD(bucket), q6, ip6q_tq);
V_ip6qb[bucket].count--;
#ifdef MAC
mac_ip6q_destroy(q6);
#endif
free(q6, M_FRAG6);
atomic_subtract_int(&V_frag6_nfragpackets, 1);
}
static void
frag6_freef(struct ip6q *q6, uint32_t bucket)
{
struct ip6_hdr *ip6;
struct ip6asfrag *af6;
struct mbuf *m;
IP6QB_LOCK_ASSERT(bucket);
while ((af6 = TAILQ_FIRST(&q6->ip6q_frags)) != NULL) {
m = af6->ip6af_m;
TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
if (af6->ip6af_off == 0 && m->m_pkthdr.rcvif != NULL) {
ip6 = mtod(m, struct ip6_hdr *);
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);
}
atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
frag6_rmqueue(q6, bucket);
}
static void
frag6_cleanup(void *arg __unused, struct ifnet *ifp)
{
struct ip6qhead *head;
struct ip6q *q6;
struct ip6asfrag *af6;
uint32_t bucket;
KASSERT(ifp != NULL, ("%s: ifp is NULL", __func__));
CURVNET_SET_QUIET(ifp->if_vnet);
#ifdef VIMAGE
if (!V_frag6_on) {
CURVNET_RESTORE();
return;
}
#endif
for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
IP6QB_LOCK(bucket);
head = IP6QB_HEAD(bucket);
TAILQ_FOREACH(q6, head, ip6q_tq) {
TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
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);
int
frag6_input(struct mbuf **mp, int *offp, int proto)
{
struct mbuf *m, *t;
struct ip6_hdr *ip6;
struct ip6_frag *ip6f;
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 bucket, *hashkeyp;
int fragoff, frgpartlen;
int nxt, offset, plen;
uint8_t ecn, ecn0;
bool only_frag;
#ifdef RSS
struct ip6_direct_ctx *ip6dc;
struct m_tag *mtag;
#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 *);
dstifp = NULL;
ia6 = in6ifa_ifwithaddr(&ip6->ip6_dst, 0 , false);
if (ia6 != NULL)
dstifp = ia6->ia_ifp;
if (ip6->ip6_plen == 0) {
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
in6_ifstat_inc(dstifp, ifs6_reass_fail);
*mp = NULL;
return (IPPROTO_DONE);
}
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);
*mp = NULL;
return (IPPROTO_DONE);
}
IP6STAT_INC(ip6s_fragments);
in6_ifstat_inc(dstifp, ifs6_reass_reqd);
if ((ip6f->ip6f_offlg & ~IP6F_RESERVED_MASK) == 0) {
IP6STAT_INC(ip6s_atomicfrags);
nxt = ip6f->ip6f_nxt;
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);
*mp = m;
return (nxt);
}
offset += sizeof(struct ip6_frag);
frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
if (frgpartlen == 0) {
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offsetof(struct ip6_hdr, ip6_plen));
in6_ifstat_inc(dstifp, ifs6_reass_fail);
IP6STAT_INC(ip6s_fragdropped);
*mp = NULL;
return (IPPROTO_DONE);
}
if (ip6_maxfrags < 0)
;
else if (atomic_load_int(&frag6_nfrags) >= (u_int)ip6_maxfrags)
goto dropfrag2;
fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
srcifp = m->m_pkthdr.rcvif;
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)
#ifdef MAC
&& mac_ip6q_match(m, q6)
#endif
)
break;
only_frag = false;
if (q6 == NULL) {
only_frag = true;
if (V_ip6_maxfragpackets < 0)
;
else if (V_ip6qb[bucket].count >= V_ip6_maxfragbucketsize ||
atomic_load_int(&V_frag6_nfragpackets) >=
(u_int)V_ip6_maxfragpackets)
goto dropfrag;
q6 = malloc(sizeof(struct ip6q), M_FRAG6, M_NOWAIT | M_ZERO);
if (q6 == NULL)
goto dropfrag;
#ifdef MAC
if (mac_ip6q_init(q6, M_NOWAIT) != 0) {
free(q6, M_FRAG6);
goto dropfrag;
}
mac_ip6q_create(m, q6);
#endif
atomic_add_int(&V_frag6_nfragpackets, 1);
TAILQ_INIT(&q6->ip6q_frags);
q6->ip6q_ident = ip6f->ip6f_ident;
q6->ip6q_ttl = frag6_milli_to_callout_ticks(V_ip6_fraglifetime);
q6->ip6q_src = ip6->ip6_src;
q6->ip6q_dst = ip6->ip6_dst;
q6->ip6q_ecn = IPV6_ECN(ip6);
q6->ip6q_unfrglen = -1;
TAILQ_INSERT_HEAD(head, q6, ip6q_tq);
V_ip6qb[bucket].count++;
}
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;
}
if (q6->ip6q_unfrglen >= 0) {
if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
if (only_frag)
frag6_rmqueue(q6, bucket);
IP6QB_UNLOCK(bucket);
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offset - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
*mp = NULL;
return (IPPROTO_DONE);
}
} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
if (only_frag)
frag6_rmqueue(q6, bucket);
IP6QB_UNLOCK(bucket);
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offset - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
*mp = NULL;
return (IPPROTO_DONE);
}
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;
struct mbuf *merr;
int erroff;
merr = af6->ip6af_m;
erroff = af6->ip6af_offset;
TAILQ_REMOVE(&q6->ip6q_frags, af6, ip6af_tq);
q6->ip6q_nfrag--;
atomic_subtract_int(&frag6_nfrags, 1);
free(af6, M_FRAG6);
merr->m_pkthdr.rcvif = srcifp;
ip6err = mtod(merr, struct ip6_hdr *);
ip6err->ip6_src = q6->ip6q_src;
ip6err->ip6_dst = q6->ip6q_dst;
icmp6_error(merr, ICMP6_PARAM_PROB,
ICMP6_PARAMPROB_HEADER,
erroff - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
}
}
}
ip6af = malloc(sizeof(struct ip6asfrag), M_FRAG6, M_NOWAIT | M_ZERO);
if (ip6af == NULL)
goto dropfrag;
ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) ? true : false;
ip6af->ip6af_off = fragoff;
ip6af->ip6af_frglen = frgpartlen;
ip6af->ip6af_offset = offset;
ip6af->ip6af_m = m;
if (only_frag) {
TAILQ_INSERT_HEAD(&q6->ip6q_frags, ip6af, ip6af_tq);
goto postinsert;
}
ecn = IPV6_ECN(ip6);
ecn0 = q6->ip6q_ecn;
if (ecn == IPTOS_ECN_CE) {
if (ecn0 == IPTOS_ECN_NOTECT) {
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_FRAG6);
goto dropfrag;
}
TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq)
if (af6->ip6af_off > ip6af->ip6af_off)
break;
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 != 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;
}
}
#ifdef MAC
mac_ip6q_update(m, q6);
#endif
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++;
plen = 0;
TAILQ_FOREACH(af6, &q6->ip6q_frags, ip6af_tq) {
if (af6->ip6af_off != plen) {
if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
frag6_freef(q6, bucket);
}
IP6QB_UNLOCK(bucket);
*mp = NULL;
return (IPPROTO_DONE);
}
plen += af6->ip6af_frglen;
}
af6 = TAILQ_LAST(&q6->ip6q_frags, ip6fraghead);
if (af6->ip6af_mff) {
if (q6->ip6q_nfrag > V_ip6_maxfragsperpacket) {
IP6STAT_ADD(ip6s_fragdropped, q6->ip6q_nfrag);
frag6_freef(q6, bucket);
}
IP6QB_UNLOCK(bucket);
*mp = NULL;
return (IPPROTO_DONE);
}
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 &=
af6->ip6af_m->m_pkthdr.csum_flags;
m->m_pkthdr.csum_data +=
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);
offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
free(ip6af, M_FRAG6);
if ((u_int)plen + (u_int)offset - sizeof(struct ip6_hdr) >
IPV6_MAXPACKET) {
frag6_freef(q6, bucket);
goto dropfrag;
}
ip6 = mtod(m, 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;
ip6_deletefraghdr(m, offset, M_NOWAIT);
m_copyback(m, ip6_get_prevhdr(m, offset), sizeof(uint8_t),
(caddr_t)&nxt);
#ifdef MAC
mac_ip6q_reassemble(q6, m);
#endif
atomic_subtract_int(&frag6_nfrags, q6->ip6q_nfrag);
frag6_rmqueue(q6, bucket);
if (m->m_flags & M_PKTHDR) {
plen = 0;
for (t = m; t; t = t->m_next)
plen += t->m_len;
m->m_pkthdr.len = plen;
m->m_pkthdr.rcvif = srcifp;
}
#ifdef RSS
mtag = m_tag_alloc(MTAG_ABI_IPV6, IPV6_TAG_DIRECT, sizeof(*ip6dc),
M_NOWAIT);
if (mtag == NULL)
goto dropfrag;
ip6dc = (struct ip6_direct_ctx *)(mtag + 1);
ip6dc->ip6dc_nxt = nxt;
ip6dc->ip6dc_off = offset;
m_tag_prepend(m, mtag);
#endif
IP6QB_UNLOCK(bucket);
IP6STAT_INC(ip6s_reassembled);
in6_ifstat_inc(dstifp, ifs6_reass_ok);
#ifdef RSS
netisr_dispatch(NETISR_IPV6_DIRECT, m);
*mp = NULL;
return (IPPROTO_DONE);
#endif
*mp = m;
*offp = offset;
return (nxt);
dropfrag:
IP6QB_UNLOCK(bucket);
dropfrag2:
in6_ifstat_inc(dstifp, ifs6_reass_fail);
IP6STAT_INC(ip6s_fragdropped);
m_freem(m);
*mp = NULL;
return (IPPROTO_DONE);
}
static struct callout frag6_callout;
static void
frag6_slowtimo(void *arg __unused)
{
VNET_ITERATOR_DECL(vnet_iter);
struct ip6qhead *head;
struct ip6q *q6, *q6tmp;
uint32_t bucket;
if (atomic_load_int(&frag6_nfrags) == 0)
goto done;
VNET_LIST_RLOCK_NOSLEEP();
VNET_FOREACH(vnet_iter) {
CURVNET_SET(vnet_iter);
for (bucket = 0; bucket < IP6REASS_NHASH; bucket++) {
if (V_ip6qb[bucket].count == 0)
continue;
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);
frag6_freef(q6, bucket);
}
while ((V_ip6_maxfragpackets == 0 ||
(V_ip6_maxfragpackets > 0 &&
V_ip6qb[bucket].count > V_ip6_maxfragbucketsize)) &&
(q6 = TAILQ_LAST(head, ip6qhead)) != NULL) {
IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
frag6_freef(q6, bucket);
}
IP6QB_UNLOCK(bucket);
}
bucket = 0;
while (V_ip6_maxfragpackets >= 0 &&
atomic_load_int(&V_frag6_nfragpackets) >
(u_int)V_ip6_maxfragpackets) {
IP6QB_LOCK(bucket);
q6 = TAILQ_LAST(IP6QB_HEAD(bucket), ip6qhead);
if (q6 != NULL) {
IP6STAT_ADD(ip6s_fragoverflow, q6->ip6q_nfrag);
frag6_freef(q6, bucket);
}
IP6QB_UNLOCK(bucket);
bucket = (bucket + 1) % IP6REASS_NHASH;
}
CURVNET_RESTORE();
}
VNET_LIST_RUNLOCK_NOSLEEP();
done:
callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS,
SBT_1MS * 10, frag6_slowtimo, NULL, 0);
}
static void
frag6_slowtimo_init(void *arg __unused)
{
callout_init(&frag6_callout, 1);
callout_reset_sbt(&frag6_callout, SBT_1MS * IP6_CALLOUT_INTERVAL_MS,
SBT_1MS * 10, frag6_slowtimo, NULL, 0);
}
SYSINIT(frag6, SI_SUB_VNET_DONE, SI_ORDER_ANY, frag6_slowtimo_init, NULL);
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();
}
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);
}
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);
frag6_freef(q6, bucket);
}
IP6QB_UNLOCK(bucket);
}
}
void
frag6_drain(void *arg __unused, int flags __unused)
{
VNET_ITERATOR_DECL(vnet_iter);
VNET_LIST_RLOCK_NOSLEEP();
VNET_FOREACH(vnet_iter) {
CURVNET_SET(vnet_iter);
frag6_drain_one();
CURVNET_RESTORE();
}
VNET_LIST_RUNLOCK_NOSLEEP();
}
#ifdef VIMAGE
void
frag6_destroy(void)
{
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);
}
}
#endif