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