1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
32 #include <linux/siphash.h>
34 #include <linux/netfilter.h>
35 #include <net/netlink.h>
37 #include <net/netfilter/nf_conntrack.h>
38 #include <net/netfilter/nf_conntrack_core.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_seqadj.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #include <net/netfilter/nf_conntrack_acct.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_labels.h>
48 #include <net/netfilter/nf_conntrack_synproxy.h>
49 #if IS_ENABLED(CONFIG_NF_NAT)
50 #include <net/netfilter/nf_nat.h>
51 #include <net/netfilter/nf_nat_helper.h>
54 #include <linux/netfilter/nfnetlink.h>
55 #include <linux/netfilter/nfnetlink_conntrack.h>
57 MODULE_LICENSE("GPL");
59 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
60 const struct nf_conntrack_tuple *tuple,
61 const struct nf_conntrack_l4proto *l4proto)
64 struct nlattr *nest_parms;
66 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
69 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
72 if (likely(l4proto->tuple_to_nlattr))
73 ret = l4proto->tuple_to_nlattr(skb, tuple);
75 nla_nest_end(skb, nest_parms);
83 static int ipv4_tuple_to_nlattr(struct sk_buff *skb,
84 const struct nf_conntrack_tuple *tuple)
86 if (nla_put_in_addr(skb, CTA_IP_V4_SRC, tuple->src.u3.ip) ||
87 nla_put_in_addr(skb, CTA_IP_V4_DST, tuple->dst.u3.ip))
92 static int ipv6_tuple_to_nlattr(struct sk_buff *skb,
93 const struct nf_conntrack_tuple *tuple)
95 if (nla_put_in6_addr(skb, CTA_IP_V6_SRC, &tuple->src.u3.in6) ||
96 nla_put_in6_addr(skb, CTA_IP_V6_DST, &tuple->dst.u3.in6))
101 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
102 const struct nf_conntrack_tuple *tuple)
105 struct nlattr *nest_parms;
107 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
109 goto nla_put_failure;
111 switch (tuple->src.l3num) {
113 ret = ipv4_tuple_to_nlattr(skb, tuple);
116 ret = ipv6_tuple_to_nlattr(skb, tuple);
120 nla_nest_end(skb, nest_parms);
128 static int ctnetlink_dump_tuples(struct sk_buff *skb,
129 const struct nf_conntrack_tuple *tuple)
131 const struct nf_conntrack_l4proto *l4proto;
135 ret = ctnetlink_dump_tuples_ip(skb, tuple);
138 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
139 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
145 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
146 const struct nf_conntrack_zone *zone, int dir)
148 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
150 if (nla_put_be16(skb, attrtype, htons(zone->id)))
151 goto nla_put_failure;
158 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
160 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
161 goto nla_put_failure;
168 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
170 long timeout = nf_ct_expires(ct) / HZ;
172 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
173 goto nla_put_failure;
180 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
182 const struct nf_conntrack_l4proto *l4proto;
183 struct nlattr *nest_proto;
186 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
187 if (!l4proto->to_nlattr)
190 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
192 goto nla_put_failure;
194 ret = l4proto->to_nlattr(skb, nest_proto, ct);
196 nla_nest_end(skb, nest_proto);
204 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
205 const struct nf_conn *ct)
207 struct nlattr *nest_helper;
208 const struct nf_conn_help *help = nfct_help(ct);
209 struct nf_conntrack_helper *helper;
214 helper = rcu_dereference(help->helper);
218 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
220 goto nla_put_failure;
221 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
222 goto nla_put_failure;
224 if (helper->to_nlattr)
225 helper->to_nlattr(skb, ct);
227 nla_nest_end(skb, nest_helper);
236 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
237 enum ip_conntrack_dir dir, int type)
239 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
240 struct nf_conn_counter *counter = acct->counter;
241 struct nlattr *nest_count;
244 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
245 pkts = atomic64_xchg(&counter[dir].packets, 0);
246 bytes = atomic64_xchg(&counter[dir].bytes, 0);
248 pkts = atomic64_read(&counter[dir].packets);
249 bytes = atomic64_read(&counter[dir].bytes);
252 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
254 goto nla_put_failure;
256 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
258 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
260 goto nla_put_failure;
262 nla_nest_end(skb, nest_count);
271 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
273 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
278 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
280 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
287 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
289 struct nlattr *nest_count;
290 const struct nf_conn_tstamp *tstamp;
292 tstamp = nf_conn_tstamp_find(ct);
296 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
298 goto nla_put_failure;
300 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
301 CTA_TIMESTAMP_PAD) ||
302 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
303 cpu_to_be64(tstamp->stop),
305 goto nla_put_failure;
306 nla_nest_end(skb, nest_count);
314 #ifdef CONFIG_NF_CONNTRACK_MARK
315 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
317 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
318 goto nla_put_failure;
325 #define ctnetlink_dump_mark(a, b) (0)
328 #ifdef CONFIG_NF_CONNTRACK_SECMARK
329 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
331 struct nlattr *nest_secctx;
335 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
340 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
342 goto nla_put_failure;
344 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
345 goto nla_put_failure;
346 nla_nest_end(skb, nest_secctx);
350 security_release_secctx(secctx, len);
354 #define ctnetlink_dump_secctx(a, b) (0)
357 #ifdef CONFIG_NF_CONNTRACK_LABELS
358 static inline int ctnetlink_label_size(const struct nf_conn *ct)
360 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
364 return nla_total_size(sizeof(labels->bits));
368 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
370 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
378 if (labels->bits[i] != 0)
379 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
382 } while (i < ARRAY_SIZE(labels->bits));
387 #define ctnetlink_dump_labels(a, b) (0)
388 #define ctnetlink_label_size(a) (0)
391 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
393 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
395 struct nlattr *nest_parms;
397 if (!(ct->status & IPS_EXPECTED))
400 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
402 goto nla_put_failure;
403 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
404 goto nla_put_failure;
405 nla_nest_end(skb, nest_parms);
414 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
416 struct nlattr *nest_parms;
418 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
420 goto nla_put_failure;
422 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
423 htonl(seq->correction_pos)) ||
424 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
425 htonl(seq->offset_before)) ||
426 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
427 htonl(seq->offset_after)))
428 goto nla_put_failure;
430 nla_nest_end(skb, nest_parms);
438 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
440 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
441 struct nf_ct_seqadj *seq;
443 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
446 spin_lock_bh(&ct->lock);
447 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
448 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
451 seq = &seqadj->seq[IP_CT_DIR_REPLY];
452 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
455 spin_unlock_bh(&ct->lock);
458 spin_unlock_bh(&ct->lock);
462 static int ctnetlink_dump_ct_synproxy(struct sk_buff *skb, struct nf_conn *ct)
464 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
465 struct nlattr *nest_parms;
470 nest_parms = nla_nest_start(skb, CTA_SYNPROXY | NLA_F_NESTED);
472 goto nla_put_failure;
474 if (nla_put_be32(skb, CTA_SYNPROXY_ISN, htonl(synproxy->isn)) ||
475 nla_put_be32(skb, CTA_SYNPROXY_ITS, htonl(synproxy->its)) ||
476 nla_put_be32(skb, CTA_SYNPROXY_TSOFF, htonl(synproxy->tsoff)))
477 goto nla_put_failure;
479 nla_nest_end(skb, nest_parms);
487 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
489 __be32 id = (__force __be32)nf_ct_get_id(ct);
491 if (nla_put_be32(skb, CTA_ID, id))
492 goto nla_put_failure;
499 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
501 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
502 goto nla_put_failure;
510 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
513 const struct nf_conntrack_zone *zone;
514 struct nlmsghdr *nlh;
515 struct nfgenmsg *nfmsg;
516 struct nlattr *nest_parms;
517 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
519 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
520 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
524 nfmsg = nlmsg_data(nlh);
525 nfmsg->nfgen_family = nf_ct_l3num(ct);
526 nfmsg->version = NFNETLINK_V0;
529 zone = nf_ct_zone(ct);
531 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
533 goto nla_put_failure;
534 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
535 goto nla_put_failure;
536 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
537 NF_CT_ZONE_DIR_ORIG) < 0)
538 goto nla_put_failure;
539 nla_nest_end(skb, nest_parms);
541 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
543 goto nla_put_failure;
544 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
545 goto nla_put_failure;
546 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
547 NF_CT_ZONE_DIR_REPL) < 0)
548 goto nla_put_failure;
549 nla_nest_end(skb, nest_parms);
551 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
552 NF_CT_DEFAULT_ZONE_DIR) < 0)
553 goto nla_put_failure;
555 if (ctnetlink_dump_status(skb, ct) < 0 ||
556 ctnetlink_dump_timeout(skb, ct) < 0 ||
557 ctnetlink_dump_acct(skb, ct, type) < 0 ||
558 ctnetlink_dump_timestamp(skb, ct) < 0 ||
559 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
560 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
561 ctnetlink_dump_mark(skb, ct) < 0 ||
562 ctnetlink_dump_secctx(skb, ct) < 0 ||
563 ctnetlink_dump_labels(skb, ct) < 0 ||
564 ctnetlink_dump_id(skb, ct) < 0 ||
565 ctnetlink_dump_use(skb, ct) < 0 ||
566 ctnetlink_dump_master(skb, ct) < 0 ||
567 ctnetlink_dump_ct_seq_adj(skb, ct) < 0 ||
568 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
569 goto nla_put_failure;
576 nlmsg_cancel(skb, nlh);
580 static const struct nla_policy cta_ip_nla_policy[CTA_IP_MAX + 1] = {
581 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
582 [CTA_IP_V4_DST] = { .type = NLA_U32 },
583 [CTA_IP_V6_SRC] = { .len = sizeof(__be32) * 4 },
584 [CTA_IP_V6_DST] = { .len = sizeof(__be32) * 4 },
587 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
588 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
590 const struct nf_conntrack_l4proto *l4proto;
591 size_t len, len4 = 0;
593 len = nla_policy_len(cta_ip_nla_policy, CTA_IP_MAX + 1);
594 len *= 3u; /* ORIG, REPLY, MASTER */
596 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
597 len += l4proto->nlattr_size;
598 if (l4proto->nlattr_tuple_size) {
599 len4 = l4proto->nlattr_tuple_size();
600 len4 *= 3u; /* ORIG, REPLY, MASTER */
607 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
609 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
611 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
612 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
613 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
617 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
619 #ifdef CONFIG_NF_CONNTRACK_SECMARK
622 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
626 return nla_total_size(0) /* CTA_SECCTX */
627 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
633 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
635 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
636 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
638 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
644 #ifdef CONFIG_NF_CONNTRACK_EVENTS
645 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
647 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
648 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
649 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
650 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
651 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
652 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
653 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
654 + ctnetlink_acct_size(ct)
655 + ctnetlink_timestamp_size(ct)
656 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
657 + nla_total_size(0) /* CTA_PROTOINFO */
658 + nla_total_size(0) /* CTA_HELP */
659 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
660 + ctnetlink_secctx_size(ct)
661 #if IS_ENABLED(CONFIG_NF_NAT)
662 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
663 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
665 #ifdef CONFIG_NF_CONNTRACK_MARK
666 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
668 #ifdef CONFIG_NF_CONNTRACK_ZONES
669 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
671 + ctnetlink_proto_size(ct)
672 + ctnetlink_label_size(ct)
677 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
679 const struct nf_conntrack_zone *zone;
681 struct nlmsghdr *nlh;
682 struct nfgenmsg *nfmsg;
683 struct nlattr *nest_parms;
684 struct nf_conn *ct = item->ct;
687 unsigned int flags = 0, group;
690 if (events & (1 << IPCT_DESTROY)) {
691 type = IPCTNL_MSG_CT_DELETE;
692 group = NFNLGRP_CONNTRACK_DESTROY;
693 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
694 type = IPCTNL_MSG_CT_NEW;
695 flags = NLM_F_CREATE|NLM_F_EXCL;
696 group = NFNLGRP_CONNTRACK_NEW;
698 type = IPCTNL_MSG_CT_NEW;
699 group = NFNLGRP_CONNTRACK_UPDATE;
704 if (!item->report && !nfnetlink_has_listeners(net, group))
707 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
711 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
712 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
716 nfmsg = nlmsg_data(nlh);
717 nfmsg->nfgen_family = nf_ct_l3num(ct);
718 nfmsg->version = NFNETLINK_V0;
721 zone = nf_ct_zone(ct);
723 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
725 goto nla_put_failure;
726 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
727 goto nla_put_failure;
728 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
729 NF_CT_ZONE_DIR_ORIG) < 0)
730 goto nla_put_failure;
731 nla_nest_end(skb, nest_parms);
733 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
735 goto nla_put_failure;
736 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
737 goto nla_put_failure;
738 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
739 NF_CT_ZONE_DIR_REPL) < 0)
740 goto nla_put_failure;
741 nla_nest_end(skb, nest_parms);
743 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
744 NF_CT_DEFAULT_ZONE_DIR) < 0)
745 goto nla_put_failure;
747 if (ctnetlink_dump_id(skb, ct) < 0)
748 goto nla_put_failure;
750 if (ctnetlink_dump_status(skb, ct) < 0)
751 goto nla_put_failure;
753 if (events & (1 << IPCT_DESTROY)) {
754 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
755 ctnetlink_dump_timestamp(skb, ct) < 0)
756 goto nla_put_failure;
758 if (ctnetlink_dump_timeout(skb, ct) < 0)
759 goto nla_put_failure;
761 if (events & (1 << IPCT_PROTOINFO)
762 && ctnetlink_dump_protoinfo(skb, ct) < 0)
763 goto nla_put_failure;
765 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
766 && ctnetlink_dump_helpinfo(skb, ct) < 0)
767 goto nla_put_failure;
769 #ifdef CONFIG_NF_CONNTRACK_SECMARK
770 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
771 && ctnetlink_dump_secctx(skb, ct) < 0)
772 goto nla_put_failure;
774 if (events & (1 << IPCT_LABEL) &&
775 ctnetlink_dump_labels(skb, ct) < 0)
776 goto nla_put_failure;
778 if (events & (1 << IPCT_RELATED) &&
779 ctnetlink_dump_master(skb, ct) < 0)
780 goto nla_put_failure;
782 if (events & (1 << IPCT_SEQADJ) &&
783 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
784 goto nla_put_failure;
786 if (events & (1 << IPCT_SYNPROXY) &&
787 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
788 goto nla_put_failure;
791 #ifdef CONFIG_NF_CONNTRACK_MARK
792 if ((events & (1 << IPCT_MARK) || ct->mark)
793 && ctnetlink_dump_mark(skb, ct) < 0)
794 goto nla_put_failure;
797 err = nfnetlink_send(skb, net, item->portid, group, item->report,
799 if (err == -ENOBUFS || err == -EAGAIN)
805 nlmsg_cancel(skb, nlh);
809 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
814 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
816 static int ctnetlink_done(struct netlink_callback *cb)
819 nf_ct_put((struct nf_conn *)cb->args[1]);
824 struct ctnetlink_filter {
832 static struct ctnetlink_filter *
833 ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
835 struct ctnetlink_filter *filter;
837 #ifndef CONFIG_NF_CONNTRACK_MARK
838 if (cda[CTA_MARK] && cda[CTA_MARK_MASK])
839 return ERR_PTR(-EOPNOTSUPP);
842 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
844 return ERR_PTR(-ENOMEM);
846 filter->family = family;
848 #ifdef CONFIG_NF_CONNTRACK_MARK
849 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
850 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
851 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
857 static int ctnetlink_start(struct netlink_callback *cb)
859 const struct nlattr * const *cda = cb->data;
860 struct ctnetlink_filter *filter = NULL;
861 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
862 u8 family = nfmsg->nfgen_family;
864 if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
865 filter = ctnetlink_alloc_filter(cda, family);
867 return PTR_ERR(filter);
874 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
876 struct ctnetlink_filter *filter = data;
881 /* Match entries of a given L3 protocol number.
882 * If it is not specified, ie. l3proto == 0,
883 * then match everything.
885 if (filter->family && nf_ct_l3num(ct) != filter->family)
888 #ifdef CONFIG_NF_CONNTRACK_MARK
889 if ((ct->mark & filter->mark.mask) != filter->mark.val)
901 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
903 struct net *net = sock_net(skb->sk);
904 struct nf_conn *ct, *last;
905 struct nf_conntrack_tuple_hash *h;
906 struct hlist_nulls_node *n;
907 struct nf_conn *nf_ct_evict[8];
911 last = (struct nf_conn *)cb->args[1];
915 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
919 if (nf_ct_should_gc(nf_ct_evict[i]))
920 nf_ct_kill(nf_ct_evict[i]);
921 nf_ct_put(nf_ct_evict[i]);
924 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
925 nf_conntrack_lock(lockp);
926 if (cb->args[0] >= nf_conntrack_htable_size) {
930 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
932 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
934 ct = nf_ct_tuplehash_to_ctrack(h);
935 if (nf_ct_is_expired(ct)) {
936 if (i < ARRAY_SIZE(nf_ct_evict) &&
937 atomic_inc_not_zero(&ct->ct_general.use))
938 nf_ct_evict[i++] = ct;
942 if (!net_eq(net, nf_ct_net(ct)))
950 if (!ctnetlink_filter_match(ct, cb->data))
955 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
957 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
961 nf_conntrack_get(&ct->ct_general);
962 cb->args[1] = (unsigned long)ct;
976 /* nf ct hash resize happened, now clear the leftover. */
977 if ((struct nf_conn *)cb->args[1] == last)
985 if (nf_ct_should_gc(nf_ct_evict[i]))
986 nf_ct_kill(nf_ct_evict[i]);
987 nf_ct_put(nf_ct_evict[i]);
993 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
994 struct nf_conntrack_tuple *t)
996 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
999 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
1000 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
1005 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
1006 struct nf_conntrack_tuple *t)
1008 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
1011 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
1012 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
1017 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
1018 struct nf_conntrack_tuple *tuple)
1020 struct nlattr *tb[CTA_IP_MAX+1];
1023 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
1027 ret = nla_validate_nested(attr, CTA_IP_MAX,
1028 cta_ip_nla_policy, NULL);
1032 switch (tuple->src.l3num) {
1034 ret = ipv4_nlattr_to_tuple(tb, tuple);
1037 ret = ipv6_nlattr_to_tuple(tb, tuple);
1044 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
1045 [CTA_PROTO_NUM] = { .type = NLA_U8 },
1048 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
1049 struct nf_conntrack_tuple *tuple)
1051 const struct nf_conntrack_l4proto *l4proto;
1052 struct nlattr *tb[CTA_PROTO_MAX+1];
1055 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
1060 if (!tb[CTA_PROTO_NUM])
1062 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
1065 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
1067 if (likely(l4proto->nlattr_to_tuple)) {
1068 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
1069 l4proto->nla_policy, NULL);
1071 ret = l4proto->nlattr_to_tuple(tb, tuple);
1080 ctnetlink_parse_zone(const struct nlattr *attr,
1081 struct nf_conntrack_zone *zone)
1083 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
1084 NF_CT_DEFAULT_ZONE_DIR, 0);
1085 #ifdef CONFIG_NF_CONNTRACK_ZONES
1087 zone->id = ntohs(nla_get_be16(attr));
1096 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1097 struct nf_conntrack_zone *zone)
1101 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1104 ret = ctnetlink_parse_zone(attr, zone);
1108 if (type == CTA_TUPLE_REPLY)
1109 zone->dir = NF_CT_ZONE_DIR_REPL;
1111 zone->dir = NF_CT_ZONE_DIR_ORIG;
1116 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1117 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1118 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1119 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1123 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1124 struct nf_conntrack_tuple *tuple, u32 type,
1125 u_int8_t l3num, struct nf_conntrack_zone *zone)
1127 struct nlattr *tb[CTA_TUPLE_MAX+1];
1130 memset(tuple, 0, sizeof(*tuple));
1132 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1137 if (!tb[CTA_TUPLE_IP])
1140 tuple->src.l3num = l3num;
1142 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1146 if (!tb[CTA_TUPLE_PROTO])
1149 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1153 if (tb[CTA_TUPLE_ZONE]) {
1157 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1163 /* orig and expect tuples get DIR_ORIGINAL */
1164 if (type == CTA_TUPLE_REPLY)
1165 tuple->dst.dir = IP_CT_DIR_REPLY;
1167 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1172 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1173 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1174 .len = NF_CT_HELPER_NAME_LEN - 1 },
1177 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1178 struct nlattr **helpinfo)
1181 struct nlattr *tb[CTA_HELP_MAX+1];
1183 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1187 if (!tb[CTA_HELP_NAME])
1190 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1192 if (tb[CTA_HELP_INFO])
1193 *helpinfo = tb[CTA_HELP_INFO];
1198 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1199 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1200 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1201 [CTA_STATUS] = { .type = NLA_U32 },
1202 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1203 [CTA_HELP] = { .type = NLA_NESTED },
1204 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1205 [CTA_TIMEOUT] = { .type = NLA_U32 },
1206 [CTA_MARK] = { .type = NLA_U32 },
1207 [CTA_ID] = { .type = NLA_U32 },
1208 [CTA_NAT_DST] = { .type = NLA_NESTED },
1209 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1210 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1211 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1212 [CTA_ZONE] = { .type = NLA_U16 },
1213 [CTA_MARK_MASK] = { .type = NLA_U32 },
1214 [CTA_LABELS] = { .type = NLA_BINARY,
1215 .len = NF_CT_LABELS_MAX_SIZE },
1216 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1217 .len = NF_CT_LABELS_MAX_SIZE },
1220 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1222 if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
1225 return ctnetlink_filter_match(ct, data);
1228 static int ctnetlink_flush_conntrack(struct net *net,
1229 const struct nlattr * const cda[],
1230 u32 portid, int report, u8 family)
1232 struct ctnetlink_filter *filter = NULL;
1234 if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
1235 filter = ctnetlink_alloc_filter(cda, family);
1237 return PTR_ERR(filter);
1240 nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
1247 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1248 struct sk_buff *skb,
1249 const struct nlmsghdr *nlh,
1250 const struct nlattr * const cda[],
1251 struct netlink_ext_ack *extack)
1253 struct nf_conntrack_tuple_hash *h;
1254 struct nf_conntrack_tuple tuple;
1256 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1257 u_int8_t u3 = nfmsg->nfgen_family;
1258 struct nf_conntrack_zone zone;
1261 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1265 if (cda[CTA_TUPLE_ORIG])
1266 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1268 else if (cda[CTA_TUPLE_REPLY])
1269 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1272 return ctnetlink_flush_conntrack(net, cda,
1273 NETLINK_CB(skb).portid,
1274 nlmsg_report(nlh), u3);
1280 h = nf_conntrack_find_get(net, &zone, &tuple);
1284 ct = nf_ct_tuplehash_to_ctrack(h);
1286 if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) {
1292 __be32 id = nla_get_be32(cda[CTA_ID]);
1294 if (id != (__force __be32)nf_ct_get_id(ct)) {
1300 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1306 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1307 struct sk_buff *skb,
1308 const struct nlmsghdr *nlh,
1309 const struct nlattr * const cda[],
1310 struct netlink_ext_ack *extack)
1312 struct nf_conntrack_tuple_hash *h;
1313 struct nf_conntrack_tuple tuple;
1315 struct sk_buff *skb2 = NULL;
1316 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1317 u_int8_t u3 = nfmsg->nfgen_family;
1318 struct nf_conntrack_zone zone;
1321 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1322 struct netlink_dump_control c = {
1323 .start = ctnetlink_start,
1324 .dump = ctnetlink_dump_table,
1325 .done = ctnetlink_done,
1326 .data = (void *)cda,
1329 return netlink_dump_start(ctnl, skb, nlh, &c);
1332 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1336 if (cda[CTA_TUPLE_ORIG])
1337 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1339 else if (cda[CTA_TUPLE_REPLY])
1340 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1348 h = nf_conntrack_find_get(net, &zone, &tuple);
1352 ct = nf_ct_tuplehash_to_ctrack(h);
1355 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1362 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1363 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1369 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1378 /* this avoids a loop in nfnetlink. */
1379 return err == -EAGAIN ? -ENOBUFS : err;
1382 static int ctnetlink_done_list(struct netlink_callback *cb)
1385 nf_ct_put((struct nf_conn *)cb->args[1]);
1390 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1392 struct nf_conn *ct, *last;
1393 struct nf_conntrack_tuple_hash *h;
1394 struct hlist_nulls_node *n;
1395 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1396 u_int8_t l3proto = nfmsg->nfgen_family;
1399 struct hlist_nulls_head *list;
1400 struct net *net = sock_net(skb->sk);
1405 last = (struct nf_conn *)cb->args[1];
1407 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1408 struct ct_pcpu *pcpu;
1410 if (!cpu_possible(cpu))
1413 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1414 spin_lock_bh(&pcpu->lock);
1415 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1417 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1418 ct = nf_ct_tuplehash_to_ctrack(h);
1419 if (l3proto && nf_ct_l3num(ct) != l3proto)
1427 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1429 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1433 if (!atomic_inc_not_zero(&ct->ct_general.use))
1436 cb->args[1] = (unsigned long)ct;
1437 spin_unlock_bh(&pcpu->lock);
1445 spin_unlock_bh(&pcpu->lock);
1456 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1458 return ctnetlink_dump_list(skb, cb, true);
1461 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1462 struct sk_buff *skb,
1463 const struct nlmsghdr *nlh,
1464 const struct nlattr * const cda[],
1465 struct netlink_ext_ack *extack)
1467 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1468 struct netlink_dump_control c = {
1469 .dump = ctnetlink_dump_dying,
1470 .done = ctnetlink_done_list,
1472 return netlink_dump_start(ctnl, skb, nlh, &c);
1479 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1481 return ctnetlink_dump_list(skb, cb, false);
1484 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1485 struct sk_buff *skb,
1486 const struct nlmsghdr *nlh,
1487 const struct nlattr * const cda[],
1488 struct netlink_ext_ack *extack)
1490 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1491 struct netlink_dump_control c = {
1492 .dump = ctnetlink_dump_unconfirmed,
1493 .done = ctnetlink_done_list,
1495 return netlink_dump_start(ctnl, skb, nlh, &c);
1501 #if IS_ENABLED(CONFIG_NF_NAT)
1503 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1504 enum nf_nat_manip_type manip,
1505 const struct nlattr *attr)
1507 struct nf_nat_hook *nat_hook;
1510 nat_hook = rcu_dereference(nf_nat_hook);
1512 #ifdef CONFIG_MODULES
1514 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1515 if (request_module("nf-nat") < 0) {
1516 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1520 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1522 nat_hook = rcu_dereference(nf_nat_hook);
1529 err = nat_hook->parse_nat_setup(ct, manip, attr);
1530 if (err == -EAGAIN) {
1531 #ifdef CONFIG_MODULES
1533 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1534 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1535 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1539 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1550 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1555 /* Ignore these unchangable bits */
1556 on &= ~IPS_UNCHANGEABLE_MASK;
1557 off &= ~IPS_UNCHANGEABLE_MASK;
1559 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1560 if (on & (1 << bit))
1561 set_bit(bit, &ct->status);
1562 else if (off & (1 << bit))
1563 clear_bit(bit, &ct->status);
1568 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1571 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1572 d = ct->status ^ status;
1574 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1578 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1579 /* SEEN_REPLY bit can only be set */
1582 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1583 /* ASSURED bit can only be set */
1586 __ctnetlink_change_status(ct, status, 0);
1591 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1593 #if IS_ENABLED(CONFIG_NF_NAT)
1596 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1599 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1604 return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1607 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1613 static int ctnetlink_change_helper(struct nf_conn *ct,
1614 const struct nlattr * const cda[])
1616 struct nf_conntrack_helper *helper;
1617 struct nf_conn_help *help = nfct_help(ct);
1618 char *helpname = NULL;
1619 struct nlattr *helpinfo = NULL;
1622 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1626 /* don't change helper of sibling connections */
1628 /* If we try to change the helper to the same thing twice,
1629 * treat the second attempt as a no-op instead of returning
1635 helper = rcu_dereference(help->helper);
1636 if (helper && !strcmp(helper->name, helpname))
1644 if (!strcmp(helpname, "")) {
1645 if (help && help->helper) {
1646 /* we had a helper before ... */
1647 nf_ct_remove_expectations(ct);
1648 RCU_INIT_POINTER(help->helper, NULL);
1655 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1656 nf_ct_protonum(ct));
1657 if (helper == NULL) {
1663 if (help->helper == helper) {
1664 /* update private helper data if allowed. */
1665 if (helper->from_nlattr)
1666 helper->from_nlattr(helpinfo, ct);
1671 /* we cannot set a helper for an existing conntrack */
1679 static int ctnetlink_change_timeout(struct nf_conn *ct,
1680 const struct nlattr * const cda[])
1682 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1684 if (timeout > INT_MAX)
1686 ct->timeout = nfct_time_stamp + (u32)timeout;
1688 if (test_bit(IPS_DYING_BIT, &ct->status))
1694 #if defined(CONFIG_NF_CONNTRACK_MARK)
1695 static void ctnetlink_change_mark(struct nf_conn *ct,
1696 const struct nlattr * const cda[])
1698 u32 mark, newmark, mask = 0;
1700 if (cda[CTA_MARK_MASK])
1701 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
1703 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1704 newmark = (ct->mark & mask) ^ mark;
1705 if (newmark != ct->mark)
1710 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1711 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1712 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1713 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1716 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1717 const struct nlattr * const cda[])
1719 const struct nlattr *attr = cda[CTA_PROTOINFO];
1720 const struct nf_conntrack_l4proto *l4proto;
1721 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1724 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1729 l4proto = nf_ct_l4proto_find(nf_ct_protonum(ct));
1730 if (l4proto->from_nlattr)
1731 err = l4proto->from_nlattr(tb, ct);
1736 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1737 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1738 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1739 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1742 static int change_seq_adj(struct nf_ct_seqadj *seq,
1743 const struct nlattr * const attr)
1746 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1748 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1752 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1755 seq->correction_pos =
1756 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1758 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1761 seq->offset_before =
1762 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1764 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1768 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1774 ctnetlink_change_seq_adj(struct nf_conn *ct,
1775 const struct nlattr * const cda[])
1777 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1783 spin_lock_bh(&ct->lock);
1784 if (cda[CTA_SEQ_ADJ_ORIG]) {
1785 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1786 cda[CTA_SEQ_ADJ_ORIG]);
1790 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1793 if (cda[CTA_SEQ_ADJ_REPLY]) {
1794 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1795 cda[CTA_SEQ_ADJ_REPLY]);
1799 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1802 spin_unlock_bh(&ct->lock);
1805 spin_unlock_bh(&ct->lock);
1809 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
1810 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 },
1811 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 },
1812 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 },
1815 static int ctnetlink_change_synproxy(struct nf_conn *ct,
1816 const struct nlattr * const cda[])
1818 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
1819 struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
1825 err = nla_parse_nested(tb, CTA_SYNPROXY_MAX, cda[CTA_SYNPROXY],
1826 synproxy_policy, NULL);
1830 if (!tb[CTA_SYNPROXY_ISN] ||
1831 !tb[CTA_SYNPROXY_ITS] ||
1832 !tb[CTA_SYNPROXY_TSOFF])
1835 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
1836 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
1837 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
1843 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1845 #ifdef CONFIG_NF_CONNTRACK_LABELS
1846 size_t len = nla_len(cda[CTA_LABELS]);
1847 const void *mask = cda[CTA_LABELS_MASK];
1849 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1853 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1854 nla_len(cda[CTA_LABELS_MASK]) != len)
1856 mask = nla_data(cda[CTA_LABELS_MASK]);
1861 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1868 ctnetlink_change_conntrack(struct nf_conn *ct,
1869 const struct nlattr * const cda[])
1873 /* only allow NAT changes and master assignation for new conntracks */
1874 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1877 if (cda[CTA_HELP]) {
1878 err = ctnetlink_change_helper(ct, cda);
1883 if (cda[CTA_TIMEOUT]) {
1884 err = ctnetlink_change_timeout(ct, cda);
1889 if (cda[CTA_STATUS]) {
1890 err = ctnetlink_change_status(ct, cda);
1895 if (cda[CTA_PROTOINFO]) {
1896 err = ctnetlink_change_protoinfo(ct, cda);
1901 #if defined(CONFIG_NF_CONNTRACK_MARK)
1903 ctnetlink_change_mark(ct, cda);
1906 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1907 err = ctnetlink_change_seq_adj(ct, cda);
1912 if (cda[CTA_SYNPROXY]) {
1913 err = ctnetlink_change_synproxy(ct, cda);
1918 if (cda[CTA_LABELS]) {
1919 err = ctnetlink_attach_labels(ct, cda);
1927 static struct nf_conn *
1928 ctnetlink_create_conntrack(struct net *net,
1929 const struct nf_conntrack_zone *zone,
1930 const struct nlattr * const cda[],
1931 struct nf_conntrack_tuple *otuple,
1932 struct nf_conntrack_tuple *rtuple,
1937 struct nf_conntrack_helper *helper;
1938 struct nf_conn_tstamp *tstamp;
1941 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1943 return ERR_PTR(-ENOMEM);
1945 if (!cda[CTA_TIMEOUT])
1948 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1949 if (timeout > INT_MAX)
1951 ct->timeout = (u32)timeout + nfct_time_stamp;
1954 if (cda[CTA_HELP]) {
1955 char *helpname = NULL;
1956 struct nlattr *helpinfo = NULL;
1958 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1962 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1963 nf_ct_protonum(ct));
1964 if (helper == NULL) {
1966 #ifdef CONFIG_MODULES
1967 if (request_module("nfct-helper-%s", helpname) < 0) {
1973 helper = __nf_conntrack_helper_find(helpname,
1975 nf_ct_protonum(ct));
1985 struct nf_conn_help *help;
1987 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1992 /* set private helper data if allowed. */
1993 if (helper->from_nlattr)
1994 helper->from_nlattr(helpinfo, ct);
1996 /* not in hash table yet so not strictly necessary */
1997 RCU_INIT_POINTER(help->helper, helper);
2000 /* try an implicit helper assignation */
2001 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
2006 err = ctnetlink_setup_nat(ct, cda);
2010 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
2011 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
2012 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
2013 nf_ct_labels_ext_add(ct);
2014 nfct_seqadj_ext_add(ct);
2015 nfct_synproxy_ext_add(ct);
2017 /* we must add conntrack extensions before confirmation. */
2018 ct->status |= IPS_CONFIRMED;
2020 if (cda[CTA_STATUS]) {
2021 err = ctnetlink_change_status(ct, cda);
2026 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2027 err = ctnetlink_change_seq_adj(ct, cda);
2032 memset(&ct->proto, 0, sizeof(ct->proto));
2033 if (cda[CTA_PROTOINFO]) {
2034 err = ctnetlink_change_protoinfo(ct, cda);
2039 if (cda[CTA_SYNPROXY]) {
2040 err = ctnetlink_change_synproxy(ct, cda);
2045 #if defined(CONFIG_NF_CONNTRACK_MARK)
2047 ctnetlink_change_mark(ct, cda);
2050 /* setup master conntrack: this is a confirmed expectation */
2051 if (cda[CTA_TUPLE_MASTER]) {
2052 struct nf_conntrack_tuple master;
2053 struct nf_conntrack_tuple_hash *master_h;
2054 struct nf_conn *master_ct;
2056 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2061 master_h = nf_conntrack_find_get(net, zone, &master);
2062 if (master_h == NULL) {
2066 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2067 __set_bit(IPS_EXPECTED_BIT, &ct->status);
2068 ct->master = master_ct;
2070 tstamp = nf_conn_tstamp_find(ct);
2072 tstamp->start = ktime_get_real_ns();
2074 err = nf_conntrack_hash_check_insert(ct);
2085 nf_conntrack_free(ct);
2086 return ERR_PTR(err);
2089 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
2090 struct sk_buff *skb,
2091 const struct nlmsghdr *nlh,
2092 const struct nlattr * const cda[],
2093 struct netlink_ext_ack *extack)
2095 struct nf_conntrack_tuple otuple, rtuple;
2096 struct nf_conntrack_tuple_hash *h = NULL;
2097 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2099 u_int8_t u3 = nfmsg->nfgen_family;
2100 struct nf_conntrack_zone zone;
2103 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2107 if (cda[CTA_TUPLE_ORIG]) {
2108 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2114 if (cda[CTA_TUPLE_REPLY]) {
2115 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2121 if (cda[CTA_TUPLE_ORIG])
2122 h = nf_conntrack_find_get(net, &zone, &otuple);
2123 else if (cda[CTA_TUPLE_REPLY])
2124 h = nf_conntrack_find_get(net, &zone, &rtuple);
2128 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2129 enum ip_conntrack_events events;
2131 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2133 if (otuple.dst.protonum != rtuple.dst.protonum)
2136 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
2142 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2143 events = 1 << IPCT_RELATED;
2145 events = 1 << IPCT_NEW;
2147 if (cda[CTA_LABELS] &&
2148 ctnetlink_attach_labels(ct, cda) == 0)
2149 events |= (1 << IPCT_LABEL);
2151 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2152 (1 << IPCT_ASSURED) |
2153 (1 << IPCT_HELPER) |
2154 (1 << IPCT_PROTOINFO) |
2155 (1 << IPCT_SEQADJ) |
2157 (1 << IPCT_SYNPROXY) |
2159 ct, NETLINK_CB(skb).portid,
2166 /* implicit 'else' */
2169 ct = nf_ct_tuplehash_to_ctrack(h);
2170 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2171 err = ctnetlink_change_conntrack(ct, cda);
2173 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2174 (1 << IPCT_ASSURED) |
2175 (1 << IPCT_HELPER) |
2177 (1 << IPCT_PROTOINFO) |
2178 (1 << IPCT_SEQADJ) |
2180 (1 << IPCT_SYNPROXY),
2181 ct, NETLINK_CB(skb).portid,
2191 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2192 __u16 cpu, const struct ip_conntrack_stat *st)
2194 struct nlmsghdr *nlh;
2195 struct nfgenmsg *nfmsg;
2196 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2198 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2199 IPCTNL_MSG_CT_GET_STATS_CPU);
2200 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2204 nfmsg = nlmsg_data(nlh);
2205 nfmsg->nfgen_family = AF_UNSPEC;
2206 nfmsg->version = NFNETLINK_V0;
2207 nfmsg->res_id = htons(cpu);
2209 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2210 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2211 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2212 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2213 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2214 htonl(st->insert_failed)) ||
2215 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2216 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2217 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2218 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2219 htonl(st->search_restart)))
2220 goto nla_put_failure;
2222 nlmsg_end(skb, nlh);
2227 nlmsg_cancel(skb, nlh);
2232 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2235 struct net *net = sock_net(skb->sk);
2237 if (cb->args[0] == nr_cpu_ids)
2240 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2241 const struct ip_conntrack_stat *st;
2243 if (!cpu_possible(cpu))
2246 st = per_cpu_ptr(net->ct.stat, cpu);
2247 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2248 NETLINK_CB(cb->skb).portid,
2258 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2259 struct sk_buff *skb,
2260 const struct nlmsghdr *nlh,
2261 const struct nlattr * const cda[],
2262 struct netlink_ext_ack *extack)
2264 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2265 struct netlink_dump_control c = {
2266 .dump = ctnetlink_ct_stat_cpu_dump,
2268 return netlink_dump_start(ctnl, skb, nlh, &c);
2275 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2278 struct nlmsghdr *nlh;
2279 struct nfgenmsg *nfmsg;
2280 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2281 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2283 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2284 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2288 nfmsg = nlmsg_data(nlh);
2289 nfmsg->nfgen_family = AF_UNSPEC;
2290 nfmsg->version = NFNETLINK_V0;
2293 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2294 goto nla_put_failure;
2296 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2297 goto nla_put_failure;
2299 nlmsg_end(skb, nlh);
2304 nlmsg_cancel(skb, nlh);
2308 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2309 struct sk_buff *skb, const struct nlmsghdr *nlh,
2310 const struct nlattr * const cda[],
2311 struct netlink_ext_ack *extack)
2313 struct sk_buff *skb2;
2316 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2320 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2322 NFNL_MSG_TYPE(nlh->nlmsg_type),
2327 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2336 /* this avoids a loop in nfnetlink. */
2337 return err == -EAGAIN ? -ENOBUFS : err;
2340 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2341 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2342 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2343 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2344 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2345 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2346 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2347 .len = NF_CT_HELPER_NAME_LEN - 1 },
2348 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2349 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2350 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2351 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2352 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2355 static struct nf_conntrack_expect *
2356 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2357 struct nf_conntrack_helper *helper,
2358 struct nf_conntrack_tuple *tuple,
2359 struct nf_conntrack_tuple *mask);
2361 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2363 ctnetlink_glue_build_size(const struct nf_conn *ct)
2365 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2366 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2367 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2368 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2369 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2370 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2371 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2372 + nla_total_size(0) /* CTA_PROTOINFO */
2373 + nla_total_size(0) /* CTA_HELP */
2374 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2375 + ctnetlink_secctx_size(ct)
2376 #if IS_ENABLED(CONFIG_NF_NAT)
2377 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2378 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2380 #ifdef CONFIG_NF_CONNTRACK_MARK
2381 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2383 #ifdef CONFIG_NF_CONNTRACK_ZONES
2384 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2386 + ctnetlink_proto_size(ct)
2390 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2391 enum ip_conntrack_info *ctinfo)
2393 return nf_ct_get(skb, ctinfo);
2396 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2398 const struct nf_conntrack_zone *zone;
2399 struct nlattr *nest_parms;
2401 zone = nf_ct_zone(ct);
2403 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2405 goto nla_put_failure;
2406 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2407 goto nla_put_failure;
2408 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2409 NF_CT_ZONE_DIR_ORIG) < 0)
2410 goto nla_put_failure;
2411 nla_nest_end(skb, nest_parms);
2413 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2415 goto nla_put_failure;
2416 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2417 goto nla_put_failure;
2418 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2419 NF_CT_ZONE_DIR_REPL) < 0)
2420 goto nla_put_failure;
2421 nla_nest_end(skb, nest_parms);
2423 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2424 NF_CT_DEFAULT_ZONE_DIR) < 0)
2425 goto nla_put_failure;
2427 if (ctnetlink_dump_id(skb, ct) < 0)
2428 goto nla_put_failure;
2430 if (ctnetlink_dump_status(skb, ct) < 0)
2431 goto nla_put_failure;
2433 if (ctnetlink_dump_timeout(skb, ct) < 0)
2434 goto nla_put_failure;
2436 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2437 goto nla_put_failure;
2439 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2440 goto nla_put_failure;
2442 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2443 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2444 goto nla_put_failure;
2446 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2447 goto nla_put_failure;
2449 if ((ct->status & IPS_SEQ_ADJUST) &&
2450 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2451 goto nla_put_failure;
2453 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2454 goto nla_put_failure;
2456 #ifdef CONFIG_NF_CONNTRACK_MARK
2457 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2458 goto nla_put_failure;
2460 if (ctnetlink_dump_labels(skb, ct) < 0)
2461 goto nla_put_failure;
2469 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2470 enum ip_conntrack_info ctinfo,
2471 u_int16_t ct_attr, u_int16_t ct_info_attr)
2473 struct nlattr *nest_parms;
2475 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2477 goto nla_put_failure;
2479 if (__ctnetlink_glue_build(skb, ct) < 0)
2480 goto nla_put_failure;
2482 nla_nest_end(skb, nest_parms);
2484 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2485 goto nla_put_failure;
2494 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2496 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2497 unsigned long d = ct->status ^ status;
2499 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2500 /* SEEN_REPLY bit can only be set */
2503 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2504 /* ASSURED bit can only be set */
2507 /* This check is less strict than ctnetlink_change_status()
2508 * because callers often flip IPS_EXPECTED bits when sending
2509 * an NFQA_CT attribute to the kernel. So ignore the
2510 * unchangeable bits but do not error out. Also user programs
2511 * are allowed to clear the bits that they are allowed to change.
2513 __ctnetlink_change_status(ct, status, ~status);
2518 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2522 if (cda[CTA_TIMEOUT]) {
2523 err = ctnetlink_change_timeout(ct, cda);
2527 if (cda[CTA_STATUS]) {
2528 err = ctnetlink_update_status(ct, cda);
2532 if (cda[CTA_HELP]) {
2533 err = ctnetlink_change_helper(ct, cda);
2537 if (cda[CTA_LABELS]) {
2538 err = ctnetlink_attach_labels(ct, cda);
2542 #if defined(CONFIG_NF_CONNTRACK_MARK)
2543 if (cda[CTA_MARK]) {
2544 ctnetlink_change_mark(ct, cda);
2551 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2553 struct nlattr *cda[CTA_MAX+1];
2556 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2560 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2563 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2564 const struct nf_conn *ct,
2565 struct nf_conntrack_tuple *tuple,
2566 struct nf_conntrack_tuple *mask)
2570 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2571 nf_ct_l3num(ct), NULL);
2575 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2576 nf_ct_l3num(ct), NULL);
2580 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2581 u32 portid, u32 report)
2583 struct nlattr *cda[CTA_EXPECT_MAX+1];
2584 struct nf_conntrack_tuple tuple, mask;
2585 struct nf_conntrack_helper *helper = NULL;
2586 struct nf_conntrack_expect *exp;
2589 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2594 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2599 if (cda[CTA_EXPECT_HELP_NAME]) {
2600 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2602 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2603 nf_ct_protonum(ct));
2608 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2609 helper, &tuple, &mask);
2611 return PTR_ERR(exp);
2613 err = nf_ct_expect_related_report(exp, portid, report);
2614 nf_ct_expect_put(exp);
2618 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2619 enum ip_conntrack_info ctinfo, int diff)
2621 if (!(ct->status & IPS_NAT_MASK))
2624 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2627 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2628 .get_ct = ctnetlink_glue_get_ct,
2629 .build_size = ctnetlink_glue_build_size,
2630 .build = ctnetlink_glue_build,
2631 .parse = ctnetlink_glue_parse,
2632 .attach_expect = ctnetlink_glue_attach_expect,
2633 .seq_adjust = ctnetlink_glue_seqadj,
2635 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2637 /***********************************************************************
2639 ***********************************************************************/
2641 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2642 const struct nf_conntrack_tuple *tuple,
2645 struct nlattr *nest_parms;
2647 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2649 goto nla_put_failure;
2650 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2651 goto nla_put_failure;
2652 nla_nest_end(skb, nest_parms);
2660 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2661 const struct nf_conntrack_tuple *tuple,
2662 const struct nf_conntrack_tuple_mask *mask)
2664 const struct nf_conntrack_l4proto *l4proto;
2665 struct nf_conntrack_tuple m;
2666 struct nlattr *nest_parms;
2669 memset(&m, 0xFF, sizeof(m));
2670 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2671 m.src.u.all = mask->src.u.all;
2672 m.dst.protonum = tuple->dst.protonum;
2674 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2676 goto nla_put_failure;
2679 ret = ctnetlink_dump_tuples_ip(skb, &m);
2681 l4proto = nf_ct_l4proto_find(tuple->dst.protonum);
2682 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2686 if (unlikely(ret < 0))
2687 goto nla_put_failure;
2689 nla_nest_end(skb, nest_parms);
2697 static const union nf_inet_addr any_addr;
2699 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2701 static __read_mostly siphash_key_t exp_id_seed;
2702 unsigned long a, b, c, d;
2704 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2706 a = (unsigned long)exp;
2707 b = (unsigned long)exp->helper;
2708 c = (unsigned long)exp->master;
2709 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2712 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2714 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2719 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2720 const struct nf_conntrack_expect *exp)
2722 struct nf_conn *master = exp->master;
2723 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2724 struct nf_conn_help *help;
2725 #if IS_ENABLED(CONFIG_NF_NAT)
2726 struct nlattr *nest_parms;
2727 struct nf_conntrack_tuple nat_tuple = {};
2729 struct nf_ct_helper_expectfn *expfn;
2734 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2735 goto nla_put_failure;
2736 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2737 goto nla_put_failure;
2738 if (ctnetlink_exp_dump_tuple(skb,
2739 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2740 CTA_EXPECT_MASTER) < 0)
2741 goto nla_put_failure;
2743 #if IS_ENABLED(CONFIG_NF_NAT)
2744 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2745 exp->saved_proto.all) {
2746 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2748 goto nla_put_failure;
2750 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2751 goto nla_put_failure;
2753 nat_tuple.src.l3num = nf_ct_l3num(master);
2754 nat_tuple.src.u3 = exp->saved_addr;
2755 nat_tuple.dst.protonum = nf_ct_protonum(master);
2756 nat_tuple.src.u = exp->saved_proto;
2758 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2759 CTA_EXPECT_NAT_TUPLE) < 0)
2760 goto nla_put_failure;
2761 nla_nest_end(skb, nest_parms);
2764 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2765 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
2766 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2767 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2768 goto nla_put_failure;
2769 help = nfct_help(master);
2771 struct nf_conntrack_helper *helper;
2773 helper = rcu_dereference(help->helper);
2775 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2776 goto nla_put_failure;
2778 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2779 if (expfn != NULL &&
2780 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2781 goto nla_put_failure;
2790 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2791 int event, const struct nf_conntrack_expect *exp)
2793 struct nlmsghdr *nlh;
2794 struct nfgenmsg *nfmsg;
2795 unsigned int flags = portid ? NLM_F_MULTI : 0;
2797 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2798 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2802 nfmsg = nlmsg_data(nlh);
2803 nfmsg->nfgen_family = exp->tuple.src.l3num;
2804 nfmsg->version = NFNETLINK_V0;
2807 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2808 goto nla_put_failure;
2810 nlmsg_end(skb, nlh);
2815 nlmsg_cancel(skb, nlh);
2819 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2821 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2823 struct nf_conntrack_expect *exp = item->exp;
2824 struct net *net = nf_ct_exp_net(exp);
2825 struct nlmsghdr *nlh;
2826 struct nfgenmsg *nfmsg;
2827 struct sk_buff *skb;
2828 unsigned int type, group;
2831 if (events & (1 << IPEXP_DESTROY)) {
2832 type = IPCTNL_MSG_EXP_DELETE;
2833 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2834 } else if (events & (1 << IPEXP_NEW)) {
2835 type = IPCTNL_MSG_EXP_NEW;
2836 flags = NLM_F_CREATE|NLM_F_EXCL;
2837 group = NFNLGRP_CONNTRACK_EXP_NEW;
2841 if (!item->report && !nfnetlink_has_listeners(net, group))
2844 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2848 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2849 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2853 nfmsg = nlmsg_data(nlh);
2854 nfmsg->nfgen_family = exp->tuple.src.l3num;
2855 nfmsg->version = NFNETLINK_V0;
2858 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2859 goto nla_put_failure;
2861 nlmsg_end(skb, nlh);
2862 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2866 nlmsg_cancel(skb, nlh);
2870 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2874 static int ctnetlink_exp_done(struct netlink_callback *cb)
2877 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2882 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2884 struct net *net = sock_net(skb->sk);
2885 struct nf_conntrack_expect *exp, *last;
2886 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2887 u_int8_t l3proto = nfmsg->nfgen_family;
2890 last = (struct nf_conntrack_expect *)cb->args[1];
2891 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2893 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2895 if (l3proto && exp->tuple.src.l3num != l3proto)
2898 if (!net_eq(nf_ct_net(exp->master), net))
2906 if (ctnetlink_exp_fill_info(skb,
2907 NETLINK_CB(cb->skb).portid,
2911 if (!refcount_inc_not_zero(&exp->use))
2913 cb->args[1] = (unsigned long)exp;
2925 nf_ct_expect_put(last);
2931 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2933 struct nf_conntrack_expect *exp, *last;
2934 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2935 struct nf_conn *ct = cb->data;
2936 struct nf_conn_help *help = nfct_help(ct);
2937 u_int8_t l3proto = nfmsg->nfgen_family;
2943 last = (struct nf_conntrack_expect *)cb->args[1];
2945 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2946 if (l3proto && exp->tuple.src.l3num != l3proto)
2953 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2957 if (!refcount_inc_not_zero(&exp->use))
2959 cb->args[1] = (unsigned long)exp;
2971 nf_ct_expect_put(last);
2976 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2977 struct sk_buff *skb,
2978 const struct nlmsghdr *nlh,
2979 const struct nlattr * const cda[],
2980 struct netlink_ext_ack *extack)
2983 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2984 u_int8_t u3 = nfmsg->nfgen_family;
2985 struct nf_conntrack_tuple tuple;
2986 struct nf_conntrack_tuple_hash *h;
2988 struct nf_conntrack_zone zone;
2989 struct netlink_dump_control c = {
2990 .dump = ctnetlink_exp_ct_dump_table,
2991 .done = ctnetlink_exp_done,
2994 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2999 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3003 h = nf_conntrack_find_get(net, &zone, &tuple);
3007 ct = nf_ct_tuplehash_to_ctrack(h);
3008 /* No expectation linked to this connection tracking. */
3009 if (!nfct_help(ct)) {
3016 err = netlink_dump_start(ctnl, skb, nlh, &c);
3022 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
3023 struct sk_buff *skb, const struct nlmsghdr *nlh,
3024 const struct nlattr * const cda[],
3025 struct netlink_ext_ack *extack)
3027 struct nf_conntrack_tuple tuple;
3028 struct nf_conntrack_expect *exp;
3029 struct sk_buff *skb2;
3030 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3031 u_int8_t u3 = nfmsg->nfgen_family;
3032 struct nf_conntrack_zone zone;
3035 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3036 if (cda[CTA_EXPECT_MASTER])
3037 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
3040 struct netlink_dump_control c = {
3041 .dump = ctnetlink_exp_dump_table,
3042 .done = ctnetlink_exp_done,
3044 return netlink_dump_start(ctnl, skb, nlh, &c);
3048 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3052 if (cda[CTA_EXPECT_TUPLE])
3053 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3055 else if (cda[CTA_EXPECT_MASTER])
3056 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3064 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3068 if (cda[CTA_EXPECT_ID]) {
3069 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3071 if (id != nf_expect_get_id(exp)) {
3072 nf_ct_expect_put(exp);
3078 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3080 nf_ct_expect_put(exp);
3085 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3086 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
3088 nf_ct_expect_put(exp);
3092 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
3101 /* this avoids a loop in nfnetlink. */
3102 return err == -EAGAIN ? -ENOBUFS : err;
3105 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3107 const struct nf_conn_help *m_help;
3108 const char *name = data;
3110 m_help = nfct_help(exp->master);
3112 return strcmp(m_help->helper->name, name) == 0;
3115 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3120 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
3121 struct sk_buff *skb, const struct nlmsghdr *nlh,
3122 const struct nlattr * const cda[],
3123 struct netlink_ext_ack *extack)
3125 struct nf_conntrack_expect *exp;
3126 struct nf_conntrack_tuple tuple;
3127 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3128 u_int8_t u3 = nfmsg->nfgen_family;
3129 struct nf_conntrack_zone zone;
3132 if (cda[CTA_EXPECT_TUPLE]) {
3133 /* delete a single expect by tuple */
3134 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3138 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3143 /* bump usage count to 2 */
3144 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3148 if (cda[CTA_EXPECT_ID]) {
3149 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3150 if (ntohl(id) != (u32)(unsigned long)exp) {
3151 nf_ct_expect_put(exp);
3156 /* after list removal, usage count == 1 */
3157 spin_lock_bh(&nf_conntrack_expect_lock);
3158 if (del_timer(&exp->timeout)) {
3159 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3161 nf_ct_expect_put(exp);
3163 spin_unlock_bh(&nf_conntrack_expect_lock);
3164 /* have to put what we 'get' above.
3165 * after this line usage count == 0 */
3166 nf_ct_expect_put(exp);
3167 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3168 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3170 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3171 NETLINK_CB(skb).portid,
3174 /* This basically means we have to flush everything*/
3175 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3176 NETLINK_CB(skb).portid,
3183 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3184 const struct nlattr * const cda[])
3186 if (cda[CTA_EXPECT_TIMEOUT]) {
3187 if (!del_timer(&x->timeout))
3190 x->timeout.expires = jiffies +
3191 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3192 add_timer(&x->timeout);
3197 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3198 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3199 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3203 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3204 struct nf_conntrack_expect *exp,
3207 #if IS_ENABLED(CONFIG_NF_NAT)
3208 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3209 struct nf_conntrack_tuple nat_tuple = {};
3212 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3213 exp_nat_nla_policy, NULL);
3217 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3220 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3221 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3226 exp->saved_addr = nat_tuple.src.u3;
3227 exp->saved_proto = nat_tuple.src.u;
3228 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3236 static struct nf_conntrack_expect *
3237 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3238 struct nf_conntrack_helper *helper,
3239 struct nf_conntrack_tuple *tuple,
3240 struct nf_conntrack_tuple *mask)
3242 u_int32_t class = 0;
3243 struct nf_conntrack_expect *exp;
3244 struct nf_conn_help *help;
3247 help = nfct_help(ct);
3249 return ERR_PTR(-EOPNOTSUPP);
3251 if (cda[CTA_EXPECT_CLASS] && helper) {
3252 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3253 if (class > helper->expect_class_max)
3254 return ERR_PTR(-EINVAL);
3256 exp = nf_ct_expect_alloc(ct);
3258 return ERR_PTR(-ENOMEM);
3260 if (cda[CTA_EXPECT_FLAGS]) {
3261 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3262 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3266 if (cda[CTA_EXPECT_FN]) {
3267 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3268 struct nf_ct_helper_expectfn *expfn;
3270 expfn = nf_ct_helper_expectfn_find_by_name(name);
3271 if (expfn == NULL) {
3275 exp->expectfn = expfn->expectfn;
3277 exp->expectfn = NULL;
3281 exp->helper = helper;
3282 exp->tuple = *tuple;
3283 exp->mask.src.u3 = mask->src.u3;
3284 exp->mask.src.u.all = mask->src.u.all;
3286 if (cda[CTA_EXPECT_NAT]) {
3287 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3288 exp, nf_ct_l3num(ct));
3294 nf_ct_expect_put(exp);
3295 return ERR_PTR(err);
3299 ctnetlink_create_expect(struct net *net,
3300 const struct nf_conntrack_zone *zone,
3301 const struct nlattr * const cda[],
3302 u_int8_t u3, u32 portid, int report)
3304 struct nf_conntrack_tuple tuple, mask, master_tuple;
3305 struct nf_conntrack_tuple_hash *h = NULL;
3306 struct nf_conntrack_helper *helper = NULL;
3307 struct nf_conntrack_expect *exp;
3311 /* caller guarantees that those three CTA_EXPECT_* exist */
3312 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3316 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3320 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3325 /* Look for master conntrack of this expectation */
3326 h = nf_conntrack_find_get(net, zone, &master_tuple);
3329 ct = nf_ct_tuplehash_to_ctrack(h);
3332 if (cda[CTA_EXPECT_HELP_NAME]) {
3333 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3335 helper = __nf_conntrack_helper_find(helpname, u3,
3336 nf_ct_protonum(ct));
3337 if (helper == NULL) {
3339 #ifdef CONFIG_MODULES
3340 if (request_module("nfct-helper-%s", helpname) < 0) {
3345 helper = __nf_conntrack_helper_find(helpname, u3,
3346 nf_ct_protonum(ct));
3358 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3364 err = nf_ct_expect_related_report(exp, portid, report);
3365 nf_ct_expect_put(exp);
3373 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3374 struct sk_buff *skb, const struct nlmsghdr *nlh,
3375 const struct nlattr * const cda[],
3376 struct netlink_ext_ack *extack)
3378 struct nf_conntrack_tuple tuple;
3379 struct nf_conntrack_expect *exp;
3380 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3381 u_int8_t u3 = nfmsg->nfgen_family;
3382 struct nf_conntrack_zone zone;
3385 if (!cda[CTA_EXPECT_TUPLE]
3386 || !cda[CTA_EXPECT_MASK]
3387 || !cda[CTA_EXPECT_MASTER])
3390 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3394 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3399 spin_lock_bh(&nf_conntrack_expect_lock);
3400 exp = __nf_ct_expect_find(net, &zone, &tuple);
3402 spin_unlock_bh(&nf_conntrack_expect_lock);
3404 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3405 err = ctnetlink_create_expect(net, &zone, cda, u3,
3406 NETLINK_CB(skb).portid,
3413 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3414 err = ctnetlink_change_expect(exp, cda);
3415 spin_unlock_bh(&nf_conntrack_expect_lock);
3421 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3422 const struct ip_conntrack_stat *st)
3424 struct nlmsghdr *nlh;
3425 struct nfgenmsg *nfmsg;
3426 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3428 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3429 IPCTNL_MSG_EXP_GET_STATS_CPU);
3430 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3434 nfmsg = nlmsg_data(nlh);
3435 nfmsg->nfgen_family = AF_UNSPEC;
3436 nfmsg->version = NFNETLINK_V0;
3437 nfmsg->res_id = htons(cpu);
3439 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3440 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3441 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3442 goto nla_put_failure;
3444 nlmsg_end(skb, nlh);
3449 nlmsg_cancel(skb, nlh);
3454 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3457 struct net *net = sock_net(skb->sk);
3459 if (cb->args[0] == nr_cpu_ids)
3462 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3463 const struct ip_conntrack_stat *st;
3465 if (!cpu_possible(cpu))
3468 st = per_cpu_ptr(net->ct.stat, cpu);
3469 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3479 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3480 struct sk_buff *skb,
3481 const struct nlmsghdr *nlh,
3482 const struct nlattr * const cda[],
3483 struct netlink_ext_ack *extack)
3485 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3486 struct netlink_dump_control c = {
3487 .dump = ctnetlink_exp_stat_cpu_dump,
3489 return netlink_dump_start(ctnl, skb, nlh, &c);
3495 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3496 static struct nf_ct_event_notifier ctnl_notifier = {
3497 .fcn = ctnetlink_conntrack_event,
3500 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3501 .fcn = ctnetlink_expect_event,
3505 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3506 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3507 .attr_count = CTA_MAX,
3508 .policy = ct_nla_policy },
3509 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3510 .attr_count = CTA_MAX,
3511 .policy = ct_nla_policy },
3512 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3513 .attr_count = CTA_MAX,
3514 .policy = ct_nla_policy },
3515 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3516 .attr_count = CTA_MAX,
3517 .policy = ct_nla_policy },
3518 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3519 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3520 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3521 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3524 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3525 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3526 .attr_count = CTA_EXPECT_MAX,
3527 .policy = exp_nla_policy },
3528 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3529 .attr_count = CTA_EXPECT_MAX,
3530 .policy = exp_nla_policy },
3531 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3532 .attr_count = CTA_EXPECT_MAX,
3533 .policy = exp_nla_policy },
3534 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3537 static const struct nfnetlink_subsystem ctnl_subsys = {
3538 .name = "conntrack",
3539 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3540 .cb_count = IPCTNL_MSG_MAX,
3544 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3545 .name = "conntrack_expect",
3546 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3547 .cb_count = IPCTNL_MSG_EXP_MAX,
3551 MODULE_ALIAS("ip_conntrack_netlink");
3552 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3553 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3555 static int __net_init ctnetlink_net_init(struct net *net)
3557 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3560 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3562 pr_err("ctnetlink_init: cannot register notifier.\n");
3566 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3568 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3569 goto err_unreg_notifier;
3574 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3576 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3582 static void ctnetlink_net_exit(struct net *net)
3584 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3585 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3586 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3590 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3594 list_for_each_entry(net, net_exit_list, exit_list)
3595 ctnetlink_net_exit(net);
3598 static struct pernet_operations ctnetlink_net_ops = {
3599 .init = ctnetlink_net_init,
3600 .exit_batch = ctnetlink_net_exit_batch,
3603 static int __init ctnetlink_init(void)
3607 ret = nfnetlink_subsys_register(&ctnl_subsys);
3609 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3613 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3615 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3616 goto err_unreg_subsys;
3619 ret = register_pernet_subsys(&ctnetlink_net_ops);
3621 pr_err("ctnetlink_init: cannot register pernet operations\n");
3622 goto err_unreg_exp_subsys;
3624 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3625 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3626 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3630 err_unreg_exp_subsys:
3631 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3633 nfnetlink_subsys_unregister(&ctnl_subsys);
3638 static void __exit ctnetlink_exit(void)
3640 unregister_pernet_subsys(&ctnetlink_net_ops);
3641 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3642 nfnetlink_subsys_unregister(&ctnl_subsys);
3643 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3644 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3649 module_init(ctnetlink_init);
3650 module_exit(ctnetlink_exit);