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-2005 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7 * (C) 2005 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.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/timer.h>
26 #include <linux/skbuff.h>
27 #include <linux/errno.h>
28 #include <linux/netlink.h>
29 #include <linux/spinlock.h>
30 #include <linux/notifier.h>
31 #include <linux/rtnetlink.h>
33 #include <linux/netfilter.h>
34 #include <linux/netfilter_ipv4.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
36 #include <linux/netfilter_ipv4/ip_conntrack.h>
37 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
38 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
39 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
40 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
42 #include <linux/netfilter/nfnetlink.h>
43 #include <linux/netfilter/nfnetlink_conntrack.h>
45 MODULE_LICENSE("GPL");
47 static char __initdata version[] = "0.90";
52 #define DEBUGP(format, args...)
57 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
58 const struct ip_conntrack_tuple *tuple)
60 struct ip_conntrack_protocol *proto;
62 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
64 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
65 if (proto && proto->tuple_to_nfattr)
66 return proto->tuple_to_nfattr(skb, tuple);
75 ctnetlink_dump_tuples(struct sk_buff *skb,
76 const struct ip_conntrack_tuple *tuple)
78 struct nfattr *nest_parms;
80 nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
81 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t), &tuple->src.ip);
82 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t), &tuple->dst.ip);
83 NFA_NEST_END(skb, nest_parms);
85 nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
86 ctnetlink_dump_tuples_proto(skb, tuple);
87 NFA_NEST_END(skb, nest_parms);
96 ctnetlink_dump_status(struct sk_buff *skb, const struct ip_conntrack *ct)
98 u_int32_t status = htonl((u_int32_t) ct->status);
99 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
107 ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct)
109 long timeout_l = ct->timeout.expires - jiffies;
115 timeout = htonl(timeout_l / HZ);
117 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
125 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
127 struct ip_conntrack_protocol *proto = ip_conntrack_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
129 struct nfattr *nest_proto;
132 if (!proto || !proto->to_nfattr)
135 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
137 ret = proto->to_nfattr(skb, nest_proto, ct);
139 ip_conntrack_proto_put(proto);
141 NFA_NEST_END(skb, nest_proto);
150 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
152 struct nfattr *nest_helper;
157 nest_helper = NFA_NEST(skb, CTA_HELP);
158 NFA_PUT(skb, CTA_HELP_NAME, CTA_HELP_MAXNAMESIZE, &ct->helper->name);
160 if (ct->helper->to_nfattr)
161 ct->helper->to_nfattr(skb, ct);
163 NFA_NEST_END(skb, nest_helper);
171 #ifdef CONFIG_IP_NF_CT_ACCT
173 ctnetlink_dump_counters(struct sk_buff *skb, const struct ip_conntrack *ct,
174 enum ip_conntrack_dir dir)
176 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
177 struct nfattr *nest_count = NFA_NEST(skb, type);
180 tmp = cpu_to_be64(ct->counters[dir].packets);
181 NFA_PUT(skb, CTA_COUNTERS_PACKETS, sizeof(u_int64_t), &tmp);
183 tmp = cpu_to_be64(ct->counters[dir].bytes);
184 NFA_PUT(skb, CTA_COUNTERS_BYTES, sizeof(u_int64_t), &tmp);
186 NFA_NEST_END(skb, nest_count);
194 #define ctnetlink_dump_counters(a, b, c) (0)
197 #ifdef CONFIG_IP_NF_CONNTRACK_MARK
199 ctnetlink_dump_mark(struct sk_buff *skb, const struct ip_conntrack *ct)
201 u_int32_t mark = htonl(ct->mark);
203 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
210 #define ctnetlink_dump_mark(a, b) (0)
214 ctnetlink_dump_id(struct sk_buff *skb, const struct ip_conntrack *ct)
216 u_int32_t id = htonl(ct->id);
217 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
225 ctnetlink_dump_use(struct sk_buff *skb, const struct ip_conntrack *ct)
227 unsigned int use = htonl(atomic_read(&ct->ct_general.use));
229 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
236 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
239 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
240 int event, int nowait,
241 const struct ip_conntrack *ct)
243 struct nlmsghdr *nlh;
244 struct nfgenmsg *nfmsg;
245 struct nfattr *nest_parms;
250 event |= NFNL_SUBSYS_CTNETLINK << 8;
251 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
252 nfmsg = NLMSG_DATA(nlh);
254 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
255 nfmsg->nfgen_family = AF_INET;
256 nfmsg->version = NFNETLINK_V0;
259 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
260 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
262 NFA_NEST_END(skb, nest_parms);
264 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
265 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
267 NFA_NEST_END(skb, nest_parms);
269 if (ctnetlink_dump_status(skb, ct) < 0 ||
270 ctnetlink_dump_timeout(skb, ct) < 0 ||
271 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
272 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
273 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
274 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
275 ctnetlink_dump_mark(skb, ct) < 0 ||
276 ctnetlink_dump_id(skb, ct) < 0 ||
277 ctnetlink_dump_use(skb, ct) < 0)
280 nlh->nlmsg_len = skb->tail - b;
285 skb_trim(skb, b - skb->data);
289 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
290 static int ctnetlink_conntrack_event(struct notifier_block *this,
291 unsigned long events, void *ptr)
293 struct nlmsghdr *nlh;
294 struct nfgenmsg *nfmsg;
295 struct nfattr *nest_parms;
296 struct ip_conntrack *ct = (struct ip_conntrack *)ptr;
300 unsigned int flags = 0, groups;
302 /* ignore our fake conntrack entry */
303 if (ct == &ip_conntrack_untracked)
306 if (events & IPCT_DESTROY) {
307 type = IPCTNL_MSG_CT_DELETE;
308 groups = NF_NETLINK_CONNTRACK_DESTROY;
311 if (events & (IPCT_NEW | IPCT_RELATED)) {
312 type = IPCTNL_MSG_CT_NEW;
313 flags = NLM_F_CREATE|NLM_F_EXCL;
314 /* dump everything */
316 groups = NF_NETLINK_CONNTRACK_NEW;
319 if (events & (IPCT_STATUS |
324 type = IPCTNL_MSG_CT_NEW;
325 groups = NF_NETLINK_CONNTRACK_UPDATE;
332 /* FIXME: Check if there are any listeners before, don't hurt performance */
334 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
340 type |= NFNL_SUBSYS_CTNETLINK << 8;
341 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
342 nfmsg = NLMSG_DATA(nlh);
344 nlh->nlmsg_flags = flags;
345 nfmsg->nfgen_family = AF_INET;
346 nfmsg->version = NFNETLINK_V0;
349 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
350 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
352 NFA_NEST_END(skb, nest_parms);
354 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
355 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
357 NFA_NEST_END(skb, nest_parms);
359 /* NAT stuff is now a status flag */
360 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
361 && ctnetlink_dump_status(skb, ct) < 0)
363 if (events & IPCT_REFRESH
364 && ctnetlink_dump_timeout(skb, ct) < 0)
366 if (events & IPCT_PROTOINFO
367 && ctnetlink_dump_protoinfo(skb, ct) < 0)
369 if (events & IPCT_HELPINFO
370 && ctnetlink_dump_helpinfo(skb, ct) < 0)
373 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
374 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
377 nlh->nlmsg_len = skb->tail - b;
378 nfnetlink_send(skb, 0, groups, 0);
386 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
388 static int ctnetlink_done(struct netlink_callback *cb)
390 DEBUGP("entered %s\n", __FUNCTION__);
395 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
397 struct ip_conntrack *ct = NULL;
398 struct ip_conntrack_tuple_hash *h;
400 u_int32_t *id = (u_int32_t *) &cb->args[1];
402 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
405 read_lock_bh(&ip_conntrack_lock);
406 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
407 list_for_each(i, &ip_conntrack_hash[cb->args[0]]) {
408 h = (struct ip_conntrack_tuple_hash *) i;
409 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
411 ct = tuplehash_to_ctrack(h);
414 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
423 read_unlock_bh(&ip_conntrack_lock);
425 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
430 #ifdef CONFIG_IP_NF_CT_ACCT
432 ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
434 struct ip_conntrack *ct = NULL;
435 struct ip_conntrack_tuple_hash *h;
437 u_int32_t *id = (u_int32_t *) &cb->args[1];
439 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
442 write_lock_bh(&ip_conntrack_lock);
443 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
444 list_for_each(i, &ip_conntrack_hash[cb->args[0]]) {
445 h = (struct ip_conntrack_tuple_hash *) i;
446 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
448 ct = tuplehash_to_ctrack(h);
451 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
458 memset(&ct->counters, 0, sizeof(ct->counters));
462 write_unlock_bh(&ip_conntrack_lock);
464 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
470 static const int cta_min_ip[CTA_IP_MAX] = {
471 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
472 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
476 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple)
478 struct nfattr *tb[CTA_IP_MAX];
480 DEBUGP("entered %s\n", __FUNCTION__);
482 memset(tb, 0, CTA_IP_MAX * sizeof(tb));
484 if (nfattr_parse_nested(tb, CTA_IP_MAX, attr) < 0)
487 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
490 if (!tb[CTA_IP_V4_SRC-1])
492 tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
494 if (!tb[CTA_IP_V4_DST-1])
496 tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
506 static const int cta_min_proto[CTA_PROTO_MAX] = {
507 [CTA_PROTO_NUM-1] = sizeof(u_int16_t),
508 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
509 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
510 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
511 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
512 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
516 ctnetlink_parse_tuple_proto(struct nfattr *attr,
517 struct ip_conntrack_tuple *tuple)
519 struct nfattr *tb[CTA_PROTO_MAX];
520 struct ip_conntrack_protocol *proto;
523 DEBUGP("entered %s\n", __FUNCTION__);
525 memset(tb, 0, CTA_PROTO_MAX * sizeof(tb));
527 if (nfattr_parse_nested(tb, CTA_PROTO_MAX, attr) < 0)
530 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
533 if (!tb[CTA_PROTO_NUM-1])
535 tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
537 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
539 if (likely(proto && proto->nfattr_to_tuple)) {
540 ret = proto->nfattr_to_tuple(tb, tuple);
541 ip_conntrack_proto_put(proto);
551 ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
552 enum ctattr_tuple type)
554 struct nfattr *tb[CTA_TUPLE_MAX];
557 DEBUGP("entered %s\n", __FUNCTION__);
559 memset(tb, 0, CTA_TUPLE_MAX * sizeof(tb));
560 memset(tuple, 0, sizeof(*tuple));
562 if (nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]) < 0)
565 if (!tb[CTA_TUPLE_IP-1])
568 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
572 if (!tb[CTA_TUPLE_PROTO-1])
575 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
579 /* orig and expect tuples get DIR_ORIGINAL */
580 if (type == CTA_TUPLE_REPLY)
581 tuple->dst.dir = IP_CT_DIR_REPLY;
583 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
595 #ifdef CONFIG_IP_NF_NAT_NEEDED
596 static const int cta_min_protonat[CTA_PROTONAT_MAX] = {
597 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
598 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
601 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
602 const struct ip_conntrack *ct,
603 struct ip_nat_range *range)
605 struct nfattr *tb[CTA_PROTONAT_MAX];
606 struct ip_nat_protocol *npt;
608 DEBUGP("entered %s\n", __FUNCTION__);
610 memset(tb, 0, CTA_PROTONAT_MAX * sizeof(tb));
612 if (nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr) < 0)
615 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
618 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
622 if (!npt->nfattr_to_range) {
623 ip_nat_proto_put(npt);
627 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
628 if (npt->nfattr_to_range(tb, range) > 0)
629 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
631 ip_nat_proto_put(npt);
641 ctnetlink_parse_nat(struct nfattr *cda[],
642 const struct ip_conntrack *ct, struct ip_nat_range *range)
644 struct nfattr *tb[CTA_NAT_MAX];
647 DEBUGP("entered %s\n", __FUNCTION__);
649 memset(tb, 0, CTA_NAT_MAX * sizeof(tb));
650 memset(range, 0, sizeof(*range));
652 if (nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]) < 0)
655 if (tb[CTA_NAT_MINIP-1])
656 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
658 if (!tb[CTA_NAT_MAXIP-1])
659 range->max_ip = range->min_ip;
661 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
664 range->flags |= IP_NAT_RANGE_MAP_IPS;
666 if (!tb[CTA_NAT_PROTO-1])
669 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
682 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
684 struct nfattr *tb[CTA_HELP_MAX];
686 DEBUGP("entered %s\n", __FUNCTION__);
687 memset(tb, 0, CTA_HELP_MAX * sizeof(tb));
689 if (nfattr_parse_nested(tb, CTA_HELP_MAX, attr) < 0)
692 if (!tb[CTA_HELP_NAME-1])
695 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
704 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
705 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
707 struct ip_conntrack_tuple_hash *h;
708 struct ip_conntrack_tuple tuple;
709 struct ip_conntrack *ct;
712 DEBUGP("entered %s\n", __FUNCTION__);
714 if (cda[CTA_TUPLE_ORIG-1])
715 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
716 else if (cda[CTA_TUPLE_REPLY-1])
717 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
719 /* Flush the whole table */
720 ip_conntrack_flush();
727 h = ip_conntrack_find_get(&tuple, NULL);
729 DEBUGP("tuple not found in conntrack hash\n");
733 ct = tuplehash_to_ctrack(h);
736 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
738 ip_conntrack_put(ct);
742 if (del_timer(&ct->timeout)) {
743 ip_conntrack_put(ct);
744 ct->timeout.function((unsigned long)ct);
747 ip_conntrack_put(ct);
754 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
755 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
757 struct ip_conntrack_tuple_hash *h;
758 struct ip_conntrack_tuple tuple;
759 struct ip_conntrack *ct;
760 struct sk_buff *skb2 = NULL;
763 DEBUGP("entered %s\n", __FUNCTION__);
765 if (nlh->nlmsg_flags & NLM_F_DUMP) {
766 struct nfgenmsg *msg = NLMSG_DATA(nlh);
769 if (msg->nfgen_family != AF_INET)
770 return -EAFNOSUPPORT;
772 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
773 IPCTNL_MSG_CT_GET_CTRZERO) {
774 #ifdef CONFIG_IP_NF_CT_ACCT
775 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
776 ctnetlink_dump_table_w,
777 ctnetlink_done)) != 0)
783 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
784 ctnetlink_dump_table,
785 ctnetlink_done)) != 0)
789 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
796 if (cda[CTA_TUPLE_ORIG-1])
797 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
798 else if (cda[CTA_TUPLE_REPLY-1])
799 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
806 h = ip_conntrack_find_get(&tuple, NULL);
808 DEBUGP("tuple not found in conntrack hash");
811 DEBUGP("tuple found\n");
812 ct = tuplehash_to_ctrack(h);
815 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
817 ip_conntrack_put(ct);
820 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
822 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
823 IPCTNL_MSG_CT_NEW, 1, ct);
824 ip_conntrack_put(ct);
828 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
842 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
844 unsigned long d, status = *(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]);
845 d = ct->status ^ status;
847 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
851 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
852 /* SEEN_REPLY bit can only be set */
856 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
857 /* ASSURED bit can only be set */
860 if (cda[CTA_NAT-1]) {
861 #ifndef CONFIG_IP_NF_NAT_NEEDED
864 unsigned int hooknum;
865 struct ip_nat_range range;
867 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
870 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
871 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
872 htons(range.min.all), htons(range.max.all));
874 /* This is tricky but it works. ip_nat_setup_info needs the
875 * hook number as parameter, so let's do the correct
876 * conversion and run away */
877 if (status & IPS_SRC_NAT_DONE)
878 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
879 else if (status & IPS_DST_NAT_DONE)
880 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
882 return -EINVAL; /* Missing NAT flags */
884 DEBUGP("NAT status: %lu\n",
885 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
887 if (ip_nat_initialized(ct, hooknum))
889 ip_nat_setup_info(ct, &range, hooknum);
891 DEBUGP("NAT status after setup_info: %lu\n",
892 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
896 /* Be careful here, modifying NAT bits can screw up things,
897 * so don't let users modify them directly if they don't pass
899 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
905 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
907 struct ip_conntrack_helper *helper;
911 DEBUGP("entered %s\n", __FUNCTION__);
913 /* don't change helper of sibling connections */
917 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
921 helper = __ip_conntrack_helper_find_byname(helpname);
923 if (!strcmp(helpname, ""))
931 /* we had a helper before ... */
932 ip_ct_remove_expectations(ct);
935 /* need to zero data of old helper */
936 memset(&ct->help, 0, sizeof(ct->help));
946 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
948 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
950 if (!del_timer(&ct->timeout))
953 ct->timeout.expires = jiffies + timeout * HZ;
954 add_timer(&ct->timeout);
960 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
964 DEBUGP("entered %s\n", __FUNCTION__);
966 if (cda[CTA_HELP-1]) {
967 err = ctnetlink_change_helper(ct, cda);
972 if (cda[CTA_TIMEOUT-1]) {
973 err = ctnetlink_change_timeout(ct, cda);
978 if (cda[CTA_STATUS-1]) {
979 err = ctnetlink_change_status(ct, cda);
984 DEBUGP("all done\n");
989 ctnetlink_create_conntrack(struct nfattr *cda[],
990 struct ip_conntrack_tuple *otuple,
991 struct ip_conntrack_tuple *rtuple)
993 struct ip_conntrack *ct;
996 DEBUGP("entered %s\n", __FUNCTION__);
998 ct = ip_conntrack_alloc(otuple, rtuple);
999 if (ct == NULL || IS_ERR(ct))
1002 if (!cda[CTA_TIMEOUT-1])
1004 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1006 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1007 ct->status |= IPS_CONFIRMED;
1009 err = ctnetlink_change_status(ct, cda);
1013 ct->helper = ip_conntrack_helper_find_get(rtuple);
1015 add_timer(&ct->timeout);
1016 ip_conntrack_hash_insert(ct);
1019 ip_conntrack_helper_put(ct->helper);
1021 DEBUGP("conntrack with id %u inserted\n", ct->id);
1025 ip_conntrack_free(ct);
1030 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1031 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1033 struct ip_conntrack_tuple otuple, rtuple;
1034 struct ip_conntrack_tuple_hash *h = NULL;
1037 DEBUGP("entered %s\n", __FUNCTION__);
1039 if (cda[CTA_TUPLE_ORIG-1]) {
1040 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1045 if (cda[CTA_TUPLE_REPLY-1]) {
1046 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1051 write_lock_bh(&ip_conntrack_lock);
1052 if (cda[CTA_TUPLE_ORIG-1])
1053 h = __ip_conntrack_find(&otuple, NULL);
1054 else if (cda[CTA_TUPLE_REPLY-1])
1055 h = __ip_conntrack_find(&rtuple, NULL);
1058 write_unlock_bh(&ip_conntrack_lock);
1059 DEBUGP("no such conntrack, create new\n");
1061 if (nlh->nlmsg_flags & NLM_F_CREATE)
1062 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1065 /* we only allow nat config for new conntracks */
1066 if (cda[CTA_NAT-1]) {
1072 /* We manipulate the conntrack inside the global conntrack table lock,
1073 * so there's no need to increase the refcount */
1074 DEBUGP("conntrack found\n");
1076 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1077 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1080 write_unlock_bh(&ip_conntrack_lock);
1084 /***********************************************************************
1086 ***********************************************************************/
1089 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1090 const struct ip_conntrack_tuple *tuple,
1091 enum ctattr_expect type)
1093 struct nfattr *nest_parms = NFA_NEST(skb, type);
1095 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1096 goto nfattr_failure;
1098 NFA_NEST_END(skb, nest_parms);
1107 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1108 const struct ip_conntrack_expect *exp)
1110 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1111 u_int32_t id = htonl(exp->id);
1112 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT);
1114 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1115 goto nfattr_failure;
1116 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1117 goto nfattr_failure;
1119 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1120 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1121 NFA_NEST_END(skb, nest_parms);
1130 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1133 const struct ip_conntrack_expect *exp)
1135 struct nlmsghdr *nlh;
1136 struct nfgenmsg *nfmsg;
1141 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1142 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1143 nfmsg = NLMSG_DATA(nlh);
1145 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1146 nfmsg->nfgen_family = AF_INET;
1147 nfmsg->version = NFNETLINK_V0;
1150 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1151 goto nfattr_failure;
1153 nlh->nlmsg_len = skb->tail - b;
1158 skb_trim(skb, b - skb->data);
1162 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1163 static int ctnetlink_expect_event(struct notifier_block *this,
1164 unsigned long events, void *ptr)
1166 struct nlmsghdr *nlh;
1167 struct nfgenmsg *nfmsg;
1168 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1169 struct sk_buff *skb;
1175 if (events & IPEXP_NEW) {
1176 type = IPCTNL_MSG_EXP_NEW;
1177 flags = NLM_F_CREATE|NLM_F_EXCL;
1181 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1187 type |= NFNL_SUBSYS_CTNETLINK << 8;
1188 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1189 nfmsg = NLMSG_DATA(nlh);
1191 nlh->nlmsg_flags = flags;
1192 nfmsg->nfgen_family = AF_INET;
1193 nfmsg->version = NFNETLINK_V0;
1196 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1197 goto nfattr_failure;
1199 nlh->nlmsg_len = skb->tail - b;
1200 proto = exp->tuple.dst.protonum;
1201 nfnetlink_send(skb, 0, NF_NETLINK_CONNTRACK_EXP_NEW, 0);
1212 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1214 struct ip_conntrack_expect *exp = NULL;
1215 struct list_head *i;
1216 u_int32_t *id = (u_int32_t *) &cb->args[0];
1218 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1220 read_lock_bh(&ip_conntrack_lock);
1221 list_for_each(i, &ip_conntrack_expect_list) {
1222 exp = (struct ip_conntrack_expect *) i;
1225 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1233 read_unlock_bh(&ip_conntrack_lock);
1235 DEBUGP("leaving, last id=%llu\n", *id);
1241 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1242 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1244 struct ip_conntrack_tuple tuple;
1245 struct ip_conntrack_expect *exp;
1246 struct sk_buff *skb2;
1249 DEBUGP("entered %s\n", __FUNCTION__);
1251 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1252 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1255 if (msg->nfgen_family != AF_INET)
1256 return -EAFNOSUPPORT;
1258 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1259 ctnetlink_exp_dump_table,
1260 ctnetlink_done)) != 0)
1262 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1263 if (rlen > skb->len)
1265 skb_pull(skb, rlen);
1269 if (cda[CTA_TUPLE_ORIG-1])
1270 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
1271 else if (cda[CTA_TUPLE_REPLY-1])
1272 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
1279 exp = ip_conntrack_expect_find_get(&tuple);
1284 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1287 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1289 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1290 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1295 ip_conntrack_expect_put(exp);
1297 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1304 ip_conntrack_expect_put(exp);
1312 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1313 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1315 struct ip_conntrack_expect *exp, *tmp;
1316 struct ip_conntrack_tuple tuple;
1317 struct ip_conntrack_helper *h;
1320 /* delete by tuple needs either orig or reply tuple */
1321 if (cda[CTA_TUPLE_ORIG-1])
1322 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
1323 else if (cda[CTA_TUPLE_REPLY-1])
1324 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
1325 else if (cda[CTA_HELP_NAME-1]) {
1326 char *name = NFA_DATA(cda[CTA_HELP_NAME-1]);
1328 /* delete all expectations for this helper */
1329 write_lock_bh(&ip_conntrack_lock);
1330 h = __ip_conntrack_helper_find_byname(name);
1332 write_unlock_bh(&ip_conntrack_lock);
1335 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1337 if (exp->master->helper == h
1338 && del_timer(&exp->timeout))
1339 __ip_ct_expect_unlink_destroy(exp);
1341 write_unlock(&ip_conntrack_lock);
1344 /* This basically means we have to flush everything*/
1345 write_lock_bh(&ip_conntrack_lock);
1346 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1348 if (del_timer(&exp->timeout))
1349 __ip_ct_expect_unlink_destroy(exp);
1351 write_unlock_bh(&ip_conntrack_lock);
1358 /* bump usage count to 2 */
1359 exp = ip_conntrack_expect_find_get(&tuple);
1363 if (cda[CTA_EXPECT_ID-1]) {
1364 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1365 if (exp->id != ntohl(id)) {
1366 ip_conntrack_expect_put(exp);
1371 /* after list removal, usage count == 1 */
1372 ip_conntrack_unexpect_related(exp);
1373 /* have to put what we 'get' above. after this line usage count == 0 */
1374 ip_conntrack_expect_put(exp);
1379 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1385 ctnetlink_create_expect(struct nfattr *cda[])
1387 struct ip_conntrack_tuple tuple, mask, master_tuple;
1388 struct ip_conntrack_tuple_hash *h = NULL;
1389 struct ip_conntrack_expect *exp;
1390 struct ip_conntrack *ct;
1393 DEBUGP("entered %s\n", __FUNCTION__);
1395 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1398 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASK);
1402 if (cda[CTA_TUPLE_ORIG-1])
1403 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_TUPLE_ORIG);
1404 else if (cda[CTA_TUPLE_REPLY-1])
1405 err = ctnetlink_parse_tuple(cda, &master_tuple,
1413 /* Look for master conntrack of this expectation */
1414 h = ip_conntrack_find_get(&master_tuple, NULL);
1417 ct = tuplehash_to_ctrack(h);
1420 /* such conntrack hasn't got any helper, abort */
1425 exp = ip_conntrack_expect_alloc(ct);
1431 exp->expectfn = NULL;
1433 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1434 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1436 err = ip_conntrack_expect_related(exp);
1437 ip_conntrack_expect_put(exp);
1440 ip_conntrack_put(tuplehash_to_ctrack(h));
1445 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1446 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1448 struct ip_conntrack_tuple tuple;
1449 struct ip_conntrack_expect *exp;
1452 DEBUGP("entered %s\n", __FUNCTION__);
1454 if (!cda[CTA_EXPECT_TUPLE-1] || !cda[CTA_EXPECT_MASK-1])
1457 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1461 write_lock_bh(&ip_conntrack_lock);
1462 exp = __ip_conntrack_expect_find(&tuple);
1465 write_unlock_bh(&ip_conntrack_lock);
1467 if (nlh->nlmsg_flags & NLM_F_CREATE)
1468 err = ctnetlink_create_expect(cda);
1473 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1474 err = ctnetlink_change_expect(exp, cda);
1475 write_unlock_bh(&ip_conntrack_lock);
1477 DEBUGP("leaving\n");
1482 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1483 static struct notifier_block ctnl_notifier = {
1484 .notifier_call = ctnetlink_conntrack_event,
1487 static struct notifier_block ctnl_notifier_exp = {
1488 .notifier_call = ctnetlink_expect_event,
1492 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1493 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1494 .cap_required = CAP_NET_ADMIN },
1495 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1496 .cap_required = CAP_NET_ADMIN },
1497 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1498 .cap_required = CAP_NET_ADMIN },
1499 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1500 .cap_required = CAP_NET_ADMIN },
1503 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_MAX] = {
1504 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1505 .cap_required = CAP_NET_ADMIN },
1506 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1507 .cap_required = CAP_NET_ADMIN },
1508 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1509 .cap_required = CAP_NET_ADMIN },
1512 static struct nfnetlink_subsystem ctnl_subsys = {
1513 .name = "conntrack",
1514 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1515 .cb_count = IPCTNL_MSG_MAX,
1516 .attr_count = CTA_MAX,
1520 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1521 .name = "conntrack_expect",
1522 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1523 .cb_count = IPCTNL_MSG_EXP_MAX,
1524 .attr_count = CTA_MAX,
1528 static int __init ctnetlink_init(void)
1532 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1533 ret = nfnetlink_subsys_register(&ctnl_subsys);
1535 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1539 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1541 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1542 goto err_unreg_subsys;
1545 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1546 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1548 printk("ctnetlink_init: cannot register notifier.\n");
1549 goto err_unreg_exp_subsys;
1552 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1554 printk("ctnetlink_init: cannot expect register notifier.\n");
1555 goto err_unreg_notifier;
1561 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1563 ip_conntrack_unregister_notifier(&ctnl_notifier);
1564 err_unreg_exp_subsys:
1565 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1568 nfnetlink_subsys_unregister(&ctnl_subsys);
1573 static void __exit ctnetlink_exit(void)
1575 printk("ctnetlink: unregistering from nfnetlink.\n");
1577 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1578 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1579 ip_conntrack_unregister_notifier(&ctnl_notifier);
1582 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1583 nfnetlink_subsys_unregister(&ctnl_subsys);
1587 module_init(ctnetlink_init);
1588 module_exit(ctnetlink_exit);