[NETFILTER] ctnetlink: Add support to identify expectations by ID's
[linux-2.6/btrfs-unstable.git] / net / ipv4 / netfilter / ip_conntrack_netlink.c
blob5c1c0a3d1c4bc5ba2a72230f1c53d79d4f8fcb89
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>
32 #include <linux/netfilter.h>
33 #include <linux/netfilter_ipv4/ip_conntrack.h>
34 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
35 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
36 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
37 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
39 #include <linux/netfilter/nfnetlink.h>
40 #include <linux/netfilter/nfnetlink_conntrack.h>
42 MODULE_LICENSE("GPL");
44 static char __initdata version[] = "0.90";
46 #if 0
47 #define DEBUGP printk
48 #else
49 #define DEBUGP(format, args...)
50 #endif
53 static inline int
54 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
55 const struct ip_conntrack_tuple *tuple)
57 struct ip_conntrack_protocol *proto;
58 int ret = 0;
60 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
62 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
63 if (likely(proto && proto->tuple_to_nfattr)) {
64 ret = proto->tuple_to_nfattr(skb, tuple);
65 ip_conntrack_proto_put(proto);
68 return ret;
70 nfattr_failure:
71 return -1;
74 static inline int
75 ctnetlink_dump_tuples(struct sk_buff *skb,
76 const struct ip_conntrack_tuple *tuple)
78 struct nfattr *nest_parms;
80 nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
81 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t), &tuple->src.ip);
82 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t), &tuple->dst.ip);
83 NFA_NEST_END(skb, nest_parms);
85 nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
86 ctnetlink_dump_tuples_proto(skb, tuple);
87 NFA_NEST_END(skb, nest_parms);
89 return 0;
91 nfattr_failure:
92 return -1;
95 static inline int
96 ctnetlink_dump_status(struct sk_buff *skb, const struct ip_conntrack *ct)
98 u_int32_t status = htonl((u_int32_t) ct->status);
99 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
100 return 0;
102 nfattr_failure:
103 return -1;
106 static inline int
107 ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct)
109 long timeout_l = ct->timeout.expires - jiffies;
110 u_int32_t timeout;
112 if (timeout_l < 0)
113 timeout = 0;
114 else
115 timeout = htonl(timeout_l / HZ);
117 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
118 return 0;
120 nfattr_failure:
121 return -1;
124 static inline int
125 ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
127 struct ip_conntrack_protocol *proto = ip_conntrack_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
129 struct nfattr *nest_proto;
130 int ret;
132 if (!proto || !proto->to_nfattr)
133 return 0;
135 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
137 ret = proto->to_nfattr(skb, nest_proto, ct);
139 ip_conntrack_proto_put(proto);
141 NFA_NEST_END(skb, nest_proto);
143 return ret;
145 nfattr_failure:
146 return -1;
149 static inline int
150 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
152 struct nfattr *nest_helper;
154 if (!ct->helper)
155 return 0;
157 nest_helper = NFA_NEST(skb, CTA_HELP);
158 NFA_PUT(skb, CTA_HELP_NAME, CTA_HELP_MAXNAMESIZE, &ct->helper->name);
160 if (ct->helper->to_nfattr)
161 ct->helper->to_nfattr(skb, ct);
163 NFA_NEST_END(skb, nest_helper);
165 return 0;
167 nfattr_failure:
168 return -1;
171 #ifdef CONFIG_IP_NF_CT_ACCT
172 static inline int
173 ctnetlink_dump_counters(struct sk_buff *skb, const struct ip_conntrack *ct,
174 enum ip_conntrack_dir dir)
176 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
177 struct nfattr *nest_count = NFA_NEST(skb, type);
178 u_int32_t tmp;
180 tmp = htonl(ct->counters[dir].packets);
181 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
183 tmp = htonl(ct->counters[dir].bytes);
184 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
186 NFA_NEST_END(skb, nest_count);
188 return 0;
190 nfattr_failure:
191 return -1;
193 #else
194 #define ctnetlink_dump_counters(a, b, c) (0)
195 #endif
197 #ifdef CONFIG_IP_NF_CONNTRACK_MARK
198 static inline int
199 ctnetlink_dump_mark(struct sk_buff *skb, const struct ip_conntrack *ct)
201 u_int32_t mark = htonl(ct->mark);
203 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
204 return 0;
206 nfattr_failure:
207 return -1;
209 #else
210 #define ctnetlink_dump_mark(a, b) (0)
211 #endif
213 static inline int
214 ctnetlink_dump_id(struct sk_buff *skb, const struct ip_conntrack *ct)
216 u_int32_t id = htonl(ct->id);
217 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
218 return 0;
220 nfattr_failure:
221 return -1;
224 static inline int
225 ctnetlink_dump_use(struct sk_buff *skb, const struct ip_conntrack *ct)
227 unsigned int use = htonl(atomic_read(&ct->ct_general.use));
229 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
230 return 0;
232 nfattr_failure:
233 return -1;
236 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
238 static int
239 ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
240 int event, int nowait,
241 const struct ip_conntrack *ct)
243 struct nlmsghdr *nlh;
244 struct nfgenmsg *nfmsg;
245 struct nfattr *nest_parms;
246 unsigned char *b;
248 b = skb->tail;
250 event |= NFNL_SUBSYS_CTNETLINK << 8;
251 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
252 nfmsg = NLMSG_DATA(nlh);
254 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
255 nfmsg->nfgen_family = AF_INET;
256 nfmsg->version = NFNETLINK_V0;
257 nfmsg->res_id = 0;
259 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
260 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
261 goto nfattr_failure;
262 NFA_NEST_END(skb, nest_parms);
264 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
265 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
266 goto nfattr_failure;
267 NFA_NEST_END(skb, nest_parms);
269 if (ctnetlink_dump_status(skb, ct) < 0 ||
270 ctnetlink_dump_timeout(skb, ct) < 0 ||
271 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
272 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
273 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
274 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
275 ctnetlink_dump_mark(skb, ct) < 0 ||
276 ctnetlink_dump_id(skb, ct) < 0 ||
277 ctnetlink_dump_use(skb, ct) < 0)
278 goto nfattr_failure;
280 nlh->nlmsg_len = skb->tail - b;
281 return skb->len;
283 nlmsg_failure:
284 nfattr_failure:
285 skb_trim(skb, b - skb->data);
286 return -1;
289 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
290 static int ctnetlink_conntrack_event(struct notifier_block *this,
291 unsigned long events, void *ptr)
293 struct nlmsghdr *nlh;
294 struct nfgenmsg *nfmsg;
295 struct nfattr *nest_parms;
296 struct ip_conntrack *ct = (struct ip_conntrack *)ptr;
297 struct sk_buff *skb;
298 unsigned int type;
299 unsigned char *b;
300 unsigned int flags = 0, group;
302 /* ignore our fake conntrack entry */
303 if (ct == &ip_conntrack_untracked)
304 return NOTIFY_DONE;
306 if (events & IPCT_DESTROY) {
307 type = IPCTNL_MSG_CT_DELETE;
308 group = NFNLGRP_CONNTRACK_DESTROY;
309 goto alloc_skb;
311 if (events & (IPCT_NEW | IPCT_RELATED)) {
312 type = IPCTNL_MSG_CT_NEW;
313 flags = NLM_F_CREATE|NLM_F_EXCL;
314 /* dump everything */
315 events = ~0UL;
316 group = NFNLGRP_CONNTRACK_NEW;
317 goto alloc_skb;
319 if (events & (IPCT_STATUS |
320 IPCT_PROTOINFO |
321 IPCT_HELPER |
322 IPCT_HELPINFO |
323 IPCT_NATINFO)) {
324 type = IPCTNL_MSG_CT_NEW;
325 group = NFNLGRP_CONNTRACK_UPDATE;
326 goto alloc_skb;
329 return NOTIFY_DONE;
331 alloc_skb:
332 /* FIXME: Check if there are any listeners before, don't hurt performance */
334 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
335 if (!skb)
336 return NOTIFY_DONE;
338 b = skb->tail;
340 type |= NFNL_SUBSYS_CTNETLINK << 8;
341 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
342 nfmsg = NLMSG_DATA(nlh);
344 nlh->nlmsg_flags = flags;
345 nfmsg->nfgen_family = AF_INET;
346 nfmsg->version = NFNETLINK_V0;
347 nfmsg->res_id = 0;
349 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
350 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
351 goto nfattr_failure;
352 NFA_NEST_END(skb, nest_parms);
354 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
355 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
356 goto nfattr_failure;
357 NFA_NEST_END(skb, nest_parms);
359 /* NAT stuff is now a status flag */
360 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
361 && ctnetlink_dump_status(skb, ct) < 0)
362 goto nfattr_failure;
363 if (events & IPCT_REFRESH
364 && ctnetlink_dump_timeout(skb, ct) < 0)
365 goto nfattr_failure;
366 if (events & IPCT_PROTOINFO
367 && ctnetlink_dump_protoinfo(skb, ct) < 0)
368 goto nfattr_failure;
369 if (events & IPCT_HELPINFO
370 && ctnetlink_dump_helpinfo(skb, ct) < 0)
371 goto nfattr_failure;
373 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
374 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
375 goto nfattr_failure;
377 nlh->nlmsg_len = skb->tail - b;
378 nfnetlink_send(skb, 0, group, 0);
379 return NOTIFY_DONE;
381 nlmsg_failure:
382 nfattr_failure:
383 kfree_skb(skb);
384 return NOTIFY_DONE;
386 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
388 static int ctnetlink_done(struct netlink_callback *cb)
390 DEBUGP("entered %s\n", __FUNCTION__);
391 return 0;
394 static int
395 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
397 struct ip_conntrack *ct = NULL;
398 struct ip_conntrack_tuple_hash *h;
399 struct list_head *i;
400 u_int32_t *id = (u_int32_t *) &cb->args[1];
402 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
403 cb->args[0], *id);
405 read_lock_bh(&ip_conntrack_lock);
406 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
407 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
408 h = (struct ip_conntrack_tuple_hash *) i;
409 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
410 continue;
411 ct = tuplehash_to_ctrack(h);
412 if (ct->id <= *id)
413 continue;
414 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
415 cb->nlh->nlmsg_seq,
416 IPCTNL_MSG_CT_NEW,
417 1, ct) < 0)
418 goto out;
419 *id = ct->id;
422 out:
423 read_unlock_bh(&ip_conntrack_lock);
425 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
427 return skb->len;
430 #ifdef CONFIG_IP_NF_CT_ACCT
431 static int
432 ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
434 struct ip_conntrack *ct = NULL;
435 struct ip_conntrack_tuple_hash *h;
436 struct list_head *i;
437 u_int32_t *id = (u_int32_t *) &cb->args[1];
439 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
440 cb->args[0], *id);
442 write_lock_bh(&ip_conntrack_lock);
443 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
444 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
445 h = (struct ip_conntrack_tuple_hash *) i;
446 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
447 continue;
448 ct = tuplehash_to_ctrack(h);
449 if (ct->id <= *id)
450 continue;
451 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
452 cb->nlh->nlmsg_seq,
453 IPCTNL_MSG_CT_NEW,
454 1, ct) < 0)
455 goto out;
456 *id = ct->id;
458 memset(&ct->counters, 0, sizeof(ct->counters));
461 out:
462 write_unlock_bh(&ip_conntrack_lock);
464 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
466 return skb->len;
468 #endif
470 static const int cta_min_ip[CTA_IP_MAX] = {
471 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
472 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
475 static inline int
476 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple)
478 struct nfattr *tb[CTA_IP_MAX];
480 DEBUGP("entered %s\n", __FUNCTION__);
482 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
484 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
485 return -EINVAL;
487 if (!tb[CTA_IP_V4_SRC-1])
488 return -EINVAL;
489 tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
491 if (!tb[CTA_IP_V4_DST-1])
492 return -EINVAL;
493 tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
495 DEBUGP("leaving\n");
497 return 0;
500 static const int cta_min_proto[CTA_PROTO_MAX] = {
501 [CTA_PROTO_NUM-1] = sizeof(u_int16_t),
502 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
503 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
504 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
505 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
506 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
509 static inline int
510 ctnetlink_parse_tuple_proto(struct nfattr *attr,
511 struct ip_conntrack_tuple *tuple)
513 struct nfattr *tb[CTA_PROTO_MAX];
514 struct ip_conntrack_protocol *proto;
515 int ret = 0;
517 DEBUGP("entered %s\n", __FUNCTION__);
519 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
521 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
522 return -EINVAL;
524 if (!tb[CTA_PROTO_NUM-1])
525 return -EINVAL;
526 tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
528 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
530 if (likely(proto && proto->nfattr_to_tuple)) {
531 ret = proto->nfattr_to_tuple(tb, tuple);
532 ip_conntrack_proto_put(proto);
535 return ret;
538 static inline int
539 ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
540 enum ctattr_tuple type)
542 struct nfattr *tb[CTA_TUPLE_MAX];
543 int err;
545 DEBUGP("entered %s\n", __FUNCTION__);
547 memset(tuple, 0, sizeof(*tuple));
549 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
551 if (!tb[CTA_TUPLE_IP-1])
552 return -EINVAL;
554 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
555 if (err < 0)
556 return err;
558 if (!tb[CTA_TUPLE_PROTO-1])
559 return -EINVAL;
561 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
562 if (err < 0)
563 return err;
565 /* orig and expect tuples get DIR_ORIGINAL */
566 if (type == CTA_TUPLE_REPLY)
567 tuple->dst.dir = IP_CT_DIR_REPLY;
568 else
569 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
571 DUMP_TUPLE(tuple);
573 DEBUGP("leaving\n");
575 return 0;
578 #ifdef CONFIG_IP_NF_NAT_NEEDED
579 static const int cta_min_protonat[CTA_PROTONAT_MAX] = {
580 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
581 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
584 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
585 const struct ip_conntrack *ct,
586 struct ip_nat_range *range)
588 struct nfattr *tb[CTA_PROTONAT_MAX];
589 struct ip_nat_protocol *npt;
591 DEBUGP("entered %s\n", __FUNCTION__);
593 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
595 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
596 return -EINVAL;
598 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
599 if (!npt)
600 return 0;
602 if (!npt->nfattr_to_range) {
603 ip_nat_proto_put(npt);
604 return 0;
607 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
608 if (npt->nfattr_to_range(tb, range) > 0)
609 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
611 ip_nat_proto_put(npt);
613 DEBUGP("leaving\n");
614 return 0;
617 static inline int
618 ctnetlink_parse_nat(struct nfattr *cda[],
619 const struct ip_conntrack *ct, struct ip_nat_range *range)
621 struct nfattr *tb[CTA_NAT_MAX];
622 int err;
624 DEBUGP("entered %s\n", __FUNCTION__);
626 memset(range, 0, sizeof(*range));
628 nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]);
630 if (tb[CTA_NAT_MINIP-1])
631 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
633 if (!tb[CTA_NAT_MAXIP-1])
634 range->max_ip = range->min_ip;
635 else
636 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
638 if (range->min_ip)
639 range->flags |= IP_NAT_RANGE_MAP_IPS;
641 if (!tb[CTA_NAT_PROTO-1])
642 return 0;
644 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
645 if (err < 0)
646 return err;
648 DEBUGP("leaving\n");
649 return 0;
651 #endif
653 static inline int
654 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
656 struct nfattr *tb[CTA_HELP_MAX];
658 DEBUGP("entered %s\n", __FUNCTION__);
660 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
662 if (!tb[CTA_HELP_NAME-1])
663 return -EINVAL;
665 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
667 return 0;
670 static int
671 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
672 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
674 struct ip_conntrack_tuple_hash *h;
675 struct ip_conntrack_tuple tuple;
676 struct ip_conntrack *ct;
677 int err = 0;
679 DEBUGP("entered %s\n", __FUNCTION__);
681 if (cda[CTA_TUPLE_ORIG-1])
682 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
683 else if (cda[CTA_TUPLE_REPLY-1])
684 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
685 else {
686 /* Flush the whole table */
687 ip_conntrack_flush();
688 return 0;
691 if (err < 0)
692 return err;
694 h = ip_conntrack_find_get(&tuple, NULL);
695 if (!h) {
696 DEBUGP("tuple not found in conntrack hash\n");
697 return -ENOENT;
700 ct = tuplehash_to_ctrack(h);
702 if (cda[CTA_ID-1]) {
703 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
704 if (ct->id != id) {
705 ip_conntrack_put(ct);
706 return -ENOENT;
709 if (del_timer(&ct->timeout)) {
710 ip_conntrack_put(ct);
711 ct->timeout.function((unsigned long)ct);
712 return 0;
714 ip_conntrack_put(ct);
715 DEBUGP("leaving\n");
717 return 0;
720 static int
721 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
722 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
724 struct ip_conntrack_tuple_hash *h;
725 struct ip_conntrack_tuple tuple;
726 struct ip_conntrack *ct;
727 struct sk_buff *skb2 = NULL;
728 int err = 0;
730 DEBUGP("entered %s\n", __FUNCTION__);
732 if (nlh->nlmsg_flags & NLM_F_DUMP) {
733 struct nfgenmsg *msg = NLMSG_DATA(nlh);
734 u32 rlen;
736 if (msg->nfgen_family != AF_INET)
737 return -EAFNOSUPPORT;
739 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
740 IPCTNL_MSG_CT_GET_CTRZERO) {
741 #ifdef CONFIG_IP_NF_CT_ACCT
742 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
743 ctnetlink_dump_table_w,
744 ctnetlink_done)) != 0)
745 return -EINVAL;
746 #else
747 return -ENOTSUPP;
748 #endif
749 } else {
750 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
751 ctnetlink_dump_table,
752 ctnetlink_done)) != 0)
753 return -EINVAL;
756 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
757 if (rlen > skb->len)
758 rlen = skb->len;
759 skb_pull(skb, rlen);
760 return 0;
763 if (cda[CTA_TUPLE_ORIG-1])
764 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
765 else if (cda[CTA_TUPLE_REPLY-1])
766 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
767 else
768 return -EINVAL;
770 if (err < 0)
771 return err;
773 h = ip_conntrack_find_get(&tuple, NULL);
774 if (!h) {
775 DEBUGP("tuple not found in conntrack hash");
776 return -ENOENT;
778 DEBUGP("tuple found\n");
779 ct = tuplehash_to_ctrack(h);
781 err = -ENOMEM;
782 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
783 if (!skb2) {
784 ip_conntrack_put(ct);
785 return -ENOMEM;
787 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
789 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
790 IPCTNL_MSG_CT_NEW, 1, ct);
791 ip_conntrack_put(ct);
792 if (err <= 0)
793 goto free;
795 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
796 if (err < 0)
797 goto out;
799 DEBUGP("leaving\n");
800 return 0;
802 free:
803 kfree_skb(skb2);
804 out:
805 return err;
808 static inline int
809 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
811 unsigned long d;
812 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
813 d = ct->status ^ status;
815 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
816 /* unchangeable */
817 return -EINVAL;
819 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
820 /* SEEN_REPLY bit can only be set */
821 return -EINVAL;
824 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
825 /* ASSURED bit can only be set */
826 return -EINVAL;
828 if (cda[CTA_NAT-1]) {
829 #ifndef CONFIG_IP_NF_NAT_NEEDED
830 return -EINVAL;
831 #else
832 unsigned int hooknum;
833 struct ip_nat_range range;
835 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
836 return -EINVAL;
838 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
839 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
840 htons(range.min.all), htons(range.max.all));
842 /* This is tricky but it works. ip_nat_setup_info needs the
843 * hook number as parameter, so let's do the correct
844 * conversion and run away */
845 if (status & IPS_SRC_NAT_DONE)
846 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
847 else if (status & IPS_DST_NAT_DONE)
848 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
849 else
850 return -EINVAL; /* Missing NAT flags */
852 DEBUGP("NAT status: %lu\n",
853 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
855 if (ip_nat_initialized(ct, hooknum))
856 return -EEXIST;
857 ip_nat_setup_info(ct, &range, hooknum);
859 DEBUGP("NAT status after setup_info: %lu\n",
860 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
861 #endif
864 /* Be careful here, modifying NAT bits can screw up things,
865 * so don't let users modify them directly if they don't pass
866 * ip_nat_range. */
867 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
868 return 0;
872 static inline int
873 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
875 struct ip_conntrack_helper *helper;
876 char *helpname;
877 int err;
879 DEBUGP("entered %s\n", __FUNCTION__);
881 /* don't change helper of sibling connections */
882 if (ct->master)
883 return -EINVAL;
885 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
886 if (err < 0)
887 return err;
889 helper = __ip_conntrack_helper_find_byname(helpname);
890 if (!helper) {
891 if (!strcmp(helpname, ""))
892 helper = NULL;
893 else
894 return -EINVAL;
897 if (ct->helper) {
898 if (!helper) {
899 /* we had a helper before ... */
900 ip_ct_remove_expectations(ct);
901 ct->helper = NULL;
902 } else {
903 /* need to zero data of old helper */
904 memset(&ct->help, 0, sizeof(ct->help));
908 ct->helper = helper;
910 return 0;
913 static inline int
914 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
916 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
918 if (!del_timer(&ct->timeout))
919 return -ETIME;
921 ct->timeout.expires = jiffies + timeout * HZ;
922 add_timer(&ct->timeout);
924 return 0;
927 static inline int
928 ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
930 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
931 struct ip_conntrack_protocol *proto;
932 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
933 int err = 0;
935 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
937 proto = ip_conntrack_proto_find_get(npt);
938 if (!proto)
939 return -EINVAL;
941 if (proto->from_nfattr)
942 err = proto->from_nfattr(tb, ct);
943 ip_conntrack_proto_put(proto);
945 return err;
948 static int
949 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
951 int err;
953 DEBUGP("entered %s\n", __FUNCTION__);
955 if (cda[CTA_HELP-1]) {
956 err = ctnetlink_change_helper(ct, cda);
957 if (err < 0)
958 return err;
961 if (cda[CTA_TIMEOUT-1]) {
962 err = ctnetlink_change_timeout(ct, cda);
963 if (err < 0)
964 return err;
967 if (cda[CTA_STATUS-1]) {
968 err = ctnetlink_change_status(ct, cda);
969 if (err < 0)
970 return err;
973 if (cda[CTA_PROTOINFO-1]) {
974 err = ctnetlink_change_protoinfo(ct, cda);
975 if (err < 0)
976 return err;
979 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
980 if (cda[CTA_MARK-1])
981 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
982 #endif
984 DEBUGP("all done\n");
985 return 0;
988 static int
989 ctnetlink_create_conntrack(struct nfattr *cda[],
990 struct ip_conntrack_tuple *otuple,
991 struct ip_conntrack_tuple *rtuple)
993 struct ip_conntrack *ct;
994 int err = -EINVAL;
996 DEBUGP("entered %s\n", __FUNCTION__);
998 ct = ip_conntrack_alloc(otuple, rtuple);
999 if (ct == NULL || IS_ERR(ct))
1000 return -ENOMEM;
1002 if (!cda[CTA_TIMEOUT-1])
1003 goto err;
1004 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1006 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1007 ct->status |= IPS_CONFIRMED;
1009 err = ctnetlink_change_status(ct, cda);
1010 if (err < 0)
1011 goto err;
1013 if (cda[CTA_PROTOINFO-1]) {
1014 err = ctnetlink_change_protoinfo(ct, cda);
1015 if (err < 0)
1016 return err;
1019 ct->helper = ip_conntrack_helper_find_get(rtuple);
1021 add_timer(&ct->timeout);
1022 ip_conntrack_hash_insert(ct);
1024 if (ct->helper)
1025 ip_conntrack_helper_put(ct->helper);
1027 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1028 if (cda[CTA_MARK-1])
1029 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1030 #endif
1032 DEBUGP("conntrack with id %u inserted\n", ct->id);
1033 return 0;
1035 err:
1036 ip_conntrack_free(ct);
1037 return err;
1040 static int
1041 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1042 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1044 struct ip_conntrack_tuple otuple, rtuple;
1045 struct ip_conntrack_tuple_hash *h = NULL;
1046 int err = 0;
1048 DEBUGP("entered %s\n", __FUNCTION__);
1050 if (cda[CTA_TUPLE_ORIG-1]) {
1051 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1052 if (err < 0)
1053 return err;
1056 if (cda[CTA_TUPLE_REPLY-1]) {
1057 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1058 if (err < 0)
1059 return err;
1062 write_lock_bh(&ip_conntrack_lock);
1063 if (cda[CTA_TUPLE_ORIG-1])
1064 h = __ip_conntrack_find(&otuple, NULL);
1065 else if (cda[CTA_TUPLE_REPLY-1])
1066 h = __ip_conntrack_find(&rtuple, NULL);
1068 if (h == NULL) {
1069 write_unlock_bh(&ip_conntrack_lock);
1070 DEBUGP("no such conntrack, create new\n");
1071 err = -ENOENT;
1072 if (nlh->nlmsg_flags & NLM_F_CREATE)
1073 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1074 return err;
1076 /* implicit 'else' */
1078 /* we only allow nat config for new conntracks */
1079 if (cda[CTA_NAT-1]) {
1080 err = -EINVAL;
1081 goto out_unlock;
1084 /* We manipulate the conntrack inside the global conntrack table lock,
1085 * so there's no need to increase the refcount */
1086 DEBUGP("conntrack found\n");
1087 err = -EEXIST;
1088 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1089 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1091 out_unlock:
1092 write_unlock_bh(&ip_conntrack_lock);
1093 return err;
1096 /***********************************************************************
1097 * EXPECT
1098 ***********************************************************************/
1100 static inline int
1101 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1102 const struct ip_conntrack_tuple *tuple,
1103 enum ctattr_expect type)
1105 struct nfattr *nest_parms = NFA_NEST(skb, type);
1107 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1108 goto nfattr_failure;
1110 NFA_NEST_END(skb, nest_parms);
1112 return 0;
1114 nfattr_failure:
1115 return -1;
1118 static inline int
1119 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1120 const struct ip_conntrack_expect *exp)
1122 struct ip_conntrack *master = exp->master;
1123 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1124 u_int32_t id = htonl(exp->id);
1126 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1127 goto nfattr_failure;
1128 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1129 goto nfattr_failure;
1130 if (ctnetlink_exp_dump_tuple(skb,
1131 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1132 CTA_EXPECT_MASTER) < 0)
1133 goto nfattr_failure;
1135 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1136 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1138 return 0;
1140 nfattr_failure:
1141 return -1;
1144 static int
1145 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1146 int event,
1147 int nowait,
1148 const struct ip_conntrack_expect *exp)
1150 struct nlmsghdr *nlh;
1151 struct nfgenmsg *nfmsg;
1152 unsigned char *b;
1154 b = skb->tail;
1156 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1157 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1158 nfmsg = NLMSG_DATA(nlh);
1160 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1161 nfmsg->nfgen_family = AF_INET;
1162 nfmsg->version = NFNETLINK_V0;
1163 nfmsg->res_id = 0;
1165 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1166 goto nfattr_failure;
1168 nlh->nlmsg_len = skb->tail - b;
1169 return skb->len;
1171 nlmsg_failure:
1172 nfattr_failure:
1173 skb_trim(skb, b - skb->data);
1174 return -1;
1177 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1178 static int ctnetlink_expect_event(struct notifier_block *this,
1179 unsigned long events, void *ptr)
1181 struct nlmsghdr *nlh;
1182 struct nfgenmsg *nfmsg;
1183 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1184 struct sk_buff *skb;
1185 unsigned int type;
1186 unsigned char *b;
1187 int flags = 0;
1188 u16 proto;
1190 if (events & IPEXP_NEW) {
1191 type = IPCTNL_MSG_EXP_NEW;
1192 flags = NLM_F_CREATE|NLM_F_EXCL;
1193 } else
1194 return NOTIFY_DONE;
1196 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1197 if (!skb)
1198 return NOTIFY_DONE;
1200 b = skb->tail;
1202 type |= NFNL_SUBSYS_CTNETLINK << 8;
1203 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1204 nfmsg = NLMSG_DATA(nlh);
1206 nlh->nlmsg_flags = flags;
1207 nfmsg->nfgen_family = AF_INET;
1208 nfmsg->version = NFNETLINK_V0;
1209 nfmsg->res_id = 0;
1211 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1212 goto nfattr_failure;
1214 nlh->nlmsg_len = skb->tail - b;
1215 proto = exp->tuple.dst.protonum;
1216 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1217 return NOTIFY_DONE;
1219 nlmsg_failure:
1220 nfattr_failure:
1221 kfree_skb(skb);
1222 return NOTIFY_DONE;
1224 #endif
1226 static int
1227 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1229 struct ip_conntrack_expect *exp = NULL;
1230 struct list_head *i;
1231 u_int32_t *id = (u_int32_t *) &cb->args[0];
1233 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1235 read_lock_bh(&ip_conntrack_lock);
1236 list_for_each_prev(i, &ip_conntrack_expect_list) {
1237 exp = (struct ip_conntrack_expect *) i;
1238 if (exp->id <= *id)
1239 continue;
1240 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1241 cb->nlh->nlmsg_seq,
1242 IPCTNL_MSG_EXP_NEW,
1243 1, exp) < 0)
1244 goto out;
1245 *id = exp->id;
1247 out:
1248 read_unlock_bh(&ip_conntrack_lock);
1250 DEBUGP("leaving, last id=%llu\n", *id);
1252 return skb->len;
1255 static int
1256 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1257 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1259 struct ip_conntrack_tuple tuple;
1260 struct ip_conntrack_expect *exp;
1261 struct sk_buff *skb2;
1262 int err = 0;
1264 DEBUGP("entered %s\n", __FUNCTION__);
1266 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1267 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1268 u32 rlen;
1270 if (msg->nfgen_family != AF_INET)
1271 return -EAFNOSUPPORT;
1273 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1274 ctnetlink_exp_dump_table,
1275 ctnetlink_done)) != 0)
1276 return -EINVAL;
1277 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1278 if (rlen > skb->len)
1279 rlen = skb->len;
1280 skb_pull(skb, rlen);
1281 return 0;
1284 if (cda[CTA_EXPECT_MASTER-1])
1285 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
1286 else
1287 return -EINVAL;
1289 if (err < 0)
1290 return err;
1292 exp = ip_conntrack_expect_find(&tuple);
1293 if (!exp)
1294 return -ENOENT;
1296 if (cda[CTA_EXPECT_ID-1]) {
1297 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1298 if (exp->id != ntohl(id)) {
1299 ip_conntrack_expect_put(exp);
1300 return -ENOENT;
1304 err = -ENOMEM;
1305 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1306 if (!skb2)
1307 goto out;
1308 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1310 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1311 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1312 1, exp);
1313 if (err <= 0)
1314 goto free;
1316 ip_conntrack_expect_put(exp);
1318 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1320 free:
1321 kfree_skb(skb2);
1322 out:
1323 ip_conntrack_expect_put(exp);
1324 return err;
1327 static int
1328 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1329 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1331 struct ip_conntrack_expect *exp, *tmp;
1332 struct ip_conntrack_tuple tuple;
1333 struct ip_conntrack_helper *h;
1334 int err;
1336 if (cda[CTA_EXPECT_TUPLE-1]) {
1337 /* delete a single expect by tuple */
1338 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1339 if (err < 0)
1340 return err;
1342 /* bump usage count to 2 */
1343 exp = ip_conntrack_expect_find(&tuple);
1344 if (!exp)
1345 return -ENOENT;
1347 if (cda[CTA_EXPECT_ID-1]) {
1348 u_int32_t id =
1349 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1350 if (exp->id != ntohl(id)) {
1351 ip_conntrack_expect_put(exp);
1352 return -ENOENT;
1356 /* after list removal, usage count == 1 */
1357 ip_conntrack_unexpect_related(exp);
1358 /* have to put what we 'get' above.
1359 * after this line usage count == 0 */
1360 ip_conntrack_expect_put(exp);
1361 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1362 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1364 /* delete all expectations for this helper */
1365 write_lock_bh(&ip_conntrack_lock);
1366 h = __ip_conntrack_helper_find_byname(name);
1367 if (!h) {
1368 write_unlock_bh(&ip_conntrack_lock);
1369 return -EINVAL;
1371 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1372 list) {
1373 if (exp->master->helper == h
1374 && del_timer(&exp->timeout)) {
1375 ip_ct_unlink_expect(exp);
1376 ip_conntrack_expect_put(exp);
1379 write_unlock(&ip_conntrack_lock);
1380 } else {
1381 /* This basically means we have to flush everything*/
1382 write_lock_bh(&ip_conntrack_lock);
1383 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1384 list) {
1385 if (del_timer(&exp->timeout)) {
1386 ip_ct_unlink_expect(exp);
1387 ip_conntrack_expect_put(exp);
1390 write_unlock_bh(&ip_conntrack_lock);
1393 return 0;
1395 static int
1396 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1398 return -EOPNOTSUPP;
1401 static int
1402 ctnetlink_create_expect(struct nfattr *cda[])
1404 struct ip_conntrack_tuple tuple, mask, master_tuple;
1405 struct ip_conntrack_tuple_hash *h = NULL;
1406 struct ip_conntrack_expect *exp;
1407 struct ip_conntrack *ct;
1408 int err = 0;
1410 DEBUGP("entered %s\n", __FUNCTION__);
1412 /* caller guarantees that those three CTA_EXPECT_* exist */
1413 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1414 if (err < 0)
1415 return err;
1416 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
1417 if (err < 0)
1418 return err;
1419 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
1420 if (err < 0)
1421 return err;
1423 /* Look for master conntrack of this expectation */
1424 h = ip_conntrack_find_get(&master_tuple, NULL);
1425 if (!h)
1426 return -ENOENT;
1427 ct = tuplehash_to_ctrack(h);
1429 if (!ct->helper) {
1430 /* such conntrack hasn't got any helper, abort */
1431 err = -EINVAL;
1432 goto out;
1435 exp = ip_conntrack_expect_alloc(ct);
1436 if (!exp) {
1437 err = -ENOMEM;
1438 goto out;
1441 exp->expectfn = NULL;
1442 exp->flags = 0;
1443 exp->master = ct;
1444 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1445 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1447 err = ip_conntrack_expect_related(exp);
1448 ip_conntrack_expect_put(exp);
1450 out:
1451 ip_conntrack_put(tuplehash_to_ctrack(h));
1452 return err;
1455 static int
1456 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1457 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1459 struct ip_conntrack_tuple tuple;
1460 struct ip_conntrack_expect *exp;
1461 int err = 0;
1463 DEBUGP("entered %s\n", __FUNCTION__);
1465 if (!cda[CTA_EXPECT_TUPLE-1]
1466 || !cda[CTA_EXPECT_MASK-1]
1467 || !cda[CTA_EXPECT_MASTER-1])
1468 return -EINVAL;
1470 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1471 if (err < 0)
1472 return err;
1474 write_lock_bh(&ip_conntrack_lock);
1475 exp = __ip_conntrack_expect_find(&tuple);
1477 if (!exp) {
1478 write_unlock_bh(&ip_conntrack_lock);
1479 err = -ENOENT;
1480 if (nlh->nlmsg_flags & NLM_F_CREATE)
1481 err = ctnetlink_create_expect(cda);
1482 return err;
1485 err = -EEXIST;
1486 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1487 err = ctnetlink_change_expect(exp, cda);
1488 write_unlock_bh(&ip_conntrack_lock);
1490 DEBUGP("leaving\n");
1492 return err;
1495 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1496 static struct notifier_block ctnl_notifier = {
1497 .notifier_call = ctnetlink_conntrack_event,
1500 static struct notifier_block ctnl_notifier_exp = {
1501 .notifier_call = ctnetlink_expect_event,
1503 #endif
1505 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1506 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1507 .attr_count = CTA_MAX,
1508 .cap_required = CAP_NET_ADMIN },
1509 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1510 .attr_count = CTA_MAX,
1511 .cap_required = CAP_NET_ADMIN },
1512 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1513 .attr_count = CTA_MAX,
1514 .cap_required = CAP_NET_ADMIN },
1515 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1516 .attr_count = CTA_MAX,
1517 .cap_required = CAP_NET_ADMIN },
1520 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1521 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1522 .attr_count = CTA_EXPECT_MAX,
1523 .cap_required = CAP_NET_ADMIN },
1524 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1525 .attr_count = CTA_EXPECT_MAX,
1526 .cap_required = CAP_NET_ADMIN },
1527 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1528 .attr_count = CTA_EXPECT_MAX,
1529 .cap_required = CAP_NET_ADMIN },
1532 static struct nfnetlink_subsystem ctnl_subsys = {
1533 .name = "conntrack",
1534 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1535 .cb_count = IPCTNL_MSG_MAX,
1536 .cb = ctnl_cb,
1539 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1540 .name = "conntrack_expect",
1541 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1542 .cb_count = IPCTNL_MSG_EXP_MAX,
1543 .cb = ctnl_exp_cb,
1546 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1548 static int __init ctnetlink_init(void)
1550 int ret;
1552 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1553 ret = nfnetlink_subsys_register(&ctnl_subsys);
1554 if (ret < 0) {
1555 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1556 goto err_out;
1559 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1560 if (ret < 0) {
1561 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1562 goto err_unreg_subsys;
1565 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1566 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1567 if (ret < 0) {
1568 printk("ctnetlink_init: cannot register notifier.\n");
1569 goto err_unreg_exp_subsys;
1572 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1573 if (ret < 0) {
1574 printk("ctnetlink_init: cannot expect register notifier.\n");
1575 goto err_unreg_notifier;
1577 #endif
1579 return 0;
1581 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1582 err_unreg_notifier:
1583 ip_conntrack_unregister_notifier(&ctnl_notifier);
1584 err_unreg_exp_subsys:
1585 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1586 #endif
1587 err_unreg_subsys:
1588 nfnetlink_subsys_unregister(&ctnl_subsys);
1589 err_out:
1590 return ret;
1593 static void __exit ctnetlink_exit(void)
1595 printk("ctnetlink: unregistering from nfnetlink.\n");
1597 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1598 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1599 ip_conntrack_unregister_notifier(&ctnl_notifier);
1600 #endif
1602 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1603 nfnetlink_subsys_unregister(&ctnl_subsys);
1604 return;
1607 module_init(ctnetlink_init);
1608 module_exit(ctnetlink_exit);