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