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);
1917 nf_ct_put(ct->master);
1921 nf_conntrack_free(ct);
1922 return ERR_PTR(err);
1925 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1926 struct sk_buff *skb,
1927 const struct nlmsghdr *nlh,
1928 const struct nlattr * const cda[],
1929 struct netlink_ext_ack *extack)
1931 struct nf_conntrack_tuple otuple, rtuple;
1932 struct nf_conntrack_tuple_hash *h = NULL;
1933 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1935 u_int8_t u3 = nfmsg->nfgen_family;
1936 struct nf_conntrack_zone zone;
1939 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1943 if (cda[CTA_TUPLE_ORIG]) {
1944 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1950 if (cda[CTA_TUPLE_REPLY]) {
1951 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1957 if (cda[CTA_TUPLE_ORIG])
1958 h = nf_conntrack_find_get(net, &zone, &otuple);
1959 else if (cda[CTA_TUPLE_REPLY])
1960 h = nf_conntrack_find_get(net, &zone, &rtuple);
1964 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1965 enum ip_conntrack_events events;
1967 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1969 if (otuple.dst.protonum != rtuple.dst.protonum)
1972 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1978 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1979 events = 1 << IPCT_RELATED;
1981 events = 1 << IPCT_NEW;
1983 if (cda[CTA_LABELS] &&
1984 ctnetlink_attach_labels(ct, cda) == 0)
1985 events |= (1 << IPCT_LABEL);
1987 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1988 (1 << IPCT_ASSURED) |
1989 (1 << IPCT_HELPER) |
1990 (1 << IPCT_PROTOINFO) |
1991 (1 << IPCT_SEQADJ) |
1992 (1 << IPCT_MARK) | events,
1993 ct, NETLINK_CB(skb).portid,
2000 /* implicit 'else' */
2003 ct = nf_ct_tuplehash_to_ctrack(h);
2004 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2005 err = ctnetlink_change_conntrack(ct, cda);
2007 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2008 (1 << IPCT_ASSURED) |
2009 (1 << IPCT_HELPER) |
2011 (1 << IPCT_PROTOINFO) |
2012 (1 << IPCT_SEQADJ) |
2014 ct, NETLINK_CB(skb).portid,
2024 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2025 __u16 cpu, const struct ip_conntrack_stat *st)
2027 struct nlmsghdr *nlh;
2028 struct nfgenmsg *nfmsg;
2029 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2031 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2032 IPCTNL_MSG_CT_GET_STATS_CPU);
2033 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2037 nfmsg = nlmsg_data(nlh);
2038 nfmsg->nfgen_family = AF_UNSPEC;
2039 nfmsg->version = NFNETLINK_V0;
2040 nfmsg->res_id = htons(cpu);
2042 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2043 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2044 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2045 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2046 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2047 htonl(st->insert_failed)) ||
2048 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2049 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2050 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2051 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2052 htonl(st->search_restart)))
2053 goto nla_put_failure;
2055 nlmsg_end(skb, nlh);
2060 nlmsg_cancel(skb, nlh);
2065 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2068 struct net *net = sock_net(skb->sk);
2070 if (cb->args[0] == nr_cpu_ids)
2073 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2074 const struct ip_conntrack_stat *st;
2076 if (!cpu_possible(cpu))
2079 st = per_cpu_ptr(net->ct.stat, cpu);
2080 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2081 NETLINK_CB(cb->skb).portid,
2091 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2092 struct sk_buff *skb,
2093 const struct nlmsghdr *nlh,
2094 const struct nlattr * const cda[],
2095 struct netlink_ext_ack *extack)
2097 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2098 struct netlink_dump_control c = {
2099 .dump = ctnetlink_ct_stat_cpu_dump,
2101 return netlink_dump_start(ctnl, skb, nlh, &c);
2108 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2111 struct nlmsghdr *nlh;
2112 struct nfgenmsg *nfmsg;
2113 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2114 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2116 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2117 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2121 nfmsg = nlmsg_data(nlh);
2122 nfmsg->nfgen_family = AF_UNSPEC;
2123 nfmsg->version = NFNETLINK_V0;
2126 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2127 goto nla_put_failure;
2129 nlmsg_end(skb, nlh);
2134 nlmsg_cancel(skb, nlh);
2138 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2139 struct sk_buff *skb, const struct nlmsghdr *nlh,
2140 const struct nlattr * const cda[],
2141 struct netlink_ext_ack *extack)
2143 struct sk_buff *skb2;
2146 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2150 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2152 NFNL_MSG_TYPE(nlh->nlmsg_type),
2157 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2166 /* this avoids a loop in nfnetlink. */
2167 return err == -EAGAIN ? -ENOBUFS : err;
2170 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2171 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2172 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2173 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2174 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2175 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2176 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2177 .len = NF_CT_HELPER_NAME_LEN - 1 },
2178 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2179 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2180 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2181 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2182 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2185 static struct nf_conntrack_expect *
2186 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2187 struct nf_conntrack_helper *helper,
2188 struct nf_conntrack_tuple *tuple,
2189 struct nf_conntrack_tuple *mask);
2191 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2193 ctnetlink_glue_build_size(const struct nf_conn *ct)
2195 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2196 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2197 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2198 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2199 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2200 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2201 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2202 + nla_total_size(0) /* CTA_PROTOINFO */
2203 + nla_total_size(0) /* CTA_HELP */
2204 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2205 + ctnetlink_secctx_size(ct)
2206 #ifdef CONFIG_NF_NAT_NEEDED
2207 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2208 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2210 #ifdef CONFIG_NF_CONNTRACK_MARK
2211 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2213 #ifdef CONFIG_NF_CONNTRACK_ZONES
2214 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2216 + ctnetlink_proto_size(ct)
2220 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2221 enum ip_conntrack_info *ctinfo)
2223 return nf_ct_get(skb, ctinfo);
2226 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2228 const struct nf_conntrack_zone *zone;
2229 struct nlattr *nest_parms;
2231 zone = nf_ct_zone(ct);
2233 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2235 goto nla_put_failure;
2236 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2237 goto nla_put_failure;
2238 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2239 NF_CT_ZONE_DIR_ORIG) < 0)
2240 goto nla_put_failure;
2241 nla_nest_end(skb, nest_parms);
2243 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2245 goto nla_put_failure;
2246 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2247 goto nla_put_failure;
2248 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2249 NF_CT_ZONE_DIR_REPL) < 0)
2250 goto nla_put_failure;
2251 nla_nest_end(skb, nest_parms);
2253 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2254 NF_CT_DEFAULT_ZONE_DIR) < 0)
2255 goto nla_put_failure;
2257 if (ctnetlink_dump_id(skb, ct) < 0)
2258 goto nla_put_failure;
2260 if (ctnetlink_dump_status(skb, ct) < 0)
2261 goto nla_put_failure;
2263 if (ctnetlink_dump_timeout(skb, ct) < 0)
2264 goto nla_put_failure;
2266 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2267 goto nla_put_failure;
2269 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2270 goto nla_put_failure;
2272 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2273 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2274 goto nla_put_failure;
2276 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2277 goto nla_put_failure;
2279 if ((ct->status & IPS_SEQ_ADJUST) &&
2280 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2281 goto nla_put_failure;
2283 #ifdef CONFIG_NF_CONNTRACK_MARK
2284 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2285 goto nla_put_failure;
2287 if (ctnetlink_dump_labels(skb, ct) < 0)
2288 goto nla_put_failure;
2296 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2297 enum ip_conntrack_info ctinfo,
2298 u_int16_t ct_attr, u_int16_t ct_info_attr)
2300 struct nlattr *nest_parms;
2302 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2304 goto nla_put_failure;
2306 if (__ctnetlink_glue_build(skb, ct) < 0)
2307 goto nla_put_failure;
2309 nla_nest_end(skb, nest_parms);
2311 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2312 goto nla_put_failure;
2321 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2323 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2324 unsigned long d = ct->status ^ status;
2326 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2327 /* SEEN_REPLY bit can only be set */
2330 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2331 /* ASSURED bit can only be set */
2334 /* This check is less strict than ctnetlink_change_status()
2335 * because callers often flip IPS_EXPECTED bits when sending
2336 * an NFQA_CT attribute to the kernel. So ignore the
2337 * unchangeable bits but do not error out. Also user programs
2338 * are allowed to clear the bits that they are allowed to change.
2340 __ctnetlink_change_status(ct, status, ~status);
2345 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2349 if (cda[CTA_TIMEOUT]) {
2350 err = ctnetlink_change_timeout(ct, cda);
2354 if (cda[CTA_STATUS]) {
2355 err = ctnetlink_update_status(ct, cda);
2359 if (cda[CTA_HELP]) {
2360 err = ctnetlink_change_helper(ct, cda);
2364 if (cda[CTA_LABELS]) {
2365 err = ctnetlink_attach_labels(ct, cda);
2369 #if defined(CONFIG_NF_CONNTRACK_MARK)
2370 if (cda[CTA_MARK]) {
2371 u32 mask = 0, mark, newmark;
2372 if (cda[CTA_MARK_MASK])
2373 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2375 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2376 newmark = (ct->mark & mask) ^ mark;
2377 if (newmark != ct->mark)
2385 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2387 struct nlattr *cda[CTA_MAX+1];
2390 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2394 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2397 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2398 const struct nf_conn *ct,
2399 struct nf_conntrack_tuple *tuple,
2400 struct nf_conntrack_tuple *mask)
2404 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2405 nf_ct_l3num(ct), NULL);
2409 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2410 nf_ct_l3num(ct), NULL);
2414 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2415 u32 portid, u32 report)
2417 struct nlattr *cda[CTA_EXPECT_MAX+1];
2418 struct nf_conntrack_tuple tuple, mask;
2419 struct nf_conntrack_helper *helper = NULL;
2420 struct nf_conntrack_expect *exp;
2423 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2428 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2433 if (cda[CTA_EXPECT_HELP_NAME]) {
2434 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2436 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2437 nf_ct_protonum(ct));
2442 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2443 helper, &tuple, &mask);
2445 return PTR_ERR(exp);
2447 err = nf_ct_expect_related_report(exp, portid, report);
2448 nf_ct_expect_put(exp);
2452 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2453 enum ip_conntrack_info ctinfo, int diff)
2455 if (!(ct->status & IPS_NAT_MASK))
2458 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2461 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2462 .get_ct = ctnetlink_glue_get_ct,
2463 .build_size = ctnetlink_glue_build_size,
2464 .build = ctnetlink_glue_build,
2465 .parse = ctnetlink_glue_parse,
2466 .attach_expect = ctnetlink_glue_attach_expect,
2467 .seq_adjust = ctnetlink_glue_seqadj,
2469 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2471 /***********************************************************************
2473 ***********************************************************************/
2475 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2476 const struct nf_conntrack_tuple *tuple,
2479 struct nlattr *nest_parms;
2481 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2483 goto nla_put_failure;
2484 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2485 goto nla_put_failure;
2486 nla_nest_end(skb, nest_parms);
2494 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2495 const struct nf_conntrack_tuple *tuple,
2496 const struct nf_conntrack_tuple_mask *mask)
2498 const struct nf_conntrack_l3proto *l3proto;
2499 const struct nf_conntrack_l4proto *l4proto;
2500 struct nf_conntrack_tuple m;
2501 struct nlattr *nest_parms;
2504 memset(&m, 0xFF, sizeof(m));
2505 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2506 m.src.u.all = mask->src.u.all;
2507 m.dst.protonum = tuple->dst.protonum;
2509 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2511 goto nla_put_failure;
2514 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2515 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2517 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2518 tuple->dst.protonum);
2519 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2523 if (unlikely(ret < 0))
2524 goto nla_put_failure;
2526 nla_nest_end(skb, nest_parms);
2534 static const union nf_inet_addr any_addr;
2536 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2538 static __read_mostly siphash_key_t exp_id_seed;
2539 unsigned long a, b, c, d;
2541 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2543 a = (unsigned long)exp;
2544 b = (unsigned long)exp->helper;
2545 c = (unsigned long)exp->master;
2546 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2549 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2551 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2556 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2557 const struct nf_conntrack_expect *exp)
2559 struct nf_conn *master = exp->master;
2560 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2561 struct nf_conn_help *help;
2562 #ifdef CONFIG_NF_NAT_NEEDED
2563 struct nlattr *nest_parms;
2564 struct nf_conntrack_tuple nat_tuple = {};
2566 struct nf_ct_helper_expectfn *expfn;
2571 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2572 goto nla_put_failure;
2573 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2574 goto nla_put_failure;
2575 if (ctnetlink_exp_dump_tuple(skb,
2576 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2577 CTA_EXPECT_MASTER) < 0)
2578 goto nla_put_failure;
2580 #ifdef CONFIG_NF_NAT_NEEDED
2581 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2582 exp->saved_proto.all) {
2583 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2585 goto nla_put_failure;
2587 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2588 goto nla_put_failure;
2590 nat_tuple.src.l3num = nf_ct_l3num(master);
2591 nat_tuple.src.u3 = exp->saved_addr;
2592 nat_tuple.dst.protonum = nf_ct_protonum(master);
2593 nat_tuple.src.u = exp->saved_proto;
2595 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2596 CTA_EXPECT_NAT_TUPLE) < 0)
2597 goto nla_put_failure;
2598 nla_nest_end(skb, nest_parms);
2601 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2602 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
2603 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2604 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2605 goto nla_put_failure;
2606 help = nfct_help(master);
2608 struct nf_conntrack_helper *helper;
2610 helper = rcu_dereference(help->helper);
2612 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2613 goto nla_put_failure;
2615 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2616 if (expfn != NULL &&
2617 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2618 goto nla_put_failure;
2627 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2628 int event, const struct nf_conntrack_expect *exp)
2630 struct nlmsghdr *nlh;
2631 struct nfgenmsg *nfmsg;
2632 unsigned int flags = portid ? NLM_F_MULTI : 0;
2634 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2635 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2639 nfmsg = nlmsg_data(nlh);
2640 nfmsg->nfgen_family = exp->tuple.src.l3num;
2641 nfmsg->version = NFNETLINK_V0;
2644 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2645 goto nla_put_failure;
2647 nlmsg_end(skb, nlh);
2652 nlmsg_cancel(skb, nlh);
2656 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2658 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2660 struct nf_conntrack_expect *exp = item->exp;
2661 struct net *net = nf_ct_exp_net(exp);
2662 struct nlmsghdr *nlh;
2663 struct nfgenmsg *nfmsg;
2664 struct sk_buff *skb;
2665 unsigned int type, group;
2668 if (events & (1 << IPEXP_DESTROY)) {
2669 type = IPCTNL_MSG_EXP_DELETE;
2670 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2671 } else if (events & (1 << IPEXP_NEW)) {
2672 type = IPCTNL_MSG_EXP_NEW;
2673 flags = NLM_F_CREATE|NLM_F_EXCL;
2674 group = NFNLGRP_CONNTRACK_EXP_NEW;
2678 if (!item->report && !nfnetlink_has_listeners(net, group))
2681 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2685 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2686 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2690 nfmsg = nlmsg_data(nlh);
2691 nfmsg->nfgen_family = exp->tuple.src.l3num;
2692 nfmsg->version = NFNETLINK_V0;
2695 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2696 goto nla_put_failure;
2698 nlmsg_end(skb, nlh);
2699 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2703 nlmsg_cancel(skb, nlh);
2707 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2711 static int ctnetlink_exp_done(struct netlink_callback *cb)
2714 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2719 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2721 struct net *net = sock_net(skb->sk);
2722 struct nf_conntrack_expect *exp, *last;
2723 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2724 u_int8_t l3proto = nfmsg->nfgen_family;
2727 last = (struct nf_conntrack_expect *)cb->args[1];
2728 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2730 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2732 if (l3proto && exp->tuple.src.l3num != l3proto)
2735 if (!net_eq(nf_ct_net(exp->master), net))
2743 if (ctnetlink_exp_fill_info(skb,
2744 NETLINK_CB(cb->skb).portid,
2748 if (!refcount_inc_not_zero(&exp->use))
2750 cb->args[1] = (unsigned long)exp;
2762 nf_ct_expect_put(last);
2768 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2770 struct nf_conntrack_expect *exp, *last;
2771 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2772 struct nf_conn *ct = cb->data;
2773 struct nf_conn_help *help = nfct_help(ct);
2774 u_int8_t l3proto = nfmsg->nfgen_family;
2780 last = (struct nf_conntrack_expect *)cb->args[1];
2782 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2783 if (l3proto && exp->tuple.src.l3num != l3proto)
2790 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2794 if (!refcount_inc_not_zero(&exp->use))
2796 cb->args[1] = (unsigned long)exp;
2808 nf_ct_expect_put(last);
2813 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2814 struct sk_buff *skb,
2815 const struct nlmsghdr *nlh,
2816 const struct nlattr * const cda[],
2817 struct netlink_ext_ack *extack)
2820 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2821 u_int8_t u3 = nfmsg->nfgen_family;
2822 struct nf_conntrack_tuple tuple;
2823 struct nf_conntrack_tuple_hash *h;
2825 struct nf_conntrack_zone zone;
2826 struct netlink_dump_control c = {
2827 .dump = ctnetlink_exp_ct_dump_table,
2828 .done = ctnetlink_exp_done,
2831 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2836 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2840 h = nf_conntrack_find_get(net, &zone, &tuple);
2844 ct = nf_ct_tuplehash_to_ctrack(h);
2845 /* No expectation linked to this connection tracking. */
2846 if (!nfct_help(ct)) {
2853 err = netlink_dump_start(ctnl, skb, nlh, &c);
2859 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2860 struct sk_buff *skb, const struct nlmsghdr *nlh,
2861 const struct nlattr * const cda[],
2862 struct netlink_ext_ack *extack)
2864 struct nf_conntrack_tuple tuple;
2865 struct nf_conntrack_expect *exp;
2866 struct sk_buff *skb2;
2867 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2868 u_int8_t u3 = nfmsg->nfgen_family;
2869 struct nf_conntrack_zone zone;
2872 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2873 if (cda[CTA_EXPECT_MASTER])
2874 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
2877 struct netlink_dump_control c = {
2878 .dump = ctnetlink_exp_dump_table,
2879 .done = ctnetlink_exp_done,
2881 return netlink_dump_start(ctnl, skb, nlh, &c);
2885 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2889 if (cda[CTA_EXPECT_TUPLE])
2890 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2892 else if (cda[CTA_EXPECT_MASTER])
2893 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2901 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2905 if (cda[CTA_EXPECT_ID]) {
2906 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2908 if (id != nf_expect_get_id(exp)) {
2909 nf_ct_expect_put(exp);
2915 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2917 nf_ct_expect_put(exp);
2922 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2923 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2925 nf_ct_expect_put(exp);
2929 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2938 /* this avoids a loop in nfnetlink. */
2939 return err == -EAGAIN ? -ENOBUFS : err;
2942 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
2944 const struct nf_conn_help *m_help;
2945 const char *name = data;
2947 m_help = nfct_help(exp->master);
2949 return strcmp(m_help->helper->name, name) == 0;
2952 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
2957 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2958 struct sk_buff *skb, const struct nlmsghdr *nlh,
2959 const struct nlattr * const cda[],
2960 struct netlink_ext_ack *extack)
2962 struct nf_conntrack_expect *exp;
2963 struct nf_conntrack_tuple tuple;
2964 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2965 u_int8_t u3 = nfmsg->nfgen_family;
2966 struct nf_conntrack_zone zone;
2969 if (cda[CTA_EXPECT_TUPLE]) {
2970 /* delete a single expect by tuple */
2971 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2975 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2980 /* bump usage count to 2 */
2981 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2985 if (cda[CTA_EXPECT_ID]) {
2986 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2987 if (ntohl(id) != (u32)(unsigned long)exp) {
2988 nf_ct_expect_put(exp);
2993 /* after list removal, usage count == 1 */
2994 spin_lock_bh(&nf_conntrack_expect_lock);
2995 if (del_timer(&exp->timeout)) {
2996 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2998 nf_ct_expect_put(exp);
3000 spin_unlock_bh(&nf_conntrack_expect_lock);
3001 /* have to put what we 'get' above.
3002 * after this line usage count == 0 */
3003 nf_ct_expect_put(exp);
3004 } else if (cda[CTA_EXPECT_HELP_NAME]) {
3005 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3007 nf_ct_expect_iterate_net(net, expect_iter_name, name,
3008 NETLINK_CB(skb).portid,
3011 /* This basically means we have to flush everything*/
3012 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
3013 NETLINK_CB(skb).portid,
3020 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3021 const struct nlattr * const cda[])
3023 if (cda[CTA_EXPECT_TIMEOUT]) {
3024 if (!del_timer(&x->timeout))
3027 x->timeout.expires = jiffies +
3028 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3029 add_timer(&x->timeout);
3034 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3035 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3036 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3040 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3041 struct nf_conntrack_expect *exp,
3044 #ifdef CONFIG_NF_NAT_NEEDED
3045 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3046 struct nf_conntrack_tuple nat_tuple = {};
3049 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3050 exp_nat_nla_policy, NULL);
3054 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3057 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3058 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3063 exp->saved_addr = nat_tuple.src.u3;
3064 exp->saved_proto = nat_tuple.src.u;
3065 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3073 static struct nf_conntrack_expect *
3074 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3075 struct nf_conntrack_helper *helper,
3076 struct nf_conntrack_tuple *tuple,
3077 struct nf_conntrack_tuple *mask)
3079 u_int32_t class = 0;
3080 struct nf_conntrack_expect *exp;
3081 struct nf_conn_help *help;
3084 help = nfct_help(ct);
3086 return ERR_PTR(-EOPNOTSUPP);
3088 if (cda[CTA_EXPECT_CLASS] && helper) {
3089 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3090 if (class > helper->expect_class_max)
3091 return ERR_PTR(-EINVAL);
3093 exp = nf_ct_expect_alloc(ct);
3095 return ERR_PTR(-ENOMEM);
3097 if (cda[CTA_EXPECT_FLAGS]) {
3098 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3099 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3103 if (cda[CTA_EXPECT_FN]) {
3104 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3105 struct nf_ct_helper_expectfn *expfn;
3107 expfn = nf_ct_helper_expectfn_find_by_name(name);
3108 if (expfn == NULL) {
3112 exp->expectfn = expfn->expectfn;
3114 exp->expectfn = NULL;
3118 exp->helper = helper;
3119 exp->tuple = *tuple;
3120 exp->mask.src.u3 = mask->src.u3;
3121 exp->mask.src.u.all = mask->src.u.all;
3123 if (cda[CTA_EXPECT_NAT]) {
3124 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3125 exp, nf_ct_l3num(ct));
3131 nf_ct_expect_put(exp);
3132 return ERR_PTR(err);
3136 ctnetlink_create_expect(struct net *net,
3137 const struct nf_conntrack_zone *zone,
3138 const struct nlattr * const cda[],
3139 u_int8_t u3, u32 portid, int report)
3141 struct nf_conntrack_tuple tuple, mask, master_tuple;
3142 struct nf_conntrack_tuple_hash *h = NULL;
3143 struct nf_conntrack_helper *helper = NULL;
3144 struct nf_conntrack_expect *exp;
3148 /* caller guarantees that those three CTA_EXPECT_* exist */
3149 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3153 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3157 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3162 /* Look for master conntrack of this expectation */
3163 h = nf_conntrack_find_get(net, zone, &master_tuple);
3166 ct = nf_ct_tuplehash_to_ctrack(h);
3169 if (cda[CTA_EXPECT_HELP_NAME]) {
3170 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3172 helper = __nf_conntrack_helper_find(helpname, u3,
3173 nf_ct_protonum(ct));
3174 if (helper == NULL) {
3176 #ifdef CONFIG_MODULES
3177 if (request_module("nfct-helper-%s", helpname) < 0) {
3182 helper = __nf_conntrack_helper_find(helpname, u3,
3183 nf_ct_protonum(ct));
3195 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3201 err = nf_ct_expect_related_report(exp, portid, report);
3202 nf_ct_expect_put(exp);
3210 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3211 struct sk_buff *skb, const struct nlmsghdr *nlh,
3212 const struct nlattr * const cda[],
3213 struct netlink_ext_ack *extack)
3215 struct nf_conntrack_tuple tuple;
3216 struct nf_conntrack_expect *exp;
3217 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3218 u_int8_t u3 = nfmsg->nfgen_family;
3219 struct nf_conntrack_zone zone;
3222 if (!cda[CTA_EXPECT_TUPLE]
3223 || !cda[CTA_EXPECT_MASK]
3224 || !cda[CTA_EXPECT_MASTER])
3227 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3231 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3236 spin_lock_bh(&nf_conntrack_expect_lock);
3237 exp = __nf_ct_expect_find(net, &zone, &tuple);
3239 spin_unlock_bh(&nf_conntrack_expect_lock);
3241 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3242 err = ctnetlink_create_expect(net, &zone, cda, u3,
3243 NETLINK_CB(skb).portid,
3250 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3251 err = ctnetlink_change_expect(exp, cda);
3252 spin_unlock_bh(&nf_conntrack_expect_lock);
3258 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3259 const struct ip_conntrack_stat *st)
3261 struct nlmsghdr *nlh;
3262 struct nfgenmsg *nfmsg;
3263 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3265 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3266 IPCTNL_MSG_EXP_GET_STATS_CPU);
3267 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3271 nfmsg = nlmsg_data(nlh);
3272 nfmsg->nfgen_family = AF_UNSPEC;
3273 nfmsg->version = NFNETLINK_V0;
3274 nfmsg->res_id = htons(cpu);
3276 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3277 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3278 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3279 goto nla_put_failure;
3281 nlmsg_end(skb, nlh);
3286 nlmsg_cancel(skb, nlh);
3291 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3294 struct net *net = sock_net(skb->sk);
3296 if (cb->args[0] == nr_cpu_ids)
3299 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3300 const struct ip_conntrack_stat *st;
3302 if (!cpu_possible(cpu))
3305 st = per_cpu_ptr(net->ct.stat, cpu);
3306 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3316 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3317 struct sk_buff *skb,
3318 const struct nlmsghdr *nlh,
3319 const struct nlattr * const cda[],
3320 struct netlink_ext_ack *extack)
3322 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3323 struct netlink_dump_control c = {
3324 .dump = ctnetlink_exp_stat_cpu_dump,
3326 return netlink_dump_start(ctnl, skb, nlh, &c);
3332 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3333 static struct nf_ct_event_notifier ctnl_notifier = {
3334 .fcn = ctnetlink_conntrack_event,
3337 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3338 .fcn = ctnetlink_expect_event,
3342 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3343 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3344 .attr_count = CTA_MAX,
3345 .policy = ct_nla_policy },
3346 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3347 .attr_count = CTA_MAX,
3348 .policy = ct_nla_policy },
3349 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3350 .attr_count = CTA_MAX,
3351 .policy = ct_nla_policy },
3352 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3353 .attr_count = CTA_MAX,
3354 .policy = ct_nla_policy },
3355 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3356 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3357 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3358 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3361 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3362 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3363 .attr_count = CTA_EXPECT_MAX,
3364 .policy = exp_nla_policy },
3365 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3366 .attr_count = CTA_EXPECT_MAX,
3367 .policy = exp_nla_policy },
3368 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3369 .attr_count = CTA_EXPECT_MAX,
3370 .policy = exp_nla_policy },
3371 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3374 static const struct nfnetlink_subsystem ctnl_subsys = {
3375 .name = "conntrack",
3376 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3377 .cb_count = IPCTNL_MSG_MAX,
3381 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3382 .name = "conntrack_expect",
3383 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3384 .cb_count = IPCTNL_MSG_EXP_MAX,
3388 MODULE_ALIAS("ip_conntrack_netlink");
3389 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3390 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3392 static int __net_init ctnetlink_net_init(struct net *net)
3394 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3397 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3399 pr_err("ctnetlink_init: cannot register notifier.\n");
3403 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3405 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3406 goto err_unreg_notifier;
3411 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3413 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3419 static void ctnetlink_net_exit(struct net *net)
3421 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3422 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3423 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3427 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3431 list_for_each_entry(net, net_exit_list, exit_list)
3432 ctnetlink_net_exit(net);
3434 /* wait for other cpus until they are done with ctnl_notifiers */
3438 static struct pernet_operations ctnetlink_net_ops = {
3439 .init = ctnetlink_net_init,
3440 .exit_batch = ctnetlink_net_exit_batch,
3443 static int __init ctnetlink_init(void)
3447 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3448 ret = nfnetlink_subsys_register(&ctnl_subsys);
3450 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3454 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3456 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3457 goto err_unreg_subsys;
3460 ret = register_pernet_subsys(&ctnetlink_net_ops);
3462 pr_err("ctnetlink_init: cannot register pernet operations\n");
3463 goto err_unreg_exp_subsys;
3465 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3466 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3467 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3471 err_unreg_exp_subsys:
3472 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3474 nfnetlink_subsys_unregister(&ctnl_subsys);
3479 static void __exit ctnetlink_exit(void)
3481 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3483 unregister_pernet_subsys(&ctnetlink_net_ops);
3484 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3485 nfnetlink_subsys_unregister(&ctnl_subsys);
3486 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3487 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3492 module_init(ctnetlink_init);
3493 module_exit(ctnetlink_exit);