merged in FreeBSD kmod.mk linking approach, mainly for amd64
[dragonfly/port-amd64.git] / sys / netinet / sctp_usrreq.c
blobc281308ee8036d301ab9229227c04ff4e6187049
1 /* $KAME: sctp_usrreq.c,v 1.47 2005/03/06 16:04:18 itojun Exp $ */
2 /* $DragonFly: src/sys/netinet/sctp_usrreq.c,v 1.12 2007/04/22 01:13:14 dillon Exp $ */
4 /*
5 * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Cisco Systems, Inc.
19 * 4. Neither the name of the project nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #if !(defined(__OpenBSD__) || defined(__APPLE__))
36 #include "opt_ipsec.h"
37 #endif
38 #if defined(__FreeBSD__) || defined(__DragonFly__)
39 #include "opt_inet6.h"
40 #include "opt_inet.h"
41 #endif
42 #if defined(__NetBSD__)
43 #include "opt_inet.h"
44 #endif
46 #ifdef __APPLE__
47 #include <sctp.h>
48 #elif !defined(__OpenBSD__)
49 #include "opt_sctp.h"
50 #endif
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/domain.h>
58 #include <sys/proc.h>
59 #include <sys/protosw.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sysctl.h>
63 #include <sys/syslog.h>
64 #include <sys/thread2.h>
65 #include <net/if.h>
66 #include <net/if_types.h>
67 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
68 #include <net/if_var.h>
69 #endif
70 #include <net/route.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/ip.h>
74 #include <netinet/ip6.h>
75 #include <netinet/in_pcb.h>
76 #include <netinet/in_var.h>
77 #include <netinet/ip_var.h>
78 #include <netinet6/ip6_var.h>
79 #include <netinet6/in6_var.h>
81 #include <netinet/ip_icmp.h>
82 #include <netinet/icmp_var.h>
83 #include <netinet/sctp_pcb.h>
84 #include <netinet/sctp_header.h>
85 #include <netinet/sctp_var.h>
86 #include <netinet/sctp_output.h>
87 #include <netinet/sctp_uio.h>
88 #include <netinet/sctp_asconf.h>
89 #include <netinet/sctputil.h>
90 #include <netinet/sctp_indata.h>
91 #include <netinet/sctp_asconf.h>
92 #ifdef IPSEC
93 #ifndef __OpenBSD__
94 #include <netinet6/ipsec.h>
95 #include <netproto/key/key.h>
96 #else
97 #undef IPSEC
98 #endif
99 #endif /* IPSEC */
101 #include <net/net_osdep.h>
103 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__) || defined(__DragonFly__)
104 #ifndef in6pcb
105 #define in6pcb inpcb
106 #endif
107 #ifndef sotoin6pcb
108 #define sotoin6pcb sotoinpcb
109 #endif
110 #endif
112 #ifdef SCTP_DEBUG
113 extern u_int32_t sctp_debug_on;
114 #endif /* SCTP_DEBUG */
117 * sysctl tunable variables
119 int sctp_auto_asconf = SCTP_DEFAULT_AUTO_ASCONF;
120 int sctp_max_burst_default = SCTP_DEF_MAX_BURST;
121 int sctp_peer_chunk_oh = sizeof(struct mbuf);
122 int sctp_strict_init = 1;
123 int sctp_no_csum_on_loopback = 1;
124 unsigned int sctp_max_chunks_on_queue = SCTP_ASOC_MAX_CHUNKS_ON_QUEUE;
125 int sctp_sendspace = (128 * 1024);
126 int sctp_recvspace = 128 * (1024 +
127 #ifdef INET6
128 sizeof(struct sockaddr_in6)
129 #else
130 sizeof(struct sockaddr_in)
131 #endif
133 int sctp_strict_sacks = 0;
134 int sctp_ecn = 1;
135 int sctp_ecn_nonce = 0;
137 unsigned int sctp_delayed_sack_time_default = SCTP_RECV_MSEC;
138 unsigned int sctp_heartbeat_interval_default = SCTP_HB_DEFAULT_MSEC;
139 unsigned int sctp_pmtu_raise_time_default = SCTP_DEF_PMTU_RAISE_SEC;
140 unsigned int sctp_shutdown_guard_time_default = SCTP_DEF_MAX_SHUTDOWN_SEC;
141 unsigned int sctp_secret_lifetime_default = SCTP_DEFAULT_SECRET_LIFE_SEC;
142 unsigned int sctp_rto_max_default = SCTP_RTO_UPPER_BOUND;
143 unsigned int sctp_rto_min_default = SCTP_RTO_LOWER_BOUND;
144 unsigned int sctp_rto_initial_default = SCTP_RTO_INITIAL;
145 unsigned int sctp_init_rto_max_default = SCTP_RTO_UPPER_BOUND;
146 unsigned int sctp_valid_cookie_life_default = SCTP_DEFAULT_COOKIE_LIFE;
147 unsigned int sctp_init_rtx_max_default = SCTP_DEF_MAX_INIT;
148 unsigned int sctp_assoc_rtx_max_default = SCTP_DEF_MAX_SEND;
149 unsigned int sctp_path_rtx_max_default = SCTP_DEF_MAX_SEND/2;
150 unsigned int sctp_nr_outgoing_streams_default = SCTP_OSTREAM_INITIAL;
152 void
153 sctp_init(void)
155 #ifdef __OpenBSD__
156 #define nmbclusters nmbclust
157 #endif
158 /* Init the SCTP pcb in sctp_pcb.c */
159 u_long sb_max_adj;
161 sctp_pcb_init();
163 #ifndef __OpenBSD__
164 if (nmbclusters > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
165 sctp_max_chunks_on_queue = nmbclusters;
166 #else
167 /* if (nmbclust > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
168 sctp_max_chunks_on_queue = nmbclust; FIX ME */
169 sctp_max_chunks_on_queue = nmbclust * 2;
170 #endif
172 * Allow a user to take no more than 1/2 the number of clusters
173 * or the SB_MAX whichever is smaller for the send window.
175 sb_max_adj = (u_long)((u_quad_t)(SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
176 sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
177 #ifndef __OpenBSD__
178 ((nmbclusters/2) * SCTP_DEFAULT_MAXSEGMENT));
179 #else
180 ((nmbclust/2) * SCTP_DEFAULT_MAXSEGMENT));
181 #endif
183 * Now for the recv window, should we take the same amount?
184 * or should I do 1/2 the SB_MAX instead in the SB_MAX min above.
185 * For now I will just copy.
187 sctp_recvspace = sctp_sendspace;
188 #ifdef __OpenBSD__
189 #undef nmbclusters
190 #endif
193 #ifdef INET6
194 void
195 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
197 bzero(ip6, sizeof(*ip6));
199 ip6->ip6_vfc = IPV6_VERSION;
200 ip6->ip6_plen = ip->ip_len;
201 ip6->ip6_nxt = ip->ip_p;
202 ip6->ip6_hlim = ip->ip_ttl;
203 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
204 IPV6_ADDR_INT32_SMP;
205 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
206 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
208 #endif /* INET6 */
210 static void
211 sctp_split_chunks(struct sctp_association *asoc,
212 struct sctp_stream_out *strm,
213 struct sctp_tmit_chunk *chk)
215 struct sctp_tmit_chunk *new_chk;
217 /* First we need a chunk */
218 new_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
219 if (new_chk == NULL) {
220 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
221 return;
223 sctppcbinfo.ipi_count_chunk++;
224 sctppcbinfo.ipi_gencnt_chunk++;
225 /* Copy it all */
226 *new_chk = *chk;
227 /* split the data */
228 new_chk->data = m_split(chk->data, (chk->send_size>>1), MB_DONTWAIT);
229 if (new_chk->data == NULL) {
230 /* Can't split */
231 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
232 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, new_chk);
233 sctppcbinfo.ipi_count_chunk--;
234 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
235 panic("Chunk count is negative");
237 sctppcbinfo.ipi_gencnt_chunk++;
238 return;
241 /* Data is now split adjust sizes */
242 chk->send_size >>= 1;
243 new_chk->send_size >>= 1;
245 chk->book_size >>= 1;
246 new_chk->book_size >>= 1;
248 /* now adjust the marks */
249 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
250 chk->rec.data.rcv_flags &= ~SCTP_DATA_LAST_FRAG;
252 new_chk->rec.data.rcv_flags &= ~SCTP_DATA_FIRST_FRAG;
253 new_chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
255 /* Increase ref count if dest is set */
256 if (chk->whoTo) {
257 new_chk->whoTo->ref_count++;
259 /* now drop it on the end of the list*/
260 asoc->stream_queue_cnt++;
261 TAILQ_INSERT_AFTER(&strm->outqueue, chk, new_chk, sctp_next);
264 static void
265 sctp_notify_mbuf(struct sctp_inpcb *inp,
266 struct sctp_tcb *stcb,
267 struct sctp_nets *net,
268 struct ip *ip,
269 struct sctphdr *sh)
272 struct icmp *icmph;
273 int totsz;
274 uint16_t nxtsz;
276 /* protection */
277 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
278 (ip == NULL) || (sh == NULL)) {
279 if (stcb != NULL)
280 SCTP_TCB_UNLOCK(stcb);
281 return;
283 /* First job is to verify the vtag matches what I would send */
284 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
285 SCTP_TCB_UNLOCK(stcb);
286 return;
288 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
289 sizeof(struct ip)));
290 if (icmph->icmp_type != ICMP_UNREACH) {
291 /* We only care about unreachable */
292 SCTP_TCB_UNLOCK(stcb);
293 return;
295 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
296 /* not a unreachable message due to frag. */
297 SCTP_TCB_UNLOCK(stcb);
298 return;
300 totsz = ip->ip_len;
301 nxtsz = ntohs(icmph->icmp_seq);
302 if (nxtsz == 0) {
304 * old type router that does not tell us what the next size
305 * mtu is. Rats we will have to guess (in a educated fashion
306 * of course)
308 nxtsz = find_next_best_mtu(totsz);
311 /* Stop any PMTU timer */
312 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
314 /* Adjust destination size limit */
315 if (net->mtu > nxtsz) {
316 net->mtu = nxtsz;
318 /* now what about the ep? */
319 if (stcb->asoc.smallest_mtu > nxtsz) {
320 struct sctp_tmit_chunk *chk, *nchk;
321 struct sctp_stream_out *strm;
322 /* Adjust that too */
323 stcb->asoc.smallest_mtu = nxtsz;
324 /* now off to subtract IP_DF flag if needed */
326 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
327 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
328 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
331 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
332 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
334 * For this guy we also mark for immediate
335 * resend since we sent to big of chunk
337 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
338 if (chk->sent != SCTP_DATAGRAM_RESEND) {
339 stcb->asoc.sent_queue_retran_cnt++;
341 chk->sent = SCTP_DATAGRAM_RESEND;
342 chk->rec.data.doing_fast_retransmit = 0;
344 /* Clear any time so NO RTT is being done */
345 chk->do_rtt = 0;
346 stcb->asoc.total_flight -= chk->book_size;
347 if (stcb->asoc.total_flight < 0) {
348 stcb->asoc.total_flight = 0;
350 stcb->asoc.total_flight_count--;
351 if (stcb->asoc.total_flight_count < 0) {
352 stcb->asoc.total_flight_count = 0;
354 net->flight_size -= chk->book_size;
355 if (net->flight_size < 0) {
356 net->flight_size = 0;
360 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
361 chk = TAILQ_FIRST(&strm->outqueue);
362 while (chk) {
363 nchk = TAILQ_NEXT(chk, sctp_next);
364 if ((chk->send_size+SCTP_MED_OVERHEAD) > nxtsz) {
365 sctp_split_chunks(&stcb->asoc, strm, chk);
367 chk = nchk;
371 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
372 SCTP_TCB_UNLOCK(stcb);
376 void
377 sctp_notify(struct sctp_inpcb *inp,
378 int error,
379 struct sctphdr *sh,
380 struct sockaddr *to,
381 struct sctp_tcb *stcb,
382 struct sctp_nets *net)
384 /* protection */
385 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
386 (sh == NULL) || (to == NULL)) {
387 #ifdef SCTP_DEBUG
388 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
389 kprintf("sctp-notify, bad call\n");
391 #endif /* SCTP_DEBUG */
392 return;
394 /* First job is to verify the vtag matches what I would send */
395 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
396 return;
399 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
401 if ((error == EHOSTUNREACH) || /* Host is not reachable */
402 (error == EHOSTDOWN) || /* Host is down */
403 (error == ECONNREFUSED) || /* Host refused the connection, (not an abort?) */
404 (error == ENOPROTOOPT) /* SCTP is not present on host */
407 * Hmm reachablity problems we must examine closely.
408 * If its not reachable, we may have lost a network.
409 * Or if there is NO protocol at the other end named SCTP.
410 * well we consider it a OOTB abort.
412 if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
413 if (net->dest_state & SCTP_ADDR_REACHABLE) {
414 /* Ok that destination is NOT reachable */
415 net->dest_state &= ~SCTP_ADDR_REACHABLE;
416 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
417 net->error_count = net->failure_threshold + 1;
418 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
419 stcb, SCTP_FAILED_THRESHOLD,
420 (void *)net);
422 if (stcb)
423 SCTP_TCB_UNLOCK(stcb);
424 } else {
426 * Here the peer is either playing tricks on us,
427 * including an address that belongs to someone who
428 * does not support SCTP OR was a userland
429 * implementation that shutdown and now is dead. In
430 * either case treat it like a OOTB abort with no TCB
432 sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
433 sctp_free_assoc(inp, stcb);
434 /* no need to unlock here, since the TCB is gone */
436 } else {
437 /* Send all others to the app */
438 if (inp->sctp_socket) {
439 SOCK_LOCK(inp->sctp_socket);
440 inp->sctp_socket->so_error = error;
441 sctp_sowwakeup(inp, inp->sctp_socket);
442 SOCK_UNLOCK(inp->sctp_socket);
444 if (stcb)
445 SCTP_TCB_UNLOCK(stcb);
449 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
450 void
451 #else
452 void *
453 #endif
454 sctp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
456 struct ip *ip = vip;
457 struct sctphdr *sh;
460 if (sa->sa_family != AF_INET ||
461 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
462 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
463 return;
464 #else
465 return (NULL);
466 #endif
469 if (PRC_IS_REDIRECT(cmd)) {
470 ip = 0;
471 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
472 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
473 return;
474 #else
475 return (NULL);
476 #endif
478 if (ip) {
479 struct sctp_inpcb *inp;
480 struct sctp_tcb *stcb;
481 struct sctp_nets *net;
482 struct sockaddr_in to, from;
484 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
485 bzero(&to, sizeof(to));
486 bzero(&from, sizeof(from));
487 from.sin_family = to.sin_family = AF_INET;
488 from.sin_len = to.sin_len = sizeof(to);
489 from.sin_port = sh->src_port;
490 from.sin_addr = ip->ip_src;
491 to.sin_port = sh->dest_port;
492 to.sin_addr = ip->ip_dst;
495 * 'to' holds the dest of the packet that failed to be sent.
496 * 'from' holds our local endpoint address.
497 * Thus we reverse the to and the from in the lookup.
499 crit_enter();
500 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
501 (struct sockaddr *)&to,
502 &inp, &net, 1);
503 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
504 if (cmd != PRC_MSGSIZE) {
505 int cm;
506 if (cmd == PRC_HOSTDEAD) {
507 cm = EHOSTUNREACH;
508 } else {
509 cm = inetctlerrmap[cmd];
511 sctp_notify(inp, cm, sh,
512 (struct sockaddr *)&to, stcb,
513 net);
514 } else {
515 /* handle possible ICMP size messages */
516 sctp_notify_mbuf(inp, stcb, net, ip, sh);
518 } else {
519 #if (defined(__FreeBSD__) && __FreeBSD_version < 500000) || defined(__DragonFly__)
520 /* XXX must be fixed for 5.x and higher, leave for 4.x */
521 if (PRC_IS_REDIRECT(cmd) && inp) {
522 in_rtchange((struct inpcb *)inp,
523 inetctlerrmap[cmd]);
525 #endif
526 if ((stcb == NULL) && (inp != NULL)) {
527 /* reduce ref-count */
528 SCTP_INP_WLOCK(inp);
529 SCTP_INP_DECR_REF(inp);
530 SCTP_INP_WUNLOCK(inp);
534 crit_exit();
536 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
537 return;
538 #else
539 return (NULL);
540 #endif
543 #if defined(__FreeBSD__) || defined(__DragonFly__)
544 static int
545 sctp_getcred(SYSCTL_HANDLER_ARGS)
547 struct sockaddr_in addrs[2];
548 struct sctp_inpcb *inp;
549 struct sctp_nets *net;
550 struct sctp_tcb *stcb;
551 int error;
553 #if __FreeBSD_version >= 500000 || defined(__DragonFly__)
554 error = suser(req->td);
555 #else
556 error = suser(req->p);
557 #endif
558 if (error)
559 return (error);
560 error = SYSCTL_IN(req, addrs, sizeof(addrs));
561 if (error)
562 return (error);
564 crit_enter();
565 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
566 sintosa(&addrs[1]),
567 &inp, &net, 1);
568 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
569 if ((inp != NULL) && (stcb == NULL)) {
570 /* reduce ref-count */
571 SCTP_INP_WLOCK(inp);
572 SCTP_INP_DECR_REF(inp);
573 SCTP_INP_WUNLOCK(inp);
575 error = ENOENT;
576 goto out;
578 error = SYSCTL_OUT(req, inp->sctp_socket->so_cred, sizeof(struct ucred));
579 SCTP_TCB_UNLOCK(stcb);
580 out:
581 crit_exit();
582 return (error);
585 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
586 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
587 #endif /* #if defined(__FreeBSD__) || defined(__DragonFly__) */
590 * sysctl definitions
592 #if defined(__FreeBSD__) || defined (__APPLE__) || defined(__DragonFly__)
594 SYSCTL_DECL(_net_inet);
596 SYSCTL_NODE(_net_inet, OID_AUTO, sctp, CTLFLAG_RD, 0,
597 "sctp values");
599 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxdgram, CTLFLAG_RW,
600 &sctp_sendspace, 0, "Maximum outgoing SCTP buffer size");
602 SYSCTL_INT(_net_inet_sctp, OID_AUTO, recvspace, CTLFLAG_RW,
603 &sctp_recvspace, 0, "Maximum incoming SCTP buffer size");
605 SYSCTL_INT(_net_inet_sctp, OID_AUTO, auto_asconf, CTLFLAG_RW,
606 &sctp_auto_asconf, 0, "Enable SCTP Auto-ASCONF");
608 SYSCTL_INT(_net_inet_sctp, OID_AUTO, ecn_enable, CTLFLAG_RW,
609 &sctp_ecn, 0, "Enable SCTP ECN");
611 SYSCTL_INT(_net_inet_sctp, OID_AUTO, ecn_nonce, CTLFLAG_RW,
612 &sctp_ecn_nonce, 0, "Enable SCTP ECN Nonce");
614 SYSCTL_INT(_net_inet_sctp, OID_AUTO, strict_sacks, CTLFLAG_RW,
615 &sctp_strict_sacks, 0, "Enable SCTP Strict SACK checking");
617 SYSCTL_INT(_net_inet_sctp, OID_AUTO, loopback_nocsum, CTLFLAG_RW,
618 &sctp_no_csum_on_loopback, 0,
619 "Enable NO Csum on packets sent on loopback");
621 SYSCTL_INT(_net_inet_sctp, OID_AUTO, strict_init, CTLFLAG_RW,
622 &sctp_strict_init, 0,
623 "Enable strict INIT/INIT-ACK singleton enforcement");
625 SYSCTL_INT(_net_inet_sctp, OID_AUTO, peer_chkoh, CTLFLAG_RW,
626 &sctp_peer_chunk_oh, 0,
627 "Amount to debit peers rwnd per chunk sent");
629 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxburst, CTLFLAG_RW,
630 &sctp_max_burst_default, 0,
631 "Default max burst for sctp endpoints");
633 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxchunks, CTLFLAG_RW,
634 &sctp_max_chunks_on_queue, 0,
635 "Default max chunks on queue per asoc");
637 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, delayed_sack_time, CTLFLAG_RW,
638 &sctp_delayed_sack_time_default, 0,
639 "Default delayed SACK timer in msec");
641 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, heartbeat_interval, CTLFLAG_RW,
642 &sctp_heartbeat_interval_default, 0,
643 "Default heartbeat interval in msec");
645 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, pmtu_raise_time, CTLFLAG_RW,
646 &sctp_pmtu_raise_time_default, 0,
647 "Default PMTU raise timer in sec");
649 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, shutdown_guard_time, CTLFLAG_RW,
650 &sctp_shutdown_guard_time_default, 0,
651 "Default shutdown guard timer in sec");
653 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, secret_lifetime, CTLFLAG_RW,
654 &sctp_secret_lifetime_default, 0,
655 "Default secret lifetime in sec");
657 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_max, CTLFLAG_RW,
658 &sctp_rto_max_default, 0,
659 "Default maximum retransmission timeout in msec");
661 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_min, CTLFLAG_RW,
662 &sctp_rto_min_default, 0,
663 "Default minimum retransmission timeout in msec");
665 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_initial, CTLFLAG_RW,
666 &sctp_rto_initial_default, 0,
667 "Default initial retransmission timeout in msec");
669 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, init_rto_max, CTLFLAG_RW,
670 &sctp_init_rto_max_default, 0,
671 "Default maximum retransmission timeout during association setup in msec");
673 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, valid_cookie_life, CTLFLAG_RW,
674 &sctp_valid_cookie_life_default, 0,
675 "Default cookie lifetime in sec");
677 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, init_rtx_max, CTLFLAG_RW,
678 &sctp_init_rtx_max_default, 0,
679 "Default maximum number of retransmission for INIT chunks");
681 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, assoc_rtx_max, CTLFLAG_RW,
682 &sctp_assoc_rtx_max_default, 0,
683 "Default maximum number of retransmissions per association");
685 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, path_rtx_max, CTLFLAG_RW,
686 &sctp_path_rtx_max_default, 0,
687 "Default maximum of retransmissions per path");
689 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, nr_outgoing_streams, CTLFLAG_RW,
690 &sctp_nr_outgoing_streams_default, 0,
691 "Default number of outgoing streams");
693 #ifdef SCTP_DEBUG
694 SYSCTL_INT(_net_inet_sctp, OID_AUTO, debug, CTLFLAG_RW,
695 &sctp_debug_on, 0, "Configure debug output");
696 #endif /* SCTP_DEBUG */
697 #endif
699 static int
700 sctp_abort(struct socket *so)
702 struct sctp_inpcb *inp;
704 inp = (struct sctp_inpcb *)so->so_pcb;
705 if (inp == 0)
706 return EINVAL; /* ??? possible? panic instead? */
708 crit_enter();
709 sctp_inpcb_free(inp, 1);
710 crit_exit();
711 return 0;
714 static int
715 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
716 sctp_attach(struct socket *so, int proto, struct thread *p)
717 #elif defined(__DragonFly__)
718 sctp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
719 #else
720 sctp_attach(struct socket *so, int proto, struct proc *p)
721 #endif
723 struct sctp_inpcb *inp;
724 struct inpcb *ip_inp;
725 int error;
727 crit_enter();
728 inp = (struct sctp_inpcb *)so->so_pcb;
729 if (inp != 0) {
730 crit_exit();
731 return EINVAL;
733 error = soreserve(so, sctp_sendspace, sctp_recvspace, NULL);
734 if (error) {
735 crit_exit();
736 return error;
738 error = sctp_inpcb_alloc(so);
739 if (error) {
740 crit_exit();
741 return error;
743 inp = (struct sctp_inpcb *)so->so_pcb;
744 SCTP_INP_WLOCK(inp);
746 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
747 ip_inp = &inp->ip_inp.inp;
748 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
749 ip_inp->inp_vflag |= INP_IPV4;
750 ip_inp->inp_ip_ttl = ip_defttl;
751 #else
752 inp->inp_vflag |= INP_IPV4;
753 inp->inp_ip_ttl = ip_defttl;
754 #endif
756 #ifdef IPSEC
757 #if !(defined(__OpenBSD__) || defined(__APPLE__))
758 error = ipsec_init_policy(so, &ip_inp->inp_sp);
759 if (error != 0) {
760 sctp_inpcb_free(inp, 1);
761 return error;
763 #endif
764 #endif /*IPSEC*/
765 SCTP_INP_WUNLOCK(inp);
766 #if defined(__NetBSD__)
767 so->so_send = sctp_sosend;
768 #endif
769 crit_exit();
770 return 0;
773 static int
774 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
775 sctp_bind(struct socket *so, struct sockaddr *addr, struct thread *p)
777 #elif defined(__FreeBSD__) || defined(__APPLE__)
778 sctp_bind(struct socket *so, struct sockaddr *addr, struct proc *p)
780 #else
781 sctp_bind(struct socket *so, struct mbuf *nam, struct proc *p)
783 struct sockaddr *addr = nam ? mtod(nam, struct sockaddr *) : NULL;
784 #endif
785 struct sctp_inpcb *inp;
786 int error;
788 #ifdef INET6
789 if (addr && addr->sa_family != AF_INET)
790 /* must be a v4 address! */
791 return EINVAL;
792 #endif /* INET6 */
794 inp = (struct sctp_inpcb *)so->so_pcb;
795 if (inp == 0)
796 return EINVAL;
798 crit_enter();
799 error = sctp_inpcb_bind(so, addr, p);
800 crit_exit();
801 return error;
805 static int
806 sctp_detach(struct socket *so)
808 struct sctp_inpcb *inp;
810 inp = (struct sctp_inpcb *)so->so_pcb;
811 if (inp == 0)
812 return EINVAL;
813 crit_enter();
814 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
815 (so->so_rcv.ssb_cc > 0)) {
816 sctp_inpcb_free(inp, 1);
817 } else {
818 sctp_inpcb_free(inp, 0);
820 crit_exit();
821 return 0;
825 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
826 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
827 struct mbuf *control, struct thread *p);
828 #else
829 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
830 struct mbuf *control, struct proc *p);
831 #endif
834 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
835 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
836 struct mbuf *control, struct thread *p)
838 #else
839 sctp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
840 struct mbuf *control, struct proc *p)
842 #endif
843 struct sctp_inpcb *inp;
844 int error;
845 inp = (struct sctp_inpcb *)so->so_pcb;
846 if (inp == 0) {
847 if (control) {
848 sctp_m_freem(control);
849 control = NULL;
851 sctp_m_freem(m);
852 return EINVAL;
854 /* Got to have an to address if we are NOT a connected socket */
855 if ((addr == NULL) &&
856 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
857 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
859 goto connected_type;
860 } else if (addr == NULL) {
861 error = EDESTADDRREQ;
862 sctp_m_freem(m);
863 if (control) {
864 sctp_m_freem(control);
865 control = NULL;
867 return (error);
869 #ifdef INET6
870 if (addr->sa_family != AF_INET) {
871 /* must be a v4 address! */
872 sctp_m_freem(m);
873 if (control) {
874 sctp_m_freem(control);
875 control = NULL;
877 error = EDESTADDRREQ;
878 return EINVAL;
880 #endif /* INET6 */
881 connected_type:
882 /* now what about control */
883 if (control) {
884 if (inp->control) {
885 kprintf("huh? control set?\n");
886 sctp_m_freem(inp->control);
887 inp->control = NULL;
889 inp->control = control;
891 /* add it in possibly */
892 if ((inp->pkt) && (inp->pkt->m_flags & M_PKTHDR)) {
893 struct mbuf *x;
894 int c_len;
896 c_len = 0;
897 /* How big is it */
898 for (x=m;x;x = x->m_next) {
899 c_len += x->m_len;
901 inp->pkt->m_pkthdr.len += c_len;
903 /* Place the data */
904 if (inp->pkt) {
905 inp->pkt_last->m_next = m;
906 inp->pkt_last = m;
907 } else {
908 inp->pkt_last = inp->pkt = m;
910 if (
911 #if defined (__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
912 /* FreeBSD uses a flag passed */
913 ((flags & PRUS_MORETOCOME) == 0)
914 #elif defined( __NetBSD__)
915 /* NetBSD uses the so_state field */
916 ((so->so_state & SS_MORETOCOME) == 0)
917 #else
918 1 /* Open BSD does not have any "more to come" indication */
919 #endif
922 * note with the current version this code will only be used
923 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
924 * re-defining sosend to use the sctp_sosend. One can
925 * optionally switch back to this code (by changing back the
926 * definitions) but this is not advisable.
928 int ret;
929 ret = sctp_output(inp, inp->pkt, addr, inp->control, p, flags);
930 inp->pkt = NULL;
931 inp->control = NULL;
932 return (ret);
933 } else {
934 return (0);
938 static int
939 sctp_disconnect(struct socket *so)
941 struct sctp_inpcb *inp;
943 crit_enter();
944 inp = (struct sctp_inpcb *)so->so_pcb;
945 if (inp == NULL) {
946 crit_exit();
947 return (ENOTCONN);
949 SCTP_INP_RLOCK(inp);
950 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
951 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
952 /* No connection */
953 crit_exit();
954 SCTP_INP_RUNLOCK(inp);
955 return (0);
956 } else {
957 int some_on_streamwheel = 0;
958 struct sctp_association *asoc;
959 struct sctp_tcb *stcb;
961 stcb = LIST_FIRST(&inp->sctp_asoc_list);
962 if (stcb == NULL) {
963 crit_exit();
964 SCTP_INP_RUNLOCK(inp);
965 return (EINVAL);
967 asoc = &stcb->asoc;
968 SCTP_TCB_LOCK(stcb);
969 if (((so->so_options & SO_LINGER) &&
970 (so->so_linger == 0)) ||
971 (so->so_rcv.ssb_cc > 0)) {
972 if (SCTP_GET_STATE(asoc) !=
973 SCTP_STATE_COOKIE_WAIT) {
974 /* Left with Data unread */
975 struct mbuf *err;
976 err = NULL;
977 MGET(err, MB_DONTWAIT, MT_DATA);
978 if (err) {
979 /* Fill in the user initiated abort */
980 struct sctp_paramhdr *ph;
981 ph = mtod(err, struct sctp_paramhdr *);
982 err->m_len = sizeof(struct sctp_paramhdr);
983 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
984 ph->param_length = htons(err->m_len);
986 sctp_send_abort_tcb(stcb, err);
988 SCTP_INP_RUNLOCK(inp);
989 sctp_free_assoc(inp, stcb);
990 /* No unlock tcb assoc is gone */
991 crit_exit();
992 return (0);
994 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
995 /* Check to see if some data queued */
996 struct sctp_stream_out *outs;
997 TAILQ_FOREACH(outs, &asoc->out_wheel,
998 next_spoke) {
999 if (!TAILQ_EMPTY(&outs->outqueue)) {
1000 some_on_streamwheel = 1;
1001 break;
1006 if (TAILQ_EMPTY(&asoc->send_queue) &&
1007 TAILQ_EMPTY(&asoc->sent_queue) &&
1008 (some_on_streamwheel == 0)) {
1009 /* there is nothing queued to send, so done */
1010 if ((SCTP_GET_STATE(asoc) !=
1011 SCTP_STATE_SHUTDOWN_SENT) &&
1012 (SCTP_GET_STATE(asoc) !=
1013 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
1014 /* only send SHUTDOWN 1st time thru */
1015 #ifdef SCTP_DEBUG
1016 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1017 kprintf("%s:%d sends a shutdown\n",
1018 __FILE__,
1019 __LINE__
1022 #endif
1023 sctp_send_shutdown(stcb,
1024 stcb->asoc.primary_destination);
1025 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1026 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1027 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1028 stcb->sctp_ep, stcb,
1029 asoc->primary_destination);
1030 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1031 stcb->sctp_ep, stcb,
1032 asoc->primary_destination);
1034 } else {
1036 * we still got (or just got) data to send,
1037 * so set SHUTDOWN_PENDING
1040 * XXX sockets draft says that MSG_EOF should
1041 * be sent with no data.
1042 * currently, we will allow user data to be
1043 * sent first and move to SHUTDOWN-PENDING
1045 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1047 SCTP_TCB_UNLOCK(stcb);
1048 SCTP_INP_RUNLOCK(inp);
1049 crit_exit();
1050 return (0);
1052 /* not reached */
1053 } else {
1054 /* UDP model does not support this */
1055 SCTP_INP_RUNLOCK(inp);
1056 crit_exit();
1057 return EOPNOTSUPP;
1062 sctp_shutdown(struct socket *so)
1064 struct sctp_inpcb *inp;
1066 crit_enter();
1067 inp = (struct sctp_inpcb *)so->so_pcb;
1068 if (inp == 0) {
1069 crit_exit();
1070 return EINVAL;
1072 SCTP_INP_RLOCK(inp);
1073 /* For UDP model this is a invalid call */
1074 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
1075 /* Restore the flags that the soshutdown took away. */
1076 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
1077 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1078 #else
1079 so->so_state &= ~SS_CANTRCVMORE;
1080 #endif
1081 /* This proc will wakeup for read and do nothing (I hope) */
1082 crit_exit();
1083 SCTP_INP_RUNLOCK(inp);
1084 return (EOPNOTSUPP);
1087 * Ok if we reach here its the TCP model and it is either a SHUT_WR
1088 * or SHUT_RDWR. This means we put the shutdown flag against it.
1091 int some_on_streamwheel = 0;
1092 struct sctp_tcb *stcb;
1093 struct sctp_association *asoc;
1094 socantsendmore(so);
1096 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1097 if (stcb == NULL) {
1099 * Ok we hit the case that the shutdown call was made
1100 * after an abort or something. Nothing to do now.
1102 crit_exit();
1103 return (0);
1105 SCTP_TCB_LOCK(stcb);
1106 asoc = &stcb->asoc;
1108 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
1109 /* Check to see if some data queued */
1110 struct sctp_stream_out *outs;
1111 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
1112 if (!TAILQ_EMPTY(&outs->outqueue)) {
1113 some_on_streamwheel = 1;
1114 break;
1118 if (TAILQ_EMPTY(&asoc->send_queue) &&
1119 TAILQ_EMPTY(&asoc->sent_queue) &&
1120 (some_on_streamwheel == 0)) {
1121 /* there is nothing queued to send, so I'm done... */
1122 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1123 /* only send SHUTDOWN the first time through */
1124 #ifdef SCTP_DEBUG
1125 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1126 kprintf("%s:%d sends a shutdown\n",
1127 __FILE__,
1128 __LINE__
1131 #endif
1132 sctp_send_shutdown(stcb,
1133 stcb->asoc.primary_destination);
1134 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1135 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1136 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1137 stcb->sctp_ep, stcb,
1138 asoc->primary_destination);
1139 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1140 stcb->sctp_ep, stcb,
1141 asoc->primary_destination);
1143 } else {
1145 * we still got (or just got) data to send, so
1146 * set SHUTDOWN_PENDING
1148 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1150 SCTP_TCB_UNLOCK(stcb);
1152 SCTP_INP_RUNLOCK(inp);
1153 crit_exit();
1154 return 0;
1158 * copies a "user" presentable address and removes embedded scope, etc.
1159 * returns 0 on success, 1 on error
1161 static uint32_t
1162 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1164 struct sockaddr_in6 lsa6;
1165 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1166 &lsa6);
1167 memcpy(ss, sa, sa->sa_len);
1168 return (0);
1172 #if defined(__NetBSD__) || defined(__OpenBSD__)
1174 * On NetBSD and OpenBSD in6_sin_2_v4mapsin6() not used and not exported,
1175 * so we have to export it here.
1177 void in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6);
1178 #endif
1180 static int
1181 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1182 struct sctp_tcb *stcb,
1183 int limit,
1184 struct sockaddr_storage *sas)
1186 struct ifnet *ifn;
1187 struct ifaddr *ifa;
1188 int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual;
1189 int ipv4_addr_legal, ipv6_addr_legal;
1190 actual = 0;
1191 if (limit <= 0)
1192 return (actual);
1194 if (stcb) {
1195 /* Turn on all the appropriate scope */
1196 loopback_scope = stcb->asoc.loopback_scope;
1197 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1198 local_scope = stcb->asoc.local_scope;
1199 site_scope = stcb->asoc.site_scope;
1200 } else {
1201 /* Turn on ALL scope, since we look at the EP */
1202 loopback_scope = ipv4_local_scope = local_scope =
1203 site_scope = 1;
1205 ipv4_addr_legal = ipv6_addr_legal = 0;
1206 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1207 ipv6_addr_legal = 1;
1208 if (
1209 #if defined(__OpenBSD__)
1210 (0) /* we always do dual bind */
1211 #elif defined (__NetBSD__)
1212 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1213 #else
1214 (((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
1215 #endif
1216 == 0) {
1217 ipv4_addr_legal = 1;
1219 } else {
1220 ipv4_addr_legal = 1;
1223 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1224 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1225 if ((loopback_scope == 0) &&
1226 (ifn->if_type == IFT_LOOP)) {
1227 /* Skip loopback if loopback_scope not set */
1228 continue;
1230 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
1231 if (stcb) {
1233 * For the BOUND-ALL case, the list
1234 * associated with a TCB is Always
1235 * considered a reverse list.. i.e.
1236 * it lists addresses that are NOT
1237 * part of the association. If this
1238 * is one of those we must skip it.
1240 if (sctp_is_addr_restricted(stcb,
1241 ifa->ifa_addr)) {
1242 continue;
1245 if ((ifa->ifa_addr->sa_family == AF_INET) &&
1246 (ipv4_addr_legal)) {
1247 struct sockaddr_in *sin;
1248 sin = (struct sockaddr_in *)ifa->ifa_addr;
1249 if (sin->sin_addr.s_addr == 0) {
1250 /* we skip unspecifed addresses */
1251 continue;
1253 if ((ipv4_local_scope == 0) &&
1254 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1255 continue;
1257 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1258 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1259 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1260 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1261 actual += sizeof(sizeof(struct sockaddr_in6));
1262 } else {
1263 memcpy(sas, sin, sizeof(*sin));
1264 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1265 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1266 actual += sizeof(*sin);
1268 if (actual >= limit) {
1269 return (actual);
1271 } else if ((ifa->ifa_addr->sa_family == AF_INET6) &&
1272 (ipv6_addr_legal)) {
1273 struct sockaddr_in6 *sin6, lsa6;
1274 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1275 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1276 /* we skip unspecifed addresses */
1277 continue;
1279 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1280 if (local_scope == 0)
1281 continue;
1282 if (sin6->sin6_scope_id == 0) {
1283 lsa6 = *sin6;
1284 if (in6_recoverscope(&lsa6,
1285 &lsa6.sin6_addr,
1286 NULL))
1287 /* bad link local address */
1288 continue;
1289 sin6 = &lsa6;
1292 if ((site_scope == 0) &&
1293 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1294 continue;
1296 memcpy(sas, sin6, sizeof(*sin6));
1297 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1298 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1299 actual += sizeof(*sin6);
1300 if (actual >= limit) {
1301 return (actual);
1306 } else {
1307 struct sctp_laddr *laddr;
1309 * If we have a TCB and we do NOT support ASCONF (it's
1310 * turned off or otherwise) then the list is always the
1311 * true list of addresses (the else case below). Otherwise
1312 * the list on the association is a list of addresses that
1313 * are NOT part of the association.
1315 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1316 /* The list is a NEGATIVE list */
1317 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1318 if (stcb) {
1319 if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) {
1320 continue;
1323 if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr))
1324 continue;
1326 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1327 sas = (struct sockaddr_storage *)((caddr_t)sas +
1328 laddr->ifa->ifa_addr->sa_len);
1329 actual += laddr->ifa->ifa_addr->sa_len;
1330 if (actual >= limit) {
1331 return (actual);
1334 } else {
1335 /* The list is a positive list if present */
1336 if (stcb) {
1337 /* Must use the specific association list */
1338 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1339 sctp_nxt_addr) {
1340 if (sctp_fill_user_address(sas,
1341 laddr->ifa->ifa_addr))
1342 continue;
1343 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1344 sas = (struct sockaddr_storage *)((caddr_t)sas +
1345 laddr->ifa->ifa_addr->sa_len);
1346 actual += laddr->ifa->ifa_addr->sa_len;
1347 if (actual >= limit) {
1348 return (actual);
1351 } else {
1352 /* No endpoint so use the endpoints individual list */
1353 LIST_FOREACH(laddr, &inp->sctp_addr_list,
1354 sctp_nxt_addr) {
1355 if (sctp_fill_user_address(sas,
1356 laddr->ifa->ifa_addr))
1357 continue;
1358 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1359 sas = (struct sockaddr_storage *)((caddr_t)sas +
1360 laddr->ifa->ifa_addr->sa_len);
1361 actual += laddr->ifa->ifa_addr->sa_len;
1362 if (actual >= limit) {
1363 return (actual);
1369 return (actual);
1372 static int
1373 sctp_count_max_addresses(struct sctp_inpcb *inp)
1375 int cnt = 0;
1377 * In both sub-set bound an bound_all cases we return the MAXIMUM
1378 * number of addresses that you COULD get. In reality the sub-set
1379 * bound may have an exclusion list for a given TCB OR in the
1380 * bound-all case a TCB may NOT include the loopback or other
1381 * addresses as well.
1383 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1384 struct ifnet *ifn;
1385 struct ifaddr *ifa;
1387 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1388 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
1389 /* Count them if they are the right type */
1390 if (ifa->ifa_addr->sa_family == AF_INET) {
1391 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1392 cnt += sizeof(struct sockaddr_in6);
1393 else
1394 cnt += sizeof(struct sockaddr_in);
1396 } else if (ifa->ifa_addr->sa_family == AF_INET6)
1397 cnt += sizeof(struct sockaddr_in6);
1400 } else {
1401 struct sctp_laddr *laddr;
1402 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1403 if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
1404 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1405 cnt += sizeof(struct sockaddr_in6);
1406 else
1407 cnt += sizeof(struct sockaddr_in);
1409 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET6)
1410 cnt += sizeof(struct sockaddr_in6);
1413 return (cnt);
1416 static int
1417 sctp_do_connect_x(struct socket *so,
1418 struct sctp_inpcb *inp,
1419 struct mbuf *m,
1420 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1421 struct thread *p,
1422 #else
1423 struct proc *p,
1424 #endif
1425 int delay
1428 int error = 0;
1429 struct sctp_tcb *stcb = NULL;
1430 struct sockaddr *sa;
1431 int num_v6=0, num_v4=0, *totaddrp, totaddr, i, incr, at;
1432 #ifdef SCTP_DEBUG
1433 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1434 kprintf("Connectx called\n");
1436 #endif /* SCTP_DEBUG */
1438 crit_enter();
1439 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1440 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1441 /* We are already connected AND the TCP model */
1442 crit_exit();
1443 return (EADDRINUSE);
1445 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1446 SCTP_INP_RLOCK(inp);
1447 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1448 SCTP_INP_RUNLOCK(inp);
1450 if (stcb) {
1451 crit_exit();
1452 return (EALREADY);
1455 SCTP_ASOC_CREATE_LOCK(inp);
1456 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1457 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1458 SCTP_ASOC_CREATE_UNLOCK(inp);
1459 crit_exit();
1460 return (EFAULT);
1463 totaddrp = mtod(m, int *);
1464 totaddr = *totaddrp;
1465 sa = (struct sockaddr *)(totaddrp + 1);
1466 at = incr = 0;
1467 /* account and validate addresses */
1468 SCTP_INP_WLOCK(inp);
1469 SCTP_INP_INCR_REF(inp);
1470 SCTP_INP_WUNLOCK(inp);
1471 for (i = 0; i < totaddr; i++) {
1472 if (sa->sa_family == AF_INET) {
1473 num_v4++;
1474 incr = sizeof(struct sockaddr_in);
1475 } else if (sa->sa_family == AF_INET6) {
1476 struct sockaddr_in6 *sin6;
1477 sin6 = (struct sockaddr_in6 *)sa;
1478 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1479 /* Must be non-mapped for connectx */
1480 SCTP_ASOC_CREATE_UNLOCK(inp);
1481 crit_exit();
1482 return EINVAL;
1484 num_v6++;
1485 incr = sizeof(struct sockaddr_in6);
1486 } else {
1487 totaddr = i;
1488 break;
1490 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1491 if (stcb != NULL) {
1492 /* Already have or am bring up an association */
1493 SCTP_ASOC_CREATE_UNLOCK(inp);
1494 SCTP_TCB_UNLOCK(stcb);
1495 crit_exit();
1496 return (EALREADY);
1498 if ((at + incr) > m->m_len) {
1499 totaddr = i;
1500 break;
1502 sa = (struct sockaddr *)((caddr_t)sa + incr);
1504 sa = (struct sockaddr *)(totaddrp + 1);
1505 SCTP_INP_WLOCK(inp);
1506 SCTP_INP_DECR_REF(inp);
1507 SCTP_INP_WUNLOCK(inp);
1508 #ifdef INET6
1509 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1510 (num_v6 > 0)) {
1511 crit_exit();
1512 SCTP_INP_WUNLOCK(inp);
1513 SCTP_ASOC_CREATE_UNLOCK(inp);
1514 return (EINVAL);
1516 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1517 (num_v4 > 0)) {
1518 struct in6pcb *inp6;
1519 inp6 = (struct in6pcb *)inp;
1520 if (
1521 #if defined(__OpenBSD__)
1522 (0) /* we always do dual bind */
1523 #elif defined (__NetBSD__)
1524 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1525 #else
1526 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1527 #endif
1530 * if IPV6_V6ONLY flag, ignore connections
1531 * destined to a v4 addr or v4-mapped addr
1533 SCTP_INP_WUNLOCK(inp);
1534 SCTP_ASOC_CREATE_UNLOCK(inp);
1535 crit_exit();
1536 return EINVAL;
1539 #endif /* INET6 */
1540 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1541 SCTP_PCB_FLAGS_UNBOUND) {
1542 /* Bind a ephemeral port */
1543 SCTP_INP_WUNLOCK(inp);
1544 error = sctp_inpcb_bind(so, NULL, p);
1545 if (error) {
1546 SCTP_ASOC_CREATE_UNLOCK(inp);
1547 crit_exit();
1548 return (error);
1550 } else {
1551 SCTP_INP_WUNLOCK(inp);
1553 /* We are GOOD to go */
1554 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0);
1555 if (stcb == NULL) {
1556 /* Gak! no memory */
1557 SCTP_ASOC_CREATE_UNLOCK(inp);
1558 crit_exit();
1559 return (error);
1561 /* move to second address */
1562 if (sa->sa_family == AF_INET)
1563 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1564 else
1565 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1567 for (i = 1; i < totaddr; i++) {
1568 if (sa->sa_family == AF_INET) {
1569 incr = sizeof(struct sockaddr_in);
1570 if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1571 /* assoc gone no un-lock */
1572 sctp_free_assoc(inp, stcb);
1573 SCTP_ASOC_CREATE_UNLOCK(inp);
1574 crit_exit();
1575 return (ENOBUFS);
1578 } else if (sa->sa_family == AF_INET6) {
1579 incr = sizeof(struct sockaddr_in6);
1580 if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1581 /* assoc gone no un-lock */
1582 sctp_free_assoc(inp, stcb);
1583 SCTP_ASOC_CREATE_UNLOCK(inp);
1584 crit_exit();
1585 return (ENOBUFS);
1588 sa = (struct sockaddr *)((caddr_t)sa + incr);
1590 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1591 if (delay) {
1592 /* doing delayed connection */
1593 stcb->asoc.delayed_connection = 1;
1594 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1595 } else {
1596 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1597 sctp_send_initiate(inp, stcb);
1599 SCTP_TCB_UNLOCK(stcb);
1600 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1601 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1602 /* Set the connected flag so we can queue data */
1603 soisconnecting(so);
1605 SCTP_ASOC_CREATE_UNLOCK(inp);
1606 crit_exit();
1607 return error;
1611 static int
1612 sctp_optsget(struct socket *so,
1613 int opt,
1614 struct mbuf **mp,
1615 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1616 struct thread *p
1617 #else
1618 struct proc *p
1619 #endif
1622 struct sctp_inpcb *inp;
1623 struct mbuf *m;
1624 int error, optval=0;
1625 struct sctp_tcb *stcb = NULL;
1627 inp = (struct sctp_inpcb *)so->so_pcb;
1628 if (inp == 0)
1629 return EINVAL;
1630 error = 0;
1632 if (mp == NULL) {
1633 #ifdef SCTP_DEBUG
1634 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1635 kprintf("optsget:MP is NULL EINVAL\n");
1637 #endif /* SCTP_DEBUG */
1638 return (EINVAL);
1640 m = *mp;
1641 if (m == NULL) {
1642 /* Got to have a mbuf */
1643 #ifdef SCTP_DEBUG
1644 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1645 kprintf("Huh no mbuf\n");
1647 #endif /* SCTP_DEBUG */
1648 return (EINVAL);
1650 #ifdef SCTP_DEBUG
1651 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1652 kprintf("optsget opt:%lxx sz:%u\n", (unsigned long)opt,
1653 m->m_len);
1655 #endif /* SCTP_DEBUG */
1657 switch (opt) {
1658 case SCTP_NODELAY:
1659 case SCTP_AUTOCLOSE:
1660 case SCTP_AUTO_ASCONF:
1661 case SCTP_DISABLE_FRAGMENTS:
1662 case SCTP_I_WANT_MAPPED_V4_ADDR:
1663 #ifdef SCTP_DEBUG
1664 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1665 kprintf("other stuff\n");
1667 #endif /* SCTP_DEBUG */
1668 SCTP_INP_RLOCK(inp);
1669 switch (opt) {
1670 case SCTP_DISABLE_FRAGMENTS:
1671 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT;
1672 break;
1673 case SCTP_I_WANT_MAPPED_V4_ADDR:
1674 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
1675 break;
1676 case SCTP_AUTO_ASCONF:
1677 optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF;
1678 break;
1679 case SCTP_NODELAY:
1680 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY;
1681 break;
1682 case SCTP_AUTOCLOSE:
1683 if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) ==
1684 SCTP_PCB_FLAGS_AUTOCLOSE)
1685 optval = inp->sctp_ep.auto_close_time;
1686 else
1687 optval = 0;
1688 break;
1690 default:
1691 error = ENOPROTOOPT;
1692 } /* end switch (sopt->sopt_name) */
1693 if (opt != SCTP_AUTOCLOSE) {
1694 /* make it an "on/off" value */
1695 optval = (optval != 0);
1697 if ((size_t)m->m_len < sizeof(int)) {
1698 error = EINVAL;
1700 SCTP_INP_RUNLOCK(inp);
1701 if (error == 0) {
1702 /* return the option value */
1703 *mtod(m, int *) = optval;
1704 m->m_len = sizeof(optval);
1706 break;
1707 case SCTP_GET_ASOC_ID_LIST:
1709 struct sctp_assoc_ids *ids;
1710 int cnt, at;
1711 u_int16_t orig;
1713 if ((size_t)m->m_len < sizeof(struct sctp_assoc_ids)) {
1714 error = EINVAL;
1715 break;
1717 ids = mtod(m, struct sctp_assoc_ids *);
1718 cnt = 0;
1719 SCTP_INP_RLOCK(inp);
1720 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1721 if (stcb == NULL) {
1722 none_out_now:
1723 ids->asls_numb_present = 0;
1724 ids->asls_more_to_get = 0;
1725 SCTP_INP_RUNLOCK(inp);
1726 break;
1728 orig = ids->asls_assoc_start;
1729 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1730 while( orig ) {
1731 stcb = LIST_NEXT(stcb , sctp_tcblist);
1732 orig--;
1733 cnt--;
1735 if ( stcb == NULL)
1736 goto none_out_now;
1738 at = 0;
1739 ids->asls_numb_present = 0;
1740 ids->asls_more_to_get = 1;
1741 while(at < MAX_ASOC_IDS_RET) {
1742 ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1743 at++;
1744 ids->asls_numb_present++;
1745 stcb = LIST_NEXT(stcb , sctp_tcblist);
1746 if (stcb == NULL) {
1747 ids->asls_more_to_get = 0;
1748 break;
1751 SCTP_INP_RUNLOCK(inp);
1753 break;
1754 case SCTP_GET_NONCE_VALUES:
1756 struct sctp_get_nonce_values *gnv;
1757 if ((size_t)m->m_len < sizeof(struct sctp_get_nonce_values)) {
1758 error = EINVAL;
1759 break;
1761 gnv = mtod(m, struct sctp_get_nonce_values *);
1762 stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id);
1763 if (stcb == NULL) {
1764 error = ENOTCONN;
1765 } else {
1766 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1767 gnv->gn_local_tag = stcb->asoc.my_vtag;
1768 SCTP_TCB_UNLOCK(stcb);
1772 break;
1773 case SCTP_PEER_PUBLIC_KEY:
1774 case SCTP_MY_PUBLIC_KEY:
1775 case SCTP_SET_AUTH_CHUNKS:
1776 case SCTP_SET_AUTH_SECRET:
1777 /* not supported yet and until we refine the draft */
1778 error = EOPNOTSUPP;
1779 break;
1781 case SCTP_DELAYED_ACK_TIME:
1783 int32_t *tm;
1784 if ((size_t)m->m_len < sizeof(int32_t)) {
1785 error = EINVAL;
1786 break;
1788 tm = mtod(m, int32_t *);
1790 *tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1792 break;
1794 case SCTP_GET_SNDBUF_USE:
1795 if ((size_t)m->m_len < sizeof(struct sctp_sockstat)) {
1796 error = EINVAL;
1797 } else {
1798 struct sctp_sockstat *ss;
1799 struct sctp_tcb *stcb;
1800 struct sctp_association *asoc;
1801 ss = mtod(m, struct sctp_sockstat *);
1802 stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id);
1803 if (stcb == NULL) {
1804 error = ENOTCONN;
1805 } else {
1806 asoc = &stcb->asoc;
1807 ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size;
1808 ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size;
1809 ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue +
1810 asoc->size_on_reasm_queue +
1811 asoc->size_on_all_streams);
1812 SCTP_TCB_UNLOCK(stcb);
1813 error = 0;
1814 m->m_len = sizeof(struct sctp_sockstat);
1817 break;
1818 case SCTP_MAXBURST:
1820 u_int8_t *burst;
1821 burst = mtod(m, u_int8_t *);
1822 SCTP_INP_RLOCK(inp);
1823 *burst = inp->sctp_ep.max_burst;
1824 SCTP_INP_RUNLOCK(inp);
1825 m->m_len = sizeof(u_int8_t);
1827 break;
1828 case SCTP_MAXSEG:
1830 u_int32_t *segsize;
1831 sctp_assoc_t *assoc_id;
1832 int ovh;
1834 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1835 error = EINVAL;
1836 break;
1838 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
1839 error = EINVAL;
1840 break;
1842 assoc_id = mtod(m, sctp_assoc_t *);
1843 segsize = mtod(m, u_int32_t *);
1844 m->m_len = sizeof(u_int32_t);
1846 if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1847 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) ||
1848 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1849 struct sctp_tcb *stcb;
1850 SCTP_INP_RLOCK(inp);
1851 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1852 if (stcb) {
1853 SCTP_TCB_LOCK(stcb);
1854 SCTP_INP_RUNLOCK(inp);
1855 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1856 SCTP_TCB_UNLOCK(stcb);
1857 } else {
1858 SCTP_INP_RUNLOCK(inp);
1859 goto skipit;
1861 } else {
1862 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1863 if (stcb) {
1864 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1865 SCTP_TCB_UNLOCK(stcb);
1866 break;
1868 skipit:
1869 /* default is to get the max, if I
1870 * can't calculate from an existing association.
1872 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1873 ovh = SCTP_MED_OVERHEAD;
1874 } else {
1875 ovh = SCTP_MED_V4_OVERHEAD;
1877 *segsize = inp->sctp_frag_point - ovh;
1880 break;
1882 case SCTP_SET_DEBUG_LEVEL:
1883 #ifdef SCTP_DEBUG
1885 u_int32_t *level;
1886 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1887 error = EINVAL;
1888 break;
1890 level = mtod(m, u_int32_t *);
1891 error = 0;
1892 *level = sctp_debug_on;
1893 m->m_len = sizeof(u_int32_t);
1894 kprintf("Returning DEBUG LEVEL %x is set\n",
1895 (u_int)sctp_debug_on);
1897 #else /* SCTP_DEBUG */
1898 error = EOPNOTSUPP;
1899 #endif
1900 break;
1901 case SCTP_GET_STAT_LOG:
1902 #ifdef SCTP_STAT_LOGGING
1903 error = sctp_fill_stat_log(m);
1904 #else /* SCTP_DEBUG */
1905 error = EOPNOTSUPP;
1906 #endif
1907 break;
1908 case SCTP_GET_PEGS:
1910 u_int32_t *pt;
1911 if ((size_t)m->m_len < sizeof(sctp_pegs)) {
1912 error = EINVAL;
1913 break;
1915 pt = mtod(m, u_int32_t *);
1916 memcpy(pt, sctp_pegs, sizeof(sctp_pegs));
1917 m->m_len = sizeof(sctp_pegs);
1919 break;
1920 case SCTP_EVENTS:
1922 struct sctp_event_subscribe *events;
1923 #ifdef SCTP_DEBUG
1924 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1925 kprintf("get events\n");
1927 #endif /* SCTP_DEBUG */
1928 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
1929 #ifdef SCTP_DEBUG
1930 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1931 kprintf("M->M_LEN is %d not %d\n",
1932 (int)m->m_len,
1933 (int)sizeof(struct sctp_event_subscribe));
1935 #endif /* SCTP_DEBUG */
1936 error = EINVAL;
1937 break;
1939 events = mtod(m, struct sctp_event_subscribe *);
1940 memset(events, 0, sizeof(events));
1941 SCTP_INP_RLOCK(inp);
1942 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT)
1943 events->sctp_data_io_event = 1;
1945 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT)
1946 events->sctp_association_event = 1;
1948 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT)
1949 events->sctp_address_event = 1;
1951 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT)
1952 events->sctp_send_failure_event = 1;
1954 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR)
1955 events->sctp_peer_error_event = 1;
1957 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)
1958 events->sctp_shutdown_event = 1;
1960 if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT)
1961 events->sctp_partial_delivery_event = 1;
1963 if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT)
1964 events->sctp_adaption_layer_event = 1;
1966 if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT)
1967 events->sctp_stream_reset_events = 1;
1968 SCTP_INP_RUNLOCK(inp);
1969 m->m_len = sizeof(struct sctp_event_subscribe);
1972 break;
1974 case SCTP_ADAPTION_LAYER:
1975 if ((size_t)m->m_len < sizeof(int)) {
1976 error = EINVAL;
1977 break;
1979 #ifdef SCTP_DEBUG
1980 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1981 kprintf("getadaption ind\n");
1983 #endif /* SCTP_DEBUG */
1984 SCTP_INP_RLOCK(inp);
1985 *mtod(m, int *) = inp->sctp_ep.adaption_layer_indicator;
1986 SCTP_INP_RUNLOCK(inp);
1987 m->m_len = sizeof(int);
1988 break;
1989 case SCTP_SET_INITIAL_DBG_SEQ:
1990 if ((size_t)m->m_len < sizeof(int)) {
1991 error = EINVAL;
1992 break;
1994 #ifdef SCTP_DEBUG
1995 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1996 kprintf("get initial dbg seq\n");
1998 #endif /* SCTP_DEBUG */
1999 SCTP_INP_RLOCK(inp);
2000 *mtod(m, int *) = inp->sctp_ep.initial_sequence_debug;
2001 SCTP_INP_RUNLOCK(inp);
2002 m->m_len = sizeof(int);
2003 break;
2004 case SCTP_GET_LOCAL_ADDR_SIZE:
2005 if ((size_t)m->m_len < sizeof(int)) {
2006 error = EINVAL;
2007 break;
2009 #ifdef SCTP_DEBUG
2010 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2011 kprintf("get local sizes\n");
2013 #endif /* SCTP_DEBUG */
2014 SCTP_INP_RLOCK(inp);
2015 *mtod(m, int *) = sctp_count_max_addresses(inp);
2016 SCTP_INP_RUNLOCK(inp);
2017 m->m_len = sizeof(int);
2018 break;
2019 case SCTP_GET_REMOTE_ADDR_SIZE:
2021 sctp_assoc_t *assoc_id;
2022 u_int32_t *val, sz;
2023 struct sctp_nets *net;
2024 #ifdef SCTP_DEBUG
2025 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2026 kprintf("get remote size\n");
2028 #endif /* SCTP_DEBUG */
2029 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
2030 #ifdef SCTP_DEBUG
2031 kprintf("m->m_len:%d not %d\n",
2032 m->m_len, sizeof(sctp_assoc_t));
2033 #endif /* SCTP_DEBUG */
2034 error = EINVAL;
2035 break;
2037 stcb = NULL;
2038 val = mtod(m, u_int32_t *);
2039 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2040 SCTP_INP_RLOCK(inp);
2041 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2042 if (stcb)
2043 SCTP_TCB_LOCK(stcb);
2044 SCTP_INP_RUNLOCK(inp);
2046 if (stcb == NULL) {
2047 assoc_id = mtod(m, sctp_assoc_t *);
2048 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
2051 if (stcb == NULL) {
2052 error = EINVAL;
2053 break;
2055 *val = 0;
2056 sz = 0;
2057 /* Count the sizes */
2058 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2059 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2060 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2061 sz += sizeof(struct sockaddr_in6);
2062 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2063 sz += sizeof(struct sockaddr_in);
2064 } else {
2065 /* huh */
2066 break;
2069 SCTP_TCB_UNLOCK(stcb);
2070 *val = sz;
2071 m->m_len = sizeof(u_int32_t);
2073 break;
2074 case SCTP_GET_PEER_ADDRESSES:
2076 * Get the address information, an array
2077 * is passed in to fill up we pack it.
2080 int cpsz, left;
2081 struct sockaddr_storage *sas;
2082 struct sctp_nets *net;
2083 struct sctp_getaddresses *saddr;
2084 #ifdef SCTP_DEBUG
2085 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2086 kprintf("get peer addresses\n");
2088 #endif /* SCTP_DEBUG */
2089 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2090 error = EINVAL;
2091 break;
2093 left = m->m_len - sizeof(struct sctp_getaddresses);
2094 saddr = mtod(m, struct sctp_getaddresses *);
2095 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2096 SCTP_INP_RLOCK(inp);
2097 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2098 if (stcb)
2099 SCTP_TCB_LOCK(stcb);
2100 SCTP_INP_RUNLOCK(inp);
2101 } else
2102 stcb = sctp_findassociation_ep_asocid(inp,
2103 saddr->sget_assoc_id);
2104 if (stcb == NULL) {
2105 error = ENOENT;
2106 break;
2108 m->m_len = sizeof(struct sctp_getaddresses);
2109 sas = (struct sockaddr_storage *)&saddr->addr[0];
2111 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2112 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2113 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2114 cpsz = sizeof(struct sockaddr_in6);
2115 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2116 cpsz = sizeof(struct sockaddr_in);
2117 } else {
2118 /* huh */
2119 break;
2121 if (left < cpsz) {
2122 /* not enough room. */
2123 #ifdef SCTP_DEBUG
2124 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2125 kprintf("Out of room\n");
2127 #endif /* SCTP_DEBUG */
2128 break;
2130 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
2131 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2132 /* Must map the address */
2133 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2134 (struct sockaddr_in6 *)sas);
2135 } else {
2136 memcpy(sas, &net->ro._l_addr, cpsz);
2138 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2140 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2141 left -= cpsz;
2142 m->m_len += cpsz;
2143 #ifdef SCTP_DEBUG
2144 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2145 kprintf("left now:%d mlen:%d\n",
2146 left, m->m_len);
2148 #endif /* SCTP_DEBUG */
2150 SCTP_TCB_UNLOCK(stcb);
2152 #ifdef SCTP_DEBUG
2153 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2154 kprintf("All done\n");
2156 #endif /* SCTP_DEBUG */
2157 break;
2158 case SCTP_GET_LOCAL_ADDRESSES:
2160 int limit, actual;
2161 struct sockaddr_storage *sas;
2162 struct sctp_getaddresses *saddr;
2163 #ifdef SCTP_DEBUG
2164 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2165 kprintf("get local addresses\n");
2167 #endif /* SCTP_DEBUG */
2168 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2169 error = EINVAL;
2170 break;
2172 saddr = mtod(m, struct sctp_getaddresses *);
2174 if (saddr->sget_assoc_id) {
2175 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2176 SCTP_INP_RLOCK(inp);
2177 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2178 if (stcb)
2179 SCTP_TCB_LOCK(stcb);
2180 SCTP_INP_RUNLOCK(inp);
2181 } else
2182 stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id);
2184 } else {
2185 stcb = NULL;
2188 * assure that the TCP model does not need a assoc id
2189 * once connected.
2191 if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
2192 (stcb == NULL) ) {
2193 SCTP_INP_RLOCK(inp);
2194 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2195 if (stcb)
2196 SCTP_TCB_LOCK(stcb);
2197 SCTP_INP_RUNLOCK(inp);
2199 sas = (struct sockaddr_storage *)&saddr->addr[0];
2200 limit = m->m_len - sizeof(sctp_assoc_t);
2201 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2202 SCTP_TCB_UNLOCK(stcb);
2203 m->m_len = sizeof(struct sockaddr_storage) + actual;
2205 break;
2206 case SCTP_PEER_ADDR_PARAMS:
2208 struct sctp_paddrparams *paddrp;
2209 struct sctp_nets *net;
2211 #ifdef SCTP_DEBUG
2212 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2213 kprintf("Getting peer_addr_params\n");
2215 #endif /* SCTP_DEBUG */
2216 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
2217 #ifdef SCTP_DEBUG
2218 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2219 kprintf("Hmm m->m_len:%d is to small\n",
2220 m->m_len);
2222 #endif /* SCTP_DEBUG */
2223 error = EINVAL;
2224 break;
2226 paddrp = mtod(m, struct sctp_paddrparams *);
2228 net = NULL;
2229 if (paddrp->spp_assoc_id) {
2230 #ifdef SCTP_DEBUG
2231 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2232 kprintf("In spp_assoc_id find type\n");
2234 #endif /* SCTP_DEBUG */
2235 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2236 SCTP_INP_RLOCK(inp);
2237 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2238 if (stcb) {
2239 SCTP_TCB_LOCK(stcb);
2240 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2242 SCTP_INP_RLOCK(inp);
2243 } else {
2244 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
2246 if (stcb == NULL) {
2247 error = ENOENT;
2248 break;
2251 if ( (stcb == NULL) &&
2252 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
2253 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
2254 /* Lookup via address */
2255 #ifdef SCTP_DEBUG
2256 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2257 kprintf("Ok we need to lookup a param\n");
2259 #endif /* SCTP_DEBUG */
2260 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2261 SCTP_INP_RLOCK(inp);
2262 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2263 if (stcb) {
2264 SCTP_TCB_LOCK(stcb);
2265 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2267 SCTP_INP_RUNLOCK(inp);
2268 } else {
2269 SCTP_INP_WLOCK(inp);
2270 SCTP_INP_INCR_REF(inp);
2271 SCTP_INP_WUNLOCK(inp);
2272 stcb = sctp_findassociation_ep_addr(&inp,
2273 (struct sockaddr *)&paddrp->spp_address,
2274 &net, NULL, NULL);
2275 if (stcb == NULL) {
2276 SCTP_INP_WLOCK(inp);
2277 SCTP_INP_DECR_REF(inp);
2278 SCTP_INP_WUNLOCK(inp);
2282 if (stcb == NULL) {
2283 error = ENOENT;
2284 break;
2286 } else {
2287 /* Effects the Endpoint */
2288 #ifdef SCTP_DEBUG
2289 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2290 kprintf("User wants EP level info\n");
2292 #endif /* SCTP_DEBUG */
2293 stcb = NULL;
2295 if (stcb) {
2296 /* Applys to the specific association */
2297 #ifdef SCTP_DEBUG
2298 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2299 kprintf("In TCB side\n");
2301 #endif /* SCTP_DEBUG */
2302 if (net) {
2303 paddrp->spp_pathmaxrxt = net->failure_threshold;
2304 } else {
2305 /* No destination so return default value */
2306 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2308 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2309 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2310 SCTP_TCB_UNLOCK(stcb);
2311 } else {
2312 /* Use endpoint defaults */
2313 SCTP_INP_RLOCK(inp);
2314 #ifdef SCTP_DEBUG
2315 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2316 kprintf("In EP levle info\n");
2318 #endif /* SCTP_DEBUG */
2319 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2320 paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT];
2321 paddrp->spp_assoc_id = (sctp_assoc_t)0;
2322 SCTP_INP_RUNLOCK(inp);
2324 m->m_len = sizeof(struct sctp_paddrparams);
2326 break;
2327 case SCTP_GET_PEER_ADDR_INFO:
2329 struct sctp_paddrinfo *paddri;
2330 struct sctp_nets *net;
2331 #ifdef SCTP_DEBUG
2332 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2333 kprintf("GetPEER ADDR_INFO\n");
2335 #endif /* SCTP_DEBUG */
2336 if ((size_t)m->m_len < sizeof(struct sctp_paddrinfo)) {
2337 error = EINVAL;
2338 break;
2340 paddri = mtod(m, struct sctp_paddrinfo *);
2341 net = NULL;
2342 if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) ||
2343 (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) {
2344 /* Lookup via address */
2345 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2346 SCTP_INP_RLOCK(inp);
2347 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2348 if (stcb) {
2349 SCTP_TCB_LOCK(stcb);
2350 net = sctp_findnet(stcb,
2351 (struct sockaddr *)&paddri->spinfo_address);
2353 SCTP_INP_RUNLOCK(inp);
2354 } else {
2355 SCTP_INP_WLOCK(inp);
2356 SCTP_INP_INCR_REF(inp);
2357 SCTP_INP_WUNLOCK(inp);
2358 stcb = sctp_findassociation_ep_addr(&inp,
2359 (struct sockaddr *)&paddri->spinfo_address,
2360 &net, NULL, NULL);
2361 if (stcb == NULL) {
2362 SCTP_INP_WLOCK(inp);
2363 SCTP_INP_DECR_REF(inp);
2364 SCTP_INP_WUNLOCK(inp);
2368 } else {
2369 stcb = NULL;
2371 if ((stcb == NULL) || (net == NULL)) {
2372 error = ENOENT;
2373 break;
2375 m->m_len = sizeof(struct sctp_paddrinfo);
2376 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB);
2377 paddri->spinfo_cwnd = net->cwnd;
2378 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2379 paddri->spinfo_rto = net->RTO;
2380 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2381 SCTP_TCB_UNLOCK(stcb);
2383 break;
2384 case SCTP_PCB_STATUS:
2386 struct sctp_pcbinfo *spcb;
2387 #ifdef SCTP_DEBUG
2388 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2389 kprintf("PCB status\n");
2391 #endif /* SCTP_DEBUG */
2392 if ((size_t)m->m_len < sizeof(struct sctp_pcbinfo)) {
2393 error = EINVAL;
2394 break;
2396 spcb = mtod(m, struct sctp_pcbinfo *);
2397 sctp_fill_pcbinfo(spcb);
2398 m->m_len = sizeof(struct sctp_pcbinfo);
2400 break;
2401 case SCTP_STATUS:
2403 struct sctp_nets *net;
2404 struct sctp_status *sstat;
2405 #ifdef SCTP_DEBUG
2406 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2407 kprintf("SCTP status\n");
2409 #endif /* SCTP_DEBUG */
2411 if ((size_t)m->m_len < sizeof(struct sctp_status)) {
2412 error = EINVAL;
2413 break;
2415 sstat = mtod(m, struct sctp_status *);
2417 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2418 SCTP_INP_RLOCK(inp);
2419 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2420 if (stcb)
2421 SCTP_TCB_LOCK(stcb);
2422 SCTP_INP_RUNLOCK(inp);
2423 } else
2424 stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id);
2426 if (stcb == NULL) {
2427 error = EINVAL;
2428 break;
2431 * I think passing the state is fine since
2432 * sctp_constants.h will be available to the user
2433 * land.
2435 sstat->sstat_state = stcb->asoc.state;
2436 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2437 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2439 * We can't include chunks that have been passed
2440 * to the socket layer. Only things in queue.
2442 sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue +
2443 stcb->asoc.cnt_on_reasm_queue +
2444 stcb->asoc.cnt_on_all_streams);
2447 sstat->sstat_instrms = stcb->asoc.streamincnt;
2448 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2449 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2450 memcpy(&sstat->sstat_primary.spinfo_address,
2451 &stcb->asoc.primary_destination->ro._l_addr,
2452 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2453 net = stcb->asoc.primary_destination;
2454 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2456 * Again the user can get info from sctp_constants.h
2457 * for what the state of the network is.
2459 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2460 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2461 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2462 sstat->sstat_primary.spinfo_rto = net->RTO;
2463 sstat->sstat_primary.spinfo_mtu = net->mtu;
2464 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2465 SCTP_TCB_UNLOCK(stcb);
2466 m->m_len = sizeof(*sstat);
2468 break;
2469 case SCTP_RTOINFO:
2471 struct sctp_rtoinfo *srto;
2472 #ifdef SCTP_DEBUG
2473 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2474 kprintf("RTO Info\n");
2476 #endif /* SCTP_DEBUG */
2477 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
2478 error = EINVAL;
2479 break;
2481 srto = mtod(m, struct sctp_rtoinfo *);
2482 if (srto->srto_assoc_id == 0) {
2483 /* Endpoint only please */
2484 SCTP_INP_RLOCK(inp);
2485 srto->srto_initial = inp->sctp_ep.initial_rto;
2486 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2487 srto->srto_min = inp->sctp_ep.sctp_minrto;
2488 SCTP_INP_RUNLOCK(inp);
2489 break;
2491 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2492 SCTP_INP_RLOCK(inp);
2493 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2494 if (stcb)
2495 SCTP_TCB_LOCK(stcb);
2496 SCTP_INP_RUNLOCK(inp);
2497 } else
2498 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2500 if (stcb == NULL) {
2501 error = EINVAL;
2502 break;
2504 srto->srto_initial = stcb->asoc.initial_rto;
2505 srto->srto_max = stcb->asoc.maxrto;
2506 srto->srto_min = stcb->asoc.minrto;
2507 SCTP_TCB_UNLOCK(stcb);
2508 m->m_len = sizeof(*srto);
2510 break;
2511 case SCTP_ASSOCINFO:
2513 struct sctp_assocparams *sasoc;
2514 #ifdef SCTP_DEBUG
2515 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2516 kprintf("Associnfo\n");
2518 #endif /* SCTP_DEBUG */
2519 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
2520 error = EINVAL;
2521 break;
2523 sasoc = mtod(m, struct sctp_assocparams *);
2524 stcb = NULL;
2526 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2527 SCTP_INP_RLOCK(inp);
2528 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2529 if (stcb)
2530 SCTP_TCB_LOCK(stcb);
2531 SCTP_INP_RUNLOCK(inp);
2533 if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) {
2534 stcb = sctp_findassociation_ep_asocid(inp,
2535 sasoc->sasoc_assoc_id);
2536 if (stcb == NULL) {
2537 error = ENOENT;
2538 break;
2540 } else {
2541 stcb = NULL;
2544 if (stcb) {
2545 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2546 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2547 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2548 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2549 sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2550 SCTP_TCB_UNLOCK(stcb);
2551 } else {
2552 SCTP_INP_RLOCK(inp);
2553 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2554 sasoc->sasoc_number_peer_destinations = 0;
2555 sasoc->sasoc_peer_rwnd = 0;
2556 sasoc->sasoc_local_rwnd = ssb_space(&inp->sctp_socket->so_rcv);
2557 sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2558 SCTP_INP_RUNLOCK(inp);
2560 m->m_len = sizeof(*sasoc);
2562 break;
2563 case SCTP_DEFAULT_SEND_PARAM:
2565 struct sctp_sndrcvinfo *s_info;
2567 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
2568 error = EINVAL;
2569 break;
2571 s_info = mtod(m, struct sctp_sndrcvinfo *);
2572 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2573 SCTP_INP_RLOCK(inp);
2574 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2575 if (stcb)
2576 SCTP_TCB_LOCK(stcb);
2577 SCTP_INP_RUNLOCK(inp);
2578 } else
2579 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2581 if (stcb == NULL) {
2582 error = ENOENT;
2583 break;
2585 /* Copy it out */
2586 *s_info = stcb->asoc.def_send;
2587 SCTP_TCB_UNLOCK(stcb);
2588 m->m_len = sizeof(*s_info);
2590 case SCTP_INITMSG:
2592 struct sctp_initmsg *sinit;
2593 #ifdef SCTP_DEBUG
2594 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2595 kprintf("initmsg\n");
2597 #endif /* SCTP_DEBUG */
2598 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
2599 error = EINVAL;
2600 break;
2602 sinit = mtod(m, struct sctp_initmsg *);
2603 SCTP_INP_RLOCK(inp);
2604 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2605 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2606 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2607 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2608 SCTP_INP_RUNLOCK(inp);
2609 m->m_len = sizeof(*sinit);
2611 break;
2612 case SCTP_PRIMARY_ADDR:
2613 /* we allow a "get" operation on this */
2615 struct sctp_setprim *ssp;
2617 #ifdef SCTP_DEBUG
2618 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2619 kprintf("setprimary\n");
2621 #endif /* SCTP_DEBUG */
2622 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
2623 error = EINVAL;
2624 break;
2626 ssp = mtod(m, struct sctp_setprim *);
2627 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2628 SCTP_INP_RLOCK(inp);
2629 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2630 if (stcb)
2631 SCTP_TCB_LOCK(stcb);
2632 SCTP_INP_RUNLOCK(inp);
2633 } else {
2634 stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id);
2635 if (stcb == NULL) {
2636 /* one last shot, try it by the address in */
2637 struct sctp_nets *net;
2639 SCTP_INP_WLOCK(inp);
2640 SCTP_INP_INCR_REF(inp);
2641 SCTP_INP_WUNLOCK(inp);
2642 stcb = sctp_findassociation_ep_addr(&inp,
2643 (struct sockaddr *)&ssp->ssp_addr,
2644 &net, NULL, NULL);
2645 if (stcb == NULL) {
2646 SCTP_INP_WLOCK(inp);
2647 SCTP_INP_DECR_REF(inp);
2648 SCTP_INP_WUNLOCK(inp);
2651 if (stcb == NULL) {
2652 error = EINVAL;
2653 break;
2656 /* simply copy out the sockaddr_storage... */
2657 memcpy(&ssp->ssp_addr,
2658 &stcb->asoc.primary_destination->ro._l_addr,
2659 ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2660 SCTP_TCB_UNLOCK(stcb);
2661 m->m_len = sizeof(*ssp);
2663 break;
2664 default:
2665 error = ENOPROTOOPT;
2666 m->m_len = 0;
2667 break;
2668 } /* end switch (sopt->sopt_name) */
2669 return (error);
2672 static int
2673 sctp_optsset(struct socket *so,
2674 int opt,
2675 struct mbuf **mp,
2676 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
2677 struct thread *p
2678 #else
2679 struct proc *p
2680 #endif
2683 int error, *mopt, set_opt;
2684 struct mbuf *m;
2685 struct sctp_tcb *stcb = NULL;
2686 struct sctp_inpcb *inp;
2688 if (mp == NULL) {
2689 #ifdef SCTP_DEBUG
2690 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2691 kprintf("optsset:MP is NULL EINVAL\n");
2693 #endif /* SCTP_DEBUG */
2694 return (EINVAL);
2696 m = *mp;
2697 if (m == NULL)
2698 return (EINVAL);
2700 inp = (struct sctp_inpcb *)so->so_pcb;
2701 if (inp == 0)
2702 return EINVAL;
2704 error = 0;
2705 switch (opt) {
2706 case SCTP_NODELAY:
2707 case SCTP_AUTOCLOSE:
2708 case SCTP_AUTO_ASCONF:
2709 case SCTP_DISABLE_FRAGMENTS:
2710 case SCTP_I_WANT_MAPPED_V4_ADDR:
2711 /* copy in the option value */
2712 if ((size_t)m->m_len < sizeof(int)) {
2713 error = EINVAL;
2714 break;
2716 mopt = mtod(m, int *);
2717 set_opt = 0;
2718 if (error)
2719 break;
2720 switch (opt) {
2721 case SCTP_DISABLE_FRAGMENTS:
2722 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2723 break;
2724 case SCTP_AUTO_ASCONF:
2725 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2726 break;
2728 case SCTP_I_WANT_MAPPED_V4_ADDR:
2729 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2730 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2731 } else {
2732 return (EINVAL);
2734 break;
2735 case SCTP_NODELAY:
2736 set_opt = SCTP_PCB_FLAGS_NODELAY;
2737 break;
2738 case SCTP_AUTOCLOSE:
2739 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2741 * The value is in ticks.
2742 * Note this does not effect old associations, only
2743 * new ones.
2745 inp->sctp_ep.auto_close_time = (*mopt * hz);
2746 break;
2748 SCTP_INP_WLOCK(inp);
2749 if (*mopt != 0) {
2750 inp->sctp_flags |= set_opt;
2751 } else {
2752 inp->sctp_flags &= ~set_opt;
2754 SCTP_INP_WUNLOCK(inp);
2755 break;
2756 case SCTP_MY_PUBLIC_KEY: /* set my public key */
2757 case SCTP_SET_AUTH_CHUNKS: /* set the authenticated chunks required */
2758 case SCTP_SET_AUTH_SECRET: /* set the actual secret for the endpoint */
2759 /* not supported yet and until we refine the draft */
2760 error = EOPNOTSUPP;
2761 break;
2763 case SCTP_CLR_STAT_LOG:
2764 #ifdef SCTP_STAT_LOGGING
2765 sctp_clr_stat_log();
2766 #else
2767 error = EOPNOTSUPP;
2768 #endif
2769 break;
2770 case SCTP_DELAYED_ACK_TIME:
2772 int32_t *tm;
2773 if ((size_t)m->m_len < sizeof(int32_t)) {
2774 error = EINVAL;
2775 break;
2777 tm = mtod(m, int32_t *);
2779 if ((*tm < 10) || (*tm > 500)) {
2780 /* can't be smaller than 10ms */
2781 /* MUST NOT be larger than 500ms */
2782 error = EINVAL;
2783 break;
2785 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm);
2787 break;
2788 case SCTP_RESET_STREAMS:
2790 struct sctp_stream_reset *strrst;
2791 uint8_t two_way, not_peer;
2793 if ((size_t)m->m_len < sizeof(struct sctp_stream_reset)) {
2794 error = EINVAL;
2795 break;
2797 strrst = mtod(m, struct sctp_stream_reset *);
2799 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2800 SCTP_INP_RLOCK(inp);
2801 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2802 if (stcb)
2803 SCTP_TCB_LOCK(stcb);
2804 SCTP_INP_RUNLOCK(inp);
2805 } else
2806 stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id);
2807 if (stcb == NULL) {
2808 error = ENOENT;
2809 break;
2811 if (stcb->asoc.peer_supports_strreset == 0) {
2812 /* Peer does not support it,
2813 * we return protocol not supported since
2814 * this is true for this feature and this
2815 * peer, not the socket request in general.
2817 error = EPROTONOSUPPORT;
2818 SCTP_TCB_UNLOCK(stcb);
2819 break;
2822 /* Having re-thought this code I added as I write the I-D there
2823 * is NO need for it. The peer, if we are requesting a stream-reset
2824 * will send a request to us but will itself do what we do, take
2825 * and copy off the "reset information" we send and queue TSN's
2826 * larger than the send-next in our response message. Thus they
2827 * will handle it.
2829 /* if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/
2830 /* Must have all sending data ack'd before we
2831 * start this procedure. This is a bit restrictive
2832 * and we SHOULD work on changing this so ONLY the
2833 * streams being RESET get held up. So, a reset-all
2834 * would require this.. but a reset specific just
2835 * needs to be sure that the ones being reset have
2836 * nothing on the send_queue. For now we will
2837 * skip this more detailed method and do a course
2838 * way.. i.e. nothing pending ... for future FIX ME!
2840 /* error = EBUSY;*/
2841 /* break;*/
2842 /* }*/
2844 if (stcb->asoc.stream_reset_outstanding) {
2845 error = EALREADY;
2846 SCTP_TCB_UNLOCK(stcb);
2847 break;
2849 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2850 two_way = 0;
2851 not_peer = 0;
2852 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2853 two_way = 1;
2854 not_peer = 1;
2855 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2856 two_way = 1;
2857 not_peer = 0;
2858 } else {
2859 error = EINVAL;
2860 SCTP_TCB_UNLOCK(stcb);
2861 break;
2863 sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2864 strrst->strrst_list, two_way, not_peer);
2865 crit_enter();
2866 sctp_chunk_output(inp, stcb, 12);
2867 SCTP_TCB_UNLOCK(stcb);
2868 crit_exit();
2871 break;
2872 case SCTP_RESET_PEGS:
2873 memset(sctp_pegs, 0, sizeof(sctp_pegs));
2874 error = 0;
2875 break;
2876 case SCTP_CONNECT_X:
2877 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2878 error = EINVAL;
2879 break;
2881 error = sctp_do_connect_x(so, inp, m, p, 0);
2882 break;
2884 case SCTP_CONNECT_X_DELAYED:
2885 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2886 error = EINVAL;
2887 break;
2889 error = sctp_do_connect_x(so, inp, m, p, 1);
2890 break;
2892 case SCTP_CONNECT_X_COMPLETE:
2894 struct sockaddr *sa;
2895 struct sctp_nets *net;
2896 if ((size_t)m->m_len < sizeof(struct sockaddr_in)) {
2897 error = EINVAL;
2898 break;
2900 sa = mtod(m, struct sockaddr *);
2901 /* find tcb */
2902 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2903 SCTP_INP_RLOCK(inp);
2904 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2905 if (stcb) {
2906 SCTP_TCB_LOCK(stcb);
2907 net = sctp_findnet(stcb, sa);
2909 SCTP_INP_RUNLOCK(inp);
2910 } else {
2911 SCTP_INP_WLOCK(inp);
2912 SCTP_INP_INCR_REF(inp);
2913 SCTP_INP_WUNLOCK(inp);
2914 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2915 if (stcb == NULL) {
2916 SCTP_INP_WLOCK(inp);
2917 SCTP_INP_DECR_REF(inp);
2918 SCTP_INP_WUNLOCK(inp);
2922 if (stcb == NULL) {
2923 error = ENOENT;
2924 break;
2926 if (stcb->asoc.delayed_connection == 1) {
2927 stcb->asoc.delayed_connection = 0;
2928 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2929 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
2930 sctp_send_initiate(inp, stcb);
2931 } else {
2932 /* already expired or did not use delayed connectx */
2933 error = EALREADY;
2935 SCTP_TCB_UNLOCK(stcb);
2937 break;
2938 case SCTP_MAXBURST:
2940 u_int8_t *burst;
2941 SCTP_INP_WLOCK(inp);
2942 burst = mtod(m, u_int8_t *);
2943 if (*burst) {
2944 inp->sctp_ep.max_burst = *burst;
2946 SCTP_INP_WUNLOCK(inp);
2948 break;
2949 case SCTP_MAXSEG:
2951 u_int32_t *segsize;
2952 int ovh;
2953 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2954 ovh = SCTP_MED_OVERHEAD;
2955 } else {
2956 ovh = SCTP_MED_V4_OVERHEAD;
2958 segsize = mtod(m, u_int32_t *);
2959 if (*segsize < 1) {
2960 error = EINVAL;
2961 break;
2963 SCTP_INP_WLOCK(inp);
2964 inp->sctp_frag_point = (*segsize+ovh);
2965 if (inp->sctp_frag_point < MHLEN) {
2966 inp->sctp_frag_point = MHLEN;
2968 SCTP_INP_WUNLOCK(inp);
2970 break;
2971 case SCTP_SET_DEBUG_LEVEL:
2972 #ifdef SCTP_DEBUG
2974 u_int32_t *level;
2975 if ((size_t)m->m_len < sizeof(u_int32_t)) {
2976 error = EINVAL;
2977 break;
2979 level = mtod(m, u_int32_t *);
2980 error = 0;
2981 sctp_debug_on = (*level & (SCTP_DEBUG_ALL |
2982 SCTP_DEBUG_NOISY));
2983 kprintf("SETTING DEBUG LEVEL to %x\n",
2984 (u_int)sctp_debug_on);
2987 #else
2988 error = EOPNOTSUPP;
2989 #endif /* SCTP_DEBUG */
2990 break;
2991 case SCTP_EVENTS:
2993 struct sctp_event_subscribe *events;
2994 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
2995 error = EINVAL;
2996 break;
2998 SCTP_INP_WLOCK(inp);
2999 events = mtod(m, struct sctp_event_subscribe *);
3000 if (events->sctp_data_io_event) {
3001 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT;
3002 } else {
3003 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT;
3006 if (events->sctp_association_event) {
3007 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT;
3008 } else {
3009 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT;
3012 if (events->sctp_address_event) {
3013 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT;
3014 } else {
3015 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT;
3018 if (events->sctp_send_failure_event) {
3019 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3020 } else {
3021 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3024 if (events->sctp_peer_error_event) {
3025 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR;
3026 } else {
3027 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR;
3030 if (events->sctp_shutdown_event) {
3031 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3032 } else {
3033 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3036 if (events->sctp_partial_delivery_event) {
3037 inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT;
3038 } else {
3039 inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT;
3042 if (events->sctp_adaption_layer_event) {
3043 inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT;
3044 } else {
3045 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT;
3048 if (events->sctp_stream_reset_events) {
3049 inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3050 } else {
3051 inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3053 SCTP_INP_WUNLOCK(inp);
3055 break;
3057 case SCTP_ADAPTION_LAYER:
3059 struct sctp_setadaption *adap_bits;
3060 if ((size_t)m->m_len < sizeof(struct sctp_setadaption)) {
3061 error = EINVAL;
3062 break;
3064 SCTP_INP_WLOCK(inp);
3065 adap_bits = mtod(m, struct sctp_setadaption *);
3066 inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind;
3067 SCTP_INP_WUNLOCK(inp);
3069 break;
3070 case SCTP_SET_INITIAL_DBG_SEQ:
3072 u_int32_t *vvv;
3073 if ((size_t)m->m_len < sizeof(u_int32_t)) {
3074 error = EINVAL;
3075 break;
3077 SCTP_INP_WLOCK(inp);
3078 vvv = mtod(m, u_int32_t *);
3079 inp->sctp_ep.initial_sequence_debug = *vvv;
3080 SCTP_INP_WUNLOCK(inp);
3082 break;
3083 case SCTP_DEFAULT_SEND_PARAM:
3085 struct sctp_sndrcvinfo *s_info;
3087 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
3088 error = EINVAL;
3089 break;
3091 s_info = mtod(m, struct sctp_sndrcvinfo *);
3093 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3094 SCTP_INP_RLOCK(inp);
3095 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3096 if (stcb)
3097 SCTP_TCB_LOCK(stcb);
3098 SCTP_INP_RUNLOCK(inp);
3099 } else
3100 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
3102 if (stcb == NULL) {
3103 error = ENOENT;
3104 break;
3106 /* Validate things */
3107 if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) {
3108 SCTP_TCB_UNLOCK(stcb);
3109 error = EINVAL;
3110 break;
3112 /* Mask off the flags that are allowed */
3113 s_info->sinfo_flags = (s_info->sinfo_flags &
3114 (MSG_UNORDERED | MSG_ADDR_OVER |
3115 MSG_PR_SCTP_TTL | MSG_PR_SCTP_BUF));
3116 /* Copy it in */
3117 stcb->asoc.def_send = *s_info;
3118 SCTP_TCB_UNLOCK(stcb);
3120 break;
3121 case SCTP_PEER_ADDR_PARAMS:
3123 struct sctp_paddrparams *paddrp;
3124 struct sctp_nets *net;
3125 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
3126 error = EINVAL;
3127 break;
3129 paddrp = mtod(m, struct sctp_paddrparams *);
3130 net = NULL;
3131 if (paddrp->spp_assoc_id) {
3132 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3133 SCTP_INP_RLOCK(inp);
3134 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3135 if (stcb) {
3136 SCTP_TCB_LOCK(stcb);
3137 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3139 SCTP_INP_RUNLOCK(inp);
3140 } else
3141 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
3142 if (stcb == NULL) {
3143 error = ENOENT;
3144 break;
3148 if ((stcb == NULL) &&
3149 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
3150 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
3151 /* Lookup via address */
3152 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3153 SCTP_INP_RLOCK(inp);
3154 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3155 if (stcb) {
3156 SCTP_TCB_LOCK(stcb);
3157 net = sctp_findnet(stcb,
3158 (struct sockaddr *)&paddrp->spp_address);
3160 SCTP_INP_RUNLOCK(inp);
3161 } else {
3162 SCTP_INP_WLOCK(inp);
3163 SCTP_INP_INCR_REF(inp);
3164 SCTP_INP_WUNLOCK(inp);
3165 stcb = sctp_findassociation_ep_addr(&inp,
3166 (struct sockaddr *)&paddrp->spp_address,
3167 &net, NULL, NULL);
3168 if (stcb == NULL) {
3169 SCTP_INP_WLOCK(inp);
3170 SCTP_INP_DECR_REF(inp);
3171 SCTP_INP_WUNLOCK(inp);
3174 } else {
3175 /* Effects the Endpoint */
3176 stcb = NULL;
3178 if (stcb) {
3179 /* Applies to the specific association */
3180 if (paddrp->spp_pathmaxrxt) {
3181 if (net) {
3182 if (paddrp->spp_pathmaxrxt)
3183 net->failure_threshold = paddrp->spp_pathmaxrxt;
3184 } else {
3185 if (paddrp->spp_pathmaxrxt)
3186 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3189 if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) {
3190 /* Just a set */
3191 int old;
3192 if (net) {
3193 net->dest_state &= ~SCTP_ADDR_NOHB;
3194 } else {
3195 old = stcb->asoc.heart_beat_delay;
3196 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3197 if (old == 0) {
3198 /* Turn back on the timer */
3199 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3202 } else if (paddrp->spp_hbinterval == 0xffffffff) {
3203 /* on demand HB */
3204 sctp_send_hb(stcb, 1, net);
3205 } else {
3206 if (net == NULL) {
3207 /* off on association */
3208 if (stcb->asoc.heart_beat_delay) {
3209 int cnt_of_unconf = 0;
3210 struct sctp_nets *lnet;
3211 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3212 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3213 cnt_of_unconf++;
3216 /* stop the timer ONLY if we have no unconfirmed addresses
3218 if (cnt_of_unconf == 0)
3219 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3221 stcb->asoc.heart_beat_delay = 0;
3222 } else {
3223 net->dest_state |= SCTP_ADDR_NOHB;
3226 SCTP_TCB_UNLOCK(stcb);
3227 } else {
3228 /* Use endpoint defaults */
3229 SCTP_INP_WLOCK(inp);
3230 if (paddrp->spp_pathmaxrxt)
3231 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3232 if (paddrp->spp_hbinterval != SCTP_ISSUE_HB)
3233 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval;
3234 SCTP_INP_WUNLOCK(inp);
3237 break;
3238 case SCTP_RTOINFO:
3240 struct sctp_rtoinfo *srto;
3241 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
3242 error = EINVAL;
3243 break;
3245 srto = mtod(m, struct sctp_rtoinfo *);
3246 if (srto->srto_assoc_id == 0) {
3247 SCTP_INP_WLOCK(inp);
3248 /* If we have a null asoc, its default for the endpoint */
3249 if (srto->srto_initial > 10)
3250 inp->sctp_ep.initial_rto = srto->srto_initial;
3251 if (srto->srto_max > 10)
3252 inp->sctp_ep.sctp_maxrto = srto->srto_max;
3253 if (srto->srto_min > 10)
3254 inp->sctp_ep.sctp_minrto = srto->srto_min;
3255 SCTP_INP_WUNLOCK(inp);
3256 break;
3258 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3259 SCTP_INP_RLOCK(inp);
3260 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3261 if (stcb)
3262 SCTP_TCB_LOCK(stcb);
3263 SCTP_INP_RUNLOCK(inp);
3264 } else
3265 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
3266 if (stcb == NULL) {
3267 error = EINVAL;
3268 break;
3270 /* Set in ms we hope :-) */
3271 if (srto->srto_initial > 10)
3272 stcb->asoc.initial_rto = srto->srto_initial;
3273 if (srto->srto_max > 10)
3274 stcb->asoc.maxrto = srto->srto_max;
3275 if (srto->srto_min > 10)
3276 stcb->asoc.minrto = srto->srto_min;
3277 SCTP_TCB_UNLOCK(stcb);
3279 break;
3280 case SCTP_ASSOCINFO:
3282 struct sctp_assocparams *sasoc;
3284 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
3285 error = EINVAL;
3286 break;
3288 sasoc = mtod(m, struct sctp_assocparams *);
3289 if (sasoc->sasoc_assoc_id) {
3290 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3291 SCTP_INP_RLOCK(inp);
3292 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3293 if (stcb)
3294 SCTP_TCB_LOCK(stcb);
3295 SCTP_INP_RUNLOCK(inp);
3296 } else
3297 stcb = sctp_findassociation_ep_asocid(inp,
3298 sasoc->sasoc_assoc_id);
3299 if (stcb == NULL) {
3300 error = ENOENT;
3301 break;
3304 } else {
3305 stcb = NULL;
3307 if (stcb) {
3308 if (sasoc->sasoc_asocmaxrxt)
3309 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3310 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3311 sasoc->sasoc_peer_rwnd = 0;
3312 sasoc->sasoc_local_rwnd = 0;
3313 if (stcb->asoc.cookie_life)
3314 stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3315 SCTP_TCB_UNLOCK(stcb);
3316 } else {
3317 SCTP_INP_WLOCK(inp);
3318 if (sasoc->sasoc_asocmaxrxt)
3319 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3320 sasoc->sasoc_number_peer_destinations = 0;
3321 sasoc->sasoc_peer_rwnd = 0;
3322 sasoc->sasoc_local_rwnd = 0;
3323 if (sasoc->sasoc_cookie_life)
3324 inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3325 SCTP_INP_WUNLOCK(inp);
3328 break;
3329 case SCTP_INITMSG:
3331 struct sctp_initmsg *sinit;
3333 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
3334 error = EINVAL;
3335 break;
3337 sinit = mtod(m, struct sctp_initmsg *);
3338 SCTP_INP_WLOCK(inp);
3339 if (sinit->sinit_num_ostreams)
3340 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3342 if (sinit->sinit_max_instreams)
3343 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3345 if (sinit->sinit_max_attempts)
3346 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3348 if (sinit->sinit_max_init_timeo > 10)
3349 /* We must be at least a 100ms (we set in ticks) */
3350 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3351 SCTP_INP_WUNLOCK(inp);
3353 break;
3354 case SCTP_PRIMARY_ADDR:
3356 struct sctp_setprim *spa;
3357 struct sctp_nets *net, *lnet;
3358 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
3359 error = EINVAL;
3360 break;
3362 spa = mtod(m, struct sctp_setprim *);
3364 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3365 SCTP_INP_RLOCK(inp);
3366 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3367 if (stcb) {
3368 SCTP_TCB_LOCK(stcb);
3369 } else {
3370 error = EINVAL;
3371 break;
3373 SCTP_INP_RUNLOCK(inp);
3374 } else
3375 stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id);
3376 if (stcb == NULL) {
3377 /* One last shot */
3378 SCTP_INP_WLOCK(inp);
3379 SCTP_INP_INCR_REF(inp);
3380 SCTP_INP_WUNLOCK(inp);
3381 stcb = sctp_findassociation_ep_addr(&inp,
3382 (struct sockaddr *)&spa->ssp_addr,
3383 &net, NULL, NULL);
3384 if (stcb == NULL) {
3385 SCTP_INP_WLOCK(inp);
3386 SCTP_INP_DECR_REF(inp);
3387 SCTP_INP_WUNLOCK(inp);
3388 error = EINVAL;
3389 break;
3391 } else {
3392 /* find the net, associd or connected lookup type */
3393 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3394 if (net == NULL) {
3395 SCTP_TCB_UNLOCK(stcb);
3396 error = EINVAL;
3397 break;
3400 if ((net != stcb->asoc.primary_destination) &&
3401 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3402 /* Ok we need to set it */
3403 lnet = stcb->asoc.primary_destination;
3404 lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq;
3405 if (sctp_set_primary_addr(stcb,
3406 (struct sockaddr *)NULL,
3407 net) == 0) {
3408 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3409 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3411 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3414 SCTP_TCB_UNLOCK(stcb);
3416 break;
3418 case SCTP_SET_PEER_PRIMARY_ADDR:
3420 struct sctp_setpeerprim *sspp;
3421 if ((size_t)m->m_len < sizeof(struct sctp_setpeerprim)) {
3422 error = EINVAL;
3423 break;
3425 sspp = mtod(m, struct sctp_setpeerprim *);
3428 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3429 SCTP_INP_RLOCK(inp);
3430 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3431 if (stcb)
3432 SCTP_TCB_UNLOCK(stcb);
3433 SCTP_INP_RUNLOCK(inp);
3434 } else
3435 stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id);
3436 if (stcb == NULL) {
3437 error = EINVAL;
3438 break;
3440 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3441 error = EINVAL;
3443 SCTP_TCB_UNLOCK(stcb);
3445 break;
3446 case SCTP_BINDX_ADD_ADDR:
3448 struct sctp_getaddresses *addrs;
3449 struct sockaddr *addr_touse;
3450 struct sockaddr_in sin;
3451 /* see if we're bound all already! */
3452 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3453 error = EINVAL;
3454 break;
3456 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
3457 error = EINVAL;
3458 break;
3460 addrs = mtod(m, struct sctp_getaddresses *);
3461 addr_touse = addrs->addr;
3462 if (addrs->addr->sa_family == AF_INET6) {
3463 struct sockaddr_in6 *sin6;
3464 sin6 = (struct sockaddr_in6 *)addr_touse;
3465 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3466 in6_sin6_2_sin(&sin, sin6);
3467 addr_touse = (struct sockaddr *)&sin;
3470 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3471 if (p == NULL) {
3472 /* Can't get proc for Net/Open BSD */
3473 error = EINVAL;
3474 break;
3476 error = sctp_inpcb_bind(so, addr_touse, p);
3477 break;
3479 /* No locks required here since bind and mgmt_ep_sa all
3480 * do their own locking. If we do something for the FIX:
3481 * below we may need to lock in that case.
3483 if (addrs->sget_assoc_id == 0) {
3484 /* add the address */
3485 struct sctp_inpcb *lep;
3486 ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3487 lep = sctp_pcb_findep(addr_touse, 1, 0);
3488 if (lep != NULL) {
3489 /* We must decrement the refcount
3490 * since we have the ep already and
3491 * are binding. No remove going on
3492 * here.
3494 SCTP_INP_WLOCK(inp);
3495 SCTP_INP_DECR_REF(inp);
3496 SCTP_INP_WUNLOCK(inp);
3498 if (lep == inp) {
3499 /* already bound to it.. ok */
3500 break;
3501 } else if (lep == NULL) {
3502 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
3503 error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3504 SCTP_ADD_IP_ADDRESS);
3505 } else {
3506 error = EADDRNOTAVAIL;
3508 if (error)
3509 break;
3511 } else {
3512 /* FIX: decide whether we allow assoc based bindx */
3515 break;
3516 case SCTP_BINDX_REM_ADDR:
3518 struct sctp_getaddresses *addrs;
3519 struct sockaddr *addr_touse;
3520 struct sockaddr_in sin;
3521 /* see if we're bound all already! */
3522 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3523 error = EINVAL;
3524 break;
3526 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
3527 error = EINVAL;
3528 break;
3530 addrs = mtod(m, struct sctp_getaddresses *);
3531 addr_touse = addrs->addr;
3532 if (addrs->addr->sa_family == AF_INET6) {
3533 struct sockaddr_in6 *sin6;
3534 sin6 = (struct sockaddr_in6 *)addr_touse;
3535 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3536 in6_sin6_2_sin(&sin, sin6);
3537 addr_touse = (struct sockaddr *)&sin;
3540 /* No lock required mgmt_ep_sa does its own locking. If
3541 * the FIX: below is ever changed we may need to
3542 * lock before calling association level binding.
3544 if (addrs->sget_assoc_id == 0) {
3545 /* delete the address */
3546 sctp_addr_mgmt_ep_sa(inp, addr_touse,
3547 SCTP_DEL_IP_ADDRESS);
3548 } else {
3549 /* FIX: decide whether we allow assoc based bindx */
3552 break;
3553 default:
3554 error = ENOPROTOOPT;
3555 break;
3556 } /* end switch (opt) */
3557 return (error);
3561 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
3563 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3565 struct mbuf *m = NULL;
3566 struct sctp_inpcb *inp;
3567 int error;
3569 inp = (struct sctp_inpcb *)so->so_pcb;
3570 crit_enter();
3571 if (inp == 0) {
3572 crit_exit();
3573 /* I made the same as TCP since we are not setup? */
3574 return (ECONNRESET);
3576 if (sopt->sopt_level != IPPROTO_SCTP) {
3577 /* wrong proto level... send back up to IP */
3578 #ifdef INET6
3579 if (INP_CHECK_SOCKAF(so, AF_INET6))
3580 error = ip6_ctloutput(so, sopt);
3581 else
3582 #endif /* INET6 */
3583 error = ip_ctloutput(so, sopt);
3584 crit_exit();
3585 return (error);
3587 if (sopt->sopt_valsize > MCLBYTES) {
3589 * Restrict us down to a cluster size, that's all we can
3590 * pass either way...
3592 sopt->sopt_valsize = MCLBYTES;
3594 if (sopt->sopt_valsize) {
3596 m = m_get(MB_WAIT, MT_DATA);
3597 if (sopt->sopt_valsize > MLEN) {
3598 MCLGET(m, MB_DONTWAIT);
3599 if ((m->m_flags & M_EXT) == 0) {
3600 sctp_m_freem(m);
3601 crit_exit();
3602 return (ENOBUFS);
3605 error = sooptcopyin(sopt, mtod(m, caddr_t), sopt->sopt_valsize,
3606 sopt->sopt_valsize);
3607 if (error) {
3608 m_free(m);
3609 goto out;
3611 m->m_len = sopt->sopt_valsize;
3613 if (sopt->sopt_dir == SOPT_SET) {
3614 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3615 error = sctp_optsset(so, sopt->sopt_name, &m, sopt->sopt_td);
3616 #else
3617 error = sctp_optsset(so, sopt->sopt_name, &m, sopt->sopt_p);
3618 #endif
3619 } else if (sopt->sopt_dir == SOPT_GET) {
3620 #if (defined (__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3621 error = sctp_optsget(so, sopt->sopt_name, &m, sopt->sopt_td);
3622 #else
3623 error = sctp_optsget(so, sopt->sopt_name, &m, sopt->sopt_p);
3624 #endif
3625 } else {
3626 error = EINVAL;
3628 if ( (error == 0) && (m != NULL)) {
3629 error = sooptcopyout(sopt, mtod(m, caddr_t), m->m_len);
3630 sctp_m_freem(m);
3631 } else if (m != NULL) {
3632 sctp_m_freem(m);
3634 out:
3635 crit_exit();
3636 return (error);
3639 #else
3640 /* NetBSD and OpenBSD */
3642 sctp_ctloutput(int op, struct socket *so, int level, int optname,
3643 struct mbuf **mp)
3645 int s, error;
3646 struct inpcb *inp;
3647 #ifdef INET6
3648 struct in6pcb *in6p;
3649 #endif
3650 int family; /* family of the socket */
3652 family = so->so_proto->pr_domain->dom_family;
3653 error = 0;
3654 crit_exit();
3655 switch (family) {
3656 case PF_INET:
3657 inp = sotoinpcb(so);
3658 #ifdef INET6
3659 in6p = NULL;
3660 #endif
3661 break;
3662 #ifdef INET6
3663 case PF_INET6:
3664 inp = NULL;
3665 in6p = sotoin6pcb(so);
3666 break;
3667 #endif
3668 default:
3669 crit_exit();
3670 return EAFNOSUPPORT;
3672 #ifndef INET6
3673 if (inp == NULL)
3674 #else
3675 if (inp == NULL && in6p == NULL)
3676 #endif
3678 crit_exit();
3679 if (op == PRCO_SETOPT && *mp)
3680 m_free(*mp);
3681 return (ECONNRESET);
3683 if (level != IPPROTO_SCTP) {
3684 switch (family) {
3685 case PF_INET:
3686 error = ip_ctloutput(op, so, level, optname, mp);
3687 break;
3688 #ifdef INET6
3689 case PF_INET6:
3690 error = ip6_ctloutput(op, so, level, optname, mp);
3691 break;
3692 #endif
3694 crit_exit();
3695 return (error);
3697 /* Ok if we reach here it is a SCTP option we hope */
3698 if (op == PRCO_SETOPT) {
3699 error = sctp_optsset(so, optname, mp, (struct proc *)NULL);
3700 if (*mp)
3701 m_free(*mp);
3702 } else if (op == PRCO_GETOPT) {
3703 error = sctp_optsget(so, optname, mp, (struct proc *)NULL);
3704 } else {
3705 error = EINVAL;
3707 crit_exit();
3708 return (error);
3711 #endif
3713 static int
3714 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3715 sctp_connect(struct socket *so, struct sockaddr *addr, struct thread *p)
3717 #else
3718 #if defined(__FreeBSD__) || defined(__APPLE__)
3719 sctp_connect(struct socket *so, struct sockaddr *addr, struct proc *p)
3721 #else
3722 sctp_connect(struct socket *so, struct mbuf *nam, struct proc *p)
3724 struct sockaddr *addr = mtod(nam, struct sockaddr *);
3725 #endif
3726 #endif
3727 int error = 0;
3728 struct sctp_inpcb *inp;
3729 struct sctp_tcb *stcb;
3731 #ifdef SCTP_DEBUG
3732 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3733 kprintf("Connect called in SCTP to ");
3734 sctp_print_address(addr);
3735 kprintf("Port %d\n", ntohs(((struct sockaddr_in *)addr)->sin_port));
3737 #endif /* SCTP_DEBUG */
3738 crit_enter();
3739 inp = (struct sctp_inpcb *)so->so_pcb;
3740 if (inp == 0) {
3741 crit_exit();
3742 /* I made the same as TCP since we are not setup? */
3743 return (ECONNRESET);
3745 SCTP_ASOC_CREATE_LOCK(inp);
3746 SCTP_INP_WLOCK(inp);
3747 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3748 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3749 /* Should I really unlock ? */
3750 SCTP_INP_WUNLOCK(inp);
3751 SCTP_ASOC_CREATE_UNLOCK(inp);
3752 crit_exit();
3753 return (EFAULT);
3755 #ifdef INET6
3756 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3757 (addr->sa_family == AF_INET6)) {
3758 SCTP_INP_WUNLOCK(inp);
3759 SCTP_ASOC_CREATE_UNLOCK(inp);
3760 crit_exit();
3761 return (EINVAL);
3763 #endif /* INET6 */
3764 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3765 SCTP_PCB_FLAGS_UNBOUND) {
3766 /* Bind a ephemeral port */
3767 SCTP_INP_WUNLOCK(inp);
3768 error = sctp_inpcb_bind(so, NULL, p);
3769 if (error) {
3770 SCTP_ASOC_CREATE_UNLOCK(inp);
3771 crit_exit();
3772 return (error);
3774 SCTP_INP_WLOCK(inp);
3776 /* Now do we connect? */
3777 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3778 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3779 /* We are already connected AND the TCP model */
3780 crit_exit();
3781 SCTP_INP_WUNLOCK(inp);
3782 SCTP_ASOC_CREATE_UNLOCK(inp);
3783 return (EADDRINUSE);
3785 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3786 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3787 if (stcb)
3788 SCTP_TCB_UNLOCK(stcb);
3789 SCTP_INP_WUNLOCK(inp);
3790 } else {
3791 SCTP_INP_INCR_REF(inp);
3792 SCTP_INP_WUNLOCK(inp);
3793 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3794 if (stcb == NULL) {
3795 SCTP_INP_WLOCK(inp);
3796 SCTP_INP_DECR_REF(inp);
3797 SCTP_INP_WUNLOCK(inp);
3800 if (stcb != NULL) {
3801 /* Already have or am bring up an association */
3802 SCTP_ASOC_CREATE_UNLOCK(inp);
3803 SCTP_TCB_UNLOCK(stcb);
3804 crit_exit();
3805 return (EALREADY);
3807 /* We are GOOD to go */
3808 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
3809 if (stcb == NULL) {
3810 /* Gak! no memory */
3811 crit_exit();
3812 return (error);
3814 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3815 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3816 /* Set the connected flag so we can queue data */
3817 soisconnecting(so);
3819 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3820 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3821 sctp_send_initiate(inp, stcb);
3822 SCTP_ASOC_CREATE_UNLOCK(inp);
3823 SCTP_TCB_UNLOCK(stcb);
3824 crit_exit();
3825 return error;
3829 sctp_usr_recvd(struct socket *so, int flags)
3831 struct sctp_socket_q_list *sq=NULL;
3833 * The user has received some data, we may be able to stuff more
3834 * up the socket. And we need to possibly update the rwnd.
3836 struct sctp_inpcb *inp;
3837 struct sctp_tcb *stcb=NULL;
3839 inp = (struct sctp_inpcb *)so->so_pcb;
3840 #ifdef SCTP_DEBUG
3841 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3842 kprintf("Read for so:%x inp:%x Flags:%x\n",
3843 (u_int)so, (u_int)inp, (u_int)flags);
3844 #endif
3846 if (inp == 0) {
3847 /* I made the same as TCP since we are not setup? */
3848 #ifdef SCTP_DEBUG
3849 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3850 kprintf("Nope, connection reset\n");
3851 #endif
3852 return (ECONNRESET);
3854 crit_enter();
3856 * Grab the first one on the list. It will re-insert itself if
3857 * it runs out of room
3859 SCTP_INP_WLOCK(inp);
3860 if ((flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3861 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3862 /* Ok the other part of our grubby tracking
3863 * stuff for our horrible layer violation that
3864 * the tsvwg thinks is ok for sctp_peeloff.. gak!
3865 * We must update the next vtag pending on the
3866 * socket buffer (if any).
3868 inp->sctp_vtag_first = sctp_get_first_vtag_from_sb(so);
3869 sq = TAILQ_FIRST(&inp->sctp_queue_list);
3870 if (sq) {
3871 stcb = sq->tcb;
3872 } else {
3873 stcb = NULL;
3875 } else {
3876 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3878 if (stcb)
3879 SCTP_TCB_LOCK(stcb);
3880 if (stcb) {
3881 long incr;
3882 /* all code in normal stcb path assumes
3883 * that you have a tcb_lock only. Thus
3884 * we must release the inp write lock.
3886 if (flags & MSG_EOR) {
3887 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3888 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3889 stcb = sctp_remove_from_socket_q(inp);
3891 #ifdef SCTP_DEBUG
3892 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3893 kprintf("remove from socket queue for inp:%x tcbret:%x\n",
3894 (u_int)inp, (u_int)stcb);
3895 #endif
3897 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3898 sizeof(struct mbuf));
3899 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) {
3900 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3901 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)));
3904 if ((TAILQ_EMPTY(&stcb->asoc.delivery_queue) == 0) ||
3905 (TAILQ_EMPTY(&stcb->asoc.reasmqueue) == 0)) {
3906 /* Deliver if there is something to be delivered */
3907 sctp_service_queues(stcb, &stcb->asoc, 1);
3909 sctp_set_rwnd(stcb, &stcb->asoc);
3910 /* if we increase by 1 or more MTU's (smallest MTUs of all
3911 * nets) we send a window update sack
3913 incr = stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd;
3914 if (incr < 0) {
3915 incr = 0;
3917 if (((uint32_t)incr >= (stcb->asoc.smallest_mtu * SCTP_SEG_TO_RWND_UPD)) ||
3918 ((((uint32_t)incr)*SCTP_SCALE_OF_RWND_TO_UPD) >= so->so_rcv.ssb_hiwat)) {
3919 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
3920 /* If the timer is up, stop it */
3921 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
3922 stcb->sctp_ep, stcb, NULL);
3924 /* Send the sack, with the new rwnd */
3925 sctp_send_sack(stcb);
3926 /* Now do the output */
3927 sctp_chunk_output(inp, stcb, 10);
3929 } else {
3930 if ((( sq ) && (flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0))
3931 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3932 stcb = sctp_remove_from_socket_q(inp);
3935 SOCKBUF_LOCK(&so->so_rcv);
3936 if (( so->so_rcv.ssb_mb == NULL ) &&
3937 (TAILQ_EMPTY(&inp->sctp_queue_list) == 0)) {
3938 int sq_cnt=0;
3939 #ifdef SCTP_DEBUG
3940 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3941 kprintf("Something off, inp:%x so->so_rcv->ssb_mb is empty and sockq is not.. cleaning\n",
3942 (u_int)inp);
3943 #endif
3944 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3945 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3946 int done_yet;
3947 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3948 while (!done_yet) {
3949 sq_cnt++;
3950 sctp_remove_from_socket_q(inp);
3951 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3954 #ifdef SCTP_DEBUG
3955 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3956 kprintf("Cleaned up %d sockq's\n", sq_cnt);
3957 #endif
3959 SOCKBUF_UNLOCK(&so->so_rcv);
3960 if (stcb)
3961 SCTP_TCB_UNLOCK(stcb);
3962 SCTP_INP_WUNLOCK(inp);
3963 crit_exit();
3964 return (0);
3968 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3969 sctp_listen(struct socket *so, struct thread *p)
3970 #else
3971 sctp_listen(struct socket *so, struct proc *p)
3972 #endif
3975 * Note this module depends on the protocol processing being
3976 * called AFTER any socket level flags and backlog are applied
3977 * to the socket. The traditional way that the socket flags are
3978 * applied is AFTER protocol processing. We have made a change
3979 * to the sys/kern/uipc_socket.c module to reverse this but this
3980 * MUST be in place if the socket API for SCTP is to work properly.
3982 int error = 0;
3983 struct sctp_inpcb *inp;
3985 crit_enter();
3986 inp = (struct sctp_inpcb *)so->so_pcb;
3987 if (inp == 0) {
3988 crit_exit();
3989 /* I made the same as TCP since we are not setup? */
3990 return (ECONNRESET);
3992 SCTP_INP_RLOCK(inp);
3993 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3994 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3995 /* We are already connected AND the TCP model */
3996 crit_exit();
3997 SCTP_INP_RUNLOCK(inp);
3998 return (EADDRINUSE);
4000 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
4001 /* We must do a bind. */
4002 SCTP_INP_RUNLOCK(inp);
4003 if ((error = sctp_inpcb_bind(so, NULL, p))) {
4004 /* bind error, probably perm */
4005 crit_exit();
4006 return (error);
4008 } else {
4009 SCTP_INP_RUNLOCK(inp);
4011 SOCK_LOCK(so);
4012 SCTP_INP_WLOCK(inp);
4013 if (inp->sctp_socket->so_qlimit) {
4014 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
4016 * For the UDP model we must TURN OFF the ACCEPT
4017 * flags since we do NOT allow the accept() call.
4018 * The TCP model (when present) will do accept which
4019 * then prohibits connect().
4021 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
4023 inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
4024 } else {
4025 if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
4027 * Turning off the listen flags if the backlog is
4028 * set to 0 (i.e. qlimit is 0).
4030 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
4032 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
4034 SCTP_INP_WUNLOCK(inp);
4035 SOCK_UNLOCK(so);
4036 crit_exit();
4037 return (error);
4041 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4042 sctp_accept(struct socket *so, struct sockaddr **addr)
4044 #else
4045 sctp_accept(struct socket *so, struct mbuf *nam)
4047 struct sockaddr *addr = mtod(nam, struct sockaddr *);
4048 #endif
4049 struct sctp_tcb *stcb;
4050 struct sockaddr *prim;
4051 struct sctp_inpcb *inp;
4053 crit_enter();
4054 inp = (struct sctp_inpcb *)so->so_pcb;
4056 if (inp == 0) {
4057 crit_exit();
4058 return (ECONNRESET);
4060 SCTP_INP_RLOCK(inp);
4061 if (so->so_state & SS_ISDISCONNECTED) {
4062 crit_exit();
4063 SCTP_INP_RUNLOCK(inp);
4064 return (ECONNABORTED);
4066 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4067 if (stcb == NULL) {
4068 crit_exit();
4069 SCTP_INP_RUNLOCK(inp);
4070 return (ECONNRESET);
4072 SCTP_TCB_LOCK(stcb);
4073 SCTP_INP_RUNLOCK(inp);
4074 prim = (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr;
4075 if (prim->sa_family == AF_INET) {
4076 struct sockaddr_in *sin;
4077 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4078 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME,
4079 M_WAITOK | M_ZERO);
4080 #else
4081 sin = (struct sockaddr_in *)addr;
4082 bzero((caddr_t)sin, sizeof (*sin));
4083 #endif
4084 sin->sin_family = AF_INET;
4085 sin->sin_len = sizeof(*sin);
4086 sin->sin_port = ((struct sockaddr_in *)prim)->sin_port;
4087 sin->sin_addr = ((struct sockaddr_in *)prim)->sin_addr;
4088 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4089 *addr = (struct sockaddr *)sin;
4090 #else
4091 nam->m_len = sizeof(*sin);
4092 #endif
4093 } else {
4094 struct sockaddr_in6 *sin6;
4095 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4096 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME,
4097 M_WAITOK | M_ZERO);
4098 #else
4099 sin6 = (struct sockaddr_in6 *)addr;
4100 #endif
4101 bzero((caddr_t)sin6, sizeof (*sin6));
4102 sin6->sin6_family = AF_INET6;
4103 sin6->sin6_len = sizeof(*sin6);
4104 sin6->sin6_port = ((struct sockaddr_in6 *)prim)->sin6_port;
4106 sin6->sin6_addr = ((struct sockaddr_in6 *)prim)->sin6_addr;
4107 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
4108 /* sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);*/
4109 in6_recoverscope(sin6, &sin6->sin6_addr, NULL); /* skip ifp check */
4110 else
4111 sin6->sin6_scope_id = 0; /*XXX*/
4112 #if defined(__FreeBSD__) || defined (__APPLE__) || defined(__DragonFly__)
4113 *addr= (struct sockaddr *)sin6;
4114 #else
4115 nam->m_len = sizeof(*sin6);
4116 #endif
4118 /* Wake any delayed sleep action */
4119 SCTP_TCB_UNLOCK(stcb);
4120 SCTP_INP_WLOCK(inp);
4121 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4122 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4123 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4124 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4125 #if defined(__NetBSD__)
4126 if (sowritable(inp->sctp_socket))
4127 sowwakeup(inp->sctp_socket);
4128 #else
4129 if (sowriteable(inp->sctp_socket))
4130 sowwakeup(inp->sctp_socket);
4131 #endif
4133 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4134 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4135 if (soreadable(inp->sctp_socket))
4136 sorwakeup(inp->sctp_socket);
4140 SCTP_INP_WUNLOCK(inp);
4141 crit_exit();
4142 return (0);
4146 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4147 sctp_ingetaddr(struct socket *so, struct sockaddr **addr)
4148 #else
4149 sctp_ingetaddr(struct socket *so, struct mbuf *nam)
4150 #endif
4152 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4153 struct sockaddr_in *sin;
4154 #else
4155 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
4156 #endif
4157 struct sctp_inpcb *inp;
4159 * Do the malloc first in case it blocks.
4161 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4162 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK |
4163 M_ZERO);
4164 #else
4165 nam->m_len = sizeof(*sin);
4166 memset(sin, 0, sizeof(*sin));
4167 #endif
4168 sin->sin_family = AF_INET;
4169 sin->sin_len = sizeof(*sin);
4170 crit_enter();
4171 inp = (struct sctp_inpcb *)so->so_pcb;
4172 if (!inp) {
4173 crit_exit();
4174 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4175 FREE(sin, M_SONAME);
4176 #endif
4177 return ECONNRESET;
4179 SCTP_INP_RLOCK(inp);
4180 sin->sin_port = inp->sctp_lport;
4181 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4182 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4183 struct sctp_tcb *stcb;
4184 struct sockaddr_in *sin_a;
4185 struct sctp_nets *net;
4186 int fnd;
4188 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4189 if (stcb == NULL) {
4190 goto notConn;
4192 fnd = 0;
4193 sin_a = NULL;
4194 SCTP_TCB_LOCK(stcb);
4195 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4196 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4197 if (sin_a->sin_family == AF_INET) {
4198 fnd = 1;
4199 break;
4202 if ((!fnd) || (sin_a == NULL)) {
4203 /* punt */
4204 SCTP_TCB_UNLOCK(stcb);
4205 goto notConn;
4207 sin->sin_addr = sctp_ipv4_source_address_selection(inp,
4208 stcb, (struct route *)&net->ro, net, 0);
4209 SCTP_TCB_UNLOCK(stcb);
4210 } else {
4211 /* For the bound all case you get back 0 */
4212 notConn:
4213 sin->sin_addr.s_addr = 0;
4216 } else {
4217 /* Take the first IPv4 address in the list */
4218 struct sctp_laddr *laddr;
4219 int fnd = 0;
4220 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4221 if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
4222 struct sockaddr_in *sin_a;
4223 sin_a = (struct sockaddr_in *)laddr->ifa->ifa_addr;
4224 sin->sin_addr = sin_a->sin_addr;
4225 fnd = 1;
4226 break;
4229 if (!fnd) {
4230 crit_exit();
4231 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4232 FREE(sin, M_SONAME);
4233 #endif
4234 SCTP_INP_RUNLOCK(inp);
4235 return ENOENT;
4238 SCTP_INP_RUNLOCK(inp);
4239 crit_exit();
4240 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4241 (*addr) = (struct sockaddr *)sin;
4242 #endif
4243 return (0);
4247 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4248 sctp_peeraddr(struct socket *so, struct sockaddr **addr)
4250 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4251 #else
4252 sctp_peeraddr(struct socket *so, struct mbuf *nam)
4254 struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
4255 #endif
4256 int fnd;
4257 struct sockaddr_in *sin_a;
4258 struct sctp_inpcb *inp;
4259 struct sctp_tcb *stcb;
4260 struct sctp_nets *net;
4262 /* Do the malloc first in case it blocks. */
4263 inp = (struct sctp_inpcb *)so->so_pcb;
4264 if ((inp == NULL) ||
4265 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4266 /* UDP type and listeners will drop out here */
4267 return (ENOTCONN);
4269 crit_enter();
4271 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4272 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK |
4273 M_ZERO);
4274 #else
4275 nam->m_len = sizeof(*sin);
4276 memset(sin, 0, sizeof(*sin));
4277 #endif
4278 sin->sin_family = AF_INET;
4279 sin->sin_len = sizeof(*sin);
4281 /* We must recapture incase we blocked */
4282 inp = (struct sctp_inpcb *)so->so_pcb;
4283 if (!inp) {
4284 crit_exit();
4285 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4286 FREE(sin, M_SONAME);
4287 #endif
4288 return ECONNRESET;
4290 SCTP_INP_RLOCK(inp);
4291 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4292 if (stcb)
4293 SCTP_TCB_LOCK(stcb);
4294 SCTP_INP_RUNLOCK(inp);
4295 if (stcb == NULL) {
4296 crit_exit();
4297 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4298 FREE(sin, M_SONAME);
4299 #endif
4300 return ECONNRESET;
4302 fnd = 0;
4303 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4304 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4305 if (sin_a->sin_family == AF_INET) {
4306 fnd = 1;
4307 sin->sin_port = stcb->rport;
4308 sin->sin_addr = sin_a->sin_addr;
4309 break;
4312 SCTP_TCB_UNLOCK(stcb);
4313 if (!fnd) {
4314 /* No IPv4 address */
4315 crit_exit();
4316 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4317 FREE(sin, M_SONAME);
4318 #endif
4319 return ENOENT;
4321 crit_exit();
4322 return (0);
4325 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4326 struct pr_usrreqs sctp_usrreqs = {
4327 .pru_abort = sctp_abort,
4328 .pru_accept = sctp_accept,
4329 .pru_attach = sctp_attach,
4330 .pru_bind = sctp_bind,
4331 .pru_connect = sctp_connect,
4332 .pru_connect2 = pru_connect2_notsupp,
4333 .pru_control = in_control,
4334 .pru_detach = sctp_detach,
4335 .pru_disconnect = sctp_disconnect,
4336 .pru_listen = sctp_listen,
4337 .pru_peeraddr = sctp_peeraddr,
4338 .pru_rcvd = sctp_usr_recvd,
4339 .pru_rcvoob = pru_rcvoob_notsupp,
4340 .pru_send = sctp_send,
4341 .pru_sense = pru_sense_null,
4342 .pru_shutdown = sctp_shutdown,
4343 .pru_sockaddr = sctp_ingetaddr,
4344 .pru_sosend = sctp_sosend,
4345 .pru_soreceive = soreceive,
4346 .pru_sopoll = sopoll
4349 #else
4350 #if defined(__NetBSD__)
4352 sctp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
4353 struct mbuf *control, struct proc *p)
4355 #else
4357 sctp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
4358 struct mbuf *control)
4360 struct proc *p = curproc;
4361 #endif
4362 int error = 0;
4363 int family;
4365 family = so->so_proto->pr_domain->dom_family;
4367 crit_enter();
4368 if (req == PRU_CONTROL) {
4369 switch (family) {
4370 case PF_INET:
4371 error = in_control(so, (long)m, (caddr_t)nam,
4372 (struct ifnet *)control
4373 #if defined(__NetBSD__)
4375 #endif
4377 break;
4378 #ifdef INET6
4379 case PF_INET6:
4380 error = in6_control(so, (long)m, (caddr_t)nam,
4381 (struct ifnet *)control, p);
4382 break;
4383 #endif
4384 default:
4385 error = EAFNOSUPPORT;
4387 crit_exit();
4388 return (error);
4390 #ifdef __NetBSD__
4391 if (req == PRU_PURGEIF) {
4392 struct ifnet *ifn;
4393 struct ifaddr *ifa;
4394 ifn = (struct ifnet *)control;
4395 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
4396 if (ifa->ifa_addr->sa_family == family) {
4397 sctp_delete_ip_address(ifa);
4400 switch (family) {
4401 case PF_INET:
4402 in_purgeif (ifn);
4403 break;
4404 #ifdef INET6
4405 case PF_INET6:
4406 in6_purgeif (ifn);
4407 break;
4408 #endif /* INET6 */
4409 default:
4410 crit_exit();
4411 return (EAFNOSUPPORT);
4413 crit_exit();
4414 return (0);
4416 #endif
4417 switch (req) {
4418 case PRU_ATTACH:
4419 error = sctp_attach(so, family, p);
4420 break;
4421 case PRU_DETACH:
4422 error = sctp_detach(so);
4423 break;
4424 case PRU_BIND:
4425 if (nam == NULL) {
4426 crit_exit();
4427 return (EINVAL);
4429 error = sctp_bind(so, nam, p);
4430 break;
4431 case PRU_LISTEN:
4432 error = sctp_listen(so, p);
4433 break;
4434 case PRU_CONNECT:
4435 if (nam == NULL) {
4436 crit_exit();
4437 return (EINVAL);
4439 error = sctp_connect(so, nam, p);
4440 break;
4441 case PRU_DISCONNECT:
4442 error = sctp_disconnect(so);
4443 break;
4444 case PRU_ACCEPT:
4445 if (nam == NULL) {
4446 crit_exit();
4447 return (EINVAL);
4449 error = sctp_accept(so, nam);
4450 break;
4451 case PRU_SHUTDOWN:
4452 error = sctp_shutdown(so);
4453 break;
4455 case PRU_RCVD:
4457 * For Open and Net BSD, this is real
4458 * ugly. The mbuf *nam that is passed
4459 * (by soreceive()) is the int flags c
4460 * ast as a (mbuf *) yuck!
4462 error = sctp_usr_recvd(so, (int)((long)nam));
4463 break;
4465 case PRU_SEND:
4466 /* Flags are ignored */
4467 #ifdef SCTP_DEBUG
4468 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
4469 kprintf("Send called on V4 side\n");
4471 #endif
4473 struct sockaddr *addr;
4474 if (nam == NULL)
4475 addr = NULL;
4476 else
4477 addr = mtod(nam, struct sockaddr *);
4479 error = sctp_send(so, 0, m, addr, control, p);
4481 break;
4482 case PRU_ABORT:
4483 error = sctp_abort(so);
4484 break;
4486 case PRU_SENSE:
4487 error = 0;
4488 break;
4489 case PRU_RCVOOB:
4490 error = EAFNOSUPPORT;
4491 break;
4492 case PRU_SENDOOB:
4493 error = EAFNOSUPPORT;
4494 break;
4495 case PRU_PEERADDR:
4496 error = sctp_peeraddr(so, nam);
4497 break;
4498 case PRU_SOCKADDR:
4499 error = sctp_ingetaddr(so, nam);
4500 break;
4501 case PRU_SLOWTIMO:
4502 error = 0;
4503 break;
4504 default:
4505 break;
4507 crit_exit();
4508 return (error);
4510 #endif
4512 /* #if defined(__NetBSD__) || defined(__OpenBSD__) */
4513 #if __OpenBSD__
4515 * Sysctl for sctp variables.
4518 sctp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
4519 size_t newlen)
4522 /* All sysctl names at this level are terminal. */
4523 if (namelen != 1)
4524 return (ENOTDIR);
4525 sysctl_int();
4527 switch (name[0]) {
4528 case SCTPCTL_MAXDGRAM:
4529 return (sysctl_int(oldp, oldlenp, newp, newlen,
4530 &sctp_sendspace));
4531 case SCTPCTL_RECVSPACE:
4532 return (sysctl_int(oldp, oldlenp, newp, newlen,
4533 &sctp_recvspace));
4534 case SCTPCTL_AUTOASCONF:
4535 return (sysctl_int(oldp, oldlenp, newp, newlen,
4536 &sctp_auto_asconf));
4537 case SCTPCTL_ECN_ENABLE:
4538 return (sysctl_int(oldp, oldlenp, newp, newlen,
4539 &sctp_ecn));
4540 case SCTPCTL_ECN_NONCE:
4541 return (sysctl_int(oldp, oldlenp, newp, newlen,
4542 &sctp_ecn_nonce));
4543 case SCTPCTL_STRICT_SACK:
4544 return (sysctl_int(oldp, oldlenp, newp, newlen,
4545 &sctp_strict_sacks));
4546 case SCTPCTL_NOCSUM_LO:
4547 return (sysctl_int(oldp, oldlenp, newp, newlen,
4548 &sctp_no_csum_on_loopback));
4549 case SCTPCTL_STRICT_INIT:
4550 return (sysctl_int(oldp, oldlenp, newp, newlen,
4551 &sctp_strict_init));
4552 case SCTPCTL_PEER_CHK_OH:
4553 return (sysctl_int(oldp, oldlenp, newp, newlen,
4554 &sctp_peer_chunk_oh));
4555 case SCTPCTL_MAXBURST:
4556 return (sysctl_int(oldp, oldlenp, newp, newlen,
4557 &sctp_max_burst_default));
4558 case SCTPCTL_MAXCHUNKONQ:
4559 return (sysctl_int(oldp, oldlenp, newp, newlen,
4560 &sctp_max_chunks_on_queue));
4561 case SCTPCTL_DELAYED_SACK:
4562 return (sysctl_int(oldp, oldlenp, newp, newlen,
4563 &sctp_delayed_sack_time_default));
4564 case SCTPCTL_HB_INTERVAL:
4565 return (sysctl_int(oldp, oldlenp, newp, newlen,
4566 &sctp_heartbeat_interval_default));
4567 case SCTPCTL_PMTU_RAISE:
4568 return (sysctl_int(oldp, oldlenp, newp, newlen,
4569 &sctp_pmtu_raise_time_default));
4570 case SCTPCTL_SHUTDOWN_GUARD:
4571 return (sysctl_int(oldp, oldlenp, newp, newlen,
4572 &sctp_shutdown_guard_time_default));
4573 case SCTPCTL_SECRET_LIFETIME:
4574 return (sysctl_int(oldp, oldlenp, newp, newlen,
4575 &sctp_secret_lifetime_default));
4576 case SCTPCTL_RTO_MAX:
4577 return (sysctl_int(oldp, oldlenp, newp, newlen,
4578 &sctp_rto_max_default));
4579 case SCTPCTL_RTO_MIN:
4580 return (sysctl_int(oldp, oldlenp, newp, newlen,
4581 &sctp_rto_min_default));
4582 case SCTPCTL_RTO_INITIAL:
4583 return (sysctl_int(oldp, oldlenp, newp, newlen,
4584 &sctp_rto_initial_default));
4585 case SCTPCTL_INIT_RTO_MAX:
4586 return (sysctl_int(oldp, oldlenp, newp, newlen,
4587 &sctp_init_rto_max_default));
4588 case SCTPCTL_COOKIE_LIFE:
4589 return (sysctl_int(oldp, oldlenp, newp, newlen,
4590 &sctp_valid_cookie_life_default));
4591 case SCTPCTL_INIT_RTX_MAX:
4592 return (sysctl_int(oldp, oldlenp, newp, newlen,
4593 &sctp_init_rtx_max_default));
4594 case SCTPCTL_ASSOC_RTX_MAX:
4595 return (sysctl_int(oldp, oldlenp, newp, newlen,
4596 &sctp_assoc_rtx_max_default));
4597 case SCTPCTL_PATH_RTX_MAX:
4598 return (sysctl_int(oldp, oldlenp, newp, newlen,
4599 &sctp_path_rtx_max_default));
4600 case SCTPCTL_NR_OUTGOING_STREAMS:
4601 return (sysctl_int(oldp, oldlenp, newp, newlen,
4602 &sctp_nr_outgoing_streams_default));
4603 #ifdef SCTP_DEBUG
4604 case SCTPCTL_DEBUG:
4605 return (sysctl_int(oldp, oldlenp, newp, newlen,
4606 &sctp_debug_on));
4607 #endif
4608 default:
4609 return (ENOPROTOOPT);
4611 /* NOTREACHED */
4613 #endif
4614 #if __NetBSD__
4616 * Sysctl for sctp variables.
4618 SYSCTL_SETUP(sysctl_net_inet_sctp_setup, "sysctl net.inet.sctp subtree setup")
4621 sysctl_createv(clog, 0, NULL, NULL,
4622 CTLFLAG_PERMANENT,
4623 CTLTYPE_NODE, "net", NULL,
4624 NULL, 0, NULL, 0,
4625 CTL_NET, CTL_EOL);
4626 sysctl_createv(clog, 0, NULL, NULL,
4627 CTLFLAG_PERMANENT,
4628 CTLTYPE_NODE, "inet", NULL,
4629 NULL, 0, NULL, 0,
4630 CTL_NET, PF_INET, CTL_EOL);
4631 sysctl_createv(clog, 0, NULL, NULL,
4632 CTLFLAG_PERMANENT,
4633 CTLTYPE_NODE, "sctp",
4634 SYSCTL_DESCR("sctp related settings"),
4635 NULL, 0, NULL, 0,
4636 CTL_NET, PF_INET, IPPROTO_SCTP, CTL_EOL);
4638 sysctl_createv(clog, 0, NULL, NULL,
4639 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4640 CTLTYPE_INT, "maxdgram",
4641 SYSCTL_DESCR("Maximum outgoing SCTP buffer size"),
4642 NULL, 0, &sctp_sendspace, 0,
4643 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXDGRAM,
4644 CTL_EOL);
4646 sysctl_createv(clog, 0, NULL, NULL,
4647 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4648 CTLTYPE_INT, "recvspace",
4649 SYSCTL_DESCR("Maximum incoming SCTP buffer size"),
4650 NULL, 0, &sctp_recvspace, 0,
4651 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_RECVSPACE,
4652 CTL_EOL);
4654 sysctl_createv(clog, 0, NULL, NULL,
4655 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4656 CTLTYPE_INT, "autoasconf",
4657 SYSCTL_DESCR("Enable SCTP Auto-ASCONF"),
4658 NULL, 0, &sctp_auto_asconf, 0,
4659 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_AUTOASCONF,
4660 CTL_EOL);
4662 sysctl_createv(clog, 0, NULL, NULL,
4663 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4664 CTLTYPE_INT, "ecn_enable",
4665 SYSCTL_DESCR("Enable SCTP ECN"),
4666 NULL, 0, &sctp_ecn, 0,
4667 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_ENABLE,
4668 CTL_EOL);
4670 sysctl_createv(clog, 0, NULL, NULL,
4671 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4672 CTLTYPE_INT, "ecn_nonce",
4673 SYSCTL_DESCR("Enable SCTP ECN Nonce"),
4674 NULL, 0, &sctp_ecn_nonce, 0,
4675 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_NONCE,
4676 CTL_EOL);
4678 sysctl_createv(clog, 0, NULL, NULL,
4679 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4680 CTLTYPE_INT, "strict_sack",
4681 SYSCTL_DESCR("Enable SCTP Strict SACK checking"),
4682 NULL, 0, &sctp_strict_sacks, 0,
4683 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_SACK,
4684 CTL_EOL);
4686 sysctl_createv(clog, 0, NULL, NULL,
4687 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4688 CTLTYPE_INT, "loopback_nocsum",
4689 SYSCTL_DESCR("Enable NO Csum on packets sent on loopback"),
4690 NULL, 0, &sctp_no_csum_on_loopback, 0,
4691 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_NOCSUM_LO,
4692 CTL_EOL);
4694 sysctl_createv(clog, 0, NULL, NULL,
4695 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4696 CTLTYPE_INT, "strict_init",
4697 SYSCTL_DESCR("Enable strict INIT/INIT-ACK singleton enforcement"),
4698 NULL, 0, &sctp_strict_init, 0,
4699 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_INIT,
4700 CTL_EOL);
4702 sysctl_createv(clog, 0, NULL, NULL,
4703 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4704 CTLTYPE_INT, "peer_chkoh",
4705 SYSCTL_DESCR("Amount to debit peers rwnd per chunk sent"),
4706 NULL, 0, &sctp_peer_chunk_oh, 0,
4707 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_PEER_CHK_OH,
4708 CTL_EOL);
4710 sysctl_createv(clog, 0, NULL, NULL,
4711 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4712 CTLTYPE_INT, "maxburst",
4713 SYSCTL_DESCR("Default max burst for sctp endpoints"),
4714 NULL, 0, &sctp_max_burst_default, 0,
4715 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXBURST,
4716 CTL_EOL);
4718 sysctl_createv(clog, 0, NULL, NULL,
4719 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4720 CTLTYPE_INT, "maxchunks",
4721 SYSCTL_DESCR("Default max chunks on queue per asoc"),
4722 NULL, 0, &sctp_max_chunks_on_queue, 0,
4723 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXCHUNKONQ,
4724 CTL_EOL);
4727 #endif