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