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