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, struct nf_conn *ct)
422 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
423 struct nf_ct_seqadj *seq;
425 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
428 spin_lock_bh(&ct->lock);
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)
437 spin_unlock_bh(&ct->lock);
440 spin_unlock_bh(&ct->lock);
444 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
446 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
447 goto nla_put_failure;
454 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
456 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
457 goto nla_put_failure;
465 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
468 const struct nf_conntrack_zone *zone;
469 struct nlmsghdr *nlh;
470 struct nfgenmsg *nfmsg;
471 struct nlattr *nest_parms;
472 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
474 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_NEW);
475 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
479 nfmsg = nlmsg_data(nlh);
480 nfmsg->nfgen_family = nf_ct_l3num(ct);
481 nfmsg->version = NFNETLINK_V0;
484 zone = nf_ct_zone(ct);
486 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
488 goto nla_put_failure;
489 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
490 goto nla_put_failure;
491 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
492 NF_CT_ZONE_DIR_ORIG) < 0)
493 goto nla_put_failure;
494 nla_nest_end(skb, nest_parms);
496 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
498 goto nla_put_failure;
499 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
500 goto nla_put_failure;
501 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
502 NF_CT_ZONE_DIR_REPL) < 0)
503 goto nla_put_failure;
504 nla_nest_end(skb, nest_parms);
506 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
507 NF_CT_DEFAULT_ZONE_DIR) < 0)
508 goto nla_put_failure;
510 if (ctnetlink_dump_status(skb, ct) < 0 ||
511 ctnetlink_dump_timeout(skb, ct) < 0 ||
512 ctnetlink_dump_acct(skb, ct, type) < 0 ||
513 ctnetlink_dump_timestamp(skb, ct) < 0 ||
514 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
515 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
516 ctnetlink_dump_mark(skb, ct) < 0 ||
517 ctnetlink_dump_secctx(skb, ct) < 0 ||
518 ctnetlink_dump_labels(skb, ct) < 0 ||
519 ctnetlink_dump_id(skb, ct) < 0 ||
520 ctnetlink_dump_use(skb, ct) < 0 ||
521 ctnetlink_dump_master(skb, ct) < 0 ||
522 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
523 goto nla_put_failure;
530 nlmsg_cancel(skb, nlh);
534 static inline size_t ctnetlink_proto_size(const struct nf_conn *ct)
536 struct nf_conntrack_l3proto *l3proto;
537 struct nf_conntrack_l4proto *l4proto;
541 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
542 len += l3proto->nla_size;
544 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
545 len += l4proto->nla_size;
551 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
553 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
555 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
556 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
557 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
561 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
563 #ifdef CONFIG_NF_CONNTRACK_SECMARK
566 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
570 return nla_total_size(0) /* CTA_SECCTX */
571 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
577 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
579 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
580 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
582 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
588 #ifdef CONFIG_NF_CONNTRACK_EVENTS
589 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
591 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
592 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
593 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
594 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
595 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
596 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
597 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
598 + ctnetlink_acct_size(ct)
599 + ctnetlink_timestamp_size(ct)
600 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
601 + nla_total_size(0) /* CTA_PROTOINFO */
602 + nla_total_size(0) /* CTA_HELP */
603 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
604 + ctnetlink_secctx_size(ct)
605 #ifdef CONFIG_NF_NAT_NEEDED
606 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
607 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
609 #ifdef CONFIG_NF_CONNTRACK_MARK
610 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
612 #ifdef CONFIG_NF_CONNTRACK_ZONES
613 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
615 + ctnetlink_proto_size(ct)
616 + ctnetlink_label_size(ct)
621 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
623 const struct nf_conntrack_zone *zone;
625 struct nlmsghdr *nlh;
626 struct nfgenmsg *nfmsg;
627 struct nlattr *nest_parms;
628 struct nf_conn *ct = item->ct;
631 unsigned int flags = 0, group;
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_msg_type(NFNL_SUBSYS_CTNETLINK, type);
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, 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, NULL);
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,
946 if (!tb[CTA_PROTO_NUM])
948 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
951 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
953 if (likely(l4proto->nlattr_to_tuple)) {
954 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
955 l4proto->nla_policy, NULL);
957 ret = l4proto->nlattr_to_tuple(tb, tuple);
966 ctnetlink_parse_zone(const struct nlattr *attr,
967 struct nf_conntrack_zone *zone)
969 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
970 NF_CT_DEFAULT_ZONE_DIR, 0);
971 #ifdef CONFIG_NF_CONNTRACK_ZONES
973 zone->id = ntohs(nla_get_be16(attr));
982 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
983 struct nf_conntrack_zone *zone)
987 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
990 ret = ctnetlink_parse_zone(attr, zone);
994 if (type == CTA_TUPLE_REPLY)
995 zone->dir = NF_CT_ZONE_DIR_REPL;
997 zone->dir = NF_CT_ZONE_DIR_ORIG;
1002 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1003 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1004 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1005 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1009 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1010 struct nf_conntrack_tuple *tuple,
1011 enum ctattr_type type, u_int8_t l3num,
1012 struct nf_conntrack_zone *zone)
1014 struct nlattr *tb[CTA_TUPLE_MAX+1];
1017 memset(tuple, 0, sizeof(*tuple));
1019 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy,
1024 if (!tb[CTA_TUPLE_IP])
1027 tuple->src.l3num = l3num;
1029 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1033 if (!tb[CTA_TUPLE_PROTO])
1036 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1040 if (tb[CTA_TUPLE_ZONE]) {
1044 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1050 /* orig and expect tuples get DIR_ORIGINAL */
1051 if (type == CTA_TUPLE_REPLY)
1052 tuple->dst.dir = IP_CT_DIR_REPLY;
1054 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1059 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1060 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1061 .len = NF_CT_HELPER_NAME_LEN - 1 },
1064 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1065 struct nlattr **helpinfo)
1068 struct nlattr *tb[CTA_HELP_MAX+1];
1070 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy, NULL);
1074 if (!tb[CTA_HELP_NAME])
1077 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1079 if (tb[CTA_HELP_INFO])
1080 *helpinfo = tb[CTA_HELP_INFO];
1085 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1086 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1087 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1088 [CTA_STATUS] = { .type = NLA_U32 },
1089 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1090 [CTA_HELP] = { .type = NLA_NESTED },
1091 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1092 [CTA_TIMEOUT] = { .type = NLA_U32 },
1093 [CTA_MARK] = { .type = NLA_U32 },
1094 [CTA_ID] = { .type = NLA_U32 },
1095 [CTA_NAT_DST] = { .type = NLA_NESTED },
1096 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1097 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1098 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1099 [CTA_ZONE] = { .type = NLA_U16 },
1100 [CTA_MARK_MASK] = { .type = NLA_U32 },
1101 [CTA_LABELS] = { .type = NLA_BINARY,
1102 .len = NF_CT_LABELS_MAX_SIZE },
1103 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1104 .len = NF_CT_LABELS_MAX_SIZE },
1107 static int ctnetlink_flush_conntrack(struct net *net,
1108 const struct nlattr * const cda[],
1109 u32 portid, int report)
1111 struct ctnetlink_filter *filter = NULL;
1113 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1114 filter = ctnetlink_alloc_filter(cda);
1116 return PTR_ERR(filter);
1119 nf_ct_iterate_cleanup(net, ctnetlink_filter_match, filter,
1126 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1127 struct sk_buff *skb,
1128 const struct nlmsghdr *nlh,
1129 const struct nlattr * const cda[])
1131 struct nf_conntrack_tuple_hash *h;
1132 struct nf_conntrack_tuple tuple;
1134 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1135 u_int8_t u3 = nfmsg->nfgen_family;
1136 struct nf_conntrack_zone zone;
1139 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1143 if (cda[CTA_TUPLE_ORIG])
1144 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1146 else if (cda[CTA_TUPLE_REPLY])
1147 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1150 return ctnetlink_flush_conntrack(net, cda,
1151 NETLINK_CB(skb).portid,
1158 h = nf_conntrack_find_get(net, &zone, &tuple);
1162 ct = nf_ct_tuplehash_to_ctrack(h);
1165 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1166 if (id != (u32)(unsigned long)ct) {
1172 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1178 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1179 struct sk_buff *skb,
1180 const struct nlmsghdr *nlh,
1181 const struct nlattr * const cda[])
1183 struct nf_conntrack_tuple_hash *h;
1184 struct nf_conntrack_tuple tuple;
1186 struct sk_buff *skb2 = NULL;
1187 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1188 u_int8_t u3 = nfmsg->nfgen_family;
1189 struct nf_conntrack_zone zone;
1192 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1193 struct netlink_dump_control c = {
1194 .dump = ctnetlink_dump_table,
1195 .done = ctnetlink_done,
1198 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1199 struct ctnetlink_filter *filter;
1201 filter = ctnetlink_alloc_filter(cda);
1203 return PTR_ERR(filter);
1207 return netlink_dump_start(ctnl, skb, nlh, &c);
1210 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1214 if (cda[CTA_TUPLE_ORIG])
1215 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1217 else if (cda[CTA_TUPLE_REPLY])
1218 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1226 h = nf_conntrack_find_get(net, &zone, &tuple);
1230 ct = nf_ct_tuplehash_to_ctrack(h);
1233 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1240 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1241 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1247 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1256 /* this avoids a loop in nfnetlink. */
1257 return err == -EAGAIN ? -ENOBUFS : err;
1260 static int ctnetlink_done_list(struct netlink_callback *cb)
1263 nf_ct_put((struct nf_conn *)cb->args[1]);
1268 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1270 struct nf_conn *ct, *last;
1271 struct nf_conntrack_tuple_hash *h;
1272 struct hlist_nulls_node *n;
1273 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1274 u_int8_t l3proto = nfmsg->nfgen_family;
1277 struct hlist_nulls_head *list;
1278 struct net *net = sock_net(skb->sk);
1283 last = (struct nf_conn *)cb->args[1];
1285 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1286 struct ct_pcpu *pcpu;
1288 if (!cpu_possible(cpu))
1291 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1292 spin_lock_bh(&pcpu->lock);
1293 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1295 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1296 ct = nf_ct_tuplehash_to_ctrack(h);
1297 if (l3proto && nf_ct_l3num(ct) != l3proto)
1305 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1307 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1311 if (!atomic_inc_not_zero(&ct->ct_general.use))
1314 cb->args[1] = (unsigned long)ct;
1315 spin_unlock_bh(&pcpu->lock);
1323 spin_unlock_bh(&pcpu->lock);
1334 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1336 return ctnetlink_dump_list(skb, cb, true);
1339 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1340 struct sk_buff *skb,
1341 const struct nlmsghdr *nlh,
1342 const struct nlattr * const cda[])
1344 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1345 struct netlink_dump_control c = {
1346 .dump = ctnetlink_dump_dying,
1347 .done = ctnetlink_done_list,
1349 return netlink_dump_start(ctnl, skb, nlh, &c);
1356 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1358 return ctnetlink_dump_list(skb, cb, false);
1361 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1362 struct sk_buff *skb,
1363 const struct nlmsghdr *nlh,
1364 const struct nlattr * const cda[])
1366 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1367 struct netlink_dump_control c = {
1368 .dump = ctnetlink_dump_unconfirmed,
1369 .done = ctnetlink_done_list,
1371 return netlink_dump_start(ctnl, skb, nlh, &c);
1377 #ifdef CONFIG_NF_NAT_NEEDED
1379 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1380 enum nf_nat_manip_type manip,
1381 const struct nlattr *attr)
1383 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1386 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1387 if (!parse_nat_setup) {
1388 #ifdef CONFIG_MODULES
1390 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1391 if (request_module("nf-nat") < 0) {
1392 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1396 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1398 if (nfnetlink_parse_nat_setup_hook)
1404 err = parse_nat_setup(ct, manip, attr);
1405 if (err == -EAGAIN) {
1406 #ifdef CONFIG_MODULES
1408 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1409 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1410 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1414 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1425 __ctnetlink_change_status(struct nf_conn *ct, unsigned long on,
1430 /* Ignore these unchangable bits */
1431 on &= ~IPS_UNCHANGEABLE_MASK;
1432 off &= ~IPS_UNCHANGEABLE_MASK;
1434 for (bit = 0; bit < __IPS_MAX_BIT; bit++) {
1435 if (on & (1 << bit))
1436 set_bit(bit, &ct->status);
1437 else if (off & (1 << bit))
1438 clear_bit(bit, &ct->status);
1443 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1446 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1447 d = ct->status ^ status;
1449 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1453 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1454 /* SEEN_REPLY bit can only be set */
1457 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1458 /* ASSURED bit can only be set */
1461 __ctnetlink_change_status(ct, status, 0);
1466 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1468 #ifdef CONFIG_NF_NAT_NEEDED
1471 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1474 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1479 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1483 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1489 static int ctnetlink_change_helper(struct nf_conn *ct,
1490 const struct nlattr * const cda[])
1492 struct nf_conntrack_helper *helper;
1493 struct nf_conn_help *help = nfct_help(ct);
1494 char *helpname = NULL;
1495 struct nlattr *helpinfo = NULL;
1498 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1502 /* don't change helper of sibling connections */
1504 /* If we try to change the helper to the same thing twice,
1505 * treat the second attempt as a no-op instead of returning
1511 helper = rcu_dereference(help->helper);
1512 if (helper && !strcmp(helper->name, helpname))
1520 if (!strcmp(helpname, "")) {
1521 if (help && help->helper) {
1522 /* we had a helper before ... */
1523 nf_ct_remove_expectations(ct);
1524 RCU_INIT_POINTER(help->helper, NULL);
1531 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1532 nf_ct_protonum(ct));
1533 if (helper == NULL) {
1539 if (help->helper == helper) {
1540 /* update private helper data if allowed. */
1541 if (helper->from_nlattr)
1542 helper->from_nlattr(helpinfo, ct);
1547 /* we cannot set a helper for an existing conntrack */
1555 static int ctnetlink_change_timeout(struct nf_conn *ct,
1556 const struct nlattr * const cda[])
1558 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1560 ct->timeout = nfct_time_stamp + timeout * HZ;
1562 if (test_bit(IPS_DYING_BIT, &ct->status))
1568 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1569 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1570 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1571 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1574 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1575 const struct nlattr * const cda[])
1577 const struct nlattr *attr = cda[CTA_PROTOINFO];
1578 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1579 struct nf_conntrack_l4proto *l4proto;
1582 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy,
1588 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1589 if (l4proto->from_nlattr)
1590 err = l4proto->from_nlattr(tb, ct);
1596 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1597 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1598 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1599 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1602 static int change_seq_adj(struct nf_ct_seqadj *seq,
1603 const struct nlattr * const attr)
1606 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1608 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy, NULL);
1612 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1615 seq->correction_pos =
1616 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1618 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1621 seq->offset_before =
1622 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1624 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1628 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1634 ctnetlink_change_seq_adj(struct nf_conn *ct,
1635 const struct nlattr * const cda[])
1637 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1643 spin_lock_bh(&ct->lock);
1644 if (cda[CTA_SEQ_ADJ_ORIG]) {
1645 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1646 cda[CTA_SEQ_ADJ_ORIG]);
1650 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1653 if (cda[CTA_SEQ_ADJ_REPLY]) {
1654 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1655 cda[CTA_SEQ_ADJ_REPLY]);
1659 set_bit(IPS_SEQ_ADJUST_BIT, &ct->status);
1662 spin_unlock_bh(&ct->lock);
1665 spin_unlock_bh(&ct->lock);
1670 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1672 #ifdef CONFIG_NF_CONNTRACK_LABELS
1673 size_t len = nla_len(cda[CTA_LABELS]);
1674 const void *mask = cda[CTA_LABELS_MASK];
1676 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1680 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1681 nla_len(cda[CTA_LABELS_MASK]) != len)
1683 mask = nla_data(cda[CTA_LABELS_MASK]);
1688 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1695 ctnetlink_change_conntrack(struct nf_conn *ct,
1696 const struct nlattr * const cda[])
1700 /* only allow NAT changes and master assignation for new conntracks */
1701 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1704 if (cda[CTA_HELP]) {
1705 err = ctnetlink_change_helper(ct, cda);
1710 if (cda[CTA_TIMEOUT]) {
1711 err = ctnetlink_change_timeout(ct, cda);
1716 if (cda[CTA_STATUS]) {
1717 err = ctnetlink_change_status(ct, cda);
1722 if (cda[CTA_PROTOINFO]) {
1723 err = ctnetlink_change_protoinfo(ct, cda);
1728 #if defined(CONFIG_NF_CONNTRACK_MARK)
1730 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1733 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1734 err = ctnetlink_change_seq_adj(ct, cda);
1739 if (cda[CTA_LABELS]) {
1740 err = ctnetlink_attach_labels(ct, cda);
1748 static struct nf_conn *
1749 ctnetlink_create_conntrack(struct net *net,
1750 const struct nf_conntrack_zone *zone,
1751 const struct nlattr * const cda[],
1752 struct nf_conntrack_tuple *otuple,
1753 struct nf_conntrack_tuple *rtuple,
1758 struct nf_conntrack_helper *helper;
1759 struct nf_conn_tstamp *tstamp;
1761 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1763 return ERR_PTR(-ENOMEM);
1765 if (!cda[CTA_TIMEOUT])
1768 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1771 if (cda[CTA_HELP]) {
1772 char *helpname = NULL;
1773 struct nlattr *helpinfo = NULL;
1775 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1779 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1780 nf_ct_protonum(ct));
1781 if (helper == NULL) {
1783 #ifdef CONFIG_MODULES
1784 if (request_module("nfct-helper-%s", helpname) < 0) {
1790 helper = __nf_conntrack_helper_find(helpname,
1792 nf_ct_protonum(ct));
1802 struct nf_conn_help *help;
1804 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1809 /* set private helper data if allowed. */
1810 if (helper->from_nlattr)
1811 helper->from_nlattr(helpinfo, ct);
1813 /* not in hash table yet so not strictly necessary */
1814 RCU_INIT_POINTER(help->helper, helper);
1817 /* try an implicit helper assignation */
1818 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1823 err = ctnetlink_setup_nat(ct, cda);
1827 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1828 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1829 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1830 nf_ct_labels_ext_add(ct);
1832 /* we must add conntrack extensions before confirmation. */
1833 ct->status |= IPS_CONFIRMED;
1835 if (cda[CTA_STATUS]) {
1836 err = ctnetlink_change_status(ct, cda);
1841 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1842 err = ctnetlink_change_seq_adj(ct, cda);
1847 memset(&ct->proto, 0, sizeof(ct->proto));
1848 if (cda[CTA_PROTOINFO]) {
1849 err = ctnetlink_change_protoinfo(ct, cda);
1854 #if defined(CONFIG_NF_CONNTRACK_MARK)
1856 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1859 /* setup master conntrack: this is a confirmed expectation */
1860 if (cda[CTA_TUPLE_MASTER]) {
1861 struct nf_conntrack_tuple master;
1862 struct nf_conntrack_tuple_hash *master_h;
1863 struct nf_conn *master_ct;
1865 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1870 master_h = nf_conntrack_find_get(net, zone, &master);
1871 if (master_h == NULL) {
1875 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1876 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1877 ct->master = master_ct;
1879 tstamp = nf_conn_tstamp_find(ct);
1881 tstamp->start = ktime_get_real_ns();
1883 err = nf_conntrack_hash_check_insert(ct);
1894 nf_conntrack_free(ct);
1895 return ERR_PTR(err);
1898 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1899 struct sk_buff *skb,
1900 const struct nlmsghdr *nlh,
1901 const struct nlattr * const cda[])
1903 struct nf_conntrack_tuple otuple, rtuple;
1904 struct nf_conntrack_tuple_hash *h = NULL;
1905 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1907 u_int8_t u3 = nfmsg->nfgen_family;
1908 struct nf_conntrack_zone zone;
1911 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1915 if (cda[CTA_TUPLE_ORIG]) {
1916 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1922 if (cda[CTA_TUPLE_REPLY]) {
1923 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1929 if (cda[CTA_TUPLE_ORIG])
1930 h = nf_conntrack_find_get(net, &zone, &otuple);
1931 else if (cda[CTA_TUPLE_REPLY])
1932 h = nf_conntrack_find_get(net, &zone, &rtuple);
1936 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1937 enum ip_conntrack_events events;
1939 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1941 if (otuple.dst.protonum != rtuple.dst.protonum)
1944 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1950 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1951 events = 1 << IPCT_RELATED;
1953 events = 1 << IPCT_NEW;
1955 if (cda[CTA_LABELS] &&
1956 ctnetlink_attach_labels(ct, cda) == 0)
1957 events |= (1 << IPCT_LABEL);
1959 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1960 (1 << IPCT_ASSURED) |
1961 (1 << IPCT_HELPER) |
1962 (1 << IPCT_PROTOINFO) |
1963 (1 << IPCT_SEQADJ) |
1964 (1 << IPCT_MARK) | events,
1965 ct, NETLINK_CB(skb).portid,
1972 /* implicit 'else' */
1975 ct = nf_ct_tuplehash_to_ctrack(h);
1976 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1977 err = ctnetlink_change_conntrack(ct, cda);
1979 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1980 (1 << IPCT_ASSURED) |
1981 (1 << IPCT_HELPER) |
1983 (1 << IPCT_PROTOINFO) |
1984 (1 << IPCT_SEQADJ) |
1986 ct, NETLINK_CB(skb).portid,
1996 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1997 __u16 cpu, const struct ip_conntrack_stat *st)
1999 struct nlmsghdr *nlh;
2000 struct nfgenmsg *nfmsg;
2001 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2003 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
2004 IPCTNL_MSG_CT_GET_STATS_CPU);
2005 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2009 nfmsg = nlmsg_data(nlh);
2010 nfmsg->nfgen_family = AF_UNSPEC;
2011 nfmsg->version = NFNETLINK_V0;
2012 nfmsg->res_id = htons(cpu);
2014 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2015 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2016 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2017 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2018 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2019 htonl(st->insert_failed)) ||
2020 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2021 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2022 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2023 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2024 htonl(st->search_restart)))
2025 goto nla_put_failure;
2027 nlmsg_end(skb, nlh);
2032 nlmsg_cancel(skb, nlh);
2037 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2040 struct net *net = sock_net(skb->sk);
2042 if (cb->args[0] == nr_cpu_ids)
2045 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2046 const struct ip_conntrack_stat *st;
2048 if (!cpu_possible(cpu))
2051 st = per_cpu_ptr(net->ct.stat, cpu);
2052 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2053 NETLINK_CB(cb->skb).portid,
2063 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2064 struct sk_buff *skb,
2065 const struct nlmsghdr *nlh,
2066 const struct nlattr * const cda[])
2068 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2069 struct netlink_dump_control c = {
2070 .dump = ctnetlink_ct_stat_cpu_dump,
2072 return netlink_dump_start(ctnl, skb, nlh, &c);
2079 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2082 struct nlmsghdr *nlh;
2083 struct nfgenmsg *nfmsg;
2084 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2085 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2087 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET_STATS);
2088 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2092 nfmsg = nlmsg_data(nlh);
2093 nfmsg->nfgen_family = AF_UNSPEC;
2094 nfmsg->version = NFNETLINK_V0;
2097 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2098 goto nla_put_failure;
2100 nlmsg_end(skb, nlh);
2105 nlmsg_cancel(skb, nlh);
2109 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2110 struct sk_buff *skb, const struct nlmsghdr *nlh,
2111 const struct nlattr * const cda[])
2113 struct sk_buff *skb2;
2116 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2120 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2122 NFNL_MSG_TYPE(nlh->nlmsg_type),
2127 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2136 /* this avoids a loop in nfnetlink. */
2137 return err == -EAGAIN ? -ENOBUFS : err;
2140 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2141 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2142 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2143 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2144 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2145 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2146 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2147 .len = NF_CT_HELPER_NAME_LEN - 1 },
2148 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2149 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2150 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2151 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2152 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2155 static struct nf_conntrack_expect *
2156 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2157 struct nf_conntrack_helper *helper,
2158 struct nf_conntrack_tuple *tuple,
2159 struct nf_conntrack_tuple *mask);
2161 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2163 ctnetlink_glue_build_size(const struct nf_conn *ct)
2165 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2166 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2167 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2168 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2169 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2170 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2171 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2172 + nla_total_size(0) /* CTA_PROTOINFO */
2173 + nla_total_size(0) /* CTA_HELP */
2174 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2175 + ctnetlink_secctx_size(ct)
2176 #ifdef CONFIG_NF_NAT_NEEDED
2177 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2178 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2180 #ifdef CONFIG_NF_CONNTRACK_MARK
2181 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2183 #ifdef CONFIG_NF_CONNTRACK_ZONES
2184 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2186 + ctnetlink_proto_size(ct)
2190 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2191 enum ip_conntrack_info *ctinfo)
2193 return nf_ct_get(skb, ctinfo);
2196 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2198 const struct nf_conntrack_zone *zone;
2199 struct nlattr *nest_parms;
2202 zone = nf_ct_zone(ct);
2204 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2206 goto nla_put_failure;
2207 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2208 goto nla_put_failure;
2209 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2210 NF_CT_ZONE_DIR_ORIG) < 0)
2211 goto nla_put_failure;
2212 nla_nest_end(skb, nest_parms);
2214 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2216 goto nla_put_failure;
2217 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2218 goto nla_put_failure;
2219 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2220 NF_CT_ZONE_DIR_REPL) < 0)
2221 goto nla_put_failure;
2222 nla_nest_end(skb, nest_parms);
2224 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2225 NF_CT_DEFAULT_ZONE_DIR) < 0)
2226 goto nla_put_failure;
2228 if (ctnetlink_dump_id(skb, ct) < 0)
2229 goto nla_put_failure;
2231 if (ctnetlink_dump_status(skb, ct) < 0)
2232 goto nla_put_failure;
2234 if (ctnetlink_dump_timeout(skb, ct) < 0)
2235 goto nla_put_failure;
2237 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2238 goto nla_put_failure;
2240 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2241 goto nla_put_failure;
2243 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2244 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2245 goto nla_put_failure;
2247 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2248 goto nla_put_failure;
2250 if ((ct->status & IPS_SEQ_ADJUST) &&
2251 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2252 goto nla_put_failure;
2254 #ifdef CONFIG_NF_CONNTRACK_MARK
2255 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2256 goto nla_put_failure;
2258 if (ctnetlink_dump_labels(skb, ct) < 0)
2259 goto nla_put_failure;
2269 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2270 enum ip_conntrack_info ctinfo,
2271 u_int16_t ct_attr, u_int16_t ct_info_attr)
2273 struct nlattr *nest_parms;
2275 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2277 goto nla_put_failure;
2279 if (__ctnetlink_glue_build(skb, ct) < 0)
2280 goto nla_put_failure;
2282 nla_nest_end(skb, nest_parms);
2284 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2285 goto nla_put_failure;
2294 ctnetlink_update_status(struct nf_conn *ct, const struct nlattr * const cda[])
2296 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
2297 unsigned long d = ct->status ^ status;
2299 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
2300 /* SEEN_REPLY bit can only be set */
2303 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
2304 /* ASSURED bit can only be set */
2307 /* This check is less strict than ctnetlink_change_status()
2308 * because callers often flip IPS_EXPECTED bits when sending
2309 * an NFQA_CT attribute to the kernel. So ignore the
2310 * unchangeable bits but do not error out. Also user programs
2311 * are allowed to clear the bits that they are allowed to change.
2313 __ctnetlink_change_status(ct, status, ~status);
2318 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2322 if (cda[CTA_TIMEOUT]) {
2323 err = ctnetlink_change_timeout(ct, cda);
2327 if (cda[CTA_STATUS]) {
2328 err = ctnetlink_update_status(ct, cda);
2332 if (cda[CTA_HELP]) {
2333 err = ctnetlink_change_helper(ct, cda);
2337 if (cda[CTA_LABELS]) {
2338 err = ctnetlink_attach_labels(ct, cda);
2342 #if defined(CONFIG_NF_CONNTRACK_MARK)
2343 if (cda[CTA_MARK]) {
2344 u32 mask = 0, mark, newmark;
2345 if (cda[CTA_MARK_MASK])
2346 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2348 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2349 newmark = (ct->mark & mask) ^ mark;
2350 if (newmark != ct->mark)
2358 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2360 struct nlattr *cda[CTA_MAX+1];
2363 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy, NULL);
2367 return ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2370 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2371 const struct nf_conn *ct,
2372 struct nf_conntrack_tuple *tuple,
2373 struct nf_conntrack_tuple *mask)
2377 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2378 nf_ct_l3num(ct), NULL);
2382 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2383 nf_ct_l3num(ct), NULL);
2387 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2388 u32 portid, u32 report)
2390 struct nlattr *cda[CTA_EXPECT_MAX+1];
2391 struct nf_conntrack_tuple tuple, mask;
2392 struct nf_conntrack_helper *helper = NULL;
2393 struct nf_conntrack_expect *exp;
2396 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy,
2401 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2406 if (cda[CTA_EXPECT_HELP_NAME]) {
2407 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2409 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2410 nf_ct_protonum(ct));
2415 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2416 helper, &tuple, &mask);
2418 return PTR_ERR(exp);
2420 err = nf_ct_expect_related_report(exp, portid, report);
2421 nf_ct_expect_put(exp);
2425 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2426 enum ip_conntrack_info ctinfo, int diff)
2428 if (!(ct->status & IPS_NAT_MASK))
2431 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2434 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2435 .get_ct = ctnetlink_glue_get_ct,
2436 .build_size = ctnetlink_glue_build_size,
2437 .build = ctnetlink_glue_build,
2438 .parse = ctnetlink_glue_parse,
2439 .attach_expect = ctnetlink_glue_attach_expect,
2440 .seq_adjust = ctnetlink_glue_seqadj,
2442 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2444 /***********************************************************************
2446 ***********************************************************************/
2448 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2449 const struct nf_conntrack_tuple *tuple,
2450 enum ctattr_expect type)
2452 struct nlattr *nest_parms;
2454 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2456 goto nla_put_failure;
2457 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2458 goto nla_put_failure;
2459 nla_nest_end(skb, nest_parms);
2467 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2468 const struct nf_conntrack_tuple *tuple,
2469 const struct nf_conntrack_tuple_mask *mask)
2472 struct nf_conntrack_l3proto *l3proto;
2473 struct nf_conntrack_l4proto *l4proto;
2474 struct nf_conntrack_tuple m;
2475 struct nlattr *nest_parms;
2477 memset(&m, 0xFF, sizeof(m));
2478 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2479 m.src.u.all = mask->src.u.all;
2480 m.dst.protonum = tuple->dst.protonum;
2482 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2484 goto nla_put_failure;
2487 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2488 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2490 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2491 tuple->dst.protonum);
2492 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2496 if (unlikely(ret < 0))
2497 goto nla_put_failure;
2499 nla_nest_end(skb, nest_parms);
2507 static const union nf_inet_addr any_addr;
2510 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2511 const struct nf_conntrack_expect *exp)
2513 struct nf_conn *master = exp->master;
2514 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2515 struct nf_conn_help *help;
2516 #ifdef CONFIG_NF_NAT_NEEDED
2517 struct nlattr *nest_parms;
2518 struct nf_conntrack_tuple nat_tuple = {};
2520 struct nf_ct_helper_expectfn *expfn;
2525 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2526 goto nla_put_failure;
2527 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2528 goto nla_put_failure;
2529 if (ctnetlink_exp_dump_tuple(skb,
2530 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2531 CTA_EXPECT_MASTER) < 0)
2532 goto nla_put_failure;
2534 #ifdef CONFIG_NF_NAT_NEEDED
2535 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2536 exp->saved_proto.all) {
2537 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2539 goto nla_put_failure;
2541 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2542 goto nla_put_failure;
2544 nat_tuple.src.l3num = nf_ct_l3num(master);
2545 nat_tuple.src.u3 = exp->saved_addr;
2546 nat_tuple.dst.protonum = nf_ct_protonum(master);
2547 nat_tuple.src.u = exp->saved_proto;
2549 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2550 CTA_EXPECT_NAT_TUPLE) < 0)
2551 goto nla_put_failure;
2552 nla_nest_end(skb, nest_parms);
2555 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2556 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2557 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2558 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2559 goto nla_put_failure;
2560 help = nfct_help(master);
2562 struct nf_conntrack_helper *helper;
2564 helper = rcu_dereference(help->helper);
2566 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2567 goto nla_put_failure;
2569 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2570 if (expfn != NULL &&
2571 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2572 goto nla_put_failure;
2581 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2582 int event, const struct nf_conntrack_expect *exp)
2584 struct nlmsghdr *nlh;
2585 struct nfgenmsg *nfmsg;
2586 unsigned int flags = portid ? NLM_F_MULTI : 0;
2588 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, event);
2589 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2593 nfmsg = nlmsg_data(nlh);
2594 nfmsg->nfgen_family = exp->tuple.src.l3num;
2595 nfmsg->version = NFNETLINK_V0;
2598 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2599 goto nla_put_failure;
2601 nlmsg_end(skb, nlh);
2606 nlmsg_cancel(skb, nlh);
2610 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2612 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2614 struct nf_conntrack_expect *exp = item->exp;
2615 struct net *net = nf_ct_exp_net(exp);
2616 struct nlmsghdr *nlh;
2617 struct nfgenmsg *nfmsg;
2618 struct sk_buff *skb;
2619 unsigned int type, group;
2622 if (events & (1 << IPEXP_DESTROY)) {
2623 type = IPCTNL_MSG_EXP_DELETE;
2624 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2625 } else if (events & (1 << IPEXP_NEW)) {
2626 type = IPCTNL_MSG_EXP_NEW;
2627 flags = NLM_F_CREATE|NLM_F_EXCL;
2628 group = NFNLGRP_CONNTRACK_EXP_NEW;
2632 if (!item->report && !nfnetlink_has_listeners(net, group))
2635 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2639 type = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK_EXP, type);
2640 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2644 nfmsg = nlmsg_data(nlh);
2645 nfmsg->nfgen_family = exp->tuple.src.l3num;
2646 nfmsg->version = NFNETLINK_V0;
2650 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2651 goto nla_put_failure;
2654 nlmsg_end(skb, nlh);
2655 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2660 nlmsg_cancel(skb, nlh);
2664 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2668 static int ctnetlink_exp_done(struct netlink_callback *cb)
2671 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2676 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2678 struct net *net = sock_net(skb->sk);
2679 struct nf_conntrack_expect *exp, *last;
2680 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2681 u_int8_t l3proto = nfmsg->nfgen_family;
2684 last = (struct nf_conntrack_expect *)cb->args[1];
2685 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2687 hlist_for_each_entry_rcu(exp, &nf_ct_expect_hash[cb->args[0]],
2689 if (l3proto && exp->tuple.src.l3num != l3proto)
2692 if (!net_eq(nf_ct_net(exp->master), net))
2700 if (ctnetlink_exp_fill_info(skb,
2701 NETLINK_CB(cb->skb).portid,
2705 if (!refcount_inc_not_zero(&exp->use))
2707 cb->args[1] = (unsigned long)exp;
2719 nf_ct_expect_put(last);
2725 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2727 struct nf_conntrack_expect *exp, *last;
2728 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2729 struct nf_conn *ct = cb->data;
2730 struct nf_conn_help *help = nfct_help(ct);
2731 u_int8_t l3proto = nfmsg->nfgen_family;
2737 last = (struct nf_conntrack_expect *)cb->args[1];
2739 hlist_for_each_entry_rcu(exp, &help->expectations, lnode) {
2740 if (l3proto && exp->tuple.src.l3num != l3proto)
2747 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2751 if (!refcount_inc_not_zero(&exp->use))
2753 cb->args[1] = (unsigned long)exp;
2765 nf_ct_expect_put(last);
2770 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2771 struct sk_buff *skb,
2772 const struct nlmsghdr *nlh,
2773 const struct nlattr * const cda[])
2776 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2777 u_int8_t u3 = nfmsg->nfgen_family;
2778 struct nf_conntrack_tuple tuple;
2779 struct nf_conntrack_tuple_hash *h;
2781 struct nf_conntrack_zone zone;
2782 struct netlink_dump_control c = {
2783 .dump = ctnetlink_exp_ct_dump_table,
2784 .done = ctnetlink_exp_done,
2787 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2792 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2796 h = nf_conntrack_find_get(net, &zone, &tuple);
2800 ct = nf_ct_tuplehash_to_ctrack(h);
2801 /* No expectation linked to this connection tracking. */
2802 if (!nfct_help(ct)) {
2809 err = netlink_dump_start(ctnl, skb, nlh, &c);
2815 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2816 struct sk_buff *skb, const struct nlmsghdr *nlh,
2817 const struct nlattr * const cda[])
2819 struct nf_conntrack_tuple tuple;
2820 struct nf_conntrack_expect *exp;
2821 struct sk_buff *skb2;
2822 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2823 u_int8_t u3 = nfmsg->nfgen_family;
2824 struct nf_conntrack_zone zone;
2827 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2828 if (cda[CTA_EXPECT_MASTER])
2829 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda);
2831 struct netlink_dump_control c = {
2832 .dump = ctnetlink_exp_dump_table,
2833 .done = ctnetlink_exp_done,
2835 return netlink_dump_start(ctnl, skb, nlh, &c);
2839 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2843 if (cda[CTA_EXPECT_TUPLE])
2844 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2846 else if (cda[CTA_EXPECT_MASTER])
2847 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2855 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2859 if (cda[CTA_EXPECT_ID]) {
2860 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2861 if (ntohl(id) != (u32)(unsigned long)exp) {
2862 nf_ct_expect_put(exp);
2868 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2870 nf_ct_expect_put(exp);
2875 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2876 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2878 nf_ct_expect_put(exp);
2882 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2891 /* this avoids a loop in nfnetlink. */
2892 return err == -EAGAIN ? -ENOBUFS : err;
2895 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2896 struct sk_buff *skb, const struct nlmsghdr *nlh,
2897 const struct nlattr * const cda[])
2899 struct nf_conntrack_expect *exp;
2900 struct nf_conntrack_tuple tuple;
2901 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2902 struct hlist_node *next;
2903 u_int8_t u3 = nfmsg->nfgen_family;
2904 struct nf_conntrack_zone zone;
2908 if (cda[CTA_EXPECT_TUPLE]) {
2909 /* delete a single expect by tuple */
2910 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2914 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2919 /* bump usage count to 2 */
2920 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2924 if (cda[CTA_EXPECT_ID]) {
2925 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2926 if (ntohl(id) != (u32)(unsigned long)exp) {
2927 nf_ct_expect_put(exp);
2932 /* after list removal, usage count == 1 */
2933 spin_lock_bh(&nf_conntrack_expect_lock);
2934 if (del_timer(&exp->timeout)) {
2935 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2937 nf_ct_expect_put(exp);
2939 spin_unlock_bh(&nf_conntrack_expect_lock);
2940 /* have to put what we 'get' above.
2941 * after this line usage count == 0 */
2942 nf_ct_expect_put(exp);
2943 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2944 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2945 struct nf_conn_help *m_help;
2947 /* delete all expectations for this helper */
2948 spin_lock_bh(&nf_conntrack_expect_lock);
2949 for (i = 0; i < nf_ct_expect_hsize; i++) {
2950 hlist_for_each_entry_safe(exp, next,
2951 &nf_ct_expect_hash[i],
2954 if (!net_eq(nf_ct_exp_net(exp), net))
2957 m_help = nfct_help(exp->master);
2958 if (!strcmp(m_help->helper->name, name) &&
2959 del_timer(&exp->timeout)) {
2960 nf_ct_unlink_expect_report(exp,
2961 NETLINK_CB(skb).portid,
2963 nf_ct_expect_put(exp);
2967 spin_unlock_bh(&nf_conntrack_expect_lock);
2969 /* This basically means we have to flush everything*/
2970 spin_lock_bh(&nf_conntrack_expect_lock);
2971 for (i = 0; i < nf_ct_expect_hsize; i++) {
2972 hlist_for_each_entry_safe(exp, next,
2973 &nf_ct_expect_hash[i],
2976 if (!net_eq(nf_ct_exp_net(exp), net))
2979 if (del_timer(&exp->timeout)) {
2980 nf_ct_unlink_expect_report(exp,
2981 NETLINK_CB(skb).portid,
2983 nf_ct_expect_put(exp);
2987 spin_unlock_bh(&nf_conntrack_expect_lock);
2993 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2994 const struct nlattr * const cda[])
2996 if (cda[CTA_EXPECT_TIMEOUT]) {
2997 if (!del_timer(&x->timeout))
3000 x->timeout.expires = jiffies +
3001 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3002 add_timer(&x->timeout);
3007 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
3008 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
3009 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
3013 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3014 struct nf_conntrack_expect *exp,
3017 #ifdef CONFIG_NF_NAT_NEEDED
3018 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3019 struct nf_conntrack_tuple nat_tuple = {};
3022 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr,
3023 exp_nat_nla_policy, NULL);
3027 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3030 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3031 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3036 exp->saved_addr = nat_tuple.src.u3;
3037 exp->saved_proto = nat_tuple.src.u;
3038 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3046 static struct nf_conntrack_expect *
3047 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3048 struct nf_conntrack_helper *helper,
3049 struct nf_conntrack_tuple *tuple,
3050 struct nf_conntrack_tuple *mask)
3052 u_int32_t class = 0;
3053 struct nf_conntrack_expect *exp;
3054 struct nf_conn_help *help;
3057 help = nfct_help(ct);
3059 return ERR_PTR(-EOPNOTSUPP);
3061 if (cda[CTA_EXPECT_CLASS] && helper) {
3062 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3063 if (class > helper->expect_class_max)
3064 return ERR_PTR(-EINVAL);
3066 exp = nf_ct_expect_alloc(ct);
3068 return ERR_PTR(-ENOMEM);
3070 if (cda[CTA_EXPECT_FLAGS]) {
3071 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3072 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3076 if (cda[CTA_EXPECT_FN]) {
3077 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3078 struct nf_ct_helper_expectfn *expfn;
3080 expfn = nf_ct_helper_expectfn_find_by_name(name);
3081 if (expfn == NULL) {
3085 exp->expectfn = expfn->expectfn;
3087 exp->expectfn = NULL;
3091 exp->helper = helper;
3092 exp->tuple = *tuple;
3093 exp->mask.src.u3 = mask->src.u3;
3094 exp->mask.src.u.all = mask->src.u.all;
3096 if (cda[CTA_EXPECT_NAT]) {
3097 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3098 exp, nf_ct_l3num(ct));
3104 nf_ct_expect_put(exp);
3105 return ERR_PTR(err);
3109 ctnetlink_create_expect(struct net *net,
3110 const struct nf_conntrack_zone *zone,
3111 const struct nlattr * const cda[],
3112 u_int8_t u3, u32 portid, int report)
3114 struct nf_conntrack_tuple tuple, mask, master_tuple;
3115 struct nf_conntrack_tuple_hash *h = NULL;
3116 struct nf_conntrack_helper *helper = NULL;
3117 struct nf_conntrack_expect *exp;
3121 /* caller guarantees that those three CTA_EXPECT_* exist */
3122 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3126 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3130 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3135 /* Look for master conntrack of this expectation */
3136 h = nf_conntrack_find_get(net, zone, &master_tuple);
3139 ct = nf_ct_tuplehash_to_ctrack(h);
3142 if (cda[CTA_EXPECT_HELP_NAME]) {
3143 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3145 helper = __nf_conntrack_helper_find(helpname, u3,
3146 nf_ct_protonum(ct));
3147 if (helper == NULL) {
3149 #ifdef CONFIG_MODULES
3150 if (request_module("nfct-helper-%s", helpname) < 0) {
3155 helper = __nf_conntrack_helper_find(helpname, u3,
3156 nf_ct_protonum(ct));
3168 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3174 err = nf_ct_expect_related_report(exp, portid, report);
3175 nf_ct_expect_put(exp);
3183 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3184 struct sk_buff *skb, const struct nlmsghdr *nlh,
3185 const struct nlattr * const cda[])
3187 struct nf_conntrack_tuple tuple;
3188 struct nf_conntrack_expect *exp;
3189 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3190 u_int8_t u3 = nfmsg->nfgen_family;
3191 struct nf_conntrack_zone zone;
3194 if (!cda[CTA_EXPECT_TUPLE]
3195 || !cda[CTA_EXPECT_MASK]
3196 || !cda[CTA_EXPECT_MASTER])
3199 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3203 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3208 spin_lock_bh(&nf_conntrack_expect_lock);
3209 exp = __nf_ct_expect_find(net, &zone, &tuple);
3211 spin_unlock_bh(&nf_conntrack_expect_lock);
3213 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3214 err = ctnetlink_create_expect(net, &zone, cda, u3,
3215 NETLINK_CB(skb).portid,
3222 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3223 err = ctnetlink_change_expect(exp, cda);
3224 spin_unlock_bh(&nf_conntrack_expect_lock);
3230 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3231 const struct ip_conntrack_stat *st)
3233 struct nlmsghdr *nlh;
3234 struct nfgenmsg *nfmsg;
3235 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3237 event = nfnl_msg_type(NFNL_SUBSYS_CTNETLINK,
3238 IPCTNL_MSG_EXP_GET_STATS_CPU);
3239 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3243 nfmsg = nlmsg_data(nlh);
3244 nfmsg->nfgen_family = AF_UNSPEC;
3245 nfmsg->version = NFNETLINK_V0;
3246 nfmsg->res_id = htons(cpu);
3248 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3249 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3250 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3251 goto nla_put_failure;
3253 nlmsg_end(skb, nlh);
3258 nlmsg_cancel(skb, nlh);
3263 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3266 struct net *net = sock_net(skb->sk);
3268 if (cb->args[0] == nr_cpu_ids)
3271 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3272 const struct ip_conntrack_stat *st;
3274 if (!cpu_possible(cpu))
3277 st = per_cpu_ptr(net->ct.stat, cpu);
3278 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3288 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3289 struct sk_buff *skb,
3290 const struct nlmsghdr *nlh,
3291 const struct nlattr * const cda[])
3293 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3294 struct netlink_dump_control c = {
3295 .dump = ctnetlink_exp_stat_cpu_dump,
3297 return netlink_dump_start(ctnl, skb, nlh, &c);
3303 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3304 static struct nf_ct_event_notifier ctnl_notifier = {
3305 .fcn = ctnetlink_conntrack_event,
3308 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3309 .fcn = ctnetlink_expect_event,
3313 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3314 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3315 .attr_count = CTA_MAX,
3316 .policy = ct_nla_policy },
3317 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3318 .attr_count = CTA_MAX,
3319 .policy = ct_nla_policy },
3320 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3321 .attr_count = CTA_MAX,
3322 .policy = ct_nla_policy },
3323 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3324 .attr_count = CTA_MAX,
3325 .policy = ct_nla_policy },
3326 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3327 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3328 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3329 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3332 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3333 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3334 .attr_count = CTA_EXPECT_MAX,
3335 .policy = exp_nla_policy },
3336 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3337 .attr_count = CTA_EXPECT_MAX,
3338 .policy = exp_nla_policy },
3339 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3340 .attr_count = CTA_EXPECT_MAX,
3341 .policy = exp_nla_policy },
3342 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3345 static const struct nfnetlink_subsystem ctnl_subsys = {
3346 .name = "conntrack",
3347 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3348 .cb_count = IPCTNL_MSG_MAX,
3352 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3353 .name = "conntrack_expect",
3354 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3355 .cb_count = IPCTNL_MSG_EXP_MAX,
3359 MODULE_ALIAS("ip_conntrack_netlink");
3360 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3361 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3363 static int __net_init ctnetlink_net_init(struct net *net)
3365 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3368 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3370 pr_err("ctnetlink_init: cannot register notifier.\n");
3374 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3376 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3377 goto err_unreg_notifier;
3382 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3384 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3390 static void ctnetlink_net_exit(struct net *net)
3392 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3393 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3394 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3398 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3402 list_for_each_entry(net, net_exit_list, exit_list)
3403 ctnetlink_net_exit(net);
3406 static struct pernet_operations ctnetlink_net_ops = {
3407 .init = ctnetlink_net_init,
3408 .exit_batch = ctnetlink_net_exit_batch,
3411 static int __init ctnetlink_init(void)
3415 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3416 ret = nfnetlink_subsys_register(&ctnl_subsys);
3418 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3422 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3424 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3425 goto err_unreg_subsys;
3428 ret = register_pernet_subsys(&ctnetlink_net_ops);
3430 pr_err("ctnetlink_init: cannot register pernet operations\n");
3431 goto err_unreg_exp_subsys;
3433 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3434 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3435 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3439 err_unreg_exp_subsys:
3440 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3442 nfnetlink_subsys_unregister(&ctnl_subsys);
3447 static void __exit ctnetlink_exit(void)
3449 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3451 unregister_pernet_subsys(&ctnetlink_net_ops);
3452 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3453 nfnetlink_subsys_unregister(&ctnl_subsys);
3454 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3455 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3460 module_init(ctnetlink_init);
3461 module_exit(ctnetlink_exit);