License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / include / net / inet_frag.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
5ab11c98
PE
2#ifndef __NET_FRAG_H__
3#define __NET_FRAG_H__
4
ac18e750 5struct netns_frags {
fb452a1a
JDB
6 /* Keep atomic mem on separate cachelines in structs that include it */
7 atomic_t mem ____cacheline_aligned_in_smp;
b2fd5321
PE
8 /* sysctls */
9 int timeout;
e31e0bdc
PE
10 int high_thresh;
11 int low_thresh;
0fbf4cb2 12 int max_dist;
ac18e750
PE
13};
14
1ab1934e
NA
15/**
16 * fragment queue flags
17 *
18 * @INET_FRAG_FIRST_IN: first fragment has arrived
19 * @INET_FRAG_LAST_IN: final fragment has arrived
20 * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
1ab1934e
NA
21 */
22enum {
23 INET_FRAG_FIRST_IN = BIT(0),
24 INET_FRAG_LAST_IN = BIT(1),
25 INET_FRAG_COMPLETE = BIT(2),
1ab1934e
NA
26};
27
28/**
29 * struct inet_frag_queue - fragment queue
30 *
31 * @lock: spinlock protecting the queue
32 * @timer: queue expiration timer
33 * @list: hash bucket list
34 * @refcnt: reference count of the queue
35 * @fragments: received fragments head
36 * @fragments_tail: received fragments tail
37 * @stamp: timestamp of the last received fragment
38 * @len: total length of the original datagram
39 * @meat: length of received fragments so far
40 * @flags: fragment queue flags
d6b915e2 41 * @max_size: maximum received fragment size
1ab1934e 42 * @net: namespace that this frag belongs to
d1fe1944 43 * @list_evictor: list of queues to forcefully evict (e.g. due to low memory)
1ab1934e 44 */
5ab11c98 45struct inet_frag_queue {
5ab11c98 46 spinlock_t lock;
1ab1934e 47 struct timer_list timer;
6e34a8b3 48 struct hlist_node list;
edcb6918 49 refcount_t refcnt;
1ab1934e 50 struct sk_buff *fragments;
d6bebca9 51 struct sk_buff *fragments_tail;
5ab11c98 52 ktime_t stamp;
1ab1934e 53 int len;
5ab11c98 54 int meat;
1ab1934e 55 __u8 flags;
5f2d04f1 56 u16 max_size;
6e34a8b3 57 struct netns_frags *net;
d1fe1944 58 struct hlist_node list_evictor;
5ab11c98
PE
59};
60
a4c4009f 61#define INETFRAGS_HASHSZ 1024
7eb95156 62
5a3da1fe
HFS
63/* averaged:
64 * max_depth = default ipfrag_high_thresh / INETFRAGS_HASHSZ /
65 * rounded up (SKB_TRUELEN(0) + sizeof(struct ipq or
66 * struct frag_queue))
67 */
b13d3cbf 68#define INETFRAGS_MAXDEPTH 128
5a3da1fe 69
19952cc4
JDB
70struct inet_frag_bucket {
71 struct hlist_head chain;
72 spinlock_t chain_lock;
73};
74
7eb95156 75struct inet_frags {
19952cc4 76 struct inet_frag_bucket hash[INETFRAGS_HASHSZ];
7088ad74 77
b13d3cbf
FW
78 struct work_struct frags_work;
79 unsigned int next_bucket;
e3a57d18
FW
80 unsigned long last_rebuild_jiffies;
81 bool rebuild;
b13d3cbf 82
7088ad74
HFS
83 /* The first call to hashfn is responsible to initialize
84 * rnd. This is best done with net_get_random_once.
ab1c724f
FW
85 *
86 * rnd_seqlock is used to let hash insertion detect
87 * when it needs to re-lookup the hash chain to use.
7088ad74 88 */
5f8e1e8b 89 u32 rnd;
ab1c724f 90 seqlock_t rnd_seqlock;
4c0ebd6f 91 unsigned int qsize;
321a3a99 92
36c77782
FW
93 unsigned int (*hashfn)(const struct inet_frag_queue *);
94 bool (*match)(const struct inet_frag_queue *q,
95 const void *arg);
c6fda282 96 void (*constructor)(struct inet_frag_queue *q,
36c77782 97 const void *arg);
1e4b8287 98 void (*destructor)(struct inet_frag_queue *);
e521db9d 99 void (*frag_expire)(unsigned long data);
d4ad4d22
NA
100 struct kmem_cache *frags_cachep;
101 const char *frags_cache_name;
7eb95156
PE
102};
103
d4ad4d22 104int inet_frags_init(struct inet_frags *);
7eb95156
PE
105void inet_frags_fini(struct inet_frags *);
106
5a63643e 107static inline void inet_frags_init_net(struct netns_frags *nf)
1d6119ba 108{
fb452a1a 109 atomic_set(&nf->mem, 0);
1d6119ba 110}
81566e83 111void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f);
e5a2bb84 112
277e650d 113void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);
3fd588eb 114void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f);
ac18e750 115struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
ab1c724f
FW
116 struct inet_frags *f, void *key, unsigned int hash);
117
5a3da1fe
HFS
118void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
119 const char *prefix);
277e650d 120
762cc408
PE
121static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
122{
edcb6918 123 if (refcount_dec_and_test(&q->refcnt))
3fd588eb 124 inet_frag_destroy(q, f);
762cc408
PE
125}
126
caaecdd3
NA
127static inline bool inet_frag_evicting(struct inet_frag_queue *q)
128{
129 return !hlist_unhashed(&q->list_evictor);
130}
131
d433673e
JDB
132/* Memory Tracking Functions. */
133
134static inline int frag_mem_limit(struct netns_frags *nf)
135{
fb452a1a 136 return atomic_read(&nf->mem);
d433673e
JDB
137}
138
0e60d245 139static inline void sub_frag_mem_limit(struct netns_frags *nf, int i)
d433673e 140{
fb452a1a 141 atomic_sub(i, &nf->mem);
d433673e
JDB
142}
143
0e60d245 144static inline void add_frag_mem_limit(struct netns_frags *nf, int i)
d433673e 145{
fb452a1a 146 atomic_add(i, &nf->mem);
d433673e
JDB
147}
148
fb452a1a 149static inline int sum_frag_mem_limit(struct netns_frags *nf)
d433673e 150{
fb452a1a 151 return atomic_read(&nf->mem);
d433673e
JDB
152}
153
be991971
HFS
154/* RFC 3168 support :
155 * We want to check ECN values of all fragments, do detect invalid combinations.
156 * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
157 */
158#define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
159#define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
160#define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
161#define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
162
163extern const u8 ip_frag_ecn_table[16];
164
5ab11c98 165#endif