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/types.h>
23 #include <linux/timer.h>
24 #include <linux/skbuff.h>
25 #include <linux/errno.h>
26 #include <linux/netlink.h>
27 #include <linux/spinlock.h>
28 #include <linux/interrupt.h>
29 #include <linux/notifier.h>
31 #include <linux/netfilter.h>
32 #include <net/netlink.h>
33 #include <net/netfilter/nf_conntrack.h>
34 #include <net/netfilter/nf_conntrack_core.h>
35 #include <net/netfilter/nf_conntrack_expect.h>
36 #include <net/netfilter/nf_conntrack_helper.h>
37 #include <net/netfilter/nf_conntrack_l3proto.h>
38 #include <net/netfilter/nf_conntrack_l4proto.h>
39 #include <net/netfilter/nf_conntrack_tuple.h>
40 #include <net/netfilter/nf_conntrack_acct.h>
41 #ifdef CONFIG_NF_NAT_NEEDED
42 #include <net/netfilter/nf_nat_core.h>
43 #include <net/netfilter/nf_nat_protocol.h>
46 #include <linux/netfilter/nfnetlink.h>
47 #include <linux/netfilter/nfnetlink_conntrack.h>
49 MODULE_LICENSE("GPL");
51 static char __initdata version[] = "0.93";
54 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
55 const struct nf_conntrack_tuple *tuple,
56 struct nf_conntrack_l4proto *l4proto)
59 struct nlattr *nest_parms;
61 nest_parms = nla_nest_start(skb, CTA_TUPLE_PROTO | NLA_F_NESTED);
64 NLA_PUT_U8(skb, CTA_PROTO_NUM, tuple->dst.protonum);
66 if (likely(l4proto->tuple_to_nlattr))
67 ret = l4proto->tuple_to_nlattr(skb, tuple);
69 nla_nest_end(skb, nest_parms);
78 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
79 const struct nf_conntrack_tuple *tuple,
80 struct nf_conntrack_l3proto *l3proto)
83 struct nlattr *nest_parms;
85 nest_parms = nla_nest_start(skb, CTA_TUPLE_IP | NLA_F_NESTED);
89 if (likely(l3proto->tuple_to_nlattr))
90 ret = l3proto->tuple_to_nlattr(skb, tuple);
92 nla_nest_end(skb, nest_parms);
101 ctnetlink_dump_tuples(struct sk_buff *skb,
102 const struct nf_conntrack_tuple *tuple)
105 struct nf_conntrack_l3proto *l3proto;
106 struct nf_conntrack_l4proto *l4proto;
108 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
109 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
111 if (unlikely(ret < 0))
114 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
115 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
121 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
123 NLA_PUT_BE32(skb, CTA_STATUS, htonl(ct->status));
131 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
133 long timeout = (ct->timeout.expires - jiffies) / HZ;
138 NLA_PUT_BE32(skb, CTA_TIMEOUT, htonl(timeout));
146 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
148 struct nf_conntrack_l4proto *l4proto;
149 struct nlattr *nest_proto;
152 l4proto = __nf_ct_l4proto_find(nf_ct_l3num(ct), nf_ct_protonum(ct));
153 if (!l4proto->to_nlattr)
156 nest_proto = nla_nest_start(skb, CTA_PROTOINFO | NLA_F_NESTED);
158 goto nla_put_failure;
160 ret = l4proto->to_nlattr(skb, nest_proto, ct);
162 nla_nest_end(skb, nest_proto);
171 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
173 struct nlattr *nest_helper;
174 const struct nf_conn_help *help = nfct_help(ct);
175 struct nf_conntrack_helper *helper;
180 helper = rcu_dereference(help->helper);
184 nest_helper = nla_nest_start(skb, CTA_HELP | NLA_F_NESTED);
186 goto nla_put_failure;
187 NLA_PUT_STRING(skb, CTA_HELP_NAME, helper->name);
189 if (helper->to_nlattr)
190 helper->to_nlattr(skb, ct);
192 nla_nest_end(skb, nest_helper);
201 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
202 enum ip_conntrack_dir dir)
204 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
205 struct nlattr *nest_count;
206 const struct nf_conn_counter *acct;
208 acct = nf_conn_acct_find(ct);
212 nest_count = nla_nest_start(skb, type | NLA_F_NESTED);
214 goto nla_put_failure;
216 NLA_PUT_BE64(skb, CTA_COUNTERS_PACKETS,
217 cpu_to_be64(acct[dir].packets));
218 NLA_PUT_BE64(skb, CTA_COUNTERS_BYTES,
219 cpu_to_be64(acct[dir].bytes));
221 nla_nest_end(skb, nest_count);
229 #ifdef CONFIG_NF_CONNTRACK_MARK
231 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
233 NLA_PUT_BE32(skb, CTA_MARK, htonl(ct->mark));
240 #define ctnetlink_dump_mark(a, b) (0)
243 #ifdef CONFIG_NF_CONNTRACK_SECMARK
245 ctnetlink_dump_secmark(struct sk_buff *skb, const struct nf_conn *ct)
247 NLA_PUT_BE32(skb, CTA_SECMARK, htonl(ct->secmark));
254 #define ctnetlink_dump_secmark(a, b) (0)
257 #define master_tuple(ct) &(ct->master->tuplehash[IP_CT_DIR_ORIGINAL].tuple)
260 ctnetlink_dump_master(struct sk_buff *skb, const struct nf_conn *ct)
262 struct nlattr *nest_parms;
264 if (!(ct->status & IPS_EXPECTED))
267 nest_parms = nla_nest_start(skb, CTA_TUPLE_MASTER | NLA_F_NESTED);
269 goto nla_put_failure;
270 if (ctnetlink_dump_tuples(skb, master_tuple(ct)) < 0)
271 goto nla_put_failure;
272 nla_nest_end(skb, nest_parms);
280 #ifdef CONFIG_NF_NAT_NEEDED
282 dump_nat_seq_adj(struct sk_buff *skb, const struct nf_nat_seq *natseq, int type)
284 struct nlattr *nest_parms;
286 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
288 goto nla_put_failure;
290 NLA_PUT_BE32(skb, CTA_NAT_SEQ_CORRECTION_POS,
291 htonl(natseq->correction_pos));
292 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_BEFORE,
293 htonl(natseq->offset_before));
294 NLA_PUT_BE32(skb, CTA_NAT_SEQ_OFFSET_AFTER,
295 htonl(natseq->offset_after));
297 nla_nest_end(skb, nest_parms);
306 ctnetlink_dump_nat_seq_adj(struct sk_buff *skb, const struct nf_conn *ct)
308 struct nf_nat_seq *natseq;
309 struct nf_conn_nat *nat = nfct_nat(ct);
311 if (!(ct->status & IPS_SEQ_ADJUST) || !nat)
314 natseq = &nat->seq[IP_CT_DIR_ORIGINAL];
315 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_ORIG) == -1)
318 natseq = &nat->seq[IP_CT_DIR_REPLY];
319 if (dump_nat_seq_adj(skb, natseq, CTA_NAT_SEQ_ADJ_REPLY) == -1)
325 #define ctnetlink_dump_nat_seq_adj(a, b) (0)
329 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
331 NLA_PUT_BE32(skb, CTA_ID, htonl((unsigned long)ct));
339 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
341 NLA_PUT_BE32(skb, CTA_USE, htonl(atomic_read(&ct->ct_general.use)));
348 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
351 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
352 int event, int nowait,
353 const struct nf_conn *ct)
355 struct nlmsghdr *nlh;
356 struct nfgenmsg *nfmsg;
357 struct nlattr *nest_parms;
358 unsigned char *b = skb_tail_pointer(skb);
360 event |= NFNL_SUBSYS_CTNETLINK << 8;
361 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
362 nfmsg = NLMSG_DATA(nlh);
364 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
365 nfmsg->nfgen_family = nf_ct_l3num(ct);
366 nfmsg->version = NFNETLINK_V0;
369 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
371 goto nla_put_failure;
372 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
373 goto nla_put_failure;
374 nla_nest_end(skb, nest_parms);
376 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
378 goto nla_put_failure;
379 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
380 goto nla_put_failure;
381 nla_nest_end(skb, nest_parms);
383 if (ctnetlink_dump_status(skb, ct) < 0 ||
384 ctnetlink_dump_timeout(skb, ct) < 0 ||
385 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
386 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
387 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
388 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
389 ctnetlink_dump_mark(skb, ct) < 0 ||
390 ctnetlink_dump_secmark(skb, ct) < 0 ||
391 ctnetlink_dump_id(skb, ct) < 0 ||
392 ctnetlink_dump_use(skb, ct) < 0 ||
393 ctnetlink_dump_master(skb, ct) < 0 ||
394 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
395 goto nla_put_failure;
397 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
406 #ifdef CONFIG_NF_CONNTRACK_EVENTS
407 static int ctnetlink_conntrack_event(struct notifier_block *this,
408 unsigned long events, void *ptr)
410 struct nlmsghdr *nlh;
411 struct nfgenmsg *nfmsg;
412 struct nlattr *nest_parms;
413 struct nf_conn *ct = (struct nf_conn *)ptr;
417 unsigned int flags = 0, group;
419 /* ignore our fake conntrack entry */
420 if (ct == &nf_conntrack_untracked)
423 if (events & IPCT_DESTROY) {
424 type = IPCTNL_MSG_CT_DELETE;
425 group = NFNLGRP_CONNTRACK_DESTROY;
426 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
427 type = IPCTNL_MSG_CT_NEW;
428 flags = NLM_F_CREATE|NLM_F_EXCL;
429 group = NFNLGRP_CONNTRACK_NEW;
430 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
431 type = IPCTNL_MSG_CT_NEW;
432 group = NFNLGRP_CONNTRACK_UPDATE;
436 if (!nfnetlink_has_listeners(group))
439 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
445 type |= NFNL_SUBSYS_CTNETLINK << 8;
446 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
447 nfmsg = NLMSG_DATA(nlh);
449 nlh->nlmsg_flags = flags;
450 nfmsg->nfgen_family = nf_ct_l3num(ct);
451 nfmsg->version = NFNETLINK_V0;
455 nest_parms = nla_nest_start(skb, CTA_TUPLE_ORIG | NLA_F_NESTED);
457 goto nla_put_failure;
458 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
459 goto nla_put_failure;
460 nla_nest_end(skb, nest_parms);
462 nest_parms = nla_nest_start(skb, CTA_TUPLE_REPLY | NLA_F_NESTED);
464 goto nla_put_failure;
465 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
466 goto nla_put_failure;
467 nla_nest_end(skb, nest_parms);
469 if (ctnetlink_dump_id(skb, ct) < 0)
470 goto nla_put_failure;
472 if (ctnetlink_dump_status(skb, ct) < 0)
473 goto nla_put_failure;
475 if (events & IPCT_DESTROY) {
476 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
477 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
478 goto nla_put_failure;
480 if (ctnetlink_dump_timeout(skb, ct) < 0)
481 goto nla_put_failure;
483 if (events & IPCT_PROTOINFO
484 && ctnetlink_dump_protoinfo(skb, ct) < 0)
485 goto nla_put_failure;
487 if ((events & IPCT_HELPER || nfct_help(ct))
488 && ctnetlink_dump_helpinfo(skb, ct) < 0)
489 goto nla_put_failure;
491 #ifdef CONFIG_NF_CONNTRACK_SECMARK
492 if ((events & IPCT_SECMARK || ct->secmark)
493 && ctnetlink_dump_secmark(skb, ct) < 0)
494 goto nla_put_failure;
497 if (events & IPCT_RELATED &&
498 ctnetlink_dump_master(skb, ct) < 0)
499 goto nla_put_failure;
501 if (events & IPCT_NATSEQADJ &&
502 ctnetlink_dump_nat_seq_adj(skb, ct) < 0)
503 goto nla_put_failure;
506 #ifdef CONFIG_NF_CONNTRACK_MARK
507 if ((events & IPCT_MARK || ct->mark)
508 && ctnetlink_dump_mark(skb, ct) < 0)
509 goto nla_put_failure;
513 nlh->nlmsg_len = skb->tail - b;
514 nfnetlink_send(skb, 0, group, 0);
523 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
525 static int ctnetlink_done(struct netlink_callback *cb)
528 nf_ct_put((struct nf_conn *)cb->args[1]);
533 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
535 struct nf_conn *ct, *last;
536 struct nf_conntrack_tuple_hash *h;
537 struct hlist_node *n;
538 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
539 u_int8_t l3proto = nfmsg->nfgen_family;
542 last = (struct nf_conn *)cb->args[1];
543 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
545 hlist_for_each_entry_rcu(h, n, &init_net.ct.hash[cb->args[0]],
547 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
549 ct = nf_ct_tuplehash_to_ctrack(h);
550 /* Dump entries of a given L3 protocol number.
551 * If it is not specified, ie. l3proto == 0,
552 * then dump everything. */
553 if (l3proto && nf_ct_l3num(ct) != l3proto)
560 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
564 if (!atomic_inc_not_zero(&ct->ct_general.use))
566 cb->args[1] = (unsigned long)ct;
570 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
571 IPCTNL_MSG_CT_GET_CTRZERO) {
572 struct nf_conn_counter *acct;
574 acct = nf_conn_acct_find(ct);
576 memset(acct, 0, sizeof(struct nf_conn_counter[IP_CT_DIR_MAX]));
593 ctnetlink_parse_tuple_ip(struct nlattr *attr, struct nf_conntrack_tuple *tuple)
595 struct nlattr *tb[CTA_IP_MAX+1];
596 struct nf_conntrack_l3proto *l3proto;
599 nla_parse_nested(tb, CTA_IP_MAX, attr, NULL);
601 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
603 if (likely(l3proto->nlattr_to_tuple)) {
604 ret = nla_validate_nested(attr, CTA_IP_MAX,
605 l3proto->nla_policy);
607 ret = l3proto->nlattr_to_tuple(tb, tuple);
610 nf_ct_l3proto_put(l3proto);
615 static const struct nla_policy proto_nla_policy[CTA_PROTO_MAX+1] = {
616 [CTA_PROTO_NUM] = { .type = NLA_U8 },
620 ctnetlink_parse_tuple_proto(struct nlattr *attr,
621 struct nf_conntrack_tuple *tuple)
623 struct nlattr *tb[CTA_PROTO_MAX+1];
624 struct nf_conntrack_l4proto *l4proto;
627 ret = nla_parse_nested(tb, CTA_PROTO_MAX, attr, proto_nla_policy);
631 if (!tb[CTA_PROTO_NUM])
633 tuple->dst.protonum = nla_get_u8(tb[CTA_PROTO_NUM]);
635 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
637 if (likely(l4proto->nlattr_to_tuple)) {
638 ret = nla_validate_nested(attr, CTA_PROTO_MAX,
639 l4proto->nla_policy);
641 ret = l4proto->nlattr_to_tuple(tb, tuple);
644 nf_ct_l4proto_put(l4proto);
650 ctnetlink_parse_tuple(struct nlattr *cda[], struct nf_conntrack_tuple *tuple,
651 enum ctattr_tuple type, u_int8_t l3num)
653 struct nlattr *tb[CTA_TUPLE_MAX+1];
656 memset(tuple, 0, sizeof(*tuple));
658 nla_parse_nested(tb, CTA_TUPLE_MAX, cda[type], NULL);
660 if (!tb[CTA_TUPLE_IP])
663 tuple->src.l3num = l3num;
665 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP], tuple);
669 if (!tb[CTA_TUPLE_PROTO])
672 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO], tuple);
676 /* orig and expect tuples get DIR_ORIGINAL */
677 if (type == CTA_TUPLE_REPLY)
678 tuple->dst.dir = IP_CT_DIR_REPLY;
680 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
686 ctnetlink_parse_help(struct nlattr *attr, char **helper_name)
688 struct nlattr *tb[CTA_HELP_MAX+1];
690 nla_parse_nested(tb, CTA_HELP_MAX, attr, NULL);
692 if (!tb[CTA_HELP_NAME])
695 *helper_name = nla_data(tb[CTA_HELP_NAME]);
700 static const struct nla_policy ct_nla_policy[CTA_MAX+1] = {
701 [CTA_STATUS] = { .type = NLA_U32 },
702 [CTA_TIMEOUT] = { .type = NLA_U32 },
703 [CTA_MARK] = { .type = NLA_U32 },
704 [CTA_USE] = { .type = NLA_U32 },
705 [CTA_ID] = { .type = NLA_U32 },
709 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
710 struct nlmsghdr *nlh, struct nlattr *cda[])
712 struct nf_conntrack_tuple_hash *h;
713 struct nf_conntrack_tuple tuple;
715 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
716 u_int8_t u3 = nfmsg->nfgen_family;
719 if (cda[CTA_TUPLE_ORIG])
720 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
721 else if (cda[CTA_TUPLE_REPLY])
722 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
724 /* Flush the whole table */
725 nf_conntrack_flush(&init_net);
732 h = nf_conntrack_find_get(&init_net, &tuple);
736 ct = nf_ct_tuplehash_to_ctrack(h);
739 u_int32_t id = ntohl(nla_get_be32(cda[CTA_ID]));
740 if (id != (u32)(unsigned long)ct) {
753 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
754 struct nlmsghdr *nlh, struct nlattr *cda[])
756 struct nf_conntrack_tuple_hash *h;
757 struct nf_conntrack_tuple tuple;
759 struct sk_buff *skb2 = NULL;
760 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
761 u_int8_t u3 = nfmsg->nfgen_family;
764 if (nlh->nlmsg_flags & NLM_F_DUMP)
765 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
768 if (cda[CTA_TUPLE_ORIG])
769 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
770 else if (cda[CTA_TUPLE_REPLY])
771 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
778 h = nf_conntrack_find_get(&init_net, &tuple);
782 ct = nf_ct_tuplehash_to_ctrack(h);
785 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
792 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
793 IPCTNL_MSG_CT_NEW, 1, ct);
799 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
811 #ifdef CONFIG_NF_NAT_NEEDED
813 ctnetlink_parse_nat_setup(struct nf_conn *ct,
814 enum nf_nat_manip_type manip,
817 typeof(nfnetlink_parse_nat_setup_hook) parse_nat_setup;
819 parse_nat_setup = rcu_dereference(nfnetlink_parse_nat_setup_hook);
820 if (!parse_nat_setup) {
821 #ifdef CONFIG_MODULES
824 if (request_module("nf-nat-ipv4") < 0) {
831 if (nfnetlink_parse_nat_setup_hook)
837 return parse_nat_setup(ct, manip, attr);
842 ctnetlink_change_status(struct nf_conn *ct, struct nlattr *cda[])
845 unsigned int status = ntohl(nla_get_be32(cda[CTA_STATUS]));
846 d = ct->status ^ status;
848 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
852 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
853 /* SEEN_REPLY bit can only be set */
856 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
857 /* ASSURED bit can only be set */
860 /* Be careful here, modifying NAT bits can screw up things,
861 * so don't let users modify them directly if they don't pass
863 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
868 ctnetlink_change_nat(struct nf_conn *ct, struct nlattr *cda[])
870 #ifdef CONFIG_NF_NAT_NEEDED
873 if (cda[CTA_NAT_DST]) {
874 ret = ctnetlink_parse_nat_setup(ct,
880 if (cda[CTA_NAT_SRC]) {
881 ret = ctnetlink_parse_nat_setup(ct,
894 ctnetlink_change_helper(struct nf_conn *ct, struct nlattr *cda[])
896 struct nf_conntrack_helper *helper;
897 struct nf_conn_help *help = nfct_help(ct);
901 /* don't change helper of sibling connections */
905 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
909 if (!strcmp(helpname, "")) {
910 if (help && help->helper) {
911 /* we had a helper before ... */
912 nf_ct_remove_expectations(ct);
913 rcu_assign_pointer(help->helper, NULL);
919 helper = __nf_conntrack_helper_find_byname(helpname);
920 if (helper == NULL) {
921 #ifdef CONFIG_MODULES
922 spin_unlock_bh(&nf_conntrack_lock);
924 if (request_module("nfct-helper-%s", helpname) < 0) {
925 spin_lock_bh(&nf_conntrack_lock);
929 spin_lock_bh(&nf_conntrack_lock);
930 helper = __nf_conntrack_helper_find_byname(helpname);
938 if (help->helper == helper)
942 /* need to zero data of old helper */
943 memset(&help->help, 0, sizeof(help->help));
945 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
950 rcu_assign_pointer(help->helper, helper);
956 ctnetlink_change_timeout(struct nf_conn *ct, struct nlattr *cda[])
958 u_int32_t timeout = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
960 if (!del_timer(&ct->timeout))
963 ct->timeout.expires = jiffies + timeout * HZ;
964 add_timer(&ct->timeout);
970 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nlattr *cda[])
972 struct nlattr *tb[CTA_PROTOINFO_MAX+1], *attr = cda[CTA_PROTOINFO];
973 struct nf_conntrack_l4proto *l4proto;
976 nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr, NULL);
978 l4proto = nf_ct_l4proto_find_get(nf_ct_l3num(ct), nf_ct_protonum(ct));
979 if (l4proto->from_nlattr)
980 err = l4proto->from_nlattr(tb, ct);
981 nf_ct_l4proto_put(l4proto);
986 #ifdef CONFIG_NF_NAT_NEEDED
988 change_nat_seq_adj(struct nf_nat_seq *natseq, struct nlattr *attr)
990 struct nlattr *cda[CTA_NAT_SEQ_MAX+1];
992 nla_parse_nested(cda, CTA_NAT_SEQ_MAX, attr, NULL);
994 if (!cda[CTA_NAT_SEQ_CORRECTION_POS])
997 natseq->correction_pos =
998 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_CORRECTION_POS]));
1000 if (!cda[CTA_NAT_SEQ_OFFSET_BEFORE])
1003 natseq->offset_before =
1004 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_BEFORE]));
1006 if (!cda[CTA_NAT_SEQ_OFFSET_AFTER])
1009 natseq->offset_after =
1010 ntohl(nla_get_be32(cda[CTA_NAT_SEQ_OFFSET_AFTER]));
1016 ctnetlink_change_nat_seq_adj(struct nf_conn *ct, struct nlattr *cda[])
1019 struct nf_conn_nat *nat = nfct_nat(ct);
1024 if (cda[CTA_NAT_SEQ_ADJ_ORIG]) {
1025 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_ORIGINAL],
1026 cda[CTA_NAT_SEQ_ADJ_ORIG]);
1030 ct->status |= IPS_SEQ_ADJUST;
1033 if (cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1034 ret = change_nat_seq_adj(&nat->seq[IP_CT_DIR_REPLY],
1035 cda[CTA_NAT_SEQ_ADJ_REPLY]);
1039 ct->status |= IPS_SEQ_ADJUST;
1047 ctnetlink_change_conntrack(struct nf_conn *ct, struct nlattr *cda[])
1051 if (cda[CTA_HELP]) {
1052 err = ctnetlink_change_helper(ct, cda);
1057 if (cda[CTA_TIMEOUT]) {
1058 err = ctnetlink_change_timeout(ct, cda);
1063 if (cda[CTA_STATUS]) {
1064 err = ctnetlink_change_status(ct, cda);
1069 if (cda[CTA_PROTOINFO]) {
1070 err = ctnetlink_change_protoinfo(ct, cda);
1075 #if defined(CONFIG_NF_CONNTRACK_MARK)
1077 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1080 #ifdef CONFIG_NF_NAT_NEEDED
1081 if (cda[CTA_NAT_SEQ_ADJ_ORIG] || cda[CTA_NAT_SEQ_ADJ_REPLY]) {
1082 err = ctnetlink_change_nat_seq_adj(ct, cda);
1092 ctnetlink_create_conntrack(struct nlattr *cda[],
1093 struct nf_conntrack_tuple *otuple,
1094 struct nf_conntrack_tuple *rtuple,
1095 struct nf_conn *master_ct)
1099 struct nf_conntrack_helper *helper;
1101 ct = nf_conntrack_alloc(&init_net, otuple, rtuple, GFP_KERNEL);
1102 if (ct == NULL || IS_ERR(ct))
1105 if (!cda[CTA_TIMEOUT])
1107 ct->timeout.expires = ntohl(nla_get_be32(cda[CTA_TIMEOUT]));
1109 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1110 ct->status |= IPS_CONFIRMED;
1113 if (cda[CTA_HELP]) {
1116 err = ctnetlink_parse_help(cda[CTA_HELP], &helpname);
1122 helper = __nf_conntrack_helper_find_byname(helpname);
1123 if (helper == NULL) {
1125 #ifdef CONFIG_MODULES
1126 if (request_module("nfct-helper-%s", helpname) < 0) {
1132 helper = __nf_conntrack_helper_find_byname(helpname);
1143 struct nf_conn_help *help;
1145 help = nf_ct_helper_ext_add(ct, GFP_ATOMIC);
1152 /* not in hash table yet so not strictly necessary */
1153 rcu_assign_pointer(help->helper, helper);
1156 /* try an implicit helper assignation */
1157 err = __nf_ct_try_assign_helper(ct, GFP_ATOMIC);
1164 if (cda[CTA_STATUS]) {
1165 err = ctnetlink_change_status(ct, cda);
1172 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1173 err = ctnetlink_change_nat(ct, cda);
1180 if (cda[CTA_PROTOINFO]) {
1181 err = ctnetlink_change_protoinfo(ct, cda);
1188 nf_ct_acct_ext_add(ct, GFP_KERNEL);
1190 #if defined(CONFIG_NF_CONNTRACK_MARK)
1192 ct->mark = ntohl(nla_get_be32(cda[CTA_MARK]));
1195 /* setup master conntrack: this is a confirmed expectation */
1197 __set_bit(IPS_EXPECTED_BIT, &ct->status);
1198 ct->master = master_ct;
1201 add_timer(&ct->timeout);
1202 nf_conntrack_hash_insert(ct);
1208 nf_conntrack_free(ct);
1213 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1214 struct nlmsghdr *nlh, struct nlattr *cda[])
1216 struct nf_conntrack_tuple otuple, rtuple;
1217 struct nf_conntrack_tuple_hash *h = NULL;
1218 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1219 u_int8_t u3 = nfmsg->nfgen_family;
1222 if (cda[CTA_TUPLE_ORIG]) {
1223 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1228 if (cda[CTA_TUPLE_REPLY]) {
1229 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1234 spin_lock_bh(&nf_conntrack_lock);
1235 if (cda[CTA_TUPLE_ORIG])
1236 h = __nf_conntrack_find(&init_net, &otuple);
1237 else if (cda[CTA_TUPLE_REPLY])
1238 h = __nf_conntrack_find(&init_net, &rtuple);
1241 struct nf_conntrack_tuple master;
1242 struct nf_conntrack_tuple_hash *master_h = NULL;
1243 struct nf_conn *master_ct = NULL;
1245 if (cda[CTA_TUPLE_MASTER]) {
1246 err = ctnetlink_parse_tuple(cda,
1253 master_h = __nf_conntrack_find(&init_net, &master);
1254 if (master_h == NULL) {
1258 master_ct = nf_ct_tuplehash_to_ctrack(master_h);
1259 nf_conntrack_get(&master_ct->ct_general);
1262 spin_unlock_bh(&nf_conntrack_lock);
1264 if (nlh->nlmsg_flags & NLM_F_CREATE)
1265 err = ctnetlink_create_conntrack(cda,
1269 if (err < 0 && master_ct)
1270 nf_ct_put(master_ct);
1274 /* implicit 'else' */
1276 /* We manipulate the conntrack inside the global conntrack table lock,
1277 * so there's no need to increase the refcount */
1279 if (!(nlh->nlmsg_flags & NLM_F_EXCL)) {
1280 /* we only allow nat config for new conntracks */
1281 if (cda[CTA_NAT_SRC] || cda[CTA_NAT_DST]) {
1285 /* can't link an existing conntrack to a master */
1286 if (cda[CTA_TUPLE_MASTER]) {
1290 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h),
1295 spin_unlock_bh(&nf_conntrack_lock);
1299 /***********************************************************************
1301 ***********************************************************************/
1304 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1305 const struct nf_conntrack_tuple *tuple,
1306 enum ctattr_expect type)
1308 struct nlattr *nest_parms;
1310 nest_parms = nla_nest_start(skb, type | NLA_F_NESTED);
1312 goto nla_put_failure;
1313 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1314 goto nla_put_failure;
1315 nla_nest_end(skb, nest_parms);
1324 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1325 const struct nf_conntrack_tuple *tuple,
1326 const struct nf_conntrack_tuple_mask *mask)
1329 struct nf_conntrack_l3proto *l3proto;
1330 struct nf_conntrack_l4proto *l4proto;
1331 struct nf_conntrack_tuple m;
1332 struct nlattr *nest_parms;
1334 memset(&m, 0xFF, sizeof(m));
1335 m.src.u.all = mask->src.u.all;
1336 memcpy(&m.src.u3, &mask->src.u3, sizeof(m.src.u3));
1338 nest_parms = nla_nest_start(skb, CTA_EXPECT_MASK | NLA_F_NESTED);
1340 goto nla_put_failure;
1342 l3proto = __nf_ct_l3proto_find(tuple->src.l3num);
1343 ret = ctnetlink_dump_tuples_ip(skb, &m, l3proto);
1345 if (unlikely(ret < 0))
1346 goto nla_put_failure;
1348 l4proto = __nf_ct_l4proto_find(tuple->src.l3num, tuple->dst.protonum);
1349 ret = ctnetlink_dump_tuples_proto(skb, &m, l4proto);
1350 if (unlikely(ret < 0))
1351 goto nla_put_failure;
1353 nla_nest_end(skb, nest_parms);
1362 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1363 const struct nf_conntrack_expect *exp)
1365 struct nf_conn *master = exp->master;
1366 long timeout = (exp->timeout.expires - jiffies) / HZ;
1371 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1372 goto nla_put_failure;
1373 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1374 goto nla_put_failure;
1375 if (ctnetlink_exp_dump_tuple(skb,
1376 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1377 CTA_EXPECT_MASTER) < 0)
1378 goto nla_put_failure;
1380 NLA_PUT_BE32(skb, CTA_EXPECT_TIMEOUT, htonl(timeout));
1381 NLA_PUT_BE32(skb, CTA_EXPECT_ID, htonl((unsigned long)exp));
1390 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1393 const struct nf_conntrack_expect *exp)
1395 struct nlmsghdr *nlh;
1396 struct nfgenmsg *nfmsg;
1397 unsigned char *b = skb_tail_pointer(skb);
1399 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1400 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1401 nfmsg = NLMSG_DATA(nlh);
1403 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1404 nfmsg->nfgen_family = exp->tuple.src.l3num;
1405 nfmsg->version = NFNETLINK_V0;
1408 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1409 goto nla_put_failure;
1411 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
1420 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1421 static int ctnetlink_expect_event(struct notifier_block *this,
1422 unsigned long events, void *ptr)
1424 struct nlmsghdr *nlh;
1425 struct nfgenmsg *nfmsg;
1426 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1427 struct sk_buff *skb;
1432 if (events & IPEXP_NEW) {
1433 type = IPCTNL_MSG_EXP_NEW;
1434 flags = NLM_F_CREATE|NLM_F_EXCL;
1438 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1441 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1447 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1448 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1449 nfmsg = NLMSG_DATA(nlh);
1451 nlh->nlmsg_flags = flags;
1452 nfmsg->nfgen_family = exp->tuple.src.l3num;
1453 nfmsg->version = NFNETLINK_V0;
1457 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1458 goto nla_put_failure;
1461 nlh->nlmsg_len = skb->tail - b;
1462 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1472 static int ctnetlink_exp_done(struct netlink_callback *cb)
1475 nf_ct_expect_put((struct nf_conntrack_expect *)cb->args[1]);
1480 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1482 struct net *net = &init_net;
1483 struct nf_conntrack_expect *exp, *last;
1484 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1485 struct hlist_node *n;
1486 u_int8_t l3proto = nfmsg->nfgen_family;
1489 last = (struct nf_conntrack_expect *)cb->args[1];
1490 for (; cb->args[0] < nf_ct_expect_hsize; cb->args[0]++) {
1492 hlist_for_each_entry(exp, n, &net->ct.expect_hash[cb->args[0]],
1494 if (l3proto && exp->tuple.src.l3num != l3proto)
1501 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1505 if (!atomic_inc_not_zero(&exp->use))
1507 cb->args[1] = (unsigned long)exp;
1519 nf_ct_expect_put(last);
1524 static const struct nla_policy exp_nla_policy[CTA_EXPECT_MAX+1] = {
1525 [CTA_EXPECT_TIMEOUT] = { .type = NLA_U32 },
1526 [CTA_EXPECT_ID] = { .type = NLA_U32 },
1530 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1531 struct nlmsghdr *nlh, struct nlattr *cda[])
1533 struct nf_conntrack_tuple tuple;
1534 struct nf_conntrack_expect *exp;
1535 struct sk_buff *skb2;
1536 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1537 u_int8_t u3 = nfmsg->nfgen_family;
1540 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1541 return netlink_dump_start(ctnl, skb, nlh,
1542 ctnetlink_exp_dump_table,
1543 ctnetlink_exp_done);
1546 if (cda[CTA_EXPECT_MASTER])
1547 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1554 exp = nf_ct_expect_find_get(&init_net, &tuple);
1558 if (cda[CTA_EXPECT_ID]) {
1559 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1560 if (ntohl(id) != (u32)(unsigned long)exp) {
1561 nf_ct_expect_put(exp);
1567 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1572 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1573 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1579 nf_ct_expect_put(exp);
1581 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1586 nf_ct_expect_put(exp);
1591 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1592 struct nlmsghdr *nlh, struct nlattr *cda[])
1594 struct nf_conntrack_expect *exp;
1595 struct nf_conntrack_tuple tuple;
1596 struct nf_conntrack_helper *h;
1597 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1598 struct hlist_node *n, *next;
1599 u_int8_t u3 = nfmsg->nfgen_family;
1603 if (cda[CTA_EXPECT_TUPLE]) {
1604 /* delete a single expect by tuple */
1605 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1609 /* bump usage count to 2 */
1610 exp = nf_ct_expect_find_get(&init_net, &tuple);
1614 if (cda[CTA_EXPECT_ID]) {
1615 __be32 id = nla_get_be32(cda[CTA_EXPECT_ID]);
1616 if (ntohl(id) != (u32)(unsigned long)exp) {
1617 nf_ct_expect_put(exp);
1622 /* after list removal, usage count == 1 */
1623 nf_ct_unexpect_related(exp);
1624 /* have to put what we 'get' above.
1625 * after this line usage count == 0 */
1626 nf_ct_expect_put(exp);
1627 } else if (cda[CTA_EXPECT_HELP_NAME]) {
1628 char *name = nla_data(cda[CTA_EXPECT_HELP_NAME]);
1629 struct nf_conn_help *m_help;
1631 /* delete all expectations for this helper */
1632 spin_lock_bh(&nf_conntrack_lock);
1633 h = __nf_conntrack_helper_find_byname(name);
1635 spin_unlock_bh(&nf_conntrack_lock);
1638 for (i = 0; i < nf_ct_expect_hsize; i++) {
1639 hlist_for_each_entry_safe(exp, n, next,
1640 &init_net.ct.expect_hash[i],
1642 m_help = nfct_help(exp->master);
1643 if (m_help->helper == h
1644 && del_timer(&exp->timeout)) {
1645 nf_ct_unlink_expect(exp);
1646 nf_ct_expect_put(exp);
1650 spin_unlock_bh(&nf_conntrack_lock);
1652 /* This basically means we have to flush everything*/
1653 spin_lock_bh(&nf_conntrack_lock);
1654 for (i = 0; i < nf_ct_expect_hsize; i++) {
1655 hlist_for_each_entry_safe(exp, n, next,
1656 &init_net.ct.expect_hash[i],
1658 if (del_timer(&exp->timeout)) {
1659 nf_ct_unlink_expect(exp);
1660 nf_ct_expect_put(exp);
1664 spin_unlock_bh(&nf_conntrack_lock);
1670 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nlattr *cda[])
1676 ctnetlink_create_expect(struct nlattr *cda[], u_int8_t u3)
1678 struct nf_conntrack_tuple tuple, mask, master_tuple;
1679 struct nf_conntrack_tuple_hash *h = NULL;
1680 struct nf_conntrack_expect *exp;
1682 struct nf_conn_help *help;
1685 /* caller guarantees that those three CTA_EXPECT_* exist */
1686 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1689 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1692 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1696 /* Look for master conntrack of this expectation */
1697 h = nf_conntrack_find_get(&init_net, &master_tuple);
1700 ct = nf_ct_tuplehash_to_ctrack(h);
1701 help = nfct_help(ct);
1703 if (!help || !help->helper) {
1704 /* such conntrack hasn't got any helper, abort */
1709 exp = nf_ct_expect_alloc(ct);
1715 exp->expectfn = NULL;
1719 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1720 memcpy(&exp->mask.src.u3, &mask.src.u3, sizeof(exp->mask.src.u3));
1721 exp->mask.src.u.all = mask.src.u.all;
1723 err = nf_ct_expect_related(exp);
1724 nf_ct_expect_put(exp);
1727 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1732 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1733 struct nlmsghdr *nlh, struct nlattr *cda[])
1735 struct nf_conntrack_tuple tuple;
1736 struct nf_conntrack_expect *exp;
1737 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1738 u_int8_t u3 = nfmsg->nfgen_family;
1741 if (!cda[CTA_EXPECT_TUPLE]
1742 || !cda[CTA_EXPECT_MASK]
1743 || !cda[CTA_EXPECT_MASTER])
1746 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1750 spin_lock_bh(&nf_conntrack_lock);
1751 exp = __nf_ct_expect_find(&init_net, &tuple);
1754 spin_unlock_bh(&nf_conntrack_lock);
1756 if (nlh->nlmsg_flags & NLM_F_CREATE)
1757 err = ctnetlink_create_expect(cda, u3);
1762 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1763 err = ctnetlink_change_expect(exp, cda);
1764 spin_unlock_bh(&nf_conntrack_lock);
1769 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1770 static struct notifier_block ctnl_notifier = {
1771 .notifier_call = ctnetlink_conntrack_event,
1774 static struct notifier_block ctnl_notifier_exp = {
1775 .notifier_call = ctnetlink_expect_event,
1779 static const struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1780 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1781 .attr_count = CTA_MAX,
1782 .policy = ct_nla_policy },
1783 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1784 .attr_count = CTA_MAX,
1785 .policy = ct_nla_policy },
1786 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1787 .attr_count = CTA_MAX,
1788 .policy = ct_nla_policy },
1789 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1790 .attr_count = CTA_MAX,
1791 .policy = ct_nla_policy },
1794 static const struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1795 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1796 .attr_count = CTA_EXPECT_MAX,
1797 .policy = exp_nla_policy },
1798 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1799 .attr_count = CTA_EXPECT_MAX,
1800 .policy = exp_nla_policy },
1801 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1802 .attr_count = CTA_EXPECT_MAX,
1803 .policy = exp_nla_policy },
1806 static const struct nfnetlink_subsystem ctnl_subsys = {
1807 .name = "conntrack",
1808 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1809 .cb_count = IPCTNL_MSG_MAX,
1813 static const struct nfnetlink_subsystem ctnl_exp_subsys = {
1814 .name = "conntrack_expect",
1815 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1816 .cb_count = IPCTNL_MSG_EXP_MAX,
1820 MODULE_ALIAS("ip_conntrack_netlink");
1821 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1822 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1824 static int __init ctnetlink_init(void)
1828 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1829 ret = nfnetlink_subsys_register(&ctnl_subsys);
1831 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1835 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1837 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1838 goto err_unreg_subsys;
1841 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1842 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1844 printk("ctnetlink_init: cannot register notifier.\n");
1845 goto err_unreg_exp_subsys;
1848 ret = nf_ct_expect_register_notifier(&ctnl_notifier_exp);
1850 printk("ctnetlink_init: cannot expect register notifier.\n");
1851 goto err_unreg_notifier;
1857 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1859 nf_conntrack_unregister_notifier(&ctnl_notifier);
1860 err_unreg_exp_subsys:
1861 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1864 nfnetlink_subsys_unregister(&ctnl_subsys);
1869 static void __exit ctnetlink_exit(void)
1871 printk("ctnetlink: unregistering from nfnetlink.\n");
1873 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1874 nf_ct_expect_unregister_notifier(&ctnl_notifier_exp);
1875 nf_conntrack_unregister_notifier(&ctnl_notifier);
1878 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1879 nfnetlink_subsys_unregister(&ctnl_subsys);
1883 module_init(ctnetlink_init);
1884 module_exit(ctnetlink_exit);