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__);
486 if (nfattr_parse_nested(tb, CTA_IP_MAX, attr) < 0)
489 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
492 if (!tb[CTA_IP_V4_SRC-1])
494 tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
496 if (!tb[CTA_IP_V4_DST-1])
498 tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
508 static const int cta_min_proto[CTA_PROTO_MAX] = {
509 [CTA_PROTO_NUM-1] = sizeof(u_int16_t),
510 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
511 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
512 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
513 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
514 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
518 ctnetlink_parse_tuple_proto(struct nfattr *attr,
519 struct ip_conntrack_tuple *tuple)
521 struct nfattr *tb[CTA_PROTO_MAX];
522 struct ip_conntrack_protocol *proto;
525 DEBUGP("entered %s\n", __FUNCTION__);
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(tuple, 0, sizeof(*tuple));
561 if (nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]) < 0)
564 if (!tb[CTA_TUPLE_IP-1])
567 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
571 if (!tb[CTA_TUPLE_PROTO-1])
574 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
578 /* orig and expect tuples get DIR_ORIGINAL */
579 if (type == CTA_TUPLE_REPLY)
580 tuple->dst.dir = IP_CT_DIR_REPLY;
582 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
594 #ifdef CONFIG_IP_NF_NAT_NEEDED
595 static const int cta_min_protonat[CTA_PROTONAT_MAX] = {
596 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
597 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
600 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
601 const struct ip_conntrack *ct,
602 struct ip_nat_range *range)
604 struct nfattr *tb[CTA_PROTONAT_MAX];
605 struct ip_nat_protocol *npt;
607 DEBUGP("entered %s\n", __FUNCTION__);
609 if (nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr) < 0)
612 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
615 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
619 if (!npt->nfattr_to_range) {
620 ip_nat_proto_put(npt);
624 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
625 if (npt->nfattr_to_range(tb, range) > 0)
626 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
628 ip_nat_proto_put(npt);
638 ctnetlink_parse_nat(struct nfattr *cda[],
639 const struct ip_conntrack *ct, struct ip_nat_range *range)
641 struct nfattr *tb[CTA_NAT_MAX];
644 DEBUGP("entered %s\n", __FUNCTION__);
646 memset(range, 0, sizeof(*range));
648 if (nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]) < 0)
651 if (tb[CTA_NAT_MINIP-1])
652 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
654 if (!tb[CTA_NAT_MAXIP-1])
655 range->max_ip = range->min_ip;
657 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
660 range->flags |= IP_NAT_RANGE_MAP_IPS;
662 if (!tb[CTA_NAT_PROTO-1])
665 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
678 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
680 struct nfattr *tb[CTA_HELP_MAX];
682 DEBUGP("entered %s\n", __FUNCTION__);
684 if (nfattr_parse_nested(tb, CTA_HELP_MAX, attr) < 0)
687 if (!tb[CTA_HELP_NAME-1])
690 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
699 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
700 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
702 struct ip_conntrack_tuple_hash *h;
703 struct ip_conntrack_tuple tuple;
704 struct ip_conntrack *ct;
707 DEBUGP("entered %s\n", __FUNCTION__);
709 if (cda[CTA_TUPLE_ORIG-1])
710 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
711 else if (cda[CTA_TUPLE_REPLY-1])
712 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
714 /* Flush the whole table */
715 ip_conntrack_flush();
722 h = ip_conntrack_find_get(&tuple, NULL);
724 DEBUGP("tuple not found in conntrack hash\n");
728 ct = tuplehash_to_ctrack(h);
731 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
733 ip_conntrack_put(ct);
737 if (del_timer(&ct->timeout)) {
738 ip_conntrack_put(ct);
739 ct->timeout.function((unsigned long)ct);
742 ip_conntrack_put(ct);
749 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
750 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
752 struct ip_conntrack_tuple_hash *h;
753 struct ip_conntrack_tuple tuple;
754 struct ip_conntrack *ct;
755 struct sk_buff *skb2 = NULL;
758 DEBUGP("entered %s\n", __FUNCTION__);
760 if (nlh->nlmsg_flags & NLM_F_DUMP) {
761 struct nfgenmsg *msg = NLMSG_DATA(nlh);
764 if (msg->nfgen_family != AF_INET)
765 return -EAFNOSUPPORT;
767 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
768 IPCTNL_MSG_CT_GET_CTRZERO) {
769 #ifdef CONFIG_IP_NF_CT_ACCT
770 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
771 ctnetlink_dump_table_w,
772 ctnetlink_done)) != 0)
778 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
779 ctnetlink_dump_table,
780 ctnetlink_done)) != 0)
784 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
791 if (cda[CTA_TUPLE_ORIG-1])
792 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
793 else if (cda[CTA_TUPLE_REPLY-1])
794 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
801 h = ip_conntrack_find_get(&tuple, NULL);
803 DEBUGP("tuple not found in conntrack hash");
806 DEBUGP("tuple found\n");
807 ct = tuplehash_to_ctrack(h);
810 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
812 ip_conntrack_put(ct);
815 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
817 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
818 IPCTNL_MSG_CT_NEW, 1, ct);
819 ip_conntrack_put(ct);
823 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
837 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
840 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
841 d = ct->status ^ status;
843 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
847 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
848 /* SEEN_REPLY bit can only be set */
852 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
853 /* ASSURED bit can only be set */
856 if (cda[CTA_NAT-1]) {
857 #ifndef CONFIG_IP_NF_NAT_NEEDED
860 unsigned int hooknum;
861 struct ip_nat_range range;
863 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
866 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
867 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
868 htons(range.min.all), htons(range.max.all));
870 /* This is tricky but it works. ip_nat_setup_info needs the
871 * hook number as parameter, so let's do the correct
872 * conversion and run away */
873 if (status & IPS_SRC_NAT_DONE)
874 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
875 else if (status & IPS_DST_NAT_DONE)
876 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
878 return -EINVAL; /* Missing NAT flags */
880 DEBUGP("NAT status: %lu\n",
881 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
883 if (ip_nat_initialized(ct, hooknum))
885 ip_nat_setup_info(ct, &range, hooknum);
887 DEBUGP("NAT status after setup_info: %lu\n",
888 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
892 /* Be careful here, modifying NAT bits can screw up things,
893 * so don't let users modify them directly if they don't pass
895 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
901 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
903 struct ip_conntrack_helper *helper;
907 DEBUGP("entered %s\n", __FUNCTION__);
909 /* don't change helper of sibling connections */
913 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
917 helper = __ip_conntrack_helper_find_byname(helpname);
919 if (!strcmp(helpname, ""))
927 /* we had a helper before ... */
928 ip_ct_remove_expectations(ct);
931 /* need to zero data of old helper */
932 memset(&ct->help, 0, sizeof(ct->help));
942 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
944 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
946 if (!del_timer(&ct->timeout))
949 ct->timeout.expires = jiffies + timeout * HZ;
950 add_timer(&ct->timeout);
956 ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
958 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
959 struct ip_conntrack_protocol *proto;
960 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
963 if (nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr) < 0)
966 proto = ip_conntrack_proto_find_get(npt);
970 if (proto->from_nfattr)
971 err = proto->from_nfattr(tb, ct);
972 ip_conntrack_proto_put(proto);
981 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
985 DEBUGP("entered %s\n", __FUNCTION__);
987 if (cda[CTA_HELP-1]) {
988 err = ctnetlink_change_helper(ct, cda);
993 if (cda[CTA_TIMEOUT-1]) {
994 err = ctnetlink_change_timeout(ct, cda);
999 if (cda[CTA_STATUS-1]) {
1000 err = ctnetlink_change_status(ct, cda);
1005 if (cda[CTA_PROTOINFO-1]) {
1006 err = ctnetlink_change_protoinfo(ct, cda);
1011 DEBUGP("all done\n");
1016 ctnetlink_create_conntrack(struct nfattr *cda[],
1017 struct ip_conntrack_tuple *otuple,
1018 struct ip_conntrack_tuple *rtuple)
1020 struct ip_conntrack *ct;
1023 DEBUGP("entered %s\n", __FUNCTION__);
1025 ct = ip_conntrack_alloc(otuple, rtuple);
1026 if (ct == NULL || IS_ERR(ct))
1029 if (!cda[CTA_TIMEOUT-1])
1031 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1033 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1034 ct->status |= IPS_CONFIRMED;
1036 err = ctnetlink_change_status(ct, cda);
1040 if (cda[CTA_PROTOINFO-1]) {
1041 err = ctnetlink_change_protoinfo(ct, cda);
1046 ct->helper = ip_conntrack_helper_find_get(rtuple);
1048 add_timer(&ct->timeout);
1049 ip_conntrack_hash_insert(ct);
1052 ip_conntrack_helper_put(ct->helper);
1054 DEBUGP("conntrack with id %u inserted\n", ct->id);
1058 ip_conntrack_free(ct);
1063 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1064 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1066 struct ip_conntrack_tuple otuple, rtuple;
1067 struct ip_conntrack_tuple_hash *h = NULL;
1070 DEBUGP("entered %s\n", __FUNCTION__);
1072 if (cda[CTA_TUPLE_ORIG-1]) {
1073 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1078 if (cda[CTA_TUPLE_REPLY-1]) {
1079 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1084 write_lock_bh(&ip_conntrack_lock);
1085 if (cda[CTA_TUPLE_ORIG-1])
1086 h = __ip_conntrack_find(&otuple, NULL);
1087 else if (cda[CTA_TUPLE_REPLY-1])
1088 h = __ip_conntrack_find(&rtuple, NULL);
1091 write_unlock_bh(&ip_conntrack_lock);
1092 DEBUGP("no such conntrack, create new\n");
1094 if (nlh->nlmsg_flags & NLM_F_CREATE)
1095 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1098 /* implicit 'else' */
1100 /* we only allow nat config for new conntracks */
1101 if (cda[CTA_NAT-1]) {
1106 /* We manipulate the conntrack inside the global conntrack table lock,
1107 * so there's no need to increase the refcount */
1108 DEBUGP("conntrack found\n");
1110 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1111 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1114 write_unlock_bh(&ip_conntrack_lock);
1118 /***********************************************************************
1120 ***********************************************************************/
1123 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1124 const struct ip_conntrack_tuple *tuple,
1125 enum ctattr_expect type)
1127 struct nfattr *nest_parms = NFA_NEST(skb, type);
1129 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1130 goto nfattr_failure;
1132 NFA_NEST_END(skb, nest_parms);
1141 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1142 const struct ip_conntrack_expect *exp)
1144 struct ip_conntrack *master = exp->master;
1145 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1146 u_int32_t id = htonl(exp->id);
1148 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1149 goto nfattr_failure;
1150 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1151 goto nfattr_failure;
1152 if (ctnetlink_exp_dump_tuple(skb,
1153 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1154 CTA_EXPECT_MASTER) < 0)
1155 goto nfattr_failure;
1157 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1158 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1167 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1170 const struct ip_conntrack_expect *exp)
1172 struct nlmsghdr *nlh;
1173 struct nfgenmsg *nfmsg;
1178 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1179 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1180 nfmsg = NLMSG_DATA(nlh);
1182 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1183 nfmsg->nfgen_family = AF_INET;
1184 nfmsg->version = NFNETLINK_V0;
1187 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1188 goto nfattr_failure;
1190 nlh->nlmsg_len = skb->tail - b;
1195 skb_trim(skb, b - skb->data);
1199 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1200 static int ctnetlink_expect_event(struct notifier_block *this,
1201 unsigned long events, void *ptr)
1203 struct nlmsghdr *nlh;
1204 struct nfgenmsg *nfmsg;
1205 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1206 struct sk_buff *skb;
1212 if (events & IPEXP_NEW) {
1213 type = IPCTNL_MSG_EXP_NEW;
1214 flags = NLM_F_CREATE|NLM_F_EXCL;
1218 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1224 type |= NFNL_SUBSYS_CTNETLINK << 8;
1225 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1226 nfmsg = NLMSG_DATA(nlh);
1228 nlh->nlmsg_flags = flags;
1229 nfmsg->nfgen_family = AF_INET;
1230 nfmsg->version = NFNETLINK_V0;
1233 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1234 goto nfattr_failure;
1236 nlh->nlmsg_len = skb->tail - b;
1237 proto = exp->tuple.dst.protonum;
1238 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1249 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1251 struct ip_conntrack_expect *exp = NULL;
1252 struct list_head *i;
1253 u_int32_t *id = (u_int32_t *) &cb->args[0];
1255 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1257 read_lock_bh(&ip_conntrack_lock);
1258 list_for_each_prev(i, &ip_conntrack_expect_list) {
1259 exp = (struct ip_conntrack_expect *) i;
1262 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1270 read_unlock_bh(&ip_conntrack_lock);
1272 DEBUGP("leaving, last id=%llu\n", *id);
1278 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1279 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1281 struct ip_conntrack_tuple tuple;
1282 struct ip_conntrack_expect *exp;
1283 struct sk_buff *skb2;
1286 DEBUGP("entered %s\n", __FUNCTION__);
1288 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1289 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1292 if (msg->nfgen_family != AF_INET)
1293 return -EAFNOSUPPORT;
1295 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1296 ctnetlink_exp_dump_table,
1297 ctnetlink_done)) != 0)
1299 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1300 if (rlen > skb->len)
1302 skb_pull(skb, rlen);
1306 if (cda[CTA_EXPECT_MASTER-1])
1307 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
1314 exp = ip_conntrack_expect_find(&tuple);
1319 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1322 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1324 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1325 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1330 ip_conntrack_expect_put(exp);
1332 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1337 ip_conntrack_expect_put(exp);
1342 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1343 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1345 struct ip_conntrack_expect *exp, *tmp;
1346 struct ip_conntrack_tuple tuple;
1347 struct ip_conntrack_helper *h;
1350 if (cda[CTA_EXPECT_TUPLE-1]) {
1351 /* delete a single expect by tuple */
1352 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1356 /* bump usage count to 2 */
1357 exp = ip_conntrack_expect_find(&tuple);
1361 if (cda[CTA_EXPECT_ID-1]) {
1363 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1364 if (exp->id != ntohl(id)) {
1365 ip_conntrack_expect_put(exp);
1370 /* after list removal, usage count == 1 */
1371 ip_conntrack_unexpect_related(exp);
1372 /* have to put what we 'get' above.
1373 * after this line usage count == 0 */
1374 ip_conntrack_expect_put(exp);
1375 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1376 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1378 /* delete all expectations for this helper */
1379 write_lock_bh(&ip_conntrack_lock);
1380 h = __ip_conntrack_helper_find_byname(name);
1382 write_unlock_bh(&ip_conntrack_lock);
1385 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1387 if (exp->master->helper == h
1388 && del_timer(&exp->timeout)) {
1389 ip_ct_unlink_expect(exp);
1390 ip_conntrack_expect_put(exp);
1393 write_unlock(&ip_conntrack_lock);
1395 /* This basically means we have to flush everything*/
1396 write_lock_bh(&ip_conntrack_lock);
1397 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1399 if (del_timer(&exp->timeout)) {
1400 ip_ct_unlink_expect(exp);
1401 ip_conntrack_expect_put(exp);
1404 write_unlock_bh(&ip_conntrack_lock);
1410 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1416 ctnetlink_create_expect(struct nfattr *cda[])
1418 struct ip_conntrack_tuple tuple, mask, master_tuple;
1419 struct ip_conntrack_tuple_hash *h = NULL;
1420 struct ip_conntrack_expect *exp;
1421 struct ip_conntrack *ct;
1424 DEBUGP("entered %s\n", __FUNCTION__);
1426 /* caller guarantees that those three CTA_EXPECT_* exist */
1427 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1430 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
1433 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
1437 /* Look for master conntrack of this expectation */
1438 h = ip_conntrack_find_get(&master_tuple, NULL);
1441 ct = tuplehash_to_ctrack(h);
1444 /* such conntrack hasn't got any helper, abort */
1449 exp = ip_conntrack_expect_alloc(ct);
1455 exp->expectfn = NULL;
1458 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1459 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1461 err = ip_conntrack_expect_related(exp);
1462 ip_conntrack_expect_put(exp);
1465 ip_conntrack_put(tuplehash_to_ctrack(h));
1470 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1471 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1473 struct ip_conntrack_tuple tuple;
1474 struct ip_conntrack_expect *exp;
1477 DEBUGP("entered %s\n", __FUNCTION__);
1479 if (!cda[CTA_EXPECT_TUPLE-1]
1480 || !cda[CTA_EXPECT_MASK-1]
1481 || !cda[CTA_EXPECT_MASTER-1])
1484 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1488 write_lock_bh(&ip_conntrack_lock);
1489 exp = __ip_conntrack_expect_find(&tuple);
1492 write_unlock_bh(&ip_conntrack_lock);
1494 if (nlh->nlmsg_flags & NLM_F_CREATE)
1495 err = ctnetlink_create_expect(cda);
1500 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1501 err = ctnetlink_change_expect(exp, cda);
1502 write_unlock_bh(&ip_conntrack_lock);
1504 DEBUGP("leaving\n");
1509 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1510 static struct notifier_block ctnl_notifier = {
1511 .notifier_call = ctnetlink_conntrack_event,
1514 static struct notifier_block ctnl_notifier_exp = {
1515 .notifier_call = ctnetlink_expect_event,
1519 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1520 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1521 .attr_count = CTA_MAX,
1522 .cap_required = CAP_NET_ADMIN },
1523 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1524 .attr_count = CTA_MAX,
1525 .cap_required = CAP_NET_ADMIN },
1526 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1527 .attr_count = CTA_MAX,
1528 .cap_required = CAP_NET_ADMIN },
1529 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1530 .attr_count = CTA_MAX,
1531 .cap_required = CAP_NET_ADMIN },
1534 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1535 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1536 .attr_count = CTA_EXPECT_MAX,
1537 .cap_required = CAP_NET_ADMIN },
1538 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1539 .attr_count = CTA_EXPECT_MAX,
1540 .cap_required = CAP_NET_ADMIN },
1541 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1542 .attr_count = CTA_EXPECT_MAX,
1543 .cap_required = CAP_NET_ADMIN },
1546 static struct nfnetlink_subsystem ctnl_subsys = {
1547 .name = "conntrack",
1548 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1549 .cb_count = IPCTNL_MSG_MAX,
1553 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1554 .name = "conntrack_expect",
1555 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1556 .cb_count = IPCTNL_MSG_EXP_MAX,
1560 static int __init ctnetlink_init(void)
1564 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1565 ret = nfnetlink_subsys_register(&ctnl_subsys);
1567 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1571 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1573 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1574 goto err_unreg_subsys;
1577 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1578 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1580 printk("ctnetlink_init: cannot register notifier.\n");
1581 goto err_unreg_exp_subsys;
1584 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1586 printk("ctnetlink_init: cannot expect register notifier.\n");
1587 goto err_unreg_notifier;
1593 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1595 ip_conntrack_unregister_notifier(&ctnl_notifier);
1596 err_unreg_exp_subsys:
1597 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1600 nfnetlink_subsys_unregister(&ctnl_subsys);
1605 static void __exit ctnetlink_exit(void)
1607 printk("ctnetlink: unregistering from nfnetlink.\n");
1609 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1610 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1611 ip_conntrack_unregister_notifier(&ctnl_notifier);
1614 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1615 nfnetlink_subsys_unregister(&ctnl_subsys);
1619 module_init(ctnetlink_init);
1620 module_exit(ctnetlink_exit);