1 /* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
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";
62 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
63 const struct nf_conntrack_tuple *tuple,
64 struct nf_conntrack_l4proto *l4proto)
67 struct nlattr *nest_parms;
69 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
72 if (nla_put_u8(skb, CTA_PROTO_NUM, tuple->dst.protonum))
75 if (likely(l4proto->tuple_to_nlattr))
76 ret = l4proto->tuple_to_nlattr(skb, tuple);
78 nla_nest_end(skb, nest_parms);
87 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
88 const struct nf_conntrack_tuple *tuple,
89 struct nf_conntrack_l3proto *l3proto)
92 struct nlattr *nest_parms;
94 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
98 if (likely(l3proto->tuple_to_nlattr))
99 ret = l3proto->tuple_to_nlattr(skb, tuple);
101 nla_nest_end(skb, nest_parms);
110 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);
131 ctnetlink_dump_zone_id(struct sk_buff *skb, int attrtype,
132 const struct nf_conntrack_zone *zone, int dir)
134 if (zone->id == NF_CT_DEFAULT_ZONE_ID || zone->dir != dir)
136 if (nla_put_be16(skb, attrtype, htons(zone->id)))
137 goto nla_put_failure;
145 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
147 if (nla_put_be32(skb, CTA_STATUS, htonl(ct->status)))
148 goto nla_put_failure;
156 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
158 long timeout = ((long)ct->timeout.expires - (long)jiffies) / HZ;
163 if (nla_put_be32(skb, CTA_TIMEOUT, htonl(timeout)))
164 goto nla_put_failure;
172 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
174 struct nf_conntrack_l4proto *l4proto;
175 struct nlattr *nest_proto;
178 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
179 if (!l4proto->to_nlattr)
182 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
184 goto nla_put_failure;
186 ret = l4proto->to_nlattr(skb, nest_proto, ct);
188 nla_nest_end(skb, nest_proto);
197 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
199 struct nlattr *nest_helper;
200 const struct nf_conn_help *help = nfct_help(ct);
201 struct nf_conntrack_helper *helper;
206 helper = rcu_dereference(help->helper);
210 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
212 goto nla_put_failure;
213 if (nla_put_string(skb, CTA_HELP_NAME, helper->name))
214 goto nla_put_failure;
216 if (helper->to_nlattr)
217 helper->to_nlattr(skb, ct);
219 nla_nest_end(skb, nest_helper);
228 dump_counters(struct sk_buff *skb, struct nf_conn_acct *acct,
229 enum ip_conntrack_dir dir, int type)
231 enum ctattr_type attr = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
232 struct nf_conn_counter *counter = acct->counter;
233 struct nlattr *nest_count;
236 if (type == IPCTNL_MSG_CT_GET_CTRZERO) {
237 pkts = atomic64_xchg(&counter[dir].packets, 0);
238 bytes = atomic64_xchg(&counter[dir].bytes, 0);
240 pkts = atomic64_read(&counter[dir].packets);
241 bytes = atomic64_read(&counter[dir].bytes);
244 nest_count = nla_nest_start(skb, attr | NLA_F_NESTED);
246 goto nla_put_failure;
248 if (nla_put_be64(skb, CTA_COUNTERS_PACKETS, cpu_to_be64(pkts)) ||
249 nla_put_be64(skb, CTA_COUNTERS_BYTES, cpu_to_be64(bytes)))
250 goto nla_put_failure;
252 nla_nest_end(skb, nest_count);
261 ctnetlink_dump_acct(struct sk_buff *skb, const struct nf_conn *ct, int type)
263 struct nf_conn_acct *acct = nf_conn_acct_find(ct);
268 if (dump_counters(skb, acct, IP_CT_DIR_ORIGINAL, type) < 0)
270 if (dump_counters(skb, acct, IP_CT_DIR_REPLY, type) < 0)
277 ctnetlink_dump_timestamp(struct sk_buff *skb, const struct nf_conn *ct)
279 struct nlattr *nest_count;
280 const struct nf_conn_tstamp *tstamp;
282 tstamp = nf_conn_tstamp_find(ct);
286 nest_count = nla_nest_start(skb, CTA_TIMESTAMP | NLA_F_NESTED);
288 goto nla_put_failure;
290 if (nla_put_be64(skb, CTA_TIMESTAMP_START, cpu_to_be64(tstamp->start)) ||
291 (tstamp->stop != 0 && nla_put_be64(skb, CTA_TIMESTAMP_STOP,
292 cpu_to_be64(tstamp->stop))))
293 goto nla_put_failure;
294 nla_nest_end(skb, nest_count);
302 #ifdef CONFIG_NF_CONNTRACK_MARK
304 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
306 if (nla_put_be32(skb, CTA_MARK, htonl(ct->mark)))
307 goto nla_put_failure;
314 #define ctnetlink_dump_mark(a, b) (0)
317 #ifdef CONFIG_NF_CONNTRACK_SECMARK
319 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
321 struct nlattr *nest_secctx;
325 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
330 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
332 goto nla_put_failure;
334 if (nla_put_string(skb, CTA_SECCTX_NAME, secctx))
335 goto nla_put_failure;
336 nla_nest_end(skb, nest_secctx);
340 security_release_secctx(secctx, len);
344 #define ctnetlink_dump_secctx(a, b) (0)
347 #ifdef CONFIG_NF_CONNTRACK_LABELS
348 static int ctnetlink_label_size(const struct nf_conn *ct)
350 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
354 return nla_total_size(labels->words * sizeof(long));
358 ctnetlink_dump_labels(struct sk_buff *skb, const struct nf_conn *ct)
360 struct nf_conn_labels *labels = nf_ct_labels_find(ct);
366 len = labels->words * sizeof(long);
369 if (labels->bits[i] != 0)
370 return nla_put(skb, CTA_LABELS, len, labels->bits);
372 } while (i < labels->words);
377 #define ctnetlink_dump_labels(a, b) (0)
378 #define ctnetlink_label_size(a) (0)
381 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
384 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
386 struct nlattr *nest_parms;
388 if (!(ct->status & IPS_EXPECTED))
391 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
393 goto nla_put_failure;
394 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
395 goto nla_put_failure;
396 nla_nest_end(skb, nest_parms);
405 dump_ct_seq_adj(struct sk_buff *skb, const struct nf_ct_seqadj *seq, int type)
407 struct nlattr *nest_parms;
409 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
411 goto nla_put_failure;
413 if (nla_put_be32(skb, CTA_SEQADJ_CORRECTION_POS,
414 htonl(seq->correction_pos)) ||
415 nla_put_be32(skb, CTA_SEQADJ_OFFSET_BEFORE,
416 htonl(seq->offset_before)) ||
417 nla_put_be32(skb, CTA_SEQADJ_OFFSET_AFTER,
418 htonl(seq->offset_after)))
419 goto nla_put_failure;
421 nla_nest_end(skb, nest_parms);
430 ctnetlink_dump_ct_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
432 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
433 struct nf_ct_seqadj *seq;
435 if (!(ct->status & IPS_SEQ_ADJUST) || !seqadj)
438 seq = &seqadj->seq[IP_CT_DIR_ORIGINAL];
439 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_ORIG) == -1)
442 seq = &seqadj->seq[IP_CT_DIR_REPLY];
443 if (dump_ct_seq_adj(skb, seq, CTA_SEQ_ADJ_REPLY) == -1)
450 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
452 if (nla_put_be32(skb, CTA_ID, htonl((unsigned long)ct)))
453 goto nla_put_failure;
461 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
463 if (nla_put_be32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use))))
464 goto nla_put_failure;
472 ctnetlink_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
475 const struct nf_conntrack_zone *zone;
476 struct nlmsghdr *nlh;
477 struct nfgenmsg *nfmsg;
478 struct nlattr *nest_parms;
479 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
481 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_NEW);
482 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
486 nfmsg = nlmsg_data(nlh);
487 nfmsg->nfgen_family = nf_ct_l3num(ct);
488 nfmsg->version = NFNETLINK_V0;
491 zone = nf_ct_zone(ct);
493 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
495 goto nla_put_failure;
496 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
497 goto nla_put_failure;
498 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
499 NF_CT_ZONE_DIR_ORIG) < 0)
500 goto nla_put_failure;
501 nla_nest_end(skb, nest_parms);
503 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
505 goto nla_put_failure;
506 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
507 goto nla_put_failure;
508 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
509 NF_CT_ZONE_DIR_REPL) < 0)
510 goto nla_put_failure;
511 nla_nest_end(skb, nest_parms);
513 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
514 NF_CT_DEFAULT_ZONE_DIR) < 0)
515 goto nla_put_failure;
517 if (ctnetlink_dump_status(skb, ct) < 0 ||
518 ctnetlink_dump_timeout(skb, ct) < 0 ||
519 ctnetlink_dump_acct(skb, ct, type) < 0 ||
520 ctnetlink_dump_timestamp(skb, ct) < 0 ||
521 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
522 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
523 ctnetlink_dump_mark(skb, ct) < 0 ||
524 ctnetlink_dump_secctx(skb, ct) < 0 ||
525 ctnetlink_dump_labels(skb, ct) < 0 ||
526 ctnetlink_dump_id(skb, ct) < 0 ||
527 ctnetlink_dump_use(skb, ct) < 0 ||
528 ctnetlink_dump_master(skb, ct) < 0 ||
529 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
530 goto nla_put_failure;
537 nlmsg_cancel(skb, nlh);
542 ctnetlink_proto_size(const struct nf_conn *ct)
544 struct nf_conntrack_l3proto *l3proto;
545 struct nf_conntrack_l4proto *l4proto;
549 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
550 len += l3proto->nla_size;
552 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
553 len += l4proto->nla_size;
560 ctnetlink_acct_size(const struct nf_conn *ct)
562 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
564 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
565 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
566 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
571 ctnetlink_secctx_size(const struct nf_conn *ct)
573 #ifdef CONFIG_NF_CONNTRACK_SECMARK
576 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
580 return nla_total_size(0) /* CTA_SECCTX */
581 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
588 ctnetlink_timestamp_size(const struct nf_conn *ct)
590 #ifdef CONFIG_NF_CONNTRACK_TIMESTAMP
591 if (!nf_ct_ext_exist(ct, NF_CT_EXT_TSTAMP))
593 return nla_total_size(0) + 2 * nla_total_size(sizeof(uint64_t));
600 ctnetlink_nlmsg_size(const struct nf_conn *ct)
602 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
603 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
604 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
605 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
606 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
607 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
608 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
609 + ctnetlink_acct_size(ct)
610 + ctnetlink_timestamp_size(ct)
611 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
612 + nla_total_size(0) /* CTA_PROTOINFO */
613 + nla_total_size(0) /* CTA_HELP */
614 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
615 + ctnetlink_secctx_size(ct)
616 #ifdef CONFIG_NF_NAT_NEEDED
617 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
618 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
620 #ifdef CONFIG_NF_CONNTRACK_MARK
621 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
623 #ifdef CONFIG_NF_CONNTRACK_ZONES
624 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
626 + ctnetlink_proto_size(ct)
627 + ctnetlink_label_size(ct)
631 #ifdef CONFIG_NF_CONNTRACK_EVENTS
633 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
635 const struct nf_conntrack_zone *zone;
637 struct nlmsghdr *nlh;
638 struct nfgenmsg *nfmsg;
639 struct nlattr *nest_parms;
640 struct nf_conn *ct = item->ct;
643 unsigned int flags = 0, group;
646 /* ignore our fake conntrack entry */
647 if (nf_ct_is_untracked(ct))
650 if (events & (1 << IPCT_DESTROY)) {
651 type = IPCTNL_MSG_CT_DELETE;
652 group = NFNLGRP_CONNTRACK_DESTROY;
653 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
654 type = IPCTNL_MSG_CT_NEW;
655 flags = NLM_F_CREATE|NLM_F_EXCL;
656 group = NFNLGRP_CONNTRACK_NEW;
658 type = IPCTNL_MSG_CT_NEW;
659 group = NFNLGRP_CONNTRACK_UPDATE;
664 if (!item->report && !nfnetlink_has_listeners(net, group))
667 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
671 type |= NFNL_SUBSYS_CTNETLINK << 8;
672 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
676 nfmsg = nlmsg_data(nlh);
677 nfmsg->nfgen_family = nf_ct_l3num(ct);
678 nfmsg->version = NFNETLINK_V0;
682 zone = nf_ct_zone(ct);
684 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
686 goto nla_put_failure;
687 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
688 goto nla_put_failure;
689 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
690 NF_CT_ZONE_DIR_ORIG) < 0)
691 goto nla_put_failure;
692 nla_nest_end(skb, nest_parms);
694 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
696 goto nla_put_failure;
697 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
698 goto nla_put_failure;
699 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
700 NF_CT_ZONE_DIR_REPL) < 0)
701 goto nla_put_failure;
702 nla_nest_end(skb, nest_parms);
704 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
705 NF_CT_DEFAULT_ZONE_DIR) < 0)
706 goto nla_put_failure;
708 if (ctnetlink_dump_id(skb, ct) < 0)
709 goto nla_put_failure;
711 if (ctnetlink_dump_status(skb, ct) < 0)
712 goto nla_put_failure;
714 if (events & (1 << IPCT_DESTROY)) {
715 if (ctnetlink_dump_acct(skb, ct, type) < 0 ||
716 ctnetlink_dump_timestamp(skb, ct) < 0)
717 goto nla_put_failure;
719 if (ctnetlink_dump_timeout(skb, ct) < 0)
720 goto nla_put_failure;
722 if (events & (1 << IPCT_PROTOINFO)
723 && ctnetlink_dump_protoinfo(skb, ct) < 0)
724 goto nla_put_failure;
726 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
727 && ctnetlink_dump_helpinfo(skb, ct) < 0)
728 goto nla_put_failure;
730 #ifdef CONFIG_NF_CONNTRACK_SECMARK
731 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
732 && ctnetlink_dump_secctx(skb, ct) < 0)
733 goto nla_put_failure;
735 if (events & (1 << IPCT_LABEL) &&
736 ctnetlink_dump_labels(skb, ct) < 0)
737 goto nla_put_failure;
739 if (events & (1 << IPCT_RELATED) &&
740 ctnetlink_dump_master(skb, ct) < 0)
741 goto nla_put_failure;
743 if (events & (1 << IPCT_SEQADJ) &&
744 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
745 goto nla_put_failure;
748 #ifdef CONFIG_NF_CONNTRACK_MARK
749 if ((events & (1 << IPCT_MARK) || ct->mark)
750 && ctnetlink_dump_mark(skb, ct) < 0)
751 goto nla_put_failure;
756 err = nfnetlink_send(skb, net, item->portid, group, item->report,
758 if (err == -ENOBUFS || err == -EAGAIN)
765 nlmsg_cancel(skb, nlh);
769 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
774 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
776 static int ctnetlink_done(struct netlink_callback *cb)
779 nf_ct_put((struct nf_conn *)cb->args[1]);
784 struct ctnetlink_filter {
791 static struct ctnetlink_filter *
792 ctnetlink_alloc_filter(const struct nlattr * const cda[])
794 #ifdef CONFIG_NF_CONNTRACK_MARK
795 struct ctnetlink_filter *filter;
797 filter = kzalloc(sizeof(*filter), GFP_KERNEL);
799 return ERR_PTR(-ENOMEM);
801 filter->mark.val = ntohl(nla_get_be32(cda[CTA_MARK]));
802 filter->mark.mask = ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
806 return ERR_PTR(-EOPNOTSUPP);
810 static int ctnetlink_filter_match(struct nf_conn *ct, void *data)
812 struct ctnetlink_filter *filter = data;
817 #ifdef CONFIG_NF_CONNTRACK_MARK
818 if ((ct->mark & filter->mark.mask) == filter->mark.val)
826 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
828 struct net *net = sock_net(skb->sk);
829 struct nf_conn *ct, *last;
830 struct nf_conntrack_tuple_hash *h;
831 struct hlist_nulls_node *n;
832 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
833 u_int8_t l3proto = nfmsg->nfgen_family;
837 last = (struct nf_conn *)cb->args[1];
840 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
842 lockp = &nf_conntrack_locks[cb->args[0] % CONNTRACK_LOCKS];
843 nf_conntrack_lock(lockp);
844 if (cb->args[0] >= net->ct.htable_size) {
848 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
850 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
852 ct = nf_ct_tuplehash_to_ctrack(h);
853 /* Dump entries of a given L3 protocol number.
854 * If it is not specified, ie. l3proto == 0,
855 * then dump everything. */
856 if (l3proto && nf_ct_l3num(ct) != l3proto)
863 if (!ctnetlink_filter_match(ct, cb->data))
868 ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
870 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
874 nf_conntrack_get(&ct->ct_general);
875 cb->args[1] = (unsigned long)ct;
895 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
897 struct nlattr *tb[CTA_IP_MAX+1];
898 struct nf_conntrack_l3proto *l3proto;
901 ret = nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
906 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
908 if (likely(l3proto->nlattr_to_tuple)) {
909 ret = nla_validate_nested(attr, CTA_IP_MAX,
910 l3proto->nla_policy);
912 ret = l3proto->nlattr_to_tuple(tb, tuple);
920 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
921 [CTA_PROTO_NUM] = { .type = NLA_U8 },
925 ctnetlink_parse_tuple_proto(struct nlattr *attr,
926 struct nf_conntrack_tuple *tuple)
928 struct nlattr *tb[CTA_PROTO_MAX+1];
929 struct nf_conntrack_l4proto *l4proto;
932 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
936 if (!tb[CTA_PROTO_NUM])
938 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
941 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
943 if (likely(l4proto->nlattr_to_tuple)) {
944 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
945 l4proto->nla_policy);
947 ret = l4proto->nlattr_to_tuple(tb, tuple);
956 ctnetlink_parse_zone(const struct nlattr *attr,
957 struct nf_conntrack_zone *zone)
959 nf_ct_zone_init(zone, NF_CT_DEFAULT_ZONE_ID,
960 NF_CT_DEFAULT_ZONE_DIR, 0);
961 #ifdef CONFIG_NF_CONNTRACK_ZONES
963 zone->id = ntohs(nla_get_be16(attr));
972 ctnetlink_parse_tuple_zone(struct nlattr *attr, enum ctattr_type type,
973 struct nf_conntrack_zone *zone)
977 if (zone->id != NF_CT_DEFAULT_ZONE_ID)
980 ret = ctnetlink_parse_zone(attr, zone);
984 if (type == CTA_TUPLE_REPLY)
985 zone->dir = NF_CT_ZONE_DIR_REPL;
987 zone->dir = NF_CT_ZONE_DIR_ORIG;
992 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
993 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
994 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
995 [CTA_TUPLE_ZONE] = { .type = NLA_U16 },
999 ctnetlink_parse_tuple(const struct nlattr * const cda[],
1000 struct nf_conntrack_tuple *tuple,
1001 enum ctattr_type type, u_int8_t l3num,
1002 struct nf_conntrack_zone *zone)
1004 struct nlattr *tb[CTA_TUPLE_MAX+1];
1007 memset(tuple, 0, sizeof(*tuple));
1009 err = nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
1013 if (!tb[CTA_TUPLE_IP])
1016 tuple->src.l3num = l3num;
1018 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
1022 if (!tb[CTA_TUPLE_PROTO])
1025 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
1029 if (tb[CTA_TUPLE_ZONE]) {
1033 err = ctnetlink_parse_tuple_zone(tb[CTA_TUPLE_ZONE],
1039 /* orig and expect tuples get DIR_ORIGINAL */
1040 if (type == CTA_TUPLE_REPLY)
1041 tuple->dst.dir = IP_CT_DIR_REPLY;
1043 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
1048 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
1049 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING,
1050 .len = NF_CT_HELPER_NAME_LEN - 1 },
1054 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name,
1055 struct nlattr **helpinfo)
1058 struct nlattr *tb[CTA_HELP_MAX+1];
1060 err = nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
1064 if (!tb[CTA_HELP_NAME])
1067 *helper_name = nla_data(tb[CTA_HELP_NAME]);
1069 if (tb[CTA_HELP_INFO])
1070 *helpinfo = tb[CTA_HELP_INFO];
1075 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
1076 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
1077 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
1078 [CTA_STATUS] = { .type = NLA_U32 },
1079 [CTA_PROTOINFO] = { .type = NLA_NESTED },
1080 [CTA_HELP] = { .type = NLA_NESTED },
1081 [CTA_NAT_SRC] = { .type = NLA_NESTED },
1082 [CTA_TIMEOUT] = { .type = NLA_U32 },
1083 [CTA_MARK] = { .type = NLA_U32 },
1084 [CTA_ID] = { .type = NLA_U32 },
1085 [CTA_NAT_DST] = { .type = NLA_NESTED },
1086 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
1087 [CTA_NAT_SEQ_ADJ_ORIG] = { .type = NLA_NESTED },
1088 [CTA_NAT_SEQ_ADJ_REPLY] = { .type = NLA_NESTED },
1089 [CTA_ZONE] = { .type = NLA_U16 },
1090 [CTA_MARK_MASK] = { .type = NLA_U32 },
1091 [CTA_LABELS] = { .type = NLA_BINARY,
1092 .len = NF_CT_LABELS_MAX_SIZE },
1093 [CTA_LABELS_MASK] = { .type = NLA_BINARY,
1094 .len = NF_CT_LABELS_MAX_SIZE },
1097 static int ctnetlink_flush_conntrack(struct net *net,
1098 const struct nlattr * const cda[],
1099 u32 portid, int report)
1101 struct ctnetlink_filter *filter = NULL;
1103 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1104 filter = ctnetlink_alloc_filter(cda);
1106 return PTR_ERR(filter);
1109 nf_ct_iterate_cleanup(net, ctnetlink_filter_match, filter,
1116 static int ctnetlink_del_conntrack(struct net *net, struct sock *ctnl,
1117 struct sk_buff *skb,
1118 const struct nlmsghdr *nlh,
1119 const struct nlattr * const cda[])
1121 struct nf_conntrack_tuple_hash *h;
1122 struct nf_conntrack_tuple tuple;
1124 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1125 u_int8_t u3 = nfmsg->nfgen_family;
1126 struct nf_conntrack_zone zone;
1129 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1133 if (cda[CTA_TUPLE_ORIG])
1134 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1136 else if (cda[CTA_TUPLE_REPLY])
1137 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1140 return ctnetlink_flush_conntrack(net, cda,
1141 NETLINK_CB(skb).portid,
1148 h = nf_conntrack_find_get(net, &zone, &tuple);
1152 ct = nf_ct_tuplehash_to_ctrack(h);
1155 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
1156 if (id != (u32)(unsigned long)ct) {
1162 if (del_timer(&ct->timeout))
1163 nf_ct_delete(ct, NETLINK_CB(skb).portid, nlmsg_report(nlh));
1170 static int ctnetlink_get_conntrack(struct net *net, struct sock *ctnl,
1171 struct sk_buff *skb,
1172 const struct nlmsghdr *nlh,
1173 const struct nlattr * const cda[])
1175 struct nf_conntrack_tuple_hash *h;
1176 struct nf_conntrack_tuple tuple;
1178 struct sk_buff *skb2 = NULL;
1179 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1180 u_int8_t u3 = nfmsg->nfgen_family;
1181 struct nf_conntrack_zone zone;
1184 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1185 struct netlink_dump_control c = {
1186 .dump = ctnetlink_dump_table,
1187 .done = ctnetlink_done,
1190 if (cda[CTA_MARK] && cda[CTA_MARK_MASK]) {
1191 struct ctnetlink_filter *filter;
1193 filter = ctnetlink_alloc_filter(cda);
1195 return PTR_ERR(filter);
1199 return netlink_dump_start(ctnl, skb, nlh, &c);
1202 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1206 if (cda[CTA_TUPLE_ORIG])
1207 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG,
1209 else if (cda[CTA_TUPLE_REPLY])
1210 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY,
1218 h = nf_conntrack_find_get(net, &zone, &tuple);
1222 ct = nf_ct_tuplehash_to_ctrack(h);
1225 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1232 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq,
1233 NFNL_MSG_TYPE(nlh->nlmsg_type), ct);
1239 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
1248 /* this avoids a loop in nfnetlink. */
1249 return err == -EAGAIN ? -ENOBUFS : err;
1252 static int ctnetlink_done_list(struct netlink_callback *cb)
1255 nf_ct_put((struct nf_conn *)cb->args[1]);
1260 ctnetlink_dump_list(struct sk_buff *skb, struct netlink_callback *cb, bool dying)
1262 struct nf_conn *ct, *last;
1263 struct nf_conntrack_tuple_hash *h;
1264 struct hlist_nulls_node *n;
1265 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1266 u_int8_t l3proto = nfmsg->nfgen_family;
1269 struct hlist_nulls_head *list;
1270 struct net *net = sock_net(skb->sk);
1275 last = (struct nf_conn *)cb->args[1];
1277 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
1278 struct ct_pcpu *pcpu;
1280 if (!cpu_possible(cpu))
1283 pcpu = per_cpu_ptr(net->ct.pcpu_lists, cpu);
1284 spin_lock_bh(&pcpu->lock);
1285 list = dying ? &pcpu->dying : &pcpu->unconfirmed;
1287 hlist_nulls_for_each_entry(h, n, list, hnnode) {
1288 ct = nf_ct_tuplehash_to_ctrack(h);
1289 if (l3proto && nf_ct_l3num(ct) != l3proto)
1297 res = ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).portid,
1299 NFNL_MSG_TYPE(cb->nlh->nlmsg_type),
1303 if (!atomic_inc_not_zero(&ct->ct_general.use))
1306 cb->args[1] = (unsigned long)ct;
1307 spin_unlock_bh(&pcpu->lock);
1315 spin_unlock_bh(&pcpu->lock);
1326 ctnetlink_dump_dying(struct sk_buff *skb, struct netlink_callback *cb)
1328 return ctnetlink_dump_list(skb, cb, true);
1331 static int ctnetlink_get_ct_dying(struct net *net, struct sock *ctnl,
1332 struct sk_buff *skb,
1333 const struct nlmsghdr *nlh,
1334 const struct nlattr * const cda[])
1336 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1337 struct netlink_dump_control c = {
1338 .dump = ctnetlink_dump_dying,
1339 .done = ctnetlink_done_list,
1341 return netlink_dump_start(ctnl, skb, nlh, &c);
1348 ctnetlink_dump_unconfirmed(struct sk_buff *skb, struct netlink_callback *cb)
1350 return ctnetlink_dump_list(skb, cb, false);
1353 static int ctnetlink_get_ct_unconfirmed(struct net *net, struct sock *ctnl,
1354 struct sk_buff *skb,
1355 const struct nlmsghdr *nlh,
1356 const struct nlattr * const cda[])
1358 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1359 struct netlink_dump_control c = {
1360 .dump = ctnetlink_dump_unconfirmed,
1361 .done = ctnetlink_done_list,
1363 return netlink_dump_start(ctnl, skb, nlh, &c);
1369 #ifdef CONFIG_NF_NAT_NEEDED
1371 ctnetlink_parse_nat_setup(struct nf_conn *ct,
1372 enum nf_nat_manip_type manip,
1373 const struct nlattr *attr)
1375 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
1378 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
1379 if (!parse_nat_setup) {
1380 #ifdef CONFIG_MODULES
1382 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1383 if (request_module("nf-nat") < 0) {
1384 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1388 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1390 if (nfnetlink_parse_nat_setup_hook)
1396 err = parse_nat_setup(ct, manip, attr);
1397 if (err == -EAGAIN) {
1398 #ifdef CONFIG_MODULES
1400 nfnl_unlock(NFNL_SUBSYS_CTNETLINK);
1401 if (request_module("nf-nat-%u", nf_ct_l3num(ct)) < 0) {
1402 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1406 nfnl_lock(NFNL_SUBSYS_CTNETLINK);
1417 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1420 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1421 d = ct->status ^ status;
1423 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1427 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1428 /* SEEN_REPLY bit can only be set */
1431 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1432 /* ASSURED bit can only be set */
1435 /* Be careful here, modifying NAT bits can screw up things,
1436 * so don't let users modify them directly if they don't pass
1438 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1443 ctnetlink_setup_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1445 #ifdef CONFIG_NF_NAT_NEEDED
1448 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1451 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_DST,
1456 ret = ctnetlink_parse_nat_setup(ct, NF_NAT_MANIP_SRC,
1460 if (!cda[CTA_NAT_DST] && !cda[CTA_NAT_SRC])
1467 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1469 struct nf_conntrack_helper *helper;
1470 struct nf_conn_help *help = nfct_help(ct);
1471 char *helpname = NULL;
1472 struct nlattr *helpinfo = NULL;
1475 /* don't change helper of sibling connections */
1479 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1483 if (!strcmp(helpname, "")) {
1484 if (help && help->helper) {
1485 /* we had a helper before ... */
1486 nf_ct_remove_expectations(ct);
1487 RCU_INIT_POINTER(help->helper, NULL);
1493 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1494 nf_ct_protonum(ct));
1495 if (helper == NULL) {
1496 #ifdef CONFIG_MODULES
1497 spin_unlock_bh(&nf_conntrack_expect_lock);
1499 if (request_module("nfct-helper-%s", helpname) < 0) {
1500 spin_lock_bh(&nf_conntrack_expect_lock);
1504 spin_lock_bh(&nf_conntrack_expect_lock);
1505 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1506 nf_ct_protonum(ct));
1514 if (help->helper == helper) {
1515 /* update private helper data if allowed. */
1516 if (helper->from_nlattr)
1517 helper->from_nlattr(helpinfo, ct);
1523 /* we cannot set a helper for an existing conntrack */
1528 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1530 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1532 if (!del_timer(&ct->timeout))
1535 ct->timeout.expires = jiffies + timeout * HZ;
1536 add_timer(&ct->timeout);
1541 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1542 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1543 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1544 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1548 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1550 const struct nlattr *attr = cda[CTA_PROTOINFO];
1551 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1552 struct nf_conntrack_l4proto *l4proto;
1555 err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1560 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1561 if (l4proto->from_nlattr)
1562 err = l4proto->from_nlattr(tb, ct);
1568 static const struct nla_policy seqadj_policy[CTA_SEQADJ_MAX+1] = {
1569 [CTA_SEQADJ_CORRECTION_POS] = { .type = NLA_U32 },
1570 [CTA_SEQADJ_OFFSET_BEFORE] = { .type = NLA_U32 },
1571 [CTA_SEQADJ_OFFSET_AFTER] = { .type = NLA_U32 },
1575 change_seq_adj(struct nf_ct_seqadj *seq, const struct nlattr * const attr)
1578 struct nlattr *cda[CTA_SEQADJ_MAX+1];
1580 err = nla_parse_nested(cda, CTA_SEQADJ_MAX, attr, seqadj_policy);
1584 if (!cda[CTA_SEQADJ_CORRECTION_POS])
1587 seq->correction_pos =
1588 ntohl(nla_get_be32(cda[CTA_SEQADJ_CORRECTION_POS]));
1590 if (!cda[CTA_SEQADJ_OFFSET_BEFORE])
1593 seq->offset_before =
1594 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_BEFORE]));
1596 if (!cda[CTA_SEQADJ_OFFSET_AFTER])
1600 ntohl(nla_get_be32(cda[CTA_SEQADJ_OFFSET_AFTER]));
1606 ctnetlink_change_seq_adj(struct nf_conn *ct,
1607 const struct nlattr * const cda[])
1609 struct nf_conn_seqadj *seqadj = nfct_seqadj(ct);
1615 if (cda[CTA_SEQ_ADJ_ORIG]) {
1616 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_ORIGINAL],
1617 cda[CTA_SEQ_ADJ_ORIG]);
1621 ct->status |= IPS_SEQ_ADJUST;
1624 if (cda[CTA_SEQ_ADJ_REPLY]) {
1625 ret = change_seq_adj(&seqadj->seq[IP_CT_DIR_REPLY],
1626 cda[CTA_SEQ_ADJ_REPLY]);
1630 ct->status |= IPS_SEQ_ADJUST;
1637 ctnetlink_attach_labels(struct nf_conn *ct, const struct nlattr * const cda[])
1639 #ifdef CONFIG_NF_CONNTRACK_LABELS
1640 size_t len = nla_len(cda[CTA_LABELS]);
1641 const void *mask = cda[CTA_LABELS_MASK];
1643 if (len & (sizeof(u32)-1)) /* must be multiple of u32 */
1647 if (nla_len(cda[CTA_LABELS_MASK]) == 0 ||
1648 nla_len(cda[CTA_LABELS_MASK]) != len)
1650 mask = nla_data(cda[CTA_LABELS_MASK]);
1655 return nf_connlabels_replace(ct, nla_data(cda[CTA_LABELS]), mask, len);
1662 ctnetlink_change_conntrack(struct nf_conn *ct,
1663 const struct nlattr * const cda[])
1667 /* only allow NAT changes and master assignation for new conntracks */
1668 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1671 if (cda[CTA_HELP]) {
1672 err = ctnetlink_change_helper(ct, cda);
1677 if (cda[CTA_TIMEOUT]) {
1678 err = ctnetlink_change_timeout(ct, cda);
1683 if (cda[CTA_STATUS]) {
1684 err = ctnetlink_change_status(ct, cda);
1689 if (cda[CTA_PROTOINFO]) {
1690 err = ctnetlink_change_protoinfo(ct, cda);
1695 #if defined(CONFIG_NF_CONNTRACK_MARK)
1697 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1700 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1701 err = ctnetlink_change_seq_adj(ct, cda);
1706 if (cda[CTA_LABELS]) {
1707 err = ctnetlink_attach_labels(ct, cda);
1715 static struct nf_conn *
1716 ctnetlink_create_conntrack(struct net *net,
1717 const struct nf_conntrack_zone *zone,
1718 const struct nlattr * const cda[],
1719 struct nf_conntrack_tuple *otuple,
1720 struct nf_conntrack_tuple *rtuple,
1725 struct nf_conntrack_helper *helper;
1726 struct nf_conn_tstamp *tstamp;
1728 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1730 return ERR_PTR(-ENOMEM);
1732 if (!cda[CTA_TIMEOUT])
1734 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1736 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1739 if (cda[CTA_HELP]) {
1740 char *helpname = NULL;
1741 struct nlattr *helpinfo = NULL;
1743 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname, &helpinfo);
1747 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1748 nf_ct_protonum(ct));
1749 if (helper == NULL) {
1751 #ifdef CONFIG_MODULES
1752 if (request_module("nfct-helper-%s", helpname) < 0) {
1758 helper = __nf_conntrack_helper_find(helpname,
1760 nf_ct_protonum(ct));
1770 struct nf_conn_help *help;
1772 help = nf_ct_helper_ext_add(ct, helper, GFP_ATOMIC);
1777 /* set private helper data if allowed. */
1778 if (helper->from_nlattr)
1779 helper->from_nlattr(helpinfo, ct);
1781 /* not in hash table yet so not strictly necessary */
1782 RCU_INIT_POINTER(help->helper, helper);
1785 /* try an implicit helper assignation */
1786 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1791 err = ctnetlink_setup_nat(ct, cda);
1795 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1796 nf_ct_tstamp_ext_add(ct, GFP_ATOMIC);
1797 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1798 nf_ct_labels_ext_add(ct);
1800 /* we must add conntrack extensions before confirmation. */
1801 ct->status |= IPS_CONFIRMED;
1803 if (cda[CTA_STATUS]) {
1804 err = ctnetlink_change_status(ct, cda);
1809 if (cda[CTA_SEQ_ADJ_ORIG] || cda[CTA_SEQ_ADJ_REPLY]) {
1810 err = ctnetlink_change_seq_adj(ct, cda);
1815 memset(&ct->proto, 0, sizeof(ct->proto));
1816 if (cda[CTA_PROTOINFO]) {
1817 err = ctnetlink_change_protoinfo(ct, cda);
1822 #if defined(CONFIG_NF_CONNTRACK_MARK)
1824 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1827 /* setup master conntrack: this is a confirmed expectation */
1828 if (cda[CTA_TUPLE_MASTER]) {
1829 struct nf_conntrack_tuple master;
1830 struct nf_conntrack_tuple_hash *master_h;
1831 struct nf_conn *master_ct;
1833 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER,
1838 master_h = nf_conntrack_find_get(net, zone, &master);
1839 if (master_h == NULL) {
1843 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1844 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1845 ct->master = master_ct;
1847 tstamp = nf_conn_tstamp_find(ct);
1849 tstamp->start = ktime_get_real_ns();
1851 err = nf_conntrack_hash_check_insert(ct);
1862 nf_conntrack_free(ct);
1863 return ERR_PTR(err);
1866 static int ctnetlink_new_conntrack(struct net *net, struct sock *ctnl,
1867 struct sk_buff *skb,
1868 const struct nlmsghdr *nlh,
1869 const struct nlattr * const cda[])
1871 struct nf_conntrack_tuple otuple, rtuple;
1872 struct nf_conntrack_tuple_hash *h = NULL;
1873 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1875 u_int8_t u3 = nfmsg->nfgen_family;
1876 struct nf_conntrack_zone zone;
1879 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1883 if (cda[CTA_TUPLE_ORIG]) {
1884 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG,
1890 if (cda[CTA_TUPLE_REPLY]) {
1891 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY,
1897 if (cda[CTA_TUPLE_ORIG])
1898 h = nf_conntrack_find_get(net, &zone, &otuple);
1899 else if (cda[CTA_TUPLE_REPLY])
1900 h = nf_conntrack_find_get(net, &zone, &rtuple);
1904 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1905 enum ip_conntrack_events events;
1907 if (!cda[CTA_TUPLE_ORIG] || !cda[CTA_TUPLE_REPLY])
1910 ct = ctnetlink_create_conntrack(net, &zone, cda, &otuple,
1916 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1917 events = IPCT_RELATED;
1921 if (cda[CTA_LABELS] &&
1922 ctnetlink_attach_labels(ct, cda) == 0)
1923 events |= (1 << IPCT_LABEL);
1925 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1926 (1 << IPCT_ASSURED) |
1927 (1 << IPCT_HELPER) |
1928 (1 << IPCT_PROTOINFO) |
1929 (1 << IPCT_SEQADJ) |
1930 (1 << IPCT_MARK) | events,
1931 ct, NETLINK_CB(skb).portid,
1938 /* implicit 'else' */
1941 ct = nf_ct_tuplehash_to_ctrack(h);
1942 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1943 spin_lock_bh(&nf_conntrack_expect_lock);
1944 err = ctnetlink_change_conntrack(ct, cda);
1945 spin_unlock_bh(&nf_conntrack_expect_lock);
1947 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1948 (1 << IPCT_ASSURED) |
1949 (1 << IPCT_HELPER) |
1951 (1 << IPCT_PROTOINFO) |
1952 (1 << IPCT_SEQADJ) |
1954 ct, NETLINK_CB(skb).portid,
1964 ctnetlink_ct_stat_cpu_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
1965 __u16 cpu, const struct ip_conntrack_stat *st)
1967 struct nlmsghdr *nlh;
1968 struct nfgenmsg *nfmsg;
1969 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
1971 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS_CPU);
1972 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
1976 nfmsg = nlmsg_data(nlh);
1977 nfmsg->nfgen_family = AF_UNSPEC;
1978 nfmsg->version = NFNETLINK_V0;
1979 nfmsg->res_id = htons(cpu);
1981 if (nla_put_be32(skb, CTA_STATS_SEARCHED, htonl(st->searched)) ||
1982 nla_put_be32(skb, CTA_STATS_FOUND, htonl(st->found)) ||
1983 nla_put_be32(skb, CTA_STATS_NEW, htonl(st->new)) ||
1984 nla_put_be32(skb, CTA_STATS_INVALID, htonl(st->invalid)) ||
1985 nla_put_be32(skb, CTA_STATS_IGNORE, htonl(st->ignore)) ||
1986 nla_put_be32(skb, CTA_STATS_DELETE, htonl(st->delete)) ||
1987 nla_put_be32(skb, CTA_STATS_DELETE_LIST, htonl(st->delete_list)) ||
1988 nla_put_be32(skb, CTA_STATS_INSERT, htonl(st->insert)) ||
1989 nla_put_be32(skb, CTA_STATS_INSERT_FAILED,
1990 htonl(st->insert_failed)) ||
1991 nla_put_be32(skb, CTA_STATS_DROP, htonl(st->drop)) ||
1992 nla_put_be32(skb, CTA_STATS_EARLY_DROP, htonl(st->early_drop)) ||
1993 nla_put_be32(skb, CTA_STATS_ERROR, htonl(st->error)) ||
1994 nla_put_be32(skb, CTA_STATS_SEARCH_RESTART,
1995 htonl(st->search_restart)))
1996 goto nla_put_failure;
1998 nlmsg_end(skb, nlh);
2003 nlmsg_cancel(skb, nlh);
2008 ctnetlink_ct_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
2011 struct net *net = sock_net(skb->sk);
2013 if (cb->args[0] == nr_cpu_ids)
2016 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
2017 const struct ip_conntrack_stat *st;
2019 if (!cpu_possible(cpu))
2022 st = per_cpu_ptr(net->ct.stat, cpu);
2023 if (ctnetlink_ct_stat_cpu_fill_info(skb,
2024 NETLINK_CB(cb->skb).portid,
2034 static int ctnetlink_stat_ct_cpu(struct net *net, struct sock *ctnl,
2035 struct sk_buff *skb,
2036 const struct nlmsghdr *nlh,
2037 const struct nlattr * const cda[])
2039 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2040 struct netlink_dump_control c = {
2041 .dump = ctnetlink_ct_stat_cpu_dump,
2043 return netlink_dump_start(ctnl, skb, nlh, &c);
2050 ctnetlink_stat_ct_fill_info(struct sk_buff *skb, u32 portid, u32 seq, u32 type,
2053 struct nlmsghdr *nlh;
2054 struct nfgenmsg *nfmsg;
2055 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
2056 unsigned int nr_conntracks = atomic_read(&net->ct.count);
2058 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_CT_GET_STATS);
2059 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2063 nfmsg = nlmsg_data(nlh);
2064 nfmsg->nfgen_family = AF_UNSPEC;
2065 nfmsg->version = NFNETLINK_V0;
2068 if (nla_put_be32(skb, CTA_STATS_GLOBAL_ENTRIES, htonl(nr_conntracks)))
2069 goto nla_put_failure;
2071 nlmsg_end(skb, nlh);
2076 nlmsg_cancel(skb, nlh);
2080 static int ctnetlink_stat_ct(struct net *net, struct sock *ctnl,
2081 struct sk_buff *skb, const struct nlmsghdr *nlh,
2082 const struct nlattr * const cda[])
2084 struct sk_buff *skb2;
2087 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2091 err = ctnetlink_stat_ct_fill_info(skb2, NETLINK_CB(skb).portid,
2093 NFNL_MSG_TYPE(nlh->nlmsg_type),
2098 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2107 /* this avoids a loop in nfnetlink. */
2108 return err == -EAGAIN ? -ENOBUFS : err;
2111 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
2112 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
2113 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
2114 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
2115 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
2116 [CTA_EXPECT_ID] = { .type = NLA_U32 },
2117 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING,
2118 .len = NF_CT_HELPER_NAME_LEN - 1 },
2119 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
2120 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
2121 [CTA_EXPECT_CLASS] = { .type = NLA_U32 },
2122 [CTA_EXPECT_NAT] = { .type = NLA_NESTED },
2123 [CTA_EXPECT_FN] = { .type = NLA_NUL_STRING },
2126 static struct nf_conntrack_expect *
2127 ctnetlink_alloc_expect(const struct nlattr *const cda[], struct nf_conn *ct,
2128 struct nf_conntrack_helper *helper,
2129 struct nf_conntrack_tuple *tuple,
2130 struct nf_conntrack_tuple *mask);
2132 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
2134 ctnetlink_glue_build_size(const struct nf_conn *ct)
2136 return 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
2137 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
2138 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
2139 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
2140 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
2141 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
2142 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
2143 + nla_total_size(0) /* CTA_PROTOINFO */
2144 + nla_total_size(0) /* CTA_HELP */
2145 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
2146 + ctnetlink_secctx_size(ct)
2147 #ifdef CONFIG_NF_NAT_NEEDED
2148 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
2149 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
2151 #ifdef CONFIG_NF_CONNTRACK_MARK
2152 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
2154 #ifdef CONFIG_NF_CONNTRACK_ZONES
2155 + nla_total_size(sizeof(u_int16_t)) /* CTA_ZONE|CTA_TUPLE_ZONE */
2157 + ctnetlink_proto_size(ct)
2161 static struct nf_conn *ctnetlink_glue_get_ct(const struct sk_buff *skb,
2162 enum ip_conntrack_info *ctinfo)
2166 ct = nf_ct_get(skb, ctinfo);
2167 if (ct && nf_ct_is_untracked(ct))
2173 static int __ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct)
2175 const struct nf_conntrack_zone *zone;
2176 struct nlattr *nest_parms;
2179 zone = nf_ct_zone(ct);
2181 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
2183 goto nla_put_failure;
2184 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
2185 goto nla_put_failure;
2186 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2187 NF_CT_ZONE_DIR_ORIG) < 0)
2188 goto nla_put_failure;
2189 nla_nest_end(skb, nest_parms);
2191 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
2193 goto nla_put_failure;
2194 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
2195 goto nla_put_failure;
2196 if (ctnetlink_dump_zone_id(skb, CTA_TUPLE_ZONE, zone,
2197 NF_CT_ZONE_DIR_REPL) < 0)
2198 goto nla_put_failure;
2199 nla_nest_end(skb, nest_parms);
2201 if (ctnetlink_dump_zone_id(skb, CTA_ZONE, zone,
2202 NF_CT_DEFAULT_ZONE_DIR) < 0)
2203 goto nla_put_failure;
2205 if (ctnetlink_dump_id(skb, ct) < 0)
2206 goto nla_put_failure;
2208 if (ctnetlink_dump_status(skb, ct) < 0)
2209 goto nla_put_failure;
2211 if (ctnetlink_dump_timeout(skb, ct) < 0)
2212 goto nla_put_failure;
2214 if (ctnetlink_dump_protoinfo(skb, ct) < 0)
2215 goto nla_put_failure;
2217 if (ctnetlink_dump_helpinfo(skb, ct) < 0)
2218 goto nla_put_failure;
2220 #ifdef CONFIG_NF_CONNTRACK_SECMARK
2221 if (ct->secmark && ctnetlink_dump_secctx(skb, ct) < 0)
2222 goto nla_put_failure;
2224 if (ct->master && ctnetlink_dump_master(skb, ct) < 0)
2225 goto nla_put_failure;
2227 if ((ct->status & IPS_SEQ_ADJUST) &&
2228 ctnetlink_dump_ct_seq_adj(skb, ct) < 0)
2229 goto nla_put_failure;
2231 #ifdef CONFIG_NF_CONNTRACK_MARK
2232 if (ct->mark && ctnetlink_dump_mark(skb, ct) < 0)
2233 goto nla_put_failure;
2235 if (ctnetlink_dump_labels(skb, ct) < 0)
2236 goto nla_put_failure;
2246 ctnetlink_glue_build(struct sk_buff *skb, struct nf_conn *ct,
2247 enum ip_conntrack_info ctinfo,
2248 u_int16_t ct_attr, u_int16_t ct_info_attr)
2250 struct nlattr *nest_parms;
2252 nest_parms = nla_nest_start(skb, ct_attr | NLA_F_NESTED);
2254 goto nla_put_failure;
2256 if (__ctnetlink_glue_build(skb, ct) < 0)
2257 goto nla_put_failure;
2259 nla_nest_end(skb, nest_parms);
2261 if (nla_put_be32(skb, ct_info_attr, htonl(ctinfo)))
2262 goto nla_put_failure;
2271 ctnetlink_glue_parse_ct(const struct nlattr *cda[], struct nf_conn *ct)
2275 if (cda[CTA_TIMEOUT]) {
2276 err = ctnetlink_change_timeout(ct, cda);
2280 if (cda[CTA_STATUS]) {
2281 err = ctnetlink_change_status(ct, cda);
2285 if (cda[CTA_HELP]) {
2286 err = ctnetlink_change_helper(ct, cda);
2290 if (cda[CTA_LABELS]) {
2291 err = ctnetlink_attach_labels(ct, cda);
2295 #if defined(CONFIG_NF_CONNTRACK_MARK)
2296 if (cda[CTA_MARK]) {
2297 u32 mask = 0, mark, newmark;
2298 if (cda[CTA_MARK_MASK])
2299 mask = ~ntohl(nla_get_be32(cda[CTA_MARK_MASK]));
2301 mark = ntohl(nla_get_be32(cda[CTA_MARK]));
2302 newmark = (ct->mark & mask) ^ mark;
2303 if (newmark != ct->mark)
2311 ctnetlink_glue_parse(const struct nlattr *attr, struct nf_conn *ct)
2313 struct nlattr *cda[CTA_MAX+1];
2316 ret = nla_parse_nested(cda, CTA_MAX, attr, ct_nla_policy);
2320 spin_lock_bh(&nf_conntrack_expect_lock);
2321 ret = ctnetlink_glue_parse_ct((const struct nlattr **)cda, ct);
2322 spin_unlock_bh(&nf_conntrack_expect_lock);
2327 static int ctnetlink_glue_exp_parse(const struct nlattr * const *cda,
2328 const struct nf_conn *ct,
2329 struct nf_conntrack_tuple *tuple,
2330 struct nf_conntrack_tuple *mask)
2334 err = ctnetlink_parse_tuple(cda, tuple, CTA_EXPECT_TUPLE,
2335 nf_ct_l3num(ct), NULL);
2339 return ctnetlink_parse_tuple(cda, mask, CTA_EXPECT_MASK,
2340 nf_ct_l3num(ct), NULL);
2344 ctnetlink_glue_attach_expect(const struct nlattr *attr, struct nf_conn *ct,
2345 u32 portid, u32 report)
2347 struct nlattr *cda[CTA_EXPECT_MAX+1];
2348 struct nf_conntrack_tuple tuple, mask;
2349 struct nf_conntrack_helper *helper = NULL;
2350 struct nf_conntrack_expect *exp;
2353 err = nla_parse_nested(cda, CTA_EXPECT_MAX, attr, exp_nla_policy);
2357 err = ctnetlink_glue_exp_parse((const struct nlattr * const *)cda,
2362 if (cda[CTA_EXPECT_HELP_NAME]) {
2363 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2365 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
2366 nf_ct_protonum(ct));
2371 exp = ctnetlink_alloc_expect((const struct nlattr * const *)cda, ct,
2372 helper, &tuple, &mask);
2374 return PTR_ERR(exp);
2376 err = nf_ct_expect_related_report(exp, portid, report);
2378 nf_ct_expect_put(exp);
2385 static void ctnetlink_glue_seqadj(struct sk_buff *skb, struct nf_conn *ct,
2386 enum ip_conntrack_info ctinfo, int diff)
2388 if (!(ct->status & IPS_NAT_MASK))
2391 nf_ct_tcp_seqadj_set(skb, ct, ctinfo, diff);
2394 static struct nfnl_ct_hook ctnetlink_glue_hook = {
2395 .get_ct = ctnetlink_glue_get_ct,
2396 .build_size = ctnetlink_glue_build_size,
2397 .build = ctnetlink_glue_build,
2398 .parse = ctnetlink_glue_parse,
2399 .attach_expect = ctnetlink_glue_attach_expect,
2400 .seq_adjust = ctnetlink_glue_seqadj,
2402 #endif /* CONFIG_NETFILTER_NETLINK_GLUE_CT */
2404 /***********************************************************************
2406 ***********************************************************************/
2409 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
2410 const struct nf_conntrack_tuple *tuple,
2411 enum ctattr_expect type)
2413 struct nlattr *nest_parms;
2415 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
2417 goto nla_put_failure;
2418 if (ctnetlink_dump_tuples(skb, tuple) < 0)
2419 goto nla_put_failure;
2420 nla_nest_end(skb, nest_parms);
2429 ctnetlink_exp_dump_mask(struct sk_buff *skb,
2430 const struct nf_conntrack_tuple *tuple,
2431 const struct nf_conntrack_tuple_mask *mask)
2434 struct nf_conntrack_l3proto *l3proto;
2435 struct nf_conntrack_l4proto *l4proto;
2436 struct nf_conntrack_tuple m;
2437 struct nlattr *nest_parms;
2439 memset(&m, 0xFF, sizeof(m));
2440 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
2441 m.src.u.all = mask->src.u.all;
2442 m.dst.protonum = tuple->dst.protonum;
2444 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
2446 goto nla_put_failure;
2449 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
2450 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
2452 l4proto = __nf_ct_l4proto_find(tuple->src.l3num,
2453 tuple->dst.protonum);
2454 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
2458 if (unlikely(ret < 0))
2459 goto nla_put_failure;
2461 nla_nest_end(skb, nest_parms);
2469 static const union nf_inet_addr any_addr;
2472 ctnetlink_exp_dump_expect(struct sk_buff *skb,
2473 const struct nf_conntrack_expect *exp)
2475 struct nf_conn *master = exp->master;
2476 long timeout = ((long)exp->timeout.expires - (long)jiffies) / HZ;
2477 struct nf_conn_help *help;
2478 #ifdef CONFIG_NF_NAT_NEEDED
2479 struct nlattr *nest_parms;
2480 struct nf_conntrack_tuple nat_tuple = {};
2482 struct nf_ct_helper_expectfn *expfn;
2487 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
2488 goto nla_put_failure;
2489 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
2490 goto nla_put_failure;
2491 if (ctnetlink_exp_dump_tuple(skb,
2492 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
2493 CTA_EXPECT_MASTER) < 0)
2494 goto nla_put_failure;
2496 #ifdef CONFIG_NF_NAT_NEEDED
2497 if (!nf_inet_addr_cmp(&exp->saved_addr, &any_addr) ||
2498 exp->saved_proto.all) {
2499 nest_parms = nla_nest_start(skb, CTA_EXPECT_NAT | NLA_F_NESTED);
2501 goto nla_put_failure;
2503 if (nla_put_be32(skb, CTA_EXPECT_NAT_DIR, htonl(exp->dir)))
2504 goto nla_put_failure;
2506 nat_tuple.src.l3num = nf_ct_l3num(master);
2507 nat_tuple.src.u3 = exp->saved_addr;
2508 nat_tuple.dst.protonum = nf_ct_protonum(master);
2509 nat_tuple.src.u = exp->saved_proto;
2511 if (ctnetlink_exp_dump_tuple(skb, &nat_tuple,
2512 CTA_EXPECT_NAT_TUPLE) < 0)
2513 goto nla_put_failure;
2514 nla_nest_end(skb, nest_parms);
2517 if (nla_put_be32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout)) ||
2518 nla_put_be32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp)) ||
2519 nla_put_be32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags)) ||
2520 nla_put_be32(skb, CTA_EXPECT_CLASS, htonl(exp->class)))
2521 goto nla_put_failure;
2522 help = nfct_help(master);
2524 struct nf_conntrack_helper *helper;
2526 helper = rcu_dereference(help->helper);
2528 nla_put_string(skb, CTA_EXPECT_HELP_NAME, helper->name))
2529 goto nla_put_failure;
2531 expfn = nf_ct_helper_expectfn_find_by_symbol(exp->expectfn);
2532 if (expfn != NULL &&
2533 nla_put_string(skb, CTA_EXPECT_FN, expfn->name))
2534 goto nla_put_failure;
2543 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 portid, u32 seq,
2544 int event, const struct nf_conntrack_expect *exp)
2546 struct nlmsghdr *nlh;
2547 struct nfgenmsg *nfmsg;
2548 unsigned int flags = portid ? NLM_F_MULTI : 0;
2550 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2551 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
2555 nfmsg = nlmsg_data(nlh);
2556 nfmsg->nfgen_family = exp->tuple.src.l3num;
2557 nfmsg->version = NFNETLINK_V0;
2560 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2561 goto nla_put_failure;
2563 nlmsg_end(skb, nlh);
2568 nlmsg_cancel(skb, nlh);
2572 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2574 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
2576 struct nf_conntrack_expect *exp = item->exp;
2577 struct net *net = nf_ct_exp_net(exp);
2578 struct nlmsghdr *nlh;
2579 struct nfgenmsg *nfmsg;
2580 struct sk_buff *skb;
2581 unsigned int type, group;
2584 if (events & (1 << IPEXP_DESTROY)) {
2585 type = IPCTNL_MSG_EXP_DELETE;
2586 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
2587 } else if (events & (1 << IPEXP_NEW)) {
2588 type = IPCTNL_MSG_EXP_NEW;
2589 flags = NLM_F_CREATE|NLM_F_EXCL;
2590 group = NFNLGRP_CONNTRACK_EXP_NEW;
2594 if (!item->report && !nfnetlink_has_listeners(net, group))
2597 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
2601 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
2602 nlh = nlmsg_put(skb, item->portid, 0, type, sizeof(*nfmsg), flags);
2606 nfmsg = nlmsg_data(nlh);
2607 nfmsg->nfgen_family = exp->tuple.src.l3num;
2608 nfmsg->version = NFNETLINK_V0;
2612 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
2613 goto nla_put_failure;
2616 nlmsg_end(skb, nlh);
2617 nfnetlink_send(skb, net, item->portid, group, item->report, GFP_ATOMIC);
2622 nlmsg_cancel(skb, nlh);
2626 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
2630 static int ctnetlink_exp_done(struct netlink_callback *cb)
2633 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
2638 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2640 struct net *net = sock_net(skb->sk);
2641 struct nf_conntrack_expect *exp, *last;
2642 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2643 u_int8_t l3proto = nfmsg->nfgen_family;
2646 last = (struct nf_conntrack_expect *)cb->args[1];
2647 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
2649 hlist_for_each_entry(exp, &net->ct.expect_hash[cb->args[0]],
2651 if (l3proto && exp->tuple.src.l3num != l3proto)
2658 if (ctnetlink_exp_fill_info(skb,
2659 NETLINK_CB(cb->skb).portid,
2663 if (!atomic_inc_not_zero(&exp->use))
2665 cb->args[1] = (unsigned long)exp;
2677 nf_ct_expect_put(last);
2683 ctnetlink_exp_ct_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
2685 struct nf_conntrack_expect *exp, *last;
2686 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
2687 struct nf_conn *ct = cb->data;
2688 struct nf_conn_help *help = nfct_help(ct);
2689 u_int8_t l3proto = nfmsg->nfgen_family;
2695 last = (struct nf_conntrack_expect *)cb->args[1];
2697 hlist_for_each_entry(exp, &help->expectations, lnode) {
2698 if (l3proto && exp->tuple.src.l3num != l3proto)
2705 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).portid,
2709 if (!atomic_inc_not_zero(&exp->use))
2711 cb->args[1] = (unsigned long)exp;
2723 nf_ct_expect_put(last);
2728 static int ctnetlink_dump_exp_ct(struct net *net, struct sock *ctnl,
2729 struct sk_buff *skb,
2730 const struct nlmsghdr *nlh,
2731 const struct nlattr * const cda[])
2734 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2735 u_int8_t u3 = nfmsg->nfgen_family;
2736 struct nf_conntrack_tuple tuple;
2737 struct nf_conntrack_tuple_hash *h;
2739 struct nf_conntrack_zone zone;
2740 struct netlink_dump_control c = {
2741 .dump = ctnetlink_exp_ct_dump_table,
2742 .done = ctnetlink_exp_done,
2745 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2750 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2754 h = nf_conntrack_find_get(net, &zone, &tuple);
2758 ct = nf_ct_tuplehash_to_ctrack(h);
2761 err = netlink_dump_start(ctnl, skb, nlh, &c);
2767 static int ctnetlink_get_expect(struct net *net, struct sock *ctnl,
2768 struct sk_buff *skb, const struct nlmsghdr *nlh,
2769 const struct nlattr * const cda[])
2771 struct nf_conntrack_tuple tuple;
2772 struct nf_conntrack_expect *exp;
2773 struct sk_buff *skb2;
2774 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2775 u_int8_t u3 = nfmsg->nfgen_family;
2776 struct nf_conntrack_zone zone;
2779 if (nlh->nlmsg_flags & NLM_F_DUMP) {
2780 if (cda[CTA_EXPECT_MASTER])
2781 return ctnetlink_dump_exp_ct(net, ctnl, skb, nlh, cda);
2783 struct netlink_dump_control c = {
2784 .dump = ctnetlink_exp_dump_table,
2785 .done = ctnetlink_exp_done,
2787 return netlink_dump_start(ctnl, skb, nlh, &c);
2791 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2795 if (cda[CTA_EXPECT_TUPLE])
2796 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2798 else if (cda[CTA_EXPECT_MASTER])
2799 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER,
2807 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2811 if (cda[CTA_EXPECT_ID]) {
2812 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2813 if (ntohl(id) != (u32)(unsigned long)exp) {
2814 nf_ct_expect_put(exp);
2820 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2822 nf_ct_expect_put(exp);
2827 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).portid,
2828 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
2830 nf_ct_expect_put(exp);
2834 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
2843 /* this avoids a loop in nfnetlink. */
2844 return err == -EAGAIN ? -ENOBUFS : err;
2847 static int ctnetlink_del_expect(struct net *net, struct sock *ctnl,
2848 struct sk_buff *skb, const struct nlmsghdr *nlh,
2849 const struct nlattr * const cda[])
2851 struct nf_conntrack_expect *exp;
2852 struct nf_conntrack_tuple tuple;
2853 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2854 struct hlist_node *next;
2855 u_int8_t u3 = nfmsg->nfgen_family;
2856 struct nf_conntrack_zone zone;
2860 if (cda[CTA_EXPECT_TUPLE]) {
2861 /* delete a single expect by tuple */
2862 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2866 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
2871 /* bump usage count to 2 */
2872 exp = nf_ct_expect_find_get(net, &zone, &tuple);
2876 if (cda[CTA_EXPECT_ID]) {
2877 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
2878 if (ntohl(id) != (u32)(unsigned long)exp) {
2879 nf_ct_expect_put(exp);
2884 /* after list removal, usage count == 1 */
2885 spin_lock_bh(&nf_conntrack_expect_lock);
2886 if (del_timer(&exp->timeout)) {
2887 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).portid,
2889 nf_ct_expect_put(exp);
2891 spin_unlock_bh(&nf_conntrack_expect_lock);
2892 /* have to put what we 'get' above.
2893 * after this line usage count == 0 */
2894 nf_ct_expect_put(exp);
2895 } else if (cda[CTA_EXPECT_HELP_NAME]) {
2896 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
2897 struct nf_conn_help *m_help;
2899 /* delete all expectations for this helper */
2900 spin_lock_bh(&nf_conntrack_expect_lock);
2901 for (i = 0; i < nf_ct_expect_hsize; i++) {
2902 hlist_for_each_entry_safe(exp, next,
2903 &net->ct.expect_hash[i],
2905 m_help = nfct_help(exp->master);
2906 if (!strcmp(m_help->helper->name, name) &&
2907 del_timer(&exp->timeout)) {
2908 nf_ct_unlink_expect_report(exp,
2909 NETLINK_CB(skb).portid,
2911 nf_ct_expect_put(exp);
2915 spin_unlock_bh(&nf_conntrack_expect_lock);
2917 /* This basically means we have to flush everything*/
2918 spin_lock_bh(&nf_conntrack_expect_lock);
2919 for (i = 0; i < nf_ct_expect_hsize; i++) {
2920 hlist_for_each_entry_safe(exp, next,
2921 &net->ct.expect_hash[i],
2923 if (del_timer(&exp->timeout)) {
2924 nf_ct_unlink_expect_report(exp,
2925 NETLINK_CB(skb).portid,
2927 nf_ct_expect_put(exp);
2931 spin_unlock_bh(&nf_conntrack_expect_lock);
2937 ctnetlink_change_expect(struct nf_conntrack_expect *x,
2938 const struct nlattr * const cda[])
2940 if (cda[CTA_EXPECT_TIMEOUT]) {
2941 if (!del_timer(&x->timeout))
2944 x->timeout.expires = jiffies +
2945 ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
2946 add_timer(&x->timeout);
2951 static const struct nla_policy exp_nat_nla_policy[CTA_EXPECT_NAT_MAX+1] = {
2952 [CTA_EXPECT_NAT_DIR] = { .type = NLA_U32 },
2953 [CTA_EXPECT_NAT_TUPLE] = { .type = NLA_NESTED },
2957 ctnetlink_parse_expect_nat(const struct nlattr *attr,
2958 struct nf_conntrack_expect *exp,
2961 #ifdef CONFIG_NF_NAT_NEEDED
2962 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
2963 struct nf_conntrack_tuple nat_tuple = {};
2966 err = nla_parse_nested(tb, CTA_EXPECT_NAT_MAX, attr, exp_nat_nla_policy);
2970 if (!tb[CTA_EXPECT_NAT_DIR] || !tb[CTA_EXPECT_NAT_TUPLE])
2973 err = ctnetlink_parse_tuple((const struct nlattr * const *)tb,
2974 &nat_tuple, CTA_EXPECT_NAT_TUPLE,
2979 exp->saved_addr = nat_tuple.src.u3;
2980 exp->saved_proto = nat_tuple.src.u;
2981 exp->dir = ntohl(nla_get_be32(tb[CTA_EXPECT_NAT_DIR]));
2989 static struct nf_conntrack_expect *
2990 ctnetlink_alloc_expect(const struct nlattr * const cda[], struct nf_conn *ct,
2991 struct nf_conntrack_helper *helper,
2992 struct nf_conntrack_tuple *tuple,
2993 struct nf_conntrack_tuple *mask)
2995 u_int32_t class = 0;
2996 struct nf_conntrack_expect *exp;
2997 struct nf_conn_help *help;
3000 if (cda[CTA_EXPECT_CLASS] && helper) {
3001 class = ntohl(nla_get_be32(cda[CTA_EXPECT_CLASS]));
3002 if (class > helper->expect_class_max)
3003 return ERR_PTR(-EINVAL);
3005 exp = nf_ct_expect_alloc(ct);
3007 return ERR_PTR(-ENOMEM);
3009 help = nfct_help(ct);
3011 if (!cda[CTA_EXPECT_TIMEOUT]) {
3015 exp->timeout.expires =
3016 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
3018 exp->flags = NF_CT_EXPECT_USERSPACE;
3019 if (cda[CTA_EXPECT_FLAGS]) {
3021 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3024 if (cda[CTA_EXPECT_FLAGS]) {
3025 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
3026 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
3030 if (cda[CTA_EXPECT_FN]) {
3031 const char *name = nla_data(cda[CTA_EXPECT_FN]);
3032 struct nf_ct_helper_expectfn *expfn;
3034 expfn = nf_ct_helper_expectfn_find_by_name(name);
3035 if (expfn == NULL) {
3039 exp->expectfn = expfn->expectfn;
3041 exp->expectfn = NULL;
3045 exp->helper = helper;
3046 exp->tuple = *tuple;
3047 exp->mask.src.u3 = mask->src.u3;
3048 exp->mask.src.u.all = mask->src.u.all;
3050 if (cda[CTA_EXPECT_NAT]) {
3051 err = ctnetlink_parse_expect_nat(cda[CTA_EXPECT_NAT],
3052 exp, nf_ct_l3num(ct));
3058 nf_ct_expect_put(exp);
3059 return ERR_PTR(err);
3063 ctnetlink_create_expect(struct net *net,
3064 const struct nf_conntrack_zone *zone,
3065 const struct nlattr * const cda[],
3066 u_int8_t u3, u32 portid, int report)
3068 struct nf_conntrack_tuple tuple, mask, master_tuple;
3069 struct nf_conntrack_tuple_hash *h = NULL;
3070 struct nf_conntrack_helper *helper = NULL;
3071 struct nf_conntrack_expect *exp;
3075 /* caller guarantees that those three CTA_EXPECT_* exist */
3076 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3080 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK,
3084 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER,
3089 /* Look for master conntrack of this expectation */
3090 h = nf_conntrack_find_get(net, zone, &master_tuple);
3093 ct = nf_ct_tuplehash_to_ctrack(h);
3095 if (cda[CTA_EXPECT_HELP_NAME]) {
3096 const char *helpname = nla_data(cda[CTA_EXPECT_HELP_NAME]);
3098 helper = __nf_conntrack_helper_find(helpname, u3,
3099 nf_ct_protonum(ct));
3100 if (helper == NULL) {
3101 #ifdef CONFIG_MODULES
3102 if (request_module("nfct-helper-%s", helpname) < 0) {
3106 helper = __nf_conntrack_helper_find(helpname, u3,
3107 nf_ct_protonum(ct));
3118 exp = ctnetlink_alloc_expect(cda, ct, helper, &tuple, &mask);
3124 err = nf_ct_expect_related_report(exp, portid, report);
3125 nf_ct_expect_put(exp);
3131 static int ctnetlink_new_expect(struct net *net, struct sock *ctnl,
3132 struct sk_buff *skb, const struct nlmsghdr *nlh,
3133 const struct nlattr * const cda[])
3135 struct nf_conntrack_tuple tuple;
3136 struct nf_conntrack_expect *exp;
3137 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
3138 u_int8_t u3 = nfmsg->nfgen_family;
3139 struct nf_conntrack_zone zone;
3142 if (!cda[CTA_EXPECT_TUPLE]
3143 || !cda[CTA_EXPECT_MASK]
3144 || !cda[CTA_EXPECT_MASTER])
3147 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
3151 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE,
3156 spin_lock_bh(&nf_conntrack_expect_lock);
3157 exp = __nf_ct_expect_find(net, &zone, &tuple);
3159 spin_unlock_bh(&nf_conntrack_expect_lock);
3161 if (nlh->nlmsg_flags & NLM_F_CREATE) {
3162 err = ctnetlink_create_expect(net, &zone, cda, u3,
3163 NETLINK_CB(skb).portid,
3170 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
3171 err = ctnetlink_change_expect(exp, cda);
3172 spin_unlock_bh(&nf_conntrack_expect_lock);
3178 ctnetlink_exp_stat_fill_info(struct sk_buff *skb, u32 portid, u32 seq, int cpu,
3179 const struct ip_conntrack_stat *st)
3181 struct nlmsghdr *nlh;
3182 struct nfgenmsg *nfmsg;
3183 unsigned int flags = portid ? NLM_F_MULTI : 0, event;
3185 event = (NFNL_SUBSYS_CTNETLINK << 8 | IPCTNL_MSG_EXP_GET_STATS_CPU);
3186 nlh = nlmsg_put(skb, portid, seq, event, sizeof(*nfmsg), flags);
3190 nfmsg = nlmsg_data(nlh);
3191 nfmsg->nfgen_family = AF_UNSPEC;
3192 nfmsg->version = NFNETLINK_V0;
3193 nfmsg->res_id = htons(cpu);
3195 if (nla_put_be32(skb, CTA_STATS_EXP_NEW, htonl(st->expect_new)) ||
3196 nla_put_be32(skb, CTA_STATS_EXP_CREATE, htonl(st->expect_create)) ||
3197 nla_put_be32(skb, CTA_STATS_EXP_DELETE, htonl(st->expect_delete)))
3198 goto nla_put_failure;
3200 nlmsg_end(skb, nlh);
3205 nlmsg_cancel(skb, nlh);
3210 ctnetlink_exp_stat_cpu_dump(struct sk_buff *skb, struct netlink_callback *cb)
3213 struct net *net = sock_net(skb->sk);
3215 if (cb->args[0] == nr_cpu_ids)
3218 for (cpu = cb->args[0]; cpu < nr_cpu_ids; cpu++) {
3219 const struct ip_conntrack_stat *st;
3221 if (!cpu_possible(cpu))
3224 st = per_cpu_ptr(net->ct.stat, cpu);
3225 if (ctnetlink_exp_stat_fill_info(skb, NETLINK_CB(cb->skb).portid,
3235 static int ctnetlink_stat_exp_cpu(struct net *net, struct sock *ctnl,
3236 struct sk_buff *skb,
3237 const struct nlmsghdr *nlh,
3238 const struct nlattr * const cda[])
3240 if (nlh->nlmsg_flags & NLM_F_DUMP) {
3241 struct netlink_dump_control c = {
3242 .dump = ctnetlink_exp_stat_cpu_dump,
3244 return netlink_dump_start(ctnl, skb, nlh, &c);
3250 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3251 static struct nf_ct_event_notifier ctnl_notifier = {
3252 .fcn = ctnetlink_conntrack_event,
3255 static struct nf_exp_event_notifier ctnl_notifier_exp = {
3256 .fcn = ctnetlink_expect_event,
3260 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
3261 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
3262 .attr_count = CTA_MAX,
3263 .policy = ct_nla_policy },
3264 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
3265 .attr_count = CTA_MAX,
3266 .policy = ct_nla_policy },
3267 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
3268 .attr_count = CTA_MAX,
3269 .policy = ct_nla_policy },
3270 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
3271 .attr_count = CTA_MAX,
3272 .policy = ct_nla_policy },
3273 [IPCTNL_MSG_CT_GET_STATS_CPU] = { .call = ctnetlink_stat_ct_cpu },
3274 [IPCTNL_MSG_CT_GET_STATS] = { .call = ctnetlink_stat_ct },
3275 [IPCTNL_MSG_CT_GET_DYING] = { .call = ctnetlink_get_ct_dying },
3276 [IPCTNL_MSG_CT_GET_UNCONFIRMED] = { .call = ctnetlink_get_ct_unconfirmed },
3279 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
3280 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
3281 .attr_count = CTA_EXPECT_MAX,
3282 .policy = exp_nla_policy },
3283 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
3284 .attr_count = CTA_EXPECT_MAX,
3285 .policy = exp_nla_policy },
3286 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
3287 .attr_count = CTA_EXPECT_MAX,
3288 .policy = exp_nla_policy },
3289 [IPCTNL_MSG_EXP_GET_STATS_CPU] = { .call = ctnetlink_stat_exp_cpu },
3292 static const struct nfnetlink_subsystem ctnl_subsys = {
3293 .name = "conntrack",
3294 .subsys_id = NFNL_SUBSYS_CTNETLINK,
3295 .cb_count = IPCTNL_MSG_MAX,
3299 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
3300 .name = "conntrack_expect",
3301 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
3302 .cb_count = IPCTNL_MSG_EXP_MAX,
3306 MODULE_ALIAS("ip_conntrack_netlink");
3307 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
3308 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
3310 static int __net_init ctnetlink_net_init(struct net *net)
3312 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3315 ret = nf_conntrack_register_notifier(net, &ctnl_notifier);
3317 pr_err("ctnetlink_init: cannot register notifier.\n");
3321 ret = nf_ct_expect_register_notifier(net, &ctnl_notifier_exp);
3323 pr_err("ctnetlink_init: cannot expect register notifier.\n");
3324 goto err_unreg_notifier;
3329 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3331 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3337 static void ctnetlink_net_exit(struct net *net)
3339 #ifdef CONFIG_NF_CONNTRACK_EVENTS
3340 nf_ct_expect_unregister_notifier(net, &ctnl_notifier_exp);
3341 nf_conntrack_unregister_notifier(net, &ctnl_notifier);
3345 static void __net_exit ctnetlink_net_exit_batch(struct list_head *net_exit_list)
3349 list_for_each_entry(net, net_exit_list, exit_list)
3350 ctnetlink_net_exit(net);
3353 static struct pernet_operations ctnetlink_net_ops = {
3354 .init = ctnetlink_net_init,
3355 .exit_batch = ctnetlink_net_exit_batch,
3358 static int __init ctnetlink_init(void)
3362 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
3363 ret = nfnetlink_subsys_register(&ctnl_subsys);
3365 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
3369 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
3371 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
3372 goto err_unreg_subsys;
3375 ret = register_pernet_subsys(&ctnetlink_net_ops);
3377 pr_err("ctnetlink_init: cannot register pernet operations\n");
3378 goto err_unreg_exp_subsys;
3380 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3381 /* setup interaction between nf_queue and nf_conntrack_netlink. */
3382 RCU_INIT_POINTER(nfnl_ct_hook, &ctnetlink_glue_hook);
3386 err_unreg_exp_subsys:
3387 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3389 nfnetlink_subsys_unregister(&ctnl_subsys);
3394 static void __exit ctnetlink_exit(void)
3396 pr_info("ctnetlink: unregistering from nfnetlink.\n");
3398 unregister_pernet_subsys(&ctnetlink_net_ops);
3399 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
3400 nfnetlink_subsys_unregister(&ctnl_subsys);
3401 #ifdef CONFIG_NETFILTER_NETLINK_GLUE_CT
3402 RCU_INIT_POINTER(nfnl_ct_hook, NULL);
3406 module_init(ctnetlink_init);
3407 module_exit(ctnetlink_exit);