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,
173 long timeout = nf_ct_expires(ct) / HZ;
175 if (skip_zero && timeout == 0)
178 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
179 goto nla_put_failure;
186 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct,
189 const struct nf_conntrack_l4proto *l4proto;
190 struct nlattr *nest_proto;
193 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
194 if (!l4proto->to_nlattr)
197 nest_proto = nla_nest_start(skb, CTA_PROTOINFO);
199 goto nla_put_failure;
201 ret = l4proto->to_nlattr(skb, nest_proto, ct, destroy);
203 nla_nest_end(skb, nest_proto);
211 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
212 const struct nf_conn *ct)
214 struct nlattr *nest_helper;
215 const struct nf_conn_help *help = nfct_help(ct);
216 struct nf_conntrack_helper *helper;
221 helper = rcu_dereference(help->helper);
225 nest_helper = nla_nest_start(skb, CTA_HELP);
227 goto nla_put_failure;
228 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
229 goto nla_put_failure;
231 if (helper->to_nlattr)
232 helper->to_nlattr(skb, ct);
234 nla_nest_end(skb, nest_helper);
243 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
244 enum ip_conntrack_dir dir, int type)
246 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
247 struct nf_conn_counter *counter = acct->counter;
248 struct nlattr *nest_count;
251 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
252 pkts = atomic64_xchg(&counter[dir].packets, 0);
253 bytes = atomic64_xchg(&counter[dir].bytes, 0);
255 pkts = atomic64_read(&counter[dir].packets);
256 bytes = atomic64_read(&counter[dir].bytes);
259 nest_count = nla_nest_start(skb, attr);
261 goto nla_put_failure;
263 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
265 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
267 goto nla_put_failure;
269 nla_nest_end(skb, nest_count);
278 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
280 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
285 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
287 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
294 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
296 struct nlattr *nest_count;
297 const struct nf_conn_tstamp *tstamp;
299 tstamp = nf_conn_tstamp_find(ct);
303 nest_count = nla_nest_start(skb, CTA_TIMESTAMP);
305 goto nla_put_failure;
307 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
308 CTA_TIMESTAMP_PAD) ||
309 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
310 cpu_to_be64(tstamp->stop),
312 goto nla_put_failure;
313 nla_nest_end(skb, nest_count);
321 #ifdef CONFIG_NF_CONNTRACK_MARK
322 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
324 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
325 goto nla_put_failure;
332 #define ctnetlink_dump_mark(a, b) (0)
335 #ifdef CONFIG_NF_CONNTRACK_SECMARK
336 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
338 struct nlattr *nest_secctx;
342 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
347 nest_secctx = nla_nest_start(skb, CTA_SECCTX);
349 goto nla_put_failure;
351 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
352 goto nla_put_failure;
353 nla_nest_end(skb, nest_secctx);
357 security_release_secctx(secctx, len);
361 #define ctnetlink_dump_secctx(a, b) (0)
364 #ifdef CONFIG_NF_CONNTRACK_LABELS
365 static inline int ctnetlink_label_size(const struct nf_conn *ct)
367 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
371 return nla_total_size(sizeof(labels->bits));
375 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
377 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
385 if (labels->bits[i] != 0)
386 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
389 } while (i < ARRAY_SIZE(labels->bits));
394 #define ctnetlink_dump_labels(a, b) (0)
395 #define ctnetlink_label_size(a) (0)
398 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
400 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
402 struct nlattr *nest_parms;
404 if (!(ct->status & IPS_EXPECTED))
407 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER);
409 goto nla_put_failure;
410 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
411 goto nla_put_failure;
412 nla_nest_end(skb, nest_parms);
421 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
423 struct nlattr *nest_parms;
425 nest_parms = nla_nest_start(skb, type);
427 goto nla_put_failure;
429 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
430 htonl(seq->correction_pos)) ||
431 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
432 htonl(seq->offset_before)) ||
433 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
434 htonl(seq->offset_after)))
435 goto nla_put_failure;
437 nla_nest_end(skb, nest_parms);
445 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
447 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
448 struct nf_ct_seqadj *seq;
450 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
453 spin_lock_bh(&ct->lock);
454 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
455 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
458 seq = &seqadj->seq[IP_CT_DIR_REPLY];
459 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
462 spin_unlock_bh(&ct->lock);
465 spin_unlock_bh(&ct->lock);
469 static int ctnetlink_dump_ct_synproxy(struct sk_buff *skb, struct nf_conn *ct)
471 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
472 struct nlattr *nest_parms;
477 nest_parms = nla_nest_start(skb, CTA_SYNPROXY);
479 goto nla_put_failure;
481 if (nla_put_be32(skb, CTA_SYNPROXY_ISN, htonl(synproxy->isn)) ||
482 nla_put_be32(skb, CTA_SYNPROXY_ITS, htonl(synproxy->its)) ||
483 nla_put_be32(skb, CTA_SYNPROXY_TSOFF, htonl(synproxy->tsoff)))
484 goto nla_put_failure;
486 nla_nest_end(skb, nest_parms);
494 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
496 __be32 id = (__force __be32)nf_ct_get_id(ct);
498 if (nla_put_be32(skb, CTA_ID, id))
499 goto nla_put_failure;
506 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
508 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
509 goto nla_put_failure;
516 /* all these functions access ct->ext. Caller must either hold a reference
517 * on ct or prevent its deletion by holding either the bucket spinlock or
518 * pcpu dying list lock.
520 static int ctnetlink_dump_extinfo(struct sk_buff *skb,
521 struct nf_conn *ct, u32 type)
523 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
524 ctnetlink_dump_timestamp(skb, ct) < 0 ||
525 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
526 ctnetlink_dump_labels(skb, ct) < 0 ||
527 ctnetlink_dump_ct_seq_adj(skb, ct) < 0 ||
528 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
534 static int ctnetlink_dump_info(struct sk_buff *skb, struct nf_conn *ct)
536 if (ctnetlink_dump_status(skb, ct) < 0 ||
537 ctnetlink_dump_mark(skb, ct) < 0 ||
538 ctnetlink_dump_secctx(skb, ct) < 0 ||
539 ctnetlink_dump_id(skb, ct) < 0 ||
540 ctnetlink_dump_use(skb, ct) < 0 ||
541 ctnetlink_dump_master(skb, ct) < 0)
544 if (!test_bit(IPS_OFFLOAD_BIT, &ct->status) &&
545 (ctnetlink_dump_timeout(skb, ct, false) < 0 ||
546 ctnetlink_dump_protoinfo(skb, ct, false) < 0))
553 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
554 struct nf_conn *ct, bool extinfo, unsigned int flags)
556 const struct nf_conntrack_zone *zone;
557 struct nlmsghdr *nlh;
558 struct nlattr *nest_parms;
562 flags |= NLM_F_MULTI;
563 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
564 nlh = nfnl_msg_put(skb, portid, seq, event, flags, nf_ct_l3num(ct),
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 nlattr *nest_parms;
712 struct nf_conn *ct = item->ct;
715 unsigned int flags = 0, group;
718 if (events & (1 << IPCT_DESTROY)) {
719 type = IPCTNL_MSG_CT_DELETE;
720 group = NFNLGRP_CONNTRACK_DESTROY;
721 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
722 type = IPCTNL_MSG_CT_NEW;
723 flags = NLM_F_CREATE|NLM_F_EXCL;
724 group = NFNLGRP_CONNTRACK_NEW;
726 type = IPCTNL_MSG_CT_NEW;
727 group = NFNLGRP_CONNTRACK_UPDATE;
732 if (!item->report && !nfnetlink_has_listeners(net, group))
735 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
739 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
740 nlh = nfnl_msg_put(skb, item->portid, 0, type, flags, nf_ct_l3num(ct),
745 zone = nf_ct_zone(ct);
747 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
749 goto nla_put_failure;
750 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
751 goto nla_put_failure;
752 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
753 NF_CT_ZONE_DIR_ORIG) < 0)
754 goto nla_put_failure;
755 nla_nest_end(skb, nest_parms);
757 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
759 goto nla_put_failure;
760 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
761 goto nla_put_failure;
762 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
763 NF_CT_ZONE_DIR_REPL) < 0)
764 goto nla_put_failure;
765 nla_nest_end(skb, nest_parms);
767 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
768 NF_CT_DEFAULT_ZONE_DIR) < 0)
769 goto nla_put_failure;
771 if (ctnetlink_dump_id(skb, ct) < 0)
772 goto nla_put_failure;
774 if (ctnetlink_dump_status(skb, ct) < 0)
775 goto nla_put_failure;
777 if (events & (1 << IPCT_DESTROY)) {
778 if (ctnetlink_dump_timeout(skb, ct, true) < 0)
779 goto nla_put_failure;
781 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
782 ctnetlink_dump_timestamp(skb, ct) < 0 ||
783 ctnetlink_dump_protoinfo(skb, ct, true) < 0)
784 goto nla_put_failure;
786 if (ctnetlink_dump_timeout(skb, ct, false) < 0)
787 goto nla_put_failure;
789 if (events & (1 << IPCT_PROTOINFO) &&
790 ctnetlink_dump_protoinfo(skb, ct, false) < 0)
791 goto nla_put_failure;
793 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
794 && ctnetlink_dump_helpinfo(skb, ct) < 0)
795 goto nla_put_failure;
797 #ifdef CONFIG_NF_CONNTRACK_SECMARK
798 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
799 && ctnetlink_dump_secctx(skb, ct) < 0)
800 goto nla_put_failure;
802 if (events & (1 << IPCT_LABEL) &&
803 ctnetlink_dump_labels(skb, ct) < 0)
804 goto nla_put_failure;
806 if (events & (1 << IPCT_RELATED) &&
807 ctnetlink_dump_master(skb, ct) < 0)
808 goto nla_put_failure;
810 if (events & (1 << IPCT_SEQADJ) &&
811 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
812 goto nla_put_failure;
814 if (events & (1 << IPCT_SYNPROXY) &&
815 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
816 goto nla_put_failure;
819 #ifdef CONFIG_NF_CONNTRACK_MARK
820 if ((events & (1 << IPCT_MARK) || ct->mark)
821 && ctnetlink_dump_mark(skb, ct) < 0)
822 goto nla_put_failure;
825 err = nfnetlink_send(skb, net, item->portid, group, item->report,
827 if (err == -ENOBUFS || err == -EAGAIN)
833 nlmsg_cancel(skb, nlh);
837 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
842 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
844 static int ctnetlink_done(struct netlink_callback *cb)
847 nf_ct_put((struct nf_conn *)cb->args[1]);
852 struct ctnetlink_filter {
855 u_int32_t orig_flags;
856 u_int32_t reply_flags;
858 struct nf_conntrack_tuple orig;
859 struct nf_conntrack_tuple reply;
860 struct nf_conntrack_zone zone;
868 static const struct nla_policy cta_filter_nla_policy[CTA_FILTER_MAX + 1] = {
869 [CTA_FILTER_ORIG_FLAGS] = { .type = NLA_U32 },
870 [CTA_FILTER_REPLY_FLAGS] = { .type = NLA_U32 },
873 static int ctnetlink_parse_filter(const struct nlattr *attr,
874 struct ctnetlink_filter *filter)
876 struct nlattr *tb[CTA_FILTER_MAX + 1];
879 ret = nla_parse_nested(tb, CTA_FILTER_MAX, attr, cta_filter_nla_policy,
884 if (tb[CTA_FILTER_ORIG_FLAGS]) {
885 filter->orig_flags = nla_get_u32(tb[CTA_FILTER_ORIG_FLAGS]);
886 if (filter->orig_flags & ~CTA_FILTER_F_ALL)
890 if (tb[CTA_FILTER_REPLY_FLAGS]) {
891 filter->reply_flags = nla_get_u32(tb[CTA_FILTER_REPLY_FLAGS]);
892 if (filter->reply_flags & ~CTA_FILTER_F_ALL)
899 static int ctnetlink_parse_zone(const struct nlattr *attr,
900 struct nf_conntrack_zone *zone);
901 static int ctnetlink_parse_tuple_filter(const struct nlattr * const cda[],
902 struct nf_conntrack_tuple *tuple,
903 u32 type, u_int8_t l3num,
904 struct nf_conntrack_zone *zone,
907 static struct ctnetlink_filter *
908 ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
910 struct ctnetlink_filter *filter;
913 #ifndef CONFIG_NF_CONNTRACK_MARK
914 if (cda[CTA_MARK] || cda[CTA_MARK_MASK])
915 return ERR_PTR(-EOPNOTSUPP);
918 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
920 return ERR_PTR(-ENOMEM);
922 filter->family = family;
924 #ifdef CONFIG_NF_CONNTRACK_MARK
926 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
927 if (cda[CTA_MARK_MASK])
928 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
930 filter->mark.mask = 0xffffffff;
931 } else if (cda[CTA_MARK_MASK]) {
936 if (!cda[CTA_FILTER])
939 err = ctnetlink_parse_zone(cda[CTA_ZONE], &filter->zone);
943 err = ctnetlink_parse_filter(cda[CTA_FILTER], filter);
947 if (filter->orig_flags) {
948 if (!cda[CTA_TUPLE_ORIG]) {
953 err = ctnetlink_parse_tuple_filter(cda, &filter->orig,
962 if (filter->reply_flags) {
963 if (!cda[CTA_TUPLE_REPLY]) {
968 err = ctnetlink_parse_tuple_filter(cda, &filter->reply,
987 static bool ctnetlink_needs_filter(u8 family, const struct nlattr * const *cda)
989 return family || cda[CTA_MARK] || cda[CTA_FILTER];
992 static int ctnetlink_start(struct netlink_callback *cb)
994 const struct nlattr * const *cda = cb->data;
995 struct ctnetlink_filter *filter = NULL;
996 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
997 u8 family = nfmsg->nfgen_family;
999 if (ctnetlink_needs_filter(family, cda)) {
1000 filter = ctnetlink_alloc_filter(cda, family);
1002 return PTR_ERR(filter);
1009 static int ctnetlink_filter_match_tuple(struct nf_conntrack_tuple *filter_tuple,
1010 struct nf_conntrack_tuple *ct_tuple,
1011 u_int32_t flags, int family)
1015 if ((flags & CTA_FILTER_FLAG(CTA_IP_SRC)) &&
1016 filter_tuple->src.u3.ip != ct_tuple->src.u3.ip)
1019 if ((flags & CTA_FILTER_FLAG(CTA_IP_DST)) &&
1020 filter_tuple->dst.u3.ip != ct_tuple->dst.u3.ip)
1024 if ((flags & CTA_FILTER_FLAG(CTA_IP_SRC)) &&
1025 !ipv6_addr_cmp(&filter_tuple->src.u3.in6,
1026 &ct_tuple->src.u3.in6))
1029 if ((flags & CTA_FILTER_FLAG(CTA_IP_DST)) &&
1030 !ipv6_addr_cmp(&filter_tuple->dst.u3.in6,
1031 &ct_tuple->dst.u3.in6))
1036 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)) &&
1037 filter_tuple->dst.protonum != ct_tuple->dst.protonum)
1040 switch (ct_tuple->dst.protonum) {
1043 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_SRC_PORT)) &&
1044 filter_tuple->src.u.tcp.port != ct_tuple->src.u.tcp.port)
1047 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_DST_PORT)) &&
1048 filter_tuple->dst.u.tcp.port != ct_tuple->dst.u.tcp.port)
1052 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_TYPE)) &&
1053 filter_tuple->dst.u.icmp.type != ct_tuple->dst.u.icmp.type)
1055 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_CODE)) &&
1056 filter_tuple->dst.u.icmp.code != ct_tuple->dst.u.icmp.code)
1058 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMP_ID)) &&
1059 filter_tuple->src.u.icmp.id != ct_tuple->src.u.icmp.id)
1062 case IPPROTO_ICMPV6:
1063 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_TYPE)) &&
1064 filter_tuple->dst.u.icmp.type != ct_tuple->dst.u.icmp.type)
1066 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_CODE)) &&
1067 filter_tuple->dst.u.icmp.code != ct_tuple->dst.u.icmp.code)
1069 if ((flags & CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_ID)) &&
1070 filter_tuple->src.u.icmp.id != ct_tuple->src.u.icmp.id)
1078 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
1080 struct ctnetlink_filter *filter = data;
1081 struct nf_conntrack_tuple *tuple;
1086 /* Match entries of a given L3 protocol number.
1087 * If it is not specified, ie. l3proto == 0,
1088 * then match everything.
1090 if (filter->family && nf_ct_l3num(ct) != filter->family)
1093 if (filter->orig_flags) {
1094 tuple = nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL);
1095 if (!ctnetlink_filter_match_tuple(&filter->orig, tuple,
1101 if (filter->reply_flags) {
1102 tuple = nf_ct_tuple(ct, IP_CT_DIR_REPLY);
1103 if (!ctnetlink_filter_match_tuple(&filter->reply, tuple,
1104 filter->reply_flags,
1109 #ifdef CONFIG_NF_CONNTRACK_MARK
1110 if ((ct->mark & filter->mark.mask) != filter->mark.val)
1122 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1124 unsigned int flags = cb->data ? NLM_F_DUMP_FILTERED : 0;
1125 struct net *net = sock_net(skb->sk);
1126 struct nf_conn *ct, *last;
1127 struct nf_conntrack_tuple_hash *h;
1128 struct hlist_nulls_node *n;
1129 struct nf_conn *nf_ct_evict[8];
1133 last = (struct nf_conn *)cb->args[1];
1137 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
1141 if (nf_ct_should_gc(nf_ct_evict[i]))
1142 nf_ct_kill(nf_ct_evict[i]);
1143 nf_ct_put(nf_ct_evict[i]);
1146 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
1147 nf_conntrack_lock(lockp);
1148 if (cb->args[0] >= nf_conntrack_htable_size) {
1152 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
1154 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
1156 ct = nf_ct_tuplehash_to_ctrack(h);
1157 if (nf_ct_is_expired(ct)) {
1158 if (i < ARRAY_SIZE(nf_ct_evict) &&
1159 atomic_inc_not_zero(&ct->ct_general.use))
1160 nf_ct_evict[i++] = ct;
1164 if (!net_eq(net, nf_ct_net(ct)))
1172 if (!ctnetlink_filter_match(ct, cb->data))
1176 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1178 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1181 nf_conntrack_get(&ct->ct_general);
1182 cb->args[1] = (unsigned long)ct;
1196 /* nf ct hash resize happened, now clear the leftover. */
1197 if ((struct nf_conn *)cb->args[1] == last)
1205 if (nf_ct_should_gc(nf_ct_evict[i]))
1206 nf_ct_kill(nf_ct_evict[i]);
1207 nf_ct_put(nf_ct_evict[i]);
1213 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
1214 struct nf_conntrack_tuple *t,
1217 if (flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1218 if (!tb[CTA_IP_V4_SRC])
1221 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
1224 if (flags & CTA_FILTER_FLAG(CTA_IP_DST)) {
1225 if (!tb[CTA_IP_V4_DST])
1228 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
1234 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
1235 struct nf_conntrack_tuple *t,
1238 if (flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1239 if (!tb[CTA_IP_V6_SRC])
1242 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
1245 if (flags & CTA_FILTER_FLAG(CTA_IP_DST)) {
1246 if (!tb[CTA_IP_V6_DST])
1249 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
1255 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
1256 struct nf_conntrack_tuple *tuple,
1259 struct nlattr *tb[CTA_IP_MAX+1];
1262 ret = nla_parse_nested_deprecated(tb, CTA_IP_MAX, attr, NULL, NULL);
1266 ret = nla_validate_nested_deprecated(attr, CTA_IP_MAX,
1267 cta_ip_nla_policy, NULL);
1271 switch (tuple->src.l3num) {
1273 ret = ipv4_nlattr_to_tuple(tb, tuple, flags);
1276 ret = ipv6_nlattr_to_tuple(tb, tuple, flags);
1283 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
1284 [CTA_PROTO_NUM] = { .type = NLA_U8 },
1287 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
1288 struct nf_conntrack_tuple *tuple,
1291 const struct nf_conntrack_l4proto *l4proto;
1292 struct nlattr *tb[CTA_PROTO_MAX+1];
1295 ret = nla_parse_nested_deprecated(tb, CTA_PROTO_MAX, attr,
1296 proto_nla_policy, NULL);
1300 if (!(flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)))
1303 if (!tb[CTA_PROTO_NUM])
1306 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
1309 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
1311 if (likely(l4proto->nlattr_to_tuple)) {
1312 ret = nla_validate_nested_deprecated(attr, CTA_PROTO_MAX,
1313 l4proto->nla_policy,
1316 ret = l4proto->nlattr_to_tuple(tb, tuple, flags);
1325 ctnetlink_parse_zone(const struct nlattr *attr,
1326 struct nf_conntrack_zone *zone)
1328 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
1329 NF_CT_DEFAULT_ZONE_DIR, 0);
1330 #ifdef CONFIG_NF_CONNTRACK_ZONES
1332 zone->id = ntohs(nla_get_be16(attr));
1341 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1342 struct nf_conntrack_zone *zone)
1346 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1349 ret = ctnetlink_parse_zone(attr, zone);
1353 if (type == CTA_TUPLE_REPLY)
1354 zone->dir = NF_CT_ZONE_DIR_REPL;
1356 zone->dir = NF_CT_ZONE_DIR_ORIG;
1361 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1362 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1363 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1364 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1367 #define CTA_FILTER_F_ALL_CTA_PROTO \
1368 (CTA_FILTER_F_CTA_PROTO_SRC_PORT | \
1369 CTA_FILTER_F_CTA_PROTO_DST_PORT | \
1370 CTA_FILTER_F_CTA_PROTO_ICMP_TYPE | \
1371 CTA_FILTER_F_CTA_PROTO_ICMP_CODE | \
1372 CTA_FILTER_F_CTA_PROTO_ICMP_ID | \
1373 CTA_FILTER_F_CTA_PROTO_ICMPV6_TYPE | \
1374 CTA_FILTER_F_CTA_PROTO_ICMPV6_CODE | \
1375 CTA_FILTER_F_CTA_PROTO_ICMPV6_ID)
1378 ctnetlink_parse_tuple_filter(const struct nlattr * const cda[],
1379 struct nf_conntrack_tuple *tuple, u32 type,
1380 u_int8_t l3num, struct nf_conntrack_zone *zone,
1383 struct nlattr *tb[CTA_TUPLE_MAX+1];
1386 memset(tuple, 0, sizeof(*tuple));
1388 err = nla_parse_nested_deprecated(tb, CTA_TUPLE_MAX, cda[type],
1389 tuple_nla_policy, NULL);
1393 if (l3num != NFPROTO_IPV4 && l3num != NFPROTO_IPV6)
1395 tuple->src.l3num = l3num;
1397 if (flags & CTA_FILTER_FLAG(CTA_IP_DST) ||
1398 flags & CTA_FILTER_FLAG(CTA_IP_SRC)) {
1399 if (!tb[CTA_TUPLE_IP])
1402 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple, flags);
1407 if (flags & CTA_FILTER_FLAG(CTA_PROTO_NUM)) {
1408 if (!tb[CTA_TUPLE_PROTO])
1411 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple, flags);
1414 } else if (flags & CTA_FILTER_FLAG(ALL_CTA_PROTO)) {
1415 /* Can't manage proto flags without a protonum */
1419 if ((flags & CTA_FILTER_FLAG(CTA_TUPLE_ZONE)) && tb[CTA_TUPLE_ZONE]) {
1423 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1429 /* orig and expect tuples get DIR_ORIGINAL */
1430 if (type == CTA_TUPLE_REPLY)
1431 tuple->dst.dir = IP_CT_DIR_REPLY;
1433 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1439 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1440 struct nf_conntrack_tuple *tuple, u32 type,
1441 u_int8_t l3num, struct nf_conntrack_zone *zone)
1443 return ctnetlink_parse_tuple_filter(cda, tuple, type, l3num, zone,
1444 CTA_FILTER_FLAG(ALL));
1447 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1448 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1449 .len = NF_CT_HELPER_NAME_LEN - 1 },
1452 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1453 struct nlattr **helpinfo)
1456 struct nlattr *tb[CTA_HELP_MAX+1];
1458 err = nla_parse_nested_deprecated(tb, CTA_HELP_MAX, attr,
1459 help_nla_policy, NULL);
1463 if (!tb[CTA_HELP_NAME])
1466 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1468 if (tb[CTA_HELP_INFO])
1469 *helpinfo = tb[CTA_HELP_INFO];
1474 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1475 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1476 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1477 [CTA_STATUS] = { .type = NLA_U32 },
1478 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1479 [CTA_HELP] = { .type = NLA_NESTED },
1480 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1481 [CTA_TIMEOUT] = { .type = NLA_U32 },
1482 [CTA_MARK] = { .type = NLA_U32 },
1483 [CTA_ID] = { .type = NLA_U32 },
1484 [CTA_NAT_DST] = { .type = NLA_NESTED },
1485 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1486 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1487 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1488 [CTA_ZONE] = { .type = NLA_U16 },
1489 [CTA_MARK_MASK] = { .type = NLA_U32 },
1490 [CTA_LABELS] = { .type = NLA_BINARY,
1491 .len = NF_CT_LABELS_MAX_SIZE },
1492 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1493 .len = NF_CT_LABELS_MAX_SIZE },
1494 [CTA_FILTER] = { .type = NLA_NESTED },
1497 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1499 if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
1502 return ctnetlink_filter_match(ct, data);
1505 static int ctnetlink_flush_conntrack(struct net *net,
1506 const struct nlattr * const cda[],
1507 u32 portid, int report, u8 family)
1509 struct ctnetlink_filter *filter = NULL;
1511 if (ctnetlink_needs_filter(family, cda)) {
1512 if (cda[CTA_FILTER])
1515 filter = ctnetlink_alloc_filter(cda, family);
1517 return PTR_ERR(filter);
1520 nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
1527 static int ctnetlink_del_conntrack(struct sk_buff *skb,
1528 const struct nfnl_info *info,
1529 const struct nlattr * const cda[])
1531 struct nfgenmsg *nfmsg = nlmsg_data(info->nlh);
1532 struct nf_conntrack_tuple_hash *h;
1533 struct nf_conntrack_tuple tuple;
1534 struct nf_conntrack_zone zone;
1538 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1542 if (cda[CTA_TUPLE_ORIG])
1543 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1544 nfmsg->nfgen_family, &zone);
1545 else if (cda[CTA_TUPLE_REPLY])
1546 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1547 nfmsg->nfgen_family, &zone);
1549 u_int8_t u3 = nfmsg->version ? nfmsg->nfgen_family : AF_UNSPEC;
1551 return ctnetlink_flush_conntrack(info->net, cda,
1552 NETLINK_CB(skb).portid,
1553 nlmsg_report(info->nlh), u3);
1559 h = nf_conntrack_find_get(info->net, &zone, &tuple);
1563 ct = nf_ct_tuplehash_to_ctrack(h);
1565 if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) {
1571 __be32 id = nla_get_be32(cda[CTA_ID]);
1573 if (id != (__force __be32)nf_ct_get_id(ct)) {
1579 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(info->nlh));
1585 static int ctnetlink_get_conntrack(struct sk_buff *skb,
1586 const struct nfnl_info *info,
1587 const struct nlattr * const cda[])
1589 struct nfgenmsg *nfmsg = nlmsg_data(info->nlh);
1590 u_int8_t u3 = nfmsg->nfgen_family;
1591 struct nf_conntrack_tuple_hash *h;
1592 struct nf_conntrack_tuple tuple;
1593 struct nf_conntrack_zone zone;
1594 struct sk_buff *skb2;
1598 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1599 struct netlink_dump_control c = {
1600 .start = ctnetlink_start,
1601 .dump = ctnetlink_dump_table,
1602 .done = ctnetlink_done,
1603 .data = (void *)cda,
1606 return netlink_dump_start(info->sk, skb, info->nlh, &c);
1609 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1613 if (cda[CTA_TUPLE_ORIG])
1614 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1616 else if (cda[CTA_TUPLE_REPLY])
1617 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1625 h = nf_conntrack_find_get(info->net, &zone, &tuple);
1629 ct = nf_ct_tuplehash_to_ctrack(h);
1632 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1638 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid,
1639 info->nlh->nlmsg_seq,
1640 NFNL_MSG_TYPE(info->nlh->nlmsg_type), ct,
1646 err = netlink_unicast(info->sk, skb2, NETLINK_CB(skb).portid,
1656 /* this avoids a loop in nfnetlink. */
1657 return err == -EAGAIN ? -ENOBUFS : err;
1660 static int ctnetlink_done_list(struct netlink_callback *cb)
1663 nf_ct_put((struct nf_conn *)cb->args[1]);
1668 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1670 struct nf_conn *ct, *last;
1671 struct nf_conntrack_tuple_hash *h;
1672 struct hlist_nulls_node *n;
1673 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1674 u_int8_t l3proto = nfmsg->nfgen_family;
1677 struct hlist_nulls_head *list;
1678 struct net *net = sock_net(skb->sk);
1683 last = (struct nf_conn *)cb->args[1];
1685 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1686 struct ct_pcpu *pcpu;
1688 if (!cpu_possible(cpu))
1691 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1692 spin_lock_bh(&pcpu->lock);
1693 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1695 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1696 ct = nf_ct_tuplehash_to_ctrack(h);
1697 if (l3proto && nf_ct_l3num(ct) != l3proto)
1705 /* We can't dump extension info for the unconfirmed
1706 * list because unconfirmed conntracks can have
1707 * ct->ext reallocated (and thus freed).
1709 * In the dying list case ct->ext can't be free'd
1710 * until after we drop pcpu->lock.
1712 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1714 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1715 ct, dying ? true : false, 0);
1717 if (!atomic_inc_not_zero(&ct->ct_general.use))
1720 cb->args[1] = (unsigned long)ct;
1721 spin_unlock_bh(&pcpu->lock);
1729 spin_unlock_bh(&pcpu->lock);
1740 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1742 return ctnetlink_dump_list(skb, cb, true);
1745 static int ctnetlink_get_ct_dying(struct sk_buff *skb,
1746 const struct nfnl_info *info,
1747 const struct nlattr * const cda[])
1749 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1750 struct netlink_dump_control c = {
1751 .dump = ctnetlink_dump_dying,
1752 .done = ctnetlink_done_list,
1754 return netlink_dump_start(info->sk, skb, info->nlh, &c);
1761 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1763 return ctnetlink_dump_list(skb, cb, false);
1766 static int ctnetlink_get_ct_unconfirmed(struct sk_buff *skb,
1767 const struct nfnl_info *info,
1768 const struct nlattr * const cda[])
1770 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
1771 struct netlink_dump_control c = {
1772 .dump = ctnetlink_dump_unconfirmed,
1773 .done = ctnetlink_done_list,
1775 return netlink_dump_start(info->sk, skb, info->nlh, &c);
1781 #if IS_ENABLED(CONFIG_NF_NAT)
1783 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1784 enum nf_nat_manip_type manip,
1785 const struct nlattr *attr)
1788 struct nf_nat_hook *nat_hook;
1791 nat_hook = rcu_dereference(nf_nat_hook);
1793 #ifdef CONFIG_MODULES
1795 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1796 if (request_module("nf-nat") < 0) {
1797 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1801 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1803 nat_hook = rcu_dereference(nf_nat_hook);
1810 err = nat_hook->parse_nat_setup(ct, manip, attr);
1811 if (err == -EAGAIN) {
1812 #ifdef CONFIG_MODULES
1814 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1815 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1816 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1820 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1831 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1836 /* Ignore these unchangable bits */
1837 on &= ~IPS_UNCHANGEABLE_MASK;
1838 off &= ~IPS_UNCHANGEABLE_MASK;
1840 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1841 if (on & (1 << bit))
1842 set_bit(bit, &ct->status);
1843 else if (off & (1 << bit))
1844 clear_bit(bit, &ct->status);
1849 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1852 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1853 d = ct->status ^ status;
1855 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1859 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1860 /* SEEN_REPLY bit can only be set */
1863 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1864 /* ASSURED bit can only be set */
1867 __ctnetlink_change_status(ct, status, 0);
1872 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1874 #if IS_ENABLED(CONFIG_NF_NAT)
1877 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1880 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1885 return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1888 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1894 static int ctnetlink_change_helper(struct nf_conn *ct,
1895 const struct nlattr * const cda[])
1897 struct nf_conntrack_helper *helper;
1898 struct nf_conn_help *help = nfct_help(ct);
1899 char *helpname = NULL;
1900 struct nlattr *helpinfo = NULL;
1903 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1907 /* don't change helper of sibling connections */
1909 /* If we try to change the helper to the same thing twice,
1910 * treat the second attempt as a no-op instead of returning
1916 helper = rcu_dereference(help->helper);
1917 if (helper && !strcmp(helper->name, helpname))
1925 if (!strcmp(helpname, "")) {
1926 if (help && help->helper) {
1927 /* we had a helper before ... */
1928 nf_ct_remove_expectations(ct);
1929 RCU_INIT_POINTER(help->helper, NULL);
1936 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1937 nf_ct_protonum(ct));
1938 if (helper == NULL) {
1944 if (help->helper == helper) {
1945 /* update private helper data if allowed. */
1946 if (helper->from_nlattr)
1947 helper->from_nlattr(helpinfo, ct);
1952 /* we cannot set a helper for an existing conntrack */
1960 static int ctnetlink_change_timeout(struct nf_conn *ct,
1961 const struct nlattr * const cda[])
1963 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1965 if (timeout > INT_MAX)
1967 ct->timeout = nfct_time_stamp + (u32)timeout;
1969 if (test_bit(IPS_DYING_BIT, &ct->status))
1975 #if defined(CONFIG_NF_CONNTRACK_MARK)
1976 static void ctnetlink_change_mark(struct nf_conn *ct,
1977 const struct nlattr * const cda[])
1979 u32 mark, newmark, mask = 0;
1981 if (cda[CTA_MARK_MASK])
1982 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1984 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1985 newmark = (ct->mark & mask) ^ mark;
1986 if (newmark != ct->mark)
1991 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1992 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1993 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1994 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1997 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1998 const struct nlattr * const cda[])
2000 const struct nlattr *attr = cda[CTA_PROTOINFO];
2001 const struct nf_conntrack_l4proto *l4proto;
2002 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
2005 err = nla_parse_nested_deprecated(tb, CTA_PROTOINFO_MAX, attr,
2006 protoinfo_policy, NULL);
2010 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
2011 if (l4proto->from_nlattr)
2012 err = l4proto->from_nlattr(tb, ct);
2017 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
2018 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
2019 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
2020 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
2023 static int change_seq_adj(struct nf_ct_seqadj *seq,
2024 const struct nlattr * const attr)
2027 struct nlattr *cda[CTA_SEQADJ_MAX+1];
2029 err = nla_parse_nested_deprecated(cda, CTA_SEQADJ_MAX, attr,
2030 seqadj_policy, NULL);
2034 if (!cda[CTA_SEQADJ_CORRECTION_POS])
2037 seq->correction_pos =
2038 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
2040 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
2043 seq->offset_before =
2044 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
2046 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
2050 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
2056 ctnetlink_change_seq_adj(struct nf_conn *ct,
2057 const struct nlattr * const cda[])
2059 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
2065 spin_lock_bh(&ct->lock);
2066 if (cda[CTA_SEQ_ADJ_ORIG]) {
2067 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
2068 cda[CTA_SEQ_ADJ_ORIG]);
2072 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
2075 if (cda[CTA_SEQ_ADJ_REPLY]) {
2076 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
2077 cda[CTA_SEQ_ADJ_REPLY]);
2081 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
2084 spin_unlock_bh(&ct->lock);
2087 spin_unlock_bh(&ct->lock);
2091 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
2092 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 },
2093 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 },
2094 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 },
2097 static int ctnetlink_change_synproxy(struct nf_conn *ct,
2098 const struct nlattr * const cda[])
2100 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
2101 struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
2107 err = nla_parse_nested_deprecated(tb, CTA_SYNPROXY_MAX,
2108 cda[CTA_SYNPROXY], synproxy_policy,
2113 if (!tb[CTA_SYNPROXY_ISN] ||
2114 !tb[CTA_SYNPROXY_ITS] ||
2115 !tb[CTA_SYNPROXY_TSOFF])
2118 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
2119 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
2120 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
2126 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
2128 #ifdef CONFIG_NF_CONNTRACK_LABELS
2129 size_t len = nla_len(cda[CTA_LABELS]);
2130 const void *mask = cda[CTA_LABELS_MASK];
2132 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
2136 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
2137 nla_len(cda[CTA_LABELS_MASK]) != len)
2139 mask = nla_data(cda[CTA_LABELS_MASK]);
2144 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
2151 ctnetlink_change_conntrack(struct nf_conn *ct,
2152 const struct nlattr * const cda[])
2156 /* only allow NAT changes and master assignation for new conntracks */
2157 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
2160 if (cda[CTA_HELP]) {
2161 err = ctnetlink_change_helper(ct, cda);
2166 if (cda[CTA_TIMEOUT]) {
2167 err = ctnetlink_change_timeout(ct, cda);
2172 if (cda[CTA_STATUS]) {
2173 err = ctnetlink_change_status(ct, cda);
2178 if (cda[CTA_PROTOINFO]) {
2179 err = ctnetlink_change_protoinfo(ct, cda);
2184 #if defined(CONFIG_NF_CONNTRACK_MARK)
2186 ctnetlink_change_mark(ct, cda);
2189 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2190 err = ctnetlink_change_seq_adj(ct, cda);
2195 if (cda[CTA_SYNPROXY]) {
2196 err = ctnetlink_change_synproxy(ct, cda);
2201 if (cda[CTA_LABELS]) {
2202 err = ctnetlink_attach_labels(ct, cda);
2210 static struct nf_conn *
2211 ctnetlink_create_conntrack(struct net *net,
2212 const struct nf_conntrack_zone *zone,
2213 const struct nlattr * const cda[],
2214 struct nf_conntrack_tuple *otuple,
2215 struct nf_conntrack_tuple *rtuple,
2220 struct nf_conntrack_helper *helper;
2221 struct nf_conn_tstamp *tstamp;
2224 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
2226 return ERR_PTR(-ENOMEM);
2228 if (!cda[CTA_TIMEOUT])
2231 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
2232 if (timeout > INT_MAX)
2234 ct->timeout = (u32)timeout + nfct_time_stamp;
2237 if (cda[CTA_HELP]) {
2238 char *helpname = NULL;
2239 struct nlattr *helpinfo = NULL;
2241 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
2245 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2246 nf_ct_protonum(ct));
2247 if (helper == NULL) {
2249 #ifdef CONFIG_MODULES
2250 if (request_module("nfct-helper-%s", helpname) < 0) {
2256 helper = __nf_conntrack_helper_find(helpname,
2258 nf_ct_protonum(ct));
2268 struct nf_conn_help *help;
2270 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
2275 /* set private helper data if allowed. */
2276 if (helper->from_nlattr)
2277 helper->from_nlattr(helpinfo, ct);
2279 /* not in hash table yet so not strictly necessary */
2280 RCU_INIT_POINTER(help->helper, helper);
2283 /* try an implicit helper assignation */
2284 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
2289 err = ctnetlink_setup_nat(ct, cda);
2293 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
2294 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
2295 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
2296 nf_ct_labels_ext_add(ct);
2297 nfct_seqadj_ext_add(ct);
2298 nfct_synproxy_ext_add(ct);
2300 /* we must add conntrack extensions before confirmation. */
2301 ct->status |= IPS_CONFIRMED;
2303 if (cda[CTA_STATUS]) {
2304 err = ctnetlink_change_status(ct, cda);
2309 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2310 err = ctnetlink_change_seq_adj(ct, cda);
2315 memset(&ct->proto, 0, sizeof(ct->proto));
2316 if (cda[CTA_PROTOINFO]) {
2317 err = ctnetlink_change_protoinfo(ct, cda);
2322 if (cda[CTA_SYNPROXY]) {
2323 err = ctnetlink_change_synproxy(ct, cda);
2328 #if defined(CONFIG_NF_CONNTRACK_MARK)
2330 ctnetlink_change_mark(ct, cda);
2333 /* setup master conntrack: this is a confirmed expectation */
2334 if (cda[CTA_TUPLE_MASTER]) {
2335 struct nf_conntrack_tuple master;
2336 struct nf_conntrack_tuple_hash *master_h;
2337 struct nf_conn *master_ct;
2339 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2344 master_h = nf_conntrack_find_get(net, zone, &master);
2345 if (master_h == NULL) {
2349 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2350 __set_bit(IPS_EXPECTED_BIT, &ct->status);
2351 ct->master = master_ct;
2353 tstamp = nf_conn_tstamp_find(ct);
2355 tstamp->start = ktime_get_real_ns();
2357 err = nf_conntrack_hash_check_insert(ct);
2368 nf_conntrack_free(ct);
2369 return ERR_PTR(err);
2372 static int ctnetlink_new_conntrack(struct sk_buff *skb,
2373 const struct nfnl_info *info,
2374 const struct nlattr * const cda[])
2376 struct nfgenmsg *nfmsg = nlmsg_data(info->nlh);
2377 struct nf_conntrack_tuple otuple, rtuple;
2378 struct nf_conntrack_tuple_hash *h = NULL;
2379 u_int8_t u3 = nfmsg->nfgen_family;
2380 struct nf_conntrack_zone zone;
2384 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2388 if (cda[CTA_TUPLE_ORIG]) {
2389 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2395 if (cda[CTA_TUPLE_REPLY]) {
2396 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2402 if (cda[CTA_TUPLE_ORIG])
2403 h = nf_conntrack_find_get(info->net, &zone, &otuple);
2404 else if (cda[CTA_TUPLE_REPLY])
2405 h = nf_conntrack_find_get(info->net, &zone, &rtuple);
2409 if (info->nlh->nlmsg_flags & NLM_F_CREATE) {
2410 enum ip_conntrack_events events;
2412 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2414 if (otuple.dst.protonum != rtuple.dst.protonum)
2417 ct = ctnetlink_create_conntrack(info->net, &zone, cda,
2418 &otuple, &rtuple, u3);
2423 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2424 events = 1 << IPCT_RELATED;
2426 events = 1 << IPCT_NEW;
2428 if (cda[CTA_LABELS] &&
2429 ctnetlink_attach_labels(ct, cda) == 0)
2430 events |= (1 << IPCT_LABEL);
2432 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2433 (1 << IPCT_ASSURED) |
2434 (1 << IPCT_HELPER) |
2435 (1 << IPCT_PROTOINFO) |
2436 (1 << IPCT_SEQADJ) |
2438 (1 << IPCT_SYNPROXY) |
2440 ct, NETLINK_CB(skb).portid,
2441 nlmsg_report(info->nlh));
2447 /* implicit 'else' */
2450 ct = nf_ct_tuplehash_to_ctrack(h);
2451 if (!(info->nlh->nlmsg_flags & NLM_F_EXCL)) {
2452 err = ctnetlink_change_conntrack(ct, cda);
2454 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2455 (1 << IPCT_ASSURED) |
2456 (1 << IPCT_HELPER) |
2458 (1 << IPCT_PROTOINFO) |
2459 (1 << IPCT_SEQADJ) |
2461 (1 << IPCT_SYNPROXY),
2462 ct, NETLINK_CB(skb).portid,
2463 nlmsg_report(info->nlh));
2472 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2473 __u16 cpu, const struct ip_conntrack_stat *st)
2475 struct nlmsghdr *nlh;
2476 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2478 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2479 IPCTNL_MSG_CT_GET_STATS_CPU);
2480 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
2481 NFNETLINK_V0, htons(cpu));
2485 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2486 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2487 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2488 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2489 htonl(st->insert_failed)) ||
2490 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2491 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2492 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2493 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2494 htonl(st->search_restart)) ||
2495 nla_put_be32(skb, CTA_STATS_CLASH_RESOLVE,
2496 htonl(st->clash_resolve)))
2497 goto nla_put_failure;
2499 nlmsg_end(skb, nlh);
2504 nlmsg_cancel(skb, nlh);
2509 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2512 struct net *net = sock_net(skb->sk);
2514 if (cb->args[0] == nr_cpu_ids)
2517 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2518 const struct ip_conntrack_stat *st;
2520 if (!cpu_possible(cpu))
2523 st = per_cpu_ptr(net->ct.stat, cpu);
2524 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2525 NETLINK_CB(cb->skb).portid,
2535 static int ctnetlink_stat_ct_cpu(struct sk_buff *skb,
2536 const struct nfnl_info *info,
2537 const struct nlattr * const cda[])
2539 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
2540 struct netlink_dump_control c = {
2541 .dump = ctnetlink_ct_stat_cpu_dump,
2543 return netlink_dump_start(info->sk, skb, info->nlh, &c);
2550 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2553 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2554 unsigned int nr_conntracks;
2555 struct nlmsghdr *nlh;
2557 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2558 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
2563 nr_conntracks = nf_conntrack_count(net);
2564 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2565 goto nla_put_failure;
2567 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2568 goto nla_put_failure;
2570 nlmsg_end(skb, nlh);
2575 nlmsg_cancel(skb, nlh);
2579 static int ctnetlink_stat_ct(struct sk_buff *skb, const struct nfnl_info *info,
2580 const struct nlattr * const cda[])
2582 struct sk_buff *skb2;
2585 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2589 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2590 info->nlh->nlmsg_seq,
2591 NFNL_MSG_TYPE(info->nlh->nlmsg_type),
2596 err = netlink_unicast(info->sk, skb2, NETLINK_CB(skb).portid,
2606 /* this avoids a loop in nfnetlink. */
2607 return err == -EAGAIN ? -ENOBUFS : err;
2610 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2611 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2612 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2613 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2614 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2615 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2616 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2617 .len = NF_CT_HELPER_NAME_LEN - 1 },
2618 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2619 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2620 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2621 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2622 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2625 static struct nf_conntrack_expect *
2626 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2627 struct nf_conntrack_helper *helper,
2628 struct nf_conntrack_tuple *tuple,
2629 struct nf_conntrack_tuple *mask);
2631 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2633 ctnetlink_glue_build_size(const struct nf_conn *ct)
2635 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2636 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2637 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2638 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2639 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2640 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2641 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2642 + nla_total_size(0) /* CTA_PROTOINFO */
2643 + nla_total_size(0) /* CTA_HELP */
2644 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2645 + ctnetlink_secctx_size(ct)
2646 #if IS_ENABLED(CONFIG_NF_NAT)
2647 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2648 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2650 #ifdef CONFIG_NF_CONNTRACK_MARK
2651 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2653 #ifdef CONFIG_NF_CONNTRACK_ZONES
2654 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2656 + ctnetlink_proto_size(ct)
2660 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2662 const struct nf_conntrack_zone *zone;
2663 struct nlattr *nest_parms;
2665 zone = nf_ct_zone(ct);
2667 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG);
2669 goto nla_put_failure;
2670 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2671 goto nla_put_failure;
2672 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2673 NF_CT_ZONE_DIR_ORIG) < 0)
2674 goto nla_put_failure;
2675 nla_nest_end(skb, nest_parms);
2677 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY);
2679 goto nla_put_failure;
2680 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2681 goto nla_put_failure;
2682 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2683 NF_CT_ZONE_DIR_REPL) < 0)
2684 goto nla_put_failure;
2685 nla_nest_end(skb, nest_parms);
2687 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2688 NF_CT_DEFAULT_ZONE_DIR) < 0)
2689 goto nla_put_failure;
2691 if (ctnetlink_dump_id(skb, ct) < 0)
2692 goto nla_put_failure;
2694 if (ctnetlink_dump_status(skb, ct) < 0)
2695 goto nla_put_failure;
2697 if (ctnetlink_dump_timeout(skb, ct, false) < 0)
2698 goto nla_put_failure;
2700 if (ctnetlink_dump_protoinfo(skb, ct, false) < 0)
2701 goto nla_put_failure;
2703 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2704 goto nla_put_failure;
2706 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2707 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2708 goto nla_put_failure;
2710 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2711 goto nla_put_failure;
2713 if ((ct->status & IPS_SEQ_ADJUST) &&
2714 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2715 goto nla_put_failure;
2717 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2718 goto nla_put_failure;
2720 #ifdef CONFIG_NF_CONNTRACK_MARK
2721 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2722 goto nla_put_failure;
2724 if (ctnetlink_dump_labels(skb, ct) < 0)
2725 goto nla_put_failure;
2733 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2734 enum ip_conntrack_info ctinfo,
2735 u_int16_t ct_attr, u_int16_t ct_info_attr)
2737 struct nlattr *nest_parms;
2739 nest_parms = nla_nest_start(skb, ct_attr);
2741 goto nla_put_failure;
2743 if (__ctnetlink_glue_build(skb, ct) < 0)
2744 goto nla_put_failure;
2746 nla_nest_end(skb, nest_parms);
2748 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2749 goto nla_put_failure;
2758 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2760 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2761 unsigned long d = ct->status ^ status;
2763 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2764 /* SEEN_REPLY bit can only be set */
2767 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2768 /* ASSURED bit can only be set */
2771 /* This check is less strict than ctnetlink_change_status()
2772 * because callers often flip IPS_EXPECTED bits when sending
2773 * an NFQA_CT attribute to the kernel. So ignore the
2774 * unchangeable bits but do not error out. Also user programs
2775 * are allowed to clear the bits that they are allowed to change.
2777 __ctnetlink_change_status(ct, status, ~status);
2782 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2786 if (cda[CTA_TIMEOUT]) {
2787 err = ctnetlink_change_timeout(ct, cda);
2791 if (cda[CTA_STATUS]) {
2792 err = ctnetlink_update_status(ct, cda);
2796 if (cda[CTA_HELP]) {
2797 err = ctnetlink_change_helper(ct, cda);
2801 if (cda[CTA_LABELS]) {
2802 err = ctnetlink_attach_labels(ct, cda);
2806 #if defined(CONFIG_NF_CONNTRACK_MARK)
2807 if (cda[CTA_MARK]) {
2808 ctnetlink_change_mark(ct, cda);
2815 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2817 struct nlattr *cda[CTA_MAX+1];
2820 ret = nla_parse_nested_deprecated(cda, CTA_MAX, attr, ct_nla_policy,
2825 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2828 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2829 const struct nf_conn *ct,
2830 struct nf_conntrack_tuple *tuple,
2831 struct nf_conntrack_tuple *mask)
2835 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2836 nf_ct_l3num(ct), NULL);
2840 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2841 nf_ct_l3num(ct), NULL);
2845 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2846 u32 portid, u32 report)
2848 struct nlattr *cda[CTA_EXPECT_MAX+1];
2849 struct nf_conntrack_tuple tuple, mask;
2850 struct nf_conntrack_helper *helper = NULL;
2851 struct nf_conntrack_expect *exp;
2854 err = nla_parse_nested_deprecated(cda, CTA_EXPECT_MAX, attr,
2855 exp_nla_policy, NULL);
2859 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2864 if (cda[CTA_EXPECT_HELP_NAME]) {
2865 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2867 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2868 nf_ct_protonum(ct));
2873 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2874 helper, &tuple, &mask);
2876 return PTR_ERR(exp);
2878 err = nf_ct_expect_related_report(exp, portid, report, 0);
2879 nf_ct_expect_put(exp);
2883 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2884 enum ip_conntrack_info ctinfo, int diff)
2886 if (!(ct->status & IPS_NAT_MASK))
2889 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2892 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2893 .build_size = ctnetlink_glue_build_size,
2894 .build = ctnetlink_glue_build,
2895 .parse = ctnetlink_glue_parse,
2896 .attach_expect = ctnetlink_glue_attach_expect,
2897 .seq_adjust = ctnetlink_glue_seqadj,
2899 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2901 /***********************************************************************
2903 ***********************************************************************/
2905 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2906 const struct nf_conntrack_tuple *tuple,
2909 struct nlattr *nest_parms;
2911 nest_parms = nla_nest_start(skb, type);
2913 goto nla_put_failure;
2914 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2915 goto nla_put_failure;
2916 nla_nest_end(skb, nest_parms);
2924 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2925 const struct nf_conntrack_tuple *tuple,
2926 const struct nf_conntrack_tuple_mask *mask)
2928 const struct nf_conntrack_l4proto *l4proto;
2929 struct nf_conntrack_tuple m;
2930 struct nlattr *nest_parms;
2933 memset(&m, 0xFF, sizeof(m));
2934 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2935 m.src.u.all = mask->src.u.all;
2936 m.src.l3num = tuple->src.l3num;
2937 m.dst.protonum = tuple->dst.protonum;
2939 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK);
2941 goto nla_put_failure;
2944 ret = ctnetlink_dump_tuples_ip(skb, &m);
2946 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
2947 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2951 if (unlikely(ret < 0))
2952 goto nla_put_failure;
2954 nla_nest_end(skb, nest_parms);
2962 static const union nf_inet_addr any_addr;
2964 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2966 static __read_mostly siphash_key_t exp_id_seed;
2967 unsigned long a, b, c, d;
2969 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2971 a = (unsigned long)exp;
2972 b = (unsigned long)exp->helper;
2973 c = (unsigned long)exp->master;
2974 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2977 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2979 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2984 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2985 const struct nf_conntrack_expect *exp)
2987 struct nf_conn *master = exp->master;
2988 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2989 struct nf_conn_help *help;
2990 #if IS_ENABLED(CONFIG_NF_NAT)
2991 struct nlattr *nest_parms;
2992 struct nf_conntrack_tuple nat_tuple = {};
2994 struct nf_ct_helper_expectfn *expfn;
2999 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
3000 goto nla_put_failure;
3001 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
3002 goto nla_put_failure;
3003 if (ctnetlink_exp_dump_tuple(skb,
3004 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
3005 CTA_EXPECT_MASTER) < 0)
3006 goto nla_put_failure;
3008 #if IS_ENABLED(CONFIG_NF_NAT)
3009 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
3010 exp->saved_proto.all) {
3011 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT);
3013 goto nla_put_failure;
3015 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
3016 goto nla_put_failure;
3018 nat_tuple.src.l3num = nf_ct_l3num(master);
3019 nat_tuple.src.u3 = exp->saved_addr;
3020 nat_tuple.dst.protonum = nf_ct_protonum(master);
3021 nat_tuple.src.u = exp->saved_proto;
3023 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
3024 CTA_EXPECT_NAT_TUPLE) < 0)
3025 goto nla_put_failure;
3026 nla_nest_end(skb, nest_parms);
3029 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
3030 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
3031 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
3032 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
3033 goto nla_put_failure;
3034 help = nfct_help(master);
3036 struct nf_conntrack_helper *helper;
3038 helper = rcu_dereference(help->helper);
3040 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
3041 goto nla_put_failure;
3043 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
3044 if (expfn != NULL &&
3045 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
3046 goto nla_put_failure;
3055 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
3056 int event, const struct nf_conntrack_expect *exp)
3058 struct nlmsghdr *nlh;
3059 unsigned int flags = portid ? NLM_F_MULTI : 0;
3061 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
3062 nlh = nfnl_msg_put(skb, portid, seq, event, flags,
3063 exp->tuple.src.l3num, NFNETLINK_V0, 0);
3067 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
3068 goto nla_put_failure;
3070 nlmsg_end(skb, nlh);
3075 nlmsg_cancel(skb, nlh);
3079 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3081 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
3083 struct nf_conntrack_expect *exp = item->exp;
3084 struct net *net = nf_ct_exp_net(exp);
3085 struct nlmsghdr *nlh;
3086 struct sk_buff *skb;
3087 unsigned int type, group;
3090 if (events & (1 << IPEXP_DESTROY)) {
3091 type = IPCTNL_MSG_EXP_DELETE;
3092 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
3093 } else if (events & (1 << IPEXP_NEW)) {
3094 type = IPCTNL_MSG_EXP_NEW;
3095 flags = NLM_F_CREATE|NLM_F_EXCL;
3096 group = NFNLGRP_CONNTRACK_EXP_NEW;
3100 if (!item->report && !nfnetlink_has_listeners(net, group))
3103 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
3107 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
3108 nlh = nfnl_msg_put(skb, item->portid, 0, type, flags,
3109 exp->tuple.src.l3num, NFNETLINK_V0, 0);
3113 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
3114 goto nla_put_failure;
3116 nlmsg_end(skb, nlh);
3117 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
3121 nlmsg_cancel(skb, nlh);
3125 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
3129 static int ctnetlink_exp_done(struct netlink_callback *cb)
3132 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
3137 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
3139 struct net *net = sock_net(skb->sk);
3140 struct nf_conntrack_expect *exp, *last;
3141 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3142 u_int8_t l3proto = nfmsg->nfgen_family;
3145 last = (struct nf_conntrack_expect *)cb->args[1];
3146 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
3148 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
3150 if (l3proto && exp->tuple.src.l3num != l3proto)
3153 if (!net_eq(nf_ct_net(exp->master), net))
3161 if (ctnetlink_exp_fill_info(skb,
3162 NETLINK_CB(cb->skb).portid,
3166 if (!refcount_inc_not_zero(&exp->use))
3168 cb->args[1] = (unsigned long)exp;
3180 nf_ct_expect_put(last);
3186 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
3188 struct nf_conntrack_expect *exp, *last;
3189 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
3190 struct nf_conn *ct = cb->data;
3191 struct nf_conn_help *help = nfct_help(ct);
3192 u_int8_t l3proto = nfmsg->nfgen_family;
3198 last = (struct nf_conntrack_expect *)cb->args[1];
3200 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
3201 if (l3proto && exp->tuple.src.l3num != l3proto)
3208 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
3212 if (!refcount_inc_not_zero(&exp->use))
3214 cb->args[1] = (unsigned long)exp;
3226 nf_ct_expect_put(last);
3231 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
3232 struct sk_buff *skb,
3233 const struct nlmsghdr *nlh,
3234 const struct nlattr * const cda[],
3235 struct netlink_ext_ack *extack)
3238 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3239 u_int8_t u3 = nfmsg->nfgen_family;
3240 struct nf_conntrack_tuple tuple;
3241 struct nf_conntrack_tuple_hash *h;
3243 struct nf_conntrack_zone zone;
3244 struct netlink_dump_control c = {
3245 .dump = ctnetlink_exp_ct_dump_table,
3246 .done = ctnetlink_exp_done,
3249 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3254 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3258 h = nf_conntrack_find_get(net, &zone, &tuple);
3262 ct = nf_ct_tuplehash_to_ctrack(h);
3263 /* No expectation linked to this connection tracking. */
3264 if (!nfct_help(ct)) {
3271 err = netlink_dump_start(ctnl, skb, nlh, &c);
3277 static int ctnetlink_get_expect(struct sk_buff *skb,
3278 const struct nfnl_info *info,
3279 const struct nlattr * const cda[])
3281 struct nfgenmsg *nfmsg = nlmsg_data(info->nlh);
3282 u_int8_t u3 = nfmsg->nfgen_family;
3283 struct nf_conntrack_tuple tuple;
3284 struct nf_conntrack_expect *exp;
3285 struct nf_conntrack_zone zone;
3286 struct sk_buff *skb2;
3289 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3290 if (cda[CTA_EXPECT_MASTER])
3291 return ctnetlink_dump_exp_ct(info->net, info->sk, skb,
3295 struct netlink_dump_control c = {
3296 .dump = ctnetlink_exp_dump_table,
3297 .done = ctnetlink_exp_done,
3299 return netlink_dump_start(info->sk, skb, info->nlh, &c);
3303 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3307 if (cda[CTA_EXPECT_TUPLE])
3308 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3310 else if (cda[CTA_EXPECT_MASTER])
3311 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3319 exp = nf_ct_expect_find_get(info->net, &zone, &tuple);
3323 if (cda[CTA_EXPECT_ID]) {
3324 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3326 if (id != nf_expect_get_id(exp)) {
3327 nf_ct_expect_put(exp);
3333 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3335 nf_ct_expect_put(exp);
3340 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3341 info->nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
3344 nf_ct_expect_put(exp);
3348 err = netlink_unicast(info->sk, skb2, NETLINK_CB(skb).portid,
3358 /* this avoids a loop in nfnetlink. */
3359 return err == -EAGAIN ? -ENOBUFS : err;
3362 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3364 const struct nf_conn_help *m_help;
3365 const char *name = data;
3367 m_help = nfct_help(exp->master);
3369 return strcmp(m_help->helper->name, name) == 0;
3372 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3377 static int ctnetlink_del_expect(struct sk_buff *skb,
3378 const struct nfnl_info *info,
3379 const struct nlattr * const cda[])
3381 struct nfgenmsg *nfmsg = nlmsg_data(info->nlh);
3382 u_int8_t u3 = nfmsg->nfgen_family;
3383 struct nf_conntrack_expect *exp;
3384 struct nf_conntrack_tuple tuple;
3385 struct nf_conntrack_zone zone;
3388 if (cda[CTA_EXPECT_TUPLE]) {
3389 /* delete a single expect by tuple */
3390 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3394 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3399 /* bump usage count to 2 */
3400 exp = nf_ct_expect_find_get(info->net, &zone, &tuple);
3404 if (cda[CTA_EXPECT_ID]) {
3405 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3406 if (ntohl(id) != (u32)(unsigned long)exp) {
3407 nf_ct_expect_put(exp);
3412 /* after list removal, usage count == 1 */
3413 spin_lock_bh(&nf_conntrack_expect_lock);
3414 if (del_timer(&exp->timeout)) {
3415 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3416 nlmsg_report(info->nlh));
3417 nf_ct_expect_put(exp);
3419 spin_unlock_bh(&nf_conntrack_expect_lock);
3420 /* have to put what we 'get' above.
3421 * after this line usage count == 0 */
3422 nf_ct_expect_put(exp);
3423 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3424 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3426 nf_ct_expect_iterate_net(info->net, expect_iter_name, name,
3427 NETLINK_CB(skb).portid,
3428 nlmsg_report(info->nlh));
3430 /* This basically means we have to flush everything*/
3431 nf_ct_expect_iterate_net(info->net, expect_iter_all, NULL,
3432 NETLINK_CB(skb).portid,
3433 nlmsg_report(info->nlh));
3439 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3440 const struct nlattr * const cda[])
3442 if (cda[CTA_EXPECT_TIMEOUT]) {
3443 if (!del_timer(&x->timeout))
3446 x->timeout.expires = jiffies +
3447 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3448 add_timer(&x->timeout);
3453 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3454 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3455 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3459 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3460 struct nf_conntrack_expect *exp,
3463 #if IS_ENABLED(CONFIG_NF_NAT)
3464 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3465 struct nf_conntrack_tuple nat_tuple = {};
3468 err = nla_parse_nested_deprecated(tb, CTA_EXPECT_NAT_MAX, attr,
3469 exp_nat_nla_policy, NULL);
3473 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3476 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3477 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3482 exp->saved_addr = nat_tuple.src.u3;
3483 exp->saved_proto = nat_tuple.src.u;
3484 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3492 static struct nf_conntrack_expect *
3493 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3494 struct nf_conntrack_helper *helper,
3495 struct nf_conntrack_tuple *tuple,
3496 struct nf_conntrack_tuple *mask)
3498 u_int32_t class = 0;
3499 struct nf_conntrack_expect *exp;
3500 struct nf_conn_help *help;
3503 help = nfct_help(ct);
3505 return ERR_PTR(-EOPNOTSUPP);
3507 if (cda[CTA_EXPECT_CLASS] && helper) {
3508 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3509 if (class > helper->expect_class_max)
3510 return ERR_PTR(-EINVAL);
3512 exp = nf_ct_expect_alloc(ct);
3514 return ERR_PTR(-ENOMEM);
3516 if (cda[CTA_EXPECT_FLAGS]) {
3517 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3518 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3522 if (cda[CTA_EXPECT_FN]) {
3523 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3524 struct nf_ct_helper_expectfn *expfn;
3526 expfn = nf_ct_helper_expectfn_find_by_name(name);
3527 if (expfn == NULL) {
3531 exp->expectfn = expfn->expectfn;
3533 exp->expectfn = NULL;
3537 exp->helper = helper;
3538 exp->tuple = *tuple;
3539 exp->mask.src.u3 = mask->src.u3;
3540 exp->mask.src.u.all = mask->src.u.all;
3542 if (cda[CTA_EXPECT_NAT]) {
3543 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3544 exp, nf_ct_l3num(ct));
3550 nf_ct_expect_put(exp);
3551 return ERR_PTR(err);
3555 ctnetlink_create_expect(struct net *net,
3556 const struct nf_conntrack_zone *zone,
3557 const struct nlattr * const cda[],
3558 u_int8_t u3, u32 portid, int report)
3560 struct nf_conntrack_tuple tuple, mask, master_tuple;
3561 struct nf_conntrack_tuple_hash *h = NULL;
3562 struct nf_conntrack_helper *helper = NULL;
3563 struct nf_conntrack_expect *exp;
3567 /* caller guarantees that those three CTA_EXPECT_* exist */
3568 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3572 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3576 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3581 /* Look for master conntrack of this expectation */
3582 h = nf_conntrack_find_get(net, zone, &master_tuple);
3585 ct = nf_ct_tuplehash_to_ctrack(h);
3588 if (cda[CTA_EXPECT_HELP_NAME]) {
3589 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3591 helper = __nf_conntrack_helper_find(helpname, u3,
3592 nf_ct_protonum(ct));
3593 if (helper == NULL) {
3595 #ifdef CONFIG_MODULES
3596 if (request_module("nfct-helper-%s", helpname) < 0) {
3601 helper = __nf_conntrack_helper_find(helpname, u3,
3602 nf_ct_protonum(ct));
3614 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3620 err = nf_ct_expect_related_report(exp, portid, report, 0);
3621 nf_ct_expect_put(exp);
3629 static int ctnetlink_new_expect(struct sk_buff *skb,
3630 const struct nfnl_info *info,
3631 const struct nlattr * const cda[])
3633 struct nfgenmsg *nfmsg = nlmsg_data(info->nlh);
3634 u_int8_t u3 = nfmsg->nfgen_family;
3635 struct nf_conntrack_tuple tuple;
3636 struct nf_conntrack_expect *exp;
3637 struct nf_conntrack_zone zone;
3640 if (!cda[CTA_EXPECT_TUPLE]
3641 || !cda[CTA_EXPECT_MASK]
3642 || !cda[CTA_EXPECT_MASTER])
3645 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3649 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3654 spin_lock_bh(&nf_conntrack_expect_lock);
3655 exp = __nf_ct_expect_find(info->net, &zone, &tuple);
3657 spin_unlock_bh(&nf_conntrack_expect_lock);
3659 if (info->nlh->nlmsg_flags & NLM_F_CREATE) {
3660 err = ctnetlink_create_expect(info->net, &zone, cda, u3,
3661 NETLINK_CB(skb).portid,
3662 nlmsg_report(info->nlh));
3668 if (!(info->nlh->nlmsg_flags & NLM_F_EXCL))
3669 err = ctnetlink_change_expect(exp, cda);
3670 spin_unlock_bh(&nf_conntrack_expect_lock);
3676 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3677 const struct ip_conntrack_stat *st)
3679 struct nlmsghdr *nlh;
3680 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3682 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3683 IPCTNL_MSG_EXP_GET_STATS_CPU);
3684 nlh = nfnl_msg_put(skb, portid, seq, event, flags, AF_UNSPEC,
3685 NFNETLINK_V0, htons(cpu));
3689 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3690 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3691 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3692 goto nla_put_failure;
3694 nlmsg_end(skb, nlh);
3699 nlmsg_cancel(skb, nlh);
3704 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3707 struct net *net = sock_net(skb->sk);
3709 if (cb->args[0] == nr_cpu_ids)
3712 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3713 const struct ip_conntrack_stat *st;
3715 if (!cpu_possible(cpu))
3718 st = per_cpu_ptr(net->ct.stat, cpu);
3719 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3729 static int ctnetlink_stat_exp_cpu(struct sk_buff *skb,
3730 const struct nfnl_info *info,
3731 const struct nlattr * const cda[])
3733 if (info->nlh->nlmsg_flags & NLM_F_DUMP) {
3734 struct netlink_dump_control c = {
3735 .dump = ctnetlink_exp_stat_cpu_dump,
3737 return netlink_dump_start(info->sk, skb, info->nlh, &c);
3743 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3744 static struct nf_ct_event_notifier ctnl_notifier = {
3745 .fcn = ctnetlink_conntrack_event,
3748 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3749 .fcn = ctnetlink_expect_event,
3753 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3754 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3755 .attr_count = CTA_MAX,
3756 .policy = ct_nla_policy },
3757 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3758 .attr_count = CTA_MAX,
3759 .policy = ct_nla_policy },
3760 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3761 .attr_count = CTA_MAX,
3762 .policy = ct_nla_policy },
3763 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3764 .attr_count = CTA_MAX,
3765 .policy = ct_nla_policy },
3766 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3767 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3768 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3769 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3772 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3773 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3774 .attr_count = CTA_EXPECT_MAX,
3775 .policy = exp_nla_policy },
3776 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3777 .attr_count = CTA_EXPECT_MAX,
3778 .policy = exp_nla_policy },
3779 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3780 .attr_count = CTA_EXPECT_MAX,
3781 .policy = exp_nla_policy },
3782 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3785 static const struct nfnetlink_subsystem ctnl_subsys = {
3786 .name = "conntrack",
3787 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3788 .cb_count = IPCTNL_MSG_MAX,
3792 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3793 .name = "conntrack_expect",
3794 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3795 .cb_count = IPCTNL_MSG_EXP_MAX,
3799 MODULE_ALIAS("ip_conntrack_netlink");
3800 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3801 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3803 static int __net_init ctnetlink_net_init(struct net *net)
3805 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3808 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3810 pr_err("ctnetlink_init: cannot register notifier.\n");
3814 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3816 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3817 goto err_unreg_notifier;
3822 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3824 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3830 static void ctnetlink_net_exit(struct net *net)
3832 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3833 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3834 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3838 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3842 list_for_each_entry(net, net_exit_list, exit_list)
3843 ctnetlink_net_exit(net);
3845 /* wait for other cpus until they are done with ctnl_notifiers */
3849 static struct pernet_operations ctnetlink_net_ops = {
3850 .init = ctnetlink_net_init,
3851 .exit_batch = ctnetlink_net_exit_batch,
3854 static int __init ctnetlink_init(void)
3858 ret = nfnetlink_subsys_register(&ctnl_subsys);
3860 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3864 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3866 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3867 goto err_unreg_subsys;
3870 ret = register_pernet_subsys(&ctnetlink_net_ops);
3872 pr_err("ctnetlink_init: cannot register pernet operations\n");
3873 goto err_unreg_exp_subsys;
3875 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3876 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3877 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3881 err_unreg_exp_subsys:
3882 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3884 nfnetlink_subsys_unregister(&ctnl_subsys);
3889 static void __exit ctnetlink_exit(void)
3891 unregister_pernet_subsys(&ctnetlink_net_ops);
3892 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3893 nfnetlink_subsys_unregister(&ctnl_subsys);
3894 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3895 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3900 module_init(ctnetlink_init);
3901 module_exit(ctnetlink_exit);