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>
32 #include <linux/siphash.h>
34 #include <linux/netfilter.h>
35 #include <net/netlink.h>
37 #include <net/netfilter/nf_conntrack.h>
38 #include <net/netfilter/nf_conntrack_core.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_seqadj.h>
42 #include <net/netfilter/nf_conntrack_l3proto.h>
43 #include <net/netfilter/nf_conntrack_l4proto.h>
44 #include <net/netfilter/nf_conntrack_tuple.h>
45 #include <net/netfilter/nf_conntrack_acct.h>
46 #include <net/netfilter/nf_conntrack_zones.h>
47 #include <net/netfilter/nf_conntrack_timestamp.h>
48 #include <net/netfilter/nf_conntrack_labels.h>
49 #include <net/netfilter/nf_conntrack_seqadj.h>
50 #include <net/netfilter/nf_conntrack_synproxy.h>
51 #ifdef CONFIG_NF_NAT_NEEDED
52 #include <net/netfilter/nf_nat_core.h>
53 #include <net/netfilter/nf_nat_l4proto.h>
54 #include <net/netfilter/nf_nat_helper.h>
57 #include <linux/netfilter/nfnetlink.h>
58 #include <linux/netfilter/nfnetlink_conntrack.h>
60 MODULE_LICENSE("GPL");
62 static char __initdata version[] = "0.93";
65 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
66 const struct nf_conntrack_tuple *tuple,
67 struct nf_conntrack_l4proto *l4proto)
70 struct nlattr *nest_parms;
72 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
75 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
78 if (likely(l4proto->tuple_to_nlattr))
79 ret = l4proto->tuple_to_nlattr(skb, tuple);
81 nla_nest_end(skb, nest_parms);
90 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
91 const struct nf_conntrack_tuple *tuple,
92 struct nf_conntrack_l3proto *l3proto)
95 struct nlattr *nest_parms;
97 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
101 if (likely(l3proto->tuple_to_nlattr))
102 ret = l3proto->tuple_to_nlattr(skb, tuple);
104 nla_nest_end(skb, nest_parms);
113 ctnetlink_dump_tuples(struct sk_buff *skb,
114 const struct nf_conntrack_tuple *tuple)
117 struct nf_conntrack_l3proto *l3proto;
118 struct nf_conntrack_l4proto *l4proto;
121 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
122 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
125 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
126 tuple->dst.protonum);
127 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
134 ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
135 const struct nf_conntrack_zone *zone, int dir)
137 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
139 if (nla_put_be16(skb, attrtype, htons(zone->id)))
140 goto nla_put_failure;
148 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
150 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
151 goto nla_put_failure;
159 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
161 long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
166 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
167 goto nla_put_failure;
175 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
177 struct nf_conntrack_l4proto *l4proto;
178 struct nlattr *nest_proto;
181 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
182 if (!l4proto->to_nlattr)
185 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
187 goto nla_put_failure;
189 ret = l4proto->to_nlattr(skb, nest_proto, ct);
191 nla_nest_end(skb, nest_proto);
200 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
202 struct nlattr *nest_helper;
203 const struct nf_conn_help *help = nfct_help(ct);
204 struct nf_conntrack_helper *helper;
209 helper = rcu_dereference(help->helper);
213 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
215 goto nla_put_failure;
216 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
217 goto nla_put_failure;
219 if (helper->to_nlattr)
220 helper->to_nlattr(skb, ct);
222 nla_nest_end(skb, nest_helper);
231 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
232 enum ip_conntrack_dir dir, int type)
234 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
235 struct nf_conn_counter *counter = acct->counter;
236 struct nlattr *nest_count;
239 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
240 pkts = atomic64_xchg(&counter[dir].packets, 0);
241 bytes = atomic64_xchg(&counter[dir].bytes, 0);
243 pkts = atomic64_read(&counter[dir].packets);
244 bytes = atomic64_read(&counter[dir].bytes);
247 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
249 goto nla_put_failure;
251 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) ||
252 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes)))
253 goto nla_put_failure;
255 nla_nest_end(skb, nest_count);
264 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
266 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
271 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
273 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
280 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
282 struct nlattr *nest_count;
283 const struct nf_conn_tstamp *tstamp;
285 tstamp = nf_conn_tstamp_find(ct);
289 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
291 goto nla_put_failure;
293 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) ||
294 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
295 cpu_to_be64(tstamp->stop))))
296 goto nla_put_failure;
297 nla_nest_end(skb, nest_count);
305 #ifdef CONFIG_NF_CONNTRACK_MARK
307 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
309 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
310 goto nla_put_failure;
317 #define ctnetlink_dump_mark(a, b) (0)
320 #ifdef CONFIG_NF_CONNTRACK_SECMARK
322 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
324 struct nlattr *nest_secctx;
328 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
333 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
335 goto nla_put_failure;
337 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
338 goto nla_put_failure;
339 nla_nest_end(skb, nest_secctx);
343 security_release_secctx(secctx, len);
347 #define ctnetlink_dump_secctx(a, b) (0)
350 #ifdef CONFIG_NF_CONNTRACK_LABELS
351 static int ctnetlink_label_size(const struct nf_conn *ct)
353 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
357 return nla_total_size(labels->words * sizeof(long));
361 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
363 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
369 len = labels->words * sizeof(long);
372 if (labels->bits[i] != 0)
373 return nla_put(skb, CTA_LABELS, len, labels->bits);
375 } while (i < labels->words);
380 #define ctnetlink_dump_labels(a, b) (0)
381 #define ctnetlink_label_size(a) (0)
384 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
387 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
389 struct nlattr *nest_parms;
391 if (!(ct->status & IPS_EXPECTED))
394 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
396 goto nla_put_failure;
397 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
398 goto nla_put_failure;
399 nla_nest_end(skb, nest_parms);
408 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
410 struct nlattr *nest_parms;
412 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
414 goto nla_put_failure;
416 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
417 htonl(seq->correction_pos)) ||
418 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
419 htonl(seq->offset_before)) ||
420 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
421 htonl(seq->offset_after)))
422 goto nla_put_failure;
424 nla_nest_end(skb, nest_parms);
433 ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
435 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
436 struct nf_ct_seqadj *seq;
438 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
441 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
442 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
445 seq = &seqadj->seq[IP_CT_DIR_REPLY];
446 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
453 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
455 __be32 id = (__force __be32)nf_ct_get_id(ct);
457 if (nla_put_be32(skb, CTA_ID, id))
458 goto nla_put_failure;
466 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
468 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
469 goto nla_put_failure;
477 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
480 const struct nf_conntrack_zone *zone;
481 struct nlmsghdr *nlh;
482 struct nfgenmsg *nfmsg;
483 struct nlattr *nest_parms;
484 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
486 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
487 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
491 nfmsg = nlmsg_data(nlh);
492 nfmsg->nfgen_family = nf_ct_l3num(ct);
493 nfmsg->version = NFNETLINK_V0;
496 zone = nf_ct_zone(ct);
498 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
500 goto nla_put_failure;
501 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
502 goto nla_put_failure;
503 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
504 NF_CT_ZONE_DIR_ORIG) < 0)
505 goto nla_put_failure;
506 nla_nest_end(skb, nest_parms);
508 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
510 goto nla_put_failure;
511 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
512 goto nla_put_failure;
513 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
514 NF_CT_ZONE_DIR_REPL) < 0)
515 goto nla_put_failure;
516 nla_nest_end(skb, nest_parms);
518 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
519 NF_CT_DEFAULT_ZONE_DIR) < 0)
520 goto nla_put_failure;
522 if (ctnetlink_dump_status(skb, ct) < 0 ||
523 ctnetlink_dump_timeout(skb, ct) < 0 ||
524 ctnetlink_dump_acct(skb, ct, type) < 0 ||
525 ctnetlink_dump_timestamp(skb, ct) < 0 ||
526 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
527 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
528 ctnetlink_dump_mark(skb, ct) < 0 ||
529 ctnetlink_dump_secctx(skb, ct) < 0 ||
530 ctnetlink_dump_labels(skb, ct) < 0 ||
531 ctnetlink_dump_id(skb, ct) < 0 ||
532 ctnetlink_dump_use(skb, ct) < 0 ||
533 ctnetlink_dump_master(skb, ct) < 0 ||
534 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
535 goto nla_put_failure;
542 nlmsg_cancel(skb, nlh);
547 ctnetlink_proto_size(const struct nf_conn *ct)
549 struct nf_conntrack_l3proto *l3proto;
550 struct nf_conntrack_l4proto *l4proto;
554 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
555 len += l3proto->nla_size;
557 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
558 len += l4proto->nla_size;
565 ctnetlink_acct_size(const struct nf_conn *ct)
567 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
569 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
570 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
571 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
576 ctnetlink_secctx_size(const struct nf_conn *ct)
578 #ifdef CONFIG_NF_CONNTRACK_SECMARK
581 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
585 return nla_total_size(0) /* CTA_SECCTX */
586 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
593 ctnetlink_timestamp_size(const struct nf_conn *ct)
595 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
596 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
598 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
605 ctnetlink_nlmsg_size(const struct nf_conn *ct)
607 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
608 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
609 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
610 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
611 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
612 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
613 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
614 + ctnetlink_acct_size(ct)
615 + ctnetlink_timestamp_size(ct)
616 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
617 + nla_total_size(0) /* CTA_PROTOINFO */
618 + nla_total_size(0) /* CTA_HELP */
619 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
620 + ctnetlink_secctx_size(ct)
621 #ifdef CONFIG_NF_NAT_NEEDED
622 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
623 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
625 #ifdef CONFIG_NF_CONNTRACK_MARK
626 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
628 #ifdef CONFIG_NF_CONNTRACK_ZONES
629 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
631 + ctnetlink_proto_size(ct)
632 + ctnetlink_label_size(ct)
636 #ifdef CONFIG_NF_CONNTRACK_EVENTS
638 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
640 const struct nf_conntrack_zone *zone;
642 struct nlmsghdr *nlh;
643 struct nfgenmsg *nfmsg;
644 struct nlattr *nest_parms;
645 struct nf_conn *ct = item->ct;
648 unsigned int flags = 0, group;
651 /* ignore our fake conntrack entry */
652 if (nf_ct_is_untracked(ct))
655 if (events & (1 << IPCT_DESTROY)) {
656 type = IPCTNL_MSG_CT_DELETE;
657 group = NFNLGRP_CONNTRACK_DESTROY;
658 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
659 type = IPCTNL_MSG_CT_NEW;
660 flags = NLM_F_CREATE|NLM_F_EXCL;
661 group = NFNLGRP_CONNTRACK_NEW;
663 type = IPCTNL_MSG_CT_NEW;
664 group = NFNLGRP_CONNTRACK_UPDATE;
669 if (!item->report && !nfnetlink_has_listeners(net, group))
672 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
676 type |= NFNL_SUBSYS_CTNETLINK << 8;
677 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
681 nfmsg = nlmsg_data(nlh);
682 nfmsg->nfgen_family = nf_ct_l3num(ct);
683 nfmsg->version = NFNETLINK_V0;
687 zone = nf_ct_zone(ct);
689 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
691 goto nla_put_failure;
692 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
693 goto nla_put_failure;
694 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
695 NF_CT_ZONE_DIR_ORIG) < 0)
696 goto nla_put_failure;
697 nla_nest_end(skb, nest_parms);
699 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
701 goto nla_put_failure;
702 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
703 goto nla_put_failure;
704 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
705 NF_CT_ZONE_DIR_REPL) < 0)
706 goto nla_put_failure;
707 nla_nest_end(skb, nest_parms);
709 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
710 NF_CT_DEFAULT_ZONE_DIR) < 0)
711 goto nla_put_failure;
713 if (ctnetlink_dump_id(skb, ct) < 0)
714 goto nla_put_failure;
716 if (ctnetlink_dump_status(skb, ct) < 0)
717 goto nla_put_failure;
719 if (events & (1 << IPCT_DESTROY)) {
720 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
721 ctnetlink_dump_timestamp(skb, ct) < 0)
722 goto nla_put_failure;
724 if (ctnetlink_dump_timeout(skb, ct) < 0)
725 goto nla_put_failure;
727 if (events & (1 << IPCT_PROTOINFO)
728 && ctnetlink_dump_protoinfo(skb, ct) < 0)
729 goto nla_put_failure;
731 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
732 && ctnetlink_dump_helpinfo(skb, ct) < 0)
733 goto nla_put_failure;
735 #ifdef CONFIG_NF_CONNTRACK_SECMARK
736 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
737 && ctnetlink_dump_secctx(skb, ct) < 0)
738 goto nla_put_failure;
740 if (events & (1 << IPCT_LABEL) &&
741 ctnetlink_dump_labels(skb, ct) < 0)
742 goto nla_put_failure;
744 if (events & (1 << IPCT_RELATED) &&
745 ctnetlink_dump_master(skb, ct) < 0)
746 goto nla_put_failure;
748 if (events & (1 << IPCT_SEQADJ) &&
749 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
750 goto nla_put_failure;
753 #ifdef CONFIG_NF_CONNTRACK_MARK
754 if ((events & (1 << IPCT_MARK) || ct->mark)
755 && ctnetlink_dump_mark(skb, ct) < 0)
756 goto nla_put_failure;
761 err = nfnetlink_send(skb, net, item->portid, group, item->report,
763 if (err == -ENOBUFS || err == -EAGAIN)
770 nlmsg_cancel(skb, nlh);
774 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
779 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
781 static int ctnetlink_done(struct netlink_callback *cb)
784 nf_ct_put((struct nf_conn *)cb->args[1]);
789 struct ctnetlink_filter {
796 static struct ctnetlink_filter *
797 ctnetlink_alloc_filter(const struct nlattr * const cda[])
799 #ifdef CONFIG_NF_CONNTRACK_MARK
800 struct ctnetlink_filter *filter;
802 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
804 return ERR_PTR(-ENOMEM);
806 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
807 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
811 return ERR_PTR(-EOPNOTSUPP);
815 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
817 struct ctnetlink_filter *filter = data;
822 #ifdef CONFIG_NF_CONNTRACK_MARK
823 if ((ct->mark & filter->mark.mask) == filter->mark.val)
831 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
833 struct net *net = sock_net(skb->sk);
834 struct nf_conn *ct, *last;
835 struct nf_conntrack_tuple_hash *h;
836 struct hlist_nulls_node *n;
837 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
838 u_int8_t l3proto = nfmsg->nfgen_family;
842 last = (struct nf_conn *)cb->args[1];
845 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
847 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
849 if (cb->args[0] >= net->ct.htable_size) {
853 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
855 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
857 ct = nf_ct_tuplehash_to_ctrack(h);
858 /* Dump entries of a given L3 protocol number.
859 * If it is not specified, ie. l3proto == 0,
860 * then dump everything. */
861 if (l3proto && nf_ct_l3num(ct) != l3proto)
868 if (!ctnetlink_filter_match(ct, cb->data))
873 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
875 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
879 nf_conntrack_get(&ct->ct_general);
880 cb->args[1] = (unsigned long)ct;
894 /* nf ct hash resize happened, now clear the leftover. */
895 if ((struct nf_conn *)cb->args[1] == last)
905 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
907 struct nlattr *tb[CTA_IP_MAX+1];
908 struct nf_conntrack_l3proto *l3proto;
911 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
916 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
918 if (likely(l3proto->nlattr_to_tuple)) {
919 ret = nla_validate_nested(attr, CTA_IP_MAX,
920 l3proto->nla_policy);
922 ret = l3proto->nlattr_to_tuple(tb, tuple);
930 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
931 [CTA_PROTO_NUM] = { .type = NLA_U8 },
935 ctnetlink_parse_tuple_proto(struct nlattr *attr,
936 struct nf_conntrack_tuple *tuple)
938 struct nlattr *tb[CTA_PROTO_MAX+1];
939 struct nf_conntrack_l4proto *l4proto;
942 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
946 if (!tb[CTA_PROTO_NUM])
948 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
951 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
953 if (likely(l4proto->nlattr_to_tuple)) {
954 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
955 l4proto->nla_policy);
957 ret = l4proto->nlattr_to_tuple(tb, tuple);
966 ctnetlink_parse_zone(const struct nlattr *attr,
967 struct nf_conntrack_zone *zone)
969 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
970 NF_CT_DEFAULT_ZONE_DIR, 0);
971 #ifdef CONFIG_NF_CONNTRACK_ZONES
973 zone->id = ntohs(nla_get_be16(attr));
982 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
983 struct nf_conntrack_zone *zone)
987 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
990 ret = ctnetlink_parse_zone(attr, zone);
994 if (type == CTA_TUPLE_REPLY)
995 zone->dir = NF_CT_ZONE_DIR_REPL;
997 zone->dir = NF_CT_ZONE_DIR_ORIG;
1002 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1003 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1004 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1005 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1009 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1010 struct nf_conntrack_tuple *tuple, u32 type,
1011 u_int8_t l3num, struct nf_conntrack_zone *zone)
1013 struct nlattr *tb[CTA_TUPLE_MAX+1];
1016 memset(tuple, 0, sizeof(*tuple));
1018 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
1022 if (!tb[CTA_TUPLE_IP])
1025 if (l3num != NFPROTO_IPV4 && l3num != NFPROTO_IPV6)
1027 tuple->src.l3num = l3num;
1029 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1033 if (!tb[CTA_TUPLE_PROTO])
1036 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1040 if (tb[CTA_TUPLE_ZONE]) {
1044 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1050 /* orig and expect tuples get DIR_ORIGINAL */
1051 if (type == CTA_TUPLE_REPLY)
1052 tuple->dst.dir = IP_CT_DIR_REPLY;
1054 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1059 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1060 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1061 .len = NF_CT_HELPER_NAME_LEN - 1 },
1065 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1066 struct nlattr **helpinfo)
1069 struct nlattr *tb[CTA_HELP_MAX+1];
1071 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
1075 if (!tb[CTA_HELP_NAME])
1078 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1080 if (tb[CTA_HELP_INFO])
1081 *helpinfo = tb[CTA_HELP_INFO];
1086 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1087 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1088 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1089 [CTA_STATUS] = { .type = NLA_U32 },
1090 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1091 [CTA_HELP] = { .type = NLA_NESTED },
1092 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1093 [CTA_TIMEOUT] = { .type = NLA_U32 },
1094 [CTA_MARK] = { .type = NLA_U32 },
1095 [CTA_ID] = { .type = NLA_U32 },
1096 [CTA_NAT_DST] = { .type = NLA_NESTED },
1097 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1098 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1099 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1100 [CTA_ZONE] = { .type = NLA_U16 },
1101 [CTA_MARK_MASK] = { .type = NLA_U32 },
1102 [CTA_LABELS] = { .type = NLA_BINARY,
1103 .len = NF_CT_LABELS_MAX_SIZE },
1104 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1105 .len = NF_CT_LABELS_MAX_SIZE },
1108 static int ctnetlink_flush_conntrack(struct net *net,
1109 const struct nlattr * const cda[],
1110 u32 portid, int report)
1112 struct ctnetlink_filter *filter = NULL;
1114 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1115 filter = ctnetlink_alloc_filter(cda);
1117 return PTR_ERR(filter);
1120 nf_ct_iterate_cleanup(net, ctnetlink_filter_match, filter,
1128 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
1129 const struct nlmsghdr *nlh,
1130 const struct nlattr * const cda[])
1132 struct net *net = sock_net(ctnl);
1133 struct nf_conntrack_tuple_hash *h;
1134 struct nf_conntrack_tuple tuple;
1136 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1137 u_int8_t u3 = nfmsg->nfgen_family;
1138 struct nf_conntrack_zone zone;
1141 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1145 if (cda[CTA_TUPLE_ORIG])
1146 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1148 else if (cda[CTA_TUPLE_REPLY])
1149 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1152 return ctnetlink_flush_conntrack(net, cda,
1153 NETLINK_CB(skb).portid,
1160 h = nf_conntrack_find_get(net, &zone, &tuple);
1164 ct = nf_ct_tuplehash_to_ctrack(h);
1167 __be32 id = nla_get_be32(cda[CTA_ID]);
1169 if (id != (__force __be32)nf_ct_get_id(ct)) {
1175 if (del_timer(&ct->timeout))
1176 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1184 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
1185 const struct nlmsghdr *nlh,
1186 const struct nlattr * const cda[])
1188 struct net *net = sock_net(ctnl);
1189 struct nf_conntrack_tuple_hash *h;
1190 struct nf_conntrack_tuple tuple;
1192 struct sk_buff *skb2 = NULL;
1193 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1194 u_int8_t u3 = nfmsg->nfgen_family;
1195 struct nf_conntrack_zone zone;
1198 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1199 struct netlink_dump_control c = {
1200 .dump = ctnetlink_dump_table,
1201 .done = ctnetlink_done,
1204 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1205 struct ctnetlink_filter *filter;
1207 filter = ctnetlink_alloc_filter(cda);
1209 return PTR_ERR(filter);
1213 return netlink_dump_start(ctnl, skb, nlh, &c);
1216 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1220 if (cda[CTA_TUPLE_ORIG])
1221 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1223 else if (cda[CTA_TUPLE_REPLY])
1224 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1232 h = nf_conntrack_find_get(net, &zone, &tuple);
1236 ct = nf_ct_tuplehash_to_ctrack(h);
1239 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1246 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1247 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1253 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1262 /* this avoids a loop in nfnetlink. */
1263 return err == -EAGAIN ? -ENOBUFS : err;
1266 static int ctnetlink_done_list(struct netlink_callback *cb)
1269 nf_ct_put((struct nf_conn *)cb->args[1]);
1274 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1276 struct nf_conn *ct, *last;
1277 struct nf_conntrack_tuple_hash *h;
1278 struct hlist_nulls_node *n;
1279 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1280 u_int8_t l3proto = nfmsg->nfgen_family;
1283 struct hlist_nulls_head *list;
1284 struct net *net = sock_net(skb->sk);
1289 last = (struct nf_conn *)cb->args[1];
1291 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1292 struct ct_pcpu *pcpu;
1294 if (!cpu_possible(cpu))
1297 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1298 spin_lock_bh(&pcpu->lock);
1299 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1301 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1302 ct = nf_ct_tuplehash_to_ctrack(h);
1303 if (l3proto && nf_ct_l3num(ct) != l3proto)
1311 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1313 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1317 if (!atomic_inc_not_zero(&ct->ct_general.use))
1320 cb->args[1] = (unsigned long)ct;
1321 spin_unlock_bh(&pcpu->lock);
1329 spin_unlock_bh(&pcpu->lock);
1340 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1342 return ctnetlink_dump_list(skb, cb, true);
1346 ctnetlink_get_ct_dying(struct sock *ctnl, struct sk_buff *skb,
1347 const struct nlmsghdr *nlh,
1348 const struct nlattr * const cda[])
1350 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1351 struct netlink_dump_control c = {
1352 .dump = ctnetlink_dump_dying,
1353 .done = ctnetlink_done_list,
1355 return netlink_dump_start(ctnl, skb, nlh, &c);
1362 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1364 return ctnetlink_dump_list(skb, cb, false);
1368 ctnetlink_get_ct_unconfirmed(struct sock *ctnl, struct sk_buff *skb,
1369 const struct nlmsghdr *nlh,
1370 const struct nlattr * const cda[])
1372 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1373 struct netlink_dump_control c = {
1374 .dump = ctnetlink_dump_unconfirmed,
1375 .done = ctnetlink_done_list,
1377 return netlink_dump_start(ctnl, skb, nlh, &c);
1383 #ifdef CONFIG_NF_NAT_NEEDED
1385 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1386 enum nf_nat_manip_type manip,
1387 const struct nlattr *attr)
1389 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1392 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1393 if (!parse_nat_setup) {
1394 #ifdef CONFIG_MODULES
1396 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1397 if (request_module("nf-nat") < 0) {
1398 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1402 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1404 if (nfnetlink_parse_nat_setup_hook)
1410 err = parse_nat_setup(ct, manip, attr);
1411 if (err == -EAGAIN) {
1412 #ifdef CONFIG_MODULES
1414 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1415 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1416 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1420 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1431 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1434 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1435 d = ct->status ^ status;
1437 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1441 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1442 /* SEEN_REPLY bit can only be set */
1445 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1446 /* ASSURED bit can only be set */
1449 /* Be careful here, modifying NAT bits can screw up things,
1450 * so don't let users modify them directly if they don't pass
1452 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1457 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1459 #ifdef CONFIG_NF_NAT_NEEDED
1462 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1465 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1470 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1474 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1481 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1483 struct nf_conntrack_helper *helper;
1484 struct nf_conn_help *help = nfct_help(ct);
1485 char *helpname = NULL;
1486 struct nlattr *helpinfo = NULL;
1489 /* don't change helper of sibling connections */
1493 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1497 if (!strcmp(helpname, "")) {
1498 if (help && help->helper) {
1499 /* we had a helper before ... */
1500 nf_ct_remove_expectations(ct);
1501 RCU_INIT_POINTER(help->helper, NULL);
1507 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1508 nf_ct_protonum(ct));
1509 if (helper == NULL) {
1510 #ifdef CONFIG_MODULES
1511 spin_unlock_bh(&nf_conntrack_expect_lock);
1513 if (request_module("nfct-helper-%s", helpname) < 0) {
1514 spin_lock_bh(&nf_conntrack_expect_lock);
1518 spin_lock_bh(&nf_conntrack_expect_lock);
1519 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1520 nf_ct_protonum(ct));
1528 if (help->helper == helper) {
1529 /* update private helper data if allowed. */
1530 if (helper->from_nlattr)
1531 helper->from_nlattr(helpinfo, ct);
1537 /* we cannot set a helper for an existing conntrack */
1542 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1544 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1546 if (!del_timer(&ct->timeout))
1549 ct->timeout.expires = jiffies + timeout * HZ;
1550 add_timer(&ct->timeout);
1555 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1556 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1557 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1558 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1562 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1564 const struct nlattr *attr = cda[CTA_PROTOINFO];
1565 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1566 struct nf_conntrack_l4proto *l4proto;
1569 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1574 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1575 if (l4proto->from_nlattr)
1576 err = l4proto->from_nlattr(tb, ct);
1582 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1583 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1584 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1585 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1589 change_seq_adj(struct nf_ct_seqadj *seq, const struct nlattr * const attr)
1592 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1594 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy);
1598 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1601 seq->correction_pos =
1602 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1604 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1607 seq->offset_before =
1608 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1610 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1614 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1620 ctnetlink_change_seq_adj(struct nf_conn *ct,
1621 const struct nlattr * const cda[])
1623 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1629 if (cda[CTA_SEQ_ADJ_ORIG]) {
1630 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1631 cda[CTA_SEQ_ADJ_ORIG]);
1635 ct->status |= IPS_SEQ_ADJUST;
1638 if (cda[CTA_SEQ_ADJ_REPLY]) {
1639 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1640 cda[CTA_SEQ_ADJ_REPLY]);
1644 ct->status |= IPS_SEQ_ADJUST;
1651 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1653 #ifdef CONFIG_NF_CONNTRACK_LABELS
1654 size_t len = nla_len(cda[CTA_LABELS]);
1655 const void *mask = cda[CTA_LABELS_MASK];
1657 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1661 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1662 nla_len(cda[CTA_LABELS_MASK]) != len)
1664 mask = nla_data(cda[CTA_LABELS_MASK]);
1669 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1676 ctnetlink_change_conntrack(struct nf_conn *ct,
1677 const struct nlattr * const cda[])
1681 /* only allow NAT changes and master assignation for new conntracks */
1682 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1685 if (cda[CTA_HELP]) {
1686 err = ctnetlink_change_helper(ct, cda);
1691 if (cda[CTA_TIMEOUT]) {
1692 err = ctnetlink_change_timeout(ct, cda);
1697 if (cda[CTA_STATUS]) {
1698 err = ctnetlink_change_status(ct, cda);
1703 if (cda[CTA_PROTOINFO]) {
1704 err = ctnetlink_change_protoinfo(ct, cda);
1709 #if defined(CONFIG_NF_CONNTRACK_MARK)
1711 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1714 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1715 err = ctnetlink_change_seq_adj(ct, cda);
1720 if (cda[CTA_LABELS]) {
1721 err = ctnetlink_attach_labels(ct, cda);
1729 static struct nf_conn *
1730 ctnetlink_create_conntrack(struct net *net,
1731 const struct nf_conntrack_zone *zone,
1732 const struct nlattr * const cda[],
1733 struct nf_conntrack_tuple *otuple,
1734 struct nf_conntrack_tuple *rtuple,
1739 struct nf_conntrack_helper *helper;
1740 struct nf_conn_tstamp *tstamp;
1742 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1744 return ERR_PTR(-ENOMEM);
1746 if (!cda[CTA_TIMEOUT])
1748 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1750 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1753 if (cda[CTA_HELP]) {
1754 char *helpname = NULL;
1755 struct nlattr *helpinfo = NULL;
1757 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1761 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1762 nf_ct_protonum(ct));
1763 if (helper == NULL) {
1765 #ifdef CONFIG_MODULES
1766 if (request_module("nfct-helper-%s", helpname) < 0) {
1772 helper = __nf_conntrack_helper_find(helpname,
1774 nf_ct_protonum(ct));
1784 struct nf_conn_help *help;
1786 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1791 /* set private helper data if allowed. */
1792 if (helper->from_nlattr)
1793 helper->from_nlattr(helpinfo, ct);
1795 /* not in hash table yet so not strictly necessary */
1796 RCU_INIT_POINTER(help->helper, helper);
1799 /* try an implicit helper assignation */
1800 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1805 err = ctnetlink_setup_nat(ct, cda);
1809 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1810 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1811 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1812 nf_ct_labels_ext_add(ct);
1813 nfct_seqadj_ext_add(ct);
1814 nfct_synproxy_ext_add(ct);
1816 /* we must add conntrack extensions before confirmation. */
1817 ct->status |= IPS_CONFIRMED;
1819 if (cda[CTA_STATUS]) {
1820 err = ctnetlink_change_status(ct, cda);
1825 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1826 err = ctnetlink_change_seq_adj(ct, cda);
1831 memset(&ct->proto, 0, sizeof(ct->proto));
1832 if (cda[CTA_PROTOINFO]) {
1833 err = ctnetlink_change_protoinfo(ct, cda);
1838 #if defined(CONFIG_NF_CONNTRACK_MARK)
1840 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1843 /* setup master conntrack: this is a confirmed expectation */
1844 if (cda[CTA_TUPLE_MASTER]) {
1845 struct nf_conntrack_tuple master;
1846 struct nf_conntrack_tuple_hash *master_h;
1847 struct nf_conn *master_ct;
1849 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1854 master_h = nf_conntrack_find_get(net, zone, &master);
1855 if (master_h == NULL) {
1859 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1860 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1861 ct->master = master_ct;
1863 tstamp = nf_conn_tstamp_find(ct);
1865 tstamp->start = ktime_get_real_ns();
1867 err = nf_conntrack_hash_check_insert(ct);
1878 nf_conntrack_free(ct);
1879 return ERR_PTR(err);
1883 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1884 const struct nlmsghdr *nlh,
1885 const struct nlattr * const cda[])
1887 struct net *net = sock_net(ctnl);
1888 struct nf_conntrack_tuple otuple, rtuple;
1889 struct nf_conntrack_tuple_hash *h = NULL;
1890 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1892 u_int8_t u3 = nfmsg->nfgen_family;
1893 struct nf_conntrack_zone zone;
1896 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1900 if (cda[CTA_TUPLE_ORIG]) {
1901 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1907 if (cda[CTA_TUPLE_REPLY]) {
1908 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1914 if (cda[CTA_TUPLE_ORIG])
1915 h = nf_conntrack_find_get(net, &zone, &otuple);
1916 else if (cda[CTA_TUPLE_REPLY])
1917 h = nf_conntrack_find_get(net, &zone, &rtuple);
1921 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1922 enum ip_conntrack_events events;
1924 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1927 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1933 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1934 events = IPCT_RELATED;
1938 if (cda[CTA_LABELS] &&
1939 ctnetlink_attach_labels(ct, cda) == 0)
1940 events |= (1 << IPCT_LABEL);
1942 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1943 (1 << IPCT_ASSURED) |
1944 (1 << IPCT_HELPER) |
1945 (1 << IPCT_PROTOINFO) |
1946 (1 << IPCT_SEQADJ) |
1947 (1 << IPCT_MARK) | events,
1948 ct, NETLINK_CB(skb).portid,
1955 /* implicit 'else' */
1958 ct = nf_ct_tuplehash_to_ctrack(h);
1959 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1960 spin_lock_bh(&nf_conntrack_expect_lock);
1961 err = ctnetlink_change_conntrack(ct, cda);
1962 spin_unlock_bh(&nf_conntrack_expect_lock);
1964 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1965 (1 << IPCT_ASSURED) |
1966 (1 << IPCT_HELPER) |
1968 (1 << IPCT_PROTOINFO) |
1969 (1 << IPCT_SEQADJ) |
1971 ct, NETLINK_CB(skb).portid,
1981 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1982 __u16 cpu, const struct ip_conntrack_stat *st)
1984 struct nlmsghdr *nlh;
1985 struct nfgenmsg *nfmsg;
1986 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1988 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1989 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1993 nfmsg = nlmsg_data(nlh);
1994 nfmsg->nfgen_family = AF_UNSPEC;
1995 nfmsg->version = NFNETLINK_V0;
1996 nfmsg->res_id = htons(cpu);
1998 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1999 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2000 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
2001 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2002 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2003 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
2004 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
2005 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2006 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2007 htonl(st->insert_failed)) ||
2008 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2009 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2010 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2011 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2012 htonl(st->search_restart)))
2013 goto nla_put_failure;
2015 nlmsg_end(skb, nlh);
2020 nlmsg_cancel(skb, nlh);
2025 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2028 struct net *net = sock_net(skb->sk);
2030 if (cb->args[0] == nr_cpu_ids)
2033 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2034 const struct ip_conntrack_stat *st;
2036 if (!cpu_possible(cpu))
2039 st = per_cpu_ptr(net->ct.stat, cpu);
2040 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2041 NETLINK_CB(cb->skb).portid,
2052 ctnetlink_stat_ct_cpu(struct sock *ctnl, struct sk_buff *skb,
2053 const struct nlmsghdr *nlh,
2054 const struct nlattr * const cda[])
2056 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2057 struct netlink_dump_control c = {
2058 .dump = ctnetlink_ct_stat_cpu_dump,
2060 return netlink_dump_start(ctnl, skb, nlh, &c);
2067 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2070 struct nlmsghdr *nlh;
2071 struct nfgenmsg *nfmsg;
2072 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2073 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2075 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
2076 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2080 nfmsg = nlmsg_data(nlh);
2081 nfmsg->nfgen_family = AF_UNSPEC;
2082 nfmsg->version = NFNETLINK_V0;
2085 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2086 goto nla_put_failure;
2088 nlmsg_end(skb, nlh);
2093 nlmsg_cancel(skb, nlh);
2098 ctnetlink_stat_ct(struct sock *ctnl, struct sk_buff *skb,
2099 const struct nlmsghdr *nlh,
2100 const struct nlattr * const cda[])
2102 struct sk_buff *skb2;
2105 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2109 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2111 NFNL_MSG_TYPE(nlh->nlmsg_type),
2116 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2125 /* this avoids a loop in nfnetlink. */
2126 return err == -EAGAIN ? -ENOBUFS : err;
2129 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2130 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2131 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2132 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2133 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2134 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2135 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2136 .len = NF_CT_HELPER_NAME_LEN - 1 },
2137 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2138 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2139 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2140 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2141 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2144 static struct nf_conntrack_expect *
2145 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2146 struct nf_conntrack_helper *helper,
2147 struct nf_conntrack_tuple *tuple,
2148 struct nf_conntrack_tuple *mask);
2150 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2152 ctnetlink_glue_build_size(const struct nf_conn *ct)
2154 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2155 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2156 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2157 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2158 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2159 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2160 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2161 + nla_total_size(0) /* CTA_PROTOINFO */
2162 + nla_total_size(0) /* CTA_HELP */
2163 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2164 + ctnetlink_secctx_size(ct)
2165 #ifdef CONFIG_NF_NAT_NEEDED
2166 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2167 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2169 #ifdef CONFIG_NF_CONNTRACK_MARK
2170 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2172 #ifdef CONFIG_NF_CONNTRACK_ZONES
2173 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2175 + ctnetlink_proto_size(ct)
2179 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2180 enum ip_conntrack_info *ctinfo)
2184 ct = nf_ct_get(skb, ctinfo);
2185 if (ct && nf_ct_is_untracked(ct))
2191 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2193 const struct nf_conntrack_zone *zone;
2194 struct nlattr *nest_parms;
2197 zone = nf_ct_zone(ct);
2199 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2201 goto nla_put_failure;
2202 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2203 goto nla_put_failure;
2204 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2205 NF_CT_ZONE_DIR_ORIG) < 0)
2206 goto nla_put_failure;
2207 nla_nest_end(skb, nest_parms);
2209 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2211 goto nla_put_failure;
2212 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2213 goto nla_put_failure;
2214 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2215 NF_CT_ZONE_DIR_REPL) < 0)
2216 goto nla_put_failure;
2217 nla_nest_end(skb, nest_parms);
2219 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2220 NF_CT_DEFAULT_ZONE_DIR) < 0)
2221 goto nla_put_failure;
2223 if (ctnetlink_dump_id(skb, ct) < 0)
2224 goto nla_put_failure;
2226 if (ctnetlink_dump_status(skb, ct) < 0)
2227 goto nla_put_failure;
2229 if (ctnetlink_dump_timeout(skb, ct) < 0)
2230 goto nla_put_failure;
2232 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2233 goto nla_put_failure;
2235 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2236 goto nla_put_failure;
2238 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2239 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2240 goto nla_put_failure;
2242 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2243 goto nla_put_failure;
2245 if ((ct->status & IPS_SEQ_ADJUST) &&
2246 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2247 goto nla_put_failure;
2249 #ifdef CONFIG_NF_CONNTRACK_MARK
2250 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2251 goto nla_put_failure;
2253 if (ctnetlink_dump_labels(skb, ct) < 0)
2254 goto nla_put_failure;
2264 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2265 enum ip_conntrack_info ctinfo,
2266 u_int16_t ct_attr, u_int16_t ct_info_attr)
2268 struct nlattr *nest_parms;
2270 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2272 goto nla_put_failure;
2274 if (__ctnetlink_glue_build(skb, ct) < 0)
2275 goto nla_put_failure;
2277 nla_nest_end(skb, nest_parms);
2279 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2280 goto nla_put_failure;
2289 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2293 if (cda[CTA_TIMEOUT]) {
2294 err = ctnetlink_change_timeout(ct, cda);
2298 if (cda[CTA_STATUS]) {
2299 err = ctnetlink_change_status(ct, cda);
2303 if (cda[CTA_HELP]) {
2304 err = ctnetlink_change_helper(ct, cda);
2308 if (cda[CTA_LABELS]) {
2309 err = ctnetlink_attach_labels(ct, cda);
2313 #if defined(CONFIG_NF_CONNTRACK_MARK)
2314 if (cda[CTA_MARK]) {
2315 u32 mask = 0, mark, newmark;
2316 if (cda[CTA_MARK_MASK])
2317 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2319 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2320 newmark = (ct->mark & mask) ^ mark;
2321 if (newmark != ct->mark)
2329 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2331 struct nlattr *cda[CTA_MAX+1];
2334 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2338 spin_lock_bh(&nf_conntrack_expect_lock);
2339 ret = ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2340 spin_unlock_bh(&nf_conntrack_expect_lock);
2345 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2346 const struct nf_conn *ct,
2347 struct nf_conntrack_tuple *tuple,
2348 struct nf_conntrack_tuple *mask)
2352 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2353 nf_ct_l3num(ct), NULL);
2357 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2358 nf_ct_l3num(ct), NULL);
2362 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2363 u32 portid, u32 report)
2365 struct nlattr *cda[CTA_EXPECT_MAX+1];
2366 struct nf_conntrack_tuple tuple, mask;
2367 struct nf_conntrack_helper *helper = NULL;
2368 struct nf_conntrack_expect *exp;
2371 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy);
2375 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2380 if (cda[CTA_EXPECT_HELP_NAME]) {
2381 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2383 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2384 nf_ct_protonum(ct));
2389 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2390 helper, &tuple, &mask);
2392 return PTR_ERR(exp);
2394 err = nf_ct_expect_related_report(exp, portid, report);
2396 nf_ct_expect_put(exp);
2403 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2404 enum ip_conntrack_info ctinfo, int diff)
2406 if (!(ct->status & IPS_NAT_MASK))
2409 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2412 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2413 .get_ct = ctnetlink_glue_get_ct,
2414 .build_size = ctnetlink_glue_build_size,
2415 .build = ctnetlink_glue_build,
2416 .parse = ctnetlink_glue_parse,
2417 .attach_expect = ctnetlink_glue_attach_expect,
2418 .seq_adjust = ctnetlink_glue_seqadj,
2420 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2422 /***********************************************************************
2424 ***********************************************************************/
2427 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2428 const struct nf_conntrack_tuple *tuple,
2431 struct nlattr *nest_parms;
2433 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2435 goto nla_put_failure;
2436 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2437 goto nla_put_failure;
2438 nla_nest_end(skb, nest_parms);
2447 ctnetlink_exp_dump_mask(struct sk_buff *skb,
2448 const struct nf_conntrack_tuple *tuple,
2449 const struct nf_conntrack_tuple_mask *mask)
2452 struct nf_conntrack_l3proto *l3proto;
2453 struct nf_conntrack_l4proto *l4proto;
2454 struct nf_conntrack_tuple m;
2455 struct nlattr *nest_parms;
2457 memset(&m, 0xFF, sizeof(m));
2458 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2459 m.src.u.all = mask->src.u.all;
2460 m.dst.protonum = tuple->dst.protonum;
2462 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2464 goto nla_put_failure;
2467 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2468 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2470 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2471 tuple->dst.protonum);
2472 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2476 if (unlikely(ret < 0))
2477 goto nla_put_failure;
2479 nla_nest_end(skb, nest_parms);
2487 static const union nf_inet_addr any_addr;
2489 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2491 static __read_mostly siphash_key_t exp_id_seed;
2492 unsigned long a, b, c, d;
2494 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2496 a = (unsigned long)exp;
2497 b = (unsigned long)exp->helper;
2498 c = (unsigned long)exp->master;
2499 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2502 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2504 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2509 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2510 const struct nf_conntrack_expect *exp)
2512 struct nf_conn *master = exp->master;
2513 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2514 struct nf_conn_help *help;
2515 #ifdef CONFIG_NF_NAT_NEEDED
2516 struct nlattr *nest_parms;
2517 struct nf_conntrack_tuple nat_tuple = {};
2519 struct nf_ct_helper_expectfn *expfn;
2524 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2525 goto nla_put_failure;
2526 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2527 goto nla_put_failure;
2528 if (ctnetlink_exp_dump_tuple(skb,
2529 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2530 CTA_EXPECT_MASTER) < 0)
2531 goto nla_put_failure;
2533 #ifdef CONFIG_NF_NAT_NEEDED
2534 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2535 exp->saved_proto.all) {
2536 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2538 goto nla_put_failure;
2540 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2541 goto nla_put_failure;
2543 nat_tuple.src.l3num = nf_ct_l3num(master);
2544 nat_tuple.src.u3 = exp->saved_addr;
2545 nat_tuple.dst.protonum = nf_ct_protonum(master);
2546 nat_tuple.src.u = exp->saved_proto;
2548 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2549 CTA_EXPECT_NAT_TUPLE) < 0)
2550 goto nla_put_failure;
2551 nla_nest_end(skb, nest_parms);
2554 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2555 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
2556 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2557 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2558 goto nla_put_failure;
2559 help = nfct_help(master);
2561 struct nf_conntrack_helper *helper;
2563 helper = rcu_dereference(help->helper);
2565 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2566 goto nla_put_failure;
2568 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2569 if (expfn != NULL &&
2570 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2571 goto nla_put_failure;
2580 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2581 int event, const struct nf_conntrack_expect *exp)
2583 struct nlmsghdr *nlh;
2584 struct nfgenmsg *nfmsg;
2585 unsigned int flags = portid ? NLM_F_MULTI : 0;
2587 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2588 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2592 nfmsg = nlmsg_data(nlh);
2593 nfmsg->nfgen_family = exp->tuple.src.l3num;
2594 nfmsg->version = NFNETLINK_V0;
2597 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2598 goto nla_put_failure;
2600 nlmsg_end(skb, nlh);
2605 nlmsg_cancel(skb, nlh);
2609 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2611 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2613 struct nf_conntrack_expect *exp = item->exp;
2614 struct net *net = nf_ct_exp_net(exp);
2615 struct nlmsghdr *nlh;
2616 struct nfgenmsg *nfmsg;
2617 struct sk_buff *skb;
2618 unsigned int type, group;
2621 if (events & (1 << IPEXP_DESTROY)) {
2622 type = IPCTNL_MSG_EXP_DELETE;
2623 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2624 } else if (events & (1 << IPEXP_NEW)) {
2625 type = IPCTNL_MSG_EXP_NEW;
2626 flags = NLM_F_CREATE|NLM_F_EXCL;
2627 group = NFNLGRP_CONNTRACK_EXP_NEW;
2631 if (!item->report && !nfnetlink_has_listeners(net, group))
2634 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2638 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2639 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2643 nfmsg = nlmsg_data(nlh);
2644 nfmsg->nfgen_family = exp->tuple.src.l3num;
2645 nfmsg->version = NFNETLINK_V0;
2649 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2650 goto nla_put_failure;
2653 nlmsg_end(skb, nlh);
2654 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2659 nlmsg_cancel(skb, nlh);
2663 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2667 static int ctnetlink_exp_done(struct netlink_callback *cb)
2670 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2675 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2677 struct net *net = sock_net(skb->sk);
2678 struct nf_conntrack_expect *exp, *last;
2679 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2680 u_int8_t l3proto = nfmsg->nfgen_family;
2683 last = (struct nf_conntrack_expect *)cb->args[1];
2684 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2686 hlist_for_each_entry(exp, &net->ct.expect_hash[cb->args[0]],
2688 if (l3proto && exp->tuple.src.l3num != l3proto)
2695 if (ctnetlink_exp_fill_info(skb,
2696 NETLINK_CB(cb->skb).portid,
2700 if (!atomic_inc_not_zero(&exp->use))
2702 cb->args[1] = (unsigned long)exp;
2714 nf_ct_expect_put(last);
2720 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2722 struct nf_conntrack_expect *exp, *last;
2723 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2724 struct nf_conn *ct = cb->data;
2725 struct nf_conn_help *help = nfct_help(ct);
2726 u_int8_t l3proto = nfmsg->nfgen_family;
2732 last = (struct nf_conntrack_expect *)cb->args[1];
2734 hlist_for_each_entry(exp, &help->expectations, lnode) {
2735 if (l3proto && exp->tuple.src.l3num != l3proto)
2742 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2746 if (!atomic_inc_not_zero(&exp->use))
2748 cb->args[1] = (unsigned long)exp;
2760 nf_ct_expect_put(last);
2765 static int ctnetlink_dump_exp_ct(struct sock *ctnl, struct sk_buff *skb,
2766 const struct nlmsghdr *nlh,
2767 const struct nlattr * const cda[])
2770 struct net *net = sock_net(ctnl);
2771 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2772 u_int8_t u3 = nfmsg->nfgen_family;
2773 struct nf_conntrack_tuple tuple;
2774 struct nf_conntrack_tuple_hash *h;
2776 struct nf_conntrack_zone zone;
2777 struct netlink_dump_control c = {
2778 .dump = ctnetlink_exp_ct_dump_table,
2779 .done = ctnetlink_exp_done,
2782 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2787 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2791 h = nf_conntrack_find_get(net, &zone, &tuple);
2795 ct = nf_ct_tuplehash_to_ctrack(h);
2798 err = netlink_dump_start(ctnl, skb, nlh, &c);
2805 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
2806 const struct nlmsghdr *nlh,
2807 const struct nlattr * const cda[])
2809 struct net *net = sock_net(ctnl);
2810 struct nf_conntrack_tuple tuple;
2811 struct nf_conntrack_expect *exp;
2812 struct sk_buff *skb2;
2813 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2814 u_int8_t u3 = nfmsg->nfgen_family;
2815 struct nf_conntrack_zone zone;
2818 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2819 if (cda[CTA_EXPECT_MASTER])
2820 return ctnetlink_dump_exp_ct(ctnl, skb, nlh, cda);
2822 struct netlink_dump_control c = {
2823 .dump = ctnetlink_exp_dump_table,
2824 .done = ctnetlink_exp_done,
2826 return netlink_dump_start(ctnl, skb, nlh, &c);
2830 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2834 if (cda[CTA_EXPECT_TUPLE])
2835 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2837 else if (cda[CTA_EXPECT_MASTER])
2838 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2846 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2850 if (cda[CTA_EXPECT_ID]) {
2851 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2853 if (id != nf_expect_get_id(exp)) {
2854 nf_ct_expect_put(exp);
2860 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2862 nf_ct_expect_put(exp);
2867 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2868 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2870 nf_ct_expect_put(exp);
2874 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2883 /* this avoids a loop in nfnetlink. */
2884 return err == -EAGAIN ? -ENOBUFS : err;
2888 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
2889 const struct nlmsghdr *nlh,
2890 const struct nlattr * const cda[])
2892 struct net *net = sock_net(ctnl);
2893 struct nf_conntrack_expect *exp;
2894 struct nf_conntrack_tuple tuple;
2895 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2896 struct hlist_node *next;
2897 u_int8_t u3 = nfmsg->nfgen_family;
2898 struct nf_conntrack_zone zone;
2902 if (cda[CTA_EXPECT_TUPLE]) {
2903 /* delete a single expect by tuple */
2904 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2908 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2913 /* bump usage count to 2 */
2914 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2918 if (cda[CTA_EXPECT_ID]) {
2919 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2920 if (ntohl(id) != (u32)(unsigned long)exp) {
2921 nf_ct_expect_put(exp);
2926 /* after list removal, usage count == 1 */
2927 spin_lock_bh(&nf_conntrack_expect_lock);
2928 if (del_timer(&exp->timeout)) {
2929 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2931 nf_ct_expect_put(exp);
2933 spin_unlock_bh(&nf_conntrack_expect_lock);
2934 /* have to put what we 'get' above.
2935 * after this line usage count == 0 */
2936 nf_ct_expect_put(exp);
2937 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2938 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2939 struct nf_conn_help *m_help;
2941 /* delete all expectations for this helper */
2942 spin_lock_bh(&nf_conntrack_expect_lock);
2943 for (i = 0; i < nf_ct_expect_hsize; i++) {
2944 hlist_for_each_entry_safe(exp, next,
2945 &net->ct.expect_hash[i],
2947 m_help = nfct_help(exp->master);
2948 if (!strcmp(m_help->helper->name, name) &&
2949 del_timer(&exp->timeout)) {
2950 nf_ct_unlink_expect_report(exp,
2951 NETLINK_CB(skb).portid,
2953 nf_ct_expect_put(exp);
2957 spin_unlock_bh(&nf_conntrack_expect_lock);
2959 /* This basically means we have to flush everything*/
2960 spin_lock_bh(&nf_conntrack_expect_lock);
2961 for (i = 0; i < nf_ct_expect_hsize; i++) {
2962 hlist_for_each_entry_safe(exp, next,
2963 &net->ct.expect_hash[i],
2965 if (del_timer(&exp->timeout)) {
2966 nf_ct_unlink_expect_report(exp,
2967 NETLINK_CB(skb).portid,
2969 nf_ct_expect_put(exp);
2973 spin_unlock_bh(&nf_conntrack_expect_lock);
2979 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2980 const struct nlattr * const cda[])
2982 if (cda[CTA_EXPECT_TIMEOUT]) {
2983 if (!del_timer(&x->timeout))
2986 x->timeout.expires = jiffies +
2987 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2988 add_timer(&x->timeout);
2993 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2994 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2995 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2999 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3000 struct nf_conntrack_expect *exp,
3003 #ifdef CONFIG_NF_NAT_NEEDED
3004 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3005 struct nf_conntrack_tuple nat_tuple = {};
3008 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
3012 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3015 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3016 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3021 exp->saved_addr = nat_tuple.src.u3;
3022 exp->saved_proto = nat_tuple.src.u;
3023 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3031 static struct nf_conntrack_expect *
3032 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3033 struct nf_conntrack_helper *helper,
3034 struct nf_conntrack_tuple *tuple,
3035 struct nf_conntrack_tuple *mask)
3037 u_int32_t class = 0;
3038 struct nf_conntrack_expect *exp;
3039 struct nf_conn_help *help;
3042 if (cda[CTA_EXPECT_CLASS] && helper) {
3043 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3044 if (class > helper->expect_class_max)
3045 return ERR_PTR(-EINVAL);
3047 exp = nf_ct_expect_alloc(ct);
3049 return ERR_PTR(-ENOMEM);
3051 help = nfct_help(ct);
3053 if (!cda[CTA_EXPECT_TIMEOUT]) {
3057 exp->timeout.expires =
3058 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3060 exp->flags = NF_CT_EXPECT_USERSPACE;
3061 if (cda[CTA_EXPECT_FLAGS]) {
3063 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3066 if (cda[CTA_EXPECT_FLAGS]) {
3067 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3068 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3072 if (cda[CTA_EXPECT_FN]) {
3073 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3074 struct nf_ct_helper_expectfn *expfn;
3076 expfn = nf_ct_helper_expectfn_find_by_name(name);
3077 if (expfn == NULL) {
3081 exp->expectfn = expfn->expectfn;
3083 exp->expectfn = NULL;
3087 exp->helper = helper;
3088 exp->tuple = *tuple;
3089 exp->mask.src.u3 = mask->src.u3;
3090 exp->mask.src.u.all = mask->src.u.all;
3092 if (cda[CTA_EXPECT_NAT]) {
3093 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3094 exp, nf_ct_l3num(ct));
3100 nf_ct_expect_put(exp);
3101 return ERR_PTR(err);
3105 ctnetlink_create_expect(struct net *net,
3106 const struct nf_conntrack_zone *zone,
3107 const struct nlattr * const cda[],
3108 u_int8_t u3, u32 portid, int report)
3110 struct nf_conntrack_tuple tuple, mask, master_tuple;
3111 struct nf_conntrack_tuple_hash *h = NULL;
3112 struct nf_conntrack_helper *helper = NULL;
3113 struct nf_conntrack_expect *exp;
3117 /* caller guarantees that those three CTA_EXPECT_* exist */
3118 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3122 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3126 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3131 /* Look for master conntrack of this expectation */
3132 h = nf_conntrack_find_get(net, zone, &master_tuple);
3135 ct = nf_ct_tuplehash_to_ctrack(h);
3137 if (cda[CTA_EXPECT_HELP_NAME]) {
3138 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3140 helper = __nf_conntrack_helper_find(helpname, u3,
3141 nf_ct_protonum(ct));
3142 if (helper == NULL) {
3143 #ifdef CONFIG_MODULES
3144 if (request_module("nfct-helper-%s", helpname) < 0) {
3148 helper = __nf_conntrack_helper_find(helpname, u3,
3149 nf_ct_protonum(ct));
3160 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3166 err = nf_ct_expect_related_report(exp, portid, report);
3167 nf_ct_expect_put(exp);
3174 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
3175 const struct nlmsghdr *nlh,
3176 const struct nlattr * const cda[])
3178 struct net *net = sock_net(ctnl);
3179 struct nf_conntrack_tuple tuple;
3180 struct nf_conntrack_expect *exp;
3181 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3182 u_int8_t u3 = nfmsg->nfgen_family;
3183 struct nf_conntrack_zone zone;
3186 if (!cda[CTA_EXPECT_TUPLE]
3187 || !cda[CTA_EXPECT_MASK]
3188 || !cda[CTA_EXPECT_MASTER])
3191 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3195 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3200 spin_lock_bh(&nf_conntrack_expect_lock);
3201 exp = __nf_ct_expect_find(net, &zone, &tuple);
3203 spin_unlock_bh(&nf_conntrack_expect_lock);
3205 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3206 err = ctnetlink_create_expect(net, &zone, cda, u3,
3207 NETLINK_CB(skb).portid,
3214 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3215 err = ctnetlink_change_expect(exp, cda);
3216 spin_unlock_bh(&nf_conntrack_expect_lock);
3222 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3223 const struct ip_conntrack_stat *st)
3225 struct nlmsghdr *nlh;
3226 struct nfgenmsg *nfmsg;
3227 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3229 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
3230 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3234 nfmsg = nlmsg_data(nlh);
3235 nfmsg->nfgen_family = AF_UNSPEC;
3236 nfmsg->version = NFNETLINK_V0;
3237 nfmsg->res_id = htons(cpu);
3239 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3240 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3241 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3242 goto nla_put_failure;
3244 nlmsg_end(skb, nlh);
3249 nlmsg_cancel(skb, nlh);
3254 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3257 struct net *net = sock_net(skb->sk);
3259 if (cb->args[0] == nr_cpu_ids)
3262 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3263 const struct ip_conntrack_stat *st;
3265 if (!cpu_possible(cpu))
3268 st = per_cpu_ptr(net->ct.stat, cpu);
3269 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3280 ctnetlink_stat_exp_cpu(struct sock *ctnl, struct sk_buff *skb,
3281 const struct nlmsghdr *nlh,
3282 const struct nlattr * const cda[])
3284 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3285 struct netlink_dump_control c = {
3286 .dump = ctnetlink_exp_stat_cpu_dump,
3288 return netlink_dump_start(ctnl, skb, nlh, &c);
3294 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3295 static struct nf_ct_event_notifier ctnl_notifier = {
3296 .fcn = ctnetlink_conntrack_event,
3299 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3300 .fcn = ctnetlink_expect_event,
3304 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3305 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3306 .attr_count = CTA_MAX,
3307 .policy = ct_nla_policy },
3308 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3309 .attr_count = CTA_MAX,
3310 .policy = ct_nla_policy },
3311 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3312 .attr_count = CTA_MAX,
3313 .policy = ct_nla_policy },
3314 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3315 .attr_count = CTA_MAX,
3316 .policy = ct_nla_policy },
3317 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3318 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3319 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3320 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3323 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3324 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3325 .attr_count = CTA_EXPECT_MAX,
3326 .policy = exp_nla_policy },
3327 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3328 .attr_count = CTA_EXPECT_MAX,
3329 .policy = exp_nla_policy },
3330 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3331 .attr_count = CTA_EXPECT_MAX,
3332 .policy = exp_nla_policy },
3333 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3336 static const struct nfnetlink_subsystem ctnl_subsys = {
3337 .name = "conntrack",
3338 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3339 .cb_count = IPCTNL_MSG_MAX,
3343 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3344 .name = "conntrack_expect",
3345 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3346 .cb_count = IPCTNL_MSG_EXP_MAX,
3350 MODULE_ALIAS("ip_conntrack_netlink");
3351 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3352 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3354 static int __net_init ctnetlink_net_init(struct net *net)
3356 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3359 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3361 pr_err("ctnetlink_init: cannot register notifier.\n");
3365 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3367 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3368 goto err_unreg_notifier;
3373 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3375 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3381 static void ctnetlink_net_exit(struct net *net)
3383 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3384 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3385 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3389 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3393 list_for_each_entry(net, net_exit_list, exit_list)
3394 ctnetlink_net_exit(net);
3396 /* wait for other cpus until they are done with ctnl_notifiers */
3400 static struct pernet_operations ctnetlink_net_ops = {
3401 .init = ctnetlink_net_init,
3402 .exit_batch = ctnetlink_net_exit_batch,
3405 static int __init ctnetlink_init(void)
3409 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3410 ret = nfnetlink_subsys_register(&ctnl_subsys);
3412 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3416 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3418 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3419 goto err_unreg_subsys;
3422 ret = register_pernet_subsys(&ctnetlink_net_ops);
3424 pr_err("ctnetlink_init: cannot register pernet operations\n");
3425 goto err_unreg_exp_subsys;
3427 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3428 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3429 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3433 err_unreg_exp_subsys:
3434 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3436 nfnetlink_subsys_unregister(&ctnl_subsys);
3441 static void __exit ctnetlink_exit(void)
3443 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3445 unregister_pernet_subsys(&ctnetlink_net_ops);
3446 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3447 nfnetlink_subsys_unregister(&ctnl_subsys);
3448 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3449 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3454 module_init(ctnetlink_init);
3455 module_exit(ctnetlink_exit);