Home | History | Annotate | Line # | Download | only in netinet6
sctp6_usrreq.c revision 1.8
      1 /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
      2 /* $NetBSD: sctp6_usrreq.c,v 1.8 2016/08/01 03:15:31 ozaki-r Exp $ */
      3 
      4 /*
      5  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Cisco Systems, Inc.
     19  * 4. Neither the name of the project nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 #include <sys/cdefs.h>
     36 __KERNEL_RCSID(0, "$NetBSD: sctp6_usrreq.c,v 1.8 2016/08/01 03:15:31 ozaki-r Exp $");
     37 
     38 #ifdef _KERNEL_OPT
     39 #include "opt_inet.h"
     40 #include "opt_ipsec.h"
     41 #include "opt_sctp.h"
     42 #endif /* _KERNEL_OPT */
     43 
     44 #include <sys/param.h>
     45 #include <sys/kernel.h>
     46 #include <sys/mbuf.h>
     47 #include <sys/domain.h>
     48 #include <sys/protosw.h>
     49 #include <sys/socket.h>
     50 #include <sys/malloc.h>
     51 #include <sys/socketvar.h>
     52 #include <sys/sysctl.h>
     53 #include <sys/errno.h>
     54 #include <sys/stat.h>
     55 #include <sys/systm.h>
     56 #include <sys/syslog.h>
     57 #include <sys/proc.h>
     58 #include <net/if.h>
     59 #include <net/route.h>
     60 #include <net/if_types.h>
     61 #include <netinet/in.h>
     62 #include <netinet/in_systm.h>
     63 #include <netinet/ip.h>
     64 #include <netinet/in_pcb.h>
     65 #include <netinet/in_var.h>
     66 #include <netinet/ip_var.h>
     67 #include <netinet/sctp_pcb.h>
     68 #include <netinet/sctp_header.h>
     69 #include <netinet/sctp_var.h>
     70 #include <netinet/sctputil.h>
     71 #include <netinet/sctp_output.h>
     72 #include <netinet/sctp_input.h>
     73 #include <netinet/sctp_asconf.h>
     74 #include <netinet6/ip6_var.h>
     75 #include <netinet6/scope6_var.h>
     76 #include <netinet/ip6.h>
     77 #include <netinet6/in6_pcb.h>
     78 #include <netinet/icmp6.h>
     79 #include <netinet6/sctp6_var.h>
     80 #include <netinet6/ip6protosw.h>
     81 #include <netinet6/nd6.h>
     82 
     83 #ifdef IPSEC
     84 #include <netipsec/ipsec.h>
     85 #include <netipsec/ipsec6.h>
     86 #endif /*IPSEC*/
     87 
     88 #if defined(NFAITH) && NFAITH > 0
     89 #include <net/if_faith.h>
     90 #endif
     91 
     92 #include <net/net_osdep.h>
     93 
     94 extern struct protosw inetsw[];
     95 
     96 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
     97 #ifndef in6pcb
     98 #define in6pcb		inpcb
     99 #endif
    100 #ifndef sotoin6pcb
    101 #define sotoin6pcb      sotoinpcb
    102 #endif
    103 #endif
    104 
    105 #ifdef SCTP_DEBUG
    106 extern u_int32_t sctp_debug_on;
    107 #endif
    108 
    109 static	int sctp6_detach(struct socket *so);
    110 
    111 extern int sctp_no_csum_on_loopback;
    112 
    113 int
    114 sctp6_input(struct mbuf **mp, int *offp, int proto)
    115 {
    116 	struct mbuf *m = *mp;
    117 	struct ip6_hdr *ip6;
    118 	struct sctphdr *sh;
    119 	struct sctp_inpcb *in6p = NULL;
    120 	struct sctp_nets *net;
    121 	int refcount_up = 0;
    122 	u_int32_t check, calc_check;
    123 	struct inpcb *in6p_ip;
    124 	struct sctp_chunkhdr *ch;
    125 	struct mbuf *opts = NULL;
    126 	int length, mlen, offset, iphlen;
    127 	u_int8_t ecn_bits;
    128 	struct sctp_tcb *stcb = NULL;
    129 	int off = *offp;
    130 	int s;
    131 
    132 	ip6 = mtod(m, struct ip6_hdr *);
    133 	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
    134 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, sizeof(*sh) + sizeof(*ch));
    135 	if (sh == NULL) {
    136 		sctp_pegs[SCTP_HDR_DROPS]++;
    137 		return IPPROTO_DONE;
    138 	}
    139 	ch = (struct sctp_chunkhdr *)((vaddr_t)sh + sizeof(struct sctphdr));
    140 
    141 	iphlen = off;
    142 	offset = iphlen + sizeof(*sh) + sizeof(*ch);
    143 
    144 #if defined(NFAITH) && NFAITH > 0
    145 	if (faithprefix(&ip6->ip6_dst))
    146 		goto bad;
    147 #endif /* NFAITH defined and > 0 */
    148 	sctp_pegs[SCTP_INPKTS]++;
    149 #ifdef SCTP_DEBUG
    150 	if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
    151 		printf("V6 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
    152 	}
    153 #endif
    154  	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
    155 		/* No multi-cast support in SCTP */
    156 		sctp_pegs[SCTP_IN_MCAST]++;
    157 		goto bad;
    158 	}
    159 	/* destination port of 0 is illegal, based on RFC2960. */
    160 	if (sh->dest_port == 0)
    161 		goto bad;
    162 	if ((sctp_no_csum_on_loopback == 0) ||
    163 	   (m_get_rcvif_NOMPSAFE(m) == NULL) ||
    164 	   (m_get_rcvif_NOMPSAFE(m)->if_type != IFT_LOOP)) {
    165 		/* we do NOT validate things from the loopback if the
    166 		 * sysctl is set to 1.
    167 		 */
    168 		check = sh->checksum;		/* save incoming checksum */
    169 		if ((check == 0) && (sctp_no_csum_on_loopback)) {
    170 			/* special hook for where we got a local address
    171 			 * somehow routed across a non IFT_LOOP type interface
    172 			 */
    173 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
    174 				goto sctp_skip_csum;
    175 		}
    176 		sh->checksum = 0;		/* prepare for calc */
    177 		calc_check = sctp_calculate_sum(m, &mlen, iphlen);
    178 		if (calc_check != check) {
    179 #ifdef SCTP_DEBUG
    180 			if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
    181 				printf("Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
    182 				       calc_check, check, m,
    183 				       mlen, iphlen);
    184 			}
    185 #endif
    186 			stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
    187 							 sh, ch, &in6p, &net);
    188 			/* in6p's ref-count increased && stcb locked */
    189 			if ((in6p) && (stcb)) {
    190 				sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
    191 				sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
    192 			}  else if ((in6p != NULL) && (stcb == NULL)) {
    193 				refcount_up = 1;
    194 			}
    195 			sctp_pegs[SCTP_BAD_CSUM]++;
    196 			goto bad;
    197 		}
    198 		sh->checksum = calc_check;
    199 	} else {
    200 sctp_skip_csum:
    201 		mlen = m->m_pkthdr.len;
    202 	}
    203 	net = NULL;
    204 	/*
    205 	 * Locate pcb and tcb for datagram
    206 	 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
    207 	 */
    208 #ifdef SCTP_DEBUG
    209 	if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
    210 		printf("V6 Find the association\n");
    211 	}
    212 #endif
    213 	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
    214 	    sh, ch, &in6p, &net);
    215 	/* in6p's ref-count increased */
    216 	if (in6p == NULL) {
    217 		struct sctp_init_chunk *init_chk, chunk_buf;
    218 
    219 		sctp_pegs[SCTP_NOPORTS]++;
    220 		if (ch->chunk_type == SCTP_INITIATION) {
    221 			/* we do a trick here to get the INIT tag,
    222 			 * dig in and get the tag from the INIT and
    223 			 * put it in the common header.
    224 			 */
    225 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
    226 			    iphlen + sizeof(*sh), sizeof(*init_chk),
    227 			    (u_int8_t *)&chunk_buf);
    228 			sh->v_tag = init_chk->init.initiate_tag;
    229 		}
    230 		sctp_send_abort(m, iphlen, sh, 0, NULL);
    231 		goto bad;
    232 	} else if (stcb == NULL) {
    233 		refcount_up = 1;
    234 	}
    235 	in6p_ip = (struct inpcb *)in6p;
    236 #ifdef IPSEC
    237 	/*
    238 	 * Check AH/ESP integrity.
    239 	 */
    240 	if (ipsec_used && ipsec6_in_reject_so(m, in6p->sctp_socket)) {
    241 /* XXX */
    242 #if 0
    243 		/* FIX ME: need to find right stat */
    244 		ipsec6stat.in_polvio++;
    245 #endif
    246 		goto bad;
    247 	}
    248 #endif /*IPSEC*/
    249 
    250 	/*
    251 	 * Construct sockaddr format source address.
    252 	 * Stuff source address and datagram in user buffer.
    253 	 */
    254 	if ((in6p->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
    255 #ifndef __OpenBSD__
    256 	    || (in6p->sctp_socket->so_options & SO_TIMESTAMP)
    257 #endif
    258 	    ) {
    259 #if defined(__FreeBSD__) || defined(__APPLE__)
    260 #if (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version < 501113) || defined(__APPLE__)
    261 		ip6_savecontrol(in6p_ip, &opts, ip6, m);
    262 #elif __FreeBSD_version >= 440000 || (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version >= 501113)
    263 		ip6_savecontrol(in6p_ip, m, &opts);
    264 #else
    265 		ip6_savecontrol(in6p_ip, m, &opts, NULL);
    266 #endif
    267 #elif defined(__NetBSD__)
    268 		ip6_savecontrol((struct in6pcb *)in6p_ip, &opts, ip6, m);
    269 #else
    270 		ip6_savecontrol((struct in6pcb *)in6p_ip, m, &opts);
    271 #endif
    272 	}
    273 
    274 	/*
    275 	 * CONTROL chunk processing
    276 	 */
    277 	length = ntohs(ip6->ip6_plen) + iphlen;
    278 	offset -= sizeof(*ch);
    279 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
    280 	s = splsoftnet();
    281 	(void)sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
    282 	    in6p, stcb, net, ecn_bits);
    283 	/* inp's ref-count reduced && stcb unlocked */
    284 	splx(s);
    285 	/* XXX this stuff below gets moved to appropriate parts later... */
    286 	if (m)
    287 		m_freem(m);
    288 	if (opts)
    289 		m_freem(opts);
    290 
    291 	if ((in6p) && refcount_up){
    292 		/* reduce ref-count */
    293 		SCTP_INP_WLOCK(in6p);
    294 		SCTP_INP_DECR_REF(in6p);
    295 		SCTP_INP_WUNLOCK(in6p);
    296 	}
    297 
    298 	return IPPROTO_DONE;
    299 
    300 bad:
    301 	if (stcb) {
    302 		SCTP_TCB_UNLOCK(stcb);
    303 	}
    304 
    305 	if ((in6p) && refcount_up){
    306 		/* reduce ref-count */
    307 		SCTP_INP_WLOCK(in6p);
    308 		SCTP_INP_DECR_REF(in6p);
    309 		SCTP_INP_WUNLOCK(in6p);
    310 	}
    311 	if (m) {
    312 		m_freem(m);
    313 	}
    314 	if (opts) {
    315 		m_freem(opts);
    316 	}
    317 	return IPPROTO_DONE;
    318 }
    319 
    320 
    321 static void
    322 sctp6_notify_mbuf(struct sctp_inpcb *inp,
    323 		  struct icmp6_hdr *icmp6,
    324 		  struct sctphdr *sh,
    325 		  struct sctp_tcb *stcb,
    326 		  struct sctp_nets *net)
    327 {
    328 	unsigned int nxtsz;
    329 
    330 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
    331 	    (icmp6 == NULL) || (sh == NULL)) {
    332 		goto out;
    333 	}
    334 
    335 	/* First do we even look at it? */
    336 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
    337 		goto out;
    338 
    339 	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
    340 		/* not PACKET TO BIG */
    341 		goto out;
    342 	}
    343 	/*
    344 	 * ok we need to look closely. We could even get smarter and
    345 	 * look at anyone that we sent to in case we get a different
    346 	 * ICMP that tells us there is no way to reach a host, but for
    347 	 * this impl, all we care about is MTU discovery.
    348 	 */
    349 	nxtsz = ntohl(icmp6->icmp6_mtu);
    350 	/* Stop any PMTU timer */
    351 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
    352 
    353 	/* Adjust destination size limit */
    354 	if (net->mtu > nxtsz) {
    355 		net->mtu = nxtsz;
    356 	}
    357 	/* now what about the ep? */
    358 	if (stcb->asoc.smallest_mtu > nxtsz) {
    359 		struct sctp_tmit_chunk *chk;
    360 		struct sctp_stream_out *strm;
    361 		/* Adjust that too */
    362 		stcb->asoc.smallest_mtu = nxtsz;
    363 		/* now off to subtract IP_DF flag if needed */
    364 
    365 		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
    366 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
    367 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    368 			}
    369 		}
    370 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
    371 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
    372 				/*
    373 				 * For this guy we also mark for immediate
    374 				 * resend since we sent to big of chunk
    375 				 */
    376 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    377 				if (chk->sent != SCTP_DATAGRAM_RESEND)
    378 					stcb->asoc.sent_queue_retran_cnt++;
    379 				chk->sent = SCTP_DATAGRAM_RESEND;
    380 				chk->rec.data.doing_fast_retransmit = 0;
    381 
    382 				chk->sent = SCTP_DATAGRAM_RESEND;
    383 				/* Clear any time so NO RTT is being done */
    384 				chk->sent_rcv_time.tv_sec = 0;
    385 				chk->sent_rcv_time.tv_usec = 0;
    386 				stcb->asoc.total_flight -= chk->send_size;
    387 				net->flight_size -= chk->send_size;
    388 			}
    389 		}
    390 		TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
    391 			TAILQ_FOREACH(chk, &strm->outqueue, sctp_next) {
    392 				if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
    393 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
    394 				}
    395 			}
    396 		}
    397 	}
    398 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
    399 out:
    400 	if (inp) {
    401 		/* reduce inp's ref-count */
    402 		SCTP_INP_WLOCK(inp);
    403 		SCTP_INP_DECR_REF(inp);
    404 		SCTP_INP_WUNLOCK(inp);
    405 	}
    406 	if (stcb) {
    407 		SCTP_TCB_UNLOCK(stcb);
    408 	}
    409 }
    410 
    411 
    412 void *
    413 sctp6_ctlinput(int cmd, const struct sockaddr *pktdst, void *d)
    414 {
    415 	struct sctphdr sh;
    416 	struct ip6ctlparam *ip6cp = NULL;
    417 	int s, cm;
    418 
    419 	if (pktdst->sa_family != AF_INET6 ||
    420 	    pktdst->sa_len != sizeof(struct sockaddr_in6))
    421 		return NULL;
    422 
    423 	if ((unsigned)cmd >= PRC_NCMDS)
    424 		return NULL;
    425 	if (PRC_IS_REDIRECT(cmd)) {
    426 		d = NULL;
    427 	} else if (inet6ctlerrmap[cmd] == 0) {
    428 		return NULL;
    429 	}
    430 
    431 	/* if the parameter is from icmp6, decode it. */
    432 	if (d != NULL) {
    433 		ip6cp = (struct ip6ctlparam *)d;
    434 	} else {
    435 		ip6cp = (struct ip6ctlparam *)NULL;
    436 	}
    437 
    438 	if (ip6cp) {
    439 		/*
    440 		 * XXX: We assume that when IPV6 is non NULL,
    441 		 * M and OFF are valid.
    442 		 */
    443 		/* check if we can safely examine src and dst ports */
    444 		struct sctp_inpcb *inp;
    445 		struct sctp_tcb *stcb;
    446 		struct sctp_nets *net;
    447 		struct sockaddr_in6 final;
    448 
    449 		if (ip6cp->ip6c_m == NULL ||
    450 		    (size_t)ip6cp->ip6c_m->m_pkthdr.len < (ip6cp->ip6c_off + sizeof(sh)))
    451 			return NULL;
    452 
    453 		memset(&sh, 0, sizeof(sh));
    454 		memset(&final, 0, sizeof(final));
    455 		inp = NULL;
    456 		net = NULL;
    457 		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
    458 		    (void *)&sh);
    459 		ip6cp->ip6c_src->sin6_port = sh.src_port;
    460 		final.sin6_len = sizeof(final);
    461 		final.sin6_family = AF_INET6;
    462 		final.sin6_addr = ((const struct sockaddr_in6 *)pktdst)->sin6_addr;
    463 		final.sin6_port = sh.dest_port;
    464 		s = splsoftnet();
    465 		stcb = sctp_findassociation_addr_sa(sin6tosa(ip6cp->ip6c_src),
    466 						    sin6tosa(&final),
    467 						    &inp, &net, 1);
    468 		/* inp's ref-count increased && stcb locked */
    469 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
    470 			if (cmd == PRC_MSGSIZE) {
    471 				sctp6_notify_mbuf(inp,
    472 						  ip6cp->ip6c_icmp6,
    473 						  &sh,
    474 						  stcb,
    475 						  net);
    476 				/* inp's ref-count reduced && stcb unlocked */
    477 			} else {
    478 				if (cmd == PRC_HOSTDEAD) {
    479 					cm = EHOSTUNREACH;
    480 				} else {
    481 					cm = inet6ctlerrmap[cmd];
    482 				}
    483 				sctp_notify(inp, cm, &sh, sin6tosa(&final),
    484 					    stcb, net);
    485 				/* inp's ref-count reduced && stcb unlocked */
    486 			}
    487 		} else {
    488 			if (PRC_IS_REDIRECT(cmd) && inp) {
    489 				in6_rtchange((struct in6pcb *)inp,
    490 					     inet6ctlerrmap[cmd]);
    491 			}
    492 			if (inp) {
    493 				/* reduce inp's ref-count */
    494 				SCTP_INP_WLOCK(inp);
    495 				SCTP_INP_DECR_REF(inp);
    496 				SCTP_INP_WUNLOCK(inp);
    497 			}
    498 			if (stcb) {
    499 				SCTP_TCB_UNLOCK(stcb);
    500 			}
    501 		}
    502 		splx(s);
    503 	}
    504 	return NULL;
    505 }
    506 
    507 /*
    508  * this routine can probably be collasped into the one in sctp_userreq.c
    509  * since they do the same thing and now we lookup with a sockaddr
    510  */
    511 #ifdef __FreeBSD__
    512 static int
    513 sctp6_getcred(SYSCTL_HANDLER_ARGS)
    514 {
    515 	struct sockaddr_in6 addrs[2];
    516 	struct sctp_inpcb *inp;
    517 	struct sctp_nets *net;
    518 	struct sctp_tcb *stcb;
    519 	int error, s;
    520 
    521 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
    522 	error = suser(req->td);
    523 #else
    524 	error = suser(req->p);
    525 #endif
    526 	if (error)
    527 		return (error);
    528 
    529 	if (req->newlen != sizeof(addrs))
    530 		return (EINVAL);
    531 	if (req->oldlen != sizeof(struct ucred))
    532 		return (EINVAL);
    533 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
    534 	if (error)
    535 		return (error);
    536 	s = splsoftnet();
    537 
    538         stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
    539                                            sin6tosa(&addrs[1]),
    540                                            &inp, &net, 1);
    541 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
    542 		error = ENOENT;
    543 		if (inp) {
    544 			SCTP_INP_WLOCK(inp);
    545 			SCTP_INP_DECR_REF(inp);
    546 			SCTP_INP_WUNLOCK(inp);
    547 		}
    548 		goto out;
    549 	}
    550 	error = SYSCTL_OUT(req, inp->sctp_socket->so_cred,
    551 			   sizeof(struct ucred));
    552 
    553 	SCTP_TCB_UNLOCK (stcb);
    554  out:
    555 	splx(s);
    556 	return (error);
    557 }
    558 
    559 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
    560 	    0, 0,
    561 	    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
    562 
    563 #endif
    564 
    565 /* This is the same as the sctp_abort() could be made common */
    566 static int
    567 sctp6_abort(struct socket *so)
    568 {
    569 	int s;
    570 	struct sctp_inpcb *inp;
    571 
    572 	KASSERT(solocked(so));
    573 
    574 	s = splsoftnet();
    575 	inp = (struct sctp_inpcb *)so->so_pcb;
    576 	if (inp == 0)
    577 		return EINVAL;	/* ??? possible? panic instead? */
    578 	soisdisconnected(so);
    579 	sctp_inpcb_free(inp, 1);
    580 	splx(s);
    581 	return 0;
    582 }
    583 
    584 static int
    585 sctp6_attach(struct socket *so, int proto)
    586 {
    587 	struct in6pcb *inp6;
    588 	int error;
    589 	struct sctp_inpcb *inp;
    590 
    591 	sosetlock(so);
    592 	inp = (struct sctp_inpcb *)so->so_pcb;
    593 	if (inp != NULL)
    594 		return EINVAL;
    595 
    596 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    597 		error = soreserve(so, sctp_sendspace, sctp_recvspace);
    598 		if (error)
    599 			return error;
    600 	}
    601 	error = sctp_inpcb_alloc(so);
    602 	if (error)
    603 		return error;
    604 	inp = (struct sctp_inpcb *)so->so_pcb;
    605 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
    606 	inp6 = (struct in6pcb *)inp;
    607 
    608 	inp->inp_vflag |=  INP_IPV6;
    609 	if (ip6_v6only) {
    610 		inp6->in6p_flags |= IN6P_IPV6_V6ONLY;
    611 	}
    612 	so->so_send = sctp_sosend;
    613 
    614 	inp6->in6p_hops = -1;	        /* use kernel default */
    615 	inp6->in6p_cksum = -1;	/* just to be sure */
    616 #ifdef INET
    617 	/*
    618 	 * XXX: ugly!!
    619 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
    620 	 * because the socket may be bound to an IPv6 wildcard address,
    621 	 * which may match an IPv4-mapped IPv6 address.
    622 	 */
    623 	inp->inp_ip_ttl = ip_defttl;
    624 #endif
    625 	/*
    626 	 * Hmm what about the IPSEC stuff that is missing here but
    627 	 * in sctp_attach()?
    628 	 */
    629 	return 0;
    630 }
    631 
    632 static int
    633 sctp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    634 {
    635 	struct sctp_inpcb *inp;
    636 	struct in6pcb *inp6;
    637 	int error;
    638 
    639 	KASSERT(solocked(so));
    640 
    641 	inp = (struct sctp_inpcb *)so->so_pcb;
    642 	if (inp == 0)
    643 		return EINVAL;
    644 
    645 	inp6 = (struct in6pcb *)inp;
    646 	inp->inp_vflag &= ~INP_IPV4;
    647 	inp->inp_vflag |= INP_IPV6;
    648 
    649 	if (nam != NULL && (inp6->in6p_flags & IN6P_IPV6_V6ONLY) == 0) {
    650 		if (nam->sa_family == AF_INET) {
    651 			/* binding v4 addr to v6 socket, so reset flags */
    652 			inp->inp_vflag |= INP_IPV4;
    653 			inp->inp_vflag &= ~INP_IPV6;
    654 		} else {
    655 			struct sockaddr_in6 *sin6_p;
    656 			sin6_p = (struct sockaddr_in6 *)nam;
    657 
    658 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
    659 			  inp->inp_vflag |= INP_IPV4;
    660 			}
    661 			else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
    662 				struct sockaddr_in sin;
    663 				in6_sin6_2_sin(&sin, sin6_p);
    664 				inp->inp_vflag |= INP_IPV4;
    665 				inp->inp_vflag &= ~INP_IPV6;
    666 				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, l);
    667 				return error;
    668 			}
    669 		}
    670 	} else if (nam != NULL) {
    671 		/* IPV6_V6ONLY socket */
    672 		if (nam->sa_family == AF_INET) {
    673 			/* can't bind v4 addr to v6 only socket! */
    674 			return EINVAL;
    675 		} else {
    676 			struct sockaddr_in6 *sin6_p;
    677 			sin6_p = (struct sockaddr_in6 *)nam;
    678 
    679 			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
    680 				/* can't bind v4-mapped addrs either! */
    681 				/* NOTE: we don't support SIIT */
    682 				return EINVAL;
    683 		}
    684 	}
    685 	error = sctp_inpcb_bind(so, nam, l);
    686 	return error;
    687 }
    688 
    689 /*This could be made common with sctp_detach() since they are identical */
    690 static int
    691 sctp6_detach(struct socket *so)
    692 {
    693 	struct sctp_inpcb *inp;
    694 
    695 	inp = (struct sctp_inpcb *)so->so_pcb;
    696 	if (inp == 0)
    697 		return EINVAL;
    698 
    699 	if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
    700 	    (so->so_rcv.sb_cc > 0))
    701 		sctp_inpcb_free(inp, 1);
    702 	else
    703 		sctp_inpcb_free(inp, 0);
    704 	return 0;
    705 }
    706 
    707 static int
    708 sctp6_disconnect(struct socket *so)
    709 {
    710 	struct sctp_inpcb *inp;
    711 
    712 	inp = (struct sctp_inpcb *)so->so_pcb;
    713 	if (inp == NULL) {
    714 		return (ENOTCONN);
    715 	}
    716 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
    717 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
    718 			/* No connection */
    719 			return (ENOTCONN);
    720 		} else {
    721 			int some_on_streamwheel = 0;
    722 			struct sctp_association *asoc;
    723 			struct sctp_tcb *stcb;
    724 
    725 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
    726 			if (stcb == NULL) {
    727 				return (EINVAL);
    728 			}
    729 			asoc = &stcb->asoc;
    730 			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
    731 				/* Check to see if some data queued */
    732 				struct sctp_stream_out *outs;
    733 				TAILQ_FOREACH(outs, &asoc->out_wheel,
    734 					      next_spoke) {
    735 					if (!TAILQ_EMPTY(&outs->outqueue)) {
    736 						some_on_streamwheel = 1;
    737 						break;
    738 					}
    739 				}
    740 			}
    741 
    742 			if (TAILQ_EMPTY(&asoc->send_queue) &&
    743 			    TAILQ_EMPTY(&asoc->sent_queue) &&
    744 			    (some_on_streamwheel == 0)) {
    745 				/* nothing queued to send, so I'm done... */
    746 				if ((SCTP_GET_STATE(asoc) !=
    747 				     SCTP_STATE_SHUTDOWN_SENT) &&
    748 				    (SCTP_GET_STATE(asoc) !=
    749 				     SCTP_STATE_SHUTDOWN_ACK_SENT)) {
    750 					/* only send SHUTDOWN the first time */
    751 #ifdef SCTP_DEBUG
    752 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    753 						printf("%s:%d sends a shutdown\n",
    754 						       __FILE__,
    755 						       __LINE__
    756 							);
    757 					}
    758 #endif
    759 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
    760 					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
    761 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
    762 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
    763 							 stcb->sctp_ep, stcb,
    764 							 asoc->primary_destination);
    765 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
    766 							 stcb->sctp_ep, stcb,
    767 							 asoc->primary_destination);
    768 				}
    769 			} else {
    770 				/*
    771 				 * we still got (or just got) data to send,
    772 				 * so set SHUTDOWN_PENDING
    773 				 */
    774 				/*
    775 				 * XXX sockets draft says that MSG_EOF should
    776 				 * be sent with no data.  currently, we will
    777 				 * allow user data to be sent first and move
    778 				 * to SHUTDOWN-PENDING
    779 				 */
    780 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
    781 			}
    782 			return (0);
    783 		}
    784 	} else {
    785 		/* UDP model does not support this */
    786 		return EOPNOTSUPP;
    787 	}
    788 }
    789 
    790 static int
    791 sctp6_recvoob(struct socket *so, struct mbuf *m, int flags)
    792 {
    793 	KASSERT(solocked(so));
    794 
    795 	return EOPNOTSUPP;
    796 }
    797 
    798 static int
    799 sctp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
    800 	   struct mbuf *control, struct lwp *l)
    801 {
    802 	struct sctp_inpcb *inp;
    803 	struct in6pcb *inp6;
    804 #ifdef INET
    805 	struct sockaddr_in6 *sin6;
    806 #endif /* INET */
    807 	/* No SPL needed since sctp_output does this */
    808 
    809 	inp = (struct sctp_inpcb *)so->so_pcb;
    810 	if (inp == NULL) {
    811 	        if (control) {
    812 			m_freem(control);
    813 			control = NULL;
    814 		}
    815 		m_freem(m);
    816 		return EINVAL;
    817 	}
    818 	inp6 = (struct in6pcb *)inp;
    819 	/* For the TCP model we may get a NULL addr, if we
    820 	 * are a connected socket thats ok.
    821 	 */
    822 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
    823 	    (nam == NULL)) {
    824 	        goto connected_type;
    825 	}
    826 	if (nam == NULL) {
    827 		m_freem(m);
    828 		if (control) {
    829 			m_freem(control);
    830 			control = NULL;
    831 		}
    832 		return (EDESTADDRREQ);
    833 	}
    834 
    835 #ifdef INET
    836 	sin6 = (struct sockaddr_in6 *)nam;
    837 	if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
    838 		/*
    839 		 * if IPV6_V6ONLY flag, we discard datagrams
    840 		 * destined to a v4 addr or v4-mapped addr
    841 		 */
    842 		if (nam->sa_family == AF_INET) {
    843 			return EINVAL;
    844 		}
    845 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    846 			return EINVAL;
    847 		}
    848 	}
    849 
    850 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    851 		if (!ip6_v6only) {
    852 			struct sockaddr_in sin;
    853 			/* convert v4-mapped into v4 addr and send */
    854 			in6_sin6_2_sin(&sin, sin6);
    855 			return sctp_send(so, m, (struct sockaddr *)&sin,
    856 			    control, l);
    857 		} else {
    858 			/* mapped addresses aren't enabled */
    859 			return EINVAL;
    860 		}
    861 	}
    862 #endif /* INET */
    863  connected_type:
    864 	/* now what about control */
    865 	if (control) {
    866 		if (inp->control) {
    867 			printf("huh? control set?\n");
    868 			m_freem(inp->control);
    869 			inp->control = NULL;
    870 		}
    871 		inp->control = control;
    872 	}
    873 	/* add it in possibly */
    874 	if ((inp->pkt) &&
    875 	    (inp->pkt->m_flags & M_PKTHDR)) {
    876 		struct mbuf *x;
    877 		int c_len;
    878 
    879 		c_len = 0;
    880 		/* How big is it */
    881 		for (x=m;x;x = x->m_next) {
    882 			c_len += x->m_len;
    883 		}
    884 		inp->pkt->m_pkthdr.len += c_len;
    885 	}
    886 	/* Place the data */
    887 	if (inp->pkt) {
    888 		inp->pkt_last->m_next = m;
    889 		inp->pkt_last = m;
    890 	} else {
    891 		inp->pkt_last = inp->pkt = m;
    892 	}
    893 	if ((so->so_state & SS_MORETOCOME) == 0) {
    894 		/*
    895 		 * note with the current version this code will only be
    896 		 * used by OpenBSD, NetBSD and FreeBSD have methods for
    897 		 * re-defining sosend() to use sctp_sosend().  One can
    898 		 * optionaly switch back to this code (by changing back
    899 		 * the defininitions but this is not advisable.
    900 		 */
    901 		int ret;
    902 		ret = sctp_output(inp, inp->pkt , nam, inp->control, l, 0);
    903 		inp->pkt = NULL;
    904 		inp->control = NULL;
    905 		return (ret);
    906 	} else {
    907 		return (0);
    908 	}
    909 }
    910 
    911 static int
    912 sctp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    913 {
    914 	KASSERT(solocked(so));
    915 
    916 	if (m)
    917 		m_freem(m);
    918 	if (control)
    919 		m_freem(control);
    920 
    921 	return EOPNOTSUPP;
    922 }
    923 
    924 static int
    925 sctp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
    926 {
    927 	int error = 0;
    928 	struct sctp_inpcb *inp;
    929 	struct in6pcb *inp6;
    930 	struct sctp_tcb *stcb;
    931 #ifdef INET
    932 	struct sockaddr_in6 *sin6;
    933 	struct sockaddr_storage ss;
    934 #endif /* INET */
    935 
    936 	inp6 = (struct in6pcb *)so->so_pcb;
    937 	inp = (struct sctp_inpcb *)so->so_pcb;
    938 	if (inp == 0) {
    939 		return (ECONNRESET);	/* I made the same as TCP since
    940 					 * we are not setup? */
    941 	}
    942 	SCTP_ASOC_CREATE_LOCK(inp);
    943 	SCTP_INP_RLOCK(inp);
    944 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
    945 	    SCTP_PCB_FLAGS_UNBOUND) {
    946 		/* Bind a ephemeral port */
    947 		SCTP_INP_RUNLOCK(inp);
    948 		error = sctp6_bind(so, NULL, l);
    949 		if (error) {
    950 			SCTP_ASOC_CREATE_UNLOCK(inp);
    951 
    952 			return (error);
    953 		}
    954 		SCTP_INP_RLOCK(inp);
    955 	}
    956 
    957 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
    958 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
    959 		/* We are already connected AND the TCP model */
    960 		SCTP_INP_RUNLOCK(inp);
    961 		SCTP_ASOC_CREATE_UNLOCK(inp);
    962 		return (EADDRINUSE);
    963 	}
    964 
    965 #ifdef INET
    966 	sin6 = (struct sockaddr_in6 *)nam;
    967 	if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
    968 		/*
    969 		 * if IPV6_V6ONLY flag, ignore connections
    970 		 * destined to a v4 addr or v4-mapped addr
    971 		 */
    972 		if (nam->sa_family == AF_INET) {
    973 			SCTP_INP_RUNLOCK(inp);
    974 			SCTP_ASOC_CREATE_UNLOCK(inp);
    975 			return EINVAL;
    976 		}
    977 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    978 			SCTP_INP_RUNLOCK(inp);
    979 			SCTP_ASOC_CREATE_UNLOCK(inp);
    980 			return EINVAL;
    981 		}
    982 	}
    983 
    984 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    985 		if (!ip6_v6only) {
    986 			/* convert v4-mapped into v4 addr */
    987 			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
    988 			nam = (struct sockaddr *)&ss;
    989 		} else {
    990 			/* mapped addresses aren't enabled */
    991 			SCTP_INP_RUNLOCK(inp);
    992 			SCTP_ASOC_CREATE_UNLOCK(inp);
    993 			return EINVAL;
    994 		}
    995 	}
    996 #endif /* INET */
    997 
    998 	/* Now do we connect? */
    999 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1000 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1001 		if (stcb) {
   1002 			SCTP_TCB_UNLOCK (stcb);
   1003 		}
   1004 		SCTP_INP_RUNLOCK(inp);
   1005 	} else {
   1006 		SCTP_INP_RUNLOCK(inp);
   1007 		SCTP_INP_WLOCK(inp);
   1008 		SCTP_INP_INCR_REF(inp);
   1009 		SCTP_INP_WUNLOCK(inp);
   1010 		stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL);
   1011 		if (stcb == NULL) {
   1012 			SCTP_INP_WLOCK(inp);
   1013 			SCTP_INP_DECR_REF(inp);
   1014 			SCTP_INP_WUNLOCK(inp);
   1015 		}
   1016 	}
   1017 
   1018 	if (stcb != NULL) {
   1019 		/* Already have or am bring up an association */
   1020 		SCTP_ASOC_CREATE_UNLOCK(inp);
   1021 		SCTP_TCB_UNLOCK (stcb);
   1022 		return (EALREADY);
   1023 	}
   1024 	/* We are GOOD to go */
   1025 	stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0);
   1026 	SCTP_ASOC_CREATE_UNLOCK(inp);
   1027 	if (stcb == NULL) {
   1028 		/* Gak! no memory */
   1029 		return (error);
   1030 	}
   1031 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
   1032 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
   1033 		/* Set the connected flag so we can queue data */
   1034 		soisconnecting(so);
   1035 	}
   1036 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
   1037 	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
   1038 	sctp_send_initiate(inp, stcb);
   1039 	SCTP_TCB_UNLOCK (stcb);
   1040 	return error;
   1041 }
   1042 
   1043 static int
   1044 sctp6_connect2(struct socket *so, struct socket *so2)
   1045 {
   1046 	KASSERT(solocked(so));
   1047 
   1048 	return EOPNOTSUPP;
   1049 }
   1050 
   1051 static int
   1052 sctp6_getaddr(struct socket *so, struct sockaddr *nam)
   1053 {
   1054 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
   1055 	struct sctp_inpcb *inp;
   1056 	int error;
   1057 
   1058 	/*
   1059 	 * Do the malloc first in case it blocks.
   1060 	 */
   1061 	memset(sin6, 0, sizeof(*sin6));
   1062 	sin6->sin6_family = AF_INET6;
   1063 	sin6->sin6_len = sizeof(*sin6);
   1064 
   1065 	inp = (struct sctp_inpcb *)so->so_pcb;
   1066 	if (inp == NULL) {
   1067 		return ECONNRESET;
   1068 	}
   1069 
   1070 	sin6->sin6_port = inp->sctp_lport;
   1071 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1072 		/* For the bound all case you get back 0 */
   1073 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1074 			struct sctp_tcb *stcb;
   1075 			const struct sockaddr_in6 *sin_a6;
   1076 			struct sctp_nets *net;
   1077 			int fnd;
   1078 
   1079 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1080 			if (stcb == NULL) {
   1081 				goto notConn6;
   1082 			}
   1083 			fnd = 0;
   1084 			sin_a6 = NULL;
   1085 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1086 				sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
   1087 				if (sin_a6->sin6_family == AF_INET6) {
   1088 					fnd = 1;
   1089 					break;
   1090 				}
   1091 			}
   1092 			if ((!fnd) || (sin_a6 == NULL)) {
   1093 				/* punt */
   1094 				goto notConn6;
   1095 			}
   1096 			sin6->sin6_addr = sctp_ipv6_source_address_selection(
   1097 			    inp, stcb, &net->ro, net, 0);
   1098 
   1099 		} else {
   1100 			/* For the bound all case you get back 0 */
   1101 		notConn6:
   1102 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
   1103 		}
   1104 	} else {
   1105 		/* Take the first IPv6 address in the list */
   1106 		struct sctp_laddr *laddr;
   1107 		int fnd = 0;
   1108 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   1109 			if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
   1110 				struct sockaddr_in6 *sin_a;
   1111 				sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
   1112 				sin6->sin6_addr = sin_a->sin6_addr;
   1113 				fnd = 1;
   1114 				break;
   1115 			}
   1116 		}
   1117 		if (!fnd) {
   1118 			return ENOENT;
   1119 		}
   1120 	}
   1121 	/* Scoping things for v6 */
   1122 	if ((error = sa6_recoverscope(sin6)) != 0)
   1123 		return (error);
   1124 
   1125 	return (0);
   1126 }
   1127 
   1128 static int
   1129 sctp6_peeraddr(struct socket *so, struct sockaddr *nam)
   1130 {
   1131 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
   1132 	int fnd, error;
   1133 	const struct sockaddr_in6 *sin_a6;
   1134 	struct sctp_inpcb *inp;
   1135 	struct sctp_tcb *stcb;
   1136 	struct sctp_nets *net;
   1137 	/*
   1138 	 * Do the malloc first in case it blocks.
   1139 	 */
   1140 	inp = (struct sctp_inpcb *)so->so_pcb;
   1141 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
   1142 		/* UDP type and listeners will drop out here */
   1143 		return (ENOTCONN);
   1144 	}
   1145 	memset(sin6, 0, sizeof(*sin6));
   1146 	sin6->sin6_family = AF_INET6;
   1147 	sin6->sin6_len = sizeof(*sin6);
   1148 
   1149 	/* We must recapture incase we blocked */
   1150 	inp = (struct sctp_inpcb *)so->so_pcb;
   1151 	if (inp == NULL) {
   1152 		return ECONNRESET;
   1153 	}
   1154 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1155 	if (stcb == NULL) {
   1156 		return ECONNRESET;
   1157 	}
   1158 	fnd = 0;
   1159 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1160 		sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
   1161 		if (sin_a6->sin6_family == AF_INET6) {
   1162 			fnd = 1;
   1163 			sin6->sin6_port = stcb->rport;
   1164 			sin6->sin6_addr = sin_a6->sin6_addr;
   1165 			break;
   1166 		}
   1167 	}
   1168 	if (!fnd) {
   1169 		/* No IPv4 address */
   1170 		return ENOENT;
   1171 	}
   1172 	if ((error = sa6_recoverscope(sin6)) != 0)
   1173 		return (error);
   1174 
   1175 	return (0);
   1176 }
   1177 
   1178 static int
   1179 sctp6_sockaddr(struct socket *so, struct sockaddr *nam)
   1180 {
   1181 	struct in6pcb *inp6 = sotoin6pcb(so);
   1182 	int	error;
   1183 
   1184 	if (inp6 == NULL)
   1185 		return EINVAL;
   1186 
   1187 	/* allow v6 addresses precedence */
   1188 	error = sctp6_getaddr(so, nam);
   1189 	if (error) {
   1190 		/* try v4 next if v6 failed */
   1191 		error = sctp_sockaddr(so, nam);
   1192 		if (error) {
   1193 			return (error);
   1194 		}
   1195 
   1196 		/* if I'm V6ONLY, convert it to v4-mapped */
   1197 		if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
   1198 			struct sockaddr_in6 sin6;
   1199 			in6_sin_2_v4mapsin6((struct sockaddr_in *)nam, &sin6);
   1200 			memcpy(nam, &sin6, sizeof(struct sockaddr_in6));
   1201 		}
   1202 	}
   1203 	return (error);
   1204 }
   1205 
   1206 #if 0
   1207 static int
   1208 sctp6_getpeeraddr(struct socket *so, struct sockaddr *nam)
   1209 {
   1210 	struct in6pcb *inp6 = sotoin6pcb(so);
   1211 	int	error;
   1212 
   1213 	if (inp6 == NULL)
   1214 		return EINVAL;
   1215 
   1216 	/* allow v6 addresses precedence */
   1217 	error = sctp6_peeraddr(so, nam);
   1218 	if (error) {
   1219 		/* try v4 next if v6 failed */
   1220 		error = sctp_peeraddr(so, nam);
   1221 		if (error) {
   1222 			return (error);
   1223 		}
   1224 		/* if I'm V6ONLY, convert it to v4-mapped */
   1225 		if ((inp6->in6p_flags & IN6P_IPV6_V6ONLY)) {
   1226 			struct sockaddr_in6 sin6;
   1227 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
   1228 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
   1229 		}
   1230 	}
   1231 	return error;
   1232 }
   1233 #endif
   1234 
   1235 static int
   1236 sctp6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
   1237 {
   1238 	int error = 0;
   1239 	int family;
   1240 
   1241 	family = so->so_proto->pr_domain->dom_family;
   1242 	switch (family) {
   1243 #ifdef INET
   1244 	case PF_INET:
   1245 		error = in_control(so, cmd, nam, ifp);
   1246 		break;
   1247 #endif
   1248 #ifdef INET6
   1249 	case PF_INET6:
   1250 		error = in6_control(so, cmd, nam, ifp);
   1251 		break;
   1252 #endif
   1253 	default:
   1254 		error = EAFNOSUPPORT;
   1255 	}
   1256 	return (error);
   1257 }
   1258 
   1259 static int
   1260 sctp6_accept(struct socket *so, struct sockaddr *nam)
   1261 {
   1262 	KASSERT(solocked(so));
   1263 
   1264 	return EOPNOTSUPP;
   1265 }
   1266 
   1267 static int
   1268 sctp6_stat(struct socket *so, struct stat *ub)
   1269 {
   1270 	return 0;
   1271 }
   1272 
   1273 static int
   1274 sctp6_listen(struct socket *so, struct lwp *l)
   1275 {
   1276 	return sctp_listen(so, l);
   1277 }
   1278 
   1279 static int
   1280 sctp6_shutdown(struct socket *so)
   1281 {
   1282 	return sctp_shutdown(so);
   1283 }
   1284 
   1285 static int
   1286 sctp6_rcvd(struct socket *so, int flags, struct lwp *l)
   1287 {
   1288 	KASSERT(solocked(so));
   1289 
   1290 	return sctp_rcvd(so, flags, l);
   1291 }
   1292 
   1293 static int
   1294 sctp6_purgeif(struct socket *so, struct ifnet *ifp)
   1295 {
   1296 	struct ifaddr *ifa;
   1297 	/* FIXME NOMPSAFE */
   1298 	IFADDR_READER_FOREACH(ifa, ifp) {
   1299 		if (ifa->ifa_addr->sa_family == PF_INET6) {
   1300 			sctp_delete_ip_address(ifa);
   1301 		}
   1302 	}
   1303 
   1304 	mutex_enter(softnet_lock);
   1305 	in6_purgeif(ifp);
   1306 	mutex_exit(softnet_lock);
   1307 
   1308 	return 0;
   1309 }
   1310 
   1311 PR_WRAP_USRREQS(sctp6)
   1312 #define	sctp6_attach	sctp6_attach_wrapper
   1313 #define	sctp6_detach	sctp6_detach_wrapper
   1314 #define sctp6_accept	sctp6_accept_wrapper
   1315 #define	sctp6_bind	sctp6_bind_wrapper
   1316 #define	sctp6_listen	sctp6_listen_wrapper
   1317 #define	sctp6_connect	sctp6_connect_wrapper
   1318 #define	sctp6_connect2	sctp6_connect2_wrapper
   1319 #define sctp6_disconnect	sctp6_disconnect_wrapper
   1320 #define sctp6_shutdown	sctp6_shutdown_wrapper
   1321 #define sctp6_abort	sctp6_abort_wrapper
   1322 #define	sctp6_ioctl	sctp6_ioctl_wrapper
   1323 #define	sctp6_stat	sctp6_stat_wrapper
   1324 #define	sctp6_peeraddr	sctp6_peeraddr_wrapper
   1325 #define sctp6_sockaddr	sctp6_sockaddr_wrapper
   1326 #define sctp6_rcvd	sctp6_rcvd_wrapper
   1327 #define sctp6_recvoob	sctp6_recvoob_wrapper
   1328 #define sctp6_send	sctp6_send_wrapper
   1329 #define sctp6_sendoob	sctp6_sendoob_wrapper
   1330 #define sctp6_purgeif	sctp6_purgeif_wrapper
   1331 
   1332 const struct pr_usrreqs sctp6_usrreqs = {
   1333 	.pr_attach	= sctp6_attach,
   1334 	.pr_detach	= sctp6_detach,
   1335 	.pr_accept	= sctp6_accept,
   1336 	.pr_bind	= sctp6_bind,
   1337 	.pr_listen	= sctp6_listen,
   1338 	.pr_connect	= sctp6_connect,
   1339 	.pr_connect2	= sctp6_connect2,
   1340 	.pr_disconnect	= sctp6_disconnect,
   1341 	.pr_shutdown	= sctp6_shutdown,
   1342 	.pr_abort	= sctp6_abort,
   1343 	.pr_ioctl	= sctp6_ioctl,
   1344 	.pr_stat	= sctp6_stat,
   1345 	.pr_peeraddr	= sctp6_peeraddr,
   1346 	.pr_sockaddr	= sctp6_sockaddr,
   1347 	.pr_rcvd	= sctp6_rcvd,
   1348 	.pr_recvoob	= sctp6_recvoob,
   1349 	.pr_send	= sctp6_send,
   1350 	.pr_sendoob	= sctp6_sendoob,
   1351 	.pr_purgeif	= sctp6_purgeif,
   1352 };
   1353