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