Merge master.kernel.org:/pub/scm/linux/kernel/git/lethal/sh-2.6
[linux-2.6] / net / sctp / ipv6.c
1 /* SCTP kernel reference Implementation
2  * (C) Copyright IBM Corp. 2002, 2004
3  * Copyright (c) 2001 Nokia, Inc.
4  * Copyright (c) 2001 La Monte H.P. Yarroll
5  * Copyright (c) 2002-2003 Intel Corp.
6  *
7  * This file is part of the SCTP kernel reference Implementation
8  *
9  * SCTP over IPv6.
10  *
11  * The SCTP reference implementation is free software;
12  * you can redistribute it and/or modify it under the terms of
13  * the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * The SCTP reference implementation is distributed in the hope that it
18  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19  *                 ************************
20  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21  * See the GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with GNU CC; see the file COPYING.  If not, write to
25  * the Free Software Foundation, 59 Temple Place - Suite 330,
26  * Boston, MA 02111-1307, USA.
27  *
28  * Please send any bug reports or fixes you make to the
29  * email address(es):
30  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
31  *
32  * Or submit a bug report through the following website:
33  *    http://www.sf.net/projects/lksctp
34  *
35  * Written or modified by:
36  *    Le Yanqun             <yanqun.le@nokia.com>
37  *    Hui Huang             <hui.huang@nokia.com>
38  *    La Monte H.P. Yarroll <piggy@acm.org>
39  *    Sridhar Samudrala     <sri@us.ibm.com>
40  *    Jon Grimm             <jgrimm@us.ibm.com>
41  *    Ardelle Fan           <ardelle.fan@intel.com>
42  *
43  * Based on:
44  *      linux/net/ipv6/tcp_ipv6.c
45  *
46  * Any bugs reported given to us we will try to fix... any fixes shared will
47  * be incorporated into the next SCTP release.
48  */
49
50 #include <linux/module.h>
51 #include <linux/errno.h>
52 #include <linux/types.h>
53 #include <linux/socket.h>
54 #include <linux/sockios.h>
55 #include <linux/net.h>
56 #include <linux/in.h>
57 #include <linux/in6.h>
58 #include <linux/netdevice.h>
59 #include <linux/init.h>
60 #include <linux/ipsec.h>
61
62 #include <linux/ipv6.h>
63 #include <linux/icmpv6.h>
64 #include <linux/random.h>
65 #include <linux/seq_file.h>
66
67 #include <net/protocol.h>
68 #include <net/ndisc.h>
69 #include <net/ip.h>
70 #include <net/ipv6.h>
71 #include <net/transp_v6.h>
72 #include <net/addrconf.h>
73 #include <net/ip6_route.h>
74 #include <net/inet_common.h>
75 #include <net/inet_ecn.h>
76 #include <net/sctp/sctp.h>
77
78 #include <asm/uaccess.h>
79
80 /* Event handler for inet6 address addition/deletion events.  */
81 static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
82                                 void *ptr)
83 {
84         struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
85         struct sctp_sockaddr_entry *addr;
86         struct list_head *pos, *temp;
87
88         switch (ev) {
89         case NETDEV_UP:
90                 addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
91                 if (addr) {
92                         addr->a.v6.sin6_family = AF_INET6;
93                         addr->a.v6.sin6_port = 0;
94                         memcpy(&addr->a.v6.sin6_addr, &ifa->addr,
95                                  sizeof(struct in6_addr));
96                         addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
97                         list_add_tail(&addr->list, &sctp_local_addr_list);
98                 }
99                 break;
100         case NETDEV_DOWN:
101                 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
102                         addr = list_entry(pos, struct sctp_sockaddr_entry, list);
103                         if (ipv6_addr_equal(&addr->a.v6.sin6_addr, &ifa->addr)) {
104                                 list_del(pos);
105                                 kfree(addr);
106                                 break;
107                         }
108                 }
109
110                 break;
111         }
112
113         return NOTIFY_DONE;
114 }
115
116 static struct notifier_block sctp_inet6addr_notifier = {
117         .notifier_call = sctp_inet6addr_event,
118 };
119
120 /* ICMP error handler. */
121 SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
122                              int type, int code, int offset, __be32 info)
123 {
124         struct inet6_dev *idev;
125         struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
126         struct sctphdr *sh = (struct sctphdr *)(skb->data + offset);
127         struct sock *sk;
128         struct sctp_association *asoc;
129         struct sctp_transport *transport;
130         struct ipv6_pinfo *np;
131         char *saveip, *savesctp;
132         int err;
133
134         idev = in6_dev_get(skb->dev);
135
136         /* Fix up skb to look at the embedded net header. */
137         saveip = skb->nh.raw;
138         savesctp  = skb->h.raw;
139         skb->nh.ipv6h = iph;
140         skb->h.raw = (char *)sh;
141         sk = sctp_err_lookup(AF_INET6, skb, sh, &asoc, &transport);
142         /* Put back, the original pointers. */
143         skb->nh.raw = saveip;
144         skb->h.raw = savesctp;
145         if (!sk) {
146                 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
147                 goto out;
148         }
149
150         /* Warning:  The sock lock is held.  Remember to call
151          * sctp_err_finish!
152          */
153
154         switch (type) {
155         case ICMPV6_PKT_TOOBIG:
156                 sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
157                 goto out_unlock;
158         case ICMPV6_PARAMPROB:
159                 if (ICMPV6_UNK_NEXTHDR == code) {
160                         sctp_icmp_proto_unreachable(sk, asoc, transport);
161                         goto out_unlock;
162                 }
163                 break;
164         default:
165                 break;
166         }
167
168         np = inet6_sk(sk);
169         icmpv6_err_convert(type, code, &err);
170         if (!sock_owned_by_user(sk) && np->recverr) {
171                 sk->sk_err = err;
172                 sk->sk_error_report(sk);
173         } else {  /* Only an error on timeout */
174                 sk->sk_err_soft = err;
175         }
176
177 out_unlock:
178         sctp_err_finish(sk, asoc);
179 out:
180         if (likely(idev != NULL))
181                 in6_dev_put(idev);
182 }
183
184 /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
185 static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
186                         int ipfragok)
187 {
188         struct sock *sk = skb->sk;
189         struct ipv6_pinfo *np = inet6_sk(sk);
190         struct flowi fl;
191
192         memset(&fl, 0, sizeof(fl));
193
194         fl.proto = sk->sk_protocol;
195
196         /* Fill in the dest address from the route entry passed with the skb
197          * and the source address from the transport.
198          */
199         ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr.v6.sin6_addr);
200         ipv6_addr_copy(&fl.fl6_src, &transport->saddr.v6.sin6_addr);
201
202         fl.fl6_flowlabel = np->flow_label;
203         IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
204         if (ipv6_addr_type(&fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
205                 fl.oif = transport->saddr.v6.sin6_scope_id;
206         else
207                 fl.oif = sk->sk_bound_dev_if;
208
209         if (np->opt && np->opt->srcrt) {
210                 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
211                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
212         }
213
214         SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
215                           "src:" NIP6_FMT " dst:" NIP6_FMT "\n",
216                           __FUNCTION__, skb, skb->len,
217                           NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
218
219         SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
220
221         return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
222 }
223
224 /* Returns the dst cache entry for the given source and destination ip
225  * addresses.
226  */
227 static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
228                                          union sctp_addr *daddr,
229                                          union sctp_addr *saddr)
230 {
231         struct dst_entry *dst;
232         struct flowi fl;
233
234         memset(&fl, 0, sizeof(fl));
235         ipv6_addr_copy(&fl.fl6_dst, &daddr->v6.sin6_addr);
236         if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
237                 fl.oif = daddr->v6.sin6_scope_id;
238
239
240         SCTP_DEBUG_PRINTK("%s: DST=" NIP6_FMT " ",
241                           __FUNCTION__, NIP6(fl.fl6_dst));
242
243         if (saddr) {
244                 ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
245                 SCTP_DEBUG_PRINTK(
246                         "SRC=" NIP6_FMT " - ",
247                         NIP6(fl.fl6_src));
248         }
249
250         dst = ip6_route_output(NULL, &fl);
251         if (!dst->error) {
252                 struct rt6_info *rt;
253                 rt = (struct rt6_info *)dst;
254                 SCTP_DEBUG_PRINTK(
255                         "rt6_dst:" NIP6_FMT " rt6_src:" NIP6_FMT "\n",
256                         NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
257                 return dst;
258         }
259         SCTP_DEBUG_PRINTK("NO ROUTE\n");
260         dst_release(dst);
261         return NULL;
262 }
263
264 /* Returns the number of consecutive initial bits that match in the 2 ipv6
265  * addresses.
266  */
267 static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
268                                          union sctp_addr *s2)
269 {
270         struct in6_addr *a1 = &s1->v6.sin6_addr;
271         struct in6_addr *a2 = &s2->v6.sin6_addr;
272         int i, j;
273
274         for (i = 0; i < 4 ; i++) {
275                 __be32 a1xora2;
276
277                 a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
278
279                 if ((j = fls(ntohl(a1xora2))))
280                         return (i * 32 + 32 - j);
281         }
282
283         return (i*32);
284 }
285
286 /* Fills in the source address(saddr) based on the destination address(daddr)
287  * and asoc's bind address list.
288  */
289 static void sctp_v6_get_saddr(struct sctp_association *asoc,
290                               struct dst_entry *dst,
291                               union sctp_addr *daddr,
292                               union sctp_addr *saddr)
293 {
294         struct sctp_bind_addr *bp;
295         rwlock_t *addr_lock;
296         struct sctp_sockaddr_entry *laddr;
297         struct list_head *pos;
298         sctp_scope_t scope;
299         union sctp_addr *baddr = NULL;
300         __u8 matchlen = 0;
301         __u8 bmatchlen;
302
303         SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
304                           "daddr:" NIP6_FMT " ",
305                           __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
306
307         if (!asoc) {
308                 ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
309                 SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: " NIP6_FMT "\n",
310                                   NIP6(saddr->v6.sin6_addr));
311                 return;
312         }
313
314         scope = sctp_scope(daddr);
315
316         bp = &asoc->base.bind_addr;
317         addr_lock = &asoc->base.addr_lock;
318
319         /* Go through the bind address list and find the best source address
320          * that matches the scope of the destination address.
321          */
322         sctp_read_lock(addr_lock);
323         list_for_each(pos, &bp->address_list) {
324                 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
325                 if ((laddr->use_as_src) &&
326                     (laddr->a.sa.sa_family == AF_INET6) &&
327                     (scope <= sctp_scope(&laddr->a))) {
328                         bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
329                         if (!baddr || (matchlen < bmatchlen)) {
330                                 baddr = &laddr->a;
331                                 matchlen = bmatchlen;
332                         }
333                 }
334         }
335
336         if (baddr) {
337                 memcpy(saddr, baddr, sizeof(union sctp_addr));
338                 SCTP_DEBUG_PRINTK("saddr: " NIP6_FMT "\n",
339                                   NIP6(saddr->v6.sin6_addr));
340         } else {
341                 printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
342                        "address for the dest:" NIP6_FMT "\n",
343                        __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
344         }
345
346         sctp_read_unlock(addr_lock);
347 }
348
349 /* Make a copy of all potential local addresses. */
350 static void sctp_v6_copy_addrlist(struct list_head *addrlist,
351                                   struct net_device *dev)
352 {
353         struct inet6_dev *in6_dev;
354         struct inet6_ifaddr *ifp;
355         struct sctp_sockaddr_entry *addr;
356
357         rcu_read_lock();
358         if ((in6_dev = __in6_dev_get(dev)) == NULL) {
359                 rcu_read_unlock();
360                 return;
361         }
362
363         read_lock(&in6_dev->lock);
364         for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
365                 /* Add the address to the local list.  */
366                 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
367                 if (addr) {
368                         addr->a.v6.sin6_family = AF_INET6;
369                         addr->a.v6.sin6_port = 0;
370                         addr->a.v6.sin6_addr = ifp->addr;
371                         addr->a.v6.sin6_scope_id = dev->ifindex;
372                         INIT_LIST_HEAD(&addr->list);
373                         list_add_tail(&addr->list, addrlist);
374                 }
375         }
376
377         read_unlock(&in6_dev->lock);
378         rcu_read_unlock();
379 }
380
381 /* Initialize a sockaddr_storage from in incoming skb. */
382 static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
383                              int is_saddr)
384 {
385         void *from;
386         __be16 *port;
387         struct sctphdr *sh;
388
389         port = &addr->v6.sin6_port;
390         addr->v6.sin6_family = AF_INET6;
391         addr->v6.sin6_flowinfo = 0; /* FIXME */
392         addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
393
394         sh = (struct sctphdr *) skb->h.raw;
395         if (is_saddr) {
396                 *port  = sh->source;
397                 from = &skb->nh.ipv6h->saddr;
398         } else {
399                 *port = sh->dest;
400                 from = &skb->nh.ipv6h->daddr;
401         }
402         ipv6_addr_copy(&addr->v6.sin6_addr, from);
403 }
404
405 /* Initialize an sctp_addr from a socket. */
406 static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
407 {
408         addr->v6.sin6_family = AF_INET6;
409         addr->v6.sin6_port = 0;
410         addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
411 }
412
413 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
414 static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
415 {
416         if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
417                 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
418                 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
419                 inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
420                 inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
421                         addr->v4.sin_addr.s_addr;
422         } else {
423                 inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
424         }
425 }
426
427 /* Initialize sk->sk_daddr from sctp_addr. */
428 static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
429 {
430         if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
431                 inet6_sk(sk)->daddr.s6_addr32[0] = 0;
432                 inet6_sk(sk)->daddr.s6_addr32[1] = 0;
433                 inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
434                 inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
435         } else {
436                 inet6_sk(sk)->daddr = addr->v6.sin6_addr;
437         }
438 }
439
440 /* Initialize a sctp_addr from an address parameter. */
441 static void sctp_v6_from_addr_param(union sctp_addr *addr,
442                                     union sctp_addr_param *param,
443                                     __be16 port, int iif)
444 {
445         addr->v6.sin6_family = AF_INET6;
446         addr->v6.sin6_port = port;
447         addr->v6.sin6_flowinfo = 0; /* BUG */
448         ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
449         addr->v6.sin6_scope_id = iif;
450 }
451
452 /* Initialize an address parameter from a sctp_addr and return the length
453  * of the address parameter.
454  */
455 static int sctp_v6_to_addr_param(const union sctp_addr *addr,
456                                  union sctp_addr_param *param)
457 {
458         int length = sizeof(sctp_ipv6addr_param_t);
459
460         param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
461         param->v6.param_hdr.length = htons(length);
462         ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
463
464         return length;
465 }
466
467 /* Initialize a sctp_addr from a dst_entry. */
468 static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
469                               __be16 port)
470 {
471         struct rt6_info *rt = (struct rt6_info *)dst;
472         addr->sa.sa_family = AF_INET6;
473         addr->v6.sin6_port = port;
474         ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
475 }
476
477 /* Compare addresses exactly.
478  * v4-mapped-v6 is also in consideration.
479  */
480 static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
481                             const union sctp_addr *addr2)
482 {
483         if (addr1->sa.sa_family != addr2->sa.sa_family) {
484                 if (addr1->sa.sa_family == AF_INET &&
485                     addr2->sa.sa_family == AF_INET6 &&
486                     IPV6_ADDR_MAPPED == ipv6_addr_type(&addr2->v6.sin6_addr)) {
487                         if (addr2->v6.sin6_port == addr1->v4.sin_port &&
488                             addr2->v6.sin6_addr.s6_addr32[3] ==
489                             addr1->v4.sin_addr.s_addr)
490                                 return 1;
491                 }
492                 if (addr2->sa.sa_family == AF_INET &&
493                     addr1->sa.sa_family == AF_INET6 &&
494                     IPV6_ADDR_MAPPED == ipv6_addr_type(&addr1->v6.sin6_addr)) {
495                         if (addr1->v6.sin6_port == addr2->v4.sin_port &&
496                             addr1->v6.sin6_addr.s6_addr32[3] ==
497                             addr2->v4.sin_addr.s_addr)
498                                 return 1;
499                 }
500                 return 0;
501         }
502         if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
503                 return 0;
504         /* If this is a linklocal address, compare the scope_id. */
505         if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
506                 if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
507                     (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
508                         return 0;
509                 }
510         }
511
512         return 1;
513 }
514
515 /* Initialize addr struct to INADDR_ANY. */
516 static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
517 {
518         memset(addr, 0x00, sizeof(union sctp_addr));
519         addr->v6.sin6_family = AF_INET6;
520         addr->v6.sin6_port = port;
521 }
522
523 /* Is this a wildcard address? */
524 static int sctp_v6_is_any(const union sctp_addr *addr)
525 {
526         return ipv6_addr_any(&addr->v6.sin6_addr);
527 }
528
529 /* Should this be available for binding?   */
530 static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
531 {
532         int type;
533         struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
534
535         type = ipv6_addr_type(in6);
536         if (IPV6_ADDR_ANY == type)
537                 return 1;
538         if (type == IPV6_ADDR_MAPPED) {
539                 if (sp && !sp->v4mapped)
540                         return 0;
541                 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
542                         return 0;
543                 sctp_v6_map_v4(addr);
544                 return sctp_get_af_specific(AF_INET)->available(addr, sp);
545         }
546         if (!(type & IPV6_ADDR_UNICAST))
547                 return 0;
548
549         return ipv6_chk_addr(in6, NULL, 0);
550 }
551
552 /* This function checks if the address is a valid address to be used for
553  * SCTP.
554  *
555  * Output:
556  * Return 0 - If the address is a non-unicast or an illegal address.
557  * Return 1 - If the address is a unicast.
558  */
559 static int sctp_v6_addr_valid(union sctp_addr *addr,
560                               struct sctp_sock *sp,
561                               const struct sk_buff *skb)
562 {
563         int ret = ipv6_addr_type(&addr->v6.sin6_addr);
564
565         /* Support v4-mapped-v6 address. */
566         if (ret == IPV6_ADDR_MAPPED) {
567                 /* Note: This routine is used in input, so v4-mapped-v6
568                  * are disallowed here when there is no sctp_sock.
569                  */
570                 if (!sp || !sp->v4mapped)
571                         return 0;
572                 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
573                         return 0;
574                 sctp_v6_map_v4(addr);
575                 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
576         }
577
578         /* Is this a non-unicast address */
579         if (!(ret & IPV6_ADDR_UNICAST))
580                 return 0;
581
582         return 1;
583 }
584
585 /* What is the scope of 'addr'?  */
586 static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
587 {
588         int v6scope;
589         sctp_scope_t retval;
590
591         /* The IPv6 scope is really a set of bit fields.
592          * See IFA_* in <net/if_inet6.h>.  Map to a generic SCTP scope.
593          */
594
595         v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
596         switch (v6scope) {
597         case IFA_HOST:
598                 retval = SCTP_SCOPE_LOOPBACK;
599                 break;
600         case IFA_LINK:
601                 retval = SCTP_SCOPE_LINK;
602                 break;
603         case IFA_SITE:
604                 retval = SCTP_SCOPE_PRIVATE;
605                 break;
606         default:
607                 retval = SCTP_SCOPE_GLOBAL;
608                 break;
609         };
610
611         return retval;
612 }
613
614 /* Create and initialize a new sk for the socket to be returned by accept(). */
615 static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
616                                              struct sctp_association *asoc)
617 {
618         struct inet_sock *inet = inet_sk(sk);
619         struct sock *newsk;
620         struct inet_sock *newinet;
621         struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
622         struct sctp6_sock *newsctp6sk;
623
624         newsk = sk_alloc(PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
625         if (!newsk)
626                 goto out;
627
628         sock_init_data(NULL, newsk);
629
630         newsk->sk_type = SOCK_STREAM;
631
632         newsk->sk_prot = sk->sk_prot;
633         newsk->sk_no_check = sk->sk_no_check;
634         newsk->sk_reuse = sk->sk_reuse;
635
636         newsk->sk_destruct = inet_sock_destruct;
637         newsk->sk_family = PF_INET6;
638         newsk->sk_protocol = IPPROTO_SCTP;
639         newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
640         newsk->sk_shutdown = sk->sk_shutdown;
641         sock_reset_flag(sk, SOCK_ZAPPED);
642
643         newsctp6sk = (struct sctp6_sock *)newsk;
644         inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
645
646         newinet = inet_sk(newsk);
647         newnp = inet6_sk(newsk);
648
649         memcpy(newnp, np, sizeof(struct ipv6_pinfo));
650
651         /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
652          * and getpeername().
653          */
654         newinet->sport = inet->sport;
655         newnp->saddr = np->saddr;
656         newnp->rcv_saddr = np->rcv_saddr;
657         newinet->dport = htons(asoc->peer.port);
658         sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
659
660         /* Init the ipv4 part of the socket since we can have sockets
661          * using v6 API for ipv4.
662          */
663         newinet->uc_ttl = -1;
664         newinet->mc_loop = 1;
665         newinet->mc_ttl = 1;
666         newinet->mc_index = 0;
667         newinet->mc_list = NULL;
668
669         if (ipv4_config.no_pmtu_disc)
670                 newinet->pmtudisc = IP_PMTUDISC_DONT;
671         else
672                 newinet->pmtudisc = IP_PMTUDISC_WANT;
673
674         sk_refcnt_debug_inc(newsk);
675
676         if (newsk->sk_prot->init(newsk)) {
677                 sk_common_release(newsk);
678                 newsk = NULL;
679         }
680
681 out:
682         return newsk;
683 }
684
685 /* Map v4 address to mapped v6 address */
686 static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
687 {
688         if (sp->v4mapped && AF_INET == addr->sa.sa_family)
689                 sctp_v4_map_v6(addr);
690 }
691
692 /* Where did this skb come from?  */
693 static int sctp_v6_skb_iif(const struct sk_buff *skb)
694 {
695         struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
696         return opt->iif;
697 }
698
699 /* Was this packet marked by Explicit Congestion Notification? */
700 static int sctp_v6_is_ce(const struct sk_buff *skb)
701 {
702         return *((__u32 *)(skb->nh.ipv6h)) & htonl(1<<20);
703 }
704
705 /* Dump the v6 addr to the seq file. */
706 static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
707 {
708         seq_printf(seq, NIP6_FMT " ", NIP6(addr->v6.sin6_addr));
709 }
710
711 /* Initialize a PF_INET6 socket msg_name. */
712 static void sctp_inet6_msgname(char *msgname, int *addr_len)
713 {
714         struct sockaddr_in6 *sin6;
715
716         sin6 = (struct sockaddr_in6 *)msgname;
717         sin6->sin6_family = AF_INET6;
718         sin6->sin6_flowinfo = 0;
719         sin6->sin6_scope_id = 0; /*FIXME */
720         *addr_len = sizeof(struct sockaddr_in6);
721 }
722
723 /* Initialize a PF_INET msgname from a ulpevent. */
724 static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
725                                      char *msgname, int *addrlen)
726 {
727         struct sockaddr_in6 *sin6, *sin6from;
728
729         if (msgname) {
730                 union sctp_addr *addr;
731                 struct sctp_association *asoc;
732
733                 asoc = event->asoc;
734                 sctp_inet6_msgname(msgname, addrlen);
735                 sin6 = (struct sockaddr_in6 *)msgname;
736                 sin6->sin6_port = htons(asoc->peer.port);
737                 addr = &asoc->peer.primary_addr;
738
739                 /* Note: If we go to a common v6 format, this code
740                  * will change.
741                  */
742
743                 /* Map ipv4 address into v4-mapped-on-v6 address.  */
744                 if (sctp_sk(asoc->base.sk)->v4mapped &&
745                     AF_INET == addr->sa.sa_family) {
746                         sctp_v4_map_v6((union sctp_addr *)sin6);
747                         sin6->sin6_addr.s6_addr32[3] =
748                                 addr->v4.sin_addr.s_addr;
749                         return;
750                 }
751
752                 sin6from = &asoc->peer.primary_addr.v6;
753                 ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
754                 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
755                         sin6->sin6_scope_id = sin6from->sin6_scope_id;
756         }
757 }
758
759 /* Initialize a msg_name from an inbound skb. */
760 static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
761                                    int *addr_len)
762 {
763         struct sctphdr *sh;
764         struct sockaddr_in6 *sin6;
765
766         if (msgname) {
767                 sctp_inet6_msgname(msgname, addr_len);
768                 sin6 = (struct sockaddr_in6 *)msgname;
769                 sh = (struct sctphdr *)skb->h.raw;
770                 sin6->sin6_port = sh->source;
771
772                 /* Map ipv4 address into v4-mapped-on-v6 address. */
773                 if (sctp_sk(skb->sk)->v4mapped &&
774                     skb->nh.iph->version == 4) {
775                         sctp_v4_map_v6((union sctp_addr *)sin6);
776                         sin6->sin6_addr.s6_addr32[3] = skb->nh.iph->saddr;
777                         return;
778                 }
779
780                 /* Otherwise, just copy the v6 address. */
781                 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
782                 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
783                         struct sctp_ulpevent *ev = sctp_skb2event(skb);
784                         sin6->sin6_scope_id = ev->iif;
785                 }
786         }
787 }
788
789 /* Do we support this AF? */
790 static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
791 {
792         switch (family) {
793         case AF_INET6:
794                 return 1;
795         /* v4-mapped-v6 addresses */
796         case AF_INET:
797                 if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
798                         return 1;
799         default:
800                 return 0;
801         }
802 }
803
804 /* Address matching with wildcards allowed.  This extra level
805  * of indirection lets us choose whether a PF_INET6 should
806  * disallow any v4 addresses if we so choose.
807  */
808 static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
809                                const union sctp_addr *addr2,
810                                struct sctp_sock *opt)
811 {
812         struct sctp_af *af1, *af2;
813
814         af1 = sctp_get_af_specific(addr1->sa.sa_family);
815         af2 = sctp_get_af_specific(addr2->sa.sa_family);
816
817         if (!af1 || !af2)
818                 return 0;
819         /* Today, wildcard AF_INET/AF_INET6. */
820         if (sctp_is_any(addr1) || sctp_is_any(addr2))
821                 return 1;
822
823         if (addr1->sa.sa_family != addr2->sa.sa_family)
824                 return 0;
825
826         return af1->cmp_addr(addr1, addr2);
827 }
828
829 /* Verify that the provided sockaddr looks bindable.   Common verification,
830  * has already been taken care of.
831  */
832 static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
833 {
834         struct sctp_af *af;
835
836         /* ASSERT: address family has already been verified. */
837         if (addr->sa.sa_family != AF_INET6)
838                 af = sctp_get_af_specific(addr->sa.sa_family);
839         else {
840                 int type = ipv6_addr_type(&addr->v6.sin6_addr);
841                 struct net_device *dev;
842
843                 if (type & IPV6_ADDR_LINKLOCAL) {
844                         if (!addr->v6.sin6_scope_id)
845                                 return 0;
846                         dev = dev_get_by_index(addr->v6.sin6_scope_id);
847                         if (!dev)
848                                 return 0;
849                         dev_put(dev);
850                 }
851                 af = opt->pf->af;
852         }
853         return af->available(addr, opt);
854 }
855
856 /* Verify that the provided sockaddr looks sendable.   Common verification,
857  * has already been taken care of.
858  */
859 static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
860 {
861         struct sctp_af *af = NULL;
862
863         /* ASSERT: address family has already been verified. */
864         if (addr->sa.sa_family != AF_INET6)
865                 af = sctp_get_af_specific(addr->sa.sa_family);
866         else {
867                 int type = ipv6_addr_type(&addr->v6.sin6_addr);
868                 struct net_device *dev;
869
870                 if (type & IPV6_ADDR_LINKLOCAL) {
871                         if (!addr->v6.sin6_scope_id)
872                                 return 0;
873                         dev = dev_get_by_index(addr->v6.sin6_scope_id);
874                         if (!dev)
875                                 return 0;
876                         dev_put(dev);
877                 }
878                 af = opt->pf->af;
879         }
880
881         return af != NULL;
882 }
883
884 /* Fill in Supported Address Type information for INIT and INIT-ACK
885  * chunks.   Note: In the future, we may want to look at sock options
886  * to determine whether a PF_INET6 socket really wants to have IPV4
887  * addresses.
888  * Returns number of addresses supported.
889  */
890 static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
891                                       __be16 *types)
892 {
893         types[0] = SCTP_PARAM_IPV4_ADDRESS;
894         types[1] = SCTP_PARAM_IPV6_ADDRESS;
895         return 2;
896 }
897
898 static const struct proto_ops inet6_seqpacket_ops = {
899         .family            = PF_INET6,
900         .owner             = THIS_MODULE,
901         .release           = inet6_release,
902         .bind              = inet6_bind,
903         .connect           = inet_dgram_connect,
904         .socketpair        = sock_no_socketpair,
905         .accept            = inet_accept,
906         .getname           = inet6_getname,
907         .poll              = sctp_poll,
908         .ioctl             = inet6_ioctl,
909         .listen            = sctp_inet_listen,
910         .shutdown          = inet_shutdown,
911         .setsockopt        = sock_common_setsockopt,
912         .getsockopt        = sock_common_getsockopt,
913         .sendmsg           = inet_sendmsg,
914         .recvmsg           = sock_common_recvmsg,
915         .mmap              = sock_no_mmap,
916 #ifdef CONFIG_COMPAT
917         .compat_setsockopt = compat_sock_common_setsockopt,
918         .compat_getsockopt = compat_sock_common_getsockopt,
919 #endif
920 };
921
922 static struct inet_protosw sctpv6_seqpacket_protosw = {
923         .type          = SOCK_SEQPACKET,
924         .protocol      = IPPROTO_SCTP,
925         .prot          = &sctpv6_prot,
926         .ops           = &inet6_seqpacket_ops,
927         .capability    = -1,
928         .no_check      = 0,
929         .flags         = SCTP_PROTOSW_FLAG
930 };
931 static struct inet_protosw sctpv6_stream_protosw = {
932         .type          = SOCK_STREAM,
933         .protocol      = IPPROTO_SCTP,
934         .prot          = &sctpv6_prot,
935         .ops           = &inet6_seqpacket_ops,
936         .capability    = -1,
937         .no_check      = 0,
938         .flags         = SCTP_PROTOSW_FLAG,
939 };
940
941 static int sctp6_rcv(struct sk_buff **pskb)
942 {
943         return sctp_rcv(*pskb) ? -1 : 0;
944 }
945
946 static struct inet6_protocol sctpv6_protocol = {
947         .handler      = sctp6_rcv,
948         .err_handler  = sctp_v6_err,
949         .flags        = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
950 };
951
952 static struct sctp_af sctp_ipv6_specific = {
953         .sa_family         = AF_INET6,
954         .sctp_xmit         = sctp_v6_xmit,
955         .setsockopt        = ipv6_setsockopt,
956         .getsockopt        = ipv6_getsockopt,
957         .get_dst           = sctp_v6_get_dst,
958         .get_saddr         = sctp_v6_get_saddr,
959         .copy_addrlist     = sctp_v6_copy_addrlist,
960         .from_skb          = sctp_v6_from_skb,
961         .from_sk           = sctp_v6_from_sk,
962         .to_sk_saddr       = sctp_v6_to_sk_saddr,
963         .to_sk_daddr       = sctp_v6_to_sk_daddr,
964         .from_addr_param   = sctp_v6_from_addr_param,
965         .to_addr_param     = sctp_v6_to_addr_param,
966         .dst_saddr         = sctp_v6_dst_saddr,
967         .cmp_addr          = sctp_v6_cmp_addr,
968         .scope             = sctp_v6_scope,
969         .addr_valid        = sctp_v6_addr_valid,
970         .inaddr_any        = sctp_v6_inaddr_any,
971         .is_any            = sctp_v6_is_any,
972         .available         = sctp_v6_available,
973         .skb_iif           = sctp_v6_skb_iif,
974         .is_ce             = sctp_v6_is_ce,
975         .seq_dump_addr     = sctp_v6_seq_dump_addr,
976         .net_header_len    = sizeof(struct ipv6hdr),
977         .sockaddr_len      = sizeof(struct sockaddr_in6),
978 #ifdef CONFIG_COMPAT
979         .compat_setsockopt = compat_ipv6_setsockopt,
980         .compat_getsockopt = compat_ipv6_getsockopt,
981 #endif
982 };
983
984 static struct sctp_pf sctp_pf_inet6_specific = {
985         .event_msgname = sctp_inet6_event_msgname,
986         .skb_msgname   = sctp_inet6_skb_msgname,
987         .af_supported  = sctp_inet6_af_supported,
988         .cmp_addr      = sctp_inet6_cmp_addr,
989         .bind_verify   = sctp_inet6_bind_verify,
990         .send_verify   = sctp_inet6_send_verify,
991         .supported_addrs = sctp_inet6_supported_addrs,
992         .create_accept_sk = sctp_v6_create_accept_sk,
993         .addr_v4map    = sctp_v6_addr_v4map,
994         .af            = &sctp_ipv6_specific,
995 };
996
997 /* Initialize IPv6 support and register with inet6 stack.  */
998 int sctp_v6_init(void)
999 {
1000         int rc = proto_register(&sctpv6_prot, 1);
1001
1002         if (rc)
1003                 goto out;
1004         /* Register inet6 protocol. */
1005         rc = -EAGAIN;
1006         if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1007                 goto out_unregister_sctp_proto;
1008
1009         /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
1010         inet6_register_protosw(&sctpv6_seqpacket_protosw);
1011         inet6_register_protosw(&sctpv6_stream_protosw);
1012
1013         /* Register the SCTP specific PF_INET6 functions. */
1014         sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
1015
1016         /* Register the SCTP specific AF_INET6 functions. */
1017         sctp_register_af(&sctp_ipv6_specific);
1018
1019         /* Register notifier for inet6 address additions/deletions. */
1020         register_inet6addr_notifier(&sctp_inet6addr_notifier);
1021         rc = 0;
1022 out:
1023         return rc;
1024 out_unregister_sctp_proto:
1025         proto_unregister(&sctpv6_prot);
1026         goto out;
1027 }
1028
1029 /* IPv6 specific exit support. */
1030 void sctp_v6_exit(void)
1031 {
1032         list_del(&sctp_ipv6_specific.list);
1033         inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1034         inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
1035         inet6_unregister_protosw(&sctpv6_stream_protosw);
1036         unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
1037         proto_unregister(&sctpv6_prot);
1038 }