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