HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / netinet / sctp_usrreq.c
blob63b7516ac118b2bd1903a8062fc1499439d291d6
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.14 2008/04/20 13:44:25 swildner 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 int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual;
1188 int ipv4_addr_legal, ipv6_addr_legal;
1189 actual = 0;
1190 if (limit <= 0)
1191 return (actual);
1193 if (stcb) {
1194 /* Turn on all the appropriate scope */
1195 loopback_scope = stcb->asoc.loopback_scope;
1196 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1197 local_scope = stcb->asoc.local_scope;
1198 site_scope = stcb->asoc.site_scope;
1199 } else {
1200 /* Turn on ALL scope, since we look at the EP */
1201 loopback_scope = ipv4_local_scope = local_scope =
1202 site_scope = 1;
1204 ipv4_addr_legal = ipv6_addr_legal = 0;
1205 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1206 ipv6_addr_legal = 1;
1207 if (
1208 #if defined(__OpenBSD__)
1209 (0) /* we always do dual bind */
1210 #elif defined (__NetBSD__)
1211 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1212 #else
1213 (((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
1214 #endif
1215 == 0) {
1216 ipv4_addr_legal = 1;
1218 } else {
1219 ipv4_addr_legal = 1;
1222 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1223 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1224 struct ifaddr_container *ifac;
1226 if ((loopback_scope == 0) &&
1227 (ifn->if_type == IFT_LOOP)) {
1228 /* Skip loopback if loopback_scope not set */
1229 continue;
1231 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid],
1232 ifa_link) {
1233 struct ifaddr *ifa = ifac->ifa;
1235 if (stcb) {
1237 * For the BOUND-ALL case, the list
1238 * associated with a TCB is Always
1239 * considered a reverse list.. i.e.
1240 * it lists addresses that are NOT
1241 * part of the association. If this
1242 * is one of those we must skip it.
1244 if (sctp_is_addr_restricted(stcb,
1245 ifa->ifa_addr)) {
1246 continue;
1249 if ((ifa->ifa_addr->sa_family == AF_INET) &&
1250 (ipv4_addr_legal)) {
1251 struct sockaddr_in *sin;
1252 sin = (struct sockaddr_in *)ifa->ifa_addr;
1253 if (sin->sin_addr.s_addr == 0) {
1254 /* we skip unspecifed addresses */
1255 continue;
1257 if ((ipv4_local_scope == 0) &&
1258 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1259 continue;
1261 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1262 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1263 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1264 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1265 actual += sizeof(sizeof(struct sockaddr_in6));
1266 } else {
1267 memcpy(sas, sin, sizeof(*sin));
1268 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1269 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1270 actual += sizeof(*sin);
1272 if (actual >= limit) {
1273 return (actual);
1275 } else if ((ifa->ifa_addr->sa_family == AF_INET6) &&
1276 (ipv6_addr_legal)) {
1277 struct sockaddr_in6 *sin6, lsa6;
1278 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1279 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1280 /* we skip unspecifed addresses */
1281 continue;
1283 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1284 if (local_scope == 0)
1285 continue;
1286 if (sin6->sin6_scope_id == 0) {
1287 lsa6 = *sin6;
1288 if (in6_recoverscope(&lsa6,
1289 &lsa6.sin6_addr,
1290 NULL))
1291 /* bad link local address */
1292 continue;
1293 sin6 = &lsa6;
1296 if ((site_scope == 0) &&
1297 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1298 continue;
1300 memcpy(sas, sin6, sizeof(*sin6));
1301 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1302 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1303 actual += sizeof(*sin6);
1304 if (actual >= limit) {
1305 return (actual);
1310 } else {
1311 struct sctp_laddr *laddr;
1313 * If we have a TCB and we do NOT support ASCONF (it's
1314 * turned off or otherwise) then the list is always the
1315 * true list of addresses (the else case below). Otherwise
1316 * the list on the association is a list of addresses that
1317 * are NOT part of the association.
1319 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1320 /* The list is a NEGATIVE list */
1321 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1322 if (stcb) {
1323 if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) {
1324 continue;
1327 if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr))
1328 continue;
1330 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1331 sas = (struct sockaddr_storage *)((caddr_t)sas +
1332 laddr->ifa->ifa_addr->sa_len);
1333 actual += laddr->ifa->ifa_addr->sa_len;
1334 if (actual >= limit) {
1335 return (actual);
1338 } else {
1339 /* The list is a positive list if present */
1340 if (stcb) {
1341 /* Must use the specific association list */
1342 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1343 sctp_nxt_addr) {
1344 if (sctp_fill_user_address(sas,
1345 laddr->ifa->ifa_addr))
1346 continue;
1347 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1348 sas = (struct sockaddr_storage *)((caddr_t)sas +
1349 laddr->ifa->ifa_addr->sa_len);
1350 actual += laddr->ifa->ifa_addr->sa_len;
1351 if (actual >= limit) {
1352 return (actual);
1355 } else {
1356 /* No endpoint so use the endpoints individual list */
1357 LIST_FOREACH(laddr, &inp->sctp_addr_list,
1358 sctp_nxt_addr) {
1359 if (sctp_fill_user_address(sas,
1360 laddr->ifa->ifa_addr))
1361 continue;
1362 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1363 sas = (struct sockaddr_storage *)((caddr_t)sas +
1364 laddr->ifa->ifa_addr->sa_len);
1365 actual += laddr->ifa->ifa_addr->sa_len;
1366 if (actual >= limit) {
1367 return (actual);
1373 return (actual);
1376 static int
1377 sctp_count_max_addresses(struct sctp_inpcb *inp)
1379 int cnt = 0;
1381 * In both sub-set bound an bound_all cases we return the MAXIMUM
1382 * number of addresses that you COULD get. In reality the sub-set
1383 * bound may have an exclusion list for a given TCB OR in the
1384 * bound-all case a TCB may NOT include the loopback or other
1385 * addresses as well.
1387 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1388 struct ifnet *ifn;
1390 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1391 struct ifaddr_container *ifac;
1393 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1394 struct ifaddr *ifa = ifac->ifa;
1396 /* Count them if they are the right type */
1397 if (ifa->ifa_addr->sa_family == AF_INET) {
1398 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1399 cnt += sizeof(struct sockaddr_in6);
1400 else
1401 cnt += sizeof(struct sockaddr_in);
1403 } else if (ifa->ifa_addr->sa_family == AF_INET6)
1404 cnt += sizeof(struct sockaddr_in6);
1407 } else {
1408 struct sctp_laddr *laddr;
1409 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1410 if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
1411 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1412 cnt += sizeof(struct sockaddr_in6);
1413 else
1414 cnt += sizeof(struct sockaddr_in);
1416 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET6)
1417 cnt += sizeof(struct sockaddr_in6);
1420 return (cnt);
1423 static int
1424 sctp_do_connect_x(struct socket *so,
1425 struct sctp_inpcb *inp,
1426 struct mbuf *m,
1427 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1428 struct thread *p,
1429 #else
1430 struct proc *p,
1431 #endif
1432 int delay
1435 int error = 0;
1436 struct sctp_tcb *stcb = NULL;
1437 struct sockaddr *sa;
1438 int num_v6=0, num_v4=0, *totaddrp, totaddr, i, incr, at;
1439 #ifdef SCTP_DEBUG
1440 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1441 kprintf("Connectx called\n");
1443 #endif /* SCTP_DEBUG */
1445 crit_enter();
1446 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1447 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1448 /* We are already connected AND the TCP model */
1449 crit_exit();
1450 return (EADDRINUSE);
1452 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1453 SCTP_INP_RLOCK(inp);
1454 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1455 SCTP_INP_RUNLOCK(inp);
1457 if (stcb) {
1458 crit_exit();
1459 return (EALREADY);
1462 SCTP_ASOC_CREATE_LOCK(inp);
1463 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1464 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1465 SCTP_ASOC_CREATE_UNLOCK(inp);
1466 crit_exit();
1467 return (EFAULT);
1470 totaddrp = mtod(m, int *);
1471 totaddr = *totaddrp;
1472 sa = (struct sockaddr *)(totaddrp + 1);
1473 at = incr = 0;
1474 /* account and validate addresses */
1475 SCTP_INP_WLOCK(inp);
1476 SCTP_INP_INCR_REF(inp);
1477 SCTP_INP_WUNLOCK(inp);
1478 for (i = 0; i < totaddr; i++) {
1479 if (sa->sa_family == AF_INET) {
1480 num_v4++;
1481 incr = sizeof(struct sockaddr_in);
1482 } else if (sa->sa_family == AF_INET6) {
1483 struct sockaddr_in6 *sin6;
1484 sin6 = (struct sockaddr_in6 *)sa;
1485 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1486 /* Must be non-mapped for connectx */
1487 SCTP_ASOC_CREATE_UNLOCK(inp);
1488 crit_exit();
1489 return EINVAL;
1491 num_v6++;
1492 incr = sizeof(struct sockaddr_in6);
1493 } else {
1494 totaddr = i;
1495 break;
1497 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1498 if (stcb != NULL) {
1499 /* Already have or am bring up an association */
1500 SCTP_ASOC_CREATE_UNLOCK(inp);
1501 SCTP_TCB_UNLOCK(stcb);
1502 crit_exit();
1503 return (EALREADY);
1505 if ((at + incr) > m->m_len) {
1506 totaddr = i;
1507 break;
1509 sa = (struct sockaddr *)((caddr_t)sa + incr);
1511 sa = (struct sockaddr *)(totaddrp + 1);
1512 SCTP_INP_WLOCK(inp);
1513 SCTP_INP_DECR_REF(inp);
1514 SCTP_INP_WUNLOCK(inp);
1515 #ifdef INET6
1516 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1517 (num_v6 > 0)) {
1518 crit_exit();
1519 SCTP_INP_WUNLOCK(inp);
1520 SCTP_ASOC_CREATE_UNLOCK(inp);
1521 return (EINVAL);
1523 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1524 (num_v4 > 0)) {
1525 struct in6pcb *inp6;
1526 inp6 = (struct in6pcb *)inp;
1527 if (
1528 #if defined(__OpenBSD__)
1529 (0) /* we always do dual bind */
1530 #elif defined (__NetBSD__)
1531 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1532 #else
1533 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1534 #endif
1537 * if IPV6_V6ONLY flag, ignore connections
1538 * destined to a v4 addr or v4-mapped addr
1540 SCTP_INP_WUNLOCK(inp);
1541 SCTP_ASOC_CREATE_UNLOCK(inp);
1542 crit_exit();
1543 return EINVAL;
1546 #endif /* INET6 */
1547 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1548 SCTP_PCB_FLAGS_UNBOUND) {
1549 /* Bind a ephemeral port */
1550 SCTP_INP_WUNLOCK(inp);
1551 error = sctp_inpcb_bind(so, NULL, p);
1552 if (error) {
1553 SCTP_ASOC_CREATE_UNLOCK(inp);
1554 crit_exit();
1555 return (error);
1557 } else {
1558 SCTP_INP_WUNLOCK(inp);
1560 /* We are GOOD to go */
1561 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0);
1562 if (stcb == NULL) {
1563 /* Gak! no memory */
1564 SCTP_ASOC_CREATE_UNLOCK(inp);
1565 crit_exit();
1566 return (error);
1568 /* move to second address */
1569 if (sa->sa_family == AF_INET)
1570 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1571 else
1572 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1574 for (i = 1; i < totaddr; i++) {
1575 if (sa->sa_family == AF_INET) {
1576 incr = sizeof(struct sockaddr_in);
1577 if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1578 /* assoc gone no un-lock */
1579 sctp_free_assoc(inp, stcb);
1580 SCTP_ASOC_CREATE_UNLOCK(inp);
1581 crit_exit();
1582 return (ENOBUFS);
1585 } else if (sa->sa_family == AF_INET6) {
1586 incr = sizeof(struct sockaddr_in6);
1587 if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1588 /* assoc gone no un-lock */
1589 sctp_free_assoc(inp, stcb);
1590 SCTP_ASOC_CREATE_UNLOCK(inp);
1591 crit_exit();
1592 return (ENOBUFS);
1595 sa = (struct sockaddr *)((caddr_t)sa + incr);
1597 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1598 if (delay) {
1599 /* doing delayed connection */
1600 stcb->asoc.delayed_connection = 1;
1601 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1602 } else {
1603 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1604 sctp_send_initiate(inp, stcb);
1606 SCTP_TCB_UNLOCK(stcb);
1607 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1608 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1609 /* Set the connected flag so we can queue data */
1610 soisconnecting(so);
1612 SCTP_ASOC_CREATE_UNLOCK(inp);
1613 crit_exit();
1614 return error;
1618 static int
1619 sctp_optsget(struct socket *so,
1620 int opt,
1621 struct mbuf **mp,
1622 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1623 struct thread *p
1624 #else
1625 struct proc *p
1626 #endif
1629 struct sctp_inpcb *inp;
1630 struct mbuf *m;
1631 int error, optval=0;
1632 struct sctp_tcb *stcb = NULL;
1634 inp = (struct sctp_inpcb *)so->so_pcb;
1635 if (inp == 0)
1636 return EINVAL;
1637 error = 0;
1639 if (mp == NULL) {
1640 #ifdef SCTP_DEBUG
1641 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1642 kprintf("optsget:MP is NULL EINVAL\n");
1644 #endif /* SCTP_DEBUG */
1645 return (EINVAL);
1647 m = *mp;
1648 if (m == NULL) {
1649 /* Got to have a mbuf */
1650 #ifdef SCTP_DEBUG
1651 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1652 kprintf("Huh no mbuf\n");
1654 #endif /* SCTP_DEBUG */
1655 return (EINVAL);
1657 #ifdef SCTP_DEBUG
1658 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1659 kprintf("optsget opt:%lxx sz:%u\n", (unsigned long)opt,
1660 m->m_len);
1662 #endif /* SCTP_DEBUG */
1664 switch (opt) {
1665 case SCTP_NODELAY:
1666 case SCTP_AUTOCLOSE:
1667 case SCTP_AUTO_ASCONF:
1668 case SCTP_DISABLE_FRAGMENTS:
1669 case SCTP_I_WANT_MAPPED_V4_ADDR:
1670 #ifdef SCTP_DEBUG
1671 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1672 kprintf("other stuff\n");
1674 #endif /* SCTP_DEBUG */
1675 SCTP_INP_RLOCK(inp);
1676 switch (opt) {
1677 case SCTP_DISABLE_FRAGMENTS:
1678 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT;
1679 break;
1680 case SCTP_I_WANT_MAPPED_V4_ADDR:
1681 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
1682 break;
1683 case SCTP_AUTO_ASCONF:
1684 optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF;
1685 break;
1686 case SCTP_NODELAY:
1687 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY;
1688 break;
1689 case SCTP_AUTOCLOSE:
1690 if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) ==
1691 SCTP_PCB_FLAGS_AUTOCLOSE)
1692 optval = inp->sctp_ep.auto_close_time;
1693 else
1694 optval = 0;
1695 break;
1697 default:
1698 error = ENOPROTOOPT;
1699 } /* end switch (sopt->sopt_name) */
1700 if (opt != SCTP_AUTOCLOSE) {
1701 /* make it an "on/off" value */
1702 optval = (optval != 0);
1704 if ((size_t)m->m_len < sizeof(int)) {
1705 error = EINVAL;
1707 SCTP_INP_RUNLOCK(inp);
1708 if (error == 0) {
1709 /* return the option value */
1710 *mtod(m, int *) = optval;
1711 m->m_len = sizeof(optval);
1713 break;
1714 case SCTP_GET_ASOC_ID_LIST:
1716 struct sctp_assoc_ids *ids;
1717 int cnt, at;
1718 u_int16_t orig;
1720 if ((size_t)m->m_len < sizeof(struct sctp_assoc_ids)) {
1721 error = EINVAL;
1722 break;
1724 ids = mtod(m, struct sctp_assoc_ids *);
1725 cnt = 0;
1726 SCTP_INP_RLOCK(inp);
1727 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1728 if (stcb == NULL) {
1729 none_out_now:
1730 ids->asls_numb_present = 0;
1731 ids->asls_more_to_get = 0;
1732 SCTP_INP_RUNLOCK(inp);
1733 break;
1735 orig = ids->asls_assoc_start;
1736 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1737 while( orig ) {
1738 stcb = LIST_NEXT(stcb , sctp_tcblist);
1739 orig--;
1740 cnt--;
1742 if ( stcb == NULL)
1743 goto none_out_now;
1745 at = 0;
1746 ids->asls_numb_present = 0;
1747 ids->asls_more_to_get = 1;
1748 while(at < MAX_ASOC_IDS_RET) {
1749 ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1750 at++;
1751 ids->asls_numb_present++;
1752 stcb = LIST_NEXT(stcb , sctp_tcblist);
1753 if (stcb == NULL) {
1754 ids->asls_more_to_get = 0;
1755 break;
1758 SCTP_INP_RUNLOCK(inp);
1760 break;
1761 case SCTP_GET_NONCE_VALUES:
1763 struct sctp_get_nonce_values *gnv;
1764 if ((size_t)m->m_len < sizeof(struct sctp_get_nonce_values)) {
1765 error = EINVAL;
1766 break;
1768 gnv = mtod(m, struct sctp_get_nonce_values *);
1769 stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id);
1770 if (stcb == NULL) {
1771 error = ENOTCONN;
1772 } else {
1773 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1774 gnv->gn_local_tag = stcb->asoc.my_vtag;
1775 SCTP_TCB_UNLOCK(stcb);
1779 break;
1780 case SCTP_PEER_PUBLIC_KEY:
1781 case SCTP_MY_PUBLIC_KEY:
1782 case SCTP_SET_AUTH_CHUNKS:
1783 case SCTP_SET_AUTH_SECRET:
1784 /* not supported yet and until we refine the draft */
1785 error = EOPNOTSUPP;
1786 break;
1788 case SCTP_DELAYED_ACK_TIME:
1790 int32_t *tm;
1791 if ((size_t)m->m_len < sizeof(int32_t)) {
1792 error = EINVAL;
1793 break;
1795 tm = mtod(m, int32_t *);
1797 *tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1799 break;
1801 case SCTP_GET_SNDBUF_USE:
1802 if ((size_t)m->m_len < sizeof(struct sctp_sockstat)) {
1803 error = EINVAL;
1804 } else {
1805 struct sctp_sockstat *ss;
1806 struct sctp_tcb *stcb;
1807 struct sctp_association *asoc;
1808 ss = mtod(m, struct sctp_sockstat *);
1809 stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id);
1810 if (stcb == NULL) {
1811 error = ENOTCONN;
1812 } else {
1813 asoc = &stcb->asoc;
1814 ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size;
1815 ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size;
1816 ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue +
1817 asoc->size_on_reasm_queue +
1818 asoc->size_on_all_streams);
1819 SCTP_TCB_UNLOCK(stcb);
1820 error = 0;
1821 m->m_len = sizeof(struct sctp_sockstat);
1824 break;
1825 case SCTP_MAXBURST:
1827 u_int8_t *burst;
1828 burst = mtod(m, u_int8_t *);
1829 SCTP_INP_RLOCK(inp);
1830 *burst = inp->sctp_ep.max_burst;
1831 SCTP_INP_RUNLOCK(inp);
1832 m->m_len = sizeof(u_int8_t);
1834 break;
1835 case SCTP_MAXSEG:
1837 u_int32_t *segsize;
1838 sctp_assoc_t *assoc_id;
1839 int ovh;
1841 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1842 error = EINVAL;
1843 break;
1845 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
1846 error = EINVAL;
1847 break;
1849 assoc_id = mtod(m, sctp_assoc_t *);
1850 segsize = mtod(m, u_int32_t *);
1851 m->m_len = sizeof(u_int32_t);
1853 if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1854 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) ||
1855 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1856 struct sctp_tcb *stcb;
1857 SCTP_INP_RLOCK(inp);
1858 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1859 if (stcb) {
1860 SCTP_TCB_LOCK(stcb);
1861 SCTP_INP_RUNLOCK(inp);
1862 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1863 SCTP_TCB_UNLOCK(stcb);
1864 } else {
1865 SCTP_INP_RUNLOCK(inp);
1866 goto skipit;
1868 } else {
1869 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1870 if (stcb) {
1871 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1872 SCTP_TCB_UNLOCK(stcb);
1873 break;
1875 skipit:
1876 /* default is to get the max, if I
1877 * can't calculate from an existing association.
1879 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1880 ovh = SCTP_MED_OVERHEAD;
1881 } else {
1882 ovh = SCTP_MED_V4_OVERHEAD;
1884 *segsize = inp->sctp_frag_point - ovh;
1887 break;
1889 case SCTP_SET_DEBUG_LEVEL:
1890 #ifdef SCTP_DEBUG
1892 u_int32_t *level;
1893 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1894 error = EINVAL;
1895 break;
1897 level = mtod(m, u_int32_t *);
1898 error = 0;
1899 *level = sctp_debug_on;
1900 m->m_len = sizeof(u_int32_t);
1901 kprintf("Returning DEBUG LEVEL %x is set\n",
1902 (u_int)sctp_debug_on);
1904 #else /* SCTP_DEBUG */
1905 error = EOPNOTSUPP;
1906 #endif
1907 break;
1908 case SCTP_GET_STAT_LOG:
1909 #ifdef SCTP_STAT_LOGGING
1910 error = sctp_fill_stat_log(m);
1911 #else /* SCTP_DEBUG */
1912 error = EOPNOTSUPP;
1913 #endif
1914 break;
1915 case SCTP_GET_PEGS:
1917 u_int32_t *pt;
1918 if ((size_t)m->m_len < sizeof(sctp_pegs)) {
1919 error = EINVAL;
1920 break;
1922 pt = mtod(m, u_int32_t *);
1923 memcpy(pt, sctp_pegs, sizeof(sctp_pegs));
1924 m->m_len = sizeof(sctp_pegs);
1926 break;
1927 case SCTP_EVENTS:
1929 struct sctp_event_subscribe *events;
1930 #ifdef SCTP_DEBUG
1931 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1932 kprintf("get events\n");
1934 #endif /* SCTP_DEBUG */
1935 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
1936 #ifdef SCTP_DEBUG
1937 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1938 kprintf("M->M_LEN is %d not %d\n",
1939 (int)m->m_len,
1940 (int)sizeof(struct sctp_event_subscribe));
1942 #endif /* SCTP_DEBUG */
1943 error = EINVAL;
1944 break;
1946 events = mtod(m, struct sctp_event_subscribe *);
1947 memset(events, 0, sizeof(events));
1948 SCTP_INP_RLOCK(inp);
1949 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT)
1950 events->sctp_data_io_event = 1;
1952 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT)
1953 events->sctp_association_event = 1;
1955 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT)
1956 events->sctp_address_event = 1;
1958 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT)
1959 events->sctp_send_failure_event = 1;
1961 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR)
1962 events->sctp_peer_error_event = 1;
1964 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)
1965 events->sctp_shutdown_event = 1;
1967 if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT)
1968 events->sctp_partial_delivery_event = 1;
1970 if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT)
1971 events->sctp_adaption_layer_event = 1;
1973 if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT)
1974 events->sctp_stream_reset_events = 1;
1975 SCTP_INP_RUNLOCK(inp);
1976 m->m_len = sizeof(struct sctp_event_subscribe);
1979 break;
1981 case SCTP_ADAPTION_LAYER:
1982 if ((size_t)m->m_len < sizeof(int)) {
1983 error = EINVAL;
1984 break;
1986 #ifdef SCTP_DEBUG
1987 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1988 kprintf("getadaption ind\n");
1990 #endif /* SCTP_DEBUG */
1991 SCTP_INP_RLOCK(inp);
1992 *mtod(m, int *) = inp->sctp_ep.adaption_layer_indicator;
1993 SCTP_INP_RUNLOCK(inp);
1994 m->m_len = sizeof(int);
1995 break;
1996 case SCTP_SET_INITIAL_DBG_SEQ:
1997 if ((size_t)m->m_len < sizeof(int)) {
1998 error = EINVAL;
1999 break;
2001 #ifdef SCTP_DEBUG
2002 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2003 kprintf("get initial dbg seq\n");
2005 #endif /* SCTP_DEBUG */
2006 SCTP_INP_RLOCK(inp);
2007 *mtod(m, int *) = inp->sctp_ep.initial_sequence_debug;
2008 SCTP_INP_RUNLOCK(inp);
2009 m->m_len = sizeof(int);
2010 break;
2011 case SCTP_GET_LOCAL_ADDR_SIZE:
2012 if ((size_t)m->m_len < sizeof(int)) {
2013 error = EINVAL;
2014 break;
2016 #ifdef SCTP_DEBUG
2017 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2018 kprintf("get local sizes\n");
2020 #endif /* SCTP_DEBUG */
2021 SCTP_INP_RLOCK(inp);
2022 *mtod(m, int *) = sctp_count_max_addresses(inp);
2023 SCTP_INP_RUNLOCK(inp);
2024 m->m_len = sizeof(int);
2025 break;
2026 case SCTP_GET_REMOTE_ADDR_SIZE:
2028 sctp_assoc_t *assoc_id;
2029 u_int32_t *val, sz;
2030 struct sctp_nets *net;
2031 #ifdef SCTP_DEBUG
2032 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2033 kprintf("get remote size\n");
2035 #endif /* SCTP_DEBUG */
2036 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
2037 #ifdef SCTP_DEBUG
2038 kprintf("m->m_len:%d not %d\n",
2039 m->m_len, sizeof(sctp_assoc_t));
2040 #endif /* SCTP_DEBUG */
2041 error = EINVAL;
2042 break;
2044 stcb = NULL;
2045 val = mtod(m, u_int32_t *);
2046 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2047 SCTP_INP_RLOCK(inp);
2048 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2049 if (stcb)
2050 SCTP_TCB_LOCK(stcb);
2051 SCTP_INP_RUNLOCK(inp);
2053 if (stcb == NULL) {
2054 assoc_id = mtod(m, sctp_assoc_t *);
2055 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
2058 if (stcb == NULL) {
2059 error = EINVAL;
2060 break;
2062 *val = 0;
2063 sz = 0;
2064 /* Count the sizes */
2065 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2066 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2067 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2068 sz += sizeof(struct sockaddr_in6);
2069 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2070 sz += sizeof(struct sockaddr_in);
2071 } else {
2072 /* huh */
2073 break;
2076 SCTP_TCB_UNLOCK(stcb);
2077 *val = sz;
2078 m->m_len = sizeof(u_int32_t);
2080 break;
2081 case SCTP_GET_PEER_ADDRESSES:
2083 * Get the address information, an array
2084 * is passed in to fill up we pack it.
2087 int cpsz, left;
2088 struct sockaddr_storage *sas;
2089 struct sctp_nets *net;
2090 struct sctp_getaddresses *saddr;
2091 #ifdef SCTP_DEBUG
2092 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2093 kprintf("get peer addresses\n");
2095 #endif /* SCTP_DEBUG */
2096 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2097 error = EINVAL;
2098 break;
2100 left = m->m_len - sizeof(struct sctp_getaddresses);
2101 saddr = mtod(m, struct sctp_getaddresses *);
2102 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2103 SCTP_INP_RLOCK(inp);
2104 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2105 if (stcb)
2106 SCTP_TCB_LOCK(stcb);
2107 SCTP_INP_RUNLOCK(inp);
2108 } else
2109 stcb = sctp_findassociation_ep_asocid(inp,
2110 saddr->sget_assoc_id);
2111 if (stcb == NULL) {
2112 error = ENOENT;
2113 break;
2115 m->m_len = sizeof(struct sctp_getaddresses);
2116 sas = (struct sockaddr_storage *)&saddr->addr[0];
2118 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2119 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2120 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2121 cpsz = sizeof(struct sockaddr_in6);
2122 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2123 cpsz = sizeof(struct sockaddr_in);
2124 } else {
2125 /* huh */
2126 break;
2128 if (left < cpsz) {
2129 /* not enough room. */
2130 #ifdef SCTP_DEBUG
2131 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2132 kprintf("Out of room\n");
2134 #endif /* SCTP_DEBUG */
2135 break;
2137 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
2138 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2139 /* Must map the address */
2140 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2141 (struct sockaddr_in6 *)sas);
2142 } else {
2143 memcpy(sas, &net->ro._l_addr, cpsz);
2145 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2147 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2148 left -= cpsz;
2149 m->m_len += cpsz;
2150 #ifdef SCTP_DEBUG
2151 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2152 kprintf("left now:%d mlen:%d\n",
2153 left, m->m_len);
2155 #endif /* SCTP_DEBUG */
2157 SCTP_TCB_UNLOCK(stcb);
2159 #ifdef SCTP_DEBUG
2160 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2161 kprintf("All done\n");
2163 #endif /* SCTP_DEBUG */
2164 break;
2165 case SCTP_GET_LOCAL_ADDRESSES:
2167 int limit, actual;
2168 struct sockaddr_storage *sas;
2169 struct sctp_getaddresses *saddr;
2170 #ifdef SCTP_DEBUG
2171 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2172 kprintf("get local addresses\n");
2174 #endif /* SCTP_DEBUG */
2175 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2176 error = EINVAL;
2177 break;
2179 saddr = mtod(m, struct sctp_getaddresses *);
2181 if (saddr->sget_assoc_id) {
2182 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2183 SCTP_INP_RLOCK(inp);
2184 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2185 if (stcb)
2186 SCTP_TCB_LOCK(stcb);
2187 SCTP_INP_RUNLOCK(inp);
2188 } else
2189 stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id);
2191 } else {
2192 stcb = NULL;
2195 * assure that the TCP model does not need a assoc id
2196 * once connected.
2198 if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
2199 (stcb == NULL) ) {
2200 SCTP_INP_RLOCK(inp);
2201 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2202 if (stcb)
2203 SCTP_TCB_LOCK(stcb);
2204 SCTP_INP_RUNLOCK(inp);
2206 sas = (struct sockaddr_storage *)&saddr->addr[0];
2207 limit = m->m_len - sizeof(sctp_assoc_t);
2208 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2209 SCTP_TCB_UNLOCK(stcb);
2210 m->m_len = sizeof(struct sockaddr_storage) + actual;
2212 break;
2213 case SCTP_PEER_ADDR_PARAMS:
2215 struct sctp_paddrparams *paddrp;
2216 struct sctp_nets *net;
2218 #ifdef SCTP_DEBUG
2219 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2220 kprintf("Getting peer_addr_params\n");
2222 #endif /* SCTP_DEBUG */
2223 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
2224 #ifdef SCTP_DEBUG
2225 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2226 kprintf("Hmm m->m_len:%d is to small\n",
2227 m->m_len);
2229 #endif /* SCTP_DEBUG */
2230 error = EINVAL;
2231 break;
2233 paddrp = mtod(m, struct sctp_paddrparams *);
2235 net = NULL;
2236 if (paddrp->spp_assoc_id) {
2237 #ifdef SCTP_DEBUG
2238 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2239 kprintf("In spp_assoc_id find type\n");
2241 #endif /* SCTP_DEBUG */
2242 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2243 SCTP_INP_RLOCK(inp);
2244 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2245 if (stcb) {
2246 SCTP_TCB_LOCK(stcb);
2247 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2249 SCTP_INP_RLOCK(inp);
2250 } else {
2251 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
2253 if (stcb == NULL) {
2254 error = ENOENT;
2255 break;
2258 if ( (stcb == NULL) &&
2259 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
2260 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
2261 /* Lookup via address */
2262 #ifdef SCTP_DEBUG
2263 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2264 kprintf("Ok we need to lookup a param\n");
2266 #endif /* SCTP_DEBUG */
2267 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2268 SCTP_INP_RLOCK(inp);
2269 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2270 if (stcb) {
2271 SCTP_TCB_LOCK(stcb);
2272 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2274 SCTP_INP_RUNLOCK(inp);
2275 } else {
2276 SCTP_INP_WLOCK(inp);
2277 SCTP_INP_INCR_REF(inp);
2278 SCTP_INP_WUNLOCK(inp);
2279 stcb = sctp_findassociation_ep_addr(&inp,
2280 (struct sockaddr *)&paddrp->spp_address,
2281 &net, NULL, NULL);
2282 if (stcb == NULL) {
2283 SCTP_INP_WLOCK(inp);
2284 SCTP_INP_DECR_REF(inp);
2285 SCTP_INP_WUNLOCK(inp);
2289 if (stcb == NULL) {
2290 error = ENOENT;
2291 break;
2293 } else {
2294 /* Effects the Endpoint */
2295 #ifdef SCTP_DEBUG
2296 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2297 kprintf("User wants EP level info\n");
2299 #endif /* SCTP_DEBUG */
2300 stcb = NULL;
2302 if (stcb) {
2303 /* Applys to the specific association */
2304 #ifdef SCTP_DEBUG
2305 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2306 kprintf("In TCB side\n");
2308 #endif /* SCTP_DEBUG */
2309 if (net) {
2310 paddrp->spp_pathmaxrxt = net->failure_threshold;
2311 } else {
2312 /* No destination so return default value */
2313 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2315 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2316 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2317 SCTP_TCB_UNLOCK(stcb);
2318 } else {
2319 /* Use endpoint defaults */
2320 SCTP_INP_RLOCK(inp);
2321 #ifdef SCTP_DEBUG
2322 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2323 kprintf("In EP level info\n");
2325 #endif /* SCTP_DEBUG */
2326 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2327 paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT];
2328 paddrp->spp_assoc_id = (sctp_assoc_t)0;
2329 SCTP_INP_RUNLOCK(inp);
2331 m->m_len = sizeof(struct sctp_paddrparams);
2333 break;
2334 case SCTP_GET_PEER_ADDR_INFO:
2336 struct sctp_paddrinfo *paddri;
2337 struct sctp_nets *net;
2338 #ifdef SCTP_DEBUG
2339 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2340 kprintf("GetPEER ADDR_INFO\n");
2342 #endif /* SCTP_DEBUG */
2343 if ((size_t)m->m_len < sizeof(struct sctp_paddrinfo)) {
2344 error = EINVAL;
2345 break;
2347 paddri = mtod(m, struct sctp_paddrinfo *);
2348 net = NULL;
2349 if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) ||
2350 (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) {
2351 /* Lookup via address */
2352 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2353 SCTP_INP_RLOCK(inp);
2354 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2355 if (stcb) {
2356 SCTP_TCB_LOCK(stcb);
2357 net = sctp_findnet(stcb,
2358 (struct sockaddr *)&paddri->spinfo_address);
2360 SCTP_INP_RUNLOCK(inp);
2361 } else {
2362 SCTP_INP_WLOCK(inp);
2363 SCTP_INP_INCR_REF(inp);
2364 SCTP_INP_WUNLOCK(inp);
2365 stcb = sctp_findassociation_ep_addr(&inp,
2366 (struct sockaddr *)&paddri->spinfo_address,
2367 &net, NULL, NULL);
2368 if (stcb == NULL) {
2369 SCTP_INP_WLOCK(inp);
2370 SCTP_INP_DECR_REF(inp);
2371 SCTP_INP_WUNLOCK(inp);
2375 } else {
2376 stcb = NULL;
2378 if ((stcb == NULL) || (net == NULL)) {
2379 error = ENOENT;
2380 break;
2382 m->m_len = sizeof(struct sctp_paddrinfo);
2383 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB);
2384 paddri->spinfo_cwnd = net->cwnd;
2385 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2386 paddri->spinfo_rto = net->RTO;
2387 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2388 SCTP_TCB_UNLOCK(stcb);
2390 break;
2391 case SCTP_PCB_STATUS:
2393 struct sctp_pcbinfo *spcb;
2394 #ifdef SCTP_DEBUG
2395 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2396 kprintf("PCB status\n");
2398 #endif /* SCTP_DEBUG */
2399 if ((size_t)m->m_len < sizeof(struct sctp_pcbinfo)) {
2400 error = EINVAL;
2401 break;
2403 spcb = mtod(m, struct sctp_pcbinfo *);
2404 sctp_fill_pcbinfo(spcb);
2405 m->m_len = sizeof(struct sctp_pcbinfo);
2407 break;
2408 case SCTP_STATUS:
2410 struct sctp_nets *net;
2411 struct sctp_status *sstat;
2412 #ifdef SCTP_DEBUG
2413 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2414 kprintf("SCTP status\n");
2416 #endif /* SCTP_DEBUG */
2418 if ((size_t)m->m_len < sizeof(struct sctp_status)) {
2419 error = EINVAL;
2420 break;
2422 sstat = mtod(m, struct sctp_status *);
2424 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2425 SCTP_INP_RLOCK(inp);
2426 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2427 if (stcb)
2428 SCTP_TCB_LOCK(stcb);
2429 SCTP_INP_RUNLOCK(inp);
2430 } else
2431 stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id);
2433 if (stcb == NULL) {
2434 error = EINVAL;
2435 break;
2438 * I think passing the state is fine since
2439 * sctp_constants.h will be available to the user
2440 * land.
2442 sstat->sstat_state = stcb->asoc.state;
2443 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2444 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2446 * We can't include chunks that have been passed
2447 * to the socket layer. Only things in queue.
2449 sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue +
2450 stcb->asoc.cnt_on_reasm_queue +
2451 stcb->asoc.cnt_on_all_streams);
2454 sstat->sstat_instrms = stcb->asoc.streamincnt;
2455 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2456 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2457 memcpy(&sstat->sstat_primary.spinfo_address,
2458 &stcb->asoc.primary_destination->ro._l_addr,
2459 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2460 net = stcb->asoc.primary_destination;
2461 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2463 * Again the user can get info from sctp_constants.h
2464 * for what the state of the network is.
2466 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2467 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2468 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2469 sstat->sstat_primary.spinfo_rto = net->RTO;
2470 sstat->sstat_primary.spinfo_mtu = net->mtu;
2471 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2472 SCTP_TCB_UNLOCK(stcb);
2473 m->m_len = sizeof(*sstat);
2475 break;
2476 case SCTP_RTOINFO:
2478 struct sctp_rtoinfo *srto;
2479 #ifdef SCTP_DEBUG
2480 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2481 kprintf("RTO Info\n");
2483 #endif /* SCTP_DEBUG */
2484 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
2485 error = EINVAL;
2486 break;
2488 srto = mtod(m, struct sctp_rtoinfo *);
2489 if (srto->srto_assoc_id == 0) {
2490 /* Endpoint only please */
2491 SCTP_INP_RLOCK(inp);
2492 srto->srto_initial = inp->sctp_ep.initial_rto;
2493 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2494 srto->srto_min = inp->sctp_ep.sctp_minrto;
2495 SCTP_INP_RUNLOCK(inp);
2496 break;
2498 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2499 SCTP_INP_RLOCK(inp);
2500 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2501 if (stcb)
2502 SCTP_TCB_LOCK(stcb);
2503 SCTP_INP_RUNLOCK(inp);
2504 } else
2505 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2507 if (stcb == NULL) {
2508 error = EINVAL;
2509 break;
2511 srto->srto_initial = stcb->asoc.initial_rto;
2512 srto->srto_max = stcb->asoc.maxrto;
2513 srto->srto_min = stcb->asoc.minrto;
2514 SCTP_TCB_UNLOCK(stcb);
2515 m->m_len = sizeof(*srto);
2517 break;
2518 case SCTP_ASSOCINFO:
2520 struct sctp_assocparams *sasoc;
2521 #ifdef SCTP_DEBUG
2522 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2523 kprintf("Associnfo\n");
2525 #endif /* SCTP_DEBUG */
2526 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
2527 error = EINVAL;
2528 break;
2530 sasoc = mtod(m, struct sctp_assocparams *);
2531 stcb = NULL;
2533 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2534 SCTP_INP_RLOCK(inp);
2535 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2536 if (stcb)
2537 SCTP_TCB_LOCK(stcb);
2538 SCTP_INP_RUNLOCK(inp);
2540 if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) {
2541 stcb = sctp_findassociation_ep_asocid(inp,
2542 sasoc->sasoc_assoc_id);
2543 if (stcb == NULL) {
2544 error = ENOENT;
2545 break;
2547 } else {
2548 stcb = NULL;
2551 if (stcb) {
2552 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2553 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2554 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2555 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2556 sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2557 SCTP_TCB_UNLOCK(stcb);
2558 } else {
2559 SCTP_INP_RLOCK(inp);
2560 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2561 sasoc->sasoc_number_peer_destinations = 0;
2562 sasoc->sasoc_peer_rwnd = 0;
2563 sasoc->sasoc_local_rwnd = ssb_space(&inp->sctp_socket->so_rcv);
2564 sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2565 SCTP_INP_RUNLOCK(inp);
2567 m->m_len = sizeof(*sasoc);
2569 break;
2570 case SCTP_DEFAULT_SEND_PARAM:
2572 struct sctp_sndrcvinfo *s_info;
2574 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
2575 error = EINVAL;
2576 break;
2578 s_info = mtod(m, struct sctp_sndrcvinfo *);
2579 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2580 SCTP_INP_RLOCK(inp);
2581 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2582 if (stcb)
2583 SCTP_TCB_LOCK(stcb);
2584 SCTP_INP_RUNLOCK(inp);
2585 } else
2586 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2588 if (stcb == NULL) {
2589 error = ENOENT;
2590 break;
2592 /* Copy it out */
2593 *s_info = stcb->asoc.def_send;
2594 SCTP_TCB_UNLOCK(stcb);
2595 m->m_len = sizeof(*s_info);
2597 case SCTP_INITMSG:
2599 struct sctp_initmsg *sinit;
2600 #ifdef SCTP_DEBUG
2601 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2602 kprintf("initmsg\n");
2604 #endif /* SCTP_DEBUG */
2605 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
2606 error = EINVAL;
2607 break;
2609 sinit = mtod(m, struct sctp_initmsg *);
2610 SCTP_INP_RLOCK(inp);
2611 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2612 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2613 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2614 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2615 SCTP_INP_RUNLOCK(inp);
2616 m->m_len = sizeof(*sinit);
2618 break;
2619 case SCTP_PRIMARY_ADDR:
2620 /* we allow a "get" operation on this */
2622 struct sctp_setprim *ssp;
2624 #ifdef SCTP_DEBUG
2625 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2626 kprintf("setprimary\n");
2628 #endif /* SCTP_DEBUG */
2629 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
2630 error = EINVAL;
2631 break;
2633 ssp = mtod(m, struct sctp_setprim *);
2634 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2635 SCTP_INP_RLOCK(inp);
2636 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2637 if (stcb)
2638 SCTP_TCB_LOCK(stcb);
2639 SCTP_INP_RUNLOCK(inp);
2640 } else {
2641 stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id);
2642 if (stcb == NULL) {
2643 /* one last shot, try it by the address in */
2644 struct sctp_nets *net;
2646 SCTP_INP_WLOCK(inp);
2647 SCTP_INP_INCR_REF(inp);
2648 SCTP_INP_WUNLOCK(inp);
2649 stcb = sctp_findassociation_ep_addr(&inp,
2650 (struct sockaddr *)&ssp->ssp_addr,
2651 &net, NULL, NULL);
2652 if (stcb == NULL) {
2653 SCTP_INP_WLOCK(inp);
2654 SCTP_INP_DECR_REF(inp);
2655 SCTP_INP_WUNLOCK(inp);
2658 if (stcb == NULL) {
2659 error = EINVAL;
2660 break;
2663 /* simply copy out the sockaddr_storage... */
2664 memcpy(&ssp->ssp_addr,
2665 &stcb->asoc.primary_destination->ro._l_addr,
2666 ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2667 SCTP_TCB_UNLOCK(stcb);
2668 m->m_len = sizeof(*ssp);
2670 break;
2671 default:
2672 error = ENOPROTOOPT;
2673 m->m_len = 0;
2674 break;
2675 } /* end switch (sopt->sopt_name) */
2676 return (error);
2679 static int
2680 sctp_optsset(struct socket *so,
2681 int opt,
2682 struct mbuf **mp,
2683 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
2684 struct thread *p
2685 #else
2686 struct proc *p
2687 #endif
2690 int error, *mopt, set_opt;
2691 struct mbuf *m;
2692 struct sctp_tcb *stcb = NULL;
2693 struct sctp_inpcb *inp;
2695 if (mp == NULL) {
2696 #ifdef SCTP_DEBUG
2697 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2698 kprintf("optsset:MP is NULL EINVAL\n");
2700 #endif /* SCTP_DEBUG */
2701 return (EINVAL);
2703 m = *mp;
2704 if (m == NULL)
2705 return (EINVAL);
2707 inp = (struct sctp_inpcb *)so->so_pcb;
2708 if (inp == 0)
2709 return EINVAL;
2711 error = 0;
2712 switch (opt) {
2713 case SCTP_NODELAY:
2714 case SCTP_AUTOCLOSE:
2715 case SCTP_AUTO_ASCONF:
2716 case SCTP_DISABLE_FRAGMENTS:
2717 case SCTP_I_WANT_MAPPED_V4_ADDR:
2718 /* copy in the option value */
2719 if ((size_t)m->m_len < sizeof(int)) {
2720 error = EINVAL;
2721 break;
2723 mopt = mtod(m, int *);
2724 set_opt = 0;
2725 if (error)
2726 break;
2727 switch (opt) {
2728 case SCTP_DISABLE_FRAGMENTS:
2729 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2730 break;
2731 case SCTP_AUTO_ASCONF:
2732 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2733 break;
2735 case SCTP_I_WANT_MAPPED_V4_ADDR:
2736 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2737 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2738 } else {
2739 return (EINVAL);
2741 break;
2742 case SCTP_NODELAY:
2743 set_opt = SCTP_PCB_FLAGS_NODELAY;
2744 break;
2745 case SCTP_AUTOCLOSE:
2746 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2748 * The value is in ticks.
2749 * Note this does not effect old associations, only
2750 * new ones.
2752 inp->sctp_ep.auto_close_time = (*mopt * hz);
2753 break;
2755 SCTP_INP_WLOCK(inp);
2756 if (*mopt != 0) {
2757 inp->sctp_flags |= set_opt;
2758 } else {
2759 inp->sctp_flags &= ~set_opt;
2761 SCTP_INP_WUNLOCK(inp);
2762 break;
2763 case SCTP_MY_PUBLIC_KEY: /* set my public key */
2764 case SCTP_SET_AUTH_CHUNKS: /* set the authenticated chunks required */
2765 case SCTP_SET_AUTH_SECRET: /* set the actual secret for the endpoint */
2766 /* not supported yet and until we refine the draft */
2767 error = EOPNOTSUPP;
2768 break;
2770 case SCTP_CLR_STAT_LOG:
2771 #ifdef SCTP_STAT_LOGGING
2772 sctp_clr_stat_log();
2773 #else
2774 error = EOPNOTSUPP;
2775 #endif
2776 break;
2777 case SCTP_DELAYED_ACK_TIME:
2779 int32_t *tm;
2780 if ((size_t)m->m_len < sizeof(int32_t)) {
2781 error = EINVAL;
2782 break;
2784 tm = mtod(m, int32_t *);
2786 if ((*tm < 10) || (*tm > 500)) {
2787 /* can't be smaller than 10ms */
2788 /* MUST NOT be larger than 500ms */
2789 error = EINVAL;
2790 break;
2792 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm);
2794 break;
2795 case SCTP_RESET_STREAMS:
2797 struct sctp_stream_reset *strrst;
2798 uint8_t two_way, not_peer;
2800 if ((size_t)m->m_len < sizeof(struct sctp_stream_reset)) {
2801 error = EINVAL;
2802 break;
2804 strrst = mtod(m, struct sctp_stream_reset *);
2806 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2807 SCTP_INP_RLOCK(inp);
2808 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2809 if (stcb)
2810 SCTP_TCB_LOCK(stcb);
2811 SCTP_INP_RUNLOCK(inp);
2812 } else
2813 stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id);
2814 if (stcb == NULL) {
2815 error = ENOENT;
2816 break;
2818 if (stcb->asoc.peer_supports_strreset == 0) {
2819 /* Peer does not support it,
2820 * we return protocol not supported since
2821 * this is true for this feature and this
2822 * peer, not the socket request in general.
2824 error = EPROTONOSUPPORT;
2825 SCTP_TCB_UNLOCK(stcb);
2826 break;
2829 /* Having re-thought this code I added as I write the I-D there
2830 * is NO need for it. The peer, if we are requesting a stream-reset
2831 * will send a request to us but will itself do what we do, take
2832 * and copy off the "reset information" we send and queue TSN's
2833 * larger than the send-next in our response message. Thus they
2834 * will handle it.
2836 /* if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/
2837 /* Must have all sending data ack'd before we
2838 * start this procedure. This is a bit restrictive
2839 * and we SHOULD work on changing this so ONLY the
2840 * streams being RESET get held up. So, a reset-all
2841 * would require this.. but a reset specific just
2842 * needs to be sure that the ones being reset have
2843 * nothing on the send_queue. For now we will
2844 * skip this more detailed method and do a course
2845 * way.. i.e. nothing pending ... for future FIX ME!
2847 /* error = EBUSY;*/
2848 /* break;*/
2849 /* }*/
2851 if (stcb->asoc.stream_reset_outstanding) {
2852 error = EALREADY;
2853 SCTP_TCB_UNLOCK(stcb);
2854 break;
2856 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2857 two_way = 0;
2858 not_peer = 0;
2859 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2860 two_way = 1;
2861 not_peer = 1;
2862 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2863 two_way = 1;
2864 not_peer = 0;
2865 } else {
2866 error = EINVAL;
2867 SCTP_TCB_UNLOCK(stcb);
2868 break;
2870 sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2871 strrst->strrst_list, two_way, not_peer);
2872 crit_enter();
2873 sctp_chunk_output(inp, stcb, 12);
2874 SCTP_TCB_UNLOCK(stcb);
2875 crit_exit();
2878 break;
2879 case SCTP_RESET_PEGS:
2880 memset(sctp_pegs, 0, sizeof(sctp_pegs));
2881 error = 0;
2882 break;
2883 case SCTP_CONNECT_X:
2884 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2885 error = EINVAL;
2886 break;
2888 error = sctp_do_connect_x(so, inp, m, p, 0);
2889 break;
2891 case SCTP_CONNECT_X_DELAYED:
2892 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2893 error = EINVAL;
2894 break;
2896 error = sctp_do_connect_x(so, inp, m, p, 1);
2897 break;
2899 case SCTP_CONNECT_X_COMPLETE:
2901 struct sockaddr *sa;
2902 struct sctp_nets *net;
2903 if ((size_t)m->m_len < sizeof(struct sockaddr_in)) {
2904 error = EINVAL;
2905 break;
2907 sa = mtod(m, struct sockaddr *);
2908 /* find tcb */
2909 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2910 SCTP_INP_RLOCK(inp);
2911 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2912 if (stcb) {
2913 SCTP_TCB_LOCK(stcb);
2914 net = sctp_findnet(stcb, sa);
2916 SCTP_INP_RUNLOCK(inp);
2917 } else {
2918 SCTP_INP_WLOCK(inp);
2919 SCTP_INP_INCR_REF(inp);
2920 SCTP_INP_WUNLOCK(inp);
2921 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2922 if (stcb == NULL) {
2923 SCTP_INP_WLOCK(inp);
2924 SCTP_INP_DECR_REF(inp);
2925 SCTP_INP_WUNLOCK(inp);
2929 if (stcb == NULL) {
2930 error = ENOENT;
2931 break;
2933 if (stcb->asoc.delayed_connection == 1) {
2934 stcb->asoc.delayed_connection = 0;
2935 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2936 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
2937 sctp_send_initiate(inp, stcb);
2938 } else {
2939 /* already expired or did not use delayed connectx */
2940 error = EALREADY;
2942 SCTP_TCB_UNLOCK(stcb);
2944 break;
2945 case SCTP_MAXBURST:
2947 u_int8_t *burst;
2948 SCTP_INP_WLOCK(inp);
2949 burst = mtod(m, u_int8_t *);
2950 if (*burst) {
2951 inp->sctp_ep.max_burst = *burst;
2953 SCTP_INP_WUNLOCK(inp);
2955 break;
2956 case SCTP_MAXSEG:
2958 u_int32_t *segsize;
2959 int ovh;
2960 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2961 ovh = SCTP_MED_OVERHEAD;
2962 } else {
2963 ovh = SCTP_MED_V4_OVERHEAD;
2965 segsize = mtod(m, u_int32_t *);
2966 if (*segsize < 1) {
2967 error = EINVAL;
2968 break;
2970 SCTP_INP_WLOCK(inp);
2971 inp->sctp_frag_point = (*segsize+ovh);
2972 if (inp->sctp_frag_point < MHLEN) {
2973 inp->sctp_frag_point = MHLEN;
2975 SCTP_INP_WUNLOCK(inp);
2977 break;
2978 case SCTP_SET_DEBUG_LEVEL:
2979 #ifdef SCTP_DEBUG
2981 u_int32_t *level;
2982 if ((size_t)m->m_len < sizeof(u_int32_t)) {
2983 error = EINVAL;
2984 break;
2986 level = mtod(m, u_int32_t *);
2987 error = 0;
2988 sctp_debug_on = (*level & (SCTP_DEBUG_ALL |
2989 SCTP_DEBUG_NOISY));
2990 kprintf("SETTING DEBUG LEVEL to %x\n",
2991 (u_int)sctp_debug_on);
2994 #else
2995 error = EOPNOTSUPP;
2996 #endif /* SCTP_DEBUG */
2997 break;
2998 case SCTP_EVENTS:
3000 struct sctp_event_subscribe *events;
3001 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
3002 error = EINVAL;
3003 break;
3005 SCTP_INP_WLOCK(inp);
3006 events = mtod(m, struct sctp_event_subscribe *);
3007 if (events->sctp_data_io_event) {
3008 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT;
3009 } else {
3010 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT;
3013 if (events->sctp_association_event) {
3014 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT;
3015 } else {
3016 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT;
3019 if (events->sctp_address_event) {
3020 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT;
3021 } else {
3022 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT;
3025 if (events->sctp_send_failure_event) {
3026 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3027 } else {
3028 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
3031 if (events->sctp_peer_error_event) {
3032 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR;
3033 } else {
3034 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR;
3037 if (events->sctp_shutdown_event) {
3038 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3039 } else {
3040 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3043 if (events->sctp_partial_delivery_event) {
3044 inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT;
3045 } else {
3046 inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT;
3049 if (events->sctp_adaption_layer_event) {
3050 inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT;
3051 } else {
3052 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT;
3055 if (events->sctp_stream_reset_events) {
3056 inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3057 } else {
3058 inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3060 SCTP_INP_WUNLOCK(inp);
3062 break;
3064 case SCTP_ADAPTION_LAYER:
3066 struct sctp_setadaption *adap_bits;
3067 if ((size_t)m->m_len < sizeof(struct sctp_setadaption)) {
3068 error = EINVAL;
3069 break;
3071 SCTP_INP_WLOCK(inp);
3072 adap_bits = mtod(m, struct sctp_setadaption *);
3073 inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind;
3074 SCTP_INP_WUNLOCK(inp);
3076 break;
3077 case SCTP_SET_INITIAL_DBG_SEQ:
3079 u_int32_t *vvv;
3080 if ((size_t)m->m_len < sizeof(u_int32_t)) {
3081 error = EINVAL;
3082 break;
3084 SCTP_INP_WLOCK(inp);
3085 vvv = mtod(m, u_int32_t *);
3086 inp->sctp_ep.initial_sequence_debug = *vvv;
3087 SCTP_INP_WUNLOCK(inp);
3089 break;
3090 case SCTP_DEFAULT_SEND_PARAM:
3092 struct sctp_sndrcvinfo *s_info;
3094 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
3095 error = EINVAL;
3096 break;
3098 s_info = mtod(m, struct sctp_sndrcvinfo *);
3100 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3101 SCTP_INP_RLOCK(inp);
3102 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3103 if (stcb)
3104 SCTP_TCB_LOCK(stcb);
3105 SCTP_INP_RUNLOCK(inp);
3106 } else
3107 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
3109 if (stcb == NULL) {
3110 error = ENOENT;
3111 break;
3113 /* Validate things */
3114 if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) {
3115 SCTP_TCB_UNLOCK(stcb);
3116 error = EINVAL;
3117 break;
3119 /* Mask off the flags that are allowed */
3120 s_info->sinfo_flags = (s_info->sinfo_flags &
3121 (MSG_UNORDERED | MSG_ADDR_OVER |
3122 MSG_PR_SCTP_TTL | MSG_PR_SCTP_BUF));
3123 /* Copy it in */
3124 stcb->asoc.def_send = *s_info;
3125 SCTP_TCB_UNLOCK(stcb);
3127 break;
3128 case SCTP_PEER_ADDR_PARAMS:
3130 struct sctp_paddrparams *paddrp;
3131 struct sctp_nets *net;
3132 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
3133 error = EINVAL;
3134 break;
3136 paddrp = mtod(m, struct sctp_paddrparams *);
3137 net = NULL;
3138 if (paddrp->spp_assoc_id) {
3139 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3140 SCTP_INP_RLOCK(inp);
3141 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3142 if (stcb) {
3143 SCTP_TCB_LOCK(stcb);
3144 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3146 SCTP_INP_RUNLOCK(inp);
3147 } else
3148 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
3149 if (stcb == NULL) {
3150 error = ENOENT;
3151 break;
3155 if ((stcb == NULL) &&
3156 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
3157 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
3158 /* Lookup via address */
3159 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3160 SCTP_INP_RLOCK(inp);
3161 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3162 if (stcb) {
3163 SCTP_TCB_LOCK(stcb);
3164 net = sctp_findnet(stcb,
3165 (struct sockaddr *)&paddrp->spp_address);
3167 SCTP_INP_RUNLOCK(inp);
3168 } else {
3169 SCTP_INP_WLOCK(inp);
3170 SCTP_INP_INCR_REF(inp);
3171 SCTP_INP_WUNLOCK(inp);
3172 stcb = sctp_findassociation_ep_addr(&inp,
3173 (struct sockaddr *)&paddrp->spp_address,
3174 &net, NULL, NULL);
3175 if (stcb == NULL) {
3176 SCTP_INP_WLOCK(inp);
3177 SCTP_INP_DECR_REF(inp);
3178 SCTP_INP_WUNLOCK(inp);
3181 } else {
3182 /* Effects the Endpoint */
3183 stcb = NULL;
3185 if (stcb) {
3186 /* Applies to the specific association */
3187 if (paddrp->spp_pathmaxrxt) {
3188 if (net) {
3189 if (paddrp->spp_pathmaxrxt)
3190 net->failure_threshold = paddrp->spp_pathmaxrxt;
3191 } else {
3192 if (paddrp->spp_pathmaxrxt)
3193 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3196 if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) {
3197 /* Just a set */
3198 int old;
3199 if (net) {
3200 net->dest_state &= ~SCTP_ADDR_NOHB;
3201 } else {
3202 old = stcb->asoc.heart_beat_delay;
3203 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3204 if (old == 0) {
3205 /* Turn back on the timer */
3206 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3209 } else if (paddrp->spp_hbinterval == 0xffffffff) {
3210 /* on demand HB */
3211 sctp_send_hb(stcb, 1, net);
3212 } else {
3213 if (net == NULL) {
3214 /* off on association */
3215 if (stcb->asoc.heart_beat_delay) {
3216 int cnt_of_unconf = 0;
3217 struct sctp_nets *lnet;
3218 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3219 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3220 cnt_of_unconf++;
3223 /* stop the timer ONLY if we have no unconfirmed addresses
3225 if (cnt_of_unconf == 0)
3226 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3228 stcb->asoc.heart_beat_delay = 0;
3229 } else {
3230 net->dest_state |= SCTP_ADDR_NOHB;
3233 SCTP_TCB_UNLOCK(stcb);
3234 } else {
3235 /* Use endpoint defaults */
3236 SCTP_INP_WLOCK(inp);
3237 if (paddrp->spp_pathmaxrxt)
3238 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3239 if (paddrp->spp_hbinterval != SCTP_ISSUE_HB)
3240 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval;
3241 SCTP_INP_WUNLOCK(inp);
3244 break;
3245 case SCTP_RTOINFO:
3247 struct sctp_rtoinfo *srto;
3248 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
3249 error = EINVAL;
3250 break;
3252 srto = mtod(m, struct sctp_rtoinfo *);
3253 if (srto->srto_assoc_id == 0) {
3254 SCTP_INP_WLOCK(inp);
3255 /* If we have a null asoc, its default for the endpoint */
3256 if (srto->srto_initial > 10)
3257 inp->sctp_ep.initial_rto = srto->srto_initial;
3258 if (srto->srto_max > 10)
3259 inp->sctp_ep.sctp_maxrto = srto->srto_max;
3260 if (srto->srto_min > 10)
3261 inp->sctp_ep.sctp_minrto = srto->srto_min;
3262 SCTP_INP_WUNLOCK(inp);
3263 break;
3265 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3266 SCTP_INP_RLOCK(inp);
3267 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3268 if (stcb)
3269 SCTP_TCB_LOCK(stcb);
3270 SCTP_INP_RUNLOCK(inp);
3271 } else
3272 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
3273 if (stcb == NULL) {
3274 error = EINVAL;
3275 break;
3277 /* Set in ms we hope :-) */
3278 if (srto->srto_initial > 10)
3279 stcb->asoc.initial_rto = srto->srto_initial;
3280 if (srto->srto_max > 10)
3281 stcb->asoc.maxrto = srto->srto_max;
3282 if (srto->srto_min > 10)
3283 stcb->asoc.minrto = srto->srto_min;
3284 SCTP_TCB_UNLOCK(stcb);
3286 break;
3287 case SCTP_ASSOCINFO:
3289 struct sctp_assocparams *sasoc;
3291 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
3292 error = EINVAL;
3293 break;
3295 sasoc = mtod(m, struct sctp_assocparams *);
3296 if (sasoc->sasoc_assoc_id) {
3297 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3298 SCTP_INP_RLOCK(inp);
3299 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3300 if (stcb)
3301 SCTP_TCB_LOCK(stcb);
3302 SCTP_INP_RUNLOCK(inp);
3303 } else
3304 stcb = sctp_findassociation_ep_asocid(inp,
3305 sasoc->sasoc_assoc_id);
3306 if (stcb == NULL) {
3307 error = ENOENT;
3308 break;
3311 } else {
3312 stcb = NULL;
3314 if (stcb) {
3315 if (sasoc->sasoc_asocmaxrxt)
3316 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3317 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3318 sasoc->sasoc_peer_rwnd = 0;
3319 sasoc->sasoc_local_rwnd = 0;
3320 if (stcb->asoc.cookie_life)
3321 stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3322 SCTP_TCB_UNLOCK(stcb);
3323 } else {
3324 SCTP_INP_WLOCK(inp);
3325 if (sasoc->sasoc_asocmaxrxt)
3326 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3327 sasoc->sasoc_number_peer_destinations = 0;
3328 sasoc->sasoc_peer_rwnd = 0;
3329 sasoc->sasoc_local_rwnd = 0;
3330 if (sasoc->sasoc_cookie_life)
3331 inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3332 SCTP_INP_WUNLOCK(inp);
3335 break;
3336 case SCTP_INITMSG:
3338 struct sctp_initmsg *sinit;
3340 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
3341 error = EINVAL;
3342 break;
3344 sinit = mtod(m, struct sctp_initmsg *);
3345 SCTP_INP_WLOCK(inp);
3346 if (sinit->sinit_num_ostreams)
3347 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3349 if (sinit->sinit_max_instreams)
3350 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3352 if (sinit->sinit_max_attempts)
3353 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3355 if (sinit->sinit_max_init_timeo > 10)
3356 /* We must be at least a 100ms (we set in ticks) */
3357 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3358 SCTP_INP_WUNLOCK(inp);
3360 break;
3361 case SCTP_PRIMARY_ADDR:
3363 struct sctp_setprim *spa;
3364 struct sctp_nets *net, *lnet;
3365 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
3366 error = EINVAL;
3367 break;
3369 spa = mtod(m, struct sctp_setprim *);
3371 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3372 SCTP_INP_RLOCK(inp);
3373 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3374 if (stcb) {
3375 SCTP_TCB_LOCK(stcb);
3376 } else {
3377 error = EINVAL;
3378 break;
3380 SCTP_INP_RUNLOCK(inp);
3381 } else
3382 stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id);
3383 if (stcb == NULL) {
3384 /* One last shot */
3385 SCTP_INP_WLOCK(inp);
3386 SCTP_INP_INCR_REF(inp);
3387 SCTP_INP_WUNLOCK(inp);
3388 stcb = sctp_findassociation_ep_addr(&inp,
3389 (struct sockaddr *)&spa->ssp_addr,
3390 &net, NULL, NULL);
3391 if (stcb == NULL) {
3392 SCTP_INP_WLOCK(inp);
3393 SCTP_INP_DECR_REF(inp);
3394 SCTP_INP_WUNLOCK(inp);
3395 error = EINVAL;
3396 break;
3398 } else {
3399 /* find the net, associd or connected lookup type */
3400 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3401 if (net == NULL) {
3402 SCTP_TCB_UNLOCK(stcb);
3403 error = EINVAL;
3404 break;
3407 if ((net != stcb->asoc.primary_destination) &&
3408 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3409 /* Ok we need to set it */
3410 lnet = stcb->asoc.primary_destination;
3411 lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq;
3412 if (sctp_set_primary_addr(stcb,
3413 (struct sockaddr *)NULL,
3414 net) == 0) {
3415 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3416 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3418 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3421 SCTP_TCB_UNLOCK(stcb);
3423 break;
3425 case SCTP_SET_PEER_PRIMARY_ADDR:
3427 struct sctp_setpeerprim *sspp;
3428 if ((size_t)m->m_len < sizeof(struct sctp_setpeerprim)) {
3429 error = EINVAL;
3430 break;
3432 sspp = mtod(m, struct sctp_setpeerprim *);
3435 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3436 SCTP_INP_RLOCK(inp);
3437 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3438 if (stcb)
3439 SCTP_TCB_UNLOCK(stcb);
3440 SCTP_INP_RUNLOCK(inp);
3441 } else
3442 stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id);
3443 if (stcb == NULL) {
3444 error = EINVAL;
3445 break;
3447 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3448 error = EINVAL;
3450 SCTP_TCB_UNLOCK(stcb);
3452 break;
3453 case SCTP_BINDX_ADD_ADDR:
3455 struct sctp_getaddresses *addrs;
3456 struct sockaddr *addr_touse;
3457 struct sockaddr_in sin;
3458 /* see if we're bound all already! */
3459 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3460 error = EINVAL;
3461 break;
3463 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
3464 error = EINVAL;
3465 break;
3467 addrs = mtod(m, struct sctp_getaddresses *);
3468 addr_touse = addrs->addr;
3469 if (addrs->addr->sa_family == AF_INET6) {
3470 struct sockaddr_in6 *sin6;
3471 sin6 = (struct sockaddr_in6 *)addr_touse;
3472 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3473 in6_sin6_2_sin(&sin, sin6);
3474 addr_touse = (struct sockaddr *)&sin;
3477 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3478 if (p == NULL) {
3479 /* Can't get proc for Net/Open BSD */
3480 error = EINVAL;
3481 break;
3483 error = sctp_inpcb_bind(so, addr_touse, p);
3484 break;
3486 /* No locks required here since bind and mgmt_ep_sa all
3487 * do their own locking. If we do something for the FIX:
3488 * below we may need to lock in that case.
3490 if (addrs->sget_assoc_id == 0) {
3491 /* add the address */
3492 struct sctp_inpcb *lep;
3493 ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3494 lep = sctp_pcb_findep(addr_touse, 1, 0);
3495 if (lep != NULL) {
3496 /* We must decrement the refcount
3497 * since we have the ep already and
3498 * are binding. No remove going on
3499 * here.
3501 SCTP_INP_WLOCK(inp);
3502 SCTP_INP_DECR_REF(inp);
3503 SCTP_INP_WUNLOCK(inp);
3505 if (lep == inp) {
3506 /* already bound to it.. ok */
3507 break;
3508 } else if (lep == NULL) {
3509 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
3510 error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3511 SCTP_ADD_IP_ADDRESS);
3512 } else {
3513 error = EADDRNOTAVAIL;
3515 if (error)
3516 break;
3518 } else {
3519 /* FIX: decide whether we allow assoc based bindx */
3522 break;
3523 case SCTP_BINDX_REM_ADDR:
3525 struct sctp_getaddresses *addrs;
3526 struct sockaddr *addr_touse;
3527 struct sockaddr_in sin;
3528 /* see if we're bound all already! */
3529 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3530 error = EINVAL;
3531 break;
3533 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
3534 error = EINVAL;
3535 break;
3537 addrs = mtod(m, struct sctp_getaddresses *);
3538 addr_touse = addrs->addr;
3539 if (addrs->addr->sa_family == AF_INET6) {
3540 struct sockaddr_in6 *sin6;
3541 sin6 = (struct sockaddr_in6 *)addr_touse;
3542 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3543 in6_sin6_2_sin(&sin, sin6);
3544 addr_touse = (struct sockaddr *)&sin;
3547 /* No lock required mgmt_ep_sa does its own locking. If
3548 * the FIX: below is ever changed we may need to
3549 * lock before calling association level binding.
3551 if (addrs->sget_assoc_id == 0) {
3552 /* delete the address */
3553 sctp_addr_mgmt_ep_sa(inp, addr_touse,
3554 SCTP_DEL_IP_ADDRESS);
3555 } else {
3556 /* FIX: decide whether we allow assoc based bindx */
3559 break;
3560 default:
3561 error = ENOPROTOOPT;
3562 break;
3563 } /* end switch (opt) */
3564 return (error);
3568 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
3570 sctp_ctloutput(struct socket *so, struct sockopt *sopt)
3572 struct mbuf *m = NULL;
3573 struct sctp_inpcb *inp;
3574 int error;
3576 inp = (struct sctp_inpcb *)so->so_pcb;
3577 crit_enter();
3578 if (inp == 0) {
3579 crit_exit();
3580 /* I made the same as TCP since we are not setup? */
3581 return (ECONNRESET);
3583 if (sopt->sopt_level != IPPROTO_SCTP) {
3584 /* wrong proto level... send back up to IP */
3585 #ifdef INET6
3586 if (INP_CHECK_SOCKAF(so, AF_INET6))
3587 error = ip6_ctloutput(so, sopt);
3588 else
3589 #endif /* INET6 */
3590 error = ip_ctloutput(so, sopt);
3591 crit_exit();
3592 return (error);
3594 if (sopt->sopt_valsize > MCLBYTES) {
3596 * Restrict us down to a cluster size, that's all we can
3597 * pass either way...
3599 sopt->sopt_valsize = MCLBYTES;
3601 if (sopt->sopt_valsize) {
3603 m = m_get(MB_WAIT, MT_DATA);
3604 if (sopt->sopt_valsize > MLEN) {
3605 MCLGET(m, MB_DONTWAIT);
3606 if ((m->m_flags & M_EXT) == 0) {
3607 sctp_m_freem(m);
3608 crit_exit();
3609 return (ENOBUFS);
3612 error = sooptcopyin(sopt, mtod(m, caddr_t), sopt->sopt_valsize,
3613 sopt->sopt_valsize);
3614 if (error) {
3615 m_free(m);
3616 goto out;
3618 m->m_len = sopt->sopt_valsize;
3620 if (sopt->sopt_dir == SOPT_SET) {
3621 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3622 error = sctp_optsset(so, sopt->sopt_name, &m, sopt->sopt_td);
3623 #else
3624 error = sctp_optsset(so, sopt->sopt_name, &m, sopt->sopt_p);
3625 #endif
3626 } else if (sopt->sopt_dir == SOPT_GET) {
3627 #if (defined (__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3628 error = sctp_optsget(so, sopt->sopt_name, &m, sopt->sopt_td);
3629 #else
3630 error = sctp_optsget(so, sopt->sopt_name, &m, sopt->sopt_p);
3631 #endif
3632 } else {
3633 error = EINVAL;
3635 if ( (error == 0) && (m != NULL)) {
3636 error = sooptcopyout(sopt, mtod(m, caddr_t), m->m_len);
3637 sctp_m_freem(m);
3638 } else if (m != NULL) {
3639 sctp_m_freem(m);
3641 out:
3642 crit_exit();
3643 return (error);
3646 #else
3647 /* NetBSD and OpenBSD */
3649 sctp_ctloutput(int op, struct socket *so, int level, int optname,
3650 struct mbuf **mp)
3652 int s, error;
3653 struct inpcb *inp;
3654 #ifdef INET6
3655 struct in6pcb *in6p;
3656 #endif
3657 int family; /* family of the socket */
3659 family = so->so_proto->pr_domain->dom_family;
3660 error = 0;
3661 crit_exit();
3662 switch (family) {
3663 case PF_INET:
3664 inp = sotoinpcb(so);
3665 #ifdef INET6
3666 in6p = NULL;
3667 #endif
3668 break;
3669 #ifdef INET6
3670 case PF_INET6:
3671 inp = NULL;
3672 in6p = sotoin6pcb(so);
3673 break;
3674 #endif
3675 default: