xref: /openbmc/linux/net/sctp/ipv6.c (revision 1b8d7ae4)
11da177e4SLinus Torvalds /* SCTP kernel reference Implementation
21da177e4SLinus Torvalds  * (C) Copyright IBM Corp. 2002, 2004
31da177e4SLinus Torvalds  * Copyright (c) 2001 Nokia, Inc.
41da177e4SLinus Torvalds  * Copyright (c) 2001 La Monte H.P. Yarroll
51da177e4SLinus Torvalds  * Copyright (c) 2002-2003 Intel Corp.
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * This file is part of the SCTP kernel reference Implementation
81da177e4SLinus Torvalds  *
91da177e4SLinus Torvalds  * SCTP over IPv6.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * The SCTP reference implementation is free software;
121da177e4SLinus Torvalds  * you can redistribute it and/or modify it under the terms of
131da177e4SLinus Torvalds  * the GNU General Public License as published by
141da177e4SLinus Torvalds  * the Free Software Foundation; either version 2, or (at your option)
151da177e4SLinus Torvalds  * any later version.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * The SCTP reference implementation is distributed in the hope that it
181da177e4SLinus Torvalds  * will be useful, but WITHOUT ANY WARRANTY; without even the implied
191da177e4SLinus Torvalds  *		   ************************
201da177e4SLinus Torvalds  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
211da177e4SLinus Torvalds  * See the GNU General Public License for more details.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
241da177e4SLinus Torvalds  * along with GNU CC; see the file COPYING.  If not, write to
251da177e4SLinus Torvalds  * the Free Software Foundation, 59 Temple Place - Suite 330,
261da177e4SLinus Torvalds  * Boston, MA 02111-1307, USA.
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * Please send any bug reports or fixes you make to the
291da177e4SLinus Torvalds  * email address(es):
301da177e4SLinus Torvalds  *    lksctp developers <lksctp-developers@lists.sourceforge.net>
311da177e4SLinus Torvalds  *
321da177e4SLinus Torvalds  * Or submit a bug report through the following website:
331da177e4SLinus Torvalds  *    http://www.sf.net/projects/lksctp
341da177e4SLinus Torvalds  *
351da177e4SLinus Torvalds  * Written or modified by:
361da177e4SLinus Torvalds  *    Le Yanqun		    <yanqun.le@nokia.com>
371da177e4SLinus Torvalds  *    Hui Huang		    <hui.huang@nokia.com>
381da177e4SLinus Torvalds  *    La Monte H.P. Yarroll <piggy@acm.org>
391da177e4SLinus Torvalds  *    Sridhar Samudrala	    <sri@us.ibm.com>
401da177e4SLinus Torvalds  *    Jon Grimm		    <jgrimm@us.ibm.com>
411da177e4SLinus Torvalds  *    Ardelle Fan	    <ardelle.fan@intel.com>
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * Based on:
441da177e4SLinus Torvalds  *	linux/net/ipv6/tcp_ipv6.c
451da177e4SLinus Torvalds  *
461da177e4SLinus Torvalds  * Any bugs reported given to us we will try to fix... any fixes shared will
471da177e4SLinus Torvalds  * be incorporated into the next SCTP release.
481da177e4SLinus Torvalds  */
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #include <linux/module.h>
511da177e4SLinus Torvalds #include <linux/errno.h>
521da177e4SLinus Torvalds #include <linux/types.h>
531da177e4SLinus Torvalds #include <linux/socket.h>
541da177e4SLinus Torvalds #include <linux/sockios.h>
551da177e4SLinus Torvalds #include <linux/net.h>
561da177e4SLinus Torvalds #include <linux/in.h>
571da177e4SLinus Torvalds #include <linux/in6.h>
581da177e4SLinus Torvalds #include <linux/netdevice.h>
591da177e4SLinus Torvalds #include <linux/init.h>
601da177e4SLinus Torvalds #include <linux/ipsec.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <linux/ipv6.h>
631da177e4SLinus Torvalds #include <linux/icmpv6.h>
641da177e4SLinus Torvalds #include <linux/random.h>
651da177e4SLinus Torvalds #include <linux/seq_file.h>
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds #include <net/protocol.h>
681da177e4SLinus Torvalds #include <net/ndisc.h>
69c752f073SArnaldo Carvalho de Melo #include <net/ip.h>
701da177e4SLinus Torvalds #include <net/ipv6.h>
711da177e4SLinus Torvalds #include <net/transp_v6.h>
721da177e4SLinus Torvalds #include <net/addrconf.h>
731da177e4SLinus Torvalds #include <net/ip6_route.h>
741da177e4SLinus Torvalds #include <net/inet_common.h>
751da177e4SLinus Torvalds #include <net/inet_ecn.h>
761da177e4SLinus Torvalds #include <net/sctp/sctp.h>
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds #include <asm/uaccess.h>
791da177e4SLinus Torvalds 
8029303547SVlad Yasevich /* Event handler for inet6 address addition/deletion events.
8129303547SVlad Yasevich  * The sctp_local_addr_list needs to be protocted by a spin lock since
8229303547SVlad Yasevich  * multiple notifiers (say IPv4 and IPv6) may be running at the same
8329303547SVlad Yasevich  * time and thus corrupt the list.
8429303547SVlad Yasevich  * The reader side is protected with RCU.
8529303547SVlad Yasevich  */
8624123186SAdrian Bunk static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
8729c7cf96SSridhar Samudrala 				void *ptr)
8829c7cf96SSridhar Samudrala {
8929c7cf96SSridhar Samudrala 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
9029303547SVlad Yasevich 	struct sctp_sockaddr_entry *addr = NULL;
9129303547SVlad Yasevich 	struct sctp_sockaddr_entry *temp;
9229c7cf96SSridhar Samudrala 
9329c7cf96SSridhar Samudrala 	switch (ev) {
9429c7cf96SSridhar Samudrala 	case NETDEV_UP:
9529c7cf96SSridhar Samudrala 		addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
9629c7cf96SSridhar Samudrala 		if (addr) {
9729c7cf96SSridhar Samudrala 			addr->a.v6.sin6_family = AF_INET6;
9829c7cf96SSridhar Samudrala 			addr->a.v6.sin6_port = 0;
9929c7cf96SSridhar Samudrala 			memcpy(&addr->a.v6.sin6_addr, &ifa->addr,
10029c7cf96SSridhar Samudrala 				 sizeof(struct in6_addr));
10129c7cf96SSridhar Samudrala 			addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
10229303547SVlad Yasevich 			addr->valid = 1;
10329303547SVlad Yasevich 			spin_lock_bh(&sctp_local_addr_lock);
10429303547SVlad Yasevich 			list_add_tail_rcu(&addr->list, &sctp_local_addr_list);
10529303547SVlad Yasevich 			spin_unlock_bh(&sctp_local_addr_lock);
10629c7cf96SSridhar Samudrala 		}
10729c7cf96SSridhar Samudrala 		break;
10829c7cf96SSridhar Samudrala 	case NETDEV_DOWN:
10929303547SVlad Yasevich 		spin_lock_bh(&sctp_local_addr_lock);
11029303547SVlad Yasevich 		list_for_each_entry_safe(addr, temp,
11129303547SVlad Yasevich 					&sctp_local_addr_list, list) {
11229303547SVlad Yasevich 			if (ipv6_addr_equal(&addr->a.v6.sin6_addr,
11329303547SVlad Yasevich 					     &ifa->addr)) {
11429303547SVlad Yasevich 				addr->valid = 0;
11529303547SVlad Yasevich 				list_del_rcu(&addr->list);
11629c7cf96SSridhar Samudrala 				break;
11729c7cf96SSridhar Samudrala 			}
11829c7cf96SSridhar Samudrala 		}
11929303547SVlad Yasevich 		spin_unlock_bh(&sctp_local_addr_lock);
12029303547SVlad Yasevich 		if (addr && !addr->valid)
12129303547SVlad Yasevich 			call_rcu(&addr->rcu, sctp_local_addr_free);
12229c7cf96SSridhar Samudrala 		break;
12329c7cf96SSridhar Samudrala 	}
12429c7cf96SSridhar Samudrala 
12529c7cf96SSridhar Samudrala 	return NOTIFY_DONE;
12629c7cf96SSridhar Samudrala }
12729c7cf96SSridhar Samudrala 
1281da177e4SLinus Torvalds static struct notifier_block sctp_inet6addr_notifier = {
12929c7cf96SSridhar Samudrala 	.notifier_call = sctp_inet6addr_event,
1301da177e4SLinus Torvalds };
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /* ICMP error handler. */
1331da177e4SLinus Torvalds SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
13404ce6909SAl Viro 			     int type, int code, int offset, __be32 info)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct inet6_dev *idev;
1371da177e4SLinus Torvalds 	struct sock *sk;
1381da177e4SLinus Torvalds 	struct sctp_association *asoc;
1391da177e4SLinus Torvalds 	struct sctp_transport *transport;
1401da177e4SLinus Torvalds 	struct ipv6_pinfo *np;
1412e07fa9cSArnaldo Carvalho de Melo 	sk_buff_data_t saveip, savesctp;
1421da177e4SLinus Torvalds 	int err;
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds 	idev = in6_dev_get(skb->dev);
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	/* Fix up skb to look at the embedded net header. */
147b0e380b1SArnaldo Carvalho de Melo 	saveip	 = skb->network_header;
148b0e380b1SArnaldo Carvalho de Melo 	savesctp = skb->transport_header;
1491ced98e8SArnaldo Carvalho de Melo 	skb_reset_network_header(skb);
150a27ef749SArnaldo Carvalho de Melo 	skb_set_transport_header(skb, offset);
151a27ef749SArnaldo Carvalho de Melo 	sk = sctp_err_lookup(AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
1521da177e4SLinus Torvalds 	/* Put back, the original pointers. */
153b0e380b1SArnaldo Carvalho de Melo 	skb->network_header   = saveip;
154b0e380b1SArnaldo Carvalho de Melo 	skb->transport_header = savesctp;
1551da177e4SLinus Torvalds 	if (!sk) {
1561da177e4SLinus Torvalds 		ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
1571da177e4SLinus Torvalds 		goto out;
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	/* Warning:  The sock lock is held.  Remember to call
1611da177e4SLinus Torvalds 	 * sctp_err_finish!
1621da177e4SLinus Torvalds 	 */
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	switch (type) {
1651da177e4SLinus Torvalds 	case ICMPV6_PKT_TOOBIG:
1661da177e4SLinus Torvalds 		sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
1671da177e4SLinus Torvalds 		goto out_unlock;
1681da177e4SLinus Torvalds 	case ICMPV6_PARAMPROB:
1691da177e4SLinus Torvalds 		if (ICMPV6_UNK_NEXTHDR == code) {
170d1ad1ff2SSridhar Samudrala 			sctp_icmp_proto_unreachable(sk, asoc, transport);
1711da177e4SLinus Torvalds 			goto out_unlock;
1721da177e4SLinus Torvalds 		}
1731da177e4SLinus Torvalds 		break;
1741da177e4SLinus Torvalds 	default:
1751da177e4SLinus Torvalds 		break;
1761da177e4SLinus Torvalds 	}
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	np = inet6_sk(sk);
1791da177e4SLinus Torvalds 	icmpv6_err_convert(type, code, &err);
1801da177e4SLinus Torvalds 	if (!sock_owned_by_user(sk) && np->recverr) {
1811da177e4SLinus Torvalds 		sk->sk_err = err;
1821da177e4SLinus Torvalds 		sk->sk_error_report(sk);
1831da177e4SLinus Torvalds 	} else {  /* Only an error on timeout */
1841da177e4SLinus Torvalds 		sk->sk_err_soft = err;
1851da177e4SLinus Torvalds 	}
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds out_unlock:
188d1ad1ff2SSridhar Samudrala 	sctp_err_finish(sk, asoc);
1891da177e4SLinus Torvalds out:
1901da177e4SLinus Torvalds 	if (likely(idev != NULL))
1911da177e4SLinus Torvalds 		in6_dev_put(idev);
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
1951da177e4SLinus Torvalds static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
1961da177e4SLinus Torvalds 			int ipfragok)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	struct sock *sk = skb->sk;
1991da177e4SLinus Torvalds 	struct ipv6_pinfo *np = inet6_sk(sk);
2001da177e4SLinus Torvalds 	struct flowi fl;
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds 	memset(&fl, 0, sizeof(fl));
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	fl.proto = sk->sk_protocol;
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 	/* Fill in the dest address from the route entry passed with the skb
2071da177e4SLinus Torvalds 	 * and the source address from the transport.
2081da177e4SLinus Torvalds 	 */
209b3f5b3b6SAl Viro 	ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr.v6.sin6_addr);
210a9266268SAl Viro 	ipv6_addr_copy(&fl.fl6_src, &transport->saddr.v6.sin6_addr);
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	fl.fl6_flowlabel = np->flow_label;
2131da177e4SLinus Torvalds 	IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
2141da177e4SLinus Torvalds 	if (ipv6_addr_type(&fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
215a9266268SAl Viro 		fl.oif = transport->saddr.v6.sin6_scope_id;
2161da177e4SLinus Torvalds 	else
2171da177e4SLinus Torvalds 		fl.oif = sk->sk_bound_dev_if;
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	if (np->opt && np->opt->srcrt) {
2201da177e4SLinus Torvalds 		struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
2211da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
2221da177e4SLinus Torvalds 	}
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
22546b86a2dSJoe Perches 			  "src:" NIP6_FMT " dst:" NIP6_FMT "\n",
2261da177e4SLinus Torvalds 			  __FUNCTION__, skb, skb->len,
2271da177e4SLinus Torvalds 			  NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
2321da177e4SLinus Torvalds }
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds /* Returns the dst cache entry for the given source and destination ip
2351da177e4SLinus Torvalds  * addresses.
2361da177e4SLinus Torvalds  */
2371da177e4SLinus Torvalds static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
2381da177e4SLinus Torvalds 					 union sctp_addr *daddr,
2391da177e4SLinus Torvalds 					 union sctp_addr *saddr)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds 	struct dst_entry *dst;
2421da177e4SLinus Torvalds 	struct flowi fl;
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	memset(&fl, 0, sizeof(fl));
2451da177e4SLinus Torvalds 	ipv6_addr_copy(&fl.fl6_dst, &daddr->v6.sin6_addr);
2461da177e4SLinus Torvalds 	if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
2471da177e4SLinus Torvalds 		fl.oif = daddr->v6.sin6_scope_id;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 
25046b86a2dSJoe Perches 	SCTP_DEBUG_PRINTK("%s: DST=" NIP6_FMT " ",
2511da177e4SLinus Torvalds 			  __FUNCTION__, NIP6(fl.fl6_dst));
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	if (saddr) {
2541da177e4SLinus Torvalds 		ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
2551da177e4SLinus Torvalds 		SCTP_DEBUG_PRINTK(
25646b86a2dSJoe Perches 			"SRC=" NIP6_FMT " - ",
2571da177e4SLinus Torvalds 			NIP6(fl.fl6_src));
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	dst = ip6_route_output(NULL, &fl);
2614251320fSVille Nuorvala 	if (!dst->error) {
2621da177e4SLinus Torvalds 		struct rt6_info *rt;
2631da177e4SLinus Torvalds 		rt = (struct rt6_info *)dst;
2641da177e4SLinus Torvalds 		SCTP_DEBUG_PRINTK(
26546b86a2dSJoe Perches 			"rt6_dst:" NIP6_FMT " rt6_src:" NIP6_FMT "\n",
2661da177e4SLinus Torvalds 			NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
2671da177e4SLinus Torvalds 		return dst;
2681da177e4SLinus Torvalds 	}
2694251320fSVille Nuorvala 	SCTP_DEBUG_PRINTK("NO ROUTE\n");
2704251320fSVille Nuorvala 	dst_release(dst);
2714251320fSVille Nuorvala 	return NULL;
2724251320fSVille Nuorvala }
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds /* Returns the number of consecutive initial bits that match in the 2 ipv6
2751da177e4SLinus Torvalds  * addresses.
2761da177e4SLinus Torvalds  */
2771da177e4SLinus Torvalds static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
2781da177e4SLinus Torvalds 					 union sctp_addr *s2)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct in6_addr *a1 = &s1->v6.sin6_addr;
2811da177e4SLinus Torvalds 	struct in6_addr *a2 = &s2->v6.sin6_addr;
2821da177e4SLinus Torvalds 	int i, j;
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds 	for (i = 0; i < 4 ; i++) {
285dbc16db1SAl Viro 		__be32 a1xora2;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 		a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
2881da177e4SLinus Torvalds 
2891da177e4SLinus Torvalds 		if ((j = fls(ntohl(a1xora2))))
2901da177e4SLinus Torvalds 			return (i * 32 + 32 - j);
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	return (i*32);
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2961da177e4SLinus Torvalds /* Fills in the source address(saddr) based on the destination address(daddr)
2971da177e4SLinus Torvalds  * and asoc's bind address list.
2981da177e4SLinus Torvalds  */
2991da177e4SLinus Torvalds static void sctp_v6_get_saddr(struct sctp_association *asoc,
3001da177e4SLinus Torvalds 			      struct dst_entry *dst,
3011da177e4SLinus Torvalds 			      union sctp_addr *daddr,
3021da177e4SLinus Torvalds 			      union sctp_addr *saddr)
3031da177e4SLinus Torvalds {
3041da177e4SLinus Torvalds 	struct sctp_bind_addr *bp;
3051da177e4SLinus Torvalds 	struct sctp_sockaddr_entry *laddr;
3061da177e4SLinus Torvalds 	sctp_scope_t scope;
3071da177e4SLinus Torvalds 	union sctp_addr *baddr = NULL;
3081da177e4SLinus Torvalds 	__u8 matchlen = 0;
3091da177e4SLinus Torvalds 	__u8 bmatchlen;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
31246b86a2dSJoe Perches 			  "daddr:" NIP6_FMT " ",
3131da177e4SLinus Torvalds 			  __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	if (!asoc) {
3161da177e4SLinus Torvalds 		ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
31746b86a2dSJoe Perches 		SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: " NIP6_FMT "\n",
3181da177e4SLinus Torvalds 				  NIP6(saddr->v6.sin6_addr));
3191da177e4SLinus Torvalds 		return;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	scope = sctp_scope(daddr);
3231da177e4SLinus Torvalds 
3241da177e4SLinus Torvalds 	bp = &asoc->base.bind_addr;
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	/* Go through the bind address list and find the best source address
3271da177e4SLinus Torvalds 	 * that matches the scope of the destination address.
3281da177e4SLinus Torvalds 	 */
329559cf710SVlad Yasevich 	rcu_read_lock();
330559cf710SVlad Yasevich 	list_for_each_entry_rcu(laddr, &bp->address_list, list) {
331559cf710SVlad Yasevich 		if (!laddr->valid)
332559cf710SVlad Yasevich 			continue;
333dc022a98SSridhar Samudrala 		if ((laddr->use_as_src) &&
3346244be4eSAl Viro 		    (laddr->a.sa.sa_family == AF_INET6) &&
3356244be4eSAl Viro 		    (scope <= sctp_scope(&laddr->a))) {
336d3f7a54aSAl Viro 			bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
3371da177e4SLinus Torvalds 			if (!baddr || (matchlen < bmatchlen)) {
338d3f7a54aSAl Viro 				baddr = &laddr->a;
3391da177e4SLinus Torvalds 				matchlen = bmatchlen;
3401da177e4SLinus Torvalds 			}
3411da177e4SLinus Torvalds 		}
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	if (baddr) {
3451da177e4SLinus Torvalds 		memcpy(saddr, baddr, sizeof(union sctp_addr));
34646b86a2dSJoe Perches 		SCTP_DEBUG_PRINTK("saddr: " NIP6_FMT "\n",
3471da177e4SLinus Torvalds 				  NIP6(saddr->v6.sin6_addr));
3481da177e4SLinus Torvalds 	} else {
3491da177e4SLinus Torvalds 		printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
35046b86a2dSJoe Perches 		       "address for the dest:" NIP6_FMT "\n",
3511da177e4SLinus Torvalds 		       __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
3521da177e4SLinus Torvalds 	}
3531da177e4SLinus Torvalds 
354559cf710SVlad Yasevich 	rcu_read_unlock();
3551da177e4SLinus Torvalds }
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds /* Make a copy of all potential local addresses. */
3581da177e4SLinus Torvalds static void sctp_v6_copy_addrlist(struct list_head *addrlist,
3591da177e4SLinus Torvalds 				  struct net_device *dev)
3601da177e4SLinus Torvalds {
3611da177e4SLinus Torvalds 	struct inet6_dev *in6_dev;
3621da177e4SLinus Torvalds 	struct inet6_ifaddr *ifp;
3631da177e4SLinus Torvalds 	struct sctp_sockaddr_entry *addr;
3641da177e4SLinus Torvalds 
3658814c4b5SYOSHIFUJI Hideaki 	rcu_read_lock();
3661da177e4SLinus Torvalds 	if ((in6_dev = __in6_dev_get(dev)) == NULL) {
3678814c4b5SYOSHIFUJI Hideaki 		rcu_read_unlock();
3681da177e4SLinus Torvalds 		return;
3691da177e4SLinus Torvalds 	}
3701da177e4SLinus Torvalds 
371e2eb8d45SJarek Poplawski 	read_lock_bh(&in6_dev->lock);
3721da177e4SLinus Torvalds 	for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
3731da177e4SLinus Torvalds 		/* Add the address to the local list.  */
3741da177e4SLinus Torvalds 		addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
3751da177e4SLinus Torvalds 		if (addr) {
3762a6fd78aSAl Viro 			addr->a.v6.sin6_family = AF_INET6;
3772a6fd78aSAl Viro 			addr->a.v6.sin6_port = 0;
3782a6fd78aSAl Viro 			addr->a.v6.sin6_addr = ifp->addr;
3792a6fd78aSAl Viro 			addr->a.v6.sin6_scope_id = dev->ifindex;
38029303547SVlad Yasevich 			addr->valid = 1;
3811da177e4SLinus Torvalds 			INIT_LIST_HEAD(&addr->list);
38229303547SVlad Yasevich 			INIT_RCU_HEAD(&addr->rcu);
3831da177e4SLinus Torvalds 			list_add_tail(&addr->list, addrlist);
3841da177e4SLinus Torvalds 		}
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
387e2eb8d45SJarek Poplawski 	read_unlock_bh(&in6_dev->lock);
3888814c4b5SYOSHIFUJI Hideaki 	rcu_read_unlock();
3891da177e4SLinus Torvalds }
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds /* Initialize a sockaddr_storage from in incoming skb. */
3921da177e4SLinus Torvalds static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
3931da177e4SLinus Torvalds 			     int is_saddr)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	void *from;
396d55c41b1SAl Viro 	__be16 *port;
3971da177e4SLinus Torvalds 	struct sctphdr *sh;
3981da177e4SLinus Torvalds 
3991da177e4SLinus Torvalds 	port = &addr->v6.sin6_port;
4001da177e4SLinus Torvalds 	addr->v6.sin6_family = AF_INET6;
4011da177e4SLinus Torvalds 	addr->v6.sin6_flowinfo = 0; /* FIXME */
4021da177e4SLinus Torvalds 	addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
4031da177e4SLinus Torvalds 
4042c0fd387SArnaldo Carvalho de Melo 	sh = sctp_hdr(skb);
4051da177e4SLinus Torvalds 	if (is_saddr) {
406d55c41b1SAl Viro 		*port  = sh->source;
4070660e03fSArnaldo Carvalho de Melo 		from = &ipv6_hdr(skb)->saddr;
4081da177e4SLinus Torvalds 	} else {
409d55c41b1SAl Viro 		*port = sh->dest;
4100660e03fSArnaldo Carvalho de Melo 		from = &ipv6_hdr(skb)->daddr;
4111da177e4SLinus Torvalds 	}
4121da177e4SLinus Torvalds 	ipv6_addr_copy(&addr->v6.sin6_addr, from);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds /* Initialize an sctp_addr from a socket. */
4161da177e4SLinus Torvalds static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
4171da177e4SLinus Torvalds {
4181da177e4SLinus Torvalds 	addr->v6.sin6_family = AF_INET6;
4197dcdbd95SAl Viro 	addr->v6.sin6_port = 0;
4201da177e4SLinus Torvalds 	addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
4211da177e4SLinus Torvalds }
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds /* Initialize sk->sk_rcv_saddr from sctp_addr. */
4241da177e4SLinus Torvalds static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds 	if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
4271da177e4SLinus Torvalds 		inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
4281da177e4SLinus Torvalds 		inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
4291da177e4SLinus Torvalds 		inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
4301da177e4SLinus Torvalds 		inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
4311da177e4SLinus Torvalds 			addr->v4.sin_addr.s_addr;
4321da177e4SLinus Torvalds 	} else {
4331da177e4SLinus Torvalds 		inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
4341da177e4SLinus Torvalds 	}
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds /* Initialize sk->sk_daddr from sctp_addr. */
4381da177e4SLinus Torvalds static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
4411da177e4SLinus Torvalds 		inet6_sk(sk)->daddr.s6_addr32[0] = 0;
4421da177e4SLinus Torvalds 		inet6_sk(sk)->daddr.s6_addr32[1] = 0;
4431da177e4SLinus Torvalds 		inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
4441da177e4SLinus Torvalds 		inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
4451da177e4SLinus Torvalds 	} else {
4461da177e4SLinus Torvalds 		inet6_sk(sk)->daddr = addr->v6.sin6_addr;
4471da177e4SLinus Torvalds 	}
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds /* Initialize a sctp_addr from an address parameter. */
4511da177e4SLinus Torvalds static void sctp_v6_from_addr_param(union sctp_addr *addr,
4521da177e4SLinus Torvalds 				    union sctp_addr_param *param,
453dd86d136SAl Viro 				    __be16 port, int iif)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	addr->v6.sin6_family = AF_INET6;
4561da177e4SLinus Torvalds 	addr->v6.sin6_port = port;
4571da177e4SLinus Torvalds 	addr->v6.sin6_flowinfo = 0; /* BUG */
4581da177e4SLinus Torvalds 	ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
4591da177e4SLinus Torvalds 	addr->v6.sin6_scope_id = iif;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /* Initialize an address parameter from a sctp_addr and return the length
4631da177e4SLinus Torvalds  * of the address parameter.
4641da177e4SLinus Torvalds  */
4651da177e4SLinus Torvalds static int sctp_v6_to_addr_param(const union sctp_addr *addr,
4661da177e4SLinus Torvalds 				 union sctp_addr_param *param)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	int length = sizeof(sctp_ipv6addr_param_t);
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
471dbc16db1SAl Viro 	param->v6.param_hdr.length = htons(length);
4721da177e4SLinus Torvalds 	ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	return length;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds /* Initialize a sctp_addr from a dst_entry. */
4781da177e4SLinus Torvalds static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
479854d43a4SAl Viro 			      __be16 port)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds 	struct rt6_info *rt = (struct rt6_info *)dst;
4821da177e4SLinus Torvalds 	addr->sa.sa_family = AF_INET6;
4831da177e4SLinus Torvalds 	addr->v6.sin6_port = port;
4841da177e4SLinus Torvalds 	ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
4851da177e4SLinus Torvalds }
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds /* Compare addresses exactly.
4881da177e4SLinus Torvalds  * v4-mapped-v6 is also in consideration.
4891da177e4SLinus Torvalds  */
4901da177e4SLinus Torvalds static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
4911da177e4SLinus Torvalds 			    const union sctp_addr *addr2)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	if (addr1->sa.sa_family != addr2->sa.sa_family) {
4941da177e4SLinus Torvalds 		if (addr1->sa.sa_family == AF_INET &&
4951da177e4SLinus Torvalds 		    addr2->sa.sa_family == AF_INET6 &&
496e773e4faSBrian Haley 		    ipv6_addr_v4mapped(&addr2->v6.sin6_addr)) {
4971da177e4SLinus Torvalds 			if (addr2->v6.sin6_port == addr1->v4.sin_port &&
4981da177e4SLinus Torvalds 			    addr2->v6.sin6_addr.s6_addr32[3] ==
4991da177e4SLinus Torvalds 			    addr1->v4.sin_addr.s_addr)
5001da177e4SLinus Torvalds 				return 1;
5011da177e4SLinus Torvalds 		}
5021da177e4SLinus Torvalds 		if (addr2->sa.sa_family == AF_INET &&
5031da177e4SLinus Torvalds 		    addr1->sa.sa_family == AF_INET6 &&
504e773e4faSBrian Haley 		    ipv6_addr_v4mapped(&addr1->v6.sin6_addr)) {
5051da177e4SLinus Torvalds 			if (addr1->v6.sin6_port == addr2->v4.sin_port &&
5061da177e4SLinus Torvalds 			    addr1->v6.sin6_addr.s6_addr32[3] ==
5071da177e4SLinus Torvalds 			    addr2->v4.sin_addr.s_addr)
5081da177e4SLinus Torvalds 				return 1;
5091da177e4SLinus Torvalds 		}
5101da177e4SLinus Torvalds 		return 0;
5111da177e4SLinus Torvalds 	}
5121da177e4SLinus Torvalds 	if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
5131da177e4SLinus Torvalds 		return 0;
5141da177e4SLinus Torvalds 	/* If this is a linklocal address, compare the scope_id. */
5151da177e4SLinus Torvalds 	if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
5161da177e4SLinus Torvalds 		if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
5171da177e4SLinus Torvalds 		    (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
5181da177e4SLinus Torvalds 			return 0;
5191da177e4SLinus Torvalds 		}
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	return 1;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds /* Initialize addr struct to INADDR_ANY. */
5266fbfa9f9SAl Viro static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	memset(addr, 0x00, sizeof(union sctp_addr));
5291da177e4SLinus Torvalds 	addr->v6.sin6_family = AF_INET6;
5301da177e4SLinus Torvalds 	addr->v6.sin6_port = port;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds /* Is this a wildcard address? */
5341da177e4SLinus Torvalds static int sctp_v6_is_any(const union sctp_addr *addr)
5351da177e4SLinus Torvalds {
536b9b9e10fSBrian Haley 	return ipv6_addr_any(&addr->v6.sin6_addr);
5371da177e4SLinus Torvalds }
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds /* Should this be available for binding?   */
5401da177e4SLinus Torvalds static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
5411da177e4SLinus Torvalds {
5421da177e4SLinus Torvalds 	int type;
5431da177e4SLinus Torvalds 	struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
5441da177e4SLinus Torvalds 
5451da177e4SLinus Torvalds 	type = ipv6_addr_type(in6);
5461da177e4SLinus Torvalds 	if (IPV6_ADDR_ANY == type)
5471da177e4SLinus Torvalds 		return 1;
5481da177e4SLinus Torvalds 	if (type == IPV6_ADDR_MAPPED) {
5491da177e4SLinus Torvalds 		if (sp && !sp->v4mapped)
5501da177e4SLinus Torvalds 			return 0;
5511da177e4SLinus Torvalds 		if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
5521da177e4SLinus Torvalds 			return 0;
5531da177e4SLinus Torvalds 		sctp_v6_map_v4(addr);
5541da177e4SLinus Torvalds 		return sctp_get_af_specific(AF_INET)->available(addr, sp);
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 	if (!(type & IPV6_ADDR_UNICAST))
5571da177e4SLinus Torvalds 		return 0;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	return ipv6_chk_addr(in6, NULL, 0);
5601da177e4SLinus Torvalds }
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds /* This function checks if the address is a valid address to be used for
5631da177e4SLinus Torvalds  * SCTP.
5641da177e4SLinus Torvalds  *
5651da177e4SLinus Torvalds  * Output:
5661da177e4SLinus Torvalds  * Return 0 - If the address is a non-unicast or an illegal address.
5671da177e4SLinus Torvalds  * Return 1 - If the address is a unicast.
5681da177e4SLinus Torvalds  */
5695636bef7SVlad Yasevich static int sctp_v6_addr_valid(union sctp_addr *addr,
5705636bef7SVlad Yasevich 			      struct sctp_sock *sp,
5715636bef7SVlad Yasevich 			      const struct sk_buff *skb)
5721da177e4SLinus Torvalds {
5731da177e4SLinus Torvalds 	int ret = ipv6_addr_type(&addr->v6.sin6_addr);
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds 	/* Support v4-mapped-v6 address. */
5761da177e4SLinus Torvalds 	if (ret == IPV6_ADDR_MAPPED) {
5771da177e4SLinus Torvalds 		/* Note: This routine is used in input, so v4-mapped-v6
5781da177e4SLinus Torvalds 		 * are disallowed here when there is no sctp_sock.
5791da177e4SLinus Torvalds 		 */
5801da177e4SLinus Torvalds 		if (!sp || !sp->v4mapped)
5811da177e4SLinus Torvalds 			return 0;
5821da177e4SLinus Torvalds 		if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
5831da177e4SLinus Torvalds 			return 0;
5841da177e4SLinus Torvalds 		sctp_v6_map_v4(addr);
5855636bef7SVlad Yasevich 		return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
5861da177e4SLinus Torvalds 	}
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	/* Is this a non-unicast address */
5891da177e4SLinus Torvalds 	if (!(ret & IPV6_ADDR_UNICAST))
5901da177e4SLinus Torvalds 		return 0;
5911da177e4SLinus Torvalds 
5921da177e4SLinus Torvalds 	return 1;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5951da177e4SLinus Torvalds /* What is the scope of 'addr'?  */
5961da177e4SLinus Torvalds static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	int v6scope;
5991da177e4SLinus Torvalds 	sctp_scope_t retval;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	/* The IPv6 scope is really a set of bit fields.
6021da177e4SLinus Torvalds 	 * See IFA_* in <net/if_inet6.h>.  Map to a generic SCTP scope.
6031da177e4SLinus Torvalds 	 */
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
6061da177e4SLinus Torvalds 	switch (v6scope) {
6071da177e4SLinus Torvalds 	case IFA_HOST:
6081da177e4SLinus Torvalds 		retval = SCTP_SCOPE_LOOPBACK;
6091da177e4SLinus Torvalds 		break;
6101da177e4SLinus Torvalds 	case IFA_LINK:
6111da177e4SLinus Torvalds 		retval = SCTP_SCOPE_LINK;
6121da177e4SLinus Torvalds 		break;
6131da177e4SLinus Torvalds 	case IFA_SITE:
6141da177e4SLinus Torvalds 		retval = SCTP_SCOPE_PRIVATE;
6151da177e4SLinus Torvalds 		break;
6161da177e4SLinus Torvalds 	default:
6171da177e4SLinus Torvalds 		retval = SCTP_SCOPE_GLOBAL;
6181da177e4SLinus Torvalds 		break;
6193ff50b79SStephen Hemminger 	}
6201da177e4SLinus Torvalds 
6211da177e4SLinus Torvalds 	return retval;
6221da177e4SLinus Torvalds }
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds /* Create and initialize a new sk for the socket to be returned by accept(). */
6251da177e4SLinus Torvalds static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
6261da177e4SLinus Torvalds 					     struct sctp_association *asoc)
6271da177e4SLinus Torvalds {
6281da177e4SLinus Torvalds 	struct inet_sock *inet = inet_sk(sk);
6291da177e4SLinus Torvalds 	struct sock *newsk;
6301da177e4SLinus Torvalds 	struct inet_sock *newinet;
6311da177e4SLinus Torvalds 	struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
6321da177e4SLinus Torvalds 	struct sctp6_sock *newsctp6sk;
6331da177e4SLinus Torvalds 
6341b8d7ae4SEric W. Biederman 	newsk = sk_alloc(sk->sk_net, PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
6351da177e4SLinus Torvalds 	if (!newsk)
6361da177e4SLinus Torvalds 		goto out;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	sock_init_data(NULL, newsk);
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	newsk->sk_type = SOCK_STREAM;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds 	newsk->sk_prot = sk->sk_prot;
6431da177e4SLinus Torvalds 	newsk->sk_no_check = sk->sk_no_check;
6441da177e4SLinus Torvalds 	newsk->sk_reuse = sk->sk_reuse;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds 	newsk->sk_destruct = inet_sock_destruct;
6471da177e4SLinus Torvalds 	newsk->sk_family = PF_INET6;
6481da177e4SLinus Torvalds 	newsk->sk_protocol = IPPROTO_SCTP;
6491da177e4SLinus Torvalds 	newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
6501da177e4SLinus Torvalds 	newsk->sk_shutdown = sk->sk_shutdown;
6511da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	newsctp6sk = (struct sctp6_sock *)newsk;
6541da177e4SLinus Torvalds 	inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
6551da177e4SLinus Torvalds 
656b225b884SDave Johnson 	sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
657b225b884SDave Johnson 
6581da177e4SLinus Torvalds 	newinet = inet_sk(newsk);
6591da177e4SLinus Torvalds 	newnp = inet6_sk(newsk);
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	memcpy(newnp, np, sizeof(struct ipv6_pinfo));
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	/* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
6641da177e4SLinus Torvalds 	 * and getpeername().
6651da177e4SLinus Torvalds 	 */
6661da177e4SLinus Torvalds 	newinet->sport = inet->sport;
6671da177e4SLinus Torvalds 	newnp->saddr = np->saddr;
6681da177e4SLinus Torvalds 	newnp->rcv_saddr = np->rcv_saddr;
6691da177e4SLinus Torvalds 	newinet->dport = htons(asoc->peer.port);
6701da177e4SLinus Torvalds 	sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	/* Init the ipv4 part of the socket since we can have sockets
6731da177e4SLinus Torvalds 	 * using v6 API for ipv4.
6741da177e4SLinus Torvalds 	 */
6751da177e4SLinus Torvalds 	newinet->uc_ttl = -1;
6761da177e4SLinus Torvalds 	newinet->mc_loop = 1;
6771da177e4SLinus Torvalds 	newinet->mc_ttl = 1;
6781da177e4SLinus Torvalds 	newinet->mc_index = 0;
6791da177e4SLinus Torvalds 	newinet->mc_list = NULL;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	if (ipv4_config.no_pmtu_disc)
6821da177e4SLinus Torvalds 		newinet->pmtudisc = IP_PMTUDISC_DONT;
6831da177e4SLinus Torvalds 	else
6841da177e4SLinus Torvalds 		newinet->pmtudisc = IP_PMTUDISC_WANT;
6851da177e4SLinus Torvalds 
686e6848976SArnaldo Carvalho de Melo 	sk_refcnt_debug_inc(newsk);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	if (newsk->sk_prot->init(newsk)) {
6891da177e4SLinus Torvalds 		sk_common_release(newsk);
6901da177e4SLinus Torvalds 		newsk = NULL;
6911da177e4SLinus Torvalds 	}
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds out:
6941da177e4SLinus Torvalds 	return newsk;
6951da177e4SLinus Torvalds }
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds /* Map v4 address to mapped v6 address */
6981da177e4SLinus Torvalds static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	if (sp->v4mapped && AF_INET == addr->sa.sa_family)
7011da177e4SLinus Torvalds 		sctp_v4_map_v6(addr);
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041da177e4SLinus Torvalds /* Where did this skb come from?  */
7051da177e4SLinus Torvalds static int sctp_v6_skb_iif(const struct sk_buff *skb)
7061da177e4SLinus Torvalds {
7071da177e4SLinus Torvalds 	struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
7081da177e4SLinus Torvalds 	return opt->iif;
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds /* Was this packet marked by Explicit Congestion Notification? */
7121da177e4SLinus Torvalds static int sctp_v6_is_ce(const struct sk_buff *skb)
7131da177e4SLinus Torvalds {
7140660e03fSArnaldo Carvalho de Melo 	return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /* Dump the v6 addr to the seq file. */
7181da177e4SLinus Torvalds static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
7191da177e4SLinus Torvalds {
72046b86a2dSJoe Perches 	seq_printf(seq, NIP6_FMT " ", NIP6(addr->v6.sin6_addr));
7211da177e4SLinus Torvalds }
7221da177e4SLinus Torvalds 
7231da177e4SLinus Torvalds /* Initialize a PF_INET6 socket msg_name. */
7241da177e4SLinus Torvalds static void sctp_inet6_msgname(char *msgname, int *addr_len)
7251da177e4SLinus Torvalds {
7261da177e4SLinus Torvalds 	struct sockaddr_in6 *sin6;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	sin6 = (struct sockaddr_in6 *)msgname;
7291da177e4SLinus Torvalds 	sin6->sin6_family = AF_INET6;
7301da177e4SLinus Torvalds 	sin6->sin6_flowinfo = 0;
7311da177e4SLinus Torvalds 	sin6->sin6_scope_id = 0; /*FIXME */
7321da177e4SLinus Torvalds 	*addr_len = sizeof(struct sockaddr_in6);
7331da177e4SLinus Torvalds }
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds /* Initialize a PF_INET msgname from a ulpevent. */
7361da177e4SLinus Torvalds static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
7371da177e4SLinus Torvalds 				     char *msgname, int *addrlen)
7381da177e4SLinus Torvalds {
7391da177e4SLinus Torvalds 	struct sockaddr_in6 *sin6, *sin6from;
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds 	if (msgname) {
7421da177e4SLinus Torvalds 		union sctp_addr *addr;
7431da177e4SLinus Torvalds 		struct sctp_association *asoc;
7441da177e4SLinus Torvalds 
7451da177e4SLinus Torvalds 		asoc = event->asoc;
7461da177e4SLinus Torvalds 		sctp_inet6_msgname(msgname, addrlen);
7471da177e4SLinus Torvalds 		sin6 = (struct sockaddr_in6 *)msgname;
7481da177e4SLinus Torvalds 		sin6->sin6_port = htons(asoc->peer.port);
7491da177e4SLinus Torvalds 		addr = &asoc->peer.primary_addr;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 		/* Note: If we go to a common v6 format, this code
7521da177e4SLinus Torvalds 		 * will change.
7531da177e4SLinus Torvalds 		 */
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 		/* Map ipv4 address into v4-mapped-on-v6 address.  */
7561da177e4SLinus Torvalds 		if (sctp_sk(asoc->base.sk)->v4mapped &&
7571da177e4SLinus Torvalds 		    AF_INET == addr->sa.sa_family) {
7581da177e4SLinus Torvalds 			sctp_v4_map_v6((union sctp_addr *)sin6);
7591da177e4SLinus Torvalds 			sin6->sin6_addr.s6_addr32[3] =
7601da177e4SLinus Torvalds 				addr->v4.sin_addr.s_addr;
7611da177e4SLinus Torvalds 			return;
7621da177e4SLinus Torvalds 		}
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 		sin6from = &asoc->peer.primary_addr.v6;
7651da177e4SLinus Torvalds 		ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
7661da177e4SLinus Torvalds 		if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
7671da177e4SLinus Torvalds 			sin6->sin6_scope_id = sin6from->sin6_scope_id;
7681da177e4SLinus Torvalds 	}
7691da177e4SLinus Torvalds }
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds /* Initialize a msg_name from an inbound skb. */
7721da177e4SLinus Torvalds static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
7731da177e4SLinus Torvalds 				   int *addr_len)
7741da177e4SLinus Torvalds {
7751da177e4SLinus Torvalds 	struct sctphdr *sh;
7761da177e4SLinus Torvalds 	struct sockaddr_in6 *sin6;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	if (msgname) {
7791da177e4SLinus Torvalds 		sctp_inet6_msgname(msgname, addr_len);
7801da177e4SLinus Torvalds 		sin6 = (struct sockaddr_in6 *)msgname;
7812c0fd387SArnaldo Carvalho de Melo 		sh = sctp_hdr(skb);
7821da177e4SLinus Torvalds 		sin6->sin6_port = sh->source;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 		/* Map ipv4 address into v4-mapped-on-v6 address. */
7851da177e4SLinus Torvalds 		if (sctp_sk(skb->sk)->v4mapped &&
786eddc9ec5SArnaldo Carvalho de Melo 		    ip_hdr(skb)->version == 4) {
7871da177e4SLinus Torvalds 			sctp_v4_map_v6((union sctp_addr *)sin6);
788eddc9ec5SArnaldo Carvalho de Melo 			sin6->sin6_addr.s6_addr32[3] = ip_hdr(skb)->saddr;
7891da177e4SLinus Torvalds 			return;
7901da177e4SLinus Torvalds 		}
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 		/* Otherwise, just copy the v6 address. */
7930660e03fSArnaldo Carvalho de Melo 		ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
7941da177e4SLinus Torvalds 		if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
7951da177e4SLinus Torvalds 			struct sctp_ulpevent *ev = sctp_skb2event(skb);
7961da177e4SLinus Torvalds 			sin6->sin6_scope_id = ev->iif;
7971da177e4SLinus Torvalds 		}
7981da177e4SLinus Torvalds 	}
7991da177e4SLinus Torvalds }
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds /* Do we support this AF? */
8021da177e4SLinus Torvalds static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
8031da177e4SLinus Torvalds {
8041da177e4SLinus Torvalds 	switch (family) {
8051da177e4SLinus Torvalds 	case AF_INET6:
8061da177e4SLinus Torvalds 		return 1;
8071da177e4SLinus Torvalds 	/* v4-mapped-v6 addresses */
8081da177e4SLinus Torvalds 	case AF_INET:
8091da177e4SLinus Torvalds 		if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
8101da177e4SLinus Torvalds 			return 1;
8111da177e4SLinus Torvalds 	default:
8121da177e4SLinus Torvalds 		return 0;
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds }
8151da177e4SLinus Torvalds 
8161da177e4SLinus Torvalds /* Address matching with wildcards allowed.  This extra level
8171da177e4SLinus Torvalds  * of indirection lets us choose whether a PF_INET6 should
8181da177e4SLinus Torvalds  * disallow any v4 addresses if we so choose.
8191da177e4SLinus Torvalds  */
8201da177e4SLinus Torvalds static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
8211da177e4SLinus Torvalds 			       const union sctp_addr *addr2,
8221da177e4SLinus Torvalds 			       struct sctp_sock *opt)
8231da177e4SLinus Torvalds {
8241da177e4SLinus Torvalds 	struct sctp_af *af1, *af2;
8251da177e4SLinus Torvalds 
8261da177e4SLinus Torvalds 	af1 = sctp_get_af_specific(addr1->sa.sa_family);
8271da177e4SLinus Torvalds 	af2 = sctp_get_af_specific(addr2->sa.sa_family);
8281da177e4SLinus Torvalds 
8291da177e4SLinus Torvalds 	if (!af1 || !af2)
8301da177e4SLinus Torvalds 		return 0;
8311da177e4SLinus Torvalds 	/* Today, wildcard AF_INET/AF_INET6. */
8321da177e4SLinus Torvalds 	if (sctp_is_any(addr1) || sctp_is_any(addr2))
8331da177e4SLinus Torvalds 		return 1;
8341da177e4SLinus Torvalds 
8351da177e4SLinus Torvalds 	if (addr1->sa.sa_family != addr2->sa.sa_family)
8361da177e4SLinus Torvalds 		return 0;
8371da177e4SLinus Torvalds 
8381da177e4SLinus Torvalds 	return af1->cmp_addr(addr1, addr2);
8391da177e4SLinus Torvalds }
8401da177e4SLinus Torvalds 
8411da177e4SLinus Torvalds /* Verify that the provided sockaddr looks bindable.   Common verification,
8421da177e4SLinus Torvalds  * has already been taken care of.
8431da177e4SLinus Torvalds  */
8441da177e4SLinus Torvalds static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
8451da177e4SLinus Torvalds {
8461da177e4SLinus Torvalds 	struct sctp_af *af;
8471da177e4SLinus Torvalds 
8481da177e4SLinus Torvalds 	/* ASSERT: address family has already been verified. */
8491da177e4SLinus Torvalds 	if (addr->sa.sa_family != AF_INET6)
8501da177e4SLinus Torvalds 		af = sctp_get_af_specific(addr->sa.sa_family);
8511da177e4SLinus Torvalds 	else {
8521da177e4SLinus Torvalds 		int type = ipv6_addr_type(&addr->v6.sin6_addr);
8536a6ddb2aSSridhar Samudrala 		struct net_device *dev;
8541da177e4SLinus Torvalds 
8556a6ddb2aSSridhar Samudrala 		if (type & IPV6_ADDR_LINKLOCAL) {
8566a6ddb2aSSridhar Samudrala 			if (!addr->v6.sin6_scope_id)
8571da177e4SLinus Torvalds 				return 0;
8586a6ddb2aSSridhar Samudrala 			dev = dev_get_by_index(addr->v6.sin6_scope_id);
8596a6ddb2aSSridhar Samudrala 			if (!dev)
8606a6ddb2aSSridhar Samudrala 				return 0;
8611669d857SVlad Yasevich 			if (!ipv6_chk_addr(&addr->v6.sin6_addr, dev, 0)) {
8621669d857SVlad Yasevich 				dev_put(dev);
8631669d857SVlad Yasevich 				return 0;
8641669d857SVlad Yasevich 			}
8656a6ddb2aSSridhar Samudrala 			dev_put(dev);
8661da177e4SLinus Torvalds 		}
8671da177e4SLinus Torvalds 		af = opt->pf->af;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 	return af->available(addr, opt);
8701da177e4SLinus Torvalds }
8711da177e4SLinus Torvalds 
8726a6ddb2aSSridhar Samudrala /* Verify that the provided sockaddr looks sendable.   Common verification,
8731da177e4SLinus Torvalds  * has already been taken care of.
8741da177e4SLinus Torvalds  */
8751da177e4SLinus Torvalds static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
8761da177e4SLinus Torvalds {
8771da177e4SLinus Torvalds 	struct sctp_af *af = NULL;
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds 	/* ASSERT: address family has already been verified. */
8801da177e4SLinus Torvalds 	if (addr->sa.sa_family != AF_INET6)
8811da177e4SLinus Torvalds 		af = sctp_get_af_specific(addr->sa.sa_family);
8821da177e4SLinus Torvalds 	else {
8831da177e4SLinus Torvalds 		int type = ipv6_addr_type(&addr->v6.sin6_addr);
8846a6ddb2aSSridhar Samudrala 		struct net_device *dev;
8851da177e4SLinus Torvalds 
8866a6ddb2aSSridhar Samudrala 		if (type & IPV6_ADDR_LINKLOCAL) {
8876a6ddb2aSSridhar Samudrala 			if (!addr->v6.sin6_scope_id)
8881da177e4SLinus Torvalds 				return 0;
8896a6ddb2aSSridhar Samudrala 			dev = dev_get_by_index(addr->v6.sin6_scope_id);
8906a6ddb2aSSridhar Samudrala 			if (!dev)
8916a6ddb2aSSridhar Samudrala 				return 0;
8926a6ddb2aSSridhar Samudrala 			dev_put(dev);
8931da177e4SLinus Torvalds 		}
8941da177e4SLinus Torvalds 		af = opt->pf->af;
8951da177e4SLinus Torvalds 	}
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	return af != NULL;
8981da177e4SLinus Torvalds }
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds /* Fill in Supported Address Type information for INIT and INIT-ACK
9011da177e4SLinus Torvalds  * chunks.   Note: In the future, we may want to look at sock options
9021da177e4SLinus Torvalds  * to determine whether a PF_INET6 socket really wants to have IPV4
9031da177e4SLinus Torvalds  * addresses.
9041da177e4SLinus Torvalds  * Returns number of addresses supported.
9051da177e4SLinus Torvalds  */
9061da177e4SLinus Torvalds static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
9073dbe8656SAl Viro 				      __be16 *types)
9081da177e4SLinus Torvalds {
9091da177e4SLinus Torvalds 	types[0] = SCTP_PARAM_IPV4_ADDRESS;
9101da177e4SLinus Torvalds 	types[1] = SCTP_PARAM_IPV6_ADDRESS;
9111da177e4SLinus Torvalds 	return 2;
9121da177e4SLinus Torvalds }
9131da177e4SLinus Torvalds 
91490ddc4f0SEric Dumazet static const struct proto_ops inet6_seqpacket_ops = {
9151da177e4SLinus Torvalds 	.family		   = PF_INET6,
9161da177e4SLinus Torvalds 	.owner		   = THIS_MODULE,
9171da177e4SLinus Torvalds 	.release	   = inet6_release,
9181da177e4SLinus Torvalds 	.bind		   = inet6_bind,
9191da177e4SLinus Torvalds 	.connect	   = inet_dgram_connect,
9201da177e4SLinus Torvalds 	.socketpair	   = sock_no_socketpair,
9211da177e4SLinus Torvalds 	.accept		   = inet_accept,
9221da177e4SLinus Torvalds 	.getname	   = inet6_getname,
9231da177e4SLinus Torvalds 	.poll		   = sctp_poll,
9241da177e4SLinus Torvalds 	.ioctl		   = inet6_ioctl,
9251da177e4SLinus Torvalds 	.listen		   = sctp_inet_listen,
9261da177e4SLinus Torvalds 	.shutdown	   = inet_shutdown,
9271da177e4SLinus Torvalds 	.setsockopt	   = sock_common_setsockopt,
9281da177e4SLinus Torvalds 	.getsockopt	   = sock_common_getsockopt,
929543d9cfeSArnaldo Carvalho de Melo 	.sendmsg	   = inet_sendmsg,
930543d9cfeSArnaldo Carvalho de Melo 	.recvmsg	   = sock_common_recvmsg,
931543d9cfeSArnaldo Carvalho de Melo 	.mmap		   = sock_no_mmap,
9323fdadf7dSDmitry Mishin #ifdef CONFIG_COMPAT
9333fdadf7dSDmitry Mishin 	.compat_setsockopt = compat_sock_common_setsockopt,
9343fdadf7dSDmitry Mishin 	.compat_getsockopt = compat_sock_common_getsockopt,
9353fdadf7dSDmitry Mishin #endif
9361da177e4SLinus Torvalds };
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds static struct inet_protosw sctpv6_seqpacket_protosw = {
9391da177e4SLinus Torvalds 	.type          = SOCK_SEQPACKET,
9401da177e4SLinus Torvalds 	.protocol      = IPPROTO_SCTP,
9411da177e4SLinus Torvalds 	.prot 	       = &sctpv6_prot,
9421da177e4SLinus Torvalds 	.ops           = &inet6_seqpacket_ops,
9431da177e4SLinus Torvalds 	.capability    = -1,
9441da177e4SLinus Torvalds 	.no_check      = 0,
9451da177e4SLinus Torvalds 	.flags         = SCTP_PROTOSW_FLAG
9461da177e4SLinus Torvalds };
9471da177e4SLinus Torvalds static struct inet_protosw sctpv6_stream_protosw = {
9481da177e4SLinus Torvalds 	.type          = SOCK_STREAM,
9491da177e4SLinus Torvalds 	.protocol      = IPPROTO_SCTP,
9501da177e4SLinus Torvalds 	.prot 	       = &sctpv6_prot,
9511da177e4SLinus Torvalds 	.ops           = &inet6_seqpacket_ops,
9521da177e4SLinus Torvalds 	.capability    = -1,
9531da177e4SLinus Torvalds 	.no_check      = 0,
9541da177e4SLinus Torvalds 	.flags         = SCTP_PROTOSW_FLAG,
9551da177e4SLinus Torvalds };
9561da177e4SLinus Torvalds 
957951dbc8aSPatrick McHardy static int sctp6_rcv(struct sk_buff **pskb)
9581da177e4SLinus Torvalds {
9591da177e4SLinus Torvalds 	return sctp_rcv(*pskb) ? -1 : 0;
9601da177e4SLinus Torvalds }
9611da177e4SLinus Torvalds 
9621da177e4SLinus Torvalds static struct inet6_protocol sctpv6_protocol = {
9631da177e4SLinus Torvalds 	.handler      = sctp6_rcv,
9641da177e4SLinus Torvalds 	.err_handler  = sctp_v6_err,
9651da177e4SLinus Torvalds 	.flags        = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
9661da177e4SLinus Torvalds };
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds static struct sctp_af sctp_ipv6_specific = {
969543d9cfeSArnaldo Carvalho de Melo 	.sa_family	   = AF_INET6,
9701da177e4SLinus Torvalds 	.sctp_xmit	   = sctp_v6_xmit,
9711da177e4SLinus Torvalds 	.setsockopt	   = ipv6_setsockopt,
9721da177e4SLinus Torvalds 	.getsockopt	   = ipv6_getsockopt,
9731da177e4SLinus Torvalds 	.get_dst	   = sctp_v6_get_dst,
9741da177e4SLinus Torvalds 	.get_saddr	   = sctp_v6_get_saddr,
9751da177e4SLinus Torvalds 	.copy_addrlist	   = sctp_v6_copy_addrlist,
9761da177e4SLinus Torvalds 	.from_skb	   = sctp_v6_from_skb,
9771da177e4SLinus Torvalds 	.from_sk	   = sctp_v6_from_sk,
9781da177e4SLinus Torvalds 	.to_sk_saddr	   = sctp_v6_to_sk_saddr,
9791da177e4SLinus Torvalds 	.to_sk_daddr	   = sctp_v6_to_sk_daddr,
9801da177e4SLinus Torvalds 	.from_addr_param   = sctp_v6_from_addr_param,
9811da177e4SLinus Torvalds 	.to_addr_param	   = sctp_v6_to_addr_param,
9821da177e4SLinus Torvalds 	.dst_saddr	   = sctp_v6_dst_saddr,
9831da177e4SLinus Torvalds 	.cmp_addr	   = sctp_v6_cmp_addr,
9841da177e4SLinus Torvalds 	.scope		   = sctp_v6_scope,
9851da177e4SLinus Torvalds 	.addr_valid	   = sctp_v6_addr_valid,
9861da177e4SLinus Torvalds 	.inaddr_any	   = sctp_v6_inaddr_any,
9871da177e4SLinus Torvalds 	.is_any		   = sctp_v6_is_any,
9881da177e4SLinus Torvalds 	.available	   = sctp_v6_available,
9891da177e4SLinus Torvalds 	.skb_iif	   = sctp_v6_skb_iif,
9901da177e4SLinus Torvalds 	.is_ce		   = sctp_v6_is_ce,
9911da177e4SLinus Torvalds 	.seq_dump_addr	   = sctp_v6_seq_dump_addr,
9921da177e4SLinus Torvalds 	.net_header_len	   = sizeof(struct ipv6hdr),
9931da177e4SLinus Torvalds 	.sockaddr_len	   = sizeof(struct sockaddr_in6),
994543d9cfeSArnaldo Carvalho de Melo #ifdef CONFIG_COMPAT
995543d9cfeSArnaldo Carvalho de Melo 	.compat_setsockopt = compat_ipv6_setsockopt,
996543d9cfeSArnaldo Carvalho de Melo 	.compat_getsockopt = compat_ipv6_getsockopt,
997543d9cfeSArnaldo Carvalho de Melo #endif
9981da177e4SLinus Torvalds };
9991da177e4SLinus Torvalds 
10001da177e4SLinus Torvalds static struct sctp_pf sctp_pf_inet6_specific = {
10011da177e4SLinus Torvalds 	.event_msgname = sctp_inet6_event_msgname,
10021da177e4SLinus Torvalds 	.skb_msgname   = sctp_inet6_skb_msgname,
10031da177e4SLinus Torvalds 	.af_supported  = sctp_inet6_af_supported,
10041da177e4SLinus Torvalds 	.cmp_addr      = sctp_inet6_cmp_addr,
10051da177e4SLinus Torvalds 	.bind_verify   = sctp_inet6_bind_verify,
10061da177e4SLinus Torvalds 	.send_verify   = sctp_inet6_send_verify,
10071da177e4SLinus Torvalds 	.supported_addrs = sctp_inet6_supported_addrs,
10081da177e4SLinus Torvalds 	.create_accept_sk = sctp_v6_create_accept_sk,
10091da177e4SLinus Torvalds 	.addr_v4map    = sctp_v6_addr_v4map,
10101da177e4SLinus Torvalds 	.af            = &sctp_ipv6_specific,
10111da177e4SLinus Torvalds };
10121da177e4SLinus Torvalds 
1013827bf122SSridhar Samudrala /* Initialize IPv6 support and register with socket layer.  */
10141da177e4SLinus Torvalds int sctp_v6_init(void)
10151da177e4SLinus Torvalds {
1016827bf122SSridhar Samudrala 	int rc;
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 	/* Register the SCTP specific PF_INET6 functions. */
10191da177e4SLinus Torvalds 	sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	/* Register the SCTP specific AF_INET6 functions. */
10221da177e4SLinus Torvalds 	sctp_register_af(&sctp_ipv6_specific);
10231da177e4SLinus Torvalds 
1024827bf122SSridhar Samudrala 	rc = proto_register(&sctpv6_prot, 1);
1025827bf122SSridhar Samudrala 	if (rc)
1026827bf122SSridhar Samudrala 		return rc;
1027827bf122SSridhar Samudrala 
1028827bf122SSridhar Samudrala 	/* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
1029827bf122SSridhar Samudrala 	inet6_register_protosw(&sctpv6_seqpacket_protosw);
1030827bf122SSridhar Samudrala 	inet6_register_protosw(&sctpv6_stream_protosw);
1031827bf122SSridhar Samudrala 
1032827bf122SSridhar Samudrala 	return 0;
1033827bf122SSridhar Samudrala }
1034827bf122SSridhar Samudrala 
1035827bf122SSridhar Samudrala /* Register with inet6 layer. */
1036827bf122SSridhar Samudrala int sctp_v6_add_protocol(void)
1037827bf122SSridhar Samudrala {
10381da177e4SLinus Torvalds 	/* Register notifier for inet6 address additions/deletions. */
10391da177e4SLinus Torvalds 	register_inet6addr_notifier(&sctp_inet6addr_notifier);
1040827bf122SSridhar Samudrala 
1041827bf122SSridhar Samudrala 	if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1042827bf122SSridhar Samudrala 		return -EAGAIN;
1043827bf122SSridhar Samudrala 
1044827bf122SSridhar Samudrala 	return 0;
10451da177e4SLinus Torvalds }
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds /* IPv6 specific exit support. */
10481da177e4SLinus Torvalds void sctp_v6_exit(void)
10491da177e4SLinus Torvalds {
10501da177e4SLinus Torvalds 	inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
10511da177e4SLinus Torvalds 	inet6_unregister_protosw(&sctpv6_stream_protosw);
10521da177e4SLinus Torvalds 	proto_unregister(&sctpv6_prot);
1053827bf122SSridhar Samudrala 	list_del(&sctp_ipv6_specific.list);
1054827bf122SSridhar Samudrala }
1055827bf122SSridhar Samudrala 
1056827bf122SSridhar Samudrala /* Unregister with inet6 layer. */
1057827bf122SSridhar Samudrala void sctp_v6_del_protocol(void)
1058827bf122SSridhar Samudrala {
1059827bf122SSridhar Samudrala 	inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1060827bf122SSridhar Samudrala 	unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
10611da177e4SLinus Torvalds }
1062