2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2004 The FreeBSD Foundation
5 * Copyright (c) 2004-2008 Robert N. M. Watson
6 * Copyright (c) 2009-2010 Brad Penoff
7 * Copyright (c) 2009-2010 Humaira Kamal
8 * Copyright (c) 2011-2012 Irene Ruengeler
9 * Copyright (c) 2011-2012 Michael Tuexen
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <netinet/sctp_os.h>
36 #include <netinet/sctp_pcb.h>
37 #include <netinet/sctputil.h>
38 #include <netinet/sctp_var.h>
39 #include <netinet/sctp_sysctl.h>
40 #include <netinet/sctp_input.h>
41 #include <netinet/sctp_peeloff.h>
43 #include <netinet6/sctp6_var.h>
45 #if defined(__Userspace_os_Linux)
46 #define __FAVOR_BSD /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */
48 #if !defined (__Userspace_os_Windows)
49 #if defined INET || defined INET6
50 #include <netinet/udp.h>
52 #include <arpa/inet.h>
54 #include <user_socketvar.h>
56 userland_mutex_t accept_mtx;
57 userland_cond_t accept_cond;
60 #include <sys/timeb.h>
63 MALLOC_DEFINE(M_PCB, "sctp_pcb", "sctp pcb");
64 MALLOC_DEFINE(M_SONAME, "sctp_soname", "sctp soname");
65 #define MAXLEN_MBUF_CHAIN 32
68 extern int sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
69 struct mbuf *top, struct mbuf *control, int flags,
70 /* proc is a dummy in __Userspace__ and will not be passed to sctp_lower_sosend */ struct proc *p);
72 extern int sctp_attach(struct socket *so, int proto, uint32_t vrf_id);
73 extern int sctpconn_attach(struct socket *so, int proto, uint32_t vrf_id);
76 usrsctp_init(uint16_t port,
77 int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df),
78 void (*debug_printf)(const char *format, ...))
80 sctp_init(port, conn_output, debug_printf);
84 /* Taken from usr/src/sys/kern/uipc_sockbuf.c and modified for __Userspace__*/
86 * Socantsendmore indicates that no more data will be sent on the socket; it
87 * would normally be applied to a socket when the user informs the system
88 * that no more data is to be sent, by the protocol code (in case
89 * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be
90 * received, and will normally be applied to the socket by a protocol when it
91 * detects that the peer will send no more data. Data queued for reading in
92 * the socket may yet be read.
95 void socantrcvmore_locked(struct socket *so)
97 SOCKBUF_LOCK_ASSERT(&so->so_rcv);
98 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
102 void socantrcvmore(struct socket *so)
104 SOCKBUF_LOCK(&so->so_rcv);
105 socantrcvmore_locked(so);
109 socantsendmore_locked(struct socket *so)
111 SOCKBUF_LOCK_ASSERT(&so->so_snd);
112 so->so_snd.sb_state |= SBS_CANTSENDMORE;
113 sowwakeup_locked(so);
117 socantsendmore(struct socket *so)
119 SOCKBUF_LOCK(&so->so_snd);
120 socantsendmore_locked(so);
125 /* Taken from usr/src/sys/kern/uipc_sockbuf.c and called within sctp_lower_sosend.
128 sbwait(struct sockbuf *sb)
130 #if defined(__Userspace__) /* __Userspace__ */
132 SOCKBUF_LOCK_ASSERT(sb);
134 sb->sb_flags |= SB_WAIT;
135 #if defined (__Userspace_os_Windows)
136 if (SleepConditionVariableCS(&(sb->sb_cond), &(sb->sb_mtx), INFINITE))
141 return (pthread_cond_wait(&(sb->sb_cond), &(sb->sb_mtx)));
145 SOCKBUF_LOCK_ASSERT(sb);
147 sb->sb_flags |= SB_WAIT;
148 return (msleep(&sb->sb_cc, &sb->sb_mtx,
149 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
157 /* Taken from /src/sys/kern/uipc_socket.c
158 * and modified for __Userspace__
160 static struct socket *
166 * soalloc() sets of socket layer state for a socket,
167 * called only by socreate() and sonewconn().
169 * sodealloc() tears down socket layer state for a socket,
170 * called only by sofree() and sonewconn().
171 * __Userspace__ TODO : Make sure so is properly deallocated
172 * when tearing down the connection.
175 so = (struct socket *)malloc(sizeof(struct socket));
180 memset(so, 0, sizeof(struct socket));
182 /* __Userspace__ Initializing the socket locks here */
183 SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
184 SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
185 SOCKBUF_COND_INIT(&so->so_snd);
186 SOCKBUF_COND_INIT(&so->so_rcv);
187 SOCK_COND_INIT(so); /* timeo_cond */
189 /* __Userspace__ Any ref counting required here? Will we have any use for aiojobq?
190 What about gencnt and numopensockets?*/
191 TAILQ_INIT(&so->so_aiojobq);
196 sodealloc(struct socket *so)
199 KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
200 KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
202 SOCKBUF_COND_DESTROY(&so->so_snd);
203 SOCKBUF_COND_DESTROY(&so->so_rcv);
205 SOCK_COND_DESTROY(so);
207 SOCKBUF_LOCK_DESTROY(&so->so_snd);
208 SOCKBUF_LOCK_DESTROY(&so->so_rcv);
213 /* Taken from /src/sys/kern/uipc_socket.c
214 * and modified for __Userspace__
217 sofree(struct socket *so)
221 ACCEPT_LOCK_ASSERT();
222 SOCK_LOCK_ASSERT(so);
223 /* SS_NOFDREF unset in accept call. this condition seems irrelevent
224 * for __Userspace__...
226 if (so->so_count != 0 ||
227 (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
234 KASSERT((so->so_qstate & SQ_COMP) != 0 ||
235 (so->so_qstate & SQ_INCOMP) != 0,
236 ("sofree: so_head != NULL, but neither SQ_COMP nor "
238 KASSERT((so->so_qstate & SQ_COMP) == 0 ||
239 (so->so_qstate & SQ_INCOMP) == 0,
240 ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
241 TAILQ_REMOVE(&head->so_incomp, so, so_list);
243 so->so_qstate &= ~SQ_INCOMP;
246 KASSERT((so->so_qstate & SQ_COMP) == 0 &&
247 (so->so_qstate & SQ_INCOMP) == 0,
248 ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
249 so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
250 if (so->so_options & SCTP_SO_ACCEPTCONN) {
251 KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
252 KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
256 sctp_close(so); /* was... sctp_detach(so); */
258 * From this point on, we assume that no other references to this
259 * socket exist anywhere else in the stack. Therefore, no locks need
260 * to be acquired or held.
262 * We used to do a lot of socket buffer and socket locking here, as
263 * well as invoke sorflush() and perform wakeups. The direct call to
264 * dom_dispose() and sbrelease_internal() are an inlining of what was
265 * necessary from sorflush().
267 * Notice that the socket buffer and kqueue state are torn down
268 * before calling pru_detach. This means that protocols shold not
269 * assume they can perform socket wakeups, etc, in their detach code.
276 /* Taken from /src/sys/kern/uipc_socket.c */
283 struct sctp_inpcb *inp;
287 inp = (struct sctp_inpcb *)so->so_pcb;
288 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
289 error = sctp6_abort(so);
292 error = sctp_abort(so);
294 error = EAFNOSUPPORT;
298 error = sctp_abort(so);
300 error = EAFNOSUPPORT;
310 /* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_connect (sctp_usrreq.c).
311 * We use sctp_connect for send_one_init_real in ms1.
314 soisconnecting(struct socket *so)
318 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
319 so->so_state |= SS_ISCONNECTING;
323 /* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_disconnect (sctp_usrreq.c).
324 * TODO Do we use sctp_disconnect?
327 soisdisconnecting(struct socket *so)
331 * Note: This code assumes that SOCK_LOCK(so) and
332 * SOCKBUF_LOCK(&so->so_rcv) are the same.
334 SOCKBUF_LOCK(&so->so_rcv);
335 so->so_state &= ~SS_ISCONNECTING;
336 so->so_state |= SS_ISDISCONNECTING;
337 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
338 sorwakeup_locked(so);
339 SOCKBUF_LOCK(&so->so_snd);
340 so->so_snd.sb_state |= SBS_CANTSENDMORE;
341 sowwakeup_locked(so);
343 /* requires 2 args but this was in orig */
344 /* wakeup(&so->so_timeo); */
348 /* Taken from sys/kern/kern_synch.c and
349 modified for __Userspace__
353 * Make all threads sleeping on the specified identifier runnable.
354 * Associating wakeup with so_timeo identifier and timeo_cond
355 * condition variable. TODO. If we use iterator thread then we need to
356 * modify wakeup so it can distinguish between iterator identifier and
365 #if defined (__Userspace_os_Windows)
366 WakeAllConditionVariable(&(so)->timeo_cond);
368 pthread_cond_broadcast(&(so)->timeo_cond);
375 * Make a thread sleeping on the specified identifier runnable.
376 * May wake more than one thread if a target thread is currently
383 /* __Userspace__ Check: We are using accept_cond for wakeup_one.
384 It seems that wakeup_one is only called within
385 soisconnected() and sonewconn() with ident &head->so_timeo
386 head is so->so_head, which is back pointer to listen socket
387 This seems to indicate that the use of accept_cond is correct
388 since socket where accepts occur is so_head in all
392 #if defined (__Userspace_os_Windows)
393 WakeAllConditionVariable(&accept_cond);
395 pthread_cond_broadcast(&accept_cond);
401 /* Called within sctp_process_cookie_[existing/new] */
403 soisconnected(struct socket *so)
409 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
410 so->so_state |= SS_ISCONNECTED;
412 if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
414 TAILQ_REMOVE(&head->so_incomp, so, so_list);
416 so->so_qstate &= ~SQ_INCOMP;
417 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
419 so->so_qstate |= SQ_COMP;
422 wakeup_one(&head->so_timeo);
427 wakeup(&so->so_timeo, so);
433 /* called within sctp_handle_cookie_echo */
436 sonewconn(struct socket *head, int connstatus)
442 over = (head->so_qlen > 3 * head->so_qlimit / 2);
445 if (regression_sonewconn_earlytest && over)
454 so->so_type = head->so_type;
455 so->so_options = head->so_options &~ SCTP_SO_ACCEPTCONN;
456 so->so_linger = head->so_linger;
457 so->so_state = head->so_state | SS_NOFDREF;
458 so->so_dom = head->so_dom;
461 mac_create_socket_from_socket(head, so);
464 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) {
468 switch (head->so_dom) {
471 if (sctp_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
479 if (sctp6_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
486 if (sctpconn_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) {
496 so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
497 so->so_snd.sb_lowat = head->so_snd.sb_lowat;
498 so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
499 so->so_snd.sb_timeo = head->so_snd.sb_timeo;
500 so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
501 so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
502 so->so_state |= connstatus;
505 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
506 so->so_qstate |= SQ_COMP;
510 * Keep removing sockets from the head until there's room for
511 * us to insert on the tail. In pre-locking revisions, this
512 * was a simple if(), but as we could be racing with other
513 * threads and soabort() requires dropping locks, we must
514 * loop waiting for the condition to be true.
516 while (head->so_incqlen > head->so_qlimit) {
518 sp = TAILQ_FIRST(&head->so_incomp);
519 TAILQ_REMOVE(&head->so_incomp, sp, so_list);
521 sp->so_qstate &= ~SQ_INCOMP;
527 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
528 so->so_qstate |= SQ_INCOMP;
534 wakeup_one(&head->so_timeo);
540 /* From /src/sys/sys/sysproto.h */
541 struct sctp_generic_sendmsg_args {
546 socklen_t tolen; /* was __socklen_t */
547 struct sctp_sndrcvinfo * sinfo;
551 struct sctp_generic_recvmsg_args {
555 struct sockaddr *from;
556 socklen_t *fromlenaddr; /* was __socklen_t */
557 struct sctp_sndrcvinfo *sinfo;
563 Source: /src/sys/gnu/fs/xfs/FreeBSD/xfs_ioctl.c
565 static __inline__ int
566 copy_to_user(void *dst, void *src, int len) {
567 memcpy(dst, src, len);
571 static __inline__ int
572 copy_from_user(void *dst, void *src, int len) {
573 memcpy(dst, src, len);
579 src/sys/dev/lmc/if_lmc.h:
580 src/sys/powerpc/powerpc/copyinout.c
583 # define copyin(u, k, len) copy_from_user(k, u, len)
586 src/sys/powerpc/powerpc/copyinout.c
589 # define copyout(k, u, len) copy_to_user(u, k, len)
592 /* copyiniov definition copied/modified from src/sys/kern/kern_subr.c */
594 copyiniov(struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error)
599 if (iovcnt > UIO_MAXIOV)
601 iovlen = iovcnt * sizeof (struct iovec);
602 *iov = malloc(iovlen); /*, M_IOV, M_WAITOK); */
603 error = copyin(iovp, *iov, iovlen);
605 free(*iov); /*, M_IOV); */
611 /* (__Userspace__) version of uiomove */
613 uiomove(void *cp, int n, struct uio *uio)
619 if ((uio->uio_rw != UIO_READ) &&
620 (uio->uio_rw != UIO_WRITE)) {
624 while (n > 0 && uio->uio_resid) {
635 switch (uio->uio_segflg) {
638 if (uio->uio_rw == UIO_READ)
639 error = copyout(cp, iov->iov_base, cnt);
641 error = copyin(iov->iov_base, cp, cnt);
647 if (uio->uio_rw == UIO_READ)
648 bcopy(cp, iov->iov_base, cnt);
650 bcopy(iov->iov_base, cp, cnt);
653 iov->iov_base = (char *)iov->iov_base + cnt;
655 uio->uio_resid -= cnt;
656 uio->uio_offset += cnt;
657 cp = (char *)cp + cnt;
665 /* Source: src/sys/kern/uipc_syscalls.c */
667 getsockaddr(namp, uaddr, len)
668 struct sockaddr **namp;
675 if (len > SOCK_MAXADDRLEN)
676 return (ENAMETOOLONG);
677 if (len < offsetof(struct sockaddr, sa_data))
679 MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK);
680 error = copyin(uaddr, sa, len);
693 /* Taken from /src/lib/libc/net/sctp_sys_calls.c
694 * and modified for __Userspace__
695 * calling sctp_generic_sendmsg from this function
698 userspace_sctp_sendmsg(struct socket *so,
706 u_int32_t timetolive,
709 struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo;
713 memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo));
714 sinfo->sinfo_ppid = ppid;
715 sinfo->sinfo_flags = flags;
716 sinfo->sinfo_stream = stream_no;
717 sinfo->sinfo_timetolive = timetolive;
718 sinfo->sinfo_context = context;
719 sinfo->sinfo_assoc_id = 0;
722 /* Perform error checks on destination (to) */
723 if (tolen > SOCK_MAXADDRLEN){
724 errno = ENAMETOOLONG;
728 ((to == NULL) || (tolen < (socklen_t)sizeof(struct sockaddr)))) {
732 /* Adding the following as part of defensive programming, in case the application
733 does not do it when preparing the destination address.*/
740 iov[0].iov_base = (caddr_t)data;
741 iov[0].iov_len = len;
745 auio.uio_segflg = UIO_USERSPACE;
746 auio.uio_rw = UIO_WRITE;
747 auio.uio_offset = 0; /* XXX */
748 auio.uio_resid = len;
749 errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, 0, sinfo);
751 return (len - auio.uio_resid);
759 usrsctp_sendv(struct socket *so,
766 unsigned int infotype,
769 struct sctp_sndrcvinfo sinfo;
778 memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
781 case SCTP_SENDV_NOINFO:
782 if ((infolen != 0) || (info != NULL)) {
787 case SCTP_SENDV_SNDINFO:
788 if ((info == NULL) || (infolen != sizeof(struct sctp_sndinfo))) {
792 sinfo.sinfo_stream = ((struct sctp_sndinfo *)info)->snd_sid;
793 sinfo.sinfo_flags = ((struct sctp_sndinfo *)info)->snd_flags;
794 sinfo.sinfo_ppid = ((struct sctp_sndinfo *)info)->snd_ppid;
795 sinfo.sinfo_context = ((struct sctp_sndinfo *)info)->snd_context;
796 sinfo.sinfo_assoc_id = ((struct sctp_sndinfo *)info)->snd_assoc_id;
799 case SCTP_SENDV_PRINFO:
800 if ((info == NULL) || (infolen != sizeof(struct sctp_prinfo))) {
804 sinfo.sinfo_stream = 0;
805 sinfo.sinfo_flags = PR_SCTP_POLICY(((struct sctp_prinfo *)info)->pr_policy);
806 sinfo.sinfo_timetolive = ((struct sctp_prinfo *)info)->pr_value;
809 case SCTP_SENDV_AUTHINFO:
813 if ((info == NULL) || (infolen != sizeof(struct sctp_sendv_spa))) {
817 if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_SNDINFO_VALID) {
818 sinfo.sinfo_stream = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_sid;
819 sinfo.sinfo_flags = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_flags;
820 sinfo.sinfo_ppid = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_ppid;
821 sinfo.sinfo_context = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_context;
822 sinfo.sinfo_assoc_id = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_assoc_id;
824 sinfo.sinfo_flags = 0;
825 sinfo.sinfo_stream = 0;
827 if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_PRINFO_VALID) {
828 sinfo.sinfo_flags |= PR_SCTP_POLICY(((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_policy);
829 sinfo.sinfo_timetolive = ((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_value;
831 if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_AUTHINFO_VALID) {
842 /* Perform error checks on destination (to) */
848 iov[0].iov_base = (caddr_t)data;
849 iov[0].iov_len = len;
853 auio.uio_segflg = UIO_USERSPACE;
854 auio.uio_rw = UIO_WRITE;
855 auio.uio_offset = 0; /* XXX */
856 auio.uio_resid = len;
857 errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, flags, use_sinfo ? &sinfo : NULL);
859 return (len - auio.uio_resid);
867 userspace_sctp_sendmbuf(struct socket *so,
868 struct mbuf* mbufdata,
875 u_int32_t timetolive,
879 struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo;
881 struct iovec iov[1]; */
886 sinfo->sinfo_ppid = ppid;
887 sinfo->sinfo_flags = flags;
888 sinfo->sinfo_stream = stream_no;
889 sinfo->sinfo_timetolive = timetolive;
890 sinfo->sinfo_context = context;
891 sinfo->sinfo_assoc_id = 0;
893 /* Perform error checks on destination (to) */
894 if (tolen > SOCK_MAXADDRLEN){
895 error = (ENAMETOOLONG);
898 if (tolen < (socklen_t)offsetof(struct sockaddr, sa_data)){
902 /* Adding the following as part of defensive programming, in case the application
903 does not do it when preparing the destination address.*/
908 error = sctp_lower_sosend(so, to, NULL/*uio*/,
909 (struct mbuf *)mbufdata, (struct mbuf *)NULL,
912 /* TODO: Needs a condition for non-blocking when error is EWOULDBLOCK */
915 else if(error == EWOULDBLOCK) {
917 retvalsendmsg = (-1);
919 SCTP_PRINTF("%s: error = %d\n", __func__, error);
921 retvalsendmsg = (-1);
923 return retvalsendmsg;
928 /* taken from usr.lib/sctp_sys_calls.c and needed here */
929 #define SCTP_SMALL_IOVEC_SIZE 2
931 /* Taken from /src/lib/libc/net/sctp_sys_calls.c
932 * and modified for __Userspace__
933 * calling sctp_generic_recvmsg from this function
936 userspace_sctp_recvmsg(struct socket *so,
939 struct sockaddr *from,
941 struct sctp_sndrcvinfo *sinfo,
945 struct iovec iov[SCTP_SMALL_IOVEC_SIZE];
952 iov[0].iov_base = dbuf;
953 iov[0].iov_len = len;
956 auio.uio_iovcnt = iovlen;
957 auio.uio_segflg = UIO_USERSPACE;
958 auio.uio_rw = UIO_READ;
959 auio.uio_offset = 0; /* XXX */
962 for (i = 0; i <iovlen; i++, tiov++) {
963 if ((auio.uio_resid += tiov->iov_len) < 0) {
965 SCTP_PRINTF("%s: error = %d\n", __func__, error);
969 ulen = auio.uio_resid;
970 if (fromlenp != NULL) {
975 error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
976 from, fromlen, msg_flags,
977 (struct sctp_sndrcvinfo *)sinfo, 1);
980 if (auio.uio_resid != (int)ulen &&
982 #if !defined(__Userspace_os_NetBSD)
985 error == EWOULDBLOCK)) {
989 if ((fromlenp != NULL) && (fromlen > 0) && (from != NULL)) {
990 switch (from->sa_family) {
993 *fromlenp = sizeof(struct sockaddr_in);
998 *fromlenp = sizeof(struct sockaddr_in6);
1002 *fromlenp = sizeof(struct sockaddr_conn);
1008 if (*fromlenp > fromlen) {
1009 *fromlenp = fromlen;
1013 /* ready return value */
1014 retval = (int)ulen - auio.uio_resid;
1017 SCTP_PRINTF("%s: error = %d\n", __func__, error);
1023 usrsctp_recvv(struct socket *so,
1026 struct sockaddr *from,
1027 socklen_t *fromlenp,
1030 unsigned int *infotype,
1034 struct iovec iov[SCTP_SMALL_IOVEC_SIZE];
1039 struct sctp_rcvinfo *rcv;
1040 struct sctp_recvv_rn *rn;
1041 struct sctp_extrcvinfo seinfo;
1047 iov[0].iov_base = dbuf;
1048 iov[0].iov_len = len;
1051 auio.uio_iovcnt = iovlen;
1052 auio.uio_segflg = UIO_USERSPACE;
1053 auio.uio_rw = UIO_READ;
1054 auio.uio_offset = 0; /* XXX */
1057 for (i = 0; i <iovlen; i++, tiov++) {
1058 if ((auio.uio_resid += tiov->iov_len) < 0) {
1063 ulen = auio.uio_resid;
1064 if (fromlenp != NULL) {
1065 fromlen = *fromlenp;
1069 errno = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
1070 from, fromlen, msg_flags,
1071 (struct sctp_sndrcvinfo *)&seinfo, 1);
1073 if (auio.uio_resid != (int)ulen &&
1075 #if !defined(__Userspace_os_NetBSD)
1076 errno == ERESTART ||
1078 errno == EWOULDBLOCK)) {
1082 if ((*msg_flags & MSG_NOTIFICATION) == 0) {
1083 struct sctp_inpcb *inp;
1085 inp = (struct sctp_inpcb *)so->so_pcb;
1086 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) &&
1087 sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
1088 *infolen >= (socklen_t)sizeof(struct sctp_recvv_rn) &&
1089 seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL) {
1090 rn = (struct sctp_recvv_rn *)info;
1091 rn->recvv_rcvinfo.rcv_sid = seinfo.sinfo_stream;
1092 rn->recvv_rcvinfo.rcv_ssn = seinfo.sinfo_ssn;
1093 rn->recvv_rcvinfo.rcv_flags = seinfo.sinfo_flags;
1094 rn->recvv_rcvinfo.rcv_ppid = seinfo.sinfo_ppid;
1095 rn->recvv_rcvinfo.rcv_context = seinfo.sinfo_context;
1096 rn->recvv_rcvinfo.rcv_tsn = seinfo.sinfo_tsn;
1097 rn->recvv_rcvinfo.rcv_cumtsn = seinfo.sinfo_cumtsn;
1098 rn->recvv_rcvinfo.rcv_assoc_id = seinfo.sinfo_assoc_id;
1099 rn->recvv_nxtinfo.nxt_sid = seinfo.sreinfo_next_stream;
1100 rn->recvv_nxtinfo.nxt_flags = 0;
1101 if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) {
1102 rn->recvv_nxtinfo.nxt_flags |= SCTP_UNORDERED;
1104 if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) {
1105 rn->recvv_nxtinfo.nxt_flags |= SCTP_NOTIFICATION;
1107 if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) {
1108 rn->recvv_nxtinfo.nxt_flags |= SCTP_COMPLETE;
1110 rn->recvv_nxtinfo.nxt_ppid = seinfo.sreinfo_next_ppid;
1111 rn->recvv_nxtinfo.nxt_length = seinfo.sreinfo_next_length;
1112 rn->recvv_nxtinfo.nxt_assoc_id = seinfo.sreinfo_next_aid;
1113 *infolen = (socklen_t)sizeof(struct sctp_recvv_rn);
1114 *infotype = SCTP_RECVV_RN;
1115 } else if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
1116 *infolen >= (socklen_t)sizeof(struct sctp_rcvinfo)) {
1117 rcv = (struct sctp_rcvinfo *)info;
1118 rcv->rcv_sid = seinfo.sinfo_stream;
1119 rcv->rcv_ssn = seinfo.sinfo_ssn;
1120 rcv->rcv_flags = seinfo.sinfo_flags;
1121 rcv->rcv_ppid = seinfo.sinfo_ppid;
1122 rcv->rcv_context = seinfo.sinfo_context;
1123 rcv->rcv_tsn = seinfo.sinfo_tsn;
1124 rcv->rcv_cumtsn = seinfo.sinfo_cumtsn;
1125 rcv->rcv_assoc_id = seinfo.sinfo_assoc_id;
1126 *infolen = (socklen_t)sizeof(struct sctp_rcvinfo);
1127 *infotype = SCTP_RECVV_RCVINFO;
1129 *infotype = SCTP_RECVV_NOINFO;
1133 if ((fromlenp != NULL) && (fromlen > 0) && (from != NULL)) {
1134 switch (from->sa_family) {
1137 *fromlenp = sizeof(struct sockaddr_in);
1142 *fromlenp = sizeof(struct sockaddr_in6);
1146 *fromlenp = sizeof(struct sockaddr_conn);
1152 if (*fromlenp > fromlen) {
1153 *fromlenp = fromlen;
1157 /* ready return value */
1158 return ((int)ulen - auio.uio_resid);
1167 #if defined(__Userspace__)
1168 /* Taken from /src/sys/kern/uipc_socket.c
1169 * and modified for __Userspace__
1170 * socreate returns a socket. The socket should be
1171 * closed with soclose().
1174 socreate(int dom, struct socket **aso, int type, int proto)
1179 if ((dom != AF_CONN) && (dom != AF_INET) && (dom != AF_INET6)) {
1182 if ((type != SOCK_STREAM) && (type != SOCK_SEQPACKET)) {
1185 if (proto != IPPROTO_SCTP) {
1195 * so_incomp represents a queue of connections that
1196 * must be completed at protocol level before being
1197 * returned. so_comp field heads a list of sockets
1198 * that are ready to be returned to the listening process
1199 *__Userspace__ These queues are being used at a number of places like accept etc.
1201 TAILQ_INIT(&so->so_incomp);
1202 TAILQ_INIT(&so->so_comp);
1207 * Auto-sizing of socket buffers is managed by the protocols and
1208 * the appropriate flags must be set in the pru_attach function.
1209 * For __Userspace__ The pru_attach function in this case is sctp_attach.
1214 error = sctp_attach(so, proto, SCTP_DEFAULT_VRFID);
1219 error = sctp6_attach(so, proto, SCTP_DEFAULT_VRFID);
1223 error = sctpconn_attach(so, proto, SCTP_DEFAULT_VRFID);
1226 error = EAFNOSUPPORT;
1230 KASSERT(so->so_count == 1, ("socreate: so_count %d", so->so_count));
1239 /* The kernel version for reference is below. The #else
1240 should be removed once the __Userspace__
1242 * socreate returns a socket with a ref count of 1. The socket should be
1243 * closed with soclose().
1246 socreate(int dom, struct socket **aso, int type, int proto,
1247 struct ucred *cred, struct thread *td)
1249 struct protosw *prp;
1254 prp = pffindproto(dom, proto, type);
1256 prp = pffindtype(dom, type);
1258 if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
1259 prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
1260 return (EPROTONOSUPPORT);
1262 if (jailed(cred) && jail_socket_unixiproute_only &&
1263 prp->pr_domain->dom_family != PF_LOCAL &&
1264 prp->pr_domain->dom_family != PF_INET &&
1265 prp->pr_domain->dom_family != PF_ROUTE) {
1266 return (EPROTONOSUPPORT);
1269 if (prp->pr_type != type)
1270 return (EPROTOTYPE);
1275 TAILQ_INIT(&so->so_incomp);
1276 TAILQ_INIT(&so->so_comp);
1278 so->so_cred = crhold(cred);
1281 mac_create_socket(cred, so);
1283 knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv),
1285 knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd),
1289 * Auto-sizing of socket buffers is managed by the protocols and
1290 * the appropriate flags must be set in the pru_attach function.
1292 error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
1294 KASSERT(so->so_count == 1, ("socreate: so_count %d",
1308 /* Taken from /src/sys/kern/uipc_syscalls.c
1309 * and modified for __Userspace__
1310 * Removing struct thread td.
1313 userspace_socket(int domain, int type, int protocol)
1315 struct socket *so = NULL;
1317 errno = socreate(domain, &so, type, protocol);
1322 * The original socket call returns the file descriptor fd.
1323 * td->td_retval[0] = fd.
1324 * We are returning struct socket *so.
1330 usrsctp_socket(int domain, int type, int protocol,
1331 int (*receive_cb)(struct socket *sock, union sctp_sockstore addr, void *data,
1332 size_t datalen, struct sctp_rcvinfo, int flags, void *ulp_info),
1333 int (*send_cb)(struct socket *sock, uint32_t sb_free),
1334 uint32_t sb_threshold,
1339 if ((protocol = IPPROTO_SCTP) && (SCTP_BASE_VAR(sctp_pcb_initialized) == 0)) {
1340 errno = EPROTONOSUPPORT;
1343 if ((receive_cb == NULL) &&
1344 ((send_cb != NULL) || (sb_threshold != 0) || (ulp_info != NULL))) {
1348 if ((domain == AF_CONN) && (SCTP_BASE_VAR(conn_output) == NULL)) {
1349 errno = EAFNOSUPPORT;
1352 errno = socreate(domain, &so, type, protocol);
1357 * The original socket call returns the file descriptor fd.
1358 * td->td_retval[0] = fd.
1359 * We are returning struct socket *so.
1361 register_recv_cb(so, receive_cb);
1362 register_send_cb(so, sb_threshold, send_cb);
1363 register_ulp_info(so, ulp_info);
1368 u_long sb_max = SB_MAX;
1370 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
1372 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
1375 * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't
1376 * become limiting if buffering efficiency is near the normal case.
1379 sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so)
1381 SOCKBUF_LOCK_ASSERT(sb);
1382 sb->sb_mbmax = (u_int)min(cc * sb_efficiency, sb_max);
1383 sb->sb_hiwat = (u_int)cc;
1384 if (sb->sb_lowat > (int)sb->sb_hiwat)
1385 sb->sb_lowat = (int)sb->sb_hiwat;
1390 sbreserve(struct sockbuf *sb, u_long cc, struct socket *so)
1395 error = sbreserve_locked(sb, cc, so);
1400 #if defined(__Userspace__)
1402 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
1404 SOCKBUF_LOCK(&so->so_snd);
1405 SOCKBUF_LOCK(&so->so_rcv);
1406 so->so_snd.sb_hiwat = (uint32_t)sndcc;
1407 so->so_rcv.sb_hiwat = (uint32_t)rcvcc;
1409 if (sbreserve_locked(&so->so_snd, sndcc, so) == 0) {
1412 if (sbreserve_locked(&so->so_rcv, rcvcc, so) == 0) {
1415 if (so->so_rcv.sb_lowat == 0)
1416 so->so_rcv.sb_lowat = 1;
1417 if (so->so_snd.sb_lowat == 0)
1418 so->so_snd.sb_lowat = MCLBYTES;
1419 if (so->so_snd.sb_lowat > (int)so->so_snd.sb_hiwat)
1420 so->so_snd.sb_lowat = (int)so->so_snd.sb_hiwat;
1421 SOCKBUF_UNLOCK(&so->so_rcv);
1422 SOCKBUF_UNLOCK(&so->so_snd);
1426 SOCKBUF_UNLOCK(&so->so_rcv);
1427 SOCKBUF_UNLOCK(&so->so_snd);
1430 #else /* kernel version for reference */
1432 soreserve(struct socket *so, u_long sndcc, u_long rcvcc)
1434 struct thread *td = curthread;
1436 SOCKBUF_LOCK(&so->so_snd);
1437 SOCKBUF_LOCK(&so->so_rcv);
1438 if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0)
1440 if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0)
1442 if (so->so_rcv.sb_lowat == 0)
1443 so->so_rcv.sb_lowat = 1;
1444 if (so->so_snd.sb_lowat == 0)
1445 so->so_snd.sb_lowat = MCLBYTES;
1446 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
1447 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
1448 SOCKBUF_UNLOCK(&so->so_rcv);
1449 SOCKBUF_UNLOCK(&so->so_snd);
1452 sbrelease_locked(&so->so_snd, so);
1454 SOCKBUF_UNLOCK(&so->so_rcv);
1455 SOCKBUF_UNLOCK(&so->so_snd);
1464 /* Taken from /src/sys/kern/uipc_sockbuf.c
1465 * and modified for __Userspace__
1468 #if defined(__Userspace__)
1470 sowakeup(struct socket *so, struct sockbuf *sb)
1473 SOCKBUF_LOCK_ASSERT(sb);
1475 sb->sb_flags &= ~SB_SEL;
1476 if (sb->sb_flags & SB_WAIT) {
1477 sb->sb_flags &= ~SB_WAIT;
1478 #if defined (__Userspace_os_Windows)
1479 WakeAllConditionVariable(&(sb)->sb_cond);
1481 pthread_cond_broadcast(&(sb)->sb_cond);
1485 /*__Userspace__ what todo about so_upcall?*/
1488 #else /* kernel version for reference */
1490 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
1491 * via SIGIO if the socket has the SS_ASYNC flag set.
1493 * Called with the socket buffer lock held; will release the lock by the end
1494 * of the function. This allows the caller to acquire the socket buffer lock
1495 * while testing for the need for various sorts of wakeup and hold it through
1496 * to the point where it's no longer required. We currently hold the lock
1497 * through calls out to other subsystems (with the exception of kqueue), and
1498 * then release it to avoid lock order issues. It's not clear that's
1502 sowakeup(struct socket *so, struct sockbuf *sb)
1505 SOCKBUF_LOCK_ASSERT(sb);
1507 selwakeuppri(&sb->sb_sel, PSOCK);
1508 sb->sb_flags &= ~SB_SEL;
1509 if (sb->sb_flags & SB_WAIT) {
1510 sb->sb_flags &= ~SB_WAIT;
1513 KNOTE_LOCKED(&sb->sb_sel.si_note, 0);
1515 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
1516 pgsigio(&so->so_sigio, SIGIO, 0);
1517 if (sb->sb_flags & SB_UPCALL)
1518 (*so->so_upcall)(so, so->so_upcallarg, M_NOWAIT);
1519 if (sb->sb_flags & SB_AIO)
1521 mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED);
1527 /* Taken from /src/sys/kern/uipc_socket.c
1528 * and modified for __Userspace__
1532 sobind(struct socket *so, struct sockaddr *nam)
1534 switch (nam->sa_family) {
1537 return (sctp_bind(so, nam));
1541 return (sctp6_bind(so, nam, NULL));
1544 return (sctpconn_bind(so, nam));
1546 return EAFNOSUPPORT;
1550 /* Taken from /src/sys/kern/uipc_syscalls.c
1551 * and modified for __Userspace__
1555 usrsctp_bind(struct socket *so, struct sockaddr *name, int namelen)
1557 struct sockaddr *sa;
1563 if ((errno = getsockaddr(&sa, (caddr_t)name, namelen)) != 0)
1566 errno = sobind(so, sa);
1576 userspace_bind(struct socket *so, struct sockaddr *name, int namelen)
1578 return (usrsctp_bind(so, name, namelen));
1581 /* Taken from /src/sys/kern/uipc_socket.c
1582 * and modified for __Userspace__
1586 solisten(struct socket *so, int backlog)
1591 return (sctp_listen(so, backlog, NULL));
1597 solisten_proto_check(struct socket *so)
1600 SOCK_LOCK_ASSERT(so);
1602 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
1603 SS_ISDISCONNECTING))
1608 static int somaxconn = SOMAXCONN;
1611 solisten_proto(struct socket *so, int backlog)
1614 SOCK_LOCK_ASSERT(so);
1616 if (backlog < 0 || backlog > somaxconn)
1617 backlog = somaxconn;
1618 so->so_qlimit = backlog;
1619 so->so_options |= SCTP_SO_ACCEPTCONN;
1625 /* Taken from /src/sys/kern/uipc_syscalls.c
1626 * and modified for __Userspace__
1630 usrsctp_listen(struct socket *so, int backlog)
1632 errno = solisten(so, backlog);
1641 userspace_listen(struct socket *so, int backlog)
1643 return (usrsctp_listen(so, backlog));
1646 /* Taken from /src/sys/kern/uipc_socket.c
1647 * and modified for __Userspace__
1651 soaccept(struct socket *so, struct sockaddr **nam)
1656 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
1657 so->so_state &= ~SS_NOFDREF;
1659 error = sctp_accept(so, nam);
1665 /* Taken from /src/sys/kern/uipc_syscalls.c
1666 * kern_accept modified for __Userspace__
1669 user_accept(struct socket *head, struct sockaddr **name, socklen_t *namelen, struct socket **ptr_accept_ret_sock)
1671 struct sockaddr *sa = NULL;
1673 struct socket *so = NULL;
1680 if ((head->so_options & SCTP_SO_ACCEPTCONN) == 0) {
1686 if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
1688 error = EWOULDBLOCK;
1691 while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
1692 if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
1693 head->so_error = ECONNABORTED;
1696 #if defined (__Userspace_os_Windows)
1697 if (SleepConditionVariableCS(&accept_cond, &accept_mtx, INFINITE))
1700 error = GetLastError();
1702 error = pthread_cond_wait(&accept_cond, &accept_mtx);
1709 if (head->so_error) {
1710 error = head->so_error;
1715 so = TAILQ_FIRST(&head->so_comp);
1716 KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
1717 KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
1720 * Before changing the flags on the socket, we have to bump the
1721 * reference count. Otherwise, if the protocol calls sofree(),
1722 * the socket will be released due to a zero refcount.
1724 SOCK_LOCK(so); /* soref() and so_state update */
1725 soref(so); /* file descriptor reference */
1727 TAILQ_REMOVE(&head->so_comp, so, so_list);
1729 so->so_state |= (head->so_state & SS_NBIO);
1730 so->so_qstate &= ~SQ_COMP;
1737 * The original accept returns fd value via td->td_retval[0] = fd;
1738 * we will return the socket for accepted connection.
1741 error = soaccept(so, &sa);
1744 * return a namelen of zero for older code which might
1745 * ignore the return value from accept.
1758 /* check sa_len before it is destroyed */
1759 if (*namelen > sa->sa_len) {
1760 *namelen = sa->sa_len;
1765 switch (sa->sa_family) {
1768 sa_len = sizeof(struct sockaddr_in);
1773 sa_len = sizeof(struct sockaddr_in6);
1777 sa_len = sizeof(struct sockaddr_conn);
1783 if (*namelen > sa_len) {
1796 *ptr_accept_ret_sock = so;
1802 /* Taken from /src/sys/kern/uipc_syscalls.c
1803 * and modified for __Userspace__
1809 accept1(struct socket *so, struct sockaddr *aname, socklen_t *anamelen, struct socket **ptr_accept_ret_sock)
1811 struct sockaddr *name;
1818 if (aname == NULL) {
1819 return (user_accept(so, NULL, NULL, ptr_accept_ret_sock));
1822 error = copyin(anamelen, &namelen, sizeof (namelen));
1826 error = user_accept(so, &name, &namelen, ptr_accept_ret_sock);
1829 * return a namelen of zero for older code which might
1830 * ignore the return value from accept.
1833 (void) copyout(&namelen,
1834 anamelen, sizeof(*anamelen));
1838 if (error == 0 && name != NULL) {
1839 error = copyout(name, aname, namelen);
1842 error = copyout(&namelen, anamelen, sizeof(namelen));
1846 FREE(name, M_SONAME);
1852 usrsctp_accept(struct socket *so, struct sockaddr *aname, socklen_t *anamelen)
1854 struct socket *accept_return_sock;
1856 errno = accept1(so, aname, anamelen, &accept_return_sock);
1860 return (accept_return_sock);
1865 userspace_accept(struct socket *so, struct sockaddr *aname, socklen_t *anamelen)
1867 return (usrsctp_accept(so, aname, anamelen));
1871 usrsctp_peeloff(struct socket *head, sctp_assoc_t id)
1875 if ((errno = sctp_can_peel_off(head, id)) != 0) {
1878 if ((so = sonewconn(head, SS_ISCONNECTED)) == NULL) {
1884 TAILQ_REMOVE(&head->so_comp, so, so_list);
1886 so->so_state |= (head->so_state & SS_NBIO);
1887 so->so_qstate &= ~SQ_COMP;
1891 if ((errno = sctp_do_peeloff(head, so, id)) != 0) {
1900 sodisconnect(struct socket *so)
1904 if ((so->so_state & SS_ISCONNECTED) == 0)
1906 if (so->so_state & SS_ISDISCONNECTING)
1908 error = sctp_disconnect(so);
1913 usrsctp_set_non_blocking(struct socket *so, int onoff)
1921 so->so_state |= SS_NBIO;
1923 so->so_state &= ~SS_NBIO;
1930 usrsctp_get_non_blocking(struct socket *so)
1939 if (so->so_state | SS_NBIO) {
1949 soconnect(struct socket *so, struct sockaddr *nam)
1953 if (so->so_options & SCTP_SO_ACCEPTCONN)
1954 return (EOPNOTSUPP);
1956 * If protocol is connection-based, can only connect once.
1957 * Otherwise, if connected, try to disconnect first. This allows
1958 * user to disconnect by connecting to, e.g., a null address.
1960 if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && (error = sodisconnect(so))) {
1964 * Prevent accumulated error from previous connection from
1968 switch (nam->sa_family) {
1971 error = sctp_connect(so, nam);
1976 error = sctp6_connect(so, nam);
1980 error = sctpconn_connect(so, nam);
1983 error = EAFNOSUPPORT;
1992 int user_connect(struct socket *so, struct sockaddr *sa)
1995 int interrupted = 0;
2001 if (so->so_state & SS_ISCONNECTING) {
2006 error = soconnect(so, sa);
2010 if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
2011 error = EINPROGRESS;
2016 while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
2017 #if defined (__Userspace_os_Windows)
2018 if (SleepConditionVariableCS(SOCK_COND(so), SOCK_MTX(so), INFINITE))
2023 error = pthread_cond_wait(SOCK_COND(so), SOCK_MTX(so));
2026 #if defined(__Userspace_os_NetBSD)
2027 if (error == EINTR) {
2029 if (error == EINTR || error == ERESTART) {
2037 error = so->so_error;
2044 so->so_state &= ~SS_ISCONNECTING;
2046 #if !defined(__Userspace_os_NetBSD)
2047 if (error == ERESTART) {
2055 int usrsctp_connect(struct socket *so, struct sockaddr *name, int namelen)
2057 struct sockaddr *sa;
2059 errno = getsockaddr(&sa, (caddr_t)name, namelen);
2063 errno = user_connect(so, sa);
2072 int userspace_connect(struct socket *so, struct sockaddr *name, int namelen)
2074 return (usrsctp_connect(so, name, namelen));
2077 #define SCTP_STACK_BUF_SIZE 2048
2080 usrsctp_close(struct socket *so) {
2082 if (so->so_options & SCTP_SO_ACCEPTCONN) {
2086 while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
2087 TAILQ_REMOVE(&so->so_comp, sp, so_list);
2089 sp->so_qstate &= ~SQ_COMP;
2104 userspace_close(struct socket *so)
2110 usrsctp_shutdown(struct socket *so, int how)
2112 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) {
2120 sctp_flush(so, how);
2123 if (how != SHUT_RD) {
2124 errno = sctp_shutdown(so);
2135 userspace_shutdown(struct socket *so, int how)
2137 return (usrsctp_shutdown(so, how));
2141 usrsctp_finish(void)
2143 if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
2146 if (SCTP_INP_INFO_TRYLOCK()) {
2147 if (!LIST_EMPTY(&SCTP_BASE_INFO(listhead))) {
2148 SCTP_INP_INFO_RUNLOCK();
2151 SCTP_INP_INFO_RUNLOCK();
2160 userspace_finish(void)
2162 return (usrsctp_finish());
2165 /* needed from sctp_usrreq.c */
2167 sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize, void *p);
2170 usrsctp_setsockopt(struct socket *so, int level, int option_name,
2171 const void *option_value, socklen_t option_len)
2180 switch (option_name) {
2182 if (option_len < (socklen_t)sizeof(int)) {
2188 buf_size = (int *)option_value;
2189 if (*buf_size < 1) {
2193 sbreserve(&so->so_rcv, (u_long)*buf_size, so);
2198 if (option_len < (socklen_t)sizeof(int)) {
2204 buf_size = (int *)option_value;
2205 if (*buf_size < 1) {
2209 sbreserve(&so->so_snd, (u_long)*buf_size, so);
2214 if (option_len < (socklen_t)sizeof(struct linger)) {
2220 l = (struct linger *)option_value;
2221 so->so_linger = l->l_linger;
2223 so->so_options |= SCTP_SO_LINGER;
2225 so->so_options &= ~SCTP_SO_LINGER;
2235 errno = sctp_setopt(so, option_name, (void *) option_value, (size_t)option_len, NULL);
2242 errno = ENOPROTOOPT;
2248 userspace_setsockopt(struct socket *so, int level, int option_name,
2249 const void *option_value, socklen_t option_len)
2251 return (usrsctp_setsockopt(so, level, option_name, option_value, option_len));
2254 /* needed from sctp_usrreq.c */
2256 sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize,
2260 usrsctp_getsockopt(struct socket *so, int level, int option_name,
2261 void *option_value, socklen_t *option_len)
2267 if (option_len == NULL) {
2273 switch (option_name) {
2275 if (*option_len < (socklen_t)sizeof(int)) {
2281 buf_size = (int *)option_value;
2282 *buf_size = so->so_rcv.sb_hiwat;;
2283 *option_len = (socklen_t)sizeof(int);
2288 if (*option_len < (socklen_t)sizeof(int)) {
2294 buf_size = (int *)option_value;
2295 *buf_size = so->so_snd.sb_hiwat;
2296 *option_len = (socklen_t)sizeof(int);
2301 if (*option_len < (socklen_t)sizeof(struct linger)) {
2307 l = (struct linger *)option_value;
2308 l->l_linger = so->so_linger;
2309 if (so->so_options & SCTP_SO_LINGER) {
2314 *option_len = (socklen_t)sizeof(struct linger);
2325 len = (size_t)*option_len;
2326 errno = sctp_getopt(so, option_name, option_value, &len, NULL);
2327 *option_len = (socklen_t)len;
2335 errno = ENOPROTOOPT;
2341 userspace_getsockopt(struct socket *so, int level, int option_name,
2342 void *option_value, socklen_t *option_len)
2344 return (usrsctp_getsockopt(so, level, option_name, option_value, option_len));
2348 usrsctp_bindx(struct socket *so, struct sockaddr *addrs, int addrcnt, int flags)
2350 struct sctp_getaddresses *gaddrs;
2351 struct sockaddr *sa;
2353 struct sockaddr_in *sin;
2356 struct sockaddr_in6 *sin6;
2360 #if defined(INET) || defined(INET6)
2364 /* validate the flags */
2365 if ((flags != SCTP_BINDX_ADD_ADDR) &&
2366 (flags != SCTP_BINDX_REM_ADDR)) {
2370 /* validate the address count and list */
2371 if ((addrcnt <= 0) || (addrs == NULL)) {
2375 /* First pre-screen the addresses */
2377 for (i = 0; i < addrcnt; i++) {
2378 switch (sa->sa_family) {
2382 if (sa->sa_len != sizeof(struct sockaddr_in)) {
2387 sin = (struct sockaddr_in *)sa;
2388 if (sin->sin_port) {
2389 /* non-zero port, check or save */
2391 /* Check against our port */
2392 if (sport != sin->sin_port) {
2397 /* save off the port */
2398 sport = sin->sin_port;
2402 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
2409 if (sa->sa_len != sizeof(struct sockaddr_in6)) {
2414 sin6 = (struct sockaddr_in6 *)sa;
2415 if (sin6->sin6_port) {
2416 /* non-zero port, check or save */
2418 /* Check against our port */
2419 if (sport != sin6->sin6_port) {
2424 /* save off the port */
2425 sport = sin6->sin6_port;
2429 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
2434 /* Invalid address family specified. */
2435 errno = EAFNOSUPPORT;
2439 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
2442 argsz = sizeof(struct sctp_getaddresses) +
2443 sizeof(struct sockaddr_storage);
2444 if ((gaddrs = (struct sctp_getaddresses *)malloc(argsz)) == NULL) {
2449 for (i = 0; i < addrcnt; i++) {
2453 memset(gaddrs, 0, argsz);
2454 gaddrs->sget_assoc_id = 0;
2456 memcpy(gaddrs->addr, sa, sa->sa_len);
2457 if (usrsctp_setsockopt(so, IPPROTO_SCTP, flags, gaddrs, (socklen_t)argsz) != 0) {
2461 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
2463 switch (sa->sa_family) {
2466 sa_len = sizeof(struct sockaddr_in);
2471 sa_len = sizeof(struct sockaddr_in6);
2478 memcpy(gaddrs->addr, sa, sa_len);
2480 * Now, if there was a port mentioned, assure that the
2481 * first address has that port to make sure it fails or
2482 * succeeds correctly.
2484 #if defined(INET) || defined(INET6)
2485 if ((i == 0) && (sport != 0)) {
2486 switch (gaddrs->addr->sa_family) {
2489 sin = (struct sockaddr_in *)gaddrs->addr;
2490 sin->sin_port = sport;
2495 sin6 = (struct sockaddr_in6 *)gaddrs->addr;
2496 sin6->sin6_port = sport;
2502 if (usrsctp_setsockopt(so, IPPROTO_SCTP, flags, gaddrs, (socklen_t)argsz) != 0) {
2506 sa = (struct sockaddr *)((caddr_t)sa + sa_len);
2514 usrsctp_connectx(struct socket *so,
2515 const struct sockaddr *addrs, int addrcnt,
2518 #if defined(INET) || defined(INET6)
2519 char buf[SCTP_STACK_BUF_SIZE];
2520 int i, ret, cnt, *aa;
2522 const struct sockaddr *at;
2524 size_t len = sizeof(int);
2526 /* validate the address count and list */
2527 if ((addrs == NULL) || (addrcnt <= 0)) {
2533 cpto = ((caddr_t)buf + sizeof(int));
2534 /* validate all the addresses and get the size */
2535 for (i = 0; i < addrcnt; i++) {
2536 switch (at->sa_family) {
2540 if (at->sa_len != sizeof(struct sockaddr_in)) {
2545 memcpy(cpto, at, sizeof(struct sockaddr_in));
2546 cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in));
2547 len += sizeof(struct sockaddr_in);
2548 at = (struct sockaddr *)((caddr_t)at + sizeof(struct sockaddr_in));
2554 if (at->sa_len != sizeof(struct sockaddr_in6)) {
2559 if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) {
2560 in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at);
2561 cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in));
2562 len += sizeof(struct sockaddr_in);
2564 memcpy(cpto, at, sizeof(struct sockaddr_in6));
2565 cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6));
2566 len += sizeof(struct sockaddr_in6);
2568 at = (struct sockaddr *)((caddr_t)at + sizeof(struct sockaddr_in6));
2575 if (len > (sizeof(buf) - sizeof(int))) {
2576 /* Never enough memory */
2582 /* do we have any? */
2589 ret = usrsctp_setsockopt(so, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf, (socklen_t)len);
2590 if ((ret == 0) && id) {
2591 p_id = (sctp_assoc_t *)buf;
2602 usrsctp_getpaddrs(struct socket *so, sctp_assoc_t id, struct sockaddr **raddrs)
2604 struct sctp_getaddresses *addrs;
2605 struct sockaddr *sa;
2611 if (raddrs == NULL) {
2616 opt_len = (socklen_t)sizeof(sctp_assoc_t);
2617 if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE, &asoc, &opt_len) != 0) {
2620 /* size required is returned in 'asoc' */
2621 opt_len = (socklen_t)((size_t)asoc + sizeof(struct sctp_getaddresses));
2622 addrs = calloc(1, (size_t)opt_len);
2623 if (addrs == NULL) {
2627 addrs->sget_assoc_id = id;
2628 /* Now lets get the array of addresses */
2629 if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES, addrs, &opt_len) != 0) {
2633 *raddrs = (struct sockaddr *)&addrs->addr[0];
2635 sa = (struct sockaddr *)&addrs->addr[0];
2636 lim = (caddr_t)addrs + opt_len;
2638 while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
2639 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
2641 while ((caddr_t)sa < lim) {
2642 switch (sa->sa_family) {
2645 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
2650 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
2654 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_conn));
2667 usrsctp_freepaddrs(struct sockaddr *addrs)
2669 /* Take away the hidden association id */
2672 fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t));
2678 usrsctp_getladdrs(struct socket *so, sctp_assoc_t id, struct sockaddr **raddrs)
2680 struct sctp_getaddresses *addrs;
2682 struct sockaddr *sa;
2683 size_t size_of_addresses;
2687 if (raddrs == NULL) {
2691 size_of_addresses = 0;
2692 opt_len = (socklen_t)sizeof(int);
2693 if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE, &size_of_addresses, &opt_len) != 0) {
2697 if (size_of_addresses == 0) {
2701 opt_len = (socklen_t)(size_of_addresses +
2702 sizeof(struct sockaddr_storage) +
2703 sizeof(struct sctp_getaddresses));
2704 addrs = calloc(1, (size_t)opt_len);
2705 if (addrs == NULL) {
2709 addrs->sget_assoc_id = id;
2710 /* Now lets get the array of addresses */
2711 if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs, &opt_len) != 0) {
2716 *raddrs = (struct sockaddr *)&addrs->addr[0];
2718 sa = (struct sockaddr *)&addrs->addr[0];
2719 lim = (caddr_t)addrs + opt_len;
2721 while (((caddr_t)sa < lim) && (sa->sa_len > 0)) {
2722 sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len);
2724 while ((caddr_t)sa < lim) {
2725 switch (sa->sa_family) {
2728 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in));
2733 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6));
2737 sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_conn));
2750 usrsctp_freeladdrs(struct sockaddr *addrs)
2752 /* Take away the hidden association id */
2755 fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t));
2762 sctp_userspace_ip_output(int *result, struct mbuf *o_pak,
2763 sctp_route_t *ro, void *stcb,
2767 struct mbuf *m_orig;
2774 #if !defined (__Userspace_os_Windows)
2777 struct sockaddr_in dst;
2778 #if defined (__Userspace_os_Windows)
2781 WSABUF send_iovec[MAXLEN_MBUF_CHAIN];
2784 struct iovec send_iovec[MAXLEN_MBUF_CHAIN];
2785 struct msghdr msg_hdr;
2787 int use_udp_tunneling;
2791 m = SCTP_HEADER_TO_CHAIN(o_pak);
2794 len = sizeof(struct ip);
2795 if (SCTP_BUF_LEN(m) < len) {
2796 if ((m = m_pullup(m, len)) == 0) {
2797 SCTP_PRINTF("Can not get the IP header in the first mbuf.\n");
2801 ip = mtod(m, struct ip *);
2802 use_udp_tunneling = (ip->ip_p == IPPROTO_UDP);
2804 if (use_udp_tunneling) {
2805 len = sizeof(struct ip) + sizeof(struct udphdr);
2806 if (SCTP_BUF_LEN(m) < len) {
2807 if ((m = m_pullup(m, len)) == 0) {
2808 SCTP_PRINTF("Can not get the UDP/IP header in the first mbuf.\n");
2811 ip = mtod(m, struct ip *);
2813 udp = (struct udphdr *)(ip + 1);
2818 if (!use_udp_tunneling) {
2819 if (ip->ip_src.s_addr == INADDR_ANY) {
2820 /* TODO get addr of outgoing interface */
2821 SCTP_PRINTF("Why did the SCTP implementation did not choose a source address?\n");
2823 /* TODO need to worry about ro->ro_dst as in ip_output? */
2824 #if defined(__Userspace_os_Linux) || defined (__Userspace_os_Windows)
2825 /* need to put certain fields into network order for Linux */
2826 ip->ip_len = htons(ip->ip_len);
2831 memset((void *)&dst, 0, sizeof(struct sockaddr_in));
2832 dst.sin_family = AF_INET;
2833 dst.sin_addr.s_addr = ip->ip_dst.s_addr;
2835 dst.sin_len = sizeof(struct sockaddr_in);
2837 if (use_udp_tunneling) {
2838 dst.sin_port = udp->uh_dport;
2843 /* tweak the mbuf chain */
2844 if (use_udp_tunneling) {
2845 m_adj(m, sizeof(struct ip) + sizeof(struct udphdr));
2848 send_len = SCTP_HEADER_LEN(m); /* length of entire packet */
2850 for (iovcnt = 0; m != NULL && iovcnt < MAXLEN_MBUF_CHAIN; m = m->m_next, iovcnt++) {
2851 #if !defined (__Userspace_os_Windows)
2852 send_iovec[iovcnt].iov_base = (caddr_t)m->m_data;
2853 send_iovec[iovcnt].iov_len = SCTP_BUF_LEN(m);
2854 send_count += send_iovec[iovcnt].iov_len;
2856 send_iovec[iovcnt].buf = (caddr_t)m->m_data;
2857 send_iovec[iovcnt].len = SCTP_BUF_LEN(m);
2858 send_count += send_iovec[iovcnt].len;
2863 SCTP_PRINTF("mbuf chain couldn't be copied completely\n");
2867 #if !defined (__Userspace_os_Windows)
2868 msg_hdr.msg_name = (struct sockaddr *) &dst;
2869 msg_hdr.msg_namelen = sizeof(struct sockaddr_in);
2870 msg_hdr.msg_iov = send_iovec;
2871 msg_hdr.msg_iovlen = iovcnt;
2872 msg_hdr.msg_control = NULL;
2873 msg_hdr.msg_controllen = 0;
2874 msg_hdr.msg_flags = 0;
2876 if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp) > -1)) {
2877 if ((res = sendmsg(SCTP_BASE_VAR(userspace_rawsctp), &msg_hdr, MSG_DONTWAIT)) != send_len) {
2881 if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp) > -1)) {
2882 if ((res = sendmsg(SCTP_BASE_VAR(userspace_udpsctp), &msg_hdr, MSG_DONTWAIT)) != send_len) {
2887 win_msg_hdr.name = (struct sockaddr *) &dst;
2888 win_msg_hdr.namelen = sizeof(struct sockaddr_in);
2889 win_msg_hdr.lpBuffers = (LPWSABUF)send_iovec;
2890 win_msg_hdr.dwBufferCount = iovcnt;
2893 win_msg_hdr.Control = winbuf;
2894 win_msg_hdr.dwFlags = 0;
2896 if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp) > -1)) {
2897 if (WSASendTo(SCTP_BASE_VAR(userspace_rawsctp), (LPWSABUF) send_iovec, iovcnt, &win_sent_len, win_msg_hdr.dwFlags, win_msg_hdr.name, (int) win_msg_hdr.namelen, NULL, NULL) != 0) {
2898 *result = WSAGetLastError();
2899 } else if (win_sent_len != send_len) {
2900 *result = WSAGetLastError();
2903 if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp) > -1)) {
2904 if (WSASendTo(SCTP_BASE_VAR(userspace_udpsctp), (LPWSABUF) send_iovec, iovcnt, &win_sent_len, win_msg_hdr.dwFlags, win_msg_hdr.name, (int) win_msg_hdr.namelen, NULL, NULL) != 0) {
2905 *result = WSAGetLastError();
2906 } else if (win_sent_len != send_len) {
2907 *result = WSAGetLastError();
2912 sctp_m_freem(m_orig);
2917 void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak,
2918 struct route_in6 *ro, void *stcb,
2922 struct mbuf *m_orig;
2927 struct ip6_hdr *ip6;
2929 #if !defined (__Userspace_os_Windows)
2932 struct sockaddr_in6 dst;
2933 #if defined (__Userspace_os_Windows)
2936 WSABUF send_iovec[MAXLEN_MBUF_CHAIN];
2939 struct iovec send_iovec[MAXLEN_MBUF_CHAIN];
2940 struct msghdr msg_hdr;
2942 int use_udp_tunneling;
2946 m = SCTP_HEADER_TO_CHAIN(o_pak);
2949 len = sizeof(struct ip6_hdr);
2951 if (SCTP_BUF_LEN(m) < len) {
2952 if ((m = m_pullup(m, len)) == 0) {
2953 SCTP_PRINTF("Can not get the IP header in the first mbuf.\n");
2958 ip6 = mtod(m, struct ip6_hdr *);
2959 use_udp_tunneling = (ip6->ip6_nxt == IPPROTO_UDP);
2961 if (use_udp_tunneling) {
2962 len = sizeof(struct ip6_hdr) + sizeof(struct udphdr);
2963 if (SCTP_BUF_LEN(m) < len) {
2964 if ((m = m_pullup(m, len)) == 0) {
2965 SCTP_PRINTF("Can not get the UDP/IP header in the first mbuf.\n");
2968 ip6 = mtod(m, struct ip6_hdr *);
2970 udp = (struct udphdr *)(ip6 + 1);
2975 if (!use_udp_tunneling) {
2976 if (ip6->ip6_src.s6_addr == in6addr_any.s6_addr) {
2977 /* TODO get addr of outgoing interface */
2978 SCTP_PRINTF("Why did the SCTP implementation did not choose a source address?\n");
2980 /* TODO need to worry about ro->ro_dst as in ip_output? */
2981 #if defined(__Userspace_os_Linux) || defined (__Userspace_os_Windows)
2982 /* need to put certain fields into network order for Linux */
2983 ip6->ip6_plen = htons(ip6->ip6_plen);
2987 memset((void *)&dst, 0, sizeof(struct sockaddr_in6));
2988 dst.sin6_family = AF_INET6;
2989 dst.sin6_addr = ip6->ip6_dst;
2990 #ifdef HAVE_SIN6_LEN
2991 dst.sin6_len = sizeof(struct sockaddr_in6);
2994 if (use_udp_tunneling) {
2995 dst.sin6_port = udp->uh_dport;
3000 /* tweak the mbuf chain */
3001 if (use_udp_tunneling) {
3002 m_adj(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
3004 m_adj(m, sizeof(struct ip6_hdr));
3007 send_len = SCTP_HEADER_LEN(m); /* length of entire packet */
3009 for (iovcnt = 0; m != NULL && iovcnt < MAXLEN_MBUF_CHAIN; m = m->m_next, iovcnt++) {
3010 #if !defined (__Userspace_os_Windows)
3011 send_iovec[iovcnt].iov_base = (caddr_t)m->m_data;
3012 send_iovec[iovcnt].iov_len = SCTP_BUF_LEN(m);
3013 send_count += send_iovec[iovcnt].iov_len;
3015 send_iovec[iovcnt].buf = (caddr_t)m->m_data;
3016 send_iovec[iovcnt].len = SCTP_BUF_LEN(m);
3017 send_count += send_iovec[iovcnt].len;
3021 SCTP_PRINTF("mbuf chain couldn't be copied completely\n");
3025 #if !defined (__Userspace_os_Windows)
3026 msg_hdr.msg_name = (struct sockaddr *) &dst;
3027 msg_hdr.msg_namelen = sizeof(struct sockaddr_in6);
3028 msg_hdr.msg_iov = send_iovec;
3029 msg_hdr.msg_iovlen = iovcnt;
3030 msg_hdr.msg_control = NULL;
3031 msg_hdr.msg_controllen = 0;
3032 msg_hdr.msg_flags = 0;
3034 if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp6) > -1)) {
3035 if ((res = sendmsg(SCTP_BASE_VAR(userspace_rawsctp6), &msg_hdr, MSG_DONTWAIT)) != send_len) {
3039 if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp6) > -1)) {
3040 if ((res = sendmsg(SCTP_BASE_VAR(userspace_udpsctp6), &msg_hdr, MSG_DONTWAIT)) != send_len) {
3045 win_msg_hdr.name = (struct sockaddr *) &dst;
3046 win_msg_hdr.namelen = sizeof(struct sockaddr_in6);
3047 win_msg_hdr.lpBuffers = (LPWSABUF)send_iovec;
3048 win_msg_hdr.dwBufferCount = iovcnt;
3051 win_msg_hdr.Control = winbuf;
3052 win_msg_hdr.dwFlags = 0;
3054 if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp6) > -1)) {
3055 if (WSASendTo(SCTP_BASE_VAR(userspace_rawsctp6), (LPWSABUF) send_iovec, iovcnt, &win_sent_len, win_msg_hdr.dwFlags, win_msg_hdr.name, (int) win_msg_hdr.namelen, NULL, NULL) != 0) {
3056 *result = WSAGetLastError();
3057 } else if (win_sent_len != send_len) {
3058 *result = WSAGetLastError();
3061 if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp6) > -1)) {
3062 if (WSASendTo(SCTP_BASE_VAR(userspace_udpsctp6), (LPWSABUF) send_iovec, iovcnt, &win_sent_len, win_msg_hdr.dwFlags, win_msg_hdr.name, (int) win_msg_hdr.namelen, NULL, NULL) != 0) {
3063 *result = WSAGetLastError();
3064 } else if (win_sent_len != send_len) {
3065 *result = WSAGetLastError();
3070 sctp_m_freem(m_orig);
3075 usrsctp_register_address(void *addr)
3077 struct sockaddr_conn sconn;
3079 memset(&sconn, 0, sizeof(struct sockaddr_conn));
3080 sconn.sconn_family = AF_CONN;
3081 #ifdef HAVE_SCONN_LEN
3082 sconn.sconn_len = sizeof(struct sockaddr_conn);
3084 sconn.sconn_port = 0;
3085 sconn.sconn_addr = addr;
3086 sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID,
3092 (struct sockaddr *)&sconn,
3098 usrsctp_deregister_address(void *addr)
3100 struct sockaddr_conn sconn;
3102 memset(&sconn, 0, sizeof(struct sockaddr_conn));
3103 sconn.sconn_family = AF_CONN;
3104 #ifdef HAVE_SCONN_LEN
3105 sconn.sconn_len = sizeof(struct sockaddr_conn);
3107 sconn.sconn_port = 0;
3108 sconn.sconn_addr = addr;
3109 sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID,
3110 (struct sockaddr *)&sconn,
3115 #define PREAMBLE_FORMAT "\n%c %02d:%02d:%02d.%06ld "
3116 #define PREAMBLE_LENGTH 19
3117 #define HEADER "0000 "
3118 #define TRAILER "# SCTP_PACKET\n"
3121 usrsctp_dumppacket(void *buf, size_t len, int outbound)
3124 char *dump_buf, *packet;
3134 if ((len == 0) || (buf == NULL)) {
3137 if ((dump_buf = malloc(PREAMBLE_LENGTH + strlen(HEADER) + 3 * len + strlen(TRAILER) + 1)) == NULL) {
3143 localtime_s(&t, &tb.time);
3144 _snprintf_s(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_LENGTH, PREAMBLE_FORMAT,
3145 outbound ? 'O' : 'I',
3146 t.tm_hour, t.tm_min, t.tm_sec, (long)(1000 * tb.millitm));
3148 gettimeofday(&tv, NULL);
3149 sec = (time_t)tv.tv_sec;
3150 t = localtime((const time_t *)&sec);
3151 snprintf(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_FORMAT,
3152 outbound ? 'O' : 'I',
3153 t->tm_hour, t->tm_min, t->tm_sec, (long)tv.tv_usec);
3155 pos += PREAMBLE_LENGTH;
3157 strncpy_s(dump_buf + pos, strlen(HEADER) + 1, HEADER, strlen(HEADER));
3159 strcpy(dump_buf + pos, HEADER);
3161 pos += strlen(HEADER);
3162 packet = (char *)buf;
3163 for (i = 0; i < len; i++) {
3164 uint8_t byte, low, high;
3166 byte = (uint8_t)packet[i];
3169 dump_buf[pos++] = high < 10 ? '0' + high : 'a' + (high - 10);
3170 dump_buf[pos++] = low < 10 ? '0' + low : 'a' + (low - 10);
3171 dump_buf[pos++] = ' ';
3174 strncpy_s(dump_buf + pos, strlen(TRAILER) + 1, TRAILER, strlen(TRAILER));
3176 strcpy(dump_buf + pos, TRAILER);
3178 pos += strlen(TRAILER);
3179 dump_buf[pos++] = '\0';
3184 usrsctp_freedumpbuffer(char *buf)
3190 usrsctp_conninput(void *addr, const void *buffer, size_t length, uint8_t ecn_bits)
3192 struct sockaddr_conn src, dst;
3195 struct sctp_chunkhdr *ch;
3197 SCTP_STAT_INCR(sctps_recvpackets);
3198 SCTP_STAT_INCR_COUNTER64(sctps_inpackets);
3199 memset(&src, 0, sizeof(struct sockaddr_conn));
3200 src.sconn_family = AF_CONN;
3201 #ifdef HAVE_SCONN_LEN
3202 src.sconn_len = sizeof(struct sockaddr_conn);
3204 src.sconn_addr = addr;
3205 memset(&dst, 0, sizeof(struct sockaddr_conn));
3206 dst.sconn_family = AF_CONN;
3207 #ifdef HAVE_SCONN_LEN
3208 dst.sconn_len = sizeof(struct sockaddr_conn);
3210 dst.sconn_addr = addr;
3211 if ((m = sctp_get_mbuf_for_msg(length, 1, M_NOWAIT, 0, MT_DATA)) == NULL) {
3214 m_copyback(m, 0, length, (caddr_t)buffer);
3215 if (SCTP_BUF_LEN(m) < (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr))) {
3216 if ((m = m_pullup(m, sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr))) == NULL) {
3217 SCTP_STAT_INCR(sctps_hdrops);
3221 sh = mtod(m, struct sctphdr *);;
3222 ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr));
3223 src.sconn_port = sh->src_port;
3224 dst.sconn_port = sh->dest_port;
3225 sctp_common_input_processing(&m, 0, sizeof(struct sctphdr), length,
3226 (struct sockaddr *)&src,
3227 (struct sockaddr *)&dst,
3229 #if !defined(SCTP_WITH_NO_CSUM)
3233 SCTP_DEFAULT_VRFID, 0);
3241 #define USRSCTP_SYSCTL_SET_DEF(__field) \
3242 void usrsctp_sysctl_set_ ## __field(uint32_t value) { \
3243 SCTP_BASE_SYSCTL(__field) = value; \
3246 USRSCTP_SYSCTL_SET_DEF(sctp_sendspace)
3247 USRSCTP_SYSCTL_SET_DEF(sctp_recvspace)
3248 USRSCTP_SYSCTL_SET_DEF(sctp_auto_asconf)
3249 USRSCTP_SYSCTL_SET_DEF(sctp_multiple_asconfs)
3250 USRSCTP_SYSCTL_SET_DEF(sctp_ecn_enable)
3251 USRSCTP_SYSCTL_SET_DEF(sctp_pr_enable)
3252 USRSCTP_SYSCTL_SET_DEF(sctp_auth_enable)
3253 USRSCTP_SYSCTL_SET_DEF(sctp_asconf_enable)
3254 USRSCTP_SYSCTL_SET_DEF(sctp_reconfig_enable)
3255 USRSCTP_SYSCTL_SET_DEF(sctp_nrsack_enable)
3256 USRSCTP_SYSCTL_SET_DEF(sctp_pktdrop_enable)
3257 USRSCTP_SYSCTL_SET_DEF(sctp_strict_sacks)
3258 #if !defined(SCTP_WITH_NO_CSUM)
3259 USRSCTP_SYSCTL_SET_DEF(sctp_no_csum_on_loopback)
3261 USRSCTP_SYSCTL_SET_DEF(sctp_peer_chunk_oh)
3262 USRSCTP_SYSCTL_SET_DEF(sctp_max_burst_default)
3263 USRSCTP_SYSCTL_SET_DEF(sctp_max_chunks_on_queue)
3264 USRSCTP_SYSCTL_SET_DEF(sctp_hashtblsize)
3265 USRSCTP_SYSCTL_SET_DEF(sctp_pcbtblsize)
3266 USRSCTP_SYSCTL_SET_DEF(sctp_min_split_point)
3267 USRSCTP_SYSCTL_SET_DEF(sctp_chunkscale)
3268 USRSCTP_SYSCTL_SET_DEF(sctp_delayed_sack_time_default)
3269 USRSCTP_SYSCTL_SET_DEF(sctp_sack_freq_default)
3270 USRSCTP_SYSCTL_SET_DEF(sctp_system_free_resc_limit)
3271 USRSCTP_SYSCTL_SET_DEF(sctp_asoc_free_resc_limit)
3272 USRSCTP_SYSCTL_SET_DEF(sctp_heartbeat_interval_default)
3273 USRSCTP_SYSCTL_SET_DEF(sctp_pmtu_raise_time_default)
3274 USRSCTP_SYSCTL_SET_DEF(sctp_shutdown_guard_time_default)
3275 USRSCTP_SYSCTL_SET_DEF(sctp_secret_lifetime_default)
3276 USRSCTP_SYSCTL_SET_DEF(sctp_rto_max_default)
3277 USRSCTP_SYSCTL_SET_DEF(sctp_rto_min_default)
3278 USRSCTP_SYSCTL_SET_DEF(sctp_rto_initial_default)
3279 USRSCTP_SYSCTL_SET_DEF(sctp_init_rto_max_default)
3280 USRSCTP_SYSCTL_SET_DEF(sctp_valid_cookie_life_default)
3281 USRSCTP_SYSCTL_SET_DEF(sctp_init_rtx_max_default)
3282 USRSCTP_SYSCTL_SET_DEF(sctp_assoc_rtx_max_default)
3283 USRSCTP_SYSCTL_SET_DEF(sctp_path_rtx_max_default)
3284 USRSCTP_SYSCTL_SET_DEF(sctp_add_more_threshold)
3285 USRSCTP_SYSCTL_SET_DEF(sctp_nr_outgoing_streams_default)
3286 USRSCTP_SYSCTL_SET_DEF(sctp_cmt_on_off)
3287 USRSCTP_SYSCTL_SET_DEF(sctp_cmt_use_dac)
3288 USRSCTP_SYSCTL_SET_DEF(sctp_use_cwnd_based_maxburst)
3289 USRSCTP_SYSCTL_SET_DEF(sctp_nat_friendly)
3290 USRSCTP_SYSCTL_SET_DEF(sctp_L2_abc_variable)
3291 USRSCTP_SYSCTL_SET_DEF(sctp_mbuf_threshold_count)
3292 USRSCTP_SYSCTL_SET_DEF(sctp_do_drain)
3293 USRSCTP_SYSCTL_SET_DEF(sctp_hb_maxburst)
3294 USRSCTP_SYSCTL_SET_DEF(sctp_abort_if_one_2_one_hits_limit)
3295 USRSCTP_SYSCTL_SET_DEF(sctp_strict_data_order)
3296 USRSCTP_SYSCTL_SET_DEF(sctp_min_residual)
3297 USRSCTP_SYSCTL_SET_DEF(sctp_max_retran_chunk)
3298 USRSCTP_SYSCTL_SET_DEF(sctp_logging_level)
3299 USRSCTP_SYSCTL_SET_DEF(sctp_default_cc_module)
3300 USRSCTP_SYSCTL_SET_DEF(sctp_default_frag_interleave)
3301 USRSCTP_SYSCTL_SET_DEF(sctp_mobility_base)
3302 USRSCTP_SYSCTL_SET_DEF(sctp_mobility_fasthandoff)
3303 USRSCTP_SYSCTL_SET_DEF(sctp_inits_include_nat_friendly)
3304 USRSCTP_SYSCTL_SET_DEF(sctp_udp_tunneling_port)
3305 USRSCTP_SYSCTL_SET_DEF(sctp_enable_sack_immediately)
3306 USRSCTP_SYSCTL_SET_DEF(sctp_vtag_time_wait)
3307 USRSCTP_SYSCTL_SET_DEF(sctp_blackhole)
3308 USRSCTP_SYSCTL_SET_DEF(sctp_diag_info_code)
3309 USRSCTP_SYSCTL_SET_DEF(sctp_fr_max_burst_default)
3310 USRSCTP_SYSCTL_SET_DEF(sctp_path_pf_threshold)
3311 USRSCTP_SYSCTL_SET_DEF(sctp_default_ss_module)
3312 USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_bw)
3313 USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_rtt)
3314 USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_eqret)
3315 USRSCTP_SYSCTL_SET_DEF(sctp_steady_step)
3316 USRSCTP_SYSCTL_SET_DEF(sctp_use_dccc_ecn)
3317 USRSCTP_SYSCTL_SET_DEF(sctp_buffer_splitting)
3318 USRSCTP_SYSCTL_SET_DEF(sctp_initial_cwnd)
3320 USRSCTP_SYSCTL_SET_DEF(sctp_debug_on)
3323 #define USRSCTP_SYSCTL_GET_DEF(__field) \
3324 uint32_t usrsctp_sysctl_get_ ## __field(void) { \
3325 return SCTP_BASE_SYSCTL(__field); \
3328 USRSCTP_SYSCTL_GET_DEF(sctp_sendspace)
3329 USRSCTP_SYSCTL_GET_DEF(sctp_recvspace)
3330 USRSCTP_SYSCTL_GET_DEF(sctp_auto_asconf)
3331 USRSCTP_SYSCTL_GET_DEF(sctp_multiple_asconfs)
3332 USRSCTP_SYSCTL_GET_DEF(sctp_ecn_enable)
3333 USRSCTP_SYSCTL_GET_DEF(sctp_pr_enable)
3334 USRSCTP_SYSCTL_GET_DEF(sctp_auth_enable)
3335 USRSCTP_SYSCTL_GET_DEF(sctp_asconf_enable)
3336 USRSCTP_SYSCTL_GET_DEF(sctp_reconfig_enable)
3337 USRSCTP_SYSCTL_GET_DEF(sctp_nrsack_enable)
3338 USRSCTP_SYSCTL_GET_DEF(sctp_pktdrop_enable)
3339 USRSCTP_SYSCTL_GET_DEF(sctp_strict_sacks)
3340 #if !defined(SCTP_WITH_NO_CSUM)
3341 USRSCTP_SYSCTL_GET_DEF(sctp_no_csum_on_loopback)
3343 USRSCTP_SYSCTL_GET_DEF(sctp_peer_chunk_oh)
3344 USRSCTP_SYSCTL_GET_DEF(sctp_max_burst_default)
3345 USRSCTP_SYSCTL_GET_DEF(sctp_max_chunks_on_queue)
3346 USRSCTP_SYSCTL_GET_DEF(sctp_hashtblsize)
3347 USRSCTP_SYSCTL_GET_DEF(sctp_pcbtblsize)
3348 USRSCTP_SYSCTL_GET_DEF(sctp_min_split_point)
3349 USRSCTP_SYSCTL_GET_DEF(sctp_chunkscale)
3350 USRSCTP_SYSCTL_GET_DEF(sctp_delayed_sack_time_default)
3351 USRSCTP_SYSCTL_GET_DEF(sctp_sack_freq_default)
3352 USRSCTP_SYSCTL_GET_DEF(sctp_system_free_resc_limit)
3353 USRSCTP_SYSCTL_GET_DEF(sctp_asoc_free_resc_limit)
3354 USRSCTP_SYSCTL_GET_DEF(sctp_heartbeat_interval_default)
3355 USRSCTP_SYSCTL_GET_DEF(sctp_pmtu_raise_time_default)
3356 USRSCTP_SYSCTL_GET_DEF(sctp_shutdown_guard_time_default)
3357 USRSCTP_SYSCTL_GET_DEF(sctp_secret_lifetime_default)
3358 USRSCTP_SYSCTL_GET_DEF(sctp_rto_max_default)
3359 USRSCTP_SYSCTL_GET_DEF(sctp_rto_min_default)
3360 USRSCTP_SYSCTL_GET_DEF(sctp_rto_initial_default)
3361 USRSCTP_SYSCTL_GET_DEF(sctp_init_rto_max_default)
3362 USRSCTP_SYSCTL_GET_DEF(sctp_valid_cookie_life_default)
3363 USRSCTP_SYSCTL_GET_DEF(sctp_init_rtx_max_default)
3364 USRSCTP_SYSCTL_GET_DEF(sctp_assoc_rtx_max_default)
3365 USRSCTP_SYSCTL_GET_DEF(sctp_path_rtx_max_default)
3366 USRSCTP_SYSCTL_GET_DEF(sctp_add_more_threshold)
3367 USRSCTP_SYSCTL_GET_DEF(sctp_nr_outgoing_streams_default)
3368 USRSCTP_SYSCTL_GET_DEF(sctp_cmt_on_off)
3369 USRSCTP_SYSCTL_GET_DEF(sctp_cmt_use_dac)
3370 USRSCTP_SYSCTL_GET_DEF(sctp_use_cwnd_based_maxburst)
3371 USRSCTP_SYSCTL_GET_DEF(sctp_nat_friendly)
3372 USRSCTP_SYSCTL_GET_DEF(sctp_L2_abc_variable)
3373 USRSCTP_SYSCTL_GET_DEF(sctp_mbuf_threshold_count)
3374 USRSCTP_SYSCTL_GET_DEF(sctp_do_drain)
3375 USRSCTP_SYSCTL_GET_DEF(sctp_hb_maxburst)
3376 USRSCTP_SYSCTL_GET_DEF(sctp_abort_if_one_2_one_hits_limit)
3377 USRSCTP_SYSCTL_GET_DEF(sctp_strict_data_order)
3378 USRSCTP_SYSCTL_GET_DEF(sctp_min_residual)
3379 USRSCTP_SYSCTL_GET_DEF(sctp_max_retran_chunk)
3380 USRSCTP_SYSCTL_GET_DEF(sctp_logging_level)
3381 USRSCTP_SYSCTL_GET_DEF(sctp_default_cc_module)
3382 USRSCTP_SYSCTL_GET_DEF(sctp_default_frag_interleave)
3383 USRSCTP_SYSCTL_GET_DEF(sctp_mobility_base)
3384 USRSCTP_SYSCTL_GET_DEF(sctp_mobility_fasthandoff)
3385 USRSCTP_SYSCTL_GET_DEF(sctp_inits_include_nat_friendly)
3386 USRSCTP_SYSCTL_GET_DEF(sctp_udp_tunneling_port)
3387 USRSCTP_SYSCTL_GET_DEF(sctp_enable_sack_immediately)
3388 USRSCTP_SYSCTL_GET_DEF(sctp_vtag_time_wait)
3389 USRSCTP_SYSCTL_GET_DEF(sctp_blackhole)
3390 USRSCTP_SYSCTL_GET_DEF(sctp_diag_info_code)
3391 USRSCTP_SYSCTL_GET_DEF(sctp_fr_max_burst_default)
3392 USRSCTP_SYSCTL_GET_DEF(sctp_path_pf_threshold)
3393 USRSCTP_SYSCTL_GET_DEF(sctp_default_ss_module)
3394 USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_bw)
3395 USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_rtt)
3396 USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_eqret)
3397 USRSCTP_SYSCTL_GET_DEF(sctp_steady_step)
3398 USRSCTP_SYSCTL_GET_DEF(sctp_use_dccc_ecn)
3399 USRSCTP_SYSCTL_GET_DEF(sctp_buffer_splitting)
3400 USRSCTP_SYSCTL_GET_DEF(sctp_initial_cwnd)
3402 USRSCTP_SYSCTL_GET_DEF(sctp_debug_on)
3405 void usrsctp_get_stat(struct sctpstat *stat)
3407 *stat = SCTP_BASE_STATS;