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