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-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
9 * I've reworked this stuff to use attributes instead of conntrack
10 * structures. 5.44 am. I need more tea. --pablo 05/07/11.
12 * Initial connection tracking via netlink development funded and
13 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
15 * Further development of this code funded by Astaro AG (http://www.astaro.com)
17 * This software may be used and distributed according to the terms
18 * of the GNU General Public License, incorporated herein by reference.
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/interrupt.h>
31 #include <linux/notifier.h>
33 #include <linux/netfilter.h>
34 #include <linux/netfilter_ipv4/ip_conntrack.h>
35 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
36 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
37 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
38 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
40 #include <linux/netfilter/nfnetlink.h>
41 #include <linux/netfilter/nfnetlink_conntrack.h>
43 MODULE_LICENSE("GPL");
45 static char __initdata version[] = "0.90";
50 #define DEBUGP(format, args...)
55 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
56 const struct ip_conntrack_tuple *tuple,
57 struct ip_conntrack_protocol *proto)
60 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
62 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
64 if (likely(proto->tuple_to_nfattr))
65 ret = proto->tuple_to_nfattr(skb, tuple);
67 NFA_NEST_END(skb, nest_parms);
76 ctnetlink_dump_tuples_ip(struct sk_buff *skb,
77 const struct ip_conntrack_tuple *tuple)
79 struct nfattr *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);
84 NFA_NEST_END(skb, nest_parms);
93 ctnetlink_dump_tuples(struct sk_buff *skb,
94 const struct ip_conntrack_tuple *tuple)
97 struct ip_conntrack_protocol *proto;
99 ret = ctnetlink_dump_tuples_ip(skb, tuple);
100 if (unlikely(ret < 0))
103 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
104 ret = ctnetlink_dump_tuples_proto(skb, tuple, proto);
105 ip_conntrack_proto_put(proto);
111 ctnetlink_dump_status(struct sk_buff *skb, const struct ip_conntrack *ct)
113 u_int32_t status = htonl((u_int32_t) ct->status);
114 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
122 ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct)
124 long timeout_l = ct->timeout.expires - jiffies;
130 timeout = htonl(timeout_l / HZ);
132 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
140 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
142 struct ip_conntrack_protocol *proto = ip_conntrack_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
144 struct nfattr *nest_proto;
147 if (!proto->to_nfattr) {
148 ip_conntrack_proto_put(proto);
152 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
154 ret = proto->to_nfattr(skb, nest_proto, ct);
156 ip_conntrack_proto_put(proto);
158 NFA_NEST_END(skb, nest_proto);
167 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
169 struct nfattr *nest_helper;
174 nest_helper = NFA_NEST(skb, CTA_HELP);
175 NFA_PUT(skb, CTA_HELP_NAME, strlen(ct->helper->name), ct->helper->name);
177 if (ct->helper->to_nfattr)
178 ct->helper->to_nfattr(skb, ct);
180 NFA_NEST_END(skb, nest_helper);
188 #ifdef CONFIG_IP_NF_CT_ACCT
190 ctnetlink_dump_counters(struct sk_buff *skb, const struct ip_conntrack *ct,
191 enum ip_conntrack_dir dir)
193 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
194 struct nfattr *nest_count = NFA_NEST(skb, type);
197 tmp = htonl(ct->counters[dir].packets);
198 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
200 tmp = htonl(ct->counters[dir].bytes);
201 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
203 NFA_NEST_END(skb, nest_count);
211 #define ctnetlink_dump_counters(a, b, c) (0)
214 #ifdef CONFIG_IP_NF_CONNTRACK_MARK
216 ctnetlink_dump_mark(struct sk_buff *skb, const struct ip_conntrack *ct)
218 u_int32_t mark = htonl(ct->mark);
220 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
227 #define ctnetlink_dump_mark(a, b) (0)
231 ctnetlink_dump_id(struct sk_buff *skb, const struct ip_conntrack *ct)
233 u_int32_t id = htonl(ct->id);
234 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
242 ctnetlink_dump_use(struct sk_buff *skb, const struct ip_conntrack *ct)
244 u_int32_t use = htonl(atomic_read(&ct->ct_general.use));
246 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
253 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
256 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
257 int event, int nowait,
258 const struct ip_conntrack *ct)
260 struct nlmsghdr *nlh;
261 struct nfgenmsg *nfmsg;
262 struct nfattr *nest_parms;
267 event |= NFNL_SUBSYS_CTNETLINK << 8;
268 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
269 nfmsg = NLMSG_DATA(nlh);
271 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
272 nfmsg->nfgen_family = AF_INET;
273 nfmsg->version = NFNETLINK_V0;
276 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
277 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
279 NFA_NEST_END(skb, nest_parms);
281 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
282 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
284 NFA_NEST_END(skb, nest_parms);
286 if (ctnetlink_dump_status(skb, ct) < 0 ||
287 ctnetlink_dump_timeout(skb, ct) < 0 ||
288 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
289 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
290 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
291 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
292 ctnetlink_dump_mark(skb, ct) < 0 ||
293 ctnetlink_dump_id(skb, ct) < 0 ||
294 ctnetlink_dump_use(skb, ct) < 0)
297 nlh->nlmsg_len = skb->tail - b;
302 skb_trim(skb, b - skb->data);
306 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
307 static int ctnetlink_conntrack_event(struct notifier_block *this,
308 unsigned long events, void *ptr)
310 struct nlmsghdr *nlh;
311 struct nfgenmsg *nfmsg;
312 struct nfattr *nest_parms;
313 struct ip_conntrack *ct = (struct ip_conntrack *)ptr;
317 unsigned int flags = 0, group;
319 /* ignore our fake conntrack entry */
320 if (ct == &ip_conntrack_untracked)
323 if (events & IPCT_DESTROY) {
324 type = IPCTNL_MSG_CT_DELETE;
325 group = NFNLGRP_CONNTRACK_DESTROY;
326 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
327 type = IPCTNL_MSG_CT_NEW;
328 flags = NLM_F_CREATE|NLM_F_EXCL;
329 /* dump everything */
331 group = NFNLGRP_CONNTRACK_NEW;
332 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
333 type = IPCTNL_MSG_CT_NEW;
334 group = NFNLGRP_CONNTRACK_UPDATE;
338 if (!nfnetlink_has_listeners(group))
341 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
347 type |= NFNL_SUBSYS_CTNETLINK << 8;
348 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
349 nfmsg = NLMSG_DATA(nlh);
351 nlh->nlmsg_flags = flags;
352 nfmsg->nfgen_family = AF_INET;
353 nfmsg->version = NFNETLINK_V0;
356 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
357 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
359 NFA_NEST_END(skb, nest_parms);
361 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
362 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
364 NFA_NEST_END(skb, nest_parms);
366 /* NAT stuff is now a status flag */
367 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
368 && ctnetlink_dump_status(skb, ct) < 0)
370 if (events & IPCT_REFRESH
371 && ctnetlink_dump_timeout(skb, ct) < 0)
373 if (events & IPCT_PROTOINFO
374 && ctnetlink_dump_protoinfo(skb, ct) < 0)
376 if (events & IPCT_HELPINFO
377 && ctnetlink_dump_helpinfo(skb, ct) < 0)
380 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
381 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
384 if (events & IPCT_MARK
385 && ctnetlink_dump_mark(skb, ct) < 0)
388 nlh->nlmsg_len = skb->tail - b;
389 nfnetlink_send(skb, 0, group, 0);
397 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
399 static int ctnetlink_done(struct netlink_callback *cb)
401 DEBUGP("entered %s\n", __FUNCTION__);
403 ip_conntrack_put((struct ip_conntrack *)cb->args[1]);
408 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
410 struct ip_conntrack *ct, *last;
411 struct ip_conntrack_tuple_hash *h;
414 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
417 read_lock_bh(&ip_conntrack_lock);
418 last = (struct ip_conntrack *)cb->args[1];
419 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++) {
421 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
422 h = (struct ip_conntrack_tuple_hash *) i;
423 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
425 ct = tuplehash_to_ctrack(h);
431 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
435 nf_conntrack_get(&ct->ct_general);
436 cb->args[1] = (unsigned long)ct;
439 #ifdef CONFIG_NF_CT_ACCT
440 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
441 IPCTNL_MSG_CT_GET_CTRZERO)
442 memset(&ct->counters, 0, sizeof(ct->counters));
451 read_unlock_bh(&ip_conntrack_lock);
453 ip_conntrack_put(last);
455 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
459 static const size_t cta_min_ip[CTA_IP_MAX] = {
460 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
461 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
465 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple)
467 struct nfattr *tb[CTA_IP_MAX];
469 DEBUGP("entered %s\n", __FUNCTION__);
471 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
473 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
476 if (!tb[CTA_IP_V4_SRC-1])
478 tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
480 if (!tb[CTA_IP_V4_DST-1])
482 tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
489 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
490 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
491 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
492 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
493 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
494 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
495 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
499 ctnetlink_parse_tuple_proto(struct nfattr *attr,
500 struct ip_conntrack_tuple *tuple)
502 struct nfattr *tb[CTA_PROTO_MAX];
503 struct ip_conntrack_protocol *proto;
506 DEBUGP("entered %s\n", __FUNCTION__);
508 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
510 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
513 if (!tb[CTA_PROTO_NUM-1])
515 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
517 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
519 if (likely(proto->nfattr_to_tuple))
520 ret = proto->nfattr_to_tuple(tb, tuple);
522 ip_conntrack_proto_put(proto);
528 ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
529 enum ctattr_tuple type)
531 struct nfattr *tb[CTA_TUPLE_MAX];
534 DEBUGP("entered %s\n", __FUNCTION__);
536 memset(tuple, 0, sizeof(*tuple));
538 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
540 if (!tb[CTA_TUPLE_IP-1])
543 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
547 if (!tb[CTA_TUPLE_PROTO-1])
550 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
554 /* orig and expect tuples get DIR_ORIGINAL */
555 if (type == CTA_TUPLE_REPLY)
556 tuple->dst.dir = IP_CT_DIR_REPLY;
558 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
567 #ifdef CONFIG_IP_NF_NAT_NEEDED
568 static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
569 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
570 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
573 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
574 const struct ip_conntrack *ct,
575 struct ip_nat_range *range)
577 struct nfattr *tb[CTA_PROTONAT_MAX];
578 struct ip_nat_protocol *npt;
580 DEBUGP("entered %s\n", __FUNCTION__);
582 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
584 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
587 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
589 if (!npt->nfattr_to_range) {
590 ip_nat_proto_put(npt);
594 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
595 if (npt->nfattr_to_range(tb, range) > 0)
596 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
598 ip_nat_proto_put(npt);
604 static const size_t cta_min_nat[CTA_NAT_MAX] = {
605 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
606 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
610 ctnetlink_parse_nat(struct nfattr *nat,
611 const struct ip_conntrack *ct, struct ip_nat_range *range)
613 struct nfattr *tb[CTA_NAT_MAX];
616 DEBUGP("entered %s\n", __FUNCTION__);
618 memset(range, 0, sizeof(*range));
620 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
622 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
625 if (tb[CTA_NAT_MINIP-1])
626 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
628 if (!tb[CTA_NAT_MAXIP-1])
629 range->max_ip = range->min_ip;
631 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
634 range->flags |= IP_NAT_RANGE_MAP_IPS;
636 if (!tb[CTA_NAT_PROTO-1])
639 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
649 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
651 struct nfattr *tb[CTA_HELP_MAX];
653 DEBUGP("entered %s\n", __FUNCTION__);
655 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
657 if (!tb[CTA_HELP_NAME-1])
660 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
665 static const size_t cta_min[CTA_MAX] = {
666 [CTA_STATUS-1] = sizeof(u_int32_t),
667 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
668 [CTA_MARK-1] = sizeof(u_int32_t),
669 [CTA_USE-1] = sizeof(u_int32_t),
670 [CTA_ID-1] = sizeof(u_int32_t)
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 (nfattr_bad_size(cda, CTA_MAX, cta_min))
687 if (cda[CTA_TUPLE_ORIG-1])
688 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
689 else if (cda[CTA_TUPLE_REPLY-1])
690 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
692 /* Flush the whole table */
693 ip_conntrack_flush();
700 h = ip_conntrack_find_get(&tuple, NULL);
702 DEBUGP("tuple not found in conntrack hash\n");
706 ct = tuplehash_to_ctrack(h);
709 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
711 ip_conntrack_put(ct);
715 if (del_timer(&ct->timeout))
716 ct->timeout.function((unsigned long)ct);
718 ip_conntrack_put(ct);
725 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
726 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
728 struct ip_conntrack_tuple_hash *h;
729 struct ip_conntrack_tuple tuple;
730 struct ip_conntrack *ct;
731 struct sk_buff *skb2 = NULL;
734 DEBUGP("entered %s\n", __FUNCTION__);
736 if (nlh->nlmsg_flags & NLM_F_DUMP) {
737 struct nfgenmsg *msg = NLMSG_DATA(nlh);
740 if (msg->nfgen_family != AF_INET)
741 return -EAFNOSUPPORT;
743 #ifndef CONFIG_IP_NF_CT_ACCT
744 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
747 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
748 ctnetlink_dump_table,
749 ctnetlink_done)) != 0)
752 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
759 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
762 if (cda[CTA_TUPLE_ORIG-1])
763 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
764 else if (cda[CTA_TUPLE_REPLY-1])
765 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
772 h = ip_conntrack_find_get(&tuple, NULL);
774 DEBUGP("tuple not found in conntrack hash");
777 DEBUGP("tuple found\n");
778 ct = tuplehash_to_ctrack(h);
781 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
783 ip_conntrack_put(ct);
786 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
788 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
789 IPCTNL_MSG_CT_NEW, 1, ct);
790 ip_conntrack_put(ct);
794 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
808 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
811 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
812 d = ct->status ^ status;
814 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
818 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
819 /* SEEN_REPLY bit can only be set */
823 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
824 /* ASSURED bit can only be set */
827 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
828 #ifndef CONFIG_IP_NF_NAT_NEEDED
831 struct ip_nat_range range;
833 if (cda[CTA_NAT_DST-1]) {
834 if (ctnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
837 if (ip_nat_initialized(ct,
838 HOOK2MANIP(NF_IP_PRE_ROUTING)))
840 ip_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
842 if (cda[CTA_NAT_SRC-1]) {
843 if (ctnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
846 if (ip_nat_initialized(ct,
847 HOOK2MANIP(NF_IP_POST_ROUTING)))
849 ip_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
854 /* Be careful here, modifying NAT bits can screw up things,
855 * so don't let users modify them directly if they don't pass
857 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
863 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
865 struct ip_conntrack_helper *helper;
869 DEBUGP("entered %s\n", __FUNCTION__);
871 /* don't change helper of sibling connections */
875 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
879 helper = __ip_conntrack_helper_find_byname(helpname);
881 if (!strcmp(helpname, ""))
889 /* we had a helper before ... */
890 ip_ct_remove_expectations(ct);
893 /* need to zero data of old helper */
894 memset(&ct->help, 0, sizeof(ct->help));
904 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
906 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
908 if (!del_timer(&ct->timeout))
911 ct->timeout.expires = jiffies + timeout * HZ;
912 add_timer(&ct->timeout);
918 ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
920 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
921 struct ip_conntrack_protocol *proto;
922 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
925 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
927 proto = ip_conntrack_proto_find_get(npt);
929 if (proto->from_nfattr)
930 err = proto->from_nfattr(tb, ct);
931 ip_conntrack_proto_put(proto);
937 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
941 DEBUGP("entered %s\n", __FUNCTION__);
943 if (cda[CTA_HELP-1]) {
944 err = ctnetlink_change_helper(ct, cda);
949 if (cda[CTA_TIMEOUT-1]) {
950 err = ctnetlink_change_timeout(ct, cda);
955 if (cda[CTA_STATUS-1]) {
956 err = ctnetlink_change_status(ct, cda);
961 if (cda[CTA_PROTOINFO-1]) {
962 err = ctnetlink_change_protoinfo(ct, cda);
967 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
969 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
972 DEBUGP("all done\n");
977 ctnetlink_create_conntrack(struct nfattr *cda[],
978 struct ip_conntrack_tuple *otuple,
979 struct ip_conntrack_tuple *rtuple)
981 struct ip_conntrack *ct;
984 DEBUGP("entered %s\n", __FUNCTION__);
986 ct = ip_conntrack_alloc(otuple, rtuple);
987 if (ct == NULL || IS_ERR(ct))
990 if (!cda[CTA_TIMEOUT-1])
992 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
994 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
995 ct->status |= IPS_CONFIRMED;
997 err = ctnetlink_change_status(ct, cda);
1001 if (cda[CTA_PROTOINFO-1]) {
1002 err = ctnetlink_change_protoinfo(ct, cda);
1007 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1008 if (cda[CTA_MARK-1])
1009 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1012 ct->helper = ip_conntrack_helper_find_get(rtuple);
1014 add_timer(&ct->timeout);
1015 ip_conntrack_hash_insert(ct);
1018 ip_conntrack_helper_put(ct->helper);
1020 DEBUGP("conntrack with id %u inserted\n", ct->id);
1024 ip_conntrack_free(ct);
1029 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1030 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1032 struct ip_conntrack_tuple otuple, rtuple;
1033 struct ip_conntrack_tuple_hash *h = NULL;
1036 DEBUGP("entered %s\n", __FUNCTION__);
1038 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1041 if (cda[CTA_TUPLE_ORIG-1]) {
1042 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1047 if (cda[CTA_TUPLE_REPLY-1]) {
1048 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1053 write_lock_bh(&ip_conntrack_lock);
1054 if (cda[CTA_TUPLE_ORIG-1])
1055 h = __ip_conntrack_find(&otuple, NULL);
1056 else if (cda[CTA_TUPLE_REPLY-1])
1057 h = __ip_conntrack_find(&rtuple, NULL);
1060 write_unlock_bh(&ip_conntrack_lock);
1061 DEBUGP("no such conntrack, create new\n");
1063 if (nlh->nlmsg_flags & NLM_F_CREATE)
1064 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1067 /* implicit 'else' */
1069 /* we only allow nat config for new conntracks */
1070 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
1075 /* We manipulate the conntrack inside the global conntrack table lock,
1076 * so there's no need to increase the refcount */
1077 DEBUGP("conntrack found\n");
1079 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1080 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1083 write_unlock_bh(&ip_conntrack_lock);
1087 /***********************************************************************
1089 ***********************************************************************/
1092 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1093 const struct ip_conntrack_tuple *tuple,
1094 enum ctattr_expect type)
1096 struct nfattr *nest_parms = NFA_NEST(skb, type);
1098 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1099 goto nfattr_failure;
1101 NFA_NEST_END(skb, nest_parms);
1110 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1111 const struct ip_conntrack_tuple *tuple,
1112 const struct ip_conntrack_tuple *mask)
1115 struct ip_conntrack_protocol *proto;
1116 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1118 ret = ctnetlink_dump_tuples_ip(skb, mask);
1119 if (unlikely(ret < 0))
1120 goto nfattr_failure;
1122 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
1123 ret = ctnetlink_dump_tuples_proto(skb, mask, proto);
1124 ip_conntrack_proto_put(proto);
1125 if (unlikely(ret < 0))
1126 goto nfattr_failure;
1128 NFA_NEST_END(skb, nest_parms);
1137 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1138 const struct ip_conntrack_expect *exp)
1140 struct ip_conntrack *master = exp->master;
1141 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1142 u_int32_t id = htonl(exp->id);
1144 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1145 goto nfattr_failure;
1146 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1147 goto nfattr_failure;
1148 if (ctnetlink_exp_dump_tuple(skb,
1149 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1150 CTA_EXPECT_MASTER) < 0)
1151 goto nfattr_failure;
1153 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1154 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1163 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1166 const struct ip_conntrack_expect *exp)
1168 struct nlmsghdr *nlh;
1169 struct nfgenmsg *nfmsg;
1174 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1175 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1176 nfmsg = NLMSG_DATA(nlh);
1178 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1179 nfmsg->nfgen_family = AF_INET;
1180 nfmsg->version = NFNETLINK_V0;
1183 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1184 goto nfattr_failure;
1186 nlh->nlmsg_len = skb->tail - b;
1191 skb_trim(skb, b - skb->data);
1195 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1196 static int ctnetlink_expect_event(struct notifier_block *this,
1197 unsigned long events, void *ptr)
1199 struct nlmsghdr *nlh;
1200 struct nfgenmsg *nfmsg;
1201 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1202 struct sk_buff *skb;
1207 if (events & IPEXP_NEW) {
1208 type = IPCTNL_MSG_EXP_NEW;
1209 flags = NLM_F_CREATE|NLM_F_EXCL;
1213 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1216 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1222 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1223 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1224 nfmsg = NLMSG_DATA(nlh);
1226 nlh->nlmsg_flags = flags;
1227 nfmsg->nfgen_family = AF_INET;
1228 nfmsg->version = NFNETLINK_V0;
1231 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1232 goto nfattr_failure;
1234 nlh->nlmsg_len = skb->tail - b;
1235 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1246 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1248 struct ip_conntrack_expect *exp = NULL;
1249 struct list_head *i;
1250 u_int32_t *id = (u_int32_t *) &cb->args[0];
1252 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1254 read_lock_bh(&ip_conntrack_lock);
1255 list_for_each_prev(i, &ip_conntrack_expect_list) {
1256 exp = (struct ip_conntrack_expect *) i;
1259 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1267 read_unlock_bh(&ip_conntrack_lock);
1269 DEBUGP("leaving, last id=%llu\n", *id);
1274 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1275 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1276 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1280 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1281 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1283 struct ip_conntrack_tuple tuple;
1284 struct ip_conntrack_expect *exp;
1285 struct sk_buff *skb2;
1288 DEBUGP("entered %s\n", __FUNCTION__);
1290 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1293 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1294 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1297 if (msg->nfgen_family != AF_INET)
1298 return -EAFNOSUPPORT;
1300 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1301 ctnetlink_exp_dump_table,
1302 ctnetlink_done)) != 0)
1304 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1305 if (rlen > skb->len)
1307 skb_pull(skb, rlen);
1311 if (cda[CTA_EXPECT_MASTER-1])
1312 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
1319 exp = ip_conntrack_expect_find(&tuple);
1323 if (cda[CTA_EXPECT_ID-1]) {
1324 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1325 if (exp->id != ntohl(id)) {
1326 ip_conntrack_expect_put(exp);
1332 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1335 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1337 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1338 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1343 ip_conntrack_expect_put(exp);
1345 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1350 ip_conntrack_expect_put(exp);
1355 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1356 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1358 struct ip_conntrack_expect *exp, *tmp;
1359 struct ip_conntrack_tuple tuple;
1360 struct ip_conntrack_helper *h;
1363 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1366 if (cda[CTA_EXPECT_TUPLE-1]) {
1367 /* delete a single expect by tuple */
1368 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1372 /* bump usage count to 2 */
1373 exp = ip_conntrack_expect_find(&tuple);
1377 if (cda[CTA_EXPECT_ID-1]) {
1379 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1380 if (exp->id != ntohl(id)) {
1381 ip_conntrack_expect_put(exp);
1386 /* after list removal, usage count == 1 */
1387 ip_conntrack_unexpect_related(exp);
1388 /* have to put what we 'get' above.
1389 * after this line usage count == 0 */
1390 ip_conntrack_expect_put(exp);
1391 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1392 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1394 /* delete all expectations for this helper */
1395 write_lock_bh(&ip_conntrack_lock);
1396 h = __ip_conntrack_helper_find_byname(name);
1398 write_unlock_bh(&ip_conntrack_lock);
1401 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1403 if (exp->master->helper == h
1404 && del_timer(&exp->timeout)) {
1405 ip_ct_unlink_expect(exp);
1406 ip_conntrack_expect_put(exp);
1409 write_unlock_bh(&ip_conntrack_lock);
1411 /* This basically means we have to flush everything*/
1412 write_lock_bh(&ip_conntrack_lock);
1413 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1415 if (del_timer(&exp->timeout)) {
1416 ip_ct_unlink_expect(exp);
1417 ip_conntrack_expect_put(exp);
1420 write_unlock_bh(&ip_conntrack_lock);
1426 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1432 ctnetlink_create_expect(struct nfattr *cda[])
1434 struct ip_conntrack_tuple tuple, mask, master_tuple;
1435 struct ip_conntrack_tuple_hash *h = NULL;
1436 struct ip_conntrack_expect *exp;
1437 struct ip_conntrack *ct;
1440 DEBUGP("entered %s\n", __FUNCTION__);
1442 /* caller guarantees that those three CTA_EXPECT_* exist */
1443 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1446 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
1449 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
1453 /* Look for master conntrack of this expectation */
1454 h = ip_conntrack_find_get(&master_tuple, NULL);
1457 ct = tuplehash_to_ctrack(h);
1460 /* such conntrack hasn't got any helper, abort */
1465 exp = ip_conntrack_expect_alloc(ct);
1471 exp->expectfn = NULL;
1474 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1475 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1477 err = ip_conntrack_expect_related(exp);
1478 ip_conntrack_expect_put(exp);
1481 ip_conntrack_put(tuplehash_to_ctrack(h));
1486 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1487 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1489 struct ip_conntrack_tuple tuple;
1490 struct ip_conntrack_expect *exp;
1493 DEBUGP("entered %s\n", __FUNCTION__);
1495 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1498 if (!cda[CTA_EXPECT_TUPLE-1]
1499 || !cda[CTA_EXPECT_MASK-1]
1500 || !cda[CTA_EXPECT_MASTER-1])
1503 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1507 write_lock_bh(&ip_conntrack_lock);
1508 exp = __ip_conntrack_expect_find(&tuple);
1511 write_unlock_bh(&ip_conntrack_lock);
1513 if (nlh->nlmsg_flags & NLM_F_CREATE)
1514 err = ctnetlink_create_expect(cda);
1519 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1520 err = ctnetlink_change_expect(exp, cda);
1521 write_unlock_bh(&ip_conntrack_lock);
1523 DEBUGP("leaving\n");
1528 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1529 static struct notifier_block ctnl_notifier = {
1530 .notifier_call = ctnetlink_conntrack_event,
1533 static struct notifier_block ctnl_notifier_exp = {
1534 .notifier_call = ctnetlink_expect_event,
1538 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1539 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1540 .attr_count = CTA_MAX, },
1541 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1542 .attr_count = CTA_MAX, },
1543 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1544 .attr_count = CTA_MAX, },
1545 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1546 .attr_count = CTA_MAX, },
1549 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1550 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1551 .attr_count = CTA_EXPECT_MAX, },
1552 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1553 .attr_count = CTA_EXPECT_MAX, },
1554 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1555 .attr_count = CTA_EXPECT_MAX, },
1558 static struct nfnetlink_subsystem ctnl_subsys = {
1559 .name = "conntrack",
1560 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1561 .cb_count = IPCTNL_MSG_MAX,
1565 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1566 .name = "conntrack_expect",
1567 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1568 .cb_count = IPCTNL_MSG_EXP_MAX,
1572 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1573 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1575 static int __init ctnetlink_init(void)
1579 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1580 ret = nfnetlink_subsys_register(&ctnl_subsys);
1582 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1586 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1588 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1589 goto err_unreg_subsys;
1592 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1593 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1595 printk("ctnetlink_init: cannot register notifier.\n");
1596 goto err_unreg_exp_subsys;
1599 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1601 printk("ctnetlink_init: cannot expect register notifier.\n");
1602 goto err_unreg_notifier;
1608 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1610 ip_conntrack_unregister_notifier(&ctnl_notifier);
1611 err_unreg_exp_subsys:
1612 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1615 nfnetlink_subsys_unregister(&ctnl_subsys);
1620 static void __exit ctnetlink_exit(void)
1622 printk("ctnetlink: unregistering from nfnetlink.\n");
1624 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1625 ip_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
1626 ip_conntrack_unregister_notifier(&ctnl_notifier);
1629 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1630 nfnetlink_subsys_unregister(&ctnl_subsys);
1634 module_init(ctnetlink_init);
1635 module_exit(ctnetlink_exit);