blob: 06ed91ee8ace179d7f31a135a642ece60aff06f8 [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
Harald Weltea051a8f2005-10-10 21:21:10 -0700180 tmp = htonl(ct->counters[dir].packets);
181 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
Harald Welte080774a2005-08-09 19:32:58 -0700182
Harald Weltea051a8f2005-10-10 21:21:10 -0700183 tmp = htonl(ct->counters[dir].bytes);
184 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
Harald Welte080774a2005-08-09 19:32:58 -0700185
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;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700300 unsigned int flags = 0, group;
Harald Welte080774a2005-08-09 19:32:58 -0700301
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;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700308 group = NFNLGRP_CONNTRACK_DESTROY;
Harald Welte080774a2005-08-09 19:32:58 -0700309 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;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700316 group = NFNLGRP_CONNTRACK_NEW;
Harald Welte080774a2005-08-09 19:32:58 -0700317 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;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700325 group = NFNLGRP_CONNTRACK_UPDATE;
Harald Welte080774a2005-08-09 19:32:58 -0700326 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;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700378 nfnetlink_send(skb, 0, group, 0);
Harald Welte080774a2005-08-09 19:32:58 -0700379 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) {
Pablo Neira Ayusoff21d572005-08-09 20:06:42 -0700407 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
Harald Welte080774a2005-08-09 19:32:58 -0700408 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) {
Pablo Neira Ayusoff21d572005-08-09 20:06:42 -0700444 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
Harald Welte080774a2005-08-09 19:32:58 -0700445 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
Harald Welte080774a2005-08-09 19:32:58 -0700482
483 if (nfattr_parse_nested(tb, CTA_IP_MAX, attr) < 0)
484 goto nfattr_failure;
485
486 if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
487 return -EINVAL;
488
489 if (!tb[CTA_IP_V4_SRC-1])
490 return -EINVAL;
491 tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
492
493 if (!tb[CTA_IP_V4_DST-1])
494 return -EINVAL;
495 tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
496
497 DEBUGP("leaving\n");
498
499 return 0;
500
501nfattr_failure:
502 return -1;
503}
504
505static const int cta_min_proto[CTA_PROTO_MAX] = {
506 [CTA_PROTO_NUM-1] = sizeof(u_int16_t),
507 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
508 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
509 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
510 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
511 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
512};
513
514static inline int
515ctnetlink_parse_tuple_proto(struct nfattr *attr,
516 struct ip_conntrack_tuple *tuple)
517{
518 struct nfattr *tb[CTA_PROTO_MAX];
519 struct ip_conntrack_protocol *proto;
520 int ret = 0;
521
522 DEBUGP("entered %s\n", __FUNCTION__);
523
Harald Welte080774a2005-08-09 19:32:58 -0700524 if (nfattr_parse_nested(tb, CTA_PROTO_MAX, attr) < 0)
525 goto nfattr_failure;
526
527 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
528 return -EINVAL;
529
530 if (!tb[CTA_PROTO_NUM-1])
531 return -EINVAL;
532 tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
533
534 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
535
536 if (likely(proto && proto->nfattr_to_tuple)) {
537 ret = proto->nfattr_to_tuple(tb, tuple);
538 ip_conntrack_proto_put(proto);
539 }
540
541 return ret;
542
543nfattr_failure:
544 return -1;
545}
546
547static inline int
548ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
549 enum ctattr_tuple type)
550{
551 struct nfattr *tb[CTA_TUPLE_MAX];
552 int err;
553
554 DEBUGP("entered %s\n", __FUNCTION__);
555
Harald Welte080774a2005-08-09 19:32:58 -0700556 memset(tuple, 0, sizeof(*tuple));
557
558 if (nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]) < 0)
559 goto nfattr_failure;
560
561 if (!tb[CTA_TUPLE_IP-1])
562 return -EINVAL;
563
564 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
565 if (err < 0)
566 return err;
567
568 if (!tb[CTA_TUPLE_PROTO-1])
569 return -EINVAL;
570
571 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
572 if (err < 0)
573 return err;
574
575 /* orig and expect tuples get DIR_ORIGINAL */
576 if (type == CTA_TUPLE_REPLY)
577 tuple->dst.dir = IP_CT_DIR_REPLY;
578 else
579 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
580
581 DUMP_TUPLE(tuple);
582
583 DEBUGP("leaving\n");
584
585 return 0;
586
587nfattr_failure:
588 return -1;
589}
590
591#ifdef CONFIG_IP_NF_NAT_NEEDED
592static const int cta_min_protonat[CTA_PROTONAT_MAX] = {
593 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
594 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
595};
596
597static int ctnetlink_parse_nat_proto(struct nfattr *attr,
598 const struct ip_conntrack *ct,
599 struct ip_nat_range *range)
600{
601 struct nfattr *tb[CTA_PROTONAT_MAX];
602 struct ip_nat_protocol *npt;
603
604 DEBUGP("entered %s\n", __FUNCTION__);
605
Harald Welte080774a2005-08-09 19:32:58 -0700606 if (nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr) < 0)
607 goto nfattr_failure;
608
609 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
610 goto nfattr_failure;
611
612 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
613 if (!npt)
614 return 0;
615
616 if (!npt->nfattr_to_range) {
617 ip_nat_proto_put(npt);
618 return 0;
619 }
620
621 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
622 if (npt->nfattr_to_range(tb, range) > 0)
623 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
624
625 ip_nat_proto_put(npt);
626
627 DEBUGP("leaving\n");
628 return 0;
629
630nfattr_failure:
631 return -1;
632}
633
634static inline int
635ctnetlink_parse_nat(struct nfattr *cda[],
636 const struct ip_conntrack *ct, struct ip_nat_range *range)
637{
638 struct nfattr *tb[CTA_NAT_MAX];
639 int err;
640
641 DEBUGP("entered %s\n", __FUNCTION__);
642
Harald Welte080774a2005-08-09 19:32:58 -0700643 memset(range, 0, sizeof(*range));
644
645 if (nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]) < 0)
646 goto nfattr_failure;
647
648 if (tb[CTA_NAT_MINIP-1])
649 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
650
651 if (!tb[CTA_NAT_MAXIP-1])
652 range->max_ip = range->min_ip;
653 else
654 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
655
656 if (range->min_ip)
657 range->flags |= IP_NAT_RANGE_MAP_IPS;
658
659 if (!tb[CTA_NAT_PROTO-1])
660 return 0;
661
662 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
663 if (err < 0)
664 return err;
665
666 DEBUGP("leaving\n");
667 return 0;
668
669nfattr_failure:
670 return -1;
671}
672#endif
673
674static inline int
675ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
676{
677 struct nfattr *tb[CTA_HELP_MAX];
678
679 DEBUGP("entered %s\n", __FUNCTION__);
Harald Welte080774a2005-08-09 19:32:58 -0700680
681 if (nfattr_parse_nested(tb, CTA_HELP_MAX, attr) < 0)
682 goto nfattr_failure;
683
684 if (!tb[CTA_HELP_NAME-1])
685 return -EINVAL;
686
687 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
688
689 return 0;
690
691nfattr_failure:
692 return -1;
693}
694
695static int
696ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
697 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
698{
699 struct ip_conntrack_tuple_hash *h;
700 struct ip_conntrack_tuple tuple;
701 struct ip_conntrack *ct;
702 int err = 0;
703
704 DEBUGP("entered %s\n", __FUNCTION__);
705
706 if (cda[CTA_TUPLE_ORIG-1])
707 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
708 else if (cda[CTA_TUPLE_REPLY-1])
709 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
710 else {
711 /* Flush the whole table */
712 ip_conntrack_flush();
713 return 0;
714 }
715
716 if (err < 0)
717 return err;
718
719 h = ip_conntrack_find_get(&tuple, NULL);
720 if (!h) {
721 DEBUGP("tuple not found in conntrack hash\n");
722 return -ENOENT;
723 }
724
725 ct = tuplehash_to_ctrack(h);
726
727 if (cda[CTA_ID-1]) {
728 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
729 if (ct->id != id) {
730 ip_conntrack_put(ct);
731 return -ENOENT;
732 }
733 }
734 if (del_timer(&ct->timeout)) {
735 ip_conntrack_put(ct);
736 ct->timeout.function((unsigned long)ct);
737 return 0;
738 }
739 ip_conntrack_put(ct);
740 DEBUGP("leaving\n");
741
742 return 0;
743}
744
745static int
746ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
747 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
748{
749 struct ip_conntrack_tuple_hash *h;
750 struct ip_conntrack_tuple tuple;
751 struct ip_conntrack *ct;
752 struct sk_buff *skb2 = NULL;
753 int err = 0;
754
755 DEBUGP("entered %s\n", __FUNCTION__);
756
757 if (nlh->nlmsg_flags & NLM_F_DUMP) {
758 struct nfgenmsg *msg = NLMSG_DATA(nlh);
759 u32 rlen;
760
761 if (msg->nfgen_family != AF_INET)
762 return -EAFNOSUPPORT;
763
764 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
765 IPCTNL_MSG_CT_GET_CTRZERO) {
766#ifdef CONFIG_IP_NF_CT_ACCT
767 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
768 ctnetlink_dump_table_w,
769 ctnetlink_done)) != 0)
770 return -EINVAL;
771#else
772 return -ENOTSUPP;
773#endif
774 } else {
775 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
776 ctnetlink_dump_table,
777 ctnetlink_done)) != 0)
778 return -EINVAL;
779 }
780
781 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
782 if (rlen > skb->len)
783 rlen = skb->len;
784 skb_pull(skb, rlen);
785 return 0;
786 }
787
788 if (cda[CTA_TUPLE_ORIG-1])
789 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
790 else if (cda[CTA_TUPLE_REPLY-1])
791 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
792 else
793 return -EINVAL;
794
795 if (err < 0)
796 return err;
797
798 h = ip_conntrack_find_get(&tuple, NULL);
799 if (!h) {
800 DEBUGP("tuple not found in conntrack hash");
801 return -ENOENT;
802 }
803 DEBUGP("tuple found\n");
804 ct = tuplehash_to_ctrack(h);
805
806 err = -ENOMEM;
807 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
808 if (!skb2) {
809 ip_conntrack_put(ct);
810 return -ENOMEM;
811 }
812 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
813
814 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
815 IPCTNL_MSG_CT_NEW, 1, ct);
816 ip_conntrack_put(ct);
817 if (err <= 0)
818 goto out;
819
820 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
821 if (err < 0)
822 goto out;
823
824 DEBUGP("leaving\n");
825 return 0;
826
827out:
828 if (skb2)
829 kfree_skb(skb2);
830 return -1;
831}
832
833static inline int
834ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
835{
Harald Welted000eaf2005-10-10 20:52:51 -0700836 unsigned long d;
837 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
Harald Welte080774a2005-08-09 19:32:58 -0700838 d = ct->status ^ status;
839
840 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
841 /* unchangeable */
842 return -EINVAL;
843
844 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
845 /* SEEN_REPLY bit can only be set */
846 return -EINVAL;
847
848
849 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
850 /* ASSURED bit can only be set */
851 return -EINVAL;
852
853 if (cda[CTA_NAT-1]) {
854#ifndef CONFIG_IP_NF_NAT_NEEDED
855 return -EINVAL;
856#else
857 unsigned int hooknum;
858 struct ip_nat_range range;
859
860 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
861 return -EINVAL;
862
863 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
864 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
865 htons(range.min.all), htons(range.max.all));
866
867 /* This is tricky but it works. ip_nat_setup_info needs the
868 * hook number as parameter, so let's do the correct
869 * conversion and run away */
870 if (status & IPS_SRC_NAT_DONE)
871 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
872 else if (status & IPS_DST_NAT_DONE)
873 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
874 else
875 return -EINVAL; /* Missing NAT flags */
876
877 DEBUGP("NAT status: %lu\n",
878 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
879
880 if (ip_nat_initialized(ct, hooknum))
881 return -EEXIST;
882 ip_nat_setup_info(ct, &range, hooknum);
883
884 DEBUGP("NAT status after setup_info: %lu\n",
885 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
886#endif
887 }
888
889 /* Be careful here, modifying NAT bits can screw up things,
890 * so don't let users modify them directly if they don't pass
891 * ip_nat_range. */
892 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
893 return 0;
894}
895
896
897static inline int
898ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
899{
900 struct ip_conntrack_helper *helper;
901 char *helpname;
902 int err;
903
904 DEBUGP("entered %s\n", __FUNCTION__);
905
906 /* don't change helper of sibling connections */
907 if (ct->master)
908 return -EINVAL;
909
910 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
911 if (err < 0)
912 return err;
913
914 helper = __ip_conntrack_helper_find_byname(helpname);
915 if (!helper) {
916 if (!strcmp(helpname, ""))
917 helper = NULL;
918 else
919 return -EINVAL;
920 }
921
922 if (ct->helper) {
923 if (!helper) {
924 /* we had a helper before ... */
925 ip_ct_remove_expectations(ct);
926 ct->helper = NULL;
927 } else {
928 /* need to zero data of old helper */
929 memset(&ct->help, 0, sizeof(ct->help));
930 }
931 }
932
933 ct->helper = helper;
934
935 return 0;
936}
937
938static inline int
939ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
940{
941 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
942
943 if (!del_timer(&ct->timeout))
944 return -ETIME;
945
946 ct->timeout.expires = jiffies + timeout * HZ;
947 add_timer(&ct->timeout);
948
949 return 0;
950}
951
952static int
953ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
954{
955 int err;
956
957 DEBUGP("entered %s\n", __FUNCTION__);
958
959 if (cda[CTA_HELP-1]) {
960 err = ctnetlink_change_helper(ct, cda);
961 if (err < 0)
962 return err;
963 }
964
965 if (cda[CTA_TIMEOUT-1]) {
966 err = ctnetlink_change_timeout(ct, cda);
967 if (err < 0)
968 return err;
969 }
970
971 if (cda[CTA_STATUS-1]) {
972 err = ctnetlink_change_status(ct, cda);
973 if (err < 0)
974 return err;
975 }
976
977 DEBUGP("all done\n");
978 return 0;
979}
980
981static int
982ctnetlink_create_conntrack(struct nfattr *cda[],
983 struct ip_conntrack_tuple *otuple,
984 struct ip_conntrack_tuple *rtuple)
985{
986 struct ip_conntrack *ct;
987 int err = -EINVAL;
988
989 DEBUGP("entered %s\n", __FUNCTION__);
990
991 ct = ip_conntrack_alloc(otuple, rtuple);
992 if (ct == NULL || IS_ERR(ct))
993 return -ENOMEM;
994
995 if (!cda[CTA_TIMEOUT-1])
996 goto err;
997 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
998
999 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1000 ct->status |= IPS_CONFIRMED;
1001
1002 err = ctnetlink_change_status(ct, cda);
1003 if (err < 0)
1004 goto err;
1005
1006 ct->helper = ip_conntrack_helper_find_get(rtuple);
1007
1008 add_timer(&ct->timeout);
1009 ip_conntrack_hash_insert(ct);
1010
1011 if (ct->helper)
1012 ip_conntrack_helper_put(ct->helper);
1013
1014 DEBUGP("conntrack with id %u inserted\n", ct->id);
1015 return 0;
1016
1017err:
1018 ip_conntrack_free(ct);
1019 return err;
1020}
1021
1022static int
1023ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1024 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1025{
1026 struct ip_conntrack_tuple otuple, rtuple;
1027 struct ip_conntrack_tuple_hash *h = NULL;
1028 int err = 0;
1029
1030 DEBUGP("entered %s\n", __FUNCTION__);
1031
1032 if (cda[CTA_TUPLE_ORIG-1]) {
1033 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1034 if (err < 0)
1035 return err;
1036 }
1037
1038 if (cda[CTA_TUPLE_REPLY-1]) {
1039 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1040 if (err < 0)
1041 return err;
1042 }
1043
1044 write_lock_bh(&ip_conntrack_lock);
1045 if (cda[CTA_TUPLE_ORIG-1])
1046 h = __ip_conntrack_find(&otuple, NULL);
1047 else if (cda[CTA_TUPLE_REPLY-1])
1048 h = __ip_conntrack_find(&rtuple, NULL);
1049
1050 if (h == NULL) {
1051 write_unlock_bh(&ip_conntrack_lock);
1052 DEBUGP("no such conntrack, create new\n");
1053 err = -ENOENT;
1054 if (nlh->nlmsg_flags & NLM_F_CREATE)
1055 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
Pablo Neira88aa0422005-08-09 20:02:55 -07001056 return err;
1057 }
1058 /* implicit 'else' */
1059
1060 /* we only allow nat config for new conntracks */
1061 if (cda[CTA_NAT-1]) {
1062 err = -EINVAL;
Harald Welte080774a2005-08-09 19:32:58 -07001063 goto out_unlock;
Harald Welte080774a2005-08-09 19:32:58 -07001064 }
1065
1066 /* We manipulate the conntrack inside the global conntrack table lock,
1067 * so there's no need to increase the refcount */
1068 DEBUGP("conntrack found\n");
1069 err = -EEXIST;
1070 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1071 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1072
1073out_unlock:
1074 write_unlock_bh(&ip_conntrack_lock);
1075 return err;
1076}
1077
1078/***********************************************************************
1079 * EXPECT
1080 ***********************************************************************/
1081
1082static inline int
1083ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1084 const struct ip_conntrack_tuple *tuple,
1085 enum ctattr_expect type)
1086{
1087 struct nfattr *nest_parms = NFA_NEST(skb, type);
1088
1089 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1090 goto nfattr_failure;
1091
1092 NFA_NEST_END(skb, nest_parms);
1093
1094 return 0;
1095
1096nfattr_failure:
1097 return -1;
1098}
1099
1100static inline int
1101ctnetlink_exp_dump_expect(struct sk_buff *skb,
1102 const struct ip_conntrack_expect *exp)
1103{
Harald Welte1444fc52005-08-09 20:04:07 -07001104 struct ip_conntrack *master = exp->master;
Harald Welte080774a2005-08-09 19:32:58 -07001105 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1106 u_int32_t id = htonl(exp->id);
Harald Welte080774a2005-08-09 19:32:58 -07001107
1108 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1109 goto nfattr_failure;
1110 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1111 goto nfattr_failure;
Harald Welte1444fc52005-08-09 20:04:07 -07001112 if (ctnetlink_exp_dump_tuple(skb,
1113 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1114 CTA_EXPECT_MASTER) < 0)
1115 goto nfattr_failure;
Harald Welte080774a2005-08-09 19:32:58 -07001116
1117 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1118 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
Harald Welte080774a2005-08-09 19:32:58 -07001119
1120 return 0;
1121
1122nfattr_failure:
1123 return -1;
1124}
1125
1126static int
1127ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1128 int event,
1129 int nowait,
1130 const struct ip_conntrack_expect *exp)
1131{
1132 struct nlmsghdr *nlh;
1133 struct nfgenmsg *nfmsg;
1134 unsigned char *b;
1135
1136 b = skb->tail;
1137
1138 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1139 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1140 nfmsg = NLMSG_DATA(nlh);
1141
1142 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1143 nfmsg->nfgen_family = AF_INET;
1144 nfmsg->version = NFNETLINK_V0;
1145 nfmsg->res_id = 0;
1146
1147 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1148 goto nfattr_failure;
1149
1150 nlh->nlmsg_len = skb->tail - b;
1151 return skb->len;
1152
1153nlmsg_failure:
1154nfattr_failure:
1155 skb_trim(skb, b - skb->data);
1156 return -1;
1157}
1158
1159#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1160static int ctnetlink_expect_event(struct notifier_block *this,
1161 unsigned long events, void *ptr)
1162{
1163 struct nlmsghdr *nlh;
1164 struct nfgenmsg *nfmsg;
1165 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1166 struct sk_buff *skb;
1167 unsigned int type;
1168 unsigned char *b;
1169 int flags = 0;
1170 u16 proto;
1171
1172 if (events & IPEXP_NEW) {
1173 type = IPCTNL_MSG_EXP_NEW;
1174 flags = NLM_F_CREATE|NLM_F_EXCL;
1175 } else
1176 return NOTIFY_DONE;
1177
1178 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1179 if (!skb)
1180 return NOTIFY_DONE;
1181
1182 b = skb->tail;
1183
1184 type |= NFNL_SUBSYS_CTNETLINK << 8;
1185 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1186 nfmsg = NLMSG_DATA(nlh);
1187
1188 nlh->nlmsg_flags = flags;
1189 nfmsg->nfgen_family = AF_INET;
1190 nfmsg->version = NFNETLINK_V0;
1191 nfmsg->res_id = 0;
1192
1193 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1194 goto nfattr_failure;
1195
1196 nlh->nlmsg_len = skb->tail - b;
1197 proto = exp->tuple.dst.protonum;
Patrick McHardyac6d4392005-08-14 19:29:52 -07001198 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
Harald Welte080774a2005-08-09 19:32:58 -07001199 return NOTIFY_DONE;
1200
1201nlmsg_failure:
1202nfattr_failure:
1203 kfree_skb(skb);
1204 return NOTIFY_DONE;
1205}
1206#endif
1207
1208static int
1209ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1210{
1211 struct ip_conntrack_expect *exp = NULL;
1212 struct list_head *i;
1213 u_int32_t *id = (u_int32_t *) &cb->args[0];
1214
1215 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1216
1217 read_lock_bh(&ip_conntrack_lock);
Pablo Neira Ayusoff21d572005-08-09 20:06:42 -07001218 list_for_each_prev(i, &ip_conntrack_expect_list) {
Harald Welte080774a2005-08-09 19:32:58 -07001219 exp = (struct ip_conntrack_expect *) i;
1220 if (exp->id <= *id)
1221 continue;
1222 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1223 cb->nlh->nlmsg_seq,
1224 IPCTNL_MSG_EXP_NEW,
1225 1, exp) < 0)
1226 goto out;
1227 *id = exp->id;
1228 }
1229out:
1230 read_unlock_bh(&ip_conntrack_lock);
1231
1232 DEBUGP("leaving, last id=%llu\n", *id);
1233
1234 return skb->len;
1235}
1236
1237static int
1238ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1239 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1240{
1241 struct ip_conntrack_tuple tuple;
1242 struct ip_conntrack_expect *exp;
1243 struct sk_buff *skb2;
1244 int err = 0;
1245
1246 DEBUGP("entered %s\n", __FUNCTION__);
1247
1248 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1249 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1250 u32 rlen;
1251
1252 if (msg->nfgen_family != AF_INET)
1253 return -EAFNOSUPPORT;
1254
1255 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1256 ctnetlink_exp_dump_table,
1257 ctnetlink_done)) != 0)
1258 return -EINVAL;
1259 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1260 if (rlen > skb->len)
1261 rlen = skb->len;
1262 skb_pull(skb, rlen);
1263 return 0;
1264 }
1265
Harald Welte1444fc52005-08-09 20:04:07 -07001266 if (cda[CTA_EXPECT_MASTER-1])
1267 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
Harald Welte080774a2005-08-09 19:32:58 -07001268 else
1269 return -EINVAL;
1270
1271 if (err < 0)
1272 return err;
1273
Patrick McHardya41bc002005-09-19 15:35:31 -07001274 exp = ip_conntrack_expect_find(&tuple);
Harald Welte080774a2005-08-09 19:32:58 -07001275 if (!exp)
1276 return -ENOENT;
1277
1278 err = -ENOMEM;
1279 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1280 if (!skb2)
1281 goto out;
1282 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1283
1284 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1285 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1286 1, exp);
1287 if (err <= 0)
1288 goto out;
1289
1290 ip_conntrack_expect_put(exp);
1291
1292 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1293 if (err < 0)
1294 goto free;
1295
1296 return err;
1297
1298out:
1299 ip_conntrack_expect_put(exp);
1300free:
1301 if (skb2)
1302 kfree_skb(skb2);
1303 return err;
1304}
1305
1306static int
1307ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1308 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1309{
1310 struct ip_conntrack_expect *exp, *tmp;
1311 struct ip_conntrack_tuple tuple;
1312 struct ip_conntrack_helper *h;
1313 int err;
1314
Harald Welte1444fc52005-08-09 20:04:07 -07001315 if (cda[CTA_EXPECT_TUPLE-1]) {
1316 /* delete a single expect by tuple */
1317 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1318 if (err < 0)
1319 return err;
1320
1321 /* bump usage count to 2 */
Patrick McHardya41bc002005-09-19 15:35:31 -07001322 exp = ip_conntrack_expect_find(&tuple);
Harald Welte1444fc52005-08-09 20:04:07 -07001323 if (!exp)
1324 return -ENOENT;
1325
1326 if (cda[CTA_EXPECT_ID-1]) {
1327 u_int32_t id =
1328 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1329 if (exp->id != ntohl(id)) {
1330 ip_conntrack_expect_put(exp);
1331 return -ENOENT;
1332 }
1333 }
1334
1335 /* after list removal, usage count == 1 */
1336 ip_conntrack_unexpect_related(exp);
1337 /* have to put what we 'get' above.
1338 * after this line usage count == 0 */
1339 ip_conntrack_expect_put(exp);
1340 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1341 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
Harald Welte080774a2005-08-09 19:32:58 -07001342
1343 /* delete all expectations for this helper */
1344 write_lock_bh(&ip_conntrack_lock);
1345 h = __ip_conntrack_helper_find_byname(name);
1346 if (!h) {
1347 write_unlock_bh(&ip_conntrack_lock);
1348 return -EINVAL;
1349 }
1350 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1351 list) {
1352 if (exp->master->helper == h
Pablo Neira Ayuso49719eb2005-09-06 15:10:46 -07001353 && del_timer(&exp->timeout)) {
1354 ip_ct_unlink_expect(exp);
1355 ip_conntrack_expect_put(exp);
1356 }
Harald Welte080774a2005-08-09 19:32:58 -07001357 }
1358 write_unlock(&ip_conntrack_lock);
Harald Welte080774a2005-08-09 19:32:58 -07001359 } else {
1360 /* This basically means we have to flush everything*/
1361 write_lock_bh(&ip_conntrack_lock);
1362 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1363 list) {
Pablo Neira Ayuso49719eb2005-09-06 15:10:46 -07001364 if (del_timer(&exp->timeout)) {
1365 ip_ct_unlink_expect(exp);
1366 ip_conntrack_expect_put(exp);
1367 }
Harald Welte080774a2005-08-09 19:32:58 -07001368 }
1369 write_unlock_bh(&ip_conntrack_lock);
Harald Welte080774a2005-08-09 19:32:58 -07001370 }
1371
Harald Welte080774a2005-08-09 19:32:58 -07001372 return 0;
1373}
1374static int
1375ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1376{
1377 return -EOPNOTSUPP;
1378}
1379
1380static int
1381ctnetlink_create_expect(struct nfattr *cda[])
1382{
1383 struct ip_conntrack_tuple tuple, mask, master_tuple;
1384 struct ip_conntrack_tuple_hash *h = NULL;
1385 struct ip_conntrack_expect *exp;
1386 struct ip_conntrack *ct;
1387 int err = 0;
1388
1389 DEBUGP("entered %s\n", __FUNCTION__);
1390
Harald Welte1444fc52005-08-09 20:04:07 -07001391 /* caller guarantees that those three CTA_EXPECT_* exist */
Harald Welte080774a2005-08-09 19:32:58 -07001392 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1393 if (err < 0)
1394 return err;
Harald Weltebd9a26b2005-08-09 20:03:22 -07001395 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
Harald Welte080774a2005-08-09 19:32:58 -07001396 if (err < 0)
1397 return err;
Harald Welte1444fc52005-08-09 20:04:07 -07001398 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
Harald Welte080774a2005-08-09 19:32:58 -07001399 if (err < 0)
1400 return err;
1401
1402 /* Look for master conntrack of this expectation */
1403 h = ip_conntrack_find_get(&master_tuple, NULL);
1404 if (!h)
1405 return -ENOENT;
1406 ct = tuplehash_to_ctrack(h);
1407
1408 if (!ct->helper) {
1409 /* such conntrack hasn't got any helper, abort */
1410 err = -EINVAL;
1411 goto out;
1412 }
1413
1414 exp = ip_conntrack_expect_alloc(ct);
1415 if (!exp) {
1416 err = -ENOMEM;
1417 goto out;
1418 }
1419
1420 exp->expectfn = NULL;
Patrick McHardy2248bcf2005-09-06 15:06:42 -07001421 exp->flags = 0;
Harald Welte080774a2005-08-09 19:32:58 -07001422 exp->master = ct;
1423 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1424 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1425
1426 err = ip_conntrack_expect_related(exp);
1427 ip_conntrack_expect_put(exp);
1428
1429out:
1430 ip_conntrack_put(tuplehash_to_ctrack(h));
1431 return err;
1432}
1433
1434static int
1435ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1436 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1437{
1438 struct ip_conntrack_tuple tuple;
1439 struct ip_conntrack_expect *exp;
1440 int err = 0;
1441
1442 DEBUGP("entered %s\n", __FUNCTION__);
1443
Harald Welte1444fc52005-08-09 20:04:07 -07001444 if (!cda[CTA_EXPECT_TUPLE-1]
1445 || !cda[CTA_EXPECT_MASK-1]
1446 || !cda[CTA_EXPECT_MASTER-1])
Harald Welte080774a2005-08-09 19:32:58 -07001447 return -EINVAL;
1448
1449 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1450 if (err < 0)
1451 return err;
1452
1453 write_lock_bh(&ip_conntrack_lock);
1454 exp = __ip_conntrack_expect_find(&tuple);
1455
1456 if (!exp) {
1457 write_unlock_bh(&ip_conntrack_lock);
1458 err = -ENOENT;
1459 if (nlh->nlmsg_flags & NLM_F_CREATE)
1460 err = ctnetlink_create_expect(cda);
1461 return err;
1462 }
1463
1464 err = -EEXIST;
1465 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1466 err = ctnetlink_change_expect(exp, cda);
1467 write_unlock_bh(&ip_conntrack_lock);
1468
1469 DEBUGP("leaving\n");
1470
1471 return err;
1472}
1473
1474#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1475static struct notifier_block ctnl_notifier = {
1476 .notifier_call = ctnetlink_conntrack_event,
1477};
1478
1479static struct notifier_block ctnl_notifier_exp = {
1480 .notifier_call = ctnetlink_expect_event,
1481};
1482#endif
1483
1484static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1485 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001486 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001487 .cap_required = CAP_NET_ADMIN },
1488 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001489 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001490 .cap_required = CAP_NET_ADMIN },
1491 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001492 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001493 .cap_required = CAP_NET_ADMIN },
1494 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001495 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001496 .cap_required = CAP_NET_ADMIN },
1497};
1498
Pablo Neira Ayuso28b19d92005-08-09 20:06:27 -07001499static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
Harald Welte080774a2005-08-09 19:32:58 -07001500 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
Harald Welte927ccbc2005-08-09 20:03:40 -07001501 .attr_count = CTA_EXPECT_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001502 .cap_required = CAP_NET_ADMIN },
1503 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
Harald Welte927ccbc2005-08-09 20:03:40 -07001504 .attr_count = CTA_EXPECT_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001505 .cap_required = CAP_NET_ADMIN },
1506 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
Harald Welte927ccbc2005-08-09 20:03:40 -07001507 .attr_count = CTA_EXPECT_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001508 .cap_required = CAP_NET_ADMIN },
1509};
1510
1511static struct nfnetlink_subsystem ctnl_subsys = {
1512 .name = "conntrack",
1513 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1514 .cb_count = IPCTNL_MSG_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001515 .cb = ctnl_cb,
1516};
1517
1518static struct nfnetlink_subsystem ctnl_exp_subsys = {
1519 .name = "conntrack_expect",
1520 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1521 .cb_count = IPCTNL_MSG_EXP_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001522 .cb = ctnl_exp_cb,
1523};
1524
1525static int __init ctnetlink_init(void)
1526{
1527 int ret;
1528
1529 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1530 ret = nfnetlink_subsys_register(&ctnl_subsys);
1531 if (ret < 0) {
1532 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1533 goto err_out;
1534 }
1535
1536 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1537 if (ret < 0) {
1538 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1539 goto err_unreg_subsys;
1540 }
1541
1542#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1543 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1544 if (ret < 0) {
1545 printk("ctnetlink_init: cannot register notifier.\n");
1546 goto err_unreg_exp_subsys;
1547 }
1548
1549 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1550 if (ret < 0) {
1551 printk("ctnetlink_init: cannot expect register notifier.\n");
1552 goto err_unreg_notifier;
1553 }
1554#endif
1555
1556 return 0;
1557
1558#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1559err_unreg_notifier:
1560 ip_conntrack_unregister_notifier(&ctnl_notifier);
1561err_unreg_exp_subsys:
1562 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1563#endif
1564err_unreg_subsys:
1565 nfnetlink_subsys_unregister(&ctnl_subsys);
1566err_out:
1567 return ret;
1568}
1569
1570static void __exit ctnetlink_exit(void)
1571{
1572 printk("ctnetlink: unregistering from nfnetlink.\n");
1573
1574#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1575 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1576 ip_conntrack_unregister_notifier(&ctnl_notifier);
1577#endif
1578
1579 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1580 nfnetlink_subsys_unregister(&ctnl_subsys);
1581 return;
1582}
1583
1584module_init(ctnetlink_init);
1585module_exit(ctnetlink_exit);