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 #include <net/netfilter/nf_conntrack_labels.h>
47 #ifdef CONFIG_NF_NAT_NEEDED
48 #include <net/netfilter/nf_nat_core.h>
49 #include <net/netfilter/nf_nat_l4proto.h>
50 #include <net/netfilter/nf_nat_helper.h>
53 #include <linux/netfilter/nfnetlink.h>
54 #include <linux/netfilter/nfnetlink_conntrack.h>
56 MODULE_LICENSE("GPL");
58 static char __initdata version[] = "0.93";
61 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
62 const struct nf_conntrack_tuple *tuple,
63 struct nf_conntrack_l4proto *l4proto)
66 struct nlattr *nest_parms;
68 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
71 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
74 if (likely(l4proto->tuple_to_nlattr))
75 ret = l4proto->tuple_to_nlattr(skb, tuple);
77 nla_nest_end(skb, nest_parms);
86 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
87 const struct nf_conntrack_tuple *tuple,
88 struct nf_conntrack_l3proto *l3proto)
91 struct nlattr *nest_parms;
93 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
97 if (likely(l3proto->tuple_to_nlattr))
98 ret = l3proto->tuple_to_nlattr(skb, tuple);
100 nla_nest_end(skb, nest_parms);
109 ctnetlink_dump_tuples(struct sk_buff *skb,
110 const struct nf_conntrack_tuple *tuple)
113 struct nf_conntrack_l3proto *l3proto;
114 struct nf_conntrack_l4proto *l4proto;
117 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
118 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
121 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
122 tuple->dst.protonum);
123 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
130 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
132 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
133 goto nla_put_failure;
141 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
143 long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
148 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
149 goto nla_put_failure;
157 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
159 struct nf_conntrack_l4proto *l4proto;
160 struct nlattr *nest_proto;
163 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
164 if (!l4proto->to_nlattr)
167 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
169 goto nla_put_failure;
171 ret = l4proto->to_nlattr(skb, nest_proto, ct);
173 nla_nest_end(skb, nest_proto);
182 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
184 struct nlattr *nest_helper;
185 const struct nf_conn_help *help = nfct_help(ct);
186 struct nf_conntrack_helper *helper;
191 helper = rcu_dereference(help->helper);
195 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
197 goto nla_put_failure;
198 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
199 goto nla_put_failure;
201 if (helper->to_nlattr)
202 helper->to_nlattr(skb, ct);
204 nla_nest_end(skb, nest_helper);
213 dump_counters(struct sk_buff *skb, u64 pkts, u64 bytes,
214 enum ip_conntrack_dir dir)
216 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
217 struct nlattr *nest_count;
219 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
221 goto nla_put_failure;
223 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) ||
224 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes)))
225 goto nla_put_failure;
227 nla_nest_end(skb, nest_count);
236 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
237 enum ip_conntrack_dir dir, int type)
239 struct nf_conn_counter *acct;
242 acct = nf_conn_acct_find(ct);
246 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
247 pkts = atomic64_xchg(&acct[dir].packets, 0);
248 bytes = atomic64_xchg(&acct[dir].bytes, 0);
250 pkts = atomic64_read(&acct[dir].packets);
251 bytes = atomic64_read(&acct[dir].bytes);
253 return dump_counters(skb, pkts, bytes, dir);
257 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
259 struct nlattr *nest_count;
260 const struct nf_conn_tstamp *tstamp;
262 tstamp = nf_conn_tstamp_find(ct);
266 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
268 goto nla_put_failure;
270 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) ||
271 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
272 cpu_to_be64(tstamp->stop))))
273 goto nla_put_failure;
274 nla_nest_end(skb, nest_count);
282 #ifdef CONFIG_NF_CONNTRACK_MARK
284 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
286 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
287 goto nla_put_failure;
294 #define ctnetlink_dump_mark(a, b) (0)
297 #ifdef CONFIG_NF_CONNTRACK_SECMARK
299 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
301 struct nlattr *nest_secctx;
305 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
310 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
312 goto nla_put_failure;
314 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
315 goto nla_put_failure;
316 nla_nest_end(skb, nest_secctx);
320 security_release_secctx(secctx, len);
324 #define ctnetlink_dump_secctx(a, b) (0)
327 #ifdef CONFIG_NF_CONNTRACK_LABELS
328 static int ctnetlink_label_size(const struct nf_conn *ct)
330 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
334 return nla_total_size(labels->words * sizeof(long));
338 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
340 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
346 len = labels->words * sizeof(long);
349 if (labels->bits[i] != 0)
350 return nla_put(skb, CTA_LABELS, len, labels->bits);
352 } while (i < labels->words);
357 #define ctnetlink_dump_labels(a, b) (0)
358 #define ctnetlink_label_size(a) (0)
361 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
364 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
366 struct nlattr *nest_parms;
368 if (!(ct->status & IPS_EXPECTED))
371 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
373 goto nla_put_failure;
374 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
375 goto nla_put_failure;
376 nla_nest_end(skb, nest_parms);
384 #ifdef CONFIG_NF_NAT_NEEDED
386 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
388 struct nlattr *nest_parms;
390 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
392 goto nla_put_failure;
394 if (nla_put_be32(skb, CTA_NAT_SEQ_CORRECTION_POS,
395 htonl(natseq->correction_pos)) ||
396 nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
397 htonl(natseq->offset_before)) ||
398 nla_put_be32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
399 htonl(natseq->offset_after)))
400 goto nla_put_failure;
402 nla_nest_end(skb, nest_parms);
411 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
413 struct nf_nat_seq *natseq;
414 struct nf_conn_nat *nat = nfct_nat(ct);
416 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
419 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
420 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
423 natseq = &nat->seq[IP_CT_DIR_REPLY];
424 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
430 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
434 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
436 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
437 goto nla_put_failure;
445 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
447 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
448 goto nla_put_failure;
456 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
459 struct nlmsghdr *nlh;
460 struct nfgenmsg *nfmsg;
461 struct nlattr *nest_parms;
462 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
464 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
465 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
469 nfmsg = nlmsg_data(nlh);
470 nfmsg->nfgen_family = nf_ct_l3num(ct);
471 nfmsg->version = NFNETLINK_V0;
474 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
476 goto nla_put_failure;
477 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
478 goto nla_put_failure;
479 nla_nest_end(skb, nest_parms);
481 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
483 goto nla_put_failure;
484 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
485 goto nla_put_failure;
486 nla_nest_end(skb, nest_parms);
488 if (nf_ct_zone(ct) &&
489 nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
490 goto nla_put_failure;
492 if (ctnetlink_dump_status(skb, ct) < 0 ||
493 ctnetlink_dump_timeout(skb, ct) < 0 ||
494 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL, type) < 0 ||
495 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY, type) < 0 ||
496 ctnetlink_dump_timestamp(skb, ct) < 0 ||
497 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
498 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
499 ctnetlink_dump_mark(skb, ct) < 0 ||
500 ctnetlink_dump_secctx(skb, ct) < 0 ||
501 ctnetlink_dump_labels(skb, ct) < 0 ||
502 ctnetlink_dump_id(skb, ct) < 0 ||
503 ctnetlink_dump_use(skb, ct) < 0 ||
504 ctnetlink_dump_master(skb, ct) < 0 ||
505 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
506 goto nla_put_failure;
513 nlmsg_cancel(skb, nlh);
518 ctnetlink_proto_size(const struct nf_conn *ct)
520 struct nf_conntrack_l3proto *l3proto;
521 struct nf_conntrack_l4proto *l4proto;
525 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
526 len += l3proto->nla_size;
528 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
529 len += l4proto->nla_size;
536 ctnetlink_counters_size(const struct nf_conn *ct)
538 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
540 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
541 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
542 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
547 ctnetlink_secctx_size(const struct nf_conn *ct)
549 #ifdef CONFIG_NF_CONNTRACK_SECMARK
552 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
556 return nla_total_size(0) /* CTA_SECCTX */
557 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
564 ctnetlink_timestamp_size(const struct nf_conn *ct)
566 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
567 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
569 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
576 ctnetlink_nlmsg_size(const struct nf_conn *ct)
578 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
579 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
580 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
581 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
582 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
583 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
584 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
585 + ctnetlink_counters_size(ct)
586 + ctnetlink_timestamp_size(ct)
587 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
588 + nla_total_size(0) /* CTA_PROTOINFO */
589 + nla_total_size(0) /* CTA_HELP */
590 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
591 + ctnetlink_secctx_size(ct)
592 #ifdef CONFIG_NF_NAT_NEEDED
593 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
594 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
596 #ifdef CONFIG_NF_CONNTRACK_MARK
597 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
599 + ctnetlink_proto_size(ct)
600 + ctnetlink_label_size(ct)
604 #ifdef CONFIG_NF_CONNTRACK_EVENTS
606 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
609 struct nlmsghdr *nlh;
610 struct nfgenmsg *nfmsg;
611 struct nlattr *nest_parms;
612 struct nf_conn *ct = item->ct;
615 unsigned int flags = 0, group;
618 /* ignore our fake conntrack entry */
619 if (nf_ct_is_untracked(ct))
622 if (events & (1 << IPCT_DESTROY)) {
623 type = IPCTNL_MSG_CT_DELETE;
624 group = NFNLGRP_CONNTRACK_DESTROY;
625 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
626 type = IPCTNL_MSG_CT_NEW;
627 flags = NLM_F_CREATE|NLM_F_EXCL;
628 group = NFNLGRP_CONNTRACK_NEW;
630 type = IPCTNL_MSG_CT_NEW;
631 group = NFNLGRP_CONNTRACK_UPDATE;
636 if (!item->report && !nfnetlink_has_listeners(net, group))
639 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
643 type |= NFNL_SUBSYS_CTNETLINK << 8;
644 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
648 nfmsg = nlmsg_data(nlh);
649 nfmsg->nfgen_family = nf_ct_l3num(ct);
650 nfmsg->version = NFNETLINK_V0;
654 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
656 goto nla_put_failure;
657 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
658 goto nla_put_failure;
659 nla_nest_end(skb, nest_parms);
661 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
663 goto nla_put_failure;
664 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
665 goto nla_put_failure;
666 nla_nest_end(skb, nest_parms);
668 if (nf_ct_zone(ct) &&
669 nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
670 goto nla_put_failure;
672 if (ctnetlink_dump_id(skb, ct) < 0)
673 goto nla_put_failure;
675 if (ctnetlink_dump_status(skb, ct) < 0)
676 goto nla_put_failure;
678 if (events & (1 << IPCT_DESTROY)) {
679 if (ctnetlink_dump_counters(skb, ct,
680 IP_CT_DIR_ORIGINAL, type) < 0 ||
681 ctnetlink_dump_counters(skb, ct,
682 IP_CT_DIR_REPLY, type) < 0 ||
683 ctnetlink_dump_timestamp(skb, ct) < 0)
684 goto nla_put_failure;
686 if (ctnetlink_dump_timeout(skb, ct) < 0)
687 goto nla_put_failure;
689 if (events & (1 << IPCT_PROTOINFO)
690 && ctnetlink_dump_protoinfo(skb, ct) < 0)
691 goto nla_put_failure;
693 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
694 && ctnetlink_dump_helpinfo(skb, ct) < 0)
695 goto nla_put_failure;
697 #ifdef CONFIG_NF_CONNTRACK_SECMARK
698 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
699 && ctnetlink_dump_secctx(skb, ct) < 0)
700 goto nla_put_failure;
702 if (events & (1 << IPCT_LABEL) &&
703 ctnetlink_dump_labels(skb, ct) < 0)
704 goto nla_put_failure;
706 if (events & (1 << IPCT_RELATED) &&
707 ctnetlink_dump_master(skb, ct) < 0)
708 goto nla_put_failure;
710 if (events & (1 << IPCT_NATSEQADJ) &&
711 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
712 goto nla_put_failure;
715 #ifdef CONFIG_NF_CONNTRACK_MARK
716 if ((events & (1 << IPCT_MARK) || ct->mark)
717 && ctnetlink_dump_mark(skb, ct) < 0)
718 goto nla_put_failure;
723 err = nfnetlink_send(skb, net, item->portid, group, item->report,
725 if (err == -ENOBUFS || err == -EAGAIN)
732 nlmsg_cancel(skb, nlh);
736 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
741 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
743 static int ctnetlink_done(struct netlink_callback *cb)
746 nf_ct_put((struct nf_conn *)cb->args[1]);
752 struct ctnetlink_dump_filter {
760 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
762 struct net *net = sock_net(skb->sk);
763 struct nf_conn *ct, *last;
764 struct nf_conntrack_tuple_hash *h;
765 struct hlist_nulls_node *n;
766 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
767 u_int8_t l3proto = nfmsg->nfgen_family;
769 #ifdef CONFIG_NF_CONNTRACK_MARK
770 const struct ctnetlink_dump_filter *filter = cb->data;
773 spin_lock_bh(&nf_conntrack_lock);
774 last = (struct nf_conn *)cb->args[1];
775 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
777 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
779 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
781 ct = nf_ct_tuplehash_to_ctrack(h);
782 /* Dump entries of a given L3 protocol number.
783 * If it is not specified, ie. l3proto == 0,
784 * then dump everything. */
785 if (l3proto && nf_ct_l3num(ct) != l3proto)
792 #ifdef CONFIG_NF_CONNTRACK_MARK
793 if (filter && !((ct->mark & filter->mark.mask) ==
800 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
802 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
806 nf_conntrack_get(&ct->ct_general);
807 cb->args[1] = (unsigned long)ct;
817 spin_unlock_bh(&nf_conntrack_lock);
825 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
827 struct nlattr *tb[CTA_IP_MAX+1];
828 struct nf_conntrack_l3proto *l3proto;
831 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
836 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
838 if (likely(l3proto->nlattr_to_tuple)) {
839 ret = nla_validate_nested(attr, CTA_IP_MAX,
840 l3proto->nla_policy);
842 ret = l3proto->nlattr_to_tuple(tb, tuple);
850 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
851 [CTA_PROTO_NUM] = { .type = NLA_U8 },
855 ctnetlink_parse_tuple_proto(struct nlattr *attr,
856 struct nf_conntrack_tuple *tuple)
858 struct nlattr *tb[CTA_PROTO_MAX+1];
859 struct nf_conntrack_l4proto *l4proto;
862 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
866 if (!tb[CTA_PROTO_NUM])
868 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
871 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
873 if (likely(l4proto->nlattr_to_tuple)) {
874 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
875 l4proto->nla_policy);
877 ret = l4proto->nlattr_to_tuple(tb, tuple);
885 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
886 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
887 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
891 ctnetlink_parse_tuple(const struct nlattr * const cda[],
892 struct nf_conntrack_tuple *tuple,
893 enum ctattr_type type, u_int8_t l3num)
895 struct nlattr *tb[CTA_TUPLE_MAX+1];
898 memset(tuple, 0, sizeof(*tuple));
900 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
904 if (!tb[CTA_TUPLE_IP])
907 tuple->src.l3num = l3num;
909 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
913 if (!tb[CTA_TUPLE_PROTO])
916 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
920 /* orig and expect tuples get DIR_ORIGINAL */
921 if (type == CTA_TUPLE_REPLY)
922 tuple->dst.dir = IP_CT_DIR_REPLY;
924 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
930 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
933 #ifdef CONFIG_NF_CONNTRACK_ZONES
934 *zone = ntohs(nla_get_be16(attr));
944 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
945 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
946 .len = NF_CT_HELPER_NAME_LEN - 1 },
950 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
951 struct nlattr **helpinfo)
954 struct nlattr *tb[CTA_HELP_MAX+1];
956 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
960 if (!tb[CTA_HELP_NAME])
963 *helper_name = nla_data(tb[CTA_HELP_NAME]);
965 if (tb[CTA_HELP_INFO])
966 *helpinfo = tb[CTA_HELP_INFO];
971 #define __CTA_LABELS_MAX_LENGTH ((XT_CONNLABEL_MAXBIT + 1) / BITS_PER_BYTE)
972 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
973 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
974 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
975 [CTA_STATUS] = { .type = NLA_U32 },
976 [CTA_PROTOINFO] = { .type = NLA_NESTED },
977 [CTA_HELP] = { .type = NLA_NESTED },
978 [CTA_NAT_SRC] = { .type = NLA_NESTED },
979 [CTA_TIMEOUT] = { .type = NLA_U32 },
980 [CTA_MARK] = { .type = NLA_U32 },
981 [CTA_ID] = { .type = NLA_U32 },
982 [CTA_NAT_DST] = { .type = NLA_NESTED },
983 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
984 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
985 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
986 [CTA_ZONE] = { .type = NLA_U16 },
987 [CTA_MARK_MASK] = { .type = NLA_U32 },
988 [CTA_LABELS] = { .type = NLA_BINARY,
989 .len = __CTA_LABELS_MAX_LENGTH },
990 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
991 .len = __CTA_LABELS_MAX_LENGTH },
995 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
996 const struct nlmsghdr *nlh,
997 const struct nlattr * const cda[])
999 struct net *net = sock_net(ctnl);
1000 struct nf_conntrack_tuple_hash *h;
1001 struct nf_conntrack_tuple tuple;
1003 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1004 u_int8_t u3 = nfmsg->nfgen_family;
1008 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1012 if (cda[CTA_TUPLE_ORIG])
1013 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1014 else if (cda[CTA_TUPLE_REPLY])
1015 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1017 /* Flush the whole table */
1018 nf_conntrack_flush_report(net,
1019 NETLINK_CB(skb).portid,
1027 h = nf_conntrack_find_get(net, zone, &tuple);
1031 ct = nf_ct_tuplehash_to_ctrack(h);
1034 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1035 if (id != (u32)(unsigned long)ct) {
1041 if (del_timer(&ct->timeout)) {
1042 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
1043 NETLINK_CB(skb).portid,
1044 nlmsg_report(nlh)) < 0) {
1045 nf_ct_delete_from_lists(ct);
1046 /* we failed to report the event, try later */
1047 nf_ct_dying_timeout(ct);
1051 /* death_by_timeout would report the event again */
1052 set_bit(IPS_DYING_BIT, &ct->status);
1053 nf_ct_delete_from_lists(ct);
1062 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
1063 const struct nlmsghdr *nlh,
1064 const struct nlattr * const cda[])
1066 struct net *net = sock_net(ctnl);
1067 struct nf_conntrack_tuple_hash *h;
1068 struct nf_conntrack_tuple tuple;
1070 struct sk_buff *skb2 = NULL;
1071 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1072 u_int8_t u3 = nfmsg->nfgen_family;
1076 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1077 struct netlink_dump_control c = {
1078 .dump = ctnetlink_dump_table,
1079 .done = ctnetlink_done,
1081 #ifdef CONFIG_NF_CONNTRACK_MARK
1082 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1083 struct ctnetlink_dump_filter *filter;
1085 filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1090 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1092 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1096 return netlink_dump_start(ctnl, skb, nlh, &c);
1099 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1103 if (cda[CTA_TUPLE_ORIG])
1104 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1105 else if (cda[CTA_TUPLE_REPLY])
1106 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1113 h = nf_conntrack_find_get(net, zone, &tuple);
1117 ct = nf_ct_tuplehash_to_ctrack(h);
1120 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1127 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1128 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1134 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1143 /* this avoids a loop in nfnetlink. */
1144 return err == -EAGAIN ? -ENOBUFS : err;
1147 static int ctnetlink_done_list(struct netlink_callback *cb)
1150 nf_ct_put((struct nf_conn *)cb->args[1]);
1155 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb,
1156 struct hlist_nulls_head *list)
1158 struct nf_conn *ct, *last;
1159 struct nf_conntrack_tuple_hash *h;
1160 struct hlist_nulls_node *n;
1161 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1162 u_int8_t l3proto = nfmsg->nfgen_family;
1168 spin_lock_bh(&nf_conntrack_lock);
1169 last = (struct nf_conn *)cb->args[1];
1171 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1172 ct = nf_ct_tuplehash_to_ctrack(h);
1173 if (l3proto && nf_ct_l3num(ct) != l3proto)
1181 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1183 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1187 nf_conntrack_get(&ct->ct_general);
1188 cb->args[1] = (unsigned long)ct;
1198 spin_unlock_bh(&nf_conntrack_lock);
1206 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1208 struct net *net = sock_net(skb->sk);
1210 return ctnetlink_dump_list(skb, cb, &net->ct.dying);
1214 ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb,
1215 const struct nlmsghdr *nlh,
1216 const struct nlattr * const cda[])
1218 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1219 struct netlink_dump_control c = {
1220 .dump = ctnetlink_dump_dying,
1221 .done = ctnetlink_done_list,
1223 return netlink_dump_start(ctnl, skb, nlh, &c);
1230 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1232 struct net *net = sock_net(skb->sk);
1234 return ctnetlink_dump_list(skb, cb, &net->ct.unconfirmed);
1238 ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb,
1239 const struct nlmsghdr *nlh,
1240 const struct nlattr * const cda[])
1242 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1243 struct netlink_dump_control c = {
1244 .dump = ctnetlink_dump_unconfirmed,
1245 .done = ctnetlink_done_list,
1247 return netlink_dump_start(ctnl, skb, nlh, &c);
1253 #ifdef CONFIG_NF_NAT_NEEDED
1255 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1256 enum nf_nat_manip_type manip,
1257 const struct nlattr *attr)
1259 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1262 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1263 if (!parse_nat_setup) {
1264 #ifdef CONFIG_MODULES
1266 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1267 if (request_module("nf-nat") < 0) {
1268 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1272 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1274 if (nfnetlink_parse_nat_setup_hook)
1280 err = parse_nat_setup(ct, manip, attr);
1281 if (err == -EAGAIN) {
1282 #ifdef CONFIG_MODULES
1284 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1285 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1286 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1290 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1301 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1304 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1305 d = ct->status ^ status;
1307 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1311 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1312 /* SEEN_REPLY bit can only be set */
1315 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1316 /* ASSURED bit can only be set */
1319 /* Be careful here, modifying NAT bits can screw up things,
1320 * so don't let users modify them directly if they don't pass
1322 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1327 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1329 #ifdef CONFIG_NF_NAT_NEEDED
1332 if (cda[CTA_NAT_DST]) {
1333 ret = ctnetlink_parse_nat_setup(ct,
1339 if (cda[CTA_NAT_SRC]) {
1340 ret = ctnetlink_parse_nat_setup(ct,
1353 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1355 struct nf_conntrack_helper *helper;
1356 struct nf_conn_help *help = nfct_help(ct);
1357 char *helpname = NULL;
1358 struct nlattr *helpinfo = NULL;
1361 /* don't change helper of sibling connections */
1365 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1369 if (!strcmp(helpname, "")) {
1370 if (help && help->helper) {
1371 /* we had a helper before ... */
1372 nf_ct_remove_expectations(ct);
1373 RCU_INIT_POINTER(help->helper, NULL);
1379 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1380 nf_ct_protonum(ct));
1381 if (helper == NULL) {
1382 #ifdef CONFIG_MODULES
1383 spin_unlock_bh(&nf_conntrack_lock);
1385 if (request_module("nfct-helper-%s", helpname) < 0) {
1386 spin_lock_bh(&nf_conntrack_lock);
1390 spin_lock_bh(&nf_conntrack_lock);
1391 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1392 nf_ct_protonum(ct));
1400 if (help->helper == helper) {
1401 /* update private helper data if allowed. */
1402 if (helper->from_nlattr)
1403 helper->from_nlattr(helpinfo, ct);
1409 /* we cannot set a helper for an existing conntrack */
1414 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1416 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1418 if (!del_timer(&ct->timeout))
1421 ct->timeout.expires = jiffies + timeout * HZ;
1422 add_timer(&ct->timeout);
1427 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1428 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1429 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1430 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1434 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1436 const struct nlattr *attr = cda[CTA_PROTOINFO];
1437 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1438 struct nf_conntrack_l4proto *l4proto;
1441 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1446 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1447 if (l4proto->from_nlattr)
1448 err = l4proto->from_nlattr(tb, ct);
1454 #ifdef CONFIG_NF_NAT_NEEDED
1455 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1456 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1457 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1458 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1462 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1465 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1467 err = nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1471 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1474 natseq->correction_pos =
1475 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1477 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1480 natseq->offset_before =
1481 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1483 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1486 natseq->offset_after =
1487 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1493 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1494 const struct nlattr * const cda[])
1497 struct nf_conn_nat *nat = nfct_nat(ct);
1502 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1503 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1504 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1508 ct->status |= IPS_SEQ_ADJUST;
1511 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1512 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1513 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1517 ct->status |= IPS_SEQ_ADJUST;
1525 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1527 #ifdef CONFIG_NF_CONNTRACK_LABELS
1528 size_t len = nla_len(cda[CTA_LABELS]);
1529 const void *mask = cda[CTA_LABELS_MASK];
1531 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1535 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1536 nla_len(cda[CTA_LABELS_MASK]) != len)
1538 mask = nla_data(cda[CTA_LABELS_MASK]);
1543 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1550 ctnetlink_change_conntrack(struct nf_conn *ct,
1551 const struct nlattr * const cda[])
1555 /* only allow NAT changes and master assignation for new conntracks */
1556 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1559 if (cda[CTA_HELP]) {
1560 err = ctnetlink_change_helper(ct, cda);
1565 if (cda[CTA_TIMEOUT]) {
1566 err = ctnetlink_change_timeout(ct, cda);
1571 if (cda[CTA_STATUS]) {
1572 err = ctnetlink_change_status(ct, cda);
1577 if (cda[CTA_PROTOINFO]) {
1578 err = ctnetlink_change_protoinfo(ct, cda);
1583 #if defined(CONFIG_NF_CONNTRACK_MARK)
1585 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1588 #ifdef CONFIG_NF_NAT_NEEDED
1589 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1590 err = ctnetlink_change_nat_seq_adj(ct, cda);
1595 if (cda[CTA_LABELS]) {
1596 err = ctnetlink_attach_labels(ct, cda);
1604 static struct nf_conn *
1605 ctnetlink_create_conntrack(struct net *net, u16 zone,
1606 const struct nlattr * const cda[],
1607 struct nf_conntrack_tuple *otuple,
1608 struct nf_conntrack_tuple *rtuple,
1613 struct nf_conntrack_helper *helper;
1614 struct nf_conn_tstamp *tstamp;
1616 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1618 return ERR_PTR(-ENOMEM);
1620 if (!cda[CTA_TIMEOUT])
1622 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1624 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1627 if (cda[CTA_HELP]) {
1628 char *helpname = NULL;
1629 struct nlattr *helpinfo = NULL;
1631 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1635 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1636 nf_ct_protonum(ct));
1637 if (helper == NULL) {
1639 #ifdef CONFIG_MODULES
1640 if (request_module("nfct-helper-%s", helpname) < 0) {
1646 helper = __nf_conntrack_helper_find(helpname,
1648 nf_ct_protonum(ct));
1658 struct nf_conn_help *help;
1660 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1665 /* set private helper data if allowed. */
1666 if (helper->from_nlattr)
1667 helper->from_nlattr(helpinfo, ct);
1669 /* not in hash table yet so not strictly necessary */
1670 RCU_INIT_POINTER(help->helper, helper);
1673 /* try an implicit helper assignation */
1674 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1679 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1680 err = ctnetlink_change_nat(ct, cda);
1685 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1686 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1687 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1688 nf_ct_labels_ext_add(ct);
1690 /* we must add conntrack extensions before confirmation. */
1691 ct->status |= IPS_CONFIRMED;
1693 if (cda[CTA_STATUS]) {
1694 err = ctnetlink_change_status(ct, cda);
1699 #ifdef CONFIG_NF_NAT_NEEDED
1700 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1701 err = ctnetlink_change_nat_seq_adj(ct, cda);
1707 memset(&ct->proto, 0, sizeof(ct->proto));
1708 if (cda[CTA_PROTOINFO]) {
1709 err = ctnetlink_change_protoinfo(ct, cda);
1714 #if defined(CONFIG_NF_CONNTRACK_MARK)
1716 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1719 /* setup master conntrack: this is a confirmed expectation */
1720 if (cda[CTA_TUPLE_MASTER]) {
1721 struct nf_conntrack_tuple master;
1722 struct nf_conntrack_tuple_hash *master_h;
1723 struct nf_conn *master_ct;
1725 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1729 master_h = nf_conntrack_find_get(net, zone, &master);
1730 if (master_h == NULL) {
1734 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1735 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1736 ct->master = master_ct;
1738 tstamp = nf_conn_tstamp_find(ct);
1740 tstamp->start = ktime_to_ns(ktime_get_real());
1742 err = nf_conntrack_hash_check_insert(ct);
1753 nf_conntrack_free(ct);
1754 return ERR_PTR(err);
1758 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1759 const struct nlmsghdr *nlh,
1760 const struct nlattr * const cda[])
1762 struct net *net = sock_net(ctnl);
1763 struct nf_conntrack_tuple otuple, rtuple;
1764 struct nf_conntrack_tuple_hash *h = NULL;
1765 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1767 u_int8_t u3 = nfmsg->nfgen_family;
1771 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1775 if (cda[CTA_TUPLE_ORIG]) {
1776 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1781 if (cda[CTA_TUPLE_REPLY]) {
1782 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1787 if (cda[CTA_TUPLE_ORIG])
1788 h = nf_conntrack_find_get(net, zone, &otuple);
1789 else if (cda[CTA_TUPLE_REPLY])
1790 h = nf_conntrack_find_get(net, zone, &rtuple);
1794 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1795 enum ip_conntrack_events events;
1797 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1800 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1806 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1807 events = IPCT_RELATED;
1811 if (cda[CTA_LABELS] &&
1812 ctnetlink_attach_labels(ct, cda) == 0)
1813 events |= (1 << IPCT_LABEL);
1815 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1816 (1 << IPCT_ASSURED) |
1817 (1 << IPCT_HELPER) |
1818 (1 << IPCT_PROTOINFO) |
1819 (1 << IPCT_NATSEQADJ) |
1820 (1 << IPCT_MARK) | events,
1821 ct, NETLINK_CB(skb).portid,
1828 /* implicit 'else' */
1831 ct = nf_ct_tuplehash_to_ctrack(h);
1832 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1833 spin_lock_bh(&nf_conntrack_lock);
1834 err = ctnetlink_change_conntrack(ct, cda);
1835 spin_unlock_bh(&nf_conntrack_lock);
1837 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1838 (1 << IPCT_ASSURED) |
1839 (1 << IPCT_HELPER) |
1840 (1 << IPCT_PROTOINFO) |
1841 (1 << IPCT_NATSEQADJ) |
1843 ct, NETLINK_CB(skb).portid,
1853 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1854 __u16 cpu, const struct ip_conntrack_stat *st)
1856 struct nlmsghdr *nlh;
1857 struct nfgenmsg *nfmsg;
1858 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1860 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1861 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1865 nfmsg = nlmsg_data(nlh);
1866 nfmsg->nfgen_family = AF_UNSPEC;
1867 nfmsg->version = NFNETLINK_V0;
1868 nfmsg->res_id = htons(cpu);
1870 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1871 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1872 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1873 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1874 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1875 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1876 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1877 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1878 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1879 htonl(st->insert_failed)) ||
1880 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1881 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1882 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1883 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1884 htonl(st->search_restart)))
1885 goto nla_put_failure;
1887 nlmsg_end(skb, nlh);
1892 nlmsg_cancel(skb, nlh);
1897 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1900 struct net *net = sock_net(skb->sk);
1902 if (cb->args[0] == nr_cpu_ids)
1905 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1906 const struct ip_conntrack_stat *st;
1908 if (!cpu_possible(cpu))
1911 st = per_cpu_ptr(net->ct.stat, cpu);
1912 if (ctnetlink_ct_stat_cpu_fill_info(skb,
1913 NETLINK_CB(cb->skb).portid,
1924 ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1925 const struct nlmsghdr *nlh,
1926 const struct nlattr * const cda[])
1928 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1929 struct netlink_dump_control c = {
1930 .dump = ctnetlink_ct_stat_cpu_dump,
1932 return netlink_dump_start(ctnl, skb, nlh, &c);
1939 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
1942 struct nlmsghdr *nlh;
1943 struct nfgenmsg *nfmsg;
1944 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1945 unsigned int nr_conntracks = atomic_read(&net->ct.count);
1947 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
1948 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1952 nfmsg = nlmsg_data(nlh);
1953 nfmsg->nfgen_family = AF_UNSPEC;
1954 nfmsg->version = NFNETLINK_V0;
1957 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1958 goto nla_put_failure;
1960 nlmsg_end(skb, nlh);
1965 nlmsg_cancel(skb, nlh);
1970 ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1971 const struct nlmsghdr *nlh,
1972 const struct nlattr * const cda[])
1974 struct sk_buff *skb2;
1977 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1981 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
1983 NFNL_MSG_TYPE(nlh->nlmsg_type),
1988 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1997 /* this avoids a loop in nfnetlink. */
1998 return err == -EAGAIN ? -ENOBUFS : err;
2001 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
2003 ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
2005 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2006 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2007 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2008 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2009 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2010 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2011 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2012 + nla_total_size(0) /* CTA_PROTOINFO */
2013 + nla_total_size(0) /* CTA_HELP */
2014 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2015 + ctnetlink_secctx_size(ct)
2016 #ifdef CONFIG_NF_NAT_NEEDED
2017 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2018 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2020 #ifdef CONFIG_NF_CONNTRACK_MARK
2021 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2023 + ctnetlink_proto_size(ct)
2028 ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
2030 struct nlattr *nest_parms;
2033 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2035 goto nla_put_failure;
2036 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2037 goto nla_put_failure;
2038 nla_nest_end(skb, nest_parms);
2040 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2042 goto nla_put_failure;
2043 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2044 goto nla_put_failure;
2045 nla_nest_end(skb, nest_parms);
2047 if (nf_ct_zone(ct)) {
2048 if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
2049 goto nla_put_failure;
2052 if (ctnetlink_dump_id(skb, ct) < 0)
2053 goto nla_put_failure;
2055 if (ctnetlink_dump_status(skb, ct) < 0)
2056 goto nla_put_failure;
2058 if (ctnetlink_dump_timeout(skb, ct) < 0)
2059 goto nla_put_failure;
2061 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2062 goto nla_put_failure;
2064 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2065 goto nla_put_failure;
2067 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2068 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2069 goto nla_put_failure;
2071 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2072 goto nla_put_failure;
2074 if ((ct->status & IPS_SEQ_ADJUST) &&
2075 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
2076 goto nla_put_failure;
2078 #ifdef CONFIG_NF_CONNTRACK_MARK
2079 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2080 goto nla_put_failure;
2082 if (ctnetlink_dump_labels(skb, ct) < 0)
2083 goto nla_put_failure;
2093 ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2097 if (cda[CTA_TIMEOUT]) {
2098 err = ctnetlink_change_timeout(ct, cda);
2102 if (cda[CTA_STATUS]) {
2103 err = ctnetlink_change_status(ct, cda);
2107 if (cda[CTA_HELP]) {
2108 err = ctnetlink_change_helper(ct, cda);
2112 if (cda[CTA_LABELS]) {
2113 err = ctnetlink_attach_labels(ct, cda);
2117 #if defined(CONFIG_NF_CONNTRACK_MARK)
2119 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2125 ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
2127 struct nlattr *cda[CTA_MAX+1];
2130 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2134 spin_lock_bh(&nf_conntrack_lock);
2135 ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
2136 spin_unlock_bh(&nf_conntrack_lock);
2141 static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
2142 .build_size = ctnetlink_nfqueue_build_size,
2143 .build = ctnetlink_nfqueue_build,
2144 .parse = ctnetlink_nfqueue_parse,
2146 #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
2148 /***********************************************************************
2150 ***********************************************************************/
2153 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2154 const struct nf_conntrack_tuple *tuple,
2155 enum ctattr_expect type)
2157 struct nlattr *nest_parms;
2159 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2161 goto nla_put_failure;
2162 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2163 goto nla_put_failure;
2164 nla_nest_end(skb, nest_parms);
2173 ctnetlink_exp_dump_mask(struct sk_buff *skb,
2174 const struct nf_conntrack_tuple *tuple,
2175 const struct nf_conntrack_tuple_mask *mask)
2178 struct nf_conntrack_l3proto *l3proto;
2179 struct nf_conntrack_l4proto *l4proto;
2180 struct nf_conntrack_tuple m;
2181 struct nlattr *nest_parms;
2183 memset(&m, 0xFF, sizeof(m));
2184 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2185 m.src.u.all = mask->src.u.all;
2186 m.dst.protonum = tuple->dst.protonum;
2188 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2190 goto nla_put_failure;
2193 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2194 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2196 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2197 tuple->dst.protonum);
2198 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2202 if (unlikely(ret < 0))
2203 goto nla_put_failure;
2205 nla_nest_end(skb, nest_parms);
2213 static const union nf_inet_addr any_addr;
2216 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2217 const struct nf_conntrack_expect *exp)
2219 struct nf_conn *master = exp->master;
2220 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2221 struct nf_conn_help *help;
2222 #ifdef CONFIG_NF_NAT_NEEDED
2223 struct nlattr *nest_parms;
2224 struct nf_conntrack_tuple nat_tuple = {};
2226 struct nf_ct_helper_expectfn *expfn;
2231 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2232 goto nla_put_failure;
2233 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2234 goto nla_put_failure;
2235 if (ctnetlink_exp_dump_tuple(skb,
2236 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2237 CTA_EXPECT_MASTER) < 0)
2238 goto nla_put_failure;
2240 #ifdef CONFIG_NF_NAT_NEEDED
2241 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2242 exp->saved_proto.all) {
2243 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2245 goto nla_put_failure;
2247 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2248 goto nla_put_failure;
2250 nat_tuple.src.l3num = nf_ct_l3num(master);
2251 nat_tuple.src.u3 = exp->saved_addr;
2252 nat_tuple.dst.protonum = nf_ct_protonum(master);
2253 nat_tuple.src.u = exp->saved_proto;
2255 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2256 CTA_EXPECT_NAT_TUPLE) < 0)
2257 goto nla_put_failure;
2258 nla_nest_end(skb, nest_parms);
2261 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2262 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2263 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2264 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2265 goto nla_put_failure;
2266 help = nfct_help(master);
2268 struct nf_conntrack_helper *helper;
2270 helper = rcu_dereference(help->helper);
2272 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2273 goto nla_put_failure;
2275 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2276 if (expfn != NULL &&
2277 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2278 goto nla_put_failure;
2287 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2288 int event, const struct nf_conntrack_expect *exp)
2290 struct nlmsghdr *nlh;
2291 struct nfgenmsg *nfmsg;
2292 unsigned int flags = portid ? NLM_F_MULTI : 0;
2294 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2295 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2299 nfmsg = nlmsg_data(nlh);
2300 nfmsg->nfgen_family = exp->tuple.src.l3num;
2301 nfmsg->version = NFNETLINK_V0;
2304 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2305 goto nla_put_failure;
2307 nlmsg_end(skb, nlh);
2312 nlmsg_cancel(skb, nlh);
2316 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2318 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2320 struct nf_conntrack_expect *exp = item->exp;
2321 struct net *net = nf_ct_exp_net(exp);
2322 struct nlmsghdr *nlh;
2323 struct nfgenmsg *nfmsg;
2324 struct sk_buff *skb;
2325 unsigned int type, group;
2328 if (events & (1 << IPEXP_DESTROY)) {
2329 type = IPCTNL_MSG_EXP_DELETE;
2330 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2331 } else if (events & (1 << IPEXP_NEW)) {
2332 type = IPCTNL_MSG_EXP_NEW;
2333 flags = NLM_F_CREATE|NLM_F_EXCL;
2334 group = NFNLGRP_CONNTRACK_EXP_NEW;
2338 if (!item->report && !nfnetlink_has_listeners(net, group))
2341 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2345 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2346 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2350 nfmsg = nlmsg_data(nlh);
2351 nfmsg->nfgen_family = exp->tuple.src.l3num;
2352 nfmsg->version = NFNETLINK_V0;
2356 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2357 goto nla_put_failure;
2360 nlmsg_end(skb, nlh);
2361 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2366 nlmsg_cancel(skb, nlh);
2370 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2374 static int ctnetlink_exp_done(struct netlink_callback *cb)
2377 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2382 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2384 struct net *net = sock_net(skb->sk);
2385 struct nf_conntrack_expect *exp, *last;
2386 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2387 u_int8_t l3proto = nfmsg->nfgen_family;
2390 last = (struct nf_conntrack_expect *)cb->args[1];
2391 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2393 hlist_for_each_entry(exp, &net->ct.expect_hash[cb->args[0]],
2395 if (l3proto && exp->tuple.src.l3num != l3proto)
2402 if (ctnetlink_exp_fill_info(skb,
2403 NETLINK_CB(cb->skb).portid,
2407 if (!atomic_inc_not_zero(&exp->use))
2409 cb->args[1] = (unsigned long)exp;
2421 nf_ct_expect_put(last);
2427 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2429 struct nf_conntrack_expect *exp, *last;
2430 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2431 struct nf_conn *ct = cb->data;
2432 struct nf_conn_help *help = nfct_help(ct);
2433 u_int8_t l3proto = nfmsg->nfgen_family;
2439 last = (struct nf_conntrack_expect *)cb->args[1];
2441 hlist_for_each_entry(exp, &help->expectations, lnode) {
2442 if (l3proto && exp->tuple.src.l3num != l3proto)
2449 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2453 if (!atomic_inc_not_zero(&exp->use))
2455 cb->args[1] = (unsigned long)exp;
2467 nf_ct_expect_put(last);
2472 static int ctnetlink_dump_exp_ct(struct sock *ctnl, struct sk_buff *skb,
2473 const struct nlmsghdr *nlh,
2474 const struct nlattr * const cda[])
2477 struct net *net = sock_net(ctnl);
2478 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2479 u_int8_t u3 = nfmsg->nfgen_family;
2480 struct nf_conntrack_tuple tuple;
2481 struct nf_conntrack_tuple_hash *h;
2484 struct netlink_dump_control c = {
2485 .dump = ctnetlink_exp_ct_dump_table,
2486 .done = ctnetlink_exp_done,
2489 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2493 if (cda[CTA_EXPECT_ZONE]) {
2494 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2499 h = nf_conntrack_find_get(net, zone, &tuple);
2503 ct = nf_ct_tuplehash_to_ctrack(h);
2506 err = netlink_dump_start(ctnl, skb, nlh, &c);
2512 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2513 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2514 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2515 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2516 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2517 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2518 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2519 .len = NF_CT_HELPER_NAME_LEN - 1 },
2520 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2521 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2522 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2523 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2524 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2528 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
2529 const struct nlmsghdr *nlh,
2530 const struct nlattr * const cda[])
2532 struct net *net = sock_net(ctnl);
2533 struct nf_conntrack_tuple tuple;
2534 struct nf_conntrack_expect *exp;
2535 struct sk_buff *skb2;
2536 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2537 u_int8_t u3 = nfmsg->nfgen_family;
2541 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2542 if (cda[CTA_EXPECT_MASTER])
2543 return ctnetlink_dump_exp_ct(ctnl, skb, nlh, cda);
2545 struct netlink_dump_control c = {
2546 .dump = ctnetlink_exp_dump_table,
2547 .done = ctnetlink_exp_done,
2549 return netlink_dump_start(ctnl, skb, nlh, &c);
2553 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2557 if (cda[CTA_EXPECT_TUPLE])
2558 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2559 else if (cda[CTA_EXPECT_MASTER])
2560 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2567 exp = nf_ct_expect_find_get(net, zone, &tuple);
2571 if (cda[CTA_EXPECT_ID]) {
2572 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2573 if (ntohl(id) != (u32)(unsigned long)exp) {
2574 nf_ct_expect_put(exp);
2580 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2582 nf_ct_expect_put(exp);
2587 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2588 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2590 nf_ct_expect_put(exp);
2594 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2603 /* this avoids a loop in nfnetlink. */
2604 return err == -EAGAIN ? -ENOBUFS : err;
2608 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
2609 const struct nlmsghdr *nlh,
2610 const struct nlattr * const cda[])
2612 struct net *net = sock_net(ctnl);
2613 struct nf_conntrack_expect *exp;
2614 struct nf_conntrack_tuple tuple;
2615 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2616 struct hlist_node *next;
2617 u_int8_t u3 = nfmsg->nfgen_family;
2622 if (cda[CTA_EXPECT_TUPLE]) {
2623 /* delete a single expect by tuple */
2624 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2628 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2632 /* bump usage count to 2 */
2633 exp = nf_ct_expect_find_get(net, zone, &tuple);
2637 if (cda[CTA_EXPECT_ID]) {
2638 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2639 if (ntohl(id) != (u32)(unsigned long)exp) {
2640 nf_ct_expect_put(exp);
2645 /* after list removal, usage count == 1 */
2646 spin_lock_bh(&nf_conntrack_lock);
2647 if (del_timer(&exp->timeout)) {
2648 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2650 nf_ct_expect_put(exp);
2652 spin_unlock_bh(&nf_conntrack_lock);
2653 /* have to put what we 'get' above.
2654 * after this line usage count == 0 */
2655 nf_ct_expect_put(exp);
2656 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2657 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2658 struct nf_conn_help *m_help;
2660 /* delete all expectations for this helper */
2661 spin_lock_bh(&nf_conntrack_lock);
2662 for (i = 0; i < nf_ct_expect_hsize; i++) {
2663 hlist_for_each_entry_safe(exp, next,
2664 &net->ct.expect_hash[i],
2666 m_help = nfct_help(exp->master);
2667 if (!strcmp(m_help->helper->name, name) &&
2668 del_timer(&exp->timeout)) {
2669 nf_ct_unlink_expect_report(exp,
2670 NETLINK_CB(skb).portid,
2672 nf_ct_expect_put(exp);
2676 spin_unlock_bh(&nf_conntrack_lock);
2678 /* This basically means we have to flush everything*/
2679 spin_lock_bh(&nf_conntrack_lock);
2680 for (i = 0; i < nf_ct_expect_hsize; i++) {
2681 hlist_for_each_entry_safe(exp, next,
2682 &net->ct.expect_hash[i],
2684 if (del_timer(&exp->timeout)) {
2685 nf_ct_unlink_expect_report(exp,
2686 NETLINK_CB(skb).portid,
2688 nf_ct_expect_put(exp);
2692 spin_unlock_bh(&nf_conntrack_lock);
2698 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2699 const struct nlattr * const cda[])
2701 if (cda[CTA_EXPECT_TIMEOUT]) {
2702 if (!del_timer(&x->timeout))
2705 x->timeout.expires = jiffies +
2706 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2707 add_timer(&x->timeout);
2712 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2713 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2714 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2718 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2719 struct nf_conntrack_expect *exp,
2722 #ifdef CONFIG_NF_NAT_NEEDED
2723 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2724 struct nf_conntrack_tuple nat_tuple = {};
2727 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2731 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2734 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2735 &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2739 exp->saved_addr = nat_tuple.src.u3;
2740 exp->saved_proto = nat_tuple.src.u;
2741 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2750 ctnetlink_create_expect(struct net *net, u16 zone,
2751 const struct nlattr * const cda[],
2753 u32 portid, int report)
2755 struct nf_conntrack_tuple tuple, mask, master_tuple;
2756 struct nf_conntrack_tuple_hash *h = NULL;
2757 struct nf_conntrack_expect *exp;
2759 struct nf_conn_help *help;
2760 struct nf_conntrack_helper *helper = NULL;
2761 u_int32_t class = 0;
2764 /* caller guarantees that those three CTA_EXPECT_* exist */
2765 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2768 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2771 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2775 /* Look for master conntrack of this expectation */
2776 h = nf_conntrack_find_get(net, zone, &master_tuple);
2779 ct = nf_ct_tuplehash_to_ctrack(h);
2781 /* Look for helper of this expectation */
2782 if (cda[CTA_EXPECT_HELP_NAME]) {
2783 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2785 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2786 nf_ct_protonum(ct));
2787 if (helper == NULL) {
2788 #ifdef CONFIG_MODULES
2789 if (request_module("nfct-helper-%s", helpname) < 0) {
2794 helper = __nf_conntrack_helper_find(helpname,
2796 nf_ct_protonum(ct));
2807 if (cda[CTA_EXPECT_CLASS] && helper) {
2808 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2809 if (class > helper->expect_class_max) {
2814 exp = nf_ct_expect_alloc(ct);
2819 help = nfct_help(ct);
2821 if (!cda[CTA_EXPECT_TIMEOUT]) {
2825 exp->timeout.expires =
2826 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2828 exp->flags = NF_CT_EXPECT_USERSPACE;
2829 if (cda[CTA_EXPECT_FLAGS]) {
2831 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2834 if (cda[CTA_EXPECT_FLAGS]) {
2835 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2836 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2840 if (cda[CTA_EXPECT_FN]) {
2841 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2842 struct nf_ct_helper_expectfn *expfn;
2844 expfn = nf_ct_helper_expectfn_find_by_name(name);
2845 if (expfn == NULL) {
2849 exp->expectfn = expfn->expectfn;
2851 exp->expectfn = NULL;
2855 exp->helper = helper;
2856 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2857 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2858 exp->mask.src.u.all = mask.src.u.all;
2860 if (cda[CTA_EXPECT_NAT]) {
2861 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2866 err = nf_ct_expect_related_report(exp, portid, report);
2868 nf_ct_expect_put(exp);
2870 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2875 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2876 const struct nlmsghdr *nlh,
2877 const struct nlattr * const cda[])
2879 struct net *net = sock_net(ctnl);
2880 struct nf_conntrack_tuple tuple;
2881 struct nf_conntrack_expect *exp;
2882 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2883 u_int8_t u3 = nfmsg->nfgen_family;
2887 if (!cda[CTA_EXPECT_TUPLE]
2888 || !cda[CTA_EXPECT_MASK]
2889 || !cda[CTA_EXPECT_MASTER])
2892 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2896 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2900 spin_lock_bh(&nf_conntrack_lock);
2901 exp = __nf_ct_expect_find(net, zone, &tuple);
2904 spin_unlock_bh(&nf_conntrack_lock);
2906 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2907 err = ctnetlink_create_expect(net, zone, cda,
2909 NETLINK_CB(skb).portid,
2916 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2917 err = ctnetlink_change_expect(exp, cda);
2918 spin_unlock_bh(&nf_conntrack_lock);
2924 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
2925 const struct ip_conntrack_stat *st)
2927 struct nlmsghdr *nlh;
2928 struct nfgenmsg *nfmsg;
2929 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2931 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
2932 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2936 nfmsg = nlmsg_data(nlh);
2937 nfmsg->nfgen_family = AF_UNSPEC;
2938 nfmsg->version = NFNETLINK_V0;
2939 nfmsg->res_id = htons(cpu);
2941 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
2942 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
2943 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
2944 goto nla_put_failure;
2946 nlmsg_end(skb, nlh);
2951 nlmsg_cancel(skb, nlh);
2956 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2959 struct net *net = sock_net(skb->sk);
2961 if (cb->args[0] == nr_cpu_ids)
2964 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2965 const struct ip_conntrack_stat *st;
2967 if (!cpu_possible(cpu))
2970 st = per_cpu_ptr(net->ct.stat, cpu);
2971 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
2982 ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
2983 const struct nlmsghdr *nlh,
2984 const struct nlattr * const cda[])
2986 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2987 struct netlink_dump_control c = {
2988 .dump = ctnetlink_exp_stat_cpu_dump,
2990 return netlink_dump_start(ctnl, skb, nlh, &c);
2996 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2997 static struct nf_ct_event_notifier ctnl_notifier = {
2998 .fcn = ctnetlink_conntrack_event,
3001 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3002 .fcn = ctnetlink_expect_event,
3006 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3007 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3008 .attr_count = CTA_MAX,
3009 .policy = ct_nla_policy },
3010 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3011 .attr_count = CTA_MAX,
3012 .policy = ct_nla_policy },
3013 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3014 .attr_count = CTA_MAX,
3015 .policy = ct_nla_policy },
3016 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3017 .attr_count = CTA_MAX,
3018 .policy = ct_nla_policy },
3019 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3020 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3021 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3022 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3025 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3026 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3027 .attr_count = CTA_EXPECT_MAX,
3028 .policy = exp_nla_policy },
3029 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3030 .attr_count = CTA_EXPECT_MAX,
3031 .policy = exp_nla_policy },
3032 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3033 .attr_count = CTA_EXPECT_MAX,
3034 .policy = exp_nla_policy },
3035 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3038 static const struct nfnetlink_subsystem ctnl_subsys = {
3039 .name = "conntrack",
3040 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3041 .cb_count = IPCTNL_MSG_MAX,
3045 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3046 .name = "conntrack_expect",
3047 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3048 .cb_count = IPCTNL_MSG_EXP_MAX,
3052 MODULE_ALIAS("ip_conntrack_netlink");
3053 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3054 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3056 static int __net_init ctnetlink_net_init(struct net *net)
3058 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3061 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3063 pr_err("ctnetlink_init: cannot register notifier.\n");
3067 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3069 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3070 goto err_unreg_notifier;
3075 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3077 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3083 static void ctnetlink_net_exit(struct net *net)
3085 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3086 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3087 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3091 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3095 list_for_each_entry(net, net_exit_list, exit_list)
3096 ctnetlink_net_exit(net);
3099 static struct pernet_operations ctnetlink_net_ops = {
3100 .init = ctnetlink_net_init,
3101 .exit_batch = ctnetlink_net_exit_batch,
3104 static int __init ctnetlink_init(void)
3108 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3109 ret = nfnetlink_subsys_register(&ctnl_subsys);
3111 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3115 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3117 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3118 goto err_unreg_subsys;
3121 ret = register_pernet_subsys(&ctnetlink_net_ops);
3123 pr_err("ctnetlink_init: cannot register pernet operations\n");
3124 goto err_unreg_exp_subsys;
3126 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3127 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3128 RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
3132 err_unreg_exp_subsys:
3133 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3135 nfnetlink_subsys_unregister(&ctnl_subsys);
3140 static void __exit ctnetlink_exit(void)
3142 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3144 unregister_pernet_subsys(&ctnetlink_net_ops);
3145 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3146 nfnetlink_subsys_unregister(&ctnl_subsys);
3147 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3148 RCU_INIT_POINTER(nfq_ct_hook, NULL);
3152 module_init(ctnetlink_init);
3153 module_exit(ctnetlink_exit);