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