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 #if IS_ENABLED(CONFIG_NF_NAT)
50 #include <net/netfilter/nf_nat.h>
51 #include <net/netfilter/nf_nat_helper.h>
54 #include <linux/netfilter/nfnetlink.h>
55 #include <linux/netfilter/nfnetlink_conntrack.h>
57 MODULE_LICENSE("GPL");
59 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
60 const struct nf_conntrack_tuple *tuple,
61 const struct nf_conntrack_l4proto *l4proto)
64 struct nlattr *nest_parms;
66 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO);
69 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
72 if (likely(l4proto->tuple_to_nlattr))
73 ret = l4proto->tuple_to_nlattr(skb, tuple);
75 nla_nest_end(skb, nest_parms);
83 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
84 const struct nf_conntrack_tuple *tuple)
86 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
87 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
92 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
93 const struct nf_conntrack_tuple *tuple)
95 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
96 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
101 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
102 const struct nf_conntrack_tuple *tuple)
105 struct nlattr *nest_parms;
107 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP);
109 goto nla_put_failure;
111 switch (tuple->src.l3num) {
113 ret = ipv4_tuple_to_nlattr(skb, tuple);
116 ret = ipv6_tuple_to_nlattr(skb, tuple);
120 nla_nest_end(skb, nest_parms);
128 static int ctnetlink_dump_tuples(struct sk_buff *skb,
129 const struct nf_conntrack_tuple *tuple)
131 const struct nf_conntrack_l4proto *l4proto;
135 ret = ctnetlink_dump_tuples_ip(skb, tuple);
138 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
139 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
145 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
146 const struct nf_conntrack_zone *zone, int dir)
148 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
150 if (nla_put_be16(skb, attrtype, htons(zone->id)))
151 goto nla_put_failure;
158 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
160 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
161 goto nla_put_failure;
168 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
170 long timeout = nf_ct_expires(ct) / HZ;
172 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
173 goto nla_put_failure;
180 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
182 const struct nf_conntrack_l4proto *l4proto;
183 struct nlattr *nest_proto;
186 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
187 if (!l4proto->to_nlattr)
190 nest_proto = nla_nest_start(skb, CTA_PROTOINFO);
192 goto nla_put_failure;
194 ret = l4proto->to_nlattr(skb, nest_proto, ct);
196 nla_nest_end(skb, nest_proto);
204 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
205 const struct nf_conn *ct)
207 struct nlattr *nest_helper;
208 const struct nf_conn_help *help = nfct_help(ct);
209 struct nf_conntrack_helper *helper;
214 helper = rcu_dereference(help->helper);
218 nest_helper = nla_nest_start(skb, CTA_HELP);
220 goto nla_put_failure;
221 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
222 goto nla_put_failure;
224 if (helper->to_nlattr)
225 helper->to_nlattr(skb, ct);
227 nla_nest_end(skb, nest_helper);
236 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
237 enum ip_conntrack_dir dir, int type)
239 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
240 struct nf_conn_counter *counter = acct->counter;
241 struct nlattr *nest_count;
244 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
245 pkts = atomic64_xchg(&counter[dir].packets, 0);
246 bytes = atomic64_xchg(&counter[dir].bytes, 0);
248 pkts = atomic64_read(&counter[dir].packets);
249 bytes = atomic64_read(&counter[dir].bytes);
252 nest_count = nla_nest_start(skb, attr);
254 goto nla_put_failure;
256 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
258 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
260 goto nla_put_failure;
262 nla_nest_end(skb, nest_count);
271 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
273 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
278 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
280 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
287 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
289 struct nlattr *nest_count;
290 const struct nf_conn_tstamp *tstamp;
292 tstamp = nf_conn_tstamp_find(ct);
296 nest_count = nla_nest_start(skb, CTA_TIMESTAMP);
298 goto nla_put_failure;
300 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
301 CTA_TIMESTAMP_PAD) ||
302 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
303 cpu_to_be64(tstamp->stop),
305 goto nla_put_failure;
306 nla_nest_end(skb, nest_count);
314 #ifdef CONFIG_NF_CONNTRACK_MARK
315 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
317 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
318 goto nla_put_failure;
325 #define ctnetlink_dump_mark(a, b) (0)
328 #ifdef CONFIG_NF_CONNTRACK_SECMARK
329 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
331 struct nlattr *nest_secctx;
335 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
340 nest_secctx = nla_nest_start(skb, CTA_SECCTX);
342 goto nla_put_failure;
344 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
345 goto nla_put_failure;
346 nla_nest_end(skb, nest_secctx);
350 security_release_secctx(secctx, len);
354 #define ctnetlink_dump_secctx(a, b) (0)
357 #ifdef CONFIG_NF_CONNTRACK_LABELS
358 static inline int ctnetlink_label_size(const struct nf_conn *ct)
360 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
364 return nla_total_size(sizeof(labels->bits));
368 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
370 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
378 if (labels->bits[i] != 0)
379 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
382 } while (i < ARRAY_SIZE(labels->bits));
387 #define ctnetlink_dump_labels(a, b) (0)
388 #define ctnetlink_label_size(a) (0)
391 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
393 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
395 struct nlattr *nest_parms;
397 if (!(ct->status & IPS_EXPECTED))
400 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER);
402 goto nla_put_failure;
403 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
404 goto nla_put_failure;
405 nla_nest_end(skb, nest_parms);
414 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
416 struct nlattr *nest_parms;
418 nest_parms = nla_nest_start(skb, type);
420 goto nla_put_failure;
422 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
423 htonl(seq->correction_pos)) ||
424 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
425 htonl(seq->offset_before)) ||
426 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
427 htonl(seq->offset_after)))
428 goto nla_put_failure;
430 nla_nest_end(skb, nest_parms);
438 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
440 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
441 struct nf_ct_seqadj *seq;
443 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
446 spin_lock_bh(&ct->lock);
447 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
448 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
451 seq = &seqadj->seq[IP_CT_DIR_REPLY];
452 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
455 spin_unlock_bh(&ct->lock);
458 spin_unlock_bh(&ct->lock);
462 static int ctnetlink_dump_ct_synproxy(struct sk_buff *skb, struct nf_conn *ct)
464 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
465 struct nlattr *nest_parms;
470 nest_parms = nla_nest_start(skb, CTA_SYNPROXY);
472 goto nla_put_failure;
474 if (nla_put_be32(skb, CTA_SYNPROXY_ISN, htonl(synproxy->isn)) ||
475 nla_put_be32(skb, CTA_SYNPROXY_ITS, htonl(synproxy->its)) ||
476 nla_put_be32(skb, CTA_SYNPROXY_TSOFF, htonl(synproxy->tsoff)))
477 goto nla_put_failure;
479 nla_nest_end(skb, nest_parms);
487 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
489 __be32 id = (__force __be32)nf_ct_get_id(ct);
491 if (nla_put_be32(skb, CTA_ID, id))
492 goto nla_put_failure;
499 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
501 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
502 goto nla_put_failure;
509 /* all these functions access ct->ext. Caller must either hold a reference
510 * on ct or prevent its deletion by holding either the bucket spinlock or
511 * pcpu dying list lock.
513 static int ctnetlink_dump_extinfo(struct sk_buff *skb,
514 struct nf_conn *ct, u32 type)
516 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
517 ctnetlink_dump_timestamp(skb, ct) < 0 ||
518 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
519 ctnetlink_dump_labels(skb, ct) < 0 ||
520 ctnetlink_dump_ct_seq_adj(skb, ct) < 0 ||
521 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
527 static int ctnetlink_dump_info(struct sk_buff *skb, struct nf_conn *ct)
529 if (ctnetlink_dump_status(skb, ct) < 0 ||
530 ctnetlink_dump_mark(skb, ct) < 0 ||
531 ctnetlink_dump_secctx(skb, ct) < 0 ||
532 ctnetlink_dump_id(skb, ct) < 0 ||
533 ctnetlink_dump_use(skb, ct) < 0 ||
534 ctnetlink_dump_master(skb, ct) < 0)
537 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status) &&
538 (ctnetlink_dump_timeout(skb, ct) < 0 ||
539 ctnetlink_dump_protoinfo(skb, ct) < 0))
546 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
547 struct nf_conn *ct, bool extinfo)
549 const struct nf_conntrack_zone *zone;
550 struct nlmsghdr *nlh;
551 struct nfgenmsg *nfmsg;
552 struct nlattr *nest_parms;
553 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
555 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
556 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
560 nfmsg = nlmsg_data(nlh);
561 nfmsg->nfgen_family = nf_ct_l3num(ct);
562 nfmsg->version = NFNETLINK_V0;
565 zone = nf_ct_zone(ct);
567 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
569 goto nla_put_failure;
570 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
571 goto nla_put_failure;
572 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
573 NF_CT_ZONE_DIR_ORIG) < 0)
574 goto nla_put_failure;
575 nla_nest_end(skb, nest_parms);
577 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
579 goto nla_put_failure;
580 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
581 goto nla_put_failure;
582 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
583 NF_CT_ZONE_DIR_REPL) < 0)
584 goto nla_put_failure;
585 nla_nest_end(skb, nest_parms);
587 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
588 NF_CT_DEFAULT_ZONE_DIR) < 0)
589 goto nla_put_failure;
591 if (ctnetlink_dump_info(skb, ct) < 0)
592 goto nla_put_failure;
593 if (extinfo && ctnetlink_dump_extinfo(skb, ct, type) < 0)
594 goto nla_put_failure;
601 nlmsg_cancel(skb, nlh);
605 static const struct nla_policy cta_ip_nla_policy[CTA_IP_MAX + 1] = {
606 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
607 [CTA_IP_V4_DST] = { .type = NLA_U32 },
608 [CTA_IP_V6_SRC] = { .len = sizeof(__be32) * 4 },
609 [CTA_IP_V6_DST] = { .len = sizeof(__be32) * 4 },
612 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
613 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
615 const struct nf_conntrack_l4proto *l4proto;
616 size_t len, len4 = 0;
618 len = nla_policy_len(cta_ip_nla_policy, CTA_IP_MAX + 1);
619 len *= 3u; /* ORIG, REPLY, MASTER */
621 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
622 len += l4proto->nlattr_size;
623 if (l4proto->nlattr_tuple_size) {
624 len4 = l4proto->nlattr_tuple_size();
625 len4 *= 3u; /* ORIG, REPLY, MASTER */
632 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
634 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
636 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
637 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
638 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
642 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
644 #ifdef CONFIG_NF_CONNTRACK_SECMARK
647 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
651 return nla_total_size(0) /* CTA_SECCTX */
652 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
658 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
660 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
661 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
663 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
669 #ifdef CONFIG_NF_CONNTRACK_EVENTS
670 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
672 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
673 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
674 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
675 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
676 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
677 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
678 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
679 + ctnetlink_acct_size(ct)
680 + ctnetlink_timestamp_size(ct)
681 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
682 + nla_total_size(0) /* CTA_PROTOINFO */
683 + nla_total_size(0) /* CTA_HELP */
684 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
685 + ctnetlink_secctx_size(ct)
686 #if IS_ENABLED(CONFIG_NF_NAT)
687 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
688 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
690 #ifdef CONFIG_NF_CONNTRACK_MARK
691 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
693 #ifdef CONFIG_NF_CONNTRACK_ZONES
694 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
696 + ctnetlink_proto_size(ct)
697 + ctnetlink_label_size(ct)
702 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
704 const struct nf_conntrack_zone *zone;
706 struct nlmsghdr *nlh;
707 struct nfgenmsg *nfmsg;
708 struct nlattr *nest_parms;
709 struct nf_conn *ct = item->ct;
712 unsigned int flags = 0, group;
715 if (events & (1 << IPCT_DESTROY)) {
716 type = IPCTNL_MSG_CT_DELETE;
717 group = NFNLGRP_CONNTRACK_DESTROY;
718 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
719 type = IPCTNL_MSG_CT_NEW;
720 flags = NLM_F_CREATE|NLM_F_EXCL;
721 group = NFNLGRP_CONNTRACK_NEW;
723 type = IPCTNL_MSG_CT_NEW;
724 group = NFNLGRP_CONNTRACK_UPDATE;
729 if (!item->report && !nfnetlink_has_listeners(net, group))
732 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
736 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
737 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
741 nfmsg = nlmsg_data(nlh);
742 nfmsg->nfgen_family = nf_ct_l3num(ct);
743 nfmsg->version = NFNETLINK_V0;
746 zone = nf_ct_zone(ct);
748 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
750 goto nla_put_failure;
751 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
752 goto nla_put_failure;
753 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
754 NF_CT_ZONE_DIR_ORIG) < 0)
755 goto nla_put_failure;
756 nla_nest_end(skb, nest_parms);
758 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
760 goto nla_put_failure;
761 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
762 goto nla_put_failure;
763 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
764 NF_CT_ZONE_DIR_REPL) < 0)
765 goto nla_put_failure;
766 nla_nest_end(skb, nest_parms);
768 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
769 NF_CT_DEFAULT_ZONE_DIR) < 0)
770 goto nla_put_failure;
772 if (ctnetlink_dump_id(skb, ct) < 0)
773 goto nla_put_failure;
775 if (ctnetlink_dump_status(skb, ct) < 0)
776 goto nla_put_failure;
778 if (events & (1 << IPCT_DESTROY)) {
779 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
780 ctnetlink_dump_timestamp(skb, ct) < 0)
781 goto nla_put_failure;
783 if (ctnetlink_dump_timeout(skb, ct) < 0)
784 goto nla_put_failure;
786 if (events & (1 << IPCT_PROTOINFO)
787 && ctnetlink_dump_protoinfo(skb, ct) < 0)
788 goto nla_put_failure;
790 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
791 && ctnetlink_dump_helpinfo(skb, ct) < 0)
792 goto nla_put_failure;
794 #ifdef CONFIG_NF_CONNTRACK_SECMARK
795 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
796 && ctnetlink_dump_secctx(skb, ct) < 0)
797 goto nla_put_failure;
799 if (events & (1 << IPCT_LABEL) &&
800 ctnetlink_dump_labels(skb, ct) < 0)
801 goto nla_put_failure;
803 if (events & (1 << IPCT_RELATED) &&
804 ctnetlink_dump_master(skb, ct) < 0)
805 goto nla_put_failure;
807 if (events & (1 << IPCT_SEQADJ) &&
808 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
809 goto nla_put_failure;
811 if (events & (1 << IPCT_SYNPROXY) &&
812 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
813 goto nla_put_failure;
816 #ifdef CONFIG_NF_CONNTRACK_MARK
817 if ((events & (1 << IPCT_MARK) || ct->mark)
818 && ctnetlink_dump_mark(skb, ct) < 0)
819 goto nla_put_failure;
822 err = nfnetlink_send(skb, net, item->portid, group, item->report,
824 if (err == -ENOBUFS || err == -EAGAIN)
830 nlmsg_cancel(skb, nlh);
834 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
839 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
841 static int ctnetlink_done(struct netlink_callback *cb)
844 nf_ct_put((struct nf_conn *)cb->args[1]);
849 struct ctnetlink_filter {
857 static struct ctnetlink_filter *
858 ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
860 struct ctnetlink_filter *filter;
862 #ifndef CONFIG_NF_CONNTRACK_MARK
863 if (cda[CTA_MARK] || cda[CTA_MARK_MASK])
864 return ERR_PTR(-EOPNOTSUPP);
867 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
869 return ERR_PTR(-ENOMEM);
871 filter->family = family;
873 #ifdef CONFIG_NF_CONNTRACK_MARK
874 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
875 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
876 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
882 static int ctnetlink_start(struct netlink_callback *cb)
884 const struct nlattr * const *cda = cb->data;
885 struct ctnetlink_filter *filter = NULL;
886 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
887 u8 family = nfmsg->nfgen_family;
889 if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
890 filter = ctnetlink_alloc_filter(cda, family);
892 return PTR_ERR(filter);
899 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
901 struct ctnetlink_filter *filter = data;
906 /* Match entries of a given L3 protocol number.
907 * If it is not specified, ie. l3proto == 0,
908 * then match everything.
910 if (filter->family && nf_ct_l3num(ct) != filter->family)
913 #ifdef CONFIG_NF_CONNTRACK_MARK
914 if ((ct->mark & filter->mark.mask) != filter->mark.val)
926 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
928 struct net *net = sock_net(skb->sk);
929 struct nf_conn *ct, *last;
930 struct nf_conntrack_tuple_hash *h;
931 struct hlist_nulls_node *n;
932 struct nf_conn *nf_ct_evict[8];
936 last = (struct nf_conn *)cb->args[1];
940 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
944 if (nf_ct_should_gc(nf_ct_evict[i]))
945 nf_ct_kill(nf_ct_evict[i]);
946 nf_ct_put(nf_ct_evict[i]);
949 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
950 nf_conntrack_lock(lockp);
951 if (cb->args[0] >= nf_conntrack_htable_size) {
955 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
957 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
959 ct = nf_ct_tuplehash_to_ctrack(h);
960 if (nf_ct_is_expired(ct)) {
961 if (i < ARRAY_SIZE(nf_ct_evict) &&
962 atomic_inc_not_zero(&ct->ct_general.use))
963 nf_ct_evict[i++] = ct;
967 if (!net_eq(net, nf_ct_net(ct)))
975 if (!ctnetlink_filter_match(ct, cb->data))
979 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
981 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
984 nf_conntrack_get(&ct->ct_general);
985 cb->args[1] = (unsigned long)ct;
999 /* nf ct hash resize happened, now clear the leftover. */
1000 if ((struct nf_conn *)cb->args[1] == last)
1008 if (nf_ct_should_gc(nf_ct_evict[i]))
1009 nf_ct_kill(nf_ct_evict[i]);
1010 nf_ct_put(nf_ct_evict[i]);
1016 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
1017 struct nf_conntrack_tuple *t)
1019 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
1022 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
1023 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
1028 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
1029 struct nf_conntrack_tuple *t)
1031 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
1034 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
1035 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
1040 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
1041 struct nf_conntrack_tuple *tuple)
1043 struct nlattr *tb[CTA_IP_MAX+1];
1046 ret = nla_parse_nested_deprecated(tb, CTA_IP_MAX, attr, NULL, NULL);
1050 ret = nla_validate_nested_deprecated(attr, CTA_IP_MAX,
1051 cta_ip_nla_policy, NULL);
1055 switch (tuple->src.l3num) {
1057 ret = ipv4_nlattr_to_tuple(tb, tuple);
1060 ret = ipv6_nlattr_to_tuple(tb, tuple);
1067 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
1068 [CTA_PROTO_NUM] = { .type = NLA_U8 },
1071 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
1072 struct nf_conntrack_tuple *tuple)
1074 const struct nf_conntrack_l4proto *l4proto;
1075 struct nlattr *tb[CTA_PROTO_MAX+1];
1078 ret = nla_parse_nested_deprecated(tb, CTA_PROTO_MAX, attr,
1079 proto_nla_policy, NULL);
1083 if (!tb[CTA_PROTO_NUM])
1085 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
1088 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
1090 if (likely(l4proto->nlattr_to_tuple)) {
1091 ret = nla_validate_nested_deprecated(attr, CTA_PROTO_MAX,
1092 l4proto->nla_policy,
1095 ret = l4proto->nlattr_to_tuple(tb, tuple);
1104 ctnetlink_parse_zone(const struct nlattr *attr,
1105 struct nf_conntrack_zone *zone)
1107 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
1108 NF_CT_DEFAULT_ZONE_DIR, 0);
1109 #ifdef CONFIG_NF_CONNTRACK_ZONES
1111 zone->id = ntohs(nla_get_be16(attr));
1120 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1121 struct nf_conntrack_zone *zone)
1125 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1128 ret = ctnetlink_parse_zone(attr, zone);
1132 if (type == CTA_TUPLE_REPLY)
1133 zone->dir = NF_CT_ZONE_DIR_REPL;
1135 zone->dir = NF_CT_ZONE_DIR_ORIG;
1140 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1141 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1142 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1143 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1147 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1148 struct nf_conntrack_tuple *tuple, u32 type,
1149 u_int8_t l3num, struct nf_conntrack_zone *zone)
1151 struct nlattr *tb[CTA_TUPLE_MAX+1];
1154 memset(tuple, 0, sizeof(*tuple));
1156 err = nla_parse_nested_deprecated(tb, CTA_TUPLE_MAX, cda[type],
1157 tuple_nla_policy, NULL);
1161 if (!tb[CTA_TUPLE_IP])
1164 tuple->src.l3num = l3num;
1166 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1170 if (!tb[CTA_TUPLE_PROTO])
1173 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1177 if (tb[CTA_TUPLE_ZONE]) {
1181 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1187 /* orig and expect tuples get DIR_ORIGINAL */
1188 if (type == CTA_TUPLE_REPLY)
1189 tuple->dst.dir = IP_CT_DIR_REPLY;
1191 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1196 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1197 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1198 .len = NF_CT_HELPER_NAME_LEN - 1 },
1201 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1202 struct nlattr **helpinfo)
1205 struct nlattr *tb[CTA_HELP_MAX+1];
1207 err = nla_parse_nested_deprecated(tb, CTA_HELP_MAX, attr,
1208 help_nla_policy, NULL);
1212 if (!tb[CTA_HELP_NAME])
1215 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1217 if (tb[CTA_HELP_INFO])
1218 *helpinfo = tb[CTA_HELP_INFO];
1223 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1224 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1225 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1226 [CTA_STATUS] = { .type = NLA_U32 },
1227 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1228 [CTA_HELP] = { .type = NLA_NESTED },
1229 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1230 [CTA_TIMEOUT] = { .type = NLA_U32 },
1231 [CTA_MARK] = { .type = NLA_U32 },
1232 [CTA_ID] = { .type = NLA_U32 },
1233 [CTA_NAT_DST] = { .type = NLA_NESTED },
1234 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1235 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1236 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1237 [CTA_ZONE] = { .type = NLA_U16 },
1238 [CTA_MARK_MASK] = { .type = NLA_U32 },
1239 [CTA_LABELS] = { .type = NLA_BINARY,
1240 .len = NF_CT_LABELS_MAX_SIZE },
1241 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1242 .len = NF_CT_LABELS_MAX_SIZE },
1245 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1247 if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
1250 return ctnetlink_filter_match(ct, data);
1253 static int ctnetlink_flush_conntrack(struct net *net,
1254 const struct nlattr * const cda[],
1255 u32 portid, int report, u8 family)
1257 struct ctnetlink_filter *filter = NULL;
1259 if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
1260 filter = ctnetlink_alloc_filter(cda, family);
1262 return PTR_ERR(filter);
1265 nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
1272 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1273 struct sk_buff *skb,
1274 const struct nlmsghdr *nlh,
1275 const struct nlattr * const cda[],
1276 struct netlink_ext_ack *extack)
1278 struct nf_conntrack_tuple_hash *h;
1279 struct nf_conntrack_tuple tuple;
1281 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1282 struct nf_conntrack_zone zone;
1285 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1289 if (cda[CTA_TUPLE_ORIG])
1290 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1291 nfmsg->nfgen_family, &zone);
1292 else if (cda[CTA_TUPLE_REPLY])
1293 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1294 nfmsg->nfgen_family, &zone);
1296 u_int8_t u3 = nfmsg->version ? nfmsg->nfgen_family : AF_UNSPEC;
1298 return ctnetlink_flush_conntrack(net, cda,
1299 NETLINK_CB(skb).portid,
1300 nlmsg_report(nlh), u3);
1306 h = nf_conntrack_find_get(net, &zone, &tuple);
1310 ct = nf_ct_tuplehash_to_ctrack(h);
1312 if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) {
1318 __be32 id = nla_get_be32(cda[CTA_ID]);
1320 if (id != (__force __be32)nf_ct_get_id(ct)) {
1326 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1332 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1333 struct sk_buff *skb,
1334 const struct nlmsghdr *nlh,
1335 const struct nlattr * const cda[],
1336 struct netlink_ext_ack *extack)
1338 struct nf_conntrack_tuple_hash *h;
1339 struct nf_conntrack_tuple tuple;
1341 struct sk_buff *skb2 = NULL;
1342 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1343 u_int8_t u3 = nfmsg->nfgen_family;
1344 struct nf_conntrack_zone zone;
1347 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1348 struct netlink_dump_control c = {
1349 .start = ctnetlink_start,
1350 .dump = ctnetlink_dump_table,
1351 .done = ctnetlink_done,
1352 .data = (void *)cda,
1355 return netlink_dump_start(ctnl, skb, nlh, &c);
1358 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1362 if (cda[CTA_TUPLE_ORIG])
1363 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1365 else if (cda[CTA_TUPLE_REPLY])
1366 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1374 h = nf_conntrack_find_get(net, &zone, &tuple);
1378 ct = nf_ct_tuplehash_to_ctrack(h);
1381 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1387 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1388 NFNL_MSG_TYPE(nlh->nlmsg_type), ct, true);
1393 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1402 /* this avoids a loop in nfnetlink. */
1403 return err == -EAGAIN ? -ENOBUFS : err;
1406 static int ctnetlink_done_list(struct netlink_callback *cb)
1409 nf_ct_put((struct nf_conn *)cb->args[1]);
1414 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1416 struct nf_conn *ct, *last;
1417 struct nf_conntrack_tuple_hash *h;
1418 struct hlist_nulls_node *n;
1419 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1420 u_int8_t l3proto = nfmsg->nfgen_family;
1423 struct hlist_nulls_head *list;
1424 struct net *net = sock_net(skb->sk);
1429 last = (struct nf_conn *)cb->args[1];
1431 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1432 struct ct_pcpu *pcpu;
1434 if (!cpu_possible(cpu))
1437 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1438 spin_lock_bh(&pcpu->lock);
1439 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1441 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1442 ct = nf_ct_tuplehash_to_ctrack(h);
1443 if (l3proto && nf_ct_l3num(ct) != l3proto)
1451 /* We can't dump extension info for the unconfirmed
1452 * list because unconfirmed conntracks can have
1453 * ct->ext reallocated (and thus freed).
1455 * In the dying list case ct->ext can't be free'd
1456 * until after we drop pcpu->lock.
1458 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1460 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1461 ct, dying ? true : false);
1463 if (!atomic_inc_not_zero(&ct->ct_general.use))
1466 cb->args[1] = (unsigned long)ct;
1467 spin_unlock_bh(&pcpu->lock);
1475 spin_unlock_bh(&pcpu->lock);
1486 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1488 return ctnetlink_dump_list(skb, cb, true);
1491 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1492 struct sk_buff *skb,
1493 const struct nlmsghdr *nlh,
1494 const struct nlattr * const cda[],
1495 struct netlink_ext_ack *extack)
1497 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1498 struct netlink_dump_control c = {
1499 .dump = ctnetlink_dump_dying,
1500 .done = ctnetlink_done_list,
1502 return netlink_dump_start(ctnl, skb, nlh, &c);
1509 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1511 return ctnetlink_dump_list(skb, cb, false);
1514 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1515 struct sk_buff *skb,
1516 const struct nlmsghdr *nlh,
1517 const struct nlattr * const cda[],
1518 struct netlink_ext_ack *extack)
1520 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1521 struct netlink_dump_control c = {
1522 .dump = ctnetlink_dump_unconfirmed,
1523 .done = ctnetlink_done_list,
1525 return netlink_dump_start(ctnl, skb, nlh, &c);
1531 #if IS_ENABLED(CONFIG_NF_NAT)
1533 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1534 enum nf_nat_manip_type manip,
1535 const struct nlattr *attr)
1538 struct nf_nat_hook *nat_hook;
1541 nat_hook = rcu_dereference(nf_nat_hook);
1543 #ifdef CONFIG_MODULES
1545 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1546 if (request_module("nf-nat") < 0) {
1547 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1551 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1553 nat_hook = rcu_dereference(nf_nat_hook);
1560 err = nat_hook->parse_nat_setup(ct, manip, attr);
1561 if (err == -EAGAIN) {
1562 #ifdef CONFIG_MODULES
1564 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1565 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1566 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1570 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1581 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1586 /* Ignore these unchangable bits */
1587 on &= ~IPS_UNCHANGEABLE_MASK;
1588 off &= ~IPS_UNCHANGEABLE_MASK;
1590 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1591 if (on & (1 << bit))
1592 set_bit(bit, &ct->status);
1593 else if (off & (1 << bit))
1594 clear_bit(bit, &ct->status);
1599 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1602 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1603 d = ct->status ^ status;
1605 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1609 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1610 /* SEEN_REPLY bit can only be set */
1613 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1614 /* ASSURED bit can only be set */
1617 __ctnetlink_change_status(ct, status, 0);
1622 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1624 #if IS_ENABLED(CONFIG_NF_NAT)
1627 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1630 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1635 return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1638 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1644 static int ctnetlink_change_helper(struct nf_conn *ct,
1645 const struct nlattr * const cda[])
1647 struct nf_conntrack_helper *helper;
1648 struct nf_conn_help *help = nfct_help(ct);
1649 char *helpname = NULL;
1650 struct nlattr *helpinfo = NULL;
1653 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1657 /* don't change helper of sibling connections */
1659 /* If we try to change the helper to the same thing twice,
1660 * treat the second attempt as a no-op instead of returning
1666 helper = rcu_dereference(help->helper);
1667 if (helper && !strcmp(helper->name, helpname))
1675 if (!strcmp(helpname, "")) {
1676 if (help && help->helper) {
1677 /* we had a helper before ... */
1678 nf_ct_remove_expectations(ct);
1679 RCU_INIT_POINTER(help->helper, NULL);
1686 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1687 nf_ct_protonum(ct));
1688 if (helper == NULL) {
1694 if (help->helper == helper) {
1695 /* update private helper data if allowed. */
1696 if (helper->from_nlattr)
1697 helper->from_nlattr(helpinfo, ct);
1702 /* we cannot set a helper for an existing conntrack */
1710 static int ctnetlink_change_timeout(struct nf_conn *ct,
1711 const struct nlattr * const cda[])
1713 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1715 if (timeout > INT_MAX)
1717 ct->timeout = nfct_time_stamp + (u32)timeout;
1719 if (test_bit(IPS_DYING_BIT, &ct->status))
1725 #if defined(CONFIG_NF_CONNTRACK_MARK)
1726 static void ctnetlink_change_mark(struct nf_conn *ct,
1727 const struct nlattr * const cda[])
1729 u32 mark, newmark, mask = 0;
1731 if (cda[CTA_MARK_MASK])
1732 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1734 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1735 newmark = (ct->mark & mask) ^ mark;
1736 if (newmark != ct->mark)
1741 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1742 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1743 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1744 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1747 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1748 const struct nlattr * const cda[])
1750 const struct nlattr *attr = cda[CTA_PROTOINFO];
1751 const struct nf_conntrack_l4proto *l4proto;
1752 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1755 err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_MAX, attr,
1756 protoinfo_policy, NULL);
1760 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
1761 if (l4proto->from_nlattr)
1762 err = l4proto->from_nlattr(tb, ct);
1767 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1768 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1769 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1770 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1773 static int change_seq_adj(struct nf_ct_seqadj *seq,
1774 const struct nlattr * const attr)
1777 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1779 err = nla_parse_nested_deprecated(cda, CTA_SEQADJ_MAX, attr,
1780 seqadj_policy, NULL);
1784 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1787 seq->correction_pos =
1788 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1790 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1793 seq->offset_before =
1794 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1796 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1800 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1806 ctnetlink_change_seq_adj(struct nf_conn *ct,
1807 const struct nlattr * const cda[])
1809 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1815 spin_lock_bh(&ct->lock);
1816 if (cda[CTA_SEQ_ADJ_ORIG]) {
1817 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1818 cda[CTA_SEQ_ADJ_ORIG]);
1822 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1825 if (cda[CTA_SEQ_ADJ_REPLY]) {
1826 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1827 cda[CTA_SEQ_ADJ_REPLY]);
1831 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1834 spin_unlock_bh(&ct->lock);
1837 spin_unlock_bh(&ct->lock);
1841 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
1842 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 },
1843 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 },
1844 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 },
1847 static int ctnetlink_change_synproxy(struct nf_conn *ct,
1848 const struct nlattr * const cda[])
1850 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
1851 struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
1857 err = nla_parse_nested_deprecated(tb, CTA_SYNPROXY_MAX,
1858 cda[CTA_SYNPROXY], synproxy_policy,
1863 if (!tb[CTA_SYNPROXY_ISN] ||
1864 !tb[CTA_SYNPROXY_ITS] ||
1865 !tb[CTA_SYNPROXY_TSOFF])
1868 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
1869 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
1870 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
1876 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1878 #ifdef CONFIG_NF_CONNTRACK_LABELS
1879 size_t len = nla_len(cda[CTA_LABELS]);
1880 const void *mask = cda[CTA_LABELS_MASK];
1882 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1886 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1887 nla_len(cda[CTA_LABELS_MASK]) != len)
1889 mask = nla_data(cda[CTA_LABELS_MASK]);
1894 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1901 ctnetlink_change_conntrack(struct nf_conn *ct,
1902 const struct nlattr * const cda[])
1906 /* only allow NAT changes and master assignation for new conntracks */
1907 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1910 if (cda[CTA_HELP]) {
1911 err = ctnetlink_change_helper(ct, cda);
1916 if (cda[CTA_TIMEOUT]) {
1917 err = ctnetlink_change_timeout(ct, cda);
1922 if (cda[CTA_STATUS]) {
1923 err = ctnetlink_change_status(ct, cda);
1928 if (cda[CTA_PROTOINFO]) {
1929 err = ctnetlink_change_protoinfo(ct, cda);
1934 #if defined(CONFIG_NF_CONNTRACK_MARK)
1936 ctnetlink_change_mark(ct, cda);
1939 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1940 err = ctnetlink_change_seq_adj(ct, cda);
1945 if (cda[CTA_SYNPROXY]) {
1946 err = ctnetlink_change_synproxy(ct, cda);
1951 if (cda[CTA_LABELS]) {
1952 err = ctnetlink_attach_labels(ct, cda);
1960 static struct nf_conn *
1961 ctnetlink_create_conntrack(struct net *net,
1962 const struct nf_conntrack_zone *zone,
1963 const struct nlattr * const cda[],
1964 struct nf_conntrack_tuple *otuple,
1965 struct nf_conntrack_tuple *rtuple,
1970 struct nf_conntrack_helper *helper;
1971 struct nf_conn_tstamp *tstamp;
1974 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1976 return ERR_PTR(-ENOMEM);
1978 if (!cda[CTA_TIMEOUT])
1981 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1982 if (timeout > INT_MAX)
1984 ct->timeout = (u32)timeout + nfct_time_stamp;
1987 if (cda[CTA_HELP]) {
1988 char *helpname = NULL;
1989 struct nlattr *helpinfo = NULL;
1991 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1995 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1996 nf_ct_protonum(ct));
1997 if (helper == NULL) {
1999 #ifdef CONFIG_MODULES
2000 if (request_module("nfct-helper-%s", helpname) < 0) {
2006 helper = __nf_conntrack_helper_find(helpname,
2008 nf_ct_protonum(ct));
2018 struct nf_conn_help *help;
2020 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
2025 /* set private helper data if allowed. */
2026 if (helper->from_nlattr)
2027 helper->from_nlattr(helpinfo, ct);
2029 /* not in hash table yet so not strictly necessary */
2030 RCU_INIT_POINTER(help->helper, helper);
2033 /* try an implicit helper assignation */
2034 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
2039 err = ctnetlink_setup_nat(ct, cda);
2043 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
2044 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
2045 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
2046 nf_ct_labels_ext_add(ct);
2047 nfct_seqadj_ext_add(ct);
2048 nfct_synproxy_ext_add(ct);
2050 /* we must add conntrack extensions before confirmation. */
2051 ct->status |= IPS_CONFIRMED;
2053 if (cda[CTA_STATUS]) {
2054 err = ctnetlink_change_status(ct, cda);
2059 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2060 err = ctnetlink_change_seq_adj(ct, cda);
2065 memset(&ct->proto, 0, sizeof(ct->proto));
2066 if (cda[CTA_PROTOINFO]) {
2067 err = ctnetlink_change_protoinfo(ct, cda);
2072 if (cda[CTA_SYNPROXY]) {
2073 err = ctnetlink_change_synproxy(ct, cda);
2078 #if defined(CONFIG_NF_CONNTRACK_MARK)
2080 ctnetlink_change_mark(ct, cda);
2083 /* setup master conntrack: this is a confirmed expectation */
2084 if (cda[CTA_TUPLE_MASTER]) {
2085 struct nf_conntrack_tuple master;
2086 struct nf_conntrack_tuple_hash *master_h;
2087 struct nf_conn *master_ct;
2089 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2094 master_h = nf_conntrack_find_get(net, zone, &master);
2095 if (master_h == NULL) {
2099 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2100 __set_bit(IPS_EXPECTED_BIT, &ct->status);
2101 ct->master = master_ct;
2103 tstamp = nf_conn_tstamp_find(ct);
2105 tstamp->start = ktime_get_real_ns();
2107 err = nf_conntrack_hash_check_insert(ct);
2118 nf_conntrack_free(ct);
2119 return ERR_PTR(err);
2122 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
2123 struct sk_buff *skb,
2124 const struct nlmsghdr *nlh,
2125 const struct nlattr * const cda[],
2126 struct netlink_ext_ack *extack)
2128 struct nf_conntrack_tuple otuple, rtuple;
2129 struct nf_conntrack_tuple_hash *h = NULL;
2130 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2132 u_int8_t u3 = nfmsg->nfgen_family;
2133 struct nf_conntrack_zone zone;
2136 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2140 if (cda[CTA_TUPLE_ORIG]) {
2141 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2147 if (cda[CTA_TUPLE_REPLY]) {
2148 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2154 if (cda[CTA_TUPLE_ORIG])
2155 h = nf_conntrack_find_get(net, &zone, &otuple);
2156 else if (cda[CTA_TUPLE_REPLY])
2157 h = nf_conntrack_find_get(net, &zone, &rtuple);
2161 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2162 enum ip_conntrack_events events;
2164 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2166 if (otuple.dst.protonum != rtuple.dst.protonum)
2169 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
2175 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2176 events = 1 << IPCT_RELATED;
2178 events = 1 << IPCT_NEW;
2180 if (cda[CTA_LABELS] &&
2181 ctnetlink_attach_labels(ct, cda) == 0)
2182 events |= (1 << IPCT_LABEL);
2184 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2185 (1 << IPCT_ASSURED) |
2186 (1 << IPCT_HELPER) |
2187 (1 << IPCT_PROTOINFO) |
2188 (1 << IPCT_SEQADJ) |
2190 (1 << IPCT_SYNPROXY) |
2192 ct, NETLINK_CB(skb).portid,
2199 /* implicit 'else' */
2202 ct = nf_ct_tuplehash_to_ctrack(h);
2203 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2204 err = ctnetlink_change_conntrack(ct, cda);
2206 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2207 (1 << IPCT_ASSURED) |
2208 (1 << IPCT_HELPER) |
2210 (1 << IPCT_PROTOINFO) |
2211 (1 << IPCT_SEQADJ) |
2213 (1 << IPCT_SYNPROXY),
2214 ct, NETLINK_CB(skb).portid,
2224 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2225 __u16 cpu, const struct ip_conntrack_stat *st)
2227 struct nlmsghdr *nlh;
2228 struct nfgenmsg *nfmsg;
2229 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2231 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2232 IPCTNL_MSG_CT_GET_STATS_CPU);
2233 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2237 nfmsg = nlmsg_data(nlh);
2238 nfmsg->nfgen_family = AF_UNSPEC;
2239 nfmsg->version = NFNETLINK_V0;
2240 nfmsg->res_id = htons(cpu);
2242 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2243 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2244 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2245 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2246 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2247 htonl(st->insert_failed)) ||
2248 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2249 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2250 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2251 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2252 htonl(st->search_restart)))
2253 goto nla_put_failure;
2255 nlmsg_end(skb, nlh);
2260 nlmsg_cancel(skb, nlh);
2265 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2268 struct net *net = sock_net(skb->sk);
2270 if (cb->args[0] == nr_cpu_ids)
2273 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2274 const struct ip_conntrack_stat *st;
2276 if (!cpu_possible(cpu))
2279 st = per_cpu_ptr(net->ct.stat, cpu);
2280 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2281 NETLINK_CB(cb->skb).portid,
2291 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2292 struct sk_buff *skb,
2293 const struct nlmsghdr *nlh,
2294 const struct nlattr * const cda[],
2295 struct netlink_ext_ack *extack)
2297 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2298 struct netlink_dump_control c = {
2299 .dump = ctnetlink_ct_stat_cpu_dump,
2301 return netlink_dump_start(ctnl, skb, nlh, &c);
2308 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2311 struct nlmsghdr *nlh;
2312 struct nfgenmsg *nfmsg;
2313 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2314 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2316 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2317 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2321 nfmsg = nlmsg_data(nlh);
2322 nfmsg->nfgen_family = AF_UNSPEC;
2323 nfmsg->version = NFNETLINK_V0;
2326 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2327 goto nla_put_failure;
2329 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2330 goto nla_put_failure;
2332 nlmsg_end(skb, nlh);
2337 nlmsg_cancel(skb, nlh);
2341 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2342 struct sk_buff *skb, const struct nlmsghdr *nlh,
2343 const struct nlattr * const cda[],
2344 struct netlink_ext_ack *extack)
2346 struct sk_buff *skb2;
2349 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2353 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2355 NFNL_MSG_TYPE(nlh->nlmsg_type),
2360 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2369 /* this avoids a loop in nfnetlink. */
2370 return err == -EAGAIN ? -ENOBUFS : err;
2373 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2374 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2375 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2376 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2377 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2378 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2379 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2380 .len = NF_CT_HELPER_NAME_LEN - 1 },
2381 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2382 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2383 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2384 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2385 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2388 static struct nf_conntrack_expect *
2389 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2390 struct nf_conntrack_helper *helper,
2391 struct nf_conntrack_tuple *tuple,
2392 struct nf_conntrack_tuple *mask);
2394 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2396 ctnetlink_glue_build_size(const struct nf_conn *ct)
2398 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2399 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2400 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2401 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2402 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2403 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2404 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2405 + nla_total_size(0) /* CTA_PROTOINFO */
2406 + nla_total_size(0) /* CTA_HELP */
2407 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2408 + ctnetlink_secctx_size(ct)
2409 #if IS_ENABLED(CONFIG_NF_NAT)
2410 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2411 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2413 #ifdef CONFIG_NF_CONNTRACK_MARK
2414 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2416 #ifdef CONFIG_NF_CONNTRACK_ZONES
2417 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2419 + ctnetlink_proto_size(ct)
2423 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2424 enum ip_conntrack_info *ctinfo)
2426 return nf_ct_get(skb, ctinfo);
2429 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2431 const struct nf_conntrack_zone *zone;
2432 struct nlattr *nest_parms;
2434 zone = nf_ct_zone(ct);
2436 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
2438 goto nla_put_failure;
2439 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2440 goto nla_put_failure;
2441 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2442 NF_CT_ZONE_DIR_ORIG) < 0)
2443 goto nla_put_failure;
2444 nla_nest_end(skb, nest_parms);
2446 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
2448 goto nla_put_failure;
2449 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2450 goto nla_put_failure;
2451 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2452 NF_CT_ZONE_DIR_REPL) < 0)
2453 goto nla_put_failure;
2454 nla_nest_end(skb, nest_parms);
2456 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2457 NF_CT_DEFAULT_ZONE_DIR) < 0)
2458 goto nla_put_failure;
2460 if (ctnetlink_dump_id(skb, ct) < 0)
2461 goto nla_put_failure;
2463 if (ctnetlink_dump_status(skb, ct) < 0)
2464 goto nla_put_failure;
2466 if (ctnetlink_dump_timeout(skb, ct) < 0)
2467 goto nla_put_failure;
2469 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2470 goto nla_put_failure;
2472 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2473 goto nla_put_failure;
2475 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2476 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2477 goto nla_put_failure;
2479 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2480 goto nla_put_failure;
2482 if ((ct->status & IPS_SEQ_ADJUST) &&
2483 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2484 goto nla_put_failure;
2486 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2487 goto nla_put_failure;
2489 #ifdef CONFIG_NF_CONNTRACK_MARK
2490 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2491 goto nla_put_failure;
2493 if (ctnetlink_dump_labels(skb, ct) < 0)
2494 goto nla_put_failure;
2502 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2503 enum ip_conntrack_info ctinfo,
2504 u_int16_t ct_attr, u_int16_t ct_info_attr)
2506 struct nlattr *nest_parms;
2508 nest_parms = nla_nest_start(skb, ct_attr);
2510 goto nla_put_failure;
2512 if (__ctnetlink_glue_build(skb, ct) < 0)
2513 goto nla_put_failure;
2515 nla_nest_end(skb, nest_parms);
2517 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2518 goto nla_put_failure;
2527 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2529 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2530 unsigned long d = ct->status ^ status;
2532 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2533 /* SEEN_REPLY bit can only be set */
2536 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2537 /* ASSURED bit can only be set */
2540 /* This check is less strict than ctnetlink_change_status()
2541 * because callers often flip IPS_EXPECTED bits when sending
2542 * an NFQA_CT attribute to the kernel. So ignore the
2543 * unchangeable bits but do not error out. Also user programs
2544 * are allowed to clear the bits that they are allowed to change.
2546 __ctnetlink_change_status(ct, status, ~status);
2551 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2555 if (cda[CTA_TIMEOUT]) {
2556 err = ctnetlink_change_timeout(ct, cda);
2560 if (cda[CTA_STATUS]) {
2561 err = ctnetlink_update_status(ct, cda);
2565 if (cda[CTA_HELP]) {
2566 err = ctnetlink_change_helper(ct, cda);
2570 if (cda[CTA_LABELS]) {
2571 err = ctnetlink_attach_labels(ct, cda);
2575 #if defined(CONFIG_NF_CONNTRACK_MARK)
2576 if (cda[CTA_MARK]) {
2577 ctnetlink_change_mark(ct, cda);
2584 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2586 struct nlattr *cda[CTA_MAX+1];
2589 ret = nla_parse_nested_deprecated(cda, CTA_MAX, attr, ct_nla_policy,
2594 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2597 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2598 const struct nf_conn *ct,
2599 struct nf_conntrack_tuple *tuple,
2600 struct nf_conntrack_tuple *mask)
2604 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2605 nf_ct_l3num(ct), NULL);
2609 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2610 nf_ct_l3num(ct), NULL);
2614 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2615 u32 portid, u32 report)
2617 struct nlattr *cda[CTA_EXPECT_MAX+1];
2618 struct nf_conntrack_tuple tuple, mask;
2619 struct nf_conntrack_helper *helper = NULL;
2620 struct nf_conntrack_expect *exp;
2623 err = nla_parse_nested_deprecated(cda, CTA_EXPECT_MAX, attr,
2624 exp_nla_policy, NULL);
2628 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2633 if (cda[CTA_EXPECT_HELP_NAME]) {
2634 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2636 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2637 nf_ct_protonum(ct));
2642 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2643 helper, &tuple, &mask);
2645 return PTR_ERR(exp);
2647 err = nf_ct_expect_related_report(exp, portid, report, 0);
2648 nf_ct_expect_put(exp);
2652 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2653 enum ip_conntrack_info ctinfo, int diff)
2655 if (!(ct->status & IPS_NAT_MASK))
2658 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2661 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2662 .get_ct = ctnetlink_glue_get_ct,
2663 .build_size = ctnetlink_glue_build_size,
2664 .build = ctnetlink_glue_build,
2665 .parse = ctnetlink_glue_parse,
2666 .attach_expect = ctnetlink_glue_attach_expect,
2667 .seq_adjust = ctnetlink_glue_seqadj,
2669 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2671 /***********************************************************************
2673 ***********************************************************************/
2675 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2676 const struct nf_conntrack_tuple *tuple,
2679 struct nlattr *nest_parms;
2681 nest_parms = nla_nest_start(skb, type);
2683 goto nla_put_failure;
2684 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2685 goto nla_put_failure;
2686 nla_nest_end(skb, nest_parms);
2694 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2695 const struct nf_conntrack_tuple *tuple,
2696 const struct nf_conntrack_tuple_mask *mask)
2698 const struct nf_conntrack_l4proto *l4proto;
2699 struct nf_conntrack_tuple m;
2700 struct nlattr *nest_parms;
2703 memset(&m, 0xFF, sizeof(m));
2704 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2705 m.src.u.all = mask->src.u.all;
2706 m.dst.protonum = tuple->dst.protonum;
2708 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK);
2710 goto nla_put_failure;
2713 ret = ctnetlink_dump_tuples_ip(skb, &m);
2715 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
2716 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2720 if (unlikely(ret < 0))
2721 goto nla_put_failure;
2723 nla_nest_end(skb, nest_parms);
2731 static const union nf_inet_addr any_addr;
2733 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2735 static __read_mostly siphash_key_t exp_id_seed;
2736 unsigned long a, b, c, d;
2738 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2740 a = (unsigned long)exp;
2741 b = (unsigned long)exp->helper;
2742 c = (unsigned long)exp->master;
2743 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2746 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2748 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2753 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2754 const struct nf_conntrack_expect *exp)
2756 struct nf_conn *master = exp->master;
2757 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2758 struct nf_conn_help *help;
2759 #if IS_ENABLED(CONFIG_NF_NAT)
2760 struct nlattr *nest_parms;
2761 struct nf_conntrack_tuple nat_tuple = {};
2763 struct nf_ct_helper_expectfn *expfn;
2768 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2769 goto nla_put_failure;
2770 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2771 goto nla_put_failure;
2772 if (ctnetlink_exp_dump_tuple(skb,
2773 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2774 CTA_EXPECT_MASTER) < 0)
2775 goto nla_put_failure;
2777 #if IS_ENABLED(CONFIG_NF_NAT)
2778 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2779 exp->saved_proto.all) {
2780 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT);
2782 goto nla_put_failure;
2784 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2785 goto nla_put_failure;
2787 nat_tuple.src.l3num = nf_ct_l3num(master);
2788 nat_tuple.src.u3 = exp->saved_addr;
2789 nat_tuple.dst.protonum = nf_ct_protonum(master);
2790 nat_tuple.src.u = exp->saved_proto;
2792 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2793 CTA_EXPECT_NAT_TUPLE) < 0)
2794 goto nla_put_failure;
2795 nla_nest_end(skb, nest_parms);
2798 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2799 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
2800 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2801 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2802 goto nla_put_failure;
2803 help = nfct_help(master);
2805 struct nf_conntrack_helper *helper;
2807 helper = rcu_dereference(help->helper);
2809 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2810 goto nla_put_failure;
2812 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2813 if (expfn != NULL &&
2814 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2815 goto nla_put_failure;
2824 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2825 int event, const struct nf_conntrack_expect *exp)
2827 struct nlmsghdr *nlh;
2828 struct nfgenmsg *nfmsg;
2829 unsigned int flags = portid ? NLM_F_MULTI : 0;
2831 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2832 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2836 nfmsg = nlmsg_data(nlh);
2837 nfmsg->nfgen_family = exp->tuple.src.l3num;
2838 nfmsg->version = NFNETLINK_V0;
2841 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2842 goto nla_put_failure;
2844 nlmsg_end(skb, nlh);
2849 nlmsg_cancel(skb, nlh);
2853 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2855 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2857 struct nf_conntrack_expect *exp = item->exp;
2858 struct net *net = nf_ct_exp_net(exp);
2859 struct nlmsghdr *nlh;
2860 struct nfgenmsg *nfmsg;
2861 struct sk_buff *skb;
2862 unsigned int type, group;
2865 if (events & (1 << IPEXP_DESTROY)) {
2866 type = IPCTNL_MSG_EXP_DELETE;
2867 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2868 } else if (events & (1 << IPEXP_NEW)) {
2869 type = IPCTNL_MSG_EXP_NEW;
2870 flags = NLM_F_CREATE|NLM_F_EXCL;
2871 group = NFNLGRP_CONNTRACK_EXP_NEW;
2875 if (!item->report && !nfnetlink_has_listeners(net, group))
2878 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2882 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2883 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2887 nfmsg = nlmsg_data(nlh);
2888 nfmsg->nfgen_family = exp->tuple.src.l3num;
2889 nfmsg->version = NFNETLINK_V0;
2892 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2893 goto nla_put_failure;
2895 nlmsg_end(skb, nlh);
2896 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2900 nlmsg_cancel(skb, nlh);
2904 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2908 static int ctnetlink_exp_done(struct netlink_callback *cb)
2911 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2916 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2918 struct net *net = sock_net(skb->sk);
2919 struct nf_conntrack_expect *exp, *last;
2920 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2921 u_int8_t l3proto = nfmsg->nfgen_family;
2924 last = (struct nf_conntrack_expect *)cb->args[1];
2925 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2927 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2929 if (l3proto && exp->tuple.src.l3num != l3proto)
2932 if (!net_eq(nf_ct_net(exp->master), net))
2940 if (ctnetlink_exp_fill_info(skb,
2941 NETLINK_CB(cb->skb).portid,
2945 if (!refcount_inc_not_zero(&exp->use))
2947 cb->args[1] = (unsigned long)exp;
2959 nf_ct_expect_put(last);
2965 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2967 struct nf_conntrack_expect *exp, *last;
2968 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2969 struct nf_conn *ct = cb->data;
2970 struct nf_conn_help *help = nfct_help(ct);
2971 u_int8_t l3proto = nfmsg->nfgen_family;
2977 last = (struct nf_conntrack_expect *)cb->args[1];
2979 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2980 if (l3proto && exp->tuple.src.l3num != l3proto)
2987 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2991 if (!refcount_inc_not_zero(&exp->use))
2993 cb->args[1] = (unsigned long)exp;
3005 nf_ct_expect_put(last);
3010 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
3011 struct sk_buff *skb,
3012 const struct nlmsghdr *nlh,
3013 const struct nlattr * const cda[],
3014 struct netlink_ext_ack *extack)
3017 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3018 u_int8_t u3 = nfmsg->nfgen_family;
3019 struct nf_conntrack_tuple tuple;
3020 struct nf_conntrack_tuple_hash *h;
3022 struct nf_conntrack_zone zone;
3023 struct netlink_dump_control c = {
3024 .dump = ctnetlink_exp_ct_dump_table,
3025 .done = ctnetlink_exp_done,
3028 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3033 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3037 h = nf_conntrack_find_get(net, &zone, &tuple);
3041 ct = nf_ct_tuplehash_to_ctrack(h);
3042 /* No expectation linked to this connection tracking. */
3043 if (!nfct_help(ct)) {
3050 err = netlink_dump_start(ctnl, skb, nlh, &c);
3056 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
3057 struct sk_buff *skb, const struct nlmsghdr *nlh,
3058 const struct nlattr * const cda[],
3059 struct netlink_ext_ack *extack)
3061 struct nf_conntrack_tuple tuple;
3062 struct nf_conntrack_expect *exp;
3063 struct sk_buff *skb2;
3064 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3065 u_int8_t u3 = nfmsg->nfgen_family;
3066 struct nf_conntrack_zone zone;
3069 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3070 if (cda[CTA_EXPECT_MASTER])
3071 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
3074 struct netlink_dump_control c = {
3075 .dump = ctnetlink_exp_dump_table,
3076 .done = ctnetlink_exp_done,
3078 return netlink_dump_start(ctnl, skb, nlh, &c);
3082 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3086 if (cda[CTA_EXPECT_TUPLE])
3087 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3089 else if (cda[CTA_EXPECT_MASTER])
3090 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3098 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3102 if (cda[CTA_EXPECT_ID]) {
3103 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3105 if (id != nf_expect_get_id(exp)) {
3106 nf_ct_expect_put(exp);
3112 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3114 nf_ct_expect_put(exp);
3119 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3120 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
3122 nf_ct_expect_put(exp);
3126 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
3135 /* this avoids a loop in nfnetlink. */
3136 return err == -EAGAIN ? -ENOBUFS : err;
3139 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3141 const struct nf_conn_help *m_help;
3142 const char *name = data;
3144 m_help = nfct_help(exp->master);
3146 return strcmp(m_help->helper->name, name) == 0;
3149 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3154 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
3155 struct sk_buff *skb, const struct nlmsghdr *nlh,
3156 const struct nlattr * const cda[],
3157 struct netlink_ext_ack *extack)
3159 struct nf_conntrack_expect *exp;
3160 struct nf_conntrack_tuple tuple;
3161 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3162 u_int8_t u3 = nfmsg->nfgen_family;
3163 struct nf_conntrack_zone zone;
3166 if (cda[CTA_EXPECT_TUPLE]) {
3167 /* delete a single expect by tuple */
3168 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3172 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3177 /* bump usage count to 2 */
3178 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3182 if (cda[CTA_EXPECT_ID]) {
3183 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3184 if (ntohl(id) != (u32)(unsigned long)exp) {
3185 nf_ct_expect_put(exp);
3190 /* after list removal, usage count == 1 */
3191 spin_lock_bh(&nf_conntrack_expect_lock);
3192 if (del_timer(&exp->timeout)) {
3193 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3195 nf_ct_expect_put(exp);
3197 spin_unlock_bh(&nf_conntrack_expect_lock);
3198 /* have to put what we 'get' above.
3199 * after this line usage count == 0 */
3200 nf_ct_expect_put(exp);
3201 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3202 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3204 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3205 NETLINK_CB(skb).portid,
3208 /* This basically means we have to flush everything*/
3209 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3210 NETLINK_CB(skb).portid,
3217 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3218 const struct nlattr * const cda[])
3220 if (cda[CTA_EXPECT_TIMEOUT]) {
3221 if (!del_timer(&x->timeout))
3224 x->timeout.expires = jiffies +
3225 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3226 add_timer(&x->timeout);
3231 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3232 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3233 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3237 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3238 struct nf_conntrack_expect *exp,
3241 #if IS_ENABLED(CONFIG_NF_NAT)
3242 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3243 struct nf_conntrack_tuple nat_tuple = {};
3246 err = nla_parse_nested_deprecated(tb, CTA_EXPECT_NAT_MAX, attr,
3247 exp_nat_nla_policy, NULL);
3251 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3254 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3255 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3260 exp->saved_addr = nat_tuple.src.u3;
3261 exp->saved_proto = nat_tuple.src.u;
3262 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3270 static struct nf_conntrack_expect *
3271 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3272 struct nf_conntrack_helper *helper,
3273 struct nf_conntrack_tuple *tuple,
3274 struct nf_conntrack_tuple *mask)
3276 u_int32_t class = 0;
3277 struct nf_conntrack_expect *exp;
3278 struct nf_conn_help *help;
3281 help = nfct_help(ct);
3283 return ERR_PTR(-EOPNOTSUPP);
3285 if (cda[CTA_EXPECT_CLASS] && helper) {
3286 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3287 if (class > helper->expect_class_max)
3288 return ERR_PTR(-EINVAL);
3290 exp = nf_ct_expect_alloc(ct);
3292 return ERR_PTR(-ENOMEM);
3294 if (cda[CTA_EXPECT_FLAGS]) {
3295 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3296 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3300 if (cda[CTA_EXPECT_FN]) {
3301 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3302 struct nf_ct_helper_expectfn *expfn;
3304 expfn = nf_ct_helper_expectfn_find_by_name(name);
3305 if (expfn == NULL) {
3309 exp->expectfn = expfn->expectfn;
3311 exp->expectfn = NULL;
3315 exp->helper = helper;
3316 exp->tuple = *tuple;
3317 exp->mask.src.u3 = mask->src.u3;
3318 exp->mask.src.u.all = mask->src.u.all;
3320 if (cda[CTA_EXPECT_NAT]) {
3321 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3322 exp, nf_ct_l3num(ct));
3328 nf_ct_expect_put(exp);
3329 return ERR_PTR(err);
3333 ctnetlink_create_expect(struct net *net,
3334 const struct nf_conntrack_zone *zone,
3335 const struct nlattr * const cda[],
3336 u_int8_t u3, u32 portid, int report)
3338 struct nf_conntrack_tuple tuple, mask, master_tuple;
3339 struct nf_conntrack_tuple_hash *h = NULL;
3340 struct nf_conntrack_helper *helper = NULL;
3341 struct nf_conntrack_expect *exp;
3345 /* caller guarantees that those three CTA_EXPECT_* exist */
3346 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3350 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3354 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3359 /* Look for master conntrack of this expectation */
3360 h = nf_conntrack_find_get(net, zone, &master_tuple);
3363 ct = nf_ct_tuplehash_to_ctrack(h);
3366 if (cda[CTA_EXPECT_HELP_NAME]) {
3367 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3369 helper = __nf_conntrack_helper_find(helpname, u3,
3370 nf_ct_protonum(ct));
3371 if (helper == NULL) {
3373 #ifdef CONFIG_MODULES
3374 if (request_module("nfct-helper-%s", helpname) < 0) {
3379 helper = __nf_conntrack_helper_find(helpname, u3,
3380 nf_ct_protonum(ct));
3392 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3398 err = nf_ct_expect_related_report(exp, portid, report, 0);
3399 nf_ct_expect_put(exp);
3407 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3408 struct sk_buff *skb, const struct nlmsghdr *nlh,
3409 const struct nlattr * const cda[],
3410 struct netlink_ext_ack *extack)
3412 struct nf_conntrack_tuple tuple;
3413 struct nf_conntrack_expect *exp;
3414 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3415 u_int8_t u3 = nfmsg->nfgen_family;
3416 struct nf_conntrack_zone zone;
3419 if (!cda[CTA_EXPECT_TUPLE]
3420 || !cda[CTA_EXPECT_MASK]
3421 || !cda[CTA_EXPECT_MASTER])
3424 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3428 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3433 spin_lock_bh(&nf_conntrack_expect_lock);
3434 exp = __nf_ct_expect_find(net, &zone, &tuple);
3436 spin_unlock_bh(&nf_conntrack_expect_lock);
3438 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3439 err = ctnetlink_create_expect(net, &zone, cda, u3,
3440 NETLINK_CB(skb).portid,
3447 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3448 err = ctnetlink_change_expect(exp, cda);
3449 spin_unlock_bh(&nf_conntrack_expect_lock);
3455 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3456 const struct ip_conntrack_stat *st)
3458 struct nlmsghdr *nlh;
3459 struct nfgenmsg *nfmsg;
3460 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3462 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3463 IPCTNL_MSG_EXP_GET_STATS_CPU);
3464 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3468 nfmsg = nlmsg_data(nlh);
3469 nfmsg->nfgen_family = AF_UNSPEC;
3470 nfmsg->version = NFNETLINK_V0;
3471 nfmsg->res_id = htons(cpu);
3473 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3474 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3475 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3476 goto nla_put_failure;
3478 nlmsg_end(skb, nlh);
3483 nlmsg_cancel(skb, nlh);
3488 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3491 struct net *net = sock_net(skb->sk);
3493 if (cb->args[0] == nr_cpu_ids)
3496 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3497 const struct ip_conntrack_stat *st;
3499 if (!cpu_possible(cpu))
3502 st = per_cpu_ptr(net->ct.stat, cpu);
3503 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3513 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3514 struct sk_buff *skb,
3515 const struct nlmsghdr *nlh,
3516 const struct nlattr * const cda[],
3517 struct netlink_ext_ack *extack)
3519 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3520 struct netlink_dump_control c = {
3521 .dump = ctnetlink_exp_stat_cpu_dump,
3523 return netlink_dump_start(ctnl, skb, nlh, &c);
3529 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3530 static struct nf_ct_event_notifier ctnl_notifier = {
3531 .fcn = ctnetlink_conntrack_event,
3534 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3535 .fcn = ctnetlink_expect_event,
3539 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3540 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3541 .attr_count = CTA_MAX,
3542 .policy = ct_nla_policy },
3543 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3544 .attr_count = CTA_MAX,
3545 .policy = ct_nla_policy },
3546 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3547 .attr_count = CTA_MAX,
3548 .policy = ct_nla_policy },
3549 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3550 .attr_count = CTA_MAX,
3551 .policy = ct_nla_policy },
3552 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3553 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3554 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3555 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3558 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3559 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3560 .attr_count = CTA_EXPECT_MAX,
3561 .policy = exp_nla_policy },
3562 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3563 .attr_count = CTA_EXPECT_MAX,
3564 .policy = exp_nla_policy },
3565 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3566 .attr_count = CTA_EXPECT_MAX,
3567 .policy = exp_nla_policy },
3568 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3571 static const struct nfnetlink_subsystem ctnl_subsys = {
3572 .name = "conntrack",
3573 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3574 .cb_count = IPCTNL_MSG_MAX,
3578 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3579 .name = "conntrack_expect",
3580 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3581 .cb_count = IPCTNL_MSG_EXP_MAX,
3585 MODULE_ALIAS("ip_conntrack_netlink");
3586 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3587 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3589 static int __net_init ctnetlink_net_init(struct net *net)
3591 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3594 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3596 pr_err("ctnetlink_init: cannot register notifier.\n");
3600 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3602 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3603 goto err_unreg_notifier;
3608 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3610 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3616 static void ctnetlink_net_exit(struct net *net)
3618 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3619 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3620 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3624 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3628 list_for_each_entry(net, net_exit_list, exit_list)
3629 ctnetlink_net_exit(net);
3631 /* wait for other cpus until they are done with ctnl_notifiers */
3635 static struct pernet_operations ctnetlink_net_ops = {
3636 .init = ctnetlink_net_init,
3637 .exit_batch = ctnetlink_net_exit_batch,
3640 static int __init ctnetlink_init(void)
3644 ret = nfnetlink_subsys_register(&ctnl_subsys);
3646 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3650 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3652 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3653 goto err_unreg_subsys;
3656 ret = register_pernet_subsys(&ctnetlink_net_ops);
3658 pr_err("ctnetlink_init: cannot register pernet operations\n");
3659 goto err_unreg_exp_subsys;
3661 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3662 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3663 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3667 err_unreg_exp_subsys:
3668 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3670 nfnetlink_subsys_unregister(&ctnl_subsys);
3675 static void __exit ctnetlink_exit(void)
3677 unregister_pernet_subsys(&ctnetlink_net_ops);
3678 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3679 nfnetlink_subsys_unregister(&ctnl_subsys);
3680 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3681 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3686 module_init(ctnetlink_init);
3687 module_exit(ctnetlink_exit);