1 #ifndef _NF_CONNTRACK_EXTEND_H
2 #define _NF_CONNTRACK_EXTEND_H
3 
4 #include <linux/slab.h>
5 
6 #include <net/netfilter/nf_conntrack.h>
7 
8 enum nf_ct_ext_id {
9 	NF_CT_EXT_HELPER,
10 #if defined(CONFIG_NF_NAT) || defined(CONFIG_NF_NAT_MODULE)
11 	NF_CT_EXT_NAT,
12 #endif
13 	NF_CT_EXT_SEQADJ,
14 	NF_CT_EXT_ACCT,
15 #ifdef CONFIG_NF_CONNTRACK_EVENTS
16 	NF_CT_EXT_ECACHE,
17 #endif
18 #ifdef CONFIG_NF_CONNTRACK_ZONES
19 	NF_CT_EXT_ZONE,
20 #endif
21 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
22 	NF_CT_EXT_TSTAMP,
23 #endif
24 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
25 	NF_CT_EXT_TIMEOUT,
26 #endif
27 #ifdef CONFIG_NF_CONNTRACK_LABELS
28 	NF_CT_EXT_LABELS,
29 #endif
30 #if IS_ENABLED(CONFIG_NETFILTER_SYNPROXY)
31 	NF_CT_EXT_SYNPROXY,
32 #endif
33 	NF_CT_EXT_NUM,
34 };
35 
36 #define NF_CT_EXT_HELPER_TYPE struct nf_conn_help
37 #define NF_CT_EXT_NAT_TYPE struct nf_conn_nat
38 #define NF_CT_EXT_SEQADJ_TYPE struct nf_conn_seqadj
39 #define NF_CT_EXT_ACCT_TYPE struct nf_conn_acct
40 #define NF_CT_EXT_ECACHE_TYPE struct nf_conntrack_ecache
41 #define NF_CT_EXT_ZONE_TYPE struct nf_conntrack_zone
42 #define NF_CT_EXT_TSTAMP_TYPE struct nf_conn_tstamp
43 #define NF_CT_EXT_TIMEOUT_TYPE struct nf_conn_timeout
44 #define NF_CT_EXT_LABELS_TYPE struct nf_conn_labels
45 #define NF_CT_EXT_SYNPROXY_TYPE struct nf_conn_synproxy
46 
47 /* Extensions: optional stuff which isn't permanently in struct. */
48 struct nf_ct_ext {
49 	struct rcu_head rcu;
50 	u16 offset[NF_CT_EXT_NUM];
51 	u16 len;
52 	char data[0];
53 };
54 
55 static inline bool __nf_ct_ext_exist(const struct nf_ct_ext *ext, u8 id)
56 {
57 	return !!ext->offset[id];
58 }
59 
60 static inline bool nf_ct_ext_exist(const struct nf_conn *ct, u8 id)
61 {
62 	return (ct->ext && __nf_ct_ext_exist(ct->ext, id));
63 }
64 
65 static inline void *__nf_ct_ext_find(const struct nf_conn *ct, u8 id)
66 {
67 	if (!nf_ct_ext_exist(ct, id))
68 		return NULL;
69 
70 	return (void *)ct->ext + ct->ext->offset[id];
71 }
72 #define nf_ct_ext_find(ext, id)	\
73 	((id##_TYPE *)__nf_ct_ext_find((ext), (id)))
74 
75 /* Destroy all relationships */
76 void __nf_ct_ext_destroy(struct nf_conn *ct);
77 static inline void nf_ct_ext_destroy(struct nf_conn *ct)
78 {
79 	if (ct->ext)
80 		__nf_ct_ext_destroy(ct);
81 }
82 
83 /* Free operation. If you want to free a object referred from private area,
84  * please implement __nf_ct_ext_free() and call it.
85  */
86 static inline void nf_ct_ext_free(struct nf_conn *ct)
87 {
88 	if (ct->ext)
89 		kfree_rcu(ct->ext, rcu);
90 }
91 
92 /* Add this type, returns pointer to data or NULL. */
93 void *__nf_ct_ext_add_length(struct nf_conn *ct, enum nf_ct_ext_id id,
94 			     size_t var_alloc_len, gfp_t gfp);
95 
96 #define nf_ct_ext_add(ct, id, gfp) \
97 	((id##_TYPE *)__nf_ct_ext_add_length((ct), (id), 0, (gfp)))
98 #define nf_ct_ext_add_length(ct, id, len, gfp) \
99 	((id##_TYPE *)__nf_ct_ext_add_length((ct), (id), (len), (gfp)))
100 
101 #define NF_CT_EXT_F_PREALLOC	0x0001
102 
103 struct nf_ct_ext_type {
104 	/* Destroys relationships (can be NULL). */
105 	void (*destroy)(struct nf_conn *ct);
106 	/* Called when realloacted (can be NULL).
107 	   Contents has already been moved. */
108 	void (*move)(void *new, void *old);
109 
110 	enum nf_ct_ext_id id;
111 
112 	unsigned int flags;
113 
114 	/* Length and min alignment. */
115 	u8 len;
116 	u8 align;
117 	/* initial size of nf_ct_ext. */
118 	u8 alloc_size;
119 };
120 
121 int nf_ct_extend_register(struct nf_ct_ext_type *type);
122 void nf_ct_extend_unregister(struct nf_ct_ext_type *type);
123 #endif /* _NF_CONNTRACK_EXTEND_H */
124