162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef __NET_FRAG_H__
362306a36Sopenharmony_ci#define __NET_FRAG_H__
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#include <linux/rhashtable-types.h>
662306a36Sopenharmony_ci#include <linux/completion.h>
762306a36Sopenharmony_ci#include <linux/in6.h>
862306a36Sopenharmony_ci#include <linux/rbtree_types.h>
962306a36Sopenharmony_ci#include <linux/refcount.h>
1062306a36Sopenharmony_ci#include <net/dropreason-core.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/* Per netns frag queues directory */
1362306a36Sopenharmony_cistruct fqdir {
1462306a36Sopenharmony_ci	/* sysctls */
1562306a36Sopenharmony_ci	long			high_thresh;
1662306a36Sopenharmony_ci	long			low_thresh;
1762306a36Sopenharmony_ci	int			timeout;
1862306a36Sopenharmony_ci	int			max_dist;
1962306a36Sopenharmony_ci	struct inet_frags	*f;
2062306a36Sopenharmony_ci	struct net		*net;
2162306a36Sopenharmony_ci	bool			dead;
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci	struct rhashtable       rhashtable ____cacheline_aligned_in_smp;
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci	/* Keep atomic mem on separate cachelines in structs that include it */
2662306a36Sopenharmony_ci	atomic_long_t		mem ____cacheline_aligned_in_smp;
2762306a36Sopenharmony_ci	struct work_struct	destroy_work;
2862306a36Sopenharmony_ci	struct llist_node	free_list;
2962306a36Sopenharmony_ci};
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci/**
3262306a36Sopenharmony_ci * enum: fragment queue flags
3362306a36Sopenharmony_ci *
3462306a36Sopenharmony_ci * @INET_FRAG_FIRST_IN: first fragment has arrived
3562306a36Sopenharmony_ci * @INET_FRAG_LAST_IN: final fragment has arrived
3662306a36Sopenharmony_ci * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
3762306a36Sopenharmony_ci * @INET_FRAG_HASH_DEAD: inet_frag_kill() has not removed fq from rhashtable
3862306a36Sopenharmony_ci * @INET_FRAG_DROP: if skbs must be dropped (instead of being consumed)
3962306a36Sopenharmony_ci */
4062306a36Sopenharmony_cienum {
4162306a36Sopenharmony_ci	INET_FRAG_FIRST_IN	= BIT(0),
4262306a36Sopenharmony_ci	INET_FRAG_LAST_IN	= BIT(1),
4362306a36Sopenharmony_ci	INET_FRAG_COMPLETE	= BIT(2),
4462306a36Sopenharmony_ci	INET_FRAG_HASH_DEAD	= BIT(3),
4562306a36Sopenharmony_ci	INET_FRAG_DROP		= BIT(4),
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistruct frag_v4_compare_key {
4962306a36Sopenharmony_ci	__be32		saddr;
5062306a36Sopenharmony_ci	__be32		daddr;
5162306a36Sopenharmony_ci	u32		user;
5262306a36Sopenharmony_ci	u32		vif;
5362306a36Sopenharmony_ci	__be16		id;
5462306a36Sopenharmony_ci	u16		protocol;
5562306a36Sopenharmony_ci};
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistruct frag_v6_compare_key {
5862306a36Sopenharmony_ci	struct in6_addr	saddr;
5962306a36Sopenharmony_ci	struct in6_addr	daddr;
6062306a36Sopenharmony_ci	u32		user;
6162306a36Sopenharmony_ci	__be32		id;
6262306a36Sopenharmony_ci	u32		iif;
6362306a36Sopenharmony_ci};
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/**
6662306a36Sopenharmony_ci * struct inet_frag_queue - fragment queue
6762306a36Sopenharmony_ci *
6862306a36Sopenharmony_ci * @node: rhash node
6962306a36Sopenharmony_ci * @key: keys identifying this frag.
7062306a36Sopenharmony_ci * @timer: queue expiration timer
7162306a36Sopenharmony_ci * @lock: spinlock protecting this frag
7262306a36Sopenharmony_ci * @refcnt: reference count of the queue
7362306a36Sopenharmony_ci * @rb_fragments: received fragments rb-tree root
7462306a36Sopenharmony_ci * @fragments_tail: received fragments tail
7562306a36Sopenharmony_ci * @last_run_head: the head of the last "run". see ip_fragment.c
7662306a36Sopenharmony_ci * @stamp: timestamp of the last received fragment
7762306a36Sopenharmony_ci * @len: total length of the original datagram
7862306a36Sopenharmony_ci * @meat: length of received fragments so far
7962306a36Sopenharmony_ci * @mono_delivery_time: stamp has a mono delivery time (EDT)
8062306a36Sopenharmony_ci * @flags: fragment queue flags
8162306a36Sopenharmony_ci * @max_size: maximum received fragment size
8262306a36Sopenharmony_ci * @fqdir: pointer to struct fqdir
8362306a36Sopenharmony_ci * @rcu: rcu head for freeing deferall
8462306a36Sopenharmony_ci */
8562306a36Sopenharmony_cistruct inet_frag_queue {
8662306a36Sopenharmony_ci	struct rhash_head	node;
8762306a36Sopenharmony_ci	union {
8862306a36Sopenharmony_ci		struct frag_v4_compare_key v4;
8962306a36Sopenharmony_ci		struct frag_v6_compare_key v6;
9062306a36Sopenharmony_ci	} key;
9162306a36Sopenharmony_ci	struct timer_list	timer;
9262306a36Sopenharmony_ci	spinlock_t		lock;
9362306a36Sopenharmony_ci	refcount_t		refcnt;
9462306a36Sopenharmony_ci	struct rb_root		rb_fragments;
9562306a36Sopenharmony_ci	struct sk_buff		*fragments_tail;
9662306a36Sopenharmony_ci	struct sk_buff		*last_run_head;
9762306a36Sopenharmony_ci	ktime_t			stamp;
9862306a36Sopenharmony_ci	int			len;
9962306a36Sopenharmony_ci	int			meat;
10062306a36Sopenharmony_ci	u8			mono_delivery_time;
10162306a36Sopenharmony_ci	__u8			flags;
10262306a36Sopenharmony_ci	u16			max_size;
10362306a36Sopenharmony_ci	struct fqdir		*fqdir;
10462306a36Sopenharmony_ci	struct rcu_head		rcu;
10562306a36Sopenharmony_ci};
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_cistruct inet_frags {
10862306a36Sopenharmony_ci	unsigned int		qsize;
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	void			(*constructor)(struct inet_frag_queue *q,
11162306a36Sopenharmony_ci					       const void *arg);
11262306a36Sopenharmony_ci	void			(*destructor)(struct inet_frag_queue *);
11362306a36Sopenharmony_ci	void			(*frag_expire)(struct timer_list *t);
11462306a36Sopenharmony_ci	struct kmem_cache	*frags_cachep;
11562306a36Sopenharmony_ci	const char		*frags_cache_name;
11662306a36Sopenharmony_ci	struct rhashtable_params rhash_params;
11762306a36Sopenharmony_ci	refcount_t		refcnt;
11862306a36Sopenharmony_ci	struct completion	completion;
11962306a36Sopenharmony_ci};
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ciint inet_frags_init(struct inet_frags *);
12262306a36Sopenharmony_civoid inet_frags_fini(struct inet_frags *);
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciint fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistatic inline void fqdir_pre_exit(struct fqdir *fqdir)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	/* Prevent creation of new frags.
12962306a36Sopenharmony_ci	 * Pairs with READ_ONCE() in inet_frag_find().
13062306a36Sopenharmony_ci	 */
13162306a36Sopenharmony_ci	WRITE_ONCE(fqdir->high_thresh, 0);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	/* Pairs with READ_ONCE() in inet_frag_kill(), ip_expire()
13462306a36Sopenharmony_ci	 * and ip6frag_expire_frag_queue().
13562306a36Sopenharmony_ci	 */
13662306a36Sopenharmony_ci	WRITE_ONCE(fqdir->dead, true);
13762306a36Sopenharmony_ci}
13862306a36Sopenharmony_civoid fqdir_exit(struct fqdir *fqdir);
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_civoid inet_frag_kill(struct inet_frag_queue *q);
14162306a36Sopenharmony_civoid inet_frag_destroy(struct inet_frag_queue *q);
14262306a36Sopenharmony_cistruct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci/* Free all skbs in the queue; return the sum of their truesizes. */
14562306a36Sopenharmony_ciunsigned int inet_frag_rbtree_purge(struct rb_root *root,
14662306a36Sopenharmony_ci				    enum skb_drop_reason reason);
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_cistatic inline void inet_frag_put(struct inet_frag_queue *q)
14962306a36Sopenharmony_ci{
15062306a36Sopenharmony_ci	if (refcount_dec_and_test(&q->refcnt))
15162306a36Sopenharmony_ci		inet_frag_destroy(q);
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ci/* Memory Tracking Functions. */
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_cistatic inline long frag_mem_limit(const struct fqdir *fqdir)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	return atomic_long_read(&fqdir->mem);
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cistatic inline void sub_frag_mem_limit(struct fqdir *fqdir, long val)
16262306a36Sopenharmony_ci{
16362306a36Sopenharmony_ci	atomic_long_sub(val, &fqdir->mem);
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic inline void add_frag_mem_limit(struct fqdir *fqdir, long val)
16762306a36Sopenharmony_ci{
16862306a36Sopenharmony_ci	atomic_long_add(val, &fqdir->mem);
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci/* RFC 3168 support :
17262306a36Sopenharmony_ci * We want to check ECN values of all fragments, do detect invalid combinations.
17362306a36Sopenharmony_ci * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
17462306a36Sopenharmony_ci */
17562306a36Sopenharmony_ci#define	IPFRAG_ECN_NOT_ECT	0x01 /* one frag had ECN_NOT_ECT */
17662306a36Sopenharmony_ci#define	IPFRAG_ECN_ECT_1	0x02 /* one frag had ECN_ECT_1 */
17762306a36Sopenharmony_ci#define	IPFRAG_ECN_ECT_0	0x04 /* one frag had ECN_ECT_0 */
17862306a36Sopenharmony_ci#define	IPFRAG_ECN_CE		0x08 /* one frag had ECN_CE */
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_ciextern const u8 ip_frag_ecn_table[16];
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci/* Return values of inet_frag_queue_insert() */
18362306a36Sopenharmony_ci#define IPFRAG_OK	0
18462306a36Sopenharmony_ci#define IPFRAG_DUP	1
18562306a36Sopenharmony_ci#define IPFRAG_OVERLAP	2
18662306a36Sopenharmony_ciint inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
18762306a36Sopenharmony_ci			   int offset, int end);
18862306a36Sopenharmony_civoid *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
18962306a36Sopenharmony_ci			      struct sk_buff *parent);
19062306a36Sopenharmony_civoid inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
19162306a36Sopenharmony_ci			    void *reasm_data, bool try_coalesce);
19262306a36Sopenharmony_cistruct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q);
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci#endif
195