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