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