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