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_seqadj.h>
41 #include <net/netfilter/nf_conntrack_l3proto.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #include <net/netfilter/nf_conntrack_acct.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_labels.h>
48 #include <net/netfilter/nf_conntrack_synproxy.h>
49 #ifdef CONFIG_NF_NAT_NEEDED
50 #include <net/netfilter/nf_nat_core.h>
51 #include <net/netfilter/nf_nat_l4proto.h>
52 #include <net/netfilter/nf_nat_helper.h>
55 #include <linux/netfilter/nfnetlink.h>
56 #include <linux/netfilter/nfnetlink_conntrack.h>
58 MODULE_LICENSE("GPL");
60 static char __initdata version[] = "0.93";
62 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
63 const struct nf_conntrack_tuple *tuple,
64 const struct nf_conntrack_l4proto *l4proto)
67 struct nlattr *nest_parms;
69 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
72 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
75 if (likely(l4proto->tuple_to_nlattr))
76 ret = l4proto->tuple_to_nlattr(skb, tuple);
78 nla_nest_end(skb, nest_parms);
86 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
87 const struct nf_conntrack_tuple *tuple,
88 const 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);
108 static int ctnetlink_dump_tuples(struct sk_buff *skb,
109 const struct nf_conntrack_tuple *tuple)
111 const struct nf_conntrack_l3proto *l3proto;
112 const struct nf_conntrack_l4proto *l4proto;
116 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
117 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
120 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
121 tuple->dst.protonum);
122 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
128 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
129 const struct nf_conntrack_zone *zone, int dir)
131 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
133 if (nla_put_be16(skb, attrtype, htons(zone->id)))
134 goto nla_put_failure;
141 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
143 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
144 goto nla_put_failure;
151 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
153 long timeout = nf_ct_expires(ct) / HZ;
155 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
156 goto nla_put_failure;
163 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
165 const struct nf_conntrack_l4proto *l4proto;
166 struct nlattr *nest_proto;
169 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
170 if (!l4proto->to_nlattr)
173 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
175 goto nla_put_failure;
177 ret = l4proto->to_nlattr(skb, nest_proto, ct);
179 nla_nest_end(skb, nest_proto);
187 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
188 const struct nf_conn *ct)
190 struct nlattr *nest_helper;
191 const struct nf_conn_help *help = nfct_help(ct);
192 struct nf_conntrack_helper *helper;
197 helper = rcu_dereference(help->helper);
201 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
203 goto nla_put_failure;
204 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
205 goto nla_put_failure;
207 if (helper->to_nlattr)
208 helper->to_nlattr(skb, ct);
210 nla_nest_end(skb, nest_helper);
219 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
220 enum ip_conntrack_dir dir, int type)
222 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
223 struct nf_conn_counter *counter = acct->counter;
224 struct nlattr *nest_count;
227 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
228 pkts = atomic64_xchg(&counter[dir].packets, 0);
229 bytes = atomic64_xchg(&counter[dir].bytes, 0);
231 pkts = atomic64_read(&counter[dir].packets);
232 bytes = atomic64_read(&counter[dir].bytes);
235 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
237 goto nla_put_failure;
239 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
241 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
243 goto nla_put_failure;
245 nla_nest_end(skb, nest_count);
254 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
256 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
261 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
263 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
270 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
272 struct nlattr *nest_count;
273 const struct nf_conn_tstamp *tstamp;
275 tstamp = nf_conn_tstamp_find(ct);
279 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
281 goto nla_put_failure;
283 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
284 CTA_TIMESTAMP_PAD) ||
285 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
286 cpu_to_be64(tstamp->stop),
288 goto nla_put_failure;
289 nla_nest_end(skb, nest_count);
297 #ifdef CONFIG_NF_CONNTRACK_MARK
298 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
300 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
301 goto nla_put_failure;
308 #define ctnetlink_dump_mark(a, b) (0)
311 #ifdef CONFIG_NF_CONNTRACK_SECMARK
312 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
314 struct nlattr *nest_secctx;
318 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
323 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
325 goto nla_put_failure;
327 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
328 goto nla_put_failure;
329 nla_nest_end(skb, nest_secctx);
333 security_release_secctx(secctx, len);
337 #define ctnetlink_dump_secctx(a, b) (0)
340 #ifdef CONFIG_NF_CONNTRACK_LABELS
341 static inline int ctnetlink_label_size(const struct nf_conn *ct)
343 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
347 return nla_total_size(sizeof(labels->bits));
351 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
353 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
361 if (labels->bits[i] != 0)
362 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
365 } while (i < ARRAY_SIZE(labels->bits));
370 #define ctnetlink_dump_labels(a, b) (0)
371 #define ctnetlink_label_size(a) (0)
374 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
376 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
378 struct nlattr *nest_parms;
380 if (!(ct->status & IPS_EXPECTED))
383 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
385 goto nla_put_failure;
386 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
387 goto nla_put_failure;
388 nla_nest_end(skb, nest_parms);
397 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
399 struct nlattr *nest_parms;
401 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
403 goto nla_put_failure;
405 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
406 htonl(seq->correction_pos)) ||
407 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
408 htonl(seq->offset_before)) ||
409 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
410 htonl(seq->offset_after)))
411 goto nla_put_failure;
413 nla_nest_end(skb, nest_parms);
421 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
423 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
424 struct nf_ct_seqadj *seq;
426 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
429 spin_lock_bh(&ct->lock);
430 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
431 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
434 seq = &seqadj->seq[IP_CT_DIR_REPLY];
435 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
438 spin_unlock_bh(&ct->lock);
441 spin_unlock_bh(&ct->lock);
445 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
447 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
448 goto nla_put_failure;
455 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
457 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
458 goto nla_put_failure;
466 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
469 const struct nf_conntrack_zone *zone;
470 struct nlmsghdr *nlh;
471 struct nfgenmsg *nfmsg;
472 struct nlattr *nest_parms;
473 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
475 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
476 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
480 nfmsg = nlmsg_data(nlh);
481 nfmsg->nfgen_family = nf_ct_l3num(ct);
482 nfmsg->version = NFNETLINK_V0;
485 zone = nf_ct_zone(ct);
487 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
489 goto nla_put_failure;
490 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
491 goto nla_put_failure;
492 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
493 NF_CT_ZONE_DIR_ORIG) < 0)
494 goto nla_put_failure;
495 nla_nest_end(skb, nest_parms);
497 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
499 goto nla_put_failure;
500 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
501 goto nla_put_failure;
502 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
503 NF_CT_ZONE_DIR_REPL) < 0)
504 goto nla_put_failure;
505 nla_nest_end(skb, nest_parms);
507 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
508 NF_CT_DEFAULT_ZONE_DIR) < 0)
509 goto nla_put_failure;
511 if (ctnetlink_dump_status(skb, ct) < 0 ||
512 ctnetlink_dump_timeout(skb, ct) < 0 ||
513 ctnetlink_dump_acct(skb, ct, type) < 0 ||
514 ctnetlink_dump_timestamp(skb, ct) < 0 ||
515 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
516 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
517 ctnetlink_dump_mark(skb, ct) < 0 ||
518 ctnetlink_dump_secctx(skb, ct) < 0 ||
519 ctnetlink_dump_labels(skb, ct) < 0 ||
520 ctnetlink_dump_id(skb, ct) < 0 ||
521 ctnetlink_dump_use(skb, ct) < 0 ||
522 ctnetlink_dump_master(skb, ct) < 0 ||
523 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
524 goto nla_put_failure;
531 nlmsg_cancel(skb, nlh);
535 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
536 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
538 const struct nf_conntrack_l3proto *l3proto;
539 const struct nf_conntrack_l4proto *l4proto;
540 size_t len, len4 = 0;
542 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
543 len = l3proto->nla_size;
544 len *= 3u; /* ORIG, REPLY, MASTER */
546 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
547 len += l4proto->nla_size;
548 if (l4proto->nlattr_tuple_size) {
549 len4 = l4proto->nlattr_tuple_size();
550 len4 *= 3u; /* ORIG, REPLY, MASTER */
557 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
559 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
561 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
562 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
563 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
567 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
569 #ifdef CONFIG_NF_CONNTRACK_SECMARK
572 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
576 return nla_total_size(0) /* CTA_SECCTX */
577 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
583 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
585 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
586 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
588 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
594 #ifdef CONFIG_NF_CONNTRACK_EVENTS
595 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
597 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
598 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
599 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
600 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
601 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
602 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
603 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
604 + ctnetlink_acct_size(ct)
605 + ctnetlink_timestamp_size(ct)
606 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
607 + nla_total_size(0) /* CTA_PROTOINFO */
608 + nla_total_size(0) /* CTA_HELP */
609 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
610 + ctnetlink_secctx_size(ct)
611 #ifdef CONFIG_NF_NAT_NEEDED
612 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
613 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
615 #ifdef CONFIG_NF_CONNTRACK_MARK
616 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
618 #ifdef CONFIG_NF_CONNTRACK_ZONES
619 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
621 + ctnetlink_proto_size(ct)
622 + ctnetlink_label_size(ct)
627 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
629 const struct nf_conntrack_zone *zone;
631 struct nlmsghdr *nlh;
632 struct nfgenmsg *nfmsg;
633 struct nlattr *nest_parms;
634 struct nf_conn *ct = item->ct;
637 unsigned int flags = 0, group;
640 if (events & (1 << IPCT_DESTROY)) {
641 type = IPCTNL_MSG_CT_DELETE;
642 group = NFNLGRP_CONNTRACK_DESTROY;
643 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
644 type = IPCTNL_MSG_CT_NEW;
645 flags = NLM_F_CREATE|NLM_F_EXCL;
646 group = NFNLGRP_CONNTRACK_NEW;
648 type = IPCTNL_MSG_CT_NEW;
649 group = NFNLGRP_CONNTRACK_UPDATE;
654 if (!item->report && !nfnetlink_has_listeners(net, group))
657 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
661 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
662 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
666 nfmsg = nlmsg_data(nlh);
667 nfmsg->nfgen_family = nf_ct_l3num(ct);
668 nfmsg->version = NFNETLINK_V0;
671 zone = nf_ct_zone(ct);
673 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
675 goto nla_put_failure;
676 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
677 goto nla_put_failure;
678 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
679 NF_CT_ZONE_DIR_ORIG) < 0)
680 goto nla_put_failure;
681 nla_nest_end(skb, nest_parms);
683 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
685 goto nla_put_failure;
686 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
687 goto nla_put_failure;
688 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
689 NF_CT_ZONE_DIR_REPL) < 0)
690 goto nla_put_failure;
691 nla_nest_end(skb, nest_parms);
693 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
694 NF_CT_DEFAULT_ZONE_DIR) < 0)
695 goto nla_put_failure;
697 if (ctnetlink_dump_id(skb, ct) < 0)
698 goto nla_put_failure;
700 if (ctnetlink_dump_status(skb, ct) < 0)
701 goto nla_put_failure;
703 if (events & (1 << IPCT_DESTROY)) {
704 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
705 ctnetlink_dump_timestamp(skb, ct) < 0)
706 goto nla_put_failure;
708 if (ctnetlink_dump_timeout(skb, ct) < 0)
709 goto nla_put_failure;
711 if (events & (1 << IPCT_PROTOINFO)
712 && ctnetlink_dump_protoinfo(skb, ct) < 0)
713 goto nla_put_failure;
715 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
716 && ctnetlink_dump_helpinfo(skb, ct) < 0)
717 goto nla_put_failure;
719 #ifdef CONFIG_NF_CONNTRACK_SECMARK
720 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
721 && ctnetlink_dump_secctx(skb, ct) < 0)
722 goto nla_put_failure;
724 if (events & (1 << IPCT_LABEL) &&
725 ctnetlink_dump_labels(skb, ct) < 0)
726 goto nla_put_failure;
728 if (events & (1 << IPCT_RELATED) &&
729 ctnetlink_dump_master(skb, ct) < 0)
730 goto nla_put_failure;
732 if (events & (1 << IPCT_SEQADJ) &&
733 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
734 goto nla_put_failure;
737 #ifdef CONFIG_NF_CONNTRACK_MARK
738 if ((events & (1 << IPCT_MARK) || ct->mark)
739 && ctnetlink_dump_mark(skb, ct) < 0)
740 goto nla_put_failure;
743 err = nfnetlink_send(skb, net, item->portid, group, item->report,
745 if (err == -ENOBUFS || err == -EAGAIN)
751 nlmsg_cancel(skb, nlh);
755 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
760 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
762 static int ctnetlink_done(struct netlink_callback *cb)
765 nf_ct_put((struct nf_conn *)cb->args[1]);
770 struct ctnetlink_filter {
777 static struct ctnetlink_filter *
778 ctnetlink_alloc_filter(const struct nlattr * const cda[])
780 #ifdef CONFIG_NF_CONNTRACK_MARK
781 struct ctnetlink_filter *filter;
783 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
785 return ERR_PTR(-ENOMEM);
787 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
788 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
792 return ERR_PTR(-EOPNOTSUPP);
796 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
798 struct ctnetlink_filter *filter = data;
803 #ifdef CONFIG_NF_CONNTRACK_MARK
804 if ((ct->mark & filter->mark.mask) == filter->mark.val)
812 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
814 struct net *net = sock_net(skb->sk);
815 struct nf_conn *ct, *last;
816 struct nf_conntrack_tuple_hash *h;
817 struct hlist_nulls_node *n;
818 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
819 u_int8_t l3proto = nfmsg->nfgen_family;
820 struct nf_conn *nf_ct_evict[8];
824 last = (struct nf_conn *)cb->args[1];
828 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
832 if (nf_ct_should_gc(nf_ct_evict[i]))
833 nf_ct_kill(nf_ct_evict[i]);
834 nf_ct_put(nf_ct_evict[i]);
837 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
838 nf_conntrack_lock(lockp);
839 if (cb->args[0] >= nf_conntrack_htable_size) {
843 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
845 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
847 ct = nf_ct_tuplehash_to_ctrack(h);
848 if (nf_ct_is_expired(ct)) {
849 if (i < ARRAY_SIZE(nf_ct_evict) &&
850 atomic_inc_not_zero(&ct->ct_general.use))
851 nf_ct_evict[i++] = ct;
855 if (!net_eq(net, nf_ct_net(ct)))
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)
903 if (nf_ct_should_gc(nf_ct_evict[i]))
904 nf_ct_kill(nf_ct_evict[i]);
905 nf_ct_put(nf_ct_evict[i]);
911 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
912 struct nf_conntrack_tuple *tuple)
914 struct nlattr *tb[CTA_IP_MAX+1];
915 struct nf_conntrack_l3proto *l3proto;
918 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
923 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
925 if (likely(l3proto->nlattr_to_tuple)) {
926 ret = nla_validate_nested(attr, CTA_IP_MAX,
927 l3proto->nla_policy, NULL);
929 ret = l3proto->nlattr_to_tuple(tb, tuple);
937 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
938 [CTA_PROTO_NUM] = { .type = NLA_U8 },
941 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
942 struct nf_conntrack_tuple *tuple)
944 const struct nf_conntrack_l4proto *l4proto;
945 struct nlattr *tb[CTA_PROTO_MAX+1];
948 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
953 if (!tb[CTA_PROTO_NUM])
955 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
958 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
960 if (likely(l4proto->nlattr_to_tuple)) {
961 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
962 l4proto->nla_policy, NULL);
964 ret = l4proto->nlattr_to_tuple(tb, tuple);
973 ctnetlink_parse_zone(const struct nlattr *attr,
974 struct nf_conntrack_zone *zone)
976 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
977 NF_CT_DEFAULT_ZONE_DIR, 0);
978 #ifdef CONFIG_NF_CONNTRACK_ZONES
980 zone->id = ntohs(nla_get_be16(attr));
989 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
990 struct nf_conntrack_zone *zone)
994 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
997 ret = ctnetlink_parse_zone(attr, zone);
1001 if (type == CTA_TUPLE_REPLY)
1002 zone->dir = NF_CT_ZONE_DIR_REPL;
1004 zone->dir = NF_CT_ZONE_DIR_ORIG;
1009 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1010 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1011 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1012 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1016 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1017 struct nf_conntrack_tuple *tuple, u32 type,
1018 u_int8_t l3num, struct nf_conntrack_zone *zone)
1020 struct nlattr *tb[CTA_TUPLE_MAX+1];
1023 memset(tuple, 0, sizeof(*tuple));
1025 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1030 if (!tb[CTA_TUPLE_IP])
1033 tuple->src.l3num = l3num;
1035 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1039 if (!tb[CTA_TUPLE_PROTO])
1042 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1046 if (tb[CTA_TUPLE_ZONE]) {
1050 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1056 /* orig and expect tuples get DIR_ORIGINAL */
1057 if (type == CTA_TUPLE_REPLY)
1058 tuple->dst.dir = IP_CT_DIR_REPLY;
1060 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1065 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1066 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1067 .len = NF_CT_HELPER_NAME_LEN - 1 },
1070 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1071 struct nlattr **helpinfo)
1074 struct nlattr *tb[CTA_HELP_MAX+1];
1076 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1080 if (!tb[CTA_HELP_NAME])
1083 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1085 if (tb[CTA_HELP_INFO])
1086 *helpinfo = tb[CTA_HELP_INFO];
1091 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1092 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1093 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1094 [CTA_STATUS] = { .type = NLA_U32 },
1095 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1096 [CTA_HELP] = { .type = NLA_NESTED },
1097 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1098 [CTA_TIMEOUT] = { .type = NLA_U32 },
1099 [CTA_MARK] = { .type = NLA_U32 },
1100 [CTA_ID] = { .type = NLA_U32 },
1101 [CTA_NAT_DST] = { .type = NLA_NESTED },
1102 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1103 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1104 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1105 [CTA_ZONE] = { .type = NLA_U16 },
1106 [CTA_MARK_MASK] = { .type = NLA_U32 },
1107 [CTA_LABELS] = { .type = NLA_BINARY,
1108 .len = NF_CT_LABELS_MAX_SIZE },
1109 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1110 .len = NF_CT_LABELS_MAX_SIZE },
1113 static int ctnetlink_flush_conntrack(struct net *net,
1114 const struct nlattr * const cda[],
1115 u32 portid, int report)
1117 struct ctnetlink_filter *filter = NULL;
1119 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1120 filter = ctnetlink_alloc_filter(cda);
1122 return PTR_ERR(filter);
1125 nf_ct_iterate_cleanup_net(net, ctnetlink_filter_match, filter,
1132 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1133 struct sk_buff *skb,
1134 const struct nlmsghdr *nlh,
1135 const struct nlattr * const cda[],
1136 struct netlink_ext_ack *extack)
1138 struct nf_conntrack_tuple_hash *h;
1139 struct nf_conntrack_tuple tuple;
1141 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1142 u_int8_t u3 = nfmsg->nfgen_family;
1143 struct nf_conntrack_zone zone;
1146 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1150 if (cda[CTA_TUPLE_ORIG])
1151 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1153 else if (cda[CTA_TUPLE_REPLY])
1154 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1157 return ctnetlink_flush_conntrack(net, cda,
1158 NETLINK_CB(skb).portid,
1165 h = nf_conntrack_find_get(net, &zone, &tuple);
1169 ct = nf_ct_tuplehash_to_ctrack(h);
1172 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1173 if (id != (u32)(unsigned long)ct) {
1179 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1185 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1186 struct sk_buff *skb,
1187 const struct nlmsghdr *nlh,
1188 const struct nlattr * const cda[],
1189 struct netlink_ext_ack *extack)
1191 struct nf_conntrack_tuple_hash *h;
1192 struct nf_conntrack_tuple tuple;
1194 struct sk_buff *skb2 = NULL;
1195 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1196 u_int8_t u3 = nfmsg->nfgen_family;
1197 struct nf_conntrack_zone zone;
1200 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1201 struct netlink_dump_control c = {
1202 .dump = ctnetlink_dump_table,
1203 .done = ctnetlink_done,
1206 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1207 struct ctnetlink_filter *filter;
1209 filter = ctnetlink_alloc_filter(cda);
1211 return PTR_ERR(filter);
1215 return netlink_dump_start(ctnl, skb, nlh, &c);
1218 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1222 if (cda[CTA_TUPLE_ORIG])
1223 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1225 else if (cda[CTA_TUPLE_REPLY])
1226 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1234 h = nf_conntrack_find_get(net, &zone, &tuple);
1238 ct = nf_ct_tuplehash_to_ctrack(h);
1241 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1248 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1249 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1255 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1264 /* this avoids a loop in nfnetlink. */
1265 return err == -EAGAIN ? -ENOBUFS : err;
1268 static int ctnetlink_done_list(struct netlink_callback *cb)
1271 nf_ct_put((struct nf_conn *)cb->args[1]);
1276 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1278 struct nf_conn *ct, *last;
1279 struct nf_conntrack_tuple_hash *h;
1280 struct hlist_nulls_node *n;
1281 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1282 u_int8_t l3proto = nfmsg->nfgen_family;
1285 struct hlist_nulls_head *list;
1286 struct net *net = sock_net(skb->sk);
1291 last = (struct nf_conn *)cb->args[1];
1293 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1294 struct ct_pcpu *pcpu;
1296 if (!cpu_possible(cpu))
1299 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1300 spin_lock_bh(&pcpu->lock);
1301 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1303 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1304 ct = nf_ct_tuplehash_to_ctrack(h);
1305 if (l3proto && nf_ct_l3num(ct) != l3proto)
1313 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1315 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1319 if (!atomic_inc_not_zero(&ct->ct_general.use))
1322 cb->args[1] = (unsigned long)ct;
1323 spin_unlock_bh(&pcpu->lock);
1331 spin_unlock_bh(&pcpu->lock);
1342 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1344 return ctnetlink_dump_list(skb, cb, true);
1347 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1348 struct sk_buff *skb,
1349 const struct nlmsghdr *nlh,
1350 const struct nlattr * const cda[],
1351 struct netlink_ext_ack *extack)
1353 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1354 struct netlink_dump_control c = {
1355 .dump = ctnetlink_dump_dying,
1356 .done = ctnetlink_done_list,
1358 return netlink_dump_start(ctnl, skb, nlh, &c);
1365 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1367 return ctnetlink_dump_list(skb, cb, false);
1370 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1371 struct sk_buff *skb,
1372 const struct nlmsghdr *nlh,
1373 const struct nlattr * const cda[],
1374 struct netlink_ext_ack *extack)
1376 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1377 struct netlink_dump_control c = {
1378 .dump = ctnetlink_dump_unconfirmed,
1379 .done = ctnetlink_done_list,
1381 return netlink_dump_start(ctnl, skb, nlh, &c);
1387 #ifdef CONFIG_NF_NAT_NEEDED
1389 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1390 enum nf_nat_manip_type manip,
1391 const struct nlattr *attr)
1393 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1396 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1397 if (!parse_nat_setup) {
1398 #ifdef CONFIG_MODULES
1400 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1401 if (request_module("nf-nat") < 0) {
1402 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1406 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1408 if (nfnetlink_parse_nat_setup_hook)
1414 err = parse_nat_setup(ct, manip, attr);
1415 if (err == -EAGAIN) {
1416 #ifdef CONFIG_MODULES
1418 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1419 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1420 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1424 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1435 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1440 /* Ignore these unchangable bits */
1441 on &= ~IPS_UNCHANGEABLE_MASK;
1442 off &= ~IPS_UNCHANGEABLE_MASK;
1444 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1445 if (on & (1 << bit))
1446 set_bit(bit, &ct->status);
1447 else if (off & (1 << bit))
1448 clear_bit(bit, &ct->status);
1453 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1456 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1457 d = ct->status ^ status;
1459 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1463 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1464 /* SEEN_REPLY bit can only be set */
1467 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1468 /* ASSURED bit can only be set */
1471 __ctnetlink_change_status(ct, status, 0);
1476 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1478 #ifdef CONFIG_NF_NAT_NEEDED
1481 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1484 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1489 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1493 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1499 static int ctnetlink_change_helper(struct nf_conn *ct,
1500 const struct nlattr * const cda[])
1502 struct nf_conntrack_helper *helper;
1503 struct nf_conn_help *help = nfct_help(ct);
1504 char *helpname = NULL;
1505 struct nlattr *helpinfo = NULL;
1508 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1512 /* don't change helper of sibling connections */
1514 /* If we try to change the helper to the same thing twice,
1515 * treat the second attempt as a no-op instead of returning
1521 helper = rcu_dereference(help->helper);
1522 if (helper && !strcmp(helper->name, helpname))
1530 if (!strcmp(helpname, "")) {
1531 if (help && help->helper) {
1532 /* we had a helper before ... */
1533 nf_ct_remove_expectations(ct);
1534 RCU_INIT_POINTER(help->helper, NULL);
1541 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1542 nf_ct_protonum(ct));
1543 if (helper == NULL) {
1549 if (help->helper == helper) {
1550 /* update private helper data if allowed. */
1551 if (helper->from_nlattr)
1552 helper->from_nlattr(helpinfo, ct);
1557 /* we cannot set a helper for an existing conntrack */
1565 static int ctnetlink_change_timeout(struct nf_conn *ct,
1566 const struct nlattr * const cda[])
1568 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1570 if (timeout > INT_MAX)
1572 ct->timeout = nfct_time_stamp + (u32)timeout;
1574 if (test_bit(IPS_DYING_BIT, &ct->status))
1580 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1581 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1582 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1583 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1586 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1587 const struct nlattr * const cda[])
1589 const struct nlattr *attr = cda[CTA_PROTOINFO];
1590 const struct nf_conntrack_l4proto *l4proto;
1591 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1594 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1600 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1601 if (l4proto->from_nlattr)
1602 err = l4proto->from_nlattr(tb, ct);
1608 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1609 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1610 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1611 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1614 static int change_seq_adj(struct nf_ct_seqadj *seq,
1615 const struct nlattr * const attr)
1618 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1620 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1624 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1627 seq->correction_pos =
1628 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1630 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1633 seq->offset_before =
1634 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1636 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1640 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1646 ctnetlink_change_seq_adj(struct nf_conn *ct,
1647 const struct nlattr * const cda[])
1649 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1655 spin_lock_bh(&ct->lock);
1656 if (cda[CTA_SEQ_ADJ_ORIG]) {
1657 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1658 cda[CTA_SEQ_ADJ_ORIG]);
1662 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1665 if (cda[CTA_SEQ_ADJ_REPLY]) {
1666 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1667 cda[CTA_SEQ_ADJ_REPLY]);
1671 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1674 spin_unlock_bh(&ct->lock);
1677 spin_unlock_bh(&ct->lock);
1682 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1684 #ifdef CONFIG_NF_CONNTRACK_LABELS
1685 size_t len = nla_len(cda[CTA_LABELS]);
1686 const void *mask = cda[CTA_LABELS_MASK];
1688 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1692 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1693 nla_len(cda[CTA_LABELS_MASK]) != len)
1695 mask = nla_data(cda[CTA_LABELS_MASK]);
1700 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1707 ctnetlink_change_conntrack(struct nf_conn *ct,
1708 const struct nlattr * const cda[])
1712 /* only allow NAT changes and master assignation for new conntracks */
1713 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1716 if (cda[CTA_HELP]) {
1717 err = ctnetlink_change_helper(ct, cda);
1722 if (cda[CTA_TIMEOUT]) {
1723 err = ctnetlink_change_timeout(ct, cda);
1728 if (cda[CTA_STATUS]) {
1729 err = ctnetlink_change_status(ct, cda);
1734 if (cda[CTA_PROTOINFO]) {
1735 err = ctnetlink_change_protoinfo(ct, cda);
1740 #if defined(CONFIG_NF_CONNTRACK_MARK)
1742 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1745 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1746 err = ctnetlink_change_seq_adj(ct, cda);
1751 if (cda[CTA_LABELS]) {
1752 err = ctnetlink_attach_labels(ct, cda);
1760 static struct nf_conn *
1761 ctnetlink_create_conntrack(struct net *net,
1762 const struct nf_conntrack_zone *zone,
1763 const struct nlattr * const cda[],
1764 struct nf_conntrack_tuple *otuple,
1765 struct nf_conntrack_tuple *rtuple,
1770 struct nf_conntrack_helper *helper;
1771 struct nf_conn_tstamp *tstamp;
1774 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1776 return ERR_PTR(-ENOMEM);
1778 if (!cda[CTA_TIMEOUT])
1781 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1782 if (timeout > INT_MAX)
1784 ct->timeout = (u32)timeout + nfct_time_stamp;
1787 if (cda[CTA_HELP]) {
1788 char *helpname = NULL;
1789 struct nlattr *helpinfo = NULL;
1791 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1795 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1796 nf_ct_protonum(ct));
1797 if (helper == NULL) {
1799 #ifdef CONFIG_MODULES
1800 if (request_module("nfct-helper-%s", helpname) < 0) {
1806 helper = __nf_conntrack_helper_find(helpname,
1808 nf_ct_protonum(ct));
1818 struct nf_conn_help *help;
1820 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1825 /* set private helper data if allowed. */
1826 if (helper->from_nlattr)
1827 helper->from_nlattr(helpinfo, ct);
1829 /* not in hash table yet so not strictly necessary */
1830 RCU_INIT_POINTER(help->helper, helper);
1833 /* try an implicit helper assignation */
1834 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1839 err = ctnetlink_setup_nat(ct, cda);
1843 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1844 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1845 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1846 nf_ct_labels_ext_add(ct);
1847 nfct_seqadj_ext_add(ct);
1848 nfct_synproxy_ext_add(ct);
1850 /* we must add conntrack extensions before confirmation. */
1851 ct->status |= IPS_CONFIRMED;
1853 if (cda[CTA_STATUS]) {
1854 err = ctnetlink_change_status(ct, cda);
1859 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1860 err = ctnetlink_change_seq_adj(ct, cda);
1865 memset(&ct->proto, 0, sizeof(ct->proto));
1866 if (cda[CTA_PROTOINFO]) {
1867 err = ctnetlink_change_protoinfo(ct, cda);
1872 #if defined(CONFIG_NF_CONNTRACK_MARK)
1874 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1877 /* setup master conntrack: this is a confirmed expectation */
1878 if (cda[CTA_TUPLE_MASTER]) {
1879 struct nf_conntrack_tuple master;
1880 struct nf_conntrack_tuple_hash *master_h;
1881 struct nf_conn *master_ct;
1883 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1888 master_h = nf_conntrack_find_get(net, zone, &master);
1889 if (master_h == NULL) {
1893 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1894 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1895 ct->master = master_ct;
1897 tstamp = nf_conn_tstamp_find(ct);
1899 tstamp->start = ktime_get_real_ns();
1901 err = nf_conntrack_hash_check_insert(ct);
1912 nf_conntrack_free(ct);
1913 return ERR_PTR(err);
1916 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1917 struct sk_buff *skb,
1918 const struct nlmsghdr *nlh,
1919 const struct nlattr * const cda[],
1920 struct netlink_ext_ack *extack)
1922 struct nf_conntrack_tuple otuple, rtuple;
1923 struct nf_conntrack_tuple_hash *h = NULL;
1924 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1926 u_int8_t u3 = nfmsg->nfgen_family;
1927 struct nf_conntrack_zone zone;
1930 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1934 if (cda[CTA_TUPLE_ORIG]) {
1935 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1941 if (cda[CTA_TUPLE_REPLY]) {
1942 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1948 if (cda[CTA_TUPLE_ORIG])
1949 h = nf_conntrack_find_get(net, &zone, &otuple);
1950 else if (cda[CTA_TUPLE_REPLY])
1951 h = nf_conntrack_find_get(net, &zone, &rtuple);
1955 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1956 enum ip_conntrack_events events;
1958 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1960 if (otuple.dst.protonum != rtuple.dst.protonum)
1963 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1969 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1970 events = 1 << IPCT_RELATED;
1972 events = 1 << IPCT_NEW;
1974 if (cda[CTA_LABELS] &&
1975 ctnetlink_attach_labels(ct, cda) == 0)
1976 events |= (1 << IPCT_LABEL);
1978 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1979 (1 << IPCT_ASSURED) |
1980 (1 << IPCT_HELPER) |
1981 (1 << IPCT_PROTOINFO) |
1982 (1 << IPCT_SEQADJ) |
1983 (1 << IPCT_MARK) | events,
1984 ct, NETLINK_CB(skb).portid,
1991 /* implicit 'else' */
1994 ct = nf_ct_tuplehash_to_ctrack(h);
1995 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1996 err = ctnetlink_change_conntrack(ct, cda);
1998 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1999 (1 << IPCT_ASSURED) |
2000 (1 << IPCT_HELPER) |
2002 (1 << IPCT_PROTOINFO) |
2003 (1 << IPCT_SEQADJ) |
2005 ct, NETLINK_CB(skb).portid,
2015 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2016 __u16 cpu, const struct ip_conntrack_stat *st)
2018 struct nlmsghdr *nlh;
2019 struct nfgenmsg *nfmsg;
2020 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2022 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2023 IPCTNL_MSG_CT_GET_STATS_CPU);
2024 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2028 nfmsg = nlmsg_data(nlh);
2029 nfmsg->nfgen_family = AF_UNSPEC;
2030 nfmsg->version = NFNETLINK_V0;
2031 nfmsg->res_id = htons(cpu);
2033 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2034 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2035 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2036 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2037 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2038 htonl(st->insert_failed)) ||
2039 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2040 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2041 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2042 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2043 htonl(st->search_restart)))
2044 goto nla_put_failure;
2046 nlmsg_end(skb, nlh);
2051 nlmsg_cancel(skb, nlh);
2056 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2059 struct net *net = sock_net(skb->sk);
2061 if (cb->args[0] == nr_cpu_ids)
2064 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2065 const struct ip_conntrack_stat *st;
2067 if (!cpu_possible(cpu))
2070 st = per_cpu_ptr(net->ct.stat, cpu);
2071 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2072 NETLINK_CB(cb->skb).portid,
2082 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2083 struct sk_buff *skb,
2084 const struct nlmsghdr *nlh,
2085 const struct nlattr * const cda[],
2086 struct netlink_ext_ack *extack)
2088 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2089 struct netlink_dump_control c = {
2090 .dump = ctnetlink_ct_stat_cpu_dump,
2092 return netlink_dump_start(ctnl, skb, nlh, &c);
2099 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2102 struct nlmsghdr *nlh;
2103 struct nfgenmsg *nfmsg;
2104 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2105 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2107 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2108 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2112 nfmsg = nlmsg_data(nlh);
2113 nfmsg->nfgen_family = AF_UNSPEC;
2114 nfmsg->version = NFNETLINK_V0;
2117 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2118 goto nla_put_failure;
2120 nlmsg_end(skb, nlh);
2125 nlmsg_cancel(skb, nlh);
2129 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2130 struct sk_buff *skb, const struct nlmsghdr *nlh,
2131 const struct nlattr * const cda[],
2132 struct netlink_ext_ack *extack)
2134 struct sk_buff *skb2;
2137 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2141 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2143 NFNL_MSG_TYPE(nlh->nlmsg_type),
2148 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2157 /* this avoids a loop in nfnetlink. */
2158 return err == -EAGAIN ? -ENOBUFS : err;
2161 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2162 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2163 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2164 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2165 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2166 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2167 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2168 .len = NF_CT_HELPER_NAME_LEN - 1 },
2169 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2170 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2171 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2172 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2173 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2176 static struct nf_conntrack_expect *
2177 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2178 struct nf_conntrack_helper *helper,
2179 struct nf_conntrack_tuple *tuple,
2180 struct nf_conntrack_tuple *mask);
2182 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2184 ctnetlink_glue_build_size(const struct nf_conn *ct)
2186 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2187 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2188 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2189 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2190 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2191 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2192 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2193 + nla_total_size(0) /* CTA_PROTOINFO */
2194 + nla_total_size(0) /* CTA_HELP */
2195 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2196 + ctnetlink_secctx_size(ct)
2197 #ifdef CONFIG_NF_NAT_NEEDED
2198 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2199 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2201 #ifdef CONFIG_NF_CONNTRACK_MARK
2202 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2204 #ifdef CONFIG_NF_CONNTRACK_ZONES
2205 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2207 + ctnetlink_proto_size(ct)
2211 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2212 enum ip_conntrack_info *ctinfo)
2214 return nf_ct_get(skb, ctinfo);
2217 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2219 const struct nf_conntrack_zone *zone;
2220 struct nlattr *nest_parms;
2222 zone = nf_ct_zone(ct);
2224 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2226 goto nla_put_failure;
2227 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2228 goto nla_put_failure;
2229 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2230 NF_CT_ZONE_DIR_ORIG) < 0)
2231 goto nla_put_failure;
2232 nla_nest_end(skb, nest_parms);
2234 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2236 goto nla_put_failure;
2237 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2238 goto nla_put_failure;
2239 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2240 NF_CT_ZONE_DIR_REPL) < 0)
2241 goto nla_put_failure;
2242 nla_nest_end(skb, nest_parms);
2244 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2245 NF_CT_DEFAULT_ZONE_DIR) < 0)
2246 goto nla_put_failure;
2248 if (ctnetlink_dump_id(skb, ct) < 0)
2249 goto nla_put_failure;
2251 if (ctnetlink_dump_status(skb, ct) < 0)
2252 goto nla_put_failure;
2254 if (ctnetlink_dump_timeout(skb, ct) < 0)
2255 goto nla_put_failure;
2257 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2258 goto nla_put_failure;
2260 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2261 goto nla_put_failure;
2263 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2264 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2265 goto nla_put_failure;
2267 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2268 goto nla_put_failure;
2270 if ((ct->status & IPS_SEQ_ADJUST) &&
2271 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2272 goto nla_put_failure;
2274 #ifdef CONFIG_NF_CONNTRACK_MARK
2275 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2276 goto nla_put_failure;
2278 if (ctnetlink_dump_labels(skb, ct) < 0)
2279 goto nla_put_failure;
2287 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2288 enum ip_conntrack_info ctinfo,
2289 u_int16_t ct_attr, u_int16_t ct_info_attr)
2291 struct nlattr *nest_parms;
2293 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2295 goto nla_put_failure;
2297 if (__ctnetlink_glue_build(skb, ct) < 0)
2298 goto nla_put_failure;
2300 nla_nest_end(skb, nest_parms);
2302 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2303 goto nla_put_failure;
2312 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2314 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2315 unsigned long d = ct->status ^ status;
2317 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2318 /* SEEN_REPLY bit can only be set */
2321 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2322 /* ASSURED bit can only be set */
2325 /* This check is less strict than ctnetlink_change_status()
2326 * because callers often flip IPS_EXPECTED bits when sending
2327 * an NFQA_CT attribute to the kernel. So ignore the
2328 * unchangeable bits but do not error out. Also user programs
2329 * are allowed to clear the bits that they are allowed to change.
2331 __ctnetlink_change_status(ct, status, ~status);
2336 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2340 if (cda[CTA_TIMEOUT]) {
2341 err = ctnetlink_change_timeout(ct, cda);
2345 if (cda[CTA_STATUS]) {
2346 err = ctnetlink_update_status(ct, cda);
2350 if (cda[CTA_HELP]) {
2351 err = ctnetlink_change_helper(ct, cda);
2355 if (cda[CTA_LABELS]) {
2356 err = ctnetlink_attach_labels(ct, cda);
2360 #if defined(CONFIG_NF_CONNTRACK_MARK)
2361 if (cda[CTA_MARK]) {
2362 u32 mask = 0, mark, newmark;
2363 if (cda[CTA_MARK_MASK])
2364 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2366 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2367 newmark = (ct->mark & mask) ^ mark;
2368 if (newmark != ct->mark)
2376 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2378 struct nlattr *cda[CTA_MAX+1];
2381 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2385 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2388 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2389 const struct nf_conn *ct,
2390 struct nf_conntrack_tuple *tuple,
2391 struct nf_conntrack_tuple *mask)
2395 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2396 nf_ct_l3num(ct), NULL);
2400 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2401 nf_ct_l3num(ct), NULL);
2405 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2406 u32 portid, u32 report)
2408 struct nlattr *cda[CTA_EXPECT_MAX+1];
2409 struct nf_conntrack_tuple tuple, mask;
2410 struct nf_conntrack_helper *helper = NULL;
2411 struct nf_conntrack_expect *exp;
2414 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2419 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2424 if (cda[CTA_EXPECT_HELP_NAME]) {
2425 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2427 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2428 nf_ct_protonum(ct));
2433 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2434 helper, &tuple, &mask);
2436 return PTR_ERR(exp);
2438 err = nf_ct_expect_related_report(exp, portid, report);
2439 nf_ct_expect_put(exp);
2443 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2444 enum ip_conntrack_info ctinfo, int diff)
2446 if (!(ct->status & IPS_NAT_MASK))
2449 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2452 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2453 .get_ct = ctnetlink_glue_get_ct,
2454 .build_size = ctnetlink_glue_build_size,
2455 .build = ctnetlink_glue_build,
2456 .parse = ctnetlink_glue_parse,
2457 .attach_expect = ctnetlink_glue_attach_expect,
2458 .seq_adjust = ctnetlink_glue_seqadj,
2460 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2462 /***********************************************************************
2464 ***********************************************************************/
2466 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2467 const struct nf_conntrack_tuple *tuple,
2470 struct nlattr *nest_parms;
2472 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2474 goto nla_put_failure;
2475 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2476 goto nla_put_failure;
2477 nla_nest_end(skb, nest_parms);
2485 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2486 const struct nf_conntrack_tuple *tuple,
2487 const struct nf_conntrack_tuple_mask *mask)
2489 const struct nf_conntrack_l3proto *l3proto;
2490 const struct nf_conntrack_l4proto *l4proto;
2491 struct nf_conntrack_tuple m;
2492 struct nlattr *nest_parms;
2495 memset(&m, 0xFF, sizeof(m));
2496 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2497 m.src.u.all = mask->src.u.all;
2498 m.dst.protonum = tuple->dst.protonum;
2500 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2502 goto nla_put_failure;
2505 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2506 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2508 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2509 tuple->dst.protonum);
2510 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2514 if (unlikely(ret < 0))
2515 goto nla_put_failure;
2517 nla_nest_end(skb, nest_parms);
2525 static const union nf_inet_addr any_addr;
2528 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2529 const struct nf_conntrack_expect *exp)
2531 struct nf_conn *master = exp->master;
2532 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2533 struct nf_conn_help *help;
2534 #ifdef CONFIG_NF_NAT_NEEDED
2535 struct nlattr *nest_parms;
2536 struct nf_conntrack_tuple nat_tuple = {};
2538 struct nf_ct_helper_expectfn *expfn;
2543 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2544 goto nla_put_failure;
2545 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2546 goto nla_put_failure;
2547 if (ctnetlink_exp_dump_tuple(skb,
2548 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2549 CTA_EXPECT_MASTER) < 0)
2550 goto nla_put_failure;
2552 #ifdef CONFIG_NF_NAT_NEEDED
2553 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2554 exp->saved_proto.all) {
2555 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2557 goto nla_put_failure;
2559 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2560 goto nla_put_failure;
2562 nat_tuple.src.l3num = nf_ct_l3num(master);
2563 nat_tuple.src.u3 = exp->saved_addr;
2564 nat_tuple.dst.protonum = nf_ct_protonum(master);
2565 nat_tuple.src.u = exp->saved_proto;
2567 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2568 CTA_EXPECT_NAT_TUPLE) < 0)
2569 goto nla_put_failure;
2570 nla_nest_end(skb, nest_parms);
2573 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2574 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2575 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2576 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2577 goto nla_put_failure;
2578 help = nfct_help(master);
2580 struct nf_conntrack_helper *helper;
2582 helper = rcu_dereference(help->helper);
2584 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2585 goto nla_put_failure;
2587 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2588 if (expfn != NULL &&
2589 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2590 goto nla_put_failure;
2599 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2600 int event, const struct nf_conntrack_expect *exp)
2602 struct nlmsghdr *nlh;
2603 struct nfgenmsg *nfmsg;
2604 unsigned int flags = portid ? NLM_F_MULTI : 0;
2606 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2607 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2611 nfmsg = nlmsg_data(nlh);
2612 nfmsg->nfgen_family = exp->tuple.src.l3num;
2613 nfmsg->version = NFNETLINK_V0;
2616 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2617 goto nla_put_failure;
2619 nlmsg_end(skb, nlh);
2624 nlmsg_cancel(skb, nlh);
2628 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2630 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2632 struct nf_conntrack_expect *exp = item->exp;
2633 struct net *net = nf_ct_exp_net(exp);
2634 struct nlmsghdr *nlh;
2635 struct nfgenmsg *nfmsg;
2636 struct sk_buff *skb;
2637 unsigned int type, group;
2640 if (events & (1 << IPEXP_DESTROY)) {
2641 type = IPCTNL_MSG_EXP_DELETE;
2642 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2643 } else if (events & (1 << IPEXP_NEW)) {
2644 type = IPCTNL_MSG_EXP_NEW;
2645 flags = NLM_F_CREATE|NLM_F_EXCL;
2646 group = NFNLGRP_CONNTRACK_EXP_NEW;
2650 if (!item->report && !nfnetlink_has_listeners(net, group))
2653 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2657 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2658 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2662 nfmsg = nlmsg_data(nlh);
2663 nfmsg->nfgen_family = exp->tuple.src.l3num;
2664 nfmsg->version = NFNETLINK_V0;
2667 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2668 goto nla_put_failure;
2670 nlmsg_end(skb, nlh);
2671 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2675 nlmsg_cancel(skb, nlh);
2679 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2683 static int ctnetlink_exp_done(struct netlink_callback *cb)
2686 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2691 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2693 struct net *net = sock_net(skb->sk);
2694 struct nf_conntrack_expect *exp, *last;
2695 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2696 u_int8_t l3proto = nfmsg->nfgen_family;
2699 last = (struct nf_conntrack_expect *)cb->args[1];
2700 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2702 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2704 if (l3proto && exp->tuple.src.l3num != l3proto)
2707 if (!net_eq(nf_ct_net(exp->master), net))
2715 if (ctnetlink_exp_fill_info(skb,
2716 NETLINK_CB(cb->skb).portid,
2720 if (!refcount_inc_not_zero(&exp->use))
2722 cb->args[1] = (unsigned long)exp;
2734 nf_ct_expect_put(last);
2740 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2742 struct nf_conntrack_expect *exp, *last;
2743 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2744 struct nf_conn *ct = cb->data;
2745 struct nf_conn_help *help = nfct_help(ct);
2746 u_int8_t l3proto = nfmsg->nfgen_family;
2752 last = (struct nf_conntrack_expect *)cb->args[1];
2754 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2755 if (l3proto && exp->tuple.src.l3num != l3proto)
2762 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2766 if (!refcount_inc_not_zero(&exp->use))
2768 cb->args[1] = (unsigned long)exp;
2780 nf_ct_expect_put(last);
2785 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2786 struct sk_buff *skb,
2787 const struct nlmsghdr *nlh,
2788 const struct nlattr * const cda[],
2789 struct netlink_ext_ack *extack)
2792 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2793 u_int8_t u3 = nfmsg->nfgen_family;
2794 struct nf_conntrack_tuple tuple;
2795 struct nf_conntrack_tuple_hash *h;
2797 struct nf_conntrack_zone zone;
2798 struct netlink_dump_control c = {
2799 .dump = ctnetlink_exp_ct_dump_table,
2800 .done = ctnetlink_exp_done,
2803 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2808 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2812 h = nf_conntrack_find_get(net, &zone, &tuple);
2816 ct = nf_ct_tuplehash_to_ctrack(h);
2817 /* No expectation linked to this connection tracking. */
2818 if (!nfct_help(ct)) {
2825 err = netlink_dump_start(ctnl, skb, nlh, &c);
2831 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2832 struct sk_buff *skb, const struct nlmsghdr *nlh,
2833 const struct nlattr * const cda[],
2834 struct netlink_ext_ack *extack)
2836 struct nf_conntrack_tuple tuple;
2837 struct nf_conntrack_expect *exp;
2838 struct sk_buff *skb2;
2839 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2840 u_int8_t u3 = nfmsg->nfgen_family;
2841 struct nf_conntrack_zone zone;
2844 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2845 if (cda[CTA_EXPECT_MASTER])
2846 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
2849 struct netlink_dump_control c = {
2850 .dump = ctnetlink_exp_dump_table,
2851 .done = ctnetlink_exp_done,
2853 return netlink_dump_start(ctnl, skb, nlh, &c);
2857 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2861 if (cda[CTA_EXPECT_TUPLE])
2862 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2864 else if (cda[CTA_EXPECT_MASTER])
2865 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2873 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2877 if (cda[CTA_EXPECT_ID]) {
2878 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2879 if (ntohl(id) != (u32)(unsigned long)exp) {
2880 nf_ct_expect_put(exp);
2886 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2888 nf_ct_expect_put(exp);
2893 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2894 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2896 nf_ct_expect_put(exp);
2900 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2909 /* this avoids a loop in nfnetlink. */
2910 return err == -EAGAIN ? -ENOBUFS : err;
2913 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
2915 const struct nf_conn_help *m_help;
2916 const char *name = data;
2918 m_help = nfct_help(exp->master);
2920 return strcmp(m_help->helper->name, name) == 0;
2923 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
2928 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2929 struct sk_buff *skb, const struct nlmsghdr *nlh,
2930 const struct nlattr * const cda[],
2931 struct netlink_ext_ack *extack)
2933 struct nf_conntrack_expect *exp;
2934 struct nf_conntrack_tuple tuple;
2935 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2936 u_int8_t u3 = nfmsg->nfgen_family;
2937 struct nf_conntrack_zone zone;
2940 if (cda[CTA_EXPECT_TUPLE]) {
2941 /* delete a single expect by tuple */
2942 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2946 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2951 /* bump usage count to 2 */
2952 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2956 if (cda[CTA_EXPECT_ID]) {
2957 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2958 if (ntohl(id) != (u32)(unsigned long)exp) {
2959 nf_ct_expect_put(exp);
2964 /* after list removal, usage count == 1 */
2965 spin_lock_bh(&nf_conntrack_expect_lock);
2966 if (del_timer(&exp->timeout)) {
2967 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2969 nf_ct_expect_put(exp);
2971 spin_unlock_bh(&nf_conntrack_expect_lock);
2972 /* have to put what we 'get' above.
2973 * after this line usage count == 0 */
2974 nf_ct_expect_put(exp);
2975 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2976 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2978 nf_ct_expect_iterate_net(net, expect_iter_name, name,
2979 NETLINK_CB(skb).portid,
2982 /* This basically means we have to flush everything*/
2983 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
2984 NETLINK_CB(skb).portid,
2991 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2992 const struct nlattr * const cda[])
2994 if (cda[CTA_EXPECT_TIMEOUT]) {
2995 if (!del_timer(&x->timeout))
2998 x->timeout.expires = jiffies +
2999 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3000 add_timer(&x->timeout);
3005 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3006 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3007 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3011 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3012 struct nf_conntrack_expect *exp,
3015 #ifdef CONFIG_NF_NAT_NEEDED
3016 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3017 struct nf_conntrack_tuple nat_tuple = {};
3020 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3021 exp_nat_nla_policy, NULL);
3025 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3028 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3029 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3034 exp->saved_addr = nat_tuple.src.u3;
3035 exp->saved_proto = nat_tuple.src.u;
3036 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3044 static struct nf_conntrack_expect *
3045 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3046 struct nf_conntrack_helper *helper,
3047 struct nf_conntrack_tuple *tuple,
3048 struct nf_conntrack_tuple *mask)
3050 u_int32_t class = 0;
3051 struct nf_conntrack_expect *exp;
3052 struct nf_conn_help *help;
3055 help = nfct_help(ct);
3057 return ERR_PTR(-EOPNOTSUPP);
3059 if (cda[CTA_EXPECT_CLASS] && helper) {
3060 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3061 if (class > helper->expect_class_max)
3062 return ERR_PTR(-EINVAL);
3064 exp = nf_ct_expect_alloc(ct);
3066 return ERR_PTR(-ENOMEM);
3068 if (cda[CTA_EXPECT_FLAGS]) {
3069 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3070 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3074 if (cda[CTA_EXPECT_FN]) {
3075 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3076 struct nf_ct_helper_expectfn *expfn;
3078 expfn = nf_ct_helper_expectfn_find_by_name(name);
3079 if (expfn == NULL) {
3083 exp->expectfn = expfn->expectfn;
3085 exp->expectfn = NULL;
3089 exp->helper = helper;
3090 exp->tuple = *tuple;
3091 exp->mask.src.u3 = mask->src.u3;
3092 exp->mask.src.u.all = mask->src.u.all;
3094 if (cda[CTA_EXPECT_NAT]) {
3095 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3096 exp, nf_ct_l3num(ct));
3102 nf_ct_expect_put(exp);
3103 return ERR_PTR(err);
3107 ctnetlink_create_expect(struct net *net,
3108 const struct nf_conntrack_zone *zone,
3109 const struct nlattr * const cda[],
3110 u_int8_t u3, u32 portid, int report)
3112 struct nf_conntrack_tuple tuple, mask, master_tuple;
3113 struct nf_conntrack_tuple_hash *h = NULL;
3114 struct nf_conntrack_helper *helper = NULL;
3115 struct nf_conntrack_expect *exp;
3119 /* caller guarantees that those three CTA_EXPECT_* exist */
3120 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3124 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3128 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3133 /* Look for master conntrack of this expectation */
3134 h = nf_conntrack_find_get(net, zone, &master_tuple);
3137 ct = nf_ct_tuplehash_to_ctrack(h);
3140 if (cda[CTA_EXPECT_HELP_NAME]) {
3141 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3143 helper = __nf_conntrack_helper_find(helpname, u3,
3144 nf_ct_protonum(ct));
3145 if (helper == NULL) {
3147 #ifdef CONFIG_MODULES
3148 if (request_module("nfct-helper-%s", helpname) < 0) {
3153 helper = __nf_conntrack_helper_find(helpname, u3,
3154 nf_ct_protonum(ct));
3166 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3172 err = nf_ct_expect_related_report(exp, portid, report);
3173 nf_ct_expect_put(exp);
3181 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3182 struct sk_buff *skb, const struct nlmsghdr *nlh,
3183 const struct nlattr * const cda[],
3184 struct netlink_ext_ack *extack)
3186 struct nf_conntrack_tuple tuple;
3187 struct nf_conntrack_expect *exp;
3188 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3189 u_int8_t u3 = nfmsg->nfgen_family;
3190 struct nf_conntrack_zone zone;
3193 if (!cda[CTA_EXPECT_TUPLE]
3194 || !cda[CTA_EXPECT_MASK]
3195 || !cda[CTA_EXPECT_MASTER])
3198 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3202 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3207 spin_lock_bh(&nf_conntrack_expect_lock);
3208 exp = __nf_ct_expect_find(net, &zone, &tuple);
3210 spin_unlock_bh(&nf_conntrack_expect_lock);
3212 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3213 err = ctnetlink_create_expect(net, &zone, cda, u3,
3214 NETLINK_CB(skb).portid,
3221 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3222 err = ctnetlink_change_expect(exp, cda);
3223 spin_unlock_bh(&nf_conntrack_expect_lock);
3229 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3230 const struct ip_conntrack_stat *st)
3232 struct nlmsghdr *nlh;
3233 struct nfgenmsg *nfmsg;
3234 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3236 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3237 IPCTNL_MSG_EXP_GET_STATS_CPU);
3238 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3242 nfmsg = nlmsg_data(nlh);
3243 nfmsg->nfgen_family = AF_UNSPEC;
3244 nfmsg->version = NFNETLINK_V0;
3245 nfmsg->res_id = htons(cpu);
3247 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3248 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3249 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3250 goto nla_put_failure;
3252 nlmsg_end(skb, nlh);
3257 nlmsg_cancel(skb, nlh);
3262 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3265 struct net *net = sock_net(skb->sk);
3267 if (cb->args[0] == nr_cpu_ids)
3270 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3271 const struct ip_conntrack_stat *st;
3273 if (!cpu_possible(cpu))
3276 st = per_cpu_ptr(net->ct.stat, cpu);
3277 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3287 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3288 struct sk_buff *skb,
3289 const struct nlmsghdr *nlh,
3290 const struct nlattr * const cda[],
3291 struct netlink_ext_ack *extack)
3293 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3294 struct netlink_dump_control c = {
3295 .dump = ctnetlink_exp_stat_cpu_dump,
3297 return netlink_dump_start(ctnl, skb, nlh, &c);
3303 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3304 static struct nf_ct_event_notifier ctnl_notifier = {
3305 .fcn = ctnetlink_conntrack_event,
3308 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3309 .fcn = ctnetlink_expect_event,
3313 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3314 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3315 .attr_count = CTA_MAX,
3316 .policy = ct_nla_policy },
3317 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3318 .attr_count = CTA_MAX,
3319 .policy = ct_nla_policy },
3320 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3321 .attr_count = CTA_MAX,
3322 .policy = ct_nla_policy },
3323 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3324 .attr_count = CTA_MAX,
3325 .policy = ct_nla_policy },
3326 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3327 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3328 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3329 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3332 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3333 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3334 .attr_count = CTA_EXPECT_MAX,
3335 .policy = exp_nla_policy },
3336 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3337 .attr_count = CTA_EXPECT_MAX,
3338 .policy = exp_nla_policy },
3339 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3340 .attr_count = CTA_EXPECT_MAX,
3341 .policy = exp_nla_policy },
3342 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3345 static const struct nfnetlink_subsystem ctnl_subsys = {
3346 .name = "conntrack",
3347 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3348 .cb_count = IPCTNL_MSG_MAX,
3352 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3353 .name = "conntrack_expect",
3354 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3355 .cb_count = IPCTNL_MSG_EXP_MAX,
3359 MODULE_ALIAS("ip_conntrack_netlink");
3360 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3361 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3363 static int __net_init ctnetlink_net_init(struct net *net)
3365 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3368 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3370 pr_err("ctnetlink_init: cannot register notifier.\n");
3374 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3376 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3377 goto err_unreg_notifier;
3382 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3384 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3390 static void ctnetlink_net_exit(struct net *net)
3392 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3393 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3394 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3398 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3402 list_for_each_entry(net, net_exit_list, exit_list)
3403 ctnetlink_net_exit(net);
3406 static struct pernet_operations ctnetlink_net_ops = {
3407 .init = ctnetlink_net_init,
3408 .exit_batch = ctnetlink_net_exit_batch,
3411 static int __init ctnetlink_init(void)
3415 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3416 ret = nfnetlink_subsys_register(&ctnl_subsys);
3418 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3422 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3424 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3425 goto err_unreg_subsys;
3428 ret = register_pernet_subsys(&ctnetlink_net_ops);
3430 pr_err("ctnetlink_init: cannot register pernet operations\n");
3431 goto err_unreg_exp_subsys;
3433 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3434 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3435 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3439 err_unreg_exp_subsys:
3440 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3442 nfnetlink_subsys_unregister(&ctnl_subsys);
3447 static void __exit ctnetlink_exit(void)
3449 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3451 unregister_pernet_subsys(&ctnetlink_net_ops);
3452 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3453 nfnetlink_subsys_unregister(&ctnl_subsys);
3454 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3455 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3460 module_init(ctnetlink_init);
3461 module_exit(ctnetlink_exit);