blob: 010f26b31c89b375f4cabf6572d51f0383c93f0e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -07002#ifndef __NET_FRAG_H__
3#define __NET_FRAG_H__
4
NeilBrown0eb71a92018-06-18 12:52:50 +10005#include <linux/rhashtable-types.h>
Eric Dumazetdc93f462019-05-27 16:56:49 -07006#include <linux/completion.h>
Eric Dumazet648700f2018-03-31 12:58:49 -07007
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -07008/* Per netns frag queues directory */
9struct fqdir {
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -080010 /* sysctls */
Eric Dumazet3e67f102018-03-31 12:58:53 -070011 long high_thresh;
12 long low_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -080013 int timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +020014 int max_dist;
Eric Dumazet093ba722018-03-31 12:58:44 -070015 struct inet_frags *f;
Eric Dumazeta39aca62019-05-24 09:03:38 -070016 struct net *net;
Eric Dumazet3c8fc872019-05-24 09:03:40 -070017 bool dead;
Eric Dumazetc2615cf2018-03-31 12:58:57 -070018
19 struct rhashtable rhashtable ____cacheline_aligned_in_smp;
20
21 /* Keep atomic mem on separate cachelines in structs that include it */
22 atomic_long_t mem ____cacheline_aligned_in_smp;
Eric Dumazetd5dd8872019-06-18 11:09:00 -070023 struct work_struct destroy_work;
Pavel Emelyanovac18e752008-01-22 06:02:14 -080024};
25
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020026/**
27 * fragment queue flags
28 *
29 * @INET_FRAG_FIRST_IN: first fragment has arrived
30 * @INET_FRAG_LAST_IN: final fragment has arrived
31 * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
Eric Dumazet3c8fc872019-05-24 09:03:40 -070032 * @INET_FRAG_HASH_DEAD: inet_frag_kill() has not removed fq from rhashtable
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020033 */
34enum {
35 INET_FRAG_FIRST_IN = BIT(0),
36 INET_FRAG_LAST_IN = BIT(1),
37 INET_FRAG_COMPLETE = BIT(2),
Eric Dumazet3c8fc872019-05-24 09:03:40 -070038 INET_FRAG_HASH_DEAD = BIT(3),
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020039};
40
Eric Dumazet648700f2018-03-31 12:58:49 -070041struct frag_v4_compare_key {
42 __be32 saddr;
43 __be32 daddr;
44 u32 user;
45 u32 vif;
46 __be16 id;
47 u16 protocol;
48};
49
50struct frag_v6_compare_key {
51 struct in6_addr saddr;
52 struct in6_addr daddr;
53 u32 user;
54 __be32 id;
55 u32 iif;
56};
57
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020058/**
59 * struct inet_frag_queue - fragment queue
60 *
Eric Dumazet648700f2018-03-31 12:58:49 -070061 * @node: rhash node
62 * @key: keys identifying this frag.
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020063 * @timer: queue expiration timer
Eric Dumazet648700f2018-03-31 12:58:49 -070064 * @lock: spinlock protecting this frag
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020065 * @refcnt: reference count of the queue
Peter Oskolkov353c9cb2018-08-11 20:27:24 +000066 * @rb_fragments: received fragments rb-tree root
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020067 * @fragments_tail: received fragments tail
Peter Oskolkov353c9cb2018-08-11 20:27:24 +000068 * @last_run_head: the head of the last "run". see ip_fragment.c
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020069 * @stamp: timestamp of the last received fragment
70 * @len: total length of the original datagram
71 * @meat: length of received fragments so far
72 * @flags: fragment queue flags
Florian Westphald6b915e2015-05-22 16:32:51 +020073 * @max_size: maximum received fragment size
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -070074 * @fqdir: pointer to struct fqdir
Eric Dumazet648700f2018-03-31 12:58:49 -070075 * @rcu: rcu head for freeing deferall
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020076 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070077struct inet_frag_queue {
Eric Dumazet648700f2018-03-31 12:58:49 -070078 struct rhash_head node;
79 union {
80 struct frag_v4_compare_key v4;
81 struct frag_v6_compare_key v6;
82 } key;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020083 struct timer_list timer;
Eric Dumazet648700f2018-03-31 12:58:49 -070084 spinlock_t lock;
Reshetova, Elenaedcb6912017-06-30 13:08:07 +030085 refcount_t refcnt;
Peter Oskolkovd8cf7572019-02-25 17:43:46 -080086 struct rb_root rb_fragments;
Changli Gaod6bebca2010-06-29 04:39:37 +000087 struct sk_buff *fragments_tail;
Peter Oskolkov353c9cb2018-08-11 20:27:24 +000088 struct sk_buff *last_run_head;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070089 ktime_t stamp;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020090 int len;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070091 int meat;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020092 __u8 flags;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +020093 u16 max_size;
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -070094 struct fqdir *fqdir;
Eric Dumazet648700f2018-03-31 12:58:49 -070095 struct rcu_head rcu;
Jesper Dangaard Brouer19952cc2013-04-03 23:38:16 +000096};
97
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070098struct inet_frags {
Alexey Dobriyan4c0ebd62017-05-23 00:20:26 +030099 unsigned int qsize;
Pavel Emelyanov321a3a92007-10-15 02:38:08 -0700100
Pavel Emelyanovc6fda282007-10-17 19:46:47 -0700101 void (*constructor)(struct inet_frag_queue *q,
Florian Westphal36c77782014-07-24 16:50:29 +0200102 const void *arg);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -0700103 void (*destructor)(struct inet_frag_queue *);
Kees Cook78802012017-10-16 17:29:20 -0700104 void (*frag_expire)(struct timer_list *t);
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200105 struct kmem_cache *frags_cachep;
106 const char *frags_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -0700107 struct rhashtable_params rhash_params;
Eric Dumazetdc93f462019-05-27 16:56:49 -0700108 refcount_t refcnt;
109 struct completion completion;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700110};
111
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200112int inet_frags_init(struct inet_frags *);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700113void inet_frags_fini(struct inet_frags *);
114
Eric Dumazet6b73d192019-05-27 16:56:47 -0700115int fqdir_init(struct fqdir **fqdirp, struct inet_frags *f, struct net *net);
Eric Dumazetd5dd8872019-06-18 11:09:00 -0700116
Qian Cai08003d02019-06-20 10:52:40 -0400117static inline void fqdir_pre_exit(struct fqdir *fqdir)
Eric Dumazetd5dd8872019-06-18 11:09:00 -0700118{
119 fqdir->high_thresh = 0; /* prevent creation of new frags */
120 fqdir->dead = true;
121}
Eric Dumazet89fb9002019-05-24 09:03:31 -0700122void fqdir_exit(struct fqdir *fqdir);
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800123
Eric Dumazet093ba722018-03-31 12:58:44 -0700124void inet_frag_kill(struct inet_frag_queue *q);
125void inet_frag_destroy(struct inet_frag_queue *q);
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700126struct inet_frag_queue *inet_frag_find(struct fqdir *fqdir, void *key);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700127
Peter Oskolkov353c9cb2018-08-11 20:27:24 +0000128/* Free all skbs in the queue; return the sum of their truesizes. */
129unsigned int inet_frag_rbtree_purge(struct rb_root *root);
130
Eric Dumazet093ba722018-03-31 12:58:44 -0700131static inline void inet_frag_put(struct inet_frag_queue *q)
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700132{
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300133 if (refcount_dec_and_test(&q->refcnt))
Eric Dumazet093ba722018-03-31 12:58:44 -0700134 inet_frag_destroy(q);
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700135}
136
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000137/* Memory Tracking Functions. */
138
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700139static inline long frag_mem_limit(const struct fqdir *fqdir)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000140{
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700141 return atomic_long_read(&fqdir->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000142}
143
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700144static inline void sub_frag_mem_limit(struct fqdir *fqdir, long val)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000145{
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700146 atomic_long_sub(val, &fqdir->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000147}
148
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700149static inline void add_frag_mem_limit(struct fqdir *fqdir, long val)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000150{
Eric Dumazet6ce3b4d2019-05-24 09:03:30 -0700151 atomic_long_add(val, &fqdir->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000152}
153
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000154/* 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
Peter Oskolkovc23f35d2019-01-22 10:02:50 -0800165/* Return values of inet_frag_queue_insert() */
166#define IPFRAG_OK 0
167#define IPFRAG_DUP 1
168#define IPFRAG_OVERLAP 2
169int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
170 int offset, int end);
171void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
172 struct sk_buff *parent);
173void inet_frag_reasm_finish(struct inet_frag_queue *q, struct sk_buff *head,
174 void *reasm_data);
175struct sk_buff *inet_frag_pull_head(struct inet_frag_queue *q);
176
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700177#endif