blob: f43ec18c91662eee1de5813dec3fa2477e71ebf6 [file] [log] [blame]
Harald Welte080774a2005-08-09 19:32:58 -07001/* Connection tracking via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
3 *
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>
8 *
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.
11 *
12 * Initial connection tracking via netlink development funded and
13 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
14 *
15 * Further development of this code funded by Astaro AG (http://www.astaro.com)
16 *
17 * This software may be used and distributed according to the terms
18 * of the GNU General Public License, incorporated herein by reference.
19 */
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/kernel.h>
24#include <linux/types.h>
25#include <linux/timer.h>
26#include <linux/skbuff.h>
27#include <linux/errno.h>
28#include <linux/netlink.h>
29#include <linux/spinlock.h>
30#include <linux/notifier.h>
31#include <linux/rtnetlink.h>
32
33#include <linux/netfilter.h>
34#include <linux/netfilter_ipv4.h>
35#include <linux/netfilter_ipv4/ip_tables.h>
36#include <linux/netfilter_ipv4/ip_conntrack.h>
37#include <linux/netfilter_ipv4/ip_conntrack_core.h>
38#include <linux/netfilter_ipv4/ip_conntrack_helper.h>
39#include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
40#include <linux/netfilter_ipv4/ip_nat_protocol.h>
41
42#include <linux/netfilter/nfnetlink.h>
43#include <linux/netfilter/nfnetlink_conntrack.h>
44
45MODULE_LICENSE("GPL");
46
47static char __initdata version[] = "0.90";
48
49#if 0
50#define DEBUGP printk
51#else
52#define DEBUGP(format, args...)
53#endif
54
55
56static inline int
57ctnetlink_dump_tuples_proto(struct sk_buff *skb,
58 const struct ip_conntrack_tuple *tuple)
59{
60 struct ip_conntrack_protocol *proto;
61
62 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
63
64 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
65 if (proto && proto->tuple_to_nfattr)
66 return proto->tuple_to_nfattr(skb, tuple);
67
68 return 0;
69
70nfattr_failure:
71 return -1;
72}
73
74static inline int
75ctnetlink_dump_tuples(struct sk_buff *skb,
76 const struct ip_conntrack_tuple *tuple)
77{
78 struct nfattr *nest_parms;
79
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);
84
85 nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
86 ctnetlink_dump_tuples_proto(skb, tuple);
87 NFA_NEST_END(skb, nest_parms);
88
89 return 0;
90
91nfattr_failure:
92 return -1;
93}
94
95static inline int
96ctnetlink_dump_status(struct sk_buff *skb, const struct ip_conntrack *ct)
97{
98 u_int32_t status = htonl((u_int32_t) ct->status);
99 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
100 return 0;
101
102nfattr_failure:
103 return -1;
104}
105
106static inline int
107ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct)
108{
109 long timeout_l = ct->timeout.expires - jiffies;
110 u_int32_t timeout;
111
112 if (timeout_l < 0)
113 timeout = 0;
114 else
115 timeout = htonl(timeout_l / HZ);
116
117 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
118 return 0;
119
120nfattr_failure:
121 return -1;
122}
123
124static inline int
125ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
126{
127 struct ip_conntrack_protocol *proto = ip_conntrack_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
128
129 struct nfattr *nest_proto;
130 int ret;
131
132 if (!proto || !proto->to_nfattr)
133 return 0;
134
135 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
136
137 ret = proto->to_nfattr(skb, nest_proto, ct);
138
139 ip_conntrack_proto_put(proto);
140
141 NFA_NEST_END(skb, nest_proto);
142
143 return ret;
144
145nfattr_failure:
146 return -1;
147}
148
149static inline int
150ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
151{
152 struct nfattr *nest_helper;
153
154 if (!ct->helper)
155 return 0;
156
157 nest_helper = NFA_NEST(skb, CTA_HELP);
158 NFA_PUT(skb, CTA_HELP_NAME, CTA_HELP_MAXNAMESIZE, &ct->helper->name);
159
160 if (ct->helper->to_nfattr)
161 ct->helper->to_nfattr(skb, ct);
162
163 NFA_NEST_END(skb, nest_helper);
164
165 return 0;
166
167nfattr_failure:
168 return -1;
169}
170
171#ifdef CONFIG_IP_NF_CT_ACCT
172static inline int
173ctnetlink_dump_counters(struct sk_buff *skb, const struct ip_conntrack *ct,
174 enum ip_conntrack_dir dir)
175{
176 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
177 struct nfattr *nest_count = NFA_NEST(skb, type);
178 u_int64_t tmp;
179
180 tmp = cpu_to_be64(ct->counters[dir].packets);
181 NFA_PUT(skb, CTA_COUNTERS_PACKETS, sizeof(u_int64_t), &tmp);
182
183 tmp = cpu_to_be64(ct->counters[dir].bytes);
184 NFA_PUT(skb, CTA_COUNTERS_BYTES, sizeof(u_int64_t), &tmp);
185
186 NFA_NEST_END(skb, nest_count);
187
188 return 0;
189
190nfattr_failure:
191 return -1;
192}
193#else
194#define ctnetlink_dump_counters(a, b, c) (0)
195#endif
196
197#ifdef CONFIG_IP_NF_CONNTRACK_MARK
198static inline int
199ctnetlink_dump_mark(struct sk_buff *skb, const struct ip_conntrack *ct)
200{
201 u_int32_t mark = htonl(ct->mark);
202
203 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
204 return 0;
205
206nfattr_failure:
207 return -1;
208}
209#else
210#define ctnetlink_dump_mark(a, b) (0)
211#endif
212
213static inline int
214ctnetlink_dump_id(struct sk_buff *skb, const struct ip_conntrack *ct)
215{
216 u_int32_t id = htonl(ct->id);
217 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
218 return 0;
219
220nfattr_failure:
221 return -1;
222}
223
224static inline int
225ctnetlink_dump_use(struct sk_buff *skb, const struct ip_conntrack *ct)
226{
227 unsigned int use = htonl(atomic_read(&ct->ct_general.use));
228
229 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
230 return 0;
231
232nfattr_failure:
233 return -1;
234}
235
236#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
237
238static int
239ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
240 int event, int nowait,
241 const struct ip_conntrack *ct)
242{
243 struct nlmsghdr *nlh;
244 struct nfgenmsg *nfmsg;
245 struct nfattr *nest_parms;
246 unsigned char *b;
247
248 b = skb->tail;
249
250 event |= NFNL_SUBSYS_CTNETLINK << 8;
251 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
252 nfmsg = NLMSG_DATA(nlh);
253
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;
258
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);
263
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);
268
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;
279
280 nlh->nlmsg_len = skb->tail - b;
281 return skb->len;
282
283nlmsg_failure:
284nfattr_failure:
285 skb_trim(skb, b - skb->data);
286 return -1;
287}
288
289#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
290static int ctnetlink_conntrack_event(struct notifier_block *this,
291 unsigned long events, void *ptr)
292{
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, groups;
301
302 /* ignore our fake conntrack entry */
303 if (ct == &ip_conntrack_untracked)
304 return NOTIFY_DONE;
305
306 if (events & IPCT_DESTROY) {
307 type = IPCTNL_MSG_CT_DELETE;
308 groups = NF_NETLINK_CONNTRACK_DESTROY;
309 goto alloc_skb;
310 }
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 groups = NF_NETLINK_CONNTRACK_NEW;
317 goto alloc_skb;
318 }
319 if (events & (IPCT_STATUS |
320 IPCT_PROTOINFO |
321 IPCT_HELPER |
322 IPCT_HELPINFO |
323 IPCT_NATINFO)) {
324 type = IPCTNL_MSG_CT_NEW;
325 groups = NF_NETLINK_CONNTRACK_UPDATE;
326 goto alloc_skb;
327 }
328
329 return NOTIFY_DONE;
330
331alloc_skb:
332 /* FIXME: Check if there are any listeners before, don't hurt performance */
333
334 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
335 if (!skb)
336 return NOTIFY_DONE;
337
338 b = skb->tail;
339
340 type |= NFNL_SUBSYS_CTNETLINK << 8;
341 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
342 nfmsg = NLMSG_DATA(nlh);
343
344 nlh->nlmsg_flags = flags;
345 nfmsg->nfgen_family = AF_INET;
346 nfmsg->version = NFNETLINK_V0;
347 nfmsg->res_id = 0;
348
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);
353
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);
358
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;
372
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;
376
377 nlh->nlmsg_len = skb->tail - b;
378 nfnetlink_send(skb, 0, groups, 0);
379 return NOTIFY_DONE;
380
381nlmsg_failure:
382nfattr_failure:
383 kfree_skb(skb);
384 return NOTIFY_DONE;
385}
386#endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
387
388static int ctnetlink_done(struct netlink_callback *cb)
389{
390 DEBUGP("entered %s\n", __FUNCTION__);
391 return 0;
392}
393
394static int
395ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
396{
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];
401
402 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
403 cb->args[0], *id);
404
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(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;
420 }
421 }
422out:
423 read_unlock_bh(&ip_conntrack_lock);
424
425 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
426
427 return skb->len;
428}
429
430#ifdef CONFIG_IP_NF_CT_ACCT
431static int
432ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
433{
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];
438
439 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
440 cb->args[0], *id);
441
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(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;
457
458 memset(&ct->counters, 0, sizeof(ct->counters));
459 }
460 }
461out:
462 write_unlock_bh(&ip_conntrack_lock);
463
464 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
465
466 return skb->len;
467}
468#endif
469
470static 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),
473};
474
475static inline int
476ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple)
477{
478 struct nfattr *tb[CTA_IP_MAX];
479
480 DEBUGP("entered %s\n", __FUNCTION__);
481
482 memset(tb, 0, CTA_IP_MAX * sizeof(tb));
483
484 if (nfattr_parse_nested(tb, CTA_IP_MAX, attr) < 0)
485 goto nfattr_failure;
486
487 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
488 return -EINVAL;
489
490 if (!tb[CTA_IP_V4_SRC-1])
491 return -EINVAL;
492 tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
493
494 if (!tb[CTA_IP_V4_DST-1])
495 return -EINVAL;
496 tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
497
498 DEBUGP("leaving\n");
499
500 return 0;
501
502nfattr_failure:
503 return -1;
504}
505
506static const int cta_min_proto[CTA_PROTO_MAX] = {
507 [CTA_PROTO_NUM-1] = sizeof(u_int16_t),
508 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
509 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
510 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
511 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
512 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
513};
514
515static inline int
516ctnetlink_parse_tuple_proto(struct nfattr *attr,
517 struct ip_conntrack_tuple *tuple)
518{
519 struct nfattr *tb[CTA_PROTO_MAX];
520 struct ip_conntrack_protocol *proto;
521 int ret = 0;
522
523 DEBUGP("entered %s\n", __FUNCTION__);
524
525 memset(tb, 0, CTA_PROTO_MAX * sizeof(tb));
526
527 if (nfattr_parse_nested(tb, CTA_PROTO_MAX, attr) < 0)
528 goto nfattr_failure;
529
530 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
531 return -EINVAL;
532
533 if (!tb[CTA_PROTO_NUM-1])
534 return -EINVAL;
535 tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
536
537 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
538
539 if (likely(proto && proto->nfattr_to_tuple)) {
540 ret = proto->nfattr_to_tuple(tb, tuple);
541 ip_conntrack_proto_put(proto);
542 }
543
544 return ret;
545
546nfattr_failure:
547 return -1;
548}
549
550static inline int
551ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
552 enum ctattr_tuple type)
553{
554 struct nfattr *tb[CTA_TUPLE_MAX];
555 int err;
556
557 DEBUGP("entered %s\n", __FUNCTION__);
558
559 memset(tb, 0, CTA_TUPLE_MAX * sizeof(tb));
560 memset(tuple, 0, sizeof(*tuple));
561
562 if (nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]) < 0)
563 goto nfattr_failure;
564
565 if (!tb[CTA_TUPLE_IP-1])
566 return -EINVAL;
567
568 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
569 if (err < 0)
570 return err;
571
572 if (!tb[CTA_TUPLE_PROTO-1])
573 return -EINVAL;
574
575 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
576 if (err < 0)
577 return err;
578
579 /* orig and expect tuples get DIR_ORIGINAL */
580 if (type == CTA_TUPLE_REPLY)
581 tuple->dst.dir = IP_CT_DIR_REPLY;
582 else
583 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
584
585 DUMP_TUPLE(tuple);
586
587 DEBUGP("leaving\n");
588
589 return 0;
590
591nfattr_failure:
592 return -1;
593}
594
595#ifdef CONFIG_IP_NF_NAT_NEEDED
596static const int cta_min_protonat[CTA_PROTONAT_MAX] = {
597 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
598 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
599};
600
601static int ctnetlink_parse_nat_proto(struct nfattr *attr,
602 const struct ip_conntrack *ct,
603 struct ip_nat_range *range)
604{
605 struct nfattr *tb[CTA_PROTONAT_MAX];
606 struct ip_nat_protocol *npt;
607
608 DEBUGP("entered %s\n", __FUNCTION__);
609
610 memset(tb, 0, CTA_PROTONAT_MAX * sizeof(tb));
611
612 if (nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr) < 0)
613 goto nfattr_failure;
614
615 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
616 goto nfattr_failure;
617
618 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
619 if (!npt)
620 return 0;
621
622 if (!npt->nfattr_to_range) {
623 ip_nat_proto_put(npt);
624 return 0;
625 }
626
627 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
628 if (npt->nfattr_to_range(tb, range) > 0)
629 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
630
631 ip_nat_proto_put(npt);
632
633 DEBUGP("leaving\n");
634 return 0;
635
636nfattr_failure:
637 return -1;
638}
639
640static inline int
641ctnetlink_parse_nat(struct nfattr *cda[],
642 const struct ip_conntrack *ct, struct ip_nat_range *range)
643{
644 struct nfattr *tb[CTA_NAT_MAX];
645 int err;
646
647 DEBUGP("entered %s\n", __FUNCTION__);
648
649 memset(tb, 0, CTA_NAT_MAX * sizeof(tb));
650 memset(range, 0, sizeof(*range));
651
652 if (nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]) < 0)
653 goto nfattr_failure;
654
655 if (tb[CTA_NAT_MINIP-1])
656 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
657
658 if (!tb[CTA_NAT_MAXIP-1])
659 range->max_ip = range->min_ip;
660 else
661 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
662
663 if (range->min_ip)
664 range->flags |= IP_NAT_RANGE_MAP_IPS;
665
666 if (!tb[CTA_NAT_PROTO-1])
667 return 0;
668
669 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
670 if (err < 0)
671 return err;
672
673 DEBUGP("leaving\n");
674 return 0;
675
676nfattr_failure:
677 return -1;
678}
679#endif
680
681static inline int
682ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
683{
684 struct nfattr *tb[CTA_HELP_MAX];
685
686 DEBUGP("entered %s\n", __FUNCTION__);
687 memset(tb, 0, CTA_HELP_MAX * sizeof(tb));
688
689 if (nfattr_parse_nested(tb, CTA_HELP_MAX, attr) < 0)
690 goto nfattr_failure;
691
692 if (!tb[CTA_HELP_NAME-1])
693 return -EINVAL;
694
695 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
696
697 return 0;
698
699nfattr_failure:
700 return -1;
701}
702
703static int
704ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
705 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
706{
707 struct ip_conntrack_tuple_hash *h;
708 struct ip_conntrack_tuple tuple;
709 struct ip_conntrack *ct;
710 int err = 0;
711
712 DEBUGP("entered %s\n", __FUNCTION__);
713
714 if (cda[CTA_TUPLE_ORIG-1])
715 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
716 else if (cda[CTA_TUPLE_REPLY-1])
717 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
718 else {
719 /* Flush the whole table */
720 ip_conntrack_flush();
721 return 0;
722 }
723
724 if (err < 0)
725 return err;
726
727 h = ip_conntrack_find_get(&tuple, NULL);
728 if (!h) {
729 DEBUGP("tuple not found in conntrack hash\n");
730 return -ENOENT;
731 }
732
733 ct = tuplehash_to_ctrack(h);
734
735 if (cda[CTA_ID-1]) {
736 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
737 if (ct->id != id) {
738 ip_conntrack_put(ct);
739 return -ENOENT;
740 }
741 }
742 if (del_timer(&ct->timeout)) {
743 ip_conntrack_put(ct);
744 ct->timeout.function((unsigned long)ct);
745 return 0;
746 }
747 ip_conntrack_put(ct);
748 DEBUGP("leaving\n");
749
750 return 0;
751}
752
753static int
754ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
755 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
756{
757 struct ip_conntrack_tuple_hash *h;
758 struct ip_conntrack_tuple tuple;
759 struct ip_conntrack *ct;
760 struct sk_buff *skb2 = NULL;
761 int err = 0;
762
763 DEBUGP("entered %s\n", __FUNCTION__);
764
765 if (nlh->nlmsg_flags & NLM_F_DUMP) {
766 struct nfgenmsg *msg = NLMSG_DATA(nlh);
767 u32 rlen;
768
769 if (msg->nfgen_family != AF_INET)
770 return -EAFNOSUPPORT;
771
772 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
773 IPCTNL_MSG_CT_GET_CTRZERO) {
774#ifdef CONFIG_IP_NF_CT_ACCT
775 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
776 ctnetlink_dump_table_w,
777 ctnetlink_done)) != 0)
778 return -EINVAL;
779#else
780 return -ENOTSUPP;
781#endif
782 } else {
783 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
784 ctnetlink_dump_table,
785 ctnetlink_done)) != 0)
786 return -EINVAL;
787 }
788
789 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
790 if (rlen > skb->len)
791 rlen = skb->len;
792 skb_pull(skb, rlen);
793 return 0;
794 }
795
796 if (cda[CTA_TUPLE_ORIG-1])
797 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
798 else if (cda[CTA_TUPLE_REPLY-1])
799 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
800 else
801 return -EINVAL;
802
803 if (err < 0)
804 return err;
805
806 h = ip_conntrack_find_get(&tuple, NULL);
807 if (!h) {
808 DEBUGP("tuple not found in conntrack hash");
809 return -ENOENT;
810 }
811 DEBUGP("tuple found\n");
812 ct = tuplehash_to_ctrack(h);
813
814 err = -ENOMEM;
815 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
816 if (!skb2) {
817 ip_conntrack_put(ct);
818 return -ENOMEM;
819 }
820 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
821
822 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
823 IPCTNL_MSG_CT_NEW, 1, ct);
824 ip_conntrack_put(ct);
825 if (err <= 0)
826 goto out;
827
828 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
829 if (err < 0)
830 goto out;
831
832 DEBUGP("leaving\n");
833 return 0;
834
835out:
836 if (skb2)
837 kfree_skb(skb2);
838 return -1;
839}
840
841static inline int
842ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
843{
844 unsigned long d, status = *(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]);
845 d = ct->status ^ status;
846
847 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
848 /* unchangeable */
849 return -EINVAL;
850
851 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
852 /* SEEN_REPLY bit can only be set */
853 return -EINVAL;
854
855
856 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
857 /* ASSURED bit can only be set */
858 return -EINVAL;
859
860 if (cda[CTA_NAT-1]) {
861#ifndef CONFIG_IP_NF_NAT_NEEDED
862 return -EINVAL;
863#else
864 unsigned int hooknum;
865 struct ip_nat_range range;
866
867 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
868 return -EINVAL;
869
870 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
871 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
872 htons(range.min.all), htons(range.max.all));
873
874 /* This is tricky but it works. ip_nat_setup_info needs the
875 * hook number as parameter, so let's do the correct
876 * conversion and run away */
877 if (status & IPS_SRC_NAT_DONE)
878 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
879 else if (status & IPS_DST_NAT_DONE)
880 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
881 else
882 return -EINVAL; /* Missing NAT flags */
883
884 DEBUGP("NAT status: %lu\n",
885 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
886
887 if (ip_nat_initialized(ct, hooknum))
888 return -EEXIST;
889 ip_nat_setup_info(ct, &range, hooknum);
890
891 DEBUGP("NAT status after setup_info: %lu\n",
892 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
893#endif
894 }
895
896 /* Be careful here, modifying NAT bits can screw up things,
897 * so don't let users modify them directly if they don't pass
898 * ip_nat_range. */
899 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
900 return 0;
901}
902
903
904static inline int
905ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
906{
907 struct ip_conntrack_helper *helper;
908 char *helpname;
909 int err;
910
911 DEBUGP("entered %s\n", __FUNCTION__);
912
913 /* don't change helper of sibling connections */
914 if (ct->master)
915 return -EINVAL;
916
917 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
918 if (err < 0)
919 return err;
920
921 helper = __ip_conntrack_helper_find_byname(helpname);
922 if (!helper) {
923 if (!strcmp(helpname, ""))
924 helper = NULL;
925 else
926 return -EINVAL;
927 }
928
929 if (ct->helper) {
930 if (!helper) {
931 /* we had a helper before ... */
932 ip_ct_remove_expectations(ct);
933 ct->helper = NULL;
934 } else {
935 /* need to zero data of old helper */
936 memset(&ct->help, 0, sizeof(ct->help));
937 }
938 }
939
940 ct->helper = helper;
941
942 return 0;
943}
944
945static inline int
946ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
947{
948 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
949
950 if (!del_timer(&ct->timeout))
951 return -ETIME;
952
953 ct->timeout.expires = jiffies + timeout * HZ;
954 add_timer(&ct->timeout);
955
956 return 0;
957}
958
959static int
960ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
961{
962 int err;
963
964 DEBUGP("entered %s\n", __FUNCTION__);
965
966 if (cda[CTA_HELP-1]) {
967 err = ctnetlink_change_helper(ct, cda);
968 if (err < 0)
969 return err;
970 }
971
972 if (cda[CTA_TIMEOUT-1]) {
973 err = ctnetlink_change_timeout(ct, cda);
974 if (err < 0)
975 return err;
976 }
977
978 if (cda[CTA_STATUS-1]) {
979 err = ctnetlink_change_status(ct, cda);
980 if (err < 0)
981 return err;
982 }
983
984 DEBUGP("all done\n");
985 return 0;
986}
987
988static int
989ctnetlink_create_conntrack(struct nfattr *cda[],
990 struct ip_conntrack_tuple *otuple,
991 struct ip_conntrack_tuple *rtuple)
992{
993 struct ip_conntrack *ct;
994 int err = -EINVAL;
995
996 DEBUGP("entered %s\n", __FUNCTION__);
997
998 ct = ip_conntrack_alloc(otuple, rtuple);
999 if (ct == NULL || IS_ERR(ct))
1000 return -ENOMEM;
1001
1002 if (!cda[CTA_TIMEOUT-1])
1003 goto err;
1004 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1005
1006 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1007 ct->status |= IPS_CONFIRMED;
1008
1009 err = ctnetlink_change_status(ct, cda);
1010 if (err < 0)
1011 goto err;
1012
1013 ct->helper = ip_conntrack_helper_find_get(rtuple);
1014
1015 add_timer(&ct->timeout);
1016 ip_conntrack_hash_insert(ct);
1017
1018 if (ct->helper)
1019 ip_conntrack_helper_put(ct->helper);
1020
1021 DEBUGP("conntrack with id %u inserted\n", ct->id);
1022 return 0;
1023
1024err:
1025 ip_conntrack_free(ct);
1026 return err;
1027}
1028
1029static int
1030ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1031 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1032{
1033 struct ip_conntrack_tuple otuple, rtuple;
1034 struct ip_conntrack_tuple_hash *h = NULL;
1035 int err = 0;
1036
1037 DEBUGP("entered %s\n", __FUNCTION__);
1038
1039 if (cda[CTA_TUPLE_ORIG-1]) {
1040 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1041 if (err < 0)
1042 return err;
1043 }
1044
1045 if (cda[CTA_TUPLE_REPLY-1]) {
1046 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1047 if (err < 0)
1048 return err;
1049 }
1050
1051 write_lock_bh(&ip_conntrack_lock);
1052 if (cda[CTA_TUPLE_ORIG-1])
1053 h = __ip_conntrack_find(&otuple, NULL);
1054 else if (cda[CTA_TUPLE_REPLY-1])
1055 h = __ip_conntrack_find(&rtuple, NULL);
1056
1057 if (h == NULL) {
1058 write_unlock_bh(&ip_conntrack_lock);
1059 DEBUGP("no such conntrack, create new\n");
1060 err = -ENOENT;
1061 if (nlh->nlmsg_flags & NLM_F_CREATE)
1062 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1063 goto out_unlock;
1064 } else {
1065 /* we only allow nat config for new conntracks */
1066 if (cda[CTA_NAT-1]) {
1067 err = -EINVAL;
1068 goto out_unlock;
1069 }
1070 }
1071
1072 /* We manipulate the conntrack inside the global conntrack table lock,
1073 * so there's no need to increase the refcount */
1074 DEBUGP("conntrack found\n");
1075 err = -EEXIST;
1076 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1077 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1078
1079out_unlock:
1080 write_unlock_bh(&ip_conntrack_lock);
1081 return err;
1082}
1083
1084/***********************************************************************
1085 * EXPECT
1086 ***********************************************************************/
1087
1088static inline int
1089ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1090 const struct ip_conntrack_tuple *tuple,
1091 enum ctattr_expect type)
1092{
1093 struct nfattr *nest_parms = NFA_NEST(skb, type);
1094
1095 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1096 goto nfattr_failure;
1097
1098 NFA_NEST_END(skb, nest_parms);
1099
1100 return 0;
1101
1102nfattr_failure:
1103 return -1;
1104}
1105
1106static inline int
1107ctnetlink_exp_dump_expect(struct sk_buff *skb,
1108 const struct ip_conntrack_expect *exp)
1109{
1110 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1111 u_int32_t id = htonl(exp->id);
1112 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT);
1113
1114 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1115 goto nfattr_failure;
1116 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1117 goto nfattr_failure;
1118
1119 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1120 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1121 NFA_NEST_END(skb, nest_parms);
1122
1123 return 0;
1124
1125nfattr_failure:
1126 return -1;
1127}
1128
1129static int
1130ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1131 int event,
1132 int nowait,
1133 const struct ip_conntrack_expect *exp)
1134{
1135 struct nlmsghdr *nlh;
1136 struct nfgenmsg *nfmsg;
1137 unsigned char *b;
1138
1139 b = skb->tail;
1140
1141 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1142 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1143 nfmsg = NLMSG_DATA(nlh);
1144
1145 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1146 nfmsg->nfgen_family = AF_INET;
1147 nfmsg->version = NFNETLINK_V0;
1148 nfmsg->res_id = 0;
1149
1150 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1151 goto nfattr_failure;
1152
1153 nlh->nlmsg_len = skb->tail - b;
1154 return skb->len;
1155
1156nlmsg_failure:
1157nfattr_failure:
1158 skb_trim(skb, b - skb->data);
1159 return -1;
1160}
1161
1162#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1163static int ctnetlink_expect_event(struct notifier_block *this,
1164 unsigned long events, void *ptr)
1165{
1166 struct nlmsghdr *nlh;
1167 struct nfgenmsg *nfmsg;
1168 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1169 struct sk_buff *skb;
1170 unsigned int type;
1171 unsigned char *b;
1172 int flags = 0;
1173 u16 proto;
1174
1175 if (events & IPEXP_NEW) {
1176 type = IPCTNL_MSG_EXP_NEW;
1177 flags = NLM_F_CREATE|NLM_F_EXCL;
1178 } else
1179 return NOTIFY_DONE;
1180
1181 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1182 if (!skb)
1183 return NOTIFY_DONE;
1184
1185 b = skb->tail;
1186
1187 type |= NFNL_SUBSYS_CTNETLINK << 8;
1188 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1189 nfmsg = NLMSG_DATA(nlh);
1190
1191 nlh->nlmsg_flags = flags;
1192 nfmsg->nfgen_family = AF_INET;
1193 nfmsg->version = NFNETLINK_V0;
1194 nfmsg->res_id = 0;
1195
1196 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1197 goto nfattr_failure;
1198
1199 nlh->nlmsg_len = skb->tail - b;
1200 proto = exp->tuple.dst.protonum;
1201 nfnetlink_send(skb, 0, NF_NETLINK_CONNTRACK_EXP_NEW, 0);
1202 return NOTIFY_DONE;
1203
1204nlmsg_failure:
1205nfattr_failure:
1206 kfree_skb(skb);
1207 return NOTIFY_DONE;
1208}
1209#endif
1210
1211static int
1212ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1213{
1214 struct ip_conntrack_expect *exp = NULL;
1215 struct list_head *i;
1216 u_int32_t *id = (u_int32_t *) &cb->args[0];
1217
1218 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1219
1220 read_lock_bh(&ip_conntrack_lock);
1221 list_for_each(i, &ip_conntrack_expect_list) {
1222 exp = (struct ip_conntrack_expect *) i;
1223 if (exp->id <= *id)
1224 continue;
1225 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1226 cb->nlh->nlmsg_seq,
1227 IPCTNL_MSG_EXP_NEW,
1228 1, exp) < 0)
1229 goto out;
1230 *id = exp->id;
1231 }
1232out:
1233 read_unlock_bh(&ip_conntrack_lock);
1234
1235 DEBUGP("leaving, last id=%llu\n", *id);
1236
1237 return skb->len;
1238}
1239
1240static int
1241ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1242 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1243{
1244 struct ip_conntrack_tuple tuple;
1245 struct ip_conntrack_expect *exp;
1246 struct sk_buff *skb2;
1247 int err = 0;
1248
1249 DEBUGP("entered %s\n", __FUNCTION__);
1250
1251 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1252 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1253 u32 rlen;
1254
1255 if (msg->nfgen_family != AF_INET)
1256 return -EAFNOSUPPORT;
1257
1258 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1259 ctnetlink_exp_dump_table,
1260 ctnetlink_done)) != 0)
1261 return -EINVAL;
1262 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1263 if (rlen > skb->len)
1264 rlen = skb->len;
1265 skb_pull(skb, rlen);
1266 return 0;
1267 }
1268
1269 if (cda[CTA_TUPLE_ORIG-1])
1270 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
1271 else if (cda[CTA_TUPLE_REPLY-1])
1272 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
1273 else
1274 return -EINVAL;
1275
1276 if (err < 0)
1277 return err;
1278
1279 exp = ip_conntrack_expect_find_get(&tuple);
1280 if (!exp)
1281 return -ENOENT;
1282
1283 err = -ENOMEM;
1284 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1285 if (!skb2)
1286 goto out;
1287 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1288
1289 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1290 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1291 1, exp);
1292 if (err <= 0)
1293 goto out;
1294
1295 ip_conntrack_expect_put(exp);
1296
1297 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1298 if (err < 0)
1299 goto free;
1300
1301 return err;
1302
1303out:
1304 ip_conntrack_expect_put(exp);
1305free:
1306 if (skb2)
1307 kfree_skb(skb2);
1308 return err;
1309}
1310
1311static int
1312ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1313 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1314{
1315 struct ip_conntrack_expect *exp, *tmp;
1316 struct ip_conntrack_tuple tuple;
1317 struct ip_conntrack_helper *h;
1318 int err;
1319
1320 /* delete by tuple needs either orig or reply tuple */
1321 if (cda[CTA_TUPLE_ORIG-1])
1322 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
1323 else if (cda[CTA_TUPLE_REPLY-1])
1324 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
1325 else if (cda[CTA_HELP_NAME-1]) {
1326 char *name = NFA_DATA(cda[CTA_HELP_NAME-1]);
1327
1328 /* delete all expectations for this helper */
1329 write_lock_bh(&ip_conntrack_lock);
1330 h = __ip_conntrack_helper_find_byname(name);
1331 if (!h) {
1332 write_unlock_bh(&ip_conntrack_lock);
1333 return -EINVAL;
1334 }
1335 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1336 list) {
1337 if (exp->master->helper == h
1338 && del_timer(&exp->timeout))
1339 __ip_ct_expect_unlink_destroy(exp);
1340 }
1341 write_unlock(&ip_conntrack_lock);
1342 return 0;
1343 } else {
1344 /* This basically means we have to flush everything*/
1345 write_lock_bh(&ip_conntrack_lock);
1346 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1347 list) {
1348 if (del_timer(&exp->timeout))
1349 __ip_ct_expect_unlink_destroy(exp);
1350 }
1351 write_unlock_bh(&ip_conntrack_lock);
1352 return 0;
1353 }
1354
1355 if (err < 0)
1356 return err;
1357
1358 /* bump usage count to 2 */
1359 exp = ip_conntrack_expect_find_get(&tuple);
1360 if (!exp)
1361 return -ENOENT;
1362
1363 if (cda[CTA_EXPECT_ID-1]) {
1364 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1365 if (exp->id != ntohl(id)) {
1366 ip_conntrack_expect_put(exp);
1367 return -ENOENT;
1368 }
1369 }
1370
1371 /* after list removal, usage count == 1 */
1372 ip_conntrack_unexpect_related(exp);
1373 /* have to put what we 'get' above. after this line usage count == 0 */
1374 ip_conntrack_expect_put(exp);
1375
1376 return 0;
1377}
1378static int
1379ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1380{
1381 return -EOPNOTSUPP;
1382}
1383
1384static int
1385ctnetlink_create_expect(struct nfattr *cda[])
1386{
1387 struct ip_conntrack_tuple tuple, mask, master_tuple;
1388 struct ip_conntrack_tuple_hash *h = NULL;
1389 struct ip_conntrack_expect *exp;
1390 struct ip_conntrack *ct;
1391 int err = 0;
1392
1393 DEBUGP("entered %s\n", __FUNCTION__);
1394
1395 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1396 if (err < 0)
1397 return err;
1398 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASK);
1399 if (err < 0)
1400 return err;
1401
1402 if (cda[CTA_TUPLE_ORIG-1])
1403 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_TUPLE_ORIG);
1404 else if (cda[CTA_TUPLE_REPLY-1])
1405 err = ctnetlink_parse_tuple(cda, &master_tuple,
1406 CTA_TUPLE_REPLY);
1407 else
1408 return -EINVAL;
1409
1410 if (err < 0)
1411 return err;
1412
1413 /* Look for master conntrack of this expectation */
1414 h = ip_conntrack_find_get(&master_tuple, NULL);
1415 if (!h)
1416 return -ENOENT;
1417 ct = tuplehash_to_ctrack(h);
1418
1419 if (!ct->helper) {
1420 /* such conntrack hasn't got any helper, abort */
1421 err = -EINVAL;
1422 goto out;
1423 }
1424
1425 exp = ip_conntrack_expect_alloc(ct);
1426 if (!exp) {
1427 err = -ENOMEM;
1428 goto out;
1429 }
1430
1431 exp->expectfn = NULL;
1432 exp->master = ct;
1433 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1434 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1435
1436 err = ip_conntrack_expect_related(exp);
1437 ip_conntrack_expect_put(exp);
1438
1439out:
1440 ip_conntrack_put(tuplehash_to_ctrack(h));
1441 return err;
1442}
1443
1444static int
1445ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1446 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1447{
1448 struct ip_conntrack_tuple tuple;
1449 struct ip_conntrack_expect *exp;
1450 int err = 0;
1451
1452 DEBUGP("entered %s\n", __FUNCTION__);
1453
1454 if (!cda[CTA_EXPECT_TUPLE-1] || !cda[CTA_EXPECT_MASK-1])
1455 return -EINVAL;
1456
1457 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1458 if (err < 0)
1459 return err;
1460
1461 write_lock_bh(&ip_conntrack_lock);
1462 exp = __ip_conntrack_expect_find(&tuple);
1463
1464 if (!exp) {
1465 write_unlock_bh(&ip_conntrack_lock);
1466 err = -ENOENT;
1467 if (nlh->nlmsg_flags & NLM_F_CREATE)
1468 err = ctnetlink_create_expect(cda);
1469 return err;
1470 }
1471
1472 err = -EEXIST;
1473 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1474 err = ctnetlink_change_expect(exp, cda);
1475 write_unlock_bh(&ip_conntrack_lock);
1476
1477 DEBUGP("leaving\n");
1478
1479 return err;
1480}
1481
1482#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1483static struct notifier_block ctnl_notifier = {
1484 .notifier_call = ctnetlink_conntrack_event,
1485};
1486
1487static struct notifier_block ctnl_notifier_exp = {
1488 .notifier_call = ctnetlink_expect_event,
1489};
1490#endif
1491
1492static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1493 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1494 .cap_required = CAP_NET_ADMIN },
1495 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1496 .cap_required = CAP_NET_ADMIN },
1497 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1498 .cap_required = CAP_NET_ADMIN },
1499 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1500 .cap_required = CAP_NET_ADMIN },
1501};
1502
1503static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_MAX] = {
1504 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1505 .cap_required = CAP_NET_ADMIN },
1506 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1507 .cap_required = CAP_NET_ADMIN },
1508 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1509 .cap_required = CAP_NET_ADMIN },
1510};
1511
1512static struct nfnetlink_subsystem ctnl_subsys = {
1513 .name = "conntrack",
1514 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1515 .cb_count = IPCTNL_MSG_MAX,
1516 .attr_count = CTA_MAX,
1517 .cb = ctnl_cb,
1518};
1519
1520static struct nfnetlink_subsystem ctnl_exp_subsys = {
1521 .name = "conntrack_expect",
1522 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1523 .cb_count = IPCTNL_MSG_EXP_MAX,
1524 .attr_count = CTA_MAX,
1525 .cb = ctnl_exp_cb,
1526};
1527
1528static int __init ctnetlink_init(void)
1529{
1530 int ret;
1531
1532 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1533 ret = nfnetlink_subsys_register(&ctnl_subsys);
1534 if (ret < 0) {
1535 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1536 goto err_out;
1537 }
1538
1539 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1540 if (ret < 0) {
1541 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1542 goto err_unreg_subsys;
1543 }
1544
1545#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1546 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1547 if (ret < 0) {
1548 printk("ctnetlink_init: cannot register notifier.\n");
1549 goto err_unreg_exp_subsys;
1550 }
1551
1552 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1553 if (ret < 0) {
1554 printk("ctnetlink_init: cannot expect register notifier.\n");
1555 goto err_unreg_notifier;
1556 }
1557#endif
1558
1559 return 0;
1560
1561#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1562err_unreg_notifier:
1563 ip_conntrack_unregister_notifier(&ctnl_notifier);
1564err_unreg_exp_subsys:
1565 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1566#endif
1567err_unreg_subsys:
1568 nfnetlink_subsys_unregister(&ctnl_subsys);
1569err_out:
1570 return ret;
1571}
1572
1573static void __exit ctnetlink_exit(void)
1574{
1575 printk("ctnetlink: unregistering from nfnetlink.\n");
1576
1577#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1578 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1579 ip_conntrack_unregister_notifier(&ctnl_notifier);
1580#endif
1581
1582 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1583 nfnetlink_subsys_unregister(&ctnl_subsys);
1584 return;
1585}
1586
1587module_init(ctnetlink_init);
1588module_exit(ctnetlink_exit);