]>
Commit | Line | Data |
---|---|---|
12f7a505 PNA |
1 | /* |
2 | * (C) 2012 Pablo Neira Ayuso <pablo@netfilter.org> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License version 2 as | |
6 | * published by the Free Software Foundation (or any later at your option). | |
7 | * | |
8 | * This software has been sponsored by Vyatta Inc. <http://www.vyatta.com> | |
9 | */ | |
10 | #include <linux/init.h> | |
11 | #include <linux/module.h> | |
12 | #include <linux/kernel.h> | |
13 | #include <linux/skbuff.h> | |
14 | #include <linux/netlink.h> | |
15 | #include <linux/rculist.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/types.h> | |
18 | #include <linux/list.h> | |
19 | #include <linux/errno.h> | |
20 | #include <net/netlink.h> | |
21 | #include <net/sock.h> | |
22 | ||
23 | #include <net/netfilter/nf_conntrack_helper.h> | |
24 | #include <net/netfilter/nf_conntrack_expect.h> | |
25 | #include <net/netfilter/nf_conntrack_ecache.h> | |
26 | ||
27 | #include <linux/netfilter/nfnetlink.h> | |
28 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
29 | #include <linux/netfilter/nfnetlink_cthelper.h> | |
30 | ||
31 | MODULE_LICENSE("GPL"); | |
32 | MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); | |
33 | MODULE_DESCRIPTION("nfnl_cthelper: User-space connection tracking helpers"); | |
34 | ||
83d90219 LZ |
35 | struct nfnl_cthelper { |
36 | struct list_head list; | |
37 | struct nf_conntrack_helper helper; | |
38 | }; | |
39 | ||
40 | static LIST_HEAD(nfnl_cthelper_list); | |
41 | ||
12f7a505 PNA |
42 | static int |
43 | nfnl_userspace_cthelper(struct sk_buff *skb, unsigned int protoff, | |
44 | struct nf_conn *ct, enum ip_conntrack_info ctinfo) | |
45 | { | |
46 | const struct nf_conn_help *help; | |
47 | struct nf_conntrack_helper *helper; | |
48 | ||
49 | help = nfct_help(ct); | |
50 | if (help == NULL) | |
51 | return NF_DROP; | |
52 | ||
e2361cb9 | 53 | /* rcu_read_lock()ed by nf_hook_thresh */ |
12f7a505 PNA |
54 | helper = rcu_dereference(help->helper); |
55 | if (helper == NULL) | |
56 | return NF_DROP; | |
57 | ||
9332ef9d | 58 | /* This is a user-space helper not yet configured, skip. */ |
12f7a505 PNA |
59 | if ((helper->flags & |
60 | (NF_CT_HELPER_F_USERSPACE | NF_CT_HELPER_F_CONFIGURED)) == | |
61 | NF_CT_HELPER_F_USERSPACE) | |
62 | return NF_ACCEPT; | |
63 | ||
64 | /* If the user-space helper is not available, don't block traffic. */ | |
65 | return NF_QUEUE_NR(helper->queue_num) | NF_VERDICT_FLAG_QUEUE_BYPASS; | |
66 | } | |
67 | ||
68 | static const struct nla_policy nfnl_cthelper_tuple_pol[NFCTH_TUPLE_MAX+1] = { | |
69 | [NFCTH_TUPLE_L3PROTONUM] = { .type = NLA_U16, }, | |
70 | [NFCTH_TUPLE_L4PROTONUM] = { .type = NLA_U8, }, | |
71 | }; | |
72 | ||
73 | static int | |
74 | nfnl_cthelper_parse_tuple(struct nf_conntrack_tuple *tuple, | |
75 | const struct nlattr *attr) | |
76 | { | |
130ffbc2 | 77 | int err; |
12f7a505 PNA |
78 | struct nlattr *tb[NFCTH_TUPLE_MAX+1]; |
79 | ||
fceb6435 JB |
80 | err = nla_parse_nested(tb, NFCTH_TUPLE_MAX, attr, |
81 | nfnl_cthelper_tuple_pol, NULL); | |
130ffbc2 DB |
82 | if (err < 0) |
83 | return err; | |
12f7a505 PNA |
84 | |
85 | if (!tb[NFCTH_TUPLE_L3PROTONUM] || !tb[NFCTH_TUPLE_L4PROTONUM]) | |
86 | return -EINVAL; | |
87 | ||
78146572 IW |
88 | /* Not all fields are initialized so first zero the tuple */ |
89 | memset(tuple, 0, sizeof(struct nf_conntrack_tuple)); | |
90 | ||
fe31d1a8 | 91 | tuple->src.l3num = ntohs(nla_get_be16(tb[NFCTH_TUPLE_L3PROTONUM])); |
12f7a505 PNA |
92 | tuple->dst.protonum = nla_get_u8(tb[NFCTH_TUPLE_L4PROTONUM]); |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
97 | static int | |
98 | nfnl_cthelper_from_nlattr(struct nlattr *attr, struct nf_conn *ct) | |
99 | { | |
b18c5d15 | 100 | struct nf_conn_help *help = nfct_help(ct); |
12f7a505 | 101 | |
7be54ca4 PNA |
102 | if (attr == NULL) |
103 | return -EINVAL; | |
104 | ||
12f7a505 PNA |
105 | if (help->helper->data_len == 0) |
106 | return -EINVAL; | |
107 | ||
157ffffe | 108 | nla_memcpy(help->data, nla_data(attr), sizeof(help->data)); |
12f7a505 PNA |
109 | return 0; |
110 | } | |
111 | ||
112 | static int | |
113 | nfnl_cthelper_to_nlattr(struct sk_buff *skb, const struct nf_conn *ct) | |
114 | { | |
115 | const struct nf_conn_help *help = nfct_help(ct); | |
116 | ||
117 | if (help->helper->data_len && | |
118 | nla_put(skb, CTA_HELP_INFO, help->helper->data_len, &help->data)) | |
119 | goto nla_put_failure; | |
120 | ||
121 | return 0; | |
122 | ||
123 | nla_put_failure: | |
124 | return -ENOSPC; | |
125 | } | |
126 | ||
127 | static const struct nla_policy nfnl_cthelper_expect_pol[NFCTH_POLICY_MAX+1] = { | |
128 | [NFCTH_POLICY_NAME] = { .type = NLA_NUL_STRING, | |
129 | .len = NF_CT_HELPER_NAME_LEN-1 }, | |
130 | [NFCTH_POLICY_EXPECT_MAX] = { .type = NLA_U32, }, | |
131 | [NFCTH_POLICY_EXPECT_TIMEOUT] = { .type = NLA_U32, }, | |
132 | }; | |
133 | ||
134 | static int | |
135 | nfnl_cthelper_expect_policy(struct nf_conntrack_expect_policy *expect_policy, | |
136 | const struct nlattr *attr) | |
137 | { | |
130ffbc2 | 138 | int err; |
12f7a505 PNA |
139 | struct nlattr *tb[NFCTH_POLICY_MAX+1]; |
140 | ||
fceb6435 JB |
141 | err = nla_parse_nested(tb, NFCTH_POLICY_MAX, attr, |
142 | nfnl_cthelper_expect_pol, NULL); | |
130ffbc2 DB |
143 | if (err < 0) |
144 | return err; | |
12f7a505 PNA |
145 | |
146 | if (!tb[NFCTH_POLICY_NAME] || | |
147 | !tb[NFCTH_POLICY_EXPECT_MAX] || | |
148 | !tb[NFCTH_POLICY_EXPECT_TIMEOUT]) | |
149 | return -EINVAL; | |
150 | ||
151 | strncpy(expect_policy->name, | |
152 | nla_data(tb[NFCTH_POLICY_NAME]), NF_CT_HELPER_NAME_LEN); | |
153 | expect_policy->max_expected = | |
154 | ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_MAX])); | |
92f73221 GF |
155 | if (expect_policy->max_expected > NF_CT_EXPECT_MAX_CNT) |
156 | return -EINVAL; | |
157 | ||
12f7a505 PNA |
158 | expect_policy->timeout = |
159 | ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_TIMEOUT])); | |
160 | ||
161 | return 0; | |
162 | } | |
163 | ||
164 | static const struct nla_policy | |
165 | nfnl_cthelper_expect_policy_set[NFCTH_POLICY_SET_MAX+1] = { | |
166 | [NFCTH_POLICY_SET_NUM] = { .type = NLA_U32, }, | |
167 | }; | |
168 | ||
169 | static int | |
170 | nfnl_cthelper_parse_expect_policy(struct nf_conntrack_helper *helper, | |
171 | const struct nlattr *attr) | |
172 | { | |
173 | int i, ret; | |
174 | struct nf_conntrack_expect_policy *expect_policy; | |
175 | struct nlattr *tb[NFCTH_POLICY_SET_MAX+1]; | |
ae5c6821 | 176 | unsigned int class_max; |
12f7a505 | 177 | |
130ffbc2 | 178 | ret = nla_parse_nested(tb, NFCTH_POLICY_SET_MAX, attr, |
fceb6435 | 179 | nfnl_cthelper_expect_policy_set, NULL); |
130ffbc2 DB |
180 | if (ret < 0) |
181 | return ret; | |
12f7a505 PNA |
182 | |
183 | if (!tb[NFCTH_POLICY_SET_NUM]) | |
184 | return -EINVAL; | |
185 | ||
ae5c6821 LZ |
186 | class_max = ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM])); |
187 | if (class_max == 0) | |
188 | return -EINVAL; | |
189 | if (class_max > NF_CT_MAX_EXPECT_CLASSES) | |
12f7a505 PNA |
190 | return -EOVERFLOW; |
191 | ||
192 | expect_policy = kzalloc(sizeof(struct nf_conntrack_expect_policy) * | |
ae5c6821 | 193 | class_max, GFP_KERNEL); |
12f7a505 PNA |
194 | if (expect_policy == NULL) |
195 | return -ENOMEM; | |
196 | ||
ae5c6821 | 197 | for (i = 0; i < class_max; i++) { |
12f7a505 PNA |
198 | if (!tb[NFCTH_POLICY_SET+i]) |
199 | goto err; | |
200 | ||
201 | ret = nfnl_cthelper_expect_policy(&expect_policy[i], | |
202 | tb[NFCTH_POLICY_SET+i]); | |
203 | if (ret < 0) | |
204 | goto err; | |
205 | } | |
ae5c6821 LZ |
206 | |
207 | helper->expect_class_max = class_max - 1; | |
12f7a505 PNA |
208 | helper->expect_policy = expect_policy; |
209 | return 0; | |
210 | err: | |
211 | kfree(expect_policy); | |
212 | return -EINVAL; | |
213 | } | |
214 | ||
215 | static int | |
216 | nfnl_cthelper_create(const struct nlattr * const tb[], | |
217 | struct nf_conntrack_tuple *tuple) | |
218 | { | |
219 | struct nf_conntrack_helper *helper; | |
83d90219 | 220 | struct nfnl_cthelper *nfcth; |
157ffffe | 221 | unsigned int size; |
12f7a505 PNA |
222 | int ret; |
223 | ||
224 | if (!tb[NFCTH_TUPLE] || !tb[NFCTH_POLICY] || !tb[NFCTH_PRIV_DATA_LEN]) | |
225 | return -EINVAL; | |
226 | ||
83d90219 LZ |
227 | nfcth = kzalloc(sizeof(*nfcth), GFP_KERNEL); |
228 | if (nfcth == NULL) | |
12f7a505 | 229 | return -ENOMEM; |
83d90219 | 230 | helper = &nfcth->helper; |
12f7a505 PNA |
231 | |
232 | ret = nfnl_cthelper_parse_expect_policy(helper, tb[NFCTH_POLICY]); | |
233 | if (ret < 0) | |
f83bf8da | 234 | goto err1; |
12f7a505 PNA |
235 | |
236 | strncpy(helper->name, nla_data(tb[NFCTH_NAME]), NF_CT_HELPER_NAME_LEN); | |
157ffffe FW |
237 | size = ntohl(nla_get_be32(tb[NFCTH_PRIV_DATA_LEN])); |
238 | if (size > FIELD_SIZEOF(struct nf_conn_help, data)) { | |
239 | ret = -ENOMEM; | |
240 | goto err2; | |
241 | } | |
242 | ||
12f7a505 PNA |
243 | helper->flags |= NF_CT_HELPER_F_USERSPACE; |
244 | memcpy(&helper->tuple, tuple, sizeof(struct nf_conntrack_tuple)); | |
245 | ||
246 | helper->me = THIS_MODULE; | |
247 | helper->help = nfnl_userspace_cthelper; | |
248 | helper->from_nlattr = nfnl_cthelper_from_nlattr; | |
249 | helper->to_nlattr = nfnl_cthelper_to_nlattr; | |
250 | ||
251 | /* Default to queue number zero, this can be updated at any time. */ | |
252 | if (tb[NFCTH_QUEUE_NUM]) | |
253 | helper->queue_num = ntohl(nla_get_be32(tb[NFCTH_QUEUE_NUM])); | |
254 | ||
255 | if (tb[NFCTH_STATUS]) { | |
256 | int status = ntohl(nla_get_be32(tb[NFCTH_STATUS])); | |
257 | ||
258 | switch(status) { | |
259 | case NFCT_HELPER_STATUS_ENABLED: | |
260 | helper->flags |= NF_CT_HELPER_F_CONFIGURED; | |
261 | break; | |
262 | case NFCT_HELPER_STATUS_DISABLED: | |
263 | helper->flags &= ~NF_CT_HELPER_F_CONFIGURED; | |
264 | break; | |
265 | } | |
266 | } | |
267 | ||
268 | ret = nf_conntrack_helper_register(helper); | |
269 | if (ret < 0) | |
f83bf8da | 270 | goto err2; |
12f7a505 | 271 | |
83d90219 | 272 | list_add_tail(&nfcth->list, &nfnl_cthelper_list); |
12f7a505 | 273 | return 0; |
f83bf8da JC |
274 | err2: |
275 | kfree(helper->expect_policy); | |
276 | err1: | |
83d90219 | 277 | kfree(nfcth); |
12f7a505 PNA |
278 | return ret; |
279 | } | |
280 | ||
2c422257 PNA |
281 | static int |
282 | nfnl_cthelper_update_policy_one(const struct nf_conntrack_expect_policy *policy, | |
283 | struct nf_conntrack_expect_policy *new_policy, | |
284 | const struct nlattr *attr) | |
285 | { | |
286 | struct nlattr *tb[NFCTH_POLICY_MAX + 1]; | |
287 | int err; | |
288 | ||
289 | err = nla_parse_nested(tb, NFCTH_POLICY_MAX, attr, | |
fceb6435 | 290 | nfnl_cthelper_expect_pol, NULL); |
2c422257 PNA |
291 | if (err < 0) |
292 | return err; | |
293 | ||
294 | if (!tb[NFCTH_POLICY_NAME] || | |
295 | !tb[NFCTH_POLICY_EXPECT_MAX] || | |
296 | !tb[NFCTH_POLICY_EXPECT_TIMEOUT]) | |
297 | return -EINVAL; | |
298 | ||
299 | if (nla_strcmp(tb[NFCTH_POLICY_NAME], policy->name)) | |
300 | return -EBUSY; | |
301 | ||
302 | new_policy->max_expected = | |
303 | ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_MAX])); | |
92f73221 GF |
304 | if (new_policy->max_expected > NF_CT_EXPECT_MAX_CNT) |
305 | return -EINVAL; | |
306 | ||
2c422257 PNA |
307 | new_policy->timeout = |
308 | ntohl(nla_get_be32(tb[NFCTH_POLICY_EXPECT_TIMEOUT])); | |
309 | ||
310 | return 0; | |
311 | } | |
312 | ||
313 | static int nfnl_cthelper_update_policy_all(struct nlattr *tb[], | |
314 | struct nf_conntrack_helper *helper) | |
315 | { | |
316 | struct nf_conntrack_expect_policy new_policy[helper->expect_class_max + 1]; | |
317 | struct nf_conntrack_expect_policy *policy; | |
318 | int i, err; | |
319 | ||
320 | /* Check first that all policy attributes are well-formed, so we don't | |
321 | * leave things in inconsistent state on errors. | |
322 | */ | |
323 | for (i = 0; i < helper->expect_class_max + 1; i++) { | |
324 | ||
325 | if (!tb[NFCTH_POLICY_SET + i]) | |
326 | return -EINVAL; | |
327 | ||
328 | err = nfnl_cthelper_update_policy_one(&helper->expect_policy[i], | |
329 | &new_policy[i], | |
330 | tb[NFCTH_POLICY_SET + i]); | |
331 | if (err < 0) | |
332 | return err; | |
333 | } | |
334 | /* Now we can safely update them. */ | |
335 | for (i = 0; i < helper->expect_class_max + 1; i++) { | |
336 | policy = (struct nf_conntrack_expect_policy *) | |
337 | &helper->expect_policy[i]; | |
338 | policy->max_expected = new_policy->max_expected; | |
339 | policy->timeout = new_policy->timeout; | |
340 | } | |
341 | ||
342 | return 0; | |
343 | } | |
344 | ||
345 | static int nfnl_cthelper_update_policy(struct nf_conntrack_helper *helper, | |
346 | const struct nlattr *attr) | |
347 | { | |
348 | struct nlattr *tb[NFCTH_POLICY_SET_MAX + 1]; | |
349 | unsigned int class_max; | |
350 | int err; | |
351 | ||
352 | err = nla_parse_nested(tb, NFCTH_POLICY_SET_MAX, attr, | |
fceb6435 | 353 | nfnl_cthelper_expect_policy_set, NULL); |
2c422257 PNA |
354 | if (err < 0) |
355 | return err; | |
356 | ||
357 | if (!tb[NFCTH_POLICY_SET_NUM]) | |
358 | return -EINVAL; | |
359 | ||
360 | class_max = ntohl(nla_get_be32(tb[NFCTH_POLICY_SET_NUM])); | |
361 | if (helper->expect_class_max + 1 != class_max) | |
362 | return -EBUSY; | |
363 | ||
364 | return nfnl_cthelper_update_policy_all(tb, helper); | |
365 | } | |
366 | ||
12f7a505 PNA |
367 | static int |
368 | nfnl_cthelper_update(const struct nlattr * const tb[], | |
369 | struct nf_conntrack_helper *helper) | |
370 | { | |
371 | int ret; | |
372 | ||
373 | if (tb[NFCTH_PRIV_DATA_LEN]) | |
374 | return -EBUSY; | |
375 | ||
376 | if (tb[NFCTH_POLICY]) { | |
2c422257 | 377 | ret = nfnl_cthelper_update_policy(helper, tb[NFCTH_POLICY]); |
12f7a505 PNA |
378 | if (ret < 0) |
379 | return ret; | |
380 | } | |
381 | if (tb[NFCTH_QUEUE_NUM]) | |
382 | helper->queue_num = ntohl(nla_get_be32(tb[NFCTH_QUEUE_NUM])); | |
383 | ||
384 | if (tb[NFCTH_STATUS]) { | |
385 | int status = ntohl(nla_get_be32(tb[NFCTH_STATUS])); | |
386 | ||
387 | switch(status) { | |
388 | case NFCT_HELPER_STATUS_ENABLED: | |
389 | helper->flags |= NF_CT_HELPER_F_CONFIGURED; | |
390 | break; | |
391 | case NFCT_HELPER_STATUS_DISABLED: | |
392 | helper->flags &= ~NF_CT_HELPER_F_CONFIGURED; | |
393 | break; | |
394 | } | |
395 | } | |
396 | return 0; | |
397 | } | |
398 | ||
7b8002a1 PNA |
399 | static int nfnl_cthelper_new(struct net *net, struct sock *nfnl, |
400 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
401 | const struct nlattr * const tb[]) | |
12f7a505 PNA |
402 | { |
403 | const char *helper_name; | |
404 | struct nf_conntrack_helper *cur, *helper = NULL; | |
405 | struct nf_conntrack_tuple tuple; | |
83d90219 LZ |
406 | struct nfnl_cthelper *nlcth; |
407 | int ret = 0; | |
12f7a505 PNA |
408 | |
409 | if (!tb[NFCTH_NAME] || !tb[NFCTH_TUPLE]) | |
410 | return -EINVAL; | |
411 | ||
412 | helper_name = nla_data(tb[NFCTH_NAME]); | |
413 | ||
414 | ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]); | |
415 | if (ret < 0) | |
416 | return ret; | |
417 | ||
83d90219 LZ |
418 | list_for_each_entry(nlcth, &nfnl_cthelper_list, list) { |
419 | cur = &nlcth->helper; | |
12f7a505 | 420 | |
83d90219 LZ |
421 | if (strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN)) |
422 | continue; | |
12f7a505 | 423 | |
83d90219 LZ |
424 | if ((tuple.src.l3num != cur->tuple.src.l3num || |
425 | tuple.dst.protonum != cur->tuple.dst.protonum)) | |
426 | continue; | |
12f7a505 | 427 | |
83d90219 LZ |
428 | if (nlh->nlmsg_flags & NLM_F_EXCL) |
429 | return -EEXIST; | |
12f7a505 | 430 | |
83d90219 LZ |
431 | helper = cur; |
432 | break; | |
12f7a505 | 433 | } |
12f7a505 PNA |
434 | |
435 | if (helper == NULL) | |
436 | ret = nfnl_cthelper_create(tb, &tuple); | |
437 | else | |
438 | ret = nfnl_cthelper_update(tb, helper); | |
439 | ||
440 | return ret; | |
12f7a505 PNA |
441 | } |
442 | ||
443 | static int | |
444 | nfnl_cthelper_dump_tuple(struct sk_buff *skb, | |
445 | struct nf_conntrack_helper *helper) | |
446 | { | |
447 | struct nlattr *nest_parms; | |
448 | ||
449 | nest_parms = nla_nest_start(skb, NFCTH_TUPLE | NLA_F_NESTED); | |
450 | if (nest_parms == NULL) | |
451 | goto nla_put_failure; | |
452 | ||
453 | if (nla_put_be16(skb, NFCTH_TUPLE_L3PROTONUM, | |
454 | htons(helper->tuple.src.l3num))) | |
455 | goto nla_put_failure; | |
456 | ||
457 | if (nla_put_u8(skb, NFCTH_TUPLE_L4PROTONUM, helper->tuple.dst.protonum)) | |
458 | goto nla_put_failure; | |
459 | ||
460 | nla_nest_end(skb, nest_parms); | |
461 | return 0; | |
462 | ||
463 | nla_put_failure: | |
464 | return -1; | |
465 | } | |
466 | ||
467 | static int | |
468 | nfnl_cthelper_dump_policy(struct sk_buff *skb, | |
469 | struct nf_conntrack_helper *helper) | |
470 | { | |
471 | int i; | |
472 | struct nlattr *nest_parms1, *nest_parms2; | |
473 | ||
474 | nest_parms1 = nla_nest_start(skb, NFCTH_POLICY | NLA_F_NESTED); | |
475 | if (nest_parms1 == NULL) | |
476 | goto nla_put_failure; | |
477 | ||
478 | if (nla_put_be32(skb, NFCTH_POLICY_SET_NUM, | |
ae5c6821 | 479 | htonl(helper->expect_class_max + 1))) |
12f7a505 PNA |
480 | goto nla_put_failure; |
481 | ||
ae5c6821 | 482 | for (i = 0; i < helper->expect_class_max + 1; i++) { |
12f7a505 PNA |
483 | nest_parms2 = nla_nest_start(skb, |
484 | (NFCTH_POLICY_SET+i) | NLA_F_NESTED); | |
485 | if (nest_parms2 == NULL) | |
486 | goto nla_put_failure; | |
487 | ||
488 | if (nla_put_string(skb, NFCTH_POLICY_NAME, | |
489 | helper->expect_policy[i].name)) | |
490 | goto nla_put_failure; | |
491 | ||
492 | if (nla_put_be32(skb, NFCTH_POLICY_EXPECT_MAX, | |
493 | htonl(helper->expect_policy[i].max_expected))) | |
494 | goto nla_put_failure; | |
495 | ||
496 | if (nla_put_be32(skb, NFCTH_POLICY_EXPECT_TIMEOUT, | |
497 | htonl(helper->expect_policy[i].timeout))) | |
498 | goto nla_put_failure; | |
499 | ||
500 | nla_nest_end(skb, nest_parms2); | |
501 | } | |
502 | nla_nest_end(skb, nest_parms1); | |
503 | return 0; | |
504 | ||
505 | nla_put_failure: | |
506 | return -1; | |
507 | } | |
508 | ||
509 | static int | |
15e47304 | 510 | nfnl_cthelper_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type, |
12f7a505 PNA |
511 | int event, struct nf_conntrack_helper *helper) |
512 | { | |
513 | struct nlmsghdr *nlh; | |
514 | struct nfgenmsg *nfmsg; | |
15e47304 | 515 | unsigned int flags = portid ? NLM_F_MULTI : 0; |
12f7a505 PNA |
516 | int status; |
517 | ||
dedb67c4 | 518 | event = nfnl_msg_type(NFNL_SUBSYS_CTHELPER, event); |
15e47304 | 519 | nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags); |
12f7a505 PNA |
520 | if (nlh == NULL) |
521 | goto nlmsg_failure; | |
522 | ||
523 | nfmsg = nlmsg_data(nlh); | |
524 | nfmsg->nfgen_family = AF_UNSPEC; | |
525 | nfmsg->version = NFNETLINK_V0; | |
526 | nfmsg->res_id = 0; | |
527 | ||
528 | if (nla_put_string(skb, NFCTH_NAME, helper->name)) | |
529 | goto nla_put_failure; | |
530 | ||
531 | if (nla_put_be32(skb, NFCTH_QUEUE_NUM, htonl(helper->queue_num))) | |
532 | goto nla_put_failure; | |
533 | ||
534 | if (nfnl_cthelper_dump_tuple(skb, helper) < 0) | |
535 | goto nla_put_failure; | |
536 | ||
537 | if (nfnl_cthelper_dump_policy(skb, helper) < 0) | |
538 | goto nla_put_failure; | |
539 | ||
540 | if (nla_put_be32(skb, NFCTH_PRIV_DATA_LEN, htonl(helper->data_len))) | |
541 | goto nla_put_failure; | |
542 | ||
543 | if (helper->flags & NF_CT_HELPER_F_CONFIGURED) | |
544 | status = NFCT_HELPER_STATUS_ENABLED; | |
545 | else | |
546 | status = NFCT_HELPER_STATUS_DISABLED; | |
547 | ||
548 | if (nla_put_be32(skb, NFCTH_STATUS, htonl(status))) | |
549 | goto nla_put_failure; | |
550 | ||
551 | nlmsg_end(skb, nlh); | |
552 | return skb->len; | |
553 | ||
554 | nlmsg_failure: | |
555 | nla_put_failure: | |
556 | nlmsg_cancel(skb, nlh); | |
557 | return -1; | |
558 | } | |
559 | ||
560 | static int | |
561 | nfnl_cthelper_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
562 | { | |
563 | struct nf_conntrack_helper *cur, *last; | |
12f7a505 PNA |
564 | |
565 | rcu_read_lock(); | |
566 | last = (struct nf_conntrack_helper *)cb->args[1]; | |
567 | for (; cb->args[0] < nf_ct_helper_hsize; cb->args[0]++) { | |
568 | restart: | |
b67bfe0d | 569 | hlist_for_each_entry_rcu(cur, |
12f7a505 PNA |
570 | &nf_ct_helper_hash[cb->args[0]], hnode) { |
571 | ||
572 | /* skip non-userspace conntrack helpers. */ | |
573 | if (!(cur->flags & NF_CT_HELPER_F_USERSPACE)) | |
574 | continue; | |
575 | ||
576 | if (cb->args[1]) { | |
577 | if (cur != last) | |
578 | continue; | |
579 | cb->args[1] = 0; | |
580 | } | |
581 | if (nfnl_cthelper_fill_info(skb, | |
15e47304 | 582 | NETLINK_CB(cb->skb).portid, |
12f7a505 PNA |
583 | cb->nlh->nlmsg_seq, |
584 | NFNL_MSG_TYPE(cb->nlh->nlmsg_type), | |
585 | NFNL_MSG_CTHELPER_NEW, cur) < 0) { | |
586 | cb->args[1] = (unsigned long)cur; | |
587 | goto out; | |
588 | } | |
589 | } | |
590 | } | |
591 | if (cb->args[1]) { | |
592 | cb->args[1] = 0; | |
593 | goto restart; | |
594 | } | |
595 | out: | |
596 | rcu_read_unlock(); | |
597 | return skb->len; | |
598 | } | |
599 | ||
7b8002a1 PNA |
600 | static int nfnl_cthelper_get(struct net *net, struct sock *nfnl, |
601 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
602 | const struct nlattr * const tb[]) | |
12f7a505 | 603 | { |
83d90219 | 604 | int ret = -ENOENT; |
12f7a505 | 605 | struct nf_conntrack_helper *cur; |
12f7a505 PNA |
606 | struct sk_buff *skb2; |
607 | char *helper_name = NULL; | |
608 | struct nf_conntrack_tuple tuple; | |
83d90219 | 609 | struct nfnl_cthelper *nlcth; |
12f7a505 PNA |
610 | bool tuple_set = false; |
611 | ||
612 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
613 | struct netlink_dump_control c = { | |
614 | .dump = nfnl_cthelper_dump_table, | |
615 | }; | |
616 | return netlink_dump_start(nfnl, skb, nlh, &c); | |
617 | } | |
618 | ||
619 | if (tb[NFCTH_NAME]) | |
620 | helper_name = nla_data(tb[NFCTH_NAME]); | |
621 | ||
622 | if (tb[NFCTH_TUPLE]) { | |
623 | ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]); | |
624 | if (ret < 0) | |
625 | return ret; | |
626 | ||
627 | tuple_set = true; | |
628 | } | |
629 | ||
83d90219 LZ |
630 | list_for_each_entry(nlcth, &nfnl_cthelper_list, list) { |
631 | cur = &nlcth->helper; | |
632 | if (helper_name && | |
633 | strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN)) | |
634 | continue; | |
12f7a505 | 635 | |
83d90219 LZ |
636 | if (tuple_set && |
637 | (tuple.src.l3num != cur->tuple.src.l3num || | |
638 | tuple.dst.protonum != cur->tuple.dst.protonum)) | |
639 | continue; | |
12f7a505 | 640 | |
83d90219 LZ |
641 | skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
642 | if (skb2 == NULL) { | |
643 | ret = -ENOMEM; | |
644 | break; | |
645 | } | |
12f7a505 | 646 | |
83d90219 LZ |
647 | ret = nfnl_cthelper_fill_info(skb2, NETLINK_CB(skb).portid, |
648 | nlh->nlmsg_seq, | |
649 | NFNL_MSG_TYPE(nlh->nlmsg_type), | |
650 | NFNL_MSG_CTHELPER_NEW, cur); | |
651 | if (ret <= 0) { | |
652 | kfree_skb(skb2); | |
653 | break; | |
654 | } | |
12f7a505 | 655 | |
83d90219 LZ |
656 | ret = netlink_unicast(nfnl, skb2, NETLINK_CB(skb).portid, |
657 | MSG_DONTWAIT); | |
658 | if (ret > 0) | |
659 | ret = 0; | |
12f7a505 | 660 | |
83d90219 LZ |
661 | /* this avoids a loop in nfnetlink. */ |
662 | return ret == -EAGAIN ? -ENOBUFS : ret; | |
12f7a505 PNA |
663 | } |
664 | return ret; | |
665 | } | |
666 | ||
7b8002a1 PNA |
667 | static int nfnl_cthelper_del(struct net *net, struct sock *nfnl, |
668 | struct sk_buff *skb, const struct nlmsghdr *nlh, | |
669 | const struct nlattr * const tb[]) | |
12f7a505 PNA |
670 | { |
671 | char *helper_name = NULL; | |
672 | struct nf_conntrack_helper *cur; | |
12f7a505 PNA |
673 | struct nf_conntrack_tuple tuple; |
674 | bool tuple_set = false, found = false; | |
83d90219 LZ |
675 | struct nfnl_cthelper *nlcth, *n; |
676 | int j = 0, ret; | |
12f7a505 PNA |
677 | |
678 | if (tb[NFCTH_NAME]) | |
679 | helper_name = nla_data(tb[NFCTH_NAME]); | |
680 | ||
681 | if (tb[NFCTH_TUPLE]) { | |
682 | ret = nfnl_cthelper_parse_tuple(&tuple, tb[NFCTH_TUPLE]); | |
683 | if (ret < 0) | |
684 | return ret; | |
685 | ||
686 | tuple_set = true; | |
687 | } | |
688 | ||
9338d7b4 | 689 | ret = -ENOENT; |
83d90219 LZ |
690 | list_for_each_entry_safe(nlcth, n, &nfnl_cthelper_list, list) { |
691 | cur = &nlcth->helper; | |
692 | j++; | |
12f7a505 | 693 | |
83d90219 LZ |
694 | if (helper_name && |
695 | strncmp(cur->name, helper_name, NF_CT_HELPER_NAME_LEN)) | |
696 | continue; | |
12f7a505 | 697 | |
83d90219 LZ |
698 | if (tuple_set && |
699 | (tuple.src.l3num != cur->tuple.src.l3num || | |
700 | tuple.dst.protonum != cur->tuple.dst.protonum)) | |
701 | continue; | |
12f7a505 | 702 | |
9338d7b4 LZ |
703 | if (refcount_dec_if_one(&cur->refcnt)) { |
704 | found = true; | |
705 | nf_conntrack_helper_unregister(cur); | |
706 | kfree(cur->expect_policy); | |
83d90219 | 707 | |
9338d7b4 LZ |
708 | list_del(&nlcth->list); |
709 | kfree(nlcth); | |
710 | } else { | |
711 | ret = -EBUSY; | |
712 | } | |
12f7a505 | 713 | } |
83d90219 | 714 | |
12f7a505 | 715 | /* Make sure we return success if we flush and there is no helpers */ |
9338d7b4 | 716 | return (found || j == 0) ? 0 : ret; |
12f7a505 PNA |
717 | } |
718 | ||
719 | static const struct nla_policy nfnl_cthelper_policy[NFCTH_MAX+1] = { | |
720 | [NFCTH_NAME] = { .type = NLA_NUL_STRING, | |
721 | .len = NF_CT_HELPER_NAME_LEN-1 }, | |
722 | [NFCTH_QUEUE_NUM] = { .type = NLA_U32, }, | |
723 | }; | |
724 | ||
725 | static const struct nfnl_callback nfnl_cthelper_cb[NFNL_MSG_CTHELPER_MAX] = { | |
726 | [NFNL_MSG_CTHELPER_NEW] = { .call = nfnl_cthelper_new, | |
727 | .attr_count = NFCTH_MAX, | |
728 | .policy = nfnl_cthelper_policy }, | |
729 | [NFNL_MSG_CTHELPER_GET] = { .call = nfnl_cthelper_get, | |
730 | .attr_count = NFCTH_MAX, | |
731 | .policy = nfnl_cthelper_policy }, | |
732 | [NFNL_MSG_CTHELPER_DEL] = { .call = nfnl_cthelper_del, | |
733 | .attr_count = NFCTH_MAX, | |
734 | .policy = nfnl_cthelper_policy }, | |
735 | }; | |
736 | ||
737 | static const struct nfnetlink_subsystem nfnl_cthelper_subsys = { | |
738 | .name = "cthelper", | |
739 | .subsys_id = NFNL_SUBSYS_CTHELPER, | |
740 | .cb_count = NFNL_MSG_CTHELPER_MAX, | |
741 | .cb = nfnl_cthelper_cb, | |
742 | }; | |
743 | ||
744 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTHELPER); | |
745 | ||
746 | static int __init nfnl_cthelper_init(void) | |
747 | { | |
748 | int ret; | |
749 | ||
750 | ret = nfnetlink_subsys_register(&nfnl_cthelper_subsys); | |
751 | if (ret < 0) { | |
752 | pr_err("nfnl_cthelper: cannot register with nfnetlink.\n"); | |
753 | goto err_out; | |
754 | } | |
755 | return 0; | |
756 | err_out: | |
757 | return ret; | |
758 | } | |
759 | ||
760 | static void __exit nfnl_cthelper_exit(void) | |
761 | { | |
762 | struct nf_conntrack_helper *cur; | |
83d90219 | 763 | struct nfnl_cthelper *nlcth, *n; |
12f7a505 PNA |
764 | |
765 | nfnetlink_subsys_unregister(&nfnl_cthelper_subsys); | |
766 | ||
83d90219 LZ |
767 | list_for_each_entry_safe(nlcth, n, &nfnl_cthelper_list, list) { |
768 | cur = &nlcth->helper; | |
12f7a505 | 769 | |
83d90219 LZ |
770 | nf_conntrack_helper_unregister(cur); |
771 | kfree(cur->expect_policy); | |
772 | kfree(nlcth); | |
12f7a505 PNA |
773 | } |
774 | } | |
775 | ||
776 | module_init(nfnl_cthelper_init); | |
777 | module_exit(nfnl_cthelper_exit); |