arcmsr(4): Use MSI if it is supported by the device.
[dragonfly.git] / sys / netinet / sctp_usrreq.c
blob539be7a4bf75042e544f7e3965cffb3f2d4fbd69
1 /* $KAME: sctp_usrreq.c,v 1.47 2005/03/06 16:04:18 itojun Exp $ */
3 /*
4 * Copyright (c) 2001, 2002, 2003, 2004 Cisco Systems, Inc.
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Cisco Systems, Inc.
18 * 4. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY CISCO SYSTEMS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL CISCO SYSTEMS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 #if !(defined(__OpenBSD__) || defined(__APPLE__))
35 #include "opt_ipsec.h"
36 #endif
37 #if defined(__FreeBSD__) || defined(__DragonFly__)
38 #include "opt_inet6.h"
39 #include "opt_inet.h"
40 #endif
41 #if defined(__NetBSD__)
42 #include "opt_inet.h"
43 #endif
45 #ifdef __APPLE__
46 #include <sctp.h>
47 #elif !defined(__OpenBSD__)
48 #include "opt_sctp.h"
49 #endif
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/kernel.h>
54 #include <sys/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/domain.h>
57 #include <sys/proc.h>
58 #include <sys/priv.h>
59 #include <sys/protosw.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/socketvar2.h>
63 #include <sys/sysctl.h>
64 #include <sys/syslog.h>
66 #include <sys/thread2.h>
67 #include <sys/msgport2.h>
69 #include <net/if.h>
70 #include <net/if_types.h>
71 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
72 #include <net/if_var.h>
73 #endif
74 #include <net/route.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/ip.h>
78 #include <netinet/ip6.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
82 #include <netinet6/ip6_var.h>
83 #include <netinet6/in6_var.h>
85 #include <netinet/ip_icmp.h>
86 #include <netinet/icmp_var.h>
87 #include <netinet/sctp_pcb.h>
88 #include <netinet/sctp_header.h>
89 #include <netinet/sctp_var.h>
90 #include <netinet/sctp_output.h>
91 #include <netinet/sctp_uio.h>
92 #include <netinet/sctp_asconf.h>
93 #include <netinet/sctputil.h>
94 #include <netinet/sctp_indata.h>
95 #include <netinet/sctp_asconf.h>
96 #ifdef IPSEC
97 #ifndef __OpenBSD__
98 #include <netinet6/ipsec.h>
99 #include <netproto/key/key.h>
100 #else
101 #undef IPSEC
102 #endif
103 #endif /* IPSEC */
105 #include <net/net_osdep.h>
107 #if defined(HAVE_NRL_INPCB) || defined(__FreeBSD__) || defined(__DragonFly__)
108 #ifndef in6pcb
109 #define in6pcb inpcb
110 #endif
111 #ifndef sotoin6pcb
112 #define sotoin6pcb sotoinpcb
113 #endif
114 #endif
116 #ifdef SCTP_DEBUG
117 extern u_int32_t sctp_debug_on;
118 #endif /* SCTP_DEBUG */
121 * sysctl tunable variables
123 int sctp_auto_asconf = SCTP_DEFAULT_AUTO_ASCONF;
124 int sctp_max_burst_default = SCTP_DEF_MAX_BURST;
125 int sctp_peer_chunk_oh = sizeof(struct mbuf);
126 int sctp_strict_init = 1;
127 int sctp_no_csum_on_loopback = 1;
128 unsigned int sctp_max_chunks_on_queue = SCTP_ASOC_MAX_CHUNKS_ON_QUEUE;
129 int sctp_sendspace = (128 * 1024);
130 int sctp_recvspace = 128 * (1024 +
131 #ifdef INET6
132 sizeof(struct sockaddr_in6)
133 #else
134 sizeof(struct sockaddr_in)
135 #endif
137 int sctp_strict_sacks = 0;
138 int sctp_ecn = 1;
139 int sctp_ecn_nonce = 0;
141 unsigned int sctp_delayed_sack_time_default = SCTP_RECV_MSEC;
142 unsigned int sctp_heartbeat_interval_default = SCTP_HB_DEFAULT_MSEC;
143 unsigned int sctp_pmtu_raise_time_default = SCTP_DEF_PMTU_RAISE_SEC;
144 unsigned int sctp_shutdown_guard_time_default = SCTP_DEF_MAX_SHUTDOWN_SEC;
145 unsigned int sctp_secret_lifetime_default = SCTP_DEFAULT_SECRET_LIFE_SEC;
146 unsigned int sctp_rto_max_default = SCTP_RTO_UPPER_BOUND;
147 unsigned int sctp_rto_min_default = SCTP_RTO_LOWER_BOUND;
148 unsigned int sctp_rto_initial_default = SCTP_RTO_INITIAL;
149 unsigned int sctp_init_rto_max_default = SCTP_RTO_UPPER_BOUND;
150 unsigned int sctp_valid_cookie_life_default = SCTP_DEFAULT_COOKIE_LIFE;
151 unsigned int sctp_init_rtx_max_default = SCTP_DEF_MAX_INIT;
152 unsigned int sctp_assoc_rtx_max_default = SCTP_DEF_MAX_SEND;
153 unsigned int sctp_path_rtx_max_default = SCTP_DEF_MAX_SEND/2;
154 unsigned int sctp_nr_outgoing_streams_default = SCTP_OSTREAM_INITIAL;
156 void
157 sctp_init(void)
159 #ifdef __OpenBSD__
160 #define nmbclusters nmbclust
161 #endif
162 /* Init the SCTP pcb in sctp_pcb.c */
163 u_long sb_max_adj;
165 sctp_pcb_init();
167 #ifndef __OpenBSD__
168 if (nmbclusters > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
169 sctp_max_chunks_on_queue = nmbclusters;
170 #else
171 /* if (nmbclust > SCTP_ASOC_MAX_CHUNKS_ON_QUEUE)
172 sctp_max_chunks_on_queue = nmbclust; FIX ME */
173 sctp_max_chunks_on_queue = nmbclust * 2;
174 #endif
176 * Allow a user to take no more than 1/2 the number of clusters
177 * or the SB_MAX whichever is smaller for the send window.
179 sb_max_adj = (u_long)((u_quad_t)(SB_MAX) * MCLBYTES / (MSIZE + MCLBYTES));
180 sctp_sendspace = min((min(SB_MAX, sb_max_adj)),
181 #ifndef __OpenBSD__
182 ((nmbclusters/2) * SCTP_DEFAULT_MAXSEGMENT));
183 #else
184 ((nmbclust/2) * SCTP_DEFAULT_MAXSEGMENT));
185 #endif
187 * Now for the recv window, should we take the same amount?
188 * or should I do 1/2 the SB_MAX instead in the SB_MAX min above.
189 * For now I will just copy.
191 sctp_recvspace = sctp_sendspace;
192 #ifdef __OpenBSD__
193 #undef nmbclusters
194 #endif
197 #ifdef INET6
198 void
199 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
201 bzero(ip6, sizeof(*ip6));
203 ip6->ip6_vfc = IPV6_VERSION;
204 ip6->ip6_plen = ip->ip_len;
205 ip6->ip6_nxt = ip->ip_p;
206 ip6->ip6_hlim = ip->ip_ttl;
207 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
208 IPV6_ADDR_INT32_SMP;
209 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
210 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
212 #endif /* INET6 */
214 static void
215 sctp_split_chunks(struct sctp_association *asoc,
216 struct sctp_stream_out *strm,
217 struct sctp_tmit_chunk *chk)
219 struct sctp_tmit_chunk *new_chk;
221 /* First we need a chunk */
222 new_chk = (struct sctp_tmit_chunk *)SCTP_ZONE_GET(sctppcbinfo.ipi_zone_chunk);
223 if (new_chk == NULL) {
224 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
225 return;
227 sctppcbinfo.ipi_count_chunk++;
228 sctppcbinfo.ipi_gencnt_chunk++;
229 /* Copy it all */
230 *new_chk = *chk;
231 /* split the data */
232 new_chk->data = m_split(chk->data, (chk->send_size>>1), MB_DONTWAIT);
233 if (new_chk->data == NULL) {
234 /* Can't split */
235 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
236 SCTP_ZONE_FREE(sctppcbinfo.ipi_zone_chunk, new_chk);
237 sctppcbinfo.ipi_count_chunk--;
238 if ((int)sctppcbinfo.ipi_count_chunk < 0) {
239 panic("Chunk count is negative");
241 sctppcbinfo.ipi_gencnt_chunk++;
242 return;
245 /* Data is now split adjust sizes */
246 chk->send_size >>= 1;
247 new_chk->send_size >>= 1;
249 chk->book_size >>= 1;
250 new_chk->book_size >>= 1;
252 /* now adjust the marks */
253 chk->rec.data.rcv_flags |= SCTP_DATA_FIRST_FRAG;
254 chk->rec.data.rcv_flags &= ~SCTP_DATA_LAST_FRAG;
256 new_chk->rec.data.rcv_flags &= ~SCTP_DATA_FIRST_FRAG;
257 new_chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
259 /* Increase ref count if dest is set */
260 if (chk->whoTo) {
261 new_chk->whoTo->ref_count++;
263 /* now drop it on the end of the list*/
264 asoc->stream_queue_cnt++;
265 TAILQ_INSERT_AFTER(&strm->outqueue, chk, new_chk, sctp_next);
268 static void
269 sctp_notify_mbuf(struct sctp_inpcb *inp,
270 struct sctp_tcb *stcb,
271 struct sctp_nets *net,
272 struct ip *ip,
273 struct sctphdr *sh)
276 struct icmp *icmph;
277 int totsz;
278 uint16_t nxtsz;
280 /* protection */
281 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
282 (ip == NULL) || (sh == NULL)) {
283 if (stcb != NULL)
284 SCTP_TCB_UNLOCK(stcb);
285 return;
287 /* First job is to verify the vtag matches what I would send */
288 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
289 SCTP_TCB_UNLOCK(stcb);
290 return;
292 icmph = (struct icmp *)((caddr_t)ip - (sizeof(struct icmp) -
293 sizeof(struct ip)));
294 if (icmph->icmp_type != ICMP_UNREACH) {
295 /* We only care about unreachable */
296 SCTP_TCB_UNLOCK(stcb);
297 return;
299 if (icmph->icmp_code != ICMP_UNREACH_NEEDFRAG) {
300 /* not a unreachable message due to frag. */
301 SCTP_TCB_UNLOCK(stcb);
302 return;
304 totsz = ip->ip_len;
305 nxtsz = ntohs(icmph->icmp_seq);
306 if (nxtsz == 0) {
308 * old type router that does not tell us what the next size
309 * mtu is. Rats we will have to guess (in a educated fashion
310 * of course)
312 nxtsz = find_next_best_mtu(totsz);
315 /* Stop any PMTU timer */
316 sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
318 /* Adjust destination size limit */
319 if (net->mtu > nxtsz) {
320 net->mtu = nxtsz;
322 /* now what about the ep? */
323 if (stcb->asoc.smallest_mtu > nxtsz) {
324 struct sctp_tmit_chunk *chk, *nchk;
325 struct sctp_stream_out *strm;
326 /* Adjust that too */
327 stcb->asoc.smallest_mtu = nxtsz;
328 /* now off to subtract IP_DF flag if needed */
330 TAILQ_FOREACH(chk, &stcb->asoc.send_queue, sctp_next) {
331 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
332 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
335 TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
336 if ((chk->send_size+IP_HDR_SIZE) > nxtsz) {
338 * For this guy we also mark for immediate
339 * resend since we sent to big of chunk
341 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
342 if (chk->sent != SCTP_DATAGRAM_RESEND) {
343 stcb->asoc.sent_queue_retran_cnt++;
345 chk->sent = SCTP_DATAGRAM_RESEND;
346 chk->rec.data.doing_fast_retransmit = 0;
348 /* Clear any time so NO RTT is being done */
349 chk->do_rtt = 0;
350 stcb->asoc.total_flight -= chk->book_size;
351 if (stcb->asoc.total_flight < 0) {
352 stcb->asoc.total_flight = 0;
354 stcb->asoc.total_flight_count--;
355 if (stcb->asoc.total_flight_count < 0) {
356 stcb->asoc.total_flight_count = 0;
358 net->flight_size -= chk->book_size;
359 if (net->flight_size < 0) {
360 net->flight_size = 0;
364 TAILQ_FOREACH(strm, &stcb->asoc.out_wheel, next_spoke) {
365 chk = TAILQ_FIRST(&strm->outqueue);
366 while (chk) {
367 nchk = TAILQ_NEXT(chk, sctp_next);
368 if ((chk->send_size+SCTP_MED_OVERHEAD) > nxtsz) {
369 sctp_split_chunks(&stcb->asoc, strm, chk);
371 chk = nchk;
375 sctp_timer_start(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, NULL);
376 SCTP_TCB_UNLOCK(stcb);
380 void
381 sctp_notify(struct sctp_inpcb *inp,
382 int error,
383 struct sctphdr *sh,
384 struct sockaddr *to,
385 struct sctp_tcb *stcb,
386 struct sctp_nets *net)
388 /* protection */
389 if ((inp == NULL) || (stcb == NULL) || (net == NULL) ||
390 (sh == NULL) || (to == NULL)) {
391 #ifdef SCTP_DEBUG
392 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
393 kprintf("sctp-notify, bad call\n");
395 #endif /* SCTP_DEBUG */
396 return;
398 /* First job is to verify the vtag matches what I would send */
399 if (ntohl(sh->v_tag) != (stcb->asoc.peer_vtag)) {
400 return;
403 /* FIX ME FIX ME PROTOPT i.e. no SCTP should ALWAYS be an ABORT */
405 if ((error == EHOSTUNREACH) || /* Host is not reachable */
406 (error == EHOSTDOWN) || /* Host is down */
407 (error == ECONNREFUSED) || /* Host refused the connection, (not an abort?) */
408 (error == ENOPROTOOPT) /* SCTP is not present on host */
411 * Hmm reachablity problems we must examine closely.
412 * If its not reachable, we may have lost a network.
413 * Or if there is NO protocol at the other end named SCTP.
414 * well we consider it a OOTB abort.
416 if ((error == EHOSTUNREACH) || (error == EHOSTDOWN)) {
417 if (net->dest_state & SCTP_ADDR_REACHABLE) {
418 /* Ok that destination is NOT reachable */
419 net->dest_state &= ~SCTP_ADDR_REACHABLE;
420 net->dest_state |= SCTP_ADDR_NOT_REACHABLE;
421 net->error_count = net->failure_threshold + 1;
422 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
423 stcb, SCTP_FAILED_THRESHOLD,
424 (void *)net);
426 if (stcb)
427 SCTP_TCB_UNLOCK(stcb);
428 } else {
430 * Here the peer is either playing tricks on us,
431 * including an address that belongs to someone who
432 * does not support SCTP OR was a userland
433 * implementation that shutdown and now is dead. In
434 * either case treat it like a OOTB abort with no TCB
436 sctp_abort_notification(stcb, SCTP_PEER_FAULTY);
437 sctp_free_assoc(inp, stcb);
438 /* no need to unlock here, since the TCB is gone */
440 } else {
441 /* Send all others to the app */
442 if (inp->sctp_socket) {
443 SOCK_LOCK(inp->sctp_socket);
444 inp->sctp_socket->so_error = error;
445 sctp_sowwakeup(inp, inp->sctp_socket);
446 SOCK_UNLOCK(inp->sctp_socket);
448 if (stcb)
449 SCTP_TCB_UNLOCK(stcb);
453 void
454 sctp_ctlinput(netmsg_t msg)
456 int cmd = msg->ctlinput.nm_cmd;
457 struct sockaddr *sa = msg->ctlinput.nm_arg;
458 struct ip *ip = msg->ctlinput.nm_extra;
459 struct sctphdr *sh;
461 if (sa->sa_family != AF_INET ||
462 ((struct sockaddr_in *)sa)->sin_addr.s_addr == INADDR_ANY) {
463 goto out;
466 if (PRC_IS_REDIRECT(cmd)) {
467 ip = NULL;
468 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
469 goto out;
471 if (ip) {
472 struct sctp_inpcb *inp;
473 struct sctp_tcb *stcb;
474 struct sctp_nets *net;
475 struct sockaddr_in to, from;
477 sh = (struct sctphdr *)((caddr_t)ip + (ip->ip_hl << 2));
478 bzero(&to, sizeof(to));
479 bzero(&from, sizeof(from));
480 from.sin_family = to.sin_family = AF_INET;
481 from.sin_len = to.sin_len = sizeof(to);
482 from.sin_port = sh->src_port;
483 from.sin_addr = ip->ip_src;
484 to.sin_port = sh->dest_port;
485 to.sin_addr = ip->ip_dst;
488 * 'to' holds the dest of the packet that failed to be sent.
489 * 'from' holds our local endpoint address.
490 * Thus we reverse the to and the from in the lookup.
492 stcb = sctp_findassociation_addr_sa((struct sockaddr *)&from,
493 (struct sockaddr *)&to,
494 &inp, &net, 1);
495 if (stcb != NULL && inp && (inp->sctp_socket != NULL)) {
496 if (cmd != PRC_MSGSIZE) {
497 int cm;
498 if (cmd == PRC_HOSTDEAD) {
499 cm = EHOSTUNREACH;
500 } else {
501 cm = inetctlerrmap[cmd];
503 sctp_notify(inp, cm, sh,
504 (struct sockaddr *)&to, stcb,
505 net);
506 } else {
507 /* handle possible ICMP size messages */
508 sctp_notify_mbuf(inp, stcb, net, ip, sh);
510 } else {
511 #if (defined(__FreeBSD__) && __FreeBSD_version < 500000) || defined(__DragonFly__)
512 /* XXX must be fixed for 5.x and higher, leave for 4.x */
513 if (PRC_IS_REDIRECT(cmd) && inp) {
514 in_rtchange((struct inpcb *)inp,
515 inetctlerrmap[cmd]);
517 #endif
518 if ((stcb == NULL) && (inp != NULL)) {
519 /* reduce ref-count */
520 SCTP_INP_WLOCK(inp);
521 SCTP_INP_DECR_REF(inp);
522 SCTP_INP_WUNLOCK(inp);
527 out:
528 lwkt_replymsg(&msg->lmsg, 0);
531 #if defined(__FreeBSD__) || defined(__DragonFly__)
532 static int
533 sctp_getcred(SYSCTL_HANDLER_ARGS)
535 struct sockaddr_in addrs[2];
536 struct sctp_inpcb *inp;
537 struct sctp_nets *net;
538 struct sctp_tcb *stcb;
539 int error;
541 #if __FreeBSD_version >= 500000 || defined(__DragonFly__)
542 error = priv_check(req->td, PRIV_ROOT);
543 #else
544 error = suser(req->p);
545 #endif
546 if (error)
547 return (error);
548 error = SYSCTL_IN(req, addrs, sizeof(addrs));
549 if (error)
550 return (error);
552 stcb = sctp_findassociation_addr_sa(sintosa(&addrs[0]),
553 sintosa(&addrs[1]),
554 &inp, &net, 1);
555 if (stcb == NULL || inp == NULL || inp->sctp_socket == NULL) {
556 if ((inp != NULL) && (stcb == NULL)) {
557 /* reduce ref-count */
558 SCTP_INP_WLOCK(inp);
559 SCTP_INP_DECR_REF(inp);
560 SCTP_INP_WUNLOCK(inp);
562 error = ENOENT;
563 goto out;
565 error = SYSCTL_OUT(req, inp->sctp_socket->so_cred, sizeof(struct ucred));
566 SCTP_TCB_UNLOCK(stcb);
567 out:
568 return (error);
571 SYSCTL_PROC(_net_inet_sctp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
572 0, 0, sctp_getcred, "S,ucred", "Get the ucred of a SCTP connection");
573 #endif /* #if defined(__FreeBSD__) || defined(__DragonFly__) */
576 * sysctl definitions
578 #if defined(__FreeBSD__) || defined (__APPLE__) || defined(__DragonFly__)
580 SYSCTL_DECL(_net_inet);
582 SYSCTL_NODE(_net_inet, OID_AUTO, sctp, CTLFLAG_RD, 0,
583 "sctp values");
585 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxdgram, CTLFLAG_RW,
586 &sctp_sendspace, 0, "Maximum outgoing SCTP buffer size");
588 SYSCTL_INT(_net_inet_sctp, OID_AUTO, recvspace, CTLFLAG_RW,
589 &sctp_recvspace, 0, "Maximum incoming SCTP buffer size");
591 SYSCTL_INT(_net_inet_sctp, OID_AUTO, auto_asconf, CTLFLAG_RW,
592 &sctp_auto_asconf, 0, "Enable SCTP Auto-ASCONF");
594 SYSCTL_INT(_net_inet_sctp, OID_AUTO, ecn_enable, CTLFLAG_RW,
595 &sctp_ecn, 0, "Enable SCTP ECN");
597 SYSCTL_INT(_net_inet_sctp, OID_AUTO, ecn_nonce, CTLFLAG_RW,
598 &sctp_ecn_nonce, 0, "Enable SCTP ECN Nonce");
600 SYSCTL_INT(_net_inet_sctp, OID_AUTO, strict_sacks, CTLFLAG_RW,
601 &sctp_strict_sacks, 0, "Enable SCTP Strict SACK checking");
603 SYSCTL_INT(_net_inet_sctp, OID_AUTO, loopback_nocsum, CTLFLAG_RW,
604 &sctp_no_csum_on_loopback, 0,
605 "Enable NO Csum on packets sent on loopback");
607 SYSCTL_INT(_net_inet_sctp, OID_AUTO, strict_init, CTLFLAG_RW,
608 &sctp_strict_init, 0,
609 "Enable strict INIT/INIT-ACK singleton enforcement");
611 SYSCTL_INT(_net_inet_sctp, OID_AUTO, peer_chkoh, CTLFLAG_RW,
612 &sctp_peer_chunk_oh, 0,
613 "Amount to debit peers rwnd per chunk sent");
615 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxburst, CTLFLAG_RW,
616 &sctp_max_burst_default, 0,
617 "Default max burst for sctp endpoints");
619 SYSCTL_INT(_net_inet_sctp, OID_AUTO, maxchunks, CTLFLAG_RW,
620 &sctp_max_chunks_on_queue, 0,
621 "Default max chunks on queue per asoc");
623 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, delayed_sack_time, CTLFLAG_RW,
624 &sctp_delayed_sack_time_default, 0,
625 "Default delayed SACK timer in msec");
627 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, heartbeat_interval, CTLFLAG_RW,
628 &sctp_heartbeat_interval_default, 0,
629 "Default heartbeat interval in msec");
631 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, pmtu_raise_time, CTLFLAG_RW,
632 &sctp_pmtu_raise_time_default, 0,
633 "Default PMTU raise timer in sec");
635 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, shutdown_guard_time, CTLFLAG_RW,
636 &sctp_shutdown_guard_time_default, 0,
637 "Default shutdown guard timer in sec");
639 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, secret_lifetime, CTLFLAG_RW,
640 &sctp_secret_lifetime_default, 0,
641 "Default secret lifetime in sec");
643 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_max, CTLFLAG_RW,
644 &sctp_rto_max_default, 0,
645 "Default maximum retransmission timeout in msec");
647 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_min, CTLFLAG_RW,
648 &sctp_rto_min_default, 0,
649 "Default minimum retransmission timeout in msec");
651 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, rto_initial, CTLFLAG_RW,
652 &sctp_rto_initial_default, 0,
653 "Default initial retransmission timeout in msec");
655 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, init_rto_max, CTLFLAG_RW,
656 &sctp_init_rto_max_default, 0,
657 "Default maximum retransmission timeout during association setup in msec");
659 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, valid_cookie_life, CTLFLAG_RW,
660 &sctp_valid_cookie_life_default, 0,
661 "Default cookie lifetime in sec");
663 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, init_rtx_max, CTLFLAG_RW,
664 &sctp_init_rtx_max_default, 0,
665 "Default maximum number of retransmission for INIT chunks");
667 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, assoc_rtx_max, CTLFLAG_RW,
668 &sctp_assoc_rtx_max_default, 0,
669 "Default maximum number of retransmissions per association");
671 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, path_rtx_max, CTLFLAG_RW,
672 &sctp_path_rtx_max_default, 0,
673 "Default maximum of retransmissions per path");
675 SYSCTL_UINT(_net_inet_sctp, OID_AUTO, nr_outgoing_streams, CTLFLAG_RW,
676 &sctp_nr_outgoing_streams_default, 0,
677 "Default number of outgoing streams");
679 #ifdef SCTP_DEBUG
680 SYSCTL_INT(_net_inet_sctp, OID_AUTO, debug, CTLFLAG_RW,
681 &sctp_debug_on, 0, "Configure debug output");
682 #endif /* SCTP_DEBUG */
683 #endif
686 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
687 * will sofree() it when we return.
689 static void
690 sctp_abort(netmsg_t msg)
692 struct socket *so = msg->abort.base.nm_so;
693 struct sctp_inpcb *inp;
694 int error;
696 inp = (struct sctp_inpcb *)so->so_pcb;
697 if (inp) {
698 sctp_inpcb_free(inp, 1);
699 error = 0;
700 } else {
701 error = EINVAL;
703 lwkt_replymsg(&msg->lmsg, error);
706 static void
707 sctp_attach(netmsg_t msg)
709 struct socket *so = msg->attach.base.nm_so;
710 struct sctp_inpcb *inp;
711 struct inpcb *ip_inp;
712 int error;
714 inp = (struct sctp_inpcb *)so->so_pcb;
715 if (inp) {
716 error = EINVAL;
717 goto out;
719 error = soreserve(so, sctp_sendspace, sctp_recvspace, NULL);
720 if (error)
721 goto out;
722 error = sctp_inpcb_alloc(so);
723 if (error)
724 goto out;
725 inp = (struct sctp_inpcb *)so->so_pcb;
726 SCTP_INP_WLOCK(inp);
728 inp->sctp_flags &= ~SCTP_PCB_FLAGS_BOUND_V6; /* I'm not v6! */
729 ip_inp = &inp->ip_inp.inp;
730 ip_inp->inp_vflag |= INP_IPV4;
731 ip_inp->inp_ip_ttl = ip_defttl;
733 #ifdef IPSEC
734 #if !(defined(__OpenBSD__) || defined(__APPLE__))
735 error = ipsec_init_policy(so, &ip_inp->inp_sp);
736 if (error != 0) {
737 sctp_inpcb_free(inp, 1);
738 goto out;
740 #endif
741 #endif /*IPSEC*/
742 SCTP_INP_WUNLOCK(inp);
743 #if defined(__NetBSD__)
744 so->so_send = sctp_sosend;
745 #endif
746 error = 0;
747 out:
748 lwkt_replymsg(&msg->lmsg, error);
751 static void
752 sctp_bind(netmsg_t msg)
754 struct socket *so = msg->bind.base.nm_so;
755 struct sockaddr *addr = msg->bind.nm_nam;
756 thread_t td = msg->bind.nm_td;
757 struct sctp_inpcb *inp;
758 int error;
760 #ifdef INET6
761 if (addr && addr->sa_family != AF_INET) {
762 /* must be a v4 address! */
763 error= EINVAL;
764 goto out;
766 #endif /* INET6 */
768 inp = (struct sctp_inpcb *)so->so_pcb;
769 if (inp) {
770 error = sctp_inpcb_bind(so, addr, td);
771 } else {
772 error = EINVAL;
774 out:
775 lwkt_replymsg(&msg->lmsg, error);
779 static void
780 sctp_detach(netmsg_t msg)
782 struct socket *so = msg->detach.base.nm_so;
783 struct sctp_inpcb *inp;
784 int error;
786 inp = (struct sctp_inpcb *)so->so_pcb;
787 if (inp == NULL) {
788 error = EINVAL;
789 goto out;
791 if (((so->so_options & SO_LINGER) && (so->so_linger == 0)) ||
792 (so->so_rcv.ssb_cc > 0)) {
793 sctp_inpcb_free(inp, 1);
794 } else {
795 sctp_inpcb_free(inp, 0);
797 error = 0;
798 out:
799 lwkt_replymsg(&msg->lmsg, error);
802 void
803 sctp_send(netmsg_t msg)
805 struct socket *so = msg->send.base.nm_so;
806 int flags = msg->send.nm_flags;
807 struct mbuf *m = msg->send.nm_m;
808 struct mbuf *control = msg->send.nm_control;
809 struct sockaddr *addr = msg->send.nm_addr;
810 struct thread *td = msg->send.nm_td;
811 int error;
812 struct sctp_inpcb *inp;
813 inp = (struct sctp_inpcb *)so->so_pcb;
814 if (inp == NULL) {
815 if (control) {
816 sctp_m_freem(control);
817 control = NULL;
819 sctp_m_freem(m);
820 error = EINVAL;
821 goto out;
823 /* Got to have an to address if we are NOT a connected socket */
824 if ((addr == NULL) &&
825 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
826 (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE))
828 goto connected_type;
829 } else if (addr == NULL) {
830 error = EDESTADDRREQ;
831 sctp_m_freem(m);
832 if (control) {
833 sctp_m_freem(control);
834 control = NULL;
836 goto out;
838 #ifdef INET6
839 if (addr->sa_family != AF_INET) {
840 /* must be a v4 address! */
841 sctp_m_freem(m);
842 if (control) {
843 sctp_m_freem(control);
844 control = NULL;
846 error = EDESTADDRREQ; /* XXX huh? */
847 error = EINVAL;
848 goto out;
850 #endif /* INET6 */
851 connected_type:
852 /* now what about control */
853 if (control) {
854 if (inp->control) {
855 kprintf("huh? control set?\n");
856 sctp_m_freem(inp->control);
857 inp->control = NULL;
859 inp->control = control;
861 /* add it in possibly */
862 if ((inp->pkt) && (inp->pkt->m_flags & M_PKTHDR)) {
863 struct mbuf *x;
864 int c_len;
866 c_len = 0;
867 /* How big is it */
868 for (x=m;x;x = x->m_next) {
869 c_len += x->m_len;
871 inp->pkt->m_pkthdr.len += c_len;
873 /* Place the data */
874 if (inp->pkt) {
875 inp->pkt_last->m_next = m;
876 inp->pkt_last = m;
877 } else {
878 inp->pkt_last = inp->pkt = m;
880 if (
881 #if defined (__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
882 /* FreeBSD uses a flag passed */
883 ((flags & PRUS_MORETOCOME) == 0)
884 #elif defined( __NetBSD__)
885 /* NetBSD uses the so_state field */
886 ((so->so_state & SS_MORETOCOME) == 0)
887 #else
888 1 /* Open BSD does not have any "more to come" indication */
889 #endif
892 * note with the current version this code will only be used
893 * by OpenBSD-- NetBSD, FreeBSD, and MacOS have methods for
894 * re-defining sosend to use the sctp_sosend. One can
895 * optionally switch back to this code (by changing back the
896 * definitions) but this is not advisable.
898 error = sctp_output(inp, inp->pkt, addr,
899 inp->control, td, flags);
900 inp->pkt = NULL;
901 inp->control = NULL;
902 } else {
903 error = 0;
905 out:
906 if (msg->send.nm_flags & PRUS_NAMALLOC) {
907 kfree(msg->send.nm_addr, M_LWKTMSG);
908 msg->send.nm_addr = NULL;
910 lwkt_replymsg(&msg->lmsg, error);
913 static void
914 sctp_disconnect(netmsg_t msg)
916 struct socket *so = msg->disconnect.base.nm_so;
917 struct sctp_inpcb *inp;
918 int error;
920 inp = (struct sctp_inpcb *)so->so_pcb;
921 if (inp == NULL) {
922 error = ENOTCONN;
923 goto out;
925 SCTP_INP_RLOCK(inp);
926 if (inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
927 if (LIST_EMPTY(&inp->sctp_asoc_list)) {
928 /* No connection */
929 SCTP_INP_RUNLOCK(inp);
930 error = 0;
931 goto out;
932 } else {
933 int some_on_streamwheel = 0;
934 struct sctp_association *asoc;
935 struct sctp_tcb *stcb;
937 stcb = LIST_FIRST(&inp->sctp_asoc_list);
938 if (stcb == NULL) {
939 SCTP_INP_RUNLOCK(inp);
940 error = EINVAL;
941 goto out;
943 asoc = &stcb->asoc;
944 SCTP_TCB_LOCK(stcb);
945 if (((so->so_options & SO_LINGER) &&
946 (so->so_linger == 0)) ||
947 (so->so_rcv.ssb_cc > 0)) {
948 if (SCTP_GET_STATE(asoc) !=
949 SCTP_STATE_COOKIE_WAIT) {
950 /* Left with Data unread */
951 struct mbuf *err;
952 err = NULL;
953 MGET(err, MB_DONTWAIT, MT_DATA);
954 if (err) {
955 /* Fill in the user initiated abort */
956 struct sctp_paramhdr *ph;
957 ph = mtod(err, struct sctp_paramhdr *);
958 err->m_len = sizeof(struct sctp_paramhdr);
959 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
960 ph->param_length = htons(err->m_len);
962 sctp_send_abort_tcb(stcb, err);
964 SCTP_INP_RUNLOCK(inp);
965 sctp_free_assoc(inp, stcb);
966 /* No unlock tcb assoc is gone */
967 error = 0;
968 goto out;
970 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
971 /* Check to see if some data queued */
972 struct sctp_stream_out *outs;
973 TAILQ_FOREACH(outs, &asoc->out_wheel,
974 next_spoke) {
975 if (!TAILQ_EMPTY(&outs->outqueue)) {
976 some_on_streamwheel = 1;
977 break;
982 if (TAILQ_EMPTY(&asoc->send_queue) &&
983 TAILQ_EMPTY(&asoc->sent_queue) &&
984 (some_on_streamwheel == 0)) {
985 /* there is nothing queued to send, so done */
986 if ((SCTP_GET_STATE(asoc) !=
987 SCTP_STATE_SHUTDOWN_SENT) &&
988 (SCTP_GET_STATE(asoc) !=
989 SCTP_STATE_SHUTDOWN_ACK_SENT)) {
990 /* only send SHUTDOWN 1st time thru */
991 #ifdef SCTP_DEBUG
992 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
993 kprintf("%s:%d sends a shutdown\n",
994 __FILE__,
995 __LINE__
998 #endif
999 sctp_send_shutdown(stcb,
1000 stcb->asoc.primary_destination);
1001 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1002 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1003 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1004 stcb->sctp_ep, stcb,
1005 asoc->primary_destination);
1006 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1007 stcb->sctp_ep, stcb,
1008 asoc->primary_destination);
1010 } else {
1012 * we still got (or just got) data to send,
1013 * so set SHUTDOWN_PENDING
1016 * XXX sockets draft says that MSG_EOF should
1017 * be sent with no data.
1018 * currently, we will allow user data to be
1019 * sent first and move to SHUTDOWN-PENDING
1021 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1023 SCTP_TCB_UNLOCK(stcb);
1024 SCTP_INP_RUNLOCK(inp);
1025 error = 0;
1027 } else {
1028 /* UDP model does not support this */
1029 SCTP_INP_RUNLOCK(inp);
1030 error = EOPNOTSUPP;
1032 out:
1033 lwkt_replymsg(&msg->lmsg, error);
1036 /* also called from ipv6 sctp code */
1037 void
1038 sctp_shutdown(netmsg_t msg)
1040 struct socket *so = msg->shutdown.base.nm_so;
1041 struct sctp_inpcb *inp;
1042 int error;
1044 inp = (struct sctp_inpcb *)so->so_pcb;
1045 if (inp == NULL) {
1046 error = EINVAL;
1047 goto out;
1049 SCTP_INP_RLOCK(inp);
1050 /* For UDP model this is a invalid call */
1051 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
1052 /* Restore the flags that the soshutdown took away. */
1053 #if defined(__FreeBSD__) && __FreeBSD_version >= 502115
1054 so->so_rcv.sb_state &= ~SBS_CANTRCVMORE;
1055 #else
1056 soclrstate(so, SS_CANTRCVMORE);
1057 #endif
1058 /* This proc will wakeup for read and do nothing (I hope) */
1059 SCTP_INP_RUNLOCK(inp);
1060 error = EOPNOTSUPP;
1061 goto out;
1064 * Ok if we reach here its the TCP model and it is either a SHUT_WR
1065 * or SHUT_RDWR. This means we put the shutdown flag against it.
1068 int some_on_streamwheel = 0;
1069 struct sctp_tcb *stcb;
1070 struct sctp_association *asoc;
1071 socantsendmore(so);
1073 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1074 if (stcb == NULL) {
1076 * Ok we hit the case that the shutdown call was made
1077 * after an abort or something. Nothing to do now.
1079 error = 0;
1080 goto out;
1082 SCTP_TCB_LOCK(stcb);
1083 asoc = &stcb->asoc;
1085 if (!TAILQ_EMPTY(&asoc->out_wheel)) {
1086 /* Check to see if some data queued */
1087 struct sctp_stream_out *outs;
1088 TAILQ_FOREACH(outs, &asoc->out_wheel, next_spoke) {
1089 if (!TAILQ_EMPTY(&outs->outqueue)) {
1090 some_on_streamwheel = 1;
1091 break;
1095 if (TAILQ_EMPTY(&asoc->send_queue) &&
1096 TAILQ_EMPTY(&asoc->sent_queue) &&
1097 (some_on_streamwheel == 0)) {
1098 /* there is nothing queued to send, so I'm done... */
1099 if (SCTP_GET_STATE(asoc) != SCTP_STATE_SHUTDOWN_SENT) {
1100 /* only send SHUTDOWN the first time through */
1101 #ifdef SCTP_DEBUG
1102 if (sctp_debug_on & SCTP_DEBUG_OUTPUT4) {
1103 kprintf("%s:%d sends a shutdown\n",
1104 __FILE__,
1105 __LINE__
1108 #endif
1109 sctp_send_shutdown(stcb,
1110 stcb->asoc.primary_destination);
1111 sctp_chunk_output(stcb->sctp_ep, stcb, 1);
1112 asoc->state = SCTP_STATE_SHUTDOWN_SENT;
1113 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN,
1114 stcb->sctp_ep, stcb,
1115 asoc->primary_destination);
1116 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD,
1117 stcb->sctp_ep, stcb,
1118 asoc->primary_destination);
1120 } else {
1122 * we still got (or just got) data to send, so
1123 * set SHUTDOWN_PENDING
1125 asoc->state |= SCTP_STATE_SHUTDOWN_PENDING;
1127 SCTP_TCB_UNLOCK(stcb);
1129 SCTP_INP_RUNLOCK(inp);
1130 error = 0;
1131 out:
1132 lwkt_replymsg(&msg->lmsg, error);
1136 * copies a "user" presentable address and removes embedded scope, etc.
1137 * returns 0 on success, 1 on error
1139 static uint32_t
1140 sctp_fill_user_address(struct sockaddr_storage *ss, struct sockaddr *sa)
1142 struct sockaddr_in6 lsa6;
1143 sa = (struct sockaddr *)sctp_recover_scope((struct sockaddr_in6 *)sa,
1144 &lsa6);
1145 memcpy(ss, sa, sa->sa_len);
1146 return (0);
1150 #if defined(__NetBSD__) || defined(__OpenBSD__)
1152 * On NetBSD and OpenBSD in6_sin_2_v4mapsin6() not used and not exported,
1153 * so we have to export it here.
1155 void in6_sin_2_v4mapsin6(struct sockaddr_in *sin, struct sockaddr_in6 *sin6);
1156 #endif
1158 static int
1159 sctp_fill_up_addresses(struct sctp_inpcb *inp,
1160 struct sctp_tcb *stcb,
1161 int limit,
1162 struct sockaddr_storage *sas)
1164 struct ifnet *ifn;
1165 int loopback_scope, ipv4_local_scope, local_scope, site_scope, actual;
1166 int ipv4_addr_legal, ipv6_addr_legal;
1167 actual = 0;
1168 if (limit <= 0)
1169 return (actual);
1171 if (stcb) {
1172 /* Turn on all the appropriate scope */
1173 loopback_scope = stcb->asoc.loopback_scope;
1174 ipv4_local_scope = stcb->asoc.ipv4_local_scope;
1175 local_scope = stcb->asoc.local_scope;
1176 site_scope = stcb->asoc.site_scope;
1177 } else {
1178 /* Turn on ALL scope, since we look at the EP */
1179 loopback_scope = ipv4_local_scope = local_scope =
1180 site_scope = 1;
1182 ipv4_addr_legal = ipv6_addr_legal = 0;
1183 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1184 ipv6_addr_legal = 1;
1185 if (
1186 #if defined(__OpenBSD__)
1187 (0) /* we always do dual bind */
1188 #elif defined (__NetBSD__)
1189 (((struct in6pcb *)inp)->in6p_flags & IN6P_IPV6_V6ONLY)
1190 #else
1191 (((struct in6pcb *)inp)->inp_flags & IN6P_IPV6_V6ONLY)
1192 #endif
1193 == 0) {
1194 ipv4_addr_legal = 1;
1196 } else {
1197 ipv4_addr_legal = 1;
1200 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1201 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1202 struct ifaddr_container *ifac;
1204 if ((loopback_scope == 0) &&
1205 (ifn->if_type == IFT_LOOP)) {
1206 /* Skip loopback if loopback_scope not set */
1207 continue;
1209 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid],
1210 ifa_link) {
1211 struct ifaddr *ifa = ifac->ifa;
1213 if (stcb) {
1215 * For the BOUND-ALL case, the list
1216 * associated with a TCB is Always
1217 * considered a reverse list.. i.e.
1218 * it lists addresses that are NOT
1219 * part of the association. If this
1220 * is one of those we must skip it.
1222 if (sctp_is_addr_restricted(stcb,
1223 ifa->ifa_addr)) {
1224 continue;
1227 if ((ifa->ifa_addr->sa_family == AF_INET) &&
1228 (ipv4_addr_legal)) {
1229 struct sockaddr_in *sin;
1230 sin = (struct sockaddr_in *)ifa->ifa_addr;
1231 if (sin->sin_addr.s_addr == 0) {
1232 /* we skip unspecifed addresses */
1233 continue;
1235 if ((ipv4_local_scope == 0) &&
1236 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1237 continue;
1239 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) {
1240 in6_sin_2_v4mapsin6(sin, (struct sockaddr_in6 *)sas);
1241 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1242 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(struct sockaddr_in6));
1243 actual += sizeof(struct sockaddr_in6);
1244 } else {
1245 memcpy(sas, sin, sizeof(*sin));
1246 ((struct sockaddr_in *)sas)->sin_port = inp->sctp_lport;
1247 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin));
1248 actual += sizeof(*sin);
1250 if (actual >= limit) {
1251 return (actual);
1253 } else if ((ifa->ifa_addr->sa_family == AF_INET6) &&
1254 (ipv6_addr_legal)) {
1255 struct sockaddr_in6 *sin6, lsa6;
1256 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
1257 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1258 /* we skip unspecifed addresses */
1259 continue;
1261 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
1262 if (local_scope == 0)
1263 continue;
1264 if (sin6->sin6_scope_id == 0) {
1265 lsa6 = *sin6;
1266 if (in6_recoverscope(&lsa6,
1267 &lsa6.sin6_addr,
1268 NULL))
1269 /* bad link local address */
1270 continue;
1271 sin6 = &lsa6;
1274 if ((site_scope == 0) &&
1275 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1276 continue;
1278 memcpy(sas, sin6, sizeof(*sin6));
1279 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1280 sas = (struct sockaddr_storage *)((caddr_t)sas + sizeof(*sin6));
1281 actual += sizeof(*sin6);
1282 if (actual >= limit) {
1283 return (actual);
1288 } else {
1289 struct sctp_laddr *laddr;
1291 * If we have a TCB and we do NOT support ASCONF (it's
1292 * turned off or otherwise) then the list is always the
1293 * true list of addresses (the else case below). Otherwise
1294 * the list on the association is a list of addresses that
1295 * are NOT part of the association.
1297 if (inp->sctp_flags & SCTP_PCB_FLAGS_DO_ASCONF) {
1298 /* The list is a NEGATIVE list */
1299 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1300 if (stcb) {
1301 if (sctp_is_addr_restricted(stcb, laddr->ifa->ifa_addr)) {
1302 continue;
1305 if (sctp_fill_user_address(sas, laddr->ifa->ifa_addr))
1306 continue;
1308 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1309 sas = (struct sockaddr_storage *)((caddr_t)sas +
1310 laddr->ifa->ifa_addr->sa_len);
1311 actual += laddr->ifa->ifa_addr->sa_len;
1312 if (actual >= limit) {
1313 return (actual);
1316 } else {
1317 /* The list is a positive list if present */
1318 if (stcb) {
1319 /* Must use the specific association list */
1320 LIST_FOREACH(laddr, &stcb->asoc.sctp_local_addr_list,
1321 sctp_nxt_addr) {
1322 if (sctp_fill_user_address(sas,
1323 laddr->ifa->ifa_addr))
1324 continue;
1325 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1326 sas = (struct sockaddr_storage *)((caddr_t)sas +
1327 laddr->ifa->ifa_addr->sa_len);
1328 actual += laddr->ifa->ifa_addr->sa_len;
1329 if (actual >= limit) {
1330 return (actual);
1333 } else {
1334 /* No endpoint so use the endpoints individual list */
1335 LIST_FOREACH(laddr, &inp->sctp_addr_list,
1336 sctp_nxt_addr) {
1337 if (sctp_fill_user_address(sas,
1338 laddr->ifa->ifa_addr))
1339 continue;
1340 ((struct sockaddr_in6 *)sas)->sin6_port = inp->sctp_lport;
1341 sas = (struct sockaddr_storage *)((caddr_t)sas +
1342 laddr->ifa->ifa_addr->sa_len);
1343 actual += laddr->ifa->ifa_addr->sa_len;
1344 if (actual >= limit) {
1345 return (actual);
1351 return (actual);
1354 static int
1355 sctp_count_max_addresses(struct sctp_inpcb *inp)
1357 int cnt = 0;
1359 * In both sub-set bound an bound_all cases we return the MAXIMUM
1360 * number of addresses that you COULD get. In reality the sub-set
1361 * bound may have an exclusion list for a given TCB OR in the
1362 * bound-all case a TCB may NOT include the loopback or other
1363 * addresses as well.
1365 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
1366 struct ifnet *ifn;
1368 TAILQ_FOREACH(ifn, &ifnet, if_list) {
1369 struct ifaddr_container *ifac;
1371 TAILQ_FOREACH(ifac, &ifn->if_addrheads[mycpuid], ifa_link) {
1372 struct ifaddr *ifa = ifac->ifa;
1374 /* Count them if they are the right type */
1375 if (ifa->ifa_addr->sa_family == AF_INET) {
1376 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1377 cnt += sizeof(struct sockaddr_in6);
1378 else
1379 cnt += sizeof(struct sockaddr_in);
1381 } else if (ifa->ifa_addr->sa_family == AF_INET6)
1382 cnt += sizeof(struct sockaddr_in6);
1385 } else {
1386 struct sctp_laddr *laddr;
1387 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
1388 if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
1389 if (inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)
1390 cnt += sizeof(struct sockaddr_in6);
1391 else
1392 cnt += sizeof(struct sockaddr_in);
1394 } else if (laddr->ifa->ifa_addr->sa_family == AF_INET6)
1395 cnt += sizeof(struct sockaddr_in6);
1398 return (cnt);
1401 static int
1402 sctp_do_connect_x(struct socket *so,
1403 struct sctp_inpcb *inp,
1404 struct mbuf *m,
1405 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1406 struct thread *p,
1407 #else
1408 struct proc *p,
1409 #endif
1410 int delay
1413 int error = 0;
1414 struct sctp_tcb *stcb = NULL;
1415 struct sockaddr *sa;
1416 int num_v6=0, num_v4=0, *totaddrp, totaddr, i, incr, at;
1417 #ifdef SCTP_DEBUG
1418 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
1419 kprintf("Connectx called\n");
1421 #endif /* SCTP_DEBUG */
1423 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1424 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
1425 /* We are already connected AND the TCP model */
1426 return (EADDRINUSE);
1428 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
1429 SCTP_INP_RLOCK(inp);
1430 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1431 SCTP_INP_RUNLOCK(inp);
1433 if (stcb) {
1434 return (EALREADY);
1437 SCTP_ASOC_CREATE_LOCK(inp);
1438 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1439 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
1440 SCTP_ASOC_CREATE_UNLOCK(inp);
1441 return (EFAULT);
1444 totaddrp = mtod(m, int *);
1445 totaddr = *totaddrp;
1446 sa = (struct sockaddr *)(totaddrp + 1);
1447 at = incr = 0;
1448 /* account and validate addresses */
1449 SCTP_INP_WLOCK(inp);
1450 SCTP_INP_INCR_REF(inp);
1451 SCTP_INP_WUNLOCK(inp);
1452 for (i = 0; i < totaddr; i++) {
1453 if (sa->sa_family == AF_INET) {
1454 num_v4++;
1455 incr = sizeof(struct sockaddr_in);
1456 } else if (sa->sa_family == AF_INET6) {
1457 struct sockaddr_in6 *sin6;
1458 sin6 = (struct sockaddr_in6 *)sa;
1459 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1460 /* Must be non-mapped for connectx */
1461 SCTP_ASOC_CREATE_UNLOCK(inp);
1462 return EINVAL;
1464 num_v6++;
1465 incr = sizeof(struct sockaddr_in6);
1466 } else {
1467 totaddr = i;
1468 break;
1470 stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
1471 if (stcb != NULL) {
1472 /* Already have or am bring up an association */
1473 SCTP_ASOC_CREATE_UNLOCK(inp);
1474 SCTP_TCB_UNLOCK(stcb);
1475 return (EALREADY);
1477 if ((at + incr) > m->m_len) {
1478 totaddr = i;
1479 break;
1481 sa = (struct sockaddr *)((caddr_t)sa + incr);
1483 sa = (struct sockaddr *)(totaddrp + 1);
1484 SCTP_INP_WLOCK(inp);
1485 SCTP_INP_DECR_REF(inp);
1486 SCTP_INP_WUNLOCK(inp);
1487 #ifdef INET6
1488 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
1489 (num_v6 > 0)) {
1490 SCTP_INP_WUNLOCK(inp);
1491 SCTP_ASOC_CREATE_UNLOCK(inp);
1492 return (EINVAL);
1494 if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
1495 (num_v4 > 0)) {
1496 struct in6pcb *inp6;
1497 inp6 = (struct in6pcb *)inp;
1498 if (
1499 #if defined(__OpenBSD__)
1500 (0) /* we always do dual bind */
1501 #elif defined (__NetBSD__)
1502 (inp6->in6p_flags & IN6P_IPV6_V6ONLY)
1503 #else
1504 (inp6->inp_flags & IN6P_IPV6_V6ONLY)
1505 #endif
1508 * if IPV6_V6ONLY flag, ignore connections
1509 * destined to a v4 addr or v4-mapped addr
1511 SCTP_INP_WUNLOCK(inp);
1512 SCTP_ASOC_CREATE_UNLOCK(inp);
1513 return EINVAL;
1516 #endif /* INET6 */
1517 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
1518 SCTP_PCB_FLAGS_UNBOUND) {
1519 /* Bind a ephemeral port */
1520 SCTP_INP_WUNLOCK(inp);
1521 error = sctp_inpcb_bind(so, NULL, p);
1522 if (error) {
1523 SCTP_ASOC_CREATE_UNLOCK(inp);
1524 return (error);
1526 } else {
1527 SCTP_INP_WUNLOCK(inp);
1529 /* We are GOOD to go */
1530 stcb = sctp_aloc_assoc(inp, sa, 1, &error, 0);
1531 if (stcb == NULL) {
1532 /* Gak! no memory */
1533 SCTP_ASOC_CREATE_UNLOCK(inp);
1534 return (error);
1536 /* move to second address */
1537 if (sa->sa_family == AF_INET)
1538 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
1539 else
1540 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
1542 for (i = 1; i < totaddr; i++) {
1543 if (sa->sa_family == AF_INET) {
1544 incr = sizeof(struct sockaddr_in);
1545 if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1546 /* assoc gone no un-lock */
1547 sctp_free_assoc(inp, stcb);
1548 SCTP_ASOC_CREATE_UNLOCK(inp);
1549 return (ENOBUFS);
1552 } else if (sa->sa_family == AF_INET6) {
1553 incr = sizeof(struct sockaddr_in6);
1554 if (sctp_add_remote_addr(stcb, sa, 0, 8)) {
1555 /* assoc gone no un-lock */
1556 sctp_free_assoc(inp, stcb);
1557 SCTP_ASOC_CREATE_UNLOCK(inp);
1558 return (ENOBUFS);
1561 sa = (struct sockaddr *)((caddr_t)sa + incr);
1563 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
1564 if (delay) {
1565 /* doing delayed connection */
1566 stcb->asoc.delayed_connection = 1;
1567 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
1568 } else {
1569 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
1570 sctp_send_initiate(inp, stcb);
1572 SCTP_TCB_UNLOCK(stcb);
1573 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
1574 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
1575 /* Set the connected flag so we can queue data */
1576 soisconnecting(so);
1578 SCTP_ASOC_CREATE_UNLOCK(inp);
1579 return error;
1583 static int
1584 sctp_optsget(struct socket *so,
1585 int opt,
1586 struct mbuf **mp,
1587 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
1588 struct thread *p
1589 #else
1590 struct proc *p
1591 #endif
1594 struct sctp_inpcb *inp;
1595 struct mbuf *m;
1596 int error, optval=0;
1597 struct sctp_tcb *stcb = NULL;
1599 inp = (struct sctp_inpcb *)so->so_pcb;
1600 if (inp == 0)
1601 return EINVAL;
1602 error = 0;
1604 if (mp == NULL) {
1605 #ifdef SCTP_DEBUG
1606 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1607 kprintf("optsget:MP is NULL EINVAL\n");
1609 #endif /* SCTP_DEBUG */
1610 return (EINVAL);
1612 m = *mp;
1613 if (m == NULL) {
1614 /* Got to have a mbuf */
1615 #ifdef SCTP_DEBUG
1616 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1617 kprintf("Huh no mbuf\n");
1619 #endif /* SCTP_DEBUG */
1620 return (EINVAL);
1622 #ifdef SCTP_DEBUG
1623 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1624 kprintf("optsget opt:%lxx sz:%u\n", (unsigned long)opt,
1625 m->m_len);
1627 #endif /* SCTP_DEBUG */
1629 switch (opt) {
1630 case SCTP_NODELAY:
1631 case SCTP_AUTOCLOSE:
1632 case SCTP_AUTO_ASCONF:
1633 case SCTP_DISABLE_FRAGMENTS:
1634 case SCTP_I_WANT_MAPPED_V4_ADDR:
1635 #ifdef SCTP_DEBUG
1636 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1637 kprintf("other stuff\n");
1639 #endif /* SCTP_DEBUG */
1640 SCTP_INP_RLOCK(inp);
1641 switch (opt) {
1642 case SCTP_DISABLE_FRAGMENTS:
1643 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NO_FRAGMENT;
1644 break;
1645 case SCTP_I_WANT_MAPPED_V4_ADDR:
1646 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
1647 break;
1648 case SCTP_AUTO_ASCONF:
1649 optval = inp->sctp_flags & SCTP_PCB_FLAGS_AUTO_ASCONF;
1650 break;
1651 case SCTP_NODELAY:
1652 optval = inp->sctp_flags & SCTP_PCB_FLAGS_NODELAY;
1653 break;
1654 case SCTP_AUTOCLOSE:
1655 if ((inp->sctp_flags & SCTP_PCB_FLAGS_AUTOCLOSE) ==
1656 SCTP_PCB_FLAGS_AUTOCLOSE)
1657 optval = inp->sctp_ep.auto_close_time;
1658 else
1659 optval = 0;
1660 break;
1662 default:
1663 error = ENOPROTOOPT;
1664 } /* end switch (sopt->sopt_name) */
1665 if (opt != SCTP_AUTOCLOSE) {
1666 /* make it an "on/off" value */
1667 optval = (optval != 0);
1669 if ((size_t)m->m_len < sizeof(int)) {
1670 error = EINVAL;
1672 SCTP_INP_RUNLOCK(inp);
1673 if (error == 0) {
1674 /* return the option value */
1675 *mtod(m, int *) = optval;
1676 m->m_len = sizeof(optval);
1678 break;
1679 case SCTP_GET_ASOC_ID_LIST:
1681 struct sctp_assoc_ids *ids;
1682 int cnt, at;
1683 u_int16_t orig;
1685 if ((size_t)m->m_len < sizeof(struct sctp_assoc_ids)) {
1686 error = EINVAL;
1687 break;
1689 ids = mtod(m, struct sctp_assoc_ids *);
1690 cnt = 0;
1691 SCTP_INP_RLOCK(inp);
1692 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1693 if (stcb == NULL) {
1694 none_out_now:
1695 ids->asls_numb_present = 0;
1696 ids->asls_more_to_get = 0;
1697 SCTP_INP_RUNLOCK(inp);
1698 break;
1700 orig = ids->asls_assoc_start;
1701 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1702 while( orig ) {
1703 stcb = LIST_NEXT(stcb , sctp_tcblist);
1704 orig--;
1705 cnt--;
1707 if ( stcb == NULL)
1708 goto none_out_now;
1710 at = 0;
1711 ids->asls_numb_present = 0;
1712 ids->asls_more_to_get = 1;
1713 while(at < MAX_ASOC_IDS_RET) {
1714 ids->asls_assoc_id[at] = sctp_get_associd(stcb);
1715 at++;
1716 ids->asls_numb_present++;
1717 stcb = LIST_NEXT(stcb , sctp_tcblist);
1718 if (stcb == NULL) {
1719 ids->asls_more_to_get = 0;
1720 break;
1723 SCTP_INP_RUNLOCK(inp);
1725 break;
1726 case SCTP_GET_NONCE_VALUES:
1728 struct sctp_get_nonce_values *gnv;
1729 if ((size_t)m->m_len < sizeof(struct sctp_get_nonce_values)) {
1730 error = EINVAL;
1731 break;
1733 gnv = mtod(m, struct sctp_get_nonce_values *);
1734 stcb = sctp_findassociation_ep_asocid(inp, gnv->gn_assoc_id);
1735 if (stcb == NULL) {
1736 error = ENOTCONN;
1737 } else {
1738 gnv->gn_peers_tag = stcb->asoc.peer_vtag;
1739 gnv->gn_local_tag = stcb->asoc.my_vtag;
1740 SCTP_TCB_UNLOCK(stcb);
1744 break;
1745 case SCTP_PEER_PUBLIC_KEY:
1746 case SCTP_MY_PUBLIC_KEY:
1747 case SCTP_SET_AUTH_CHUNKS:
1748 case SCTP_SET_AUTH_SECRET:
1749 /* not supported yet and until we refine the draft */
1750 error = EOPNOTSUPP;
1751 break;
1753 case SCTP_DELAYED_ACK_TIME:
1755 int32_t *tm;
1756 if ((size_t)m->m_len < sizeof(int32_t)) {
1757 error = EINVAL;
1758 break;
1760 tm = mtod(m, int32_t *);
1762 *tm = TICKS_TO_MSEC(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1764 break;
1766 case SCTP_GET_SNDBUF_USE:
1767 if ((size_t)m->m_len < sizeof(struct sctp_sockstat)) {
1768 error = EINVAL;
1769 } else {
1770 struct sctp_sockstat *ss;
1771 struct sctp_tcb *stcb;
1772 struct sctp_association *asoc;
1773 ss = mtod(m, struct sctp_sockstat *);
1774 stcb = sctp_findassociation_ep_asocid(inp, ss->ss_assoc_id);
1775 if (stcb == NULL) {
1776 error = ENOTCONN;
1777 } else {
1778 asoc = &stcb->asoc;
1779 ss->ss_total_sndbuf = (u_int32_t)asoc->total_output_queue_size;
1780 ss->ss_total_mbuf_sndbuf = (u_int32_t)asoc->total_output_mbuf_queue_size;
1781 ss->ss_total_recv_buf = (u_int32_t)(asoc->size_on_delivery_queue +
1782 asoc->size_on_reasm_queue +
1783 asoc->size_on_all_streams);
1784 SCTP_TCB_UNLOCK(stcb);
1785 error = 0;
1786 m->m_len = sizeof(struct sctp_sockstat);
1789 break;
1790 case SCTP_MAXBURST:
1792 u_int8_t *burst;
1793 burst = mtod(m, u_int8_t *);
1794 SCTP_INP_RLOCK(inp);
1795 *burst = inp->sctp_ep.max_burst;
1796 SCTP_INP_RUNLOCK(inp);
1797 m->m_len = sizeof(u_int8_t);
1799 break;
1800 case SCTP_MAXSEG:
1802 u_int32_t *segsize;
1803 sctp_assoc_t *assoc_id;
1804 int ovh;
1806 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1807 error = EINVAL;
1808 break;
1810 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
1811 error = EINVAL;
1812 break;
1814 assoc_id = mtod(m, sctp_assoc_t *);
1815 segsize = mtod(m, u_int32_t *);
1816 m->m_len = sizeof(u_int32_t);
1818 if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
1819 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) ||
1820 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
1821 struct sctp_tcb *stcb;
1822 SCTP_INP_RLOCK(inp);
1823 stcb = LIST_FIRST(&inp->sctp_asoc_list);
1824 if (stcb) {
1825 SCTP_TCB_LOCK(stcb);
1826 SCTP_INP_RUNLOCK(inp);
1827 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1828 SCTP_TCB_UNLOCK(stcb);
1829 } else {
1830 SCTP_INP_RUNLOCK(inp);
1831 goto skipit;
1833 } else {
1834 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
1835 if (stcb) {
1836 *segsize = sctp_get_frag_point(stcb, &stcb->asoc);
1837 SCTP_TCB_UNLOCK(stcb);
1838 break;
1840 skipit:
1841 /* default is to get the max, if I
1842 * can't calculate from an existing association.
1844 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1845 ovh = SCTP_MED_OVERHEAD;
1846 } else {
1847 ovh = SCTP_MED_V4_OVERHEAD;
1849 *segsize = inp->sctp_frag_point - ovh;
1852 break;
1854 case SCTP_SET_DEBUG_LEVEL:
1855 #ifdef SCTP_DEBUG
1857 u_int32_t *level;
1858 if ((size_t)m->m_len < sizeof(u_int32_t)) {
1859 error = EINVAL;
1860 break;
1862 level = mtod(m, u_int32_t *);
1863 error = 0;
1864 *level = sctp_debug_on;
1865 m->m_len = sizeof(u_int32_t);
1866 kprintf("Returning DEBUG LEVEL %x is set\n",
1867 (u_int)sctp_debug_on);
1869 #else /* SCTP_DEBUG */
1870 error = EOPNOTSUPP;
1871 #endif
1872 break;
1873 case SCTP_GET_STAT_LOG:
1874 #ifdef SCTP_STAT_LOGGING
1875 error = sctp_fill_stat_log(m);
1876 #else /* SCTP_DEBUG */
1877 error = EOPNOTSUPP;
1878 #endif
1879 break;
1880 case SCTP_GET_PEGS:
1882 u_int32_t *pt;
1883 if ((size_t)m->m_len < sizeof(sctp_pegs)) {
1884 error = EINVAL;
1885 break;
1887 pt = mtod(m, u_int32_t *);
1888 memcpy(pt, sctp_pegs, sizeof(sctp_pegs));
1889 m->m_len = sizeof(sctp_pegs);
1891 break;
1892 case SCTP_EVENTS:
1894 struct sctp_event_subscribe *events;
1895 #ifdef SCTP_DEBUG
1896 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1897 kprintf("get events\n");
1899 #endif /* SCTP_DEBUG */
1900 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
1901 #ifdef SCTP_DEBUG
1902 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
1903 kprintf("M->M_LEN is %d not %d\n",
1904 (int)m->m_len,
1905 (int)sizeof(struct sctp_event_subscribe));
1907 #endif /* SCTP_DEBUG */
1908 error = EINVAL;
1909 break;
1911 events = mtod(m, struct sctp_event_subscribe *);
1912 memset(events, 0, sizeof(*events));
1913 SCTP_INP_RLOCK(inp);
1914 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT)
1915 events->sctp_data_io_event = 1;
1917 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVASSOCEVNT)
1918 events->sctp_association_event = 1;
1920 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPADDREVNT)
1921 events->sctp_address_event = 1;
1923 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSENDFAILEVNT)
1924 events->sctp_send_failure_event = 1;
1926 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVPEERERR)
1927 events->sctp_peer_error_event = 1;
1929 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)
1930 events->sctp_shutdown_event = 1;
1932 if (inp->sctp_flags & SCTP_PCB_FLAGS_PDAPIEVNT)
1933 events->sctp_partial_delivery_event = 1;
1935 if (inp->sctp_flags & SCTP_PCB_FLAGS_ADAPTIONEVNT)
1936 events->sctp_adaption_layer_event = 1;
1938 if (inp->sctp_flags & SCTP_PCB_FLAGS_STREAM_RESETEVNT)
1939 events->sctp_stream_reset_events = 1;
1940 SCTP_INP_RUNLOCK(inp);
1941 m->m_len = sizeof(struct sctp_event_subscribe);
1944 break;
1946 case SCTP_ADAPTION_LAYER:
1947 if ((size_t)m->m_len < sizeof(int)) {
1948 error = EINVAL;
1949 break;
1951 #ifdef SCTP_DEBUG
1952 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1953 kprintf("getadaption ind\n");
1955 #endif /* SCTP_DEBUG */
1956 SCTP_INP_RLOCK(inp);
1957 *mtod(m, int *) = inp->sctp_ep.adaption_layer_indicator;
1958 SCTP_INP_RUNLOCK(inp);
1959 m->m_len = sizeof(int);
1960 break;
1961 case SCTP_SET_INITIAL_DBG_SEQ:
1962 if ((size_t)m->m_len < sizeof(int)) {
1963 error = EINVAL;
1964 break;
1966 #ifdef SCTP_DEBUG
1967 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1968 kprintf("get initial dbg seq\n");
1970 #endif /* SCTP_DEBUG */
1971 SCTP_INP_RLOCK(inp);
1972 *mtod(m, int *) = inp->sctp_ep.initial_sequence_debug;
1973 SCTP_INP_RUNLOCK(inp);
1974 m->m_len = sizeof(int);
1975 break;
1976 case SCTP_GET_LOCAL_ADDR_SIZE:
1977 if ((size_t)m->m_len < sizeof(int)) {
1978 error = EINVAL;
1979 break;
1981 #ifdef SCTP_DEBUG
1982 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1983 kprintf("get local sizes\n");
1985 #endif /* SCTP_DEBUG */
1986 SCTP_INP_RLOCK(inp);
1987 *mtod(m, int *) = sctp_count_max_addresses(inp);
1988 SCTP_INP_RUNLOCK(inp);
1989 m->m_len = sizeof(int);
1990 break;
1991 case SCTP_GET_REMOTE_ADDR_SIZE:
1993 sctp_assoc_t *assoc_id;
1994 u_int32_t *val, sz;
1995 struct sctp_nets *net;
1996 #ifdef SCTP_DEBUG
1997 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
1998 kprintf("get remote size\n");
2000 #endif /* SCTP_DEBUG */
2001 if ((size_t)m->m_len < sizeof(sctp_assoc_t)) {
2002 #ifdef SCTP_DEBUG
2003 kprintf("m->m_len:%d not %zd\n",
2004 m->m_len, sizeof(sctp_assoc_t));
2005 #endif /* SCTP_DEBUG */
2006 error = EINVAL;
2007 break;
2009 stcb = NULL;
2010 val = mtod(m, u_int32_t *);
2011 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2012 SCTP_INP_RLOCK(inp);
2013 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2014 if (stcb)
2015 SCTP_TCB_LOCK(stcb);
2016 SCTP_INP_RUNLOCK(inp);
2018 if (stcb == NULL) {
2019 assoc_id = mtod(m, sctp_assoc_t *);
2020 stcb = sctp_findassociation_ep_asocid(inp, *assoc_id);
2023 if (stcb == NULL) {
2024 error = EINVAL;
2025 break;
2027 *val = 0;
2028 sz = 0;
2029 /* Count the sizes */
2030 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2031 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2032 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2033 sz += sizeof(struct sockaddr_in6);
2034 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2035 sz += sizeof(struct sockaddr_in);
2036 } else {
2037 /* huh */
2038 break;
2041 SCTP_TCB_UNLOCK(stcb);
2042 *val = sz;
2043 m->m_len = sizeof(u_int32_t);
2045 break;
2046 case SCTP_GET_PEER_ADDRESSES:
2048 * Get the address information, an array
2049 * is passed in to fill up we pack it.
2052 int cpsz, left;
2053 struct sockaddr_storage *sas;
2054 struct sctp_nets *net;
2055 struct sctp_getaddresses *saddr;
2056 #ifdef SCTP_DEBUG
2057 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2058 kprintf("get peer addresses\n");
2060 #endif /* SCTP_DEBUG */
2061 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2062 error = EINVAL;
2063 break;
2065 left = m->m_len - sizeof(struct sctp_getaddresses);
2066 saddr = mtod(m, struct sctp_getaddresses *);
2067 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2068 SCTP_INP_RLOCK(inp);
2069 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2070 if (stcb)
2071 SCTP_TCB_LOCK(stcb);
2072 SCTP_INP_RUNLOCK(inp);
2073 } else
2074 stcb = sctp_findassociation_ep_asocid(inp,
2075 saddr->sget_assoc_id);
2076 if (stcb == NULL) {
2077 error = ENOENT;
2078 break;
2080 m->m_len = sizeof(struct sctp_getaddresses);
2081 sas = (struct sockaddr_storage *)&saddr->addr[0];
2083 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
2084 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) ||
2085 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET6)) {
2086 cpsz = sizeof(struct sockaddr_in6);
2087 } else if (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET) {
2088 cpsz = sizeof(struct sockaddr_in);
2089 } else {
2090 /* huh */
2091 break;
2093 if (left < cpsz) {
2094 /* not enough room. */
2095 #ifdef SCTP_DEBUG
2096 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2097 kprintf("Out of room\n");
2099 #endif /* SCTP_DEBUG */
2100 break;
2102 if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_NEEDS_MAPPED_V4) &&
2103 (((struct sockaddr *)&net->ro._l_addr)->sa_family == AF_INET)) {
2104 /* Must map the address */
2105 in6_sin_2_v4mapsin6((struct sockaddr_in *)&net->ro._l_addr,
2106 (struct sockaddr_in6 *)sas);
2107 } else {
2108 memcpy(sas, &net->ro._l_addr, cpsz);
2110 ((struct sockaddr_in *)sas)->sin_port = stcb->rport;
2112 sas = (struct sockaddr_storage *)((caddr_t)sas + cpsz);
2113 left -= cpsz;
2114 m->m_len += cpsz;
2115 #ifdef SCTP_DEBUG
2116 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2117 kprintf("left now:%d mlen:%d\n",
2118 left, m->m_len);
2120 #endif /* SCTP_DEBUG */
2122 SCTP_TCB_UNLOCK(stcb);
2124 #ifdef SCTP_DEBUG
2125 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2126 kprintf("All done\n");
2128 #endif /* SCTP_DEBUG */
2129 break;
2130 case SCTP_GET_LOCAL_ADDRESSES:
2132 int limit, actual;
2133 struct sockaddr_storage *sas;
2134 struct sctp_getaddresses *saddr;
2135 #ifdef SCTP_DEBUG
2136 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2137 kprintf("get local addresses\n");
2139 #endif /* SCTP_DEBUG */
2140 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
2141 error = EINVAL;
2142 break;
2144 saddr = mtod(m, struct sctp_getaddresses *);
2146 if (saddr->sget_assoc_id) {
2147 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2148 SCTP_INP_RLOCK(inp);
2149 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2150 if (stcb)
2151 SCTP_TCB_LOCK(stcb);
2152 SCTP_INP_RUNLOCK(inp);
2153 } else
2154 stcb = sctp_findassociation_ep_asocid(inp, saddr->sget_assoc_id);
2156 } else {
2157 stcb = NULL;
2160 * assure that the TCP model does not need a assoc id
2161 * once connected.
2163 if ( (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) &&
2164 (stcb == NULL) ) {
2165 SCTP_INP_RLOCK(inp);
2166 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2167 if (stcb)
2168 SCTP_TCB_LOCK(stcb);
2169 SCTP_INP_RUNLOCK(inp);
2171 sas = (struct sockaddr_storage *)&saddr->addr[0];
2172 limit = m->m_len - sizeof(sctp_assoc_t);
2173 actual = sctp_fill_up_addresses(inp, stcb, limit, sas);
2174 SCTP_TCB_UNLOCK(stcb);
2175 m->m_len = sizeof(struct sockaddr_storage) + actual;
2177 break;
2178 case SCTP_PEER_ADDR_PARAMS:
2180 struct sctp_paddrparams *paddrp;
2181 struct sctp_nets *net;
2183 #ifdef SCTP_DEBUG
2184 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2185 kprintf("Getting peer_addr_params\n");
2187 #endif /* SCTP_DEBUG */
2188 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
2189 #ifdef SCTP_DEBUG
2190 if (sctp_debug_on & SCTP_DEBUG_USRREQ2) {
2191 kprintf("Hmm m->m_len:%d is to small\n",
2192 m->m_len);
2194 #endif /* SCTP_DEBUG */
2195 error = EINVAL;
2196 break;
2198 paddrp = mtod(m, struct sctp_paddrparams *);
2200 net = NULL;
2201 if (paddrp->spp_assoc_id) {
2202 #ifdef SCTP_DEBUG
2203 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2204 kprintf("In spp_assoc_id find type\n");
2206 #endif /* SCTP_DEBUG */
2207 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2208 SCTP_INP_RLOCK(inp);
2209 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2210 if (stcb) {
2211 SCTP_TCB_LOCK(stcb);
2212 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2214 SCTP_INP_RLOCK(inp);
2215 } else {
2216 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
2218 if (stcb == NULL) {
2219 error = ENOENT;
2220 break;
2223 if ( (stcb == NULL) &&
2224 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
2225 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
2226 /* Lookup via address */
2227 #ifdef SCTP_DEBUG
2228 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2229 kprintf("Ok we need to lookup a param\n");
2231 #endif /* SCTP_DEBUG */
2232 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2233 SCTP_INP_RLOCK(inp);
2234 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2235 if (stcb) {
2236 SCTP_TCB_LOCK(stcb);
2237 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
2239 SCTP_INP_RUNLOCK(inp);
2240 } else {
2241 SCTP_INP_WLOCK(inp);
2242 SCTP_INP_INCR_REF(inp);
2243 SCTP_INP_WUNLOCK(inp);
2244 stcb = sctp_findassociation_ep_addr(&inp,
2245 (struct sockaddr *)&paddrp->spp_address,
2246 &net, NULL, NULL);
2247 if (stcb == NULL) {
2248 SCTP_INP_WLOCK(inp);
2249 SCTP_INP_DECR_REF(inp);
2250 SCTP_INP_WUNLOCK(inp);
2254 if (stcb == NULL) {
2255 error = ENOENT;
2256 break;
2258 } else {
2259 /* Effects the Endpoint */
2260 #ifdef SCTP_DEBUG
2261 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2262 kprintf("User wants EP level info\n");
2264 #endif /* SCTP_DEBUG */
2265 stcb = NULL;
2267 if (stcb) {
2268 /* Applys to the specific association */
2269 #ifdef SCTP_DEBUG
2270 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2271 kprintf("In TCB side\n");
2273 #endif /* SCTP_DEBUG */
2274 if (net) {
2275 paddrp->spp_pathmaxrxt = net->failure_threshold;
2276 } else {
2277 /* No destination so return default value */
2278 paddrp->spp_pathmaxrxt = stcb->asoc.def_net_failure;
2280 paddrp->spp_hbinterval = stcb->asoc.heart_beat_delay;
2281 paddrp->spp_assoc_id = sctp_get_associd(stcb);
2282 SCTP_TCB_UNLOCK(stcb);
2283 } else {
2284 /* Use endpoint defaults */
2285 SCTP_INP_RLOCK(inp);
2286 #ifdef SCTP_DEBUG
2287 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2288 kprintf("In EP level info\n");
2290 #endif /* SCTP_DEBUG */
2291 paddrp->spp_pathmaxrxt = inp->sctp_ep.def_net_failure;
2292 paddrp->spp_hbinterval = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT];
2293 paddrp->spp_assoc_id = (sctp_assoc_t)0;
2294 SCTP_INP_RUNLOCK(inp);
2296 m->m_len = sizeof(struct sctp_paddrparams);
2298 break;
2299 case SCTP_GET_PEER_ADDR_INFO:
2301 struct sctp_paddrinfo *paddri;
2302 struct sctp_nets *net;
2303 #ifdef SCTP_DEBUG
2304 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2305 kprintf("GetPEER ADDR_INFO\n");
2307 #endif /* SCTP_DEBUG */
2308 if ((size_t)m->m_len < sizeof(struct sctp_paddrinfo)) {
2309 error = EINVAL;
2310 break;
2312 paddri = mtod(m, struct sctp_paddrinfo *);
2313 net = NULL;
2314 if ((((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET) ||
2315 (((struct sockaddr *)&paddri->spinfo_address)->sa_family == AF_INET6)) {
2316 /* Lookup via address */
2317 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2318 SCTP_INP_RLOCK(inp);
2319 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2320 if (stcb) {
2321 SCTP_TCB_LOCK(stcb);
2322 net = sctp_findnet(stcb,
2323 (struct sockaddr *)&paddri->spinfo_address);
2325 SCTP_INP_RUNLOCK(inp);
2326 } else {
2327 SCTP_INP_WLOCK(inp);
2328 SCTP_INP_INCR_REF(inp);
2329 SCTP_INP_WUNLOCK(inp);
2330 stcb = sctp_findassociation_ep_addr(&inp,
2331 (struct sockaddr *)&paddri->spinfo_address,
2332 &net, NULL, NULL);
2333 if (stcb == NULL) {
2334 SCTP_INP_WLOCK(inp);
2335 SCTP_INP_DECR_REF(inp);
2336 SCTP_INP_WUNLOCK(inp);
2340 } else {
2341 stcb = NULL;
2343 if ((stcb == NULL) || (net == NULL)) {
2344 error = ENOENT;
2345 break;
2347 m->m_len = sizeof(struct sctp_paddrinfo);
2348 paddri->spinfo_state = net->dest_state & (SCTP_REACHABLE_MASK|SCTP_ADDR_NOHB);
2349 paddri->spinfo_cwnd = net->cwnd;
2350 paddri->spinfo_srtt = ((net->lastsa >> 2) + net->lastsv) >> 1;
2351 paddri->spinfo_rto = net->RTO;
2352 paddri->spinfo_assoc_id = sctp_get_associd(stcb);
2353 SCTP_TCB_UNLOCK(stcb);
2355 break;
2356 case SCTP_PCB_STATUS:
2358 struct sctp_pcbinfo *spcb;
2359 #ifdef SCTP_DEBUG
2360 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2361 kprintf("PCB status\n");
2363 #endif /* SCTP_DEBUG */
2364 if ((size_t)m->m_len < sizeof(struct sctp_pcbinfo)) {
2365 error = EINVAL;
2366 break;
2368 spcb = mtod(m, struct sctp_pcbinfo *);
2369 sctp_fill_pcbinfo(spcb);
2370 m->m_len = sizeof(struct sctp_pcbinfo);
2372 break;
2373 case SCTP_STATUS:
2375 struct sctp_nets *net;
2376 struct sctp_status *sstat;
2377 #ifdef SCTP_DEBUG
2378 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2379 kprintf("SCTP status\n");
2381 #endif /* SCTP_DEBUG */
2383 if ((size_t)m->m_len < sizeof(struct sctp_status)) {
2384 error = EINVAL;
2385 break;
2387 sstat = mtod(m, struct sctp_status *);
2389 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2390 SCTP_INP_RLOCK(inp);
2391 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2392 if (stcb)
2393 SCTP_TCB_LOCK(stcb);
2394 SCTP_INP_RUNLOCK(inp);
2395 } else
2396 stcb = sctp_findassociation_ep_asocid(inp, sstat->sstat_assoc_id);
2398 if (stcb == NULL) {
2399 error = EINVAL;
2400 break;
2403 * I think passing the state is fine since
2404 * sctp_constants.h will be available to the user
2405 * land.
2407 sstat->sstat_state = stcb->asoc.state;
2408 sstat->sstat_rwnd = stcb->asoc.peers_rwnd;
2409 sstat->sstat_unackdata = stcb->asoc.sent_queue_cnt;
2411 * We can't include chunks that have been passed
2412 * to the socket layer. Only things in queue.
2414 sstat->sstat_penddata = (stcb->asoc.cnt_on_delivery_queue +
2415 stcb->asoc.cnt_on_reasm_queue +
2416 stcb->asoc.cnt_on_all_streams);
2419 sstat->sstat_instrms = stcb->asoc.streamincnt;
2420 sstat->sstat_outstrms = stcb->asoc.streamoutcnt;
2421 sstat->sstat_fragmentation_point = sctp_get_frag_point(stcb, &stcb->asoc);
2422 memcpy(&sstat->sstat_primary.spinfo_address,
2423 &stcb->asoc.primary_destination->ro._l_addr,
2424 ((struct sockaddr *)(&stcb->asoc.primary_destination->ro._l_addr))->sa_len);
2425 net = stcb->asoc.primary_destination;
2426 ((struct sockaddr_in *)&sstat->sstat_primary.spinfo_address)->sin_port = stcb->rport;
2428 * Again the user can get info from sctp_constants.h
2429 * for what the state of the network is.
2431 sstat->sstat_primary.spinfo_state = net->dest_state & SCTP_REACHABLE_MASK;
2432 sstat->sstat_primary.spinfo_cwnd = net->cwnd;
2433 sstat->sstat_primary.spinfo_srtt = net->lastsa;
2434 sstat->sstat_primary.spinfo_rto = net->RTO;
2435 sstat->sstat_primary.spinfo_mtu = net->mtu;
2436 sstat->sstat_primary.spinfo_assoc_id = sctp_get_associd(stcb);
2437 SCTP_TCB_UNLOCK(stcb);
2438 m->m_len = sizeof(*sstat);
2440 break;
2441 case SCTP_RTOINFO:
2443 struct sctp_rtoinfo *srto;
2444 #ifdef SCTP_DEBUG
2445 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2446 kprintf("RTO Info\n");
2448 #endif /* SCTP_DEBUG */
2449 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
2450 error = EINVAL;
2451 break;
2453 srto = mtod(m, struct sctp_rtoinfo *);
2454 if (srto->srto_assoc_id == 0) {
2455 /* Endpoint only please */
2456 SCTP_INP_RLOCK(inp);
2457 srto->srto_initial = inp->sctp_ep.initial_rto;
2458 srto->srto_max = inp->sctp_ep.sctp_maxrto;
2459 srto->srto_min = inp->sctp_ep.sctp_minrto;
2460 SCTP_INP_RUNLOCK(inp);
2461 break;
2463 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2464 SCTP_INP_RLOCK(inp);
2465 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2466 if (stcb)
2467 SCTP_TCB_LOCK(stcb);
2468 SCTP_INP_RUNLOCK(inp);
2469 } else
2470 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
2472 if (stcb == NULL) {
2473 error = EINVAL;
2474 break;
2476 srto->srto_initial = stcb->asoc.initial_rto;
2477 srto->srto_max = stcb->asoc.maxrto;
2478 srto->srto_min = stcb->asoc.minrto;
2479 SCTP_TCB_UNLOCK(stcb);
2480 m->m_len = sizeof(*srto);
2482 break;
2483 case SCTP_ASSOCINFO:
2485 struct sctp_assocparams *sasoc;
2486 #ifdef SCTP_DEBUG
2487 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2488 kprintf("Associnfo\n");
2490 #endif /* SCTP_DEBUG */
2491 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
2492 error = EINVAL;
2493 break;
2495 sasoc = mtod(m, struct sctp_assocparams *);
2496 stcb = NULL;
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);
2505 if ((sasoc->sasoc_assoc_id) && (stcb == NULL)) {
2506 stcb = sctp_findassociation_ep_asocid(inp,
2507 sasoc->sasoc_assoc_id);
2508 if (stcb == NULL) {
2509 error = ENOENT;
2510 break;
2512 } else {
2513 stcb = NULL;
2516 if (stcb) {
2517 sasoc->sasoc_asocmaxrxt = stcb->asoc.max_send_times;
2518 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
2519 sasoc->sasoc_peer_rwnd = stcb->asoc.peers_rwnd;
2520 sasoc->sasoc_local_rwnd = stcb->asoc.my_rwnd;
2521 sasoc->sasoc_cookie_life = stcb->asoc.cookie_life;
2522 SCTP_TCB_UNLOCK(stcb);
2523 } else {
2524 SCTP_INP_RLOCK(inp);
2525 sasoc->sasoc_asocmaxrxt = inp->sctp_ep.max_send_times;
2526 sasoc->sasoc_number_peer_destinations = 0;
2527 sasoc->sasoc_peer_rwnd = 0;
2528 sasoc->sasoc_local_rwnd = ssb_space(&inp->sctp_socket->so_rcv);
2529 sasoc->sasoc_cookie_life = inp->sctp_ep.def_cookie_life;
2530 SCTP_INP_RUNLOCK(inp);
2532 m->m_len = sizeof(*sasoc);
2534 break;
2535 case SCTP_DEFAULT_SEND_PARAM:
2537 struct sctp_sndrcvinfo *s_info;
2539 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
2540 error = EINVAL;
2541 break;
2543 s_info = mtod(m, struct sctp_sndrcvinfo *);
2544 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2545 SCTP_INP_RLOCK(inp);
2546 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2547 if (stcb)
2548 SCTP_TCB_LOCK(stcb);
2549 SCTP_INP_RUNLOCK(inp);
2550 } else
2551 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
2553 if (stcb == NULL) {
2554 error = ENOENT;
2555 break;
2557 /* Copy it out */
2558 *s_info = stcb->asoc.def_send;
2559 SCTP_TCB_UNLOCK(stcb);
2560 m->m_len = sizeof(*s_info);
2562 case SCTP_INITMSG:
2564 struct sctp_initmsg *sinit;
2565 #ifdef SCTP_DEBUG
2566 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2567 kprintf("initmsg\n");
2569 #endif /* SCTP_DEBUG */
2570 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
2571 error = EINVAL;
2572 break;
2574 sinit = mtod(m, struct sctp_initmsg *);
2575 SCTP_INP_RLOCK(inp);
2576 sinit->sinit_num_ostreams = inp->sctp_ep.pre_open_stream_count;
2577 sinit->sinit_max_instreams = inp->sctp_ep.max_open_streams_intome;
2578 sinit->sinit_max_attempts = inp->sctp_ep.max_init_times;
2579 sinit->sinit_max_init_timeo = inp->sctp_ep.initial_init_rto_max;
2580 SCTP_INP_RUNLOCK(inp);
2581 m->m_len = sizeof(*sinit);
2583 break;
2584 case SCTP_PRIMARY_ADDR:
2585 /* we allow a "get" operation on this */
2587 struct sctp_setprim *ssp;
2589 #ifdef SCTP_DEBUG
2590 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2591 kprintf("setprimary\n");
2593 #endif /* SCTP_DEBUG */
2594 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
2595 error = EINVAL;
2596 break;
2598 ssp = mtod(m, struct sctp_setprim *);
2599 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2600 SCTP_INP_RLOCK(inp);
2601 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2602 if (stcb)
2603 SCTP_TCB_LOCK(stcb);
2604 SCTP_INP_RUNLOCK(inp);
2605 } else {
2606 stcb = sctp_findassociation_ep_asocid(inp, ssp->ssp_assoc_id);
2607 if (stcb == NULL) {
2608 /* one last shot, try it by the address in */
2609 struct sctp_nets *net;
2611 SCTP_INP_WLOCK(inp);
2612 SCTP_INP_INCR_REF(inp);
2613 SCTP_INP_WUNLOCK(inp);
2614 stcb = sctp_findassociation_ep_addr(&inp,
2615 (struct sockaddr *)&ssp->ssp_addr,
2616 &net, NULL, NULL);
2617 if (stcb == NULL) {
2618 SCTP_INP_WLOCK(inp);
2619 SCTP_INP_DECR_REF(inp);
2620 SCTP_INP_WUNLOCK(inp);
2623 if (stcb == NULL) {
2624 error = EINVAL;
2625 break;
2628 /* simply copy out the sockaddr_storage... */
2629 memcpy(&ssp->ssp_addr,
2630 &stcb->asoc.primary_destination->ro._l_addr,
2631 ((struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr)->sa_len);
2632 SCTP_TCB_UNLOCK(stcb);
2633 m->m_len = sizeof(*ssp);
2635 break;
2636 default:
2637 error = ENOPROTOOPT;
2638 m->m_len = 0;
2639 break;
2640 } /* end switch (sopt->sopt_name) */
2641 return (error);
2644 static int
2645 sctp_optsset(struct socket *so,
2646 int opt,
2647 struct mbuf **mp,
2648 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
2649 struct thread *p
2650 #else
2651 struct proc *p
2652 #endif
2655 int error, *mopt, set_opt;
2656 struct mbuf *m;
2657 struct sctp_tcb *stcb = NULL;
2658 struct sctp_inpcb *inp;
2660 if (mp == NULL) {
2661 #ifdef SCTP_DEBUG
2662 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
2663 kprintf("optsset:MP is NULL EINVAL\n");
2665 #endif /* SCTP_DEBUG */
2666 return (EINVAL);
2668 m = *mp;
2669 if (m == NULL)
2670 return (EINVAL);
2672 inp = (struct sctp_inpcb *)so->so_pcb;
2673 if (inp == 0)
2674 return EINVAL;
2676 error = 0;
2677 switch (opt) {
2678 case SCTP_NODELAY:
2679 case SCTP_AUTOCLOSE:
2680 case SCTP_AUTO_ASCONF:
2681 case SCTP_DISABLE_FRAGMENTS:
2682 case SCTP_I_WANT_MAPPED_V4_ADDR:
2683 /* copy in the option value */
2684 if ((size_t)m->m_len < sizeof(int)) {
2685 error = EINVAL;
2686 break;
2688 mopt = mtod(m, int *);
2689 set_opt = 0;
2690 if (error)
2691 break;
2692 switch (opt) {
2693 case SCTP_DISABLE_FRAGMENTS:
2694 set_opt = SCTP_PCB_FLAGS_NO_FRAGMENT;
2695 break;
2696 case SCTP_AUTO_ASCONF:
2697 set_opt = SCTP_PCB_FLAGS_AUTO_ASCONF;
2698 break;
2700 case SCTP_I_WANT_MAPPED_V4_ADDR:
2701 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2702 set_opt = SCTP_PCB_FLAGS_NEEDS_MAPPED_V4;
2703 } else {
2704 return (EINVAL);
2706 break;
2707 case SCTP_NODELAY:
2708 set_opt = SCTP_PCB_FLAGS_NODELAY;
2709 break;
2710 case SCTP_AUTOCLOSE:
2711 set_opt = SCTP_PCB_FLAGS_AUTOCLOSE;
2713 * The value is in ticks.
2714 * Note this does not effect old associations, only
2715 * new ones.
2717 inp->sctp_ep.auto_close_time = (*mopt * hz);
2718 break;
2720 SCTP_INP_WLOCK(inp);
2721 if (*mopt != 0) {
2722 inp->sctp_flags |= set_opt;
2723 } else {
2724 inp->sctp_flags &= ~set_opt;
2726 SCTP_INP_WUNLOCK(inp);
2727 break;
2728 case SCTP_MY_PUBLIC_KEY: /* set my public key */
2729 case SCTP_SET_AUTH_CHUNKS: /* set the authenticated chunks required */
2730 case SCTP_SET_AUTH_SECRET: /* set the actual secret for the endpoint */
2731 /* not supported yet and until we refine the draft */
2732 error = EOPNOTSUPP;
2733 break;
2735 case SCTP_CLR_STAT_LOG:
2736 #ifdef SCTP_STAT_LOGGING
2737 sctp_clr_stat_log();
2738 #else
2739 error = EOPNOTSUPP;
2740 #endif
2741 break;
2742 case SCTP_DELAYED_ACK_TIME:
2744 int32_t *tm;
2745 if ((size_t)m->m_len < sizeof(int32_t)) {
2746 error = EINVAL;
2747 break;
2749 tm = mtod(m, int32_t *);
2751 if ((*tm < 10) || (*tm > 500)) {
2752 /* can't be smaller than 10ms */
2753 /* MUST NOT be larger than 500ms */
2754 error = EINVAL;
2755 break;
2757 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV] = MSEC_TO_TICKS(*tm);
2759 break;
2760 case SCTP_RESET_STREAMS:
2762 struct sctp_stream_reset *strrst;
2763 uint8_t two_way, not_peer;
2765 if ((size_t)m->m_len < sizeof(struct sctp_stream_reset)) {
2766 error = EINVAL;
2767 break;
2769 strrst = mtod(m, struct sctp_stream_reset *);
2771 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2772 SCTP_INP_RLOCK(inp);
2773 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2774 if (stcb)
2775 SCTP_TCB_LOCK(stcb);
2776 SCTP_INP_RUNLOCK(inp);
2777 } else
2778 stcb = sctp_findassociation_ep_asocid(inp, strrst->strrst_assoc_id);
2779 if (stcb == NULL) {
2780 error = ENOENT;
2781 break;
2783 if (stcb->asoc.peer_supports_strreset == 0) {
2784 /* Peer does not support it,
2785 * we return protocol not supported since
2786 * this is true for this feature and this
2787 * peer, not the socket request in general.
2789 error = EPROTONOSUPPORT;
2790 SCTP_TCB_UNLOCK(stcb);
2791 break;
2794 /* Having re-thought this code I added as I write the I-D there
2795 * is NO need for it. The peer, if we are requesting a stream-reset
2796 * will send a request to us but will itself do what we do, take
2797 * and copy off the "reset information" we send and queue TSN's
2798 * larger than the send-next in our response message. Thus they
2799 * will handle it.
2801 /* if (stcb->asoc.sending_seq != (stcb->asoc.last_acked_seq + 1)) {*/
2802 /* Must have all sending data ack'd before we
2803 * start this procedure. This is a bit restrictive
2804 * and we SHOULD work on changing this so ONLY the
2805 * streams being RESET get held up. So, a reset-all
2806 * would require this.. but a reset specific just
2807 * needs to be sure that the ones being reset have
2808 * nothing on the send_queue. For now we will
2809 * skip this more detailed method and do a course
2810 * way.. i.e. nothing pending ... for future FIX ME!
2812 /* error = EBUSY;*/
2813 /* break;*/
2814 /* }*/
2816 if (stcb->asoc.stream_reset_outstanding) {
2817 error = EALREADY;
2818 SCTP_TCB_UNLOCK(stcb);
2819 break;
2821 if (strrst->strrst_flags == SCTP_RESET_LOCAL_RECV) {
2822 two_way = 0;
2823 not_peer = 0;
2824 } else if (strrst->strrst_flags == SCTP_RESET_LOCAL_SEND) {
2825 two_way = 1;
2826 not_peer = 1;
2827 } else if (strrst->strrst_flags == SCTP_RESET_BOTH) {
2828 two_way = 1;
2829 not_peer = 0;
2830 } else {
2831 error = EINVAL;
2832 SCTP_TCB_UNLOCK(stcb);
2833 break;
2835 sctp_send_str_reset_req(stcb, strrst->strrst_num_streams,
2836 strrst->strrst_list, two_way, not_peer);
2837 sctp_chunk_output(inp, stcb, 12);
2838 SCTP_TCB_UNLOCK(stcb);
2841 break;
2842 case SCTP_RESET_PEGS:
2843 memset(sctp_pegs, 0, sizeof(sctp_pegs));
2844 error = 0;
2845 break;
2846 case SCTP_CONNECT_X:
2847 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2848 error = EINVAL;
2849 break;
2851 error = sctp_do_connect_x(so, inp, m, p, 0);
2852 break;
2854 case SCTP_CONNECT_X_DELAYED:
2855 if ((size_t)m->m_len < (sizeof(int) + sizeof(struct sockaddr_in))) {
2856 error = EINVAL;
2857 break;
2859 error = sctp_do_connect_x(so, inp, m, p, 1);
2860 break;
2862 case SCTP_CONNECT_X_COMPLETE:
2864 struct sockaddr *sa;
2865 struct sctp_nets *net;
2866 if ((size_t)m->m_len < sizeof(struct sockaddr_in)) {
2867 error = EINVAL;
2868 break;
2870 sa = mtod(m, struct sockaddr *);
2871 /* find tcb */
2872 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
2873 SCTP_INP_RLOCK(inp);
2874 stcb = LIST_FIRST(&inp->sctp_asoc_list);
2875 if (stcb) {
2876 SCTP_TCB_LOCK(stcb);
2877 net = sctp_findnet(stcb, sa);
2879 SCTP_INP_RUNLOCK(inp);
2880 } else {
2881 SCTP_INP_WLOCK(inp);
2882 SCTP_INP_INCR_REF(inp);
2883 SCTP_INP_WUNLOCK(inp);
2884 stcb = sctp_findassociation_ep_addr(&inp, sa, &net, NULL, NULL);
2885 if (stcb == NULL) {
2886 SCTP_INP_WLOCK(inp);
2887 SCTP_INP_DECR_REF(inp);
2888 SCTP_INP_WUNLOCK(inp);
2892 if (stcb == NULL) {
2893 error = ENOENT;
2894 break;
2896 if (stcb->asoc.delayed_connection == 1) {
2897 stcb->asoc.delayed_connection = 0;
2898 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
2899 sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, stcb->asoc.primary_destination);
2900 sctp_send_initiate(inp, stcb);
2901 } else {
2902 /* already expired or did not use delayed connectx */
2903 error = EALREADY;
2905 SCTP_TCB_UNLOCK(stcb);
2907 break;
2908 case SCTP_MAXBURST:
2910 u_int8_t *burst;
2911 SCTP_INP_WLOCK(inp);
2912 burst = mtod(m, u_int8_t *);
2913 if (*burst) {
2914 inp->sctp_ep.max_burst = *burst;
2916 SCTP_INP_WUNLOCK(inp);
2918 break;
2919 case SCTP_MAXSEG:
2921 u_int32_t *segsize;
2922 int ovh;
2923 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
2924 ovh = SCTP_MED_OVERHEAD;
2925 } else {
2926 ovh = SCTP_MED_V4_OVERHEAD;
2928 segsize = mtod(m, u_int32_t *);
2929 if (*segsize < 1) {
2930 error = EINVAL;
2931 break;
2933 SCTP_INP_WLOCK(inp);
2934 inp->sctp_frag_point = (*segsize+ovh);
2935 if (inp->sctp_frag_point < MHLEN) {
2936 inp->sctp_frag_point = MHLEN;
2938 SCTP_INP_WUNLOCK(inp);
2940 break;
2941 case SCTP_SET_DEBUG_LEVEL:
2942 #ifdef SCTP_DEBUG
2944 u_int32_t *level;
2945 if ((size_t)m->m_len < sizeof(u_int32_t)) {
2946 error = EINVAL;
2947 break;
2949 level = mtod(m, u_int32_t *);
2950 error = 0;
2951 sctp_debug_on = (*level & (SCTP_DEBUG_ALL |
2952 SCTP_DEBUG_NOISY));
2953 kprintf("SETTING DEBUG LEVEL to %x\n",
2954 (u_int)sctp_debug_on);
2957 #else
2958 error = EOPNOTSUPP;
2959 #endif /* SCTP_DEBUG */
2960 break;
2961 case SCTP_EVENTS:
2963 struct sctp_event_subscribe *events;
2964 if ((size_t)m->m_len < sizeof(struct sctp_event_subscribe)) {
2965 error = EINVAL;
2966 break;
2968 SCTP_INP_WLOCK(inp);
2969 events = mtod(m, struct sctp_event_subscribe *);
2970 if (events->sctp_data_io_event) {
2971 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVDATAIOEVNT;
2972 } else {
2973 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVDATAIOEVNT;
2976 if (events->sctp_association_event) {
2977 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVASSOCEVNT;
2978 } else {
2979 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVASSOCEVNT;
2982 if (events->sctp_address_event) {
2983 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPADDREVNT;
2984 } else {
2985 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPADDREVNT;
2988 if (events->sctp_send_failure_event) {
2989 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2990 } else {
2991 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSENDFAILEVNT;
2994 if (events->sctp_peer_error_event) {
2995 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVPEERERR;
2996 } else {
2997 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVPEERERR;
3000 if (events->sctp_shutdown_event) {
3001 inp->sctp_flags |= SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3002 } else {
3003 inp->sctp_flags &= ~SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT;
3006 if (events->sctp_partial_delivery_event) {
3007 inp->sctp_flags |= SCTP_PCB_FLAGS_PDAPIEVNT;
3008 } else {
3009 inp->sctp_flags &= ~SCTP_PCB_FLAGS_PDAPIEVNT;
3012 if (events->sctp_adaption_layer_event) {
3013 inp->sctp_flags |= SCTP_PCB_FLAGS_ADAPTIONEVNT;
3014 } else {
3015 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ADAPTIONEVNT;
3018 if (events->sctp_stream_reset_events) {
3019 inp->sctp_flags |= SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3020 } else {
3021 inp->sctp_flags &= ~SCTP_PCB_FLAGS_STREAM_RESETEVNT;
3023 SCTP_INP_WUNLOCK(inp);
3025 break;
3027 case SCTP_ADAPTION_LAYER:
3029 struct sctp_setadaption *adap_bits;
3030 if ((size_t)m->m_len < sizeof(struct sctp_setadaption)) {
3031 error = EINVAL;
3032 break;
3034 SCTP_INP_WLOCK(inp);
3035 adap_bits = mtod(m, struct sctp_setadaption *);
3036 inp->sctp_ep.adaption_layer_indicator = adap_bits->ssb_adaption_ind;
3037 SCTP_INP_WUNLOCK(inp);
3039 break;
3040 case SCTP_SET_INITIAL_DBG_SEQ:
3042 u_int32_t *vvv;
3043 if ((size_t)m->m_len < sizeof(u_int32_t)) {
3044 error = EINVAL;
3045 break;
3047 SCTP_INP_WLOCK(inp);
3048 vvv = mtod(m, u_int32_t *);
3049 inp->sctp_ep.initial_sequence_debug = *vvv;
3050 SCTP_INP_WUNLOCK(inp);
3052 break;
3053 case SCTP_DEFAULT_SEND_PARAM:
3055 struct sctp_sndrcvinfo *s_info;
3057 if (m->m_len != sizeof(struct sctp_sndrcvinfo)) {
3058 error = EINVAL;
3059 break;
3061 s_info = mtod(m, struct sctp_sndrcvinfo *);
3063 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3064 SCTP_INP_RLOCK(inp);
3065 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3066 if (stcb)
3067 SCTP_TCB_LOCK(stcb);
3068 SCTP_INP_RUNLOCK(inp);
3069 } else
3070 stcb = sctp_findassociation_ep_asocid(inp, s_info->sinfo_assoc_id);
3072 if (stcb == NULL) {
3073 error = ENOENT;
3074 break;
3076 /* Validate things */
3077 if (s_info->sinfo_stream > stcb->asoc.streamoutcnt) {
3078 SCTP_TCB_UNLOCK(stcb);
3079 error = EINVAL;
3080 break;
3082 /* Mask off the flags that are allowed */
3083 s_info->sinfo_flags = (s_info->sinfo_flags &
3084 (MSG_UNORDERED | MSG_ADDR_OVER |
3085 MSG_PR_SCTP_TTL | MSG_PR_SCTP_BUF));
3086 /* Copy it in */
3087 stcb->asoc.def_send = *s_info;
3088 SCTP_TCB_UNLOCK(stcb);
3090 break;
3091 case SCTP_PEER_ADDR_PARAMS:
3093 struct sctp_paddrparams *paddrp;
3094 struct sctp_nets *net;
3095 if ((size_t)m->m_len < sizeof(struct sctp_paddrparams)) {
3096 error = EINVAL;
3097 break;
3099 paddrp = mtod(m, struct sctp_paddrparams *);
3100 net = NULL;
3101 if (paddrp->spp_assoc_id) {
3102 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3103 SCTP_INP_RLOCK(inp);
3104 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3105 if (stcb) {
3106 SCTP_TCB_LOCK(stcb);
3107 net = sctp_findnet(stcb, (struct sockaddr *)&paddrp->spp_address);
3109 SCTP_INP_RUNLOCK(inp);
3110 } else
3111 stcb = sctp_findassociation_ep_asocid(inp, paddrp->spp_assoc_id);
3112 if (stcb == NULL) {
3113 error = ENOENT;
3114 break;
3118 if ((stcb == NULL) &&
3119 ((((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET) ||
3120 (((struct sockaddr *)&paddrp->spp_address)->sa_family == AF_INET6))) {
3121 /* Lookup via address */
3122 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3123 SCTP_INP_RLOCK(inp);
3124 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3125 if (stcb) {
3126 SCTP_TCB_LOCK(stcb);
3127 net = sctp_findnet(stcb,
3128 (struct sockaddr *)&paddrp->spp_address);
3130 SCTP_INP_RUNLOCK(inp);
3131 } else {
3132 SCTP_INP_WLOCK(inp);
3133 SCTP_INP_INCR_REF(inp);
3134 SCTP_INP_WUNLOCK(inp);
3135 stcb = sctp_findassociation_ep_addr(&inp,
3136 (struct sockaddr *)&paddrp->spp_address,
3137 &net, NULL, NULL);
3138 if (stcb == NULL) {
3139 SCTP_INP_WLOCK(inp);
3140 SCTP_INP_DECR_REF(inp);
3141 SCTP_INP_WUNLOCK(inp);
3144 } else {
3145 /* Effects the Endpoint */
3146 stcb = NULL;
3148 if (stcb) {
3149 /* Applies to the specific association */
3150 if (paddrp->spp_pathmaxrxt) {
3151 if (net) {
3152 if (paddrp->spp_pathmaxrxt)
3153 net->failure_threshold = paddrp->spp_pathmaxrxt;
3154 } else {
3155 if (paddrp->spp_pathmaxrxt)
3156 stcb->asoc.def_net_failure = paddrp->spp_pathmaxrxt;
3159 if ((paddrp->spp_hbinterval != 0) && (paddrp->spp_hbinterval != 0xffffffff)) {
3160 /* Just a set */
3161 int old;
3162 if (net) {
3163 net->dest_state &= ~SCTP_ADDR_NOHB;
3164 } else {
3165 old = stcb->asoc.heart_beat_delay;
3166 stcb->asoc.heart_beat_delay = paddrp->spp_hbinterval;
3167 if (old == 0) {
3168 /* Turn back on the timer */
3169 sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3172 } else if (paddrp->spp_hbinterval == 0xffffffff) {
3173 /* on demand HB */
3174 sctp_send_hb(stcb, 1, net);
3175 } else {
3176 if (net == NULL) {
3177 /* off on association */
3178 if (stcb->asoc.heart_beat_delay) {
3179 int cnt_of_unconf = 0;
3180 struct sctp_nets *lnet;
3181 TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
3182 if (lnet->dest_state & SCTP_ADDR_UNCONFIRMED) {
3183 cnt_of_unconf++;
3186 /* stop the timer ONLY if we have no unconfirmed addresses
3188 if (cnt_of_unconf == 0)
3189 sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
3191 stcb->asoc.heart_beat_delay = 0;
3192 } else {
3193 net->dest_state |= SCTP_ADDR_NOHB;
3196 SCTP_TCB_UNLOCK(stcb);
3197 } else {
3198 /* Use endpoint defaults */
3199 SCTP_INP_WLOCK(inp);
3200 if (paddrp->spp_pathmaxrxt)
3201 inp->sctp_ep.def_net_failure = paddrp->spp_pathmaxrxt;
3202 if (paddrp->spp_hbinterval != SCTP_ISSUE_HB)
3203 inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT] = paddrp->spp_hbinterval;
3204 SCTP_INP_WUNLOCK(inp);
3207 break;
3208 case SCTP_RTOINFO:
3210 struct sctp_rtoinfo *srto;
3211 if ((size_t)m->m_len < sizeof(struct sctp_rtoinfo)) {
3212 error = EINVAL;
3213 break;
3215 srto = mtod(m, struct sctp_rtoinfo *);
3216 if (srto->srto_assoc_id == 0) {
3217 SCTP_INP_WLOCK(inp);
3218 /* If we have a null asoc, its default for the endpoint */
3219 if (srto->srto_initial > 10)
3220 inp->sctp_ep.initial_rto = srto->srto_initial;
3221 if (srto->srto_max > 10)
3222 inp->sctp_ep.sctp_maxrto = srto->srto_max;
3223 if (srto->srto_min > 10)
3224 inp->sctp_ep.sctp_minrto = srto->srto_min;
3225 SCTP_INP_WUNLOCK(inp);
3226 break;
3228 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3229 SCTP_INP_RLOCK(inp);
3230 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3231 if (stcb)
3232 SCTP_TCB_LOCK(stcb);
3233 SCTP_INP_RUNLOCK(inp);
3234 } else
3235 stcb = sctp_findassociation_ep_asocid(inp, srto->srto_assoc_id);
3236 if (stcb == NULL) {
3237 error = EINVAL;
3238 break;
3240 /* Set in ms we hope :-) */
3241 if (srto->srto_initial > 10)
3242 stcb->asoc.initial_rto = srto->srto_initial;
3243 if (srto->srto_max > 10)
3244 stcb->asoc.maxrto = srto->srto_max;
3245 if (srto->srto_min > 10)
3246 stcb->asoc.minrto = srto->srto_min;
3247 SCTP_TCB_UNLOCK(stcb);
3249 break;
3250 case SCTP_ASSOCINFO:
3252 struct sctp_assocparams *sasoc;
3254 if ((size_t)m->m_len < sizeof(struct sctp_assocparams)) {
3255 error = EINVAL;
3256 break;
3258 sasoc = mtod(m, struct sctp_assocparams *);
3259 if (sasoc->sasoc_assoc_id) {
3260 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3261 SCTP_INP_RLOCK(inp);
3262 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3263 if (stcb)
3264 SCTP_TCB_LOCK(stcb);
3265 SCTP_INP_RUNLOCK(inp);
3266 } else
3267 stcb = sctp_findassociation_ep_asocid(inp,
3268 sasoc->sasoc_assoc_id);
3269 if (stcb == NULL) {
3270 error = ENOENT;
3271 break;
3274 } else {
3275 stcb = NULL;
3277 if (stcb) {
3278 if (sasoc->sasoc_asocmaxrxt)
3279 stcb->asoc.max_send_times = sasoc->sasoc_asocmaxrxt;
3280 sasoc->sasoc_number_peer_destinations = stcb->asoc.numnets;
3281 sasoc->sasoc_peer_rwnd = 0;
3282 sasoc->sasoc_local_rwnd = 0;
3283 if (stcb->asoc.cookie_life)
3284 stcb->asoc.cookie_life = sasoc->sasoc_cookie_life;
3285 SCTP_TCB_UNLOCK(stcb);
3286 } else {
3287 SCTP_INP_WLOCK(inp);
3288 if (sasoc->sasoc_asocmaxrxt)
3289 inp->sctp_ep.max_send_times = sasoc->sasoc_asocmaxrxt;
3290 sasoc->sasoc_number_peer_destinations = 0;
3291 sasoc->sasoc_peer_rwnd = 0;
3292 sasoc->sasoc_local_rwnd = 0;
3293 if (sasoc->sasoc_cookie_life)
3294 inp->sctp_ep.def_cookie_life = sasoc->sasoc_cookie_life;
3295 SCTP_INP_WUNLOCK(inp);
3298 break;
3299 case SCTP_INITMSG:
3301 struct sctp_initmsg *sinit;
3303 if ((size_t)m->m_len < sizeof(struct sctp_initmsg)) {
3304 error = EINVAL;
3305 break;
3307 sinit = mtod(m, struct sctp_initmsg *);
3308 SCTP_INP_WLOCK(inp);
3309 if (sinit->sinit_num_ostreams)
3310 inp->sctp_ep.pre_open_stream_count = sinit->sinit_num_ostreams;
3312 if (sinit->sinit_max_instreams)
3313 inp->sctp_ep.max_open_streams_intome = sinit->sinit_max_instreams;
3315 if (sinit->sinit_max_attempts)
3316 inp->sctp_ep.max_init_times = sinit->sinit_max_attempts;
3318 if (sinit->sinit_max_init_timeo > 10)
3319 /* We must be at least a 100ms (we set in ticks) */
3320 inp->sctp_ep.initial_init_rto_max = sinit->sinit_max_init_timeo;
3321 SCTP_INP_WUNLOCK(inp);
3323 break;
3324 case SCTP_PRIMARY_ADDR:
3326 struct sctp_setprim *spa;
3327 struct sctp_nets *net, *lnet;
3328 if ((size_t)m->m_len < sizeof(struct sctp_setprim)) {
3329 error = EINVAL;
3330 break;
3332 spa = mtod(m, struct sctp_setprim *);
3334 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3335 SCTP_INP_RLOCK(inp);
3336 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3337 if (stcb) {
3338 SCTP_TCB_LOCK(stcb);
3339 } else {
3340 error = EINVAL;
3341 break;
3343 SCTP_INP_RUNLOCK(inp);
3344 } else
3345 stcb = sctp_findassociation_ep_asocid(inp, spa->ssp_assoc_id);
3346 if (stcb == NULL) {
3347 /* One last shot */
3348 SCTP_INP_WLOCK(inp);
3349 SCTP_INP_INCR_REF(inp);
3350 SCTP_INP_WUNLOCK(inp);
3351 stcb = sctp_findassociation_ep_addr(&inp,
3352 (struct sockaddr *)&spa->ssp_addr,
3353 &net, NULL, NULL);
3354 if (stcb == NULL) {
3355 SCTP_INP_WLOCK(inp);
3356 SCTP_INP_DECR_REF(inp);
3357 SCTP_INP_WUNLOCK(inp);
3358 error = EINVAL;
3359 break;
3361 } else {
3362 /* find the net, associd or connected lookup type */
3363 net = sctp_findnet(stcb, (struct sockaddr *)&spa->ssp_addr);
3364 if (net == NULL) {
3365 SCTP_TCB_UNLOCK(stcb);
3366 error = EINVAL;
3367 break;
3370 if ((net != stcb->asoc.primary_destination) &&
3371 (!(net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
3372 /* Ok we need to set it */
3373 lnet = stcb->asoc.primary_destination;
3374 lnet->next_tsn_at_change = net->next_tsn_at_change = stcb->asoc.sending_seq;
3375 if (sctp_set_primary_addr(stcb,
3376 NULL,
3377 net) == 0) {
3378 if (net->dest_state & SCTP_ADDR_SWITCH_PRIMARY) {
3379 net->dest_state |= SCTP_ADDR_DOUBLE_SWITCH;
3381 net->dest_state |= SCTP_ADDR_SWITCH_PRIMARY;
3384 SCTP_TCB_UNLOCK(stcb);
3386 break;
3388 case SCTP_SET_PEER_PRIMARY_ADDR:
3390 struct sctp_setpeerprim *sspp;
3391 if ((size_t)m->m_len < sizeof(struct sctp_setpeerprim)) {
3392 error = EINVAL;
3393 break;
3395 sspp = mtod(m, struct sctp_setpeerprim *);
3398 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3399 SCTP_INP_RLOCK(inp);
3400 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3401 if (stcb)
3402 SCTP_TCB_UNLOCK(stcb);
3403 SCTP_INP_RUNLOCK(inp);
3404 } else
3405 stcb = sctp_findassociation_ep_asocid(inp, sspp->sspp_assoc_id);
3406 if (stcb == NULL) {
3407 error = EINVAL;
3408 break;
3410 if (sctp_set_primary_ip_address_sa(stcb, (struct sockaddr *)&sspp->sspp_addr) != 0) {
3411 error = EINVAL;
3413 SCTP_TCB_UNLOCK(stcb);
3415 break;
3416 case SCTP_BINDX_ADD_ADDR:
3418 struct sctp_getaddresses *addrs;
3419 struct sockaddr *addr_touse;
3420 struct sockaddr_in sin;
3421 /* see if we're bound all already! */
3422 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3423 error = EINVAL;
3424 break;
3426 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
3427 error = EINVAL;
3428 break;
3430 addrs = mtod(m, struct sctp_getaddresses *);
3431 addr_touse = addrs->addr;
3432 if (addrs->addr->sa_family == AF_INET6) {
3433 struct sockaddr_in6 *sin6;
3434 sin6 = (struct sockaddr_in6 *)addr_touse;
3435 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3436 in6_sin6_2_sin(&sin, sin6);
3437 addr_touse = (struct sockaddr *)&sin;
3440 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3441 if (p == NULL) {
3442 /* Can't get proc for Net/Open BSD */
3443 error = EINVAL;
3444 break;
3446 error = sctp_inpcb_bind(so, addr_touse, p);
3447 break;
3449 /* No locks required here since bind and mgmt_ep_sa all
3450 * do their own locking. If we do something for the FIX:
3451 * below we may need to lock in that case.
3453 if (addrs->sget_assoc_id == 0) {
3454 /* add the address */
3455 struct sctp_inpcb *lep;
3456 ((struct sockaddr_in *)addr_touse)->sin_port = inp->sctp_lport;
3457 lep = sctp_pcb_findep(addr_touse, 1, 0);
3458 if (lep != NULL) {
3459 /* We must decrement the refcount
3460 * since we have the ep already and
3461 * are binding. No remove going on
3462 * here.
3464 SCTP_INP_WLOCK(inp);
3465 SCTP_INP_DECR_REF(inp);
3466 SCTP_INP_WUNLOCK(inp);
3468 if (lep == inp) {
3469 /* already bound to it.. ok */
3470 break;
3471 } else if (lep == NULL) {
3472 ((struct sockaddr_in *)addr_touse)->sin_port = 0;
3473 error = sctp_addr_mgmt_ep_sa(inp, addr_touse,
3474 SCTP_ADD_IP_ADDRESS);
3475 } else {
3476 error = EADDRNOTAVAIL;
3478 if (error)
3479 break;
3481 } else {
3482 /* FIX: decide whether we allow assoc based bindx */
3485 break;
3486 case SCTP_BINDX_REM_ADDR:
3488 struct sctp_getaddresses *addrs;
3489 struct sockaddr *addr_touse;
3490 struct sockaddr_in sin;
3491 /* see if we're bound all already! */
3492 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3493 error = EINVAL;
3494 break;
3496 if ((size_t)m->m_len < sizeof(struct sctp_getaddresses)) {
3497 error = EINVAL;
3498 break;
3500 addrs = mtod(m, struct sctp_getaddresses *);
3501 addr_touse = addrs->addr;
3502 if (addrs->addr->sa_family == AF_INET6) {
3503 struct sockaddr_in6 *sin6;
3504 sin6 = (struct sockaddr_in6 *)addr_touse;
3505 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
3506 in6_sin6_2_sin(&sin, sin6);
3507 addr_touse = (struct sockaddr *)&sin;
3510 /* No lock required mgmt_ep_sa does its own locking. If
3511 * the FIX: below is ever changed we may need to
3512 * lock before calling association level binding.
3514 if (addrs->sget_assoc_id == 0) {
3515 /* delete the address */
3516 sctp_addr_mgmt_ep_sa(inp, addr_touse,
3517 SCTP_DEL_IP_ADDRESS);
3518 } else {
3519 /* FIX: decide whether we allow assoc based bindx */
3522 break;
3523 default:
3524 error = ENOPROTOOPT;
3525 break;
3526 } /* end switch (opt) */
3527 return (error);
3530 void
3531 sctp_ctloutput(netmsg_t msg)
3533 struct socket *so = msg->ctloutput.base.nm_so;
3534 struct sockopt *sopt = msg->ctloutput.nm_sopt;
3535 struct mbuf *m = NULL;
3536 struct sctp_inpcb *inp;
3537 int error;
3539 inp = (struct sctp_inpcb *)so->so_pcb;
3541 if (inp == NULL) {
3542 /* I made the same as TCP since we are not setup? */
3543 error = ECONNRESET;
3544 goto out;
3546 if (sopt->sopt_level != IPPROTO_SCTP) {
3547 /* wrong proto level... send back up to IP */
3548 #ifdef INET6
3549 if (INP_CHECK_SOCKAF(so, AF_INET6))
3550 ip6_ctloutput_dispatch(msg);
3551 else
3552 #endif /* INET6 */
3553 ip_ctloutput(msg);
3554 /* msg invalid now */
3555 return;
3557 if (sopt->sopt_valsize > MCLBYTES) {
3559 * Restrict us down to a cluster size, that's all we can
3560 * pass either way...
3562 sopt->sopt_valsize = MCLBYTES;
3564 if (sopt->sopt_valsize) {
3566 m = m_get(MB_WAIT, MT_DATA);
3567 if (sopt->sopt_valsize > MLEN) {
3568 MCLGET(m, MB_DONTWAIT);
3569 if ((m->m_flags & M_EXT) == 0) {
3570 sctp_m_freem(m);
3571 error = ENOBUFS;
3572 goto out;
3575 error = sooptcopyin(sopt, mtod(m, caddr_t), sopt->sopt_valsize,
3576 sopt->sopt_valsize);
3577 if (error) {
3578 m_free(m);
3579 goto out;
3581 m->m_len = sopt->sopt_valsize;
3583 if (sopt->sopt_dir == SOPT_SET) {
3584 #if (defined(__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3585 error = sctp_optsset(so, sopt->sopt_name, &m, sopt->sopt_td);
3586 #else
3587 error = sctp_optsset(so, sopt->sopt_name, &m, sopt->sopt_p);
3588 #endif
3589 } else if (sopt->sopt_dir == SOPT_GET) {
3590 #if (defined (__FreeBSD__) && __FreeBSD_version >= 500000) || defined(__DragonFly__)
3591 error = sctp_optsget(so, sopt->sopt_name, &m, sopt->sopt_td);
3592 #else
3593 error = sctp_optsget(so, sopt->sopt_name, &m, sopt->sopt_p);
3594 #endif
3595 } else {
3596 error = EINVAL;
3598 if ( (error == 0) && (m != NULL)) {
3599 error = sooptcopyout(sopt, mtod(m, caddr_t), m->m_len);
3600 sctp_m_freem(m);
3601 } else if (m != NULL) {
3602 sctp_m_freem(m);
3604 out:
3605 lwkt_replymsg(&msg->lmsg, error);
3608 static void
3609 sctp_connect(netmsg_t msg)
3611 struct socket *so = msg->connect.base.nm_so;
3612 struct sockaddr *addr = msg->connect.nm_nam;
3613 struct sctp_inpcb *inp;
3614 struct sctp_tcb *stcb;
3615 int error = 0;
3617 #ifdef SCTP_DEBUG
3618 if (sctp_debug_on & SCTP_DEBUG_PCB1) {
3619 kprintf("Connect called in SCTP to ");
3620 sctp_print_address(addr);
3621 kprintf("Port %d\n", ntohs(((struct sockaddr_in *)addr)->sin_port));
3623 #endif /* SCTP_DEBUG */
3624 inp = (struct sctp_inpcb *)so->so_pcb;
3625 if (inp == NULL) {
3626 /* I made the same as TCP since we are not setup? */
3627 error = ECONNRESET;
3628 goto out;
3630 SCTP_ASOC_CREATE_LOCK(inp);
3631 SCTP_INP_WLOCK(inp);
3632 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
3633 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
3634 /* Should I really unlock ? */
3635 SCTP_INP_WUNLOCK(inp);
3636 SCTP_ASOC_CREATE_UNLOCK(inp);
3637 error = EFAULT;
3638 goto out;
3640 #ifdef INET6
3641 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
3642 (addr->sa_family == AF_INET6)) {
3643 SCTP_INP_WUNLOCK(inp);
3644 SCTP_ASOC_CREATE_UNLOCK(inp);
3645 error = EINVAL;
3646 goto out;
3648 #endif /* INET6 */
3649 if ((inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) ==
3650 SCTP_PCB_FLAGS_UNBOUND) {
3651 /* Bind a ephemeral port */
3652 SCTP_INP_WUNLOCK(inp);
3653 error = sctp_inpcb_bind(so, NULL, msg->connect.nm_td);
3654 if (error) {
3655 SCTP_ASOC_CREATE_UNLOCK(inp);
3656 goto out;
3658 SCTP_INP_WLOCK(inp);
3660 /* Now do we connect? */
3661 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3662 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3663 /* We are already connected AND the TCP model */
3664 SCTP_INP_WUNLOCK(inp);
3665 SCTP_ASOC_CREATE_UNLOCK(inp);
3666 error = EADDRINUSE;
3667 goto out;
3669 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
3670 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3671 if (stcb)
3672 SCTP_TCB_UNLOCK(stcb);
3673 SCTP_INP_WUNLOCK(inp);
3674 } else {
3675 SCTP_INP_INCR_REF(inp);
3676 SCTP_INP_WUNLOCK(inp);
3677 stcb = sctp_findassociation_ep_addr(&inp, addr, NULL, NULL, NULL);
3678 if (stcb == NULL) {
3679 SCTP_INP_WLOCK(inp);
3680 SCTP_INP_DECR_REF(inp);
3681 SCTP_INP_WUNLOCK(inp);
3684 if (stcb != NULL) {
3685 /* Already have or am bring up an association */
3686 SCTP_ASOC_CREATE_UNLOCK(inp);
3687 SCTP_TCB_UNLOCK(stcb);
3688 error = EALREADY;
3689 goto out;
3691 /* We are GOOD to go */
3692 stcb = sctp_aloc_assoc(inp, addr, 1, &error, 0);
3693 if (stcb == NULL) {
3694 /* Gak! no memory */
3695 goto out;
3697 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) {
3698 stcb->sctp_ep->sctp_flags |= SCTP_PCB_FLAGS_CONNECTED;
3699 /* Set the connected flag so we can queue data */
3700 soisconnecting(so);
3702 stcb->asoc.state = SCTP_STATE_COOKIE_WAIT;
3703 SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
3704 sctp_send_initiate(inp, stcb);
3705 SCTP_ASOC_CREATE_UNLOCK(inp);
3706 SCTP_TCB_UNLOCK(stcb);
3707 out:
3708 lwkt_replymsg(&msg->lmsg, error);
3711 void
3712 sctp_usr_recvd(netmsg_t msg)
3714 struct socket *so = msg->rcvd.base.nm_so;
3715 struct sctp_socket_q_list *sq = NULL;
3716 int flags = msg->rcvd.nm_flags;
3717 int error;
3720 * The user has received some data, we may be able to stuff more
3721 * up the socket. And we need to possibly update the rwnd.
3723 struct sctp_inpcb *inp;
3724 struct sctp_tcb *stcb=NULL;
3726 inp = (struct sctp_inpcb *)so->so_pcb;
3727 #ifdef SCTP_DEBUG
3728 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3729 kprintf("Read for so:%p inp:%p Flags:%x\n",
3730 so, inp, (u_int)flags);
3731 #endif
3733 if (inp == NULL) {
3734 /* I made the same as TCP since we are not setup? */
3735 #ifdef SCTP_DEBUG
3736 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3737 kprintf("Nope, connection reset\n");
3738 #endif
3739 error = ECONNRESET;
3740 goto out;
3743 * Grab the first one on the list. It will re-insert itself if
3744 * it runs out of room
3746 SCTP_INP_WLOCK(inp);
3747 if ((flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3748 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3749 /* Ok the other part of our grubby tracking
3750 * stuff for our horrible layer violation that
3751 * the tsvwg thinks is ok for sctp_peeloff.. gak!
3752 * We must update the next vtag pending on the
3753 * socket buffer (if any).
3755 inp->sctp_vtag_first = sctp_get_first_vtag_from_sb(so);
3756 sq = TAILQ_FIRST(&inp->sctp_queue_list);
3757 if (sq) {
3758 stcb = sq->tcb;
3759 } else {
3760 stcb = NULL;
3762 } else {
3763 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3765 if (stcb)
3766 SCTP_TCB_LOCK(stcb);
3767 if (stcb) {
3768 long incr;
3769 /* all code in normal stcb path assumes
3770 * that you have a tcb_lock only. Thus
3771 * we must release the inp write lock.
3773 if (flags & MSG_EOR) {
3774 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3775 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3776 stcb = sctp_remove_from_socket_q(inp);
3778 #ifdef SCTP_DEBUG
3779 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3780 kprintf("remove from socket queue for inp:%p tcbret:%p\n",
3781 inp, stcb);
3782 #endif
3784 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3785 sizeof(struct mbuf));
3786 if (inp->sctp_flags & SCTP_PCB_FLAGS_RECVDATAIOEVNT) {
3787 stcb->asoc.my_rwnd_control_len = sctp_sbspace_sub(stcb->asoc.my_rwnd_control_len,
3788 CMSG_LEN(sizeof(struct sctp_sndrcvinfo)));
3791 if ((TAILQ_EMPTY(&stcb->asoc.delivery_queue) == 0) ||
3792 (TAILQ_EMPTY(&stcb->asoc.reasmqueue) == 0)) {
3793 /* Deliver if there is something to be delivered */
3794 sctp_service_queues(stcb, &stcb->asoc, 1);
3796 sctp_set_rwnd(stcb, &stcb->asoc);
3797 /* if we increase by 1 or more MTU's (smallest MTUs of all
3798 * nets) we send a window update sack
3800 incr = stcb->asoc.my_rwnd - stcb->asoc.my_last_reported_rwnd;
3801 if (incr < 0) {
3802 incr = 0;
3804 if (((uint32_t)incr >= (stcb->asoc.smallest_mtu * SCTP_SEG_TO_RWND_UPD)) ||
3805 ((((uint32_t)incr)*SCTP_SCALE_OF_RWND_TO_UPD) >= so->so_rcv.ssb_hiwat)) {
3806 if (callout_pending(&stcb->asoc.dack_timer.timer)) {
3807 /* If the timer is up, stop it */
3808 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
3809 stcb->sctp_ep, stcb, NULL);
3811 /* Send the sack, with the new rwnd */
3812 sctp_send_sack(stcb);
3813 /* Now do the output */
3814 sctp_chunk_output(inp, stcb, 10);
3816 } else {
3817 if ((( sq ) && (flags & MSG_EOR) && ((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0))
3818 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3819 stcb = sctp_remove_from_socket_q(inp);
3822 SOCKBUF_LOCK(&so->so_rcv);
3823 if (( so->so_rcv.ssb_mb == NULL ) &&
3824 (TAILQ_EMPTY(&inp->sctp_queue_list) == 0)) {
3825 int sq_cnt=0;
3826 #ifdef SCTP_DEBUG
3827 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3828 kprintf("Something off, inp:%p so->so_rcv->ssb_mb is empty and sockq is not.. cleaning\n",
3829 inp);
3830 #endif
3831 if (((inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) == 0)
3832 && ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
3833 int done_yet;
3834 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3835 while (!done_yet) {
3836 sq_cnt++;
3837 sctp_remove_from_socket_q(inp);
3838 done_yet = TAILQ_EMPTY(&inp->sctp_queue_list);
3841 #ifdef SCTP_DEBUG
3842 if (sctp_debug_on & SCTP_DEBUG_USRREQ2)
3843 kprintf("Cleaned up %d sockq's\n", sq_cnt);
3844 #endif
3846 SOCKBUF_UNLOCK(&so->so_rcv);
3847 if (stcb)
3848 SCTP_TCB_UNLOCK(stcb);
3849 SCTP_INP_WUNLOCK(inp);
3850 error = 0;
3851 out:
3852 lwkt_replymsg(&msg->lmsg, error);
3855 void
3856 sctp_listen(netmsg_t msg)
3858 struct socket *so = msg->listen.base.nm_so;
3859 int error;
3862 * Note this module depends on the protocol processing being
3863 * called AFTER any socket level flags and backlog are applied
3864 * to the socket. The traditional way that the socket flags are
3865 * applied is AFTER protocol processing. We have made a change
3866 * to the sys/kern/uipc_socket.c module to reverse this but this
3867 * MUST be in place if the socket API for SCTP is to work properly.
3869 struct sctp_inpcb *inp;
3871 inp = (struct sctp_inpcb *)so->so_pcb;
3872 if (inp == NULL) {
3873 /* I made the same as TCP since we are not setup? */
3874 error = ECONNRESET;
3875 goto out;
3877 SCTP_INP_RLOCK(inp);
3878 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
3879 (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED)) {
3880 /* We are already connected AND the TCP model */
3881 SCTP_INP_RUNLOCK(inp);
3882 error = EADDRINUSE;
3883 goto out;
3885 if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
3886 /* We must do a bind. */
3887 SCTP_INP_RUNLOCK(inp);
3888 if ((error = sctp_inpcb_bind(so, NULL, msg->listen.nm_td))) {
3889 /* bind error, probably perm */
3890 goto out;
3892 } else {
3893 SCTP_INP_RUNLOCK(inp);
3895 SOCK_LOCK(so);
3896 SCTP_INP_WLOCK(inp);
3897 if (inp->sctp_socket->so_qlimit) {
3898 if (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE) {
3900 * For the UDP model we must TURN OFF the ACCEPT
3901 * flags since we do NOT allow the accept() call.
3902 * The TCP model (when present) will do accept which
3903 * then prohibits connect().
3905 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
3907 inp->sctp_flags |= SCTP_PCB_FLAGS_ACCEPTING;
3908 } else {
3909 if (inp->sctp_flags & SCTP_PCB_FLAGS_ACCEPTING) {
3911 * Turning off the listen flags if the backlog is
3912 * set to 0 (i.e. qlimit is 0).
3914 inp->sctp_flags &= ~SCTP_PCB_FLAGS_ACCEPTING;
3916 inp->sctp_socket->so_options &= ~SO_ACCEPTCONN;
3918 SCTP_INP_WUNLOCK(inp);
3919 SOCK_UNLOCK(so);
3920 error = 0;
3921 out:
3922 lwkt_replymsg(&msg->lmsg, error);
3925 void
3926 sctp_accept(netmsg_t msg)
3928 struct socket *so = msg->accept.base.nm_so;
3929 struct sockaddr **addr = msg->accept.nm_nam;
3930 struct sctp_tcb *stcb;
3931 struct sockaddr *prim;
3932 struct sctp_inpcb *inp;
3933 int error;
3935 inp = (struct sctp_inpcb *)so->so_pcb;
3937 if (inp == NULL) {
3938 error = ECONNRESET;
3939 goto out;
3941 SCTP_INP_RLOCK(inp);
3942 if (so->so_state & SS_ISDISCONNECTED) {
3943 SCTP_INP_RUNLOCK(inp);
3944 error = ECONNABORTED;
3945 goto out;
3947 stcb = LIST_FIRST(&inp->sctp_asoc_list);
3948 if (stcb == NULL) {
3949 SCTP_INP_RUNLOCK(inp);
3950 error = ECONNRESET;
3951 goto out;
3953 SCTP_TCB_LOCK(stcb);
3954 SCTP_INP_RUNLOCK(inp);
3955 prim = (struct sockaddr *)&stcb->asoc.primary_destination->ro._l_addr;
3956 if (prim->sa_family == AF_INET) {
3957 struct sockaddr_in *sin;
3958 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
3959 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
3960 #else
3961 sin = (struct sockaddr_in *)addr;
3962 bzero((caddr_t)sin, sizeof (*sin));
3963 #endif
3964 sin->sin_family = AF_INET;
3965 sin->sin_len = sizeof(*sin);
3966 sin->sin_port = ((struct sockaddr_in *)prim)->sin_port;
3967 sin->sin_addr = ((struct sockaddr_in *)prim)->sin_addr;
3968 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
3969 *addr = (struct sockaddr *)sin;
3970 #else
3971 nam->m_len = sizeof(*sin);
3972 #endif
3973 } else {
3974 struct sockaddr_in6 *sin6;
3975 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
3976 sin6 = kmalloc(sizeof *sin6, M_SONAME, M_WAITOK | M_ZERO);
3977 #else
3978 sin6 = (struct sockaddr_in6 *)addr;
3979 #endif
3980 bzero((caddr_t)sin6, sizeof (*sin6));
3981 sin6->sin6_family = AF_INET6;
3982 sin6->sin6_len = sizeof(*sin6);
3983 sin6->sin6_port = ((struct sockaddr_in6 *)prim)->sin6_port;
3985 sin6->sin6_addr = ((struct sockaddr_in6 *)prim)->sin6_addr;
3986 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
3987 /* sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);*/
3988 in6_recoverscope(sin6, &sin6->sin6_addr, NULL); /* skip ifp check */
3989 else
3990 sin6->sin6_scope_id = 0; /*XXX*/
3991 #if defined(__FreeBSD__) || defined (__APPLE__) || defined(__DragonFly__)
3992 *addr= (struct sockaddr *)sin6;
3993 #else
3994 nam->m_len = sizeof(*sin6);
3995 #endif
3997 /* Wake any delayed sleep action */
3998 SCTP_TCB_UNLOCK(stcb);
3999 SCTP_INP_WLOCK(inp);
4000 if (inp->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE) {
4001 inp->sctp_flags &= ~SCTP_PCB_FLAGS_DONT_WAKE;
4002 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT) {
4003 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEOUTPUT;
4004 #if defined(__NetBSD__)
4005 if (sowritable(inp->sctp_socket))
4006 sowwakeup(inp->sctp_socket);
4007 #else
4008 if (sowriteable(inp->sctp_socket))
4009 sowwakeup(inp->sctp_socket);
4010 #endif
4012 if (inp->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT) {
4013 inp->sctp_flags &= ~SCTP_PCB_FLAGS_WAKEINPUT;
4014 if (soreadable(inp->sctp_socket))
4015 sorwakeup(inp->sctp_socket);
4019 SCTP_INP_WUNLOCK(inp);
4020 error = 0;
4021 out:
4022 lwkt_replymsg(&msg->lmsg, error);
4025 static
4026 void
4027 sctp_ingetaddr(netmsg_t msg)
4029 int error;
4031 error = sctp_ingetaddr_oncpu(msg->sockaddr.base.nm_so,
4032 msg->sockaddr.nm_nam);
4033 lwkt_replymsg(&msg->lmsg, error);
4037 sctp_ingetaddr_oncpu(struct socket *so, struct sockaddr **addr)
4039 struct sockaddr_in *sin;
4040 struct sctp_inpcb *inp;
4042 * Do the malloc first in case it blocks.
4044 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4045 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
4046 #else
4047 nam->m_len = sizeof(*sin);
4048 memset(sin, 0, sizeof(*sin));
4049 #endif
4050 sin->sin_family = AF_INET;
4051 sin->sin_len = sizeof(*sin);
4052 inp = (struct sctp_inpcb *)so->so_pcb;
4053 if (!inp) {
4054 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4055 kfree(sin, M_SONAME);
4056 #endif
4057 return ECONNRESET;
4059 SCTP_INP_RLOCK(inp);
4060 sin->sin_port = inp->sctp_lport;
4061 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
4062 if (inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) {
4063 struct sctp_tcb *stcb;
4064 struct sockaddr_in *sin_a;
4065 struct sctp_nets *net;
4066 int fnd;
4068 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4069 if (stcb == NULL) {
4070 goto notConn;
4072 fnd = 0;
4073 sin_a = NULL;
4074 SCTP_TCB_LOCK(stcb);
4075 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4076 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4077 if (sin_a->sin_family == AF_INET) {
4078 fnd = 1;
4079 break;
4082 if ((!fnd) || (sin_a == NULL)) {
4083 /* punt */
4084 SCTP_TCB_UNLOCK(stcb);
4085 goto notConn;
4087 sin->sin_addr = sctp_ipv4_source_address_selection(inp,
4088 stcb, (struct route *)&net->ro, net, 0);
4089 SCTP_TCB_UNLOCK(stcb);
4090 } else {
4091 /* For the bound all case you get back 0 */
4092 notConn:
4093 sin->sin_addr.s_addr = 0;
4096 } else {
4097 /* Take the first IPv4 address in the list */
4098 struct sctp_laddr *laddr;
4099 int fnd = 0;
4100 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
4101 if (laddr->ifa->ifa_addr->sa_family == AF_INET) {
4102 struct sockaddr_in *sin_a;
4103 sin_a = (struct sockaddr_in *)laddr->ifa->ifa_addr;
4104 sin->sin_addr = sin_a->sin_addr;
4105 fnd = 1;
4106 break;
4109 if (!fnd) {
4110 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4111 kfree(sin, M_SONAME);
4112 #endif
4113 SCTP_INP_RUNLOCK(inp);
4114 return ENOENT;
4117 SCTP_INP_RUNLOCK(inp);
4118 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4119 (*addr) = (struct sockaddr *)sin;
4120 #endif
4121 return (0);
4124 void
4125 sctp_peeraddr(netmsg_t msg)
4127 int error;
4129 error = sctp_peeraddr_oncpu(msg->peeraddr.base.nm_so,
4130 msg->peeraddr.nm_nam);
4131 lwkt_replymsg(&msg->lmsg, error);
4135 sctp_peeraddr_oncpu(struct socket *so, struct sockaddr **addr)
4137 struct sockaddr_in *sin = (struct sockaddr_in *)*addr;
4138 struct sockaddr_in *sin_a;
4139 struct sctp_inpcb *inp;
4140 struct sctp_tcb *stcb;
4141 struct sctp_nets *net;
4142 int fnd;
4143 int error;
4145 /* Do the malloc first in case it blocks. */
4146 inp = (struct sctp_inpcb *)so->so_pcb;
4147 if ((inp == NULL) ||
4148 ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0)) {
4149 /* UDP type and listeners will drop out here */
4150 error = ENOTCONN;
4151 goto out;
4154 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4155 /* XXX huh? why assign it above and then allocate it here? */
4156 sin = kmalloc(sizeof *sin, M_SONAME, M_WAITOK | M_ZERO);
4157 #else
4158 nam->m_len = sizeof(*sin);
4159 memset(sin, 0, sizeof(*sin));
4160 #endif
4161 sin->sin_family = AF_INET;
4162 sin->sin_len = sizeof(*sin);
4164 /* We must recapture incase we blocked */
4165 inp = (struct sctp_inpcb *)so->so_pcb;
4166 if (!inp) {
4167 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4168 kfree(sin, M_SONAME);
4169 #endif
4170 error = ECONNRESET;
4171 goto out;
4173 SCTP_INP_RLOCK(inp);
4174 stcb = LIST_FIRST(&inp->sctp_asoc_list);
4175 if (stcb)
4176 SCTP_TCB_LOCK(stcb);
4177 SCTP_INP_RUNLOCK(inp);
4178 if (stcb == NULL) {
4179 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4180 kfree(sin, M_SONAME);
4181 #endif
4182 error = ECONNRESET;
4183 goto out;
4185 fnd = 0;
4186 TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
4187 sin_a = (struct sockaddr_in *)&net->ro._l_addr;
4188 if (sin_a->sin_family == AF_INET) {
4189 fnd = 1;
4190 sin->sin_port = stcb->rport;
4191 sin->sin_addr = sin_a->sin_addr;
4192 break;
4195 SCTP_TCB_UNLOCK(stcb);
4196 if (!fnd) {
4197 /* No IPv4 address */
4198 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4199 kfree(sin, M_SONAME);
4200 #endif
4201 error = ENOENT;
4202 } else {
4203 error = 0;
4205 out:
4206 return error;
4209 #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__DragonFly__)
4210 struct pr_usrreqs sctp_usrreqs = {
4211 .pru_abort = sctp_abort,
4212 .pru_accept = sctp_accept,
4213 .pru_attach = sctp_attach,
4214 .pru_bind = sctp_bind,
4215 .pru_connect = sctp_connect,
4216 .pru_connect2 = pr_generic_notsupp,
4217 .pru_control = in_control_dispatch,
4218 .pru_detach = sctp_detach,
4219 .pru_disconnect = sctp_disconnect,
4220 .pru_listen = sctp_listen,
4221 .pru_peeraddr = sctp_peeraddr,
4222 .pru_rcvd = sctp_usr_recvd,
4223 .pru_rcvoob = pr_generic_notsupp,
4224 .pru_send = sctp_send,
4225 .pru_sense = pru_sense_null,
4226 .pru_shutdown = sctp_shutdown,
4227 .pru_sockaddr = sctp_ingetaddr,
4228 .pru_sosend = sctp_sosend,
4229 .pru_soreceive = soreceive
4232 #else
4233 #if defined(__NetBSD__)
4235 sctp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
4236 struct mbuf *control, struct proc *p)
4238 #else
4239 #error x
4241 sctp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
4242 struct mbuf *control)
4244 struct proc *p = curproc;
4245 #endif
4246 int error = 0;
4247 int family;
4249 family = so->so_proto->pr_domain->dom_family;
4251 if (req == PRU_CONTROL) {
4252 switch (family) {
4253 case PF_INET:
4254 error = in_control(so, (long)m, (caddr_t)nam,
4255 (struct ifnet *)control
4256 #if defined(__NetBSD__)
4258 #endif
4260 break;
4261 #ifdef INET6
4262 case PF_INET6:
4263 error = in6_control(so, (long)m, (caddr_t)nam,
4264 (struct ifnet *)control, p);
4265 break;
4266 #endif
4267 default:
4268 error = EAFNOSUPPORT;
4270 return (error);
4272 #ifdef __NetBSD__
4273 if (req == PRU_PURGEIF) {
4274 struct ifnet *ifn;
4275 struct ifaddr *ifa;
4276 ifn = (struct ifnet *)control;
4277 TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list) {
4278 if (ifa->ifa_addr->sa_family == family) {
4279 sctp_delete_ip_address(ifa);
4282 switch (family) {
4283 case PF_INET:
4284 in_purgeif (ifn);
4285 break;
4286 #ifdef INET6
4287 case PF_INET6:
4288 in6_purgeif (ifn);
4289 break;
4290 #endif /* INET6 */
4291 default:
4292 return (EAFNOSUPPORT);
4294 return (0);
4296 #endif
4297 switch (req) {
4298 case PRU_ATTACH:
4299 error = sctp_attach(so, family, p);
4300 break;
4301 case PRU_DETACH:
4302 error = sctp_detach(so);
4303 break;
4304 case PRU_BIND:
4305 if (nam == NULL) {
4306 return (EINVAL);
4308 error = sctp_bind(so, nam, p);
4309 break;
4310 case PRU_LISTEN:
4311 error = sctp_listen(so, p);
4312 break;
4313 case PRU_CONNECT:
4314 if (nam == NULL) {
4315 return (EINVAL);
4317 error = sctp_connect(so, nam, p);
4318 break;
4319 case PRU_DISCONNECT:
4320 error = sctp_disconnect(so);
4321 break;
4322 case PRU_ACCEPT:
4323 if (nam == NULL) {
4324 return (EINVAL);
4326 error = sctp_accept(so, nam);
4327 break;
4328 case PRU_SHUTDOWN:
4329 error = sctp_shutdown(so);
4330 break;
4332 case PRU_RCVD:
4334 * For Open and Net BSD, this is real
4335 * ugly. The mbuf *nam that is passed
4336 * (by soreceive()) is the int flags c
4337 * ast as a (mbuf *) yuck!
4339 error = sctp_usr_recvd(so, (int)((long)nam));
4340 break;
4342 case PRU_SEND:
4343 /* Flags are ignored */
4344 #ifdef SCTP_DEBUG
4345 if (sctp_debug_on & SCTP_DEBUG_USRREQ1) {
4346 kprintf("Send called on V4 side\n");
4348 #endif
4350 struct sockaddr *addr;
4351 if (nam == NULL)
4352 addr = NULL;
4353 else
4354 addr = mtod(nam, struct sockaddr *);
4356 error = sctp_send(so, 0, m, addr, control, p);
4358 break;
4359 case PRU_ABORT:
4360 error = sctp_abort(so);
4361 break;
4363 case PRU_SENSE:
4364 error = 0;
4365 break;
4366 case PRU_RCVOOB:
4367 error = EAFNOSUPPORT;
4368 break;
4369 case PRU_SENDOOB:
4370 error = EAFNOSUPPORT;
4371 break;
4372 case PRU_PEERADDR:
4373 error = sctp_peeraddr(so, nam);
4374 break;
4375 case PRU_SOCKADDR:
4376 error = sctp_ingetaddr(so, nam);
4377 break;
4378 case PRU_SLOWTIMO:
4379 error = 0;
4380 break;
4381 default:
4382 break;
4384 return (error);
4386 #endif
4388 /* #if defined(__NetBSD__) || defined(__OpenBSD__) */
4389 #if __OpenBSD__
4391 * Sysctl for sctp variables.
4394 sctp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
4395 size_t newlen)
4398 /* All sysctl names at this level are terminal. */
4399 if (namelen != 1)
4400 return (ENOTDIR);
4401 sysctl_int();
4403 switch (name[0]) {
4404 case SCTPCTL_MAXDGRAM:
4405 return (sysctl_int(oldp, oldlenp, newp, newlen,
4406 &sctp_sendspace));
4407 case SCTPCTL_RECVSPACE:
4408 return (sysctl_int(oldp, oldlenp, newp, newlen,
4409 &sctp_recvspace));
4410 case SCTPCTL_AUTOASCONF:
4411 return (sysctl_int(oldp, oldlenp, newp, newlen,
4412 &sctp_auto_asconf));
4413 case SCTPCTL_ECN_ENABLE:
4414 return (sysctl_int(oldp, oldlenp, newp, newlen,
4415 &sctp_ecn));
4416 case SCTPCTL_ECN_NONCE:
4417 return (sysctl_int(oldp, oldlenp, newp, newlen,
4418 &sctp_ecn_nonce));
4419 case SCTPCTL_STRICT_SACK:
4420 return (sysctl_int(oldp, oldlenp, newp, newlen,
4421 &sctp_strict_sacks));
4422 case SCTPCTL_NOCSUM_LO:
4423 return (sysctl_int(oldp, oldlenp, newp, newlen,
4424 &sctp_no_csum_on_loopback));
4425 case SCTPCTL_STRICT_INIT:
4426 return (sysctl_int(oldp, oldlenp, newp, newlen,
4427 &sctp_strict_init));
4428 case SCTPCTL_PEER_CHK_OH:
4429 return (sysctl_int(oldp, oldlenp, newp, newlen,
4430 &sctp_peer_chunk_oh));
4431 case SCTPCTL_MAXBURST:
4432 return (sysctl_int(oldp, oldlenp, newp, newlen,
4433 &sctp_max_burst_default));
4434 case SCTPCTL_MAXCHUNKONQ:
4435 return (sysctl_int(oldp, oldlenp, newp, newlen,
4436 &sctp_max_chunks_on_queue));
4437 case SCTPCTL_DELAYED_SACK:
4438 return (sysctl_int(oldp, oldlenp, newp, newlen,
4439 &sctp_delayed_sack_time_default));
4440 case SCTPCTL_HB_INTERVAL:
4441 return (sysctl_int(oldp, oldlenp, newp, newlen,
4442 &sctp_heartbeat_interval_default));
4443 case SCTPCTL_PMTU_RAISE:
4444 return (sysctl_int(oldp, oldlenp, newp, newlen,
4445 &sctp_pmtu_raise_time_default));
4446 case SCTPCTL_SHUTDOWN_GUARD:
4447 return (sysctl_int(oldp, oldlenp, newp, newlen,
4448 &sctp_shutdown_guard_time_default));
4449 case SCTPCTL_SECRET_LIFETIME:
4450 return (sysctl_int(oldp, oldlenp, newp, newlen,
4451 &sctp_secret_lifetime_default));
4452 case SCTPCTL_RTO_MAX:
4453 return (sysctl_int(oldp, oldlenp, newp, newlen,
4454 &sctp_rto_max_default));
4455 case SCTPCTL_RTO_MIN:
4456 return (sysctl_int(oldp, oldlenp, newp, newlen,
4457 &sctp_rto_min_default));
4458 case SCTPCTL_RTO_INITIAL:
4459 return (sysctl_int(oldp, oldlenp, newp, newlen,
4460 &sctp_rto_initial_default));
4461 case SCTPCTL_INIT_RTO_MAX:
4462 return (sysctl_int(oldp, oldlenp, newp, newlen,
4463 &sctp_init_rto_max_default));
4464 case SCTPCTL_COOKIE_LIFE:
4465 return (sysctl_int(oldp, oldlenp, newp, newlen,
4466 &sctp_valid_cookie_life_default));
4467 case SCTPCTL_INIT_RTX_MAX:
4468 return (sysctl_int(oldp, oldlenp, newp, newlen,
4469 &sctp_init_rtx_max_default));
4470 case SCTPCTL_ASSOC_RTX_MAX:
4471 return (sysctl_int(oldp, oldlenp, newp, newlen,
4472 &sctp_assoc_rtx_max_default));
4473 case SCTPCTL_PATH_RTX_MAX:
4474 return (sysctl_int(oldp, oldlenp, newp, newlen,
4475 &sctp_path_rtx_max_default));
4476 case SCTPCTL_NR_OUTGOING_STREAMS:
4477 return (sysctl_int(oldp, oldlenp, newp, newlen,
4478 &sctp_nr_outgoing_streams_default));
4479 #ifdef SCTP_DEBUG
4480 case SCTPCTL_DEBUG:
4481 return (sysctl_int(oldp, oldlenp, newp, newlen,
4482 &sctp_debug_on));
4483 #endif
4484 default:
4485 return (ENOPROTOOPT);
4487 /* NOTREACHED */
4489 #endif
4490 #if __NetBSD__
4492 * Sysctl for sctp variables.
4494 SYSCTL_SETUP(sysctl_net_inet_sctp_setup, "sysctl net.inet.sctp subtree setup")
4497 sysctl_createv(clog, 0, NULL, NULL,
4498 CTLFLAG_PERMANENT,
4499 CTLTYPE_NODE, "net", NULL,
4500 NULL, 0, NULL, 0,
4501 CTL_NET, CTL_EOL);
4502 sysctl_createv(clog, 0, NULL, NULL,
4503 CTLFLAG_PERMANENT,
4504 CTLTYPE_NODE, "inet", NULL,
4505 NULL, 0, NULL, 0,
4506 CTL_NET, PF_INET, CTL_EOL);
4507 sysctl_createv(clog, 0, NULL, NULL,
4508 CTLFLAG_PERMANENT,
4509 CTLTYPE_NODE, "sctp",
4510 SYSCTL_DESCR("sctp related settings"),
4511 NULL, 0, NULL, 0,
4512 CTL_NET, PF_INET, IPPROTO_SCTP, CTL_EOL);
4514 sysctl_createv(clog, 0, NULL, NULL,
4515 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4516 CTLTYPE_INT, "maxdgram",
4517 SYSCTL_DESCR("Maximum outgoing SCTP buffer size"),
4518 NULL, 0, &sctp_sendspace, 0,
4519 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXDGRAM,
4520 CTL_EOL);
4522 sysctl_createv(clog, 0, NULL, NULL,
4523 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4524 CTLTYPE_INT, "recvspace",
4525 SYSCTL_DESCR("Maximum incoming SCTP buffer size"),
4526 NULL, 0, &sctp_recvspace, 0,
4527 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_RECVSPACE,
4528 CTL_EOL);
4530 sysctl_createv(clog, 0, NULL, NULL,
4531 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4532 CTLTYPE_INT, "autoasconf",
4533 SYSCTL_DESCR("Enable SCTP Auto-ASCONF"),
4534 NULL, 0, &sctp_auto_asconf, 0,
4535 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_AUTOASCONF,
4536 CTL_EOL);
4538 sysctl_createv(clog, 0, NULL, NULL,
4539 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4540 CTLTYPE_INT, "ecn_enable",
4541 SYSCTL_DESCR("Enable SCTP ECN"),
4542 NULL, 0, &sctp_ecn, 0,
4543 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_ENABLE,
4544 CTL_EOL);
4546 sysctl_createv(clog, 0, NULL, NULL,
4547 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4548 CTLTYPE_INT, "ecn_nonce",
4549 SYSCTL_DESCR("Enable SCTP ECN Nonce"),
4550 NULL, 0, &sctp_ecn_nonce, 0,
4551 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_ECN_NONCE,
4552 CTL_EOL);
4554 sysctl_createv(clog, 0, NULL, NULL,
4555 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4556 CTLTYPE_INT, "strict_sack",
4557 SYSCTL_DESCR("Enable SCTP Strict SACK checking"),
4558 NULL, 0, &sctp_strict_sacks, 0,
4559 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_SACK,
4560 CTL_EOL);
4562 sysctl_createv(clog, 0, NULL, NULL,
4563 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4564 CTLTYPE_INT, "loopback_nocsum",
4565 SYSCTL_DESCR("Enable NO Csum on packets sent on loopback"),
4566 NULL, 0, &sctp_no_csum_on_loopback, 0,
4567 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_NOCSUM_LO,
4568 CTL_EOL);
4570 sysctl_createv(clog, 0, NULL, NULL,
4571 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4572 CTLTYPE_INT, "strict_init",
4573 SYSCTL_DESCR("Enable strict INIT/INIT-ACK singleton enforcement"),
4574 NULL, 0, &sctp_strict_init, 0,
4575 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_STRICT_INIT,
4576 CTL_EOL);
4578 sysctl_createv(clog, 0, NULL, NULL,
4579 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4580 CTLTYPE_INT, "peer_chkoh",
4581 SYSCTL_DESCR("Amount to debit peers rwnd per chunk sent"),
4582 NULL, 0, &sctp_peer_chunk_oh, 0,
4583 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_PEER_CHK_OH,
4584 CTL_EOL);
4586 sysctl_createv(clog, 0, NULL, NULL,
4587 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4588 CTLTYPE_INT, "maxburst",
4589 SYSCTL_DESCR("Default max burst for sctp endpoints"),
4590 NULL, 0, &sctp_max_burst_default, 0,
4591 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXBURST,
4592 CTL_EOL);
4594 sysctl_createv(clog, 0, NULL, NULL,
4595 CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
4596 CTLTYPE_INT, "maxchunks",
4597 SYSCTL_DESCR("Default max chunks on queue per asoc"),
4598 NULL, 0, &sctp_max_chunks_on_queue, 0,
4599 CTL_NET, PF_INET, IPPROTO_SCTP, SCTPCTL_MAXCHUNKONQ,
4600 CTL_EOL);
4603 #endif