michael@0: /*- michael@0: * Copyright (c) 1982, 1986, 1988, 1990, 1993 michael@0: * The Regents of the University of California. michael@0: * Copyright (c) 2004 The FreeBSD Foundation michael@0: * Copyright (c) 2004-2008 Robert N. M. Watson michael@0: * Copyright (c) 2009-2010 Brad Penoff michael@0: * Copyright (c) 2009-2010 Humaira Kamal michael@0: * Copyright (c) 2011-2012 Irene Ruengeler michael@0: * Copyright (c) 2011-2012 Michael Tuexen michael@0: * All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND michael@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE michael@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL michael@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS michael@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) michael@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT michael@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY michael@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF michael@0: * SUCH DAMAGE. michael@0: * michael@0: */ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef INET6 michael@0: #include michael@0: #endif michael@0: #if defined(__Userspace_os_Linux) michael@0: #define __FAVOR_BSD /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */ michael@0: #endif michael@0: #if !defined (__Userspace_os_Windows) michael@0: #include michael@0: #include michael@0: #else michael@0: #include michael@0: #endif michael@0: userland_mutex_t accept_mtx; michael@0: userland_cond_t accept_cond; michael@0: #ifdef _WIN32 michael@0: #include michael@0: #include michael@0: #endif michael@0: michael@0: MALLOC_DEFINE(M_PCB, "sctp_pcb", "sctp pcb"); michael@0: MALLOC_DEFINE(M_SONAME, "sctp_soname", "sctp soname"); michael@0: #define MAXLEN_MBUF_CHAIN 32 michael@0: michael@0: /* Prototypes */ michael@0: extern int sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, michael@0: struct mbuf *top, struct mbuf *control, int flags, michael@0: /* proc is a dummy in __Userspace__ and will not be passed to sctp_lower_sosend */ struct proc *p); michael@0: michael@0: extern int sctp_attach(struct socket *so, int proto, uint32_t vrf_id); michael@0: extern int sctpconn_attach(struct socket *so, int proto, uint32_t vrf_id); michael@0: michael@0: void michael@0: usrsctp_init(uint16_t port, michael@0: int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), michael@0: void (*debug_printf)(const char *format, ...)) michael@0: { michael@0: sctp_init(port, conn_output, debug_printf); michael@0: } michael@0: michael@0: michael@0: /* Taken from usr/src/sys/kern/uipc_sockbuf.c and modified for __Userspace__*/ michael@0: /* michael@0: * Socantsendmore indicates that no more data will be sent on the socket; it michael@0: * would normally be applied to a socket when the user informs the system michael@0: * that no more data is to be sent, by the protocol code (in case michael@0: * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be michael@0: * received, and will normally be applied to the socket by a protocol when it michael@0: * detects that the peer will send no more data. Data queued for reading in michael@0: * the socket may yet be read. michael@0: */ michael@0: michael@0: void socantrcvmore_locked(struct socket *so) michael@0: { michael@0: SOCKBUF_LOCK_ASSERT(&so->so_rcv); michael@0: so->so_rcv.sb_state |= SBS_CANTRCVMORE; michael@0: sorwakeup_locked(so); michael@0: } michael@0: michael@0: void socantrcvmore(struct socket *so) michael@0: { michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: socantrcvmore_locked(so); michael@0: } michael@0: michael@0: void michael@0: socantsendmore_locked(struct socket *so) michael@0: { michael@0: SOCKBUF_LOCK_ASSERT(&so->so_snd); michael@0: so->so_snd.sb_state |= SBS_CANTSENDMORE; michael@0: sowwakeup_locked(so); michael@0: } michael@0: michael@0: void michael@0: socantsendmore(struct socket *so) michael@0: { michael@0: SOCKBUF_LOCK(&so->so_snd); michael@0: socantsendmore_locked(so); michael@0: } michael@0: michael@0: michael@0: michael@0: /* Taken from usr/src/sys/kern/uipc_sockbuf.c and called within sctp_lower_sosend. michael@0: */ michael@0: int michael@0: sbwait(struct sockbuf *sb) michael@0: { michael@0: #if defined(__Userspace__) /* __Userspace__ */ michael@0: michael@0: SOCKBUF_LOCK_ASSERT(sb); michael@0: michael@0: sb->sb_flags |= SB_WAIT; michael@0: #if defined (__Userspace_os_Windows) michael@0: if (SleepConditionVariableCS(&(sb->sb_cond), &(sb->sb_mtx), INFINITE)) michael@0: return 0; michael@0: else michael@0: return -1; michael@0: #else michael@0: return (pthread_cond_wait(&(sb->sb_cond), &(sb->sb_mtx))); michael@0: #endif michael@0: michael@0: #else michael@0: SOCKBUF_LOCK_ASSERT(sb); michael@0: michael@0: sb->sb_flags |= SB_WAIT; michael@0: return (msleep(&sb->sb_cc, &sb->sb_mtx, michael@0: (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", michael@0: sb->sb_timeo)); michael@0: #endif michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: /* Taken from /src/sys/kern/uipc_socket.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: static struct socket * michael@0: soalloc(void) michael@0: { michael@0: struct socket *so; michael@0: michael@0: /* michael@0: * soalloc() sets of socket layer state for a socket, michael@0: * called only by socreate() and sonewconn(). michael@0: * michael@0: * sodealloc() tears down socket layer state for a socket, michael@0: * called only by sofree() and sonewconn(). michael@0: * __Userspace__ TODO : Make sure so is properly deallocated michael@0: * when tearing down the connection. michael@0: */ michael@0: michael@0: so = (struct socket *)malloc(sizeof(struct socket)); michael@0: michael@0: if (so == NULL) { michael@0: return (NULL); michael@0: } michael@0: memset(so, 0, sizeof(struct socket)); michael@0: michael@0: /* __Userspace__ Initializing the socket locks here */ michael@0: SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd"); michael@0: SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv"); michael@0: SOCKBUF_COND_INIT(&so->so_snd); michael@0: SOCKBUF_COND_INIT(&so->so_rcv); michael@0: SOCK_COND_INIT(so); /* timeo_cond */ michael@0: michael@0: /* __Userspace__ Any ref counting required here? Will we have any use for aiojobq? michael@0: What about gencnt and numopensockets?*/ michael@0: TAILQ_INIT(&so->so_aiojobq); michael@0: return (so); michael@0: } michael@0: michael@0: static void michael@0: sodealloc(struct socket *so) michael@0: { michael@0: michael@0: KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); michael@0: KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL")); michael@0: michael@0: SOCKBUF_COND_DESTROY(&so->so_snd); michael@0: SOCKBUF_COND_DESTROY(&so->so_rcv); michael@0: michael@0: SOCK_COND_DESTROY(so); michael@0: michael@0: SOCKBUF_LOCK_DESTROY(&so->so_snd); michael@0: SOCKBUF_LOCK_DESTROY(&so->so_rcv); michael@0: michael@0: free(so); michael@0: } michael@0: michael@0: /* Taken from /src/sys/kern/uipc_socket.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: void michael@0: sofree(struct socket *so) michael@0: { michael@0: struct socket *head; michael@0: michael@0: ACCEPT_LOCK_ASSERT(); michael@0: SOCK_LOCK_ASSERT(so); michael@0: /* SS_NOFDREF unset in accept call. this condition seems irrelevent michael@0: * for __Userspace__... michael@0: */ michael@0: if (so->so_count != 0 || michael@0: (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) { michael@0: SOCK_UNLOCK(so); michael@0: ACCEPT_UNLOCK(); michael@0: return; michael@0: } michael@0: head = so->so_head; michael@0: if (head != NULL) { michael@0: KASSERT((so->so_qstate & SQ_COMP) != 0 || michael@0: (so->so_qstate & SQ_INCOMP) != 0, michael@0: ("sofree: so_head != NULL, but neither SQ_COMP nor " michael@0: "SQ_INCOMP")); michael@0: KASSERT((so->so_qstate & SQ_COMP) == 0 || michael@0: (so->so_qstate & SQ_INCOMP) == 0, michael@0: ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP")); michael@0: TAILQ_REMOVE(&head->so_incomp, so, so_list); michael@0: head->so_incqlen--; michael@0: so->so_qstate &= ~SQ_INCOMP; michael@0: so->so_head = NULL; michael@0: } michael@0: KASSERT((so->so_qstate & SQ_COMP) == 0 && michael@0: (so->so_qstate & SQ_INCOMP) == 0, michael@0: ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)", michael@0: so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP)); michael@0: if (so->so_options & SCTP_SO_ACCEPTCONN) { michael@0: KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated")); michael@0: KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated")); michael@0: } michael@0: SOCK_UNLOCK(so); michael@0: ACCEPT_UNLOCK(); michael@0: sctp_close(so); /* was... sctp_detach(so); */ michael@0: /* michael@0: * From this point on, we assume that no other references to this michael@0: * socket exist anywhere else in the stack. Therefore, no locks need michael@0: * to be acquired or held. michael@0: * michael@0: * We used to do a lot of socket buffer and socket locking here, as michael@0: * well as invoke sorflush() and perform wakeups. The direct call to michael@0: * dom_dispose() and sbrelease_internal() are an inlining of what was michael@0: * necessary from sorflush(). michael@0: * michael@0: * Notice that the socket buffer and kqueue state are torn down michael@0: * before calling pru_detach. This means that protocols shold not michael@0: * assume they can perform socket wakeups, etc, in their detach code. michael@0: */ michael@0: sodealloc(so); michael@0: } michael@0: michael@0: michael@0: michael@0: /* Taken from /src/sys/kern/uipc_socket.c */ michael@0: int michael@0: soabort(so) michael@0: struct socket *so; michael@0: { michael@0: int error; michael@0: #if defined(INET6) michael@0: struct sctp_inpcb *inp; michael@0: #endif michael@0: michael@0: #if defined(INET6) michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { michael@0: error = sctp6_abort(so); michael@0: } else { michael@0: #if defined(INET) michael@0: error = sctp_abort(so); michael@0: #else michael@0: error = EAFNOSUPPORT; michael@0: #endif michael@0: } michael@0: #elif defined(INET) michael@0: error = sctp_abort(so); michael@0: #else michael@0: error = EAFNOSUPPORT; michael@0: #endif michael@0: if (error) { michael@0: sofree(so); michael@0: return error; michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: michael@0: /* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_connect (sctp_usrreq.c). michael@0: * We use sctp_connect for send_one_init_real in ms1. michael@0: */ michael@0: void michael@0: soisconnecting(struct socket *so) michael@0: { michael@0: michael@0: SOCK_LOCK(so); michael@0: so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); michael@0: so->so_state |= SS_ISCONNECTING; michael@0: SOCK_UNLOCK(so); michael@0: } michael@0: michael@0: /* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_disconnect (sctp_usrreq.c). michael@0: * TODO Do we use sctp_disconnect? michael@0: */ michael@0: void michael@0: soisdisconnecting(struct socket *so) michael@0: { michael@0: michael@0: /* michael@0: * Note: This code assumes that SOCK_LOCK(so) and michael@0: * SOCKBUF_LOCK(&so->so_rcv) are the same. michael@0: */ michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: so->so_state &= ~SS_ISCONNECTING; michael@0: so->so_state |= SS_ISDISCONNECTING; michael@0: so->so_rcv.sb_state |= SBS_CANTRCVMORE; michael@0: sorwakeup_locked(so); michael@0: SOCKBUF_LOCK(&so->so_snd); michael@0: so->so_snd.sb_state |= SBS_CANTSENDMORE; michael@0: sowwakeup_locked(so); michael@0: wakeup("dummy",so); michael@0: /* requires 2 args but this was in orig */ michael@0: /* wakeup(&so->so_timeo); */ michael@0: } michael@0: michael@0: michael@0: /* Taken from sys/kern/kern_synch.c and michael@0: modified for __Userspace__ michael@0: */ michael@0: michael@0: /* michael@0: * Make all threads sleeping on the specified identifier runnable. michael@0: * Associating wakeup with so_timeo identifier and timeo_cond michael@0: * condition variable. TODO. If we use iterator thread then we need to michael@0: * modify wakeup so it can distinguish between iterator identifier and michael@0: * timeo identifier. michael@0: */ michael@0: void michael@0: wakeup(ident, so) michael@0: void *ident; michael@0: struct socket *so; michael@0: { michael@0: SOCK_LOCK(so); michael@0: #if defined (__Userspace_os_Windows) michael@0: WakeAllConditionVariable(&(so)->timeo_cond); michael@0: #else michael@0: pthread_cond_broadcast(&(so)->timeo_cond); michael@0: #endif michael@0: SOCK_UNLOCK(so); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Make a thread sleeping on the specified identifier runnable. michael@0: * May wake more than one thread if a target thread is currently michael@0: * swapped out. michael@0: */ michael@0: void michael@0: wakeup_one(ident) michael@0: void *ident; michael@0: { michael@0: /* __Userspace__ Check: We are using accept_cond for wakeup_one. michael@0: It seems that wakeup_one is only called within michael@0: soisconnected() and sonewconn() with ident &head->so_timeo michael@0: head is so->so_head, which is back pointer to listen socket michael@0: This seems to indicate that the use of accept_cond is correct michael@0: since socket where accepts occur is so_head in all michael@0: subsidiary sockets. michael@0: */ michael@0: ACCEPT_LOCK(); michael@0: #if defined (__Userspace_os_Windows) michael@0: WakeAllConditionVariable(&accept_cond); michael@0: #else michael@0: pthread_cond_broadcast(&accept_cond); michael@0: #endif michael@0: ACCEPT_UNLOCK(); michael@0: } michael@0: michael@0: michael@0: /* Called within sctp_process_cookie_[existing/new] */ michael@0: void michael@0: soisconnected(struct socket *so) michael@0: { michael@0: struct socket *head; michael@0: michael@0: ACCEPT_LOCK(); michael@0: SOCK_LOCK(so); michael@0: so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); michael@0: so->so_state |= SS_ISCONNECTED; michael@0: head = so->so_head; michael@0: if (head != NULL && (so->so_qstate & SQ_INCOMP)) { michael@0: SOCK_UNLOCK(so); michael@0: TAILQ_REMOVE(&head->so_incomp, so, so_list); michael@0: head->so_incqlen--; michael@0: so->so_qstate &= ~SQ_INCOMP; michael@0: TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); michael@0: head->so_qlen++; michael@0: so->so_qstate |= SQ_COMP; michael@0: ACCEPT_UNLOCK(); michael@0: sorwakeup(head); michael@0: wakeup_one(&head->so_timeo); michael@0: return; michael@0: } michael@0: SOCK_UNLOCK(so); michael@0: ACCEPT_UNLOCK(); michael@0: wakeup(&so->so_timeo, so); michael@0: sorwakeup(so); michael@0: sowwakeup(so); michael@0: michael@0: } michael@0: michael@0: /* called within sctp_handle_cookie_echo */ michael@0: michael@0: struct socket * michael@0: sonewconn(struct socket *head, int connstatus) michael@0: { michael@0: struct socket *so; michael@0: int over; michael@0: michael@0: ACCEPT_LOCK(); michael@0: over = (head->so_qlen > 3 * head->so_qlimit / 2); michael@0: ACCEPT_UNLOCK(); michael@0: #ifdef REGRESSION michael@0: if (regression_sonewconn_earlytest && over) michael@0: #else michael@0: if (over) michael@0: #endif michael@0: return (NULL); michael@0: so = soalloc(); michael@0: if (so == NULL) michael@0: return (NULL); michael@0: so->so_head = head; michael@0: so->so_type = head->so_type; michael@0: so->so_options = head->so_options &~ SCTP_SO_ACCEPTCONN; michael@0: so->so_linger = head->so_linger; michael@0: so->so_state = head->so_state | SS_NOFDREF; michael@0: so->so_dom = head->so_dom; michael@0: #ifdef MAC michael@0: SOCK_LOCK(head); michael@0: mac_create_socket_from_socket(head, so); michael@0: SOCK_UNLOCK(head); michael@0: #endif michael@0: if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) { michael@0: sodealloc(so); michael@0: return (NULL); michael@0: } michael@0: switch (head->so_dom) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: if (sctp_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) { michael@0: sodealloc(so); michael@0: return (NULL); michael@0: } michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: if (sctp6_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) { michael@0: sodealloc(so); michael@0: return (NULL); michael@0: } michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: if (sctpconn_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) { michael@0: sodealloc(so); michael@0: return (NULL); michael@0: } michael@0: break; michael@0: default: michael@0: sodealloc(so); michael@0: return (NULL); michael@0: break; michael@0: } michael@0: so->so_rcv.sb_lowat = head->so_rcv.sb_lowat; michael@0: so->so_snd.sb_lowat = head->so_snd.sb_lowat; michael@0: so->so_rcv.sb_timeo = head->so_rcv.sb_timeo; michael@0: so->so_snd.sb_timeo = head->so_snd.sb_timeo; michael@0: so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE; michael@0: so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE; michael@0: so->so_state |= connstatus; michael@0: ACCEPT_LOCK(); michael@0: if (connstatus) { michael@0: TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); michael@0: so->so_qstate |= SQ_COMP; michael@0: head->so_qlen++; michael@0: } else { michael@0: /* michael@0: * Keep removing sockets from the head until there's room for michael@0: * us to insert on the tail. In pre-locking revisions, this michael@0: * was a simple if(), but as we could be racing with other michael@0: * threads and soabort() requires dropping locks, we must michael@0: * loop waiting for the condition to be true. michael@0: */ michael@0: while (head->so_incqlen > head->so_qlimit) { michael@0: struct socket *sp; michael@0: sp = TAILQ_FIRST(&head->so_incomp); michael@0: TAILQ_REMOVE(&head->so_incomp, sp, so_list); michael@0: head->so_incqlen--; michael@0: sp->so_qstate &= ~SQ_INCOMP; michael@0: sp->so_head = NULL; michael@0: ACCEPT_UNLOCK(); michael@0: soabort(sp); michael@0: ACCEPT_LOCK(); michael@0: } michael@0: TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); michael@0: so->so_qstate |= SQ_INCOMP; michael@0: head->so_incqlen++; michael@0: } michael@0: ACCEPT_UNLOCK(); michael@0: if (connstatus) { michael@0: sorwakeup(head); michael@0: wakeup_one(&head->so_timeo); michael@0: } michael@0: return (so); michael@0: michael@0: } michael@0: michael@0: /* From /src/sys/sys/sysproto.h */ michael@0: struct sctp_generic_sendmsg_args { michael@0: int sd; michael@0: caddr_t msg; michael@0: int mlen; michael@0: caddr_t to; michael@0: socklen_t tolen; /* was __socklen_t */ michael@0: struct sctp_sndrcvinfo * sinfo; michael@0: int flags; michael@0: }; michael@0: michael@0: struct sctp_generic_recvmsg_args { michael@0: int sd; michael@0: struct iovec *iov; michael@0: int iovlen; michael@0: struct sockaddr *from; michael@0: socklen_t *fromlenaddr; /* was __socklen_t */ michael@0: struct sctp_sndrcvinfo *sinfo; michael@0: int *msg_flags; michael@0: }; michael@0: michael@0: michael@0: /* michael@0: Source: /src/sys/gnu/fs/xfs/FreeBSD/xfs_ioctl.c michael@0: */ michael@0: static __inline__ int michael@0: copy_to_user(void *dst, void *src, int len) { michael@0: memcpy(dst, src, len); michael@0: return 0; michael@0: } michael@0: michael@0: static __inline__ int michael@0: copy_from_user(void *dst, void *src, int len) { michael@0: memcpy(dst, src, len); michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: References: michael@0: src/sys/dev/lmc/if_lmc.h: michael@0: src/sys/powerpc/powerpc/copyinout.c michael@0: src/sys/sys/systm.h michael@0: */ michael@0: # define copyin(u, k, len) copy_from_user(k, u, len) michael@0: michael@0: /* References: michael@0: src/sys/powerpc/powerpc/copyinout.c michael@0: src/sys/sys/systm.h michael@0: */ michael@0: # define copyout(k, u, len) copy_to_user(u, k, len) michael@0: michael@0: michael@0: /* copyiniov definition copied/modified from src/sys/kern/kern_subr.c */ michael@0: int michael@0: copyiniov(struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error) michael@0: { michael@0: u_int iovlen; michael@0: michael@0: *iov = NULL; michael@0: if (iovcnt > UIO_MAXIOV) michael@0: return (error); michael@0: iovlen = iovcnt * sizeof (struct iovec); michael@0: *iov = malloc(iovlen); /*, M_IOV, M_WAITOK); */ michael@0: error = copyin(iovp, *iov, iovlen); michael@0: if (error) { michael@0: free(*iov); /*, M_IOV); */ michael@0: *iov = NULL; michael@0: } michael@0: return (error); michael@0: } michael@0: michael@0: /* (__Userspace__) version of uiomove */ michael@0: int michael@0: uiomove(void *cp, int n, struct uio *uio) michael@0: { michael@0: struct iovec *iov; michael@0: int cnt; michael@0: int error = 0; michael@0: michael@0: if ((uio->uio_rw != UIO_READ) && michael@0: (uio->uio_rw != UIO_WRITE)) { michael@0: return (EINVAL); michael@0: } michael@0: michael@0: while (n > 0 && uio->uio_resid) { michael@0: iov = uio->uio_iov; michael@0: cnt = iov->iov_len; michael@0: if (cnt == 0) { michael@0: uio->uio_iov++; michael@0: uio->uio_iovcnt--; michael@0: continue; michael@0: } michael@0: if (cnt > n) michael@0: cnt = n; michael@0: michael@0: switch (uio->uio_segflg) { michael@0: michael@0: case UIO_USERSPACE: michael@0: if (uio->uio_rw == UIO_READ) michael@0: error = copyout(cp, iov->iov_base, cnt); michael@0: else michael@0: error = copyin(iov->iov_base, cp, cnt); michael@0: if (error) michael@0: goto out; michael@0: break; michael@0: michael@0: case UIO_SYSSPACE: michael@0: if (uio->uio_rw == UIO_READ) michael@0: bcopy(cp, iov->iov_base, cnt); michael@0: else michael@0: bcopy(iov->iov_base, cp, cnt); michael@0: break; michael@0: } michael@0: iov->iov_base = (char *)iov->iov_base + cnt; michael@0: iov->iov_len -= cnt; michael@0: uio->uio_resid -= cnt; michael@0: uio->uio_offset += cnt; michael@0: cp = (char *)cp + cnt; michael@0: n -= cnt; michael@0: } michael@0: out: michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: /* Source: src/sys/kern/uipc_syscalls.c */ michael@0: int michael@0: getsockaddr(namp, uaddr, len) michael@0: struct sockaddr **namp; michael@0: caddr_t uaddr; michael@0: size_t len; michael@0: { michael@0: struct sockaddr *sa; michael@0: int error; michael@0: michael@0: if (len > SOCK_MAXADDRLEN) michael@0: return (ENAMETOOLONG); michael@0: if (len < offsetof(struct sockaddr, sa_data)) michael@0: return (EINVAL); michael@0: MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK); michael@0: error = copyin(uaddr, sa, len); michael@0: if (error) { michael@0: FREE(sa, M_SONAME); michael@0: } else { michael@0: #ifdef HAVE_SA_LEN michael@0: sa->sa_len = len; michael@0: #endif michael@0: *namp = sa; michael@0: } michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: /* Taken from /src/lib/libc/net/sctp_sys_calls.c michael@0: * and modified for __Userspace__ michael@0: * calling sctp_generic_sendmsg from this function michael@0: */ michael@0: ssize_t michael@0: userspace_sctp_sendmsg(struct socket *so, michael@0: const void *data, michael@0: size_t len, michael@0: struct sockaddr *to, michael@0: socklen_t tolen, michael@0: u_int32_t ppid, michael@0: u_int32_t flags, michael@0: u_int16_t stream_no, michael@0: u_int32_t timetolive, michael@0: u_int32_t context) michael@0: { michael@0: struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo; michael@0: struct uio auio; michael@0: struct iovec iov[1]; michael@0: michael@0: sinfo->sinfo_ppid = ppid; michael@0: sinfo->sinfo_flags = flags; michael@0: sinfo->sinfo_stream = stream_no; michael@0: sinfo->sinfo_timetolive = timetolive; michael@0: sinfo->sinfo_context = context; michael@0: sinfo->sinfo_assoc_id = 0; michael@0: michael@0: michael@0: /* Perform error checks on destination (to) */ michael@0: if (tolen > SOCK_MAXADDRLEN){ michael@0: errno = ENAMETOOLONG; michael@0: return (-1); michael@0: } michael@0: if ((tolen > 0) && michael@0: ((to == NULL) || (tolen < (socklen_t)sizeof(struct sockaddr)))) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: /* Adding the following as part of defensive programming, in case the application michael@0: does not do it when preparing the destination address.*/ michael@0: #ifdef HAVE_SA_LEN michael@0: if (to != NULL) { michael@0: to->sa_len = tolen; michael@0: } michael@0: #endif michael@0: michael@0: iov[0].iov_base = (caddr_t)data; michael@0: iov[0].iov_len = len; michael@0: michael@0: auio.uio_iov = iov; michael@0: auio.uio_iovcnt = 1; michael@0: auio.uio_segflg = UIO_USERSPACE; michael@0: auio.uio_rw = UIO_WRITE; michael@0: auio.uio_offset = 0; /* XXX */ michael@0: auio.uio_resid = len; michael@0: errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, 0, sinfo); michael@0: if (errno == 0) { michael@0: return (len - auio.uio_resid); michael@0: } else { michael@0: return (-1); michael@0: } michael@0: } michael@0: michael@0: michael@0: ssize_t michael@0: usrsctp_sendv(struct socket *so, michael@0: const void *data, michael@0: size_t len, michael@0: struct sockaddr *to, michael@0: int addrcnt, michael@0: void *info, michael@0: socklen_t infolen, michael@0: unsigned int infotype, michael@0: int flags) michael@0: { michael@0: struct sctp_sndrcvinfo sinfo; michael@0: struct uio auio; michael@0: struct iovec iov[1]; michael@0: int use_sinfo; michael@0: michael@0: if (so == NULL) { michael@0: errno = EBADF; michael@0: return (-1); michael@0: } michael@0: memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); michael@0: use_sinfo = 0; michael@0: switch (infotype) { michael@0: case SCTP_SENDV_NOINFO: michael@0: if ((infolen != 0) || (info != NULL)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: break; michael@0: case SCTP_SENDV_SNDINFO: michael@0: if ((info == NULL) || (infolen != sizeof(struct sctp_sndinfo))) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: sinfo.sinfo_stream = ((struct sctp_sndinfo *)info)->snd_sid; michael@0: sinfo.sinfo_flags = ((struct sctp_sndinfo *)info)->snd_flags; michael@0: sinfo.sinfo_ppid = ((struct sctp_sndinfo *)info)->snd_ppid; michael@0: sinfo.sinfo_context = ((struct sctp_sndinfo *)info)->snd_context; michael@0: sinfo.sinfo_assoc_id = ((struct sctp_sndinfo *)info)->snd_assoc_id; michael@0: use_sinfo = 1; michael@0: break; michael@0: case SCTP_SENDV_PRINFO: michael@0: if ((info == NULL) || (infolen != sizeof(struct sctp_prinfo))) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: sinfo.sinfo_stream = 0; michael@0: sinfo.sinfo_flags = PR_SCTP_POLICY(((struct sctp_prinfo *)info)->pr_policy); michael@0: sinfo.sinfo_timetolive = ((struct sctp_prinfo *)info)->pr_value; michael@0: use_sinfo = 1; michael@0: break; michael@0: case SCTP_SENDV_AUTHINFO: michael@0: errno = EINVAL; michael@0: return (-1); michael@0: case SCTP_SENDV_SPA: michael@0: if ((info == NULL) || (infolen != sizeof(struct sctp_sendv_spa))) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_SNDINFO_VALID) { michael@0: sinfo.sinfo_stream = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_sid; michael@0: sinfo.sinfo_flags = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_flags; michael@0: sinfo.sinfo_ppid = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_ppid; michael@0: sinfo.sinfo_context = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_context; michael@0: sinfo.sinfo_assoc_id = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_assoc_id; michael@0: } else { michael@0: sinfo.sinfo_flags = 0; michael@0: sinfo.sinfo_stream = 0; michael@0: } michael@0: if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_PRINFO_VALID) { michael@0: sinfo.sinfo_flags |= PR_SCTP_POLICY(((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_policy); michael@0: sinfo.sinfo_timetolive = ((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_value; michael@0: } michael@0: if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_AUTHINFO_VALID) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: use_sinfo = 1; michael@0: break; michael@0: default: michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: michael@0: /* Perform error checks on destination (to) */ michael@0: if (addrcnt > 1) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: michael@0: iov[0].iov_base = (caddr_t)data; michael@0: iov[0].iov_len = len; michael@0: michael@0: auio.uio_iov = iov; michael@0: auio.uio_iovcnt = 1; michael@0: auio.uio_segflg = UIO_USERSPACE; michael@0: auio.uio_rw = UIO_WRITE; michael@0: auio.uio_offset = 0; /* XXX */ michael@0: auio.uio_resid = len; michael@0: errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, flags, use_sinfo ? &sinfo : NULL); michael@0: if (errno == 0) { michael@0: return (len - auio.uio_resid); michael@0: } else { michael@0: return (-1); michael@0: } michael@0: } michael@0: michael@0: michael@0: ssize_t michael@0: userspace_sctp_sendmbuf(struct socket *so, michael@0: struct mbuf* mbufdata, michael@0: size_t len, michael@0: struct sockaddr *to, michael@0: socklen_t tolen, michael@0: u_int32_t ppid, michael@0: u_int32_t flags, michael@0: u_int16_t stream_no, michael@0: u_int32_t timetolive, michael@0: u_int32_t context) michael@0: { michael@0: michael@0: struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo; michael@0: /* struct uio auio; michael@0: struct iovec iov[1]; */ michael@0: int error = 0; michael@0: int uflags = 0; michael@0: int retvalsendmsg; michael@0: michael@0: sinfo->sinfo_ppid = ppid; michael@0: sinfo->sinfo_flags = flags; michael@0: sinfo->sinfo_stream = stream_no; michael@0: sinfo->sinfo_timetolive = timetolive; michael@0: sinfo->sinfo_context = context; michael@0: sinfo->sinfo_assoc_id = 0; michael@0: michael@0: /* Perform error checks on destination (to) */ michael@0: if (tolen > SOCK_MAXADDRLEN){ michael@0: error = (ENAMETOOLONG); michael@0: goto sendmsg_return; michael@0: } michael@0: if (tolen < (socklen_t)offsetof(struct sockaddr, sa_data)){ michael@0: error = (EINVAL); michael@0: goto sendmsg_return; michael@0: } michael@0: /* Adding the following as part of defensive programming, in case the application michael@0: does not do it when preparing the destination address.*/ michael@0: #ifdef HAVE_SA_LEN michael@0: to->sa_len = tolen; michael@0: #endif michael@0: michael@0: error = sctp_lower_sosend(so, to, NULL/*uio*/, michael@0: (struct mbuf *)mbufdata, (struct mbuf *)NULL, michael@0: uflags, sinfo); michael@0: sendmsg_return: michael@0: /* TODO: Needs a condition for non-blocking when error is EWOULDBLOCK */ michael@0: if (0 == error) michael@0: retvalsendmsg = len; michael@0: else if(error == EWOULDBLOCK) { michael@0: errno = EWOULDBLOCK; michael@0: retvalsendmsg = (-1); michael@0: } else { michael@0: SCTP_PRINTF("%s: error = %d\n", __func__, error); michael@0: errno = error; michael@0: retvalsendmsg = (-1); michael@0: } michael@0: return retvalsendmsg; michael@0: michael@0: } michael@0: michael@0: michael@0: /* taken from usr.lib/sctp_sys_calls.c and needed here */ michael@0: #define SCTP_SMALL_IOVEC_SIZE 2 michael@0: michael@0: /* Taken from /src/lib/libc/net/sctp_sys_calls.c michael@0: * and modified for __Userspace__ michael@0: * calling sctp_generic_recvmsg from this function michael@0: */ michael@0: ssize_t michael@0: userspace_sctp_recvmsg(struct socket *so, michael@0: void *dbuf, michael@0: size_t len, michael@0: struct sockaddr *from, michael@0: socklen_t *fromlenp, michael@0: struct sctp_sndrcvinfo *sinfo, michael@0: int *msg_flags) michael@0: { michael@0: struct uio auio; michael@0: struct iovec iov[SCTP_SMALL_IOVEC_SIZE]; michael@0: struct iovec *tiov; michael@0: int iovlen = 1; michael@0: int error = 0; michael@0: int ulen, i, retval; michael@0: socklen_t fromlen; michael@0: michael@0: iov[0].iov_base = dbuf; michael@0: iov[0].iov_len = len; michael@0: michael@0: auio.uio_iov = iov; michael@0: auio.uio_iovcnt = iovlen; michael@0: auio.uio_segflg = UIO_USERSPACE; michael@0: auio.uio_rw = UIO_READ; michael@0: auio.uio_offset = 0; /* XXX */ michael@0: auio.uio_resid = 0; michael@0: tiov = iov; michael@0: for (i = 0; i iov_len) < 0) { michael@0: error = EINVAL; michael@0: SCTP_PRINTF("%s: error = %d\n", __func__, error); michael@0: return (-1); michael@0: } michael@0: } michael@0: ulen = auio.uio_resid; michael@0: if (fromlenp != NULL) { michael@0: fromlen = *fromlenp; michael@0: } else { michael@0: fromlen = 0; michael@0: } michael@0: error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL, michael@0: from, fromlen, msg_flags, michael@0: (struct sctp_sndrcvinfo *)sinfo, 1); michael@0: michael@0: if (error) { michael@0: if (auio.uio_resid != (int)ulen && michael@0: (error == EINTR || michael@0: #if !defined(__Userspace_os_NetBSD) michael@0: error == ERESTART || michael@0: #endif michael@0: error == EWOULDBLOCK)) { michael@0: error = 0; michael@0: } michael@0: } michael@0: if ((fromlenp != NULL) && (fromlen > 0) && (from != NULL)) { michael@0: switch (from->sa_family) { michael@0: #if defined(INET) michael@0: case AF_INET: michael@0: *fromlenp = sizeof(struct sockaddr_in); michael@0: break; michael@0: #endif michael@0: #if defined(INET6) michael@0: case AF_INET6: michael@0: *fromlenp = sizeof(struct sockaddr_in6); michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: *fromlenp = sizeof(struct sockaddr_conn); michael@0: break; michael@0: default: michael@0: *fromlenp = 0; michael@0: break; michael@0: } michael@0: if (*fromlenp > fromlen) { michael@0: *fromlenp = fromlen; michael@0: } michael@0: } michael@0: if (error == 0){ michael@0: /* ready return value */ michael@0: retval = (int)ulen - auio.uio_resid; michael@0: return (retval); michael@0: } else { michael@0: SCTP_PRINTF("%s: error = %d\n", __func__, error); michael@0: return (-1); michael@0: } michael@0: } michael@0: michael@0: ssize_t michael@0: usrsctp_recvv(struct socket *so, michael@0: void *dbuf, michael@0: size_t len, michael@0: struct sockaddr *from, michael@0: socklen_t *fromlenp, michael@0: void *info, michael@0: socklen_t *infolen, michael@0: unsigned int *infotype, michael@0: int *msg_flags) michael@0: { michael@0: struct uio auio; michael@0: struct iovec iov[SCTP_SMALL_IOVEC_SIZE]; michael@0: struct iovec *tiov; michael@0: int iovlen = 1; michael@0: int ulen, i; michael@0: socklen_t fromlen; michael@0: struct sctp_rcvinfo *rcv; michael@0: struct sctp_recvv_rn *rn; michael@0: struct sctp_extrcvinfo seinfo; michael@0: michael@0: if (so == NULL) { michael@0: errno = EBADF; michael@0: return (-1); michael@0: } michael@0: iov[0].iov_base = dbuf; michael@0: iov[0].iov_len = len; michael@0: michael@0: auio.uio_iov = iov; michael@0: auio.uio_iovcnt = iovlen; michael@0: auio.uio_segflg = UIO_USERSPACE; michael@0: auio.uio_rw = UIO_READ; michael@0: auio.uio_offset = 0; /* XXX */ michael@0: auio.uio_resid = 0; michael@0: tiov = iov; michael@0: for (i = 0; i iov_len) < 0) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: } michael@0: ulen = auio.uio_resid; michael@0: if (fromlenp != NULL) { michael@0: fromlen = *fromlenp; michael@0: } else { michael@0: fromlen = 0; michael@0: } michael@0: errno = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL, michael@0: from, fromlen, msg_flags, michael@0: (struct sctp_sndrcvinfo *)&seinfo, 1); michael@0: if (errno) { michael@0: if (auio.uio_resid != (int)ulen && michael@0: (errno == EINTR || michael@0: #if !defined(__Userspace_os_NetBSD) michael@0: errno == ERESTART || michael@0: #endif michael@0: errno == EWOULDBLOCK)) { michael@0: errno = 0; michael@0: } michael@0: } michael@0: if ((*msg_flags & MSG_NOTIFICATION) == 0) { michael@0: struct sctp_inpcb *inp; michael@0: michael@0: inp = (struct sctp_inpcb *)so->so_pcb; michael@0: if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) && michael@0: sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO) && michael@0: *infolen >= (socklen_t)sizeof(struct sctp_recvv_rn) && michael@0: seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL) { michael@0: rn = (struct sctp_recvv_rn *)info; michael@0: rn->recvv_rcvinfo.rcv_sid = seinfo.sinfo_stream; michael@0: rn->recvv_rcvinfo.rcv_ssn = seinfo.sinfo_ssn; michael@0: rn->recvv_rcvinfo.rcv_flags = seinfo.sinfo_flags; michael@0: rn->recvv_rcvinfo.rcv_ppid = seinfo.sinfo_ppid; michael@0: rn->recvv_rcvinfo.rcv_context = seinfo.sinfo_context; michael@0: rn->recvv_rcvinfo.rcv_tsn = seinfo.sinfo_tsn; michael@0: rn->recvv_rcvinfo.rcv_cumtsn = seinfo.sinfo_cumtsn; michael@0: rn->recvv_rcvinfo.rcv_assoc_id = seinfo.sinfo_assoc_id; michael@0: rn->recvv_nxtinfo.nxt_sid = seinfo.sreinfo_next_stream; michael@0: rn->recvv_nxtinfo.nxt_flags = 0; michael@0: if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) { michael@0: rn->recvv_nxtinfo.nxt_flags |= SCTP_UNORDERED; michael@0: } michael@0: if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) { michael@0: rn->recvv_nxtinfo.nxt_flags |= SCTP_NOTIFICATION; michael@0: } michael@0: if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) { michael@0: rn->recvv_nxtinfo.nxt_flags |= SCTP_COMPLETE; michael@0: } michael@0: rn->recvv_nxtinfo.nxt_ppid = seinfo.sreinfo_next_ppid; michael@0: rn->recvv_nxtinfo.nxt_length = seinfo.sreinfo_next_length; michael@0: rn->recvv_nxtinfo.nxt_assoc_id = seinfo.sreinfo_next_aid; michael@0: *infolen = (socklen_t)sizeof(struct sctp_recvv_rn); michael@0: *infotype = SCTP_RECVV_RN; michael@0: } else if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO) && michael@0: *infolen >= (socklen_t)sizeof(struct sctp_rcvinfo)) { michael@0: rcv = (struct sctp_rcvinfo *)info; michael@0: rcv->rcv_sid = seinfo.sinfo_stream; michael@0: rcv->rcv_ssn = seinfo.sinfo_ssn; michael@0: rcv->rcv_flags = seinfo.sinfo_flags; michael@0: rcv->rcv_ppid = seinfo.sinfo_ppid; michael@0: rcv->rcv_context = seinfo.sinfo_context; michael@0: rcv->rcv_tsn = seinfo.sinfo_tsn; michael@0: rcv->rcv_cumtsn = seinfo.sinfo_cumtsn; michael@0: rcv->rcv_assoc_id = seinfo.sinfo_assoc_id; michael@0: *infolen = (socklen_t)sizeof(struct sctp_rcvinfo); michael@0: *infotype = SCTP_RECVV_RCVINFO; michael@0: } else { michael@0: *infotype = SCTP_RECVV_NOINFO; michael@0: *infolen = 0; michael@0: } michael@0: } michael@0: if ((fromlenp != NULL) && (fromlen > 0) && (from != NULL)) { michael@0: switch (from->sa_family) { michael@0: #if defined(INET) michael@0: case AF_INET: michael@0: *fromlenp = sizeof(struct sockaddr_in); michael@0: break; michael@0: #endif michael@0: #if defined(INET6) michael@0: case AF_INET6: michael@0: *fromlenp = sizeof(struct sockaddr_in6); michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: *fromlenp = sizeof(struct sockaddr_conn); michael@0: break; michael@0: default: michael@0: *fromlenp = 0; michael@0: break; michael@0: } michael@0: if (*fromlenp > fromlen) { michael@0: *fromlenp = fromlen; michael@0: } michael@0: } michael@0: if (errno == 0) { michael@0: /* ready return value */ michael@0: return ((int)ulen - auio.uio_resid); michael@0: } else { michael@0: return (-1); michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: #if defined(__Userspace__) michael@0: /* Taken from /src/sys/kern/uipc_socket.c michael@0: * and modified for __Userspace__ michael@0: * socreate returns a socket. The socket should be michael@0: * closed with soclose(). michael@0: */ michael@0: int michael@0: socreate(int dom, struct socket **aso, int type, int proto) michael@0: { michael@0: struct socket *so; michael@0: int error; michael@0: michael@0: if ((dom != AF_CONN) && (dom != AF_INET) && (dom != AF_INET6)) { michael@0: return (EINVAL); michael@0: } michael@0: if ((type != SOCK_STREAM) && (type != SOCK_SEQPACKET)) { michael@0: return (EINVAL); michael@0: } michael@0: if (proto != IPPROTO_SCTP) { michael@0: return (EINVAL); michael@0: } michael@0: michael@0: so = soalloc(); michael@0: if (so == NULL) { michael@0: return (ENOBUFS); michael@0: } michael@0: michael@0: /* michael@0: * so_incomp represents a queue of connections that michael@0: * must be completed at protocol level before being michael@0: * returned. so_comp field heads a list of sockets michael@0: * that are ready to be returned to the listening process michael@0: *__Userspace__ These queues are being used at a number of places like accept etc. michael@0: */ michael@0: TAILQ_INIT(&so->so_incomp); michael@0: TAILQ_INIT(&so->so_comp); michael@0: so->so_type = type; michael@0: so->so_count = 1; michael@0: so->so_dom = dom; michael@0: /* michael@0: * Auto-sizing of socket buffers is managed by the protocols and michael@0: * the appropriate flags must be set in the pru_attach function. michael@0: * For __Userspace__ The pru_attach function in this case is sctp_attach. michael@0: */ michael@0: switch (dom) { michael@0: #if defined(INET) michael@0: case AF_INET: michael@0: error = sctp_attach(so, proto, SCTP_DEFAULT_VRFID); michael@0: break; michael@0: #endif michael@0: #if defined(INET6) michael@0: case AF_INET6: michael@0: error = sctp6_attach(so, proto, SCTP_DEFAULT_VRFID); michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: error = sctpconn_attach(so, proto, SCTP_DEFAULT_VRFID); michael@0: break; michael@0: default: michael@0: error = EAFNOSUPPORT; michael@0: break; michael@0: } michael@0: if (error) { michael@0: KASSERT(so->so_count == 1, ("socreate: so_count %d", so->so_count)); michael@0: so->so_count = 0; michael@0: sodealloc(so); michael@0: return (error); michael@0: } michael@0: *aso = so; michael@0: return (0); michael@0: } michael@0: #else michael@0: /* The kernel version for reference is below. The #else michael@0: should be removed once the __Userspace__ michael@0: version is tested. michael@0: * socreate returns a socket with a ref count of 1. The socket should be michael@0: * closed with soclose(). michael@0: */ michael@0: int michael@0: socreate(int dom, struct socket **aso, int type, int proto, michael@0: struct ucred *cred, struct thread *td) michael@0: { michael@0: struct protosw *prp; michael@0: struct socket *so; michael@0: int error; michael@0: michael@0: if (proto) michael@0: prp = pffindproto(dom, proto, type); michael@0: else michael@0: prp = pffindtype(dom, type); michael@0: michael@0: if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL || michael@0: prp->pr_usrreqs->pru_attach == pru_attach_notsupp) michael@0: return (EPROTONOSUPPORT); michael@0: michael@0: if (jailed(cred) && jail_socket_unixiproute_only && michael@0: prp->pr_domain->dom_family != PF_LOCAL && michael@0: prp->pr_domain->dom_family != PF_INET && michael@0: prp->pr_domain->dom_family != PF_ROUTE) { michael@0: return (EPROTONOSUPPORT); michael@0: } michael@0: michael@0: if (prp->pr_type != type) michael@0: return (EPROTOTYPE); michael@0: so = soalloc(); michael@0: if (so == NULL) michael@0: return (ENOBUFS); michael@0: michael@0: TAILQ_INIT(&so->so_incomp); michael@0: TAILQ_INIT(&so->so_comp); michael@0: so->so_type = type; michael@0: so->so_cred = crhold(cred); michael@0: so->so_proto = prp; michael@0: #ifdef MAC michael@0: mac_create_socket(cred, so); michael@0: #endif michael@0: knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv), michael@0: NULL, NULL, NULL); michael@0: knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd), michael@0: NULL, NULL, NULL); michael@0: so->so_count = 1; michael@0: /* michael@0: * Auto-sizing of socket buffers is managed by the protocols and michael@0: * the appropriate flags must be set in the pru_attach function. michael@0: */ michael@0: error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); michael@0: if (error) { michael@0: KASSERT(so->so_count == 1, ("socreate: so_count %d", michael@0: so->so_count)); michael@0: so->so_count = 0; michael@0: sodealloc(so); michael@0: return (error); michael@0: } michael@0: *aso = so; michael@0: return (0); michael@0: } michael@0: #endif michael@0: michael@0: michael@0: michael@0: michael@0: /* Taken from /src/sys/kern/uipc_syscalls.c michael@0: * and modified for __Userspace__ michael@0: * Removing struct thread td. michael@0: */ michael@0: struct socket * michael@0: userspace_socket(int domain, int type, int protocol) michael@0: { michael@0: struct socket *so = NULL; michael@0: michael@0: errno = socreate(domain, &so, type, protocol); michael@0: if (errno) { michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * The original socket call returns the file descriptor fd. michael@0: * td->td_retval[0] = fd. michael@0: * We are returning struct socket *so. michael@0: */ michael@0: return (so); michael@0: } michael@0: michael@0: struct socket * michael@0: usrsctp_socket(int domain, int type, int protocol, michael@0: int (*receive_cb)(struct socket *sock, union sctp_sockstore addr, void *data, michael@0: size_t datalen, struct sctp_rcvinfo, int flags, void *ulp_info), michael@0: int (*send_cb)(struct socket *sock, uint32_t sb_free), michael@0: uint32_t sb_threshold, michael@0: void *ulp_info) michael@0: { michael@0: struct socket *so; michael@0: michael@0: if ((protocol = IPPROTO_SCTP) && (SCTP_BASE_VAR(sctp_pcb_initialized) == 0)) { michael@0: errno = EPROTONOSUPPORT; michael@0: return (NULL); michael@0: } michael@0: if ((receive_cb == NULL) && michael@0: ((send_cb != NULL) || (sb_threshold != 0) || (ulp_info != NULL))) { michael@0: errno = EINVAL; michael@0: return (NULL); michael@0: } michael@0: if ((domain == AF_CONN) && (SCTP_BASE_VAR(conn_output) == NULL)) { michael@0: errno = EAFNOSUPPORT; michael@0: return (NULL); michael@0: } michael@0: errno = socreate(domain, &so, type, protocol); michael@0: if (errno) { michael@0: return (NULL); michael@0: } michael@0: /* michael@0: * The original socket call returns the file descriptor fd. michael@0: * td->td_retval[0] = fd. michael@0: * We are returning struct socket *so. michael@0: */ michael@0: register_recv_cb(so, receive_cb); michael@0: register_send_cb(so, sb_threshold, send_cb); michael@0: register_ulp_info(so, ulp_info); michael@0: return (so); michael@0: } michael@0: michael@0: michael@0: u_long sb_max = SB_MAX; michael@0: u_long sb_max_adj = michael@0: SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ michael@0: michael@0: static u_long sb_efficiency = 8; /* parameter for sbreserve() */ michael@0: michael@0: /* michael@0: * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't michael@0: * become limiting if buffering efficiency is near the normal case. michael@0: */ michael@0: int michael@0: sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so) michael@0: { michael@0: SOCKBUF_LOCK_ASSERT(sb); michael@0: sb->sb_mbmax = (u_int)min(cc * sb_efficiency, sb_max); michael@0: sb->sb_hiwat = cc; michael@0: if (sb->sb_lowat > (int)sb->sb_hiwat) michael@0: sb->sb_lowat = (int)sb->sb_hiwat; michael@0: return (1); michael@0: } michael@0: michael@0: static int michael@0: sbreserve(struct sockbuf *sb, u_long cc, struct socket *so) michael@0: { michael@0: int error; michael@0: michael@0: SOCKBUF_LOCK(sb); michael@0: error = sbreserve_locked(sb, cc, so); michael@0: SOCKBUF_UNLOCK(sb); michael@0: return (error); michael@0: } michael@0: michael@0: #if defined(__Userspace__) michael@0: int michael@0: soreserve(struct socket *so, u_long sndcc, u_long rcvcc) michael@0: { michael@0: SOCKBUF_LOCK(&so->so_snd); michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: so->so_snd.sb_hiwat = (uint32_t)sndcc; michael@0: so->so_rcv.sb_hiwat = (uint32_t)rcvcc; michael@0: michael@0: if (sbreserve_locked(&so->so_snd, sndcc, so) == 0) { michael@0: goto bad; michael@0: } michael@0: if (sbreserve_locked(&so->so_rcv, rcvcc, so) == 0) { michael@0: goto bad; michael@0: } michael@0: if (so->so_rcv.sb_lowat == 0) michael@0: so->so_rcv.sb_lowat = 1; michael@0: if (so->so_snd.sb_lowat == 0) michael@0: so->so_snd.sb_lowat = MCLBYTES; michael@0: if (so->so_snd.sb_lowat > (int)so->so_snd.sb_hiwat) michael@0: so->so_snd.sb_lowat = (int)so->so_snd.sb_hiwat; michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: SOCKBUF_UNLOCK(&so->so_snd); michael@0: return (0); michael@0: michael@0: bad: michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: SOCKBUF_UNLOCK(&so->so_snd); michael@0: return (ENOBUFS); michael@0: } michael@0: #else /* kernel version for reference */ michael@0: int michael@0: soreserve(struct socket *so, u_long sndcc, u_long rcvcc) michael@0: { michael@0: struct thread *td = curthread; michael@0: michael@0: SOCKBUF_LOCK(&so->so_snd); michael@0: SOCKBUF_LOCK(&so->so_rcv); michael@0: if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0) michael@0: goto bad; michael@0: if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0) michael@0: goto bad2; michael@0: if (so->so_rcv.sb_lowat == 0) michael@0: so->so_rcv.sb_lowat = 1; michael@0: if (so->so_snd.sb_lowat == 0) michael@0: so->so_snd.sb_lowat = MCLBYTES; michael@0: if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) michael@0: so->so_snd.sb_lowat = so->so_snd.sb_hiwat; michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: SOCKBUF_UNLOCK(&so->so_snd); michael@0: return (0); michael@0: bad2: michael@0: sbrelease_locked(&so->so_snd, so); michael@0: bad: michael@0: SOCKBUF_UNLOCK(&so->so_rcv); michael@0: SOCKBUF_UNLOCK(&so->so_snd); michael@0: return (ENOBUFS); michael@0: } michael@0: #endif michael@0: michael@0: michael@0: michael@0: michael@0: michael@0: /* Taken from /src/sys/kern/uipc_sockbuf.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: michael@0: #if defined(__Userspace__) michael@0: void michael@0: sowakeup(struct socket *so, struct sockbuf *sb) michael@0: { michael@0: michael@0: SOCKBUF_LOCK_ASSERT(sb); michael@0: michael@0: sb->sb_flags &= ~SB_SEL; michael@0: if (sb->sb_flags & SB_WAIT) { michael@0: sb->sb_flags &= ~SB_WAIT; michael@0: #if defined (__Userspace_os_Windows) michael@0: WakeAllConditionVariable(&(sb)->sb_cond); michael@0: #else michael@0: pthread_cond_broadcast(&(sb)->sb_cond); michael@0: #endif michael@0: } michael@0: SOCKBUF_UNLOCK(sb); michael@0: /*__Userspace__ what todo about so_upcall?*/ michael@0: michael@0: } michael@0: #else /* kernel version for reference */ michael@0: /* michael@0: * Wakeup processes waiting on a socket buffer. Do asynchronous notification michael@0: * via SIGIO if the socket has the SS_ASYNC flag set. michael@0: * michael@0: * Called with the socket buffer lock held; will release the lock by the end michael@0: * of the function. This allows the caller to acquire the socket buffer lock michael@0: * while testing for the need for various sorts of wakeup and hold it through michael@0: * to the point where it's no longer required. We currently hold the lock michael@0: * through calls out to other subsystems (with the exception of kqueue), and michael@0: * then release it to avoid lock order issues. It's not clear that's michael@0: * correct. michael@0: */ michael@0: void michael@0: sowakeup(struct socket *so, struct sockbuf *sb) michael@0: { michael@0: michael@0: SOCKBUF_LOCK_ASSERT(sb); michael@0: michael@0: selwakeuppri(&sb->sb_sel, PSOCK); michael@0: sb->sb_flags &= ~SB_SEL; michael@0: if (sb->sb_flags & SB_WAIT) { michael@0: sb->sb_flags &= ~SB_WAIT; michael@0: wakeup(&sb->sb_cc); michael@0: } michael@0: KNOTE_LOCKED(&sb->sb_sel.si_note, 0); michael@0: SOCKBUF_UNLOCK(sb); michael@0: if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) michael@0: pgsigio(&so->so_sigio, SIGIO, 0); michael@0: if (sb->sb_flags & SB_UPCALL) michael@0: (*so->so_upcall)(so, so->so_upcallarg, M_NOWAIT); michael@0: if (sb->sb_flags & SB_AIO) michael@0: aio_swake(so, sb); michael@0: mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED); michael@0: } michael@0: #endif michael@0: michael@0: michael@0: michael@0: /* Taken from /src/sys/kern/uipc_socket.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: michael@0: int michael@0: sobind(struct socket *so, struct sockaddr *nam) michael@0: { michael@0: switch (nam->sa_family) { michael@0: #if defined(INET) michael@0: case AF_INET: michael@0: return (sctp_bind(so, nam)); michael@0: #endif michael@0: #if defined(INET6) michael@0: case AF_INET6: michael@0: return (sctp6_bind(so, nam, NULL)); michael@0: #endif michael@0: case AF_CONN: michael@0: return (sctpconn_bind(so, nam)); michael@0: default: michael@0: return EAFNOSUPPORT; michael@0: } michael@0: } michael@0: michael@0: /* Taken from /src/sys/kern/uipc_syscalls.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: michael@0: int michael@0: usrsctp_bind(struct socket *so, struct sockaddr *name, int namelen) michael@0: { michael@0: struct sockaddr *sa; michael@0: michael@0: if (so == NULL) { michael@0: errno = EBADF; michael@0: return (-1); michael@0: } michael@0: if ((errno = getsockaddr(&sa, (caddr_t)name, namelen)) != 0) michael@0: return (-1); michael@0: michael@0: errno = sobind(so, sa); michael@0: FREE(sa, M_SONAME); michael@0: if (errno) { michael@0: return (-1); michael@0: } else { michael@0: return (0); michael@0: } michael@0: } michael@0: michael@0: int michael@0: userspace_bind(struct socket *so, struct sockaddr *name, int namelen) michael@0: { michael@0: return (usrsctp_bind(so, name, namelen)); michael@0: } michael@0: michael@0: /* Taken from /src/sys/kern/uipc_socket.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: michael@0: int michael@0: solisten(struct socket *so, int backlog) michael@0: { michael@0: if (so == NULL) { michael@0: return (EBADF); michael@0: } else { michael@0: return (sctp_listen(so, backlog, NULL)); michael@0: } michael@0: } michael@0: michael@0: michael@0: int michael@0: solisten_proto_check(struct socket *so) michael@0: { michael@0: michael@0: SOCK_LOCK_ASSERT(so); michael@0: michael@0: if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | michael@0: SS_ISDISCONNECTING)) michael@0: return (EINVAL); michael@0: return (0); michael@0: } michael@0: michael@0: static int somaxconn = SOMAXCONN; michael@0: michael@0: void michael@0: solisten_proto(struct socket *so, int backlog) michael@0: { michael@0: michael@0: SOCK_LOCK_ASSERT(so); michael@0: michael@0: if (backlog < 0 || backlog > somaxconn) michael@0: backlog = somaxconn; michael@0: so->so_qlimit = backlog; michael@0: so->so_options |= SCTP_SO_ACCEPTCONN; michael@0: } michael@0: michael@0: michael@0: michael@0: michael@0: /* Taken from /src/sys/kern/uipc_syscalls.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: michael@0: int michael@0: usrsctp_listen(struct socket *so, int backlog) michael@0: { michael@0: errno = solisten(so, backlog); michael@0: if (errno) { michael@0: return (-1); michael@0: } else { michael@0: return (0); michael@0: } michael@0: } michael@0: michael@0: int michael@0: userspace_listen(struct socket *so, int backlog) michael@0: { michael@0: return (usrsctp_listen(so, backlog)); michael@0: } michael@0: michael@0: /* Taken from /src/sys/kern/uipc_socket.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: michael@0: int michael@0: soaccept(struct socket *so, struct sockaddr **nam) michael@0: { michael@0: int error; michael@0: michael@0: SOCK_LOCK(so); michael@0: KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF")); michael@0: so->so_state &= ~SS_NOFDREF; michael@0: SOCK_UNLOCK(so); michael@0: error = sctp_accept(so, nam); michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: michael@0: /* Taken from /src/sys/kern/uipc_syscalls.c michael@0: * kern_accept modified for __Userspace__ michael@0: */ michael@0: int michael@0: user_accept(struct socket *head, struct sockaddr **name, socklen_t *namelen, struct socket **ptr_accept_ret_sock) michael@0: { michael@0: struct sockaddr *sa = NULL; michael@0: int error; michael@0: struct socket *so = NULL; michael@0: michael@0: michael@0: if (name) { michael@0: *name = NULL; michael@0: } michael@0: michael@0: if ((head->so_options & SCTP_SO_ACCEPTCONN) == 0) { michael@0: error = EINVAL; michael@0: goto done; michael@0: } michael@0: michael@0: ACCEPT_LOCK(); michael@0: if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) { michael@0: ACCEPT_UNLOCK(); michael@0: error = EWOULDBLOCK; michael@0: goto noconnection; michael@0: } michael@0: while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) { michael@0: if (head->so_rcv.sb_state & SBS_CANTRCVMORE) { michael@0: head->so_error = ECONNABORTED; michael@0: break; michael@0: } michael@0: #if defined (__Userspace_os_Windows) michael@0: if (SleepConditionVariableCS(&accept_cond, &accept_mtx, INFINITE)) michael@0: error = 0; michael@0: else michael@0: error = GetLastError(); michael@0: #else michael@0: error = pthread_cond_wait(&accept_cond, &accept_mtx); michael@0: #endif michael@0: if (error) { michael@0: ACCEPT_UNLOCK(); michael@0: goto noconnection; michael@0: } michael@0: } michael@0: if (head->so_error) { michael@0: error = head->so_error; michael@0: head->so_error = 0; michael@0: ACCEPT_UNLOCK(); michael@0: goto noconnection; michael@0: } michael@0: so = TAILQ_FIRST(&head->so_comp); michael@0: KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP")); michael@0: KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP")); michael@0: michael@0: /* michael@0: * Before changing the flags on the socket, we have to bump the michael@0: * reference count. Otherwise, if the protocol calls sofree(), michael@0: * the socket will be released due to a zero refcount. michael@0: */ michael@0: SOCK_LOCK(so); /* soref() and so_state update */ michael@0: soref(so); /* file descriptor reference */ michael@0: michael@0: TAILQ_REMOVE(&head->so_comp, so, so_list); michael@0: head->so_qlen--; michael@0: so->so_state |= (head->so_state & SS_NBIO); michael@0: so->so_qstate &= ~SQ_COMP; michael@0: so->so_head = NULL; michael@0: SOCK_UNLOCK(so); michael@0: ACCEPT_UNLOCK(); michael@0: michael@0: michael@0: /* michael@0: * The original accept returns fd value via td->td_retval[0] = fd; michael@0: * we will return the socket for accepted connection. michael@0: */ michael@0: michael@0: error = soaccept(so, &sa); michael@0: if (error) { michael@0: /* michael@0: * return a namelen of zero for older code which might michael@0: * ignore the return value from accept. michael@0: */ michael@0: if (name) michael@0: *namelen = 0; michael@0: goto noconnection; michael@0: } michael@0: if (sa == NULL) { michael@0: if (name) michael@0: *namelen = 0; michael@0: goto done; michael@0: } michael@0: if (name) { michael@0: #ifdef HAVE_SA_LEN michael@0: /* check sa_len before it is destroyed */ michael@0: if (*namelen > sa->sa_len) { michael@0: *namelen = sa->sa_len; michael@0: } michael@0: #else michael@0: socklen_t sa_len; michael@0: michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sa_len = sizeof(struct sockaddr_in); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sa_len = sizeof(struct sockaddr_in6); michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: sa_len = sizeof(struct sockaddr_conn); michael@0: break; michael@0: default: michael@0: sa_len = 0; michael@0: break; michael@0: } michael@0: if (*namelen > sa_len) { michael@0: *namelen = sa_len; michael@0: } michael@0: #endif michael@0: *name = sa; michael@0: sa = NULL; michael@0: } michael@0: noconnection: michael@0: if (sa) { michael@0: FREE(sa, M_SONAME); michael@0: } michael@0: michael@0: done: michael@0: *ptr_accept_ret_sock = so; michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: michael@0: /* Taken from /src/sys/kern/uipc_syscalls.c michael@0: * and modified for __Userspace__ michael@0: */ michael@0: /* michael@0: * accept1() michael@0: */ michael@0: static int michael@0: accept1(struct socket *so, struct sockaddr *aname, socklen_t *anamelen, struct socket **ptr_accept_ret_sock) michael@0: { michael@0: struct sockaddr *name; michael@0: socklen_t namelen; michael@0: int error; michael@0: michael@0: if (so == NULL) { michael@0: return (EBADF); michael@0: } michael@0: if (aname == NULL) { michael@0: return (user_accept(so, NULL, NULL, ptr_accept_ret_sock)); michael@0: } michael@0: michael@0: error = copyin(anamelen, &namelen, sizeof (namelen)); michael@0: if (error) michael@0: return (error); michael@0: michael@0: error = user_accept(so, &name, &namelen, ptr_accept_ret_sock); michael@0: michael@0: /* michael@0: * return a namelen of zero for older code which might michael@0: * ignore the return value from accept. michael@0: */ michael@0: if (error) { michael@0: (void) copyout(&namelen, michael@0: anamelen, sizeof(*anamelen)); michael@0: return (error); michael@0: } michael@0: michael@0: if (error == 0 && name != NULL) { michael@0: error = copyout(name, aname, namelen); michael@0: } michael@0: if (error == 0) { michael@0: error = copyout(&namelen, anamelen, sizeof(namelen)); michael@0: } michael@0: michael@0: if (name) { michael@0: FREE(name, M_SONAME); michael@0: } michael@0: return (error); michael@0: } michael@0: michael@0: struct socket * michael@0: usrsctp_accept(struct socket *so, struct sockaddr *aname, socklen_t *anamelen) michael@0: { michael@0: struct socket *accept_return_sock; michael@0: michael@0: errno = accept1(so, aname, anamelen, &accept_return_sock); michael@0: if (errno) { michael@0: return (NULL); michael@0: } else { michael@0: return (accept_return_sock); michael@0: } michael@0: } michael@0: michael@0: struct socket * michael@0: userspace_accept(struct socket *so, struct sockaddr *aname, socklen_t *anamelen) michael@0: { michael@0: return (usrsctp_accept(so, aname, anamelen)); michael@0: } michael@0: michael@0: struct socket * michael@0: usrsctp_peeloff(struct socket *head, sctp_assoc_t id) michael@0: { michael@0: struct socket *so; michael@0: michael@0: if ((errno = sctp_can_peel_off(head, id)) != 0) { michael@0: return (NULL); michael@0: } michael@0: if ((so = sonewconn(head, SS_ISCONNECTED)) == NULL) { michael@0: return (NULL); michael@0: } michael@0: ACCEPT_LOCK(); michael@0: SOCK_LOCK(so); michael@0: soref(so); michael@0: TAILQ_REMOVE(&head->so_comp, so, so_list); michael@0: head->so_qlen--; michael@0: so->so_state |= (head->so_state & SS_NBIO); michael@0: so->so_qstate &= ~SQ_COMP; michael@0: so->so_head = NULL; michael@0: SOCK_UNLOCK(so); michael@0: ACCEPT_UNLOCK(); michael@0: if ((errno = sctp_do_peeloff(head, so, id)) != 0) { michael@0: so->so_count = 0; michael@0: sodealloc(so); michael@0: return (NULL); michael@0: } michael@0: return (so); michael@0: } michael@0: michael@0: int michael@0: sodisconnect(struct socket *so) michael@0: { michael@0: int error; michael@0: michael@0: if ((so->so_state & SS_ISCONNECTED) == 0) michael@0: return (ENOTCONN); michael@0: if (so->so_state & SS_ISDISCONNECTING) michael@0: return (EALREADY); michael@0: error = sctp_disconnect(so); michael@0: return (error); michael@0: } michael@0: michael@0: int michael@0: usrsctp_set_non_blocking(struct socket *so, int onoff) michael@0: { michael@0: if (so == NULL) { michael@0: errno = EBADF; michael@0: return (-1); michael@0: } michael@0: SOCK_LOCK(so); michael@0: if (onoff != 0) { michael@0: so->so_state |= SS_NBIO; michael@0: } else { michael@0: so->so_state &= ~SS_NBIO; michael@0: } michael@0: SOCK_UNLOCK(so); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: usrsctp_get_non_blocking(struct socket *so) michael@0: { michael@0: int result; michael@0: michael@0: if (so == NULL) { michael@0: errno = EBADF; michael@0: return (-1); michael@0: } michael@0: SOCK_LOCK(so); michael@0: if (so->so_state | SS_NBIO) { michael@0: result = 1; michael@0: } else { michael@0: result = 0; michael@0: } michael@0: SOCK_UNLOCK(so); michael@0: return (result); michael@0: } michael@0: michael@0: int michael@0: soconnect(struct socket *so, struct sockaddr *nam) michael@0: { michael@0: int error; michael@0: michael@0: if (so->so_options & SCTP_SO_ACCEPTCONN) michael@0: return (EOPNOTSUPP); michael@0: /* michael@0: * If protocol is connection-based, can only connect once. michael@0: * Otherwise, if connected, try to disconnect first. This allows michael@0: * user to disconnect by connecting to, e.g., a null address. michael@0: */ michael@0: if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && (error = sodisconnect(so))) { michael@0: error = EISCONN; michael@0: } else { michael@0: /* michael@0: * Prevent accumulated error from previous connection from michael@0: * biting us. michael@0: */ michael@0: so->so_error = 0; michael@0: switch (nam->sa_family) { michael@0: #if defined(INET) michael@0: case AF_INET: michael@0: error = sctp_connect(so, nam); michael@0: break; michael@0: #endif michael@0: #if defined(INET6) michael@0: case AF_INET6: michael@0: error = sctp6_connect(so, nam); michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: error = sctpconn_connect(so, nam); michael@0: break; michael@0: default: michael@0: error = EAFNOSUPPORT; michael@0: } michael@0: } michael@0: michael@0: return (error); michael@0: } michael@0: michael@0: michael@0: michael@0: int user_connect(struct socket *so, struct sockaddr *sa) michael@0: { michael@0: int error; michael@0: int interrupted = 0; michael@0: michael@0: if (so == NULL) { michael@0: error = EBADF; michael@0: goto done1; michael@0: } michael@0: if (so->so_state & SS_ISCONNECTING) { michael@0: error = EALREADY; michael@0: goto done1; michael@0: } michael@0: michael@0: error = soconnect(so, sa); michael@0: if (error) { michael@0: goto bad; michael@0: } michael@0: if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { michael@0: error = EINPROGRESS; michael@0: goto done1; michael@0: } michael@0: michael@0: SOCK_LOCK(so); michael@0: while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { michael@0: #if defined (__Userspace_os_Windows) michael@0: if (SleepConditionVariableCS(SOCK_COND(so), SOCK_MTX(so), INFINITE)) michael@0: error = 0; michael@0: else michael@0: error = -1; michael@0: #else michael@0: error = pthread_cond_wait(SOCK_COND(so), SOCK_MTX(so)); michael@0: #endif michael@0: if (error) { michael@0: #if defined(__Userspace_os_NetBSD) michael@0: if (error == EINTR) { michael@0: #else michael@0: if (error == EINTR || error == ERESTART) { michael@0: #endif michael@0: interrupted = 1; michael@0: } michael@0: break; michael@0: } michael@0: } michael@0: if (error == 0) { michael@0: error = so->so_error; michael@0: so->so_error = 0; michael@0: } michael@0: SOCK_UNLOCK(so); michael@0: michael@0: bad: michael@0: if (!interrupted) { michael@0: so->so_state &= ~SS_ISCONNECTING; michael@0: } michael@0: #if !defined(__Userspace_os_NetBSD) michael@0: if (error == ERESTART) { michael@0: error = EINTR; michael@0: } michael@0: #endif michael@0: done1: michael@0: return (error); michael@0: } michael@0: michael@0: int usrsctp_connect(struct socket *so, struct sockaddr *name, int namelen) michael@0: { michael@0: struct sockaddr *sa; michael@0: michael@0: errno = getsockaddr(&sa, (caddr_t)name, namelen); michael@0: if (errno) michael@0: return (-1); michael@0: michael@0: errno = user_connect(so, sa); michael@0: FREE(sa, M_SONAME); michael@0: if (errno) { michael@0: return (-1); michael@0: } else { michael@0: return (0); michael@0: } michael@0: } michael@0: michael@0: int userspace_connect(struct socket *so, struct sockaddr *name, int namelen) michael@0: { michael@0: return (usrsctp_connect(so, name, namelen)); michael@0: } michael@0: michael@0: #define SCTP_STACK_BUF_SIZE 2048 michael@0: michael@0: void michael@0: usrsctp_close(struct socket *so) { michael@0: if (so != NULL) { michael@0: if (so->so_options & SCTP_SO_ACCEPTCONN) { michael@0: struct socket *sp; michael@0: michael@0: ACCEPT_LOCK(); michael@0: while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) { michael@0: TAILQ_REMOVE(&so->so_comp, sp, so_list); michael@0: so->so_qlen--; michael@0: sp->so_qstate &= ~SQ_COMP; michael@0: sp->so_head = NULL; michael@0: ACCEPT_UNLOCK(); michael@0: soabort(sp); michael@0: ACCEPT_LOCK(); michael@0: } michael@0: ACCEPT_UNLOCK(); michael@0: } michael@0: ACCEPT_LOCK(); michael@0: SOCK_LOCK(so); michael@0: sorele(so); michael@0: } michael@0: } michael@0: michael@0: void michael@0: userspace_close(struct socket *so) michael@0: { michael@0: usrsctp_close(so); michael@0: } michael@0: michael@0: int michael@0: usrsctp_shutdown(struct socket *so, int how) michael@0: { michael@0: if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: if (so == NULL) { michael@0: errno = EBADF; michael@0: return (-1); michael@0: } michael@0: sctp_flush(so, how); michael@0: if (how != SHUT_WR) michael@0: socantrcvmore(so); michael@0: if (how != SHUT_RD) { michael@0: errno = sctp_shutdown(so); michael@0: if (errno) { michael@0: return (-1); michael@0: } else { michael@0: return (0); michael@0: } michael@0: } michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: userspace_shutdown(struct socket *so, int how) michael@0: { michael@0: return (usrsctp_shutdown(so, how)); michael@0: } michael@0: michael@0: int michael@0: usrsctp_finish(void) michael@0: { michael@0: if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { michael@0: return (0); michael@0: } michael@0: if (SCTP_INP_INFO_TRYLOCK()) { michael@0: if (!LIST_EMPTY(&SCTP_BASE_INFO(listhead))) { michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: return (-1); michael@0: } michael@0: SCTP_INP_INFO_RUNLOCK(); michael@0: } else { michael@0: return (-1); michael@0: } michael@0: sctp_finish(); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: userspace_finish(void) michael@0: { michael@0: return (usrsctp_finish()); michael@0: } michael@0: michael@0: /* needed from sctp_usrreq.c */ michael@0: int michael@0: sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize, void *p); michael@0: michael@0: int michael@0: usrsctp_setsockopt(struct socket *so, int level, int option_name, michael@0: const void *option_value, socklen_t option_len) michael@0: { michael@0: if (so == NULL) { michael@0: errno = EBADF; michael@0: return (-1); michael@0: } michael@0: switch (level) { michael@0: case SOL_SOCKET: michael@0: { michael@0: switch (option_name) { michael@0: case SO_RCVBUF: michael@0: if (option_len < (socklen_t)sizeof(int)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } else { michael@0: int *buf_size; michael@0: michael@0: buf_size = (int *)option_value; michael@0: if (*buf_size < 1) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: sbreserve(&so->so_rcv, (u_long)*buf_size, so); michael@0: return (0); michael@0: } michael@0: break; michael@0: case SO_SNDBUF: michael@0: if (option_len < (socklen_t)sizeof(int)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } else { michael@0: int *buf_size; michael@0: michael@0: buf_size = (int *)option_value; michael@0: if (*buf_size < 1) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: sbreserve(&so->so_snd, (u_long)*buf_size, so); michael@0: return (0); michael@0: } michael@0: break; michael@0: case SO_LINGER: michael@0: if (option_len < (socklen_t)sizeof(struct linger)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } else { michael@0: struct linger *l; michael@0: michael@0: l = (struct linger *)option_value; michael@0: so->so_linger = l->l_linger; michael@0: if (l->l_onoff) { michael@0: so->so_options |= SCTP_SO_LINGER; michael@0: } else { michael@0: so->so_options &= ~SCTP_SO_LINGER; michael@0: } michael@0: return (0); michael@0: } michael@0: default: michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: } michael@0: case IPPROTO_SCTP: michael@0: errno = sctp_setopt(so, option_name, (void *) option_value, (size_t)option_len, NULL); michael@0: if (errno) { michael@0: return (-1); michael@0: } else { michael@0: return (0); michael@0: } michael@0: default: michael@0: errno = ENOPROTOOPT; michael@0: return (-1); michael@0: } michael@0: } michael@0: michael@0: int michael@0: userspace_setsockopt(struct socket *so, int level, int option_name, michael@0: const void *option_value, socklen_t option_len) michael@0: { michael@0: return (usrsctp_setsockopt(so, level, option_name, option_value, option_len)); michael@0: } michael@0: michael@0: /* needed from sctp_usrreq.c */ michael@0: int michael@0: sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize, michael@0: void *p); michael@0: michael@0: int michael@0: usrsctp_getsockopt(struct socket *so, int level, int option_name, michael@0: void *option_value, socklen_t *option_len) michael@0: { michael@0: if (so == NULL) { michael@0: errno = EBADF; michael@0: return (-1); michael@0: } michael@0: if (option_len == NULL) { michael@0: errno = EFAULT; michael@0: return (-1); michael@0: } michael@0: switch (level) { michael@0: case SOL_SOCKET: michael@0: switch (option_name) { michael@0: case SO_RCVBUF: michael@0: if (*option_len < (socklen_t)sizeof(int)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } else { michael@0: int *buf_size; michael@0: michael@0: buf_size = (int *)option_value; michael@0: *buf_size = so->so_rcv.sb_hiwat;; michael@0: *option_len = (socklen_t)sizeof(int); michael@0: return (0); michael@0: } michael@0: break; michael@0: case SO_SNDBUF: michael@0: if (*option_len < (socklen_t)sizeof(int)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } else { michael@0: int *buf_size; michael@0: michael@0: buf_size = (int *)option_value; michael@0: *buf_size = so->so_snd.sb_hiwat; michael@0: *option_len = (socklen_t)sizeof(int); michael@0: return (0); michael@0: } michael@0: break; michael@0: case SO_LINGER: michael@0: if (*option_len < (socklen_t)sizeof(struct linger)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } else { michael@0: struct linger *l; michael@0: michael@0: l = (struct linger *)option_value; michael@0: l->l_linger = so->so_linger; michael@0: if (so->so_options & SCTP_SO_LINGER) { michael@0: l->l_onoff = 1; michael@0: } else { michael@0: l->l_onoff = 0; michael@0: } michael@0: *option_len = (socklen_t)sizeof(struct linger); michael@0: return (0); michael@0: } michael@0: default: michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: case IPPROTO_SCTP: michael@0: { michael@0: size_t len; michael@0: michael@0: len = (size_t)*option_len; michael@0: errno = sctp_getopt(so, option_name, option_value, &len, NULL); michael@0: *option_len = (socklen_t)len; michael@0: if (errno) { michael@0: return (-1); michael@0: } else { michael@0: return (0); michael@0: } michael@0: } michael@0: default: michael@0: errno = ENOPROTOOPT; michael@0: return (-1); michael@0: } michael@0: } michael@0: michael@0: int michael@0: userspace_getsockopt(struct socket *so, int level, int option_name, michael@0: void *option_value, socklen_t *option_len) michael@0: { michael@0: return (usrsctp_getsockopt(so, level, option_name, option_value, option_len)); michael@0: } michael@0: michael@0: int michael@0: usrsctp_bindx(struct socket *so, struct sockaddr *addrs, int addrcnt, int flags) michael@0: { michael@0: struct sctp_getaddresses *gaddrs; michael@0: struct sockaddr *sa; michael@0: #ifdef INET michael@0: struct sockaddr_in *sin; michael@0: #endif michael@0: #ifdef INET6 michael@0: struct sockaddr_in6 *sin6; michael@0: #endif michael@0: int i; michael@0: size_t argsz; michael@0: #if defined(INET) || defined(INET6) michael@0: uint16_t sport = 0; michael@0: #endif michael@0: michael@0: /* validate the flags */ michael@0: if ((flags != SCTP_BINDX_ADD_ADDR) && michael@0: (flags != SCTP_BINDX_REM_ADDR)) { michael@0: errno = EFAULT; michael@0: return (-1); michael@0: } michael@0: /* validate the address count and list */ michael@0: if ((addrcnt <= 0) || (addrs == NULL)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: /* First pre-screen the addresses */ michael@0: sa = addrs; michael@0: for (i = 0; i < addrcnt; i++) { michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #ifdef HAVE_SA_LEN michael@0: if (sa->sa_len != sizeof(struct sockaddr_in)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: #endif michael@0: sin = (struct sockaddr_in *)sa; michael@0: if (sin->sin_port) { michael@0: /* non-zero port, check or save */ michael@0: if (sport) { michael@0: /* Check against our port */ michael@0: if (sport != sin->sin_port) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: } else { michael@0: /* save off the port */ michael@0: sport = sin->sin_port; michael@0: } michael@0: } michael@0: #ifndef HAVE_SA_LEN michael@0: sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in)); michael@0: #endif michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #ifdef HAVE_SA_LEN michael@0: if (sa->sa_len != sizeof(struct sockaddr_in6)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: #endif michael@0: sin6 = (struct sockaddr_in6 *)sa; michael@0: if (sin6->sin6_port) { michael@0: /* non-zero port, check or save */ michael@0: if (sport) { michael@0: /* Check against our port */ michael@0: if (sport != sin6->sin6_port) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: } else { michael@0: /* save off the port */ michael@0: sport = sin6->sin6_port; michael@0: } michael@0: } michael@0: #ifndef HAVE_SA_LEN michael@0: sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6)); michael@0: #endif michael@0: break; michael@0: #endif michael@0: default: michael@0: /* Invalid address family specified. */ michael@0: errno = EAFNOSUPPORT; michael@0: return (-1); michael@0: } michael@0: #ifdef HAVE_SA_LEN michael@0: sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); michael@0: #endif michael@0: } michael@0: argsz = sizeof(struct sctp_getaddresses) + michael@0: sizeof(struct sockaddr_storage); michael@0: if ((gaddrs = (struct sctp_getaddresses *)malloc(argsz)) == NULL) { michael@0: errno = ENOMEM; michael@0: return (-1); michael@0: } michael@0: sa = addrs; michael@0: for (i = 0; i < addrcnt; i++) { michael@0: #ifndef HAVE_SA_LEN michael@0: size_t sa_len; michael@0: #endif michael@0: memset(gaddrs, 0, argsz); michael@0: gaddrs->sget_assoc_id = 0; michael@0: #ifdef HAVE_SA_LEN michael@0: memcpy(gaddrs->addr, sa, sa->sa_len); michael@0: if (usrsctp_setsockopt(so, IPPROTO_SCTP, flags, gaddrs, (socklen_t)argsz) != 0) { michael@0: free(gaddrs); michael@0: return (-1); michael@0: } michael@0: sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); michael@0: #else michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sa_len = sizeof(struct sockaddr_in); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sa_len = sizeof(struct sockaddr_in6); michael@0: break; michael@0: #endif michael@0: default: michael@0: sa_len = 0; michael@0: break; michael@0: } michael@0: memcpy(gaddrs->addr, sa, sa_len); michael@0: /* michael@0: * Now, if there was a port mentioned, assure that the michael@0: * first address has that port to make sure it fails or michael@0: * succeeds correctly. michael@0: */ michael@0: if ((i == 0) && (sport != 0)) { michael@0: switch (gaddrs->addr->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sin = (struct sockaddr_in *)gaddrs->addr; michael@0: sin->sin_port = sport; michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sin6 = (struct sockaddr_in6 *)gaddrs->addr; michael@0: sin6->sin6_port = sport; michael@0: break; michael@0: #endif michael@0: } michael@0: } michael@0: if (usrsctp_setsockopt(so, IPPROTO_SCTP, flags, gaddrs, (socklen_t)argsz) != 0) { michael@0: free(gaddrs); michael@0: return (-1); michael@0: } michael@0: sa = (struct sockaddr *)((caddr_t)sa + sa_len); michael@0: #endif michael@0: } michael@0: free(gaddrs); michael@0: return (0); michael@0: } michael@0: michael@0: int michael@0: usrsctp_connectx(struct socket *so, michael@0: const struct sockaddr *addrs, int addrcnt, michael@0: sctp_assoc_t *id) michael@0: { michael@0: #if defined(INET) || defined(INET6) michael@0: char buf[SCTP_STACK_BUF_SIZE]; michael@0: int i, ret, cnt, *aa; michael@0: char *cpto; michael@0: const struct sockaddr *at; michael@0: sctp_assoc_t *p_id; michael@0: size_t len = sizeof(int); michael@0: michael@0: /* validate the address count and list */ michael@0: if ((addrs == NULL) || (addrcnt <= 0)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: at = addrs; michael@0: cnt = 0; michael@0: cpto = ((caddr_t)buf + sizeof(int)); michael@0: /* validate all the addresses and get the size */ michael@0: for (i = 0; i < addrcnt; i++) { michael@0: switch (at->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: #ifdef HAVE_SA_LEN michael@0: if (at->sa_len != sizeof(struct sockaddr_in)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: #endif michael@0: memcpy(cpto, at, sizeof(struct sockaddr_in)); michael@0: cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); michael@0: len += sizeof(struct sockaddr_in); michael@0: at = (struct sockaddr *)((caddr_t)at + sizeof(struct sockaddr_in)); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: #ifdef HAVE_SA_LEN michael@0: if (at->sa_len != sizeof(struct sockaddr_in6)) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: #endif michael@0: if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) { michael@0: in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at); michael@0: cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); michael@0: len += sizeof(struct sockaddr_in); michael@0: } else { michael@0: memcpy(cpto, at, sizeof(struct sockaddr_in6)); michael@0: cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6)); michael@0: len += sizeof(struct sockaddr_in6); michael@0: } michael@0: at = (struct sockaddr *)((caddr_t)at + sizeof(struct sockaddr_in6)); michael@0: break; michael@0: #endif michael@0: default: michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: if (len > (sizeof(buf) - sizeof(int))) { michael@0: /* Never enough memory */ michael@0: errno = E2BIG; michael@0: return (-1); michael@0: } michael@0: cnt++; michael@0: } michael@0: /* do we have any? */ michael@0: if (cnt == 0) { michael@0: errno = EINVAL; michael@0: return (-1); michael@0: } michael@0: aa = (int *)buf; michael@0: *aa = cnt; michael@0: ret = usrsctp_setsockopt(so, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf, (socklen_t)len); michael@0: if ((ret == 0) && id) { michael@0: p_id = (sctp_assoc_t *)buf; michael@0: *id = *p_id; michael@0: } michael@0: return (ret); michael@0: #else michael@0: errno = EINVAL; michael@0: return (-1); michael@0: #endif michael@0: } michael@0: michael@0: int michael@0: usrsctp_getpaddrs(struct socket *so, sctp_assoc_t id, struct sockaddr **raddrs) michael@0: { michael@0: struct sctp_getaddresses *addrs; michael@0: struct sockaddr *sa; michael@0: sctp_assoc_t asoc; michael@0: caddr_t lim; michael@0: socklen_t opt_len; michael@0: int cnt; michael@0: michael@0: if (raddrs == NULL) { michael@0: errno = EFAULT; michael@0: return (-1); michael@0: } michael@0: asoc = id; michael@0: opt_len = (socklen_t)sizeof(sctp_assoc_t); michael@0: if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE, &asoc, &opt_len) != 0) { michael@0: return (-1); michael@0: } michael@0: /* size required is returned in 'asoc' */ michael@0: opt_len = (socklen_t)((size_t)asoc + sizeof(struct sctp_getaddresses)); michael@0: addrs = calloc(1, (size_t)opt_len); michael@0: if (addrs == NULL) { michael@0: errno = ENOMEM; michael@0: return (-1); michael@0: } michael@0: addrs->sget_assoc_id = id; michael@0: /* Now lets get the array of addresses */ michael@0: if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES, addrs, &opt_len) != 0) { michael@0: free(addrs); michael@0: return (-1); michael@0: } michael@0: *raddrs = (struct sockaddr *)&addrs->addr[0]; michael@0: cnt = 0; michael@0: sa = (struct sockaddr *)&addrs->addr[0]; michael@0: lim = (caddr_t)addrs + opt_len; michael@0: #ifdef HAVE_SA_LEN michael@0: while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { michael@0: sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); michael@0: #else michael@0: while ((caddr_t)sa < lim) { michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in)); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6)); michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_conn)); michael@0: break; michael@0: default: michael@0: return (cnt); michael@0: break; michael@0: } michael@0: #endif michael@0: cnt++; michael@0: } michael@0: return (cnt); michael@0: } michael@0: michael@0: void michael@0: usrsctp_freepaddrs(struct sockaddr *addrs) michael@0: { michael@0: /* Take away the hidden association id */ michael@0: void *fr_addr; michael@0: michael@0: fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); michael@0: /* Now free it */ michael@0: free(fr_addr); michael@0: } michael@0: michael@0: int michael@0: usrsctp_getladdrs(struct socket *so, sctp_assoc_t id, struct sockaddr **raddrs) michael@0: { michael@0: struct sctp_getaddresses *addrs; michael@0: caddr_t lim; michael@0: struct sockaddr *sa; michael@0: size_t size_of_addresses; michael@0: socklen_t opt_len; michael@0: int cnt; michael@0: michael@0: if (raddrs == NULL) { michael@0: errno = EFAULT; michael@0: return (-1); michael@0: } michael@0: size_of_addresses = 0; michael@0: opt_len = (socklen_t)sizeof(int); michael@0: if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE, &size_of_addresses, &opt_len) != 0) { michael@0: errno = ENOMEM; michael@0: return (-1); michael@0: } michael@0: if (size_of_addresses == 0) { michael@0: errno = ENOTCONN; michael@0: return (-1); michael@0: } michael@0: opt_len = (socklen_t)(size_of_addresses + michael@0: sizeof(struct sockaddr_storage) + michael@0: sizeof(struct sctp_getaddresses)); michael@0: addrs = calloc(1, (size_t)opt_len); michael@0: if (addrs == NULL) { michael@0: errno = ENOMEM; michael@0: return (-1); michael@0: } michael@0: addrs->sget_assoc_id = id; michael@0: /* Now lets get the array of addresses */ michael@0: if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs, &opt_len) != 0) { michael@0: free(addrs); michael@0: errno = ENOMEM; michael@0: return (-1); michael@0: } michael@0: *raddrs = (struct sockaddr *)&addrs->addr[0]; michael@0: cnt = 0; michael@0: sa = (struct sockaddr *)&addrs->addr[0]; michael@0: lim = (caddr_t)addrs + opt_len; michael@0: #ifdef HAVE_SA_LEN michael@0: while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { michael@0: sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); michael@0: #else michael@0: while ((caddr_t)sa < lim) { michael@0: switch (sa->sa_family) { michael@0: #ifdef INET michael@0: case AF_INET: michael@0: sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in)); michael@0: break; michael@0: #endif michael@0: #ifdef INET6 michael@0: case AF_INET6: michael@0: sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6)); michael@0: break; michael@0: #endif michael@0: case AF_CONN: michael@0: sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_conn)); michael@0: break; michael@0: default: michael@0: return (cnt); michael@0: break; michael@0: } michael@0: #endif michael@0: cnt++; michael@0: } michael@0: return (cnt); michael@0: } michael@0: michael@0: void michael@0: usrsctp_freeladdrs(struct sockaddr *addrs) michael@0: { michael@0: /* Take away the hidden association id */ michael@0: void *fr_addr; michael@0: michael@0: fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); michael@0: /* Now free it */ michael@0: free(fr_addr); michael@0: } michael@0: michael@0: #ifdef INET michael@0: void michael@0: sctp_userspace_ip_output(int *result, struct mbuf *o_pak, michael@0: sctp_route_t *ro, void *stcb, michael@0: uint32_t vrf_id) michael@0: { michael@0: struct mbuf *m; michael@0: struct mbuf *m_orig; michael@0: int iovcnt; michael@0: int send_len; michael@0: int len; michael@0: int send_count; michael@0: struct ip *ip; michael@0: struct udphdr *udp; michael@0: #if !defined (__Userspace_os_Windows) michael@0: int res; michael@0: #endif michael@0: struct sockaddr_in dst; michael@0: #if defined (__Userspace_os_Windows) michael@0: WSAMSG win_msg_hdr; michael@0: int win_sent_len; michael@0: WSABUF send_iovec[MAXLEN_MBUF_CHAIN]; michael@0: WSABUF winbuf; michael@0: #else michael@0: struct iovec send_iovec[MAXLEN_MBUF_CHAIN]; michael@0: struct msghdr msg_hdr; michael@0: #endif michael@0: int use_udp_tunneling; michael@0: michael@0: *result = 0; michael@0: send_count = 0; michael@0: michael@0: m = SCTP_HEADER_TO_CHAIN(o_pak); michael@0: m_orig = m; michael@0: michael@0: len = sizeof(struct ip); michael@0: if (SCTP_BUF_LEN(m) < len) { michael@0: if ((m = m_pullup(m, len)) == 0) { michael@0: SCTP_PRINTF("Can not get the IP header in the first mbuf.\n"); michael@0: return; michael@0: } michael@0: } michael@0: ip = mtod(m, struct ip *); michael@0: use_udp_tunneling = (ip->ip_p == IPPROTO_UDP); michael@0: michael@0: if (use_udp_tunneling) { michael@0: len = sizeof(struct ip) + sizeof(struct udphdr); michael@0: if (SCTP_BUF_LEN(m) < len) { michael@0: if ((m = m_pullup(m, len)) == 0) { michael@0: SCTP_PRINTF("Can not get the UDP/IP header in the first mbuf.\n"); michael@0: return; michael@0: } michael@0: ip = mtod(m, struct ip *); michael@0: } michael@0: udp = (struct udphdr *)(ip + 1); michael@0: } else { michael@0: udp = NULL; michael@0: } michael@0: michael@0: if (!use_udp_tunneling) { michael@0: if (ip->ip_src.s_addr == INADDR_ANY) { michael@0: /* TODO get addr of outgoing interface */ michael@0: SCTP_PRINTF("Why did the SCTP implementation did not choose a source address?\n"); michael@0: } michael@0: /* TODO need to worry about ro->ro_dst as in ip_output? */ michael@0: #if defined(__Userspace_os_Linux) || defined (__Userspace_os_Windows) michael@0: /* need to put certain fields into network order for Linux */ michael@0: ip->ip_len = htons(ip->ip_len); michael@0: ip->ip_off = 0; michael@0: #endif michael@0: } michael@0: michael@0: memset((void *)&dst, 0, sizeof(struct sockaddr_in)); michael@0: dst.sin_family = AF_INET; michael@0: dst.sin_addr.s_addr = ip->ip_dst.s_addr; michael@0: #ifdef HAVE_SIN_LEN michael@0: dst.sin_len = sizeof(struct sockaddr_in); michael@0: #endif michael@0: if (use_udp_tunneling) { michael@0: dst.sin_port = udp->uh_dport; michael@0: } else { michael@0: dst.sin_port = 0; michael@0: } michael@0: michael@0: /* tweak the mbuf chain */ michael@0: if (use_udp_tunneling) { michael@0: m_adj(m, sizeof(struct ip) + sizeof(struct udphdr)); michael@0: } michael@0: michael@0: send_len = SCTP_HEADER_LEN(m); /* length of entire packet */ michael@0: send_count = 0; michael@0: for (iovcnt = 0; m != NULL && iovcnt < MAXLEN_MBUF_CHAIN; m = m->m_next, iovcnt++) { michael@0: #if !defined (__Userspace_os_Windows) michael@0: send_iovec[iovcnt].iov_base = (caddr_t)m->m_data; michael@0: send_iovec[iovcnt].iov_len = SCTP_BUF_LEN(m); michael@0: send_count += send_iovec[iovcnt].iov_len; michael@0: #else michael@0: send_iovec[iovcnt].buf = (caddr_t)m->m_data; michael@0: send_iovec[iovcnt].len = SCTP_BUF_LEN(m); michael@0: send_count += send_iovec[iovcnt].len; michael@0: #endif michael@0: } michael@0: michael@0: if (m != NULL) { michael@0: SCTP_PRINTF("mbuf chain couldn't be copied completely\n"); michael@0: goto free_mbuf; michael@0: } michael@0: michael@0: #if !defined (__Userspace_os_Windows) michael@0: msg_hdr.msg_name = (struct sockaddr *) &dst; michael@0: msg_hdr.msg_namelen = sizeof(struct sockaddr_in); michael@0: msg_hdr.msg_iov = send_iovec; michael@0: msg_hdr.msg_iovlen = iovcnt; michael@0: msg_hdr.msg_control = NULL; michael@0: msg_hdr.msg_controllen = 0; michael@0: msg_hdr.msg_flags = 0; michael@0: michael@0: if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp) > -1)) { michael@0: if ((res = sendmsg(SCTP_BASE_VAR(userspace_rawsctp), &msg_hdr, MSG_DONTWAIT)) != send_len) { michael@0: *result = errno; michael@0: } michael@0: } michael@0: if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp) > -1)) { michael@0: if ((res = sendmsg(SCTP_BASE_VAR(userspace_udpsctp), &msg_hdr, MSG_DONTWAIT)) != send_len) { michael@0: *result = errno; michael@0: } michael@0: } michael@0: #else michael@0: win_msg_hdr.name = (struct sockaddr *) &dst; michael@0: win_msg_hdr.namelen = sizeof(struct sockaddr_in); michael@0: win_msg_hdr.lpBuffers = (LPWSABUF)send_iovec; michael@0: win_msg_hdr.dwBufferCount = iovcnt; michael@0: winbuf.len = 0; michael@0: winbuf.buf = NULL; michael@0: win_msg_hdr.Control = winbuf; michael@0: win_msg_hdr.dwFlags = 0; michael@0: michael@0: if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp) > -1)) { michael@0: 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) { michael@0: *result = WSAGetLastError(); michael@0: } else if (win_sent_len != send_len) { michael@0: *result = WSAGetLastError(); michael@0: } michael@0: } michael@0: if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp) > -1)) { michael@0: 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) { michael@0: *result = WSAGetLastError(); michael@0: } else if (win_sent_len != send_len) { michael@0: *result = WSAGetLastError(); michael@0: } michael@0: } michael@0: #endif michael@0: free_mbuf: michael@0: sctp_m_freem(m_orig); michael@0: } michael@0: #endif michael@0: michael@0: #if defined (INET6) michael@0: void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak, michael@0: struct route_in6 *ro, void *stcb, michael@0: uint32_t vrf_id) michael@0: { michael@0: struct mbuf *m; michael@0: struct mbuf *m_orig; michael@0: int iovcnt; michael@0: int send_len; michael@0: int len; michael@0: int send_count; michael@0: struct ip6_hdr *ip6; michael@0: struct udphdr *udp; michael@0: #if !defined (__Userspace_os_Windows) michael@0: int res; michael@0: #endif michael@0: struct sockaddr_in6 dst; michael@0: #if defined (__Userspace_os_Windows) michael@0: WSAMSG win_msg_hdr; michael@0: int win_sent_len; michael@0: WSABUF send_iovec[MAXLEN_MBUF_CHAIN]; michael@0: WSABUF winbuf; michael@0: #else michael@0: struct iovec send_iovec[MAXLEN_MBUF_CHAIN]; michael@0: struct msghdr msg_hdr; michael@0: #endif michael@0: int use_udp_tunneling; michael@0: michael@0: *result = 0; michael@0: send_count = 0; michael@0: michael@0: m = SCTP_HEADER_TO_CHAIN(o_pak); michael@0: m_orig = m; michael@0: michael@0: len = sizeof(struct ip6_hdr); michael@0: michael@0: if (SCTP_BUF_LEN(m) < len) { michael@0: if ((m = m_pullup(m, len)) == 0) { michael@0: SCTP_PRINTF("Can not get the IP header in the first mbuf.\n"); michael@0: return; michael@0: } michael@0: } michael@0: michael@0: ip6 = mtod(m, struct ip6_hdr *); michael@0: use_udp_tunneling = (ip6->ip6_nxt == IPPROTO_UDP); michael@0: michael@0: if (use_udp_tunneling) { michael@0: len = sizeof(struct ip6_hdr) + sizeof(struct udphdr); michael@0: if (SCTP_BUF_LEN(m) < len) { michael@0: if ((m = m_pullup(m, len)) == 0) { michael@0: SCTP_PRINTF("Can not get the UDP/IP header in the first mbuf.\n"); michael@0: return; michael@0: } michael@0: ip6 = mtod(m, struct ip6_hdr *); michael@0: } michael@0: udp = (struct udphdr *)(ip6 + 1); michael@0: } else { michael@0: udp = NULL; michael@0: } michael@0: michael@0: if (!use_udp_tunneling) { michael@0: if (ip6->ip6_src.s6_addr == in6addr_any.s6_addr) { michael@0: /* TODO get addr of outgoing interface */ michael@0: SCTP_PRINTF("Why did the SCTP implementation did not choose a source address?\n"); michael@0: } michael@0: /* TODO need to worry about ro->ro_dst as in ip_output? */ michael@0: #if defined(__Userspace_os_Linux) || defined (__Userspace_os_Windows) michael@0: /* need to put certain fields into network order for Linux */ michael@0: ip6->ip6_plen = htons(ip6->ip6_plen); michael@0: #endif michael@0: } michael@0: michael@0: memset((void *)&dst, 0, sizeof(struct sockaddr_in6)); michael@0: dst.sin6_family = AF_INET6; michael@0: dst.sin6_addr = ip6->ip6_dst; michael@0: #ifdef HAVE_SIN6_LEN michael@0: dst.sin6_len = sizeof(struct sockaddr_in6); michael@0: #endif michael@0: michael@0: if (use_udp_tunneling) { michael@0: dst.sin6_port = udp->uh_dport; michael@0: } else { michael@0: dst.sin6_port = 0; michael@0: } michael@0: michael@0: /* tweak the mbuf chain */ michael@0: if (use_udp_tunneling) { michael@0: m_adj(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); michael@0: } else { michael@0: m_adj(m, sizeof(struct ip6_hdr)); michael@0: } michael@0: michael@0: send_len = SCTP_HEADER_LEN(m); /* length of entire packet */ michael@0: send_count = 0; michael@0: for (iovcnt = 0; m != NULL && iovcnt < MAXLEN_MBUF_CHAIN; m = m->m_next, iovcnt++) { michael@0: #if !defined (__Userspace_os_Windows) michael@0: send_iovec[iovcnt].iov_base = (caddr_t)m->m_data; michael@0: send_iovec[iovcnt].iov_len = SCTP_BUF_LEN(m); michael@0: send_count += send_iovec[iovcnt].iov_len; michael@0: #else michael@0: send_iovec[iovcnt].buf = (caddr_t)m->m_data; michael@0: send_iovec[iovcnt].len = SCTP_BUF_LEN(m); michael@0: send_count += send_iovec[iovcnt].len; michael@0: #endif michael@0: } michael@0: if (m != NULL) { michael@0: SCTP_PRINTF("mbuf chain couldn't be copied completely\n"); michael@0: goto free_mbuf; michael@0: } michael@0: michael@0: #if !defined (__Userspace_os_Windows) michael@0: msg_hdr.msg_name = (struct sockaddr *) &dst; michael@0: msg_hdr.msg_namelen = sizeof(struct sockaddr_in6); michael@0: msg_hdr.msg_iov = send_iovec; michael@0: msg_hdr.msg_iovlen = iovcnt; michael@0: msg_hdr.msg_control = NULL; michael@0: msg_hdr.msg_controllen = 0; michael@0: msg_hdr.msg_flags = 0; michael@0: michael@0: if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp6) > -1)) { michael@0: if ((res = sendmsg(SCTP_BASE_VAR(userspace_rawsctp6), &msg_hdr, MSG_DONTWAIT)) != send_len) { michael@0: *result = errno; michael@0: } michael@0: } michael@0: if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp6) > -1)) { michael@0: if ((res = sendmsg(SCTP_BASE_VAR(userspace_udpsctp6), &msg_hdr, MSG_DONTWAIT)) != send_len) { michael@0: *result = errno; michael@0: } michael@0: } michael@0: #else michael@0: win_msg_hdr.name = (struct sockaddr *) &dst; michael@0: win_msg_hdr.namelen = sizeof(struct sockaddr_in6); michael@0: win_msg_hdr.lpBuffers = (LPWSABUF)send_iovec; michael@0: win_msg_hdr.dwBufferCount = iovcnt; michael@0: winbuf.len = 0; michael@0: winbuf.buf = NULL; michael@0: win_msg_hdr.Control = winbuf; michael@0: win_msg_hdr.dwFlags = 0; michael@0: michael@0: if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp6) > -1)) { michael@0: 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) { michael@0: *result = WSAGetLastError(); michael@0: } else if (win_sent_len != send_len) { michael@0: *result = WSAGetLastError(); michael@0: } michael@0: } michael@0: if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp6) > -1)) { michael@0: 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) { michael@0: *result = WSAGetLastError(); michael@0: } else if (win_sent_len != send_len) { michael@0: *result = WSAGetLastError(); michael@0: } michael@0: } michael@0: #endif michael@0: free_mbuf: michael@0: sctp_m_freem(m_orig); michael@0: } michael@0: #endif michael@0: michael@0: void michael@0: usrsctp_register_address(void *addr) michael@0: { michael@0: struct sockaddr_conn sconn; michael@0: michael@0: memset(&sconn, 0, sizeof(struct sockaddr_conn)); michael@0: sconn.sconn_family = AF_CONN; michael@0: #ifdef HAVE_SCONN_LEN michael@0: sconn.sconn_len = sizeof(struct sockaddr_conn); michael@0: #endif michael@0: sconn.sconn_port = 0; michael@0: sconn.sconn_addr = addr; michael@0: sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, michael@0: NULL, michael@0: 0xffffffff, michael@0: 0, michael@0: "conn", michael@0: NULL, michael@0: (struct sockaddr *)&sconn, michael@0: 0, michael@0: 0); michael@0: } michael@0: michael@0: void michael@0: usrsctp_deregister_address(void *addr) michael@0: { michael@0: struct sockaddr_conn sconn; michael@0: michael@0: memset(&sconn, 0, sizeof(struct sockaddr_conn)); michael@0: sconn.sconn_family = AF_CONN; michael@0: #ifdef HAVE_SCONN_LEN michael@0: sconn.sconn_len = sizeof(struct sockaddr_conn); michael@0: #endif michael@0: sconn.sconn_port = 0; michael@0: sconn.sconn_addr = addr; michael@0: sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, michael@0: (struct sockaddr *)&sconn, michael@0: 0xffffffff, michael@0: "conn"); michael@0: } michael@0: michael@0: #define PREAMBLE_FORMAT "\n%c %02d:%02d:%02d.%06ld " michael@0: #define PREAMBLE_LENGTH 19 michael@0: #define HEADER "0000 " michael@0: #define TRAILER "# SCTP_PACKET\n" michael@0: michael@0: char * michael@0: usrsctp_dumppacket(void *buf, size_t len, int outbound) michael@0: { michael@0: size_t i, pos; michael@0: char *dump_buf, *packet; michael@0: #ifdef _WIN32 michael@0: struct timeb tb; michael@0: struct tm t; michael@0: #else michael@0: struct timeval tv; michael@0: struct tm *t; michael@0: time_t sec; michael@0: #endif michael@0: michael@0: if ((len == 0) || (buf == NULL)) { michael@0: return (NULL); michael@0: } michael@0: if ((dump_buf = malloc(PREAMBLE_LENGTH + strlen(HEADER) + 3 * len + strlen(TRAILER) + 1)) == NULL) { michael@0: return (NULL); michael@0: } michael@0: pos = 0; michael@0: #ifdef _WIN32 michael@0: ftime(&tb); michael@0: localtime_s(&t, &tb.time); michael@0: _snprintf_s(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_LENGTH, PREAMBLE_FORMAT, michael@0: outbound ? 'O' : 'I', michael@0: t.tm_hour, t.tm_min, t.tm_sec, (long)(1000 * tb.millitm)); michael@0: #else michael@0: gettimeofday(&tv, NULL); michael@0: sec = (time_t)tv.tv_sec; michael@0: t = localtime((const time_t *)&sec); michael@0: snprintf(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_FORMAT, michael@0: outbound ? 'O' : 'I', michael@0: t->tm_hour, t->tm_min, t->tm_sec, (long)tv.tv_usec); michael@0: #endif michael@0: pos += PREAMBLE_LENGTH; michael@0: #ifdef _WIN32 michael@0: strncpy_s(dump_buf + pos, strlen(HEADER) + 1, HEADER, strlen(HEADER)); michael@0: #else michael@0: strcpy(dump_buf + pos, HEADER); michael@0: #endif michael@0: pos += strlen(HEADER); michael@0: packet = (char *)buf; michael@0: for (i = 0; i < len; i++) { michael@0: uint8_t byte, low, high; michael@0: michael@0: byte = (uint8_t)packet[i]; michael@0: high = byte / 16; michael@0: low = byte % 16; michael@0: dump_buf[pos++] = high < 10 ? '0' + high : 'a' + (high - 10); michael@0: dump_buf[pos++] = low < 10 ? '0' + low : 'a' + (low - 10); michael@0: dump_buf[pos++] = ' '; michael@0: } michael@0: #ifdef _WIN32 michael@0: strncpy_s(dump_buf + pos, strlen(TRAILER) + 1, TRAILER, strlen(TRAILER)); michael@0: #else michael@0: strcpy(dump_buf + pos, TRAILER); michael@0: #endif michael@0: pos += strlen(TRAILER); michael@0: dump_buf[pos++] = '\0'; michael@0: return (dump_buf); michael@0: } michael@0: michael@0: void michael@0: usrsctp_freedumpbuffer(char *buf) michael@0: { michael@0: free(buf); michael@0: } michael@0: michael@0: void michael@0: usrsctp_conninput(void *addr, const void *buffer, size_t length, uint8_t ecn_bits) michael@0: { michael@0: struct sockaddr_conn src, dst; michael@0: struct mbuf *m; michael@0: struct sctphdr *sh; michael@0: struct sctp_chunkhdr *ch; michael@0: michael@0: SCTP_STAT_INCR(sctps_recvpackets); michael@0: SCTP_STAT_INCR_COUNTER64(sctps_inpackets); michael@0: memset(&src, 0, sizeof(struct sockaddr_conn)); michael@0: src.sconn_family = AF_CONN; michael@0: #ifdef HAVE_SCONN_LEN michael@0: src.sconn_len = sizeof(struct sockaddr_conn); michael@0: #endif michael@0: src.sconn_addr = addr; michael@0: memset(&dst, 0, sizeof(struct sockaddr_conn)); michael@0: dst.sconn_family = AF_CONN; michael@0: #ifdef HAVE_SCONN_LEN michael@0: dst.sconn_len = sizeof(struct sockaddr_conn); michael@0: #endif michael@0: dst.sconn_addr = addr; michael@0: if ((m = sctp_get_mbuf_for_msg(length, 1, M_NOWAIT, 0, MT_DATA)) == NULL) { michael@0: return; michael@0: } michael@0: m_copyback(m, 0, length, (caddr_t)buffer); michael@0: if (SCTP_BUF_LEN(m) < (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr))) { michael@0: if ((m = m_pullup(m, sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr))) == NULL) { michael@0: SCTP_STAT_INCR(sctps_hdrops); michael@0: return; michael@0: } michael@0: } michael@0: sh = mtod(m, struct sctphdr *);; michael@0: ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); michael@0: src.sconn_port = sh->src_port; michael@0: dst.sconn_port = sh->dest_port; michael@0: sctp_common_input_processing(&m, 0, sizeof(struct sctphdr), length, michael@0: (struct sockaddr *)&src, michael@0: (struct sockaddr *)&dst, michael@0: sh, ch, michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: 1, michael@0: #endif michael@0: ecn_bits, michael@0: SCTP_DEFAULT_VRFID, 0); michael@0: if (m) { michael@0: sctp_m_freem(m); michael@0: } michael@0: return; michael@0: } michael@0: michael@0: michael@0: #define USRSCTP_SYSCTL_SET_DEF(__field) \ michael@0: void usrsctp_sysctl_set_ ## __field(uint32_t value) { \ michael@0: SCTP_BASE_SYSCTL(__field) = value; \ michael@0: } michael@0: michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_sendspace) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_recvspace) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_auto_asconf) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_multiple_asconfs) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_ecn_enable) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_strict_sacks) michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_no_csum_on_loopback) michael@0: #endif michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_peer_chunk_oh) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_max_burst_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_max_chunks_on_queue) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_hashtblsize) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_pcbtblsize) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_min_split_point) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_chunkscale) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_delayed_sack_time_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_sack_freq_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_system_free_resc_limit) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_asoc_free_resc_limit) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_heartbeat_interval_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_pmtu_raise_time_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_shutdown_guard_time_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_secret_lifetime_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_rto_max_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_rto_min_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_rto_initial_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_init_rto_max_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_valid_cookie_life_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_init_rtx_max_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_assoc_rtx_max_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_path_rtx_max_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_add_more_threshold) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_nr_outgoing_streams_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_cmt_on_off) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_cmt_use_dac) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_nr_sack_on_off) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_use_cwnd_based_maxburst) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_asconf_auth_nochk) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_auth_disable) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_nat_friendly) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_L2_abc_variable) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_mbuf_threshold_count) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_do_drain) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_hb_maxburst) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_abort_if_one_2_one_hits_limit) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_strict_data_order) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_min_residual) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_max_retran_chunk) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_logging_level) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_default_cc_module) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_default_frag_interleave) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_mobility_base) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_mobility_fasthandoff) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_inits_include_nat_friendly) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_udp_tunneling_port) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_enable_sack_immediately) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_vtag_time_wait) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_blackhole) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_fr_max_burst_default) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_path_pf_threshold) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_default_ss_module) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_bw) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_rtt) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_eqret) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_steady_step) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_use_dccc_ecn) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_buffer_splitting) michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_initial_cwnd) michael@0: #ifdef SCTP_DEBUG michael@0: USRSCTP_SYSCTL_SET_DEF(sctp_debug_on) michael@0: #endif michael@0: michael@0: #define USRSCTP_SYSCTL_GET_DEF(__field) \ michael@0: uint32_t usrsctp_sysctl_get_ ## __field(void) { \ michael@0: return SCTP_BASE_SYSCTL(__field); \ michael@0: } michael@0: michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_sendspace) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_recvspace) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_auto_asconf) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_multiple_asconfs) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_ecn_enable) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_strict_sacks) michael@0: #if !defined(SCTP_WITH_NO_CSUM) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_no_csum_on_loopback) michael@0: #endif michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_peer_chunk_oh) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_max_burst_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_max_chunks_on_queue) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_hashtblsize) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_pcbtblsize) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_min_split_point) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_chunkscale) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_delayed_sack_time_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_sack_freq_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_system_free_resc_limit) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_asoc_free_resc_limit) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_heartbeat_interval_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_pmtu_raise_time_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_shutdown_guard_time_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_secret_lifetime_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_rto_max_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_rto_min_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_rto_initial_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_init_rto_max_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_valid_cookie_life_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_init_rtx_max_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_assoc_rtx_max_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_path_rtx_max_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_add_more_threshold) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_nr_outgoing_streams_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_cmt_on_off) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_cmt_use_dac) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_nr_sack_on_off) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_use_cwnd_based_maxburst) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_asconf_auth_nochk) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_auth_disable) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_nat_friendly) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_L2_abc_variable) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_mbuf_threshold_count) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_do_drain) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_hb_maxburst) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_abort_if_one_2_one_hits_limit) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_strict_data_order) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_min_residual) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_max_retran_chunk) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_logging_level) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_default_cc_module) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_default_frag_interleave) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_mobility_base) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_mobility_fasthandoff) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_inits_include_nat_friendly) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_udp_tunneling_port) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_enable_sack_immediately) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_vtag_time_wait) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_blackhole) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_fr_max_burst_default) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_path_pf_threshold) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_default_ss_module) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_bw) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_rtt) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_eqret) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_steady_step) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_use_dccc_ecn) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_buffer_splitting) michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_initial_cwnd) michael@0: #ifdef SCTP_DEBUG michael@0: USRSCTP_SYSCTL_GET_DEF(sctp_debug_on) michael@0: #endif michael@0: michael@0: void usrsctp_get_stat(struct sctpstat *stat) michael@0: { michael@0: *stat = SCTP_BASE_STATS; michael@0: }