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