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/skbuff.h>
26 #include <linux/errno.h>
27 #include <linux/netlink.h>
28 #include <linux/spinlock.h>
29 #include <linux/interrupt.h>
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
34 #include <net/netfilter/nf_conntrack.h>
35 #include <net/netfilter/nf_conntrack_core.h>
36 #include <net/netfilter/nf_conntrack_expect.h>
37 #include <net/netfilter/nf_conntrack_helper.h>
38 #include <net/netfilter/nf_conntrack_l3proto.h>
39 #include <net/netfilter/nf_conntrack_l4proto.h>
40 #include <net/netfilter/nf_conntrack_tuple.h>
41 #include <net/netfilter/nf_conntrack_acct.h>
42 #ifdef CONFIG_NF_NAT_NEEDED
43 #include <net/netfilter/nf_nat_core.h>
44 #include <net/netfilter/nf_nat_protocol.h>
47 #include <linux/netfilter/nfnetlink.h>
48 #include <linux/netfilter/nfnetlink_conntrack.h>
50 MODULE_LICENSE("GPL");
52 static char __initdata version[] = "0.93";
55 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
56 const struct nf_conntrack_tuple *tuple,
57 struct nf_conntrack_l4proto *l4proto)
60 struct nlattr *nest_parms;
62 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
65 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
67 if (likely(l4proto->tuple_to_nlattr))
68 ret = l4proto->tuple_to_nlattr(skb, tuple);
70 nla_nest_end(skb, nest_parms);
79 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
80 const struct nf_conntrack_tuple *tuple,
81 struct nf_conntrack_l3proto *l3proto)
84 struct nlattr *nest_parms;
86 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
90 if (likely(l3proto->tuple_to_nlattr))
91 ret = l3proto->tuple_to_nlattr(skb, tuple);
93 nla_nest_end(skb, nest_parms);
102 ctnetlink_dump_tuples(struct sk_buff *skb,
103 const struct nf_conntrack_tuple *tuple)
106 struct nf_conntrack_l3proto *l3proto;
107 struct nf_conntrack_l4proto *l4proto;
109 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
110 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
112 if (unlikely(ret < 0))
115 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
116 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
122 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
124 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
132 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
134 long timeout = (ct->timeout.expires - jiffies) / HZ;
139 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
147 ctnetlink_dump_protoinfo(struct sk_buff *skb, struct nf_conn *ct)
149 struct nf_conntrack_l4proto *l4proto;
150 struct nlattr *nest_proto;
153 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
154 if (!l4proto->to_nlattr)
157 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
159 goto nla_put_failure;
161 ret = l4proto->to_nlattr(skb, nest_proto, ct);
163 nla_nest_end(skb, nest_proto);
172 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
174 struct nlattr *nest_helper;
175 const struct nf_conn_help *help = nfct_help(ct);
176 struct nf_conntrack_helper *helper;
181 helper = rcu_dereference(help->helper);
185 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
187 goto nla_put_failure;
188 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
190 if (helper->to_nlattr)
191 helper->to_nlattr(skb, ct);
193 nla_nest_end(skb, nest_helper);
202 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
203 enum ip_conntrack_dir dir)
205 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
206 struct nlattr *nest_count;
207 const struct nf_conn_counter *acct;
209 acct = nf_conn_acct_find(ct);
213 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
215 goto nla_put_failure;
217 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
218 cpu_to_be64(acct[dir].packets));
219 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
220 cpu_to_be64(acct[dir].bytes));
222 nla_nest_end(skb, nest_count);
230 #ifdef CONFIG_NF_CONNTRACK_MARK
232 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
234 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
241 #define ctnetlink_dump_mark(a, b) (0)
244 #ifdef CONFIG_NF_CONNTRACK_SECMARK
246 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
248 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
255 #define ctnetlink_dump_secmark(a, b) (0)
258 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
261 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
263 struct nlattr *nest_parms;
265 if (!(ct->status & IPS_EXPECTED))
268 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
270 goto nla_put_failure;
271 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
272 goto nla_put_failure;
273 nla_nest_end(skb, nest_parms);
281 #ifdef CONFIG_NF_NAT_NEEDED
283 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
285 struct nlattr *nest_parms;
287 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
289 goto nla_put_failure;
291 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
292 htonl(natseq->correction_pos));
293 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
294 htonl(natseq->offset_before));
295 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
296 htonl(natseq->offset_after));
298 nla_nest_end(skb, nest_parms);
307 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
309 struct nf_nat_seq *natseq;
310 struct nf_conn_nat *nat = nfct_nat(ct);
312 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
315 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
316 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
319 natseq = &nat->seq[IP_CT_DIR_REPLY];
320 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
326 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
330 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
332 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
340 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
342 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
350 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
351 int event, struct nf_conn *ct)
353 struct nlmsghdr *nlh;
354 struct nfgenmsg *nfmsg;
355 struct nlattr *nest_parms;
356 unsigned int flags = pid ? NLM_F_MULTI : 0;
358 event |= NFNL_SUBSYS_CTNETLINK << 8;
359 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
363 nfmsg = nlmsg_data(nlh);
364 nfmsg->nfgen_family = nf_ct_l3num(ct);
365 nfmsg->version = NFNETLINK_V0;
368 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
370 goto nla_put_failure;
371 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
372 goto nla_put_failure;
373 nla_nest_end(skb, nest_parms);
375 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
377 goto nla_put_failure;
378 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
379 goto nla_put_failure;
380 nla_nest_end(skb, nest_parms);
382 if (ctnetlink_dump_status(skb, ct) < 0 ||
383 ctnetlink_dump_timeout(skb, ct) < 0 ||
384 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
385 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
386 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
387 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
388 ctnetlink_dump_mark(skb, ct) < 0 ||
389 ctnetlink_dump_secmark(skb, ct) < 0 ||
390 ctnetlink_dump_id(skb, ct) < 0 ||
391 ctnetlink_dump_use(skb, ct) < 0 ||
392 ctnetlink_dump_master(skb, ct) < 0 ||
393 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
394 goto nla_put_failure;
401 nlmsg_cancel(skb, nlh);
405 #ifdef CONFIG_NF_CONNTRACK_EVENTS
407 ctnetlink_proto_size(const struct nf_conn *ct)
409 struct nf_conntrack_l3proto *l3proto;
410 struct nf_conntrack_l4proto *l4proto;
414 l3proto = __nf_ct_l3proto_find(nf_ct_l3num(ct));
415 len += l3proto->nla_size;
417 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
418 len += l4proto->nla_size;
425 ctnetlink_nlmsg_size(const struct nf_conn *ct)
427 return NLMSG_ALIGN(sizeof(struct nfgenmsg))
428 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
429 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
430 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
431 + 3 * nla_total_size(sizeof(u_int8_t)) /* CTA_PROTO_NUM */
432 + nla_total_size(sizeof(u_int32_t)) /* CTA_ID */
433 + nla_total_size(sizeof(u_int32_t)) /* CTA_STATUS */
434 #ifdef CONFIG_NF_CT_ACCT
435 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
436 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_PACKETS */
437 + 2 * nla_total_size(sizeof(uint64_t)) /* CTA_COUNTERS_BYTES */
439 + nla_total_size(sizeof(u_int32_t)) /* CTA_TIMEOUT */
440 + nla_total_size(0) /* CTA_PROTOINFO */
441 + nla_total_size(0) /* CTA_HELP */
442 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
443 #ifdef CONFIG_NF_CONNTRACK_SECMARK
444 + nla_total_size(sizeof(u_int32_t)) /* CTA_SECMARK */
446 #ifdef CONFIG_NF_NAT_NEEDED
447 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
448 + 6 * nla_total_size(sizeof(u_int32_t)) /* CTA_NAT_SEQ_OFFSET */
450 #ifdef CONFIG_NF_CONNTRACK_MARK
451 + nla_total_size(sizeof(u_int32_t)) /* CTA_MARK */
453 + ctnetlink_proto_size(ct)
458 ctnetlink_conntrack_event(unsigned int events, struct nf_ct_event *item)
461 struct nlmsghdr *nlh;
462 struct nfgenmsg *nfmsg;
463 struct nlattr *nest_parms;
464 struct nf_conn *ct = item->ct;
467 unsigned int flags = 0, group;
470 /* ignore our fake conntrack entry */
471 if (ct == &nf_conntrack_untracked)
474 if (events & (1 << IPCT_DESTROY)) {
475 type = IPCTNL_MSG_CT_DELETE;
476 group = NFNLGRP_CONNTRACK_DESTROY;
477 } else if (events & ((1 << IPCT_NEW) | (1 << IPCT_RELATED))) {
478 type = IPCTNL_MSG_CT_NEW;
479 flags = NLM_F_CREATE|NLM_F_EXCL;
480 group = NFNLGRP_CONNTRACK_NEW;
482 type = IPCTNL_MSG_CT_NEW;
483 group = NFNLGRP_CONNTRACK_UPDATE;
488 if (!item->report && !nfnetlink_has_listeners(net, group))
491 skb = nlmsg_new(ctnetlink_nlmsg_size(ct), GFP_ATOMIC);
495 type |= NFNL_SUBSYS_CTNETLINK << 8;
496 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
500 nfmsg = nlmsg_data(nlh);
501 nfmsg->nfgen_family = nf_ct_l3num(ct);
502 nfmsg->version = NFNETLINK_V0;
506 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
508 goto nla_put_failure;
509 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
510 goto nla_put_failure;
511 nla_nest_end(skb, nest_parms);
513 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
515 goto nla_put_failure;
516 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
517 goto nla_put_failure;
518 nla_nest_end(skb, nest_parms);
520 if (ctnetlink_dump_id(skb, ct) < 0)
521 goto nla_put_failure;
523 if (ctnetlink_dump_status(skb, ct) < 0)
524 goto nla_put_failure;
526 if (events & (1 << IPCT_DESTROY)) {
527 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
528 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
529 goto nla_put_failure;
531 if (ctnetlink_dump_timeout(skb, ct) < 0)
532 goto nla_put_failure;
534 if (events & (1 << IPCT_PROTOINFO)
535 && ctnetlink_dump_protoinfo(skb, ct) < 0)
536 goto nla_put_failure;
538 if ((events & (1 << IPCT_HELPER) || nfct_help(ct))
539 && ctnetlink_dump_helpinfo(skb, ct) < 0)
540 goto nla_put_failure;
542 #ifdef CONFIG_NF_CONNTRACK_SECMARK
543 if ((events & (1 << IPCT_SECMARK) || ct->secmark)
544 && ctnetlink_dump_secmark(skb, ct) < 0)
545 goto nla_put_failure;
548 if (events & (1 << IPCT_RELATED) &&
549 ctnetlink_dump_master(skb, ct) < 0)
550 goto nla_put_failure;
552 if (events & (1 << IPCT_NATSEQADJ) &&
553 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
554 goto nla_put_failure;
557 #ifdef CONFIG_NF_CONNTRACK_MARK
558 if ((events & (1 << IPCT_MARK) || ct->mark)
559 && ctnetlink_dump_mark(skb, ct) < 0)
560 goto nla_put_failure;
565 err = nfnetlink_send(skb, net, item->pid, group, item->report,
567 if (err == -ENOBUFS || err == -EAGAIN)
574 nlmsg_cancel(skb, nlh);
578 nfnetlink_set_err(net, 0, group, -ENOBUFS);
581 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
583 static int ctnetlink_done(struct netlink_callback *cb)
586 nf_ct_put((struct nf_conn *)cb->args[1]);
591 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
593 struct net *net = sock_net(skb->sk);
594 struct nf_conn *ct, *last;
595 struct nf_conntrack_tuple_hash *h;
596 struct hlist_nulls_node *n;
597 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
598 u_int8_t l3proto = nfmsg->nfgen_family;
601 last = (struct nf_conn *)cb->args[1];
602 for (; cb->args[0] < net->ct.htable_size; cb->args[0]++) {
604 hlist_nulls_for_each_entry_rcu(h, n, &net->ct.hash[cb->args[0]],
606 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
608 ct = nf_ct_tuplehash_to_ctrack(h);
609 if (!atomic_inc_not_zero(&ct->ct_general.use))
611 /* Dump entries of a given L3 protocol number.
612 * If it is not specified, ie. l3proto == 0,
613 * then dump everything. */
614 if (l3proto && nf_ct_l3num(ct) != l3proto)
621 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
623 IPCTNL_MSG_CT_NEW, ct) < 0) {
624 cb->args[1] = (unsigned long)ct;
628 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
629 IPCTNL_MSG_CT_GET_CTRZERO) {
630 struct nf_conn_counter *acct;
632 acct = nf_conn_acct_find(ct);
634 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
653 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
655 struct nlattr *tb[CTA_IP_MAX+1];
656 struct nf_conntrack_l3proto *l3proto;
659 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
662 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
664 if (likely(l3proto->nlattr_to_tuple)) {
665 ret = nla_validate_nested(attr, CTA_IP_MAX,
666 l3proto->nla_policy);
668 ret = l3proto->nlattr_to_tuple(tb, tuple);
676 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
677 [CTA_PROTO_NUM] = { .type = NLA_U8 },
681 ctnetlink_parse_tuple_proto(struct nlattr *attr,
682 struct nf_conntrack_tuple *tuple)
684 struct nlattr *tb[CTA_PROTO_MAX+1];
685 struct nf_conntrack_l4proto *l4proto;
688 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
692 if (!tb[CTA_PROTO_NUM])
694 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
697 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
699 if (likely(l4proto->nlattr_to_tuple)) {
700 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
701 l4proto->nla_policy);
703 ret = l4proto->nlattr_to_tuple(tb, tuple);
711 static const struct nla_policy tuple_nla_policy[CTA_TUPLE_MAX+1] = {
712 [CTA_TUPLE_IP] = { .type = NLA_NESTED },
713 [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
717 ctnetlink_parse_tuple(const struct nlattr * const cda[],
718 struct nf_conntrack_tuple *tuple,
719 enum ctattr_tuple type, u_int8_t l3num)
721 struct nlattr *tb[CTA_TUPLE_MAX+1];
724 memset(tuple, 0, sizeof(*tuple));
726 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], tuple_nla_policy);
728 if (!tb[CTA_TUPLE_IP])
731 tuple->src.l3num = l3num;
733 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
737 if (!tb[CTA_TUPLE_PROTO])
740 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
744 /* orig and expect tuples get DIR_ORIGINAL */
745 if (type == CTA_TUPLE_REPLY)
746 tuple->dst.dir = IP_CT_DIR_REPLY;
748 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
753 static const struct nla_policy help_nla_policy[CTA_HELP_MAX+1] = {
754 [CTA_HELP_NAME] = { .type = NLA_NUL_STRING },
758 ctnetlink_parse_help(const struct nlattr *attr, char **helper_name)
760 struct nlattr *tb[CTA_HELP_MAX+1];
762 nla_parse_nested(tb, CTA_HELP_MAX, attr, help_nla_policy);
764 if (!tb[CTA_HELP_NAME])
767 *helper_name = nla_data(tb[CTA_HELP_NAME]);
772 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
773 [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
774 [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
775 [CTA_STATUS] = { .type = NLA_U32 },
776 [CTA_PROTOINFO] = { .type = NLA_NESTED },
777 [CTA_HELP] = { .type = NLA_NESTED },
778 [CTA_NAT_SRC] = { .type = NLA_NESTED },
779 [CTA_TIMEOUT] = { .type = NLA_U32 },
780 [CTA_MARK] = { .type = NLA_U32 },
781 [CTA_ID] = { .type = NLA_U32 },
782 [CTA_NAT_DST] = { .type = NLA_NESTED },
783 [CTA_TUPLE_MASTER] = { .type = NLA_NESTED },
787 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
788 const struct nlmsghdr *nlh,
789 const struct nlattr * const cda[])
791 struct net *net = sock_net(ctnl);
792 struct nf_conntrack_tuple_hash *h;
793 struct nf_conntrack_tuple tuple;
795 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
796 u_int8_t u3 = nfmsg->nfgen_family;
799 if (cda[CTA_TUPLE_ORIG])
800 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
801 else if (cda[CTA_TUPLE_REPLY])
802 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
804 /* Flush the whole table */
805 nf_conntrack_flush_report(net,
814 h = nf_conntrack_find_get(net, &tuple);
818 ct = nf_ct_tuplehash_to_ctrack(h);
821 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
822 if (id != (u32)(unsigned long)ct) {
828 if (nf_conntrack_event_report(IPCT_DESTROY, ct,
830 nlmsg_report(nlh)) < 0) {
831 nf_ct_delete_from_lists(ct);
832 /* we failed to report the event, try later */
833 nf_ct_insert_dying_list(ct);
838 /* death_by_timeout would report the event again */
839 set_bit(IPS_DYING_BIT, &ct->status);
848 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
849 const struct nlmsghdr *nlh,
850 const struct nlattr * const cda[])
852 struct net *net = sock_net(ctnl);
853 struct nf_conntrack_tuple_hash *h;
854 struct nf_conntrack_tuple tuple;
856 struct sk_buff *skb2 = NULL;
857 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
858 u_int8_t u3 = nfmsg->nfgen_family;
861 if (nlh->nlmsg_flags & NLM_F_DUMP)
862 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
865 if (cda[CTA_TUPLE_ORIG])
866 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
867 else if (cda[CTA_TUPLE_REPLY])
868 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
875 h = nf_conntrack_find_get(net, &tuple);
879 ct = nf_ct_tuplehash_to_ctrack(h);
882 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
889 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
890 IPCTNL_MSG_CT_NEW, ct);
896 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
908 #ifdef CONFIG_NF_NAT_NEEDED
910 ctnetlink_parse_nat_setup(struct nf_conn *ct,
911 enum nf_nat_manip_type manip,
912 const struct nlattr *attr)
914 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
916 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
917 if (!parse_nat_setup) {
918 #ifdef CONFIG_MODULES
920 spin_unlock_bh(&nf_conntrack_lock);
922 if (request_module("nf-nat-ipv4") < 0) {
924 spin_lock_bh(&nf_conntrack_lock);
929 spin_lock_bh(&nf_conntrack_lock);
931 if (nfnetlink_parse_nat_setup_hook)
937 return parse_nat_setup(ct, manip, attr);
942 ctnetlink_change_status(struct nf_conn *ct, const struct nlattr * const cda[])
945 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
946 d = ct->status ^ status;
948 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
952 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
953 /* SEEN_REPLY bit can only be set */
956 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
957 /* ASSURED bit can only be set */
960 /* Be careful here, modifying NAT bits can screw up things,
961 * so don't let users modify them directly if they don't pass
963 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
968 ctnetlink_change_nat(struct nf_conn *ct, const struct nlattr * const cda[])
970 #ifdef CONFIG_NF_NAT_NEEDED
973 if (cda[CTA_NAT_DST]) {
974 ret = ctnetlink_parse_nat_setup(ct,
980 if (cda[CTA_NAT_SRC]) {
981 ret = ctnetlink_parse_nat_setup(ct,
994 ctnetlink_change_helper(struct nf_conn *ct, const struct nlattr * const cda[])
996 struct nf_conntrack_helper *helper;
997 struct nf_conn_help *help = nfct_help(ct);
998 char *helpname = NULL;
1001 /* don't change helper of sibling connections */
1005 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1009 if (!strcmp(helpname, "")) {
1010 if (help && help->helper) {
1011 /* we had a helper before ... */
1012 nf_ct_remove_expectations(ct);
1013 rcu_assign_pointer(help->helper, NULL);
1019 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1020 nf_ct_protonum(ct));
1021 if (helper == NULL) {
1022 #ifdef CONFIG_MODULES
1023 spin_unlock_bh(&nf_conntrack_lock);
1025 if (request_module("nfct-helper-%s", helpname) < 0) {
1026 spin_lock_bh(&nf_conntrack_lock);
1030 spin_lock_bh(&nf_conntrack_lock);
1031 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1032 nf_ct_protonum(ct));
1040 if (help->helper == helper)
1044 /* need to zero data of old helper */
1045 memset(&help->help, 0, sizeof(help->help));
1047 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1052 rcu_assign_pointer(help->helper, helper);
1058 ctnetlink_change_timeout(struct nf_conn *ct, const struct nlattr * const cda[])
1060 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1062 if (!del_timer(&ct->timeout))
1065 ct->timeout.expires = jiffies + timeout * HZ;
1066 add_timer(&ct->timeout);
1071 static const struct nla_policy protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
1072 [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
1073 [CTA_PROTOINFO_DCCP] = { .type = NLA_NESTED },
1074 [CTA_PROTOINFO_SCTP] = { .type = NLA_NESTED },
1078 ctnetlink_change_protoinfo(struct nf_conn *ct, const struct nlattr * const cda[])
1080 const struct nlattr *attr = cda[CTA_PROTOINFO];
1081 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
1082 struct nf_conntrack_l4proto *l4proto;
1085 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, protoinfo_policy);
1088 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1089 if (l4proto->from_nlattr)
1090 err = l4proto->from_nlattr(tb, ct);
1096 #ifdef CONFIG_NF_NAT_NEEDED
1097 static const struct nla_policy nat_seq_policy[CTA_NAT_SEQ_MAX+1] = {
1098 [CTA_NAT_SEQ_CORRECTION_POS] = { .type = NLA_U32 },
1099 [CTA_NAT_SEQ_OFFSET_BEFORE] = { .type = NLA_U32 },
1100 [CTA_NAT_SEQ_OFFSET_AFTER] = { .type = NLA_U32 },
1104 change_nat_seq_adj(struct nf_nat_seq *natseq, const struct nlattr * const attr)
1106 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1108 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, nat_seq_policy);
1110 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1113 natseq->correction_pos =
1114 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1116 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1119 natseq->offset_before =
1120 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1122 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1125 natseq->offset_after =
1126 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1132 ctnetlink_change_nat_seq_adj(struct nf_conn *ct,
1133 const struct nlattr * const cda[])
1136 struct nf_conn_nat *nat = nfct_nat(ct);
1141 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1142 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1143 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1147 ct->status |= IPS_SEQ_ADJUST;
1150 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1151 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1152 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1156 ct->status |= IPS_SEQ_ADJUST;
1164 ctnetlink_change_conntrack(struct nf_conn *ct,
1165 const struct nlattr * const cda[])
1169 /* only allow NAT changes and master assignation for new conntracks */
1170 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1173 if (cda[CTA_HELP]) {
1174 err = ctnetlink_change_helper(ct, cda);
1179 if (cda[CTA_TIMEOUT]) {
1180 err = ctnetlink_change_timeout(ct, cda);
1185 if (cda[CTA_STATUS]) {
1186 err = ctnetlink_change_status(ct, cda);
1191 if (cda[CTA_PROTOINFO]) {
1192 err = ctnetlink_change_protoinfo(ct, cda);
1197 #if defined(CONFIG_NF_CONNTRACK_MARK)
1199 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1202 #ifdef CONFIG_NF_NAT_NEEDED
1203 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1204 err = ctnetlink_change_nat_seq_adj(ct, cda);
1213 static struct nf_conn *
1214 ctnetlink_create_conntrack(struct net *net,
1215 const struct nlattr * const cda[],
1216 struct nf_conntrack_tuple *otuple,
1217 struct nf_conntrack_tuple *rtuple,
1222 struct nf_conntrack_helper *helper;
1224 ct = nf_conntrack_alloc(net, otuple, rtuple, GFP_ATOMIC);
1226 return ERR_PTR(-ENOMEM);
1228 if (!cda[CTA_TIMEOUT])
1230 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1232 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1233 ct->status |= IPS_CONFIRMED;
1236 if (cda[CTA_HELP]) {
1237 char *helpname = NULL;
1239 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1243 helper = __nf_conntrack_helper_find(helpname, nf_ct_l3num(ct),
1244 nf_ct_protonum(ct));
1245 if (helper == NULL) {
1247 #ifdef CONFIG_MODULES
1248 if (request_module("nfct-helper-%s", helpname) < 0) {
1254 helper = __nf_conntrack_helper_find(helpname,
1256 nf_ct_protonum(ct));
1266 struct nf_conn_help *help;
1268 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1274 /* not in hash table yet so not strictly necessary */
1275 rcu_assign_pointer(help->helper, helper);
1278 /* try an implicit helper assignation */
1279 err = __nf_ct_try_assign_helper(ct, NULL, GFP_ATOMIC);
1284 if (cda[CTA_STATUS]) {
1285 err = ctnetlink_change_status(ct, cda);
1290 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1291 err = ctnetlink_change_nat(ct, cda);
1296 #ifdef CONFIG_NF_NAT_NEEDED
1297 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1298 err = ctnetlink_change_nat_seq_adj(ct, cda);
1304 if (cda[CTA_PROTOINFO]) {
1305 err = ctnetlink_change_protoinfo(ct, cda);
1310 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1311 nf_ct_ecache_ext_add(ct, 0, 0, GFP_ATOMIC);
1313 #if defined(CONFIG_NF_CONNTRACK_MARK)
1315 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1318 /* setup master conntrack: this is a confirmed expectation */
1319 if (cda[CTA_TUPLE_MASTER]) {
1320 struct nf_conntrack_tuple master;
1321 struct nf_conntrack_tuple_hash *master_h;
1322 struct nf_conn *master_ct;
1324 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1328 master_h = nf_conntrack_find_get(net, &master);
1329 if (master_h == NULL) {
1333 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1334 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1335 ct->master = master_ct;
1338 add_timer(&ct->timeout);
1339 nf_conntrack_hash_insert(ct);
1347 nf_conntrack_free(ct);
1348 return ERR_PTR(err);
1352 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1353 const struct nlmsghdr *nlh,
1354 const struct nlattr * const cda[])
1356 struct net *net = sock_net(ctnl);
1357 struct nf_conntrack_tuple otuple, rtuple;
1358 struct nf_conntrack_tuple_hash *h = NULL;
1359 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1360 u_int8_t u3 = nfmsg->nfgen_family;
1363 if (cda[CTA_TUPLE_ORIG]) {
1364 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1369 if (cda[CTA_TUPLE_REPLY]) {
1370 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1375 spin_lock_bh(&nf_conntrack_lock);
1376 if (cda[CTA_TUPLE_ORIG])
1377 h = __nf_conntrack_find(net, &otuple);
1378 else if (cda[CTA_TUPLE_REPLY])
1379 h = __nf_conntrack_find(net, &rtuple);
1383 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1385 enum ip_conntrack_events events;
1387 ct = ctnetlink_create_conntrack(net, cda, &otuple,
1394 nf_conntrack_get(&ct->ct_general);
1395 spin_unlock_bh(&nf_conntrack_lock);
1396 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1397 events = IPCT_RELATED;
1401 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1402 (1 << IPCT_ASSURED) |
1403 (1 << IPCT_HELPER) |
1404 (1 << IPCT_PROTOINFO) |
1405 (1 << IPCT_NATSEQADJ) |
1406 (1 << IPCT_MARK) | events,
1407 ct, NETLINK_CB(skb).pid,
1411 spin_unlock_bh(&nf_conntrack_lock);
1415 /* implicit 'else' */
1417 /* We manipulate the conntrack inside the global conntrack table lock,
1418 * so there's no need to increase the refcount */
1420 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1421 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1423 err = ctnetlink_change_conntrack(ct, cda);
1425 nf_conntrack_get(&ct->ct_general);
1426 spin_unlock_bh(&nf_conntrack_lock);
1427 nf_conntrack_eventmask_report((1 << IPCT_REPLY) |
1428 (1 << IPCT_ASSURED) |
1429 (1 << IPCT_HELPER) |
1430 (1 << IPCT_PROTOINFO) |
1431 (1 << IPCT_NATSEQADJ) |
1433 ct, NETLINK_CB(skb).pid,
1437 spin_unlock_bh(&nf_conntrack_lock);
1443 spin_unlock_bh(&nf_conntrack_lock);
1447 /***********************************************************************
1449 ***********************************************************************/
1452 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1453 const struct nf_conntrack_tuple *tuple,
1454 enum ctattr_expect type)
1456 struct nlattr *nest_parms;
1458 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1460 goto nla_put_failure;
1461 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1462 goto nla_put_failure;
1463 nla_nest_end(skb, nest_parms);
1472 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1473 const struct nf_conntrack_tuple *tuple,
1474 const struct nf_conntrack_tuple_mask *mask)
1477 struct nf_conntrack_l3proto *l3proto;
1478 struct nf_conntrack_l4proto *l4proto;
1479 struct nf_conntrack_tuple m;
1480 struct nlattr *nest_parms;
1482 memset(&m, 0xFF, sizeof(m));
1483 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1484 m.src.u.all = mask->src.u.all;
1485 m.dst.protonum = tuple->dst.protonum;
1487 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1489 goto nla_put_failure;
1491 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1492 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1494 if (unlikely(ret < 0))
1495 goto nla_put_failure;
1497 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1498 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1499 if (unlikely(ret < 0))
1500 goto nla_put_failure;
1502 nla_nest_end(skb, nest_parms);
1511 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1512 const struct nf_conntrack_expect *exp)
1514 struct nf_conn *master = exp->master;
1515 long timeout = (exp->timeout.expires - jiffies) / HZ;
1520 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1521 goto nla_put_failure;
1522 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1523 goto nla_put_failure;
1524 if (ctnetlink_exp_dump_tuple(skb,
1525 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1526 CTA_EXPECT_MASTER) < 0)
1527 goto nla_put_failure;
1529 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1530 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1539 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1540 int event, const struct nf_conntrack_expect *exp)
1542 struct nlmsghdr *nlh;
1543 struct nfgenmsg *nfmsg;
1544 unsigned int flags = pid ? NLM_F_MULTI : 0;
1546 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1547 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1551 nfmsg = nlmsg_data(nlh);
1552 nfmsg->nfgen_family = exp->tuple.src.l3num;
1553 nfmsg->version = NFNETLINK_V0;
1556 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1557 goto nla_put_failure;
1559 nlmsg_end(skb, nlh);
1564 nlmsg_cancel(skb, nlh);
1568 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1570 ctnetlink_expect_event(unsigned int events, struct nf_exp_event *item)
1572 struct nf_conntrack_expect *exp = item->exp;
1573 struct net *net = nf_ct_exp_net(exp);
1574 struct nlmsghdr *nlh;
1575 struct nfgenmsg *nfmsg;
1576 struct sk_buff *skb;
1580 if (events & (1 << IPEXP_NEW)) {
1581 type = IPCTNL_MSG_EXP_NEW;
1582 flags = NLM_F_CREATE|NLM_F_EXCL;
1586 if (!item->report &&
1587 !nfnetlink_has_listeners(net, NFNLGRP_CONNTRACK_EXP_NEW))
1590 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1594 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1595 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1599 nfmsg = nlmsg_data(nlh);
1600 nfmsg->nfgen_family = exp->tuple.src.l3num;
1601 nfmsg->version = NFNETLINK_V0;
1605 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1606 goto nla_put_failure;
1609 nlmsg_end(skb, nlh);
1610 nfnetlink_send(skb, net, item->pid, NFNLGRP_CONNTRACK_EXP_NEW,
1611 item->report, GFP_ATOMIC);
1616 nlmsg_cancel(skb, nlh);
1620 nfnetlink_set_err(net, 0, 0, -ENOBUFS);
1624 static int ctnetlink_exp_done(struct netlink_callback *cb)
1627 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1632 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1634 struct net *net = sock_net(skb->sk);
1635 struct nf_conntrack_expect *exp, *last;
1636 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1637 struct hlist_node *n;
1638 u_int8_t l3proto = nfmsg->nfgen_family;
1641 last = (struct nf_conntrack_expect *)cb->args[1];
1642 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1644 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1646 if (l3proto && exp->tuple.src.l3num != l3proto)
1653 if (ctnetlink_exp_fill_info(skb,
1654 NETLINK_CB(cb->skb).pid,
1658 if (!atomic_inc_not_zero(&exp->use))
1660 cb->args[1] = (unsigned long)exp;
1672 nf_ct_expect_put(last);
1677 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1678 [CTA_EXPECT_MASTER] = { .type = NLA_NESTED },
1679 [CTA_EXPECT_TUPLE] = { .type = NLA_NESTED },
1680 [CTA_EXPECT_MASK] = { .type = NLA_NESTED },
1681 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1682 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1683 [CTA_EXPECT_HELP_NAME] = { .type = NLA_NUL_STRING },
1687 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1688 const struct nlmsghdr *nlh,
1689 const struct nlattr * const cda[])
1691 struct net *net = sock_net(ctnl);
1692 struct nf_conntrack_tuple tuple;
1693 struct nf_conntrack_expect *exp;
1694 struct sk_buff *skb2;
1695 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1696 u_int8_t u3 = nfmsg->nfgen_family;
1699 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1700 return netlink_dump_start(ctnl, skb, nlh,
1701 ctnetlink_exp_dump_table,
1702 ctnetlink_exp_done);
1705 if (cda[CTA_EXPECT_MASTER])
1706 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1713 exp = nf_ct_expect_find_get(net, &tuple);
1717 if (cda[CTA_EXPECT_ID]) {
1718 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1719 if (ntohl(id) != (u32)(unsigned long)exp) {
1720 nf_ct_expect_put(exp);
1726 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1731 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1732 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1737 nf_ct_expect_put(exp);
1739 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1744 nf_ct_expect_put(exp);
1749 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1750 const struct nlmsghdr *nlh,
1751 const struct nlattr * const cda[])
1753 struct net *net = sock_net(ctnl);
1754 struct nf_conntrack_expect *exp;
1755 struct nf_conntrack_tuple tuple;
1756 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1757 struct hlist_node *n, *next;
1758 u_int8_t u3 = nfmsg->nfgen_family;
1762 if (cda[CTA_EXPECT_TUPLE]) {
1763 /* delete a single expect by tuple */
1764 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1768 /* bump usage count to 2 */
1769 exp = nf_ct_expect_find_get(net, &tuple);
1773 if (cda[CTA_EXPECT_ID]) {
1774 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1775 if (ntohl(id) != (u32)(unsigned long)exp) {
1776 nf_ct_expect_put(exp);
1781 /* after list removal, usage count == 1 */
1782 nf_ct_unexpect_related(exp);
1783 /* have to put what we 'get' above.
1784 * after this line usage count == 0 */
1785 nf_ct_expect_put(exp);
1786 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1787 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1788 struct nf_conn_help *m_help;
1790 /* delete all expectations for this helper */
1791 spin_lock_bh(&nf_conntrack_lock);
1792 for (i = 0; i < nf_ct_expect_hsize; i++) {
1793 hlist_for_each_entry_safe(exp, n, next,
1794 &net->ct.expect_hash[i],
1796 m_help = nfct_help(exp->master);
1797 if (!strcmp(m_help->helper->name, name) &&
1798 del_timer(&exp->timeout)) {
1799 nf_ct_unlink_expect(exp);
1800 nf_ct_expect_put(exp);
1804 spin_unlock_bh(&nf_conntrack_lock);
1806 /* This basically means we have to flush everything*/
1807 spin_lock_bh(&nf_conntrack_lock);
1808 for (i = 0; i < nf_ct_expect_hsize; i++) {
1809 hlist_for_each_entry_safe(exp, n, next,
1810 &net->ct.expect_hash[i],
1812 if (del_timer(&exp->timeout)) {
1813 nf_ct_unlink_expect(exp);
1814 nf_ct_expect_put(exp);
1818 spin_unlock_bh(&nf_conntrack_lock);
1824 ctnetlink_change_expect(struct nf_conntrack_expect *x,
1825 const struct nlattr * const cda[])
1831 ctnetlink_create_expect(struct net *net, const struct nlattr * const cda[],
1833 u32 pid, int report)
1835 struct nf_conntrack_tuple tuple, mask, master_tuple;
1836 struct nf_conntrack_tuple_hash *h = NULL;
1837 struct nf_conntrack_expect *exp;
1839 struct nf_conn_help *help;
1842 /* caller guarantees that those three CTA_EXPECT_* exist */
1843 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1846 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1849 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1853 /* Look for master conntrack of this expectation */
1854 h = nf_conntrack_find_get(net, &master_tuple);
1857 ct = nf_ct_tuplehash_to_ctrack(h);
1858 help = nfct_help(ct);
1860 if (!help || !help->helper) {
1861 /* such conntrack hasn't got any helper, abort */
1866 exp = nf_ct_expect_alloc(ct);
1873 exp->expectfn = NULL;
1877 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1878 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1879 exp->mask.src.u.all = mask.src.u.all;
1881 err = nf_ct_expect_related_report(exp, pid, report);
1882 nf_ct_expect_put(exp);
1885 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1890 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1891 const struct nlmsghdr *nlh,
1892 const struct nlattr * const cda[])
1894 struct net *net = sock_net(ctnl);
1895 struct nf_conntrack_tuple tuple;
1896 struct nf_conntrack_expect *exp;
1897 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1898 u_int8_t u3 = nfmsg->nfgen_family;
1901 if (!cda[CTA_EXPECT_TUPLE]
1902 || !cda[CTA_EXPECT_MASK]
1903 || !cda[CTA_EXPECT_MASTER])
1906 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1910 spin_lock_bh(&nf_conntrack_lock);
1911 exp = __nf_ct_expect_find(net, &tuple);
1914 spin_unlock_bh(&nf_conntrack_lock);
1916 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1917 err = ctnetlink_create_expect(net, cda,
1919 NETLINK_CB(skb).pid,
1926 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1927 err = ctnetlink_change_expect(exp, cda);
1928 spin_unlock_bh(&nf_conntrack_lock);
1933 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1934 static struct nf_ct_event_notifier ctnl_notifier = {
1935 .fcn = ctnetlink_conntrack_event,
1938 static struct nf_exp_event_notifier ctnl_notifier_exp = {
1939 .fcn = ctnetlink_expect_event,
1943 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1944 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1945 .attr_count = CTA_MAX,
1946 .policy = ct_nla_policy },
1947 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1948 .attr_count = CTA_MAX,
1949 .policy = ct_nla_policy },
1950 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1951 .attr_count = CTA_MAX,
1952 .policy = ct_nla_policy },
1953 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1954 .attr_count = CTA_MAX,
1955 .policy = ct_nla_policy },
1958 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1959 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1960 .attr_count = CTA_EXPECT_MAX,
1961 .policy = exp_nla_policy },
1962 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1963 .attr_count = CTA_EXPECT_MAX,
1964 .policy = exp_nla_policy },
1965 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1966 .attr_count = CTA_EXPECT_MAX,
1967 .policy = exp_nla_policy },
1970 static const struct nfnetlink_subsystem ctnl_subsys = {
1971 .name = "conntrack",
1972 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1973 .cb_count = IPCTNL_MSG_MAX,
1977 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1978 .name = "conntrack_expect",
1979 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1980 .cb_count = IPCTNL_MSG_EXP_MAX,
1984 MODULE_ALIAS("ip_conntrack_netlink");
1985 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1986 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1988 static int __init ctnetlink_init(void)
1992 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1993 ret = nfnetlink_subsys_register(&ctnl_subsys);
1995 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1999 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
2001 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
2002 goto err_unreg_subsys;
2005 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2006 ret = nf_conntrack_register_notifier(&ctnl_notifier);
2008 printk("ctnetlink_init: cannot register notifier.\n");
2009 goto err_unreg_exp_subsys;
2012 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
2014 printk("ctnetlink_init: cannot expect register notifier.\n");
2015 goto err_unreg_notifier;
2021 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2023 nf_conntrack_unregister_notifier(&ctnl_notifier);
2024 err_unreg_exp_subsys:
2025 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2028 nfnetlink_subsys_unregister(&ctnl_subsys);
2033 static void __exit ctnetlink_exit(void)
2035 printk("ctnetlink: unregistering from nfnetlink.\n");
2037 #ifdef CONFIG_NF_CONNTRACK_EVENTS
2038 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
2039 nf_conntrack_unregister_notifier(&ctnl_notifier);
2042 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
2043 nfnetlink_subsys_unregister(&ctnl_subsys);
2047 module_init(ctnetlink_init);
2048 module_exit(ctnetlink_exit);