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