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;
63 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
65 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
66 if (likely(proto && proto->tuple_to_nfattr)) {
67 ret = proto->tuple_to_nfattr(skb, tuple);
68 ip_conntrack_proto_put(proto);
78 ctnetlink_dump_tuples(struct sk_buff *skb,
79 const struct ip_conntrack_tuple *tuple)
81 struct nfattr *nest_parms;
83 nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
84 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t), &tuple->src.ip);
85 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t), &tuple->dst.ip);
86 NFA_NEST_END(skb, nest_parms);
88 nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
89 ctnetlink_dump_tuples_proto(skb, tuple);
90 NFA_NEST_END(skb, nest_parms);
99 ctnetlink_dump_status(struct sk_buff *skb, const struct ip_conntrack *ct)
101 u_int32_t status = htonl((u_int32_t) ct->status);
102 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
110 ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct)
112 long timeout_l = ct->timeout.expires - jiffies;
118 timeout = htonl(timeout_l / HZ);
120 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
128 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
130 struct ip_conntrack_protocol *proto = ip_conntrack_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
132 struct nfattr *nest_proto;
135 if (!proto || !proto->to_nfattr)
138 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
140 ret = proto->to_nfattr(skb, nest_proto, ct);
142 ip_conntrack_proto_put(proto);
144 NFA_NEST_END(skb, nest_proto);
153 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
155 struct nfattr *nest_helper;
160 nest_helper = NFA_NEST(skb, CTA_HELP);
161 NFA_PUT(skb, CTA_HELP_NAME, CTA_HELP_MAXNAMESIZE, &ct->helper->name);
163 if (ct->helper->to_nfattr)
164 ct->helper->to_nfattr(skb, ct);
166 NFA_NEST_END(skb, nest_helper);
174 #ifdef CONFIG_IP_NF_CT_ACCT
176 ctnetlink_dump_counters(struct sk_buff *skb, const struct ip_conntrack *ct,
177 enum ip_conntrack_dir dir)
179 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
180 struct nfattr *nest_count = NFA_NEST(skb, type);
183 tmp = htonl(ct->counters[dir].packets);
184 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
186 tmp = htonl(ct->counters[dir].bytes);
187 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
189 NFA_NEST_END(skb, nest_count);
197 #define ctnetlink_dump_counters(a, b, c) (0)
200 #ifdef CONFIG_IP_NF_CONNTRACK_MARK
202 ctnetlink_dump_mark(struct sk_buff *skb, const struct ip_conntrack *ct)
204 u_int32_t mark = htonl(ct->mark);
206 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
213 #define ctnetlink_dump_mark(a, b) (0)
217 ctnetlink_dump_id(struct sk_buff *skb, const struct ip_conntrack *ct)
219 u_int32_t id = htonl(ct->id);
220 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
228 ctnetlink_dump_use(struct sk_buff *skb, const struct ip_conntrack *ct)
230 unsigned int use = htonl(atomic_read(&ct->ct_general.use));
232 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
239 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
242 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
243 int event, int nowait,
244 const struct ip_conntrack *ct)
246 struct nlmsghdr *nlh;
247 struct nfgenmsg *nfmsg;
248 struct nfattr *nest_parms;
253 event |= NFNL_SUBSYS_CTNETLINK << 8;
254 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
255 nfmsg = NLMSG_DATA(nlh);
257 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
258 nfmsg->nfgen_family = AF_INET;
259 nfmsg->version = NFNETLINK_V0;
262 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
263 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
265 NFA_NEST_END(skb, nest_parms);
267 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
268 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
270 NFA_NEST_END(skb, nest_parms);
272 if (ctnetlink_dump_status(skb, ct) < 0 ||
273 ctnetlink_dump_timeout(skb, ct) < 0 ||
274 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
275 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
276 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
277 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
278 ctnetlink_dump_mark(skb, ct) < 0 ||
279 ctnetlink_dump_id(skb, ct) < 0 ||
280 ctnetlink_dump_use(skb, ct) < 0)
283 nlh->nlmsg_len = skb->tail - b;
288 skb_trim(skb, b - skb->data);
292 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
293 static int ctnetlink_conntrack_event(struct notifier_block *this,
294 unsigned long events, void *ptr)
296 struct nlmsghdr *nlh;
297 struct nfgenmsg *nfmsg;
298 struct nfattr *nest_parms;
299 struct ip_conntrack *ct = (struct ip_conntrack *)ptr;
303 unsigned int flags = 0, group;
305 /* ignore our fake conntrack entry */
306 if (ct == &ip_conntrack_untracked)
309 if (events & IPCT_DESTROY) {
310 type = IPCTNL_MSG_CT_DELETE;
311 group = NFNLGRP_CONNTRACK_DESTROY;
314 if (events & (IPCT_NEW | IPCT_RELATED)) {
315 type = IPCTNL_MSG_CT_NEW;
316 flags = NLM_F_CREATE|NLM_F_EXCL;
317 /* dump everything */
319 group = NFNLGRP_CONNTRACK_NEW;
322 if (events & (IPCT_STATUS |
327 type = IPCTNL_MSG_CT_NEW;
328 group = NFNLGRP_CONNTRACK_UPDATE;
335 /* FIXME: Check if there are any listeners before, don't hurt performance */
337 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
343 type |= NFNL_SUBSYS_CTNETLINK << 8;
344 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
345 nfmsg = NLMSG_DATA(nlh);
347 nlh->nlmsg_flags = flags;
348 nfmsg->nfgen_family = AF_INET;
349 nfmsg->version = NFNETLINK_V0;
352 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
353 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
355 NFA_NEST_END(skb, nest_parms);
357 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
358 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
360 NFA_NEST_END(skb, nest_parms);
362 /* NAT stuff is now a status flag */
363 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
364 && ctnetlink_dump_status(skb, ct) < 0)
366 if (events & IPCT_REFRESH
367 && ctnetlink_dump_timeout(skb, ct) < 0)
369 if (events & IPCT_PROTOINFO
370 && ctnetlink_dump_protoinfo(skb, ct) < 0)
372 if (events & IPCT_HELPINFO
373 && ctnetlink_dump_helpinfo(skb, ct) < 0)
376 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
377 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
380 nlh->nlmsg_len = skb->tail - b;
381 nfnetlink_send(skb, 0, group, 0);
389 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
391 static int ctnetlink_done(struct netlink_callback *cb)
393 DEBUGP("entered %s\n", __FUNCTION__);
398 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
400 struct ip_conntrack *ct = NULL;
401 struct ip_conntrack_tuple_hash *h;
403 u_int32_t *id = (u_int32_t *) &cb->args[1];
405 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
408 read_lock_bh(&ip_conntrack_lock);
409 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
410 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
411 h = (struct ip_conntrack_tuple_hash *) i;
412 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
414 ct = tuplehash_to_ctrack(h);
417 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
426 read_unlock_bh(&ip_conntrack_lock);
428 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
433 #ifdef CONFIG_IP_NF_CT_ACCT
435 ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
437 struct ip_conntrack *ct = NULL;
438 struct ip_conntrack_tuple_hash *h;
440 u_int32_t *id = (u_int32_t *) &cb->args[1];
442 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
445 write_lock_bh(&ip_conntrack_lock);
446 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
447 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
448 h = (struct ip_conntrack_tuple_hash *) i;
449 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
451 ct = tuplehash_to_ctrack(h);
454 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
461 memset(&ct->counters, 0, sizeof(ct->counters));
465 write_unlock_bh(&ip_conntrack_lock);
467 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
473 static const int cta_min_ip[CTA_IP_MAX] = {
474 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
475 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
479 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple)
481 struct nfattr *tb[CTA_IP_MAX];
483 DEBUGP("entered %s\n", __FUNCTION__);
485 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
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]);
503 static const int cta_min_proto[CTA_PROTO_MAX] = {
504 [CTA_PROTO_NUM-1] = sizeof(u_int16_t),
505 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
506 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
507 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
508 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
509 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
513 ctnetlink_parse_tuple_proto(struct nfattr *attr,
514 struct ip_conntrack_tuple *tuple)
516 struct nfattr *tb[CTA_PROTO_MAX];
517 struct ip_conntrack_protocol *proto;
520 DEBUGP("entered %s\n", __FUNCTION__);
522 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
524 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
527 if (!tb[CTA_PROTO_NUM-1])
529 tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
531 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
533 if (likely(proto && proto->nfattr_to_tuple)) {
534 ret = proto->nfattr_to_tuple(tb, tuple);
535 ip_conntrack_proto_put(proto);
542 ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
543 enum ctattr_tuple type)
545 struct nfattr *tb[CTA_TUPLE_MAX];
548 DEBUGP("entered %s\n", __FUNCTION__);
550 memset(tuple, 0, sizeof(*tuple));
552 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
554 if (!tb[CTA_TUPLE_IP-1])
557 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
561 if (!tb[CTA_TUPLE_PROTO-1])
564 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
568 /* orig and expect tuples get DIR_ORIGINAL */
569 if (type == CTA_TUPLE_REPLY)
570 tuple->dst.dir = IP_CT_DIR_REPLY;
572 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
581 #ifdef CONFIG_IP_NF_NAT_NEEDED
582 static const int cta_min_protonat[CTA_PROTONAT_MAX] = {
583 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
584 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
587 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
588 const struct ip_conntrack *ct,
589 struct ip_nat_range *range)
591 struct nfattr *tb[CTA_PROTONAT_MAX];
592 struct ip_nat_protocol *npt;
594 DEBUGP("entered %s\n", __FUNCTION__);
596 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
598 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
601 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
605 if (!npt->nfattr_to_range) {
606 ip_nat_proto_put(npt);
610 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
611 if (npt->nfattr_to_range(tb, range) > 0)
612 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
614 ip_nat_proto_put(npt);
621 ctnetlink_parse_nat(struct nfattr *cda[],
622 const struct ip_conntrack *ct, struct ip_nat_range *range)
624 struct nfattr *tb[CTA_NAT_MAX];
627 DEBUGP("entered %s\n", __FUNCTION__);
629 memset(range, 0, sizeof(*range));
631 nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]);
633 if (tb[CTA_NAT_MINIP-1])
634 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
636 if (!tb[CTA_NAT_MAXIP-1])
637 range->max_ip = range->min_ip;
639 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
642 range->flags |= IP_NAT_RANGE_MAP_IPS;
644 if (!tb[CTA_NAT_PROTO-1])
647 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
657 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
659 struct nfattr *tb[CTA_HELP_MAX];
661 DEBUGP("entered %s\n", __FUNCTION__);
663 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
665 if (!tb[CTA_HELP_NAME-1])
668 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
674 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
675 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
677 struct ip_conntrack_tuple_hash *h;
678 struct ip_conntrack_tuple tuple;
679 struct ip_conntrack *ct;
682 DEBUGP("entered %s\n", __FUNCTION__);
684 if (cda[CTA_TUPLE_ORIG-1])
685 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
686 else if (cda[CTA_TUPLE_REPLY-1])
687 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
689 /* Flush the whole table */
690 ip_conntrack_flush();
697 h = ip_conntrack_find_get(&tuple, NULL);
699 DEBUGP("tuple not found in conntrack hash\n");
703 ct = tuplehash_to_ctrack(h);
706 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
708 ip_conntrack_put(ct);
712 if (del_timer(&ct->timeout)) {
713 ip_conntrack_put(ct);
714 ct->timeout.function((unsigned long)ct);
717 ip_conntrack_put(ct);
724 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
725 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
727 struct ip_conntrack_tuple_hash *h;
728 struct ip_conntrack_tuple tuple;
729 struct ip_conntrack *ct;
730 struct sk_buff *skb2 = NULL;
733 DEBUGP("entered %s\n", __FUNCTION__);
735 if (nlh->nlmsg_flags & NLM_F_DUMP) {
736 struct nfgenmsg *msg = NLMSG_DATA(nlh);
739 if (msg->nfgen_family != AF_INET)
740 return -EAFNOSUPPORT;
742 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
743 IPCTNL_MSG_CT_GET_CTRZERO) {
744 #ifdef CONFIG_IP_NF_CT_ACCT
745 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
746 ctnetlink_dump_table_w,
747 ctnetlink_done)) != 0)
753 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
754 ctnetlink_dump_table,
755 ctnetlink_done)) != 0)
759 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
766 if (cda[CTA_TUPLE_ORIG-1])
767 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
768 else if (cda[CTA_TUPLE_REPLY-1])
769 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
776 h = ip_conntrack_find_get(&tuple, NULL);
778 DEBUGP("tuple not found in conntrack hash");
781 DEBUGP("tuple found\n");
782 ct = tuplehash_to_ctrack(h);
785 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
787 ip_conntrack_put(ct);
790 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
792 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
793 IPCTNL_MSG_CT_NEW, 1, ct);
794 ip_conntrack_put(ct);
798 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
812 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
815 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
816 d = ct->status ^ status;
818 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
822 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
823 /* SEEN_REPLY bit can only be set */
827 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
828 /* ASSURED bit can only be set */
831 if (cda[CTA_NAT-1]) {
832 #ifndef CONFIG_IP_NF_NAT_NEEDED
835 unsigned int hooknum;
836 struct ip_nat_range range;
838 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
841 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
842 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
843 htons(range.min.all), htons(range.max.all));
845 /* This is tricky but it works. ip_nat_setup_info needs the
846 * hook number as parameter, so let's do the correct
847 * conversion and run away */
848 if (status & IPS_SRC_NAT_DONE)
849 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
850 else if (status & IPS_DST_NAT_DONE)
851 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
853 return -EINVAL; /* Missing NAT flags */
855 DEBUGP("NAT status: %lu\n",
856 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
858 if (ip_nat_initialized(ct, hooknum))
860 ip_nat_setup_info(ct, &range, hooknum);
862 DEBUGP("NAT status after setup_info: %lu\n",
863 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
867 /* Be careful here, modifying NAT bits can screw up things,
868 * so don't let users modify them directly if they don't pass
870 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
876 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
878 struct ip_conntrack_helper *helper;
882 DEBUGP("entered %s\n", __FUNCTION__);
884 /* don't change helper of sibling connections */
888 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
892 helper = __ip_conntrack_helper_find_byname(helpname);
894 if (!strcmp(helpname, ""))
902 /* we had a helper before ... */
903 ip_ct_remove_expectations(ct);
906 /* need to zero data of old helper */
907 memset(&ct->help, 0, sizeof(ct->help));
917 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
919 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
921 if (!del_timer(&ct->timeout))
924 ct->timeout.expires = jiffies + timeout * HZ;
925 add_timer(&ct->timeout);
931 ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
933 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
934 struct ip_conntrack_protocol *proto;
935 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
938 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
940 proto = ip_conntrack_proto_find_get(npt);
944 if (proto->from_nfattr)
945 err = proto->from_nfattr(tb, ct);
946 ip_conntrack_proto_put(proto);
952 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
956 DEBUGP("entered %s\n", __FUNCTION__);
958 if (cda[CTA_HELP-1]) {
959 err = ctnetlink_change_helper(ct, cda);
964 if (cda[CTA_TIMEOUT-1]) {
965 err = ctnetlink_change_timeout(ct, cda);
970 if (cda[CTA_STATUS-1]) {
971 err = ctnetlink_change_status(ct, cda);
976 if (cda[CTA_PROTOINFO-1]) {
977 err = ctnetlink_change_protoinfo(ct, cda);
982 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
984 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
987 DEBUGP("all done\n");
992 ctnetlink_create_conntrack(struct nfattr *cda[],
993 struct ip_conntrack_tuple *otuple,
994 struct ip_conntrack_tuple *rtuple)
996 struct ip_conntrack *ct;
999 DEBUGP("entered %s\n", __FUNCTION__);
1001 ct = ip_conntrack_alloc(otuple, rtuple);
1002 if (ct == NULL || IS_ERR(ct))
1005 if (!cda[CTA_TIMEOUT-1])
1007 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1009 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1010 ct->status |= IPS_CONFIRMED;
1012 err = ctnetlink_change_status(ct, cda);
1016 if (cda[CTA_PROTOINFO-1]) {
1017 err = ctnetlink_change_protoinfo(ct, cda);
1022 ct->helper = ip_conntrack_helper_find_get(rtuple);
1024 add_timer(&ct->timeout);
1025 ip_conntrack_hash_insert(ct);
1028 ip_conntrack_helper_put(ct->helper);
1030 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1031 if (cda[CTA_MARK-1])
1032 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1035 DEBUGP("conntrack with id %u inserted\n", ct->id);
1039 ip_conntrack_free(ct);
1044 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1045 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1047 struct ip_conntrack_tuple otuple, rtuple;
1048 struct ip_conntrack_tuple_hash *h = NULL;
1051 DEBUGP("entered %s\n", __FUNCTION__);
1053 if (cda[CTA_TUPLE_ORIG-1]) {
1054 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1059 if (cda[CTA_TUPLE_REPLY-1]) {
1060 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1065 write_lock_bh(&ip_conntrack_lock);
1066 if (cda[CTA_TUPLE_ORIG-1])
1067 h = __ip_conntrack_find(&otuple, NULL);
1068 else if (cda[CTA_TUPLE_REPLY-1])
1069 h = __ip_conntrack_find(&rtuple, NULL);
1072 write_unlock_bh(&ip_conntrack_lock);
1073 DEBUGP("no such conntrack, create new\n");
1075 if (nlh->nlmsg_flags & NLM_F_CREATE)
1076 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1079 /* implicit 'else' */
1081 /* we only allow nat config for new conntracks */
1082 if (cda[CTA_NAT-1]) {
1087 /* We manipulate the conntrack inside the global conntrack table lock,
1088 * so there's no need to increase the refcount */
1089 DEBUGP("conntrack found\n");
1091 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1092 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1095 write_unlock_bh(&ip_conntrack_lock);
1099 /***********************************************************************
1101 ***********************************************************************/
1104 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1105 const struct ip_conntrack_tuple *tuple,
1106 enum ctattr_expect type)
1108 struct nfattr *nest_parms = NFA_NEST(skb, type);
1110 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1111 goto nfattr_failure;
1113 NFA_NEST_END(skb, nest_parms);
1122 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1123 const struct ip_conntrack_expect *exp)
1125 struct ip_conntrack *master = exp->master;
1126 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1127 u_int32_t id = htonl(exp->id);
1129 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1130 goto nfattr_failure;
1131 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1132 goto nfattr_failure;
1133 if (ctnetlink_exp_dump_tuple(skb,
1134 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1135 CTA_EXPECT_MASTER) < 0)
1136 goto nfattr_failure;
1138 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1139 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1148 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1151 const struct ip_conntrack_expect *exp)
1153 struct nlmsghdr *nlh;
1154 struct nfgenmsg *nfmsg;
1159 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1160 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1161 nfmsg = NLMSG_DATA(nlh);
1163 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1164 nfmsg->nfgen_family = AF_INET;
1165 nfmsg->version = NFNETLINK_V0;
1168 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1169 goto nfattr_failure;
1171 nlh->nlmsg_len = skb->tail - b;
1176 skb_trim(skb, b - skb->data);
1180 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1181 static int ctnetlink_expect_event(struct notifier_block *this,
1182 unsigned long events, void *ptr)
1184 struct nlmsghdr *nlh;
1185 struct nfgenmsg *nfmsg;
1186 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1187 struct sk_buff *skb;
1193 if (events & IPEXP_NEW) {
1194 type = IPCTNL_MSG_EXP_NEW;
1195 flags = NLM_F_CREATE|NLM_F_EXCL;
1199 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1205 type |= NFNL_SUBSYS_CTNETLINK << 8;
1206 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1207 nfmsg = NLMSG_DATA(nlh);
1209 nlh->nlmsg_flags = flags;
1210 nfmsg->nfgen_family = AF_INET;
1211 nfmsg->version = NFNETLINK_V0;
1214 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1215 goto nfattr_failure;
1217 nlh->nlmsg_len = skb->tail - b;
1218 proto = exp->tuple.dst.protonum;
1219 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1230 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1232 struct ip_conntrack_expect *exp = NULL;
1233 struct list_head *i;
1234 u_int32_t *id = (u_int32_t *) &cb->args[0];
1236 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1238 read_lock_bh(&ip_conntrack_lock);
1239 list_for_each_prev(i, &ip_conntrack_expect_list) {
1240 exp = (struct ip_conntrack_expect *) i;
1243 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1251 read_unlock_bh(&ip_conntrack_lock);
1253 DEBUGP("leaving, last id=%llu\n", *id);
1259 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1260 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1262 struct ip_conntrack_tuple tuple;
1263 struct ip_conntrack_expect *exp;
1264 struct sk_buff *skb2;
1267 DEBUGP("entered %s\n", __FUNCTION__);
1269 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1270 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1273 if (msg->nfgen_family != AF_INET)
1274 return -EAFNOSUPPORT;
1276 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1277 ctnetlink_exp_dump_table,
1278 ctnetlink_done)) != 0)
1280 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1281 if (rlen > skb->len)
1283 skb_pull(skb, rlen);
1287 if (cda[CTA_EXPECT_MASTER-1])
1288 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
1295 exp = ip_conntrack_expect_find(&tuple);
1300 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1303 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1305 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1306 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1311 ip_conntrack_expect_put(exp);
1313 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1318 ip_conntrack_expect_put(exp);
1323 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1324 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1326 struct ip_conntrack_expect *exp, *tmp;
1327 struct ip_conntrack_tuple tuple;
1328 struct ip_conntrack_helper *h;
1331 if (cda[CTA_EXPECT_TUPLE-1]) {
1332 /* delete a single expect by tuple */
1333 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1337 /* bump usage count to 2 */
1338 exp = ip_conntrack_expect_find(&tuple);
1342 if (cda[CTA_EXPECT_ID-1]) {
1344 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1345 if (exp->id != ntohl(id)) {
1346 ip_conntrack_expect_put(exp);
1351 /* after list removal, usage count == 1 */
1352 ip_conntrack_unexpect_related(exp);
1353 /* have to put what we 'get' above.
1354 * after this line usage count == 0 */
1355 ip_conntrack_expect_put(exp);
1356 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1357 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1359 /* delete all expectations for this helper */
1360 write_lock_bh(&ip_conntrack_lock);
1361 h = __ip_conntrack_helper_find_byname(name);
1363 write_unlock_bh(&ip_conntrack_lock);
1366 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1368 if (exp->master->helper == h
1369 && del_timer(&exp->timeout)) {
1370 ip_ct_unlink_expect(exp);
1371 ip_conntrack_expect_put(exp);
1374 write_unlock(&ip_conntrack_lock);
1376 /* This basically means we have to flush everything*/
1377 write_lock_bh(&ip_conntrack_lock);
1378 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1380 if (del_timer(&exp->timeout)) {
1381 ip_ct_unlink_expect(exp);
1382 ip_conntrack_expect_put(exp);
1385 write_unlock_bh(&ip_conntrack_lock);
1391 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1397 ctnetlink_create_expect(struct nfattr *cda[])
1399 struct ip_conntrack_tuple tuple, mask, master_tuple;
1400 struct ip_conntrack_tuple_hash *h = NULL;
1401 struct ip_conntrack_expect *exp;
1402 struct ip_conntrack *ct;
1405 DEBUGP("entered %s\n", __FUNCTION__);
1407 /* caller guarantees that those three CTA_EXPECT_* exist */
1408 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1411 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
1414 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
1418 /* Look for master conntrack of this expectation */
1419 h = ip_conntrack_find_get(&master_tuple, NULL);
1422 ct = tuplehash_to_ctrack(h);
1425 /* such conntrack hasn't got any helper, abort */
1430 exp = ip_conntrack_expect_alloc(ct);
1436 exp->expectfn = NULL;
1439 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1440 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1442 err = ip_conntrack_expect_related(exp);
1443 ip_conntrack_expect_put(exp);
1446 ip_conntrack_put(tuplehash_to_ctrack(h));
1451 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1452 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1454 struct ip_conntrack_tuple tuple;
1455 struct ip_conntrack_expect *exp;
1458 DEBUGP("entered %s\n", __FUNCTION__);
1460 if (!cda[CTA_EXPECT_TUPLE-1]
1461 || !cda[CTA_EXPECT_MASK-1]
1462 || !cda[CTA_EXPECT_MASTER-1])
1465 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1469 write_lock_bh(&ip_conntrack_lock);
1470 exp = __ip_conntrack_expect_find(&tuple);
1473 write_unlock_bh(&ip_conntrack_lock);
1475 if (nlh->nlmsg_flags & NLM_F_CREATE)
1476 err = ctnetlink_create_expect(cda);
1481 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1482 err = ctnetlink_change_expect(exp, cda);
1483 write_unlock_bh(&ip_conntrack_lock);
1485 DEBUGP("leaving\n");
1490 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1491 static struct notifier_block ctnl_notifier = {
1492 .notifier_call = ctnetlink_conntrack_event,
1495 static struct notifier_block ctnl_notifier_exp = {
1496 .notifier_call = ctnetlink_expect_event,
1500 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1501 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1502 .attr_count = CTA_MAX,
1503 .cap_required = CAP_NET_ADMIN },
1504 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1505 .attr_count = CTA_MAX,
1506 .cap_required = CAP_NET_ADMIN },
1507 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1508 .attr_count = CTA_MAX,
1509 .cap_required = CAP_NET_ADMIN },
1510 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1511 .attr_count = CTA_MAX,
1512 .cap_required = CAP_NET_ADMIN },
1515 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1516 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1517 .attr_count = CTA_EXPECT_MAX,
1518 .cap_required = CAP_NET_ADMIN },
1519 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1520 .attr_count = CTA_EXPECT_MAX,
1521 .cap_required = CAP_NET_ADMIN },
1522 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1523 .attr_count = CTA_EXPECT_MAX,
1524 .cap_required = CAP_NET_ADMIN },
1527 static struct nfnetlink_subsystem ctnl_subsys = {
1528 .name = "conntrack",
1529 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1530 .cb_count = IPCTNL_MSG_MAX,
1534 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1535 .name = "conntrack_expect",
1536 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1537 .cb_count = IPCTNL_MSG_EXP_MAX,
1541 static int __init ctnetlink_init(void)
1545 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1546 ret = nfnetlink_subsys_register(&ctnl_subsys);
1548 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1552 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1554 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1555 goto err_unreg_subsys;
1558 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1559 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1561 printk("ctnetlink_init: cannot register notifier.\n");
1562 goto err_unreg_exp_subsys;
1565 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1567 printk("ctnetlink_init: cannot expect register notifier.\n");
1568 goto err_unreg_notifier;
1574 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1576 ip_conntrack_unregister_notifier(&ctnl_notifier);
1577 err_unreg_exp_subsys:
1578 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1581 nfnetlink_subsys_unregister(&ctnl_subsys);
1586 static void __exit ctnetlink_exit(void)
1588 printk("ctnetlink: unregistering from nfnetlink.\n");
1590 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1591 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1592 ip_conntrack_unregister_notifier(&ctnl_notifier);
1595 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1596 nfnetlink_subsys_unregister(&ctnl_subsys);
1600 module_init(ctnetlink_init);
1601 module_exit(ctnetlink_exit);