xref: /freebsd/sys/netinet6/sctp6_usrreq.c (revision 4133f23624058951a3b66e3ad735de980a485f36)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2007, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. 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 are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <netinet/sctp_os.h>
39 #ifdef INET6
40 #include <sys/proc.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_var.h>
44 #include <netinet6/sctp6_var.h>
45 #include <netinet/sctp_sysctl.h>
46 #include <netinet/sctp_output.h>
47 #include <netinet/sctp_uio.h>
48 #include <netinet/sctp_asconf.h>
49 #include <netinet/sctputil.h>
50 #include <netinet/sctp_indata.h>
51 #include <netinet/sctp_timer.h>
52 #include <netinet/sctp_auth.h>
53 #include <netinet/sctp_input.h>
54 #include <netinet/sctp_output.h>
55 #include <netinet/sctp_bsd_addr.h>
56 #include <netinet/sctp_crc32.h>
57 #include <netinet/icmp6.h>
58 #include <netinet/udp.h>
59 
60 int
61 sctp6_input_with_port(struct mbuf **i_pak, int *offp, uint16_t port)
62 {
63 	struct mbuf *m;
64 	int iphlen;
65 	uint32_t vrf_id;
66 	uint8_t ecn_bits;
67 	struct sockaddr_in6 src, dst;
68 	struct ip6_hdr *ip6;
69 	struct sctphdr *sh;
70 	struct sctp_chunkhdr *ch;
71 	int length, offset;
72 	uint8_t compute_crc;
73 	uint32_t mflowid;
74 	uint8_t mflowtype;
75 	uint16_t fibnum;
76 
77 	iphlen = *offp;
78 	if (SCTP_GET_PKT_VRFID(*i_pak, vrf_id)) {
79 		SCTP_RELEASE_PKT(*i_pak);
80 		return (IPPROTO_DONE);
81 	}
82 	m = SCTP_HEADER_TO_CHAIN(*i_pak);
83 #ifdef SCTP_MBUF_LOGGING
84 	/* Log in any input mbufs */
85 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
86 		sctp_log_mbc(m, SCTP_MBUF_INPUT);
87 	}
88 #endif
89 #ifdef SCTP_PACKET_LOGGING
90 	if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
91 		sctp_packet_log(m);
92 	}
93 #endif
94 	SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
95 	    "sctp6_input(): Packet of length %d received on %s with csum_flags 0x%b.\n",
96 	    m->m_pkthdr.len,
97 	    if_name(m->m_pkthdr.rcvif),
98 	    (int)m->m_pkthdr.csum_flags, CSUM_BITS);
99 	mflowid = m->m_pkthdr.flowid;
100 	mflowtype = M_HASHTYPE_GET(m);
101 	fibnum = M_GETFIB(m);
102 	SCTP_STAT_INCR(sctps_recvpackets);
103 	SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
104 	/* Get IP, SCTP, and first chunk header together in the first mbuf. */
105 	offset = iphlen + sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
106 	ip6 = mtod(m, struct ip6_hdr *);
107 	IP6_EXTHDR_GET(sh, struct sctphdr *, m, iphlen,
108 	    (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr)));
109 	if (sh == NULL) {
110 		SCTP_STAT_INCR(sctps_hdrops);
111 		return (IPPROTO_DONE);
112 	}
113 	ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
114 	offset -= sizeof(struct sctp_chunkhdr);
115 	memset(&src, 0, sizeof(struct sockaddr_in6));
116 	src.sin6_family = AF_INET6;
117 	src.sin6_len = sizeof(struct sockaddr_in6);
118 	src.sin6_port = sh->src_port;
119 	src.sin6_addr = ip6->ip6_src;
120 	if (in6_setscope(&src.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
121 		goto out;
122 	}
123 	memset(&dst, 0, sizeof(struct sockaddr_in6));
124 	dst.sin6_family = AF_INET6;
125 	dst.sin6_len = sizeof(struct sockaddr_in6);
126 	dst.sin6_port = sh->dest_port;
127 	dst.sin6_addr = ip6->ip6_dst;
128 	if (in6_setscope(&dst.sin6_addr, m->m_pkthdr.rcvif, NULL) != 0) {
129 		goto out;
130 	}
131 	length = ntohs(ip6->ip6_plen) + iphlen;
132 	/* Validate mbuf chain length with IP payload length. */
133 	if (SCTP_HEADER_LEN(m) != length) {
134 		SCTPDBG(SCTP_DEBUG_INPUT1,
135 		    "sctp6_input() length:%d reported length:%d\n", length, SCTP_HEADER_LEN(m));
136 		SCTP_STAT_INCR(sctps_hdrops);
137 		goto out;
138 	}
139 	if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
140 		goto out;
141 	}
142 	ecn_bits = ((ntohl(ip6->ip6_flow) >> 20) & 0x000000ff);
143 	if (m->m_pkthdr.csum_flags & CSUM_SCTP_VALID) {
144 		SCTP_STAT_INCR(sctps_recvhwcrc);
145 		compute_crc = 0;
146 	} else {
147 		SCTP_STAT_INCR(sctps_recvswcrc);
148 		compute_crc = 1;
149 	}
150 	sctp_common_input_processing(&m, iphlen, offset, length,
151 	    (struct sockaddr *)&src,
152 	    (struct sockaddr *)&dst,
153 	    sh, ch,
154 	    compute_crc,
155 	    ecn_bits,
156 	    mflowtype, mflowid, fibnum,
157 	    vrf_id, port);
158 out:
159 	if (m) {
160 		sctp_m_freem(m);
161 	}
162 	return (IPPROTO_DONE);
163 }
164 
165 
166 int
167 sctp6_input(struct mbuf **i_pak, int *offp, int proto SCTP_UNUSED)
168 {
169 	return (sctp6_input_with_port(i_pak, offp, 0));
170 }
171 
172 void
173 sctp6_notify(struct sctp_inpcb *inp,
174     struct sctp_tcb *stcb,
175     struct sctp_nets *net,
176     uint8_t icmp6_type,
177     uint8_t icmp6_code,
178     uint32_t next_mtu)
179 {
180 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
181 	struct socket *so;
182 #endif
183 	int timer_stopped;
184 
185 	switch (icmp6_type) {
186 	case ICMP6_DST_UNREACH:
187 		if ((icmp6_code == ICMP6_DST_UNREACH_NOROUTE) ||
188 		    (icmp6_code == ICMP6_DST_UNREACH_ADMIN) ||
189 		    (icmp6_code == ICMP6_DST_UNREACH_BEYONDSCOPE) ||
190 		    (icmp6_code == ICMP6_DST_UNREACH_ADDR)) {
191 			/* Mark the net unreachable. */
192 			if (net->dest_state & SCTP_ADDR_REACHABLE) {
193 				/* Ok that destination is not reachable */
194 				net->dest_state &= ~SCTP_ADDR_REACHABLE;
195 				net->dest_state &= ~SCTP_ADDR_PF;
196 				sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
197 				    stcb, 0, (void *)net, SCTP_SO_NOT_LOCKED);
198 			}
199 		}
200 		SCTP_TCB_UNLOCK(stcb);
201 		break;
202 	case ICMP6_PARAM_PROB:
203 		/* Treat it like an ABORT. */
204 		if (icmp6_code == ICMP6_PARAMPROB_NEXTHEADER) {
205 			sctp_abort_notification(stcb, 1, 0, NULL, SCTP_SO_NOT_LOCKED);
206 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
207 			so = SCTP_INP_SO(inp);
208 			atomic_add_int(&stcb->asoc.refcnt, 1);
209 			SCTP_TCB_UNLOCK(stcb);
210 			SCTP_SOCKET_LOCK(so, 1);
211 			SCTP_TCB_LOCK(stcb);
212 			atomic_subtract_int(&stcb->asoc.refcnt, 1);
213 #endif
214 			(void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
215 			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_2);
216 #if defined(__APPLE__) || defined(SCTP_SO_LOCK_TESTING)
217 			SCTP_SOCKET_UNLOCK(so, 1);
218 #endif
219 		} else {
220 			SCTP_TCB_UNLOCK(stcb);
221 		}
222 		break;
223 	case ICMP6_PACKET_TOO_BIG:
224 		if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
225 			SCTP_TCB_UNLOCK(stcb);
226 			break;
227 		}
228 		if (SCTP_OS_TIMER_PENDING(&net->pmtu_timer.timer)) {
229 			timer_stopped = 1;
230 			sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
231 			    SCTP_FROM_SCTP_USRREQ + SCTP_LOC_1);
232 		} else {
233 			timer_stopped = 0;
234 		}
235 		/* Update the path MTU. */
236 		if (net->port) {
237 			next_mtu -= sizeof(struct udphdr);
238 		}
239 		if (net->mtu > next_mtu) {
240 			net->mtu = next_mtu;
241 		}
242 		/* Update the association MTU */
243 		if (stcb->asoc.smallest_mtu > next_mtu) {
244 			sctp_pathmtu_adjustment(stcb, next_mtu);
245 		}
246 		/* Finally, start the PMTU timer if it was running before. */
247 		if (timer_stopped) {
248 			sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net);
249 		}
250 		SCTP_TCB_UNLOCK(stcb);
251 		break;
252 	default:
253 		SCTP_TCB_UNLOCK(stcb);
254 		break;
255 	}
256 }
257 
258 void
259 sctp6_ctlinput(int cmd, struct sockaddr *pktdst, void *d)
260 {
261 	struct ip6ctlparam *ip6cp;
262 	struct sctp_inpcb *inp;
263 	struct sctp_tcb *stcb;
264 	struct sctp_nets *net;
265 	struct sctphdr sh;
266 	struct sockaddr_in6 src, dst;
267 
268 	if (pktdst->sa_family != AF_INET6 ||
269 	    pktdst->sa_len != sizeof(struct sockaddr_in6)) {
270 		return;
271 	}
272 
273 	if ((unsigned)cmd >= PRC_NCMDS) {
274 		return;
275 	}
276 	if (PRC_IS_REDIRECT(cmd)) {
277 		d = NULL;
278 	} else if (inet6ctlerrmap[cmd] == 0) {
279 		return;
280 	}
281 	/* If the parameter is from icmp6, decode it. */
282 	if (d != NULL) {
283 		ip6cp = (struct ip6ctlparam *)d;
284 	} else {
285 		ip6cp = (struct ip6ctlparam *)NULL;
286 	}
287 
288 	if (ip6cp != NULL) {
289 		/*
290 		 * XXX: We assume that when IPV6 is non NULL, M and OFF are
291 		 * valid.
292 		 */
293 		if (ip6cp->ip6c_m == NULL) {
294 			return;
295 		}
296 
297 		/*
298 		 * Check if we can safely examine the ports and the
299 		 * verification tag of the SCTP common header.
300 		 */
301 		if (ip6cp->ip6c_m->m_pkthdr.len <
302 		    (int32_t)(ip6cp->ip6c_off + offsetof(struct sctphdr, checksum))) {
303 			return;
304 		}
305 
306 		/* Copy out the port numbers and the verification tag. */
307 		memset(&sh, 0, sizeof(sh));
308 		m_copydata(ip6cp->ip6c_m,
309 		    ip6cp->ip6c_off,
310 		    sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
311 		    (caddr_t)&sh);
312 		memset(&src, 0, sizeof(struct sockaddr_in6));
313 		src.sin6_family = AF_INET6;
314 		src.sin6_len = sizeof(struct sockaddr_in6);
315 		src.sin6_port = sh.src_port;
316 		src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
317 		if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
318 			return;
319 		}
320 		memset(&dst, 0, sizeof(struct sockaddr_in6));
321 		dst.sin6_family = AF_INET6;
322 		dst.sin6_len = sizeof(struct sockaddr_in6);
323 		dst.sin6_port = sh.dest_port;
324 		dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
325 		if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
326 			return;
327 		}
328 		inp = NULL;
329 		net = NULL;
330 		stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
331 		    (struct sockaddr *)&src,
332 		    &inp, &net, 1, SCTP_DEFAULT_VRFID);
333 		if ((stcb != NULL) &&
334 		    (net != NULL) &&
335 		    (inp != NULL)) {
336 			/* Check the verification tag */
337 			if (ntohl(sh.v_tag) != 0) {
338 				/*
339 				 * This must be the verification tag used
340 				 * for sending out packets. We don't
341 				 * consider packets reflecting the
342 				 * verification tag.
343 				 */
344 				if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
345 					SCTP_TCB_UNLOCK(stcb);
346 					return;
347 				}
348 			} else {
349 				if (ip6cp->ip6c_m->m_pkthdr.len >=
350 				    ip6cp->ip6c_off + sizeof(struct sctphdr) +
351 				    sizeof(struct sctp_chunkhdr) +
352 				    offsetof(struct sctp_init, a_rwnd)) {
353 					/*
354 					 * In this case we can check if we
355 					 * got an INIT chunk and if the
356 					 * initiate tag matches.
357 					 */
358 					uint32_t initiate_tag;
359 					uint8_t chunk_type;
360 
361 					m_copydata(ip6cp->ip6c_m,
362 					    ip6cp->ip6c_off +
363 					    sizeof(struct sctphdr),
364 					    sizeof(uint8_t),
365 					    (caddr_t)&chunk_type);
366 					m_copydata(ip6cp->ip6c_m,
367 					    ip6cp->ip6c_off +
368 					    sizeof(struct sctphdr) +
369 					    sizeof(struct sctp_chunkhdr),
370 					    sizeof(uint32_t),
371 					    (caddr_t)&initiate_tag);
372 					if ((chunk_type != SCTP_INITIATION) ||
373 					    (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
374 						SCTP_TCB_UNLOCK(stcb);
375 						return;
376 					}
377 				} else {
378 					SCTP_TCB_UNLOCK(stcb);
379 					return;
380 				}
381 			}
382 			sctp6_notify(inp, stcb, net,
383 			    ip6cp->ip6c_icmp6->icmp6_type,
384 			    ip6cp->ip6c_icmp6->icmp6_code,
385 			    ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
386 		} else {
387 			if ((stcb == NULL) && (inp != NULL)) {
388 				/* reduce inp's ref-count */
389 				SCTP_INP_WLOCK(inp);
390 				SCTP_INP_DECR_REF(inp);
391 				SCTP_INP_WUNLOCK(inp);
392 			}
393 			if (stcb) {
394 				SCTP_TCB_UNLOCK(stcb);
395 			}
396 		}
397 	}
398 }
399 
400 /*
401  * this routine can probably be collasped into the one in sctp_userreq.c
402  * since they do the same thing and now we lookup with a sockaddr
403  */
404 static int
405 sctp6_getcred(SYSCTL_HANDLER_ARGS)
406 {
407 	struct xucred xuc;
408 	struct sockaddr_in6 addrs[2];
409 	struct sctp_inpcb *inp;
410 	struct sctp_nets *net;
411 	struct sctp_tcb *stcb;
412 	int error;
413 	uint32_t vrf_id;
414 
415 	vrf_id = SCTP_DEFAULT_VRFID;
416 
417 	error = priv_check(req->td, PRIV_NETINET_GETCRED);
418 	if (error)
419 		return (error);
420 
421 	if (req->newlen != sizeof(addrs)) {
422 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
423 		return (EINVAL);
424 	}
425 	if (req->oldlen != sizeof(struct ucred)) {
426 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
427 		return (EINVAL);
428 	}
429 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
430 	if (error)
431 		return (error);
432 
433 	stcb = sctp_findassociation_addr_sa(sin6tosa(&addrs[1]),
434 	    sin6tosa(&addrs[0]),
435 	    &inp, &net, 1, vrf_id);
436 	if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
437 		if ((inp != NULL) && (stcb == NULL)) {
438 			/* reduce ref-count */
439 			SCTP_INP_WLOCK(inp);
440 			SCTP_INP_DECR_REF(inp);
441 			goto cred_can_cont;
442 		}
443 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
444 		error = ENOENT;
445 		goto out;
446 	}
447 	SCTP_TCB_UNLOCK(stcb);
448 	/*
449 	 * We use the write lock here, only since in the error leg we need
450 	 * it. If we used RLOCK, then we would have to
451 	 * wlock/decr/unlock/rlock. Which in theory could create a hole.
452 	 * Better to use higher wlock.
453 	 */
454 	SCTP_INP_WLOCK(inp);
455 cred_can_cont:
456 	error = cr_canseesocket(req->td->td_ucred, inp->sctp_socket);
457 	if (error) {
458 		SCTP_INP_WUNLOCK(inp);
459 		goto out;
460 	}
461 	cru2x(inp->sctp_socket->so_cred, &xuc);
462 	SCTP_INP_WUNLOCK(inp);
463 	error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
464 out:
465 	return (error);
466 }
467 
468 SYSCTL_PROC(_net_inet6_sctp6, OID_AUTO, getcred, CTLTYPE_OPAQUE | CTLFLAG_RW,
469     0, 0,
470     sctp6_getcred, "S,ucred", "Get the ucred of a SCTP6 connection");
471 
472 
473 /* This is the same as the sctp_abort() could be made common */
474 static void
475 sctp6_abort(struct socket *so)
476 {
477 	struct sctp_inpcb *inp;
478 	uint32_t flags;
479 
480 	inp = (struct sctp_inpcb *)so->so_pcb;
481 	if (inp == NULL) {
482 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
483 		return;
484 	}
485 sctp_must_try_again:
486 	flags = inp->sctp_flags;
487 #ifdef SCTP_LOG_CLOSING
488 	sctp_log_closing(inp, NULL, 17);
489 #endif
490 	if (((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
491 	    (atomic_cmpset_int(&inp->sctp_flags, flags, (flags | SCTP_PCB_FLAGS_SOCKET_GONE | SCTP_PCB_FLAGS_CLOSE_IP)))) {
492 #ifdef SCTP_LOG_CLOSING
493 		sctp_log_closing(inp, NULL, 16);
494 #endif
495 		sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
496 		    SCTP_CALLED_AFTER_CMPSET_OFCLOSE);
497 		SOCK_LOCK(so);
498 		SCTP_SB_CLEAR(so->so_snd);
499 		/*
500 		 * same for the rcv ones, they are only here for the
501 		 * accounting/select.
502 		 */
503 		SCTP_SB_CLEAR(so->so_rcv);
504 		/* Now null out the reference, we are completely detached. */
505 		so->so_pcb = NULL;
506 		SOCK_UNLOCK(so);
507 	} else {
508 		flags = inp->sctp_flags;
509 		if ((flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
510 			goto sctp_must_try_again;
511 		}
512 	}
513 	return;
514 }
515 
516 static int
517 sctp6_attach(struct socket *so, int proto SCTP_UNUSED, struct thread *p SCTP_UNUSED)
518 {
519 	int error;
520 	struct sctp_inpcb *inp;
521 	uint32_t vrf_id = SCTP_DEFAULT_VRFID;
522 
523 	inp = (struct sctp_inpcb *)so->so_pcb;
524 	if (inp != NULL) {
525 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
526 		return (EINVAL);
527 	}
528 
529 	if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
530 		error = SCTP_SORESERVE(so, SCTP_BASE_SYSCTL(sctp_sendspace), SCTP_BASE_SYSCTL(sctp_recvspace));
531 		if (error)
532 			return (error);
533 	}
534 	error = sctp_inpcb_alloc(so, vrf_id);
535 	if (error)
536 		return (error);
537 	inp = (struct sctp_inpcb *)so->so_pcb;
538 	SCTP_INP_WLOCK(inp);
539 	inp->sctp_flags |= SCTP_PCB_FLAGS_BOUND_V6;	/* I'm v6! */
540 
541 	inp->ip_inp.inp.inp_vflag |= INP_IPV6;
542 	inp->ip_inp.inp.in6p_hops = -1;	/* use kernel default */
543 	inp->ip_inp.inp.in6p_cksum = -1;	/* just to be sure */
544 #ifdef INET
545 	/*
546 	 * XXX: ugly!! IPv4 TTL initialization is necessary for an IPv6
547 	 * socket as well, because the socket may be bound to an IPv6
548 	 * wildcard address, which may match an IPv4-mapped IPv6 address.
549 	 */
550 	inp->ip_inp.inp.inp_ip_ttl = MODULE_GLOBAL(ip_defttl);
551 #endif
552 	SCTP_INP_WUNLOCK(inp);
553 	return (0);
554 }
555 
556 static int
557 sctp6_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
558 {
559 	struct sctp_inpcb *inp;
560 	int error;
561 	u_char vflagsav;
562 
563 	inp = (struct sctp_inpcb *)so->so_pcb;
564 	if (inp == NULL) {
565 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
566 		return (EINVAL);
567 	}
568 
569 	if (addr) {
570 		switch (addr->sa_family) {
571 #ifdef INET
572 		case AF_INET:
573 			if (addr->sa_len != sizeof(struct sockaddr_in)) {
574 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
575 				return (EINVAL);
576 			}
577 			break;
578 #endif
579 #ifdef INET6
580 		case AF_INET6:
581 			if (addr->sa_len != sizeof(struct sockaddr_in6)) {
582 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
583 				return (EINVAL);
584 			}
585 			break;
586 #endif
587 		default:
588 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
589 			return (EINVAL);
590 		}
591 	}
592 	vflagsav = inp->ip_inp.inp.inp_vflag;
593 	inp->ip_inp.inp.inp_vflag &= ~INP_IPV4;
594 	inp->ip_inp.inp.inp_vflag |= INP_IPV6;
595 	if ((addr != NULL) && (SCTP_IPV6_V6ONLY(inp) == 0)) {
596 		switch (addr->sa_family) {
597 #ifdef INET
598 		case AF_INET:
599 			/* binding v4 addr to v6 socket, so reset flags */
600 			inp->ip_inp.inp.inp_vflag |= INP_IPV4;
601 			inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
602 			break;
603 #endif
604 #ifdef INET6
605 		case AF_INET6:
606 			{
607 				struct sockaddr_in6 *sin6_p;
608 
609 				sin6_p = (struct sockaddr_in6 *)addr;
610 
611 				if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) {
612 					inp->ip_inp.inp.inp_vflag |= INP_IPV4;
613 				}
614 #ifdef INET
615 				if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
616 					struct sockaddr_in sin;
617 
618 					in6_sin6_2_sin(&sin, sin6_p);
619 					inp->ip_inp.inp.inp_vflag |= INP_IPV4;
620 					inp->ip_inp.inp.inp_vflag &= ~INP_IPV6;
621 					error = sctp_inpcb_bind(so, (struct sockaddr *)&sin, NULL, p);
622 					goto out;
623 				}
624 #endif
625 				break;
626 			}
627 #endif
628 		default:
629 			break;
630 		}
631 	} else if (addr != NULL) {
632 		struct sockaddr_in6 *sin6_p;
633 
634 		/* IPV6_V6ONLY socket */
635 #ifdef INET
636 		if (addr->sa_family == AF_INET) {
637 			/* can't bind v4 addr to v6 only socket! */
638 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
639 			error = EINVAL;
640 			goto out;
641 		}
642 #endif
643 		sin6_p = (struct sockaddr_in6 *)addr;
644 
645 		if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
646 			/* can't bind v4-mapped addrs either! */
647 			/* NOTE: we don't support SIIT */
648 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
649 			error = EINVAL;
650 			goto out;
651 		}
652 	}
653 	error = sctp_inpcb_bind(so, addr, NULL, p);
654 out:
655 	if (error != 0)
656 		inp->ip_inp.inp.inp_vflag = vflagsav;
657 	return (error);
658 }
659 
660 
661 static void
662 sctp6_close(struct socket *so)
663 {
664 	sctp_close(so);
665 }
666 
667 /* This could be made common with sctp_detach() since they are identical */
668 
669 static
670 int
671 sctp6_disconnect(struct socket *so)
672 {
673 	return (sctp_disconnect(so));
674 }
675 
676 
677 int
678 sctp_sendm(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
679     struct mbuf *control, struct thread *p);
680 
681 
682 static int
683 sctp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
684     struct mbuf *control, struct thread *p)
685 {
686 	struct sctp_inpcb *inp;
687 
688 #ifdef INET
689 	struct sockaddr_in6 *sin6;
690 #endif				/* INET */
691 	/* No SPL needed since sctp_output does this */
692 
693 	inp = (struct sctp_inpcb *)so->so_pcb;
694 	if (inp == NULL) {
695 		if (control) {
696 			SCTP_RELEASE_PKT(control);
697 			control = NULL;
698 		}
699 		SCTP_RELEASE_PKT(m);
700 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
701 		return (EINVAL);
702 	}
703 	/*
704 	 * For the TCP model we may get a NULL addr, if we are a connected
705 	 * socket thats ok.
706 	 */
707 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
708 	    (addr == NULL)) {
709 		goto connected_type;
710 	}
711 	if (addr == NULL) {
712 		SCTP_RELEASE_PKT(m);
713 		if (control) {
714 			SCTP_RELEASE_PKT(control);
715 			control = NULL;
716 		}
717 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EDESTADDRREQ);
718 		return (EDESTADDRREQ);
719 	}
720 #ifdef INET
721 	sin6 = (struct sockaddr_in6 *)addr;
722 	if (SCTP_IPV6_V6ONLY(inp)) {
723 		/*
724 		 * if IPV6_V6ONLY flag, we discard datagrams destined to a
725 		 * v4 addr or v4-mapped addr
726 		 */
727 		if (addr->sa_family == AF_INET) {
728 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
729 			return (EINVAL);
730 		}
731 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
732 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
733 			return (EINVAL);
734 		}
735 	}
736 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
737 		struct sockaddr_in sin;
738 
739 		/* convert v4-mapped into v4 addr and send */
740 		in6_sin6_2_sin(&sin, sin6);
741 		return (sctp_sendm(so, flags, m, (struct sockaddr *)&sin, control, p));
742 	}
743 #endif				/* INET */
744 connected_type:
745 	/* now what about control */
746 	if (control) {
747 		if (inp->control) {
748 			SCTP_PRINTF("huh? control set?\n");
749 			SCTP_RELEASE_PKT(inp->control);
750 			inp->control = NULL;
751 		}
752 		inp->control = control;
753 	}
754 	/* Place the data */
755 	if (inp->pkt) {
756 		SCTP_BUF_NEXT(inp->pkt_last) = m;
757 		inp->pkt_last = m;
758 	} else {
759 		inp->pkt_last = inp->pkt = m;
760 	}
761 	if (
762 	/* FreeBSD and MacOSX uses a flag passed */
763 	    ((flags & PRUS_MORETOCOME) == 0)
764 	    ) {
765 		/*
766 		 * note with the current version this code will only be used
767 		 * by OpenBSD, NetBSD and FreeBSD have methods for
768 		 * re-defining sosend() to use sctp_sosend().  One can
769 		 * optionaly switch back to this code (by changing back the
770 		 * defininitions but this is not advisable.
771 		 */
772 		int ret;
773 
774 		ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
775 		inp->pkt = NULL;
776 		inp->control = NULL;
777 		return (ret);
778 	} else {
779 		return (0);
780 	}
781 }
782 
783 static int
784 sctp6_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
785 {
786 	uint32_t vrf_id;
787 	int error = 0;
788 	struct sctp_inpcb *inp;
789 	struct sctp_tcb *stcb;
790 #ifdef INET
791 	struct sockaddr_in6 *sin6;
792 	union sctp_sockstore store;
793 #endif
794 
795 	inp = (struct sctp_inpcb *)so->so_pcb;
796 	if (inp == NULL) {
797 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
798 		return (ECONNRESET);	/* I made the same as TCP since we are
799 					 * not setup? */
800 	}
801 	if (addr == NULL) {
802 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
803 		return (EINVAL);
804 	}
805 	switch (addr->sa_family) {
806 #ifdef INET
807 	case AF_INET:
808 		if (addr->sa_len != sizeof(struct sockaddr_in)) {
809 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
810 			return (EINVAL);
811 		}
812 		break;
813 #endif
814 #ifdef INET6
815 	case AF_INET6:
816 		if (addr->sa_len != sizeof(struct sockaddr_in6)) {
817 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
818 			return (EINVAL);
819 		}
820 		break;
821 #endif
822 	default:
823 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
824 		return (EINVAL);
825 	}
826 
827 	vrf_id = inp->def_vrf_id;
828 	SCTP_ASOC_CREATE_LOCK(inp);
829 	SCTP_INP_RLOCK(inp);
830 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
831 	    SCTP_PCB_FLAGS_UNBOUND) {
832 		/* Bind a ephemeral port */
833 		SCTP_INP_RUNLOCK(inp);
834 		error = sctp6_bind(so, NULL, p);
835 		if (error) {
836 			SCTP_ASOC_CREATE_UNLOCK(inp);
837 
838 			return (error);
839 		}
840 		SCTP_INP_RLOCK(inp);
841 	}
842 	if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
843 	    (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
844 		/* We are already connected AND the TCP model */
845 		SCTP_INP_RUNLOCK(inp);
846 		SCTP_ASOC_CREATE_UNLOCK(inp);
847 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EADDRINUSE);
848 		return (EADDRINUSE);
849 	}
850 #ifdef INET
851 	sin6 = (struct sockaddr_in6 *)addr;
852 	if (SCTP_IPV6_V6ONLY(inp)) {
853 		/*
854 		 * if IPV6_V6ONLY flag, ignore connections destined to a v4
855 		 * addr or v4-mapped addr
856 		 */
857 		if (addr->sa_family == AF_INET) {
858 			SCTP_INP_RUNLOCK(inp);
859 			SCTP_ASOC_CREATE_UNLOCK(inp);
860 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
861 			return (EINVAL);
862 		}
863 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
864 			SCTP_INP_RUNLOCK(inp);
865 			SCTP_ASOC_CREATE_UNLOCK(inp);
866 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
867 			return (EINVAL);
868 		}
869 	}
870 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
871 		/* convert v4-mapped into v4 addr */
872 		in6_sin6_2_sin(&store.sin, sin6);
873 		addr = &store.sa;
874 	}
875 #endif				/* INET */
876 	/* Now do we connect? */
877 	if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
878 		stcb = LIST_FIRST(&inp->sctp_asoc_list);
879 		if (stcb) {
880 			SCTP_TCB_LOCK(stcb);
881 		}
882 		SCTP_INP_RUNLOCK(inp);
883 	} else {
884 		SCTP_INP_RUNLOCK(inp);
885 		SCTP_INP_WLOCK(inp);
886 		SCTP_INP_INCR_REF(inp);
887 		SCTP_INP_WUNLOCK(inp);
888 		stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
889 		if (stcb == NULL) {
890 			SCTP_INP_WLOCK(inp);
891 			SCTP_INP_DECR_REF(inp);
892 			SCTP_INP_WUNLOCK(inp);
893 		}
894 	}
895 
896 	if (stcb != NULL) {
897 		/* Already have or am bring up an association */
898 		SCTP_ASOC_CREATE_UNLOCK(inp);
899 		SCTP_TCB_UNLOCK(stcb);
900 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EALREADY);
901 		return (EALREADY);
902 	}
903 	/* We are GOOD to go */
904 	stcb = sctp_aloc_assoc(inp, addr, &error, 0, vrf_id,
905 	    inp->sctp_ep.pre_open_stream_count,
906 	    inp->sctp_ep.port, p,
907 	    SCTP_INITIALIZE_AUTH_PARAMS);
908 	SCTP_ASOC_CREATE_UNLOCK(inp);
909 	if (stcb == NULL) {
910 		/* Gak! no memory */
911 		return (error);
912 	}
913 	if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
914 		stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
915 		/* Set the connected flag so we can queue data */
916 		soisconnecting(so);
917 	}
918 	SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
919 	(void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
920 	sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
921 	SCTP_TCB_UNLOCK(stcb);
922 	return (error);
923 }
924 
925 static int
926 sctp6_getaddr(struct socket *so, struct sockaddr **addr)
927 {
928 	struct sockaddr_in6 *sin6;
929 	struct sctp_inpcb *inp;
930 	uint32_t vrf_id;
931 	struct sctp_ifa *sctp_ifa;
932 
933 	int error;
934 
935 	/*
936 	 * Do the malloc first in case it blocks.
937 	 */
938 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof(*sin6));
939 	if (sin6 == NULL)
940 		return (ENOMEM);
941 	sin6->sin6_family = AF_INET6;
942 	sin6->sin6_len = sizeof(*sin6);
943 
944 	inp = (struct sctp_inpcb *)so->so_pcb;
945 	if (inp == NULL) {
946 		SCTP_FREE_SONAME(sin6);
947 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
948 		return (ECONNRESET);
949 	}
950 	SCTP_INP_RLOCK(inp);
951 	sin6->sin6_port = inp->sctp_lport;
952 	if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
953 		/* For the bound all case you get back 0 */
954 		if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
955 			struct sctp_tcb *stcb;
956 			struct sockaddr_in6 *sin_a6;
957 			struct sctp_nets *net;
958 			int fnd;
959 
960 			stcb = LIST_FIRST(&inp->sctp_asoc_list);
961 			if (stcb == NULL) {
962 				SCTP_INP_RUNLOCK(inp);
963 				SCTP_FREE_SONAME(sin6);
964 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
965 				return (ENOENT);
966 			}
967 			fnd = 0;
968 			sin_a6 = NULL;
969 			TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
970 				sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
971 				if (sin_a6 == NULL)
972 					/* this will make coverity happy */
973 					continue;
974 
975 				if (sin_a6->sin6_family == AF_INET6) {
976 					fnd = 1;
977 					break;
978 				}
979 			}
980 			if ((!fnd) || (sin_a6 == NULL)) {
981 				/* punt */
982 				SCTP_INP_RUNLOCK(inp);
983 				SCTP_FREE_SONAME(sin6);
984 				SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
985 				return (ENOENT);
986 			}
987 			vrf_id = inp->def_vrf_id;
988 			sctp_ifa = sctp_source_address_selection(inp, stcb, (sctp_route_t *)&net->ro, net, 0, vrf_id);
989 			if (sctp_ifa) {
990 				sin6->sin6_addr = sctp_ifa->address.sin6.sin6_addr;
991 			}
992 		} else {
993 			/* For the bound all case you get back 0 */
994 			memset(&sin6->sin6_addr, 0, sizeof(sin6->sin6_addr));
995 		}
996 	} else {
997 		/* Take the first IPv6 address in the list */
998 		struct sctp_laddr *laddr;
999 		int fnd = 0;
1000 
1001 		LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1002 			if (laddr->ifa->address.sa.sa_family == AF_INET6) {
1003 				struct sockaddr_in6 *sin_a;
1004 
1005 				sin_a = &laddr->ifa->address.sin6;
1006 				sin6->sin6_addr = sin_a->sin6_addr;
1007 				fnd = 1;
1008 				break;
1009 			}
1010 		}
1011 		if (!fnd) {
1012 			SCTP_FREE_SONAME(sin6);
1013 			SCTP_INP_RUNLOCK(inp);
1014 			SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1015 			return (ENOENT);
1016 		}
1017 	}
1018 	SCTP_INP_RUNLOCK(inp);
1019 	/* Scoping things for v6 */
1020 	if ((error = sa6_recoverscope(sin6)) != 0) {
1021 		SCTP_FREE_SONAME(sin6);
1022 		return (error);
1023 	}
1024 	(*addr) = (struct sockaddr *)sin6;
1025 	return (0);
1026 }
1027 
1028 static int
1029 sctp6_peeraddr(struct socket *so, struct sockaddr **addr)
1030 {
1031 	struct sockaddr_in6 *sin6;
1032 	int fnd;
1033 	struct sockaddr_in6 *sin_a6;
1034 	struct sctp_inpcb *inp;
1035 	struct sctp_tcb *stcb;
1036 	struct sctp_nets *net;
1037 	int error;
1038 
1039 	/* Do the malloc first in case it blocks. */
1040 	SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1041 	if (sin6 == NULL)
1042 		return (ENOMEM);
1043 	sin6->sin6_family = AF_INET6;
1044 	sin6->sin6_len = sizeof(*sin6);
1045 
1046 	inp = (struct sctp_inpcb *)so->so_pcb;
1047 	if ((inp == NULL) ||
1048 	    ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
1049 		/* UDP type and listeners will drop out here */
1050 		SCTP_FREE_SONAME(sin6);
1051 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOTCONN);
1052 		return (ENOTCONN);
1053 	}
1054 	SCTP_INP_RLOCK(inp);
1055 	stcb = LIST_FIRST(&inp->sctp_asoc_list);
1056 	if (stcb) {
1057 		SCTP_TCB_LOCK(stcb);
1058 	}
1059 	SCTP_INP_RUNLOCK(inp);
1060 	if (stcb == NULL) {
1061 		SCTP_FREE_SONAME(sin6);
1062 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ECONNRESET);
1063 		return (ECONNRESET);
1064 	}
1065 	fnd = 0;
1066 	TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
1067 		sin_a6 = (struct sockaddr_in6 *)&net->ro._l_addr;
1068 		if (sin_a6->sin6_family == AF_INET6) {
1069 			fnd = 1;
1070 			sin6->sin6_port = stcb->rport;
1071 			sin6->sin6_addr = sin_a6->sin6_addr;
1072 			break;
1073 		}
1074 	}
1075 	SCTP_TCB_UNLOCK(stcb);
1076 	if (!fnd) {
1077 		/* No IPv4 address */
1078 		SCTP_FREE_SONAME(sin6);
1079 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, ENOENT);
1080 		return (ENOENT);
1081 	}
1082 	if ((error = sa6_recoverscope(sin6)) != 0) {
1083 		SCTP_FREE_SONAME(sin6);
1084 		SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, error);
1085 		return (error);
1086 	}
1087 	*addr = (struct sockaddr *)sin6;
1088 	return (0);
1089 }
1090 
1091 static int
1092 sctp6_in6getaddr(struct socket *so, struct sockaddr **nam)
1093 {
1094 	struct inpcb *inp = sotoinpcb(so);
1095 	int error;
1096 
1097 	if (inp == NULL) {
1098 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1099 		return (EINVAL);
1100 	}
1101 
1102 	/* allow v6 addresses precedence */
1103 	error = sctp6_getaddr(so, nam);
1104 #ifdef INET
1105 	if (error) {
1106 		struct sockaddr_in6 *sin6;
1107 
1108 		/* try v4 next if v6 failed */
1109 		error = sctp_ingetaddr(so, nam);
1110 		if (error) {
1111 			return (error);
1112 		}
1113 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1114 		if (sin6 == NULL) {
1115 			SCTP_FREE_SONAME(*nam);
1116 			return (ENOMEM);
1117 		}
1118 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1119 		SCTP_FREE_SONAME(*nam);
1120 		*nam = (struct sockaddr *)sin6;
1121 	}
1122 #endif
1123 	return (error);
1124 }
1125 
1126 
1127 static int
1128 sctp6_getpeeraddr(struct socket *so, struct sockaddr **nam)
1129 {
1130 	struct inpcb *inp = sotoinpcb(so);
1131 	int error;
1132 
1133 	if (inp == NULL) {
1134 		SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP6_USRREQ, EINVAL);
1135 		return (EINVAL);
1136 	}
1137 
1138 	/* allow v6 addresses precedence */
1139 	error = sctp6_peeraddr(so, nam);
1140 #ifdef INET
1141 	if (error) {
1142 		struct sockaddr_in6 *sin6;
1143 
1144 		/* try v4 next if v6 failed */
1145 		error = sctp_peeraddr(so, nam);
1146 		if (error) {
1147 			return (error);
1148 		}
1149 		SCTP_MALLOC_SONAME(sin6, struct sockaddr_in6 *, sizeof *sin6);
1150 		if (sin6 == NULL) {
1151 			SCTP_FREE_SONAME(*nam);
1152 			return (ENOMEM);
1153 		}
1154 		in6_sin_2_v4mapsin6((struct sockaddr_in *)*nam, sin6);
1155 		SCTP_FREE_SONAME(*nam);
1156 		*nam = (struct sockaddr *)sin6;
1157 	}
1158 #endif
1159 	return (error);
1160 }
1161 
1162 struct pr_usrreqs sctp6_usrreqs = {
1163 	.pru_abort = sctp6_abort,
1164 	.pru_accept = sctp_accept,
1165 	.pru_attach = sctp6_attach,
1166 	.pru_bind = sctp6_bind,
1167 	.pru_connect = sctp6_connect,
1168 	.pru_control = in6_control,
1169 	.pru_close = sctp6_close,
1170 	.pru_detach = sctp6_close,
1171 	.pru_sopoll = sopoll_generic,
1172 	.pru_flush = sctp_flush,
1173 	.pru_disconnect = sctp6_disconnect,
1174 	.pru_listen = sctp_listen,
1175 	.pru_peeraddr = sctp6_getpeeraddr,
1176 	.pru_send = sctp6_send,
1177 	.pru_shutdown = sctp_shutdown,
1178 	.pru_sockaddr = sctp6_in6getaddr,
1179 	.pru_sosend = sctp_sosend,
1180 	.pru_soreceive = sctp_soreceive
1181 };
1182 
1183 #endif
1184