[NET]: SPIN_LOCK_UNLOCKED cleanup in drivers/atm, net
[linux-2.6.git] / net / ipv6 / mip6.c
1 /*
2  * Copyright (C)2003-2006 Helsinki University of Technology
3  * Copyright (C)2003-2006 USAGI/WIDE Project
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 /*
20  * Authors:
21  *      Noriaki TAKAMIYA @USAGI
22  *      Masahide NAKAMURA @USAGI
23  */
24
25 #include <linux/module.h>
26 #include <linux/skbuff.h>
27 #include <linux/time.h>
28 #include <linux/ipv6.h>
29 #include <linux/icmpv6.h>
30 #include <net/sock.h>
31 #include <net/ipv6.h>
32 #include <net/ip6_checksum.h>
33 #include <net/xfrm.h>
34 #include <net/mip6.h>
35
36 static xfrm_address_t *mip6_xfrm_addr(struct xfrm_state *x, xfrm_address_t *addr)
37 {
38         return x->coaddr;
39 }
40
41 static inline unsigned int calc_padlen(unsigned int len, unsigned int n)
42 {
43         return (n - len + 16) & 0x7;
44 }
45
46 static inline void *mip6_padn(__u8 *data, __u8 padlen)
47 {
48         if (!data)
49                 return NULL;
50         if (padlen == 1) {
51                 data[0] = MIP6_OPT_PAD_1;
52         } else if (padlen > 1) {
53                 data[0] = MIP6_OPT_PAD_N;
54                 data[1] = padlen - 2;
55                 if (padlen > 2)
56                         memset(data+2, 0, data[1]);
57         }
58         return data + padlen;
59 }
60
61 static inline void mip6_param_prob(struct sk_buff *skb, int code, int pos)
62 {
63         icmpv6_send(skb, ICMPV6_PARAMPROB, code, pos, skb->dev);
64 }
65
66 static int mip6_mh_len(int type)
67 {
68         int len = 0;
69
70         switch (type) {
71         case IP6_MH_TYPE_BRR:
72                 len = 0;
73                 break;
74         case IP6_MH_TYPE_HOTI:
75         case IP6_MH_TYPE_COTI:
76         case IP6_MH_TYPE_BU:
77         case IP6_MH_TYPE_BACK:
78                 len = 1;
79                 break;
80         case IP6_MH_TYPE_HOT:
81         case IP6_MH_TYPE_COT:
82         case IP6_MH_TYPE_BERROR:
83                 len = 2;
84                 break;
85         }
86         return len;
87 }
88
89 int mip6_mh_filter(struct sock *sk, struct sk_buff *skb)
90 {
91         struct ip6_mh *mh;
92
93         if (!pskb_may_pull(skb, (skb_transport_offset(skb)) + 8) ||
94             !pskb_may_pull(skb, (skb_transport_offset(skb) +
95                                  ((skb_transport_header(skb)[1] + 1) << 3))))
96                 return -1;
97
98         mh = (struct ip6_mh *)skb_transport_header(skb);
99
100         if (mh->ip6mh_hdrlen < mip6_mh_len(mh->ip6mh_type)) {
101                 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH message too short: %d vs >=%d\n",
102                                mh->ip6mh_hdrlen, mip6_mh_len(mh->ip6mh_type));
103                 mip6_param_prob(skb, 0, ((&mh->ip6mh_hdrlen) -
104                                          skb_network_header(skb)));
105                 return -1;
106         }
107
108         if (mh->ip6mh_proto != IPPROTO_NONE) {
109                 LIMIT_NETDEBUG(KERN_DEBUG "mip6: MH invalid payload proto = %d\n",
110                                mh->ip6mh_proto);
111                 mip6_param_prob(skb, 0, ((&mh->ip6mh_proto) -
112                                          skb_network_header(skb)));
113                 return -1;
114         }
115
116         return 0;
117 }
118
119 struct mip6_report_rate_limiter {
120         spinlock_t lock;
121         struct timeval stamp;
122         int iif;
123         struct in6_addr src;
124         struct in6_addr dst;
125 };
126
127 static struct mip6_report_rate_limiter mip6_report_rl = {
128         .lock = __SPIN_LOCK_UNLOCKED(mip6_report_rl.lock)
129 };
130
131 static int mip6_destopt_input(struct xfrm_state *x, struct sk_buff *skb)
132 {
133         struct ipv6hdr *iph = ipv6_hdr(skb);
134         struct ipv6_destopt_hdr *destopt = (struct ipv6_destopt_hdr *)skb->data;
135
136         if (!ipv6_addr_equal(&iph->saddr, (struct in6_addr *)x->coaddr) &&
137             !ipv6_addr_any((struct in6_addr *)x->coaddr))
138                 return -ENOENT;
139
140         return destopt->nexthdr;
141 }
142
143 /* Destination Option Header is inserted.
144  * IP Header's src address is replaced with Home Address Option in
145  * Destination Option Header.
146  */
147 static int mip6_destopt_output(struct xfrm_state *x, struct sk_buff *skb)
148 {
149         struct ipv6hdr *iph;
150         struct ipv6_destopt_hdr *dstopt;
151         struct ipv6_destopt_hao *hao;
152         u8 nexthdr;
153         int len;
154
155         iph = (struct ipv6hdr *)skb->data;
156         iph->payload_len = htons(skb->len - sizeof(*iph));
157
158         nexthdr = *skb_network_header(skb);
159         *skb_network_header(skb) = IPPROTO_DSTOPTS;
160
161         dstopt = (struct ipv6_destopt_hdr *)skb_transport_header(skb);
162         dstopt->nexthdr = nexthdr;
163
164         hao = mip6_padn((char *)(dstopt + 1),
165                         calc_padlen(sizeof(*dstopt), 6));
166
167         hao->type = IPV6_TLV_HAO;
168         hao->length = sizeof(*hao) - 2;
169         BUG_TRAP(hao->length == 16);
170
171         len = ((char *)hao - (char *)dstopt) + sizeof(*hao);
172
173         memcpy(&hao->addr, &iph->saddr, sizeof(hao->addr));
174         memcpy(&iph->saddr, x->coaddr, sizeof(iph->saddr));
175
176         BUG_TRAP(len == x->props.header_len);
177         dstopt->hdrlen = (x->props.header_len >> 3) - 1;
178
179         return 0;
180 }
181
182 static inline int mip6_report_rl_allow(struct timeval *stamp,
183                                        struct in6_addr *dst,
184                                        struct in6_addr *src, int iif)
185 {
186         int allow = 0;
187
188         spin_lock_bh(&mip6_report_rl.lock);
189         if (mip6_report_rl.stamp.tv_sec != stamp->tv_sec ||
190             mip6_report_rl.stamp.tv_usec != stamp->tv_usec ||
191             mip6_report_rl.iif != iif ||
192             !ipv6_addr_equal(&mip6_report_rl.src, src) ||
193             !ipv6_addr_equal(&mip6_report_rl.dst, dst)) {
194                 mip6_report_rl.stamp.tv_sec = stamp->tv_sec;
195                 mip6_report_rl.stamp.tv_usec = stamp->tv_usec;
196                 mip6_report_rl.iif = iif;
197                 ipv6_addr_copy(&mip6_report_rl.src, src);
198                 ipv6_addr_copy(&mip6_report_rl.dst, dst);
199                 allow = 1;
200         }
201         spin_unlock_bh(&mip6_report_rl.lock);
202         return allow;
203 }
204
205 static int mip6_destopt_reject(struct xfrm_state *x, struct sk_buff *skb, struct flowi *fl)
206 {
207         struct inet6_skb_parm *opt = (struct inet6_skb_parm *)skb->cb;
208         struct ipv6_destopt_hao *hao = NULL;
209         struct xfrm_selector sel;
210         int offset;
211         struct timeval stamp;
212         int err = 0;
213
214         if (unlikely(fl->proto == IPPROTO_MH &&
215                      fl->fl_mh_type <= IP6_MH_TYPE_MAX))
216                 goto out;
217
218         if (likely(opt->dsthao)) {
219                 offset = ipv6_find_tlv(skb, opt->dsthao, IPV6_TLV_HAO);
220                 if (likely(offset >= 0))
221                         hao = (struct ipv6_destopt_hao *)
222                                         (skb_network_header(skb) + offset);
223         }
224
225         skb_get_timestamp(skb, &stamp);
226
227         if (!mip6_report_rl_allow(&stamp, &ipv6_hdr(skb)->daddr,
228                                   hao ? &hao->addr : &ipv6_hdr(skb)->saddr,
229                                   opt->iif))
230                 goto out;
231
232         memset(&sel, 0, sizeof(sel));
233         memcpy(&sel.daddr, (xfrm_address_t *)&ipv6_hdr(skb)->daddr,
234                sizeof(sel.daddr));
235         sel.prefixlen_d = 128;
236         memcpy(&sel.saddr, (xfrm_address_t *)&ipv6_hdr(skb)->saddr,
237                sizeof(sel.saddr));
238         sel.prefixlen_s = 128;
239         sel.family = AF_INET6;
240         sel.proto = fl->proto;
241         sel.dport = xfrm_flowi_dport(fl);
242         if (sel.dport)
243                 sel.dport_mask = htons(~0);
244         sel.sport = xfrm_flowi_sport(fl);
245         if (sel.sport)
246                 sel.sport_mask = htons(~0);
247         sel.ifindex = fl->oif;
248
249         err = km_report(IPPROTO_DSTOPTS, &sel,
250                         (hao ? (xfrm_address_t *)&hao->addr : NULL));
251
252  out:
253         return err;
254 }
255
256 static int mip6_destopt_offset(struct xfrm_state *x, struct sk_buff *skb,
257                                u8 **nexthdr)
258 {
259         u16 offset = sizeof(struct ipv6hdr);
260         struct ipv6_opt_hdr *exthdr =
261                                    (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
262         const unsigned char *nh = skb_network_header(skb);
263         unsigned int packet_len = skb->tail - skb->network_header;
264         int found_rhdr = 0;
265
266         *nexthdr = &ipv6_hdr(skb)->nexthdr;
267
268         while (offset + 1 <= packet_len) {
269
270                 switch (**nexthdr) {
271                 case NEXTHDR_HOP:
272                         break;
273                 case NEXTHDR_ROUTING:
274                         found_rhdr = 1;
275                         break;
276                 case NEXTHDR_DEST:
277                         /*
278                          * HAO MUST NOT appear more than once.
279                          * XXX: It is better to try to find by the end of
280                          * XXX: packet if HAO exists.
281                          */
282                         if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0) {
283                                 LIMIT_NETDEBUG(KERN_WARNING "mip6: hao exists already, override\n");
284                                 return offset;
285                         }
286
287                         if (found_rhdr)
288                                 return offset;
289
290                         break;
291                 default:
292                         return offset;
293                 }
294
295                 offset += ipv6_optlen(exthdr);
296                 *nexthdr = &exthdr->nexthdr;
297                 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
298         }
299
300         return offset;
301 }
302
303 static int mip6_destopt_init_state(struct xfrm_state *x)
304 {
305         if (x->id.spi) {
306                 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
307                        x->id.spi);
308                 return -EINVAL;
309         }
310         if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
311                 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
312                        __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
313                 return -EINVAL;
314         }
315
316         x->props.header_len = sizeof(struct ipv6_destopt_hdr) +
317                 calc_padlen(sizeof(struct ipv6_destopt_hdr), 6) +
318                 sizeof(struct ipv6_destopt_hao);
319         BUG_TRAP(x->props.header_len == 24);
320
321         return 0;
322 }
323
324 /*
325  * Do nothing about destroying since it has no specific operation for
326  * destination options header unlike IPsec protocols.
327  */
328 static void mip6_destopt_destroy(struct xfrm_state *x)
329 {
330 }
331
332 static struct xfrm_type mip6_destopt_type =
333 {
334         .description    = "MIP6DESTOPT",
335         .owner          = THIS_MODULE,
336         .proto          = IPPROTO_DSTOPTS,
337         .flags          = XFRM_TYPE_NON_FRAGMENT,
338         .init_state     = mip6_destopt_init_state,
339         .destructor     = mip6_destopt_destroy,
340         .input          = mip6_destopt_input,
341         .output         = mip6_destopt_output,
342         .reject         = mip6_destopt_reject,
343         .hdr_offset     = mip6_destopt_offset,
344         .local_addr     = mip6_xfrm_addr,
345 };
346
347 static int mip6_rthdr_input(struct xfrm_state *x, struct sk_buff *skb)
348 {
349         struct rt2_hdr *rt2 = (struct rt2_hdr *)skb->data;
350
351         if (!ipv6_addr_equal(&rt2->addr, (struct in6_addr *)x->coaddr) &&
352             !ipv6_addr_any((struct in6_addr *)x->coaddr))
353                 return -ENOENT;
354
355         return rt2->rt_hdr.nexthdr;
356 }
357
358 /* Routing Header type 2 is inserted.
359  * IP Header's dst address is replaced with Routing Header's Home Address.
360  */
361 static int mip6_rthdr_output(struct xfrm_state *x, struct sk_buff *skb)
362 {
363         struct ipv6hdr *iph;
364         struct rt2_hdr *rt2;
365         u8 nexthdr;
366
367         iph = (struct ipv6hdr *)skb->data;
368         iph->payload_len = htons(skb->len - sizeof(*iph));
369
370         nexthdr = *skb_network_header(skb);
371         *skb_network_header(skb) = IPPROTO_ROUTING;
372
373         rt2 = (struct rt2_hdr *)skb_transport_header(skb);
374         rt2->rt_hdr.nexthdr = nexthdr;
375         rt2->rt_hdr.hdrlen = (x->props.header_len >> 3) - 1;
376         rt2->rt_hdr.type = IPV6_SRCRT_TYPE_2;
377         rt2->rt_hdr.segments_left = 1;
378         memset(&rt2->reserved, 0, sizeof(rt2->reserved));
379
380         BUG_TRAP(rt2->rt_hdr.hdrlen == 2);
381
382         memcpy(&rt2->addr, &iph->daddr, sizeof(rt2->addr));
383         memcpy(&iph->daddr, x->coaddr, sizeof(iph->daddr));
384
385         return 0;
386 }
387
388 static int mip6_rthdr_offset(struct xfrm_state *x, struct sk_buff *skb,
389                              u8 **nexthdr)
390 {
391         u16 offset = sizeof(struct ipv6hdr);
392         struct ipv6_opt_hdr *exthdr =
393                                    (struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
394         const unsigned char *nh = skb_network_header(skb);
395         unsigned int packet_len = skb->tail - skb->network_header;
396         int found_rhdr = 0;
397
398         *nexthdr = &ipv6_hdr(skb)->nexthdr;
399
400         while (offset + 1 <= packet_len) {
401
402                 switch (**nexthdr) {
403                 case NEXTHDR_HOP:
404                         break;
405                 case NEXTHDR_ROUTING:
406                         if (offset + 3 <= packet_len) {
407                                 struct ipv6_rt_hdr *rt;
408                                 rt = (struct ipv6_rt_hdr *)(nh + offset);
409                                 if (rt->type != 0)
410                                         return offset;
411                         }
412                         found_rhdr = 1;
413                         break;
414                 case NEXTHDR_DEST:
415                         if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
416                                 return offset;
417
418                         if (found_rhdr)
419                                 return offset;
420
421                         break;
422                 default:
423                         return offset;
424                 }
425
426                 offset += ipv6_optlen(exthdr);
427                 *nexthdr = &exthdr->nexthdr;
428                 exthdr = (struct ipv6_opt_hdr *)(nh + offset);
429         }
430
431         return offset;
432 }
433
434 static int mip6_rthdr_init_state(struct xfrm_state *x)
435 {
436         if (x->id.spi) {
437                 printk(KERN_INFO "%s: spi is not 0: %u\n", __FUNCTION__,
438                        x->id.spi);
439                 return -EINVAL;
440         }
441         if (x->props.mode != XFRM_MODE_ROUTEOPTIMIZATION) {
442                 printk(KERN_INFO "%s: state's mode is not %u: %u\n",
443                        __FUNCTION__, XFRM_MODE_ROUTEOPTIMIZATION, x->props.mode);
444                 return -EINVAL;
445         }
446
447         x->props.header_len = sizeof(struct rt2_hdr);
448
449         return 0;
450 }
451
452 /*
453  * Do nothing about destroying since it has no specific operation for routing
454  * header type 2 unlike IPsec protocols.
455  */
456 static void mip6_rthdr_destroy(struct xfrm_state *x)
457 {
458 }
459
460 static struct xfrm_type mip6_rthdr_type =
461 {
462         .description    = "MIP6RT",
463         .owner          = THIS_MODULE,
464         .proto          = IPPROTO_ROUTING,
465         .flags          = XFRM_TYPE_NON_FRAGMENT,
466         .init_state     = mip6_rthdr_init_state,
467         .destructor     = mip6_rthdr_destroy,
468         .input          = mip6_rthdr_input,
469         .output         = mip6_rthdr_output,
470         .hdr_offset     = mip6_rthdr_offset,
471         .remote_addr    = mip6_xfrm_addr,
472 };
473
474 int __init mip6_init(void)
475 {
476         printk(KERN_INFO "Mobile IPv6\n");
477
478         if (xfrm_register_type(&mip6_destopt_type, AF_INET6) < 0) {
479                 printk(KERN_INFO "%s: can't add xfrm type(destopt)\n", __FUNCTION__);
480                 goto mip6_destopt_xfrm_fail;
481         }
482         if (xfrm_register_type(&mip6_rthdr_type, AF_INET6) < 0) {
483                 printk(KERN_INFO "%s: can't add xfrm type(rthdr)\n", __FUNCTION__);
484                 goto mip6_rthdr_xfrm_fail;
485         }
486         return 0;
487
488  mip6_rthdr_xfrm_fail:
489         xfrm_unregister_type(&mip6_destopt_type, AF_INET6);
490  mip6_destopt_xfrm_fail:
491         return -EAGAIN;
492 }
493
494 void __exit mip6_fini(void)
495 {
496         if (xfrm_unregister_type(&mip6_rthdr_type, AF_INET6) < 0)
497                 printk(KERN_INFO "%s: can't remove xfrm type(rthdr)\n", __FUNCTION__);
498         if (xfrm_unregister_type(&mip6_destopt_type, AF_INET6) < 0)
499                 printk(KERN_INFO "%s: can't remove xfrm type(destopt)\n", __FUNCTION__);
500 }