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-2012 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/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_l3proto.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_tuple.h>
43 #include <net/netfilter/nf_conntrack_acct.h>
44 #include <net/netfilter/nf_conntrack_zones.h>
45 #include <net/netfilter/nf_conntrack_timestamp.h>
46 #ifdef CONFIG_NF_NAT_NEEDED
47 #include <net/netfilter/nf_nat_core.h>
48 #include <net/netfilter/nf_nat_l4proto.h>
49 #include <net/netfilter/nf_nat_helper.h>
52 #include <linux/netfilter/nfnetlink.h>
53 #include <linux/netfilter/nfnetlink_conntrack.h>
55 MODULE_LICENSE("GPL");
57 static char __initdata version[] = "0.93";
60 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
61 const struct nf_conntrack_tuple *tuple,
62 struct nf_conntrack_l4proto *l4proto)
65 struct nlattr *nest_parms;
67 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
70 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
73 if (likely(l4proto->tuple_to_nlattr))
74 ret = l4proto->tuple_to_nlattr(skb, tuple);
76 nla_nest_end(skb, nest_parms);
85 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
86 const struct nf_conntrack_tuple *tuple,
87 struct nf_conntrack_l3proto *l3proto)
90 struct nlattr *nest_parms;
92 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
96 if (likely(l3proto->tuple_to_nlattr))
97 ret = l3proto->tuple_to_nlattr(skb, tuple);
99 nla_nest_end(skb, nest_parms);
108 ctnetlink_dump_tuples(struct sk_buff *skb,
109 const struct nf_conntrack_tuple *tuple)
112 struct nf_conntrack_l3proto *l3proto;
113 struct nf_conntrack_l4proto *l4proto;
116 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
117 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
120 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
121 tuple->dst.protonum);
122 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
129 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
131 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
132 goto nla_put_failure;
140 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
142 long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
147 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
148 goto nla_put_failure;
156 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
158 struct nf_conntrack_l4proto *l4proto;
159 struct nlattr *nest_proto;
162 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
163 if (!l4proto->to_nlattr)
166 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
168 goto nla_put_failure;
170 ret = l4proto->to_nlattr(skb, nest_proto, ct);
172 nla_nest_end(skb, nest_proto);
181 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
183 struct nlattr *nest_helper;
184 const struct nf_conn_help *help = nfct_help(ct);
185 struct nf_conntrack_helper *helper;
190 helper = rcu_dereference(help->helper);
194 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
196 goto nla_put_failure;
197 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
198 goto nla_put_failure;
200 if (helper->to_nlattr)
201 helper->to_nlattr(skb, ct);
203 nla_nest_end(skb, nest_helper);
212 dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes,
213 enum ip_conntrack_dir dir)
215 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
216 struct nlattr *nest_count;
218 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
220 goto nla_put_failure;
222 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) ||
223 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes)))
224 goto nla_put_failure;
226 nla_nest_end(skb, nest_count);
235 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
236 enum ip_conntrack_dir dir, int type)
238 struct nf_conn_counter *acct;
241 acct = nf_conn_acct_find(ct);
245 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
246 pkts = atomic64_xchg(&acct[dir].packets, 0);
247 bytes = atomic64_xchg(&acct[dir].bytes, 0);
249 pkts = atomic64_read(&acct[dir].packets);
250 bytes = atomic64_read(&acct[dir].bytes);
252 return dump_counters(skb, pkts, bytes, dir);
256 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
258 struct nlattr *nest_count;
259 const struct nf_conn_tstamp *tstamp;
261 tstamp = nf_conn_tstamp_find(ct);
265 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
267 goto nla_put_failure;
269 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) ||
270 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
271 cpu_to_be64(tstamp->stop))))
272 goto nla_put_failure;
273 nla_nest_end(skb, nest_count);
281 #ifdef CONFIG_NF_CONNTRACK_MARK
283 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
285 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
286 goto nla_put_failure;
293 #define ctnetlink_dump_mark(a, b) (0)
296 #ifdef CONFIG_NF_CONNTRACK_SECMARK
298 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
300 struct nlattr *nest_secctx;
304 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
309 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
311 goto nla_put_failure;
313 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
314 goto nla_put_failure;
315 nla_nest_end(skb, nest_secctx);
319 security_release_secctx(secctx, len);
323 #define ctnetlink_dump_secctx(a, b) (0)
326 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
329 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
331 struct nlattr *nest_parms;
333 if (!(ct->status & IPS_EXPECTED))
336 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
338 goto nla_put_failure;
339 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
340 goto nla_put_failure;
341 nla_nest_end(skb, nest_parms);
349 #ifdef CONFIG_NF_NAT_NEEDED
351 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
353 struct nlattr *nest_parms;
355 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
357 goto nla_put_failure;
359 if (nla_put_be32(skb, CTA_NAT_SEQ_CORRECTION_POS,
360 htonl(natseq->correction_pos)) ||
361 nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
362 htonl(natseq->offset_before)) ||
363 nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
364 htonl(natseq->offset_after)))
365 goto nla_put_failure;
367 nla_nest_end(skb, nest_parms);
376 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
378 struct nf_nat_seq *natseq;
379 struct nf_conn_nat *nat = nfct_nat(ct);
381 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
384 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
385 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
388 natseq = &nat->seq[IP_CT_DIR_REPLY];
389 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
395 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
399 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
401 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
402 goto nla_put_failure;
410 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
412 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
413 goto nla_put_failure;
421 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
424 struct nlmsghdr *nlh;
425 struct nfgenmsg *nfmsg;
426 struct nlattr *nest_parms;
427 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
429 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
430 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
434 nfmsg = nlmsg_data(nlh);
435 nfmsg->nfgen_family = nf_ct_l3num(ct);
436 nfmsg->version = NFNETLINK_V0;
439 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
441 goto nla_put_failure;
442 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
443 goto nla_put_failure;
444 nla_nest_end(skb, nest_parms);
446 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
448 goto nla_put_failure;
449 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
450 goto nla_put_failure;
451 nla_nest_end(skb, nest_parms);
453 if (nf_ct_zone(ct) &&
454 nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
455 goto nla_put_failure;
457 if (ctnetlink_dump_status(skb, ct) < 0 ||
458 ctnetlink_dump_timeout(skb, ct) < 0 ||
459 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 ||
460 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 ||
461 ctnetlink_dump_timestamp(skb, ct) < 0 ||
462 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
463 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
464 ctnetlink_dump_mark(skb, ct) < 0 ||
465 ctnetlink_dump_secctx(skb, ct) < 0 ||
466 ctnetlink_dump_id(skb, ct) < 0 ||
467 ctnetlink_dump_use(skb, ct) < 0 ||
468 ctnetlink_dump_master(skb, ct) < 0 ||
469 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
470 goto nla_put_failure;
477 nlmsg_cancel(skb, nlh);
482 ctnetlink_proto_size(const struct nf_conn *ct)
484 struct nf_conntrack_l3proto *l3proto;
485 struct nf_conntrack_l4proto *l4proto;
489 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
490 len += l3proto->nla_size;
492 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
493 len += l4proto->nla_size;
500 ctnetlink_counters_size(const struct nf_conn *ct)
502 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
504 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
505 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
506 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
511 ctnetlink_secctx_size(const struct nf_conn *ct)
513 #ifdef CONFIG_NF_CONNTRACK_SECMARK
516 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
520 return nla_total_size(0) /* CTA_SECCTX */
521 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
528 ctnetlink_timestamp_size(const struct nf_conn *ct)
530 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
531 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
533 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
540 ctnetlink_nlmsg_size(const struct nf_conn *ct)
542 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
543 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
544 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
545 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
546 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
547 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
548 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
549 + ctnetlink_counters_size(ct)
550 + ctnetlink_timestamp_size(ct)
551 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
552 + nla_total_size(0) /* CTA_PROTOINFO */
553 + nla_total_size(0) /* CTA_HELP */
554 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
555 + ctnetlink_secctx_size(ct)
556 #ifdef CONFIG_NF_NAT_NEEDED
557 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
558 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
560 #ifdef CONFIG_NF_CONNTRACK_MARK
561 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
563 + ctnetlink_proto_size(ct)
567 #ifdef CONFIG_NF_CONNTRACK_EVENTS
569 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
572 struct nlmsghdr *nlh;
573 struct nfgenmsg *nfmsg;
574 struct nlattr *nest_parms;
575 struct nf_conn *ct = item->ct;
578 unsigned int flags = 0, group;
581 /* ignore our fake conntrack entry */
582 if (nf_ct_is_untracked(ct))
585 if (events & (1 << IPCT_DESTROY)) {
586 type = IPCTNL_MSG_CT_DELETE;
587 group = NFNLGRP_CONNTRACK_DESTROY;
588 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
589 type = IPCTNL_MSG_CT_NEW;
590 flags = NLM_F_CREATE|NLM_F_EXCL;
591 group = NFNLGRP_CONNTRACK_NEW;
593 type = IPCTNL_MSG_CT_NEW;
594 group = NFNLGRP_CONNTRACK_UPDATE;
599 if (!item->report && !nfnetlink_has_listeners(net, group))
602 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
606 type |= NFNL_SUBSYS_CTNETLINK << 8;
607 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
611 nfmsg = nlmsg_data(nlh);
612 nfmsg->nfgen_family = nf_ct_l3num(ct);
613 nfmsg->version = NFNETLINK_V0;
617 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
619 goto nla_put_failure;
620 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
621 goto nla_put_failure;
622 nla_nest_end(skb, nest_parms);
624 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
626 goto nla_put_failure;
627 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
628 goto nla_put_failure;
629 nla_nest_end(skb, nest_parms);
631 if (nf_ct_zone(ct) &&
632 nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
633 goto nla_put_failure;
635 if (ctnetlink_dump_id(skb, ct) < 0)
636 goto nla_put_failure;
638 if (ctnetlink_dump_status(skb, ct) < 0)
639 goto nla_put_failure;
641 if (events & (1 << IPCT_DESTROY)) {
642 if (ctnetlink_dump_counters(skb, ct,
643 IP_CT_DIR_ORIGINAL, type) < 0 ||
644 ctnetlink_dump_counters(skb, ct,
645 IP_CT_DIR_REPLY, type) < 0 ||
646 ctnetlink_dump_timestamp(skb, ct) < 0)
647 goto nla_put_failure;
649 if (ctnetlink_dump_timeout(skb, ct) < 0)
650 goto nla_put_failure;
652 if (events & (1 << IPCT_PROTOINFO)
653 && ctnetlink_dump_protoinfo(skb, ct) < 0)
654 goto nla_put_failure;
656 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
657 && ctnetlink_dump_helpinfo(skb, ct) < 0)
658 goto nla_put_failure;
660 #ifdef CONFIG_NF_CONNTRACK_SECMARK
661 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
662 && ctnetlink_dump_secctx(skb, ct) < 0)
663 goto nla_put_failure;
666 if (events & (1 << IPCT_RELATED) &&
667 ctnetlink_dump_master(skb, ct) < 0)
668 goto nla_put_failure;
670 if (events & (1 << IPCT_NATSEQADJ) &&
671 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
672 goto nla_put_failure;
675 #ifdef CONFIG_NF_CONNTRACK_MARK
676 if ((events & (1 << IPCT_MARK) || ct->mark)
677 && ctnetlink_dump_mark(skb, ct) < 0)
678 goto nla_put_failure;
683 err = nfnetlink_send(skb, net, item->portid, group, item->report,
685 if (err == -ENOBUFS || err == -EAGAIN)
692 nlmsg_cancel(skb, nlh);
696 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
701 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
703 static int ctnetlink_done(struct netlink_callback *cb)
706 nf_ct_put((struct nf_conn *)cb->args[1]);
712 struct ctnetlink_dump_filter {
720 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
722 struct net *net = sock_net(skb->sk);
723 struct nf_conn *ct, *last;
724 struct nf_conntrack_tuple_hash *h;
725 struct hlist_nulls_node *n;
726 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
727 u_int8_t l3proto = nfmsg->nfgen_family;
729 #ifdef CONFIG_NF_CONNTRACK_MARK
730 const struct ctnetlink_dump_filter *filter = cb->data;
733 spin_lock_bh(&nf_conntrack_lock);
734 last = (struct nf_conn *)cb->args[1];
735 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
737 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
739 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
741 ct = nf_ct_tuplehash_to_ctrack(h);
742 /* Dump entries of a given L3 protocol number.
743 * If it is not specified, ie. l3proto == 0,
744 * then dump everything. */
745 if (l3proto && nf_ct_l3num(ct) != l3proto)
752 #ifdef CONFIG_NF_CONNTRACK_MARK
753 if (filter && !((ct->mark & filter->mark.mask) ==
760 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
762 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
766 nf_conntrack_get(&ct->ct_general);
767 cb->args[1] = (unsigned long)ct;
777 spin_unlock_bh(&nf_conntrack_lock);
785 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
787 struct nlattr *tb[CTA_IP_MAX+1];
788 struct nf_conntrack_l3proto *l3proto;
791 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
794 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
796 if (likely(l3proto->nlattr_to_tuple)) {
797 ret = nla_validate_nested(attr, CTA_IP_MAX,
798 l3proto->nla_policy);
800 ret = l3proto->nlattr_to_tuple(tb, tuple);
808 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
809 [CTA_PROTO_NUM] = { .type = NLA_U8 },
813 ctnetlink_parse_tuple_proto(struct nlattr *attr,
814 struct nf_conntrack_tuple *tuple)
816 struct nlattr *tb[CTA_PROTO_MAX+1];
817 struct nf_conntrack_l4proto *l4proto;
820 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
824 if (!tb[CTA_PROTO_NUM])
826 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
829 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
831 if (likely(l4proto->nlattr_to_tuple)) {
832 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
833 l4proto->nla_policy);
835 ret = l4proto->nlattr_to_tuple(tb, tuple);
843 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
844 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
845 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
849 ctnetlink_parse_tuple(const struct nlattr * const cda[],
850 struct nf_conntrack_tuple *tuple,
851 enum ctattr_type type, u_int8_t l3num)
853 struct nlattr *tb[CTA_TUPLE_MAX+1];
856 memset(tuple, 0, sizeof(*tuple));
858 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
860 if (!tb[CTA_TUPLE_IP])
863 tuple->src.l3num = l3num;
865 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
869 if (!tb[CTA_TUPLE_PROTO])
872 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
876 /* orig and expect tuples get DIR_ORIGINAL */
877 if (type == CTA_TUPLE_REPLY)
878 tuple->dst.dir = IP_CT_DIR_REPLY;
880 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
886 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
889 #ifdef CONFIG_NF_CONNTRACK_ZONES
890 *zone = ntohs(nla_get_be16(attr));
900 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
901 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
902 .len = NF_CT_HELPER_NAME_LEN - 1 },
906 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
907 struct nlattr **helpinfo)
909 struct nlattr *tb[CTA_HELP_MAX+1];
911 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
913 if (!tb[CTA_HELP_NAME])
916 *helper_name = nla_data(tb[CTA_HELP_NAME]);
918 if (tb[CTA_HELP_INFO])
919 *helpinfo = tb[CTA_HELP_INFO];
924 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
925 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
926 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
927 [CTA_STATUS] = { .type = NLA_U32 },
928 [CTA_PROTOINFO] = { .type = NLA_NESTED },
929 [CTA_HELP] = { .type = NLA_NESTED },
930 [CTA_NAT_SRC] = { .type = NLA_NESTED },
931 [CTA_TIMEOUT] = { .type = NLA_U32 },
932 [CTA_MARK] = { .type = NLA_U32 },
933 [CTA_ID] = { .type = NLA_U32 },
934 [CTA_NAT_DST] = { .type = NLA_NESTED },
935 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
936 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
937 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
938 [CTA_ZONE] = { .type = NLA_U16 },
939 [CTA_MARK_MASK] = { .type = NLA_U32 },
943 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
944 const struct nlmsghdr *nlh,
945 const struct nlattr * const cda[])
947 struct net *net = sock_net(ctnl);
948 struct nf_conntrack_tuple_hash *h;
949 struct nf_conntrack_tuple tuple;
951 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
952 u_int8_t u3 = nfmsg->nfgen_family;
956 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
960 if (cda[CTA_TUPLE_ORIG])
961 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
962 else if (cda[CTA_TUPLE_REPLY])
963 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
965 /* Flush the whole table */
966 nf_conntrack_flush_report(net,
967 NETLINK_CB(skb).portid,
975 h = nf_conntrack_find_get(net, zone, &tuple);
979 ct = nf_ct_tuplehash_to_ctrack(h);
982 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
983 if (id != (u32)(unsigned long)ct) {
989 if (del_timer(&ct->timeout)) {
990 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
991 NETLINK_CB(skb).portid,
992 nlmsg_report(nlh)) < 0) {
993 nf_ct_delete_from_lists(ct);
994 /* we failed to report the event, try later */
995 nf_ct_dying_timeout(ct);
999 /* death_by_timeout would report the event again */
1000 set_bit(IPS_DYING_BIT, &ct->status);
1001 nf_ct_delete_from_lists(ct);
1010 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
1011 const struct nlmsghdr *nlh,
1012 const struct nlattr * const cda[])
1014 struct net *net = sock_net(ctnl);
1015 struct nf_conntrack_tuple_hash *h;
1016 struct nf_conntrack_tuple tuple;
1018 struct sk_buff *skb2 = NULL;
1019 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1020 u_int8_t u3 = nfmsg->nfgen_family;
1024 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1025 struct netlink_dump_control c = {
1026 .dump = ctnetlink_dump_table,
1027 .done = ctnetlink_done,
1029 #ifdef CONFIG_NF_CONNTRACK_MARK
1030 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1031 struct ctnetlink_dump_filter *filter;
1033 filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1038 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1040 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1044 return netlink_dump_start(ctnl, skb, nlh, &c);
1047 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1051 if (cda[CTA_TUPLE_ORIG])
1052 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1053 else if (cda[CTA_TUPLE_REPLY])
1054 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1061 h = nf_conntrack_find_get(net, zone, &tuple);
1065 ct = nf_ct_tuplehash_to_ctrack(h);
1068 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1075 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1076 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1082 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1091 /* this avoids a loop in nfnetlink. */
1092 return err == -EAGAIN ? -ENOBUFS : err;
1095 static int ctnetlink_done_list(struct netlink_callback *cb)
1098 nf_ct_put((struct nf_conn *)cb->args[1]);
1103 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb,
1104 struct hlist_nulls_head *list)
1106 struct nf_conn *ct, *last;
1107 struct nf_conntrack_tuple_hash *h;
1108 struct hlist_nulls_node *n;
1109 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1110 u_int8_t l3proto = nfmsg->nfgen_family;
1116 spin_lock_bh(&nf_conntrack_lock);
1117 last = (struct nf_conn *)cb->args[1];
1119 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1120 ct = nf_ct_tuplehash_to_ctrack(h);
1121 if (l3proto && nf_ct_l3num(ct) != l3proto)
1129 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1131 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1135 nf_conntrack_get(&ct->ct_general);
1136 cb->args[1] = (unsigned long)ct;
1146 spin_unlock_bh(&nf_conntrack_lock);
1154 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1156 struct net *net = sock_net(skb->sk);
1158 return ctnetlink_dump_list(skb, cb, &net->ct.dying);
1162 ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb,
1163 const struct nlmsghdr *nlh,
1164 const struct nlattr * const cda[])
1166 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1167 struct netlink_dump_control c = {
1168 .dump = ctnetlink_dump_dying,
1169 .done = ctnetlink_done_list,
1171 return netlink_dump_start(ctnl, skb, nlh, &c);
1178 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1180 struct net *net = sock_net(skb->sk);
1182 return ctnetlink_dump_list(skb, cb, &net->ct.unconfirmed);
1186 ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb,
1187 const struct nlmsghdr *nlh,
1188 const struct nlattr * const cda[])
1190 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1191 struct netlink_dump_control c = {
1192 .dump = ctnetlink_dump_unconfirmed,
1193 .done = ctnetlink_done_list,
1195 return netlink_dump_start(ctnl, skb, nlh, &c);
1201 #ifdef CONFIG_NF_NAT_NEEDED
1203 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1204 enum nf_nat_manip_type manip,
1205 const struct nlattr *attr)
1207 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1210 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1211 if (!parse_nat_setup) {
1212 #ifdef CONFIG_MODULES
1215 if (request_module("nf-nat") < 0) {
1222 if (nfnetlink_parse_nat_setup_hook)
1228 err = parse_nat_setup(ct, manip, attr);
1229 if (err == -EAGAIN) {
1230 #ifdef CONFIG_MODULES
1233 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1249 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1252 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1253 d = ct->status ^ status;
1255 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1259 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1260 /* SEEN_REPLY bit can only be set */
1263 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1264 /* ASSURED bit can only be set */
1267 /* Be careful here, modifying NAT bits can screw up things,
1268 * so don't let users modify them directly if they don't pass
1270 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1275 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1277 #ifdef CONFIG_NF_NAT_NEEDED
1280 if (cda[CTA_NAT_DST]) {
1281 ret = ctnetlink_parse_nat_setup(ct,
1287 if (cda[CTA_NAT_SRC]) {
1288 ret = ctnetlink_parse_nat_setup(ct,
1301 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1303 struct nf_conntrack_helper *helper;
1304 struct nf_conn_help *help = nfct_help(ct);
1305 char *helpname = NULL;
1306 struct nlattr *helpinfo = NULL;
1309 /* don't change helper of sibling connections */
1313 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1317 if (!strcmp(helpname, "")) {
1318 if (help && help->helper) {
1319 /* we had a helper before ... */
1320 nf_ct_remove_expectations(ct);
1321 RCU_INIT_POINTER(help->helper, NULL);
1327 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1328 nf_ct_protonum(ct));
1329 if (helper == NULL) {
1330 #ifdef CONFIG_MODULES
1331 spin_unlock_bh(&nf_conntrack_lock);
1333 if (request_module("nfct-helper-%s", helpname) < 0) {
1334 spin_lock_bh(&nf_conntrack_lock);
1338 spin_lock_bh(&nf_conntrack_lock);
1339 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1340 nf_ct_protonum(ct));
1348 if (help->helper == helper) {
1349 /* update private helper data if allowed. */
1350 if (helper->from_nlattr)
1351 helper->from_nlattr(helpinfo, ct);
1357 /* we cannot set a helper for an existing conntrack */
1362 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1364 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1366 if (!del_timer(&ct->timeout))
1369 ct->timeout.expires = jiffies + timeout * HZ;
1370 add_timer(&ct->timeout);
1375 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1376 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1377 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1378 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1382 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1384 const struct nlattr *attr = cda[CTA_PROTOINFO];
1385 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1386 struct nf_conntrack_l4proto *l4proto;
1389 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1392 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1393 if (l4proto->from_nlattr)
1394 err = l4proto->from_nlattr(tb, ct);
1400 #ifdef CONFIG_NF_NAT_NEEDED
1401 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1402 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1403 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1404 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1408 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1410 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1412 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1414 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1417 natseq->correction_pos =
1418 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1420 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1423 natseq->offset_before =
1424 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1426 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1429 natseq->offset_after =
1430 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1436 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1437 const struct nlattr * const cda[])
1440 struct nf_conn_nat *nat = nfct_nat(ct);
1445 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1446 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1447 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1451 ct->status |= IPS_SEQ_ADJUST;
1454 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1455 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1456 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1460 ct->status |= IPS_SEQ_ADJUST;
1468 ctnetlink_change_conntrack(struct nf_conn *ct,
1469 const struct nlattr * const cda[])
1473 /* only allow NAT changes and master assignation for new conntracks */
1474 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1477 if (cda[CTA_HELP]) {
1478 err = ctnetlink_change_helper(ct, cda);
1483 if (cda[CTA_TIMEOUT]) {
1484 err = ctnetlink_change_timeout(ct, cda);
1489 if (cda[CTA_STATUS]) {
1490 err = ctnetlink_change_status(ct, cda);
1495 if (cda[CTA_PROTOINFO]) {
1496 err = ctnetlink_change_protoinfo(ct, cda);
1501 #if defined(CONFIG_NF_CONNTRACK_MARK)
1503 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1506 #ifdef CONFIG_NF_NAT_NEEDED
1507 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1508 err = ctnetlink_change_nat_seq_adj(ct, cda);
1517 static struct nf_conn *
1518 ctnetlink_create_conntrack(struct net *net, u16 zone,
1519 const struct nlattr * const cda[],
1520 struct nf_conntrack_tuple *otuple,
1521 struct nf_conntrack_tuple *rtuple,
1526 struct nf_conntrack_helper *helper;
1527 struct nf_conn_tstamp *tstamp;
1529 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1531 return ERR_PTR(-ENOMEM);
1533 if (!cda[CTA_TIMEOUT])
1535 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1537 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1540 if (cda[CTA_HELP]) {
1541 char *helpname = NULL;
1542 struct nlattr *helpinfo = NULL;
1544 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1548 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1549 nf_ct_protonum(ct));
1550 if (helper == NULL) {
1552 #ifdef CONFIG_MODULES
1553 if (request_module("nfct-helper-%s", helpname) < 0) {
1559 helper = __nf_conntrack_helper_find(helpname,
1561 nf_ct_protonum(ct));
1571 struct nf_conn_help *help;
1573 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1578 /* set private helper data if allowed. */
1579 if (helper->from_nlattr)
1580 helper->from_nlattr(helpinfo, ct);
1582 /* not in hash table yet so not strictly necessary */
1583 RCU_INIT_POINTER(help->helper, helper);
1586 /* try an implicit helper assignation */
1587 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1592 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1593 err = ctnetlink_change_nat(ct, cda);
1598 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1599 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1600 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1601 /* we must add conntrack extensions before confirmation. */
1602 ct->status |= IPS_CONFIRMED;
1604 if (cda[CTA_STATUS]) {
1605 err = ctnetlink_change_status(ct, cda);
1610 #ifdef CONFIG_NF_NAT_NEEDED
1611 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1612 err = ctnetlink_change_nat_seq_adj(ct, cda);
1618 memset(&ct->proto, 0, sizeof(ct->proto));
1619 if (cda[CTA_PROTOINFO]) {
1620 err = ctnetlink_change_protoinfo(ct, cda);
1625 #if defined(CONFIG_NF_CONNTRACK_MARK)
1627 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1630 /* setup master conntrack: this is a confirmed expectation */
1631 if (cda[CTA_TUPLE_MASTER]) {
1632 struct nf_conntrack_tuple master;
1633 struct nf_conntrack_tuple_hash *master_h;
1634 struct nf_conn *master_ct;
1636 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1640 master_h = nf_conntrack_find_get(net, zone, &master);
1641 if (master_h == NULL) {
1645 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1646 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1647 ct->master = master_ct;
1649 tstamp = nf_conn_tstamp_find(ct);
1651 tstamp->start = ktime_to_ns(ktime_get_real());
1653 err = nf_conntrack_hash_check_insert(ct);
1664 nf_conntrack_free(ct);
1665 return ERR_PTR(err);
1669 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1670 const struct nlmsghdr *nlh,
1671 const struct nlattr * const cda[])
1673 struct net *net = sock_net(ctnl);
1674 struct nf_conntrack_tuple otuple, rtuple;
1675 struct nf_conntrack_tuple_hash *h = NULL;
1676 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1678 u_int8_t u3 = nfmsg->nfgen_family;
1682 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1686 if (cda[CTA_TUPLE_ORIG]) {
1687 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1692 if (cda[CTA_TUPLE_REPLY]) {
1693 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1698 if (cda[CTA_TUPLE_ORIG])
1699 h = nf_conntrack_find_get(net, zone, &otuple);
1700 else if (cda[CTA_TUPLE_REPLY])
1701 h = nf_conntrack_find_get(net, zone, &rtuple);
1705 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1706 enum ip_conntrack_events events;
1708 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1714 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1715 events = IPCT_RELATED;
1719 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1720 (1 << IPCT_ASSURED) |
1721 (1 << IPCT_HELPER) |
1722 (1 << IPCT_PROTOINFO) |
1723 (1 << IPCT_NATSEQADJ) |
1724 (1 << IPCT_MARK) | events,
1725 ct, NETLINK_CB(skb).portid,
1732 /* implicit 'else' */
1735 ct = nf_ct_tuplehash_to_ctrack(h);
1736 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1737 spin_lock_bh(&nf_conntrack_lock);
1738 err = ctnetlink_change_conntrack(ct, cda);
1739 spin_unlock_bh(&nf_conntrack_lock);
1741 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1742 (1 << IPCT_ASSURED) |
1743 (1 << IPCT_HELPER) |
1744 (1 << IPCT_PROTOINFO) |
1745 (1 << IPCT_NATSEQADJ) |
1747 ct, NETLINK_CB(skb).portid,
1757 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1758 __u16 cpu, const struct ip_conntrack_stat *st)
1760 struct nlmsghdr *nlh;
1761 struct nfgenmsg *nfmsg;
1762 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1764 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1765 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1769 nfmsg = nlmsg_data(nlh);
1770 nfmsg->nfgen_family = AF_UNSPEC;
1771 nfmsg->version = NFNETLINK_V0;
1772 nfmsg->res_id = htons(cpu);
1774 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1775 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1776 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1777 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1778 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1779 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1780 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1781 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1782 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1783 htonl(st->insert_failed)) ||
1784 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1785 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1786 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1787 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1788 htonl(st->search_restart)))
1789 goto nla_put_failure;
1791 nlmsg_end(skb, nlh);
1796 nlmsg_cancel(skb, nlh);
1801 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1804 struct net *net = sock_net(skb->sk);
1806 if (cb->args[0] == nr_cpu_ids)
1809 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1810 const struct ip_conntrack_stat *st;
1812 if (!cpu_possible(cpu))
1815 st = per_cpu_ptr(net->ct.stat, cpu);
1816 if (ctnetlink_ct_stat_cpu_fill_info(skb,
1817 NETLINK_CB(cb->skb).portid,
1828 ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1829 const struct nlmsghdr *nlh,
1830 const struct nlattr * const cda[])
1832 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1833 struct netlink_dump_control c = {
1834 .dump = ctnetlink_ct_stat_cpu_dump,
1836 return netlink_dump_start(ctnl, skb, nlh, &c);
1843 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
1846 struct nlmsghdr *nlh;
1847 struct nfgenmsg *nfmsg;
1848 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1849 unsigned int nr_conntracks = atomic_read(&net->ct.count);
1851 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
1852 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1856 nfmsg = nlmsg_data(nlh);
1857 nfmsg->nfgen_family = AF_UNSPEC;
1858 nfmsg->version = NFNETLINK_V0;
1861 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1862 goto nla_put_failure;
1864 nlmsg_end(skb, nlh);
1869 nlmsg_cancel(skb, nlh);
1874 ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1875 const struct nlmsghdr *nlh,
1876 const struct nlattr * const cda[])
1878 struct sk_buff *skb2;
1881 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1885 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
1887 NFNL_MSG_TYPE(nlh->nlmsg_type),
1892 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1901 /* this avoids a loop in nfnetlink. */
1902 return err == -EAGAIN ? -ENOBUFS : err;
1905 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
1907 ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
1909 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
1910 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
1911 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
1912 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
1913 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
1914 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
1915 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
1916 + nla_total_size(0) /* CTA_PROTOINFO */
1917 + nla_total_size(0) /* CTA_HELP */
1918 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
1919 + ctnetlink_secctx_size(ct)
1920 #ifdef CONFIG_NF_NAT_NEEDED
1921 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
1922 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
1924 #ifdef CONFIG_NF_CONNTRACK_MARK
1925 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
1927 + ctnetlink_proto_size(ct)
1932 ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
1934 struct nlattr *nest_parms;
1937 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
1939 goto nla_put_failure;
1940 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
1941 goto nla_put_failure;
1942 nla_nest_end(skb, nest_parms);
1944 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
1946 goto nla_put_failure;
1947 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
1948 goto nla_put_failure;
1949 nla_nest_end(skb, nest_parms);
1951 if (nf_ct_zone(ct)) {
1952 if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
1953 goto nla_put_failure;
1956 if (ctnetlink_dump_id(skb, ct) < 0)
1957 goto nla_put_failure;
1959 if (ctnetlink_dump_status(skb, ct) < 0)
1960 goto nla_put_failure;
1962 if (ctnetlink_dump_timeout(skb, ct) < 0)
1963 goto nla_put_failure;
1965 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
1966 goto nla_put_failure;
1968 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
1969 goto nla_put_failure;
1971 #ifdef CONFIG_NF_CONNTRACK_SECMARK
1972 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
1973 goto nla_put_failure;
1975 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
1976 goto nla_put_failure;
1978 if ((ct->status & IPS_SEQ_ADJUST) &&
1979 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
1980 goto nla_put_failure;
1982 #ifdef CONFIG_NF_CONNTRACK_MARK
1983 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
1984 goto nla_put_failure;
1995 ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
1999 if (cda[CTA_TIMEOUT]) {
2000 err = ctnetlink_change_timeout(ct, cda);
2004 if (cda[CTA_STATUS]) {
2005 err = ctnetlink_change_status(ct, cda);
2009 if (cda[CTA_HELP]) {
2010 err = ctnetlink_change_helper(ct, cda);
2014 #if defined(CONFIG_NF_CONNTRACK_MARK)
2016 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2022 ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
2024 struct nlattr *cda[CTA_MAX+1];
2027 nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2029 spin_lock_bh(&nf_conntrack_lock);
2030 ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
2031 spin_unlock_bh(&nf_conntrack_lock);
2036 static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
2037 .build_size = ctnetlink_nfqueue_build_size,
2038 .build = ctnetlink_nfqueue_build,
2039 .parse = ctnetlink_nfqueue_parse,
2041 #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
2043 /***********************************************************************
2045 ***********************************************************************/
2048 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2049 const struct nf_conntrack_tuple *tuple,
2050 enum ctattr_expect type)
2052 struct nlattr *nest_parms;
2054 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2056 goto nla_put_failure;
2057 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2058 goto nla_put_failure;
2059 nla_nest_end(skb, nest_parms);
2068 ctnetlink_exp_dump_mask(struct sk_buff *skb,
2069 const struct nf_conntrack_tuple *tuple,
2070 const struct nf_conntrack_tuple_mask *mask)
2073 struct nf_conntrack_l3proto *l3proto;
2074 struct nf_conntrack_l4proto *l4proto;
2075 struct nf_conntrack_tuple m;
2076 struct nlattr *nest_parms;
2078 memset(&m, 0xFF, sizeof(m));
2079 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2080 m.src.u.all = mask->src.u.all;
2081 m.dst.protonum = tuple->dst.protonum;
2083 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2085 goto nla_put_failure;
2088 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2089 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2091 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2092 tuple->dst.protonum);
2093 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2097 if (unlikely(ret < 0))
2098 goto nla_put_failure;
2100 nla_nest_end(skb, nest_parms);
2108 static const union nf_inet_addr any_addr;
2111 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2112 const struct nf_conntrack_expect *exp)
2114 struct nf_conn *master = exp->master;
2115 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2116 struct nf_conn_help *help;
2117 #ifdef CONFIG_NF_NAT_NEEDED
2118 struct nlattr *nest_parms;
2119 struct nf_conntrack_tuple nat_tuple = {};
2121 struct nf_ct_helper_expectfn *expfn;
2126 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2127 goto nla_put_failure;
2128 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2129 goto nla_put_failure;
2130 if (ctnetlink_exp_dump_tuple(skb,
2131 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2132 CTA_EXPECT_MASTER) < 0)
2133 goto nla_put_failure;
2135 #ifdef CONFIG_NF_NAT_NEEDED
2136 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2137 exp->saved_proto.all) {
2138 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2140 goto nla_put_failure;
2142 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2143 goto nla_put_failure;
2145 nat_tuple.src.l3num = nf_ct_l3num(master);
2146 nat_tuple.src.u3 = exp->saved_addr;
2147 nat_tuple.dst.protonum = nf_ct_protonum(master);
2148 nat_tuple.src.u = exp->saved_proto;
2150 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2151 CTA_EXPECT_NAT_TUPLE) < 0)
2152 goto nla_put_failure;
2153 nla_nest_end(skb, nest_parms);
2156 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2157 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2158 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2159 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2160 goto nla_put_failure;
2161 help = nfct_help(master);
2163 struct nf_conntrack_helper *helper;
2165 helper = rcu_dereference(help->helper);
2167 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2168 goto nla_put_failure;
2170 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2171 if (expfn != NULL &&
2172 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2173 goto nla_put_failure;
2182 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2183 int event, const struct nf_conntrack_expect *exp)
2185 struct nlmsghdr *nlh;
2186 struct nfgenmsg *nfmsg;
2187 unsigned int flags = portid ? NLM_F_MULTI : 0;
2189 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2190 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2194 nfmsg = nlmsg_data(nlh);
2195 nfmsg->nfgen_family = exp->tuple.src.l3num;
2196 nfmsg->version = NFNETLINK_V0;
2199 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2200 goto nla_put_failure;
2202 nlmsg_end(skb, nlh);
2207 nlmsg_cancel(skb, nlh);
2211 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2213 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2215 struct nf_conntrack_expect *exp = item->exp;
2216 struct net *net = nf_ct_exp_net(exp);
2217 struct nlmsghdr *nlh;
2218 struct nfgenmsg *nfmsg;
2219 struct sk_buff *skb;
2220 unsigned int type, group;
2223 if (events & (1 << IPEXP_DESTROY)) {
2224 type = IPCTNL_MSG_EXP_DELETE;
2225 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2226 } else if (events & (1 << IPEXP_NEW)) {
2227 type = IPCTNL_MSG_EXP_NEW;
2228 flags = NLM_F_CREATE|NLM_F_EXCL;
2229 group = NFNLGRP_CONNTRACK_EXP_NEW;
2233 if (!item->report && !nfnetlink_has_listeners(net, group))
2236 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2240 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2241 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2245 nfmsg = nlmsg_data(nlh);
2246 nfmsg->nfgen_family = exp->tuple.src.l3num;
2247 nfmsg->version = NFNETLINK_V0;
2251 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2252 goto nla_put_failure;
2255 nlmsg_end(skb, nlh);
2256 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2261 nlmsg_cancel(skb, nlh);
2265 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2269 static int ctnetlink_exp_done(struct netlink_callback *cb)
2272 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2277 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2279 struct net *net = sock_net(skb->sk);
2280 struct nf_conntrack_expect *exp, *last;
2281 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2282 struct hlist_node *n;
2283 u_int8_t l3proto = nfmsg->nfgen_family;
2286 last = (struct nf_conntrack_expect *)cb->args[1];
2287 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2289 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
2291 if (l3proto && exp->tuple.src.l3num != l3proto)
2298 if (ctnetlink_exp_fill_info(skb,
2299 NETLINK_CB(cb->skb).portid,
2303 if (!atomic_inc_not_zero(&exp->use))
2305 cb->args[1] = (unsigned long)exp;
2317 nf_ct_expect_put(last);
2322 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2323 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2324 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2325 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2326 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2327 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2328 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2329 .len = NF_CT_HELPER_NAME_LEN - 1 },
2330 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2331 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2332 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2333 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2334 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2338 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
2339 const struct nlmsghdr *nlh,
2340 const struct nlattr * const cda[])
2342 struct net *net = sock_net(ctnl);
2343 struct nf_conntrack_tuple tuple;
2344 struct nf_conntrack_expect *exp;
2345 struct sk_buff *skb2;
2346 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2347 u_int8_t u3 = nfmsg->nfgen_family;
2351 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2352 struct netlink_dump_control c = {
2353 .dump = ctnetlink_exp_dump_table,
2354 .done = ctnetlink_exp_done,
2356 return netlink_dump_start(ctnl, skb, nlh, &c);
2359 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2363 if (cda[CTA_EXPECT_TUPLE])
2364 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2365 else if (cda[CTA_EXPECT_MASTER])
2366 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2373 exp = nf_ct_expect_find_get(net, zone, &tuple);
2377 if (cda[CTA_EXPECT_ID]) {
2378 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2379 if (ntohl(id) != (u32)(unsigned long)exp) {
2380 nf_ct_expect_put(exp);
2386 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2388 nf_ct_expect_put(exp);
2393 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2394 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2396 nf_ct_expect_put(exp);
2400 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2409 /* this avoids a loop in nfnetlink. */
2410 return err == -EAGAIN ? -ENOBUFS : err;
2414 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
2415 const struct nlmsghdr *nlh,
2416 const struct nlattr * const cda[])
2418 struct net *net = sock_net(ctnl);
2419 struct nf_conntrack_expect *exp;
2420 struct nf_conntrack_tuple tuple;
2421 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2422 struct hlist_node *n, *next;
2423 u_int8_t u3 = nfmsg->nfgen_family;
2428 if (cda[CTA_EXPECT_TUPLE]) {
2429 /* delete a single expect by tuple */
2430 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2434 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2438 /* bump usage count to 2 */
2439 exp = nf_ct_expect_find_get(net, zone, &tuple);
2443 if (cda[CTA_EXPECT_ID]) {
2444 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2445 if (ntohl(id) != (u32)(unsigned long)exp) {
2446 nf_ct_expect_put(exp);
2451 /* after list removal, usage count == 1 */
2452 spin_lock_bh(&nf_conntrack_lock);
2453 if (del_timer(&exp->timeout)) {
2454 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2456 nf_ct_expect_put(exp);
2458 spin_unlock_bh(&nf_conntrack_lock);
2459 /* have to put what we 'get' above.
2460 * after this line usage count == 0 */
2461 nf_ct_expect_put(exp);
2462 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2463 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2464 struct nf_conn_help *m_help;
2466 /* delete all expectations for this helper */
2467 spin_lock_bh(&nf_conntrack_lock);
2468 for (i = 0; i < nf_ct_expect_hsize; i++) {
2469 hlist_for_each_entry_safe(exp, n, next,
2470 &net->ct.expect_hash[i],
2472 m_help = nfct_help(exp->master);
2473 if (!strcmp(m_help->helper->name, name) &&
2474 del_timer(&exp->timeout)) {
2475 nf_ct_unlink_expect_report(exp,
2476 NETLINK_CB(skb).portid,
2478 nf_ct_expect_put(exp);
2482 spin_unlock_bh(&nf_conntrack_lock);
2484 /* This basically means we have to flush everything*/
2485 spin_lock_bh(&nf_conntrack_lock);
2486 for (i = 0; i < nf_ct_expect_hsize; i++) {
2487 hlist_for_each_entry_safe(exp, n, next,
2488 &net->ct.expect_hash[i],
2490 if (del_timer(&exp->timeout)) {
2491 nf_ct_unlink_expect_report(exp,
2492 NETLINK_CB(skb).portid,
2494 nf_ct_expect_put(exp);
2498 spin_unlock_bh(&nf_conntrack_lock);
2504 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2505 const struct nlattr * const cda[])
2507 if (cda[CTA_EXPECT_TIMEOUT]) {
2508 if (!del_timer(&x->timeout))
2511 x->timeout.expires = jiffies +
2512 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2513 add_timer(&x->timeout);
2518 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2519 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2520 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2524 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2525 struct nf_conntrack_expect *exp,
2528 #ifdef CONFIG_NF_NAT_NEEDED
2529 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2530 struct nf_conntrack_tuple nat_tuple = {};
2533 nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2535 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2538 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2539 &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2543 exp->saved_addr = nat_tuple.src.u3;
2544 exp->saved_proto = nat_tuple.src.u;
2545 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2554 ctnetlink_create_expect(struct net *net, u16 zone,
2555 const struct nlattr * const cda[],
2557 u32 portid, int report)
2559 struct nf_conntrack_tuple tuple, mask, master_tuple;
2560 struct nf_conntrack_tuple_hash *h = NULL;
2561 struct nf_conntrack_expect *exp;
2563 struct nf_conn_help *help;
2564 struct nf_conntrack_helper *helper = NULL;
2565 u_int32_t class = 0;
2568 /* caller guarantees that those three CTA_EXPECT_* exist */
2569 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2572 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2575 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2579 /* Look for master conntrack of this expectation */
2580 h = nf_conntrack_find_get(net, zone, &master_tuple);
2583 ct = nf_ct_tuplehash_to_ctrack(h);
2585 /* Look for helper of this expectation */
2586 if (cda[CTA_EXPECT_HELP_NAME]) {
2587 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2589 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2590 nf_ct_protonum(ct));
2591 if (helper == NULL) {
2592 #ifdef CONFIG_MODULES
2593 if (request_module("nfct-helper-%s", helpname) < 0) {
2598 helper = __nf_conntrack_helper_find(helpname,
2600 nf_ct_protonum(ct));
2611 if (cda[CTA_EXPECT_CLASS] && helper) {
2612 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2613 if (class > helper->expect_class_max) {
2618 exp = nf_ct_expect_alloc(ct);
2623 help = nfct_help(ct);
2625 if (!cda[CTA_EXPECT_TIMEOUT]) {
2629 exp->timeout.expires =
2630 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2632 exp->flags = NF_CT_EXPECT_USERSPACE;
2633 if (cda[CTA_EXPECT_FLAGS]) {
2635 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2638 if (cda[CTA_EXPECT_FLAGS]) {
2639 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2640 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2644 if (cda[CTA_EXPECT_FN]) {
2645 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2646 struct nf_ct_helper_expectfn *expfn;
2648 expfn = nf_ct_helper_expectfn_find_by_name(name);
2649 if (expfn == NULL) {
2653 exp->expectfn = expfn->expectfn;
2655 exp->expectfn = NULL;
2659 exp->helper = helper;
2660 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2661 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2662 exp->mask.src.u.all = mask.src.u.all;
2664 if (cda[CTA_EXPECT_NAT]) {
2665 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2670 err = nf_ct_expect_related_report(exp, portid, report);
2672 nf_ct_expect_put(exp);
2674 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2679 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2680 const struct nlmsghdr *nlh,
2681 const struct nlattr * const cda[])
2683 struct net *net = sock_net(ctnl);
2684 struct nf_conntrack_tuple tuple;
2685 struct nf_conntrack_expect *exp;
2686 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2687 u_int8_t u3 = nfmsg->nfgen_family;
2691 if (!cda[CTA_EXPECT_TUPLE]
2692 || !cda[CTA_EXPECT_MASK]
2693 || !cda[CTA_EXPECT_MASTER])
2696 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2700 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2704 spin_lock_bh(&nf_conntrack_lock);
2705 exp = __nf_ct_expect_find(net, zone, &tuple);
2708 spin_unlock_bh(&nf_conntrack_lock);
2710 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2711 err = ctnetlink_create_expect(net, zone, cda,
2713 NETLINK_CB(skb).portid,
2720 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2721 err = ctnetlink_change_expect(exp, cda);
2722 spin_unlock_bh(&nf_conntrack_lock);
2728 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
2729 const struct ip_conntrack_stat *st)
2731 struct nlmsghdr *nlh;
2732 struct nfgenmsg *nfmsg;
2733 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2735 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
2736 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2740 nfmsg = nlmsg_data(nlh);
2741 nfmsg->nfgen_family = AF_UNSPEC;
2742 nfmsg->version = NFNETLINK_V0;
2743 nfmsg->res_id = htons(cpu);
2745 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
2746 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
2747 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
2748 goto nla_put_failure;
2750 nlmsg_end(skb, nlh);
2755 nlmsg_cancel(skb, nlh);
2760 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2763 struct net *net = sock_net(skb->sk);
2765 if (cb->args[0] == nr_cpu_ids)
2768 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2769 const struct ip_conntrack_stat *st;
2771 if (!cpu_possible(cpu))
2774 st = per_cpu_ptr(net->ct.stat, cpu);
2775 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
2786 ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
2787 const struct nlmsghdr *nlh,
2788 const struct nlattr * const cda[])
2790 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2791 struct netlink_dump_control c = {
2792 .dump = ctnetlink_exp_stat_cpu_dump,
2794 return netlink_dump_start(ctnl, skb, nlh, &c);
2800 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2801 static struct nf_ct_event_notifier ctnl_notifier = {
2802 .fcn = ctnetlink_conntrack_event,
2805 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2806 .fcn = ctnetlink_expect_event,
2810 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2811 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
2812 .attr_count = CTA_MAX,
2813 .policy = ct_nla_policy },
2814 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
2815 .attr_count = CTA_MAX,
2816 .policy = ct_nla_policy },
2817 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
2818 .attr_count = CTA_MAX,
2819 .policy = ct_nla_policy },
2820 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
2821 .attr_count = CTA_MAX,
2822 .policy = ct_nla_policy },
2823 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
2824 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
2825 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
2826 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
2829 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2830 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
2831 .attr_count = CTA_EXPECT_MAX,
2832 .policy = exp_nla_policy },
2833 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
2834 .attr_count = CTA_EXPECT_MAX,
2835 .policy = exp_nla_policy },
2836 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
2837 .attr_count = CTA_EXPECT_MAX,
2838 .policy = exp_nla_policy },
2839 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
2842 static const struct nfnetlink_subsystem ctnl_subsys = {
2843 .name = "conntrack",
2844 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2845 .cb_count = IPCTNL_MSG_MAX,
2849 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2850 .name = "conntrack_expect",
2851 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2852 .cb_count = IPCTNL_MSG_EXP_MAX,
2856 MODULE_ALIAS("ip_conntrack_netlink");
2857 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2858 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2860 static int __net_init ctnetlink_net_init(struct net *net)
2862 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2865 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
2867 pr_err("ctnetlink_init: cannot register notifier.\n");
2871 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
2873 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2874 goto err_unreg_notifier;
2879 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2881 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2887 static void ctnetlink_net_exit(struct net *net)
2889 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2890 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
2891 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
2895 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
2899 list_for_each_entry(net, net_exit_list, exit_list)
2900 ctnetlink_net_exit(net);
2903 static struct pernet_operations ctnetlink_net_ops = {
2904 .init = ctnetlink_net_init,
2905 .exit_batch = ctnetlink_net_exit_batch,
2908 static int __init ctnetlink_init(void)
2912 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2913 ret = nfnetlink_subsys_register(&ctnl_subsys);
2915 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2919 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2921 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2922 goto err_unreg_subsys;
2925 ret = register_pernet_subsys(&ctnetlink_net_ops);
2927 pr_err("ctnetlink_init: cannot register pernet operations\n");
2928 goto err_unreg_exp_subsys;
2930 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2931 /* setup interaction between nf_queue and nf_conntrack_netlink. */
2932 RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
2936 err_unreg_exp_subsys:
2937 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2939 nfnetlink_subsys_unregister(&ctnl_subsys);
2944 static void __exit ctnetlink_exit(void)
2946 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2948 unregister_pernet_subsys(&ctnetlink_net_ops);
2949 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2950 nfnetlink_subsys_unregister(&ctnl_subsys);
2951 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2952 RCU_INIT_POINTER(nfq_ct_hook, NULL);
2956 module_init(ctnetlink_init);
2957 module_exit(ctnetlink_exit);