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 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
834 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
836 if (likely(l3proto->nlattr_to_tuple)) {
837 ret = nla_validate_nested(attr, CTA_IP_MAX,
838 l3proto->nla_policy);
840 ret = l3proto->nlattr_to_tuple(tb, tuple);
848 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
849 [CTA_PROTO_NUM] = { .type = NLA_U8 },
853 ctnetlink_parse_tuple_proto(struct nlattr *attr,
854 struct nf_conntrack_tuple *tuple)
856 struct nlattr *tb[CTA_PROTO_MAX+1];
857 struct nf_conntrack_l4proto *l4proto;
860 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
864 if (!tb[CTA_PROTO_NUM])
866 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
869 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
871 if (likely(l4proto->nlattr_to_tuple)) {
872 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
873 l4proto->nla_policy);
875 ret = l4proto->nlattr_to_tuple(tb, tuple);
883 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
884 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
885 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
889 ctnetlink_parse_tuple(const struct nlattr * const cda[],
890 struct nf_conntrack_tuple *tuple,
891 enum ctattr_type type, u_int8_t l3num)
893 struct nlattr *tb[CTA_TUPLE_MAX+1];
896 memset(tuple, 0, sizeof(*tuple));
898 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
900 if (!tb[CTA_TUPLE_IP])
903 tuple->src.l3num = l3num;
905 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
909 if (!tb[CTA_TUPLE_PROTO])
912 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
916 /* orig and expect tuples get DIR_ORIGINAL */
917 if (type == CTA_TUPLE_REPLY)
918 tuple->dst.dir = IP_CT_DIR_REPLY;
920 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
926 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
929 #ifdef CONFIG_NF_CONNTRACK_ZONES
930 *zone = ntohs(nla_get_be16(attr));
940 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
941 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
942 .len = NF_CT_HELPER_NAME_LEN - 1 },
946 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
947 struct nlattr **helpinfo)
949 struct nlattr *tb[CTA_HELP_MAX+1];
951 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
953 if (!tb[CTA_HELP_NAME])
956 *helper_name = nla_data(tb[CTA_HELP_NAME]);
958 if (tb[CTA_HELP_INFO])
959 *helpinfo = tb[CTA_HELP_INFO];
964 #define __CTA_LABELS_MAX_LENGTH ((XT_CONNLABEL_MAXBIT + 1) / BITS_PER_BYTE)
965 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
966 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
967 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
968 [CTA_STATUS] = { .type = NLA_U32 },
969 [CTA_PROTOINFO] = { .type = NLA_NESTED },
970 [CTA_HELP] = { .type = NLA_NESTED },
971 [CTA_NAT_SRC] = { .type = NLA_NESTED },
972 [CTA_TIMEOUT] = { .type = NLA_U32 },
973 [CTA_MARK] = { .type = NLA_U32 },
974 [CTA_ID] = { .type = NLA_U32 },
975 [CTA_NAT_DST] = { .type = NLA_NESTED },
976 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
977 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
978 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
979 [CTA_ZONE] = { .type = NLA_U16 },
980 [CTA_MARK_MASK] = { .type = NLA_U32 },
981 [CTA_LABELS] = { .type = NLA_BINARY,
982 .len = __CTA_LABELS_MAX_LENGTH },
983 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
984 .len = __CTA_LABELS_MAX_LENGTH },
988 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
989 const struct nlmsghdr *nlh,
990 const struct nlattr * const cda[])
992 struct net *net = sock_net(ctnl);
993 struct nf_conntrack_tuple_hash *h;
994 struct nf_conntrack_tuple tuple;
996 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
997 u_int8_t u3 = nfmsg->nfgen_family;
1001 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1005 if (cda[CTA_TUPLE_ORIG])
1006 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1007 else if (cda[CTA_TUPLE_REPLY])
1008 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1010 /* Flush the whole table */
1011 nf_conntrack_flush_report(net,
1012 NETLINK_CB(skb).portid,
1020 h = nf_conntrack_find_get(net, zone, &tuple);
1024 ct = nf_ct_tuplehash_to_ctrack(h);
1027 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1028 if (id != (u32)(unsigned long)ct) {
1034 if (del_timer(&ct->timeout)) {
1035 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
1036 NETLINK_CB(skb).portid,
1037 nlmsg_report(nlh)) < 0) {
1038 nf_ct_delete_from_lists(ct);
1039 /* we failed to report the event, try later */
1040 nf_ct_dying_timeout(ct);
1044 /* death_by_timeout would report the event again */
1045 set_bit(IPS_DYING_BIT, &ct->status);
1046 nf_ct_delete_from_lists(ct);
1055 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
1056 const struct nlmsghdr *nlh,
1057 const struct nlattr * const cda[])
1059 struct net *net = sock_net(ctnl);
1060 struct nf_conntrack_tuple_hash *h;
1061 struct nf_conntrack_tuple tuple;
1063 struct sk_buff *skb2 = NULL;
1064 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1065 u_int8_t u3 = nfmsg->nfgen_family;
1069 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1070 struct netlink_dump_control c = {
1071 .dump = ctnetlink_dump_table,
1072 .done = ctnetlink_done,
1074 #ifdef CONFIG_NF_CONNTRACK_MARK
1075 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1076 struct ctnetlink_dump_filter *filter;
1078 filter = kzalloc(sizeof(struct ctnetlink_dump_filter),
1083 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
1085 ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1089 return netlink_dump_start(ctnl, skb, nlh, &c);
1092 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1096 if (cda[CTA_TUPLE_ORIG])
1097 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
1098 else if (cda[CTA_TUPLE_REPLY])
1099 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
1106 h = nf_conntrack_find_get(net, zone, &tuple);
1110 ct = nf_ct_tuplehash_to_ctrack(h);
1113 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1120 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1121 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1127 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1136 /* this avoids a loop in nfnetlink. */
1137 return err == -EAGAIN ? -ENOBUFS : err;
1140 static int ctnetlink_done_list(struct netlink_callback *cb)
1143 nf_ct_put((struct nf_conn *)cb->args[1]);
1148 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb,
1149 struct hlist_nulls_head *list)
1151 struct nf_conn *ct, *last;
1152 struct nf_conntrack_tuple_hash *h;
1153 struct hlist_nulls_node *n;
1154 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1155 u_int8_t l3proto = nfmsg->nfgen_family;
1161 spin_lock_bh(&nf_conntrack_lock);
1162 last = (struct nf_conn *)cb->args[1];
1164 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1165 ct = nf_ct_tuplehash_to_ctrack(h);
1166 if (l3proto && nf_ct_l3num(ct) != l3proto)
1174 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1176 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1180 nf_conntrack_get(&ct->ct_general);
1181 cb->args[1] = (unsigned long)ct;
1191 spin_unlock_bh(&nf_conntrack_lock);
1199 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1201 struct net *net = sock_net(skb->sk);
1203 return ctnetlink_dump_list(skb, cb, &net->ct.dying);
1207 ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb,
1208 const struct nlmsghdr *nlh,
1209 const struct nlattr * const cda[])
1211 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1212 struct netlink_dump_control c = {
1213 .dump = ctnetlink_dump_dying,
1214 .done = ctnetlink_done_list,
1216 return netlink_dump_start(ctnl, skb, nlh, &c);
1223 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1225 struct net *net = sock_net(skb->sk);
1227 return ctnetlink_dump_list(skb, cb, &net->ct.unconfirmed);
1231 ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb,
1232 const struct nlmsghdr *nlh,
1233 const struct nlattr * const cda[])
1235 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1236 struct netlink_dump_control c = {
1237 .dump = ctnetlink_dump_unconfirmed,
1238 .done = ctnetlink_done_list,
1240 return netlink_dump_start(ctnl, skb, nlh, &c);
1246 #ifdef CONFIG_NF_NAT_NEEDED
1248 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1249 enum nf_nat_manip_type manip,
1250 const struct nlattr *attr)
1252 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1255 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1256 if (!parse_nat_setup) {
1257 #ifdef CONFIG_MODULES
1259 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1260 if (request_module("nf-nat") < 0) {
1261 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1265 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1267 if (nfnetlink_parse_nat_setup_hook)
1273 err = parse_nat_setup(ct, manip, attr);
1274 if (err == -EAGAIN) {
1275 #ifdef CONFIG_MODULES
1277 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1278 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1279 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1283 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1294 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1297 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1298 d = ct->status ^ status;
1300 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1304 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1305 /* SEEN_REPLY bit can only be set */
1308 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1309 /* ASSURED bit can only be set */
1312 /* Be careful here, modifying NAT bits can screw up things,
1313 * so don't let users modify them directly if they don't pass
1315 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1320 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1322 #ifdef CONFIG_NF_NAT_NEEDED
1325 if (cda[CTA_NAT_DST]) {
1326 ret = ctnetlink_parse_nat_setup(ct,
1332 if (cda[CTA_NAT_SRC]) {
1333 ret = ctnetlink_parse_nat_setup(ct,
1346 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1348 struct nf_conntrack_helper *helper;
1349 struct nf_conn_help *help = nfct_help(ct);
1350 char *helpname = NULL;
1351 struct nlattr *helpinfo = NULL;
1354 /* don't change helper of sibling connections */
1358 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1362 if (!strcmp(helpname, "")) {
1363 if (help && help->helper) {
1364 /* we had a helper before ... */
1365 nf_ct_remove_expectations(ct);
1366 RCU_INIT_POINTER(help->helper, NULL);
1372 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1373 nf_ct_protonum(ct));
1374 if (helper == NULL) {
1375 #ifdef CONFIG_MODULES
1376 spin_unlock_bh(&nf_conntrack_lock);
1378 if (request_module("nfct-helper-%s", helpname) < 0) {
1379 spin_lock_bh(&nf_conntrack_lock);
1383 spin_lock_bh(&nf_conntrack_lock);
1384 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1385 nf_ct_protonum(ct));
1393 if (help->helper == helper) {
1394 /* update private helper data if allowed. */
1395 if (helper->from_nlattr)
1396 helper->from_nlattr(helpinfo, ct);
1402 /* we cannot set a helper for an existing conntrack */
1407 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1409 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1411 if (!del_timer(&ct->timeout))
1414 ct->timeout.expires = jiffies + timeout * HZ;
1415 add_timer(&ct->timeout);
1420 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1421 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1422 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1423 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1427 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1429 const struct nlattr *attr = cda[CTA_PROTOINFO];
1430 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1431 struct nf_conntrack_l4proto *l4proto;
1434 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1437 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1438 if (l4proto->from_nlattr)
1439 err = l4proto->from_nlattr(tb, ct);
1445 #ifdef CONFIG_NF_NAT_NEEDED
1446 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1447 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1448 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1449 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1453 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1455 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1457 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1459 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1462 natseq->correction_pos =
1463 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1465 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1468 natseq->offset_before =
1469 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1471 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1474 natseq->offset_after =
1475 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1481 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1482 const struct nlattr * const cda[])
1485 struct nf_conn_nat *nat = nfct_nat(ct);
1490 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1491 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1492 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1496 ct->status |= IPS_SEQ_ADJUST;
1499 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1500 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1501 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1505 ct->status |= IPS_SEQ_ADJUST;
1513 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1515 #ifdef CONFIG_NF_CONNTRACK_LABELS
1516 size_t len = nla_len(cda[CTA_LABELS]);
1517 const void *mask = cda[CTA_LABELS_MASK];
1519 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1523 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1524 nla_len(cda[CTA_LABELS_MASK]) != len)
1526 mask = nla_data(cda[CTA_LABELS_MASK]);
1531 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1538 ctnetlink_change_conntrack(struct nf_conn *ct,
1539 const struct nlattr * const cda[])
1543 /* only allow NAT changes and master assignation for new conntracks */
1544 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1547 if (cda[CTA_HELP]) {
1548 err = ctnetlink_change_helper(ct, cda);
1553 if (cda[CTA_TIMEOUT]) {
1554 err = ctnetlink_change_timeout(ct, cda);
1559 if (cda[CTA_STATUS]) {
1560 err = ctnetlink_change_status(ct, cda);
1565 if (cda[CTA_PROTOINFO]) {
1566 err = ctnetlink_change_protoinfo(ct, cda);
1571 #if defined(CONFIG_NF_CONNTRACK_MARK)
1573 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1576 #ifdef CONFIG_NF_NAT_NEEDED
1577 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1578 err = ctnetlink_change_nat_seq_adj(ct, cda);
1583 if (cda[CTA_LABELS]) {
1584 err = ctnetlink_attach_labels(ct, cda);
1592 static struct nf_conn *
1593 ctnetlink_create_conntrack(struct net *net, u16 zone,
1594 const struct nlattr * const cda[],
1595 struct nf_conntrack_tuple *otuple,
1596 struct nf_conntrack_tuple *rtuple,
1601 struct nf_conntrack_helper *helper;
1602 struct nf_conn_tstamp *tstamp;
1604 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1606 return ERR_PTR(-ENOMEM);
1608 if (!cda[CTA_TIMEOUT])
1610 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1612 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1615 if (cda[CTA_HELP]) {
1616 char *helpname = NULL;
1617 struct nlattr *helpinfo = NULL;
1619 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1623 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1624 nf_ct_protonum(ct));
1625 if (helper == NULL) {
1627 #ifdef CONFIG_MODULES
1628 if (request_module("nfct-helper-%s", helpname) < 0) {
1634 helper = __nf_conntrack_helper_find(helpname,
1636 nf_ct_protonum(ct));
1646 struct nf_conn_help *help;
1648 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1653 /* set private helper data if allowed. */
1654 if (helper->from_nlattr)
1655 helper->from_nlattr(helpinfo, ct);
1657 /* not in hash table yet so not strictly necessary */
1658 RCU_INIT_POINTER(help->helper, helper);
1661 /* try an implicit helper assignation */
1662 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1667 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1668 err = ctnetlink_change_nat(ct, cda);
1673 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1674 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1675 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1676 nf_ct_labels_ext_add(ct);
1678 /* we must add conntrack extensions before confirmation. */
1679 ct->status |= IPS_CONFIRMED;
1681 if (cda[CTA_STATUS]) {
1682 err = ctnetlink_change_status(ct, cda);
1687 #ifdef CONFIG_NF_NAT_NEEDED
1688 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1689 err = ctnetlink_change_nat_seq_adj(ct, cda);
1695 memset(&ct->proto, 0, sizeof(ct->proto));
1696 if (cda[CTA_PROTOINFO]) {
1697 err = ctnetlink_change_protoinfo(ct, cda);
1702 #if defined(CONFIG_NF_CONNTRACK_MARK)
1704 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1707 /* setup master conntrack: this is a confirmed expectation */
1708 if (cda[CTA_TUPLE_MASTER]) {
1709 struct nf_conntrack_tuple master;
1710 struct nf_conntrack_tuple_hash *master_h;
1711 struct nf_conn *master_ct;
1713 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1717 master_h = nf_conntrack_find_get(net, zone, &master);
1718 if (master_h == NULL) {
1722 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1723 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1724 ct->master = master_ct;
1726 tstamp = nf_conn_tstamp_find(ct);
1728 tstamp->start = ktime_to_ns(ktime_get_real());
1730 err = nf_conntrack_hash_check_insert(ct);
1741 nf_conntrack_free(ct);
1742 return ERR_PTR(err);
1746 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1747 const struct nlmsghdr *nlh,
1748 const struct nlattr * const cda[])
1750 struct net *net = sock_net(ctnl);
1751 struct nf_conntrack_tuple otuple, rtuple;
1752 struct nf_conntrack_tuple_hash *h = NULL;
1753 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1755 u_int8_t u3 = nfmsg->nfgen_family;
1759 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1763 if (cda[CTA_TUPLE_ORIG]) {
1764 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1769 if (cda[CTA_TUPLE_REPLY]) {
1770 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1775 if (cda[CTA_TUPLE_ORIG])
1776 h = nf_conntrack_find_get(net, zone, &otuple);
1777 else if (cda[CTA_TUPLE_REPLY])
1778 h = nf_conntrack_find_get(net, zone, &rtuple);
1782 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1783 enum ip_conntrack_events events;
1785 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1788 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1794 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1795 events = IPCT_RELATED;
1799 if (cda[CTA_LABELS] &&
1800 ctnetlink_attach_labels(ct, cda) == 0)
1801 events |= (1 << IPCT_LABEL);
1803 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1804 (1 << IPCT_ASSURED) |
1805 (1 << IPCT_HELPER) |
1806 (1 << IPCT_PROTOINFO) |
1807 (1 << IPCT_NATSEQADJ) |
1808 (1 << IPCT_MARK) | events,
1809 ct, NETLINK_CB(skb).portid,
1816 /* implicit 'else' */
1819 ct = nf_ct_tuplehash_to_ctrack(h);
1820 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1821 spin_lock_bh(&nf_conntrack_lock);
1822 err = ctnetlink_change_conntrack(ct, cda);
1823 spin_unlock_bh(&nf_conntrack_lock);
1825 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1826 (1 << IPCT_ASSURED) |
1827 (1 << IPCT_HELPER) |
1829 (1 << IPCT_PROTOINFO) |
1830 (1 << IPCT_NATSEQADJ) |
1832 ct, NETLINK_CB(skb).portid,
1842 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1843 __u16 cpu, const struct ip_conntrack_stat *st)
1845 struct nlmsghdr *nlh;
1846 struct nfgenmsg *nfmsg;
1847 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1849 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1850 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1854 nfmsg = nlmsg_data(nlh);
1855 nfmsg->nfgen_family = AF_UNSPEC;
1856 nfmsg->version = NFNETLINK_V0;
1857 nfmsg->res_id = htons(cpu);
1859 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1860 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1861 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1862 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1863 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1864 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1865 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1866 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1867 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1868 htonl(st->insert_failed)) ||
1869 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1870 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1871 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1872 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1873 htonl(st->search_restart)))
1874 goto nla_put_failure;
1876 nlmsg_end(skb, nlh);
1881 nlmsg_cancel(skb, nlh);
1886 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
1889 struct net *net = sock_net(skb->sk);
1891 if (cb->args[0] == nr_cpu_ids)
1894 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1895 const struct ip_conntrack_stat *st;
1897 if (!cpu_possible(cpu))
1900 st = per_cpu_ptr(net->ct.stat, cpu);
1901 if (ctnetlink_ct_stat_cpu_fill_info(skb,
1902 NETLINK_CB(cb->skb).portid,
1913 ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
1914 const struct nlmsghdr *nlh,
1915 const struct nlattr * const cda[])
1917 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1918 struct netlink_dump_control c = {
1919 .dump = ctnetlink_ct_stat_cpu_dump,
1921 return netlink_dump_start(ctnl, skb, nlh, &c);
1928 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
1931 struct nlmsghdr *nlh;
1932 struct nfgenmsg *nfmsg;
1933 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1934 unsigned int nr_conntracks = atomic_read(&net->ct.count);
1936 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
1937 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1941 nfmsg = nlmsg_data(nlh);
1942 nfmsg->nfgen_family = AF_UNSPEC;
1943 nfmsg->version = NFNETLINK_V0;
1946 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
1947 goto nla_put_failure;
1949 nlmsg_end(skb, nlh);
1954 nlmsg_cancel(skb, nlh);
1959 ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
1960 const struct nlmsghdr *nlh,
1961 const struct nlattr * const cda[])
1963 struct sk_buff *skb2;
1966 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1970 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
1972 NFNL_MSG_TYPE(nlh->nlmsg_type),
1977 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1986 /* this avoids a loop in nfnetlink. */
1987 return err == -EAGAIN ? -ENOBUFS : err;
1990 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
1992 ctnetlink_nfqueue_build_size(const struct nf_conn *ct)
1994 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
1995 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
1996 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
1997 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
1998 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
1999 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2000 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2001 + nla_total_size(0) /* CTA_PROTOINFO */
2002 + nla_total_size(0) /* CTA_HELP */
2003 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2004 + ctnetlink_secctx_size(ct)
2005 #ifdef CONFIG_NF_NAT_NEEDED
2006 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2007 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2009 #ifdef CONFIG_NF_CONNTRACK_MARK
2010 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2012 + ctnetlink_proto_size(ct)
2017 ctnetlink_nfqueue_build(struct sk_buff *skb, struct nf_conn *ct)
2019 struct nlattr *nest_parms;
2022 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2024 goto nla_put_failure;
2025 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2026 goto nla_put_failure;
2027 nla_nest_end(skb, nest_parms);
2029 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2031 goto nla_put_failure;
2032 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2033 goto nla_put_failure;
2034 nla_nest_end(skb, nest_parms);
2036 if (nf_ct_zone(ct)) {
2037 if (nla_put_be16(skb, CTA_ZONE, htons(nf_ct_zone(ct))))
2038 goto nla_put_failure;
2041 if (ctnetlink_dump_id(skb, ct) < 0)
2042 goto nla_put_failure;
2044 if (ctnetlink_dump_status(skb, ct) < 0)
2045 goto nla_put_failure;
2047 if (ctnetlink_dump_timeout(skb, ct) < 0)
2048 goto nla_put_failure;
2050 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2051 goto nla_put_failure;
2053 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2054 goto nla_put_failure;
2056 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2057 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2058 goto nla_put_failure;
2060 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2061 goto nla_put_failure;
2063 if ((ct->status & IPS_SEQ_ADJUST) &&
2064 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
2065 goto nla_put_failure;
2067 #ifdef CONFIG_NF_CONNTRACK_MARK
2068 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2069 goto nla_put_failure;
2071 if (ctnetlink_dump_labels(skb, ct) < 0)
2072 goto nla_put_failure;
2082 ctnetlink_nfqueue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2086 if (cda[CTA_TIMEOUT]) {
2087 err = ctnetlink_change_timeout(ct, cda);
2091 if (cda[CTA_STATUS]) {
2092 err = ctnetlink_change_status(ct, cda);
2096 if (cda[CTA_HELP]) {
2097 err = ctnetlink_change_helper(ct, cda);
2101 if (cda[CTA_LABELS]) {
2102 err = ctnetlink_attach_labels(ct, cda);
2106 #if defined(CONFIG_NF_CONNTRACK_MARK)
2108 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2114 ctnetlink_nfqueue_parse(const struct nlattr *attr, struct nf_conn *ct)
2116 struct nlattr *cda[CTA_MAX+1];
2119 nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2121 spin_lock_bh(&nf_conntrack_lock);
2122 ret = ctnetlink_nfqueue_parse_ct((const struct nlattr **)cda, ct);
2123 spin_unlock_bh(&nf_conntrack_lock);
2128 static struct nfq_ct_hook ctnetlink_nfqueue_hook = {
2129 .build_size = ctnetlink_nfqueue_build_size,
2130 .build = ctnetlink_nfqueue_build,
2131 .parse = ctnetlink_nfqueue_parse,
2133 #endif /* CONFIG_NETFILTER_NETLINK_QUEUE_CT */
2135 /***********************************************************************
2137 ***********************************************************************/
2140 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2141 const struct nf_conntrack_tuple *tuple,
2142 enum ctattr_expect type)
2144 struct nlattr *nest_parms;
2146 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2148 goto nla_put_failure;
2149 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2150 goto nla_put_failure;
2151 nla_nest_end(skb, nest_parms);
2160 ctnetlink_exp_dump_mask(struct sk_buff *skb,
2161 const struct nf_conntrack_tuple *tuple,
2162 const struct nf_conntrack_tuple_mask *mask)
2165 struct nf_conntrack_l3proto *l3proto;
2166 struct nf_conntrack_l4proto *l4proto;
2167 struct nf_conntrack_tuple m;
2168 struct nlattr *nest_parms;
2170 memset(&m, 0xFF, sizeof(m));
2171 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2172 m.src.u.all = mask->src.u.all;
2173 m.dst.protonum = tuple->dst.protonum;
2175 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2177 goto nla_put_failure;
2180 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2181 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2183 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2184 tuple->dst.protonum);
2185 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2189 if (unlikely(ret < 0))
2190 goto nla_put_failure;
2192 nla_nest_end(skb, nest_parms);
2200 static const union nf_inet_addr any_addr;
2203 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2204 const struct nf_conntrack_expect *exp)
2206 struct nf_conn *master = exp->master;
2207 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2208 struct nf_conn_help *help;
2209 #ifdef CONFIG_NF_NAT_NEEDED
2210 struct nlattr *nest_parms;
2211 struct nf_conntrack_tuple nat_tuple = {};
2213 struct nf_ct_helper_expectfn *expfn;
2218 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2219 goto nla_put_failure;
2220 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2221 goto nla_put_failure;
2222 if (ctnetlink_exp_dump_tuple(skb,
2223 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2224 CTA_EXPECT_MASTER) < 0)
2225 goto nla_put_failure;
2227 #ifdef CONFIG_NF_NAT_NEEDED
2228 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2229 exp->saved_proto.all) {
2230 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2232 goto nla_put_failure;
2234 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2235 goto nla_put_failure;
2237 nat_tuple.src.l3num = nf_ct_l3num(master);
2238 nat_tuple.src.u3 = exp->saved_addr;
2239 nat_tuple.dst.protonum = nf_ct_protonum(master);
2240 nat_tuple.src.u = exp->saved_proto;
2242 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2243 CTA_EXPECT_NAT_TUPLE) < 0)
2244 goto nla_put_failure;
2245 nla_nest_end(skb, nest_parms);
2248 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2249 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2250 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2251 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2252 goto nla_put_failure;
2253 help = nfct_help(master);
2255 struct nf_conntrack_helper *helper;
2257 helper = rcu_dereference(help->helper);
2259 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2260 goto nla_put_failure;
2262 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2263 if (expfn != NULL &&
2264 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2265 goto nla_put_failure;
2274 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2275 int event, const struct nf_conntrack_expect *exp)
2277 struct nlmsghdr *nlh;
2278 struct nfgenmsg *nfmsg;
2279 unsigned int flags = portid ? NLM_F_MULTI : 0;
2281 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2282 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2286 nfmsg = nlmsg_data(nlh);
2287 nfmsg->nfgen_family = exp->tuple.src.l3num;
2288 nfmsg->version = NFNETLINK_V0;
2291 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2292 goto nla_put_failure;
2294 nlmsg_end(skb, nlh);
2299 nlmsg_cancel(skb, nlh);
2303 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2305 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2307 struct nf_conntrack_expect *exp = item->exp;
2308 struct net *net = nf_ct_exp_net(exp);
2309 struct nlmsghdr *nlh;
2310 struct nfgenmsg *nfmsg;
2311 struct sk_buff *skb;
2312 unsigned int type, group;
2315 if (events & (1 << IPEXP_DESTROY)) {
2316 type = IPCTNL_MSG_EXP_DELETE;
2317 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2318 } else if (events & (1 << IPEXP_NEW)) {
2319 type = IPCTNL_MSG_EXP_NEW;
2320 flags = NLM_F_CREATE|NLM_F_EXCL;
2321 group = NFNLGRP_CONNTRACK_EXP_NEW;
2325 if (!item->report && !nfnetlink_has_listeners(net, group))
2328 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2332 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2333 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2337 nfmsg = nlmsg_data(nlh);
2338 nfmsg->nfgen_family = exp->tuple.src.l3num;
2339 nfmsg->version = NFNETLINK_V0;
2343 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2344 goto nla_put_failure;
2347 nlmsg_end(skb, nlh);
2348 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2353 nlmsg_cancel(skb, nlh);
2357 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2361 static int ctnetlink_exp_done(struct netlink_callback *cb)
2364 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2369 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2371 struct net *net = sock_net(skb->sk);
2372 struct nf_conntrack_expect *exp, *last;
2373 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2374 u_int8_t l3proto = nfmsg->nfgen_family;
2377 last = (struct nf_conntrack_expect *)cb->args[1];
2378 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2380 hlist_for_each_entry(exp, &net->ct.expect_hash[cb->args[0]],
2382 if (l3proto && exp->tuple.src.l3num != l3proto)
2389 if (ctnetlink_exp_fill_info(skb,
2390 NETLINK_CB(cb->skb).portid,
2394 if (!atomic_inc_not_zero(&exp->use))
2396 cb->args[1] = (unsigned long)exp;
2408 nf_ct_expect_put(last);
2414 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2416 struct nf_conntrack_expect *exp, *last;
2417 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2418 struct nf_conn *ct = cb->data;
2419 struct nf_conn_help *help = nfct_help(ct);
2420 u_int8_t l3proto = nfmsg->nfgen_family;
2426 last = (struct nf_conntrack_expect *)cb->args[1];
2428 hlist_for_each_entry(exp, &help->expectations, lnode) {
2429 if (l3proto && exp->tuple.src.l3num != l3proto)
2436 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2440 if (!atomic_inc_not_zero(&exp->use))
2442 cb->args[1] = (unsigned long)exp;
2454 nf_ct_expect_put(last);
2459 static int ctnetlink_dump_exp_ct(struct sock *ctnl, struct sk_buff *skb,
2460 const struct nlmsghdr *nlh,
2461 const struct nlattr * const cda[])
2464 struct net *net = sock_net(ctnl);
2465 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2466 u_int8_t u3 = nfmsg->nfgen_family;
2467 struct nf_conntrack_tuple tuple;
2468 struct nf_conntrack_tuple_hash *h;
2471 struct netlink_dump_control c = {
2472 .dump = ctnetlink_exp_ct_dump_table,
2473 .done = ctnetlink_exp_done,
2476 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2480 if (cda[CTA_EXPECT_ZONE]) {
2481 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2486 h = nf_conntrack_find_get(net, zone, &tuple);
2490 ct = nf_ct_tuplehash_to_ctrack(h);
2493 err = netlink_dump_start(ctnl, skb, nlh, &c);
2499 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2500 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2501 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2502 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2503 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2504 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2505 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2506 .len = NF_CT_HELPER_NAME_LEN - 1 },
2507 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2508 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2509 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2510 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2511 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2515 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
2516 const struct nlmsghdr *nlh,
2517 const struct nlattr * const cda[])
2519 struct net *net = sock_net(ctnl);
2520 struct nf_conntrack_tuple tuple;
2521 struct nf_conntrack_expect *exp;
2522 struct sk_buff *skb2;
2523 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2524 u_int8_t u3 = nfmsg->nfgen_family;
2528 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2529 if (cda[CTA_EXPECT_MASTER])
2530 return ctnetlink_dump_exp_ct(ctnl, skb, nlh, cda);
2532 struct netlink_dump_control c = {
2533 .dump = ctnetlink_exp_dump_table,
2534 .done = ctnetlink_exp_done,
2536 return netlink_dump_start(ctnl, skb, nlh, &c);
2540 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2544 if (cda[CTA_EXPECT_TUPLE])
2545 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2546 else if (cda[CTA_EXPECT_MASTER])
2547 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
2554 exp = nf_ct_expect_find_get(net, zone, &tuple);
2558 if (cda[CTA_EXPECT_ID]) {
2559 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2560 if (ntohl(id) != (u32)(unsigned long)exp) {
2561 nf_ct_expect_put(exp);
2567 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2569 nf_ct_expect_put(exp);
2574 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2575 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2577 nf_ct_expect_put(exp);
2581 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2590 /* this avoids a loop in nfnetlink. */
2591 return err == -EAGAIN ? -ENOBUFS : err;
2595 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
2596 const struct nlmsghdr *nlh,
2597 const struct nlattr * const cda[])
2599 struct net *net = sock_net(ctnl);
2600 struct nf_conntrack_expect *exp;
2601 struct nf_conntrack_tuple tuple;
2602 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2603 struct hlist_node *next;
2604 u_int8_t u3 = nfmsg->nfgen_family;
2609 if (cda[CTA_EXPECT_TUPLE]) {
2610 /* delete a single expect by tuple */
2611 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2615 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2619 /* bump usage count to 2 */
2620 exp = nf_ct_expect_find_get(net, zone, &tuple);
2624 if (cda[CTA_EXPECT_ID]) {
2625 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2626 if (ntohl(id) != (u32)(unsigned long)exp) {
2627 nf_ct_expect_put(exp);
2632 /* after list removal, usage count == 1 */
2633 spin_lock_bh(&nf_conntrack_lock);
2634 if (del_timer(&exp->timeout)) {
2635 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2637 nf_ct_expect_put(exp);
2639 spin_unlock_bh(&nf_conntrack_lock);
2640 /* have to put what we 'get' above.
2641 * after this line usage count == 0 */
2642 nf_ct_expect_put(exp);
2643 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2644 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2645 struct nf_conn_help *m_help;
2647 /* delete all expectations for this helper */
2648 spin_lock_bh(&nf_conntrack_lock);
2649 for (i = 0; i < nf_ct_expect_hsize; i++) {
2650 hlist_for_each_entry_safe(exp, next,
2651 &net->ct.expect_hash[i],
2653 m_help = nfct_help(exp->master);
2654 if (!strcmp(m_help->helper->name, name) &&
2655 del_timer(&exp->timeout)) {
2656 nf_ct_unlink_expect_report(exp,
2657 NETLINK_CB(skb).portid,
2659 nf_ct_expect_put(exp);
2663 spin_unlock_bh(&nf_conntrack_lock);
2665 /* This basically means we have to flush everything*/
2666 spin_lock_bh(&nf_conntrack_lock);
2667 for (i = 0; i < nf_ct_expect_hsize; i++) {
2668 hlist_for_each_entry_safe(exp, next,
2669 &net->ct.expect_hash[i],
2671 if (del_timer(&exp->timeout)) {
2672 nf_ct_unlink_expect_report(exp,
2673 NETLINK_CB(skb).portid,
2675 nf_ct_expect_put(exp);
2679 spin_unlock_bh(&nf_conntrack_lock);
2685 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2686 const struct nlattr * const cda[])
2688 if (cda[CTA_EXPECT_TIMEOUT]) {
2689 if (!del_timer(&x->timeout))
2692 x->timeout.expires = jiffies +
2693 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2694 add_timer(&x->timeout);
2699 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2700 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2701 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2705 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2706 struct nf_conntrack_expect *exp,
2709 #ifdef CONFIG_NF_NAT_NEEDED
2710 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2711 struct nf_conntrack_tuple nat_tuple = {};
2714 nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2716 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2719 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2720 &nat_tuple, CTA_EXPECT_NAT_TUPLE, u3);
2724 exp->saved_addr = nat_tuple.src.u3;
2725 exp->saved_proto = nat_tuple.src.u;
2726 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2735 ctnetlink_create_expect(struct net *net, u16 zone,
2736 const struct nlattr * const cda[],
2738 u32 portid, int report)
2740 struct nf_conntrack_tuple tuple, mask, master_tuple;
2741 struct nf_conntrack_tuple_hash *h = NULL;
2742 struct nf_conntrack_expect *exp;
2744 struct nf_conn_help *help;
2745 struct nf_conntrack_helper *helper = NULL;
2746 u_int32_t class = 0;
2749 /* caller guarantees that those three CTA_EXPECT_* exist */
2750 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2753 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
2756 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
2760 /* Look for master conntrack of this expectation */
2761 h = nf_conntrack_find_get(net, zone, &master_tuple);
2764 ct = nf_ct_tuplehash_to_ctrack(h);
2766 /* Look for helper of this expectation */
2767 if (cda[CTA_EXPECT_HELP_NAME]) {
2768 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2770 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2771 nf_ct_protonum(ct));
2772 if (helper == NULL) {
2773 #ifdef CONFIG_MODULES
2774 if (request_module("nfct-helper-%s", helpname) < 0) {
2779 helper = __nf_conntrack_helper_find(helpname,
2781 nf_ct_protonum(ct));
2792 if (cda[CTA_EXPECT_CLASS] && helper) {
2793 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
2794 if (class > helper->expect_class_max) {
2799 exp = nf_ct_expect_alloc(ct);
2804 help = nfct_help(ct);
2806 if (!cda[CTA_EXPECT_TIMEOUT]) {
2810 exp->timeout.expires =
2811 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2813 exp->flags = NF_CT_EXPECT_USERSPACE;
2814 if (cda[CTA_EXPECT_FLAGS]) {
2816 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2819 if (cda[CTA_EXPECT_FLAGS]) {
2820 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
2821 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2825 if (cda[CTA_EXPECT_FN]) {
2826 const char *name = nla_data(cda[CTA_EXPECT_FN]);
2827 struct nf_ct_helper_expectfn *expfn;
2829 expfn = nf_ct_helper_expectfn_find_by_name(name);
2830 if (expfn == NULL) {
2834 exp->expectfn = expfn->expectfn;
2836 exp->expectfn = NULL;
2840 exp->helper = helper;
2841 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2842 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2843 exp->mask.src.u.all = mask.src.u.all;
2845 if (cda[CTA_EXPECT_NAT]) {
2846 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
2851 err = nf_ct_expect_related_report(exp, portid, report);
2853 nf_ct_expect_put(exp);
2855 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2860 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2861 const struct nlmsghdr *nlh,
2862 const struct nlattr * const cda[])
2864 struct net *net = sock_net(ctnl);
2865 struct nf_conntrack_tuple tuple;
2866 struct nf_conntrack_expect *exp;
2867 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2868 u_int8_t u3 = nfmsg->nfgen_family;
2872 if (!cda[CTA_EXPECT_TUPLE]
2873 || !cda[CTA_EXPECT_MASK]
2874 || !cda[CTA_EXPECT_MASTER])
2877 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2881 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2885 spin_lock_bh(&nf_conntrack_lock);
2886 exp = __nf_ct_expect_find(net, zone, &tuple);
2889 spin_unlock_bh(&nf_conntrack_lock);
2891 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2892 err = ctnetlink_create_expect(net, zone, cda,
2894 NETLINK_CB(skb).portid,
2901 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2902 err = ctnetlink_change_expect(exp, cda);
2903 spin_unlock_bh(&nf_conntrack_lock);
2909 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
2910 const struct ip_conntrack_stat *st)
2912 struct nlmsghdr *nlh;
2913 struct nfgenmsg *nfmsg;
2914 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2916 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
2917 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2921 nfmsg = nlmsg_data(nlh);
2922 nfmsg->nfgen_family = AF_UNSPEC;
2923 nfmsg->version = NFNETLINK_V0;
2924 nfmsg->res_id = htons(cpu);
2926 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
2927 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
2928 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
2929 goto nla_put_failure;
2931 nlmsg_end(skb, nlh);
2936 nlmsg_cancel(skb, nlh);
2941 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2944 struct net *net = sock_net(skb->sk);
2946 if (cb->args[0] == nr_cpu_ids)
2949 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2950 const struct ip_conntrack_stat *st;
2952 if (!cpu_possible(cpu))
2955 st = per_cpu_ptr(net->ct.stat, cpu);
2956 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
2967 ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
2968 const struct nlmsghdr *nlh,
2969 const struct nlattr * const cda[])
2971 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2972 struct netlink_dump_control c = {
2973 .dump = ctnetlink_exp_stat_cpu_dump,
2975 return netlink_dump_start(ctnl, skb, nlh, &c);
2981 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2982 static struct nf_ct_event_notifier ctnl_notifier = {
2983 .fcn = ctnetlink_conntrack_event,
2986 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2987 .fcn = ctnetlink_expect_event,
2991 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2992 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
2993 .attr_count = CTA_MAX,
2994 .policy = ct_nla_policy },
2995 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
2996 .attr_count = CTA_MAX,
2997 .policy = ct_nla_policy },
2998 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
2999 .attr_count = CTA_MAX,
3000 .policy = ct_nla_policy },
3001 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3002 .attr_count = CTA_MAX,
3003 .policy = ct_nla_policy },
3004 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3005 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3006 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3007 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3010 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3011 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3012 .attr_count = CTA_EXPECT_MAX,
3013 .policy = exp_nla_policy },
3014 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3015 .attr_count = CTA_EXPECT_MAX,
3016 .policy = exp_nla_policy },
3017 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3018 .attr_count = CTA_EXPECT_MAX,
3019 .policy = exp_nla_policy },
3020 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3023 static const struct nfnetlink_subsystem ctnl_subsys = {
3024 .name = "conntrack",
3025 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3026 .cb_count = IPCTNL_MSG_MAX,
3030 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3031 .name = "conntrack_expect",
3032 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3033 .cb_count = IPCTNL_MSG_EXP_MAX,
3037 MODULE_ALIAS("ip_conntrack_netlink");
3038 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3039 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3041 static int __net_init ctnetlink_net_init(struct net *net)
3043 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3046 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3048 pr_err("ctnetlink_init: cannot register notifier.\n");
3052 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3054 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3055 goto err_unreg_notifier;
3060 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3062 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3068 static void ctnetlink_net_exit(struct net *net)
3070 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3071 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3072 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3076 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3080 list_for_each_entry(net, net_exit_list, exit_list)
3081 ctnetlink_net_exit(net);
3084 static struct pernet_operations ctnetlink_net_ops = {
3085 .init = ctnetlink_net_init,
3086 .exit_batch = ctnetlink_net_exit_batch,
3089 static int __init ctnetlink_init(void)
3093 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3094 ret = nfnetlink_subsys_register(&ctnl_subsys);
3096 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3100 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3102 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3103 goto err_unreg_subsys;
3106 ret = register_pernet_subsys(&ctnetlink_net_ops);
3108 pr_err("ctnetlink_init: cannot register pernet operations\n");
3109 goto err_unreg_exp_subsys;
3111 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3112 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3113 RCU_INIT_POINTER(nfq_ct_hook, &ctnetlink_nfqueue_hook);
3117 err_unreg_exp_subsys:
3118 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3120 nfnetlink_subsys_unregister(&ctnl_subsys);
3125 static void __exit ctnetlink_exit(void)
3127 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3129 unregister_pernet_subsys(&ctnetlink_net_ops);
3130 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3131 nfnetlink_subsys_unregister(&ctnl_subsys);
3132 #ifdef CONFIG_NETFILTER_NETLINK_QUEUE_CT
3133 RCU_INIT_POINTER(nfq_ct_hook, NULL);
3137 module_init(ctnetlink_init);
3138 module_exit(ctnetlink_exit);