blob: 842fe2c081c9700fc79408c90deb372d4d8874de [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;
Yasuyuki Kozakaieaae4fa2005-11-09 12:58:46 -080061 int ret = 0;
Harald Welte080774a2005-08-09 19:32:58 -070062
63 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
64
65 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
Yasuyuki Kozakaieaae4fa2005-11-09 12:58:46 -080066 if (likely(proto && proto->tuple_to_nfattr)) {
67 ret = proto->tuple_to_nfattr(skb, tuple);
68 ip_conntrack_proto_put(proto);
69 }
Harald Welte080774a2005-08-09 19:32:58 -070070
Yasuyuki Kozakaieaae4fa2005-11-09 12:58:46 -080071 return ret;
Harald Welte080774a2005-08-09 19:32:58 -070072
73nfattr_failure:
74 return -1;
75}
76
77static inline int
78ctnetlink_dump_tuples(struct sk_buff *skb,
79 const struct ip_conntrack_tuple *tuple)
80{
81 struct nfattr *nest_parms;
82
83 nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
84 NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t), &tuple->src.ip);
85 NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t), &tuple->dst.ip);
86 NFA_NEST_END(skb, nest_parms);
87
88 nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
89 ctnetlink_dump_tuples_proto(skb, tuple);
90 NFA_NEST_END(skb, nest_parms);
91
92 return 0;
93
94nfattr_failure:
95 return -1;
96}
97
98static inline int
99ctnetlink_dump_status(struct sk_buff *skb, const struct ip_conntrack *ct)
100{
101 u_int32_t status = htonl((u_int32_t) ct->status);
102 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
103 return 0;
104
105nfattr_failure:
106 return -1;
107}
108
109static inline int
110ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct)
111{
112 long timeout_l = ct->timeout.expires - jiffies;
113 u_int32_t timeout;
114
115 if (timeout_l < 0)
116 timeout = 0;
117 else
118 timeout = htonl(timeout_l / HZ);
119
120 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
121 return 0;
122
123nfattr_failure:
124 return -1;
125}
126
127static inline int
128ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
129{
130 struct ip_conntrack_protocol *proto = ip_conntrack_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
131
132 struct nfattr *nest_proto;
133 int ret;
134
135 if (!proto || !proto->to_nfattr)
136 return 0;
137
138 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
139
140 ret = proto->to_nfattr(skb, nest_proto, ct);
141
142 ip_conntrack_proto_put(proto);
143
144 NFA_NEST_END(skb, nest_proto);
145
146 return ret;
147
148nfattr_failure:
149 return -1;
150}
151
152static inline int
153ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct)
154{
155 struct nfattr *nest_helper;
156
157 if (!ct->helper)
158 return 0;
159
160 nest_helper = NFA_NEST(skb, CTA_HELP);
161 NFA_PUT(skb, CTA_HELP_NAME, CTA_HELP_MAXNAMESIZE, &ct->helper->name);
162
163 if (ct->helper->to_nfattr)
164 ct->helper->to_nfattr(skb, ct);
165
166 NFA_NEST_END(skb, nest_helper);
167
168 return 0;
169
170nfattr_failure:
171 return -1;
172}
173
174#ifdef CONFIG_IP_NF_CT_ACCT
175static inline int
176ctnetlink_dump_counters(struct sk_buff *skb, const struct ip_conntrack *ct,
177 enum ip_conntrack_dir dir)
178{
179 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
180 struct nfattr *nest_count = NFA_NEST(skb, type);
Yasuyuki Kozakai46998f52005-11-09 12:58:05 -0800181 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].packets);
184 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
Harald Welte080774a2005-08-09 19:32:58 -0700185
Harald Weltea051a8f2005-10-10 21:21:10 -0700186 tmp = htonl(ct->counters[dir].bytes);
187 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
Harald Welte080774a2005-08-09 19:32:58 -0700188
189 NFA_NEST_END(skb, nest_count);
190
191 return 0;
192
193nfattr_failure:
194 return -1;
195}
196#else
197#define ctnetlink_dump_counters(a, b, c) (0)
198#endif
199
200#ifdef CONFIG_IP_NF_CONNTRACK_MARK
201static inline int
202ctnetlink_dump_mark(struct sk_buff *skb, const struct ip_conntrack *ct)
203{
204 u_int32_t mark = htonl(ct->mark);
205
206 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
207 return 0;
208
209nfattr_failure:
210 return -1;
211}
212#else
213#define ctnetlink_dump_mark(a, b) (0)
214#endif
215
216static inline int
217ctnetlink_dump_id(struct sk_buff *skb, const struct ip_conntrack *ct)
218{
219 u_int32_t id = htonl(ct->id);
220 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
221 return 0;
222
223nfattr_failure:
224 return -1;
225}
226
227static inline int
228ctnetlink_dump_use(struct sk_buff *skb, const struct ip_conntrack *ct)
229{
230 unsigned int use = htonl(atomic_read(&ct->ct_general.use));
231
232 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
233 return 0;
234
235nfattr_failure:
236 return -1;
237}
238
239#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
240
241static int
242ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
243 int event, int nowait,
244 const struct ip_conntrack *ct)
245{
246 struct nlmsghdr *nlh;
247 struct nfgenmsg *nfmsg;
248 struct nfattr *nest_parms;
249 unsigned char *b;
250
251 b = skb->tail;
252
253 event |= NFNL_SUBSYS_CTNETLINK << 8;
254 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
255 nfmsg = NLMSG_DATA(nlh);
256
257 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
258 nfmsg->nfgen_family = AF_INET;
259 nfmsg->version = NFNETLINK_V0;
260 nfmsg->res_id = 0;
261
262 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
263 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
264 goto nfattr_failure;
265 NFA_NEST_END(skb, nest_parms);
266
267 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
268 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
269 goto nfattr_failure;
270 NFA_NEST_END(skb, nest_parms);
271
272 if (ctnetlink_dump_status(skb, ct) < 0 ||
273 ctnetlink_dump_timeout(skb, ct) < 0 ||
274 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
275 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
276 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
277 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
278 ctnetlink_dump_mark(skb, ct) < 0 ||
279 ctnetlink_dump_id(skb, ct) < 0 ||
280 ctnetlink_dump_use(skb, ct) < 0)
281 goto nfattr_failure;
282
283 nlh->nlmsg_len = skb->tail - b;
284 return skb->len;
285
286nlmsg_failure:
287nfattr_failure:
288 skb_trim(skb, b - skb->data);
289 return -1;
290}
291
292#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
293static int ctnetlink_conntrack_event(struct notifier_block *this,
294 unsigned long events, void *ptr)
295{
296 struct nlmsghdr *nlh;
297 struct nfgenmsg *nfmsg;
298 struct nfattr *nest_parms;
299 struct ip_conntrack *ct = (struct ip_conntrack *)ptr;
300 struct sk_buff *skb;
301 unsigned int type;
302 unsigned char *b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700303 unsigned int flags = 0, group;
Harald Welte080774a2005-08-09 19:32:58 -0700304
305 /* ignore our fake conntrack entry */
306 if (ct == &ip_conntrack_untracked)
307 return NOTIFY_DONE;
308
309 if (events & IPCT_DESTROY) {
310 type = IPCTNL_MSG_CT_DELETE;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700311 group = NFNLGRP_CONNTRACK_DESTROY;
Harald Welte080774a2005-08-09 19:32:58 -0700312 goto alloc_skb;
313 }
314 if (events & (IPCT_NEW | IPCT_RELATED)) {
315 type = IPCTNL_MSG_CT_NEW;
316 flags = NLM_F_CREATE|NLM_F_EXCL;
317 /* dump everything */
318 events = ~0UL;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700319 group = NFNLGRP_CONNTRACK_NEW;
Harald Welte080774a2005-08-09 19:32:58 -0700320 goto alloc_skb;
321 }
322 if (events & (IPCT_STATUS |
323 IPCT_PROTOINFO |
324 IPCT_HELPER |
325 IPCT_HELPINFO |
326 IPCT_NATINFO)) {
327 type = IPCTNL_MSG_CT_NEW;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700328 group = NFNLGRP_CONNTRACK_UPDATE;
Harald Welte080774a2005-08-09 19:32:58 -0700329 goto alloc_skb;
330 }
331
332 return NOTIFY_DONE;
333
334alloc_skb:
335 /* FIXME: Check if there are any listeners before, don't hurt performance */
336
337 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
338 if (!skb)
339 return NOTIFY_DONE;
340
341 b = skb->tail;
342
343 type |= NFNL_SUBSYS_CTNETLINK << 8;
344 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
345 nfmsg = NLMSG_DATA(nlh);
346
347 nlh->nlmsg_flags = flags;
348 nfmsg->nfgen_family = AF_INET;
349 nfmsg->version = NFNETLINK_V0;
350 nfmsg->res_id = 0;
351
352 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
353 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
354 goto nfattr_failure;
355 NFA_NEST_END(skb, nest_parms);
356
357 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
358 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
359 goto nfattr_failure;
360 NFA_NEST_END(skb, nest_parms);
361
362 /* NAT stuff is now a status flag */
363 if ((events & IPCT_STATUS || events & IPCT_NATINFO)
364 && ctnetlink_dump_status(skb, ct) < 0)
365 goto nfattr_failure;
366 if (events & IPCT_REFRESH
367 && ctnetlink_dump_timeout(skb, ct) < 0)
368 goto nfattr_failure;
369 if (events & IPCT_PROTOINFO
370 && ctnetlink_dump_protoinfo(skb, ct) < 0)
371 goto nfattr_failure;
372 if (events & IPCT_HELPINFO
373 && ctnetlink_dump_helpinfo(skb, ct) < 0)
374 goto nfattr_failure;
375
376 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
377 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
378 goto nfattr_failure;
379
380 nlh->nlmsg_len = skb->tail - b;
Patrick McHardyac6d4392005-08-14 19:29:52 -0700381 nfnetlink_send(skb, 0, group, 0);
Harald Welte080774a2005-08-09 19:32:58 -0700382 return NOTIFY_DONE;
383
384nlmsg_failure:
385nfattr_failure:
386 kfree_skb(skb);
387 return NOTIFY_DONE;
388}
389#endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */
390
391static int ctnetlink_done(struct netlink_callback *cb)
392{
393 DEBUGP("entered %s\n", __FUNCTION__);
394 return 0;
395}
396
397static int
398ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
399{
400 struct ip_conntrack *ct = NULL;
401 struct ip_conntrack_tuple_hash *h;
402 struct list_head *i;
403 u_int32_t *id = (u_int32_t *) &cb->args[1];
404
405 DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__,
406 cb->args[0], *id);
407
408 read_lock_bh(&ip_conntrack_lock);
409 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
Pablo Neira Ayusoff21d572005-08-09 20:06:42 -0700410 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
Harald Welte080774a2005-08-09 19:32:58 -0700411 h = (struct ip_conntrack_tuple_hash *) i;
412 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
413 continue;
414 ct = tuplehash_to_ctrack(h);
415 if (ct->id <= *id)
416 continue;
417 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
418 cb->nlh->nlmsg_seq,
419 IPCTNL_MSG_CT_NEW,
420 1, ct) < 0)
421 goto out;
422 *id = ct->id;
423 }
424 }
425out:
426 read_unlock_bh(&ip_conntrack_lock);
427
428 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
429
430 return skb->len;
431}
432
433#ifdef CONFIG_IP_NF_CT_ACCT
434static int
435ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
436{
437 struct ip_conntrack *ct = NULL;
438 struct ip_conntrack_tuple_hash *h;
439 struct list_head *i;
440 u_int32_t *id = (u_int32_t *) &cb->args[1];
441
442 DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__,
443 cb->args[0], *id);
444
445 write_lock_bh(&ip_conntrack_lock);
446 for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
Pablo Neira Ayusoff21d572005-08-09 20:06:42 -0700447 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
Harald Welte080774a2005-08-09 19:32:58 -0700448 h = (struct ip_conntrack_tuple_hash *) i;
449 if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
450 continue;
451 ct = tuplehash_to_ctrack(h);
452 if (ct->id <= *id)
453 continue;
454 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
455 cb->nlh->nlmsg_seq,
456 IPCTNL_MSG_CT_NEW,
457 1, ct) < 0)
458 goto out;
459 *id = ct->id;
460
461 memset(&ct->counters, 0, sizeof(ct->counters));
462 }
463 }
464out:
465 write_unlock_bh(&ip_conntrack_lock);
466
467 DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
468
469 return skb->len;
470}
471#endif
472
473static const int cta_min_ip[CTA_IP_MAX] = {
474 [CTA_IP_V4_SRC-1] = sizeof(u_int32_t),
475 [CTA_IP_V4_DST-1] = sizeof(u_int32_t),
476};
477
478static inline int
479ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple)
480{
481 struct nfattr *tb[CTA_IP_MAX];
482
483 DEBUGP("entered %s\n", __FUNCTION__);
484
Harald Weltea2506c02005-11-09 12:59:13 -0800485 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
Harald Welte080774a2005-08-09 19:32:58 -0700486
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;
Harald Welte080774a2005-08-09 19:32:58 -0700501}
502
503static const int cta_min_proto[CTA_PROTO_MAX] = {
504 [CTA_PROTO_NUM-1] = sizeof(u_int16_t),
505 [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t),
506 [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t),
507 [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
508 [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
509 [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t),
510};
511
512static inline int
513ctnetlink_parse_tuple_proto(struct nfattr *attr,
514 struct ip_conntrack_tuple *tuple)
515{
516 struct nfattr *tb[CTA_PROTO_MAX];
517 struct ip_conntrack_protocol *proto;
518 int ret = 0;
519
520 DEBUGP("entered %s\n", __FUNCTION__);
521
Harald Weltea2506c02005-11-09 12:59:13 -0800522 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
Harald Welte080774a2005-08-09 19:32:58 -0700523
524 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
525 return -EINVAL;
526
527 if (!tb[CTA_PROTO_NUM-1])
528 return -EINVAL;
529 tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
530
531 proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
532
533 if (likely(proto && proto->nfattr_to_tuple)) {
534 ret = proto->nfattr_to_tuple(tb, tuple);
535 ip_conntrack_proto_put(proto);
536 }
537
538 return ret;
Harald Welte080774a2005-08-09 19:32:58 -0700539}
540
541static inline int
542ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
543 enum ctattr_tuple type)
544{
545 struct nfattr *tb[CTA_TUPLE_MAX];
546 int err;
547
548 DEBUGP("entered %s\n", __FUNCTION__);
549
Harald Welte080774a2005-08-09 19:32:58 -0700550 memset(tuple, 0, sizeof(*tuple));
551
Harald Weltea2506c02005-11-09 12:59:13 -0800552 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
Harald Welte080774a2005-08-09 19:32:58 -0700553
554 if (!tb[CTA_TUPLE_IP-1])
555 return -EINVAL;
556
557 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
558 if (err < 0)
559 return err;
560
561 if (!tb[CTA_TUPLE_PROTO-1])
562 return -EINVAL;
563
564 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
565 if (err < 0)
566 return err;
567
568 /* orig and expect tuples get DIR_ORIGINAL */
569 if (type == CTA_TUPLE_REPLY)
570 tuple->dst.dir = IP_CT_DIR_REPLY;
571 else
572 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
573
574 DUMP_TUPLE(tuple);
575
576 DEBUGP("leaving\n");
577
578 return 0;
Harald Welte080774a2005-08-09 19:32:58 -0700579}
580
581#ifdef CONFIG_IP_NF_NAT_NEEDED
582static const int cta_min_protonat[CTA_PROTONAT_MAX] = {
583 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
584 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
585};
586
587static int ctnetlink_parse_nat_proto(struct nfattr *attr,
588 const struct ip_conntrack *ct,
589 struct ip_nat_range *range)
590{
591 struct nfattr *tb[CTA_PROTONAT_MAX];
592 struct ip_nat_protocol *npt;
593
594 DEBUGP("entered %s\n", __FUNCTION__);
595
Harald Weltea2506c02005-11-09 12:59:13 -0800596 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
Harald Welte080774a2005-08-09 19:32:58 -0700597
598 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
Harald Weltea2506c02005-11-09 12:59:13 -0800599 return -1;
Harald Welte080774a2005-08-09 19:32:58 -0700600
601 npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
602 if (!npt)
603 return 0;
604
605 if (!npt->nfattr_to_range) {
606 ip_nat_proto_put(npt);
607 return 0;
608 }
609
610 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
611 if (npt->nfattr_to_range(tb, range) > 0)
612 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
613
614 ip_nat_proto_put(npt);
615
616 DEBUGP("leaving\n");
617 return 0;
Harald Welte080774a2005-08-09 19:32:58 -0700618}
619
620static inline int
621ctnetlink_parse_nat(struct nfattr *cda[],
622 const struct ip_conntrack *ct, struct ip_nat_range *range)
623{
624 struct nfattr *tb[CTA_NAT_MAX];
625 int err;
626
627 DEBUGP("entered %s\n", __FUNCTION__);
628
Harald Welte080774a2005-08-09 19:32:58 -0700629 memset(range, 0, sizeof(*range));
630
Harald Weltea2506c02005-11-09 12:59:13 -0800631 nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]);
Harald Welte080774a2005-08-09 19:32:58 -0700632
633 if (tb[CTA_NAT_MINIP-1])
634 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
635
636 if (!tb[CTA_NAT_MAXIP-1])
637 range->max_ip = range->min_ip;
638 else
639 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
640
641 if (range->min_ip)
642 range->flags |= IP_NAT_RANGE_MAP_IPS;
643
644 if (!tb[CTA_NAT_PROTO-1])
645 return 0;
646
647 err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
648 if (err < 0)
649 return err;
650
651 DEBUGP("leaving\n");
652 return 0;
Harald Welte080774a2005-08-09 19:32:58 -0700653}
654#endif
655
656static inline int
657ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
658{
659 struct nfattr *tb[CTA_HELP_MAX];
660
661 DEBUGP("entered %s\n", __FUNCTION__);
Harald Welte080774a2005-08-09 19:32:58 -0700662
Harald Weltea2506c02005-11-09 12:59:13 -0800663 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
Harald Welte080774a2005-08-09 19:32:58 -0700664
665 if (!tb[CTA_HELP_NAME-1])
666 return -EINVAL;
667
668 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
669
670 return 0;
Harald Welte080774a2005-08-09 19:32:58 -0700671}
672
673static int
674ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
675 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
676{
677 struct ip_conntrack_tuple_hash *h;
678 struct ip_conntrack_tuple tuple;
679 struct ip_conntrack *ct;
680 int err = 0;
681
682 DEBUGP("entered %s\n", __FUNCTION__);
683
684 if (cda[CTA_TUPLE_ORIG-1])
685 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
686 else if (cda[CTA_TUPLE_REPLY-1])
687 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
688 else {
689 /* Flush the whole table */
690 ip_conntrack_flush();
691 return 0;
692 }
693
694 if (err < 0)
695 return err;
696
697 h = ip_conntrack_find_get(&tuple, NULL);
698 if (!h) {
699 DEBUGP("tuple not found in conntrack hash\n");
700 return -ENOENT;
701 }
702
703 ct = tuplehash_to_ctrack(h);
704
705 if (cda[CTA_ID-1]) {
706 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
707 if (ct->id != id) {
708 ip_conntrack_put(ct);
709 return -ENOENT;
710 }
711 }
712 if (del_timer(&ct->timeout)) {
713 ip_conntrack_put(ct);
714 ct->timeout.function((unsigned long)ct);
715 return 0;
716 }
717 ip_conntrack_put(ct);
718 DEBUGP("leaving\n");
719
720 return 0;
721}
722
723static int
724ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
725 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
726{
727 struct ip_conntrack_tuple_hash *h;
728 struct ip_conntrack_tuple tuple;
729 struct ip_conntrack *ct;
730 struct sk_buff *skb2 = NULL;
731 int err = 0;
732
733 DEBUGP("entered %s\n", __FUNCTION__);
734
735 if (nlh->nlmsg_flags & NLM_F_DUMP) {
736 struct nfgenmsg *msg = NLMSG_DATA(nlh);
737 u32 rlen;
738
739 if (msg->nfgen_family != AF_INET)
740 return -EAFNOSUPPORT;
741
742 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
743 IPCTNL_MSG_CT_GET_CTRZERO) {
744#ifdef CONFIG_IP_NF_CT_ACCT
745 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
746 ctnetlink_dump_table_w,
747 ctnetlink_done)) != 0)
748 return -EINVAL;
749#else
750 return -ENOTSUPP;
751#endif
752 } else {
753 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
754 ctnetlink_dump_table,
755 ctnetlink_done)) != 0)
756 return -EINVAL;
757 }
758
759 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
760 if (rlen > skb->len)
761 rlen = skb->len;
762 skb_pull(skb, rlen);
763 return 0;
764 }
765
766 if (cda[CTA_TUPLE_ORIG-1])
767 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
768 else if (cda[CTA_TUPLE_REPLY-1])
769 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
770 else
771 return -EINVAL;
772
773 if (err < 0)
774 return err;
775
776 h = ip_conntrack_find_get(&tuple, NULL);
777 if (!h) {
778 DEBUGP("tuple not found in conntrack hash");
779 return -ENOENT;
780 }
781 DEBUGP("tuple found\n");
782 ct = tuplehash_to_ctrack(h);
783
784 err = -ENOMEM;
785 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
786 if (!skb2) {
787 ip_conntrack_put(ct);
788 return -ENOMEM;
789 }
790 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
791
792 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
793 IPCTNL_MSG_CT_NEW, 1, ct);
794 ip_conntrack_put(ct);
795 if (err <= 0)
Harald Welte0f81eb42005-11-03 19:05:37 +0100796 goto free;
Harald Welte080774a2005-08-09 19:32:58 -0700797
798 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
799 if (err < 0)
800 goto out;
801
802 DEBUGP("leaving\n");
803 return 0;
804
Harald Welte0f81eb42005-11-03 19:05:37 +0100805free:
806 kfree_skb(skb2);
Harald Welte080774a2005-08-09 19:32:58 -0700807out:
Harald Welte080774a2005-08-09 19:32:58 -0700808 return -1;
809}
810
811static inline int
812ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
813{
Harald Welted000eaf2005-10-10 20:52:51 -0700814 unsigned long d;
815 unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
Harald Welte080774a2005-08-09 19:32:58 -0700816 d = ct->status ^ status;
817
818 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
819 /* unchangeable */
820 return -EINVAL;
821
822 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
823 /* SEEN_REPLY bit can only be set */
824 return -EINVAL;
825
826
827 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
828 /* ASSURED bit can only be set */
829 return -EINVAL;
830
831 if (cda[CTA_NAT-1]) {
832#ifndef CONFIG_IP_NF_NAT_NEEDED
833 return -EINVAL;
834#else
835 unsigned int hooknum;
836 struct ip_nat_range range;
837
838 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
839 return -EINVAL;
840
841 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n",
842 NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
843 htons(range.min.all), htons(range.max.all));
844
845 /* This is tricky but it works. ip_nat_setup_info needs the
846 * hook number as parameter, so let's do the correct
847 * conversion and run away */
848 if (status & IPS_SRC_NAT_DONE)
849 hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
850 else if (status & IPS_DST_NAT_DONE)
851 hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */
852 else
853 return -EINVAL; /* Missing NAT flags */
854
855 DEBUGP("NAT status: %lu\n",
856 status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
857
858 if (ip_nat_initialized(ct, hooknum))
859 return -EEXIST;
860 ip_nat_setup_info(ct, &range, hooknum);
861
862 DEBUGP("NAT status after setup_info: %lu\n",
863 ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
864#endif
865 }
866
867 /* Be careful here, modifying NAT bits can screw up things,
868 * so don't let users modify them directly if they don't pass
869 * ip_nat_range. */
870 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
871 return 0;
872}
873
874
875static inline int
876ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
877{
878 struct ip_conntrack_helper *helper;
879 char *helpname;
880 int err;
881
882 DEBUGP("entered %s\n", __FUNCTION__);
883
884 /* don't change helper of sibling connections */
885 if (ct->master)
886 return -EINVAL;
887
888 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
889 if (err < 0)
890 return err;
891
892 helper = __ip_conntrack_helper_find_byname(helpname);
893 if (!helper) {
894 if (!strcmp(helpname, ""))
895 helper = NULL;
896 else
897 return -EINVAL;
898 }
899
900 if (ct->helper) {
901 if (!helper) {
902 /* we had a helper before ... */
903 ip_ct_remove_expectations(ct);
904 ct->helper = NULL;
905 } else {
906 /* need to zero data of old helper */
907 memset(&ct->help, 0, sizeof(ct->help));
908 }
909 }
910
911 ct->helper = helper;
912
913 return 0;
914}
915
916static inline int
917ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
918{
919 u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
920
921 if (!del_timer(&ct->timeout))
922 return -ETIME;
923
924 ct->timeout.expires = jiffies + timeout * HZ;
925 add_timer(&ct->timeout);
926
927 return 0;
928}
929
Pablo Neira Ayuso061cb4a2005-10-10 21:23:46 -0700930static inline int
931ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
932{
933 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
934 struct ip_conntrack_protocol *proto;
935 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
936 int err = 0;
937
Harald Weltea2506c02005-11-09 12:59:13 -0800938 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
Pablo Neira Ayuso061cb4a2005-10-10 21:23:46 -0700939
940 proto = ip_conntrack_proto_find_get(npt);
941 if (!proto)
942 return -EINVAL;
943
944 if (proto->from_nfattr)
945 err = proto->from_nfattr(tb, ct);
946 ip_conntrack_proto_put(proto);
947
948 return err;
Pablo Neira Ayuso061cb4a2005-10-10 21:23:46 -0700949}
950
Harald Welte080774a2005-08-09 19:32:58 -0700951static int
952ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
953{
954 int err;
955
956 DEBUGP("entered %s\n", __FUNCTION__);
957
958 if (cda[CTA_HELP-1]) {
959 err = ctnetlink_change_helper(ct, cda);
960 if (err < 0)
961 return err;
962 }
963
964 if (cda[CTA_TIMEOUT-1]) {
965 err = ctnetlink_change_timeout(ct, cda);
966 if (err < 0)
967 return err;
968 }
969
970 if (cda[CTA_STATUS-1]) {
971 err = ctnetlink_change_status(ct, cda);
972 if (err < 0)
973 return err;
974 }
975
Pablo Neira Ayuso061cb4a2005-10-10 21:23:46 -0700976 if (cda[CTA_PROTOINFO-1]) {
977 err = ctnetlink_change_protoinfo(ct, cda);
978 if (err < 0)
979 return err;
980 }
981
Pablo Neira Ayuso02a78cd2005-11-09 13:00:04 -0800982#if defined(CONFIG_IP_NF_CONNTRACK_MARK)
983 if (cda[CTA_MARK-1])
984 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
985#endif
986
Harald Welte080774a2005-08-09 19:32:58 -0700987 DEBUGP("all done\n");
988 return 0;
989}
990
991static int
992ctnetlink_create_conntrack(struct nfattr *cda[],
993 struct ip_conntrack_tuple *otuple,
994 struct ip_conntrack_tuple *rtuple)
995{
996 struct ip_conntrack *ct;
997 int err = -EINVAL;
998
999 DEBUGP("entered %s\n", __FUNCTION__);
1000
1001 ct = ip_conntrack_alloc(otuple, rtuple);
1002 if (ct == NULL || IS_ERR(ct))
1003 return -ENOMEM;
1004
1005 if (!cda[CTA_TIMEOUT-1])
1006 goto err;
1007 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1008
1009 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1010 ct->status |= IPS_CONFIRMED;
1011
1012 err = ctnetlink_change_status(ct, cda);
1013 if (err < 0)
1014 goto err;
1015
Pablo Neira Ayuso061cb4a2005-10-10 21:23:46 -07001016 if (cda[CTA_PROTOINFO-1]) {
1017 err = ctnetlink_change_protoinfo(ct, cda);
1018 if (err < 0)
1019 return err;
1020 }
1021
Harald Welte080774a2005-08-09 19:32:58 -07001022 ct->helper = ip_conntrack_helper_find_get(rtuple);
1023
1024 add_timer(&ct->timeout);
1025 ip_conntrack_hash_insert(ct);
1026
1027 if (ct->helper)
1028 ip_conntrack_helper_put(ct->helper);
1029
Pablo Neira Ayuso02a78cd2005-11-09 13:00:04 -08001030#if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1031 if (cda[CTA_MARK-1])
1032 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1033#endif
1034
Harald Welte080774a2005-08-09 19:32:58 -07001035 DEBUGP("conntrack with id %u inserted\n", ct->id);
1036 return 0;
1037
1038err:
1039 ip_conntrack_free(ct);
1040 return err;
1041}
1042
1043static int
1044ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1045 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1046{
1047 struct ip_conntrack_tuple otuple, rtuple;
1048 struct ip_conntrack_tuple_hash *h = NULL;
1049 int err = 0;
1050
1051 DEBUGP("entered %s\n", __FUNCTION__);
1052
1053 if (cda[CTA_TUPLE_ORIG-1]) {
1054 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1055 if (err < 0)
1056 return err;
1057 }
1058
1059 if (cda[CTA_TUPLE_REPLY-1]) {
1060 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1061 if (err < 0)
1062 return err;
1063 }
1064
1065 write_lock_bh(&ip_conntrack_lock);
1066 if (cda[CTA_TUPLE_ORIG-1])
1067 h = __ip_conntrack_find(&otuple, NULL);
1068 else if (cda[CTA_TUPLE_REPLY-1])
1069 h = __ip_conntrack_find(&rtuple, NULL);
1070
1071 if (h == NULL) {
1072 write_unlock_bh(&ip_conntrack_lock);
1073 DEBUGP("no such conntrack, create new\n");
1074 err = -ENOENT;
1075 if (nlh->nlmsg_flags & NLM_F_CREATE)
1076 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
Pablo Neira88aa0422005-08-09 20:02:55 -07001077 return err;
1078 }
1079 /* implicit 'else' */
1080
1081 /* we only allow nat config for new conntracks */
1082 if (cda[CTA_NAT-1]) {
1083 err = -EINVAL;
Harald Welte080774a2005-08-09 19:32:58 -07001084 goto out_unlock;
Harald Welte080774a2005-08-09 19:32:58 -07001085 }
1086
1087 /* We manipulate the conntrack inside the global conntrack table lock,
1088 * so there's no need to increase the refcount */
1089 DEBUGP("conntrack found\n");
1090 err = -EEXIST;
1091 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1092 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1093
1094out_unlock:
1095 write_unlock_bh(&ip_conntrack_lock);
1096 return err;
1097}
1098
1099/***********************************************************************
1100 * EXPECT
1101 ***********************************************************************/
1102
1103static inline int
1104ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1105 const struct ip_conntrack_tuple *tuple,
1106 enum ctattr_expect type)
1107{
1108 struct nfattr *nest_parms = NFA_NEST(skb, type);
1109
1110 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1111 goto nfattr_failure;
1112
1113 NFA_NEST_END(skb, nest_parms);
1114
1115 return 0;
1116
1117nfattr_failure:
1118 return -1;
1119}
1120
1121static inline int
1122ctnetlink_exp_dump_expect(struct sk_buff *skb,
1123 const struct ip_conntrack_expect *exp)
1124{
Harald Welte1444fc52005-08-09 20:04:07 -07001125 struct ip_conntrack *master = exp->master;
Harald Welte080774a2005-08-09 19:32:58 -07001126 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1127 u_int32_t id = htonl(exp->id);
Harald Welte080774a2005-08-09 19:32:58 -07001128
1129 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1130 goto nfattr_failure;
1131 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1132 goto nfattr_failure;
Harald Welte1444fc52005-08-09 20:04:07 -07001133 if (ctnetlink_exp_dump_tuple(skb,
1134 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1135 CTA_EXPECT_MASTER) < 0)
1136 goto nfattr_failure;
Harald Welte080774a2005-08-09 19:32:58 -07001137
1138 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1139 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
Harald Welte080774a2005-08-09 19:32:58 -07001140
1141 return 0;
1142
1143nfattr_failure:
1144 return -1;
1145}
1146
1147static int
1148ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1149 int event,
1150 int nowait,
1151 const struct ip_conntrack_expect *exp)
1152{
1153 struct nlmsghdr *nlh;
1154 struct nfgenmsg *nfmsg;
1155 unsigned char *b;
1156
1157 b = skb->tail;
1158
1159 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1160 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1161 nfmsg = NLMSG_DATA(nlh);
1162
1163 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1164 nfmsg->nfgen_family = AF_INET;
1165 nfmsg->version = NFNETLINK_V0;
1166 nfmsg->res_id = 0;
1167
1168 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1169 goto nfattr_failure;
1170
1171 nlh->nlmsg_len = skb->tail - b;
1172 return skb->len;
1173
1174nlmsg_failure:
1175nfattr_failure:
1176 skb_trim(skb, b - skb->data);
1177 return -1;
1178}
1179
1180#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1181static int ctnetlink_expect_event(struct notifier_block *this,
1182 unsigned long events, void *ptr)
1183{
1184 struct nlmsghdr *nlh;
1185 struct nfgenmsg *nfmsg;
1186 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1187 struct sk_buff *skb;
1188 unsigned int type;
1189 unsigned char *b;
1190 int flags = 0;
1191 u16 proto;
1192
1193 if (events & IPEXP_NEW) {
1194 type = IPCTNL_MSG_EXP_NEW;
1195 flags = NLM_F_CREATE|NLM_F_EXCL;
1196 } else
1197 return NOTIFY_DONE;
1198
1199 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1200 if (!skb)
1201 return NOTIFY_DONE;
1202
1203 b = skb->tail;
1204
1205 type |= NFNL_SUBSYS_CTNETLINK << 8;
1206 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1207 nfmsg = NLMSG_DATA(nlh);
1208
1209 nlh->nlmsg_flags = flags;
1210 nfmsg->nfgen_family = AF_INET;
1211 nfmsg->version = NFNETLINK_V0;
1212 nfmsg->res_id = 0;
1213
1214 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1215 goto nfattr_failure;
1216
1217 nlh->nlmsg_len = skb->tail - b;
1218 proto = exp->tuple.dst.protonum;
Patrick McHardyac6d4392005-08-14 19:29:52 -07001219 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
Harald Welte080774a2005-08-09 19:32:58 -07001220 return NOTIFY_DONE;
1221
1222nlmsg_failure:
1223nfattr_failure:
1224 kfree_skb(skb);
1225 return NOTIFY_DONE;
1226}
1227#endif
1228
1229static int
1230ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1231{
1232 struct ip_conntrack_expect *exp = NULL;
1233 struct list_head *i;
1234 u_int32_t *id = (u_int32_t *) &cb->args[0];
1235
1236 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1237
1238 read_lock_bh(&ip_conntrack_lock);
Pablo Neira Ayusoff21d572005-08-09 20:06:42 -07001239 list_for_each_prev(i, &ip_conntrack_expect_list) {
Harald Welte080774a2005-08-09 19:32:58 -07001240 exp = (struct ip_conntrack_expect *) i;
1241 if (exp->id <= *id)
1242 continue;
1243 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1244 cb->nlh->nlmsg_seq,
1245 IPCTNL_MSG_EXP_NEW,
1246 1, exp) < 0)
1247 goto out;
1248 *id = exp->id;
1249 }
1250out:
1251 read_unlock_bh(&ip_conntrack_lock);
1252
1253 DEBUGP("leaving, last id=%llu\n", *id);
1254
1255 return skb->len;
1256}
1257
1258static int
1259ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1260 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1261{
1262 struct ip_conntrack_tuple tuple;
1263 struct ip_conntrack_expect *exp;
1264 struct sk_buff *skb2;
1265 int err = 0;
1266
1267 DEBUGP("entered %s\n", __FUNCTION__);
1268
1269 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1270 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1271 u32 rlen;
1272
1273 if (msg->nfgen_family != AF_INET)
1274 return -EAFNOSUPPORT;
1275
1276 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1277 ctnetlink_exp_dump_table,
1278 ctnetlink_done)) != 0)
1279 return -EINVAL;
1280 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1281 if (rlen > skb->len)
1282 rlen = skb->len;
1283 skb_pull(skb, rlen);
1284 return 0;
1285 }
1286
Harald Welte1444fc52005-08-09 20:04:07 -07001287 if (cda[CTA_EXPECT_MASTER-1])
1288 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
Harald Welte080774a2005-08-09 19:32:58 -07001289 else
1290 return -EINVAL;
1291
1292 if (err < 0)
1293 return err;
1294
Patrick McHardya41bc002005-09-19 15:35:31 -07001295 exp = ip_conntrack_expect_find(&tuple);
Harald Welte080774a2005-08-09 19:32:58 -07001296 if (!exp)
1297 return -ENOENT;
1298
1299 err = -ENOMEM;
1300 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1301 if (!skb2)
1302 goto out;
1303 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1304
1305 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1306 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1307 1, exp);
1308 if (err <= 0)
Harald Welte080774a2005-08-09 19:32:58 -07001309 goto free;
1310
Harald Welte0f81eb42005-11-03 19:05:37 +01001311 ip_conntrack_expect_put(exp);
Harald Welte080774a2005-08-09 19:32:58 -07001312
Harald Welte0f81eb42005-11-03 19:05:37 +01001313 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1314
1315free:
1316 kfree_skb(skb2);
Harald Welte080774a2005-08-09 19:32:58 -07001317out:
1318 ip_conntrack_expect_put(exp);
Harald Welte080774a2005-08-09 19:32:58 -07001319 return err;
1320}
1321
1322static int
1323ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1324 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1325{
1326 struct ip_conntrack_expect *exp, *tmp;
1327 struct ip_conntrack_tuple tuple;
1328 struct ip_conntrack_helper *h;
1329 int err;
1330
Harald Welte1444fc52005-08-09 20:04:07 -07001331 if (cda[CTA_EXPECT_TUPLE-1]) {
1332 /* delete a single expect by tuple */
1333 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1334 if (err < 0)
1335 return err;
1336
1337 /* bump usage count to 2 */
Patrick McHardya41bc002005-09-19 15:35:31 -07001338 exp = ip_conntrack_expect_find(&tuple);
Harald Welte1444fc52005-08-09 20:04:07 -07001339 if (!exp)
1340 return -ENOENT;
1341
1342 if (cda[CTA_EXPECT_ID-1]) {
1343 u_int32_t id =
1344 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1345 if (exp->id != ntohl(id)) {
1346 ip_conntrack_expect_put(exp);
1347 return -ENOENT;
1348 }
1349 }
1350
1351 /* after list removal, usage count == 1 */
1352 ip_conntrack_unexpect_related(exp);
1353 /* have to put what we 'get' above.
1354 * after this line usage count == 0 */
1355 ip_conntrack_expect_put(exp);
1356 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1357 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
Harald Welte080774a2005-08-09 19:32:58 -07001358
1359 /* delete all expectations for this helper */
1360 write_lock_bh(&ip_conntrack_lock);
1361 h = __ip_conntrack_helper_find_byname(name);
1362 if (!h) {
1363 write_unlock_bh(&ip_conntrack_lock);
1364 return -EINVAL;
1365 }
1366 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1367 list) {
1368 if (exp->master->helper == h
Pablo Neira Ayuso49719eb2005-09-06 15:10:46 -07001369 && del_timer(&exp->timeout)) {
1370 ip_ct_unlink_expect(exp);
1371 ip_conntrack_expect_put(exp);
1372 }
Harald Welte080774a2005-08-09 19:32:58 -07001373 }
1374 write_unlock(&ip_conntrack_lock);
Harald Welte080774a2005-08-09 19:32:58 -07001375 } else {
1376 /* This basically means we have to flush everything*/
1377 write_lock_bh(&ip_conntrack_lock);
1378 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1379 list) {
Pablo Neira Ayuso49719eb2005-09-06 15:10:46 -07001380 if (del_timer(&exp->timeout)) {
1381 ip_ct_unlink_expect(exp);
1382 ip_conntrack_expect_put(exp);
1383 }
Harald Welte080774a2005-08-09 19:32:58 -07001384 }
1385 write_unlock_bh(&ip_conntrack_lock);
Harald Welte080774a2005-08-09 19:32:58 -07001386 }
1387
Harald Welte080774a2005-08-09 19:32:58 -07001388 return 0;
1389}
1390static int
1391ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1392{
1393 return -EOPNOTSUPP;
1394}
1395
1396static int
1397ctnetlink_create_expect(struct nfattr *cda[])
1398{
1399 struct ip_conntrack_tuple tuple, mask, master_tuple;
1400 struct ip_conntrack_tuple_hash *h = NULL;
1401 struct ip_conntrack_expect *exp;
1402 struct ip_conntrack *ct;
1403 int err = 0;
1404
1405 DEBUGP("entered %s\n", __FUNCTION__);
1406
Harald Welte1444fc52005-08-09 20:04:07 -07001407 /* caller guarantees that those three CTA_EXPECT_* exist */
Harald Welte080774a2005-08-09 19:32:58 -07001408 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1409 if (err < 0)
1410 return err;
Harald Weltebd9a26b2005-08-09 20:03:22 -07001411 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
Harald Welte080774a2005-08-09 19:32:58 -07001412 if (err < 0)
1413 return err;
Harald Welte1444fc52005-08-09 20:04:07 -07001414 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
Harald Welte080774a2005-08-09 19:32:58 -07001415 if (err < 0)
1416 return err;
1417
1418 /* Look for master conntrack of this expectation */
1419 h = ip_conntrack_find_get(&master_tuple, NULL);
1420 if (!h)
1421 return -ENOENT;
1422 ct = tuplehash_to_ctrack(h);
1423
1424 if (!ct->helper) {
1425 /* such conntrack hasn't got any helper, abort */
1426 err = -EINVAL;
1427 goto out;
1428 }
1429
1430 exp = ip_conntrack_expect_alloc(ct);
1431 if (!exp) {
1432 err = -ENOMEM;
1433 goto out;
1434 }
1435
1436 exp->expectfn = NULL;
Patrick McHardy2248bcf2005-09-06 15:06:42 -07001437 exp->flags = 0;
Harald Welte080774a2005-08-09 19:32:58 -07001438 exp->master = ct;
1439 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1440 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1441
1442 err = ip_conntrack_expect_related(exp);
1443 ip_conntrack_expect_put(exp);
1444
1445out:
1446 ip_conntrack_put(tuplehash_to_ctrack(h));
1447 return err;
1448}
1449
1450static int
1451ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1452 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1453{
1454 struct ip_conntrack_tuple tuple;
1455 struct ip_conntrack_expect *exp;
1456 int err = 0;
1457
1458 DEBUGP("entered %s\n", __FUNCTION__);
1459
Harald Welte1444fc52005-08-09 20:04:07 -07001460 if (!cda[CTA_EXPECT_TUPLE-1]
1461 || !cda[CTA_EXPECT_MASK-1]
1462 || !cda[CTA_EXPECT_MASTER-1])
Harald Welte080774a2005-08-09 19:32:58 -07001463 return -EINVAL;
1464
1465 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1466 if (err < 0)
1467 return err;
1468
1469 write_lock_bh(&ip_conntrack_lock);
1470 exp = __ip_conntrack_expect_find(&tuple);
1471
1472 if (!exp) {
1473 write_unlock_bh(&ip_conntrack_lock);
1474 err = -ENOENT;
1475 if (nlh->nlmsg_flags & NLM_F_CREATE)
1476 err = ctnetlink_create_expect(cda);
1477 return err;
1478 }
1479
1480 err = -EEXIST;
1481 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1482 err = ctnetlink_change_expect(exp, cda);
1483 write_unlock_bh(&ip_conntrack_lock);
1484
1485 DEBUGP("leaving\n");
1486
1487 return err;
1488}
1489
1490#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1491static struct notifier_block ctnl_notifier = {
1492 .notifier_call = ctnetlink_conntrack_event,
1493};
1494
1495static struct notifier_block ctnl_notifier_exp = {
1496 .notifier_call = ctnetlink_expect_event,
1497};
1498#endif
1499
1500static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1501 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001502 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001503 .cap_required = CAP_NET_ADMIN },
1504 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001505 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001506 .cap_required = CAP_NET_ADMIN },
1507 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001508 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001509 .cap_required = CAP_NET_ADMIN },
1510 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001511 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001512 .cap_required = CAP_NET_ADMIN },
1513};
1514
Pablo Neira Ayuso28b19d92005-08-09 20:06:27 -07001515static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
Harald Welte080774a2005-08-09 19:32:58 -07001516 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
Harald Welte927ccbc2005-08-09 20:03:40 -07001517 .attr_count = CTA_EXPECT_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001518 .cap_required = CAP_NET_ADMIN },
1519 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
Harald Welte927ccbc2005-08-09 20:03:40 -07001520 .attr_count = CTA_EXPECT_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001521 .cap_required = CAP_NET_ADMIN },
1522 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
Harald Welte927ccbc2005-08-09 20:03:40 -07001523 .attr_count = CTA_EXPECT_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001524 .cap_required = CAP_NET_ADMIN },
1525};
1526
1527static struct nfnetlink_subsystem ctnl_subsys = {
1528 .name = "conntrack",
1529 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1530 .cb_count = IPCTNL_MSG_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001531 .cb = ctnl_cb,
1532};
1533
1534static struct nfnetlink_subsystem ctnl_exp_subsys = {
1535 .name = "conntrack_expect",
1536 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1537 .cb_count = IPCTNL_MSG_EXP_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001538 .cb = ctnl_exp_cb,
1539};
1540
Pablo Neira Ayuso119a3182005-11-09 13:00:29 -08001541MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1542
Harald Welte080774a2005-08-09 19:32:58 -07001543static int __init ctnetlink_init(void)
1544{
1545 int ret;
1546
1547 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1548 ret = nfnetlink_subsys_register(&ctnl_subsys);
1549 if (ret < 0) {
1550 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1551 goto err_out;
1552 }
1553
1554 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1555 if (ret < 0) {
1556 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1557 goto err_unreg_subsys;
1558 }
1559
1560#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1561 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1562 if (ret < 0) {
1563 printk("ctnetlink_init: cannot register notifier.\n");
1564 goto err_unreg_exp_subsys;
1565 }
1566
1567 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1568 if (ret < 0) {
1569 printk("ctnetlink_init: cannot expect register notifier.\n");
1570 goto err_unreg_notifier;
1571 }
1572#endif
1573
1574 return 0;
1575
1576#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1577err_unreg_notifier:
1578 ip_conntrack_unregister_notifier(&ctnl_notifier);
1579err_unreg_exp_subsys:
1580 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1581#endif
1582err_unreg_subsys:
1583 nfnetlink_subsys_unregister(&ctnl_subsys);
1584err_out:
1585 return ret;
1586}
1587
1588static void __exit ctnetlink_exit(void)
1589{
1590 printk("ctnetlink: unregistering from nfnetlink.\n");
1591
1592#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1593 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1594 ip_conntrack_unregister_notifier(&ctnl_notifier);
1595#endif
1596
1597 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1598 nfnetlink_subsys_unregister(&ctnl_subsys);
1599 return;
1600}
1601
1602module_init(ctnetlink_init);
1603module_exit(ctnetlink_exit);