projects/linux·blob master · refs

1199 lines · 31.1 KiB

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