| 6 |
| * Copyright (c) 2009-2010 Brad Penoff |
| 7 | * Copyright (c) 2009-2010 Humaira Kamal |
| 8 | * Copyright (c) 2011-2012 Irene Ruengeler |
| 9 | * Copyright (c) 2011-2012 Michael Tuexen |
| 10 | * All rights reserved. |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer. |
| 17 | * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | * notice, this list of conditions and the following disclaimer in the |
| 19 | * documentation and/or other materials provided with the distribution. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
| 34 |
| 35 | #include <netinet/sctp_os.h> |
| 36 | #include <netinet/sctp_pcb.h> |
| 37 | #include <netinet/sctputil.h> |
| 38 | #include <netinet/sctp_var.h> |
| 39 | #include <netinet/sctp_sysctl.h> |
| 40 | #include <netinet/sctp_input.h> |
| 41 | #include <netinet/sctp_peeloff.h> |
| 42 | #include <netinet/sctp_callout.h> |
| 43 | #include <netinet/sctp_crc32.h> |
| 44 | #ifdef INET6 |
| 45 | #include <netinet6/sctp6_var.h> |
| 46 | #endif |
| 47 | #if defined(__Userspace_os_FreeBSD) |
| 48 | #include <sys/param.h> |
| 49 | #endif |
| 50 | #if defined(__Userspace_os_Linux) |
| 51 | #define __FAVOR_BSD /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */ |
| 52 | #endif |
| 53 | #if !defined (__Userspace_os_Windows) |
| 54 | #if defined INET || defined INET6 |
| 55 | #include <netinet/udp.h> |
| 56 | #endif |
| 57 | #include <arpa/inet.h> |
| 58 | #else |
| 59 | #include <user_socketvar.h> |
| 60 | #endif |
| 61 | userland_mutex_t accept_mtx; |
| 62 | userland_cond_t accept_cond; |
| 63 | #ifdef _WIN32 |
| 64 | #include <time.h> |
| 65 | #include <sys/timeb.h> |
| 66 | #endif |
| 67 |
| 68 | MALLOC_DEFINE(M_PCB, "sctp_pcb", "sctp pcb"); |
| 69 | MALLOC_DEFINE(M_SONAME, "sctp_soname", "sctp soname"); |
| 70 | #define MAXLEN_MBUF_CHAIN 32 |
| 71 |
| 72 | /* Prototypes */ |
| 73 | extern int sctp_sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, |
| 74 | struct mbuf *top, struct mbuf *control, int flags, |
| 75 | /* proc is a dummy in __Userspace__ and will not be passed to sctp_lower_sosend */ |
| 76 | struct proc *p); |
| 77 |
| 78 | extern int sctp_attach(struct socket *so, int proto, uint32_t vrf_id); |
| 79 | extern int sctpconn_attach(struct socket *so, int proto, uint32_t vrf_id); |
| 80 |
| 81 | static void init_sync(void) { |
| 82 | #if defined(__Userspace_os_Windows) |
| 83 | #if defined(INET) || defined(INET6) |
| 84 | WSADATA wsaData; |
| 85 |
| 86 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) { |
| 87 | SCTP_PRINTF("WSAStartup failed\n"); |
| 88 | exit (-1); |
| 89 | } |
| 90 | #endif |
| 91 | InitializeConditionVariable(&accept_cond); |
| 92 | InitializeCriticalSection(&accept_mtx); |
| 93 | #else |
| 94 | pthread_mutexattr_t mutex_attr; |
| 95 |
| 96 | pthread_mutexattr_init(&mutex_attr); |
| 97 | #ifdef INVARIANTS |
| 98 | pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK); |
| 99 | #endif |
| 100 | pthread_mutex_init(&accept_mtx, &mutex_attr); |
| 101 | pthread_mutexattr_destroy(&mutex_attr); |
| 102 | pthread_cond_init(&accept_cond, NULL); |
| 103 | #endif |
| 104 | } |
| 105 |
| 106 | void |
| 107 | usrsctp_init(uint16_t port, |
| 108 | int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), |
| 109 | void (*debug_printf)(const char *format, ...)) |
| 110 | { |
| 111 | init_sync(); |
| 112 | sctp_init(port, conn_output, debug_printf, 1); |
| 113 | } |
| 114 |
| 115 |
| 116 | void |
| 117 | usrsctp_init_nothreads(uint16_t port, |
| 118 | int (*conn_output)(void *addr, void *buffer, size_t length, uint8_t tos, uint8_t set_df), |
| 119 | void (*debug_printf)(const char *format, ...)) |
| 120 | { |
| 121 | init_sync(); |
| 122 | sctp_init(port, conn_output, debug_printf, 0); |
| 123 | } |
| 124 |
| 125 |
| 126 | /* Taken from usr/src/sys/kern/uipc_sockbuf.c and modified for __Userspace__*/ |
| 127 | /* |
| 128 | * Socantsendmore indicates that no more data will be sent on the socket; it |
| 129 | * would normally be applied to a socket when the user informs the system |
| 130 | * that no more data is to be sent, by the protocol code (in case |
| 131 | * PRU_SHUTDOWN). Socantrcvmore indicates that no more data will be |
| 132 | * received, and will normally be applied to the socket by a protocol when it |
| 133 | * detects that the peer will send no more data. Data queued for reading in |
| 134 | * the socket may yet be read. |
| 135 | */ |
| 136 |
| 137 | void socantrcvmore_locked(struct socket *so) |
| 138 | { |
| 139 | SOCKBUF_LOCK_ASSERT(&so->so_rcv); |
| 140 | so->so_rcv.sb_state |= SBS_CANTRCVMORE; |
| 141 | sorwakeup_locked(so); |
| 142 | } |
| 143 |
| 144 | void socantrcvmore(struct socket *so) |
| 145 | { |
| 146 | SOCKBUF_LOCK(&so->so_rcv); |
| 147 | socantrcvmore_locked(so); |
| 148 | } |
| 149 |
| 150 | void |
| 151 | socantsendmore_locked(struct socket *so) |
| 152 | { |
| 153 | SOCKBUF_LOCK_ASSERT(&so->so_snd); |
| 154 | so->so_snd.sb_state |= SBS_CANTSENDMORE; |
| 155 | sowwakeup_locked(so); |
| 156 | } |
| 157 |
| 158 | void |
| 159 | socantsendmore(struct socket *so) |
| 160 | { |
| 161 | SOCKBUF_LOCK(&so->so_snd); |
| 162 | socantsendmore_locked(so); |
| 163 | } |
| 164 |
| 165 |
| 166 |
| 167 | /* Taken from usr/src/sys/kern/uipc_sockbuf.c and called within sctp_lower_sosend. |
| 168 | */ |
| 169 | int |
| 170 | sbwait(struct sockbuf *sb) |
| 171 | { |
| 172 | #if defined(__Userspace__) /* __Userspace__ */ |
| 173 |
| 174 | SOCKBUF_LOCK_ASSERT(sb); |
| 175 |
| 176 | sb->sb_flags |= SB_WAIT; |
| 177 | #if defined (__Userspace_os_Windows) |
| 178 | if (SleepConditionVariableCS(&(sb->sb_cond), &(sb->sb_mtx), INFINITE)) |
| 179 | return 0; |
| 180 | else |
| 181 | return -1; |
| 182 | #else |
| 183 | return (pthread_cond_wait(&(sb->sb_cond), &(sb->sb_mtx))); |
| 184 | #endif |
| 185 |
| 186 | #else |
| 187 | SOCKBUF_LOCK_ASSERT(sb); |
| 188 |
| 189 | sb->sb_flags |= SB_WAIT; |
| 190 | return (msleep(&sb->sb_cc, &sb->sb_mtx, |
| 191 | (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait", |
| 192 | sb->sb_timeo)); |
| 193 | #endif |
| 194 | } |
| 195 |
| 196 |
| 197 |
| 198 |
| 199 | /* Taken from /src/sys/kern/uipc_socket.c |
| 200 | * and modified for __Userspace__ |
| 201 | */ |
| 202 | static struct socket * |
| 203 | soalloc(void) |
| 204 | { |
| 205 | struct socket *so; |
| 206 |
| 207 | /* |
| 208 | * soalloc() sets of socket layer state for a socket, |
| 209 | * called only by socreate() and sonewconn(). |
| 210 | * |
| 211 | * sodealloc() tears down socket layer state for a socket, |
| 212 | * called only by sofree() and sonewconn(). |
| 213 | * __Userspace__ TODO : Make sure so is properly deallocated |
| 214 | * when tearing down the connection. |
| 215 | */ |
| 216 |
| 217 | so = (struct socket *)malloc(sizeof(struct socket)); |
| 218 |
| 219 | if (so == NULL) { |
| 220 | return (NULL); |
| 221 | } |
| 222 | memset(so, 0, sizeof(struct socket)); |
| 223 |
| 224 | /* __Userspace__ Initializing the socket locks here */ |
| 225 | SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd"); |
| 226 | SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv"); |
| 227 | SOCKBUF_COND_INIT(&so->so_snd); |
| 228 | SOCKBUF_COND_INIT(&so->so_rcv); |
| 229 | SOCK_COND_INIT(so); /* timeo_cond */ |
| 230 |
| 231 | /* __Userspace__ Any ref counting required here? Will we have any use for aiojobq? |
| 232 | What about gencnt and numopensockets?*/ |
| 233 | TAILQ_INIT(&so->so_aiojobq); |
| 234 | return (so); |
| 235 | } |
| 236 |
| 237 | static void |
| 238 | sodealloc(struct socket *so) |
| 239 | { |
| 240 |
| 241 | KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count)); |
| 242 | KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL")); |
| 243 |
| 244 | SOCKBUF_COND_DESTROY(&so->so_snd); |
| 245 | SOCKBUF_COND_DESTROY(&so->so_rcv); |
| 246 |
| 247 | SOCK_COND_DESTROY(so); |
| 248 |
| 249 | SOCKBUF_LOCK_DESTROY(&so->so_snd); |
| 250 | SOCKBUF_LOCK_DESTROY(&so->so_rcv); |
| 251 |
| 252 | free(so); |
| 253 | } |
| 254 |
| 255 | /* Taken from /src/sys/kern/uipc_socket.c |
| 256 | * and modified for __Userspace__ |
| 257 | */ |
| 258 | void |
| 259 | sofree(struct socket *so) |
| 260 | { |
| 261 | struct socket *head; |
| 262 |
| 263 | ACCEPT_LOCK_ASSERT(); |
| 264 | SOCK_LOCK_ASSERT(so); |
| 265 | /* SS_NOFDREF unset in accept call. this condition seems irrelevent |
| 266 | * for __Userspace__... |
| 267 | */ |
| 268 | if (so->so_count != 0 || |
| 269 | (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) { |
| 270 | SOCK_UNLOCK(so); |
| 271 | ACCEPT_UNLOCK(); |
| 272 | return; |
| 273 | } |
| 274 | head = so->so_head; |
| 275 | if (head != NULL) { |
| 276 | KASSERT((so->so_qstate & SQ_COMP) != 0 || |
| 277 | (so->so_qstate & SQ_INCOMP) != 0, |
| 278 | ("sofree: so_head != NULL, but neither SQ_COMP nor " |
| 279 | "SQ_INCOMP")); |
| 280 | KASSERT((so->so_qstate & SQ_COMP) == 0 || |
| 281 | (so->so_qstate & SQ_INCOMP) == 0, |
| 282 | ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP")); |
| 283 | TAILQ_REMOVE(&head->so_incomp, so, so_list); |
| 284 | head->so_incqlen--; |
| 285 | so->so_qstate &= ~SQ_INCOMP; |
| 286 | so->so_head = NULL; |
| 287 | } |
| 288 | KASSERT((so->so_qstate & SQ_COMP) == 0 && |
| 289 | (so->so_qstate & SQ_INCOMP) == 0, |
| 290 | ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)", |
| 291 | so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP)); |
| 292 | if (so->so_options & SCTP_SO_ACCEPTCONN) { |
| 293 | KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated")); |
| 294 | KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated")); |
| 295 | } |
| 296 | SOCK_UNLOCK(so); |
| 297 | ACCEPT_UNLOCK(); |
| 298 | sctp_close(so); /* was... sctp_detach(so); */ |
| 299 | /* |
| 300 | * From this point on, we assume that no other references to this |
| 301 | * socket exist anywhere else in the stack. Therefore, no locks need |
| 302 | * to be acquired or held. |
| 303 | * |
| 304 | * We used to do a lot of socket buffer and socket locking here, as |
| 305 | * well as invoke sorflush() and perform wakeups. The direct call to |
| 306 | * dom_dispose() and sbrelease_internal() are an inlining of what was |
| 307 | * necessary from sorflush(). |
| 308 | * |
| 309 | * Notice that the socket buffer and kqueue state are torn down |
| 310 | * before calling pru_detach. This means that protocols shold not |
| 311 | * assume they can perform socket wakeups, etc, in their detach code. |
| 312 | */ |
| 313 | sodealloc(so); |
| 314 | } |
| 315 |
| 316 |
| 317 |
| 318 | /* Taken from /src/sys/kern/uipc_socket.c */ |
| 319 | void |
| 320 | soabort(struct socket *so) |
| 321 | { |
| 322 | #if defined(INET6) |
| 323 | struct sctp_inpcb *inp; |
| 324 | #endif |
| 325 |
| 326 | #if defined(INET6) |
| 327 | inp = (struct sctp_inpcb *)so->so_pcb; |
| 328 | if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) { |
| 329 | sctp6_abort(so); |
| 330 | } else { |
| 331 | #if defined(INET) |
| 332 | sctp_abort(so); |
| 333 | #endif |
| 334 | } |
| 335 | #elif defined(INET) |
| 336 | sctp_abort(so); |
| 337 | #endif |
| 338 | ACCEPT_LOCK(); |
| 339 | SOCK_LOCK(so); |
| 340 | sofree(so); |
| 341 | } |
| 342 |
| 343 |
| 344 | /* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_connect (sctp_usrreq.c). |
| 345 | * We use sctp_connect for send_one_init_real in ms1. |
| 346 | */ |
| 347 | void |
| 348 | soisconnecting(struct socket *so) |
| 349 | { |
| 350 |
| 351 | SOCK_LOCK(so); |
| 352 | so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); |
| 353 | so->so_state |= SS_ISCONNECTING; |
| 354 | SOCK_UNLOCK(so); |
| 355 | } |
| 356 |
| 357 | /* Taken from usr/src/sys/kern/uipc_socket.c and called within sctp_disconnect (sctp_usrreq.c). |
| 358 | * TODO Do we use sctp_disconnect? |
| 359 | */ |
| 360 | void |
| 361 | soisdisconnecting(struct socket *so) |
| 362 | { |
| 363 |
| 364 | /* |
| 365 | * Note: This code assumes that SOCK_LOCK(so) and |
| 366 | * SOCKBUF_LOCK(&so->so_rcv) are the same. |
| 367 | */ |
| 368 | SOCKBUF_LOCK(&so->so_rcv); |
| 369 | so->so_state &= ~SS_ISCONNECTING; |
| 370 | so->so_state |= SS_ISDISCONNECTING; |
| 371 | so->so_rcv.sb_state |= SBS_CANTRCVMORE; |
| 372 | sorwakeup_locked(so); |
| 373 | SOCKBUF_LOCK(&so->so_snd); |
| 374 | so->so_snd.sb_state |= SBS_CANTSENDMORE; |
| 375 | sowwakeup_locked(so); |
| 376 | wakeup("dummy",so); |
| 377 | /* requires 2 args but this was in orig */ |
| 378 | /* wakeup(&so->so_timeo); */ |
| 379 | } |
| 380 |
| 381 |
| 382 | /* Taken from sys/kern/kern_synch.c and |
| 383 | modified for __Userspace__ |
| 384 | */ |
| 385 |
| 386 | /* |
| 387 | * Make all threads sleeping on the specified identifier runnable. |
| 388 | * Associating wakeup with so_timeo identifier and timeo_cond |
| 389 | * condition variable. TODO. If we use iterator thread then we need to |
| 390 | * modify wakeup so it can distinguish between iterator identifier and |
| 391 | * timeo identifier. |
| 392 | */ |
| 393 | void |
| 394 | wakeup(void *ident, struct socket *so) |
| 395 | { |
| 396 | SOCK_LOCK(so); |
| 397 | #if defined (__Userspace_os_Windows) |
| 398 | WakeAllConditionVariable(&(so)->timeo_cond); |
| 399 | #else |
| 400 | pthread_cond_broadcast(&(so)->timeo_cond); |
| 401 | #endif |
| 402 | SOCK_UNLOCK(so); |
| 403 | } |
| 404 |
| 405 |
| 406 | /* |
| 407 | * Make a thread sleeping on the specified identifier runnable. |
| 408 | * May wake more than one thread if a target thread is currently |
| 409 | * swapped out. |
| 410 | */ |
| 411 | void |
| 412 | wakeup_one(void *ident) |
| 413 | { |
| 414 | /* __Userspace__ Check: We are using accept_cond for wakeup_one. |
| 415 | It seems that wakeup_one is only called within |
| 416 | soisconnected() and sonewconn() with ident &head->so_timeo |
| 417 | head is so->so_head, which is back pointer to listen socket |
| 418 | This seems to indicate that the use of accept_cond is correct |
| 419 | since socket where accepts occur is so_head in all |
| 420 | subsidiary sockets. |
| 421 | */ |
| 422 | ACCEPT_LOCK(); |
| 423 | #if defined (__Userspace_os_Windows) |
| 424 | WakeAllConditionVariable(&accept_cond); |
| 425 | #else |
| 426 | pthread_cond_broadcast(&accept_cond); |
| 427 | #endif |
| 428 | ACCEPT_UNLOCK(); |
| 429 | } |
| 430 |
| 431 |
| 432 | /* Called within sctp_process_cookie_[existing/new] */ |
| 433 | void |
| 434 | soisconnected(struct socket *so) |
| 435 | { |
| 436 | struct socket *head; |
| 437 |
| 438 | ACCEPT_LOCK(); |
| 439 | SOCK_LOCK(so); |
| 440 | so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); |
| 441 | so->so_state |= SS_ISCONNECTED; |
| 442 | head = so->so_head; |
| 443 | if (head != NULL && (so->so_qstate & SQ_INCOMP)) { |
| 444 | SOCK_UNLOCK(so); |
| 445 | TAILQ_REMOVE(&head->so_incomp, so, so_list); |
| 446 | head->so_incqlen--; |
| 447 | so->so_qstate &= ~SQ_INCOMP; |
| 448 | TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); |
| 449 | head->so_qlen++; |
| 450 | so->so_qstate |= SQ_COMP; |
| 451 | ACCEPT_UNLOCK(); |
| 452 | sorwakeup(head); |
| 453 | wakeup_one(&head->so_timeo); |
| 454 | return; |
| 455 | } |
| 456 | SOCK_UNLOCK(so); |
| 457 | ACCEPT_UNLOCK(); |
| 458 | wakeup(&so->so_timeo, so); |
| 459 | sorwakeup(so); |
| 460 | sowwakeup(so); |
| 461 |
| 462 | } |
| 463 |
| 464 | /* called within sctp_handle_cookie_echo */ |
| 465 |
| 466 | struct socket * |
| 467 | sonewconn(struct socket *head, int connstatus) |
| 468 | { |
| 469 | struct socket *so; |
| 470 | int over; |
| 471 |
| 472 | ACCEPT_LOCK(); |
| 473 | over = (head->so_qlen > 3 * head->so_qlimit / 2); |
| 474 | ACCEPT_UNLOCK(); |
| 475 | #ifdef REGRESSION |
| 476 | if (regression_sonewconn_earlytest && over) |
| 477 | #else |
| 478 | if (over) |
| 479 | #endif |
| 480 | return (NULL); |
| 481 | so = soalloc(); |
| 482 | if (so == NULL) |
| 483 | return (NULL); |
| 484 | so->so_head = head; |
| 485 | so->so_type = head->so_type; |
| 486 | so->so_options = head->so_options &~ SCTP_SO_ACCEPTCONN; |
| 487 | so->so_linger = head->so_linger; |
| 488 | so->so_state = head->so_state | SS_NOFDREF; |
| 489 | so->so_dom = head->so_dom; |
| 490 | #ifdef MAC |
| 491 | SOCK_LOCK(head); |
| 492 | mac_create_socket_from_socket(head, so); |
| 493 | SOCK_UNLOCK(head); |
| 494 | #endif |
| 495 | if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat)) { |
| 496 | sodealloc(so); |
| 497 | return (NULL); |
| 498 | } |
| 499 | switch (head->so_dom) { |
| 500 | #ifdef INET |
| 501 | case AF_INET: |
| 502 | if (sctp_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) { |
| 503 | sodealloc(so); |
| 504 | return (NULL); |
| 505 | } |
| 506 | break; |
| 507 | #endif |
| 508 | #ifdef INET6 |
| 509 | case AF_INET6: |
| 510 | if (sctp6_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) { |
| 511 | sodealloc(so); |
| 512 | return (NULL); |
| 513 | } |
| 514 | break; |
| 515 | #endif |
| 516 | case AF_CONN: |
| 517 | if (sctpconn_attach(so, IPPROTO_SCTP, SCTP_DEFAULT_VRFID)) { |
| 518 | sodealloc(so); |
| 519 | return (NULL); |
| 520 | } |
| 521 | break; |
| 522 | default: |
| 523 | sodealloc(so); |
| 524 | return (NULL); |
| 525 | break; |
| 526 | } |
| 527 | so->so_rcv.sb_lowat = head->so_rcv.sb_lowat; |
| 528 | so->so_snd.sb_lowat = head->so_snd.sb_lowat; |
| 529 | so->so_rcv.sb_timeo = head->so_rcv.sb_timeo; |
| 530 | so->so_snd.sb_timeo = head->so_snd.sb_timeo; |
| 531 | so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE; |
| 532 | so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE; |
| 533 | so->so_state |= connstatus; |
| 534 | ACCEPT_LOCK(); |
| 535 | if (connstatus) { |
| 536 | TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); |
| 537 | so->so_qstate |= SQ_COMP; |
| 538 | head->so_qlen++; |
| 539 | } else { |
| 540 | /* |
| 541 | * Keep removing sockets from the head until there's room for |
| 542 | * us to insert on the tail. In pre-locking revisions, this |
| 543 | * was a simple if (), but as we could be racing with other |
| 544 | * threads and soabort() requires dropping locks, we must |
| 545 | * loop waiting for the condition to be true. |
| 546 | */ |
| 547 | while (head->so_incqlen > head->so_qlimit) { |
| 548 | struct socket *sp; |
| 549 | sp = TAILQ_FIRST(&head->so_incomp); |
| 550 | TAILQ_REMOVE(&head->so_incomp, sp, so_list); |
| 551 | head->so_incqlen--; |
| 552 | sp->so_qstate &= ~SQ_INCOMP; |
| 553 | sp->so_head = NULL; |
| 554 | ACCEPT_UNLOCK(); |
| 555 | soabort(sp); |
| 556 | ACCEPT_LOCK(); |
| 557 | } |
| 558 | TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); |
| 559 | so->so_qstate |= SQ_INCOMP; |
| 560 | head->so_incqlen++; |
| 561 | } |
| 562 | ACCEPT_UNLOCK(); |
| 563 | if (connstatus) { |
| 564 | sorwakeup(head); |
| 565 | wakeup_one(&head->so_timeo); |
| 566 | } |
| 567 | return (so); |
| 568 |
| 569 | } |
| 570 |
| 571 | /* From /src/sys/sys/sysproto.h */ |
| 572 | struct sctp_generic_sendmsg_args { |
| 573 | int sd; |
| 574 | caddr_t msg; |
| 575 | int mlen; |
| 576 | caddr_t to; |
| 577 | socklen_t tolen; /* was __socklen_t */ |
| 578 | struct sctp_sndrcvinfo * sinfo; |
| 579 | int flags; |
| 580 | }; |
| 581 |
| 582 | struct sctp_generic_recvmsg_args { |
| 583 | int sd; |
| 584 | struct iovec *iov; |
| 585 | int iovlen; |
| 586 | struct sockaddr *from; |
| 587 | socklen_t *fromlenaddr; /* was __socklen_t */ |
| 588 | struct sctp_sndrcvinfo *sinfo; |
| 589 | int *msg_flags; |
| 590 | }; |
| 591 |
| 592 |
| 593 | /* |
| 594 | Source: /src/sys/gnu/fs/xfs/FreeBSD/xfs_ioctl.c |
| 595 | */ |
| 596 | static __inline__ int |
| 597 | copy_to_user(void *dst, void *src, size_t len) { |
| 598 | memcpy(dst, src, len); |
| 599 | return 0; |
| 600 | } |
| 601 |
| 602 | static __inline__ int |
| 603 | copy_from_user(void *dst, void *src, size_t len) { |
| 604 | memcpy(dst, src, len); |
| 605 | return 0; |
| 606 | } |
| 607 |
| 608 | /* |
| 609 | References: |
| 610 | src/sys/dev/lmc/if_lmc.h: |
| 611 | src/sys/powerpc/powerpc/copyinout.c |
| 612 | src/sys/sys/systm.h |
| 613 | */ |
| 614 | # define copyin(u, k, len) copy_from_user(k, u, len) |
| 615 |
| 616 | /* References: |
| 617 | src/sys/powerpc/powerpc/copyinout.c |
| 618 | src/sys/sys/systm.h |
| 619 | */ |
| 620 | # define copyout(k, u, len) copy_to_user(u, k, len) |
| 621 |
| 622 |
| 623 | /* copyiniov definition copied/modified from src/sys/kern/kern_subr.c */ |
| 624 | int |
| 625 | copyiniov(struct iovec *iovp, u_int iovcnt, struct iovec **iov, int error) |
| 626 | { |
| 627 | u_int iovlen; |
| 628 |
| 629 | *iov = NULL; |
| 630 | if (iovcnt > UIO_MAXIOV) |
| 631 | return (error); |
| 632 | iovlen = iovcnt * sizeof (struct iovec); |
| 633 | *iov = malloc(iovlen); /*, M_IOV, M_WAITOK); */ |
| 634 | error = copyin(iovp, *iov, iovlen); |
| 635 | if (error) { |
| 636 | free(*iov); /*, M_IOV); */ |
| 637 | *iov = NULL; |
| 638 | } |
| 639 | return (error); |
| 640 | } |
| 641 |
| 642 | /* (__Userspace__) version of uiomove */ |
| 643 | int |
| 644 | uiomove(void *cp, int n, struct uio *uio) |
| 645 | { |
| 646 | struct iovec *iov; |
| 647 | size_t cnt; |
| 648 | int error = 0; |
| 649 |
| 650 | if ((uio->uio_rw != UIO_READ) && |
| 651 | (uio->uio_rw != UIO_WRITE)) { |
| 652 | return (EINVAL); |
| 653 | } |
| 654 |
| 655 | while (n > 0 && uio->uio_resid) { |
| 656 | iov = uio->uio_iov; |
| 657 | cnt = iov->iov_len; |
| 658 | if (cnt == 0) { |
| 659 | uio->uio_iov++; |
| 660 | uio->uio_iovcnt--; |
| 661 | continue; |
| 662 | } |
| 663 | if (cnt > (size_t)n) |
| 664 | cnt = n; |
| 665 |
| 666 | switch (uio->uio_segflg) { |
| 667 |
| 668 | case UIO_USERSPACE: |
| 669 | if (uio->uio_rw == UIO_READ) |
| 670 | error = copyout(cp, iov->iov_base, cnt); |
| 671 | else |
| 672 | error = copyin(iov->iov_base, cp, cnt); |
| 673 | if (error) |
| 674 | goto out; |
| 675 | break; |
| 676 |
| 677 | case UIO_SYSSPACE: |
| 678 | if (uio->uio_rw == UIO_READ) |
| 679 | memcpy(iov->iov_base, cp, cnt); |
| 680 | else |
| 681 | memcpy(cp, iov->iov_base, cnt); |
| 682 | break; |
| 683 | } |
| 684 | iov->iov_base = (char *)iov->iov_base + cnt; |
| 685 | iov->iov_len -= cnt; |
| 686 | uio->uio_resid -= cnt; |
| 687 | uio->uio_offset += (off_t)cnt; |
| 688 | cp = (char *)cp + cnt; |
| 689 | n -= (int)cnt; |
| 690 | } |
| 691 | out: |
| 692 | return (error); |
| 693 | } |
| 694 |
| 695 |
| 696 | /* Source: src/sys/kern/uipc_syscalls.c */ |
| 697 | int |
| 698 | getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len) |
| 699 | { |
| 700 | struct sockaddr *sa; |
| 701 | int error; |
| 702 |
| 703 | if (len > SOCK_MAXADDRLEN) |
| 704 | return (ENAMETOOLONG); |
| 705 | if (len < offsetof(struct sockaddr, sa_data)) |
| 706 | return (EINVAL); |
| 707 | MALLOC(sa, struct sockaddr *, len, M_SONAME, M_WAITOK); |
| 708 | error = copyin(uaddr, sa, len); |
| 709 | if (error) { |
| 710 | FREE(sa, M_SONAME); |
| 711 | } else { |
| 712 | #ifdef HAVE_SA_LEN |
| 713 | sa->sa_len = len; |
| 714 | #endif |
| 715 | *namp = sa; |
| 716 | } |
| 717 | return (error); |
| 718 | } |
| 719 |
| 720 | int |
| 721 | usrsctp_getsockopt(struct socket *so, int level, int option_name, |
| 722 | void *option_value, socklen_t *option_len); |
| 723 |
| 724 | sctp_assoc_t |
| 725 | usrsctp_getassocid(struct socket *sock, struct sockaddr *sa) |
| 726 | { |
| 727 | struct sctp_paddrinfo sp; |
| 728 | socklen_t siz; |
| 729 | #ifndef HAVE_SA_LEN |
| 730 | size_t sa_len; |
| 731 | #endif |
| 732 |
| 733 | /* First get the assoc id */ |
| 734 | siz = sizeof(sp); |
| 735 | memset(&sp, 0, sizeof(sp)); |
| 736 | #ifdef HAVE_SA_LEN |
| 737 | memcpy((caddr_t)&sp.spinfo_address, sa, sa->sa_len); |
| 738 | #else |
| 739 | switch (sa->sa_family) { |
| 740 | #ifdef INET |
| 741 | case AF_INET: |
| 742 | sa_len = sizeof(struct sockaddr_in); |
| 743 | break; |
| 744 | #endif |
| 745 | #ifdef INET6 |
| 746 | case AF_INET6: |
| 747 | sa_len = sizeof(struct sockaddr_in6); |
| 748 | break; |
| 749 | #endif |
| 750 | case AF_CONN: |
| 751 | sa_len = sizeof(struct sockaddr_conn); |
| 752 | break; |
| 753 | default: |
| 754 | sa_len = 0; |
| 755 | break; |
| 756 | } |
| 757 | memcpy((caddr_t)&sp.spinfo_address, sa, sa_len); |
| 758 | #endif |
| 759 | if (usrsctp_getsockopt(sock, IPPROTO_SCTP, SCTP_GET_PEER_ADDR_INFO, &sp, &siz) != 0) { |
| 760 | /* We depend on the fact that 0 can never be returned */ |
| 761 | return ((sctp_assoc_t) 0); |
| 762 | } |
| 763 | return (sp.spinfo_assoc_id); |
| 764 | } |
| 765 |
| 766 |
| 767 | /* Taken from /src/lib/libc/net/sctp_sys_calls.c |
| 768 | * and modified for __Userspace__ |
| 769 | * calling sctp_generic_sendmsg from this function |
| 770 | */ |
| 771 | ssize_t |
| 772 | userspace_sctp_sendmsg(struct socket *so, |
| 773 | const void *data, |
| 774 | size_t len, |
| 775 | struct sockaddr *to, |
| 776 | socklen_t tolen, |
| 777 | u_int32_t ppid, |
| 778 | u_int32_t flags, |
| 779 | u_int16_t stream_no, |
| 780 | u_int32_t timetolive, |
| 781 | u_int32_t context) |
| 782 | { |
| 783 | struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo; |
| 784 | struct uio auio; |
| 785 | struct iovec iov[1]; |
| 786 |
| 787 | memset(sinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 788 | sinfo->sinfo_ppid = ppid; |
| 789 | sinfo->sinfo_flags = flags; |
| 790 | sinfo->sinfo_stream = stream_no; |
| 791 | sinfo->sinfo_timetolive = timetolive; |
| 792 | sinfo->sinfo_context = context; |
| 793 | sinfo->sinfo_assoc_id = 0; |
| 794 |
| 795 |
| 796 | /* Perform error checks on destination (to) */ |
| 797 | if (tolen > SOCK_MAXADDRLEN) { |
| 798 | errno = ENAMETOOLONG; |
| 799 | return (-1); |
| 800 | } |
| 801 | if ((tolen > 0) && |
| 802 | ((to == NULL) || (tolen < (socklen_t)sizeof(struct sockaddr)))) { |
| 803 | errno = EINVAL; |
| 804 | return (-1); |
| 805 | } |
| 806 | if (data == NULL) { |
| 807 | errno = EFAULT; |
| 808 | return (-1); |
| 809 | } |
| 810 | /* Adding the following as part of defensive programming, in case the application |
| 811 | does not do it when preparing the destination address.*/ |
| 812 | #ifdef HAVE_SA_LEN |
| 813 | if (to != NULL) { |
| 814 | to->sa_len = tolen; |
| 815 | } |
| 816 | #endif |
| 817 |
| 818 | iov[0].iov_base = (caddr_t)data; |
| 819 | iov[0].iov_len = len; |
| 820 |
| 821 | auio.uio_iov = iov; |
| 822 | auio.uio_iovcnt = 1; |
| 823 | auio.uio_segflg = UIO_USERSPACE; |
| 824 | auio.uio_rw = UIO_WRITE; |
| 825 | auio.uio_offset = 0; /* XXX */ |
| 826 | auio.uio_resid = len; |
| 827 | errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, 0, sinfo); |
| 828 | if (errno == 0) { |
| 829 | return (len - auio.uio_resid); |
| 830 | } else { |
| 831 | return (-1); |
| 832 | } |
| 833 | } |
| 834 |
| 835 |
| 836 | ssize_t |
| 837 | usrsctp_sendv(struct socket *so, |
| 838 | const void *data, |
| 839 | size_t len, |
| 840 | struct sockaddr *to, |
| 841 | int addrcnt, |
| 842 | void *info, |
| 843 | socklen_t infolen, |
| 844 | unsigned int infotype, |
| 845 | int flags) |
| 846 | { |
| 847 | struct sctp_sndrcvinfo sinfo; |
| 848 | struct uio auio; |
| 849 | struct iovec iov[1]; |
| 850 | int use_sinfo; |
| 851 | sctp_assoc_t *assoc_id; |
| 852 |
| 853 | if (so == NULL) { |
| 854 | errno = EBADF; |
| 855 | return (-1); |
| 856 | } |
| 857 | if (data == NULL) { |
| 858 | errno = EFAULT; |
| 859 | return (-1); |
| 860 | } |
| 861 | memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo)); |
| 862 | assoc_id = NULL; |
| 863 | use_sinfo = 0; |
| 864 | switch (infotype) { |
| 865 | case SCTP_SENDV_NOINFO: |
| 866 | if ((infolen != 0) || (info != NULL)) { |
| 867 | errno = EINVAL; |
| 868 | return (-1); |
| 869 | } |
| 870 | break; |
| 871 | case SCTP_SENDV_SNDINFO: |
| 872 | if ((info == NULL) || (infolen != sizeof(struct sctp_sndinfo))) { |
| 873 | errno = EINVAL; |
| 874 | return (-1); |
| 875 | } |
| 876 | sinfo.sinfo_stream = ((struct sctp_sndinfo *)info)->snd_sid; |
| 877 | sinfo.sinfo_flags = ((struct sctp_sndinfo *)info)->snd_flags; |
| 878 | sinfo.sinfo_ppid = ((struct sctp_sndinfo *)info)->snd_ppid; |
| 879 | sinfo.sinfo_context = ((struct sctp_sndinfo *)info)->snd_context; |
| 880 | sinfo.sinfo_assoc_id = ((struct sctp_sndinfo *)info)->snd_assoc_id; |
| 881 | assoc_id = &(((struct sctp_sndinfo *)info)->snd_assoc_id); |
| 882 | use_sinfo = 1; |
| 883 | break; |
| 884 | case SCTP_SENDV_PRINFO: |
| 885 | if ((info == NULL) || (infolen != sizeof(struct sctp_prinfo))) { |
| 886 | errno = EINVAL; |
| 887 | return (-1); |
| 888 | } |
| 889 | sinfo.sinfo_stream = 0; |
| 890 | sinfo.sinfo_flags = PR_SCTP_POLICY(((struct sctp_prinfo *)info)->pr_policy); |
| 891 | sinfo.sinfo_timetolive = ((struct sctp_prinfo *)info)->pr_value; |
| 892 | use_sinfo = 1; |
| 893 | break; |
| 894 | case SCTP_SENDV_AUTHINFO: |
| 895 | errno = EINVAL; |
| 896 | return (-1); |
| 897 | case SCTP_SENDV_SPA: |
| 898 | if ((info == NULL) || (infolen != sizeof(struct sctp_sendv_spa))) { |
| 899 | errno = EINVAL; |
| 900 | return (-1); |
| 901 | } |
| 902 | if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_SNDINFO_VALID) { |
| 903 | sinfo.sinfo_stream = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_sid; |
| 904 | sinfo.sinfo_flags = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_flags; |
| 905 | sinfo.sinfo_ppid = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_ppid; |
| 906 | sinfo.sinfo_context = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_context; |
| 907 | sinfo.sinfo_assoc_id = ((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_assoc_id; |
| 908 | assoc_id = &(((struct sctp_sendv_spa *)info)->sendv_sndinfo.snd_assoc_id); |
| 909 | } else { |
| 910 | sinfo.sinfo_flags = 0; |
| 911 | sinfo.sinfo_stream = 0; |
| 912 | } |
| 913 | if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_PRINFO_VALID) { |
| 914 | sinfo.sinfo_flags |= PR_SCTP_POLICY(((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_policy); |
| 915 | sinfo.sinfo_timetolive = ((struct sctp_sendv_spa *)info)->sendv_prinfo.pr_value; |
| 916 | } |
| 917 | if (((struct sctp_sendv_spa *)info)->sendv_flags & SCTP_SEND_AUTHINFO_VALID) { |
| 918 | errno = EINVAL; |
| 919 | return (-1); |
| 920 | } |
| 921 | use_sinfo = 1; |
| 922 | break; |
| 923 | default: |
| 924 | errno = EINVAL; |
| 925 | return (-1); |
| 926 | } |
| 927 |
| 928 | /* Perform error checks on destination (to) */ |
| 929 | if (addrcnt > 1) { |
| 930 | errno = EINVAL; |
| 931 | return (-1); |
| 932 | } |
| 933 |
| 934 | iov[0].iov_base = (caddr_t)data; |
| 935 | iov[0].iov_len = len; |
| 936 |
| 937 | auio.uio_iov = iov; |
| 938 | auio.uio_iovcnt = 1; |
| 939 | auio.uio_segflg = UIO_USERSPACE; |
| 940 | auio.uio_rw = UIO_WRITE; |
| 941 | auio.uio_offset = 0; /* XXX */ |
| 942 | auio.uio_resid = len; |
| 943 | errno = sctp_lower_sosend(so, to, &auio, NULL, NULL, flags, use_sinfo ? &sinfo : NULL); |
| 944 | if (errno == 0) { |
| 945 | if ((to != NULL) && (assoc_id != NULL)) { |
| 946 | *assoc_id = usrsctp_getassocid(so, to); |
| 947 | } |
| 948 | return (len - auio.uio_resid); |
| 949 | } else { |
| 950 | return (-1); |
| 951 | } |
| 952 | } |
| 953 |
| 954 |
| 955 | ssize_t |
| 956 | userspace_sctp_sendmbuf(struct socket *so, |
| 957 | struct mbuf* mbufdata, |
| 958 | size_t len, |
| 959 | struct sockaddr *to, |
| 960 | socklen_t tolen, |
| 961 | u_int32_t ppid, |
| 962 | u_int32_t flags, |
| 963 | u_int16_t stream_no, |
| 964 | u_int32_t timetolive, |
| 965 | u_int32_t context) |
| 966 | { |
| 967 |
| 968 | struct sctp_sndrcvinfo sndrcvinfo, *sinfo = &sndrcvinfo; |
| 969 | /* struct uio auio; |
| 970 | struct iovec iov[1]; */ |
| 971 | int error = 0; |
| 972 | int uflags = 0; |
| 973 | ssize_t retval; |
| 974 |
| 975 | sinfo->sinfo_ppid = ppid; |
| 976 | sinfo->sinfo_flags = flags; |
| 977 | sinfo->sinfo_stream = stream_no; |
| 978 | sinfo->sinfo_timetolive = timetolive; |
| 979 | sinfo->sinfo_context = context; |
| 980 | sinfo->sinfo_assoc_id = 0; |
| 981 |
| 982 | /* Perform error checks on destination (to) */ |
| 983 | if (tolen > SOCK_MAXADDRLEN){ |
| 984 | error = (ENAMETOOLONG); |
| 985 | goto sendmsg_return; |
| 986 | } |
| 987 | if (tolen < (socklen_t)offsetof(struct sockaddr, sa_data)){ |
| 988 | error = (EINVAL); |
| 989 | goto sendmsg_return; |
| 990 | } |
| 991 | /* Adding the following as part of defensive programming, in case the application |
| 992 | does not do it when preparing the destination address.*/ |
| 993 | #ifdef HAVE_SA_LEN |
| 994 | to->sa_len = tolen; |
| 995 | #endif |
| 996 |
| 997 | error = sctp_lower_sosend(so, to, NULL/*uio*/, |
| 998 | (struct mbuf *)mbufdata, (struct mbuf *)NULL, |
| 999 | uflags, sinfo); |
| 1000 | sendmsg_return: |
| 1001 | /* TODO: Needs a condition for non-blocking when error is EWOULDBLOCK */ |
| 1002 | if (0 == error) |
| 1003 | retval = len; |
| 1004 | else if (error == EWOULDBLOCK) { |
| 1005 | errno = EWOULDBLOCK; |
| 1006 | retval = -1; |
| 1007 | } else { |
| 1008 | SCTP_PRINTF("%s: error = %d\n", __func__, error); |
| 1009 | errno = error; |
| 1010 | retval = -1; |
| 1011 | } |
| 1012 | return (retval); |
| 1013 | } |
| 1014 |
| 1015 |
| 1016 | /* taken from usr.lib/sctp_sys_calls.c and needed here */ |
| 1017 | #define SCTP_SMALL_IOVEC_SIZE 2 |
| 1018 |
| 1019 | /* Taken from /src/lib/libc/net/sctp_sys_calls.c |
| 1020 | * and modified for __Userspace__ |
| 1021 | * calling sctp_generic_recvmsg from this function |
| 1022 | */ |
| 1023 | ssize_t |
| 1024 | userspace_sctp_recvmsg(struct socket *so, |
| 1025 | void *dbuf, |
| 1026 | size_t len, |
| 1027 | struct sockaddr *from, |
| 1028 | socklen_t *fromlenp, |
| 1029 | struct sctp_sndrcvinfo *sinfo, |
| 1030 | int *msg_flags) |
| 1031 | { |
| 1032 | struct uio auio; |
| 1033 | struct iovec iov[SCTP_SMALL_IOVEC_SIZE]; |
| 1034 | struct iovec *tiov; |
| 1035 | int iovlen = 1; |
| 1036 | int error = 0; |
| 1037 | ssize_t ulen; |
| 1038 | int i; |
| 1039 | socklen_t fromlen; |
| 1040 |
| 1041 | iov[0].iov_base = dbuf; |
| 1042 | iov[0].iov_len = len; |
| 1043 |
| 1044 | auio.uio_iov = iov; |
| 1045 | auio.uio_iovcnt = iovlen; |
| 1046 | auio.uio_segflg = UIO_USERSPACE; |
| 1047 | auio.uio_rw = UIO_READ; |
| 1048 | auio.uio_offset = 0; /* XXX */ |
| 1049 | auio.uio_resid = 0; |
| 1050 | tiov = iov; |
| 1051 | for (i = 0; i <iovlen; i++, tiov++) { |
| 1052 | if ((auio.uio_resid += tiov->iov_len) < 0) { |
| 1053 | error = EINVAL; |
| 1054 | SCTP_PRINTF("%s: error = %d\n", __func__, error); |
| 1055 | return (-1); |
| 1056 | } |
| 1057 | } |
| 1058 | ulen = auio.uio_resid; |
| 1059 | if (fromlenp != NULL) { |
| 1060 | fromlen = *fromlenp; |
| 1061 | } else { |
| 1062 | fromlen = 0; |
| 1063 | } |
| 1064 | error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL, |
| 1065 | from, fromlen, msg_flags, |
| 1066 | (struct sctp_sndrcvinfo *)sinfo, 1); |
| 1067 |
| 1068 | if (error) { |
| 1069 | if ((auio.uio_resid != ulen) && |
| 1070 | (error == EINTR || |
| 1071 | #if !defined(__Userspace_os_NetBSD) |
| 1072 | error == ERESTART || |
| 1073 | #endif |
| 1074 | error == EWOULDBLOCK)) { |
| 1075 | error = 0; |
| 1076 | } |
| 1077 | } |
| 1078 | if ((fromlenp != NULL) && (fromlen > 0) && (from != NULL)) { |
| 1079 | switch (from->sa_family) { |
| 1080 | #if defined(INET) |
| 1081 | case AF_INET: |
| 1082 | *fromlenp = sizeof(struct sockaddr_in); |
| 1083 | break; |
| 1084 | #endif |
| 1085 | #if defined(INET6) |
| 1086 | case AF_INET6: |
| 1087 | *fromlenp = sizeof(struct sockaddr_in6); |
| 1088 | break; |
| 1089 | #endif |
| 1090 | case AF_CONN: |
| 1091 | *fromlenp = sizeof(struct sockaddr_conn); |
| 1092 | break; |
| 1093 | default: |
| 1094 | *fromlenp = 0; |
| 1095 | break; |
| 1096 | } |
| 1097 | if (*fromlenp > fromlen) { |
| 1098 | *fromlenp = fromlen; |
| 1099 | } |
| 1100 | } |
| 1101 | if (error == 0) { |
| 1102 | /* ready return value */ |
| 1103 | return (ulen - auio.uio_resid); |
| 1104 | } else { |
| 1105 | SCTP_PRINTF("%s: error = %d\n", __func__, error); |
| 1106 | return (-1); |
| 1107 | } |
| 1108 | } |
| 1109 |
| 1110 | ssize_t |
| 1111 | usrsctp_recvv(struct socket *so, |
| 1112 | void *dbuf, |
| 1113 | size_t len, |
| 1114 | struct sockaddr *from, |
| 1115 | socklen_t *fromlenp, |
| 1116 | void *info, |
| 1117 | socklen_t *infolen, |
| 1118 | unsigned int *infotype, |
| 1119 | int *msg_flags) |
| 1120 | { |
| 1121 | struct uio auio; |
| 1122 | struct iovec iov[SCTP_SMALL_IOVEC_SIZE]; |
| 1123 | struct iovec *tiov; |
| 1124 | int iovlen = 1; |
| 1125 | ssize_t ulen; |
| 1126 | int i; |
| 1127 | socklen_t fromlen; |
| 1128 | struct sctp_rcvinfo *rcv; |
| 1129 | struct sctp_recvv_rn *rn; |
| 1130 | struct sctp_extrcvinfo seinfo; |
| 1131 |
| 1132 | if (so == NULL) { |
| 1133 | errno = EBADF; |
| 1134 | return (-1); |
| 1135 | } |
| 1136 | iov[0].iov_base = dbuf; |
| 1137 | iov[0].iov_len = len; |
| 1138 |
| 1139 | auio.uio_iov = iov; |
| 1140 | auio.uio_iovcnt = iovlen; |
| 1141 | auio.uio_segflg = UIO_USERSPACE; |
| 1142 | auio.uio_rw = UIO_READ; |
| 1143 | auio.uio_offset = 0; /* XXX */ |
| 1144 | auio.uio_resid = 0; |
| 1145 | tiov = iov; |
| 1146 | for (i = 0; i <iovlen; i++, tiov++) { |
| 1147 | if ((auio.uio_resid += tiov->iov_len) < 0) { |
| 1148 | errno = EINVAL; |
| 1149 | return (-1); |
| 1150 | } |
| 1151 | } |
| 1152 | ulen = auio.uio_resid; |
| 1153 | if (fromlenp != NULL) { |
| 1154 | fromlen = *fromlenp; |
| 1155 | } else { |
| 1156 | fromlen = 0; |
| 1157 | } |
| 1158 | errno = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL, |
| 1159 | from, fromlen, msg_flags, |
| 1160 | (struct sctp_sndrcvinfo *)&seinfo, 1); |
| 1161 | if (errno) { |
| 1162 | if ((auio.uio_resid != ulen) && |
| 1163 | (errno == EINTR || |
| 1164 | #if !defined(__Userspace_os_NetBSD) |
| 1165 | errno == ERESTART || |
| 1166 | #endif |
| 1167 | errno == EWOULDBLOCK)) { |
| 1168 | errno = 0; |
| 1169 | } |
| 1170 | } |
| 1171 | if (errno != 0) { |
| 1172 | goto out; |
| 1173 | } |
| 1174 | if ((*msg_flags & MSG_NOTIFICATION) == 0) { |
| 1175 | struct sctp_inpcb *inp; |
| 1176 |
| 1177 | inp = (struct sctp_inpcb *)so->so_pcb; |
| 1178 | if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO) && |
| 1179 | sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO) && |
| 1180 | *infolen >= (socklen_t)sizeof(struct sctp_recvv_rn) && |
| 1181 | seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_AVAIL) { |
| 1182 | rn = (struct sctp_recvv_rn *)info; |
| 1183 | rn->recvv_rcvinfo.rcv_sid = seinfo.sinfo_stream; |
| 1184 | rn->recvv_rcvinfo.rcv_ssn = seinfo.sinfo_ssn; |
| 1185 | rn->recvv_rcvinfo.rcv_flags = seinfo.sinfo_flags; |
| 1186 | rn->recvv_rcvinfo.rcv_ppid = seinfo.sinfo_ppid; |
| 1187 | rn->recvv_rcvinfo.rcv_context = seinfo.sinfo_context; |
| 1188 | rn->recvv_rcvinfo.rcv_tsn = seinfo.sinfo_tsn; |
| 1189 | rn->recvv_rcvinfo.rcv_cumtsn = seinfo.sinfo_cumtsn; |
| 1190 | rn->recvv_rcvinfo.rcv_assoc_id = seinfo.sinfo_assoc_id; |
| 1191 | rn->recvv_nxtinfo.nxt_sid = seinfo.sreinfo_next_stream; |
| 1192 | rn->recvv_nxtinfo.nxt_flags = 0; |
| 1193 | if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_IS_UNORDERED) { |
| 1194 | rn->recvv_nxtinfo.nxt_flags |= SCTP_UNORDERED; |
| 1195 | } |
| 1196 | if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_IS_NOTIFICATION) { |
| 1197 | rn->recvv_nxtinfo.nxt_flags |= SCTP_NOTIFICATION; |
| 1198 | } |
| 1199 | if (seinfo.sreinfo_next_flags & SCTP_NEXT_MSG_ISCOMPLETE) { |
| 1200 | rn->recvv_nxtinfo.nxt_flags |= SCTP_COMPLETE; |
| 1201 | } |
| 1202 | rn->recvv_nxtinfo.nxt_ppid = seinfo.sreinfo_next_ppid; |
| 1203 | rn->recvv_nxtinfo.nxt_length = seinfo.sreinfo_next_length; |
| 1204 | rn->recvv_nxtinfo.nxt_assoc_id = seinfo.sreinfo_next_aid; |
| 1205 | *infolen = (socklen_t)sizeof(struct sctp_recvv_rn); |
| 1206 | *infotype = SCTP_RECVV_RN; |
| 1207 | } else if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVRCVINFO) && |
| 1208 | *infolen >= (socklen_t)sizeof(struct sctp_rcvinfo)) { |
| 1209 | rcv = (struct sctp_rcvinfo *)info; |
| 1210 | rcv->rcv_sid = seinfo.sinfo_stream; |
| 1211 | rcv->rcv_ssn = seinfo.sinfo_ssn; |
| 1212 | rcv->rcv_flags = seinfo.sinfo_flags; |
| 1213 | rcv->rcv_ppid = seinfo.sinfo_ppid; |
| 1214 | rcv->rcv_context = seinfo.sinfo_context; |
| 1215 | rcv->rcv_tsn = seinfo.sinfo_tsn; |
| 1216 | rcv->rcv_cumtsn = seinfo.sinfo_cumtsn; |
| 1217 | rcv->rcv_assoc_id = seinfo.sinfo_assoc_id; |
| 1218 | *infolen = (socklen_t)sizeof(struct sctp_rcvinfo); |
| 1219 | *infotype = SCTP_RECVV_RCVINFO; |
| 1220 | } else { |
| 1221 | *infotype = SCTP_RECVV_NOINFO; |
| 1222 | *infolen = 0; |
| 1223 | } |
| 1224 | } |
| 1225 | if ((fromlenp != NULL) && |
| 1226 | (fromlen > 0) && |
| 1227 | (from != NULL) && |
| 1228 | (ulen > auio.uio_resid)) { |
| 1229 | switch (from->sa_family) { |
| 1230 | #if defined(INET) |
| 1231 | case AF_INET: |
| 1232 | *fromlenp = sizeof(struct sockaddr_in); |
| 1233 | break; |
| 1234 | #endif |
| 1235 | #if defined(INET6) |
| 1236 | case AF_INET6: |
| 1237 | *fromlenp = sizeof(struct sockaddr_in6); |
| 1238 | break; |
| 1239 | #endif |
| 1240 | case AF_CONN: |
| 1241 | *fromlenp = sizeof(struct sockaddr_conn); |
| 1242 | break; |
| 1243 | default: |
| 1244 | *fromlenp = 0; |
| 1245 | break; |
| 1246 | } |
| 1247 | if (*fromlenp > fromlen) { |
| 1248 | *fromlenp = fromlen; |
| 1249 | } |
| 1250 | } |
| 1251 | out: |
| 1252 | if (errno == 0) { |
| 1253 | /* ready return value */ |
| 1254 | return (ulen - auio.uio_resid); |
| 1255 | } else { |
| 1256 | return (-1); |
| 1257 | } |
| 1258 | } |
| 1259 |
| 1260 |
| 1261 |
| 1262 |
| 1263 | #if defined(__Userspace__) |
| 1264 | /* Taken from /src/sys/kern/uipc_socket.c |
| 1265 | * and modified for __Userspace__ |
| 1266 | * socreate returns a socket. The socket should be |
| 1267 | * closed with soclose(). |
| 1268 | */ |
| 1269 | int |
| 1270 | socreate(int dom, struct socket **aso, int type, int proto) |
| 1271 | { |
| 1272 | struct socket *so; |
| 1273 | int error; |
| 1274 |
| 1275 | if ((dom != AF_CONN) && (dom != AF_INET) && (dom != AF_INET6)) { |
| 1276 | return (EINVAL); |
| 1277 | } |
| 1278 | if ((type != SOCK_STREAM) && (type != SOCK_SEQPACKET)) { |
| 1279 | return (EINVAL); |
| 1280 | } |
| 1281 | if (proto != IPPROTO_SCTP) { |
| 1282 | return (EINVAL); |
| 1283 | } |
| 1284 |
| 1285 | so = soalloc(); |
| 1286 | if (so == NULL) { |
| 1287 | return (ENOBUFS); |
| 1288 | } |
| 1289 |
| 1290 | /* |
| 1291 | * so_incomp represents a queue of connections that |
| 1292 | * must be completed at protocol level before being |
| 1293 | * returned. so_comp field heads a list of sockets |
| 1294 | * that are ready to be returned to the listening process |
| 1295 | *__Userspace__ These queues are being used at a number of places like accept etc. |
| 1296 | */ |
| 1297 | TAILQ_INIT(&so->so_incomp); |
| 1298 | TAILQ_INIT(&so->so_comp); |
| 1299 | so->so_type = type; |
| 1300 | so->so_count = 1; |
| 1301 | so->so_dom = dom; |
| 1302 | /* |
| 1303 | * Auto-sizing of socket buffers is managed by the protocols and |
| 1304 | * the appropriate flags must be set in the pru_attach function. |
| 1305 | * For __Userspace__ The pru_attach function in this case is sctp_attach. |
| 1306 | */ |
| 1307 | switch (dom) { |
| 1308 | #if defined(INET) |
| 1309 | case AF_INET: |
| 1310 | error = sctp_attach(so, proto, SCTP_DEFAULT_VRFID); |
| 1311 | break; |
| 1312 | #endif |
| 1313 | #if defined(INET6) |
| 1314 | case AF_INET6: |
| 1315 | error = sctp6_attach(so, proto, SCTP_DEFAULT_VRFID); |
| 1316 | break; |
| 1317 | #endif |
| 1318 | case AF_CONN: |
| 1319 | error = sctpconn_attach(so, proto, SCTP_DEFAULT_VRFID); |
| 1320 | break; |
| 1321 | default: |
| 1322 | error = EAFNOSUPPORT; |
| 1323 | break; |
| 1324 | } |
| 1325 | if (error) { |
| 1326 | KASSERT(so->so_count == 1, ("socreate: so_count %d", so->so_count)); |
| 1327 | so->so_count = 0; |
| 1328 | sodealloc(so); |
| 1329 | return (error); |
| 1330 | } |
| 1331 | *aso = so; |
| 1332 | return (0); |
| 1333 | } |
| 1334 | #else |
| 1335 | /* The kernel version for reference is below. The #else |
| 1336 | should be removed once the __Userspace__ |
| 1337 | version is tested. |
| 1338 | * socreate returns a socket with a ref count of 1. The socket should be |
| 1339 | * closed with soclose(). |
| 1340 | */ |
| 1341 | int |
| 1342 | socreate(int dom, struct socket **aso, int type, int proto, |
| 1343 | struct ucred *cred, struct thread *td) |
| 1344 | { |
| 1345 | struct protosw *prp; |
| 1346 | struct socket *so; |
| 1347 | int error; |
| 1348 |
| 1349 | if (proto) |
| 1350 | prp = pffindproto(dom, proto, type); |
| 1351 | else |
| 1352 | prp = pffindtype(dom, type); |
| 1353 |
| 1354 | if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL || |
| 1355 | prp->pr_usrreqs->pru_attach == pru_attach_notsupp) |
| 1356 | return (EPROTONOSUPPORT); |
| 1357 |
| 1358 | if (jailed(cred) && jail_socket_unixiproute_only && |
| 1359 | prp->pr_domain->dom_family != PF_LOCAL && |
| 1360 | prp->pr_domain->dom_family != PF_INET && |
| 1361 | prp->pr_domain->dom_family != PF_ROUTE) { |
| 1362 | return (EPROTONOSUPPORT); |
| 1363 | } |
| 1364 |
| 1365 | if (prp->pr_type != type) |
| 1366 | return (EPROTOTYPE); |
| 1367 | so = soalloc(); |
| 1368 | if (so == NULL) |
| 1369 | return (ENOBUFS); |
| 1370 |
| 1371 | TAILQ_INIT(&so->so_incomp); |
| 1372 | TAILQ_INIT(&so->so_comp); |
| 1373 | so->so_type = type; |
| 1374 | so->so_cred = crhold(cred); |
| 1375 | so->so_proto = prp; |
| 1376 | #ifdef MAC |
| 1377 | mac_create_socket(cred, so); |
| 1378 | #endif |
| 1379 | knlist_init(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv), |
| 1380 | NULL, NULL, NULL); |
| 1381 | knlist_init(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd), |
| 1382 | NULL, NULL, NULL); |
| 1383 | so->so_count = 1; |
| 1384 | /* |
| 1385 | * Auto-sizing of socket buffers is managed by the protocols and |
| 1386 | * the appropriate flags must be set in the pru_attach function. |
| 1387 | */ |
| 1388 | error = (*prp->pr_usrreqs->pru_attach)(so, proto, td); |
| 1389 | if (error) { |
| 1390 | KASSERT(so->so_count == 1, ("socreate: so_count %d", |
| 1391 | so->so_count)); |
| 1392 | so->so_count = 0; |
| 1393 | sodealloc(so); |
| 1394 | return (error); |
| 1395 | } |
| 1396 | *aso = so; |
| 1397 | return (0); |
| 1398 | } |
| 1399 | #endif |
| 1400 |
| 1401 |
| 1402 |
| 1403 |
| 1404 | /* Taken from /src/sys/kern/uipc_syscalls.c |
| 1405 | * and modified for __Userspace__ |
| 1406 | * Removing struct thread td. |
| 1407 | */ |
| 1408 | struct socket * |
| 1409 | userspace_socket(int domain, int type, int protocol) |
| 1410 | { |
| 1411 | struct socket *so = NULL; |
| 1412 |
| 1413 | errno = socreate(domain, &so, type, protocol); |
| 1414 | if (errno) { |
| 1415 | return (NULL); |
| 1416 | } |
| 1417 | /* |
| 1418 | * The original socket call returns the file descriptor fd. |
| 1419 | * td->td_retval[0] = fd. |
| 1420 | * We are returning struct socket *so. |
| 1421 | */ |
| 1422 | return (so); |
| 1423 | } |
| 1424 |
| 1425 | struct socket * |
| 1426 | usrsctp_socket(int domain, int type, int protocol, |
| 1427 | int (*receive_cb)(struct socket *sock, union sctp_sockstore addr, void *data, |
| 1428 | size_t datalen, struct sctp_rcvinfo, int flags, void *ulp_info), |
| 1429 | int (*send_cb)(struct socket *sock, uint32_t sb_free), |
| 1430 | uint32_t sb_threshold, |
| 1431 | void *ulp_info) |
| 1432 | { |
| 1433 | struct socket *so; |
| 1434 |
| 1435 | if ((protocol == IPPROTO_SCTP) && (SCTP_BASE_VAR(sctp_pcb_initialized) == 0)) { |
| 1436 | errno = EPROTONOSUPPORT; |
| 1437 | return (NULL); |
| 1438 | } |
| 1439 | if ((receive_cb == NULL) && |
| 1440 | ((send_cb != NULL) || (sb_threshold != 0) || (ulp_info != NULL))) { |
| 1441 | errno = EINVAL; |
| 1442 | return (NULL); |
| 1443 | } |
| 1444 | if ((domain == AF_CONN) && (SCTP_BASE_VAR(conn_output) == NULL)) { |
| 1445 | errno = EAFNOSUPPORT; |
| 1446 | return (NULL); |
| 1447 | } |
| 1448 | errno = socreate(domain, &so, type, protocol); |
| 1449 | if (errno) { |
| 1450 | return (NULL); |
| 1451 | } |
| 1452 | /* |
| 1453 | * The original socket call returns the file descriptor fd. |
| 1454 | * td->td_retval[0] = fd. |
| 1455 | * We are returning struct socket *so. |
| 1456 | */ |
| 1457 | register_recv_cb(so, receive_cb); |
| 1458 | register_send_cb(so, sb_threshold, send_cb); |
| 1459 | register_ulp_info(so, ulp_info); |
| 1460 | return (so); |
| 1461 | } |
| 1462 |
| 1463 |
| 1464 | u_long sb_max = SB_MAX; |
| 1465 | u_long sb_max_adj = |
| 1466 | SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ |
| 1467 |
| 1468 | static u_long sb_efficiency = 8; /* parameter for sbreserve() */ |
| 1469 |
| 1470 | /* |
| 1471 | * Allot mbufs to a sockbuf. Attempt to scale mbmax so that mbcnt doesn't |
| 1472 | * become limiting if buffering efficiency is near the normal case. |
| 1473 | */ |
| 1474 | int |
| 1475 | sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so) |
| 1476 | { |
| 1477 | SOCKBUF_LOCK_ASSERT(sb); |
| 1478 | sb->sb_mbmax = (u_int)min(cc * sb_efficiency, sb_max); |
| 1479 | sb->sb_hiwat = (u_int)cc; |
| 1480 | if (sb->sb_lowat > (int)sb->sb_hiwat) |
| 1481 | sb->sb_lowat = (int)sb->sb_hiwat; |
| 1482 | return (1); |
| 1483 | } |
| 1484 |
| 1485 | static int |
| 1486 | sbreserve(struct sockbuf *sb, u_long cc, struct socket *so) |
| 1487 | { |
| 1488 | int error; |
| 1489 |
| 1490 | SOCKBUF_LOCK(sb); |
| 1491 | error = sbreserve_locked(sb, cc, so); |
| 1492 | SOCKBUF_UNLOCK(sb); |
| 1493 | return (error); |
| 1494 | } |
| 1495 |
| 1496 | #if defined(__Userspace__) |
| 1497 | int |
| 1498 | soreserve(struct socket *so, u_long sndcc, u_long rcvcc) |
| 1499 | { |
| 1500 | SOCKBUF_LOCK(&so->so_snd); |
| 1501 | SOCKBUF_LOCK(&so->so_rcv); |
| 1502 | so->so_snd.sb_hiwat = (uint32_t)sndcc; |
| 1503 | so->so_rcv.sb_hiwat = (uint32_t)rcvcc; |
| 1504 |
| 1505 | if (sbreserve_locked(&so->so_snd, sndcc, so) == 0) { |
| 1506 | goto bad; |
| 1507 | } |
| 1508 | if (sbreserve_locked(&so->so_rcv, rcvcc, so) == 0) { |
| 1509 | goto bad; |
| 1510 | } |
| 1511 | if (so->so_rcv.sb_lowat == 0) |
| 1512 | so->so_rcv.sb_lowat = 1; |
| 1513 | if (so->so_snd.sb_lowat == 0) |
| 1514 | so->so_snd.sb_lowat = MCLBYTES; |
| 1515 | if (so->so_snd.sb_lowat > (int)so->so_snd.sb_hiwat) |
| 1516 | so->so_snd.sb_lowat = (int)so->so_snd.sb_hiwat; |
| 1517 | SOCKBUF_UNLOCK(&so->so_rcv); |
| 1518 | SOCKBUF_UNLOCK(&so->so_snd); |
| 1519 | return (0); |
| 1520 |
| 1521 | bad: |
| 1522 | SOCKBUF_UNLOCK(&so->so_rcv); |
| 1523 | SOCKBUF_UNLOCK(&so->so_snd); |
| 1524 | return (ENOBUFS); |
| 1525 | } |
| 1526 | #else /* kernel version for reference */ |
| 1527 | int |
| 1528 | soreserve(struct socket *so, u_long sndcc, u_long rcvcc) |
| 1529 | { |
| 1530 | struct thread *td = curthread; |
| 1531 |
| 1532 | SOCKBUF_LOCK(&so->so_snd); |
| 1533 | SOCKBUF_LOCK(&so->so_rcv); |
| 1534 | if (sbreserve_locked(&so->so_snd, sndcc, so, td) == 0) |
| 1535 | goto bad; |
| 1536 | if (sbreserve_locked(&so->so_rcv, rcvcc, so, td) == 0) |
| 1537 | goto bad2; |
| 1538 | if (so->so_rcv.sb_lowat == 0) |
| 1539 | so->so_rcv.sb_lowat = 1; |
| 1540 | if (so->so_snd.sb_lowat == 0) |
| 1541 | so->so_snd.sb_lowat = MCLBYTES; |
| 1542 | if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat) |
| 1543 | so->so_snd.sb_lowat = so->so_snd.sb_hiwat; |
| 1544 | SOCKBUF_UNLOCK(&so->so_rcv); |
| 1545 | SOCKBUF_UNLOCK(&so->so_snd); |
| 1546 | return (0); |
| 1547 | bad2: |
| 1548 | sbrelease_locked(&so->so_snd, so); |
| 1549 | bad: |
| 1550 | SOCKBUF_UNLOCK(&so->so_rcv); |
| 1551 | SOCKBUF_UNLOCK(&so->so_snd); |
| 1552 | return (ENOBUFS); |
| 1553 | } |
| 1554 | #endif |
| 1555 |
| 1556 |
| 1557 |
| 1558 |
| 1559 |
| 1560 | /* Taken from /src/sys/kern/uipc_sockbuf.c |
| 1561 | * and modified for __Userspace__ |
| 1562 | */ |
| 1563 |
| 1564 | #if defined(__Userspace__) |
| 1565 | void |
| 1566 | sowakeup(struct socket *so, struct sockbuf *sb) |
| 1567 | { |
| 1568 |
| 1569 | SOCKBUF_LOCK_ASSERT(sb); |
| 1570 |
| 1571 | sb->sb_flags &= ~SB_SEL; |
| 1572 | if (sb->sb_flags & SB_WAIT) { |
| 1573 | sb->sb_flags &= ~SB_WAIT; |
| 1574 | #if defined (__Userspace_os_Windows) |
| 1575 | WakeAllConditionVariable(&(sb)->sb_cond); |
| 1576 | #else |
| 1577 | pthread_cond_broadcast(&(sb)->sb_cond); |
| 1578 | #endif |
| 1579 | } |
| 1580 | SOCKBUF_UNLOCK(sb); |
| 1581 | } |
| 1582 | #else /* kernel version for reference */ |
| 1583 | /* |
| 1584 | * Wakeup processes waiting on a socket buffer. Do asynchronous notification |
| 1585 | * via SIGIO if the socket has the SS_ASYNC flag set. |
| 1586 | * |
| 1587 | * Called with the socket buffer lock held; will release the lock by the end |
| 1588 | * of the function. This allows the caller to acquire the socket buffer lock |
| 1589 | * while testing for the need for various sorts of wakeup and hold it through |
| 1590 | * to the point where it's no longer required. We currently hold the lock |
| 1591 | * through calls out to other subsystems (with the exception of kqueue), and |
| 1592 | * then release it to avoid lock order issues. It's not clear that's |
| 1593 | * correct. |
| 1594 | */ |
| 1595 | void |
| 1596 | sowakeup(struct socket *so, struct sockbuf *sb) |
| 1597 | { |
| 1598 |
| 1599 | SOCKBUF_LOCK_ASSERT(sb); |
| 1600 |
| 1601 | selwakeuppri(&sb->sb_sel, PSOCK); |
| 1602 | sb->sb_flags &= ~SB_SEL; |
| 1603 | if (sb->sb_flags & SB_WAIT) { |
| 1604 | sb->sb_flags &= ~SB_WAIT; |
| 1605 | wakeup(&sb->sb_cc); |
| 1606 | } |
| 1607 | KNOTE_LOCKED(&sb->sb_sel.si_note, 0); |
| 1608 | SOCKBUF_UNLOCK(sb); |
| 1609 | if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) |
| 1610 | pgsigio(&so->so_sigio, SIGIO, 0); |
| 1611 | if (sb->sb_flags & SB_UPCALL) |
| 1612 | (*so->so_upcall)(so, so->so_upcallarg, M_NOWAIT); |
| 1613 | if (sb->sb_flags & SB_AIO) |
| 1614 | aio_swake(so, sb); |
| 1615 | mtx_assert(SOCKBUF_MTX(sb), MA_NOTOWNED); |
| 1616 | } |
| 1617 | #endif |
| 1618 |
| 1619 |
| 1620 |
| 1621 | /* Taken from /src/sys/kern/uipc_socket.c |
| 1622 | * and modified for __Userspace__ |
| 1623 | */ |
| 1624 |
| 1625 | int |
| 1626 | sobind(struct socket *so, struct sockaddr *nam) |
| 1627 | { |
| 1628 | switch (nam->sa_family) { |
| 1629 | #if defined(INET) |
| 1630 | case AF_INET: |
| 1631 | return (sctp_bind(so, nam)); |
| 1632 | #endif |
| 1633 | #if defined(INET6) |
| 1634 | case AF_INET6: |
| 1635 | return (sctp6_bind(so, nam, NULL)); |
| 1636 | #endif |
| 1637 | case AF_CONN: |
| 1638 | return (sctpconn_bind(so, nam)); |
| 1639 | default: |
| 1640 | return EAFNOSUPPORT; |
| 1641 | } |
| 1642 | } |
| 1643 |
| 1644 | /* Taken from /src/sys/kern/uipc_syscalls.c |
| 1645 | * and modified for __Userspace__ |
| 1646 | */ |
| 1647 |
| 1648 | int |
| 1649 | usrsctp_bind(struct socket *so, struct sockaddr *name, int namelen) |
| 1650 | { |
| 1651 | struct sockaddr *sa; |
| 1652 |
| 1653 | if (so == NULL) { |
| 1654 | errno = EBADF; |
| 1655 | return (-1); |
| 1656 | } |
| 1657 | if ((errno = getsockaddr(&sa, (caddr_t)name, namelen)) != 0) |
| 1658 | return (-1); |
| 1659 |
| 1660 | errno = sobind(so, sa); |
| 1661 | FREE(sa, M_SONAME); |
| 1662 | if (errno) { |
| 1663 | return (-1); |
| 1664 | } else { |
| 1665 | return (0); |
| 1666 | } |
| 1667 | } |
| 1668 |
| 1669 | int |
| 1670 | userspace_bind(struct socket *so, struct sockaddr *name, int namelen) |
| 1671 | { |
| 1672 | return (usrsctp_bind(so, name, namelen)); |
| 1673 | } |
| 1674 |
| 1675 | /* Taken from /src/sys/kern/uipc_socket.c |
| 1676 | * and modified for __Userspace__ |
| 1677 | */ |
| 1678 |
| 1679 | int |
| 1680 | solisten(struct socket *so, int backlog) |
| 1681 | { |
| 1682 | if (so == NULL) { |
| 1683 | return (EBADF); |
| 1684 | } else { |
| 1685 | return (sctp_listen(so, backlog, NULL)); |
| 1686 | } |
| 1687 | } |
| 1688 |
| 1689 |
| 1690 | int |
| 1691 | solisten_proto_check(struct socket *so) |
| 1692 | { |
| 1693 |
| 1694 | SOCK_LOCK_ASSERT(so); |
| 1695 |
| 1696 | if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | |
| 1697 | SS_ISDISCONNECTING)) |
| 1698 | return (EINVAL); |
| 1699 | return (0); |
| 1700 | } |
| 1701 |
| 1702 | static int somaxconn = SOMAXCONN; |
| 1703 |
| 1704 | void |
| 1705 | solisten_proto(struct socket *so, int backlog) |
| 1706 | { |
| 1707 |
| 1708 | SOCK_LOCK_ASSERT(so); |
| 1709 |
| 1710 | if (backlog < 0 || backlog > somaxconn) |
| 1711 | backlog = somaxconn; |
| 1712 | so->so_qlimit = backlog; |
| 1713 | so->so_options |= SCTP_SO_ACCEPTCONN; |
| 1714 | } |
| 1715 |
| 1716 |
| 1717 |
| 1718 |
| 1719 | /* Taken from /src/sys/kern/uipc_syscalls.c |
| 1720 | * and modified for __Userspace__ |
| 1721 | */ |
| 1722 |
| 1723 | int |
| 1724 | usrsctp_listen(struct socket *so, int backlog) |
| 1725 | { |
| 1726 | errno = solisten(so, backlog); |
| 1727 | if (errno) { |
| 1728 | return (-1); |
| 1729 | } else { |
| 1730 | return (0); |
| 1731 | } |
| 1732 | } |
| 1733 |
| 1734 | int |
| 1735 | userspace_listen(struct socket *so, int backlog) |
| 1736 | { |
| 1737 | return (usrsctp_listen(so, backlog)); |
| 1738 | } |
| 1739 |
| 1740 | /* Taken from /src/sys/kern/uipc_socket.c |
| 1741 | * and modified for __Userspace__ |
| 1742 | */ |
| 1743 |
| 1744 | int |
| 1745 | soaccept(struct socket *so, struct sockaddr **nam) |
| 1746 | { |
| 1747 | int error; |
| 1748 |
| 1749 | SOCK_LOCK(so); |
| 1750 | KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF")); |
| 1751 | so->so_state &= ~SS_NOFDREF; |
| 1752 | SOCK_UNLOCK(so); |
| 1753 | error = sctp_accept(so, nam); |
| 1754 | return (error); |
| 1755 | } |
| 1756 |
| 1757 |
| 1758 |
| 1759 | /* Taken from /src/sys/kern/uipc_syscalls.c |
| 1760 | * kern_accept modified for __Userspace__ |
| 1761 | */ |
| 1762 | int |
| 1763 | user_accept(struct socket *head, struct sockaddr **name, socklen_t *namelen, struct socket **ptr_accept_ret_sock) |
| 1764 | { |
| 1765 | struct sockaddr *sa = NULL; |
| 1766 | int error; |
| 1767 | struct socket *so = NULL; |
| 1768 |
| 1769 |
| 1770 | if (name) { |
| 1771 | *name = NULL; |
| 1772 | } |
| 1773 |
| 1774 | if ((head->so_options & SCTP_SO_ACCEPTCONN) == 0) { |
| 1775 | error = EINVAL; |
| 1776 | goto done; |
| 1777 | } |
| 1778 |
| 1779 | ACCEPT_LOCK(); |
| 1780 | if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) { |
| 1781 | ACCEPT_UNLOCK(); |
| 1782 | error = EWOULDBLOCK; |
| 1783 | goto noconnection; |
| 1784 | } |
| 1785 | while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) { |
| 1786 | if (head->so_rcv.sb_state & SBS_CANTRCVMORE) { |
| 1787 | head->so_error = ECONNABORTED; |
| 1788 | break; |
| 1789 | } |
| 1790 | #if defined (__Userspace_os_Windows) |
| 1791 | if (SleepConditionVariableCS(&accept_cond, &accept_mtx, INFINITE)) |
| 1792 | error = 0; |
| 1793 | else |
| 1794 | error = GetLastError(); |
| 1795 | #else |
| 1796 | error = pthread_cond_wait(&accept_cond, &accept_mtx); |
| 1797 | #endif |
| 1798 | if (error) { |
| 1799 | ACCEPT_UNLOCK(); |
| 1800 | goto noconnection; |
| 1801 | } |
| 1802 | } |
| 1803 | if (head->so_error) { |
| 1804 | error = head->so_error; |
| 1805 | head->so_error = 0; |
| 1806 | ACCEPT_UNLOCK(); |
| 1807 | goto noconnection; |
| 1808 | } |
| 1809 | so = TAILQ_FIRST(&head->so_comp); |
| 1810 | KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP")); |
| 1811 | KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP")); |
| 1812 |
| 1813 | /* |
| 1814 | * Before changing the flags on the socket, we have to bump the |
| 1815 | * reference count. Otherwise, if the protocol calls sofree(), |
| 1816 | * the socket will be released due to a zero refcount. |
| 1817 | */ |
| 1818 | SOCK_LOCK(so); /* soref() and so_state update */ |
| 1819 | soref(so); /* file descriptor reference */ |
| 1820 |
| 1821 | TAILQ_REMOVE(&head->so_comp, so, so_list); |
| 1822 | head->so_qlen--; |
| 1823 | so->so_state |= (head->so_state & SS_NBIO); |
| 1824 | so->so_qstate &= ~SQ_COMP; |
| 1825 | so->so_head = NULL; |
| 1826 | SOCK_UNLOCK(so); |
| 1827 | ACCEPT_UNLOCK(); |
| 1828 |
| 1829 |
| 1830 | /* |
| 1831 | * The original accept returns fd value via td->td_retval[0] = fd; |
| 1832 | * we will return the socket for accepted connection. |
| 1833 | */ |
| 1834 |
| 1835 | error = soaccept(so, &sa); |
| 1836 | if (error) { |
| 1837 | /* |
| 1838 | * return a namelen of zero for older code which might |
| 1839 | * ignore the return value from accept. |
| 1840 | */ |
| 1841 | if (name) |
| 1842 | *namelen = 0; |
| 1843 | goto noconnection; |
| 1844 | } |
| 1845 | if (sa == NULL) { |
| 1846 | if (name) |
| 1847 | *namelen = 0; |
| 1848 | goto done; |
| 1849 | } |
| 1850 | if (name) { |
| 1851 | #ifdef HAVE_SA_LEN |
| 1852 | /* check sa_len before it is destroyed */ |
| 1853 | if (*namelen > sa->sa_len) { |
| 1854 | *namelen = sa->sa_len; |
| 1855 | } |
| 1856 | #else |
| 1857 | socklen_t sa_len; |
| 1858 |
| 1859 | switch (sa->sa_family) { |
| 1860 | #ifdef INET |
| 1861 | case AF_INET: |
| 1862 | sa_len = sizeof(struct sockaddr_in); |
| 1863 | break; |
| 1864 | #endif |
| 1865 | #ifdef INET6 |
| 1866 | case AF_INET6: |
| 1867 | sa_len = sizeof(struct sockaddr_in6); |
| 1868 | break; |
| 1869 | #endif |
| 1870 | case AF_CONN: |
| 1871 | sa_len = sizeof(struct sockaddr_conn); |
| 1872 | break; |
| 1873 | default: |
| 1874 | sa_len = 0; |
| 1875 | break; |
| 1876 | } |
| 1877 | if (*namelen > sa_len) { |
| 1878 | *namelen = sa_len; |
| 1879 | } |
| 1880 | #endif |
| 1881 | *name = sa; |
| 1882 | sa = NULL; |
| 1883 | } |
| 1884 | noconnection: |
| 1885 | if (sa) { |
| 1886 | FREE(sa, M_SONAME); |
| 1887 | } |
| 1888 |
| 1889 | done: |
| 1890 | *ptr_accept_ret_sock = so; |
| 1891 | return (error); |
| 1892 | } |
| 1893 |
| 1894 |
| 1895 |
| 1896 | /* Taken from /src/sys/kern/uipc_syscalls.c |
| 1897 | * and modified for __Userspace__ |
| 1898 | */ |
| 1899 | /* |
| 1900 | * accept1() |
| 1901 | */ |
| 1902 | static int |
| 1903 | accept1(struct socket *so, struct sockaddr *aname, socklen_t *anamelen, struct socket **ptr_accept_ret_sock) |
| 1904 | { |
| 1905 | struct sockaddr *name; |
| 1906 | socklen_t namelen; |
| 1907 | int error; |
| 1908 |
| 1909 | if (so == NULL) { |
| 1910 | return (EBADF); |
| 1911 | } |
| 1912 | if (aname == NULL) { |
| 1913 | return (user_accept(so, NULL, NULL, ptr_accept_ret_sock)); |
| 1914 | } |
| 1915 |
| 1916 | error = copyin(anamelen, &namelen, sizeof (namelen)); |
| 1917 | if (error) |
| 1918 | return (error); |
| 1919 |
| 1920 | error = user_accept(so, &name, &namelen, ptr_accept_ret_sock); |
| 1921 |
| 1922 | /* |
| 1923 | * return a namelen of zero for older code which might |
| 1924 | * ignore the return value from accept. |
| 1925 | */ |
| 1926 | if (error) { |
| 1927 | (void) copyout(&namelen, |
| 1928 | anamelen, sizeof(*anamelen)); |
| 1929 | return (error); |
| 1930 | } |
| 1931 |
| 1932 | if (error == 0 && name != NULL) { |
| 1933 | error = copyout(name, aname, namelen); |
| 1934 | } |
| 1935 | if (error == 0) { |
| 1936 | error = copyout(&namelen, anamelen, sizeof(namelen)); |
| 1937 | } |
| 1938 |
| 1939 | if (name) { |
| 1940 | FREE(name, M_SONAME); |
| 1941 | } |
| 1942 | return (error); |
| 1943 | } |
| 1944 |
| 1945 | struct socket * |
| 1946 | usrsctp_accept(struct socket *so, struct sockaddr *aname, socklen_t *anamelen) |
| 1947 | { |
| 1948 | struct socket *accept_return_sock = NULL; |
| 1949 |
| 1950 | errno = accept1(so, aname, anamelen, &accept_return_sock); |
| 1951 | if (errno) { |
| 1952 | return (NULL); |
| 1953 | } else { |
| 1954 | return (accept_return_sock); |
| 1955 | } |
| 1956 | } |
| 1957 |
| 1958 | struct socket * |
| 1959 | userspace_accept(struct socket *so, struct sockaddr *aname, socklen_t *anamelen) |
| 1960 | { |
| 1961 | return (usrsctp_accept(so, aname, anamelen)); |
| 1962 | } |
| 1963 |
| 1964 | struct socket * |
| 1965 | usrsctp_peeloff(struct socket *head, sctp_assoc_t id) |
| 1966 | { |
| 1967 | struct socket *so; |
| 1968 |
| 1969 | if ((errno = sctp_can_peel_off(head, id)) != 0) { |
| 1970 | return (NULL); |
| 1971 | } |
| 1972 | if ((so = sonewconn(head, SS_ISCONNECTED)) == NULL) { |
| 1973 | return (NULL); |
| 1974 | } |
| 1975 | ACCEPT_LOCK(); |
| 1976 | SOCK_LOCK(so); |
| 1977 | soref(so); |
| 1978 | TAILQ_REMOVE(&head->so_comp, so, so_list); |
| 1979 | head->so_qlen--; |
| 1980 | so->so_state |= (head->so_state & SS_NBIO); |
| 1981 | so->so_qstate &= ~SQ_COMP; |
| 1982 | so->so_head = NULL; |
| 1983 | SOCK_UNLOCK(so); |
| 1984 | ACCEPT_UNLOCK(); |
| 1985 | if ((errno = sctp_do_peeloff(head, so, id)) != 0) { |
| 1986 | so->so_count = 0; |
| 1987 | sodealloc(so); |
| 1988 | return (NULL); |
| 1989 | } |
| 1990 | return (so); |
| 1991 | } |
| 1992 |
| 1993 | int |
| 1994 | sodisconnect(struct socket *so) |
| 1995 | { |
| 1996 | int error; |
| 1997 |
| 1998 | if ((so->so_state & SS_ISCONNECTED) == 0) |
| 1999 | return (ENOTCONN); |
| 2000 | if (so->so_state & SS_ISDISCONNECTING) |
| 2001 | return (EALREADY); |
| 2002 | error = sctp_disconnect(so); |
| 2003 | return (error); |
| 2004 | } |
| 2005 |
| 2006 | int |
| 2007 | usrsctp_set_non_blocking(struct socket *so, int onoff) |
| 2008 | { |
| 2009 | if (so == NULL) { |
| 2010 | errno = EBADF; |
| 2011 | return (-1); |
| 2012 | } |
| 2013 | SOCK_LOCK(so); |
| 2014 | if (onoff != 0) { |
| 2015 | so->so_state |= SS_NBIO; |
| 2016 | } else { |
| 2017 | so->so_state &= ~SS_NBIO; |
| 2018 | } |
| 2019 | SOCK_UNLOCK(so); |
| 2020 | return (0); |
| 2021 | } |
| 2022 |
| 2023 | int |
| 2024 | usrsctp_get_non_blocking(struct socket *so) |
| 2025 | { |
| 2026 | int result; |
| 2027 |
| 2028 | if (so == NULL) { |
| 2029 | errno = EBADF; |
| 2030 | return (-1); |
| 2031 | } |
| 2032 | SOCK_LOCK(so); |
| 2033 | if (so->so_state & SS_NBIO) { |
| 2034 | result = 1; |
| 2035 | } else { |
| 2036 | result = 0; |
| 2037 | } |
| 2038 | SOCK_UNLOCK(so); |
| 2039 | return (result); |
| 2040 | } |
| 2041 |
| 2042 | int |
| 2043 | soconnect(struct socket *so, struct sockaddr *nam) |
| 2044 | { |
| 2045 | int error; |
| 2046 |
| 2047 | if (so->so_options & SCTP_SO_ACCEPTCONN) |
| 2048 | return (EOPNOTSUPP); |
| 2049 | /* |
| 2050 | * If protocol is connection-based, can only connect once. |
| 2051 | * Otherwise, if connected, try to disconnect first. This allows |
| 2052 | * user to disconnect by connecting to, e.g., a null address. |
| 2053 | */ |
| 2054 | if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) && (error = sodisconnect(so))) { |
| 2055 | error = EISCONN; |
| 2056 | } else { |
| 2057 | /* |
| 2058 | * Prevent accumulated error from previous connection from |
| 2059 | * biting us. |
| 2060 | */ |
| 2061 | so->so_error = 0; |
| 2062 | switch (nam->sa_family) { |
| 2063 | #if defined(INET) |
| 2064 | case AF_INET: |
| 2065 | error = sctp_connect(so, nam); |
| 2066 | break; |
| 2067 | #endif |
| 2068 | #if defined(INET6) |
| 2069 | case AF_INET6: |
| 2070 | error = sctp6_connect(so, nam); |
| 2071 | break; |
| 2072 | #endif |
| 2073 | case AF_CONN: |
| 2074 | error = sctpconn_connect(so, nam); |
| 2075 | break; |
| 2076 | default: |
| 2077 | error = EAFNOSUPPORT; |
| 2078 | } |
| 2079 | } |
| 2080 |
| 2081 | return (error); |
| 2082 | } |
| 2083 |
| 2084 |
| 2085 |
| 2086 | int user_connect(struct socket *so, struct sockaddr *sa) |
| 2087 | { |
| 2088 | int error; |
| 2089 | int interrupted = 0; |
| 2090 |
| 2091 | if (so == NULL) { |
| 2092 | error = EBADF; |
| 2093 | goto done1; |
| 2094 | } |
| 2095 | if (so->so_state & SS_ISCONNECTING) { |
| 2096 | error = EALREADY; |
| 2097 | goto done1; |
| 2098 | } |
| 2099 |
| 2100 | error = soconnect(so, sa); |
| 2101 | if (error) { |
| 2102 | goto bad; |
| 2103 | } |
| 2104 | if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) { |
| 2105 | error = EINPROGRESS; |
| 2106 | goto done1; |
| 2107 | } |
| 2108 |
| 2109 | SOCK_LOCK(so); |
| 2110 | while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) { |
| 2111 | #if defined (__Userspace_os_Windows) |
| 2112 | if (SleepConditionVariableCS(SOCK_COND(so), SOCK_MTX(so), INFINITE)) |
| 2113 | error = 0; |
| 2114 | else |
| 2115 | error = -1; |
| 2116 | #else |
| 2117 | error = pthread_cond_wait(SOCK_COND(so), SOCK_MTX(so)); |
| 2118 | #endif |
| 2119 | if (error) { |
| 2120 | #if defined(__Userspace_os_NetBSD) |
| 2121 | if (error == EINTR) { |
| 2122 | #else |
| 2123 | if (error == EINTR || error == ERESTART) { |
| 2124 | #endif |
| 2125 | interrupted = 1; |
| 2126 | } |
| 2127 | break; |
| 2128 | } |
| 2129 | } |
| 2130 | if (error == 0) { |
| 2131 | error = so->so_error; |
| 2132 | so->so_error = 0; |
| 2133 | } |
| 2134 | SOCK_UNLOCK(so); |
| 2135 |
| 2136 | bad: |
| 2137 | if (!interrupted) { |
| 2138 | so->so_state &= ~SS_ISCONNECTING; |
| 2139 | } |
| 2140 | #if !defined(__Userspace_os_NetBSD) |
| 2141 | if (error == ERESTART) { |
| 2142 | error = EINTR; |
| 2143 | } |
| 2144 | #endif |
| 2145 | done1: |
| 2146 | return (error); |
| 2147 | } |
| 2148 |
| 2149 | int usrsctp_connect(struct socket *so, struct sockaddr *name, int namelen) |
| 2150 | { |
| 2151 | struct sockaddr *sa = NULL; |
| 2152 |
| 2153 | errno = getsockaddr(&sa, (caddr_t)name, namelen); |
| 2154 | if (errno) |
| 2155 | return (-1); |
| 2156 |
| 2157 | errno = user_connect(so, sa); |
| 2158 | FREE(sa, M_SONAME); |
| 2159 | if (errno) { |
| 2160 | return (-1); |
| 2161 | } else { |
| 2162 | return (0); |
| 2163 | } |
| 2164 | } |
| 2165 |
| 2166 | int userspace_connect(struct socket *so, struct sockaddr *name, int namelen) |
| 2167 | { |
| 2168 | return (usrsctp_connect(so, name, namelen)); |
| 2169 | } |
| 2170 |
| 2171 | #define SCTP_STACK_BUF_SIZE 2048 |
| 2172 |
| 2173 | void |
| 2174 | usrsctp_close(struct socket *so) { |
| 2175 | if (so != NULL) { |
| 2176 | if (so->so_options & SCTP_SO_ACCEPTCONN) { |
| 2177 | struct socket *sp; |
| 2178 |
| 2179 | ACCEPT_LOCK(); |
| 2180 | while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) { |
| 2181 | TAILQ_REMOVE(&so->so_comp, sp, so_list); |
| 2182 | so->so_qlen--; |
| 2183 | sp->so_qstate &= ~SQ_COMP; |
| 2184 | sp->so_head = NULL; |
| 2185 | ACCEPT_UNLOCK(); |
| 2186 | soabort(sp); |
| 2187 | ACCEPT_LOCK(); |
| 2188 | } |
| 2189 | ACCEPT_UNLOCK(); |
| 2190 | } |
| 2191 | ACCEPT_LOCK(); |
| 2192 | SOCK_LOCK(so); |
| 2193 | sorele(so); |
| 2194 | } |
| 2195 | } |
| 2196 |
| 2197 | void |
| 2198 | userspace_close(struct socket *so) |
| 2199 | { |
| 2200 | usrsctp_close(so); |
| 2201 | } |
| 2202 |
| 2203 | int |
| 2204 | usrsctp_shutdown(struct socket *so, int how) |
| 2205 | { |
| 2206 | if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) { |
| 2207 | errno = EINVAL; |
| 2208 | return (-1); |
| 2209 | } |
| 2210 | if (so == NULL) { |
| 2211 | errno = EBADF; |
| 2212 | return (-1); |
| 2213 | } |
| 2214 | sctp_flush(so, how); |
| 2215 | if (how != SHUT_WR) |
| 2216 | socantrcvmore(so); |
| 2217 | if (how != SHUT_RD) { |
| 2218 | errno = sctp_shutdown(so); |
| 2219 | if (errno) { |
| 2220 | return (-1); |
| 2221 | } else { |
| 2222 | return (0); |
| 2223 | } |
| 2224 | } |
| 2225 | return (0); |
| 2226 | } |
| 2227 |
| 2228 | int |
| 2229 | userspace_shutdown(struct socket *so, int how) |
| 2230 | { |
| 2231 | return (usrsctp_shutdown(so, how)); |
| 2232 | } |
| 2233 |
| 2234 | int |
| 2235 | usrsctp_finish(void) |
| 2236 | { |
| 2237 | if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) { |
| 2238 | return (0); |
| 2239 | } |
| 2240 | if (SCTP_INP_INFO_TRYLOCK()) { |
| 2241 | if (!LIST_EMPTY(&SCTP_BASE_INFO(listhead))) { |
| 2242 | SCTP_INP_INFO_RUNLOCK(); |
| 2243 | return (-1); |
| 2244 | } |
| 2245 | SCTP_INP_INFO_RUNLOCK(); |
| 2246 | } else { |
| 2247 | return (-1); |
| 2248 | } |
| 2249 | sctp_finish(); |
| 2250 | #if defined(__Userspace_os_Windows) |
| 2251 | DeleteConditionVariable(&accept_cond); |
| 2252 | DeleteCriticalSection(&accept_mtx); |
| 2253 | #if defined(INET) || defined(INET6) |
| 2254 | WSACleanup(); |
| 2255 | #endif |
| 2256 | #else |
| 2257 | pthread_cond_destroy(&accept_cond); |
| 2258 | pthread_mutex_destroy(&accept_mtx); |
| 2259 | #endif |
| 2260 | return (0); |
| 2261 | } |
| 2262 |
| 2263 | int |
| 2264 | userspace_finish(void) |
| 2265 | { |
| 2266 | return (usrsctp_finish()); |
| 2267 | } |
| 2268 |
| 2269 | /* needed from sctp_usrreq.c */ |
| 2270 | int |
| 2271 | sctp_setopt(struct socket *so, int optname, void *optval, size_t optsize, void *p); |
| 2272 |
| 2273 | int |
| 2274 | usrsctp_setsockopt(struct socket *so, int level, int option_name, |
| 2275 | const void *option_value, socklen_t option_len) |
| 2276 | { |
| 2277 | if (so == NULL) { |
| 2278 | errno = EBADF; |
| 2279 | return (-1); |
| 2280 | } |
| 2281 | switch (level) { |
| 2282 | case SOL_SOCKET: |
| 2283 | { |
| 2284 | switch (option_name) { |
| 2285 | case SO_RCVBUF: |
| 2286 | if (option_len < (socklen_t)sizeof(int)) { |
| 2287 | errno = EINVAL; |
| 2288 | return (-1); |
| 2289 | } else { |
| 2290 | int *buf_size; |
| 2291 |
| 2292 | buf_size = (int *)option_value; |
| 2293 | if (*buf_size < 1) { |
| 2294 | errno = EINVAL; |
| 2295 | return (-1); |
| 2296 | } |
| 2297 | sbreserve(&so->so_rcv, (u_long)*buf_size, so); |
| 2298 | return (0); |
| 2299 | } |
| 2300 | break; |
| 2301 | case SO_SNDBUF: |
| 2302 | if (option_len < (socklen_t)sizeof(int)) { |
| 2303 | errno = EINVAL; |
| 2304 | return (-1); |
| 2305 | } else { |
| 2306 | int *buf_size; |
| 2307 |
| 2308 | buf_size = (int *)option_value; |
| 2309 | if (*buf_size < 1) { |
| 2310 | errno = EINVAL; |
| 2311 | return (-1); |
| 2312 | } |
| 2313 | sbreserve(&so->so_snd, (u_long)*buf_size, so); |
| 2314 | return (0); |
| 2315 | } |
| 2316 | break; |
| 2317 | case SO_LINGER: |
| 2318 | if (option_len < (socklen_t)sizeof(struct linger)) { |
| 2319 | errno = EINVAL; |
| 2320 | return (-1); |
| 2321 | } else { |
| 2322 | struct linger *l; |
| 2323 |
| 2324 | l = (struct linger *)option_value; |
| 2325 | so->so_linger = l->l_linger; |
| 2326 | if (l->l_onoff) { |
| 2327 | so->so_options |= SCTP_SO_LINGER; |
| 2328 | } else { |
| 2329 | so->so_options &= ~SCTP_SO_LINGER; |
| 2330 | } |
| 2331 | return (0); |
| 2332 | } |
| 2333 | default: |
| 2334 | errno = EINVAL; |
| 2335 | return (-1); |
| 2336 | } |
| 2337 | } |
| 2338 | case IPPROTO_SCTP: |
| 2339 | errno = sctp_setopt(so, option_name, (void *) option_value, (size_t)option_len, NULL); |
| 2340 | if (errno) { |
| 2341 | return (-1); |
| 2342 | } else { |
| 2343 | return (0); |
| 2344 | } |
| 2345 | default: |
| 2346 | errno = ENOPROTOOPT; |
| 2347 | return (-1); |
| 2348 | } |
| 2349 | } |
| 2350 |
| 2351 | int |
| 2352 | userspace_setsockopt(struct socket *so, int level, int option_name, |
| 2353 | const void *option_value, socklen_t option_len) |
| 2354 | { |
| 2355 | return (usrsctp_setsockopt(so, level, option_name, option_value, option_len)); |
| 2356 | } |
| 2357 |
| 2358 | /* needed from sctp_usrreq.c */ |
| 2359 | int |
| 2360 | sctp_getopt(struct socket *so, int optname, void *optval, size_t *optsize, |
| 2361 | void *p); |
| 2362 |
| 2363 | int |
| 2364 | usrsctp_getsockopt(struct socket *so, int level, int option_name, |
| 2365 | void *option_value, socklen_t *option_len) |
| 2366 | { |
| 2367 | if (so == NULL) { |
| 2368 | errno = EBADF; |
| 2369 | return (-1); |
| 2370 | } |
| 2371 | if (option_len == NULL) { |
| 2372 | errno = EFAULT; |
| 2373 | return (-1); |
| 2374 | } |
| 2375 | switch (level) { |
| 2376 | case SOL_SOCKET: |
| 2377 | switch (option_name) { |
| 2378 | case SO_RCVBUF: |
| 2379 | if (*option_len < (socklen_t)sizeof(int)) { |
| 2380 | errno = EINVAL; |
| 2381 | return (-1); |
| 2382 | } else { |
| 2383 | int *buf_size; |
| 2384 |
| 2385 | buf_size = (int *)option_value; |
| 2386 | *buf_size = so->so_rcv.sb_hiwat;; |
| 2387 | *option_len = (socklen_t)sizeof(int); |
| 2388 | return (0); |
| 2389 | } |
| 2390 | break; |
| 2391 | case SO_SNDBUF: |
| 2392 | if (*option_len < (socklen_t)sizeof(int)) { |
| 2393 | errno = EINVAL; |
| 2394 | return (-1); |
| 2395 | } else { |
| 2396 | int *buf_size; |
| 2397 |
| 2398 | buf_size = (int *)option_value; |
| 2399 | *buf_size = so->so_snd.sb_hiwat; |
| 2400 | *option_len = (socklen_t)sizeof(int); |
| 2401 | return (0); |
| 2402 | } |
| 2403 | break; |
| 2404 | case SO_LINGER: |
| 2405 | if (*option_len < (socklen_t)sizeof(struct linger)) { |
| 2406 | errno = EINVAL; |
| 2407 | return (-1); |
| 2408 | } else { |
| 2409 | struct linger *l; |
| 2410 |
| 2411 | l = (struct linger *)option_value; |
| 2412 | l->l_linger = so->so_linger; |
| 2413 | if (so->so_options & SCTP_SO_LINGER) { |
| 2414 | l->l_onoff = 1; |
| 2415 | } else { |
| 2416 | l->l_onoff = 0; |
| 2417 | } |
| 2418 | *option_len = (socklen_t)sizeof(struct linger); |
| 2419 | return (0); |
| 2420 | } |
| 2421 | break; |
| 2422 | case SO_ERROR: |
| 2423 | if (*option_len < (socklen_t)sizeof(int)) { |
| 2424 | errno = EINVAL; |
| 2425 | return (-1); |
| 2426 | } else { |
| 2427 | int *intval; |
| 2428 |
| 2429 | intval = (int *)option_value; |
| 2430 | *intval = so->so_error; |
| 2431 | *option_len = (socklen_t)sizeof(int); |
| 2432 | return (0); |
| 2433 | } |
| 2434 | break; |
| 2435 | default: |
| 2436 | errno = EINVAL; |
| 2437 | return (-1); |
| 2438 | } |
| 2439 | case IPPROTO_SCTP: |
| 2440 | { |
| 2441 | size_t len; |
| 2442 |
| 2443 | len = (size_t)*option_len; |
| 2444 | errno = sctp_getopt(so, option_name, option_value, &len, NULL); |
| 2445 | *option_len = (socklen_t)len; |
| 2446 | if (errno) { |
| 2447 | return (-1); |
| 2448 | } else { |
| 2449 | return (0); |
| 2450 | } |
| 2451 | } |
| 2452 | default: |
| 2453 | errno = ENOPROTOOPT; |
| 2454 | return (-1); |
| 2455 | } |
| 2456 | } |
| 2457 |
| 2458 | int |
| 2459 | userspace_getsockopt(struct socket *so, int level, int option_name, |
| 2460 | void *option_value, socklen_t *option_len) |
| 2461 | { |
| 2462 | return (usrsctp_getsockopt(so, level, option_name, option_value, option_len)); |
| 2463 | } |
| 2464 |
| 2465 | int |
| 2466 | usrsctp_opt_info(struct socket *so, sctp_assoc_t id, int opt, void *arg, socklen_t *size) |
| 2467 | { |
| 2468 | if (arg == NULL) { |
| 2469 | errno = EINVAL; |
| 2470 | return (-1); |
| 2471 | } |
| 2472 | if ((id == SCTP_CURRENT_ASSOC) || |
| 2473 | (id == SCTP_ALL_ASSOC)) { |
| 2474 | errno = EINVAL; |
| 2475 | return (-1); |
| 2476 | } |
| 2477 | switch (opt) { |
| 2478 | case SCTP_RTOINFO: |
| 2479 | ((struct sctp_rtoinfo *)arg)->srto_assoc_id = id; |
| 2480 | break; |
| 2481 | case SCTP_ASSOCINFO: |
| 2482 | ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; |
| 2483 | break; |
| 2484 | case SCTP_DEFAULT_SEND_PARAM: |
| 2485 | ((struct sctp_assocparams *)arg)->sasoc_assoc_id = id; |
| 2486 | break; |
| 2487 | case SCTP_PRIMARY_ADDR: |
| 2488 | ((struct sctp_setprim *)arg)->ssp_assoc_id = id; |
| 2489 | break; |
| 2490 | case SCTP_PEER_ADDR_PARAMS: |
| 2491 | ((struct sctp_paddrparams *)arg)->spp_assoc_id = id; |
| 2492 | break; |
| 2493 | case SCTP_MAXSEG: |
| 2494 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2495 | break; |
| 2496 | case SCTP_AUTH_KEY: |
| 2497 | ((struct sctp_authkey *)arg)->sca_assoc_id = id; |
| 2498 | break; |
| 2499 | case SCTP_AUTH_ACTIVE_KEY: |
| 2500 | ((struct sctp_authkeyid *)arg)->scact_assoc_id = id; |
| 2501 | break; |
| 2502 | case SCTP_DELAYED_SACK: |
| 2503 | ((struct sctp_sack_info *)arg)->sack_assoc_id = id; |
| 2504 | break; |
| 2505 | case SCTP_CONTEXT: |
| 2506 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2507 | break; |
| 2508 | case SCTP_STATUS: |
| 2509 | ((struct sctp_status *)arg)->sstat_assoc_id = id; |
| 2510 | break; |
| 2511 | case SCTP_GET_PEER_ADDR_INFO: |
| 2512 | ((struct sctp_paddrinfo *)arg)->spinfo_assoc_id = id; |
| 2513 | break; |
| 2514 | case SCTP_PEER_AUTH_CHUNKS: |
| 2515 | ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; |
| 2516 | break; |
| 2517 | case SCTP_LOCAL_AUTH_CHUNKS: |
| 2518 | ((struct sctp_authchunks *)arg)->gauth_assoc_id = id; |
| 2519 | break; |
| 2520 | case SCTP_TIMEOUTS: |
| 2521 | ((struct sctp_timeouts *)arg)->stimo_assoc_id = id; |
| 2522 | break; |
| 2523 | case SCTP_EVENT: |
| 2524 | ((struct sctp_event *)arg)->se_assoc_id = id; |
| 2525 | break; |
| 2526 | case SCTP_DEFAULT_SNDINFO: |
| 2527 | ((struct sctp_sndinfo *)arg)->snd_assoc_id = id; |
| 2528 | break; |
| 2529 | case SCTP_DEFAULT_PRINFO: |
| 2530 | ((struct sctp_default_prinfo *)arg)->pr_assoc_id = id; |
| 2531 | break; |
| 2532 | case SCTP_PEER_ADDR_THLDS: |
| 2533 | ((struct sctp_paddrthlds *)arg)->spt_assoc_id = id; |
| 2534 | break; |
| 2535 | case SCTP_REMOTE_UDP_ENCAPS_PORT: |
| 2536 | ((struct sctp_udpencaps *)arg)->sue_assoc_id = id; |
| 2537 | break; |
| 2538 | case SCTP_ECN_SUPPORTED: |
| 2539 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2540 | break; |
| 2541 | case SCTP_PR_SUPPORTED: |
| 2542 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2543 | break; |
| 2544 | case SCTP_AUTH_SUPPORTED: |
| 2545 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2546 | break; |
| 2547 | case SCTP_ASCONF_SUPPORTED: |
| 2548 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2549 | break; |
| 2550 | case SCTP_RECONFIG_SUPPORTED: |
| 2551 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2552 | break; |
| 2553 | case SCTP_NRSACK_SUPPORTED: |
| 2554 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2555 | break; |
| 2556 | case SCTP_PKTDROP_SUPPORTED: |
| 2557 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2558 | break; |
| 2559 | case SCTP_MAX_BURST: |
| 2560 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2561 | break; |
| 2562 | case SCTP_ENABLE_STREAM_RESET: |
| 2563 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2564 | break; |
| 2565 | case SCTP_PR_STREAM_STATUS: |
| 2566 | ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id; |
| 2567 | break; |
| 2568 | case SCTP_PR_ASSOC_STATUS: |
| 2569 | ((struct sctp_prstatus *)arg)->sprstat_assoc_id = id; |
| 2570 | break; |
| 2571 | case SCTP_MAX_CWND: |
| 2572 | ((struct sctp_assoc_value *)arg)->assoc_id = id; |
| 2573 | break; |
| 2574 | default: |
| 2575 | break; |
| 2576 | } |
| 2577 | return (usrsctp_getsockopt(so, IPPROTO_SCTP, opt, arg, size)); |
| 2578 | } |
| 2579 |
| 2580 | int |
| 2581 | usrsctp_set_ulpinfo(struct socket *so, void *ulp_info) |
| 2582 | { |
| 2583 | return (register_ulp_info(so, ulp_info)); |
| 2584 | } |
| 2585 |
| 2586 | int |
| 2587 | usrsctp_bindx(struct socket *so, struct sockaddr *addrs, int addrcnt, int flags) |
| 2588 | { |
| 2589 | struct sctp_getaddresses *gaddrs; |
| 2590 | struct sockaddr *sa; |
| 2591 | #ifdef INET |
| 2592 | struct sockaddr_in *sin; |
| 2593 | #endif |
| 2594 | #ifdef INET6 |
| 2595 | struct sockaddr_in6 *sin6; |
| 2596 | #endif |
| 2597 | int i; |
| 2598 | size_t argsz; |
| 2599 | #if defined(INET) || defined(INET6) |
| 2600 | uint16_t sport = 0; |
| 2601 | #endif |
| 2602 |
| 2603 | /* validate the flags */ |
| 2604 | if ((flags != SCTP_BINDX_ADD_ADDR) && |
| 2605 | (flags != SCTP_BINDX_REM_ADDR)) { |
| 2606 | errno = EFAULT; |
| 2607 | return (-1); |
| 2608 | } |
| 2609 | /* validate the address count and list */ |
| 2610 | if ((addrcnt <= 0) || (addrs == NULL)) { |
| 2611 | errno = EINVAL; |
| 2612 | return (-1); |
| 2613 | } |
| 2614 | /* First pre-screen the addresses */ |
| 2615 | sa = addrs; |
| 2616 | for (i = 0; i < addrcnt; i++) { |
| 2617 | switch (sa->sa_family) { |
| 2618 | #ifdef INET |
| 2619 | case AF_INET: |
| 2620 | #ifdef HAVE_SA_LEN |
| 2621 | if (sa->sa_len != sizeof(struct sockaddr_in)) { |
| 2622 | errno = EINVAL; |
| 2623 | return (-1); |
| 2624 | } |
| 2625 | #endif |
| 2626 | sin = (struct sockaddr_in *)sa; |
| 2627 | if (sin->sin_port) { |
| 2628 | /* non-zero port, check or save */ |
| 2629 | if (sport) { |
| 2630 | /* Check against our port */ |
| 2631 | if (sport != sin->sin_port) { |
| 2632 | errno = EINVAL; |
| 2633 | return (-1); |
| 2634 | } |
| 2635 | } else { |
| 2636 | /* save off the port */ |
| 2637 | sport = sin->sin_port; |
| 2638 | } |
| 2639 | } |
| 2640 | #ifndef HAVE_SA_LEN |
| 2641 | sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in)); |
| 2642 | #endif |
| 2643 | break; |
| 2644 | #endif |
| 2645 | #ifdef INET6 |
| 2646 | case AF_INET6: |
| 2647 | #ifdef HAVE_SA_LEN |
| 2648 | if (sa->sa_len != sizeof(struct sockaddr_in6)) { |
| 2649 | errno = EINVAL; |
| 2650 | return (-1); |
| 2651 | } |
| 2652 | #endif |
| 2653 | sin6 = (struct sockaddr_in6 *)sa; |
| 2654 | if (sin6->sin6_port) { |
| 2655 | /* non-zero port, check or save */ |
| 2656 | if (sport) { |
| 2657 | /* Check against our port */ |
| 2658 | if (sport != sin6->sin6_port) { |
| 2659 | errno = EINVAL; |
| 2660 | return (-1); |
| 2661 | } |
| 2662 | } else { |
| 2663 | /* save off the port */ |
| 2664 | sport = sin6->sin6_port; |
| 2665 | } |
| 2666 | } |
| 2667 | #ifndef HAVE_SA_LEN |
| 2668 | sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6)); |
| 2669 | #endif |
| 2670 | break; |
| 2671 | #endif |
| 2672 | default: |
| 2673 | /* Invalid address family specified. */ |
| 2674 | errno = EAFNOSUPPORT; |
| 2675 | return (-1); |
| 2676 | } |
| 2677 | #ifdef HAVE_SA_LEN |
| 2678 | sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); |
| 2679 | #endif |
| 2680 | } |
| 2681 | argsz = sizeof(struct sctp_getaddresses) + |
| 2682 | sizeof(struct sockaddr_storage); |
| 2683 | if ((gaddrs = (struct sctp_getaddresses *)malloc(argsz)) == NULL) { |
| 2684 | errno = ENOMEM; |
| 2685 | return (-1); |
| 2686 | } |
| 2687 | sa = addrs; |
| 2688 | for (i = 0; i < addrcnt; i++) { |
| 2689 | #ifndef HAVE_SA_LEN |
| 2690 | size_t sa_len; |
| 2691 | #endif |
| 2692 | memset(gaddrs, 0, argsz); |
| 2693 | gaddrs->sget_assoc_id = 0; |
| 2694 | #ifdef HAVE_SA_LEN |
| 2695 | memcpy(gaddrs->addr, sa, sa->sa_len); |
| 2696 | #if defined(INET) || defined(INET6) |
| 2697 | if ((i == 0) && (sport != 0)) { |
| 2698 | switch (gaddrs->addr->sa_family) { |
| 2699 | #ifdef INET |
| 2700 | case AF_INET: |
| 2701 | sin = (struct sockaddr_in *)gaddrs->addr; |
| 2702 | sin->sin_port = sport; |
| 2703 | break; |
| 2704 | #endif |
| 2705 | #ifdef INET6 |
| 2706 | case AF_INET6: |
| 2707 | sin6 = (struct sockaddr_in6 *)gaddrs->addr; |
| 2708 | sin6->sin6_port = sport; |
| 2709 | break; |
| 2710 | #endif |
| 2711 | } |
| 2712 | } |
| 2713 | #endif |
| 2714 | if (usrsctp_setsockopt(so, IPPROTO_SCTP, flags, gaddrs, (socklen_t)argsz) != 0) { |
| 2715 | free(gaddrs); |
| 2716 | return (-1); |
| 2717 | } |
| 2718 | sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); |
| 2719 | #else |
| 2720 | switch (sa->sa_family) { |
| 2721 | #ifdef INET |
| 2722 | case AF_INET: |
| 2723 | sa_len = sizeof(struct sockaddr_in); |
| 2724 | break; |
| 2725 | #endif |
| 2726 | #ifdef INET6 |
| 2727 | case AF_INET6: |
| 2728 | sa_len = sizeof(struct sockaddr_in6); |
| 2729 | break; |
| 2730 | #endif |
| 2731 | default: |
| 2732 | sa_len = 0; |
| 2733 | break; |
| 2734 | } |
| 2735 | memcpy(gaddrs->addr, sa, sa_len); |
| 2736 | /* |
| 2737 | * Now, if there was a port mentioned, assure that the |
| 2738 | * first address has that port to make sure it fails or |
| 2739 | * succeeds correctly. |
| 2740 | */ |
| 2741 | #if defined(INET) || defined(INET6) |
| 2742 | if ((i == 0) && (sport != 0)) { |
| 2743 | switch (gaddrs->addr->sa_family) { |
| 2744 | #ifdef INET |
| 2745 | case AF_INET: |
| 2746 | sin = (struct sockaddr_in *)gaddrs->addr; |
| 2747 | sin->sin_port = sport; |
| 2748 | break; |
| 2749 | #endif |
| 2750 | #ifdef INET6 |
| 2751 | case AF_INET6: |
| 2752 | sin6 = (struct sockaddr_in6 *)gaddrs->addr; |
| 2753 | sin6->sin6_port = sport; |
| 2754 | break; |
| 2755 | #endif |
| 2756 | } |
| 2757 | } |
| 2758 | #endif |
| 2759 | if (usrsctp_setsockopt(so, IPPROTO_SCTP, flags, gaddrs, (socklen_t)argsz) != 0) { |
| 2760 | free(gaddrs); |
| 2761 | return (-1); |
| 2762 | } |
| 2763 | sa = (struct sockaddr *)((caddr_t)sa + sa_len); |
| 2764 | #endif |
| 2765 | } |
| 2766 | free(gaddrs); |
| 2767 | return (0); |
| 2768 | } |
| 2769 |
| 2770 | int |
| 2771 | usrsctp_connectx(struct socket *so, |
| 2772 | const struct sockaddr *addrs, int addrcnt, |
| 2773 | sctp_assoc_t *id) |
| 2774 | { |
| 2775 | #if defined(INET) || defined(INET6) |
| 2776 | char buf[SCTP_STACK_BUF_SIZE]; |
| 2777 | int i, ret, cnt, *aa; |
| 2778 | char *cpto; |
| 2779 | const struct sockaddr *at; |
| 2780 | sctp_assoc_t *p_id; |
| 2781 | size_t len = sizeof(int); |
| 2782 |
| 2783 | /* validate the address count and list */ |
| 2784 | if ((addrs == NULL) || (addrcnt <= 0)) { |
| 2785 | errno = EINVAL; |
| 2786 | return (-1); |
| 2787 | } |
| 2788 | at = addrs; |
| 2789 | cnt = 0; |
| 2790 | cpto = ((caddr_t)buf + sizeof(int)); |
| 2791 | /* validate all the addresses and get the size */ |
| 2792 | for (i = 0; i < addrcnt; i++) { |
| 2793 | switch (at->sa_family) { |
| 2794 | #ifdef INET |
| 2795 | case AF_INET: |
| 2796 | #ifdef HAVE_SA_LEN |
| 2797 | if (at->sa_len != sizeof(struct sockaddr_in)) { |
| 2798 | errno = EINVAL; |
| 2799 | return (-1); |
| 2800 | } |
| 2801 | #endif |
| 2802 | len += sizeof(struct sockaddr_in); |
| 2803 | if (len > SCTP_STACK_BUF_SIZE) { |
| 2804 | errno = ENOMEM; |
| 2805 | return (-1); |
| 2806 | } |
| 2807 | memcpy(cpto, at, sizeof(struct sockaddr_in)); |
| 2808 | cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); |
| 2809 | at = (struct sockaddr *)((caddr_t)at + sizeof(struct sockaddr_in)); |
| 2810 | break; |
| 2811 | #endif |
| 2812 | #ifdef INET6 |
| 2813 | case AF_INET6: |
| 2814 | #ifdef HAVE_SA_LEN |
| 2815 | if (at->sa_len != sizeof(struct sockaddr_in6)) { |
| 2816 | errno = EINVAL; |
| 2817 | return (-1); |
| 2818 | } |
| 2819 | #endif |
| 2820 | #ifdef INET |
| 2821 | if (IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6 *)at)->sin6_addr)) { |
| 2822 | len += sizeof(struct sockaddr_in); |
| 2823 | if (len > SCTP_STACK_BUF_SIZE) { |
| 2824 | errno = ENOMEM; |
| 2825 | return (-1); |
| 2826 | } |
| 2827 | in6_sin6_2_sin((struct sockaddr_in *)cpto, (struct sockaddr_in6 *)at); |
| 2828 | cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in)); |
| 2829 | } else { |
| 2830 | len += sizeof(struct sockaddr_in6); |
| 2831 | if (len > SCTP_STACK_BUF_SIZE) { |
| 2832 | errno = ENOMEM; |
| 2833 | return (-1); |
| 2834 | } |
| 2835 | memcpy(cpto, at, sizeof(struct sockaddr_in6)); |
| 2836 | cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6)); |
| 2837 | } |
| 2838 | #else |
| 2839 | len += sizeof(struct sockaddr_in6); |
| 2840 | if (len > SCTP_STACK_BUF_SIZE) { |
| 2841 | errno = ENOMEM; |
| 2842 | return (-1); |
| 2843 | } |
| 2844 | memcpy(cpto, at, sizeof(struct sockaddr_in6)); |
| 2845 | cpto = ((caddr_t)cpto + sizeof(struct sockaddr_in6)); |
| 2846 | #endif |
| 2847 | at = (struct sockaddr *)((caddr_t)at + sizeof(struct sockaddr_in6)); |
| 2848 | break; |
| 2849 | #endif |
| 2850 | default: |
| 2851 | errno = EINVAL; |
| 2852 | return (-1); |
| 2853 | } |
| 2854 | cnt++; |
| 2855 | } |
| 2856 | aa = (int *)buf; |
| 2857 | *aa = cnt; |
| 2858 | ret = usrsctp_setsockopt(so, IPPROTO_SCTP, SCTP_CONNECT_X, (void *)buf, (socklen_t)len); |
| 2859 | if ((ret == 0) && id) { |
| 2860 | p_id = (sctp_assoc_t *)buf; |
| 2861 | *id = *p_id; |
| 2862 | } |
| 2863 | return (ret); |
| 2864 | #else |
| 2865 | errno = EINVAL; |
| 2866 | return (-1); |
| 2867 | #endif |
| 2868 | } |
| 2869 |
| 2870 | int |
| 2871 | usrsctp_getpaddrs(struct socket *so, sctp_assoc_t id, struct sockaddr **raddrs) |
| 2872 | { |
| 2873 | struct sctp_getaddresses *addrs; |
| 2874 | struct sockaddr *sa; |
| 2875 | sctp_assoc_t asoc; |
| 2876 | caddr_t lim; |
| 2877 | socklen_t opt_len; |
| 2878 | int cnt; |
| 2879 |
| 2880 | if (raddrs == NULL) { |
| 2881 | errno = EFAULT; |
| 2882 | return (-1); |
| 2883 | } |
| 2884 | asoc = id; |
| 2885 | opt_len = (socklen_t)sizeof(sctp_assoc_t); |
| 2886 | if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_REMOTE_ADDR_SIZE, &asoc, &opt_len) != 0) { |
| 2887 | return (-1); |
| 2888 | } |
| 2889 | /* size required is returned in 'asoc' */ |
| 2890 | opt_len = (socklen_t)((size_t)asoc + sizeof(struct sctp_getaddresses)); |
| 2891 | addrs = calloc(1, (size_t)opt_len); |
| 2892 | if (addrs == NULL) { |
| 2893 | errno = ENOMEM; |
| 2894 | return (-1); |
| 2895 | } |
| 2896 | addrs->sget_assoc_id = id; |
| 2897 | /* Now lets get the array of addresses */ |
| 2898 | if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_PEER_ADDRESSES, addrs, &opt_len) != 0) { |
| 2899 | free(addrs); |
| 2900 | return (-1); |
| 2901 | } |
| 2902 | *raddrs = (struct sockaddr *)&addrs->addr[0]; |
| 2903 | cnt = 0; |
| 2904 | sa = (struct sockaddr *)&addrs->addr[0]; |
| 2905 | lim = (caddr_t)addrs + opt_len; |
| 2906 | #ifdef HAVE_SA_LEN |
| 2907 | while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { |
| 2908 | sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); |
| 2909 | #else |
| 2910 | while ((caddr_t)sa < lim) { |
| 2911 | switch (sa->sa_family) { |
| 2912 | #ifdef INET |
| 2913 | case AF_INET: |
| 2914 | sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in)); |
| 2915 | break; |
| 2916 | #endif |
| 2917 | #ifdef INET6 |
| 2918 | case AF_INET6: |
| 2919 | sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6)); |
| 2920 | break; |
| 2921 | #endif |
| 2922 | case AF_CONN: |
| 2923 | sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_conn)); |
| 2924 | break; |
| 2925 | default: |
| 2926 | return (cnt); |
| 2927 | break; |
| 2928 | } |
| 2929 | #endif |
| 2930 | cnt++; |
| 2931 | } |
| 2932 | return (cnt); |
| 2933 | } |
| 2934 |
| 2935 | void |
| 2936 | usrsctp_freepaddrs(struct sockaddr *addrs) |
| 2937 | { |
| 2938 | /* Take away the hidden association id */ |
| 2939 | void *fr_addr; |
| 2940 |
| 2941 | fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); |
| 2942 | /* Now free it */ |
| 2943 | free(fr_addr); |
| 2944 | } |
| 2945 |
| 2946 | int |
| 2947 | usrsctp_getladdrs(struct socket *so, sctp_assoc_t id, struct sockaddr **raddrs) |
| 2948 | { |
| 2949 | struct sctp_getaddresses *addrs; |
| 2950 | caddr_t lim; |
| 2951 | struct sockaddr *sa; |
| 2952 | size_t size_of_addresses; |
| 2953 | socklen_t opt_len; |
| 2954 | int cnt; |
| 2955 |
| 2956 | if (raddrs == NULL) { |
| 2957 | errno = EFAULT; |
| 2958 | return (-1); |
| 2959 | } |
| 2960 | size_of_addresses = 0; |
| 2961 | opt_len = (socklen_t)sizeof(int); |
| 2962 | if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDR_SIZE, &size_of_addresses, &opt_len) != 0) { |
| 2963 | errno = ENOMEM; |
| 2964 | return (-1); |
| 2965 | } |
| 2966 | if (size_of_addresses == 0) { |
| 2967 | errno = ENOTCONN; |
| 2968 | return (-1); |
| 2969 | } |
| 2970 | opt_len = (socklen_t)(size_of_addresses + |
| 2971 | sizeof(struct sockaddr_storage) + |
| 2972 | sizeof(struct sctp_getaddresses)); |
| 2973 | addrs = calloc(1, (size_t)opt_len); |
| 2974 | if (addrs == NULL) { |
| 2975 | errno = ENOMEM; |
| 2976 | return (-1); |
| 2977 | } |
| 2978 | addrs->sget_assoc_id = id; |
| 2979 | /* Now lets get the array of addresses */ |
| 2980 | if (usrsctp_getsockopt(so, IPPROTO_SCTP, SCTP_GET_LOCAL_ADDRESSES, addrs, &opt_len) != 0) { |
| 2981 | free(addrs); |
| 2982 | errno = ENOMEM; |
| 2983 | return (-1); |
| 2984 | } |
| 2985 | *raddrs = (struct sockaddr *)&addrs->addr[0]; |
| 2986 | cnt = 0; |
| 2987 | sa = (struct sockaddr *)&addrs->addr[0]; |
| 2988 | lim = (caddr_t)addrs + opt_len; |
| 2989 | #ifdef HAVE_SA_LEN |
| 2990 | while (((caddr_t)sa < lim) && (sa->sa_len > 0)) { |
| 2991 | sa = (struct sockaddr *)((caddr_t)sa + sa->sa_len); |
| 2992 | #else |
| 2993 | while ((caddr_t)sa < lim) { |
| 2994 | switch (sa->sa_family) { |
| 2995 | #ifdef INET |
| 2996 | case AF_INET: |
| 2997 | sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in)); |
| 2998 | break; |
| 2999 | #endif |
| 3000 | #ifdef INET6 |
| 3001 | case AF_INET6: |
| 3002 | sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_in6)); |
| 3003 | break; |
| 3004 | #endif |
| 3005 | case AF_CONN: |
| 3006 | sa = (struct sockaddr *)((caddr_t)sa + sizeof(struct sockaddr_conn)); |
| 3007 | break; |
| 3008 | default: |
| 3009 | return (cnt); |
| 3010 | break; |
| 3011 | } |
| 3012 | #endif |
| 3013 | cnt++; |
| 3014 | } |
| 3015 | return (cnt); |
| 3016 | } |
| 3017 |
| 3018 | void |
| 3019 | usrsctp_freeladdrs(struct sockaddr *addrs) |
| 3020 | { |
| 3021 | /* Take away the hidden association id */ |
| 3022 | void *fr_addr; |
| 3023 |
| 3024 | fr_addr = (void *)((caddr_t)addrs - sizeof(sctp_assoc_t)); |
| 3025 | /* Now free it */ |
| 3026 | free(fr_addr); |
| 3027 | } |
| 3028 |
| 3029 | #ifdef INET |
| 3030 | void |
| 3031 | sctp_userspace_ip_output(int *result, struct mbuf *o_pak, |
| 3032 | sctp_route_t *ro, void *stcb, |
| 3033 | uint32_t vrf_id) |
| 3034 | { |
| 3035 | struct mbuf *m; |
| 3036 | struct mbuf *m_orig; |
| 3037 | int iovcnt; |
| 3038 | int send_len; |
| 3039 | int len; |
| 3040 | int send_count; |
| 3041 | struct ip *ip; |
| 3042 | struct udphdr *udp; |
| 3043 | #if !defined (__Userspace_os_Windows) |
| 3044 | int res; |
| 3045 | #endif |
| 3046 | struct sockaddr_in dst; |
| 3047 | #if defined (__Userspace_os_Windows) |
| 3048 | WSAMSG win_msg_hdr; |
| 3049 | DWORD win_sent_len; |
| 3050 | WSABUF send_iovec[MAXLEN_MBUF_CHAIN]; |
| 3051 | WSABUF winbuf; |
| 3052 | #else |
| 3053 | struct iovec send_iovec[MAXLEN_MBUF_CHAIN]; |
| 3054 | struct msghdr msg_hdr; |
| 3055 | #endif |
| 3056 | int use_udp_tunneling; |
| 3057 |
| 3058 | *result = 0; |
| 3059 |
| 3060 | m = SCTP_HEADER_TO_CHAIN(o_pak); |
| 3061 | m_orig = m; |
| 3062 |
| 3063 | len = sizeof(struct ip); |
| 3064 | if (SCTP_BUF_LEN(m) < len) { |
| 3065 | if ((m = m_pullup(m, len)) == 0) { |
| 3066 | SCTP_PRINTF("Can not get the IP header in the first mbuf.\n"); |
| 3067 | return; |
| 3068 | } |
| 3069 | } |
| 3070 | ip = mtod(m, struct ip *); |
| 3071 | use_udp_tunneling = (ip->ip_p == IPPROTO_UDP); |
| 3072 |
| 3073 | if (use_udp_tunneling) { |
| 3074 | len = sizeof(struct ip) + sizeof(struct udphdr); |
| 3075 | if (SCTP_BUF_LEN(m) < len) { |
| 3076 | if ((m = m_pullup(m, len)) == 0) { |
| 3077 | SCTP_PRINTF("Can not get the UDP/IP header in the first mbuf.\n"); |
| 3078 | return; |
| 3079 | } |
| 3080 | ip = mtod(m, struct ip *); |
| 3081 | } |
| 3082 | udp = (struct udphdr *)(ip + 1); |
| 3083 | } else { |
| 3084 | udp = NULL; |
| 3085 | } |
| 3086 |
| 3087 | if (!use_udp_tunneling) { |
| 3088 | if (ip->ip_src.s_addr == INADDR_ANY) { |
| 3089 | /* TODO get addr of outgoing interface */ |
| 3090 | SCTP_PRINTF("Why did the SCTP implementation did not choose a source address?\n"); |
| 3091 | } |
| 3092 | /* TODO need to worry about ro->ro_dst as in ip_output? */ |
| 3093 | #if defined(__Userspace_os_Linux) || defined (__Userspace_os_Windows) || (defined(__Userspace_os_FreeBSD) && (__FreeBSD_version >= 1100030)) |
| 3094 | /* need to put certain fields into network order for Linux */ |
| 3095 | ip->ip_len = htons(ip->ip_len); |
| 3096 | #endif |
| 3097 | } |
| 3098 |
| 3099 | memset((void *)&dst, 0, sizeof(struct sockaddr_in)); |
| 3100 | dst.sin_family = AF_INET; |
| 3101 | dst.sin_addr.s_addr = ip->ip_dst.s_addr; |
| 3102 | #ifdef HAVE_SIN_LEN |
| 3103 | dst.sin_len = sizeof(struct sockaddr_in); |
| 3104 | #endif |
| 3105 | if (use_udp_tunneling) { |
| 3106 | dst.sin_port = udp->uh_dport; |
| 3107 | } else { |
| 3108 | dst.sin_port = 0; |
| 3109 | } |
| 3110 |
| 3111 | /* tweak the mbuf chain */ |
| 3112 | if (use_udp_tunneling) { |
| 3113 | m_adj(m, sizeof(struct ip) + sizeof(struct udphdr)); |
| 3114 | } |
| 3115 |
| 3116 | send_len = SCTP_HEADER_LEN(m); /* length of entire packet */ |
| 3117 | send_count = 0; |
| 3118 | for (iovcnt = 0; m != NULL && iovcnt < MAXLEN_MBUF_CHAIN; m = m->m_next, iovcnt++) { |
| 3119 | #if !defined (__Userspace_os_Windows) |
| 3120 | send_iovec[iovcnt].iov_base = (caddr_t)m->m_data; |
| 3121 | send_iovec[iovcnt].iov_len = SCTP_BUF_LEN(m); |
| 3122 | send_count += send_iovec[iovcnt].iov_len; |
| 3123 | #else |
| 3124 | send_iovec[iovcnt].buf = (caddr_t)m->m_data; |
| 3125 | send_iovec[iovcnt].len = SCTP_BUF_LEN(m); |
| 3126 | send_count += send_iovec[iovcnt].len; |
| 3127 | #endif |
| 3128 | } |
| 3129 |
| 3130 | if (m != NULL) { |
| 3131 | SCTP_PRINTF("mbuf chain couldn't be copied completely\n"); |
| 3132 | goto free_mbuf; |
| 3133 | } |
| 3134 |
| 3135 | #if !defined (__Userspace_os_Windows) |
| 3136 | msg_hdr.msg_name = (struct sockaddr *) &dst; |
| 3137 | msg_hdr.msg_namelen = sizeof(struct sockaddr_in); |
| 3138 | msg_hdr.msg_iov = send_iovec; |
| 3139 | msg_hdr.msg_iovlen = iovcnt; |
| 3140 | msg_hdr.msg_control = NULL; |
| 3141 | msg_hdr.msg_controllen = 0; |
| 3142 | msg_hdr.msg_flags = 0; |
| 3143 |
| 3144 | if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp) != -1)) { |
| 3145 | if ((res = sendmsg(SCTP_BASE_VAR(userspace_rawsctp), &msg_hdr, MSG_DONTWAIT)) != send_len) { |
| 3146 | *result = errno; |
| 3147 | } |
| 3148 | } |
| 3149 | if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp) != -1)) { |
| 3150 | if ((res = sendmsg(SCTP_BASE_VAR(userspace_udpsctp), &msg_hdr, MSG_DONTWAIT)) != send_len) { |
| 3151 | *result = errno; |
| 3152 | } |
| 3153 | } |
| 3154 | #else |
| 3155 | win_msg_hdr.name = (struct sockaddr *) &dst; |
| 3156 | win_msg_hdr.namelen = sizeof(struct sockaddr_in); |
| 3157 | win_msg_hdr.lpBuffers = (LPWSABUF)send_iovec; |
| 3158 | win_msg_hdr.dwBufferCount = iovcnt; |
| 3159 | winbuf.len = 0; |
| 3160 | winbuf.buf = NULL; |
| 3161 | win_msg_hdr.Control = winbuf; |
| 3162 | win_msg_hdr.dwFlags = 0; |
| 3163 |
| 3164 | if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp) != -1)) { |
| 3165 | 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) { |
| 3166 | *result = WSAGetLastError(); |
| 3167 | } else if ((int)win_sent_len != send_len) { |
| 3168 | *result = WSAGetLastError(); |
| 3169 | } |
| 3170 | } |
| 3171 | if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp) != -1)) { |
| 3172 | 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) { |
| 3173 | *result = WSAGetLastError(); |
| 3174 | } else if ((int)win_sent_len != send_len) { |
| 3175 | *result = WSAGetLastError(); |
| 3176 | } |
| 3177 | } |
| 3178 | #endif |
| 3179 | free_mbuf: |
| 3180 | sctp_m_freem(m_orig); |
| 3181 | } |
| 3182 | #endif |
| 3183 |
| 3184 | #if defined (INET6) |
| 3185 | void sctp_userspace_ip6_output(int *result, struct mbuf *o_pak, |
| 3186 | struct route_in6 *ro, void *stcb, |
| 3187 | uint32_t vrf_id) |
| 3188 | { |
| 3189 | struct mbuf *m; |
| 3190 | struct mbuf *m_orig; |
| 3191 | int iovcnt; |
| 3192 | int send_len; |
| 3193 | int len; |
| 3194 | int send_count; |
| 3195 | struct ip6_hdr *ip6; |
| 3196 | struct udphdr *udp; |
| 3197 | #if !defined (__Userspace_os_Windows) |
| 3198 | int res; |
| 3199 | #endif |
| 3200 | struct sockaddr_in6 dst; |
| 3201 | #if defined (__Userspace_os_Windows) |
| 3202 | WSAMSG win_msg_hdr; |
| 3203 | DWORD win_sent_len; |
| 3204 | WSABUF send_iovec[MAXLEN_MBUF_CHAIN]; |
| 3205 | WSABUF winbuf; |
| 3206 | #else |
| 3207 | struct iovec send_iovec[MAXLEN_MBUF_CHAIN]; |
| 3208 | struct msghdr msg_hdr; |
| 3209 | #endif |
| 3210 | int use_udp_tunneling; |
| 3211 |
| 3212 | *result = 0; |
| 3213 |
| 3214 | m = SCTP_HEADER_TO_CHAIN(o_pak); |
| 3215 | m_orig = m; |
| 3216 |
| 3217 | len = sizeof(struct ip6_hdr); |
| 3218 |
| 3219 | if (SCTP_BUF_LEN(m) < len) { |
| 3220 | if ((m = m_pullup(m, len)) == 0) { |
| 3221 | SCTP_PRINTF("Can not get the IP header in the first mbuf.\n"); |
| 3222 | return; |
| 3223 | } |
| 3224 | } |
| 3225 |
| 3226 | ip6 = mtod(m, struct ip6_hdr *); |
| 3227 | use_udp_tunneling = (ip6->ip6_nxt == IPPROTO_UDP); |
| 3228 |
| 3229 | if (use_udp_tunneling) { |
| 3230 | len = sizeof(struct ip6_hdr) + sizeof(struct udphdr); |
| 3231 | if (SCTP_BUF_LEN(m) < len) { |
| 3232 | if ((m = m_pullup(m, len)) == 0) { |
| 3233 | SCTP_PRINTF("Can not get the UDP/IP header in the first mbuf.\n"); |
| 3234 | return; |
| 3235 | } |
| 3236 | ip6 = mtod(m, struct ip6_hdr *); |
| 3237 | } |
| 3238 | udp = (struct udphdr *)(ip6 + 1); |
| 3239 | } else { |
| 3240 | udp = NULL; |
| 3241 | } |
| 3242 |
| 3243 | if (!use_udp_tunneling) { |
| 3244 | if (ip6->ip6_src.s6_addr == in6addr_any.s6_addr) { |
| 3245 | /* TODO get addr of outgoing interface */ |
| 3246 | SCTP_PRINTF("Why did the SCTP implementation did not choose a source address?\n"); |
| 3247 | } |
| 3248 | /* TODO need to worry about ro->ro_dst as in ip_output? */ |
| 3249 | } |
| 3250 |
| 3251 | memset((void *)&dst, 0, sizeof(struct sockaddr_in6)); |
| 3252 | dst.sin6_family = AF_INET6; |
| 3253 | dst.sin6_addr = ip6->ip6_dst; |
| 3254 | #ifdef HAVE_SIN6_LEN |
| 3255 | dst.sin6_len = sizeof(struct sockaddr_in6); |
| 3256 | #endif |
| 3257 |
| 3258 | if (use_udp_tunneling) { |
| 3259 | dst.sin6_port = udp->uh_dport; |
| 3260 | } else { |
| 3261 | dst.sin6_port = 0; |
| 3262 | } |
| 3263 |
| 3264 | /* tweak the mbuf chain */ |
| 3265 | if (use_udp_tunneling) { |
| 3266 | m_adj(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); |
| 3267 | } else { |
| 3268 | m_adj(m, sizeof(struct ip6_hdr)); |
| 3269 | } |
| 3270 |
| 3271 | send_len = SCTP_HEADER_LEN(m); /* length of entire packet */ |
| 3272 | send_count = 0; |
| 3273 | for (iovcnt = 0; m != NULL && iovcnt < MAXLEN_MBUF_CHAIN; m = m->m_next, iovcnt++) { |
| 3274 | #if !defined (__Userspace_os_Windows) |
| 3275 | send_iovec[iovcnt].iov_base = (caddr_t)m->m_data; |
| 3276 | send_iovec[iovcnt].iov_len = SCTP_BUF_LEN(m); |
| 3277 | send_count += send_iovec[iovcnt].iov_len; |
| 3278 | #else |
| 3279 | send_iovec[iovcnt].buf = (caddr_t)m->m_data; |
| 3280 | send_iovec[iovcnt].len = SCTP_BUF_LEN(m); |
| 3281 | send_count += send_iovec[iovcnt].len; |
| 3282 | #endif |
| 3283 | } |
| 3284 | if (m != NULL) { |
| 3285 | SCTP_PRINTF("mbuf chain couldn't be copied completely\n"); |
| 3286 | goto free_mbuf; |
| 3287 | } |
| 3288 |
| 3289 | #if !defined (__Userspace_os_Windows) |
| 3290 | msg_hdr.msg_name = (struct sockaddr *) &dst; |
| 3291 | msg_hdr.msg_namelen = sizeof(struct sockaddr_in6); |
| 3292 | msg_hdr.msg_iov = send_iovec; |
| 3293 | msg_hdr.msg_iovlen = iovcnt; |
| 3294 | msg_hdr.msg_control = NULL; |
| 3295 | msg_hdr.msg_controllen = 0; |
| 3296 | msg_hdr.msg_flags = 0; |
| 3297 |
| 3298 | if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp6) != -1)) { |
| 3299 | if ((res = sendmsg(SCTP_BASE_VAR(userspace_rawsctp6), &msg_hdr, MSG_DONTWAIT)) != send_len) { |
| 3300 | *result = errno; |
| 3301 | } |
| 3302 | } |
| 3303 | if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp6) != -1)) { |
| 3304 | if ((res = sendmsg(SCTP_BASE_VAR(userspace_udpsctp6), &msg_hdr, MSG_DONTWAIT)) != send_len) { |
| 3305 | *result = errno; |
| 3306 | } |
| 3307 | } |
| 3308 | #else |
| 3309 | win_msg_hdr.name = (struct sockaddr *) &dst; |
| 3310 | win_msg_hdr.namelen = sizeof(struct sockaddr_in6); |
| 3311 | win_msg_hdr.lpBuffers = (LPWSABUF)send_iovec; |
| 3312 | win_msg_hdr.dwBufferCount = iovcnt; |
| 3313 | winbuf.len = 0; |
| 3314 | winbuf.buf = NULL; |
| 3315 | win_msg_hdr.Control = winbuf; |
| 3316 | win_msg_hdr.dwFlags = 0; |
| 3317 |
| 3318 | if ((!use_udp_tunneling) && (SCTP_BASE_VAR(userspace_rawsctp6) != -1)) { |
| 3319 | 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) { |
| 3320 | *result = WSAGetLastError(); |
| 3321 | } else if ((int)win_sent_len != send_len) { |
| 3322 | *result = WSAGetLastError(); |
| 3323 | } |
| 3324 | } |
| 3325 | if ((use_udp_tunneling) && (SCTP_BASE_VAR(userspace_udpsctp6) != -1)) { |
| 3326 | 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) { |
| 3327 | *result = WSAGetLastError(); |
| 3328 | } else if ((int)win_sent_len != send_len) { |
| 3329 | *result = WSAGetLastError(); |
| 3330 | } |
| 3331 | } |
| 3332 | #endif |
| 3333 | free_mbuf: |
| 3334 | sctp_m_freem(m_orig); |
| 3335 | } |
| 3336 | #endif |
| 3337 |
| 3338 | void |
| 3339 | usrsctp_register_address(void *addr) |
| 3340 | { |
| 3341 | struct sockaddr_conn sconn; |
| 3342 |
| 3343 | memset(&sconn, 0, sizeof(struct sockaddr_conn)); |
| 3344 | sconn.sconn_family = AF_CONN; |
| 3345 | #ifdef HAVE_SCONN_LEN |
| 3346 | sconn.sconn_len = sizeof(struct sockaddr_conn); |
| 3347 | #endif |
| 3348 | sconn.sconn_port = 0; |
| 3349 | sconn.sconn_addr = addr; |
| 3350 | sctp_add_addr_to_vrf(SCTP_DEFAULT_VRFID, |
| 3351 | NULL, |
| 3352 | 0xffffffff, |
| 3353 | 0, |
| 3354 | "conn", |
| 3355 | NULL, |
| 3356 | (struct sockaddr *)&sconn, |
| 3357 | 0, |
| 3358 | 0); |
| 3359 | } |
| 3360 |
| 3361 | void |
| 3362 | usrsctp_deregister_address(void *addr) |
| 3363 | { |
| 3364 | struct sockaddr_conn sconn; |
| 3365 |
| 3366 | memset(&sconn, 0, sizeof(struct sockaddr_conn)); |
| 3367 | sconn.sconn_family = AF_CONN; |
| 3368 | #ifdef HAVE_SCONN_LEN |
| 3369 | sconn.sconn_len = sizeof(struct sockaddr_conn); |
| 3370 | #endif |
| 3371 | sconn.sconn_port = 0; |
| 3372 | sconn.sconn_addr = addr; |
| 3373 | sctp_del_addr_from_vrf(SCTP_DEFAULT_VRFID, |
| 3374 | (struct sockaddr *)&sconn, |
| 3375 | 0xffffffff, |
| 3376 | "conn"); |
| 3377 | } |
| 3378 |
| 3379 | #define PREAMBLE_FORMAT "\n%c %02d:%02d:%02d.%06ld " |
| 3380 | #define PREAMBLE_LENGTH 19 |
| 3381 | #define HEADER "0000 " |
| 3382 | #define TRAILER "# SCTP_PACKET\n" |
| 3383 |
| 3384 | char * |
| 3385 | usrsctp_dumppacket(const void *buf, size_t len, int outbound) |
| 3386 | { |
| 3387 | size_t i, pos; |
| 3388 | char *dump_buf, *packet; |
| 3389 | struct tm t; |
| 3390 | #ifdef _WIN32 |
| 3391 | struct timeb tb; |
| 3392 | #else |
| 3393 | struct timeval tv; |
| 3394 | time_t sec; |
| 3395 | #endif |
| 3396 |
| 3397 | if ((len == 0) || (buf == NULL)) { |
| 3398 | return (NULL); |
| 3399 | } |
| 3400 | if ((dump_buf = malloc(PREAMBLE_LENGTH + strlen(HEADER) + 3 * len + strlen(TRAILER) + 1)) == NULL) { |
| 3401 | return (NULL); |
| 3402 | } |
| 3403 | pos = 0; |
| 3404 | #ifdef _WIN32 |
| 3405 | ftime(&tb); |
| 3406 | localtime_s(&t, &tb.time); |
| 3407 | #if defined(__MINGW32__) |
| 3408 | snprintf(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_FORMAT, |
| 3409 | outbound ? 'O' : 'I', |
| 3410 | t.tm_hour, t.tm_min, t.tm_sec, (long)(1000 * tb.millitm)); |
| 3411 | #else |
| 3412 | _snprintf_s(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_LENGTH, PREAMBLE_FORMAT, |
| 3413 | outbound ? 'O' : 'I', |
| 3414 | t.tm_hour, t.tm_min, t.tm_sec, (long)(1000 * tb.millitm)); |
| 3415 | #endif |
| 3416 | #else |
| 3417 | gettimeofday(&tv, NULL); |
| 3418 | sec = (time_t)tv.tv_sec; |
| 3419 | localtime_r((const time_t *)&sec, &t); |
| 3420 | snprintf(dump_buf, PREAMBLE_LENGTH + 1, PREAMBLE_FORMAT, |
| 3421 | outbound ? 'O' : 'I', |
| 3422 | t.tm_hour, t.tm_min, t.tm_sec, (long)tv.tv_usec); |
| 3423 | #endif |
| 3424 | pos += PREAMBLE_LENGTH; |
| 3425 | #if defined(_WIN32) && !defined(__MINGW32__) |
| 3426 | strncpy_s(dump_buf + pos, strlen(HEADER) + 1, HEADER, strlen(HEADER)); |
| 3427 | #else |
| 3428 | strcpy(dump_buf + pos, HEADER); |
| 3429 | #endif |
| 3430 | pos += strlen(HEADER); |
| 3431 | packet = (char *)buf; |
| 3432 | for (i = 0; i < len; i++) { |
| 3433 | uint8_t byte, low, high; |
| 3434 |
| 3435 | byte = (uint8_t)packet[i]; |
| 3436 | high = byte / 16; |
| 3437 | low = byte % 16; |
| 3438 | dump_buf[pos++] = high < 10 ? '0' + high : 'a' + (high - 10); |
| 3439 | dump_buf[pos++] = low < 10 ? '0' + low : 'a' + (low - 10); |
| 3440 | dump_buf[pos++] = ' '; |
| 3441 | } |
| 3442 | #if defined(_WIN32) && !defined(__MINGW32__) |
| 3443 | strncpy_s(dump_buf + pos, strlen(TRAILER) + 1, TRAILER, strlen(TRAILER)); |
| 3444 | #else |
| 3445 | strcpy(dump_buf + pos, TRAILER); |
| 3446 | #endif |
| 3447 | pos += strlen(TRAILER); |
| 3448 | dump_buf[pos++] = '\0'; |
| 3449 | return (dump_buf); |
| 3450 | } |
| 3451 |
| 3452 | void |
| 3453 | usrsctp_freedumpbuffer(char *buf) |
| 3454 | { |
| 3455 | free(buf); |
| 3456 | } |
| 3457 |
| 3458 | void |
| 3459 | usrsctp_enable_crc32c_offload(void) |
| 3460 | { |
| 3461 | SCTP_BASE_VAR(crc32c_offloaded) = 1; |
| 3462 | } |
| 3463 |
| 3464 | void |
| 3465 | usrsctp_disable_crc32c_offload(void) |
| 3466 | { |
| 3467 | SCTP_BASE_VAR(crc32c_offloaded) = 0; |
| 3468 | } |
| 3469 |
| 3470 | /* Compute the CRC32C in network byte order */ |
| 3471 | uint32_t |
| 3472 | usrsctp_crc32c(void *buffer, size_t length) |
| 3473 | { |
| 3474 | uint32_t base = 0xffffffff; |
| 3475 |
| 3476 | base = calculate_crc32c(0xffffffff, (unsigned char *)buffer, (unsigned int) length); |
| 3477 | base = sctp_finalize_crc32c(base); |
| 3478 | return (base); |
| 3479 | } |
| 3480 |
| 3481 | void |
| 3482 | usrsctp_conninput(void *addr, const void *buffer, size_t length, uint8_t ecn_bits) |
| 3483 | { |
| 3484 | struct sockaddr_conn src, dst; |
| 3485 | struct mbuf *m; |
| 3486 | struct sctphdr *sh; |
| 3487 | struct sctp_chunkhdr *ch; |
| 3488 |
| 3489 | SCTP_STAT_INCR(sctps_recvpackets); |
| 3490 | SCTP_STAT_INCR_COUNTER64(sctps_inpackets); |
| 3491 | memset(&src, 0, sizeof(struct sockaddr_conn)); |
| 3492 | src.sconn_family = AF_CONN; |
| 3493 | #ifdef HAVE_SCONN_LEN |
| 3494 | src.sconn_len = sizeof(struct sockaddr_conn); |
| 3495 | #endif |
| 3496 | src.sconn_addr = addr; |
| 3497 | memset(&dst, 0, sizeof(struct sockaddr_conn)); |
| 3498 | dst.sconn_family = AF_CONN; |
| 3499 | #ifdef HAVE_SCONN_LEN |
| 3500 | dst.sconn_len = sizeof(struct sockaddr_conn); |
| 3501 | #endif |
| 3502 | dst.sconn_addr = addr; |
| 3503 | if ((m = sctp_get_mbuf_for_msg((unsigned int)length, 1, M_NOWAIT, 0, MT_DATA)) == NULL) { |
| 3504 | return; |
| 3505 | } |
| 3506 | m_copyback(m, 0, (int)length, (caddr_t)buffer); |
| 3507 | if (SCTP_BUF_LEN(m) < (int)(sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr))) { |
| 3508 | if ((m = m_pullup(m, sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr))) == NULL) { |
| 3509 | SCTP_STAT_INCR(sctps_hdrops); |
| 3510 | return; |
| 3511 | } |
| 3512 | } |
| 3513 | sh = mtod(m, struct sctphdr *);; |
| 3514 | ch = (struct sctp_chunkhdr *)((caddr_t)sh + sizeof(struct sctphdr)); |
| 3515 | src.sconn_port = sh->src_port; |
| 3516 | dst.sconn_port = sh->dest_port; |
| 3517 | sctp_common_input_processing(&m, 0, sizeof(struct sctphdr), (int)length, |
| 3518 | (struct sockaddr *)&src, |
| 3519 | (struct sockaddr *)&dst, |
| 3520 | sh, ch, |
| 3521 | SCTP_BASE_VAR(crc32c_offloaded) == 1 ? 0 : 1, |
| 3522 | ecn_bits, |
| 3523 | SCTP_DEFAULT_VRFID, 0); |
| 3524 | if (m) { |
| 3525 | sctp_m_freem(m); |
| 3526 | } |
| 3527 | return; |
| 3528 | } |
| 3529 |
| 3530 | void usrsctp_handle_timers(uint32_t delta) |
| 3531 | { |
| 3532 | sctp_handle_tick(delta); |
| 3533 | } |
| 3534 |
| 3535 | int |
| 3536 | usrsctp_get_events(struct socket *so) |
| 3537 | { |
| 3538 | int events = 0; |
| 3539 |
| 3540 | if (so == NULL) { |
| 3541 | errno = EBADF; |
| 3542 | return -1; |
| 3543 | } |
| 3544 |
| 3545 | SOCK_LOCK(so); |
| 3546 | if (soreadable(so)) { |
| 3547 | events |= SCTP_EVENT_READ; |
| 3548 | } |
| 3549 | if (sowriteable(so)) { |
| 3550 | events |= SCTP_EVENT_WRITE; |
| 3551 | } |
| 3552 | if (so->so_error) { |
| 3553 | events |= SCTP_EVENT_ERROR; |
| 3554 | } |
| 3555 | SOCK_UNLOCK(so); |
| 3556 |
| 3557 | return events; |
| 3558 | } |
| 3559 |
| 3560 | int |
| 3561 | usrsctp_set_upcall(struct socket *so, void (*upcall)(struct socket *, void *, int), void *arg) |
| 3562 | { |
| 3563 | if (so == NULL) { |
| 3564 | errno = EBADF; |
| 3565 | return (-1); |
| 3566 | } |
| 3567 |
| 3568 | SOCK_LOCK(so); |
| 3569 | so->so_upcall = upcall; |
| 3570 | so->so_upcallarg = arg; |
| 3571 | so->so_snd.sb_flags |= SB_UPCALL; |
| 3572 | so->so_rcv.sb_flags |= SB_UPCALL; |
| 3573 | SOCK_UNLOCK(so); |
| 3574 |
| 3575 | return (0); |
| 3576 | } |
| 3577 |
| 3578 | #define USRSCTP_TUNABLE_SET_DEF(__field, __prefix) \ |
| 3579 | int usrsctp_tunable_set_ ## __field(uint32_t value) \ |
| 3580 | { \ |
| 3581 | if ((value < __prefix##_MIN) || \ |
| 3582 | (value > __prefix##_MAX)) { \ |
| 3583 | errno = EINVAL; \ |
| 3584 | return (-1); \ |
| 3585 | } else { \ |
| 3586 | SCTP_BASE_SYSCTL(__field) = value; \ |
| 3587 | return (0); \ |
| 3588 | } \ |
| 3589 | } |
| 3590 |
| 3591 | USRSCTP_TUNABLE_SET_DEF(sctp_hashtblsize, SCTPCTL_TCBHASHSIZE) |
| 3592 | USRSCTP_TUNABLE_SET_DEF(sctp_pcbtblsize, SCTPCTL_PCBHASHSIZE) |
| 3593 | USRSCTP_TUNABLE_SET_DEF(sctp_chunkscale, SCTPCTL_CHUNKSCALE) |
| 3594 |
| 3595 | #define USRSCTP_SYSCTL_SET_DEF(__field, __prefix) \ |
| 3596 | int usrsctp_sysctl_set_ ## __field(uint32_t value) \ |
| 3597 | { \ |
| 3598 | if ((value < __prefix##_MIN) || \ |
| 3599 | (value > __prefix##_MAX)) { \ |
| 3600 | errno = EINVAL; \ |
| 3601 | return (-1); \ |
| 3602 | } else { \ |
| 3603 | SCTP_BASE_SYSCTL(__field) = value; \ |
| 3604 | return (0); \ |
| 3605 | } \ |
| 3606 | } |
| 3607 |
| 3608 | #if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || defined(__clang__) |
| 3609 | #pragma GCC diagnostic push |
| 3610 | #pragma GCC diagnostic ignored "-Wtype-limits" |
| 3611 | #endif |
| 3612 | USRSCTP_SYSCTL_SET_DEF(sctp_sendspace, SCTPCTL_MAXDGRAM) |
| 3613 | USRSCTP_SYSCTL_SET_DEF(sctp_recvspace, SCTPCTL_RECVSPACE) |
| 3614 | USRSCTP_SYSCTL_SET_DEF(sctp_auto_asconf, SCTPCTL_AUTOASCONF) |
| 3615 | USRSCTP_SYSCTL_SET_DEF(sctp_ecn_enable, SCTPCTL_ECN_ENABLE) |
| 3616 | USRSCTP_SYSCTL_SET_DEF(sctp_pr_enable, SCTPCTL_PR_ENABLE) |
| 3617 | USRSCTP_SYSCTL_SET_DEF(sctp_auth_enable, SCTPCTL_AUTH_ENABLE) |
| 3618 | USRSCTP_SYSCTL_SET_DEF(sctp_asconf_enable, SCTPCTL_ASCONF_ENABLE) |
| 3619 | USRSCTP_SYSCTL_SET_DEF(sctp_reconfig_enable, SCTPCTL_RECONFIG_ENABLE) |
| 3620 | USRSCTP_SYSCTL_SET_DEF(sctp_nrsack_enable, SCTPCTL_NRSACK_ENABLE) |
| 3621 | USRSCTP_SYSCTL_SET_DEF(sctp_pktdrop_enable, SCTPCTL_PKTDROP_ENABLE) |
| 3622 | USRSCTP_SYSCTL_SET_DEF(sctp_no_csum_on_loopback, SCTPCTL_LOOPBACK_NOCSUM) |
| 3623 | USRSCTP_SYSCTL_SET_DEF(sctp_peer_chunk_oh, SCTPCTL_PEER_CHKOH) |
| 3624 | USRSCTP_SYSCTL_SET_DEF(sctp_max_burst_default, SCTPCTL_MAXBURST) |
| 3625 | USRSCTP_SYSCTL_SET_DEF(sctp_max_chunks_on_queue, SCTPCTL_MAXCHUNKS) |
| 3626 | USRSCTP_SYSCTL_SET_DEF(sctp_min_split_point, SCTPCTL_MIN_SPLIT_POINT) |
| 3627 | USRSCTP_SYSCTL_SET_DEF(sctp_delayed_sack_time_default, SCTPCTL_DELAYED_SACK_TIME) |
| 3628 | USRSCTP_SYSCTL_SET_DEF(sctp_sack_freq_default, SCTPCTL_SACK_FREQ) |
| 3629 | USRSCTP_SYSCTL_SET_DEF(sctp_system_free_resc_limit, SCTPCTL_SYS_RESOURCE) |
| 3630 | USRSCTP_SYSCTL_SET_DEF(sctp_asoc_free_resc_limit, SCTPCTL_ASOC_RESOURCE) |
| 3631 | USRSCTP_SYSCTL_SET_DEF(sctp_heartbeat_interval_default, SCTPCTL_HEARTBEAT_INTERVAL) |
| 3632 | USRSCTP_SYSCTL_SET_DEF(sctp_pmtu_raise_time_default, SCTPCTL_PMTU_RAISE_TIME) |
| 3633 | USRSCTP_SYSCTL_SET_DEF(sctp_shutdown_guard_time_default, SCTPCTL_SHUTDOWN_GUARD_TIME) |
| 3634 | USRSCTP_SYSCTL_SET_DEF(sctp_secret_lifetime_default, SCTPCTL_SECRET_LIFETIME) |
| 3635 | USRSCTP_SYSCTL_SET_DEF(sctp_rto_max_default, SCTPCTL_RTO_MAX) |
| 3636 | USRSCTP_SYSCTL_SET_DEF(sctp_rto_min_default, SCTPCTL_RTO_MIN) |
| 3637 | USRSCTP_SYSCTL_SET_DEF(sctp_rto_initial_default, SCTPCTL_RTO_INITIAL) |
| 3638 | USRSCTP_SYSCTL_SET_DEF(sctp_init_rto_max_default, SCTPCTL_INIT_RTO_MAX) |
| 3639 | USRSCTP_SYSCTL_SET_DEF(sctp_valid_cookie_life_default, SCTPCTL_VALID_COOKIE_LIFE) |
| 3640 | USRSCTP_SYSCTL_SET_DEF(sctp_init_rtx_max_default, SCTPCTL_INIT_RTX_MAX) |
| 3641 | USRSCTP_SYSCTL_SET_DEF(sctp_assoc_rtx_max_default, SCTPCTL_ASSOC_RTX_MAX) |
| 3642 | USRSCTP_SYSCTL_SET_DEF(sctp_path_rtx_max_default, SCTPCTL_PATH_RTX_MAX) |
| 3643 | USRSCTP_SYSCTL_SET_DEF(sctp_add_more_threshold, SCTPCTL_ADD_MORE_ON_OUTPUT) |
| 3644 | USRSCTP_SYSCTL_SET_DEF(sctp_nr_incoming_streams_default, SCTPCTL_INCOMING_STREAMS) |
| 3645 | USRSCTP_SYSCTL_SET_DEF(sctp_nr_outgoing_streams_default, SCTPCTL_OUTGOING_STREAMS) |
| 3646 | USRSCTP_SYSCTL_SET_DEF(sctp_cmt_on_off, SCTPCTL_CMT_ON_OFF) |
| 3647 | USRSCTP_SYSCTL_SET_DEF(sctp_cmt_use_dac, SCTPCTL_CMT_USE_DAC) |
| 3648 | USRSCTP_SYSCTL_SET_DEF(sctp_use_cwnd_based_maxburst, SCTPCTL_CWND_MAXBURST) |
| 3649 | USRSCTP_SYSCTL_SET_DEF(sctp_nat_friendly, SCTPCTL_NAT_FRIENDLY) |
| 3650 | USRSCTP_SYSCTL_SET_DEF(sctp_L2_abc_variable, SCTPCTL_ABC_L_VAR) |
| 3651 | USRSCTP_SYSCTL_SET_DEF(sctp_mbuf_threshold_count, SCTPCTL_MAX_CHAINED_MBUFS) |
| 3652 | USRSCTP_SYSCTL_SET_DEF(sctp_do_drain, SCTPCTL_DO_SCTP_DRAIN) |
| 3653 | USRSCTP_SYSCTL_SET_DEF(sctp_hb_maxburst, SCTPCTL_HB_MAX_BURST) |
| 3654 | USRSCTP_SYSCTL_SET_DEF(sctp_abort_if_one_2_one_hits_limit, SCTPCTL_ABORT_AT_LIMIT) |
| 3655 | USRSCTP_SYSCTL_SET_DEF(sctp_min_residual, SCTPCTL_MIN_RESIDUAL) |
| 3656 | USRSCTP_SYSCTL_SET_DEF(sctp_max_retran_chunk, SCTPCTL_MAX_RETRAN_CHUNK) |
| 3657 | USRSCTP_SYSCTL_SET_DEF(sctp_logging_level, SCTPCTL_LOGGING_LEVEL) |
| 3658 | USRSCTP_SYSCTL_SET_DEF(sctp_default_cc_module, SCTPCTL_DEFAULT_CC_MODULE) |
| 3659 | USRSCTP_SYSCTL_SET_DEF(sctp_default_frag_interleave, SCTPCTL_DEFAULT_FRAG_INTERLEAVE) |
| 3660 | USRSCTP_SYSCTL_SET_DEF(sctp_mobility_base, SCTPCTL_MOBILITY_BASE) |
| 3661 | USRSCTP_SYSCTL_SET_DEF(sctp_mobility_fasthandoff, SCTPCTL_MOBILITY_FASTHANDOFF) |
| 3662 | USRSCTP_SYSCTL_SET_DEF(sctp_inits_include_nat_friendly, SCTPCTL_NAT_FRIENDLY_INITS) |
| 3663 | USRSCTP_SYSCTL_SET_DEF(sctp_udp_tunneling_port, SCTPCTL_UDP_TUNNELING_PORT) |
| 3664 | USRSCTP_SYSCTL_SET_DEF(sctp_enable_sack_immediately, SCTPCTL_SACK_IMMEDIATELY_ENABLE) |
| 3665 | USRSCTP_SYSCTL_SET_DEF(sctp_vtag_time_wait, SCTPCTL_TIME_WAIT) |
| 3666 | USRSCTP_SYSCTL_SET_DEF(sctp_blackhole, SCTPCTL_BLACKHOLE) |
| 3667 | USRSCTP_SYSCTL_SET_DEF(sctp_diag_info_code, SCTPCTL_DIAG_INFO_CODE) |
| 3668 | USRSCTP_SYSCTL_SET_DEF(sctp_fr_max_burst_default, SCTPCTL_FRMAXBURST) |
| 3669 | USRSCTP_SYSCTL_SET_DEF(sctp_path_pf_threshold, SCTPCTL_PATH_PF_THRESHOLD) |
| 3670 | USRSCTP_SYSCTL_SET_DEF(sctp_default_ss_module, SCTPCTL_DEFAULT_SS_MODULE) |
| 3671 | USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_bw, SCTPCTL_RTTVAR_BW) |
| 3672 | USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_rtt, SCTPCTL_RTTVAR_RTT) |
| 3673 | USRSCTP_SYSCTL_SET_DEF(sctp_rttvar_eqret, SCTPCTL_RTTVAR_EQRET) |
| 3674 | USRSCTP_SYSCTL_SET_DEF(sctp_steady_step, SCTPCTL_RTTVAR_STEADYS) |
| 3675 | USRSCTP_SYSCTL_SET_DEF(sctp_use_dccc_ecn, SCTPCTL_RTTVAR_DCCCECN) |
| 3676 | USRSCTP_SYSCTL_SET_DEF(sctp_buffer_splitting, SCTPCTL_BUFFER_SPLITTING) |
| 3677 | USRSCTP_SYSCTL_SET_DEF(sctp_initial_cwnd, SCTPCTL_INITIAL_CWND) |
| 3678 | #ifdef SCTP_DEBUG |
| 3679 | USRSCTP_SYSCTL_SET_DEF(sctp_debug_on, SCTPCTL_DEBUG) |
| 3680 | #endif |
| 3681 | #if __GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || defined(__clang__) |
| 3682 | #pragma GCC diagnostic pop |
| 3683 | #endif |
| 3684 |
| 3685 | #define USRSCTP_SYSCTL_GET_DEF(__field) \ |
| 3686 | uint32_t usrsctp_sysctl_get_ ## __field(void) { \ |
| 3687 | return SCTP_BASE_SYSCTL(__field); \ |
| 3688 | } |
| 3689 |
| 3690 | USRSCTP_SYSCTL_GET_DEF(sctp_sendspace) |
| 3691 | USRSCTP_SYSCTL_GET_DEF(sctp_recvspace) |
| 3692 | USRSCTP_SYSCTL_GET_DEF(sctp_auto_asconf) |
| 3693 | USRSCTP_SYSCTL_GET_DEF(sctp_multiple_asconfs) |
| 3694 | USRSCTP_SYSCTL_GET_DEF(sctp_ecn_enable) |
| 3695 | USRSCTP_SYSCTL_GET_DEF(sctp_pr_enable) |
| 3696 | USRSCTP_SYSCTL_GET_DEF(sctp_auth_enable) |
| 3697 | USRSCTP_SYSCTL_GET_DEF(sctp_asconf_enable) |
| 3698 | USRSCTP_SYSCTL_GET_DEF(sctp_reconfig_enable) |
| 3699 | USRSCTP_SYSCTL_GET_DEF(sctp_nrsack_enable) |
| 3700 | USRSCTP_SYSCTL_GET_DEF(sctp_pktdrop_enable) |
| 3701 | USRSCTP_SYSCTL_GET_DEF(sctp_no_csum_on_loopback) |
| 3702 | USRSCTP_SYSCTL_GET_DEF(sctp_peer_chunk_oh) |
| 3703 | USRSCTP_SYSCTL_GET_DEF(sctp_max_burst_default) |
| 3704 | USRSCTP_SYSCTL_GET_DEF(sctp_max_chunks_on_queue) |
| 3705 | USRSCTP_SYSCTL_GET_DEF(sctp_hashtblsize) |
| 3706 | USRSCTP_SYSCTL_GET_DEF(sctp_pcbtblsize) |
| 3707 | USRSCTP_SYSCTL_GET_DEF(sctp_min_split_point) |
| 3708 | USRSCTP_SYSCTL_GET_DEF(sctp_chunkscale) |
| 3709 | USRSCTP_SYSCTL_GET_DEF(sctp_delayed_sack_time_default) |
| 3710 | USRSCTP_SYSCTL_GET_DEF(sctp_sack_freq_default) |
| 3711 | USRSCTP_SYSCTL_GET_DEF(sctp_system_free_resc_limit) |
| 3712 | USRSCTP_SYSCTL_GET_DEF(sctp_asoc_free_resc_limit) |
| 3713 | USRSCTP_SYSCTL_GET_DEF(sctp_heartbeat_interval_default) |
| 3714 | USRSCTP_SYSCTL_GET_DEF(sctp_pmtu_raise_time_default) |
| 3715 | USRSCTP_SYSCTL_GET_DEF(sctp_shutdown_guard_time_default) |
| 3716 | USRSCTP_SYSCTL_GET_DEF(sctp_secret_lifetime_default) |
| 3717 | USRSCTP_SYSCTL_GET_DEF(sctp_rto_max_default) |
| 3718 | USRSCTP_SYSCTL_GET_DEF(sctp_rto_min_default) |
| 3719 | USRSCTP_SYSCTL_GET_DEF(sctp_rto_initial_default) |
| 3720 | USRSCTP_SYSCTL_GET_DEF(sctp_init_rto_max_default) |
| 3721 | USRSCTP_SYSCTL_GET_DEF(sctp_valid_cookie_life_default) |
| 3722 | USRSCTP_SYSCTL_GET_DEF(sctp_init_rtx_max_default) |
| 3723 | USRSCTP_SYSCTL_GET_DEF(sctp_assoc_rtx_max_default) |
| 3724 | USRSCTP_SYSCTL_GET_DEF(sctp_path_rtx_max_default) |
| 3725 | USRSCTP_SYSCTL_GET_DEF(sctp_add_more_threshold) |
| 3726 | USRSCTP_SYSCTL_GET_DEF(sctp_nr_incoming_streams_default) |
| 3727 | USRSCTP_SYSCTL_GET_DEF(sctp_nr_outgoing_streams_default) |
| 3728 | USRSCTP_SYSCTL_GET_DEF(sctp_cmt_on_off) |
| 3729 | USRSCTP_SYSCTL_GET_DEF(sctp_cmt_use_dac) |
| 3730 | USRSCTP_SYSCTL_GET_DEF(sctp_use_cwnd_based_maxburst) |
| 3731 | USRSCTP_SYSCTL_GET_DEF(sctp_nat_friendly) |
| 3732 | USRSCTP_SYSCTL_GET_DEF(sctp_L2_abc_variable) |
| 3733 | USRSCTP_SYSCTL_GET_DEF(sctp_mbuf_threshold_count) |
| 3734 | USRSCTP_SYSCTL_GET_DEF(sctp_do_drain) |
| 3735 | USRSCTP_SYSCTL_GET_DEF(sctp_hb_maxburst) |
| 3736 | USRSCTP_SYSCTL_GET_DEF(sctp_abort_if_one_2_one_hits_limit) |
| 3737 | USRSCTP_SYSCTL_GET_DEF(sctp_min_residual) |
| 3738 | USRSCTP_SYSCTL_GET_DEF(sctp_max_retran_chunk) |
| 3739 | USRSCTP_SYSCTL_GET_DEF(sctp_logging_level) |
| 3740 | USRSCTP_SYSCTL_GET_DEF(sctp_default_cc_module) |
| 3741 | USRSCTP_SYSCTL_GET_DEF(sctp_default_frag_interleave) |
| 3742 | USRSCTP_SYSCTL_GET_DEF(sctp_mobility_base) |
| 3743 | USRSCTP_SYSCTL_GET_DEF(sctp_mobility_fasthandoff) |
| 3744 | USRSCTP_SYSCTL_GET_DEF(sctp_inits_include_nat_friendly) |
| 3745 | USRSCTP_SYSCTL_GET_DEF(sctp_udp_tunneling_port) |
| 3746 | USRSCTP_SYSCTL_GET_DEF(sctp_enable_sack_immediately) |
| 3747 | USRSCTP_SYSCTL_GET_DEF(sctp_vtag_time_wait) |
| 3748 | USRSCTP_SYSCTL_GET_DEF(sctp_blackhole) |
| 3749 | USRSCTP_SYSCTL_GET_DEF(sctp_diag_info_code) |
| 3750 | USRSCTP_SYSCTL_GET_DEF(sctp_fr_max_burst_default) |
| 3751 | USRSCTP_SYSCTL_GET_DEF(sctp_path_pf_threshold) |
| 3752 | USRSCTP_SYSCTL_GET_DEF(sctp_default_ss_module) |
| 3753 | USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_bw) |
| 3754 | USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_rtt) |
| 3755 | USRSCTP_SYSCTL_GET_DEF(sctp_rttvar_eqret) |
| 3756 | USRSCTP_SYSCTL_GET_DEF(sctp_steady_step) |
| 3757 | USRSCTP_SYSCTL_GET_DEF(sctp_use_dccc_ecn) |
| 3758 | USRSCTP_SYSCTL_GET_DEF(sctp_buffer_splitting) |
| 3759 | USRSCTP_SYSCTL_GET_DEF(sctp_initial_cwnd) |
| 3760 | #ifdef SCTP_DEBUG |
| 3761 | USRSCTP_SYSCTL_GET_DEF(sctp_debug_on) |
| 3762 | #endif |
| 3763 |
| 3764 | void usrsctp_get_stat(struct sctpstat *stat) |
| 3765 | { |
| 3766 | *stat = SCTP_BASE_STATS; |
| 3767 | } |
| 3768 |