blob: c2c724abde579cba78e4aeb5516930a7e6cb3ff0 [file] [log] [blame]
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -07001#ifndef __NET_FRAG_H__
2#define __NET_FRAG_H__
3
Eric Dumazet23ce9c52018-10-10 12:29:56 -07004#include <linux/rhashtable.h>
5
Pavel Emelyanovac18e752008-01-22 06:02:14 -08006struct netns_frags {
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -08007 /* sysctls */
Eric Dumazet7f617062018-10-10 12:30:00 -07008 long high_thresh;
9 long low_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -080010 int timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +020011 int max_dist;
Eric Dumazet2ffb1c32018-10-10 12:29:50 -070012 struct inet_frags *f;
Eric Dumazet5b68fda2018-10-10 12:30:04 -070013
14 struct rhashtable rhashtable ____cacheline_aligned_in_smp;
15
16 /* Keep atomic mem on separate cachelines in structs that include it */
17 atomic_long_t mem ____cacheline_aligned_in_smp;
Pavel Emelyanovac18e752008-01-22 06:02:14 -080018};
19
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020020/**
21 * fragment queue flags
22 *
23 * @INET_FRAG_FIRST_IN: first fragment has arrived
24 * @INET_FRAG_LAST_IN: final fragment has arrived
25 * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020026 */
27enum {
28 INET_FRAG_FIRST_IN = BIT(0),
29 INET_FRAG_LAST_IN = BIT(1),
30 INET_FRAG_COMPLETE = BIT(2),
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020031};
32
Eric Dumazet23ce9c52018-10-10 12:29:56 -070033struct frag_v4_compare_key {
34 __be32 saddr;
35 __be32 daddr;
36 u32 user;
37 u32 vif;
38 __be16 id;
39 u16 protocol;
40};
41
42struct frag_v6_compare_key {
43 struct in6_addr saddr;
44 struct in6_addr daddr;
45 u32 user;
46 __be32 id;
47 u32 iif;
48};
49
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020050/**
51 * struct inet_frag_queue - fragment queue
52 *
Eric Dumazet23ce9c52018-10-10 12:29:56 -070053 * @node: rhash node
54 * @key: keys identifying this frag.
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020055 * @timer: queue expiration timer
Eric Dumazet23ce9c52018-10-10 12:29:56 -070056 * @lock: spinlock protecting this frag
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020057 * @refcnt: reference count of the queue
58 * @fragments: received fragments head
Peter Oskolkove9e4ac42018-10-10 12:30:14 -070059 * @rb_fragments: received fragments rb-tree root
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020060 * @fragments_tail: received fragments tail
Peter Oskolkove9e4ac42018-10-10 12:30:14 -070061 * @last_run_head: the head of the last "run". see ip_fragment.c
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020062 * @stamp: timestamp of the last received fragment
63 * @len: total length of the original datagram
64 * @meat: length of received fragments so far
65 * @flags: fragment queue flags
Florian Westphald6b915e2015-05-22 16:32:51 +020066 * @max_size: maximum received fragment size
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020067 * @net: namespace that this frag belongs to
Eric Dumazet23ce9c52018-10-10 12:29:56 -070068 * @rcu: rcu head for freeing deferall
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020069 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070070struct inet_frag_queue {
Eric Dumazet23ce9c52018-10-10 12:29:56 -070071 struct rhash_head node;
72 union {
73 struct frag_v4_compare_key v4;
74 struct frag_v6_compare_key v6;
75 } key;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020076 struct timer_list timer;
Eric Dumazet23ce9c52018-10-10 12:29:56 -070077 spinlock_t lock;
Jesper Dangaard Brouer6e34a8b2013-01-28 23:44:49 +000078 atomic_t refcnt;
Peter Oskolkovaaee29e2019-04-26 08:41:05 -070079 struct sk_buff *fragments; /* used in 6lopwpan IPv6. */
80 struct rb_root rb_fragments; /* Used in IPv4/IPv6. */
Changli Gaod6bebca2010-06-29 04:39:37 +000081 struct sk_buff *fragments_tail;
Peter Oskolkove9e4ac42018-10-10 12:30:14 -070082 struct sk_buff *last_run_head;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070083 ktime_t stamp;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020084 int len;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070085 int meat;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020086 __u8 flags;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +020087 u16 max_size;
Eric Dumazet23ce9c52018-10-10 12:29:56 -070088 struct netns_frags *net;
89 struct rcu_head rcu;
Jesper Dangaard Brouer19952cc42013-04-03 23:38:16 +000090};
91
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070092struct inet_frags {
Jesper Dangaard Brouer5f8e1e82013-01-28 23:44:37 +000093 int qsize;
Pavel Emelyanov321a3a92007-10-15 02:38:08 -070094
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070095 void (*constructor)(struct inet_frag_queue *q,
Florian Westphal36c77782014-07-24 16:50:29 +020096 const void *arg);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -070097 void (*destructor)(struct inet_frag_queue *);
Pavel Emelyanove521db92007-10-17 19:45:23 -070098 void (*frag_expire)(unsigned long data);
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020099 struct kmem_cache *frags_cachep;
100 const char *frags_cache_name;
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700101 struct rhashtable_params rhash_params;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700102};
103
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200104int inet_frags_init(struct inet_frags *);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700105void inet_frags_fini(struct inet_frags *);
106
Eric Dumazet7fca7712018-10-10 12:29:49 -0700107static inline int inet_frags_init_net(struct netns_frags *nf)
Eric Dumazet1d6119b2015-11-02 09:03:11 -0800108{
Eric Dumazet7f617062018-10-10 12:30:00 -0700109 atomic_long_set(&nf->mem, 0);
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700110 return rhashtable_init(&nf->rhashtable, &nf->f->rhash_params);
Eric Dumazet1d6119b2015-11-02 09:03:11 -0800111}
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700112void inet_frags_exit_net(struct netns_frags *nf);
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800113
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700114void inet_frag_kill(struct inet_frag_queue *q);
115void inet_frag_destroy(struct inet_frag_queue *q);
Eric Dumazet23ce9c52018-10-10 12:29:56 -0700116struct inet_frag_queue *inet_frag_find(struct netns_frags *nf, void *key);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700117
Peter Oskolkove9e4ac42018-10-10 12:30:14 -0700118/* Free all skbs in the queue; return the sum of their truesizes. */
119unsigned int inet_frag_rbtree_purge(struct rb_root *root);
120
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700121static inline void inet_frag_put(struct inet_frag_queue *q)
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700122{
123 if (atomic_dec_and_test(&q->refcnt))
Eric Dumazet2ffb1c32018-10-10 12:29:50 -0700124 inet_frag_destroy(q);
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700125}
126
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000127/* Memory Tracking Functions. */
128
Eric Dumazet7f617062018-10-10 12:30:00 -0700129static inline long frag_mem_limit(const struct netns_frags *nf)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000130{
Eric Dumazet7f617062018-10-10 12:30:00 -0700131 return atomic_long_read(&nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000132}
133
Eric Dumazet7f617062018-10-10 12:30:00 -0700134static inline void sub_frag_mem_limit(struct netns_frags *nf, long val)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000135{
Eric Dumazet7f617062018-10-10 12:30:00 -0700136 atomic_long_sub(val, &nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000137}
138
Eric Dumazet7f617062018-10-10 12:30:00 -0700139static inline void add_frag_mem_limit(struct netns_frags *nf, long val)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000140{
Eric Dumazet7f617062018-10-10 12:30:00 -0700141 atomic_long_add(val, &nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000142}
143
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000144/* RFC 3168 support :
145 * We want to check ECN values of all fragments, do detect invalid combinations.
146 * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
147 */
148#define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
149#define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
150#define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
151#define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
152
153extern const u8 ip_frag_ecn_table[16];
154
Peter Oskolkovaaee29e2019-04-26 08:41:05 -0700155/* Return values of inet_frag_queue_insert() */
156#define IPFRAG_OK 0
157#define IPFRAG_DUP 1
158#define IPFRAG_OVERLAP 2
159int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
160 int offset, int end);
161void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
162 struct sk_buff *parent);
163void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
164 void *reasm_data);
165struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q);
166
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700167#endif