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