#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/errno.h>
#include <sys/pool.h>
#include <sys/mutex.h>
#include <net/if.h>
#include <net/if_var.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet/icmp6.h>
#include <netinet/ip.h>
struct mutex frag6_mutex = MUTEX_INITIALIZER(IPL_SOFTNET);
u_int frag6_nfragpackets;
u_int frag6_nfrags;
TAILQ_HEAD(ip6q_head, ip6q) frag6_queue;
void frag6_freef(struct ip6q *);
void frag6_unlink(struct ip6q *, struct ip6q_head *);
struct pool ip6af_pool;
struct pool ip6q_pool;
void
frag6_init(void)
{
pool_init(&ip6af_pool, sizeof(struct ip6asfrag),
0, IPL_SOFTNET, 0, "ip6af", NULL);
pool_init(&ip6q_pool, sizeof(struct ip6q),
0, IPL_SOFTNET, 0, "ip6q", NULL);
TAILQ_INIT(&frag6_queue);
}
int
frag6_input(struct mbuf **mp, int *offp, int proto, int af,
struct netstack *ns)
{
struct mbuf *t;
struct ip6_hdr *ip6;
struct ip6_frag *ip6f;
struct ip6q *q6;
struct ip6asfrag *af6, *ip6af, *naf6, *paf6;
int offset = *offp, nxt, i, next;
int first_frag = 0;
int fragoff, frgpartlen;
u_int8_t ecn, ecn0;
ip6 = mtod(*mp, struct ip6_hdr *);
ip6f = ip6_exthdr_get(mp, offset, sizeof(*ip6f));
if (ip6f == NULL)
return IPPROTO_DONE;
if (ip6->ip6_plen == 0) {
icmp6_error(*mp, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offset);
return IPPROTO_DONE;
}
if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
(((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
icmp6_error(*mp, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offsetof(struct ip6_hdr, ip6_plen));
return IPPROTO_DONE;
}
ip6stat_inc(ip6s_fragments);
offset += sizeof(struct ip6_frag);
fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
if (fragoff == 0 && !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
ip6stat_inc(ip6s_reassembled);
*offp = offset;
return ip6f->ip6f_nxt;
}
if (sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) <= offset) {
m_freemp(mp);
return IPPROTO_DONE;
}
mtx_enter(&frag6_mutex);
if (frag6_nfrags >= atomic_load_int(&ip6_maxfrags)) {
mtx_leave(&frag6_mutex);
goto dropfrag;
}
TAILQ_FOREACH(q6, &frag6_queue, ip6q_queue)
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 == NULL) {
first_frag = 1;
if (frag6_nfragpackets >=
atomic_load_int(&ip6_maxfragpackets)) {
mtx_leave(&frag6_mutex);
goto dropfrag;
}
frag6_nfragpackets++;
q6 = pool_get(&ip6q_pool, PR_NOWAIT | PR_ZERO);
if (q6 == NULL) {
mtx_leave(&frag6_mutex);
goto dropfrag;
}
TAILQ_INSERT_HEAD(&frag6_queue, q6, ip6q_queue);
LIST_INIT(&q6->ip6q_asfrag);
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_ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
q6->ip6q_unfrglen = -1;
q6->ip6q_nfrag = 0;
}
if (fragoff == 0) {
q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
sizeof(struct ip6_frag);
q6->ip6q_nxt = ip6f->ip6f_nxt;
}
frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
if (q6->ip6q_unfrglen >= 0) {
if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
mtx_leave(&frag6_mutex);
icmp6_error(*mp, ICMP6_PARAM_PROB,
ICMP6_PARAMPROB_HEADER,
offset - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
return (IPPROTO_DONE);
}
} else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
mtx_leave(&frag6_mutex);
icmp6_error(*mp, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
offset - sizeof(struct ip6_frag) +
offsetof(struct ip6_frag, ip6f_offlg));
return (IPPROTO_DONE);
}
if (fragoff == 0) {
LIST_FOREACH_SAFE(af6, &q6->ip6q_asfrag, ip6af_list, naf6) {
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;
LIST_REMOVE(af6, ip6af_list);
pool_put(&ip6af_pool, af6);
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 = pool_get(&ip6af_pool, PR_NOWAIT | PR_ZERO);
if (ip6af == NULL) {
mtx_leave(&frag6_mutex);
goto dropfrag;
}
ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
ip6af->ip6af_off = fragoff;
ip6af->ip6af_frglen = frgpartlen;
ip6af->ip6af_offset = offset;
ip6af->ip6af_m = *mp;
if (first_frag) {
paf6 = NULL;
goto insert;
}
ecn = (ntohl(ip6->ip6_flow) >> 20) & IPTOS_ECN_MASK;
ecn0 = q6->ip6q_ecn;
if (ecn == IPTOS_ECN_CE) {
if (ecn0 == IPTOS_ECN_NOTECT) {
mtx_leave(&frag6_mutex);
pool_put(&ip6af_pool, ip6af);
goto dropfrag;
}
if (ecn0 != IPTOS_ECN_CE)
q6->ip6q_ecn = IPTOS_ECN_CE;
}
if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) {
mtx_leave(&frag6_mutex);
pool_put(&ip6af_pool, ip6af);
goto dropfrag;
}
for (paf6 = NULL, af6 = LIST_FIRST(&q6->ip6q_asfrag);
af6 != NULL;
paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list))
if (af6->ip6af_off > ip6af->ip6af_off)
break;
if (paf6 != NULL) {
i = (paf6->ip6af_off + paf6->ip6af_frglen) - ip6af->ip6af_off;
if (i > 0)
goto flushfrags;
}
if (af6 != NULL) {
i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
if (i > 0)
goto flushfrags;
}
insert:
if (paf6 != NULL)
LIST_INSERT_AFTER(paf6, ip6af, ip6af_list);
else
LIST_INSERT_HEAD(&q6->ip6q_asfrag, ip6af, ip6af_list);
frag6_nfrags++;
q6->ip6q_nfrag++;
next = 0;
for (paf6 = NULL, af6 = LIST_FIRST(&q6->ip6q_asfrag);
af6 != NULL;
paf6 = af6, af6 = LIST_NEXT(af6, ip6af_list)) {
if (af6->ip6af_off != next) {
mtx_leave(&frag6_mutex);
return IPPROTO_DONE;
}
next += af6->ip6af_frglen;
}
if (paf6->ip6af_mff) {
mtx_leave(&frag6_mutex);
return IPPROTO_DONE;
}
ip6af = LIST_FIRST(&q6->ip6q_asfrag);
LIST_REMOVE(ip6af, ip6af_list);
t = *mp = ip6af->ip6af_m;
while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
LIST_REMOVE(af6, ip6af_list);
while (t->m_next)
t = t->m_next;
t->m_next = af6->ip6af_m;
m_adj(t->m_next, af6->ip6af_offset);
m_removehdr(t->m_next);
pool_put(&ip6af_pool, af6);
}
offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
pool_put(&ip6af_pool, ip6af);
next += offset - sizeof(struct ip6_hdr);
if ((u_int)next > IPV6_MAXPACKET) {
TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
frag6_nfrags -= q6->ip6q_nfrag;
frag6_nfragpackets--;
mtx_leave(&frag6_mutex);
pool_put(&ip6q_pool, q6);
goto dropfrag;
}
ip6 = mtod(*mp, struct ip6_hdr *);
ip6->ip6_plen = htons(next);
ip6->ip6_src = q6->ip6q_src;
ip6->ip6_dst = q6->ip6q_dst;
if (q6->ip6q_ecn == IPTOS_ECN_CE)
ip6->ip6_flow |= htonl(IPTOS_ECN_CE << 20);
nxt = q6->ip6q_nxt;
if (frag6_deletefraghdr(*mp, offset) != 0) {
TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
frag6_nfrags -= q6->ip6q_nfrag;
frag6_nfragpackets--;
mtx_leave(&frag6_mutex);
pool_put(&ip6q_pool, q6);
goto dropfrag;
}
TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
frag6_nfrags -= q6->ip6q_nfrag;
frag6_nfragpackets--;
mtx_leave(&frag6_mutex);
pool_put(&ip6q_pool, q6);
m_calchdrlen(*mp);
{
int prvnxt = ip6_get_prevhdr(*mp, offset);
uint8_t *prvnxtp;
prvnxtp = ip6_exthdr_get(mp, prvnxt, sizeof(*prvnxtp));
if (prvnxtp == NULL)
goto dropfrag;
*prvnxtp = nxt;
}
ip6stat_inc(ip6s_reassembled);
*offp = offset;
return nxt;
flushfrags:
TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
frag6_nfrags -= q6->ip6q_nfrag;
frag6_nfragpackets--;
mtx_leave(&frag6_mutex);
pool_put(&ip6af_pool, ip6af);
while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
LIST_REMOVE(af6, ip6af_list);
m_freem(af6->ip6af_m);
pool_put(&ip6af_pool, af6);
}
ip6stat_add(ip6s_fragdropped, q6->ip6q_nfrag + 1);
pool_put(&ip6q_pool, q6);
m_freemp(mp);
return IPPROTO_DONE;
dropfrag:
ip6stat_inc(ip6s_fragdropped);
m_freemp(mp);
return IPPROTO_DONE;
}
int
frag6_deletefraghdr(struct mbuf *m, int offset)
{
struct mbuf *t;
if (m->m_len >= offset + sizeof(struct ip6_frag)) {
memmove(mtod(m, caddr_t) + sizeof(struct ip6_frag),
mtod(m, caddr_t), 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)
return (ENOBUFS);
m_adj(t, sizeof(struct ip6_frag));
m_cat(m, t);
}
return (0);
}
void
frag6_freef(struct ip6q *q6)
{
struct ip6asfrag *af6;
while ((af6 = LIST_FIRST(&q6->ip6q_asfrag)) != NULL) {
struct mbuf *m = af6->ip6af_m;
LIST_REMOVE(af6, ip6af_list);
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;
NET_LOCK_SHARED();
icmp6_error(m, ICMP6_TIME_EXCEEDED,
ICMP6_TIME_EXCEED_REASSEMBLY, 0);
NET_UNLOCK_SHARED();
} else
m_freem(m);
pool_put(&ip6af_pool, af6);
}
pool_put(&ip6q_pool, q6);
}
void
frag6_unlink(struct ip6q *q6, struct ip6q_head *rmq6)
{
MUTEX_ASSERT_LOCKED(&frag6_mutex);
TAILQ_REMOVE(&frag6_queue, q6, ip6q_queue);
TAILQ_INSERT_HEAD(rmq6, q6, ip6q_queue);
frag6_nfrags -= q6->ip6q_nfrag;
frag6_nfragpackets--;
}
void
frag6_slowtimo(void)
{
struct ip6q_head rmq6;
struct ip6q *q6, *nq6;
u_int ip6_maxfragpackets_local = atomic_load_int(&ip6_maxfragpackets);
TAILQ_INIT(&rmq6);
mtx_enter(&frag6_mutex);
TAILQ_FOREACH_SAFE(q6, &frag6_queue, ip6q_queue, nq6) {
if (--q6->ip6q_ttl == 0) {
ip6stat_inc(ip6s_fragtimeout);
frag6_unlink(q6, &rmq6);
}
}
while (frag6_nfragpackets > ip6_maxfragpackets_local &&
!TAILQ_EMPTY(&frag6_queue)) {
ip6stat_inc(ip6s_fragoverflow);
frag6_unlink(TAILQ_LAST(&frag6_queue, ip6q_head), &rmq6);
}
mtx_leave(&frag6_mutex);
while ((q6 = TAILQ_FIRST(&rmq6)) != NULL) {
TAILQ_REMOVE(&rmq6, q6, ip6q_queue);
frag6_freef(q6);
}
}