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