[NETFILTER]: nf_conntrack: fix helper module unload races
[safe/jmp/linux-2.6] / net / ipv6 / netfilter / nf_conntrack_l3proto_ipv6.c
1 /*
2  * Copyright (C)2004 USAGI/WIDE Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * Author:
9  *      Yasuyuki Kozakai @USAGI <yasuyuki.kozakai@toshiba.co.jp>
10  */
11
12 #include <linux/types.h>
13 #include <linux/ipv6.h>
14 #include <linux/in6.h>
15 #include <linux/netfilter.h>
16 #include <linux/module.h>
17 #include <linux/skbuff.h>
18 #include <linux/icmp.h>
19 #include <linux/sysctl.h>
20 #include <net/ipv6.h>
21
22 #include <linux/netfilter_ipv6.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_helper.h>
25 #include <net/netfilter/nf_conntrack_l4proto.h>
26 #include <net/netfilter/nf_conntrack_l3proto.h>
27 #include <net/netfilter/nf_conntrack_core.h>
28
29 #if 0
30 #define DEBUGP printk
31 #else
32 #define DEBUGP(format, args...)
33 #endif
34
35 static int ipv6_pkt_to_tuple(const struct sk_buff *skb, unsigned int nhoff,
36                              struct nf_conntrack_tuple *tuple)
37 {
38         u_int32_t _addrs[8], *ap;
39
40         ap = skb_header_pointer(skb, nhoff + offsetof(struct ipv6hdr, saddr),
41                                 sizeof(_addrs), _addrs);
42         if (ap == NULL)
43                 return 0;
44
45         memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
46         memcpy(tuple->dst.u3.ip6, ap + 4, sizeof(tuple->dst.u3.ip6));
47
48         return 1;
49 }
50
51 static int ipv6_invert_tuple(struct nf_conntrack_tuple *tuple,
52                              const struct nf_conntrack_tuple *orig)
53 {
54         memcpy(tuple->src.u3.ip6, orig->dst.u3.ip6, sizeof(tuple->src.u3.ip6));
55         memcpy(tuple->dst.u3.ip6, orig->src.u3.ip6, sizeof(tuple->dst.u3.ip6));
56
57         return 1;
58 }
59
60 static int ipv6_print_tuple(struct seq_file *s,
61                             const struct nf_conntrack_tuple *tuple)
62 {
63         return seq_printf(s, "src=" NIP6_FMT " dst=" NIP6_FMT " ",
64                           NIP6(*((struct in6_addr *)tuple->src.u3.ip6)),
65                           NIP6(*((struct in6_addr *)tuple->dst.u3.ip6)));
66 }
67
68 static int ipv6_print_conntrack(struct seq_file *s,
69                                 const struct nf_conn *conntrack)
70 {
71         return 0;
72 }
73
74 /*
75  * Based on ipv6_skip_exthdr() in net/ipv6/exthdr.c
76  *
77  * This function parses (probably truncated) exthdr set "hdr"
78  * of length "len". "nexthdrp" initially points to some place,
79  * where type of the first header can be found.
80  *
81  * It skips all well-known exthdrs, and returns pointer to the start
82  * of unparsable area i.e. the first header with unknown type.
83  * if success, *nexthdr is updated by type/protocol of this header.
84  *
85  * NOTES: - it may return pointer pointing beyond end of packet,
86  *          if the last recognized header is truncated in the middle.
87  *        - if packet is truncated, so that all parsed headers are skipped,
88  *          it returns -1.
89  *        - if packet is fragmented, return pointer of the fragment header.
90  *        - ESP is unparsable for now and considered like
91  *          normal payload protocol.
92  *        - Note also special handling of AUTH header. Thanks to IPsec wizards.
93  */
94
95 int nf_ct_ipv6_skip_exthdr(struct sk_buff *skb, int start, u8 *nexthdrp,
96                            int len)
97 {
98         u8 nexthdr = *nexthdrp;
99
100         while (ipv6_ext_hdr(nexthdr)) {
101                 struct ipv6_opt_hdr hdr;
102                 int hdrlen;
103
104                 if (len < (int)sizeof(struct ipv6_opt_hdr))
105                         return -1;
106                 if (nexthdr == NEXTHDR_NONE)
107                         break;
108                 if (nexthdr == NEXTHDR_FRAGMENT)
109                         break;
110                 if (skb_copy_bits(skb, start, &hdr, sizeof(hdr)))
111                         BUG();
112                 if (nexthdr == NEXTHDR_AUTH)
113                         hdrlen = (hdr.hdrlen+2)<<2;
114                 else
115                         hdrlen = ipv6_optlen(&hdr);
116
117                 nexthdr = hdr.nexthdr;
118                 len -= hdrlen;
119                 start += hdrlen;
120         }
121
122         *nexthdrp = nexthdr;
123         return start;
124 }
125
126 static int
127 ipv6_prepare(struct sk_buff **pskb, unsigned int hooknum, unsigned int *dataoff,
128              u_int8_t *protonum)
129 {
130         unsigned int extoff = (u8 *)(ipv6_hdr(*pskb) + 1) - (*pskb)->data;
131         unsigned char pnum = ipv6_hdr(*pskb)->nexthdr;
132         int protoff = nf_ct_ipv6_skip_exthdr(*pskb, extoff, &pnum,
133                                              (*pskb)->len - extoff);
134         /*
135          * (protoff == (*pskb)->len) mean that the packet doesn't have no data
136          * except of IPv6 & ext headers. but it's tracked anyway. - YK
137          */
138         if ((protoff < 0) || (protoff > (*pskb)->len)) {
139                 DEBUGP("ip6_conntrack_core: can't find proto in pkt\n");
140                 NF_CT_STAT_INC_ATOMIC(error);
141                 NF_CT_STAT_INC_ATOMIC(invalid);
142                 return -NF_ACCEPT;
143         }
144
145         *dataoff = protoff;
146         *protonum = pnum;
147         return NF_ACCEPT;
148 }
149
150 static u_int32_t ipv6_get_features(const struct nf_conntrack_tuple *tuple)
151 {
152         return NF_CT_F_BASIC;
153 }
154
155 static unsigned int ipv6_confirm(unsigned int hooknum,
156                                  struct sk_buff **pskb,
157                                  const struct net_device *in,
158                                  const struct net_device *out,
159                                  int (*okfn)(struct sk_buff *))
160 {
161         struct nf_conn *ct;
162         struct nf_conn_help *help;
163         struct nf_conntrack_helper *helper;
164         enum ip_conntrack_info ctinfo;
165         unsigned int ret, protoff;
166         unsigned int extoff = (u8 *)(ipv6_hdr(*pskb) + 1) - (*pskb)->data;
167         unsigned char pnum = ipv6_hdr(*pskb)->nexthdr;
168
169
170         /* This is where we call the helper: as the packet goes out. */
171         ct = nf_ct_get(*pskb, &ctinfo);
172         if (!ct || ctinfo == IP_CT_RELATED + IP_CT_IS_REPLY)
173                 goto out;
174
175         help = nfct_help(ct);
176         if (!help)
177                 goto out;
178         /* rcu_read_lock()ed by nf_hook_slow */
179         helper = rcu_dereference(help->helper);
180         if (!helper)
181                 goto out;
182
183         protoff = nf_ct_ipv6_skip_exthdr(*pskb, extoff, &pnum,
184                                          (*pskb)->len - extoff);
185         if (protoff > (*pskb)->len || pnum == NEXTHDR_FRAGMENT) {
186                 DEBUGP("proto header not found\n");
187                 return NF_ACCEPT;
188         }
189
190         ret = helper->help(pskb, protoff, ct, ctinfo);
191         if (ret != NF_ACCEPT)
192                 return ret;
193 out:
194         /* We've seen it coming out the other side: confirm it */
195         return nf_conntrack_confirm(pskb);
196 }
197
198 static unsigned int ipv6_defrag(unsigned int hooknum,
199                                 struct sk_buff **pskb,
200                                 const struct net_device *in,
201                                 const struct net_device *out,
202                                 int (*okfn)(struct sk_buff *))
203 {
204         struct sk_buff *reasm;
205
206         /* Previously seen (loopback)?  */
207         if ((*pskb)->nfct)
208                 return NF_ACCEPT;
209
210         reasm = nf_ct_frag6_gather(*pskb);
211
212         /* queued */
213         if (reasm == NULL)
214                 return NF_STOLEN;
215
216         /* error occured or not fragmented */
217         if (reasm == *pskb)
218                 return NF_ACCEPT;
219
220         nf_ct_frag6_output(hooknum, reasm, (struct net_device *)in,
221                            (struct net_device *)out, okfn);
222
223         return NF_STOLEN;
224 }
225
226 static unsigned int ipv6_conntrack_in(unsigned int hooknum,
227                                       struct sk_buff **pskb,
228                                       const struct net_device *in,
229                                       const struct net_device *out,
230                                       int (*okfn)(struct sk_buff *))
231 {
232         struct sk_buff *reasm = (*pskb)->nfct_reasm;
233
234         /* This packet is fragmented and has reassembled packet. */
235         if (reasm) {
236                 /* Reassembled packet isn't parsed yet ? */
237                 if (!reasm->nfct) {
238                         unsigned int ret;
239
240                         ret = nf_conntrack_in(PF_INET6, hooknum, &reasm);
241                         if (ret != NF_ACCEPT)
242                                 return ret;
243                 }
244                 nf_conntrack_get(reasm->nfct);
245                 (*pskb)->nfct = reasm->nfct;
246                 (*pskb)->nfctinfo = reasm->nfctinfo;
247                 return NF_ACCEPT;
248         }
249
250         return nf_conntrack_in(PF_INET6, hooknum, pskb);
251 }
252
253 static unsigned int ipv6_conntrack_local(unsigned int hooknum,
254                                          struct sk_buff **pskb,
255                                          const struct net_device *in,
256                                          const struct net_device *out,
257                                          int (*okfn)(struct sk_buff *))
258 {
259         /* root is playing with raw sockets. */
260         if ((*pskb)->len < sizeof(struct ipv6hdr)) {
261                 if (net_ratelimit())
262                         printk("ipv6_conntrack_local: packet too short\n");
263                 return NF_ACCEPT;
264         }
265         return ipv6_conntrack_in(hooknum, pskb, in, out, okfn);
266 }
267
268 static struct nf_hook_ops ipv6_conntrack_ops[] = {
269         {
270                 .hook           = ipv6_defrag,
271                 .owner          = THIS_MODULE,
272                 .pf             = PF_INET6,
273                 .hooknum        = NF_IP6_PRE_ROUTING,
274                 .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
275         },
276         {
277                 .hook           = ipv6_conntrack_in,
278                 .owner          = THIS_MODULE,
279                 .pf             = PF_INET6,
280                 .hooknum        = NF_IP6_PRE_ROUTING,
281                 .priority       = NF_IP6_PRI_CONNTRACK,
282         },
283         {
284                 .hook           = ipv6_conntrack_local,
285                 .owner          = THIS_MODULE,
286                 .pf             = PF_INET6,
287                 .hooknum        = NF_IP6_LOCAL_OUT,
288                 .priority       = NF_IP6_PRI_CONNTRACK,
289         },
290         {
291                 .hook           = ipv6_defrag,
292                 .owner          = THIS_MODULE,
293                 .pf             = PF_INET6,
294                 .hooknum        = NF_IP6_LOCAL_OUT,
295                 .priority       = NF_IP6_PRI_CONNTRACK_DEFRAG,
296         },
297         {
298                 .hook           = ipv6_confirm,
299                 .owner          = THIS_MODULE,
300                 .pf             = PF_INET6,
301                 .hooknum        = NF_IP6_POST_ROUTING,
302                 .priority       = NF_IP6_PRI_LAST,
303         },
304         {
305                 .hook           = ipv6_confirm,
306                 .owner          = THIS_MODULE,
307                 .pf             = PF_INET6,
308                 .hooknum        = NF_IP6_LOCAL_IN,
309                 .priority       = NF_IP6_PRI_LAST-1,
310         },
311 };
312
313 #ifdef CONFIG_SYSCTL
314 static ctl_table nf_ct_ipv6_sysctl_table[] = {
315         {
316                 .ctl_name       = NET_NF_CONNTRACK_FRAG6_TIMEOUT,
317                 .procname       = "nf_conntrack_frag6_timeout",
318                 .data           = &nf_ct_frag6_timeout,
319                 .maxlen         = sizeof(unsigned int),
320                 .mode           = 0644,
321                 .proc_handler   = &proc_dointvec_jiffies,
322         },
323         {
324                 .ctl_name       = NET_NF_CONNTRACK_FRAG6_LOW_THRESH,
325                 .procname       = "nf_conntrack_frag6_low_thresh",
326                 .data           = &nf_ct_frag6_low_thresh,
327                 .maxlen         = sizeof(unsigned int),
328                 .mode           = 0644,
329                 .proc_handler   = &proc_dointvec,
330         },
331         {
332                 .ctl_name       = NET_NF_CONNTRACK_FRAG6_HIGH_THRESH,
333                 .procname       = "nf_conntrack_frag6_high_thresh",
334                 .data           = &nf_ct_frag6_high_thresh,
335                 .maxlen         = sizeof(unsigned int),
336                 .mode           = 0644,
337                 .proc_handler   = &proc_dointvec,
338         },
339         { .ctl_name = 0 }
340 };
341 #endif
342
343 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
344
345 #include <linux/netfilter/nfnetlink.h>
346 #include <linux/netfilter/nfnetlink_conntrack.h>
347
348 static int ipv6_tuple_to_nfattr(struct sk_buff *skb,
349                                 const struct nf_conntrack_tuple *tuple)
350 {
351         NFA_PUT(skb, CTA_IP_V6_SRC, sizeof(u_int32_t) * 4,
352                 &tuple->src.u3.ip6);
353         NFA_PUT(skb, CTA_IP_V6_DST, sizeof(u_int32_t) * 4,
354                 &tuple->dst.u3.ip6);
355         return 0;
356
357 nfattr_failure:
358         return -1;
359 }
360
361 static const size_t cta_min_ip[CTA_IP_MAX] = {
362         [CTA_IP_V6_SRC-1]       = sizeof(u_int32_t)*4,
363         [CTA_IP_V6_DST-1]       = sizeof(u_int32_t)*4,
364 };
365
366 static int ipv6_nfattr_to_tuple(struct nfattr *tb[],
367                                 struct nf_conntrack_tuple *t)
368 {
369         if (!tb[CTA_IP_V6_SRC-1] || !tb[CTA_IP_V6_DST-1])
370                 return -EINVAL;
371
372         if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip))
373                 return -EINVAL;
374
375         memcpy(&t->src.u3.ip6, NFA_DATA(tb[CTA_IP_V6_SRC-1]),
376                sizeof(u_int32_t) * 4);
377         memcpy(&t->dst.u3.ip6, NFA_DATA(tb[CTA_IP_V6_DST-1]),
378                sizeof(u_int32_t) * 4);
379
380         return 0;
381 }
382 #endif
383
384 struct nf_conntrack_l3proto nf_conntrack_l3proto_ipv6 = {
385         .l3proto                = PF_INET6,
386         .name                   = "ipv6",
387         .pkt_to_tuple           = ipv6_pkt_to_tuple,
388         .invert_tuple           = ipv6_invert_tuple,
389         .print_tuple            = ipv6_print_tuple,
390         .print_conntrack        = ipv6_print_conntrack,
391         .prepare                = ipv6_prepare,
392 #if defined(CONFIG_NF_CT_NETLINK) || defined(CONFIG_NF_CT_NETLINK_MODULE)
393         .tuple_to_nfattr        = ipv6_tuple_to_nfattr,
394         .nfattr_to_tuple        = ipv6_nfattr_to_tuple,
395 #endif
396 #ifdef CONFIG_SYSCTL
397         .ctl_table_path         = nf_net_netfilter_sysctl_path,
398         .ctl_table              = nf_ct_ipv6_sysctl_table,
399 #endif
400         .get_features           = ipv6_get_features,
401         .me                     = THIS_MODULE,
402 };
403
404 MODULE_ALIAS("nf_conntrack-" __stringify(AF_INET6));
405 MODULE_LICENSE("GPL");
406 MODULE_AUTHOR("Yasuyuki KOZAKAI @USAGI <yasuyuki.kozakai@toshiba.co.jp>");
407
408 static int __init nf_conntrack_l3proto_ipv6_init(void)
409 {
410         int ret = 0;
411
412         need_conntrack();
413
414         ret = nf_ct_frag6_init();
415         if (ret < 0) {
416                 printk("nf_conntrack_ipv6: can't initialize frag6.\n");
417                 return ret;
418         }
419         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_tcp6);
420         if (ret < 0) {
421                 printk("nf_conntrack_ipv6: can't register tcp.\n");
422                 goto cleanup_frag6;
423         }
424
425         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_udp6);
426         if (ret < 0) {
427                 printk("nf_conntrack_ipv6: can't register udp.\n");
428                 goto cleanup_tcp;
429         }
430
431         ret = nf_conntrack_l4proto_register(&nf_conntrack_l4proto_icmpv6);
432         if (ret < 0) {
433                 printk("nf_conntrack_ipv6: can't register icmpv6.\n");
434                 goto cleanup_udp;
435         }
436
437         ret = nf_conntrack_l3proto_register(&nf_conntrack_l3proto_ipv6);
438         if (ret < 0) {
439                 printk("nf_conntrack_ipv6: can't register ipv6\n");
440                 goto cleanup_icmpv6;
441         }
442
443         ret = nf_register_hooks(ipv6_conntrack_ops,
444                                 ARRAY_SIZE(ipv6_conntrack_ops));
445         if (ret < 0) {
446                 printk("nf_conntrack_ipv6: can't register pre-routing defrag "
447                        "hook.\n");
448                 goto cleanup_ipv6;
449         }
450         return ret;
451
452  cleanup_ipv6:
453         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
454  cleanup_icmpv6:
455         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
456  cleanup_udp:
457         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
458  cleanup_tcp:
459         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
460  cleanup_frag6:
461         nf_ct_frag6_cleanup();
462         return ret;
463 }
464
465 static void __exit nf_conntrack_l3proto_ipv6_fini(void)
466 {
467         synchronize_net();
468         nf_unregister_hooks(ipv6_conntrack_ops, ARRAY_SIZE(ipv6_conntrack_ops));
469         nf_conntrack_l3proto_unregister(&nf_conntrack_l3proto_ipv6);
470         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_icmpv6);
471         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_udp6);
472         nf_conntrack_l4proto_unregister(&nf_conntrack_l4proto_tcp6);
473         nf_ct_frag6_cleanup();
474 }
475
476 module_init(nf_conntrack_l3proto_ipv6_init);
477 module_exit(nf_conntrack_l3proto_ipv6_fini);