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