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