]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/netfilter/nf_conntrack_netlink.c
[NETFILTER]: nf_conntrack: rename struct nf_conntrack_protocol
[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         err = ctnetlink_change_status(ct, cda);
967         if (err < 0)
968                 goto err;
969
970         if (cda[CTA_PROTOINFO-1]) {
971                 err = ctnetlink_change_protoinfo(ct, cda);
972                 if (err < 0)
973                         return err;
974         }
975
976 #if defined(CONFIG_NF_CONNTRACK_MARK)
977         if (cda[CTA_MARK-1])
978                 ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1]));
979 #endif
980
981         help = nfct_help(ct);
982         if (help)
983                 help->helper = nf_ct_helper_find_get(rtuple);
984
985         add_timer(&ct->timeout);
986         nf_conntrack_hash_insert(ct);
987
988         if (help && help->helper)
989                 nf_ct_helper_put(help->helper);
990
991         return 0;
992
993 err:    
994         nf_conntrack_free(ct);
995         return err;
996 }
997
998 static int 
999 ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, 
1000                         struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1001 {
1002         struct nf_conntrack_tuple otuple, rtuple;
1003         struct nf_conntrack_tuple_hash *h = NULL;
1004         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1005         u_int8_t u3 = nfmsg->nfgen_family;
1006         int err = 0;
1007
1008         if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1009                 return -EINVAL;
1010
1011         if (cda[CTA_TUPLE_ORIG-1]) {
1012                 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1013                 if (err < 0)
1014                         return err;
1015         }
1016
1017         if (cda[CTA_TUPLE_REPLY-1]) {
1018                 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1019                 if (err < 0)
1020                         return err;
1021         }
1022
1023         write_lock_bh(&nf_conntrack_lock);
1024         if (cda[CTA_TUPLE_ORIG-1])
1025                 h = __nf_conntrack_find(&otuple, NULL);
1026         else if (cda[CTA_TUPLE_REPLY-1])
1027                 h = __nf_conntrack_find(&rtuple, NULL);
1028
1029         if (h == NULL) {
1030                 write_unlock_bh(&nf_conntrack_lock);
1031                 err = -ENOENT;
1032                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1033                         err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1034                 return err;
1035         }
1036         /* implicit 'else' */
1037
1038         /* we only allow nat config for new conntracks */
1039         if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
1040                 err = -EINVAL;
1041                 goto out_unlock;
1042         }
1043
1044         /* We manipulate the conntrack inside the global conntrack table lock,
1045          * so there's no need to increase the refcount */
1046         err = -EEXIST;
1047         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1048                 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1049
1050 out_unlock:
1051         write_unlock_bh(&nf_conntrack_lock);
1052         return err;
1053 }
1054
1055 /*********************************************************************** 
1056  * EXPECT 
1057  ***********************************************************************/ 
1058
1059 static inline int
1060 ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1061                          const struct nf_conntrack_tuple *tuple,
1062                          enum ctattr_expect type)
1063 {
1064         struct nfattr *nest_parms = NFA_NEST(skb, type);
1065         
1066         if (ctnetlink_dump_tuples(skb, tuple) < 0)
1067                 goto nfattr_failure;
1068
1069         NFA_NEST_END(skb, nest_parms);
1070
1071         return 0;
1072
1073 nfattr_failure:
1074         return -1;
1075 }                       
1076
1077 static inline int
1078 ctnetlink_exp_dump_mask(struct sk_buff *skb,
1079                         const struct nf_conntrack_tuple *tuple,
1080                         const struct nf_conntrack_tuple *mask)
1081 {
1082         int ret;
1083         struct nf_conntrack_l3proto *l3proto;
1084         struct nf_conntrack_l4proto *l4proto;
1085         struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1086
1087         l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1088         ret = ctnetlink_dump_tuples_ip(skb, mask, l3proto);
1089         nf_ct_l3proto_put(l3proto);
1090
1091         if (unlikely(ret < 0))
1092                 goto nfattr_failure;
1093
1094         l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1095         ret = ctnetlink_dump_tuples_proto(skb, mask, l4proto);
1096         nf_ct_l4proto_put(l4proto);
1097         if (unlikely(ret < 0))
1098                 goto nfattr_failure;
1099
1100         NFA_NEST_END(skb, nest_parms);
1101
1102         return 0;
1103
1104 nfattr_failure:
1105         return -1;
1106 }
1107
1108 static inline int
1109 ctnetlink_exp_dump_expect(struct sk_buff *skb,
1110                           const struct nf_conntrack_expect *exp)
1111 {
1112         struct nf_conn *master = exp->master;
1113         u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1114         u_int32_t id = htonl(exp->id);
1115
1116         if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1117                 goto nfattr_failure;
1118         if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
1119                 goto nfattr_failure;
1120         if (ctnetlink_exp_dump_tuple(skb,
1121                                  &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1122                                  CTA_EXPECT_MASTER) < 0)
1123                 goto nfattr_failure;
1124         
1125         NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1126         NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1127
1128         return 0;
1129         
1130 nfattr_failure:
1131         return -1;
1132 }
1133
1134 static int
1135 ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
1136                     int event, 
1137                     int nowait, 
1138                     const struct nf_conntrack_expect *exp)
1139 {
1140         struct nlmsghdr *nlh;
1141         struct nfgenmsg *nfmsg;
1142         unsigned char *b;
1143
1144         b = skb->tail;
1145
1146         event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1147         nlh    = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1148         nfmsg  = NLMSG_DATA(nlh);
1149
1150         nlh->nlmsg_flags    = (nowait && pid) ? NLM_F_MULTI : 0;
1151         nfmsg->nfgen_family = exp->tuple.src.l3num;
1152         nfmsg->version      = NFNETLINK_V0;
1153         nfmsg->res_id       = 0;
1154
1155         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1156                 goto nfattr_failure;
1157
1158         nlh->nlmsg_len = skb->tail - b;
1159         return skb->len;
1160
1161 nlmsg_failure:
1162 nfattr_failure:
1163         skb_trim(skb, b - skb->data);
1164         return -1;
1165 }
1166
1167 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1168 static int ctnetlink_expect_event(struct notifier_block *this,
1169                                   unsigned long events, void *ptr)
1170 {
1171         struct nlmsghdr *nlh;
1172         struct nfgenmsg *nfmsg;
1173         struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1174         struct sk_buff *skb;
1175         unsigned int type;
1176         unsigned char *b;
1177         int flags = 0;
1178
1179         if (events & IPEXP_NEW) {
1180                 type = IPCTNL_MSG_EXP_NEW;
1181                 flags = NLM_F_CREATE|NLM_F_EXCL;
1182         } else
1183                 return NOTIFY_DONE;
1184
1185         if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1186                 return NOTIFY_DONE;
1187
1188         skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1189         if (!skb)
1190                 return NOTIFY_DONE;
1191
1192         b = skb->tail;
1193
1194         type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1195         nlh   = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1196         nfmsg = NLMSG_DATA(nlh);
1197
1198         nlh->nlmsg_flags    = flags;
1199         nfmsg->nfgen_family = exp->tuple.src.l3num;
1200         nfmsg->version      = NFNETLINK_V0;
1201         nfmsg->res_id       = 0;
1202
1203         if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1204                 goto nfattr_failure;
1205
1206         nlh->nlmsg_len = skb->tail - b;
1207         nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1208         return NOTIFY_DONE;
1209
1210 nlmsg_failure:
1211 nfattr_failure:
1212         kfree_skb(skb);
1213         return NOTIFY_DONE;
1214 }
1215 #endif
1216
1217 static int
1218 ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1219 {
1220         struct nf_conntrack_expect *exp = NULL;
1221         struct list_head *i;
1222         u_int32_t *id = (u_int32_t *) &cb->args[0];
1223         struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1224         u_int8_t l3proto = nfmsg->nfgen_family;
1225
1226         read_lock_bh(&nf_conntrack_lock);
1227         list_for_each_prev(i, &nf_conntrack_expect_list) {
1228                 exp = (struct nf_conntrack_expect *) i;
1229                 if (l3proto && exp->tuple.src.l3num != l3proto)
1230                         continue;
1231                 if (exp->id <= *id)
1232                         continue;
1233                 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1234                                             cb->nlh->nlmsg_seq,
1235                                             IPCTNL_MSG_EXP_NEW,
1236                                             1, exp) < 0)
1237                         goto out;
1238                 *id = exp->id;
1239         }
1240 out:    
1241         read_unlock_bh(&nf_conntrack_lock);
1242
1243         return skb->len;
1244 }
1245
1246 static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1247         [CTA_EXPECT_TIMEOUT-1]          = sizeof(u_int32_t),
1248         [CTA_EXPECT_ID-1]               = sizeof(u_int32_t)
1249 };
1250
1251 static int
1252 ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, 
1253                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1254 {
1255         struct nf_conntrack_tuple tuple;
1256         struct nf_conntrack_expect *exp;
1257         struct sk_buff *skb2;
1258         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1259         u_int8_t u3 = nfmsg->nfgen_family;
1260         int err = 0;
1261
1262         if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1263                 return -EINVAL;
1264
1265         if (nlh->nlmsg_flags & NLM_F_DUMP) {
1266                 u32 rlen;
1267
1268                 if ((*errp = netlink_dump_start(ctnl, skb, nlh,
1269                                                 ctnetlink_exp_dump_table,
1270                                                 ctnetlink_done)) != 0)
1271                         return -EINVAL;
1272                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
1273                 if (rlen > skb->len)
1274                         rlen = skb->len;
1275                 skb_pull(skb, rlen);
1276                 return 0;
1277         }
1278
1279         if (cda[CTA_EXPECT_MASTER-1])
1280                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1281         else
1282                 return -EINVAL;
1283
1284         if (err < 0)
1285                 return err;
1286
1287         exp = nf_conntrack_expect_find(&tuple);
1288         if (!exp)
1289                 return -ENOENT;
1290
1291         if (cda[CTA_EXPECT_ID-1]) {
1292                 u_int32_t id = *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1293                 if (exp->id != ntohl(id)) {
1294                         nf_conntrack_expect_put(exp);
1295                         return -ENOENT;
1296                 }
1297         }       
1298
1299         err = -ENOMEM;
1300         skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1301         if (!skb2)
1302                 goto out;
1303
1304         err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, 
1305                                       nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1306                                       1, exp);
1307         if (err <= 0)
1308                 goto free;
1309
1310         nf_conntrack_expect_put(exp);
1311
1312         return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1313
1314 free:
1315         kfree_skb(skb2);
1316 out:
1317         nf_conntrack_expect_put(exp);
1318         return err;
1319 }
1320
1321 static int
1322 ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, 
1323                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1324 {
1325         struct nf_conntrack_expect *exp, *tmp;
1326         struct nf_conntrack_tuple tuple;
1327         struct nf_conntrack_helper *h;
1328         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1329         u_int8_t u3 = nfmsg->nfgen_family;
1330         int err;
1331
1332         if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1333                 return -EINVAL;
1334
1335         if (cda[CTA_EXPECT_TUPLE-1]) {
1336                 /* delete a single expect by tuple */
1337                 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1338                 if (err < 0)
1339                         return err;
1340
1341                 /* bump usage count to 2 */
1342                 exp = nf_conntrack_expect_find(&tuple);
1343                 if (!exp)
1344                         return -ENOENT;
1345
1346                 if (cda[CTA_EXPECT_ID-1]) {
1347                         u_int32_t id = 
1348                                 *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
1349                         if (exp->id != ntohl(id)) {
1350                                 nf_conntrack_expect_put(exp);
1351                                 return -ENOENT;
1352                         }
1353                 }
1354
1355                 /* after list removal, usage count == 1 */
1356                 nf_conntrack_unexpect_related(exp);
1357                 /* have to put what we 'get' above. 
1358                  * after this line usage count == 0 */
1359                 nf_conntrack_expect_put(exp);
1360         } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1361                 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1362
1363                 /* delete all expectations for this helper */
1364                 write_lock_bh(&nf_conntrack_lock);
1365                 h = __nf_conntrack_helper_find_byname(name);
1366                 if (!h) {
1367                         write_unlock_bh(&nf_conntrack_lock);
1368                         return -EINVAL;
1369                 }
1370                 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1371                                          list) {
1372                         struct nf_conn_help *m_help = nfct_help(exp->master);
1373                         if (m_help->helper == h
1374                             && del_timer(&exp->timeout)) {
1375                                 nf_ct_unlink_expect(exp);
1376                                 nf_conntrack_expect_put(exp);
1377                         }
1378                 }
1379                 write_unlock_bh(&nf_conntrack_lock);
1380         } else {
1381                 /* This basically means we have to flush everything*/
1382                 write_lock_bh(&nf_conntrack_lock);
1383                 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1384                                          list) {
1385                         if (del_timer(&exp->timeout)) {
1386                                 nf_ct_unlink_expect(exp);
1387                                 nf_conntrack_expect_put(exp);
1388                         }
1389                 }
1390                 write_unlock_bh(&nf_conntrack_lock);
1391         }
1392
1393         return 0;
1394 }
1395 static int
1396 ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1397 {
1398         return -EOPNOTSUPP;
1399 }
1400
1401 static int
1402 ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1403 {
1404         struct nf_conntrack_tuple tuple, mask, master_tuple;
1405         struct nf_conntrack_tuple_hash *h = NULL;
1406         struct nf_conntrack_expect *exp;
1407         struct nf_conn *ct;
1408         struct nf_conn_help *help;
1409         int err = 0;
1410
1411         /* caller guarantees that those three CTA_EXPECT_* exist */
1412         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1413         if (err < 0)
1414                 return err;
1415         err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1416         if (err < 0)
1417                 return err;
1418         err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1419         if (err < 0)
1420                 return err;
1421
1422         /* Look for master conntrack of this expectation */
1423         h = nf_conntrack_find_get(&master_tuple, NULL);
1424         if (!h)
1425                 return -ENOENT;
1426         ct = nf_ct_tuplehash_to_ctrack(h);
1427         help = nfct_help(ct);
1428
1429         if (!help || !help->helper) {
1430                 /* such conntrack hasn't got any helper, abort */
1431                 err = -EINVAL;
1432                 goto out;
1433         }
1434
1435         exp = nf_conntrack_expect_alloc(ct);
1436         if (!exp) {
1437                 err = -ENOMEM;
1438                 goto out;
1439         }
1440         
1441         exp->expectfn = NULL;
1442         exp->flags = 0;
1443         exp->master = ct;
1444         memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1445         memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1446
1447         err = nf_conntrack_expect_related(exp);
1448         nf_conntrack_expect_put(exp);
1449
1450 out:    
1451         nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1452         return err;
1453 }
1454
1455 static int
1456 ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
1457                      struct nlmsghdr *nlh, struct nfattr *cda[], int *errp)
1458 {
1459         struct nf_conntrack_tuple tuple;
1460         struct nf_conntrack_expect *exp;
1461         struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1462         u_int8_t u3 = nfmsg->nfgen_family;
1463         int err = 0;
1464
1465         if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1466                 return -EINVAL;
1467
1468         if (!cda[CTA_EXPECT_TUPLE-1]
1469             || !cda[CTA_EXPECT_MASK-1]
1470             || !cda[CTA_EXPECT_MASTER-1])
1471                 return -EINVAL;
1472
1473         err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1474         if (err < 0)
1475                 return err;
1476
1477         write_lock_bh(&nf_conntrack_lock);
1478         exp = __nf_conntrack_expect_find(&tuple);
1479
1480         if (!exp) {
1481                 write_unlock_bh(&nf_conntrack_lock);
1482                 err = -ENOENT;
1483                 if (nlh->nlmsg_flags & NLM_F_CREATE)
1484                         err = ctnetlink_create_expect(cda, u3);
1485                 return err;
1486         }
1487
1488         err = -EEXIST;
1489         if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1490                 err = ctnetlink_change_expect(exp, cda);
1491         write_unlock_bh(&nf_conntrack_lock);
1492
1493         return err;
1494 }
1495
1496 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1497 static struct notifier_block ctnl_notifier = {
1498         .notifier_call  = ctnetlink_conntrack_event,
1499 };
1500
1501 static struct notifier_block ctnl_notifier_exp = {
1502         .notifier_call  = ctnetlink_expect_event,
1503 };
1504 #endif
1505
1506 static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1507         [IPCTNL_MSG_CT_NEW]             = { .call = ctnetlink_new_conntrack,
1508                                             .attr_count = CTA_MAX, },
1509         [IPCTNL_MSG_CT_GET]             = { .call = ctnetlink_get_conntrack,
1510                                             .attr_count = CTA_MAX, },
1511         [IPCTNL_MSG_CT_DELETE]          = { .call = ctnetlink_del_conntrack,
1512                                             .attr_count = CTA_MAX, },
1513         [IPCTNL_MSG_CT_GET_CTRZERO]     = { .call = ctnetlink_get_conntrack,
1514                                             .attr_count = CTA_MAX, },
1515 };
1516
1517 static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1518         [IPCTNL_MSG_EXP_GET]            = { .call = ctnetlink_get_expect,
1519                                             .attr_count = CTA_EXPECT_MAX, },
1520         [IPCTNL_MSG_EXP_NEW]            = { .call = ctnetlink_new_expect,
1521                                             .attr_count = CTA_EXPECT_MAX, },
1522         [IPCTNL_MSG_EXP_DELETE]         = { .call = ctnetlink_del_expect,
1523                                             .attr_count = CTA_EXPECT_MAX, },
1524 };
1525
1526 static struct nfnetlink_subsystem ctnl_subsys = {
1527         .name                           = "conntrack",
1528         .subsys_id                      = NFNL_SUBSYS_CTNETLINK,
1529         .cb_count                       = IPCTNL_MSG_MAX,
1530         .cb                             = ctnl_cb,
1531 };
1532
1533 static struct nfnetlink_subsystem ctnl_exp_subsys = {
1534         .name                           = "conntrack_expect",
1535         .subsys_id                      = NFNL_SUBSYS_CTNETLINK_EXP,
1536         .cb_count                       = IPCTNL_MSG_EXP_MAX,
1537         .cb                             = ctnl_exp_cb,
1538 };
1539
1540 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
1541 MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
1542
1543 static int __init ctnetlink_init(void)
1544 {
1545         int ret;
1546
1547         printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1548         ret = nfnetlink_subsys_register(&ctnl_subsys);
1549         if (ret < 0) {
1550                 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1551                 goto err_out;
1552         }
1553
1554         ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1555         if (ret < 0) {
1556                 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1557                 goto err_unreg_subsys;
1558         }
1559
1560 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1561         ret = nf_conntrack_register_notifier(&ctnl_notifier);
1562         if (ret < 0) {
1563                 printk("ctnetlink_init: cannot register notifier.\n");
1564                 goto err_unreg_exp_subsys;
1565         }
1566
1567         ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1568         if (ret < 0) {
1569                 printk("ctnetlink_init: cannot expect register notifier.\n");
1570                 goto err_unreg_notifier;
1571         }
1572 #endif
1573
1574         return 0;
1575
1576 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1577 err_unreg_notifier:
1578         nf_conntrack_unregister_notifier(&ctnl_notifier);
1579 err_unreg_exp_subsys:
1580         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1581 #endif
1582 err_unreg_subsys:
1583         nfnetlink_subsys_unregister(&ctnl_subsys);
1584 err_out:
1585         return ret;
1586 }
1587
1588 static void __exit ctnetlink_exit(void)
1589 {
1590         printk("ctnetlink: unregistering from nfnetlink.\n");
1591
1592 #ifdef CONFIG_NF_CONNTRACK_EVENTS
1593         nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
1594         nf_conntrack_unregister_notifier(&ctnl_notifier);
1595 #endif
1596
1597         nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1598         nfnetlink_subsys_unregister(&ctnl_subsys);
1599         return;
1600 }
1601
1602 module_init(ctnetlink_init);
1603 module_exit(ctnetlink_exit);