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_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 int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
61 const struct nf_conntrack_tuple *tuple,
62 const struct nf_conntrack_l4proto *l4proto)
65 struct nlattr *nest_parms;
67 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
70 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
73 if (likely(l4proto->tuple_to_nlattr))
74 ret = l4proto->tuple_to_nlattr(skb, tuple);
76 nla_nest_end(skb, nest_parms);
84 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
85 const struct nf_conntrack_tuple *tuple)
87 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
88 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
93 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
94 const struct nf_conntrack_tuple *tuple)
96 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
97 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
102 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
103 const struct nf_conntrack_tuple *tuple)
106 struct nlattr *nest_parms;
108 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
110 goto nla_put_failure;
112 switch (tuple->src.l3num) {
114 ret = ipv4_tuple_to_nlattr(skb, tuple);
117 ret = ipv6_tuple_to_nlattr(skb, tuple);
121 nla_nest_end(skb, nest_parms);
129 static int ctnetlink_dump_tuples(struct sk_buff *skb,
130 const struct nf_conntrack_tuple *tuple)
132 const struct nf_conntrack_l4proto *l4proto;
136 ret = ctnetlink_dump_tuples_ip(skb, tuple);
139 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
140 tuple->dst.protonum);
141 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
147 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
148 const struct nf_conntrack_zone *zone, int dir)
150 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
152 if (nla_put_be16(skb, attrtype, htons(zone->id)))
153 goto nla_put_failure;
160 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
162 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
163 goto nla_put_failure;
170 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
172 long timeout = nf_ct_expires(ct) / HZ;
174 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
175 goto nla_put_failure;
182 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
184 const struct nf_conntrack_l4proto *l4proto;
185 struct nlattr *nest_proto;
188 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
189 if (!l4proto->to_nlattr)
192 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
194 goto nla_put_failure;
196 ret = l4proto->to_nlattr(skb, nest_proto, ct);
198 nla_nest_end(skb, nest_proto);
206 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
207 const struct nf_conn *ct)
209 struct nlattr *nest_helper;
210 const struct nf_conn_help *help = nfct_help(ct);
211 struct nf_conntrack_helper *helper;
217 helper = rcu_dereference(help->helper);
221 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
223 goto nla_put_failure;
224 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
225 goto nla_put_failure;
227 if (helper->to_nlattr)
228 helper->to_nlattr(skb, ct);
230 nla_nest_end(skb, nest_helper);
241 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
242 enum ip_conntrack_dir dir, int type)
244 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
245 struct nf_conn_counter *counter = acct->counter;
246 struct nlattr *nest_count;
249 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
250 pkts = atomic64_xchg(&counter[dir].packets, 0);
251 bytes = atomic64_xchg(&counter[dir].bytes, 0);
253 pkts = atomic64_read(&counter[dir].packets);
254 bytes = atomic64_read(&counter[dir].bytes);
257 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
259 goto nla_put_failure;
261 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
263 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
265 goto nla_put_failure;
267 nla_nest_end(skb, nest_count);
276 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
278 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
283 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
285 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
292 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
294 struct nlattr *nest_count;
295 const struct nf_conn_tstamp *tstamp;
297 tstamp = nf_conn_tstamp_find(ct);
301 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
303 goto nla_put_failure;
305 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
306 CTA_TIMESTAMP_PAD) ||
307 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
308 cpu_to_be64(tstamp->stop),
310 goto nla_put_failure;
311 nla_nest_end(skb, nest_count);
319 #ifdef CONFIG_NF_CONNTRACK_MARK
320 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
322 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
323 goto nla_put_failure;
330 #define ctnetlink_dump_mark(a, b) (0)
333 #ifdef CONFIG_NF_CONNTRACK_SECMARK
334 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
336 struct nlattr *nest_secctx;
340 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
345 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
347 goto nla_put_failure;
349 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
350 goto nla_put_failure;
351 nla_nest_end(skb, nest_secctx);
355 security_release_secctx(secctx, len);
359 #define ctnetlink_dump_secctx(a, b) (0)
362 #ifdef CONFIG_NF_CONNTRACK_LABELS
363 static inline int ctnetlink_label_size(const struct nf_conn *ct)
365 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
369 return nla_total_size(sizeof(labels->bits));
373 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
375 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
383 if (labels->bits[i] != 0)
384 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
387 } while (i < ARRAY_SIZE(labels->bits));
392 #define ctnetlink_dump_labels(a, b) (0)
393 #define ctnetlink_label_size(a) (0)
396 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
398 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
400 struct nlattr *nest_parms;
402 if (!(ct->status & IPS_EXPECTED))
405 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
407 goto nla_put_failure;
408 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
409 goto nla_put_failure;
410 nla_nest_end(skb, nest_parms);
419 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
421 struct nlattr *nest_parms;
423 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
425 goto nla_put_failure;
427 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
428 htonl(seq->correction_pos)) ||
429 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
430 htonl(seq->offset_before)) ||
431 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
432 htonl(seq->offset_after)))
433 goto nla_put_failure;
435 nla_nest_end(skb, nest_parms);
443 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
445 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
446 struct nf_ct_seqadj *seq;
448 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
451 spin_lock_bh(&ct->lock);
452 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
453 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
456 seq = &seqadj->seq[IP_CT_DIR_REPLY];
457 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
460 spin_unlock_bh(&ct->lock);
463 spin_unlock_bh(&ct->lock);
467 static int ctnetlink_dump_ct_synproxy(struct sk_buff *skb, struct nf_conn *ct)
469 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
470 struct nlattr *nest_parms;
475 nest_parms = nla_nest_start(skb, CTA_SYNPROXY | NLA_F_NESTED);
477 goto nla_put_failure;
479 if (nla_put_be32(skb, CTA_SYNPROXY_ISN, htonl(synproxy->isn)) ||
480 nla_put_be32(skb, CTA_SYNPROXY_ITS, htonl(synproxy->its)) ||
481 nla_put_be32(skb, CTA_SYNPROXY_TSOFF, htonl(synproxy->tsoff)))
482 goto nla_put_failure;
484 nla_nest_end(skb, nest_parms);
492 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
494 __be32 id = (__force __be32)nf_ct_get_id(ct);
496 if (nla_put_be32(skb, CTA_ID, id))
497 goto nla_put_failure;
504 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
506 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
507 goto nla_put_failure;
515 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
518 const struct nf_conntrack_zone *zone;
519 struct nlmsghdr *nlh;
520 struct nlattr *nest_parms;
521 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
523 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
524 nlh = nfnl_msg_put(skb, portid, seq, event, flags, nf_ct_l3num(ct),
529 zone = nf_ct_zone(ct);
531 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
533 goto nla_put_failure;
534 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
535 goto nla_put_failure;
536 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
537 NF_CT_ZONE_DIR_ORIG) < 0)
538 goto nla_put_failure;
539 nla_nest_end(skb, nest_parms);
541 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
543 goto nla_put_failure;
544 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
545 goto nla_put_failure;
546 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
547 NF_CT_ZONE_DIR_REPL) < 0)
548 goto nla_put_failure;
549 nla_nest_end(skb, nest_parms);
551 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
552 NF_CT_DEFAULT_ZONE_DIR) < 0)
553 goto nla_put_failure;
555 if (ctnetlink_dump_status(skb, ct) < 0 ||
556 ctnetlink_dump_acct(skb, ct, type) < 0 ||
557 ctnetlink_dump_timestamp(skb, ct) < 0 ||
558 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
559 ctnetlink_dump_mark(skb, ct) < 0 ||
560 ctnetlink_dump_secctx(skb, ct) < 0 ||
561 ctnetlink_dump_labels(skb, ct) < 0 ||
562 ctnetlink_dump_id(skb, ct) < 0 ||
563 ctnetlink_dump_use(skb, ct) < 0 ||
564 ctnetlink_dump_master(skb, ct) < 0 ||
565 ctnetlink_dump_ct_seq_adj(skb, ct) < 0 ||
566 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
567 goto nla_put_failure;
569 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status) &&
570 (ctnetlink_dump_timeout(skb, ct) < 0 ||
571 ctnetlink_dump_protoinfo(skb, ct) < 0))
572 goto nla_put_failure;
579 nlmsg_cancel(skb, nlh);
583 static const struct nla_policy cta_ip_nla_policy[CTA_IP_MAX + 1] = {
584 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
585 [CTA_IP_V4_DST] = { .type = NLA_U32 },
586 [CTA_IP_V6_SRC] = { .len = sizeof(__be32) * 4 },
587 [CTA_IP_V6_DST] = { .len = sizeof(__be32) * 4 },
590 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
591 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
593 const struct nf_conntrack_l4proto *l4proto;
594 size_t len, len4 = 0;
596 len = nla_policy_len(cta_ip_nla_policy, CTA_IP_MAX + 1);
597 len *= 3u; /* ORIG, REPLY, MASTER */
599 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
600 len += l4proto->nlattr_size;
601 if (l4proto->nlattr_tuple_size) {
602 len4 = l4proto->nlattr_tuple_size();
603 len4 *= 3u; /* ORIG, REPLY, MASTER */
610 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
612 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
614 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
615 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
616 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
620 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
622 #ifdef CONFIG_NF_CONNTRACK_SECMARK
625 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
629 return nla_total_size(0) /* CTA_SECCTX */
630 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
636 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
638 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
639 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
641 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
647 #ifdef CONFIG_NF_CONNTRACK_EVENTS
648 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
650 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
651 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
652 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
653 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
654 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
655 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
656 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
657 + ctnetlink_acct_size(ct)
658 + ctnetlink_timestamp_size(ct)
659 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
660 + nla_total_size(0) /* CTA_PROTOINFO */
661 + nla_total_size(0) /* CTA_HELP */
662 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
663 + ctnetlink_secctx_size(ct)
664 #ifdef CONFIG_NF_NAT_NEEDED
665 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
666 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
668 #ifdef CONFIG_NF_CONNTRACK_MARK
669 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
671 #ifdef CONFIG_NF_CONNTRACK_ZONES
672 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
674 + ctnetlink_proto_size(ct)
675 + ctnetlink_label_size(ct)
680 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
682 const struct nf_conntrack_zone *zone;
684 struct nlmsghdr *nlh;
685 struct nlattr *nest_parms;
686 struct nf_conn *ct = item->ct;
689 unsigned int flags = 0, group;
692 if (events & (1 << IPCT_DESTROY)) {
693 type = IPCTNL_MSG_CT_DELETE;
694 group = NFNLGRP_CONNTRACK_DESTROY;
695 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
696 type = IPCTNL_MSG_CT_NEW;
697 flags = NLM_F_CREATE|NLM_F_EXCL;
698 group = NFNLGRP_CONNTRACK_NEW;
700 type = IPCTNL_MSG_CT_NEW;
701 group = NFNLGRP_CONNTRACK_UPDATE;
706 if (!item->report && !nfnetlink_has_listeners(net, group))
709 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
713 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
714 nlh = nfnl_msg_put(skb, item->portid, 0, type, flags, nf_ct_l3num(ct),
719 zone = nf_ct_zone(ct);
721 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
723 goto nla_put_failure;
724 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
725 goto nla_put_failure;
726 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
727 NF_CT_ZONE_DIR_ORIG) < 0)
728 goto nla_put_failure;
729 nla_nest_end(skb, nest_parms);
731 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
733 goto nla_put_failure;
734 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
735 goto nla_put_failure;
736 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
737 NF_CT_ZONE_DIR_REPL) < 0)
738 goto nla_put_failure;
739 nla_nest_end(skb, nest_parms);
741 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
742 NF_CT_DEFAULT_ZONE_DIR) < 0)
743 goto nla_put_failure;
745 if (ctnetlink_dump_id(skb, ct) < 0)
746 goto nla_put_failure;
748 if (ctnetlink_dump_status(skb, ct) < 0)
749 goto nla_put_failure;
751 if (events & (1 << IPCT_DESTROY)) {
752 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
753 ctnetlink_dump_timestamp(skb, ct) < 0)
754 goto nla_put_failure;
756 if (ctnetlink_dump_timeout(skb, ct) < 0)
757 goto nla_put_failure;
759 if (events & (1 << IPCT_PROTOINFO)
760 && ctnetlink_dump_protoinfo(skb, ct) < 0)
761 goto nla_put_failure;
763 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
764 && ctnetlink_dump_helpinfo(skb, ct) < 0)
765 goto nla_put_failure;
767 #ifdef CONFIG_NF_CONNTRACK_SECMARK
768 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
769 && ctnetlink_dump_secctx(skb, ct) < 0)
770 goto nla_put_failure;
772 if (events & (1 << IPCT_LABEL) &&
773 ctnetlink_dump_labels(skb, ct) < 0)
774 goto nla_put_failure;
776 if (events & (1 << IPCT_RELATED) &&
777 ctnetlink_dump_master(skb, ct) < 0)
778 goto nla_put_failure;
780 if (events & (1 << IPCT_SEQADJ) &&
781 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
782 goto nla_put_failure;
784 if (events & (1 << IPCT_SYNPROXY) &&
785 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
786 goto nla_put_failure;
789 #ifdef CONFIG_NF_CONNTRACK_MARK
790 if ((events & (1 << IPCT_MARK) || ct->mark)
791 && ctnetlink_dump_mark(skb, ct) < 0)
792 goto nla_put_failure;
795 err = nfnetlink_send(skb, net, item->portid, group, item->report,
797 if (err == -ENOBUFS || err == -EAGAIN)
803 nlmsg_cancel(skb, nlh);
807 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
812 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
814 static int ctnetlink_done(struct netlink_callback *cb)
817 nf_ct_put((struct nf_conn *)cb->args[1]);
822 struct ctnetlink_filter {
829 static struct ctnetlink_filter *
830 ctnetlink_alloc_filter(const struct nlattr * const cda[])
832 #ifdef CONFIG_NF_CONNTRACK_MARK
833 struct ctnetlink_filter *filter;
835 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
837 return ERR_PTR(-ENOMEM);
839 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
840 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
844 return ERR_PTR(-EOPNOTSUPP);
848 static int ctnetlink_start(struct netlink_callback *cb)
850 const struct nlattr * const *cda = cb->data;
851 struct ctnetlink_filter *filter = NULL;
853 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
854 filter = ctnetlink_alloc_filter(cda);
856 return PTR_ERR(filter);
863 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
865 struct ctnetlink_filter *filter = data;
870 #ifdef CONFIG_NF_CONNTRACK_MARK
871 if ((ct->mark & filter->mark.mask) == filter->mark.val)
879 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
881 struct net *net = sock_net(skb->sk);
882 struct nf_conn *ct, *last;
883 struct nf_conntrack_tuple_hash *h;
884 struct hlist_nulls_node *n;
885 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
886 u_int8_t l3proto = nfmsg->nfgen_family;
887 struct nf_conn *nf_ct_evict[8];
891 last = (struct nf_conn *)cb->args[1];
895 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
899 if (nf_ct_should_gc(nf_ct_evict[i]))
900 nf_ct_kill(nf_ct_evict[i]);
901 nf_ct_put(nf_ct_evict[i]);
904 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
905 nf_conntrack_lock(lockp);
906 if (cb->args[0] >= nf_conntrack_htable_size) {
910 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
912 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
914 ct = nf_ct_tuplehash_to_ctrack(h);
915 if (nf_ct_is_expired(ct)) {
916 if (i < ARRAY_SIZE(nf_ct_evict) &&
917 atomic_inc_not_zero(&ct->ct_general.use))
918 nf_ct_evict[i++] = ct;
922 if (!net_eq(net, nf_ct_net(ct)))
925 /* Dump entries of a given L3 protocol number.
926 * If it is not specified, ie. l3proto == 0,
927 * then dump everything. */
928 if (l3proto && nf_ct_l3num(ct) != l3proto)
935 if (!ctnetlink_filter_match(ct, cb->data))
940 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
942 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
946 nf_conntrack_get(&ct->ct_general);
947 cb->args[1] = (unsigned long)ct;
961 /* nf ct hash resize happened, now clear the leftover. */
962 if ((struct nf_conn *)cb->args[1] == last)
970 if (nf_ct_should_gc(nf_ct_evict[i]))
971 nf_ct_kill(nf_ct_evict[i]);
972 nf_ct_put(nf_ct_evict[i]);
978 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
979 struct nf_conntrack_tuple *t)
981 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
984 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
985 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
990 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
991 struct nf_conntrack_tuple *t)
993 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
996 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
997 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
1002 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
1003 struct nf_conntrack_tuple *tuple)
1005 struct nlattr *tb[CTA_IP_MAX+1];
1008 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
1012 ret = nla_validate_nested(attr, CTA_IP_MAX,
1013 cta_ip_nla_policy, NULL);
1017 switch (tuple->src.l3num) {
1019 ret = ipv4_nlattr_to_tuple(tb, tuple);
1022 ret = ipv6_nlattr_to_tuple(tb, tuple);
1029 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
1030 [CTA_PROTO_NUM] = { .type = NLA_U8 },
1033 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
1034 struct nf_conntrack_tuple *tuple)
1036 const struct nf_conntrack_l4proto *l4proto;
1037 struct nlattr *tb[CTA_PROTO_MAX+1];
1040 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
1045 if (!tb[CTA_PROTO_NUM])
1047 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
1050 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1052 if (likely(l4proto->nlattr_to_tuple)) {
1053 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
1054 l4proto->nla_policy, NULL);
1056 ret = l4proto->nlattr_to_tuple(tb, tuple);
1065 ctnetlink_parse_zone(const struct nlattr *attr,
1066 struct nf_conntrack_zone *zone)
1068 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
1069 NF_CT_DEFAULT_ZONE_DIR, 0);
1070 #ifdef CONFIG_NF_CONNTRACK_ZONES
1072 zone->id = ntohs(nla_get_be16(attr));
1081 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1082 struct nf_conntrack_zone *zone)
1086 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1089 ret = ctnetlink_parse_zone(attr, zone);
1093 if (type == CTA_TUPLE_REPLY)
1094 zone->dir = NF_CT_ZONE_DIR_REPL;
1096 zone->dir = NF_CT_ZONE_DIR_ORIG;
1101 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1102 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1103 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1104 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1108 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1109 struct nf_conntrack_tuple *tuple, u32 type,
1110 u_int8_t l3num, struct nf_conntrack_zone *zone)
1112 struct nlattr *tb[CTA_TUPLE_MAX+1];
1115 memset(tuple, 0, sizeof(*tuple));
1117 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1122 if (!tb[CTA_TUPLE_IP])
1125 if (l3num != NFPROTO_IPV4 && l3num != NFPROTO_IPV6)
1127 tuple->src.l3num = l3num;
1129 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1133 if (!tb[CTA_TUPLE_PROTO])
1136 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1140 if (tb[CTA_TUPLE_ZONE]) {
1144 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1150 /* orig and expect tuples get DIR_ORIGINAL */
1151 if (type == CTA_TUPLE_REPLY)
1152 tuple->dst.dir = IP_CT_DIR_REPLY;
1154 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1159 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1160 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1161 .len = NF_CT_HELPER_NAME_LEN - 1 },
1164 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1165 struct nlattr **helpinfo)
1168 struct nlattr *tb[CTA_HELP_MAX+1];
1170 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1174 if (!tb[CTA_HELP_NAME])
1177 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1179 if (tb[CTA_HELP_INFO])
1180 *helpinfo = tb[CTA_HELP_INFO];
1185 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1186 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1187 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1188 [CTA_STATUS] = { .type = NLA_U32 },
1189 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1190 [CTA_HELP] = { .type = NLA_NESTED },
1191 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1192 [CTA_TIMEOUT] = { .type = NLA_U32 },
1193 [CTA_MARK] = { .type = NLA_U32 },
1194 [CTA_ID] = { .type = NLA_U32 },
1195 [CTA_NAT_DST] = { .type = NLA_NESTED },
1196 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1197 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1198 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1199 [CTA_ZONE] = { .type = NLA_U16 },
1200 [CTA_MARK_MASK] = { .type = NLA_U32 },
1201 [CTA_LABELS] = { .type = NLA_BINARY,
1202 .len = NF_CT_LABELS_MAX_SIZE },
1203 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1204 .len = NF_CT_LABELS_MAX_SIZE },
1207 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1209 return ctnetlink_filter_match(ct, data);
1212 static int ctnetlink_flush_conntrack(struct net *net,
1213 const struct nlattr * const cda[],
1214 u32 portid, int report)
1216 struct ctnetlink_filter *filter = NULL;
1218 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1219 filter = ctnetlink_alloc_filter(cda);
1221 return PTR_ERR(filter);
1224 nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
1231 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1232 struct sk_buff *skb,
1233 const struct nlmsghdr *nlh,
1234 const struct nlattr * const cda[],
1235 struct netlink_ext_ack *extack)
1237 struct nf_conntrack_tuple_hash *h;
1238 struct nf_conntrack_tuple tuple;
1240 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1241 u_int8_t u3 = nfmsg->nfgen_family;
1242 struct nf_conntrack_zone zone;
1245 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1249 if (cda[CTA_TUPLE_ORIG])
1250 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1252 else if (cda[CTA_TUPLE_REPLY])
1253 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1256 return ctnetlink_flush_conntrack(net, cda,
1257 NETLINK_CB(skb).portid,
1264 h = nf_conntrack_find_get(net, &zone, &tuple);
1268 ct = nf_ct_tuplehash_to_ctrack(h);
1271 __be32 id = nla_get_be32(cda[CTA_ID]);
1273 if (id != (__force __be32)nf_ct_get_id(ct)) {
1279 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1285 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1286 struct sk_buff *skb,
1287 const struct nlmsghdr *nlh,
1288 const struct nlattr * const cda[],
1289 struct netlink_ext_ack *extack)
1291 struct nf_conntrack_tuple_hash *h;
1292 struct nf_conntrack_tuple tuple;
1294 struct sk_buff *skb2 = NULL;
1295 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1296 u_int8_t u3 = nfmsg->nfgen_family;
1297 struct nf_conntrack_zone zone;
1300 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1301 struct netlink_dump_control c = {
1302 .start = ctnetlink_start,
1303 .dump = ctnetlink_dump_table,
1304 .done = ctnetlink_done,
1305 .data = (void *)cda,
1308 return netlink_dump_start(ctnl, skb, nlh, &c);
1311 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1315 if (cda[CTA_TUPLE_ORIG])
1316 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1318 else if (cda[CTA_TUPLE_REPLY])
1319 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1327 h = nf_conntrack_find_get(net, &zone, &tuple);
1331 ct = nf_ct_tuplehash_to_ctrack(h);
1334 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1341 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1342 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1348 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1357 /* this avoids a loop in nfnetlink. */
1358 return err == -EAGAIN ? -ENOBUFS : err;
1361 static int ctnetlink_done_list(struct netlink_callback *cb)
1364 nf_ct_put((struct nf_conn *)cb->args[1]);
1369 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1371 struct nf_conn *ct, *last;
1372 struct nf_conntrack_tuple_hash *h;
1373 struct hlist_nulls_node *n;
1374 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1375 u_int8_t l3proto = nfmsg->nfgen_family;
1378 struct hlist_nulls_head *list;
1379 struct net *net = sock_net(skb->sk);
1384 last = (struct nf_conn *)cb->args[1];
1386 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1387 struct ct_pcpu *pcpu;
1389 if (!cpu_possible(cpu))
1392 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1393 spin_lock_bh(&pcpu->lock);
1394 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1396 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1397 ct = nf_ct_tuplehash_to_ctrack(h);
1398 if (l3proto && nf_ct_l3num(ct) != l3proto)
1406 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1408 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1412 if (!atomic_inc_not_zero(&ct->ct_general.use))
1415 cb->args[1] = (unsigned long)ct;
1416 spin_unlock_bh(&pcpu->lock);
1424 spin_unlock_bh(&pcpu->lock);
1435 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1437 return ctnetlink_dump_list(skb, cb, true);
1440 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1441 struct sk_buff *skb,
1442 const struct nlmsghdr *nlh,
1443 const struct nlattr * const cda[],
1444 struct netlink_ext_ack *extack)
1446 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1447 struct netlink_dump_control c = {
1448 .dump = ctnetlink_dump_dying,
1449 .done = ctnetlink_done_list,
1451 return netlink_dump_start(ctnl, skb, nlh, &c);
1458 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1460 return ctnetlink_dump_list(skb, cb, false);
1463 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1464 struct sk_buff *skb,
1465 const struct nlmsghdr *nlh,
1466 const struct nlattr * const cda[],
1467 struct netlink_ext_ack *extack)
1469 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1470 struct netlink_dump_control c = {
1471 .dump = ctnetlink_dump_unconfirmed,
1472 .done = ctnetlink_done_list,
1474 return netlink_dump_start(ctnl, skb, nlh, &c);
1480 #ifdef CONFIG_NF_NAT_NEEDED
1482 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1483 enum nf_nat_manip_type manip,
1484 const struct nlattr *attr)
1486 struct nf_nat_hook *nat_hook;
1489 nat_hook = rcu_dereference(nf_nat_hook);
1491 #ifdef CONFIG_MODULES
1493 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1494 if (request_module("nf-nat") < 0) {
1495 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1499 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1501 nat_hook = rcu_dereference(nf_nat_hook);
1508 err = nat_hook->parse_nat_setup(ct, manip, attr);
1509 if (err == -EAGAIN) {
1510 #ifdef CONFIG_MODULES
1512 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1513 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1514 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1518 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1529 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1534 /* Ignore these unchangable bits */
1535 on &= ~IPS_UNCHANGEABLE_MASK;
1536 off &= ~IPS_UNCHANGEABLE_MASK;
1538 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1539 if (on & (1 << bit))
1540 set_bit(bit, &ct->status);
1541 else if (off & (1 << bit))
1542 clear_bit(bit, &ct->status);
1547 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1550 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1551 d = ct->status ^ status;
1553 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1557 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1558 /* SEEN_REPLY bit can only be set */
1561 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1562 /* ASSURED bit can only be set */
1565 __ctnetlink_change_status(ct, status, 0);
1570 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1572 #ifdef CONFIG_NF_NAT_NEEDED
1575 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1578 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1583 return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1586 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1592 static int ctnetlink_change_helper(struct nf_conn *ct,
1593 const struct nlattr * const cda[])
1595 struct nf_conntrack_helper *helper;
1596 struct nf_conn_help *help = nfct_help(ct);
1597 char *helpname = NULL;
1598 struct nlattr *helpinfo = NULL;
1601 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1605 /* don't change helper of sibling connections */
1607 /* If we try to change the helper to the same thing twice,
1608 * treat the second attempt as a no-op instead of returning
1614 helper = rcu_dereference(help->helper);
1615 if (helper && !strcmp(helper->name, helpname))
1623 if (!strcmp(helpname, "")) {
1624 if (help && help->helper) {
1625 /* we had a helper before ... */
1626 nf_ct_remove_expectations(ct);
1627 RCU_INIT_POINTER(help->helper, NULL);
1634 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1635 nf_ct_protonum(ct));
1636 if (helper == NULL) {
1642 if (help->helper == helper) {
1643 /* update private helper data if allowed. */
1644 if (helper->from_nlattr)
1645 helper->from_nlattr(helpinfo, ct);
1650 /* we cannot set a helper for an existing conntrack */
1658 static int ctnetlink_change_timeout(struct nf_conn *ct,
1659 const struct nlattr * const cda[])
1661 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1663 if (timeout > INT_MAX)
1665 ct->timeout = nfct_time_stamp + (u32)timeout;
1667 if (test_bit(IPS_DYING_BIT, &ct->status))
1673 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1674 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1675 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1676 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1679 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1680 const struct nlattr * const cda[])
1682 const struct nlattr *attr = cda[CTA_PROTOINFO];
1683 const struct nf_conntrack_l4proto *l4proto;
1684 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1687 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1693 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1694 if (l4proto->from_nlattr)
1695 err = l4proto->from_nlattr(tb, ct);
1701 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1702 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1703 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1704 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1707 static int change_seq_adj(struct nf_ct_seqadj *seq,
1708 const struct nlattr * const attr)
1711 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1713 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1717 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1720 seq->correction_pos =
1721 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1723 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1726 seq->offset_before =
1727 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1729 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1733 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1739 ctnetlink_change_seq_adj(struct nf_conn *ct,
1740 const struct nlattr * const cda[])
1742 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1748 spin_lock_bh(&ct->lock);
1749 if (cda[CTA_SEQ_ADJ_ORIG]) {
1750 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1751 cda[CTA_SEQ_ADJ_ORIG]);
1755 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1758 if (cda[CTA_SEQ_ADJ_REPLY]) {
1759 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1760 cda[CTA_SEQ_ADJ_REPLY]);
1764 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1767 spin_unlock_bh(&ct->lock);
1770 spin_unlock_bh(&ct->lock);
1774 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
1775 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 },
1776 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 },
1777 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 },
1780 static int ctnetlink_change_synproxy(struct nf_conn *ct,
1781 const struct nlattr * const cda[])
1783 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
1784 struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
1790 err = nla_parse_nested(tb, CTA_SYNPROXY_MAX, cda[CTA_SYNPROXY],
1791 synproxy_policy, NULL);
1795 if (!tb[CTA_SYNPROXY_ISN] ||
1796 !tb[CTA_SYNPROXY_ITS] ||
1797 !tb[CTA_SYNPROXY_TSOFF])
1800 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
1801 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
1802 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
1808 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1810 #ifdef CONFIG_NF_CONNTRACK_LABELS
1811 size_t len = nla_len(cda[CTA_LABELS]);
1812 const void *mask = cda[CTA_LABELS_MASK];
1814 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1818 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1819 nla_len(cda[CTA_LABELS_MASK]) != len)
1821 mask = nla_data(cda[CTA_LABELS_MASK]);
1826 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1833 ctnetlink_change_conntrack(struct nf_conn *ct,
1834 const struct nlattr * const cda[])
1838 /* only allow NAT changes and master assignation for new conntracks */
1839 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1842 if (cda[CTA_HELP]) {
1843 err = ctnetlink_change_helper(ct, cda);
1848 if (cda[CTA_TIMEOUT]) {
1849 err = ctnetlink_change_timeout(ct, cda);
1854 if (cda[CTA_STATUS]) {
1855 err = ctnetlink_change_status(ct, cda);
1860 if (cda[CTA_PROTOINFO]) {
1861 err = ctnetlink_change_protoinfo(ct, cda);
1866 #if defined(CONFIG_NF_CONNTRACK_MARK)
1868 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1871 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1872 err = ctnetlink_change_seq_adj(ct, cda);
1877 if (cda[CTA_SYNPROXY]) {
1878 err = ctnetlink_change_synproxy(ct, cda);
1883 if (cda[CTA_LABELS]) {
1884 err = ctnetlink_attach_labels(ct, cda);
1892 static struct nf_conn *
1893 ctnetlink_create_conntrack(struct net *net,
1894 const struct nf_conntrack_zone *zone,
1895 const struct nlattr * const cda[],
1896 struct nf_conntrack_tuple *otuple,
1897 struct nf_conntrack_tuple *rtuple,
1902 struct nf_conntrack_helper *helper;
1903 struct nf_conn_tstamp *tstamp;
1906 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1908 return ERR_PTR(-ENOMEM);
1910 if (!cda[CTA_TIMEOUT])
1913 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1914 if (timeout > INT_MAX)
1916 ct->timeout = (u32)timeout + nfct_time_stamp;
1919 if (cda[CTA_HELP]) {
1920 char *helpname = NULL;
1921 struct nlattr *helpinfo = NULL;
1923 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1927 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1928 nf_ct_protonum(ct));
1929 if (helper == NULL) {
1931 #ifdef CONFIG_MODULES
1932 if (request_module("nfct-helper-%s", helpname) < 0) {
1938 helper = __nf_conntrack_helper_find(helpname,
1940 nf_ct_protonum(ct));
1950 struct nf_conn_help *help;
1952 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1957 /* set private helper data if allowed. */
1958 if (helper->from_nlattr)
1959 helper->from_nlattr(helpinfo, ct);
1961 /* not in hash table yet so not strictly necessary */
1962 RCU_INIT_POINTER(help->helper, helper);
1965 /* try an implicit helper assignation */
1966 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1971 err = ctnetlink_setup_nat(ct, cda);
1975 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1976 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1977 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1978 nf_ct_labels_ext_add(ct);
1979 nfct_seqadj_ext_add(ct);
1980 nfct_synproxy_ext_add(ct);
1982 /* we must add conntrack extensions before confirmation. */
1983 ct->status |= IPS_CONFIRMED;
1985 if (cda[CTA_STATUS]) {
1986 err = ctnetlink_change_status(ct, cda);
1991 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1992 err = ctnetlink_change_seq_adj(ct, cda);
1997 memset(&ct->proto, 0, sizeof(ct->proto));
1998 if (cda[CTA_PROTOINFO]) {
1999 err = ctnetlink_change_protoinfo(ct, cda);
2004 if (cda[CTA_SYNPROXY]) {
2005 err = ctnetlink_change_synproxy(ct, cda);
2010 #if defined(CONFIG_NF_CONNTRACK_MARK)
2012 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2015 /* setup master conntrack: this is a confirmed expectation */
2016 if (cda[CTA_TUPLE_MASTER]) {
2017 struct nf_conntrack_tuple master;
2018 struct nf_conntrack_tuple_hash *master_h;
2019 struct nf_conn *master_ct;
2021 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2026 master_h = nf_conntrack_find_get(net, zone, &master);
2027 if (master_h == NULL) {
2031 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2032 __set_bit(IPS_EXPECTED_BIT, &ct->status);
2033 ct->master = master_ct;
2035 tstamp = nf_conn_tstamp_find(ct);
2037 tstamp->start = ktime_get_real_ns();
2039 err = nf_conntrack_hash_check_insert(ct);
2049 nf_ct_put(ct->master);
2053 nf_conntrack_free(ct);
2054 return ERR_PTR(err);
2057 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
2058 struct sk_buff *skb,
2059 const struct nlmsghdr *nlh,
2060 const struct nlattr * const cda[],
2061 struct netlink_ext_ack *extack)
2063 struct nf_conntrack_tuple otuple, rtuple;
2064 struct nf_conntrack_tuple_hash *h = NULL;
2065 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2067 u_int8_t u3 = nfmsg->nfgen_family;
2068 struct nf_conntrack_zone zone;
2071 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2075 if (cda[CTA_TUPLE_ORIG]) {
2076 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2082 if (cda[CTA_TUPLE_REPLY]) {
2083 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2089 if (cda[CTA_TUPLE_ORIG])
2090 h = nf_conntrack_find_get(net, &zone, &otuple);
2091 else if (cda[CTA_TUPLE_REPLY])
2092 h = nf_conntrack_find_get(net, &zone, &rtuple);
2096 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2097 enum ip_conntrack_events events;
2099 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2101 if (otuple.dst.protonum != rtuple.dst.protonum)
2104 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
2110 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2111 events = 1 << IPCT_RELATED;
2113 events = 1 << IPCT_NEW;
2115 if (cda[CTA_LABELS] &&
2116 ctnetlink_attach_labels(ct, cda) == 0)
2117 events |= (1 << IPCT_LABEL);
2119 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2120 (1 << IPCT_ASSURED) |
2121 (1 << IPCT_HELPER) |
2122 (1 << IPCT_PROTOINFO) |
2123 (1 << IPCT_SEQADJ) |
2125 (1 << IPCT_SYNPROXY) |
2127 ct, NETLINK_CB(skb).portid,
2134 /* implicit 'else' */
2137 ct = nf_ct_tuplehash_to_ctrack(h);
2138 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2139 err = ctnetlink_change_conntrack(ct, cda);
2141 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2142 (1 << IPCT_ASSURED) |
2143 (1 << IPCT_HELPER) |
2145 (1 << IPCT_PROTOINFO) |
2146 (1 << IPCT_SEQADJ) |
2148 (1 << IPCT_SYNPROXY),
2149 ct, NETLINK_CB(skb).portid,
2159 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2160 __u16 cpu, const struct ip_conntrack_stat *st)
2162 struct nlmsghdr *nlh;
2163 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2165 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2166 IPCTNL_MSG_CT_GET_STATS_CPU);
2167 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
2168 NFNETLINK_V0, htons(cpu));
2172 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2173 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2174 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2175 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2176 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2177 htonl(st->insert_failed)) ||
2178 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2179 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2180 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2181 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2182 htonl(st->search_restart)))
2183 goto nla_put_failure;
2185 nlmsg_end(skb, nlh);
2190 nlmsg_cancel(skb, nlh);
2195 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2198 struct net *net = sock_net(skb->sk);
2200 if (cb->args[0] == nr_cpu_ids)
2203 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2204 const struct ip_conntrack_stat *st;
2206 if (!cpu_possible(cpu))
2209 st = per_cpu_ptr(net->ct.stat, cpu);
2210 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2211 NETLINK_CB(cb->skb).portid,
2221 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2222 struct sk_buff *skb,
2223 const struct nlmsghdr *nlh,
2224 const struct nlattr * const cda[],
2225 struct netlink_ext_ack *extack)
2227 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2228 struct netlink_dump_control c = {
2229 .dump = ctnetlink_ct_stat_cpu_dump,
2231 return netlink_dump_start(ctnl, skb, nlh, &c);
2238 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2241 struct nlmsghdr *nlh;
2242 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2243 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2245 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2246 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
2251 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2252 goto nla_put_failure;
2254 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2255 goto nla_put_failure;
2257 nlmsg_end(skb, nlh);
2262 nlmsg_cancel(skb, nlh);
2266 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2267 struct sk_buff *skb, const struct nlmsghdr *nlh,
2268 const struct nlattr * const cda[],
2269 struct netlink_ext_ack *extack)
2271 struct sk_buff *skb2;
2274 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2278 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2280 NFNL_MSG_TYPE(nlh->nlmsg_type),
2285 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2294 /* this avoids a loop in nfnetlink. */
2295 return err == -EAGAIN ? -ENOBUFS : err;
2298 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2299 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2300 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2301 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2302 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2303 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2304 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2305 .len = NF_CT_HELPER_NAME_LEN - 1 },
2306 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2307 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2308 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2309 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2310 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2313 static struct nf_conntrack_expect *
2314 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2315 struct nf_conntrack_helper *helper,
2316 struct nf_conntrack_tuple *tuple,
2317 struct nf_conntrack_tuple *mask);
2319 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2321 ctnetlink_glue_build_size(const struct nf_conn *ct)
2323 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2324 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2325 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2326 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2327 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2328 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2329 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2330 + nla_total_size(0) /* CTA_PROTOINFO */
2331 + nla_total_size(0) /* CTA_HELP */
2332 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2333 + ctnetlink_secctx_size(ct)
2334 #ifdef CONFIG_NF_NAT_NEEDED
2335 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2336 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2338 #ifdef CONFIG_NF_CONNTRACK_MARK
2339 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2341 #ifdef CONFIG_NF_CONNTRACK_ZONES
2342 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2344 + ctnetlink_proto_size(ct)
2348 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2349 enum ip_conntrack_info *ctinfo)
2351 return nf_ct_get(skb, ctinfo);
2354 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2356 const struct nf_conntrack_zone *zone;
2357 struct nlattr *nest_parms;
2359 zone = nf_ct_zone(ct);
2361 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2363 goto nla_put_failure;
2364 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2365 goto nla_put_failure;
2366 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2367 NF_CT_ZONE_DIR_ORIG) < 0)
2368 goto nla_put_failure;
2369 nla_nest_end(skb, nest_parms);
2371 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2373 goto nla_put_failure;
2374 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2375 goto nla_put_failure;
2376 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2377 NF_CT_ZONE_DIR_REPL) < 0)
2378 goto nla_put_failure;
2379 nla_nest_end(skb, nest_parms);
2381 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2382 NF_CT_DEFAULT_ZONE_DIR) < 0)
2383 goto nla_put_failure;
2385 if (ctnetlink_dump_id(skb, ct) < 0)
2386 goto nla_put_failure;
2388 if (ctnetlink_dump_status(skb, ct) < 0)
2389 goto nla_put_failure;
2391 if (ctnetlink_dump_timeout(skb, ct) < 0)
2392 goto nla_put_failure;
2394 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2395 goto nla_put_failure;
2397 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2398 goto nla_put_failure;
2400 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2401 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2402 goto nla_put_failure;
2404 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2405 goto nla_put_failure;
2407 if ((ct->status & IPS_SEQ_ADJUST) &&
2408 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2409 goto nla_put_failure;
2411 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2412 goto nla_put_failure;
2414 #ifdef CONFIG_NF_CONNTRACK_MARK
2415 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2416 goto nla_put_failure;
2418 if (ctnetlink_dump_labels(skb, ct) < 0)
2419 goto nla_put_failure;
2427 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2428 enum ip_conntrack_info ctinfo,
2429 u_int16_t ct_attr, u_int16_t ct_info_attr)
2431 struct nlattr *nest_parms;
2433 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2435 goto nla_put_failure;
2437 if (__ctnetlink_glue_build(skb, ct) < 0)
2438 goto nla_put_failure;
2440 nla_nest_end(skb, nest_parms);
2442 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2443 goto nla_put_failure;
2452 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2454 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2455 unsigned long d = ct->status ^ status;
2457 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2458 /* SEEN_REPLY bit can only be set */
2461 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2462 /* ASSURED bit can only be set */
2465 /* This check is less strict than ctnetlink_change_status()
2466 * because callers often flip IPS_EXPECTED bits when sending
2467 * an NFQA_CT attribute to the kernel. So ignore the
2468 * unchangeable bits but do not error out. Also user programs
2469 * are allowed to clear the bits that they are allowed to change.
2471 __ctnetlink_change_status(ct, status, ~status);
2476 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2480 if (cda[CTA_TIMEOUT]) {
2481 err = ctnetlink_change_timeout(ct, cda);
2485 if (cda[CTA_STATUS]) {
2486 err = ctnetlink_update_status(ct, cda);
2490 if (cda[CTA_HELP]) {
2491 err = ctnetlink_change_helper(ct, cda);
2495 if (cda[CTA_LABELS]) {
2496 err = ctnetlink_attach_labels(ct, cda);
2500 #if defined(CONFIG_NF_CONNTRACK_MARK)
2501 if (cda[CTA_MARK]) {
2502 u32 mask = 0, mark, newmark;
2503 if (cda[CTA_MARK_MASK])
2504 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2506 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2507 newmark = (ct->mark & mask) ^ mark;
2508 if (newmark != ct->mark)
2516 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2518 struct nlattr *cda[CTA_MAX+1];
2521 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2525 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2528 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2529 const struct nf_conn *ct,
2530 struct nf_conntrack_tuple *tuple,
2531 struct nf_conntrack_tuple *mask)
2535 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2536 nf_ct_l3num(ct), NULL);
2540 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2541 nf_ct_l3num(ct), NULL);
2545 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2546 u32 portid, u32 report)
2548 struct nlattr *cda[CTA_EXPECT_MAX+1];
2549 struct nf_conntrack_tuple tuple, mask;
2550 struct nf_conntrack_helper *helper = NULL;
2551 struct nf_conntrack_expect *exp;
2554 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2559 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2564 if (cda[CTA_EXPECT_HELP_NAME]) {
2565 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2567 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2568 nf_ct_protonum(ct));
2573 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2574 helper, &tuple, &mask);
2576 return PTR_ERR(exp);
2578 err = nf_ct_expect_related_report(exp, portid, report);
2579 nf_ct_expect_put(exp);
2583 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2584 enum ip_conntrack_info ctinfo, int diff)
2586 if (!(ct->status & IPS_NAT_MASK))
2589 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2592 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2593 .get_ct = ctnetlink_glue_get_ct,
2594 .build_size = ctnetlink_glue_build_size,
2595 .build = ctnetlink_glue_build,
2596 .parse = ctnetlink_glue_parse,
2597 .attach_expect = ctnetlink_glue_attach_expect,
2598 .seq_adjust = ctnetlink_glue_seqadj,
2600 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2602 /***********************************************************************
2604 ***********************************************************************/
2606 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2607 const struct nf_conntrack_tuple *tuple,
2610 struct nlattr *nest_parms;
2612 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2614 goto nla_put_failure;
2615 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2616 goto nla_put_failure;
2617 nla_nest_end(skb, nest_parms);
2625 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2626 const struct nf_conntrack_tuple *tuple,
2627 const struct nf_conntrack_tuple_mask *mask)
2629 const struct nf_conntrack_l4proto *l4proto;
2630 struct nf_conntrack_tuple m;
2631 struct nlattr *nest_parms;
2634 memset(&m, 0xFF, sizeof(m));
2635 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2636 m.src.u.all = mask->src.u.all;
2637 m.src.l3num = tuple->src.l3num;
2638 m.dst.protonum = tuple->dst.protonum;
2640 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2642 goto nla_put_failure;
2645 ret = ctnetlink_dump_tuples_ip(skb, &m);
2647 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2648 tuple->dst.protonum);
2649 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2653 if (unlikely(ret < 0))
2654 goto nla_put_failure;
2656 nla_nest_end(skb, nest_parms);
2664 #if IS_ENABLED(CONFIG_NF_NAT)
2665 static const union nf_inet_addr any_addr;
2668 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2670 static __read_mostly siphash_key_t exp_id_seed;
2671 unsigned long a, b, c, d;
2673 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2675 a = (unsigned long)exp;
2676 b = (unsigned long)exp->helper;
2677 c = (unsigned long)exp->master;
2678 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2681 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2683 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2688 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2689 const struct nf_conntrack_expect *exp)
2691 struct nf_conn *master = exp->master;
2692 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2693 struct nf_conn_help *help;
2694 #ifdef CONFIG_NF_NAT_NEEDED
2695 struct nlattr *nest_parms;
2696 struct nf_conntrack_tuple nat_tuple = {};
2698 struct nf_ct_helper_expectfn *expfn;
2703 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2704 goto nla_put_failure;
2705 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2706 goto nla_put_failure;
2707 if (ctnetlink_exp_dump_tuple(skb,
2708 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2709 CTA_EXPECT_MASTER) < 0)
2710 goto nla_put_failure;
2712 #ifdef CONFIG_NF_NAT_NEEDED
2713 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2714 exp->saved_proto.all) {
2715 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2717 goto nla_put_failure;
2719 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2720 goto nla_put_failure;
2722 nat_tuple.src.l3num = nf_ct_l3num(master);
2723 nat_tuple.src.u3 = exp->saved_addr;
2724 nat_tuple.dst.protonum = nf_ct_protonum(master);
2725 nat_tuple.src.u = exp->saved_proto;
2727 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2728 CTA_EXPECT_NAT_TUPLE) < 0)
2729 goto nla_put_failure;
2730 nla_nest_end(skb, nest_parms);
2733 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2734 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
2735 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2736 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2737 goto nla_put_failure;
2738 help = nfct_help(master);
2740 struct nf_conntrack_helper *helper;
2742 helper = rcu_dereference(help->helper);
2744 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2745 goto nla_put_failure;
2747 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2748 if (expfn != NULL &&
2749 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2750 goto nla_put_failure;
2759 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2760 int event, const struct nf_conntrack_expect *exp)
2762 struct nlmsghdr *nlh;
2763 unsigned int flags = portid ? NLM_F_MULTI : 0;
2765 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2766 nlh = nfnl_msg_put(skb, portid, seq, event, flags,
2767 exp->tuple.src.l3num, NFNETLINK_V0, 0);
2771 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2772 goto nla_put_failure;
2774 nlmsg_end(skb, nlh);
2779 nlmsg_cancel(skb, nlh);
2783 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2785 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2787 struct nf_conntrack_expect *exp = item->exp;
2788 struct net *net = nf_ct_exp_net(exp);
2789 struct nlmsghdr *nlh;
2790 struct sk_buff *skb;
2791 unsigned int type, group;
2794 if (events & (1 << IPEXP_DESTROY)) {
2795 type = IPCTNL_MSG_EXP_DELETE;
2796 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2797 } else if (events & (1 << IPEXP_NEW)) {
2798 type = IPCTNL_MSG_EXP_NEW;
2799 flags = NLM_F_CREATE|NLM_F_EXCL;
2800 group = NFNLGRP_CONNTRACK_EXP_NEW;
2804 if (!item->report && !nfnetlink_has_listeners(net, group))
2807 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2811 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2812 nlh = nfnl_msg_put(skb, item->portid, 0, type, flags,
2813 exp->tuple.src.l3num, NFNETLINK_V0, 0);
2817 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2818 goto nla_put_failure;
2820 nlmsg_end(skb, nlh);
2821 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2825 nlmsg_cancel(skb, nlh);
2829 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2833 static int ctnetlink_exp_done(struct netlink_callback *cb)
2836 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2841 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2843 struct net *net = sock_net(skb->sk);
2844 struct nf_conntrack_expect *exp, *last;
2845 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2846 u_int8_t l3proto = nfmsg->nfgen_family;
2849 last = (struct nf_conntrack_expect *)cb->args[1];
2850 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2852 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2854 if (l3proto && exp->tuple.src.l3num != l3proto)
2857 if (!net_eq(nf_ct_net(exp->master), net))
2865 if (ctnetlink_exp_fill_info(skb,
2866 NETLINK_CB(cb->skb).portid,
2870 if (!refcount_inc_not_zero(&exp->use))
2872 cb->args[1] = (unsigned long)exp;
2884 nf_ct_expect_put(last);
2890 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2892 struct nf_conntrack_expect *exp, *last;
2893 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2894 struct nf_conn *ct = cb->data;
2895 struct nf_conn_help *help = nfct_help(ct);
2896 u_int8_t l3proto = nfmsg->nfgen_family;
2902 last = (struct nf_conntrack_expect *)cb->args[1];
2904 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2905 if (l3proto && exp->tuple.src.l3num != l3proto)
2912 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2916 if (!refcount_inc_not_zero(&exp->use))
2918 cb->args[1] = (unsigned long)exp;
2930 nf_ct_expect_put(last);
2935 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2936 struct sk_buff *skb,
2937 const struct nlmsghdr *nlh,
2938 const struct nlattr * const cda[],
2939 struct netlink_ext_ack *extack)
2942 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2943 u_int8_t u3 = nfmsg->nfgen_family;
2944 struct nf_conntrack_tuple tuple;
2945 struct nf_conntrack_tuple_hash *h;
2947 struct nf_conntrack_zone zone;
2948 struct netlink_dump_control c = {
2949 .dump = ctnetlink_exp_ct_dump_table,
2950 .done = ctnetlink_exp_done,
2953 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2958 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2962 h = nf_conntrack_find_get(net, &zone, &tuple);
2966 ct = nf_ct_tuplehash_to_ctrack(h);
2967 /* No expectation linked to this connection tracking. */
2968 if (!nfct_help(ct)) {
2975 err = netlink_dump_start(ctnl, skb, nlh, &c);
2981 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2982 struct sk_buff *skb, const struct nlmsghdr *nlh,
2983 const struct nlattr * const cda[],
2984 struct netlink_ext_ack *extack)
2986 struct nf_conntrack_tuple tuple;
2987 struct nf_conntrack_expect *exp;
2988 struct sk_buff *skb2;
2989 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2990 u_int8_t u3 = nfmsg->nfgen_family;
2991 struct nf_conntrack_zone zone;
2994 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2995 if (cda[CTA_EXPECT_MASTER])
2996 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
2999 struct netlink_dump_control c = {
3000 .dump = ctnetlink_exp_dump_table,
3001 .done = ctnetlink_exp_done,
3003 return netlink_dump_start(ctnl, skb, nlh, &c);
3007 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3011 if (cda[CTA_EXPECT_TUPLE])
3012 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3014 else if (cda[CTA_EXPECT_MASTER])
3015 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3023 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3027 if (cda[CTA_EXPECT_ID]) {
3028 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3030 if (id != nf_expect_get_id(exp)) {
3031 nf_ct_expect_put(exp);
3037 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3039 nf_ct_expect_put(exp);
3044 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3045 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
3047 nf_ct_expect_put(exp);
3051 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
3060 /* this avoids a loop in nfnetlink. */
3061 return err == -EAGAIN ? -ENOBUFS : err;
3064 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3066 const struct nf_conn_help *m_help;
3067 const char *name = data;
3069 m_help = nfct_help(exp->master);
3071 return strcmp(m_help->helper->name, name) == 0;
3074 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3079 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
3080 struct sk_buff *skb, const struct nlmsghdr *nlh,
3081 const struct nlattr * const cda[],
3082 struct netlink_ext_ack *extack)
3084 struct nf_conntrack_expect *exp;
3085 struct nf_conntrack_tuple tuple;
3086 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3087 u_int8_t u3 = nfmsg->nfgen_family;
3088 struct nf_conntrack_zone zone;
3091 if (cda[CTA_EXPECT_TUPLE]) {
3092 /* delete a single expect by tuple */
3093 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3097 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3102 /* bump usage count to 2 */
3103 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3107 if (cda[CTA_EXPECT_ID]) {
3108 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3109 if (ntohl(id) != (u32)(unsigned long)exp) {
3110 nf_ct_expect_put(exp);
3115 /* after list removal, usage count == 1 */
3116 spin_lock_bh(&nf_conntrack_expect_lock);
3117 if (del_timer(&exp->timeout)) {
3118 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3120 nf_ct_expect_put(exp);
3122 spin_unlock_bh(&nf_conntrack_expect_lock);
3123 /* have to put what we 'get' above.
3124 * after this line usage count == 0 */
3125 nf_ct_expect_put(exp);
3126 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3127 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3129 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3130 NETLINK_CB(skb).portid,
3133 /* This basically means we have to flush everything*/
3134 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3135 NETLINK_CB(skb).portid,
3142 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3143 const struct nlattr * const cda[])
3145 if (cda[CTA_EXPECT_TIMEOUT]) {
3146 if (!del_timer(&x->timeout))
3149 x->timeout.expires = jiffies +
3150 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3151 add_timer(&x->timeout);
3156 #if IS_ENABLED(CONFIG_NF_NAT)
3157 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3158 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3159 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3164 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3165 struct nf_conntrack_expect *exp,
3168 #ifdef CONFIG_NF_NAT_NEEDED
3169 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3170 struct nf_conntrack_tuple nat_tuple = {};
3173 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3174 exp_nat_nla_policy, NULL);
3178 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3181 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3182 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3187 exp->saved_addr = nat_tuple.src.u3;
3188 exp->saved_proto = nat_tuple.src.u;
3189 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3197 static struct nf_conntrack_expect *
3198 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3199 struct nf_conntrack_helper *helper,
3200 struct nf_conntrack_tuple *tuple,
3201 struct nf_conntrack_tuple *mask)
3203 u_int32_t class = 0;
3204 struct nf_conntrack_expect *exp;
3205 struct nf_conn_help *help;
3208 help = nfct_help(ct);
3210 return ERR_PTR(-EOPNOTSUPP);
3212 if (cda[CTA_EXPECT_CLASS] && helper) {
3213 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3214 if (class > helper->expect_class_max)
3215 return ERR_PTR(-EINVAL);
3217 exp = nf_ct_expect_alloc(ct);
3219 return ERR_PTR(-ENOMEM);
3221 if (cda[CTA_EXPECT_FLAGS]) {
3222 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3223 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3227 if (cda[CTA_EXPECT_FN]) {
3228 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3229 struct nf_ct_helper_expectfn *expfn;
3231 expfn = nf_ct_helper_expectfn_find_by_name(name);
3232 if (expfn == NULL) {
3236 exp->expectfn = expfn->expectfn;
3238 exp->expectfn = NULL;
3242 exp->helper = helper;
3243 exp->tuple = *tuple;
3244 exp->mask.src.u3 = mask->src.u3;
3245 exp->mask.src.u.all = mask->src.u.all;
3247 if (cda[CTA_EXPECT_NAT]) {
3248 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3249 exp, nf_ct_l3num(ct));
3255 nf_ct_expect_put(exp);
3256 return ERR_PTR(err);
3260 ctnetlink_create_expect(struct net *net,
3261 const struct nf_conntrack_zone *zone,
3262 const struct nlattr * const cda[],
3263 u_int8_t u3, u32 portid, int report)
3265 struct nf_conntrack_tuple tuple, mask, master_tuple;
3266 struct nf_conntrack_tuple_hash *h = NULL;
3267 struct nf_conntrack_helper *helper = NULL;
3268 struct nf_conntrack_expect *exp;
3272 /* caller guarantees that those three CTA_EXPECT_* exist */
3273 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3277 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3281 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3286 /* Look for master conntrack of this expectation */
3287 h = nf_conntrack_find_get(net, zone, &master_tuple);
3290 ct = nf_ct_tuplehash_to_ctrack(h);
3293 if (cda[CTA_EXPECT_HELP_NAME]) {
3294 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3296 helper = __nf_conntrack_helper_find(helpname, u3,
3297 nf_ct_protonum(ct));
3298 if (helper == NULL) {
3300 #ifdef CONFIG_MODULES
3301 if (request_module("nfct-helper-%s", helpname) < 0) {
3306 helper = __nf_conntrack_helper_find(helpname, u3,
3307 nf_ct_protonum(ct));
3319 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3325 err = nf_ct_expect_related_report(exp, portid, report);
3326 nf_ct_expect_put(exp);
3334 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3335 struct sk_buff *skb, const struct nlmsghdr *nlh,
3336 const struct nlattr * const cda[],
3337 struct netlink_ext_ack *extack)
3339 struct nf_conntrack_tuple tuple;
3340 struct nf_conntrack_expect *exp;
3341 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3342 u_int8_t u3 = nfmsg->nfgen_family;
3343 struct nf_conntrack_zone zone;
3346 if (!cda[CTA_EXPECT_TUPLE]
3347 || !cda[CTA_EXPECT_MASK]
3348 || !cda[CTA_EXPECT_MASTER])
3351 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3355 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3360 spin_lock_bh(&nf_conntrack_expect_lock);
3361 exp = __nf_ct_expect_find(net, &zone, &tuple);
3363 spin_unlock_bh(&nf_conntrack_expect_lock);
3365 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3366 err = ctnetlink_create_expect(net, &zone, cda, u3,
3367 NETLINK_CB(skb).portid,
3374 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3375 err = ctnetlink_change_expect(exp, cda);
3376 spin_unlock_bh(&nf_conntrack_expect_lock);
3382 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3383 const struct ip_conntrack_stat *st)
3385 struct nlmsghdr *nlh;
3386 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3388 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3389 IPCTNL_MSG_EXP_GET_STATS_CPU);
3390 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
3391 NFNETLINK_V0, htons(cpu));
3395 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3396 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3397 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3398 goto nla_put_failure;
3400 nlmsg_end(skb, nlh);
3405 nlmsg_cancel(skb, nlh);
3410 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3413 struct net *net = sock_net(skb->sk);
3415 if (cb->args[0] == nr_cpu_ids)
3418 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3419 const struct ip_conntrack_stat *st;
3421 if (!cpu_possible(cpu))
3424 st = per_cpu_ptr(net->ct.stat, cpu);
3425 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3435 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3436 struct sk_buff *skb,
3437 const struct nlmsghdr *nlh,
3438 const struct nlattr * const cda[],
3439 struct netlink_ext_ack *extack)
3441 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3442 struct netlink_dump_control c = {
3443 .dump = ctnetlink_exp_stat_cpu_dump,
3445 return netlink_dump_start(ctnl, skb, nlh, &c);
3451 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3452 static struct nf_ct_event_notifier ctnl_notifier = {
3453 .fcn = ctnetlink_conntrack_event,
3456 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3457 .fcn = ctnetlink_expect_event,
3461 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3462 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3463 .attr_count = CTA_MAX,
3464 .policy = ct_nla_policy },
3465 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3466 .attr_count = CTA_MAX,
3467 .policy = ct_nla_policy },
3468 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3469 .attr_count = CTA_MAX,
3470 .policy = ct_nla_policy },
3471 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3472 .attr_count = CTA_MAX,
3473 .policy = ct_nla_policy },
3474 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3475 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3476 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3477 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3480 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3481 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3482 .attr_count = CTA_EXPECT_MAX,
3483 .policy = exp_nla_policy },
3484 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3485 .attr_count = CTA_EXPECT_MAX,
3486 .policy = exp_nla_policy },
3487 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3488 .attr_count = CTA_EXPECT_MAX,
3489 .policy = exp_nla_policy },
3490 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3493 static const struct nfnetlink_subsystem ctnl_subsys = {
3494 .name = "conntrack",
3495 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3496 .cb_count = IPCTNL_MSG_MAX,
3500 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3501 .name = "conntrack_expect",
3502 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3503 .cb_count = IPCTNL_MSG_EXP_MAX,
3507 MODULE_ALIAS("ip_conntrack_netlink");
3508 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3509 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3511 static int __net_init ctnetlink_net_init(struct net *net)
3513 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3516 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3518 pr_err("ctnetlink_init: cannot register notifier.\n");
3522 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3524 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3525 goto err_unreg_notifier;
3530 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3532 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3538 static void ctnetlink_net_exit(struct net *net)
3540 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3541 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3542 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3546 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3550 list_for_each_entry(net, net_exit_list, exit_list)
3551 ctnetlink_net_exit(net);
3553 /* wait for other cpus until they are done with ctnl_notifiers */
3557 static struct pernet_operations ctnetlink_net_ops = {
3558 .init = ctnetlink_net_init,
3559 .exit_batch = ctnetlink_net_exit_batch,
3562 static int __init ctnetlink_init(void)
3566 ret = nfnetlink_subsys_register(&ctnl_subsys);
3568 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3572 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3574 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3575 goto err_unreg_subsys;
3578 ret = register_pernet_subsys(&ctnetlink_net_ops);
3580 pr_err("ctnetlink_init: cannot register pernet operations\n");
3581 goto err_unreg_exp_subsys;
3583 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3584 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3585 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3589 err_unreg_exp_subsys:
3590 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3592 nfnetlink_subsys_unregister(&ctnl_subsys);
3597 static void __exit ctnetlink_exit(void)
3599 unregister_pernet_subsys(&ctnetlink_net_ops);
3600 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3601 nfnetlink_subsys_unregister(&ctnl_subsys);
3602 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3603 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3608 module_init(ctnetlink_init);
3609 module_exit(ctnetlink_exit);