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 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1284 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1290 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1291 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1292 nfmsg = NLMSG_DATA(nlh);
1294 nlh->nlmsg_flags = flags;
1295 nfmsg->nfgen_family = exp->tuple.src.l3num;
1296 nfmsg->version = NFNETLINK_V0;
1299 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1300 goto nfattr_failure;
1302 nlh->nlmsg_len = skb->tail - b;
1303 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1314 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1316 struct nf_conntrack_expect *exp = NULL;
1317 struct list_head *i;
1318 u_int32_t *id = (u_int32_t *) &cb->args[0];
1319 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1320 u_int8_t l3proto = nfmsg->nfgen_family;
1322 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1324 read_lock_bh(&nf_conntrack_lock);
1325 list_for_each_prev(i, &nf_conntrack_expect_list) {
1326 exp = (struct nf_conntrack_expect *) i;
1327 if (l3proto && exp->tuple.src.l3num != l3proto)
1331 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1339 read_unlock_bh(&nf_conntrack_lock);
1341 DEBUGP("leaving, last id=%llu\n", *id);
1346 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1347 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1348 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1352 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1353 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1355 struct nf_conntrack_tuple tuple;
1356 struct nf_conntrack_expect *exp;
1357 struct sk_buff *skb2;
1358 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1359 u_int8_t u3 = nfmsg->nfgen_family;
1362 DEBUGP("entered %s\n", __FUNCTION__);
1364 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1367 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1370 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1371 ctnetlink_exp_dump_table,
1372 ctnetlink_done)) != 0)
1374 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1375 if (rlen > skb->len)
1377 skb_pull(skb, rlen);
1381 if (cda[CTA_EXPECT_MASTER-1])
1382 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1389 exp = nf_conntrack_expect_find(&tuple);
1393 if (cda[CTA_EXPECT_ID-1]) {
1394 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1395 if (exp->id != ntohl(id)) {
1396 nf_conntrack_expect_put(exp);
1402 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1405 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1407 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1408 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1413 nf_conntrack_expect_put(exp);
1415 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1420 nf_conntrack_expect_put(exp);
1425 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1426 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1428 struct nf_conntrack_expect *exp, *tmp;
1429 struct nf_conntrack_tuple tuple;
1430 struct nf_conntrack_helper *h;
1431 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1432 u_int8_t u3 = nfmsg->nfgen_family;
1435 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1438 if (cda[CTA_EXPECT_TUPLE-1]) {
1439 /* delete a single expect by tuple */
1440 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1444 /* bump usage count to 2 */
1445 exp = nf_conntrack_expect_find(&tuple);
1449 if (cda[CTA_EXPECT_ID-1]) {
1451 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1452 if (exp->id != ntohl(id)) {
1453 nf_conntrack_expect_put(exp);
1458 /* after list removal, usage count == 1 */
1459 nf_conntrack_unexpect_related(exp);
1460 /* have to put what we 'get' above.
1461 * after this line usage count == 0 */
1462 nf_conntrack_expect_put(exp);
1463 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1464 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1466 /* delete all expectations for this helper */
1467 write_lock_bh(&nf_conntrack_lock);
1468 h = __nf_conntrack_helper_find_byname(name);
1470 write_unlock_bh(&nf_conntrack_lock);
1473 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1475 struct nf_conn_help *m_help = nfct_help(exp->master);
1476 if (m_help->helper == h
1477 && del_timer(&exp->timeout)) {
1478 nf_ct_unlink_expect(exp);
1479 nf_conntrack_expect_put(exp);
1482 write_unlock_bh(&nf_conntrack_lock);
1484 /* This basically means we have to flush everything*/
1485 write_lock_bh(&nf_conntrack_lock);
1486 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1488 if (del_timer(&exp->timeout)) {
1489 nf_ct_unlink_expect(exp);
1490 nf_conntrack_expect_put(exp);
1493 write_unlock_bh(&nf_conntrack_lock);
1499 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1505 ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1507 struct nf_conntrack_tuple tuple, mask, master_tuple;
1508 struct nf_conntrack_tuple_hash *h = NULL;
1509 struct nf_conntrack_expect *exp;
1511 struct nf_conn_help *help;
1514 DEBUGP("entered %s\n", __FUNCTION__);
1516 /* caller guarantees that those three CTA_EXPECT_* exist */
1517 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1520 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1523 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1527 /* Look for master conntrack of this expectation */
1528 h = nf_conntrack_find_get(&master_tuple, NULL);
1531 ct = nf_ct_tuplehash_to_ctrack(h);
1532 help = nfct_help(ct);
1534 if (!help || !help->helper) {
1535 /* such conntrack hasn't got any helper, abort */
1540 exp = nf_conntrack_expect_alloc(ct);
1546 exp->expectfn = NULL;
1549 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1550 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1552 err = nf_conntrack_expect_related(exp);
1553 nf_conntrack_expect_put(exp);
1556 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1561 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1562 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1564 struct nf_conntrack_tuple tuple;
1565 struct nf_conntrack_expect *exp;
1566 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1567 u_int8_t u3 = nfmsg->nfgen_family;
1570 DEBUGP("entered %s\n", __FUNCTION__);
1572 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1575 if (!cda[CTA_EXPECT_TUPLE-1]
1576 || !cda[CTA_EXPECT_MASK-1]
1577 || !cda[CTA_EXPECT_MASTER-1])
1580 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1584 write_lock_bh(&nf_conntrack_lock);
1585 exp = __nf_conntrack_expect_find(&tuple);
1588 write_unlock_bh(&nf_conntrack_lock);
1590 if (nlh->nlmsg_flags & NLM_F_CREATE)
1591 err = ctnetlink_create_expect(cda, u3);
1596 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1597 err = ctnetlink_change_expect(exp, cda);
1598 write_unlock_bh(&nf_conntrack_lock);
1600 DEBUGP("leaving\n");
1605 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1606 static struct notifier_block ctnl_notifier = {
1607 .notifier_call = ctnetlink_conntrack_event,
1610 static struct notifier_block ctnl_notifier_exp = {
1611 .notifier_call = ctnetlink_expect_event,
1615 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1616 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1617 .attr_count = CTA_MAX, },
1618 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1619 .attr_count = CTA_MAX, },
1620 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1621 .attr_count = CTA_MAX, },
1622 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1623 .attr_count = CTA_MAX, },
1626 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1627 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1628 .attr_count = CTA_EXPECT_MAX, },
1629 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1630 .attr_count = CTA_EXPECT_MAX, },
1631 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1632 .attr_count = CTA_EXPECT_MAX, },
1635 static struct nfnetlink_subsystem ctnl_subsys = {
1636 .name = "conntrack",
1637 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1638 .cb_count = IPCTNL_MSG_MAX,
1642 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1643 .name = "conntrack_expect",
1644 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1645 .cb_count = IPCTNL_MSG_EXP_MAX,
1649 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1650 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1652 static int __init ctnetlink_init(void)
1656 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1657 ret = nfnetlink_subsys_register(&ctnl_subsys);
1659 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1663 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1665 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1666 goto err_unreg_subsys;
1669 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1670 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1672 printk("ctnetlink_init: cannot register notifier.\n");
1673 goto err_unreg_exp_subsys;
1676 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1678 printk("ctnetlink_init: cannot expect register notifier.\n");
1679 goto err_unreg_notifier;
1685 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1687 nf_conntrack_unregister_notifier(&ctnl_notifier);
1688 err_unreg_exp_subsys:
1689 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1692 nfnetlink_subsys_unregister(&ctnl_subsys);
1697 static void __exit ctnetlink_exit(void)
1699 printk("ctnetlink: unregistering from nfnetlink.\n");
1701 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1702 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
1703 nf_conntrack_unregister_notifier(&ctnl_notifier);
1706 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1707 nfnetlink_subsys_unregister(&ctnl_subsys);
1711 module_init(ctnetlink_init);
1712 module_exit(ctnetlink_exit);