[NETFILTER] ctnetlink: More thorough size checking of attributes
[linux-2.6/libata-dev.git] / net / ipv4 / netfilter / ip_conntrack_netlink.c
blobf5e5e3158670f783ed46bc64688b60ce6aeb89e8
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 size_t 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 size_t 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 size_t 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 const size_t cta_min_nat[CTA_NAT_MAX] = {
618 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
619 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
622 static inline int
623 ctnetlink_parse_nat(struct nfattr *cda[],
624 const struct ip_conntrack *ct, struct ip_nat_range *range)
626 struct nfattr *tb[CTA_NAT_MAX];
627 int err;
629 DEBUGP("entered %s\n", __FUNCTION__);
631 memset(range, 0, sizeof(*range));
633 nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]);
635 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
636 return -EINVAL;
638 if (tb[CTA_NAT_MINIP-1])
639 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
641 if (!tb[CTA_NAT_MAXIP-1])
642 range->max_ip = range->min_ip;
643 else
644 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
646 if (range->min_ip)
647 range->flags |= IP_NAT_RANGE_MAP_IPS;
649 if (!tb[CTA_NAT_PROTO-1])
650 return 0;
652 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
653 if (err < 0)
654 return err;
656 DEBUGP("leaving\n");
657 return 0;
659 #endif
661 static inline int
662 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
664 struct nfattr *tb[CTA_HELP_MAX];
666 DEBUGP("entered %s\n", __FUNCTION__);
668 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
670 if (!tb[CTA_HELP_NAME-1])
671 return -EINVAL;
673 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
675 return 0;
678 static const size_t cta_min[CTA_MAX] = {
679 [CTA_STATUS-1] = sizeof(u_int32_t),
680 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
681 [CTA_MARK-1] = sizeof(u_int32_t),
682 [CTA_USE-1] = sizeof(u_int32_t),
683 [CTA_ID-1] = sizeof(u_int32_t)
686 static int
687 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
688 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
690 struct ip_conntrack_tuple_hash *h;
691 struct ip_conntrack_tuple tuple;
692 struct ip_conntrack *ct;
693 int err = 0;
695 DEBUGP("entered %s\n", __FUNCTION__);
697 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
698 return -EINVAL;
700 if (cda[CTA_TUPLE_ORIG-1])
701 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
702 else if (cda[CTA_TUPLE_REPLY-1])
703 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
704 else {
705 /* Flush the whole table */
706 ip_conntrack_flush();
707 return 0;
710 if (err < 0)
711 return err;
713 h = ip_conntrack_find_get(&tuple, NULL);
714 if (!h) {
715 DEBUGP("tuple not found in conntrack hash\n");
716 return -ENOENT;
719 ct = tuplehash_to_ctrack(h);
721 if (cda[CTA_ID-1]) {
722 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
723 if (ct->id != id) {
724 ip_conntrack_put(ct);
725 return -ENOENT;
728 if (del_timer(&ct->timeout)) {
729 ip_conntrack_put(ct);
730 ct->timeout.function((unsigned long)ct);
731 return 0;
733 ip_conntrack_put(ct);
734 DEBUGP("leaving\n");
736 return 0;
739 static int
740 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
741 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
743 struct ip_conntrack_tuple_hash *h;
744 struct ip_conntrack_tuple tuple;
745 struct ip_conntrack *ct;
746 struct sk_buff *skb2 = NULL;
747 int err = 0;
749 DEBUGP("entered %s\n", __FUNCTION__);
751 if (nlh->nlmsg_flags & NLM_F_DUMP) {
752 struct nfgenmsg *msg = NLMSG_DATA(nlh);
753 u32 rlen;
755 if (msg->nfgen_family != AF_INET)
756 return -EAFNOSUPPORT;
758 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
759 IPCTNL_MSG_CT_GET_CTRZERO) {
760 #ifdef CONFIG_IP_NF_CT_ACCT
761 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
762 ctnetlink_dump_table_w,
763 ctnetlink_done)) != 0)
764 return -EINVAL;
765 #else
766 return -ENOTSUPP;
767 #endif
768 } else {
769 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
770 ctnetlink_dump_table,
771 ctnetlink_done)) != 0)
772 return -EINVAL;
775 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
776 if (rlen > skb->len)
777 rlen = skb->len;
778 skb_pull(skb, rlen);
779 return 0;
782 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
783 return -EINVAL;
785 if (cda[CTA_TUPLE_ORIG-1])
786 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
787 else if (cda[CTA_TUPLE_REPLY-1])
788 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
789 else
790 return -EINVAL;
792 if (err < 0)
793 return err;
795 h = ip_conntrack_find_get(&tuple, NULL);
796 if (!h) {
797 DEBUGP("tuple not found in conntrack hash");
798 return -ENOENT;
800 DEBUGP("tuple found\n");
801 ct = tuplehash_to_ctrack(h);
803 err = -ENOMEM;
804 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
805 if (!skb2) {
806 ip_conntrack_put(ct);
807 return -ENOMEM;
809 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
811 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
812 IPCTNL_MSG_CT_NEW, 1, ct);
813 ip_conntrack_put(ct);
814 if (err <= 0)
815 goto free;
817 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
818 if (err < 0)
819 goto out;
821 DEBUGP("leaving\n");
822 return 0;
824 free:
825 kfree_skb(skb2);
826 out:
827 return err;
830 static inline int
831 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
833 unsigned long d;
834 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
835 d = ct->status ^ status;
837 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
838 /* unchangeable */
839 return -EINVAL;
841 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
842 /* SEEN_REPLY bit can only be set */
843 return -EINVAL;
846 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
847 /* ASSURED bit can only be set */
848 return -EINVAL;
850 if (cda[CTA_NAT-1]) {
851 #ifndef CONFIG_IP_NF_NAT_NEEDED
852 return -EINVAL;
853 #else
854 unsigned int hooknum;
855 struct ip_nat_range range;
857 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
858 return -EINVAL;
860 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
861 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
862 htons(range.min.all), htons(range.max.all));
864 /* This is tricky but it works. ip_nat_setup_info needs the
865 * hook number as parameter, so let's do the correct
866 * conversion and run away */
867 if (status & IPS_SRC_NAT_DONE)
868 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
869 else if (status & IPS_DST_NAT_DONE)
870 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
871 else
872 return -EINVAL; /* Missing NAT flags */
874 DEBUGP("NAT status: %lu\n",
875 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
877 if (ip_nat_initialized(ct, hooknum))
878 return -EEXIST;
879 ip_nat_setup_info(ct, &range, hooknum);
881 DEBUGP("NAT status after setup_info: %lu\n",
882 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
883 #endif
886 /* Be careful here, modifying NAT bits can screw up things,
887 * so don't let users modify them directly if they don't pass
888 * ip_nat_range. */
889 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
890 return 0;
894 static inline int
895 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
897 struct ip_conntrack_helper *helper;
898 char *helpname;
899 int err;
901 DEBUGP("entered %s\n", __FUNCTION__);
903 /* don't change helper of sibling connections */
904 if (ct->master)
905 return -EINVAL;
907 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
908 if (err < 0)
909 return err;
911 helper = __ip_conntrack_helper_find_byname(helpname);
912 if (!helper) {
913 if (!strcmp(helpname, ""))
914 helper = NULL;
915 else
916 return -EINVAL;
919 if (ct->helper) {
920 if (!helper) {
921 /* we had a helper before ... */
922 ip_ct_remove_expectations(ct);
923 ct->helper = NULL;
924 } else {
925 /* need to zero data of old helper */
926 memset(&ct->help, 0, sizeof(ct->help));
930 ct->helper = helper;
932 return 0;
935 static inline int
936 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
938 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
940 if (!del_timer(&ct->timeout))
941 return -ETIME;
943 ct->timeout.expires = jiffies + timeout * HZ;
944 add_timer(&ct->timeout);
946 return 0;
949 static inline int
950 ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
952 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
953 struct ip_conntrack_protocol *proto;
954 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
955 int err = 0;
957 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
959 proto = ip_conntrack_proto_find_get(npt);
960 if (!proto)
961 return -EINVAL;
963 if (proto->from_nfattr)
964 err = proto->from_nfattr(tb, ct);
965 ip_conntrack_proto_put(proto);
967 return err;
970 static int
971 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
973 int err;
975 DEBUGP("entered %s\n", __FUNCTION__);
977 if (cda[CTA_HELP-1]) {
978 err = ctnetlink_change_helper(ct, cda);
979 if (err < 0)
980 return err;
983 if (cda[CTA_TIMEOUT-1]) {
984 err = ctnetlink_change_timeout(ct, cda);
985 if (err < 0)
986 return err;
989 if (cda[CTA_STATUS-1]) {
990 err = ctnetlink_change_status(ct, cda);
991 if (err < 0)
992 return err;
995 if (cda[CTA_PROTOINFO-1]) {
996 err = ctnetlink_change_protoinfo(ct, cda);
997 if (err < 0)
998 return err;
1001 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1002 if (cda[CTA_MARK-1])
1003 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1004 #endif
1006 DEBUGP("all done\n");
1007 return 0;
1010 static int
1011 ctnetlink_create_conntrack(struct nfattr *cda[],
1012 struct ip_conntrack_tuple *otuple,
1013 struct ip_conntrack_tuple *rtuple)
1015 struct ip_conntrack *ct;
1016 int err = -EINVAL;
1018 DEBUGP("entered %s\n", __FUNCTION__);
1020 ct = ip_conntrack_alloc(otuple, rtuple);
1021 if (ct == NULL || IS_ERR(ct))
1022 return -ENOMEM;
1024 if (!cda[CTA_TIMEOUT-1])
1025 goto err;
1026 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1028 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1029 ct->status |= IPS_CONFIRMED;
1031 err = ctnetlink_change_status(ct, cda);
1032 if (err < 0)
1033 goto err;
1035 if (cda[CTA_PROTOINFO-1]) {
1036 err = ctnetlink_change_protoinfo(ct, cda);
1037 if (err < 0)
1038 return err;
1041 ct->helper = ip_conntrack_helper_find_get(rtuple);
1043 add_timer(&ct->timeout);
1044 ip_conntrack_hash_insert(ct);
1046 if (ct->helper)
1047 ip_conntrack_helper_put(ct->helper);
1049 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1050 if (cda[CTA_MARK-1])
1051 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1052 #endif
1054 DEBUGP("conntrack with id %u inserted\n", ct->id);
1055 return 0;
1057 err:
1058 ip_conntrack_free(ct);
1059 return err;
1062 static int
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;
1068 int err = 0;
1070 DEBUGP("entered %s\n", __FUNCTION__);
1072 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1073 return -EINVAL;
1075 if (cda[CTA_TUPLE_ORIG-1]) {
1076 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1077 if (err < 0)
1078 return err;
1081 if (cda[CTA_TUPLE_REPLY-1]) {
1082 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1083 if (err < 0)
1084 return err;
1087 write_lock_bh(&ip_conntrack_lock);
1088 if (cda[CTA_TUPLE_ORIG-1])
1089 h = __ip_conntrack_find(&otuple, NULL);
1090 else if (cda[CTA_TUPLE_REPLY-1])
1091 h = __ip_conntrack_find(&rtuple, NULL);
1093 if (h == NULL) {
1094 write_unlock_bh(&ip_conntrack_lock);
1095 DEBUGP("no such conntrack, create new\n");
1096 err = -ENOENT;
1097 if (nlh->nlmsg_flags & NLM_F_CREATE)
1098 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1099 return err;
1101 /* implicit 'else' */
1103 /* we only allow nat config for new conntracks */
1104 if (cda[CTA_NAT-1]) {
1105 err = -EINVAL;
1106 goto out_unlock;
1109 /* We manipulate the conntrack inside the global conntrack table lock,
1110 * so there's no need to increase the refcount */
1111 DEBUGP("conntrack found\n");
1112 err = -EEXIST;
1113 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1114 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1116 out_unlock:
1117 write_unlock_bh(&ip_conntrack_lock);
1118 return err;
1121 /***********************************************************************
1122 * EXPECT
1123 ***********************************************************************/
1125 static inline int
1126 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1127 const struct ip_conntrack_tuple *tuple,
1128 enum ctattr_expect type)
1130 struct nfattr *nest_parms = NFA_NEST(skb, type);
1132 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1133 goto nfattr_failure;
1135 NFA_NEST_END(skb, nest_parms);
1137 return 0;
1139 nfattr_failure:
1140 return -1;
1143 static inline int
1144 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1145 const struct ip_conntrack_expect *exp)
1147 struct ip_conntrack *master = exp->master;
1148 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1149 u_int32_t id = htonl(exp->id);
1151 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1152 goto nfattr_failure;
1153 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1154 goto nfattr_failure;
1155 if (ctnetlink_exp_dump_tuple(skb,
1156 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1157 CTA_EXPECT_MASTER) < 0)
1158 goto nfattr_failure;
1160 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1161 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1163 return 0;
1165 nfattr_failure:
1166 return -1;
1169 static int
1170 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1171 int event,
1172 int nowait,
1173 const struct ip_conntrack_expect *exp)
1175 struct nlmsghdr *nlh;
1176 struct nfgenmsg *nfmsg;
1177 unsigned char *b;
1179 b = skb->tail;
1181 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1182 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1183 nfmsg = NLMSG_DATA(nlh);
1185 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1186 nfmsg->nfgen_family = AF_INET;
1187 nfmsg->version = NFNETLINK_V0;
1188 nfmsg->res_id = 0;
1190 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1191 goto nfattr_failure;
1193 nlh->nlmsg_len = skb->tail - b;
1194 return skb->len;
1196 nlmsg_failure:
1197 nfattr_failure:
1198 skb_trim(skb, b - skb->data);
1199 return -1;
1202 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1203 static int ctnetlink_expect_event(struct notifier_block *this,
1204 unsigned long events, void *ptr)
1206 struct nlmsghdr *nlh;
1207 struct nfgenmsg *nfmsg;
1208 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1209 struct sk_buff *skb;
1210 unsigned int type;
1211 unsigned char *b;
1212 int flags = 0;
1213 u16 proto;
1215 if (events & IPEXP_NEW) {
1216 type = IPCTNL_MSG_EXP_NEW;
1217 flags = NLM_F_CREATE|NLM_F_EXCL;
1218 } else
1219 return NOTIFY_DONE;
1221 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1222 if (!skb)
1223 return NOTIFY_DONE;
1225 b = skb->tail;
1227 type |= NFNL_SUBSYS_CTNETLINK << 8;
1228 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1229 nfmsg = NLMSG_DATA(nlh);
1231 nlh->nlmsg_flags = flags;
1232 nfmsg->nfgen_family = AF_INET;
1233 nfmsg->version = NFNETLINK_V0;
1234 nfmsg->res_id = 0;
1236 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1237 goto nfattr_failure;
1239 nlh->nlmsg_len = skb->tail - b;
1240 proto = exp->tuple.dst.protonum;
1241 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1242 return NOTIFY_DONE;
1244 nlmsg_failure:
1245 nfattr_failure:
1246 kfree_skb(skb);
1247 return NOTIFY_DONE;
1249 #endif
1251 static int
1252 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1254 struct ip_conntrack_expect *exp = NULL;
1255 struct list_head *i;
1256 u_int32_t *id = (u_int32_t *) &cb->args[0];
1258 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1260 read_lock_bh(&ip_conntrack_lock);
1261 list_for_each_prev(i, &ip_conntrack_expect_list) {
1262 exp = (struct ip_conntrack_expect *) i;
1263 if (exp->id <= *id)
1264 continue;
1265 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1266 cb->nlh->nlmsg_seq,
1267 IPCTNL_MSG_EXP_NEW,
1268 1, exp) < 0)
1269 goto out;
1270 *id = exp->id;
1272 out:
1273 read_unlock_bh(&ip_conntrack_lock);
1275 DEBUGP("leaving, last id=%llu\n", *id);
1277 return skb->len;
1280 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1281 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1282 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1285 static int
1286 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1287 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1289 struct ip_conntrack_tuple tuple;
1290 struct ip_conntrack_expect *exp;
1291 struct sk_buff *skb2;
1292 int err = 0;
1294 DEBUGP("entered %s\n", __FUNCTION__);
1296 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1297 return -EINVAL;
1299 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1300 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1301 u32 rlen;
1303 if (msg->nfgen_family != AF_INET)
1304 return -EAFNOSUPPORT;
1306 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1307 ctnetlink_exp_dump_table,
1308 ctnetlink_done)) != 0)
1309 return -EINVAL;
1310 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1311 if (rlen > skb->len)
1312 rlen = skb->len;
1313 skb_pull(skb, rlen);
1314 return 0;
1317 if (cda[CTA_EXPECT_MASTER-1])
1318 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
1319 else
1320 return -EINVAL;
1322 if (err < 0)
1323 return err;
1325 exp = ip_conntrack_expect_find(&tuple);
1326 if (!exp)
1327 return -ENOENT;
1329 if (cda[CTA_EXPECT_ID-1]) {
1330 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1331 if (exp->id != ntohl(id)) {
1332 ip_conntrack_expect_put(exp);
1333 return -ENOENT;
1337 err = -ENOMEM;
1338 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1339 if (!skb2)
1340 goto out;
1341 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1343 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1344 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1345 1, exp);
1346 if (err <= 0)
1347 goto free;
1349 ip_conntrack_expect_put(exp);
1351 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1353 free:
1354 kfree_skb(skb2);
1355 out:
1356 ip_conntrack_expect_put(exp);
1357 return err;
1360 static int
1361 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1362 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1364 struct ip_conntrack_expect *exp, *tmp;
1365 struct ip_conntrack_tuple tuple;
1366 struct ip_conntrack_helper *h;
1367 int err;
1369 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1370 return -EINVAL;
1372 if (cda[CTA_EXPECT_TUPLE-1]) {
1373 /* delete a single expect by tuple */
1374 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1375 if (err < 0)
1376 return err;
1378 /* bump usage count to 2 */
1379 exp = ip_conntrack_expect_find(&tuple);
1380 if (!exp)
1381 return -ENOENT;
1383 if (cda[CTA_EXPECT_ID-1]) {
1384 u_int32_t id =
1385 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1386 if (exp->id != ntohl(id)) {
1387 ip_conntrack_expect_put(exp);
1388 return -ENOENT;
1392 /* after list removal, usage count == 1 */
1393 ip_conntrack_unexpect_related(exp);
1394 /* have to put what we 'get' above.
1395 * after this line usage count == 0 */
1396 ip_conntrack_expect_put(exp);
1397 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1398 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1400 /* delete all expectations for this helper */
1401 write_lock_bh(&ip_conntrack_lock);
1402 h = __ip_conntrack_helper_find_byname(name);
1403 if (!h) {
1404 write_unlock_bh(&ip_conntrack_lock);
1405 return -EINVAL;
1407 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1408 list) {
1409 if (exp->master->helper == h
1410 && del_timer(&exp->timeout)) {
1411 ip_ct_unlink_expect(exp);
1412 ip_conntrack_expect_put(exp);
1415 write_unlock_bh(&ip_conntrack_lock);
1416 } else {
1417 /* This basically means we have to flush everything*/
1418 write_lock_bh(&ip_conntrack_lock);
1419 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1420 list) {
1421 if (del_timer(&exp->timeout)) {
1422 ip_ct_unlink_expect(exp);
1423 ip_conntrack_expect_put(exp);
1426 write_unlock_bh(&ip_conntrack_lock);
1429 return 0;
1431 static int
1432 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1434 return -EOPNOTSUPP;
1437 static int
1438 ctnetlink_create_expect(struct nfattr *cda[])
1440 struct ip_conntrack_tuple tuple, mask, master_tuple;
1441 struct ip_conntrack_tuple_hash *h = NULL;
1442 struct ip_conntrack_expect *exp;
1443 struct ip_conntrack *ct;
1444 int err = 0;
1446 DEBUGP("entered %s\n", __FUNCTION__);
1448 /* caller guarantees that those three CTA_EXPECT_* exist */
1449 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1450 if (err < 0)
1451 return err;
1452 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
1453 if (err < 0)
1454 return err;
1455 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
1456 if (err < 0)
1457 return err;
1459 /* Look for master conntrack of this expectation */
1460 h = ip_conntrack_find_get(&master_tuple, NULL);
1461 if (!h)
1462 return -ENOENT;
1463 ct = tuplehash_to_ctrack(h);
1465 if (!ct->helper) {
1466 /* such conntrack hasn't got any helper, abort */
1467 err = -EINVAL;
1468 goto out;
1471 exp = ip_conntrack_expect_alloc(ct);
1472 if (!exp) {
1473 err = -ENOMEM;
1474 goto out;
1477 exp->expectfn = NULL;
1478 exp->flags = 0;
1479 exp->master = ct;
1480 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1481 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1483 err = ip_conntrack_expect_related(exp);
1484 ip_conntrack_expect_put(exp);
1486 out:
1487 ip_conntrack_put(tuplehash_to_ctrack(h));
1488 return err;
1491 static int
1492 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1493 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1495 struct ip_conntrack_tuple tuple;
1496 struct ip_conntrack_expect *exp;
1497 int err = 0;
1499 DEBUGP("entered %s\n", __FUNCTION__);
1501 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1502 return -EINVAL;
1504 if (!cda[CTA_EXPECT_TUPLE-1]
1505 || !cda[CTA_EXPECT_MASK-1]
1506 || !cda[CTA_EXPECT_MASTER-1])
1507 return -EINVAL;
1509 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1510 if (err < 0)
1511 return err;
1513 write_lock_bh(&ip_conntrack_lock);
1514 exp = __ip_conntrack_expect_find(&tuple);
1516 if (!exp) {
1517 write_unlock_bh(&ip_conntrack_lock);
1518 err = -ENOENT;
1519 if (nlh->nlmsg_flags & NLM_F_CREATE)
1520 err = ctnetlink_create_expect(cda);
1521 return err;
1524 err = -EEXIST;
1525 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1526 err = ctnetlink_change_expect(exp, cda);
1527 write_unlock_bh(&ip_conntrack_lock);
1529 DEBUGP("leaving\n");
1531 return err;
1534 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1535 static struct notifier_block ctnl_notifier = {
1536 .notifier_call = ctnetlink_conntrack_event,
1539 static struct notifier_block ctnl_notifier_exp = {
1540 .notifier_call = ctnetlink_expect_event,
1542 #endif
1544 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1545 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1546 .attr_count = CTA_MAX,
1547 .cap_required = CAP_NET_ADMIN },
1548 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1549 .attr_count = CTA_MAX,
1550 .cap_required = CAP_NET_ADMIN },
1551 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1552 .attr_count = CTA_MAX,
1553 .cap_required = CAP_NET_ADMIN },
1554 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1555 .attr_count = CTA_MAX,
1556 .cap_required = CAP_NET_ADMIN },
1559 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1560 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1561 .attr_count = CTA_EXPECT_MAX,
1562 .cap_required = CAP_NET_ADMIN },
1563 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1564 .attr_count = CTA_EXPECT_MAX,
1565 .cap_required = CAP_NET_ADMIN },
1566 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1567 .attr_count = CTA_EXPECT_MAX,
1568 .cap_required = CAP_NET_ADMIN },
1571 static struct nfnetlink_subsystem ctnl_subsys = {
1572 .name = "conntrack",
1573 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1574 .cb_count = IPCTNL_MSG_MAX,
1575 .cb = ctnl_cb,
1578 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1579 .name = "conntrack_expect",
1580 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1581 .cb_count = IPCTNL_MSG_EXP_MAX,
1582 .cb = ctnl_exp_cb,
1585 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1587 static int __init ctnetlink_init(void)
1589 int ret;
1591 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1592 ret = nfnetlink_subsys_register(&ctnl_subsys);
1593 if (ret < 0) {
1594 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1595 goto err_out;
1598 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1599 if (ret < 0) {
1600 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1601 goto err_unreg_subsys;
1604 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1605 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1606 if (ret < 0) {
1607 printk("ctnetlink_init: cannot register notifier.\n");
1608 goto err_unreg_exp_subsys;
1611 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1612 if (ret < 0) {
1613 printk("ctnetlink_init: cannot expect register notifier.\n");
1614 goto err_unreg_notifier;
1616 #endif
1618 return 0;
1620 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1621 err_unreg_notifier:
1622 ip_conntrack_unregister_notifier(&ctnl_notifier);
1623 err_unreg_exp_subsys:
1624 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1625 #endif
1626 err_unreg_subsys:
1627 nfnetlink_subsys_unregister(&ctnl_subsys);
1628 err_out:
1629 return ret;
1632 static void __exit ctnetlink_exit(void)
1634 printk("ctnetlink: unregistering from nfnetlink.\n");
1636 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1637 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1638 ip_conntrack_unregister_notifier(&ctnl_notifier);
1639 #endif
1641 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1642 nfnetlink_subsys_unregister(&ctnl_subsys);
1643 return;
1646 module_init(ctnetlink_init);
1647 module_exit(ctnetlink_exit);