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