2019-05-27 06:55:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2014-02-28 06:32:50 +00:00
|
|
|
/* 6LoWPAN fragment reassembly
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Alexander Aring <aar@pengutronix.de>
|
|
|
|
*
|
|
|
|
* Based on: net/ipv6/reassembly.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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 13:29:05 +00:00
|
|
|
#include <net/6lowpan.h>
|
2018-07-13 23:14:01 +00:00
|
|
|
#include <net/ipv6_frag.h>
|
2014-02-28 06:32:50 +00:00
|
|
|
#include <net/inet_frag.h>
|
2019-02-15 02:29:53 +00:00
|
|
|
#include <net/ip.h>
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2015-01-04 16:10:54 +00:00
|
|
|
#include "6lowpan_i.h"
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2014-08-01 10:29:48 +00:00
|
|
|
static const char lowpan_frags_cache_name[] = "lowpan-frags";
|
|
|
|
|
2014-02-28 06:32:50 +00:00
|
|
|
static struct inet_frags lowpan_frags;
|
|
|
|
|
2019-02-15 02:29:53 +00: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 06:32:50 +00:00
|
|
|
|
2014-07-24 14:50:29 +00:00
|
|
|
static void lowpan_frag_init(struct inet_frag_queue *q, const void *a)
|
2014-02-28 06:32:50 +00: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-03-31 19:58:49 +00:00
|
|
|
const struct frag_lowpan_compare_key *key = a;
|
2014-02-28 06:32:50 +00: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-03-31 19:58:49 +00:00
|
|
|
BUILD_BUG_ON(sizeof(*key) > sizeof(q->key));
|
|
|
|
memcpy(&q->key, key, sizeof(*key));
|
2014-02-28 06:32:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-17 00:29:20 +00:00
|
|
|
static void lowpan_frag_expire(struct timer_list *t)
|
2014-02-28 06:32:50 +00:00
|
|
|
{
|
2017-10-17 00:29:20 +00:00
|
|
|
struct inet_frag_queue *frag = from_timer(frag, t, timer);
|
2014-02-28 06:32:50 +00:00
|
|
|
struct frag_queue *fq;
|
|
|
|
|
2017-10-17 00:29:20 +00:00
|
|
|
fq = container_of(frag, struct frag_queue, q);
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2014-03-13 19:58:03 +00:00
|
|
|
spin_lock(&fq->q.lock);
|
|
|
|
|
2014-08-01 10:29:44 +00:00
|
|
|
if (fq->q.flags & INET_FRAG_COMPLETE)
|
2014-03-13 19:58:03 +00:00
|
|
|
goto out;
|
|
|
|
|
2018-03-31 19:58:44 +00:00
|
|
|
inet_frag_kill(&fq->q);
|
2014-03-13 19:58:03 +00:00
|
|
|
out:
|
|
|
|
spin_unlock(&fq->q.lock);
|
2018-03-31 19:58:44 +00:00
|
|
|
inet_frag_put(&fq->q);
|
2014-02-28 06:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct lowpan_frag_queue *
|
2015-09-02 12:21:25 +00:00
|
|
|
fq_find(struct net *net, const struct lowpan_802154_cb *cb,
|
2014-03-14 20:24:02 +00:00
|
|
|
const struct ieee802154_addr *src,
|
|
|
|
const struct ieee802154_addr *dst)
|
2014-02-28 06:32:50 +00:00
|
|
|
{
|
2014-04-18 01:22:54 +00:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
2018-04-20 18:54:13 +00: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-03-31 19:58:49 +00:00
|
|
|
struct inet_frag_queue *q;
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2018-04-20 18:54:13 +00:00
|
|
|
key.tag = cb->d_tag;
|
|
|
|
key.d_size = cb->d_size;
|
|
|
|
key.src = *src;
|
|
|
|
key.dst = *dst;
|
|
|
|
|
2019-05-24 16:03:39 +00:00
|
|
|
q = inet_frag_find(ieee802154_lowpan->fqdir, &key);
|
2018-03-31 19:58:52 +00:00
|
|
|
if (!q)
|
2014-02-28 06:32:50 +00:00
|
|
|
return NULL;
|
2018-03-31 19:58:52 +00:00
|
|
|
|
2014-02-28 06:32:50 +00:00
|
|
|
return container_of(q, struct lowpan_frag_queue, q);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int lowpan_frag_queue(struct lowpan_frag_queue *fq,
|
2015-09-02 12:21:25 +00:00
|
|
|
struct sk_buff *skb, u8 frag_type)
|
2014-02-28 06:32:50 +00:00
|
|
|
{
|
2019-02-15 02:29:53 +00:00
|
|
|
struct sk_buff *prev_tail;
|
2015-09-02 12:21:16 +00:00
|
|
|
struct net_device *ldev;
|
2019-02-15 02:29:53 +00: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 06:32:50 +00:00
|
|
|
|
2014-08-01 10:29:44 +00:00
|
|
|
if (fq->q.flags & INET_FRAG_COMPLETE)
|
2014-02-28 06:32:50 +00:00
|
|
|
goto err;
|
|
|
|
|
2015-09-02 12:21:25 +00:00
|
|
|
offset = lowpan_802154_cb(skb)->d_offset << 3;
|
|
|
|
end = lowpan_802154_cb(skb)->d_size;
|
2014-02-28 06:32:50 +00: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 10:29:44 +00:00
|
|
|
((fq->q.flags & INET_FRAG_LAST_IN) && end != fq->q.len))
|
2014-02-28 06:32:50 +00:00
|
|
|
goto err;
|
2014-08-01 10:29:44 +00:00
|
|
|
fq->q.flags |= INET_FRAG_LAST_IN;
|
2014-02-28 06:32:50 +00:00
|
|
|
fq->q.len = end;
|
|
|
|
} else {
|
|
|
|
if (end > fq->q.len) {
|
|
|
|
/* Some bits beyond end -> corruption. */
|
2014-08-01 10:29:44 +00:00
|
|
|
if (fq->q.flags & INET_FRAG_LAST_IN)
|
2014-02-28 06:32:50 +00:00
|
|
|
goto err;
|
|
|
|
fq->q.len = end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 12:21:16 +00:00
|
|
|
ldev = skb->dev;
|
|
|
|
if (ldev)
|
2014-02-28 06:32:50 +00:00
|
|
|
skb->dev = NULL;
|
2019-02-15 02:29:53 +00: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 06:32:50 +00:00
|
|
|
|
|
|
|
fq->q.stamp = skb->tstamp;
|
2015-09-02 12:21:25 +00:00
|
|
|
if (frag_type == LOWPAN_DISPATCH_FRAG1)
|
2014-08-01 10:29:44 +00:00
|
|
|
fq->q.flags |= INET_FRAG_FIRST_IN;
|
2015-09-02 12:21:25 +00:00
|
|
|
|
|
|
|
fq->q.meat += skb->len;
|
2019-05-24 16:03:30 +00:00
|
|
|
add_frag_mem_limit(fq->q.fqdir, skb->truesize);
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2014-08-01 10:29:44 +00:00
|
|
|
if (fq->q.flags == (INET_FRAG_FIRST_IN | INET_FRAG_LAST_IN) &&
|
2014-02-28 06:32:50 +00:00
|
|
|
fq->q.meat == fq->q.len) {
|
|
|
|
int res;
|
|
|
|
unsigned long orefdst = skb->_skb_refdst;
|
|
|
|
|
|
|
|
skb->_skb_refdst = 0UL;
|
2019-02-15 02:29:53 +00:00
|
|
|
res = lowpan_frag_reasm(fq, skb, prev_tail, ldev);
|
2014-02-28 06:32:50 +00:00
|
|
|
skb->_skb_refdst = orefdst;
|
|
|
|
return res;
|
|
|
|
}
|
2019-02-15 02:29:53 +00:00
|
|
|
skb_dst_drop(skb);
|
2014-02-28 06:32:50 +00: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 02:29:53 +00: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 06:32:50 +00:00
|
|
|
{
|
2019-02-15 02:29:53 +00:00
|
|
|
void *reasm_data;
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2018-03-31 19:58:44 +00:00
|
|
|
inet_frag_kill(&fq->q);
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2019-02-15 02:29:53 +00:00
|
|
|
reasm_data = inet_frag_reasm_prepare(&fq->q, skb, prev_tail);
|
|
|
|
if (!reasm_data)
|
2014-02-28 06:32:50 +00:00
|
|
|
goto out_oom;
|
inet: frags: re-introduce skb coalescing for local delivery
Before commit d4289fcc9b16 ("net: IP6 defrag: use rbtrees for IPv6
defrag"), a netperf UDP_STREAM test[0] using big IPv6 datagrams (thus
generating many fragments) and running over an IPsec tunnel, reported
more than 6Gbps throughput. After that patch, the same test gets only
9Mbps when receiving on a be2net nic (driver can make a big difference
here, for example, ixgbe doesn't seem to be affected).
By reusing the IPv4 defragmentation code, IPv6 lost fragment coalescing
(IPv4 fragment coalescing was dropped by commit 14fe22e33462 ("Revert
"ipv4: use skb coalescing in defragmentation"")).
Without fragment coalescing, be2net runs out of Rx ring entries and
starts to drop frames (ethtool reports rx_drops_no_frags errors). Since
the netperf traffic is only composed of UDP fragments, any lost packet
prevents reassembly of the full datagram. Therefore, fragments which
have no possibility to ever get reassembled pile up in the reassembly
queue, until the memory accounting exeeds the threshold. At that point
no fragment is accepted anymore, which effectively discards all
netperf traffic.
When reassembly timeout expires, some stale fragments are removed from
the reassembly queue, so a few packets can be received, reassembled
and delivered to the netperf receiver. But the nic still drops frames
and soon the reassembly queue gets filled again with stale fragments.
These long time frames where no datagram can be received explain why
the performance drop is so significant.
Re-introducing fragment coalescing is enough to get the initial
performances again (6.6Gbps with be2net): driver doesn't drop frames
anymore (no more rx_drops_no_frags errors) and the reassembly engine
works at full speed.
This patch is quite conservative and only coalesces skbs for local
IPv4 and IPv6 delivery (in order to avoid changing skb geometry when
forwarding). Coalescing could be extended in the future if need be, as
more scenarios would probably benefit from it.
[0]: Test configuration
Sender:
ip xfrm policy flush
ip xfrm state flush
ip xfrm state add src fc00:1::1 dst fc00:2::1 proto esp spi 0x1000 aead 'rfc4106(gcm(aes))' 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b 96 mode transport sel src fc00:1::1 dst fc00:2::1
ip xfrm policy add src fc00:1::1 dst fc00:2::1 dir in tmpl src fc00:1::1 dst fc00:2::1 proto esp mode transport action allow
ip xfrm state add src fc00:2::1 dst fc00:1::1 proto esp spi 0x1001 aead 'rfc4106(gcm(aes))' 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b 96 mode transport sel src fc00:2::1 dst fc00:1::1
ip xfrm policy add src fc00:2::1 dst fc00:1::1 dir out tmpl src fc00:2::1 dst fc00:1::1 proto esp mode transport action allow
netserver -D -L fc00:2::1
Receiver:
ip xfrm policy flush
ip xfrm state flush
ip xfrm state add src fc00:2::1 dst fc00:1::1 proto esp spi 0x1001 aead 'rfc4106(gcm(aes))' 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b 96 mode transport sel src fc00:2::1 dst fc00:1::1
ip xfrm policy add src fc00:2::1 dst fc00:1::1 dir in tmpl src fc00:2::1 dst fc00:1::1 proto esp mode transport action allow
ip xfrm state add src fc00:1::1 dst fc00:2::1 proto esp spi 0x1000 aead 'rfc4106(gcm(aes))' 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b 96 mode transport sel src fc00:1::1 dst fc00:2::1
ip xfrm policy add src fc00:1::1 dst fc00:2::1 dir out tmpl src fc00:1::1 dst fc00:2::1 proto esp mode transport action allow
netperf -H fc00:2::1 -f k -P 0 -L fc00:1::1 -l 60 -t UDP_STREAM -I 99,5 -i 5,5 -T5,5 -6
Signed-off-by: Guillaume Nault <gnault@redhat.com>
Acked-by: Florian Westphal <fw@strlen.de>
Signed-off-by: David S. Miller <davem@davemloft.net>
2019-08-02 15:15:03 +00:00
|
|
|
inet_frag_reasm_finish(&fq->q, skb, reasm_data, false);
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2019-02-15 02:29:53 +00:00
|
|
|
skb->dev = ldev;
|
|
|
|
skb->tstamp = fq->q.stamp;
|
|
|
|
fq->q.rb_fragments = RB_ROOT;
|
2014-02-28 06:32:50 +00:00
|
|
|
fq->q.fragments_tail = NULL;
|
2019-02-15 02:29:53 +00:00
|
|
|
fq->q.last_run_head = NULL;
|
2014-02-28 06:32:50 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
out_oom:
|
|
|
|
net_dbg_ratelimited("lowpan_frag_reasm: no memory for reassembly\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-09-02 12:21:25 +00: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 06:32:50 +00:00
|
|
|
{
|
|
|
|
bool fail;
|
2015-09-02 12:21:25 +00:00
|
|
|
u8 high = 0, low = 0;
|
2014-10-06 09:00:51 +00:00
|
|
|
__be16 d_tag = 0;
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2015-09-02 12:21:25 +00:00
|
|
|
fail = lowpan_fetch_skb(skb, &high, 1);
|
2014-02-28 06:32:50 +00:00
|
|
|
fail |= lowpan_fetch_skb(skb, &low, 1);
|
2015-09-02 12:21:25 +00: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 09:00:51 +00:00
|
|
|
fail |= lowpan_fetch_skb(skb, &d_tag, 2);
|
2015-09-02 12:21:25 +00:00
|
|
|
cb->d_tag = ntohs(d_tag);
|
2014-02-28 06:32:50 +00:00
|
|
|
|
|
|
|
if (frag_type == LOWPAN_DISPATCH_FRAGN) {
|
2015-09-02 12:21:25 +00:00
|
|
|
fail |= lowpan_fetch_skb(skb, &cb->d_offset, 1);
|
2014-02-28 06:32:50 +00:00
|
|
|
} else {
|
|
|
|
skb_reset_network_header(skb);
|
2015-09-02 12:21:25 +00: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 06:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(fail))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-09-02 12:21:25 +00:00
|
|
|
int lowpan_frag_rcv(struct sk_buff *skb, u8 frag_type)
|
2014-02-28 06:32:50 +00:00
|
|
|
{
|
|
|
|
struct lowpan_frag_queue *fq;
|
|
|
|
struct net *net = dev_net(skb->dev);
|
2015-09-02 12:21:25 +00:00
|
|
|
struct lowpan_802154_cb *cb = lowpan_802154_cb(skb);
|
2018-04-20 18:54:13 +00:00
|
|
|
struct ieee802154_hdr hdr = {};
|
2014-02-28 06:32:50 +00:00
|
|
|
int err;
|
|
|
|
|
2015-09-02 12:21:25 +00:00
|
|
|
if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
|
|
|
|
goto err;
|
2014-03-14 20:24:02 +00:00
|
|
|
|
2015-09-02 12:21:25 +00:00
|
|
|
err = lowpan_get_cb(skb, frag_type, cb);
|
2014-02-28 06:32:50 +00:00
|
|
|
if (err < 0)
|
|
|
|
goto err;
|
|
|
|
|
2015-09-02 12:21:25 +00: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-19 17:03:32 +00:00
|
|
|
net_warn_ratelimited("lowpan_frag_rcv: datagram size exceeds MTU\n");
|
2014-02-28 06:32:50 +00:00
|
|
|
goto err;
|
2014-08-19 17:03:32 +00:00
|
|
|
}
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2015-09-02 12:21:25 +00:00
|
|
|
fq = fq_find(net, cb, &hdr.source, &hdr.dest);
|
2014-02-28 06:32:50 +00:00
|
|
|
if (fq != NULL) {
|
|
|
|
int ret;
|
2014-07-02 03:31:09 +00:00
|
|
|
|
2014-02-28 06:32:50 +00:00
|
|
|
spin_lock(&fq->q.lock);
|
|
|
|
ret = lowpan_frag_queue(fq, skb, frag_type);
|
|
|
|
spin_unlock(&fq->q.lock);
|
|
|
|
|
2018-03-31 19:58:44 +00:00
|
|
|
inet_frag_put(&fq->q);
|
2014-02-28 06:32:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
err:
|
|
|
|
kfree_skb(skb);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_SYSCTL
|
2014-07-24 14:50:37 +00:00
|
|
|
|
2014-02-28 06:32:50 +00:00
|
|
|
static struct ctl_table lowpan_frags_ns_ctl_table[] = {
|
|
|
|
{
|
|
|
|
.procname = "6lowpanfrag_high_thresh",
|
2018-03-31 19:58:53 +00:00
|
|
|
.maxlen = sizeof(unsigned long),
|
2014-02-28 06:32:50 +00:00
|
|
|
.mode = 0644,
|
2018-03-31 19:58:53 +00:00
|
|
|
.proc_handler = proc_doulongvec_minmax,
|
2014-02-28 06:32:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.procname = "6lowpanfrag_low_thresh",
|
2018-03-31 19:58:53 +00:00
|
|
|
.maxlen = sizeof(unsigned long),
|
2014-02-28 06:32:50 +00:00
|
|
|
.mode = 0644,
|
2018-03-31 19:58:53 +00:00
|
|
|
.proc_handler = proc_doulongvec_minmax,
|
2014-02-28 06:32:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
.procname = "6lowpanfrag_time",
|
|
|
|
.maxlen = sizeof(int),
|
|
|
|
.mode = 0644,
|
|
|
|
.proc_handler = proc_dointvec_jiffies,
|
|
|
|
},
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
2014-07-24 14:50:35 +00:00
|
|
|
/* secret interval has been deprecated */
|
|
|
|
static int lowpan_frags_secret_interval_unused;
|
2014-02-28 06:32:50 +00:00
|
|
|
static struct ctl_table lowpan_frags_ctl_table[] = {
|
|
|
|
{
|
|
|
|
.procname = "6lowpanfrag_secret_interval",
|
2014-07-24 14:50:35 +00:00
|
|
|
.data = &lowpan_frags_secret_interval_unused,
|
2014-02-28 06:32:50 +00: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 01:22:54 +00:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
2014-02-28 06:32:50 +00: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-24 16:03:39 +00: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;
|
2019-05-24 16:03:36 +00:00
|
|
|
|
2014-02-28 06:32:50 +00:00
|
|
|
hdr = register_net_sysctl(net, "net/ieee802154/6lowpan", table);
|
|
|
|
if (hdr == NULL)
|
|
|
|
goto err_reg;
|
|
|
|
|
2014-04-18 01:22:54 +00:00
|
|
|
ieee802154_lowpan->sysctl.frags_hdr = hdr;
|
2014-02-28 06:32:50 +00: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 01:22:54 +00:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2014-04-18 01:22:54 +00:00
|
|
|
table = ieee802154_lowpan->sysctl.frags_hdr->ctl_table_arg;
|
|
|
|
unregister_net_sysctl_table(ieee802154_lowpan->sysctl.frags_hdr);
|
2014-02-28 06:32:50 +00:00
|
|
|
if (!net_eq(net, &init_net))
|
|
|
|
kfree(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct ctl_table_header *lowpan_ctl_header;
|
|
|
|
|
2014-09-30 20:34:08 +00:00
|
|
|
static int __init lowpan_frags_sysctl_register(void)
|
2014-02-28 06:32:50 +00: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 05:27:46 +00:00
|
|
|
static inline int lowpan_frags_ns_sysctl_register(struct net *net)
|
2014-02-28 06:32:50 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void lowpan_frags_ns_sysctl_unregister(struct net *net)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-01 05:27:46 +00:00
|
|
|
static inline int __init lowpan_frags_sysctl_register(void)
|
2014-02-28 06:32:50 +00: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 01:22:54 +00:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
2018-03-31 19:58:43 +00:00
|
|
|
int res;
|
2014-02-28 06:32:50 +00:00
|
|
|
|
2014-04-18 01:22:54 +00:00
|
|
|
|
2019-05-24 16:03:38 +00:00
|
|
|
res = fqdir_init(&ieee802154_lowpan->fqdir, &lowpan_frags, net);
|
2018-03-31 19:58:43 +00:00
|
|
|
if (res < 0)
|
|
|
|
return res;
|
2019-05-24 16:03:39 +00: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;
|
|
|
|
|
2018-03-31 19:58:43 +00:00
|
|
|
res = lowpan_frags_ns_sysctl_register(net);
|
|
|
|
if (res < 0)
|
2019-05-24 16:03:39 +00:00
|
|
|
fqdir_exit(ieee802154_lowpan->fqdir);
|
2018-03-31 19:58:43 +00:00
|
|
|
return res;
|
2014-02-28 06:32:50 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 18:09:00 +00:00
|
|
|
static void __net_exit lowpan_frags_pre_exit_net(struct net *net)
|
|
|
|
{
|
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
|
|
|
|
|
|
|
fqdir_pre_exit(ieee802154_lowpan->fqdir);
|
|
|
|
}
|
|
|
|
|
2014-02-28 06:32:50 +00:00
|
|
|
static void __net_exit lowpan_frags_exit_net(struct net *net)
|
|
|
|
{
|
2014-04-18 01:22:54 +00:00
|
|
|
struct netns_ieee802154_lowpan *ieee802154_lowpan =
|
|
|
|
net_ieee802154_lowpan(net);
|
|
|
|
|
2014-02-28 06:32:50 +00:00
|
|
|
lowpan_frags_ns_sysctl_unregister(net);
|
2019-05-24 16:03:39 +00:00
|
|
|
fqdir_exit(ieee802154_lowpan->fqdir);
|
2014-02-28 06:32:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct pernet_operations lowpan_frags_ops = {
|
2019-06-18 18:09:00 +00:00
|
|
|
.init = lowpan_frags_init_net,
|
|
|
|
.pre_exit = lowpan_frags_pre_exit_net,
|
|
|
|
.exit = lowpan_frags_exit_net,
|
2014-02-28 06:32:50 +00: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-03-31 19:58:49 +00: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 06:32:50 +00: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 10:29:48 +00: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-03-31 19:58:49 +00:00
|
|
|
lowpan_frags.rhash_params = lowpan_rhash_params;
|
2014-08-01 10:29:48 +00:00
|
|
|
ret = inet_frags_init(&lowpan_frags);
|
|
|
|
if (ret)
|
2018-03-31 19:58:46 +00:00
|
|
|
goto out;
|
2014-03-07 10:06:54 +00:00
|
|
|
|
2018-03-31 19:58:46 +00: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 10:06:54 +00:00
|
|
|
return ret;
|
2014-02-28 06:32:50 +00:00
|
|
|
err_pernet:
|
|
|
|
lowpan_frags_sysctl_unregister();
|
2018-03-31 19:58:46 +00:00
|
|
|
err_sysctl:
|
|
|
|
inet_frags_fini(&lowpan_frags);
|
2014-02-28 06:32:50 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void lowpan_net_frag_exit(void)
|
|
|
|
{
|
|
|
|
lowpan_frags_sysctl_unregister();
|
|
|
|
unregister_pernet_subsys(&lowpan_frags_ops);
|
2019-05-27 23:56:48 +00:00
|
|
|
inet_frags_fini(&lowpan_frags);
|
2014-02-28 06:32:50 +00:00
|
|
|
}
|