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;
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 struct nlattr *tb[CTA_PROTO_MAX+1];
940 struct nf_conntrack_l4proto *l4proto;
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, 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[])
1132 struct nf_conntrack_tuple_hash *h;
1133 struct nf_conntrack_tuple tuple;
1135 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1136 u_int8_t u3 = nfmsg->nfgen_family;
1137 struct nf_conntrack_zone zone;
1140 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1144 if (cda[CTA_TUPLE_ORIG])
1145 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1147 else if (cda[CTA_TUPLE_REPLY])
1148 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1151 return ctnetlink_flush_conntrack(net, cda,
1152 NETLINK_CB(skb).portid,
1159 h = nf_conntrack_find_get(net, &zone, &tuple);
1163 ct = nf_ct_tuplehash_to_ctrack(h);
1166 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1167 if (id != (u32)(unsigned long)ct) {
1173 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1179 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1180 struct sk_buff *skb,
1181 const struct nlmsghdr *nlh,
1182 const struct nlattr * const cda[])
1184 struct nf_conntrack_tuple_hash *h;
1185 struct nf_conntrack_tuple tuple;
1187 struct sk_buff *skb2 = NULL;
1188 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1189 u_int8_t u3 = nfmsg->nfgen_family;
1190 struct nf_conntrack_zone zone;
1193 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1194 struct netlink_dump_control c = {
1195 .dump = ctnetlink_dump_table,
1196 .done = ctnetlink_done,
1199 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1200 struct ctnetlink_filter *filter;
1202 filter = ctnetlink_alloc_filter(cda);
1204 return PTR_ERR(filter);
1208 return netlink_dump_start(ctnl, skb, nlh, &c);
1211 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1215 if (cda[CTA_TUPLE_ORIG])
1216 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1218 else if (cda[CTA_TUPLE_REPLY])
1219 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1227 h = nf_conntrack_find_get(net, &zone, &tuple);
1231 ct = nf_ct_tuplehash_to_ctrack(h);
1234 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1241 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1242 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1248 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1257 /* this avoids a loop in nfnetlink. */
1258 return err == -EAGAIN ? -ENOBUFS : err;
1261 static int ctnetlink_done_list(struct netlink_callback *cb)
1264 nf_ct_put((struct nf_conn *)cb->args[1]);
1269 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1271 struct nf_conn *ct, *last;
1272 struct nf_conntrack_tuple_hash *h;
1273 struct hlist_nulls_node *n;
1274 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1275 u_int8_t l3proto = nfmsg->nfgen_family;
1278 struct hlist_nulls_head *list;
1279 struct net *net = sock_net(skb->sk);
1284 last = (struct nf_conn *)cb->args[1];
1286 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1287 struct ct_pcpu *pcpu;
1289 if (!cpu_possible(cpu))
1292 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1293 spin_lock_bh(&pcpu->lock);
1294 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1296 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1297 ct = nf_ct_tuplehash_to_ctrack(h);
1298 if (l3proto && nf_ct_l3num(ct) != l3proto)
1306 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1308 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1312 if (!atomic_inc_not_zero(&ct->ct_general.use))
1315 cb->args[1] = (unsigned long)ct;
1316 spin_unlock_bh(&pcpu->lock);
1324 spin_unlock_bh(&pcpu->lock);
1335 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1337 return ctnetlink_dump_list(skb, cb, true);
1340 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1341 struct sk_buff *skb,
1342 const struct nlmsghdr *nlh,
1343 const struct nlattr * const cda[])
1345 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1346 struct netlink_dump_control c = {
1347 .dump = ctnetlink_dump_dying,
1348 .done = ctnetlink_done_list,
1350 return netlink_dump_start(ctnl, skb, nlh, &c);
1357 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1359 return ctnetlink_dump_list(skb, cb, false);
1362 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1363 struct sk_buff *skb,
1364 const struct nlmsghdr *nlh,
1365 const struct nlattr * const cda[])
1367 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1368 struct netlink_dump_control c = {
1369 .dump = ctnetlink_dump_unconfirmed,
1370 .done = ctnetlink_done_list,
1372 return netlink_dump_start(ctnl, skb, nlh, &c);
1378 #ifdef CONFIG_NF_NAT_NEEDED
1380 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1381 enum nf_nat_manip_type manip,
1382 const struct nlattr *attr)
1384 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1387 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1388 if (!parse_nat_setup) {
1389 #ifdef CONFIG_MODULES
1391 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1392 if (request_module("nf-nat") < 0) {
1393 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1397 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1399 if (nfnetlink_parse_nat_setup_hook)
1405 err = parse_nat_setup(ct, manip, attr);
1406 if (err == -EAGAIN) {
1407 #ifdef CONFIG_MODULES
1409 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1410 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1411 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1415 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1426 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1431 /* Ignore these unchangable bits */
1432 on &= ~IPS_UNCHANGEABLE_MASK;
1433 off &= ~IPS_UNCHANGEABLE_MASK;
1435 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1436 if (on & (1 << bit))
1437 set_bit(bit, &ct->status);
1438 else if (off & (1 << bit))
1439 clear_bit(bit, &ct->status);
1444 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1447 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1448 d = ct->status ^ status;
1450 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1454 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1455 /* SEEN_REPLY bit can only be set */
1458 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1459 /* ASSURED bit can only be set */
1462 __ctnetlink_change_status(ct, status, 0);
1467 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1469 #ifdef CONFIG_NF_NAT_NEEDED
1472 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1475 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1480 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1484 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1490 static int ctnetlink_change_helper(struct nf_conn *ct,
1491 const struct nlattr * const cda[])
1493 struct nf_conntrack_helper *helper;
1494 struct nf_conn_help *help = nfct_help(ct);
1495 char *helpname = NULL;
1496 struct nlattr *helpinfo = NULL;
1499 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1503 /* don't change helper of sibling connections */
1505 /* If we try to change the helper to the same thing twice,
1506 * treat the second attempt as a no-op instead of returning
1512 helper = rcu_dereference(help->helper);
1513 if (helper && !strcmp(helper->name, helpname))
1521 if (!strcmp(helpname, "")) {
1522 if (help && help->helper) {
1523 /* we had a helper before ... */
1524 nf_ct_remove_expectations(ct);
1525 RCU_INIT_POINTER(help->helper, NULL);
1532 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1533 nf_ct_protonum(ct));
1534 if (helper == NULL) {
1540 if (help->helper == helper) {
1541 /* update private helper data if allowed. */
1542 if (helper->from_nlattr)
1543 helper->from_nlattr(helpinfo, ct);
1548 /* we cannot set a helper for an existing conntrack */
1556 static int ctnetlink_change_timeout(struct nf_conn *ct,
1557 const struct nlattr * const cda[])
1559 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1561 ct->timeout = nfct_time_stamp + timeout * HZ;
1563 if (test_bit(IPS_DYING_BIT, &ct->status))
1569 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1570 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1571 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1572 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1575 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1576 const struct nlattr * const cda[])
1578 const struct nlattr *attr = cda[CTA_PROTOINFO];
1579 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1580 struct nf_conntrack_l4proto *l4proto;
1583 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1589 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1590 if (l4proto->from_nlattr)
1591 err = l4proto->from_nlattr(tb, ct);
1597 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1598 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1599 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1600 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1603 static int change_seq_adj(struct nf_ct_seqadj *seq,
1604 const struct nlattr * const attr)
1607 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1609 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1613 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1616 seq->correction_pos =
1617 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1619 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1622 seq->offset_before =
1623 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1625 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1629 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1635 ctnetlink_change_seq_adj(struct nf_conn *ct,
1636 const struct nlattr * const cda[])
1638 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1644 spin_lock_bh(&ct->lock);
1645 if (cda[CTA_SEQ_ADJ_ORIG]) {
1646 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1647 cda[CTA_SEQ_ADJ_ORIG]);
1651 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1654 if (cda[CTA_SEQ_ADJ_REPLY]) {
1655 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1656 cda[CTA_SEQ_ADJ_REPLY]);
1660 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1663 spin_unlock_bh(&ct->lock);
1666 spin_unlock_bh(&ct->lock);
1671 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1673 #ifdef CONFIG_NF_CONNTRACK_LABELS
1674 size_t len = nla_len(cda[CTA_LABELS]);
1675 const void *mask = cda[CTA_LABELS_MASK];
1677 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1681 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1682 nla_len(cda[CTA_LABELS_MASK]) != len)
1684 mask = nla_data(cda[CTA_LABELS_MASK]);
1689 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1696 ctnetlink_change_conntrack(struct nf_conn *ct,
1697 const struct nlattr * const cda[])
1701 /* only allow NAT changes and master assignation for new conntracks */
1702 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1705 if (cda[CTA_HELP]) {
1706 err = ctnetlink_change_helper(ct, cda);
1711 if (cda[CTA_TIMEOUT]) {
1712 err = ctnetlink_change_timeout(ct, cda);
1717 if (cda[CTA_STATUS]) {
1718 err = ctnetlink_change_status(ct, cda);
1723 if (cda[CTA_PROTOINFO]) {
1724 err = ctnetlink_change_protoinfo(ct, cda);
1729 #if defined(CONFIG_NF_CONNTRACK_MARK)
1731 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1734 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1735 err = ctnetlink_change_seq_adj(ct, cda);
1740 if (cda[CTA_LABELS]) {
1741 err = ctnetlink_attach_labels(ct, cda);
1749 static struct nf_conn *
1750 ctnetlink_create_conntrack(struct net *net,
1751 const struct nf_conntrack_zone *zone,
1752 const struct nlattr * const cda[],
1753 struct nf_conntrack_tuple *otuple,
1754 struct nf_conntrack_tuple *rtuple,
1759 struct nf_conntrack_helper *helper;
1760 struct nf_conn_tstamp *tstamp;
1762 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1764 return ERR_PTR(-ENOMEM);
1766 if (!cda[CTA_TIMEOUT])
1769 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1772 if (cda[CTA_HELP]) {
1773 char *helpname = NULL;
1774 struct nlattr *helpinfo = NULL;
1776 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1780 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1781 nf_ct_protonum(ct));
1782 if (helper == NULL) {
1784 #ifdef CONFIG_MODULES
1785 if (request_module("nfct-helper-%s", helpname) < 0) {
1791 helper = __nf_conntrack_helper_find(helpname,
1793 nf_ct_protonum(ct));
1803 struct nf_conn_help *help;
1805 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1810 /* set private helper data if allowed. */
1811 if (helper->from_nlattr)
1812 helper->from_nlattr(helpinfo, ct);
1814 /* not in hash table yet so not strictly necessary */
1815 RCU_INIT_POINTER(help->helper, helper);
1818 /* try an implicit helper assignation */
1819 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1824 err = ctnetlink_setup_nat(ct, cda);
1828 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1829 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1830 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1831 nf_ct_labels_ext_add(ct);
1832 nfct_seqadj_ext_add(ct);
1833 nfct_synproxy_ext_add(ct);
1835 /* we must add conntrack extensions before confirmation. */
1836 ct->status |= IPS_CONFIRMED;
1838 if (cda[CTA_STATUS]) {
1839 err = ctnetlink_change_status(ct, cda);
1844 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1845 err = ctnetlink_change_seq_adj(ct, cda);
1850 memset(&ct->proto, 0, sizeof(ct->proto));
1851 if (cda[CTA_PROTOINFO]) {
1852 err = ctnetlink_change_protoinfo(ct, cda);
1857 #if defined(CONFIG_NF_CONNTRACK_MARK)
1859 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1862 /* setup master conntrack: this is a confirmed expectation */
1863 if (cda[CTA_TUPLE_MASTER]) {
1864 struct nf_conntrack_tuple master;
1865 struct nf_conntrack_tuple_hash *master_h;
1866 struct nf_conn *master_ct;
1868 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1873 master_h = nf_conntrack_find_get(net, zone, &master);
1874 if (master_h == NULL) {
1878 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1879 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1880 ct->master = master_ct;
1882 tstamp = nf_conn_tstamp_find(ct);
1884 tstamp->start = ktime_get_real_ns();
1886 err = nf_conntrack_hash_check_insert(ct);
1897 nf_conntrack_free(ct);
1898 return ERR_PTR(err);
1901 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1902 struct sk_buff *skb,
1903 const struct nlmsghdr *nlh,
1904 const struct nlattr * const cda[])
1906 struct nf_conntrack_tuple otuple, rtuple;
1907 struct nf_conntrack_tuple_hash *h = NULL;
1908 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1910 u_int8_t u3 = nfmsg->nfgen_family;
1911 struct nf_conntrack_zone zone;
1914 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1918 if (cda[CTA_TUPLE_ORIG]) {
1919 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1925 if (cda[CTA_TUPLE_REPLY]) {
1926 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1932 if (cda[CTA_TUPLE_ORIG])
1933 h = nf_conntrack_find_get(net, &zone, &otuple);
1934 else if (cda[CTA_TUPLE_REPLY])
1935 h = nf_conntrack_find_get(net, &zone, &rtuple);
1939 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1940 enum ip_conntrack_events events;
1942 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1944 if (otuple.dst.protonum != rtuple.dst.protonum)
1947 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1953 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1954 events = 1 << IPCT_RELATED;
1956 events = 1 << IPCT_NEW;
1958 if (cda[CTA_LABELS] &&
1959 ctnetlink_attach_labels(ct, cda) == 0)
1960 events |= (1 << IPCT_LABEL);
1962 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1963 (1 << IPCT_ASSURED) |
1964 (1 << IPCT_HELPER) |
1965 (1 << IPCT_PROTOINFO) |
1966 (1 << IPCT_SEQADJ) |
1967 (1 << IPCT_MARK) | events,
1968 ct, NETLINK_CB(skb).portid,
1975 /* implicit 'else' */
1978 ct = nf_ct_tuplehash_to_ctrack(h);
1979 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1980 err = ctnetlink_change_conntrack(ct, cda);
1982 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1983 (1 << IPCT_ASSURED) |
1984 (1 << IPCT_HELPER) |
1986 (1 << IPCT_PROTOINFO) |
1987 (1 << IPCT_SEQADJ) |
1989 ct, NETLINK_CB(skb).portid,
1999 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2000 __u16 cpu, const struct ip_conntrack_stat *st)
2002 struct nlmsghdr *nlh;
2003 struct nfgenmsg *nfmsg;
2004 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2006 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2007 IPCTNL_MSG_CT_GET_STATS_CPU);
2008 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2012 nfmsg = nlmsg_data(nlh);
2013 nfmsg->nfgen_family = AF_UNSPEC;
2014 nfmsg->version = NFNETLINK_V0;
2015 nfmsg->res_id = htons(cpu);
2017 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2018 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2019 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2020 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2021 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2022 htonl(st->insert_failed)) ||
2023 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2024 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2025 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2026 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2027 htonl(st->search_restart)))
2028 goto nla_put_failure;
2030 nlmsg_end(skb, nlh);
2035 nlmsg_cancel(skb, nlh);
2040 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2043 struct net *net = sock_net(skb->sk);
2045 if (cb->args[0] == nr_cpu_ids)
2048 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2049 const struct ip_conntrack_stat *st;
2051 if (!cpu_possible(cpu))
2054 st = per_cpu_ptr(net->ct.stat, cpu);
2055 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2056 NETLINK_CB(cb->skb).portid,
2066 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2067 struct sk_buff *skb,
2068 const struct nlmsghdr *nlh,
2069 const struct nlattr * const cda[])
2071 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2072 struct netlink_dump_control c = {
2073 .dump = ctnetlink_ct_stat_cpu_dump,
2075 return netlink_dump_start(ctnl, skb, nlh, &c);
2082 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2085 struct nlmsghdr *nlh;
2086 struct nfgenmsg *nfmsg;
2087 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2088 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2090 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2091 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2095 nfmsg = nlmsg_data(nlh);
2096 nfmsg->nfgen_family = AF_UNSPEC;
2097 nfmsg->version = NFNETLINK_V0;
2100 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2101 goto nla_put_failure;
2103 nlmsg_end(skb, nlh);
2108 nlmsg_cancel(skb, nlh);
2112 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2113 struct sk_buff *skb, const struct nlmsghdr *nlh,
2114 const struct nlattr * const cda[])
2116 struct sk_buff *skb2;
2119 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2123 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2125 NFNL_MSG_TYPE(nlh->nlmsg_type),
2130 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2139 /* this avoids a loop in nfnetlink. */
2140 return err == -EAGAIN ? -ENOBUFS : err;
2143 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2144 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2145 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2146 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2147 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2148 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2149 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2150 .len = NF_CT_HELPER_NAME_LEN - 1 },
2151 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2152 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2153 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2154 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2155 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2158 static struct nf_conntrack_expect *
2159 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2160 struct nf_conntrack_helper *helper,
2161 struct nf_conntrack_tuple *tuple,
2162 struct nf_conntrack_tuple *mask);
2164 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2166 ctnetlink_glue_build_size(const struct nf_conn *ct)
2168 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2169 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2170 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2171 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2172 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2173 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2174 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2175 + nla_total_size(0) /* CTA_PROTOINFO */
2176 + nla_total_size(0) /* CTA_HELP */
2177 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2178 + ctnetlink_secctx_size(ct)
2179 #ifdef CONFIG_NF_NAT_NEEDED
2180 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2181 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2183 #ifdef CONFIG_NF_CONNTRACK_MARK
2184 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2186 #ifdef CONFIG_NF_CONNTRACK_ZONES
2187 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2189 + ctnetlink_proto_size(ct)
2193 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2194 enum ip_conntrack_info *ctinfo)
2196 return nf_ct_get(skb, ctinfo);
2199 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2201 const struct nf_conntrack_zone *zone;
2202 struct nlattr *nest_parms;
2205 zone = nf_ct_zone(ct);
2207 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2209 goto nla_put_failure;
2210 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2211 goto nla_put_failure;
2212 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2213 NF_CT_ZONE_DIR_ORIG) < 0)
2214 goto nla_put_failure;
2215 nla_nest_end(skb, nest_parms);
2217 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2219 goto nla_put_failure;
2220 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2221 goto nla_put_failure;
2222 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2223 NF_CT_ZONE_DIR_REPL) < 0)
2224 goto nla_put_failure;
2225 nla_nest_end(skb, nest_parms);
2227 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2228 NF_CT_DEFAULT_ZONE_DIR) < 0)
2229 goto nla_put_failure;
2231 if (ctnetlink_dump_id(skb, ct) < 0)
2232 goto nla_put_failure;
2234 if (ctnetlink_dump_status(skb, ct) < 0)
2235 goto nla_put_failure;
2237 if (ctnetlink_dump_timeout(skb, ct) < 0)
2238 goto nla_put_failure;
2240 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2241 goto nla_put_failure;
2243 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2244 goto nla_put_failure;
2246 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2247 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2248 goto nla_put_failure;
2250 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2251 goto nla_put_failure;
2253 if ((ct->status & IPS_SEQ_ADJUST) &&
2254 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2255 goto nla_put_failure;
2257 #ifdef CONFIG_NF_CONNTRACK_MARK
2258 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2259 goto nla_put_failure;
2261 if (ctnetlink_dump_labels(skb, ct) < 0)
2262 goto nla_put_failure;
2272 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2273 enum ip_conntrack_info ctinfo,
2274 u_int16_t ct_attr, u_int16_t ct_info_attr)
2276 struct nlattr *nest_parms;
2278 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2280 goto nla_put_failure;
2282 if (__ctnetlink_glue_build(skb, ct) < 0)
2283 goto nla_put_failure;
2285 nla_nest_end(skb, nest_parms);
2287 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2288 goto nla_put_failure;
2297 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2299 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2300 unsigned long d = ct->status ^ status;
2302 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2303 /* SEEN_REPLY bit can only be set */
2306 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2307 /* ASSURED bit can only be set */
2310 /* This check is less strict than ctnetlink_change_status()
2311 * because callers often flip IPS_EXPECTED bits when sending
2312 * an NFQA_CT attribute to the kernel. So ignore the
2313 * unchangeable bits but do not error out. Also user programs
2314 * are allowed to clear the bits that they are allowed to change.
2316 __ctnetlink_change_status(ct, status, ~status);
2321 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2325 if (cda[CTA_TIMEOUT]) {
2326 err = ctnetlink_change_timeout(ct, cda);
2330 if (cda[CTA_STATUS]) {
2331 err = ctnetlink_update_status(ct, cda);
2335 if (cda[CTA_HELP]) {
2336 err = ctnetlink_change_helper(ct, cda);
2340 if (cda[CTA_LABELS]) {
2341 err = ctnetlink_attach_labels(ct, cda);
2345 #if defined(CONFIG_NF_CONNTRACK_MARK)
2346 if (cda[CTA_MARK]) {
2347 u32 mask = 0, mark, newmark;
2348 if (cda[CTA_MARK_MASK])
2349 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2351 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2352 newmark = (ct->mark & mask) ^ mark;
2353 if (newmark != ct->mark)
2361 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2363 struct nlattr *cda[CTA_MAX+1];
2366 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2370 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2373 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2374 const struct nf_conn *ct,
2375 struct nf_conntrack_tuple *tuple,
2376 struct nf_conntrack_tuple *mask)
2380 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2381 nf_ct_l3num(ct), NULL);
2385 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2386 nf_ct_l3num(ct), NULL);
2390 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2391 u32 portid, u32 report)
2393 struct nlattr *cda[CTA_EXPECT_MAX+1];
2394 struct nf_conntrack_tuple tuple, mask;
2395 struct nf_conntrack_helper *helper = NULL;
2396 struct nf_conntrack_expect *exp;
2399 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2404 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2409 if (cda[CTA_EXPECT_HELP_NAME]) {
2410 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2412 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2413 nf_ct_protonum(ct));
2418 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2419 helper, &tuple, &mask);
2421 return PTR_ERR(exp);
2423 err = nf_ct_expect_related_report(exp, portid, report);
2424 nf_ct_expect_put(exp);
2428 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2429 enum ip_conntrack_info ctinfo, int diff)
2431 if (!(ct->status & IPS_NAT_MASK))
2434 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2437 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2438 .get_ct = ctnetlink_glue_get_ct,
2439 .build_size = ctnetlink_glue_build_size,
2440 .build = ctnetlink_glue_build,
2441 .parse = ctnetlink_glue_parse,
2442 .attach_expect = ctnetlink_glue_attach_expect,
2443 .seq_adjust = ctnetlink_glue_seqadj,
2445 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2447 /***********************************************************************
2449 ***********************************************************************/
2451 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2452 const struct nf_conntrack_tuple *tuple,
2455 struct nlattr *nest_parms;
2457 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2459 goto nla_put_failure;
2460 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2461 goto nla_put_failure;
2462 nla_nest_end(skb, nest_parms);
2470 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2471 const struct nf_conntrack_tuple *tuple,
2472 const struct nf_conntrack_tuple_mask *mask)
2475 struct nf_conntrack_l3proto *l3proto;
2476 struct nf_conntrack_l4proto *l4proto;
2477 struct nf_conntrack_tuple m;
2478 struct nlattr *nest_parms;
2480 memset(&m, 0xFF, sizeof(m));
2481 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2482 m.src.u.all = mask->src.u.all;
2483 m.dst.protonum = tuple->dst.protonum;
2485 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2487 goto nla_put_failure;
2490 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2491 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2493 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2494 tuple->dst.protonum);
2495 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2499 if (unlikely(ret < 0))
2500 goto nla_put_failure;
2502 nla_nest_end(skb, nest_parms);
2510 static const union nf_inet_addr any_addr;
2513 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2514 const struct nf_conntrack_expect *exp)
2516 struct nf_conn *master = exp->master;
2517 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2518 struct nf_conn_help *help;
2519 #ifdef CONFIG_NF_NAT_NEEDED
2520 struct nlattr *nest_parms;
2521 struct nf_conntrack_tuple nat_tuple = {};
2523 struct nf_ct_helper_expectfn *expfn;
2528 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2529 goto nla_put_failure;
2530 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2531 goto nla_put_failure;
2532 if (ctnetlink_exp_dump_tuple(skb,
2533 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2534 CTA_EXPECT_MASTER) < 0)
2535 goto nla_put_failure;
2537 #ifdef CONFIG_NF_NAT_NEEDED
2538 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2539 exp->saved_proto.all) {
2540 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2542 goto nla_put_failure;
2544 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2545 goto nla_put_failure;
2547 nat_tuple.src.l3num = nf_ct_l3num(master);
2548 nat_tuple.src.u3 = exp->saved_addr;
2549 nat_tuple.dst.protonum = nf_ct_protonum(master);
2550 nat_tuple.src.u = exp->saved_proto;
2552 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2553 CTA_EXPECT_NAT_TUPLE) < 0)
2554 goto nla_put_failure;
2555 nla_nest_end(skb, nest_parms);
2558 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2559 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2560 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2561 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2562 goto nla_put_failure;
2563 help = nfct_help(master);
2565 struct nf_conntrack_helper *helper;
2567 helper = rcu_dereference(help->helper);
2569 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2570 goto nla_put_failure;
2572 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2573 if (expfn != NULL &&
2574 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2575 goto nla_put_failure;
2584 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2585 int event, const struct nf_conntrack_expect *exp)
2587 struct nlmsghdr *nlh;
2588 struct nfgenmsg *nfmsg;
2589 unsigned int flags = portid ? NLM_F_MULTI : 0;
2591 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2592 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2596 nfmsg = nlmsg_data(nlh);
2597 nfmsg->nfgen_family = exp->tuple.src.l3num;
2598 nfmsg->version = NFNETLINK_V0;
2601 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2602 goto nla_put_failure;
2604 nlmsg_end(skb, nlh);
2609 nlmsg_cancel(skb, nlh);
2613 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2615 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2617 struct nf_conntrack_expect *exp = item->exp;
2618 struct net *net = nf_ct_exp_net(exp);
2619 struct nlmsghdr *nlh;
2620 struct nfgenmsg *nfmsg;
2621 struct sk_buff *skb;
2622 unsigned int type, group;
2625 if (events & (1 << IPEXP_DESTROY)) {
2626 type = IPCTNL_MSG_EXP_DELETE;
2627 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2628 } else if (events & (1 << IPEXP_NEW)) {
2629 type = IPCTNL_MSG_EXP_NEW;
2630 flags = NLM_F_CREATE|NLM_F_EXCL;
2631 group = NFNLGRP_CONNTRACK_EXP_NEW;
2635 if (!item->report && !nfnetlink_has_listeners(net, group))
2638 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2642 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2643 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2647 nfmsg = nlmsg_data(nlh);
2648 nfmsg->nfgen_family = exp->tuple.src.l3num;
2649 nfmsg->version = NFNETLINK_V0;
2653 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2654 goto nla_put_failure;
2657 nlmsg_end(skb, nlh);
2658 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2663 nlmsg_cancel(skb, nlh);
2667 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2671 static int ctnetlink_exp_done(struct netlink_callback *cb)
2674 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2679 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2681 struct net *net = sock_net(skb->sk);
2682 struct nf_conntrack_expect *exp, *last;
2683 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2684 u_int8_t l3proto = nfmsg->nfgen_family;
2687 last = (struct nf_conntrack_expect *)cb->args[1];
2688 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2690 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2692 if (l3proto && exp->tuple.src.l3num != l3proto)
2695 if (!net_eq(nf_ct_net(exp->master), net))
2703 if (ctnetlink_exp_fill_info(skb,
2704 NETLINK_CB(cb->skb).portid,
2708 if (!refcount_inc_not_zero(&exp->use))
2710 cb->args[1] = (unsigned long)exp;
2722 nf_ct_expect_put(last);
2728 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2730 struct nf_conntrack_expect *exp, *last;
2731 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2732 struct nf_conn *ct = cb->data;
2733 struct nf_conn_help *help = nfct_help(ct);
2734 u_int8_t l3proto = nfmsg->nfgen_family;
2740 last = (struct nf_conntrack_expect *)cb->args[1];
2742 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2743 if (l3proto && exp->tuple.src.l3num != l3proto)
2750 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2754 if (!refcount_inc_not_zero(&exp->use))
2756 cb->args[1] = (unsigned long)exp;
2768 nf_ct_expect_put(last);
2773 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2774 struct sk_buff *skb,
2775 const struct nlmsghdr *nlh,
2776 const struct nlattr * const cda[])
2779 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2780 u_int8_t u3 = nfmsg->nfgen_family;
2781 struct nf_conntrack_tuple tuple;
2782 struct nf_conntrack_tuple_hash *h;
2784 struct nf_conntrack_zone zone;
2785 struct netlink_dump_control c = {
2786 .dump = ctnetlink_exp_ct_dump_table,
2787 .done = ctnetlink_exp_done,
2790 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2795 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2799 h = nf_conntrack_find_get(net, &zone, &tuple);
2803 ct = nf_ct_tuplehash_to_ctrack(h);
2804 /* No expectation linked to this connection tracking. */
2805 if (!nfct_help(ct)) {
2812 err = netlink_dump_start(ctnl, skb, nlh, &c);
2818 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2819 struct sk_buff *skb, const struct nlmsghdr *nlh,
2820 const struct nlattr * const cda[])
2822 struct nf_conntrack_tuple tuple;
2823 struct nf_conntrack_expect *exp;
2824 struct sk_buff *skb2;
2825 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2826 u_int8_t u3 = nfmsg->nfgen_family;
2827 struct nf_conntrack_zone zone;
2830 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2831 if (cda[CTA_EXPECT_MASTER])
2832 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda);
2834 struct netlink_dump_control c = {
2835 .dump = ctnetlink_exp_dump_table,
2836 .done = ctnetlink_exp_done,
2838 return netlink_dump_start(ctnl, skb, nlh, &c);
2842 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2846 if (cda[CTA_EXPECT_TUPLE])
2847 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2849 else if (cda[CTA_EXPECT_MASTER])
2850 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2858 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2862 if (cda[CTA_EXPECT_ID]) {
2863 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2864 if (ntohl(id) != (u32)(unsigned long)exp) {
2865 nf_ct_expect_put(exp);
2871 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2873 nf_ct_expect_put(exp);
2878 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2879 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2881 nf_ct_expect_put(exp);
2885 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2894 /* this avoids a loop in nfnetlink. */
2895 return err == -EAGAIN ? -ENOBUFS : err;
2898 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2899 struct sk_buff *skb, const struct nlmsghdr *nlh,
2900 const struct nlattr * const cda[])
2902 struct nf_conntrack_expect *exp;
2903 struct nf_conntrack_tuple tuple;
2904 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2905 struct hlist_node *next;
2906 u_int8_t u3 = nfmsg->nfgen_family;
2907 struct nf_conntrack_zone zone;
2911 if (cda[CTA_EXPECT_TUPLE]) {
2912 /* delete a single expect by tuple */
2913 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2917 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2922 /* bump usage count to 2 */
2923 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2927 if (cda[CTA_EXPECT_ID]) {
2928 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2929 if (ntohl(id) != (u32)(unsigned long)exp) {
2930 nf_ct_expect_put(exp);
2935 /* after list removal, usage count == 1 */
2936 spin_lock_bh(&nf_conntrack_expect_lock);
2937 if (del_timer(&exp->timeout)) {
2938 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2940 nf_ct_expect_put(exp);
2942 spin_unlock_bh(&nf_conntrack_expect_lock);
2943 /* have to put what we 'get' above.
2944 * after this line usage count == 0 */
2945 nf_ct_expect_put(exp);
2946 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2947 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2948 struct nf_conn_help *m_help;
2950 /* delete all expectations for this helper */
2951 spin_lock_bh(&nf_conntrack_expect_lock);
2952 for (i = 0; i < nf_ct_expect_hsize; i++) {
2953 hlist_for_each_entry_safe(exp, next,
2954 &nf_ct_expect_hash[i],
2957 if (!net_eq(nf_ct_exp_net(exp), net))
2960 m_help = nfct_help(exp->master);
2961 if (!strcmp(m_help->helper->name, name) &&
2962 del_timer(&exp->timeout)) {
2963 nf_ct_unlink_expect_report(exp,
2964 NETLINK_CB(skb).portid,
2966 nf_ct_expect_put(exp);
2970 spin_unlock_bh(&nf_conntrack_expect_lock);
2972 /* This basically means we have to flush everything*/
2973 spin_lock_bh(&nf_conntrack_expect_lock);
2974 for (i = 0; i < nf_ct_expect_hsize; i++) {
2975 hlist_for_each_entry_safe(exp, next,
2976 &nf_ct_expect_hash[i],
2979 if (!net_eq(nf_ct_exp_net(exp), net))
2982 if (del_timer(&exp->timeout)) {
2983 nf_ct_unlink_expect_report(exp,
2984 NETLINK_CB(skb).portid,
2986 nf_ct_expect_put(exp);
2990 spin_unlock_bh(&nf_conntrack_expect_lock);
2996 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2997 const struct nlattr * const cda[])
2999 if (cda[CTA_EXPECT_TIMEOUT]) {
3000 if (!del_timer(&x->timeout))
3003 x->timeout.expires = jiffies +
3004 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3005 add_timer(&x->timeout);
3010 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3011 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3012 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3016 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3017 struct nf_conntrack_expect *exp,
3020 #ifdef CONFIG_NF_NAT_NEEDED
3021 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3022 struct nf_conntrack_tuple nat_tuple = {};
3025 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3026 exp_nat_nla_policy, NULL);
3030 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3033 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3034 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3039 exp->saved_addr = nat_tuple.src.u3;
3040 exp->saved_proto = nat_tuple.src.u;
3041 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3049 static struct nf_conntrack_expect *
3050 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3051 struct nf_conntrack_helper *helper,
3052 struct nf_conntrack_tuple *tuple,
3053 struct nf_conntrack_tuple *mask)
3055 u_int32_t class = 0;
3056 struct nf_conntrack_expect *exp;
3057 struct nf_conn_help *help;
3060 help = nfct_help(ct);
3062 return ERR_PTR(-EOPNOTSUPP);
3064 if (cda[CTA_EXPECT_CLASS] && helper) {
3065 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3066 if (class > helper->expect_class_max)
3067 return ERR_PTR(-EINVAL);
3069 exp = nf_ct_expect_alloc(ct);
3071 return ERR_PTR(-ENOMEM);
3073 if (cda[CTA_EXPECT_FLAGS]) {
3074 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3075 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3079 if (cda[CTA_EXPECT_FN]) {
3080 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3081 struct nf_ct_helper_expectfn *expfn;
3083 expfn = nf_ct_helper_expectfn_find_by_name(name);
3084 if (expfn == NULL) {
3088 exp->expectfn = expfn->expectfn;
3090 exp->expectfn = NULL;
3094 exp->helper = helper;
3095 exp->tuple = *tuple;
3096 exp->mask.src.u3 = mask->src.u3;
3097 exp->mask.src.u.all = mask->src.u.all;
3099 if (cda[CTA_EXPECT_NAT]) {
3100 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3101 exp, nf_ct_l3num(ct));
3107 nf_ct_expect_put(exp);
3108 return ERR_PTR(err);
3112 ctnetlink_create_expect(struct net *net,
3113 const struct nf_conntrack_zone *zone,
3114 const struct nlattr * const cda[],
3115 u_int8_t u3, u32 portid, int report)
3117 struct nf_conntrack_tuple tuple, mask, master_tuple;
3118 struct nf_conntrack_tuple_hash *h = NULL;
3119 struct nf_conntrack_helper *helper = NULL;
3120 struct nf_conntrack_expect *exp;
3124 /* caller guarantees that those three CTA_EXPECT_* exist */
3125 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3129 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3133 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3138 /* Look for master conntrack of this expectation */
3139 h = nf_conntrack_find_get(net, zone, &master_tuple);
3142 ct = nf_ct_tuplehash_to_ctrack(h);
3145 if (cda[CTA_EXPECT_HELP_NAME]) {
3146 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3148 helper = __nf_conntrack_helper_find(helpname, u3,
3149 nf_ct_protonum(ct));
3150 if (helper == NULL) {
3152 #ifdef CONFIG_MODULES
3153 if (request_module("nfct-helper-%s", helpname) < 0) {
3158 helper = __nf_conntrack_helper_find(helpname, u3,
3159 nf_ct_protonum(ct));
3171 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3177 err = nf_ct_expect_related_report(exp, portid, report);
3178 nf_ct_expect_put(exp);
3186 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3187 struct sk_buff *skb, const struct nlmsghdr *nlh,
3188 const struct nlattr * const cda[])
3190 struct nf_conntrack_tuple tuple;
3191 struct nf_conntrack_expect *exp;
3192 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3193 u_int8_t u3 = nfmsg->nfgen_family;
3194 struct nf_conntrack_zone zone;
3197 if (!cda[CTA_EXPECT_TUPLE]
3198 || !cda[CTA_EXPECT_MASK]
3199 || !cda[CTA_EXPECT_MASTER])
3202 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3206 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3211 spin_lock_bh(&nf_conntrack_expect_lock);
3212 exp = __nf_ct_expect_find(net, &zone, &tuple);
3214 spin_unlock_bh(&nf_conntrack_expect_lock);
3216 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3217 err = ctnetlink_create_expect(net, &zone, cda, u3,
3218 NETLINK_CB(skb).portid,
3225 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3226 err = ctnetlink_change_expect(exp, cda);
3227 spin_unlock_bh(&nf_conntrack_expect_lock);
3233 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3234 const struct ip_conntrack_stat *st)
3236 struct nlmsghdr *nlh;
3237 struct nfgenmsg *nfmsg;
3238 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3240 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3241 IPCTNL_MSG_EXP_GET_STATS_CPU);
3242 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3246 nfmsg = nlmsg_data(nlh);
3247 nfmsg->nfgen_family = AF_UNSPEC;
3248 nfmsg->version = NFNETLINK_V0;
3249 nfmsg->res_id = htons(cpu);
3251 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3252 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3253 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3254 goto nla_put_failure;
3256 nlmsg_end(skb, nlh);
3261 nlmsg_cancel(skb, nlh);
3266 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3269 struct net *net = sock_net(skb->sk);
3271 if (cb->args[0] == nr_cpu_ids)
3274 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3275 const struct ip_conntrack_stat *st;
3277 if (!cpu_possible(cpu))
3280 st = per_cpu_ptr(net->ct.stat, cpu);
3281 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3291 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3292 struct sk_buff *skb,
3293 const struct nlmsghdr *nlh,
3294 const struct nlattr * const cda[])
3296 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3297 struct netlink_dump_control c = {
3298 .dump = ctnetlink_exp_stat_cpu_dump,
3300 return netlink_dump_start(ctnl, skb, nlh, &c);
3306 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3307 static struct nf_ct_event_notifier ctnl_notifier = {
3308 .fcn = ctnetlink_conntrack_event,
3311 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3312 .fcn = ctnetlink_expect_event,
3316 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3317 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3318 .attr_count = CTA_MAX,
3319 .policy = ct_nla_policy },
3320 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3321 .attr_count = CTA_MAX,
3322 .policy = ct_nla_policy },
3323 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3324 .attr_count = CTA_MAX,
3325 .policy = ct_nla_policy },
3326 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3327 .attr_count = CTA_MAX,
3328 .policy = ct_nla_policy },
3329 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3330 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3331 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3332 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3335 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3336 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3337 .attr_count = CTA_EXPECT_MAX,
3338 .policy = exp_nla_policy },
3339 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3340 .attr_count = CTA_EXPECT_MAX,
3341 .policy = exp_nla_policy },
3342 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3343 .attr_count = CTA_EXPECT_MAX,
3344 .policy = exp_nla_policy },
3345 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3348 static const struct nfnetlink_subsystem ctnl_subsys = {
3349 .name = "conntrack",
3350 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3351 .cb_count = IPCTNL_MSG_MAX,
3355 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3356 .name = "conntrack_expect",
3357 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3358 .cb_count = IPCTNL_MSG_EXP_MAX,
3362 MODULE_ALIAS("ip_conntrack_netlink");
3363 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3364 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3366 static int __net_init ctnetlink_net_init(struct net *net)
3368 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3371 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3373 pr_err("ctnetlink_init: cannot register notifier.\n");
3377 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3379 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3380 goto err_unreg_notifier;
3385 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3387 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3393 static void ctnetlink_net_exit(struct net *net)
3395 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3396 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3397 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3401 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3405 list_for_each_entry(net, net_exit_list, exit_list)
3406 ctnetlink_net_exit(net);
3409 static struct pernet_operations ctnetlink_net_ops = {
3410 .init = ctnetlink_net_init,
3411 .exit_batch = ctnetlink_net_exit_batch,
3414 static int __init ctnetlink_init(void)
3418 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3419 ret = nfnetlink_subsys_register(&ctnl_subsys);
3421 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3425 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3427 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3428 goto err_unreg_subsys;
3431 ret = register_pernet_subsys(&ctnetlink_net_ops);
3433 pr_err("ctnetlink_init: cannot register pernet operations\n");
3434 goto err_unreg_exp_subsys;
3436 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3437 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3438 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3442 err_unreg_exp_subsys:
3443 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3445 nfnetlink_subsys_unregister(&ctnl_subsys);
3450 static void __exit ctnetlink_exit(void)
3452 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3454 unregister_pernet_subsys(&ctnetlink_net_ops);
3455 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3456 nfnetlink_subsys_unregister(&ctnl_subsys);
3457 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3458 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3463 module_init(ctnetlink_init);
3464 module_exit(ctnetlink_exit);