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