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