xref: /netbsd-src/sys/netinet6/sctp6_usrreq.c (revision e89934bbf778a6d6d6894877c4da59d0c7835b0f)
1 /* $KAME: sctp6_usrreq.c,v 1.38 2005/08/24 08:08:56 suz Exp $ */
2 /* $NetBSD: sctp6_usrreq.c,v 1.11 2016/12/13 08:29:03 ozaki-r Exp $ */
3 
4 /*
5  * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Cisco Systems, Inc.
19  * 4. Neither the name of the project nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: sctp6_usrreq.c,v 1.11 2016/12/13 08:29:03 ozaki-r 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_so(m, in6p->sctp_socket)) {
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 	inp6->in6p_hops = -1;	        /* use kernel default */
613 	inp6->in6p_cksum = -1;	/* just to be sure */
614 #ifdef INET
615 	/*
616 	 * XXX: ugly!!
617 	 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
618 	 * because the socket may be bound to an IPv6 wildcard address,
619 	 * which may match an IPv4-mapped IPv6 address.
620 	 */
621 	inp->inp_ip_ttl = ip_defttl;
622 #endif
623 	/*
624 	 * Hmm what about the IPSEC stuff that is missing here but
625 	 * in sctp_attach()?
626 	 */
627 	return 0;
628 }
629 
630 static int
631 sctp6_bind(struct socket *so, struct sockaddr *nam, struct lwp *l)
632 {
633 	struct sctp_inpcb *inp;
634 	struct in6pcb *inp6;
635 	int error;
636 
637 	KASSERT(solocked(so));
638 
639 	inp = (struct sctp_inpcb *)so->so_pcb;
640 	if (inp == 0)
641 		return EINVAL;
642 
643 	inp6 = (struct in6pcb *)inp;
644 	inp->inp_vflag &= ~INP_IPV4;
645 	inp->inp_vflag |= INP_IPV6;
646 
647 	if (nam != NULL && (inp6->in6p_flags & IN6P_IPV6_V6ONLY) == 0) {
648 		if (nam->sa_family == AF_INET) {
649 			/* binding v4 addr to v6 socket, so reset flags */
650 			inp->inp_vflag |= INP_IPV4;
651 			inp->inp_vflag &= ~INP_IPV6;
652 		} else {
653 			struct sockaddr_in6 *sin6_p;
654 			sin6_p = (struct sockaddr_in6 *)nam;
655 
656 			if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
657 			  inp->inp_vflag |= INP_IPV4;
658 			}
659 			else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
660 				struct sockaddr_in sin;
661 				in6_sin6_2_sin(&sin, sin6_p);
662 				inp->inp_vflag |= INP_IPV4;
663 				inp->inp_vflag &= ~INP_IPV6;
664 				error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, l);
665 				return error;
666 			}
667 		}
668 	} else if (nam != NULL) {
669 		/* IPV6_V6ONLY socket */
670 		if (nam->sa_family == AF_INET) {
671 			/* can't bind v4 addr to v6 only socket! */
672 			return EINVAL;
673 		} else {
674 			struct sockaddr_in6 *sin6_p;
675 			sin6_p = (struct sockaddr_in6 *)nam;
676 
677 			if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr))
678 				/* can't bind v4-mapped addrs either! */
679 				/* NOTE: we don't support SIIT */
680 				return EINVAL;
681 		}
682 	}
683 	error = sctp_inpcb_bind(so, nam, l);
684 	return error;
685 }
686 
687 /*This could be made common with sctp_detach() since they are identical */
688 static int
689 sctp6_detach(struct socket *so)
690 {
691 	struct sctp_inpcb *inp;
692 
693 	inp = (struct sctp_inpcb *)so->so_pcb;
694 	if (inp == 0)
695 		return EINVAL;
696 
697 	if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
698 	    (so->so_rcv.sb_cc > 0))
699 		sctp_inpcb_free(inp, 1);
700 	else
701 		sctp_inpcb_free(inp, 0);
702 	return 0;
703 }
704 
705 static int
706 sctp6_disconnect(struct socket *so)
707 {
708 	struct sctp_inpcb *inp;
709 
710 	inp = (struct sctp_inpcb *)so->so_pcb;
711 	if (inp == NULL) {
712 		return (ENOTCONN);
713 	}
714 	if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
715 		if (LIST_EMPTY(&inp->sctp_asoc_list)) {
716 			/* No connection */
717 			return (ENOTCONN);
718 		} else {
719 			int some_on_streamwheel = 0;
720 			struct sctp_association *asoc;
721 			struct sctp_tcb *stcb;
722 
723 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
724 			if (stcb == NULL) {
725 				return (EINVAL);
726 			}
727 			asoc = &stcb->asoc;
728 			if (!TAILQ_EMPTY(&asoc->out_wheel)) {
729 				/* Check to see if some data queued */
730 				struct sctp_stream_out *outs;
731 				TAILQ_FOREACH(outs, &asoc->out_wheel,
732 					      next_spoke) {
733 					if (!TAILQ_EMPTY(&outs->outqueue)) {
734 						some_on_streamwheel = 1;
735 						break;
736 					}
737 				}
738 			}
739 
740 			if (TAILQ_EMPTY(&asoc->send_queue) &&
741 			    TAILQ_EMPTY(&asoc->sent_queue) &&
742 			    (some_on_streamwheel == 0)) {
743 				/* nothing queued to send, so I'm done... */
744 				if ((SCTP_GET_STATE(asoc) !=
745 				     SCTP_STATE_SHUTDOWN_SENT) &&
746 				    (SCTP_GET_STATE(asoc) !=
747 				     SCTP_STATE_SHUTDOWN_ACK_SENT)) {
748 					/* only send SHUTDOWN the first time */
749 #ifdef SCTP_DEBUG
750 					if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
751 						printf("%s:%d sends a shutdown\n",
752 						       __FILE__,
753 						       __LINE__
754 							);
755 					}
756 #endif
757 					sctp_send_shutdown(stcb, stcb->asoc.primary_destination);
758 					sctp_chunk_output(stcb->sctp_ep, stcb, 1);
759 					asoc->state = SCTP_STATE_SHUTDOWN_SENT;
760 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
761 							 stcb->sctp_ep, stcb,
762 							 asoc->primary_destination);
763 					sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
764 							 stcb->sctp_ep, stcb,
765 							 asoc->primary_destination);
766 				}
767 			} else {
768 				/*
769 				 * we still got (or just got) data to send,
770 				 * so set SHUTDOWN_PENDING
771 				 */
772 				/*
773 				 * XXX sockets draft says that MSG_EOF should
774 				 * be sent with no data.  currently, we will
775 				 * allow user data to be sent first and move
776 				 * to SHUTDOWN-PENDING
777 				 */
778 				asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
779 			}
780 			return (0);
781 		}
782 	} else {
783 		/* UDP model does not support this */
784 		return EOPNOTSUPP;
785 	}
786 }
787 
788 static int
789 sctp6_recvoob(struct socket *so, struct mbuf *m, int flags)
790 {
791 	KASSERT(solocked(so));
792 
793 	return EOPNOTSUPP;
794 }
795 
796 static int
797 sctp6_send(struct socket *so, struct mbuf *m, struct sockaddr *nam,
798 	   struct mbuf *control, struct lwp *l)
799 {
800 	struct sctp_inpcb *inp;
801 	struct in6pcb *inp6;
802 #ifdef INET
803 	struct sockaddr_in6 *sin6;
804 #endif /* INET */
805 	/* No SPL needed since sctp_output does this */
806 
807 	inp = (struct sctp_inpcb *)so->so_pcb;
808 	if (inp == NULL) {
809 	        if (control) {
810 			m_freem(control);
811 			control = NULL;
812 		}
813 		m_freem(m);
814 		return EINVAL;
815 	}
816 	inp6 = (struct in6pcb *)inp;
817 	/* For the TCP model we may get a NULL addr, if we
818 	 * are a connected socket thats ok.
819 	 */
820 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
821 	    (nam == NULL)) {
822 	        goto connected_type;
823 	}
824 	if (nam == NULL) {
825 		m_freem(m);
826 		if (control) {
827 			m_freem(control);
828 			control = NULL;
829 		}
830 		return (EDESTADDRREQ);
831 	}
832 
833 #ifdef INET
834 	sin6 = (struct sockaddr_in6 *)nam;
835 	if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
836 		/*
837 		 * if IPV6_V6ONLY flag, we discard datagrams
838 		 * destined to a v4 addr or v4-mapped addr
839 		 */
840 		if (nam->sa_family == AF_INET) {
841 			return EINVAL;
842 		}
843 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
844 			return EINVAL;
845 		}
846 	}
847 
848 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
849 		if (!ip6_v6only) {
850 			struct sockaddr_in sin;
851 			/* convert v4-mapped into v4 addr and send */
852 			in6_sin6_2_sin(&sin, sin6);
853 			return sctp_send(so, m, (struct sockaddr *)&sin,
854 			    control, l);
855 		} else {
856 			/* mapped addresses aren't enabled */
857 			return EINVAL;
858 		}
859 	}
860 #endif /* INET */
861  connected_type:
862 	/* now what about control */
863 	if (control) {
864 		if (inp->control) {
865 			printf("huh? control set?\n");
866 			m_freem(inp->control);
867 			inp->control = NULL;
868 		}
869 		inp->control = control;
870 	}
871 	/* add it in possibly */
872 	if ((inp->pkt) &&
873 	    (inp->pkt->m_flags & M_PKTHDR)) {
874 		struct mbuf *x;
875 		int c_len;
876 
877 		c_len = 0;
878 		/* How big is it */
879 		for (x=m;x;x = x->m_next) {
880 			c_len += x->m_len;
881 		}
882 		inp->pkt->m_pkthdr.len += c_len;
883 	}
884 	/* Place the data */
885 	if (inp->pkt) {
886 		inp->pkt_last->m_next = m;
887 		inp->pkt_last = m;
888 	} else {
889 		inp->pkt_last = inp->pkt = m;
890 	}
891 	if ((so->so_state & SS_MORETOCOME) == 0) {
892 		/*
893 		 * note with the current version this code will only be
894 		 * used by OpenBSD, NetBSD and FreeBSD have methods for
895 		 * re-defining sosend() to use sctp_sosend().  One can
896 		 * optionaly switch back to this code (by changing back
897 		 * the defininitions but this is not advisable.
898 		 */
899 		int ret;
900 		ret = sctp_output(inp, inp->pkt , nam, inp->control, l, 0);
901 		inp->pkt = NULL;
902 		inp->control = NULL;
903 		return (ret);
904 	} else {
905 		return (0);
906 	}
907 }
908 
909 static int
910 sctp6_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
911 {
912 	KASSERT(solocked(so));
913 
914 	if (m)
915 		m_freem(m);
916 	if (control)
917 		m_freem(control);
918 
919 	return EOPNOTSUPP;
920 }
921 
922 static int
923 sctp6_connect(struct socket *so, struct sockaddr *nam, struct lwp *l)
924 {
925 	int error = 0;
926 	struct sctp_inpcb *inp;
927 	struct in6pcb *inp6;
928 	struct sctp_tcb *stcb;
929 #ifdef INET
930 	struct sockaddr_in6 *sin6;
931 	struct sockaddr_storage ss;
932 #endif /* INET */
933 
934 	inp6 = (struct in6pcb *)so->so_pcb;
935 	inp = (struct sctp_inpcb *)so->so_pcb;
936 	if (inp == 0) {
937 		return (ECONNRESET);	/* I made the same as TCP since
938 					 * we are not setup? */
939 	}
940 	SCTP_ASOC_CREATE_LOCK(inp);
941 	SCTP_INP_RLOCK(inp);
942 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
943 	    SCTP_PCB_FLAGS_UNBOUND) {
944 		/* Bind a ephemeral port */
945 		SCTP_INP_RUNLOCK(inp);
946 		error = sctp6_bind(so, NULL, l);
947 		if (error) {
948 			SCTP_ASOC_CREATE_UNLOCK(inp);
949 
950 			return (error);
951 		}
952 		SCTP_INP_RLOCK(inp);
953 	}
954 
955 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
956 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
957 		/* We are already connected AND the TCP model */
958 		SCTP_INP_RUNLOCK(inp);
959 		SCTP_ASOC_CREATE_UNLOCK(inp);
960 		return (EADDRINUSE);
961 	}
962 
963 #ifdef INET
964 	sin6 = (struct sockaddr_in6 *)nam;
965 	if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
966 		/*
967 		 * if IPV6_V6ONLY flag, ignore connections
968 		 * destined to a v4 addr or v4-mapped addr
969 		 */
970 		if (nam->sa_family == AF_INET) {
971 			SCTP_INP_RUNLOCK(inp);
972 			SCTP_ASOC_CREATE_UNLOCK(inp);
973 			return EINVAL;
974 		}
975 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
976 			SCTP_INP_RUNLOCK(inp);
977 			SCTP_ASOC_CREATE_UNLOCK(inp);
978 			return EINVAL;
979 		}
980 	}
981 
982 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
983 		if (!ip6_v6only) {
984 			/* convert v4-mapped into v4 addr */
985 			in6_sin6_2_sin((struct sockaddr_in *)&ss, sin6);
986 			nam = (struct sockaddr *)&ss;
987 		} else {
988 			/* mapped addresses aren't enabled */
989 			SCTP_INP_RUNLOCK(inp);
990 			SCTP_ASOC_CREATE_UNLOCK(inp);
991 			return EINVAL;
992 		}
993 	}
994 #endif /* INET */
995 
996 	/* Now do we connect? */
997 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
998 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
999 		if (stcb) {
1000 			SCTP_TCB_UNLOCK (stcb);
1001 		}
1002 		SCTP_INP_RUNLOCK(inp);
1003 	} else {
1004 		SCTP_INP_RUNLOCK(inp);
1005 		SCTP_INP_WLOCK(inp);
1006 		SCTP_INP_INCR_REF(inp);
1007 		SCTP_INP_WUNLOCK(inp);
1008 		stcb = sctp_findassociation_ep_addr(&inp, nam, NULL, NULL, NULL);
1009 		if (stcb == NULL) {
1010 			SCTP_INP_WLOCK(inp);
1011 			SCTP_INP_DECR_REF(inp);
1012 			SCTP_INP_WUNLOCK(inp);
1013 		}
1014 	}
1015 
1016 	if (stcb != NULL) {
1017 		/* Already have or am bring up an association */
1018 		SCTP_ASOC_CREATE_UNLOCK(inp);
1019 		SCTP_TCB_UNLOCK (stcb);
1020 		return (EALREADY);
1021 	}
1022 	/* We are GOOD to go */
1023 	stcb = sctp_aloc_assoc(inp, nam, 1, &error, 0);
1024 	SCTP_ASOC_CREATE_UNLOCK(inp);
1025 	if (stcb == NULL) {
1026 		/* Gak! no memory */
1027 		return (error);
1028 	}
1029 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1030 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1031 		/* Set the connected flag so we can queue data */
1032 		soisconnecting(so);
1033 	}
1034 	stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1035 	SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1036 	sctp_send_initiate(inp, stcb);
1037 	SCTP_TCB_UNLOCK (stcb);
1038 	return error;
1039 }
1040 
1041 static int
1042 sctp6_connect2(struct socket *so, struct socket *so2)
1043 {
1044 	KASSERT(solocked(so));
1045 
1046 	return EOPNOTSUPP;
1047 }
1048 
1049 static int
1050 sctp6_getaddr(struct socket *so, struct sockaddr *nam)
1051 {
1052 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1053 	struct sctp_inpcb *inp;
1054 	int error;
1055 
1056 	/*
1057 	 * Do the malloc first in case it blocks.
1058 	 */
1059 	memset(sin6, 0, sizeof(*sin6));
1060 	sin6->sin6_family = AF_INET6;
1061 	sin6->sin6_len = sizeof(*sin6);
1062 
1063 	inp = (struct sctp_inpcb *)so->so_pcb;
1064 	if (inp == NULL) {
1065 		return ECONNRESET;
1066 	}
1067 
1068 	sin6->sin6_port = inp->sctp_lport;
1069 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1070 		/* For the bound all case you get back 0 */
1071 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1072 			struct sctp_tcb *stcb;
1073 			const struct sockaddr_in6 *sin_a6;
1074 			struct sctp_nets *net;
1075 			int fnd;
1076 
1077 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
1078 			if (stcb == NULL) {
1079 				goto notConn6;
1080 			}
1081 			fnd = 0;
1082 			sin_a6 = NULL;
1083 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1084 				sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
1085 				if (sin_a6->sin6_family == AF_INET6) {
1086 					fnd = 1;
1087 					break;
1088 				}
1089 			}
1090 			if ((!fnd) || (sin_a6 == NULL)) {
1091 				/* punt */
1092 				goto notConn6;
1093 			}
1094 			sin6->sin6_addr = sctp_ipv6_source_address_selection(
1095 			    inp, stcb, &net->ro, net, 0);
1096 
1097 		} else {
1098 			/* For the bound all case you get back 0 */
1099 		notConn6:
1100 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
1101 		}
1102 	} else {
1103 		/* Take the first IPv6 address in the list */
1104 		struct sctp_laddr *laddr;
1105 		int fnd = 0;
1106 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1107 			if (laddr->ifa->ifa_addr->sa_family == AF_INET6) {
1108 				struct sockaddr_in6 *sin_a;
1109 				sin_a = (struct sockaddr_in6 *)laddr->ifa->ifa_addr;
1110 				sin6->sin6_addr = sin_a->sin6_addr;
1111 				fnd = 1;
1112 				break;
1113 			}
1114 		}
1115 		if (!fnd) {
1116 			return ENOENT;
1117 		}
1118 	}
1119 	/* Scoping things for v6 */
1120 	if ((error = sa6_recoverscope(sin6)) != 0)
1121 		return (error);
1122 
1123 	return (0);
1124 }
1125 
1126 static int
1127 sctp6_peeraddr(struct socket *so, struct sockaddr *nam)
1128 {
1129 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1130 	int fnd, error;
1131 	const struct sockaddr_in6 *sin_a6;
1132 	struct sctp_inpcb *inp;
1133 	struct sctp_tcb *stcb;
1134 	struct sctp_nets *net;
1135 	/*
1136 	 * Do the malloc first in case it blocks.
1137 	 */
1138 	inp = (struct sctp_inpcb *)so->so_pcb;
1139 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
1140 		/* UDP type and listeners will drop out here */
1141 		return (ENOTCONN);
1142 	}
1143 	memset(sin6, 0, sizeof(*sin6));
1144 	sin6->sin6_family = AF_INET6;
1145 	sin6->sin6_len = sizeof(*sin6);
1146 
1147 	/* We must recapture incase we blocked */
1148 	inp = (struct sctp_inpcb *)so->so_pcb;
1149 	if (inp == NULL) {
1150 		return ECONNRESET;
1151 	}
1152 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1153 	if (stcb == NULL) {
1154 		return ECONNRESET;
1155 	}
1156 	fnd = 0;
1157 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1158 		sin_a6 = (const struct sockaddr_in6 *)rtcache_getdst(&net->ro);
1159 		if (sin_a6->sin6_family == AF_INET6) {
1160 			fnd = 1;
1161 			sin6->sin6_port = stcb->rport;
1162 			sin6->sin6_addr = sin_a6->sin6_addr;
1163 			break;
1164 		}
1165 	}
1166 	if (!fnd) {
1167 		/* No IPv4 address */
1168 		return ENOENT;
1169 	}
1170 	if ((error = sa6_recoverscope(sin6)) != 0)
1171 		return (error);
1172 
1173 	return (0);
1174 }
1175 
1176 static int
1177 sctp6_sockaddr(struct socket *so, struct sockaddr *nam)
1178 {
1179 	struct in6pcb *inp6 = sotoin6pcb(so);
1180 	int	error;
1181 
1182 	if (inp6 == NULL)
1183 		return EINVAL;
1184 
1185 	/* allow v6 addresses precedence */
1186 	error = sctp6_getaddr(so, nam);
1187 	if (error) {
1188 		/* try v4 next if v6 failed */
1189 		error = sctp_sockaddr(so, nam);
1190 		if (error) {
1191 			return (error);
1192 		}
1193 
1194 		/* if I'm V6ONLY, convert it to v4-mapped */
1195 		if (inp6->in6p_flags & IN6P_IPV6_V6ONLY) {
1196 			struct sockaddr_in6 sin6;
1197 			in6_sin_2_v4mapsin6((struct sockaddr_in *)nam, &sin6);
1198 			memcpy(nam, &sin6, sizeof(struct sockaddr_in6));
1199 		}
1200 	}
1201 	return (error);
1202 }
1203 
1204 #if 0
1205 static int
1206 sctp6_getpeeraddr(struct socket *so, struct sockaddr *nam)
1207 {
1208 	struct in6pcb *inp6 = sotoin6pcb(so);
1209 	int	error;
1210 
1211 	if (inp6 == NULL)
1212 		return EINVAL;
1213 
1214 	/* allow v6 addresses precedence */
1215 	error = sctp6_peeraddr(so, nam);
1216 	if (error) {
1217 		/* try v4 next if v6 failed */
1218 		error = sctp_peeraddr(so, nam);
1219 		if (error) {
1220 			return (error);
1221 		}
1222 		/* if I'm V6ONLY, convert it to v4-mapped */
1223 		if ((inp6->in6p_flags & IN6P_IPV6_V6ONLY)) {
1224 			struct sockaddr_in6 sin6;
1225 			in6_sin_2_v4mapsin6((struct sockaddr_in *)addr, &sin6);
1226 			memcpy(addr, &sin6, sizeof(struct sockaddr_in6));
1227 		}
1228 	}
1229 	return error;
1230 }
1231 #endif
1232 
1233 static int
1234 sctp6_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
1235 {
1236 	int error = 0;
1237 	int family;
1238 
1239 	family = so->so_proto->pr_domain->dom_family;
1240 	switch (family) {
1241 #ifdef INET
1242 	case PF_INET:
1243 		error = in_control(so, cmd, nam, ifp);
1244 		break;
1245 #endif
1246 #ifdef INET6
1247 	case PF_INET6:
1248 		error = in6_control(so, cmd, nam, ifp);
1249 		break;
1250 #endif
1251 	default:
1252 		error = EAFNOSUPPORT;
1253 	}
1254 	return (error);
1255 }
1256 
1257 static int
1258 sctp6_accept(struct socket *so, struct sockaddr *nam)
1259 {
1260 	KASSERT(solocked(so));
1261 
1262 	return EOPNOTSUPP;
1263 }
1264 
1265 static int
1266 sctp6_stat(struct socket *so, struct stat *ub)
1267 {
1268 	return 0;
1269 }
1270 
1271 static int
1272 sctp6_listen(struct socket *so, struct lwp *l)
1273 {
1274 	return sctp_listen(so, l);
1275 }
1276 
1277 static int
1278 sctp6_shutdown(struct socket *so)
1279 {
1280 	return sctp_shutdown(so);
1281 }
1282 
1283 static int
1284 sctp6_rcvd(struct socket *so, int flags, struct lwp *l)
1285 {
1286 	KASSERT(solocked(so));
1287 
1288 	return sctp_rcvd(so, flags, l);
1289 }
1290 
1291 static int
1292 sctp6_purgeif(struct socket *so, struct ifnet *ifp)
1293 {
1294 	struct ifaddr *ifa;
1295 	/* FIXME NOMPSAFE */
1296 	IFADDR_READER_FOREACH(ifa, ifp) {
1297 		if (ifa->ifa_addr->sa_family == PF_INET6) {
1298 			sctp_delete_ip_address(ifa);
1299 		}
1300 	}
1301 
1302 #ifndef NET_MPSAFE
1303 	mutex_enter(softnet_lock);
1304 #endif
1305 	in6_purgeif(ifp);
1306 #ifndef NET_MPSAFE
1307 	mutex_exit(softnet_lock);
1308 #endif
1309 
1310 	return 0;
1311 }
1312 
1313 PR_WRAP_USRREQS(sctp6)
1314 #define	sctp6_attach	sctp6_attach_wrapper
1315 #define	sctp6_detach	sctp6_detach_wrapper
1316 #define sctp6_accept	sctp6_accept_wrapper
1317 #define	sctp6_bind	sctp6_bind_wrapper
1318 #define	sctp6_listen	sctp6_listen_wrapper
1319 #define	sctp6_connect	sctp6_connect_wrapper
1320 #define	sctp6_connect2	sctp6_connect2_wrapper
1321 #define sctp6_disconnect	sctp6_disconnect_wrapper
1322 #define sctp6_shutdown	sctp6_shutdown_wrapper
1323 #define sctp6_abort	sctp6_abort_wrapper
1324 #define	sctp6_ioctl	sctp6_ioctl_wrapper
1325 #define	sctp6_stat	sctp6_stat_wrapper
1326 #define	sctp6_peeraddr	sctp6_peeraddr_wrapper
1327 #define sctp6_sockaddr	sctp6_sockaddr_wrapper
1328 #define sctp6_rcvd	sctp6_rcvd_wrapper
1329 #define sctp6_recvoob	sctp6_recvoob_wrapper
1330 #define sctp6_send	sctp6_send_wrapper
1331 #define sctp6_sendoob	sctp6_sendoob_wrapper
1332 #define sctp6_purgeif	sctp6_purgeif_wrapper
1333 
1334 const struct pr_usrreqs sctp6_usrreqs = {
1335 	.pr_attach	= sctp6_attach,
1336 	.pr_detach	= sctp6_detach,
1337 	.pr_accept	= sctp6_accept,
1338 	.pr_bind	= sctp6_bind,
1339 	.pr_listen	= sctp6_listen,
1340 	.pr_connect	= sctp6_connect,
1341 	.pr_connect2	= sctp6_connect2,
1342 	.pr_disconnect	= sctp6_disconnect,
1343 	.pr_shutdown	= sctp6_shutdown,
1344 	.pr_abort	= sctp6_abort,
1345 	.pr_ioctl	= sctp6_ioctl,
1346 	.pr_stat	= sctp6_stat,
1347 	.pr_peeraddr	= sctp6_peeraddr,
1348 	.pr_sockaddr	= sctp6_sockaddr,
1349 	.pr_rcvd	= sctp6_rcvd,
1350 	.pr_recvoob	= sctp6_recvoob,
1351 	.pr_send	= sctp6_send,
1352 	.pr_sendoob	= sctp6_sendoob,
1353 	.pr_purgeif	= sctp6_purgeif,
1354 };
1355