]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/netfilter/ip_conntrack_netlink.c
Merge branch 'upstream'
[net-next-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 #include <linux/rtnetlink.h>
32
33 #include <linux/netfilter.h>
34 #include <linux/netfilter_ipv4.h>
35 #include <linux/netfilter_ipv4/ip_tables.h>
36 #include <linux/netfilter_ipv4/ip_conntrack.h>
37 #include <linux/netfilter_ipv4/ip_conntrack_core.h>
38 #include <linux/netfilter_ipv4/ip_conntrack_helper.h>
39 #include <linux/netfilter_ipv4/ip_conntrack_protocol.h>
40 #include <linux/netfilter_ipv4/ip_nat_protocol.h>
41
42 #include <linux/netfilter/nfnetlink.h>
43 #include <linux/netfilter/nfnetlink_conntrack.h>
44
45 MODULE_LICENSE("GPL");
46
47 static char __initdata version[] = "0.90";
48
49 #if 0
50 #define DEBUGP printk
51 #else
52 #define DEBUGP(format, args...)
53 #endif
54
55
56 static inline int
57 ctnetlink_dump_tuples_proto(struct sk_buff *skb, 
58                             const struct ip_conntrack_tuple *tuple)
59 {
60         struct ip_conntrack_protocol *proto;
61
62         NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
63
64         proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
65         if (proto && proto->tuple_to_nfattr)
66                 return proto->tuple_to_nfattr(skb, tuple);
67
68         return 0;
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_int64_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         
483         if (nfattr_parse_nested(tb, CTA_IP_MAX, attr) < 0)
484                 goto nfattr_failure;
485
486         if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
487                 return -EINVAL;
488
489         if (!tb[CTA_IP_V4_SRC-1])
490                 return -EINVAL;
491         tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
492
493         if (!tb[CTA_IP_V4_DST-1])
494                 return -EINVAL;
495         tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
496
497         DEBUGP("leaving\n");
498
499         return 0;
500
501 nfattr_failure:
502         return -1;
503 }
504
505 static const int cta_min_proto[CTA_PROTO_MAX] = {
506         [CTA_PROTO_NUM-1]       = sizeof(u_int16_t),
507         [CTA_PROTO_SRC_PORT-1]  = sizeof(u_int16_t),
508         [CTA_PROTO_DST_PORT-1]  = sizeof(u_int16_t),
509         [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
510         [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
511         [CTA_PROTO_ICMP_ID-1]   = sizeof(u_int16_t),
512 };
513
514 static inline int
515 ctnetlink_parse_tuple_proto(struct nfattr *attr, 
516                             struct ip_conntrack_tuple *tuple)
517 {
518         struct nfattr *tb[CTA_PROTO_MAX];
519         struct ip_conntrack_protocol *proto;
520         int ret = 0;
521
522         DEBUGP("entered %s\n", __FUNCTION__);
523
524         if (nfattr_parse_nested(tb, CTA_PROTO_MAX, attr) < 0)
525                 goto nfattr_failure;
526
527         if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
528                 return -EINVAL;
529
530         if (!tb[CTA_PROTO_NUM-1])
531                 return -EINVAL;
532         tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
533
534         proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
535
536         if (likely(proto && proto->nfattr_to_tuple)) {
537                 ret = proto->nfattr_to_tuple(tb, tuple);
538                 ip_conntrack_proto_put(proto);
539         }
540         
541         return ret;
542
543 nfattr_failure:
544         return -1;
545 }
546
547 static inline int
548 ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
549                       enum ctattr_tuple type)
550 {
551         struct nfattr *tb[CTA_TUPLE_MAX];
552         int err;
553
554         DEBUGP("entered %s\n", __FUNCTION__);
555
556         memset(tuple, 0, sizeof(*tuple));
557
558         if (nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]) < 0)
559                 goto nfattr_failure;
560
561         if (!tb[CTA_TUPLE_IP-1])
562                 return -EINVAL;
563
564         err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
565         if (err < 0)
566                 return err;
567
568         if (!tb[CTA_TUPLE_PROTO-1])
569                 return -EINVAL;
570
571         err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
572         if (err < 0)
573                 return err;
574
575         /* orig and expect tuples get DIR_ORIGINAL */
576         if (type == CTA_TUPLE_REPLY)
577                 tuple->dst.dir = IP_CT_DIR_REPLY;
578         else
579                 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
580
581         DUMP_TUPLE(tuple);
582
583         DEBUGP("leaving\n");
584
585         return 0;
586
587 nfattr_failure:
588         return -1;
589 }
590
591 #ifdef CONFIG_IP_NF_NAT_NEEDED
592 static const int cta_min_protonat[CTA_PROTONAT_MAX] = {
593         [CTA_PROTONAT_PORT_MIN-1]       = sizeof(u_int16_t),
594         [CTA_PROTONAT_PORT_MAX-1]       = sizeof(u_int16_t),
595 };
596
597 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
598                                      const struct ip_conntrack *ct,
599                                      struct ip_nat_range *range)
600 {
601         struct nfattr *tb[CTA_PROTONAT_MAX];
602         struct ip_nat_protocol *npt;
603
604         DEBUGP("entered %s\n", __FUNCTION__);
605
606         if (nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr) < 0)
607                 goto nfattr_failure;
608
609         if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
610                 goto nfattr_failure;
611
612         npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
613         if (!npt)
614                 return 0;
615
616         if (!npt->nfattr_to_range) {
617                 ip_nat_proto_put(npt);
618                 return 0;
619         }
620
621         /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
622         if (npt->nfattr_to_range(tb, range) > 0)
623                 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
624
625         ip_nat_proto_put(npt);
626
627         DEBUGP("leaving\n");
628         return 0;
629
630 nfattr_failure:
631         return -1;
632 }
633
634 static inline int
635 ctnetlink_parse_nat(struct nfattr *cda[],
636                     const struct ip_conntrack *ct, struct ip_nat_range *range)
637 {
638         struct nfattr *tb[CTA_NAT_MAX];
639         int err;
640
641         DEBUGP("entered %s\n", __FUNCTION__);
642
643         memset(range, 0, sizeof(*range));
644         
645         if (nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]) < 0)
646                 goto nfattr_failure;
647
648         if (tb[CTA_NAT_MINIP-1])
649                 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
650
651         if (!tb[CTA_NAT_MAXIP-1])
652                 range->max_ip = range->min_ip;
653         else
654                 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
655
656         if (range->min_ip)
657                 range->flags |= IP_NAT_RANGE_MAP_IPS;
658
659         if (!tb[CTA_NAT_PROTO-1])
660                 return 0;
661
662         err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
663         if (err < 0)
664                 return err;
665
666         DEBUGP("leaving\n");
667         return 0;
668
669 nfattr_failure:
670         return -1;
671 }
672 #endif
673
674 static inline int
675 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
676 {
677         struct nfattr *tb[CTA_HELP_MAX];
678
679         DEBUGP("entered %s\n", __FUNCTION__);
680
681         if (nfattr_parse_nested(tb, CTA_HELP_MAX, attr) < 0)
682                 goto nfattr_failure;
683
684         if (!tb[CTA_HELP_NAME-1])
685                 return -EINVAL;
686
687         *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
688
689         return 0;
690
691 nfattr_failure:
692         return -1;
693 }
694
695 static int
696 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, 
697                         struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
698 {
699         struct ip_conntrack_tuple_hash *h;
700         struct ip_conntrack_tuple tuple;
701         struct ip_conntrack *ct;
702         int err = 0;
703
704         DEBUGP("entered %s\n", __FUNCTION__);
705
706         if (cda[CTA_TUPLE_ORIG-1])
707                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
708         else if (cda[CTA_TUPLE_REPLY-1])
709                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
710         else {
711                 /* Flush the whole table */
712                 ip_conntrack_flush();
713                 return 0;
714         }
715
716         if (err < 0)
717                 return err;
718
719         h = ip_conntrack_find_get(&tuple, NULL);
720         if (!h) {
721                 DEBUGP("tuple not found in conntrack hash\n");
722                 return -ENOENT;
723         }
724
725         ct = tuplehash_to_ctrack(h);
726         
727         if (cda[CTA_ID-1]) {
728                 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
729                 if (ct->id != id) {
730                         ip_conntrack_put(ct);
731                         return -ENOENT;
732                 }
733         }       
734         if (del_timer(&ct->timeout)) {
735                 ip_conntrack_put(ct);
736                 ct->timeout.function((unsigned long)ct);
737                 return 0;
738         }
739         ip_conntrack_put(ct);
740         DEBUGP("leaving\n");
741
742         return 0;
743 }
744
745 static int
746 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, 
747                         struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
748 {
749         struct ip_conntrack_tuple_hash *h;
750         struct ip_conntrack_tuple tuple;
751         struct ip_conntrack *ct;
752         struct sk_buff *skb2 = NULL;
753         int err = 0;
754
755         DEBUGP("entered %s\n", __FUNCTION__);
756
757         if (nlh->nlmsg_flags & NLM_F_DUMP) {
758                 struct nfgenmsg *msg = NLMSG_DATA(nlh);
759                 u32 rlen;
760
761                 if (msg->nfgen_family != AF_INET)
762                         return -EAFNOSUPPORT;
763
764                 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
765                                         IPCTNL_MSG_CT_GET_CTRZERO) {
766 #ifdef CONFIG_IP_NF_CT_ACCT
767                         if ((*errp = netlink_dump_start(ctnl, skb, nlh,
768                                                 ctnetlink_dump_table_w,
769                                                 ctnetlink_done)) != 0)
770                                 return -EINVAL;
771 #else
772                         return -ENOTSUPP;
773 #endif
774                 } else {
775                         if ((*errp = netlink_dump_start(ctnl, skb, nlh,
776                                                         ctnetlink_dump_table,
777                                                         ctnetlink_done)) != 0)
778                         return -EINVAL;
779                 }
780
781                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
782                 if (rlen > skb->len)
783                         rlen = skb->len;
784                 skb_pull(skb, rlen);
785                 return 0;
786         }
787
788         if (cda[CTA_TUPLE_ORIG-1])
789                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
790         else if (cda[CTA_TUPLE_REPLY-1])
791                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
792         else
793                 return -EINVAL;
794
795         if (err < 0)
796                 return err;
797
798         h = ip_conntrack_find_get(&tuple, NULL);
799         if (!h) {
800                 DEBUGP("tuple not found in conntrack hash");
801                 return -ENOENT;
802         }
803         DEBUGP("tuple found\n");
804         ct = tuplehash_to_ctrack(h);
805
806         err = -ENOMEM;
807         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
808         if (!skb2) {
809                 ip_conntrack_put(ct);
810                 return -ENOMEM;
811         }
812         NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
813
814         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, 
815                                   IPCTNL_MSG_CT_NEW, 1, ct);
816         ip_conntrack_put(ct);
817         if (err <= 0)
818                 goto out;
819
820         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
821         if (err < 0)
822                 goto out;
823
824         DEBUGP("leaving\n");
825         return 0;
826
827 out:
828         if (skb2)
829                 kfree_skb(skb2);
830         return -1;
831 }
832
833 static inline int
834 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
835 {
836         unsigned long d;
837         unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
838         d = ct->status ^ status;
839
840         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
841                 /* unchangeable */
842                 return -EINVAL;
843         
844         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
845                 /* SEEN_REPLY bit can only be set */
846                 return -EINVAL;
847
848         
849         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
850                 /* ASSURED bit can only be set */
851                 return -EINVAL;
852
853         if (cda[CTA_NAT-1]) {
854 #ifndef CONFIG_IP_NF_NAT_NEEDED
855                 return -EINVAL;
856 #else
857                 unsigned int hooknum;
858                 struct ip_nat_range range;
859
860                 if (ctnetlink_parse_nat(cda, ct, &range) < 0)
861                         return -EINVAL;
862
863                 DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n", 
864                        NIPQUAD(range.min_ip), NIPQUAD(range.max_ip),
865                        htons(range.min.all), htons(range.max.all));
866                 
867                 /* This is tricky but it works. ip_nat_setup_info needs the
868                  * hook number as parameter, so let's do the correct 
869                  * conversion and run away */
870                 if (status & IPS_SRC_NAT_DONE)
871                         hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */
872                 else if (status & IPS_DST_NAT_DONE)
873                         hooknum = NF_IP_PRE_ROUTING;  /* IP_NAT_MANIP_DST */
874                 else 
875                         return -EINVAL; /* Missing NAT flags */
876
877                 DEBUGP("NAT status: %lu\n", 
878                        status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
879                 
880                 if (ip_nat_initialized(ct, hooknum))
881                         return -EEXIST;
882                 ip_nat_setup_info(ct, &range, hooknum);
883
884                 DEBUGP("NAT status after setup_info: %lu\n",
885                        ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK));
886 #endif
887         }
888
889         /* Be careful here, modifying NAT bits can screw up things,
890          * so don't let users modify them directly if they don't pass
891          * ip_nat_range. */
892         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
893         return 0;
894 }
895
896
897 static inline int
898 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
899 {
900         struct ip_conntrack_helper *helper;
901         char *helpname;
902         int err;
903
904         DEBUGP("entered %s\n", __FUNCTION__);
905
906         /* don't change helper of sibling connections */
907         if (ct->master)
908                 return -EINVAL;
909
910         err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
911         if (err < 0)
912                 return err;
913
914         helper = __ip_conntrack_helper_find_byname(helpname);
915         if (!helper) {
916                 if (!strcmp(helpname, ""))
917                         helper = NULL;
918                 else
919                         return -EINVAL;
920         }
921
922         if (ct->helper) {
923                 if (!helper) {
924                         /* we had a helper before ... */
925                         ip_ct_remove_expectations(ct);
926                         ct->helper = NULL;
927                 } else {
928                         /* need to zero data of old helper */
929                         memset(&ct->help, 0, sizeof(ct->help));
930                 }
931         }
932         
933         ct->helper = helper;
934
935         return 0;
936 }
937
938 static inline int
939 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
940 {
941         u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
942         
943         if (!del_timer(&ct->timeout))
944                 return -ETIME;
945
946         ct->timeout.expires = jiffies + timeout * HZ;
947         add_timer(&ct->timeout);
948
949         return 0;
950 }
951
952 static inline int
953 ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
954 {
955         struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
956         struct ip_conntrack_protocol *proto;
957         u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
958         int err = 0;
959
960         if (nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr) < 0)
961                 goto nfattr_failure;
962
963         proto = ip_conntrack_proto_find_get(npt);
964         if (!proto)
965                 return -EINVAL;
966
967         if (proto->from_nfattr)
968                 err = proto->from_nfattr(tb, ct);
969         ip_conntrack_proto_put(proto); 
970
971         return err;
972
973 nfattr_failure:
974         return -ENOMEM;
975 }
976
977 static int
978 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
979 {
980         int err;
981
982         DEBUGP("entered %s\n", __FUNCTION__);
983
984         if (cda[CTA_HELP-1]) {
985                 err = ctnetlink_change_helper(ct, cda);
986                 if (err < 0)
987                         return err;
988         }
989
990         if (cda[CTA_TIMEOUT-1]) {
991                 err = ctnetlink_change_timeout(ct, cda);
992                 if (err < 0)
993                         return err;
994         }
995
996         if (cda[CTA_STATUS-1]) {
997                 err = ctnetlink_change_status(ct, cda);
998                 if (err < 0)
999                         return err;
1000         }
1001
1002         if (cda[CTA_PROTOINFO-1]) {
1003                 err = ctnetlink_change_protoinfo(ct, cda);
1004                 if (err < 0)
1005                         return err;
1006         }
1007
1008         DEBUGP("all done\n");
1009         return 0;
1010 }
1011
1012 static int
1013 ctnetlink_create_conntrack(struct nfattr *cda[], 
1014                            struct ip_conntrack_tuple *otuple,
1015                            struct ip_conntrack_tuple *rtuple)
1016 {
1017         struct ip_conntrack *ct;
1018         int err = -EINVAL;
1019
1020         DEBUGP("entered %s\n", __FUNCTION__);
1021
1022         ct = ip_conntrack_alloc(otuple, rtuple);
1023         if (ct == NULL || IS_ERR(ct))
1024                 return -ENOMEM; 
1025
1026         if (!cda[CTA_TIMEOUT-1])
1027                 goto err;
1028         ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1029
1030         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1031         ct->status |= IPS_CONFIRMED;
1032
1033         err = ctnetlink_change_status(ct, cda);
1034         if (err < 0)
1035                 goto err;
1036
1037         if (cda[CTA_PROTOINFO-1]) {
1038                 err = ctnetlink_change_protoinfo(ct, cda);
1039                 if (err < 0)
1040                         return err;
1041         }
1042
1043         ct->helper = ip_conntrack_helper_find_get(rtuple);
1044
1045         add_timer(&ct->timeout);
1046         ip_conntrack_hash_insert(ct);
1047
1048         if (ct->helper)
1049                 ip_conntrack_helper_put(ct->helper);
1050
1051         DEBUGP("conntrack with id %u inserted\n", ct->id);
1052         return 0;
1053
1054 err:    
1055         ip_conntrack_free(ct);
1056         return err;
1057 }
1058
1059 static int 
1060 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, 
1061                         struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1062 {
1063         struct ip_conntrack_tuple otuple, rtuple;
1064         struct ip_conntrack_tuple_hash *h = NULL;
1065         int err = 0;
1066
1067         DEBUGP("entered %s\n", __FUNCTION__);
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         u16 proto;
1208
1209         if (events & IPEXP_NEW) {
1210                 type = IPCTNL_MSG_EXP_NEW;
1211                 flags = NLM_F_CREATE|NLM_F_EXCL;
1212         } else
1213                 return NOTIFY_DONE;
1214
1215         skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1216         if (!skb)
1217                 return NOTIFY_DONE;
1218
1219         b = skb->tail;
1220
1221         type |= NFNL_SUBSYS_CTNETLINK << 8;
1222         nlh   = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1223         nfmsg = NLMSG_DATA(nlh);
1224
1225         nlh->nlmsg_flags    = flags;
1226         nfmsg->nfgen_family = AF_INET;
1227         nfmsg->version      = NFNETLINK_V0;
1228         nfmsg->res_id       = 0;
1229
1230         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1231                 goto nfattr_failure;
1232
1233         nlh->nlmsg_len = skb->tail - b;
1234         proto = exp->tuple.dst.protonum;
1235         nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1236         return NOTIFY_DONE;
1237
1238 nlmsg_failure:
1239 nfattr_failure:
1240         kfree_skb(skb);
1241         return NOTIFY_DONE;
1242 }
1243 #endif
1244
1245 static int
1246 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1247 {
1248         struct ip_conntrack_expect *exp = NULL;
1249         struct list_head *i;
1250         u_int32_t *id = (u_int32_t *) &cb->args[0];
1251
1252         DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1253
1254         read_lock_bh(&ip_conntrack_lock);
1255         list_for_each_prev(i, &ip_conntrack_expect_list) {
1256                 exp = (struct ip_conntrack_expect *) i;
1257                 if (exp->id <= *id)
1258                         continue;
1259                 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1260                                             cb->nlh->nlmsg_seq,
1261                                             IPCTNL_MSG_EXP_NEW,
1262                                             1, exp) < 0)
1263                         goto out;
1264                 *id = exp->id;
1265         }
1266 out:    
1267         read_unlock_bh(&ip_conntrack_lock);
1268
1269         DEBUGP("leaving, last id=%llu\n", *id);
1270
1271         return skb->len;
1272 }
1273
1274 static int
1275 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, 
1276                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1277 {
1278         struct ip_conntrack_tuple tuple;
1279         struct ip_conntrack_expect *exp;
1280         struct sk_buff *skb2;
1281         int err = 0;
1282
1283         DEBUGP("entered %s\n", __FUNCTION__);
1284
1285         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1286                 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1287                 u32 rlen;
1288
1289                 if (msg->nfgen_family != AF_INET)
1290                         return -EAFNOSUPPORT;
1291
1292                 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1293                                                 ctnetlink_exp_dump_table,
1294                                                 ctnetlink_done)) != 0)
1295                         return -EINVAL;
1296                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1297                 if (rlen > skb->len)
1298                         rlen = skb->len;
1299                 skb_pull(skb, rlen);
1300                 return 0;
1301         }
1302
1303         if (cda[CTA_EXPECT_MASTER-1])
1304                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
1305         else
1306                 return -EINVAL;
1307
1308         if (err < 0)
1309                 return err;
1310
1311         exp = ip_conntrack_expect_find(&tuple);
1312         if (!exp)
1313                 return -ENOENT;
1314
1315         err = -ENOMEM;
1316         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1317         if (!skb2)
1318                 goto out;
1319         NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1320         
1321         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, 
1322                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1323                                       1, exp);
1324         if (err <= 0)
1325                 goto out;
1326
1327         ip_conntrack_expect_put(exp);
1328
1329         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1330         if (err < 0)
1331                 goto free;
1332
1333         return err;
1334
1335 out:
1336         ip_conntrack_expect_put(exp);
1337 free:
1338         if (skb2)
1339                 kfree_skb(skb2);
1340         return err;
1341 }
1342
1343 static int
1344 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, 
1345                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1346 {
1347         struct ip_conntrack_expect *exp, *tmp;
1348         struct ip_conntrack_tuple tuple;
1349         struct ip_conntrack_helper *h;
1350         int err;
1351
1352         if (cda[CTA_EXPECT_TUPLE-1]) {
1353                 /* delete a single expect by tuple */
1354                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1355                 if (err < 0)
1356                         return err;
1357
1358                 /* bump usage count to 2 */
1359                 exp = ip_conntrack_expect_find(&tuple);
1360                 if (!exp)
1361                         return -ENOENT;
1362
1363                 if (cda[CTA_EXPECT_ID-1]) {
1364                         u_int32_t id = 
1365                                 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1366                         if (exp->id != ntohl(id)) {
1367                                 ip_conntrack_expect_put(exp);
1368                                 return -ENOENT;
1369                         }
1370                 }
1371
1372                 /* after list removal, usage count == 1 */
1373                 ip_conntrack_unexpect_related(exp);
1374                 /* have to put what we 'get' above. 
1375                  * after this line usage count == 0 */
1376                 ip_conntrack_expect_put(exp);
1377         } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1378                 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1379
1380                 /* delete all expectations for this helper */
1381                 write_lock_bh(&ip_conntrack_lock);
1382                 h = __ip_conntrack_helper_find_byname(name);
1383                 if (!h) {
1384                         write_unlock_bh(&ip_conntrack_lock);
1385                         return -EINVAL;
1386                 }
1387                 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1388                                          list) {
1389                         if (exp->master->helper == h 
1390                             && del_timer(&exp->timeout)) {
1391                                 ip_ct_unlink_expect(exp);
1392                                 ip_conntrack_expect_put(exp);
1393                         }
1394                 }
1395                 write_unlock(&ip_conntrack_lock);
1396         } else {
1397                 /* This basically means we have to flush everything*/
1398                 write_lock_bh(&ip_conntrack_lock);
1399                 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1400                                          list) {
1401                         if (del_timer(&exp->timeout)) {
1402                                 ip_ct_unlink_expect(exp);
1403                                 ip_conntrack_expect_put(exp);
1404                         }
1405                 }
1406                 write_unlock_bh(&ip_conntrack_lock);
1407         }
1408
1409         return 0;
1410 }
1411 static int
1412 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1413 {
1414         return -EOPNOTSUPP;
1415 }
1416
1417 static int
1418 ctnetlink_create_expect(struct nfattr *cda[])
1419 {
1420         struct ip_conntrack_tuple tuple, mask, master_tuple;
1421         struct ip_conntrack_tuple_hash *h = NULL;
1422         struct ip_conntrack_expect *exp;
1423         struct ip_conntrack *ct;
1424         int err = 0;
1425
1426         DEBUGP("entered %s\n", __FUNCTION__);
1427
1428         /* caller guarantees that those three CTA_EXPECT_* exist */
1429         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1430         if (err < 0)
1431                 return err;
1432         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
1433         if (err < 0)
1434                 return err;
1435         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
1436         if (err < 0)
1437                 return err;
1438
1439         /* Look for master conntrack of this expectation */
1440         h = ip_conntrack_find_get(&master_tuple, NULL);
1441         if (!h)
1442                 return -ENOENT;
1443         ct = tuplehash_to_ctrack(h);
1444
1445         if (!ct->helper) {
1446                 /* such conntrack hasn't got any helper, abort */
1447                 err = -EINVAL;
1448                 goto out;
1449         }
1450
1451         exp = ip_conntrack_expect_alloc(ct);
1452         if (!exp) {
1453                 err = -ENOMEM;
1454                 goto out;
1455         }
1456         
1457         exp->expectfn = NULL;
1458         exp->flags = 0;
1459         exp->master = ct;
1460         memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1461         memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1462
1463         err = ip_conntrack_expect_related(exp);
1464         ip_conntrack_expect_put(exp);
1465
1466 out:    
1467         ip_conntrack_put(tuplehash_to_ctrack(h));
1468         return err;
1469 }
1470
1471 static int
1472 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1473                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1474 {
1475         struct ip_conntrack_tuple tuple;
1476         struct ip_conntrack_expect *exp;
1477         int err = 0;
1478
1479         DEBUGP("entered %s\n", __FUNCTION__);   
1480
1481         if (!cda[CTA_EXPECT_TUPLE-1]
1482             || !cda[CTA_EXPECT_MASK-1]
1483             || !cda[CTA_EXPECT_MASTER-1])
1484                 return -EINVAL;
1485
1486         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1487         if (err < 0)
1488                 return err;
1489
1490         write_lock_bh(&ip_conntrack_lock);
1491         exp = __ip_conntrack_expect_find(&tuple);
1492
1493         if (!exp) {
1494                 write_unlock_bh(&ip_conntrack_lock);
1495                 err = -ENOENT;
1496                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1497                         err = ctnetlink_create_expect(cda);
1498                 return err;
1499         }
1500
1501         err = -EEXIST;
1502         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1503                 err = ctnetlink_change_expect(exp, cda);
1504         write_unlock_bh(&ip_conntrack_lock);
1505
1506         DEBUGP("leaving\n");
1507         
1508         return err;
1509 }
1510
1511 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1512 static struct notifier_block ctnl_notifier = {
1513         .notifier_call  = ctnetlink_conntrack_event,
1514 };
1515
1516 static struct notifier_block ctnl_notifier_exp = {
1517         .notifier_call  = ctnetlink_expect_event,
1518 };
1519 #endif
1520
1521 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1522         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
1523                                             .attr_count = CTA_MAX,
1524                                             .cap_required = CAP_NET_ADMIN },
1525         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
1526                                             .attr_count = CTA_MAX,
1527                                             .cap_required = CAP_NET_ADMIN },
1528         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
1529                                             .attr_count = CTA_MAX,
1530                                             .cap_required = CAP_NET_ADMIN },
1531         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
1532                                             .attr_count = CTA_MAX,
1533                                             .cap_required = CAP_NET_ADMIN },
1534 };
1535
1536 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1537         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
1538                                             .attr_count = CTA_EXPECT_MAX,
1539                                             .cap_required = CAP_NET_ADMIN },
1540         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
1541                                             .attr_count = CTA_EXPECT_MAX,
1542                                             .cap_required = CAP_NET_ADMIN },
1543         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
1544                                             .attr_count = CTA_EXPECT_MAX,
1545                                             .cap_required = CAP_NET_ADMIN },
1546 };
1547
1548 static struct nfnetlink_subsystem ctnl_subsys = {
1549         .name                           = "conntrack",
1550         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
1551         .cb_count                       = IPCTNL_MSG_MAX,
1552         .cb                             = ctnl_cb,
1553 };
1554
1555 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1556         .name                           = "conntrack_expect",
1557         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
1558         .cb_count                       = IPCTNL_MSG_EXP_MAX,
1559         .cb                             = ctnl_exp_cb,
1560 };
1561
1562 static int __init ctnetlink_init(void)
1563 {
1564         int ret;
1565
1566         printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1567         ret = nfnetlink_subsys_register(&ctnl_subsys);
1568         if (ret < 0) {
1569                 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1570                 goto err_out;
1571         }
1572
1573         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1574         if (ret < 0) {
1575                 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1576                 goto err_unreg_subsys;
1577         }
1578
1579 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1580         ret = ip_conntrack_register_notifier(&ctnl_notifier);
1581         if (ret < 0) {
1582                 printk("ctnetlink_init: cannot register notifier.\n");
1583                 goto err_unreg_exp_subsys;
1584         }
1585
1586         ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1587         if (ret < 0) {
1588                 printk("ctnetlink_init: cannot expect register notifier.\n");
1589                 goto err_unreg_notifier;
1590         }
1591 #endif
1592
1593         return 0;
1594
1595 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1596 err_unreg_notifier:
1597         ip_conntrack_unregister_notifier(&ctnl_notifier);
1598 err_unreg_exp_subsys:
1599         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1600 #endif
1601 err_unreg_subsys:
1602         nfnetlink_subsys_unregister(&ctnl_subsys);
1603 err_out:
1604         return ret;
1605 }
1606
1607 static void __exit ctnetlink_exit(void)
1608 {
1609         printk("ctnetlink: unregistering from nfnetlink.\n");
1610
1611 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1612         ip_conntrack_unregister_notifier(&ctnl_notifier_exp);
1613         ip_conntrack_unregister_notifier(&ctnl_notifier);
1614 #endif
1615
1616         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1617         nfnetlink_subsys_unregister(&ctnl_subsys);
1618         return;
1619 }
1620
1621 module_init(ctnetlink_init);
1622 module_exit(ctnetlink_exit);