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