blob: 69e531ed81894393e07cac9e953825fcb55ef42a [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
Pavel Emelyanovac18e752008-01-22 06:02:14 -08005struct netns_frags {
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +02006 /* Keep atomic mem on separate cachelines in structs that include it */
7 atomic_t mem ____cacheline_aligned_in_smp;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -08008 /* sysctls */
9 int timeout;
Pavel Emelyanove31e0bdc72008-01-22 06:10:13 -080010 int high_thresh;
11 int low_thresh;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +020012 int max_dist;
Eric Dumazet093ba722018-03-31 12:58:44 -070013 struct inet_frags *f;
Pavel Emelyanovac18e752008-01-22 06:02:14 -080014};
15
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020016/**
17 * fragment queue flags
18 *
19 * @INET_FRAG_FIRST_IN: first fragment has arrived
20 * @INET_FRAG_LAST_IN: final fragment has arrived
21 * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020022 */
23enum {
24 INET_FRAG_FIRST_IN = BIT(0),
25 INET_FRAG_LAST_IN = BIT(1),
26 INET_FRAG_COMPLETE = BIT(2),
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020027};
28
29/**
30 * struct inet_frag_queue - fragment queue
31 *
32 * @lock: spinlock protecting the queue
33 * @timer: queue expiration timer
34 * @list: hash bucket list
35 * @refcnt: reference count of the queue
36 * @fragments: received fragments head
37 * @fragments_tail: received fragments tail
38 * @stamp: timestamp of the last received fragment
39 * @len: total length of the original datagram
40 * @meat: length of received fragments so far
41 * @flags: fragment queue flags
Florian Westphald6b915e2015-05-22 16:32:51 +020042 * @max_size: maximum received fragment size
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020043 * @net: namespace that this frag belongs to
Florian Westphald1fe1942015-07-23 12:05:37 +020044 * @list_evictor: list of queues to forcefully evict (e.g. due to low memory)
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020045 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070046struct inet_frag_queue {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070047 spinlock_t lock;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020048 struct timer_list timer;
Jesper Dangaard Brouer6e34a8b2013-01-28 23:44:49 +000049 struct hlist_node list;
Reshetova, Elenaedcb6912017-06-30 13:08:07 +030050 refcount_t refcnt;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020051 struct sk_buff *fragments;
Changli Gaod6bebca2010-06-29 04:39:37 +000052 struct sk_buff *fragments_tail;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070053 ktime_t stamp;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020054 int len;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070055 int meat;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020056 __u8 flags;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +020057 u16 max_size;
Jesper Dangaard Brouer6e34a8b2013-01-28 23:44:49 +000058 struct netns_frags *net;
Florian Westphald1fe1942015-07-23 12:05:37 +020059 struct hlist_node list_evictor;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070060};
61
Jesper Dangaard Brouera4c40092013-04-25 09:52:25 +000062#define INETFRAGS_HASHSZ 1024
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070063
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000064/* averaged:
65 * max_depth = default ipfrag_high_thresh / INETFRAGS_HASHSZ /
66 * rounded up (SKB_TRUELEN(0) + sizeof(struct ipq or
67 * struct frag_queue))
68 */
Florian Westphalb13d3cb2014-07-24 16:50:32 +020069#define INETFRAGS_MAXDEPTH 128
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000070
Jesper Dangaard Brouer19952cc2013-04-03 23:38:16 +000071struct inet_frag_bucket {
72 struct hlist_head chain;
73 spinlock_t chain_lock;
74};
75
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070076struct inet_frags {
Jesper Dangaard Brouer19952cc2013-04-03 23:38:16 +000077 struct inet_frag_bucket hash[INETFRAGS_HASHSZ];
Hannes Frederic Sowa7088ad72013-10-23 11:06:57 +020078
Florian Westphalb13d3cb2014-07-24 16:50:32 +020079 struct work_struct frags_work;
80 unsigned int next_bucket;
Florian Westphale3a57d12014-07-24 16:50:35 +020081 unsigned long last_rebuild_jiffies;
82 bool rebuild;
Florian Westphalb13d3cb2014-07-24 16:50:32 +020083
Hannes Frederic Sowa7088ad72013-10-23 11:06:57 +020084 /* The first call to hashfn is responsible to initialize
85 * rnd. This is best done with net_get_random_once.
Florian Westphalab1c7242014-07-24 16:50:36 +020086 *
87 * rnd_seqlock is used to let hash insertion detect
88 * when it needs to re-lookup the hash chain to use.
Hannes Frederic Sowa7088ad72013-10-23 11:06:57 +020089 */
Jesper Dangaard Brouer5f8e1e82013-01-28 23:44:37 +000090 u32 rnd;
Florian Westphalab1c7242014-07-24 16:50:36 +020091 seqlock_t rnd_seqlock;
Alexey Dobriyan4c0ebd62017-05-23 00:20:26 +030092 unsigned int qsize;
Pavel Emelyanov321a3a92007-10-15 02:38:08 -070093
Florian Westphal36c77782014-07-24 16:50:29 +020094 unsigned int (*hashfn)(const struct inet_frag_queue *);
95 bool (*match)(const struct inet_frag_queue *q,
96 const void *arg);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070097 void (*constructor)(struct inet_frag_queue *q,
Florian Westphal36c77782014-07-24 16:50:29 +020098 const void *arg);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -070099 void (*destructor)(struct inet_frag_queue *);
Kees Cook78802012017-10-16 17:29:20 -0700100 void (*frag_expire)(struct timer_list *t);
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200101 struct kmem_cache *frags_cachep;
102 const char *frags_cache_name;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700103};
104
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200105int inet_frags_init(struct inet_frags *);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700106void inet_frags_fini(struct inet_frags *);
107
Eric Dumazet787bea72018-03-31 12:58:43 -0700108static inline int inet_frags_init_net(struct netns_frags *nf)
Eric Dumazet1d6119b2015-11-02 09:03:11 -0800109{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200110 atomic_set(&nf->mem, 0);
Eric Dumazet787bea72018-03-31 12:58:43 -0700111 return 0;
Eric Dumazet1d6119b2015-11-02 09:03:11 -0800112}
Eric Dumazet093ba722018-03-31 12:58:44 -0700113void inet_frags_exit_net(struct netns_frags *nf);
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800114
Eric Dumazet093ba722018-03-31 12:58:44 -0700115void inet_frag_kill(struct inet_frag_queue *q);
116void inet_frag_destroy(struct inet_frag_queue *q);
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800117struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
Florian Westphalab1c7242014-07-24 16:50:36 +0200118 struct inet_frags *f, void *key, unsigned int hash);
119
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000120void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
121 const char *prefix);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700122
Eric Dumazet093ba722018-03-31 12:58:44 -0700123static inline void inet_frag_put(struct inet_frag_queue *q)
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700124{
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300125 if (refcount_dec_and_test(&q->refcnt))
Eric Dumazet093ba722018-03-31 12:58:44 -0700126 inet_frag_destroy(q);
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700127}
128
Nikolay Aleksandrovcaaecdd2015-07-23 12:05:40 +0200129static inline bool inet_frag_evicting(struct inet_frag_queue *q)
130{
131 return !hlist_unhashed(&q->list_evictor);
132}
133
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000134/* Memory Tracking Functions. */
135
136static inline int frag_mem_limit(struct netns_frags *nf)
137{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200138 return atomic_read(&nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000139}
140
Florian Westphal0e60d242015-07-23 12:05:38 +0200141static inline void sub_frag_mem_limit(struct netns_frags *nf, int i)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000142{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200143 atomic_sub(i, &nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000144}
145
Florian Westphal0e60d242015-07-23 12:05:38 +0200146static inline void add_frag_mem_limit(struct netns_frags *nf, int i)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000147{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200148 atomic_add(i, &nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000149}
150
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200151static inline int sum_frag_mem_limit(struct netns_frags *nf)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000152{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200153 return atomic_read(&nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000154}
155
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000156/* RFC 3168 support :
157 * We want to check ECN values of all fragments, do detect invalid combinations.
158 * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
159 */
160#define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
161#define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
162#define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
163#define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
164
165extern const u8 ip_frag_ecn_table[16];
166
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700167#endif