GNU Linux-libre 4.19.286-gnu1
[releases.git] / include / net / netfilter / nf_conntrack_extend.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _NF_CONNTRACK_EXTEND_H
3 #define _NF_CONNTRACK_EXTEND_H
4
5 #include <linux/slab.h>
6
7 #include <net/netfilter/nf_conntrack.h>
8
9 enum nf_ct_ext_id {
10         NF_CT_EXT_HELPER,
11 #if defined(CONFIG_NF_NAT) || defined(CONFIG_NF_NAT_MODULE)
12         NF_CT_EXT_NAT,
13 #endif
14         NF_CT_EXT_SEQADJ,
15         NF_CT_EXT_ACCT,
16 #ifdef CONFIG_NF_CONNTRACK_EVENTS
17         NF_CT_EXT_ECACHE,
18 #endif
19 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
20         NF_CT_EXT_TSTAMP,
21 #endif
22 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
23         NF_CT_EXT_TIMEOUT,
24 #endif
25 #ifdef CONFIG_NF_CONNTRACK_LABELS
26         NF_CT_EXT_LABELS,
27 #endif
28 #if IS_ENABLED(CONFIG_NETFILTER_SYNPROXY)
29         NF_CT_EXT_SYNPROXY,
30 #endif
31         NF_CT_EXT_NUM,
32 };
33
34 #define NF_CT_EXT_HELPER_TYPE struct nf_conn_help
35 #define NF_CT_EXT_NAT_TYPE struct nf_conn_nat
36 #define NF_CT_EXT_SEQADJ_TYPE struct nf_conn_seqadj
37 #define NF_CT_EXT_ACCT_TYPE struct nf_conn_acct
38 #define NF_CT_EXT_ECACHE_TYPE struct nf_conntrack_ecache
39 #define NF_CT_EXT_TSTAMP_TYPE struct nf_conn_tstamp
40 #define NF_CT_EXT_TIMEOUT_TYPE struct nf_conn_timeout
41 #define NF_CT_EXT_LABELS_TYPE struct nf_conn_labels
42 #define NF_CT_EXT_SYNPROXY_TYPE struct nf_conn_synproxy
43
44 /* Extensions: optional stuff which isn't permanently in struct. */
45 struct nf_ct_ext {
46         struct rcu_head rcu;
47         u8 offset[NF_CT_EXT_NUM];
48         u8 len;
49         char data[0];
50 };
51
52 static inline bool __nf_ct_ext_exist(const struct nf_ct_ext *ext, u8 id)
53 {
54         return !!ext->offset[id];
55 }
56
57 static inline bool nf_ct_ext_exist(const struct nf_conn *ct, u8 id)
58 {
59         return (ct->ext && __nf_ct_ext_exist(ct->ext, id));
60 }
61
62 static inline void *__nf_ct_ext_find(const struct nf_conn *ct, u8 id)
63 {
64         if (!nf_ct_ext_exist(ct, id))
65                 return NULL;
66
67         return (void *)ct->ext + ct->ext->offset[id];
68 }
69 #define nf_ct_ext_find(ext, id) \
70         ((id##_TYPE *)__nf_ct_ext_find((ext), (id)))
71
72 /* Destroy all relationships */
73 void nf_ct_ext_destroy(struct nf_conn *ct);
74
75 /* Free operation. If you want to free a object referred from private area,
76  * please implement __nf_ct_ext_free() and call it.
77  */
78 static inline void nf_ct_ext_free(struct nf_conn *ct)
79 {
80         if (ct->ext)
81                 kfree_rcu(ct->ext, rcu);
82 }
83
84 /* Add this type, returns pointer to data or NULL. */
85 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp);
86
87 struct nf_ct_ext_type {
88         /* Destroys relationships (can be NULL). */
89         void (*destroy)(struct nf_conn *ct);
90
91         enum nf_ct_ext_id id;
92
93         /* Length and min alignment. */
94         u8 len;
95         u8 align;
96 };
97
98 int nf_ct_extend_register(const struct nf_ct_ext_type *type);
99 void nf_ct_extend_unregister(const struct nf_ct_ext_type *type);
100 #endif /* _NF_CONNTRACK_EXTEND_H */