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_ACCT,
14 #ifdef CONFIG_NF_CONNTRACK_EVENTS
15 	NF_CT_EXT_ECACHE,
16 #endif
17 #ifdef CONFIG_NF_CONNTRACK_ZONES
18 	NF_CT_EXT_ZONE,
19 #endif
20 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
21 	NF_CT_EXT_TSTAMP,
22 #endif
23 #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
24 	NF_CT_EXT_TIMEOUT,
25 #endif
26 	NF_CT_EXT_NUM,
27 };
28 
29 #define NF_CT_EXT_HELPER_TYPE struct nf_conn_help
30 #define NF_CT_EXT_NAT_TYPE struct nf_conn_nat
31 #define NF_CT_EXT_ACCT_TYPE struct nf_conn_counter
32 #define NF_CT_EXT_ECACHE_TYPE struct nf_conntrack_ecache
33 #define NF_CT_EXT_ZONE_TYPE struct nf_conntrack_zone
34 #define NF_CT_EXT_TSTAMP_TYPE struct nf_conn_tstamp
35 #define NF_CT_EXT_TIMEOUT_TYPE struct nf_conn_timeout
36 
37 /* Extensions: optional stuff which isn't permanently in struct. */
38 struct nf_ct_ext {
39 	struct rcu_head rcu;
40 	u16 offset[NF_CT_EXT_NUM];
41 	u16 len;
42 	char data[0];
43 };
44 
__nf_ct_ext_exist(const struct nf_ct_ext * ext,u8 id)45 static inline bool __nf_ct_ext_exist(const struct nf_ct_ext *ext, u8 id)
46 {
47 	return !!ext->offset[id];
48 }
49 
nf_ct_ext_exist(const struct nf_conn * ct,u8 id)50 static inline bool nf_ct_ext_exist(const struct nf_conn *ct, u8 id)
51 {
52 	return (ct->ext && __nf_ct_ext_exist(ct->ext, id));
53 }
54 
__nf_ct_ext_find(const struct nf_conn * ct,u8 id)55 static inline void *__nf_ct_ext_find(const struct nf_conn *ct, u8 id)
56 {
57 	if (!nf_ct_ext_exist(ct, id))
58 		return NULL;
59 
60 	return (void *)ct->ext + ct->ext->offset[id];
61 }
62 #define nf_ct_ext_find(ext, id)	\
63 	((id##_TYPE *)__nf_ct_ext_find((ext), (id)))
64 
65 /* Destroy all relationships */
66 extern void __nf_ct_ext_destroy(struct nf_conn *ct);
nf_ct_ext_destroy(struct nf_conn * ct)67 static inline void nf_ct_ext_destroy(struct nf_conn *ct)
68 {
69 	if (ct->ext)
70 		__nf_ct_ext_destroy(ct);
71 }
72 
73 /* Free operation. If you want to free a object referred from private area,
74  * please implement __nf_ct_ext_free() and call it.
75  */
nf_ct_ext_free(struct nf_conn * ct)76 static inline void nf_ct_ext_free(struct nf_conn *ct)
77 {
78 	if (ct->ext)
79 		kfree(ct->ext);
80 }
81 
82 /* Add this type, returns pointer to data or NULL. */
83 void *
84 __nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp);
85 #define nf_ct_ext_add(ct, id, gfp) \
86 	((id##_TYPE *)__nf_ct_ext_add((ct), (id), (gfp)))
87 
88 #define NF_CT_EXT_F_PREALLOC	0x0001
89 
90 struct nf_ct_ext_type {
91 	/* Destroys relationships (can be NULL). */
92 	void (*destroy)(struct nf_conn *ct);
93 	/* Called when realloacted (can be NULL).
94 	   Contents has already been moved. */
95 	void (*move)(void *new, void *old);
96 
97 	enum nf_ct_ext_id id;
98 
99 	unsigned int flags;
100 
101 	/* Length and min alignment. */
102 	u8 len;
103 	u8 align;
104 	/* initial size of nf_ct_ext. */
105 	u8 alloc_size;
106 };
107 
108 int nf_ct_extend_register(struct nf_ct_ext_type *type);
109 void nf_ct_extend_unregister(struct nf_ct_ext_type *type);
110 #endif /* _NF_CONNTRACK_EXTEND_H */
111