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