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 #ifdef CONFIG_NF_NAT_NEEDED
49 #include <net/netfilter/nf_nat_core.h>
50 #include <net/netfilter/nf_nat_l4proto.h>
51 #include <net/netfilter/nf_nat_helper.h>
54 #include <linux/netfilter/nfnetlink.h>
55 #include <linux/netfilter/nfnetlink_conntrack.h>
57 MODULE_LICENSE("GPL");
59 static char __initdata version[] = "0.93";
61 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
62 const struct nf_conntrack_tuple *tuple,
63 struct nf_conntrack_l4proto *l4proto)
66 struct nlattr *nest_parms;
68 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
71 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
74 if (likely(l4proto->tuple_to_nlattr))
75 ret = l4proto->tuple_to_nlattr(skb, tuple);
77 nla_nest_end(skb, nest_parms);
85 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
86 const struct nf_conntrack_tuple *tuple,
87 struct nf_conntrack_l3proto *l3proto)
90 struct nlattr *nest_parms;
92 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
96 if (likely(l3proto->tuple_to_nlattr))
97 ret = l3proto->tuple_to_nlattr(skb, tuple);
99 nla_nest_end(skb, nest_parms);
107 static int ctnetlink_dump_tuples(struct sk_buff *skb,
108 const struct nf_conntrack_tuple *tuple)
111 struct nf_conntrack_l3proto *l3proto;
112 struct nf_conntrack_l4proto *l4proto;
115 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
116 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
119 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
120 tuple->dst.protonum);
121 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
127 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
128 const struct nf_conntrack_zone *zone, int dir)
130 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
132 if (nla_put_be16(skb, attrtype, htons(zone->id)))
133 goto nla_put_failure;
140 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
142 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
143 goto nla_put_failure;
150 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
152 long timeout = nf_ct_expires(ct) / HZ;
154 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
155 goto nla_put_failure;
162 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
164 struct nf_conntrack_l4proto *l4proto;
165 struct nlattr *nest_proto;
168 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
169 if (!l4proto->to_nlattr)
172 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
174 goto nla_put_failure;
176 ret = l4proto->to_nlattr(skb, nest_proto, ct);
178 nla_nest_end(skb, nest_proto);
186 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
187 const struct nf_conn *ct)
189 struct nlattr *nest_helper;
190 const struct nf_conn_help *help = nfct_help(ct);
191 struct nf_conntrack_helper *helper;
196 helper = rcu_dereference(help->helper);
200 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
202 goto nla_put_failure;
203 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
204 goto nla_put_failure;
206 if (helper->to_nlattr)
207 helper->to_nlattr(skb, ct);
209 nla_nest_end(skb, nest_helper);
218 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
219 enum ip_conntrack_dir dir, int type)
221 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
222 struct nf_conn_counter *counter = acct->counter;
223 struct nlattr *nest_count;
226 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
227 pkts = atomic64_xchg(&counter[dir].packets, 0);
228 bytes = atomic64_xchg(&counter[dir].bytes, 0);
230 pkts = atomic64_read(&counter[dir].packets);
231 bytes = atomic64_read(&counter[dir].bytes);
234 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
236 goto nla_put_failure;
238 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
240 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
242 goto nla_put_failure;
244 nla_nest_end(skb, nest_count);
253 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
255 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
260 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
262 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
269 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
271 struct nlattr *nest_count;
272 const struct nf_conn_tstamp *tstamp;
274 tstamp = nf_conn_tstamp_find(ct);
278 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
280 goto nla_put_failure;
282 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
283 CTA_TIMESTAMP_PAD) ||
284 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
285 cpu_to_be64(tstamp->stop),
287 goto nla_put_failure;
288 nla_nest_end(skb, nest_count);
296 #ifdef CONFIG_NF_CONNTRACK_MARK
297 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
299 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
300 goto nla_put_failure;
307 #define ctnetlink_dump_mark(a, b) (0)
310 #ifdef CONFIG_NF_CONNTRACK_SECMARK
311 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
313 struct nlattr *nest_secctx;
317 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
322 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
324 goto nla_put_failure;
326 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
327 goto nla_put_failure;
328 nla_nest_end(skb, nest_secctx);
332 security_release_secctx(secctx, len);
336 #define ctnetlink_dump_secctx(a, b) (0)
339 #ifdef CONFIG_NF_CONNTRACK_LABELS
340 static inline int ctnetlink_label_size(const struct nf_conn *ct)
342 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
346 return nla_total_size(sizeof(labels->bits));
350 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
352 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
360 if (labels->bits[i] != 0)
361 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
364 } while (i < ARRAY_SIZE(labels->bits));
369 #define ctnetlink_dump_labels(a, b) (0)
370 #define ctnetlink_label_size(a) (0)
373 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
375 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
377 struct nlattr *nest_parms;
379 if (!(ct->status & IPS_EXPECTED))
382 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
384 goto nla_put_failure;
385 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
386 goto nla_put_failure;
387 nla_nest_end(skb, nest_parms);
396 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
398 struct nlattr *nest_parms;
400 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
402 goto nla_put_failure;
404 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
405 htonl(seq->correction_pos)) ||
406 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
407 htonl(seq->offset_before)) ||
408 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
409 htonl(seq->offset_after)))
410 goto nla_put_failure;
412 nla_nest_end(skb, nest_parms);
420 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb,
421 const struct nf_conn *ct)
423 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
424 struct nf_ct_seqadj *seq;
426 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
429 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
430 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
433 seq = &seqadj->seq[IP_CT_DIR_REPLY];
434 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
440 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
442 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
443 goto nla_put_failure;
450 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
452 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
453 goto nla_put_failure;
461 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
464 const struct nf_conntrack_zone *zone;
465 struct nlmsghdr *nlh;
466 struct nfgenmsg *nfmsg;
467 struct nlattr *nest_parms;
468 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
470 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
471 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
475 nfmsg = nlmsg_data(nlh);
476 nfmsg->nfgen_family = nf_ct_l3num(ct);
477 nfmsg->version = NFNETLINK_V0;
480 zone = nf_ct_zone(ct);
482 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
484 goto nla_put_failure;
485 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
486 goto nla_put_failure;
487 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
488 NF_CT_ZONE_DIR_ORIG) < 0)
489 goto nla_put_failure;
490 nla_nest_end(skb, nest_parms);
492 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
494 goto nla_put_failure;
495 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
496 goto nla_put_failure;
497 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
498 NF_CT_ZONE_DIR_REPL) < 0)
499 goto nla_put_failure;
500 nla_nest_end(skb, nest_parms);
502 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
503 NF_CT_DEFAULT_ZONE_DIR) < 0)
504 goto nla_put_failure;
506 if (ctnetlink_dump_status(skb, ct) < 0 ||
507 ctnetlink_dump_timeout(skb, ct) < 0 ||
508 ctnetlink_dump_acct(skb, ct, type) < 0 ||
509 ctnetlink_dump_timestamp(skb, ct) < 0 ||
510 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
511 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
512 ctnetlink_dump_mark(skb, ct) < 0 ||
513 ctnetlink_dump_secctx(skb, ct) < 0 ||
514 ctnetlink_dump_labels(skb, ct) < 0 ||
515 ctnetlink_dump_id(skb, ct) < 0 ||
516 ctnetlink_dump_use(skb, ct) < 0 ||
517 ctnetlink_dump_master(skb, ct) < 0 ||
518 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
519 goto nla_put_failure;
526 nlmsg_cancel(skb, nlh);
530 static inline size_t ctnetlink_proto_size(const struct nf_conn *ct)
532 struct nf_conntrack_l3proto *l3proto;
533 struct nf_conntrack_l4proto *l4proto;
537 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
538 len += l3proto->nla_size;
540 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
541 len += l4proto->nla_size;
547 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
549 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
551 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
552 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
553 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
557 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
559 #ifdef CONFIG_NF_CONNTRACK_SECMARK
562 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
566 return nla_total_size(0) /* CTA_SECCTX */
567 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
573 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
575 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
576 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
578 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
584 #ifdef CONFIG_NF_CONNTRACK_EVENTS
585 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
587 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
588 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
589 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
590 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
591 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
592 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
593 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
594 + ctnetlink_acct_size(ct)
595 + ctnetlink_timestamp_size(ct)
596 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
597 + nla_total_size(0) /* CTA_PROTOINFO */
598 + nla_total_size(0) /* CTA_HELP */
599 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
600 + ctnetlink_secctx_size(ct)
601 #ifdef CONFIG_NF_NAT_NEEDED
602 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
603 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
605 #ifdef CONFIG_NF_CONNTRACK_MARK
606 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
608 #ifdef CONFIG_NF_CONNTRACK_ZONES
609 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
611 + ctnetlink_proto_size(ct)
612 + ctnetlink_label_size(ct)
617 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
619 const struct nf_conntrack_zone *zone;
621 struct nlmsghdr *nlh;
622 struct nfgenmsg *nfmsg;
623 struct nlattr *nest_parms;
624 struct nf_conn *ct = item->ct;
627 unsigned int flags = 0, group;
630 /* ignore our fake conntrack entry */
631 if (nf_ct_is_untracked(ct))
634 if (events & (1 << IPCT_DESTROY)) {
635 type = IPCTNL_MSG_CT_DELETE;
636 group = NFNLGRP_CONNTRACK_DESTROY;
637 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
638 type = IPCTNL_MSG_CT_NEW;
639 flags = NLM_F_CREATE|NLM_F_EXCL;
640 group = NFNLGRP_CONNTRACK_NEW;
642 type = IPCTNL_MSG_CT_NEW;
643 group = NFNLGRP_CONNTRACK_UPDATE;
648 if (!item->report && !nfnetlink_has_listeners(net, group))
651 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
655 type |= NFNL_SUBSYS_CTNETLINK << 8;
656 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
660 nfmsg = nlmsg_data(nlh);
661 nfmsg->nfgen_family = nf_ct_l3num(ct);
662 nfmsg->version = NFNETLINK_V0;
666 zone = nf_ct_zone(ct);
668 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
670 goto nla_put_failure;
671 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
672 goto nla_put_failure;
673 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
674 NF_CT_ZONE_DIR_ORIG) < 0)
675 goto nla_put_failure;
676 nla_nest_end(skb, nest_parms);
678 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
680 goto nla_put_failure;
681 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
682 goto nla_put_failure;
683 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
684 NF_CT_ZONE_DIR_REPL) < 0)
685 goto nla_put_failure;
686 nla_nest_end(skb, nest_parms);
688 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
689 NF_CT_DEFAULT_ZONE_DIR) < 0)
690 goto nla_put_failure;
692 if (ctnetlink_dump_id(skb, ct) < 0)
693 goto nla_put_failure;
695 if (ctnetlink_dump_status(skb, ct) < 0)
696 goto nla_put_failure;
698 if (events & (1 << IPCT_DESTROY)) {
699 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
700 ctnetlink_dump_timestamp(skb, ct) < 0)
701 goto nla_put_failure;
703 if (ctnetlink_dump_timeout(skb, ct) < 0)
704 goto nla_put_failure;
706 if (events & (1 << IPCT_PROTOINFO)
707 && ctnetlink_dump_protoinfo(skb, ct) < 0)
708 goto nla_put_failure;
710 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
711 && ctnetlink_dump_helpinfo(skb, ct) < 0)
712 goto nla_put_failure;
714 #ifdef CONFIG_NF_CONNTRACK_SECMARK
715 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
716 && ctnetlink_dump_secctx(skb, ct) < 0)
717 goto nla_put_failure;
719 if (events & (1 << IPCT_LABEL) &&
720 ctnetlink_dump_labels(skb, ct) < 0)
721 goto nla_put_failure;
723 if (events & (1 << IPCT_RELATED) &&
724 ctnetlink_dump_master(skb, ct) < 0)
725 goto nla_put_failure;
727 if (events & (1 << IPCT_SEQADJ) &&
728 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
729 goto nla_put_failure;
732 #ifdef CONFIG_NF_CONNTRACK_MARK
733 if ((events & (1 << IPCT_MARK) || ct->mark)
734 && ctnetlink_dump_mark(skb, ct) < 0)
735 goto nla_put_failure;
740 err = nfnetlink_send(skb, net, item->portid, group, item->report,
742 if (err == -ENOBUFS || err == -EAGAIN)
749 nlmsg_cancel(skb, nlh);
753 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
758 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
760 static int ctnetlink_done(struct netlink_callback *cb)
763 nf_ct_put((struct nf_conn *)cb->args[1]);
768 struct ctnetlink_filter {
775 static struct ctnetlink_filter *
776 ctnetlink_alloc_filter(const struct nlattr * const cda[])
778 #ifdef CONFIG_NF_CONNTRACK_MARK
779 struct ctnetlink_filter *filter;
781 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
783 return ERR_PTR(-ENOMEM);
785 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
786 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
790 return ERR_PTR(-EOPNOTSUPP);
794 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
796 struct ctnetlink_filter *filter = data;
801 #ifdef CONFIG_NF_CONNTRACK_MARK
802 if ((ct->mark & filter->mark.mask) == filter->mark.val)
810 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
812 struct net *net = sock_net(skb->sk);
813 struct nf_conn *ct, *last;
814 struct nf_conntrack_tuple_hash *h;
815 struct hlist_nulls_node *n;
816 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
817 u_int8_t l3proto = nfmsg->nfgen_family;
818 struct nf_conn *nf_ct_evict[8];
822 last = (struct nf_conn *)cb->args[1];
826 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
830 if (nf_ct_should_gc(nf_ct_evict[i]))
831 nf_ct_kill(nf_ct_evict[i]);
832 nf_ct_put(nf_ct_evict[i]);
835 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
836 nf_conntrack_lock(lockp);
837 if (cb->args[0] >= nf_conntrack_htable_size) {
841 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
843 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
845 ct = nf_ct_tuplehash_to_ctrack(h);
846 if (nf_ct_is_expired(ct)) {
847 if (i < ARRAY_SIZE(nf_ct_evict) &&
848 atomic_inc_not_zero(&ct->ct_general.use))
849 nf_ct_evict[i++] = ct;
853 if (!net_eq(net, nf_ct_net(ct)))
856 /* Dump entries of a given L3 protocol number.
857 * If it is not specified, ie. l3proto == 0,
858 * then dump everything. */
859 if (l3proto && nf_ct_l3num(ct) != l3proto)
866 if (!ctnetlink_filter_match(ct, cb->data))
871 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
873 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
877 nf_conntrack_get(&ct->ct_general);
878 cb->args[1] = (unsigned long)ct;
896 if (nf_ct_should_gc(nf_ct_evict[i]))
897 nf_ct_kill(nf_ct_evict[i]);
898 nf_ct_put(nf_ct_evict[i]);
904 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
905 struct nf_conntrack_tuple *tuple)
907 struct nlattr *tb[CTA_IP_MAX+1];
908 struct nf_conntrack_l3proto *l3proto;
911 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
916 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
918 if (likely(l3proto->nlattr_to_tuple)) {
919 ret = nla_validate_nested(attr, CTA_IP_MAX,
920 l3proto->nla_policy);
922 ret = l3proto->nlattr_to_tuple(tb, tuple);
930 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
931 [CTA_PROTO_NUM] = { .type = NLA_U8 },
934 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
935 struct nf_conntrack_tuple *tuple)
937 struct nlattr *tb[CTA_PROTO_MAX+1];
938 struct nf_conntrack_l4proto *l4proto;
941 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
945 if (!tb[CTA_PROTO_NUM])
947 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
950 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
952 if (likely(l4proto->nlattr_to_tuple)) {
953 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
954 l4proto->nla_policy);
956 ret = l4proto->nlattr_to_tuple(tb, tuple);
965 ctnetlink_parse_zone(const struct nlattr *attr,
966 struct nf_conntrack_zone *zone)
968 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
969 NF_CT_DEFAULT_ZONE_DIR, 0);
970 #ifdef CONFIG_NF_CONNTRACK_ZONES
972 zone->id = ntohs(nla_get_be16(attr));
981 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
982 struct nf_conntrack_zone *zone)
986 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
989 ret = ctnetlink_parse_zone(attr, zone);
993 if (type == CTA_TUPLE_REPLY)
994 zone->dir = NF_CT_ZONE_DIR_REPL;
996 zone->dir = NF_CT_ZONE_DIR_ORIG;
1001 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1002 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1003 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1004 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1008 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1009 struct nf_conntrack_tuple *tuple,
1010 enum ctattr_type type, u_int8_t l3num,
1011 struct nf_conntrack_zone *zone)
1013 struct nlattr *tb[CTA_TUPLE_MAX+1];
1016 memset(tuple, 0, sizeof(*tuple));
1018 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
1022 if (!tb[CTA_TUPLE_IP])
1025 tuple->src.l3num = l3num;
1027 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1031 if (!tb[CTA_TUPLE_PROTO])
1034 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1038 if (tb[CTA_TUPLE_ZONE]) {
1042 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1048 /* orig and expect tuples get DIR_ORIGINAL */
1049 if (type == CTA_TUPLE_REPLY)
1050 tuple->dst.dir = IP_CT_DIR_REPLY;
1052 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1057 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1058 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1059 .len = NF_CT_HELPER_NAME_LEN - 1 },
1062 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1063 struct nlattr **helpinfo)
1066 struct nlattr *tb[CTA_HELP_MAX+1];
1068 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
1072 if (!tb[CTA_HELP_NAME])
1075 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1077 if (tb[CTA_HELP_INFO])
1078 *helpinfo = tb[CTA_HELP_INFO];
1083 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1084 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1085 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1086 [CTA_STATUS] = { .type = NLA_U32 },
1087 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1088 [CTA_HELP] = { .type = NLA_NESTED },
1089 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1090 [CTA_TIMEOUT] = { .type = NLA_U32 },
1091 [CTA_MARK] = { .type = NLA_U32 },
1092 [CTA_ID] = { .type = NLA_U32 },
1093 [CTA_NAT_DST] = { .type = NLA_NESTED },
1094 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1095 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1096 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1097 [CTA_ZONE] = { .type = NLA_U16 },
1098 [CTA_MARK_MASK] = { .type = NLA_U32 },
1099 [CTA_LABELS] = { .type = NLA_BINARY,
1100 .len = NF_CT_LABELS_MAX_SIZE },
1101 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1102 .len = NF_CT_LABELS_MAX_SIZE },
1105 static int ctnetlink_flush_conntrack(struct net *net,
1106 const struct nlattr * const cda[],
1107 u32 portid, int report)
1109 struct ctnetlink_filter *filter = NULL;
1111 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1112 filter = ctnetlink_alloc_filter(cda);
1114 return PTR_ERR(filter);
1117 nf_ct_iterate_cleanup(net, ctnetlink_filter_match, filter,
1124 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1125 struct sk_buff *skb,
1126 const struct nlmsghdr *nlh,
1127 const struct nlattr * const cda[])
1129 struct nf_conntrack_tuple_hash *h;
1130 struct nf_conntrack_tuple tuple;
1132 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1133 u_int8_t u3 = nfmsg->nfgen_family;
1134 struct nf_conntrack_zone zone;
1137 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1141 if (cda[CTA_TUPLE_ORIG])
1142 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1144 else if (cda[CTA_TUPLE_REPLY])
1145 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1148 return ctnetlink_flush_conntrack(net, cda,
1149 NETLINK_CB(skb).portid,
1156 h = nf_conntrack_find_get(net, &zone, &tuple);
1160 ct = nf_ct_tuplehash_to_ctrack(h);
1163 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1164 if (id != (u32)(unsigned long)ct) {
1170 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1176 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1177 struct sk_buff *skb,
1178 const struct nlmsghdr *nlh,
1179 const struct nlattr * const cda[])
1181 struct nf_conntrack_tuple_hash *h;
1182 struct nf_conntrack_tuple tuple;
1184 struct sk_buff *skb2 = NULL;
1185 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1186 u_int8_t u3 = nfmsg->nfgen_family;
1187 struct nf_conntrack_zone zone;
1190 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1191 struct netlink_dump_control c = {
1192 .dump = ctnetlink_dump_table,
1193 .done = ctnetlink_done,
1196 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1197 struct ctnetlink_filter *filter;
1199 filter = ctnetlink_alloc_filter(cda);
1201 return PTR_ERR(filter);
1205 return netlink_dump_start(ctnl, skb, nlh, &c);
1208 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1212 if (cda[CTA_TUPLE_ORIG])
1213 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1215 else if (cda[CTA_TUPLE_REPLY])
1216 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1224 h = nf_conntrack_find_get(net, &zone, &tuple);
1228 ct = nf_ct_tuplehash_to_ctrack(h);
1231 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1238 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1239 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1245 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1254 /* this avoids a loop in nfnetlink. */
1255 return err == -EAGAIN ? -ENOBUFS : err;
1258 static int ctnetlink_done_list(struct netlink_callback *cb)
1261 nf_ct_put((struct nf_conn *)cb->args[1]);
1266 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1268 struct nf_conn *ct, *last;
1269 struct nf_conntrack_tuple_hash *h;
1270 struct hlist_nulls_node *n;
1271 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1272 u_int8_t l3proto = nfmsg->nfgen_family;
1275 struct hlist_nulls_head *list;
1276 struct net *net = sock_net(skb->sk);
1281 last = (struct nf_conn *)cb->args[1];
1283 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1284 struct ct_pcpu *pcpu;
1286 if (!cpu_possible(cpu))
1289 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1290 spin_lock_bh(&pcpu->lock);
1291 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1293 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1294 ct = nf_ct_tuplehash_to_ctrack(h);
1295 if (l3proto && nf_ct_l3num(ct) != l3proto)
1303 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1305 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1309 if (!atomic_inc_not_zero(&ct->ct_general.use))
1312 cb->args[1] = (unsigned long)ct;
1313 spin_unlock_bh(&pcpu->lock);
1321 spin_unlock_bh(&pcpu->lock);
1332 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1334 return ctnetlink_dump_list(skb, cb, true);
1337 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1338 struct sk_buff *skb,
1339 const struct nlmsghdr *nlh,
1340 const struct nlattr * const cda[])
1342 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1343 struct netlink_dump_control c = {
1344 .dump = ctnetlink_dump_dying,
1345 .done = ctnetlink_done_list,
1347 return netlink_dump_start(ctnl, skb, nlh, &c);
1354 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1356 return ctnetlink_dump_list(skb, cb, false);
1359 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1360 struct sk_buff *skb,
1361 const struct nlmsghdr *nlh,
1362 const struct nlattr * const cda[])
1364 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1365 struct netlink_dump_control c = {
1366 .dump = ctnetlink_dump_unconfirmed,
1367 .done = ctnetlink_done_list,
1369 return netlink_dump_start(ctnl, skb, nlh, &c);
1375 #ifdef CONFIG_NF_NAT_NEEDED
1377 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1378 enum nf_nat_manip_type manip,
1379 const struct nlattr *attr)
1381 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1384 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1385 if (!parse_nat_setup) {
1386 #ifdef CONFIG_MODULES
1388 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1389 if (request_module("nf-nat") < 0) {
1390 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1394 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1396 if (nfnetlink_parse_nat_setup_hook)
1402 err = parse_nat_setup(ct, manip, attr);
1403 if (err == -EAGAIN) {
1404 #ifdef CONFIG_MODULES
1406 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1407 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1408 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1412 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1423 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1426 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1427 d = ct->status ^ status;
1429 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1433 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1434 /* SEEN_REPLY bit can only be set */
1437 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1438 /* ASSURED bit can only be set */
1441 /* Be careful here, modifying NAT bits can screw up things,
1442 * so don't let users modify them directly if they don't pass
1444 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1449 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1451 #ifdef CONFIG_NF_NAT_NEEDED
1454 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1457 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1462 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1466 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1472 static int ctnetlink_change_helper(struct nf_conn *ct,
1473 const struct nlattr * const cda[])
1475 struct nf_conntrack_helper *helper;
1476 struct nf_conn_help *help = nfct_help(ct);
1477 char *helpname = NULL;
1478 struct nlattr *helpinfo = NULL;
1481 /* don't change helper of sibling connections */
1485 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1489 if (!strcmp(helpname, "")) {
1490 if (help && help->helper) {
1491 /* we had a helper before ... */
1492 nf_ct_remove_expectations(ct);
1493 RCU_INIT_POINTER(help->helper, NULL);
1499 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1500 nf_ct_protonum(ct));
1501 if (helper == NULL) {
1502 #ifdef CONFIG_MODULES
1503 spin_unlock_bh(&nf_conntrack_expect_lock);
1505 if (request_module("nfct-helper-%s", helpname) < 0) {
1506 spin_lock_bh(&nf_conntrack_expect_lock);
1510 spin_lock_bh(&nf_conntrack_expect_lock);
1511 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1512 nf_ct_protonum(ct));
1520 if (help->helper == helper) {
1521 /* update private helper data if allowed. */
1522 if (helper->from_nlattr)
1523 helper->from_nlattr(helpinfo, ct);
1529 /* we cannot set a helper for an existing conntrack */
1533 static int ctnetlink_change_timeout(struct nf_conn *ct,
1534 const struct nlattr * const cda[])
1536 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1538 ct->timeout = nfct_time_stamp + timeout * HZ;
1540 if (test_bit(IPS_DYING_BIT, &ct->status))
1546 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1547 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1548 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1549 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1552 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1553 const struct nlattr * const cda[])
1555 const struct nlattr *attr = cda[CTA_PROTOINFO];
1556 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1557 struct nf_conntrack_l4proto *l4proto;
1560 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1565 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1566 if (l4proto->from_nlattr)
1567 err = l4proto->from_nlattr(tb, ct);
1573 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1574 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1575 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1576 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1579 static int change_seq_adj(struct nf_ct_seqadj *seq,
1580 const struct nlattr * const attr)
1583 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1585 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy);
1589 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1592 seq->correction_pos =
1593 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1595 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1598 seq->offset_before =
1599 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1601 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1605 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1611 ctnetlink_change_seq_adj(struct nf_conn *ct,
1612 const struct nlattr * const cda[])
1614 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1620 if (cda[CTA_SEQ_ADJ_ORIG]) {
1621 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1622 cda[CTA_SEQ_ADJ_ORIG]);
1626 ct->status |= IPS_SEQ_ADJUST;
1629 if (cda[CTA_SEQ_ADJ_REPLY]) {
1630 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1631 cda[CTA_SEQ_ADJ_REPLY]);
1635 ct->status |= IPS_SEQ_ADJUST;
1642 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1644 #ifdef CONFIG_NF_CONNTRACK_LABELS
1645 size_t len = nla_len(cda[CTA_LABELS]);
1646 const void *mask = cda[CTA_LABELS_MASK];
1648 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1652 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1653 nla_len(cda[CTA_LABELS_MASK]) != len)
1655 mask = nla_data(cda[CTA_LABELS_MASK]);
1660 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1667 ctnetlink_change_conntrack(struct nf_conn *ct,
1668 const struct nlattr * const cda[])
1672 /* only allow NAT changes and master assignation for new conntracks */
1673 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1676 if (cda[CTA_HELP]) {
1677 err = ctnetlink_change_helper(ct, cda);
1682 if (cda[CTA_TIMEOUT]) {
1683 err = ctnetlink_change_timeout(ct, cda);
1688 if (cda[CTA_STATUS]) {
1689 err = ctnetlink_change_status(ct, cda);
1694 if (cda[CTA_PROTOINFO]) {
1695 err = ctnetlink_change_protoinfo(ct, cda);
1700 #if defined(CONFIG_NF_CONNTRACK_MARK)
1702 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1705 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1706 err = ctnetlink_change_seq_adj(ct, cda);
1711 if (cda[CTA_LABELS]) {
1712 err = ctnetlink_attach_labels(ct, cda);
1720 static struct nf_conn *
1721 ctnetlink_create_conntrack(struct net *net,
1722 const struct nf_conntrack_zone *zone,
1723 const struct nlattr * const cda[],
1724 struct nf_conntrack_tuple *otuple,
1725 struct nf_conntrack_tuple *rtuple,
1730 struct nf_conntrack_helper *helper;
1731 struct nf_conn_tstamp *tstamp;
1733 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1735 return ERR_PTR(-ENOMEM);
1737 if (!cda[CTA_TIMEOUT])
1740 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1743 if (cda[CTA_HELP]) {
1744 char *helpname = NULL;
1745 struct nlattr *helpinfo = NULL;
1747 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1751 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1752 nf_ct_protonum(ct));
1753 if (helper == NULL) {
1755 #ifdef CONFIG_MODULES
1756 if (request_module("nfct-helper-%s", helpname) < 0) {
1762 helper = __nf_conntrack_helper_find(helpname,
1764 nf_ct_protonum(ct));
1774 struct nf_conn_help *help;
1776 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1781 /* set private helper data if allowed. */
1782 if (helper->from_nlattr)
1783 helper->from_nlattr(helpinfo, ct);
1785 /* not in hash table yet so not strictly necessary */
1786 RCU_INIT_POINTER(help->helper, helper);
1789 /* try an implicit helper assignation */
1790 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1795 err = ctnetlink_setup_nat(ct, cda);
1799 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1800 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1801 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1802 nf_ct_labels_ext_add(ct);
1804 /* we must add conntrack extensions before confirmation. */
1805 ct->status |= IPS_CONFIRMED;
1807 if (cda[CTA_STATUS]) {
1808 err = ctnetlink_change_status(ct, cda);
1813 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1814 err = ctnetlink_change_seq_adj(ct, cda);
1819 memset(&ct->proto, 0, sizeof(ct->proto));
1820 if (cda[CTA_PROTOINFO]) {
1821 err = ctnetlink_change_protoinfo(ct, cda);
1826 #if defined(CONFIG_NF_CONNTRACK_MARK)
1828 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1831 /* setup master conntrack: this is a confirmed expectation */
1832 if (cda[CTA_TUPLE_MASTER]) {
1833 struct nf_conntrack_tuple master;
1834 struct nf_conntrack_tuple_hash *master_h;
1835 struct nf_conn *master_ct;
1837 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1842 master_h = nf_conntrack_find_get(net, zone, &master);
1843 if (master_h == NULL) {
1847 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1848 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1849 ct->master = master_ct;
1851 tstamp = nf_conn_tstamp_find(ct);
1853 tstamp->start = ktime_get_real_ns();
1855 err = nf_conntrack_hash_check_insert(ct);
1866 nf_conntrack_free(ct);
1867 return ERR_PTR(err);
1870 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1871 struct sk_buff *skb,
1872 const struct nlmsghdr *nlh,
1873 const struct nlattr * const cda[])
1875 struct nf_conntrack_tuple otuple, rtuple;
1876 struct nf_conntrack_tuple_hash *h = NULL;
1877 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1879 u_int8_t u3 = nfmsg->nfgen_family;
1880 struct nf_conntrack_zone zone;
1883 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1887 if (cda[CTA_TUPLE_ORIG]) {
1888 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1894 if (cda[CTA_TUPLE_REPLY]) {
1895 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1901 if (cda[CTA_TUPLE_ORIG])
1902 h = nf_conntrack_find_get(net, &zone, &otuple);
1903 else if (cda[CTA_TUPLE_REPLY])
1904 h = nf_conntrack_find_get(net, &zone, &rtuple);
1908 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1909 enum ip_conntrack_events events;
1911 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1913 if (otuple.dst.protonum != rtuple.dst.protonum)
1916 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1922 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1923 events = IPCT_RELATED;
1927 if (cda[CTA_LABELS] &&
1928 ctnetlink_attach_labels(ct, cda) == 0)
1929 events |= (1 << IPCT_LABEL);
1931 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1932 (1 << IPCT_ASSURED) |
1933 (1 << IPCT_HELPER) |
1934 (1 << IPCT_PROTOINFO) |
1935 (1 << IPCT_SEQADJ) |
1936 (1 << IPCT_MARK) | events,
1937 ct, NETLINK_CB(skb).portid,
1944 /* implicit 'else' */
1947 ct = nf_ct_tuplehash_to_ctrack(h);
1948 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1949 spin_lock_bh(&nf_conntrack_expect_lock);
1950 err = ctnetlink_change_conntrack(ct, cda);
1951 spin_unlock_bh(&nf_conntrack_expect_lock);
1953 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1954 (1 << IPCT_ASSURED) |
1955 (1 << IPCT_HELPER) |
1957 (1 << IPCT_PROTOINFO) |
1958 (1 << IPCT_SEQADJ) |
1960 ct, NETLINK_CB(skb).portid,
1970 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1971 __u16 cpu, const struct ip_conntrack_stat *st)
1973 struct nlmsghdr *nlh;
1974 struct nfgenmsg *nfmsg;
1975 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1977 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1978 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1982 nfmsg = nlmsg_data(nlh);
1983 nfmsg->nfgen_family = AF_UNSPEC;
1984 nfmsg->version = NFNETLINK_V0;
1985 nfmsg->res_id = htons(cpu);
1987 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1988 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1989 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1990 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1991 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1992 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1993 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1994 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1995 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1996 htonl(st->insert_failed)) ||
1997 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1998 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1999 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2000 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2001 htonl(st->search_restart)))
2002 goto nla_put_failure;
2004 nlmsg_end(skb, nlh);
2009 nlmsg_cancel(skb, nlh);
2014 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2017 struct net *net = sock_net(skb->sk);
2019 if (cb->args[0] == nr_cpu_ids)
2022 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2023 const struct ip_conntrack_stat *st;
2025 if (!cpu_possible(cpu))
2028 st = per_cpu_ptr(net->ct.stat, cpu);
2029 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2030 NETLINK_CB(cb->skb).portid,
2040 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2041 struct sk_buff *skb,
2042 const struct nlmsghdr *nlh,
2043 const struct nlattr * const cda[])
2045 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2046 struct netlink_dump_control c = {
2047 .dump = ctnetlink_ct_stat_cpu_dump,
2049 return netlink_dump_start(ctnl, skb, nlh, &c);
2056 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2059 struct nlmsghdr *nlh;
2060 struct nfgenmsg *nfmsg;
2061 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2062 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2064 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
2065 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2069 nfmsg = nlmsg_data(nlh);
2070 nfmsg->nfgen_family = AF_UNSPEC;
2071 nfmsg->version = NFNETLINK_V0;
2074 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2075 goto nla_put_failure;
2077 nlmsg_end(skb, nlh);
2082 nlmsg_cancel(skb, nlh);
2086 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2087 struct sk_buff *skb, const struct nlmsghdr *nlh,
2088 const struct nlattr * const cda[])
2090 struct sk_buff *skb2;
2093 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2097 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2099 NFNL_MSG_TYPE(nlh->nlmsg_type),
2104 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2113 /* this avoids a loop in nfnetlink. */
2114 return err == -EAGAIN ? -ENOBUFS : err;
2117 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2118 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2119 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2120 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2121 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2122 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2123 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2124 .len = NF_CT_HELPER_NAME_LEN - 1 },
2125 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2126 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2127 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2128 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2129 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2132 static struct nf_conntrack_expect *
2133 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2134 struct nf_conntrack_helper *helper,
2135 struct nf_conntrack_tuple *tuple,
2136 struct nf_conntrack_tuple *mask);
2138 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2140 ctnetlink_glue_build_size(const struct nf_conn *ct)
2142 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2143 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2144 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2145 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2146 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2147 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2148 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2149 + nla_total_size(0) /* CTA_PROTOINFO */
2150 + nla_total_size(0) /* CTA_HELP */
2151 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2152 + ctnetlink_secctx_size(ct)
2153 #ifdef CONFIG_NF_NAT_NEEDED
2154 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2155 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2157 #ifdef CONFIG_NF_CONNTRACK_MARK
2158 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2160 #ifdef CONFIG_NF_CONNTRACK_ZONES
2161 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2163 + ctnetlink_proto_size(ct)
2167 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2168 enum ip_conntrack_info *ctinfo)
2172 ct = nf_ct_get(skb, ctinfo);
2173 if (ct && nf_ct_is_untracked(ct))
2179 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2181 const struct nf_conntrack_zone *zone;
2182 struct nlattr *nest_parms;
2185 zone = nf_ct_zone(ct);
2187 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2189 goto nla_put_failure;
2190 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2191 goto nla_put_failure;
2192 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2193 NF_CT_ZONE_DIR_ORIG) < 0)
2194 goto nla_put_failure;
2195 nla_nest_end(skb, nest_parms);
2197 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2199 goto nla_put_failure;
2200 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2201 goto nla_put_failure;
2202 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2203 NF_CT_ZONE_DIR_REPL) < 0)
2204 goto nla_put_failure;
2205 nla_nest_end(skb, nest_parms);
2207 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2208 NF_CT_DEFAULT_ZONE_DIR) < 0)
2209 goto nla_put_failure;
2211 if (ctnetlink_dump_id(skb, ct) < 0)
2212 goto nla_put_failure;
2214 if (ctnetlink_dump_status(skb, ct) < 0)
2215 goto nla_put_failure;
2217 if (ctnetlink_dump_timeout(skb, ct) < 0)
2218 goto nla_put_failure;
2220 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2221 goto nla_put_failure;
2223 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2224 goto nla_put_failure;
2226 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2227 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2228 goto nla_put_failure;
2230 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2231 goto nla_put_failure;
2233 if ((ct->status & IPS_SEQ_ADJUST) &&
2234 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2235 goto nla_put_failure;
2237 #ifdef CONFIG_NF_CONNTRACK_MARK
2238 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2239 goto nla_put_failure;
2241 if (ctnetlink_dump_labels(skb, ct) < 0)
2242 goto nla_put_failure;
2252 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2253 enum ip_conntrack_info ctinfo,
2254 u_int16_t ct_attr, u_int16_t ct_info_attr)
2256 struct nlattr *nest_parms;
2258 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2260 goto nla_put_failure;
2262 if (__ctnetlink_glue_build(skb, ct) < 0)
2263 goto nla_put_failure;
2265 nla_nest_end(skb, nest_parms);
2267 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2268 goto nla_put_failure;
2277 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2281 if (cda[CTA_TIMEOUT]) {
2282 err = ctnetlink_change_timeout(ct, cda);
2286 if (cda[CTA_STATUS]) {
2287 err = ctnetlink_change_status(ct, cda);
2291 if (cda[CTA_HELP]) {
2292 err = ctnetlink_change_helper(ct, cda);
2296 if (cda[CTA_LABELS]) {
2297 err = ctnetlink_attach_labels(ct, cda);
2301 #if defined(CONFIG_NF_CONNTRACK_MARK)
2302 if (cda[CTA_MARK]) {
2303 u32 mask = 0, mark, newmark;
2304 if (cda[CTA_MARK_MASK])
2305 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2307 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2308 newmark = (ct->mark & mask) ^ mark;
2309 if (newmark != ct->mark)
2317 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2319 struct nlattr *cda[CTA_MAX+1];
2322 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2326 spin_lock_bh(&nf_conntrack_expect_lock);
2327 ret = ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2328 spin_unlock_bh(&nf_conntrack_expect_lock);
2333 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2334 const struct nf_conn *ct,
2335 struct nf_conntrack_tuple *tuple,
2336 struct nf_conntrack_tuple *mask)
2340 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2341 nf_ct_l3num(ct), NULL);
2345 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2346 nf_ct_l3num(ct), NULL);
2350 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2351 u32 portid, u32 report)
2353 struct nlattr *cda[CTA_EXPECT_MAX+1];
2354 struct nf_conntrack_tuple tuple, mask;
2355 struct nf_conntrack_helper *helper = NULL;
2356 struct nf_conntrack_expect *exp;
2359 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy);
2363 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2368 if (cda[CTA_EXPECT_HELP_NAME]) {
2369 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2371 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2372 nf_ct_protonum(ct));
2377 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2378 helper, &tuple, &mask);
2380 return PTR_ERR(exp);
2382 err = nf_ct_expect_related_report(exp, portid, report);
2383 nf_ct_expect_put(exp);
2387 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2388 enum ip_conntrack_info ctinfo, int diff)
2390 if (!(ct->status & IPS_NAT_MASK))
2393 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2396 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2397 .get_ct = ctnetlink_glue_get_ct,
2398 .build_size = ctnetlink_glue_build_size,
2399 .build = ctnetlink_glue_build,
2400 .parse = ctnetlink_glue_parse,
2401 .attach_expect = ctnetlink_glue_attach_expect,
2402 .seq_adjust = ctnetlink_glue_seqadj,
2404 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2406 /***********************************************************************
2408 ***********************************************************************/
2410 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2411 const struct nf_conntrack_tuple *tuple,
2412 enum ctattr_expect type)
2414 struct nlattr *nest_parms;
2416 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2418 goto nla_put_failure;
2419 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2420 goto nla_put_failure;
2421 nla_nest_end(skb, nest_parms);
2429 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2430 const struct nf_conntrack_tuple *tuple,
2431 const struct nf_conntrack_tuple_mask *mask)
2434 struct nf_conntrack_l3proto *l3proto;
2435 struct nf_conntrack_l4proto *l4proto;
2436 struct nf_conntrack_tuple m;
2437 struct nlattr *nest_parms;
2439 memset(&m, 0xFF, sizeof(m));
2440 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2441 m.src.u.all = mask->src.u.all;
2442 m.dst.protonum = tuple->dst.protonum;
2444 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2446 goto nla_put_failure;
2449 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2450 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2452 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2453 tuple->dst.protonum);
2454 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2458 if (unlikely(ret < 0))
2459 goto nla_put_failure;
2461 nla_nest_end(skb, nest_parms);
2469 static const union nf_inet_addr any_addr;
2472 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2473 const struct nf_conntrack_expect *exp)
2475 struct nf_conn *master = exp->master;
2476 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2477 struct nf_conn_help *help;
2478 #ifdef CONFIG_NF_NAT_NEEDED
2479 struct nlattr *nest_parms;
2480 struct nf_conntrack_tuple nat_tuple = {};
2482 struct nf_ct_helper_expectfn *expfn;
2487 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2488 goto nla_put_failure;
2489 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2490 goto nla_put_failure;
2491 if (ctnetlink_exp_dump_tuple(skb,
2492 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2493 CTA_EXPECT_MASTER) < 0)
2494 goto nla_put_failure;
2496 #ifdef CONFIG_NF_NAT_NEEDED
2497 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2498 exp->saved_proto.all) {
2499 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2501 goto nla_put_failure;
2503 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2504 goto nla_put_failure;
2506 nat_tuple.src.l3num = nf_ct_l3num(master);
2507 nat_tuple.src.u3 = exp->saved_addr;
2508 nat_tuple.dst.protonum = nf_ct_protonum(master);
2509 nat_tuple.src.u = exp->saved_proto;
2511 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2512 CTA_EXPECT_NAT_TUPLE) < 0)
2513 goto nla_put_failure;
2514 nla_nest_end(skb, nest_parms);
2517 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2518 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2519 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2520 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2521 goto nla_put_failure;
2522 help = nfct_help(master);
2524 struct nf_conntrack_helper *helper;
2526 helper = rcu_dereference(help->helper);
2528 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2529 goto nla_put_failure;
2531 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2532 if (expfn != NULL &&
2533 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2534 goto nla_put_failure;
2543 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2544 int event, const struct nf_conntrack_expect *exp)
2546 struct nlmsghdr *nlh;
2547 struct nfgenmsg *nfmsg;
2548 unsigned int flags = portid ? NLM_F_MULTI : 0;
2550 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2551 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2555 nfmsg = nlmsg_data(nlh);
2556 nfmsg->nfgen_family = exp->tuple.src.l3num;
2557 nfmsg->version = NFNETLINK_V0;
2560 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2561 goto nla_put_failure;
2563 nlmsg_end(skb, nlh);
2568 nlmsg_cancel(skb, nlh);
2572 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2574 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2576 struct nf_conntrack_expect *exp = item->exp;
2577 struct net *net = nf_ct_exp_net(exp);
2578 struct nlmsghdr *nlh;
2579 struct nfgenmsg *nfmsg;
2580 struct sk_buff *skb;
2581 unsigned int type, group;
2584 if (events & (1 << IPEXP_DESTROY)) {
2585 type = IPCTNL_MSG_EXP_DELETE;
2586 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2587 } else if (events & (1 << IPEXP_NEW)) {
2588 type = IPCTNL_MSG_EXP_NEW;
2589 flags = NLM_F_CREATE|NLM_F_EXCL;
2590 group = NFNLGRP_CONNTRACK_EXP_NEW;
2594 if (!item->report && !nfnetlink_has_listeners(net, group))
2597 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2601 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2602 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2606 nfmsg = nlmsg_data(nlh);
2607 nfmsg->nfgen_family = exp->tuple.src.l3num;
2608 nfmsg->version = NFNETLINK_V0;
2612 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2613 goto nla_put_failure;
2616 nlmsg_end(skb, nlh);
2617 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2622 nlmsg_cancel(skb, nlh);
2626 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2630 static int ctnetlink_exp_done(struct netlink_callback *cb)
2633 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2638 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2640 struct net *net = sock_net(skb->sk);
2641 struct nf_conntrack_expect *exp, *last;
2642 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2643 u_int8_t l3proto = nfmsg->nfgen_family;
2646 last = (struct nf_conntrack_expect *)cb->args[1];
2647 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2649 hlist_for_each_entry(exp, &nf_ct_expect_hash[cb->args[0]],
2651 if (l3proto && exp->tuple.src.l3num != l3proto)
2654 if (!net_eq(nf_ct_net(exp->master), net))
2662 if (ctnetlink_exp_fill_info(skb,
2663 NETLINK_CB(cb->skb).portid,
2667 if (!atomic_inc_not_zero(&exp->use))
2669 cb->args[1] = (unsigned long)exp;
2681 nf_ct_expect_put(last);
2687 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2689 struct nf_conntrack_expect *exp, *last;
2690 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2691 struct nf_conn *ct = cb->data;
2692 struct nf_conn_help *help = nfct_help(ct);
2693 u_int8_t l3proto = nfmsg->nfgen_family;
2699 last = (struct nf_conntrack_expect *)cb->args[1];
2701 hlist_for_each_entry(exp, &help->expectations, lnode) {
2702 if (l3proto && exp->tuple.src.l3num != l3proto)
2709 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2713 if (!atomic_inc_not_zero(&exp->use))
2715 cb->args[1] = (unsigned long)exp;
2727 nf_ct_expect_put(last);
2732 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2733 struct sk_buff *skb,
2734 const struct nlmsghdr *nlh,
2735 const struct nlattr * const cda[])
2738 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2739 u_int8_t u3 = nfmsg->nfgen_family;
2740 struct nf_conntrack_tuple tuple;
2741 struct nf_conntrack_tuple_hash *h;
2743 struct nf_conntrack_zone zone;
2744 struct netlink_dump_control c = {
2745 .dump = ctnetlink_exp_ct_dump_table,
2746 .done = ctnetlink_exp_done,
2749 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2754 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2758 h = nf_conntrack_find_get(net, &zone, &tuple);
2762 ct = nf_ct_tuplehash_to_ctrack(h);
2765 err = netlink_dump_start(ctnl, skb, nlh, &c);
2771 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2772 struct sk_buff *skb, const struct nlmsghdr *nlh,
2773 const struct nlattr * const cda[])
2775 struct nf_conntrack_tuple tuple;
2776 struct nf_conntrack_expect *exp;
2777 struct sk_buff *skb2;
2778 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2779 u_int8_t u3 = nfmsg->nfgen_family;
2780 struct nf_conntrack_zone zone;
2783 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2784 if (cda[CTA_EXPECT_MASTER])
2785 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda);
2787 struct netlink_dump_control c = {
2788 .dump = ctnetlink_exp_dump_table,
2789 .done = ctnetlink_exp_done,
2791 return netlink_dump_start(ctnl, skb, nlh, &c);
2795 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2799 if (cda[CTA_EXPECT_TUPLE])
2800 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2802 else if (cda[CTA_EXPECT_MASTER])
2803 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2811 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2815 if (cda[CTA_EXPECT_ID]) {
2816 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2817 if (ntohl(id) != (u32)(unsigned long)exp) {
2818 nf_ct_expect_put(exp);
2824 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2826 nf_ct_expect_put(exp);
2831 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2832 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2834 nf_ct_expect_put(exp);
2838 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2847 /* this avoids a loop in nfnetlink. */
2848 return err == -EAGAIN ? -ENOBUFS : err;
2851 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2852 struct sk_buff *skb, const struct nlmsghdr *nlh,
2853 const struct nlattr * const cda[])
2855 struct nf_conntrack_expect *exp;
2856 struct nf_conntrack_tuple tuple;
2857 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2858 struct hlist_node *next;
2859 u_int8_t u3 = nfmsg->nfgen_family;
2860 struct nf_conntrack_zone zone;
2864 if (cda[CTA_EXPECT_TUPLE]) {
2865 /* delete a single expect by tuple */
2866 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2870 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2875 /* bump usage count to 2 */
2876 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2880 if (cda[CTA_EXPECT_ID]) {
2881 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2882 if (ntohl(id) != (u32)(unsigned long)exp) {
2883 nf_ct_expect_put(exp);
2888 /* after list removal, usage count == 1 */
2889 spin_lock_bh(&nf_conntrack_expect_lock);
2890 if (del_timer(&exp->timeout)) {
2891 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2893 nf_ct_expect_put(exp);
2895 spin_unlock_bh(&nf_conntrack_expect_lock);
2896 /* have to put what we 'get' above.
2897 * after this line usage count == 0 */
2898 nf_ct_expect_put(exp);
2899 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2900 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2901 struct nf_conn_help *m_help;
2903 /* delete all expectations for this helper */
2904 spin_lock_bh(&nf_conntrack_expect_lock);
2905 for (i = 0; i < nf_ct_expect_hsize; i++) {
2906 hlist_for_each_entry_safe(exp, next,
2907 &nf_ct_expect_hash[i],
2910 if (!net_eq(nf_ct_exp_net(exp), net))
2913 m_help = nfct_help(exp->master);
2914 if (!strcmp(m_help->helper->name, name) &&
2915 del_timer(&exp->timeout)) {
2916 nf_ct_unlink_expect_report(exp,
2917 NETLINK_CB(skb).portid,
2919 nf_ct_expect_put(exp);
2923 spin_unlock_bh(&nf_conntrack_expect_lock);
2925 /* This basically means we have to flush everything*/
2926 spin_lock_bh(&nf_conntrack_expect_lock);
2927 for (i = 0; i < nf_ct_expect_hsize; i++) {
2928 hlist_for_each_entry_safe(exp, next,
2929 &nf_ct_expect_hash[i],
2932 if (!net_eq(nf_ct_exp_net(exp), net))
2935 if (del_timer(&exp->timeout)) {
2936 nf_ct_unlink_expect_report(exp,
2937 NETLINK_CB(skb).portid,
2939 nf_ct_expect_put(exp);
2943 spin_unlock_bh(&nf_conntrack_expect_lock);
2949 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2950 const struct nlattr * const cda[])
2952 if (cda[CTA_EXPECT_TIMEOUT]) {
2953 if (!del_timer(&x->timeout))
2956 x->timeout.expires = jiffies +
2957 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2958 add_timer(&x->timeout);
2963 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2964 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2965 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2969 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2970 struct nf_conntrack_expect *exp,
2973 #ifdef CONFIG_NF_NAT_NEEDED
2974 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2975 struct nf_conntrack_tuple nat_tuple = {};
2978 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2982 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2985 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2986 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
2991 exp->saved_addr = nat_tuple.src.u3;
2992 exp->saved_proto = nat_tuple.src.u;
2993 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3001 static struct nf_conntrack_expect *
3002 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3003 struct nf_conntrack_helper *helper,
3004 struct nf_conntrack_tuple *tuple,
3005 struct nf_conntrack_tuple *mask)
3007 u_int32_t class = 0;
3008 struct nf_conntrack_expect *exp;
3009 struct nf_conn_help *help;
3012 if (cda[CTA_EXPECT_CLASS] && helper) {
3013 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3014 if (class > helper->expect_class_max)
3015 return ERR_PTR(-EINVAL);
3017 exp = nf_ct_expect_alloc(ct);
3019 return ERR_PTR(-ENOMEM);
3021 help = nfct_help(ct);
3023 if (!cda[CTA_EXPECT_TIMEOUT]) {
3027 exp->timeout.expires =
3028 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3030 exp->flags = NF_CT_EXPECT_USERSPACE;
3031 if (cda[CTA_EXPECT_FLAGS]) {
3033 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3036 if (cda[CTA_EXPECT_FLAGS]) {
3037 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3038 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3042 if (cda[CTA_EXPECT_FN]) {
3043 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3044 struct nf_ct_helper_expectfn *expfn;
3046 expfn = nf_ct_helper_expectfn_find_by_name(name);
3047 if (expfn == NULL) {
3051 exp->expectfn = expfn->expectfn;
3053 exp->expectfn = NULL;
3057 exp->helper = helper;
3058 exp->tuple = *tuple;
3059 exp->mask.src.u3 = mask->src.u3;
3060 exp->mask.src.u.all = mask->src.u.all;
3062 if (cda[CTA_EXPECT_NAT]) {
3063 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3064 exp, nf_ct_l3num(ct));
3070 nf_ct_expect_put(exp);
3071 return ERR_PTR(err);
3075 ctnetlink_create_expect(struct net *net,
3076 const struct nf_conntrack_zone *zone,
3077 const struct nlattr * const cda[],
3078 u_int8_t u3, u32 portid, int report)
3080 struct nf_conntrack_tuple tuple, mask, master_tuple;
3081 struct nf_conntrack_tuple_hash *h = NULL;
3082 struct nf_conntrack_helper *helper = NULL;
3083 struct nf_conntrack_expect *exp;
3087 /* caller guarantees that those three CTA_EXPECT_* exist */
3088 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3092 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3096 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3101 /* Look for master conntrack of this expectation */
3102 h = nf_conntrack_find_get(net, zone, &master_tuple);
3105 ct = nf_ct_tuplehash_to_ctrack(h);
3107 if (cda[CTA_EXPECT_HELP_NAME]) {
3108 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3110 helper = __nf_conntrack_helper_find(helpname, u3,
3111 nf_ct_protonum(ct));
3112 if (helper == NULL) {
3113 #ifdef CONFIG_MODULES
3114 if (request_module("nfct-helper-%s", helpname) < 0) {
3118 helper = __nf_conntrack_helper_find(helpname, u3,
3119 nf_ct_protonum(ct));
3130 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3136 err = nf_ct_expect_related_report(exp, portid, report);
3137 nf_ct_expect_put(exp);
3143 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3144 struct sk_buff *skb, const struct nlmsghdr *nlh,
3145 const struct nlattr * const cda[])
3147 struct nf_conntrack_tuple tuple;
3148 struct nf_conntrack_expect *exp;
3149 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3150 u_int8_t u3 = nfmsg->nfgen_family;
3151 struct nf_conntrack_zone zone;
3154 if (!cda[CTA_EXPECT_TUPLE]
3155 || !cda[CTA_EXPECT_MASK]
3156 || !cda[CTA_EXPECT_MASTER])
3159 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3163 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3168 spin_lock_bh(&nf_conntrack_expect_lock);
3169 exp = __nf_ct_expect_find(net, &zone, &tuple);
3171 spin_unlock_bh(&nf_conntrack_expect_lock);
3173 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3174 err = ctnetlink_create_expect(net, &zone, cda, u3,
3175 NETLINK_CB(skb).portid,
3182 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3183 err = ctnetlink_change_expect(exp, cda);
3184 spin_unlock_bh(&nf_conntrack_expect_lock);
3190 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3191 const struct ip_conntrack_stat *st)
3193 struct nlmsghdr *nlh;
3194 struct nfgenmsg *nfmsg;
3195 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3197 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
3198 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3202 nfmsg = nlmsg_data(nlh);
3203 nfmsg->nfgen_family = AF_UNSPEC;
3204 nfmsg->version = NFNETLINK_V0;
3205 nfmsg->res_id = htons(cpu);
3207 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3208 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3209 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3210 goto nla_put_failure;
3212 nlmsg_end(skb, nlh);
3217 nlmsg_cancel(skb, nlh);
3222 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3225 struct net *net = sock_net(skb->sk);
3227 if (cb->args[0] == nr_cpu_ids)
3230 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3231 const struct ip_conntrack_stat *st;
3233 if (!cpu_possible(cpu))
3236 st = per_cpu_ptr(net->ct.stat, cpu);
3237 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3247 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3248 struct sk_buff *skb,
3249 const struct nlmsghdr *nlh,
3250 const struct nlattr * const cda[])
3252 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3253 struct netlink_dump_control c = {
3254 .dump = ctnetlink_exp_stat_cpu_dump,
3256 return netlink_dump_start(ctnl, skb, nlh, &c);
3262 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3263 static struct nf_ct_event_notifier ctnl_notifier = {
3264 .fcn = ctnetlink_conntrack_event,
3267 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3268 .fcn = ctnetlink_expect_event,
3272 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3273 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3274 .attr_count = CTA_MAX,
3275 .policy = ct_nla_policy },
3276 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3277 .attr_count = CTA_MAX,
3278 .policy = ct_nla_policy },
3279 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3280 .attr_count = CTA_MAX,
3281 .policy = ct_nla_policy },
3282 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3283 .attr_count = CTA_MAX,
3284 .policy = ct_nla_policy },
3285 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3286 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3287 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3288 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3291 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3292 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3293 .attr_count = CTA_EXPECT_MAX,
3294 .policy = exp_nla_policy },
3295 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3296 .attr_count = CTA_EXPECT_MAX,
3297 .policy = exp_nla_policy },
3298 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3299 .attr_count = CTA_EXPECT_MAX,
3300 .policy = exp_nla_policy },
3301 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3304 static const struct nfnetlink_subsystem ctnl_subsys = {
3305 .name = "conntrack",
3306 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3307 .cb_count = IPCTNL_MSG_MAX,
3311 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3312 .name = "conntrack_expect",
3313 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3314 .cb_count = IPCTNL_MSG_EXP_MAX,
3318 MODULE_ALIAS("ip_conntrack_netlink");
3319 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3320 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3322 static int __net_init ctnetlink_net_init(struct net *net)
3324 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3327 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3329 pr_err("ctnetlink_init: cannot register notifier.\n");
3333 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3335 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3336 goto err_unreg_notifier;
3341 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3343 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3349 static void ctnetlink_net_exit(struct net *net)
3351 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3352 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3353 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3357 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3361 list_for_each_entry(net, net_exit_list, exit_list)
3362 ctnetlink_net_exit(net);
3365 static struct pernet_operations ctnetlink_net_ops = {
3366 .init = ctnetlink_net_init,
3367 .exit_batch = ctnetlink_net_exit_batch,
3370 static int __init ctnetlink_init(void)
3374 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3375 ret = nfnetlink_subsys_register(&ctnl_subsys);
3377 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3381 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3383 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3384 goto err_unreg_subsys;
3387 ret = register_pernet_subsys(&ctnetlink_net_ops);
3389 pr_err("ctnetlink_init: cannot register pernet operations\n");
3390 goto err_unreg_exp_subsys;
3392 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3393 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3394 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3398 err_unreg_exp_subsys:
3399 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3401 nfnetlink_subsys_unregister(&ctnl_subsys);
3406 static void __exit ctnetlink_exit(void)
3408 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3410 unregister_pernet_subsys(&ctnetlink_net_ops);
3411 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3412 nfnetlink_subsys_unregister(&ctnl_subsys);
3413 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3414 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3418 module_init(ctnetlink_init);
3419 module_exit(ctnetlink_exit);