Coverage Report

Created: 2025-11-11 06:43

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