xref: /linux/include/net/inet_frag.h (revision 6c8c1406)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __NET_FRAG_H__
3 #define __NET_FRAG_H__
4 
5 #include <linux/rhashtable-types.h>
6 #include <linux/completion.h>
7 #include <linux/in6.h>
8 #include <linux/rbtree_types.h>
9 #include <linux/refcount.h>
10 
11 /* Per netns frag queues directory */
12 struct fqdir {
13 	/* sysctls */
14 	long			high_thresh;
15 	long			low_thresh;
16 	int			timeout;
17 	int			max_dist;
18 	struct inet_frags	*f;
19 	struct net		*net;
20 	bool			dead;
21 
22 	struct rhashtable       rhashtable ____cacheline_aligned_in_smp;
23 
24 	/* Keep atomic mem on separate cachelines in structs that include it */
25 	atomic_long_t		mem ____cacheline_aligned_in_smp;
26 	struct work_struct	destroy_work;
27 	struct llist_node	free_list;
28 };
29 
30 /**
31  * fragment queue flags
32  *
33  * @INET_FRAG_FIRST_IN: first fragment has arrived
34  * @INET_FRAG_LAST_IN: final fragment has arrived
35  * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
36  * @INET_FRAG_HASH_DEAD: inet_frag_kill() has not removed fq from rhashtable
37  */
38 enum {
39 	INET_FRAG_FIRST_IN	= BIT(0),
40 	INET_FRAG_LAST_IN	= BIT(1),
41 	INET_FRAG_COMPLETE	= BIT(2),
42 	INET_FRAG_HASH_DEAD	= BIT(3),
43 };
44 
45 struct frag_v4_compare_key {
46 	__be32		saddr;
47 	__be32		daddr;
48 	u32		user;
49 	u32		vif;
50 	__be16		id;
51 	u16		protocol;
52 };
53 
54 struct frag_v6_compare_key {
55 	struct in6_addr	saddr;
56 	struct in6_addr	daddr;
57 	u32		user;
58 	__be32		id;
59 	u32		iif;
60 };
61 
62 /**
63  * struct inet_frag_queue - fragment queue
64  *
65  * @node: rhash node
66  * @key: keys identifying this frag.
67  * @timer: queue expiration timer
68  * @lock: spinlock protecting this frag
69  * @refcnt: reference count of the queue
70  * @rb_fragments: received fragments rb-tree root
71  * @fragments_tail: received fragments tail
72  * @last_run_head: the head of the last "run". see ip_fragment.c
73  * @stamp: timestamp of the last received fragment
74  * @len: total length of the original datagram
75  * @meat: length of received fragments so far
76  * @mono_delivery_time: stamp has a mono delivery time (EDT)
77  * @flags: fragment queue flags
78  * @max_size: maximum received fragment size
79  * @fqdir: pointer to struct fqdir
80  * @rcu: rcu head for freeing deferall
81  */
82 struct inet_frag_queue {
83 	struct rhash_head	node;
84 	union {
85 		struct frag_v4_compare_key v4;
86 		struct frag_v6_compare_key v6;
87 	} key;
88 	struct timer_list	timer;
89 	spinlock_t		lock;
90 	refcount_t		refcnt;
91 	struct rb_root		rb_fragments;
92 	struct sk_buff		*fragments_tail;
93 	struct sk_buff		*last_run_head;
94 	ktime_t			stamp;
95 	int			len;
96 	int			meat;
97 	u8			mono_delivery_time;
98 	__u8			flags;
99 	u16			max_size;
100 	struct fqdir		*fqdir;
101 	struct rcu_head		rcu;
102 };
103 
104 struct inet_frags {
105 	unsigned int		qsize;
106 
107 	void			(*constructor)(struct inet_frag_queue *q,
108 					       const void *arg);
109 	void			(*destructor)(struct inet_frag_queue *);
110 	void			(*frag_expire)(struct timer_list *t);
111 	struct kmem_cache	*frags_cachep;
112 	const char		*frags_cache_name;
113 	struct rhashtable_params rhash_params;
114 	refcount_t		refcnt;
115 	struct completion	completion;
116 };
117 
118 int inet_frags_init(struct inet_frags *);
119 void inet_frags_fini(struct inet_frags *);
120 
121 int fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net);
122 
123 static inline void fqdir_pre_exit(struct fqdir *fqdir)
124 {
125 	/* Prevent creation of new frags.
126 	 * Pairs with READ_ONCE() in inet_frag_find().
127 	 */
128 	WRITE_ONCE(fqdir->high_thresh, 0);
129 
130 	/* Pairs with READ_ONCE() in inet_frag_kill(), ip_expire()
131 	 * and ip6frag_expire_frag_queue().
132 	 */
133 	WRITE_ONCE(fqdir->dead, true);
134 }
135 void fqdir_exit(struct fqdir *fqdir);
136 
137 void inet_frag_kill(struct inet_frag_queue *q);
138 void inet_frag_destroy(struct inet_frag_queue *q);
139 struct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key);
140 
141 /* Free all skbs in the queue; return the sum of their truesizes. */
142 unsigned int inet_frag_rbtree_purge(struct rb_root *root);
143 
144 static inline void inet_frag_put(struct inet_frag_queue *q)
145 {
146 	if (refcount_dec_and_test(&q->refcnt))
147 		inet_frag_destroy(q);
148 }
149 
150 /* Memory Tracking Functions. */
151 
152 static inline long frag_mem_limit(const struct fqdir *fqdir)
153 {
154 	return atomic_long_read(&fqdir->mem);
155 }
156 
157 static inline void sub_frag_mem_limit(struct fqdir *fqdir, long val)
158 {
159 	atomic_long_sub(val, &fqdir->mem);
160 }
161 
162 static inline void add_frag_mem_limit(struct fqdir *fqdir, long val)
163 {
164 	atomic_long_add(val, &fqdir->mem);
165 }
166 
167 /* RFC 3168 support :
168  * We want to check ECN values of all fragments, do detect invalid combinations.
169  * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
170  */
171 #define	IPFRAG_ECN_NOT_ECT	0x01 /* one frag had ECN_NOT_ECT */
172 #define	IPFRAG_ECN_ECT_1	0x02 /* one frag had ECN_ECT_1 */
173 #define	IPFRAG_ECN_ECT_0	0x04 /* one frag had ECN_ECT_0 */
174 #define	IPFRAG_ECN_CE		0x08 /* one frag had ECN_CE */
175 
176 extern const u8 ip_frag_ecn_table[16];
177 
178 /* Return values of inet_frag_queue_insert() */
179 #define IPFRAG_OK	0
180 #define IPFRAG_DUP	1
181 #define IPFRAG_OVERLAP	2
182 int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
183 			   int offset, int end);
184 void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
185 			      struct sk_buff *parent);
186 void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
187 			    void *reasm_data, bool try_coalesce);
188 struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q);
189 
190 #endif
191