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>
20 enum nf_ct_helper_flags {
21 NF_CT_HELPER_F_USERSPACE = (1 << 0),
22 NF_CT_HELPER_F_CONFIGURED = (1 << 1),
25 #define NF_CT_HELPER_NAME_LEN 16
27 struct nf_conntrack_helper {
28 struct hlist_node hnode; /* Internal use. */
30 char name[NF_CT_HELPER_NAME_LEN]; /* name of the module */
32 struct module *me; /* pointer to self */
33 const struct nf_conntrack_expect_policy *expect_policy;
35 /* Tuple of things we will help (compared against server response) */
36 struct nf_conntrack_tuple tuple;
38 /* Function to call when data passes; return verdict, or -1 to
40 int (*help)(struct sk_buff *skb,
43 enum ip_conntrack_info conntrackinfo);
45 void (*destroy)(struct nf_conn *ct);
47 int (*from_nlattr)(struct nlattr *attr, struct nf_conn *ct);
48 int (*to_nlattr)(struct sk_buff *skb, const struct nf_conn *ct);
49 unsigned int expect_class_max;
53 /* For user-space helpers: */
54 unsigned int queue_num;
55 /* length of userspace private data stored in nf_conn_help->data */
59 /* Must be kept in sync with the classes defined by helpers */
60 #define NF_CT_MAX_EXPECT_CLASSES 4
62 /* nf_conn feature for connections that have a helper */
65 struct nf_conntrack_helper __rcu *helper;
67 struct hlist_head expectations;
69 /* Current number of expected connections */
70 u8 expecting[NF_CT_MAX_EXPECT_CLASSES];
72 /* private helper information. */
73 char data[32] __aligned(8);
76 #define NF_CT_HELPER_BUILD_BUG_ON(structsize) \
77 BUILD_BUG_ON((structsize) > FIELD_SIZEOF(struct nf_conn_help, data))
79 struct nf_conntrack_helper *__nf_conntrack_helper_find(const char *name,
80 u16 l3num, u8 protonum);
82 struct nf_conntrack_helper *nf_conntrack_helper_try_module_get(const char *name,
85 void nf_conntrack_helper_put(struct nf_conntrack_helper *helper);
87 void nf_ct_helper_init(struct nf_conntrack_helper *helper,
88 u16 l3num, u16 protonum, const char *name,
89 u16 default_port, u16 spec_port, u32 id,
90 const struct nf_conntrack_expect_policy *exp_pol,
92 int (*help)(struct sk_buff *skb, unsigned int protoff,
94 enum ip_conntrack_info ctinfo),
95 int (*from_nlattr)(struct nlattr *attr,
97 struct module *module);
99 int nf_conntrack_helper_register(struct nf_conntrack_helper *);
100 void nf_conntrack_helper_unregister(struct nf_conntrack_helper *);
102 int nf_conntrack_helpers_register(struct nf_conntrack_helper *, unsigned int);
103 void nf_conntrack_helpers_unregister(struct nf_conntrack_helper *,
106 struct nf_conn_help *nf_ct_helper_ext_add(struct nf_conn *ct, gfp_t gfp);
108 int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,
111 void nf_ct_helper_destroy(struct nf_conn *ct);
113 static inline struct nf_conn_help *nfct_help(const struct nf_conn *ct)
115 return nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
118 static inline void *nfct_help_data(const struct nf_conn *ct)
120 struct nf_conn_help *help;
122 help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
124 return (void *)help->data;
127 int nf_conntrack_helper_pernet_init(struct net *net);
128 void nf_conntrack_helper_pernet_fini(struct net *net);
130 int nf_conntrack_helper_init(void);
131 void nf_conntrack_helper_fini(void);
133 int nf_conntrack_broadcast_help(struct sk_buff *skb, struct nf_conn *ct,
134 enum ip_conntrack_info ctinfo,
135 unsigned int timeout);
137 struct nf_ct_helper_expectfn {
138 struct list_head head;
140 void (*expectfn)(struct nf_conn *ct, struct nf_conntrack_expect *exp);
144 void nf_ct_helper_log(struct sk_buff *skb, const struct nf_conn *ct,
145 const char *fmt, ...);
147 void nf_ct_helper_expectfn_register(struct nf_ct_helper_expectfn *n);
148 void nf_ct_helper_expectfn_unregister(struct nf_ct_helper_expectfn *n);
149 struct nf_ct_helper_expectfn *
150 nf_ct_helper_expectfn_find_by_name(const char *name);
151 struct nf_ct_helper_expectfn *
152 nf_ct_helper_expectfn_find_by_symbol(const void *symbol);
154 extern struct hlist_head *nf_ct_helper_hash;
155 extern unsigned int nf_ct_helper_hsize;
157 #endif /*_NF_CONNTRACK_HELPER_H*/