#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.78 2024/04/19 05:04:06 ozaki-r Exp $");
#ifdef _KERNEL_OPT
#include "opt_net_mpsafe.h"
#endif
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/errno.h>
#include <sys/time.h>
#include <sys/kmem.h>
#include <sys/kernel.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/ip6_private.h>
#include <netinet/icmp6.h>
struct ip6q {
u_int32_t ip6q_head;
u_int16_t ip6q_len;
u_int8_t ip6q_nxt;
u_int8_t ip6q_hlim;
struct ip6asfrag *ip6q_down;
struct ip6asfrag *ip6q_up;
u_int32_t ip6q_ident;
u_int8_t ip6q_ttl;
struct in6_addr ip6q_src, ip6q_dst;
struct ip6q *ip6q_next;
struct ip6q *ip6q_prev;
int ip6q_unfrglen;
int ip6q_nfrag;
int ip6q_ipsec;
};
struct ip6asfrag {
u_int32_t ip6af_head;
u_int16_t ip6af_len;
u_int8_t ip6af_nxt;
u_int8_t ip6af_hlim;
struct ip6asfrag *ip6af_down;
struct ip6asfrag *ip6af_up;
struct mbuf *ip6af_m;
int ip6af_offset;
int ip6af_frglen;
int ip6af_off;
bool ip6af_mff;
};
static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
static void frag6_deq(struct ip6asfrag *);
static void frag6_insque(struct ip6q *, struct ip6q *);
static void frag6_remque(struct ip6q *);
static void frag6_freef(struct ip6q *);
static int frag6_drainwanted;
static u_int frag6_nfragpackets;
static u_int frag6_nfrags;
static struct ip6q ip6q;
static kmutex_t frag6_lock __cacheline_aligned;
void
frag6_init(void)
{
ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
mutex_init(&frag6_lock, MUTEX_DEFAULT, IPL_NONE);
}
static void
frag6_dropfrag(struct ip6q *q6)
{
frag6_remque(q6);
frag6_nfrags -= q6->ip6q_nfrag;
kmem_intr_free(q6, sizeof(*q6));
frag6_nfragpackets--;
}
int
frag6_input(struct mbuf **mp, int *offp, int proto)
{
struct rtentry *rt;
struct mbuf *m = *mp, *t;
struct ip6_hdr *ip6;
struct ip6_frag *ip6f;
struct ip6q *q6;
struct ip6asfrag *af6, *ip6af, *af6dwn;
int offset = *offp, nxt, i, next;
int ipsecflags = m->m_flags & (M_DECRYPTED|M_AUTHIPHDR);
int first_frag = 0;
int fragoff, frgpartlen;
struct ifnet *dstifp;
static struct route ro;
union {
struct sockaddr dst;
struct sockaddr_in6 dst6;
} u;
ip6 = mtod(m, struct ip6_hdr *);
IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
if (ip6f == NULL)
return IPPROTO_DONE;
dstifp = NULL;
sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
if ((rt = rtcache_lookup(&ro, &u.dst)) != NULL)
dstifp = ((struct in6_ifaddr *)rt->rt_ifa)->ia_ifp;
if (ip6->ip6_plen == 0) {
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
in6_ifstat_inc(dstifp, ifs6_reass_fail);
goto done;
}
frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset
- sizeof(struct ip6_frag);
if ((frgpartlen == 0) ||
((ip6f->ip6f_offlg & IP6F_MORE_FRAG) && (frgpartlen & 0x7) != 0)) {
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offsetof(struct ip6_hdr, ip6_plen));
in6_ifstat_inc(dstifp, ifs6_reass_fail);
goto done;
}
IP6_STATINC(IP6_STAT_FRAGMENTS);
in6_ifstat_inc(dstifp, ifs6_reass_reqd);
offset += sizeof(struct ip6_frag);
fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
if (fragoff == 0 && !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
IP6_STATINC(IP6_STAT_REASSEMBLED);
in6_ifstat_inc(dstifp, ifs6_reass_ok);
*offp = offset;
rtcache_unref(rt, &ro);
return ip6f->ip6f_nxt;
}
mutex_enter(&frag6_lock);
if (ip6_maxfrags < 0)
;
else if (frag6_nfrags >= (u_int)ip6_maxfrags)
goto dropfrag;
for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
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))
break;
if (q6 != &ip6q) {
if (q6->ip6q_ipsec != ipsecflags) {
goto dropfrag;
}
}
if (q6 == &ip6q) {
first_frag = 1;
if (ip6_maxfragpackets < 0)
;
else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
goto dropfrag;
frag6_nfragpackets++;
q6 = kmem_intr_zalloc(sizeof(struct ip6q), KM_NOSLEEP);
if (q6 == NULL) {
goto dropfrag;
}
frag6_insque(q6, &ip6q);
q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
q6->ip6q_ident = ip6f->ip6f_ident;
q6->ip6q_ttl = IPV6_FRAGTTL;
q6->ip6q_src = ip6->ip6_src;
q6->ip6q_dst = ip6->ip6_dst;
q6->ip6q_unfrglen = -1;
q6->ip6q_nfrag = 0;
q6->ip6q_ipsec = ipsecflags;
}
if (fragoff == 0) {
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) {
mutex_exit(&frag6_lock);
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offset - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
goto done;
}
} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
mutex_exit(&frag6_lock);
icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offset - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
goto done;
}
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 = af6->ip6af_m;
struct ip6_hdr *ip6err;
int erroff = af6->ip6af_offset;
frag6_deq(af6);
kmem_intr_free(af6, sizeof(struct ip6asfrag));
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 = kmem_intr_zalloc(sizeof(struct ip6asfrag), KM_NOSLEEP);
if (ip6af == NULL) {
goto dropfrag;
}
ip6af->ip6af_head = ip6->ip6_flow;
ip6af->ip6af_len = ip6->ip6_plen;
ip6af->ip6af_nxt = ip6->ip6_nxt;
ip6af->ip6af_hlim = ip6->ip6_hlim;
ip6af->ip6af_mff = (ip6f->ip6f_offlg & IP6F_MORE_FRAG) != 0;
ip6af->ip6af_off = fragoff;
ip6af->ip6af_frglen = frgpartlen;
ip6af->ip6af_offset = offset;
ip6af->ip6af_m = m;
if (first_frag) {
af6 = (struct ip6asfrag *)q6;
goto insert;
}
for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
af6 = af6->ip6af_down)
if (af6->ip6af_off > ip6af->ip6af_off)
break;
if (af6->ip6af_up != (struct ip6asfrag *)q6) {
i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
- ip6af->ip6af_off;
if (i > 0) {
kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
goto dropfrag;
}
}
if (af6 != (struct ip6asfrag *)q6) {
i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
if (i > 0) {
kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
goto dropfrag;
}
}
insert:
frag6_enq(ip6af, af6->ip6af_up);
frag6_nfrags++;
q6->ip6q_nfrag++;
next = 0;
for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
af6 = af6->ip6af_down) {
if (af6->ip6af_off != next) {
mutex_exit(&frag6_lock);
goto done;
}
next += af6->ip6af_frglen;
}
if (af6->ip6af_up->ip6af_mff) {
mutex_exit(&frag6_lock);
goto done;
}
ip6af = q6->ip6q_down;
t = m = ip6af->ip6af_m;
af6 = ip6af->ip6af_down;
frag6_deq(ip6af);
while (af6 != (struct ip6asfrag *)q6) {
af6dwn = af6->ip6af_down;
frag6_deq(af6);
while (t->m_next)
t = t->m_next;
t->m_next = af6->ip6af_m;
m_adj(t->m_next, af6->ip6af_offset);
m_remove_pkthdr(t->m_next);
kmem_intr_free(af6, sizeof(struct ip6asfrag));
af6 = af6dwn;
}
offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
next += offset - sizeof(struct ip6_hdr);
if ((u_int)next > IPV6_MAXPACKET) {
frag6_dropfrag(q6);
goto dropfrag;
}
ip6 = mtod(m, struct ip6_hdr *);
ip6->ip6_plen = htons(next);
ip6->ip6_src = q6->ip6q_src;
ip6->ip6_dst = q6->ip6q_dst;
nxt = q6->ip6q_nxt;
if (m->m_len >= offset + sizeof(struct ip6_frag)) {
memmove((char *)ip6 + sizeof(struct ip6_frag), ip6, offset);
m->m_data += sizeof(struct ip6_frag);
m->m_len -= sizeof(struct ip6_frag);
} else {
if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
frag6_dropfrag(q6);
goto dropfrag;
}
m_adj(t, sizeof(struct ip6_frag));
m_cat(m, t);
}
frag6_dropfrag(q6);
{
KASSERT(m->m_flags & M_PKTHDR);
int plen = 0;
for (t = m; t; t = t->m_next) {
plen += t->m_len;
}
m->m_pkthdr.len = plen;
}
{
const int prvnxt = ip6_get_prevhdr(m, offset);
uint8_t *prvnxtp;
IP6_EXTHDR_GET(prvnxtp, uint8_t *, m, prvnxt,
sizeof(*prvnxtp));
if (prvnxtp == NULL) {
goto dropfrag;
}
*prvnxtp = nxt;
}
IP6_STATINC(IP6_STAT_REASSEMBLED);
in6_ifstat_inc(dstifp, ifs6_reass_ok);
rtcache_unref(rt, &ro);
mutex_exit(&frag6_lock);
*mp = m;
*offp = offset;
return nxt;
dropfrag:
mutex_exit(&frag6_lock);
in6_ifstat_inc(dstifp, ifs6_reass_fail);
IP6_STATINC(IP6_STAT_FRAGDROPPED);
m_freem(m);
done:
rtcache_unref(rt, &ro);
return IPPROTO_DONE;
}
int
ip6_reass_packet(struct mbuf **mp, int offset)
{
if (frag6_input(mp, &offset, IPPROTO_IPV6) == IPPROTO_DONE) {
*mp = NULL;
return EINVAL;
}
return 0;
}
static void
frag6_freef(struct ip6q *q6)
{
struct ip6asfrag *af6, *down6;
KASSERT(mutex_owned(&frag6_lock));
for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
af6 = down6) {
struct mbuf *m = af6->ip6af_m;
down6 = af6->ip6af_down;
frag6_deq(af6);
if (af6->ip6af_off == 0) {
struct ip6_hdr *ip6;
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);
}
kmem_intr_free(af6, sizeof(struct ip6asfrag));
}
frag6_dropfrag(q6);
}
void
frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
{
KASSERT(mutex_owned(&frag6_lock));
af6->ip6af_up = up6;
af6->ip6af_down = up6->ip6af_down;
up6->ip6af_down->ip6af_up = af6;
up6->ip6af_down = af6;
}
void
frag6_deq(struct ip6asfrag *af6)
{
KASSERT(mutex_owned(&frag6_lock));
af6->ip6af_up->ip6af_down = af6->ip6af_down;
af6->ip6af_down->ip6af_up = af6->ip6af_up;
}
void
frag6_insque(struct ip6q *newq, struct ip6q *oldq)
{
KASSERT(mutex_owned(&frag6_lock));
newq->ip6q_prev = oldq;
newq->ip6q_next = oldq->ip6q_next;
oldq->ip6q_next->ip6q_prev = newq;
oldq->ip6q_next = newq;
}
void
frag6_remque(struct ip6q *p6)
{
KASSERT(mutex_owned(&frag6_lock));
p6->ip6q_prev->ip6q_next = p6->ip6q_next;
p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
}
void
frag6_fasttimo(void)
{
SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
if (frag6_drainwanted) {
frag6_drain();
frag6_drainwanted = 0;
}
SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
}
void
frag6_slowtimo(void)
{
struct ip6q *q6;
SOFTNET_KERNEL_LOCK_UNLESS_NET_MPSAFE();
mutex_enter(&frag6_lock);
q6 = ip6q.ip6q_next;
if (q6) {
while (q6 != &ip6q) {
--q6->ip6q_ttl;
q6 = q6->ip6q_next;
if (q6->ip6q_prev->ip6q_ttl == 0) {
IP6_STATINC(IP6_STAT_FRAGTIMEOUT);
frag6_freef(q6->ip6q_prev);
}
}
}
while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
ip6q.ip6q_prev) {
IP6_STATINC(IP6_STAT_FRAGOVERFLOW);
frag6_freef(ip6q.ip6q_prev);
}
mutex_exit(&frag6_lock);
SOFTNET_KERNEL_UNLOCK_UNLESS_NET_MPSAFE();
#if 0
rtcache_free(&ip6_forward_rt);
rtcache_free(&ipsrcchk_rt);
#endif
}
void
frag6_drainstub(void)
{
frag6_drainwanted = 1;
}
void
frag6_drain(void)
{
if (mutex_tryenter(&frag6_lock)) {
while (ip6q.ip6q_next != &ip6q) {
IP6_STATINC(IP6_STAT_FRAGDROPPED);
frag6_freef(ip6q.ip6q_next);
}
mutex_exit(&frag6_lock);
}
}