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