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 #include "nf_internals.h"
59 MODULE_LICENSE("GPL");
61 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
62 const struct nf_conntrack_tuple *tuple,
63 const struct nf_conntrack_l4proto *l4proto)
66 struct nlattr *nest_parms;
68 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO);
71 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
74 if (likely(l4proto->tuple_to_nlattr))
75 ret = l4proto->tuple_to_nlattr(skb, tuple);
77 nla_nest_end(skb, nest_parms);
85 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
86 const struct nf_conntrack_tuple *tuple)
88 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
89 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
94 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
95 const struct nf_conntrack_tuple *tuple)
97 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
98 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
103 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
104 const struct nf_conntrack_tuple *tuple)
107 struct nlattr *nest_parms;
109 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP);
111 goto nla_put_failure;
113 switch (tuple->src.l3num) {
115 ret = ipv4_tuple_to_nlattr(skb, tuple);
118 ret = ipv6_tuple_to_nlattr(skb, tuple);
122 nla_nest_end(skb, nest_parms);
130 static int ctnetlink_dump_tuples(struct sk_buff *skb,
131 const struct nf_conntrack_tuple *tuple)
133 const struct nf_conntrack_l4proto *l4proto;
137 ret = ctnetlink_dump_tuples_ip(skb, tuple);
140 l4proto = nf_ct_l4proto_find(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_protonum(ct));
189 if (!l4proto->to_nlattr)
192 nest_proto = nla_nest_start(skb, CTA_PROTOINFO);
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;
216 helper = rcu_dereference(help->helper);
220 nest_helper = nla_nest_start(skb, CTA_HELP);
222 goto nla_put_failure;
223 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
224 goto nla_put_failure;
226 if (helper->to_nlattr)
227 helper->to_nlattr(skb, ct);
229 nla_nest_end(skb, nest_helper);
238 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
239 enum ip_conntrack_dir dir, int type)
241 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
242 struct nf_conn_counter *counter = acct->counter;
243 struct nlattr *nest_count;
246 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
247 pkts = atomic64_xchg(&counter[dir].packets, 0);
248 bytes = atomic64_xchg(&counter[dir].bytes, 0);
250 pkts = atomic64_read(&counter[dir].packets);
251 bytes = atomic64_read(&counter[dir].bytes);
254 nest_count = nla_nest_start(skb, attr);
256 goto nla_put_failure;
258 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
260 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
262 goto nla_put_failure;
264 nla_nest_end(skb, nest_count);
273 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
275 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
280 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
282 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
289 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
291 struct nlattr *nest_count;
292 const struct nf_conn_tstamp *tstamp;
294 tstamp = nf_conn_tstamp_find(ct);
298 nest_count = nla_nest_start(skb, CTA_TIMESTAMP);
300 goto nla_put_failure;
302 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
303 CTA_TIMESTAMP_PAD) ||
304 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
305 cpu_to_be64(tstamp->stop),
307 goto nla_put_failure;
308 nla_nest_end(skb, nest_count);
316 #ifdef CONFIG_NF_CONNTRACK_MARK
317 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
319 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
320 goto nla_put_failure;
327 #define ctnetlink_dump_mark(a, b) (0)
330 #ifdef CONFIG_NF_CONNTRACK_SECMARK
331 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
333 struct nlattr *nest_secctx;
337 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
342 nest_secctx = nla_nest_start(skb, CTA_SECCTX);
344 goto nla_put_failure;
346 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
347 goto nla_put_failure;
348 nla_nest_end(skb, nest_secctx);
352 security_release_secctx(secctx, len);
356 #define ctnetlink_dump_secctx(a, b) (0)
359 #ifdef CONFIG_NF_CONNTRACK_LABELS
360 static inline int ctnetlink_label_size(const struct nf_conn *ct)
362 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
366 return nla_total_size(sizeof(labels->bits));
370 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
372 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
380 if (labels->bits[i] != 0)
381 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
384 } while (i < ARRAY_SIZE(labels->bits));
389 #define ctnetlink_dump_labels(a, b) (0)
390 #define ctnetlink_label_size(a) (0)
393 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
395 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
397 struct nlattr *nest_parms;
399 if (!(ct->status & IPS_EXPECTED))
402 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER);
404 goto nla_put_failure;
405 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
406 goto nla_put_failure;
407 nla_nest_end(skb, nest_parms);
416 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
418 struct nlattr *nest_parms;
420 nest_parms = nla_nest_start(skb, type);
422 goto nla_put_failure;
424 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
425 htonl(seq->correction_pos)) ||
426 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
427 htonl(seq->offset_before)) ||
428 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
429 htonl(seq->offset_after)))
430 goto nla_put_failure;
432 nla_nest_end(skb, nest_parms);
440 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
442 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
443 struct nf_ct_seqadj *seq;
445 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
448 spin_lock_bh(&ct->lock);
449 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
450 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
453 seq = &seqadj->seq[IP_CT_DIR_REPLY];
454 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
457 spin_unlock_bh(&ct->lock);
460 spin_unlock_bh(&ct->lock);
464 static int ctnetlink_dump_ct_synproxy(struct sk_buff *skb, struct nf_conn *ct)
466 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
467 struct nlattr *nest_parms;
472 nest_parms = nla_nest_start(skb, CTA_SYNPROXY);
474 goto nla_put_failure;
476 if (nla_put_be32(skb, CTA_SYNPROXY_ISN, htonl(synproxy->isn)) ||
477 nla_put_be32(skb, CTA_SYNPROXY_ITS, htonl(synproxy->its)) ||
478 nla_put_be32(skb, CTA_SYNPROXY_TSOFF, htonl(synproxy->tsoff)))
479 goto nla_put_failure;
481 nla_nest_end(skb, nest_parms);
489 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
491 __be32 id = (__force __be32)nf_ct_get_id(ct);
493 if (nla_put_be32(skb, CTA_ID, id))
494 goto nla_put_failure;
501 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
503 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
504 goto nla_put_failure;
511 /* all these functions access ct->ext. Caller must either hold a reference
512 * on ct or prevent its deletion by holding either the bucket spinlock or
513 * pcpu dying list lock.
515 static int ctnetlink_dump_extinfo(struct sk_buff *skb,
516 struct nf_conn *ct, u32 type)
518 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
519 ctnetlink_dump_timestamp(skb, ct) < 0 ||
520 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
521 ctnetlink_dump_labels(skb, ct) < 0 ||
522 ctnetlink_dump_ct_seq_adj(skb, ct) < 0 ||
523 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
529 static int ctnetlink_dump_info(struct sk_buff *skb, struct nf_conn *ct)
531 if (ctnetlink_dump_status(skb, ct) < 0 ||
532 ctnetlink_dump_mark(skb, ct) < 0 ||
533 ctnetlink_dump_secctx(skb, ct) < 0 ||
534 ctnetlink_dump_id(skb, ct) < 0 ||
535 ctnetlink_dump_use(skb, ct) < 0 ||
536 ctnetlink_dump_master(skb, ct) < 0)
539 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status) &&
540 (ctnetlink_dump_timeout(skb, ct) < 0 ||
541 ctnetlink_dump_protoinfo(skb, ct) < 0))
548 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
549 struct nf_conn *ct, bool extinfo, unsigned int flags)
551 const struct nf_conntrack_zone *zone;
552 struct nlmsghdr *nlh;
553 struct nfgenmsg *nfmsg;
554 struct nlattr *nest_parms;
558 flags |= NLM_F_MULTI;
559 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
560 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
564 nfmsg = nlmsg_data(nlh);
565 nfmsg->nfgen_family = nf_ct_l3num(ct);
566 nfmsg->version = NFNETLINK_V0;
569 zone = nf_ct_zone(ct);
571 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
573 goto nla_put_failure;
574 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
575 goto nla_put_failure;
576 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
577 NF_CT_ZONE_DIR_ORIG) < 0)
578 goto nla_put_failure;
579 nla_nest_end(skb, nest_parms);
581 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
583 goto nla_put_failure;
584 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
585 goto nla_put_failure;
586 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
587 NF_CT_ZONE_DIR_REPL) < 0)
588 goto nla_put_failure;
589 nla_nest_end(skb, nest_parms);
591 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
592 NF_CT_DEFAULT_ZONE_DIR) < 0)
593 goto nla_put_failure;
595 if (ctnetlink_dump_info(skb, ct) < 0)
596 goto nla_put_failure;
597 if (extinfo && ctnetlink_dump_extinfo(skb, ct, type) < 0)
598 goto nla_put_failure;
605 nlmsg_cancel(skb, nlh);
609 static const struct nla_policy cta_ip_nla_policy[CTA_IP_MAX + 1] = {
610 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
611 [CTA_IP_V4_DST] = { .type = NLA_U32 },
612 [CTA_IP_V6_SRC] = { .len = sizeof(__be32) * 4 },
613 [CTA_IP_V6_DST] = { .len = sizeof(__be32) * 4 },
616 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
617 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
619 const struct nf_conntrack_l4proto *l4proto;
620 size_t len, len4 = 0;
622 len = nla_policy_len(cta_ip_nla_policy, CTA_IP_MAX + 1);
623 len *= 3u; /* ORIG, REPLY, MASTER */
625 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
626 len += l4proto->nlattr_size;
627 if (l4proto->nlattr_tuple_size) {
628 len4 = l4proto->nlattr_tuple_size();
629 len4 *= 3u; /* ORIG, REPLY, MASTER */
636 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
638 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
640 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
641 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
642 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
646 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
648 #ifdef CONFIG_NF_CONNTRACK_SECMARK
651 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
655 return nla_total_size(0) /* CTA_SECCTX */
656 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
662 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
664 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
665 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
667 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
673 #ifdef CONFIG_NF_CONNTRACK_EVENTS
674 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
676 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
677 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
678 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
679 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
680 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
681 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
682 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
683 + ctnetlink_acct_size(ct)
684 + ctnetlink_timestamp_size(ct)
685 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
686 + nla_total_size(0) /* CTA_PROTOINFO */
687 + nla_total_size(0) /* CTA_HELP */
688 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
689 + ctnetlink_secctx_size(ct)
690 #if IS_ENABLED(CONFIG_NF_NAT)
691 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
692 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
694 #ifdef CONFIG_NF_CONNTRACK_MARK
695 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
697 #ifdef CONFIG_NF_CONNTRACK_ZONES
698 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
700 + ctnetlink_proto_size(ct)
701 + ctnetlink_label_size(ct)
706 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
708 const struct nf_conntrack_zone *zone;
710 struct nlmsghdr *nlh;
711 struct nfgenmsg *nfmsg;
712 struct nlattr *nest_parms;
713 struct nf_conn *ct = item->ct;
716 unsigned int flags = 0, group;
719 if (events & (1 << IPCT_DESTROY)) {
720 type = IPCTNL_MSG_CT_DELETE;
721 group = NFNLGRP_CONNTRACK_DESTROY;
722 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
723 type = IPCTNL_MSG_CT_NEW;
724 flags = NLM_F_CREATE|NLM_F_EXCL;
725 group = NFNLGRP_CONNTRACK_NEW;
727 type = IPCTNL_MSG_CT_NEW;
728 group = NFNLGRP_CONNTRACK_UPDATE;
733 if (!item->report && !nfnetlink_has_listeners(net, group))
736 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
740 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
741 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
745 nfmsg = nlmsg_data(nlh);
746 nfmsg->nfgen_family = nf_ct_l3num(ct);
747 nfmsg->version = NFNETLINK_V0;
750 zone = nf_ct_zone(ct);
752 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
754 goto nla_put_failure;
755 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
756 goto nla_put_failure;
757 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
758 NF_CT_ZONE_DIR_ORIG) < 0)
759 goto nla_put_failure;
760 nla_nest_end(skb, nest_parms);
762 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
764 goto nla_put_failure;
765 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
766 goto nla_put_failure;
767 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
768 NF_CT_ZONE_DIR_REPL) < 0)
769 goto nla_put_failure;
770 nla_nest_end(skb, nest_parms);
772 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
773 NF_CT_DEFAULT_ZONE_DIR) < 0)
774 goto nla_put_failure;
776 if (ctnetlink_dump_id(skb, ct) < 0)
777 goto nla_put_failure;
779 if (ctnetlink_dump_status(skb, ct) < 0)
780 goto nla_put_failure;
782 if (events & (1 << IPCT_DESTROY)) {
783 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
784 ctnetlink_dump_timestamp(skb, ct) < 0)
785 goto nla_put_failure;
787 if (ctnetlink_dump_timeout(skb, ct) < 0)
788 goto nla_put_failure;
790 if (events & (1 << IPCT_PROTOINFO)
791 && ctnetlink_dump_protoinfo(skb, ct) < 0)
792 goto nla_put_failure;
794 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
795 && ctnetlink_dump_helpinfo(skb, ct) < 0)
796 goto nla_put_failure;
798 #ifdef CONFIG_NF_CONNTRACK_SECMARK
799 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
800 && ctnetlink_dump_secctx(skb, ct) < 0)
801 goto nla_put_failure;
803 if (events & (1 << IPCT_LABEL) &&
804 ctnetlink_dump_labels(skb, ct) < 0)
805 goto nla_put_failure;
807 if (events & (1 << IPCT_RELATED) &&
808 ctnetlink_dump_master(skb, ct) < 0)
809 goto nla_put_failure;
811 if (events & (1 << IPCT_SEQADJ) &&
812 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
813 goto nla_put_failure;
815 if (events & (1 << IPCT_SYNPROXY) &&
816 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
817 goto nla_put_failure;
820 #ifdef CONFIG_NF_CONNTRACK_MARK
821 if ((events & (1 << IPCT_MARK) || ct->mark)
822 && ctnetlink_dump_mark(skb, ct) < 0)
823 goto nla_put_failure;
826 err = nfnetlink_send(skb, net, item->portid, group, item->report,
828 if (err == -ENOBUFS || err == -EAGAIN)
834 nlmsg_cancel(skb, nlh);
838 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
843 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
845 static int ctnetlink_done(struct netlink_callback *cb)
848 nf_ct_put((struct nf_conn *)cb->args[1]);
853 struct ctnetlink_filter {
856 u_int32_t orig_flags;
857 u_int32_t reply_flags;
859 struct nf_conntrack_tuple orig;
860 struct nf_conntrack_tuple reply;
861 struct nf_conntrack_zone zone;
869 static const struct nla_policy cta_filter_nla_policy[CTA_FILTER_MAX + 1] = {
870 [CTA_FILTER_ORIG_FLAGS] = { .type = NLA_U32 },
871 [CTA_FILTER_REPLY_FLAGS] = { .type = NLA_U32 },
874 static int ctnetlink_parse_filter(const struct nlattr *attr,
875 struct ctnetlink_filter *filter)
877 struct nlattr *tb[CTA_FILTER_MAX + 1];
880 ret = nla_parse_nested(tb, CTA_FILTER_MAX, attr, cta_filter_nla_policy,
885 if (tb[CTA_FILTER_ORIG_FLAGS]) {
886 filter->orig_flags = nla_get_u32(tb[CTA_FILTER_ORIG_FLAGS]);
887 if (filter->orig_flags & ~CTA_FILTER_F_ALL)
891 if (tb[CTA_FILTER_REPLY_FLAGS]) {
892 filter->reply_flags = nla_get_u32(tb[CTA_FILTER_REPLY_FLAGS]);
893 if (filter->reply_flags & ~CTA_FILTER_F_ALL)
900 static int ctnetlink_parse_zone(const struct nlattr *attr,
901 struct nf_conntrack_zone *zone);
902 static int ctnetlink_parse_tuple_filter(const struct nlattr * const cda[],
903 struct nf_conntrack_tuple *tuple,
904 u32 type, u_int8_t l3num,
905 struct nf_conntrack_zone *zone,
908 static struct ctnetlink_filter *
909 ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
911 struct ctnetlink_filter *filter;
914 #ifndef CONFIG_NF_CONNTRACK_MARK
915 if (cda[CTA_MARK] || cda[CTA_MARK_MASK])
916 return ERR_PTR(-EOPNOTSUPP);
919 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
921 return ERR_PTR(-ENOMEM);
923 filter->family = family;
925 #ifdef CONFIG_NF_CONNTRACK_MARK
927 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
928 if (cda[CTA_MARK_MASK])
929 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
931 filter->mark.mask = 0xffffffff;
932 } else if (cda[CTA_MARK_MASK]) {
937 if (!cda[CTA_FILTER])
940 err = ctnetlink_parse_zone(cda[CTA_ZONE], &filter->zone);
944 err = ctnetlink_parse_filter(cda[CTA_FILTER], filter);
948 if (filter->orig_flags) {
949 if (!cda[CTA_TUPLE_ORIG]) {
954 err = ctnetlink_parse_tuple_filter(cda, &filter->orig,
963 if (filter->reply_flags) {
964 if (!cda[CTA_TUPLE_REPLY]) {
969 err = ctnetlink_parse_tuple_filter(cda, &filter->reply,
988 static bool ctnetlink_needs_filter(u8 family, const struct nlattr * const *cda)
990 return family || cda[CTA_MARK] || cda[CTA_FILTER];
993 static int ctnetlink_start(struct netlink_callback *cb)
995 const struct nlattr * const *cda = cb->data;
996 struct ctnetlink_filter *filter = NULL;
997 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
998 u8 family = nfmsg->nfgen_family;
1000 if (ctnetlink_needs_filter(family, cda)) {
1001 filter = ctnetlink_alloc_filter(cda, family);
1003 return PTR_ERR(filter);
1010 static int ctnetlink_filter_match_tuple(struct nf_conntrack_tuple *filter_tuple,
1011 struct nf_conntrack_tuple *ct_tuple,
1012 u_int32_t flags, int family)
1016 if ((flags & CTA_FILTER_FLAG(CTA_IP_SRC)) &&
1017 filter_tuple->src.u3.ip != ct_tuple->src.u3.ip)
1020 if ((flags & CTA_FILTER_FLAG(CTA_IP_DST)) &&
1021 filter_tuple->dst.u3.ip != ct_tuple->dst.u3.ip)
1025 if ((flags & CTA_FILTER_FLAG(CTA_IP_SRC)) &&
1026 !ipv6_addr_cmp(&filter_tuple->src.u3.in6,
1027 &ct_tuple->src.u3.in6))
1030 if ((flags & CTA_FILTER_FLAG(CTA_IP_DST)) &&
1031 !ipv6_addr_cmp(&filter_tuple->dst.u3.in6,
1032 &ct_tuple->dst.u3.in6))
1037 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)) &&
1038 filter_tuple->dst.protonum != ct_tuple->dst.protonum)
1041 switch (ct_tuple->dst.protonum) {
1044 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_SRC_PORT)) &&
1045 filter_tuple->src.u.tcp.port != ct_tuple->src.u.tcp.port)
1048 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_DST_PORT)) &&
1049 filter_tuple->dst.u.tcp.port != ct_tuple->dst.u.tcp.port)
1053 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_TYPE)) &&
1054 filter_tuple->dst.u.icmp.type != ct_tuple->dst.u.icmp.type)
1056 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_CODE)) &&
1057 filter_tuple->dst.u.icmp.code != ct_tuple->dst.u.icmp.code)
1059 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_ID)) &&
1060 filter_tuple->src.u.icmp.id != ct_tuple->src.u.icmp.id)
1063 case IPPROTO_ICMPV6:
1064 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_TYPE)) &&
1065 filter_tuple->dst.u.icmp.type != ct_tuple->dst.u.icmp.type)
1067 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_CODE)) &&
1068 filter_tuple->dst.u.icmp.code != ct_tuple->dst.u.icmp.code)
1070 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_ID)) &&
1071 filter_tuple->src.u.icmp.id != ct_tuple->src.u.icmp.id)
1079 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
1081 struct ctnetlink_filter *filter = data;
1082 struct nf_conntrack_tuple *tuple;
1087 /* Match entries of a given L3 protocol number.
1088 * If it is not specified, ie. l3proto == 0,
1089 * then match everything.
1091 if (filter->family && nf_ct_l3num(ct) != filter->family)
1094 if (filter->orig_flags) {
1095 tuple = nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL);
1096 if (!ctnetlink_filter_match_tuple(&filter->orig, tuple,
1102 if (filter->reply_flags) {
1103 tuple = nf_ct_tuple(ct, IP_CT_DIR_REPLY);
1104 if (!ctnetlink_filter_match_tuple(&filter->reply, tuple,
1105 filter->reply_flags,
1110 #ifdef CONFIG_NF_CONNTRACK_MARK
1111 if ((ct->mark & filter->mark.mask) != filter->mark.val)
1123 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1125 unsigned int flags = cb->data ? NLM_F_DUMP_FILTERED : 0;
1126 struct net *net = sock_net(skb->sk);
1127 struct nf_conn *ct, *last;
1128 struct nf_conntrack_tuple_hash *h;
1129 struct hlist_nulls_node *n;
1130 struct nf_conn *nf_ct_evict[8];
1134 last = (struct nf_conn *)cb->args[1];
1138 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
1142 if (nf_ct_should_gc(nf_ct_evict[i]))
1143 nf_ct_kill(nf_ct_evict[i]);
1144 nf_ct_put(nf_ct_evict[i]);
1147 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
1148 nf_conntrack_lock(lockp);
1149 if (cb->args[0] >= nf_conntrack_htable_size) {
1153 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
1155 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1157 ct = nf_ct_tuplehash_to_ctrack(h);
1158 if (nf_ct_is_expired(ct)) {
1159 if (i < ARRAY_SIZE(nf_ct_evict) &&
1160 atomic_inc_not_zero(&ct->ct_general.use))
1161 nf_ct_evict[i++] = ct;
1165 if (!net_eq(net, nf_ct_net(ct)))
1173 if (!ctnetlink_filter_match(ct, cb->data))
1177 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1179 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1182 nf_conntrack_get(&ct->ct_general);
1183 cb->args[1] = (unsigned long)ct;
1197 /* nf ct hash resize happened, now clear the leftover. */
1198 if ((struct nf_conn *)cb->args[1] == last)
1206 if (nf_ct_should_gc(nf_ct_evict[i]))
1207 nf_ct_kill(nf_ct_evict[i]);
1208 nf_ct_put(nf_ct_evict[i]);
1214 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
1215 struct nf_conntrack_tuple *t,
1218 if (flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1219 if (!tb[CTA_IP_V4_SRC])
1222 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
1225 if (flags & CTA_FILTER_FLAG(CTA_IP_DST)) {
1226 if (!tb[CTA_IP_V4_DST])
1229 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
1235 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
1236 struct nf_conntrack_tuple *t,
1239 if (flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1240 if (!tb[CTA_IP_V6_SRC])
1243 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
1246 if (flags & CTA_FILTER_FLAG(CTA_IP_DST)) {
1247 if (!tb[CTA_IP_V6_DST])
1250 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
1256 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
1257 struct nf_conntrack_tuple *tuple,
1260 struct nlattr *tb[CTA_IP_MAX+1];
1263 ret = nla_parse_nested_deprecated(tb, CTA_IP_MAX, attr, NULL, NULL);
1267 ret = nla_validate_nested_deprecated(attr, CTA_IP_MAX,
1268 cta_ip_nla_policy, NULL);
1272 switch (tuple->src.l3num) {
1274 ret = ipv4_nlattr_to_tuple(tb, tuple, flags);
1277 ret = ipv6_nlattr_to_tuple(tb, tuple, flags);
1284 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
1285 [CTA_PROTO_NUM] = { .type = NLA_U8 },
1288 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
1289 struct nf_conntrack_tuple *tuple,
1292 const struct nf_conntrack_l4proto *l4proto;
1293 struct nlattr *tb[CTA_PROTO_MAX+1];
1296 ret = nla_parse_nested_deprecated(tb, CTA_PROTO_MAX, attr,
1297 proto_nla_policy, NULL);
1301 if (!(flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)))
1304 if (!tb[CTA_PROTO_NUM])
1307 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
1310 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
1312 if (likely(l4proto->nlattr_to_tuple)) {
1313 ret = nla_validate_nested_deprecated(attr, CTA_PROTO_MAX,
1314 l4proto->nla_policy,
1317 ret = l4proto->nlattr_to_tuple(tb, tuple, flags);
1326 ctnetlink_parse_zone(const struct nlattr *attr,
1327 struct nf_conntrack_zone *zone)
1329 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
1330 NF_CT_DEFAULT_ZONE_DIR, 0);
1331 #ifdef CONFIG_NF_CONNTRACK_ZONES
1333 zone->id = ntohs(nla_get_be16(attr));
1342 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1343 struct nf_conntrack_zone *zone)
1347 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1350 ret = ctnetlink_parse_zone(attr, zone);
1354 if (type == CTA_TUPLE_REPLY)
1355 zone->dir = NF_CT_ZONE_DIR_REPL;
1357 zone->dir = NF_CT_ZONE_DIR_ORIG;
1362 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1363 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1364 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1365 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1368 #define CTA_FILTER_F_ALL_CTA_PROTO \
1369 (CTA_FILTER_F_CTA_PROTO_SRC_PORT | \
1370 CTA_FILTER_F_CTA_PROTO_DST_PORT | \
1371 CTA_FILTER_F_CTA_PROTO_ICMP_TYPE | \
1372 CTA_FILTER_F_CTA_PROTO_ICMP_CODE | \
1373 CTA_FILTER_F_CTA_PROTO_ICMP_ID | \
1374 CTA_FILTER_F_CTA_PROTO_ICMPV6_TYPE | \
1375 CTA_FILTER_F_CTA_PROTO_ICMPV6_CODE | \
1376 CTA_FILTER_F_CTA_PROTO_ICMPV6_ID)
1379 ctnetlink_parse_tuple_filter(const struct nlattr * const cda[],
1380 struct nf_conntrack_tuple *tuple, u32 type,
1381 u_int8_t l3num, struct nf_conntrack_zone *zone,
1384 struct nlattr *tb[CTA_TUPLE_MAX+1];
1387 memset(tuple, 0, sizeof(*tuple));
1389 err = nla_parse_nested_deprecated(tb, CTA_TUPLE_MAX, cda[type],
1390 tuple_nla_policy, NULL);
1394 if (l3num != NFPROTO_IPV4 && l3num != NFPROTO_IPV6)
1396 tuple->src.l3num = l3num;
1398 if (flags & CTA_FILTER_FLAG(CTA_IP_DST) ||
1399 flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1400 if (!tb[CTA_TUPLE_IP])
1403 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple, flags);
1408 if (flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)) {
1409 if (!tb[CTA_TUPLE_PROTO])
1412 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple, flags);
1415 } else if (flags & CTA_FILTER_FLAG(ALL_CTA_PROTO)) {
1416 /* Can't manage proto flags without a protonum */
1420 if ((flags & CTA_FILTER_FLAG(CTA_TUPLE_ZONE)) && tb[CTA_TUPLE_ZONE]) {
1424 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1430 /* orig and expect tuples get DIR_ORIGINAL */
1431 if (type == CTA_TUPLE_REPLY)
1432 tuple->dst.dir = IP_CT_DIR_REPLY;
1434 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1440 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1441 struct nf_conntrack_tuple *tuple, u32 type,
1442 u_int8_t l3num, struct nf_conntrack_zone *zone)
1444 return ctnetlink_parse_tuple_filter(cda, tuple, type, l3num, zone,
1445 CTA_FILTER_FLAG(ALL));
1448 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1449 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1450 .len = NF_CT_HELPER_NAME_LEN - 1 },
1453 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1454 struct nlattr **helpinfo)
1457 struct nlattr *tb[CTA_HELP_MAX+1];
1459 err = nla_parse_nested_deprecated(tb, CTA_HELP_MAX, attr,
1460 help_nla_policy, NULL);
1464 if (!tb[CTA_HELP_NAME])
1467 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1469 if (tb[CTA_HELP_INFO])
1470 *helpinfo = tb[CTA_HELP_INFO];
1475 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1476 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1477 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1478 [CTA_STATUS] = { .type = NLA_U32 },
1479 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1480 [CTA_HELP] = { .type = NLA_NESTED },
1481 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1482 [CTA_TIMEOUT] = { .type = NLA_U32 },
1483 [CTA_MARK] = { .type = NLA_U32 },
1484 [CTA_ID] = { .type = NLA_U32 },
1485 [CTA_NAT_DST] = { .type = NLA_NESTED },
1486 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1487 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1488 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1489 [CTA_ZONE] = { .type = NLA_U16 },
1490 [CTA_MARK_MASK] = { .type = NLA_U32 },
1491 [CTA_LABELS] = { .type = NLA_BINARY,
1492 .len = NF_CT_LABELS_MAX_SIZE },
1493 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1494 .len = NF_CT_LABELS_MAX_SIZE },
1495 [CTA_FILTER] = { .type = NLA_NESTED },
1498 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1500 if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
1503 return ctnetlink_filter_match(ct, data);
1506 static int ctnetlink_flush_conntrack(struct net *net,
1507 const struct nlattr * const cda[],
1508 u32 portid, int report, u8 family)
1510 struct ctnetlink_filter *filter = NULL;
1512 if (ctnetlink_needs_filter(family, cda)) {
1513 if (cda[CTA_FILTER])
1516 filter = ctnetlink_alloc_filter(cda, family);
1518 return PTR_ERR(filter);
1521 nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
1528 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1529 struct sk_buff *skb,
1530 const struct nlmsghdr *nlh,
1531 const struct nlattr * const cda[],
1532 struct netlink_ext_ack *extack)
1534 struct nf_conntrack_tuple_hash *h;
1535 struct nf_conntrack_tuple tuple;
1537 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1538 struct nf_conntrack_zone zone;
1541 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1545 if (cda[CTA_TUPLE_ORIG])
1546 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1547 nfmsg->nfgen_family, &zone);
1548 else if (cda[CTA_TUPLE_REPLY])
1549 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1550 nfmsg->nfgen_family, &zone);
1552 u_int8_t u3 = nfmsg->version ? nfmsg->nfgen_family : AF_UNSPEC;
1554 return ctnetlink_flush_conntrack(net, cda,
1555 NETLINK_CB(skb).portid,
1556 nlmsg_report(nlh), u3);
1562 h = nf_conntrack_find_get(net, &zone, &tuple);
1566 ct = nf_ct_tuplehash_to_ctrack(h);
1568 if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) {
1574 __be32 id = nla_get_be32(cda[CTA_ID]);
1576 if (id != (__force __be32)nf_ct_get_id(ct)) {
1582 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1588 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1589 struct sk_buff *skb,
1590 const struct nlmsghdr *nlh,
1591 const struct nlattr * const cda[],
1592 struct netlink_ext_ack *extack)
1594 struct nf_conntrack_tuple_hash *h;
1595 struct nf_conntrack_tuple tuple;
1597 struct sk_buff *skb2 = NULL;
1598 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1599 u_int8_t u3 = nfmsg->nfgen_family;
1600 struct nf_conntrack_zone zone;
1603 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1604 struct netlink_dump_control c = {
1605 .start = ctnetlink_start,
1606 .dump = ctnetlink_dump_table,
1607 .done = ctnetlink_done,
1608 .data = (void *)cda,
1611 return netlink_dump_start(ctnl, skb, nlh, &c);
1614 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1618 if (cda[CTA_TUPLE_ORIG])
1619 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1621 else if (cda[CTA_TUPLE_REPLY])
1622 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1630 h = nf_conntrack_find_get(net, &zone, &tuple);
1634 ct = nf_ct_tuplehash_to_ctrack(h);
1637 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1643 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1644 NFNL_MSG_TYPE(nlh->nlmsg_type), ct, true, 0);
1649 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1658 /* this avoids a loop in nfnetlink. */
1659 return err == -EAGAIN ? -ENOBUFS : err;
1662 static int ctnetlink_done_list(struct netlink_callback *cb)
1665 nf_ct_put((struct nf_conn *)cb->args[1]);
1670 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1672 struct nf_conn *ct, *last;
1673 struct nf_conntrack_tuple_hash *h;
1674 struct hlist_nulls_node *n;
1675 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1676 u_int8_t l3proto = nfmsg->nfgen_family;
1679 struct hlist_nulls_head *list;
1680 struct net *net = sock_net(skb->sk);
1685 last = (struct nf_conn *)cb->args[1];
1687 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1688 struct ct_pcpu *pcpu;
1690 if (!cpu_possible(cpu))
1693 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1694 spin_lock_bh(&pcpu->lock);
1695 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1697 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1698 ct = nf_ct_tuplehash_to_ctrack(h);
1699 if (l3proto && nf_ct_l3num(ct) != l3proto)
1707 /* We can't dump extension info for the unconfirmed
1708 * list because unconfirmed conntracks can have
1709 * ct->ext reallocated (and thus freed).
1711 * In the dying list case ct->ext can't be free'd
1712 * until after we drop pcpu->lock.
1714 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1716 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1717 ct, dying ? true : false, 0);
1719 if (!atomic_inc_not_zero(&ct->ct_general.use))
1722 cb->args[1] = (unsigned long)ct;
1723 spin_unlock_bh(&pcpu->lock);
1731 spin_unlock_bh(&pcpu->lock);
1742 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1744 return ctnetlink_dump_list(skb, cb, true);
1747 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1748 struct sk_buff *skb,
1749 const struct nlmsghdr *nlh,
1750 const struct nlattr * const cda[],
1751 struct netlink_ext_ack *extack)
1753 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1754 struct netlink_dump_control c = {
1755 .dump = ctnetlink_dump_dying,
1756 .done = ctnetlink_done_list,
1758 return netlink_dump_start(ctnl, skb, nlh, &c);
1765 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1767 return ctnetlink_dump_list(skb, cb, false);
1770 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1771 struct sk_buff *skb,
1772 const struct nlmsghdr *nlh,
1773 const struct nlattr * const cda[],
1774 struct netlink_ext_ack *extack)
1776 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1777 struct netlink_dump_control c = {
1778 .dump = ctnetlink_dump_unconfirmed,
1779 .done = ctnetlink_done_list,
1781 return netlink_dump_start(ctnl, skb, nlh, &c);
1787 #if IS_ENABLED(CONFIG_NF_NAT)
1789 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1790 enum nf_nat_manip_type manip,
1791 const struct nlattr *attr)
1794 struct nf_nat_hook *nat_hook;
1797 nat_hook = rcu_dereference(nf_nat_hook);
1799 #ifdef CONFIG_MODULES
1801 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1802 if (request_module("nf-nat") < 0) {
1803 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1807 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1809 nat_hook = rcu_dereference(nf_nat_hook);
1816 err = nat_hook->parse_nat_setup(ct, manip, attr);
1817 if (err == -EAGAIN) {
1818 #ifdef CONFIG_MODULES
1820 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1821 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1822 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1826 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1837 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1842 /* Ignore these unchangable bits */
1843 on &= ~IPS_UNCHANGEABLE_MASK;
1844 off &= ~IPS_UNCHANGEABLE_MASK;
1846 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1847 if (on & (1 << bit))
1848 set_bit(bit, &ct->status);
1849 else if (off & (1 << bit))
1850 clear_bit(bit, &ct->status);
1855 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1858 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1859 d = ct->status ^ status;
1861 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1865 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1866 /* SEEN_REPLY bit can only be set */
1869 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1870 /* ASSURED bit can only be set */
1873 __ctnetlink_change_status(ct, status, 0);
1878 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1880 #if IS_ENABLED(CONFIG_NF_NAT)
1883 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1886 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1891 return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1894 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1900 static int ctnetlink_change_helper(struct nf_conn *ct,
1901 const struct nlattr * const cda[])
1903 struct nf_conntrack_helper *helper;
1904 struct nf_conn_help *help = nfct_help(ct);
1905 char *helpname = NULL;
1906 struct nlattr *helpinfo = NULL;
1909 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1913 /* don't change helper of sibling connections */
1915 /* If we try to change the helper to the same thing twice,
1916 * treat the second attempt as a no-op instead of returning
1922 helper = rcu_dereference(help->helper);
1923 if (helper && !strcmp(helper->name, helpname))
1931 if (!strcmp(helpname, "")) {
1932 if (help && help->helper) {
1933 /* we had a helper before ... */
1934 nf_ct_remove_expectations(ct);
1935 RCU_INIT_POINTER(help->helper, NULL);
1942 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1943 nf_ct_protonum(ct));
1944 if (helper == NULL) {
1950 if (help->helper == helper) {
1951 /* update private helper data if allowed. */
1952 if (helper->from_nlattr)
1953 helper->from_nlattr(helpinfo, ct);
1958 /* we cannot set a helper for an existing conntrack */
1966 static int ctnetlink_change_timeout(struct nf_conn *ct,
1967 const struct nlattr * const cda[])
1969 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1971 if (timeout > INT_MAX)
1973 ct->timeout = nfct_time_stamp + (u32)timeout;
1975 if (test_bit(IPS_DYING_BIT, &ct->status))
1981 #if defined(CONFIG_NF_CONNTRACK_MARK)
1982 static void ctnetlink_change_mark(struct nf_conn *ct,
1983 const struct nlattr * const cda[])
1985 u32 mark, newmark, mask = 0;
1987 if (cda[CTA_MARK_MASK])
1988 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1990 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1991 newmark = (ct->mark & mask) ^ mark;
1992 if (newmark != ct->mark)
1997 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1998 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1999 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
2000 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
2003 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
2004 const struct nlattr * const cda[])
2006 const struct nlattr *attr = cda[CTA_PROTOINFO];
2007 const struct nf_conntrack_l4proto *l4proto;
2008 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
2011 err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_MAX, attr,
2012 protoinfo_policy, NULL);
2016 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
2017 if (l4proto->from_nlattr)
2018 err = l4proto->from_nlattr(tb, ct);
2023 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
2024 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
2025 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
2026 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
2029 static int change_seq_adj(struct nf_ct_seqadj *seq,
2030 const struct nlattr * const attr)
2033 struct nlattr *cda[CTA_SEQADJ_MAX+1];
2035 err = nla_parse_nested_deprecated(cda, CTA_SEQADJ_MAX, attr,
2036 seqadj_policy, NULL);
2040 if (!cda[CTA_SEQADJ_CORRECTION_POS])
2043 seq->correction_pos =
2044 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
2046 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
2049 seq->offset_before =
2050 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
2052 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
2056 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
2062 ctnetlink_change_seq_adj(struct nf_conn *ct,
2063 const struct nlattr * const cda[])
2065 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
2071 spin_lock_bh(&ct->lock);
2072 if (cda[CTA_SEQ_ADJ_ORIG]) {
2073 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
2074 cda[CTA_SEQ_ADJ_ORIG]);
2078 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
2081 if (cda[CTA_SEQ_ADJ_REPLY]) {
2082 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
2083 cda[CTA_SEQ_ADJ_REPLY]);
2087 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
2090 spin_unlock_bh(&ct->lock);
2093 spin_unlock_bh(&ct->lock);
2097 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
2098 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 },
2099 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 },
2100 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 },
2103 static int ctnetlink_change_synproxy(struct nf_conn *ct,
2104 const struct nlattr * const cda[])
2106 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
2107 struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
2113 err = nla_parse_nested_deprecated(tb, CTA_SYNPROXY_MAX,
2114 cda[CTA_SYNPROXY], synproxy_policy,
2119 if (!tb[CTA_SYNPROXY_ISN] ||
2120 !tb[CTA_SYNPROXY_ITS] ||
2121 !tb[CTA_SYNPROXY_TSOFF])
2124 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
2125 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
2126 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
2132 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
2134 #ifdef CONFIG_NF_CONNTRACK_LABELS
2135 size_t len = nla_len(cda[CTA_LABELS]);
2136 const void *mask = cda[CTA_LABELS_MASK];
2138 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
2142 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
2143 nla_len(cda[CTA_LABELS_MASK]) != len)
2145 mask = nla_data(cda[CTA_LABELS_MASK]);
2150 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
2157 ctnetlink_change_conntrack(struct nf_conn *ct,
2158 const struct nlattr * const cda[])
2162 /* only allow NAT changes and master assignation for new conntracks */
2163 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
2166 if (cda[CTA_HELP]) {
2167 err = ctnetlink_change_helper(ct, cda);
2172 if (cda[CTA_TIMEOUT]) {
2173 err = ctnetlink_change_timeout(ct, cda);
2178 if (cda[CTA_STATUS]) {
2179 err = ctnetlink_change_status(ct, cda);
2184 if (cda[CTA_PROTOINFO]) {
2185 err = ctnetlink_change_protoinfo(ct, cda);
2190 #if defined(CONFIG_NF_CONNTRACK_MARK)
2192 ctnetlink_change_mark(ct, cda);
2195 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2196 err = ctnetlink_change_seq_adj(ct, cda);
2201 if (cda[CTA_SYNPROXY]) {
2202 err = ctnetlink_change_synproxy(ct, cda);
2207 if (cda[CTA_LABELS]) {
2208 err = ctnetlink_attach_labels(ct, cda);
2216 static struct nf_conn *
2217 ctnetlink_create_conntrack(struct net *net,
2218 const struct nf_conntrack_zone *zone,
2219 const struct nlattr * const cda[],
2220 struct nf_conntrack_tuple *otuple,
2221 struct nf_conntrack_tuple *rtuple,
2226 struct nf_conntrack_helper *helper;
2227 struct nf_conn_tstamp *tstamp;
2230 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
2232 return ERR_PTR(-ENOMEM);
2234 if (!cda[CTA_TIMEOUT])
2237 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
2238 if (timeout > INT_MAX)
2240 ct->timeout = (u32)timeout + nfct_time_stamp;
2243 if (cda[CTA_HELP]) {
2244 char *helpname = NULL;
2245 struct nlattr *helpinfo = NULL;
2247 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
2251 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2252 nf_ct_protonum(ct));
2253 if (helper == NULL) {
2255 #ifdef CONFIG_MODULES
2256 if (request_module("nfct-helper-%s", helpname) < 0) {
2262 helper = __nf_conntrack_helper_find(helpname,
2264 nf_ct_protonum(ct));
2274 struct nf_conn_help *help;
2276 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
2281 /* set private helper data if allowed. */
2282 if (helper->from_nlattr)
2283 helper->from_nlattr(helpinfo, ct);
2285 /* not in hash table yet so not strictly necessary */
2286 RCU_INIT_POINTER(help->helper, helper);
2289 /* try an implicit helper assignation */
2290 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
2295 err = ctnetlink_setup_nat(ct, cda);
2299 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
2300 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
2301 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
2302 nf_ct_labels_ext_add(ct);
2303 nfct_seqadj_ext_add(ct);
2304 nfct_synproxy_ext_add(ct);
2306 /* we must add conntrack extensions before confirmation. */
2307 ct->status |= IPS_CONFIRMED;
2309 if (cda[CTA_STATUS]) {
2310 err = ctnetlink_change_status(ct, cda);
2315 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2316 err = ctnetlink_change_seq_adj(ct, cda);
2321 memset(&ct->proto, 0, sizeof(ct->proto));
2322 if (cda[CTA_PROTOINFO]) {
2323 err = ctnetlink_change_protoinfo(ct, cda);
2328 if (cda[CTA_SYNPROXY]) {
2329 err = ctnetlink_change_synproxy(ct, cda);
2334 #if defined(CONFIG_NF_CONNTRACK_MARK)
2336 ctnetlink_change_mark(ct, cda);
2339 /* setup master conntrack: this is a confirmed expectation */
2340 if (cda[CTA_TUPLE_MASTER]) {
2341 struct nf_conntrack_tuple master;
2342 struct nf_conntrack_tuple_hash *master_h;
2343 struct nf_conn *master_ct;
2345 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2350 master_h = nf_conntrack_find_get(net, zone, &master);
2351 if (master_h == NULL) {
2355 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2356 __set_bit(IPS_EXPECTED_BIT, &ct->status);
2357 ct->master = master_ct;
2359 tstamp = nf_conn_tstamp_find(ct);
2361 tstamp->start = ktime_get_real_ns();
2363 err = nf_conntrack_hash_check_insert(ct);
2374 nf_conntrack_free(ct);
2375 return ERR_PTR(err);
2378 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
2379 struct sk_buff *skb,
2380 const struct nlmsghdr *nlh,
2381 const struct nlattr * const cda[],
2382 struct netlink_ext_ack *extack)
2384 struct nf_conntrack_tuple otuple, rtuple;
2385 struct nf_conntrack_tuple_hash *h = NULL;
2386 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2388 u_int8_t u3 = nfmsg->nfgen_family;
2389 struct nf_conntrack_zone zone;
2392 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2396 if (cda[CTA_TUPLE_ORIG]) {
2397 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2403 if (cda[CTA_TUPLE_REPLY]) {
2404 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2410 if (cda[CTA_TUPLE_ORIG])
2411 h = nf_conntrack_find_get(net, &zone, &otuple);
2412 else if (cda[CTA_TUPLE_REPLY])
2413 h = nf_conntrack_find_get(net, &zone, &rtuple);
2417 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2418 enum ip_conntrack_events events;
2420 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2422 if (otuple.dst.protonum != rtuple.dst.protonum)
2425 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
2431 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2432 events = 1 << IPCT_RELATED;
2434 events = 1 << IPCT_NEW;
2436 if (cda[CTA_LABELS] &&
2437 ctnetlink_attach_labels(ct, cda) == 0)
2438 events |= (1 << IPCT_LABEL);
2440 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2441 (1 << IPCT_ASSURED) |
2442 (1 << IPCT_HELPER) |
2443 (1 << IPCT_PROTOINFO) |
2444 (1 << IPCT_SEQADJ) |
2446 (1 << IPCT_SYNPROXY) |
2448 ct, NETLINK_CB(skb).portid,
2455 /* implicit 'else' */
2458 ct = nf_ct_tuplehash_to_ctrack(h);
2459 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2460 err = ctnetlink_change_conntrack(ct, cda);
2462 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2463 (1 << IPCT_ASSURED) |
2464 (1 << IPCT_HELPER) |
2466 (1 << IPCT_PROTOINFO) |
2467 (1 << IPCT_SEQADJ) |
2469 (1 << IPCT_SYNPROXY),
2470 ct, NETLINK_CB(skb).portid,
2480 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2481 __u16 cpu, const struct ip_conntrack_stat *st)
2483 struct nlmsghdr *nlh;
2484 struct nfgenmsg *nfmsg;
2485 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2487 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2488 IPCTNL_MSG_CT_GET_STATS_CPU);
2489 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2493 nfmsg = nlmsg_data(nlh);
2494 nfmsg->nfgen_family = AF_UNSPEC;
2495 nfmsg->version = NFNETLINK_V0;
2496 nfmsg->res_id = htons(cpu);
2498 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2499 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2500 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2501 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2502 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2503 htonl(st->insert_failed)) ||
2504 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2505 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2506 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2507 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2508 htonl(st->search_restart)))
2509 goto nla_put_failure;
2511 nlmsg_end(skb, nlh);
2516 nlmsg_cancel(skb, nlh);
2521 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2524 struct net *net = sock_net(skb->sk);
2526 if (cb->args[0] == nr_cpu_ids)
2529 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2530 const struct ip_conntrack_stat *st;
2532 if (!cpu_possible(cpu))
2535 st = per_cpu_ptr(net->ct.stat, cpu);
2536 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2537 NETLINK_CB(cb->skb).portid,
2547 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2548 struct sk_buff *skb,
2549 const struct nlmsghdr *nlh,
2550 const struct nlattr * const cda[],
2551 struct netlink_ext_ack *extack)
2553 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2554 struct netlink_dump_control c = {
2555 .dump = ctnetlink_ct_stat_cpu_dump,
2557 return netlink_dump_start(ctnl, skb, nlh, &c);
2564 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2567 struct nlmsghdr *nlh;
2568 struct nfgenmsg *nfmsg;
2569 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2570 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2572 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2573 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2577 nfmsg = nlmsg_data(nlh);
2578 nfmsg->nfgen_family = AF_UNSPEC;
2579 nfmsg->version = NFNETLINK_V0;
2582 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2583 goto nla_put_failure;
2585 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2586 goto nla_put_failure;
2588 nlmsg_end(skb, nlh);
2593 nlmsg_cancel(skb, nlh);
2597 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2598 struct sk_buff *skb, const struct nlmsghdr *nlh,
2599 const struct nlattr * const cda[],
2600 struct netlink_ext_ack *extack)
2602 struct sk_buff *skb2;
2605 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2609 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2611 NFNL_MSG_TYPE(nlh->nlmsg_type),
2616 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2625 /* this avoids a loop in nfnetlink. */
2626 return err == -EAGAIN ? -ENOBUFS : err;
2629 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2630 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2631 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2632 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2633 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2634 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2635 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2636 .len = NF_CT_HELPER_NAME_LEN - 1 },
2637 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2638 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2639 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2640 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2641 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2644 static struct nf_conntrack_expect *
2645 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2646 struct nf_conntrack_helper *helper,
2647 struct nf_conntrack_tuple *tuple,
2648 struct nf_conntrack_tuple *mask);
2650 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2652 ctnetlink_glue_build_size(const struct nf_conn *ct)
2654 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2655 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2656 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2657 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2658 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2659 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2660 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2661 + nla_total_size(0) /* CTA_PROTOINFO */
2662 + nla_total_size(0) /* CTA_HELP */
2663 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2664 + ctnetlink_secctx_size(ct)
2665 #if IS_ENABLED(CONFIG_NF_NAT)
2666 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2667 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2669 #ifdef CONFIG_NF_CONNTRACK_MARK
2670 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2672 #ifdef CONFIG_NF_CONNTRACK_ZONES
2673 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2675 + ctnetlink_proto_size(ct)
2679 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2680 enum ip_conntrack_info *ctinfo)
2682 return nf_ct_get(skb, ctinfo);
2685 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2687 const struct nf_conntrack_zone *zone;
2688 struct nlattr *nest_parms;
2690 zone = nf_ct_zone(ct);
2692 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
2694 goto nla_put_failure;
2695 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2696 goto nla_put_failure;
2697 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2698 NF_CT_ZONE_DIR_ORIG) < 0)
2699 goto nla_put_failure;
2700 nla_nest_end(skb, nest_parms);
2702 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
2704 goto nla_put_failure;
2705 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2706 goto nla_put_failure;
2707 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2708 NF_CT_ZONE_DIR_REPL) < 0)
2709 goto nla_put_failure;
2710 nla_nest_end(skb, nest_parms);
2712 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2713 NF_CT_DEFAULT_ZONE_DIR) < 0)
2714 goto nla_put_failure;
2716 if (ctnetlink_dump_id(skb, ct) < 0)
2717 goto nla_put_failure;
2719 if (ctnetlink_dump_status(skb, ct) < 0)
2720 goto nla_put_failure;
2722 if (ctnetlink_dump_timeout(skb, ct) < 0)
2723 goto nla_put_failure;
2725 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2726 goto nla_put_failure;
2728 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2729 goto nla_put_failure;
2731 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2732 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2733 goto nla_put_failure;
2735 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2736 goto nla_put_failure;
2738 if ((ct->status & IPS_SEQ_ADJUST) &&
2739 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2740 goto nla_put_failure;
2742 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2743 goto nla_put_failure;
2745 #ifdef CONFIG_NF_CONNTRACK_MARK
2746 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2747 goto nla_put_failure;
2749 if (ctnetlink_dump_labels(skb, ct) < 0)
2750 goto nla_put_failure;
2758 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2759 enum ip_conntrack_info ctinfo,
2760 u_int16_t ct_attr, u_int16_t ct_info_attr)
2762 struct nlattr *nest_parms;
2764 nest_parms = nla_nest_start(skb, ct_attr);
2766 goto nla_put_failure;
2768 if (__ctnetlink_glue_build(skb, ct) < 0)
2769 goto nla_put_failure;
2771 nla_nest_end(skb, nest_parms);
2773 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2774 goto nla_put_failure;
2783 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2785 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2786 unsigned long d = ct->status ^ status;
2788 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2789 /* SEEN_REPLY bit can only be set */
2792 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2793 /* ASSURED bit can only be set */
2796 /* This check is less strict than ctnetlink_change_status()
2797 * because callers often flip IPS_EXPECTED bits when sending
2798 * an NFQA_CT attribute to the kernel. So ignore the
2799 * unchangeable bits but do not error out. Also user programs
2800 * are allowed to clear the bits that they are allowed to change.
2802 __ctnetlink_change_status(ct, status, ~status);
2807 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2811 if (cda[CTA_TIMEOUT]) {
2812 err = ctnetlink_change_timeout(ct, cda);
2816 if (cda[CTA_STATUS]) {
2817 err = ctnetlink_update_status(ct, cda);
2821 if (cda[CTA_HELP]) {
2822 err = ctnetlink_change_helper(ct, cda);
2826 if (cda[CTA_LABELS]) {
2827 err = ctnetlink_attach_labels(ct, cda);
2831 #if defined(CONFIG_NF_CONNTRACK_MARK)
2832 if (cda[CTA_MARK]) {
2833 ctnetlink_change_mark(ct, cda);
2840 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2842 struct nlattr *cda[CTA_MAX+1];
2845 ret = nla_parse_nested_deprecated(cda, CTA_MAX, attr, ct_nla_policy,
2850 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2853 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2854 const struct nf_conn *ct,
2855 struct nf_conntrack_tuple *tuple,
2856 struct nf_conntrack_tuple *mask)
2860 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2861 nf_ct_l3num(ct), NULL);
2865 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2866 nf_ct_l3num(ct), NULL);
2870 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2871 u32 portid, u32 report)
2873 struct nlattr *cda[CTA_EXPECT_MAX+1];
2874 struct nf_conntrack_tuple tuple, mask;
2875 struct nf_conntrack_helper *helper = NULL;
2876 struct nf_conntrack_expect *exp;
2879 err = nla_parse_nested_deprecated(cda, CTA_EXPECT_MAX, attr,
2880 exp_nla_policy, NULL);
2884 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2889 if (cda[CTA_EXPECT_HELP_NAME]) {
2890 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2892 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2893 nf_ct_protonum(ct));
2898 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2899 helper, &tuple, &mask);
2901 return PTR_ERR(exp);
2903 err = nf_ct_expect_related_report(exp, portid, report, 0);
2904 nf_ct_expect_put(exp);
2908 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2909 enum ip_conntrack_info ctinfo, int diff)
2911 if (!(ct->status & IPS_NAT_MASK))
2914 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2917 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2918 .get_ct = ctnetlink_glue_get_ct,
2919 .build_size = ctnetlink_glue_build_size,
2920 .build = ctnetlink_glue_build,
2921 .parse = ctnetlink_glue_parse,
2922 .attach_expect = ctnetlink_glue_attach_expect,
2923 .seq_adjust = ctnetlink_glue_seqadj,
2925 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2927 /***********************************************************************
2929 ***********************************************************************/
2931 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2932 const struct nf_conntrack_tuple *tuple,
2935 struct nlattr *nest_parms;
2937 nest_parms = nla_nest_start(skb, type);
2939 goto nla_put_failure;
2940 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2941 goto nla_put_failure;
2942 nla_nest_end(skb, nest_parms);
2950 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2951 const struct nf_conntrack_tuple *tuple,
2952 const struct nf_conntrack_tuple_mask *mask)
2954 const struct nf_conntrack_l4proto *l4proto;
2955 struct nf_conntrack_tuple m;
2956 struct nlattr *nest_parms;
2959 memset(&m, 0xFF, sizeof(m));
2960 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2961 m.src.u.all = mask->src.u.all;
2962 m.dst.protonum = tuple->dst.protonum;
2964 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK);
2966 goto nla_put_failure;
2969 ret = ctnetlink_dump_tuples_ip(skb, &m);
2971 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
2972 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2976 if (unlikely(ret < 0))
2977 goto nla_put_failure;
2979 nla_nest_end(skb, nest_parms);
2987 static const union nf_inet_addr any_addr;
2989 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2991 static __read_mostly siphash_key_t exp_id_seed;
2992 unsigned long a, b, c, d;
2994 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2996 a = (unsigned long)exp;
2997 b = (unsigned long)exp->helper;
2998 c = (unsigned long)exp->master;
2999 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
3002 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
3004 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
3009 ctnetlink_exp_dump_expect(struct sk_buff *skb,
3010 const struct nf_conntrack_expect *exp)
3012 struct nf_conn *master = exp->master;
3013 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
3014 struct nf_conn_help *help;
3015 #if IS_ENABLED(CONFIG_NF_NAT)
3016 struct nlattr *nest_parms;
3017 struct nf_conntrack_tuple nat_tuple = {};
3019 struct nf_ct_helper_expectfn *expfn;
3024 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
3025 goto nla_put_failure;
3026 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
3027 goto nla_put_failure;
3028 if (ctnetlink_exp_dump_tuple(skb,
3029 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
3030 CTA_EXPECT_MASTER) < 0)
3031 goto nla_put_failure;
3033 #if IS_ENABLED(CONFIG_NF_NAT)
3034 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
3035 exp->saved_proto.all) {
3036 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT);
3038 goto nla_put_failure;
3040 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
3041 goto nla_put_failure;
3043 nat_tuple.src.l3num = nf_ct_l3num(master);
3044 nat_tuple.src.u3 = exp->saved_addr;
3045 nat_tuple.dst.protonum = nf_ct_protonum(master);
3046 nat_tuple.src.u = exp->saved_proto;
3048 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
3049 CTA_EXPECT_NAT_TUPLE) < 0)
3050 goto nla_put_failure;
3051 nla_nest_end(skb, nest_parms);
3054 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
3055 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
3056 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
3057 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
3058 goto nla_put_failure;
3059 help = nfct_help(master);
3061 struct nf_conntrack_helper *helper;
3063 helper = rcu_dereference(help->helper);
3065 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
3066 goto nla_put_failure;
3068 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
3069 if (expfn != NULL &&
3070 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
3071 goto nla_put_failure;
3080 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
3081 int event, const struct nf_conntrack_expect *exp)
3083 struct nlmsghdr *nlh;
3084 struct nfgenmsg *nfmsg;
3085 unsigned int flags = portid ? NLM_F_MULTI : 0;
3087 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
3088 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3092 nfmsg = nlmsg_data(nlh);
3093 nfmsg->nfgen_family = exp->tuple.src.l3num;
3094 nfmsg->version = NFNETLINK_V0;
3097 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
3098 goto nla_put_failure;
3100 nlmsg_end(skb, nlh);
3105 nlmsg_cancel(skb, nlh);
3109 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3111 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
3113 struct nf_conntrack_expect *exp = item->exp;
3114 struct net *net = nf_ct_exp_net(exp);
3115 struct nlmsghdr *nlh;
3116 struct nfgenmsg *nfmsg;
3117 struct sk_buff *skb;
3118 unsigned int type, group;
3121 if (events & (1 << IPEXP_DESTROY)) {
3122 type = IPCTNL_MSG_EXP_DELETE;
3123 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
3124 } else if (events & (1 << IPEXP_NEW)) {
3125 type = IPCTNL_MSG_EXP_NEW;
3126 flags = NLM_F_CREATE|NLM_F_EXCL;
3127 group = NFNLGRP_CONNTRACK_EXP_NEW;
3131 if (!item->report && !nfnetlink_has_listeners(net, group))
3134 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3138 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
3139 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
3143 nfmsg = nlmsg_data(nlh);
3144 nfmsg->nfgen_family = exp->tuple.src.l3num;
3145 nfmsg->version = NFNETLINK_V0;
3148 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
3149 goto nla_put_failure;
3151 nlmsg_end(skb, nlh);
3152 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
3156 nlmsg_cancel(skb, nlh);
3160 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
3164 static int ctnetlink_exp_done(struct netlink_callback *cb)
3167 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
3172 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
3174 struct net *net = sock_net(skb->sk);
3175 struct nf_conntrack_expect *exp, *last;
3176 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3177 u_int8_t l3proto = nfmsg->nfgen_family;
3180 last = (struct nf_conntrack_expect *)cb->args[1];
3181 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
3183 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
3185 if (l3proto && exp->tuple.src.l3num != l3proto)
3188 if (!net_eq(nf_ct_net(exp->master), net))
3196 if (ctnetlink_exp_fill_info(skb,
3197 NETLINK_CB(cb->skb).portid,
3201 if (!refcount_inc_not_zero(&exp->use))
3203 cb->args[1] = (unsigned long)exp;
3215 nf_ct_expect_put(last);
3221 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
3223 struct nf_conntrack_expect *exp, *last;
3224 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3225 struct nf_conn *ct = cb->data;
3226 struct nf_conn_help *help = nfct_help(ct);
3227 u_int8_t l3proto = nfmsg->nfgen_family;
3233 last = (struct nf_conntrack_expect *)cb->args[1];
3235 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
3236 if (l3proto && exp->tuple.src.l3num != l3proto)
3243 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
3247 if (!refcount_inc_not_zero(&exp->use))
3249 cb->args[1] = (unsigned long)exp;
3261 nf_ct_expect_put(last);
3266 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
3267 struct sk_buff *skb,
3268 const struct nlmsghdr *nlh,
3269 const struct nlattr * const cda[],
3270 struct netlink_ext_ack *extack)
3273 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3274 u_int8_t u3 = nfmsg->nfgen_family;
3275 struct nf_conntrack_tuple tuple;
3276 struct nf_conntrack_tuple_hash *h;
3278 struct nf_conntrack_zone zone;
3279 struct netlink_dump_control c = {
3280 .dump = ctnetlink_exp_ct_dump_table,
3281 .done = ctnetlink_exp_done,
3284 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3289 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3293 h = nf_conntrack_find_get(net, &zone, &tuple);
3297 ct = nf_ct_tuplehash_to_ctrack(h);
3298 /* No expectation linked to this connection tracking. */
3299 if (!nfct_help(ct)) {
3306 err = netlink_dump_start(ctnl, skb, nlh, &c);
3312 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
3313 struct sk_buff *skb, const struct nlmsghdr *nlh,
3314 const struct nlattr * const cda[],
3315 struct netlink_ext_ack *extack)
3317 struct nf_conntrack_tuple tuple;
3318 struct nf_conntrack_expect *exp;
3319 struct sk_buff *skb2;
3320 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3321 u_int8_t u3 = nfmsg->nfgen_family;
3322 struct nf_conntrack_zone zone;
3325 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3326 if (cda[CTA_EXPECT_MASTER])
3327 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
3330 struct netlink_dump_control c = {
3331 .dump = ctnetlink_exp_dump_table,
3332 .done = ctnetlink_exp_done,
3334 return netlink_dump_start(ctnl, skb, nlh, &c);
3338 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3342 if (cda[CTA_EXPECT_TUPLE])
3343 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3345 else if (cda[CTA_EXPECT_MASTER])
3346 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3354 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3358 if (cda[CTA_EXPECT_ID]) {
3359 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3361 if (id != nf_expect_get_id(exp)) {
3362 nf_ct_expect_put(exp);
3368 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3370 nf_ct_expect_put(exp);
3375 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3376 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
3378 nf_ct_expect_put(exp);
3382 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
3391 /* this avoids a loop in nfnetlink. */
3392 return err == -EAGAIN ? -ENOBUFS : err;
3395 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3397 const struct nf_conn_help *m_help;
3398 const char *name = data;
3400 m_help = nfct_help(exp->master);
3402 return strcmp(m_help->helper->name, name) == 0;
3405 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3410 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
3411 struct sk_buff *skb, const struct nlmsghdr *nlh,
3412 const struct nlattr * const cda[],
3413 struct netlink_ext_ack *extack)
3415 struct nf_conntrack_expect *exp;
3416 struct nf_conntrack_tuple tuple;
3417 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3418 u_int8_t u3 = nfmsg->nfgen_family;
3419 struct nf_conntrack_zone zone;
3422 if (cda[CTA_EXPECT_TUPLE]) {
3423 /* delete a single expect by tuple */
3424 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3428 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3433 /* bump usage count to 2 */
3434 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3438 if (cda[CTA_EXPECT_ID]) {
3439 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3440 if (ntohl(id) != (u32)(unsigned long)exp) {
3441 nf_ct_expect_put(exp);
3446 /* after list removal, usage count == 1 */
3447 spin_lock_bh(&nf_conntrack_expect_lock);
3448 if (del_timer(&exp->timeout)) {
3449 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3451 nf_ct_expect_put(exp);
3453 spin_unlock_bh(&nf_conntrack_expect_lock);
3454 /* have to put what we 'get' above.
3455 * after this line usage count == 0 */
3456 nf_ct_expect_put(exp);
3457 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3458 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3460 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3461 NETLINK_CB(skb).portid,
3464 /* This basically means we have to flush everything*/
3465 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3466 NETLINK_CB(skb).portid,
3473 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3474 const struct nlattr * const cda[])
3476 if (cda[CTA_EXPECT_TIMEOUT]) {
3477 if (!del_timer(&x->timeout))
3480 x->timeout.expires = jiffies +
3481 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3482 add_timer(&x->timeout);
3487 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3488 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3489 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3493 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3494 struct nf_conntrack_expect *exp,
3497 #if IS_ENABLED(CONFIG_NF_NAT)
3498 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3499 struct nf_conntrack_tuple nat_tuple = {};
3502 err = nla_parse_nested_deprecated(tb, CTA_EXPECT_NAT_MAX, attr,
3503 exp_nat_nla_policy, NULL);
3507 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3510 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3511 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3516 exp->saved_addr = nat_tuple.src.u3;
3517 exp->saved_proto = nat_tuple.src.u;
3518 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3526 static struct nf_conntrack_expect *
3527 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3528 struct nf_conntrack_helper *helper,
3529 struct nf_conntrack_tuple *tuple,
3530 struct nf_conntrack_tuple *mask)
3532 u_int32_t class = 0;
3533 struct nf_conntrack_expect *exp;
3534 struct nf_conn_help *help;
3537 help = nfct_help(ct);
3539 return ERR_PTR(-EOPNOTSUPP);
3541 if (cda[CTA_EXPECT_CLASS] && helper) {
3542 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3543 if (class > helper->expect_class_max)
3544 return ERR_PTR(-EINVAL);
3546 exp = nf_ct_expect_alloc(ct);
3548 return ERR_PTR(-ENOMEM);
3550 if (cda[CTA_EXPECT_FLAGS]) {
3551 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3552 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3556 if (cda[CTA_EXPECT_FN]) {
3557 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3558 struct nf_ct_helper_expectfn *expfn;
3560 expfn = nf_ct_helper_expectfn_find_by_name(name);
3561 if (expfn == NULL) {
3565 exp->expectfn = expfn->expectfn;
3567 exp->expectfn = NULL;
3571 exp->helper = helper;
3572 exp->tuple = *tuple;
3573 exp->mask.src.u3 = mask->src.u3;
3574 exp->mask.src.u.all = mask->src.u.all;
3576 if (cda[CTA_EXPECT_NAT]) {
3577 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3578 exp, nf_ct_l3num(ct));
3584 nf_ct_expect_put(exp);
3585 return ERR_PTR(err);
3589 ctnetlink_create_expect(struct net *net,
3590 const struct nf_conntrack_zone *zone,
3591 const struct nlattr * const cda[],
3592 u_int8_t u3, u32 portid, int report)
3594 struct nf_conntrack_tuple tuple, mask, master_tuple;
3595 struct nf_conntrack_tuple_hash *h = NULL;
3596 struct nf_conntrack_helper *helper = NULL;
3597 struct nf_conntrack_expect *exp;
3601 /* caller guarantees that those three CTA_EXPECT_* exist */
3602 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3606 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3610 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3615 /* Look for master conntrack of this expectation */
3616 h = nf_conntrack_find_get(net, zone, &master_tuple);
3619 ct = nf_ct_tuplehash_to_ctrack(h);
3622 if (cda[CTA_EXPECT_HELP_NAME]) {
3623 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3625 helper = __nf_conntrack_helper_find(helpname, u3,
3626 nf_ct_protonum(ct));
3627 if (helper == NULL) {
3629 #ifdef CONFIG_MODULES
3630 if (request_module("nfct-helper-%s", helpname) < 0) {
3635 helper = __nf_conntrack_helper_find(helpname, u3,
3636 nf_ct_protonum(ct));
3648 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3654 err = nf_ct_expect_related_report(exp, portid, report, 0);
3655 nf_ct_expect_put(exp);
3663 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3664 struct sk_buff *skb, const struct nlmsghdr *nlh,
3665 const struct nlattr * const cda[],
3666 struct netlink_ext_ack *extack)
3668 struct nf_conntrack_tuple tuple;
3669 struct nf_conntrack_expect *exp;
3670 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3671 u_int8_t u3 = nfmsg->nfgen_family;
3672 struct nf_conntrack_zone zone;
3675 if (!cda[CTA_EXPECT_TUPLE]
3676 || !cda[CTA_EXPECT_MASK]
3677 || !cda[CTA_EXPECT_MASTER])
3680 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3684 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3689 spin_lock_bh(&nf_conntrack_expect_lock);
3690 exp = __nf_ct_expect_find(net, &zone, &tuple);
3692 spin_unlock_bh(&nf_conntrack_expect_lock);
3694 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3695 err = ctnetlink_create_expect(net, &zone, cda, u3,
3696 NETLINK_CB(skb).portid,
3703 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3704 err = ctnetlink_change_expect(exp, cda);
3705 spin_unlock_bh(&nf_conntrack_expect_lock);
3711 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3712 const struct ip_conntrack_stat *st)
3714 struct nlmsghdr *nlh;
3715 struct nfgenmsg *nfmsg;
3716 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3718 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3719 IPCTNL_MSG_EXP_GET_STATS_CPU);
3720 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3724 nfmsg = nlmsg_data(nlh);
3725 nfmsg->nfgen_family = AF_UNSPEC;
3726 nfmsg->version = NFNETLINK_V0;
3727 nfmsg->res_id = htons(cpu);
3729 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3730 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3731 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3732 goto nla_put_failure;
3734 nlmsg_end(skb, nlh);
3739 nlmsg_cancel(skb, nlh);
3744 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3747 struct net *net = sock_net(skb->sk);
3749 if (cb->args[0] == nr_cpu_ids)
3752 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3753 const struct ip_conntrack_stat *st;
3755 if (!cpu_possible(cpu))
3758 st = per_cpu_ptr(net->ct.stat, cpu);
3759 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3769 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3770 struct sk_buff *skb,
3771 const struct nlmsghdr *nlh,
3772 const struct nlattr * const cda[],
3773 struct netlink_ext_ack *extack)
3775 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3776 struct netlink_dump_control c = {
3777 .dump = ctnetlink_exp_stat_cpu_dump,
3779 return netlink_dump_start(ctnl, skb, nlh, &c);
3785 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3786 static struct nf_ct_event_notifier ctnl_notifier = {
3787 .fcn = ctnetlink_conntrack_event,
3790 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3791 .fcn = ctnetlink_expect_event,
3795 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3796 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3797 .attr_count = CTA_MAX,
3798 .policy = ct_nla_policy },
3799 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3800 .attr_count = CTA_MAX,
3801 .policy = ct_nla_policy },
3802 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3803 .attr_count = CTA_MAX,
3804 .policy = ct_nla_policy },
3805 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3806 .attr_count = CTA_MAX,
3807 .policy = ct_nla_policy },
3808 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3809 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3810 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3811 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3814 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3815 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3816 .attr_count = CTA_EXPECT_MAX,
3817 .policy = exp_nla_policy },
3818 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3819 .attr_count = CTA_EXPECT_MAX,
3820 .policy = exp_nla_policy },
3821 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3822 .attr_count = CTA_EXPECT_MAX,
3823 .policy = exp_nla_policy },
3824 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3827 static const struct nfnetlink_subsystem ctnl_subsys = {
3828 .name = "conntrack",
3829 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3830 .cb_count = IPCTNL_MSG_MAX,
3834 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3835 .name = "conntrack_expect",
3836 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3837 .cb_count = IPCTNL_MSG_EXP_MAX,
3841 MODULE_ALIAS("ip_conntrack_netlink");
3842 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3843 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3845 static int __net_init ctnetlink_net_init(struct net *net)
3847 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3850 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3852 pr_err("ctnetlink_init: cannot register notifier.\n");
3856 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3858 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3859 goto err_unreg_notifier;
3864 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3866 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3872 static void ctnetlink_net_exit(struct net *net)
3874 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3875 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3876 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3880 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3884 list_for_each_entry(net, net_exit_list, exit_list)
3885 ctnetlink_net_exit(net);
3887 /* wait for other cpus until they are done with ctnl_notifiers */
3891 static struct pernet_operations ctnetlink_net_ops = {
3892 .init = ctnetlink_net_init,
3893 .exit_batch = ctnetlink_net_exit_batch,
3896 static int __init ctnetlink_init(void)
3900 ret = nfnetlink_subsys_register(&ctnl_subsys);
3902 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3906 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3908 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3909 goto err_unreg_subsys;
3912 ret = register_pernet_subsys(&ctnetlink_net_ops);
3914 pr_err("ctnetlink_init: cannot register pernet operations\n");
3915 goto err_unreg_exp_subsys;
3917 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3918 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3919 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3923 err_unreg_exp_subsys:
3924 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3926 nfnetlink_subsys_unregister(&ctnl_subsys);
3931 static void __exit ctnetlink_exit(void)
3933 unregister_pernet_subsys(&ctnetlink_net_ops);
3934 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3935 nfnetlink_subsys_unregister(&ctnl_subsys);
3936 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3937 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3942 module_init(ctnetlink_init);
3943 module_exit(ctnetlink_exit);