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>
32 #include <linux/siphash.h>
34 #include <linux/netfilter.h>
35 #include <net/netlink.h>
37 #include <net/netfilter/nf_conntrack.h>
38 #include <net/netfilter/nf_conntrack_core.h>
39 #include <net/netfilter/nf_conntrack_expect.h>
40 #include <net/netfilter/nf_conntrack_helper.h>
41 #include <net/netfilter/nf_conntrack_seqadj.h>
42 #include <net/netfilter/nf_conntrack_l3proto.h>
43 #include <net/netfilter/nf_conntrack_l4proto.h>
44 #include <net/netfilter/nf_conntrack_tuple.h>
45 #include <net/netfilter/nf_conntrack_acct.h>
46 #include <net/netfilter/nf_conntrack_zones.h>
47 #include <net/netfilter/nf_conntrack_timestamp.h>
48 #include <net/netfilter/nf_conntrack_labels.h>
49 #include <net/netfilter/nf_conntrack_seqadj.h>
50 #include <net/netfilter/nf_conntrack_synproxy.h>
51 #ifdef CONFIG_NF_NAT_NEEDED
52 #include <net/netfilter/nf_nat_core.h>
53 #include <net/netfilter/nf_nat_l4proto.h>
54 #include <net/netfilter/nf_nat_helper.h>
57 #include <linux/netfilter/nfnetlink.h>
58 #include <linux/netfilter/nfnetlink_conntrack.h>
60 MODULE_LICENSE("GPL");
62 static char __initdata version[] = "0.93";
64 static int ctnetlink_dump_tuples_proto(struct sk_buff *skb,
65 const struct nf_conntrack_tuple *tuple,
66 struct nf_conntrack_l4proto *l4proto)
69 struct nlattr *nest_parms;
71 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
74 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
77 if (likely(l4proto->tuple_to_nlattr))
78 ret = l4proto->tuple_to_nlattr(skb, tuple);
80 nla_nest_end(skb, nest_parms);
88 static int ctnetlink_dump_tuples_ip(struct sk_buff *skb,
89 const struct nf_conntrack_tuple *tuple,
90 struct nf_conntrack_l3proto *l3proto)
93 struct nlattr *nest_parms;
95 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
99 if (likely(l3proto->tuple_to_nlattr))
100 ret = l3proto->tuple_to_nlattr(skb, tuple);
102 nla_nest_end(skb, nest_parms);
110 static int ctnetlink_dump_tuples(struct sk_buff *skb,
111 const struct nf_conntrack_tuple *tuple)
114 struct nf_conntrack_l3proto *l3proto;
115 struct nf_conntrack_l4proto *l4proto;
118 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
119 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
122 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
123 tuple->dst.protonum);
124 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
130 static int ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
131 const struct nf_conntrack_zone *zone, int dir)
133 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
135 if (nla_put_be16(skb, attrtype, htons(zone->id)))
136 goto nla_put_failure;
143 static int ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
145 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
146 goto nla_put_failure;
153 static int ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
155 long timeout = nf_ct_expires(ct) / HZ;
157 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
158 goto nla_put_failure;
165 static int ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
167 struct nf_conntrack_l4proto *l4proto;
168 struct nlattr *nest_proto;
171 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
172 if (!l4proto->to_nlattr)
175 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
177 goto nla_put_failure;
179 ret = l4proto->to_nlattr(skb, nest_proto, ct);
181 nla_nest_end(skb, nest_proto);
189 static int ctnetlink_dump_helpinfo(struct sk_buff *skb,
190 const struct nf_conn *ct)
192 struct nlattr *nest_helper;
193 const struct nf_conn_help *help = nfct_help(ct);
194 struct nf_conntrack_helper *helper;
199 helper = rcu_dereference(help->helper);
203 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
205 goto nla_put_failure;
206 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
207 goto nla_put_failure;
209 if (helper->to_nlattr)
210 helper->to_nlattr(skb, ct);
212 nla_nest_end(skb, nest_helper);
221 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
222 enum ip_conntrack_dir dir, int type)
224 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
225 struct nf_conn_counter *counter = acct->counter;
226 struct nlattr *nest_count;
229 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
230 pkts = atomic64_xchg(&counter[dir].packets, 0);
231 bytes = atomic64_xchg(&counter[dir].bytes, 0);
233 pkts = atomic64_read(&counter[dir].packets);
234 bytes = atomic64_read(&counter[dir].bytes);
237 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
239 goto nla_put_failure;
241 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts),
243 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes),
245 goto nla_put_failure;
247 nla_nest_end(skb, nest_count);
256 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
258 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
263 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
265 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
272 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
274 struct nlattr *nest_count;
275 const struct nf_conn_tstamp *tstamp;
277 tstamp = nf_conn_tstamp_find(ct);
281 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
283 goto nla_put_failure;
285 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start),
286 CTA_TIMESTAMP_PAD) ||
287 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
288 cpu_to_be64(tstamp->stop),
290 goto nla_put_failure;
291 nla_nest_end(skb, nest_count);
299 #ifdef CONFIG_NF_CONNTRACK_MARK
300 static int ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
302 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
303 goto nla_put_failure;
310 #define ctnetlink_dump_mark(a, b) (0)
313 #ifdef CONFIG_NF_CONNTRACK_SECMARK
314 static int ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
316 struct nlattr *nest_secctx;
320 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
325 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
327 goto nla_put_failure;
329 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
330 goto nla_put_failure;
331 nla_nest_end(skb, nest_secctx);
335 security_release_secctx(secctx, len);
339 #define ctnetlink_dump_secctx(a, b) (0)
342 #ifdef CONFIG_NF_CONNTRACK_LABELS
343 static inline int ctnetlink_label_size(const struct nf_conn *ct)
345 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
349 return nla_total_size(sizeof(labels->bits));
353 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
355 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
363 if (labels->bits[i] != 0)
364 return nla_put(skb, CTA_LABELS, sizeof(labels->bits),
367 } while (i < ARRAY_SIZE(labels->bits));
372 #define ctnetlink_dump_labels(a, b) (0)
373 #define ctnetlink_label_size(a) (0)
376 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
378 static int ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
380 struct nlattr *nest_parms;
382 if (!(ct->status & IPS_EXPECTED))
385 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
387 goto nla_put_failure;
388 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
389 goto nla_put_failure;
390 nla_nest_end(skb, nest_parms);
399 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
401 struct nlattr *nest_parms;
403 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
405 goto nla_put_failure;
407 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
408 htonl(seq->correction_pos)) ||
409 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
410 htonl(seq->offset_before)) ||
411 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
412 htonl(seq->offset_after)))
413 goto nla_put_failure;
415 nla_nest_end(skb, nest_parms);
423 static int ctnetlink_dump_ct_seq_adj(struct sk_buff *skb,
424 const struct nf_conn *ct)
426 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
427 struct nf_ct_seqadj *seq;
429 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
432 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
433 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
436 seq = &seqadj->seq[IP_CT_DIR_REPLY];
437 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
443 static int ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
445 __be32 id = (__force __be32)nf_ct_get_id(ct);
447 if (nla_put_be32(skb, CTA_ID, id))
448 goto nla_put_failure;
455 static int ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
457 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
458 goto nla_put_failure;
466 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
469 const struct nf_conntrack_zone *zone;
470 struct nlmsghdr *nlh;
471 struct nfgenmsg *nfmsg;
472 struct nlattr *nest_parms;
473 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
475 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
476 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
480 nfmsg = nlmsg_data(nlh);
481 nfmsg->nfgen_family = nf_ct_l3num(ct);
482 nfmsg->version = NFNETLINK_V0;
485 zone = nf_ct_zone(ct);
487 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
489 goto nla_put_failure;
490 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
491 goto nla_put_failure;
492 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
493 NF_CT_ZONE_DIR_ORIG) < 0)
494 goto nla_put_failure;
495 nla_nest_end(skb, nest_parms);
497 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
499 goto nla_put_failure;
500 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
501 goto nla_put_failure;
502 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
503 NF_CT_ZONE_DIR_REPL) < 0)
504 goto nla_put_failure;
505 nla_nest_end(skb, nest_parms);
507 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
508 NF_CT_DEFAULT_ZONE_DIR) < 0)
509 goto nla_put_failure;
511 if (ctnetlink_dump_status(skb, ct) < 0 ||
512 ctnetlink_dump_timeout(skb, ct) < 0 ||
513 ctnetlink_dump_acct(skb, ct, type) < 0 ||
514 ctnetlink_dump_timestamp(skb, ct) < 0 ||
515 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
516 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
517 ctnetlink_dump_mark(skb, ct) < 0 ||
518 ctnetlink_dump_secctx(skb, ct) < 0 ||
519 ctnetlink_dump_labels(skb, ct) < 0 ||
520 ctnetlink_dump_id(skb, ct) < 0 ||
521 ctnetlink_dump_use(skb, ct) < 0 ||
522 ctnetlink_dump_master(skb, ct) < 0 ||
523 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
524 goto nla_put_failure;
531 nlmsg_cancel(skb, nlh);
535 static inline size_t ctnetlink_proto_size(const struct nf_conn *ct)
537 struct nf_conntrack_l3proto *l3proto;
538 struct nf_conntrack_l4proto *l4proto;
542 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
543 len += l3proto->nla_size;
545 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
546 len += l4proto->nla_size;
552 static inline size_t ctnetlink_acct_size(const struct nf_conn *ct)
554 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
556 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
557 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
558 + 2 * nla_total_size_64bit(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
562 static inline int ctnetlink_secctx_size(const struct nf_conn *ct)
564 #ifdef CONFIG_NF_CONNTRACK_SECMARK
567 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
571 return nla_total_size(0) /* CTA_SECCTX */
572 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
578 static inline size_t ctnetlink_timestamp_size(const struct nf_conn *ct)
580 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
581 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
583 return nla_total_size(0) + 2 * nla_total_size_64bit(sizeof(uint64_t));
589 #ifdef CONFIG_NF_CONNTRACK_EVENTS
590 static size_t ctnetlink_nlmsg_size(const struct nf_conn *ct)
592 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
593 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
594 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
595 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
596 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
597 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
598 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
599 + ctnetlink_acct_size(ct)
600 + ctnetlink_timestamp_size(ct)
601 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
602 + nla_total_size(0) /* CTA_PROTOINFO */
603 + nla_total_size(0) /* CTA_HELP */
604 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
605 + ctnetlink_secctx_size(ct)
606 #ifdef CONFIG_NF_NAT_NEEDED
607 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
608 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
610 #ifdef CONFIG_NF_CONNTRACK_MARK
611 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
613 #ifdef CONFIG_NF_CONNTRACK_ZONES
614 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
616 + ctnetlink_proto_size(ct)
617 + ctnetlink_label_size(ct)
622 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
624 const struct nf_conntrack_zone *zone;
626 struct nlmsghdr *nlh;
627 struct nfgenmsg *nfmsg;
628 struct nlattr *nest_parms;
629 struct nf_conn *ct = item->ct;
632 unsigned int flags = 0, group;
635 /* ignore our fake conntrack entry */
636 if (nf_ct_is_untracked(ct))
639 if (events & (1 << IPCT_DESTROY)) {
640 type = IPCTNL_MSG_CT_DELETE;
641 group = NFNLGRP_CONNTRACK_DESTROY;
642 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
643 type = IPCTNL_MSG_CT_NEW;
644 flags = NLM_F_CREATE|NLM_F_EXCL;
645 group = NFNLGRP_CONNTRACK_NEW;
647 type = IPCTNL_MSG_CT_NEW;
648 group = NFNLGRP_CONNTRACK_UPDATE;
653 if (!item->report && !nfnetlink_has_listeners(net, group))
656 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
660 type |= NFNL_SUBSYS_CTNETLINK << 8;
661 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
665 nfmsg = nlmsg_data(nlh);
666 nfmsg->nfgen_family = nf_ct_l3num(ct);
667 nfmsg->version = NFNETLINK_V0;
671 zone = nf_ct_zone(ct);
673 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
675 goto nla_put_failure;
676 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
677 goto nla_put_failure;
678 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
679 NF_CT_ZONE_DIR_ORIG) < 0)
680 goto nla_put_failure;
681 nla_nest_end(skb, nest_parms);
683 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
685 goto nla_put_failure;
686 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
687 goto nla_put_failure;
688 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
689 NF_CT_ZONE_DIR_REPL) < 0)
690 goto nla_put_failure;
691 nla_nest_end(skb, nest_parms);
693 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
694 NF_CT_DEFAULT_ZONE_DIR) < 0)
695 goto nla_put_failure;
697 if (ctnetlink_dump_id(skb, ct) < 0)
698 goto nla_put_failure;
700 if (ctnetlink_dump_status(skb, ct) < 0)
701 goto nla_put_failure;
703 if (events & (1 << IPCT_DESTROY)) {
704 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
705 ctnetlink_dump_timestamp(skb, ct) < 0)
706 goto nla_put_failure;
708 if (ctnetlink_dump_timeout(skb, ct) < 0)
709 goto nla_put_failure;
711 if (events & (1 << IPCT_PROTOINFO)
712 && ctnetlink_dump_protoinfo(skb, ct) < 0)
713 goto nla_put_failure;
715 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
716 && ctnetlink_dump_helpinfo(skb, ct) < 0)
717 goto nla_put_failure;
719 #ifdef CONFIG_NF_CONNTRACK_SECMARK
720 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
721 && ctnetlink_dump_secctx(skb, ct) < 0)
722 goto nla_put_failure;
724 if (events & (1 << IPCT_LABEL) &&
725 ctnetlink_dump_labels(skb, ct) < 0)
726 goto nla_put_failure;
728 if (events & (1 << IPCT_RELATED) &&
729 ctnetlink_dump_master(skb, ct) < 0)
730 goto nla_put_failure;
732 if (events & (1 << IPCT_SEQADJ) &&
733 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
734 goto nla_put_failure;
737 #ifdef CONFIG_NF_CONNTRACK_MARK
738 if ((events & (1 << IPCT_MARK) || ct->mark)
739 && ctnetlink_dump_mark(skb, ct) < 0)
740 goto nla_put_failure;
745 err = nfnetlink_send(skb, net, item->portid, group, item->report,
747 if (err == -ENOBUFS || err == -EAGAIN)
754 nlmsg_cancel(skb, nlh);
758 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
763 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
765 static int ctnetlink_done(struct netlink_callback *cb)
768 nf_ct_put((struct nf_conn *)cb->args[1]);
773 struct ctnetlink_filter {
780 static struct ctnetlink_filter *
781 ctnetlink_alloc_filter(const struct nlattr * const cda[])
783 #ifdef CONFIG_NF_CONNTRACK_MARK
784 struct ctnetlink_filter *filter;
786 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
788 return ERR_PTR(-ENOMEM);
790 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
791 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
795 return ERR_PTR(-EOPNOTSUPP);
799 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
801 struct ctnetlink_filter *filter = data;
806 #ifdef CONFIG_NF_CONNTRACK_MARK
807 if ((ct->mark & filter->mark.mask) == filter->mark.val)
815 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
817 struct net *net = sock_net(skb->sk);
818 struct nf_conn *ct, *last;
819 struct nf_conntrack_tuple_hash *h;
820 struct hlist_nulls_node *n;
821 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
822 u_int8_t l3proto = nfmsg->nfgen_family;
823 struct nf_conn *nf_ct_evict[8];
827 last = (struct nf_conn *)cb->args[1];
831 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
835 if (nf_ct_should_gc(nf_ct_evict[i]))
836 nf_ct_kill(nf_ct_evict[i]);
837 nf_ct_put(nf_ct_evict[i]);
840 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
841 nf_conntrack_lock(lockp);
842 if (cb->args[0] >= nf_conntrack_htable_size) {
846 hlist_nulls_for_each_entry(h, n, &nf_conntrack_hash[cb->args[0]],
848 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
850 ct = nf_ct_tuplehash_to_ctrack(h);
851 if (nf_ct_is_expired(ct)) {
852 if (i < ARRAY_SIZE(nf_ct_evict) &&
853 atomic_inc_not_zero(&ct->ct_general.use))
854 nf_ct_evict[i++] = ct;
858 if (!net_eq(net, nf_ct_net(ct)))
861 /* Dump entries of a given L3 protocol number.
862 * If it is not specified, ie. l3proto == 0,
863 * then dump everything. */
864 if (l3proto && nf_ct_l3num(ct) != l3proto)
871 if (!ctnetlink_filter_match(ct, cb->data))
876 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
878 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
882 nf_conntrack_get(&ct->ct_general);
883 cb->args[1] = (unsigned long)ct;
897 /* nf ct hash resize happened, now clear the leftover. */
898 if ((struct nf_conn *)cb->args[1] == last)
906 if (nf_ct_should_gc(nf_ct_evict[i]))
907 nf_ct_kill(nf_ct_evict[i]);
908 nf_ct_put(nf_ct_evict[i]);
914 static int ctnetlink_parse_tuple_ip(struct nlattr *attr,
915 struct nf_conntrack_tuple *tuple)
917 struct nlattr *tb[CTA_IP_MAX+1];
918 struct nf_conntrack_l3proto *l3proto;
921 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
926 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
928 if (likely(l3proto->nlattr_to_tuple)) {
929 ret = nla_validate_nested(attr, CTA_IP_MAX,
930 l3proto->nla_policy);
932 ret = l3proto->nlattr_to_tuple(tb, tuple);
940 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
941 [CTA_PROTO_NUM] = { .type = NLA_U8 },
944 static int ctnetlink_parse_tuple_proto(struct nlattr *attr,
945 struct nf_conntrack_tuple *tuple)
947 struct nlattr *tb[CTA_PROTO_MAX+1];
948 struct nf_conntrack_l4proto *l4proto;
951 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
955 if (!tb[CTA_PROTO_NUM])
957 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
960 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
962 if (likely(l4proto->nlattr_to_tuple)) {
963 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
964 l4proto->nla_policy);
966 ret = l4proto->nlattr_to_tuple(tb, tuple);
975 ctnetlink_parse_zone(const struct nlattr *attr,
976 struct nf_conntrack_zone *zone)
978 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
979 NF_CT_DEFAULT_ZONE_DIR, 0);
980 #ifdef CONFIG_NF_CONNTRACK_ZONES
982 zone->id = ntohs(nla_get_be16(attr));
991 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
992 struct nf_conntrack_zone *zone)
996 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
999 ret = ctnetlink_parse_zone(attr, zone);
1003 if (type == CTA_TUPLE_REPLY)
1004 zone->dir = NF_CT_ZONE_DIR_REPL;
1006 zone->dir = NF_CT_ZONE_DIR_ORIG;
1011 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
1012 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
1013 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
1014 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
1018 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1019 struct nf_conntrack_tuple *tuple, u32 type,
1020 u_int8_t l3num, struct nf_conntrack_zone *zone)
1022 struct nlattr *tb[CTA_TUPLE_MAX+1];
1025 memset(tuple, 0, sizeof(*tuple));
1027 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
1031 if (!tb[CTA_TUPLE_IP])
1034 if (l3num != NFPROTO_IPV4 && l3num != NFPROTO_IPV6)
1036 tuple->src.l3num = l3num;
1038 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1042 if (!tb[CTA_TUPLE_PROTO])
1045 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1049 if (tb[CTA_TUPLE_ZONE]) {
1053 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1059 /* orig and expect tuples get DIR_ORIGINAL */
1060 if (type == CTA_TUPLE_REPLY)
1061 tuple->dst.dir = IP_CT_DIR_REPLY;
1063 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1068 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1069 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1070 .len = NF_CT_HELPER_NAME_LEN - 1 },
1073 static int ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1074 struct nlattr **helpinfo)
1077 struct nlattr *tb[CTA_HELP_MAX+1];
1079 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
1083 if (!tb[CTA_HELP_NAME])
1086 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1088 if (tb[CTA_HELP_INFO])
1089 *helpinfo = tb[CTA_HELP_INFO];
1094 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1095 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1096 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1097 [CTA_STATUS] = { .type = NLA_U32 },
1098 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1099 [CTA_HELP] = { .type = NLA_NESTED },
1100 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1101 [CTA_TIMEOUT] = { .type = NLA_U32 },
1102 [CTA_MARK] = { .type = NLA_U32 },
1103 [CTA_ID] = { .type = NLA_U32 },
1104 [CTA_NAT_DST] = { .type = NLA_NESTED },
1105 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1106 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1107 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1108 [CTA_ZONE] = { .type = NLA_U16 },
1109 [CTA_MARK_MASK] = { .type = NLA_U32 },
1110 [CTA_LABELS] = { .type = NLA_BINARY,
1111 .len = NF_CT_LABELS_MAX_SIZE },
1112 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1113 .len = NF_CT_LABELS_MAX_SIZE },
1116 static int ctnetlink_flush_conntrack(struct net *net,
1117 const struct nlattr * const cda[],
1118 u32 portid, int report)
1120 struct ctnetlink_filter *filter = NULL;
1122 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1123 filter = ctnetlink_alloc_filter(cda);
1125 return PTR_ERR(filter);
1128 nf_ct_iterate_cleanup(net, ctnetlink_filter_match, filter,
1135 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1136 struct sk_buff *skb,
1137 const struct nlmsghdr *nlh,
1138 const struct nlattr * const cda[])
1140 struct nf_conntrack_tuple_hash *h;
1141 struct nf_conntrack_tuple tuple;
1143 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1144 u_int8_t u3 = nfmsg->nfgen_family;
1145 struct nf_conntrack_zone zone;
1148 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1152 if (cda[CTA_TUPLE_ORIG])
1153 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1155 else if (cda[CTA_TUPLE_REPLY])
1156 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1159 return ctnetlink_flush_conntrack(net, cda,
1160 NETLINK_CB(skb).portid,
1167 h = nf_conntrack_find_get(net, &zone, &tuple);
1171 ct = nf_ct_tuplehash_to_ctrack(h);
1174 __be32 id = nla_get_be32(cda[CTA_ID]);
1176 if (id != (__force __be32)nf_ct_get_id(ct)) {
1182 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1188 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1189 struct sk_buff *skb,
1190 const struct nlmsghdr *nlh,
1191 const struct nlattr * const cda[])
1193 struct nf_conntrack_tuple_hash *h;
1194 struct nf_conntrack_tuple tuple;
1196 struct sk_buff *skb2 = NULL;
1197 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1198 u_int8_t u3 = nfmsg->nfgen_family;
1199 struct nf_conntrack_zone zone;
1202 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1203 struct netlink_dump_control c = {
1204 .dump = ctnetlink_dump_table,
1205 .done = ctnetlink_done,
1208 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1209 struct ctnetlink_filter *filter;
1211 filter = ctnetlink_alloc_filter(cda);
1213 return PTR_ERR(filter);
1217 return netlink_dump_start(ctnl, skb, nlh, &c);
1220 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1224 if (cda[CTA_TUPLE_ORIG])
1225 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1227 else if (cda[CTA_TUPLE_REPLY])
1228 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1236 h = nf_conntrack_find_get(net, &zone, &tuple);
1240 ct = nf_ct_tuplehash_to_ctrack(h);
1243 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1250 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1251 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1257 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1266 /* this avoids a loop in nfnetlink. */
1267 return err == -EAGAIN ? -ENOBUFS : err;
1270 static int ctnetlink_done_list(struct netlink_callback *cb)
1273 nf_ct_put((struct nf_conn *)cb->args[1]);
1278 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1280 struct nf_conn *ct, *last;
1281 struct nf_conntrack_tuple_hash *h;
1282 struct hlist_nulls_node *n;
1283 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1284 u_int8_t l3proto = nfmsg->nfgen_family;
1287 struct hlist_nulls_head *list;
1288 struct net *net = sock_net(skb->sk);
1293 last = (struct nf_conn *)cb->args[1];
1295 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1296 struct ct_pcpu *pcpu;
1298 if (!cpu_possible(cpu))
1301 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1302 spin_lock_bh(&pcpu->lock);
1303 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1305 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1306 ct = nf_ct_tuplehash_to_ctrack(h);
1307 if (l3proto && nf_ct_l3num(ct) != l3proto)
1315 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1317 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1321 if (!atomic_inc_not_zero(&ct->ct_general.use))
1324 cb->args[1] = (unsigned long)ct;
1325 spin_unlock_bh(&pcpu->lock);
1333 spin_unlock_bh(&pcpu->lock);
1344 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1346 return ctnetlink_dump_list(skb, cb, true);
1349 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1350 struct sk_buff *skb,
1351 const struct nlmsghdr *nlh,
1352 const struct nlattr * const cda[])
1354 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1355 struct netlink_dump_control c = {
1356 .dump = ctnetlink_dump_dying,
1357 .done = ctnetlink_done_list,
1359 return netlink_dump_start(ctnl, skb, nlh, &c);
1366 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1368 return ctnetlink_dump_list(skb, cb, false);
1371 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1372 struct sk_buff *skb,
1373 const struct nlmsghdr *nlh,
1374 const struct nlattr * const cda[])
1376 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1377 struct netlink_dump_control c = {
1378 .dump = ctnetlink_dump_unconfirmed,
1379 .done = ctnetlink_done_list,
1381 return netlink_dump_start(ctnl, skb, nlh, &c);
1387 #ifdef CONFIG_NF_NAT_NEEDED
1389 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1390 enum nf_nat_manip_type manip,
1391 const struct nlattr *attr)
1393 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1396 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1397 if (!parse_nat_setup) {
1398 #ifdef CONFIG_MODULES
1400 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1401 if (request_module("nf-nat") < 0) {
1402 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1406 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1408 if (nfnetlink_parse_nat_setup_hook)
1414 err = parse_nat_setup(ct, manip, attr);
1415 if (err == -EAGAIN) {
1416 #ifdef CONFIG_MODULES
1418 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1419 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1420 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1424 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1435 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1438 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1439 d = ct->status ^ status;
1441 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1445 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1446 /* SEEN_REPLY bit can only be set */
1449 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1450 /* ASSURED bit can only be set */
1453 /* Be careful here, modifying NAT bits can screw up things,
1454 * so don't let users modify them directly if they don't pass
1456 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1461 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1463 #ifdef CONFIG_NF_NAT_NEEDED
1466 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1469 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1474 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1478 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1484 static int ctnetlink_change_helper(struct nf_conn *ct,
1485 const struct nlattr * const cda[])
1487 struct nf_conntrack_helper *helper;
1488 struct nf_conn_help *help = nfct_help(ct);
1489 char *helpname = NULL;
1490 struct nlattr *helpinfo = NULL;
1493 /* don't change helper of sibling connections */
1497 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1501 if (!strcmp(helpname, "")) {
1502 if (help && help->helper) {
1503 /* we had a helper before ... */
1504 nf_ct_remove_expectations(ct);
1505 RCU_INIT_POINTER(help->helper, NULL);
1511 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1512 nf_ct_protonum(ct));
1513 if (helper == NULL) {
1514 #ifdef CONFIG_MODULES
1515 spin_unlock_bh(&nf_conntrack_expect_lock);
1517 if (request_module("nfct-helper-%s", helpname) < 0) {
1518 spin_lock_bh(&nf_conntrack_expect_lock);
1522 spin_lock_bh(&nf_conntrack_expect_lock);
1523 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1524 nf_ct_protonum(ct));
1532 if (help->helper == helper) {
1533 /* update private helper data if allowed. */
1534 if (helper->from_nlattr)
1535 helper->from_nlattr(helpinfo, ct);
1541 /* we cannot set a helper for an existing conntrack */
1545 static int ctnetlink_change_timeout(struct nf_conn *ct,
1546 const struct nlattr * const cda[])
1548 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1550 ct->timeout = nfct_time_stamp + timeout * HZ;
1552 if (test_bit(IPS_DYING_BIT, &ct->status))
1558 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1559 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1560 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1561 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1564 static int ctnetlink_change_protoinfo(struct nf_conn *ct,
1565 const struct nlattr * const cda[])
1567 const struct nlattr *attr = cda[CTA_PROTOINFO];
1568 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1569 struct nf_conntrack_l4proto *l4proto;
1572 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1577 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1578 if (l4proto->from_nlattr)
1579 err = l4proto->from_nlattr(tb, ct);
1585 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1586 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1587 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1588 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1591 static int change_seq_adj(struct nf_ct_seqadj *seq,
1592 const struct nlattr * const attr)
1595 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1597 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy);
1601 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1604 seq->correction_pos =
1605 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1607 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1610 seq->offset_before =
1611 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1613 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1617 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1623 ctnetlink_change_seq_adj(struct nf_conn *ct,
1624 const struct nlattr * const cda[])
1626 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1632 if (cda[CTA_SEQ_ADJ_ORIG]) {
1633 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1634 cda[CTA_SEQ_ADJ_ORIG]);
1638 ct->status |= IPS_SEQ_ADJUST;
1641 if (cda[CTA_SEQ_ADJ_REPLY]) {
1642 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1643 cda[CTA_SEQ_ADJ_REPLY]);
1647 ct->status |= IPS_SEQ_ADJUST;
1654 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1656 #ifdef CONFIG_NF_CONNTRACK_LABELS
1657 size_t len = nla_len(cda[CTA_LABELS]);
1658 const void *mask = cda[CTA_LABELS_MASK];
1660 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1664 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1665 nla_len(cda[CTA_LABELS_MASK]) != len)
1667 mask = nla_data(cda[CTA_LABELS_MASK]);
1672 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1679 ctnetlink_change_conntrack(struct nf_conn *ct,
1680 const struct nlattr * const cda[])
1684 /* only allow NAT changes and master assignation for new conntracks */
1685 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1688 if (cda[CTA_HELP]) {
1689 err = ctnetlink_change_helper(ct, cda);
1694 if (cda[CTA_TIMEOUT]) {
1695 err = ctnetlink_change_timeout(ct, cda);
1700 if (cda[CTA_STATUS]) {
1701 err = ctnetlink_change_status(ct, cda);
1706 if (cda[CTA_PROTOINFO]) {
1707 err = ctnetlink_change_protoinfo(ct, cda);
1712 #if defined(CONFIG_NF_CONNTRACK_MARK)
1714 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1717 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1718 err = ctnetlink_change_seq_adj(ct, cda);
1723 if (cda[CTA_LABELS]) {
1724 err = ctnetlink_attach_labels(ct, cda);
1732 static struct nf_conn *
1733 ctnetlink_create_conntrack(struct net *net,
1734 const struct nf_conntrack_zone *zone,
1735 const struct nlattr * const cda[],
1736 struct nf_conntrack_tuple *otuple,
1737 struct nf_conntrack_tuple *rtuple,
1742 struct nf_conntrack_helper *helper;
1743 struct nf_conn_tstamp *tstamp;
1745 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1747 return ERR_PTR(-ENOMEM);
1749 if (!cda[CTA_TIMEOUT])
1752 ct->timeout = nfct_time_stamp + ntohl(nla_get_be32(cda[CTA_TIMEOUT])) * HZ;
1755 if (cda[CTA_HELP]) {
1756 char *helpname = NULL;
1757 struct nlattr *helpinfo = NULL;
1759 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1763 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1764 nf_ct_protonum(ct));
1765 if (helper == NULL) {
1767 #ifdef CONFIG_MODULES
1768 if (request_module("nfct-helper-%s", helpname) < 0) {
1774 helper = __nf_conntrack_helper_find(helpname,
1776 nf_ct_protonum(ct));
1786 struct nf_conn_help *help;
1788 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1793 /* set private helper data if allowed. */
1794 if (helper->from_nlattr)
1795 helper->from_nlattr(helpinfo, ct);
1797 /* not in hash table yet so not strictly necessary */
1798 RCU_INIT_POINTER(help->helper, helper);
1801 /* try an implicit helper assignation */
1802 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1807 err = ctnetlink_setup_nat(ct, cda);
1811 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1812 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1813 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1814 nf_ct_labels_ext_add(ct);
1815 nfct_seqadj_ext_add(ct);
1816 nfct_synproxy_ext_add(ct);
1818 /* we must add conntrack extensions before confirmation. */
1819 ct->status |= IPS_CONFIRMED;
1821 if (cda[CTA_STATUS]) {
1822 err = ctnetlink_change_status(ct, cda);
1827 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1828 err = ctnetlink_change_seq_adj(ct, cda);
1833 memset(&ct->proto, 0, sizeof(ct->proto));
1834 if (cda[CTA_PROTOINFO]) {
1835 err = ctnetlink_change_protoinfo(ct, cda);
1840 #if defined(CONFIG_NF_CONNTRACK_MARK)
1842 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1845 /* setup master conntrack: this is a confirmed expectation */
1846 if (cda[CTA_TUPLE_MASTER]) {
1847 struct nf_conntrack_tuple master;
1848 struct nf_conntrack_tuple_hash *master_h;
1849 struct nf_conn *master_ct;
1851 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1856 master_h = nf_conntrack_find_get(net, zone, &master);
1857 if (master_h == NULL) {
1861 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1862 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1863 ct->master = master_ct;
1865 tstamp = nf_conn_tstamp_find(ct);
1867 tstamp->start = ktime_get_real_ns();
1869 err = nf_conntrack_hash_check_insert(ct);
1880 nf_conntrack_free(ct);
1881 return ERR_PTR(err);
1884 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1885 struct sk_buff *skb,
1886 const struct nlmsghdr *nlh,
1887 const struct nlattr * const cda[])
1889 struct nf_conntrack_tuple otuple, rtuple;
1890 struct nf_conntrack_tuple_hash *h = NULL;
1891 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1893 u_int8_t u3 = nfmsg->nfgen_family;
1894 struct nf_conntrack_zone zone;
1897 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1901 if (cda[CTA_TUPLE_ORIG]) {
1902 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1908 if (cda[CTA_TUPLE_REPLY]) {
1909 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1915 if (cda[CTA_TUPLE_ORIG])
1916 h = nf_conntrack_find_get(net, &zone, &otuple);
1917 else if (cda[CTA_TUPLE_REPLY])
1918 h = nf_conntrack_find_get(net, &zone, &rtuple);
1922 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1923 enum ip_conntrack_events events;
1925 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1927 if (otuple.dst.protonum != rtuple.dst.protonum)
1930 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1936 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1937 events = IPCT_RELATED;
1941 if (cda[CTA_LABELS] &&
1942 ctnetlink_attach_labels(ct, cda) == 0)
1943 events |= (1 << IPCT_LABEL);
1945 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1946 (1 << IPCT_ASSURED) |
1947 (1 << IPCT_HELPER) |
1948 (1 << IPCT_PROTOINFO) |
1949 (1 << IPCT_SEQADJ) |
1950 (1 << IPCT_MARK) | events,
1951 ct, NETLINK_CB(skb).portid,
1958 /* implicit 'else' */
1961 ct = nf_ct_tuplehash_to_ctrack(h);
1962 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1963 spin_lock_bh(&nf_conntrack_expect_lock);
1964 err = ctnetlink_change_conntrack(ct, cda);
1965 spin_unlock_bh(&nf_conntrack_expect_lock);
1967 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1968 (1 << IPCT_ASSURED) |
1969 (1 << IPCT_HELPER) |
1971 (1 << IPCT_PROTOINFO) |
1972 (1 << IPCT_SEQADJ) |
1974 ct, NETLINK_CB(skb).portid,
1984 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1985 __u16 cpu, const struct ip_conntrack_stat *st)
1987 struct nlmsghdr *nlh;
1988 struct nfgenmsg *nfmsg;
1989 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1991 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1992 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1996 nfmsg = nlmsg_data(nlh);
1997 nfmsg->nfgen_family = AF_UNSPEC;
1998 nfmsg->version = NFNETLINK_V0;
1999 nfmsg->res_id = htons(cpu);
2001 if (nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
2002 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
2003 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
2004 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
2005 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
2006 htonl(st->insert_failed)) ||
2007 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
2008 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
2009 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
2010 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
2011 htonl(st->search_restart)))
2012 goto nla_put_failure;
2014 nlmsg_end(skb, nlh);
2019 nlmsg_cancel(skb, nlh);
2024 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2027 struct net *net = sock_net(skb->sk);
2029 if (cb->args[0] == nr_cpu_ids)
2032 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2033 const struct ip_conntrack_stat *st;
2035 if (!cpu_possible(cpu))
2038 st = per_cpu_ptr(net->ct.stat, cpu);
2039 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2040 NETLINK_CB(cb->skb).portid,
2050 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2051 struct sk_buff *skb,
2052 const struct nlmsghdr *nlh,
2053 const struct nlattr * const cda[])
2055 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2056 struct netlink_dump_control c = {
2057 .dump = ctnetlink_ct_stat_cpu_dump,
2059 return netlink_dump_start(ctnl, skb, nlh, &c);
2066 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2069 struct nlmsghdr *nlh;
2070 struct nfgenmsg *nfmsg;
2071 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2072 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2074 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
2075 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2079 nfmsg = nlmsg_data(nlh);
2080 nfmsg->nfgen_family = AF_UNSPEC;
2081 nfmsg->version = NFNETLINK_V0;
2084 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2085 goto nla_put_failure;
2087 nlmsg_end(skb, nlh);
2092 nlmsg_cancel(skb, nlh);
2096 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2097 struct sk_buff *skb, const struct nlmsghdr *nlh,
2098 const struct nlattr * const cda[])
2100 struct sk_buff *skb2;
2103 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2107 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2109 NFNL_MSG_TYPE(nlh->nlmsg_type),
2114 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2123 /* this avoids a loop in nfnetlink. */
2124 return err == -EAGAIN ? -ENOBUFS : err;
2127 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2128 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2129 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2130 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2131 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2132 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2133 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2134 .len = NF_CT_HELPER_NAME_LEN - 1 },
2135 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2136 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2137 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2138 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2139 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2142 static struct nf_conntrack_expect *
2143 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2144 struct nf_conntrack_helper *helper,
2145 struct nf_conntrack_tuple *tuple,
2146 struct nf_conntrack_tuple *mask);
2148 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2150 ctnetlink_glue_build_size(const struct nf_conn *ct)
2152 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2153 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2154 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2155 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2156 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2157 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2158 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2159 + nla_total_size(0) /* CTA_PROTOINFO */
2160 + nla_total_size(0) /* CTA_HELP */
2161 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2162 + ctnetlink_secctx_size(ct)
2163 #ifdef CONFIG_NF_NAT_NEEDED
2164 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2165 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2167 #ifdef CONFIG_NF_CONNTRACK_MARK
2168 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2170 #ifdef CONFIG_NF_CONNTRACK_ZONES
2171 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2173 + ctnetlink_proto_size(ct)
2177 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2178 enum ip_conntrack_info *ctinfo)
2182 ct = nf_ct_get(skb, ctinfo);
2183 if (ct && nf_ct_is_untracked(ct))
2189 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2191 const struct nf_conntrack_zone *zone;
2192 struct nlattr *nest_parms;
2195 zone = nf_ct_zone(ct);
2197 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2199 goto nla_put_failure;
2200 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2201 goto nla_put_failure;
2202 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2203 NF_CT_ZONE_DIR_ORIG) < 0)
2204 goto nla_put_failure;
2205 nla_nest_end(skb, nest_parms);
2207 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2209 goto nla_put_failure;
2210 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2211 goto nla_put_failure;
2212 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2213 NF_CT_ZONE_DIR_REPL) < 0)
2214 goto nla_put_failure;
2215 nla_nest_end(skb, nest_parms);
2217 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2218 NF_CT_DEFAULT_ZONE_DIR) < 0)
2219 goto nla_put_failure;
2221 if (ctnetlink_dump_id(skb, ct) < 0)
2222 goto nla_put_failure;
2224 if (ctnetlink_dump_status(skb, ct) < 0)
2225 goto nla_put_failure;
2227 if (ctnetlink_dump_timeout(skb, ct) < 0)
2228 goto nla_put_failure;
2230 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2231 goto nla_put_failure;
2233 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2234 goto nla_put_failure;
2236 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2237 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2238 goto nla_put_failure;
2240 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2241 goto nla_put_failure;
2243 if ((ct->status & IPS_SEQ_ADJUST) &&
2244 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2245 goto nla_put_failure;
2247 #ifdef CONFIG_NF_CONNTRACK_MARK
2248 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2249 goto nla_put_failure;
2251 if (ctnetlink_dump_labels(skb, ct) < 0)
2252 goto nla_put_failure;
2262 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2263 enum ip_conntrack_info ctinfo,
2264 u_int16_t ct_attr, u_int16_t ct_info_attr)
2266 struct nlattr *nest_parms;
2268 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2270 goto nla_put_failure;
2272 if (__ctnetlink_glue_build(skb, ct) < 0)
2273 goto nla_put_failure;
2275 nla_nest_end(skb, nest_parms);
2277 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2278 goto nla_put_failure;
2287 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2291 if (cda[CTA_TIMEOUT]) {
2292 err = ctnetlink_change_timeout(ct, cda);
2296 if (cda[CTA_STATUS]) {
2297 err = ctnetlink_change_status(ct, cda);
2301 if (cda[CTA_HELP]) {
2302 err = ctnetlink_change_helper(ct, cda);
2306 if (cda[CTA_LABELS]) {
2307 err = ctnetlink_attach_labels(ct, cda);
2311 #if defined(CONFIG_NF_CONNTRACK_MARK)
2312 if (cda[CTA_MARK]) {
2313 u32 mask = 0, mark, newmark;
2314 if (cda[CTA_MARK_MASK])
2315 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2317 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2318 newmark = (ct->mark & mask) ^ mark;
2319 if (newmark != ct->mark)
2327 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2329 struct nlattr *cda[CTA_MAX+1];
2332 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2336 spin_lock_bh(&nf_conntrack_expect_lock);
2337 ret = ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2338 spin_unlock_bh(&nf_conntrack_expect_lock);
2343 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2344 const struct nf_conn *ct,
2345 struct nf_conntrack_tuple *tuple,
2346 struct nf_conntrack_tuple *mask)
2350 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2351 nf_ct_l3num(ct), NULL);
2355 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2356 nf_ct_l3num(ct), NULL);
2360 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2361 u32 portid, u32 report)
2363 struct nlattr *cda[CTA_EXPECT_MAX+1];
2364 struct nf_conntrack_tuple tuple, mask;
2365 struct nf_conntrack_helper *helper = NULL;
2366 struct nf_conntrack_expect *exp;
2369 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy);
2373 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2378 if (cda[CTA_EXPECT_HELP_NAME]) {
2379 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2381 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2382 nf_ct_protonum(ct));
2387 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2388 helper, &tuple, &mask);
2390 return PTR_ERR(exp);
2392 err = nf_ct_expect_related_report(exp, portid, report);
2393 nf_ct_expect_put(exp);
2397 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2398 enum ip_conntrack_info ctinfo, int diff)
2400 if (!(ct->status & IPS_NAT_MASK))
2403 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2406 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2407 .get_ct = ctnetlink_glue_get_ct,
2408 .build_size = ctnetlink_glue_build_size,
2409 .build = ctnetlink_glue_build,
2410 .parse = ctnetlink_glue_parse,
2411 .attach_expect = ctnetlink_glue_attach_expect,
2412 .seq_adjust = ctnetlink_glue_seqadj,
2414 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2416 /***********************************************************************
2418 ***********************************************************************/
2420 static int ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2421 const struct nf_conntrack_tuple *tuple,
2424 struct nlattr *nest_parms;
2426 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2428 goto nla_put_failure;
2429 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2430 goto nla_put_failure;
2431 nla_nest_end(skb, nest_parms);
2439 static int ctnetlink_exp_dump_mask(struct sk_buff *skb,
2440 const struct nf_conntrack_tuple *tuple,
2441 const struct nf_conntrack_tuple_mask *mask)
2444 struct nf_conntrack_l3proto *l3proto;
2445 struct nf_conntrack_l4proto *l4proto;
2446 struct nf_conntrack_tuple m;
2447 struct nlattr *nest_parms;
2449 memset(&m, 0xFF, sizeof(m));
2450 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2451 m.src.u.all = mask->src.u.all;
2452 m.dst.protonum = tuple->dst.protonum;
2454 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2456 goto nla_put_failure;
2459 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2460 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2462 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2463 tuple->dst.protonum);
2464 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2468 if (unlikely(ret < 0))
2469 goto nla_put_failure;
2471 nla_nest_end(skb, nest_parms);
2479 static const union nf_inet_addr any_addr;
2481 static __be32 nf_expect_get_id(const struct nf_conntrack_expect *exp)
2483 static __read_mostly siphash_key_t exp_id_seed;
2484 unsigned long a, b, c, d;
2486 net_get_random_once(&exp_id_seed, sizeof(exp_id_seed));
2488 a = (unsigned long)exp;
2489 b = (unsigned long)exp->helper;
2490 c = (unsigned long)exp->master;
2491 d = (unsigned long)siphash(&exp->tuple, sizeof(exp->tuple), &exp_id_seed);
2494 return (__force __be32)siphash_4u64((u64)a, (u64)b, (u64)c, (u64)d, &exp_id_seed);
2496 return (__force __be32)siphash_4u32((u32)a, (u32)b, (u32)c, (u32)d, &exp_id_seed);
2501 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2502 const struct nf_conntrack_expect *exp)
2504 struct nf_conn *master = exp->master;
2505 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2506 struct nf_conn_help *help;
2507 #ifdef CONFIG_NF_NAT_NEEDED
2508 struct nlattr *nest_parms;
2509 struct nf_conntrack_tuple nat_tuple = {};
2511 struct nf_ct_helper_expectfn *expfn;
2516 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2517 goto nla_put_failure;
2518 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2519 goto nla_put_failure;
2520 if (ctnetlink_exp_dump_tuple(skb,
2521 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2522 CTA_EXPECT_MASTER) < 0)
2523 goto nla_put_failure;
2525 #ifdef CONFIG_NF_NAT_NEEDED
2526 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2527 exp->saved_proto.all) {
2528 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2530 goto nla_put_failure;
2532 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2533 goto nla_put_failure;
2535 nat_tuple.src.l3num = nf_ct_l3num(master);
2536 nat_tuple.src.u3 = exp->saved_addr;
2537 nat_tuple.dst.protonum = nf_ct_protonum(master);
2538 nat_tuple.src.u = exp->saved_proto;
2540 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2541 CTA_EXPECT_NAT_TUPLE) < 0)
2542 goto nla_put_failure;
2543 nla_nest_end(skb, nest_parms);
2546 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2547 nla_put_be32(skb, CTA_EXPECT_ID, nf_expect_get_id(exp)) ||
2548 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2549 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2550 goto nla_put_failure;
2551 help = nfct_help(master);
2553 struct nf_conntrack_helper *helper;
2555 helper = rcu_dereference(help->helper);
2557 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2558 goto nla_put_failure;
2560 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2561 if (expfn != NULL &&
2562 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2563 goto nla_put_failure;
2572 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2573 int event, const struct nf_conntrack_expect *exp)
2575 struct nlmsghdr *nlh;
2576 struct nfgenmsg *nfmsg;
2577 unsigned int flags = portid ? NLM_F_MULTI : 0;
2579 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2580 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2584 nfmsg = nlmsg_data(nlh);
2585 nfmsg->nfgen_family = exp->tuple.src.l3num;
2586 nfmsg->version = NFNETLINK_V0;
2589 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2590 goto nla_put_failure;
2592 nlmsg_end(skb, nlh);
2597 nlmsg_cancel(skb, nlh);
2601 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2603 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2605 struct nf_conntrack_expect *exp = item->exp;
2606 struct net *net = nf_ct_exp_net(exp);
2607 struct nlmsghdr *nlh;
2608 struct nfgenmsg *nfmsg;
2609 struct sk_buff *skb;
2610 unsigned int type, group;
2613 if (events & (1 << IPEXP_DESTROY)) {
2614 type = IPCTNL_MSG_EXP_DELETE;
2615 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2616 } else if (events & (1 << IPEXP_NEW)) {
2617 type = IPCTNL_MSG_EXP_NEW;
2618 flags = NLM_F_CREATE|NLM_F_EXCL;
2619 group = NFNLGRP_CONNTRACK_EXP_NEW;
2623 if (!item->report && !nfnetlink_has_listeners(net, group))
2626 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2630 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2631 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2635 nfmsg = nlmsg_data(nlh);
2636 nfmsg->nfgen_family = exp->tuple.src.l3num;
2637 nfmsg->version = NFNETLINK_V0;
2641 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2642 goto nla_put_failure;
2645 nlmsg_end(skb, nlh);
2646 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2651 nlmsg_cancel(skb, nlh);
2655 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2659 static int ctnetlink_exp_done(struct netlink_callback *cb)
2662 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2667 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2669 struct net *net = sock_net(skb->sk);
2670 struct nf_conntrack_expect *exp, *last;
2671 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2672 u_int8_t l3proto = nfmsg->nfgen_family;
2675 last = (struct nf_conntrack_expect *)cb->args[1];
2676 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2678 hlist_for_each_entry(exp, &nf_ct_expect_hash[cb->args[0]],
2680 if (l3proto && exp->tuple.src.l3num != l3proto)
2683 if (!net_eq(nf_ct_net(exp->master), net))
2691 if (ctnetlink_exp_fill_info(skb,
2692 NETLINK_CB(cb->skb).portid,
2696 if (!atomic_inc_not_zero(&exp->use))
2698 cb->args[1] = (unsigned long)exp;
2710 nf_ct_expect_put(last);
2716 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2718 struct nf_conntrack_expect *exp, *last;
2719 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2720 struct nf_conn *ct = cb->data;
2721 struct nf_conn_help *help = nfct_help(ct);
2722 u_int8_t l3proto = nfmsg->nfgen_family;
2728 last = (struct nf_conntrack_expect *)cb->args[1];
2730 hlist_for_each_entry(exp, &help->expectations, lnode) {
2731 if (l3proto && exp->tuple.src.l3num != l3proto)
2738 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2742 if (!atomic_inc_not_zero(&exp->use))
2744 cb->args[1] = (unsigned long)exp;
2756 nf_ct_expect_put(last);
2761 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2762 struct sk_buff *skb,
2763 const struct nlmsghdr *nlh,
2764 const struct nlattr * const cda[])
2767 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2768 u_int8_t u3 = nfmsg->nfgen_family;
2769 struct nf_conntrack_tuple tuple;
2770 struct nf_conntrack_tuple_hash *h;
2772 struct nf_conntrack_zone zone;
2773 struct netlink_dump_control c = {
2774 .dump = ctnetlink_exp_ct_dump_table,
2775 .done = ctnetlink_exp_done,
2778 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2783 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2787 h = nf_conntrack_find_get(net, &zone, &tuple);
2791 ct = nf_ct_tuplehash_to_ctrack(h);
2794 err = netlink_dump_start(ctnl, skb, nlh, &c);
2800 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2801 struct sk_buff *skb, const struct nlmsghdr *nlh,
2802 const struct nlattr * const cda[])
2804 struct nf_conntrack_tuple tuple;
2805 struct nf_conntrack_expect *exp;
2806 struct sk_buff *skb2;
2807 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2808 u_int8_t u3 = nfmsg->nfgen_family;
2809 struct nf_conntrack_zone zone;
2812 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2813 if (cda[CTA_EXPECT_MASTER])
2814 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda);
2816 struct netlink_dump_control c = {
2817 .dump = ctnetlink_exp_dump_table,
2818 .done = ctnetlink_exp_done,
2820 return netlink_dump_start(ctnl, skb, nlh, &c);
2824 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2828 if (cda[CTA_EXPECT_TUPLE])
2829 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2831 else if (cda[CTA_EXPECT_MASTER])
2832 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2840 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2844 if (cda[CTA_EXPECT_ID]) {
2845 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2847 if (id != nf_expect_get_id(exp)) {
2848 nf_ct_expect_put(exp);
2854 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2856 nf_ct_expect_put(exp);
2861 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2862 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2864 nf_ct_expect_put(exp);
2868 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2877 /* this avoids a loop in nfnetlink. */
2878 return err == -EAGAIN ? -ENOBUFS : err;
2881 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2882 struct sk_buff *skb, const struct nlmsghdr *nlh,
2883 const struct nlattr * const cda[])
2885 struct nf_conntrack_expect *exp;
2886 struct nf_conntrack_tuple tuple;
2887 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2888 struct hlist_node *next;
2889 u_int8_t u3 = nfmsg->nfgen_family;
2890 struct nf_conntrack_zone zone;
2894 if (cda[CTA_EXPECT_TUPLE]) {
2895 /* delete a single expect by tuple */
2896 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2900 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2905 /* bump usage count to 2 */
2906 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2910 if (cda[CTA_EXPECT_ID]) {
2911 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2912 if (ntohl(id) != (u32)(unsigned long)exp) {
2913 nf_ct_expect_put(exp);
2918 /* after list removal, usage count == 1 */
2919 spin_lock_bh(&nf_conntrack_expect_lock);
2920 if (del_timer(&exp->timeout)) {
2921 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2923 nf_ct_expect_put(exp);
2925 spin_unlock_bh(&nf_conntrack_expect_lock);
2926 /* have to put what we 'get' above.
2927 * after this line usage count == 0 */
2928 nf_ct_expect_put(exp);
2929 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2930 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2931 struct nf_conn_help *m_help;
2933 /* delete all expectations for this helper */
2934 spin_lock_bh(&nf_conntrack_expect_lock);
2935 for (i = 0; i < nf_ct_expect_hsize; i++) {
2936 hlist_for_each_entry_safe(exp, next,
2937 &nf_ct_expect_hash[i],
2940 if (!net_eq(nf_ct_exp_net(exp), net))
2943 m_help = nfct_help(exp->master);
2944 if (!strcmp(m_help->helper->name, name) &&
2945 del_timer(&exp->timeout)) {
2946 nf_ct_unlink_expect_report(exp,
2947 NETLINK_CB(skb).portid,
2949 nf_ct_expect_put(exp);
2953 spin_unlock_bh(&nf_conntrack_expect_lock);
2955 /* This basically means we have to flush everything*/
2956 spin_lock_bh(&nf_conntrack_expect_lock);
2957 for (i = 0; i < nf_ct_expect_hsize; i++) {
2958 hlist_for_each_entry_safe(exp, next,
2959 &nf_ct_expect_hash[i],
2962 if (!net_eq(nf_ct_exp_net(exp), net))
2965 if (del_timer(&exp->timeout)) {
2966 nf_ct_unlink_expect_report(exp,
2967 NETLINK_CB(skb).portid,
2969 nf_ct_expect_put(exp);
2973 spin_unlock_bh(&nf_conntrack_expect_lock);
2979 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2980 const struct nlattr * const cda[])
2982 if (cda[CTA_EXPECT_TIMEOUT]) {
2983 if (!del_timer(&x->timeout))
2986 x->timeout.expires = jiffies +
2987 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2988 add_timer(&x->timeout);
2993 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2994 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2995 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2999 ctnetlink_parse_expect_nat(const struct nlattr *attr,
3000 struct nf_conntrack_expect *exp,
3003 #ifdef CONFIG_NF_NAT_NEEDED
3004 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
3005 struct nf_conntrack_tuple nat_tuple = {};
3008 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
3012 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
3015 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
3016 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
3021 exp->saved_addr = nat_tuple.src.u3;
3022 exp->saved_proto = nat_tuple.src.u;
3023 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
3031 static struct nf_conntrack_expect *
3032 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
3033 struct nf_conntrack_helper *helper,
3034 struct nf_conntrack_tuple *tuple,
3035 struct nf_conntrack_tuple *mask)
3037 u_int32_t class = 0;
3038 struct nf_conntrack_expect *exp;
3039 struct nf_conn_help *help;
3042 if (cda[CTA_EXPECT_CLASS] && helper) {
3043 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3044 if (class > helper->expect_class_max)
3045 return ERR_PTR(-EINVAL);
3047 exp = nf_ct_expect_alloc(ct);
3049 return ERR_PTR(-ENOMEM);
3051 help = nfct_help(ct);
3053 if (!cda[CTA_EXPECT_TIMEOUT]) {
3057 exp->timeout.expires =
3058 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3060 exp->flags = NF_CT_EXPECT_USERSPACE;
3061 if (cda[CTA_EXPECT_FLAGS]) {
3063 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3066 if (cda[CTA_EXPECT_FLAGS]) {
3067 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3068 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3072 if (cda[CTA_EXPECT_FN]) {
3073 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3074 struct nf_ct_helper_expectfn *expfn;
3076 expfn = nf_ct_helper_expectfn_find_by_name(name);
3077 if (expfn == NULL) {
3081 exp->expectfn = expfn->expectfn;
3083 exp->expectfn = NULL;
3087 exp->helper = helper;
3088 exp->tuple = *tuple;
3089 exp->mask.src.u3 = mask->src.u3;
3090 exp->mask.src.u.all = mask->src.u.all;
3092 if (cda[CTA_EXPECT_NAT]) {
3093 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3094 exp, nf_ct_l3num(ct));
3100 nf_ct_expect_put(exp);
3101 return ERR_PTR(err);
3105 ctnetlink_create_expect(struct net *net,
3106 const struct nf_conntrack_zone *zone,
3107 const struct nlattr * const cda[],
3108 u_int8_t u3, u32 portid, int report)
3110 struct nf_conntrack_tuple tuple, mask, master_tuple;
3111 struct nf_conntrack_tuple_hash *h = NULL;
3112 struct nf_conntrack_helper *helper = NULL;
3113 struct nf_conntrack_expect *exp;
3117 /* caller guarantees that those three CTA_EXPECT_* exist */
3118 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3122 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3126 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3131 /* Look for master conntrack of this expectation */
3132 h = nf_conntrack_find_get(net, zone, &master_tuple);
3135 ct = nf_ct_tuplehash_to_ctrack(h);
3137 if (cda[CTA_EXPECT_HELP_NAME]) {
3138 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3140 helper = __nf_conntrack_helper_find(helpname, u3,
3141 nf_ct_protonum(ct));
3142 if (helper == NULL) {
3143 #ifdef CONFIG_MODULES
3144 if (request_module("nfct-helper-%s", helpname) < 0) {
3148 helper = __nf_conntrack_helper_find(helpname, u3,
3149 nf_ct_protonum(ct));
3160 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3166 err = nf_ct_expect_related_report(exp, portid, report);
3167 nf_ct_expect_put(exp);
3173 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3174 struct sk_buff *skb, const struct nlmsghdr *nlh,
3175 const struct nlattr * const cda[])
3177 struct nf_conntrack_tuple tuple;
3178 struct nf_conntrack_expect *exp;
3179 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3180 u_int8_t u3 = nfmsg->nfgen_family;
3181 struct nf_conntrack_zone zone;
3184 if (!cda[CTA_EXPECT_TUPLE]
3185 || !cda[CTA_EXPECT_MASK]
3186 || !cda[CTA_EXPECT_MASTER])
3189 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3193 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3198 spin_lock_bh(&nf_conntrack_expect_lock);
3199 exp = __nf_ct_expect_find(net, &zone, &tuple);
3201 spin_unlock_bh(&nf_conntrack_expect_lock);
3203 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3204 err = ctnetlink_create_expect(net, &zone, cda, u3,
3205 NETLINK_CB(skb).portid,
3212 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3213 err = ctnetlink_change_expect(exp, cda);
3214 spin_unlock_bh(&nf_conntrack_expect_lock);
3220 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3221 const struct ip_conntrack_stat *st)
3223 struct nlmsghdr *nlh;
3224 struct nfgenmsg *nfmsg;
3225 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3227 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
3228 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3232 nfmsg = nlmsg_data(nlh);
3233 nfmsg->nfgen_family = AF_UNSPEC;
3234 nfmsg->version = NFNETLINK_V0;
3235 nfmsg->res_id = htons(cpu);
3237 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3238 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3239 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3240 goto nla_put_failure;
3242 nlmsg_end(skb, nlh);
3247 nlmsg_cancel(skb, nlh);
3252 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3255 struct net *net = sock_net(skb->sk);
3257 if (cb->args[0] == nr_cpu_ids)
3260 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3261 const struct ip_conntrack_stat *st;
3263 if (!cpu_possible(cpu))
3266 st = per_cpu_ptr(net->ct.stat, cpu);
3267 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3277 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3278 struct sk_buff *skb,
3279 const struct nlmsghdr *nlh,
3280 const struct nlattr * const cda[])
3282 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3283 struct netlink_dump_control c = {
3284 .dump = ctnetlink_exp_stat_cpu_dump,
3286 return netlink_dump_start(ctnl, skb, nlh, &c);
3292 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3293 static struct nf_ct_event_notifier ctnl_notifier = {
3294 .fcn = ctnetlink_conntrack_event,
3297 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3298 .fcn = ctnetlink_expect_event,
3302 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3303 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3304 .attr_count = CTA_MAX,
3305 .policy = ct_nla_policy },
3306 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3307 .attr_count = CTA_MAX,
3308 .policy = ct_nla_policy },
3309 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3310 .attr_count = CTA_MAX,
3311 .policy = ct_nla_policy },
3312 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3313 .attr_count = CTA_MAX,
3314 .policy = ct_nla_policy },
3315 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3316 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3317 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3318 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3321 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3322 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3323 .attr_count = CTA_EXPECT_MAX,
3324 .policy = exp_nla_policy },
3325 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3326 .attr_count = CTA_EXPECT_MAX,
3327 .policy = exp_nla_policy },
3328 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3329 .attr_count = CTA_EXPECT_MAX,
3330 .policy = exp_nla_policy },
3331 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3334 static const struct nfnetlink_subsystem ctnl_subsys = {
3335 .name = "conntrack",
3336 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3337 .cb_count = IPCTNL_MSG_MAX,
3341 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3342 .name = "conntrack_expect",
3343 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3344 .cb_count = IPCTNL_MSG_EXP_MAX,
3348 MODULE_ALIAS("ip_conntrack_netlink");
3349 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3350 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3352 static int __net_init ctnetlink_net_init(struct net *net)
3354 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3357 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3359 pr_err("ctnetlink_init: cannot register notifier.\n");
3363 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3365 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3366 goto err_unreg_notifier;
3371 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3373 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3379 static void ctnetlink_net_exit(struct net *net)
3381 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3382 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3383 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3387 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3391 list_for_each_entry(net, net_exit_list, exit_list)
3392 ctnetlink_net_exit(net);
3394 /* wait for other cpus until they are done with ctnl_notifiers */
3398 static struct pernet_operations ctnetlink_net_ops = {
3399 .init = ctnetlink_net_init,
3400 .exit_batch = ctnetlink_net_exit_batch,
3403 static int __init ctnetlink_init(void)
3407 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3408 ret = nfnetlink_subsys_register(&ctnl_subsys);
3410 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3414 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3416 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3417 goto err_unreg_subsys;
3420 ret = register_pernet_subsys(&ctnetlink_net_ops);
3422 pr_err("ctnetlink_init: cannot register pernet operations\n");
3423 goto err_unreg_exp_subsys;
3425 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3426 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3427 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3431 err_unreg_exp_subsys:
3432 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3434 nfnetlink_subsys_unregister(&ctnl_subsys);
3439 static void __exit ctnetlink_exit(void)
3441 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3443 unregister_pernet_subsys(&ctnetlink_net_ops);
3444 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3445 nfnetlink_subsys_unregister(&ctnl_subsys);
3446 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3447 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3452 module_init(ctnetlink_init);
3453 module_exit(ctnetlink_exit);