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>
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_seqadj.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_tuple.h>
43 #include <net/netfilter/nf_conntrack_acct.h>
44 #include <net/netfilter/nf_conntrack_zones.h>
45 #include <net/netfilter/nf_conntrack_timestamp.h>
46 #include <net/netfilter/nf_conntrack_labels.h>
47 #include <net/netfilter/nf_conntrack_synproxy.h>
48 #ifdef CONFIG_NF_NAT_NEEDED
49 #include <net/netfilter/nf_nat_core.h>
50 #include <net/netfilter/nf_nat_l4proto.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 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
490 goto nla_put_failure;
497 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
499 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
500 goto nla_put_failure;
508 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
511 const struct nf_conntrack_zone *zone;
512 struct nlmsghdr *nlh;
513 struct nfgenmsg *nfmsg;
514 struct nlattr *nest_parms;
515 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
517 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
518 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
522 nfmsg = nlmsg_data(nlh);
523 nfmsg->nfgen_family = nf_ct_l3num(ct);
524 nfmsg->version = NFNETLINK_V0;
527 zone = nf_ct_zone(ct);
529 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
531 goto nla_put_failure;
532 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
533 goto nla_put_failure;
534 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
535 NF_CT_ZONE_DIR_ORIG) < 0)
536 goto nla_put_failure;
537 nla_nest_end(skb, nest_parms);
539 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
541 goto nla_put_failure;
542 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
543 goto nla_put_failure;
544 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
545 NF_CT_ZONE_DIR_REPL) < 0)
546 goto nla_put_failure;
547 nla_nest_end(skb, nest_parms);
549 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
550 NF_CT_DEFAULT_ZONE_DIR) < 0)
551 goto nla_put_failure;
553 if (ctnetlink_dump_status(skb, ct) < 0 ||
554 ctnetlink_dump_timeout(skb, ct) < 0 ||
555 ctnetlink_dump_acct(skb, ct, type) < 0 ||
556 ctnetlink_dump_timestamp(skb, ct) < 0 ||
557 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
558 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
559 ctnetlink_dump_mark(skb, ct) < 0 ||
560 ctnetlink_dump_secctx(skb, ct) < 0 ||
561 ctnetlink_dump_labels(skb, ct) < 0 ||
562 ctnetlink_dump_id(skb, ct) < 0 ||
563 ctnetlink_dump_use(skb, ct) < 0 ||
564 ctnetlink_dump_master(skb, ct) < 0 ||
565 ctnetlink_dump_ct_seq_adj(skb, ct) < 0 ||
566 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
567 goto nla_put_failure;
574 nlmsg_cancel(skb, nlh);
578 static const struct nla_policy cta_ip_nla_policy[CTA_IP_MAX + 1] = {
579 [CTA_IP_V4_SRC] = { .type = NLA_U32 },
580 [CTA_IP_V4_DST] = { .type = NLA_U32 },
581 [CTA_IP_V6_SRC] = { .len = sizeof(__be32) * 4 },
582 [CTA_IP_V6_DST] = { .len = sizeof(__be32) * 4 },
585 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
586 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
588 const struct nf_conntrack_l4proto *l4proto;
589 size_t len, len4 = 0;
591 len = nla_policy_len(cta_ip_nla_policy, CTA_IP_MAX + 1);
592 len *= 3u; /* ORIG, REPLY, MASTER */
594 l4proto = __nf_ct_l4proto_find(nf_ct_protonum(ct));
595 len += l4proto->nlattr_size;
596 if (l4proto->nlattr_tuple_size) {
597 len4 = l4proto->nlattr_tuple_size();
598 len4 *= 3u; /* ORIG, REPLY, MASTER */
605 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
607 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
609 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
610 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
611 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
615 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
617 #ifdef CONFIG_NF_CONNTRACK_SECMARK
620 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
624 return nla_total_size(0) /* CTA_SECCTX */
625 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
631 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
633 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
634 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
636 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
642 #ifdef CONFIG_NF_CONNTRACK_EVENTS
643 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
645 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
646 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
647 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
648 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
649 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
650 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
651 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
652 + ctnetlink_acct_size(ct)
653 + ctnetlink_timestamp_size(ct)
654 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
655 + nla_total_size(0) /* CTA_PROTOINFO */
656 + nla_total_size(0) /* CTA_HELP */
657 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
658 + ctnetlink_secctx_size(ct)
659 #ifdef CONFIG_NF_NAT_NEEDED
660 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
661 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
663 #ifdef CONFIG_NF_CONNTRACK_MARK
664 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
666 #ifdef CONFIG_NF_CONNTRACK_ZONES
667 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
669 + ctnetlink_proto_size(ct)
670 + ctnetlink_label_size(ct)
675 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
677 const struct nf_conntrack_zone *zone;
679 struct nlmsghdr *nlh;
680 struct nfgenmsg *nfmsg;
681 struct nlattr *nest_parms;
682 struct nf_conn *ct = item->ct;
685 unsigned int flags = 0, group;
688 if (events & (1 << IPCT_DESTROY)) {
689 type = IPCTNL_MSG_CT_DELETE;
690 group = NFNLGRP_CONNTRACK_DESTROY;
691 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
692 type = IPCTNL_MSG_CT_NEW;
693 flags = NLM_F_CREATE|NLM_F_EXCL;
694 group = NFNLGRP_CONNTRACK_NEW;
696 type = IPCTNL_MSG_CT_NEW;
697 group = NFNLGRP_CONNTRACK_UPDATE;
702 if (!item->report && !nfnetlink_has_listeners(net, group))
705 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
709 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
710 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
714 nfmsg = nlmsg_data(nlh);
715 nfmsg->nfgen_family = nf_ct_l3num(ct);
716 nfmsg->version = NFNETLINK_V0;
719 zone = nf_ct_zone(ct);
721 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
723 goto nla_put_failure;
724 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
725 goto nla_put_failure;
726 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
727 NF_CT_ZONE_DIR_ORIG) < 0)
728 goto nla_put_failure;
729 nla_nest_end(skb, nest_parms);
731 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
733 goto nla_put_failure;
734 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
735 goto nla_put_failure;
736 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
737 NF_CT_ZONE_DIR_REPL) < 0)
738 goto nla_put_failure;
739 nla_nest_end(skb, nest_parms);
741 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
742 NF_CT_DEFAULT_ZONE_DIR) < 0)
743 goto nla_put_failure;
745 if (ctnetlink_dump_id(skb, ct) < 0)
746 goto nla_put_failure;
748 if (ctnetlink_dump_status(skb, ct) < 0)
749 goto nla_put_failure;
751 if (events & (1 << IPCT_DESTROY)) {
752 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
753 ctnetlink_dump_timestamp(skb, ct) < 0)
754 goto nla_put_failure;
756 if (ctnetlink_dump_timeout(skb, ct) < 0)
757 goto nla_put_failure;
759 if (events & (1 << IPCT_PROTOINFO)
760 && ctnetlink_dump_protoinfo(skb, ct) < 0)
761 goto nla_put_failure;
763 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
764 && ctnetlink_dump_helpinfo(skb, ct) < 0)
765 goto nla_put_failure;
767 #ifdef CONFIG_NF_CONNTRACK_SECMARK
768 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
769 && ctnetlink_dump_secctx(skb, ct) < 0)
770 goto nla_put_failure;
772 if (events & (1 << IPCT_LABEL) &&
773 ctnetlink_dump_labels(skb, ct) < 0)
774 goto nla_put_failure;
776 if (events & (1 << IPCT_RELATED) &&
777 ctnetlink_dump_master(skb, ct) < 0)
778 goto nla_put_failure;
780 if (events & (1 << IPCT_SEQADJ) &&
781 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
782 goto nla_put_failure;
784 if (events & (1 << IPCT_SYNPROXY) &&
785 ctnetlink_dump_ct_synproxy(skb, ct) < 0)
786 goto nla_put_failure;
789 #ifdef CONFIG_NF_CONNTRACK_MARK
790 if ((events & (1 << IPCT_MARK) || ct->mark)
791 && ctnetlink_dump_mark(skb, ct) < 0)
792 goto nla_put_failure;
795 err = nfnetlink_send(skb, net, item->portid, group, item->report,
797 if (err == -ENOBUFS || err == -EAGAIN)
803 nlmsg_cancel(skb, nlh);
807 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
812 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
814 static int ctnetlink_done(struct netlink_callback *cb)
817 nf_ct_put((struct nf_conn *)cb->args[1]);
822 struct ctnetlink_filter {
830 static struct ctnetlink_filter *
831 ctnetlink_alloc_filter(const struct nlattr * const cda[], u8 family)
833 struct ctnetlink_filter *filter;
835 #ifndef CONFIG_NF_CONNTRACK_MARK
836 if (cda[CTA_MARK] && cda[CTA_MARK_MASK])
837 return ERR_PTR(-EOPNOTSUPP);
840 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
842 return ERR_PTR(-ENOMEM);
844 filter->family = family;
846 #ifdef CONFIG_NF_CONNTRACK_MARK
847 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
848 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
849 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
855 static int ctnetlink_start(struct netlink_callback *cb)
857 const struct nlattr * const *cda = cb->data;
858 struct ctnetlink_filter *filter = NULL;
859 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
860 u8 family = nfmsg->nfgen_family;
862 if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
863 filter = ctnetlink_alloc_filter(cda, family);
865 return PTR_ERR(filter);
872 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
874 struct ctnetlink_filter *filter = data;
879 /* Match entries of a given L3 protocol number.
880 * If it is not specified, ie. l3proto == 0,
881 * then match everything.
883 if (filter->family && nf_ct_l3num(ct) != filter->family)
886 #ifdef CONFIG_NF_CONNTRACK_MARK
887 if ((ct->mark & filter->mark.mask) != filter->mark.val)
899 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
901 struct net *net = sock_net(skb->sk);
902 struct nf_conn *ct, *last;
903 struct nf_conntrack_tuple_hash *h;
904 struct hlist_nulls_node *n;
905 struct nf_conn *nf_ct_evict[8];
909 last = (struct nf_conn *)cb->args[1];
913 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
917 if (nf_ct_should_gc(nf_ct_evict[i]))
918 nf_ct_kill(nf_ct_evict[i]);
919 nf_ct_put(nf_ct_evict[i]);
922 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
923 nf_conntrack_lock(lockp);
924 if (cb->args[0] >= nf_conntrack_htable_size) {
928 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
930 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
932 ct = nf_ct_tuplehash_to_ctrack(h);
933 if (nf_ct_is_expired(ct)) {
934 if (i < ARRAY_SIZE(nf_ct_evict) &&
935 atomic_inc_not_zero(&ct->ct_general.use))
936 nf_ct_evict[i++] = ct;
940 if (!net_eq(net, nf_ct_net(ct)))
948 if (!ctnetlink_filter_match(ct, cb->data))
953 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
955 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
959 nf_conntrack_get(&ct->ct_general);
960 cb->args[1] = (unsigned long)ct;
974 /* nf ct hash resize happened, now clear the leftover. */
975 if ((struct nf_conn *)cb->args[1] == last)
983 if (nf_ct_should_gc(nf_ct_evict[i]))
984 nf_ct_kill(nf_ct_evict[i]);
985 nf_ct_put(nf_ct_evict[i]);
991 static int ipv4_nlattr_to_tuple(struct nlattr *tb[],
992 struct nf_conntrack_tuple *t)
994 if (!tb[CTA_IP_V4_SRC] || !tb[CTA_IP_V4_DST])
997 t->src.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_SRC]);
998 t->dst.u3.ip = nla_get_in_addr(tb[CTA_IP_V4_DST]);
1003 static int ipv6_nlattr_to_tuple(struct nlattr *tb[],
1004 struct nf_conntrack_tuple *t)
1006 if (!tb[CTA_IP_V6_SRC] || !tb[CTA_IP_V6_DST])
1009 t->src.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_SRC]);
1010 t->dst.u3.in6 = nla_get_in6_addr(tb[CTA_IP_V6_DST]);
1015 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
1016 struct nf_conntrack_tuple *tuple)
1018 struct nlattr *tb[CTA_IP_MAX+1];
1021 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
1025 ret = nla_validate_nested(attr, CTA_IP_MAX,
1026 cta_ip_nla_policy, NULL);
1030 switch (tuple->src.l3num) {
1032 ret = ipv4_nlattr_to_tuple(tb, tuple);
1035 ret = ipv6_nlattr_to_tuple(tb, tuple);
1042 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
1043 [CTA_PROTO_NUM] = { .type = NLA_U8 },
1046 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
1047 struct nf_conntrack_tuple *tuple)
1049 const struct nf_conntrack_l4proto *l4proto;
1050 struct nlattr *tb[CTA_PROTO_MAX+1];
1053 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
1058 if (!tb[CTA_PROTO_NUM])
1060 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
1063 l4proto = __nf_ct_l4proto_find(tuple->dst.protonum);
1065 if (likely(l4proto->nlattr_to_tuple)) {
1066 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
1067 l4proto->nla_policy, NULL);
1069 ret = l4proto->nlattr_to_tuple(tb, tuple);
1078 ctnetlink_parse_zone(const struct nlattr *attr,
1079 struct nf_conntrack_zone *zone)
1081 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
1082 NF_CT_DEFAULT_ZONE_DIR, 0);
1083 #ifdef CONFIG_NF_CONNTRACK_ZONES
1085 zone->id = ntohs(nla_get_be16(attr));
1094 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1095 struct nf_conntrack_zone *zone)
1099 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1102 ret = ctnetlink_parse_zone(attr, zone);
1106 if (type == CTA_TUPLE_REPLY)
1107 zone->dir = NF_CT_ZONE_DIR_REPL;
1109 zone->dir = NF_CT_ZONE_DIR_ORIG;
1114 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1115 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1116 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1117 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1121 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1122 struct nf_conntrack_tuple *tuple, u32 type,
1123 u_int8_t l3num, struct nf_conntrack_zone *zone)
1125 struct nlattr *tb[CTA_TUPLE_MAX+1];
1128 memset(tuple, 0, sizeof(*tuple));
1130 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1135 if (!tb[CTA_TUPLE_IP])
1138 tuple->src.l3num = l3num;
1140 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1144 if (!tb[CTA_TUPLE_PROTO])
1147 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1151 if (tb[CTA_TUPLE_ZONE]) {
1155 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1161 /* orig and expect tuples get DIR_ORIGINAL */
1162 if (type == CTA_TUPLE_REPLY)
1163 tuple->dst.dir = IP_CT_DIR_REPLY;
1165 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1170 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1171 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1172 .len = NF_CT_HELPER_NAME_LEN - 1 },
1175 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1176 struct nlattr **helpinfo)
1179 struct nlattr *tb[CTA_HELP_MAX+1];
1181 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1185 if (!tb[CTA_HELP_NAME])
1188 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1190 if (tb[CTA_HELP_INFO])
1191 *helpinfo = tb[CTA_HELP_INFO];
1196 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1197 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1198 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1199 [CTA_STATUS] = { .type = NLA_U32 },
1200 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1201 [CTA_HELP] = { .type = NLA_NESTED },
1202 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1203 [CTA_TIMEOUT] = { .type = NLA_U32 },
1204 [CTA_MARK] = { .type = NLA_U32 },
1205 [CTA_ID] = { .type = NLA_U32 },
1206 [CTA_NAT_DST] = { .type = NLA_NESTED },
1207 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1208 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1209 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1210 [CTA_ZONE] = { .type = NLA_U16 },
1211 [CTA_MARK_MASK] = { .type = NLA_U32 },
1212 [CTA_LABELS] = { .type = NLA_BINARY,
1213 .len = NF_CT_LABELS_MAX_SIZE },
1214 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1215 .len = NF_CT_LABELS_MAX_SIZE },
1218 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1220 if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
1223 return ctnetlink_filter_match(ct, data);
1226 static int ctnetlink_flush_conntrack(struct net *net,
1227 const struct nlattr * const cda[],
1228 u32 portid, int report, u8 family)
1230 struct ctnetlink_filter *filter = NULL;
1232 if (family || (cda[CTA_MARK] && cda[CTA_MARK_MASK])) {
1233 filter = ctnetlink_alloc_filter(cda, family);
1235 return PTR_ERR(filter);
1238 nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
1245 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1246 struct sk_buff *skb,
1247 const struct nlmsghdr *nlh,
1248 const struct nlattr * const cda[],
1249 struct netlink_ext_ack *extack)
1251 struct nf_conntrack_tuple_hash *h;
1252 struct nf_conntrack_tuple tuple;
1254 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1255 u_int8_t u3 = nfmsg->nfgen_family;
1256 struct nf_conntrack_zone zone;
1259 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1263 if (cda[CTA_TUPLE_ORIG])
1264 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1266 else if (cda[CTA_TUPLE_REPLY])
1267 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1270 return ctnetlink_flush_conntrack(net, cda,
1271 NETLINK_CB(skb).portid,
1272 nlmsg_report(nlh), u3);
1278 h = nf_conntrack_find_get(net, &zone, &tuple);
1282 ct = nf_ct_tuplehash_to_ctrack(h);
1284 if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) {
1290 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1291 if (id != (u32)(unsigned long)ct) {
1297 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1303 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1304 struct sk_buff *skb,
1305 const struct nlmsghdr *nlh,
1306 const struct nlattr * const cda[],
1307 struct netlink_ext_ack *extack)
1309 struct nf_conntrack_tuple_hash *h;
1310 struct nf_conntrack_tuple tuple;
1312 struct sk_buff *skb2 = NULL;
1313 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1314 u_int8_t u3 = nfmsg->nfgen_family;
1315 struct nf_conntrack_zone zone;
1318 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1319 struct netlink_dump_control c = {
1320 .start = ctnetlink_start,
1321 .dump = ctnetlink_dump_table,
1322 .done = ctnetlink_done,
1323 .data = (void *)cda,
1326 return netlink_dump_start(ctnl, skb, nlh, &c);
1329 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1333 if (cda[CTA_TUPLE_ORIG])
1334 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1336 else if (cda[CTA_TUPLE_REPLY])
1337 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1345 h = nf_conntrack_find_get(net, &zone, &tuple);
1349 ct = nf_ct_tuplehash_to_ctrack(h);
1352 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1359 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1360 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1366 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1375 /* this avoids a loop in nfnetlink. */
1376 return err == -EAGAIN ? -ENOBUFS : err;
1379 static int ctnetlink_done_list(struct netlink_callback *cb)
1382 nf_ct_put((struct nf_conn *)cb->args[1]);
1387 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1389 struct nf_conn *ct, *last;
1390 struct nf_conntrack_tuple_hash *h;
1391 struct hlist_nulls_node *n;
1392 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1393 u_int8_t l3proto = nfmsg->nfgen_family;
1396 struct hlist_nulls_head *list;
1397 struct net *net = sock_net(skb->sk);
1402 last = (struct nf_conn *)cb->args[1];
1404 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1405 struct ct_pcpu *pcpu;
1407 if (!cpu_possible(cpu))
1410 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1411 spin_lock_bh(&pcpu->lock);
1412 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1414 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1415 ct = nf_ct_tuplehash_to_ctrack(h);
1416 if (l3proto && nf_ct_l3num(ct) != l3proto)
1424 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1426 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1430 if (!atomic_inc_not_zero(&ct->ct_general.use))
1433 cb->args[1] = (unsigned long)ct;
1434 spin_unlock_bh(&pcpu->lock);
1442 spin_unlock_bh(&pcpu->lock);
1453 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1455 return ctnetlink_dump_list(skb, cb, true);
1458 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1459 struct sk_buff *skb,
1460 const struct nlmsghdr *nlh,
1461 const struct nlattr * const cda[],
1462 struct netlink_ext_ack *extack)
1464 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1465 struct netlink_dump_control c = {
1466 .dump = ctnetlink_dump_dying,
1467 .done = ctnetlink_done_list,
1469 return netlink_dump_start(ctnl, skb, nlh, &c);
1476 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1478 return ctnetlink_dump_list(skb, cb, false);
1481 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1482 struct sk_buff *skb,
1483 const struct nlmsghdr *nlh,
1484 const struct nlattr * const cda[],
1485 struct netlink_ext_ack *extack)
1487 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1488 struct netlink_dump_control c = {
1489 .dump = ctnetlink_dump_unconfirmed,
1490 .done = ctnetlink_done_list,
1492 return netlink_dump_start(ctnl, skb, nlh, &c);
1498 #ifdef CONFIG_NF_NAT_NEEDED
1500 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1501 enum nf_nat_manip_type manip,
1502 const struct nlattr *attr)
1504 struct nf_nat_hook *nat_hook;
1507 nat_hook = rcu_dereference(nf_nat_hook);
1509 #ifdef CONFIG_MODULES
1511 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1512 if (request_module("nf-nat") < 0) {
1513 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1517 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1519 nat_hook = rcu_dereference(nf_nat_hook);
1526 err = nat_hook->parse_nat_setup(ct, manip, attr);
1527 if (err == -EAGAIN) {
1528 #ifdef CONFIG_MODULES
1530 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1531 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1532 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1536 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1547 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1552 /* Ignore these unchangable bits */
1553 on &= ~IPS_UNCHANGEABLE_MASK;
1554 off &= ~IPS_UNCHANGEABLE_MASK;
1556 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1557 if (on & (1 << bit))
1558 set_bit(bit, &ct->status);
1559 else if (off & (1 << bit))
1560 clear_bit(bit, &ct->status);
1565 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1568 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1569 d = ct->status ^ status;
1571 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1575 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1576 /* SEEN_REPLY bit can only be set */
1579 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1580 /* ASSURED bit can only be set */
1583 __ctnetlink_change_status(ct, status, 0);
1588 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1590 #ifdef CONFIG_NF_NAT_NEEDED
1593 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1596 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1601 return ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1604 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1610 static int ctnetlink_change_helper(struct nf_conn *ct,
1611 const struct nlattr * const cda[])
1613 struct nf_conntrack_helper *helper;
1614 struct nf_conn_help *help = nfct_help(ct);
1615 char *helpname = NULL;
1616 struct nlattr *helpinfo = NULL;
1619 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1623 /* don't change helper of sibling connections */
1625 /* If we try to change the helper to the same thing twice,
1626 * treat the second attempt as a no-op instead of returning
1632 helper = rcu_dereference(help->helper);
1633 if (helper && !strcmp(helper->name, helpname))
1641 if (!strcmp(helpname, "")) {
1642 if (help && help->helper) {
1643 /* we had a helper before ... */
1644 nf_ct_remove_expectations(ct);
1645 RCU_INIT_POINTER(help->helper, NULL);
1652 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1653 nf_ct_protonum(ct));
1654 if (helper == NULL) {
1660 if (help->helper == helper) {
1661 /* update private helper data if allowed. */
1662 if (helper->from_nlattr)
1663 helper->from_nlattr(helpinfo, ct);
1668 /* we cannot set a helper for an existing conntrack */
1676 static int ctnetlink_change_timeout(struct nf_conn *ct,
1677 const struct nlattr * const cda[])
1679 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1681 if (timeout > INT_MAX)
1683 ct->timeout = nfct_time_stamp + (u32)timeout;
1685 if (test_bit(IPS_DYING_BIT, &ct->status))
1691 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1692 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1693 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1694 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1697 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1698 const struct nlattr * const cda[])
1700 const struct nlattr *attr = cda[CTA_PROTOINFO];
1701 const struct nf_conntrack_l4proto *l4proto;
1702 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1705 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1711 l4proto = __nf_ct_l4proto_find(nf_ct_protonum(ct));
1712 if (l4proto->from_nlattr)
1713 err = l4proto->from_nlattr(tb, ct);
1719 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1720 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1721 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1722 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1725 static int change_seq_adj(struct nf_ct_seqadj *seq,
1726 const struct nlattr * const attr)
1729 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1731 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1735 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1738 seq->correction_pos =
1739 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1741 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1744 seq->offset_before =
1745 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1747 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1751 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1757 ctnetlink_change_seq_adj(struct nf_conn *ct,
1758 const struct nlattr * const cda[])
1760 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1766 spin_lock_bh(&ct->lock);
1767 if (cda[CTA_SEQ_ADJ_ORIG]) {
1768 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1769 cda[CTA_SEQ_ADJ_ORIG]);
1773 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1776 if (cda[CTA_SEQ_ADJ_REPLY]) {
1777 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1778 cda[CTA_SEQ_ADJ_REPLY]);
1782 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1785 spin_unlock_bh(&ct->lock);
1788 spin_unlock_bh(&ct->lock);
1792 static const struct nla_policy synproxy_policy[CTA_SYNPROXY_MAX + 1] = {
1793 [CTA_SYNPROXY_ISN] = { .type = NLA_U32 },
1794 [CTA_SYNPROXY_ITS] = { .type = NLA_U32 },
1795 [CTA_SYNPROXY_TSOFF] = { .type = NLA_U32 },
1798 static int ctnetlink_change_synproxy(struct nf_conn *ct,
1799 const struct nlattr * const cda[])
1801 struct nf_conn_synproxy *synproxy = nfct_synproxy(ct);
1802 struct nlattr *tb[CTA_SYNPROXY_MAX + 1];
1808 err = nla_parse_nested(tb, CTA_SYNPROXY_MAX, cda[CTA_SYNPROXY],
1809 synproxy_policy, NULL);
1813 if (!tb[CTA_SYNPROXY_ISN] ||
1814 !tb[CTA_SYNPROXY_ITS] ||
1815 !tb[CTA_SYNPROXY_TSOFF])
1818 synproxy->isn = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ISN]));
1819 synproxy->its = ntohl(nla_get_be32(tb[CTA_SYNPROXY_ITS]));
1820 synproxy->tsoff = ntohl(nla_get_be32(tb[CTA_SYNPROXY_TSOFF]));
1826 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1828 #ifdef CONFIG_NF_CONNTRACK_LABELS
1829 size_t len = nla_len(cda[CTA_LABELS]);
1830 const void *mask = cda[CTA_LABELS_MASK];
1832 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1836 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1837 nla_len(cda[CTA_LABELS_MASK]) != len)
1839 mask = nla_data(cda[CTA_LABELS_MASK]);
1844 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1851 ctnetlink_change_conntrack(struct nf_conn *ct,
1852 const struct nlattr * const cda[])
1856 /* only allow NAT changes and master assignation for new conntracks */
1857 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1860 if (cda[CTA_HELP]) {
1861 err = ctnetlink_change_helper(ct, cda);
1866 if (cda[CTA_TIMEOUT]) {
1867 err = ctnetlink_change_timeout(ct, cda);
1872 if (cda[CTA_STATUS]) {
1873 err = ctnetlink_change_status(ct, cda);
1878 if (cda[CTA_PROTOINFO]) {
1879 err = ctnetlink_change_protoinfo(ct, cda);
1884 #if defined(CONFIG_NF_CONNTRACK_MARK)
1886 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1889 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1890 err = ctnetlink_change_seq_adj(ct, cda);
1895 if (cda[CTA_SYNPROXY]) {
1896 err = ctnetlink_change_synproxy(ct, cda);
1901 if (cda[CTA_LABELS]) {
1902 err = ctnetlink_attach_labels(ct, cda);
1910 static struct nf_conn *
1911 ctnetlink_create_conntrack(struct net *net,
1912 const struct nf_conntrack_zone *zone,
1913 const struct nlattr * const cda[],
1914 struct nf_conntrack_tuple *otuple,
1915 struct nf_conntrack_tuple *rtuple,
1920 struct nf_conntrack_helper *helper;
1921 struct nf_conn_tstamp *tstamp;
1924 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1926 return ERR_PTR(-ENOMEM);
1928 if (!cda[CTA_TIMEOUT])
1931 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1932 if (timeout > INT_MAX)
1934 ct->timeout = (u32)timeout + nfct_time_stamp;
1937 if (cda[CTA_HELP]) {
1938 char *helpname = NULL;
1939 struct nlattr *helpinfo = NULL;
1941 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1945 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1946 nf_ct_protonum(ct));
1947 if (helper == NULL) {
1949 #ifdef CONFIG_MODULES
1950 if (request_module("nfct-helper-%s", helpname) < 0) {
1956 helper = __nf_conntrack_helper_find(helpname,
1958 nf_ct_protonum(ct));
1968 struct nf_conn_help *help;
1970 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1975 /* set private helper data if allowed. */
1976 if (helper->from_nlattr)
1977 helper->from_nlattr(helpinfo, ct);
1979 /* not in hash table yet so not strictly necessary */
1980 RCU_INIT_POINTER(help->helper, helper);
1983 /* try an implicit helper assignation */
1984 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1989 err = ctnetlink_setup_nat(ct, cda);
1993 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1994 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1995 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1996 nf_ct_labels_ext_add(ct);
1997 nfct_seqadj_ext_add(ct);
1998 nfct_synproxy_ext_add(ct);
2000 /* we must add conntrack extensions before confirmation. */
2001 ct->status |= IPS_CONFIRMED;
2003 if (cda[CTA_STATUS]) {
2004 err = ctnetlink_change_status(ct, cda);
2009 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
2010 err = ctnetlink_change_seq_adj(ct, cda);
2015 memset(&ct->proto, 0, sizeof(ct->proto));
2016 if (cda[CTA_PROTOINFO]) {
2017 err = ctnetlink_change_protoinfo(ct, cda);
2022 if (cda[CTA_SYNPROXY]) {
2023 err = ctnetlink_change_synproxy(ct, cda);
2028 #if defined(CONFIG_NF_CONNTRACK_MARK)
2030 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2033 /* setup master conntrack: this is a confirmed expectation */
2034 if (cda[CTA_TUPLE_MASTER]) {
2035 struct nf_conntrack_tuple master;
2036 struct nf_conntrack_tuple_hash *master_h;
2037 struct nf_conn *master_ct;
2039 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
2044 master_h = nf_conntrack_find_get(net, zone, &master);
2045 if (master_h == NULL) {
2049 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
2050 __set_bit(IPS_EXPECTED_BIT, &ct->status);
2051 ct->master = master_ct;
2053 tstamp = nf_conn_tstamp_find(ct);
2055 tstamp->start = ktime_get_real_ns();
2057 err = nf_conntrack_hash_check_insert(ct);
2068 nf_conntrack_free(ct);
2069 return ERR_PTR(err);
2072 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
2073 struct sk_buff *skb,
2074 const struct nlmsghdr *nlh,
2075 const struct nlattr * const cda[],
2076 struct netlink_ext_ack *extack)
2078 struct nf_conntrack_tuple otuple, rtuple;
2079 struct nf_conntrack_tuple_hash *h = NULL;
2080 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2082 u_int8_t u3 = nfmsg->nfgen_family;
2083 struct nf_conntrack_zone zone;
2086 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
2090 if (cda[CTA_TUPLE_ORIG]) {
2091 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
2097 if (cda[CTA_TUPLE_REPLY]) {
2098 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
2104 if (cda[CTA_TUPLE_ORIG])
2105 h = nf_conntrack_find_get(net, &zone, &otuple);
2106 else if (cda[CTA_TUPLE_REPLY])
2107 h = nf_conntrack_find_get(net, &zone, &rtuple);
2111 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2112 enum ip_conntrack_events events;
2114 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
2116 if (otuple.dst.protonum != rtuple.dst.protonum)
2119 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
2125 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
2126 events = 1 << IPCT_RELATED;
2128 events = 1 << IPCT_NEW;
2130 if (cda[CTA_LABELS] &&
2131 ctnetlink_attach_labels(ct, cda) == 0)
2132 events |= (1 << IPCT_LABEL);
2134 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2135 (1 << IPCT_ASSURED) |
2136 (1 << IPCT_HELPER) |
2137 (1 << IPCT_PROTOINFO) |
2138 (1 << IPCT_SEQADJ) |
2140 (1 << IPCT_SYNPROXY) |
2142 ct, NETLINK_CB(skb).portid,
2149 /* implicit 'else' */
2152 ct = nf_ct_tuplehash_to_ctrack(h);
2153 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2154 err = ctnetlink_change_conntrack(ct, cda);
2156 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2157 (1 << IPCT_ASSURED) |
2158 (1 << IPCT_HELPER) |
2160 (1 << IPCT_PROTOINFO) |
2161 (1 << IPCT_SEQADJ) |
2163 (1 << IPCT_SYNPROXY),
2164 ct, NETLINK_CB(skb).portid,
2174 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2175 __u16 cpu, const struct ip_conntrack_stat *st)
2177 struct nlmsghdr *nlh;
2178 struct nfgenmsg *nfmsg;
2179 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2181 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2182 IPCTNL_MSG_CT_GET_STATS_CPU);
2183 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2187 nfmsg = nlmsg_data(nlh);
2188 nfmsg->nfgen_family = AF_UNSPEC;
2189 nfmsg->version = NFNETLINK_V0;
2190 nfmsg->res_id = htons(cpu);
2192 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2193 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2194 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2195 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2196 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2197 htonl(st->insert_failed)) ||
2198 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2199 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2200 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2201 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2202 htonl(st->search_restart)))
2203 goto nla_put_failure;
2205 nlmsg_end(skb, nlh);
2210 nlmsg_cancel(skb, nlh);
2215 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2218 struct net *net = sock_net(skb->sk);
2220 if (cb->args[0] == nr_cpu_ids)
2223 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2224 const struct ip_conntrack_stat *st;
2226 if (!cpu_possible(cpu))
2229 st = per_cpu_ptr(net->ct.stat, cpu);
2230 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2231 NETLINK_CB(cb->skb).portid,
2241 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2242 struct sk_buff *skb,
2243 const struct nlmsghdr *nlh,
2244 const struct nlattr * const cda[],
2245 struct netlink_ext_ack *extack)
2247 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2248 struct netlink_dump_control c = {
2249 .dump = ctnetlink_ct_stat_cpu_dump,
2251 return netlink_dump_start(ctnl, skb, nlh, &c);
2258 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2261 struct nlmsghdr *nlh;
2262 struct nfgenmsg *nfmsg;
2263 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2264 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2266 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2267 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2271 nfmsg = nlmsg_data(nlh);
2272 nfmsg->nfgen_family = AF_UNSPEC;
2273 nfmsg->version = NFNETLINK_V0;
2276 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2277 goto nla_put_failure;
2279 if (nla_put_be32(skb, CTA_STATS_GLOBAL_MAX_ENTRIES, htonl(nf_conntrack_max)))
2280 goto nla_put_failure;
2282 nlmsg_end(skb, nlh);
2287 nlmsg_cancel(skb, nlh);
2291 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2292 struct sk_buff *skb, const struct nlmsghdr *nlh,
2293 const struct nlattr * const cda[],
2294 struct netlink_ext_ack *extack)
2296 struct sk_buff *skb2;
2299 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2303 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2305 NFNL_MSG_TYPE(nlh->nlmsg_type),
2310 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2319 /* this avoids a loop in nfnetlink. */
2320 return err == -EAGAIN ? -ENOBUFS : err;
2323 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2324 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2325 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2326 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2327 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2328 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2329 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2330 .len = NF_CT_HELPER_NAME_LEN - 1 },
2331 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2332 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2333 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2334 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2335 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2338 static struct nf_conntrack_expect *
2339 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2340 struct nf_conntrack_helper *helper,
2341 struct nf_conntrack_tuple *tuple,
2342 struct nf_conntrack_tuple *mask);
2344 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2346 ctnetlink_glue_build_size(const struct nf_conn *ct)
2348 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2349 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2350 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2351 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2352 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2353 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2354 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2355 + nla_total_size(0) /* CTA_PROTOINFO */
2356 + nla_total_size(0) /* CTA_HELP */
2357 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2358 + ctnetlink_secctx_size(ct)
2359 #ifdef CONFIG_NF_NAT_NEEDED
2360 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2361 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2363 #ifdef CONFIG_NF_CONNTRACK_MARK
2364 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2366 #ifdef CONFIG_NF_CONNTRACK_ZONES
2367 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2369 + ctnetlink_proto_size(ct)
2373 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2374 enum ip_conntrack_info *ctinfo)
2376 return nf_ct_get(skb, ctinfo);
2379 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2381 const struct nf_conntrack_zone *zone;
2382 struct nlattr *nest_parms;
2384 zone = nf_ct_zone(ct);
2386 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2388 goto nla_put_failure;
2389 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2390 goto nla_put_failure;
2391 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2392 NF_CT_ZONE_DIR_ORIG) < 0)
2393 goto nla_put_failure;
2394 nla_nest_end(skb, nest_parms);
2396 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2398 goto nla_put_failure;
2399 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2400 goto nla_put_failure;
2401 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2402 NF_CT_ZONE_DIR_REPL) < 0)
2403 goto nla_put_failure;
2404 nla_nest_end(skb, nest_parms);
2406 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2407 NF_CT_DEFAULT_ZONE_DIR) < 0)
2408 goto nla_put_failure;
2410 if (ctnetlink_dump_id(skb, ct) < 0)
2411 goto nla_put_failure;
2413 if (ctnetlink_dump_status(skb, ct) < 0)
2414 goto nla_put_failure;
2416 if (ctnetlink_dump_timeout(skb, ct) < 0)
2417 goto nla_put_failure;
2419 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2420 goto nla_put_failure;
2422 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2423 goto nla_put_failure;
2425 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2426 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2427 goto nla_put_failure;
2429 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2430 goto nla_put_failure;
2432 if ((ct->status & IPS_SEQ_ADJUST) &&
2433 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2434 goto nla_put_failure;
2436 if (ctnetlink_dump_ct_synproxy(skb, ct) < 0)
2437 goto nla_put_failure;
2439 #ifdef CONFIG_NF_CONNTRACK_MARK
2440 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2441 goto nla_put_failure;
2443 if (ctnetlink_dump_labels(skb, ct) < 0)
2444 goto nla_put_failure;
2452 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2453 enum ip_conntrack_info ctinfo,
2454 u_int16_t ct_attr, u_int16_t ct_info_attr)
2456 struct nlattr *nest_parms;
2458 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2460 goto nla_put_failure;
2462 if (__ctnetlink_glue_build(skb, ct) < 0)
2463 goto nla_put_failure;
2465 nla_nest_end(skb, nest_parms);
2467 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2468 goto nla_put_failure;
2477 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2479 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2480 unsigned long d = ct->status ^ status;
2482 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2483 /* SEEN_REPLY bit can only be set */
2486 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2487 /* ASSURED bit can only be set */
2490 /* This check is less strict than ctnetlink_change_status()
2491 * because callers often flip IPS_EXPECTED bits when sending
2492 * an NFQA_CT attribute to the kernel. So ignore the
2493 * unchangeable bits but do not error out. Also user programs
2494 * are allowed to clear the bits that they are allowed to change.
2496 __ctnetlink_change_status(ct, status, ~status);
2501 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2505 if (cda[CTA_TIMEOUT]) {
2506 err = ctnetlink_change_timeout(ct, cda);
2510 if (cda[CTA_STATUS]) {
2511 err = ctnetlink_update_status(ct, cda);
2515 if (cda[CTA_HELP]) {
2516 err = ctnetlink_change_helper(ct, cda);
2520 if (cda[CTA_LABELS]) {
2521 err = ctnetlink_attach_labels(ct, cda);
2525 #if defined(CONFIG_NF_CONNTRACK_MARK)
2526 if (cda[CTA_MARK]) {
2527 u32 mask = 0, mark, newmark;
2528 if (cda[CTA_MARK_MASK])
2529 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2531 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2532 newmark = (ct->mark & mask) ^ mark;
2533 if (newmark != ct->mark)
2541 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2543 struct nlattr *cda[CTA_MAX+1];
2546 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2550 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2553 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2554 const struct nf_conn *ct,
2555 struct nf_conntrack_tuple *tuple,
2556 struct nf_conntrack_tuple *mask)
2560 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2561 nf_ct_l3num(ct), NULL);
2565 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2566 nf_ct_l3num(ct), NULL);
2570 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2571 u32 portid, u32 report)
2573 struct nlattr *cda[CTA_EXPECT_MAX+1];
2574 struct nf_conntrack_tuple tuple, mask;
2575 struct nf_conntrack_helper *helper = NULL;
2576 struct nf_conntrack_expect *exp;
2579 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2584 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2589 if (cda[CTA_EXPECT_HELP_NAME]) {
2590 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2592 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2593 nf_ct_protonum(ct));
2598 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2599 helper, &tuple, &mask);
2601 return PTR_ERR(exp);
2603 err = nf_ct_expect_related_report(exp, portid, report);
2604 nf_ct_expect_put(exp);
2608 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2609 enum ip_conntrack_info ctinfo, int diff)
2611 if (!(ct->status & IPS_NAT_MASK))
2614 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2617 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2618 .get_ct = ctnetlink_glue_get_ct,
2619 .build_size = ctnetlink_glue_build_size,
2620 .build = ctnetlink_glue_build,
2621 .parse = ctnetlink_glue_parse,
2622 .attach_expect = ctnetlink_glue_attach_expect,
2623 .seq_adjust = ctnetlink_glue_seqadj,
2625 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2627 /***********************************************************************
2629 ***********************************************************************/
2631 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2632 const struct nf_conntrack_tuple *tuple,
2635 struct nlattr *nest_parms;
2637 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2639 goto nla_put_failure;
2640 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2641 goto nla_put_failure;
2642 nla_nest_end(skb, nest_parms);
2650 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2651 const struct nf_conntrack_tuple *tuple,
2652 const struct nf_conntrack_tuple_mask *mask)
2654 const struct nf_conntrack_l4proto *l4proto;
2655 struct nf_conntrack_tuple m;
2656 struct nlattr *nest_parms;
2659 memset(&m, 0xFF, sizeof(m));
2660 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2661 m.src.u.all = mask->src.u.all;
2662 m.dst.protonum = tuple->dst.protonum;
2664 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2666 goto nla_put_failure;
2669 ret = ctnetlink_dump_tuples_ip(skb, &m);
2671 l4proto = __nf_ct_l4proto_find(tuple->dst.protonum);
2672 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2676 if (unlikely(ret < 0))
2677 goto nla_put_failure;
2679 nla_nest_end(skb, nest_parms);
2687 static const union nf_inet_addr any_addr;
2690 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2691 const struct nf_conntrack_expect *exp)
2693 struct nf_conn *master = exp->master;
2694 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2695 struct nf_conn_help *help;
2696 #ifdef CONFIG_NF_NAT_NEEDED
2697 struct nlattr *nest_parms;
2698 struct nf_conntrack_tuple nat_tuple = {};
2700 struct nf_ct_helper_expectfn *expfn;
2705 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2706 goto nla_put_failure;
2707 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2708 goto nla_put_failure;
2709 if (ctnetlink_exp_dump_tuple(skb,
2710 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2711 CTA_EXPECT_MASTER) < 0)
2712 goto nla_put_failure;
2714 #ifdef CONFIG_NF_NAT_NEEDED
2715 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2716 exp->saved_proto.all) {
2717 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2719 goto nla_put_failure;
2721 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2722 goto nla_put_failure;
2724 nat_tuple.src.l3num = nf_ct_l3num(master);
2725 nat_tuple.src.u3 = exp->saved_addr;
2726 nat_tuple.dst.protonum = nf_ct_protonum(master);
2727 nat_tuple.src.u = exp->saved_proto;
2729 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2730 CTA_EXPECT_NAT_TUPLE) < 0)
2731 goto nla_put_failure;
2732 nla_nest_end(skb, nest_parms);
2735 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2736 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2737 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2738 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2739 goto nla_put_failure;
2740 help = nfct_help(master);
2742 struct nf_conntrack_helper *helper;
2744 helper = rcu_dereference(help->helper);
2746 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2747 goto nla_put_failure;
2749 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2750 if (expfn != NULL &&
2751 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2752 goto nla_put_failure;
2761 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2762 int event, const struct nf_conntrack_expect *exp)
2764 struct nlmsghdr *nlh;
2765 struct nfgenmsg *nfmsg;
2766 unsigned int flags = portid ? NLM_F_MULTI : 0;
2768 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2769 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2773 nfmsg = nlmsg_data(nlh);
2774 nfmsg->nfgen_family = exp->tuple.src.l3num;
2775 nfmsg->version = NFNETLINK_V0;
2778 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2779 goto nla_put_failure;
2781 nlmsg_end(skb, nlh);
2786 nlmsg_cancel(skb, nlh);
2790 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2792 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2794 struct nf_conntrack_expect *exp = item->exp;
2795 struct net *net = nf_ct_exp_net(exp);
2796 struct nlmsghdr *nlh;
2797 struct nfgenmsg *nfmsg;
2798 struct sk_buff *skb;
2799 unsigned int type, group;
2802 if (events & (1 << IPEXP_DESTROY)) {
2803 type = IPCTNL_MSG_EXP_DELETE;
2804 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2805 } else if (events & (1 << IPEXP_NEW)) {
2806 type = IPCTNL_MSG_EXP_NEW;
2807 flags = NLM_F_CREATE|NLM_F_EXCL;
2808 group = NFNLGRP_CONNTRACK_EXP_NEW;
2812 if (!item->report && !nfnetlink_has_listeners(net, group))
2815 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2819 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2820 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2824 nfmsg = nlmsg_data(nlh);
2825 nfmsg->nfgen_family = exp->tuple.src.l3num;
2826 nfmsg->version = NFNETLINK_V0;
2829 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2830 goto nla_put_failure;
2832 nlmsg_end(skb, nlh);
2833 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2837 nlmsg_cancel(skb, nlh);
2841 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2845 static int ctnetlink_exp_done(struct netlink_callback *cb)
2848 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2853 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2855 struct net *net = sock_net(skb->sk);
2856 struct nf_conntrack_expect *exp, *last;
2857 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2858 u_int8_t l3proto = nfmsg->nfgen_family;
2861 last = (struct nf_conntrack_expect *)cb->args[1];
2862 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2864 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2866 if (l3proto && exp->tuple.src.l3num != l3proto)
2869 if (!net_eq(nf_ct_net(exp->master), net))
2877 if (ctnetlink_exp_fill_info(skb,
2878 NETLINK_CB(cb->skb).portid,
2882 if (!refcount_inc_not_zero(&exp->use))
2884 cb->args[1] = (unsigned long)exp;
2896 nf_ct_expect_put(last);
2902 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2904 struct nf_conntrack_expect *exp, *last;
2905 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2906 struct nf_conn *ct = cb->data;
2907 struct nf_conn_help *help = nfct_help(ct);
2908 u_int8_t l3proto = nfmsg->nfgen_family;
2914 last = (struct nf_conntrack_expect *)cb->args[1];
2916 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2917 if (l3proto && exp->tuple.src.l3num != l3proto)
2924 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2928 if (!refcount_inc_not_zero(&exp->use))
2930 cb->args[1] = (unsigned long)exp;
2942 nf_ct_expect_put(last);
2947 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2948 struct sk_buff *skb,
2949 const struct nlmsghdr *nlh,
2950 const struct nlattr * const cda[],
2951 struct netlink_ext_ack *extack)
2954 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2955 u_int8_t u3 = nfmsg->nfgen_family;
2956 struct nf_conntrack_tuple tuple;
2957 struct nf_conntrack_tuple_hash *h;
2959 struct nf_conntrack_zone zone;
2960 struct netlink_dump_control c = {
2961 .dump = ctnetlink_exp_ct_dump_table,
2962 .done = ctnetlink_exp_done,
2965 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2970 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2974 h = nf_conntrack_find_get(net, &zone, &tuple);
2978 ct = nf_ct_tuplehash_to_ctrack(h);
2979 /* No expectation linked to this connection tracking. */
2980 if (!nfct_help(ct)) {
2987 err = netlink_dump_start(ctnl, skb, nlh, &c);
2993 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2994 struct sk_buff *skb, const struct nlmsghdr *nlh,
2995 const struct nlattr * const cda[],
2996 struct netlink_ext_ack *extack)
2998 struct nf_conntrack_tuple tuple;
2999 struct nf_conntrack_expect *exp;
3000 struct sk_buff *skb2;
3001 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3002 u_int8_t u3 = nfmsg->nfgen_family;
3003 struct nf_conntrack_zone zone;
3006 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3007 if (cda[CTA_EXPECT_MASTER])
3008 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
3011 struct netlink_dump_control c = {
3012 .dump = ctnetlink_exp_dump_table,
3013 .done = ctnetlink_exp_done,
3015 return netlink_dump_start(ctnl, skb, nlh, &c);
3019 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3023 if (cda[CTA_EXPECT_TUPLE])
3024 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3026 else if (cda[CTA_EXPECT_MASTER])
3027 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
3035 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3039 if (cda[CTA_EXPECT_ID]) {
3040 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3041 if (ntohl(id) != (u32)(unsigned long)exp) {
3042 nf_ct_expect_put(exp);
3048 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
3050 nf_ct_expect_put(exp);
3055 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
3056 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
3058 nf_ct_expect_put(exp);
3062 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
3071 /* this avoids a loop in nfnetlink. */
3072 return err == -EAGAIN ? -ENOBUFS : err;
3075 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
3077 const struct nf_conn_help *m_help;
3078 const char *name = data;
3080 m_help = nfct_help(exp->master);
3082 return strcmp(m_help->helper->name, name) == 0;
3085 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
3090 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
3091 struct sk_buff *skb, const struct nlmsghdr *nlh,
3092 const struct nlattr * const cda[],
3093 struct netlink_ext_ack *extack)
3095 struct nf_conntrack_expect *exp;
3096 struct nf_conntrack_tuple tuple;
3097 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3098 u_int8_t u3 = nfmsg->nfgen_family;
3099 struct nf_conntrack_zone zone;
3102 if (cda[CTA_EXPECT_TUPLE]) {
3103 /* delete a single expect by tuple */
3104 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3108 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3113 /* bump usage count to 2 */
3114 exp = nf_ct_expect_find_get(net, &zone, &tuple);
3118 if (cda[CTA_EXPECT_ID]) {
3119 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
3120 if (ntohl(id) != (u32)(unsigned long)exp) {
3121 nf_ct_expect_put(exp);
3126 /* after list removal, usage count == 1 */
3127 spin_lock_bh(&nf_conntrack_expect_lock);
3128 if (del_timer(&exp->timeout)) {
3129 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
3131 nf_ct_expect_put(exp);
3133 spin_unlock_bh(&nf_conntrack_expect_lock);
3134 /* have to put what we 'get' above.
3135 * after this line usage count == 0 */
3136 nf_ct_expect_put(exp);
3137 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3138 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3140 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3141 NETLINK_CB(skb).portid,
3144 /* This basically means we have to flush everything*/
3145 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3146 NETLINK_CB(skb).portid,
3153 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3154 const struct nlattr * const cda[])
3156 if (cda[CTA_EXPECT_TIMEOUT]) {
3157 if (!del_timer(&x->timeout))
3160 x->timeout.expires = jiffies +
3161 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3162 add_timer(&x->timeout);
3167 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3168 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3169 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3173 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3174 struct nf_conntrack_expect *exp,
3177 #ifdef CONFIG_NF_NAT_NEEDED
3178 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3179 struct nf_conntrack_tuple nat_tuple = {};
3182 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3183 exp_nat_nla_policy, NULL);
3187 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3190 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3191 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3196 exp->saved_addr = nat_tuple.src.u3;
3197 exp->saved_proto = nat_tuple.src.u;
3198 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3206 static struct nf_conntrack_expect *
3207 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3208 struct nf_conntrack_helper *helper,
3209 struct nf_conntrack_tuple *tuple,
3210 struct nf_conntrack_tuple *mask)
3212 u_int32_t class = 0;
3213 struct nf_conntrack_expect *exp;
3214 struct nf_conn_help *help;
3217 help = nfct_help(ct);
3219 return ERR_PTR(-EOPNOTSUPP);
3221 if (cda[CTA_EXPECT_CLASS] && helper) {
3222 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3223 if (class > helper->expect_class_max)
3224 return ERR_PTR(-EINVAL);
3226 exp = nf_ct_expect_alloc(ct);
3228 return ERR_PTR(-ENOMEM);
3230 if (cda[CTA_EXPECT_FLAGS]) {
3231 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3232 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3236 if (cda[CTA_EXPECT_FN]) {
3237 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3238 struct nf_ct_helper_expectfn *expfn;
3240 expfn = nf_ct_helper_expectfn_find_by_name(name);
3241 if (expfn == NULL) {
3245 exp->expectfn = expfn->expectfn;
3247 exp->expectfn = NULL;
3251 exp->helper = helper;
3252 exp->tuple = *tuple;
3253 exp->mask.src.u3 = mask->src.u3;
3254 exp->mask.src.u.all = mask->src.u.all;
3256 if (cda[CTA_EXPECT_NAT]) {
3257 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3258 exp, nf_ct_l3num(ct));
3264 nf_ct_expect_put(exp);
3265 return ERR_PTR(err);
3269 ctnetlink_create_expect(struct net *net,
3270 const struct nf_conntrack_zone *zone,
3271 const struct nlattr * const cda[],
3272 u_int8_t u3, u32 portid, int report)
3274 struct nf_conntrack_tuple tuple, mask, master_tuple;
3275 struct nf_conntrack_tuple_hash *h = NULL;
3276 struct nf_conntrack_helper *helper = NULL;
3277 struct nf_conntrack_expect *exp;
3281 /* caller guarantees that those three CTA_EXPECT_* exist */
3282 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3286 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3290 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3295 /* Look for master conntrack of this expectation */
3296 h = nf_conntrack_find_get(net, zone, &master_tuple);
3299 ct = nf_ct_tuplehash_to_ctrack(h);
3302 if (cda[CTA_EXPECT_HELP_NAME]) {
3303 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3305 helper = __nf_conntrack_helper_find(helpname, u3,
3306 nf_ct_protonum(ct));
3307 if (helper == NULL) {
3309 #ifdef CONFIG_MODULES
3310 if (request_module("nfct-helper-%s", helpname) < 0) {
3315 helper = __nf_conntrack_helper_find(helpname, u3,
3316 nf_ct_protonum(ct));
3328 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3334 err = nf_ct_expect_related_report(exp, portid, report);
3335 nf_ct_expect_put(exp);
3343 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3344 struct sk_buff *skb, const struct nlmsghdr *nlh,
3345 const struct nlattr * const cda[],
3346 struct netlink_ext_ack *extack)
3348 struct nf_conntrack_tuple tuple;
3349 struct nf_conntrack_expect *exp;
3350 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3351 u_int8_t u3 = nfmsg->nfgen_family;
3352 struct nf_conntrack_zone zone;
3355 if (!cda[CTA_EXPECT_TUPLE]
3356 || !cda[CTA_EXPECT_MASK]
3357 || !cda[CTA_EXPECT_MASTER])
3360 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3364 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3369 spin_lock_bh(&nf_conntrack_expect_lock);
3370 exp = __nf_ct_expect_find(net, &zone, &tuple);
3372 spin_unlock_bh(&nf_conntrack_expect_lock);
3374 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3375 err = ctnetlink_create_expect(net, &zone, cda, u3,
3376 NETLINK_CB(skb).portid,
3383 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3384 err = ctnetlink_change_expect(exp, cda);
3385 spin_unlock_bh(&nf_conntrack_expect_lock);
3391 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3392 const struct ip_conntrack_stat *st)
3394 struct nlmsghdr *nlh;
3395 struct nfgenmsg *nfmsg;
3396 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3398 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3399 IPCTNL_MSG_EXP_GET_STATS_CPU);
3400 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3404 nfmsg = nlmsg_data(nlh);
3405 nfmsg->nfgen_family = AF_UNSPEC;
3406 nfmsg->version = NFNETLINK_V0;
3407 nfmsg->res_id = htons(cpu);
3409 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3410 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3411 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3412 goto nla_put_failure;
3414 nlmsg_end(skb, nlh);
3419 nlmsg_cancel(skb, nlh);
3424 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3427 struct net *net = sock_net(skb->sk);
3429 if (cb->args[0] == nr_cpu_ids)
3432 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3433 const struct ip_conntrack_stat *st;
3435 if (!cpu_possible(cpu))
3438 st = per_cpu_ptr(net->ct.stat, cpu);
3439 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3449 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3450 struct sk_buff *skb,
3451 const struct nlmsghdr *nlh,
3452 const struct nlattr * const cda[],
3453 struct netlink_ext_ack *extack)
3455 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3456 struct netlink_dump_control c = {
3457 .dump = ctnetlink_exp_stat_cpu_dump,
3459 return netlink_dump_start(ctnl, skb, nlh, &c);
3465 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3466 static struct nf_ct_event_notifier ctnl_notifier = {
3467 .fcn = ctnetlink_conntrack_event,
3470 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3471 .fcn = ctnetlink_expect_event,
3475 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3476 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3477 .attr_count = CTA_MAX,
3478 .policy = ct_nla_policy },
3479 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3480 .attr_count = CTA_MAX,
3481 .policy = ct_nla_policy },
3482 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3483 .attr_count = CTA_MAX,
3484 .policy = ct_nla_policy },
3485 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3486 .attr_count = CTA_MAX,
3487 .policy = ct_nla_policy },
3488 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3489 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3490 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3491 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3494 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3495 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3496 .attr_count = CTA_EXPECT_MAX,
3497 .policy = exp_nla_policy },
3498 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3499 .attr_count = CTA_EXPECT_MAX,
3500 .policy = exp_nla_policy },
3501 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3502 .attr_count = CTA_EXPECT_MAX,
3503 .policy = exp_nla_policy },
3504 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3507 static const struct nfnetlink_subsystem ctnl_subsys = {
3508 .name = "conntrack",
3509 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3510 .cb_count = IPCTNL_MSG_MAX,
3514 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3515 .name = "conntrack_expect",
3516 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3517 .cb_count = IPCTNL_MSG_EXP_MAX,
3521 MODULE_ALIAS("ip_conntrack_netlink");
3522 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3523 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3525 static int __net_init ctnetlink_net_init(struct net *net)
3527 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3530 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3532 pr_err("ctnetlink_init: cannot register notifier.\n");
3536 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3538 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3539 goto err_unreg_notifier;
3544 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3546 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3552 static void ctnetlink_net_exit(struct net *net)
3554 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3555 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3556 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3560 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3564 list_for_each_entry(net, net_exit_list, exit_list)
3565 ctnetlink_net_exit(net);
3568 static struct pernet_operations ctnetlink_net_ops = {
3569 .init = ctnetlink_net_init,
3570 .exit_batch = ctnetlink_net_exit_batch,
3573 static int __init ctnetlink_init(void)
3577 ret = nfnetlink_subsys_register(&ctnl_subsys);
3579 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3583 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3585 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3586 goto err_unreg_subsys;
3589 ret = register_pernet_subsys(&ctnetlink_net_ops);
3591 pr_err("ctnetlink_init: cannot register pernet operations\n");
3592 goto err_unreg_exp_subsys;
3594 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3595 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3596 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3600 err_unreg_exp_subsys:
3601 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3603 nfnetlink_subsys_unregister(&ctnl_subsys);
3608 static void __exit ctnetlink_exit(void)
3610 unregister_pernet_subsys(&ctnetlink_net_ops);
3611 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3612 nfnetlink_subsys_unregister(&ctnl_subsys);
3613 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3614 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3619 module_init(ctnetlink_init);
3620 module_exit(ctnetlink_exit);