[ARM] 3335/1: Old-abi Thumb sys_syscall broken
[linux-2.6/linux-loongson.git] / net / ipv4 / netfilter / ip_conntrack_netlink.c
blob01bd7cab936762208bbd5b678bc57d981d1cf1ae
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";
47 #if 0
48 #define DEBUGP printk
49 #else
50 #define DEBUGP(format, args...)
51 #endif
54 static inline int
55 ctnetlink_dump_tuples_proto(struct sk_buff *skb,
56 const struct ip_conntrack_tuple *tuple,
57 struct ip_conntrack_protocol *proto)
59 int ret = 0;
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);
69 return ret;
71 nfattr_failure:
72 return -1;
75 static inline int
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);
86 return 0;
88 nfattr_failure:
89 return -1;
92 static inline int
93 ctnetlink_dump_tuples(struct sk_buff *skb,
94 const struct ip_conntrack_tuple *tuple)
96 int ret;
97 struct ip_conntrack_protocol *proto;
99 ret = ctnetlink_dump_tuples_ip(skb, tuple);
100 if (unlikely(ret < 0))
101 return ret;
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);
107 return ret;
110 static inline int
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);
115 return 0;
117 nfattr_failure:
118 return -1;
121 static inline int
122 ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct)
124 long timeout_l = ct->timeout.expires - jiffies;
125 u_int32_t timeout;
127 if (timeout_l < 0)
128 timeout = 0;
129 else
130 timeout = htonl(timeout_l / HZ);
132 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
133 return 0;
135 nfattr_failure:
136 return -1;
139 static inline int
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;
145 int ret;
147 if (!proto->to_nfattr) {
148 ip_conntrack_proto_put(proto);
149 return 0;
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);
160 return ret;
162 nfattr_failure:
163 return -1;
166 static inline int
167 ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
169 struct nfattr *nest_helper;
171 if (!ct->helper)
172 return 0;
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);
182 return 0;
184 nfattr_failure:
185 return -1;
188 #ifdef CONFIG_IP_NF_CT_ACCT
189 static inline int
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);
195 u_int32_t tmp;
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);
205 return 0;
207 nfattr_failure:
208 return -1;
210 #else
211 #define ctnetlink_dump_counters(a, b, c) (0)
212 #endif
214 #ifdef CONFIG_IP_NF_CONNTRACK_MARK
215 static inline int
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);
221 return 0;
223 nfattr_failure:
224 return -1;
226 #else
227 #define ctnetlink_dump_mark(a, b) (0)
228 #endif
230 static inline int
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);
235 return 0;
237 nfattr_failure:
238 return -1;
241 static inline int
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);
247 return 0;
249 nfattr_failure:
250 return -1;
253 #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
255 static int
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;
263 unsigned char *b;
265 b = skb->tail;
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;
274 nfmsg->res_id = 0;
276 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
277 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
278 goto nfattr_failure;
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)
283 goto nfattr_failure;
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)
295 goto nfattr_failure;
297 nlh->nlmsg_len = skb->tail - b;
298 return skb->len;
300 nlmsg_failure:
301 nfattr_failure:
302 skb_trim(skb, b - skb->data);
303 return -1;
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;
314 struct sk_buff *skb;
315 unsigned int type;
316 unsigned char *b;
317 unsigned int flags = 0, group;
319 /* ignore our fake conntrack entry */
320 if (ct == &ip_conntrack_untracked)
321 return NOTIFY_DONE;
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 */
330 events = ~0UL;
331 group = NFNLGRP_CONNTRACK_NEW;
332 } else if (events & (IPCT_STATUS |
333 IPCT_PROTOINFO |
334 IPCT_HELPER |
335 IPCT_HELPINFO |
336 IPCT_NATINFO)) {
337 type = IPCTNL_MSG_CT_NEW;
338 group = NFNLGRP_CONNTRACK_UPDATE;
339 } else
340 return NOTIFY_DONE;
342 if (!nfnetlink_has_listeners(group))
343 return NOTIFY_DONE;
345 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
346 if (!skb)
347 return NOTIFY_DONE;
349 b = skb->tail;
351 type |= NFNL_SUBSYS_CTNETLINK << 8;
352 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
353 nfmsg = NLMSG_DATA(nlh);
355 nlh->nlmsg_flags = flags;
356 nfmsg->nfgen_family = AF_INET;
357 nfmsg->version = NFNETLINK_V0;
358 nfmsg->res_id = 0;
360 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
361 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
362 goto nfattr_failure;
363 NFA_NEST_END(skb, nest_parms);
365 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
366 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
367 goto nfattr_failure;
368 NFA_NEST_END(skb, nest_parms);
370 /* NAT stuff is now a status flag */
371 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
372 && ctnetlink_dump_status(skb, ct) < 0)
373 goto nfattr_failure;
374 if (events & IPCT_REFRESH
375 && ctnetlink_dump_timeout(skb, ct) < 0)
376 goto nfattr_failure;
377 if (events & IPCT_PROTOINFO
378 && ctnetlink_dump_protoinfo(skb, ct) < 0)
379 goto nfattr_failure;
380 if (events & IPCT_HELPINFO
381 && ctnetlink_dump_helpinfo(skb, ct) < 0)
382 goto nfattr_failure;
384 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
385 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
386 goto nfattr_failure;
388 nlh->nlmsg_len = skb->tail - b;
389 nfnetlink_send(skb, 0, group, 0);
390 return NOTIFY_DONE;
392 nlmsg_failure:
393 nfattr_failure:
394 kfree_skb(skb);
395 return NOTIFY_DONE;
397 #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
399 static int ctnetlink_done(struct netlink_callback *cb)
401 DEBUGP("entered %s\n", __FUNCTION__);
402 return 0;
405 static int
406 ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
408 struct ip_conntrack *ct = NULL;
409 struct ip_conntrack_tuple_hash *h;
410 struct list_head *i;
411 u_int32_t *id = (u_int32_t *) &cb->args[1];
413 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
414 cb->args[0], *id);
416 read_lock_bh(&ip_conntrack_lock);
417 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
418 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
419 h = (struct ip_conntrack_tuple_hash *) i;
420 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
421 continue;
422 ct = tuplehash_to_ctrack(h);
423 if (ct->id <= *id)
424 continue;
425 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
426 cb->nlh->nlmsg_seq,
427 IPCTNL_MSG_CT_NEW,
428 1, ct) < 0)
429 goto out;
430 *id = ct->id;
433 out:
434 read_unlock_bh(&ip_conntrack_lock);
436 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
438 return skb->len;
441 #ifdef CONFIG_IP_NF_CT_ACCT
442 static int
443 ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
445 struct ip_conntrack *ct = NULL;
446 struct ip_conntrack_tuple_hash *h;
447 struct list_head *i;
448 u_int32_t *id = (u_int32_t *) &cb->args[1];
450 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
451 cb->args[0], *id);
453 write_lock_bh(&ip_conntrack_lock);
454 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
455 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
456 h = (struct ip_conntrack_tuple_hash *) i;
457 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
458 continue;
459 ct = tuplehash_to_ctrack(h);
460 if (ct->id <= *id)
461 continue;
462 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
463 cb->nlh->nlmsg_seq,
464 IPCTNL_MSG_CT_NEW,
465 1, ct) < 0)
466 goto out;
467 *id = ct->id;
469 memset(&ct->counters, 0, sizeof(ct->counters));
472 out:
473 write_unlock_bh(&ip_conntrack_lock);
475 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
477 return skb->len;
479 #endif
481 static const size_t cta_min_ip[CTA_IP_MAX] = {
482 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
483 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
486 static inline int
487 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple)
489 struct nfattr *tb[CTA_IP_MAX];
491 DEBUGP("entered %s\n", __FUNCTION__);
493 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
495 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
496 return -EINVAL;
498 if (!tb[CTA_IP_V4_SRC-1])
499 return -EINVAL;
500 tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
502 if (!tb[CTA_IP_V4_DST-1])
503 return -EINVAL;
504 tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
506 DEBUGP("leaving\n");
508 return 0;
511 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
512 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
513 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
514 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
515 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
516 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
517 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
520 static inline int
521 ctnetlink_parse_tuple_proto(struct nfattr *attr,
522 struct ip_conntrack_tuple *tuple)
524 struct nfattr *tb[CTA_PROTO_MAX];
525 struct ip_conntrack_protocol *proto;
526 int ret = 0;
528 DEBUGP("entered %s\n", __FUNCTION__);
530 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
532 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
533 return -EINVAL;
535 if (!tb[CTA_PROTO_NUM-1])
536 return -EINVAL;
537 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
539 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
541 if (likely(proto->nfattr_to_tuple))
542 ret = proto->nfattr_to_tuple(tb, tuple);
544 ip_conntrack_proto_put(proto);
546 return ret;
549 static inline int
550 ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
551 enum ctattr_tuple type)
553 struct nfattr *tb[CTA_TUPLE_MAX];
554 int err;
556 DEBUGP("entered %s\n", __FUNCTION__);
558 memset(tuple, 0, sizeof(*tuple));
560 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
562 if (!tb[CTA_TUPLE_IP-1])
563 return -EINVAL;
565 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
566 if (err < 0)
567 return err;
569 if (!tb[CTA_TUPLE_PROTO-1])
570 return -EINVAL;
572 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
573 if (err < 0)
574 return err;
576 /* orig and expect tuples get DIR_ORIGINAL */
577 if (type == CTA_TUPLE_REPLY)
578 tuple->dst.dir = IP_CT_DIR_REPLY;
579 else
580 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
582 DUMP_TUPLE(tuple);
584 DEBUGP("leaving\n");
586 return 0;
589 #ifdef CONFIG_IP_NF_NAT_NEEDED
590 static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
591 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
592 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
595 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
596 const struct ip_conntrack *ct,
597 struct ip_nat_range *range)
599 struct nfattr *tb[CTA_PROTONAT_MAX];
600 struct ip_nat_protocol *npt;
602 DEBUGP("entered %s\n", __FUNCTION__);
604 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
606 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
607 return -EINVAL;
609 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
611 if (!npt->nfattr_to_range) {
612 ip_nat_proto_put(npt);
613 return 0;
616 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
617 if (npt->nfattr_to_range(tb, range) > 0)
618 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
620 ip_nat_proto_put(npt);
622 DEBUGP("leaving\n");
623 return 0;
626 static const size_t cta_min_nat[CTA_NAT_MAX] = {
627 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
628 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
631 static inline int
632 ctnetlink_parse_nat(struct nfattr *cda[],
633 const struct ip_conntrack *ct, struct ip_nat_range *range)
635 struct nfattr *tb[CTA_NAT_MAX];
636 int err;
638 DEBUGP("entered %s\n", __FUNCTION__);
640 memset(range, 0, sizeof(*range));
642 nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]);
644 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
645 return -EINVAL;
647 if (tb[CTA_NAT_MINIP-1])
648 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
650 if (!tb[CTA_NAT_MAXIP-1])
651 range->max_ip = range->min_ip;
652 else
653 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
655 if (range->min_ip)
656 range->flags |= IP_NAT_RANGE_MAP_IPS;
658 if (!tb[CTA_NAT_PROTO-1])
659 return 0;
661 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
662 if (err < 0)
663 return err;
665 DEBUGP("leaving\n");
666 return 0;
668 #endif
670 static inline int
671 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
673 struct nfattr *tb[CTA_HELP_MAX];
675 DEBUGP("entered %s\n", __FUNCTION__);
677 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
679 if (!tb[CTA_HELP_NAME-1])
680 return -EINVAL;
682 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
684 return 0;
687 static const size_t cta_min[CTA_MAX] = {
688 [CTA_STATUS-1] = sizeof(u_int32_t),
689 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
690 [CTA_MARK-1] = sizeof(u_int32_t),
691 [CTA_USE-1] = sizeof(u_int32_t),
692 [CTA_ID-1] = sizeof(u_int32_t)
695 static int
696 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
697 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
699 struct ip_conntrack_tuple_hash *h;
700 struct ip_conntrack_tuple tuple;
701 struct ip_conntrack *ct;
702 int err = 0;
704 DEBUGP("entered %s\n", __FUNCTION__);
706 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
707 return -EINVAL;
709 if (cda[CTA_TUPLE_ORIG-1])
710 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
711 else if (cda[CTA_TUPLE_REPLY-1])
712 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
713 else {
714 /* Flush the whole table */
715 ip_conntrack_flush();
716 return 0;
719 if (err < 0)
720 return err;
722 h = ip_conntrack_find_get(&tuple, NULL);
723 if (!h) {
724 DEBUGP("tuple not found in conntrack hash\n");
725 return -ENOENT;
728 ct = tuplehash_to_ctrack(h);
730 if (cda[CTA_ID-1]) {
731 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
732 if (ct->id != id) {
733 ip_conntrack_put(ct);
734 return -ENOENT;
737 if (del_timer(&ct->timeout))
738 ct->timeout.function((unsigned long)ct);
740 ip_conntrack_put(ct);
741 DEBUGP("leaving\n");
743 return 0;
746 static int
747 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
748 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
750 struct ip_conntrack_tuple_hash *h;
751 struct ip_conntrack_tuple tuple;
752 struct ip_conntrack *ct;
753 struct sk_buff *skb2 = NULL;
754 int err = 0;
756 DEBUGP("entered %s\n", __FUNCTION__);
758 if (nlh->nlmsg_flags & NLM_F_DUMP) {
759 struct nfgenmsg *msg = NLMSG_DATA(nlh);
760 u32 rlen;
762 if (msg->nfgen_family != AF_INET)
763 return -EAFNOSUPPORT;
765 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
766 IPCTNL_MSG_CT_GET_CTRZERO) {
767 #ifdef CONFIG_IP_NF_CT_ACCT
768 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
769 ctnetlink_dump_table_w,
770 ctnetlink_done)) != 0)
771 return -EINVAL;
772 #else
773 return -ENOTSUPP;
774 #endif
775 } else {
776 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
777 ctnetlink_dump_table,
778 ctnetlink_done)) != 0)
779 return -EINVAL;
782 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
783 if (rlen > skb->len)
784 rlen = skb->len;
785 skb_pull(skb, rlen);
786 return 0;
789 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
790 return -EINVAL;
792 if (cda[CTA_TUPLE_ORIG-1])
793 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
794 else if (cda[CTA_TUPLE_REPLY-1])
795 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
796 else
797 return -EINVAL;
799 if (err < 0)
800 return err;
802 h = ip_conntrack_find_get(&tuple, NULL);
803 if (!h) {
804 DEBUGP("tuple not found in conntrack hash");
805 return -ENOENT;
807 DEBUGP("tuple found\n");
808 ct = tuplehash_to_ctrack(h);
810 err = -ENOMEM;
811 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
812 if (!skb2) {
813 ip_conntrack_put(ct);
814 return -ENOMEM;
816 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
818 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
819 IPCTNL_MSG_CT_NEW, 1, ct);
820 ip_conntrack_put(ct);
821 if (err <= 0)
822 goto free;
824 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
825 if (err < 0)
826 goto out;
828 DEBUGP("leaving\n");
829 return 0;
831 free:
832 kfree_skb(skb2);
833 out:
834 return err;
837 static inline int
838 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
840 unsigned long d;
841 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
842 d = ct->status ^ status;
844 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
845 /* unchangeable */
846 return -EINVAL;
848 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
849 /* SEEN_REPLY bit can only be set */
850 return -EINVAL;
853 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
854 /* ASSURED bit can only be set */
855 return -EINVAL;
857 if (cda[CTA_NAT-1]) {
858 #ifndef CONFIG_IP_NF_NAT_NEEDED
859 return -EINVAL;
860 #else
861 unsigned int hooknum;
862 struct ip_nat_range range;
864 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
865 return -EINVAL;
867 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
868 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
869 htons(range.min.all), htons(range.max.all));
871 /* This is tricky but it works. ip_nat_setup_info needs the
872 * hook number as parameter, so let's do the correct
873 * conversion and run away */
874 if (status & IPS_SRC_NAT_DONE)
875 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
876 else if (status & IPS_DST_NAT_DONE)
877 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
878 else
879 return -EINVAL; /* Missing NAT flags */
881 DEBUGP("NAT status: %lu\n",
882 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
884 if (ip_nat_initialized(ct, HOOK2MANIP(hooknum)))
885 return -EEXIST;
886 ip_nat_setup_info(ct, &range, hooknum);
888 DEBUGP("NAT status after setup_info: %lu\n",
889 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
890 #endif
893 /* Be careful here, modifying NAT bits can screw up things,
894 * so don't let users modify them directly if they don't pass
895 * ip_nat_range. */
896 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
897 return 0;
901 static inline int
902 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
904 struct ip_conntrack_helper *helper;
905 char *helpname;
906 int err;
908 DEBUGP("entered %s\n", __FUNCTION__);
910 /* don't change helper of sibling connections */
911 if (ct->master)
912 return -EINVAL;
914 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
915 if (err < 0)
916 return err;
918 helper = __ip_conntrack_helper_find_byname(helpname);
919 if (!helper) {
920 if (!strcmp(helpname, ""))
921 helper = NULL;
922 else
923 return -EINVAL;
926 if (ct->helper) {
927 if (!helper) {
928 /* we had a helper before ... */
929 ip_ct_remove_expectations(ct);
930 ct->helper = NULL;
931 } else {
932 /* need to zero data of old helper */
933 memset(&ct->help, 0, sizeof(ct->help));
937 ct->helper = helper;
939 return 0;
942 static inline int
943 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
945 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
947 if (!del_timer(&ct->timeout))
948 return -ETIME;
950 ct->timeout.expires = jiffies + timeout * HZ;
951 add_timer(&ct->timeout);
953 return 0;
956 static inline int
957 ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
959 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
960 struct ip_conntrack_protocol *proto;
961 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
962 int err = 0;
964 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
966 proto = ip_conntrack_proto_find_get(npt);
968 if (proto->from_nfattr)
969 err = proto->from_nfattr(tb, ct);
970 ip_conntrack_proto_put(proto);
972 return err;
975 static int
976 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
978 int err;
980 DEBUGP("entered %s\n", __FUNCTION__);
982 if (cda[CTA_HELP-1]) {
983 err = ctnetlink_change_helper(ct, cda);
984 if (err < 0)
985 return err;
988 if (cda[CTA_TIMEOUT-1]) {
989 err = ctnetlink_change_timeout(ct, cda);
990 if (err < 0)
991 return err;
994 if (cda[CTA_STATUS-1]) {
995 err = ctnetlink_change_status(ct, cda);
996 if (err < 0)
997 return err;
1000 if (cda[CTA_PROTOINFO-1]) {
1001 err = ctnetlink_change_protoinfo(ct, cda);
1002 if (err < 0)
1003 return err;
1006 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1007 if (cda[CTA_MARK-1])
1008 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1009 #endif
1011 DEBUGP("all done\n");
1012 return 0;
1015 static int
1016 ctnetlink_create_conntrack(struct nfattr *cda[],
1017 struct ip_conntrack_tuple *otuple,
1018 struct ip_conntrack_tuple *rtuple)
1020 struct ip_conntrack *ct;
1021 int err = -EINVAL;
1023 DEBUGP("entered %s\n", __FUNCTION__);
1025 ct = ip_conntrack_alloc(otuple, rtuple);
1026 if (ct == NULL || IS_ERR(ct))
1027 return -ENOMEM;
1029 if (!cda[CTA_TIMEOUT-1])
1030 goto err;
1031 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1033 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1034 ct->status |= IPS_CONFIRMED;
1036 err = ctnetlink_change_status(ct, cda);
1037 if (err < 0)
1038 goto err;
1040 if (cda[CTA_PROTOINFO-1]) {
1041 err = ctnetlink_change_protoinfo(ct, cda);
1042 if (err < 0)
1043 return err;
1046 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1047 if (cda[CTA_MARK-1])
1048 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1049 #endif
1051 ct->helper = ip_conntrack_helper_find_get(rtuple);
1053 add_timer(&ct->timeout);
1054 ip_conntrack_hash_insert(ct);
1056 if (ct->helper)
1057 ip_conntrack_helper_put(ct->helper);
1059 DEBUGP("conntrack with id %u inserted\n", ct->id);
1060 return 0;
1062 err:
1063 ip_conntrack_free(ct);
1064 return err;
1067 static int
1068 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1069 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1071 struct ip_conntrack_tuple otuple, rtuple;
1072 struct ip_conntrack_tuple_hash *h = NULL;
1073 int err = 0;
1075 DEBUGP("entered %s\n", __FUNCTION__);
1077 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1078 return -EINVAL;
1080 if (cda[CTA_TUPLE_ORIG-1]) {
1081 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1082 if (err < 0)
1083 return err;
1086 if (cda[CTA_TUPLE_REPLY-1]) {
1087 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1088 if (err < 0)
1089 return err;
1092 write_lock_bh(&ip_conntrack_lock);
1093 if (cda[CTA_TUPLE_ORIG-1])
1094 h = __ip_conntrack_find(&otuple, NULL);
1095 else if (cda[CTA_TUPLE_REPLY-1])
1096 h = __ip_conntrack_find(&rtuple, NULL);
1098 if (h == NULL) {
1099 write_unlock_bh(&ip_conntrack_lock);
1100 DEBUGP("no such conntrack, create new\n");
1101 err = -ENOENT;
1102 if (nlh->nlmsg_flags & NLM_F_CREATE)
1103 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1104 return err;
1106 /* implicit 'else' */
1108 /* we only allow nat config for new conntracks */
1109 if (cda[CTA_NAT-1]) {
1110 err = -EINVAL;
1111 goto out_unlock;
1114 /* We manipulate the conntrack inside the global conntrack table lock,
1115 * so there's no need to increase the refcount */
1116 DEBUGP("conntrack found\n");
1117 err = -EEXIST;
1118 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1119 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1121 out_unlock:
1122 write_unlock_bh(&ip_conntrack_lock);
1123 return err;
1126 /***********************************************************************
1127 * EXPECT
1128 ***********************************************************************/
1130 static inline int
1131 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1132 const struct ip_conntrack_tuple *tuple,
1133 enum ctattr_expect type)
1135 struct nfattr *nest_parms = NFA_NEST(skb, type);
1137 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1138 goto nfattr_failure;
1140 NFA_NEST_END(skb, nest_parms);
1142 return 0;
1144 nfattr_failure:
1145 return -1;
1148 static inline int
1149 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1150 const struct ip_conntrack_tuple *tuple,
1151 const struct ip_conntrack_tuple *mask)
1153 int ret;
1154 struct ip_conntrack_protocol *proto;
1155 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1157 ret = ctnetlink_dump_tuples_ip(skb, mask);
1158 if (unlikely(ret < 0))
1159 goto nfattr_failure;
1161 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
1162 ret = ctnetlink_dump_tuples_proto(skb, mask, proto);
1163 ip_conntrack_proto_put(proto);
1164 if (unlikely(ret < 0))
1165 goto nfattr_failure;
1167 NFA_NEST_END(skb, nest_parms);
1169 return 0;
1171 nfattr_failure:
1172 return -1;
1175 static inline int
1176 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1177 const struct ip_conntrack_expect *exp)
1179 struct ip_conntrack *master = exp->master;
1180 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1181 u_int32_t id = htonl(exp->id);
1183 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1184 goto nfattr_failure;
1185 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1186 goto nfattr_failure;
1187 if (ctnetlink_exp_dump_tuple(skb,
1188 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1189 CTA_EXPECT_MASTER) < 0)
1190 goto nfattr_failure;
1192 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1193 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1195 return 0;
1197 nfattr_failure:
1198 return -1;
1201 static int
1202 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1203 int event,
1204 int nowait,
1205 const struct ip_conntrack_expect *exp)
1207 struct nlmsghdr *nlh;
1208 struct nfgenmsg *nfmsg;
1209 unsigned char *b;
1211 b = skb->tail;
1213 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1214 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1215 nfmsg = NLMSG_DATA(nlh);
1217 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1218 nfmsg->nfgen_family = AF_INET;
1219 nfmsg->version = NFNETLINK_V0;
1220 nfmsg->res_id = 0;
1222 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1223 goto nfattr_failure;
1225 nlh->nlmsg_len = skb->tail - b;
1226 return skb->len;
1228 nlmsg_failure:
1229 nfattr_failure:
1230 skb_trim(skb, b - skb->data);
1231 return -1;
1234 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1235 static int ctnetlink_expect_event(struct notifier_block *this,
1236 unsigned long events, void *ptr)
1238 struct nlmsghdr *nlh;
1239 struct nfgenmsg *nfmsg;
1240 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1241 struct sk_buff *skb;
1242 unsigned int type;
1243 unsigned char *b;
1244 int flags = 0;
1246 if (events & IPEXP_NEW) {
1247 type = IPCTNL_MSG_EXP_NEW;
1248 flags = NLM_F_CREATE|NLM_F_EXCL;
1249 } else
1250 return NOTIFY_DONE;
1252 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1253 if (!skb)
1254 return NOTIFY_DONE;
1256 b = skb->tail;
1258 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1259 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1260 nfmsg = NLMSG_DATA(nlh);
1262 nlh->nlmsg_flags = flags;
1263 nfmsg->nfgen_family = AF_INET;
1264 nfmsg->version = NFNETLINK_V0;
1265 nfmsg->res_id = 0;
1267 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1268 goto nfattr_failure;
1270 nlh->nlmsg_len = skb->tail - b;
1271 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1272 return NOTIFY_DONE;
1274 nlmsg_failure:
1275 nfattr_failure:
1276 kfree_skb(skb);
1277 return NOTIFY_DONE;
1279 #endif
1281 static int
1282 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1284 struct ip_conntrack_expect *exp = NULL;
1285 struct list_head *i;
1286 u_int32_t *id = (u_int32_t *) &cb->args[0];
1288 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1290 read_lock_bh(&ip_conntrack_lock);
1291 list_for_each_prev(i, &ip_conntrack_expect_list) {
1292 exp = (struct ip_conntrack_expect *) i;
1293 if (exp->id <= *id)
1294 continue;
1295 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1296 cb->nlh->nlmsg_seq,
1297 IPCTNL_MSG_EXP_NEW,
1298 1, exp) < 0)
1299 goto out;
1300 *id = exp->id;
1302 out:
1303 read_unlock_bh(&ip_conntrack_lock);
1305 DEBUGP("leaving, last id=%llu\n", *id);
1307 return skb->len;
1310 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1311 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1312 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1315 static int
1316 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1317 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1319 struct ip_conntrack_tuple tuple;
1320 struct ip_conntrack_expect *exp;
1321 struct sk_buff *skb2;
1322 int err = 0;
1324 DEBUGP("entered %s\n", __FUNCTION__);
1326 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1327 return -EINVAL;
1329 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1330 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1331 u32 rlen;
1333 if (msg->nfgen_family != AF_INET)
1334 return -EAFNOSUPPORT;
1336 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1337 ctnetlink_exp_dump_table,
1338 ctnetlink_done)) != 0)
1339 return -EINVAL;
1340 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1341 if (rlen > skb->len)
1342 rlen = skb->len;
1343 skb_pull(skb, rlen);
1344 return 0;
1347 if (cda[CTA_EXPECT_MASTER-1])
1348 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
1349 else
1350 return -EINVAL;
1352 if (err < 0)
1353 return err;
1355 exp = ip_conntrack_expect_find(&tuple);
1356 if (!exp)
1357 return -ENOENT;
1359 if (cda[CTA_EXPECT_ID-1]) {
1360 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1361 if (exp->id != ntohl(id)) {
1362 ip_conntrack_expect_put(exp);
1363 return -ENOENT;
1367 err = -ENOMEM;
1368 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1369 if (!skb2)
1370 goto out;
1371 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1373 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1374 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1375 1, exp);
1376 if (err <= 0)
1377 goto free;
1379 ip_conntrack_expect_put(exp);
1381 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1383 free:
1384 kfree_skb(skb2);
1385 out:
1386 ip_conntrack_expect_put(exp);
1387 return err;
1390 static int
1391 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1392 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1394 struct ip_conntrack_expect *exp, *tmp;
1395 struct ip_conntrack_tuple tuple;
1396 struct ip_conntrack_helper *h;
1397 int err;
1399 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1400 return -EINVAL;
1402 if (cda[CTA_EXPECT_TUPLE-1]) {
1403 /* delete a single expect by tuple */
1404 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1405 if (err < 0)
1406 return err;
1408 /* bump usage count to 2 */
1409 exp = ip_conntrack_expect_find(&tuple);
1410 if (!exp)
1411 return -ENOENT;
1413 if (cda[CTA_EXPECT_ID-1]) {
1414 u_int32_t id =
1415 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1416 if (exp->id != ntohl(id)) {
1417 ip_conntrack_expect_put(exp);
1418 return -ENOENT;
1422 /* after list removal, usage count == 1 */
1423 ip_conntrack_unexpect_related(exp);
1424 /* have to put what we 'get' above.
1425 * after this line usage count == 0 */
1426 ip_conntrack_expect_put(exp);
1427 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1428 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1430 /* delete all expectations for this helper */
1431 write_lock_bh(&ip_conntrack_lock);
1432 h = __ip_conntrack_helper_find_byname(name);
1433 if (!h) {
1434 write_unlock_bh(&ip_conntrack_lock);
1435 return -EINVAL;
1437 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1438 list) {
1439 if (exp->master->helper == h
1440 && del_timer(&exp->timeout)) {
1441 ip_ct_unlink_expect(exp);
1442 ip_conntrack_expect_put(exp);
1445 write_unlock_bh(&ip_conntrack_lock);
1446 } else {
1447 /* This basically means we have to flush everything*/
1448 write_lock_bh(&ip_conntrack_lock);
1449 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1450 list) {
1451 if (del_timer(&exp->timeout)) {
1452 ip_ct_unlink_expect(exp);
1453 ip_conntrack_expect_put(exp);
1456 write_unlock_bh(&ip_conntrack_lock);
1459 return 0;
1461 static int
1462 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1464 return -EOPNOTSUPP;
1467 static int
1468 ctnetlink_create_expect(struct nfattr *cda[])
1470 struct ip_conntrack_tuple tuple, mask, master_tuple;
1471 struct ip_conntrack_tuple_hash *h = NULL;
1472 struct ip_conntrack_expect *exp;
1473 struct ip_conntrack *ct;
1474 int err = 0;
1476 DEBUGP("entered %s\n", __FUNCTION__);
1478 /* caller guarantees that those three CTA_EXPECT_* exist */
1479 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1480 if (err < 0)
1481 return err;
1482 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
1483 if (err < 0)
1484 return err;
1485 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
1486 if (err < 0)
1487 return err;
1489 /* Look for master conntrack of this expectation */
1490 h = ip_conntrack_find_get(&master_tuple, NULL);
1491 if (!h)
1492 return -ENOENT;
1493 ct = tuplehash_to_ctrack(h);
1495 if (!ct->helper) {
1496 /* such conntrack hasn't got any helper, abort */
1497 err = -EINVAL;
1498 goto out;
1501 exp = ip_conntrack_expect_alloc(ct);
1502 if (!exp) {
1503 err = -ENOMEM;
1504 goto out;
1507 exp->expectfn = NULL;
1508 exp->flags = 0;
1509 exp->master = ct;
1510 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1511 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1513 err = ip_conntrack_expect_related(exp);
1514 ip_conntrack_expect_put(exp);
1516 out:
1517 ip_conntrack_put(tuplehash_to_ctrack(h));
1518 return err;
1521 static int
1522 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1523 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1525 struct ip_conntrack_tuple tuple;
1526 struct ip_conntrack_expect *exp;
1527 int err = 0;
1529 DEBUGP("entered %s\n", __FUNCTION__);
1531 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1532 return -EINVAL;
1534 if (!cda[CTA_EXPECT_TUPLE-1]
1535 || !cda[CTA_EXPECT_MASK-1]
1536 || !cda[CTA_EXPECT_MASTER-1])
1537 return -EINVAL;
1539 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1540 if (err < 0)
1541 return err;
1543 write_lock_bh(&ip_conntrack_lock);
1544 exp = __ip_conntrack_expect_find(&tuple);
1546 if (!exp) {
1547 write_unlock_bh(&ip_conntrack_lock);
1548 err = -ENOENT;
1549 if (nlh->nlmsg_flags & NLM_F_CREATE)
1550 err = ctnetlink_create_expect(cda);
1551 return err;
1554 err = -EEXIST;
1555 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1556 err = ctnetlink_change_expect(exp, cda);
1557 write_unlock_bh(&ip_conntrack_lock);
1559 DEBUGP("leaving\n");
1561 return err;
1564 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1565 static struct notifier_block ctnl_notifier = {
1566 .notifier_call = ctnetlink_conntrack_event,
1569 static struct notifier_block ctnl_notifier_exp = {
1570 .notifier_call = ctnetlink_expect_event,
1572 #endif
1574 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1575 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1576 .attr_count = CTA_MAX, },
1577 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1578 .attr_count = CTA_MAX, },
1579 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1580 .attr_count = CTA_MAX, },
1581 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1582 .attr_count = CTA_MAX, },
1585 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1586 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1587 .attr_count = CTA_EXPECT_MAX, },
1588 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1589 .attr_count = CTA_EXPECT_MAX, },
1590 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1591 .attr_count = CTA_EXPECT_MAX, },
1594 static struct nfnetlink_subsystem ctnl_subsys = {
1595 .name = "conntrack",
1596 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1597 .cb_count = IPCTNL_MSG_MAX,
1598 .cb = ctnl_cb,
1601 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1602 .name = "conntrack_expect",
1603 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1604 .cb_count = IPCTNL_MSG_EXP_MAX,
1605 .cb = ctnl_exp_cb,
1608 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1609 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1611 static int __init ctnetlink_init(void)
1613 int ret;
1615 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1616 ret = nfnetlink_subsys_register(&ctnl_subsys);
1617 if (ret < 0) {
1618 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1619 goto err_out;
1622 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1623 if (ret < 0) {
1624 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1625 goto err_unreg_subsys;
1628 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1629 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1630 if (ret < 0) {
1631 printk("ctnetlink_init: cannot register notifier.\n");
1632 goto err_unreg_exp_subsys;
1635 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1636 if (ret < 0) {
1637 printk("ctnetlink_init: cannot expect register notifier.\n");
1638 goto err_unreg_notifier;
1640 #endif
1642 return 0;
1644 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1645 err_unreg_notifier:
1646 ip_conntrack_unregister_notifier(&ctnl_notifier);
1647 err_unreg_exp_subsys:
1648 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1649 #endif
1650 err_unreg_subsys:
1651 nfnetlink_subsys_unregister(&ctnl_subsys);
1652 err_out:
1653 return ret;
1656 static void __exit ctnetlink_exit(void)
1658 printk("ctnetlink: unregistering from nfnetlink.\n");
1660 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1661 ip_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
1662 ip_conntrack_unregister_notifier(&ctnl_notifier);
1663 #endif
1665 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1666 nfnetlink_subsys_unregister(&ctnl_subsys);
1667 return;
1670 module_init(ctnetlink_init);
1671 module_exit(ctnetlink_exit);