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