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>
30 #include <linux/notifier.h>
32 #include <linux/netfilter.h>
33 #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, const 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, const 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 * The general structure of a ctnetlink event is
410 * <l3/l4-proto-attributes>
412 * <l3/l4-proto-attributes>
416 * <l4-proto-attributes>
418 * <l3/l4-proto-attributes>
420 * Therefore the formular is
422 * size = sizeof(headers) + sizeof(generic_nlas) + 3 * sizeof(tuple_nlas)
423 * + sizeof(protoinfo_nlas)
425 static struct sk_buff *
426 ctnetlink_alloc_skb(const struct nf_conntrack_tuple *tuple, gfp_t gfp)
428 struct nf_conntrack_l3proto *l3proto;
429 struct nf_conntrack_l4proto *l4proto;
432 #define NLA_TYPE_SIZE(type) nla_total_size(sizeof(type))
434 /* proto independant part */
435 len = NLMSG_ALIGN(sizeof(struct nfgenmsg))
436 + 3 * nla_total_size(0) /* CTA_TUPLE_ORIG|REPL|MASTER */
437 + 3 * nla_total_size(0) /* CTA_TUPLE_IP */
438 + 3 * nla_total_size(0) /* CTA_TUPLE_PROTO */
439 + 3 * NLA_TYPE_SIZE(u_int8_t) /* CTA_PROTO_NUM */
440 + NLA_TYPE_SIZE(u_int32_t) /* CTA_ID */
441 + NLA_TYPE_SIZE(u_int32_t) /* CTA_STATUS */
442 #ifdef CONFIG_NF_CT_ACCT
443 + 2 * nla_total_size(0) /* CTA_COUNTERS_ORIG|REPL */
444 + 2 * NLA_TYPE_SIZE(uint64_t) /* CTA_COUNTERS_PACKETS */
445 + 2 * NLA_TYPE_SIZE(uint64_t) /* CTA_COUNTERS_BYTES */
447 + NLA_TYPE_SIZE(u_int32_t) /* CTA_TIMEOUT */
448 + nla_total_size(0) /* CTA_PROTOINFO */
449 + nla_total_size(0) /* CTA_HELP */
450 + nla_total_size(NF_CT_HELPER_NAME_LEN) /* CTA_HELP_NAME */
451 #ifdef CONFIG_NF_CONNTRACK_SECMARK
452 + NLA_TYPE_SIZE(u_int32_t) /* CTA_SECMARK */
454 #ifdef CONFIG_NF_NAT_NEEDED
455 + 2 * nla_total_size(0) /* CTA_NAT_SEQ_ADJ_ORIG|REPL */
456 + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_POS */
457 + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_BEFORE */
458 + 2 * NLA_TYPE_SIZE(u_int32_t) /* CTA_NAT_SEQ_CORRECTION_AFTER */
460 #ifdef CONFIG_NF_CONNTRACK_MARK
461 + NLA_TYPE_SIZE(u_int32_t) /* CTA_MARK */
468 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
469 len += l3proto->nla_size;
471 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
472 len += l4proto->nla_size;
475 return nlmsg_new(len, gfp);
478 static int ctnetlink_conntrack_event(struct notifier_block *this,
479 unsigned long events, void *ptr)
481 struct nlmsghdr *nlh;
482 struct nfgenmsg *nfmsg;
483 struct nlattr *nest_parms;
484 struct nf_ct_event *item = (struct nf_ct_event *)ptr;
485 struct nf_conn *ct = item->ct;
488 unsigned int flags = 0, group;
490 /* ignore our fake conntrack entry */
491 if (ct == &nf_conntrack_untracked)
494 if (events & IPCT_DESTROY) {
495 type = IPCTNL_MSG_CT_DELETE;
496 group = NFNLGRP_CONNTRACK_DESTROY;
497 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
498 type = IPCTNL_MSG_CT_NEW;
499 flags = NLM_F_CREATE|NLM_F_EXCL;
500 group = NFNLGRP_CONNTRACK_NEW;
501 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
502 type = IPCTNL_MSG_CT_NEW;
503 group = NFNLGRP_CONNTRACK_UPDATE;
507 if (!item->report && !nfnetlink_has_listeners(group))
510 skb = ctnetlink_alloc_skb(nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL),
515 type |= NFNL_SUBSYS_CTNETLINK << 8;
516 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
520 nfmsg = nlmsg_data(nlh);
521 nfmsg->nfgen_family = nf_ct_l3num(ct);
522 nfmsg->version = NFNETLINK_V0;
526 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
528 goto nla_put_failure;
529 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
530 goto nla_put_failure;
531 nla_nest_end(skb, nest_parms);
533 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
535 goto nla_put_failure;
536 if (ctnetlink_dump_tuples(skb, nf_ct_tuple(ct, IP_CT_DIR_REPLY)) < 0)
537 goto nla_put_failure;
538 nla_nest_end(skb, nest_parms);
540 if (ctnetlink_dump_id(skb, ct) < 0)
541 goto nla_put_failure;
543 if (ctnetlink_dump_status(skb, ct) < 0)
544 goto nla_put_failure;
546 if (events & IPCT_DESTROY) {
547 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
548 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
549 goto nla_put_failure;
551 if (ctnetlink_dump_timeout(skb, ct) < 0)
552 goto nla_put_failure;
554 if (events & IPCT_PROTOINFO
555 && ctnetlink_dump_protoinfo(skb, ct) < 0)
556 goto nla_put_failure;
558 if ((events & IPCT_HELPER || nfct_help(ct))
559 && ctnetlink_dump_helpinfo(skb, ct) < 0)
560 goto nla_put_failure;
562 #ifdef CONFIG_NF_CONNTRACK_SECMARK
563 if ((events & IPCT_SECMARK || ct->secmark)
564 && ctnetlink_dump_secmark(skb, ct) < 0)
565 goto nla_put_failure;
568 if (events & IPCT_RELATED &&
569 ctnetlink_dump_master(skb, ct) < 0)
570 goto nla_put_failure;
572 if (events & IPCT_NATSEQADJ &&
573 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
574 goto nla_put_failure;
577 #ifdef CONFIG_NF_CONNTRACK_MARK
578 if ((events & IPCT_MARK || ct->mark)
579 && ctnetlink_dump_mark(skb, ct) < 0)
580 goto nla_put_failure;
585 nfnetlink_send(skb, item->pid, group, item->report);
590 nlmsg_cancel(skb, nlh);
594 nfnetlink_set_err(0, group, -ENOBUFS);
597 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
599 static int ctnetlink_done(struct netlink_callback *cb)
602 nf_ct_put((struct nf_conn *)cb->args[1]);
607 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
609 struct nf_conn *ct, *last;
610 struct nf_conntrack_tuple_hash *h;
611 struct hlist_nulls_node *n;
612 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
613 u_int8_t l3proto = nfmsg->nfgen_family;
616 last = (struct nf_conn *)cb->args[1];
617 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
619 hlist_nulls_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
621 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
623 ct = nf_ct_tuplehash_to_ctrack(h);
624 if (!atomic_inc_not_zero(&ct->ct_general.use))
626 /* Dump entries of a given L3 protocol number.
627 * If it is not specified, ie. l3proto == 0,
628 * then dump everything. */
629 if (l3proto && nf_ct_l3num(ct) != l3proto)
636 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
638 IPCTNL_MSG_CT_NEW, ct) < 0) {
639 cb->args[1] = (unsigned long)ct;
643 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
644 IPCTNL_MSG_CT_GET_CTRZERO) {
645 struct nf_conn_counter *acct;
647 acct = nf_conn_acct_find(ct);
649 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
668 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
670 struct nlattr *tb[CTA_IP_MAX+1];
671 struct nf_conntrack_l3proto *l3proto;
674 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
677 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
679 if (likely(l3proto->nlattr_to_tuple)) {
680 ret = nla_validate_nested(attr, CTA_IP_MAX,
681 l3proto->nla_policy);
683 ret = l3proto->nlattr_to_tuple(tb, tuple);
691 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
692 [CTA_PROTO_NUM] = { .type = NLA_U8 },
696 ctnetlink_parse_tuple_proto(struct nlattr *attr,
697 struct nf_conntrack_tuple *tuple)
699 struct nlattr *tb[CTA_PROTO_MAX+1];
700 struct nf_conntrack_l4proto *l4proto;
703 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
707 if (!tb[CTA_PROTO_NUM])
709 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
712 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
714 if (likely(l4proto->nlattr_to_tuple)) {
715 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
716 l4proto->nla_policy);
718 ret = l4proto->nlattr_to_tuple(tb, tuple);
727 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
728 enum ctattr_tuple type, u_int8_t l3num)
730 struct nlattr *tb[CTA_TUPLE_MAX+1];
733 memset(tuple, 0, sizeof(*tuple));
735 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
737 if (!tb[CTA_TUPLE_IP])
740 tuple->src.l3num = l3num;
742 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
746 if (!tb[CTA_TUPLE_PROTO])
749 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
753 /* orig and expect tuples get DIR_ORIGINAL */
754 if (type == CTA_TUPLE_REPLY)
755 tuple->dst.dir = IP_CT_DIR_REPLY;
757 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
763 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
765 struct nlattr *tb[CTA_HELP_MAX+1];
767 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
769 if (!tb[CTA_HELP_NAME])
772 *helper_name = nla_data(tb[CTA_HELP_NAME]);
777 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
778 [CTA_STATUS] = { .type = NLA_U32 },
779 [CTA_TIMEOUT] = { .type = NLA_U32 },
780 [CTA_MARK] = { .type = NLA_U32 },
781 [CTA_USE] = { .type = NLA_U32 },
782 [CTA_ID] = { .type = NLA_U32 },
786 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
787 struct nlmsghdr *nlh, struct nlattr *cda[])
789 struct nf_conntrack_tuple_hash *h;
790 struct nf_conntrack_tuple tuple;
792 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
793 u_int8_t u3 = nfmsg->nfgen_family;
796 if (cda[CTA_TUPLE_ORIG])
797 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
798 else if (cda[CTA_TUPLE_REPLY])
799 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
801 /* Flush the whole table */
802 nf_conntrack_flush(&init_net,
811 h = nf_conntrack_find_get(&init_net, &tuple);
815 ct = nf_ct_tuplehash_to_ctrack(h);
818 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
819 if (id != (u32)(unsigned long)ct) {
825 nf_conntrack_event_report(IPCT_DESTROY,
830 /* death_by_timeout would report the event again */
831 set_bit(IPS_DYING_BIT, &ct->status);
840 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
841 struct nlmsghdr *nlh, struct nlattr *cda[])
843 struct nf_conntrack_tuple_hash *h;
844 struct nf_conntrack_tuple tuple;
846 struct sk_buff *skb2 = NULL;
847 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
848 u_int8_t u3 = nfmsg->nfgen_family;
851 if (nlh->nlmsg_flags & NLM_F_DUMP)
852 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
855 if (cda[CTA_TUPLE_ORIG])
856 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
857 else if (cda[CTA_TUPLE_REPLY])
858 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
865 h = nf_conntrack_find_get(&init_net, &tuple);
869 ct = nf_ct_tuplehash_to_ctrack(h);
872 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
879 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
880 IPCTNL_MSG_CT_NEW, ct);
886 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
898 #ifdef CONFIG_NF_NAT_NEEDED
900 ctnetlink_parse_nat_setup(struct nf_conn *ct,
901 enum nf_nat_manip_type manip,
904 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
906 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
907 if (!parse_nat_setup) {
908 #ifdef CONFIG_MODULES
910 spin_unlock_bh(&nf_conntrack_lock);
912 if (request_module("nf-nat-ipv4") < 0) {
914 spin_lock_bh(&nf_conntrack_lock);
919 spin_lock_bh(&nf_conntrack_lock);
921 if (nfnetlink_parse_nat_setup_hook)
927 return parse_nat_setup(ct, manip, attr);
932 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
935 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
936 d = ct->status ^ status;
938 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
942 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
943 /* SEEN_REPLY bit can only be set */
946 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
947 /* ASSURED bit can only be set */
950 /* Be careful here, modifying NAT bits can screw up things,
951 * so don't let users modify them directly if they don't pass
953 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
958 ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
960 #ifdef CONFIG_NF_NAT_NEEDED
963 if (cda[CTA_NAT_DST]) {
964 ret = ctnetlink_parse_nat_setup(ct,
970 if (cda[CTA_NAT_SRC]) {
971 ret = ctnetlink_parse_nat_setup(ct,
984 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
986 struct nf_conntrack_helper *helper;
987 struct nf_conn_help *help = nfct_help(ct);
988 char *helpname = NULL;
991 /* don't change helper of sibling connections */
995 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
999 if (!strcmp(helpname, "")) {
1000 if (help && help->helper) {
1001 /* we had a helper before ... */
1002 nf_ct_remove_expectations(ct);
1003 rcu_assign_pointer(help->helper, NULL);
1009 helper = __nf_conntrack_helper_find_byname(helpname);
1010 if (helper == NULL) {
1011 #ifdef CONFIG_MODULES
1012 spin_unlock_bh(&nf_conntrack_lock);
1014 if (request_module("nfct-helper-%s", helpname) < 0) {
1015 spin_lock_bh(&nf_conntrack_lock);
1019 spin_lock_bh(&nf_conntrack_lock);
1020 helper = __nf_conntrack_helper_find_byname(helpname);
1028 if (help->helper == helper)
1032 /* need to zero data of old helper */
1033 memset(&help->help, 0, sizeof(help->help));
1035 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1040 rcu_assign_pointer(help->helper, helper);
1046 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
1048 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1050 if (!del_timer(&ct->timeout))
1053 ct->timeout.expires = jiffies + timeout * HZ;
1054 add_timer(&ct->timeout);
1060 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
1062 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
1063 struct nf_conntrack_l4proto *l4proto;
1066 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
1069 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
1070 if (l4proto->from_nlattr)
1071 err = l4proto->from_nlattr(tb, ct);
1077 #ifdef CONFIG_NF_NAT_NEEDED
1079 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
1081 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
1083 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
1085 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
1088 natseq->correction_pos =
1089 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1091 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1094 natseq->offset_before =
1095 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1097 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1100 natseq->offset_after =
1101 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1107 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1110 struct nf_conn_nat *nat = nfct_nat(ct);
1115 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1116 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1117 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1121 ct->status |= IPS_SEQ_ADJUST;
1124 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1125 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1126 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1130 ct->status |= IPS_SEQ_ADJUST;
1138 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1142 /* only allow NAT changes and master assignation for new conntracks */
1143 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST] || cda[CTA_TUPLE_MASTER])
1146 if (cda[CTA_HELP]) {
1147 err = ctnetlink_change_helper(ct, cda);
1152 if (cda[CTA_TIMEOUT]) {
1153 err = ctnetlink_change_timeout(ct, cda);
1158 if (cda[CTA_STATUS]) {
1159 err = ctnetlink_change_status(ct, cda);
1164 if (cda[CTA_PROTOINFO]) {
1165 err = ctnetlink_change_protoinfo(ct, cda);
1170 #if defined(CONFIG_NF_CONNTRACK_MARK)
1172 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1175 #ifdef CONFIG_NF_NAT_NEEDED
1176 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1177 err = ctnetlink_change_nat_seq_adj(ct, cda);
1186 static struct nf_conn *
1187 ctnetlink_create_conntrack(struct nlattr *cda[],
1188 struct nf_conntrack_tuple *otuple,
1189 struct nf_conntrack_tuple *rtuple,
1194 struct nf_conntrack_helper *helper;
1196 ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_ATOMIC);
1198 return ERR_PTR(-ENOMEM);
1200 if (!cda[CTA_TIMEOUT])
1202 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1204 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1205 ct->status |= IPS_CONFIRMED;
1208 if (cda[CTA_HELP]) {
1209 char *helpname = NULL;
1211 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1215 helper = __nf_conntrack_helper_find_byname(helpname);
1216 if (helper == NULL) {
1218 #ifdef CONFIG_MODULES
1219 if (request_module("nfct-helper-%s", helpname) < 0) {
1225 helper = __nf_conntrack_helper_find_byname(helpname);
1235 struct nf_conn_help *help;
1237 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1243 /* not in hash table yet so not strictly necessary */
1244 rcu_assign_pointer(help->helper, helper);
1247 /* try an implicit helper assignation */
1248 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1253 if (cda[CTA_STATUS]) {
1254 err = ctnetlink_change_status(ct, cda);
1259 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1260 err = ctnetlink_change_nat(ct, cda);
1265 #ifdef CONFIG_NF_NAT_NEEDED
1266 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1267 err = ctnetlink_change_nat_seq_adj(ct, cda);
1273 if (cda[CTA_PROTOINFO]) {
1274 err = ctnetlink_change_protoinfo(ct, cda);
1279 nf_ct_acct_ext_add(ct, GFP_ATOMIC);
1281 #if defined(CONFIG_NF_CONNTRACK_MARK)
1283 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1286 /* setup master conntrack: this is a confirmed expectation */
1287 if (cda[CTA_TUPLE_MASTER]) {
1288 struct nf_conntrack_tuple master;
1289 struct nf_conntrack_tuple_hash *master_h;
1290 struct nf_conn *master_ct;
1292 err = ctnetlink_parse_tuple(cda, &master, CTA_TUPLE_MASTER, u3);
1296 master_h = nf_conntrack_find_get(&init_net, &master);
1297 if (master_h == NULL) {
1301 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1302 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1303 ct->master = master_ct;
1306 add_timer(&ct->timeout);
1307 nf_conntrack_hash_insert(ct);
1315 nf_conntrack_free(ct);
1316 return ERR_PTR(err);
1320 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1321 struct nlmsghdr *nlh, struct nlattr *cda[])
1323 struct nf_conntrack_tuple otuple, rtuple;
1324 struct nf_conntrack_tuple_hash *h = NULL;
1325 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1326 u_int8_t u3 = nfmsg->nfgen_family;
1329 if (cda[CTA_TUPLE_ORIG]) {
1330 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1335 if (cda[CTA_TUPLE_REPLY]) {
1336 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1341 spin_lock_bh(&nf_conntrack_lock);
1342 if (cda[CTA_TUPLE_ORIG])
1343 h = __nf_conntrack_find(&init_net, &otuple);
1344 else if (cda[CTA_TUPLE_REPLY])
1345 h = __nf_conntrack_find(&init_net, &rtuple);
1349 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1351 enum ip_conntrack_events events;
1353 ct = ctnetlink_create_conntrack(cda, &otuple,
1360 nf_conntrack_get(&ct->ct_general);
1361 spin_unlock_bh(&nf_conntrack_lock);
1362 if (test_bit(IPS_EXPECTED_BIT, &ct->status))
1363 events = IPCT_RELATED;
1367 nf_conntrack_event_report(IPCT_STATUS |
1372 ct, NETLINK_CB(skb).pid,
1376 spin_unlock_bh(&nf_conntrack_lock);
1380 /* implicit 'else' */
1382 /* We manipulate the conntrack inside the global conntrack table lock,
1383 * so there's no need to increase the refcount */
1385 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1386 struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
1388 err = ctnetlink_change_conntrack(ct, cda);
1390 nf_conntrack_get(&ct->ct_general);
1391 spin_unlock_bh(&nf_conntrack_lock);
1392 nf_conntrack_event_report(IPCT_STATUS |
1397 ct, NETLINK_CB(skb).pid,
1401 spin_unlock_bh(&nf_conntrack_lock);
1407 spin_unlock_bh(&nf_conntrack_lock);
1411 /***********************************************************************
1413 ***********************************************************************/
1416 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1417 const struct nf_conntrack_tuple *tuple,
1418 enum ctattr_expect type)
1420 struct nlattr *nest_parms;
1422 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1424 goto nla_put_failure;
1425 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1426 goto nla_put_failure;
1427 nla_nest_end(skb, nest_parms);
1436 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1437 const struct nf_conntrack_tuple *tuple,
1438 const struct nf_conntrack_tuple_mask *mask)
1441 struct nf_conntrack_l3proto *l3proto;
1442 struct nf_conntrack_l4proto *l4proto;
1443 struct nf_conntrack_tuple m;
1444 struct nlattr *nest_parms;
1446 memset(&m, 0xFF, sizeof(m));
1447 m.src.u.all = mask->src.u.all;
1448 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1450 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1452 goto nla_put_failure;
1454 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1455 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1457 if (unlikely(ret < 0))
1458 goto nla_put_failure;
1460 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1461 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1462 if (unlikely(ret < 0))
1463 goto nla_put_failure;
1465 nla_nest_end(skb, nest_parms);
1474 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1475 const struct nf_conntrack_expect *exp)
1477 struct nf_conn *master = exp->master;
1478 long timeout = (exp->timeout.expires - jiffies) / HZ;
1483 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1484 goto nla_put_failure;
1485 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1486 goto nla_put_failure;
1487 if (ctnetlink_exp_dump_tuple(skb,
1488 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1489 CTA_EXPECT_MASTER) < 0)
1490 goto nla_put_failure;
1492 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1493 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1502 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1503 int event, const struct nf_conntrack_expect *exp)
1505 struct nlmsghdr *nlh;
1506 struct nfgenmsg *nfmsg;
1507 unsigned int flags = pid ? NLM_F_MULTI : 0;
1509 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1510 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*nfmsg), flags);
1514 nfmsg = nlmsg_data(nlh);
1515 nfmsg->nfgen_family = exp->tuple.src.l3num;
1516 nfmsg->version = NFNETLINK_V0;
1519 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1520 goto nla_put_failure;
1522 nlmsg_end(skb, nlh);
1527 nlmsg_cancel(skb, nlh);
1531 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1532 static int ctnetlink_expect_event(struct notifier_block *this,
1533 unsigned long events, void *ptr)
1535 struct nlmsghdr *nlh;
1536 struct nfgenmsg *nfmsg;
1537 struct nf_exp_event *item = (struct nf_exp_event *)ptr;
1538 struct nf_conntrack_expect *exp = item->exp;
1539 struct sk_buff *skb;
1543 if (events & IPEXP_NEW) {
1544 type = IPCTNL_MSG_EXP_NEW;
1545 flags = NLM_F_CREATE|NLM_F_EXCL;
1549 if (!item->report &&
1550 !nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1553 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1557 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1558 nlh = nlmsg_put(skb, item->pid, 0, type, sizeof(*nfmsg), flags);
1562 nfmsg = nlmsg_data(nlh);
1563 nfmsg->nfgen_family = exp->tuple.src.l3num;
1564 nfmsg->version = NFNETLINK_V0;
1568 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1569 goto nla_put_failure;
1572 nlmsg_end(skb, nlh);
1573 nfnetlink_send(skb, item->pid, NFNLGRP_CONNTRACK_EXP_NEW, item->report);
1578 nlmsg_cancel(skb, nlh);
1582 nfnetlink_set_err(0, 0, -ENOBUFS);
1586 static int ctnetlink_exp_done(struct netlink_callback *cb)
1589 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1594 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1596 struct net *net = &init_net;
1597 struct nf_conntrack_expect *exp, *last;
1598 struct nfgenmsg *nfmsg = nlmsg_data(cb->nlh);
1599 struct hlist_node *n;
1600 u_int8_t l3proto = nfmsg->nfgen_family;
1603 last = (struct nf_conntrack_expect *)cb->args[1];
1604 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1606 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1608 if (l3proto && exp->tuple.src.l3num != l3proto)
1615 if (ctnetlink_exp_fill_info(skb,
1616 NETLINK_CB(cb->skb).pid,
1620 if (!atomic_inc_not_zero(&exp->use))
1622 cb->args[1] = (unsigned long)exp;
1634 nf_ct_expect_put(last);
1639 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1640 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1641 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1645 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1646 struct nlmsghdr *nlh, struct nlattr *cda[])
1648 struct nf_conntrack_tuple tuple;
1649 struct nf_conntrack_expect *exp;
1650 struct sk_buff *skb2;
1651 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1652 u_int8_t u3 = nfmsg->nfgen_family;
1655 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1656 return netlink_dump_start(ctnl, skb, nlh,
1657 ctnetlink_exp_dump_table,
1658 ctnetlink_exp_done);
1661 if (cda[CTA_EXPECT_MASTER])
1662 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1669 exp = nf_ct_expect_find_get(&init_net, &tuple);
1673 if (cda[CTA_EXPECT_ID]) {
1674 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1675 if (ntohl(id) != (u32)(unsigned long)exp) {
1676 nf_ct_expect_put(exp);
1682 skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1687 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1688 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, exp);
1693 nf_ct_expect_put(exp);
1695 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1700 nf_ct_expect_put(exp);
1705 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1706 struct nlmsghdr *nlh, struct nlattr *cda[])
1708 struct nf_conntrack_expect *exp;
1709 struct nf_conntrack_tuple tuple;
1710 struct nf_conntrack_helper *h;
1711 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1712 struct hlist_node *n, *next;
1713 u_int8_t u3 = nfmsg->nfgen_family;
1717 if (cda[CTA_EXPECT_TUPLE]) {
1718 /* delete a single expect by tuple */
1719 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1723 /* bump usage count to 2 */
1724 exp = nf_ct_expect_find_get(&init_net, &tuple);
1728 if (cda[CTA_EXPECT_ID]) {
1729 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1730 if (ntohl(id) != (u32)(unsigned long)exp) {
1731 nf_ct_expect_put(exp);
1736 /* after list removal, usage count == 1 */
1737 nf_ct_unexpect_related(exp);
1738 /* have to put what we 'get' above.
1739 * after this line usage count == 0 */
1740 nf_ct_expect_put(exp);
1741 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1742 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1743 struct nf_conn_help *m_help;
1745 /* delete all expectations for this helper */
1746 spin_lock_bh(&nf_conntrack_lock);
1747 h = __nf_conntrack_helper_find_byname(name);
1749 spin_unlock_bh(&nf_conntrack_lock);
1752 for (i = 0; i < nf_ct_expect_hsize; i++) {
1753 hlist_for_each_entry_safe(exp, n, next,
1754 &init_net.ct.expect_hash[i],
1756 m_help = nfct_help(exp->master);
1757 if (m_help->helper == h
1758 && del_timer(&exp->timeout)) {
1759 nf_ct_unlink_expect(exp);
1760 nf_ct_expect_put(exp);
1764 spin_unlock_bh(&nf_conntrack_lock);
1766 /* This basically means we have to flush everything*/
1767 spin_lock_bh(&nf_conntrack_lock);
1768 for (i = 0; i < nf_ct_expect_hsize; i++) {
1769 hlist_for_each_entry_safe(exp, n, next,
1770 &init_net.ct.expect_hash[i],
1772 if (del_timer(&exp->timeout)) {
1773 nf_ct_unlink_expect(exp);
1774 nf_ct_expect_put(exp);
1778 spin_unlock_bh(&nf_conntrack_lock);
1784 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1790 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3, u32 pid, int report)
1792 struct nf_conntrack_tuple tuple, mask, master_tuple;
1793 struct nf_conntrack_tuple_hash *h = NULL;
1794 struct nf_conntrack_expect *exp;
1796 struct nf_conn_help *help;
1799 /* caller guarantees that those three CTA_EXPECT_* exist */
1800 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1803 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1806 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1810 /* Look for master conntrack of this expectation */
1811 h = nf_conntrack_find_get(&init_net, &master_tuple);
1814 ct = nf_ct_tuplehash_to_ctrack(h);
1815 help = nfct_help(ct);
1817 if (!help || !help->helper) {
1818 /* such conntrack hasn't got any helper, abort */
1823 exp = nf_ct_expect_alloc(ct);
1830 exp->expectfn = NULL;
1834 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1835 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1836 exp->mask.src.u.all = mask.src.u.all;
1838 err = nf_ct_expect_related_report(exp, pid, report);
1839 nf_ct_expect_put(exp);
1842 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1847 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1848 struct nlmsghdr *nlh, struct nlattr *cda[])
1850 struct nf_conntrack_tuple tuple;
1851 struct nf_conntrack_expect *exp;
1852 struct nfgenmsg *nfmsg = nlmsg_data(nlh);
1853 u_int8_t u3 = nfmsg->nfgen_family;
1856 if (!cda[CTA_EXPECT_TUPLE]
1857 || !cda[CTA_EXPECT_MASK]
1858 || !cda[CTA_EXPECT_MASTER])
1861 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1865 spin_lock_bh(&nf_conntrack_lock);
1866 exp = __nf_ct_expect_find(&init_net, &tuple);
1869 spin_unlock_bh(&nf_conntrack_lock);
1871 if (nlh->nlmsg_flags & NLM_F_CREATE) {
1872 err = ctnetlink_create_expect(cda,
1874 NETLINK_CB(skb).pid,
1881 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1882 err = ctnetlink_change_expect(exp, cda);
1883 spin_unlock_bh(&nf_conntrack_lock);
1888 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1889 static struct notifier_block ctnl_notifier = {
1890 .notifier_call = ctnetlink_conntrack_event,
1893 static struct notifier_block ctnl_notifier_exp = {
1894 .notifier_call = ctnetlink_expect_event,
1898 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1899 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1900 .attr_count = CTA_MAX,
1901 .policy = ct_nla_policy },
1902 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1903 .attr_count = CTA_MAX,
1904 .policy = ct_nla_policy },
1905 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1906 .attr_count = CTA_MAX,
1907 .policy = ct_nla_policy },
1908 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1909 .attr_count = CTA_MAX,
1910 .policy = ct_nla_policy },
1913 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1914 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1915 .attr_count = CTA_EXPECT_MAX,
1916 .policy = exp_nla_policy },
1917 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1918 .attr_count = CTA_EXPECT_MAX,
1919 .policy = exp_nla_policy },
1920 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1921 .attr_count = CTA_EXPECT_MAX,
1922 .policy = exp_nla_policy },
1925 static const struct nfnetlink_subsystem ctnl_subsys = {
1926 .name = "conntrack",
1927 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1928 .cb_count = IPCTNL_MSG_MAX,
1932 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1933 .name = "conntrack_expect",
1934 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1935 .cb_count = IPCTNL_MSG_EXP_MAX,
1939 MODULE_ALIAS("ip_conntrack_netlink");
1940 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1941 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1943 static int __init ctnetlink_init(void)
1947 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1948 ret = nfnetlink_subsys_register(&ctnl_subsys);
1950 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1954 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1956 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1957 goto err_unreg_subsys;
1960 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1961 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1963 printk("ctnetlink_init: cannot register notifier.\n");
1964 goto err_unreg_exp_subsys;
1967 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1969 printk("ctnetlink_init: cannot expect register notifier.\n");
1970 goto err_unreg_notifier;
1976 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1978 nf_conntrack_unregister_notifier(&ctnl_notifier);
1979 err_unreg_exp_subsys:
1980 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1983 nfnetlink_subsys_unregister(&ctnl_subsys);
1988 static void __exit ctnetlink_exit(void)
1990 printk("ctnetlink: unregistering from nfnetlink.\n");
1992 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1993 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1994 nf_conntrack_unregister_notifier(&ctnl_notifier);
1997 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1998 nfnetlink_subsys_unregister(&ctnl_subsys);
2002 module_init(ctnetlink_init);
2003 module_exit(ctnetlink_exit);