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-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
9 * I've reworked this stuff to use attributes instead of conntrack
10 * structures. 5.44 am. I need more tea. --pablo 05/07/11.
12 * Initial connection tracking via netlink development funded and
13 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
15 * Further development of this code funded by Astaro AG (http://www.astaro.com)
17 * This software may be used and distributed according to the terms
18 * of the GNU General Public License, incorporated herein by reference.
20 * Derived from ip_conntrack_netlink.c: Port by Pablo Neira Ayuso (05/11/14)
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/types.h>
27 #include <linux/timer.h>
28 #include <linux/skbuff.h>
29 #include <linux/errno.h>
30 #include <linux/netlink.h>
31 #include <linux/spinlock.h>
32 #include <linux/interrupt.h>
33 #include <linux/notifier.h>
35 #include <linux/netfilter.h>
36 #include <net/netfilter/nf_conntrack.h>
37 #include <net/netfilter/nf_conntrack_core.h>
38 #include <net/netfilter/nf_conntrack_helper.h>
39 #include <net/netfilter/nf_conntrack_l3proto.h>
40 #include <net/netfilter/nf_conntrack_protocol.h>
41 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
43 #include <linux/netfilter/nfnetlink.h>
44 #include <linux/netfilter/nfnetlink_conntrack.h>
46 MODULE_LICENSE("GPL");
48 static char __initdata version[] = "0.93";
53 #define DEBUGP(format, args...)
58 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
59 const struct nf_conntrack_tuple *tuple,
60 struct nf_conntrack_protocol *proto)
63 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
65 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
67 if (likely(proto->tuple_to_nfattr))
68 ret = proto->tuple_to_nfattr(skb, tuple);
70 NFA_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 nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
86 if (likely(l3proto->tuple_to_nfattr))
87 ret = l3proto->tuple_to_nfattr(skb, tuple);
89 NFA_NEST_END(skb, nest_parms);
98 ctnetlink_dump_tuples(struct sk_buff *skb,
99 const struct nf_conntrack_tuple *tuple)
102 struct nf_conntrack_l3proto *l3proto;
103 struct nf_conntrack_protocol *proto;
105 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
106 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
107 nf_ct_l3proto_put(l3proto);
109 if (unlikely(ret < 0))
112 proto = nf_ct_proto_find_get(tuple->src.l3num, tuple->dst.protonum);
113 ret = ctnetlink_dump_tuples_proto(skb, tuple, proto);
114 nf_ct_proto_put(proto);
120 ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
122 u_int32_t status = htonl((u_int32_t) ct->status);
123 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
131 ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
133 long timeout_l = ct->timeout.expires - jiffies;
139 timeout = htonl(timeout_l / HZ);
141 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
149 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
151 struct nf_conntrack_protocol *proto = nf_ct_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
152 struct nfattr *nest_proto;
155 if (!proto->to_nfattr) {
156 nf_ct_proto_put(proto);
160 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
162 ret = proto->to_nfattr(skb, nest_proto, ct);
164 nf_ct_proto_put(proto);
166 NFA_NEST_END(skb, nest_proto);
175 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
177 struct nfattr *nest_helper;
178 const struct nf_conn_help *help = nfct_help(ct);
180 if (!help || !help->helper)
183 nest_helper = NFA_NEST(skb, CTA_HELP);
184 NFA_PUT(skb, CTA_HELP_NAME, strlen(help->helper->name), help->helper->name);
186 if (help->helper->to_nfattr)
187 help->helper->to_nfattr(skb, ct);
189 NFA_NEST_END(skb, nest_helper);
197 #ifdef CONFIG_NF_CT_ACCT
199 ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
200 enum ip_conntrack_dir dir)
202 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
203 struct nfattr *nest_count = NFA_NEST(skb, type);
206 tmp = htonl(ct->counters[dir].packets);
207 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
209 tmp = htonl(ct->counters[dir].bytes);
210 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
212 NFA_NEST_END(skb, nest_count);
220 #define ctnetlink_dump_counters(a, b, c) (0)
223 #ifdef CONFIG_NF_CONNTRACK_MARK
225 ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
227 u_int32_t mark = htonl(ct->mark);
229 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
236 #define ctnetlink_dump_mark(a, b) (0)
240 ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
242 u_int32_t id = htonl(ct->id);
243 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
251 ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
253 u_int32_t use = htonl(atomic_read(&ct->ct_general.use));
255 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
262 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
265 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
266 int event, int nowait,
267 const struct nf_conn *ct)
269 struct nlmsghdr *nlh;
270 struct nfgenmsg *nfmsg;
271 struct nfattr *nest_parms;
276 event |= NFNL_SUBSYS_CTNETLINK << 8;
277 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
278 nfmsg = NLMSG_DATA(nlh);
280 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
281 nfmsg->nfgen_family =
282 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
283 nfmsg->version = NFNETLINK_V0;
286 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
287 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
289 NFA_NEST_END(skb, nest_parms);
291 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
292 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
294 NFA_NEST_END(skb, nest_parms);
296 if (ctnetlink_dump_status(skb, ct) < 0 ||
297 ctnetlink_dump_timeout(skb, ct) < 0 ||
298 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
299 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
300 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
301 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
302 ctnetlink_dump_mark(skb, ct) < 0 ||
303 ctnetlink_dump_id(skb, ct) < 0 ||
304 ctnetlink_dump_use(skb, ct) < 0)
307 nlh->nlmsg_len = skb->tail - b;
312 skb_trim(skb, b - skb->data);
316 #ifdef CONFIG_NF_CONNTRACK_EVENTS
317 static int ctnetlink_conntrack_event(struct notifier_block *this,
318 unsigned long events, void *ptr)
320 struct nlmsghdr *nlh;
321 struct nfgenmsg *nfmsg;
322 struct nfattr *nest_parms;
323 struct nf_conn *ct = (struct nf_conn *)ptr;
327 unsigned int flags = 0, group;
329 /* ignore our fake conntrack entry */
330 if (ct == &nf_conntrack_untracked)
333 if (events & IPCT_DESTROY) {
334 type = IPCTNL_MSG_CT_DELETE;
335 group = NFNLGRP_CONNTRACK_DESTROY;
336 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
337 type = IPCTNL_MSG_CT_NEW;
338 flags = NLM_F_CREATE|NLM_F_EXCL;
339 /* dump everything */
341 group = NFNLGRP_CONNTRACK_NEW;
342 } else if (events & (IPCT_STATUS |
347 type = IPCTNL_MSG_CT_NEW;
348 group = NFNLGRP_CONNTRACK_UPDATE;
352 if (!nfnetlink_has_listeners(group))
355 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
361 type |= NFNL_SUBSYS_CTNETLINK << 8;
362 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
363 nfmsg = NLMSG_DATA(nlh);
365 nlh->nlmsg_flags = flags;
366 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
367 nfmsg->version = NFNETLINK_V0;
370 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
371 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
373 NFA_NEST_END(skb, nest_parms);
375 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
376 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
378 NFA_NEST_END(skb, nest_parms);
380 /* NAT stuff is now a status flag */
381 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
382 && ctnetlink_dump_status(skb, ct) < 0)
384 if (events & IPCT_REFRESH
385 && ctnetlink_dump_timeout(skb, ct) < 0)
387 if (events & IPCT_PROTOINFO
388 && ctnetlink_dump_protoinfo(skb, ct) < 0)
390 if (events & IPCT_HELPINFO
391 && ctnetlink_dump_helpinfo(skb, ct) < 0)
394 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
395 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
398 if (events & IPCT_MARK
399 && ctnetlink_dump_mark(skb, ct) < 0)
402 nlh->nlmsg_len = skb->tail - b;
403 nfnetlink_send(skb, 0, group, 0);
411 #endif /* CONFIG_NF_CONNTRACK_EVENTS */
413 static int ctnetlink_done(struct netlink_callback *cb)
416 nf_ct_put((struct nf_conn *)cb->args[1]);
417 DEBUGP("entered %s\n", __FUNCTION__);
421 #define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
424 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
426 struct nf_conn *ct, *last;
427 struct nf_conntrack_tuple_hash *h;
429 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
430 u_int8_t l3proto = nfmsg->nfgen_family;
432 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
435 read_lock_bh(&nf_conntrack_lock);
436 last = (struct nf_conn *)cb->args[1];
437 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
439 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
440 h = (struct nf_conntrack_tuple_hash *) i;
441 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
443 ct = nf_ct_tuplehash_to_ctrack(h);
444 /* Dump entries of a given L3 protocol number.
445 * If it is not specified, ie. l3proto == 0,
446 * then dump everything. */
447 if (l3proto && L3PROTO(ct) != l3proto)
454 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
458 nf_conntrack_get(&ct->ct_general);
459 cb->args[1] = (unsigned long)ct;
469 read_unlock_bh(&nf_conntrack_lock);
473 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
477 #ifdef CONFIG_NF_CT_ACCT
479 ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
481 struct nf_conn *ct = NULL;
482 struct nf_conntrack_tuple_hash *h;
484 u_int32_t *id = (u_int32_t *) &cb->args[1];
485 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
486 u_int8_t l3proto = nfmsg->nfgen_family;
488 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
491 write_lock_bh(&nf_conntrack_lock);
492 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++, *id = 0) {
493 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
494 h = (struct nf_conntrack_tuple_hash *) i;
495 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
497 ct = nf_ct_tuplehash_to_ctrack(h);
498 if (l3proto && L3PROTO(ct) != l3proto)
502 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
509 memset(&ct->counters, 0, sizeof(ct->counters));
513 write_unlock_bh(&nf_conntrack_lock);
515 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
522 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct nf_conntrack_tuple *tuple)
524 struct nfattr *tb[CTA_IP_MAX];
525 struct nf_conntrack_l3proto *l3proto;
528 DEBUGP("entered %s\n", __FUNCTION__);
530 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
532 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
534 if (likely(l3proto->nfattr_to_tuple))
535 ret = l3proto->nfattr_to_tuple(tb, tuple);
537 nf_ct_l3proto_put(l3proto);
544 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
545 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
549 ctnetlink_parse_tuple_proto(struct nfattr *attr,
550 struct nf_conntrack_tuple *tuple)
552 struct nfattr *tb[CTA_PROTO_MAX];
553 struct nf_conntrack_protocol *proto;
556 DEBUGP("entered %s\n", __FUNCTION__);
558 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
560 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
563 if (!tb[CTA_PROTO_NUM-1])
565 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
567 proto = nf_ct_proto_find_get(tuple->src.l3num, tuple->dst.protonum);
569 if (likely(proto->nfattr_to_tuple))
570 ret = proto->nfattr_to_tuple(tb, tuple);
572 nf_ct_proto_put(proto);
578 ctnetlink_parse_tuple(struct nfattr *cda[], struct nf_conntrack_tuple *tuple,
579 enum ctattr_tuple type, u_int8_t l3num)
581 struct nfattr *tb[CTA_TUPLE_MAX];
584 DEBUGP("entered %s\n", __FUNCTION__);
586 memset(tuple, 0, sizeof(*tuple));
588 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
590 if (!tb[CTA_TUPLE_IP-1])
593 tuple->src.l3num = l3num;
595 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
599 if (!tb[CTA_TUPLE_PROTO-1])
602 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
606 /* orig and expect tuples get DIR_ORIGINAL */
607 if (type == CTA_TUPLE_REPLY)
608 tuple->dst.dir = IP_CT_DIR_REPLY;
610 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
612 NF_CT_DUMP_TUPLE(tuple);
619 #ifdef CONFIG_IP_NF_NAT_NEEDED
620 static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
621 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
622 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
625 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
626 const struct nf_conn *ct,
627 struct ip_nat_range *range)
629 struct nfattr *tb[CTA_PROTONAT_MAX];
630 struct ip_nat_protocol *npt;
632 DEBUGP("entered %s\n", __FUNCTION__);
634 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
636 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
639 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
641 if (!npt->nfattr_to_range) {
642 ip_nat_proto_put(npt);
646 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
647 if (npt->nfattr_to_range(tb, range) > 0)
648 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
650 ip_nat_proto_put(npt);
656 static const size_t cta_min_nat[CTA_NAT_MAX] = {
657 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
658 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
662 ctnetlink_parse_nat(struct nfattr *nat,
663 const struct nf_conn *ct, struct ip_nat_range *range)
665 struct nfattr *tb[CTA_NAT_MAX];
668 DEBUGP("entered %s\n", __FUNCTION__);
670 memset(range, 0, sizeof(*range));
672 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
674 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
677 if (tb[CTA_NAT_MINIP-1])
678 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
680 if (!tb[CTA_NAT_MAXIP-1])
681 range->max_ip = range->min_ip;
683 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
686 range->flags |= IP_NAT_RANGE_MAP_IPS;
688 if (!tb[CTA_NAT_PROTO-1])
691 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
701 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
703 struct nfattr *tb[CTA_HELP_MAX];
705 DEBUGP("entered %s\n", __FUNCTION__);
707 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
709 if (!tb[CTA_HELP_NAME-1])
712 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
717 static const size_t cta_min[CTA_MAX] = {
718 [CTA_STATUS-1] = sizeof(u_int32_t),
719 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
720 [CTA_MARK-1] = sizeof(u_int32_t),
721 [CTA_USE-1] = sizeof(u_int32_t),
722 [CTA_ID-1] = sizeof(u_int32_t)
726 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
727 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
729 struct nf_conntrack_tuple_hash *h;
730 struct nf_conntrack_tuple tuple;
732 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
733 u_int8_t u3 = nfmsg->nfgen_family;
736 DEBUGP("entered %s\n", __FUNCTION__);
738 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
741 if (cda[CTA_TUPLE_ORIG-1])
742 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
743 else if (cda[CTA_TUPLE_REPLY-1])
744 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
746 /* Flush the whole table */
747 nf_conntrack_flush();
754 h = nf_conntrack_find_get(&tuple, NULL);
756 DEBUGP("tuple not found in conntrack hash\n");
760 ct = nf_ct_tuplehash_to_ctrack(h);
763 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
769 if (del_timer(&ct->timeout))
770 ct->timeout.function((unsigned long)ct);
779 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
780 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
782 struct nf_conntrack_tuple_hash *h;
783 struct nf_conntrack_tuple tuple;
785 struct sk_buff *skb2 = NULL;
786 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
787 u_int8_t u3 = nfmsg->nfgen_family;
790 DEBUGP("entered %s\n", __FUNCTION__);
792 if (nlh->nlmsg_flags & NLM_F_DUMP) {
795 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
796 IPCTNL_MSG_CT_GET_CTRZERO) {
797 #ifdef CONFIG_NF_CT_ACCT
798 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
799 ctnetlink_dump_table_w,
800 ctnetlink_done)) != 0)
806 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
807 ctnetlink_dump_table,
808 ctnetlink_done)) != 0)
812 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
819 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
822 if (cda[CTA_TUPLE_ORIG-1])
823 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
824 else if (cda[CTA_TUPLE_REPLY-1])
825 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
832 h = nf_conntrack_find_get(&tuple, NULL);
834 DEBUGP("tuple not found in conntrack hash");
837 DEBUGP("tuple found\n");
838 ct = nf_ct_tuplehash_to_ctrack(h);
841 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
846 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
848 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
849 IPCTNL_MSG_CT_NEW, 1, ct);
854 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
868 ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
871 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
872 d = ct->status ^ status;
874 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
878 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
879 /* SEEN_REPLY bit can only be set */
883 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
884 /* ASSURED bit can only be set */
887 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
888 #ifndef CONFIG_IP_NF_NAT_NEEDED
891 struct ip_nat_range range;
893 if (cda[CTA_NAT_DST-1]) {
894 if (ctnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
897 if (ip_nat_initialized(ct,
898 HOOK2MANIP(NF_IP_PRE_ROUTING)))
900 ip_nat_setup_info(ct, &range, hooknum);
902 if (cda[CTA_NAT_SRC-1]) {
903 if (ctnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
906 if (ip_nat_initialized(ct,
907 HOOK2MANIP(NF_IP_POST_ROUTING)))
909 ip_nat_setup_info(ct, &range, hooknum);
914 /* Be careful here, modifying NAT bits can screw up things,
915 * so don't let users modify them directly if they don't pass
917 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
923 ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
925 struct nf_conntrack_helper *helper;
926 struct nf_conn_help *help = nfct_help(ct);
930 DEBUGP("entered %s\n", __FUNCTION__);
933 /* FIXME: we need to reallocate and rehash */
937 /* don't change helper of sibling connections */
941 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
945 helper = __nf_conntrack_helper_find_byname(helpname);
947 if (!strcmp(helpname, ""))
955 /* we had a helper before ... */
956 nf_ct_remove_expectations(ct);
959 /* need to zero data of old helper */
960 memset(&help->help, 0, sizeof(help->help));
964 help->helper = helper;
970 ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[])
972 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
974 if (!del_timer(&ct->timeout))
977 ct->timeout.expires = jiffies + timeout * HZ;
978 add_timer(&ct->timeout);
984 ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[])
986 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
987 struct nf_conntrack_protocol *proto;
988 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
989 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
992 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
994 proto = nf_ct_proto_find_get(l3num, npt);
996 if (proto->from_nfattr)
997 err = proto->from_nfattr(tb, ct);
998 nf_ct_proto_put(proto);
1004 ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
1008 DEBUGP("entered %s\n", __FUNCTION__);
1010 if (cda[CTA_HELP-1]) {
1011 err = ctnetlink_change_helper(ct, cda);
1016 if (cda[CTA_TIMEOUT-1]) {
1017 err = ctnetlink_change_timeout(ct, cda);
1022 if (cda[CTA_STATUS-1]) {
1023 err = ctnetlink_change_status(ct, cda);
1028 if (cda[CTA_PROTOINFO-1]) {
1029 err = ctnetlink_change_protoinfo(ct, cda);
1034 #if defined(CONFIG_NF_CONNTRACK_MARK)
1035 if (cda[CTA_MARK-1])
1036 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1039 DEBUGP("all done\n");
1044 ctnetlink_create_conntrack(struct nfattr *cda[],
1045 struct nf_conntrack_tuple *otuple,
1046 struct nf_conntrack_tuple *rtuple)
1051 DEBUGP("entered %s\n", __FUNCTION__);
1053 ct = nf_conntrack_alloc(otuple, rtuple);
1054 if (ct == NULL || IS_ERR(ct))
1057 if (!cda[CTA_TIMEOUT-1])
1059 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1061 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1062 ct->status |= IPS_CONFIRMED;
1064 err = ctnetlink_change_status(ct, cda);
1068 if (cda[CTA_PROTOINFO-1]) {
1069 err = ctnetlink_change_protoinfo(ct, cda);
1074 #if defined(CONFIG_NF_CONNTRACK_MARK)
1075 if (cda[CTA_MARK-1])
1076 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1079 add_timer(&ct->timeout);
1080 nf_conntrack_hash_insert(ct);
1082 DEBUGP("conntrack with id %u inserted\n", ct->id);
1086 nf_conntrack_free(ct);
1091 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1092 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1094 struct nf_conntrack_tuple otuple, rtuple;
1095 struct nf_conntrack_tuple_hash *h = NULL;
1096 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1097 u_int8_t u3 = nfmsg->nfgen_family;
1100 DEBUGP("entered %s\n", __FUNCTION__);
1102 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1105 if (cda[CTA_TUPLE_ORIG-1]) {
1106 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1111 if (cda[CTA_TUPLE_REPLY-1]) {
1112 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1117 write_lock_bh(&nf_conntrack_lock);
1118 if (cda[CTA_TUPLE_ORIG-1])
1119 h = __nf_conntrack_find(&otuple, NULL);
1120 else if (cda[CTA_TUPLE_REPLY-1])
1121 h = __nf_conntrack_find(&rtuple, NULL);
1124 write_unlock_bh(&nf_conntrack_lock);
1125 DEBUGP("no such conntrack, create new\n");
1127 if (nlh->nlmsg_flags & NLM_F_CREATE)
1128 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1131 /* implicit 'else' */
1133 /* we only allow nat config for new conntracks */
1134 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
1139 /* We manipulate the conntrack inside the global conntrack table lock,
1140 * so there's no need to increase the refcount */
1141 DEBUGP("conntrack found\n");
1143 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1144 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1147 write_unlock_bh(&nf_conntrack_lock);
1151 /***********************************************************************
1153 ***********************************************************************/
1156 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1157 const struct nf_conntrack_tuple *tuple,
1158 enum ctattr_expect type)
1160 struct nfattr *nest_parms = NFA_NEST(skb, type);
1162 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1163 goto nfattr_failure;
1165 NFA_NEST_END(skb, nest_parms);
1174 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1175 const struct nf_conntrack_tuple *tuple,
1176 const struct nf_conntrack_tuple *mask)
1179 struct nf_conntrack_l3proto *l3proto;
1180 struct nf_conntrack_protocol *proto;
1181 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1183 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1184 ret = ctnetlink_dump_tuples_ip(skb, mask, l3proto);
1185 nf_ct_l3proto_put(l3proto);
1187 if (unlikely(ret < 0))
1188 goto nfattr_failure;
1190 proto = nf_ct_proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1191 ret = ctnetlink_dump_tuples_proto(skb, mask, proto);
1192 nf_ct_proto_put(proto);
1193 if (unlikely(ret < 0))
1194 goto nfattr_failure;
1196 NFA_NEST_END(skb, nest_parms);
1205 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1206 const struct nf_conntrack_expect *exp)
1208 struct nf_conn *master = exp->master;
1209 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1210 u_int32_t id = htonl(exp->id);
1212 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1213 goto nfattr_failure;
1214 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1215 goto nfattr_failure;
1216 if (ctnetlink_exp_dump_tuple(skb,
1217 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1218 CTA_EXPECT_MASTER) < 0)
1219 goto nfattr_failure;
1221 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1222 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1231 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1234 const struct nf_conntrack_expect *exp)
1236 struct nlmsghdr *nlh;
1237 struct nfgenmsg *nfmsg;
1242 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1243 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1244 nfmsg = NLMSG_DATA(nlh);
1246 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1247 nfmsg->nfgen_family = exp->tuple.src.l3num;
1248 nfmsg->version = NFNETLINK_V0;
1251 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1252 goto nfattr_failure;
1254 nlh->nlmsg_len = skb->tail - b;
1259 skb_trim(skb, b - skb->data);
1263 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1264 static int ctnetlink_expect_event(struct notifier_block *this,
1265 unsigned long events, void *ptr)
1267 struct nlmsghdr *nlh;
1268 struct nfgenmsg *nfmsg;
1269 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1270 struct sk_buff *skb;
1275 if (events & IPEXP_NEW) {
1276 type = IPCTNL_MSG_EXP_NEW;
1277 flags = NLM_F_CREATE|NLM_F_EXCL;
1281 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1287 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1288 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1289 nfmsg = NLMSG_DATA(nlh);
1291 nlh->nlmsg_flags = flags;
1292 nfmsg->nfgen_family = exp->tuple.src.l3num;
1293 nfmsg->version = NFNETLINK_V0;
1296 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1297 goto nfattr_failure;
1299 nlh->nlmsg_len = skb->tail - b;
1300 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1311 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1313 struct nf_conntrack_expect *exp = NULL;
1314 struct list_head *i;
1315 u_int32_t *id = (u_int32_t *) &cb->args[0];
1316 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1317 u_int8_t l3proto = nfmsg->nfgen_family;
1319 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1321 read_lock_bh(&nf_conntrack_lock);
1322 list_for_each_prev(i, &nf_conntrack_expect_list) {
1323 exp = (struct nf_conntrack_expect *) i;
1324 if (l3proto && exp->tuple.src.l3num != l3proto)
1328 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1336 read_unlock_bh(&nf_conntrack_lock);
1338 DEBUGP("leaving, last id=%llu\n", *id);
1343 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1344 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1345 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1349 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1350 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1352 struct nf_conntrack_tuple tuple;
1353 struct nf_conntrack_expect *exp;
1354 struct sk_buff *skb2;
1355 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1356 u_int8_t u3 = nfmsg->nfgen_family;
1359 DEBUGP("entered %s\n", __FUNCTION__);
1361 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1364 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1367 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1368 ctnetlink_exp_dump_table,
1369 ctnetlink_done)) != 0)
1371 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1372 if (rlen > skb->len)
1374 skb_pull(skb, rlen);
1378 if (cda[CTA_EXPECT_MASTER-1])
1379 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1386 exp = nf_conntrack_expect_find(&tuple);
1390 if (cda[CTA_EXPECT_ID-1]) {
1391 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1392 if (exp->id != ntohl(id)) {
1393 nf_conntrack_expect_put(exp);
1399 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1402 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1404 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1405 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1410 nf_conntrack_expect_put(exp);
1412 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1417 nf_conntrack_expect_put(exp);
1422 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1423 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1425 struct nf_conntrack_expect *exp, *tmp;
1426 struct nf_conntrack_tuple tuple;
1427 struct nf_conntrack_helper *h;
1428 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1429 u_int8_t u3 = nfmsg->nfgen_family;
1432 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1435 if (cda[CTA_EXPECT_TUPLE-1]) {
1436 /* delete a single expect by tuple */
1437 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1441 /* bump usage count to 2 */
1442 exp = nf_conntrack_expect_find(&tuple);
1446 if (cda[CTA_EXPECT_ID-1]) {
1448 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1449 if (exp->id != ntohl(id)) {
1450 nf_conntrack_expect_put(exp);
1455 /* after list removal, usage count == 1 */
1456 nf_conntrack_unexpect_related(exp);
1457 /* have to put what we 'get' above.
1458 * after this line usage count == 0 */
1459 nf_conntrack_expect_put(exp);
1460 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1461 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1463 /* delete all expectations for this helper */
1464 write_lock_bh(&nf_conntrack_lock);
1465 h = __nf_conntrack_helper_find_byname(name);
1467 write_unlock_bh(&nf_conntrack_lock);
1470 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1472 struct nf_conn_help *m_help = nfct_help(exp->master);
1473 if (m_help->helper == h
1474 && del_timer(&exp->timeout)) {
1475 nf_ct_unlink_expect(exp);
1476 nf_conntrack_expect_put(exp);
1479 write_unlock_bh(&nf_conntrack_lock);
1481 /* This basically means we have to flush everything*/
1482 write_lock_bh(&nf_conntrack_lock);
1483 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1485 if (del_timer(&exp->timeout)) {
1486 nf_ct_unlink_expect(exp);
1487 nf_conntrack_expect_put(exp);
1490 write_unlock_bh(&nf_conntrack_lock);
1496 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1502 ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1504 struct nf_conntrack_tuple tuple, mask, master_tuple;
1505 struct nf_conntrack_tuple_hash *h = NULL;
1506 struct nf_conntrack_expect *exp;
1508 struct nf_conn_help *help;
1511 DEBUGP("entered %s\n", __FUNCTION__);
1513 /* caller guarantees that those three CTA_EXPECT_* exist */
1514 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1517 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1520 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1524 /* Look for master conntrack of this expectation */
1525 h = nf_conntrack_find_get(&master_tuple, NULL);
1528 ct = nf_ct_tuplehash_to_ctrack(h);
1529 help = nfct_help(ct);
1531 if (!help || !help->helper) {
1532 /* such conntrack hasn't got any helper, abort */
1537 exp = nf_conntrack_expect_alloc(ct);
1543 exp->expectfn = NULL;
1546 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1547 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1549 err = nf_conntrack_expect_related(exp);
1550 nf_conntrack_expect_put(exp);
1553 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1558 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1559 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1561 struct nf_conntrack_tuple tuple;
1562 struct nf_conntrack_expect *exp;
1563 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1564 u_int8_t u3 = nfmsg->nfgen_family;
1567 DEBUGP("entered %s\n", __FUNCTION__);
1569 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1572 if (!cda[CTA_EXPECT_TUPLE-1]
1573 || !cda[CTA_EXPECT_MASK-1]
1574 || !cda[CTA_EXPECT_MASTER-1])
1577 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1581 write_lock_bh(&nf_conntrack_lock);
1582 exp = __nf_conntrack_expect_find(&tuple);
1585 write_unlock_bh(&nf_conntrack_lock);
1587 if (nlh->nlmsg_flags & NLM_F_CREATE)
1588 err = ctnetlink_create_expect(cda, u3);
1593 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1594 err = ctnetlink_change_expect(exp, cda);
1595 write_unlock_bh(&nf_conntrack_lock);
1597 DEBUGP("leaving\n");
1602 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1603 static struct notifier_block ctnl_notifier = {
1604 .notifier_call = ctnetlink_conntrack_event,
1607 static struct notifier_block ctnl_notifier_exp = {
1608 .notifier_call = ctnetlink_expect_event,
1612 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1613 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1614 .attr_count = CTA_MAX, },
1615 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1616 .attr_count = CTA_MAX, },
1617 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1618 .attr_count = CTA_MAX, },
1619 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1620 .attr_count = CTA_MAX, },
1623 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1624 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1625 .attr_count = CTA_EXPECT_MAX, },
1626 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1627 .attr_count = CTA_EXPECT_MAX, },
1628 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1629 .attr_count = CTA_EXPECT_MAX, },
1632 static struct nfnetlink_subsystem ctnl_subsys = {
1633 .name = "conntrack",
1634 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1635 .cb_count = IPCTNL_MSG_MAX,
1639 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1640 .name = "conntrack_expect",
1641 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1642 .cb_count = IPCTNL_MSG_EXP_MAX,
1646 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1647 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1649 static int __init ctnetlink_init(void)
1653 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1654 ret = nfnetlink_subsys_register(&ctnl_subsys);
1656 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1660 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1662 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1663 goto err_unreg_subsys;
1666 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1667 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1669 printk("ctnetlink_init: cannot register notifier.\n");
1670 goto err_unreg_exp_subsys;
1673 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1675 printk("ctnetlink_init: cannot expect register notifier.\n");
1676 goto err_unreg_notifier;
1682 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1684 nf_conntrack_unregister_notifier(&ctnl_notifier);
1685 err_unreg_exp_subsys:
1686 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1689 nfnetlink_subsys_unregister(&ctnl_subsys);
1694 static void __exit ctnetlink_exit(void)
1696 printk("ctnetlink: unregistering from nfnetlink.\n");
1698 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1699 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
1700 nf_conntrack_unregister_notifier(&ctnl_notifier);
1703 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1704 nfnetlink_subsys_unregister(&ctnl_subsys);
1708 module_init(ctnetlink_init);
1709 module_exit(ctnetlink_exit);