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