1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>
5 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005-2012 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_seqadj.h>
41 #include <net/netfilter/nf_conntrack_l3proto.h>
42 #include <net/netfilter/nf_conntrack_l4proto.h>
43 #include <net/netfilter/nf_conntrack_tuple.h>
44 #include <net/netfilter/nf_conntrack_acct.h>
45 #include <net/netfilter/nf_conntrack_zones.h>
46 #include <net/netfilter/nf_conntrack_timestamp.h>
47 #include <net/netfilter/nf_conntrack_labels.h>
48 #include <net/netfilter/nf_conntrack_synproxy.h>
49 #ifdef CONFIG_NF_NAT_NEEDED
50 #include <net/netfilter/nf_nat_core.h>
51 #include <net/netfilter/nf_nat_l4proto.h>
52 #include <net/netfilter/nf_nat_helper.h>
55 #include <linux/netfilter/nfnetlink.h>
56 #include <linux/netfilter/nfnetlink_conntrack.h>
58 MODULE_LICENSE("GPL");
60 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
61 const struct nf_conntrack_tuple *tuple,
62 const struct nf_conntrack_l4proto *l4proto)
65 struct nlattr *nest_parms;
67 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
70 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
73 if (likely(l4proto->tuple_to_nlattr))
74 ret = l4proto->tuple_to_nlattr(skb, tuple);
76 nla_nest_end(skb, nest_parms);
84 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
85 const struct nf_conntrack_tuple *tuple,
86 const struct nf_conntrack_l3proto *l3proto)
89 struct nlattr *nest_parms;
91 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
95 if (likely(l3proto->tuple_to_nlattr))
96 ret = l3proto->tuple_to_nlattr(skb, tuple);
98 nla_nest_end(skb, nest_parms);
106 static int ctnetlink_dump_tuples(struct sk_buff *skb,
107 const struct nf_conntrack_tuple *tuple)
109 const struct nf_conntrack_l3proto *l3proto;
110 const struct nf_conntrack_l4proto *l4proto;
114 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
115 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
118 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
119 tuple->dst.protonum);
120 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
126 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
127 const struct nf_conntrack_zone *zone, int dir)
129 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
131 if (nla_put_be16(skb, attrtype, htons(zone->id)))
132 goto nla_put_failure;
139 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
141 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
142 goto nla_put_failure;
149 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
151 long timeout = nf_ct_expires(ct) / HZ;
153 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
154 goto nla_put_failure;
161 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
163 const struct nf_conntrack_l4proto *l4proto;
164 struct nlattr *nest_proto;
167 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
168 if (!l4proto->to_nlattr)
171 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
173 goto nla_put_failure;
175 ret = l4proto->to_nlattr(skb, nest_proto, ct);
177 nla_nest_end(skb, nest_proto);
185 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
186 const struct nf_conn *ct)
188 struct nlattr *nest_helper;
189 const struct nf_conn_help *help = nfct_help(ct);
190 struct nf_conntrack_helper *helper;
195 helper = rcu_dereference(help->helper);
199 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
201 goto nla_put_failure;
202 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
203 goto nla_put_failure;
205 if (helper->to_nlattr)
206 helper->to_nlattr(skb, ct);
208 nla_nest_end(skb, nest_helper);
217 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
218 enum ip_conntrack_dir dir, int type)
220 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
221 struct nf_conn_counter *counter = acct->counter;
222 struct nlattr *nest_count;
225 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
226 pkts = atomic64_xchg(&counter[dir].packets, 0);
227 bytes = atomic64_xchg(&counter[dir].bytes, 0);
229 pkts = atomic64_read(&counter[dir].packets);
230 bytes = atomic64_read(&counter[dir].bytes);
233 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
235 goto nla_put_failure;
237 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
239 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
241 goto nla_put_failure;
243 nla_nest_end(skb, nest_count);
252 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
254 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
259 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
261 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
268 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
270 struct nlattr *nest_count;
271 const struct nf_conn_tstamp *tstamp;
273 tstamp = nf_conn_tstamp_find(ct);
277 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
279 goto nla_put_failure;
281 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
282 CTA_TIMESTAMP_PAD) ||
283 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
284 cpu_to_be64(tstamp->stop),
286 goto nla_put_failure;
287 nla_nest_end(skb, nest_count);
295 #ifdef CONFIG_NF_CONNTRACK_MARK
296 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
298 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
299 goto nla_put_failure;
306 #define ctnetlink_dump_mark(a, b) (0)
309 #ifdef CONFIG_NF_CONNTRACK_SECMARK
310 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
312 struct nlattr *nest_secctx;
316 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
321 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
323 goto nla_put_failure;
325 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
326 goto nla_put_failure;
327 nla_nest_end(skb, nest_secctx);
331 security_release_secctx(secctx, len);
335 #define ctnetlink_dump_secctx(a, b) (0)
338 #ifdef CONFIG_NF_CONNTRACK_LABELS
339 static inline int ctnetlink_label_size(const struct nf_conn *ct)
341 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
345 return nla_total_size(sizeof(labels->bits));
349 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
351 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
359 if (labels->bits[i] != 0)
360 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
363 } while (i < ARRAY_SIZE(labels->bits));
368 #define ctnetlink_dump_labels(a, b) (0)
369 #define ctnetlink_label_size(a) (0)
372 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
374 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
376 struct nlattr *nest_parms;
378 if (!(ct->status & IPS_EXPECTED))
381 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
383 goto nla_put_failure;
384 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
385 goto nla_put_failure;
386 nla_nest_end(skb, nest_parms);
395 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
397 struct nlattr *nest_parms;
399 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
401 goto nla_put_failure;
403 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
404 htonl(seq->correction_pos)) ||
405 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
406 htonl(seq->offset_before)) ||
407 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
408 htonl(seq->offset_after)))
409 goto nla_put_failure;
411 nla_nest_end(skb, nest_parms);
419 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
421 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
422 struct nf_ct_seqadj *seq;
424 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
427 spin_lock_bh(&ct->lock);
428 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
429 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
432 seq = &seqadj->seq[IP_CT_DIR_REPLY];
433 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
436 spin_unlock_bh(&ct->lock);
439 spin_unlock_bh(&ct->lock);
443 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
445 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
446 goto nla_put_failure;
453 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
455 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
456 goto nla_put_failure;
464 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
467 const struct nf_conntrack_zone *zone;
468 struct nlmsghdr *nlh;
469 struct nfgenmsg *nfmsg;
470 struct nlattr *nest_parms;
471 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
473 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
474 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
478 nfmsg = nlmsg_data(nlh);
479 nfmsg->nfgen_family = nf_ct_l3num(ct);
480 nfmsg->version = NFNETLINK_V0;
483 zone = nf_ct_zone(ct);
485 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
487 goto nla_put_failure;
488 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
489 goto nla_put_failure;
490 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
491 NF_CT_ZONE_DIR_ORIG) < 0)
492 goto nla_put_failure;
493 nla_nest_end(skb, nest_parms);
495 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
497 goto nla_put_failure;
498 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
499 goto nla_put_failure;
500 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
501 NF_CT_ZONE_DIR_REPL) < 0)
502 goto nla_put_failure;
503 nla_nest_end(skb, nest_parms);
505 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
506 NF_CT_DEFAULT_ZONE_DIR) < 0)
507 goto nla_put_failure;
509 if (ctnetlink_dump_status(skb, ct) < 0 ||
510 ctnetlink_dump_timeout(skb, ct) < 0 ||
511 ctnetlink_dump_acct(skb, ct, type) < 0 ||
512 ctnetlink_dump_timestamp(skb, ct) < 0 ||
513 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
514 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
515 ctnetlink_dump_mark(skb, ct) < 0 ||
516 ctnetlink_dump_secctx(skb, ct) < 0 ||
517 ctnetlink_dump_labels(skb, ct) < 0 ||
518 ctnetlink_dump_id(skb, ct) < 0 ||
519 ctnetlink_dump_use(skb, ct) < 0 ||
520 ctnetlink_dump_master(skb, ct) < 0 ||
521 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
522 goto nla_put_failure;
529 nlmsg_cancel(skb, nlh);
533 #if defined(CONFIG_NETFILTER_NETLINK_GLUE_CT) || defined(CONFIG_NF_CONNTRACK_EVENTS)
534 static size_t ctnetlink_proto_size(const struct nf_conn *ct)
536 const struct nf_conntrack_l3proto *l3proto;
537 const struct nf_conntrack_l4proto *l4proto;
538 size_t len, len4 = 0;
540 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
541 len = l3proto->nla_size;
542 len *= 3u; /* ORIG, REPLY, MASTER */
544 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
545 len += l4proto->nlattr_size;
546 if (l4proto->nlattr_tuple_size) {
547 len4 = l4proto->nlattr_tuple_size();
548 len4 *= 3u; /* ORIG, REPLY, MASTER */
555 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
557 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
559 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
560 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
561 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
565 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
567 #ifdef CONFIG_NF_CONNTRACK_SECMARK
570 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
574 return nla_total_size(0) /* CTA_SECCTX */
575 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
581 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
583 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
584 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
586 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
592 #ifdef CONFIG_NF_CONNTRACK_EVENTS
593 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
595 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
596 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
597 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
598 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
599 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
600 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
601 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
602 + ctnetlink_acct_size(ct)
603 + ctnetlink_timestamp_size(ct)
604 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
605 + nla_total_size(0) /* CTA_PROTOINFO */
606 + nla_total_size(0) /* CTA_HELP */
607 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
608 + ctnetlink_secctx_size(ct)
609 #ifdef CONFIG_NF_NAT_NEEDED
610 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
611 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
613 #ifdef CONFIG_NF_CONNTRACK_MARK
614 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
616 #ifdef CONFIG_NF_CONNTRACK_ZONES
617 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
619 + ctnetlink_proto_size(ct)
620 + ctnetlink_label_size(ct)
625 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
627 const struct nf_conntrack_zone *zone;
629 struct nlmsghdr *nlh;
630 struct nfgenmsg *nfmsg;
631 struct nlattr *nest_parms;
632 struct nf_conn *ct = item->ct;
635 unsigned int flags = 0, group;
638 if (events & (1 << IPCT_DESTROY)) {
639 type = IPCTNL_MSG_CT_DELETE;
640 group = NFNLGRP_CONNTRACK_DESTROY;
641 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
642 type = IPCTNL_MSG_CT_NEW;
643 flags = NLM_F_CREATE|NLM_F_EXCL;
644 group = NFNLGRP_CONNTRACK_NEW;
646 type = IPCTNL_MSG_CT_NEW;
647 group = NFNLGRP_CONNTRACK_UPDATE;
652 if (!item->report && !nfnetlink_has_listeners(net, group))
655 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
659 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
660 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
664 nfmsg = nlmsg_data(nlh);
665 nfmsg->nfgen_family = nf_ct_l3num(ct);
666 nfmsg->version = NFNETLINK_V0;
669 zone = nf_ct_zone(ct);
671 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
673 goto nla_put_failure;
674 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
675 goto nla_put_failure;
676 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
677 NF_CT_ZONE_DIR_ORIG) < 0)
678 goto nla_put_failure;
679 nla_nest_end(skb, nest_parms);
681 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
683 goto nla_put_failure;
684 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
685 goto nla_put_failure;
686 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
687 NF_CT_ZONE_DIR_REPL) < 0)
688 goto nla_put_failure;
689 nla_nest_end(skb, nest_parms);
691 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
692 NF_CT_DEFAULT_ZONE_DIR) < 0)
693 goto nla_put_failure;
695 if (ctnetlink_dump_id(skb, ct) < 0)
696 goto nla_put_failure;
698 if (ctnetlink_dump_status(skb, ct) < 0)
699 goto nla_put_failure;
701 if (events & (1 << IPCT_DESTROY)) {
702 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
703 ctnetlink_dump_timestamp(skb, ct) < 0)
704 goto nla_put_failure;
706 if (ctnetlink_dump_timeout(skb, ct) < 0)
707 goto nla_put_failure;
709 if (events & (1 << IPCT_PROTOINFO)
710 && ctnetlink_dump_protoinfo(skb, ct) < 0)
711 goto nla_put_failure;
713 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
714 && ctnetlink_dump_helpinfo(skb, ct) < 0)
715 goto nla_put_failure;
717 #ifdef CONFIG_NF_CONNTRACK_SECMARK
718 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
719 && ctnetlink_dump_secctx(skb, ct) < 0)
720 goto nla_put_failure;
722 if (events & (1 << IPCT_LABEL) &&
723 ctnetlink_dump_labels(skb, ct) < 0)
724 goto nla_put_failure;
726 if (events & (1 << IPCT_RELATED) &&
727 ctnetlink_dump_master(skb, ct) < 0)
728 goto nla_put_failure;
730 if (events & (1 << IPCT_SEQADJ) &&
731 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
732 goto nla_put_failure;
735 #ifdef CONFIG_NF_CONNTRACK_MARK
736 if ((events & (1 << IPCT_MARK) || ct->mark)
737 && ctnetlink_dump_mark(skb, ct) < 0)
738 goto nla_put_failure;
741 err = nfnetlink_send(skb, net, item->portid, group, item->report,
743 if (err == -ENOBUFS || err == -EAGAIN)
749 nlmsg_cancel(skb, nlh);
753 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
758 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
760 static int ctnetlink_done(struct netlink_callback *cb)
763 nf_ct_put((struct nf_conn *)cb->args[1]);
768 struct ctnetlink_filter {
775 static struct ctnetlink_filter *
776 ctnetlink_alloc_filter(const struct nlattr * const cda[])
778 #ifdef CONFIG_NF_CONNTRACK_MARK
779 struct ctnetlink_filter *filter;
781 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
783 return ERR_PTR(-ENOMEM);
785 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
786 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
790 return ERR_PTR(-EOPNOTSUPP);
794 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
796 struct ctnetlink_filter *filter = data;
801 #ifdef CONFIG_NF_CONNTRACK_MARK
802 if ((ct->mark & filter->mark.mask) == filter->mark.val)
810 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
812 struct net *net = sock_net(skb->sk);
813 struct nf_conn *ct, *last;
814 struct nf_conntrack_tuple_hash *h;
815 struct hlist_nulls_node *n;
816 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
817 u_int8_t l3proto = nfmsg->nfgen_family;
818 struct nf_conn *nf_ct_evict[8];
822 last = (struct nf_conn *)cb->args[1];
826 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
830 if (nf_ct_should_gc(nf_ct_evict[i]))
831 nf_ct_kill(nf_ct_evict[i]);
832 nf_ct_put(nf_ct_evict[i]);
835 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
836 nf_conntrack_lock(lockp);
837 if (cb->args[0] >= nf_conntrack_htable_size) {
841 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
843 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
845 ct = nf_ct_tuplehash_to_ctrack(h);
846 if (nf_ct_is_expired(ct)) {
847 if (i < ARRAY_SIZE(nf_ct_evict) &&
848 atomic_inc_not_zero(&ct->ct_general.use))
849 nf_ct_evict[i++] = ct;
853 if (!net_eq(net, nf_ct_net(ct)))
856 /* Dump entries of a given L3 protocol number.
857 * If it is not specified, ie. l3proto == 0,
858 * then dump everything. */
859 if (l3proto && nf_ct_l3num(ct) != l3proto)
866 if (!ctnetlink_filter_match(ct, cb->data))
871 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
873 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
877 nf_conntrack_get(&ct->ct_general);
878 cb->args[1] = (unsigned long)ct;
892 /* nf ct hash resize happened, now clear the leftover. */
893 if ((struct nf_conn *)cb->args[1] == last)
901 if (nf_ct_should_gc(nf_ct_evict[i]))
902 nf_ct_kill(nf_ct_evict[i]);
903 nf_ct_put(nf_ct_evict[i]);
909 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
910 struct nf_conntrack_tuple *tuple)
912 struct nlattr *tb[CTA_IP_MAX+1];
913 struct nf_conntrack_l3proto *l3proto;
916 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
921 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
923 if (likely(l3proto->nlattr_to_tuple)) {
924 ret = nla_validate_nested(attr, CTA_IP_MAX,
925 l3proto->nla_policy, NULL);
927 ret = l3proto->nlattr_to_tuple(tb, tuple);
935 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
936 [CTA_PROTO_NUM] = { .type = NLA_U8 },
939 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
940 struct nf_conntrack_tuple *tuple)
942 const struct nf_conntrack_l4proto *l4proto;
943 struct nlattr *tb[CTA_PROTO_MAX+1];
946 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
951 if (!tb[CTA_PROTO_NUM])
953 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
956 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
958 if (likely(l4proto->nlattr_to_tuple)) {
959 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
960 l4proto->nla_policy, NULL);
962 ret = l4proto->nlattr_to_tuple(tb, tuple);
971 ctnetlink_parse_zone(const struct nlattr *attr,
972 struct nf_conntrack_zone *zone)
974 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
975 NF_CT_DEFAULT_ZONE_DIR, 0);
976 #ifdef CONFIG_NF_CONNTRACK_ZONES
978 zone->id = ntohs(nla_get_be16(attr));
987 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
988 struct nf_conntrack_zone *zone)
992 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
995 ret = ctnetlink_parse_zone(attr, zone);
999 if (type == CTA_TUPLE_REPLY)
1000 zone->dir = NF_CT_ZONE_DIR_REPL;
1002 zone->dir = NF_CT_ZONE_DIR_ORIG;
1007 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1008 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1009 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1010 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1014 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1015 struct nf_conntrack_tuple *tuple, u32 type,
1016 u_int8_t l3num, struct nf_conntrack_zone *zone)
1018 struct nlattr *tb[CTA_TUPLE_MAX+1];
1021 memset(tuple, 0, sizeof(*tuple));
1023 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1028 if (!tb[CTA_TUPLE_IP])
1031 tuple->src.l3num = l3num;
1033 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1037 if (!tb[CTA_TUPLE_PROTO])
1040 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1044 if (tb[CTA_TUPLE_ZONE]) {
1048 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1054 /* orig and expect tuples get DIR_ORIGINAL */
1055 if (type == CTA_TUPLE_REPLY)
1056 tuple->dst.dir = IP_CT_DIR_REPLY;
1058 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1063 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1064 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1065 .len = NF_CT_HELPER_NAME_LEN - 1 },
1068 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1069 struct nlattr **helpinfo)
1072 struct nlattr *tb[CTA_HELP_MAX+1];
1074 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1078 if (!tb[CTA_HELP_NAME])
1081 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1083 if (tb[CTA_HELP_INFO])
1084 *helpinfo = tb[CTA_HELP_INFO];
1089 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1090 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1091 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1092 [CTA_STATUS] = { .type = NLA_U32 },
1093 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1094 [CTA_HELP] = { .type = NLA_NESTED },
1095 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1096 [CTA_TIMEOUT] = { .type = NLA_U32 },
1097 [CTA_MARK] = { .type = NLA_U32 },
1098 [CTA_ID] = { .type = NLA_U32 },
1099 [CTA_NAT_DST] = { .type = NLA_NESTED },
1100 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1101 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1102 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1103 [CTA_ZONE] = { .type = NLA_U16 },
1104 [CTA_MARK_MASK] = { .type = NLA_U32 },
1105 [CTA_LABELS] = { .type = NLA_BINARY,
1106 .len = NF_CT_LABELS_MAX_SIZE },
1107 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1108 .len = NF_CT_LABELS_MAX_SIZE },
1111 static int ctnetlink_flush_iterate(struct nf_conn *ct, void *data)
1113 if (test_bit(IPS_OFFLOAD_BIT, &ct->status))
1116 return ctnetlink_filter_match(ct, data);
1119 static int ctnetlink_flush_conntrack(struct net *net,
1120 const struct nlattr * const cda[],
1121 u32 portid, int report)
1123 struct ctnetlink_filter *filter = NULL;
1125 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1126 filter = ctnetlink_alloc_filter(cda);
1128 return PTR_ERR(filter);
1131 nf_ct_iterate_cleanup_net(net, ctnetlink_flush_iterate, filter,
1138 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1139 struct sk_buff *skb,
1140 const struct nlmsghdr *nlh,
1141 const struct nlattr * const cda[],
1142 struct netlink_ext_ack *extack)
1144 struct nf_conntrack_tuple_hash *h;
1145 struct nf_conntrack_tuple tuple;
1147 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1148 u_int8_t u3 = nfmsg->nfgen_family;
1149 struct nf_conntrack_zone zone;
1152 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1156 if (cda[CTA_TUPLE_ORIG])
1157 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1159 else if (cda[CTA_TUPLE_REPLY])
1160 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1163 return ctnetlink_flush_conntrack(net, cda,
1164 NETLINK_CB(skb).portid,
1171 h = nf_conntrack_find_get(net, &zone, &tuple);
1175 ct = nf_ct_tuplehash_to_ctrack(h);
1177 if (test_bit(IPS_OFFLOAD_BIT, &ct->status)) {
1183 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1184 if (id != (u32)(unsigned long)ct) {
1190 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1196 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1197 struct sk_buff *skb,
1198 const struct nlmsghdr *nlh,
1199 const struct nlattr * const cda[],
1200 struct netlink_ext_ack *extack)
1202 struct nf_conntrack_tuple_hash *h;
1203 struct nf_conntrack_tuple tuple;
1205 struct sk_buff *skb2 = NULL;
1206 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1207 u_int8_t u3 = nfmsg->nfgen_family;
1208 struct nf_conntrack_zone zone;
1211 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1212 struct netlink_dump_control c = {
1213 .dump = ctnetlink_dump_table,
1214 .done = ctnetlink_done,
1217 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1218 struct ctnetlink_filter *filter;
1220 filter = ctnetlink_alloc_filter(cda);
1222 return PTR_ERR(filter);
1226 return netlink_dump_start(ctnl, skb, nlh, &c);
1229 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1233 if (cda[CTA_TUPLE_ORIG])
1234 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1236 else if (cda[CTA_TUPLE_REPLY])
1237 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1245 h = nf_conntrack_find_get(net, &zone, &tuple);
1249 ct = nf_ct_tuplehash_to_ctrack(h);
1252 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1259 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1260 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1266 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1275 /* this avoids a loop in nfnetlink. */
1276 return err == -EAGAIN ? -ENOBUFS : err;
1279 static int ctnetlink_done_list(struct netlink_callback *cb)
1282 nf_ct_put((struct nf_conn *)cb->args[1]);
1287 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1289 struct nf_conn *ct, *last;
1290 struct nf_conntrack_tuple_hash *h;
1291 struct hlist_nulls_node *n;
1292 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1293 u_int8_t l3proto = nfmsg->nfgen_family;
1296 struct hlist_nulls_head *list;
1297 struct net *net = sock_net(skb->sk);
1302 last = (struct nf_conn *)cb->args[1];
1304 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1305 struct ct_pcpu *pcpu;
1307 if (!cpu_possible(cpu))
1310 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1311 spin_lock_bh(&pcpu->lock);
1312 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1314 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1315 ct = nf_ct_tuplehash_to_ctrack(h);
1316 if (l3proto && nf_ct_l3num(ct) != l3proto)
1324 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1326 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1330 if (!atomic_inc_not_zero(&ct->ct_general.use))
1333 cb->args[1] = (unsigned long)ct;
1334 spin_unlock_bh(&pcpu->lock);
1342 spin_unlock_bh(&pcpu->lock);
1353 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1355 return ctnetlink_dump_list(skb, cb, true);
1358 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1359 struct sk_buff *skb,
1360 const struct nlmsghdr *nlh,
1361 const struct nlattr * const cda[],
1362 struct netlink_ext_ack *extack)
1364 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1365 struct netlink_dump_control c = {
1366 .dump = ctnetlink_dump_dying,
1367 .done = ctnetlink_done_list,
1369 return netlink_dump_start(ctnl, skb, nlh, &c);
1376 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1378 return ctnetlink_dump_list(skb, cb, false);
1381 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1382 struct sk_buff *skb,
1383 const struct nlmsghdr *nlh,
1384 const struct nlattr * const cda[],
1385 struct netlink_ext_ack *extack)
1387 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1388 struct netlink_dump_control c = {
1389 .dump = ctnetlink_dump_unconfirmed,
1390 .done = ctnetlink_done_list,
1392 return netlink_dump_start(ctnl, skb, nlh, &c);
1398 #ifdef CONFIG_NF_NAT_NEEDED
1400 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1401 enum nf_nat_manip_type manip,
1402 const struct nlattr *attr)
1404 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1407 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1408 if (!parse_nat_setup) {
1409 #ifdef CONFIG_MODULES
1411 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1412 if (request_module("nf-nat") < 0) {
1413 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1417 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1419 if (nfnetlink_parse_nat_setup_hook)
1425 err = parse_nat_setup(ct, manip, attr);
1426 if (err == -EAGAIN) {
1427 #ifdef CONFIG_MODULES
1429 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1430 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1431 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1435 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1446 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1451 /* Ignore these unchangable bits */
1452 on &= ~IPS_UNCHANGEABLE_MASK;
1453 off &= ~IPS_UNCHANGEABLE_MASK;
1455 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1456 if (on & (1 << bit))
1457 set_bit(bit, &ct->status);
1458 else if (off & (1 << bit))
1459 clear_bit(bit, &ct->status);
1464 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1467 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1468 d = ct->status ^ status;
1470 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1474 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1475 /* SEEN_REPLY bit can only be set */
1478 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1479 /* ASSURED bit can only be set */
1482 __ctnetlink_change_status(ct, status, 0);
1487 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1489 #ifdef CONFIG_NF_NAT_NEEDED
1492 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1495 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1500 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1504 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1510 static int ctnetlink_change_helper(struct nf_conn *ct,
1511 const struct nlattr * const cda[])
1513 struct nf_conntrack_helper *helper;
1514 struct nf_conn_help *help = nfct_help(ct);
1515 char *helpname = NULL;
1516 struct nlattr *helpinfo = NULL;
1519 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1523 /* don't change helper of sibling connections */
1525 /* If we try to change the helper to the same thing twice,
1526 * treat the second attempt as a no-op instead of returning
1532 helper = rcu_dereference(help->helper);
1533 if (helper && !strcmp(helper->name, helpname))
1541 if (!strcmp(helpname, "")) {
1542 if (help && help->helper) {
1543 /* we had a helper before ... */
1544 nf_ct_remove_expectations(ct);
1545 RCU_INIT_POINTER(help->helper, NULL);
1552 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1553 nf_ct_protonum(ct));
1554 if (helper == NULL) {
1560 if (help->helper == helper) {
1561 /* update private helper data if allowed. */
1562 if (helper->from_nlattr)
1563 helper->from_nlattr(helpinfo, ct);
1568 /* we cannot set a helper for an existing conntrack */
1576 static int ctnetlink_change_timeout(struct nf_conn *ct,
1577 const struct nlattr * const cda[])
1579 u64 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1581 if (timeout > INT_MAX)
1583 ct->timeout = nfct_time_stamp + (u32)timeout;
1585 if (test_bit(IPS_DYING_BIT, &ct->status))
1591 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1592 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1593 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1594 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1597 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1598 const struct nlattr * const cda[])
1600 const struct nlattr *attr = cda[CTA_PROTOINFO];
1601 const struct nf_conntrack_l4proto *l4proto;
1602 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1605 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1611 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1612 if (l4proto->from_nlattr)
1613 err = l4proto->from_nlattr(tb, ct);
1619 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1620 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1621 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1622 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1625 static int change_seq_adj(struct nf_ct_seqadj *seq,
1626 const struct nlattr * const attr)
1629 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1631 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1635 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1638 seq->correction_pos =
1639 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1641 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1644 seq->offset_before =
1645 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1647 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1651 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1657 ctnetlink_change_seq_adj(struct nf_conn *ct,
1658 const struct nlattr * const cda[])
1660 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1666 spin_lock_bh(&ct->lock);
1667 if (cda[CTA_SEQ_ADJ_ORIG]) {
1668 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1669 cda[CTA_SEQ_ADJ_ORIG]);
1673 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1676 if (cda[CTA_SEQ_ADJ_REPLY]) {
1677 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1678 cda[CTA_SEQ_ADJ_REPLY]);
1682 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1685 spin_unlock_bh(&ct->lock);
1688 spin_unlock_bh(&ct->lock);
1693 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1695 #ifdef CONFIG_NF_CONNTRACK_LABELS
1696 size_t len = nla_len(cda[CTA_LABELS]);
1697 const void *mask = cda[CTA_LABELS_MASK];
1699 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1703 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1704 nla_len(cda[CTA_LABELS_MASK]) != len)
1706 mask = nla_data(cda[CTA_LABELS_MASK]);
1711 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1718 ctnetlink_change_conntrack(struct nf_conn *ct,
1719 const struct nlattr * const cda[])
1723 /* only allow NAT changes and master assignation for new conntracks */
1724 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1727 if (cda[CTA_HELP]) {
1728 err = ctnetlink_change_helper(ct, cda);
1733 if (cda[CTA_TIMEOUT]) {
1734 err = ctnetlink_change_timeout(ct, cda);
1739 if (cda[CTA_STATUS]) {
1740 err = ctnetlink_change_status(ct, cda);
1745 if (cda[CTA_PROTOINFO]) {
1746 err = ctnetlink_change_protoinfo(ct, cda);
1751 #if defined(CONFIG_NF_CONNTRACK_MARK)
1753 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1756 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1757 err = ctnetlink_change_seq_adj(ct, cda);
1762 if (cda[CTA_LABELS]) {
1763 err = ctnetlink_attach_labels(ct, cda);
1771 static struct nf_conn *
1772 ctnetlink_create_conntrack(struct net *net,
1773 const struct nf_conntrack_zone *zone,
1774 const struct nlattr * const cda[],
1775 struct nf_conntrack_tuple *otuple,
1776 struct nf_conntrack_tuple *rtuple,
1781 struct nf_conntrack_helper *helper;
1782 struct nf_conn_tstamp *tstamp;
1785 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1787 return ERR_PTR(-ENOMEM);
1789 if (!cda[CTA_TIMEOUT])
1792 timeout = (u64)ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1793 if (timeout > INT_MAX)
1795 ct->timeout = (u32)timeout + nfct_time_stamp;
1798 if (cda[CTA_HELP]) {
1799 char *helpname = NULL;
1800 struct nlattr *helpinfo = NULL;
1802 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1806 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1807 nf_ct_protonum(ct));
1808 if (helper == NULL) {
1810 #ifdef CONFIG_MODULES
1811 if (request_module("nfct-helper-%s", helpname) < 0) {
1817 helper = __nf_conntrack_helper_find(helpname,
1819 nf_ct_protonum(ct));
1829 struct nf_conn_help *help;
1831 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1836 /* set private helper data if allowed. */
1837 if (helper->from_nlattr)
1838 helper->from_nlattr(helpinfo, ct);
1840 /* not in hash table yet so not strictly necessary */
1841 RCU_INIT_POINTER(help->helper, helper);
1844 /* try an implicit helper assignation */
1845 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1850 err = ctnetlink_setup_nat(ct, cda);
1854 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1855 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1856 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1857 nf_ct_labels_ext_add(ct);
1858 nfct_seqadj_ext_add(ct);
1859 nfct_synproxy_ext_add(ct);
1861 /* we must add conntrack extensions before confirmation. */
1862 ct->status |= IPS_CONFIRMED;
1864 if (cda[CTA_STATUS]) {
1865 err = ctnetlink_change_status(ct, cda);
1870 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1871 err = ctnetlink_change_seq_adj(ct, cda);
1876 memset(&ct->proto, 0, sizeof(ct->proto));
1877 if (cda[CTA_PROTOINFO]) {
1878 err = ctnetlink_change_protoinfo(ct, cda);
1883 #if defined(CONFIG_NF_CONNTRACK_MARK)
1885 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1888 /* setup master conntrack: this is a confirmed expectation */
1889 if (cda[CTA_TUPLE_MASTER]) {
1890 struct nf_conntrack_tuple master;
1891 struct nf_conntrack_tuple_hash *master_h;
1892 struct nf_conn *master_ct;
1894 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1899 master_h = nf_conntrack_find_get(net, zone, &master);
1900 if (master_h == NULL) {
1904 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1905 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1906 ct->master = master_ct;
1908 tstamp = nf_conn_tstamp_find(ct);
1910 tstamp->start = ktime_get_real_ns();
1912 err = nf_conntrack_hash_check_insert(ct);
1923 nf_conntrack_free(ct);
1924 return ERR_PTR(err);
1927 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1928 struct sk_buff *skb,
1929 const struct nlmsghdr *nlh,
1930 const struct nlattr * const cda[],
1931 struct netlink_ext_ack *extack)
1933 struct nf_conntrack_tuple otuple, rtuple;
1934 struct nf_conntrack_tuple_hash *h = NULL;
1935 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1937 u_int8_t u3 = nfmsg->nfgen_family;
1938 struct nf_conntrack_zone zone;
1941 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1945 if (cda[CTA_TUPLE_ORIG]) {
1946 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1952 if (cda[CTA_TUPLE_REPLY]) {
1953 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1959 if (cda[CTA_TUPLE_ORIG])
1960 h = nf_conntrack_find_get(net, &zone, &otuple);
1961 else if (cda[CTA_TUPLE_REPLY])
1962 h = nf_conntrack_find_get(net, &zone, &rtuple);
1966 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1967 enum ip_conntrack_events events;
1969 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1971 if (otuple.dst.protonum != rtuple.dst.protonum)
1974 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1980 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1981 events = 1 << IPCT_RELATED;
1983 events = 1 << IPCT_NEW;
1985 if (cda[CTA_LABELS] &&
1986 ctnetlink_attach_labels(ct, cda) == 0)
1987 events |= (1 << IPCT_LABEL);
1989 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1990 (1 << IPCT_ASSURED) |
1991 (1 << IPCT_HELPER) |
1992 (1 << IPCT_PROTOINFO) |
1993 (1 << IPCT_SEQADJ) |
1994 (1 << IPCT_MARK) | events,
1995 ct, NETLINK_CB(skb).portid,
2002 /* implicit 'else' */
2005 ct = nf_ct_tuplehash_to_ctrack(h);
2006 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
2007 err = ctnetlink_change_conntrack(ct, cda);
2009 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
2010 (1 << IPCT_ASSURED) |
2011 (1 << IPCT_HELPER) |
2013 (1 << IPCT_PROTOINFO) |
2014 (1 << IPCT_SEQADJ) |
2016 ct, NETLINK_CB(skb).portid,
2026 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2027 __u16 cpu, const struct ip_conntrack_stat *st)
2029 struct nlmsghdr *nlh;
2030 struct nfgenmsg *nfmsg;
2031 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2033 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2034 IPCTNL_MSG_CT_GET_STATS_CPU);
2035 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2039 nfmsg = nlmsg_data(nlh);
2040 nfmsg->nfgen_family = AF_UNSPEC;
2041 nfmsg->version = NFNETLINK_V0;
2042 nfmsg->res_id = htons(cpu);
2044 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2045 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2046 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2047 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2048 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2049 htonl(st->insert_failed)) ||
2050 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2051 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2052 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2053 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2054 htonl(st->search_restart)))
2055 goto nla_put_failure;
2057 nlmsg_end(skb, nlh);
2062 nlmsg_cancel(skb, nlh);
2067 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2070 struct net *net = sock_net(skb->sk);
2072 if (cb->args[0] == nr_cpu_ids)
2075 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2076 const struct ip_conntrack_stat *st;
2078 if (!cpu_possible(cpu))
2081 st = per_cpu_ptr(net->ct.stat, cpu);
2082 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2083 NETLINK_CB(cb->skb).portid,
2093 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2094 struct sk_buff *skb,
2095 const struct nlmsghdr *nlh,
2096 const struct nlattr * const cda[],
2097 struct netlink_ext_ack *extack)
2099 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2100 struct netlink_dump_control c = {
2101 .dump = ctnetlink_ct_stat_cpu_dump,
2103 return netlink_dump_start(ctnl, skb, nlh, &c);
2110 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2113 struct nlmsghdr *nlh;
2114 struct nfgenmsg *nfmsg;
2115 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2116 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2118 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2119 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2123 nfmsg = nlmsg_data(nlh);
2124 nfmsg->nfgen_family = AF_UNSPEC;
2125 nfmsg->version = NFNETLINK_V0;
2128 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2129 goto nla_put_failure;
2131 nlmsg_end(skb, nlh);
2136 nlmsg_cancel(skb, nlh);
2140 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2141 struct sk_buff *skb, const struct nlmsghdr *nlh,
2142 const struct nlattr * const cda[],
2143 struct netlink_ext_ack *extack)
2145 struct sk_buff *skb2;
2148 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2152 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2154 NFNL_MSG_TYPE(nlh->nlmsg_type),
2159 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2168 /* this avoids a loop in nfnetlink. */
2169 return err == -EAGAIN ? -ENOBUFS : err;
2172 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2173 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2174 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2175 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2176 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2177 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2178 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2179 .len = NF_CT_HELPER_NAME_LEN - 1 },
2180 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2181 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2182 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2183 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2184 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2187 static struct nf_conntrack_expect *
2188 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2189 struct nf_conntrack_helper *helper,
2190 struct nf_conntrack_tuple *tuple,
2191 struct nf_conntrack_tuple *mask);
2193 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2195 ctnetlink_glue_build_size(const struct nf_conn *ct)
2197 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2198 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2199 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2200 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2201 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2202 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2203 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2204 + nla_total_size(0) /* CTA_PROTOINFO */
2205 + nla_total_size(0) /* CTA_HELP */
2206 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2207 + ctnetlink_secctx_size(ct)
2208 #ifdef CONFIG_NF_NAT_NEEDED
2209 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2210 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2212 #ifdef CONFIG_NF_CONNTRACK_MARK
2213 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2215 #ifdef CONFIG_NF_CONNTRACK_ZONES
2216 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2218 + ctnetlink_proto_size(ct)
2222 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2223 enum ip_conntrack_info *ctinfo)
2225 return nf_ct_get(skb, ctinfo);
2228 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2230 const struct nf_conntrack_zone *zone;
2231 struct nlattr *nest_parms;
2233 zone = nf_ct_zone(ct);
2235 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2237 goto nla_put_failure;
2238 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2239 goto nla_put_failure;
2240 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2241 NF_CT_ZONE_DIR_ORIG) < 0)
2242 goto nla_put_failure;
2243 nla_nest_end(skb, nest_parms);
2245 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2247 goto nla_put_failure;
2248 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2249 goto nla_put_failure;
2250 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2251 NF_CT_ZONE_DIR_REPL) < 0)
2252 goto nla_put_failure;
2253 nla_nest_end(skb, nest_parms);
2255 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2256 NF_CT_DEFAULT_ZONE_DIR) < 0)
2257 goto nla_put_failure;
2259 if (ctnetlink_dump_id(skb, ct) < 0)
2260 goto nla_put_failure;
2262 if (ctnetlink_dump_status(skb, ct) < 0)
2263 goto nla_put_failure;
2265 if (ctnetlink_dump_timeout(skb, ct) < 0)
2266 goto nla_put_failure;
2268 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2269 goto nla_put_failure;
2271 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2272 goto nla_put_failure;
2274 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2275 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2276 goto nla_put_failure;
2278 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2279 goto nla_put_failure;
2281 if ((ct->status & IPS_SEQ_ADJUST) &&
2282 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2283 goto nla_put_failure;
2285 #ifdef CONFIG_NF_CONNTRACK_MARK
2286 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2287 goto nla_put_failure;
2289 if (ctnetlink_dump_labels(skb, ct) < 0)
2290 goto nla_put_failure;
2298 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2299 enum ip_conntrack_info ctinfo,
2300 u_int16_t ct_attr, u_int16_t ct_info_attr)
2302 struct nlattr *nest_parms;
2304 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2306 goto nla_put_failure;
2308 if (__ctnetlink_glue_build(skb, ct) < 0)
2309 goto nla_put_failure;
2311 nla_nest_end(skb, nest_parms);
2313 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2314 goto nla_put_failure;
2323 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2325 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2326 unsigned long d = ct->status ^ status;
2328 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2329 /* SEEN_REPLY bit can only be set */
2332 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2333 /* ASSURED bit can only be set */
2336 /* This check is less strict than ctnetlink_change_status()
2337 * because callers often flip IPS_EXPECTED bits when sending
2338 * an NFQA_CT attribute to the kernel. So ignore the
2339 * unchangeable bits but do not error out. Also user programs
2340 * are allowed to clear the bits that they are allowed to change.
2342 __ctnetlink_change_status(ct, status, ~status);
2347 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2351 if (cda[CTA_TIMEOUT]) {
2352 err = ctnetlink_change_timeout(ct, cda);
2356 if (cda[CTA_STATUS]) {
2357 err = ctnetlink_update_status(ct, cda);
2361 if (cda[CTA_HELP]) {
2362 err = ctnetlink_change_helper(ct, cda);
2366 if (cda[CTA_LABELS]) {
2367 err = ctnetlink_attach_labels(ct, cda);
2371 #if defined(CONFIG_NF_CONNTRACK_MARK)
2372 if (cda[CTA_MARK]) {
2373 u32 mask = 0, mark, newmark;
2374 if (cda[CTA_MARK_MASK])
2375 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2377 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2378 newmark = (ct->mark & mask) ^ mark;
2379 if (newmark != ct->mark)
2387 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2389 struct nlattr *cda[CTA_MAX+1];
2392 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2396 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2399 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2400 const struct nf_conn *ct,
2401 struct nf_conntrack_tuple *tuple,
2402 struct nf_conntrack_tuple *mask)
2406 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2407 nf_ct_l3num(ct), NULL);
2411 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2412 nf_ct_l3num(ct), NULL);
2416 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2417 u32 portid, u32 report)
2419 struct nlattr *cda[CTA_EXPECT_MAX+1];
2420 struct nf_conntrack_tuple tuple, mask;
2421 struct nf_conntrack_helper *helper = NULL;
2422 struct nf_conntrack_expect *exp;
2425 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2430 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2435 if (cda[CTA_EXPECT_HELP_NAME]) {
2436 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2438 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2439 nf_ct_protonum(ct));
2444 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2445 helper, &tuple, &mask);
2447 return PTR_ERR(exp);
2449 err = nf_ct_expect_related_report(exp, portid, report);
2450 nf_ct_expect_put(exp);
2454 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2455 enum ip_conntrack_info ctinfo, int diff)
2457 if (!(ct->status & IPS_NAT_MASK))
2460 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2463 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2464 .get_ct = ctnetlink_glue_get_ct,
2465 .build_size = ctnetlink_glue_build_size,
2466 .build = ctnetlink_glue_build,
2467 .parse = ctnetlink_glue_parse,
2468 .attach_expect = ctnetlink_glue_attach_expect,
2469 .seq_adjust = ctnetlink_glue_seqadj,
2471 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2473 /***********************************************************************
2475 ***********************************************************************/
2477 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2478 const struct nf_conntrack_tuple *tuple,
2481 struct nlattr *nest_parms;
2483 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2485 goto nla_put_failure;
2486 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2487 goto nla_put_failure;
2488 nla_nest_end(skb, nest_parms);
2496 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2497 const struct nf_conntrack_tuple *tuple,
2498 const struct nf_conntrack_tuple_mask *mask)
2500 const struct nf_conntrack_l3proto *l3proto;
2501 const struct nf_conntrack_l4proto *l4proto;
2502 struct nf_conntrack_tuple m;
2503 struct nlattr *nest_parms;
2506 memset(&m, 0xFF, sizeof(m));
2507 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2508 m.src.u.all = mask->src.u.all;
2509 m.dst.protonum = tuple->dst.protonum;
2511 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2513 goto nla_put_failure;
2516 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2517 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2519 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2520 tuple->dst.protonum);
2521 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2525 if (unlikely(ret < 0))
2526 goto nla_put_failure;
2528 nla_nest_end(skb, nest_parms);
2536 static const union nf_inet_addr any_addr;
2539 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2540 const struct nf_conntrack_expect *exp)
2542 struct nf_conn *master = exp->master;
2543 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2544 struct nf_conn_help *help;
2545 #ifdef CONFIG_NF_NAT_NEEDED
2546 struct nlattr *nest_parms;
2547 struct nf_conntrack_tuple nat_tuple = {};
2549 struct nf_ct_helper_expectfn *expfn;
2554 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2555 goto nla_put_failure;
2556 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2557 goto nla_put_failure;
2558 if (ctnetlink_exp_dump_tuple(skb,
2559 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2560 CTA_EXPECT_MASTER) < 0)
2561 goto nla_put_failure;
2563 #ifdef CONFIG_NF_NAT_NEEDED
2564 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2565 exp->saved_proto.all) {
2566 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2568 goto nla_put_failure;
2570 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2571 goto nla_put_failure;
2573 nat_tuple.src.l3num = nf_ct_l3num(master);
2574 nat_tuple.src.u3 = exp->saved_addr;
2575 nat_tuple.dst.protonum = nf_ct_protonum(master);
2576 nat_tuple.src.u = exp->saved_proto;
2578 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2579 CTA_EXPECT_NAT_TUPLE) < 0)
2580 goto nla_put_failure;
2581 nla_nest_end(skb, nest_parms);
2584 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2585 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2586 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2587 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2588 goto nla_put_failure;
2589 help = nfct_help(master);
2591 struct nf_conntrack_helper *helper;
2593 helper = rcu_dereference(help->helper);
2595 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2596 goto nla_put_failure;
2598 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2599 if (expfn != NULL &&
2600 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2601 goto nla_put_failure;
2610 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2611 int event, const struct nf_conntrack_expect *exp)
2613 struct nlmsghdr *nlh;
2614 struct nfgenmsg *nfmsg;
2615 unsigned int flags = portid ? NLM_F_MULTI : 0;
2617 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2618 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2622 nfmsg = nlmsg_data(nlh);
2623 nfmsg->nfgen_family = exp->tuple.src.l3num;
2624 nfmsg->version = NFNETLINK_V0;
2627 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2628 goto nla_put_failure;
2630 nlmsg_end(skb, nlh);
2635 nlmsg_cancel(skb, nlh);
2639 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2641 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2643 struct nf_conntrack_expect *exp = item->exp;
2644 struct net *net = nf_ct_exp_net(exp);
2645 struct nlmsghdr *nlh;
2646 struct nfgenmsg *nfmsg;
2647 struct sk_buff *skb;
2648 unsigned int type, group;
2651 if (events & (1 << IPEXP_DESTROY)) {
2652 type = IPCTNL_MSG_EXP_DELETE;
2653 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2654 } else if (events & (1 << IPEXP_NEW)) {
2655 type = IPCTNL_MSG_EXP_NEW;
2656 flags = NLM_F_CREATE|NLM_F_EXCL;
2657 group = NFNLGRP_CONNTRACK_EXP_NEW;
2661 if (!item->report && !nfnetlink_has_listeners(net, group))
2664 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2668 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2669 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2673 nfmsg = nlmsg_data(nlh);
2674 nfmsg->nfgen_family = exp->tuple.src.l3num;
2675 nfmsg->version = NFNETLINK_V0;
2678 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2679 goto nla_put_failure;
2681 nlmsg_end(skb, nlh);
2682 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2686 nlmsg_cancel(skb, nlh);
2690 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2694 static int ctnetlink_exp_done(struct netlink_callback *cb)
2697 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2702 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2704 struct net *net = sock_net(skb->sk);
2705 struct nf_conntrack_expect *exp, *last;
2706 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2707 u_int8_t l3proto = nfmsg->nfgen_family;
2710 last = (struct nf_conntrack_expect *)cb->args[1];
2711 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2713 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2715 if (l3proto && exp->tuple.src.l3num != l3proto)
2718 if (!net_eq(nf_ct_net(exp->master), net))
2726 if (ctnetlink_exp_fill_info(skb,
2727 NETLINK_CB(cb->skb).portid,
2731 if (!refcount_inc_not_zero(&exp->use))
2733 cb->args[1] = (unsigned long)exp;
2745 nf_ct_expect_put(last);
2751 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2753 struct nf_conntrack_expect *exp, *last;
2754 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2755 struct nf_conn *ct = cb->data;
2756 struct nf_conn_help *help = nfct_help(ct);
2757 u_int8_t l3proto = nfmsg->nfgen_family;
2763 last = (struct nf_conntrack_expect *)cb->args[1];
2765 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2766 if (l3proto && exp->tuple.src.l3num != l3proto)
2773 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2777 if (!refcount_inc_not_zero(&exp->use))
2779 cb->args[1] = (unsigned long)exp;
2791 nf_ct_expect_put(last);
2796 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2797 struct sk_buff *skb,
2798 const struct nlmsghdr *nlh,
2799 const struct nlattr * const cda[],
2800 struct netlink_ext_ack *extack)
2803 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2804 u_int8_t u3 = nfmsg->nfgen_family;
2805 struct nf_conntrack_tuple tuple;
2806 struct nf_conntrack_tuple_hash *h;
2808 struct nf_conntrack_zone zone;
2809 struct netlink_dump_control c = {
2810 .dump = ctnetlink_exp_ct_dump_table,
2811 .done = ctnetlink_exp_done,
2814 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2819 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2823 h = nf_conntrack_find_get(net, &zone, &tuple);
2827 ct = nf_ct_tuplehash_to_ctrack(h);
2828 /* No expectation linked to this connection tracking. */
2829 if (!nfct_help(ct)) {
2836 err = netlink_dump_start(ctnl, skb, nlh, &c);
2842 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2843 struct sk_buff *skb, const struct nlmsghdr *nlh,
2844 const struct nlattr * const cda[],
2845 struct netlink_ext_ack *extack)
2847 struct nf_conntrack_tuple tuple;
2848 struct nf_conntrack_expect *exp;
2849 struct sk_buff *skb2;
2850 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2851 u_int8_t u3 = nfmsg->nfgen_family;
2852 struct nf_conntrack_zone zone;
2855 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2856 if (cda[CTA_EXPECT_MASTER])
2857 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
2860 struct netlink_dump_control c = {
2861 .dump = ctnetlink_exp_dump_table,
2862 .done = ctnetlink_exp_done,
2864 return netlink_dump_start(ctnl, skb, nlh, &c);
2868 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2872 if (cda[CTA_EXPECT_TUPLE])
2873 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2875 else if (cda[CTA_EXPECT_MASTER])
2876 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2884 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2888 if (cda[CTA_EXPECT_ID]) {
2889 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2890 if (ntohl(id) != (u32)(unsigned long)exp) {
2891 nf_ct_expect_put(exp);
2897 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2899 nf_ct_expect_put(exp);
2904 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2905 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2907 nf_ct_expect_put(exp);
2911 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2920 /* this avoids a loop in nfnetlink. */
2921 return err == -EAGAIN ? -ENOBUFS : err;
2924 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
2926 const struct nf_conn_help *m_help;
2927 const char *name = data;
2929 m_help = nfct_help(exp->master);
2931 return strcmp(m_help->helper->name, name) == 0;
2934 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
2939 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2940 struct sk_buff *skb, const struct nlmsghdr *nlh,
2941 const struct nlattr * const cda[],
2942 struct netlink_ext_ack *extack)
2944 struct nf_conntrack_expect *exp;
2945 struct nf_conntrack_tuple tuple;
2946 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2947 u_int8_t u3 = nfmsg->nfgen_family;
2948 struct nf_conntrack_zone zone;
2951 if (cda[CTA_EXPECT_TUPLE]) {
2952 /* delete a single expect by tuple */
2953 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2957 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2962 /* bump usage count to 2 */
2963 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2967 if (cda[CTA_EXPECT_ID]) {
2968 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2969 if (ntohl(id) != (u32)(unsigned long)exp) {
2970 nf_ct_expect_put(exp);
2975 /* after list removal, usage count == 1 */
2976 spin_lock_bh(&nf_conntrack_expect_lock);
2977 if (del_timer(&exp->timeout)) {
2978 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2980 nf_ct_expect_put(exp);
2982 spin_unlock_bh(&nf_conntrack_expect_lock);
2983 /* have to put what we 'get' above.
2984 * after this line usage count == 0 */
2985 nf_ct_expect_put(exp);
2986 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2987 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2989 nf_ct_expect_iterate_net(net, expect_iter_name, name,
2990 NETLINK_CB(skb).portid,
2993 /* This basically means we have to flush everything*/
2994 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
2995 NETLINK_CB(skb).portid,
3002 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3003 const struct nlattr * const cda[])
3005 if (cda[CTA_EXPECT_TIMEOUT]) {
3006 if (!del_timer(&x->timeout))
3009 x->timeout.expires = jiffies +
3010 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3011 add_timer(&x->timeout);
3016 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3017 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3018 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3022 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3023 struct nf_conntrack_expect *exp,
3026 #ifdef CONFIG_NF_NAT_NEEDED
3027 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3028 struct nf_conntrack_tuple nat_tuple = {};
3031 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3032 exp_nat_nla_policy, NULL);
3036 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3039 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3040 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3045 exp->saved_addr = nat_tuple.src.u3;
3046 exp->saved_proto = nat_tuple.src.u;
3047 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3055 static struct nf_conntrack_expect *
3056 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3057 struct nf_conntrack_helper *helper,
3058 struct nf_conntrack_tuple *tuple,
3059 struct nf_conntrack_tuple *mask)
3061 u_int32_t class = 0;
3062 struct nf_conntrack_expect *exp;
3063 struct nf_conn_help *help;
3066 help = nfct_help(ct);
3068 return ERR_PTR(-EOPNOTSUPP);
3070 if (cda[CTA_EXPECT_CLASS] && helper) {
3071 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3072 if (class > helper->expect_class_max)
3073 return ERR_PTR(-EINVAL);
3075 exp = nf_ct_expect_alloc(ct);
3077 return ERR_PTR(-ENOMEM);
3079 if (cda[CTA_EXPECT_FLAGS]) {
3080 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3081 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3085 if (cda[CTA_EXPECT_FN]) {
3086 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3087 struct nf_ct_helper_expectfn *expfn;
3089 expfn = nf_ct_helper_expectfn_find_by_name(name);
3090 if (expfn == NULL) {
3094 exp->expectfn = expfn->expectfn;
3096 exp->expectfn = NULL;
3100 exp->helper = helper;
3101 exp->tuple = *tuple;
3102 exp->mask.src.u3 = mask->src.u3;
3103 exp->mask.src.u.all = mask->src.u.all;
3105 if (cda[CTA_EXPECT_NAT]) {
3106 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3107 exp, nf_ct_l3num(ct));
3113 nf_ct_expect_put(exp);
3114 return ERR_PTR(err);
3118 ctnetlink_create_expect(struct net *net,
3119 const struct nf_conntrack_zone *zone,
3120 const struct nlattr * const cda[],
3121 u_int8_t u3, u32 portid, int report)
3123 struct nf_conntrack_tuple tuple, mask, master_tuple;
3124 struct nf_conntrack_tuple_hash *h = NULL;
3125 struct nf_conntrack_helper *helper = NULL;
3126 struct nf_conntrack_expect *exp;
3130 /* caller guarantees that those three CTA_EXPECT_* exist */
3131 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3135 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3139 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3144 /* Look for master conntrack of this expectation */
3145 h = nf_conntrack_find_get(net, zone, &master_tuple);
3148 ct = nf_ct_tuplehash_to_ctrack(h);
3151 if (cda[CTA_EXPECT_HELP_NAME]) {
3152 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3154 helper = __nf_conntrack_helper_find(helpname, u3,
3155 nf_ct_protonum(ct));
3156 if (helper == NULL) {
3158 #ifdef CONFIG_MODULES
3159 if (request_module("nfct-helper-%s", helpname) < 0) {
3164 helper = __nf_conntrack_helper_find(helpname, u3,
3165 nf_ct_protonum(ct));
3177 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3183 err = nf_ct_expect_related_report(exp, portid, report);
3184 nf_ct_expect_put(exp);
3192 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3193 struct sk_buff *skb, const struct nlmsghdr *nlh,
3194 const struct nlattr * const cda[],
3195 struct netlink_ext_ack *extack)
3197 struct nf_conntrack_tuple tuple;
3198 struct nf_conntrack_expect *exp;
3199 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3200 u_int8_t u3 = nfmsg->nfgen_family;
3201 struct nf_conntrack_zone zone;
3204 if (!cda[CTA_EXPECT_TUPLE]
3205 || !cda[CTA_EXPECT_MASK]
3206 || !cda[CTA_EXPECT_MASTER])
3209 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3213 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3218 spin_lock_bh(&nf_conntrack_expect_lock);
3219 exp = __nf_ct_expect_find(net, &zone, &tuple);
3221 spin_unlock_bh(&nf_conntrack_expect_lock);
3223 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3224 err = ctnetlink_create_expect(net, &zone, cda, u3,
3225 NETLINK_CB(skb).portid,
3232 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3233 err = ctnetlink_change_expect(exp, cda);
3234 spin_unlock_bh(&nf_conntrack_expect_lock);
3240 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3241 const struct ip_conntrack_stat *st)
3243 struct nlmsghdr *nlh;
3244 struct nfgenmsg *nfmsg;
3245 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3247 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3248 IPCTNL_MSG_EXP_GET_STATS_CPU);
3249 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3253 nfmsg = nlmsg_data(nlh);
3254 nfmsg->nfgen_family = AF_UNSPEC;
3255 nfmsg->version = NFNETLINK_V0;
3256 nfmsg->res_id = htons(cpu);
3258 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3259 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3260 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3261 goto nla_put_failure;
3263 nlmsg_end(skb, nlh);
3268 nlmsg_cancel(skb, nlh);
3273 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3276 struct net *net = sock_net(skb->sk);
3278 if (cb->args[0] == nr_cpu_ids)
3281 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3282 const struct ip_conntrack_stat *st;
3284 if (!cpu_possible(cpu))
3287 st = per_cpu_ptr(net->ct.stat, cpu);
3288 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3298 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3299 struct sk_buff *skb,
3300 const struct nlmsghdr *nlh,
3301 const struct nlattr * const cda[],
3302 struct netlink_ext_ack *extack)
3304 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3305 struct netlink_dump_control c = {
3306 .dump = ctnetlink_exp_stat_cpu_dump,
3308 return netlink_dump_start(ctnl, skb, nlh, &c);
3314 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3315 static struct nf_ct_event_notifier ctnl_notifier = {
3316 .fcn = ctnetlink_conntrack_event,
3319 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3320 .fcn = ctnetlink_expect_event,
3324 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3325 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3326 .attr_count = CTA_MAX,
3327 .policy = ct_nla_policy },
3328 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3329 .attr_count = CTA_MAX,
3330 .policy = ct_nla_policy },
3331 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3332 .attr_count = CTA_MAX,
3333 .policy = ct_nla_policy },
3334 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3335 .attr_count = CTA_MAX,
3336 .policy = ct_nla_policy },
3337 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3338 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3339 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3340 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3343 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3344 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3345 .attr_count = CTA_EXPECT_MAX,
3346 .policy = exp_nla_policy },
3347 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3348 .attr_count = CTA_EXPECT_MAX,
3349 .policy = exp_nla_policy },
3350 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3351 .attr_count = CTA_EXPECT_MAX,
3352 .policy = exp_nla_policy },
3353 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3356 static const struct nfnetlink_subsystem ctnl_subsys = {
3357 .name = "conntrack",
3358 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3359 .cb_count = IPCTNL_MSG_MAX,
3363 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3364 .name = "conntrack_expect",
3365 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3366 .cb_count = IPCTNL_MSG_EXP_MAX,
3370 MODULE_ALIAS("ip_conntrack_netlink");
3371 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3372 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3374 static int __net_init ctnetlink_net_init(struct net *net)
3376 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3379 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3381 pr_err("ctnetlink_init: cannot register notifier.\n");
3385 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3387 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3388 goto err_unreg_notifier;
3393 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3395 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3401 static void ctnetlink_net_exit(struct net *net)
3403 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3404 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3405 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3409 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3413 list_for_each_entry(net, net_exit_list, exit_list)
3414 ctnetlink_net_exit(net);
3417 static struct pernet_operations ctnetlink_net_ops = {
3418 .init = ctnetlink_net_init,
3419 .exit_batch = ctnetlink_net_exit_batch,
3422 static int __init ctnetlink_init(void)
3426 ret = nfnetlink_subsys_register(&ctnl_subsys);
3428 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3432 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3434 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3435 goto err_unreg_subsys;
3438 ret = register_pernet_subsys(&ctnetlink_net_ops);
3440 pr_err("ctnetlink_init: cannot register pernet operations\n");
3441 goto err_unreg_exp_subsys;
3443 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3444 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3445 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3449 err_unreg_exp_subsys:
3450 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3452 nfnetlink_subsys_unregister(&ctnl_subsys);
3457 static void __exit ctnetlink_exit(void)
3459 unregister_pernet_subsys(&ctnetlink_net_ops);
3460 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3461 nfnetlink_subsys_unregister(&ctnl_subsys);
3462 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3463 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3468 module_init(ctnetlink_init);
3469 module_exit(ctnetlink_exit);