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