blob: a52e7273e7a59bc8ce47b21d29235a740add8db0 [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
Eric Dumazet648700f2018-03-31 12:58:49 -07005#include <linux/rhashtable.h>
6
Pavel Emelyanovac18e752008-01-22 06:02:14 -08007struct netns_frags {
Eric Dumazet648700f2018-03-31 12:58:49 -07008 struct rhashtable rhashtable ____cacheline_aligned_in_smp;
9
Jesper Dangaard Brouerfb452a12017-09-01 11:26:08 +020010 /* Keep atomic mem on separate cachelines in structs that include it */
Eric Dumazet3e67f102018-03-31 12:58:53 -070011 atomic_long_t mem ____cacheline_aligned_in_smp;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -080012 /* sysctls */
Eric Dumazet3e67f102018-03-31 12:58:53 -070013 long high_thresh;
14 long low_thresh;
Pavel Emelyanovb2fd5322008-01-22 06:09:37 -080015 int timeout;
Nikolay Borisov0fbf4cb2016-02-15 12:11:31 +020016 int max_dist;
Eric Dumazet093ba722018-03-31 12:58:44 -070017 struct inet_frags *f;
Pavel Emelyanovac18e752008-01-22 06:02:14 -080018};
19
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020020/**
21 * fragment queue flags
22 *
23 * @INET_FRAG_FIRST_IN: first fragment has arrived
24 * @INET_FRAG_LAST_IN: final fragment has arrived
25 * @INET_FRAG_COMPLETE: frag queue has been processed and is due for destruction
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020026 */
27enum {
28 INET_FRAG_FIRST_IN = BIT(0),
29 INET_FRAG_LAST_IN = BIT(1),
30 INET_FRAG_COMPLETE = BIT(2),
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020031};
32
Eric Dumazet648700f2018-03-31 12:58:49 -070033struct frag_v4_compare_key {
34 __be32 saddr;
35 __be32 daddr;
36 u32 user;
37 u32 vif;
38 __be16 id;
39 u16 protocol;
40};
41
42struct frag_v6_compare_key {
43 struct in6_addr saddr;
44 struct in6_addr daddr;
45 u32 user;
46 __be32 id;
47 u32 iif;
48};
49
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020050/**
51 * struct inet_frag_queue - fragment queue
52 *
Eric Dumazet648700f2018-03-31 12:58:49 -070053 * @node: rhash node
54 * @key: keys identifying this frag.
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020055 * @timer: queue expiration timer
Eric Dumazet648700f2018-03-31 12:58:49 -070056 * @lock: spinlock protecting this frag
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020057 * @refcnt: reference count of the queue
58 * @fragments: received fragments head
59 * @fragments_tail: received fragments tail
60 * @stamp: timestamp of the last received fragment
61 * @len: total length of the original datagram
62 * @meat: length of received fragments so far
63 * @flags: fragment queue flags
Florian Westphald6b915e2015-05-22 16:32:51 +020064 * @max_size: maximum received fragment size
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020065 * @net: namespace that this frag belongs to
Eric Dumazet648700f2018-03-31 12:58:49 -070066 * @rcu: rcu head for freeing deferall
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020067 */
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070068struct inet_frag_queue {
Eric Dumazet648700f2018-03-31 12:58:49 -070069 struct rhash_head node;
70 union {
71 struct frag_v4_compare_key v4;
72 struct frag_v6_compare_key v6;
73 } key;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020074 struct timer_list timer;
Eric Dumazet648700f2018-03-31 12:58:49 -070075 spinlock_t lock;
Reshetova, Elenaedcb6912017-06-30 13:08:07 +030076 refcount_t refcnt;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020077 struct sk_buff *fragments;
Changli Gaod6bebca2010-06-29 04:39:37 +000078 struct sk_buff *fragments_tail;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070079 ktime_t stamp;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020080 int len;
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -070081 int meat;
Nikolay Aleksandrov1ab19342014-08-01 12:29:45 +020082 __u8 flags;
Patrick McHardy5f2d04f2012-08-26 19:13:55 +020083 u16 max_size;
Eric Dumazet648700f2018-03-31 12:58:49 -070084 struct netns_frags *net;
85 struct rcu_head rcu;
Jesper Dangaard Brouer19952cc2013-04-03 23:38:16 +000086};
87
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070088struct inet_frags {
Alexey Dobriyan4c0ebd62017-05-23 00:20:26 +030089 unsigned int qsize;
Pavel Emelyanov321a3a92007-10-15 02:38:08 -070090
Pavel Emelyanovc6fda282007-10-17 19:46:47 -070091 void (*constructor)(struct inet_frag_queue *q,
Florian Westphal36c77782014-07-24 16:50:29 +020092 const void *arg);
Pavel Emelyanov1e4b8282007-10-15 02:39:14 -070093 void (*destructor)(struct inet_frag_queue *);
Kees Cook78802012017-10-16 17:29:20 -070094 void (*frag_expire)(struct timer_list *t);
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +020095 struct kmem_cache *frags_cachep;
96 const char *frags_cache_name;
Eric Dumazet648700f2018-03-31 12:58:49 -070097 struct rhashtable_params rhash_params;
Pavel Emelyanov7eb95152007-10-15 02:31:52 -070098};
99
Nikolay Aleksandrovd4ad4d22014-08-01 12:29:48 +0200100int inet_frags_init(struct inet_frags *);
Pavel Emelyanov7eb95152007-10-15 02:31:52 -0700101void inet_frags_fini(struct inet_frags *);
102
Eric Dumazet787bea72018-03-31 12:58:43 -0700103static inline int inet_frags_init_net(struct netns_frags *nf)
Eric Dumazet1d6119b2015-11-02 09:03:11 -0800104{
Eric Dumazet3e67f102018-03-31 12:58:53 -0700105 atomic_long_set(&nf->mem, 0);
Eric Dumazet648700f2018-03-31 12:58:49 -0700106 return rhashtable_init(&nf->rhashtable, &nf->f->rhash_params);
Eric Dumazet1d6119b2015-11-02 09:03:11 -0800107}
Eric Dumazet093ba722018-03-31 12:58:44 -0700108void inet_frags_exit_net(struct netns_frags *nf);
Pavel Emelyanove5a2bb82008-01-22 06:06:23 -0800109
Eric Dumazet093ba722018-03-31 12:58:44 -0700110void inet_frag_kill(struct inet_frag_queue *q);
111void inet_frag_destroy(struct inet_frag_queue *q);
Eric Dumazet648700f2018-03-31 12:58:49 -0700112struct inet_frag_queue *inet_frag_find(struct netns_frags *nf, void *key);
Pavel Emelyanov277e6502007-10-15 02:37:18 -0700113
Eric Dumazet093ba722018-03-31 12:58:44 -0700114static inline void inet_frag_put(struct inet_frag_queue *q)
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700115{
Reshetova, Elenaedcb6912017-06-30 13:08:07 +0300116 if (refcount_dec_and_test(&q->refcnt))
Eric Dumazet093ba722018-03-31 12:58:44 -0700117 inet_frag_destroy(q);
Pavel Emelyanov762cc402007-10-15 02:41:56 -0700118}
119
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000120/* Memory Tracking Functions. */
121
Eric Dumazet3e67f102018-03-31 12:58:53 -0700122static inline long frag_mem_limit(const struct netns_frags *nf)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000123{
Eric Dumazet3e67f102018-03-31 12:58:53 -0700124 return atomic_long_read(&nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000125}
126
Eric Dumazet3e67f102018-03-31 12:58:53 -0700127static inline void sub_frag_mem_limit(struct netns_frags *nf, long val)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000128{
Eric Dumazet3e67f102018-03-31 12:58:53 -0700129 atomic_long_sub(val, &nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000130}
131
Eric Dumazet3e67f102018-03-31 12:58:53 -0700132static inline void add_frag_mem_limit(struct netns_frags *nf, long val)
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000133{
Eric Dumazet3e67f102018-03-31 12:58:53 -0700134 atomic_long_add(val, &nf->mem);
Jesper Dangaard Brouerd4336732013-01-28 23:45:12 +0000135}
136
Hannes Frederic Sowabe991972013-03-22 08:24:37 +0000137/* RFC 3168 support :
138 * We want to check ECN values of all fragments, do detect invalid combinations.
139 * In ipq->ecn, we store the OR value of each ip4_frag_ecn() fragment value.
140 */
141#define IPFRAG_ECN_NOT_ECT 0x01 /* one frag had ECN_NOT_ECT */
142#define IPFRAG_ECN_ECT_1 0x02 /* one frag had ECN_ECT_1 */
143#define IPFRAG_ECN_ECT_0 0x04 /* one frag had ECN_ECT_0 */
144#define IPFRAG_ECN_CE 0x08 /* one frag had ECN_CE */
145
146extern const u8 ip_frag_ecn_table[16];
147
Pavel Emelyanov5ab11c92007-10-15 02:24:19 -0700148#endif