OMAP3: SR: Wait for VP idle before a VP disable
[linux-ginger.git] / net / netfilter / nf_conntrack_netlink.c
blob59d8064eb5224b8b1278c2275b0af31573be3575
1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/skbuff.h>
26 #include <linux/errno.h>
27 #include <linux/netlink.h>
28 #include <linux/spinlock.h>
29 #include <linux/interrupt.h>
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
33 #include <net/netfilter/nf_conntrack.h>
34 #include <net/netfilter/nf_conntrack_core.h>
35 #include <net/netfilter/nf_conntrack_expect.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_tuple.h>
40 #include <net/netfilter/nf_conntrack_acct.h>
41 #ifdef CONFIG_NF_NAT_NEEDED
42 #include <net/netfilter/nf_nat_core.h>
43 #include <net/netfilter/nf_nat_protocol.h>
44 #endif
46 #include <linux/netfilter/nfnetlink.h>
47 #include <linux/netfilter/nfnetlink_conntrack.h>
49 MODULE_LICENSE("GPL");
51 static char __initdata version[] = "0.93";
53 static inline int
54 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
55 const struct nf_conntrack_tuple *tuple,
56 struct nf_conntrack_l4proto *l4proto)
58 int ret = 0;
59 struct nlattr *nest_parms;
61 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
62 if (!nest_parms)
63 goto nla_put_failure;
64 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
66 if (likely(l4proto->tuple_to_nlattr))
67 ret = l4proto->tuple_to_nlattr(skb, tuple);
69 nla_nest_end(skb, nest_parms);
71 return ret;
73 nla_put_failure:
74 return -1;
77 static inline int
78 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
79 const struct nf_conntrack_tuple *tuple,
80 struct nf_conntrack_l3proto *l3proto)
82 int ret = 0;
83 struct nlattr *nest_parms;
85 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
86 if (!nest_parms)
87 goto nla_put_failure;
89 if (likely(l3proto->tuple_to_nlattr))
90 ret = l3proto->tuple_to_nlattr(skb, tuple);
92 nla_nest_end(skb, nest_parms);
94 return ret;
96 nla_put_failure:
97 return -1;
100 static int
101 ctnetlink_dump_tuples(struct sk_buff *skb,
102 const struct nf_conntrack_tuple *tuple)
104 int ret;
105 struct nf_conntrack_l3proto *l3proto;
106 struct nf_conntrack_l4proto *l4proto;
108 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
109 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
111 if (unlikely(ret < 0))
112 return ret;
114 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
115 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
117 return ret;
120 static inline int
121 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
123 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
124 return 0;
126 nla_put_failure:
127 return -1;
130 static inline int
131 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
133 long timeout = (ct->timeout.expires - jiffies) / HZ;
135 if (timeout < 0)
136 timeout = 0;
138 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
139 return 0;
141 nla_put_failure:
142 return -1;
145 static inline int
146 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
148 struct nf_conntrack_l4proto *l4proto;
149 struct nlattr *nest_proto;
150 int ret;
152 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
153 if (!l4proto->to_nlattr)
154 return 0;
156 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
157 if (!nest_proto)
158 goto nla_put_failure;
160 ret = l4proto->to_nlattr(skb, nest_proto, ct);
162 nla_nest_end(skb, nest_proto);
164 return ret;
166 nla_put_failure:
167 return -1;
170 static inline int
171 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
173 struct nlattr *nest_helper;
174 const struct nf_conn_help *help = nfct_help(ct);
175 struct nf_conntrack_helper *helper;
177 if (!help)
178 return 0;
180 helper = rcu_dereference(help->helper);
181 if (!helper)
182 goto out;
184 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
185 if (!nest_helper)
186 goto nla_put_failure;
187 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
189 if (helper->to_nlattr)
190 helper->to_nlattr(skb, ct);
192 nla_nest_end(skb, nest_helper);
193 out:
194 return 0;
196 nla_put_failure:
197 return -1;
200 static int
201 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
202 enum ip_conntrack_dir dir)
204 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
205 struct nlattr *nest_count;
206 const struct nf_conn_counter *acct;
208 acct = nf_conn_acct_find(ct);
209 if (!acct)
210 return 0;
212 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
213 if (!nest_count)
214 goto nla_put_failure;
216 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
217 cpu_to_be64(acct[dir].packets));
218 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
219 cpu_to_be64(acct[dir].bytes));
221 nla_nest_end(skb, nest_count);
223 return 0;
225 nla_put_failure:
226 return -1;
229 #ifdef CONFIG_NF_CONNTRACK_MARK
230 static inline int
231 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
233 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
234 return 0;
236 nla_put_failure:
237 return -1;
239 #else
240 #define ctnetlink_dump_mark(a, b) (0)
241 #endif
243 #ifdef CONFIG_NF_CONNTRACK_SECMARK
244 static inline int
245 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
247 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
248 return 0;
250 nla_put_failure:
251 return -1;
253 #else
254 #define ctnetlink_dump_secmark(a, b) (0)
255 #endif
257 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
259 static inline int
260 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
262 struct nlattr *nest_parms;
264 if (!(ct->status & IPS_EXPECTED))
265 return 0;
267 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
268 if (!nest_parms)
269 goto nla_put_failure;
270 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
271 goto nla_put_failure;
272 nla_nest_end(skb, nest_parms);
274 return 0;
276 nla_put_failure:
277 return -1;
280 #ifdef CONFIG_NF_NAT_NEEDED
281 static int
282 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
284 struct nlattr *nest_parms;
286 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
287 if (!nest_parms)
288 goto nla_put_failure;
290 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
291 htonl(natseq->correction_pos));
292 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
293 htonl(natseq->offset_before));
294 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
295 htonl(natseq->offset_after));
297 nla_nest_end(skb, nest_parms);
299 return 0;
301 nla_put_failure:
302 return -1;
305 static inline int
306 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
308 struct nf_nat_seq *natseq;
309 struct nf_conn_nat *nat = nfct_nat(ct);
311 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
312 return 0;
314 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
315 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
316 return -1;
318 natseq = &nat->seq[IP_CT_DIR_REPLY];
319 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
320 return -1;
322 return 0;
324 #else
325 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
326 #endif
328 static inline int
329 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
331 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
332 return 0;
334 nla_put_failure:
335 return -1;
338 static inline int
339 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
341 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
342 return 0;
344 nla_put_failure:
345 return -1;
348 static int
349 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
350 int event, struct nf_conn *ct)
352 struct nlmsghdr *nlh;
353 struct nfgenmsg *nfmsg;
354 struct nlattr *nest_parms;
355 unsigned int flags = pid ? NLM_F_MULTI : 0;
357 event |= NFNL_SUBSYS_CTNETLINK << 8;
358 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
359 if (nlh == NULL)
360 goto nlmsg_failure;
362 nfmsg = nlmsg_data(nlh);
363 nfmsg->nfgen_family = nf_ct_l3num(ct);
364 nfmsg->version = NFNETLINK_V0;
365 nfmsg->res_id = 0;
367 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
368 if (!nest_parms)
369 goto nla_put_failure;
370 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
371 goto nla_put_failure;
372 nla_nest_end(skb, nest_parms);
374 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
375 if (!nest_parms)
376 goto nla_put_failure;
377 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
378 goto nla_put_failure;
379 nla_nest_end(skb, nest_parms);
381 if (ctnetlink_dump_status(skb, ct) < 0 ||
382 ctnetlink_dump_timeout(skb, ct) < 0 ||
383 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
384 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
385 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
386 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
387 ctnetlink_dump_mark(skb, ct) < 0 ||
388 ctnetlink_dump_secmark(skb, ct) < 0 ||
389 ctnetlink_dump_id(skb, ct) < 0 ||
390 ctnetlink_dump_use(skb, ct) < 0 ||
391 ctnetlink_dump_master(skb, ct) < 0 ||
392 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
393 goto nla_put_failure;
395 nlmsg_end(skb, nlh);
396 return skb->len;
398 nlmsg_failure:
399 nla_put_failure:
400 nlmsg_cancel(skb, nlh);
401 return -1;
404 #ifdef CONFIG_NF_CONNTRACK_EVENTS
405 static inline size_t
406 ctnetlink_proto_size(const struct nf_conn *ct)
408 struct nf_conntrack_l3proto *l3proto;
409 struct nf_conntrack_l4proto *l4proto;
410 size_t len = 0;
412 rcu_read_lock();
413 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
414 len += l3proto->nla_size;
416 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
417 len += l4proto->nla_size;
418 rcu_read_unlock();
420 return len;
423 static inline size_t
424 ctnetlink_nlmsg_size(const struct nf_conn *ct)
426 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
427 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
428 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
429 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
430 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
431 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
432 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
433 #ifdef CONFIG_NF_CT_ACCT
434 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
435 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
436 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
437 #endif
438 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
439 + nla_total_size(0) /* CTA_PROTOINFO */
440 + nla_total_size(0) /* CTA_HELP */
441 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
442 #ifdef CONFIG_NF_CONNTRACK_SECMARK
443 + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */
444 #endif
445 #ifdef CONFIG_NF_NAT_NEEDED
446 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
447 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
448 #endif
449 #ifdef CONFIG_NF_CONNTRACK_MARK
450 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
451 #endif
452 + ctnetlink_proto_size(ct)
456 static int
457 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
459 struct nlmsghdr *nlh;
460 struct nfgenmsg *nfmsg;
461 struct nlattr *nest_parms;
462 struct nf_conn *ct = item->ct;
463 struct sk_buff *skb;
464 unsigned int type;
465 unsigned int flags = 0, group;
466 int err;
468 /* ignore our fake conntrack entry */
469 if (ct == &nf_conntrack_untracked)
470 return 0;
472 if (events & (1 << IPCT_DESTROY)) {
473 type = IPCTNL_MSG_CT_DELETE;
474 group = NFNLGRP_CONNTRACK_DESTROY;
475 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
476 type = IPCTNL_MSG_CT_NEW;
477 flags = NLM_F_CREATE|NLM_F_EXCL;
478 group = NFNLGRP_CONNTRACK_NEW;
479 } else if (events) {
480 type = IPCTNL_MSG_CT_NEW;
481 group = NFNLGRP_CONNTRACK_UPDATE;
482 } else
483 return 0;
485 if (!item->report && !nfnetlink_has_listeners(group))
486 return 0;
488 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
489 if (skb == NULL)
490 goto errout;
492 type |= NFNL_SUBSYS_CTNETLINK << 8;
493 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
494 if (nlh == NULL)
495 goto nlmsg_failure;
497 nfmsg = nlmsg_data(nlh);
498 nfmsg->nfgen_family = nf_ct_l3num(ct);
499 nfmsg->version = NFNETLINK_V0;
500 nfmsg->res_id = 0;
502 rcu_read_lock();
503 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
504 if (!nest_parms)
505 goto nla_put_failure;
506 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
507 goto nla_put_failure;
508 nla_nest_end(skb, nest_parms);
510 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
511 if (!nest_parms)
512 goto nla_put_failure;
513 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
514 goto nla_put_failure;
515 nla_nest_end(skb, nest_parms);
517 if (ctnetlink_dump_id(skb, ct) < 0)
518 goto nla_put_failure;
520 if (ctnetlink_dump_status(skb, ct) < 0)
521 goto nla_put_failure;
523 if (events & (1 << IPCT_DESTROY)) {
524 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
525 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
526 goto nla_put_failure;
527 } else {
528 if (ctnetlink_dump_timeout(skb, ct) < 0)
529 goto nla_put_failure;
531 if (events & (1 << IPCT_PROTOINFO)
532 && ctnetlink_dump_protoinfo(skb, ct) < 0)
533 goto nla_put_failure;
535 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
536 && ctnetlink_dump_helpinfo(skb, ct) < 0)
537 goto nla_put_failure;
539 #ifdef CONFIG_NF_CONNTRACK_SECMARK
540 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
541 && ctnetlink_dump_secmark(skb, ct) < 0)
542 goto nla_put_failure;
543 #endif
545 if (events & (1 << IPCT_RELATED) &&
546 ctnetlink_dump_master(skb, ct) < 0)
547 goto nla_put_failure;
549 if (events & (1 << IPCT_NATSEQADJ) &&
550 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
551 goto nla_put_failure;
554 #ifdef CONFIG_NF_CONNTRACK_MARK
555 if ((events & (1 << IPCT_MARK) || ct->mark)
556 && ctnetlink_dump_mark(skb, ct) < 0)
557 goto nla_put_failure;
558 #endif
559 rcu_read_unlock();
561 nlmsg_end(skb, nlh);
562 err = nfnetlink_send(skb, item->pid, group, item->report, GFP_ATOMIC);
563 if (err == -ENOBUFS || err == -EAGAIN)
564 return -ENOBUFS;
566 return 0;
568 nla_put_failure:
569 rcu_read_unlock();
570 nlmsg_cancel(skb, nlh);
571 nlmsg_failure:
572 kfree_skb(skb);
573 errout:
574 nfnetlink_set_err(0, group, -ENOBUFS);
575 return 0;
577 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
579 static int ctnetlink_done(struct netlink_callback *cb)
581 if (cb->args[1])
582 nf_ct_put((struct nf_conn *)cb->args[1]);
583 return 0;
586 static int
587 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
589 struct nf_conn *ct, *last;
590 struct nf_conntrack_tuple_hash *h;
591 struct hlist_nulls_node *n;
592 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
593 u_int8_t l3proto = nfmsg->nfgen_family;
595 rcu_read_lock();
596 last = (struct nf_conn *)cb->args[1];
597 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
598 restart:
599 hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
600 hnnode) {
601 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
602 continue;
603 ct = nf_ct_tuplehash_to_ctrack(h);
604 if (!atomic_inc_not_zero(&ct->ct_general.use))
605 continue;
606 /* Dump entries of a given L3 protocol number.
607 * If it is not specified, ie. l3proto == 0,
608 * then dump everything. */
609 if (l3proto && nf_ct_l3num(ct) != l3proto)
610 goto releasect;
611 if (cb->args[1]) {
612 if (ct != last)
613 goto releasect;
614 cb->args[1] = 0;
616 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
617 cb->nlh->nlmsg_seq,
618 IPCTNL_MSG_CT_NEW, ct) < 0) {
619 cb->args[1] = (unsigned long)ct;
620 goto out;
623 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
624 IPCTNL_MSG_CT_GET_CTRZERO) {
625 struct nf_conn_counter *acct;
627 acct = nf_conn_acct_find(ct);
628 if (acct)
629 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
631 releasect:
632 nf_ct_put(ct);
634 if (cb->args[1]) {
635 cb->args[1] = 0;
636 goto restart;
639 out:
640 rcu_read_unlock();
641 if (last)
642 nf_ct_put(last);
644 return skb->len;
647 static inline int
648 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
650 struct nlattr *tb[CTA_IP_MAX+1];
651 struct nf_conntrack_l3proto *l3proto;
652 int ret = 0;
654 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
656 rcu_read_lock();
657 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
659 if (likely(l3proto->nlattr_to_tuple)) {
660 ret = nla_validate_nested(attr, CTA_IP_MAX,
661 l3proto->nla_policy);
662 if (ret == 0)
663 ret = l3proto->nlattr_to_tuple(tb, tuple);
666 rcu_read_unlock();
668 return ret;
671 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
672 [CTA_PROTO_NUM] = { .type = NLA_U8 },
675 static inline int
676 ctnetlink_parse_tuple_proto(struct nlattr *attr,
677 struct nf_conntrack_tuple *tuple)
679 struct nlattr *tb[CTA_PROTO_MAX+1];
680 struct nf_conntrack_l4proto *l4proto;
681 int ret = 0;
683 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
684 if (ret < 0)
685 return ret;
687 if (!tb[CTA_PROTO_NUM])
688 return -EINVAL;
689 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
691 rcu_read_lock();
692 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
694 if (likely(l4proto->nlattr_to_tuple)) {
695 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
696 l4proto->nla_policy);
697 if (ret == 0)
698 ret = l4proto->nlattr_to_tuple(tb, tuple);
701 rcu_read_unlock();
703 return ret;
706 static int
707 ctnetlink_parse_tuple(const struct nlattr * const cda[],
708 struct nf_conntrack_tuple *tuple,
709 enum ctattr_tuple type, u_int8_t l3num)
711 struct nlattr *tb[CTA_TUPLE_MAX+1];
712 int err;
714 memset(tuple, 0, sizeof(*tuple));
716 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
718 if (!tb[CTA_TUPLE_IP])
719 return -EINVAL;
721 tuple->src.l3num = l3num;
723 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
724 if (err < 0)
725 return err;
727 if (!tb[CTA_TUPLE_PROTO])
728 return -EINVAL;
730 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
731 if (err < 0)
732 return err;
734 /* orig and expect tuples get DIR_ORIGINAL */
735 if (type == CTA_TUPLE_REPLY)
736 tuple->dst.dir = IP_CT_DIR_REPLY;
737 else
738 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
740 return 0;
743 static inline int
744 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
746 struct nlattr *tb[CTA_HELP_MAX+1];
748 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
750 if (!tb[CTA_HELP_NAME])
751 return -EINVAL;
753 *helper_name = nla_data(tb[CTA_HELP_NAME]);
755 return 0;
758 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
759 [CTA_STATUS] = { .type = NLA_U32 },
760 [CTA_TIMEOUT] = { .type = NLA_U32 },
761 [CTA_MARK] = { .type = NLA_U32 },
762 [CTA_USE] = { .type = NLA_U32 },
763 [CTA_ID] = { .type = NLA_U32 },
766 static int
767 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
768 const struct nlmsghdr *nlh,
769 const struct nlattr * const cda[])
771 struct nf_conntrack_tuple_hash *h;
772 struct nf_conntrack_tuple tuple;
773 struct nf_conn *ct;
774 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
775 u_int8_t u3 = nfmsg->nfgen_family;
776 int err = 0;
778 if (cda[CTA_TUPLE_ORIG])
779 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
780 else if (cda[CTA_TUPLE_REPLY])
781 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
782 else {
783 /* Flush the whole table */
784 nf_conntrack_flush_report(&init_net,
785 NETLINK_CB(skb).pid,
786 nlmsg_report(nlh));
787 return 0;
790 if (err < 0)
791 return err;
793 h = nf_conntrack_find_get(&init_net, &tuple);
794 if (!h)
795 return -ENOENT;
797 ct = nf_ct_tuplehash_to_ctrack(h);
799 if (cda[CTA_ID]) {
800 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
801 if (id != (u32)(unsigned long)ct) {
802 nf_ct_put(ct);
803 return -ENOENT;
807 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
808 NETLINK_CB(skb).pid,
809 nlmsg_report(nlh)) < 0) {
810 nf_ct_delete_from_lists(ct);
811 /* we failed to report the event, try later */
812 nf_ct_insert_dying_list(ct);
813 nf_ct_put(ct);
814 return 0;
817 /* death_by_timeout would report the event again */
818 set_bit(IPS_DYING_BIT, &ct->status);
820 nf_ct_kill(ct);
821 nf_ct_put(ct);
823 return 0;
826 static int
827 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
828 const struct nlmsghdr *nlh,
829 const struct nlattr * const cda[])
831 struct nf_conntrack_tuple_hash *h;
832 struct nf_conntrack_tuple tuple;
833 struct nf_conn *ct;
834 struct sk_buff *skb2 = NULL;
835 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
836 u_int8_t u3 = nfmsg->nfgen_family;
837 int err = 0;
839 if (nlh->nlmsg_flags & NLM_F_DUMP)
840 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
841 ctnetlink_done);
843 if (cda[CTA_TUPLE_ORIG])
844 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
845 else if (cda[CTA_TUPLE_REPLY])
846 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
847 else
848 return -EINVAL;
850 if (err < 0)
851 return err;
853 h = nf_conntrack_find_get(&init_net, &tuple);
854 if (!h)
855 return -ENOENT;
857 ct = nf_ct_tuplehash_to_ctrack(h);
859 err = -ENOMEM;
860 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
861 if (skb2 == NULL) {
862 nf_ct_put(ct);
863 return -ENOMEM;
866 rcu_read_lock();
867 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
868 IPCTNL_MSG_CT_NEW, ct);
869 rcu_read_unlock();
870 nf_ct_put(ct);
871 if (err <= 0)
872 goto free;
874 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
875 if (err < 0)
876 goto out;
878 return 0;
880 free:
881 kfree_skb(skb2);
882 out:
883 return err;
886 #ifdef CONFIG_NF_NAT_NEEDED
887 static int
888 ctnetlink_parse_nat_setup(struct nf_conn *ct,
889 enum nf_nat_manip_type manip,
890 const struct nlattr *attr)
892 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
894 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
895 if (!parse_nat_setup) {
896 #ifdef CONFIG_MODULES
897 rcu_read_unlock();
898 spin_unlock_bh(&nf_conntrack_lock);
899 nfnl_unlock();
900 if (request_module("nf-nat-ipv4") < 0) {
901 nfnl_lock();
902 spin_lock_bh(&nf_conntrack_lock);
903 rcu_read_lock();
904 return -EOPNOTSUPP;
906 nfnl_lock();
907 spin_lock_bh(&nf_conntrack_lock);
908 rcu_read_lock();
909 if (nfnetlink_parse_nat_setup_hook)
910 return -EAGAIN;
911 #endif
912 return -EOPNOTSUPP;
915 return parse_nat_setup(ct, manip, attr);
917 #endif
919 static int
920 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
922 unsigned long d;
923 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
924 d = ct->status ^ status;
926 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
927 /* unchangeable */
928 return -EBUSY;
930 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
931 /* SEEN_REPLY bit can only be set */
932 return -EBUSY;
934 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
935 /* ASSURED bit can only be set */
936 return -EBUSY;
938 /* Be careful here, modifying NAT bits can screw up things,
939 * so don't let users modify them directly if they don't pass
940 * nf_nat_range. */
941 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
942 return 0;
945 static int
946 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
948 #ifdef CONFIG_NF_NAT_NEEDED
949 int ret;
951 if (cda[CTA_NAT_DST]) {
952 ret = ctnetlink_parse_nat_setup(ct,
953 IP_NAT_MANIP_DST,
954 cda[CTA_NAT_DST]);
955 if (ret < 0)
956 return ret;
958 if (cda[CTA_NAT_SRC]) {
959 ret = ctnetlink_parse_nat_setup(ct,
960 IP_NAT_MANIP_SRC,
961 cda[CTA_NAT_SRC]);
962 if (ret < 0)
963 return ret;
965 return 0;
966 #else
967 return -EOPNOTSUPP;
968 #endif
971 static inline int
972 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
974 struct nf_conntrack_helper *helper;
975 struct nf_conn_help *help = nfct_help(ct);
976 char *helpname = NULL;
977 int err;
979 /* don't change helper of sibling connections */
980 if (ct->master)
981 return -EBUSY;
983 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
984 if (err < 0)
985 return err;
987 if (!strcmp(helpname, "")) {
988 if (help && help->helper) {
989 /* we had a helper before ... */
990 nf_ct_remove_expectations(ct);
991 rcu_assign_pointer(help->helper, NULL);
994 return 0;
997 helper = __nf_conntrack_helper_find_byname(helpname);
998 if (helper == NULL) {
999 #ifdef CONFIG_MODULES
1000 spin_unlock_bh(&nf_conntrack_lock);
1002 if (request_module("nfct-helper-%s", helpname) < 0) {
1003 spin_lock_bh(&nf_conntrack_lock);
1004 return -EOPNOTSUPP;
1007 spin_lock_bh(&nf_conntrack_lock);
1008 helper = __nf_conntrack_helper_find_byname(helpname);
1009 if (helper)
1010 return -EAGAIN;
1011 #endif
1012 return -EOPNOTSUPP;
1015 if (help) {
1016 if (help->helper == helper)
1017 return 0;
1018 if (help->helper)
1019 return -EBUSY;
1020 /* need to zero data of old helper */
1021 memset(&help->help, 0, sizeof(help->help));
1022 } else {
1023 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1024 if (help == NULL)
1025 return -ENOMEM;
1028 rcu_assign_pointer(help->helper, helper);
1030 return 0;
1033 static inline int
1034 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1036 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1038 if (!del_timer(&ct->timeout))
1039 return -ETIME;
1041 ct->timeout.expires = jiffies + timeout * HZ;
1042 add_timer(&ct->timeout);
1044 return 0;
1047 static inline int
1048 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1050 const struct nlattr *attr = cda[CTA_PROTOINFO];
1051 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1052 struct nf_conntrack_l4proto *l4proto;
1053 int err = 0;
1055 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1057 rcu_read_lock();
1058 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1059 if (l4proto->from_nlattr)
1060 err = l4proto->from_nlattr(tb, ct);
1061 rcu_read_unlock();
1063 return err;
1066 #ifdef CONFIG_NF_NAT_NEEDED
1067 static inline int
1068 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1070 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1072 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1074 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1075 return -EINVAL;
1077 natseq->correction_pos =
1078 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1080 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1081 return -EINVAL;
1083 natseq->offset_before =
1084 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1086 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1087 return -EINVAL;
1089 natseq->offset_after =
1090 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1092 return 0;
1095 static int
1096 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1097 const struct nlattr * const cda[])
1099 int ret = 0;
1100 struct nf_conn_nat *nat = nfct_nat(ct);
1102 if (!nat)
1103 return 0;
1105 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1106 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1107 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1108 if (ret < 0)
1109 return ret;
1111 ct->status |= IPS_SEQ_ADJUST;
1114 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1115 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1116 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1117 if (ret < 0)
1118 return ret;
1120 ct->status |= IPS_SEQ_ADJUST;
1123 return 0;
1125 #endif
1127 static int
1128 ctnetlink_change_conntrack(struct nf_conn *ct,
1129 const struct nlattr * const cda[])
1131 int err;
1133 /* only allow NAT changes and master assignation for new conntracks */
1134 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1135 return -EOPNOTSUPP;
1137 if (cda[CTA_HELP]) {
1138 err = ctnetlink_change_helper(ct, cda);
1139 if (err < 0)
1140 return err;
1143 if (cda[CTA_TIMEOUT]) {
1144 err = ctnetlink_change_timeout(ct, cda);
1145 if (err < 0)
1146 return err;
1149 if (cda[CTA_STATUS]) {
1150 err = ctnetlink_change_status(ct, cda);
1151 if (err < 0)
1152 return err;
1155 if (cda[CTA_PROTOINFO]) {
1156 err = ctnetlink_change_protoinfo(ct, cda);
1157 if (err < 0)
1158 return err;
1161 #if defined(CONFIG_NF_CONNTRACK_MARK)
1162 if (cda[CTA_MARK])
1163 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1164 #endif
1166 #ifdef CONFIG_NF_NAT_NEEDED
1167 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1168 err = ctnetlink_change_nat_seq_adj(ct, cda);
1169 if (err < 0)
1170 return err;
1172 #endif
1174 return 0;
1177 static struct nf_conn *
1178 ctnetlink_create_conntrack(const struct nlattr * const cda[],
1179 struct nf_conntrack_tuple *otuple,
1180 struct nf_conntrack_tuple *rtuple,
1181 u8 u3)
1183 struct nf_conn *ct;
1184 int err = -EINVAL;
1185 struct nf_conntrack_helper *helper;
1187 ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
1188 if (IS_ERR(ct))
1189 return ERR_PTR(-ENOMEM);
1191 if (!cda[CTA_TIMEOUT])
1192 goto err1;
1193 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1195 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1196 ct->status |= IPS_CONFIRMED;
1198 rcu_read_lock();
1199 if (cda[CTA_HELP]) {
1200 char *helpname = NULL;
1202 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1203 if (err < 0)
1204 goto err2;
1206 helper = __nf_conntrack_helper_find_byname(helpname);
1207 if (helper == NULL) {
1208 rcu_read_unlock();
1209 #ifdef CONFIG_MODULES
1210 if (request_module("nfct-helper-%s", helpname) < 0) {
1211 err = -EOPNOTSUPP;
1212 goto err1;
1215 rcu_read_lock();
1216 helper = __nf_conntrack_helper_find_byname(helpname);
1217 if (helper) {
1218 err = -EAGAIN;
1219 goto err2;
1221 rcu_read_unlock();
1222 #endif
1223 err = -EOPNOTSUPP;
1224 goto err1;
1225 } else {
1226 struct nf_conn_help *help;
1228 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1229 if (help == NULL) {
1230 err = -ENOMEM;
1231 goto err2;
1234 /* not in hash table yet so not strictly necessary */
1235 rcu_assign_pointer(help->helper, helper);
1237 } else {
1238 /* try an implicit helper assignation */
1239 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1240 if (err < 0)
1241 goto err2;
1244 if (cda[CTA_STATUS]) {
1245 err = ctnetlink_change_status(ct, cda);
1246 if (err < 0)
1247 goto err2;
1250 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1251 err = ctnetlink_change_nat(ct, cda);
1252 if (err < 0)
1253 goto err2;
1256 #ifdef CONFIG_NF_NAT_NEEDED
1257 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1258 err = ctnetlink_change_nat_seq_adj(ct, cda);
1259 if (err < 0)
1260 goto err2;
1262 #endif
1264 if (cda[CTA_PROTOINFO]) {
1265 err = ctnetlink_change_protoinfo(ct, cda);
1266 if (err < 0)
1267 goto err2;
1270 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1271 nf_ct_ecache_ext_add(ct, GFP_ATOMIC);
1273 #if defined(CONFIG_NF_CONNTRACK_MARK)
1274 if (cda[CTA_MARK])
1275 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1276 #endif
1278 /* setup master conntrack: this is a confirmed expectation */
1279 if (cda[CTA_TUPLE_MASTER]) {
1280 struct nf_conntrack_tuple master;
1281 struct nf_conntrack_tuple_hash *master_h;
1282 struct nf_conn *master_ct;
1284 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1285 if (err < 0)
1286 goto err2;
1288 master_h = nf_conntrack_find_get(&init_net, &master);
1289 if (master_h == NULL) {
1290 err = -ENOENT;
1291 goto err2;
1293 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1294 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1295 ct->master = master_ct;
1298 add_timer(&ct->timeout);
1299 nf_conntrack_hash_insert(ct);
1300 rcu_read_unlock();
1302 return ct;
1304 err2:
1305 rcu_read_unlock();
1306 err1:
1307 nf_conntrack_free(ct);
1308 return ERR_PTR(err);
1311 static int
1312 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1313 const struct nlmsghdr *nlh,
1314 const struct nlattr * const cda[])
1316 struct nf_conntrack_tuple otuple, rtuple;
1317 struct nf_conntrack_tuple_hash *h = NULL;
1318 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1319 u_int8_t u3 = nfmsg->nfgen_family;
1320 int err = 0;
1322 if (cda[CTA_TUPLE_ORIG]) {
1323 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1324 if (err < 0)
1325 return err;
1328 if (cda[CTA_TUPLE_REPLY]) {
1329 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1330 if (err < 0)
1331 return err;
1334 spin_lock_bh(&nf_conntrack_lock);
1335 if (cda[CTA_TUPLE_ORIG])
1336 h = __nf_conntrack_find(&init_net, &otuple);
1337 else if (cda[CTA_TUPLE_REPLY])
1338 h = __nf_conntrack_find(&init_net, &rtuple);
1340 if (h == NULL) {
1341 err = -ENOENT;
1342 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1343 struct nf_conn *ct;
1344 enum ip_conntrack_events events;
1346 ct = ctnetlink_create_conntrack(cda, &otuple,
1347 &rtuple, u3);
1348 if (IS_ERR(ct)) {
1349 err = PTR_ERR(ct);
1350 goto out_unlock;
1352 err = 0;
1353 nf_conntrack_get(&ct->ct_general);
1354 spin_unlock_bh(&nf_conntrack_lock);
1355 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1356 events = IPCT_RELATED;
1357 else
1358 events = IPCT_NEW;
1360 nf_conntrack_eventmask_report((1 << IPCT_STATUS) |
1361 (1 << IPCT_HELPER) |
1362 (1 << IPCT_PROTOINFO) |
1363 (1 << IPCT_NATSEQADJ) |
1364 (1 << IPCT_MARK) | events,
1365 ct, NETLINK_CB(skb).pid,
1366 nlmsg_report(nlh));
1367 nf_ct_put(ct);
1368 } else
1369 spin_unlock_bh(&nf_conntrack_lock);
1371 return err;
1373 /* implicit 'else' */
1375 /* We manipulate the conntrack inside the global conntrack table lock,
1376 * so there's no need to increase the refcount */
1377 err = -EEXIST;
1378 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1379 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1381 err = ctnetlink_change_conntrack(ct, cda);
1382 if (err == 0) {
1383 nf_conntrack_get(&ct->ct_general);
1384 spin_unlock_bh(&nf_conntrack_lock);
1385 nf_conntrack_eventmask_report((1 << IPCT_STATUS) |
1386 (1 << IPCT_HELPER) |
1387 (1 << IPCT_PROTOINFO) |
1388 (1 << IPCT_NATSEQADJ) |
1389 (1 << IPCT_MARK),
1390 ct, NETLINK_CB(skb).pid,
1391 nlmsg_report(nlh));
1392 nf_ct_put(ct);
1393 } else
1394 spin_unlock_bh(&nf_conntrack_lock);
1396 return err;
1399 out_unlock:
1400 spin_unlock_bh(&nf_conntrack_lock);
1401 return err;
1404 /***********************************************************************
1405 * EXPECT
1406 ***********************************************************************/
1408 static inline int
1409 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1410 const struct nf_conntrack_tuple *tuple,
1411 enum ctattr_expect type)
1413 struct nlattr *nest_parms;
1415 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1416 if (!nest_parms)
1417 goto nla_put_failure;
1418 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1419 goto nla_put_failure;
1420 nla_nest_end(skb, nest_parms);
1422 return 0;
1424 nla_put_failure:
1425 return -1;
1428 static inline int
1429 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1430 const struct nf_conntrack_tuple *tuple,
1431 const struct nf_conntrack_tuple_mask *mask)
1433 int ret;
1434 struct nf_conntrack_l3proto *l3proto;
1435 struct nf_conntrack_l4proto *l4proto;
1436 struct nf_conntrack_tuple m;
1437 struct nlattr *nest_parms;
1439 memset(&m, 0xFF, sizeof(m));
1440 m.src.u.all = mask->src.u.all;
1441 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1443 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1444 if (!nest_parms)
1445 goto nla_put_failure;
1447 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1448 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1450 if (unlikely(ret < 0))
1451 goto nla_put_failure;
1453 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1454 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1455 if (unlikely(ret < 0))
1456 goto nla_put_failure;
1458 nla_nest_end(skb, nest_parms);
1460 return 0;
1462 nla_put_failure:
1463 return -1;
1466 static int
1467 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1468 const struct nf_conntrack_expect *exp)
1470 struct nf_conn *master = exp->master;
1471 long timeout = (exp->timeout.expires - jiffies) / HZ;
1473 if (timeout < 0)
1474 timeout = 0;
1476 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1477 goto nla_put_failure;
1478 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1479 goto nla_put_failure;
1480 if (ctnetlink_exp_dump_tuple(skb,
1481 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1482 CTA_EXPECT_MASTER) < 0)
1483 goto nla_put_failure;
1485 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1486 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1488 return 0;
1490 nla_put_failure:
1491 return -1;
1494 static int
1495 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1496 int event, const struct nf_conntrack_expect *exp)
1498 struct nlmsghdr *nlh;
1499 struct nfgenmsg *nfmsg;
1500 unsigned int flags = pid ? NLM_F_MULTI : 0;
1502 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1503 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1504 if (nlh == NULL)
1505 goto nlmsg_failure;
1507 nfmsg = nlmsg_data(nlh);
1508 nfmsg->nfgen_family = exp->tuple.src.l3num;
1509 nfmsg->version = NFNETLINK_V0;
1510 nfmsg->res_id = 0;
1512 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1513 goto nla_put_failure;
1515 nlmsg_end(skb, nlh);
1516 return skb->len;
1518 nlmsg_failure:
1519 nla_put_failure:
1520 nlmsg_cancel(skb, nlh);
1521 return -1;
1524 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1525 static int
1526 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1528 struct nlmsghdr *nlh;
1529 struct nfgenmsg *nfmsg;
1530 struct nf_conntrack_expect *exp = item->exp;
1531 struct sk_buff *skb;
1532 unsigned int type;
1533 int flags = 0;
1535 if (events & (1 << IPEXP_NEW)) {
1536 type = IPCTNL_MSG_EXP_NEW;
1537 flags = NLM_F_CREATE|NLM_F_EXCL;
1538 } else
1539 return 0;
1541 if (!item->report &&
1542 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1543 return 0;
1545 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1546 if (skb == NULL)
1547 goto errout;
1549 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1550 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1551 if (nlh == NULL)
1552 goto nlmsg_failure;
1554 nfmsg = nlmsg_data(nlh);
1555 nfmsg->nfgen_family = exp->tuple.src.l3num;
1556 nfmsg->version = NFNETLINK_V0;
1557 nfmsg->res_id = 0;
1559 rcu_read_lock();
1560 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1561 goto nla_put_failure;
1562 rcu_read_unlock();
1564 nlmsg_end(skb, nlh);
1565 nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW,
1566 item->report, GFP_ATOMIC);
1567 return 0;
1569 nla_put_failure:
1570 rcu_read_unlock();
1571 nlmsg_cancel(skb, nlh);
1572 nlmsg_failure:
1573 kfree_skb(skb);
1574 errout:
1575 nfnetlink_set_err(0, 0, -ENOBUFS);
1576 return 0;
1578 #endif
1579 static int ctnetlink_exp_done(struct netlink_callback *cb)
1581 if (cb->args[1])
1582 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1583 return 0;
1586 static int
1587 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1589 struct net *net = &init_net;
1590 struct nf_conntrack_expect *exp, *last;
1591 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1592 struct hlist_node *n;
1593 u_int8_t l3proto = nfmsg->nfgen_family;
1595 rcu_read_lock();
1596 last = (struct nf_conntrack_expect *)cb->args[1];
1597 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1598 restart:
1599 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1600 hnode) {
1601 if (l3proto && exp->tuple.src.l3num != l3proto)
1602 continue;
1603 if (cb->args[1]) {
1604 if (exp != last)
1605 continue;
1606 cb->args[1] = 0;
1608 if (ctnetlink_exp_fill_info(skb,
1609 NETLINK_CB(cb->skb).pid,
1610 cb->nlh->nlmsg_seq,
1611 IPCTNL_MSG_EXP_NEW,
1612 exp) < 0) {
1613 if (!atomic_inc_not_zero(&exp->use))
1614 continue;
1615 cb->args[1] = (unsigned long)exp;
1616 goto out;
1619 if (cb->args[1]) {
1620 cb->args[1] = 0;
1621 goto restart;
1624 out:
1625 rcu_read_unlock();
1626 if (last)
1627 nf_ct_expect_put(last);
1629 return skb->len;
1632 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1633 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1634 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1637 static int
1638 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1639 const struct nlmsghdr *nlh,
1640 const struct nlattr * const cda[])
1642 struct nf_conntrack_tuple tuple;
1643 struct nf_conntrack_expect *exp;
1644 struct sk_buff *skb2;
1645 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1646 u_int8_t u3 = nfmsg->nfgen_family;
1647 int err = 0;
1649 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1650 return netlink_dump_start(ctnl, skb, nlh,
1651 ctnetlink_exp_dump_table,
1652 ctnetlink_exp_done);
1655 if (cda[CTA_EXPECT_MASTER])
1656 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1657 else
1658 return -EINVAL;
1660 if (err < 0)
1661 return err;
1663 exp = nf_ct_expect_find_get(&init_net, &tuple);
1664 if (!exp)
1665 return -ENOENT;
1667 if (cda[CTA_EXPECT_ID]) {
1668 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1669 if (ntohl(id) != (u32)(unsigned long)exp) {
1670 nf_ct_expect_put(exp);
1671 return -ENOENT;
1675 err = -ENOMEM;
1676 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1677 if (skb2 == NULL)
1678 goto out;
1680 rcu_read_lock();
1681 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1682 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1683 rcu_read_unlock();
1684 if (err <= 0)
1685 goto free;
1687 nf_ct_expect_put(exp);
1689 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1691 free:
1692 kfree_skb(skb2);
1693 out:
1694 nf_ct_expect_put(exp);
1695 return err;
1698 static int
1699 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1700 const struct nlmsghdr *nlh,
1701 const struct nlattr * const cda[])
1703 struct nf_conntrack_expect *exp;
1704 struct nf_conntrack_tuple tuple;
1705 struct nf_conntrack_helper *h;
1706 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1707 struct hlist_node *n, *next;
1708 u_int8_t u3 = nfmsg->nfgen_family;
1709 unsigned int i;
1710 int err;
1712 if (cda[CTA_EXPECT_TUPLE]) {
1713 /* delete a single expect by tuple */
1714 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1715 if (err < 0)
1716 return err;
1718 /* bump usage count to 2 */
1719 exp = nf_ct_expect_find_get(&init_net, &tuple);
1720 if (!exp)
1721 return -ENOENT;
1723 if (cda[CTA_EXPECT_ID]) {
1724 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1725 if (ntohl(id) != (u32)(unsigned long)exp) {
1726 nf_ct_expect_put(exp);
1727 return -ENOENT;
1731 /* after list removal, usage count == 1 */
1732 nf_ct_unexpect_related(exp);
1733 /* have to put what we 'get' above.
1734 * after this line usage count == 0 */
1735 nf_ct_expect_put(exp);
1736 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1737 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1738 struct nf_conn_help *m_help;
1740 /* delete all expectations for this helper */
1741 spin_lock_bh(&nf_conntrack_lock);
1742 h = __nf_conntrack_helper_find_byname(name);
1743 if (!h) {
1744 spin_unlock_bh(&nf_conntrack_lock);
1745 return -EOPNOTSUPP;
1747 for (i = 0; i < nf_ct_expect_hsize; i++) {
1748 hlist_for_each_entry_safe(exp, n, next,
1749 &init_net.ct.expect_hash[i],
1750 hnode) {
1751 m_help = nfct_help(exp->master);
1752 if (m_help->helper == h
1753 && del_timer(&exp->timeout)) {
1754 nf_ct_unlink_expect(exp);
1755 nf_ct_expect_put(exp);
1759 spin_unlock_bh(&nf_conntrack_lock);
1760 } else {
1761 /* This basically means we have to flush everything*/
1762 spin_lock_bh(&nf_conntrack_lock);
1763 for (i = 0; i < nf_ct_expect_hsize; i++) {
1764 hlist_for_each_entry_safe(exp, n, next,
1765 &init_net.ct.expect_hash[i],
1766 hnode) {
1767 if (del_timer(&exp->timeout)) {
1768 nf_ct_unlink_expect(exp);
1769 nf_ct_expect_put(exp);
1773 spin_unlock_bh(&nf_conntrack_lock);
1776 return 0;
1778 static int
1779 ctnetlink_change_expect(struct nf_conntrack_expect *x,
1780 const struct nlattr * const cda[])
1782 return -EOPNOTSUPP;
1785 static int
1786 ctnetlink_create_expect(const struct nlattr * const cda[], u_int8_t u3,
1787 u32 pid, int report)
1789 struct nf_conntrack_tuple tuple, mask, master_tuple;
1790 struct nf_conntrack_tuple_hash *h = NULL;
1791 struct nf_conntrack_expect *exp;
1792 struct nf_conn *ct;
1793 struct nf_conn_help *help;
1794 int err = 0;
1796 /* caller guarantees that those three CTA_EXPECT_* exist */
1797 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1798 if (err < 0)
1799 return err;
1800 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1801 if (err < 0)
1802 return err;
1803 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1804 if (err < 0)
1805 return err;
1807 /* Look for master conntrack of this expectation */
1808 h = nf_conntrack_find_get(&init_net, &master_tuple);
1809 if (!h)
1810 return -ENOENT;
1811 ct = nf_ct_tuplehash_to_ctrack(h);
1812 help = nfct_help(ct);
1814 if (!help || !help->helper) {
1815 /* such conntrack hasn't got any helper, abort */
1816 err = -EOPNOTSUPP;
1817 goto out;
1820 exp = nf_ct_expect_alloc(ct);
1821 if (!exp) {
1822 err = -ENOMEM;
1823 goto out;
1826 exp->class = 0;
1827 exp->expectfn = NULL;
1828 exp->flags = 0;
1829 exp->master = ct;
1830 exp->helper = NULL;
1831 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1832 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1833 exp->mask.src.u.all = mask.src.u.all;
1835 err = nf_ct_expect_related_report(exp, pid, report);
1836 nf_ct_expect_put(exp);
1838 out:
1839 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1840 return err;
1843 static int
1844 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1845 const struct nlmsghdr *nlh,
1846 const struct nlattr * const cda[])
1848 struct nf_conntrack_tuple tuple;
1849 struct nf_conntrack_expect *exp;
1850 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1851 u_int8_t u3 = nfmsg->nfgen_family;
1852 int err = 0;
1854 if (!cda[CTA_EXPECT_TUPLE]
1855 || !cda[CTA_EXPECT_MASK]
1856 || !cda[CTA_EXPECT_MASTER])
1857 return -EINVAL;
1859 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1860 if (err < 0)
1861 return err;
1863 spin_lock_bh(&nf_conntrack_lock);
1864 exp = __nf_ct_expect_find(&init_net, &tuple);
1866 if (!exp) {
1867 spin_unlock_bh(&nf_conntrack_lock);
1868 err = -ENOENT;
1869 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1870 err = ctnetlink_create_expect(cda,
1872 NETLINK_CB(skb).pid,
1873 nlmsg_report(nlh));
1875 return err;
1878 err = -EEXIST;
1879 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1880 err = ctnetlink_change_expect(exp, cda);
1881 spin_unlock_bh(&nf_conntrack_lock);
1883 return err;
1886 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1887 static struct nf_ct_event_notifier ctnl_notifier = {
1888 .fcn = ctnetlink_conntrack_event,
1891 static struct nf_exp_event_notifier ctnl_notifier_exp = {
1892 .fcn = ctnetlink_expect_event,
1894 #endif
1896 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1897 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1898 .attr_count = CTA_MAX,
1899 .policy = ct_nla_policy },
1900 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1901 .attr_count = CTA_MAX,
1902 .policy = ct_nla_policy },
1903 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1904 .attr_count = CTA_MAX,
1905 .policy = ct_nla_policy },
1906 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1907 .attr_count = CTA_MAX,
1908 .policy = ct_nla_policy },
1911 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1912 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1913 .attr_count = CTA_EXPECT_MAX,
1914 .policy = exp_nla_policy },
1915 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1916 .attr_count = CTA_EXPECT_MAX,
1917 .policy = exp_nla_policy },
1918 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1919 .attr_count = CTA_EXPECT_MAX,
1920 .policy = exp_nla_policy },
1923 static const struct nfnetlink_subsystem ctnl_subsys = {
1924 .name = "conntrack",
1925 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1926 .cb_count = IPCTNL_MSG_MAX,
1927 .cb = ctnl_cb,
1930 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1931 .name = "conntrack_expect",
1932 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1933 .cb_count = IPCTNL_MSG_EXP_MAX,
1934 .cb = ctnl_exp_cb,
1937 MODULE_ALIAS("ip_conntrack_netlink");
1938 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1939 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1941 static int __init ctnetlink_init(void)
1943 int ret;
1945 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1946 ret = nfnetlink_subsys_register(&ctnl_subsys);
1947 if (ret < 0) {
1948 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1949 goto err_out;
1952 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1953 if (ret < 0) {
1954 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1955 goto err_unreg_subsys;
1958 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1959 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1960 if (ret < 0) {
1961 printk("ctnetlink_init: cannot register notifier.\n");
1962 goto err_unreg_exp_subsys;
1965 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1966 if (ret < 0) {
1967 printk("ctnetlink_init: cannot expect register notifier.\n");
1968 goto err_unreg_notifier;
1970 #endif
1972 return 0;
1974 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1975 err_unreg_notifier:
1976 nf_conntrack_unregister_notifier(&ctnl_notifier);
1977 err_unreg_exp_subsys:
1978 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1979 #endif
1980 err_unreg_subsys:
1981 nfnetlink_subsys_unregister(&ctnl_subsys);
1982 err_out:
1983 return ret;
1986 static void __exit ctnetlink_exit(void)
1988 printk("ctnetlink: unregistering from nfnetlink.\n");
1990 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1991 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1992 nf_conntrack_unregister_notifier(&ctnl_notifier);
1993 #endif
1995 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1996 nfnetlink_subsys_unregister(&ctnl_subsys);
1997 return;
2000 module_init(ctnetlink_init);
2001 module_exit(ctnetlink_exit);