2014-02-28 14:32:50 +08:00
|
|
|
/* 6LoWPAN fragment reassembly
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alexander Aring <aar@pengutronix.de>
|
|
|
|
*
|
|
|
|
* Based on: net/ipv6/reassembly.c
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) "6LoWPAN: " fmt
|
|
|
|
|
|
|
|
#include <linux/net.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/netdevice.h>
|
|
|
|
#include <linux/random.h>
|
|
|
|
#include <linux/jhash.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/export.h>
|
|
|
|
|
|
|
|
#include <net/ieee802154_netdev.h>
|
2014-03-05 21:29:05 +08:00
|
|
|
#include <net/6lowpan.h>
|
2018-07-14 07:14:01 +08:00
|
|
|
#include <net/ipv6_frag.h>
|
2014-02-28 14:32:50 +08:00
|
|
|
#include <net/inet_frag.h>
|
2019-02-15 10:29:53 +08:00
|
|
|
#include <net/ip.h>
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2015-01-05 00:10:54 +08:00
|
|
|
#include "6lowpan_i.h"
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2014-08-01 18:29:48 +08:00
|
|
|
static const char lowpan_frags_cache_name[] = "lowpan-frags";
|
|
|
|
|
2014-02-28 14:32:50 +08:00
|
|
|
static struct inet_frags lowpan_frags;
|
|
|
|
|
2019-02-15 10:29:53 +08:00
|
|
|
static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *skb,
|
|
|
|
struct sk_buff *prev, struct net_device *ldev);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2014-07-24 22:50:29 +08:00
|
|
|
static void lowpan_frag_init(struct inet_frag_queue *q, const void *a)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
inet: frags: use rhashtables for reassembly units
Some applications still rely on IP fragmentation, and to be fair linux
reassembly unit is not working under any serious load.
It uses static hash tables of 1024 buckets, and up to 128 items per bucket (!!!)
A work queue is supposed to garbage collect items when host is under memory
pressure, and doing a hash rebuild, changing seed used in hash computations.
This work queue blocks softirqs for up to 25 ms when doing a hash rebuild,
occurring every 5 seconds if host is under fire.
Then there is the problem of sharing this hash table for all netns.
It is time to switch to rhashtables, and allocate one of them per netns
to speedup netns dismantle, since this is a critical metric these days.
Lookup is now using RCU. A followup patch will even remove
the refcount hold/release left from prior implementation and save
a couple of atomic operations.
Before this patch, 16 cpus (16 RX queue NIC) could not handle more
than 1 Mpps frags DDOS.
After the patch, I reach 9 Mpps without any tuning, and can use up to 2GB
of storage for the fragments (exact number depends on frags being evicted
after timeout)
$ grep FRAG /proc/net/sockstat
FRAG: inuse 1966916 memory 2140004608
A followup patch will change the limits for 64bit arches.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Florian Westphal <fw@strlen.de>
Cc: Jesper Dangaard Brouer <brouer@redhat.com>
Cc: Alexander Aring <alex.aring@gmail.com>
Cc: Stefan Schmidt <stefan@osg.samsung.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-04-01 03:58:49 +08:00
|
|
|
const struct frag_lowpan_compare_key *key = a;
|
2014-02-28 14:32:50 +08:00
|
|
|
|
inet: frags: use rhashtables for reassembly units
Some applications still rely on IP fragmentation, and to be fair linux
reassembly unit is not working under any serious load.
It uses static hash tables of 1024 buckets, and up to 128 items per bucket (!!!)
A work queue is supposed to garbage collect items when host is under memory
pressure, and doing a hash rebuild, changing seed used in hash computations.
This work queue blocks softirqs for up to 25 ms when doing a hash rebuild,
occurring every 5 seconds if host is under fire.
Then there is the problem of sharing this hash table for all netns.
It is time to switch to rhashtables, and allocate one of them per netns
to speedup netns dismantle, since this is a critical metric these days.
Lookup is now using RCU. A followup patch will even remove
the refcount hold/release left from prior implementation and save
a couple of atomic operations.
Before this patch, 16 cpus (16 RX queue NIC) could not handle more
than 1 Mpps frags DDOS.
After the patch, I reach 9 Mpps without any tuning, and can use up to 2GB
of storage for the fragments (exact number depends on frags being evicted
after timeout)
$ grep FRAG /proc/net/sockstat
FRAG: inuse 1966916 memory 2140004608
A followup patch will change the limits for 64bit arches.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Florian Westphal <fw@strlen.de>
Cc: Jesper Dangaard Brouer <brouer@redhat.com>
Cc: Alexander Aring <alex.aring@gmail.com>
Cc: Stefan Schmidt <stefan@osg.samsung.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-04-01 03:58:49 +08:00
|
|
|
BUILD_BUG_ON(sizeof(*key) > sizeof(q->key));
|
|
|
|
memcpy(&q->key, key, sizeof(*key));
|
2014-02-28 14:32:50 +08:00
|
|
|
}
|
|
|
|
|
2017-10-17 08:29:20 +08:00
|
|
|
static void lowpan_frag_expire(struct timer_list *t)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
2017-10-17 08:29:20 +08:00
|
|
|
struct inet_frag_queue *frag = from_timer(frag, t, timer);
|
2014-02-28 14:32:50 +08:00
|
|
|
struct frag_queue *fq;
|
|
|
|
|
2017-10-17 08:29:20 +08:00
|
|
|
fq = container_of(frag, struct frag_queue, q);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2014-03-14 03:58:03 +08:00
|
|
|
spin_lock(&fq->q.lock);
|
|
|
|
|
2014-08-01 18:29:44 +08:00
|
|
|
if (fq->q.flags & INET_FRAG_COMPLETE)
|
2014-03-14 03:58:03 +08:00
|
|
|
goto out;
|
|
|
|
|
2018-04-01 03:58:44 +08:00
|
|
|
inet_frag_kill(&fq->q);
|
2014-03-14 03:58:03 +08:00
|
|
|
out:
|
|
|
|
spin_unlock(&fq->q.lock);
|
2018-04-01 03:58:44 +08:00
|
|
|
inet_frag_put(&fq->q);
|
2014-02-28 14:32:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct lowpan_frag_queue *
|
2015-09-02 20:21:25 +08:00
|
|
|
fq_find(struct net *net, const struct lowpan_802154_cb *cb,
|
2014-03-15 04:24:02 +08:00
|
|
|
const struct ieee802154_addr *src,
|
|
|
|
const struct ieee802154_addr *dst)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
2014-04-18 09:22:54 +08:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
2018-04-21 02:54:13 +08:00
|
|
|
struct frag_lowpan_compare_key key = {};
|
inet: frags: use rhashtables for reassembly units
Some applications still rely on IP fragmentation, and to be fair linux
reassembly unit is not working under any serious load.
It uses static hash tables of 1024 buckets, and up to 128 items per bucket (!!!)
A work queue is supposed to garbage collect items when host is under memory
pressure, and doing a hash rebuild, changing seed used in hash computations.
This work queue blocks softirqs for up to 25 ms when doing a hash rebuild,
occurring every 5 seconds if host is under fire.
Then there is the problem of sharing this hash table for all netns.
It is time to switch to rhashtables, and allocate one of them per netns
to speedup netns dismantle, since this is a critical metric these days.
Lookup is now using RCU. A followup patch will even remove
the refcount hold/release left from prior implementation and save
a couple of atomic operations.
Before this patch, 16 cpus (16 RX queue NIC) could not handle more
than 1 Mpps frags DDOS.
After the patch, I reach 9 Mpps without any tuning, and can use up to 2GB
of storage for the fragments (exact number depends on frags being evicted
after timeout)
$ grep FRAG /proc/net/sockstat
FRAG: inuse 1966916 memory 2140004608
A followup patch will change the limits for 64bit arches.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Florian Westphal <fw@strlen.de>
Cc: Jesper Dangaard Brouer <brouer@redhat.com>
Cc: Alexander Aring <alex.aring@gmail.com>
Cc: Stefan Schmidt <stefan@osg.samsung.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-04-01 03:58:49 +08:00
|
|
|
struct inet_frag_queue *q;
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2018-04-21 02:54:13 +08:00
|
|
|
key.tag = cb->d_tag;
|
|
|
|
key.d_size = cb->d_size;
|
|
|
|
key.src = *src;
|
|
|
|
key.dst = *dst;
|
|
|
|
|
2019-05-25 00:03:32 +08:00
|
|
|
q = inet_frag_find(&ieee802154_lowpan->fqdir, &key);
|
2018-04-01 03:58:52 +08:00
|
|
|
if (!q)
|
2014-02-28 14:32:50 +08:00
|
|
|
return NULL;
|
2018-04-01 03:58:52 +08:00
|
|
|
|
2014-02-28 14:32:50 +08:00
|
|
|
return container_of(q, struct lowpan_frag_queue, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lowpan_frag_queue(struct lowpan_frag_queue *fq,
|
2015-09-02 20:21:25 +08:00
|
|
|
struct sk_buff *skb, u8 frag_type)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
2019-02-15 10:29:53 +08:00
|
|
|
struct sk_buff *prev_tail;
|
2015-09-02 20:21:16 +08:00
|
|
|
struct net_device *ldev;
|
2019-02-15 10:29:53 +08:00
|
|
|
int end, offset, err;
|
|
|
|
|
|
|
|
/* inet_frag_queue_* functions use skb->cb; see struct ipfrag_skb_cb
|
|
|
|
* in inet_fragment.c
|
|
|
|
*/
|
|
|
|
BUILD_BUG_ON(sizeof(struct lowpan_802154_cb) > sizeof(struct inet_skb_parm));
|
|
|
|
BUILD_BUG_ON(sizeof(struct lowpan_802154_cb) > sizeof(struct inet6_skb_parm));
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2014-08-01 18:29:44 +08:00
|
|
|
if (fq->q.flags & INET_FRAG_COMPLETE)
|
2014-02-28 14:32:50 +08:00
|
|
|
goto err;
|
|
|
|
|
2015-09-02 20:21:25 +08:00
|
|
|
offset = lowpan_802154_cb(skb)->d_offset << 3;
|
|
|
|
end = lowpan_802154_cb(skb)->d_size;
|
2014-02-28 14:32:50 +08:00
|
|
|
|
|
|
|
/* Is this the final fragment? */
|
|
|
|
if (offset + skb->len == end) {
|
|
|
|
/* If we already have some bits beyond end
|
|
|
|
* or have different end, the segment is corrupted.
|
|
|
|
*/
|
|
|
|
if (end < fq->q.len ||
|
2014-08-01 18:29:44 +08:00
|
|
|
((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
|
2014-02-28 14:32:50 +08:00
|
|
|
goto err;
|
2014-08-01 18:29:44 +08:00
|
|
|
fq->q.flags |= INET_FRAG_LAST_IN;
|
2014-02-28 14:32:50 +08:00
|
|
|
fq->q.len = end;
|
|
|
|
} else {
|
|
|
|
if (end > fq->q.len) {
|
|
|
|
/* Some bits beyond end -> corruption. */
|
2014-08-01 18:29:44 +08:00
|
|
|
if (fq->q.flags & INET_FRAG_LAST_IN)
|
2014-02-28 14:32:50 +08:00
|
|
|
goto err;
|
|
|
|
fq->q.len = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:21:16 +08:00
|
|
|
ldev = skb->dev;
|
|
|
|
if (ldev)
|
2014-02-28 14:32:50 +08:00
|
|
|
skb->dev = NULL;
|
2019-02-15 10:29:53 +08:00
|
|
|
barrier();
|
|
|
|
|
|
|
|
prev_tail = fq->q.fragments_tail;
|
|
|
|
err = inet_frag_queue_insert(&fq->q, skb, offset, end);
|
|
|
|
if (err)
|
|
|
|
goto err;
|
2014-02-28 14:32:50 +08:00
|
|
|
|
|
|
|
fq->q.stamp = skb->tstamp;
|
2015-09-02 20:21:25 +08:00
|
|
|
if (frag_type == LOWPAN_DISPATCH_FRAG1)
|
2014-08-01 18:29:44 +08:00
|
|
|
fq->q.flags |= INET_FRAG_FIRST_IN;
|
2015-09-02 20:21:25 +08:00
|
|
|
|
|
|
|
fq->q.meat += skb->len;
|
2019-05-25 00:03:30 +08:00
|
|
|
add_frag_mem_limit(fq->q.fqdir, skb->truesize);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2014-08-01 18:29:44 +08:00
|
|
|
if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
|
2014-02-28 14:32:50 +08:00
|
|
|
fq->q.meat == fq->q.len) {
|
|
|
|
int res;
|
|
|
|
unsigned long orefdst = skb->_skb_refdst;
|
|
|
|
|
|
|
|
skb->_skb_refdst = 0UL;
|
2019-02-15 10:29:53 +08:00
|
|
|
res = lowpan_frag_reasm(fq, skb, prev_tail, ldev);
|
2014-02-28 14:32:50 +08:00
|
|
|
skb->_skb_refdst = orefdst;
|
|
|
|
return res;
|
|
|
|
}
|
2019-02-15 10:29:53 +08:00
|
|
|
skb_dst_drop(skb);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
err:
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if this packet is complete.
|
|
|
|
*
|
|
|
|
* It is called with locked fq, and caller must check that
|
|
|
|
* queue is eligible for reassembly i.e. it is not COMPLETE,
|
|
|
|
* the last and the first frames arrived and all the bits are here.
|
|
|
|
*/
|
2019-02-15 10:29:53 +08:00
|
|
|
static int lowpan_frag_reasm(struct lowpan_frag_queue *fq, struct sk_buff *skb,
|
|
|
|
struct sk_buff *prev_tail, struct net_device *ldev)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
2019-02-15 10:29:53 +08:00
|
|
|
void *reasm_data;
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2018-04-01 03:58:44 +08:00
|
|
|
inet_frag_kill(&fq->q);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2019-02-15 10:29:53 +08:00
|
|
|
reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
|
|
|
|
if (!reasm_data)
|
2014-02-28 14:32:50 +08:00
|
|
|
goto out_oom;
|
2019-02-15 10:29:53 +08:00
|
|
|
inet_frag_reasm_finish(&fq->q, skb, reasm_data);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2019-02-15 10:29:53 +08:00
|
|
|
skb->dev = ldev;
|
|
|
|
skb->tstamp = fq->q.stamp;
|
|
|
|
fq->q.rb_fragments = RB_ROOT;
|
2014-02-28 14:32:50 +08:00
|
|
|
fq->q.fragments_tail = NULL;
|
2019-02-15 10:29:53 +08:00
|
|
|
fq->q.last_run_head = NULL;
|
2014-02-28 14:32:50 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
out_oom:
|
|
|
|
net_dbg_ratelimited("lowpan_frag_reasm: no memory for reassembly\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:21:25 +08:00
|
|
|
static int lowpan_frag_rx_handlers_result(struct sk_buff *skb,
|
|
|
|
lowpan_rx_result res)
|
|
|
|
{
|
|
|
|
switch (res) {
|
|
|
|
case RX_QUEUED:
|
|
|
|
return NET_RX_SUCCESS;
|
|
|
|
case RX_CONTINUE:
|
|
|
|
/* nobody cared about this packet */
|
|
|
|
net_warn_ratelimited("%s: received unknown dispatch\n",
|
|
|
|
__func__);
|
|
|
|
|
|
|
|
/* fall-through */
|
|
|
|
default:
|
|
|
|
/* all others failure */
|
|
|
|
return NET_RX_DROP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static lowpan_rx_result lowpan_frag_rx_h_iphc(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!lowpan_is_iphc(*skb_network_header(skb)))
|
|
|
|
return RX_CONTINUE;
|
|
|
|
|
|
|
|
ret = lowpan_iphc_decompress(skb);
|
|
|
|
if (ret < 0)
|
|
|
|
return RX_DROP;
|
|
|
|
|
|
|
|
return RX_QUEUED;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lowpan_invoke_frag_rx_handlers(struct sk_buff *skb)
|
|
|
|
{
|
|
|
|
lowpan_rx_result res;
|
|
|
|
|
|
|
|
#define CALL_RXH(rxh) \
|
|
|
|
do { \
|
|
|
|
res = rxh(skb); \
|
|
|
|
if (res != RX_CONTINUE) \
|
|
|
|
goto rxh_next; \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* likely at first */
|
|
|
|
CALL_RXH(lowpan_frag_rx_h_iphc);
|
|
|
|
CALL_RXH(lowpan_rx_h_ipv6);
|
|
|
|
|
|
|
|
rxh_next:
|
|
|
|
return lowpan_frag_rx_handlers_result(skb, res);
|
|
|
|
#undef CALL_RXH
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK 0x07
|
|
|
|
#define LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT 8
|
|
|
|
|
|
|
|
static int lowpan_get_cb(struct sk_buff *skb, u8 frag_type,
|
|
|
|
struct lowpan_802154_cb *cb)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
|
|
|
bool fail;
|
2015-09-02 20:21:25 +08:00
|
|
|
u8 high = 0, low = 0;
|
2014-10-06 17:00:51 +08:00
|
|
|
__be16 d_tag = 0;
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2015-09-02 20:21:25 +08:00
|
|
|
fail = lowpan_fetch_skb(skb, &high, 1);
|
2014-02-28 14:32:50 +08:00
|
|
|
fail |= lowpan_fetch_skb(skb, &low, 1);
|
2015-09-02 20:21:25 +08:00
|
|
|
/* remove the dispatch value and use first three bits as high value
|
|
|
|
* for the datagram size
|
|
|
|
*/
|
|
|
|
cb->d_size = (high & LOWPAN_FRAG_DGRAM_SIZE_HIGH_MASK) <<
|
|
|
|
LOWPAN_FRAG_DGRAM_SIZE_HIGH_SHIFT | low;
|
2014-10-06 17:00:51 +08:00
|
|
|
fail |= lowpan_fetch_skb(skb, &d_tag, 2);
|
2015-09-02 20:21:25 +08:00
|
|
|
cb->d_tag = ntohs(d_tag);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
|
|
|
if (frag_type == LOWPAN_DISPATCH_FRAGN) {
|
2015-09-02 20:21:25 +08:00
|
|
|
fail |= lowpan_fetch_skb(skb, &cb->d_offset, 1);
|
2014-02-28 14:32:50 +08:00
|
|
|
} else {
|
|
|
|
skb_reset_network_header(skb);
|
2015-09-02 20:21:25 +08:00
|
|
|
cb->d_offset = 0;
|
|
|
|
/* check if datagram_size has ipv6hdr on FRAG1 */
|
|
|
|
fail |= cb->d_size < sizeof(struct ipv6hdr);
|
|
|
|
/* check if we can dereference the dispatch value */
|
|
|
|
fail |= !skb->len;
|
2014-02-28 14:32:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(fail))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:21:25 +08:00
|
|
|
int lowpan_frag_rcv(struct sk_buff *skb, u8 frag_type)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
|
|
|
struct lowpan_frag_queue *fq;
|
|
|
|
struct net *net = dev_net(skb->dev);
|
2015-09-02 20:21:25 +08:00
|
|
|
struct lowpan_802154_cb *cb = lowpan_802154_cb(skb);
|
2018-04-21 02:54:13 +08:00
|
|
|
struct ieee802154_hdr hdr = {};
|
2014-02-28 14:32:50 +08:00
|
|
|
int err;
|
|
|
|
|
2015-09-02 20:21:25 +08:00
|
|
|
if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
|
|
|
|
goto err;
|
2014-03-15 04:24:02 +08:00
|
|
|
|
2015-09-02 20:21:25 +08:00
|
|
|
err = lowpan_get_cb(skb, frag_type, cb);
|
2014-02-28 14:32:50 +08:00
|
|
|
if (err < 0)
|
|
|
|
goto err;
|
|
|
|
|
2015-09-02 20:21:25 +08:00
|
|
|
if (frag_type == LOWPAN_DISPATCH_FRAG1) {
|
|
|
|
err = lowpan_invoke_frag_rx_handlers(skb);
|
|
|
|
if (err == NET_RX_DROP)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cb->d_size > IPV6_MIN_MTU) {
|
2014-08-20 01:03:32 +08:00
|
|
|
net_warn_ratelimited("lowpan_frag_rcv: datagram size exceeds MTU\n");
|
2014-02-28 14:32:50 +08:00
|
|
|
goto err;
|
2014-08-20 01:03:32 +08:00
|
|
|
}
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2015-09-02 20:21:25 +08:00
|
|
|
fq = fq_find(net, cb, &hdr.source, &hdr.dest);
|
2014-02-28 14:32:50 +08:00
|
|
|
if (fq != NULL) {
|
|
|
|
int ret;
|
2014-07-02 11:31:09 +08:00
|
|
|
|
2014-02-28 14:32:50 +08:00
|
|
|
spin_lock(&fq->q.lock);
|
|
|
|
ret = lowpan_frag_queue(fq, skb, frag_type);
|
|
|
|
spin_unlock(&fq->q.lock);
|
|
|
|
|
2018-04-01 03:58:44 +08:00
|
|
|
inet_frag_put(&fq->q);
|
2014-02-28 14:32:50 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSCTL
|
2014-07-24 22:50:37 +08:00
|
|
|
|
2014-02-28 14:32:50 +08:00
|
|
|
static struct ctl_table lowpan_frags_ns_ctl_table[] = {
|
|
|
|
{
|
|
|
|
.procname = "6lowpanfrag_high_thresh",
|
2018-04-01 03:58:53 +08:00
|
|
|
.maxlen = sizeof(unsigned long),
|
2014-02-28 14:32:50 +08:00
|
|
|
.mode = 0644,
|
2018-04-01 03:58:53 +08:00
|
|
|
.proc_handler = proc_doulongvec_minmax,
|
2014-02-28 14:32:50 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.procname = "6lowpanfrag_low_thresh",
|
2018-04-01 03:58:53 +08:00
|
|
|
.maxlen = sizeof(unsigned long),
|
2014-02-28 14:32:50 +08:00
|
|
|
.mode = 0644,
|
2018-04-01 03:58:53 +08:00
|
|
|
.proc_handler = proc_doulongvec_minmax,
|
2014-02-28 14:32:50 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.procname = "6lowpanfrag_time",
|
|
|
|
.maxlen = sizeof(int),
|
|
|
|
.mode = 0644,
|
|
|
|
.proc_handler = proc_dointvec_jiffies,
|
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2014-07-24 22:50:35 +08:00
|
|
|
/* secret interval has been deprecated */
|
|
|
|
static int lowpan_frags_secret_interval_unused;
|
2014-02-28 14:32:50 +08:00
|
|
|
static struct ctl_table lowpan_frags_ctl_table[] = {
|
|
|
|
{
|
|
|
|
.procname = "6lowpanfrag_secret_interval",
|
2014-07-24 22:50:35 +08:00
|
|
|
.data = &lowpan_frags_secret_interval_unused,
|
2014-02-28 14:32:50 +08:00
|
|
|
.maxlen = sizeof(int),
|
|
|
|
.mode = 0644,
|
|
|
|
.proc_handler = proc_dointvec_jiffies,
|
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
static int __net_init lowpan_frags_ns_sysctl_register(struct net *net)
|
|
|
|
{
|
|
|
|
struct ctl_table *table;
|
|
|
|
struct ctl_table_header *hdr;
|
2014-04-18 09:22:54 +08:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
|
|
|
table = lowpan_frags_ns_ctl_table;
|
|
|
|
if (!net_eq(net, &init_net)) {
|
|
|
|
table = kmemdup(table, sizeof(lowpan_frags_ns_ctl_table),
|
|
|
|
GFP_KERNEL);
|
|
|
|
if (table == NULL)
|
|
|
|
goto err_alloc;
|
|
|
|
|
|
|
|
/* Don't export sysctls to unprivileged users */
|
|
|
|
if (net->user_ns != &init_user_ns)
|
|
|
|
table[0].procname = NULL;
|
|
|
|
}
|
|
|
|
|
2019-05-25 00:03:36 +08:00
|
|
|
table[0].data = &ieee802154_lowpan->fqdir.high_thresh;
|
|
|
|
table[0].extra1 = &ieee802154_lowpan->fqdir.low_thresh;
|
|
|
|
table[1].data = &ieee802154_lowpan->fqdir.low_thresh;
|
|
|
|
table[1].extra2 = &ieee802154_lowpan->fqdir.high_thresh;
|
|
|
|
table[2].data = &ieee802154_lowpan->fqdir.timeout;
|
|
|
|
|
2014-02-28 14:32:50 +08:00
|
|
|
hdr = register_net_sysctl(net, "net/ieee802154/6lowpan", table);
|
|
|
|
if (hdr == NULL)
|
|
|
|
goto err_reg;
|
|
|
|
|
2014-04-18 09:22:54 +08:00
|
|
|
ieee802154_lowpan->sysctl.frags_hdr = hdr;
|
2014-02-28 14:32:50 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_reg:
|
|
|
|
if (!net_eq(net, &init_net))
|
|
|
|
kfree(table);
|
|
|
|
err_alloc:
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __net_exit lowpan_frags_ns_sysctl_unregister(struct net *net)
|
|
|
|
{
|
|
|
|
struct ctl_table *table;
|
2014-04-18 09:22:54 +08:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2014-04-18 09:22:54 +08:00
|
|
|
table = ieee802154_lowpan->sysctl.frags_hdr->ctl_table_arg;
|
|
|
|
unregister_net_sysctl_table(ieee802154_lowpan->sysctl.frags_hdr);
|
2014-02-28 14:32:50 +08:00
|
|
|
if (!net_eq(net, &init_net))
|
|
|
|
kfree(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ctl_table_header *lowpan_ctl_header;
|
|
|
|
|
2014-10-01 04:34:08 +08:00
|
|
|
static int __init lowpan_frags_sysctl_register(void)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
|
|
|
lowpan_ctl_header = register_net_sysctl(&init_net,
|
|
|
|
"net/ieee802154/6lowpan",
|
|
|
|
lowpan_frags_ctl_table);
|
|
|
|
return lowpan_ctl_header == NULL ? -ENOMEM : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void lowpan_frags_sysctl_unregister(void)
|
|
|
|
{
|
|
|
|
unregister_net_sysctl_table(lowpan_ctl_header);
|
|
|
|
}
|
|
|
|
#else
|
2014-10-01 13:27:46 +08:00
|
|
|
static inline int lowpan_frags_ns_sysctl_register(struct net *net)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lowpan_frags_ns_sysctl_unregister(struct net *net)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-01 13:27:46 +08:00
|
|
|
static inline int __init lowpan_frags_sysctl_register(void)
|
2014-02-28 14:32:50 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lowpan_frags_sysctl_unregister(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int __net_init lowpan_frags_init_net(struct net *net)
|
|
|
|
{
|
2014-04-18 09:22:54 +08:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
2018-04-01 03:58:43 +08:00
|
|
|
int res;
|
2014-02-28 14:32:50 +08:00
|
|
|
|
2019-05-25 00:03:32 +08:00
|
|
|
ieee802154_lowpan->fqdir.high_thresh = IPV6_FRAG_HIGH_THRESH;
|
|
|
|
ieee802154_lowpan->fqdir.low_thresh = IPV6_FRAG_LOW_THRESH;
|
|
|
|
ieee802154_lowpan->fqdir.timeout = IPV6_FRAG_TIMEOUT;
|
2014-04-18 09:22:54 +08:00
|
|
|
|
2019-05-25 00:03:38 +08:00
|
|
|
res = fqdir_init(&ieee802154_lowpan->fqdir, &lowpan_frags, net);
|
2018-04-01 03:58:43 +08:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
|
|
|
res = lowpan_frags_ns_sysctl_register(net);
|
|
|
|
if (res < 0)
|
2019-05-25 00:03:32 +08:00
|
|
|
fqdir_exit(&ieee802154_lowpan->fqdir);
|
2018-04-01 03:58:43 +08:00
|
|
|
return res;
|
2014-02-28 14:32:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __net_exit lowpan_frags_exit_net(struct net *net)
|
|
|
|
{
|
2014-04-18 09:22:54 +08:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
|
|
|
|
2014-02-28 14:32:50 +08:00
|
|
|
lowpan_frags_ns_sysctl_unregister(net);
|
2019-05-25 00:03:32 +08:00
|
|
|
fqdir_exit(&ieee802154_lowpan->fqdir);
|
2014-02-28 14:32:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pernet_operations lowpan_frags_ops = {
|
|
|
|
.init = lowpan_frags_init_net,
|
|
|
|
.exit = lowpan_frags_exit_net,
|
|
|
|
};
|
|
|
|
|
inet: frags: use rhashtables for reassembly units
Some applications still rely on IP fragmentation, and to be fair linux
reassembly unit is not working under any serious load.
It uses static hash tables of 1024 buckets, and up to 128 items per bucket (!!!)
A work queue is supposed to garbage collect items when host is under memory
pressure, and doing a hash rebuild, changing seed used in hash computations.
This work queue blocks softirqs for up to 25 ms when doing a hash rebuild,
occurring every 5 seconds if host is under fire.
Then there is the problem of sharing this hash table for all netns.
It is time to switch to rhashtables, and allocate one of them per netns
to speedup netns dismantle, since this is a critical metric these days.
Lookup is now using RCU. A followup patch will even remove
the refcount hold/release left from prior implementation and save
a couple of atomic operations.
Before this patch, 16 cpus (16 RX queue NIC) could not handle more
than 1 Mpps frags DDOS.
After the patch, I reach 9 Mpps without any tuning, and can use up to 2GB
of storage for the fragments (exact number depends on frags being evicted
after timeout)
$ grep FRAG /proc/net/sockstat
FRAG: inuse 1966916 memory 2140004608
A followup patch will change the limits for 64bit arches.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Florian Westphal <fw@strlen.de>
Cc: Jesper Dangaard Brouer <brouer@redhat.com>
Cc: Alexander Aring <alex.aring@gmail.com>
Cc: Stefan Schmidt <stefan@osg.samsung.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-04-01 03:58:49 +08:00
|
|
|
static u32 lowpan_key_hashfn(const void *data, u32 len, u32 seed)
|
|
|
|
{
|
|
|
|
return jhash2(data,
|
|
|
|
sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 lowpan_obj_hashfn(const void *data, u32 len, u32 seed)
|
|
|
|
{
|
|
|
|
const struct inet_frag_queue *fq = data;
|
|
|
|
|
|
|
|
return jhash2((const u32 *)&fq->key,
|
|
|
|
sizeof(struct frag_lowpan_compare_key) / sizeof(u32), seed);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lowpan_obj_cmpfn(struct rhashtable_compare_arg *arg, const void *ptr)
|
|
|
|
{
|
|
|
|
const struct frag_lowpan_compare_key *key = arg->key;
|
|
|
|
const struct inet_frag_queue *fq = ptr;
|
|
|
|
|
|
|
|
return !!memcmp(&fq->key, key, sizeof(*key));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rhashtable_params lowpan_rhash_params = {
|
|
|
|
.head_offset = offsetof(struct inet_frag_queue, node),
|
|
|
|
.hashfn = lowpan_key_hashfn,
|
|
|
|
.obj_hashfn = lowpan_obj_hashfn,
|
|
|
|
.obj_cmpfn = lowpan_obj_cmpfn,
|
|
|
|
.automatic_shrinking = true,
|
|
|
|
};
|
|
|
|
|
2014-02-28 14:32:50 +08:00
|
|
|
int __init lowpan_net_frag_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
lowpan_frags.constructor = lowpan_frag_init;
|
|
|
|
lowpan_frags.destructor = NULL;
|
|
|
|
lowpan_frags.qsize = sizeof(struct frag_queue);
|
|
|
|
lowpan_frags.frag_expire = lowpan_frag_expire;
|
2014-08-01 18:29:48 +08:00
|
|
|
lowpan_frags.frags_cache_name = lowpan_frags_cache_name;
|
inet: frags: use rhashtables for reassembly units
Some applications still rely on IP fragmentation, and to be fair linux
reassembly unit is not working under any serious load.
It uses static hash tables of 1024 buckets, and up to 128 items per bucket (!!!)
A work queue is supposed to garbage collect items when host is under memory
pressure, and doing a hash rebuild, changing seed used in hash computations.
This work queue blocks softirqs for up to 25 ms when doing a hash rebuild,
occurring every 5 seconds if host is under fire.
Then there is the problem of sharing this hash table for all netns.
It is time to switch to rhashtables, and allocate one of them per netns
to speedup netns dismantle, since this is a critical metric these days.
Lookup is now using RCU. A followup patch will even remove
the refcount hold/release left from prior implementation and save
a couple of atomic operations.
Before this patch, 16 cpus (16 RX queue NIC) could not handle more
than 1 Mpps frags DDOS.
After the patch, I reach 9 Mpps without any tuning, and can use up to 2GB
of storage for the fragments (exact number depends on frags being evicted
after timeout)
$ grep FRAG /proc/net/sockstat
FRAG: inuse 1966916 memory 2140004608
A followup patch will change the limits for 64bit arches.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: Florian Westphal <fw@strlen.de>
Cc: Jesper Dangaard Brouer <brouer@redhat.com>
Cc: Alexander Aring <alex.aring@gmail.com>
Cc: Stefan Schmidt <stefan@osg.samsung.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
2018-04-01 03:58:49 +08:00
|
|
|
lowpan_frags.rhash_params = lowpan_rhash_params;
|
2014-08-01 18:29:48 +08:00
|
|
|
ret = inet_frags_init(&lowpan_frags);
|
|
|
|
if (ret)
|
2018-04-01 03:58:46 +08:00
|
|
|
goto out;
|
2014-03-07 18:06:54 +08:00
|
|
|
|
2018-04-01 03:58:46 +08:00
|
|
|
ret = lowpan_frags_sysctl_register();
|
|
|
|
if (ret)
|
|
|
|
goto err_sysctl;
|
|
|
|
|
|
|
|
ret = register_pernet_subsys(&lowpan_frags_ops);
|
|
|
|
if (ret)
|
|
|
|
goto err_pernet;
|
|
|
|
out:
|
2014-03-07 18:06:54 +08:00
|
|
|
return ret;
|
2014-02-28 14:32:50 +08:00
|
|
|
err_pernet:
|
|
|
|
lowpan_frags_sysctl_unregister();
|
2018-04-01 03:58:46 +08:00
|
|
|
err_sysctl:
|
|
|
|
inet_frags_fini(&lowpan_frags);
|
2014-02-28 14:32:50 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lowpan_net_frag_exit(void)
|
|
|
|
{
|
|
|
|
inet_frags_fini(&lowpan_frags);
|
|
|
|
lowpan_frags_sysctl_unregister();
|
|
|
|
unregister_pernet_subsys(&lowpan_frags_ops);
|
|
|
|
}
|