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