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