1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
32 #include <linux/siphash.h>
34 #include <linux/netfilter.h>
35 #include <net/netlink.h>
37 #include <net/netfilter/nf_conntrack.h>
38 #include <net/netfilter/nf_conntrack_core.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_seqadj.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #include <net/netfilter/nf_conntrack_acct.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_labels.h>
48 #include <net/netfilter/nf_conntrack_synproxy.h>
49 #if IS_ENABLED(CONFIG_NF_NAT)
50 #include <net/netfilter/nf_nat.h>
51 #include <net/netfilter/nf_nat_helper.h>
54 #include <linux/netfilter/nfnetlink.h>
55 #include <linux/netfilter/nfnetlink_conntrack.h>
57 #include "nf_internals.h"
59 MODULE_LICENSE("GPL");
61 static int ctnetlink_dump_tuples_proto(struct sk_buff
*skb
,
62 const struct nf_conntrack_tuple
*tuple
,
63 const struct nf_conntrack_l4proto
*l4proto
)
66 struct nlattr
*nest_parms
;
68 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_PROTO
);
71 if (nla_put_u8(skb
, CTA_PROTO_NUM
, tuple
->dst
.protonum
))
74 if (likely(l4proto
->tuple_to_nlattr
))
75 ret
= l4proto
->tuple_to_nlattr(skb
, tuple
);
77 nla_nest_end(skb
, nest_parms
);
85 static int ipv4_tuple_to_nlattr(struct sk_buff
*skb
,
86 const struct nf_conntrack_tuple
*tuple
)
88 if (nla_put_in_addr(skb
, CTA_IP_V4_SRC
, tuple
->src
.u3
.ip
) ||
89 nla_put_in_addr(skb
, CTA_IP_V4_DST
, tuple
->dst
.u3
.ip
))
94 static int ipv6_tuple_to_nlattr(struct sk_buff
*skb
,
95 const struct nf_conntrack_tuple
*tuple
)
97 if (nla_put_in6_addr(skb
, CTA_IP_V6_SRC
, &tuple
->src
.u3
.in6
) ||
98 nla_put_in6_addr(skb
, CTA_IP_V6_DST
, &tuple
->dst
.u3
.in6
))
103 static int ctnetlink_dump_tuples_ip(struct sk_buff
*skb
,
104 const struct nf_conntrack_tuple
*tuple
)
107 struct nlattr
*nest_parms
;
109 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_IP
);
111 goto nla_put_failure
;
113 switch (tuple
->src
.l3num
) {
115 ret
= ipv4_tuple_to_nlattr(skb
, tuple
);
118 ret
= ipv6_tuple_to_nlattr(skb
, tuple
);
122 nla_nest_end(skb
, nest_parms
);
130 static int ctnetlink_dump_tuples(struct sk_buff
*skb
,
131 const struct nf_conntrack_tuple
*tuple
)
133 const struct nf_conntrack_l4proto
*l4proto
;
137 ret
= ctnetlink_dump_tuples_ip(skb
, tuple
);
140 l4proto
= nf_ct_l4proto_find(tuple
->dst
.protonum
);
141 ret
= ctnetlink_dump_tuples_proto(skb
, tuple
, l4proto
);
147 static int ctnetlink_dump_zone_id(struct sk_buff
*skb
, int attrtype
,
148 const struct nf_conntrack_zone
*zone
, int dir
)
150 if (zone
->id
== NF_CT_DEFAULT_ZONE_ID
|| zone
->dir
!= dir
)
152 if (nla_put_be16(skb
, attrtype
, htons(zone
->id
)))
153 goto nla_put_failure
;
160 static int ctnetlink_dump_status(struct sk_buff
*skb
, const struct nf_conn
*ct
)
162 if (nla_put_be32(skb
, CTA_STATUS
, htonl(ct
->status
)))
163 goto nla_put_failure
;
170 static int ctnetlink_dump_timeout(struct sk_buff
*skb
, const struct nf_conn
*ct
,
173 long timeout
= nf_ct_expires(ct
) / HZ
;
175 if (skip_zero
&& timeout
== 0)
178 if (nla_put_be32(skb
, CTA_TIMEOUT
, htonl(timeout
)))
179 goto nla_put_failure
;
186 static int ctnetlink_dump_protoinfo(struct sk_buff
*skb
, struct nf_conn
*ct
,
189 const struct nf_conntrack_l4proto
*l4proto
;
190 struct nlattr
*nest_proto
;
193 l4proto
= nf_ct_l4proto_find(nf_ct_protonum(ct
));
194 if (!l4proto
->to_nlattr
)
197 nest_proto
= nla_nest_start(skb
, CTA_PROTOINFO
);
199 goto nla_put_failure
;
201 ret
= l4proto
->to_nlattr(skb
, nest_proto
, ct
, destroy
);
203 nla_nest_end(skb
, nest_proto
);
211 static int ctnetlink_dump_helpinfo(struct sk_buff
*skb
,
212 const struct nf_conn
*ct
)
214 struct nlattr
*nest_helper
;
215 const struct nf_conn_help
*help
= nfct_help(ct
);
216 struct nf_conntrack_helper
*helper
;
222 helper
= rcu_dereference(help
->helper
);
226 nest_helper
= nla_nest_start(skb
, CTA_HELP
);
228 goto nla_put_failure
;
229 if (nla_put_string(skb
, CTA_HELP_NAME
, helper
->name
))
230 goto nla_put_failure
;
232 if (helper
->to_nlattr
)
233 helper
->to_nlattr(skb
, ct
);
235 nla_nest_end(skb
, nest_helper
);
246 dump_counters(struct sk_buff
*skb
, struct nf_conn_acct
*acct
,
247 enum ip_conntrack_dir dir
, int type
)
249 enum ctattr_type attr
= dir
? CTA_COUNTERS_REPLY
: CTA_COUNTERS_ORIG
;
250 struct nf_conn_counter
*counter
= acct
->counter
;
251 struct nlattr
*nest_count
;
254 if (type
== IPCTNL_MSG_CT_GET_CTRZERO
) {
255 pkts
= atomic64_xchg(&counter
[dir
].packets
, 0);
256 bytes
= atomic64_xchg(&counter
[dir
].bytes
, 0);
258 pkts
= atomic64_read(&counter
[dir
].packets
);
259 bytes
= atomic64_read(&counter
[dir
].bytes
);
262 nest_count
= nla_nest_start(skb
, attr
);
264 goto nla_put_failure
;
266 if (nla_put_be64(skb
, CTA_COUNTERS_PACKETS
, cpu_to_be64(pkts
),
268 nla_put_be64(skb
, CTA_COUNTERS_BYTES
, cpu_to_be64(bytes
),
270 goto nla_put_failure
;
272 nla_nest_end(skb
, nest_count
);
281 ctnetlink_dump_acct(struct sk_buff
*skb
, const struct nf_conn
*ct
, int type
)
283 struct nf_conn_acct
*acct
= nf_conn_acct_find(ct
);
288 if (dump_counters(skb
, acct
, IP_CT_DIR_ORIGINAL
, type
) < 0)
290 if (dump_counters(skb
, acct
, IP_CT_DIR_REPLY
, type
) < 0)
297 ctnetlink_dump_timestamp(struct sk_buff
*skb
, const struct nf_conn
*ct
)
299 struct nlattr
*nest_count
;
300 const struct nf_conn_tstamp
*tstamp
;
302 tstamp
= nf_conn_tstamp_find(ct
);
306 nest_count
= nla_nest_start(skb
, CTA_TIMESTAMP
);
308 goto nla_put_failure
;
310 if (nla_put_be64(skb
, CTA_TIMESTAMP_START
, cpu_to_be64(tstamp
->start
),
311 CTA_TIMESTAMP_PAD
) ||
312 (tstamp
->stop
!= 0 && nla_put_be64(skb
, CTA_TIMESTAMP_STOP
,
313 cpu_to_be64(tstamp
->stop
),
315 goto nla_put_failure
;
316 nla_nest_end(skb
, nest_count
);
324 #ifdef CONFIG_NF_CONNTRACK_MARK
325 static int ctnetlink_dump_mark(struct sk_buff
*skb
, const struct nf_conn
*ct
)
327 if (nla_put_be32(skb
, CTA_MARK
, htonl(ct
->mark
)))
328 goto nla_put_failure
;
335 #define ctnetlink_dump_mark(a, b) (0)
338 #ifdef CONFIG_NF_CONNTRACK_SECMARK
339 static int ctnetlink_dump_secctx(struct sk_buff
*skb
, const struct nf_conn
*ct
)
341 struct nlattr
*nest_secctx
;
345 ret
= security_secid_to_secctx(ct
->secmark
, &secctx
, &len
);
350 nest_secctx
= nla_nest_start(skb
, CTA_SECCTX
);
352 goto nla_put_failure
;
354 if (nla_put_string(skb
, CTA_SECCTX_NAME
, secctx
))
355 goto nla_put_failure
;
356 nla_nest_end(skb
, nest_secctx
);
360 security_release_secctx(secctx
, len
);
364 #define ctnetlink_dump_secctx(a, b) (0)
367 #ifdef CONFIG_NF_CONNTRACK_LABELS
368 static inline int ctnetlink_label_size(const struct nf_conn
*ct
)
370 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
374 return nla_total_size(sizeof(labels
->bits
));
378 ctnetlink_dump_labels(struct sk_buff
*skb
, const struct nf_conn
*ct
)
380 struct nf_conn_labels
*labels
= nf_ct_labels_find(ct
);
388 if (labels
->bits
[i
] != 0)
389 return nla_put(skb
, CTA_LABELS
, sizeof(labels
->bits
),
392 } while (i
< ARRAY_SIZE(labels
->bits
));
397 #define ctnetlink_dump_labels(a, b) (0)
398 #define ctnetlink_label_size(a) (0)
401 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
403 static int ctnetlink_dump_master(struct sk_buff
*skb
, const struct nf_conn
*ct
)
405 struct nlattr
*nest_parms
;
407 if (!(ct
->status
& IPS_EXPECTED
))
410 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_MASTER
);
412 goto nla_put_failure
;
413 if (ctnetlink_dump_tuples(skb
, master_tuple(ct
)) < 0)
414 goto nla_put_failure
;
415 nla_nest_end(skb
, nest_parms
);
424 dump_ct_seq_adj(struct sk_buff
*skb
, const struct nf_ct_seqadj
*seq
, int type
)
426 struct nlattr
*nest_parms
;
428 nest_parms
= nla_nest_start(skb
, type
);
430 goto nla_put_failure
;
432 if (nla_put_be32(skb
, CTA_SEQADJ_CORRECTION_POS
,
433 htonl(seq
->correction_pos
)) ||
434 nla_put_be32(skb
, CTA_SEQADJ_OFFSET_BEFORE
,
435 htonl(seq
->offset_before
)) ||
436 nla_put_be32(skb
, CTA_SEQADJ_OFFSET_AFTER
,
437 htonl(seq
->offset_after
)))
438 goto nla_put_failure
;
440 nla_nest_end(skb
, nest_parms
);
448 static int ctnetlink_dump_ct_seq_adj(struct sk_buff
*skb
, struct nf_conn
*ct
)
450 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
451 struct nf_ct_seqadj
*seq
;
453 if (!(ct
->status
& IPS_SEQ_ADJUST
) || !seqadj
)
456 spin_lock_bh(&ct
->lock
);
457 seq
= &seqadj
->seq
[IP_CT_DIR_ORIGINAL
];
458 if (dump_ct_seq_adj(skb
, seq
, CTA_SEQ_ADJ_ORIG
) == -1)
461 seq
= &seqadj
->seq
[IP_CT_DIR_REPLY
];
462 if (dump_ct_seq_adj(skb
, seq
, CTA_SEQ_ADJ_REPLY
) == -1)
465 spin_unlock_bh(&ct
->lock
);
468 spin_unlock_bh(&ct
->lock
);
472 static int ctnetlink_dump_ct_synproxy(struct sk_buff
*skb
, struct nf_conn
*ct
)
474 struct nf_conn_synproxy
*synproxy
= nfct_synproxy(ct
);
475 struct nlattr
*nest_parms
;
480 nest_parms
= nla_nest_start(skb
, CTA_SYNPROXY
);
482 goto nla_put_failure
;
484 if (nla_put_be32(skb
, CTA_SYNPROXY_ISN
, htonl(synproxy
->isn
)) ||
485 nla_put_be32(skb
, CTA_SYNPROXY_ITS
, htonl(synproxy
->its
)) ||
486 nla_put_be32(skb
, CTA_SYNPROXY_TSOFF
, htonl(synproxy
->tsoff
)))
487 goto nla_put_failure
;
489 nla_nest_end(skb
, nest_parms
);
497 static int ctnetlink_dump_id(struct sk_buff
*skb
, const struct nf_conn
*ct
)
499 __be32 id
= (__force __be32
)nf_ct_get_id(ct
);
501 if (nla_put_be32(skb
, CTA_ID
, id
))
502 goto nla_put_failure
;
509 static int ctnetlink_dump_use(struct sk_buff
*skb
, const struct nf_conn
*ct
)
511 if (nla_put_be32(skb
, CTA_USE
, htonl(atomic_read(&ct
->ct_general
.use
))))
512 goto nla_put_failure
;
519 /* all these functions access ct->ext. Caller must either hold a reference
520 * on ct or prevent its deletion by holding either the bucket spinlock or
521 * pcpu dying list lock.
523 static int ctnetlink_dump_extinfo(struct sk_buff
*skb
,
524 struct nf_conn
*ct
, u32 type
)
526 if (ctnetlink_dump_acct(skb
, ct
, type
) < 0 ||
527 ctnetlink_dump_timestamp(skb
, ct
) < 0 ||
528 ctnetlink_dump_helpinfo(skb
, ct
) < 0 ||
529 ctnetlink_dump_labels(skb
, ct
) < 0 ||
530 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0 ||
531 ctnetlink_dump_ct_synproxy(skb
, ct
) < 0)
537 static int ctnetlink_dump_info(struct sk_buff
*skb
, struct nf_conn
*ct
)
539 if (ctnetlink_dump_status(skb
, ct
) < 0 ||
540 ctnetlink_dump_mark(skb
, ct
) < 0 ||
541 ctnetlink_dump_secctx(skb
, ct
) < 0 ||
542 ctnetlink_dump_id(skb
, ct
) < 0 ||
543 ctnetlink_dump_use(skb
, ct
) < 0 ||
544 ctnetlink_dump_master(skb
, ct
) < 0)
547 if (!test_bit(IPS_OFFLOAD_BIT
, &ct
->status
) &&
548 (ctnetlink_dump_timeout(skb
, ct
, false) < 0 ||
549 ctnetlink_dump_protoinfo(skb
, ct
, false) < 0))
556 ctnetlink_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
557 struct nf_conn
*ct
, bool extinfo
, unsigned int flags
)
559 const struct nf_conntrack_zone
*zone
;
560 struct nlmsghdr
*nlh
;
561 struct nlattr
*nest_parms
;
565 flags
|= NLM_F_MULTI
;
566 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
, IPCTNL_MSG_CT_NEW
);
567 nlh
= nfnl_msg_put(skb
, portid
, seq
, event
, flags
, nf_ct_l3num(ct
),
572 zone
= nf_ct_zone(ct
);
574 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
);
576 goto nla_put_failure
;
577 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
578 goto nla_put_failure
;
579 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
580 NF_CT_ZONE_DIR_ORIG
) < 0)
581 goto nla_put_failure
;
582 nla_nest_end(skb
, nest_parms
);
584 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
);
586 goto nla_put_failure
;
587 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
588 goto nla_put_failure
;
589 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
590 NF_CT_ZONE_DIR_REPL
) < 0)
591 goto nla_put_failure
;
592 nla_nest_end(skb
, nest_parms
);
594 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
595 NF_CT_DEFAULT_ZONE_DIR
) < 0)
596 goto nla_put_failure
;
598 if (ctnetlink_dump_info(skb
, ct
) < 0)
599 goto nla_put_failure
;
600 if (extinfo
&& ctnetlink_dump_extinfo(skb
, ct
, type
) < 0)
601 goto nla_put_failure
;
608 nlmsg_cancel(skb
, nlh
);
612 static const struct nla_policy cta_ip_nla_policy
[CTA_IP_MAX
+ 1] = {
613 [CTA_IP_V4_SRC
] = { .type
= NLA_U32
},
614 [CTA_IP_V4_DST
] = { .type
= NLA_U32
},
615 [CTA_IP_V6_SRC
] = { .len
= sizeof(__be32
) * 4 },
616 [CTA_IP_V6_DST
] = { .len
= sizeof(__be32
) * 4 },
619 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
620 static size_t ctnetlink_proto_size(const struct nf_conn
*ct
)
622 const struct nf_conntrack_l4proto
*l4proto
;
623 size_t len
, len4
= 0;
625 len
= nla_policy_len(cta_ip_nla_policy
, CTA_IP_MAX
+ 1);
626 len
*= 3u; /* ORIG, REPLY, MASTER */
628 l4proto
= nf_ct_l4proto_find(nf_ct_protonum(ct
));
629 len
+= l4proto
->nlattr_size
;
630 if (l4proto
->nlattr_tuple_size
) {
631 len4
= l4proto
->nlattr_tuple_size();
632 len4
*= 3u; /* ORIG, REPLY, MASTER */
639 static inline size_t ctnetlink_acct_size(const struct nf_conn
*ct
)
641 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_ACCT
))
643 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
644 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
645 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
649 static inline int ctnetlink_secctx_size(const struct nf_conn
*ct
)
651 #ifdef CONFIG_NF_CONNTRACK_SECMARK
654 ret
= security_secid_to_secctx(ct
->secmark
, NULL
, &len
);
658 return nla_total_size(0) /* CTA_SECCTX */
659 + nla_total_size(sizeof(char) * len
); /* CTA_SECCTX_NAME */
665 static inline size_t ctnetlink_timestamp_size(const struct nf_conn
*ct
)
667 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
668 if (!nf_ct_ext_exist(ct
, NF_CT_EXT_TSTAMP
))
670 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
676 #ifdef CONFIG_NF_CONNTRACK_EVENTS
677 static size_t ctnetlink_nlmsg_size(const struct nf_conn
*ct
)
679 return NLMSG_ALIGN(sizeof(struct nfgenmsg
))
680 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
681 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
682 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
683 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
684 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
685 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
686 + ctnetlink_acct_size(ct
)
687 + ctnetlink_timestamp_size(ct
)
688 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
689 + nla_total_size(0) /* CTA_PROTOINFO */
690 + nla_total_size(0) /* CTA_HELP */
691 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
692 + ctnetlink_secctx_size(ct
)
693 #if IS_ENABLED(CONFIG_NF_NAT)
694 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
695 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
697 #ifdef CONFIG_NF_CONNTRACK_MARK
698 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
700 #ifdef CONFIG_NF_CONNTRACK_ZONES
701 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
703 + ctnetlink_proto_size(ct
)
704 + ctnetlink_label_size(ct
)
709 ctnetlink_conntrack_event(unsigned int events
, const struct nf_ct_event
*item
)
711 const struct nf_conntrack_zone
*zone
;
713 struct nlmsghdr
*nlh
;
714 struct nlattr
*nest_parms
;
715 struct nf_conn
*ct
= item
->ct
;
718 unsigned int flags
= 0, group
;
721 if (events
& (1 << IPCT_DESTROY
)) {
722 type
= IPCTNL_MSG_CT_DELETE
;
723 group
= NFNLGRP_CONNTRACK_DESTROY
;
724 } else if (events
& ((1 << IPCT_NEW
) | (1 << IPCT_RELATED
))) {
725 type
= IPCTNL_MSG_CT_NEW
;
726 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
727 group
= NFNLGRP_CONNTRACK_NEW
;
729 type
= IPCTNL_MSG_CT_NEW
;
730 group
= NFNLGRP_CONNTRACK_UPDATE
;
735 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
738 skb
= nlmsg_new(ctnetlink_nlmsg_size(ct
), GFP_ATOMIC
);
742 type
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
, type
);
743 nlh
= nfnl_msg_put(skb
, item
->portid
, 0, type
, flags
, nf_ct_l3num(ct
),
748 zone
= nf_ct_zone(ct
);
750 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
);
752 goto nla_put_failure
;
753 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
754 goto nla_put_failure
;
755 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
756 NF_CT_ZONE_DIR_ORIG
) < 0)
757 goto nla_put_failure
;
758 nla_nest_end(skb
, nest_parms
);
760 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
);
762 goto nla_put_failure
;
763 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
764 goto nla_put_failure
;
765 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
766 NF_CT_ZONE_DIR_REPL
) < 0)
767 goto nla_put_failure
;
768 nla_nest_end(skb
, nest_parms
);
770 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
771 NF_CT_DEFAULT_ZONE_DIR
) < 0)
772 goto nla_put_failure
;
774 if (ctnetlink_dump_id(skb
, ct
) < 0)
775 goto nla_put_failure
;
777 if (ctnetlink_dump_status(skb
, ct
) < 0)
778 goto nla_put_failure
;
780 if (events
& (1 << IPCT_DESTROY
)) {
781 if (ctnetlink_dump_timeout(skb
, ct
, true) < 0)
782 goto nla_put_failure
;
784 if (ctnetlink_dump_acct(skb
, ct
, type
) < 0 ||
785 ctnetlink_dump_timestamp(skb
, ct
) < 0 ||
786 ctnetlink_dump_protoinfo(skb
, ct
, true) < 0)
787 goto nla_put_failure
;
789 if (ctnetlink_dump_timeout(skb
, ct
, false) < 0)
790 goto nla_put_failure
;
792 if (events
& (1 << IPCT_PROTOINFO
) &&
793 ctnetlink_dump_protoinfo(skb
, ct
, false) < 0)
794 goto nla_put_failure
;
796 if ((events
& (1 << IPCT_HELPER
) || nfct_help(ct
))
797 && ctnetlink_dump_helpinfo(skb
, ct
) < 0)
798 goto nla_put_failure
;
800 #ifdef CONFIG_NF_CONNTRACK_SECMARK
801 if ((events
& (1 << IPCT_SECMARK
) || ct
->secmark
)
802 && ctnetlink_dump_secctx(skb
, ct
) < 0)
803 goto nla_put_failure
;
805 if (events
& (1 << IPCT_LABEL
) &&
806 ctnetlink_dump_labels(skb
, ct
) < 0)
807 goto nla_put_failure
;
809 if (events
& (1 << IPCT_RELATED
) &&
810 ctnetlink_dump_master(skb
, ct
) < 0)
811 goto nla_put_failure
;
813 if (events
& (1 << IPCT_SEQADJ
) &&
814 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
815 goto nla_put_failure
;
817 if (events
& (1 << IPCT_SYNPROXY
) &&
818 ctnetlink_dump_ct_synproxy(skb
, ct
) < 0)
819 goto nla_put_failure
;
822 #ifdef CONFIG_NF_CONNTRACK_MARK
823 if ((events
& (1 << IPCT_MARK
) || ct
->mark
)
824 && ctnetlink_dump_mark(skb
, ct
) < 0)
825 goto nla_put_failure
;
828 err
= nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
,
830 if (err
== -ENOBUFS
|| err
== -EAGAIN
)
836 nlmsg_cancel(skb
, nlh
);
840 if (nfnetlink_set_err(net
, 0, group
, -ENOBUFS
) > 0)
845 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
847 static int ctnetlink_done(struct netlink_callback
*cb
)
850 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
855 struct ctnetlink_filter_u32
{
860 struct ctnetlink_filter
{
863 u_int32_t orig_flags
;
864 u_int32_t reply_flags
;
866 struct nf_conntrack_tuple orig
;
867 struct nf_conntrack_tuple reply
;
868 struct nf_conntrack_zone zone
;
870 struct ctnetlink_filter_u32 mark
;
871 struct ctnetlink_filter_u32 status
;
874 static const struct nla_policy cta_filter_nla_policy
[CTA_FILTER_MAX
+ 1] = {
875 [CTA_FILTER_ORIG_FLAGS
] = { .type
= NLA_U32
},
876 [CTA_FILTER_REPLY_FLAGS
] = { .type
= NLA_U32
},
879 static int ctnetlink_parse_filter(const struct nlattr
*attr
,
880 struct ctnetlink_filter
*filter
)
882 struct nlattr
*tb
[CTA_FILTER_MAX
+ 1];
885 ret
= nla_parse_nested(tb
, CTA_FILTER_MAX
, attr
, cta_filter_nla_policy
,
890 if (tb
[CTA_FILTER_ORIG_FLAGS
]) {
891 filter
->orig_flags
= nla_get_u32(tb
[CTA_FILTER_ORIG_FLAGS
]);
892 if (filter
->orig_flags
& ~CTA_FILTER_F_ALL
)
896 if (tb
[CTA_FILTER_REPLY_FLAGS
]) {
897 filter
->reply_flags
= nla_get_u32(tb
[CTA_FILTER_REPLY_FLAGS
]);
898 if (filter
->reply_flags
& ~CTA_FILTER_F_ALL
)
905 static int ctnetlink_parse_zone(const struct nlattr
*attr
,
906 struct nf_conntrack_zone
*zone
);
907 static int ctnetlink_parse_tuple_filter(const struct nlattr
* const cda
[],
908 struct nf_conntrack_tuple
*tuple
,
909 u32 type
, u_int8_t l3num
,
910 struct nf_conntrack_zone
*zone
,
913 static int ctnetlink_filter_parse_mark(struct ctnetlink_filter_u32
*mark
,
914 const struct nlattr
* const cda
[])
916 #ifdef CONFIG_NF_CONNTRACK_MARK
918 mark
->val
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
920 if (cda
[CTA_MARK_MASK
])
921 mark
->mask
= ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
923 mark
->mask
= 0xffffffff;
924 } else if (cda
[CTA_MARK_MASK
]) {
931 static int ctnetlink_filter_parse_status(struct ctnetlink_filter_u32
*status
,
932 const struct nlattr
* const cda
[])
934 if (cda
[CTA_STATUS
]) {
935 status
->val
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
936 if (cda
[CTA_STATUS_MASK
])
937 status
->mask
= ntohl(nla_get_be32(cda
[CTA_STATUS_MASK
]));
939 status
->mask
= status
->val
;
941 /* status->val == 0? always true, else always false. */
942 if (status
->mask
== 0)
944 } else if (cda
[CTA_STATUS_MASK
]) {
948 /* CTA_STATUS is NLA_U32, if this fires UAPI needs to be extended */
949 BUILD_BUG_ON(__IPS_MAX_BIT
>= 32);
953 static struct ctnetlink_filter
*
954 ctnetlink_alloc_filter(const struct nlattr
* const cda
[], u8 family
)
956 struct ctnetlink_filter
*filter
;
959 #ifndef CONFIG_NF_CONNTRACK_MARK
960 if (cda
[CTA_MARK
] || cda
[CTA_MARK_MASK
])
961 return ERR_PTR(-EOPNOTSUPP
);
964 filter
= kzalloc(sizeof(*filter
), GFP_KERNEL
);
966 return ERR_PTR(-ENOMEM
);
968 filter
->family
= family
;
970 err
= ctnetlink_filter_parse_mark(&filter
->mark
, cda
);
974 err
= ctnetlink_filter_parse_status(&filter
->status
, cda
);
978 if (!cda
[CTA_FILTER
])
981 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &filter
->zone
);
985 err
= ctnetlink_parse_filter(cda
[CTA_FILTER
], filter
);
989 if (filter
->orig_flags
) {
990 if (!cda
[CTA_TUPLE_ORIG
]) {
995 err
= ctnetlink_parse_tuple_filter(cda
, &filter
->orig
,
1004 if (filter
->reply_flags
) {
1005 if (!cda
[CTA_TUPLE_REPLY
]) {
1010 err
= ctnetlink_parse_tuple_filter(cda
, &filter
->reply
,
1014 filter
->orig_flags
);
1026 return ERR_PTR(err
);
1029 static bool ctnetlink_needs_filter(u8 family
, const struct nlattr
* const *cda
)
1031 return family
|| cda
[CTA_MARK
] || cda
[CTA_FILTER
] || cda
[CTA_STATUS
];
1034 static int ctnetlink_start(struct netlink_callback
*cb
)
1036 const struct nlattr
* const *cda
= cb
->data
;
1037 struct ctnetlink_filter
*filter
= NULL
;
1038 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1039 u8 family
= nfmsg
->nfgen_family
;
1041 if (ctnetlink_needs_filter(family
, cda
)) {
1042 filter
= ctnetlink_alloc_filter(cda
, family
);
1044 return PTR_ERR(filter
);
1051 static int ctnetlink_filter_match_tuple(struct nf_conntrack_tuple
*filter_tuple
,
1052 struct nf_conntrack_tuple
*ct_tuple
,
1053 u_int32_t flags
, int family
)
1057 if ((flags
& CTA_FILTER_FLAG(CTA_IP_SRC
)) &&
1058 filter_tuple
->src
.u3
.ip
!= ct_tuple
->src
.u3
.ip
)
1061 if ((flags
& CTA_FILTER_FLAG(CTA_IP_DST
)) &&
1062 filter_tuple
->dst
.u3
.ip
!= ct_tuple
->dst
.u3
.ip
)
1066 if ((flags
& CTA_FILTER_FLAG(CTA_IP_SRC
)) &&
1067 !ipv6_addr_cmp(&filter_tuple
->src
.u3
.in6
,
1068 &ct_tuple
->src
.u3
.in6
))
1071 if ((flags
& CTA_FILTER_FLAG(CTA_IP_DST
)) &&
1072 !ipv6_addr_cmp(&filter_tuple
->dst
.u3
.in6
,
1073 &ct_tuple
->dst
.u3
.in6
))
1078 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_NUM
)) &&
1079 filter_tuple
->dst
.protonum
!= ct_tuple
->dst
.protonum
)
1082 switch (ct_tuple
->dst
.protonum
) {
1085 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_SRC_PORT
)) &&
1086 filter_tuple
->src
.u
.tcp
.port
!= ct_tuple
->src
.u
.tcp
.port
)
1089 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_DST_PORT
)) &&
1090 filter_tuple
->dst
.u
.tcp
.port
!= ct_tuple
->dst
.u
.tcp
.port
)
1094 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_ICMP_TYPE
)) &&
1095 filter_tuple
->dst
.u
.icmp
.type
!= ct_tuple
->dst
.u
.icmp
.type
)
1097 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_ICMP_CODE
)) &&
1098 filter_tuple
->dst
.u
.icmp
.code
!= ct_tuple
->dst
.u
.icmp
.code
)
1100 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_ICMP_ID
)) &&
1101 filter_tuple
->src
.u
.icmp
.id
!= ct_tuple
->src
.u
.icmp
.id
)
1104 case IPPROTO_ICMPV6
:
1105 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_TYPE
)) &&
1106 filter_tuple
->dst
.u
.icmp
.type
!= ct_tuple
->dst
.u
.icmp
.type
)
1108 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_CODE
)) &&
1109 filter_tuple
->dst
.u
.icmp
.code
!= ct_tuple
->dst
.u
.icmp
.code
)
1111 if ((flags
& CTA_FILTER_FLAG(CTA_PROTO_ICMPV6_ID
)) &&
1112 filter_tuple
->src
.u
.icmp
.id
!= ct_tuple
->src
.u
.icmp
.id
)
1120 static int ctnetlink_filter_match(struct nf_conn
*ct
, void *data
)
1122 struct ctnetlink_filter
*filter
= data
;
1123 struct nf_conntrack_tuple
*tuple
;
1129 /* Match entries of a given L3 protocol number.
1130 * If it is not specified, ie. l3proto == 0,
1131 * then match everything.
1133 if (filter
->family
&& nf_ct_l3num(ct
) != filter
->family
)
1136 if (filter
->orig_flags
) {
1137 tuple
= nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
);
1138 if (!ctnetlink_filter_match_tuple(&filter
->orig
, tuple
,
1144 if (filter
->reply_flags
) {
1145 tuple
= nf_ct_tuple(ct
, IP_CT_DIR_REPLY
);
1146 if (!ctnetlink_filter_match_tuple(&filter
->reply
, tuple
,
1147 filter
->reply_flags
,
1152 #ifdef CONFIG_NF_CONNTRACK_MARK
1153 if ((ct
->mark
& filter
->mark
.mask
) != filter
->mark
.val
)
1156 status
= (u32
)READ_ONCE(ct
->status
);
1157 if ((status
& filter
->status
.mask
) != filter
->status
.val
)
1168 ctnetlink_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1170 unsigned int flags
= cb
->data
? NLM_F_DUMP_FILTERED
: 0;
1171 struct net
*net
= sock_net(skb
->sk
);
1172 struct nf_conn
*ct
, *last
;
1173 struct nf_conntrack_tuple_hash
*h
;
1174 struct hlist_nulls_node
*n
;
1175 struct nf_conn
*nf_ct_evict
[8];
1179 last
= (struct nf_conn
*)cb
->args
[1];
1183 for (; cb
->args
[0] < nf_conntrack_htable_size
; cb
->args
[0]++) {
1187 if (nf_ct_should_gc(nf_ct_evict
[i
]))
1188 nf_ct_kill(nf_ct_evict
[i
]);
1189 nf_ct_put(nf_ct_evict
[i
]);
1192 lockp
= &nf_conntrack_locks
[cb
->args
[0] % CONNTRACK_LOCKS
];
1193 nf_conntrack_lock(lockp
);
1194 if (cb
->args
[0] >= nf_conntrack_htable_size
) {
1198 hlist_nulls_for_each_entry(h
, n
, &nf_conntrack_hash
[cb
->args
[0]],
1200 if (NF_CT_DIRECTION(h
) != IP_CT_DIR_ORIGINAL
)
1202 ct
= nf_ct_tuplehash_to_ctrack(h
);
1203 if (nf_ct_is_expired(ct
)) {
1204 if (i
< ARRAY_SIZE(nf_ct_evict
) &&
1205 atomic_inc_not_zero(&ct
->ct_general
.use
))
1206 nf_ct_evict
[i
++] = ct
;
1210 if (!net_eq(net
, nf_ct_net(ct
)))
1218 if (!ctnetlink_filter_match(ct
, cb
->data
))
1222 ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
1224 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
1227 nf_conntrack_get(&ct
->ct_general
);
1228 cb
->args
[1] = (unsigned long)ct
;
1242 /* nf ct hash resize happened, now clear the leftover. */
1243 if ((struct nf_conn
*)cb
->args
[1] == last
)
1251 if (nf_ct_should_gc(nf_ct_evict
[i
]))
1252 nf_ct_kill(nf_ct_evict
[i
]);
1253 nf_ct_put(nf_ct_evict
[i
]);
1259 static int ipv4_nlattr_to_tuple(struct nlattr
*tb
[],
1260 struct nf_conntrack_tuple
*t
,
1263 if (flags
& CTA_FILTER_FLAG(CTA_IP_SRC
)) {
1264 if (!tb
[CTA_IP_V4_SRC
])
1267 t
->src
.u3
.ip
= nla_get_in_addr(tb
[CTA_IP_V4_SRC
]);
1270 if (flags
& CTA_FILTER_FLAG(CTA_IP_DST
)) {
1271 if (!tb
[CTA_IP_V4_DST
])
1274 t
->dst
.u3
.ip
= nla_get_in_addr(tb
[CTA_IP_V4_DST
]);
1280 static int ipv6_nlattr_to_tuple(struct nlattr
*tb
[],
1281 struct nf_conntrack_tuple
*t
,
1284 if (flags
& CTA_FILTER_FLAG(CTA_IP_SRC
)) {
1285 if (!tb
[CTA_IP_V6_SRC
])
1288 t
->src
.u3
.in6
= nla_get_in6_addr(tb
[CTA_IP_V6_SRC
]);
1291 if (flags
& CTA_FILTER_FLAG(CTA_IP_DST
)) {
1292 if (!tb
[CTA_IP_V6_DST
])
1295 t
->dst
.u3
.in6
= nla_get_in6_addr(tb
[CTA_IP_V6_DST
]);
1301 static int ctnetlink_parse_tuple_ip(struct nlattr
*attr
,
1302 struct nf_conntrack_tuple
*tuple
,
1305 struct nlattr
*tb
[CTA_IP_MAX
+1];
1308 ret
= nla_parse_nested_deprecated(tb
, CTA_IP_MAX
, attr
, NULL
, NULL
);
1312 ret
= nla_validate_nested_deprecated(attr
, CTA_IP_MAX
,
1313 cta_ip_nla_policy
, NULL
);
1317 switch (tuple
->src
.l3num
) {
1319 ret
= ipv4_nlattr_to_tuple(tb
, tuple
, flags
);
1322 ret
= ipv6_nlattr_to_tuple(tb
, tuple
, flags
);
1329 static const struct nla_policy proto_nla_policy
[CTA_PROTO_MAX
+1] = {
1330 [CTA_PROTO_NUM
] = { .type
= NLA_U8
},
1333 static int ctnetlink_parse_tuple_proto(struct nlattr
*attr
,
1334 struct nf_conntrack_tuple
*tuple
,
1337 const struct nf_conntrack_l4proto
*l4proto
;
1338 struct nlattr
*tb
[CTA_PROTO_MAX
+1];
1341 ret
= nla_parse_nested_deprecated(tb
, CTA_PROTO_MAX
, attr
,
1342 proto_nla_policy
, NULL
);
1346 if (!(flags
& CTA_FILTER_FLAG(CTA_PROTO_NUM
)))
1349 if (!tb
[CTA_PROTO_NUM
])
1352 tuple
->dst
.protonum
= nla_get_u8(tb
[CTA_PROTO_NUM
]);
1355 l4proto
= nf_ct_l4proto_find(tuple
->dst
.protonum
);
1357 if (likely(l4proto
->nlattr_to_tuple
)) {
1358 ret
= nla_validate_nested_deprecated(attr
, CTA_PROTO_MAX
,
1359 l4proto
->nla_policy
,
1362 ret
= l4proto
->nlattr_to_tuple(tb
, tuple
, flags
);
1371 ctnetlink_parse_zone(const struct nlattr
*attr
,
1372 struct nf_conntrack_zone
*zone
)
1374 nf_ct_zone_init(zone
, NF_CT_DEFAULT_ZONE_ID
,
1375 NF_CT_DEFAULT_ZONE_DIR
, 0);
1376 #ifdef CONFIG_NF_CONNTRACK_ZONES
1378 zone
->id
= ntohs(nla_get_be16(attr
));
1387 ctnetlink_parse_tuple_zone(struct nlattr
*attr
, enum ctattr_type type
,
1388 struct nf_conntrack_zone
*zone
)
1392 if (zone
->id
!= NF_CT_DEFAULT_ZONE_ID
)
1395 ret
= ctnetlink_parse_zone(attr
, zone
);
1399 if (type
== CTA_TUPLE_REPLY
)
1400 zone
->dir
= NF_CT_ZONE_DIR_REPL
;
1402 zone
->dir
= NF_CT_ZONE_DIR_ORIG
;
1407 static const struct nla_policy tuple_nla_policy
[CTA_TUPLE_MAX
+1] = {
1408 [CTA_TUPLE_IP
] = { .type
= NLA_NESTED
},
1409 [CTA_TUPLE_PROTO
] = { .type
= NLA_NESTED
},
1410 [CTA_TUPLE_ZONE
] = { .type
= NLA_U16
},
1413 #define CTA_FILTER_F_ALL_CTA_PROTO \
1414 (CTA_FILTER_F_CTA_PROTO_SRC_PORT | \
1415 CTA_FILTER_F_CTA_PROTO_DST_PORT | \
1416 CTA_FILTER_F_CTA_PROTO_ICMP_TYPE | \
1417 CTA_FILTER_F_CTA_PROTO_ICMP_CODE | \
1418 CTA_FILTER_F_CTA_PROTO_ICMP_ID | \
1419 CTA_FILTER_F_CTA_PROTO_ICMPV6_TYPE | \
1420 CTA_FILTER_F_CTA_PROTO_ICMPV6_CODE | \
1421 CTA_FILTER_F_CTA_PROTO_ICMPV6_ID)
1424 ctnetlink_parse_tuple_filter(const struct nlattr
* const cda
[],
1425 struct nf_conntrack_tuple
*tuple
, u32 type
,
1426 u_int8_t l3num
, struct nf_conntrack_zone
*zone
,
1429 struct nlattr
*tb
[CTA_TUPLE_MAX
+1];
1432 memset(tuple
, 0, sizeof(*tuple
));
1434 err
= nla_parse_nested_deprecated(tb
, CTA_TUPLE_MAX
, cda
[type
],
1435 tuple_nla_policy
, NULL
);
1439 if (l3num
!= NFPROTO_IPV4
&& l3num
!= NFPROTO_IPV6
)
1441 tuple
->src
.l3num
= l3num
;
1443 if (flags
& CTA_FILTER_FLAG(CTA_IP_DST
) ||
1444 flags
& CTA_FILTER_FLAG(CTA_IP_SRC
)) {
1445 if (!tb
[CTA_TUPLE_IP
])
1448 err
= ctnetlink_parse_tuple_ip(tb
[CTA_TUPLE_IP
], tuple
, flags
);
1453 if (flags
& CTA_FILTER_FLAG(CTA_PROTO_NUM
)) {
1454 if (!tb
[CTA_TUPLE_PROTO
])
1457 err
= ctnetlink_parse_tuple_proto(tb
[CTA_TUPLE_PROTO
], tuple
, flags
);
1460 } else if (flags
& CTA_FILTER_FLAG(ALL_CTA_PROTO
)) {
1461 /* Can't manage proto flags without a protonum */
1465 if ((flags
& CTA_FILTER_FLAG(CTA_TUPLE_ZONE
)) && tb
[CTA_TUPLE_ZONE
]) {
1469 err
= ctnetlink_parse_tuple_zone(tb
[CTA_TUPLE_ZONE
],
1475 /* orig and expect tuples get DIR_ORIGINAL */
1476 if (type
== CTA_TUPLE_REPLY
)
1477 tuple
->dst
.dir
= IP_CT_DIR_REPLY
;
1479 tuple
->dst
.dir
= IP_CT_DIR_ORIGINAL
;
1485 ctnetlink_parse_tuple(const struct nlattr
* const cda
[],
1486 struct nf_conntrack_tuple
*tuple
, u32 type
,
1487 u_int8_t l3num
, struct nf_conntrack_zone
*zone
)
1489 return ctnetlink_parse_tuple_filter(cda
, tuple
, type
, l3num
, zone
,
1490 CTA_FILTER_FLAG(ALL
));
1493 static const struct nla_policy help_nla_policy
[CTA_HELP_MAX
+1] = {
1494 [CTA_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
1495 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
1498 static int ctnetlink_parse_help(const struct nlattr
*attr
, char **helper_name
,
1499 struct nlattr
**helpinfo
)
1502 struct nlattr
*tb
[CTA_HELP_MAX
+1];
1504 err
= nla_parse_nested_deprecated(tb
, CTA_HELP_MAX
, attr
,
1505 help_nla_policy
, NULL
);
1509 if (!tb
[CTA_HELP_NAME
])
1512 *helper_name
= nla_data(tb
[CTA_HELP_NAME
]);
1514 if (tb
[CTA_HELP_INFO
])
1515 *helpinfo
= tb
[CTA_HELP_INFO
];
1520 static const struct nla_policy ct_nla_policy
[CTA_MAX
+1] = {
1521 [CTA_TUPLE_ORIG
] = { .type
= NLA_NESTED
},
1522 [CTA_TUPLE_REPLY
] = { .type
= NLA_NESTED
},
1523 [CTA_STATUS
] = { .type
= NLA_U32
},
1524 [CTA_PROTOINFO
] = { .type
= NLA_NESTED
},
1525 [CTA_HELP
] = { .type
= NLA_NESTED
},
1526 [CTA_NAT_SRC
] = { .type
= NLA_NESTED
},
1527 [CTA_TIMEOUT
] = { .type
= NLA_U32
},
1528 [CTA_MARK
] = { .type
= NLA_U32
},
1529 [CTA_ID
] = { .type
= NLA_U32
},
1530 [CTA_NAT_DST
] = { .type
= NLA_NESTED
},
1531 [CTA_TUPLE_MASTER
] = { .type
= NLA_NESTED
},
1532 [CTA_NAT_SEQ_ADJ_ORIG
] = { .type
= NLA_NESTED
},
1533 [CTA_NAT_SEQ_ADJ_REPLY
] = { .type
= NLA_NESTED
},
1534 [CTA_ZONE
] = { .type
= NLA_U16
},
1535 [CTA_MARK_MASK
] = { .type
= NLA_U32
},
1536 [CTA_LABELS
] = { .type
= NLA_BINARY
,
1537 .len
= NF_CT_LABELS_MAX_SIZE
},
1538 [CTA_LABELS_MASK
] = { .type
= NLA_BINARY
,
1539 .len
= NF_CT_LABELS_MAX_SIZE
},
1540 [CTA_FILTER
] = { .type
= NLA_NESTED
},
1541 [CTA_STATUS_MASK
] = { .type
= NLA_U32
},
1544 static int ctnetlink_flush_iterate(struct nf_conn
*ct
, void *data
)
1546 if (test_bit(IPS_OFFLOAD_BIT
, &ct
->status
))
1549 return ctnetlink_filter_match(ct
, data
);
1552 static int ctnetlink_flush_conntrack(struct net
*net
,
1553 const struct nlattr
* const cda
[],
1554 u32 portid
, int report
, u8 family
)
1556 struct ctnetlink_filter
*filter
= NULL
;
1558 if (ctnetlink_needs_filter(family
, cda
)) {
1559 if (cda
[CTA_FILTER
])
1562 filter
= ctnetlink_alloc_filter(cda
, family
);
1564 return PTR_ERR(filter
);
1567 nf_ct_iterate_cleanup_net(net
, ctnetlink_flush_iterate
, filter
,
1574 static int ctnetlink_del_conntrack(struct sk_buff
*skb
,
1575 const struct nfnl_info
*info
,
1576 const struct nlattr
* const cda
[])
1578 u8 family
= info
->nfmsg
->nfgen_family
;
1579 struct nf_conntrack_tuple_hash
*h
;
1580 struct nf_conntrack_tuple tuple
;
1581 struct nf_conntrack_zone zone
;
1585 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1589 if (cda
[CTA_TUPLE_ORIG
])
1590 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1592 else if (cda
[CTA_TUPLE_REPLY
])
1593 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1596 u_int8_t u3
= info
->nfmsg
->version
? family
: AF_UNSPEC
;
1598 return ctnetlink_flush_conntrack(info
->net
, cda
,
1599 NETLINK_CB(skb
).portid
,
1600 nlmsg_report(info
->nlh
), u3
);
1606 h
= nf_conntrack_find_get(info
->net
, &zone
, &tuple
);
1610 ct
= nf_ct_tuplehash_to_ctrack(h
);
1612 if (test_bit(IPS_OFFLOAD_BIT
, &ct
->status
)) {
1618 __be32 id
= nla_get_be32(cda
[CTA_ID
]);
1620 if (id
!= (__force __be32
)nf_ct_get_id(ct
)) {
1626 nf_ct_delete(ct
, NETLINK_CB(skb
).portid
, nlmsg_report(info
->nlh
));
1632 static int ctnetlink_get_conntrack(struct sk_buff
*skb
,
1633 const struct nfnl_info
*info
,
1634 const struct nlattr
* const cda
[])
1636 u_int8_t u3
= info
->nfmsg
->nfgen_family
;
1637 struct nf_conntrack_tuple_hash
*h
;
1638 struct nf_conntrack_tuple tuple
;
1639 struct nf_conntrack_zone zone
;
1640 struct sk_buff
*skb2
;
1644 if (info
->nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1645 struct netlink_dump_control c
= {
1646 .start
= ctnetlink_start
,
1647 .dump
= ctnetlink_dump_table
,
1648 .done
= ctnetlink_done
,
1649 .data
= (void *)cda
,
1652 return netlink_dump_start(info
->sk
, skb
, info
->nlh
, &c
);
1655 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
1659 if (cda
[CTA_TUPLE_ORIG
])
1660 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_ORIG
,
1662 else if (cda
[CTA_TUPLE_REPLY
])
1663 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_TUPLE_REPLY
,
1671 h
= nf_conntrack_find_get(info
->net
, &zone
, &tuple
);
1675 ct
= nf_ct_tuplehash_to_ctrack(h
);
1677 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
1683 err
= ctnetlink_fill_info(skb2
, NETLINK_CB(skb
).portid
,
1684 info
->nlh
->nlmsg_seq
,
1685 NFNL_MSG_TYPE(info
->nlh
->nlmsg_type
), ct
,
1693 return nfnetlink_unicast(skb2
, info
->net
, NETLINK_CB(skb
).portid
);
1696 static int ctnetlink_done_list(struct netlink_callback
*cb
)
1699 nf_ct_put((struct nf_conn
*)cb
->args
[1]);
1704 ctnetlink_dump_list(struct sk_buff
*skb
, struct netlink_callback
*cb
, bool dying
)
1706 struct nf_conn
*ct
, *last
;
1707 struct nf_conntrack_tuple_hash
*h
;
1708 struct hlist_nulls_node
*n
;
1709 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
1710 u_int8_t l3proto
= nfmsg
->nfgen_family
;
1713 struct hlist_nulls_head
*list
;
1714 struct net
*net
= sock_net(skb
->sk
);
1719 last
= (struct nf_conn
*)cb
->args
[1];
1721 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
1722 struct ct_pcpu
*pcpu
;
1724 if (!cpu_possible(cpu
))
1727 pcpu
= per_cpu_ptr(net
->ct
.pcpu_lists
, cpu
);
1728 spin_lock_bh(&pcpu
->lock
);
1729 list
= dying
? &pcpu
->dying
: &pcpu
->unconfirmed
;
1731 hlist_nulls_for_each_entry(h
, n
, list
, hnnode
) {
1732 ct
= nf_ct_tuplehash_to_ctrack(h
);
1733 if (l3proto
&& nf_ct_l3num(ct
) != l3proto
)
1741 /* We can't dump extension info for the unconfirmed
1742 * list because unconfirmed conntracks can have
1743 * ct->ext reallocated (and thus freed).
1745 * In the dying list case ct->ext can't be free'd
1746 * until after we drop pcpu->lock.
1748 res
= ctnetlink_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
1750 NFNL_MSG_TYPE(cb
->nlh
->nlmsg_type
),
1751 ct
, dying
? true : false, 0);
1753 if (!atomic_inc_not_zero(&ct
->ct_general
.use
))
1756 cb
->args
[1] = (unsigned long)ct
;
1757 spin_unlock_bh(&pcpu
->lock
);
1765 spin_unlock_bh(&pcpu
->lock
);
1776 ctnetlink_dump_dying(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1778 return ctnetlink_dump_list(skb
, cb
, true);
1781 static int ctnetlink_get_ct_dying(struct sk_buff
*skb
,
1782 const struct nfnl_info
*info
,
1783 const struct nlattr
* const cda
[])
1785 if (info
->nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1786 struct netlink_dump_control c
= {
1787 .dump
= ctnetlink_dump_dying
,
1788 .done
= ctnetlink_done_list
,
1790 return netlink_dump_start(info
->sk
, skb
, info
->nlh
, &c
);
1797 ctnetlink_dump_unconfirmed(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1799 return ctnetlink_dump_list(skb
, cb
, false);
1802 static int ctnetlink_get_ct_unconfirmed(struct sk_buff
*skb
,
1803 const struct nfnl_info
*info
,
1804 const struct nlattr
* const cda
[])
1806 if (info
->nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1807 struct netlink_dump_control c
= {
1808 .dump
= ctnetlink_dump_unconfirmed
,
1809 .done
= ctnetlink_done_list
,
1811 return netlink_dump_start(info
->sk
, skb
, info
->nlh
, &c
);
1817 #if IS_ENABLED(CONFIG_NF_NAT)
1819 ctnetlink_parse_nat_setup(struct nf_conn
*ct
,
1820 enum nf_nat_manip_type manip
,
1821 const struct nlattr
*attr
)
1824 struct nf_nat_hook
*nat_hook
;
1827 nat_hook
= rcu_dereference(nf_nat_hook
);
1829 #ifdef CONFIG_MODULES
1831 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1832 if (request_module("nf-nat") < 0) {
1833 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1837 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1839 nat_hook
= rcu_dereference(nf_nat_hook
);
1846 err
= nat_hook
->parse_nat_setup(ct
, manip
, attr
);
1847 if (err
== -EAGAIN
) {
1848 #ifdef CONFIG_MODULES
1850 nfnl_unlock(NFNL_SUBSYS_CTNETLINK
);
1851 if (request_module("nf-nat-%u", nf_ct_l3num(ct
)) < 0) {
1852 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1856 nfnl_lock(NFNL_SUBSYS_CTNETLINK
);
1867 __ctnetlink_change_status(struct nf_conn
*ct
, unsigned long on
,
1872 /* Ignore these unchangable bits */
1873 on
&= ~IPS_UNCHANGEABLE_MASK
;
1874 off
&= ~IPS_UNCHANGEABLE_MASK
;
1876 for (bit
= 0; bit
< __IPS_MAX_BIT
; bit
++) {
1877 if (on
& (1 << bit
))
1878 set_bit(bit
, &ct
->status
);
1879 else if (off
& (1 << bit
))
1880 clear_bit(bit
, &ct
->status
);
1885 ctnetlink_change_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1888 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
1889 d
= ct
->status
^ status
;
1891 if (d
& (IPS_EXPECTED
|IPS_CONFIRMED
|IPS_DYING
))
1895 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
1896 /* SEEN_REPLY bit can only be set */
1899 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
1900 /* ASSURED bit can only be set */
1903 __ctnetlink_change_status(ct
, status
, 0);
1908 ctnetlink_setup_nat(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
1910 #if IS_ENABLED(CONFIG_NF_NAT)
1913 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1916 ret
= ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_DST
,
1921 return ctnetlink_parse_nat_setup(ct
, NF_NAT_MANIP_SRC
,
1924 if (!cda
[CTA_NAT_DST
] && !cda
[CTA_NAT_SRC
])
1930 static int ctnetlink_change_helper(struct nf_conn
*ct
,
1931 const struct nlattr
* const cda
[])
1933 struct nf_conntrack_helper
*helper
;
1934 struct nf_conn_help
*help
= nfct_help(ct
);
1935 char *helpname
= NULL
;
1936 struct nlattr
*helpinfo
= NULL
;
1939 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
1943 /* don't change helper of sibling connections */
1945 /* If we try to change the helper to the same thing twice,
1946 * treat the second attempt as a no-op instead of returning
1952 helper
= rcu_dereference(help
->helper
);
1953 if (helper
&& !strcmp(helper
->name
, helpname
))
1961 if (!strcmp(helpname
, "")) {
1962 if (help
&& help
->helper
) {
1963 /* we had a helper before ... */
1964 nf_ct_remove_expectations(ct
);
1965 RCU_INIT_POINTER(help
->helper
, NULL
);
1972 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
1973 nf_ct_protonum(ct
));
1974 if (helper
== NULL
) {
1980 if (help
->helper
== helper
) {
1981 /* update private helper data if allowed. */
1982 if (helper
->from_nlattr
)
1983 helper
->from_nlattr(helpinfo
, ct
);
1988 /* we cannot set a helper for an existing conntrack */
1996 static int ctnetlink_change_timeout(struct nf_conn
*ct
,
1997 const struct nlattr
* const cda
[])
1999 u64 timeout
= (u64
)ntohl(nla_get_be32(cda
[CTA_TIMEOUT
])) * HZ
;
2001 if (timeout
> INT_MAX
)
2003 ct
->timeout
= nfct_time_stamp
+ (u32
)timeout
;
2005 if (test_bit(IPS_DYING_BIT
, &ct
->status
))
2011 #if defined(CONFIG_NF_CONNTRACK_MARK)
2012 static void ctnetlink_change_mark(struct nf_conn
*ct
,
2013 const struct nlattr
* const cda
[])
2015 u32 mark
, newmark
, mask
= 0;
2017 if (cda
[CTA_MARK_MASK
])
2018 mask
= ~ntohl(nla_get_be32(cda
[CTA_MARK_MASK
]));
2020 mark
= ntohl(nla_get_be32(cda
[CTA_MARK
]));
2021 newmark
= (ct
->mark
& mask
) ^ mark
;
2022 if (newmark
!= ct
->mark
)
2027 static const struct nla_policy protoinfo_policy
[CTA_PROTOINFO_MAX
+1] = {
2028 [CTA_PROTOINFO_TCP
] = { .type
= NLA_NESTED
},
2029 [CTA_PROTOINFO_DCCP
] = { .type
= NLA_NESTED
},
2030 [CTA_PROTOINFO_SCTP
] = { .type
= NLA_NESTED
},
2033 static int ctnetlink_change_protoinfo(struct nf_conn
*ct
,
2034 const struct nlattr
* const cda
[])
2036 const struct nlattr
*attr
= cda
[CTA_PROTOINFO
];
2037 const struct nf_conntrack_l4proto
*l4proto
;
2038 struct nlattr
*tb
[CTA_PROTOINFO_MAX
+1];
2041 err
= nla_parse_nested_deprecated(tb
, CTA_PROTOINFO_MAX
, attr
,
2042 protoinfo_policy
, NULL
);
2046 l4proto
= nf_ct_l4proto_find(nf_ct_protonum(ct
));
2047 if (l4proto
->from_nlattr
)
2048 err
= l4proto
->from_nlattr(tb
, ct
);
2053 static const struct nla_policy seqadj_policy
[CTA_SEQADJ_MAX
+1] = {
2054 [CTA_SEQADJ_CORRECTION_POS
] = { .type
= NLA_U32
},
2055 [CTA_SEQADJ_OFFSET_BEFORE
] = { .type
= NLA_U32
},
2056 [CTA_SEQADJ_OFFSET_AFTER
] = { .type
= NLA_U32
},
2059 static int change_seq_adj(struct nf_ct_seqadj
*seq
,
2060 const struct nlattr
* const attr
)
2063 struct nlattr
*cda
[CTA_SEQADJ_MAX
+1];
2065 err
= nla_parse_nested_deprecated(cda
, CTA_SEQADJ_MAX
, attr
,
2066 seqadj_policy
, NULL
);
2070 if (!cda
[CTA_SEQADJ_CORRECTION_POS
])
2073 seq
->correction_pos
=
2074 ntohl(nla_get_be32(cda
[CTA_SEQADJ_CORRECTION_POS
]));
2076 if (!cda
[CTA_SEQADJ_OFFSET_BEFORE
])
2079 seq
->offset_before
=
2080 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_BEFORE
]));
2082 if (!cda
[CTA_SEQADJ_OFFSET_AFTER
])
2086 ntohl(nla_get_be32(cda
[CTA_SEQADJ_OFFSET_AFTER
]));
2092 ctnetlink_change_seq_adj(struct nf_conn
*ct
,
2093 const struct nlattr
* const cda
[])
2095 struct nf_conn_seqadj
*seqadj
= nfct_seqadj(ct
);
2101 spin_lock_bh(&ct
->lock
);
2102 if (cda
[CTA_SEQ_ADJ_ORIG
]) {
2103 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_ORIGINAL
],
2104 cda
[CTA_SEQ_ADJ_ORIG
]);
2108 set_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
);
2111 if (cda
[CTA_SEQ_ADJ_REPLY
]) {
2112 ret
= change_seq_adj(&seqadj
->seq
[IP_CT_DIR_REPLY
],
2113 cda
[CTA_SEQ_ADJ_REPLY
]);
2117 set_bit(IPS_SEQ_ADJUST_BIT
, &ct
->status
);
2120 spin_unlock_bh(&ct
->lock
);
2123 spin_unlock_bh(&ct
->lock
);
2127 static const struct nla_policy synproxy_policy
[CTA_SYNPROXY_MAX
+ 1] = {
2128 [CTA_SYNPROXY_ISN
] = { .type
= NLA_U32
},
2129 [CTA_SYNPROXY_ITS
] = { .type
= NLA_U32
},
2130 [CTA_SYNPROXY_TSOFF
] = { .type
= NLA_U32
},
2133 static int ctnetlink_change_synproxy(struct nf_conn
*ct
,
2134 const struct nlattr
* const cda
[])
2136 struct nf_conn_synproxy
*synproxy
= nfct_synproxy(ct
);
2137 struct nlattr
*tb
[CTA_SYNPROXY_MAX
+ 1];
2143 err
= nla_parse_nested_deprecated(tb
, CTA_SYNPROXY_MAX
,
2144 cda
[CTA_SYNPROXY
], synproxy_policy
,
2149 if (!tb
[CTA_SYNPROXY_ISN
] ||
2150 !tb
[CTA_SYNPROXY_ITS
] ||
2151 !tb
[CTA_SYNPROXY_TSOFF
])
2154 synproxy
->isn
= ntohl(nla_get_be32(tb
[CTA_SYNPROXY_ISN
]));
2155 synproxy
->its
= ntohl(nla_get_be32(tb
[CTA_SYNPROXY_ITS
]));
2156 synproxy
->tsoff
= ntohl(nla_get_be32(tb
[CTA_SYNPROXY_TSOFF
]));
2162 ctnetlink_attach_labels(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
2164 #ifdef CONFIG_NF_CONNTRACK_LABELS
2165 size_t len
= nla_len(cda
[CTA_LABELS
]);
2166 const void *mask
= cda
[CTA_LABELS_MASK
];
2168 if (len
& (sizeof(u32
)-1)) /* must be multiple of u32 */
2172 if (nla_len(cda
[CTA_LABELS_MASK
]) == 0 ||
2173 nla_len(cda
[CTA_LABELS_MASK
]) != len
)
2175 mask
= nla_data(cda
[CTA_LABELS_MASK
]);
2180 return nf_connlabels_replace(ct
, nla_data(cda
[CTA_LABELS
]), mask
, len
);
2187 ctnetlink_change_conntrack(struct nf_conn
*ct
,
2188 const struct nlattr
* const cda
[])
2192 /* only allow NAT changes and master assignation for new conntracks */
2193 if (cda
[CTA_NAT_SRC
] || cda
[CTA_NAT_DST
] || cda
[CTA_TUPLE_MASTER
])
2196 if (cda
[CTA_HELP
]) {
2197 err
= ctnetlink_change_helper(ct
, cda
);
2202 if (cda
[CTA_TIMEOUT
]) {
2203 err
= ctnetlink_change_timeout(ct
, cda
);
2208 if (cda
[CTA_STATUS
]) {
2209 err
= ctnetlink_change_status(ct
, cda
);
2214 if (cda
[CTA_PROTOINFO
]) {
2215 err
= ctnetlink_change_protoinfo(ct
, cda
);
2220 #if defined(CONFIG_NF_CONNTRACK_MARK)
2222 ctnetlink_change_mark(ct
, cda
);
2225 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
2226 err
= ctnetlink_change_seq_adj(ct
, cda
);
2231 if (cda
[CTA_SYNPROXY
]) {
2232 err
= ctnetlink_change_synproxy(ct
, cda
);
2237 if (cda
[CTA_LABELS
]) {
2238 err
= ctnetlink_attach_labels(ct
, cda
);
2246 static struct nf_conn
*
2247 ctnetlink_create_conntrack(struct net
*net
,
2248 const struct nf_conntrack_zone
*zone
,
2249 const struct nlattr
* const cda
[],
2250 struct nf_conntrack_tuple
*otuple
,
2251 struct nf_conntrack_tuple
*rtuple
,
2256 struct nf_conntrack_helper
*helper
;
2257 struct nf_conn_tstamp
*tstamp
;
2260 ct
= nf_conntrack_alloc(net
, zone
, otuple
, rtuple
, GFP_ATOMIC
);
2262 return ERR_PTR(-ENOMEM
);
2264 if (!cda
[CTA_TIMEOUT
])
2267 timeout
= (u64
)ntohl(nla_get_be32(cda
[CTA_TIMEOUT
])) * HZ
;
2268 if (timeout
> INT_MAX
)
2270 ct
->timeout
= (u32
)timeout
+ nfct_time_stamp
;
2273 if (cda
[CTA_HELP
]) {
2274 char *helpname
= NULL
;
2275 struct nlattr
*helpinfo
= NULL
;
2277 err
= ctnetlink_parse_help(cda
[CTA_HELP
], &helpname
, &helpinfo
);
2281 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2282 nf_ct_protonum(ct
));
2283 if (helper
== NULL
) {
2285 #ifdef CONFIG_MODULES
2286 if (request_module("nfct-helper-%s", helpname
) < 0) {
2292 helper
= __nf_conntrack_helper_find(helpname
,
2294 nf_ct_protonum(ct
));
2304 struct nf_conn_help
*help
;
2306 help
= nf_ct_helper_ext_add(ct
, GFP_ATOMIC
);
2311 /* set private helper data if allowed. */
2312 if (helper
->from_nlattr
)
2313 helper
->from_nlattr(helpinfo
, ct
);
2315 /* not in hash table yet so not strictly necessary */
2316 RCU_INIT_POINTER(help
->helper
, helper
);
2319 /* try an implicit helper assignation */
2320 err
= __nf_ct_try_assign_helper(ct
, NULL
, GFP_ATOMIC
);
2325 err
= ctnetlink_setup_nat(ct
, cda
);
2329 nf_ct_acct_ext_add(ct
, GFP_ATOMIC
);
2330 nf_ct_tstamp_ext_add(ct
, GFP_ATOMIC
);
2331 nf_ct_ecache_ext_add(ct
, 0, 0, GFP_ATOMIC
);
2332 nf_ct_labels_ext_add(ct
);
2333 nfct_seqadj_ext_add(ct
);
2334 nfct_synproxy_ext_add(ct
);
2336 /* we must add conntrack extensions before confirmation. */
2337 ct
->status
|= IPS_CONFIRMED
;
2339 if (cda
[CTA_STATUS
]) {
2340 err
= ctnetlink_change_status(ct
, cda
);
2345 if (cda
[CTA_SEQ_ADJ_ORIG
] || cda
[CTA_SEQ_ADJ_REPLY
]) {
2346 err
= ctnetlink_change_seq_adj(ct
, cda
);
2351 memset(&ct
->proto
, 0, sizeof(ct
->proto
));
2352 if (cda
[CTA_PROTOINFO
]) {
2353 err
= ctnetlink_change_protoinfo(ct
, cda
);
2358 if (cda
[CTA_SYNPROXY
]) {
2359 err
= ctnetlink_change_synproxy(ct
, cda
);
2364 #if defined(CONFIG_NF_CONNTRACK_MARK)
2366 ctnetlink_change_mark(ct
, cda
);
2369 /* setup master conntrack: this is a confirmed expectation */
2370 if (cda
[CTA_TUPLE_MASTER
]) {
2371 struct nf_conntrack_tuple master
;
2372 struct nf_conntrack_tuple_hash
*master_h
;
2373 struct nf_conn
*master_ct
;
2375 err
= ctnetlink_parse_tuple(cda
, &master
, CTA_TUPLE_MASTER
,
2380 master_h
= nf_conntrack_find_get(net
, zone
, &master
);
2381 if (master_h
== NULL
) {
2385 master_ct
= nf_ct_tuplehash_to_ctrack(master_h
);
2386 __set_bit(IPS_EXPECTED_BIT
, &ct
->status
);
2387 ct
->master
= master_ct
;
2389 tstamp
= nf_conn_tstamp_find(ct
);
2391 tstamp
->start
= ktime_get_real_ns();
2393 err
= nf_conntrack_hash_check_insert(ct
);
2404 nf_conntrack_free(ct
);
2405 return ERR_PTR(err
);
2408 static int ctnetlink_new_conntrack(struct sk_buff
*skb
,
2409 const struct nfnl_info
*info
,
2410 const struct nlattr
* const cda
[])
2412 struct nf_conntrack_tuple otuple
, rtuple
;
2413 struct nf_conntrack_tuple_hash
*h
= NULL
;
2414 u_int8_t u3
= info
->nfmsg
->nfgen_family
;
2415 struct nf_conntrack_zone zone
;
2419 err
= ctnetlink_parse_zone(cda
[CTA_ZONE
], &zone
);
2423 if (cda
[CTA_TUPLE_ORIG
]) {
2424 err
= ctnetlink_parse_tuple(cda
, &otuple
, CTA_TUPLE_ORIG
,
2430 if (cda
[CTA_TUPLE_REPLY
]) {
2431 err
= ctnetlink_parse_tuple(cda
, &rtuple
, CTA_TUPLE_REPLY
,
2437 if (cda
[CTA_TUPLE_ORIG
])
2438 h
= nf_conntrack_find_get(info
->net
, &zone
, &otuple
);
2439 else if (cda
[CTA_TUPLE_REPLY
])
2440 h
= nf_conntrack_find_get(info
->net
, &zone
, &rtuple
);
2444 if (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
) {
2445 enum ip_conntrack_events events
;
2447 if (!cda
[CTA_TUPLE_ORIG
] || !cda
[CTA_TUPLE_REPLY
])
2449 if (otuple
.dst
.protonum
!= rtuple
.dst
.protonum
)
2452 ct
= ctnetlink_create_conntrack(info
->net
, &zone
, cda
,
2453 &otuple
, &rtuple
, u3
);
2458 if (test_bit(IPS_EXPECTED_BIT
, &ct
->status
))
2459 events
= 1 << IPCT_RELATED
;
2461 events
= 1 << IPCT_NEW
;
2463 if (cda
[CTA_LABELS
] &&
2464 ctnetlink_attach_labels(ct
, cda
) == 0)
2465 events
|= (1 << IPCT_LABEL
);
2467 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
2468 (1 << IPCT_ASSURED
) |
2469 (1 << IPCT_HELPER
) |
2470 (1 << IPCT_PROTOINFO
) |
2471 (1 << IPCT_SEQADJ
) |
2473 (1 << IPCT_SYNPROXY
) |
2475 ct
, NETLINK_CB(skb
).portid
,
2476 nlmsg_report(info
->nlh
));
2482 /* implicit 'else' */
2485 ct
= nf_ct_tuplehash_to_ctrack(h
);
2486 if (!(info
->nlh
->nlmsg_flags
& NLM_F_EXCL
)) {
2487 err
= ctnetlink_change_conntrack(ct
, cda
);
2489 nf_conntrack_eventmask_report((1 << IPCT_REPLY
) |
2490 (1 << IPCT_ASSURED
) |
2491 (1 << IPCT_HELPER
) |
2493 (1 << IPCT_PROTOINFO
) |
2494 (1 << IPCT_SEQADJ
) |
2496 (1 << IPCT_SYNPROXY
),
2497 ct
, NETLINK_CB(skb
).portid
,
2498 nlmsg_report(info
->nlh
));
2507 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
2508 __u16 cpu
, const struct ip_conntrack_stat
*st
)
2510 struct nlmsghdr
*nlh
;
2511 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2513 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
,
2514 IPCTNL_MSG_CT_GET_STATS_CPU
);
2515 nlh
= nfnl_msg_put(skb
, portid
, seq
, event
, flags
, AF_UNSPEC
,
2516 NFNETLINK_V0
, htons(cpu
));
2520 if (nla_put_be32(skb
, CTA_STATS_FOUND
, htonl(st
->found
)) ||
2521 nla_put_be32(skb
, CTA_STATS_INVALID
, htonl(st
->invalid
)) ||
2522 nla_put_be32(skb
, CTA_STATS_INSERT
, htonl(st
->insert
)) ||
2523 nla_put_be32(skb
, CTA_STATS_INSERT_FAILED
,
2524 htonl(st
->insert_failed
)) ||
2525 nla_put_be32(skb
, CTA_STATS_DROP
, htonl(st
->drop
)) ||
2526 nla_put_be32(skb
, CTA_STATS_EARLY_DROP
, htonl(st
->early_drop
)) ||
2527 nla_put_be32(skb
, CTA_STATS_ERROR
, htonl(st
->error
)) ||
2528 nla_put_be32(skb
, CTA_STATS_SEARCH_RESTART
,
2529 htonl(st
->search_restart
)) ||
2530 nla_put_be32(skb
, CTA_STATS_CLASH_RESOLVE
,
2531 htonl(st
->clash_resolve
)) ||
2532 nla_put_be32(skb
, CTA_STATS_CHAIN_TOOLONG
,
2533 htonl(st
->chaintoolong
)))
2534 goto nla_put_failure
;
2536 nlmsg_end(skb
, nlh
);
2541 nlmsg_cancel(skb
, nlh
);
2546 ctnetlink_ct_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2549 struct net
*net
= sock_net(skb
->sk
);
2551 if (cb
->args
[0] == nr_cpu_ids
)
2554 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
2555 const struct ip_conntrack_stat
*st
;
2557 if (!cpu_possible(cpu
))
2560 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
2561 if (ctnetlink_ct_stat_cpu_fill_info(skb
,
2562 NETLINK_CB(cb
->skb
).portid
,
2572 static int ctnetlink_stat_ct_cpu(struct sk_buff
*skb
,
2573 const struct nfnl_info
*info
,
2574 const struct nlattr
* const cda
[])
2576 if (info
->nlh
->nlmsg_flags
& NLM_F_DUMP
) {
2577 struct netlink_dump_control c
= {
2578 .dump
= ctnetlink_ct_stat_cpu_dump
,
2580 return netlink_dump_start(info
->sk
, skb
, info
->nlh
, &c
);
2587 ctnetlink_stat_ct_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, u32 type
,
2590 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
2591 unsigned int nr_conntracks
;
2592 struct nlmsghdr
*nlh
;
2594 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
, IPCTNL_MSG_CT_GET_STATS
);
2595 nlh
= nfnl_msg_put(skb
, portid
, seq
, event
, flags
, AF_UNSPEC
,
2600 nr_conntracks
= nf_conntrack_count(net
);
2601 if (nla_put_be32(skb
, CTA_STATS_GLOBAL_ENTRIES
, htonl(nr_conntracks
)))
2602 goto nla_put_failure
;
2604 if (nla_put_be32(skb
, CTA_STATS_GLOBAL_MAX_ENTRIES
, htonl(nf_conntrack_max
)))
2605 goto nla_put_failure
;
2607 nlmsg_end(skb
, nlh
);
2612 nlmsg_cancel(skb
, nlh
);
2616 static int ctnetlink_stat_ct(struct sk_buff
*skb
, const struct nfnl_info
*info
,
2617 const struct nlattr
* const cda
[])
2619 struct sk_buff
*skb2
;
2622 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
2626 err
= ctnetlink_stat_ct_fill_info(skb2
, NETLINK_CB(skb
).portid
,
2627 info
->nlh
->nlmsg_seq
,
2628 NFNL_MSG_TYPE(info
->nlh
->nlmsg_type
),
2635 return nfnetlink_unicast(skb2
, info
->net
, NETLINK_CB(skb
).portid
);
2638 static const struct nla_policy exp_nla_policy
[CTA_EXPECT_MAX
+1] = {
2639 [CTA_EXPECT_MASTER
] = { .type
= NLA_NESTED
},
2640 [CTA_EXPECT_TUPLE
] = { .type
= NLA_NESTED
},
2641 [CTA_EXPECT_MASK
] = { .type
= NLA_NESTED
},
2642 [CTA_EXPECT_TIMEOUT
] = { .type
= NLA_U32
},
2643 [CTA_EXPECT_ID
] = { .type
= NLA_U32
},
2644 [CTA_EXPECT_HELP_NAME
] = { .type
= NLA_NUL_STRING
,
2645 .len
= NF_CT_HELPER_NAME_LEN
- 1 },
2646 [CTA_EXPECT_ZONE
] = { .type
= NLA_U16
},
2647 [CTA_EXPECT_FLAGS
] = { .type
= NLA_U32
},
2648 [CTA_EXPECT_CLASS
] = { .type
= NLA_U32
},
2649 [CTA_EXPECT_NAT
] = { .type
= NLA_NESTED
},
2650 [CTA_EXPECT_FN
] = { .type
= NLA_NUL_STRING
},
2653 static struct nf_conntrack_expect
*
2654 ctnetlink_alloc_expect(const struct nlattr
*const cda
[], struct nf_conn
*ct
,
2655 struct nf_conntrack_helper
*helper
,
2656 struct nf_conntrack_tuple
*tuple
,
2657 struct nf_conntrack_tuple
*mask
);
2659 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2661 ctnetlink_glue_build_size(const struct nf_conn
*ct
)
2663 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2664 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2665 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2666 + 3 * nla_total_size(sizeof(u_int8_t
)) /* CTA_PROTO_NUM */
2667 + nla_total_size(sizeof(u_int32_t
)) /* CTA_ID */
2668 + nla_total_size(sizeof(u_int32_t
)) /* CTA_STATUS */
2669 + nla_total_size(sizeof(u_int32_t
)) /* CTA_TIMEOUT */
2670 + nla_total_size(0) /* CTA_PROTOINFO */
2671 + nla_total_size(0) /* CTA_HELP */
2672 + nla_total_size(NF_CT_HELPER_NAME_LEN
) /* CTA_HELP_NAME */
2673 + ctnetlink_secctx_size(ct
)
2674 + ctnetlink_acct_size(ct
)
2675 + ctnetlink_timestamp_size(ct
)
2676 #if IS_ENABLED(CONFIG_NF_NAT)
2677 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2678 + 6 * nla_total_size(sizeof(u_int32_t
)) /* CTA_NAT_SEQ_OFFSET */
2680 #ifdef CONFIG_NF_CONNTRACK_MARK
2681 + nla_total_size(sizeof(u_int32_t
)) /* CTA_MARK */
2683 #ifdef CONFIG_NF_CONNTRACK_ZONES
2684 + nla_total_size(sizeof(u_int16_t
)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2686 + ctnetlink_proto_size(ct
)
2690 static int __ctnetlink_glue_build(struct sk_buff
*skb
, struct nf_conn
*ct
)
2692 const struct nf_conntrack_zone
*zone
;
2693 struct nlattr
*nest_parms
;
2695 zone
= nf_ct_zone(ct
);
2697 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_ORIG
);
2699 goto nla_put_failure
;
2700 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_ORIGINAL
)) < 0)
2701 goto nla_put_failure
;
2702 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2703 NF_CT_ZONE_DIR_ORIG
) < 0)
2704 goto nla_put_failure
;
2705 nla_nest_end(skb
, nest_parms
);
2707 nest_parms
= nla_nest_start(skb
, CTA_TUPLE_REPLY
);
2709 goto nla_put_failure
;
2710 if (ctnetlink_dump_tuples(skb
, nf_ct_tuple(ct
, IP_CT_DIR_REPLY
)) < 0)
2711 goto nla_put_failure
;
2712 if (ctnetlink_dump_zone_id(skb
, CTA_TUPLE_ZONE
, zone
,
2713 NF_CT_ZONE_DIR_REPL
) < 0)
2714 goto nla_put_failure
;
2715 nla_nest_end(skb
, nest_parms
);
2717 if (ctnetlink_dump_zone_id(skb
, CTA_ZONE
, zone
,
2718 NF_CT_DEFAULT_ZONE_DIR
) < 0)
2719 goto nla_put_failure
;
2721 if (ctnetlink_dump_id(skb
, ct
) < 0)
2722 goto nla_put_failure
;
2724 if (ctnetlink_dump_status(skb
, ct
) < 0)
2725 goto nla_put_failure
;
2727 if (ctnetlink_dump_timeout(skb
, ct
, false) < 0)
2728 goto nla_put_failure
;
2730 if (ctnetlink_dump_protoinfo(skb
, ct
, false) < 0)
2731 goto nla_put_failure
;
2733 if (ctnetlink_dump_acct(skb
, ct
, IPCTNL_MSG_CT_GET
) < 0 ||
2734 ctnetlink_dump_timestamp(skb
, ct
) < 0)
2735 goto nla_put_failure
;
2737 if (ctnetlink_dump_helpinfo(skb
, ct
) < 0)
2738 goto nla_put_failure
;
2740 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2741 if (ct
->secmark
&& ctnetlink_dump_secctx(skb
, ct
) < 0)
2742 goto nla_put_failure
;
2744 if (ct
->master
&& ctnetlink_dump_master(skb
, ct
) < 0)
2745 goto nla_put_failure
;
2747 if ((ct
->status
& IPS_SEQ_ADJUST
) &&
2748 ctnetlink_dump_ct_seq_adj(skb
, ct
) < 0)
2749 goto nla_put_failure
;
2751 if (ctnetlink_dump_ct_synproxy(skb
, ct
) < 0)
2752 goto nla_put_failure
;
2754 #ifdef CONFIG_NF_CONNTRACK_MARK
2755 if (ct
->mark
&& ctnetlink_dump_mark(skb
, ct
) < 0)
2756 goto nla_put_failure
;
2758 if (ctnetlink_dump_labels(skb
, ct
) < 0)
2759 goto nla_put_failure
;
2767 ctnetlink_glue_build(struct sk_buff
*skb
, struct nf_conn
*ct
,
2768 enum ip_conntrack_info ctinfo
,
2769 u_int16_t ct_attr
, u_int16_t ct_info_attr
)
2771 struct nlattr
*nest_parms
;
2773 nest_parms
= nla_nest_start(skb
, ct_attr
);
2775 goto nla_put_failure
;
2777 if (__ctnetlink_glue_build(skb
, ct
) < 0)
2778 goto nla_put_failure
;
2780 nla_nest_end(skb
, nest_parms
);
2782 if (nla_put_be32(skb
, ct_info_attr
, htonl(ctinfo
)))
2783 goto nla_put_failure
;
2792 ctnetlink_update_status(struct nf_conn
*ct
, const struct nlattr
* const cda
[])
2794 unsigned int status
= ntohl(nla_get_be32(cda
[CTA_STATUS
]));
2795 unsigned long d
= ct
->status
^ status
;
2797 if (d
& IPS_SEEN_REPLY
&& !(status
& IPS_SEEN_REPLY
))
2798 /* SEEN_REPLY bit can only be set */
2801 if (d
& IPS_ASSURED
&& !(status
& IPS_ASSURED
))
2802 /* ASSURED bit can only be set */
2805 /* This check is less strict than ctnetlink_change_status()
2806 * because callers often flip IPS_EXPECTED bits when sending
2807 * an NFQA_CT attribute to the kernel. So ignore the
2808 * unchangeable bits but do not error out. Also user programs
2809 * are allowed to clear the bits that they are allowed to change.
2811 __ctnetlink_change_status(ct
, status
, ~status
);
2816 ctnetlink_glue_parse_ct(const struct nlattr
*cda
[], struct nf_conn
*ct
)
2820 if (cda
[CTA_TIMEOUT
]) {
2821 err
= ctnetlink_change_timeout(ct
, cda
);
2825 if (cda
[CTA_STATUS
]) {
2826 err
= ctnetlink_update_status(ct
, cda
);
2830 if (cda
[CTA_HELP
]) {
2831 err
= ctnetlink_change_helper(ct
, cda
);
2835 if (cda
[CTA_LABELS
]) {
2836 err
= ctnetlink_attach_labels(ct
, cda
);
2840 #if defined(CONFIG_NF_CONNTRACK_MARK)
2841 if (cda
[CTA_MARK
]) {
2842 ctnetlink_change_mark(ct
, cda
);
2849 ctnetlink_glue_parse(const struct nlattr
*attr
, struct nf_conn
*ct
)
2851 struct nlattr
*cda
[CTA_MAX
+1];
2854 ret
= nla_parse_nested_deprecated(cda
, CTA_MAX
, attr
, ct_nla_policy
,
2859 return ctnetlink_glue_parse_ct((const struct nlattr
**)cda
, ct
);
2862 static int ctnetlink_glue_exp_parse(const struct nlattr
* const *cda
,
2863 const struct nf_conn
*ct
,
2864 struct nf_conntrack_tuple
*tuple
,
2865 struct nf_conntrack_tuple
*mask
)
2869 err
= ctnetlink_parse_tuple(cda
, tuple
, CTA_EXPECT_TUPLE
,
2870 nf_ct_l3num(ct
), NULL
);
2874 return ctnetlink_parse_tuple(cda
, mask
, CTA_EXPECT_MASK
,
2875 nf_ct_l3num(ct
), NULL
);
2879 ctnetlink_glue_attach_expect(const struct nlattr
*attr
, struct nf_conn
*ct
,
2880 u32 portid
, u32 report
)
2882 struct nlattr
*cda
[CTA_EXPECT_MAX
+1];
2883 struct nf_conntrack_tuple tuple
, mask
;
2884 struct nf_conntrack_helper
*helper
= NULL
;
2885 struct nf_conntrack_expect
*exp
;
2888 err
= nla_parse_nested_deprecated(cda
, CTA_EXPECT_MAX
, attr
,
2889 exp_nla_policy
, NULL
);
2893 err
= ctnetlink_glue_exp_parse((const struct nlattr
* const *)cda
,
2898 if (cda
[CTA_EXPECT_HELP_NAME
]) {
2899 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
2901 helper
= __nf_conntrack_helper_find(helpname
, nf_ct_l3num(ct
),
2902 nf_ct_protonum(ct
));
2907 exp
= ctnetlink_alloc_expect((const struct nlattr
* const *)cda
, ct
,
2908 helper
, &tuple
, &mask
);
2910 return PTR_ERR(exp
);
2912 err
= nf_ct_expect_related_report(exp
, portid
, report
, 0);
2913 nf_ct_expect_put(exp
);
2917 static void ctnetlink_glue_seqadj(struct sk_buff
*skb
, struct nf_conn
*ct
,
2918 enum ip_conntrack_info ctinfo
, int diff
)
2920 if (!(ct
->status
& IPS_NAT_MASK
))
2923 nf_ct_tcp_seqadj_set(skb
, ct
, ctinfo
, diff
);
2926 static struct nfnl_ct_hook ctnetlink_glue_hook
= {
2927 .build_size
= ctnetlink_glue_build_size
,
2928 .build
= ctnetlink_glue_build
,
2929 .parse
= ctnetlink_glue_parse
,
2930 .attach_expect
= ctnetlink_glue_attach_expect
,
2931 .seq_adjust
= ctnetlink_glue_seqadj
,
2933 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2935 /***********************************************************************
2937 ***********************************************************************/
2939 static int ctnetlink_exp_dump_tuple(struct sk_buff
*skb
,
2940 const struct nf_conntrack_tuple
*tuple
,
2943 struct nlattr
*nest_parms
;
2945 nest_parms
= nla_nest_start(skb
, type
);
2947 goto nla_put_failure
;
2948 if (ctnetlink_dump_tuples(skb
, tuple
) < 0)
2949 goto nla_put_failure
;
2950 nla_nest_end(skb
, nest_parms
);
2958 static int ctnetlink_exp_dump_mask(struct sk_buff
*skb
,
2959 const struct nf_conntrack_tuple
*tuple
,
2960 const struct nf_conntrack_tuple_mask
*mask
)
2962 const struct nf_conntrack_l4proto
*l4proto
;
2963 struct nf_conntrack_tuple m
;
2964 struct nlattr
*nest_parms
;
2967 memset(&m
, 0xFF, sizeof(m
));
2968 memcpy(&m
.src
.u3
, &mask
->src
.u3
, sizeof(m
.src
.u3
));
2969 m
.src
.u
.all
= mask
->src
.u
.all
;
2970 m
.src
.l3num
= tuple
->src
.l3num
;
2971 m
.dst
.protonum
= tuple
->dst
.protonum
;
2973 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_MASK
);
2975 goto nla_put_failure
;
2978 ret
= ctnetlink_dump_tuples_ip(skb
, &m
);
2980 l4proto
= nf_ct_l4proto_find(tuple
->dst
.protonum
);
2981 ret
= ctnetlink_dump_tuples_proto(skb
, &m
, l4proto
);
2985 if (unlikely(ret
< 0))
2986 goto nla_put_failure
;
2988 nla_nest_end(skb
, nest_parms
);
2996 static const union nf_inet_addr any_addr
;
2998 static __be32
nf_expect_get_id(const struct nf_conntrack_expect
*exp
)
3000 static __read_mostly siphash_key_t exp_id_seed
;
3001 unsigned long a
, b
, c
, d
;
3003 net_get_random_once(&exp_id_seed
, sizeof(exp_id_seed
));
3005 a
= (unsigned long)exp
;
3006 b
= (unsigned long)exp
->helper
;
3007 c
= (unsigned long)exp
->master
;
3008 d
= (unsigned long)siphash(&exp
->tuple
, sizeof(exp
->tuple
), &exp_id_seed
);
3011 return (__force __be32
)siphash_4u64((u64
)a
, (u64
)b
, (u64
)c
, (u64
)d
, &exp_id_seed
);
3013 return (__force __be32
)siphash_4u32((u32
)a
, (u32
)b
, (u32
)c
, (u32
)d
, &exp_id_seed
);
3018 ctnetlink_exp_dump_expect(struct sk_buff
*skb
,
3019 const struct nf_conntrack_expect
*exp
)
3021 struct nf_conn
*master
= exp
->master
;
3022 long timeout
= ((long)exp
->timeout
.expires
- (long)jiffies
) / HZ
;
3023 struct nf_conn_help
*help
;
3024 #if IS_ENABLED(CONFIG_NF_NAT)
3025 struct nlattr
*nest_parms
;
3026 struct nf_conntrack_tuple nat_tuple
= {};
3028 struct nf_ct_helper_expectfn
*expfn
;
3033 if (ctnetlink_exp_dump_tuple(skb
, &exp
->tuple
, CTA_EXPECT_TUPLE
) < 0)
3034 goto nla_put_failure
;
3035 if (ctnetlink_exp_dump_mask(skb
, &exp
->tuple
, &exp
->mask
) < 0)
3036 goto nla_put_failure
;
3037 if (ctnetlink_exp_dump_tuple(skb
,
3038 &master
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
,
3039 CTA_EXPECT_MASTER
) < 0)
3040 goto nla_put_failure
;
3042 #if IS_ENABLED(CONFIG_NF_NAT)
3043 if (!nf_inet_addr_cmp(&exp
->saved_addr
, &any_addr
) ||
3044 exp
->saved_proto
.all
) {
3045 nest_parms
= nla_nest_start(skb
, CTA_EXPECT_NAT
);
3047 goto nla_put_failure
;
3049 if (nla_put_be32(skb
, CTA_EXPECT_NAT_DIR
, htonl(exp
->dir
)))
3050 goto nla_put_failure
;
3052 nat_tuple
.src
.l3num
= nf_ct_l3num(master
);
3053 nat_tuple
.src
.u3
= exp
->saved_addr
;
3054 nat_tuple
.dst
.protonum
= nf_ct_protonum(master
);
3055 nat_tuple
.src
.u
= exp
->saved_proto
;
3057 if (ctnetlink_exp_dump_tuple(skb
, &nat_tuple
,
3058 CTA_EXPECT_NAT_TUPLE
) < 0)
3059 goto nla_put_failure
;
3060 nla_nest_end(skb
, nest_parms
);
3063 if (nla_put_be32(skb
, CTA_EXPECT_TIMEOUT
, htonl(timeout
)) ||
3064 nla_put_be32(skb
, CTA_EXPECT_ID
, nf_expect_get_id(exp
)) ||
3065 nla_put_be32(skb
, CTA_EXPECT_FLAGS
, htonl(exp
->flags
)) ||
3066 nla_put_be32(skb
, CTA_EXPECT_CLASS
, htonl(exp
->class)))
3067 goto nla_put_failure
;
3068 help
= nfct_help(master
);
3070 struct nf_conntrack_helper
*helper
;
3072 helper
= rcu_dereference(help
->helper
);
3074 nla_put_string(skb
, CTA_EXPECT_HELP_NAME
, helper
->name
))
3075 goto nla_put_failure
;
3077 expfn
= nf_ct_helper_expectfn_find_by_symbol(exp
->expectfn
);
3078 if (expfn
!= NULL
&&
3079 nla_put_string(skb
, CTA_EXPECT_FN
, expfn
->name
))
3080 goto nla_put_failure
;
3089 ctnetlink_exp_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
,
3090 int event
, const struct nf_conntrack_expect
*exp
)
3092 struct nlmsghdr
*nlh
;
3093 unsigned int flags
= portid
? NLM_F_MULTI
: 0;
3095 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP
, event
);
3096 nlh
= nfnl_msg_put(skb
, portid
, seq
, event
, flags
,
3097 exp
->tuple
.src
.l3num
, NFNETLINK_V0
, 0);
3101 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
3102 goto nla_put_failure
;
3104 nlmsg_end(skb
, nlh
);
3109 nlmsg_cancel(skb
, nlh
);
3113 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3115 ctnetlink_expect_event(unsigned int events
, const struct nf_exp_event
*item
)
3117 struct nf_conntrack_expect
*exp
= item
->exp
;
3118 struct net
*net
= nf_ct_exp_net(exp
);
3119 struct nlmsghdr
*nlh
;
3120 struct sk_buff
*skb
;
3121 unsigned int type
, group
;
3124 if (events
& (1 << IPEXP_DESTROY
)) {
3125 type
= IPCTNL_MSG_EXP_DELETE
;
3126 group
= NFNLGRP_CONNTRACK_EXP_DESTROY
;
3127 } else if (events
& (1 << IPEXP_NEW
)) {
3128 type
= IPCTNL_MSG_EXP_NEW
;
3129 flags
= NLM_F_CREATE
|NLM_F_EXCL
;
3130 group
= NFNLGRP_CONNTRACK_EXP_NEW
;
3134 if (!item
->report
&& !nfnetlink_has_listeners(net
, group
))
3137 skb
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_ATOMIC
);
3141 type
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP
, type
);
3142 nlh
= nfnl_msg_put(skb
, item
->portid
, 0, type
, flags
,
3143 exp
->tuple
.src
.l3num
, NFNETLINK_V0
, 0);
3147 if (ctnetlink_exp_dump_expect(skb
, exp
) < 0)
3148 goto nla_put_failure
;
3150 nlmsg_end(skb
, nlh
);
3151 nfnetlink_send(skb
, net
, item
->portid
, group
, item
->report
, GFP_ATOMIC
);
3155 nlmsg_cancel(skb
, nlh
);
3159 nfnetlink_set_err(net
, 0, 0, -ENOBUFS
);
3163 static int ctnetlink_exp_done(struct netlink_callback
*cb
)
3166 nf_ct_expect_put((struct nf_conntrack_expect
*)cb
->args
[1]);
3171 ctnetlink_exp_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3173 struct net
*net
= sock_net(skb
->sk
);
3174 struct nf_conntrack_expect
*exp
, *last
;
3175 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
3176 u_int8_t l3proto
= nfmsg
->nfgen_family
;
3179 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
3180 for (; cb
->args
[0] < nf_ct_expect_hsize
; cb
->args
[0]++) {
3182 hlist_for_each_entry_rcu(exp
, &nf_ct_expect_hash
[cb
->args
[0]],
3184 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
3187 if (!net_eq(nf_ct_net(exp
->master
), net
))
3195 if (ctnetlink_exp_fill_info(skb
,
3196 NETLINK_CB(cb
->skb
).portid
,
3200 if (!refcount_inc_not_zero(&exp
->use
))
3202 cb
->args
[1] = (unsigned long)exp
;
3214 nf_ct_expect_put(last
);
3220 ctnetlink_exp_ct_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3222 struct nf_conntrack_expect
*exp
, *last
;
3223 struct nfgenmsg
*nfmsg
= nlmsg_data(cb
->nlh
);
3224 struct nf_conn
*ct
= cb
->data
;
3225 struct nf_conn_help
*help
= nfct_help(ct
);
3226 u_int8_t l3proto
= nfmsg
->nfgen_family
;
3232 last
= (struct nf_conntrack_expect
*)cb
->args
[1];
3234 hlist_for_each_entry_rcu(exp
, &help
->expectations
, lnode
) {
3235 if (l3proto
&& exp
->tuple
.src
.l3num
!= l3proto
)
3242 if (ctnetlink_exp_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
3246 if (!refcount_inc_not_zero(&exp
->use
))
3248 cb
->args
[1] = (unsigned long)exp
;
3260 nf_ct_expect_put(last
);
3265 static int ctnetlink_dump_exp_ct(struct net
*net
, struct sock
*ctnl
,
3266 struct sk_buff
*skb
,
3267 const struct nlmsghdr
*nlh
,
3268 const struct nlattr
* const cda
[],
3269 struct netlink_ext_ack
*extack
)
3272 struct nfgenmsg
*nfmsg
= nlmsg_data(nlh
);
3273 u_int8_t u3
= nfmsg
->nfgen_family
;
3274 struct nf_conntrack_tuple tuple
;
3275 struct nf_conntrack_tuple_hash
*h
;
3277 struct nf_conntrack_zone zone
;
3278 struct netlink_dump_control c
= {
3279 .dump
= ctnetlink_exp_ct_dump_table
,
3280 .done
= ctnetlink_exp_done
,
3283 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
3288 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3292 h
= nf_conntrack_find_get(net
, &zone
, &tuple
);
3296 ct
= nf_ct_tuplehash_to_ctrack(h
);
3297 /* No expectation linked to this connection tracking. */
3298 if (!nfct_help(ct
)) {
3305 err
= netlink_dump_start(ctnl
, skb
, nlh
, &c
);
3311 static int ctnetlink_get_expect(struct sk_buff
*skb
,
3312 const struct nfnl_info
*info
,
3313 const struct nlattr
* const cda
[])
3315 u_int8_t u3
= info
->nfmsg
->nfgen_family
;
3316 struct nf_conntrack_tuple tuple
;
3317 struct nf_conntrack_expect
*exp
;
3318 struct nf_conntrack_zone zone
;
3319 struct sk_buff
*skb2
;
3322 if (info
->nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3323 if (cda
[CTA_EXPECT_MASTER
])
3324 return ctnetlink_dump_exp_ct(info
->net
, info
->sk
, skb
,
3328 struct netlink_dump_control c
= {
3329 .dump
= ctnetlink_exp_dump_table
,
3330 .done
= ctnetlink_exp_done
,
3332 return netlink_dump_start(info
->sk
, skb
, info
->nlh
, &c
);
3336 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3340 if (cda
[CTA_EXPECT_TUPLE
])
3341 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3343 else if (cda
[CTA_EXPECT_MASTER
])
3344 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_MASTER
,
3352 exp
= nf_ct_expect_find_get(info
->net
, &zone
, &tuple
);
3356 if (cda
[CTA_EXPECT_ID
]) {
3357 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
3359 if (id
!= nf_expect_get_id(exp
)) {
3360 nf_ct_expect_put(exp
);
3365 skb2
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
3367 nf_ct_expect_put(exp
);
3372 err
= ctnetlink_exp_fill_info(skb2
, NETLINK_CB(skb
).portid
,
3373 info
->nlh
->nlmsg_seq
, IPCTNL_MSG_EXP_NEW
,
3376 nf_ct_expect_put(exp
);
3382 return nfnetlink_unicast(skb2
, info
->net
, NETLINK_CB(skb
).portid
);
3385 static bool expect_iter_name(struct nf_conntrack_expect
*exp
, void *data
)
3387 const struct nf_conn_help
*m_help
;
3388 const char *name
= data
;
3390 m_help
= nfct_help(exp
->master
);
3392 return strcmp(m_help
->helper
->name
, name
) == 0;
3395 static bool expect_iter_all(struct nf_conntrack_expect
*exp
, void *data
)
3400 static int ctnetlink_del_expect(struct sk_buff
*skb
,
3401 const struct nfnl_info
*info
,
3402 const struct nlattr
* const cda
[])
3404 u_int8_t u3
= info
->nfmsg
->nfgen_family
;
3405 struct nf_conntrack_expect
*exp
;
3406 struct nf_conntrack_tuple tuple
;
3407 struct nf_conntrack_zone zone
;
3410 if (cda
[CTA_EXPECT_TUPLE
]) {
3411 /* delete a single expect by tuple */
3412 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3416 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3421 /* bump usage count to 2 */
3422 exp
= nf_ct_expect_find_get(info
->net
, &zone
, &tuple
);
3426 if (cda
[CTA_EXPECT_ID
]) {
3427 __be32 id
= nla_get_be32(cda
[CTA_EXPECT_ID
]);
3428 if (ntohl(id
) != (u32
)(unsigned long)exp
) {
3429 nf_ct_expect_put(exp
);
3434 /* after list removal, usage count == 1 */
3435 spin_lock_bh(&nf_conntrack_expect_lock
);
3436 if (del_timer(&exp
->timeout
)) {
3437 nf_ct_unlink_expect_report(exp
, NETLINK_CB(skb
).portid
,
3438 nlmsg_report(info
->nlh
));
3439 nf_ct_expect_put(exp
);
3441 spin_unlock_bh(&nf_conntrack_expect_lock
);
3442 /* have to put what we 'get' above.
3443 * after this line usage count == 0 */
3444 nf_ct_expect_put(exp
);
3445 } else if (cda
[CTA_EXPECT_HELP_NAME
]) {
3446 char *name
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
3448 nf_ct_expect_iterate_net(info
->net
, expect_iter_name
, name
,
3449 NETLINK_CB(skb
).portid
,
3450 nlmsg_report(info
->nlh
));
3452 /* This basically means we have to flush everything*/
3453 nf_ct_expect_iterate_net(info
->net
, expect_iter_all
, NULL
,
3454 NETLINK_CB(skb
).portid
,
3455 nlmsg_report(info
->nlh
));
3461 ctnetlink_change_expect(struct nf_conntrack_expect
*x
,
3462 const struct nlattr
* const cda
[])
3464 if (cda
[CTA_EXPECT_TIMEOUT
]) {
3465 if (!del_timer(&x
->timeout
))
3468 x
->timeout
.expires
= jiffies
+
3469 ntohl(nla_get_be32(cda
[CTA_EXPECT_TIMEOUT
])) * HZ
;
3470 add_timer(&x
->timeout
);
3475 static const struct nla_policy exp_nat_nla_policy
[CTA_EXPECT_NAT_MAX
+1] = {
3476 [CTA_EXPECT_NAT_DIR
] = { .type
= NLA_U32
},
3477 [CTA_EXPECT_NAT_TUPLE
] = { .type
= NLA_NESTED
},
3481 ctnetlink_parse_expect_nat(const struct nlattr
*attr
,
3482 struct nf_conntrack_expect
*exp
,
3485 #if IS_ENABLED(CONFIG_NF_NAT)
3486 struct nlattr
*tb
[CTA_EXPECT_NAT_MAX
+1];
3487 struct nf_conntrack_tuple nat_tuple
= {};
3490 err
= nla_parse_nested_deprecated(tb
, CTA_EXPECT_NAT_MAX
, attr
,
3491 exp_nat_nla_policy
, NULL
);
3495 if (!tb
[CTA_EXPECT_NAT_DIR
] || !tb
[CTA_EXPECT_NAT_TUPLE
])
3498 err
= ctnetlink_parse_tuple((const struct nlattr
* const *)tb
,
3499 &nat_tuple
, CTA_EXPECT_NAT_TUPLE
,
3504 exp
->saved_addr
= nat_tuple
.src
.u3
;
3505 exp
->saved_proto
= nat_tuple
.src
.u
;
3506 exp
->dir
= ntohl(nla_get_be32(tb
[CTA_EXPECT_NAT_DIR
]));
3514 static struct nf_conntrack_expect
*
3515 ctnetlink_alloc_expect(const struct nlattr
* const cda
[], struct nf_conn
*ct
,
3516 struct nf_conntrack_helper
*helper
,
3517 struct nf_conntrack_tuple
*tuple
,
3518 struct nf_conntrack_tuple
*mask
)
3520 u_int32_t
class = 0;
3521 struct nf_conntrack_expect
*exp
;
3522 struct nf_conn_help
*help
;
3525 help
= nfct_help(ct
);
3527 return ERR_PTR(-EOPNOTSUPP
);
3529 if (cda
[CTA_EXPECT_CLASS
] && helper
) {
3530 class = ntohl(nla_get_be32(cda
[CTA_EXPECT_CLASS
]));
3531 if (class > helper
->expect_class_max
)
3532 return ERR_PTR(-EINVAL
);
3534 exp
= nf_ct_expect_alloc(ct
);
3536 return ERR_PTR(-ENOMEM
);
3538 if (cda
[CTA_EXPECT_FLAGS
]) {
3539 exp
->flags
= ntohl(nla_get_be32(cda
[CTA_EXPECT_FLAGS
]));
3540 exp
->flags
&= ~NF_CT_EXPECT_USERSPACE
;
3544 if (cda
[CTA_EXPECT_FN
]) {
3545 const char *name
= nla_data(cda
[CTA_EXPECT_FN
]);
3546 struct nf_ct_helper_expectfn
*expfn
;
3548 expfn
= nf_ct_helper_expectfn_find_by_name(name
);
3549 if (expfn
== NULL
) {
3553 exp
->expectfn
= expfn
->expectfn
;
3555 exp
->expectfn
= NULL
;
3559 exp
->helper
= helper
;
3560 exp
->tuple
= *tuple
;
3561 exp
->mask
.src
.u3
= mask
->src
.u3
;
3562 exp
->mask
.src
.u
.all
= mask
->src
.u
.all
;
3564 if (cda
[CTA_EXPECT_NAT
]) {
3565 err
= ctnetlink_parse_expect_nat(cda
[CTA_EXPECT_NAT
],
3566 exp
, nf_ct_l3num(ct
));
3572 nf_ct_expect_put(exp
);
3573 return ERR_PTR(err
);
3577 ctnetlink_create_expect(struct net
*net
,
3578 const struct nf_conntrack_zone
*zone
,
3579 const struct nlattr
* const cda
[],
3580 u_int8_t u3
, u32 portid
, int report
)
3582 struct nf_conntrack_tuple tuple
, mask
, master_tuple
;
3583 struct nf_conntrack_tuple_hash
*h
= NULL
;
3584 struct nf_conntrack_helper
*helper
= NULL
;
3585 struct nf_conntrack_expect
*exp
;
3589 /* caller guarantees that those three CTA_EXPECT_* exist */
3590 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3594 err
= ctnetlink_parse_tuple(cda
, &mask
, CTA_EXPECT_MASK
,
3598 err
= ctnetlink_parse_tuple(cda
, &master_tuple
, CTA_EXPECT_MASTER
,
3603 /* Look for master conntrack of this expectation */
3604 h
= nf_conntrack_find_get(net
, zone
, &master_tuple
);
3607 ct
= nf_ct_tuplehash_to_ctrack(h
);
3610 if (cda
[CTA_EXPECT_HELP_NAME
]) {
3611 const char *helpname
= nla_data(cda
[CTA_EXPECT_HELP_NAME
]);
3613 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3614 nf_ct_protonum(ct
));
3615 if (helper
== NULL
) {
3617 #ifdef CONFIG_MODULES
3618 if (request_module("nfct-helper-%s", helpname
) < 0) {
3623 helper
= __nf_conntrack_helper_find(helpname
, u3
,
3624 nf_ct_protonum(ct
));
3636 exp
= ctnetlink_alloc_expect(cda
, ct
, helper
, &tuple
, &mask
);
3642 err
= nf_ct_expect_related_report(exp
, portid
, report
, 0);
3643 nf_ct_expect_put(exp
);
3651 static int ctnetlink_new_expect(struct sk_buff
*skb
,
3652 const struct nfnl_info
*info
,
3653 const struct nlattr
* const cda
[])
3655 u_int8_t u3
= info
->nfmsg
->nfgen_family
;
3656 struct nf_conntrack_tuple tuple
;
3657 struct nf_conntrack_expect
*exp
;
3658 struct nf_conntrack_zone zone
;
3661 if (!cda
[CTA_EXPECT_TUPLE
]
3662 || !cda
[CTA_EXPECT_MASK
]
3663 || !cda
[CTA_EXPECT_MASTER
])
3666 err
= ctnetlink_parse_zone(cda
[CTA_EXPECT_ZONE
], &zone
);
3670 err
= ctnetlink_parse_tuple(cda
, &tuple
, CTA_EXPECT_TUPLE
,
3675 spin_lock_bh(&nf_conntrack_expect_lock
);
3676 exp
= __nf_ct_expect_find(info
->net
, &zone
, &tuple
);
3678 spin_unlock_bh(&nf_conntrack_expect_lock
);
3680 if (info
->nlh
->nlmsg_flags
& NLM_F_CREATE
) {
3681 err
= ctnetlink_create_expect(info
->net
, &zone
, cda
, u3
,
3682 NETLINK_CB(skb
).portid
,
3683 nlmsg_report(info
->nlh
));
3689 if (!(info
->nlh
->nlmsg_flags
& NLM_F_EXCL
))
3690 err
= ctnetlink_change_expect(exp
, cda
);
3691 spin_unlock_bh(&nf_conntrack_expect_lock
);
3697 ctnetlink_exp_stat_fill_info(struct sk_buff
*skb
, u32 portid
, u32 seq
, int cpu
,
3698 const struct ip_conntrack_stat
*st
)
3700 struct nlmsghdr
*nlh
;
3701 unsigned int flags
= portid
? NLM_F_MULTI
: 0, event
;
3703 event
= nfnl_msg_type(NFNL_SUBSYS_CTNETLINK
,
3704 IPCTNL_MSG_EXP_GET_STATS_CPU
);
3705 nlh
= nfnl_msg_put(skb
, portid
, seq
, event
, flags
, AF_UNSPEC
,
3706 NFNETLINK_V0
, htons(cpu
));
3710 if (nla_put_be32(skb
, CTA_STATS_EXP_NEW
, htonl(st
->expect_new
)) ||
3711 nla_put_be32(skb
, CTA_STATS_EXP_CREATE
, htonl(st
->expect_create
)) ||
3712 nla_put_be32(skb
, CTA_STATS_EXP_DELETE
, htonl(st
->expect_delete
)))
3713 goto nla_put_failure
;
3715 nlmsg_end(skb
, nlh
);
3720 nlmsg_cancel(skb
, nlh
);
3725 ctnetlink_exp_stat_cpu_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
3728 struct net
*net
= sock_net(skb
->sk
);
3730 if (cb
->args
[0] == nr_cpu_ids
)
3733 for (cpu
= cb
->args
[0]; cpu
< nr_cpu_ids
; cpu
++) {
3734 const struct ip_conntrack_stat
*st
;
3736 if (!cpu_possible(cpu
))
3739 st
= per_cpu_ptr(net
->ct
.stat
, cpu
);
3740 if (ctnetlink_exp_stat_fill_info(skb
, NETLINK_CB(cb
->skb
).portid
,
3750 static int ctnetlink_stat_exp_cpu(struct sk_buff
*skb
,
3751 const struct nfnl_info
*info
,
3752 const struct nlattr
* const cda
[])
3754 if (info
->nlh
->nlmsg_flags
& NLM_F_DUMP
) {
3755 struct netlink_dump_control c
= {
3756 .dump
= ctnetlink_exp_stat_cpu_dump
,
3758 return netlink_dump_start(info
->sk
, skb
, info
->nlh
, &c
);
3764 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3765 static struct nf_ct_event_notifier ctnl_notifier
= {
3766 .ct_event
= ctnetlink_conntrack_event
,
3767 .exp_event
= ctnetlink_expect_event
,
3771 static const struct nfnl_callback ctnl_cb
[IPCTNL_MSG_MAX
] = {
3772 [IPCTNL_MSG_CT_NEW
] = {
3773 .call
= ctnetlink_new_conntrack
,
3774 .type
= NFNL_CB_MUTEX
,
3775 .attr_count
= CTA_MAX
,
3776 .policy
= ct_nla_policy
3778 [IPCTNL_MSG_CT_GET
] = {
3779 .call
= ctnetlink_get_conntrack
,
3780 .type
= NFNL_CB_MUTEX
,
3781 .attr_count
= CTA_MAX
,
3782 .policy
= ct_nla_policy
3784 [IPCTNL_MSG_CT_DELETE
] = {
3785 .call
= ctnetlink_del_conntrack
,
3786 .type
= NFNL_CB_MUTEX
,
3787 .attr_count
= CTA_MAX
,
3788 .policy
= ct_nla_policy
3790 [IPCTNL_MSG_CT_GET_CTRZERO
] = {
3791 .call
= ctnetlink_get_conntrack
,
3792 .type
= NFNL_CB_MUTEX
,
3793 .attr_count
= CTA_MAX
,
3794 .policy
= ct_nla_policy
3796 [IPCTNL_MSG_CT_GET_STATS_CPU
] = {
3797 .call
= ctnetlink_stat_ct_cpu
,
3798 .type
= NFNL_CB_MUTEX
,
3800 [IPCTNL_MSG_CT_GET_STATS
] = {
3801 .call
= ctnetlink_stat_ct
,
3802 .type
= NFNL_CB_MUTEX
,
3804 [IPCTNL_MSG_CT_GET_DYING
] = {
3805 .call
= ctnetlink_get_ct_dying
,
3806 .type
= NFNL_CB_MUTEX
,
3808 [IPCTNL_MSG_CT_GET_UNCONFIRMED
] = {
3809 .call
= ctnetlink_get_ct_unconfirmed
,
3810 .type
= NFNL_CB_MUTEX
,
3814 static const struct nfnl_callback ctnl_exp_cb
[IPCTNL_MSG_EXP_MAX
] = {
3815 [IPCTNL_MSG_EXP_GET
] = {
3816 .call
= ctnetlink_get_expect
,
3817 .type
= NFNL_CB_MUTEX
,
3818 .attr_count
= CTA_EXPECT_MAX
,
3819 .policy
= exp_nla_policy
3821 [IPCTNL_MSG_EXP_NEW
] = {
3822 .call
= ctnetlink_new_expect
,
3823 .type
= NFNL_CB_MUTEX
,
3824 .attr_count
= CTA_EXPECT_MAX
,
3825 .policy
= exp_nla_policy
3827 [IPCTNL_MSG_EXP_DELETE
] = {
3828 .call
= ctnetlink_del_expect
,
3829 .type
= NFNL_CB_MUTEX
,
3830 .attr_count
= CTA_EXPECT_MAX
,
3831 .policy
= exp_nla_policy
3833 [IPCTNL_MSG_EXP_GET_STATS_CPU
] = {
3834 .call
= ctnetlink_stat_exp_cpu
,
3835 .type
= NFNL_CB_MUTEX
,
3839 static const struct nfnetlink_subsystem ctnl_subsys
= {
3840 .name
= "conntrack",
3841 .subsys_id
= NFNL_SUBSYS_CTNETLINK
,
3842 .cb_count
= IPCTNL_MSG_MAX
,
3846 static const struct nfnetlink_subsystem ctnl_exp_subsys
= {
3847 .name
= "conntrack_expect",
3848 .subsys_id
= NFNL_SUBSYS_CTNETLINK_EXP
,
3849 .cb_count
= IPCTNL_MSG_EXP_MAX
,
3853 MODULE_ALIAS("ip_conntrack_netlink");
3854 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK
);
3855 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP
);
3857 static int __net_init
ctnetlink_net_init(struct net
*net
)
3859 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3860 nf_conntrack_register_notifier(net
, &ctnl_notifier
);
3865 static void ctnetlink_net_pre_exit(struct net
*net
)
3867 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3868 nf_conntrack_unregister_notifier(net
);
3872 static struct pernet_operations ctnetlink_net_ops
= {
3873 .init
= ctnetlink_net_init
,
3874 .pre_exit
= ctnetlink_net_pre_exit
,
3877 static int __init
ctnetlink_init(void)
3881 ret
= nfnetlink_subsys_register(&ctnl_subsys
);
3883 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3887 ret
= nfnetlink_subsys_register(&ctnl_exp_subsys
);
3889 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3890 goto err_unreg_subsys
;
3893 ret
= register_pernet_subsys(&ctnetlink_net_ops
);
3895 pr_err("ctnetlink_init: cannot register pernet operations\n");
3896 goto err_unreg_exp_subsys
;
3898 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3899 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3900 RCU_INIT_POINTER(nfnl_ct_hook
, &ctnetlink_glue_hook
);
3904 err_unreg_exp_subsys
:
3905 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3907 nfnetlink_subsys_unregister(&ctnl_subsys
);
3912 static void __exit
ctnetlink_exit(void)
3914 unregister_pernet_subsys(&ctnetlink_net_ops
);
3915 nfnetlink_subsys_unregister(&ctnl_exp_subsys
);
3916 nfnetlink_subsys_unregister(&ctnl_subsys
);
3917 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3918 RCU_INIT_POINTER(nfnl_ct_hook
, NULL
);
3923 module_init(ctnetlink_init
);
3924 module_exit(ctnetlink_exit
);