xref: /netbsd-src/sys/netinet6/sctp6_usrreq.c (revision 796c32c94f6e154afc9de0f63da35c91bb739b45)
1 /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
2 /* $NetBSD: sctp6_usrreq.c,v 1.14 2017/10/17 19:23:42 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.14 2017/10/17 19:23:42 rjs Exp $");
37 
38 #ifdef _KERNEL_OPT
39 #include "opt_inet.h"
40 #include "opt_ipsec.h"
41 #include "opt_sctp.h"
42 #include "opt_net_mpsafe.h"
43 #endif /* _KERNEL_OPT */
44 
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/mbuf.h>
48 #include <sys/domain.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/malloc.h>
52 #include <sys/socketvar.h>
53 #include <sys/sysctl.h>
54 #include <sys/errno.h>
55 #include <sys/stat.h>
56 #include <sys/systm.h>
57 #include <sys/syslog.h>
58 #include <sys/proc.h>
59 #include <net/if.h>
60 #include <net/route.h>
61 #include <net/if_types.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/sctp_pcb.h>
69 #include <netinet/sctp_header.h>
70 #include <netinet/sctp_var.h>
71 #include <netinet/sctputil.h>
72 #include <netinet/sctp_output.h>
73 #include <netinet/sctp_input.h>
74 #include <netinet/sctp_asconf.h>
75 #include <netinet6/ip6_var.h>
76 #include <netinet6/scope6_var.h>
77 #include <netinet/ip6.h>
78 #include <netinet6/in6_pcb.h>
79 #include <netinet/icmp6.h>
80 #include <netinet6/sctp6_var.h>
81 #include <netinet6/ip6protosw.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 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__)
95 #ifndef in6pcb
96 #define in6pcb		inpcb
97 #endif
98 #ifndef sotoin6pcb
99 #define sotoin6pcb      sotoinpcb
100 #endif
101 #endif
102 
103 #ifdef SCTP_DEBUG
104 extern u_int32_t sctp_debug_on;
105 #endif
106 
107 static	int sctp6_detach(struct socket *so);
108 
109 extern int sctp_no_csum_on_loopback;
110 
111 int
112 sctp6_input(struct mbuf **mp, int *offp, int proto)
113 {
114 	struct mbuf *m = *mp;
115 	struct ip6_hdr *ip6;
116 	struct sctphdr *sh;
117 	struct sctp_inpcb *in6p = NULL;
118 	struct sctp_nets *net;
119 	int refcount_up = 0;
120 	u_int32_t check, calc_check;
121 	struct inpcb *in6p_ip;
122 	struct sctp_chunkhdr *ch;
123 	struct mbuf *opts = NULL;
124 	int length, mlen, offset, iphlen;
125 	u_int8_t ecn_bits;
126 	struct sctp_tcb *stcb = NULL;
127 	int off = *offp;
128 	int s;
129 
130 	ip6 = mtod(m, struct ip6_hdr *);
131 	/* Ensure that (sctphdr + sctp_chunkhdr) in a row. */
132 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, off, sizeof(*sh) + sizeof(*ch));
133 	if (sh == NULL) {
134 		sctp_pegs[SCTP_HDR_DROPS]++;
135 		return IPPROTO_DONE;
136 	}
137 	ch = (struct sctp_chunkhdr *)((vaddr_t)sh + sizeof(struct sctphdr));
138 
139 	iphlen = off;
140 	offset = iphlen + sizeof(*sh) + sizeof(*ch);
141 
142 #if defined(NFAITH) && NFAITH > 0
143 	if (faithprefix(&ip6->ip6_dst))
144 		goto bad;
145 #endif /* NFAITH defined and > 0 */
146 	sctp_pegs[SCTP_INPKTS]++;
147 #ifdef SCTP_DEBUG
148 	if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
149 		printf("V6 input gets a packet iphlen:%d pktlen:%d\n", iphlen, m->m_pkthdr.len);
150 	}
151 #endif
152  	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
153 		/* No multi-cast support in SCTP */
154 		sctp_pegs[SCTP_IN_MCAST]++;
155 		goto bad;
156 	}
157 	/* destination port of 0 is illegal, based on RFC2960. */
158 	if (sh->dest_port == 0)
159 		goto bad;
160 	if ((sctp_no_csum_on_loopback == 0) ||
161 	   (m_get_rcvif_NOMPSAFE(m) == NULL) ||
162 	   (m_get_rcvif_NOMPSAFE(m)->if_type != IFT_LOOP)) {
163 		/* we do NOT validate things from the loopback if the
164 		 * sysctl is set to 1.
165 		 */
166 		check = sh->checksum;		/* save incoming checksum */
167 		if ((check == 0) && (sctp_no_csum_on_loopback)) {
168 			/* special hook for where we got a local address
169 			 * somehow routed across a non IFT_LOOP type interface
170 			 */
171 			if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &ip6->ip6_dst))
172 				goto sctp_skip_csum;
173 		}
174 		sh->checksum = 0;		/* prepare for calc */
175 		calc_check = sctp_calculate_sum(m, &mlen, iphlen);
176 		if (calc_check != check) {
177 #ifdef SCTP_DEBUG
178 			if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
179 				printf("Bad CSUM on SCTP packet calc_check:%x check:%x  m:%p mlen:%d iphlen:%d\n",
180 				       calc_check, check, m,
181 				       mlen, iphlen);
182 			}
183 #endif
184 			stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
185 							 sh, ch, &in6p, &net);
186 			/* in6p's ref-count increased && stcb locked */
187 			if ((in6p) && (stcb)) {
188 				sctp_send_packet_dropped(stcb, net, m, iphlen, 1);
189 				sctp_chunk_output((struct sctp_inpcb *)in6p, stcb, 2);
190 			}  else if ((in6p != NULL) && (stcb == NULL)) {
191 				refcount_up = 1;
192 			}
193 			sctp_pegs[SCTP_BAD_CSUM]++;
194 			goto bad;
195 		}
196 		sh->checksum = calc_check;
197 	} else {
198 sctp_skip_csum:
199 		mlen = m->m_pkthdr.len;
200 	}
201 	net = NULL;
202 	/*
203 	 * Locate pcb and tcb for datagram
204 	 * sctp_findassociation_addr() wants IP/SCTP/first chunk header...
205 	 */
206 #ifdef SCTP_DEBUG
207 	if (sctp_debug_on & SCTP_DEBUG_INPUT1) {
208 		printf("V6 Find the association\n");
209 	}
210 #endif
211 	stcb = sctp_findassociation_addr(m, iphlen, offset - sizeof(*ch),
212 	    sh, ch, &in6p, &net);
213 	/* in6p's ref-count increased */
214 	if (in6p == NULL) {
215 		struct sctp_init_chunk *init_chk, chunk_buf;
216 
217 		sctp_pegs[SCTP_NOPORTS]++;
218 		if (ch->chunk_type == SCTP_INITIATION) {
219 			/* we do a trick here to get the INIT tag,
220 			 * dig in and get the tag from the INIT and
221 			 * put it in the common header.
222 			 */
223 			init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
224 			    iphlen + sizeof(*sh), sizeof(*init_chk),
225 			    (u_int8_t *)&chunk_buf);
226 			sh->v_tag = init_chk->init.initiate_tag;
227 		}
228 		sctp_send_abort(m, iphlen, sh, 0, NULL);
229 		goto bad;
230 	} else if (stcb == NULL) {
231 		refcount_up = 1;
232 	}
233 	in6p_ip = (struct inpcb *)in6p;
234 #ifdef IPSEC
235 	/*
236 	 * Check AH/ESP integrity.
237 	 */
238 	if (ipsec_used && ipsec6_in_reject(m, (struct in6pcb *)in6p_ip)) {
239 /* XXX */
240 #if 0
241 		/* FIX ME: need to find right stat */
242 		ipsec6stat.in_polvio++;
243 #endif
244 		goto bad;
245 	}
246 #endif /*IPSEC*/
247 
248 	/*
249 	 * Construct sockaddr format source address.
250 	 * Stuff source address and datagram in user buffer.
251 	 */
252 	if ((in6p->ip_inp.inp.inp_flags & INP_CONTROLOPTS)
253 #ifndef __OpenBSD__
254 	    || (in6p->sctp_socket->so_options & SO_TIMESTAMP)
255 #endif
256 	    ) {
257 #if defined(__FreeBSD__) || defined(__APPLE__)
258 #if (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version < 501113) || defined(__APPLE__)
259 		ip6_savecontrol(in6p_ip, &opts, ip6, m);
260 #elif __FreeBSD_version >= 440000 || (defined(SCTP_BASE_FREEBSD) && __FreeBSD_version >= 501113)
261 		ip6_savecontrol(in6p_ip, m, &opts);
262 #else
263 		ip6_savecontrol(in6p_ip, m, &opts, NULL);
264 #endif
265 #elif defined(__NetBSD__)
266 		ip6_savecontrol((struct in6pcb *)in6p_ip, &opts, ip6, m);
267 #else
268 		ip6_savecontrol((struct in6pcb *)in6p_ip, m, &opts);
269 #endif
270 	}
271 
272 	/*
273 	 * CONTROL chunk processing
274 	 */
275 	length = ntohs(ip6->ip6_plen) + iphlen;
276 	offset -= sizeof(*ch);
277 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
278 	s = splsoftnet();
279 	(void)sctp_common_input_processing(&m, iphlen, offset, length, sh, ch,
280 	    in6p, stcb, net, ecn_bits);
281 	/* inp's ref-count reduced && stcb unlocked */
282 	splx(s);
283 	/* XXX this stuff below gets moved to appropriate parts later... */
284 	if (m)
285 		m_freem(m);
286 	if (opts)
287 		m_freem(opts);
288 
289 	if ((in6p) && refcount_up){
290 		/* reduce ref-count */
291 		SCTP_INP_WLOCK(in6p);
292 		SCTP_INP_DECR_REF(in6p);
293 		SCTP_INP_WUNLOCK(in6p);
294 	}
295 
296 	return IPPROTO_DONE;
297 
298 bad:
299 	if (stcb) {
300 		SCTP_TCB_UNLOCK(stcb);
301 	}
302 
303 	if ((in6p) && refcount_up){
304 		/* reduce ref-count */
305 		SCTP_INP_WLOCK(in6p);
306 		SCTP_INP_DECR_REF(in6p);
307 		SCTP_INP_WUNLOCK(in6p);
308 	}
309 	if (m) {
310 		m_freem(m);
311 	}
312 	if (opts) {
313 		m_freem(opts);
314 	}
315 	return IPPROTO_DONE;
316 }
317 
318 
319 static void
320 sctp6_notify_mbuf(struct sctp_inpcb *inp,
321 		  struct icmp6_hdr *icmp6,
322 		  struct sctphdr *sh,
323 		  struct sctp_tcb *stcb,
324 		  struct sctp_nets *net)
325 {
326 	unsigned int nxtsz;
327 
328 	if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
329 	    (icmp6 == NULL) || (sh == NULL)) {
330 		goto out;
331 	}
332 
333 	/* First do we even look at it? */
334 	if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag))
335 		goto out;
336 
337 	if (icmp6->icmp6_type != ICMP6_PACKET_TOO_BIG) {
338 		/* not PACKET TO BIG */
339 		goto out;
340 	}
341 	/*
342 	 * ok we need to look closely. We could even get smarter and
343 	 * look at anyone that we sent to in case we get a different
344 	 * ICMP that tells us there is no way to reach a host, but for
345 	 * this impl, all we care about is MTU discovery.
346 	 */
347 	nxtsz = ntohl(icmp6->icmp6_mtu);
348 	/* Stop any PMTU timer */
349 	sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
350 
351 	/* Adjust destination size limit */
352 	if (net->mtu > nxtsz) {
353 		net->mtu = nxtsz;
354 	}
355 	/* now what about the ep? */
356 	if (stcb->asoc.smallest_mtu > nxtsz) {
357 		struct sctp_tmit_chunk *chk;
358 		struct sctp_stream_out *strm;
359 		/* Adjust that too */
360 		stcb->asoc.smallest_mtu = nxtsz;
361 		/* now off to subtract IP_DF flag if needed */
362 
363 		TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
364 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
365 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
366 			}
367 		}
368 		TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
369 			if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
370 				/*
371 				 * For this guy we also mark for immediate
372 				 * resend since we sent to big of chunk
373 				 */
374 				chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
375 				if (chk->sent != SCTP_DATAGRAM_RESEND)
376 					stcb->asoc.sent_queue_retran_cnt++;
377 				chk->sent = SCTP_DATAGRAM_RESEND;
378 				chk->rec.data.doing_fast_retransmit = 0;
379 
380 				chk->sent = SCTP_DATAGRAM_RESEND;
381 				/* Clear any time so NO RTT is being done */
382 				chk->sent_rcv_time.tv_sec = 0;
383 				chk->sent_rcv_time.tv_usec = 0;
384 				stcb->asoc.total_flight -= chk->send_size;
385 				net->flight_size -= chk->send_size;
386 			}
387 		}
388 		TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
389 			TAILQ_FOREACH(chk, &strm->outqueue, sctp_next) {
390 				if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
391 					chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
392 				}
393 			}
394 		}
395 	}
396 	sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
397 out:
398 	if (inp) {
399 		/* reduce inp's ref-count */
400 		SCTP_INP_WLOCK(inp);
401 		SCTP_INP_DECR_REF(inp);
402 		SCTP_INP_WUNLOCK(inp);
403 	}
404 	if (stcb) {
405 		SCTP_TCB_UNLOCK(stcb);
406 	}
407 }
408 
409 
410 void *
411 sctp6_ctlinput(int cmd, const struct sockaddr *pktdst, void *d)
412 {
413 	struct sctphdr sh;
414 	struct ip6ctlparam *ip6cp = NULL;
415 	int s, cm;
416 
417 	if (pktdst->sa_family != AF_INET6 ||
418 	    pktdst->sa_len != sizeof(struct sockaddr_in6))
419 		return NULL;
420 
421 	if ((unsigned)cmd >= PRC_NCMDS)
422 		return NULL;
423 	if (PRC_IS_REDIRECT(cmd)) {
424 		d = NULL;
425 	} else if (inet6ctlerrmap[cmd] == 0) {
426 		return NULL;
427 	}
428 
429 	/* if the parameter is from icmp6, decode it. */
430 	if (d != NULL) {
431 		ip6cp = (struct ip6ctlparam *)d;
432 	} else {
433 		ip6cp = (struct ip6ctlparam *)NULL;
434 	}
435 
436 	if (ip6cp) {
437 		/*
438 		 * XXX: We assume that when IPV6 is non NULL,
439 		 * M and OFF are valid.
440 		 */
441 		/* check if we can safely examine src and dst ports */
442 		struct sctp_inpcb *inp;
443 		struct sctp_tcb *stcb;
444 		struct sctp_nets *net;
445 		struct sockaddr_in6 final;
446 
447 		if (ip6cp->ip6c_m == NULL ||
448 		    (size_t)ip6cp->ip6c_m->m_pkthdr.len < (ip6cp->ip6c_off + sizeof(sh)))
449 			return NULL;
450 
451 		memset(&sh, 0, sizeof(sh));
452 		memset(&final, 0, sizeof(final));
453 		inp = NULL;
454 		net = NULL;
455 		m_copydata(ip6cp->ip6c_m, ip6cp->ip6c_off, sizeof(sh),
456 		    (void *)&sh);
457 		ip6cp->ip6c_src->sin6_port = sh.src_port;
458 		final.sin6_len = sizeof(final);
459 		final.sin6_family = AF_INET6;
460 		final.sin6_addr = ((const struct sockaddr_in6 *)pktdst)->sin6_addr;
461 		final.sin6_port = sh.dest_port;
462 		s = splsoftnet();
463 		stcb = sctp_findassociation_addr_sa(sin6tosa(ip6cp->ip6c_src),
464 						    sin6tosa(&final),
465 						    &inp, &net, 1);
466 		/* inp's ref-count increased && stcb locked */
467 		if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
468 			if (cmd == PRC_MSGSIZE) {
469 				sctp6_notify_mbuf(inp,
470 						  ip6cp->ip6c_icmp6,
471 						  &sh,
472 						  stcb,
473 						  net);
474 				/* inp's ref-count reduced && stcb unlocked */
475 			} else {
476 				if (cmd == PRC_HOSTDEAD) {
477 					cm = EHOSTUNREACH;
478 				} else {
479 					cm = inet6ctlerrmap[cmd];
480 				}
481 				sctp_notify(inp, cm, &sh, sin6tosa(&final),
482 					    stcb, net);
483 				/* inp's ref-count reduced && stcb unlocked */
484 			}
485 		} else {
486 			if (PRC_IS_REDIRECT(cmd) && inp) {
487 				in6_rtchange((struct in6pcb *)inp,
488 					     inet6ctlerrmap[cmd]);
489 			}
490 			if (inp) {
491 				/* reduce inp's ref-count */
492 				SCTP_INP_WLOCK(inp);
493 				SCTP_INP_DECR_REF(inp);
494 				SCTP_INP_WUNLOCK(inp);
495 			}
496 			if (stcb) {
497 				SCTP_TCB_UNLOCK(stcb);
498 			}
499 		}
500 		splx(s);
501 	}
502 	return NULL;
503 }
504 
505 /*
506  * this routine can probably be collasped into the one in sctp_userreq.c
507  * since they do the same thing and now we lookup with a sockaddr
508  */
509 #ifdef __FreeBSD__
510 static int
511 sctp6_getcred(SYSCTL_HANDLER_ARGS)
512 {
513 	struct sockaddr_in6 addrs[2];
514 	struct sctp_inpcb *inp;
515 	struct sctp_nets *net;
516 	struct sctp_tcb *stcb;
517 	int error, s;
518 
519 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
520 	error = suser(req->td);
521 #else
522 	error = suser(req->p);
523 #endif
524 	if (error)
525 		return (error);
526 
527 	if (req->newlen != sizeof(addrs))
528 		return (EINVAL);
529 	if (req->oldlen != sizeof(struct ucred))
530 		return (EINVAL);
531 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
532 	if (error)
533 		return (error);
534 	s = splsoftnet();
535 
536         stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[0]),
537                                            sin6tosa(&addrs[1]),
538                                            &inp, &net, 1);
539 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
540 		error = ENOENT;
541 		if (inp) {
542 			SCTP_INP_WLOCK(inp);
543 			SCTP_INP_DECR_REF(inp);
544 			SCTP_INP_WUNLOCK(inp);
545 		}
546 		goto out;
547 	}
548 	error = SYSCTL_OUT(req, inp->sctp_socket->so_cred,
549 			   sizeof(struct ucred));
550 
551 	SCTP_TCB_UNLOCK (stcb);
552  out:
553 	splx(s);
554 	return (error);
555 }
556 
557 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
558 	    0, 0,
559 	    sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
560 
561 #endif
562 
563 /* This is the same as the sctp_abort() could be made common */
564 static int
565 sctp6_abort(struct socket *so)
566 {
567 	int s;
568 	struct sctp_inpcb *inp;
569 
570 	KASSERT(solocked(so));
571 
572 	s = splsoftnet();
573 	inp = (struct sctp_inpcb *)so->so_pcb;
574 	if (inp == 0)
575 		return EINVAL;	/* ??? possible? panic instead? */
576 	soisdisconnected(so);
577 	sctp_inpcb_free(inp, 1);
578 	splx(s);
579 	return 0;
580 }
581 
582 static int
583 sctp6_attach(struct socket *so, int proto)
584 {
585 	struct in6pcb *inp6;
586 	int error;
587 	struct sctp_inpcb *inp;
588 
589 	sosetlock(so);
590 	inp = (struct sctp_inpcb *)so->so_pcb;
591 	if (inp != NULL)
592 		return EINVAL;
593 
594 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
595 		error = soreserve(so, sctp_sendspace, sctp_recvspace);
596 		if (error)
597 			return error;
598 	}
599 	error = sctp_inpcb_alloc(so);
600 	if (error)
601 		return error;
602 	inp = (struct sctp_inpcb *)so->so_pcb;
603 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
604 	inp6 = (struct in6pcb *)inp;
605 
606 	inp->inp_vflag |=  INP_IPV6;
607 	if (ip6_v6only) {
608 		inp6->in6p_flags |= IN6P_IPV6_V6ONLY;
609 	}
610 	so->so_send = sctp_sosend;
611 
612 #ifdef IPSEC
613 	inp6->in6p_af = proto;
614 #endif
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 	/* FIXME NOMPSAFE */
1299 	IFADDR_READER_FOREACH(ifa, ifp) {
1300 		if (ifa->ifa_addr->sa_family == PF_INET6) {
1301 			sctp_delete_ip_address(ifa);
1302 		}
1303 	}
1304 
1305 #ifndef NET_MPSAFE
1306 	mutex_enter(softnet_lock);
1307 #endif
1308 	in6_purgeif(ifp);
1309 #ifndef NET_MPSAFE
1310 	mutex_exit(softnet_lock);
1311 #endif
1312 
1313 	return 0;
1314 }
1315 
1316 PR_WRAP_USRREQS(sctp6)
1317 #define	sctp6_attach	sctp6_attach_wrapper
1318 #define	sctp6_detach	sctp6_detach_wrapper
1319 #define sctp6_accept	sctp6_accept_wrapper
1320 #define	sctp6_bind	sctp6_bind_wrapper
1321 #define	sctp6_listen	sctp6_listen_wrapper
1322 #define	sctp6_connect	sctp6_connect_wrapper
1323 #define	sctp6_connect2	sctp6_connect2_wrapper
1324 #define sctp6_disconnect	sctp6_disconnect_wrapper
1325 #define sctp6_shutdown	sctp6_shutdown_wrapper
1326 #define sctp6_abort	sctp6_abort_wrapper
1327 #define	sctp6_ioctl	sctp6_ioctl_wrapper
1328 #define	sctp6_stat	sctp6_stat_wrapper
1329 #define	sctp6_peeraddr	sctp6_peeraddr_wrapper
1330 #define sctp6_sockaddr	sctp6_sockaddr_wrapper
1331 #define sctp6_rcvd	sctp6_rcvd_wrapper
1332 #define sctp6_recvoob	sctp6_recvoob_wrapper
1333 #define sctp6_send	sctp6_send_wrapper
1334 #define sctp6_sendoob	sctp6_sendoob_wrapper
1335 #define sctp6_purgeif	sctp6_purgeif_wrapper
1336 
1337 const struct pr_usrreqs sctp6_usrreqs = {
1338 	.pr_attach	= sctp6_attach,
1339 	.pr_detach	= sctp6_detach,
1340 	.pr_accept	= sctp6_accept,
1341 	.pr_bind	= sctp6_bind,
1342 	.pr_listen	= sctp6_listen,
1343 	.pr_connect	= sctp6_connect,
1344 	.pr_connect2	= sctp6_connect2,
1345 	.pr_disconnect	= sctp6_disconnect,
1346 	.pr_shutdown	= sctp6_shutdown,
1347 	.pr_abort	= sctp6_abort,
1348 	.pr_ioctl	= sctp6_ioctl,
1349 	.pr_stat	= sctp6_stat,
1350 	.pr_peeraddr	= sctp6_peeraddr,
1351 	.pr_sockaddr	= sctp6_sockaddr,
1352 	.pr_rcvd	= sctp6_rcvd,
1353 	.pr_recvoob	= sctp6_recvoob,
1354 	.pr_send	= sctp6_send,
1355 	.pr_sendoob	= sctp6_sendoob,
1356 	.pr_purgeif	= sctp6_purgeif,
1357 };
1358