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