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_l3proto.h>
43 #include <net/netfilter/nf_conntrack_l4proto.h>
44 #include <net/netfilter/nf_conntrack_tuple.h>
45 #include <net/netfilter/nf_conntrack_acct.h>
46 #include <net/netfilter/nf_conntrack_zones.h>
47 #include <net/netfilter/nf_conntrack_timestamp.h>
48 #include <net/netfilter/nf_conntrack_labels.h>
49 #include <net/netfilter/nf_conntrack_seqadj.h>
50 #include <net/netfilter/nf_conntrack_synproxy.h>
51 #ifdef CONFIG_NF_NAT_NEEDED
52 #include <net/netfilter/nf_nat_core.h>
53 #include <net/netfilter/nf_nat_l4proto.h>
54 #include <net/netfilter/nf_nat_helper.h>
57 #include <linux/netfilter/nfnetlink.h>
58 #include <linux/netfilter/nfnetlink_conntrack.h>
60 MODULE_LICENSE("GPL");
62 static char __initdata version[] = "0.93";
64 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
65 const struct nf_conntrack_tuple *tuple,
66 const struct nf_conntrack_l4proto *l4proto)
69 struct nlattr *nest_parms;
71 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
74 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
77 if (likely(l4proto->tuple_to_nlattr))
78 ret = l4proto->tuple_to_nlattr(skb, tuple);
80 nla_nest_end(skb, nest_parms);
88 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
89 const struct nf_conntrack_tuple *tuple,
90 const struct nf_conntrack_l3proto *l3proto)
93 struct nlattr *nest_parms;
95 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
99 if (likely(l3proto->tuple_to_nlattr))
100 ret = l3proto->tuple_to_nlattr(skb, tuple);
102 nla_nest_end(skb, nest_parms);
110 static int ctnetlink_dump_tuples(struct sk_buff *skb,
111 const struct nf_conntrack_tuple *tuple)
113 const struct nf_conntrack_l3proto *l3proto;
114 const struct nf_conntrack_l4proto *l4proto;
118 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
119 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
122 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
123 tuple->dst.protonum);
124 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
130 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
131 const struct nf_conntrack_zone *zone, int dir)
133 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
135 if (nla_put_be16(skb, attrtype, htons(zone->id)))
136 goto nla_put_failure;
143 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
145 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
146 goto nla_put_failure;
153 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
155 long timeout = nf_ct_expires(ct) / HZ;
157 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
158 goto nla_put_failure;
165 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
167 const struct nf_conntrack_l4proto *l4proto;
168 struct nlattr *nest_proto;
171 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
172 if (!l4proto->to_nlattr)
175 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
177 goto nla_put_failure;
179 ret = l4proto->to_nlattr(skb, nest_proto, ct);
181 nla_nest_end(skb, nest_proto);
189 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
190 const struct nf_conn *ct)
192 struct nlattr *nest_helper;
193 const struct nf_conn_help *help = nfct_help(ct);
194 struct nf_conntrack_helper *helper;
200 helper = rcu_dereference(help->helper);
204 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
206 goto nla_put_failure;
207 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
208 goto nla_put_failure;
210 if (helper->to_nlattr)
211 helper->to_nlattr(skb, ct);
213 nla_nest_end(skb, nest_helper);
224 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
225 enum ip_conntrack_dir dir, int type)
227 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
228 struct nf_conn_counter *counter = acct->counter;
229 struct nlattr *nest_count;
232 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
233 pkts = atomic64_xchg(&counter[dir].packets, 0);
234 bytes = atomic64_xchg(&counter[dir].bytes, 0);
236 pkts = atomic64_read(&counter[dir].packets);
237 bytes = atomic64_read(&counter[dir].bytes);
240 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
242 goto nla_put_failure;
244 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
246 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
248 goto nla_put_failure;
250 nla_nest_end(skb, nest_count);
259 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
261 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
266 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
268 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
275 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
277 struct nlattr *nest_count;
278 const struct nf_conn_tstamp *tstamp;
280 tstamp = nf_conn_tstamp_find(ct);
284 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
286 goto nla_put_failure;
288 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
289 CTA_TIMESTAMP_PAD) ||
290 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
291 cpu_to_be64(tstamp->stop),
293 goto nla_put_failure;
294 nla_nest_end(skb, nest_count);
302 #ifdef CONFIG_NF_CONNTRACK_MARK
303 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
305 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
306 goto nla_put_failure;
313 #define ctnetlink_dump_mark(a, b) (0)
316 #ifdef CONFIG_NF_CONNTRACK_SECMARK
317 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
319 struct nlattr *nest_secctx;
323 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
328 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
330 goto nla_put_failure;
332 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
333 goto nla_put_failure;
334 nla_nest_end(skb, nest_secctx);
338 security_release_secctx(secctx, len);
342 #define ctnetlink_dump_secctx(a, b) (0)
345 #ifdef CONFIG_NF_CONNTRACK_LABELS
346 static inline int ctnetlink_label_size(const struct nf_conn *ct)
348 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
352 return nla_total_size(sizeof(labels->bits));
356 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
358 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
366 if (labels->bits[i] != 0)
367 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
370 } while (i < ARRAY_SIZE(labels->bits));
375 #define ctnetlink_dump_labels(a, b) (0)
376 #define ctnetlink_label_size(a) (0)
379 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
381 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
383 struct nlattr *nest_parms;
385 if (!(ct->status & IPS_EXPECTED))
388 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
390 goto nla_put_failure;
391 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
392 goto nla_put_failure;
393 nla_nest_end(skb, nest_parms);
402 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
404 struct nlattr *nest_parms;
406 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
408 goto nla_put_failure;
410 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
411 htonl(seq->correction_pos)) ||
412 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
413 htonl(seq->offset_before)) ||
414 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
415 htonl(seq->offset_after)))
416 goto nla_put_failure;
418 nla_nest_end(skb, nest_parms);
426 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
428 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
429 struct nf_ct_seqadj *seq;
431 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
434 spin_lock_bh(&ct->lock);
435 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
436 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
439 seq = &seqadj->seq[IP_CT_DIR_REPLY];
440 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
443 spin_unlock_bh(&ct->lock);
446 spin_unlock_bh(&ct->lock);
450 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
452 __be32 id = (__force __be32)nf_ct_get_id(ct);
454 if (nla_put_be32(skb, CTA_ID, id))
455 goto nla_put_failure;
462 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
464 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
465 goto nla_put_failure;
473 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
476 const struct nf_conntrack_zone *zone;
477 struct nlmsghdr *nlh;
478 struct nfgenmsg *nfmsg;
479 struct nlattr *nest_parms;
480 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
482 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
483 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
487 nfmsg = nlmsg_data(nlh);
488 nfmsg->nfgen_family = nf_ct_l3num(ct);
489 nfmsg->version = NFNETLINK_V0;
492 zone = nf_ct_zone(ct);
494 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
496 goto nla_put_failure;
497 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
498 goto nla_put_failure;
499 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
500 NF_CT_ZONE_DIR_ORIG) < 0)
501 goto nla_put_failure;
502 nla_nest_end(skb, nest_parms);
504 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
506 goto nla_put_failure;
507 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
508 goto nla_put_failure;
509 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
510 NF_CT_ZONE_DIR_REPL) < 0)
511 goto nla_put_failure;
512 nla_nest_end(skb, nest_parms);
514 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
515 NF_CT_DEFAULT_ZONE_DIR) < 0)
516 goto nla_put_failure;
518 if (ctnetlink_dump_status(skb, ct) < 0 ||
519 ctnetlink_dump_timeout(skb, ct) < 0 ||
520 ctnetlink_dump_acct(skb, ct, type) < 0 ||
521 ctnetlink_dump_timestamp(skb, ct) < 0 ||
522 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
523 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
524 ctnetlink_dump_mark(skb, ct) < 0 ||
525 ctnetlink_dump_secctx(skb, ct) < 0 ||
526 ctnetlink_dump_labels(skb, ct) < 0 ||
527 ctnetlink_dump_id(skb, ct) < 0 ||
528 ctnetlink_dump_use(skb, ct) < 0 ||
529 ctnetlink_dump_master(skb, ct) < 0 ||
530 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
531 goto nla_put_failure;
538 nlmsg_cancel(skb, nlh);
542 static inline size_t ctnetlink_proto_size(const struct nf_conn *ct)
544 const struct nf_conntrack_l3proto *l3proto;
545 const struct nf_conntrack_l4proto *l4proto;
548 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
549 len = l3proto->nla_size;
550 len *= 3u; /* ORIG, REPLY, MASTER */
552 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
553 len += l4proto->nla_size;
558 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
560 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
562 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
563 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
564 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
568 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
570 #ifdef CONFIG_NF_CONNTRACK_SECMARK
573 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
577 return nla_total_size(0) /* CTA_SECCTX */
578 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
584 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
586 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
587 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
589 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
595 #ifdef CONFIG_NF_CONNTRACK_EVENTS
596 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
598 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
599 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
600 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
601 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
602 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
603 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
604 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
605 + ctnetlink_acct_size(ct)
606 + ctnetlink_timestamp_size(ct)
607 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
608 + nla_total_size(0) /* CTA_PROTOINFO */
609 + nla_total_size(0) /* CTA_HELP */
610 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
611 + ctnetlink_secctx_size(ct)
612 #ifdef CONFIG_NF_NAT_NEEDED
613 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
614 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
616 #ifdef CONFIG_NF_CONNTRACK_MARK
617 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
619 #ifdef CONFIG_NF_CONNTRACK_ZONES
620 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
622 + ctnetlink_proto_size(ct)
623 + ctnetlink_label_size(ct)
628 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
630 const struct nf_conntrack_zone *zone;
632 struct nlmsghdr *nlh;
633 struct nfgenmsg *nfmsg;
634 struct nlattr *nest_parms;
635 struct nf_conn *ct = item->ct;
638 unsigned int flags = 0, group;
641 if (events & (1 << IPCT_DESTROY)) {
642 type = IPCTNL_MSG_CT_DELETE;
643 group = NFNLGRP_CONNTRACK_DESTROY;
644 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
645 type = IPCTNL_MSG_CT_NEW;
646 flags = NLM_F_CREATE|NLM_F_EXCL;
647 group = NFNLGRP_CONNTRACK_NEW;
649 type = IPCTNL_MSG_CT_NEW;
650 group = NFNLGRP_CONNTRACK_UPDATE;
655 if (!item->report && !nfnetlink_has_listeners(net, group))
658 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
662 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
663 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
667 nfmsg = nlmsg_data(nlh);
668 nfmsg->nfgen_family = nf_ct_l3num(ct);
669 nfmsg->version = NFNETLINK_V0;
672 zone = nf_ct_zone(ct);
674 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
676 goto nla_put_failure;
677 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
678 goto nla_put_failure;
679 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
680 NF_CT_ZONE_DIR_ORIG) < 0)
681 goto nla_put_failure;
682 nla_nest_end(skb, nest_parms);
684 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
686 goto nla_put_failure;
687 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
688 goto nla_put_failure;
689 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
690 NF_CT_ZONE_DIR_REPL) < 0)
691 goto nla_put_failure;
692 nla_nest_end(skb, nest_parms);
694 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
695 NF_CT_DEFAULT_ZONE_DIR) < 0)
696 goto nla_put_failure;
698 if (ctnetlink_dump_id(skb, ct) < 0)
699 goto nla_put_failure;
701 if (ctnetlink_dump_status(skb, ct) < 0)
702 goto nla_put_failure;
704 if (events & (1 << IPCT_DESTROY)) {
705 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
706 ctnetlink_dump_timestamp(skb, ct) < 0)
707 goto nla_put_failure;
709 if (ctnetlink_dump_timeout(skb, ct) < 0)
710 goto nla_put_failure;
712 if (events & (1 << IPCT_PROTOINFO)
713 && ctnetlink_dump_protoinfo(skb, ct) < 0)
714 goto nla_put_failure;
716 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
717 && ctnetlink_dump_helpinfo(skb, ct) < 0)
718 goto nla_put_failure;
720 #ifdef CONFIG_NF_CONNTRACK_SECMARK
721 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
722 && ctnetlink_dump_secctx(skb, ct) < 0)
723 goto nla_put_failure;
725 if (events & (1 << IPCT_LABEL) &&
726 ctnetlink_dump_labels(skb, ct) < 0)
727 goto nla_put_failure;
729 if (events & (1 << IPCT_RELATED) &&
730 ctnetlink_dump_master(skb, ct) < 0)
731 goto nla_put_failure;
733 if (events & (1 << IPCT_SEQADJ) &&
734 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
735 goto nla_put_failure;
738 #ifdef CONFIG_NF_CONNTRACK_MARK
739 if ((events & (1 << IPCT_MARK) || ct->mark)
740 && ctnetlink_dump_mark(skb, ct) < 0)
741 goto nla_put_failure;
744 err = nfnetlink_send(skb, net, item->portid, group, item->report,
746 if (err == -ENOBUFS || err == -EAGAIN)
752 nlmsg_cancel(skb, nlh);
756 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
761 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
763 static int ctnetlink_done(struct netlink_callback *cb)
766 nf_ct_put((struct nf_conn *)cb->args[1]);
771 struct ctnetlink_filter {
778 static struct ctnetlink_filter *
779 ctnetlink_alloc_filter(const struct nlattr * const cda[])
781 #ifdef CONFIG_NF_CONNTRACK_MARK
782 struct ctnetlink_filter *filter;
784 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
786 return ERR_PTR(-ENOMEM);
788 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
789 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
793 return ERR_PTR(-EOPNOTSUPP);
797 static int ctnetlink_start(struct netlink_callback *cb)
799 const struct nlattr * const *cda = cb->data;
800 struct ctnetlink_filter *filter = NULL;
802 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
803 filter = ctnetlink_alloc_filter(cda);
805 return PTR_ERR(filter);
812 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
814 struct ctnetlink_filter *filter = data;
819 #ifdef CONFIG_NF_CONNTRACK_MARK
820 if ((ct->mark & filter->mark.mask) == filter->mark.val)
828 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
830 struct net *net = sock_net(skb->sk);
831 struct nf_conn *ct, *last;
832 struct nf_conntrack_tuple_hash *h;
833 struct hlist_nulls_node *n;
834 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
835 u_int8_t l3proto = nfmsg->nfgen_family;
836 struct nf_conn *nf_ct_evict[8];
840 last = (struct nf_conn *)cb->args[1];
844 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
848 if (nf_ct_should_gc(nf_ct_evict[i]))
849 nf_ct_kill(nf_ct_evict[i]);
850 nf_ct_put(nf_ct_evict[i]);
853 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
854 nf_conntrack_lock(lockp);
855 if (cb->args[0] >= nf_conntrack_htable_size) {
859 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
861 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
863 ct = nf_ct_tuplehash_to_ctrack(h);
864 if (nf_ct_is_expired(ct)) {
865 if (i < ARRAY_SIZE(nf_ct_evict) &&
866 atomic_inc_not_zero(&ct->ct_general.use))
867 nf_ct_evict[i++] = ct;
871 if (!net_eq(net, nf_ct_net(ct)))
874 /* Dump entries of a given L3 protocol number.
875 * If it is not specified, ie. l3proto == 0,
876 * then dump everything. */
877 if (l3proto && nf_ct_l3num(ct) != l3proto)
884 if (!ctnetlink_filter_match(ct, cb->data))
889 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
891 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
895 nf_conntrack_get(&ct->ct_general);
896 cb->args[1] = (unsigned long)ct;
910 /* nf ct hash resize happened, now clear the leftover. */
911 if ((struct nf_conn *)cb->args[1] == last)
919 if (nf_ct_should_gc(nf_ct_evict[i]))
920 nf_ct_kill(nf_ct_evict[i]);
921 nf_ct_put(nf_ct_evict[i]);
927 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
928 struct nf_conntrack_tuple *tuple)
930 struct nlattr *tb[CTA_IP_MAX+1];
931 struct nf_conntrack_l3proto *l3proto;
934 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
939 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
941 if (likely(l3proto->nlattr_to_tuple)) {
942 ret = nla_validate_nested(attr, CTA_IP_MAX,
943 l3proto->nla_policy, NULL);
945 ret = l3proto->nlattr_to_tuple(tb, tuple);
953 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
954 [CTA_PROTO_NUM] = { .type = NLA_U8 },
957 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
958 struct nf_conntrack_tuple *tuple)
960 const struct nf_conntrack_l4proto *l4proto;
961 struct nlattr *tb[CTA_PROTO_MAX+1];
964 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
969 if (!tb[CTA_PROTO_NUM])
971 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
974 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
976 if (likely(l4proto->nlattr_to_tuple)) {
977 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
978 l4proto->nla_policy, NULL);
980 ret = l4proto->nlattr_to_tuple(tb, tuple);
989 ctnetlink_parse_zone(const struct nlattr *attr,
990 struct nf_conntrack_zone *zone)
992 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
993 NF_CT_DEFAULT_ZONE_DIR, 0);
994 #ifdef CONFIG_NF_CONNTRACK_ZONES
996 zone->id = ntohs(nla_get_be16(attr));
1005 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
1006 struct nf_conntrack_zone *zone)
1010 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
1013 ret = ctnetlink_parse_zone(attr, zone);
1017 if (type == CTA_TUPLE_REPLY)
1018 zone->dir = NF_CT_ZONE_DIR_REPL;
1020 zone->dir = NF_CT_ZONE_DIR_ORIG;
1025 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1026 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1027 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1028 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1032 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1033 struct nf_conntrack_tuple *tuple, u32 type,
1034 u_int8_t l3num, struct nf_conntrack_zone *zone)
1036 struct nlattr *tb[CTA_TUPLE_MAX+1];
1039 memset(tuple, 0, sizeof(*tuple));
1041 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1046 if (!tb[CTA_TUPLE_IP])
1049 if (l3num != NFPROTO_IPV4 && l3num != NFPROTO_IPV6)
1051 tuple->src.l3num = l3num;
1053 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1057 if (!tb[CTA_TUPLE_PROTO])
1060 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1064 if (tb[CTA_TUPLE_ZONE]) {
1068 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1074 /* orig and expect tuples get DIR_ORIGINAL */
1075 if (type == CTA_TUPLE_REPLY)
1076 tuple->dst.dir = IP_CT_DIR_REPLY;
1078 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1083 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1084 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1085 .len = NF_CT_HELPER_NAME_LEN - 1 },
1088 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1089 struct nlattr **helpinfo)
1092 struct nlattr *tb[CTA_HELP_MAX+1];
1094 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1098 if (!tb[CTA_HELP_NAME])
1101 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1103 if (tb[CTA_HELP_INFO])
1104 *helpinfo = tb[CTA_HELP_INFO];
1109 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1110 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1111 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1112 [CTA_STATUS] = { .type = NLA_U32 },
1113 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1114 [CTA_HELP] = { .type = NLA_NESTED },
1115 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1116 [CTA_TIMEOUT] = { .type = NLA_U32 },
1117 [CTA_MARK] = { .type = NLA_U32 },
1118 [CTA_ID] = { .type = NLA_U32 },
1119 [CTA_NAT_DST] = { .type = NLA_NESTED },
1120 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1121 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1122 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1123 [CTA_ZONE] = { .type = NLA_U16 },
1124 [CTA_MARK_MASK] = { .type = NLA_U32 },
1125 [CTA_LABELS] = { .type = NLA_BINARY,
1126 .len = NF_CT_LABELS_MAX_SIZE },
1127 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1128 .len = NF_CT_LABELS_MAX_SIZE },
1131 static int ctnetlink_flush_conntrack(struct net *net,
1132 const struct nlattr * const cda[],
1133 u32 portid, int report)
1135 struct ctnetlink_filter *filter = NULL;
1137 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1138 filter = ctnetlink_alloc_filter(cda);
1140 return PTR_ERR(filter);
1143 nf_ct_iterate_cleanup_net(net, ctnetlink_filter_match, filter,
1150 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1151 struct sk_buff *skb,
1152 const struct nlmsghdr *nlh,
1153 const struct nlattr * const cda[],
1154 struct netlink_ext_ack *extack)
1156 struct nf_conntrack_tuple_hash *h;
1157 struct nf_conntrack_tuple tuple;
1159 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1160 u_int8_t u3 = nfmsg->nfgen_family;
1161 struct nf_conntrack_zone zone;
1164 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1168 if (cda[CTA_TUPLE_ORIG])
1169 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1171 else if (cda[CTA_TUPLE_REPLY])
1172 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1175 return ctnetlink_flush_conntrack(net, cda,
1176 NETLINK_CB(skb).portid,
1183 h = nf_conntrack_find_get(net, &zone, &tuple);
1187 ct = nf_ct_tuplehash_to_ctrack(h);
1190 __be32 id = nla_get_be32(cda[CTA_ID]);
1192 if (id != (__force __be32)nf_ct_get_id(ct)) {
1198 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1204 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1205 struct sk_buff *skb,
1206 const struct nlmsghdr *nlh,
1207 const struct nlattr * const cda[],
1208 struct netlink_ext_ack *extack)
1210 struct nf_conntrack_tuple_hash *h;
1211 struct nf_conntrack_tuple tuple;
1213 struct sk_buff *skb2 = NULL;
1214 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1215 u_int8_t u3 = nfmsg->nfgen_family;
1216 struct nf_conntrack_zone zone;
1219 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1220 struct netlink_dump_control c = {
1221 .start = ctnetlink_start,
1222 .dump = ctnetlink_dump_table,
1223 .done = ctnetlink_done,
1224 .data = (void *)cda,
1227 return netlink_dump_start(ctnl, skb, nlh, &c);
1230 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1234 if (cda[CTA_TUPLE_ORIG])
1235 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1237 else if (cda[CTA_TUPLE_REPLY])
1238 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1246 h = nf_conntrack_find_get(net, &zone, &tuple);
1250 ct = nf_ct_tuplehash_to_ctrack(h);
1253 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1260 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1261 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1267 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1276 /* this avoids a loop in nfnetlink. */
1277 return err == -EAGAIN ? -ENOBUFS : err;
1280 static int ctnetlink_done_list(struct netlink_callback *cb)
1283 nf_ct_put((struct nf_conn *)cb->args[1]);
1288 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1290 struct nf_conn *ct, *last;
1291 struct nf_conntrack_tuple_hash *h;
1292 struct hlist_nulls_node *n;
1293 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1294 u_int8_t l3proto = nfmsg->nfgen_family;
1297 struct hlist_nulls_head *list;
1298 struct net *net = sock_net(skb->sk);
1303 last = (struct nf_conn *)cb->args[1];
1305 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1306 struct ct_pcpu *pcpu;
1308 if (!cpu_possible(cpu))
1311 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1312 spin_lock_bh(&pcpu->lock);
1313 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1315 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1316 ct = nf_ct_tuplehash_to_ctrack(h);
1317 if (l3proto && nf_ct_l3num(ct) != l3proto)
1325 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1327 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1331 if (!atomic_inc_not_zero(&ct->ct_general.use))
1334 cb->args[1] = (unsigned long)ct;
1335 spin_unlock_bh(&pcpu->lock);
1343 spin_unlock_bh(&pcpu->lock);
1354 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1356 return ctnetlink_dump_list(skb, cb, true);
1359 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1360 struct sk_buff *skb,
1361 const struct nlmsghdr *nlh,
1362 const struct nlattr * const cda[],
1363 struct netlink_ext_ack *extack)
1365 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1366 struct netlink_dump_control c = {
1367 .dump = ctnetlink_dump_dying,
1368 .done = ctnetlink_done_list,
1370 return netlink_dump_start(ctnl, skb, nlh, &c);
1377 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1379 return ctnetlink_dump_list(skb, cb, false);
1382 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1383 struct sk_buff *skb,
1384 const struct nlmsghdr *nlh,
1385 const struct nlattr * const cda[],
1386 struct netlink_ext_ack *extack)
1388 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1389 struct netlink_dump_control c = {
1390 .dump = ctnetlink_dump_unconfirmed,
1391 .done = ctnetlink_done_list,
1393 return netlink_dump_start(ctnl, skb, nlh, &c);
1399 #ifdef CONFIG_NF_NAT_NEEDED
1401 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1402 enum nf_nat_manip_type manip,
1403 const struct nlattr *attr)
1405 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1408 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1409 if (!parse_nat_setup) {
1410 #ifdef CONFIG_MODULES
1412 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1413 if (request_module("nf-nat") < 0) {
1414 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1418 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1420 if (nfnetlink_parse_nat_setup_hook)
1426 err = parse_nat_setup(ct, manip, attr);
1427 if (err == -EAGAIN) {
1428 #ifdef CONFIG_MODULES
1430 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1431 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1432 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1436 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1447 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1452 /* Ignore these unchangable bits */
1453 on &= ~IPS_UNCHANGEABLE_MASK;
1454 off &= ~IPS_UNCHANGEABLE_MASK;
1456 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1457 if (on & (1 << bit))
1458 set_bit(bit, &ct->status);
1459 else if (off & (1 << bit))
1460 clear_bit(bit, &ct->status);
1465 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1468 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1469 d = ct->status ^ status;
1471 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1475 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1476 /* SEEN_REPLY bit can only be set */
1479 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1480 /* ASSURED bit can only be set */
1483 __ctnetlink_change_status(ct, status, 0);
1488 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1490 #ifdef CONFIG_NF_NAT_NEEDED
1493 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1496 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1501 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1505 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1511 static int ctnetlink_change_helper(struct nf_conn *ct,
1512 const struct nlattr * const cda[])
1514 struct nf_conntrack_helper *helper;
1515 struct nf_conn_help *help = nfct_help(ct);
1516 char *helpname = NULL;
1517 struct nlattr *helpinfo = NULL;
1520 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1524 /* don't change helper of sibling connections */
1526 /* If we try to change the helper to the same thing twice,
1527 * treat the second attempt as a no-op instead of returning
1533 helper = rcu_dereference(help->helper);
1534 if (helper && !strcmp(helper->name, helpname))
1542 if (!strcmp(helpname, "")) {
1543 if (help && help->helper) {
1544 /* we had a helper before ... */
1545 nf_ct_remove_expectations(ct);
1546 RCU_INIT_POINTER(help->helper, NULL);
1553 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1554 nf_ct_protonum(ct));
1555 if (helper == NULL) {
1561 if (help->helper == helper) {
1562 /* update private helper data if allowed. */
1563 if (helper->from_nlattr)
1564 helper->from_nlattr(helpinfo, ct);
1569 /* we cannot set a helper for an existing conntrack */
1577 static int ctnetlink_change_timeout(struct nf_conn *ct,
1578 const struct nlattr * const cda[])
1580 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1582 ct->timeout = nfct_time_stamp + timeout * HZ;
1584 if (test_bit(IPS_DYING_BIT, &ct->status))
1590 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1591 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1592 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1593 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1596 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1597 const struct nlattr * const cda[])
1599 const struct nlattr *attr = cda[CTA_PROTOINFO];
1600 const struct nf_conntrack_l4proto *l4proto;
1601 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1604 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1610 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1611 if (l4proto->from_nlattr)
1612 err = l4proto->from_nlattr(tb, ct);
1618 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1619 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1620 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1621 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1624 static int change_seq_adj(struct nf_ct_seqadj *seq,
1625 const struct nlattr * const attr)
1628 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1630 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1634 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1637 seq->correction_pos =
1638 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1640 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1643 seq->offset_before =
1644 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1646 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1650 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1656 ctnetlink_change_seq_adj(struct nf_conn *ct,
1657 const struct nlattr * const cda[])
1659 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1665 spin_lock_bh(&ct->lock);
1666 if (cda[CTA_SEQ_ADJ_ORIG]) {
1667 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1668 cda[CTA_SEQ_ADJ_ORIG]);
1672 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1675 if (cda[CTA_SEQ_ADJ_REPLY]) {
1676 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1677 cda[CTA_SEQ_ADJ_REPLY]);
1681 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1684 spin_unlock_bh(&ct->lock);
1687 spin_unlock_bh(&ct->lock);
1692 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1694 #ifdef CONFIG_NF_CONNTRACK_LABELS
1695 size_t len = nla_len(cda[CTA_LABELS]);
1696 const void *mask = cda[CTA_LABELS_MASK];
1698 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1702 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1703 nla_len(cda[CTA_LABELS_MASK]) != len)
1705 mask = nla_data(cda[CTA_LABELS_MASK]);
1710 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1717 ctnetlink_change_conntrack(struct nf_conn *ct,
1718 const struct nlattr * const cda[])
1722 /* only allow NAT changes and master assignation for new conntracks */
1723 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1726 if (cda[CTA_HELP]) {
1727 err = ctnetlink_change_helper(ct, cda);
1732 if (cda[CTA_TIMEOUT]) {
1733 err = ctnetlink_change_timeout(ct, cda);
1738 if (cda[CTA_STATUS]) {
1739 err = ctnetlink_change_status(ct, cda);
1744 if (cda[CTA_PROTOINFO]) {
1745 err = ctnetlink_change_protoinfo(ct, cda);
1750 #if defined(CONFIG_NF_CONNTRACK_MARK)
1752 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1755 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1756 err = ctnetlink_change_seq_adj(ct, cda);
1761 if (cda[CTA_LABELS]) {
1762 err = ctnetlink_attach_labels(ct, cda);
1770 static struct nf_conn *
1771 ctnetlink_create_conntrack(struct net *net,
1772 const struct nf_conntrack_zone *zone,
1773 const struct nlattr * const cda[],
1774 struct nf_conntrack_tuple *otuple,
1775 struct nf_conntrack_tuple *rtuple,
1780 struct nf_conntrack_helper *helper;
1781 struct nf_conn_tstamp *tstamp;
1783 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1785 return ERR_PTR(-ENOMEM);
1787 if (!cda[CTA_TIMEOUT])
1790 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1793 if (cda[CTA_HELP]) {
1794 char *helpname = NULL;
1795 struct nlattr *helpinfo = NULL;
1797 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1801 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1802 nf_ct_protonum(ct));
1803 if (helper == NULL) {
1805 #ifdef CONFIG_MODULES
1806 if (request_module("nfct-helper-%s", helpname) < 0) {
1812 helper = __nf_conntrack_helper_find(helpname,
1814 nf_ct_protonum(ct));
1824 struct nf_conn_help *help;
1826 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1831 /* set private helper data if allowed. */
1832 if (helper->from_nlattr)
1833 helper->from_nlattr(helpinfo, ct);
1835 /* not in hash table yet so not strictly necessary */
1836 RCU_INIT_POINTER(help->helper, helper);
1839 /* try an implicit helper assignation */
1840 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1845 err = ctnetlink_setup_nat(ct, cda);
1849 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1850 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1851 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1852 nf_ct_labels_ext_add(ct);
1853 nfct_seqadj_ext_add(ct);
1854 nfct_synproxy_ext_add(ct);
1856 /* we must add conntrack extensions before confirmation. */
1857 ct->status |= IPS_CONFIRMED;
1859 if (cda[CTA_STATUS]) {
1860 err = ctnetlink_change_status(ct, cda);
1865 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1866 err = ctnetlink_change_seq_adj(ct, cda);
1871 memset(&ct->proto, 0, sizeof(ct->proto));
1872 if (cda[CTA_PROTOINFO]) {
1873 err = ctnetlink_change_protoinfo(ct, cda);
1878 #if defined(CONFIG_NF_CONNTRACK_MARK)
1880 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1883 /* setup master conntrack: this is a confirmed expectation */
1884 if (cda[CTA_TUPLE_MASTER]) {
1885 struct nf_conntrack_tuple master;
1886 struct nf_conntrack_tuple_hash *master_h;
1887 struct nf_conn *master_ct;
1889 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1894 master_h = nf_conntrack_find_get(net, zone, &master);
1895 if (master_h == NULL) {
1899 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1900 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1901 ct->master = master_ct;
1903 tstamp = nf_conn_tstamp_find(ct);
1905 tstamp->start = ktime_get_real_ns();
1907 err = nf_conntrack_hash_check_insert(ct);
1918 nf_conntrack_free(ct);
1919 return ERR_PTR(err);
1922 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1923 struct sk_buff *skb,
1924 const struct nlmsghdr *nlh,
1925 const struct nlattr * const cda[],
1926 struct netlink_ext_ack *extack)
1928 struct nf_conntrack_tuple otuple, rtuple;
1929 struct nf_conntrack_tuple_hash *h = NULL;
1930 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1932 u_int8_t u3 = nfmsg->nfgen_family;
1933 struct nf_conntrack_zone zone;
1936 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1940 if (cda[CTA_TUPLE_ORIG]) {
1941 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1947 if (cda[CTA_TUPLE_REPLY]) {
1948 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1954 if (cda[CTA_TUPLE_ORIG])
1955 h = nf_conntrack_find_get(net, &zone, &otuple);
1956 else if (cda[CTA_TUPLE_REPLY])
1957 h = nf_conntrack_find_get(net, &zone, &rtuple);
1961 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1962 enum ip_conntrack_events events;
1964 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1966 if (otuple.dst.protonum != rtuple.dst.protonum)
1969 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1975 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1976 events = 1 << IPCT_RELATED;
1978 events = 1 << IPCT_NEW;
1980 if (cda[CTA_LABELS] &&
1981 ctnetlink_attach_labels(ct, cda) == 0)
1982 events |= (1 << IPCT_LABEL);
1984 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1985 (1 << IPCT_ASSURED) |
1986 (1 << IPCT_HELPER) |
1987 (1 << IPCT_PROTOINFO) |
1988 (1 << IPCT_SEQADJ) |
1989 (1 << IPCT_MARK) | events,
1990 ct, NETLINK_CB(skb).portid,
1997 /* implicit 'else' */
2000 ct = nf_ct_tuplehash_to_ctrack(h);
2001 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2002 err = ctnetlink_change_conntrack(ct, cda);
2004 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2005 (1 << IPCT_ASSURED) |
2006 (1 << IPCT_HELPER) |
2008 (1 << IPCT_PROTOINFO) |
2009 (1 << IPCT_SEQADJ) |
2011 ct, NETLINK_CB(skb).portid,
2021 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2022 __u16 cpu, const struct ip_conntrack_stat *st)
2024 struct nlmsghdr *nlh;
2025 struct nfgenmsg *nfmsg;
2026 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2028 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2029 IPCTNL_MSG_CT_GET_STATS_CPU);
2030 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2034 nfmsg = nlmsg_data(nlh);
2035 nfmsg->nfgen_family = AF_UNSPEC;
2036 nfmsg->version = NFNETLINK_V0;
2037 nfmsg->res_id = htons(cpu);
2039 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2040 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2041 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2042 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2043 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2044 htonl(st->insert_failed)) ||
2045 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2046 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2047 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2048 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2049 htonl(st->search_restart)))
2050 goto nla_put_failure;
2052 nlmsg_end(skb, nlh);
2057 nlmsg_cancel(skb, nlh);
2062 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2065 struct net *net = sock_net(skb->sk);
2067 if (cb->args[0] == nr_cpu_ids)
2070 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2071 const struct ip_conntrack_stat *st;
2073 if (!cpu_possible(cpu))
2076 st = per_cpu_ptr(net->ct.stat, cpu);
2077 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2078 NETLINK_CB(cb->skb).portid,
2088 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2089 struct sk_buff *skb,
2090 const struct nlmsghdr *nlh,
2091 const struct nlattr * const cda[],
2092 struct netlink_ext_ack *extack)
2094 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2095 struct netlink_dump_control c = {
2096 .dump = ctnetlink_ct_stat_cpu_dump,
2098 return netlink_dump_start(ctnl, skb, nlh, &c);
2105 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2108 struct nlmsghdr *nlh;
2109 struct nfgenmsg *nfmsg;
2110 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2111 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2113 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2114 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2118 nfmsg = nlmsg_data(nlh);
2119 nfmsg->nfgen_family = AF_UNSPEC;
2120 nfmsg->version = NFNETLINK_V0;
2123 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2124 goto nla_put_failure;
2126 nlmsg_end(skb, nlh);
2131 nlmsg_cancel(skb, nlh);
2135 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2136 struct sk_buff *skb, const struct nlmsghdr *nlh,
2137 const struct nlattr * const cda[],
2138 struct netlink_ext_ack *extack)
2140 struct sk_buff *skb2;
2143 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2147 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2149 NFNL_MSG_TYPE(nlh->nlmsg_type),
2154 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2163 /* this avoids a loop in nfnetlink. */
2164 return err == -EAGAIN ? -ENOBUFS : err;
2167 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2168 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2169 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2170 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2171 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2172 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2173 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2174 .len = NF_CT_HELPER_NAME_LEN - 1 },
2175 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2176 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2177 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2178 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2179 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2182 static struct nf_conntrack_expect *
2183 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2184 struct nf_conntrack_helper *helper,
2185 struct nf_conntrack_tuple *tuple,
2186 struct nf_conntrack_tuple *mask);
2188 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2190 ctnetlink_glue_build_size(const struct nf_conn *ct)
2192 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2193 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2194 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2195 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2196 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2197 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2198 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2199 + nla_total_size(0) /* CTA_PROTOINFO */
2200 + nla_total_size(0) /* CTA_HELP */
2201 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2202 + ctnetlink_secctx_size(ct)
2203 #ifdef CONFIG_NF_NAT_NEEDED
2204 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2205 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2207 #ifdef CONFIG_NF_CONNTRACK_MARK
2208 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2210 #ifdef CONFIG_NF_CONNTRACK_ZONES
2211 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2213 + ctnetlink_proto_size(ct)
2217 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2218 enum ip_conntrack_info *ctinfo)
2220 return nf_ct_get(skb, ctinfo);
2223 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2225 const struct nf_conntrack_zone *zone;
2226 struct nlattr *nest_parms;
2228 zone = nf_ct_zone(ct);
2230 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2232 goto nla_put_failure;
2233 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2234 goto nla_put_failure;
2235 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2236 NF_CT_ZONE_DIR_ORIG) < 0)
2237 goto nla_put_failure;
2238 nla_nest_end(skb, nest_parms);
2240 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2242 goto nla_put_failure;
2243 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2244 goto nla_put_failure;
2245 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2246 NF_CT_ZONE_DIR_REPL) < 0)
2247 goto nla_put_failure;
2248 nla_nest_end(skb, nest_parms);
2250 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2251 NF_CT_DEFAULT_ZONE_DIR) < 0)
2252 goto nla_put_failure;
2254 if (ctnetlink_dump_id(skb, ct) < 0)
2255 goto nla_put_failure;
2257 if (ctnetlink_dump_status(skb, ct) < 0)
2258 goto nla_put_failure;
2260 if (ctnetlink_dump_timeout(skb, ct) < 0)
2261 goto nla_put_failure;
2263 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2264 goto nla_put_failure;
2266 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2267 goto nla_put_failure;
2269 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2270 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2271 goto nla_put_failure;
2273 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2274 goto nla_put_failure;
2276 if ((ct->status & IPS_SEQ_ADJUST) &&
2277 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2278 goto nla_put_failure;
2280 #ifdef CONFIG_NF_CONNTRACK_MARK
2281 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2282 goto nla_put_failure;
2284 if (ctnetlink_dump_labels(skb, ct) < 0)
2285 goto nla_put_failure;
2293 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2294 enum ip_conntrack_info ctinfo,
2295 u_int16_t ct_attr, u_int16_t ct_info_attr)
2297 struct nlattr *nest_parms;
2299 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2301 goto nla_put_failure;
2303 if (__ctnetlink_glue_build(skb, ct) < 0)
2304 goto nla_put_failure;
2306 nla_nest_end(skb, nest_parms);
2308 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2309 goto nla_put_failure;
2318 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2320 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2321 unsigned long d = ct->status ^ status;
2323 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2324 /* SEEN_REPLY bit can only be set */
2327 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2328 /* ASSURED bit can only be set */
2331 /* This check is less strict than ctnetlink_change_status()
2332 * because callers often flip IPS_EXPECTED bits when sending
2333 * an NFQA_CT attribute to the kernel. So ignore the
2334 * unchangeable bits but do not error out. Also user programs
2335 * are allowed to clear the bits that they are allowed to change.
2337 __ctnetlink_change_status(ct, status, ~status);
2342 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2346 if (cda[CTA_TIMEOUT]) {
2347 err = ctnetlink_change_timeout(ct, cda);
2351 if (cda[CTA_STATUS]) {
2352 err = ctnetlink_update_status(ct, cda);
2356 if (cda[CTA_HELP]) {
2357 err = ctnetlink_change_helper(ct, cda);
2361 if (cda[CTA_LABELS]) {
2362 err = ctnetlink_attach_labels(ct, cda);
2366 #if defined(CONFIG_NF_CONNTRACK_MARK)
2367 if (cda[CTA_MARK]) {
2368 u32 mask = 0, mark, newmark;
2369 if (cda[CTA_MARK_MASK])
2370 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2372 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2373 newmark = (ct->mark & mask) ^ mark;
2374 if (newmark != ct->mark)
2382 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2384 struct nlattr *cda[CTA_MAX+1];
2387 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2391 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2394 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2395 const struct nf_conn *ct,
2396 struct nf_conntrack_tuple *tuple,
2397 struct nf_conntrack_tuple *mask)
2401 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2402 nf_ct_l3num(ct), NULL);
2406 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2407 nf_ct_l3num(ct), NULL);
2411 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2412 u32 portid, u32 report)
2414 struct nlattr *cda[CTA_EXPECT_MAX+1];
2415 struct nf_conntrack_tuple tuple, mask;
2416 struct nf_conntrack_helper *helper = NULL;
2417 struct nf_conntrack_expect *exp;
2420 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2425 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2430 if (cda[CTA_EXPECT_HELP_NAME]) {
2431 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2433 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2434 nf_ct_protonum(ct));
2439 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2440 helper, &tuple, &mask);
2442 return PTR_ERR(exp);
2444 err = nf_ct_expect_related_report(exp, portid, report);
2445 nf_ct_expect_put(exp);
2449 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2450 enum ip_conntrack_info ctinfo, int diff)
2452 if (!(ct->status & IPS_NAT_MASK))
2455 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2458 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2459 .get_ct = ctnetlink_glue_get_ct,
2460 .build_size = ctnetlink_glue_build_size,
2461 .build = ctnetlink_glue_build,
2462 .parse = ctnetlink_glue_parse,
2463 .attach_expect = ctnetlink_glue_attach_expect,
2464 .seq_adjust = ctnetlink_glue_seqadj,
2466 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2468 /***********************************************************************
2470 ***********************************************************************/
2472 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2473 const struct nf_conntrack_tuple *tuple,
2476 struct nlattr *nest_parms;
2478 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2480 goto nla_put_failure;
2481 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2482 goto nla_put_failure;
2483 nla_nest_end(skb, nest_parms);
2491 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2492 const struct nf_conntrack_tuple *tuple,
2493 const struct nf_conntrack_tuple_mask *mask)
2495 const struct nf_conntrack_l3proto *l3proto;
2496 const struct nf_conntrack_l4proto *l4proto;
2497 struct nf_conntrack_tuple m;
2498 struct nlattr *nest_parms;
2501 memset(&m, 0xFF, sizeof(m));
2502 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2503 m.src.u.all = mask->src.u.all;
2504 m.dst.protonum = tuple->dst.protonum;
2506 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2508 goto nla_put_failure;
2511 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2512 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2514 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2515 tuple->dst.protonum);
2516 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2520 if (unlikely(ret < 0))
2521 goto nla_put_failure;
2523 nla_nest_end(skb, nest_parms);
2531 static const union nf_inet_addr any_addr;
2533 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2535 static __read_mostly siphash_key_t exp_id_seed;
2536 unsigned long a, b, c, d;
2538 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2540 a = (unsigned long)exp;
2541 b = (unsigned long)exp->helper;
2542 c = (unsigned long)exp->master;
2543 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2546 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2548 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2553 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2554 const struct nf_conntrack_expect *exp)
2556 struct nf_conn *master = exp->master;
2557 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2558 struct nf_conn_help *help;
2559 #ifdef CONFIG_NF_NAT_NEEDED
2560 struct nlattr *nest_parms;
2561 struct nf_conntrack_tuple nat_tuple = {};
2563 struct nf_ct_helper_expectfn *expfn;
2568 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2569 goto nla_put_failure;
2570 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2571 goto nla_put_failure;
2572 if (ctnetlink_exp_dump_tuple(skb,
2573 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2574 CTA_EXPECT_MASTER) < 0)
2575 goto nla_put_failure;
2577 #ifdef CONFIG_NF_NAT_NEEDED
2578 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2579 exp->saved_proto.all) {
2580 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2582 goto nla_put_failure;
2584 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2585 goto nla_put_failure;
2587 nat_tuple.src.l3num = nf_ct_l3num(master);
2588 nat_tuple.src.u3 = exp->saved_addr;
2589 nat_tuple.dst.protonum = nf_ct_protonum(master);
2590 nat_tuple.src.u = exp->saved_proto;
2592 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2593 CTA_EXPECT_NAT_TUPLE) < 0)
2594 goto nla_put_failure;
2595 nla_nest_end(skb, nest_parms);
2598 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2599 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
2600 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2601 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2602 goto nla_put_failure;
2603 help = nfct_help(master);
2605 struct nf_conntrack_helper *helper;
2607 helper = rcu_dereference(help->helper);
2609 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2610 goto nla_put_failure;
2612 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2613 if (expfn != NULL &&
2614 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2615 goto nla_put_failure;
2624 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2625 int event, const struct nf_conntrack_expect *exp)
2627 struct nlmsghdr *nlh;
2628 struct nfgenmsg *nfmsg;
2629 unsigned int flags = portid ? NLM_F_MULTI : 0;
2631 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2632 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2636 nfmsg = nlmsg_data(nlh);
2637 nfmsg->nfgen_family = exp->tuple.src.l3num;
2638 nfmsg->version = NFNETLINK_V0;
2641 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2642 goto nla_put_failure;
2644 nlmsg_end(skb, nlh);
2649 nlmsg_cancel(skb, nlh);
2653 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2655 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2657 struct nf_conntrack_expect *exp = item->exp;
2658 struct net *net = nf_ct_exp_net(exp);
2659 struct nlmsghdr *nlh;
2660 struct nfgenmsg *nfmsg;
2661 struct sk_buff *skb;
2662 unsigned int type, group;
2665 if (events & (1 << IPEXP_DESTROY)) {
2666 type = IPCTNL_MSG_EXP_DELETE;
2667 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2668 } else if (events & (1 << IPEXP_NEW)) {
2669 type = IPCTNL_MSG_EXP_NEW;
2670 flags = NLM_F_CREATE|NLM_F_EXCL;
2671 group = NFNLGRP_CONNTRACK_EXP_NEW;
2675 if (!item->report && !nfnetlink_has_listeners(net, group))
2678 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2682 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2683 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2687 nfmsg = nlmsg_data(nlh);
2688 nfmsg->nfgen_family = exp->tuple.src.l3num;
2689 nfmsg->version = NFNETLINK_V0;
2692 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2693 goto nla_put_failure;
2695 nlmsg_end(skb, nlh);
2696 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2700 nlmsg_cancel(skb, nlh);
2704 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2708 static int ctnetlink_exp_done(struct netlink_callback *cb)
2711 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2716 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2718 struct net *net = sock_net(skb->sk);
2719 struct nf_conntrack_expect *exp, *last;
2720 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2721 u_int8_t l3proto = nfmsg->nfgen_family;
2724 last = (struct nf_conntrack_expect *)cb->args[1];
2725 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2727 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2729 if (l3proto && exp->tuple.src.l3num != l3proto)
2732 if (!net_eq(nf_ct_net(exp->master), net))
2740 if (ctnetlink_exp_fill_info(skb,
2741 NETLINK_CB(cb->skb).portid,
2745 if (!refcount_inc_not_zero(&exp->use))
2747 cb->args[1] = (unsigned long)exp;
2759 nf_ct_expect_put(last);
2765 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2767 struct nf_conntrack_expect *exp, *last;
2768 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2769 struct nf_conn *ct = cb->data;
2770 struct nf_conn_help *help = nfct_help(ct);
2771 u_int8_t l3proto = nfmsg->nfgen_family;
2777 last = (struct nf_conntrack_expect *)cb->args[1];
2779 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2780 if (l3proto && exp->tuple.src.l3num != l3proto)
2787 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2791 if (!refcount_inc_not_zero(&exp->use))
2793 cb->args[1] = (unsigned long)exp;
2805 nf_ct_expect_put(last);
2810 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2811 struct sk_buff *skb,
2812 const struct nlmsghdr *nlh,
2813 const struct nlattr * const cda[],
2814 struct netlink_ext_ack *extack)
2817 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2818 u_int8_t u3 = nfmsg->nfgen_family;
2819 struct nf_conntrack_tuple tuple;
2820 struct nf_conntrack_tuple_hash *h;
2822 struct nf_conntrack_zone zone;
2823 struct netlink_dump_control c = {
2824 .dump = ctnetlink_exp_ct_dump_table,
2825 .done = ctnetlink_exp_done,
2828 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2833 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2837 h = nf_conntrack_find_get(net, &zone, &tuple);
2841 ct = nf_ct_tuplehash_to_ctrack(h);
2842 /* No expectation linked to this connection tracking. */
2843 if (!nfct_help(ct)) {
2850 err = netlink_dump_start(ctnl, skb, nlh, &c);
2856 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2857 struct sk_buff *skb, const struct nlmsghdr *nlh,
2858 const struct nlattr * const cda[],
2859 struct netlink_ext_ack *extack)
2861 struct nf_conntrack_tuple tuple;
2862 struct nf_conntrack_expect *exp;
2863 struct sk_buff *skb2;
2864 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2865 u_int8_t u3 = nfmsg->nfgen_family;
2866 struct nf_conntrack_zone zone;
2869 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2870 if (cda[CTA_EXPECT_MASTER])
2871 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
2874 struct netlink_dump_control c = {
2875 .dump = ctnetlink_exp_dump_table,
2876 .done = ctnetlink_exp_done,
2878 return netlink_dump_start(ctnl, skb, nlh, &c);
2882 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2886 if (cda[CTA_EXPECT_TUPLE])
2887 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2889 else if (cda[CTA_EXPECT_MASTER])
2890 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2898 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2902 if (cda[CTA_EXPECT_ID]) {
2903 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2905 if (id != nf_expect_get_id(exp)) {
2906 nf_ct_expect_put(exp);
2912 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2914 nf_ct_expect_put(exp);
2919 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2920 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2922 nf_ct_expect_put(exp);
2926 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2935 /* this avoids a loop in nfnetlink. */
2936 return err == -EAGAIN ? -ENOBUFS : err;
2939 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
2941 const struct nf_conn_help *m_help;
2942 const char *name = data;
2944 m_help = nfct_help(exp->master);
2946 return strcmp(m_help->helper->name, name) == 0;
2949 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
2954 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2955 struct sk_buff *skb, const struct nlmsghdr *nlh,
2956 const struct nlattr * const cda[],
2957 struct netlink_ext_ack *extack)
2959 struct nf_conntrack_expect *exp;
2960 struct nf_conntrack_tuple tuple;
2961 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2962 u_int8_t u3 = nfmsg->nfgen_family;
2963 struct nf_conntrack_zone zone;
2966 if (cda[CTA_EXPECT_TUPLE]) {
2967 /* delete a single expect by tuple */
2968 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2972 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2977 /* bump usage count to 2 */
2978 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2982 if (cda[CTA_EXPECT_ID]) {
2983 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2984 if (ntohl(id) != (u32)(unsigned long)exp) {
2985 nf_ct_expect_put(exp);
2990 /* after list removal, usage count == 1 */
2991 spin_lock_bh(&nf_conntrack_expect_lock);
2992 if (del_timer(&exp->timeout)) {
2993 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2995 nf_ct_expect_put(exp);
2997 spin_unlock_bh(&nf_conntrack_expect_lock);
2998 /* have to put what we 'get' above.
2999 * after this line usage count == 0 */
3000 nf_ct_expect_put(exp);
3001 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3002 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3004 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3005 NETLINK_CB(skb).portid,
3008 /* This basically means we have to flush everything*/
3009 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3010 NETLINK_CB(skb).portid,
3017 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3018 const struct nlattr * const cda[])
3020 if (cda[CTA_EXPECT_TIMEOUT]) {
3021 if (!del_timer(&x->timeout))
3024 x->timeout.expires = jiffies +
3025 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3026 add_timer(&x->timeout);
3031 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3032 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3033 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3037 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3038 struct nf_conntrack_expect *exp,
3041 #ifdef CONFIG_NF_NAT_NEEDED
3042 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3043 struct nf_conntrack_tuple nat_tuple = {};
3046 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3047 exp_nat_nla_policy, NULL);
3051 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3054 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3055 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3060 exp->saved_addr = nat_tuple.src.u3;
3061 exp->saved_proto = nat_tuple.src.u;
3062 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3070 static struct nf_conntrack_expect *
3071 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3072 struct nf_conntrack_helper *helper,
3073 struct nf_conntrack_tuple *tuple,
3074 struct nf_conntrack_tuple *mask)
3076 u_int32_t class = 0;
3077 struct nf_conntrack_expect *exp;
3078 struct nf_conn_help *help;
3081 help = nfct_help(ct);
3083 return ERR_PTR(-EOPNOTSUPP);
3085 if (cda[CTA_EXPECT_CLASS] && helper) {
3086 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3087 if (class > helper->expect_class_max)
3088 return ERR_PTR(-EINVAL);
3090 exp = nf_ct_expect_alloc(ct);
3092 return ERR_PTR(-ENOMEM);
3094 if (cda[CTA_EXPECT_FLAGS]) {
3095 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3096 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3100 if (cda[CTA_EXPECT_FN]) {
3101 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3102 struct nf_ct_helper_expectfn *expfn;
3104 expfn = nf_ct_helper_expectfn_find_by_name(name);
3105 if (expfn == NULL) {
3109 exp->expectfn = expfn->expectfn;
3111 exp->expectfn = NULL;
3115 exp->helper = helper;
3116 exp->tuple = *tuple;
3117 exp->mask.src.u3 = mask->src.u3;
3118 exp->mask.src.u.all = mask->src.u.all;
3120 if (cda[CTA_EXPECT_NAT]) {
3121 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3122 exp, nf_ct_l3num(ct));
3128 nf_ct_expect_put(exp);
3129 return ERR_PTR(err);
3133 ctnetlink_create_expect(struct net *net,
3134 const struct nf_conntrack_zone *zone,
3135 const struct nlattr * const cda[],
3136 u_int8_t u3, u32 portid, int report)
3138 struct nf_conntrack_tuple tuple, mask, master_tuple;
3139 struct nf_conntrack_tuple_hash *h = NULL;
3140 struct nf_conntrack_helper *helper = NULL;
3141 struct nf_conntrack_expect *exp;
3145 /* caller guarantees that those three CTA_EXPECT_* exist */
3146 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3150 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3154 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3159 /* Look for master conntrack of this expectation */
3160 h = nf_conntrack_find_get(net, zone, &master_tuple);
3163 ct = nf_ct_tuplehash_to_ctrack(h);
3166 if (cda[CTA_EXPECT_HELP_NAME]) {
3167 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3169 helper = __nf_conntrack_helper_find(helpname, u3,
3170 nf_ct_protonum(ct));
3171 if (helper == NULL) {
3173 #ifdef CONFIG_MODULES
3174 if (request_module("nfct-helper-%s", helpname) < 0) {
3179 helper = __nf_conntrack_helper_find(helpname, u3,
3180 nf_ct_protonum(ct));
3192 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3198 err = nf_ct_expect_related_report(exp, portid, report);
3199 nf_ct_expect_put(exp);
3207 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3208 struct sk_buff *skb, const struct nlmsghdr *nlh,
3209 const struct nlattr * const cda[],
3210 struct netlink_ext_ack *extack)
3212 struct nf_conntrack_tuple tuple;
3213 struct nf_conntrack_expect *exp;
3214 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3215 u_int8_t u3 = nfmsg->nfgen_family;
3216 struct nf_conntrack_zone zone;
3219 if (!cda[CTA_EXPECT_TUPLE]
3220 || !cda[CTA_EXPECT_MASK]
3221 || !cda[CTA_EXPECT_MASTER])
3224 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3228 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3233 spin_lock_bh(&nf_conntrack_expect_lock);
3234 exp = __nf_ct_expect_find(net, &zone, &tuple);
3236 spin_unlock_bh(&nf_conntrack_expect_lock);
3238 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3239 err = ctnetlink_create_expect(net, &zone, cda, u3,
3240 NETLINK_CB(skb).portid,
3247 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3248 err = ctnetlink_change_expect(exp, cda);
3249 spin_unlock_bh(&nf_conntrack_expect_lock);
3255 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3256 const struct ip_conntrack_stat *st)
3258 struct nlmsghdr *nlh;
3259 struct nfgenmsg *nfmsg;
3260 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3262 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3263 IPCTNL_MSG_EXP_GET_STATS_CPU);
3264 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3268 nfmsg = nlmsg_data(nlh);
3269 nfmsg->nfgen_family = AF_UNSPEC;
3270 nfmsg->version = NFNETLINK_V0;
3271 nfmsg->res_id = htons(cpu);
3273 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3274 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3275 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3276 goto nla_put_failure;
3278 nlmsg_end(skb, nlh);
3283 nlmsg_cancel(skb, nlh);
3288 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3291 struct net *net = sock_net(skb->sk);
3293 if (cb->args[0] == nr_cpu_ids)
3296 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3297 const struct ip_conntrack_stat *st;
3299 if (!cpu_possible(cpu))
3302 st = per_cpu_ptr(net->ct.stat, cpu);
3303 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3313 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3314 struct sk_buff *skb,
3315 const struct nlmsghdr *nlh,
3316 const struct nlattr * const cda[],
3317 struct netlink_ext_ack *extack)
3319 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3320 struct netlink_dump_control c = {
3321 .dump = ctnetlink_exp_stat_cpu_dump,
3323 return netlink_dump_start(ctnl, skb, nlh, &c);
3329 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3330 static struct nf_ct_event_notifier ctnl_notifier = {
3331 .fcn = ctnetlink_conntrack_event,
3334 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3335 .fcn = ctnetlink_expect_event,
3339 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3340 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3341 .attr_count = CTA_MAX,
3342 .policy = ct_nla_policy },
3343 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3344 .attr_count = CTA_MAX,
3345 .policy = ct_nla_policy },
3346 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3347 .attr_count = CTA_MAX,
3348 .policy = ct_nla_policy },
3349 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3350 .attr_count = CTA_MAX,
3351 .policy = ct_nla_policy },
3352 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3353 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3354 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3355 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3358 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3359 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3360 .attr_count = CTA_EXPECT_MAX,
3361 .policy = exp_nla_policy },
3362 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3363 .attr_count = CTA_EXPECT_MAX,
3364 .policy = exp_nla_policy },
3365 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3366 .attr_count = CTA_EXPECT_MAX,
3367 .policy = exp_nla_policy },
3368 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3371 static const struct nfnetlink_subsystem ctnl_subsys = {
3372 .name = "conntrack",
3373 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3374 .cb_count = IPCTNL_MSG_MAX,
3378 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3379 .name = "conntrack_expect",
3380 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3381 .cb_count = IPCTNL_MSG_EXP_MAX,
3385 MODULE_ALIAS("ip_conntrack_netlink");
3386 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3387 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3389 static int __net_init ctnetlink_net_init(struct net *net)
3391 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3394 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3396 pr_err("ctnetlink_init: cannot register notifier.\n");
3400 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3402 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3403 goto err_unreg_notifier;
3408 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3410 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3416 static void ctnetlink_net_exit(struct net *net)
3418 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3419 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3420 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3424 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3428 list_for_each_entry(net, net_exit_list, exit_list)
3429 ctnetlink_net_exit(net);
3431 /* wait for other cpus until they are done with ctnl_notifiers */
3435 static struct pernet_operations ctnetlink_net_ops = {
3436 .init = ctnetlink_net_init,
3437 .exit_batch = ctnetlink_net_exit_batch,
3440 static int __init ctnetlink_init(void)
3444 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3445 ret = nfnetlink_subsys_register(&ctnl_subsys);
3447 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3451 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3453 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3454 goto err_unreg_subsys;
3457 ret = register_pernet_subsys(&ctnetlink_net_ops);
3459 pr_err("ctnetlink_init: cannot register pernet operations\n");
3460 goto err_unreg_exp_subsys;
3462 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3463 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3464 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3468 err_unreg_exp_subsys:
3469 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3471 nfnetlink_subsys_unregister(&ctnl_subsys);
3476 static void __exit ctnetlink_exit(void)
3478 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3480 unregister_pernet_subsys(&ctnetlink_net_ops);
3481 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3482 nfnetlink_subsys_unregister(&ctnl_subsys);
3483 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3484 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3489 module_init(ctnetlink_init);
3490 module_exit(ctnetlink_exit);