blob: 351f0c3cdcd9df16078a40f07963fb605eeaa882 [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;
Pavel Emelyanovac18e752008-01-22 06:02:14 -080013};
14
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020015/**
16 * fragment queue flags
17 *
18 * @INET_FRAG_FIRST_IN: first fragment has arrived
19 * @INET_FRAG_LAST_IN: final fragment has arrived
20 * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020021 */
22enum {
23 INET_FRAG_FIRST_IN = BIT(0),
24 INET_FRAG_LAST_IN = BIT(1),
25 INET_FRAG_COMPLETE = BIT(2),
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020026};
27
28/**
29 * struct inet_frag_queue - fragment queue
30 *
31 * @lock: spinlock protecting the queue
32 * @timer: queue expiration timer
33 * @list: hash bucket list
34 * @refcnt: reference count of the queue
35 * @fragments: received fragments head
36 * @fragments_tail: received fragments tail
37 * @stamp: timestamp of the last received fragment
38 * @len: total length of the original datagram
39 * @meat: length of received fragments so far
40 * @flags: fragment queue flags
Florian Westphald6b915e2015-05-22 16:32:51 +020041 * @max_size: maximum received fragment size
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020042 * @net: namespace that this frag belongs to
Florian Westphald1fe1942015-07-23 12:05:37 +020043 * @list_evictor: list of queues to forcefully evict (e.g. due to low memory)
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020044 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070045struct inet_frag_queue {
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070046 spinlock_t lock;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020047 struct timer_list timer;
Jesper Dangaard Brouer6e34a8b2013-01-28 23:44:49 +000048 struct hlist_node list;
Reshetova, Elenaedcb6912017-06-30 13:08:07 +030049 refcount_t refcnt;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020050 struct sk_buff *fragments;
Changli Gaod6bebca2010-06-29 04:39:37 +000051 struct sk_buff *fragments_tail;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070052 ktime_t stamp;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020053 int len;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070054 int meat;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020055 __u8 flags;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +020056 u16 max_size;
Jesper Dangaard Brouer6e34a8b2013-01-28 23:44:49 +000057 struct netns_frags *net;
Florian Westphald1fe1942015-07-23 12:05:37 +020058 struct hlist_node list_evictor;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070059};
60
Jesper Dangaard Brouera4c40092013-04-25 09:52:25 +000061#define INETFRAGS_HASHSZ 1024
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070062
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000063/* averaged:
64 * max_depth = default ipfrag_high_thresh / INETFRAGS_HASHSZ /
65 * rounded up (SKB_TRUELEN(0) + sizeof(struct ipq or
66 * struct frag_queue))
67 */
Florian Westphalb13d3cb2014-07-24 16:50:32 +020068#define INETFRAGS_MAXDEPTH 128
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +000069
Jesper Dangaard Brouer19952cc2013-04-03 23:38:16 +000070struct inet_frag_bucket {
71 struct hlist_head chain;
72 spinlock_t chain_lock;
73};
74
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070075struct inet_frags {
Jesper Dangaard Brouer19952cc2013-04-03 23:38:16 +000076 struct inet_frag_bucket hash[INETFRAGS_HASHSZ];
Hannes Frederic Sowa7088ad72013-10-23 11:06:57 +020077
Florian Westphalb13d3cb2014-07-24 16:50:32 +020078 struct work_struct frags_work;
79 unsigned int next_bucket;
Florian Westphale3a57d12014-07-24 16:50:35 +020080 unsigned long last_rebuild_jiffies;
81 bool rebuild;
Florian Westphalb13d3cb2014-07-24 16:50:32 +020082
Hannes Frederic Sowa7088ad72013-10-23 11:06:57 +020083 /* The first call to hashfn is responsible to initialize
84 * rnd. This is best done with net_get_random_once.
Florian Westphalab1c7242014-07-24 16:50:36 +020085 *
86 * rnd_seqlock is used to let hash insertion detect
87 * when it needs to re-lookup the hash chain to use.
Hannes Frederic Sowa7088ad72013-10-23 11:06:57 +020088 */
Jesper Dangaard Brouer5f8e1e82013-01-28 23:44:37 +000089 u32 rnd;
Florian Westphalab1c7242014-07-24 16:50:36 +020090 seqlock_t rnd_seqlock;
Alexey Dobriyan4c0ebd62017-05-23 00:20:26 +030091 unsigned int qsize;
Pavel Emelyanov321a3a92007-10-15 02:38:08 -070092
Florian Westphal36c77782014-07-24 16:50:29 +020093 unsigned int (*hashfn)(const struct inet_frag_queue *);
94 bool (*match)(const struct inet_frag_queue *q,
95 const void *arg);
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070096 void (*constructor)(struct inet_frag_queue *q,
Florian Westphal36c77782014-07-24 16:50:29 +020097 const void *arg);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -070098 void (*destructor)(struct inet_frag_queue *);
Kees Cook78802012017-10-16 17:29:20 -070099 void (*frag_expire)(struct timer_list *t);
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200100 struct kmem_cache *frags_cachep;
101 const char *frags_cache_name;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700102};
103
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200104int inet_frags_init(struct inet_frags *);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700105void inet_frags_fini(struct inet_frags *);
106
Jesper Dangaard Brouer5a636432017-09-01 11:26:13 +0200107static inline void inet_frags_init_net(struct netns_frags *nf)
Eric Dumazet1d6119b2015-11-02 09:03:11 -0800108{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200109 atomic_set(&nf->mem, 0);
Eric Dumazet1d6119b2015-11-02 09:03:11 -0800110}
Pavel Emelyanov81566e82008-01-22 06:12:39 -0800111void inet_frags_exit_net(struct netns_frags *nf, struct inet_frags *f);
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800112
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700113void inet_frag_kill(struct inet_frag_queue *q, struct inet_frags *f);
Florian Westphal3fd588e2014-07-24 16:50:34 +0200114void inet_frag_destroy(struct inet_frag_queue *q, struct inet_frags *f);
Pavel Emelyanovac18e752008-01-22 06:02:14 -0800115struct inet_frag_queue *inet_frag_find(struct netns_frags *nf,
Florian Westphalab1c7242014-07-24 16:50:36 +0200116 struct inet_frags *f, void *key, unsigned int hash);
117
Hannes Frederic Sowa5a3da1f2013-03-15 11:32:30 +0000118void inet_frag_maybe_warn_overflow(struct inet_frag_queue *q,
119 const char *prefix);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700120
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700121static inline void inet_frag_put(struct inet_frag_queue *q, struct inet_frags *f)
122{
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300123 if (refcount_dec_and_test(&q->refcnt))
Florian Westphal3fd588e2014-07-24 16:50:34 +0200124 inet_frag_destroy(q, f);
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700125}
126
Nikolay Aleksandrovcaaecdd2015-07-23 12:05:40 +0200127static inline bool inet_frag_evicting(struct inet_frag_queue *q)
128{
129 return !hlist_unhashed(&q->list_evictor);
130}
131
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000132/* Memory Tracking Functions. */
133
134static inline int frag_mem_limit(struct netns_frags *nf)
135{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200136 return atomic_read(&nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000137}
138
Florian Westphal0e60d242015-07-23 12:05:38 +0200139static inline void sub_frag_mem_limit(struct netns_frags *nf, int i)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000140{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200141 atomic_sub(i, &nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000142}
143
Florian Westphal0e60d242015-07-23 12:05:38 +0200144static inline void add_frag_mem_limit(struct netns_frags *nf, int i)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000145{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200146 atomic_add(i, &nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000147}
148
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200149static inline int sum_frag_mem_limit(struct netns_frags *nf)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000150{
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +0200151 return atomic_read(&nf->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
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700165#endif