blob: 838262e17376dab3be9e3e21237d39e2c42a74b9 [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
Harald Welte080774a2005-08-09 19:32:58 -0700982 DEBUGP("all done\n");
983 return 0;
984}
985
986static int
987ctnetlink_create_conntrack(struct nfattr *cda[],
988 struct ip_conntrack_tuple *otuple,
989 struct ip_conntrack_tuple *rtuple)
990{
991 struct ip_conntrack *ct;
992 int err = -EINVAL;
993
994 DEBUGP("entered %s\n", __FUNCTION__);
995
996 ct = ip_conntrack_alloc(otuple, rtuple);
997 if (ct == NULL || IS_ERR(ct))
998 return -ENOMEM;
999
1000 if (!cda[CTA_TIMEOUT-1])
1001 goto err;
1002 ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1003
1004 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1005 ct->status |= IPS_CONFIRMED;
1006
1007 err = ctnetlink_change_status(ct, cda);
1008 if (err < 0)
1009 goto err;
1010
Pablo Neira Ayuso061cb4a2005-10-10 21:23:46 -07001011 if (cda[CTA_PROTOINFO-1]) {
1012 err = ctnetlink_change_protoinfo(ct, cda);
1013 if (err < 0)
1014 return err;
1015 }
1016
Harald Welte080774a2005-08-09 19:32:58 -07001017 ct->helper = ip_conntrack_helper_find_get(rtuple);
1018
1019 add_timer(&ct->timeout);
1020 ip_conntrack_hash_insert(ct);
1021
1022 if (ct->helper)
1023 ip_conntrack_helper_put(ct->helper);
1024
1025 DEBUGP("conntrack with id %u inserted\n", ct->id);
1026 return 0;
1027
1028err:
1029 ip_conntrack_free(ct);
1030 return err;
1031}
1032
1033static int
1034ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
1035 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1036{
1037 struct ip_conntrack_tuple otuple, rtuple;
1038 struct ip_conntrack_tuple_hash *h = NULL;
1039 int err = 0;
1040
1041 DEBUGP("entered %s\n", __FUNCTION__);
1042
1043 if (cda[CTA_TUPLE_ORIG-1]) {
1044 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1045 if (err < 0)
1046 return err;
1047 }
1048
1049 if (cda[CTA_TUPLE_REPLY-1]) {
1050 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1051 if (err < 0)
1052 return err;
1053 }
1054
1055 write_lock_bh(&ip_conntrack_lock);
1056 if (cda[CTA_TUPLE_ORIG-1])
1057 h = __ip_conntrack_find(&otuple, NULL);
1058 else if (cda[CTA_TUPLE_REPLY-1])
1059 h = __ip_conntrack_find(&rtuple, NULL);
1060
1061 if (h == NULL) {
1062 write_unlock_bh(&ip_conntrack_lock);
1063 DEBUGP("no such conntrack, create new\n");
1064 err = -ENOENT;
1065 if (nlh->nlmsg_flags & NLM_F_CREATE)
1066 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
Pablo Neira88aa0422005-08-09 20:02:55 -07001067 return err;
1068 }
1069 /* implicit 'else' */
1070
1071 /* we only allow nat config for new conntracks */
1072 if (cda[CTA_NAT-1]) {
1073 err = -EINVAL;
Harald Welte080774a2005-08-09 19:32:58 -07001074 goto out_unlock;
Harald Welte080774a2005-08-09 19:32:58 -07001075 }
1076
1077 /* We manipulate the conntrack inside the global conntrack table lock,
1078 * so there's no need to increase the refcount */
1079 DEBUGP("conntrack found\n");
1080 err = -EEXIST;
1081 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1082 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1083
1084out_unlock:
1085 write_unlock_bh(&ip_conntrack_lock);
1086 return err;
1087}
1088
1089/***********************************************************************
1090 * EXPECT
1091 ***********************************************************************/
1092
1093static inline int
1094ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1095 const struct ip_conntrack_tuple *tuple,
1096 enum ctattr_expect type)
1097{
1098 struct nfattr *nest_parms = NFA_NEST(skb, type);
1099
1100 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1101 goto nfattr_failure;
1102
1103 NFA_NEST_END(skb, nest_parms);
1104
1105 return 0;
1106
1107nfattr_failure:
1108 return -1;
1109}
1110
1111static inline int
1112ctnetlink_exp_dump_expect(struct sk_buff *skb,
1113 const struct ip_conntrack_expect *exp)
1114{
Harald Welte1444fc52005-08-09 20:04:07 -07001115 struct ip_conntrack *master = exp->master;
Harald Welte080774a2005-08-09 19:32:58 -07001116 u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1117 u_int32_t id = htonl(exp->id);
Harald Welte080774a2005-08-09 19:32:58 -07001118
1119 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1120 goto nfattr_failure;
1121 if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0)
1122 goto nfattr_failure;
Harald Welte1444fc52005-08-09 20:04:07 -07001123 if (ctnetlink_exp_dump_tuple(skb,
1124 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1125 CTA_EXPECT_MASTER) < 0)
1126 goto nfattr_failure;
Harald Welte080774a2005-08-09 19:32:58 -07001127
1128 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1129 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
Harald Welte080774a2005-08-09 19:32:58 -07001130
1131 return 0;
1132
1133nfattr_failure:
1134 return -1;
1135}
1136
1137static int
1138ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1139 int event,
1140 int nowait,
1141 const struct ip_conntrack_expect *exp)
1142{
1143 struct nlmsghdr *nlh;
1144 struct nfgenmsg *nfmsg;
1145 unsigned char *b;
1146
1147 b = skb->tail;
1148
1149 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1150 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1151 nfmsg = NLMSG_DATA(nlh);
1152
1153 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1154 nfmsg->nfgen_family = AF_INET;
1155 nfmsg->version = NFNETLINK_V0;
1156 nfmsg->res_id = 0;
1157
1158 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1159 goto nfattr_failure;
1160
1161 nlh->nlmsg_len = skb->tail - b;
1162 return skb->len;
1163
1164nlmsg_failure:
1165nfattr_failure:
1166 skb_trim(skb, b - skb->data);
1167 return -1;
1168}
1169
1170#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1171static int ctnetlink_expect_event(struct notifier_block *this,
1172 unsigned long events, void *ptr)
1173{
1174 struct nlmsghdr *nlh;
1175 struct nfgenmsg *nfmsg;
1176 struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1177 struct sk_buff *skb;
1178 unsigned int type;
1179 unsigned char *b;
1180 int flags = 0;
1181 u16 proto;
1182
1183 if (events & IPEXP_NEW) {
1184 type = IPCTNL_MSG_EXP_NEW;
1185 flags = NLM_F_CREATE|NLM_F_EXCL;
1186 } else
1187 return NOTIFY_DONE;
1188
1189 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1190 if (!skb)
1191 return NOTIFY_DONE;
1192
1193 b = skb->tail;
1194
1195 type |= NFNL_SUBSYS_CTNETLINK << 8;
1196 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1197 nfmsg = NLMSG_DATA(nlh);
1198
1199 nlh->nlmsg_flags = flags;
1200 nfmsg->nfgen_family = AF_INET;
1201 nfmsg->version = NFNETLINK_V0;
1202 nfmsg->res_id = 0;
1203
1204 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1205 goto nfattr_failure;
1206
1207 nlh->nlmsg_len = skb->tail - b;
1208 proto = exp->tuple.dst.protonum;
Patrick McHardyac6d4392005-08-14 19:29:52 -07001209 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
Harald Welte080774a2005-08-09 19:32:58 -07001210 return NOTIFY_DONE;
1211
1212nlmsg_failure:
1213nfattr_failure:
1214 kfree_skb(skb);
1215 return NOTIFY_DONE;
1216}
1217#endif
1218
1219static int
1220ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1221{
1222 struct ip_conntrack_expect *exp = NULL;
1223 struct list_head *i;
1224 u_int32_t *id = (u_int32_t *) &cb->args[0];
1225
1226 DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1227
1228 read_lock_bh(&ip_conntrack_lock);
Pablo Neira Ayusoff21d572005-08-09 20:06:42 -07001229 list_for_each_prev(i, &ip_conntrack_expect_list) {
Harald Welte080774a2005-08-09 19:32:58 -07001230 exp = (struct ip_conntrack_expect *) i;
1231 if (exp->id <= *id)
1232 continue;
1233 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1234 cb->nlh->nlmsg_seq,
1235 IPCTNL_MSG_EXP_NEW,
1236 1, exp) < 0)
1237 goto out;
1238 *id = exp->id;
1239 }
1240out:
1241 read_unlock_bh(&ip_conntrack_lock);
1242
1243 DEBUGP("leaving, last id=%llu\n", *id);
1244
1245 return skb->len;
1246}
1247
1248static int
1249ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
1250 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1251{
1252 struct ip_conntrack_tuple tuple;
1253 struct ip_conntrack_expect *exp;
1254 struct sk_buff *skb2;
1255 int err = 0;
1256
1257 DEBUGP("entered %s\n", __FUNCTION__);
1258
1259 if (nlh->nlmsg_flags & NLM_F_DUMP) {
1260 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1261 u32 rlen;
1262
1263 if (msg->nfgen_family != AF_INET)
1264 return -EAFNOSUPPORT;
1265
1266 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1267 ctnetlink_exp_dump_table,
1268 ctnetlink_done)) != 0)
1269 return -EINVAL;
1270 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1271 if (rlen > skb->len)
1272 rlen = skb->len;
1273 skb_pull(skb, rlen);
1274 return 0;
1275 }
1276
Harald Welte1444fc52005-08-09 20:04:07 -07001277 if (cda[CTA_EXPECT_MASTER-1])
1278 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
Harald Welte080774a2005-08-09 19:32:58 -07001279 else
1280 return -EINVAL;
1281
1282 if (err < 0)
1283 return err;
1284
Patrick McHardya41bc002005-09-19 15:35:31 -07001285 exp = ip_conntrack_expect_find(&tuple);
Harald Welte080774a2005-08-09 19:32:58 -07001286 if (!exp)
1287 return -ENOENT;
1288
1289 err = -ENOMEM;
1290 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1291 if (!skb2)
1292 goto out;
1293 NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1294
1295 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
1296 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1297 1, exp);
1298 if (err <= 0)
Harald Welte080774a2005-08-09 19:32:58 -07001299 goto free;
1300
Harald Welte0f81eb42005-11-03 19:05:37 +01001301 ip_conntrack_expect_put(exp);
Harald Welte080774a2005-08-09 19:32:58 -07001302
Harald Welte0f81eb42005-11-03 19:05:37 +01001303 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1304
1305free:
1306 kfree_skb(skb2);
Harald Welte080774a2005-08-09 19:32:58 -07001307out:
1308 ip_conntrack_expect_put(exp);
Harald Welte080774a2005-08-09 19:32:58 -07001309 return err;
1310}
1311
1312static int
1313ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
1314 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1315{
1316 struct ip_conntrack_expect *exp, *tmp;
1317 struct ip_conntrack_tuple tuple;
1318 struct ip_conntrack_helper *h;
1319 int err;
1320
Harald Welte1444fc52005-08-09 20:04:07 -07001321 if (cda[CTA_EXPECT_TUPLE-1]) {
1322 /* delete a single expect by tuple */
1323 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1324 if (err < 0)
1325 return err;
1326
1327 /* bump usage count to 2 */
Patrick McHardya41bc002005-09-19 15:35:31 -07001328 exp = ip_conntrack_expect_find(&tuple);
Harald Welte1444fc52005-08-09 20:04:07 -07001329 if (!exp)
1330 return -ENOENT;
1331
1332 if (cda[CTA_EXPECT_ID-1]) {
1333 u_int32_t id =
1334 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1335 if (exp->id != ntohl(id)) {
1336 ip_conntrack_expect_put(exp);
1337 return -ENOENT;
1338 }
1339 }
1340
1341 /* after list removal, usage count == 1 */
1342 ip_conntrack_unexpect_related(exp);
1343 /* have to put what we 'get' above.
1344 * after this line usage count == 0 */
1345 ip_conntrack_expect_put(exp);
1346 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1347 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
Harald Welte080774a2005-08-09 19:32:58 -07001348
1349 /* delete all expectations for this helper */
1350 write_lock_bh(&ip_conntrack_lock);
1351 h = __ip_conntrack_helper_find_byname(name);
1352 if (!h) {
1353 write_unlock_bh(&ip_conntrack_lock);
1354 return -EINVAL;
1355 }
1356 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1357 list) {
1358 if (exp->master->helper == h
Pablo Neira Ayuso49719eb2005-09-06 15:10:46 -07001359 && del_timer(&exp->timeout)) {
1360 ip_ct_unlink_expect(exp);
1361 ip_conntrack_expect_put(exp);
1362 }
Harald Welte080774a2005-08-09 19:32:58 -07001363 }
1364 write_unlock(&ip_conntrack_lock);
Harald Welte080774a2005-08-09 19:32:58 -07001365 } else {
1366 /* This basically means we have to flush everything*/
1367 write_lock_bh(&ip_conntrack_lock);
1368 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1369 list) {
Pablo Neira Ayuso49719eb2005-09-06 15:10:46 -07001370 if (del_timer(&exp->timeout)) {
1371 ip_ct_unlink_expect(exp);
1372 ip_conntrack_expect_put(exp);
1373 }
Harald Welte080774a2005-08-09 19:32:58 -07001374 }
1375 write_unlock_bh(&ip_conntrack_lock);
Harald Welte080774a2005-08-09 19:32:58 -07001376 }
1377
Harald Welte080774a2005-08-09 19:32:58 -07001378 return 0;
1379}
1380static int
1381ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1382{
1383 return -EOPNOTSUPP;
1384}
1385
1386static int
1387ctnetlink_create_expect(struct nfattr *cda[])
1388{
1389 struct ip_conntrack_tuple tuple, mask, master_tuple;
1390 struct ip_conntrack_tuple_hash *h = NULL;
1391 struct ip_conntrack_expect *exp;
1392 struct ip_conntrack *ct;
1393 int err = 0;
1394
1395 DEBUGP("entered %s\n", __FUNCTION__);
1396
Harald Welte1444fc52005-08-09 20:04:07 -07001397 /* caller guarantees that those three CTA_EXPECT_* exist */
Harald Welte080774a2005-08-09 19:32:58 -07001398 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1399 if (err < 0)
1400 return err;
Harald Weltebd9a26b2005-08-09 20:03:22 -07001401 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
Harald Welte080774a2005-08-09 19:32:58 -07001402 if (err < 0)
1403 return err;
Harald Welte1444fc52005-08-09 20:04:07 -07001404 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
Harald Welte080774a2005-08-09 19:32:58 -07001405 if (err < 0)
1406 return err;
1407
1408 /* Look for master conntrack of this expectation */
1409 h = ip_conntrack_find_get(&master_tuple, NULL);
1410 if (!h)
1411 return -ENOENT;
1412 ct = tuplehash_to_ctrack(h);
1413
1414 if (!ct->helper) {
1415 /* such conntrack hasn't got any helper, abort */
1416 err = -EINVAL;
1417 goto out;
1418 }
1419
1420 exp = ip_conntrack_expect_alloc(ct);
1421 if (!exp) {
1422 err = -ENOMEM;
1423 goto out;
1424 }
1425
1426 exp->expectfn = NULL;
Patrick McHardy2248bcf2005-09-06 15:06:42 -07001427 exp->flags = 0;
Harald Welte080774a2005-08-09 19:32:58 -07001428 exp->master = ct;
1429 memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1430 memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1431
1432 err = ip_conntrack_expect_related(exp);
1433 ip_conntrack_expect_put(exp);
1434
1435out:
1436 ip_conntrack_put(tuplehash_to_ctrack(h));
1437 return err;
1438}
1439
1440static int
1441ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1442 struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1443{
1444 struct ip_conntrack_tuple tuple;
1445 struct ip_conntrack_expect *exp;
1446 int err = 0;
1447
1448 DEBUGP("entered %s\n", __FUNCTION__);
1449
Harald Welte1444fc52005-08-09 20:04:07 -07001450 if (!cda[CTA_EXPECT_TUPLE-1]
1451 || !cda[CTA_EXPECT_MASK-1]
1452 || !cda[CTA_EXPECT_MASTER-1])
Harald Welte080774a2005-08-09 19:32:58 -07001453 return -EINVAL;
1454
1455 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1456 if (err < 0)
1457 return err;
1458
1459 write_lock_bh(&ip_conntrack_lock);
1460 exp = __ip_conntrack_expect_find(&tuple);
1461
1462 if (!exp) {
1463 write_unlock_bh(&ip_conntrack_lock);
1464 err = -ENOENT;
1465 if (nlh->nlmsg_flags & NLM_F_CREATE)
1466 err = ctnetlink_create_expect(cda);
1467 return err;
1468 }
1469
1470 err = -EEXIST;
1471 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1472 err = ctnetlink_change_expect(exp, cda);
1473 write_unlock_bh(&ip_conntrack_lock);
1474
1475 DEBUGP("leaving\n");
1476
1477 return err;
1478}
1479
1480#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1481static struct notifier_block ctnl_notifier = {
1482 .notifier_call = ctnetlink_conntrack_event,
1483};
1484
1485static struct notifier_block ctnl_notifier_exp = {
1486 .notifier_call = ctnetlink_expect_event,
1487};
1488#endif
1489
1490static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1491 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_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] = { .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 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001498 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001499 .cap_required = CAP_NET_ADMIN },
1500 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
Harald Welte927ccbc2005-08-09 20:03:40 -07001501 .attr_count = CTA_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001502 .cap_required = CAP_NET_ADMIN },
1503};
1504
Pablo Neira Ayuso28b19d92005-08-09 20:06:27 -07001505static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
Harald Welte080774a2005-08-09 19:32:58 -07001506 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_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 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
Harald Welte927ccbc2005-08-09 20:03:40 -07001510 .attr_count = CTA_EXPECT_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001511 .cap_required = CAP_NET_ADMIN },
1512 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
Harald Welte927ccbc2005-08-09 20:03:40 -07001513 .attr_count = CTA_EXPECT_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001514 .cap_required = CAP_NET_ADMIN },
1515};
1516
1517static struct nfnetlink_subsystem ctnl_subsys = {
1518 .name = "conntrack",
1519 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1520 .cb_count = IPCTNL_MSG_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001521 .cb = ctnl_cb,
1522};
1523
1524static struct nfnetlink_subsystem ctnl_exp_subsys = {
1525 .name = "conntrack_expect",
1526 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1527 .cb_count = IPCTNL_MSG_EXP_MAX,
Harald Welte080774a2005-08-09 19:32:58 -07001528 .cb = ctnl_exp_cb,
1529};
1530
1531static int __init ctnetlink_init(void)
1532{
1533 int ret;
1534
1535 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1536 ret = nfnetlink_subsys_register(&ctnl_subsys);
1537 if (ret < 0) {
1538 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1539 goto err_out;
1540 }
1541
1542 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1543 if (ret < 0) {
1544 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1545 goto err_unreg_subsys;
1546 }
1547
1548#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1549 ret = ip_conntrack_register_notifier(&ctnl_notifier);
1550 if (ret < 0) {
1551 printk("ctnetlink_init: cannot register notifier.\n");
1552 goto err_unreg_exp_subsys;
1553 }
1554
1555 ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1556 if (ret < 0) {
1557 printk("ctnetlink_init: cannot expect register notifier.\n");
1558 goto err_unreg_notifier;
1559 }
1560#endif
1561
1562 return 0;
1563
1564#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1565err_unreg_notifier:
1566 ip_conntrack_unregister_notifier(&ctnl_notifier);
1567err_unreg_exp_subsys:
1568 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1569#endif
1570err_unreg_subsys:
1571 nfnetlink_subsys_unregister(&ctnl_subsys);
1572err_out:
1573 return ret;
1574}
1575
1576static void __exit ctnetlink_exit(void)
1577{
1578 printk("ctnetlink: unregistering from nfnetlink.\n");
1579
1580#ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1581 ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1582 ip_conntrack_unregister_notifier(&ctnl_notifier);
1583#endif
1584
1585 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1586 nfnetlink_subsys_unregister(&ctnl_subsys);
1587 return;
1588}
1589
1590module_init(ctnetlink_init);
1591module_exit(ctnetlink_exit);