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