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