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