[NET]: Conversions from kmalloc+memset to k(z|c)alloc.
[safe/jmp/linux-2.6] / net / ipv4 / netfilter / ip_conntrack_netlink.c
1 /* Connection tracking via netlink socket. Allows for user space
2  * protocol helpers and general trouble making from userspace.
3  *
4  * (C) 2001 by Jay Schulist <jschlst@samba.org>
5  * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
6  * (C) 2003 by Patrick Mchardy <kaber@trash.net>
7  * (C) 2005-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         for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++) {
419 restart:
420                 last = (struct ip_conntrack *)cb->args[1];
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 (last != NULL) {
427                                 if (ct == last) {
428                                         ip_conntrack_put(last);
429                                         cb->args[1] = 0;
430                                         last = NULL;
431                                 } else
432                                         continue;
433                         }
434                         if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
435                                                 cb->nlh->nlmsg_seq,
436                                                 IPCTNL_MSG_CT_NEW,
437                                                 1, ct) < 0) {
438                                 nf_conntrack_get(&ct->ct_general);
439                                 cb->args[1] = (unsigned long)ct;
440                                 goto out;
441                         }
442                 }
443                 if (last != NULL) {
444                         ip_conntrack_put(last);
445                         cb->args[1] = 0;
446                         goto restart;
447                 }
448         }
449 out:
450         read_unlock_bh(&ip_conntrack_lock);
451
452         DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
453
454         return skb->len;
455 }
456
457 #ifdef CONFIG_IP_NF_CT_ACCT
458 static int
459 ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb)
460 {
461         struct ip_conntrack *ct = NULL;
462         struct ip_conntrack_tuple_hash *h;
463         struct list_head *i;
464         u_int32_t *id = (u_int32_t *) &cb->args[1];
465
466         DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__, 
467                         cb->args[0], *id);
468
469         write_lock_bh(&ip_conntrack_lock);
470         for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) {
471                 list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) {
472                         h = (struct ip_conntrack_tuple_hash *) i;
473                         if (DIRECTION(h) != IP_CT_DIR_ORIGINAL)
474                                 continue;
475                         ct = tuplehash_to_ctrack(h);
476                         if (ct->id <= *id)
477                                 continue;
478                         if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
479                                                 cb->nlh->nlmsg_seq,
480                                                 IPCTNL_MSG_CT_NEW,
481                                                 1, ct) < 0)
482                                 goto out;
483                         *id = ct->id;
484
485                         memset(&ct->counters, 0, sizeof(ct->counters));
486                 }
487         }
488 out:    
489         write_unlock_bh(&ip_conntrack_lock);
490
491         DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id);
492
493         return skb->len;
494 }
495 #endif
496
497 static const size_t cta_min_ip[CTA_IP_MAX] = {
498         [CTA_IP_V4_SRC-1]       = sizeof(u_int32_t),
499         [CTA_IP_V4_DST-1]       = sizeof(u_int32_t),
500 };
501
502 static inline int
503 ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple)
504 {
505         struct nfattr *tb[CTA_IP_MAX];
506
507         DEBUGP("entered %s\n", __FUNCTION__);
508
509         nfattr_parse_nested(tb, CTA_IP_MAX, attr);
510
511         if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
512                 return -EINVAL;
513
514         if (!tb[CTA_IP_V4_SRC-1])
515                 return -EINVAL;
516         tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]);
517
518         if (!tb[CTA_IP_V4_DST-1])
519                 return -EINVAL;
520         tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]);
521
522         DEBUGP("leaving\n");
523
524         return 0;
525 }
526
527 static const size_t cta_min_proto[CTA_PROTO_MAX] = {
528         [CTA_PROTO_NUM-1]       = sizeof(u_int8_t),
529         [CTA_PROTO_SRC_PORT-1]  = sizeof(u_int16_t),
530         [CTA_PROTO_DST_PORT-1]  = sizeof(u_int16_t),
531         [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t),
532         [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t),
533         [CTA_PROTO_ICMP_ID-1]   = sizeof(u_int16_t),
534 };
535
536 static inline int
537 ctnetlink_parse_tuple_proto(struct nfattr *attr, 
538                             struct ip_conntrack_tuple *tuple)
539 {
540         struct nfattr *tb[CTA_PROTO_MAX];
541         struct ip_conntrack_protocol *proto;
542         int ret = 0;
543
544         DEBUGP("entered %s\n", __FUNCTION__);
545
546         nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
547
548         if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
549                 return -EINVAL;
550
551         if (!tb[CTA_PROTO_NUM-1])
552                 return -EINVAL;
553         tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
554
555         proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
556
557         if (likely(proto->nfattr_to_tuple))
558                 ret = proto->nfattr_to_tuple(tb, tuple);
559         
560         ip_conntrack_proto_put(proto);
561         
562         return ret;
563 }
564
565 static inline int
566 ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple,
567                       enum ctattr_tuple type)
568 {
569         struct nfattr *tb[CTA_TUPLE_MAX];
570         int err;
571
572         DEBUGP("entered %s\n", __FUNCTION__);
573
574         memset(tuple, 0, sizeof(*tuple));
575
576         nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
577
578         if (!tb[CTA_TUPLE_IP-1])
579                 return -EINVAL;
580
581         err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
582         if (err < 0)
583                 return err;
584
585         if (!tb[CTA_TUPLE_PROTO-1])
586                 return -EINVAL;
587
588         err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
589         if (err < 0)
590                 return err;
591
592         /* orig and expect tuples get DIR_ORIGINAL */
593         if (type == CTA_TUPLE_REPLY)
594                 tuple->dst.dir = IP_CT_DIR_REPLY;
595         else
596                 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
597
598         DUMP_TUPLE(tuple);
599
600         DEBUGP("leaving\n");
601
602         return 0;
603 }
604
605 #ifdef CONFIG_IP_NF_NAT_NEEDED
606 static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
607         [CTA_PROTONAT_PORT_MIN-1]       = sizeof(u_int16_t),
608         [CTA_PROTONAT_PORT_MAX-1]       = sizeof(u_int16_t),
609 };
610
611 static int ctnetlink_parse_nat_proto(struct nfattr *attr,
612                                      const struct ip_conntrack *ct,
613                                      struct ip_nat_range *range)
614 {
615         struct nfattr *tb[CTA_PROTONAT_MAX];
616         struct ip_nat_protocol *npt;
617
618         DEBUGP("entered %s\n", __FUNCTION__);
619
620         nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
621
622         if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
623                 return -EINVAL;
624
625         npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
626
627         if (!npt->nfattr_to_range) {
628                 ip_nat_proto_put(npt);
629                 return 0;
630         }
631
632         /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
633         if (npt->nfattr_to_range(tb, range) > 0)
634                 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
635
636         ip_nat_proto_put(npt);
637
638         DEBUGP("leaving\n");
639         return 0;
640 }
641
642 static const size_t cta_min_nat[CTA_NAT_MAX] = {
643         [CTA_NAT_MINIP-1]       = sizeof(u_int32_t),
644         [CTA_NAT_MAXIP-1]       = sizeof(u_int32_t),
645 };
646
647 static inline int
648 ctnetlink_parse_nat(struct nfattr *nat,
649                     const struct ip_conntrack *ct, struct ip_nat_range *range)
650 {
651         struct nfattr *tb[CTA_NAT_MAX];
652         int err;
653
654         DEBUGP("entered %s\n", __FUNCTION__);
655
656         memset(range, 0, sizeof(*range));
657         
658         nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
659
660         if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
661                 return -EINVAL;
662
663         if (tb[CTA_NAT_MINIP-1])
664                 range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
665
666         if (!tb[CTA_NAT_MAXIP-1])
667                 range->max_ip = range->min_ip;
668         else
669                 range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
670
671         if (range->min_ip)
672                 range->flags |= IP_NAT_RANGE_MAP_IPS;
673
674         if (!tb[CTA_NAT_PROTO-1])
675                 return 0;
676
677         err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
678         if (err < 0)
679                 return err;
680
681         DEBUGP("leaving\n");
682         return 0;
683 }
684 #endif
685
686 static inline int
687 ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
688 {
689         struct nfattr *tb[CTA_HELP_MAX];
690
691         DEBUGP("entered %s\n", __FUNCTION__);
692
693         nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
694
695         if (!tb[CTA_HELP_NAME-1])
696                 return -EINVAL;
697
698         *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
699
700         return 0;
701 }
702
703 static const size_t cta_min[CTA_MAX] = {
704         [CTA_STATUS-1]          = sizeof(u_int32_t),
705         [CTA_TIMEOUT-1]         = sizeof(u_int32_t),
706         [CTA_MARK-1]            = sizeof(u_int32_t),
707         [CTA_USE-1]             = sizeof(u_int32_t),
708         [CTA_ID-1]              = sizeof(u_int32_t)
709 };
710
711 static int
712 ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, 
713                         struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
714 {
715         struct ip_conntrack_tuple_hash *h;
716         struct ip_conntrack_tuple tuple;
717         struct ip_conntrack *ct;
718         int err = 0;
719
720         DEBUGP("entered %s\n", __FUNCTION__);
721
722         if (nfattr_bad_size(cda, CTA_MAX, cta_min))
723                 return -EINVAL;
724
725         if (cda[CTA_TUPLE_ORIG-1])
726                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
727         else if (cda[CTA_TUPLE_REPLY-1])
728                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
729         else {
730                 /* Flush the whole table */
731                 ip_conntrack_flush();
732                 return 0;
733         }
734
735         if (err < 0)
736                 return err;
737
738         h = ip_conntrack_find_get(&tuple, NULL);
739         if (!h) {
740                 DEBUGP("tuple not found in conntrack hash\n");
741                 return -ENOENT;
742         }
743
744         ct = tuplehash_to_ctrack(h);
745         
746         if (cda[CTA_ID-1]) {
747                 u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1]));
748                 if (ct->id != id) {
749                         ip_conntrack_put(ct);
750                         return -ENOENT;
751                 }
752         }       
753         if (del_timer(&ct->timeout))
754                 ct->timeout.function((unsigned long)ct);
755
756         ip_conntrack_put(ct);
757         DEBUGP("leaving\n");
758
759         return 0;
760 }
761
762 static int
763 ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, 
764                         struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
765 {
766         struct ip_conntrack_tuple_hash *h;
767         struct ip_conntrack_tuple tuple;
768         struct ip_conntrack *ct;
769         struct sk_buff *skb2 = NULL;
770         int err = 0;
771
772         DEBUGP("entered %s\n", __FUNCTION__);
773
774         if (nlh->nlmsg_flags & NLM_F_DUMP) {
775                 struct nfgenmsg *msg = NLMSG_DATA(nlh);
776                 u32 rlen;
777
778                 if (msg->nfgen_family != AF_INET)
779                         return -EAFNOSUPPORT;
780
781                 if (NFNL_MSG_TYPE(nlh->nlmsg_type) ==
782                                         IPCTNL_MSG_CT_GET_CTRZERO) {
783 #ifdef CONFIG_IP_NF_CT_ACCT
784                         if ((*errp = netlink_dump_start(ctnl, skb, nlh,
785                                                 ctnetlink_dump_table_w,
786                                                 ctnetlink_done)) != 0)
787                                 return -EINVAL;
788 #else
789                         return -ENOTSUPP;
790 #endif
791                 } else {
792                         if ((*errp = netlink_dump_start(ctnl, skb, nlh,
793                                                         ctnetlink_dump_table,
794                                                         ctnetlink_done)) != 0)
795                         return -EINVAL;
796                 }
797
798                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
799                 if (rlen > skb->len)
800                         rlen = skb->len;
801                 skb_pull(skb, rlen);
802                 return 0;
803         }
804
805         if (nfattr_bad_size(cda, CTA_MAX, cta_min))
806                 return -EINVAL;
807
808         if (cda[CTA_TUPLE_ORIG-1])
809                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG);
810         else if (cda[CTA_TUPLE_REPLY-1])
811                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY);
812         else
813                 return -EINVAL;
814
815         if (err < 0)
816                 return err;
817
818         h = ip_conntrack_find_get(&tuple, NULL);
819         if (!h) {
820                 DEBUGP("tuple not found in conntrack hash");
821                 return -ENOENT;
822         }
823         DEBUGP("tuple found\n");
824         ct = tuplehash_to_ctrack(h);
825
826         err = -ENOMEM;
827         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
828         if (!skb2) {
829                 ip_conntrack_put(ct);
830                 return -ENOMEM;
831         }
832         NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
833
834         err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, 
835                                   IPCTNL_MSG_CT_NEW, 1, ct);
836         ip_conntrack_put(ct);
837         if (err <= 0)
838                 goto free;
839
840         err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
841         if (err < 0)
842                 goto out;
843
844         DEBUGP("leaving\n");
845         return 0;
846
847 free:
848         kfree_skb(skb2);
849 out:
850         return err;
851 }
852
853 static inline int
854 ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[])
855 {
856         unsigned long d;
857         unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1]));
858         d = ct->status ^ status;
859
860         if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
861                 /* unchangeable */
862                 return -EINVAL;
863         
864         if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
865                 /* SEEN_REPLY bit can only be set */
866                 return -EINVAL;
867
868         
869         if (d & IPS_ASSURED && !(status & IPS_ASSURED))
870                 /* ASSURED bit can only be set */
871                 return -EINVAL;
872
873         if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
874 #ifndef CONFIG_IP_NF_NAT_NEEDED
875                 return -EINVAL;
876 #else
877                 struct ip_nat_range range;
878
879                 if (cda[CTA_NAT_DST-1]) {
880                         if (ctnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
881                                                 &range) < 0)
882                                 return -EINVAL;
883                         if (ip_nat_initialized(ct,
884                                                HOOK2MANIP(NF_IP_PRE_ROUTING)))
885                                 return -EEXIST;
886                         ip_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
887                 }
888                 if (cda[CTA_NAT_SRC-1]) {
889                         if (ctnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
890                                                 &range) < 0)
891                                 return -EINVAL;
892                         if (ip_nat_initialized(ct,
893                                                HOOK2MANIP(NF_IP_POST_ROUTING)))
894                                 return -EEXIST;
895                         ip_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
896                 }
897 #endif
898         }
899
900         /* Be careful here, modifying NAT bits can screw up things,
901          * so don't let users modify them directly if they don't pass
902          * ip_nat_range. */
903         ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
904         return 0;
905 }
906
907
908 static inline int
909 ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[])
910 {
911         struct ip_conntrack_helper *helper;
912         char *helpname;
913         int err;
914
915         DEBUGP("entered %s\n", __FUNCTION__);
916
917         /* don't change helper of sibling connections */
918         if (ct->master)
919                 return -EINVAL;
920
921         err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
922         if (err < 0)
923                 return err;
924
925         helper = __ip_conntrack_helper_find_byname(helpname);
926         if (!helper) {
927                 if (!strcmp(helpname, ""))
928                         helper = NULL;
929                 else
930                         return -EINVAL;
931         }
932
933         if (ct->helper) {
934                 if (!helper) {
935                         /* we had a helper before ... */
936                         ip_ct_remove_expectations(ct);
937                         ct->helper = NULL;
938                 } else {
939                         /* need to zero data of old helper */
940                         memset(&ct->help, 0, sizeof(ct->help));
941                 }
942         }
943         
944         ct->helper = helper;
945
946         return 0;
947 }
948
949 static inline int
950 ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[])
951 {
952         u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
953         
954         if (!del_timer(&ct->timeout))
955                 return -ETIME;
956
957         ct->timeout.expires = jiffies + timeout * HZ;
958         add_timer(&ct->timeout);
959
960         return 0;
961 }
962
963 static inline int
964 ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[])
965 {
966         struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
967         struct ip_conntrack_protocol *proto;
968         u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
969         int err = 0;
970
971         nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
972
973         proto = ip_conntrack_proto_find_get(npt);
974
975         if (proto->from_nfattr)
976                 err = proto->from_nfattr(tb, ct);
977         ip_conntrack_proto_put(proto); 
978
979         return err;
980 }
981
982 static int
983 ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[])
984 {
985         int err;
986
987         DEBUGP("entered %s\n", __FUNCTION__);
988
989         if (cda[CTA_HELP-1]) {
990                 err = ctnetlink_change_helper(ct, cda);
991                 if (err < 0)
992                         return err;
993         }
994
995         if (cda[CTA_TIMEOUT-1]) {
996                 err = ctnetlink_change_timeout(ct, cda);
997                 if (err < 0)
998                         return err;
999         }
1000
1001         if (cda[CTA_STATUS-1]) {
1002                 err = ctnetlink_change_status(ct, cda);
1003                 if (err < 0)
1004                         return err;
1005         }
1006
1007         if (cda[CTA_PROTOINFO-1]) {
1008                 err = ctnetlink_change_protoinfo(ct, cda);
1009                 if (err < 0)
1010                         return err;
1011         }
1012
1013 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1014         if (cda[CTA_MARK-1])
1015                 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1016 #endif
1017
1018         DEBUGP("all done\n");
1019         return 0;
1020 }
1021
1022 static int
1023 ctnetlink_create_conntrack(struct nfattr *cda[], 
1024                            struct ip_conntrack_tuple *otuple,
1025                            struct ip_conntrack_tuple *rtuple)
1026 {
1027         struct ip_conntrack *ct;
1028         int err = -EINVAL;
1029
1030         DEBUGP("entered %s\n", __FUNCTION__);
1031
1032         ct = ip_conntrack_alloc(otuple, rtuple);
1033         if (ct == NULL || IS_ERR(ct))
1034                 return -ENOMEM; 
1035
1036         if (!cda[CTA_TIMEOUT-1])
1037                 goto err;
1038         ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1]));
1039
1040         ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
1041         ct->status |= IPS_CONFIRMED;
1042
1043         err = ctnetlink_change_status(ct, cda);
1044         if (err < 0)
1045                 goto err;
1046
1047         if (cda[CTA_PROTOINFO-1]) {
1048                 err = ctnetlink_change_protoinfo(ct, cda);
1049                 if (err < 0)
1050                         return err;
1051         }
1052
1053 #if defined(CONFIG_IP_NF_CONNTRACK_MARK)
1054         if (cda[CTA_MARK-1])
1055                 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
1056 #endif
1057
1058         ct->helper = ip_conntrack_helper_find_get(rtuple);
1059
1060         add_timer(&ct->timeout);
1061         ip_conntrack_hash_insert(ct);
1062
1063         if (ct->helper)
1064                 ip_conntrack_helper_put(ct->helper);
1065
1066         DEBUGP("conntrack with id %u inserted\n", ct->id);
1067         return 0;
1068
1069 err:    
1070         ip_conntrack_free(ct);
1071         return err;
1072 }
1073
1074 static int 
1075 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, 
1076                         struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1077 {
1078         struct ip_conntrack_tuple otuple, rtuple;
1079         struct ip_conntrack_tuple_hash *h = NULL;
1080         int err = 0;
1081
1082         DEBUGP("entered %s\n", __FUNCTION__);
1083
1084         if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1085                 return -EINVAL;
1086
1087         if (cda[CTA_TUPLE_ORIG-1]) {
1088                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG);
1089                 if (err < 0)
1090                         return err;
1091         }
1092
1093         if (cda[CTA_TUPLE_REPLY-1]) {
1094                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY);
1095                 if (err < 0)
1096                         return err;
1097         }
1098
1099         write_lock_bh(&ip_conntrack_lock);
1100         if (cda[CTA_TUPLE_ORIG-1])
1101                 h = __ip_conntrack_find(&otuple, NULL);
1102         else if (cda[CTA_TUPLE_REPLY-1])
1103                 h = __ip_conntrack_find(&rtuple, NULL);
1104
1105         if (h == NULL) {
1106                 write_unlock_bh(&ip_conntrack_lock);
1107                 DEBUGP("no such conntrack, create new\n");
1108                 err = -ENOENT;
1109                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1110                         err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1111                 return err;
1112         }
1113         /* implicit 'else' */
1114
1115         /* we only allow nat config for new conntracks */
1116         if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
1117                 err = -EINVAL;
1118                 goto out_unlock;
1119         }
1120
1121         /* We manipulate the conntrack inside the global conntrack table lock,
1122          * so there's no need to increase the refcount */
1123         DEBUGP("conntrack found\n");
1124         err = -EEXIST;
1125         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1126                 err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda);
1127
1128 out_unlock:
1129         write_unlock_bh(&ip_conntrack_lock);
1130         return err;
1131 }
1132
1133 /*********************************************************************** 
1134  * EXPECT 
1135  ***********************************************************************/ 
1136
1137 static inline int
1138 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1139                          const struct ip_conntrack_tuple *tuple,
1140                          enum ctattr_expect type)
1141 {
1142         struct nfattr *nest_parms = NFA_NEST(skb, type);
1143         
1144         if (ctnetlink_dump_tuples(skb, tuple) < 0)
1145                 goto nfattr_failure;
1146
1147         NFA_NEST_END(skb, nest_parms);
1148
1149         return 0;
1150
1151 nfattr_failure:
1152         return -1;
1153 }                       
1154
1155 static inline int
1156 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1157                         const struct ip_conntrack_tuple *tuple,
1158                         const struct ip_conntrack_tuple *mask)
1159 {
1160         int ret;
1161         struct ip_conntrack_protocol *proto;
1162         struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1163
1164         ret = ctnetlink_dump_tuples_ip(skb, mask);
1165         if (unlikely(ret < 0))
1166                 goto nfattr_failure;
1167
1168         proto = ip_conntrack_proto_find_get(tuple->dst.protonum);
1169         ret = ctnetlink_dump_tuples_proto(skb, mask, proto);
1170         ip_conntrack_proto_put(proto);
1171         if (unlikely(ret < 0))
1172                 goto nfattr_failure;
1173
1174         NFA_NEST_END(skb, nest_parms);
1175
1176         return 0;
1177
1178 nfattr_failure:
1179         return -1;
1180 }
1181
1182 static inline int
1183 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1184                           const struct ip_conntrack_expect *exp)
1185 {
1186         struct ip_conntrack *master = exp->master;
1187         u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1188         u_int32_t id = htonl(exp->id);
1189
1190         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1191                 goto nfattr_failure;
1192         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1193                 goto nfattr_failure;
1194         if (ctnetlink_exp_dump_tuple(skb,
1195                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1196                                  CTA_EXPECT_MASTER) < 0)
1197                 goto nfattr_failure;
1198         
1199         NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1200         NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1201
1202         return 0;
1203         
1204 nfattr_failure:
1205         return -1;
1206 }
1207
1208 static int
1209 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1210                     int event, 
1211                     int nowait, 
1212                     const struct ip_conntrack_expect *exp)
1213 {
1214         struct nlmsghdr *nlh;
1215         struct nfgenmsg *nfmsg;
1216         unsigned char *b;
1217
1218         b = skb->tail;
1219
1220         event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1221         nlh    = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1222         nfmsg  = NLMSG_DATA(nlh);
1223
1224         nlh->nlmsg_flags    = (nowait && pid) ? NLM_F_MULTI : 0;
1225         nfmsg->nfgen_family = AF_INET;
1226         nfmsg->version      = NFNETLINK_V0;
1227         nfmsg->res_id       = 0;
1228
1229         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1230                 goto nfattr_failure;
1231
1232         nlh->nlmsg_len = skb->tail - b;
1233         return skb->len;
1234
1235 nlmsg_failure:
1236 nfattr_failure:
1237         skb_trim(skb, b - skb->data);
1238         return -1;
1239 }
1240
1241 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1242 static int ctnetlink_expect_event(struct notifier_block *this,
1243                                   unsigned long events, void *ptr)
1244 {
1245         struct nlmsghdr *nlh;
1246         struct nfgenmsg *nfmsg;
1247         struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr;
1248         struct sk_buff *skb;
1249         unsigned int type;
1250         unsigned char *b;
1251         int flags = 0;
1252
1253         if (events & IPEXP_NEW) {
1254                 type = IPCTNL_MSG_EXP_NEW;
1255                 flags = NLM_F_CREATE|NLM_F_EXCL;
1256         } else
1257                 return NOTIFY_DONE;
1258
1259         skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1260         if (!skb)
1261                 return NOTIFY_DONE;
1262
1263         b = skb->tail;
1264
1265         type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1266         nlh   = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1267         nfmsg = NLMSG_DATA(nlh);
1268
1269         nlh->nlmsg_flags    = flags;
1270         nfmsg->nfgen_family = AF_INET;
1271         nfmsg->version      = NFNETLINK_V0;
1272         nfmsg->res_id       = 0;
1273
1274         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1275                 goto nfattr_failure;
1276
1277         nlh->nlmsg_len = skb->tail - b;
1278         nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1279         return NOTIFY_DONE;
1280
1281 nlmsg_failure:
1282 nfattr_failure:
1283         kfree_skb(skb);
1284         return NOTIFY_DONE;
1285 }
1286 #endif
1287
1288 static int
1289 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1290 {
1291         struct ip_conntrack_expect *exp = NULL;
1292         struct list_head *i;
1293         u_int32_t *id = (u_int32_t *) &cb->args[0];
1294
1295         DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id);
1296
1297         read_lock_bh(&ip_conntrack_lock);
1298         list_for_each_prev(i, &ip_conntrack_expect_list) {
1299                 exp = (struct ip_conntrack_expect *) i;
1300                 if (exp->id <= *id)
1301                         continue;
1302                 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1303                                             cb->nlh->nlmsg_seq,
1304                                             IPCTNL_MSG_EXP_NEW,
1305                                             1, exp) < 0)
1306                         goto out;
1307                 *id = exp->id;
1308         }
1309 out:    
1310         read_unlock_bh(&ip_conntrack_lock);
1311
1312         DEBUGP("leaving, last id=%llu\n", *id);
1313
1314         return skb->len;
1315 }
1316
1317 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1318         [CTA_EXPECT_TIMEOUT-1]          = sizeof(u_int32_t),
1319         [CTA_EXPECT_ID-1]               = sizeof(u_int32_t)
1320 };
1321
1322 static int
1323 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, 
1324                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1325 {
1326         struct ip_conntrack_tuple tuple;
1327         struct ip_conntrack_expect *exp;
1328         struct sk_buff *skb2;
1329         int err = 0;
1330
1331         DEBUGP("entered %s\n", __FUNCTION__);
1332
1333         if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1334                 return -EINVAL;
1335
1336         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1337                 struct nfgenmsg *msg = NLMSG_DATA(nlh);
1338                 u32 rlen;
1339
1340                 if (msg->nfgen_family != AF_INET)
1341                         return -EAFNOSUPPORT;
1342
1343                 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1344                                                 ctnetlink_exp_dump_table,
1345                                                 ctnetlink_done)) != 0)
1346                         return -EINVAL;
1347                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1348                 if (rlen > skb->len)
1349                         rlen = skb->len;
1350                 skb_pull(skb, rlen);
1351                 return 0;
1352         }
1353
1354         if (cda[CTA_EXPECT_MASTER-1])
1355                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER);
1356         else
1357                 return -EINVAL;
1358
1359         if (err < 0)
1360                 return err;
1361
1362         exp = ip_conntrack_expect_find(&tuple);
1363         if (!exp)
1364                 return -ENOENT;
1365
1366         if (cda[CTA_EXPECT_ID-1]) {
1367                 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1368                 if (exp->id != ntohl(id)) {
1369                         ip_conntrack_expect_put(exp);
1370                         return -ENOENT;
1371                 }
1372         }       
1373
1374         err = -ENOMEM;
1375         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1376         if (!skb2)
1377                 goto out;
1378         NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid;
1379         
1380         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, 
1381                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1382                                       1, exp);
1383         if (err <= 0)
1384                 goto free;
1385
1386         ip_conntrack_expect_put(exp);
1387
1388         return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1389
1390 free:
1391         kfree_skb(skb2);
1392 out:
1393         ip_conntrack_expect_put(exp);
1394         return err;
1395 }
1396
1397 static int
1398 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, 
1399                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1400 {
1401         struct ip_conntrack_expect *exp, *tmp;
1402         struct ip_conntrack_tuple tuple;
1403         struct ip_conntrack_helper *h;
1404         int err;
1405
1406         if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1407                 return -EINVAL;
1408
1409         if (cda[CTA_EXPECT_TUPLE-1]) {
1410                 /* delete a single expect by tuple */
1411                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1412                 if (err < 0)
1413                         return err;
1414
1415                 /* bump usage count to 2 */
1416                 exp = ip_conntrack_expect_find(&tuple);
1417                 if (!exp)
1418                         return -ENOENT;
1419
1420                 if (cda[CTA_EXPECT_ID-1]) {
1421                         u_int32_t id = 
1422                                 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1423                         if (exp->id != ntohl(id)) {
1424                                 ip_conntrack_expect_put(exp);
1425                                 return -ENOENT;
1426                         }
1427                 }
1428
1429                 /* after list removal, usage count == 1 */
1430                 ip_conntrack_unexpect_related(exp);
1431                 /* have to put what we 'get' above. 
1432                  * after this line usage count == 0 */
1433                 ip_conntrack_expect_put(exp);
1434         } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1435                 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1436
1437                 /* delete all expectations for this helper */
1438                 write_lock_bh(&ip_conntrack_lock);
1439                 h = __ip_conntrack_helper_find_byname(name);
1440                 if (!h) {
1441                         write_unlock_bh(&ip_conntrack_lock);
1442                         return -EINVAL;
1443                 }
1444                 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1445                                          list) {
1446                         if (exp->master->helper == h 
1447                             && del_timer(&exp->timeout)) {
1448                                 ip_ct_unlink_expect(exp);
1449                                 ip_conntrack_expect_put(exp);
1450                         }
1451                 }
1452                 write_unlock_bh(&ip_conntrack_lock);
1453         } else {
1454                 /* This basically means we have to flush everything*/
1455                 write_lock_bh(&ip_conntrack_lock);
1456                 list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list,
1457                                          list) {
1458                         if (del_timer(&exp->timeout)) {
1459                                 ip_ct_unlink_expect(exp);
1460                                 ip_conntrack_expect_put(exp);
1461                         }
1462                 }
1463                 write_unlock_bh(&ip_conntrack_lock);
1464         }
1465
1466         return 0;
1467 }
1468 static int
1469 ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[])
1470 {
1471         return -EOPNOTSUPP;
1472 }
1473
1474 static int
1475 ctnetlink_create_expect(struct nfattr *cda[])
1476 {
1477         struct ip_conntrack_tuple tuple, mask, master_tuple;
1478         struct ip_conntrack_tuple_hash *h = NULL;
1479         struct ip_conntrack_expect *exp;
1480         struct ip_conntrack *ct;
1481         int err = 0;
1482
1483         DEBUGP("entered %s\n", __FUNCTION__);
1484
1485         /* caller guarantees that those three CTA_EXPECT_* exist */
1486         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1487         if (err < 0)
1488                 return err;
1489         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK);
1490         if (err < 0)
1491                 return err;
1492         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER);
1493         if (err < 0)
1494                 return err;
1495
1496         /* Look for master conntrack of this expectation */
1497         h = ip_conntrack_find_get(&master_tuple, NULL);
1498         if (!h)
1499                 return -ENOENT;
1500         ct = tuplehash_to_ctrack(h);
1501
1502         if (!ct->helper) {
1503                 /* such conntrack hasn't got any helper, abort */
1504                 err = -EINVAL;
1505                 goto out;
1506         }
1507
1508         exp = ip_conntrack_expect_alloc(ct);
1509         if (!exp) {
1510                 err = -ENOMEM;
1511                 goto out;
1512         }
1513         
1514         exp->expectfn = NULL;
1515         exp->flags = 0;
1516         exp->master = ct;
1517         memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple));
1518         memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple));
1519
1520         err = ip_conntrack_expect_related(exp);
1521         ip_conntrack_expect_put(exp);
1522
1523 out:    
1524         ip_conntrack_put(tuplehash_to_ctrack(h));
1525         return err;
1526 }
1527
1528 static int
1529 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1530                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1531 {
1532         struct ip_conntrack_tuple tuple;
1533         struct ip_conntrack_expect *exp;
1534         int err = 0;
1535
1536         DEBUGP("entered %s\n", __FUNCTION__);   
1537
1538         if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1539                 return -EINVAL;
1540
1541         if (!cda[CTA_EXPECT_TUPLE-1]
1542             || !cda[CTA_EXPECT_MASK-1]
1543             || !cda[CTA_EXPECT_MASTER-1])
1544                 return -EINVAL;
1545
1546         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE);
1547         if (err < 0)
1548                 return err;
1549
1550         write_lock_bh(&ip_conntrack_lock);
1551         exp = __ip_conntrack_expect_find(&tuple);
1552
1553         if (!exp) {
1554                 write_unlock_bh(&ip_conntrack_lock);
1555                 err = -ENOENT;
1556                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1557                         err = ctnetlink_create_expect(cda);
1558                 return err;
1559         }
1560
1561         err = -EEXIST;
1562         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1563                 err = ctnetlink_change_expect(exp, cda);
1564         write_unlock_bh(&ip_conntrack_lock);
1565
1566         DEBUGP("leaving\n");
1567         
1568         return err;
1569 }
1570
1571 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1572 static struct notifier_block ctnl_notifier = {
1573         .notifier_call  = ctnetlink_conntrack_event,
1574 };
1575
1576 static struct notifier_block ctnl_notifier_exp = {
1577         .notifier_call  = ctnetlink_expect_event,
1578 };
1579 #endif
1580
1581 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1582         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
1583                                             .attr_count = CTA_MAX, },
1584         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
1585                                             .attr_count = CTA_MAX, },
1586         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
1587                                             .attr_count = CTA_MAX, },
1588         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
1589                                             .attr_count = CTA_MAX, },
1590 };
1591
1592 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1593         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
1594                                             .attr_count = CTA_EXPECT_MAX, },
1595         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
1596                                             .attr_count = CTA_EXPECT_MAX, },
1597         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
1598                                             .attr_count = CTA_EXPECT_MAX, },
1599 };
1600
1601 static struct nfnetlink_subsystem ctnl_subsys = {
1602         .name                           = "conntrack",
1603         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
1604         .cb_count                       = IPCTNL_MSG_MAX,
1605         .cb                             = ctnl_cb,
1606 };
1607
1608 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1609         .name                           = "conntrack_expect",
1610         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
1611         .cb_count                       = IPCTNL_MSG_EXP_MAX,
1612         .cb                             = ctnl_exp_cb,
1613 };
1614
1615 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1616 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1617
1618 static int __init ctnetlink_init(void)
1619 {
1620         int ret;
1621
1622         printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1623         ret = nfnetlink_subsys_register(&ctnl_subsys);
1624         if (ret < 0) {
1625                 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1626                 goto err_out;
1627         }
1628
1629         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1630         if (ret < 0) {
1631                 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1632                 goto err_unreg_subsys;
1633         }
1634
1635 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1636         ret = ip_conntrack_register_notifier(&ctnl_notifier);
1637         if (ret < 0) {
1638                 printk("ctnetlink_init: cannot register notifier.\n");
1639                 goto err_unreg_exp_subsys;
1640         }
1641
1642         ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1643         if (ret < 0) {
1644                 printk("ctnetlink_init: cannot expect register notifier.\n");
1645                 goto err_unreg_notifier;
1646         }
1647 #endif
1648
1649         return 0;
1650
1651 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1652 err_unreg_notifier:
1653         ip_conntrack_unregister_notifier(&ctnl_notifier);
1654 err_unreg_exp_subsys:
1655         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1656 #endif
1657 err_unreg_subsys:
1658         nfnetlink_subsys_unregister(&ctnl_subsys);
1659 err_out:
1660         return ret;
1661 }
1662
1663 static void __exit ctnetlink_exit(void)
1664 {
1665         printk("ctnetlink: unregistering from nfnetlink.\n");
1666
1667 #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS
1668         ip_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
1669         ip_conntrack_unregister_notifier(&ctnl_notifier);
1670 #endif
1671
1672         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1673         nfnetlink_subsys_unregister(&ctnl_subsys);
1674         return;
1675 }
1676
1677 module_init(ctnetlink_init);
1678 module_exit(ctnetlink_exit);