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 IS_ENABLED(CONFIG_NF_NAT)
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 #if IS_ENABLED(CONFIG_NET_ACT_CT)
32 	NF_CT_EXT_ACT_CT,
33 #endif
34 	NF_CT_EXT_NUM,
35 };
36 
37 #define NF_CT_EXT_HELPER_TYPE struct nf_conn_help
38 #define NF_CT_EXT_NAT_TYPE struct nf_conn_nat
39 #define NF_CT_EXT_SEQADJ_TYPE struct nf_conn_seqadj
40 #define NF_CT_EXT_ACCT_TYPE struct nf_conn_acct
41 #define NF_CT_EXT_ECACHE_TYPE struct nf_conntrack_ecache
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 #define NF_CT_EXT_ACT_CT_TYPE struct nf_conn_act_ct_ext
47 
48 /* Extensions: optional stuff which isn't permanently in struct. */
49 struct nf_ct_ext {
50 	u8 offset[NF_CT_EXT_NUM];
51 	u8 len;
52 	char data[];
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 
78 /* Add this type, returns pointer to data or NULL. */
79 void *nf_ct_ext_add(struct nf_conn *ct, enum nf_ct_ext_id id, gfp_t gfp);
80 
81 struct nf_ct_ext_type {
82 	/* Destroys relationships (can be NULL). */
83 	void (*destroy)(struct nf_conn *ct);
84 
85 	enum nf_ct_ext_id id;
86 
87 	/* Length and min alignment. */
88 	u8 len;
89 	u8 align;
90 };
91 
92 int nf_ct_extend_register(const struct nf_ct_ext_type *type);
93 void nf_ct_extend_unregister(const struct nf_ct_ext_type *type);
94 #endif /* _NF_CONNTRACK_EXTEND_H */
95