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