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