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-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
9 * Initial connection tracking via netlink development funded and
10 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/rculist.h>
22 #include <linux/rculist_nulls.h>
23 #include <linux/types.h>
24 #include <linux/timer.h>
25 #include <linux/security.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/interrupt.h>
31 #include <linux/slab.h>
33 #include <linux/netfilter.h>
34 #include <net/netlink.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_expect.h>
39 #include <net/netfilter/nf_conntrack_helper.h>
40 #include <net/netfilter/nf_conntrack_l3proto.h>
41 #include <net/netfilter/nf_conntrack_l4proto.h>
42 #include <net/netfilter/nf_conntrack_tuple.h>
43 #include <net/netfilter/nf_conntrack_acct.h>
44 #include <net/netfilter/nf_conntrack_zones.h>
45 #ifdef CONFIG_NF_NAT_NEEDED
46 #include <net/netfilter/nf_nat_core.h>
47 #include <net/netfilter/nf_nat_protocol.h>
50 #include <linux/netfilter/nfnetlink.h>
51 #include <linux/netfilter/nfnetlink_conntrack.h>
53 MODULE_LICENSE("GPL");
55 static char __initdata version[] = "0.93";
58 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
59 const struct nf_conntrack_tuple *tuple,
60 struct nf_conntrack_l4proto *l4proto)
63 struct nlattr *nest_parms;
65 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
68 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
70 if (likely(l4proto->tuple_to_nlattr))
71 ret = l4proto->tuple_to_nlattr(skb, tuple);
73 nla_nest_end(skb, nest_parms);
82 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
83 const struct nf_conntrack_tuple *tuple,
84 struct nf_conntrack_l3proto *l3proto)
87 struct nlattr *nest_parms;
89 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
93 if (likely(l3proto->tuple_to_nlattr))
94 ret = l3proto->tuple_to_nlattr(skb, tuple);
96 nla_nest_end(skb, nest_parms);
105 ctnetlink_dump_tuples(struct sk_buff *skb,
106 const struct nf_conntrack_tuple *tuple)
109 struct nf_conntrack_l3proto *l3proto;
110 struct nf_conntrack_l4proto *l4proto;
112 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
113 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
115 if (unlikely(ret < 0))
118 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
119 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
125 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
127 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
135 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
137 long timeout = (ct->timeout.expires - jiffies) / HZ;
142 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
150 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
152 struct nf_conntrack_l4proto *l4proto;
153 struct nlattr *nest_proto;
156 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
157 if (!l4proto->to_nlattr)
160 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
162 goto nla_put_failure;
164 ret = l4proto->to_nlattr(skb, nest_proto, ct);
166 nla_nest_end(skb, nest_proto);
175 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
177 struct nlattr *nest_helper;
178 const struct nf_conn_help *help = nfct_help(ct);
179 struct nf_conntrack_helper *helper;
184 helper = rcu_dereference(help->helper);
188 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
190 goto nla_put_failure;
191 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
193 if (helper->to_nlattr)
194 helper->to_nlattr(skb, ct);
196 nla_nest_end(skb, nest_helper);
205 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
206 enum ip_conntrack_dir dir)
208 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
209 struct nlattr *nest_count;
210 const struct nf_conn_counter *acct;
212 acct = nf_conn_acct_find(ct);
216 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
218 goto nla_put_failure;
220 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
221 cpu_to_be64(acct[dir].packets));
222 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
223 cpu_to_be64(acct[dir].bytes));
225 nla_nest_end(skb, nest_count);
233 #ifdef CONFIG_NF_CONNTRACK_MARK
235 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
237 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
244 #define ctnetlink_dump_mark(a, b) (0)
247 #ifdef CONFIG_NF_CONNTRACK_SECMARK
249 ctnetlink_dump_secctx(struct sk_buff *skb, const struct nf_conn *ct)
251 struct nlattr *nest_secctx;
255 ret = security_secid_to_secctx(ct->secmark, &secctx, &len);
260 nest_secctx = nla_nest_start(skb, CTA_SECCTX | NLA_F_NESTED);
262 goto nla_put_failure;
264 NLA_PUT_STRING(skb, CTA_SECCTX_NAME, secctx);
265 nla_nest_end(skb, nest_secctx);
269 security_release_secctx(secctx, len);
273 #define ctnetlink_dump_secctx(a, b) (0)
276 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
279 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
281 struct nlattr *nest_parms;
283 if (!(ct->status & IPS_EXPECTED))
286 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
288 goto nla_put_failure;
289 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
290 goto nla_put_failure;
291 nla_nest_end(skb, nest_parms);
299 #ifdef CONFIG_NF_NAT_NEEDED
301 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
303 struct nlattr *nest_parms;
305 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
307 goto nla_put_failure;
309 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
310 htonl(natseq->correction_pos));
311 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
312 htonl(natseq->offset_before));
313 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
314 htonl(natseq->offset_after));
316 nla_nest_end(skb, nest_parms);
325 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
327 struct nf_nat_seq *natseq;
328 struct nf_conn_nat *nat = nfct_nat(ct);
330 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
333 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
334 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
337 natseq = &nat->seq[IP_CT_DIR_REPLY];
338 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
344 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
348 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
350 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
358 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
360 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
368 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
369 int event, struct nf_conn *ct)
371 struct nlmsghdr *nlh;
372 struct nfgenmsg *nfmsg;
373 struct nlattr *nest_parms;
374 unsigned int flags = pid ? NLM_F_MULTI : 0;
376 event |= NFNL_SUBSYS_CTNETLINK << 8;
377 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
381 nfmsg = nlmsg_data(nlh);
382 nfmsg->nfgen_family = nf_ct_l3num(ct);
383 nfmsg->version = NFNETLINK_V0;
386 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
388 goto nla_put_failure;
389 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
390 goto nla_put_failure;
391 nla_nest_end(skb, nest_parms);
393 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
395 goto nla_put_failure;
396 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
397 goto nla_put_failure;
398 nla_nest_end(skb, nest_parms);
401 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
403 if (ctnetlink_dump_status(skb, ct) < 0 ||
404 ctnetlink_dump_timeout(skb, ct) < 0 ||
405 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
406 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
407 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
408 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
409 ctnetlink_dump_mark(skb, ct) < 0 ||
410 ctnetlink_dump_secctx(skb, ct) < 0 ||
411 ctnetlink_dump_id(skb, ct) < 0 ||
412 ctnetlink_dump_use(skb, ct) < 0 ||
413 ctnetlink_dump_master(skb, ct) < 0 ||
414 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
415 goto nla_put_failure;
422 nlmsg_cancel(skb, nlh);
426 #ifdef CONFIG_NF_CONNTRACK_EVENTS
428 ctnetlink_proto_size(const struct nf_conn *ct)
430 struct nf_conntrack_l3proto *l3proto;
431 struct nf_conntrack_l4proto *l4proto;
435 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
436 len += l3proto->nla_size;
438 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
439 len += l4proto->nla_size;
446 ctnetlink_counters_size(const struct nf_conn *ct)
448 if (!nf_ct_ext_exist(ct, NF_CT_EXT_ACCT))
450 return 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
451 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
452 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
457 ctnetlink_secctx_size(const struct nf_conn *ct)
459 #ifdef CONFIG_NF_CONNTRACK_SECMARK
462 ret = security_secid_to_secctx(ct->secmark, NULL, &len);
466 return nla_total_size(0) /* CTA_SECCTX */
467 + nla_total_size(sizeof(char) * len); /* CTA_SECCTX_NAME */
474 ctnetlink_nlmsg_size(const struct nf_conn *ct)
476 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
477 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
478 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
479 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
480 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
481 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
482 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
483 + ctnetlink_counters_size(ct)
484 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
485 + nla_total_size(0) /* CTA_PROTOINFO */
486 + nla_total_size(0) /* CTA_HELP */
487 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
488 + ctnetlink_secctx_size(ct)
489 #ifdef CONFIG_NF_NAT_NEEDED
490 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
491 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
493 #ifdef CONFIG_NF_CONNTRACK_MARK
494 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
496 + ctnetlink_proto_size(ct)
501 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
504 struct nlmsghdr *nlh;
505 struct nfgenmsg *nfmsg;
506 struct nlattr *nest_parms;
507 struct nf_conn *ct = item->ct;
510 unsigned int flags = 0, group;
513 /* ignore our fake conntrack entry */
514 if (nf_ct_is_untracked(ct))
517 if (events & (1 << IPCT_DESTROY)) {
518 type = IPCTNL_MSG_CT_DELETE;
519 group = NFNLGRP_CONNTRACK_DESTROY;
520 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
521 type = IPCTNL_MSG_CT_NEW;
522 flags = NLM_F_CREATE|NLM_F_EXCL;
523 group = NFNLGRP_CONNTRACK_NEW;
525 type = IPCTNL_MSG_CT_NEW;
526 group = NFNLGRP_CONNTRACK_UPDATE;
532 /* container deinit, netlink may have died before death_by_timeout */
536 if (!item->report && !nfnetlink_has_listeners(net, group))
539 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
543 type |= NFNL_SUBSYS_CTNETLINK << 8;
544 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
548 nfmsg = nlmsg_data(nlh);
549 nfmsg->nfgen_family = nf_ct_l3num(ct);
550 nfmsg->version = NFNETLINK_V0;
554 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
556 goto nla_put_failure;
557 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
558 goto nla_put_failure;
559 nla_nest_end(skb, nest_parms);
561 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
563 goto nla_put_failure;
564 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
565 goto nla_put_failure;
566 nla_nest_end(skb, nest_parms);
569 NLA_PUT_BE16(skb, CTA_ZONE, htons(nf_ct_zone(ct)));
571 if (ctnetlink_dump_id(skb, ct) < 0)
572 goto nla_put_failure;
574 if (ctnetlink_dump_status(skb, ct) < 0)
575 goto nla_put_failure;
577 if (events & (1 << IPCT_DESTROY)) {
578 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
579 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
580 goto nla_put_failure;
582 if (ctnetlink_dump_timeout(skb, ct) < 0)
583 goto nla_put_failure;
585 if (events & (1 << IPCT_PROTOINFO)
586 && ctnetlink_dump_protoinfo(skb, ct) < 0)
587 goto nla_put_failure;
589 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
590 && ctnetlink_dump_helpinfo(skb, ct) < 0)
591 goto nla_put_failure;
593 #ifdef CONFIG_NF_CONNTRACK_SECMARK
594 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
595 && ctnetlink_dump_secctx(skb, ct) < 0)
596 goto nla_put_failure;
599 if (events & (1 << IPCT_RELATED) &&
600 ctnetlink_dump_master(skb, ct) < 0)
601 goto nla_put_failure;
603 if (events & (1 << IPCT_NATSEQADJ) &&
604 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
605 goto nla_put_failure;
608 #ifdef CONFIG_NF_CONNTRACK_MARK
609 if ((events & (1 << IPCT_MARK) || ct->mark)
610 && ctnetlink_dump_mark(skb, ct) < 0)
611 goto nla_put_failure;
616 err = nfnetlink_send(skb, net, item->pid, group, item->report,
618 if (err == -ENOBUFS || err == -EAGAIN)
625 nlmsg_cancel(skb, nlh);
629 if (nfnetlink_set_err(net, 0, group, -ENOBUFS) > 0)
634 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
636 static int ctnetlink_done(struct netlink_callback *cb)
639 nf_ct_put((struct nf_conn *)cb->args[1]);
644 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
646 struct net *net = sock_net(skb->sk);
647 struct nf_conn *ct, *last;
648 struct nf_conntrack_tuple_hash *h;
649 struct hlist_nulls_node *n;
650 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
651 u_int8_t l3proto = nfmsg->nfgen_family;
653 spin_lock_bh(&nf_conntrack_lock);
654 last = (struct nf_conn *)cb->args[1];
655 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
657 hlist_nulls_for_each_entry(h, n, &net->ct.hash[cb->args[0]],
659 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
661 ct = nf_ct_tuplehash_to_ctrack(h);
662 /* Dump entries of a given L3 protocol number.
663 * If it is not specified, ie. l3proto == 0,
664 * then dump everything. */
665 if (l3proto && nf_ct_l3num(ct) != l3proto)
672 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
674 IPCTNL_MSG_CT_NEW, ct) < 0) {
675 nf_conntrack_get(&ct->ct_general);
676 cb->args[1] = (unsigned long)ct;
680 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
681 IPCTNL_MSG_CT_GET_CTRZERO) {
682 struct nf_conn_counter *acct;
684 acct = nf_conn_acct_find(ct);
686 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
695 spin_unlock_bh(&nf_conntrack_lock);
703 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
705 struct nlattr *tb[CTA_IP_MAX+1];
706 struct nf_conntrack_l3proto *l3proto;
709 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
712 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
714 if (likely(l3proto->nlattr_to_tuple)) {
715 ret = nla_validate_nested(attr, CTA_IP_MAX,
716 l3proto->nla_policy);
718 ret = l3proto->nlattr_to_tuple(tb, tuple);
726 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
727 [CTA_PROTO_NUM] = { .type = NLA_U8 },
731 ctnetlink_parse_tuple_proto(struct nlattr *attr,
732 struct nf_conntrack_tuple *tuple)
734 struct nlattr *tb[CTA_PROTO_MAX+1];
735 struct nf_conntrack_l4proto *l4proto;
738 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
742 if (!tb[CTA_PROTO_NUM])
744 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
747 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
749 if (likely(l4proto->nlattr_to_tuple)) {
750 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
751 l4proto->nla_policy);
753 ret = l4proto->nlattr_to_tuple(tb, tuple);
761 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
762 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
763 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
767 ctnetlink_parse_tuple(const struct nlattr * const cda[],
768 struct nf_conntrack_tuple *tuple,
769 enum ctattr_tuple type, u_int8_t l3num)
771 struct nlattr *tb[CTA_TUPLE_MAX+1];
774 memset(tuple, 0, sizeof(*tuple));
776 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
778 if (!tb[CTA_TUPLE_IP])
781 tuple->src.l3num = l3num;
783 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
787 if (!tb[CTA_TUPLE_PROTO])
790 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
794 /* orig and expect tuples get DIR_ORIGINAL */
795 if (type == CTA_TUPLE_REPLY)
796 tuple->dst.dir = IP_CT_DIR_REPLY;
798 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
804 ctnetlink_parse_zone(const struct nlattr *attr, u16 *zone)
807 #ifdef CONFIG_NF_CONNTRACK_ZONES
808 *zone = ntohs(nla_get_be16(attr));
818 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
819 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
823 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
825 struct nlattr *tb[CTA_HELP_MAX+1];
827 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
829 if (!tb[CTA_HELP_NAME])
832 *helper_name = nla_data(tb[CTA_HELP_NAME]);
837 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
838 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
839 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
840 [CTA_STATUS] = { .type = NLA_U32 },
841 [CTA_PROTOINFO] = { .type = NLA_NESTED },
842 [CTA_HELP] = { .type = NLA_NESTED },
843 [CTA_NAT_SRC] = { .type = NLA_NESTED },
844 [CTA_TIMEOUT] = { .type = NLA_U32 },
845 [CTA_MARK] = { .type = NLA_U32 },
846 [CTA_ID] = { .type = NLA_U32 },
847 [CTA_NAT_DST] = { .type = NLA_NESTED },
848 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
849 [CTA_ZONE] = { .type = NLA_U16 },
853 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
854 const struct nlmsghdr *nlh,
855 const struct nlattr * const cda[])
857 struct net *net = sock_net(ctnl);
858 struct nf_conntrack_tuple_hash *h;
859 struct nf_conntrack_tuple tuple;
861 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
862 u_int8_t u3 = nfmsg->nfgen_family;
866 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
870 if (cda[CTA_TUPLE_ORIG])
871 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
872 else if (cda[CTA_TUPLE_REPLY])
873 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
875 /* Flush the whole table */
876 nf_conntrack_flush_report(net,
885 h = nf_conntrack_find_get(net, zone, &tuple);
889 ct = nf_ct_tuplehash_to_ctrack(h);
892 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
893 if (id != (u32)(unsigned long)ct) {
899 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
901 nlmsg_report(nlh)) < 0) {
902 nf_ct_delete_from_lists(ct);
903 /* we failed to report the event, try later */
904 nf_ct_insert_dying_list(ct);
909 /* death_by_timeout would report the event again */
910 set_bit(IPS_DYING_BIT, &ct->status);
919 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
920 const struct nlmsghdr *nlh,
921 const struct nlattr * const cda[])
923 struct net *net = sock_net(ctnl);
924 struct nf_conntrack_tuple_hash *h;
925 struct nf_conntrack_tuple tuple;
927 struct sk_buff *skb2 = NULL;
928 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
929 u_int8_t u3 = nfmsg->nfgen_family;
933 if (nlh->nlmsg_flags & NLM_F_DUMP)
934 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
937 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
941 if (cda[CTA_TUPLE_ORIG])
942 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
943 else if (cda[CTA_TUPLE_REPLY])
944 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
951 h = nf_conntrack_find_get(net, zone, &tuple);
955 ct = nf_ct_tuplehash_to_ctrack(h);
958 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
965 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
966 IPCTNL_MSG_CT_NEW, ct);
972 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
981 /* this avoids a loop in nfnetlink. */
982 return err == -EAGAIN ? -ENOBUFS : err;
985 #ifdef CONFIG_NF_NAT_NEEDED
987 ctnetlink_parse_nat_setup(struct nf_conn *ct,
988 enum nf_nat_manip_type manip,
989 const struct nlattr *attr)
991 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
993 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
994 if (!parse_nat_setup) {
995 #ifdef CONFIG_MODULES
997 spin_unlock_bh(&nf_conntrack_lock);
999 if (request_module("nf-nat-ipv4") < 0) {
1001 spin_lock_bh(&nf_conntrack_lock);
1006 spin_lock_bh(&nf_conntrack_lock);
1008 if (nfnetlink_parse_nat_setup_hook)
1014 return parse_nat_setup(ct, manip, attr);
1019 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
1022 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
1023 d = ct->status ^ status;
1025 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
1029 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
1030 /* SEEN_REPLY bit can only be set */
1033 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
1034 /* ASSURED bit can only be set */
1037 /* Be careful here, modifying NAT bits can screw up things,
1038 * so don't let users modify them directly if they don't pass
1040 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
1045 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
1047 #ifdef CONFIG_NF_NAT_NEEDED
1050 if (cda[CTA_NAT_DST]) {
1051 ret = ctnetlink_parse_nat_setup(ct,
1057 if (cda[CTA_NAT_SRC]) {
1058 ret = ctnetlink_parse_nat_setup(ct,
1071 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
1073 struct nf_conntrack_helper *helper;
1074 struct nf_conn_help *help = nfct_help(ct);
1075 char *helpname = NULL;
1078 /* don't change helper of sibling connections */
1082 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1086 if (!strcmp(helpname, "")) {
1087 if (help && help->helper) {
1088 /* we had a helper before ... */
1089 nf_ct_remove_expectations(ct);
1090 rcu_assign_pointer(help->helper, NULL);
1096 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1097 nf_ct_protonum(ct));
1098 if (helper == NULL) {
1099 #ifdef CONFIG_MODULES
1100 spin_unlock_bh(&nf_conntrack_lock);
1102 if (request_module("nfct-helper-%s", helpname) < 0) {
1103 spin_lock_bh(&nf_conntrack_lock);
1107 spin_lock_bh(&nf_conntrack_lock);
1108 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1109 nf_ct_protonum(ct));
1117 if (help->helper == helper)
1121 /* need to zero data of old helper */
1122 memset(&help->help, 0, sizeof(help->help));
1124 /* we cannot set a helper for an existing conntrack */
1128 rcu_assign_pointer(help->helper, helper);
1134 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1136 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1138 if (!del_timer(&ct->timeout))
1141 ct->timeout.expires = jiffies + timeout * HZ;
1142 add_timer(&ct->timeout);
1147 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1148 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1149 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1150 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1154 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1156 const struct nlattr *attr = cda[CTA_PROTOINFO];
1157 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1158 struct nf_conntrack_l4proto *l4proto;
1161 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1164 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1165 if (l4proto->from_nlattr)
1166 err = l4proto->from_nlattr(tb, ct);
1172 #ifdef CONFIG_NF_NAT_NEEDED
1173 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1174 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1175 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1176 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1180 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1182 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1184 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1186 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1189 natseq->correction_pos =
1190 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1192 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1195 natseq->offset_before =
1196 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1198 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1201 natseq->offset_after =
1202 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1208 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1209 const struct nlattr * const cda[])
1212 struct nf_conn_nat *nat = nfct_nat(ct);
1217 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1218 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1219 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1223 ct->status |= IPS_SEQ_ADJUST;
1226 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1227 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1228 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1232 ct->status |= IPS_SEQ_ADJUST;
1240 ctnetlink_change_conntrack(struct nf_conn *ct,
1241 const struct nlattr * const cda[])
1245 /* only allow NAT changes and master assignation for new conntracks */
1246 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1249 if (cda[CTA_HELP]) {
1250 err = ctnetlink_change_helper(ct, cda);
1255 if (cda[CTA_TIMEOUT]) {
1256 err = ctnetlink_change_timeout(ct, cda);
1261 if (cda[CTA_STATUS]) {
1262 err = ctnetlink_change_status(ct, cda);
1267 if (cda[CTA_PROTOINFO]) {
1268 err = ctnetlink_change_protoinfo(ct, cda);
1273 #if defined(CONFIG_NF_CONNTRACK_MARK)
1275 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1278 #ifdef CONFIG_NF_NAT_NEEDED
1279 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1280 err = ctnetlink_change_nat_seq_adj(ct, cda);
1289 static struct nf_conn *
1290 ctnetlink_create_conntrack(struct net *net, u16 zone,
1291 const struct nlattr * const cda[],
1292 struct nf_conntrack_tuple *otuple,
1293 struct nf_conntrack_tuple *rtuple,
1298 struct nf_conntrack_helper *helper;
1300 ct = nf_conntrack_alloc(net, zone, otuple, rtuple, GFP_ATOMIC);
1302 return ERR_PTR(-ENOMEM);
1304 if (!cda[CTA_TIMEOUT])
1306 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1308 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1311 if (cda[CTA_HELP]) {
1312 char *helpname = NULL;
1314 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1318 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1319 nf_ct_protonum(ct));
1320 if (helper == NULL) {
1322 #ifdef CONFIG_MODULES
1323 if (request_module("nfct-helper-%s", helpname) < 0) {
1329 helper = __nf_conntrack_helper_find(helpname,
1331 nf_ct_protonum(ct));
1341 struct nf_conn_help *help;
1343 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1349 /* not in hash table yet so not strictly necessary */
1350 rcu_assign_pointer(help->helper, helper);
1353 /* try an implicit helper assignation */
1354 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1359 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1360 err = ctnetlink_change_nat(ct, cda);
1365 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1366 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1367 /* we must add conntrack extensions before confirmation. */
1368 ct->status |= IPS_CONFIRMED;
1370 if (cda[CTA_STATUS]) {
1371 err = ctnetlink_change_status(ct, cda);
1376 #ifdef CONFIG_NF_NAT_NEEDED
1377 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1378 err = ctnetlink_change_nat_seq_adj(ct, cda);
1384 if (cda[CTA_PROTOINFO]) {
1385 err = ctnetlink_change_protoinfo(ct, cda);
1390 #if defined(CONFIG_NF_CONNTRACK_MARK)
1392 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1395 /* setup master conntrack: this is a confirmed expectation */
1396 if (cda[CTA_TUPLE_MASTER]) {
1397 struct nf_conntrack_tuple master;
1398 struct nf_conntrack_tuple_hash *master_h;
1399 struct nf_conn *master_ct;
1401 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1405 master_h = nf_conntrack_find_get(net, zone, &master);
1406 if (master_h == NULL) {
1410 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1411 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1412 ct->master = master_ct;
1415 add_timer(&ct->timeout);
1416 nf_conntrack_hash_insert(ct);
1424 nf_conntrack_free(ct);
1425 return ERR_PTR(err);
1429 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1430 const struct nlmsghdr *nlh,
1431 const struct nlattr * const cda[])
1433 struct net *net = sock_net(ctnl);
1434 struct nf_conntrack_tuple otuple, rtuple;
1435 struct nf_conntrack_tuple_hash *h = NULL;
1436 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1437 u_int8_t u3 = nfmsg->nfgen_family;
1441 err = ctnetlink_parse_zone(cda[CTA_ZONE], &zone);
1445 if (cda[CTA_TUPLE_ORIG]) {
1446 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1451 if (cda[CTA_TUPLE_REPLY]) {
1452 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1457 spin_lock_bh(&nf_conntrack_lock);
1458 if (cda[CTA_TUPLE_ORIG])
1459 h = __nf_conntrack_find(net, zone, &otuple);
1460 else if (cda[CTA_TUPLE_REPLY])
1461 h = __nf_conntrack_find(net, zone, &rtuple);
1465 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1467 enum ip_conntrack_events events;
1469 ct = ctnetlink_create_conntrack(net, zone, cda, &otuple,
1476 nf_conntrack_get(&ct->ct_general);
1477 spin_unlock_bh(&nf_conntrack_lock);
1478 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1479 events = IPCT_RELATED;
1483 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1484 (1 << IPCT_ASSURED) |
1485 (1 << IPCT_HELPER) |
1486 (1 << IPCT_PROTOINFO) |
1487 (1 << IPCT_NATSEQADJ) |
1488 (1 << IPCT_MARK) | events,
1489 ct, NETLINK_CB(skb).pid,
1493 spin_unlock_bh(&nf_conntrack_lock);
1497 /* implicit 'else' */
1499 /* We manipulate the conntrack inside the global conntrack table lock,
1500 * so there's no need to increase the refcount */
1502 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1503 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1505 err = ctnetlink_change_conntrack(ct, cda);
1507 nf_conntrack_get(&ct->ct_general);
1508 spin_unlock_bh(&nf_conntrack_lock);
1509 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1510 (1 << IPCT_ASSURED) |
1511 (1 << IPCT_HELPER) |
1512 (1 << IPCT_PROTOINFO) |
1513 (1 << IPCT_NATSEQADJ) |
1515 ct, NETLINK_CB(skb).pid,
1519 spin_unlock_bh(&nf_conntrack_lock);
1525 spin_unlock_bh(&nf_conntrack_lock);
1529 /***********************************************************************
1531 ***********************************************************************/
1534 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1535 const struct nf_conntrack_tuple *tuple,
1536 enum ctattr_expect type)
1538 struct nlattr *nest_parms;
1540 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1542 goto nla_put_failure;
1543 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1544 goto nla_put_failure;
1545 nla_nest_end(skb, nest_parms);
1554 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1555 const struct nf_conntrack_tuple *tuple,
1556 const struct nf_conntrack_tuple_mask *mask)
1559 struct nf_conntrack_l3proto *l3proto;
1560 struct nf_conntrack_l4proto *l4proto;
1561 struct nf_conntrack_tuple m;
1562 struct nlattr *nest_parms;
1564 memset(&m, 0xFF, sizeof(m));
1565 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1566 m.src.u.all = mask->src.u.all;
1567 m.dst.protonum = tuple->dst.protonum;
1569 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1571 goto nla_put_failure;
1573 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1574 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1576 if (unlikely(ret < 0))
1577 goto nla_put_failure;
1579 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1580 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1581 if (unlikely(ret < 0))
1582 goto nla_put_failure;
1584 nla_nest_end(skb, nest_parms);
1593 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1594 const struct nf_conntrack_expect *exp)
1596 struct nf_conn *master = exp->master;
1597 long timeout = (exp->timeout.expires - jiffies) / HZ;
1598 struct nf_conn_help *help;
1603 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1604 goto nla_put_failure;
1605 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1606 goto nla_put_failure;
1607 if (ctnetlink_exp_dump_tuple(skb,
1608 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1609 CTA_EXPECT_MASTER) < 0)
1610 goto nla_put_failure;
1612 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1613 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1614 NLA_PUT_BE32(skb, CTA_EXPECT_FLAGS, htonl(exp->flags));
1615 help = nfct_help(master);
1617 struct nf_conntrack_helper *helper;
1619 helper = rcu_dereference(help->helper);
1621 NLA_PUT_STRING(skb, CTA_EXPECT_HELP_NAME, helper->name);
1631 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1632 int event, const struct nf_conntrack_expect *exp)
1634 struct nlmsghdr *nlh;
1635 struct nfgenmsg *nfmsg;
1636 unsigned int flags = pid ? NLM_F_MULTI : 0;
1638 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1639 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1643 nfmsg = nlmsg_data(nlh);
1644 nfmsg->nfgen_family = exp->tuple.src.l3num;
1645 nfmsg->version = NFNETLINK_V0;
1648 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1649 goto nla_put_failure;
1651 nlmsg_end(skb, nlh);
1656 nlmsg_cancel(skb, nlh);
1660 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1662 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1664 struct nf_conntrack_expect *exp = item->exp;
1665 struct net *net = nf_ct_exp_net(exp);
1666 struct nlmsghdr *nlh;
1667 struct nfgenmsg *nfmsg;
1668 struct sk_buff *skb;
1669 unsigned int type, group;
1672 if (events & (1 << IPEXP_DESTROY)) {
1673 type = IPCTNL_MSG_EXP_DELETE;
1674 group = NFNLGRP_CONNTRACK_EXP_DESTROY;
1675 } else if (events & (1 << IPEXP_NEW)) {
1676 type = IPCTNL_MSG_EXP_NEW;
1677 flags = NLM_F_CREATE|NLM_F_EXCL;
1678 group = NFNLGRP_CONNTRACK_EXP_NEW;
1682 if (!item->report && !nfnetlink_has_listeners(net, group))
1685 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1689 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1690 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1694 nfmsg = nlmsg_data(nlh);
1695 nfmsg->nfgen_family = exp->tuple.src.l3num;
1696 nfmsg->version = NFNETLINK_V0;
1700 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1701 goto nla_put_failure;
1704 nlmsg_end(skb, nlh);
1705 nfnetlink_send(skb, net, item->pid, group, item->report, GFP_ATOMIC);
1710 nlmsg_cancel(skb, nlh);
1714 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1718 static int ctnetlink_exp_done(struct netlink_callback *cb)
1721 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1726 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1728 struct net *net = sock_net(skb->sk);
1729 struct nf_conntrack_expect *exp, *last;
1730 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1731 struct hlist_node *n;
1732 u_int8_t l3proto = nfmsg->nfgen_family;
1735 last = (struct nf_conntrack_expect *)cb->args[1];
1736 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1738 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1740 if (l3proto && exp->tuple.src.l3num != l3proto)
1747 if (ctnetlink_exp_fill_info(skb,
1748 NETLINK_CB(cb->skb).pid,
1752 if (!atomic_inc_not_zero(&exp->use))
1754 cb->args[1] = (unsigned long)exp;
1766 nf_ct_expect_put(last);
1771 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1772 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1773 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1774 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
1775 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1776 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1777 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
1778 [CTA_EXPECT_ZONE] = { .type = NLA_U16 },
1779 [CTA_EXPECT_FLAGS] = { .type = NLA_U32 },
1783 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1784 const struct nlmsghdr *nlh,
1785 const struct nlattr * const cda[])
1787 struct net *net = sock_net(ctnl);
1788 struct nf_conntrack_tuple tuple;
1789 struct nf_conntrack_expect *exp;
1790 struct sk_buff *skb2;
1791 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1792 u_int8_t u3 = nfmsg->nfgen_family;
1796 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1797 return netlink_dump_start(ctnl, skb, nlh,
1798 ctnetlink_exp_dump_table,
1799 ctnetlink_exp_done);
1802 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1806 if (cda[CTA_EXPECT_MASTER])
1807 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1814 exp = nf_ct_expect_find_get(net, zone, &tuple);
1818 if (cda[CTA_EXPECT_ID]) {
1819 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1820 if (ntohl(id) != (u32)(unsigned long)exp) {
1821 nf_ct_expect_put(exp);
1827 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1832 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1833 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1838 nf_ct_expect_put(exp);
1840 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1845 nf_ct_expect_put(exp);
1850 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1851 const struct nlmsghdr *nlh,
1852 const struct nlattr * const cda[])
1854 struct net *net = sock_net(ctnl);
1855 struct nf_conntrack_expect *exp;
1856 struct nf_conntrack_tuple tuple;
1857 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1858 struct hlist_node *n, *next;
1859 u_int8_t u3 = nfmsg->nfgen_family;
1864 if (cda[CTA_EXPECT_TUPLE]) {
1865 /* delete a single expect by tuple */
1866 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
1870 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1874 /* bump usage count to 2 */
1875 exp = nf_ct_expect_find_get(net, zone, &tuple);
1879 if (cda[CTA_EXPECT_ID]) {
1880 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1881 if (ntohl(id) != (u32)(unsigned long)exp) {
1882 nf_ct_expect_put(exp);
1887 /* after list removal, usage count == 1 */
1888 spin_lock_bh(&nf_conntrack_lock);
1889 if (del_timer(&exp->timeout)) {
1890 nf_ct_unlink_expect_report(exp, NETLINK_CB(skb).pid,
1892 nf_ct_expect_put(exp);
1894 spin_unlock_bh(&nf_conntrack_lock);
1895 /* have to put what we 'get' above.
1896 * after this line usage count == 0 */
1897 nf_ct_expect_put(exp);
1898 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1899 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1900 struct nf_conn_help *m_help;
1902 /* delete all expectations for this helper */
1903 spin_lock_bh(&nf_conntrack_lock);
1904 for (i = 0; i < nf_ct_expect_hsize; i++) {
1905 hlist_for_each_entry_safe(exp, n, next,
1906 &net->ct.expect_hash[i],
1908 m_help = nfct_help(exp->master);
1909 if (!strcmp(m_help->helper->name, name) &&
1910 del_timer(&exp->timeout)) {
1911 nf_ct_unlink_expect_report(exp,
1912 NETLINK_CB(skb).pid,
1914 nf_ct_expect_put(exp);
1918 spin_unlock_bh(&nf_conntrack_lock);
1920 /* This basically means we have to flush everything*/
1921 spin_lock_bh(&nf_conntrack_lock);
1922 for (i = 0; i < nf_ct_expect_hsize; i++) {
1923 hlist_for_each_entry_safe(exp, n, next,
1924 &net->ct.expect_hash[i],
1926 if (del_timer(&exp->timeout)) {
1927 nf_ct_unlink_expect_report(exp,
1928 NETLINK_CB(skb).pid,
1930 nf_ct_expect_put(exp);
1934 spin_unlock_bh(&nf_conntrack_lock);
1940 ctnetlink_change_expect(struct nf_conntrack_expect *x,
1941 const struct nlattr * const cda[])
1947 ctnetlink_create_expect(struct net *net, u16 zone,
1948 const struct nlattr * const cda[],
1950 u32 pid, int report)
1952 struct nf_conntrack_tuple tuple, mask, master_tuple;
1953 struct nf_conntrack_tuple_hash *h = NULL;
1954 struct nf_conntrack_expect *exp;
1956 struct nf_conn_help *help;
1959 /* caller guarantees that those three CTA_EXPECT_* exist */
1960 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1963 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1966 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1970 /* Look for master conntrack of this expectation */
1971 h = nf_conntrack_find_get(net, zone, &master_tuple);
1974 ct = nf_ct_tuplehash_to_ctrack(h);
1975 exp = nf_ct_expect_alloc(ct);
1980 help = nfct_help(ct);
1982 if (!cda[CTA_EXPECT_TIMEOUT]) {
1986 exp->timeout.expires =
1987 jiffies + ntohl(nla_get_be32(cda[CTA_EXPECT_TIMEOUT])) * HZ;
1989 exp->flags = NF_CT_EXPECT_USERSPACE;
1990 if (cda[CTA_EXPECT_FLAGS]) {
1992 ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
1995 if (cda[CTA_EXPECT_FLAGS]) {
1996 exp->flags = ntohl(nla_get_be32(cda[CTA_EXPECT_FLAGS]));
1997 exp->flags &= ~NF_CT_EXPECT_USERSPACE;
2003 exp->expectfn = NULL;
2006 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
2007 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
2008 exp->mask.src.u.all = mask.src.u.all;
2010 err = nf_ct_expect_related_report(exp, pid, report);
2011 nf_ct_expect_put(exp);
2014 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
2019 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
2020 const struct nlmsghdr *nlh,
2021 const struct nlattr * const cda[])
2023 struct net *net = sock_net(ctnl);
2024 struct nf_conntrack_tuple tuple;
2025 struct nf_conntrack_expect *exp;
2026 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
2027 u_int8_t u3 = nfmsg->nfgen_family;
2031 if (!cda[CTA_EXPECT_TUPLE]
2032 || !cda[CTA_EXPECT_MASK]
2033 || !cda[CTA_EXPECT_MASTER])
2036 err = ctnetlink_parse_zone(cda[CTA_EXPECT_ZONE], &zone);
2040 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
2044 spin_lock_bh(&nf_conntrack_lock);
2045 exp = __nf_ct_expect_find(net, zone, &tuple);
2048 spin_unlock_bh(&nf_conntrack_lock);
2050 if (nlh->nlmsg_flags & NLM_F_CREATE) {
2051 err = ctnetlink_create_expect(net, zone, cda,
2053 NETLINK_CB(skb).pid,
2060 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
2061 err = ctnetlink_change_expect(exp, cda);
2062 spin_unlock_bh(&nf_conntrack_lock);
2067 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2068 static struct nf_ct_event_notifier ctnl_notifier = {
2069 .fcn = ctnetlink_conntrack_event,
2072 static struct nf_exp_event_notifier ctnl_notifier_exp = {
2073 .fcn = ctnetlink_expect_event,
2077 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
2078 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
2079 .attr_count = CTA_MAX,
2080 .policy = ct_nla_policy },
2081 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
2082 .attr_count = CTA_MAX,
2083 .policy = ct_nla_policy },
2084 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
2085 .attr_count = CTA_MAX,
2086 .policy = ct_nla_policy },
2087 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
2088 .attr_count = CTA_MAX,
2089 .policy = ct_nla_policy },
2092 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
2093 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
2094 .attr_count = CTA_EXPECT_MAX,
2095 .policy = exp_nla_policy },
2096 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
2097 .attr_count = CTA_EXPECT_MAX,
2098 .policy = exp_nla_policy },
2099 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
2100 .attr_count = CTA_EXPECT_MAX,
2101 .policy = exp_nla_policy },
2104 static const struct nfnetlink_subsystem ctnl_subsys = {
2105 .name = "conntrack",
2106 .subsys_id = NFNL_SUBSYS_CTNETLINK,
2107 .cb_count = IPCTNL_MSG_MAX,
2111 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
2112 .name = "conntrack_expect",
2113 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
2114 .cb_count = IPCTNL_MSG_EXP_MAX,
2118 MODULE_ALIAS("ip_conntrack_netlink");
2119 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
2120 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
2122 static int __init ctnetlink_init(void)
2126 pr_info("ctnetlink v%s: registering with nfnetlink.\n", version);
2127 ret = nfnetlink_subsys_register(&ctnl_subsys);
2129 pr_err("ctnetlink_init: cannot register with nfnetlink.\n");
2133 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2135 pr_err("ctnetlink_init: cannot register exp with nfnetlink.\n");
2136 goto err_unreg_subsys;
2139 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2140 ret = nf_conntrack_register_notifier(&ctnl_notifier);
2142 pr_err("ctnetlink_init: cannot register notifier.\n");
2143 goto err_unreg_exp_subsys;
2146 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
2148 pr_err("ctnetlink_init: cannot expect register notifier.\n");
2149 goto err_unreg_notifier;
2155 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2157 nf_conntrack_unregister_notifier(&ctnl_notifier);
2158 err_unreg_exp_subsys:
2159 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2162 nfnetlink_subsys_unregister(&ctnl_subsys);
2167 static void __exit ctnetlink_exit(void)
2169 pr_info("ctnetlink: unregistering from nfnetlink.\n");
2171 nf_ct_remove_userspace_expectations();
2172 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2173 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
2174 nf_conntrack_unregister_notifier(&ctnl_notifier);
2177 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2178 nfnetlink_subsys_unregister(&ctnl_subsys);
2181 module_init(ctnetlink_init);
2182 module_exit(ctnetlink_exit);