1 /* SPDX-License-Identifier: GPL-2.0 */
3 * connection tracking helpers.
5 * 16 Dec 2003: Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
6 * - generalize L3 protocol dependent part.
8 * Derived from include/linux/netfiter_ipv4/ip_conntrack_helper.h
11 #ifndef _NF_CONNTRACK_HELPER_H
12 #define _NF_CONNTRACK_HELPER_H
13 #include <linux/refcount.h>
14 #include <net/netfilter/nf_conntrack.h>
15 #include <net/netfilter/nf_conntrack_extend.h>
16 #include <net/netfilter/nf_conntrack_expect.h>
18 #define NF_NAT_HELPER_PREFIX "ip_nat_"
19 #define NF_NAT_HELPER_NAME(name) NF_NAT_HELPER_PREFIX name
20 #define MODULE_ALIAS_NF_NAT_HELPER(name) \
21 MODULE_ALIAS(NF_NAT_HELPER_NAME(name))
25 enum nf_ct_helper_flags {
26 NF_CT_HELPER_F_USERSPACE = (1 << 0),
27 NF_CT_HELPER_F_CONFIGURED = (1 << 1),
30 #define NF_CT_HELPER_NAME_LEN 16
32 struct nf_conntrack_helper {
33 struct hlist_node hnode; /* Internal use. */
35 char name[NF_CT_HELPER_NAME_LEN]; /* name of the module */
37 struct module *me; /* pointer to self */
38 const struct nf_conntrack_expect_policy *expect_policy;
40 /* Tuple of things we will help (compared against server response) */
41 struct nf_conntrack_tuple tuple;
43 /* Function to call when data passes; return verdict, or -1 to
45 int (*help)(struct sk_buff *skb,
48 enum ip_conntrack_info conntrackinfo);
50 void (*destroy)(struct nf_conn *ct);
52 int (*from_nlattr)(struct nlattr *attr, struct nf_conn *ct);
53 int (*to_nlattr)(struct sk_buff *skb, const struct nf_conn *ct);
54 unsigned int expect_class_max;
58 /* For user-space helpers: */
59 unsigned int queue_num;
60 /* length of userspace private data stored in nf_conn_help->data */
62 /* name of NAT helper module */
63 char nat_mod_name[NF_CT_HELPER_NAME_LEN];
66 /* Must be kept in sync with the classes defined by helpers */
67 #define NF_CT_MAX_EXPECT_CLASSES 4
69 /* nf_conn feature for connections that have a helper */
72 struct nf_conntrack_helper __rcu *helper;
74 struct hlist_head expectations;
76 /* Current number of expected connections */
77 u8 expecting[NF_CT_MAX_EXPECT_CLASSES];
79 /* private helper information. */
80 char data[32] __aligned(8);
83 #define NF_CT_HELPER_BUILD_BUG_ON(structsize) \
84 BUILD_BUG_ON((structsize) > sizeof_field(struct nf_conn_help, data))
86 struct nf_conntrack_helper *__nf_conntrack_helper_find(const char *name,
87 u16 l3num, u8 protonum);
89 struct nf_conntrack_helper *nf_conntrack_helper_try_module_get(const char *name,
92 void nf_conntrack_helper_put(struct nf_conntrack_helper *helper);
94 void nf_ct_helper_init(struct nf_conntrack_helper *helper,
95 u16 l3num, u16 protonum, const char *name,
96 u16 default_port, u16 spec_port, u32 id,
97 const struct nf_conntrack_expect_policy *exp_pol,
99 int (*help)(struct sk_buff *skb, unsigned int protoff,
101 enum ip_conntrack_info ctinfo),
102 int (*from_nlattr)(struct nlattr *attr,
104 struct module *module);
106 int nf_conntrack_helper_register(struct nf_conntrack_helper *);
107 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *);
109 int nf_conntrack_helpers_register(struct nf_conntrack_helper *, unsigned int);
110 void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *,
113 struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp);
115 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
118 int nf_ct_helper(struct sk_buff *skb, struct nf_conn *ct,
119 enum ip_conntrack_info ctinfo, u16 proto);
120 int nf_ct_add_helper(struct nf_conn *ct, const char *name, u8 family,
121 u8 proto, bool nat, struct nf_conntrack_helper **hp);
123 void nf_ct_helper_destroy(struct nf_conn *ct);
125 static inline struct nf_conn_help *nfct_help(const struct nf_conn *ct)
127 return nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
130 static inline void *nfct_help_data(const struct nf_conn *ct)
132 struct nf_conn_help *help;
134 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
136 return (void *)help->data;
139 int nf_conntrack_helper_init(void);
140 void nf_conntrack_helper_fini(void);
142 int nf_conntrack_broadcast_help(struct sk_buff *skb, struct nf_conn *ct,
143 enum ip_conntrack_info ctinfo,
144 unsigned int timeout);
146 struct nf_ct_helper_expectfn {
147 struct list_head head;
149 void (*expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
153 void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
154 const char *fmt, ...);
156 void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n);
157 void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n);
158 struct nf_ct_helper_expectfn *
159 nf_ct_helper_expectfn_find_by_name(const char *name);
160 struct nf_ct_helper_expectfn *
161 nf_ct_helper_expectfn_find_by_symbol(const void *symbol);
163 extern struct hlist_head *nf_ct_helper_hash;
164 extern unsigned int nf_ct_helper_hsize;
166 struct nf_conntrack_nat_helper {
167 struct list_head list;
168 char mod_name[NF_CT_HELPER_NAME_LEN]; /* module name */
169 struct module *module; /* pointer to self */
172 #define NF_CT_NAT_HELPER_INIT(name) \
174 .mod_name = NF_NAT_HELPER_NAME(name), \
175 .module = THIS_MODULE \
178 void nf_nat_helper_register(struct nf_conntrack_nat_helper *nat);
179 void nf_nat_helper_unregister(struct nf_conntrack_nat_helper *nat);
180 int nf_nat_helper_try_module_get(const char *name, u16 l3num,
182 void nf_nat_helper_put(struct nf_conntrack_helper *helper);
183 #endif /*_NF_CONNTRACK_HELPER_H*/