1 /* SPDX-License-Identifier: GPL-2.0 */
5 #include <linux/rhashtable-types.h>
6 #include <linux/completion.h>
8 #include <linux/rbtree_types.h>
9 #include <linux/refcount.h>
11 /* Per netns frag queues directory */
22 struct rhashtable rhashtable ____cacheline_aligned_in_smp;
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;
31 * fragment queue flags
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
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),
45 struct frag_v4_compare_key {
54 struct frag_v6_compare_key {
55 struct in6_addr saddr;
56 struct in6_addr daddr;
63 * struct inet_frag_queue - fragment queue
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
82 struct inet_frag_queue {
83 struct rhash_head node;
85 struct frag_v4_compare_key v4;
86 struct frag_v6_compare_key v6;
88 struct timer_list timer;
91 struct rb_root rb_fragments;
92 struct sk_buff *fragments_tail;
93 struct sk_buff *last_run_head;
97 u8 mono_delivery_time;
107 void (*constructor)(struct inet_frag_queue *q,
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;
115 struct completion completion;
118 int inet_frags_init(struct inet_frags *);
119 void inet_frags_fini(struct inet_frags *);
121 int fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net);
123 static inline void fqdir_pre_exit(struct fqdir *fqdir)
125 /* Prevent creation of new frags.
126 * Pairs with READ_ONCE() in inet_frag_find().
128 WRITE_ONCE(fqdir->high_thresh, 0);
130 /* Pairs with READ_ONCE() in inet_frag_kill(), ip_expire()
131 * and ip6frag_expire_frag_queue().
133 WRITE_ONCE(fqdir->dead, true);
135 void fqdir_exit(struct fqdir *fqdir);
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);
141 /* Free all skbs in the queue; return the sum of their truesizes. */
142 unsigned int inet_frag_rbtree_purge(struct rb_root *root);
144 static inline void inet_frag_put(struct inet_frag_queue *q)
146 if (refcount_dec_and_test(&q->refcnt))
147 inet_frag_destroy(q);
150 /* Memory Tracking Functions. */
152 static inline long frag_mem_limit(const struct fqdir *fqdir)
154 return atomic_long_read(&fqdir->mem);
157 static inline void sub_frag_mem_limit(struct fqdir *fqdir, long val)
159 atomic_long_sub(val, &fqdir->mem);
162 static inline void add_frag_mem_limit(struct fqdir *fqdir, long val)
164 atomic_long_add(val, &fqdir->mem);
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.
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 */
176 extern const u8 ip_frag_ecn_table[16];
178 /* Return values of inet_frag_queue_insert() */
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);