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