Home | History | Annotate | Line # | Download | only in netinet6
sctp6_usrreq.c revision 1.4
      1 /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
      2 /* $NetBSD: sctp6_usrreq.c,v 1.4 2016/04/25 21:21:02 rjs 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.4 2016/04/25 21:21:02 rjs 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->m_pkthdr.rcvif == NULL) ||
    164 	   (m->m_pkthdr.rcvif->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((struct sockaddr *)ip6cp->ip6c_src,
    466 						    (struct sockaddr *)&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,
    484 					    (struct sockaddr *)&final,
    485 					    stcb, net);
    486 				/* inp's ref-count reduced && stcb unlocked */
    487 			}
    488 		} else {
    489 			if (PRC_IS_REDIRECT(cmd) && inp) {
    490 				in6_rtchange((struct in6pcb *)inp,
    491 					     inet6ctlerrmap[cmd]);
    492 			}
    493 			if (inp) {
    494 				/* reduce inp's ref-count */
    495 				SCTP_INP_WLOCK(inp);
    496 				SCTP_INP_DECR_REF(inp);
    497 				SCTP_INP_WUNLOCK(inp);
    498 			}
    499 			if (stcb) {
    500 				SCTP_TCB_UNLOCK(stcb);
    501 			}
    502 		}
    503 		splx(s);
    504 	}
    505 	return NULL;
    506 }
    507 
    508 /*
    509  * this routine can probably be collasped into the one in sctp_userreq.c
    510  * since they do the same thing and now we lookup with a sockaddr
    511  */
    512 #ifdef __FreeBSD__
    513 static int
    514 sctp6_getcred(SYSCTL_HANDLER_ARGS)
    515 {
    516 	struct sockaddr_in6 addrs[2];
    517 	struct sctp_inpcb *inp;
    518 	struct sctp_nets *net;
    519 	struct sctp_tcb *stcb;
    520 	int error, s;
    521 
    522 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
    523 	error = suser(req->td);
    524 #else
    525 	error = suser(req->p);
    526 #endif
    527 	if (error)
    528 		return (error);
    529 
    530 	if (req->newlen != sizeof(addrs))
    531 		return (EINVAL);
    532 	if (req->oldlen != sizeof(struct ucred))
    533 		return (EINVAL);
    534 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
    535 	if (error)
    536 		return (error);
    537 	s = splsoftnet();
    538 
    539         stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
    540                                            sin6tosa(&addrs[1]),
    541                                            &inp, &net, 1);
    542 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
    543 		error = ENOENT;
    544 		if (inp) {
    545 			SCTP_INP_WLOCK(inp);
    546 			SCTP_INP_DECR_REF(inp);
    547 			SCTP_INP_WUNLOCK(inp);
    548 		}
    549 		goto out;
    550 	}
    551 	error = SYSCTL_OUT(req, inp->sctp_socket->so_cred,
    552 			   sizeof(struct ucred));
    553 
    554 	SCTP_TCB_UNLOCK (stcb);
    555  out:
    556 	splx(s);
    557 	return (error);
    558 }
    559 
    560 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
    561 	    0, 0,
    562 	    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
    563 
    564 #endif
    565 
    566 /* This is the same as the sctp_abort() could be made common */
    567 static int
    568 sctp6_abort(struct socket *so)
    569 {
    570 	int s;
    571 	struct sctp_inpcb *inp;
    572 
    573 	KASSERT(solocked(so));
    574 
    575 	s = splsoftnet();
    576 	inp = (struct sctp_inpcb *)so->so_pcb;
    577 	if (inp == 0)
    578 		return EINVAL;	/* ??? possible? panic instead? */
    579 	soisdisconnected(so);
    580 	sctp_inpcb_free(inp, 1);
    581 	splx(s);
    582 	return 0;
    583 }
    584 
    585 static int
    586 sctp6_attach(struct socket *so, int proto)
    587 {
    588 	struct in6pcb *inp6;
    589 	int error;
    590 	struct sctp_inpcb *inp;
    591 
    592 	sosetlock(so);
    593 	inp = (struct sctp_inpcb *)so->so_pcb;
    594 	if (inp != NULL)
    595 		return EINVAL;
    596 
    597 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    598 		error = soreserve(so, sctp_sendspace, sctp_recvspace);
    599 		if (error)
    600 			return error;
    601 	}
    602 	error = sctp_inpcb_alloc(so);
    603 	if (error)
    604 		return error;
    605 	inp = (struct sctp_inpcb *)so->so_pcb;
    606 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
    607 	inp6 = (struct in6pcb *)inp;
    608 
    609 	inp->inp_vflag |=  INP_IPV6;
    610 	if (ip6_v6only) {
    611 		inp6->in6p_flags |= IN6P_IPV6_V6ONLY;
    612 	}
    613 	so->so_send = sctp_sosend;
    614 
    615 	inp6->in6p_hops = -1;	        /* use kernel default */
    616 	inp6->in6p_cksum = -1;	/* just to be sure */
    617 #ifdef INET
    618 	/*
    619 	 * XXX: ugly!!
    620 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
    621 	 * because the socket may be bound to an IPv6 wildcard address,
    622 	 * which may match an IPv4-mapped IPv6 address.
    623 	 */
    624 	inp->inp_ip_ttl = ip_defttl;
    625 #endif
    626 	/*
    627 	 * Hmm what about the IPSEC stuff that is missing here but
    628 	 * in sctp_attach()?
    629 	 */
    630 	return 0;
    631 }
    632 
    633 static int
    634 sctp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
    635 {
    636 	struct sctp_inpcb *inp;
    637 	struct in6pcb *inp6;
    638 	int error;
    639 
    640 	KASSERT(solocked(so));
    641 
    642 	inp = (struct sctp_inpcb *)so->so_pcb;
    643 	if (inp == 0)
    644 		return EINVAL;
    645 
    646 	inp6 = (struct in6pcb *)inp;
    647 	inp->inp_vflag &= ~INP_IPV4;
    648 	inp->inp_vflag |= INP_IPV6;
    649 
    650 	if (nam != NULL && (inp6->in6p_flags & IN6P_IPV6_V6ONLY) == 0) {
    651 		if (nam->sa_family == AF_INET) {
    652 			/* binding v4 addr to v6 socket, so reset flags */
    653 			inp->inp_vflag |= INP_IPV4;
    654 			inp->inp_vflag &= ~INP_IPV6;
    655 		} else {
    656 			struct sockaddr_in6 *sin6_p;
    657 			sin6_p = (struct sockaddr_in6 *)nam;
    658 
    659 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
    660 			  inp->inp_vflag |= INP_IPV4;
    661 			}
    662 			else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
    663 				struct sockaddr_in sin;
    664 				in6_sin6_2_sin(&sin, sin6_p);
    665 				inp->inp_vflag |= INP_IPV4;
    666 				inp->inp_vflag &= ~INP_IPV6;
    667 				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, l);
    668 				return error;
    669 			}
    670 		}
    671 	} else if (nam != NULL) {
    672 		/* IPV6_V6ONLY socket */
    673 		if (nam->sa_family == AF_INET) {
    674 			/* can't bind v4 addr to v6 only socket! */
    675 			return EINVAL;
    676 		} else {
    677 			struct sockaddr_in6 *sin6_p;
    678 			sin6_p = (struct sockaddr_in6 *)nam;
    679 
    680 			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
    681 				/* can't bind v4-mapped addrs either! */
    682 				/* NOTE: we don't support SIIT */
    683 				return EINVAL;
    684 		}
    685 	}
    686 	error = sctp_inpcb_bind(so, nam, l);
    687 	return error;
    688 }
    689 
    690 /*This could be made common with sctp_detach() since they are identical */
    691 static int
    692 sctp6_detach(struct socket *so)
    693 {
    694 	struct sctp_inpcb *inp;
    695 
    696 	inp = (struct sctp_inpcb *)so->so_pcb;
    697 	if (inp == 0)
    698 		return EINVAL;
    699 
    700 	if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
    701 	    (so->so_rcv.sb_cc > 0))
    702 		sctp_inpcb_free(inp, 1);
    703 	else
    704 		sctp_inpcb_free(inp, 0);
    705 	return 0;
    706 }
    707 
    708 static int
    709 sctp6_disconnect(struct socket *so)
    710 {
    711 	struct sctp_inpcb *inp;
    712 
    713 	inp = (struct sctp_inpcb *)so->so_pcb;
    714 	if (inp == NULL) {
    715 		return (ENOTCONN);
    716 	}
    717 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
    718 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
    719 			/* No connection */
    720 			return (ENOTCONN);
    721 		} else {
    722 			int some_on_streamwheel = 0;
    723 			struct sctp_association *asoc;
    724 			struct sctp_tcb *stcb;
    725 
    726 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
    727 			if (stcb == NULL) {
    728 				return (EINVAL);
    729 			}
    730 			asoc = &stcb->asoc;
    731 			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
    732 				/* Check to see if some data queued */
    733 				struct sctp_stream_out *outs;
    734 				TAILQ_FOREACH(outs, &asoc->out_wheel,
    735 					      next_spoke) {
    736 					if (!TAILQ_EMPTY(&outs->outqueue)) {
    737 						some_on_streamwheel = 1;
    738 						break;
    739 					}
    740 				}
    741 			}
    742 
    743 			if (TAILQ_EMPTY(&asoc->send_queue) &&
    744 			    TAILQ_EMPTY(&asoc->sent_queue) &&
    745 			    (some_on_streamwheel == 0)) {
    746 				/* nothing queued to send, so I'm done... */
    747 				if ((SCTP_GET_STATE(asoc) !=
    748 				     SCTP_STATE_SHUTDOWN_SENT) &&
    749 				    (SCTP_GET_STATE(asoc) !=
    750 				     SCTP_STATE_SHUTDOWN_ACK_SENT)) {
    751 					/* only send SHUTDOWN the first time */
    752 #ifdef SCTP_DEBUG
    753 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
    754 						printf("%s:%d sends a shutdown\n",
    755 						       __FILE__,
    756 						       __LINE__
    757 							);
    758 					}
    759 #endif
    760 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
    761 					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
    762 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
    763 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
    764 							 stcb->sctp_ep, stcb,
    765 							 asoc->primary_destination);
    766 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
    767 							 stcb->sctp_ep, stcb,
    768 							 asoc->primary_destination);
    769 				}
    770 			} else {
    771 				/*
    772 				 * we still got (or just got) data to send,
    773 				 * so set SHUTDOWN_PENDING
    774 				 */
    775 				/*
    776 				 * XXX sockets draft says that MSG_EOF should
    777 				 * be sent with no data.  currently, we will
    778 				 * allow user data to be sent first and move
    779 				 * to SHUTDOWN-PENDING
    780 				 */
    781 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
    782 			}
    783 			return (0);
    784 		}
    785 	} else {
    786 		/* UDP model does not support this */
    787 		return EOPNOTSUPP;
    788 	}
    789 }
    790 
    791 static int
    792 sctp6_recvoob(struct socket *so, struct mbuf *m, int flags)
    793 {
    794 	KASSERT(solocked(so));
    795 
    796 	return EOPNOTSUPP;
    797 }
    798 
    799 static int
    800 sctp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
    801 	   struct mbuf *control, struct lwp *l)
    802 {
    803 	struct sctp_inpcb *inp;
    804 	struct in6pcb *inp6;
    805 #ifdef INET
    806 	struct sockaddr_in6 *sin6;
    807 #endif /* INET */
    808 	/* No SPL needed since sctp_output does this */
    809 
    810 	inp = (struct sctp_inpcb *)so->so_pcb;
    811 	if (inp == NULL) {
    812 	        if (control) {
    813 			m_freem(control);
    814 			control = NULL;
    815 		}
    816 		m_freem(m);
    817 		return EINVAL;
    818 	}
    819 	inp6 = (struct in6pcb *)inp;
    820 	/* For the TCP model we may get a NULL addr, if we
    821 	 * are a connected socket thats ok.
    822 	 */
    823 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
    824 	    (nam == NULL)) {
    825 	        goto connected_type;
    826 	}
    827 	if (nam == NULL) {
    828 		m_freem(m);
    829 		if (control) {
    830 			m_freem(control);
    831 			control = NULL;
    832 		}
    833 		return (EDESTADDRREQ);
    834 	}
    835 
    836 #ifdef INET
    837 	sin6 = (struct sockaddr_in6 *)nam;
    838 	if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
    839 		/*
    840 		 * if IPV6_V6ONLY flag, we discard datagrams
    841 		 * destined to a v4 addr or v4-mapped addr
    842 		 */
    843 		if (nam->sa_family == AF_INET) {
    844 			return EINVAL;
    845 		}
    846 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    847 			return EINVAL;
    848 		}
    849 	}
    850 
    851 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    852 		if (!ip6_v6only) {
    853 			struct sockaddr_in sin;
    854 			/* convert v4-mapped into v4 addr and send */
    855 			in6_sin6_2_sin(&sin, sin6);
    856 			return sctp_send(so, m, (struct sockaddr *)&sin,
    857 			    control, l);
    858 		} else {
    859 			/* mapped addresses aren't enabled */
    860 			return EINVAL;
    861 		}
    862 	}
    863 #endif /* INET */
    864  connected_type:
    865 	/* now what about control */
    866 	if (control) {
    867 		if (inp->control) {
    868 			printf("huh? control set?\n");
    869 			m_freem(inp->control);
    870 			inp->control = NULL;
    871 		}
    872 		inp->control = control;
    873 	}
    874 	/* add it in possibly */
    875 	if ((inp->pkt) &&
    876 	    (inp->pkt->m_flags & M_PKTHDR)) {
    877 		struct mbuf *x;
    878 		int c_len;
    879 
    880 		c_len = 0;
    881 		/* How big is it */
    882 		for (x=m;x;x = x->m_next) {
    883 			c_len += x->m_len;
    884 		}
    885 		inp->pkt->m_pkthdr.len += c_len;
    886 	}
    887 	/* Place the data */
    888 	if (inp->pkt) {
    889 		inp->pkt_last->m_next = m;
    890 		inp->pkt_last = m;
    891 	} else {
    892 		inp->pkt_last = inp->pkt = m;
    893 	}
    894 	if ((so->so_state & SS_MORETOCOME) == 0) {
    895 		/*
    896 		 * note with the current version this code will only be
    897 		 * used by OpenBSD, NetBSD and FreeBSD have methods for
    898 		 * re-defining sosend() to use sctp_sosend().  One can
    899 		 * optionaly switch back to this code (by changing back
    900 		 * the defininitions but this is not advisable.
    901 		 */
    902 		int ret;
    903 		ret = sctp_output(inp, inp->pkt , nam, inp->control, l, 0);
    904 		inp->pkt = NULL;
    905 		inp->control = NULL;
    906 		return (ret);
    907 	} else {
    908 		return (0);
    909 	}
    910 }
    911 
    912 static int
    913 sctp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
    914 {
    915 	KASSERT(solocked(so));
    916 
    917 	if (m)
    918 		m_freem(m);
    919 	if (control)
    920 		m_freem(control);
    921 
    922 	return EOPNOTSUPP;
    923 }
    924 
    925 static int
    926 sctp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
    927 {
    928 	int error = 0;
    929 	struct sctp_inpcb *inp;
    930 	struct in6pcb *inp6;
    931 	struct sctp_tcb *stcb;
    932 #ifdef INET
    933 	struct sockaddr_in6 *sin6;
    934 	struct sockaddr_storage ss;
    935 #endif /* INET */
    936 
    937 	inp6 = (struct in6pcb *)so->so_pcb;
    938 	inp = (struct sctp_inpcb *)so->so_pcb;
    939 	if (inp == 0) {
    940 		return (ECONNRESET);	/* I made the same as TCP since
    941 					 * we are not setup? */
    942 	}
    943 	SCTP_ASOC_CREATE_LOCK(inp);
    944 	SCTP_INP_RLOCK(inp);
    945 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
    946 	    SCTP_PCB_FLAGS_UNBOUND) {
    947 		/* Bind a ephemeral port */
    948 		SCTP_INP_RUNLOCK(inp);
    949 		error = sctp6_bind(so, NULL, l);
    950 		if (error) {
    951 			SCTP_ASOC_CREATE_UNLOCK(inp);
    952 
    953 			return (error);
    954 		}
    955 		SCTP_INP_RLOCK(inp);
    956 	}
    957 
    958 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
    959 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
    960 		/* We are already connected AND the TCP model */
    961 		SCTP_INP_RUNLOCK(inp);
    962 		SCTP_ASOC_CREATE_UNLOCK(inp);
    963 		return (EADDRINUSE);
    964 	}
    965 
    966 #ifdef INET
    967 	sin6 = (struct sockaddr_in6 *)nam;
    968 	if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
    969 		/*
    970 		 * if IPV6_V6ONLY flag, ignore connections
    971 		 * destined to a v4 addr or v4-mapped addr
    972 		 */
    973 		if (nam->sa_family == AF_INET) {
    974 			SCTP_INP_RUNLOCK(inp);
    975 			SCTP_ASOC_CREATE_UNLOCK(inp);
    976 			return EINVAL;
    977 		}
    978 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    979 			SCTP_INP_RUNLOCK(inp);
    980 			SCTP_ASOC_CREATE_UNLOCK(inp);
    981 			return EINVAL;
    982 		}
    983 	}
    984 
    985 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
    986 		if (!ip6_v6only) {
    987 			/* convert v4-mapped into v4 addr */
    988 			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
    989 			nam = (struct sockaddr *)&ss;
    990 		} else {
    991 			/* mapped addresses aren't enabled */
    992 			SCTP_INP_RUNLOCK(inp);
    993 			SCTP_ASOC_CREATE_UNLOCK(inp);
    994 			return EINVAL;
    995 		}
    996 	}
    997 #endif /* INET */
    998 
    999 	/* Now do we connect? */
   1000 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1001 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1002 		if (stcb) {
   1003 			SCTP_TCB_UNLOCK (stcb);
   1004 		}
   1005 		SCTP_INP_RUNLOCK(inp);
   1006 	} else {
   1007 		SCTP_INP_RUNLOCK(inp);
   1008 		SCTP_INP_WLOCK(inp);
   1009 		SCTP_INP_INCR_REF(inp);
   1010 		SCTP_INP_WUNLOCK(inp);
   1011 		stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL);
   1012 		if (stcb == NULL) {
   1013 			SCTP_INP_WLOCK(inp);
   1014 			SCTP_INP_DECR_REF(inp);
   1015 			SCTP_INP_WUNLOCK(inp);
   1016 		}
   1017 	}
   1018 
   1019 	if (stcb != NULL) {
   1020 		/* Already have or am bring up an association */
   1021 		SCTP_ASOC_CREATE_UNLOCK(inp);
   1022 		SCTP_TCB_UNLOCK (stcb);
   1023 		return (EALREADY);
   1024 	}
   1025 	/* We are GOOD to go */
   1026 	stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0);
   1027 	SCTP_ASOC_CREATE_UNLOCK(inp);
   1028 	if (stcb == NULL) {
   1029 		/* Gak! no memory */
   1030 		return (error);
   1031 	}
   1032 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
   1033 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
   1034 		/* Set the connected flag so we can queue data */
   1035 		soisconnecting(so);
   1036 	}
   1037 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
   1038 	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
   1039 	sctp_send_initiate(inp, stcb);
   1040 	SCTP_TCB_UNLOCK (stcb);
   1041 	return error;
   1042 }
   1043 
   1044 static int
   1045 sctp6_connect2(struct socket *so, struct socket *so2)
   1046 {
   1047 	KASSERT(solocked(so));
   1048 
   1049 	return EOPNOTSUPP;
   1050 }
   1051 
   1052 static int
   1053 sctp6_getaddr(struct socket *so, struct sockaddr *nam)
   1054 {
   1055 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
   1056 	struct sctp_inpcb *inp;
   1057 	int error;
   1058 
   1059 	/*
   1060 	 * Do the malloc first in case it blocks.
   1061 	 */
   1062 	memset(sin6, 0, sizeof(*sin6));
   1063 	sin6->sin6_family = AF_INET6;
   1064 	sin6->sin6_len = sizeof(*sin6);
   1065 
   1066 	inp = (struct sctp_inpcb *)so->so_pcb;
   1067 	if (inp == NULL) {
   1068 		return ECONNRESET;
   1069 	}
   1070 
   1071 	sin6->sin6_port = inp->sctp_lport;
   1072 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
   1073 		/* For the bound all case you get back 0 */
   1074 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
   1075 			struct sctp_tcb *stcb;
   1076 			const struct sockaddr_in6 *sin_a6;
   1077 			struct sctp_nets *net;
   1078 			int fnd;
   1079 
   1080 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1081 			if (stcb == NULL) {
   1082 				goto notConn6;
   1083 			}
   1084 			fnd = 0;
   1085 			sin_a6 = NULL;
   1086 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1087 				sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
   1088 				if (sin_a6->sin6_family == AF_INET6) {
   1089 					fnd = 1;
   1090 					break;
   1091 				}
   1092 			}
   1093 			if ((!fnd) || (sin_a6 == NULL)) {
   1094 				/* punt */
   1095 				goto notConn6;
   1096 			}
   1097 			sin6->sin6_addr = sctp_ipv6_source_address_selection(
   1098 			    inp, stcb, &net->ro, net, 0);
   1099 
   1100 		} else {
   1101 			/* For the bound all case you get back 0 */
   1102 		notConn6:
   1103 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
   1104 		}
   1105 	} else {
   1106 		/* Take the first IPv6 address in the list */
   1107 		struct sctp_laddr *laddr;
   1108 		int fnd = 0;
   1109 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
   1110 			if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
   1111 				struct sockaddr_in6 *sin_a;
   1112 				sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
   1113 				sin6->sin6_addr = sin_a->sin6_addr;
   1114 				fnd = 1;
   1115 				break;
   1116 			}
   1117 		}
   1118 		if (!fnd) {
   1119 			return ENOENT;
   1120 		}
   1121 	}
   1122 	/* Scoping things for v6 */
   1123 	if ((error = sa6_recoverscope(sin6)) != 0)
   1124 		return (error);
   1125 
   1126 	return (0);
   1127 }
   1128 
   1129 static int
   1130 sctp6_peeraddr(struct socket *so, struct sockaddr *nam)
   1131 {
   1132 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
   1133 	int fnd, error;
   1134 	const struct sockaddr_in6 *sin_a6;
   1135 	struct sctp_inpcb *inp;
   1136 	struct sctp_tcb *stcb;
   1137 	struct sctp_nets *net;
   1138 	/*
   1139 	 * Do the malloc first in case it blocks.
   1140 	 */
   1141 	inp = (struct sctp_inpcb *)so->so_pcb;
   1142 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
   1143 		/* UDP type and listeners will drop out here */
   1144 		return (ENOTCONN);
   1145 	}
   1146 	memset(sin6, 0, sizeof(*sin6));
   1147 	sin6->sin6_family = AF_INET6;
   1148 	sin6->sin6_len = sizeof(*sin6);
   1149 
   1150 	/* We must recapture incase we blocked */
   1151 	inp = (struct sctp_inpcb *)so->so_pcb;
   1152 	if (inp == NULL) {
   1153 		return ECONNRESET;
   1154 	}
   1155 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
   1156 	if (stcb == NULL) {
   1157 		return ECONNRESET;
   1158 	}
   1159 	fnd = 0;
   1160 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
   1161 		sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
   1162 		if (sin_a6->sin6_family == AF_INET6) {
   1163 			fnd = 1;
   1164 			sin6->sin6_port = stcb->rport;
   1165 			sin6->sin6_addr = sin_a6->sin6_addr;
   1166 			break;
   1167 		}
   1168 	}
   1169 	if (!fnd) {
   1170 		/* No IPv4 address */
   1171 		return ENOENT;
   1172 	}
   1173 	if ((error = sa6_recoverscope(sin6)) != 0)
   1174 		return (error);
   1175 
   1176 	return (0);
   1177 }
   1178 
   1179 static int
   1180 sctp6_sockaddr(struct socket *so, struct sockaddr *nam)
   1181 {
   1182 	struct in6pcb *inp6 = sotoin6pcb(so);
   1183 	int	error;
   1184 
   1185 	if (inp6 == NULL)
   1186 		return EINVAL;
   1187 
   1188 	/* allow v6 addresses precedence */
   1189 	error = sctp6_getaddr(so, nam);
   1190 	if (error) {
   1191 		/* try v4 next if v6 failed */
   1192 		error = sctp_sockaddr(so, nam);
   1193 		if (error) {
   1194 			return (error);
   1195 		}
   1196 
   1197 		/* if I'm V6ONLY, convert it to v4-mapped */
   1198 		if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
   1199 			struct sockaddr_in6 sin6;
   1200 			in6_sin_2_v4mapsin6((struct sockaddr_in *)nam, &sin6);
   1201 			memcpy(nam, &sin6, sizeof(struct sockaddr_in6));
   1202 		}
   1203 	}
   1204 	return (error);
   1205 }
   1206 
   1207 #if 0
   1208 static int
   1209 sctp6_getpeeraddr(struct socket *so, struct sockaddr *nam)
   1210 {
   1211 	struct in6pcb *inp6 = sotoin6pcb(so);
   1212 	int	error;
   1213 
   1214 	if (inp6 == NULL)
   1215 		return EINVAL;
   1216 
   1217 	/* allow v6 addresses precedence */
   1218 	error = sctp6_peeraddr(so, nam);
   1219 	if (error) {
   1220 		/* try v4 next if v6 failed */
   1221 		error = sctp_peeraddr(so, nam);
   1222 		if (error) {
   1223 			return (error);
   1224 		}
   1225 		/* if I'm V6ONLY, convert it to v4-mapped */
   1226 		if ((inp6->in6p_flags & IN6P_IPV6_V6ONLY)) {
   1227 			struct sockaddr_in6 sin6;
   1228 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
   1229 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
   1230 		}
   1231 	}
   1232 	return error;
   1233 }
   1234 #endif
   1235 
   1236 static int
   1237 sctp6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
   1238 {
   1239 	int error = 0;
   1240 	int family;
   1241 
   1242 	family = so->so_proto->pr_domain->dom_family;
   1243 	switch (family) {
   1244 #ifdef INET
   1245 	case PF_INET:
   1246 		error = in_control(so, cmd, nam, ifp);
   1247 		break;
   1248 #endif
   1249 #ifdef INET6
   1250 	case PF_INET6:
   1251 		error = in6_control(so, cmd, nam, ifp);
   1252 		break;
   1253 #endif
   1254 	default:
   1255 		error = EAFNOSUPPORT;
   1256 	}
   1257 	return (error);
   1258 }
   1259 
   1260 static int
   1261 sctp6_accept(struct socket *so, struct sockaddr *nam)
   1262 {
   1263 	KASSERT(solocked(so));
   1264 
   1265 	return EOPNOTSUPP;
   1266 }
   1267 
   1268 static int
   1269 sctp6_stat(struct socket *so, struct stat *ub)
   1270 {
   1271 	return 0;
   1272 }
   1273 
   1274 static int
   1275 sctp6_listen(struct socket *so, struct lwp *l)
   1276 {
   1277 	return sctp_listen(so, l);
   1278 }
   1279 
   1280 static int
   1281 sctp6_shutdown(struct socket *so)
   1282 {
   1283 	return sctp_shutdown(so);
   1284 }
   1285 
   1286 static int
   1287 sctp6_rcvd(struct socket *so, int flags, struct lwp *l)
   1288 {
   1289 	KASSERT(solocked(so));
   1290 
   1291 	return sctp_rcvd(so, flags, l);
   1292 }
   1293 
   1294 static int
   1295 sctp6_purgeif(struct socket *so, struct ifnet *ifp)
   1296 {
   1297 	struct ifaddr *ifa;
   1298 	IFADDR_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