OSDN Git Service

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