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