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_seqadj.h>
49 #include <net/netfilter/nf_conntrack_synproxy.h>
50 #ifdef CONFIG_NF_NAT_NEEDED
51 #include <net/netfilter/nf_nat_core.h>
52 #include <net/netfilter/nf_nat_l4proto.h>
53 #include <net/netfilter/nf_nat_helper.h>
56 #include <linux/netfilter/nfnetlink.h>
57 #include <linux/netfilter/nfnetlink_conntrack.h>
59 MODULE_LICENSE("GPL");
61 static char __initdata version[] = "0.93";
63 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
64 const struct nf_conntrack_tuple *tuple,
65 const struct nf_conntrack_l4proto *l4proto)
68 struct nlattr *nest_parms;
70 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
73 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
76 if (likely(l4proto->tuple_to_nlattr))
77 ret = l4proto->tuple_to_nlattr(skb, tuple);
79 nla_nest_end(skb, nest_parms);
87 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
88 const struct nf_conntrack_tuple *tuple,
89 const struct nf_conntrack_l3proto *l3proto)
92 struct nlattr *nest_parms;
94 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
98 if (likely(l3proto->tuple_to_nlattr))
99 ret = l3proto->tuple_to_nlattr(skb, tuple);
101 nla_nest_end(skb, nest_parms);
109 static int ctnetlink_dump_tuples(struct sk_buff *skb,
110 const struct nf_conntrack_tuple *tuple)
112 const struct nf_conntrack_l3proto *l3proto;
113 const struct nf_conntrack_l4proto *l4proto;
117 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
118 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
121 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
122 tuple->dst.protonum);
123 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
129 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
130 const struct nf_conntrack_zone *zone, int dir)
132 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
134 if (nla_put_be16(skb, attrtype, htons(zone->id)))
135 goto nla_put_failure;
142 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
144 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
145 goto nla_put_failure;
152 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
154 long timeout = nf_ct_expires(ct) / HZ;
156 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
157 goto nla_put_failure;
164 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
166 const struct nf_conntrack_l4proto *l4proto;
167 struct nlattr *nest_proto;
170 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
171 if (!l4proto->to_nlattr)
174 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
176 goto nla_put_failure;
178 ret = l4proto->to_nlattr(skb, nest_proto, ct);
180 nla_nest_end(skb, nest_proto);
188 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
189 const struct nf_conn *ct)
191 struct nlattr *nest_helper;
192 const struct nf_conn_help *help = nfct_help(ct);
193 struct nf_conntrack_helper *helper;
198 helper = rcu_dereference(help->helper);
202 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
204 goto nla_put_failure;
205 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
206 goto nla_put_failure;
208 if (helper->to_nlattr)
209 helper->to_nlattr(skb, ct);
211 nla_nest_end(skb, nest_helper);
220 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
221 enum ip_conntrack_dir dir, int type)
223 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
224 struct nf_conn_counter *counter = acct->counter;
225 struct nlattr *nest_count;
228 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
229 pkts = atomic64_xchg(&counter[dir].packets, 0);
230 bytes = atomic64_xchg(&counter[dir].bytes, 0);
232 pkts = atomic64_read(&counter[dir].packets);
233 bytes = atomic64_read(&counter[dir].bytes);
236 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
238 goto nla_put_failure;
240 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
242 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
244 goto nla_put_failure;
246 nla_nest_end(skb, nest_count);
255 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
257 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
262 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
264 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
271 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
273 struct nlattr *nest_count;
274 const struct nf_conn_tstamp *tstamp;
276 tstamp = nf_conn_tstamp_find(ct);
280 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
282 goto nla_put_failure;
284 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
285 CTA_TIMESTAMP_PAD) ||
286 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
287 cpu_to_be64(tstamp->stop),
289 goto nla_put_failure;
290 nla_nest_end(skb, nest_count);
298 #ifdef CONFIG_NF_CONNTRACK_MARK
299 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
301 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
302 goto nla_put_failure;
309 #define ctnetlink_dump_mark(a, b) (0)
312 #ifdef CONFIG_NF_CONNTRACK_SECMARK
313 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
315 struct nlattr *nest_secctx;
319 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
324 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
326 goto nla_put_failure;
328 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
329 goto nla_put_failure;
330 nla_nest_end(skb, nest_secctx);
334 security_release_secctx(secctx, len);
338 #define ctnetlink_dump_secctx(a, b) (0)
341 #ifdef CONFIG_NF_CONNTRACK_LABELS
342 static inline int ctnetlink_label_size(const struct nf_conn *ct)
344 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
348 return nla_total_size(sizeof(labels->bits));
352 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
354 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
362 if (labels->bits[i] != 0)
363 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
366 } while (i < ARRAY_SIZE(labels->bits));
371 #define ctnetlink_dump_labels(a, b) (0)
372 #define ctnetlink_label_size(a) (0)
375 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
377 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
379 struct nlattr *nest_parms;
381 if (!(ct->status & IPS_EXPECTED))
384 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
386 goto nla_put_failure;
387 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
388 goto nla_put_failure;
389 nla_nest_end(skb, nest_parms);
398 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
400 struct nlattr *nest_parms;
402 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
404 goto nla_put_failure;
406 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
407 htonl(seq->correction_pos)) ||
408 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
409 htonl(seq->offset_before)) ||
410 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
411 htonl(seq->offset_after)))
412 goto nla_put_failure;
414 nla_nest_end(skb, nest_parms);
422 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, struct nf_conn *ct)
424 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
425 struct nf_ct_seqadj *seq;
427 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
430 spin_lock_bh(&ct->lock);
431 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
432 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
435 seq = &seqadj->seq[IP_CT_DIR_REPLY];
436 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
439 spin_unlock_bh(&ct->lock);
442 spin_unlock_bh(&ct->lock);
446 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
448 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
449 goto nla_put_failure;
456 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
458 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
459 goto nla_put_failure;
467 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
470 const struct nf_conntrack_zone *zone;
471 struct nlmsghdr *nlh;
472 struct nfgenmsg *nfmsg;
473 struct nlattr *nest_parms;
474 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
476 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
477 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
481 nfmsg = nlmsg_data(nlh);
482 nfmsg->nfgen_family = nf_ct_l3num(ct);
483 nfmsg->version = NFNETLINK_V0;
486 zone = nf_ct_zone(ct);
488 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
490 goto nla_put_failure;
491 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
492 goto nla_put_failure;
493 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
494 NF_CT_ZONE_DIR_ORIG) < 0)
495 goto nla_put_failure;
496 nla_nest_end(skb, nest_parms);
498 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
500 goto nla_put_failure;
501 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
502 goto nla_put_failure;
503 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
504 NF_CT_ZONE_DIR_REPL) < 0)
505 goto nla_put_failure;
506 nla_nest_end(skb, nest_parms);
508 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
509 NF_CT_DEFAULT_ZONE_DIR) < 0)
510 goto nla_put_failure;
512 if (ctnetlink_dump_status(skb, ct) < 0 ||
513 ctnetlink_dump_timeout(skb, ct) < 0 ||
514 ctnetlink_dump_acct(skb, ct, type) < 0 ||
515 ctnetlink_dump_timestamp(skb, ct) < 0 ||
516 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
517 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
518 ctnetlink_dump_mark(skb, ct) < 0 ||
519 ctnetlink_dump_secctx(skb, ct) < 0 ||
520 ctnetlink_dump_labels(skb, ct) < 0 ||
521 ctnetlink_dump_id(skb, ct) < 0 ||
522 ctnetlink_dump_use(skb, ct) < 0 ||
523 ctnetlink_dump_master(skb, ct) < 0 ||
524 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
525 goto nla_put_failure;
532 nlmsg_cancel(skb, nlh);
536 static inline size_t ctnetlink_proto_size(const struct nf_conn *ct)
538 const struct nf_conntrack_l3proto *l3proto;
539 const struct nf_conntrack_l4proto *l4proto;
542 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
543 len = l3proto->nla_size;
544 len *= 3u; /* ORIG, REPLY, MASTER */
546 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
547 len += l4proto->nla_size;
552 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
554 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
556 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
557 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
558 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
562 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
564 #ifdef CONFIG_NF_CONNTRACK_SECMARK
567 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
571 return nla_total_size(0) /* CTA_SECCTX */
572 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
578 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
580 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
581 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
583 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
589 #ifdef CONFIG_NF_CONNTRACK_EVENTS
590 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
592 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
593 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
594 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
595 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
596 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
597 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
598 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
599 + ctnetlink_acct_size(ct)
600 + ctnetlink_timestamp_size(ct)
601 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
602 + nla_total_size(0) /* CTA_PROTOINFO */
603 + nla_total_size(0) /* CTA_HELP */
604 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
605 + ctnetlink_secctx_size(ct)
606 #ifdef CONFIG_NF_NAT_NEEDED
607 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
608 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
610 #ifdef CONFIG_NF_CONNTRACK_MARK
611 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
613 #ifdef CONFIG_NF_CONNTRACK_ZONES
614 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
616 + ctnetlink_proto_size(ct)
617 + ctnetlink_label_size(ct)
622 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
624 const struct nf_conntrack_zone *zone;
626 struct nlmsghdr *nlh;
627 struct nfgenmsg *nfmsg;
628 struct nlattr *nest_parms;
629 struct nf_conn *ct = item->ct;
632 unsigned int flags = 0, group;
635 if (events & (1 << IPCT_DESTROY)) {
636 type = IPCTNL_MSG_CT_DELETE;
637 group = NFNLGRP_CONNTRACK_DESTROY;
638 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
639 type = IPCTNL_MSG_CT_NEW;
640 flags = NLM_F_CREATE|NLM_F_EXCL;
641 group = NFNLGRP_CONNTRACK_NEW;
643 type = IPCTNL_MSG_CT_NEW;
644 group = NFNLGRP_CONNTRACK_UPDATE;
649 if (!item->report && !nfnetlink_has_listeners(net, group))
652 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
656 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
657 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
661 nfmsg = nlmsg_data(nlh);
662 nfmsg->nfgen_family = nf_ct_l3num(ct);
663 nfmsg->version = NFNETLINK_V0;
666 zone = nf_ct_zone(ct);
668 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
670 goto nla_put_failure;
671 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
672 goto nla_put_failure;
673 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
674 NF_CT_ZONE_DIR_ORIG) < 0)
675 goto nla_put_failure;
676 nla_nest_end(skb, nest_parms);
678 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
680 goto nla_put_failure;
681 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
682 goto nla_put_failure;
683 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
684 NF_CT_ZONE_DIR_REPL) < 0)
685 goto nla_put_failure;
686 nla_nest_end(skb, nest_parms);
688 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
689 NF_CT_DEFAULT_ZONE_DIR) < 0)
690 goto nla_put_failure;
692 if (ctnetlink_dump_id(skb, ct) < 0)
693 goto nla_put_failure;
695 if (ctnetlink_dump_status(skb, ct) < 0)
696 goto nla_put_failure;
698 if (events & (1 << IPCT_DESTROY)) {
699 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
700 ctnetlink_dump_timestamp(skb, ct) < 0)
701 goto nla_put_failure;
703 if (ctnetlink_dump_timeout(skb, ct) < 0)
704 goto nla_put_failure;
706 if (events & (1 << IPCT_PROTOINFO)
707 && ctnetlink_dump_protoinfo(skb, ct) < 0)
708 goto nla_put_failure;
710 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
711 && ctnetlink_dump_helpinfo(skb, ct) < 0)
712 goto nla_put_failure;
714 #ifdef CONFIG_NF_CONNTRACK_SECMARK
715 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
716 && ctnetlink_dump_secctx(skb, ct) < 0)
717 goto nla_put_failure;
719 if (events & (1 << IPCT_LABEL) &&
720 ctnetlink_dump_labels(skb, ct) < 0)
721 goto nla_put_failure;
723 if (events & (1 << IPCT_RELATED) &&
724 ctnetlink_dump_master(skb, ct) < 0)
725 goto nla_put_failure;
727 if (events & (1 << IPCT_SEQADJ) &&
728 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
729 goto nla_put_failure;
732 #ifdef CONFIG_NF_CONNTRACK_MARK
733 if ((events & (1 << IPCT_MARK) || ct->mark)
734 && ctnetlink_dump_mark(skb, ct) < 0)
735 goto nla_put_failure;
738 err = nfnetlink_send(skb, net, item->portid, group, item->report,
740 if (err == -ENOBUFS || err == -EAGAIN)
746 nlmsg_cancel(skb, nlh);
750 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
755 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
757 static int ctnetlink_done(struct netlink_callback *cb)
760 nf_ct_put((struct nf_conn *)cb->args[1]);
765 struct ctnetlink_filter {
772 static struct ctnetlink_filter *
773 ctnetlink_alloc_filter(const struct nlattr * const cda[])
775 #ifdef CONFIG_NF_CONNTRACK_MARK
776 struct ctnetlink_filter *filter;
778 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
780 return ERR_PTR(-ENOMEM);
782 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
783 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
787 return ERR_PTR(-EOPNOTSUPP);
791 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
793 struct ctnetlink_filter *filter = data;
798 #ifdef CONFIG_NF_CONNTRACK_MARK
799 if ((ct->mark & filter->mark.mask) == filter->mark.val)
807 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
809 struct net *net = sock_net(skb->sk);
810 struct nf_conn *ct, *last;
811 struct nf_conntrack_tuple_hash *h;
812 struct hlist_nulls_node *n;
813 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
814 u_int8_t l3proto = nfmsg->nfgen_family;
815 struct nf_conn *nf_ct_evict[8];
819 last = (struct nf_conn *)cb->args[1];
823 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
827 if (nf_ct_should_gc(nf_ct_evict[i]))
828 nf_ct_kill(nf_ct_evict[i]);
829 nf_ct_put(nf_ct_evict[i]);
832 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
833 nf_conntrack_lock(lockp);
834 if (cb->args[0] >= nf_conntrack_htable_size) {
838 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
840 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
842 ct = nf_ct_tuplehash_to_ctrack(h);
843 if (nf_ct_is_expired(ct)) {
844 if (i < ARRAY_SIZE(nf_ct_evict) &&
845 atomic_inc_not_zero(&ct->ct_general.use))
846 nf_ct_evict[i++] = ct;
850 if (!net_eq(net, nf_ct_net(ct)))
853 /* Dump entries of a given L3 protocol number.
854 * If it is not specified, ie. l3proto == 0,
855 * then dump everything. */
856 if (l3proto && nf_ct_l3num(ct) != l3proto)
863 if (!ctnetlink_filter_match(ct, cb->data))
868 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
870 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
874 nf_conntrack_get(&ct->ct_general);
875 cb->args[1] = (unsigned long)ct;
889 /* nf ct hash resize happened, now clear the leftover. */
890 if ((struct nf_conn *)cb->args[1] == last)
898 if (nf_ct_should_gc(nf_ct_evict[i]))
899 nf_ct_kill(nf_ct_evict[i]);
900 nf_ct_put(nf_ct_evict[i]);
906 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
907 struct nf_conntrack_tuple *tuple)
909 struct nlattr *tb[CTA_IP_MAX+1];
910 struct nf_conntrack_l3proto *l3proto;
913 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
918 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
920 if (likely(l3proto->nlattr_to_tuple)) {
921 ret = nla_validate_nested(attr, CTA_IP_MAX,
922 l3proto->nla_policy, NULL);
924 ret = l3proto->nlattr_to_tuple(tb, tuple);
932 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
933 [CTA_PROTO_NUM] = { .type = NLA_U8 },
936 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
937 struct nf_conntrack_tuple *tuple)
939 const struct nf_conntrack_l4proto *l4proto;
940 struct nlattr *tb[CTA_PROTO_MAX+1];
943 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
948 if (!tb[CTA_PROTO_NUM])
950 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
953 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
955 if (likely(l4proto->nlattr_to_tuple)) {
956 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
957 l4proto->nla_policy, NULL);
959 ret = l4proto->nlattr_to_tuple(tb, tuple);
968 ctnetlink_parse_zone(const struct nlattr *attr,
969 struct nf_conntrack_zone *zone)
971 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
972 NF_CT_DEFAULT_ZONE_DIR, 0);
973 #ifdef CONFIG_NF_CONNTRACK_ZONES
975 zone->id = ntohs(nla_get_be16(attr));
984 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
985 struct nf_conntrack_zone *zone)
989 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
992 ret = ctnetlink_parse_zone(attr, zone);
996 if (type == CTA_TUPLE_REPLY)
997 zone->dir = NF_CT_ZONE_DIR_REPL;
999 zone->dir = NF_CT_ZONE_DIR_ORIG;
1004 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1005 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1006 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1007 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1011 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1012 struct nf_conntrack_tuple *tuple, u32 type,
1013 u_int8_t l3num, struct nf_conntrack_zone *zone)
1015 struct nlattr *tb[CTA_TUPLE_MAX+1];
1018 memset(tuple, 0, sizeof(*tuple));
1020 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1025 if (!tb[CTA_TUPLE_IP])
1028 tuple->src.l3num = l3num;
1030 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1034 if (!tb[CTA_TUPLE_PROTO])
1037 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1041 if (tb[CTA_TUPLE_ZONE]) {
1045 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1051 /* orig and expect tuples get DIR_ORIGINAL */
1052 if (type == CTA_TUPLE_REPLY)
1053 tuple->dst.dir = IP_CT_DIR_REPLY;
1055 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1060 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1061 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1062 .len = NF_CT_HELPER_NAME_LEN - 1 },
1065 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1066 struct nlattr **helpinfo)
1069 struct nlattr *tb[CTA_HELP_MAX+1];
1071 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1075 if (!tb[CTA_HELP_NAME])
1078 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1080 if (tb[CTA_HELP_INFO])
1081 *helpinfo = tb[CTA_HELP_INFO];
1086 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1087 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1088 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1089 [CTA_STATUS] = { .type = NLA_U32 },
1090 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1091 [CTA_HELP] = { .type = NLA_NESTED },
1092 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1093 [CTA_TIMEOUT] = { .type = NLA_U32 },
1094 [CTA_MARK] = { .type = NLA_U32 },
1095 [CTA_ID] = { .type = NLA_U32 },
1096 [CTA_NAT_DST] = { .type = NLA_NESTED },
1097 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1098 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1099 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1100 [CTA_ZONE] = { .type = NLA_U16 },
1101 [CTA_MARK_MASK] = { .type = NLA_U32 },
1102 [CTA_LABELS] = { .type = NLA_BINARY,
1103 .len = NF_CT_LABELS_MAX_SIZE },
1104 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1105 .len = NF_CT_LABELS_MAX_SIZE },
1108 static int ctnetlink_flush_conntrack(struct net *net,
1109 const struct nlattr * const cda[],
1110 u32 portid, int report)
1112 struct ctnetlink_filter *filter = NULL;
1114 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1115 filter = ctnetlink_alloc_filter(cda);
1117 return PTR_ERR(filter);
1120 nf_ct_iterate_cleanup_net(net, ctnetlink_filter_match, filter,
1127 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1128 struct sk_buff *skb,
1129 const struct nlmsghdr *nlh,
1130 const struct nlattr * const cda[],
1131 struct netlink_ext_ack *extack)
1133 struct nf_conntrack_tuple_hash *h;
1134 struct nf_conntrack_tuple tuple;
1136 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1137 u_int8_t u3 = nfmsg->nfgen_family;
1138 struct nf_conntrack_zone zone;
1141 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1145 if (cda[CTA_TUPLE_ORIG])
1146 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1148 else if (cda[CTA_TUPLE_REPLY])
1149 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1152 return ctnetlink_flush_conntrack(net, cda,
1153 NETLINK_CB(skb).portid,
1160 h = nf_conntrack_find_get(net, &zone, &tuple);
1164 ct = nf_ct_tuplehash_to_ctrack(h);
1167 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1168 if (id != (u32)(unsigned long)ct) {
1174 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1180 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1181 struct sk_buff *skb,
1182 const struct nlmsghdr *nlh,
1183 const struct nlattr * const cda[],
1184 struct netlink_ext_ack *extack)
1186 struct nf_conntrack_tuple_hash *h;
1187 struct nf_conntrack_tuple tuple;
1189 struct sk_buff *skb2 = NULL;
1190 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1191 u_int8_t u3 = nfmsg->nfgen_family;
1192 struct nf_conntrack_zone zone;
1195 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1196 struct netlink_dump_control c = {
1197 .dump = ctnetlink_dump_table,
1198 .done = ctnetlink_done,
1201 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1202 struct ctnetlink_filter *filter;
1204 filter = ctnetlink_alloc_filter(cda);
1206 return PTR_ERR(filter);
1210 return netlink_dump_start(ctnl, skb, nlh, &c);
1213 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1217 if (cda[CTA_TUPLE_ORIG])
1218 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1220 else if (cda[CTA_TUPLE_REPLY])
1221 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1229 h = nf_conntrack_find_get(net, &zone, &tuple);
1233 ct = nf_ct_tuplehash_to_ctrack(h);
1236 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1243 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1244 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1250 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1259 /* this avoids a loop in nfnetlink. */
1260 return err == -EAGAIN ? -ENOBUFS : err;
1263 static int ctnetlink_done_list(struct netlink_callback *cb)
1266 nf_ct_put((struct nf_conn *)cb->args[1]);
1271 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1273 struct nf_conn *ct, *last;
1274 struct nf_conntrack_tuple_hash *h;
1275 struct hlist_nulls_node *n;
1276 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1277 u_int8_t l3proto = nfmsg->nfgen_family;
1280 struct hlist_nulls_head *list;
1281 struct net *net = sock_net(skb->sk);
1286 last = (struct nf_conn *)cb->args[1];
1288 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1289 struct ct_pcpu *pcpu;
1291 if (!cpu_possible(cpu))
1294 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1295 spin_lock_bh(&pcpu->lock);
1296 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1298 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1299 ct = nf_ct_tuplehash_to_ctrack(h);
1300 if (l3proto && nf_ct_l3num(ct) != l3proto)
1308 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1310 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1314 if (!atomic_inc_not_zero(&ct->ct_general.use))
1317 cb->args[1] = (unsigned long)ct;
1318 spin_unlock_bh(&pcpu->lock);
1326 spin_unlock_bh(&pcpu->lock);
1337 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1339 return ctnetlink_dump_list(skb, cb, true);
1342 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1343 struct sk_buff *skb,
1344 const struct nlmsghdr *nlh,
1345 const struct nlattr * const cda[],
1346 struct netlink_ext_ack *extack)
1348 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1349 struct netlink_dump_control c = {
1350 .dump = ctnetlink_dump_dying,
1351 .done = ctnetlink_done_list,
1353 return netlink_dump_start(ctnl, skb, nlh, &c);
1360 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1362 return ctnetlink_dump_list(skb, cb, false);
1365 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1366 struct sk_buff *skb,
1367 const struct nlmsghdr *nlh,
1368 const struct nlattr * const cda[],
1369 struct netlink_ext_ack *extack)
1371 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1372 struct netlink_dump_control c = {
1373 .dump = ctnetlink_dump_unconfirmed,
1374 .done = ctnetlink_done_list,
1376 return netlink_dump_start(ctnl, skb, nlh, &c);
1382 #ifdef CONFIG_NF_NAT_NEEDED
1384 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1385 enum nf_nat_manip_type manip,
1386 const struct nlattr *attr)
1388 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1391 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1392 if (!parse_nat_setup) {
1393 #ifdef CONFIG_MODULES
1395 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1396 if (request_module("nf-nat") < 0) {
1397 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1401 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1403 if (nfnetlink_parse_nat_setup_hook)
1409 err = parse_nat_setup(ct, manip, attr);
1410 if (err == -EAGAIN) {
1411 #ifdef CONFIG_MODULES
1413 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1414 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1415 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1419 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1430 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1435 /* Ignore these unchangable bits */
1436 on &= ~IPS_UNCHANGEABLE_MASK;
1437 off &= ~IPS_UNCHANGEABLE_MASK;
1439 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1440 if (on & (1 << bit))
1441 set_bit(bit, &ct->status);
1442 else if (off & (1 << bit))
1443 clear_bit(bit, &ct->status);
1448 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1451 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1452 d = ct->status ^ status;
1454 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1458 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1459 /* SEEN_REPLY bit can only be set */
1462 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1463 /* ASSURED bit can only be set */
1466 __ctnetlink_change_status(ct, status, 0);
1471 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1473 #ifdef CONFIG_NF_NAT_NEEDED
1476 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1479 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1484 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1488 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1494 static int ctnetlink_change_helper(struct nf_conn *ct,
1495 const struct nlattr * const cda[])
1497 struct nf_conntrack_helper *helper;
1498 struct nf_conn_help *help = nfct_help(ct);
1499 char *helpname = NULL;
1500 struct nlattr *helpinfo = NULL;
1503 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1507 /* don't change helper of sibling connections */
1509 /* If we try to change the helper to the same thing twice,
1510 * treat the second attempt as a no-op instead of returning
1516 helper = rcu_dereference(help->helper);
1517 if (helper && !strcmp(helper->name, helpname))
1525 if (!strcmp(helpname, "")) {
1526 if (help && help->helper) {
1527 /* we had a helper before ... */
1528 nf_ct_remove_expectations(ct);
1529 RCU_INIT_POINTER(help->helper, NULL);
1536 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1537 nf_ct_protonum(ct));
1538 if (helper == NULL) {
1544 if (help->helper == helper) {
1545 /* update private helper data if allowed. */
1546 if (helper->from_nlattr)
1547 helper->from_nlattr(helpinfo, ct);
1552 /* we cannot set a helper for an existing conntrack */
1560 static int ctnetlink_change_timeout(struct nf_conn *ct,
1561 const struct nlattr * const cda[])
1563 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1565 ct->timeout = nfct_time_stamp + timeout * HZ;
1567 if (test_bit(IPS_DYING_BIT, &ct->status))
1573 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1574 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1575 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1576 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1579 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1580 const struct nlattr * const cda[])
1582 const struct nlattr *attr = cda[CTA_PROTOINFO];
1583 const struct nf_conntrack_l4proto *l4proto;
1584 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1587 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1593 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1594 if (l4proto->from_nlattr)
1595 err = l4proto->from_nlattr(tb, ct);
1601 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1602 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1603 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1604 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1607 static int change_seq_adj(struct nf_ct_seqadj *seq,
1608 const struct nlattr * const attr)
1611 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1613 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1617 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1620 seq->correction_pos =
1621 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1623 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1626 seq->offset_before =
1627 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1629 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1633 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1639 ctnetlink_change_seq_adj(struct nf_conn *ct,
1640 const struct nlattr * const cda[])
1642 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1648 spin_lock_bh(&ct->lock);
1649 if (cda[CTA_SEQ_ADJ_ORIG]) {
1650 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1651 cda[CTA_SEQ_ADJ_ORIG]);
1655 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1658 if (cda[CTA_SEQ_ADJ_REPLY]) {
1659 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1660 cda[CTA_SEQ_ADJ_REPLY]);
1664 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1667 spin_unlock_bh(&ct->lock);
1670 spin_unlock_bh(&ct->lock);
1675 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1677 #ifdef CONFIG_NF_CONNTRACK_LABELS
1678 size_t len = nla_len(cda[CTA_LABELS]);
1679 const void *mask = cda[CTA_LABELS_MASK];
1681 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1685 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1686 nla_len(cda[CTA_LABELS_MASK]) != len)
1688 mask = nla_data(cda[CTA_LABELS_MASK]);
1693 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1700 ctnetlink_change_conntrack(struct nf_conn *ct,
1701 const struct nlattr * const cda[])
1705 /* only allow NAT changes and master assignation for new conntracks */
1706 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1709 if (cda[CTA_HELP]) {
1710 err = ctnetlink_change_helper(ct, cda);
1715 if (cda[CTA_TIMEOUT]) {
1716 err = ctnetlink_change_timeout(ct, cda);
1721 if (cda[CTA_STATUS]) {
1722 err = ctnetlink_change_status(ct, cda);
1727 if (cda[CTA_PROTOINFO]) {
1728 err = ctnetlink_change_protoinfo(ct, cda);
1733 #if defined(CONFIG_NF_CONNTRACK_MARK)
1735 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1738 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1739 err = ctnetlink_change_seq_adj(ct, cda);
1744 if (cda[CTA_LABELS]) {
1745 err = ctnetlink_attach_labels(ct, cda);
1753 static struct nf_conn *
1754 ctnetlink_create_conntrack(struct net *net,
1755 const struct nf_conntrack_zone *zone,
1756 const struct nlattr * const cda[],
1757 struct nf_conntrack_tuple *otuple,
1758 struct nf_conntrack_tuple *rtuple,
1763 struct nf_conntrack_helper *helper;
1764 struct nf_conn_tstamp *tstamp;
1766 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1768 return ERR_PTR(-ENOMEM);
1770 if (!cda[CTA_TIMEOUT])
1773 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1776 if (cda[CTA_HELP]) {
1777 char *helpname = NULL;
1778 struct nlattr *helpinfo = NULL;
1780 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1784 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1785 nf_ct_protonum(ct));
1786 if (helper == NULL) {
1788 #ifdef CONFIG_MODULES
1789 if (request_module("nfct-helper-%s", helpname) < 0) {
1795 helper = __nf_conntrack_helper_find(helpname,
1797 nf_ct_protonum(ct));
1807 struct nf_conn_help *help;
1809 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1814 /* set private helper data if allowed. */
1815 if (helper->from_nlattr)
1816 helper->from_nlattr(helpinfo, ct);
1818 /* not in hash table yet so not strictly necessary */
1819 RCU_INIT_POINTER(help->helper, helper);
1822 /* try an implicit helper assignation */
1823 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1828 err = ctnetlink_setup_nat(ct, cda);
1832 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1833 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1834 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1835 nf_ct_labels_ext_add(ct);
1836 nfct_seqadj_ext_add(ct);
1837 nfct_synproxy_ext_add(ct);
1839 /* we must add conntrack extensions before confirmation. */
1840 ct->status |= IPS_CONFIRMED;
1842 if (cda[CTA_STATUS]) {
1843 err = ctnetlink_change_status(ct, cda);
1848 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1849 err = ctnetlink_change_seq_adj(ct, cda);
1854 memset(&ct->proto, 0, sizeof(ct->proto));
1855 if (cda[CTA_PROTOINFO]) {
1856 err = ctnetlink_change_protoinfo(ct, cda);
1861 #if defined(CONFIG_NF_CONNTRACK_MARK)
1863 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1866 /* setup master conntrack: this is a confirmed expectation */
1867 if (cda[CTA_TUPLE_MASTER]) {
1868 struct nf_conntrack_tuple master;
1869 struct nf_conntrack_tuple_hash *master_h;
1870 struct nf_conn *master_ct;
1872 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1877 master_h = nf_conntrack_find_get(net, zone, &master);
1878 if (master_h == NULL) {
1882 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1883 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1884 ct->master = master_ct;
1886 tstamp = nf_conn_tstamp_find(ct);
1888 tstamp->start = ktime_get_real_ns();
1890 err = nf_conntrack_hash_check_insert(ct);
1901 nf_conntrack_free(ct);
1902 return ERR_PTR(err);
1905 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1906 struct sk_buff *skb,
1907 const struct nlmsghdr *nlh,
1908 const struct nlattr * const cda[],
1909 struct netlink_ext_ack *extack)
1911 struct nf_conntrack_tuple otuple, rtuple;
1912 struct nf_conntrack_tuple_hash *h = NULL;
1913 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1915 u_int8_t u3 = nfmsg->nfgen_family;
1916 struct nf_conntrack_zone zone;
1919 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1923 if (cda[CTA_TUPLE_ORIG]) {
1924 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1930 if (cda[CTA_TUPLE_REPLY]) {
1931 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1937 if (cda[CTA_TUPLE_ORIG])
1938 h = nf_conntrack_find_get(net, &zone, &otuple);
1939 else if (cda[CTA_TUPLE_REPLY])
1940 h = nf_conntrack_find_get(net, &zone, &rtuple);
1944 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1945 enum ip_conntrack_events events;
1947 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1949 if (otuple.dst.protonum != rtuple.dst.protonum)
1952 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1958 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1959 events = 1 << IPCT_RELATED;
1961 events = 1 << IPCT_NEW;
1963 if (cda[CTA_LABELS] &&
1964 ctnetlink_attach_labels(ct, cda) == 0)
1965 events |= (1 << IPCT_LABEL);
1967 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1968 (1 << IPCT_ASSURED) |
1969 (1 << IPCT_HELPER) |
1970 (1 << IPCT_PROTOINFO) |
1971 (1 << IPCT_SEQADJ) |
1972 (1 << IPCT_MARK) | events,
1973 ct, NETLINK_CB(skb).portid,
1980 /* implicit 'else' */
1983 ct = nf_ct_tuplehash_to_ctrack(h);
1984 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1985 err = ctnetlink_change_conntrack(ct, cda);
1987 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1988 (1 << IPCT_ASSURED) |
1989 (1 << IPCT_HELPER) |
1991 (1 << IPCT_PROTOINFO) |
1992 (1 << IPCT_SEQADJ) |
1994 ct, NETLINK_CB(skb).portid,
2004 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2005 __u16 cpu, const struct ip_conntrack_stat *st)
2007 struct nlmsghdr *nlh;
2008 struct nfgenmsg *nfmsg;
2009 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2011 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2012 IPCTNL_MSG_CT_GET_STATS_CPU);
2013 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2017 nfmsg = nlmsg_data(nlh);
2018 nfmsg->nfgen_family = AF_UNSPEC;
2019 nfmsg->version = NFNETLINK_V0;
2020 nfmsg->res_id = htons(cpu);
2022 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2023 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2024 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2025 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2026 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2027 htonl(st->insert_failed)) ||
2028 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2029 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2030 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2031 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2032 htonl(st->search_restart)))
2033 goto nla_put_failure;
2035 nlmsg_end(skb, nlh);
2040 nlmsg_cancel(skb, nlh);
2045 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2048 struct net *net = sock_net(skb->sk);
2050 if (cb->args[0] == nr_cpu_ids)
2053 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2054 const struct ip_conntrack_stat *st;
2056 if (!cpu_possible(cpu))
2059 st = per_cpu_ptr(net->ct.stat, cpu);
2060 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2061 NETLINK_CB(cb->skb).portid,
2071 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2072 struct sk_buff *skb,
2073 const struct nlmsghdr *nlh,
2074 const struct nlattr * const cda[],
2075 struct netlink_ext_ack *extack)
2077 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2078 struct netlink_dump_control c = {
2079 .dump = ctnetlink_ct_stat_cpu_dump,
2081 return netlink_dump_start(ctnl, skb, nlh, &c);
2088 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2091 struct nlmsghdr *nlh;
2092 struct nfgenmsg *nfmsg;
2093 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2094 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2096 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2097 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2101 nfmsg = nlmsg_data(nlh);
2102 nfmsg->nfgen_family = AF_UNSPEC;
2103 nfmsg->version = NFNETLINK_V0;
2106 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2107 goto nla_put_failure;
2109 nlmsg_end(skb, nlh);
2114 nlmsg_cancel(skb, nlh);
2118 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2119 struct sk_buff *skb, const struct nlmsghdr *nlh,
2120 const struct nlattr * const cda[],
2121 struct netlink_ext_ack *extack)
2123 struct sk_buff *skb2;
2126 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2130 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2132 NFNL_MSG_TYPE(nlh->nlmsg_type),
2137 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2146 /* this avoids a loop in nfnetlink. */
2147 return err == -EAGAIN ? -ENOBUFS : err;
2150 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2151 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2152 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2153 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2154 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2155 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2156 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2157 .len = NF_CT_HELPER_NAME_LEN - 1 },
2158 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2159 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2160 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2161 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2162 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2165 static struct nf_conntrack_expect *
2166 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2167 struct nf_conntrack_helper *helper,
2168 struct nf_conntrack_tuple *tuple,
2169 struct nf_conntrack_tuple *mask);
2171 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2173 ctnetlink_glue_build_size(const struct nf_conn *ct)
2175 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2176 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2177 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2178 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2179 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2180 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2181 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2182 + nla_total_size(0) /* CTA_PROTOINFO */
2183 + nla_total_size(0) /* CTA_HELP */
2184 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2185 + ctnetlink_secctx_size(ct)
2186 #ifdef CONFIG_NF_NAT_NEEDED
2187 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2188 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2190 #ifdef CONFIG_NF_CONNTRACK_MARK
2191 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2193 #ifdef CONFIG_NF_CONNTRACK_ZONES
2194 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2196 + ctnetlink_proto_size(ct)
2200 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2201 enum ip_conntrack_info *ctinfo)
2203 return nf_ct_get(skb, ctinfo);
2206 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2208 const struct nf_conntrack_zone *zone;
2209 struct nlattr *nest_parms;
2211 zone = nf_ct_zone(ct);
2213 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2215 goto nla_put_failure;
2216 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2217 goto nla_put_failure;
2218 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2219 NF_CT_ZONE_DIR_ORIG) < 0)
2220 goto nla_put_failure;
2221 nla_nest_end(skb, nest_parms);
2223 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2225 goto nla_put_failure;
2226 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2227 goto nla_put_failure;
2228 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2229 NF_CT_ZONE_DIR_REPL) < 0)
2230 goto nla_put_failure;
2231 nla_nest_end(skb, nest_parms);
2233 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2234 NF_CT_DEFAULT_ZONE_DIR) < 0)
2235 goto nla_put_failure;
2237 if (ctnetlink_dump_id(skb, ct) < 0)
2238 goto nla_put_failure;
2240 if (ctnetlink_dump_status(skb, ct) < 0)
2241 goto nla_put_failure;
2243 if (ctnetlink_dump_timeout(skb, ct) < 0)
2244 goto nla_put_failure;
2246 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2247 goto nla_put_failure;
2249 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2250 goto nla_put_failure;
2252 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2253 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2254 goto nla_put_failure;
2256 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2257 goto nla_put_failure;
2259 if ((ct->status & IPS_SEQ_ADJUST) &&
2260 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2261 goto nla_put_failure;
2263 #ifdef CONFIG_NF_CONNTRACK_MARK
2264 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2265 goto nla_put_failure;
2267 if (ctnetlink_dump_labels(skb, ct) < 0)
2268 goto nla_put_failure;
2276 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2277 enum ip_conntrack_info ctinfo,
2278 u_int16_t ct_attr, u_int16_t ct_info_attr)
2280 struct nlattr *nest_parms;
2282 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2284 goto nla_put_failure;
2286 if (__ctnetlink_glue_build(skb, ct) < 0)
2287 goto nla_put_failure;
2289 nla_nest_end(skb, nest_parms);
2291 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2292 goto nla_put_failure;
2301 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2303 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2304 unsigned long d = ct->status ^ status;
2306 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2307 /* SEEN_REPLY bit can only be set */
2310 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2311 /* ASSURED bit can only be set */
2314 /* This check is less strict than ctnetlink_change_status()
2315 * because callers often flip IPS_EXPECTED bits when sending
2316 * an NFQA_CT attribute to the kernel. So ignore the
2317 * unchangeable bits but do not error out. Also user programs
2318 * are allowed to clear the bits that they are allowed to change.
2320 __ctnetlink_change_status(ct, status, ~status);
2325 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2329 if (cda[CTA_TIMEOUT]) {
2330 err = ctnetlink_change_timeout(ct, cda);
2334 if (cda[CTA_STATUS]) {
2335 err = ctnetlink_update_status(ct, cda);
2339 if (cda[CTA_HELP]) {
2340 err = ctnetlink_change_helper(ct, cda);
2344 if (cda[CTA_LABELS]) {
2345 err = ctnetlink_attach_labels(ct, cda);
2349 #if defined(CONFIG_NF_CONNTRACK_MARK)
2350 if (cda[CTA_MARK]) {
2351 u32 mask = 0, mark, newmark;
2352 if (cda[CTA_MARK_MASK])
2353 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2355 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2356 newmark = (ct->mark & mask) ^ mark;
2357 if (newmark != ct->mark)
2365 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2367 struct nlattr *cda[CTA_MAX+1];
2370 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2374 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2377 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2378 const struct nf_conn *ct,
2379 struct nf_conntrack_tuple *tuple,
2380 struct nf_conntrack_tuple *mask)
2384 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2385 nf_ct_l3num(ct), NULL);
2389 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2390 nf_ct_l3num(ct), NULL);
2394 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2395 u32 portid, u32 report)
2397 struct nlattr *cda[CTA_EXPECT_MAX+1];
2398 struct nf_conntrack_tuple tuple, mask;
2399 struct nf_conntrack_helper *helper = NULL;
2400 struct nf_conntrack_expect *exp;
2403 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2408 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2413 if (cda[CTA_EXPECT_HELP_NAME]) {
2414 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2416 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2417 nf_ct_protonum(ct));
2422 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2423 helper, &tuple, &mask);
2425 return PTR_ERR(exp);
2427 err = nf_ct_expect_related_report(exp, portid, report);
2428 nf_ct_expect_put(exp);
2432 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2433 enum ip_conntrack_info ctinfo, int diff)
2435 if (!(ct->status & IPS_NAT_MASK))
2438 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2441 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2442 .get_ct = ctnetlink_glue_get_ct,
2443 .build_size = ctnetlink_glue_build_size,
2444 .build = ctnetlink_glue_build,
2445 .parse = ctnetlink_glue_parse,
2446 .attach_expect = ctnetlink_glue_attach_expect,
2447 .seq_adjust = ctnetlink_glue_seqadj,
2449 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2451 /***********************************************************************
2453 ***********************************************************************/
2455 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2456 const struct nf_conntrack_tuple *tuple,
2459 struct nlattr *nest_parms;
2461 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2463 goto nla_put_failure;
2464 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2465 goto nla_put_failure;
2466 nla_nest_end(skb, nest_parms);
2474 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2475 const struct nf_conntrack_tuple *tuple,
2476 const struct nf_conntrack_tuple_mask *mask)
2478 const struct nf_conntrack_l3proto *l3proto;
2479 const struct nf_conntrack_l4proto *l4proto;
2480 struct nf_conntrack_tuple m;
2481 struct nlattr *nest_parms;
2484 memset(&m, 0xFF, sizeof(m));
2485 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2486 m.src.u.all = mask->src.u.all;
2487 m.dst.protonum = tuple->dst.protonum;
2489 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2491 goto nla_put_failure;
2494 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2495 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2497 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2498 tuple->dst.protonum);
2499 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2503 if (unlikely(ret < 0))
2504 goto nla_put_failure;
2506 nla_nest_end(skb, nest_parms);
2514 static const union nf_inet_addr any_addr;
2517 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2518 const struct nf_conntrack_expect *exp)
2520 struct nf_conn *master = exp->master;
2521 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2522 struct nf_conn_help *help;
2523 #ifdef CONFIG_NF_NAT_NEEDED
2524 struct nlattr *nest_parms;
2525 struct nf_conntrack_tuple nat_tuple = {};
2527 struct nf_ct_helper_expectfn *expfn;
2532 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2533 goto nla_put_failure;
2534 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2535 goto nla_put_failure;
2536 if (ctnetlink_exp_dump_tuple(skb,
2537 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2538 CTA_EXPECT_MASTER) < 0)
2539 goto nla_put_failure;
2541 #ifdef CONFIG_NF_NAT_NEEDED
2542 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2543 exp->saved_proto.all) {
2544 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2546 goto nla_put_failure;
2548 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2549 goto nla_put_failure;
2551 nat_tuple.src.l3num = nf_ct_l3num(master);
2552 nat_tuple.src.u3 = exp->saved_addr;
2553 nat_tuple.dst.protonum = nf_ct_protonum(master);
2554 nat_tuple.src.u = exp->saved_proto;
2556 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2557 CTA_EXPECT_NAT_TUPLE) < 0)
2558 goto nla_put_failure;
2559 nla_nest_end(skb, nest_parms);
2562 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2563 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2564 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2565 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2566 goto nla_put_failure;
2567 help = nfct_help(master);
2569 struct nf_conntrack_helper *helper;
2571 helper = rcu_dereference(help->helper);
2573 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2574 goto nla_put_failure;
2576 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2577 if (expfn != NULL &&
2578 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2579 goto nla_put_failure;
2588 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2589 int event, const struct nf_conntrack_expect *exp)
2591 struct nlmsghdr *nlh;
2592 struct nfgenmsg *nfmsg;
2593 unsigned int flags = portid ? NLM_F_MULTI : 0;
2595 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2596 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2600 nfmsg = nlmsg_data(nlh);
2601 nfmsg->nfgen_family = exp->tuple.src.l3num;
2602 nfmsg->version = NFNETLINK_V0;
2605 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2606 goto nla_put_failure;
2608 nlmsg_end(skb, nlh);
2613 nlmsg_cancel(skb, nlh);
2617 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2619 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2621 struct nf_conntrack_expect *exp = item->exp;
2622 struct net *net = nf_ct_exp_net(exp);
2623 struct nlmsghdr *nlh;
2624 struct nfgenmsg *nfmsg;
2625 struct sk_buff *skb;
2626 unsigned int type, group;
2629 if (events & (1 << IPEXP_DESTROY)) {
2630 type = IPCTNL_MSG_EXP_DELETE;
2631 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2632 } else if (events & (1 << IPEXP_NEW)) {
2633 type = IPCTNL_MSG_EXP_NEW;
2634 flags = NLM_F_CREATE|NLM_F_EXCL;
2635 group = NFNLGRP_CONNTRACK_EXP_NEW;
2639 if (!item->report && !nfnetlink_has_listeners(net, group))
2642 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2646 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2647 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2651 nfmsg = nlmsg_data(nlh);
2652 nfmsg->nfgen_family = exp->tuple.src.l3num;
2653 nfmsg->version = NFNETLINK_V0;
2656 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2657 goto nla_put_failure;
2659 nlmsg_end(skb, nlh);
2660 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2664 nlmsg_cancel(skb, nlh);
2668 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2672 static int ctnetlink_exp_done(struct netlink_callback *cb)
2675 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2680 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2682 struct net *net = sock_net(skb->sk);
2683 struct nf_conntrack_expect *exp, *last;
2684 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2685 u_int8_t l3proto = nfmsg->nfgen_family;
2688 last = (struct nf_conntrack_expect *)cb->args[1];
2689 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2691 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2693 if (l3proto && exp->tuple.src.l3num != l3proto)
2696 if (!net_eq(nf_ct_net(exp->master), net))
2704 if (ctnetlink_exp_fill_info(skb,
2705 NETLINK_CB(cb->skb).portid,
2709 if (!refcount_inc_not_zero(&exp->use))
2711 cb->args[1] = (unsigned long)exp;
2723 nf_ct_expect_put(last);
2729 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2731 struct nf_conntrack_expect *exp, *last;
2732 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2733 struct nf_conn *ct = cb->data;
2734 struct nf_conn_help *help = nfct_help(ct);
2735 u_int8_t l3proto = nfmsg->nfgen_family;
2741 last = (struct nf_conntrack_expect *)cb->args[1];
2743 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2744 if (l3proto && exp->tuple.src.l3num != l3proto)
2751 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2755 if (!refcount_inc_not_zero(&exp->use))
2757 cb->args[1] = (unsigned long)exp;
2769 nf_ct_expect_put(last);
2774 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2775 struct sk_buff *skb,
2776 const struct nlmsghdr *nlh,
2777 const struct nlattr * const cda[],
2778 struct netlink_ext_ack *extack)
2781 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2782 u_int8_t u3 = nfmsg->nfgen_family;
2783 struct nf_conntrack_tuple tuple;
2784 struct nf_conntrack_tuple_hash *h;
2786 struct nf_conntrack_zone zone;
2787 struct netlink_dump_control c = {
2788 .dump = ctnetlink_exp_ct_dump_table,
2789 .done = ctnetlink_exp_done,
2792 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2797 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2801 h = nf_conntrack_find_get(net, &zone, &tuple);
2805 ct = nf_ct_tuplehash_to_ctrack(h);
2806 /* No expectation linked to this connection tracking. */
2807 if (!nfct_help(ct)) {
2814 err = netlink_dump_start(ctnl, skb, nlh, &c);
2820 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2821 struct sk_buff *skb, const struct nlmsghdr *nlh,
2822 const struct nlattr * const cda[],
2823 struct netlink_ext_ack *extack)
2825 struct nf_conntrack_tuple tuple;
2826 struct nf_conntrack_expect *exp;
2827 struct sk_buff *skb2;
2828 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2829 u_int8_t u3 = nfmsg->nfgen_family;
2830 struct nf_conntrack_zone zone;
2833 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2834 if (cda[CTA_EXPECT_MASTER])
2835 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
2838 struct netlink_dump_control c = {
2839 .dump = ctnetlink_exp_dump_table,
2840 .done = ctnetlink_exp_done,
2842 return netlink_dump_start(ctnl, skb, nlh, &c);
2846 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2850 if (cda[CTA_EXPECT_TUPLE])
2851 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2853 else if (cda[CTA_EXPECT_MASTER])
2854 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2862 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2866 if (cda[CTA_EXPECT_ID]) {
2867 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2868 if (ntohl(id) != (u32)(unsigned long)exp) {
2869 nf_ct_expect_put(exp);
2875 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2877 nf_ct_expect_put(exp);
2882 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2883 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2885 nf_ct_expect_put(exp);
2889 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2898 /* this avoids a loop in nfnetlink. */
2899 return err == -EAGAIN ? -ENOBUFS : err;
2902 static bool expect_iter_name(struct nf_conntrack_expect *exp, void *data)
2904 const struct nf_conn_help *m_help;
2905 const char *name = data;
2907 m_help = nfct_help(exp->master);
2909 return strcmp(m_help->helper->name, name) == 0;
2912 static bool expect_iter_all(struct nf_conntrack_expect *exp, void *data)
2917 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2918 struct sk_buff *skb, const struct nlmsghdr *nlh,
2919 const struct nlattr * const cda[],
2920 struct netlink_ext_ack *extack)
2922 struct nf_conntrack_expect *exp;
2923 struct nf_conntrack_tuple tuple;
2924 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2925 u_int8_t u3 = nfmsg->nfgen_family;
2926 struct nf_conntrack_zone zone;
2929 if (cda[CTA_EXPECT_TUPLE]) {
2930 /* delete a single expect by tuple */
2931 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2935 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2940 /* bump usage count to 2 */
2941 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2945 if (cda[CTA_EXPECT_ID]) {
2946 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2947 if (ntohl(id) != (u32)(unsigned long)exp) {
2948 nf_ct_expect_put(exp);
2953 /* after list removal, usage count == 1 */
2954 spin_lock_bh(&nf_conntrack_expect_lock);
2955 if (del_timer(&exp->timeout)) {
2956 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2958 nf_ct_expect_put(exp);
2960 spin_unlock_bh(&nf_conntrack_expect_lock);
2961 /* have to put what we 'get' above.
2962 * after this line usage count == 0 */
2963 nf_ct_expect_put(exp);
2964 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2965 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2967 nf_ct_expect_iterate_net(net, expect_iter_name, name,
2968 NETLINK_CB(skb).portid,
2971 /* This basically means we have to flush everything*/
2972 nf_ct_expect_iterate_net(net, expect_iter_all, NULL,
2973 NETLINK_CB(skb).portid,
2980 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2981 const struct nlattr * const cda[])
2983 if (cda[CTA_EXPECT_TIMEOUT]) {
2984 if (!del_timer(&x->timeout))
2987 x->timeout.expires = jiffies +
2988 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2989 add_timer(&x->timeout);
2994 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2995 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2996 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3000 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3001 struct nf_conntrack_expect *exp,
3004 #ifdef CONFIG_NF_NAT_NEEDED
3005 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3006 struct nf_conntrack_tuple nat_tuple = {};
3009 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3010 exp_nat_nla_policy, NULL);
3014 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3017 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3018 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3023 exp->saved_addr = nat_tuple.src.u3;
3024 exp->saved_proto = nat_tuple.src.u;
3025 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3033 static struct nf_conntrack_expect *
3034 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3035 struct nf_conntrack_helper *helper,
3036 struct nf_conntrack_tuple *tuple,
3037 struct nf_conntrack_tuple *mask)
3039 u_int32_t class = 0;
3040 struct nf_conntrack_expect *exp;
3041 struct nf_conn_help *help;
3044 help = nfct_help(ct);
3046 return ERR_PTR(-EOPNOTSUPP);
3048 if (cda[CTA_EXPECT_CLASS] && helper) {
3049 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3050 if (class > helper->expect_class_max)
3051 return ERR_PTR(-EINVAL);
3053 exp = nf_ct_expect_alloc(ct);
3055 return ERR_PTR(-ENOMEM);
3057 if (cda[CTA_EXPECT_FLAGS]) {
3058 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3059 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3063 if (cda[CTA_EXPECT_FN]) {
3064 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3065 struct nf_ct_helper_expectfn *expfn;
3067 expfn = nf_ct_helper_expectfn_find_by_name(name);
3068 if (expfn == NULL) {
3072 exp->expectfn = expfn->expectfn;
3074 exp->expectfn = NULL;
3078 exp->helper = helper;
3079 exp->tuple = *tuple;
3080 exp->mask.src.u3 = mask->src.u3;
3081 exp->mask.src.u.all = mask->src.u.all;
3083 if (cda[CTA_EXPECT_NAT]) {
3084 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3085 exp, nf_ct_l3num(ct));
3091 nf_ct_expect_put(exp);
3092 return ERR_PTR(err);
3096 ctnetlink_create_expect(struct net *net,
3097 const struct nf_conntrack_zone *zone,
3098 const struct nlattr * const cda[],
3099 u_int8_t u3, u32 portid, int report)
3101 struct nf_conntrack_tuple tuple, mask, master_tuple;
3102 struct nf_conntrack_tuple_hash *h = NULL;
3103 struct nf_conntrack_helper *helper = NULL;
3104 struct nf_conntrack_expect *exp;
3108 /* caller guarantees that those three CTA_EXPECT_* exist */
3109 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3113 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3117 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3122 /* Look for master conntrack of this expectation */
3123 h = nf_conntrack_find_get(net, zone, &master_tuple);
3126 ct = nf_ct_tuplehash_to_ctrack(h);
3129 if (cda[CTA_EXPECT_HELP_NAME]) {
3130 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3132 helper = __nf_conntrack_helper_find(helpname, u3,
3133 nf_ct_protonum(ct));
3134 if (helper == NULL) {
3136 #ifdef CONFIG_MODULES
3137 if (request_module("nfct-helper-%s", helpname) < 0) {
3142 helper = __nf_conntrack_helper_find(helpname, u3,
3143 nf_ct_protonum(ct));
3155 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3161 err = nf_ct_expect_related_report(exp, portid, report);
3162 nf_ct_expect_put(exp);
3170 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3171 struct sk_buff *skb, const struct nlmsghdr *nlh,
3172 const struct nlattr * const cda[],
3173 struct netlink_ext_ack *extack)
3175 struct nf_conntrack_tuple tuple;
3176 struct nf_conntrack_expect *exp;
3177 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3178 u_int8_t u3 = nfmsg->nfgen_family;
3179 struct nf_conntrack_zone zone;
3182 if (!cda[CTA_EXPECT_TUPLE]
3183 || !cda[CTA_EXPECT_MASK]
3184 || !cda[CTA_EXPECT_MASTER])
3187 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3191 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3196 spin_lock_bh(&nf_conntrack_expect_lock);
3197 exp = __nf_ct_expect_find(net, &zone, &tuple);
3199 spin_unlock_bh(&nf_conntrack_expect_lock);
3201 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3202 err = ctnetlink_create_expect(net, &zone, cda, u3,
3203 NETLINK_CB(skb).portid,
3210 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3211 err = ctnetlink_change_expect(exp, cda);
3212 spin_unlock_bh(&nf_conntrack_expect_lock);
3218 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3219 const struct ip_conntrack_stat *st)
3221 struct nlmsghdr *nlh;
3222 struct nfgenmsg *nfmsg;
3223 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3225 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3226 IPCTNL_MSG_EXP_GET_STATS_CPU);
3227 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3231 nfmsg = nlmsg_data(nlh);
3232 nfmsg->nfgen_family = AF_UNSPEC;
3233 nfmsg->version = NFNETLINK_V0;
3234 nfmsg->res_id = htons(cpu);
3236 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3237 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3238 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3239 goto nla_put_failure;
3241 nlmsg_end(skb, nlh);
3246 nlmsg_cancel(skb, nlh);
3251 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3254 struct net *net = sock_net(skb->sk);
3256 if (cb->args[0] == nr_cpu_ids)
3259 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3260 const struct ip_conntrack_stat *st;
3262 if (!cpu_possible(cpu))
3265 st = per_cpu_ptr(net->ct.stat, cpu);
3266 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3276 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3277 struct sk_buff *skb,
3278 const struct nlmsghdr *nlh,
3279 const struct nlattr * const cda[],
3280 struct netlink_ext_ack *extack)
3282 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3283 struct netlink_dump_control c = {
3284 .dump = ctnetlink_exp_stat_cpu_dump,
3286 return netlink_dump_start(ctnl, skb, nlh, &c);
3292 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3293 static struct nf_ct_event_notifier ctnl_notifier = {
3294 .fcn = ctnetlink_conntrack_event,
3297 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3298 .fcn = ctnetlink_expect_event,
3302 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3303 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3304 .attr_count = CTA_MAX,
3305 .policy = ct_nla_policy },
3306 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3307 .attr_count = CTA_MAX,
3308 .policy = ct_nla_policy },
3309 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3310 .attr_count = CTA_MAX,
3311 .policy = ct_nla_policy },
3312 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3313 .attr_count = CTA_MAX,
3314 .policy = ct_nla_policy },
3315 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3316 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3317 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3318 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3321 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3322 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3323 .attr_count = CTA_EXPECT_MAX,
3324 .policy = exp_nla_policy },
3325 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3326 .attr_count = CTA_EXPECT_MAX,
3327 .policy = exp_nla_policy },
3328 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3329 .attr_count = CTA_EXPECT_MAX,
3330 .policy = exp_nla_policy },
3331 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3334 static const struct nfnetlink_subsystem ctnl_subsys = {
3335 .name = "conntrack",
3336 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3337 .cb_count = IPCTNL_MSG_MAX,
3341 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3342 .name = "conntrack_expect",
3343 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3344 .cb_count = IPCTNL_MSG_EXP_MAX,
3348 MODULE_ALIAS("ip_conntrack_netlink");
3349 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3350 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3352 static int __net_init ctnetlink_net_init(struct net *net)
3354 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3357 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3359 pr_err("ctnetlink_init: cannot register notifier.\n");
3363 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3365 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3366 goto err_unreg_notifier;
3371 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3373 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3379 static void ctnetlink_net_exit(struct net *net)
3381 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3382 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3383 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3387 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3391 list_for_each_entry(net, net_exit_list, exit_list)
3392 ctnetlink_net_exit(net);
3395 static struct pernet_operations ctnetlink_net_ops = {
3396 .init = ctnetlink_net_init,
3397 .exit_batch = ctnetlink_net_exit_batch,
3400 static int __init ctnetlink_init(void)
3404 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3405 ret = nfnetlink_subsys_register(&ctnl_subsys);
3407 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3411 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3413 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3414 goto err_unreg_subsys;
3417 ret = register_pernet_subsys(&ctnetlink_net_ops);
3419 pr_err("ctnetlink_init: cannot register pernet operations\n");
3420 goto err_unreg_exp_subsys;
3422 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3423 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3424 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3428 err_unreg_exp_subsys:
3429 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3431 nfnetlink_subsys_unregister(&ctnl_subsys);
3436 static void __exit ctnetlink_exit(void)
3438 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3440 unregister_pernet_subsys(&ctnetlink_net_ops);
3441 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3442 nfnetlink_subsys_unregister(&ctnl_subsys);
3443 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3444 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3449 module_init(ctnetlink_init);
3450 module_exit(ctnetlink_exit);