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 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 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)
113 struct nf_conntrack_l3proto *l3proto;
114 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 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 struct nf_conntrack_l3proto *l3proto;
539 struct nf_conntrack_l4proto *l4proto;
543 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
544 len += l3proto->nla_size;
546 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
547 len += l4proto->nla_size;
553 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
555 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
557 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
558 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
559 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
563 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
565 #ifdef CONFIG_NF_CONNTRACK_SECMARK
568 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
572 return nla_total_size(0) /* CTA_SECCTX */
573 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
579 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
581 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
582 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
584 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
590 #ifdef CONFIG_NF_CONNTRACK_EVENTS
591 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
593 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
594 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
595 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
596 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
597 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
598 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
599 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
600 + ctnetlink_acct_size(ct)
601 + ctnetlink_timestamp_size(ct)
602 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
603 + nla_total_size(0) /* CTA_PROTOINFO */
604 + nla_total_size(0) /* CTA_HELP */
605 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
606 + ctnetlink_secctx_size(ct)
607 #ifdef CONFIG_NF_NAT_NEEDED
608 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
609 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
611 #ifdef CONFIG_NF_CONNTRACK_MARK
612 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
614 #ifdef CONFIG_NF_CONNTRACK_ZONES
615 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
617 + ctnetlink_proto_size(ct)
618 + ctnetlink_label_size(ct)
623 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
625 const struct nf_conntrack_zone *zone;
627 struct nlmsghdr *nlh;
628 struct nfgenmsg *nfmsg;
629 struct nlattr *nest_parms;
630 struct nf_conn *ct = item->ct;
633 unsigned int flags = 0, group;
636 if (events & (1 << IPCT_DESTROY)) {
637 type = IPCTNL_MSG_CT_DELETE;
638 group = NFNLGRP_CONNTRACK_DESTROY;
639 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
640 type = IPCTNL_MSG_CT_NEW;
641 flags = NLM_F_CREATE|NLM_F_EXCL;
642 group = NFNLGRP_CONNTRACK_NEW;
644 type = IPCTNL_MSG_CT_NEW;
645 group = NFNLGRP_CONNTRACK_UPDATE;
650 if (!item->report && !nfnetlink_has_listeners(net, group))
653 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
657 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, type);
658 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
662 nfmsg = nlmsg_data(nlh);
663 nfmsg->nfgen_family = nf_ct_l3num(ct);
664 nfmsg->version = NFNETLINK_V0;
668 zone = nf_ct_zone(ct);
670 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
672 goto nla_put_failure;
673 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
674 goto nla_put_failure;
675 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
676 NF_CT_ZONE_DIR_ORIG) < 0)
677 goto nla_put_failure;
678 nla_nest_end(skb, nest_parms);
680 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
682 goto nla_put_failure;
683 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
684 goto nla_put_failure;
685 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
686 NF_CT_ZONE_DIR_REPL) < 0)
687 goto nla_put_failure;
688 nla_nest_end(skb, nest_parms);
690 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
691 NF_CT_DEFAULT_ZONE_DIR) < 0)
692 goto nla_put_failure;
694 if (ctnetlink_dump_id(skb, ct) < 0)
695 goto nla_put_failure;
697 if (ctnetlink_dump_status(skb, ct) < 0)
698 goto nla_put_failure;
700 if (events & (1 << IPCT_DESTROY)) {
701 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
702 ctnetlink_dump_timestamp(skb, ct) < 0)
703 goto nla_put_failure;
705 if (ctnetlink_dump_timeout(skb, ct) < 0)
706 goto nla_put_failure;
708 if (events & (1 << IPCT_PROTOINFO)
709 && ctnetlink_dump_protoinfo(skb, ct) < 0)
710 goto nla_put_failure;
712 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
713 && ctnetlink_dump_helpinfo(skb, ct) < 0)
714 goto nla_put_failure;
716 #ifdef CONFIG_NF_CONNTRACK_SECMARK
717 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
718 && ctnetlink_dump_secctx(skb, ct) < 0)
719 goto nla_put_failure;
721 if (events & (1 << IPCT_LABEL) &&
722 ctnetlink_dump_labels(skb, ct) < 0)
723 goto nla_put_failure;
725 if (events & (1 << IPCT_RELATED) &&
726 ctnetlink_dump_master(skb, ct) < 0)
727 goto nla_put_failure;
729 if (events & (1 << IPCT_SEQADJ) &&
730 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
731 goto nla_put_failure;
734 #ifdef CONFIG_NF_CONNTRACK_MARK
735 if ((events & (1 << IPCT_MARK) || ct->mark)
736 && ctnetlink_dump_mark(skb, ct) < 0)
737 goto nla_put_failure;
742 err = nfnetlink_send(skb, net, item->portid, group, item->report,
744 if (err == -ENOBUFS || err == -EAGAIN)
751 nlmsg_cancel(skb, nlh);
755 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
760 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
762 static int ctnetlink_done(struct netlink_callback *cb)
765 nf_ct_put((struct nf_conn *)cb->args[1]);
770 struct ctnetlink_filter {
777 static struct ctnetlink_filter *
778 ctnetlink_alloc_filter(const struct nlattr * const cda[])
780 #ifdef CONFIG_NF_CONNTRACK_MARK
781 struct ctnetlink_filter *filter;
783 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
785 return ERR_PTR(-ENOMEM);
787 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
788 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
792 return ERR_PTR(-EOPNOTSUPP);
796 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
798 struct ctnetlink_filter *filter = data;
803 #ifdef CONFIG_NF_CONNTRACK_MARK
804 if ((ct->mark & filter->mark.mask) == filter->mark.val)
812 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
814 struct net *net = sock_net(skb->sk);
815 struct nf_conn *ct, *last;
816 struct nf_conntrack_tuple_hash *h;
817 struct hlist_nulls_node *n;
818 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
819 u_int8_t l3proto = nfmsg->nfgen_family;
820 struct nf_conn *nf_ct_evict[8];
824 last = (struct nf_conn *)cb->args[1];
828 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
832 if (nf_ct_should_gc(nf_ct_evict[i]))
833 nf_ct_kill(nf_ct_evict[i]);
834 nf_ct_put(nf_ct_evict[i]);
837 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
838 nf_conntrack_lock(lockp);
839 if (cb->args[0] >= nf_conntrack_htable_size) {
843 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
845 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
847 ct = nf_ct_tuplehash_to_ctrack(h);
848 if (nf_ct_is_expired(ct)) {
849 if (i < ARRAY_SIZE(nf_ct_evict) &&
850 atomic_inc_not_zero(&ct->ct_general.use))
851 nf_ct_evict[i++] = ct;
855 if (!net_eq(net, nf_ct_net(ct)))
858 /* Dump entries of a given L3 protocol number.
859 * If it is not specified, ie. l3proto == 0,
860 * then dump everything. */
861 if (l3proto && nf_ct_l3num(ct) != l3proto)
868 if (!ctnetlink_filter_match(ct, cb->data))
873 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
875 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
879 nf_conntrack_get(&ct->ct_general);
880 cb->args[1] = (unsigned long)ct;
894 /* nf ct hash resize happened, now clear the leftover. */
895 if ((struct nf_conn *)cb->args[1] == last)
903 if (nf_ct_should_gc(nf_ct_evict[i]))
904 nf_ct_kill(nf_ct_evict[i]);
905 nf_ct_put(nf_ct_evict[i]);
911 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
912 struct nf_conntrack_tuple *tuple)
914 struct nlattr *tb[CTA_IP_MAX+1];
915 struct nf_conntrack_l3proto *l3proto;
918 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL, NULL);
923 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
925 if (likely(l3proto->nlattr_to_tuple)) {
926 ret = nla_validate_nested(attr, CTA_IP_MAX,
927 l3proto->nla_policy, NULL);
929 ret = l3proto->nlattr_to_tuple(tb, tuple);
937 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
938 [CTA_PROTO_NUM] = { .type = NLA_U8 },
941 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
942 struct nf_conntrack_tuple *tuple)
944 struct nlattr *tb[CTA_PROTO_MAX+1];
945 struct nf_conntrack_l4proto *l4proto;
948 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy,
953 if (!tb[CTA_PROTO_NUM])
955 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
958 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
960 if (likely(l4proto->nlattr_to_tuple)) {
961 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
962 l4proto->nla_policy, NULL);
964 ret = l4proto->nlattr_to_tuple(tb, tuple);
973 ctnetlink_parse_zone(const struct nlattr *attr,
974 struct nf_conntrack_zone *zone)
976 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
977 NF_CT_DEFAULT_ZONE_DIR, 0);
978 #ifdef CONFIG_NF_CONNTRACK_ZONES
980 zone->id = ntohs(nla_get_be16(attr));
989 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
990 struct nf_conntrack_zone *zone)
994 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
997 ret = ctnetlink_parse_zone(attr, zone);
1001 if (type == CTA_TUPLE_REPLY)
1002 zone->dir = NF_CT_ZONE_DIR_REPL;
1004 zone->dir = NF_CT_ZONE_DIR_ORIG;
1009 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1010 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1011 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1012 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1016 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1017 struct nf_conntrack_tuple *tuple, u32 type,
1018 u_int8_t l3num, struct nf_conntrack_zone *zone)
1020 struct nlattr *tb[CTA_TUPLE_MAX+1];
1023 memset(tuple, 0, sizeof(*tuple));
1025 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1030 if (!tb[CTA_TUPLE_IP])
1033 tuple->src.l3num = l3num;
1035 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1039 if (!tb[CTA_TUPLE_PROTO])
1042 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1046 if (tb[CTA_TUPLE_ZONE]) {
1050 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1056 /* orig and expect tuples get DIR_ORIGINAL */
1057 if (type == CTA_TUPLE_REPLY)
1058 tuple->dst.dir = IP_CT_DIR_REPLY;
1060 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1065 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1066 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1067 .len = NF_CT_HELPER_NAME_LEN - 1 },
1070 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1071 struct nlattr **helpinfo)
1074 struct nlattr *tb[CTA_HELP_MAX+1];
1076 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1080 if (!tb[CTA_HELP_NAME])
1083 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1085 if (tb[CTA_HELP_INFO])
1086 *helpinfo = tb[CTA_HELP_INFO];
1091 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1092 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1093 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1094 [CTA_STATUS] = { .type = NLA_U32 },
1095 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1096 [CTA_HELP] = { .type = NLA_NESTED },
1097 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1098 [CTA_TIMEOUT] = { .type = NLA_U32 },
1099 [CTA_MARK] = { .type = NLA_U32 },
1100 [CTA_ID] = { .type = NLA_U32 },
1101 [CTA_NAT_DST] = { .type = NLA_NESTED },
1102 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1103 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1104 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1105 [CTA_ZONE] = { .type = NLA_U16 },
1106 [CTA_MARK_MASK] = { .type = NLA_U32 },
1107 [CTA_LABELS] = { .type = NLA_BINARY,
1108 .len = NF_CT_LABELS_MAX_SIZE },
1109 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1110 .len = NF_CT_LABELS_MAX_SIZE },
1113 static int ctnetlink_flush_conntrack(struct net *net,
1114 const struct nlattr * const cda[],
1115 u32 portid, int report)
1117 struct ctnetlink_filter *filter = NULL;
1119 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1120 filter = ctnetlink_alloc_filter(cda);
1122 return PTR_ERR(filter);
1125 nf_ct_iterate_cleanup_net(net, ctnetlink_filter_match, filter,
1132 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1133 struct sk_buff *skb,
1134 const struct nlmsghdr *nlh,
1135 const struct nlattr * const cda[],
1136 struct netlink_ext_ack *extack)
1138 struct nf_conntrack_tuple_hash *h;
1139 struct nf_conntrack_tuple tuple;
1141 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1142 u_int8_t u3 = nfmsg->nfgen_family;
1143 struct nf_conntrack_zone zone;
1146 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1150 if (cda[CTA_TUPLE_ORIG])
1151 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1153 else if (cda[CTA_TUPLE_REPLY])
1154 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1157 return ctnetlink_flush_conntrack(net, cda,
1158 NETLINK_CB(skb).portid,
1165 h = nf_conntrack_find_get(net, &zone, &tuple);
1169 ct = nf_ct_tuplehash_to_ctrack(h);
1172 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1173 if (id != (u32)(unsigned long)ct) {
1179 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1185 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1186 struct sk_buff *skb,
1187 const struct nlmsghdr *nlh,
1188 const struct nlattr * const cda[],
1189 struct netlink_ext_ack *extack)
1191 struct nf_conntrack_tuple_hash *h;
1192 struct nf_conntrack_tuple tuple;
1194 struct sk_buff *skb2 = NULL;
1195 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1196 u_int8_t u3 = nfmsg->nfgen_family;
1197 struct nf_conntrack_zone zone;
1200 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1201 struct netlink_dump_control c = {
1202 .dump = ctnetlink_dump_table,
1203 .done = ctnetlink_done,
1206 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1207 struct ctnetlink_filter *filter;
1209 filter = ctnetlink_alloc_filter(cda);
1211 return PTR_ERR(filter);
1215 return netlink_dump_start(ctnl, skb, nlh, &c);
1218 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1222 if (cda[CTA_TUPLE_ORIG])
1223 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1225 else if (cda[CTA_TUPLE_REPLY])
1226 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1234 h = nf_conntrack_find_get(net, &zone, &tuple);
1238 ct = nf_ct_tuplehash_to_ctrack(h);
1241 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1248 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1249 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1255 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1264 /* this avoids a loop in nfnetlink. */
1265 return err == -EAGAIN ? -ENOBUFS : err;
1268 static int ctnetlink_done_list(struct netlink_callback *cb)
1271 nf_ct_put((struct nf_conn *)cb->args[1]);
1276 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1278 struct nf_conn *ct, *last;
1279 struct nf_conntrack_tuple_hash *h;
1280 struct hlist_nulls_node *n;
1281 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1282 u_int8_t l3proto = nfmsg->nfgen_family;
1285 struct hlist_nulls_head *list;
1286 struct net *net = sock_net(skb->sk);
1291 last = (struct nf_conn *)cb->args[1];
1293 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1294 struct ct_pcpu *pcpu;
1296 if (!cpu_possible(cpu))
1299 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1300 spin_lock_bh(&pcpu->lock);
1301 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1303 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1304 ct = nf_ct_tuplehash_to_ctrack(h);
1305 if (l3proto && nf_ct_l3num(ct) != l3proto)
1313 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1315 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1319 if (!atomic_inc_not_zero(&ct->ct_general.use))
1322 cb->args[1] = (unsigned long)ct;
1323 spin_unlock_bh(&pcpu->lock);
1331 spin_unlock_bh(&pcpu->lock);
1342 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1344 return ctnetlink_dump_list(skb, cb, true);
1347 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1348 struct sk_buff *skb,
1349 const struct nlmsghdr *nlh,
1350 const struct nlattr * const cda[],
1351 struct netlink_ext_ack *extack)
1353 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1354 struct netlink_dump_control c = {
1355 .dump = ctnetlink_dump_dying,
1356 .done = ctnetlink_done_list,
1358 return netlink_dump_start(ctnl, skb, nlh, &c);
1365 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1367 return ctnetlink_dump_list(skb, cb, false);
1370 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1371 struct sk_buff *skb,
1372 const struct nlmsghdr *nlh,
1373 const struct nlattr * const cda[],
1374 struct netlink_ext_ack *extack)
1376 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1377 struct netlink_dump_control c = {
1378 .dump = ctnetlink_dump_unconfirmed,
1379 .done = ctnetlink_done_list,
1381 return netlink_dump_start(ctnl, skb, nlh, &c);
1387 #ifdef CONFIG_NF_NAT_NEEDED
1389 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1390 enum nf_nat_manip_type manip,
1391 const struct nlattr *attr)
1393 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1396 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1397 if (!parse_nat_setup) {
1398 #ifdef CONFIG_MODULES
1400 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1401 if (request_module("nf-nat") < 0) {
1402 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1406 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1408 if (nfnetlink_parse_nat_setup_hook)
1414 err = parse_nat_setup(ct, manip, attr);
1415 if (err == -EAGAIN) {
1416 #ifdef CONFIG_MODULES
1418 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1419 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1420 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1424 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1435 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1440 /* Ignore these unchangable bits */
1441 on &= ~IPS_UNCHANGEABLE_MASK;
1442 off &= ~IPS_UNCHANGEABLE_MASK;
1444 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1445 if (on & (1 << bit))
1446 set_bit(bit, &ct->status);
1447 else if (off & (1 << bit))
1448 clear_bit(bit, &ct->status);
1453 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1456 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1457 d = ct->status ^ status;
1459 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1463 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1464 /* SEEN_REPLY bit can only be set */
1467 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1468 /* ASSURED bit can only be set */
1471 __ctnetlink_change_status(ct, status, 0);
1476 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1478 #ifdef CONFIG_NF_NAT_NEEDED
1481 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1484 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1489 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1493 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1499 static int ctnetlink_change_helper(struct nf_conn *ct,
1500 const struct nlattr * const cda[])
1502 struct nf_conntrack_helper *helper;
1503 struct nf_conn_help *help = nfct_help(ct);
1504 char *helpname = NULL;
1505 struct nlattr *helpinfo = NULL;
1508 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1512 /* don't change helper of sibling connections */
1514 /* If we try to change the helper to the same thing twice,
1515 * treat the second attempt as a no-op instead of returning
1521 helper = rcu_dereference(help->helper);
1522 if (helper && !strcmp(helper->name, helpname))
1530 if (!strcmp(helpname, "")) {
1531 if (help && help->helper) {
1532 /* we had a helper before ... */
1533 nf_ct_remove_expectations(ct);
1534 RCU_INIT_POINTER(help->helper, NULL);
1541 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1542 nf_ct_protonum(ct));
1543 if (helper == NULL) {
1549 if (help->helper == helper) {
1550 /* update private helper data if allowed. */
1551 if (helper->from_nlattr)
1552 helper->from_nlattr(helpinfo, ct);
1557 /* we cannot set a helper for an existing conntrack */
1565 static int ctnetlink_change_timeout(struct nf_conn *ct,
1566 const struct nlattr * const cda[])
1568 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1570 ct->timeout = nfct_time_stamp + timeout * HZ;
1572 if (test_bit(IPS_DYING_BIT, &ct->status))
1578 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1579 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1580 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1581 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1584 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1585 const struct nlattr * const cda[])
1587 const struct nlattr *attr = cda[CTA_PROTOINFO];
1588 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1589 struct nf_conntrack_l4proto *l4proto;
1592 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1598 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1599 if (l4proto->from_nlattr)
1600 err = l4proto->from_nlattr(tb, ct);
1606 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1607 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1608 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1609 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1612 static int change_seq_adj(struct nf_ct_seqadj *seq,
1613 const struct nlattr * const attr)
1616 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1618 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1622 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1625 seq->correction_pos =
1626 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1628 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1631 seq->offset_before =
1632 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1634 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1638 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1644 ctnetlink_change_seq_adj(struct nf_conn *ct,
1645 const struct nlattr * const cda[])
1647 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1653 spin_lock_bh(&ct->lock);
1654 if (cda[CTA_SEQ_ADJ_ORIG]) {
1655 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1656 cda[CTA_SEQ_ADJ_ORIG]);
1660 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1663 if (cda[CTA_SEQ_ADJ_REPLY]) {
1664 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1665 cda[CTA_SEQ_ADJ_REPLY]);
1669 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1672 spin_unlock_bh(&ct->lock);
1675 spin_unlock_bh(&ct->lock);
1680 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1682 #ifdef CONFIG_NF_CONNTRACK_LABELS
1683 size_t len = nla_len(cda[CTA_LABELS]);
1684 const void *mask = cda[CTA_LABELS_MASK];
1686 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1690 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1691 nla_len(cda[CTA_LABELS_MASK]) != len)
1693 mask = nla_data(cda[CTA_LABELS_MASK]);
1698 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1705 ctnetlink_change_conntrack(struct nf_conn *ct,
1706 const struct nlattr * const cda[])
1710 /* only allow NAT changes and master assignation for new conntracks */
1711 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1714 if (cda[CTA_HELP]) {
1715 err = ctnetlink_change_helper(ct, cda);
1720 if (cda[CTA_TIMEOUT]) {
1721 err = ctnetlink_change_timeout(ct, cda);
1726 if (cda[CTA_STATUS]) {
1727 err = ctnetlink_change_status(ct, cda);
1732 if (cda[CTA_PROTOINFO]) {
1733 err = ctnetlink_change_protoinfo(ct, cda);
1738 #if defined(CONFIG_NF_CONNTRACK_MARK)
1740 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1743 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1744 err = ctnetlink_change_seq_adj(ct, cda);
1749 if (cda[CTA_LABELS]) {
1750 err = ctnetlink_attach_labels(ct, cda);
1758 static struct nf_conn *
1759 ctnetlink_create_conntrack(struct net *net,
1760 const struct nf_conntrack_zone *zone,
1761 const struct nlattr * const cda[],
1762 struct nf_conntrack_tuple *otuple,
1763 struct nf_conntrack_tuple *rtuple,
1768 struct nf_conntrack_helper *helper;
1769 struct nf_conn_tstamp *tstamp;
1771 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1773 return ERR_PTR(-ENOMEM);
1775 if (!cda[CTA_TIMEOUT])
1778 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1781 if (cda[CTA_HELP]) {
1782 char *helpname = NULL;
1783 struct nlattr *helpinfo = NULL;
1785 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1789 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1790 nf_ct_protonum(ct));
1791 if (helper == NULL) {
1793 #ifdef CONFIG_MODULES
1794 if (request_module("nfct-helper-%s", helpname) < 0) {
1800 helper = __nf_conntrack_helper_find(helpname,
1802 nf_ct_protonum(ct));
1812 struct nf_conn_help *help;
1814 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1819 /* set private helper data if allowed. */
1820 if (helper->from_nlattr)
1821 helper->from_nlattr(helpinfo, ct);
1823 /* not in hash table yet so not strictly necessary */
1824 RCU_INIT_POINTER(help->helper, helper);
1827 /* try an implicit helper assignation */
1828 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1833 err = ctnetlink_setup_nat(ct, cda);
1837 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1838 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1839 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1840 nf_ct_labels_ext_add(ct);
1841 nfct_seqadj_ext_add(ct);
1842 nfct_synproxy_ext_add(ct);
1844 /* we must add conntrack extensions before confirmation. */
1845 ct->status |= IPS_CONFIRMED;
1847 if (cda[CTA_STATUS]) {
1848 err = ctnetlink_change_status(ct, cda);
1853 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1854 err = ctnetlink_change_seq_adj(ct, cda);
1859 memset(&ct->proto, 0, sizeof(ct->proto));
1860 if (cda[CTA_PROTOINFO]) {
1861 err = ctnetlink_change_protoinfo(ct, cda);
1866 #if defined(CONFIG_NF_CONNTRACK_MARK)
1868 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1871 /* setup master conntrack: this is a confirmed expectation */
1872 if (cda[CTA_TUPLE_MASTER]) {
1873 struct nf_conntrack_tuple master;
1874 struct nf_conntrack_tuple_hash *master_h;
1875 struct nf_conn *master_ct;
1877 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1882 master_h = nf_conntrack_find_get(net, zone, &master);
1883 if (master_h == NULL) {
1887 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1888 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1889 ct->master = master_ct;
1891 tstamp = nf_conn_tstamp_find(ct);
1893 tstamp->start = ktime_get_real_ns();
1895 err = nf_conntrack_hash_check_insert(ct);
1906 nf_conntrack_free(ct);
1907 return ERR_PTR(err);
1910 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1911 struct sk_buff *skb,
1912 const struct nlmsghdr *nlh,
1913 const struct nlattr * const cda[],
1914 struct netlink_ext_ack *extack)
1916 struct nf_conntrack_tuple otuple, rtuple;
1917 struct nf_conntrack_tuple_hash *h = NULL;
1918 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1920 u_int8_t u3 = nfmsg->nfgen_family;
1921 struct nf_conntrack_zone zone;
1924 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1928 if (cda[CTA_TUPLE_ORIG]) {
1929 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1935 if (cda[CTA_TUPLE_REPLY]) {
1936 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1942 if (cda[CTA_TUPLE_ORIG])
1943 h = nf_conntrack_find_get(net, &zone, &otuple);
1944 else if (cda[CTA_TUPLE_REPLY])
1945 h = nf_conntrack_find_get(net, &zone, &rtuple);
1949 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1950 enum ip_conntrack_events events;
1952 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1954 if (otuple.dst.protonum != rtuple.dst.protonum)
1957 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1963 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1964 events = 1 << IPCT_RELATED;
1966 events = 1 << IPCT_NEW;
1968 if (cda[CTA_LABELS] &&
1969 ctnetlink_attach_labels(ct, cda) == 0)
1970 events |= (1 << IPCT_LABEL);
1972 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1973 (1 << IPCT_ASSURED) |
1974 (1 << IPCT_HELPER) |
1975 (1 << IPCT_PROTOINFO) |
1976 (1 << IPCT_SEQADJ) |
1977 (1 << IPCT_MARK) | events,
1978 ct, NETLINK_CB(skb).portid,
1985 /* implicit 'else' */
1988 ct = nf_ct_tuplehash_to_ctrack(h);
1989 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1990 err = ctnetlink_change_conntrack(ct, cda);
1992 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1993 (1 << IPCT_ASSURED) |
1994 (1 << IPCT_HELPER) |
1996 (1 << IPCT_PROTOINFO) |
1997 (1 << IPCT_SEQADJ) |
1999 ct, NETLINK_CB(skb).portid,
2009 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2010 __u16 cpu, const struct ip_conntrack_stat *st)
2012 struct nlmsghdr *nlh;
2013 struct nfgenmsg *nfmsg;
2014 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2016 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2017 IPCTNL_MSG_CT_GET_STATS_CPU);
2018 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2022 nfmsg = nlmsg_data(nlh);
2023 nfmsg->nfgen_family = AF_UNSPEC;
2024 nfmsg->version = NFNETLINK_V0;
2025 nfmsg->res_id = htons(cpu);
2027 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2028 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2029 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2030 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2031 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2032 htonl(st->insert_failed)) ||
2033 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2034 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2035 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2036 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2037 htonl(st->search_restart)))
2038 goto nla_put_failure;
2040 nlmsg_end(skb, nlh);
2045 nlmsg_cancel(skb, nlh);
2050 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2053 struct net *net = sock_net(skb->sk);
2055 if (cb->args[0] == nr_cpu_ids)
2058 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2059 const struct ip_conntrack_stat *st;
2061 if (!cpu_possible(cpu))
2064 st = per_cpu_ptr(net->ct.stat, cpu);
2065 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2066 NETLINK_CB(cb->skb).portid,
2076 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2077 struct sk_buff *skb,
2078 const struct nlmsghdr *nlh,
2079 const struct nlattr * const cda[],
2080 struct netlink_ext_ack *extack)
2082 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2083 struct netlink_dump_control c = {
2084 .dump = ctnetlink_ct_stat_cpu_dump,
2086 return netlink_dump_start(ctnl, skb, nlh, &c);
2093 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2096 struct nlmsghdr *nlh;
2097 struct nfgenmsg *nfmsg;
2098 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2099 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2101 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2102 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2106 nfmsg = nlmsg_data(nlh);
2107 nfmsg->nfgen_family = AF_UNSPEC;
2108 nfmsg->version = NFNETLINK_V0;
2111 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2112 goto nla_put_failure;
2114 nlmsg_end(skb, nlh);
2119 nlmsg_cancel(skb, nlh);
2123 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2124 struct sk_buff *skb, const struct nlmsghdr *nlh,
2125 const struct nlattr * const cda[],
2126 struct netlink_ext_ack *extack)
2128 struct sk_buff *skb2;
2131 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2135 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2137 NFNL_MSG_TYPE(nlh->nlmsg_type),
2142 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2151 /* this avoids a loop in nfnetlink. */
2152 return err == -EAGAIN ? -ENOBUFS : err;
2155 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2156 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2157 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2158 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2159 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2160 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2161 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2162 .len = NF_CT_HELPER_NAME_LEN - 1 },
2163 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2164 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2165 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2166 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2167 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2170 static struct nf_conntrack_expect *
2171 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2172 struct nf_conntrack_helper *helper,
2173 struct nf_conntrack_tuple *tuple,
2174 struct nf_conntrack_tuple *mask);
2176 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2178 ctnetlink_glue_build_size(const struct nf_conn *ct)
2180 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2181 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2182 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2183 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2184 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2185 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2186 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2187 + nla_total_size(0) /* CTA_PROTOINFO */
2188 + nla_total_size(0) /* CTA_HELP */
2189 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2190 + ctnetlink_secctx_size(ct)
2191 #ifdef CONFIG_NF_NAT_NEEDED
2192 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2193 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2195 #ifdef CONFIG_NF_CONNTRACK_MARK
2196 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2198 #ifdef CONFIG_NF_CONNTRACK_ZONES
2199 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2201 + ctnetlink_proto_size(ct)
2205 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2206 enum ip_conntrack_info *ctinfo)
2208 return nf_ct_get(skb, ctinfo);
2211 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2213 const struct nf_conntrack_zone *zone;
2214 struct nlattr *nest_parms;
2217 zone = nf_ct_zone(ct);
2219 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2221 goto nla_put_failure;
2222 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2223 goto nla_put_failure;
2224 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2225 NF_CT_ZONE_DIR_ORIG) < 0)
2226 goto nla_put_failure;
2227 nla_nest_end(skb, nest_parms);
2229 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2231 goto nla_put_failure;
2232 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2233 goto nla_put_failure;
2234 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2235 NF_CT_ZONE_DIR_REPL) < 0)
2236 goto nla_put_failure;
2237 nla_nest_end(skb, nest_parms);
2239 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2240 NF_CT_DEFAULT_ZONE_DIR) < 0)
2241 goto nla_put_failure;
2243 if (ctnetlink_dump_id(skb, ct) < 0)
2244 goto nla_put_failure;
2246 if (ctnetlink_dump_status(skb, ct) < 0)
2247 goto nla_put_failure;
2249 if (ctnetlink_dump_timeout(skb, ct) < 0)
2250 goto nla_put_failure;
2252 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2253 goto nla_put_failure;
2255 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2256 goto nla_put_failure;
2258 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2259 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2260 goto nla_put_failure;
2262 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2263 goto nla_put_failure;
2265 if ((ct->status & IPS_SEQ_ADJUST) &&
2266 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2267 goto nla_put_failure;
2269 #ifdef CONFIG_NF_CONNTRACK_MARK
2270 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2271 goto nla_put_failure;
2273 if (ctnetlink_dump_labels(skb, ct) < 0)
2274 goto nla_put_failure;
2284 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2285 enum ip_conntrack_info ctinfo,
2286 u_int16_t ct_attr, u_int16_t ct_info_attr)
2288 struct nlattr *nest_parms;
2290 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2292 goto nla_put_failure;
2294 if (__ctnetlink_glue_build(skb, ct) < 0)
2295 goto nla_put_failure;
2297 nla_nest_end(skb, nest_parms);
2299 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2300 goto nla_put_failure;
2309 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2311 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2312 unsigned long d = ct->status ^ status;
2314 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2315 /* SEEN_REPLY bit can only be set */
2318 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2319 /* ASSURED bit can only be set */
2322 /* This check is less strict than ctnetlink_change_status()
2323 * because callers often flip IPS_EXPECTED bits when sending
2324 * an NFQA_CT attribute to the kernel. So ignore the
2325 * unchangeable bits but do not error out. Also user programs
2326 * are allowed to clear the bits that they are allowed to change.
2328 __ctnetlink_change_status(ct, status, ~status);
2333 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2337 if (cda[CTA_TIMEOUT]) {
2338 err = ctnetlink_change_timeout(ct, cda);
2342 if (cda[CTA_STATUS]) {
2343 err = ctnetlink_update_status(ct, cda);
2347 if (cda[CTA_HELP]) {
2348 err = ctnetlink_change_helper(ct, cda);
2352 if (cda[CTA_LABELS]) {
2353 err = ctnetlink_attach_labels(ct, cda);
2357 #if defined(CONFIG_NF_CONNTRACK_MARK)
2358 if (cda[CTA_MARK]) {
2359 u32 mask = 0, mark, newmark;
2360 if (cda[CTA_MARK_MASK])
2361 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2363 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2364 newmark = (ct->mark & mask) ^ mark;
2365 if (newmark != ct->mark)
2373 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2375 struct nlattr *cda[CTA_MAX+1];
2378 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2382 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2385 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2386 const struct nf_conn *ct,
2387 struct nf_conntrack_tuple *tuple,
2388 struct nf_conntrack_tuple *mask)
2392 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2393 nf_ct_l3num(ct), NULL);
2397 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2398 nf_ct_l3num(ct), NULL);
2402 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2403 u32 portid, u32 report)
2405 struct nlattr *cda[CTA_EXPECT_MAX+1];
2406 struct nf_conntrack_tuple tuple, mask;
2407 struct nf_conntrack_helper *helper = NULL;
2408 struct nf_conntrack_expect *exp;
2411 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2416 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2421 if (cda[CTA_EXPECT_HELP_NAME]) {
2422 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2424 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2425 nf_ct_protonum(ct));
2430 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2431 helper, &tuple, &mask);
2433 return PTR_ERR(exp);
2435 err = nf_ct_expect_related_report(exp, portid, report);
2436 nf_ct_expect_put(exp);
2440 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2441 enum ip_conntrack_info ctinfo, int diff)
2443 if (!(ct->status & IPS_NAT_MASK))
2446 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2449 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2450 .get_ct = ctnetlink_glue_get_ct,
2451 .build_size = ctnetlink_glue_build_size,
2452 .build = ctnetlink_glue_build,
2453 .parse = ctnetlink_glue_parse,
2454 .attach_expect = ctnetlink_glue_attach_expect,
2455 .seq_adjust = ctnetlink_glue_seqadj,
2457 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2459 /***********************************************************************
2461 ***********************************************************************/
2463 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2464 const struct nf_conntrack_tuple *tuple,
2467 struct nlattr *nest_parms;
2469 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2471 goto nla_put_failure;
2472 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2473 goto nla_put_failure;
2474 nla_nest_end(skb, nest_parms);
2482 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2483 const struct nf_conntrack_tuple *tuple,
2484 const struct nf_conntrack_tuple_mask *mask)
2487 struct nf_conntrack_l3proto *l3proto;
2488 struct nf_conntrack_l4proto *l4proto;
2489 struct nf_conntrack_tuple m;
2490 struct nlattr *nest_parms;
2492 memset(&m, 0xFF, sizeof(m));
2493 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2494 m.src.u.all = mask->src.u.all;
2495 m.dst.protonum = tuple->dst.protonum;
2497 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2499 goto nla_put_failure;
2502 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2503 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2505 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2506 tuple->dst.protonum);
2507 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2511 if (unlikely(ret < 0))
2512 goto nla_put_failure;
2514 nla_nest_end(skb, nest_parms);
2522 static const union nf_inet_addr any_addr;
2525 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2526 const struct nf_conntrack_expect *exp)
2528 struct nf_conn *master = exp->master;
2529 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2530 struct nf_conn_help *help;
2531 #ifdef CONFIG_NF_NAT_NEEDED
2532 struct nlattr *nest_parms;
2533 struct nf_conntrack_tuple nat_tuple = {};
2535 struct nf_ct_helper_expectfn *expfn;
2540 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2541 goto nla_put_failure;
2542 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2543 goto nla_put_failure;
2544 if (ctnetlink_exp_dump_tuple(skb,
2545 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2546 CTA_EXPECT_MASTER) < 0)
2547 goto nla_put_failure;
2549 #ifdef CONFIG_NF_NAT_NEEDED
2550 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2551 exp->saved_proto.all) {
2552 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2554 goto nla_put_failure;
2556 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2557 goto nla_put_failure;
2559 nat_tuple.src.l3num = nf_ct_l3num(master);
2560 nat_tuple.src.u3 = exp->saved_addr;
2561 nat_tuple.dst.protonum = nf_ct_protonum(master);
2562 nat_tuple.src.u = exp->saved_proto;
2564 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2565 CTA_EXPECT_NAT_TUPLE) < 0)
2566 goto nla_put_failure;
2567 nla_nest_end(skb, nest_parms);
2570 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2571 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2572 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2573 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2574 goto nla_put_failure;
2575 help = nfct_help(master);
2577 struct nf_conntrack_helper *helper;
2579 helper = rcu_dereference(help->helper);
2581 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2582 goto nla_put_failure;
2584 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2585 if (expfn != NULL &&
2586 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2587 goto nla_put_failure;
2596 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2597 int event, const struct nf_conntrack_expect *exp)
2599 struct nlmsghdr *nlh;
2600 struct nfgenmsg *nfmsg;
2601 unsigned int flags = portid ? NLM_F_MULTI : 0;
2603 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2604 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2608 nfmsg = nlmsg_data(nlh);
2609 nfmsg->nfgen_family = exp->tuple.src.l3num;
2610 nfmsg->version = NFNETLINK_V0;
2613 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2614 goto nla_put_failure;
2616 nlmsg_end(skb, nlh);
2621 nlmsg_cancel(skb, nlh);
2625 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2627 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2629 struct nf_conntrack_expect *exp = item->exp;
2630 struct net *net = nf_ct_exp_net(exp);
2631 struct nlmsghdr *nlh;
2632 struct nfgenmsg *nfmsg;
2633 struct sk_buff *skb;
2634 unsigned int type, group;
2637 if (events & (1 << IPEXP_DESTROY)) {
2638 type = IPCTNL_MSG_EXP_DELETE;
2639 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2640 } else if (events & (1 << IPEXP_NEW)) {
2641 type = IPCTNL_MSG_EXP_NEW;
2642 flags = NLM_F_CREATE|NLM_F_EXCL;
2643 group = NFNLGRP_CONNTRACK_EXP_NEW;
2647 if (!item->report && !nfnetlink_has_listeners(net, group))
2650 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2654 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2655 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2659 nfmsg = nlmsg_data(nlh);
2660 nfmsg->nfgen_family = exp->tuple.src.l3num;
2661 nfmsg->version = NFNETLINK_V0;
2665 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2666 goto nla_put_failure;
2669 nlmsg_end(skb, nlh);
2670 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2675 nlmsg_cancel(skb, nlh);
2679 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2683 static int ctnetlink_exp_done(struct netlink_callback *cb)
2686 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2691 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2693 struct net *net = sock_net(skb->sk);
2694 struct nf_conntrack_expect *exp, *last;
2695 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2696 u_int8_t l3proto = nfmsg->nfgen_family;
2699 last = (struct nf_conntrack_expect *)cb->args[1];
2700 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2702 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2704 if (l3proto && exp->tuple.src.l3num != l3proto)
2707 if (!net_eq(nf_ct_net(exp->master), net))
2715 if (ctnetlink_exp_fill_info(skb,
2716 NETLINK_CB(cb->skb).portid,
2720 if (!refcount_inc_not_zero(&exp->use))
2722 cb->args[1] = (unsigned long)exp;
2734 nf_ct_expect_put(last);
2740 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2742 struct nf_conntrack_expect *exp, *last;
2743 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2744 struct nf_conn *ct = cb->data;
2745 struct nf_conn_help *help = nfct_help(ct);
2746 u_int8_t l3proto = nfmsg->nfgen_family;
2752 last = (struct nf_conntrack_expect *)cb->args[1];
2754 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2755 if (l3proto && exp->tuple.src.l3num != l3proto)
2762 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2766 if (!refcount_inc_not_zero(&exp->use))
2768 cb->args[1] = (unsigned long)exp;
2780 nf_ct_expect_put(last);
2785 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2786 struct sk_buff *skb,
2787 const struct nlmsghdr *nlh,
2788 const struct nlattr * const cda[],
2789 struct netlink_ext_ack *extack)
2792 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2793 u_int8_t u3 = nfmsg->nfgen_family;
2794 struct nf_conntrack_tuple tuple;
2795 struct nf_conntrack_tuple_hash *h;
2797 struct nf_conntrack_zone zone;
2798 struct netlink_dump_control c = {
2799 .dump = ctnetlink_exp_ct_dump_table,
2800 .done = ctnetlink_exp_done,
2803 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2808 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2812 h = nf_conntrack_find_get(net, &zone, &tuple);
2816 ct = nf_ct_tuplehash_to_ctrack(h);
2817 /* No expectation linked to this connection tracking. */
2818 if (!nfct_help(ct)) {
2825 err = netlink_dump_start(ctnl, skb, nlh, &c);
2831 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2832 struct sk_buff *skb, const struct nlmsghdr *nlh,
2833 const struct nlattr * const cda[],
2834 struct netlink_ext_ack *extack)
2836 struct nf_conntrack_tuple tuple;
2837 struct nf_conntrack_expect *exp;
2838 struct sk_buff *skb2;
2839 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2840 u_int8_t u3 = nfmsg->nfgen_family;
2841 struct nf_conntrack_zone zone;
2844 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2845 if (cda[CTA_EXPECT_MASTER])
2846 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda,
2849 struct netlink_dump_control c = {
2850 .dump = ctnetlink_exp_dump_table,
2851 .done = ctnetlink_exp_done,
2853 return netlink_dump_start(ctnl, skb, nlh, &c);
2857 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2861 if (cda[CTA_EXPECT_TUPLE])
2862 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2864 else if (cda[CTA_EXPECT_MASTER])
2865 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2873 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2877 if (cda[CTA_EXPECT_ID]) {
2878 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2879 if (ntohl(id) != (u32)(unsigned long)exp) {
2880 nf_ct_expect_put(exp);
2886 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2888 nf_ct_expect_put(exp);
2893 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2894 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2896 nf_ct_expect_put(exp);
2900 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2909 /* this avoids a loop in nfnetlink. */
2910 return err == -EAGAIN ? -ENOBUFS : err;
2913 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2914 struct sk_buff *skb, const struct nlmsghdr *nlh,
2915 const struct nlattr * const cda[],
2916 struct netlink_ext_ack *extack)
2918 struct nf_conntrack_expect *exp;
2919 struct nf_conntrack_tuple tuple;
2920 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2921 struct hlist_node *next;
2922 u_int8_t u3 = nfmsg->nfgen_family;
2923 struct nf_conntrack_zone zone;
2927 if (cda[CTA_EXPECT_TUPLE]) {
2928 /* delete a single expect by tuple */
2929 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2933 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2938 /* bump usage count to 2 */
2939 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2943 if (cda[CTA_EXPECT_ID]) {
2944 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2945 if (ntohl(id) != (u32)(unsigned long)exp) {
2946 nf_ct_expect_put(exp);
2951 /* after list removal, usage count == 1 */
2952 spin_lock_bh(&nf_conntrack_expect_lock);
2953 if (del_timer(&exp->timeout)) {
2954 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2956 nf_ct_expect_put(exp);
2958 spin_unlock_bh(&nf_conntrack_expect_lock);
2959 /* have to put what we 'get' above.
2960 * after this line usage count == 0 */
2961 nf_ct_expect_put(exp);
2962 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2963 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2964 struct nf_conn_help *m_help;
2966 /* delete all expectations for this helper */
2967 spin_lock_bh(&nf_conntrack_expect_lock);
2968 for (i = 0; i < nf_ct_expect_hsize; i++) {
2969 hlist_for_each_entry_safe(exp, next,
2970 &nf_ct_expect_hash[i],
2973 if (!net_eq(nf_ct_exp_net(exp), net))
2976 m_help = nfct_help(exp->master);
2977 if (!strcmp(m_help->helper->name, name) &&
2978 del_timer(&exp->timeout)) {
2979 nf_ct_unlink_expect_report(exp,
2980 NETLINK_CB(skb).portid,
2982 nf_ct_expect_put(exp);
2986 spin_unlock_bh(&nf_conntrack_expect_lock);
2988 /* This basically means we have to flush everything*/
2989 spin_lock_bh(&nf_conntrack_expect_lock);
2990 for (i = 0; i < nf_ct_expect_hsize; i++) {
2991 hlist_for_each_entry_safe(exp, next,
2992 &nf_ct_expect_hash[i],
2995 if (!net_eq(nf_ct_exp_net(exp), net))
2998 if (del_timer(&exp->timeout)) {
2999 nf_ct_unlink_expect_report(exp,
3000 NETLINK_CB(skb).portid,
3002 nf_ct_expect_put(exp);
3006 spin_unlock_bh(&nf_conntrack_expect_lock);
3012 ctnetlink_change_expect(struct nf_conntrack_expect *x,
3013 const struct nlattr * const cda[])
3015 if (cda[CTA_EXPECT_TIMEOUT]) {
3016 if (!del_timer(&x->timeout))
3019 x->timeout.expires = jiffies +
3020 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3021 add_timer(&x->timeout);
3026 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3027 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3028 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3032 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3033 struct nf_conntrack_expect *exp,
3036 #ifdef CONFIG_NF_NAT_NEEDED
3037 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3038 struct nf_conntrack_tuple nat_tuple = {};
3041 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3042 exp_nat_nla_policy, NULL);
3046 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3049 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3050 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3055 exp->saved_addr = nat_tuple.src.u3;
3056 exp->saved_proto = nat_tuple.src.u;
3057 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3065 static struct nf_conntrack_expect *
3066 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3067 struct nf_conntrack_helper *helper,
3068 struct nf_conntrack_tuple *tuple,
3069 struct nf_conntrack_tuple *mask)
3071 u_int32_t class = 0;
3072 struct nf_conntrack_expect *exp;
3073 struct nf_conn_help *help;
3076 help = nfct_help(ct);
3078 return ERR_PTR(-EOPNOTSUPP);
3080 if (cda[CTA_EXPECT_CLASS] && helper) {
3081 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3082 if (class > helper->expect_class_max)
3083 return ERR_PTR(-EINVAL);
3085 exp = nf_ct_expect_alloc(ct);
3087 return ERR_PTR(-ENOMEM);
3089 if (cda[CTA_EXPECT_FLAGS]) {
3090 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3091 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3095 if (cda[CTA_EXPECT_FN]) {
3096 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3097 struct nf_ct_helper_expectfn *expfn;
3099 expfn = nf_ct_helper_expectfn_find_by_name(name);
3100 if (expfn == NULL) {
3104 exp->expectfn = expfn->expectfn;
3106 exp->expectfn = NULL;
3110 exp->helper = helper;
3111 exp->tuple = *tuple;
3112 exp->mask.src.u3 = mask->src.u3;
3113 exp->mask.src.u.all = mask->src.u.all;
3115 if (cda[CTA_EXPECT_NAT]) {
3116 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3117 exp, nf_ct_l3num(ct));
3123 nf_ct_expect_put(exp);
3124 return ERR_PTR(err);
3128 ctnetlink_create_expect(struct net *net,
3129 const struct nf_conntrack_zone *zone,
3130 const struct nlattr * const cda[],
3131 u_int8_t u3, u32 portid, int report)
3133 struct nf_conntrack_tuple tuple, mask, master_tuple;
3134 struct nf_conntrack_tuple_hash *h = NULL;
3135 struct nf_conntrack_helper *helper = NULL;
3136 struct nf_conntrack_expect *exp;
3140 /* caller guarantees that those three CTA_EXPECT_* exist */
3141 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3145 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3149 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3154 /* Look for master conntrack of this expectation */
3155 h = nf_conntrack_find_get(net, zone, &master_tuple);
3158 ct = nf_ct_tuplehash_to_ctrack(h);
3161 if (cda[CTA_EXPECT_HELP_NAME]) {
3162 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3164 helper = __nf_conntrack_helper_find(helpname, u3,
3165 nf_ct_protonum(ct));
3166 if (helper == NULL) {
3168 #ifdef CONFIG_MODULES
3169 if (request_module("nfct-helper-%s", helpname) < 0) {
3174 helper = __nf_conntrack_helper_find(helpname, u3,
3175 nf_ct_protonum(ct));
3187 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3193 err = nf_ct_expect_related_report(exp, portid, report);
3194 nf_ct_expect_put(exp);
3202 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3203 struct sk_buff *skb, const struct nlmsghdr *nlh,
3204 const struct nlattr * const cda[],
3205 struct netlink_ext_ack *extack)
3207 struct nf_conntrack_tuple tuple;
3208 struct nf_conntrack_expect *exp;
3209 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3210 u_int8_t u3 = nfmsg->nfgen_family;
3211 struct nf_conntrack_zone zone;
3214 if (!cda[CTA_EXPECT_TUPLE]
3215 || !cda[CTA_EXPECT_MASK]
3216 || !cda[CTA_EXPECT_MASTER])
3219 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3223 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3228 spin_lock_bh(&nf_conntrack_expect_lock);
3229 exp = __nf_ct_expect_find(net, &zone, &tuple);
3231 spin_unlock_bh(&nf_conntrack_expect_lock);
3233 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3234 err = ctnetlink_create_expect(net, &zone, cda, u3,
3235 NETLINK_CB(skb).portid,
3242 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3243 err = ctnetlink_change_expect(exp, cda);
3244 spin_unlock_bh(&nf_conntrack_expect_lock);
3250 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3251 const struct ip_conntrack_stat *st)
3253 struct nlmsghdr *nlh;
3254 struct nfgenmsg *nfmsg;
3255 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3257 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3258 IPCTNL_MSG_EXP_GET_STATS_CPU);
3259 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3263 nfmsg = nlmsg_data(nlh);
3264 nfmsg->nfgen_family = AF_UNSPEC;
3265 nfmsg->version = NFNETLINK_V0;
3266 nfmsg->res_id = htons(cpu);
3268 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3269 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3270 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3271 goto nla_put_failure;
3273 nlmsg_end(skb, nlh);
3278 nlmsg_cancel(skb, nlh);
3283 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3286 struct net *net = sock_net(skb->sk);
3288 if (cb->args[0] == nr_cpu_ids)
3291 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3292 const struct ip_conntrack_stat *st;
3294 if (!cpu_possible(cpu))
3297 st = per_cpu_ptr(net->ct.stat, cpu);
3298 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3308 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3309 struct sk_buff *skb,
3310 const struct nlmsghdr *nlh,
3311 const struct nlattr * const cda[],
3312 struct netlink_ext_ack *extack)
3314 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3315 struct netlink_dump_control c = {
3316 .dump = ctnetlink_exp_stat_cpu_dump,
3318 return netlink_dump_start(ctnl, skb, nlh, &c);
3324 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3325 static struct nf_ct_event_notifier ctnl_notifier = {
3326 .fcn = ctnetlink_conntrack_event,
3329 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3330 .fcn = ctnetlink_expect_event,
3334 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3335 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3336 .attr_count = CTA_MAX,
3337 .policy = ct_nla_policy },
3338 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3339 .attr_count = CTA_MAX,
3340 .policy = ct_nla_policy },
3341 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3342 .attr_count = CTA_MAX,
3343 .policy = ct_nla_policy },
3344 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3345 .attr_count = CTA_MAX,
3346 .policy = ct_nla_policy },
3347 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3348 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3349 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3350 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3353 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3354 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3355 .attr_count = CTA_EXPECT_MAX,
3356 .policy = exp_nla_policy },
3357 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3358 .attr_count = CTA_EXPECT_MAX,
3359 .policy = exp_nla_policy },
3360 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3361 .attr_count = CTA_EXPECT_MAX,
3362 .policy = exp_nla_policy },
3363 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3366 static const struct nfnetlink_subsystem ctnl_subsys = {
3367 .name = "conntrack",
3368 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3369 .cb_count = IPCTNL_MSG_MAX,
3373 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3374 .name = "conntrack_expect",
3375 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3376 .cb_count = IPCTNL_MSG_EXP_MAX,
3380 MODULE_ALIAS("ip_conntrack_netlink");
3381 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3382 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3384 static int __net_init ctnetlink_net_init(struct net *net)
3386 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3389 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3391 pr_err("ctnetlink_init: cannot register notifier.\n");
3395 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3397 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3398 goto err_unreg_notifier;
3403 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3405 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3411 static void ctnetlink_net_exit(struct net *net)
3413 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3414 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3415 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3419 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3423 list_for_each_entry(net, net_exit_list, exit_list)
3424 ctnetlink_net_exit(net);
3427 static struct pernet_operations ctnetlink_net_ops = {
3428 .init = ctnetlink_net_init,
3429 .exit_batch = ctnetlink_net_exit_batch,
3432 static int __init ctnetlink_init(void)
3436 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3437 ret = nfnetlink_subsys_register(&ctnl_subsys);
3439 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3443 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3445 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3446 goto err_unreg_subsys;
3449 ret = register_pernet_subsys(&ctnetlink_net_ops);
3451 pr_err("ctnetlink_init: cannot register pernet operations\n");
3452 goto err_unreg_exp_subsys;
3454 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3455 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3456 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3460 err_unreg_exp_subsys:
3461 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3463 nfnetlink_subsys_unregister(&ctnl_subsys);
3468 static void __exit ctnetlink_exit(void)
3470 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3472 unregister_pernet_subsys(&ctnetlink_net_ops);
3473 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3474 nfnetlink_subsys_unregister(&ctnl_subsys);
3475 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3476 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3481 module_init(ctnetlink_init);
3482 module_exit(ctnetlink_exit);