Coverage Report

Created: 2025-07-11 06:39

/src/usrsctp/usrsctplib/netinet/sctputil.c
Line
Count
Source (jump to first uncovered line)
1
/*-
2
 * SPDX-License-Identifier: BSD-3-Clause
3
 *
4
 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5
 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6
 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions are met:
10
 *
11
 * a) Redistributions of source code must retain the above copyright notice,
12
 *    this list of conditions and the following disclaimer.
13
 *
14
 * b) Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in
16
 *    the documentation and/or other materials provided with the distribution.
17
 *
18
 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19
 *    contributors may be used to endorse or promote products derived
20
 *    from this software without specific prior written permission.
21
 *
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32
 * THE POSSIBILITY OF SUCH DAMAGE.
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
#ifdef INET6
41
#if defined(__Userspace__) || defined(__FreeBSD__)
42
#include <netinet6/sctp6_var.h>
43
#endif
44
#endif
45
#include <netinet/sctp_header.h>
46
#include <netinet/sctp_output.h>
47
#include <netinet/sctp_uio.h>
48
#include <netinet/sctp_timer.h>
49
#include <netinet/sctp_indata.h>
50
#include <netinet/sctp_auth.h>
51
#include <netinet/sctp_asconf.h>
52
#include <netinet/sctp_bsd_addr.h>
53
#if defined(__Userspace__)
54
#include <netinet/sctp_constants.h>
55
#endif
56
#if defined(__FreeBSD__) && !defined(__Userspace__)
57
#include <netinet/sctp_kdtrace.h>
58
#if defined(INET6) || defined(INET)
59
#include <netinet/tcp_var.h>
60
#endif
61
#include <netinet/udp.h>
62
#include <netinet/udp_var.h>
63
#include <sys/proc.h>
64
#ifdef INET6
65
#include <netinet/icmp6.h>
66
#endif
67
#endif
68
69
#if defined(_WIN32) && !defined(__Userspace__)
70
#if !defined(SCTP_LOCAL_TRACE_BUF)
71
#include "eventrace_netinet.h"
72
#include "sctputil.tmh" /* this is the file that will be auto generated */
73
#endif
74
#else
75
#ifndef KTR_SCTP
76
#define KTR_SCTP KTR_SUBSYS
77
#endif
78
#endif
79
80
extern const struct sctp_cc_functions sctp_cc_functions[];
81
extern const struct sctp_ss_functions sctp_ss_functions[];
82
83
void
84
sctp_sblog(struct sockbuf *sb, struct sctp_tcb *stcb, int from, int incr)
85
0
{
86
#if defined(SCTP_LOCAL_TRACE_BUF)
87
  struct sctp_cwnd_log sctp_clog;
88
89
  sctp_clog.x.sb.stcb = stcb;
90
  sctp_clog.x.sb.so_sbcc = SCTP_SBAVAIL(sb);
91
  if (stcb)
92
    sctp_clog.x.sb.stcb_sbcc = stcb->asoc.sb_cc;
93
  else
94
    sctp_clog.x.sb.stcb_sbcc = 0;
95
  sctp_clog.x.sb.incr = incr;
96
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
97
       SCTP_LOG_EVENT_SB,
98
       from,
99
       sctp_clog.x.misc.log1,
100
       sctp_clog.x.misc.log2,
101
       sctp_clog.x.misc.log3,
102
       sctp_clog.x.misc.log4);
103
#endif
104
0
}
105
106
void
107
sctp_log_closing(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int16_t loc)
108
0
{
109
#if defined(SCTP_LOCAL_TRACE_BUF)
110
  struct sctp_cwnd_log sctp_clog;
111
112
  sctp_clog.x.close.inp = (void *)inp;
113
  sctp_clog.x.close.sctp_flags = inp->sctp_flags;
114
  if (stcb) {
115
    sctp_clog.x.close.stcb = (void *)stcb;
116
    sctp_clog.x.close.state = (uint16_t)stcb->asoc.state;
117
  } else {
118
    sctp_clog.x.close.stcb = 0;
119
    sctp_clog.x.close.state = 0;
120
  }
121
  sctp_clog.x.close.loc = loc;
122
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
123
       SCTP_LOG_EVENT_CLOSE,
124
       0,
125
       sctp_clog.x.misc.log1,
126
       sctp_clog.x.misc.log2,
127
       sctp_clog.x.misc.log3,
128
       sctp_clog.x.misc.log4);
129
#endif
130
0
}
131
132
void
133
rto_logging(struct sctp_nets *net, int from)
134
0
{
135
#if defined(SCTP_LOCAL_TRACE_BUF)
136
  struct sctp_cwnd_log sctp_clog;
137
138
  memset(&sctp_clog, 0, sizeof(sctp_clog));
139
  sctp_clog.x.rto.net = (void *) net;
140
  sctp_clog.x.rto.rtt = net->rtt / 1000;
141
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
142
       SCTP_LOG_EVENT_RTT,
143
       from,
144
       sctp_clog.x.misc.log1,
145
       sctp_clog.x.misc.log2,
146
       sctp_clog.x.misc.log3,
147
       sctp_clog.x.misc.log4);
148
#endif
149
0
}
150
151
void
152
sctp_log_strm_del_alt(struct sctp_tcb *stcb, uint32_t tsn, uint16_t sseq, uint16_t stream, int from)
153
0
{
154
#if defined(SCTP_LOCAL_TRACE_BUF)
155
  struct sctp_cwnd_log sctp_clog;
156
157
  sctp_clog.x.strlog.stcb = stcb;
158
  sctp_clog.x.strlog.n_tsn = tsn;
159
  sctp_clog.x.strlog.n_sseq = sseq;
160
  sctp_clog.x.strlog.e_tsn = 0;
161
  sctp_clog.x.strlog.e_sseq = 0;
162
  sctp_clog.x.strlog.strm = stream;
163
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
164
       SCTP_LOG_EVENT_STRM,
165
       from,
166
       sctp_clog.x.misc.log1,
167
       sctp_clog.x.misc.log2,
168
       sctp_clog.x.misc.log3,
169
       sctp_clog.x.misc.log4);
170
#endif
171
0
}
172
173
void
174
sctp_log_nagle_event(struct sctp_tcb *stcb, int action)
175
0
{
176
#if defined(SCTP_LOCAL_TRACE_BUF)
177
  struct sctp_cwnd_log sctp_clog;
178
179
  sctp_clog.x.nagle.stcb = (void *)stcb;
180
  sctp_clog.x.nagle.total_flight = stcb->asoc.total_flight;
181
  sctp_clog.x.nagle.total_in_queue = stcb->asoc.total_output_queue_size;
182
  sctp_clog.x.nagle.count_in_queue = stcb->asoc.chunks_on_out_queue;
183
  sctp_clog.x.nagle.count_in_flight = stcb->asoc.total_flight_count;
184
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
185
       SCTP_LOG_EVENT_NAGLE,
186
       action,
187
       sctp_clog.x.misc.log1,
188
       sctp_clog.x.misc.log2,
189
       sctp_clog.x.misc.log3,
190
       sctp_clog.x.misc.log4);
191
#endif
192
0
}
193
194
void
195
sctp_log_sack(uint32_t old_cumack, uint32_t cumack, uint32_t tsn, uint16_t gaps, uint16_t dups, int from)
196
0
{
197
#if defined(SCTP_LOCAL_TRACE_BUF)
198
  struct sctp_cwnd_log sctp_clog;
199
200
  sctp_clog.x.sack.cumack = cumack;
201
  sctp_clog.x.sack.oldcumack = old_cumack;
202
  sctp_clog.x.sack.tsn = tsn;
203
  sctp_clog.x.sack.numGaps = gaps;
204
  sctp_clog.x.sack.numDups = dups;
205
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
206
       SCTP_LOG_EVENT_SACK,
207
       from,
208
       sctp_clog.x.misc.log1,
209
       sctp_clog.x.misc.log2,
210
       sctp_clog.x.misc.log3,
211
       sctp_clog.x.misc.log4);
212
#endif
213
0
}
214
215
void
216
sctp_log_map(uint32_t map, uint32_t cum, uint32_t high, int from)
217
0
{
218
#if defined(SCTP_LOCAL_TRACE_BUF)
219
  struct sctp_cwnd_log sctp_clog;
220
221
  memset(&sctp_clog, 0, sizeof(sctp_clog));
222
  sctp_clog.x.map.base = map;
223
  sctp_clog.x.map.cum = cum;
224
  sctp_clog.x.map.high = high;
225
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
226
       SCTP_LOG_EVENT_MAP,
227
       from,
228
       sctp_clog.x.misc.log1,
229
       sctp_clog.x.misc.log2,
230
       sctp_clog.x.misc.log3,
231
       sctp_clog.x.misc.log4);
232
#endif
233
0
}
234
235
void
236
sctp_log_fr(uint32_t biggest_tsn, uint32_t biggest_new_tsn, uint32_t tsn, int from)
237
0
{
238
#if defined(SCTP_LOCAL_TRACE_BUF)
239
  struct sctp_cwnd_log sctp_clog;
240
241
  memset(&sctp_clog, 0, sizeof(sctp_clog));
242
  sctp_clog.x.fr.largest_tsn = biggest_tsn;
243
  sctp_clog.x.fr.largest_new_tsn = biggest_new_tsn;
244
  sctp_clog.x.fr.tsn = tsn;
245
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
246
       SCTP_LOG_EVENT_FR,
247
       from,
248
       sctp_clog.x.misc.log1,
249
       sctp_clog.x.misc.log2,
250
       sctp_clog.x.misc.log3,
251
       sctp_clog.x.misc.log4);
252
#endif
253
0
}
254
255
#ifdef SCTP_MBUF_LOGGING
256
void
257
sctp_log_mb(struct mbuf *m, int from)
258
{
259
#if defined(SCTP_LOCAL_TRACE_BUF)
260
  struct sctp_cwnd_log sctp_clog;
261
262
  sctp_clog.x.mb.mp = m;
263
  sctp_clog.x.mb.mbuf_flags = (uint8_t)(SCTP_BUF_GET_FLAGS(m));
264
  sctp_clog.x.mb.size = (uint16_t)(SCTP_BUF_LEN(m));
265
  sctp_clog.x.mb.data = SCTP_BUF_AT(m, 0);
266
  if (SCTP_BUF_IS_EXTENDED(m)) {
267
    sctp_clog.x.mb.ext = SCTP_BUF_EXTEND_BASE(m);
268
#if defined(__APPLE__) && !defined(__Userspace__)
269
    /* APPLE does not use a ref_cnt, but a forward/backward ref queue */
270
#else
271
    sctp_clog.x.mb.refcnt = (uint8_t)(SCTP_BUF_EXTEND_REFCNT(m));
272
#endif
273
  } else {
274
    sctp_clog.x.mb.ext = 0;
275
    sctp_clog.x.mb.refcnt = 0;
276
  }
277
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
278
       SCTP_LOG_EVENT_MBUF,
279
       from,
280
       sctp_clog.x.misc.log1,
281
       sctp_clog.x.misc.log2,
282
       sctp_clog.x.misc.log3,
283
       sctp_clog.x.misc.log4);
284
#endif
285
}
286
287
void
288
sctp_log_mbc(struct mbuf *m, int from)
289
{
290
  struct mbuf *mat;
291
292
  for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
293
    sctp_log_mb(mat, from);
294
  }
295
}
296
#endif
297
298
void
299
sctp_log_strm_del(struct sctp_queued_to_read *control, struct sctp_queued_to_read *poschk, int from)
300
0
{
301
#if defined(SCTP_LOCAL_TRACE_BUF)
302
  struct sctp_cwnd_log sctp_clog;
303
304
  if (control == NULL) {
305
    SCTP_PRINTF("Gak log of NULL?\n");
306
    return;
307
  }
308
  sctp_clog.x.strlog.stcb = control->stcb;
309
  sctp_clog.x.strlog.n_tsn = control->sinfo_tsn;
310
  sctp_clog.x.strlog.n_sseq = (uint16_t)control->mid;
311
  sctp_clog.x.strlog.strm = control->sinfo_stream;
312
  if (poschk != NULL) {
313
    sctp_clog.x.strlog.e_tsn = poschk->sinfo_tsn;
314
    sctp_clog.x.strlog.e_sseq = (uint16_t)poschk->mid;
315
  } else {
316
    sctp_clog.x.strlog.e_tsn = 0;
317
    sctp_clog.x.strlog.e_sseq = 0;
318
  }
319
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
320
       SCTP_LOG_EVENT_STRM,
321
       from,
322
       sctp_clog.x.misc.log1,
323
       sctp_clog.x.misc.log2,
324
       sctp_clog.x.misc.log3,
325
       sctp_clog.x.misc.log4);
326
#endif
327
0
}
328
329
void
330
sctp_log_cwnd(struct sctp_tcb *stcb, struct sctp_nets *net, int augment, uint8_t from)
331
0
{
332
#if defined(SCTP_LOCAL_TRACE_BUF)
333
  struct sctp_cwnd_log sctp_clog;
334
335
  sctp_clog.x.cwnd.net = net;
336
  if (stcb->asoc.send_queue_cnt > 255)
337
    sctp_clog.x.cwnd.cnt_in_send = 255;
338
  else
339
    sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
340
  if (stcb->asoc.stream_queue_cnt > 255)
341
    sctp_clog.x.cwnd.cnt_in_str = 255;
342
  else
343
    sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
344
345
  if (net) {
346
    sctp_clog.x.cwnd.cwnd_new_value = net->cwnd;
347
    sctp_clog.x.cwnd.inflight = net->flight_size;
348
    sctp_clog.x.cwnd.pseudo_cumack = net->pseudo_cumack;
349
    sctp_clog.x.cwnd.meets_pseudo_cumack = net->new_pseudo_cumack;
350
    sctp_clog.x.cwnd.need_new_pseudo_cumack = net->find_pseudo_cumack;
351
  }
352
  if (SCTP_CWNDLOG_PRESEND == from) {
353
    sctp_clog.x.cwnd.meets_pseudo_cumack = stcb->asoc.peers_rwnd;
354
  }
355
  sctp_clog.x.cwnd.cwnd_augment = augment;
356
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
357
       SCTP_LOG_EVENT_CWND,
358
       from,
359
       sctp_clog.x.misc.log1,
360
       sctp_clog.x.misc.log2,
361
       sctp_clog.x.misc.log3,
362
       sctp_clog.x.misc.log4);
363
#endif
364
0
}
365
366
#if !defined(__APPLE__) && !defined(__Userspace__)
367
void
368
sctp_log_lock(struct sctp_inpcb *inp, struct sctp_tcb *stcb, uint8_t from)
369
{
370
#if defined(SCTP_LOCAL_TRACE_BUF)
371
  struct sctp_cwnd_log sctp_clog;
372
373
  memset(&sctp_clog, 0, sizeof(sctp_clog));
374
  if (inp) {
375
    sctp_clog.x.lock.sock = (void *) inp->sctp_socket;
376
377
  } else {
378
    sctp_clog.x.lock.sock = (void *) NULL;
379
  }
380
  sctp_clog.x.lock.inp = (void *) inp;
381
#if defined(__FreeBSD__)
382
  if (stcb) {
383
    sctp_clog.x.lock.tcb_lock = mtx_owned(&stcb->tcb_mtx);
384
  } else {
385
    sctp_clog.x.lock.tcb_lock = SCTP_LOCK_UNKNOWN;
386
  }
387
  if (inp) {
388
    sctp_clog.x.lock.inp_lock = mtx_owned(&inp->inp_mtx);
389
    sctp_clog.x.lock.create_lock = mtx_owned(&inp->inp_create_mtx);
390
  } else {
391
    sctp_clog.x.lock.inp_lock = SCTP_LOCK_UNKNOWN;
392
    sctp_clog.x.lock.create_lock = SCTP_LOCK_UNKNOWN;
393
  }
394
  sctp_clog.x.lock.info_lock = rw_wowned(&SCTP_BASE_INFO(ipi_ep_mtx));
395
  if (inp && (inp->sctp_socket)) {
396
    sctp_clog.x.lock.sock_lock = mtx_owned(SOCK_MTX(inp->sctp_socket));
397
    sctp_clog.x.lock.sockrcvbuf_lock = mtx_owned(SOCKBUF_MTX(&inp->sctp_socket->so_rcv));
398
    sctp_clog.x.lock.socksndbuf_lock = mtx_owned(SOCKBUF_MTX(&inp->sctp_socket->so_snd));
399
  } else {
400
    sctp_clog.x.lock.sock_lock = SCTP_LOCK_UNKNOWN;
401
    sctp_clog.x.lock.sockrcvbuf_lock = SCTP_LOCK_UNKNOWN;
402
    sctp_clog.x.lock.socksndbuf_lock = SCTP_LOCK_UNKNOWN;
403
  }
404
#endif
405
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
406
       SCTP_LOG_LOCK_EVENT,
407
       from,
408
       sctp_clog.x.misc.log1,
409
       sctp_clog.x.misc.log2,
410
       sctp_clog.x.misc.log3,
411
       sctp_clog.x.misc.log4);
412
#endif
413
}
414
#endif
415
416
void
417
sctp_log_maxburst(struct sctp_tcb *stcb, struct sctp_nets *net, int error, int burst, uint8_t from)
418
0
{
419
#if defined(SCTP_LOCAL_TRACE_BUF)
420
  struct sctp_cwnd_log sctp_clog;
421
422
  memset(&sctp_clog, 0, sizeof(sctp_clog));
423
  sctp_clog.x.cwnd.net = net;
424
  sctp_clog.x.cwnd.cwnd_new_value = error;
425
  sctp_clog.x.cwnd.inflight = net->flight_size;
426
  sctp_clog.x.cwnd.cwnd_augment = burst;
427
  if (stcb->asoc.send_queue_cnt > 255)
428
    sctp_clog.x.cwnd.cnt_in_send = 255;
429
  else
430
    sctp_clog.x.cwnd.cnt_in_send = stcb->asoc.send_queue_cnt;
431
  if (stcb->asoc.stream_queue_cnt > 255)
432
    sctp_clog.x.cwnd.cnt_in_str = 255;
433
  else
434
    sctp_clog.x.cwnd.cnt_in_str = stcb->asoc.stream_queue_cnt;
435
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
436
       SCTP_LOG_EVENT_MAXBURST,
437
       from,
438
       sctp_clog.x.misc.log1,
439
       sctp_clog.x.misc.log2,
440
       sctp_clog.x.misc.log3,
441
       sctp_clog.x.misc.log4);
442
#endif
443
0
}
444
445
void
446
sctp_log_rwnd(uint8_t from, uint32_t peers_rwnd, uint32_t snd_size, uint32_t overhead)
447
0
{
448
#if defined(SCTP_LOCAL_TRACE_BUF)
449
  struct sctp_cwnd_log sctp_clog;
450
451
  sctp_clog.x.rwnd.rwnd = peers_rwnd;
452
  sctp_clog.x.rwnd.send_size = snd_size;
453
  sctp_clog.x.rwnd.overhead = overhead;
454
  sctp_clog.x.rwnd.new_rwnd = 0;
455
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
456
       SCTP_LOG_EVENT_RWND,
457
       from,
458
       sctp_clog.x.misc.log1,
459
       sctp_clog.x.misc.log2,
460
       sctp_clog.x.misc.log3,
461
       sctp_clog.x.misc.log4);
462
#endif
463
0
}
464
465
void
466
sctp_log_rwnd_set(uint8_t from, uint32_t peers_rwnd, uint32_t flight_size, uint32_t overhead, uint32_t a_rwndval)
467
0
{
468
#if defined(SCTP_LOCAL_TRACE_BUF)
469
  struct sctp_cwnd_log sctp_clog;
470
471
  sctp_clog.x.rwnd.rwnd = peers_rwnd;
472
  sctp_clog.x.rwnd.send_size = flight_size;
473
  sctp_clog.x.rwnd.overhead = overhead;
474
  sctp_clog.x.rwnd.new_rwnd = a_rwndval;
475
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
476
       SCTP_LOG_EVENT_RWND,
477
       from,
478
       sctp_clog.x.misc.log1,
479
       sctp_clog.x.misc.log2,
480
       sctp_clog.x.misc.log3,
481
       sctp_clog.x.misc.log4);
482
#endif
483
0
}
484
485
#ifdef SCTP_MBCNT_LOGGING
486
static void
487
sctp_log_mbcnt(uint8_t from, uint32_t total_oq, uint32_t book, uint32_t total_mbcnt_q, uint32_t mbcnt)
488
{
489
#if defined(SCTP_LOCAL_TRACE_BUF)
490
  struct sctp_cwnd_log sctp_clog;
491
492
  sctp_clog.x.mbcnt.total_queue_size = total_oq;
493
  sctp_clog.x.mbcnt.size_change = book;
494
  sctp_clog.x.mbcnt.total_queue_mb_size = total_mbcnt_q;
495
  sctp_clog.x.mbcnt.mbcnt_change = mbcnt;
496
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
497
       SCTP_LOG_EVENT_MBCNT,
498
       from,
499
       sctp_clog.x.misc.log1,
500
       sctp_clog.x.misc.log2,
501
       sctp_clog.x.misc.log3,
502
       sctp_clog.x.misc.log4);
503
#endif
504
}
505
#endif
506
507
void
508
sctp_misc_ints(uint8_t from, uint32_t a, uint32_t b, uint32_t c, uint32_t d)
509
0
{
510
#if defined(SCTP_LOCAL_TRACE_BUF)
511
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
512
       SCTP_LOG_MISC_EVENT,
513
       from,
514
       a, b, c, d);
515
#endif
516
0
}
517
518
void
519
sctp_wakeup_log(struct sctp_tcb *stcb, uint32_t wake_cnt, int from)
520
0
{
521
#if defined(SCTP_LOCAL_TRACE_BUF)
522
  struct sctp_cwnd_log sctp_clog;
523
524
  sctp_clog.x.wake.stcb = (void *)stcb;
525
  sctp_clog.x.wake.wake_cnt = wake_cnt;
526
  sctp_clog.x.wake.flight = stcb->asoc.total_flight_count;
527
  sctp_clog.x.wake.send_q = stcb->asoc.send_queue_cnt;
528
  sctp_clog.x.wake.sent_q = stcb->asoc.sent_queue_cnt;
529
530
  if (stcb->asoc.stream_queue_cnt < 0xff)
531
    sctp_clog.x.wake.stream_qcnt = (uint8_t) stcb->asoc.stream_queue_cnt;
532
  else
533
    sctp_clog.x.wake.stream_qcnt = 0xff;
534
535
  if (stcb->asoc.chunks_on_out_queue < 0xff)
536
    sctp_clog.x.wake.chunks_on_oque = (uint8_t) stcb->asoc.chunks_on_out_queue;
537
  else
538
    sctp_clog.x.wake.chunks_on_oque = 0xff;
539
540
  sctp_clog.x.wake.sctpflags = 0;
541
  /* set in the defered mode stuff */
542
  if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_DONT_WAKE)
543
    sctp_clog.x.wake.sctpflags |= 1;
544
  if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEOUTPUT)
545
    sctp_clog.x.wake.sctpflags |= 2;
546
  if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_WAKEINPUT)
547
    sctp_clog.x.wake.sctpflags |= 4;
548
  /* what about the sb */
549
  if (stcb->sctp_socket) {
550
    struct socket *so = stcb->sctp_socket;
551
552
    sctp_clog.x.wake.sbflags = (uint8_t)((so->so_snd.sb_flags & 0x00ff));
553
  } else {
554
    sctp_clog.x.wake.sbflags = 0xff;
555
  }
556
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
557
       SCTP_LOG_EVENT_WAKE,
558
       from,
559
       sctp_clog.x.misc.log1,
560
       sctp_clog.x.misc.log2,
561
       sctp_clog.x.misc.log3,
562
       sctp_clog.x.misc.log4);
563
#endif
564
0
}
565
566
void
567
sctp_log_block(uint8_t from, struct sctp_association *asoc, ssize_t sendlen)
568
0
{
569
#if defined(SCTP_LOCAL_TRACE_BUF)
570
  struct sctp_cwnd_log sctp_clog;
571
572
  sctp_clog.x.blk.onsb = asoc->total_output_queue_size;
573
  sctp_clog.x.blk.send_sent_qcnt = (uint16_t) (asoc->send_queue_cnt + asoc->sent_queue_cnt);
574
  sctp_clog.x.blk.peer_rwnd = asoc->peers_rwnd;
575
  sctp_clog.x.blk.stream_qcnt = (uint16_t) asoc->stream_queue_cnt;
576
  sctp_clog.x.blk.chunks_on_oque = (uint16_t) asoc->chunks_on_out_queue;
577
  sctp_clog.x.blk.flight_size = (uint16_t) (asoc->total_flight/1024);
578
  sctp_clog.x.blk.sndlen = (uint32_t)sendlen;
579
  SCTP_CTR6(KTR_SCTP, "SCTP:%d[%d]:%x-%x-%x-%x",
580
       SCTP_LOG_EVENT_BLOCK,
581
       from,
582
       sctp_clog.x.misc.log1,
583
       sctp_clog.x.misc.log2,
584
       sctp_clog.x.misc.log3,
585
       sctp_clog.x.misc.log4);
586
#endif
587
0
}
588
589
int
590
sctp_fill_stat_log(void *optval SCTP_UNUSED, size_t *optsize SCTP_UNUSED)
591
0
{
592
  /* May need to fix this if ktrdump does not work */
593
0
  return (0);
594
0
}
595
596
#ifdef SCTP_AUDITING_ENABLED
597
uint8_t sctp_audit_data[SCTP_AUDIT_SIZE][2];
598
static int sctp_audit_indx = 0;
599
600
static
601
void
602
sctp_print_audit_report(void)
603
{
604
  int i;
605
  int cnt;
606
607
  cnt = 0;
608
  for (i = sctp_audit_indx; i < SCTP_AUDIT_SIZE; i++) {
609
    if ((sctp_audit_data[i][0] == 0xe0) &&
610
        (sctp_audit_data[i][1] == 0x01)) {
611
      cnt = 0;
612
      SCTP_PRINTF("\n");
613
    } else if (sctp_audit_data[i][0] == 0xf0) {
614
      cnt = 0;
615
      SCTP_PRINTF("\n");
616
    } else if ((sctp_audit_data[i][0] == 0xc0) &&
617
        (sctp_audit_data[i][1] == 0x01)) {
618
      SCTP_PRINTF("\n");
619
      cnt = 0;
620
    }
621
    SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
622
          (uint32_t) sctp_audit_data[i][1]);
623
    cnt++;
624
    if ((cnt % 14) == 0)
625
      SCTP_PRINTF("\n");
626
  }
627
  for (i = 0; i < sctp_audit_indx; i++) {
628
    if ((sctp_audit_data[i][0] == 0xe0) &&
629
        (sctp_audit_data[i][1] == 0x01)) {
630
      cnt = 0;
631
      SCTP_PRINTF("\n");
632
    } else if (sctp_audit_data[i][0] == 0xf0) {
633
      cnt = 0;
634
      SCTP_PRINTF("\n");
635
    } else if ((sctp_audit_data[i][0] == 0xc0) &&
636
        (sctp_audit_data[i][1] == 0x01)) {
637
      SCTP_PRINTF("\n");
638
      cnt = 0;
639
    }
640
    SCTP_PRINTF("%2.2x%2.2x ", (uint32_t) sctp_audit_data[i][0],
641
          (uint32_t) sctp_audit_data[i][1]);
642
    cnt++;
643
    if ((cnt % 14) == 0)
644
      SCTP_PRINTF("\n");
645
  }
646
  SCTP_PRINTF("\n");
647
}
648
649
void
650
sctp_auditing(int from, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
651
    struct sctp_nets *net)
652
{
653
  int resend_cnt, tot_out, rep, tot_book_cnt;
654
  struct sctp_nets *lnet;
655
  struct sctp_tmit_chunk *chk;
656
657
  sctp_audit_data[sctp_audit_indx][0] = 0xAA;
658
  sctp_audit_data[sctp_audit_indx][1] = 0x000000ff & from;
659
  sctp_audit_indx++;
660
  if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
661
    sctp_audit_indx = 0;
662
  }
663
  if (inp == NULL) {
664
    sctp_audit_data[sctp_audit_indx][0] = 0xAF;
665
    sctp_audit_data[sctp_audit_indx][1] = 0x01;
666
    sctp_audit_indx++;
667
    if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
668
      sctp_audit_indx = 0;
669
    }
670
    return;
671
  }
672
  if (stcb == NULL) {
673
    sctp_audit_data[sctp_audit_indx][0] = 0xAF;
674
    sctp_audit_data[sctp_audit_indx][1] = 0x02;
675
    sctp_audit_indx++;
676
    if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
677
      sctp_audit_indx = 0;
678
    }
679
    return;
680
  }
681
  sctp_audit_data[sctp_audit_indx][0] = 0xA1;
682
  sctp_audit_data[sctp_audit_indx][1] =
683
      (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
684
  sctp_audit_indx++;
685
  if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
686
    sctp_audit_indx = 0;
687
  }
688
  rep = 0;
689
  tot_book_cnt = 0;
690
  resend_cnt = tot_out = 0;
691
  TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
692
    if (chk->sent == SCTP_DATAGRAM_RESEND) {
693
      resend_cnt++;
694
    } else if (chk->sent < SCTP_DATAGRAM_RESEND) {
695
      tot_out += chk->book_size;
696
      tot_book_cnt++;
697
    }
698
  }
699
  if (resend_cnt != stcb->asoc.sent_queue_retran_cnt) {
700
    sctp_audit_data[sctp_audit_indx][0] = 0xAF;
701
    sctp_audit_data[sctp_audit_indx][1] = 0xA1;
702
    sctp_audit_indx++;
703
    if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
704
      sctp_audit_indx = 0;
705
    }
706
    SCTP_PRINTF("resend_cnt:%d asoc-tot:%d\n",
707
          resend_cnt, stcb->asoc.sent_queue_retran_cnt);
708
    rep = 1;
709
    stcb->asoc.sent_queue_retran_cnt = resend_cnt;
710
    sctp_audit_data[sctp_audit_indx][0] = 0xA2;
711
    sctp_audit_data[sctp_audit_indx][1] =
712
        (0x000000ff & stcb->asoc.sent_queue_retran_cnt);
713
    sctp_audit_indx++;
714
    if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
715
      sctp_audit_indx = 0;
716
    }
717
  }
718
  if (tot_out != stcb->asoc.total_flight) {
719
    sctp_audit_data[sctp_audit_indx][0] = 0xAF;
720
    sctp_audit_data[sctp_audit_indx][1] = 0xA2;
721
    sctp_audit_indx++;
722
    if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
723
      sctp_audit_indx = 0;
724
    }
725
    rep = 1;
726
    SCTP_PRINTF("tot_flt:%d asoc_tot:%d\n", tot_out,
727
          (int)stcb->asoc.total_flight);
728
    stcb->asoc.total_flight = tot_out;
729
  }
730
  if (tot_book_cnt != stcb->asoc.total_flight_count) {
731
    sctp_audit_data[sctp_audit_indx][0] = 0xAF;
732
    sctp_audit_data[sctp_audit_indx][1] = 0xA5;
733
    sctp_audit_indx++;
734
    if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
735
      sctp_audit_indx = 0;
736
    }
737
    rep = 1;
738
    SCTP_PRINTF("tot_flt_book:%d\n", tot_book_cnt);
739
740
    stcb->asoc.total_flight_count = tot_book_cnt;
741
  }
742
  tot_out = 0;
743
  TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
744
    tot_out += lnet->flight_size;
745
  }
746
  if (tot_out != stcb->asoc.total_flight) {
747
    sctp_audit_data[sctp_audit_indx][0] = 0xAF;
748
    sctp_audit_data[sctp_audit_indx][1] = 0xA3;
749
    sctp_audit_indx++;
750
    if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
751
      sctp_audit_indx = 0;
752
    }
753
    rep = 1;
754
    SCTP_PRINTF("real flight:%d net total was %d\n",
755
          stcb->asoc.total_flight, tot_out);
756
    /* now corrective action */
757
    TAILQ_FOREACH(lnet, &stcb->asoc.nets, sctp_next) {
758
      tot_out = 0;
759
      TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
760
        if ((chk->whoTo == lnet) &&
761
            (chk->sent < SCTP_DATAGRAM_RESEND)) {
762
          tot_out += chk->book_size;
763
        }
764
      }
765
      if (lnet->flight_size != tot_out) {
766
        SCTP_PRINTF("net:%p flight was %d corrected to %d\n",
767
              (void *)lnet, lnet->flight_size,
768
              tot_out);
769
        lnet->flight_size = tot_out;
770
      }
771
    }
772
  }
773
  if (rep) {
774
    sctp_print_audit_report();
775
  }
776
}
777
778
void
779
sctp_audit_log(uint8_t ev, uint8_t fd)
780
{
781
782
  sctp_audit_data[sctp_audit_indx][0] = ev;
783
  sctp_audit_data[sctp_audit_indx][1] = fd;
784
  sctp_audit_indx++;
785
  if (sctp_audit_indx >= SCTP_AUDIT_SIZE) {
786
    sctp_audit_indx = 0;
787
  }
788
}
789
790
#endif
791
792
/*
793
 * The conversion from time to ticks and vice versa is done by rounding
794
 * upwards. This way we can test in the code the time to be positive and
795
 * know that this corresponds to a positive number of ticks.
796
 */
797
798
uint32_t
799
sctp_msecs_to_ticks(uint32_t msecs)
800
116k
{
801
116k
  uint64_t temp;
802
116k
  uint32_t ticks;
803
804
116k
  if (hz == 1000) {
805
116k
    ticks = msecs;
806
116k
  } else {
807
1
    temp = (((uint64_t)msecs * hz) + 999) / 1000;
808
1
    if (temp > UINT32_MAX) {
809
0
      ticks = UINT32_MAX;
810
1
    } else {
811
1
      ticks = (uint32_t)temp;
812
1
    }
813
1
  }
814
116k
  return (ticks);
815
116k
}
816
817
uint32_t
818
sctp_ticks_to_msecs(uint32_t ticks)
819
21.0k
{
820
21.0k
  uint64_t temp;
821
21.0k
  uint32_t msecs;
822
823
21.0k
  if (hz == 1000) {
824
21.0k
    msecs = ticks;
825
21.0k
  } else {
826
0
    temp = (((uint64_t)ticks * 1000) + (hz - 1)) / hz;
827
0
    if (temp > UINT32_MAX) {
828
0
      msecs = UINT32_MAX;
829
0
    } else {
830
0
      msecs = (uint32_t)temp;
831
0
    }
832
0
  }
833
21.0k
  return (msecs);
834
21.0k
}
835
836
uint32_t
837
sctp_secs_to_ticks(uint32_t secs)
838
52.5k
{
839
52.5k
  uint64_t temp;
840
52.5k
  uint32_t ticks;
841
842
52.5k
  temp = (uint64_t)secs * hz;
843
52.5k
  if (temp > UINT32_MAX) {
844
0
    ticks = UINT32_MAX;
845
52.5k
  } else {
846
52.5k
    ticks = (uint32_t)temp;
847
52.5k
  }
848
52.5k
  return (ticks);
849
52.5k
}
850
851
uint32_t
852
sctp_ticks_to_secs(uint32_t ticks)
853
0
{
854
0
  uint64_t temp;
855
0
  uint32_t secs;
856
857
0
  temp = ((uint64_t)ticks + (hz - 1)) / hz;
858
0
  if (temp > UINT32_MAX) {
859
0
    secs = UINT32_MAX;
860
0
  } else {
861
0
    secs = (uint32_t)temp;
862
0
  }
863
0
  return (secs);
864
0
}
865
866
/*
867
 * sctp_stop_timers_for_shutdown() should be called
868
 * when entering the SHUTDOWN_SENT or SHUTDOWN_ACK_SENT
869
 * state to make sure that all timers are stopped.
870
 */
871
void
872
sctp_stop_timers_for_shutdown(struct sctp_tcb *stcb)
873
37
{
874
37
  struct sctp_inpcb *inp;
875
37
  struct sctp_nets *net;
876
877
37
  inp = stcb->sctp_ep;
878
879
37
  sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
880
37
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_12);
881
37
  sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
882
37
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_13);
883
37
  sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
884
37
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_14);
885
37
  sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
886
37
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_15);
887
140
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
888
140
    sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
889
140
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_16);
890
140
    sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
891
140
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_17);
892
140
  }
893
37
}
894
895
void
896
sctp_stop_association_timers(struct sctp_tcb *stcb, bool stop_assoc_kill_timer)
897
21.0k
{
898
21.0k
  struct sctp_inpcb *inp;
899
21.0k
  struct sctp_nets *net;
900
901
21.0k
  inp = stcb->sctp_ep;
902
21.0k
  sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
903
21.0k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_18);
904
21.0k
  sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
905
21.0k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_19);
906
21.0k
  if (stop_assoc_kill_timer) {
907
10.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
908
10.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_20);
909
10.5k
  }
910
21.0k
  sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
911
21.0k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_21);
912
21.0k
  sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
913
21.0k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_22);
914
21.0k
  sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNGUARD, inp, stcb, NULL,
915
21.0k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_23);
916
  /* Mobility adaptation */
917
21.0k
  sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, inp, stcb, NULL,
918
21.0k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_24);
919
29.5k
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
920
29.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
921
29.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_25);
922
29.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
923
29.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_26);
924
29.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, net,
925
29.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_27);
926
29.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net,
927
29.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_28);
928
29.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, net,
929
29.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_29);
930
29.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
931
29.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_30);
932
29.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
933
29.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_31);
934
29.5k
  }
935
21.0k
}
936
937
/*
938
 * A list of sizes based on typical mtu's, used only if next hop size not
939
 * returned. These values MUST be multiples of 4 and MUST be ordered.
940
 */
941
static uint32_t sctp_mtu_sizes[] = {
942
  68,
943
  296,
944
  508,
945
  512,
946
  544,
947
  576,
948
  1004,
949
  1492,
950
  1500,
951
  1536,
952
  2000,
953
  2048,
954
  4352,
955
  4464,
956
  8168,
957
  17912,
958
  32000,
959
  65532
960
};
961
962
/*
963
 * Return the largest MTU in sctp_mtu_sizes smaller than val.
964
 * If val is smaller than the minimum, just return the largest
965
 * multiple of 4 smaller or equal to val.
966
 * Ensure that the result is a multiple of 4.
967
 */
968
uint32_t
969
sctp_get_prev_mtu(uint32_t val)
970
0
{
971
0
  uint32_t i;
972
973
0
  val &= 0xfffffffc;
974
0
  if (val <= sctp_mtu_sizes[0]) {
975
0
    return (val);
976
0
  }
977
0
  for (i = 1; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
978
0
    if (val <= sctp_mtu_sizes[i]) {
979
0
      break;
980
0
    }
981
0
  }
982
0
  KASSERT((sctp_mtu_sizes[i - 1] & 0x00000003) == 0,
983
0
          ("sctp_mtu_sizes[%u] not a multiple of 4", i - 1));
984
0
  return (sctp_mtu_sizes[i - 1]);
985
0
}
986
987
/*
988
 * Return the smallest MTU in sctp_mtu_sizes larger than val.
989
 * If val is larger than the maximum, just return the largest multiple of 4 smaller
990
 * or equal to val.
991
 * Ensure that the result is a multiple of 4.
992
 */
993
uint32_t
994
sctp_get_next_mtu(uint32_t val)
995
0
{
996
  /* select another MTU that is just bigger than this one */
997
0
  uint32_t i;
998
999
0
  val &= 0xfffffffc;
1000
0
  for (i = 0; i < (sizeof(sctp_mtu_sizes) / sizeof(uint32_t)); i++) {
1001
0
    if (val < sctp_mtu_sizes[i]) {
1002
0
      KASSERT((sctp_mtu_sizes[i] & 0x00000003) == 0,
1003
0
        ("sctp_mtu_sizes[%u] not a multiple of 4", i));
1004
0
      return (sctp_mtu_sizes[i]);
1005
0
    }
1006
0
  }
1007
0
  return (val);
1008
0
}
1009
1010
void
1011
sctp_fill_random_store(struct sctp_pcb *m)
1012
36.2k
{
1013
  /*
1014
   * Here we use the MD5/SHA-1 to hash with our good randomNumbers and
1015
   * our counter. The result becomes our good random numbers and we
1016
   * then setup to give these out. Note that we do no locking to
1017
   * protect this. This is ok, since if competing folks call this we
1018
   * will get more gobbled gook in the random store which is what we
1019
   * want. There is a danger that two guys will use the same random
1020
   * numbers, but thats ok too since that is random as well :->
1021
   */
1022
36.2k
  m->store_at = 0;
1023
36.2k
#if defined(__Userspace__) && defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
1024
762k
  for (int i = 0; i < (int) (sizeof(m->random_store) / sizeof(m->random_store[0])); i++) {
1025
725k
    m->random_store[i] = (uint8_t) rand();
1026
725k
  }
1027
#else
1028
  (void)sctp_hmac(SCTP_HMAC, (uint8_t *)m->random_numbers,
1029
      sizeof(m->random_numbers), (uint8_t *)&m->random_counter,
1030
      sizeof(m->random_counter), (uint8_t *)m->random_store);
1031
#endif
1032
36.2k
  m->random_counter++;
1033
36.2k
}
1034
1035
uint32_t
1036
sctp_select_initial_TSN(struct sctp_pcb *inp)
1037
159k
{
1038
  /*
1039
   * A true implementation should use random selection process to get
1040
   * the initial stream sequence number, using RFC1750 as a good
1041
   * guideline
1042
   */
1043
159k
  uint32_t x, *xp;
1044
159k
  uint8_t *p;
1045
159k
  int store_at, new_store;
1046
1047
159k
  if (inp->initial_sequence_debug != 0) {
1048
0
    uint32_t ret;
1049
1050
0
    ret = inp->initial_sequence_debug;
1051
0
    inp->initial_sequence_debug++;
1052
0
    return (ret);
1053
0
  }
1054
159k
 retry:
1055
159k
  store_at = inp->store_at;
1056
159k
  new_store = store_at + sizeof(uint32_t);
1057
159k
  if (new_store >= (SCTP_SIGNATURE_SIZE-3)) {
1058
25.7k
    new_store = 0;
1059
25.7k
  }
1060
159k
  if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
1061
0
    goto retry;
1062
0
  }
1063
159k
  if (new_store == 0) {
1064
    /* Refill the random store */
1065
25.7k
    sctp_fill_random_store(inp);
1066
25.7k
  }
1067
159k
  p = &inp->random_store[store_at];
1068
159k
  xp = (uint32_t *)p;
1069
159k
  x = *xp;
1070
159k
  return (x);
1071
159k
}
1072
1073
uint32_t
1074
sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
1075
47.9k
{
1076
47.9k
  uint32_t x;
1077
47.9k
  struct timeval now;
1078
1079
47.9k
  if (check) {
1080
26.9k
    (void)SCTP_GETTIME_TIMEVAL(&now);
1081
26.9k
  }
1082
47.9k
  for (;;) {
1083
47.9k
    x = sctp_select_initial_TSN(&inp->sctp_ep);
1084
47.9k
    if (x == 0) {
1085
      /* we never use 0 */
1086
0
      continue;
1087
0
    }
1088
47.9k
    if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
1089
47.9k
      break;
1090
47.9k
    }
1091
47.9k
  }
1092
47.9k
  return (x);
1093
47.9k
}
1094
1095
int32_t
1096
sctp_map_assoc_state(int kernel_state)
1097
0
{
1098
0
  int32_t user_state;
1099
1100
0
  if (kernel_state & SCTP_STATE_WAS_ABORTED) {
1101
0
    user_state = SCTP_CLOSED;
1102
0
  } else if (kernel_state & SCTP_STATE_SHUTDOWN_PENDING) {
1103
0
    user_state = SCTP_SHUTDOWN_PENDING;
1104
0
  } else {
1105
0
    switch (kernel_state & SCTP_STATE_MASK) {
1106
0
    case SCTP_STATE_EMPTY:
1107
0
      user_state = SCTP_CLOSED;
1108
0
      break;
1109
0
    case SCTP_STATE_INUSE:
1110
0
      user_state = SCTP_CLOSED;
1111
0
      break;
1112
0
    case SCTP_STATE_COOKIE_WAIT:
1113
0
      user_state = SCTP_COOKIE_WAIT;
1114
0
      break;
1115
0
    case SCTP_STATE_COOKIE_ECHOED:
1116
0
      user_state = SCTP_COOKIE_ECHOED;
1117
0
      break;
1118
0
    case SCTP_STATE_OPEN:
1119
0
      user_state = SCTP_ESTABLISHED;
1120
0
      break;
1121
0
    case SCTP_STATE_SHUTDOWN_SENT:
1122
0
      user_state = SCTP_SHUTDOWN_SENT;
1123
0
      break;
1124
0
    case SCTP_STATE_SHUTDOWN_RECEIVED:
1125
0
      user_state = SCTP_SHUTDOWN_RECEIVED;
1126
0
      break;
1127
0
    case SCTP_STATE_SHUTDOWN_ACK_SENT:
1128
0
      user_state = SCTP_SHUTDOWN_ACK_SENT;
1129
0
      break;
1130
0
    default:
1131
0
      user_state = SCTP_CLOSED;
1132
0
      break;
1133
0
    }
1134
0
  }
1135
0
  return (user_state);
1136
0
}
1137
1138
int
1139
sctp_init_asoc(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
1140
               uint32_t override_tag, uint32_t initial_tsn, uint32_t vrf_id,
1141
               uint16_t o_strms)
1142
10.5k
{
1143
10.5k
  struct sctp_association *asoc;
1144
  /*
1145
   * Anything set to zero is taken care of by the allocation routine's
1146
   * bzero
1147
   */
1148
1149
  /*
1150
   * Up front select what scoping to apply on addresses I tell my peer
1151
   * Not sure what to do with these right now, we will need to come up
1152
   * with a way to set them. We may need to pass them through from the
1153
   * caller in the sctp_aloc_assoc() function.
1154
   */
1155
10.5k
  int i;
1156
#if defined(SCTP_DETAILED_STR_STATS)
1157
  int j;
1158
#endif
1159
1160
10.5k
  asoc = &stcb->asoc;
1161
  /* init all variables to a known value. */
1162
10.5k
  SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
1163
10.5k
  asoc->max_burst = inp->sctp_ep.max_burst;
1164
10.5k
  asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
1165
10.5k
  asoc->heart_beat_delay = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1166
10.5k
  asoc->cookie_life = inp->sctp_ep.def_cookie_life;
1167
10.5k
  asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
1168
10.5k
  asoc->ecn_supported = inp->ecn_supported;
1169
10.5k
  asoc->prsctp_supported = inp->prsctp_supported;
1170
10.5k
  asoc->auth_supported = inp->auth_supported;
1171
10.5k
  asoc->asconf_supported = inp->asconf_supported;
1172
10.5k
  asoc->reconfig_supported = inp->reconfig_supported;
1173
10.5k
  asoc->nrsack_supported = inp->nrsack_supported;
1174
10.5k
  asoc->pktdrop_supported = inp->pktdrop_supported;
1175
10.5k
  asoc->idata_supported = inp->idata_supported;
1176
10.5k
  asoc->rcv_edmid = inp->rcv_edmid;
1177
10.5k
  asoc->snd_edmid = SCTP_EDMID_NONE;
1178
10.5k
  asoc->sctp_cmt_pf = (uint8_t)0;
1179
10.5k
  asoc->sctp_frag_point = inp->sctp_frag_point;
1180
10.5k
  asoc->sctp_features = inp->sctp_features;
1181
10.5k
  asoc->default_dscp = inp->sctp_ep.default_dscp;
1182
10.5k
  asoc->max_cwnd = inp->max_cwnd;
1183
10.5k
#ifdef INET6
1184
10.5k
  if (inp->sctp_ep.default_flowlabel) {
1185
0
    asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
1186
10.5k
  } else {
1187
10.5k
    if (inp->ip_inp.inp.inp_flags & IN6P_AUTOFLOWLABEL) {
1188
0
      asoc->default_flowlabel = sctp_select_initial_TSN(&inp->sctp_ep);
1189
0
      asoc->default_flowlabel &= 0x000fffff;
1190
0
      asoc->default_flowlabel |= 0x80000000;
1191
10.5k
    } else {
1192
10.5k
      asoc->default_flowlabel = 0;
1193
10.5k
    }
1194
10.5k
  }
1195
10.5k
#endif
1196
10.5k
  asoc->sb_send_resv = 0;
1197
10.5k
  if (override_tag) {
1198
0
    asoc->my_vtag = override_tag;
1199
10.5k
  } else {
1200
10.5k
    asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport,  1);
1201
10.5k
  }
1202
  /* Get the nonce tags */
1203
10.5k
  asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1204
10.5k
  asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1205
10.5k
  asoc->vrf_id = vrf_id;
1206
1207
#ifdef SCTP_ASOCLOG_OF_TSNS
1208
  asoc->tsn_in_at = 0;
1209
  asoc->tsn_out_at = 0;
1210
  asoc->tsn_in_wrapped = 0;
1211
  asoc->tsn_out_wrapped = 0;
1212
  asoc->cumack_log_at = 0;
1213
  asoc->cumack_log_atsnt = 0;
1214
#endif
1215
#ifdef SCTP_FS_SPEC_LOG
1216
  asoc->fs_index = 0;
1217
#endif
1218
10.5k
  asoc->refcnt = 0;
1219
10.5k
  asoc->assoc_up_sent = 0;
1220
10.5k
  if (override_tag) {
1221
0
    asoc->init_seq_number = initial_tsn;
1222
10.5k
  } else {
1223
10.5k
    asoc->init_seq_number = sctp_select_initial_TSN(&inp->sctp_ep);
1224
10.5k
  }
1225
10.5k
  asoc->asconf_seq_out = asoc->init_seq_number;
1226
10.5k
  asoc->str_reset_seq_out = asoc->init_seq_number;
1227
10.5k
  asoc->sending_seq = asoc->init_seq_number;
1228
10.5k
  asoc->asconf_seq_out_acked = asoc->init_seq_number - 1;
1229
  /* we are optimistic here */
1230
10.5k
  asoc->peer_supports_nat = 0;
1231
10.5k
  asoc->sent_queue_retran_cnt = 0;
1232
1233
  /* for CMT */
1234
10.5k
  asoc->last_net_cmt_send_started = NULL;
1235
1236
10.5k
  asoc->last_acked_seq = asoc->init_seq_number - 1;
1237
10.5k
  asoc->advanced_peer_ack_point = asoc->init_seq_number - 1;
1238
10.5k
  asoc->asconf_seq_in = asoc->init_seq_number - 1;
1239
1240
  /* here we are different, we hold the next one we expect */
1241
10.5k
  asoc->str_reset_seq_in = asoc->init_seq_number;
1242
1243
10.5k
  asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1244
10.5k
  asoc->initial_rto = inp->sctp_ep.initial_rto;
1245
1246
10.5k
  asoc->default_mtu = inp->sctp_ep.default_mtu;
1247
10.5k
  asoc->max_init_times = inp->sctp_ep.max_init_times;
1248
10.5k
  asoc->max_send_times = inp->sctp_ep.max_send_times;
1249
10.5k
  asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1250
10.5k
  asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1251
10.5k
  asoc->free_chunk_cnt = 0;
1252
1253
10.5k
  asoc->iam_blocking = 0;
1254
10.5k
  asoc->context = inp->sctp_context;
1255
10.5k
  asoc->local_strreset_support = inp->local_strreset_support;
1256
10.5k
  asoc->def_send = inp->def_send;
1257
10.5k
  asoc->delayed_ack = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1258
10.5k
  asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1259
10.5k
  asoc->pr_sctp_cnt = 0;
1260
10.5k
  asoc->total_output_queue_size = 0;
1261
1262
10.5k
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
1263
0
    asoc->scope.ipv6_addr_legal = 1;
1264
0
    if (SCTP_IPV6_V6ONLY(inp) == 0) {
1265
0
      asoc->scope.ipv4_addr_legal = 1;
1266
0
    } else {
1267
0
      asoc->scope.ipv4_addr_legal = 0;
1268
0
    }
1269
0
#if defined(__Userspace__)
1270
0
      asoc->scope.conn_addr_legal = 0;
1271
0
#endif
1272
10.5k
  } else {
1273
10.5k
    asoc->scope.ipv6_addr_legal = 0;
1274
10.5k
#if defined(__Userspace__)
1275
10.5k
    if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
1276
10.5k
      asoc->scope.conn_addr_legal = 1;
1277
10.5k
      asoc->scope.ipv4_addr_legal = 0;
1278
10.5k
    } else {
1279
0
      asoc->scope.conn_addr_legal = 0;
1280
0
      asoc->scope.ipv4_addr_legal = 1;
1281
0
    }
1282
#else
1283
    asoc->scope.ipv4_addr_legal = 1;
1284
#endif
1285
10.5k
  }
1286
1287
10.5k
  asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1288
10.5k
  asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1289
1290
10.5k
  asoc->smallest_mtu = 0;
1291
10.5k
  asoc->minrto = inp->sctp_ep.sctp_minrto;
1292
10.5k
  asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1293
1294
10.5k
  asoc->stream_locked_on = 0;
1295
10.5k
  asoc->ecn_echo_cnt_onq = 0;
1296
10.5k
  asoc->stream_locked = 0;
1297
1298
10.5k
  asoc->send_sack = 1;
1299
1300
10.5k
  LIST_INIT(&asoc->sctp_restricted_addrs);
1301
1302
10.5k
  TAILQ_INIT(&asoc->nets);
1303
10.5k
  TAILQ_INIT(&asoc->pending_reply_queue);
1304
10.5k
  TAILQ_INIT(&asoc->asconf_ack_sent);
1305
  /* Setup to fill the hb random cache at first HB */
1306
10.5k
  asoc->hb_random_idx = 4;
1307
1308
10.5k
  asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1309
1310
10.5k
  stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1311
10.5k
  stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1312
1313
10.5k
  stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1314
10.5k
  stcb->asoc.ss_functions = sctp_ss_functions[inp->sctp_ep.sctp_default_ss_module];
1315
1316
  /*
1317
   * Now the stream parameters, here we allocate space for all streams
1318
   * that we request by default.
1319
   */
1320
10.5k
  asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1321
10.5k
      o_strms;
1322
10.5k
  SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1323
10.5k
        asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1324
10.5k
        SCTP_M_STRMO);
1325
10.5k
  if (asoc->strmout == NULL) {
1326
    /* big trouble no memory */
1327
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1328
0
    return (ENOMEM);
1329
0
  }
1330
10.5k
  SCTP_TCB_LOCK(stcb);
1331
2.71M
  for (i = 0; i < asoc->streamoutcnt; i++) {
1332
    /*
1333
     * inbound side must be set to 0xffff, also NOTE when we get
1334
     * the INIT-ACK back (for INIT sender) we MUST reduce the
1335
     * count (streamoutcnt) but first check if we sent to any of
1336
     * the upper streams that were dropped (if some were). Those
1337
     * that were dropped must be notified to the upper layer as
1338
     * failed to send.
1339
     */
1340
2.69M
    TAILQ_INIT(&asoc->strmout[i].outqueue);
1341
2.69M
    asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1342
2.69M
    asoc->strmout[i].chunks_on_queues = 0;
1343
#if defined(SCTP_DETAILED_STR_STATS)
1344
    for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
1345
      asoc->strmout[i].abandoned_sent[j] = 0;
1346
      asoc->strmout[i].abandoned_unsent[j] = 0;
1347
    }
1348
#else
1349
2.69M
    asoc->strmout[i].abandoned_sent[0] = 0;
1350
2.69M
    asoc->strmout[i].abandoned_unsent[0] = 0;
1351
2.69M
#endif
1352
2.69M
    asoc->strmout[i].next_mid_ordered = 0;
1353
2.69M
    asoc->strmout[i].next_mid_unordered = 0;
1354
2.69M
    asoc->strmout[i].sid = i;
1355
2.69M
    asoc->strmout[i].last_msg_incomplete = 0;
1356
2.69M
    asoc->strmout[i].state = SCTP_STREAM_OPENING;
1357
2.69M
  }
1358
10.5k
  asoc->ss_functions.sctp_ss_init(stcb, asoc);
1359
10.5k
  SCTP_TCB_UNLOCK(stcb);
1360
1361
  /* Now the mapping array */
1362
10.5k
  asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1363
10.5k
  SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1364
10.5k
        SCTP_M_MAP);
1365
10.5k
  if (asoc->mapping_array == NULL) {
1366
0
    SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1367
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1368
0
    return (ENOMEM);
1369
0
  }
1370
10.5k
  memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1371
10.5k
  SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1372
10.5k
      SCTP_M_MAP);
1373
10.5k
  if (asoc->nr_mapping_array == NULL) {
1374
0
    SCTP_FREE(asoc->strmout, SCTP_M_STRMO);
1375
0
    SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1376
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
1377
0
    return (ENOMEM);
1378
0
  }
1379
10.5k
  memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1380
1381
  /* Now the init of the other outqueues */
1382
10.5k
  TAILQ_INIT(&asoc->free_chunks);
1383
10.5k
  TAILQ_INIT(&asoc->control_send_queue);
1384
10.5k
  TAILQ_INIT(&asoc->asconf_send_queue);
1385
10.5k
  TAILQ_INIT(&asoc->send_queue);
1386
10.5k
  TAILQ_INIT(&asoc->sent_queue);
1387
10.5k
  TAILQ_INIT(&asoc->resetHead);
1388
10.5k
  asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1389
10.5k
  TAILQ_INIT(&asoc->asconf_queue);
1390
  /* authentication fields */
1391
10.5k
  asoc->authinfo.random = NULL;
1392
10.5k
  asoc->authinfo.active_keyid = 0;
1393
10.5k
  asoc->authinfo.assoc_key = NULL;
1394
10.5k
  asoc->authinfo.assoc_keyid = 0;
1395
10.5k
  asoc->authinfo.recv_key = NULL;
1396
10.5k
  asoc->authinfo.recv_keyid = 0;
1397
10.5k
  LIST_INIT(&asoc->shared_keys);
1398
10.5k
  asoc->marked_retrans = 0;
1399
10.5k
  asoc->port = inp->sctp_ep.port;
1400
10.5k
  asoc->timoinit = 0;
1401
10.5k
  asoc->timodata = 0;
1402
10.5k
  asoc->timosack = 0;
1403
10.5k
  asoc->timoshutdown = 0;
1404
10.5k
  asoc->timoheartbeat = 0;
1405
10.5k
  asoc->timocookie = 0;
1406
10.5k
  asoc->timoshutdownack = 0;
1407
10.5k
  (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1408
10.5k
  asoc->discontinuity_time = asoc->start_time;
1409
52.5k
  for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1410
42.0k
    asoc->abandoned_unsent[i] = 0;
1411
42.0k
    asoc->abandoned_sent[i] = 0;
1412
42.0k
  }
1413
  /* sa_ignore MEMLEAK {memory is put in the assoc mapping array and freed later when
1414
   * the association is freed.
1415
   */
1416
10.5k
  return (0);
1417
10.5k
}
1418
1419
void
1420
sctp_print_mapping_array(struct sctp_association *asoc)
1421
0
{
1422
0
  unsigned int i, limit;
1423
1424
0
  SCTP_PRINTF("Mapping array size: %d, baseTSN: %8.8x, cumAck: %8.8x, highestTSN: (%8.8x, %8.8x).\n",
1425
0
              asoc->mapping_array_size,
1426
0
              asoc->mapping_array_base_tsn,
1427
0
              asoc->cumulative_tsn,
1428
0
              asoc->highest_tsn_inside_map,
1429
0
              asoc->highest_tsn_inside_nr_map);
1430
0
  for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1431
0
    if (asoc->mapping_array[limit - 1] != 0) {
1432
0
      break;
1433
0
    }
1434
0
  }
1435
0
  SCTP_PRINTF("Renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1436
0
  for (i = 0; i < limit; i++) {
1437
0
    SCTP_PRINTF("%2.2x%c", asoc->mapping_array[i], ((i + 1) % 16) ? ' ' : '\n');
1438
0
  }
1439
0
  if (limit % 16)
1440
0
    SCTP_PRINTF("\n");
1441
0
  for (limit = asoc->mapping_array_size; limit > 1; limit--) {
1442
0
    if (asoc->nr_mapping_array[limit - 1]) {
1443
0
      break;
1444
0
    }
1445
0
  }
1446
0
  SCTP_PRINTF("Non renegable mapping array (last %d entries are zero):\n", asoc->mapping_array_size - limit);
1447
0
  for (i = 0; i < limit; i++) {
1448
0
    SCTP_PRINTF("%2.2x%c", asoc->nr_mapping_array[i], ((i + 1) % 16) ? ' ': '\n');
1449
0
  }
1450
0
  if (limit % 16)
1451
0
    SCTP_PRINTF("\n");
1452
0
}
1453
1454
int
1455
sctp_expand_mapping_array(struct sctp_association *asoc, uint32_t needed)
1456
2.41k
{
1457
  /* mapping array needs to grow */
1458
2.41k
  uint8_t *new_array1, *new_array2;
1459
2.41k
  uint32_t new_size;
1460
1461
2.41k
  new_size = asoc->mapping_array_size + ((needed+7)/8 + SCTP_MAPPING_ARRAY_INCR);
1462
2.41k
  SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1463
2.41k
  SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1464
2.41k
  if ((new_array1 == NULL) || (new_array2 == NULL)) {
1465
    /* can't get more, forget it */
1466
0
    SCTP_PRINTF("No memory for expansion of SCTP mapping array %d\n", new_size);
1467
0
    if (new_array1) {
1468
0
      SCTP_FREE(new_array1, SCTP_M_MAP);
1469
0
    }
1470
0
    if (new_array2) {
1471
0
      SCTP_FREE(new_array2, SCTP_M_MAP);
1472
0
    }
1473
0
    return (-1);
1474
0
  }
1475
2.41k
  memset(new_array1, 0, new_size);
1476
2.41k
  memset(new_array2, 0, new_size);
1477
2.41k
  memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1478
2.41k
  memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1479
2.41k
  SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1480
2.41k
  SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1481
2.41k
  asoc->mapping_array = new_array1;
1482
2.41k
  asoc->nr_mapping_array = new_array2;
1483
2.41k
  asoc->mapping_array_size = new_size;
1484
2.41k
  return (0);
1485
2.41k
}
1486
1487
static void
1488
sctp_iterator_work(struct sctp_iterator *it)
1489
0
{
1490
#if defined(__FreeBSD__) && !defined(__Userspace__)
1491
  struct epoch_tracker et;
1492
#endif
1493
0
  struct sctp_inpcb *tinp;
1494
0
  int iteration_count = 0;
1495
0
  int inp_skip = 0;
1496
0
  int first_in = 1;
1497
1498
#if defined(__FreeBSD__) && !defined(__Userspace__)
1499
  NET_EPOCH_ENTER(et);
1500
#endif
1501
0
  SCTP_INP_INFO_RLOCK();
1502
0
  SCTP_ITERATOR_LOCK();
1503
0
  sctp_it_ctl.cur_it = it;
1504
0
  if (it->inp) {
1505
0
    SCTP_INP_RLOCK(it->inp);
1506
0
    SCTP_INP_DECR_REF(it->inp);
1507
0
  }
1508
0
  if (it->inp == NULL) {
1509
    /* iterator is complete */
1510
0
done_with_iterator:
1511
0
    sctp_it_ctl.cur_it = NULL;
1512
0
    SCTP_ITERATOR_UNLOCK();
1513
0
    SCTP_INP_INFO_RUNLOCK();
1514
0
    if (it->function_atend != NULL) {
1515
0
      (*it->function_atend) (it->pointer, it->val);
1516
0
    }
1517
0
    SCTP_FREE(it, SCTP_M_ITER);
1518
#if defined(__FreeBSD__) && !defined(__Userspace__)
1519
    NET_EPOCH_EXIT(et);
1520
#endif
1521
0
    return;
1522
0
  }
1523
0
select_a_new_ep:
1524
0
  if (first_in) {
1525
0
    first_in = 0;
1526
0
  } else {
1527
0
    SCTP_INP_RLOCK(it->inp);
1528
0
  }
1529
0
  while (((it->pcb_flags) &&
1530
0
          ((it->inp->sctp_flags & it->pcb_flags) != it->pcb_flags)) ||
1531
0
         ((it->pcb_features) &&
1532
0
    ((it->inp->sctp_features & it->pcb_features) != it->pcb_features))) {
1533
    /* endpoint flags or features don't match, so keep looking */
1534
0
    if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1535
0
      SCTP_INP_RUNLOCK(it->inp);
1536
0
      goto done_with_iterator;
1537
0
    }
1538
0
    tinp = it->inp;
1539
0
    it->inp = LIST_NEXT(it->inp, sctp_list);
1540
0
    it->stcb = NULL;
1541
0
    SCTP_INP_RUNLOCK(tinp);
1542
0
    if (it->inp == NULL) {
1543
0
      goto done_with_iterator;
1544
0
    }
1545
0
    SCTP_INP_RLOCK(it->inp);
1546
0
  }
1547
  /* now go through each assoc which is in the desired state */
1548
0
  if (it->done_current_ep == 0) {
1549
0
    if (it->function_inp != NULL)
1550
0
      inp_skip = (*it->function_inp)(it->inp, it->pointer, it->val);
1551
0
    it->done_current_ep = 1;
1552
0
  }
1553
0
  if (it->stcb == NULL) {
1554
    /* run the per instance function */
1555
0
    it->stcb = LIST_FIRST(&it->inp->sctp_asoc_list);
1556
0
  }
1557
0
  if ((inp_skip) || it->stcb == NULL) {
1558
0
    if (it->function_inp_end != NULL) {
1559
0
      inp_skip = (*it->function_inp_end)(it->inp,
1560
0
                 it->pointer,
1561
0
                 it->val);
1562
0
    }
1563
0
    SCTP_INP_RUNLOCK(it->inp);
1564
0
    goto no_stcb;
1565
0
  }
1566
0
  while (it->stcb != NULL) {
1567
0
    SCTP_TCB_LOCK(it->stcb);
1568
0
    if (it->asoc_state && ((it->stcb->asoc.state & it->asoc_state) != it->asoc_state)) {
1569
      /* not in the right state... keep looking */
1570
0
      SCTP_TCB_UNLOCK(it->stcb);
1571
0
      goto next_assoc;
1572
0
    }
1573
    /* see if we have limited out the iterator loop */
1574
0
    iteration_count++;
1575
0
    if (iteration_count > SCTP_ITERATOR_MAX_AT_ONCE) {
1576
      /* Pause to let others grab the lock */
1577
0
      atomic_add_int(&it->stcb->asoc.refcnt, 1);
1578
0
      SCTP_TCB_UNLOCK(it->stcb);
1579
0
      SCTP_INP_INCR_REF(it->inp);
1580
0
      SCTP_INP_RUNLOCK(it->inp);
1581
0
      SCTP_ITERATOR_UNLOCK();
1582
0
      SCTP_INP_INFO_RUNLOCK();
1583
0
      SCTP_INP_INFO_RLOCK();
1584
0
      SCTP_ITERATOR_LOCK();
1585
0
      if (sctp_it_ctl.iterator_flags) {
1586
        /* We won't be staying here */
1587
0
        SCTP_INP_DECR_REF(it->inp);
1588
0
        atomic_subtract_int(&it->stcb->asoc.refcnt, 1);
1589
0
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
1590
0
        if (sctp_it_ctl.iterator_flags &
1591
0
           SCTP_ITERATOR_MUST_EXIT) {
1592
0
          goto done_with_iterator;
1593
0
        }
1594
0
#endif
1595
0
        if (sctp_it_ctl.iterator_flags &
1596
0
           SCTP_ITERATOR_STOP_CUR_IT) {
1597
0
          sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_IT;
1598
0
          goto done_with_iterator;
1599
0
        }
1600
0
        if (sctp_it_ctl.iterator_flags &
1601
0
           SCTP_ITERATOR_STOP_CUR_INP) {
1602
0
          sctp_it_ctl.iterator_flags &= ~SCTP_ITERATOR_STOP_CUR_INP;
1603
0
          goto no_stcb;
1604
0
        }
1605
        /* If we reach here huh? */
1606
0
        SCTP_PRINTF("Unknown it ctl flag %x\n",
1607
0
              sctp_it_ctl.iterator_flags);
1608
0
        sctp_it_ctl.iterator_flags = 0;
1609
0
      }
1610
0
      SCTP_INP_RLOCK(it->inp);
1611
0
      SCTP_INP_DECR_REF(it->inp);
1612
0
      SCTP_TCB_LOCK(it->stcb);
1613
0
      atomic_subtract_int(&it->stcb->asoc.refcnt, 1);
1614
0
      iteration_count = 0;
1615
0
    }
1616
0
    KASSERT(it->inp == it->stcb->sctp_ep,
1617
0
            ("%s: stcb %p does not belong to inp %p, but inp %p",
1618
0
             __func__, it->stcb, it->inp, it->stcb->sctp_ep));
1619
0
    SCTP_INP_RLOCK_ASSERT(it->inp);
1620
0
    SCTP_TCB_LOCK_ASSERT(it->stcb);
1621
1622
    /* run function on this one */
1623
0
    (*it->function_assoc)(it->inp, it->stcb, it->pointer, it->val);
1624
0
    SCTP_INP_RLOCK_ASSERT(it->inp);
1625
0
    SCTP_TCB_LOCK_ASSERT(it->stcb);
1626
1627
    /*
1628
     * we lie here, it really needs to have its own type but
1629
     * first I must verify that this won't effect things :-0
1630
     */
1631
0
    if (it->no_chunk_output == 0) {
1632
0
      sctp_chunk_output(it->inp, it->stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1633
0
      SCTP_INP_RLOCK_ASSERT(it->inp);
1634
0
      SCTP_TCB_LOCK_ASSERT(it->stcb);
1635
0
    }
1636
1637
0
    SCTP_TCB_UNLOCK(it->stcb);
1638
0
  next_assoc:
1639
0
    it->stcb = LIST_NEXT(it->stcb, sctp_tcblist);
1640
0
    if (it->stcb == NULL) {
1641
      /* Run last function */
1642
0
      if (it->function_inp_end != NULL) {
1643
0
        inp_skip = (*it->function_inp_end)(it->inp,
1644
0
                   it->pointer,
1645
0
                   it->val);
1646
0
      }
1647
0
    }
1648
0
  }
1649
0
  SCTP_INP_RUNLOCK(it->inp);
1650
0
 no_stcb:
1651
  /* done with all assocs on this endpoint, move on to next endpoint */
1652
0
  it->done_current_ep = 0;
1653
0
  if (it->iterator_flags & SCTP_ITERATOR_DO_SINGLE_INP) {
1654
0
    it->inp = NULL;
1655
0
  } else {
1656
0
    it->inp = LIST_NEXT(it->inp, sctp_list);
1657
0
  }
1658
0
  it->stcb = NULL;
1659
0
  if (it->inp == NULL) {
1660
0
    goto done_with_iterator;
1661
0
  }
1662
0
  goto select_a_new_ep;
1663
0
}
1664
1665
void
1666
sctp_iterator_worker(void)
1667
0
{
1668
0
  struct sctp_iterator *it;
1669
1670
  /* This function is called with the WQ lock in place */
1671
0
  sctp_it_ctl.iterator_running = 1;
1672
0
  while ((it = TAILQ_FIRST(&sctp_it_ctl.iteratorhead)) != NULL) {
1673
    /* now lets work on this one */
1674
0
    TAILQ_REMOVE(&sctp_it_ctl.iteratorhead, it, sctp_nxt_itr);
1675
0
    SCTP_IPI_ITERATOR_WQ_UNLOCK();
1676
#if defined(__FreeBSD__) && !defined(__Userspace__)
1677
    CURVNET_SET(it->vn);
1678
#endif
1679
0
    sctp_iterator_work(it);
1680
#if defined(__FreeBSD__) && !defined(__Userspace__)
1681
    CURVNET_RESTORE();
1682
#endif
1683
0
    SCTP_IPI_ITERATOR_WQ_LOCK();
1684
#if !defined(__FreeBSD__) && !defined(__Userspace__)
1685
    if (sctp_it_ctl.iterator_flags & SCTP_ITERATOR_MUST_EXIT) {
1686
      break;
1687
    }
1688
#endif
1689
    /*sa_ignore FREED_MEMORY*/
1690
0
  }
1691
0
  sctp_it_ctl.iterator_running = 0;
1692
0
  return;
1693
0
}
1694
1695
static void
1696
sctp_handle_addr_wq(void)
1697
0
{
1698
  /* deal with the ADDR wq from the rtsock calls */
1699
0
  struct sctp_laddr *wi, *nwi;
1700
0
  struct sctp_asconf_iterator *asc;
1701
1702
0
  SCTP_MALLOC(asc, struct sctp_asconf_iterator *,
1703
0
        sizeof(struct sctp_asconf_iterator), SCTP_M_ASC_IT);
1704
0
  if (asc == NULL) {
1705
    /* Try later, no memory */
1706
0
    sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
1707
0
         (struct sctp_inpcb *)NULL,
1708
0
         (struct sctp_tcb *)NULL,
1709
0
         (struct sctp_nets *)NULL);
1710
0
    return;
1711
0
  }
1712
0
  LIST_INIT(&asc->list_of_work);
1713
0
  asc->cnt = 0;
1714
1715
0
  LIST_FOREACH_SAFE(wi, &SCTP_BASE_INFO(addr_wq), sctp_nxt_addr, nwi) {
1716
0
    LIST_REMOVE(wi, sctp_nxt_addr);
1717
0
    LIST_INSERT_HEAD(&asc->list_of_work, wi, sctp_nxt_addr);
1718
0
    asc->cnt++;
1719
0
  }
1720
1721
0
  if (asc->cnt == 0) {
1722
0
    SCTP_FREE(asc, SCTP_M_ASC_IT);
1723
0
  } else {
1724
0
    int ret;
1725
1726
0
    ret = sctp_initiate_iterator(sctp_asconf_iterator_ep,
1727
0
                                 sctp_asconf_iterator_stcb,
1728
0
                                 NULL, /* No ep end for boundall */
1729
0
                                 SCTP_PCB_FLAGS_BOUNDALL,
1730
0
                                 SCTP_PCB_ANY_FEATURES,
1731
0
                                 SCTP_ASOC_ANY_STATE,
1732
0
                                 (void *)asc, 0,
1733
0
                                 sctp_asconf_iterator_end, NULL, 0);
1734
0
    if (ret) {
1735
0
      SCTP_PRINTF("Failed to initiate iterator for handle_addr_wq\n");
1736
      /* Freeing if we are stopping or put back on the addr_wq. */
1737
0
      if (SCTP_BASE_VAR(sctp_pcb_initialized) == 0) {
1738
0
        sctp_asconf_iterator_end(asc, 0);
1739
0
      } else {
1740
0
        LIST_FOREACH(wi, &asc->list_of_work, sctp_nxt_addr) {
1741
0
          LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
1742
0
        }
1743
0
        SCTP_FREE(asc, SCTP_M_ASC_IT);
1744
0
      }
1745
0
    }
1746
0
  }
1747
0
}
1748
1749
/*-
1750
 * The following table shows which pointers for the inp, stcb, or net are
1751
 * stored for each timer after it was started.
1752
 *
1753
 *|Name                         |Timer                        |inp |stcb|net |
1754
 *|-----------------------------|-----------------------------|----|----|----|
1755
 *|SCTP_TIMER_TYPE_SEND         |net->rxt_timer               |Yes |Yes |Yes |
1756
 *|SCTP_TIMER_TYPE_INIT         |net->rxt_timer               |Yes |Yes |Yes |
1757
 *|SCTP_TIMER_TYPE_RECV         |stcb->asoc.dack_timer        |Yes |Yes |No  |
1758
 *|SCTP_TIMER_TYPE_SHUTDOWN     |net->rxt_timer               |Yes |Yes |Yes |
1759
 *|SCTP_TIMER_TYPE_HEARTBEAT    |net->hb_timer                |Yes |Yes |Yes |
1760
 *|SCTP_TIMER_TYPE_COOKIE       |net->rxt_timer               |Yes |Yes |Yes |
1761
 *|SCTP_TIMER_TYPE_NEWCOOKIE    |inp->sctp_ep.signature_change|Yes |No  |No  |
1762
 *|SCTP_TIMER_TYPE_PATHMTURAISE |net->pmtu_timer              |Yes |Yes |Yes |
1763
 *|SCTP_TIMER_TYPE_SHUTDOWNACK  |net->rxt_timer               |Yes |Yes |Yes |
1764
 *|SCTP_TIMER_TYPE_ASCONF       |stcb->asoc.asconf_timer      |Yes |Yes |Yes |
1765
 *|SCTP_TIMER_TYPE_SHUTDOWNGUARD|stcb->asoc.shut_guard_timer  |Yes |Yes |No  |
1766
 *|SCTP_TIMER_TYPE_AUTOCLOSE    |stcb->asoc.autoclose_timer   |Yes |Yes |No  |
1767
 *|SCTP_TIMER_TYPE_STRRESET     |stcb->asoc.strreset_timer    |Yes |Yes |No  |
1768
 *|SCTP_TIMER_TYPE_INPKILL      |inp->sctp_ep.signature_change|Yes |No  |No  |
1769
 *|SCTP_TIMER_TYPE_ASOCKILL     |stcb->asoc.strreset_timer    |Yes |Yes |No  |
1770
 *|SCTP_TIMER_TYPE_ADDR_WQ      |SCTP_BASE_INFO(addr_wq_timer)|No  |No  |No  |
1771
 *|SCTP_TIMER_TYPE_PRIM_DELETED |stcb->asoc.delete_prim_timer |Yes |Yes |No  |
1772
 */
1773
1774
void
1775
sctp_timeout_handler(void *t)
1776
1.83k
{
1777
#if defined(__FreeBSD__) && !defined(__Userspace__)
1778
  struct epoch_tracker et;
1779
#endif
1780
1.83k
  struct timeval tv;
1781
1.83k
  struct sctp_inpcb *inp;
1782
1.83k
  struct sctp_tcb *stcb;
1783
1.83k
  struct sctp_nets *net;
1784
1.83k
  struct sctp_timer *tmr;
1785
1.83k
  struct mbuf *op_err;
1786
#if defined(__APPLE__) && !defined(__Userspace__)
1787
  struct socket *so;
1788
#endif
1789
1.83k
#if defined(__Userspace__)
1790
1.83k
  struct socket *upcall_socket = NULL;
1791
1.83k
#endif
1792
1.83k
  int type;
1793
1.83k
  int i, secret;
1794
1.83k
  bool did_output, released_asoc_reference;
1795
1796
  /*
1797
   * If inp, stcb or net are not NULL, then references to these were
1798
   * added when the timer was started, and must be released before this
1799
   * function returns.
1800
   */
1801
1.83k
  tmr = (struct sctp_timer *)t;
1802
1.83k
  inp = (struct sctp_inpcb *)tmr->ep;
1803
1.83k
  stcb = (struct sctp_tcb *)tmr->tcb;
1804
1.83k
  net = (struct sctp_nets *)tmr->net;
1805
#if defined(__FreeBSD__) && !defined(__Userspace__)
1806
  CURVNET_SET((struct vnet *)tmr->vnet);
1807
  NET_EPOCH_ENTER(et);
1808
#endif
1809
1.83k
  released_asoc_reference = false;
1810
1811
#ifdef SCTP_AUDITING_ENABLED
1812
  sctp_audit_log(0xF0, (uint8_t) tmr->type);
1813
  sctp_auditing(3, inp, stcb, net);
1814
#endif
1815
1816
  /* sanity checks... */
1817
1.83k
  KASSERT(tmr->self == NULL || tmr->self == tmr,
1818
1.83k
          ("sctp_timeout_handler: tmr->self corrupted"));
1819
1.83k
  KASSERT(SCTP_IS_TIMER_TYPE_VALID(tmr->type),
1820
1.83k
          ("sctp_timeout_handler: invalid timer type %d", tmr->type));
1821
1.83k
  type = tmr->type;
1822
1.83k
  KASSERT(stcb == NULL || stcb->sctp_ep == inp,
1823
1.83k
          ("sctp_timeout_handler of type %d: inp = %p, stcb->sctp_ep %p",
1824
1.83k
           type, stcb, stcb->sctp_ep));
1825
1.83k
  tmr->stopped_from = 0xa001;
1826
1.83k
  if ((stcb != NULL) && (stcb->asoc.state == SCTP_STATE_EMPTY)) {
1827
0
    SCTPDBG(SCTP_DEBUG_TIMER2,
1828
0
            "Timer type %d handler exiting due to CLOSED association.\n",
1829
0
            type);
1830
0
    goto out_decr;
1831
0
  }
1832
1.83k
  tmr->stopped_from = 0xa002;
1833
1.83k
  SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d goes off.\n", type);
1834
1.83k
  if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1835
0
    SCTPDBG(SCTP_DEBUG_TIMER2,
1836
0
      "Timer type %d handler exiting due to not being active.\n",
1837
0
      type);
1838
0
    goto out_decr;
1839
0
  }
1840
1841
1.83k
  tmr->stopped_from = 0xa003;
1842
1.83k
  if (stcb) {
1843
1.81k
    SCTP_TCB_LOCK(stcb);
1844
    /*
1845
     * Release reference so that association can be freed if
1846
     * necessary below.
1847
     * This is safe now that we have acquired the lock.
1848
     */
1849
1.81k
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
1850
1.81k
    released_asoc_reference = true;
1851
1.81k
    if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
1852
1.81k
        ((stcb->asoc.state == SCTP_STATE_EMPTY) ||
1853
18
         (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1854
18
      SCTPDBG(SCTP_DEBUG_TIMER2,
1855
18
              "Timer type %d handler exiting due to CLOSED association.\n",
1856
18
              type);
1857
18
      goto out;
1858
18
    }
1859
1.81k
  } else if (inp != NULL) {
1860
18
    SCTP_INP_WLOCK(inp);
1861
18
  } else {
1862
0
    SCTP_WQ_ADDR_LOCK();
1863
0
  }
1864
1865
  /* Record in stopped_from which timeout occurred. */
1866
1.81k
  tmr->stopped_from = type;
1867
  /* mark as being serviced now */
1868
1.81k
  if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1869
    /*
1870
     * Callout has been rescheduled.
1871
     */
1872
0
    goto out;
1873
0
  }
1874
1.81k
  if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1875
    /*
1876
     * Not active, so no action.
1877
     */
1878
0
    goto out;
1879
0
  }
1880
1.81k
  SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1881
1882
1.81k
#if defined(__Userspace__)
1883
1.81k
  if ((stcb != NULL) &&
1884
1.81k
      ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
1885
1.81k
      (stcb->sctp_socket != NULL)) {
1886
0
    upcall_socket = stcb->sctp_socket;
1887
0
    SOCK_LOCK(upcall_socket);
1888
0
    soref(upcall_socket);
1889
0
    SOCK_UNLOCK(upcall_socket);
1890
0
  }
1891
1.81k
#endif
1892
  /* call the handler for the appropriate timer type */
1893
1.81k
  switch (type) {
1894
0
  case SCTP_TIMER_TYPE_SEND:
1895
0
    KASSERT(inp != NULL && stcb != NULL && net != NULL,
1896
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1897
0
             type, inp, stcb, net));
1898
0
    SCTP_STAT_INCR(sctps_timodata);
1899
0
    stcb->asoc.timodata++;
1900
0
    stcb->asoc.num_send_timers_up--;
1901
0
    if (stcb->asoc.num_send_timers_up < 0) {
1902
0
      stcb->asoc.num_send_timers_up = 0;
1903
0
    }
1904
0
    SCTP_TCB_LOCK_ASSERT(stcb);
1905
0
    if (sctp_t3rxt_timer(inp, stcb, net)) {
1906
      /* no need to unlock on tcb its gone */
1907
1908
0
      goto out_decr;
1909
0
    }
1910
0
    SCTP_TCB_LOCK_ASSERT(stcb);
1911
#ifdef SCTP_AUDITING_ENABLED
1912
    sctp_auditing(4, inp, stcb, net);
1913
#endif
1914
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
1915
0
    did_output = true;
1916
0
    if ((stcb->asoc.num_send_timers_up == 0) &&
1917
0
        (stcb->asoc.sent_queue_cnt > 0)) {
1918
0
      struct sctp_tmit_chunk *chk;
1919
1920
      /*
1921
       * Safeguard. If there on some on the sent queue
1922
       * somewhere but no timers running something is
1923
       * wrong... so we start a timer on the first chunk
1924
       * on the send queue on whatever net it is sent to.
1925
       */
1926
0
      TAILQ_FOREACH(chk, &stcb->asoc.sent_queue, sctp_next) {
1927
0
        if (chk->whoTo != NULL) {
1928
0
          break;
1929
0
        }
1930
0
      }
1931
0
      if (chk != NULL) {
1932
0
        sctp_timer_start(SCTP_TIMER_TYPE_SEND, stcb->sctp_ep, stcb, chk->whoTo);
1933
0
      }
1934
0
    }
1935
0
    break;
1936
0
  case SCTP_TIMER_TYPE_INIT:
1937
0
    KASSERT(inp != NULL && stcb != NULL && net != NULL,
1938
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1939
0
             type, inp, stcb, net));
1940
0
    SCTP_STAT_INCR(sctps_timoinit);
1941
0
    stcb->asoc.timoinit++;
1942
0
    if (sctp_t1init_timer(inp, stcb, net)) {
1943
      /* no need to unlock on tcb its gone */
1944
0
      goto out_decr;
1945
0
    }
1946
0
    did_output = false;
1947
0
    break;
1948
0
  case SCTP_TIMER_TYPE_RECV:
1949
0
    KASSERT(inp != NULL && stcb != NULL && net == NULL,
1950
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1951
0
             type, inp, stcb, net));
1952
0
    SCTP_STAT_INCR(sctps_timosack);
1953
0
    stcb->asoc.timosack++;
1954
0
    sctp_send_sack(stcb, SCTP_SO_NOT_LOCKED);
1955
#ifdef SCTP_AUDITING_ENABLED
1956
    sctp_auditing(4, inp, stcb, NULL);
1957
#endif
1958
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SACK_TMR, SCTP_SO_NOT_LOCKED);
1959
0
    did_output = true;
1960
0
    break;
1961
0
  case SCTP_TIMER_TYPE_SHUTDOWN:
1962
0
    KASSERT(inp != NULL && stcb != NULL && net != NULL,
1963
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1964
0
             type, inp, stcb, net));
1965
0
    SCTP_STAT_INCR(sctps_timoshutdown);
1966
0
    stcb->asoc.timoshutdown++;
1967
0
    if (sctp_shutdown_timer(inp, stcb, net)) {
1968
      /* no need to unlock on tcb its gone */
1969
0
      goto out_decr;
1970
0
    }
1971
#ifdef SCTP_AUDITING_ENABLED
1972
    sctp_auditing(4, inp, stcb, net);
1973
#endif
1974
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_TMR, SCTP_SO_NOT_LOCKED);
1975
0
    did_output = true;
1976
0
    break;
1977
0
  case SCTP_TIMER_TYPE_HEARTBEAT:
1978
0
    KASSERT(inp != NULL && stcb != NULL && net != NULL,
1979
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
1980
0
             type, inp, stcb, net));
1981
0
    SCTP_STAT_INCR(sctps_timoheartbeat);
1982
0
    stcb->asoc.timoheartbeat++;
1983
0
    if (sctp_heartbeat_timer(inp, stcb, net)) {
1984
      /* no need to unlock on tcb its gone */
1985
0
      goto out_decr;
1986
0
    }
1987
#ifdef SCTP_AUDITING_ENABLED
1988
    sctp_auditing(4, inp, stcb, net);
1989
#endif
1990
0
    if ((net->dest_state & SCTP_ADDR_NOHB) == 0) {
1991
0
      sctp_timer_start(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net);
1992
0
      sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_HB_TMR, SCTP_SO_NOT_LOCKED);
1993
0
      did_output = true;
1994
0
    } else {
1995
0
      did_output = false;
1996
0
    }
1997
0
    break;
1998
0
  case SCTP_TIMER_TYPE_COOKIE:
1999
0
    KASSERT(inp != NULL && stcb != NULL && net != NULL,
2000
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2001
0
             type, inp, stcb, net));
2002
0
    SCTP_STAT_INCR(sctps_timocookie);
2003
0
    stcb->asoc.timocookie++;
2004
0
    if (sctp_cookie_timer(inp, stcb, net)) {
2005
      /* no need to unlock on tcb its gone */
2006
0
      goto out_decr;
2007
0
    }
2008
#ifdef SCTP_AUDITING_ENABLED
2009
    sctp_auditing(4, inp, stcb, net);
2010
#endif
2011
    /*
2012
     * We consider T3 and Cookie timer pretty much the same with
2013
     * respect to where from in chunk_output.
2014
     */
2015
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_T3, SCTP_SO_NOT_LOCKED);
2016
0
    did_output = true;
2017
0
    break;
2018
0
  case SCTP_TIMER_TYPE_NEWCOOKIE:
2019
0
    KASSERT(inp != NULL && stcb == NULL && net == NULL,
2020
0
      ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2021
0
       type, inp, stcb, net));
2022
0
    SCTP_STAT_INCR(sctps_timosecret);
2023
0
    (void)SCTP_GETTIME_TIMEVAL(&tv);
2024
0
    inp->sctp_ep.time_of_secret_change = tv.tv_sec;
2025
0
    inp->sctp_ep.last_secret_number =
2026
0
        inp->sctp_ep.current_secret_number;
2027
0
    inp->sctp_ep.current_secret_number++;
2028
0
    if (inp->sctp_ep.current_secret_number >=
2029
0
        SCTP_HOW_MANY_SECRETS) {
2030
0
      inp->sctp_ep.current_secret_number = 0;
2031
0
    }
2032
0
    secret = (int)inp->sctp_ep.current_secret_number;
2033
0
    for (i = 0; i < SCTP_NUMBER_OF_SECRETS; i++) {
2034
0
      inp->sctp_ep.secret_key[secret][i] =
2035
0
          sctp_select_initial_TSN(&inp->sctp_ep);
2036
0
    }
2037
0
    sctp_timer_start(SCTP_TIMER_TYPE_NEWCOOKIE, inp, NULL, NULL);
2038
0
    did_output = false;
2039
0
    break;
2040
0
  case SCTP_TIMER_TYPE_PATHMTURAISE:
2041
0
    KASSERT(inp != NULL && stcb != NULL && net != NULL,
2042
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2043
0
             type, inp, stcb, net));
2044
0
    SCTP_STAT_INCR(sctps_timopathmtu);
2045
0
    sctp_pathmtu_timer(inp, stcb, net);
2046
0
    did_output = false;
2047
0
    break;
2048
0
  case SCTP_TIMER_TYPE_SHUTDOWNACK:
2049
0
    KASSERT(inp != NULL && stcb != NULL && net != NULL,
2050
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2051
0
             type, inp, stcb, net));
2052
0
    if (sctp_shutdownack_timer(inp, stcb, net)) {
2053
      /* no need to unlock on tcb its gone */
2054
0
      goto out_decr;
2055
0
    }
2056
0
    SCTP_STAT_INCR(sctps_timoshutdownack);
2057
0
    stcb->asoc.timoshutdownack++;
2058
#ifdef SCTP_AUDITING_ENABLED
2059
    sctp_auditing(4, inp, stcb, net);
2060
#endif
2061
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_SHUT_ACK_TMR, SCTP_SO_NOT_LOCKED);
2062
0
    did_output = true;
2063
0
    break;
2064
0
  case SCTP_TIMER_TYPE_ASCONF:
2065
0
    KASSERT(inp != NULL && stcb != NULL && net != NULL,
2066
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2067
0
             type, inp, stcb, net));
2068
0
    SCTP_STAT_INCR(sctps_timoasconf);
2069
0
    if (sctp_asconf_timer(inp, stcb, net)) {
2070
      /* no need to unlock on tcb its gone */
2071
0
      goto out_decr;
2072
0
    }
2073
#ifdef SCTP_AUDITING_ENABLED
2074
    sctp_auditing(4, inp, stcb, net);
2075
#endif
2076
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_ASCONF_TMR, SCTP_SO_NOT_LOCKED);
2077
0
    did_output = true;
2078
0
    break;
2079
0
  case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2080
0
    KASSERT(inp != NULL && stcb != NULL && net == NULL,
2081
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2082
0
             type, inp, stcb, net));
2083
0
    SCTP_STAT_INCR(sctps_timoshutdownguard);
2084
0
    op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
2085
0
                                 "Shutdown guard timer expired");
2086
0
    sctp_abort_an_association(inp, stcb, op_err, true, SCTP_SO_NOT_LOCKED);
2087
    /* no need to unlock on tcb its gone */
2088
0
    goto out_decr;
2089
0
  case SCTP_TIMER_TYPE_AUTOCLOSE:
2090
0
    KASSERT(inp != NULL && stcb != NULL && net == NULL,
2091
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2092
0
             type, inp, stcb, net));
2093
0
    SCTP_STAT_INCR(sctps_timoautoclose);
2094
0
    sctp_autoclose_timer(inp, stcb);
2095
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_AUTOCLOSE_TMR, SCTP_SO_NOT_LOCKED);
2096
0
    did_output = true;
2097
0
    break;
2098
0
  case SCTP_TIMER_TYPE_STRRESET:
2099
0
    KASSERT(inp != NULL && stcb != NULL && net == NULL,
2100
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2101
0
             type, inp, stcb, net));
2102
0
    SCTP_STAT_INCR(sctps_timostrmrst);
2103
0
    if (sctp_strreset_timer(inp, stcb)) {
2104
      /* no need to unlock on tcb its gone */
2105
0
      goto out_decr;
2106
0
    }
2107
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_STRRST_TMR, SCTP_SO_NOT_LOCKED);
2108
0
    did_output = true;
2109
0
    break;
2110
18
  case SCTP_TIMER_TYPE_INPKILL:
2111
18
    KASSERT(inp != NULL && stcb == NULL && net == NULL,
2112
18
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2113
18
             type, inp, stcb, net));
2114
18
    SCTP_STAT_INCR(sctps_timoinpkill);
2115
    /*
2116
     * special case, take away our increment since WE are the
2117
     * killer
2118
     */
2119
18
    sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
2120
18
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
2121
#if defined(__APPLE__) && !defined(__Userspace__)
2122
    SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1);
2123
#endif
2124
18
    SCTP_INP_DECR_REF(inp);
2125
18
    SCTP_INP_WUNLOCK(inp);
2126
18
    sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
2127
18
                    SCTP_CALLED_FROM_INPKILL_TIMER);
2128
#if defined(__APPLE__) && !defined(__Userspace__)
2129
    SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1);
2130
#endif
2131
18
    inp = NULL;
2132
18
    goto out_decr;
2133
1.79k
  case SCTP_TIMER_TYPE_ASOCKILL:
2134
1.79k
    KASSERT(inp != NULL && stcb != NULL && net == NULL,
2135
1.79k
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2136
1.79k
             type, inp, stcb, net));
2137
1.79k
    SCTP_STAT_INCR(sctps_timoassockill);
2138
    /* Can we free it yet? */
2139
1.79k
    sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
2140
1.79k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_1);
2141
#if defined(__APPLE__) && !defined(__Userspace__)
2142
    so = SCTP_INP_SO(inp);
2143
    atomic_add_int(&stcb->asoc.refcnt, 1);
2144
    SCTP_TCB_UNLOCK(stcb);
2145
    SCTP_SOCKET_LOCK(so, 1);
2146
    SCTP_TCB_LOCK(stcb);
2147
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
2148
#endif
2149
1.79k
    (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2150
1.79k
                          SCTP_FROM_SCTPUTIL + SCTP_LOC_2);
2151
#if defined(__APPLE__) && !defined(__Userspace__)
2152
    SCTP_SOCKET_UNLOCK(so, 1);
2153
#endif
2154
    /*
2155
     * free asoc, always unlocks (or destroy's) so prevent
2156
     * duplicate unlock or unlock of a free mtx :-0
2157
     */
2158
1.79k
    stcb = NULL;
2159
1.79k
    goto out_decr;
2160
0
  case SCTP_TIMER_TYPE_ADDR_WQ:
2161
0
    KASSERT(inp == NULL && stcb == NULL && net == NULL,
2162
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2163
0
             type, inp, stcb, net));
2164
0
    sctp_handle_addr_wq();
2165
0
    did_output = true;
2166
0
    break;
2167
0
  case SCTP_TIMER_TYPE_PRIM_DELETED:
2168
0
    KASSERT(inp != NULL && stcb != NULL && net == NULL,
2169
0
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2170
0
             type, inp, stcb, net));
2171
0
    SCTP_STAT_INCR(sctps_timodelprim);
2172
0
    sctp_delete_prim_timer(inp, stcb);
2173
0
    did_output = false;
2174
0
    break;
2175
0
  default:
2176
0
#ifdef INVARIANTS
2177
0
    panic("Unknown timer type %d", type);
2178
#else
2179
    goto out;
2180
#endif
2181
1.81k
  }
2182
#ifdef SCTP_AUDITING_ENABLED
2183
  sctp_audit_log(0xF1, (uint8_t) type);
2184
  if (inp != NULL)
2185
    sctp_auditing(5, inp, stcb, net);
2186
#endif
2187
0
  if (did_output && (stcb != NULL)) {
2188
    /*
2189
     * Now we need to clean up the control chunk chain if an
2190
     * ECNE is on it. It must be marked as UNSENT again so next
2191
     * call will continue to send it until such time that we get
2192
     * a CWR, to remove it. It is, however, less likely that we
2193
     * will find a ecn echo on the chain though.
2194
     */
2195
0
    sctp_fix_ecn_echo(&stcb->asoc);
2196
0
  }
2197
18
out:
2198
18
  if (stcb != NULL) {
2199
18
    SCTP_TCB_UNLOCK(stcb);
2200
18
  } else if (inp != NULL) {
2201
0
    SCTP_INP_WUNLOCK(inp);
2202
0
  } else {
2203
0
    SCTP_WQ_ADDR_UNLOCK();
2204
0
  }
2205
2206
1.83k
out_decr:
2207
1.83k
#if defined(__Userspace__)
2208
1.83k
  if (upcall_socket != NULL) {
2209
0
    if ((upcall_socket->so_upcall != NULL) &&
2210
0
        (upcall_socket->so_error != 0)) {
2211
0
      (*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
2212
0
    }
2213
0
    ACCEPT_LOCK();
2214
0
    SOCK_LOCK(upcall_socket);
2215
0
    sorele(upcall_socket);
2216
0
  }
2217
1.83k
#endif
2218
  /* These reference counts were incremented in sctp_timer_start(). */
2219
1.83k
  if (inp != NULL) {
2220
1.81k
    SCTP_INP_DECR_REF(inp);
2221
1.81k
  }
2222
1.83k
  if ((stcb != NULL) && !released_asoc_reference) {
2223
0
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
2224
0
  }
2225
1.83k
  if (net != NULL) {
2226
18
    sctp_free_remote_addr(net);
2227
18
  }
2228
1.83k
  SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d handler finished.\n", type);
2229
#if defined(__FreeBSD__) && !defined(__Userspace__)
2230
  CURVNET_RESTORE();
2231
  NET_EPOCH_EXIT(et);
2232
#endif
2233
1.83k
}
2234
2235
/*-
2236
 * The following table shows which parameters must be provided
2237
 * when calling sctp_timer_start(). For parameters not being
2238
 * provided, NULL must be used.
2239
 *
2240
 * |Name                         |inp |stcb|net |
2241
 * |-----------------------------|----|----|----|
2242
 * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2243
 * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2244
 * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2245
 * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2246
 * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2247
 * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2248
 * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2249
 * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2250
 * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2251
 * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |Yes |
2252
 * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2253
 * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2254
 * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |Yes |
2255
 * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2256
 * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2257
 * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2258
 * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2259
 *
2260
 */
2261
2262
void
2263
sctp_timer_start(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2264
    struct sctp_nets *net)
2265
128k
{
2266
128k
  struct sctp_timer *tmr;
2267
128k
  uint32_t to_ticks;
2268
128k
  uint32_t rndval, jitter;
2269
2270
128k
  KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2271
128k
          ("sctp_timer_start of type %d: inp = %p, stcb->sctp_ep %p",
2272
128k
           t_type, stcb, stcb->sctp_ep));
2273
128k
  tmr = NULL;
2274
128k
  if (stcb != NULL) {
2275
117k
    SCTP_TCB_LOCK_ASSERT(stcb);
2276
117k
  } else if (inp != NULL) {
2277
10.5k
    SCTP_INP_WLOCK_ASSERT(inp);
2278
18.4E
  } else {
2279
18.4E
    SCTP_WQ_ADDR_LOCK_ASSERT();
2280
18.4E
  }
2281
128k
  if (stcb != NULL) {
2282
    /* Don't restart timer on association that's about to be killed. */
2283
117k
    if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
2284
117k
        (t_type != SCTP_TIMER_TYPE_ASOCKILL)) {
2285
0
      SCTPDBG(SCTP_DEBUG_TIMER2,
2286
0
        "Timer type %d not started: inp=%p, stcb=%p, net=%p (stcb deleted).\n",
2287
0
        t_type, inp, stcb, net);
2288
0
      return;
2289
0
    }
2290
    /* Don't restart timer on net that's been removed. */
2291
117k
    if (net != NULL && (net->dest_state & SCTP_ADDR_BEING_DELETED)) {
2292
0
      SCTPDBG(SCTP_DEBUG_TIMER2,
2293
0
        "Timer type %d not started: inp=%p, stcb=%p, net=%p (net deleted).\n",
2294
0
        t_type, inp, stcb, net);
2295
0
      return;
2296
0
    }
2297
117k
  }
2298
128k
  switch (t_type) {
2299
5.56k
  case SCTP_TIMER_TYPE_SEND:
2300
    /* Here we use the RTO timer. */
2301
5.56k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2302
0
#ifdef INVARIANTS
2303
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2304
0
            t_type, inp, stcb, net);
2305
#else
2306
      return;
2307
#endif
2308
0
    }
2309
5.56k
    tmr = &net->rxt_timer;
2310
5.56k
    if (net->RTO == 0) {
2311
104
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2312
5.46k
    } else {
2313
5.46k
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2314
5.46k
    }
2315
5.56k
    break;
2316
21.1k
  case SCTP_TIMER_TYPE_INIT:
2317
    /*
2318
     * Here we use the INIT timer default usually about 1
2319
     * second.
2320
     */
2321
21.1k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2322
0
#ifdef INVARIANTS
2323
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2324
0
            t_type, inp, stcb, net);
2325
#else
2326
      return;
2327
#endif
2328
0
    }
2329
21.1k
    tmr = &net->rxt_timer;
2330
21.1k
    if (net->RTO == 0) {
2331
19.6k
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2332
19.6k
    } else {
2333
1.41k
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2334
1.41k
    }
2335
21.1k
    break;
2336
146
  case SCTP_TIMER_TYPE_RECV:
2337
    /*
2338
     * Here we use the Delayed-Ack timer value from the inp,
2339
     * usually about 200ms.
2340
     */
2341
146
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2342
0
#ifdef INVARIANTS
2343
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2344
0
            t_type, inp, stcb, net);
2345
#else
2346
      return;
2347
#endif
2348
0
    }
2349
146
    tmr = &stcb->asoc.dack_timer;
2350
146
    to_ticks = sctp_msecs_to_ticks(stcb->asoc.delayed_ack);
2351
146
    break;
2352
0
  case SCTP_TIMER_TYPE_SHUTDOWN:
2353
    /* Here we use the RTO of the destination. */
2354
0
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2355
0
#ifdef INVARIANTS
2356
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2357
0
            t_type, inp, stcb, net);
2358
#else
2359
      return;
2360
#endif
2361
0
    }
2362
0
    tmr = &net->rxt_timer;
2363
0
    if (net->RTO == 0) {
2364
0
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2365
0
    } else {
2366
0
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2367
0
    }
2368
0
    break;
2369
38.0k
  case SCTP_TIMER_TYPE_HEARTBEAT:
2370
    /*
2371
     * The net is used here so that we can add in the RTO. Even
2372
     * though we use a different timer. We also add the HB timer
2373
     * PLUS a random jitter.
2374
     */
2375
38.0k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2376
0
#ifdef INVARIANTS
2377
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2378
0
            t_type, inp, stcb, net);
2379
#else
2380
      return;
2381
#endif
2382
0
    }
2383
38.0k
    if ((net->dest_state & SCTP_ADDR_NOHB) &&
2384
38.0k
        ((net->dest_state & SCTP_ADDR_UNCONFIRMED) == 0)) {
2385
0
      SCTPDBG(SCTP_DEBUG_TIMER2,
2386
0
              "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2387
0
              t_type, inp, stcb, net);
2388
0
      return;
2389
0
    }
2390
38.0k
    tmr = &net->hb_timer;
2391
38.0k
    if (net->RTO == 0) {
2392
27.7k
      to_ticks = stcb->asoc.initial_rto;
2393
27.7k
    } else {
2394
10.3k
      to_ticks = net->RTO;
2395
10.3k
    }
2396
38.0k
    if (!((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2397
38.0k
          (net->dest_state & SCTP_ADDR_REACHABLE)) &&
2398
38.0k
        ((net->dest_state & SCTP_ADDR_PF) == 0)) {
2399
12.1k
      if (to_ticks > 1) {
2400
12.1k
        rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2401
12.1k
        jitter = rndval % to_ticks;
2402
12.1k
        to_ticks >>= 1;
2403
12.1k
        if (jitter < (UINT32_MAX - to_ticks)) {
2404
12.1k
          to_ticks += jitter;
2405
12.1k
        } else {
2406
0
          to_ticks = UINT32_MAX;
2407
0
        }
2408
12.1k
      }
2409
12.1k
      if (net->heart_beat_delay < (UINT32_MAX - to_ticks)) {
2410
12.1k
        to_ticks += net->heart_beat_delay;
2411
12.1k
      } else {
2412
0
        to_ticks = UINT32_MAX;
2413
0
      }
2414
12.1k
    }
2415
    /*
2416
     * Now we must convert the to_ticks that are now in
2417
     * ms to ticks.
2418
     */
2419
38.0k
    to_ticks = sctp_msecs_to_ticks(to_ticks);
2420
38.0k
    break;
2421
15.7k
  case SCTP_TIMER_TYPE_COOKIE:
2422
    /*
2423
     * Here we can use the RTO timer from the network since one
2424
     * RTT was complete. If a retransmission happened then we will
2425
     * be using the RTO initial value.
2426
     */
2427
15.7k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2428
0
#ifdef INVARIANTS
2429
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2430
0
            t_type, inp, stcb, net);
2431
#else
2432
      return;
2433
#endif
2434
0
    }
2435
15.7k
    tmr = &net->rxt_timer;
2436
15.7k
    if (net->RTO == 0) {
2437
0
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2438
15.7k
    } else {
2439
15.7k
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2440
15.7k
    }
2441
15.7k
    break;
2442
10.5k
  case SCTP_TIMER_TYPE_NEWCOOKIE:
2443
    /*
2444
     * Nothing needed but the endpoint here usually about 60
2445
     * minutes.
2446
     */
2447
10.5k
    if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2448
0
#ifdef INVARIANTS
2449
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2450
0
            t_type, inp, stcb, net);
2451
#else
2452
      return;
2453
#endif
2454
0
    }
2455
10.5k
    tmr = &inp->sctp_ep.signature_change;
2456
10.5k
    to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2457
10.5k
    break;
2458
32.9k
  case SCTP_TIMER_TYPE_PATHMTURAISE:
2459
    /*
2460
     * Here we use the value found in the EP for PMTUD, usually
2461
     * about 10 minutes.
2462
     */
2463
32.9k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2464
0
#ifdef INVARIANTS
2465
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2466
0
            t_type, inp, stcb, net);
2467
#else
2468
      return;
2469
#endif
2470
0
    }
2471
32.9k
    if (net->dest_state & SCTP_ADDR_NO_PMTUD) {
2472
0
      SCTPDBG(SCTP_DEBUG_TIMER2,
2473
0
              "Timer type %d not started: inp=%p, stcb=%p, net=%p.\n",
2474
0
              t_type, inp, stcb, net);
2475
0
      return;
2476
0
    }
2477
32.9k
    tmr = &net->pmtu_timer;
2478
32.9k
    to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2479
32.9k
    break;
2480
37
  case SCTP_TIMER_TYPE_SHUTDOWNACK:
2481
    /* Here we use the RTO of the destination. */
2482
37
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2483
0
#ifdef INVARIANTS
2484
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2485
0
            t_type, inp, stcb, net);
2486
#else
2487
      return;
2488
#endif
2489
0
    }
2490
37
    tmr = &net->rxt_timer;
2491
37
    if (net->RTO == 0) {
2492
0
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2493
37
    } else {
2494
37
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2495
37
    }
2496
37
    break;
2497
0
  case SCTP_TIMER_TYPE_ASCONF:
2498
    /*
2499
     * Here the timer comes from the stcb but its value is from
2500
     * the net's RTO.
2501
     */
2502
0
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2503
0
#ifdef INVARIANTS
2504
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2505
0
            t_type, inp, stcb, net);
2506
#else
2507
      return;
2508
#endif
2509
0
    }
2510
0
    tmr = &stcb->asoc.asconf_timer;
2511
0
    if (net->RTO == 0) {
2512
0
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2513
0
    } else {
2514
0
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2515
0
    }
2516
0
    break;
2517
0
  case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2518
    /*
2519
     * Here we use the endpoints shutdown guard timer usually
2520
     * about 3 minutes.
2521
     */
2522
0
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2523
0
#ifdef INVARIANTS
2524
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2525
0
            t_type, inp, stcb, net);
2526
#else
2527
      return;
2528
#endif
2529
0
    }
2530
0
    tmr = &stcb->asoc.shut_guard_timer;
2531
0
    if (inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN] == 0) {
2532
0
      if (stcb->asoc.maxrto < UINT32_MAX / 5) {
2533
0
        to_ticks = sctp_msecs_to_ticks(5 * stcb->asoc.maxrto);
2534
0
      } else {
2535
0
        to_ticks = sctp_msecs_to_ticks(UINT32_MAX);
2536
0
      }
2537
0
    } else {
2538
0
      to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_MAXSHUTDOWN];
2539
0
    }
2540
0
    break;
2541
0
  case SCTP_TIMER_TYPE_AUTOCLOSE:
2542
0
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2543
0
#ifdef INVARIANTS
2544
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2545
0
            t_type, inp, stcb, net);
2546
#else
2547
      return;
2548
#endif
2549
0
    }
2550
0
    tmr = &stcb->asoc.autoclose_timer;
2551
0
    to_ticks = stcb->asoc.sctp_autoclose_ticks;
2552
0
    break;
2553
2.17k
  case SCTP_TIMER_TYPE_STRRESET:
2554
    /*
2555
     * Here the timer comes from the stcb but its value is from
2556
     * the net's RTO.
2557
     */
2558
2.17k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2559
0
#ifdef INVARIANTS
2560
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2561
0
            t_type, inp, stcb, net);
2562
#else
2563
      return;
2564
#endif
2565
0
    }
2566
2.17k
    tmr = &stcb->asoc.strreset_timer;
2567
2.17k
    if (net->RTO == 0) {
2568
11
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2569
2.16k
    } else {
2570
2.16k
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2571
2.16k
    }
2572
2.17k
    break;
2573
18
  case SCTP_TIMER_TYPE_INPKILL:
2574
    /*
2575
     * The inp is setup to die. We re-use the signature_change
2576
     * timer since that has stopped and we are in the GONE
2577
     * state.
2578
     */
2579
18
    if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2580
0
#ifdef INVARIANTS
2581
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2582
0
            t_type, inp, stcb, net);
2583
#else
2584
      return;
2585
#endif
2586
0
    }
2587
18
    tmr = &inp->sctp_ep.signature_change;
2588
18
    to_ticks = sctp_msecs_to_ticks(SCTP_INP_KILL_TIMEOUT);
2589
18
    break;
2590
1.79k
  case SCTP_TIMER_TYPE_ASOCKILL:
2591
1.79k
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2592
0
#ifdef INVARIANTS
2593
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2594
0
            t_type, inp, stcb, net);
2595
#else
2596
      return;
2597
#endif
2598
0
    }
2599
1.79k
    tmr = &stcb->asoc.strreset_timer;
2600
1.79k
    to_ticks = sctp_msecs_to_ticks(SCTP_ASOC_KILL_TIMEOUT);
2601
1.79k
    break;
2602
0
  case SCTP_TIMER_TYPE_ADDR_WQ:
2603
0
    if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2604
0
#ifdef INVARIANTS
2605
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2606
0
            t_type, inp, stcb, net);
2607
#else
2608
      return;
2609
#endif
2610
0
    }
2611
    /* Only 1 tick away :-) */
2612
0
    tmr = &SCTP_BASE_INFO(addr_wq_timer);
2613
0
    to_ticks = SCTP_ADDRESS_TICK_DELAY;
2614
0
    break;
2615
0
  case SCTP_TIMER_TYPE_PRIM_DELETED:
2616
0
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2617
0
#ifdef INVARIANTS
2618
0
      panic("sctp_timer_start of type %d: inp = %p, stcb = %p, net = %p",
2619
0
            t_type, inp, stcb, net);
2620
#else
2621
      return;
2622
#endif
2623
0
    }
2624
0
    tmr = &stcb->asoc.delete_prim_timer;
2625
0
    to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2626
0
    break;
2627
0
  default:
2628
0
#ifdef INVARIANTS
2629
0
    panic("Unknown timer type %d", t_type);
2630
#else
2631
    return;
2632
#endif
2633
128k
  }
2634
128k
  KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2635
128k
  KASSERT(to_ticks > 0, ("to_ticks == 0 for timer type %d", t_type));
2636
128k
  if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
2637
    /*
2638
     * We do NOT allow you to have it already running. If it is,
2639
     * we leave the current one up unchanged.
2640
     */
2641
19.2k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2642
19.2k
            "Timer type %d already running: inp=%p, stcb=%p, net=%p.\n",
2643
19.2k
             t_type, inp, stcb, net);
2644
19.2k
    return;
2645
19.2k
  }
2646
  /* At this point we can proceed. */
2647
108k
  if (t_type == SCTP_TIMER_TYPE_SEND) {
2648
3.01k
    stcb->asoc.num_send_timers_up++;
2649
3.01k
  }
2650
108k
  tmr->stopped_from = 0;
2651
108k
  tmr->type = t_type;
2652
108k
  tmr->ep = (void *)inp;
2653
108k
  tmr->tcb = (void *)stcb;
2654
108k
  if (t_type == SCTP_TIMER_TYPE_STRRESET) {
2655
2.15k
    tmr->net = NULL;
2656
106k
  } else {
2657
106k
    tmr->net = (void *)net;
2658
106k
  }
2659
108k
  tmr->self = (void *)tmr;
2660
#if defined(__FreeBSD__) && !defined(__Userspace__)
2661
  tmr->vnet = (void *)curvnet;
2662
#endif
2663
108k
  tmr->ticks = sctp_get_tick_count();
2664
108k
  if (SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr) == 0) {
2665
108k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2666
108k
            "Timer type %d started: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2667
108k
             t_type, to_ticks, inp, stcb, net);
2668
    /*
2669
     * If this is a newly scheduled callout, as opposed to a
2670
     * rescheduled one, increment relevant reference counts.
2671
     */
2672
108k
    if (tmr->ep != NULL) {
2673
108k
      SCTP_INP_INCR_REF(inp);
2674
108k
    }
2675
108k
    if (tmr->tcb != NULL) {
2676
98.3k
      atomic_add_int(&stcb->asoc.refcnt, 1);
2677
98.3k
    }
2678
108k
    if (tmr->net != NULL) {
2679
94.2k
      atomic_add_int(&net->ref_count, 1);
2680
94.2k
    }
2681
18.4E
  } else {
2682
    /*
2683
     * This should not happen, since we checked for pending
2684
     * above.
2685
     */
2686
18.4E
    SCTPDBG(SCTP_DEBUG_TIMER2,
2687
18.4E
            "Timer type %d restarted: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2688
18.4E
             t_type, to_ticks, inp, stcb, net);
2689
18.4E
  }
2690
108k
  return;
2691
128k
}
2692
2693
/*-
2694
 * The following table shows which parameters must be provided
2695
 * when calling sctp_timer_stop(). For parameters not being
2696
 * provided, NULL must be used.
2697
 *
2698
 * |Name                         |inp |stcb|net |
2699
 * |-----------------------------|----|----|----|
2700
 * |SCTP_TIMER_TYPE_SEND         |Yes |Yes |Yes |
2701
 * |SCTP_TIMER_TYPE_INIT         |Yes |Yes |Yes |
2702
 * |SCTP_TIMER_TYPE_RECV         |Yes |Yes |No  |
2703
 * |SCTP_TIMER_TYPE_SHUTDOWN     |Yes |Yes |Yes |
2704
 * |SCTP_TIMER_TYPE_HEARTBEAT    |Yes |Yes |Yes |
2705
 * |SCTP_TIMER_TYPE_COOKIE       |Yes |Yes |Yes |
2706
 * |SCTP_TIMER_TYPE_NEWCOOKIE    |Yes |No  |No  |
2707
 * |SCTP_TIMER_TYPE_PATHMTURAISE |Yes |Yes |Yes |
2708
 * |SCTP_TIMER_TYPE_SHUTDOWNACK  |Yes |Yes |Yes |
2709
 * |SCTP_TIMER_TYPE_ASCONF       |Yes |Yes |No  |
2710
 * |SCTP_TIMER_TYPE_SHUTDOWNGUARD|Yes |Yes |No  |
2711
 * |SCTP_TIMER_TYPE_AUTOCLOSE    |Yes |Yes |No  |
2712
 * |SCTP_TIMER_TYPE_STRRESET     |Yes |Yes |No  |
2713
 * |SCTP_TIMER_TYPE_INPKILL      |Yes |No  |No  |
2714
 * |SCTP_TIMER_TYPE_ASOCKILL     |Yes |Yes |No  |
2715
 * |SCTP_TIMER_TYPE_ADDR_WQ      |No  |No  |No  |
2716
 * |SCTP_TIMER_TYPE_PRIM_DELETED |Yes |Yes |No  |
2717
 *
2718
 */
2719
2720
void
2721
sctp_timer_stop(int t_type, struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2722
    struct sctp_nets *net, uint32_t from)
2723
442k
{
2724
442k
  struct sctp_timer *tmr;
2725
2726
442k
  KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2727
442k
          ("sctp_timer_stop of type %d: inp = %p, stcb->sctp_ep %p",
2728
442k
           t_type, stcb, stcb->sctp_ep));
2729
442k
  if (stcb != NULL) {
2730
431k
    SCTP_TCB_LOCK_ASSERT(stcb);
2731
431k
  } else if (inp != NULL) {
2732
10.5k
    SCTP_INP_WLOCK_ASSERT(inp);
2733
10.5k
  } else {
2734
0
    SCTP_WQ_ADDR_LOCK_ASSERT();
2735
0
  }
2736
442k
  tmr = NULL;
2737
442k
  switch (t_type) {
2738
29.5k
  case SCTP_TIMER_TYPE_SEND:
2739
29.5k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2740
0
#ifdef INVARIANTS
2741
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2742
0
            t_type, inp, stcb, net);
2743
#else
2744
      return;
2745
#endif
2746
0
    }
2747
29.5k
    tmr = &net->rxt_timer;
2748
29.5k
    break;
2749
53.7k
  case SCTP_TIMER_TYPE_INIT:
2750
53.7k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2751
0
#ifdef INVARIANTS
2752
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2753
0
            t_type, inp, stcb, net);
2754
#else
2755
      return;
2756
#endif
2757
0
    }
2758
53.7k
    tmr = &net->rxt_timer;
2759
53.7k
    break;
2760
28.2k
  case SCTP_TIMER_TYPE_RECV:
2761
28.2k
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2762
0
#ifdef INVARIANTS
2763
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2764
0
            t_type, inp, stcb, net);
2765
#else
2766
      return;
2767
#endif
2768
0
    }
2769
28.2k
    tmr = &stcb->asoc.dack_timer;
2770
28.2k
    break;
2771
29.5k
  case SCTP_TIMER_TYPE_SHUTDOWN:
2772
29.5k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2773
0
#ifdef INVARIANTS
2774
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2775
0
            t_type, inp, stcb, net);
2776
#else
2777
      return;
2778
#endif
2779
0
    }
2780
29.5k
    tmr = &net->rxt_timer;
2781
29.5k
    break;
2782
51.3k
  case SCTP_TIMER_TYPE_HEARTBEAT:
2783
51.3k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2784
0
#ifdef INVARIANTS
2785
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2786
0
            t_type, inp, stcb, net);
2787
#else
2788
      return;
2789
#endif
2790
0
    }
2791
51.3k
    tmr = &net->hb_timer;
2792
51.3k
    break;
2793
40.0k
  case SCTP_TIMER_TYPE_COOKIE:
2794
40.0k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2795
0
#ifdef INVARIANTS
2796
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2797
0
            t_type, inp, stcb, net);
2798
#else
2799
      return;
2800
#endif
2801
0
    }
2802
40.0k
    tmr = &net->rxt_timer;
2803
40.0k
    break;
2804
10.5k
  case SCTP_TIMER_TYPE_NEWCOOKIE:
2805
10.5k
    if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2806
0
#ifdef INVARIANTS
2807
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2808
0
            t_type, inp, stcb, net);
2809
#else
2810
      return;
2811
#endif
2812
0
    }
2813
10.5k
    tmr = &inp->sctp_ep.signature_change;
2814
10.5k
    break;
2815
51.3k
  case SCTP_TIMER_TYPE_PATHMTURAISE:
2816
51.3k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2817
0
#ifdef INVARIANTS
2818
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2819
0
            t_type, inp, stcb, net);
2820
#else
2821
      return;
2822
#endif
2823
0
    }
2824
51.3k
    tmr = &net->pmtu_timer;
2825
51.3k
    break;
2826
29.5k
  case SCTP_TIMER_TYPE_SHUTDOWNACK:
2827
29.5k
    if ((inp == NULL) || (stcb == NULL) || (net == NULL)) {
2828
0
#ifdef INVARIANTS
2829
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2830
0
            t_type, inp, stcb, net);
2831
#else
2832
      return;
2833
#endif
2834
0
    }
2835
29.5k
    tmr = &net->rxt_timer;
2836
29.5k
    break;
2837
21.4k
  case SCTP_TIMER_TYPE_ASCONF:
2838
21.4k
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2839
0
#ifdef INVARIANTS
2840
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2841
0
            t_type, inp, stcb, net);
2842
#else
2843
      return;
2844
#endif
2845
0
    }
2846
21.4k
    tmr = &stcb->asoc.asconf_timer;
2847
21.4k
    break;
2848
21.0k
  case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2849
21.0k
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2850
0
#ifdef INVARIANTS
2851
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2852
0
            t_type, inp, stcb, net);
2853
#else
2854
      return;
2855
#endif
2856
0
    }
2857
21.0k
    tmr = &stcb->asoc.shut_guard_timer;
2858
21.0k
    break;
2859
21.0k
  case SCTP_TIMER_TYPE_AUTOCLOSE:
2860
21.0k
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2861
0
#ifdef INVARIANTS
2862
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2863
0
            t_type, inp, stcb, net);
2864
#else
2865
      return;
2866
#endif
2867
0
    }
2868
21.0k
    tmr = &stcb->asoc.autoclose_timer;
2869
21.0k
    break;
2870
21.0k
  case SCTP_TIMER_TYPE_STRRESET:
2871
21.0k
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2872
0
#ifdef INVARIANTS
2873
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2874
0
            t_type, inp, stcb, net);
2875
#else
2876
      return;
2877
#endif
2878
0
    }
2879
21.0k
    tmr = &stcb->asoc.strreset_timer;
2880
21.0k
    break;
2881
18
  case SCTP_TIMER_TYPE_INPKILL:
2882
    /*
2883
     * The inp is setup to die. We re-use the signature_change
2884
     * timer since that has stopped and we are in the GONE
2885
     * state.
2886
     */
2887
18
    if ((inp == NULL) || (stcb != NULL) || (net != NULL)) {
2888
0
#ifdef INVARIANTS
2889
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2890
0
            t_type, inp, stcb, net);
2891
#else
2892
      return;
2893
#endif
2894
0
    }
2895
18
    tmr = &inp->sctp_ep.signature_change;
2896
18
    break;
2897
12.3k
  case SCTP_TIMER_TYPE_ASOCKILL:
2898
12.3k
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2899
0
#ifdef INVARIANTS
2900
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2901
0
            t_type, inp, stcb, net);
2902
#else
2903
      return;
2904
#endif
2905
0
    }
2906
12.3k
    tmr = &stcb->asoc.strreset_timer;
2907
12.3k
    break;
2908
0
  case SCTP_TIMER_TYPE_ADDR_WQ:
2909
0
    if ((inp != NULL) || (stcb != NULL) || (net != NULL)) {
2910
0
#ifdef INVARIANTS
2911
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2912
0
            t_type, inp, stcb, net);
2913
#else
2914
      return;
2915
#endif
2916
0
    }
2917
0
    tmr = &SCTP_BASE_INFO(addr_wq_timer);
2918
0
    break;
2919
21.0k
  case SCTP_TIMER_TYPE_PRIM_DELETED:
2920
21.0k
    if ((inp == NULL) || (stcb == NULL) || (net != NULL)) {
2921
0
#ifdef INVARIANTS
2922
0
      panic("sctp_timer_stop of type %d: inp = %p, stcb = %p, net = %p",
2923
0
            t_type, inp, stcb, net);
2924
#else
2925
      return;
2926
#endif
2927
0
    }
2928
21.0k
    tmr = &stcb->asoc.delete_prim_timer;
2929
21.0k
    break;
2930
0
  default:
2931
0
#ifdef INVARIANTS
2932
0
    panic("Unknown timer type %d", t_type);
2933
#else
2934
    return;
2935
#endif
2936
442k
  }
2937
442k
  KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2938
442k
  if ((tmr->type != SCTP_TIMER_TYPE_NONE) &&
2939
442k
      (tmr->type != t_type)) {
2940
    /*
2941
     * Ok we have a timer that is under joint use. Cookie timer
2942
     * per chance with the SEND timer. We therefore are NOT
2943
     * running the timer that the caller wants stopped.  So just
2944
     * return.
2945
     */
2946
87.5k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2947
87.5k
            "Shared timer type %d not running: inp=%p, stcb=%p, net=%p.\n",
2948
87.5k
             t_type, inp, stcb, net);
2949
87.5k
    return;
2950
87.5k
  }
2951
354k
  if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2952
14.2k
    stcb->asoc.num_send_timers_up--;
2953
14.2k
    if (stcb->asoc.num_send_timers_up < 0) {
2954
11.2k
      stcb->asoc.num_send_timers_up = 0;
2955
11.2k
    }
2956
14.2k
  }
2957
354k
  tmr->self = NULL;
2958
354k
  tmr->stopped_from = from;
2959
354k
  if (SCTP_OS_TIMER_STOP(&tmr->timer) == 1) {
2960
107k
    KASSERT(tmr->ep == inp,
2961
107k
            ("sctp_timer_stop of type %d: inp = %p, tmr->inp = %p",
2962
107k
             t_type, inp, tmr->ep));
2963
107k
    KASSERT(tmr->tcb == stcb,
2964
107k
            ("sctp_timer_stop of type %d: stcb = %p, tmr->stcb = %p",
2965
107k
             t_type, stcb, tmr->tcb));
2966
107k
    KASSERT(((t_type == SCTP_TIMER_TYPE_ASCONF) && (tmr->net != NULL)) ||
2967
107k
            ((t_type != SCTP_TIMER_TYPE_ASCONF) && (tmr->net == net)),
2968
107k
            ("sctp_timer_stop of type %d: net = %p, tmr->net = %p",
2969
107k
             t_type, net, tmr->net));
2970
107k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2971
107k
            "Timer type %d stopped: inp=%p, stcb=%p, net=%p.\n",
2972
107k
            t_type, inp, stcb, net);
2973
    /*
2974
     * If the timer was actually stopped, decrement reference counts
2975
     * that were incremented in sctp_timer_start().
2976
     */
2977
107k
    if (tmr->ep != NULL) {
2978
107k
      tmr->ep = NULL;
2979
107k
      SCTP_INP_DECR_REF(inp);
2980
107k
    }
2981
107k
    if (tmr->tcb != NULL) {
2982
96.5k
      tmr->tcb = NULL;
2983
96.5k
      atomic_subtract_int(&stcb->asoc.refcnt, 1);
2984
96.5k
    }
2985
107k
    if (tmr->net != NULL) {
2986
94.2k
      struct sctp_nets *tmr_net;
2987
2988
      /*
2989
       * Can't use net, since it doesn't work for
2990
       * SCTP_TIMER_TYPE_ASCONF.
2991
       */
2992
94.2k
      tmr_net = tmr->net;
2993
94.2k
      tmr->net = NULL;
2994
94.2k
      sctp_free_remote_addr(tmr_net);
2995
94.2k
    }
2996
247k
  } else {
2997
247k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2998
247k
            "Timer type %d not stopped: inp=%p, stcb=%p, net=%p.\n",
2999
247k
             t_type, inp, stcb, net);
3000
247k
  }
3001
354k
  return;
3002
354k
}
3003
3004
uint32_t
3005
sctp_calculate_len(struct mbuf *m)
3006
52.5k
{
3007
52.5k
  struct mbuf *at;
3008
52.5k
  uint32_t tlen;
3009
3010
52.5k
  tlen = 0;
3011
200k
  for (at = m; at != NULL; at = SCTP_BUF_NEXT(at)) {
3012
147k
    tlen += SCTP_BUF_LEN(at);
3013
147k
  }
3014
52.5k
  return (tlen);
3015
52.5k
}
3016
3017
/*
3018
 * Given an association and starting time of the current RTT period, update
3019
 * RTO in number of msecs. net should point to the current network.
3020
 * Return 1, if an RTO update was performed, return 0 if no update was
3021
 * performed due to invalid starting point.
3022
 */
3023
3024
int
3025
sctp_calculate_rto(struct sctp_tcb *stcb,
3026
       struct sctp_association *asoc,
3027
       struct sctp_nets *net,
3028
       struct timeval *old,
3029
       int rtt_from_sack)
3030
15.1k
{
3031
15.1k
  struct timeval now;
3032
15.1k
  uint64_t rtt_us;  /* RTT in us */
3033
15.1k
  int32_t rtt;    /* RTT in ms */
3034
15.1k
  uint32_t new_rto;
3035
15.1k
  int first_measure = 0;
3036
3037
  /************************/
3038
  /* 1. calculate new RTT */
3039
  /************************/
3040
  /* get the current time */
3041
15.1k
  if (stcb->asoc.use_precise_time) {
3042
0
    (void)SCTP_GETPTIME_TIMEVAL(&now);
3043
15.1k
  } else {
3044
15.1k
    (void)SCTP_GETTIME_TIMEVAL(&now);
3045
15.1k
  }
3046
15.1k
  if ((old->tv_sec > now.tv_sec) ||
3047
15.1k
      ((old->tv_sec == now.tv_sec) && (old->tv_usec > now.tv_usec))) {
3048
    /* The starting point is in the future. */
3049
321
    return (0);
3050
321
  }
3051
14.8k
  timevalsub(&now, old);
3052
14.8k
  rtt_us = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
3053
14.8k
  if (rtt_us > SCTP_RTO_UPPER_BOUND * 1000) {
3054
    /* The RTT is larger than a sane value. */
3055
1.72k
    return (0);
3056
1.72k
  }
3057
  /* store the current RTT in us */
3058
13.0k
  net->rtt = rtt_us;
3059
  /* compute rtt in ms */
3060
13.0k
  rtt = (int32_t)(net->rtt / 1000);
3061
13.0k
  if ((asoc->cc_functions.sctp_rtt_calculated) && (rtt_from_sack == SCTP_RTT_FROM_DATA)) {
3062
    /* Tell the CC module that a new update has just occurred from a sack */
3063
0
    (*asoc->cc_functions.sctp_rtt_calculated)(stcb, net, &now);
3064
0
  }
3065
  /* Do we need to determine the lan? We do this only
3066
   * on sacks i.e. RTT being determined from data not
3067
   * non-data (HB/INIT->INITACK).
3068
   */
3069
13.0k
  if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
3070
13.0k
      (net->lan_type == SCTP_LAN_UNKNOWN)) {
3071
0
    if (net->rtt > SCTP_LOCAL_LAN_RTT) {
3072
0
      net->lan_type = SCTP_LAN_INTERNET;
3073
0
    } else {
3074
0
      net->lan_type = SCTP_LAN_LOCAL;
3075
0
    }
3076
0
  }
3077
3078
  /***************************/
3079
  /* 2. update RTTVAR & SRTT */
3080
  /***************************/
3081
  /*-
3082
   * Compute the scaled average lastsa and the
3083
   * scaled variance lastsv as described in van Jacobson
3084
   * Paper "Congestion Avoidance and Control", Annex A.
3085
   *
3086
   * (net->lastsa >> SCTP_RTT_SHIFT) is the srtt
3087
   * (net->lastsv >> SCTP_RTT_VAR_SHIFT) is the rttvar
3088
   */
3089
13.0k
  if (net->RTO_measured) {
3090
5.21k
    rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
3091
5.21k
    net->lastsa += rtt;
3092
5.21k
    if (rtt < 0) {
3093
0
      rtt = -rtt;
3094
0
    }
3095
5.21k
    rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
3096
5.21k
    net->lastsv += rtt;
3097
5.21k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
3098
0
      rto_logging(net, SCTP_LOG_RTTVAR);
3099
0
    }
3100
7.87k
  } else {
3101
    /* First RTO measurement */
3102
7.87k
    net->RTO_measured = 1;
3103
7.87k
    first_measure = 1;
3104
7.87k
    net->lastsa = rtt << SCTP_RTT_SHIFT;
3105
7.87k
    net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
3106
7.87k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
3107
0
      rto_logging(net, SCTP_LOG_INITIAL_RTT);
3108
0
    }
3109
7.87k
  }
3110
13.0k
  if (net->lastsv == 0) {
3111
7.87k
    net->lastsv = SCTP_CLOCK_GRANULARITY;
3112
7.87k
  }
3113
13.0k
  new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
3114
13.0k
  if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
3115
13.0k
      (stcb->asoc.sat_network_lockout == 0)) {
3116
0
    stcb->asoc.sat_network = 1;
3117
13.0k
  } else if ((!first_measure) && stcb->asoc.sat_network) {
3118
0
    stcb->asoc.sat_network = 0;
3119
0
    stcb->asoc.sat_network_lockout = 1;
3120
0
  }
3121
  /* bound it, per C6/C7 in Section 5.3.1 */
3122
13.0k
  if (new_rto < stcb->asoc.minrto) {
3123
13.0k
    new_rto = stcb->asoc.minrto;
3124
13.0k
  }
3125
13.0k
  if (new_rto > stcb->asoc.maxrto) {
3126
0
    new_rto = stcb->asoc.maxrto;
3127
0
  }
3128
13.0k
  net->RTO = new_rto;
3129
13.0k
  return (1);
3130
14.8k
}
3131
3132
/*
3133
 * return a pointer to a contiguous piece of data from the given mbuf chain
3134
 * starting at 'off' for 'len' bytes.  If the desired piece spans more than
3135
 * one mbuf, a copy is made at 'ptr'. caller must ensure that the buffer size
3136
 * is >= 'len' returns NULL if there there isn't 'len' bytes in the chain.
3137
 */
3138
caddr_t
3139
sctp_m_getptr(struct mbuf *m, int off, int len, uint8_t * in_ptr)
3140
2.88M
{
3141
2.88M
  uint32_t count;
3142
2.88M
  uint8_t *ptr;
3143
3144
2.88M
  ptr = in_ptr;
3145
2.88M
  if ((off < 0) || (len <= 0))
3146
0
    return (NULL);
3147
3148
  /* find the desired start location */
3149
18.1M
  while ((m != NULL) && (off > 0)) {
3150
18.0M
    if (off < SCTP_BUF_LEN(m))
3151
2.86M
      break;
3152
15.2M
    off -= SCTP_BUF_LEN(m);
3153
15.2M
    m = SCTP_BUF_NEXT(m);
3154
15.2M
  }
3155
2.88M
  if (m == NULL)
3156
8.69k
    return (NULL);
3157
3158
  /* is the current mbuf large enough (eg. contiguous)? */
3159
2.87M
  if ((SCTP_BUF_LEN(m) - off) >= len) {
3160
2.86M
    return (mtod(m, caddr_t) + off);
3161
2.86M
  } else {
3162
    /* else, it spans more than one mbuf, so save a temp copy... */
3163
16.9k
    while ((m != NULL) && (len > 0)) {
3164
10.8k
      count = min(SCTP_BUF_LEN(m) - off, len);
3165
10.8k
      memcpy(ptr, mtod(m, caddr_t) + off, count);
3166
10.8k
      len -= count;
3167
10.8k
      ptr += count;
3168
10.8k
      off = 0;
3169
10.8k
      m = SCTP_BUF_NEXT(m);
3170
10.8k
    }
3171
6.16k
    if ((m == NULL) && (len > 0))
3172
1.84k
      return (NULL);
3173
4.32k
    else
3174
4.32k
      return ((caddr_t)in_ptr);
3175
6.16k
  }
3176
2.87M
}
3177
3178
struct sctp_paramhdr *
3179
sctp_get_next_param(struct mbuf *m,
3180
    int offset,
3181
    struct sctp_paramhdr *pull,
3182
    int pull_limit)
3183
326k
{
3184
  /* This just provides a typed signature to Peter's Pull routine */
3185
326k
  return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
3186
326k
      (uint8_t *) pull));
3187
326k
}
3188
3189
struct mbuf *
3190
sctp_add_pad_tombuf(struct mbuf *m, int padlen)
3191
32.7k
{
3192
32.7k
  struct mbuf *m_last;
3193
32.7k
  caddr_t dp;
3194
3195
32.7k
  if (padlen > 3) {
3196
0
    return (NULL);
3197
0
  }
3198
32.7k
  if (padlen <= M_TRAILINGSPACE(m)) {
3199
    /*
3200
     * The easy way. We hope the majority of the time we hit
3201
     * here :)
3202
     */
3203
28.1k
    m_last = m;
3204
28.1k
  } else {
3205
    /* Hard way we must grow the mbuf chain */
3206
4.63k
    m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
3207
4.63k
    if (m_last == NULL) {
3208
0
      return (NULL);
3209
0
    }
3210
4.63k
    SCTP_BUF_LEN(m_last) = 0;
3211
4.63k
    SCTP_BUF_NEXT(m_last) = NULL;
3212
4.63k
    SCTP_BUF_NEXT(m) = m_last;
3213
4.63k
  }
3214
32.7k
  dp = mtod(m_last, caddr_t) + SCTP_BUF_LEN(m_last);
3215
32.7k
  SCTP_BUF_LEN(m_last) += padlen;
3216
32.7k
  memset(dp, 0, padlen);
3217
32.7k
  return (m_last);
3218
32.7k
}
3219
3220
struct mbuf *
3221
sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
3222
7.92k
{
3223
  /* find the last mbuf in chain and pad it */
3224
7.92k
  struct mbuf *m_at;
3225
3226
7.92k
  if (last_mbuf != NULL) {
3227
4.92k
    return (sctp_add_pad_tombuf(last_mbuf, padval));
3228
4.92k
  } else {
3229
3.14k
    for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3230
3.14k
      if (SCTP_BUF_NEXT(m_at) == NULL) {
3231
3.00k
        return (sctp_add_pad_tombuf(m_at, padval));
3232
3.00k
      }
3233
3.14k
    }
3234
3.00k
  }
3235
0
  return (NULL);
3236
7.92k
}
3237
3238
static void
3239
sctp_notify_assoc_change(uint16_t state, struct sctp_tcb *stcb,
3240
                         uint16_t error, struct sctp_abort_chunk *abort,
3241
                         bool from_peer, bool timedout, int so_locked)
3242
6.73k
{
3243
6.73k
  struct mbuf *m_notify;
3244
6.73k
  struct sctp_assoc_change *sac;
3245
6.73k
  struct sctp_queued_to_read *control;
3246
6.73k
  struct sctp_inpcb *inp;
3247
6.73k
  unsigned int notif_len;
3248
6.73k
  unsigned int i;
3249
6.73k
  uint16_t abort_len;
3250
#if defined(__APPLE__) && !defined(__Userspace__)
3251
  struct socket *so;
3252
#endif
3253
3254
6.73k
  KASSERT(abort == NULL || from_peer,
3255
6.73k
          ("sctp_notify_assoc_change: ABORT chunk provided for local termination"));
3256
6.73k
  KASSERT(!from_peer || !timedout,
3257
6.73k
          ("sctp_notify_assoc_change: timeouts can only be local"));
3258
6.73k
  KASSERT(stcb != NULL, ("stcb == NULL"));
3259
6.73k
  SCTP_TCB_LOCK_ASSERT(stcb);
3260
6.73k
  inp = stcb->sctp_ep;
3261
6.73k
  SCTP_INP_READ_LOCK_ASSERT(inp);
3262
3263
6.73k
  if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
3264
6.73k
    notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3265
6.73k
    if (abort != NULL) {
3266
58
      abort_len = ntohs(abort->ch.chunk_length);
3267
      /*
3268
       * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
3269
       * contiguous.
3270
       */
3271
58
      if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
3272
1
        abort_len = SCTP_CHUNK_BUFFER_SIZE;
3273
1
      }
3274
6.68k
    } else {
3275
6.68k
      abort_len = 0;
3276
6.68k
    }
3277
6.73k
    if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3278
5.17k
      notif_len += SCTP_ASSOC_SUPPORTS_MAX;
3279
5.17k
    } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3280
1.56k
      notif_len += abort_len;
3281
1.56k
    }
3282
6.73k
    m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3283
6.73k
    if (m_notify == NULL) {
3284
      /* Retry with smaller value. */
3285
0
      notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3286
0
      m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3287
0
      if (m_notify == NULL) {
3288
0
        goto set_error;
3289
0
      }
3290
0
    }
3291
6.73k
    SCTP_BUF_NEXT(m_notify) = NULL;
3292
6.73k
    sac = mtod(m_notify, struct sctp_assoc_change *);
3293
6.73k
    memset(sac, 0, notif_len);
3294
6.73k
    sac->sac_type = SCTP_ASSOC_CHANGE;
3295
6.73k
    sac->sac_flags = 0;
3296
6.73k
    sac->sac_length = sizeof(struct sctp_assoc_change);
3297
6.73k
    sac->sac_state = state;
3298
6.73k
    sac->sac_error = error;
3299
6.73k
    if (state == SCTP_CANT_STR_ASSOC) {
3300
649
      sac->sac_outbound_streams = 0;
3301
649
      sac->sac_inbound_streams = 0;
3302
6.09k
    } else {
3303
6.09k
      sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
3304
6.09k
      sac->sac_inbound_streams = stcb->asoc.streamincnt;
3305
6.09k
    }
3306
6.73k
    sac->sac_assoc_id = sctp_get_associd(stcb);
3307
6.73k
    if (notif_len > sizeof(struct sctp_assoc_change)) {
3308
5.22k
      if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3309
5.17k
        i = 0;
3310
5.17k
        if (stcb->asoc.prsctp_supported == 1) {
3311
5.16k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
3312
5.16k
        }
3313
5.17k
        if (stcb->asoc.auth_supported == 1) {
3314
5.17k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
3315
5.17k
        }
3316
5.17k
        if (stcb->asoc.asconf_supported == 1) {
3317
5.16k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
3318
5.16k
        }
3319
5.17k
        if (stcb->asoc.idata_supported == 1) {
3320
3.03k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
3321
3.03k
        }
3322
5.17k
        sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
3323
5.17k
        if (stcb->asoc.reconfig_supported == 1) {
3324
5.17k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
3325
5.17k
        }
3326
5.17k
        sac->sac_length += i;
3327
5.17k
      } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3328
58
        memcpy(sac->sac_info, abort, abort_len);
3329
58
        sac->sac_length += abort_len;
3330
58
      }
3331
5.22k
    }
3332
6.73k
    SCTP_BUF_LEN(m_notify) = sac->sac_length;
3333
6.73k
    control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3334
6.73k
                                     0, 0, stcb->asoc.context, 0, 0, 0,
3335
6.73k
                                     m_notify);
3336
6.73k
    if (control != NULL) {
3337
6.73k
      control->length = SCTP_BUF_LEN(m_notify);
3338
6.73k
      control->spec_flags = M_NOTIFICATION;
3339
      /* not that we need this */
3340
6.73k
      control->tail_mbuf = m_notify;
3341
6.73k
      sctp_add_to_readq(inp, stcb, control,
3342
6.73k
                        &stcb->sctp_socket->so_rcv, 1,
3343
6.73k
                        SCTP_READ_LOCK_HELD, so_locked);
3344
6.73k
    } else {
3345
0
      sctp_m_freem(m_notify);
3346
0
    }
3347
6.73k
  }
3348
  /*
3349
   * For 1-to-1 style sockets, we send up and error when an ABORT
3350
   * comes in.
3351
   */
3352
6.73k
set_error:
3353
6.73k
  if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3354
6.73k
       (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
3355
6.73k
      ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
3356
1.56k
    SOCK_LOCK(stcb->sctp_socket);
3357
1.56k
    if (from_peer) {
3358
58
      if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
3359
32
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
3360
32
        stcb->sctp_socket->so_error = ECONNREFUSED;
3361
32
      } else {
3362
26
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
3363
26
        stcb->sctp_socket->so_error = ECONNRESET;
3364
26
      }
3365
1.51k
    } else {
3366
1.51k
      if (timedout) {
3367
0
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ETIMEDOUT);
3368
0
        stcb->sctp_socket->so_error = ETIMEDOUT;
3369
1.51k
      } else {
3370
1.51k
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
3371
1.51k
        stcb->sctp_socket->so_error = ECONNABORTED;
3372
1.51k
      }
3373
1.51k
    }
3374
1.56k
    SOCK_UNLOCK(stcb->sctp_socket);
3375
1.56k
  }
3376
  /* Wake ANY sleepers */
3377
#if defined(__APPLE__) && !defined(__Userspace__)
3378
  so = SCTP_INP_SO(inp);
3379
  if (!so_locked) {
3380
    atomic_add_int(&stcb->asoc.refcnt, 1);
3381
    SCTP_TCB_UNLOCK(stcb);
3382
    SCTP_SOCKET_LOCK(so, 1);
3383
    SCTP_TCB_LOCK(stcb);
3384
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
3385
    if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3386
      SCTP_SOCKET_UNLOCK(so, 1);
3387
      return;
3388
    }
3389
  }
3390
#endif
3391
6.73k
  if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3392
6.73k
       (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
3393
6.73k
      ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
3394
1.56k
    socantrcvmore(stcb->sctp_socket);
3395
1.56k
  }
3396
6.73k
  sorwakeup(stcb->sctp_socket);
3397
6.73k
  sowwakeup(stcb->sctp_socket);
3398
#if defined(__APPLE__) && !defined(__Userspace__)
3399
  if (!so_locked) {
3400
    SCTP_SOCKET_UNLOCK(so, 1);
3401
  }
3402
#endif
3403
6.73k
}
3404
3405
static void
3406
sctp_notify_peer_addr_change(struct sctp_tcb *stcb, uint32_t state,
3407
    struct sockaddr *sa, uint32_t error, int so_locked)
3408
53.6k
{
3409
53.6k
  struct mbuf *m_notify;
3410
53.6k
  struct sctp_paddr_change *spc;
3411
53.6k
  struct sctp_queued_to_read *control;
3412
3413
53.6k
  KASSERT(stcb != NULL, ("stcb == NULL"));
3414
53.6k
  SCTP_TCB_LOCK_ASSERT(stcb);
3415
53.6k
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3416
3417
53.6k
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPADDREVNT)) {
3418
    /* event not enabled */
3419
0
    return;
3420
0
  }
3421
3422
53.6k
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
3423
53.6k
  if (m_notify == NULL)
3424
0
    return;
3425
53.6k
  SCTP_BUF_LEN(m_notify) = 0;
3426
53.6k
  spc = mtod(m_notify, struct sctp_paddr_change *);
3427
53.6k
  memset(spc, 0, sizeof(struct sctp_paddr_change));
3428
53.6k
  spc->spc_type = SCTP_PEER_ADDR_CHANGE;
3429
53.6k
  spc->spc_flags = 0;
3430
53.6k
  spc->spc_length = sizeof(struct sctp_paddr_change);
3431
53.6k
  switch (sa->sa_family) {
3432
0
#ifdef INET
3433
47.2k
  case AF_INET:
3434
47.2k
#ifdef INET6
3435
47.2k
    if (sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
3436
0
      in6_sin_2_v4mapsin6((struct sockaddr_in *)sa,
3437
0
                          (struct sockaddr_in6 *)&spc->spc_aaddr);
3438
47.2k
    } else {
3439
47.2k
      memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3440
47.2k
    }
3441
#else
3442
    memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3443
#endif
3444
47.2k
    break;
3445
0
#endif
3446
0
#ifdef INET6
3447
5.80k
  case AF_INET6:
3448
5.80k
  {
3449
#ifdef SCTP_EMBEDDED_V6_SCOPE
3450
    struct sockaddr_in6 *sin6;
3451
#endif /* SCTP_EMBEDDED_V6_SCOPE */
3452
5.80k
    memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in6));
3453
3454
#ifdef SCTP_EMBEDDED_V6_SCOPE
3455
    sin6 = (struct sockaddr_in6 *)&spc->spc_aaddr;
3456
    if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
3457
      if (sin6->sin6_scope_id == 0) {
3458
        /* recover scope_id for user */
3459
#ifdef SCTP_KAME
3460
        (void)sa6_recoverscope(sin6);
3461
#else
3462
        (void)in6_recoverscope(sin6, &sin6->sin6_addr,
3463
                               NULL);
3464
#endif
3465
      } else {
3466
        /* clear embedded scope_id for user */
3467
        in6_clearscope(&sin6->sin6_addr);
3468
      }
3469
    }
3470
#endif /* SCTP_EMBEDDED_V6_SCOPE */
3471
5.80k
    break;
3472
0
  }
3473
0
#endif
3474
0
#if defined(__Userspace__)
3475
542
  case AF_CONN:
3476
542
    memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_conn));
3477
542
    break;
3478
0
#endif
3479
0
  default:
3480
    /* TSNH */
3481
0
    break;
3482
53.6k
  }
3483
53.6k
  spc->spc_state = state;
3484
53.6k
  spc->spc_error = error;
3485
53.6k
  spc->spc_assoc_id = sctp_get_associd(stcb);
3486
3487
53.6k
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
3488
53.6k
  SCTP_BUF_NEXT(m_notify) = NULL;
3489
3490
  /* append to socket */
3491
53.6k
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3492
53.6k
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3493
53.6k
                                   m_notify);
3494
53.6k
  if (control == NULL) {
3495
    /* no memory */
3496
0
    sctp_m_freem(m_notify);
3497
0
    return;
3498
0
  }
3499
53.6k
  control->length = SCTP_BUF_LEN(m_notify);
3500
53.6k
  control->spec_flags = M_NOTIFICATION;
3501
  /* not that we need this */
3502
53.6k
  control->tail_mbuf = m_notify;
3503
53.6k
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3504
53.6k
                    &stcb->sctp_socket->so_rcv, 1,
3505
53.6k
                    SCTP_READ_LOCK_HELD, so_locked);
3506
53.6k
}
3507
3508
static void
3509
sctp_notify_send_failed(struct sctp_tcb *stcb, uint8_t sent, uint32_t error,
3510
                        struct sctp_tmit_chunk *chk, int so_locked)
3511
436
{
3512
436
  struct mbuf *m_notify;
3513
436
  struct sctp_send_failed *ssf;
3514
436
  struct sctp_send_failed_event *ssfe;
3515
436
  struct sctp_queued_to_read *control;
3516
436
  struct sctp_chunkhdr *chkhdr;
3517
436
  int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
3518
3519
436
  KASSERT(stcb != NULL, ("stcb == NULL"));
3520
436
  SCTP_TCB_LOCK_ASSERT(stcb);
3521
436
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3522
3523
436
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3524
436
      sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3525
    /* event not enabled */
3526
0
    return;
3527
0
  }
3528
3529
436
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3530
436
    notifhdr_len = sizeof(struct sctp_send_failed_event);
3531
436
  } else {
3532
0
    notifhdr_len = sizeof(struct sctp_send_failed);
3533
0
  }
3534
436
  m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3535
436
  if (m_notify == NULL)
3536
    /* no space left */
3537
0
    return;
3538
436
  SCTP_BUF_LEN(m_notify) = notifhdr_len;
3539
436
  if (stcb->asoc.idata_supported) {
3540
313
    chkhdr_len = sizeof(struct sctp_idata_chunk);
3541
313
  } else {
3542
123
    chkhdr_len = sizeof(struct sctp_data_chunk);
3543
123
  }
3544
  /* Use some defaults in case we can't access the chunk header */
3545
436
  if (chk->send_size >= chkhdr_len) {
3546
436
    payload_len = chk->send_size - chkhdr_len;
3547
436
  } else {
3548
0
    payload_len = 0;
3549
0
  }
3550
436
  padding_len = 0;
3551
436
  if (chk->data != NULL) {
3552
436
    chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
3553
436
    if (chkhdr != NULL) {
3554
436
      chk_len = ntohs(chkhdr->chunk_length);
3555
436
      if ((chk_len >= chkhdr_len) &&
3556
436
          (chk->send_size >= chk_len) &&
3557
436
          (chk->send_size - chk_len < 4)) {
3558
436
        padding_len = chk->send_size - chk_len;
3559
436
        payload_len = chk->send_size - chkhdr_len - padding_len;
3560
436
      }
3561
436
    }
3562
436
  }
3563
436
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3564
436
    ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3565
436
    memset(ssfe, 0, notifhdr_len);
3566
436
    ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3567
436
    if (sent) {
3568
414
      ssfe->ssfe_flags = SCTP_DATA_SENT;
3569
414
    } else {
3570
22
      ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3571
22
    }
3572
436
    ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
3573
436
    ssfe->ssfe_error = error;
3574
    /* not exactly what the user sent in, but should be close :) */
3575
436
    ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
3576
436
    ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
3577
436
    ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
3578
436
    ssfe->ssfe_info.snd_context = chk->rec.data.context;
3579
436
    ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3580
436
    ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3581
436
  } else {
3582
0
    ssf = mtod(m_notify, struct sctp_send_failed *);
3583
0
    memset(ssf, 0, notifhdr_len);
3584
0
    ssf->ssf_type = SCTP_SEND_FAILED;
3585
0
    if (sent) {
3586
0
      ssf->ssf_flags = SCTP_DATA_SENT;
3587
0
    } else {
3588
0
      ssf->ssf_flags = SCTP_DATA_UNSENT;
3589
0
    }
3590
0
    ssf->ssf_length = (uint32_t)(notifhdr_len + payload_len);
3591
0
    ssf->ssf_error = error;
3592
    /* not exactly what the user sent in, but should be close :) */
3593
0
    ssf->ssf_info.sinfo_stream = chk->rec.data.sid;
3594
0
    ssf->ssf_info.sinfo_ssn = (uint16_t)chk->rec.data.mid;
3595
0
    ssf->ssf_info.sinfo_flags = chk->rec.data.rcv_flags;
3596
0
    ssf->ssf_info.sinfo_ppid = chk->rec.data.ppid;
3597
0
    ssf->ssf_info.sinfo_context = chk->rec.data.context;
3598
0
    ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3599
0
    ssf->ssf_assoc_id = sctp_get_associd(stcb);
3600
0
  }
3601
436
  if (chk->data != NULL) {
3602
    /* Trim off the sctp chunk header (it should be there) */
3603
436
    if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3604
436
      m_adj(chk->data, chkhdr_len);
3605
436
      m_adj(chk->data, -padding_len);
3606
436
      sctp_mbuf_crush(chk->data);
3607
436
      chk->send_size -= (chkhdr_len + padding_len);
3608
436
    }
3609
436
  }
3610
436
  SCTP_BUF_NEXT(m_notify) = chk->data;
3611
  /* Steal off the mbuf */
3612
436
  chk->data = NULL;
3613
  /*
3614
   * For this case, we check the actual socket buffer, since the assoc
3615
   * is going away we don't want to overfill the socket buffer for a
3616
   * non-reader
3617
   */
3618
436
  if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3619
14
    sctp_m_freem(m_notify);
3620
14
    return;
3621
14
  }
3622
  /* append to socket */
3623
422
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3624
422
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3625
422
                                   m_notify);
3626
422
  if (control == NULL) {
3627
    /* no memory */
3628
0
    sctp_m_freem(m_notify);
3629
0
    return;
3630
0
  }
3631
422
  control->length = SCTP_BUF_LEN(m_notify);
3632
422
  control->spec_flags = M_NOTIFICATION;
3633
  /* not that we need this */
3634
422
  control->tail_mbuf = m_notify;
3635
422
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3636
422
                    &stcb->sctp_socket->so_rcv, 1,
3637
422
                    SCTP_READ_LOCK_HELD, so_locked);
3638
422
}
3639
3640
static void
3641
sctp_notify_send_failed2(struct sctp_tcb *stcb, uint32_t error,
3642
                         struct sctp_stream_queue_pending *sp, int so_locked)
3643
496
{
3644
496
  struct mbuf *m_notify;
3645
496
  struct sctp_send_failed *ssf;
3646
496
  struct sctp_send_failed_event *ssfe;
3647
496
  struct sctp_queued_to_read *control;
3648
496
  int notifhdr_len;
3649
3650
496
  KASSERT(stcb != NULL, ("stcb == NULL"));
3651
496
  SCTP_TCB_LOCK_ASSERT(stcb);
3652
496
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3653
3654
496
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3655
496
      sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3656
    /* event not enabled */
3657
0
    return;
3658
0
  }
3659
3660
496
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3661
496
    notifhdr_len = sizeof(struct sctp_send_failed_event);
3662
496
  } else {
3663
0
    notifhdr_len = sizeof(struct sctp_send_failed);
3664
0
  }
3665
496
  m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3666
496
  if (m_notify == NULL) {
3667
    /* no space left */
3668
0
    return;
3669
0
  }
3670
496
  SCTP_BUF_LEN(m_notify) = notifhdr_len;
3671
496
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3672
496
    ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3673
496
    memset(ssfe, 0, notifhdr_len);
3674
496
    ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3675
496
    ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3676
496
    ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
3677
496
    ssfe->ssfe_error = error;
3678
    /* not exactly what the user sent in, but should be close :) */
3679
496
    ssfe->ssfe_info.snd_sid = sp->sid;
3680
496
    if (sp->some_taken) {
3681
0
      ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
3682
496
    } else {
3683
496
      ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
3684
496
    }
3685
496
    ssfe->ssfe_info.snd_ppid = sp->ppid;
3686
496
    ssfe->ssfe_info.snd_context = sp->context;
3687
496
    ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3688
496
    ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3689
496
  } else {
3690
0
    ssf = mtod(m_notify, struct sctp_send_failed *);
3691
0
    memset(ssf, 0, notifhdr_len);
3692
0
    ssf->ssf_type = SCTP_SEND_FAILED;
3693
0
    ssf->ssf_flags = SCTP_DATA_UNSENT;
3694
0
    ssf->ssf_length = (uint32_t)(notifhdr_len + sp->length);
3695
0
    ssf->ssf_error = error;
3696
    /* not exactly what the user sent in, but should be close :) */
3697
0
    ssf->ssf_info.sinfo_stream = sp->sid;
3698
0
    ssf->ssf_info.sinfo_ssn = 0;
3699
0
    if (sp->some_taken) {
3700
0
      ssf->ssf_info.sinfo_flags = SCTP_DATA_LAST_FRAG;
3701
0
    } else {
3702
0
      ssf->ssf_info.sinfo_flags = SCTP_DATA_NOT_FRAG;
3703
0
    }
3704
0
    ssf->ssf_info.sinfo_ppid = sp->ppid;
3705
0
    ssf->ssf_info.sinfo_context = sp->context;
3706
0
    ssf->ssf_info.sinfo_assoc_id = sctp_get_associd(stcb);
3707
0
    ssf->ssf_assoc_id = sctp_get_associd(stcb);
3708
0
  }
3709
496
  SCTP_BUF_NEXT(m_notify) = sp->data;
3710
3711
  /* Steal off the mbuf */
3712
496
  sp->data = NULL;
3713
  /*
3714
   * For this case, we check the actual socket buffer, since the assoc
3715
   * is going away we don't want to overfill the socket buffer for a
3716
   * non-reader
3717
   */
3718
496
  if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3719
9
    sctp_m_freem(m_notify);
3720
9
    return;
3721
9
  }
3722
  /* append to socket */
3723
487
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3724
487
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3725
487
                                   m_notify);
3726
487
  if (control == NULL) {
3727
    /* no memory */
3728
0
    sctp_m_freem(m_notify);
3729
0
    return;
3730
0
  }
3731
487
  control->length = SCTP_BUF_LEN(m_notify);
3732
487
  control->spec_flags = M_NOTIFICATION;
3733
  /* not that we need this */
3734
487
  control->tail_mbuf = m_notify;
3735
487
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3736
487
                    &stcb->sctp_socket->so_rcv, 1,
3737
487
                    SCTP_READ_LOCK_HELD, so_locked);
3738
487
}
3739
3740
static void
3741
sctp_notify_adaptation_layer(struct sctp_tcb *stcb, int so_locked)
3742
5.17k
{
3743
5.17k
  struct mbuf *m_notify;
3744
5.17k
  struct sctp_adaptation_event *sai;
3745
5.17k
  struct sctp_queued_to_read *control;
3746
3747
5.17k
  KASSERT(stcb != NULL, ("stcb == NULL"));
3748
5.17k
  SCTP_TCB_LOCK_ASSERT(stcb);
3749
5.17k
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3750
3751
5.17k
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ADAPTATIONEVNT)) {
3752
    /* event not enabled */
3753
0
    return;
3754
0
  }
3755
3756
5.17k
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3757
5.17k
  if (m_notify == NULL)
3758
    /* no space left */
3759
0
    return;
3760
5.17k
  SCTP_BUF_LEN(m_notify) = 0;
3761
5.17k
  sai = mtod(m_notify, struct sctp_adaptation_event *);
3762
5.17k
  memset(sai, 0, sizeof(struct sctp_adaptation_event));
3763
5.17k
  sai->sai_type = SCTP_ADAPTATION_INDICATION;
3764
5.17k
  sai->sai_flags = 0;
3765
5.17k
  sai->sai_length = sizeof(struct sctp_adaptation_event);
3766
5.17k
  sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3767
5.17k
  sai->sai_assoc_id = sctp_get_associd(stcb);
3768
3769
5.17k
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3770
5.17k
  SCTP_BUF_NEXT(m_notify) = NULL;
3771
3772
  /* append to socket */
3773
5.17k
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3774
5.17k
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3775
5.17k
                                   m_notify);
3776
5.17k
  if (control == NULL) {
3777
    /* no memory */
3778
0
    sctp_m_freem(m_notify);
3779
0
    return;
3780
0
  }
3781
5.17k
  control->length = SCTP_BUF_LEN(m_notify);
3782
5.17k
  control->spec_flags = M_NOTIFICATION;
3783
  /* not that we need this */
3784
5.17k
  control->tail_mbuf = m_notify;
3785
5.17k
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3786
5.17k
                    &stcb->sctp_socket->so_rcv, 1,
3787
5.17k
                    SCTP_READ_LOCK_HELD, so_locked);
3788
5.17k
}
3789
3790
static void
3791
sctp_notify_partial_delivery_indication(struct sctp_tcb *stcb, uint32_t error,
3792
                                        struct sctp_queued_to_read *aborted_control,
3793
                                        int so_locked)
3794
879
{
3795
879
  struct mbuf *m_notify;
3796
879
  struct sctp_pdapi_event *pdapi;
3797
879
  struct sctp_queued_to_read *control;
3798
879
  struct sockbuf *sb;
3799
3800
879
  KASSERT(aborted_control != NULL, ("aborted_control is NULL"));
3801
879
  KASSERT(stcb != NULL, ("stcb == NULL"));
3802
879
  SCTP_TCB_LOCK_ASSERT(stcb);
3803
879
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3804
3805
879
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_PDAPIEVNT)) {
3806
    /* event not enabled */
3807
0
    return;
3808
0
  }
3809
3810
879
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3811
879
  if (m_notify == NULL)
3812
    /* no space left */
3813
0
    return;
3814
879
  SCTP_BUF_LEN(m_notify) = 0;
3815
879
  pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3816
879
  memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3817
879
  pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3818
879
  pdapi->pdapi_flags = 0;
3819
879
  pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3820
879
  pdapi->pdapi_indication = error;
3821
879
  pdapi->pdapi_stream = aborted_control->sinfo_stream;
3822
879
  pdapi->pdapi_seq = (uint16_t)aborted_control->mid;
3823
879
  pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3824
3825
879
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3826
879
  SCTP_BUF_NEXT(m_notify) = NULL;
3827
879
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3828
879
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3829
879
                                   m_notify);
3830
879
  if (control == NULL) {
3831
    /* no memory */
3832
0
    sctp_m_freem(m_notify);
3833
0
    return;
3834
0
  }
3835
879
  control->length = SCTP_BUF_LEN(m_notify);
3836
879
  control->spec_flags = M_NOTIFICATION;
3837
  /* not that we need this */
3838
879
  control->tail_mbuf = m_notify;
3839
879
  sb = &stcb->sctp_socket->so_rcv;
3840
879
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3841
0
    sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m_notify));
3842
0
  }
3843
879
  sctp_sballoc(stcb, sb, m_notify);
3844
879
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
3845
0
    sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
3846
0
  }
3847
879
  control->end_added = 1;
3848
879
  TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, aborted_control, control, next);
3849
879
  if (stcb->sctp_ep && stcb->sctp_socket) {
3850
    /* This should always be the case */
3851
#if defined(__APPLE__) && !defined(__Userspace__)
3852
    struct socket *so;
3853
3854
    so = SCTP_INP_SO(stcb->sctp_ep);
3855
    if (!so_locked) {
3856
      atomic_add_int(&stcb->asoc.refcnt, 1);
3857
      SCTP_TCB_UNLOCK(stcb);
3858
      SCTP_SOCKET_LOCK(so, 1);
3859
      SCTP_TCB_LOCK(stcb);
3860
      atomic_subtract_int(&stcb->asoc.refcnt, 1);
3861
      if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
3862
        SCTP_SOCKET_UNLOCK(so, 1);
3863
        return;
3864
      }
3865
    }
3866
#endif
3867
879
    sctp_sorwakeup(stcb->sctp_ep, stcb->sctp_socket);
3868
#if defined(__APPLE__) && !defined(__Userspace__)
3869
    if (!so_locked) {
3870
      SCTP_SOCKET_UNLOCK(so, 1);
3871
    }
3872
#endif
3873
879
  }
3874
879
}
3875
3876
static void
3877
sctp_notify_shutdown_event(struct sctp_tcb *stcb, int so_locked)
3878
84
{
3879
84
  struct mbuf *m_notify;
3880
84
  struct sctp_shutdown_event *sse;
3881
84
  struct sctp_queued_to_read *control;
3882
3883
84
  KASSERT(stcb != NULL, ("stcb == NULL"));
3884
84
  SCTP_TCB_LOCK_ASSERT(stcb);
3885
84
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3886
3887
  /*
3888
   * For TCP model AND UDP connected sockets we will send an error up
3889
   * when an SHUTDOWN completes
3890
   */
3891
84
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3892
84
      (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
3893
    /* mark socket closed for read/write and wakeup! */
3894
#if defined(__APPLE__) && !defined(__Userspace__)
3895
    struct socket *so;
3896
3897
    so = SCTP_INP_SO(stcb->sctp_ep);
3898
    atomic_add_int(&stcb->asoc.refcnt, 1);
3899
    SCTP_TCB_UNLOCK(stcb);
3900
    SCTP_SOCKET_LOCK(so, 1);
3901
    SCTP_TCB_LOCK(stcb);
3902
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
3903
    if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
3904
      SCTP_SOCKET_UNLOCK(so, 1);
3905
      return;
3906
    }
3907
#endif
3908
84
    socantsendmore(stcb->sctp_socket);
3909
#if defined(__APPLE__) && !defined(__Userspace__)
3910
    SCTP_SOCKET_UNLOCK(so, 1);
3911
#endif
3912
84
  }
3913
3914
84
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSHUTDOWNEVNT)) {
3915
    /* event not enabled */
3916
0
    return;
3917
0
  }
3918
3919
84
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3920
84
  if (m_notify == NULL)
3921
    /* no space left */
3922
0
    return;
3923
84
  sse = mtod(m_notify, struct sctp_shutdown_event *);
3924
84
  memset(sse, 0, sizeof(struct sctp_shutdown_event));
3925
84
  sse->sse_type = SCTP_SHUTDOWN_EVENT;
3926
84
  sse->sse_flags = 0;
3927
84
  sse->sse_length = sizeof(struct sctp_shutdown_event);
3928
84
  sse->sse_assoc_id = sctp_get_associd(stcb);
3929
3930
84
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3931
84
  SCTP_BUF_NEXT(m_notify) = NULL;
3932
3933
  /* append to socket */
3934
84
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3935
84
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3936
84
                                   m_notify);
3937
84
  if (control == NULL) {
3938
    /* no memory */
3939
0
    sctp_m_freem(m_notify);
3940
0
    return;
3941
0
  }
3942
84
  control->length = SCTP_BUF_LEN(m_notify);
3943
84
  control->spec_flags = M_NOTIFICATION;
3944
  /* not that we need this */
3945
84
  control->tail_mbuf = m_notify;
3946
84
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3947
84
                    &stcb->sctp_socket->so_rcv, 1,
3948
84
                    SCTP_READ_LOCK_HELD, so_locked);
3949
84
}
3950
3951
static void
3952
sctp_notify_sender_dry_event(struct sctp_tcb *stcb, int so_locked)
3953
267
{
3954
267
  struct mbuf *m_notify;
3955
267
  struct sctp_sender_dry_event *event;
3956
267
  struct sctp_queued_to_read *control;
3957
3958
267
  KASSERT(stcb != NULL, ("stcb == NULL"));
3959
267
  SCTP_TCB_LOCK_ASSERT(stcb);
3960
267
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3961
3962
267
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_DRYEVNT)) {
3963
    /* event not enabled */
3964
0
    return;
3965
0
  }
3966
3967
267
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3968
267
  if (m_notify == NULL) {
3969
    /* no space left */
3970
0
    return;
3971
0
  }
3972
267
  SCTP_BUF_LEN(m_notify) = 0;
3973
267
  event = mtod(m_notify, struct sctp_sender_dry_event *);
3974
267
  memset(event, 0, sizeof(struct sctp_sender_dry_event));
3975
267
  event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3976
267
  event->sender_dry_flags = 0;
3977
267
  event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3978
267
  event->sender_dry_assoc_id = sctp_get_associd(stcb);
3979
3980
267
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3981
267
  SCTP_BUF_NEXT(m_notify) = NULL;
3982
3983
  /* append to socket */
3984
267
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3985
267
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3986
267
                                   m_notify);
3987
267
  if (control == NULL) {
3988
    /* no memory */
3989
0
    sctp_m_freem(m_notify);
3990
0
    return;
3991
0
  }
3992
267
  control->length = SCTP_BUF_LEN(m_notify);
3993
267
  control->spec_flags = M_NOTIFICATION;
3994
  /* not that we need this */
3995
267
  control->tail_mbuf = m_notify;
3996
267
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3997
267
                    &stcb->sctp_socket->so_rcv, 1,
3998
267
                    SCTP_READ_LOCK_HELD, so_locked);
3999
267
}
4000
4001
static void
4002
sctp_notify_stream_reset_add(struct sctp_tcb *stcb, int flag, int so_locked)
4003
31
{
4004
31
  struct mbuf *m_notify;
4005
31
  struct sctp_queued_to_read *control;
4006
31
  struct sctp_stream_change_event *stradd;
4007
4008
31
  KASSERT(stcb != NULL, ("stcb == NULL"));
4009
31
  SCTP_TCB_LOCK_ASSERT(stcb);
4010
31
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
4011
4012
31
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_CHANGEEVNT)) {
4013
    /* event not enabled */
4014
0
    return;
4015
0
  }
4016
4017
31
  if ((stcb->asoc.peer_req_out) && flag) {
4018
    /* Peer made the request, don't tell the local user */
4019
0
    stcb->asoc.peer_req_out = 0;
4020
0
    return;
4021
0
  }
4022
31
  stcb->asoc.peer_req_out = 0;
4023
31
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
4024
31
  if (m_notify == NULL)
4025
    /* no space left */
4026
0
    return;
4027
31
  SCTP_BUF_LEN(m_notify) = 0;
4028
31
  stradd = mtod(m_notify, struct sctp_stream_change_event *);
4029
31
  memset(stradd, 0, sizeof(struct sctp_stream_change_event));
4030
31
  stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
4031
31
  stradd->strchange_flags = flag;
4032
31
  stradd->strchange_length = sizeof(struct sctp_stream_change_event);
4033
31
  stradd->strchange_assoc_id = sctp_get_associd(stcb);
4034
31
  stradd->strchange_instrms = stcb->asoc.streamincnt;
4035
31
  stradd->strchange_outstrms = stcb->asoc.streamoutcnt;
4036
31
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
4037
31
  SCTP_BUF_NEXT(m_notify) = NULL;
4038
31
  if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
4039
    /* no space */
4040
5
    sctp_m_freem(m_notify);
4041
5
    return;
4042
5
  }
4043
  /* append to socket */
4044
26
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4045
26
                                   0, 0, stcb->asoc.context, 0, 0, 0,
4046
26
                                   m_notify);
4047
26
  if (control == NULL) {
4048
    /* no memory */
4049
0
    sctp_m_freem(m_notify);
4050
0
    return;
4051
0
  }
4052
26
  control->length = SCTP_BUF_LEN(m_notify);
4053
26
  control->spec_flags = M_NOTIFICATION;
4054
  /* not that we need this */
4055
26
  control->tail_mbuf = m_notify;
4056
26
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
4057
26
                    &stcb->sctp_socket->so_rcv, 1,
4058
26
                    SCTP_READ_LOCK_HELD, so_locked);
4059
26
}
4060
4061
static void
4062
sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, int flag, int so_locked)
4063
80
{
4064
80
  struct mbuf *m_notify;
4065
80
  struct sctp_queued_to_read *control;
4066
80
  struct sctp_assoc_reset_event *strasoc;
4067
4068
80
  KASSERT(stcb != NULL, ("stcb == NULL"));
4069
80
  SCTP_TCB_LOCK_ASSERT(stcb);
4070
80
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
4071
4072
80
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_ASSOC_RESETEVNT)) {
4073
    /* event not enabled */
4074
0
    return;
4075
0
  }
4076
4077
80
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
4078
80
  if (m_notify == NULL)
4079
    /* no space left */
4080
0
    return;
4081
80
  SCTP_BUF_LEN(m_notify) = 0;
4082
80
  strasoc = mtod(m_notify, struct sctp_assoc_reset_event  *);
4083
80
  memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
4084
80
  strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
4085
80
  strasoc->assocreset_flags = flag;
4086
80
  strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
4087
80
  strasoc->assocreset_assoc_id= sctp_get_associd(stcb);
4088
80
  strasoc->assocreset_local_tsn = stcb->asoc.sending_seq;
4089
80
  strasoc->assocreset_remote_tsn = stcb->asoc.mapping_array_base_tsn + 1;
4090
80
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
4091
80
  SCTP_BUF_NEXT(m_notify) = NULL;
4092
80
  if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
4093
    /* no space */
4094
19
    sctp_m_freem(m_notify);
4095
19
    return;
4096
19
  }
4097
  /* append to socket */
4098
61
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4099
61
                                   0, 0, stcb->asoc.context, 0, 0, 0,
4100
61
                                   m_notify);
4101
61
  if (control == NULL) {
4102
    /* no memory */
4103
0
    sctp_m_freem(m_notify);
4104
0
    return;
4105
0
  }
4106
61
  control->length = SCTP_BUF_LEN(m_notify);
4107
61
  control->spec_flags = M_NOTIFICATION;
4108
  /* not that we need this */
4109
61
  control->tail_mbuf = m_notify;
4110
61
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
4111
61
                    &stcb->sctp_socket->so_rcv, 1,
4112
61
                    SCTP_READ_LOCK_HELD, so_locked);
4113
61
}
4114
4115
static void
4116
sctp_notify_stream_reset(struct sctp_tcb *stcb,
4117
                         int number_entries, uint16_t *list, int flag, int so_locked)
4118
535
{
4119
535
  struct mbuf *m_notify;
4120
535
  struct sctp_queued_to_read *control;
4121
535
  struct sctp_stream_reset_event *strreset;
4122
535
  int len;
4123
4124
535
  KASSERT(stcb != NULL, ("stcb == NULL"));
4125
535
  SCTP_TCB_LOCK_ASSERT(stcb);
4126
535
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
4127
4128
535
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_STREAM_RESETEVNT)) {
4129
    /* event not enabled */
4130
0
    return;
4131
0
  }
4132
4133
535
  m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4134
535
  if (m_notify == NULL)
4135
    /* no space left */
4136
0
    return;
4137
535
  SCTP_BUF_LEN(m_notify) = 0;
4138
535
  len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
4139
535
  if (len > M_TRAILINGSPACE(m_notify)) {
4140
    /* never enough room */
4141
0
    sctp_m_freem(m_notify);
4142
0
    return;
4143
0
  }
4144
535
  strreset = mtod(m_notify, struct sctp_stream_reset_event *);
4145
535
  memset(strreset, 0, len);
4146
535
  strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
4147
535
  strreset->strreset_flags = flag;
4148
535
  strreset->strreset_length = len;
4149
535
  strreset->strreset_assoc_id = sctp_get_associd(stcb);
4150
535
  if (number_entries) {
4151
277
    int i;
4152
4153
8.30k
    for (i = 0; i < number_entries; i++) {
4154
8.02k
      strreset->strreset_stream_list[i] = ntohs(list[i]);
4155
8.02k
    }
4156
277
  }
4157
535
  SCTP_BUF_LEN(m_notify) = len;
4158
535
  SCTP_BUF_NEXT(m_notify) = NULL;
4159
535
  if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
4160
    /* no space */
4161
31
    sctp_m_freem(m_notify);
4162
31
    return;
4163
31
  }
4164
  /* append to socket */
4165
504
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4166
504
                                   0, 0, stcb->asoc.context, 0, 0, 0,
4167
504
                                   m_notify);
4168
504
  if (control == NULL) {
4169
    /* no memory */
4170
0
    sctp_m_freem(m_notify);
4171
0
    return;
4172
0
  }
4173
504
  control->length = SCTP_BUF_LEN(m_notify);
4174
504
  control->spec_flags = M_NOTIFICATION;
4175
  /* not that we need this */
4176
504
  control->tail_mbuf = m_notify;
4177
504
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
4178
504
                    &stcb->sctp_socket->so_rcv, 1,
4179
504
                    SCTP_READ_LOCK_HELD, so_locked);
4180
504
}
4181
4182
static void
4183
sctp_notify_remote_error(struct sctp_tcb *stcb, uint16_t error,
4184
                         struct sctp_error_chunk *chunk, int so_locked)
4185
853k
{
4186
853k
  struct mbuf *m_notify;
4187
853k
  struct sctp_remote_error *sre;
4188
853k
  struct sctp_queued_to_read *control;
4189
853k
  unsigned int notif_len;
4190
853k
  uint16_t chunk_len;
4191
4192
853k
  KASSERT(stcb != NULL, ("stcb == NULL"));
4193
853k
  SCTP_TCB_LOCK_ASSERT(stcb);
4194
853k
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
4195
4196
853k
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
4197
0
    return;
4198
0
  }
4199
4200
853k
  if (chunk != NULL) {
4201
853k
    chunk_len = ntohs(chunk->ch.chunk_length);
4202
    /*
4203
     * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
4204
     * contiguous.
4205
     */
4206
853k
    if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
4207
134
      chunk_len = SCTP_CHUNK_BUFFER_SIZE;
4208
134
    }
4209
853k
  } else {
4210
0
    chunk_len = 0;
4211
0
  }
4212
853k
  notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
4213
853k
  m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4214
853k
  if (m_notify == NULL) {
4215
    /* Retry with smaller value. */
4216
0
    notif_len = (unsigned int)sizeof(struct sctp_remote_error);
4217
0
    m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4218
0
    if (m_notify == NULL) {
4219
0
      return;
4220
0
    }
4221
0
  }
4222
853k
  SCTP_BUF_NEXT(m_notify) = NULL;
4223
853k
  sre = mtod(m_notify, struct sctp_remote_error *);
4224
853k
  memset(sre, 0, notif_len);
4225
853k
  sre->sre_type = SCTP_REMOTE_ERROR;
4226
853k
  sre->sre_flags = 0;
4227
853k
  sre->sre_length = sizeof(struct sctp_remote_error);
4228
853k
  sre->sre_error = error;
4229
853k
  sre->sre_assoc_id = sctp_get_associd(stcb);
4230
853k
  if (notif_len > sizeof(struct sctp_remote_error)) {
4231
853k
    memcpy(sre->sre_data, chunk, chunk_len);
4232
853k
    sre->sre_length += chunk_len;
4233
853k
  }
4234
853k
  SCTP_BUF_LEN(m_notify) = sre->sre_length;
4235
853k
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4236
853k
                                   0, 0, stcb->asoc.context, 0, 0, 0,
4237
853k
                                   m_notify);
4238
853k
  if (control != NULL) {
4239
853k
    control->length = SCTP_BUF_LEN(m_notify);
4240
853k
    control->spec_flags = M_NOTIFICATION;
4241
    /* not that we need this */
4242
853k
    control->tail_mbuf = m_notify;
4243
853k
    sctp_add_to_readq(stcb->sctp_ep, stcb, control,
4244
853k
                      &stcb->sctp_socket->so_rcv, 1,
4245
853k
                      SCTP_READ_LOCK_HELD, so_locked);
4246
853k
  } else {
4247
0
    sctp_m_freem(m_notify);
4248
0
  }
4249
853k
}
4250
4251
void
4252
sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
4253
                uint32_t error, void *data, int so_locked)
4254
916k
{
4255
916k
  struct sctp_inpcb *inp;
4256
916k
  struct sctp_nets *net;
4257
4258
916k
  KASSERT(stcb != NULL, ("stcb == NULL"));
4259
916k
  SCTP_TCB_LOCK_ASSERT(stcb);
4260
4261
916k
  inp = stcb->sctp_ep;
4262
#if defined(__APPLE__) && !defined(__Userspace__)
4263
  if (so_locked) {
4264
    sctp_lock_assert(SCTP_INP_SO(inp));
4265
  } else {
4266
    sctp_unlock_assert(SCTP_INP_SO(inp));
4267
  }
4268
#endif
4269
916k
  if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4270
0
    return;
4271
0
  }
4272
916k
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4273
916k
      (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4274
356k
    if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
4275
356k
        (notification == SCTP_NOTIFY_INTERFACE_UP) ||
4276
356k
        (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
4277
      /* Don't report these in front states */
4278
24
      return;
4279
24
    }
4280
356k
  }
4281
916k
  if (notification != SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION) {
4282
916k
    SCTP_INP_READ_LOCK(inp);
4283
916k
  }
4284
916k
  SCTP_INP_READ_LOCK_ASSERT(inp);
4285
4286
916k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4287
916k
      (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4288
916k
      (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4289
0
    SCTP_INP_READ_UNLOCK(inp);
4290
0
    return;
4291
0
  }
4292
4293
916k
  switch (notification) {
4294
5.17k
  case SCTP_NOTIFY_ASSOC_UP:
4295
5.17k
    if (stcb->asoc.assoc_up_sent == 0) {
4296
5.17k
      sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, false, false, so_locked);
4297
5.17k
      stcb->asoc.assoc_up_sent = 1;
4298
5.17k
    }
4299
5.17k
    if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
4300
5.17k
      sctp_notify_adaptation_layer(stcb, so_locked);
4301
5.17k
    }
4302
5.17k
    if (stcb->asoc.auth_supported == 0) {
4303
0
      sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, 0, so_locked);
4304
0
    }
4305
5.17k
    break;
4306
0
  case SCTP_NOTIFY_ASSOC_DOWN:
4307
0
    sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, false, false, so_locked);
4308
0
#if defined(__Userspace__)
4309
0
    if (inp->recv_callback) {
4310
0
      if (stcb->sctp_socket) {
4311
0
        union sctp_sockstore addr;
4312
0
        struct sctp_rcvinfo rcv;
4313
4314
0
        memset(&addr, 0, sizeof(union sctp_sockstore));
4315
0
        memset(&rcv, 0, sizeof(struct sctp_rcvinfo));
4316
0
        atomic_add_int(&stcb->asoc.refcnt, 1);
4317
0
        SCTP_TCB_UNLOCK(stcb);
4318
0
        inp->recv_callback(stcb->sctp_socket, addr, NULL, 0, rcv, 0, inp->ulp_info);
4319
0
        SCTP_TCB_LOCK(stcb);
4320
0
        atomic_subtract_int(&stcb->asoc.refcnt, 1);
4321
0
      }
4322
0
    }
4323
0
#endif
4324
0
    break;
4325
0
  case SCTP_NOTIFY_INTERFACE_DOWN:
4326
0
    net = (struct sctp_nets *)data;
4327
0
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_UNREACHABLE,
4328
0
                                 &net->ro._l_addr.sa, error, so_locked);
4329
0
    break;
4330
0
  case SCTP_NOTIFY_INTERFACE_UP:
4331
0
    net = (struct sctp_nets *)data;
4332
0
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_AVAILABLE,
4333
0
                                 &net->ro._l_addr.sa, error, so_locked);
4334
0
    break;
4335
1
  case SCTP_NOTIFY_INTERFACE_CONFIRMED:
4336
1
    net = (struct sctp_nets *)data;
4337
1
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_CONFIRMED,
4338
1
                                 &net->ro._l_addr.sa, error, so_locked);
4339
1
    break;
4340
496
  case SCTP_NOTIFY_SPECIAL_SP_FAIL:
4341
496
    sctp_notify_send_failed2(stcb, error,
4342
496
                             (struct sctp_stream_queue_pending *)data, so_locked);
4343
496
    break;
4344
414
  case SCTP_NOTIFY_SENT_DG_FAIL:
4345
414
    sctp_notify_send_failed(stcb, 1, error,
4346
414
        (struct sctp_tmit_chunk *)data, so_locked);
4347
414
    break;
4348
22
  case SCTP_NOTIFY_UNSENT_DG_FAIL:
4349
22
    sctp_notify_send_failed(stcb, 0, error,
4350
22
                            (struct sctp_tmit_chunk *)data, so_locked);
4351
22
    break;
4352
879
  case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
4353
879
    sctp_notify_partial_delivery_indication(stcb, error,
4354
879
                                            (struct sctp_queued_to_read *)data,
4355
879
                                            so_locked);
4356
879
    break;
4357
1.51k
  case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
4358
1.51k
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4359
1.51k
        (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4360
603
      sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, false, false, so_locked);
4361
907
    } else {
4362
907
      sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, false, false, so_locked);
4363
907
    }
4364
1.51k
    break;
4365
58
  case SCTP_NOTIFY_ASSOC_REM_ABORTED:
4366
58
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4367
58
        (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4368
46
      sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, true, false, so_locked);
4369
46
    } else {
4370
12
      sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, true, false, so_locked);
4371
12
    }
4372
58
    break;
4373
0
  case SCTP_NOTIFY_ASSOC_TIMEDOUT:
4374
0
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4375
0
        (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4376
0
      sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, false, true, so_locked);
4377
0
    } else {
4378
0
      sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, false, true, so_locked);
4379
0
    }
4380
0
    break;
4381
0
  case SCTP_NOTIFY_ASSOC_RESTART:
4382
0
    sctp_notify_assoc_change(SCTP_RESTART, stcb, error, NULL, false, false, so_locked);
4383
0
    if (stcb->asoc.auth_supported == 0) {
4384
0
      sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, 0, so_locked);
4385
0
    }
4386
0
    break;
4387
80
  case SCTP_NOTIFY_STR_RESET_SEND:
4388
80
    sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN, so_locked);
4389
80
    break;
4390
455
  case SCTP_NOTIFY_STR_RESET_RECV:
4391
455
    sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING, so_locked);
4392
455
    break;
4393
0
  case SCTP_NOTIFY_STR_RESET_FAILED_OUT:
4394
0
    sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
4395
0
                             (SCTP_STREAM_RESET_OUTGOING_SSN|SCTP_STREAM_RESET_FAILED), so_locked);
4396
0
    break;
4397
0
  case SCTP_NOTIFY_STR_RESET_DENIED_OUT:
4398
0
    sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
4399
0
                             (SCTP_STREAM_RESET_OUTGOING_SSN|SCTP_STREAM_RESET_DENIED), so_locked);
4400
0
    break;
4401
0
  case SCTP_NOTIFY_STR_RESET_FAILED_IN:
4402
0
    sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
4403
0
                             (SCTP_STREAM_RESET_INCOMING|SCTP_STREAM_RESET_FAILED), so_locked);
4404
0
    break;
4405
0
  case SCTP_NOTIFY_STR_RESET_DENIED_IN:
4406
0
    sctp_notify_stream_reset(stcb, error, ((uint16_t *) data),
4407
0
                             (SCTP_STREAM_RESET_INCOMING|SCTP_STREAM_RESET_DENIED), so_locked);
4408
0
    break;
4409
31
  case SCTP_NOTIFY_STR_RESET_ADD:
4410
31
    sctp_notify_stream_reset_add(stcb, error, so_locked);
4411
31
    break;
4412
80
  case SCTP_NOTIFY_STR_RESET_TSN:
4413
80
    sctp_notify_stream_reset_tsn(stcb, error, so_locked);
4414
80
    break;
4415
25.9k
  case SCTP_NOTIFY_ASCONF_ADD_IP:
4416
25.9k
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
4417
25.9k
                                 error, so_locked);
4418
25.9k
    break;
4419
26.5k
  case SCTP_NOTIFY_ASCONF_DELETE_IP:
4420
26.5k
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
4421
26.5k
                                 error, so_locked);
4422
26.5k
    break;
4423
1.21k
  case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
4424
1.21k
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
4425
1.21k
                                 error, so_locked);
4426
1.21k
    break;
4427
84
  case SCTP_NOTIFY_PEER_SHUTDOWN:
4428
84
    sctp_notify_shutdown_event(stcb, so_locked);
4429
84
    break;
4430
0
  case SCTP_NOTIFY_AUTH_NEW_KEY:
4431
0
    sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
4432
0
                               *(uint16_t *)data, so_locked);
4433
0
    break;
4434
0
  case SCTP_NOTIFY_AUTH_FREE_KEY:
4435
0
    sctp_notify_authentication(stcb, SCTP_AUTH_FREE_KEY,
4436
0
                               *(uint16_t *)data, so_locked);
4437
0
    break;
4438
0
  case SCTP_NOTIFY_NO_PEER_AUTH:
4439
0
    sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH,
4440
0
                               0, so_locked);
4441
0
    break;
4442
267
  case SCTP_NOTIFY_SENDER_DRY:
4443
267
    sctp_notify_sender_dry_event(stcb, so_locked);
4444
267
    break;
4445
853k
  case SCTP_NOTIFY_REMOTE_ERROR:
4446
853k
    sctp_notify_remote_error(stcb, error, data, so_locked);
4447
853k
    break;
4448
0
  default:
4449
0
    SCTPDBG(SCTP_DEBUG_UTIL1, "%s: unknown notification %xh (%u)\n",
4450
0
            __func__, notification, notification);
4451
0
    break;
4452
916k
  }
4453
916k
  if (notification != SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION) {
4454
916k
    SCTP_INP_READ_UNLOCK(inp);
4455
916k
  }
4456
916k
}
4457
4458
void
4459
sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int so_locked)
4460
1.56k
{
4461
1.56k
  struct sctp_association *asoc;
4462
1.56k
  struct sctp_stream_out *outs;
4463
1.56k
  struct sctp_tmit_chunk *chk, *nchk;
4464
1.56k
  struct sctp_stream_queue_pending *sp, *nsp;
4465
1.56k
  int i;
4466
4467
1.56k
  if (stcb == NULL) {
4468
0
    return;
4469
0
  }
4470
1.56k
  asoc = &stcb->asoc;
4471
1.56k
  if (asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) {
4472
    /* already being freed */
4473
0
    return;
4474
0
  }
4475
#if defined(__APPLE__) && !defined(__Userspace__)
4476
  if (so_locked) {
4477
    sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
4478
  } else {
4479
    sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
4480
  }
4481
#endif
4482
1.56k
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4483
1.56k
      (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4484
1.56k
      (asoc->state & SCTP_STATE_CLOSED_SOCKET)) {
4485
0
    return;
4486
0
  }
4487
  /* now through all the gunk freeing chunks */
4488
  /* sent queue SHOULD be empty */
4489
1.56k
  TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
4490
414
    TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4491
414
    asoc->sent_queue_cnt--;
4492
414
    if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
4493
414
      if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
4494
414
        asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4495
414
#ifdef INVARIANTS
4496
414
      } else {
4497
0
        panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4498
0
#endif
4499
0
      }
4500
414
    }
4501
414
    if (chk->data != NULL) {
4502
414
      sctp_free_bufspace(stcb, asoc, chk, 1);
4503
414
      sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
4504
414
                      error, chk, so_locked);
4505
414
      if (chk->data) {
4506
0
        sctp_m_freem(chk->data);
4507
0
        chk->data = NULL;
4508
0
      }
4509
414
    }
4510
414
    sctp_free_a_chunk(stcb, chk, so_locked);
4511
    /*sa_ignore FREED_MEMORY*/
4512
414
  }
4513
  /* pending send queue SHOULD be empty */
4514
1.56k
  TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
4515
22
    TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4516
22
    asoc->send_queue_cnt--;
4517
22
    if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
4518
22
      asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4519
22
#ifdef INVARIANTS
4520
22
    } else {
4521
0
      panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4522
0
#endif
4523
0
    }
4524
22
    if (chk->data != NULL) {
4525
22
      sctp_free_bufspace(stcb, asoc, chk, 1);
4526
22
      sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
4527
22
                      error, chk, so_locked);
4528
22
      if (chk->data) {
4529
0
        sctp_m_freem(chk->data);
4530
0
        chk->data = NULL;
4531
0
      }
4532
22
    }
4533
22
    sctp_free_a_chunk(stcb, chk, so_locked);
4534
    /*sa_ignore FREED_MEMORY*/
4535
22
  }
4536
383k
  for (i = 0; i < asoc->streamoutcnt; i++) {
4537
    /* For each stream */
4538
382k
    outs = &asoc->strmout[i];
4539
    /* clean up any sends there */
4540
382k
    TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
4541
496
      atomic_subtract_int(&asoc->stream_queue_cnt, 1);
4542
496
      TAILQ_REMOVE(&outs->outqueue, sp, next);
4543
496
      stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
4544
496
      sctp_free_spbufspace(stcb, asoc, sp);
4545
496
      if (sp->data) {
4546
496
        sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
4547
496
            error, (void *)sp, so_locked);
4548
496
        if (sp->data) {
4549
0
          sctp_m_freem(sp->data);
4550
0
          sp->data = NULL;
4551
0
          sp->tail_mbuf = NULL;
4552
0
          sp->length = 0;
4553
0
        }
4554
496
      }
4555
496
      if (sp->net) {
4556
0
        sctp_free_remote_addr(sp->net);
4557
0
        sp->net = NULL;
4558
0
      }
4559
      /* Free the chunk */
4560
496
      sctp_free_a_strmoq(stcb, sp, so_locked);
4561
      /*sa_ignore FREED_MEMORY*/
4562
496
    }
4563
382k
  }
4564
1.56k
}
4565
4566
void
4567
sctp_abort_notification(struct sctp_tcb *stcb, bool from_peer, bool timeout,
4568
                        uint16_t error, struct sctp_abort_chunk *abort,
4569
                        int so_locked)
4570
1.56k
{
4571
1.56k
  if (stcb == NULL) {
4572
0
    return;
4573
0
  }
4574
#if defined(__APPLE__) && !defined(__Userspace__)
4575
  if (so_locked) {
4576
    sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
4577
  } else {
4578
    sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
4579
  }
4580
#endif
4581
1.56k
  SCTP_TCB_LOCK_ASSERT(stcb);
4582
4583
1.56k
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4584
1.56k
      ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4585
1.56k
       (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4586
1.56k
    sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_WAS_ABORTED);
4587
1.56k
  }
4588
1.56k
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4589
1.56k
      (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4590
1.56k
      (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4591
0
    return;
4592
0
  }
4593
1.56k
  SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4594
  /* Tell them we lost the asoc */
4595
1.56k
  sctp_report_all_outbound(stcb, error, so_locked);
4596
1.56k
  if (from_peer) {
4597
58
    sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4598
1.51k
  } else {
4599
1.51k
    if (timeout) {
4600
0
      sctp_ulp_notify(SCTP_NOTIFY_ASSOC_TIMEDOUT, stcb, error, abort, so_locked);
4601
1.51k
    } else {
4602
1.51k
      sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4603
1.51k
    }
4604
1.51k
  }
4605
1.56k
}
4606
4607
void
4608
sctp_abort_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4609
                       struct mbuf *m, int iphlen,
4610
                       struct sockaddr *src, struct sockaddr *dst,
4611
                       struct sctphdr *sh, struct mbuf *op_err,
4612
#if defined(__FreeBSD__) && !defined(__Userspace__)
4613
                       uint8_t mflowtype, uint32_t mflowid,
4614
#endif
4615
                       uint32_t vrf_id, uint16_t port)
4616
467
{
4617
#if defined(__APPLE__) && !defined(__Userspace__)
4618
  struct socket *so;
4619
#endif
4620
467
  struct sctp_gen_error_cause* cause;
4621
467
  uint32_t vtag;
4622
467
  uint16_t cause_code;
4623
4624
467
  if (stcb != NULL) {
4625
467
    vtag = stcb->asoc.peer_vtag;
4626
467
    vrf_id = stcb->asoc.vrf_id;
4627
467
    if (op_err != NULL) {
4628
      /* Read the cause code from the error cause. */
4629
280
      cause = mtod(op_err, struct sctp_gen_error_cause *);
4630
280
      cause_code = ntohs(cause->code);
4631
280
    } else {
4632
187
      cause_code = 0;
4633
187
    }
4634
467
  } else {
4635
0
    vtag = 0;
4636
0
  }
4637
467
  sctp_send_abort(m, iphlen, src, dst, sh, vtag, op_err,
4638
#if defined(__FreeBSD__) && !defined(__Userspace__)
4639
                  mflowtype, mflowid, inp->fibnum,
4640
#endif
4641
467
                  vrf_id, port);
4642
467
  if (stcb != NULL) {
4643
    /* We have a TCB to abort, send notification too */
4644
467
    sctp_abort_notification(stcb, false, false, cause_code, NULL, SCTP_SO_NOT_LOCKED);
4645
    /* Ok, now lets free it */
4646
#if defined(__APPLE__) && !defined(__Userspace__)
4647
    so = SCTP_INP_SO(inp);
4648
    atomic_add_int(&stcb->asoc.refcnt, 1);
4649
    SCTP_TCB_UNLOCK(stcb);
4650
    SCTP_SOCKET_LOCK(so, 1);
4651
    SCTP_TCB_LOCK(stcb);
4652
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
4653
#endif
4654
467
    SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4655
467
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4656
467
        (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4657
0
      SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4658
0
    }
4659
467
    (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4660
467
                          SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4661
#if defined(__APPLE__) && !defined(__Userspace__)
4662
    SCTP_SOCKET_UNLOCK(so, 1);
4663
#endif
4664
467
  }
4665
467
}
4666
#ifdef SCTP_ASOCLOG_OF_TSNS
4667
void
4668
sctp_print_out_track_log(struct sctp_tcb *stcb)
4669
{
4670
#ifdef NOSIY_PRINTS
4671
  int i;
4672
  SCTP_PRINTF("Last ep reason:%x\n", stcb->sctp_ep->last_abort_code);
4673
  SCTP_PRINTF("IN bound TSN log-aaa\n");
4674
  if ((stcb->asoc.tsn_in_at == 0) && (stcb->asoc.tsn_in_wrapped == 0)) {
4675
    SCTP_PRINTF("None rcvd\n");
4676
    goto none_in;
4677
  }
4678
  if (stcb->asoc.tsn_in_wrapped) {
4679
    for (i = stcb->asoc.tsn_in_at; i < SCTP_TSN_LOG_SIZE; i++) {
4680
      SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4681
            stcb->asoc.in_tsnlog[i].tsn,
4682
            stcb->asoc.in_tsnlog[i].strm,
4683
            stcb->asoc.in_tsnlog[i].seq,
4684
            stcb->asoc.in_tsnlog[i].flgs,
4685
            stcb->asoc.in_tsnlog[i].sz);
4686
    }
4687
  }
4688
  if (stcb->asoc.tsn_in_at) {
4689
    for (i = 0; i < stcb->asoc.tsn_in_at; i++) {
4690
      SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4691
            stcb->asoc.in_tsnlog[i].tsn,
4692
            stcb->asoc.in_tsnlog[i].strm,
4693
            stcb->asoc.in_tsnlog[i].seq,
4694
            stcb->asoc.in_tsnlog[i].flgs,
4695
            stcb->asoc.in_tsnlog[i].sz);
4696
    }
4697
  }
4698
 none_in:
4699
  SCTP_PRINTF("OUT bound TSN log-aaa\n");
4700
  if ((stcb->asoc.tsn_out_at == 0) &&
4701
      (stcb->asoc.tsn_out_wrapped == 0)) {
4702
    SCTP_PRINTF("None sent\n");
4703
  }
4704
  if (stcb->asoc.tsn_out_wrapped) {
4705
    for (i = stcb->asoc.tsn_out_at; i < SCTP_TSN_LOG_SIZE; i++) {
4706
      SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4707
            stcb->asoc.out_tsnlog[i].tsn,
4708
            stcb->asoc.out_tsnlog[i].strm,
4709
            stcb->asoc.out_tsnlog[i].seq,
4710
            stcb->asoc.out_tsnlog[i].flgs,
4711
            stcb->asoc.out_tsnlog[i].sz);
4712
    }
4713
  }
4714
  if (stcb->asoc.tsn_out_at) {
4715
    for (i = 0; i < stcb->asoc.tsn_out_at; i++) {
4716
      SCTP_PRINTF("TSN:%x strm:%d seq:%d flags:%x sz:%d\n",
4717
            stcb->asoc.out_tsnlog[i].tsn,
4718
            stcb->asoc.out_tsnlog[i].strm,
4719
            stcb->asoc.out_tsnlog[i].seq,
4720
            stcb->asoc.out_tsnlog[i].flgs,
4721
            stcb->asoc.out_tsnlog[i].sz);
4722
    }
4723
  }
4724
#endif
4725
}
4726
#endif
4727
4728
void
4729
sctp_abort_an_association(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
4730
                          struct mbuf *op_err, bool timedout, int so_locked)
4731
1.04k
{
4732
#if defined(__APPLE__) && !defined(__Userspace__)
4733
  struct socket *so;
4734
#endif
4735
1.04k
  struct sctp_gen_error_cause* cause;
4736
1.04k
  uint16_t cause_code;
4737
4738
#if defined(__APPLE__) && !defined(__Userspace__)
4739
  so = SCTP_INP_SO(inp);
4740
#endif
4741
#if defined(__APPLE__) && !defined(__Userspace__)
4742
  if (so_locked) {
4743
    sctp_lock_assert(SCTP_INP_SO(inp));
4744
  } else {
4745
    sctp_unlock_assert(SCTP_INP_SO(inp));
4746
  }
4747
#endif
4748
1.04k
  if (stcb == NULL) {
4749
    /* Got to have a TCB */
4750
0
    if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
4751
0
      if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4752
#if defined(__APPLE__) && !defined(__Userspace__)
4753
        if (!so_locked) {
4754
          SCTP_SOCKET_LOCK(so, 1);
4755
        }
4756
#endif
4757
0
        sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4758
0
            SCTP_CALLED_DIRECTLY_NOCMPSET);
4759
#if defined(__APPLE__) && !defined(__Userspace__)
4760
        if (!so_locked) {
4761
          SCTP_SOCKET_UNLOCK(so, 1);
4762
        }
4763
#endif
4764
0
      }
4765
0
    }
4766
0
    return;
4767
0
  }
4768
1.04k
  if (op_err != NULL) {
4769
    /* Read the cause code from the error cause. */
4770
1.04k
    cause = mtod(op_err, struct sctp_gen_error_cause *);
4771
1.04k
    cause_code = ntohs(cause->code);
4772
1.04k
  } else {
4773
0
    cause_code = 0;
4774
0
  }
4775
  /* notify the peer */
4776
1.04k
  sctp_send_abort_tcb(stcb, op_err, so_locked);
4777
1.04k
  SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4778
1.04k
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4779
1.04k
      (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4780
906
    SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4781
906
  }
4782
  /* notify the ulp */
4783
1.04k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4784
1.04k
    sctp_abort_notification(stcb, false, timedout, cause_code, NULL, so_locked);
4785
1.04k
  }
4786
  /* now free the asoc */
4787
#ifdef SCTP_ASOCLOG_OF_TSNS
4788
  sctp_print_out_track_log(stcb);
4789
#endif
4790
#if defined(__APPLE__) && !defined(__Userspace__)
4791
  if (!so_locked) {
4792
    atomic_add_int(&stcb->asoc.refcnt, 1);
4793
    SCTP_TCB_UNLOCK(stcb);
4794
    SCTP_SOCKET_LOCK(so, 1);
4795
    SCTP_TCB_LOCK(stcb);
4796
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
4797
  }
4798
#endif
4799
1.04k
  (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4800
1.04k
                        SCTP_FROM_SCTPUTIL + SCTP_LOC_5);
4801
#if defined(__APPLE__) && !defined(__Userspace__)
4802
  if (!so_locked) {
4803
    SCTP_SOCKET_UNLOCK(so, 1);
4804
  }
4805
#endif
4806
1.04k
}
4807
4808
void
4809
sctp_handle_ootb(struct mbuf *m, int iphlen, int offset,
4810
                 struct sockaddr *src, struct sockaddr *dst,
4811
                 struct sctphdr *sh, struct sctp_inpcb *inp,
4812
                 struct mbuf *cause,
4813
#if defined(__FreeBSD__) && !defined(__Userspace__)
4814
                 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
4815
#endif
4816
                 uint32_t vrf_id, uint16_t port)
4817
307
{
4818
307
  struct sctp_chunkhdr *ch, chunk_buf;
4819
307
  unsigned int chk_length;
4820
307
  int contains_init_chunk;
4821
4822
307
  SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4823
  /* Generate a TO address for future reference */
4824
307
  if (inp && (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)) {
4825
0
    if (LIST_EMPTY(&inp->sctp_asoc_list)) {
4826
#if defined(__APPLE__) && !defined(__Userspace__)
4827
      SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1);
4828
#endif
4829
0
      sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
4830
0
          SCTP_CALLED_DIRECTLY_NOCMPSET);
4831
#if defined(__APPLE__) && !defined(__Userspace__)
4832
      SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1);
4833
#endif
4834
0
    }
4835
0
  }
4836
307
  contains_init_chunk = 0;
4837
307
  ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4838
307
      sizeof(*ch), (uint8_t *) & chunk_buf);
4839
59.0k
  while (ch != NULL) {
4840
58.7k
    chk_length = ntohs(ch->chunk_length);
4841
58.7k
    if (chk_length < sizeof(*ch)) {
4842
      /* break to abort land */
4843
48
      break;
4844
48
    }
4845
58.7k
    switch (ch->chunk_type) {
4846
32.4k
    case SCTP_INIT:
4847
32.4k
      contains_init_chunk = 1;
4848
32.4k
      break;
4849
4
    case SCTP_PACKET_DROPPED:
4850
      /* we don't respond to pkt-dropped */
4851
4
      return;
4852
1
    case SCTP_ABORT_ASSOCIATION:
4853
      /* we don't respond with an ABORT to an ABORT */
4854
1
      return;
4855
1
    case SCTP_SHUTDOWN_COMPLETE:
4856
      /*
4857
       * we ignore it since we are not waiting for it and
4858
       * peer is gone
4859
       */
4860
1
      return;
4861
1
    case SCTP_SHUTDOWN_ACK:
4862
1
      sctp_send_shutdown_complete2(src, dst, sh,
4863
#if defined(__FreeBSD__) && !defined(__Userspace__)
4864
                                   mflowtype, mflowid, fibnum,
4865
#endif
4866
1
                                   vrf_id, port);
4867
1
      return;
4868
26.2k
    default:
4869
26.2k
      break;
4870
58.7k
    }
4871
58.7k
    offset += SCTP_SIZE32(chk_length);
4872
58.7k
    ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4873
58.7k
        sizeof(*ch), (uint8_t *) & chunk_buf);
4874
58.7k
  }
4875
300
  if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4876
300
      ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4877
300
       (contains_init_chunk == 0))) {
4878
300
    sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4879
#if defined(__FreeBSD__) && !defined(__Userspace__)
4880
                    mflowtype, mflowid, fibnum,
4881
#endif
4882
300
                    vrf_id, port);
4883
300
  }
4884
300
}
4885
4886
/*
4887
 * check the inbound datagram to make sure there is not an abort inside it,
4888
 * if there is return 1, else return 0.
4889
 */
4890
int
4891
sctp_is_there_an_abort_here(struct mbuf *m, int iphlen, uint32_t *vtag)
4892
767
{
4893
767
  struct sctp_chunkhdr *ch;
4894
767
  struct sctp_init_chunk *init_chk, chunk_buf;
4895
767
  int offset;
4896
767
  unsigned int chk_length;
4897
4898
767
  offset = iphlen + sizeof(struct sctphdr);
4899
767
  ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4900
767
      (uint8_t *) & chunk_buf);
4901
90.7k
  while (ch != NULL) {
4902
89.9k
    chk_length = ntohs(ch->chunk_length);
4903
89.9k
    if (chk_length < sizeof(*ch)) {
4904
      /* packet is probably corrupt */
4905
48
      break;
4906
48
    }
4907
    /* we seem to be ok, is it an abort? */
4908
89.9k
    if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4909
      /* yep, tell them */
4910
0
      return (1);
4911
0
    }
4912
89.9k
    if ((ch->chunk_type == SCTP_INITIATION) ||
4913
89.9k
        (ch->chunk_type == SCTP_INITIATION_ACK)) {
4914
      /* need to update the Vtag */
4915
33.5k
      init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4916
33.5k
          offset, sizeof(struct sctp_init_chunk), (uint8_t *) & chunk_buf);
4917
33.5k
      if (init_chk != NULL) {
4918
33.3k
        *vtag = ntohl(init_chk->init.initiate_tag);
4919
33.3k
      }
4920
33.5k
    }
4921
    /* Nope, move to the next chunk */
4922
89.9k
    offset += SCTP_SIZE32(chk_length);
4923
89.9k
    ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4924
89.9k
        sizeof(*ch), (uint8_t *) & chunk_buf);
4925
89.9k
  }
4926
767
  return (0);
4927
767
}
4928
4929
/*
4930
 * currently (2/02), ifa_addr embeds scope_id's and don't have sin6_scope_id
4931
 * set (i.e. it's 0) so, create this function to compare link local scopes
4932
 */
4933
#ifdef INET6
4934
uint32_t
4935
sctp_is_same_scope(struct sockaddr_in6 *addr1, struct sockaddr_in6 *addr2)
4936
0
{
4937
0
#if defined(__Userspace__)
4938
    /*__Userspace__ Returning 1 here always */
4939
0
#endif
4940
#if defined(SCTP_EMBEDDED_V6_SCOPE)
4941
  struct sockaddr_in6 a, b;
4942
4943
  /* save copies */
4944
  a = *addr1;
4945
  b = *addr2;
4946
4947
  if (a.sin6_scope_id == 0)
4948
#ifdef SCTP_KAME
4949
    if (sa6_recoverscope(&a)) {
4950
#else
4951
    if (in6_recoverscope(&a, &a.sin6_addr, NULL)) {
4952
#endif        /* SCTP_KAME */
4953
      /* can't get scope, so can't match */
4954
      return (0);
4955
    }
4956
  if (b.sin6_scope_id == 0)
4957
#ifdef SCTP_KAME
4958
    if (sa6_recoverscope(&b)) {
4959
#else
4960
    if (in6_recoverscope(&b, &b.sin6_addr, NULL)) {
4961
#endif        /* SCTP_KAME */
4962
      /* can't get scope, so can't match */
4963
      return (0);
4964
    }
4965
  if (a.sin6_scope_id != b.sin6_scope_id)
4966
    return (0);
4967
#else
4968
0
  if (addr1->sin6_scope_id != addr2->sin6_scope_id)
4969
0
    return (0);
4970
0
#endif /* SCTP_EMBEDDED_V6_SCOPE */
4971
4972
0
  return (1);
4973
0
}
4974
4975
#if defined(SCTP_EMBEDDED_V6_SCOPE)
4976
/*
4977
 * returns a sockaddr_in6 with embedded scope recovered and removed
4978
 */
4979
struct sockaddr_in6 *
4980
sctp_recover_scope(struct sockaddr_in6 *addr, struct sockaddr_in6 *store)
4981
{
4982
  /* check and strip embedded scope junk */
4983
  if (addr->sin6_family == AF_INET6) {
4984
    if (IN6_IS_SCOPE_LINKLOCAL(&addr->sin6_addr)) {
4985
      if (addr->sin6_scope_id == 0) {
4986
        *store = *addr;
4987
#ifdef SCTP_KAME
4988
        if (!sa6_recoverscope(store)) {
4989
#else
4990
        if (!in6_recoverscope(store, &store->sin6_addr,
4991
            NULL)) {
4992
#endif /* SCTP_KAME */
4993
          /* use the recovered scope */
4994
          addr = store;
4995
        }
4996
      } else {
4997
        /* else, return the original "to" addr */
4998
        in6_clearscope(&addr->sin6_addr);
4999
      }
5000
    }
5001
  }
5002
  return (addr);
5003
}
5004
#endif /* SCTP_EMBEDDED_V6_SCOPE */
5005
#endif
5006
5007
/*
5008
 * are the two addresses the same?  currently a "scopeless" check returns: 1
5009
 * if same, 0 if not
5010
 */
5011
int
5012
sctp_cmpaddr(struct sockaddr *sa1, struct sockaddr *sa2)
5013
219k
{
5014
5015
  /* must be valid */
5016
219k
  if (sa1 == NULL || sa2 == NULL)
5017
0
    return (0);
5018
5019
  /* must be the same family */
5020
219k
  if (sa1->sa_family != sa2->sa_family)
5021
49.7k
    return (0);
5022
5023
169k
  switch (sa1->sa_family) {
5024
0
#ifdef INET6
5025
32.1k
  case AF_INET6:
5026
32.1k
  {
5027
    /* IPv6 addresses */
5028
32.1k
    struct sockaddr_in6 *sin6_1, *sin6_2;
5029
5030
32.1k
    sin6_1 = (struct sockaddr_in6 *)sa1;
5031
32.1k
    sin6_2 = (struct sockaddr_in6 *)sa2;
5032
32.1k
    return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
5033
32.1k
        sin6_2));
5034
0
  }
5035
0
#endif
5036
0
#ifdef INET
5037
105k
  case AF_INET:
5038
105k
  {
5039
    /* IPv4 addresses */
5040
105k
    struct sockaddr_in *sin_1, *sin_2;
5041
5042
105k
    sin_1 = (struct sockaddr_in *)sa1;
5043
105k
    sin_2 = (struct sockaddr_in *)sa2;
5044
105k
    return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
5045
0
  }
5046
0
#endif
5047
0
#if defined(__Userspace__)
5048
32.3k
  case AF_CONN:
5049
32.3k
  {
5050
32.3k
    struct sockaddr_conn *sconn_1, *sconn_2;
5051
5052
32.3k
    sconn_1 = (struct sockaddr_conn *)sa1;
5053
32.3k
    sconn_2 = (struct sockaddr_conn *)sa2;
5054
32.3k
    return (sconn_1->sconn_addr == sconn_2->sconn_addr);
5055
0
  }
5056
0
#endif
5057
0
  default:
5058
    /* we don't do these... */
5059
0
    return (0);
5060
169k
  }
5061
169k
}
5062
5063
void
5064
sctp_print_address(struct sockaddr *sa)
5065
0
{
5066
0
#ifdef INET6
5067
#if defined(__FreeBSD__) && !defined(__Userspace__)
5068
  char ip6buf[INET6_ADDRSTRLEN];
5069
#endif
5070
0
#endif
5071
5072
0
  switch (sa->sa_family) {
5073
0
#ifdef INET6
5074
0
  case AF_INET6:
5075
0
  {
5076
0
    struct sockaddr_in6 *sin6;
5077
5078
0
    sin6 = (struct sockaddr_in6 *)sa;
5079
0
#if defined(__Userspace__)
5080
0
    SCTP_PRINTF("IPv6 address: %x:%x:%x:%x:%x:%x:%x:%x:port:%d scope:%u\n",
5081
0
          ntohs(sin6->sin6_addr.s6_addr16[0]),
5082
0
          ntohs(sin6->sin6_addr.s6_addr16[1]),
5083
0
          ntohs(sin6->sin6_addr.s6_addr16[2]),
5084
0
          ntohs(sin6->sin6_addr.s6_addr16[3]),
5085
0
          ntohs(sin6->sin6_addr.s6_addr16[4]),
5086
0
          ntohs(sin6->sin6_addr.s6_addr16[5]),
5087
0
          ntohs(sin6->sin6_addr.s6_addr16[6]),
5088
0
          ntohs(sin6->sin6_addr.s6_addr16[7]),
5089
0
          ntohs(sin6->sin6_port),
5090
0
          sin6->sin6_scope_id);
5091
#else
5092
#if defined(__FreeBSD__) && !defined(__Userspace__)
5093
    SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
5094
          ip6_sprintf(ip6buf, &sin6->sin6_addr),
5095
          ntohs(sin6->sin6_port),
5096
          sin6->sin6_scope_id);
5097
#else
5098
    SCTP_PRINTF("IPv6 address: %s:port:%d scope:%u\n",
5099
          ip6_sprintf(&sin6->sin6_addr),
5100
          ntohs(sin6->sin6_port),
5101
          sin6->sin6_scope_id);
5102
#endif
5103
#endif
5104
0
    break;
5105
0
  }
5106
0
#endif
5107
0
#ifdef INET
5108
0
  case AF_INET:
5109
0
  {
5110
0
    struct sockaddr_in *sin;
5111
0
    unsigned char *p;
5112
5113
0
    sin = (struct sockaddr_in *)sa;
5114
0
    p = (unsigned char *)&sin->sin_addr;
5115
0
    SCTP_PRINTF("IPv4 address: %u.%u.%u.%u:%d\n",
5116
0
          p[0], p[1], p[2], p[3], ntohs(sin->sin_port));
5117
0
    break;
5118
0
  }
5119
0
#endif
5120
0
#if defined(__Userspace__)
5121
0
  case AF_CONN:
5122
0
  {
5123
0
    struct sockaddr_conn *sconn;
5124
5125
0
    sconn = (struct sockaddr_conn *)sa;
5126
0
    SCTP_PRINTF("AF_CONN address: %p\n", sconn->sconn_addr);
5127
0
    break;
5128
0
  }
5129
0
#endif
5130
0
  default:
5131
0
    SCTP_PRINTF("?\n");
5132
0
    break;
5133
0
  }
5134
0
}
5135
5136
void
5137
sctp_pull_off_control_to_new_inp(struct sctp_inpcb *old_inp,
5138
    struct sctp_inpcb *new_inp,
5139
    struct sctp_tcb *stcb,
5140
    int waitflags)
5141
0
{
5142
  /*
5143
   * go through our old INP and pull off any control structures that
5144
   * belong to stcb and move then to the new inp.
5145
   */
5146
0
  struct socket *old_so, *new_so;
5147
0
  struct sctp_queued_to_read *control, *nctl;
5148
0
  struct sctp_readhead tmp_queue;
5149
0
  struct mbuf *m;
5150
#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
5151
  int error = 0;
5152
#endif
5153
5154
0
  old_so = old_inp->sctp_socket;
5155
0
  new_so = new_inp->sctp_socket;
5156
0
  TAILQ_INIT(&tmp_queue);
5157
#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
5158
#if defined(__FreeBSD__)
5159
  error = SOCK_IO_RECV_LOCK(old_so, waitflags);
5160
#else
5161
  error = sblock(&old_so->so_rcv, waitflags);
5162
#endif
5163
  if (error) {
5164
    /* Gak, can't get I/O lock, we have a problem.
5165
     * data will be left stranded.. and we
5166
     * don't dare look at it since the
5167
     * other thread may be reading something.
5168
     * Oh well, its a screwed up app that does
5169
     * a peeloff OR a accept while reading
5170
     * from the main socket... actually its
5171
     * only the peeloff() case, since I think
5172
     * read will fail on a listening socket..
5173
     */
5174
    return;
5175
  }
5176
#endif
5177
  /* lock the socket buffers */
5178
0
  SCTP_INP_READ_LOCK(old_inp);
5179
0
  TAILQ_FOREACH_SAFE(control, &old_inp->read_queue, next, nctl) {
5180
    /* Pull off all for out target stcb */
5181
0
    if (control->stcb == stcb) {
5182
      /* remove it we want it */
5183
0
      TAILQ_REMOVE(&old_inp->read_queue, control, next);
5184
0
      TAILQ_INSERT_TAIL(&tmp_queue, control, next);
5185
0
      m = control->data;
5186
0
      while (m) {
5187
0
        if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5188
0
          sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE,SCTP_BUF_LEN(m));
5189
0
        }
5190
0
        sctp_sbfree(control, stcb, &old_so->so_rcv, m);
5191
0
        if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5192
0
          sctp_sblog(&old_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
5193
0
        }
5194
0
        m = SCTP_BUF_NEXT(m);
5195
0
      }
5196
0
    }
5197
0
  }
5198
0
  SCTP_INP_READ_UNLOCK(old_inp);
5199
  /* Remove the recv-lock on the old socket */
5200
#if defined(__APPLE__) && !defined(__Userspace__)
5201
  sbunlock(&old_so->so_rcv, 1);
5202
#endif
5203
#if defined(__FreeBSD__) && !defined(__Userspace__)
5204
  SOCK_IO_RECV_UNLOCK(old_so);
5205
#endif
5206
  /* Now we move them over to the new socket buffer */
5207
0
  SCTP_INP_READ_LOCK(new_inp);
5208
0
  TAILQ_FOREACH_SAFE(control, &tmp_queue, next, nctl) {
5209
0
    TAILQ_INSERT_TAIL(&new_inp->read_queue, control, next);
5210
0
    m = control->data;
5211
0
    while (m) {
5212
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5213
0
        sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
5214
0
      }
5215
0
      sctp_sballoc(stcb, &new_so->so_rcv, m);
5216
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5217
0
        sctp_sblog(&new_so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
5218
0
      }
5219
0
      m = SCTP_BUF_NEXT(m);
5220
0
    }
5221
0
  }
5222
0
  SCTP_INP_READ_UNLOCK(new_inp);
5223
0
}
5224
5225
void
5226
sctp_wakeup_the_read_socket(struct sctp_inpcb *inp,
5227
    struct sctp_tcb *stcb,
5228
    int so_locked
5229
#if !(defined(__APPLE__) && !defined(__Userspace__))
5230
    SCTP_UNUSED
5231
#endif
5232
)
5233
922k
{
5234
922k
  if ((inp != NULL) &&
5235
922k
      (inp->sctp_socket != NULL) &&
5236
922k
      (((inp->sctp_flags & (SCTP_PCB_FLAGS_TCPTYPE | SCTP_PCB_FLAGS_IN_TCPPOOL)) == 0) ||
5237
922k
       !SCTP_IS_LISTENING(inp))) {
5238
#if defined(__APPLE__) && !defined(__Userspace__)
5239
    struct socket *so;
5240
5241
    so = SCTP_INP_SO(inp);
5242
    if (!so_locked) {
5243
      if (stcb) {
5244
        atomic_add_int(&stcb->asoc.refcnt, 1);
5245
        SCTP_TCB_UNLOCK(stcb);
5246
      }
5247
      SCTP_SOCKET_LOCK(so, 1);
5248
      if (stcb) {
5249
        SCTP_TCB_LOCK(stcb);
5250
        atomic_subtract_int(&stcb->asoc.refcnt, 1);
5251
      }
5252
      if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
5253
        SCTP_SOCKET_UNLOCK(so, 1);
5254
        return;
5255
      }
5256
    }
5257
#endif
5258
922k
    sctp_sorwakeup(inp, inp->sctp_socket);
5259
#if defined(__APPLE__) && !defined(__Userspace__)
5260
    if (!so_locked) {
5261
      SCTP_SOCKET_UNLOCK(so, 1);
5262
    }
5263
#endif
5264
922k
  }
5265
922k
}
5266
#if defined(__Userspace__)
5267
5268
void
5269
sctp_invoke_recv_callback(struct sctp_inpcb *inp,
5270
                          struct sctp_tcb *stcb,
5271
                          struct sctp_queued_to_read *control,
5272
                          int inp_read_lock_held)
5273
922k
{
5274
922k
  uint32_t pd_point, length;
5275
5276
922k
  if ((inp->recv_callback == NULL) ||
5277
922k
      (stcb == NULL) ||
5278
922k
      (stcb->sctp_socket == NULL)) {
5279
922k
    return;
5280
922k
  }
5281
5282
0
  length = control->length;
5283
0
  if (stcb != NULL && stcb->sctp_socket != NULL) {
5284
0
    pd_point = min(SCTP_SB_LIMIT_RCV(stcb->sctp_socket) >> SCTP_PARTIAL_DELIVERY_SHIFT,
5285
0
             stcb->sctp_ep->partial_delivery_point);
5286
0
  } else {
5287
0
    pd_point = inp->partial_delivery_point;
5288
0
  }
5289
0
  if ((control->end_added == 1) || (length >= pd_point)) {
5290
0
    struct socket *so;
5291
0
    struct mbuf *m;
5292
0
    char *buffer;
5293
0
    struct sctp_rcvinfo rcv;
5294
0
    union sctp_sockstore addr;
5295
0
    int flags;
5296
5297
0
    if ((buffer = malloc(length)) == NULL) {
5298
0
      return;
5299
0
    }
5300
0
    if (inp_read_lock_held == 0) {
5301
0
      SCTP_INP_READ_LOCK(inp);
5302
0
    }
5303
0
    so = stcb->sctp_socket;
5304
0
    for (m = control->data; m; m = SCTP_BUF_NEXT(m)) {
5305
0
      sctp_sbfree(control, control->stcb, &so->so_rcv, m);
5306
0
    }
5307
0
    m_copydata(control->data, 0, length, buffer);
5308
0
    memset(&rcv, 0, sizeof(struct sctp_rcvinfo));
5309
0
    rcv.rcv_sid = control->sinfo_stream;
5310
0
    rcv.rcv_ssn = (uint16_t)control->mid;
5311
0
    rcv.rcv_flags = control->sinfo_flags;
5312
0
    rcv.rcv_ppid = control->sinfo_ppid;
5313
0
    rcv.rcv_tsn = control->sinfo_tsn;
5314
0
    rcv.rcv_cumtsn = control->sinfo_cumtsn;
5315
0
    rcv.rcv_context = control->sinfo_context;
5316
0
    rcv.rcv_assoc_id = control->sinfo_assoc_id;
5317
0
    memset(&addr, 0, sizeof(union sctp_sockstore));
5318
0
    switch (control->whoFrom->ro._l_addr.sa.sa_family) {
5319
0
#ifdef INET
5320
0
    case AF_INET:
5321
0
      addr.sin = control->whoFrom->ro._l_addr.sin;
5322
0
      break;
5323
0
#endif
5324
0
#ifdef INET6
5325
0
    case AF_INET6:
5326
0
      addr.sin6 = control->whoFrom->ro._l_addr.sin6;
5327
0
      break;
5328
0
#endif
5329
0
    case AF_CONN:
5330
0
      addr.sconn = control->whoFrom->ro._l_addr.sconn;
5331
0
      break;
5332
0
    default:
5333
0
      addr.sa = control->whoFrom->ro._l_addr.sa;
5334
0
      break;
5335
0
    }
5336
0
    flags = 0;
5337
0
    if (control->end_added == 1) {
5338
0
      flags |= MSG_EOR;
5339
0
    }
5340
0
    if (control->spec_flags & M_NOTIFICATION) {
5341
0
      flags |= MSG_NOTIFICATION;
5342
0
    }
5343
0
    sctp_m_freem(control->data);
5344
0
    control->data = NULL;
5345
0
    control->tail_mbuf = NULL;
5346
0
    control->length = 0;
5347
0
    if (control->end_added) {
5348
0
      TAILQ_REMOVE(&stcb->sctp_ep->read_queue, control, next);
5349
0
      control->on_read_q = 0;
5350
0
      sctp_free_remote_addr(control->whoFrom);
5351
0
      control->whoFrom = NULL;
5352
0
      sctp_free_a_readq(stcb, control);
5353
0
    }
5354
0
    atomic_add_int(&stcb->asoc.refcnt, 1);
5355
0
    SCTP_TCB_UNLOCK(stcb);
5356
0
    if (inp_read_lock_held == 0) {
5357
0
      SCTP_INP_READ_UNLOCK(inp);
5358
0
    }
5359
0
    inp->recv_callback(so, addr, buffer, length, rcv, flags, inp->ulp_info);
5360
0
    SCTP_TCB_LOCK(stcb);
5361
0
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
5362
0
  }
5363
0
}
5364
#endif
5365
5366
void
5367
sctp_add_to_readq(struct sctp_inpcb *inp,
5368
    struct sctp_tcb *stcb,
5369
    struct sctp_queued_to_read *control,
5370
    struct sockbuf *sb,
5371
    int end,
5372
    int inp_read_lock_held,
5373
    int so_locked)
5374
922k
{
5375
  /*
5376
   * Here we must place the control on the end of the socket read
5377
   * queue AND increment sb_cc so that select will work properly on
5378
   * read.
5379
   */
5380
922k
  struct mbuf *m, *prev = NULL;
5381
5382
922k
  if (inp == NULL) {
5383
    /* Gak, TSNH!! */
5384
0
#ifdef INVARIANTS
5385
0
    panic("Gak, inp NULL on add_to_readq");
5386
0
#endif
5387
0
    return;
5388
0
  }
5389
#if defined(__APPLE__) && !defined(__Userspace__)
5390
  if (so_locked) {
5391
    sctp_lock_assert(SCTP_INP_SO(inp));
5392
  } else {
5393
    sctp_unlock_assert(SCTP_INP_SO(inp));
5394
  }
5395
#endif
5396
922k
  if (inp_read_lock_held == SCTP_READ_LOCK_NOT_HELD) {
5397
986
    SCTP_INP_READ_LOCK(inp);
5398
986
  }
5399
922k
  if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ) {
5400
0
    if (!control->on_strm_q) {
5401
0
      sctp_free_remote_addr(control->whoFrom);
5402
0
      if (control->data) {
5403
0
        sctp_m_freem(control->data);
5404
0
        control->data = NULL;
5405
0
      }
5406
0
      sctp_free_a_readq(stcb, control);
5407
0
    }
5408
0
    if (inp_read_lock_held == SCTP_READ_LOCK_NOT_HELD) {
5409
0
      SCTP_INP_READ_UNLOCK(inp);
5410
0
    }
5411
0
    return;
5412
0
  }
5413
922k
  if ((control->spec_flags & M_NOTIFICATION) == 0) {
5414
986
    atomic_add_int(&inp->total_recvs, 1);
5415
986
    if (!control->do_not_ref_stcb) {
5416
986
      atomic_add_int(&stcb->total_recvs, 1);
5417
986
    }
5418
986
  }
5419
922k
  m = control->data;
5420
922k
  control->held_length = 0;
5421
922k
  control->length = 0;
5422
1.84M
  while (m != NULL) {
5423
924k
    if (SCTP_BUF_LEN(m) == 0) {
5424
      /* Skip mbufs with NO length */
5425
172
      if (prev == NULL) {
5426
        /* First one */
5427
172
        control->data = sctp_m_free(m);
5428
172
        m = control->data;
5429
172
      } else {
5430
0
        SCTP_BUF_NEXT(prev) = sctp_m_free(m);
5431
0
        m = SCTP_BUF_NEXT(prev);
5432
0
      }
5433
172
      if (m == NULL) {
5434
0
        control->tail_mbuf = prev;
5435
0
      }
5436
172
      continue;
5437
172
    }
5438
923k
    prev = m;
5439
923k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5440
0
      sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBALLOC, SCTP_BUF_LEN(m));
5441
0
    }
5442
923k
    sctp_sballoc(stcb, sb, m);
5443
923k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
5444
0
      sctp_sblog(sb, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
5445
0
    }
5446
923k
    atomic_add_int(&control->length, SCTP_BUF_LEN(m));
5447
923k
    m = SCTP_BUF_NEXT(m);
5448
923k
  }
5449
922k
  if (prev != NULL) {
5450
922k
    control->tail_mbuf = prev;
5451
922k
  } else {
5452
    /* Everything got collapsed out?? */
5453
0
    if (!control->on_strm_q) {
5454
0
      sctp_free_remote_addr(control->whoFrom);
5455
0
      sctp_free_a_readq(stcb, control);
5456
0
    }
5457
0
    if (inp_read_lock_held == 0)
5458
0
      SCTP_INP_READ_UNLOCK(inp);
5459
0
    return;
5460
0
  }
5461
922k
  if (end) {
5462
922k
    control->end_added = 1;
5463
922k
  }
5464
922k
  TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
5465
922k
  control->on_read_q = 1;
5466
922k
#if defined(__Userspace__)
5467
922k
  sctp_invoke_recv_callback(inp, stcb, control, SCTP_READ_LOCK_HELD);
5468
922k
#endif
5469
922k
  if ((inp != NULL) && (inp->sctp_socket != NULL)) {
5470
922k
    sctp_wakeup_the_read_socket(inp, stcb, so_locked);
5471
922k
  }
5472
922k
  if (inp_read_lock_held == SCTP_READ_LOCK_NOT_HELD) {
5473
986
    SCTP_INP_READ_UNLOCK(inp);
5474
986
  }
5475
922k
}
5476
5477
/*************HOLD THIS COMMENT FOR PATCH FILE OF
5478
 *************ALTERNATE ROUTING CODE
5479
 */
5480
5481
/*************HOLD THIS COMMENT FOR END OF PATCH FILE OF
5482
 *************ALTERNATE ROUTING CODE
5483
 */
5484
5485
struct mbuf *
5486
sctp_generate_cause(uint16_t code, char *info)
5487
8.30k
{
5488
8.30k
  struct mbuf *m;
5489
8.30k
  struct sctp_gen_error_cause *cause;
5490
8.30k
  size_t info_len;
5491
8.30k
  uint16_t len;
5492
5493
8.30k
  if ((code == 0) || (info == NULL)) {
5494
494
    return (NULL);
5495
494
  }
5496
7.80k
  info_len = strlen(info);
5497
7.80k
  if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
5498
0
    return (NULL);
5499
0
  }
5500
7.80k
  len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
5501
7.80k
  m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
5502
7.80k
  if (m != NULL) {
5503
7.80k
    SCTP_BUF_LEN(m) = len;
5504
7.80k
    cause = mtod(m, struct sctp_gen_error_cause *);
5505
7.80k
    cause->code = htons(code);
5506
7.80k
    cause->length = htons(len);
5507
7.80k
    memcpy(cause->info, info, info_len);
5508
7.80k
  }
5509
7.80k
  return (m);
5510
7.80k
}
5511
5512
struct mbuf *
5513
sctp_generate_no_user_data_cause(uint32_t tsn)
5514
11
{
5515
11
  struct mbuf *m;
5516
11
  struct sctp_error_no_user_data *no_user_data_cause;
5517
11
  uint16_t len;
5518
5519
11
  len = (uint16_t)sizeof(struct sctp_error_no_user_data);
5520
11
  m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
5521
11
  if (m != NULL) {
5522
11
    SCTP_BUF_LEN(m) = len;
5523
11
    no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
5524
11
    no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
5525
11
    no_user_data_cause->cause.length = htons(len);
5526
11
    no_user_data_cause->tsn = htonl(tsn);
5527
11
  }
5528
11
  return (m);
5529
11
}
5530
5531
void
5532
sctp_free_bufspace(struct sctp_tcb *stcb, struct sctp_association *asoc,
5533
    struct sctp_tmit_chunk *tp1, int chk_cnt)
5534
436
{
5535
436
  if (tp1->data == NULL) {
5536
0
    return;
5537
0
  }
5538
436
  atomic_subtract_int(&asoc->chunks_on_out_queue, chk_cnt);
5539
#ifdef SCTP_MBCNT_LOGGING
5540
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBCNT_LOGGING_ENABLE) {
5541
    sctp_log_mbcnt(SCTP_LOG_MBCNT_DECREASE,
5542
                   asoc->total_output_queue_size,
5543
                   tp1->book_size,
5544
                   0,
5545
                   tp1->mbcnt);
5546
  }
5547
#endif
5548
436
  if (asoc->total_output_queue_size >= tp1->book_size) {
5549
436
    atomic_subtract_int(&asoc->total_output_queue_size, tp1->book_size);
5550
436
  } else {
5551
0
    asoc->total_output_queue_size = 0;
5552
0
  }
5553
436
  if ((stcb->sctp_socket != NULL) &&
5554
436
      (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
5555
436
       ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
5556
436
    SCTP_SB_DECR(&stcb->sctp_socket->so_snd, tp1->book_size);
5557
436
  }
5558
436
}
5559
5560
int
5561
sctp_release_pr_sctp_chunk(struct sctp_tcb *stcb, struct sctp_tmit_chunk *tp1,
5562
                           uint8_t sent, int so_locked)
5563
0
{
5564
0
  struct sctp_stream_out *strq;
5565
0
  struct sctp_tmit_chunk *chk = NULL, *tp2;
5566
0
  struct sctp_stream_queue_pending *sp;
5567
0
  uint32_t mid;
5568
0
  uint16_t sid;
5569
0
  uint8_t foundeom = 0;
5570
0
  int ret_sz = 0;
5571
0
  int notdone;
5572
0
  int do_wakeup_routine = 0;
5573
5574
#if defined(__APPLE__) && !defined(__Userspace__)
5575
  if (so_locked) {
5576
    sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
5577
  } else {
5578
    sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
5579
  }
5580
#endif
5581
0
  SCTP_TCB_LOCK_ASSERT(stcb);
5582
5583
0
  sid = tp1->rec.data.sid;
5584
0
  mid = tp1->rec.data.mid;
5585
0
  if (sent || ((tp1->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0)) {
5586
0
    stcb->asoc.abandoned_sent[0]++;
5587
0
    stcb->asoc.abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
5588
0
    stcb->asoc.strmout[sid].abandoned_sent[0]++;
5589
#if defined(SCTP_DETAILED_STR_STATS)
5590
    stcb->asoc.strmout[sid].abandoned_sent[PR_SCTP_POLICY(tp1->flags)]++;
5591
#endif
5592
0
  } else {
5593
0
    stcb->asoc.abandoned_unsent[0]++;
5594
0
    stcb->asoc.abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
5595
0
    stcb->asoc.strmout[sid].abandoned_unsent[0]++;
5596
#if defined(SCTP_DETAILED_STR_STATS)
5597
    stcb->asoc.strmout[sid].abandoned_unsent[PR_SCTP_POLICY(tp1->flags)]++;
5598
#endif
5599
0
  }
5600
0
  do {
5601
0
    ret_sz += tp1->book_size;
5602
0
    if (tp1->data != NULL) {
5603
0
      if (tp1->sent < SCTP_DATAGRAM_RESEND) {
5604
0
        sctp_flight_size_decrease(tp1);
5605
0
        sctp_total_flight_decrease(stcb, tp1);
5606
0
      }
5607
0
      sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
5608
0
      stcb->asoc.peers_rwnd += tp1->send_size;
5609
0
      stcb->asoc.peers_rwnd += SCTP_BASE_SYSCTL(sctp_peer_chunk_oh);
5610
0
      if (sent) {
5611
0
        sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
5612
0
      } else {
5613
0
        sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
5614
0
      }
5615
0
      if (tp1->data) {
5616
0
        sctp_m_freem(tp1->data);
5617
0
        tp1->data = NULL;
5618
0
      }
5619
0
      do_wakeup_routine = 1;
5620
0
      if (PR_SCTP_BUF_ENABLED(tp1->flags)) {
5621
0
        stcb->asoc.sent_queue_cnt_removeable--;
5622
0
      }
5623
0
    }
5624
0
    tp1->sent = SCTP_FORWARD_TSN_SKIP;
5625
0
    if ((tp1->rec.data.rcv_flags & SCTP_DATA_NOT_FRAG) ==
5626
0
        SCTP_DATA_NOT_FRAG) {
5627
      /* not frag'ed we ae done   */
5628
0
      notdone = 0;
5629
0
      foundeom = 1;
5630
0
    } else if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5631
      /* end of frag, we are done */
5632
0
      notdone = 0;
5633
0
      foundeom = 1;
5634
0
    } else {
5635
      /*
5636
       * Its a begin or middle piece, we must mark all of
5637
       * it
5638
       */
5639
0
      notdone = 1;
5640
0
      tp1 = TAILQ_NEXT(tp1, sctp_next);
5641
0
    }
5642
0
  } while (tp1 && notdone);
5643
0
  if (foundeom == 0) {
5644
    /*
5645
     * The multi-part message was scattered across the send and
5646
     * sent queue.
5647
     */
5648
0
    TAILQ_FOREACH_SAFE(tp1, &stcb->asoc.send_queue, sctp_next, tp2) {
5649
0
      if ((tp1->rec.data.sid != sid) ||
5650
0
          (!SCTP_MID_EQ(stcb->asoc.idata_supported, tp1->rec.data.mid, mid))) {
5651
0
        break;
5652
0
      }
5653
      /* save to chk in case we have some on stream out
5654
       * queue. If so and we have an un-transmitted one
5655
       * we don't have to fudge the TSN.
5656
       */
5657
0
      chk = tp1;
5658
0
      ret_sz += tp1->book_size;
5659
0
      sctp_free_bufspace(stcb, &stcb->asoc, tp1, 1);
5660
0
      if (sent) {
5661
0
        sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb, 0, tp1, so_locked);
5662
0
      } else {
5663
0
        sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb, 0, tp1, so_locked);
5664
0
      }
5665
0
      if (tp1->data) {
5666
0
        sctp_m_freem(tp1->data);
5667
0
        tp1->data = NULL;
5668
0
      }
5669
      /* No flight involved here book the size to 0 */
5670
0
      tp1->book_size = 0;
5671
0
      if (tp1->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) {
5672
0
        foundeom = 1;
5673
0
      }
5674
0
      do_wakeup_routine = 1;
5675
0
      tp1->sent = SCTP_FORWARD_TSN_SKIP;
5676
0
      TAILQ_REMOVE(&stcb->asoc.send_queue, tp1, sctp_next);
5677
      /* on to the sent queue so we can wait for it to be passed by. */
5678
0
      TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, tp1,
5679
0
            sctp_next);
5680
0
      stcb->asoc.send_queue_cnt--;
5681
0
      stcb->asoc.sent_queue_cnt++;
5682
0
    }
5683
0
  }
5684
0
  if (foundeom == 0) {
5685
    /*
5686
     * Still no eom found. That means there
5687
     * is stuff left on the stream out queue.. yuck.
5688
     */
5689
0
    strq = &stcb->asoc.strmout[sid];
5690
0
    sp = TAILQ_FIRST(&strq->outqueue);
5691
0
    if (sp != NULL) {
5692
0
      sp->discard_rest = 1;
5693
      /*
5694
       * We may need to put a chunk on the
5695
       * queue that holds the TSN that
5696
       * would have been sent with the LAST
5697
       * bit.
5698
       */
5699
0
      if (chk == NULL) {
5700
        /* Yep, we have to */
5701
0
        sctp_alloc_a_chunk(stcb, chk);
5702
0
        if (chk == NULL) {
5703
          /* we are hosed. All we can
5704
           * do is nothing.. which will
5705
           * cause an abort if the peer is
5706
           * paying attention.
5707
           */
5708
0
          goto oh_well;
5709
0
        }
5710
0
        memset(chk, 0, sizeof(*chk));
5711
0
        chk->rec.data.rcv_flags = 0;
5712
0
        chk->sent = SCTP_FORWARD_TSN_SKIP;
5713
0
        chk->asoc = &stcb->asoc;
5714
0
        if (stcb->asoc.idata_supported == 0) {
5715
0
          if (sp->sinfo_flags & SCTP_UNORDERED) {
5716
0
            chk->rec.data.mid = 0;
5717
0
          } else {
5718
0
            chk->rec.data.mid = strq->next_mid_ordered;
5719
0
          }
5720
0
        } else {
5721
0
          if (sp->sinfo_flags & SCTP_UNORDERED) {
5722
0
            chk->rec.data.mid = strq->next_mid_unordered;
5723
0
          } else {
5724
0
            chk->rec.data.mid = strq->next_mid_ordered;
5725
0
          }
5726
0
        }
5727
0
        chk->rec.data.sid = sp->sid;
5728
0
        chk->rec.data.ppid = sp->ppid;
5729
0
        chk->rec.data.context = sp->context;
5730
0
        chk->flags = sp->act_flags;
5731
0
        chk->whoTo = NULL;
5732
#if defined(__FreeBSD__) && !defined(__Userspace__)
5733
        chk->rec.data.tsn = atomic_fetchadd_int(&stcb->asoc.sending_seq, 1);
5734
#else
5735
0
        chk->rec.data.tsn = stcb->asoc.sending_seq++;
5736
0
#endif
5737
0
        strq->chunks_on_queues++;
5738
0
        TAILQ_INSERT_TAIL(&stcb->asoc.sent_queue, chk, sctp_next);
5739
0
        stcb->asoc.sent_queue_cnt++;
5740
0
        stcb->asoc.pr_sctp_cnt++;
5741
0
      }
5742
0
      chk->rec.data.rcv_flags |= SCTP_DATA_LAST_FRAG;
5743
0
      if (sp->sinfo_flags & SCTP_UNORDERED) {
5744
0
        chk->rec.data.rcv_flags |= SCTP_DATA_UNORDERED;
5745
0
      }
5746
0
      if (stcb->asoc.idata_supported == 0) {
5747
0
        if ((sp->sinfo_flags & SCTP_UNORDERED) == 0) {
5748
0
          strq->next_mid_ordered++;
5749
0
        }
5750
0
      } else {
5751
0
        if (sp->sinfo_flags & SCTP_UNORDERED) {
5752
0
          strq->next_mid_unordered++;
5753
0
        } else {
5754
0
          strq->next_mid_ordered++;
5755
0
        }
5756
0
      }
5757
0
    oh_well:
5758
0
      if (sp->data) {
5759
        /* Pull any data to free up the SB and
5760
         * allow sender to "add more" while we
5761
         * will throw away :-)
5762
         */
5763
0
        sctp_free_spbufspace(stcb, &stcb->asoc, sp);
5764
0
        ret_sz += sp->length;
5765
0
        do_wakeup_routine = 1;
5766
0
        sp->some_taken = 1;
5767
0
        sctp_m_freem(sp->data);
5768
0
        sp->data = NULL;
5769
0
        sp->tail_mbuf = NULL;
5770
0
        sp->length = 0;
5771
0
      }
5772
0
    }
5773
0
  }
5774
0
  if (do_wakeup_routine) {
5775
#if defined(__APPLE__) && !defined(__Userspace__)
5776
    struct socket *so;
5777
5778
    so = SCTP_INP_SO(stcb->sctp_ep);
5779
    if (!so_locked) {
5780
      atomic_add_int(&stcb->asoc.refcnt, 1);
5781
      SCTP_TCB_UNLOCK(stcb);
5782
      SCTP_SOCKET_LOCK(so, 1);
5783
      SCTP_TCB_LOCK(stcb);
5784
      atomic_subtract_int(&stcb->asoc.refcnt, 1);
5785
      if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
5786
        /* assoc was freed while we were unlocked */
5787
        SCTP_SOCKET_UNLOCK(so, 1);
5788
        return (ret_sz);
5789
      }
5790
    }
5791
#endif
5792
0
    sctp_sowwakeup(stcb->sctp_ep, stcb->sctp_socket);
5793
#if defined(__APPLE__) && !defined(__Userspace__)
5794
    if (!so_locked) {
5795
      SCTP_SOCKET_UNLOCK(so, 1);
5796
    }
5797
#endif
5798
0
  }
5799
0
  return (ret_sz);
5800
0
}
5801
5802
/*
5803
 * checks to see if the given address, sa, is one that is currently known by
5804
 * the kernel note: can't distinguish the same address on multiple interfaces
5805
 * and doesn't handle multiple addresses with different zone/scope id's note:
5806
 * ifa_ifwithaddr() compares the entire sockaddr struct
5807
 */
5808
struct sctp_ifa *
5809
sctp_find_ifa_in_ep(struct sctp_inpcb *inp, struct sockaddr *addr,
5810
        int holds_lock)
5811
0
{
5812
0
  struct sctp_laddr *laddr;
5813
5814
0
  if (holds_lock == 0) {
5815
0
    SCTP_INP_RLOCK(inp);
5816
0
  }
5817
5818
0
  LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
5819
0
    if (laddr->ifa == NULL)
5820
0
      continue;
5821
0
    if (addr->sa_family != laddr->ifa->address.sa.sa_family)
5822
0
      continue;
5823
0
#ifdef INET
5824
0
    if (addr->sa_family == AF_INET) {
5825
0
      if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5826
0
          laddr->ifa->address.sin.sin_addr.s_addr) {
5827
        /* found him. */
5828
0
        break;
5829
0
      }
5830
0
    }
5831
0
#endif
5832
0
#ifdef INET6
5833
0
    if (addr->sa_family == AF_INET6) {
5834
0
      if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5835
0
             &laddr->ifa->address.sin6)) {
5836
        /* found him. */
5837
0
        break;
5838
0
      }
5839
0
    }
5840
0
#endif
5841
0
#if defined(__Userspace__)
5842
0
    if (addr->sa_family == AF_CONN) {
5843
0
      if (((struct sockaddr_conn *)addr)->sconn_addr == laddr->ifa->address.sconn.sconn_addr) {
5844
        /* found him. */
5845
0
        break;
5846
0
      }
5847
0
    }
5848
0
#endif
5849
0
  }
5850
0
  if (holds_lock == 0) {
5851
0
    SCTP_INP_RUNLOCK(inp);
5852
0
  }
5853
0
  if (laddr != NULL) {
5854
0
    return (laddr->ifa);
5855
0
  } else {
5856
0
    return (NULL);
5857
0
  }
5858
0
}
5859
5860
uint32_t
5861
sctp_get_ifa_hash_val(struct sockaddr *addr)
5862
46.9k
{
5863
46.9k
  switch (addr->sa_family) {
5864
0
#ifdef INET
5865
22.1k
  case AF_INET:
5866
22.1k
  {
5867
22.1k
    struct sockaddr_in *sin;
5868
5869
22.1k
    sin = (struct sockaddr_in *)addr;
5870
22.1k
    return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5871
0
  }
5872
0
#endif
5873
0
#ifdef INET6
5874
3.76k
  case AF_INET6:
5875
3.76k
  {
5876
3.76k
    struct sockaddr_in6 *sin6;
5877
3.76k
    uint32_t hash_of_addr;
5878
5879
3.76k
    sin6 = (struct sockaddr_in6 *)addr;
5880
3.76k
#if !defined(_WIN32) && !(defined(__FreeBSD__) && defined(__Userspace__)) && !defined(__APPLE__)
5881
3.76k
    hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
5882
3.76k
        sin6->sin6_addr.s6_addr32[1] +
5883
3.76k
        sin6->sin6_addr.s6_addr32[2] +
5884
3.76k
        sin6->sin6_addr.s6_addr32[3]);
5885
#else
5886
    hash_of_addr = (((uint32_t *)&sin6->sin6_addr)[0] +
5887
        ((uint32_t *)&sin6->sin6_addr)[1] +
5888
        ((uint32_t *)&sin6->sin6_addr)[2] +
5889
        ((uint32_t *)&sin6->sin6_addr)[3]);
5890
#endif
5891
3.76k
    hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
5892
3.76k
    return (hash_of_addr);
5893
0
  }
5894
0
#endif
5895
0
#if defined(__Userspace__)
5896
21.0k
  case AF_CONN:
5897
21.0k
  {
5898
21.0k
    struct sockaddr_conn *sconn;
5899
21.0k
    uintptr_t temp;
5900
5901
21.0k
    sconn = (struct sockaddr_conn *)addr;
5902
21.0k
    temp = (uintptr_t)sconn->sconn_addr;
5903
21.0k
    return ((uint32_t)(temp ^ (temp >> 16)));
5904
0
  }
5905
0
#endif
5906
0
  default:
5907
0
    break;
5908
46.9k
  }
5909
0
  return (0);
5910
46.9k
}
5911
5912
struct sctp_ifa *
5913
sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
5914
46.9k
{
5915
46.9k
  struct sctp_ifa *sctp_ifap;
5916
46.9k
  struct sctp_vrf *vrf;
5917
46.9k
  struct sctp_ifalist *hash_head;
5918
46.9k
  uint32_t hash_of_addr;
5919
5920
46.9k
  if (holds_lock == 0) {
5921
46.9k
    SCTP_IPI_ADDR_RLOCK();
5922
46.9k
  } else {
5923
3
    SCTP_IPI_ADDR_LOCK_ASSERT();
5924
3
  }
5925
5926
46.9k
  vrf = sctp_find_vrf(vrf_id);
5927
46.9k
  if (vrf == NULL) {
5928
0
    if (holds_lock == 0)
5929
0
      SCTP_IPI_ADDR_RUNLOCK();
5930
0
    return (NULL);
5931
0
  }
5932
5933
46.9k
  hash_of_addr = sctp_get_ifa_hash_val(addr);
5934
5935
46.9k
  hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5936
46.9k
  if (hash_head == NULL) {
5937
0
    SCTP_PRINTF("hash_of_addr:%x mask:%x table:%x - ",
5938
0
          hash_of_addr, (uint32_t)vrf->vrf_addr_hashmark,
5939
0
          (uint32_t)(hash_of_addr & vrf->vrf_addr_hashmark));
5940
0
    sctp_print_address(addr);
5941
0
    SCTP_PRINTF("No such bucket for address\n");
5942
0
    if (holds_lock == 0)
5943
0
      SCTP_IPI_ADDR_RUNLOCK();
5944
5945
0
    return (NULL);
5946
0
  }
5947
46.9k
  LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5948
29.7k
    if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5949
4.51k
      continue;
5950
25.1k
#ifdef INET
5951
25.1k
    if (addr->sa_family == AF_INET) {
5952
4.18k
      if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5953
4.18k
          sctp_ifap->address.sin.sin_addr.s_addr) {
5954
        /* found him. */
5955
0
        break;
5956
0
      }
5957
4.18k
    }
5958
25.1k
#endif
5959
25.1k
#ifdef INET6
5960
25.1k
    if (addr->sa_family == AF_INET6) {
5961
0
      if (SCTP6_ARE_ADDR_EQUAL((struct sockaddr_in6 *)addr,
5962
0
             &sctp_ifap->address.sin6)) {
5963
        /* found him. */
5964
0
        break;
5965
0
      }
5966
0
    }
5967
25.1k
#endif
5968
25.1k
#if defined(__Userspace__)
5969
25.1k
    if (addr->sa_family == AF_CONN) {
5970
21.0k
      if (((struct sockaddr_conn *)addr)->sconn_addr == sctp_ifap->address.sconn.sconn_addr) {
5971
        /* found him. */
5972
21.0k
        break;
5973
21.0k
      }
5974
21.0k
    }
5975
25.1k
#endif
5976
25.1k
  }
5977
46.9k
  if (holds_lock == 0)
5978
46.9k
    SCTP_IPI_ADDR_RUNLOCK();
5979
46.9k
  return (sctp_ifap);
5980
46.9k
}
5981
5982
static void
5983
sctp_user_rcvd(struct sctp_tcb *stcb, uint32_t *freed_so_far, int hold_rlock,
5984
         uint32_t rwnd_req)
5985
16.7k
{
5986
  /* User pulled some data, do we need a rwnd update? */
5987
#if defined(__FreeBSD__) && !defined(__Userspace__)
5988
  struct epoch_tracker et;
5989
#endif
5990
16.7k
  int r_unlocked = 0;
5991
16.7k
  uint32_t dif, rwnd;
5992
16.7k
  struct socket *so = NULL;
5993
5994
16.7k
  if (stcb == NULL)
5995
0
    return;
5996
5997
16.7k
  atomic_add_int(&stcb->asoc.refcnt, 1);
5998
5999
16.7k
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6000
16.7k
      (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
6001
    /* Pre-check If we are freeing no update */
6002
7.23k
    goto no_lock;
6003
7.23k
  }
6004
9.53k
  SCTP_INP_INCR_REF(stcb->sctp_ep);
6005
9.53k
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
6006
9.53k
      (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
6007
0
    goto out;
6008
0
  }
6009
9.53k
  so = stcb->sctp_socket;
6010
9.53k
  if (so == NULL) {
6011
0
    goto out;
6012
0
  }
6013
9.53k
  atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
6014
  /* Have you have freed enough to look */
6015
9.53k
  *freed_so_far = 0;
6016
  /* Yep, its worth a look and the lock overhead */
6017
6018
  /* Figure out what the rwnd would be */
6019
9.53k
  rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
6020
9.53k
  if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
6021
8.94k
    dif = rwnd - stcb->asoc.my_last_reported_rwnd;
6022
8.94k
  } else {
6023
586
    dif = 0;
6024
586
  }
6025
9.53k
  if (dif >= rwnd_req) {
6026
1.47k
    if (hold_rlock) {
6027
1.46k
      SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
6028
1.46k
      r_unlocked = 1;
6029
1.46k
    }
6030
1.47k
    if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6031
      /*
6032
       * One last check before we allow the guy possibly
6033
       * to get in. There is a race, where the guy has not
6034
       * reached the gate. In that case
6035
       */
6036
0
      goto out;
6037
0
    }
6038
1.47k
    SCTP_TCB_LOCK(stcb);
6039
1.47k
    if (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6040
      /* No reports here */
6041
0
      SCTP_TCB_UNLOCK(stcb);
6042
0
      goto out;
6043
0
    }
6044
1.47k
    SCTP_STAT_INCR(sctps_wu_sacks_sent);
6045
#if defined(__FreeBSD__) && !defined(__Userspace__)
6046
    NET_EPOCH_ENTER(et);
6047
#endif
6048
1.47k
    sctp_send_sack(stcb, SCTP_SO_LOCKED);
6049
6050
1.47k
    sctp_chunk_output(stcb->sctp_ep, stcb,
6051
1.47k
          SCTP_OUTPUT_FROM_USR_RCVD, SCTP_SO_LOCKED);
6052
    /* make sure no timer is running */
6053
#if defined(__FreeBSD__) && !defined(__Userspace__)
6054
    NET_EPOCH_EXIT(et);
6055
#endif
6056
1.47k
    sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
6057
1.47k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
6058
1.47k
    SCTP_TCB_UNLOCK(stcb);
6059
8.06k
  } else {
6060
    /* Update how much we have pending */
6061
8.06k
    stcb->freed_by_sorcv_sincelast = dif;
6062
8.06k
  }
6063
9.53k
 out:
6064
9.53k
  if (so && r_unlocked && hold_rlock) {
6065
1.46k
    SCTP_INP_READ_LOCK(stcb->sctp_ep);
6066
1.46k
  }
6067
6068
9.53k
  SCTP_INP_DECR_REF(stcb->sctp_ep);
6069
16.7k
 no_lock:
6070
16.7k
  atomic_subtract_int(&stcb->asoc.refcnt, 1);
6071
16.7k
  return;
6072
9.53k
}
6073
6074
int
6075
sctp_sorecvmsg(struct socket *so,
6076
    struct uio *uio,
6077
    struct mbuf **mp,
6078
    struct sockaddr *from,
6079
    int fromlen,
6080
    int *msg_flags,
6081
    struct sctp_sndrcvinfo *sinfo,
6082
    int filling_sinfo)
6083
924k
{
6084
  /*
6085
   * MSG flags we will look at MSG_DONTWAIT - non-blocking IO.
6086
   * MSG_PEEK - Look don't touch :-D (only valid with OUT mbuf copy
6087
   * mp=NULL thus uio is the copy method to userland) MSG_WAITALL - ??
6088
   * On the way out we may send out any combination of:
6089
   * MSG_NOTIFICATION MSG_EOR
6090
   *
6091
   */
6092
924k
  struct sctp_inpcb *inp = NULL;
6093
924k
  ssize_t my_len = 0;
6094
924k
  ssize_t cp_len = 0;
6095
924k
  int error = 0;
6096
924k
  struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
6097
924k
  struct mbuf *m = NULL;
6098
924k
  struct sctp_tcb *stcb = NULL;
6099
924k
  int wakeup_read_socket = 0;
6100
924k
  int freecnt_applied = 0;
6101
924k
  int out_flags = 0, in_flags = 0;
6102
924k
  int block_allowed = 1;
6103
924k
  uint32_t freed_so_far = 0;
6104
924k
  ssize_t copied_so_far = 0;
6105
924k
  int in_eeor_mode = 0;
6106
924k
  int no_rcv_needed = 0;
6107
924k
  uint32_t rwnd_req = 0;
6108
924k
  int hold_sblock = 0;
6109
924k
  int hold_rlock = 0;
6110
924k
  ssize_t slen = 0;
6111
924k
  uint32_t held_length = 0;
6112
#if defined(__FreeBSD__) && !defined(__Userspace__)
6113
  int sockbuf_lock = 0;
6114
#endif
6115
6116
924k
  if (uio == NULL) {
6117
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6118
0
    return (EINVAL);
6119
0
  }
6120
6121
924k
  if (msg_flags) {
6122
924k
    in_flags = *msg_flags;
6123
924k
    if (in_flags & MSG_PEEK)
6124
924k
      SCTP_STAT_INCR(sctps_read_peeks);
6125
924k
  } else {
6126
0
    in_flags = 0;
6127
0
  }
6128
#if defined(__APPLE__) && !defined(__Userspace__)
6129
#if defined(APPLE_LEOPARD)
6130
  slen = uio->uio_resid;
6131
#else
6132
  slen = uio_resid(uio);
6133
#endif
6134
#else
6135
924k
  slen = uio->uio_resid;
6136
924k
#endif
6137
6138
  /* Pull in and set up our int flags */
6139
924k
  if (in_flags & MSG_OOB) {
6140
    /* Out of band's NOT supported */
6141
0
    return (EOPNOTSUPP);
6142
0
  }
6143
924k
  if ((in_flags & MSG_PEEK) && (mp != NULL)) {
6144
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6145
0
    return (EINVAL);
6146
0
  }
6147
924k
  if ((in_flags & (MSG_DONTWAIT
6148
#if defined(__FreeBSD__) && !defined(__Userspace__)
6149
       | MSG_NBIO
6150
#endif
6151
924k
         )) ||
6152
924k
      SCTP_SO_IS_NBIO(so)) {
6153
924k
    block_allowed = 0;
6154
924k
  }
6155
  /* setup the endpoint */
6156
924k
  inp = (struct sctp_inpcb *)so->so_pcb;
6157
924k
  if (inp == NULL) {
6158
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
6159
0
    return (EFAULT);
6160
0
  }
6161
924k
  rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
6162
  /* Must be at least a MTU's worth */
6163
924k
  if (rwnd_req < SCTP_MIN_RWND)
6164
0
    rwnd_req = SCTP_MIN_RWND;
6165
924k
  in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
6166
924k
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RECV_RWND_LOGGING_ENABLE) {
6167
#if defined(__APPLE__) && !defined(__Userspace__)
6168
#if defined(APPLE_LEOPARD)
6169
    sctp_misc_ints(SCTP_SORECV_ENTER,
6170
                   rwnd_req, in_eeor_mode, SCTP_SBAVAIL(&so->so_rcv), uio->uio_resid);
6171
#else
6172
    sctp_misc_ints(SCTP_SORECV_ENTER,
6173
                   rwnd_req, in_eeor_mode, SCTP_SBAVAIL(&so->so_rcv), uio_resid(uio));
6174
#endif
6175
#else
6176
0
    sctp_misc_ints(SCTP_SORECV_ENTER,
6177
0
                   rwnd_req, in_eeor_mode, SCTP_SBAVAIL(&so->so_rcv), (uint32_t)uio->uio_resid);
6178
0
#endif
6179
0
  }
6180
924k
#if defined(__Userspace__)
6181
924k
  SOCKBUF_LOCK(&so->so_rcv);
6182
924k
  hold_sblock = 1;
6183
924k
#endif
6184
924k
  if (SCTP_BASE_SYSCTL(sctp_logging_level) &SCTP_RECV_RWND_LOGGING_ENABLE) {
6185
#if defined(__APPLE__) && !defined(__Userspace__)
6186
#if defined(APPLE_LEOPARD)
6187
    sctp_misc_ints(SCTP_SORECV_ENTERPL,
6188
                   rwnd_req, block_allowed, SCTP_SBAVAIL(&so->so_rcv), uio->uio_resid);
6189
#else
6190
    sctp_misc_ints(SCTP_SORECV_ENTERPL,
6191
                   rwnd_req, block_allowed, SCTP_SBAVAIL(&so->so_rcv), uio_resid(uio));
6192
#endif
6193
#else
6194
0
    sctp_misc_ints(SCTP_SORECV_ENTERPL,
6195
0
                   rwnd_req, block_allowed, SCTP_SBAVAIL(&so->so_rcv), (uint32_t)uio->uio_resid);
6196
0
#endif
6197
0
  }
6198
6199
#if defined(__APPLE__) && !defined(__Userspace__)
6200
  error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
6201
#endif
6202
#if defined(__FreeBSD__) && !defined(__Userspace__)
6203
  error = SOCK_IO_RECV_LOCK(so, SBLOCKWAIT(in_flags));
6204
#endif
6205
924k
  if (error) {
6206
0
    goto release_unlocked;
6207
0
  }
6208
#if defined(__FreeBSD__) && !defined(__Userspace__)
6209
  sockbuf_lock = 1;
6210
#endif
6211
924k
 restart:
6212
924k
#if defined(__Userspace__)
6213
924k
  if (hold_sblock == 0) {
6214
0
    SOCKBUF_LOCK(&so->so_rcv);
6215
0
    hold_sblock = 1;
6216
0
  }
6217
924k
#endif
6218
#if defined(__APPLE__) && !defined(__Userspace__)
6219
  sbunlock(&so->so_rcv, 1);
6220
#endif
6221
6222
924k
 restart_nosblocks:
6223
924k
  if (hold_sblock == 0) {
6224
0
    SOCKBUF_LOCK(&so->so_rcv);
6225
0
    hold_sblock = 1;
6226
0
  }
6227
924k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
6228
924k
      (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
6229
0
    goto out;
6230
0
  }
6231
#if (defined(__FreeBSD__) || defined(_WIN32)) && !defined(__Userspace__)
6232
  if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) && SCTP_SBAVAIL(&so->so_rcv) == 0) {
6233
#else
6234
924k
  if ((so->so_state & SS_CANTRCVMORE) && SCTP_SBAVAIL(&so->so_rcv) == 0) {
6235
0
#endif
6236
0
    if (so->so_error) {
6237
0
      error = so->so_error;
6238
0
      if ((in_flags & MSG_PEEK) == 0)
6239
0
        so->so_error = 0;
6240
0
      goto out;
6241
0
    } else {
6242
0
      if (SCTP_SBAVAIL(&so->so_rcv) == 0) {
6243
        /* indicate EOF */
6244
0
        error = 0;
6245
0
        goto out;
6246
0
      }
6247
0
    }
6248
0
  }
6249
924k
  if (SCTP_SBAVAIL(&so->so_rcv) <= held_length) {
6250
1.56k
    if (so->so_error) {
6251
1.56k
      error = so->so_error;
6252
1.56k
      if ((in_flags & MSG_PEEK) == 0) {
6253
1.56k
        so->so_error = 0;
6254
1.56k
      }
6255
1.56k
      goto out;
6256
1.56k
    }
6257
0
    if ((SCTP_SBAVAIL(&so->so_rcv) == 0) &&
6258
0
        ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6259
0
         (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6260
0
      if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) == 0) {
6261
        /* For active open side clear flags for re-use
6262
         * passive open is blocked by connect.
6263
         */
6264
0
        if (inp->sctp_flags & SCTP_PCB_FLAGS_WAS_ABORTED) {
6265
          /* You were aborted, passive side always hits here */
6266
0
          SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
6267
0
          error = ECONNRESET;
6268
0
        }
6269
0
        so->so_state &= ~(SS_ISCONNECTING |
6270
0
              SS_ISDISCONNECTING |
6271
0
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
6272
0
              SS_ISCONFIRMING |
6273
0
#endif
6274
0
              SS_ISCONNECTED);
6275
0
        if (error == 0) {
6276
0
          if ((inp->sctp_flags & SCTP_PCB_FLAGS_WAS_CONNECTED) == 0) {
6277
0
            SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOTCONN);
6278
0
            error = ENOTCONN;
6279
0
          }
6280
0
        }
6281
0
        goto out;
6282
0
      }
6283
0
    }
6284
0
    if (block_allowed) {
6285
#if defined(__FreeBSD__) && !defined(__Userspace__)
6286
      error = sbwait(so, SO_RCV);
6287
#else
6288
0
      error = sbwait(&so->so_rcv);
6289
0
#endif
6290
0
      if (error) {
6291
0
        goto out;
6292
0
      }
6293
0
      held_length = 0;
6294
0
      goto restart_nosblocks;
6295
0
    } else {
6296
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EWOULDBLOCK);
6297
0
      error = EWOULDBLOCK;
6298
0
      goto out;
6299
0
    }
6300
0
  }
6301
923k
  if (hold_sblock == 1) {
6302
923k
    SOCKBUF_UNLOCK(&so->so_rcv);
6303
923k
    hold_sblock = 0;
6304
923k
  }
6305
#if defined(__APPLE__) && !defined(__Userspace__)
6306
  error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
6307
#endif
6308
  /* we possibly have data we can read */
6309
  /*sa_ignore FREED_MEMORY*/
6310
923k
  control = TAILQ_FIRST(&inp->read_queue);
6311
923k
  if (control == NULL) {
6312
    /* This could be happening since
6313
     * the appender did the increment but as not
6314
     * yet did the tailq insert onto the read_queue
6315
     */
6316
0
    if (hold_rlock == 0) {
6317
0
      SCTP_INP_READ_LOCK(inp);
6318
0
    }
6319
0
    control = TAILQ_FIRST(&inp->read_queue);
6320
0
    if ((control == NULL) && (SCTP_SBAVAIL(&so->so_rcv) > 0)) {
6321
0
#ifdef INVARIANTS
6322
0
      panic("Huh, its non zero and nothing on control?");
6323
0
#endif
6324
0
      SCTP_SB_CLEAR(so->so_rcv);
6325
0
    }
6326
0
    SCTP_INP_READ_UNLOCK(inp);
6327
0
    hold_rlock = 0;
6328
0
    goto restart;
6329
0
  }
6330
6331
923k
  if ((control->length == 0) &&
6332
923k
      (control->do_not_ref_stcb)) {
6333
    /* Clean up code for freeing assoc that left behind a pdapi..
6334
     * maybe a peer in EEOR that just closed after sending and
6335
     * never indicated a EOR.
6336
     */
6337
0
    if (hold_rlock == 0) {
6338
0
      hold_rlock = 1;
6339
0
      SCTP_INP_READ_LOCK(inp);
6340
0
    }
6341
0
    control->held_length = 0;
6342
0
    if (control->data) {
6343
      /* Hmm there is data here .. fix */
6344
0
      struct mbuf *m_tmp;
6345
0
      int cnt = 0;
6346
0
      m_tmp = control->data;
6347
0
      while (m_tmp) {
6348
0
        cnt += SCTP_BUF_LEN(m_tmp);
6349
0
        if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6350
0
          control->tail_mbuf = m_tmp;
6351
0
          control->end_added = 1;
6352
0
        }
6353
0
        m_tmp = SCTP_BUF_NEXT(m_tmp);
6354
0
      }
6355
0
      control->length = cnt;
6356
0
    } else {
6357
      /* remove it */
6358
0
      TAILQ_REMOVE(&inp->read_queue, control, next);
6359
      /* Add back any hidden data */
6360
0
      sctp_free_remote_addr(control->whoFrom);
6361
0
      sctp_free_a_readq(stcb, control);
6362
0
    }
6363
0
    if (hold_rlock) {
6364
0
      hold_rlock = 0;
6365
0
      SCTP_INP_READ_UNLOCK(inp);
6366
0
    }
6367
0
    goto restart;
6368
0
  }
6369
923k
  if ((control->length == 0) &&
6370
923k
      (control->end_added == 1)) {
6371
    /* Do we also need to check for (control->pdapi_aborted == 1)? */
6372
0
    if (hold_rlock == 0) {
6373
0
      hold_rlock = 1;
6374
0
      SCTP_INP_READ_LOCK(inp);
6375
0
    }
6376
0
    TAILQ_REMOVE(&inp->read_queue, control, next);
6377
0
    if (control->data) {
6378
0
#ifdef INVARIANTS
6379
0
      panic("control->data not null but control->length == 0");
6380
#else
6381
      SCTP_PRINTF("Strange, data left in the control buffer. Cleaning up.\n");
6382
      sctp_m_freem(control->data);
6383
      control->data = NULL;
6384
#endif
6385
0
    }
6386
0
    if (control->aux_data) {
6387
0
      sctp_m_free (control->aux_data);
6388
0
      control->aux_data = NULL;
6389
0
    }
6390
0
#ifdef INVARIANTS
6391
0
    if (control->on_strm_q) {
6392
0
      panic("About to free ctl:%p so:%p and its in %d",
6393
0
            control, so, control->on_strm_q);
6394
0
    }
6395
0
#endif
6396
0
    sctp_free_remote_addr(control->whoFrom);
6397
0
    sctp_free_a_readq(stcb, control);
6398
0
    if (hold_rlock) {
6399
0
      hold_rlock = 0;
6400
0
      SCTP_INP_READ_UNLOCK(inp);
6401
0
    }
6402
0
    goto restart;
6403
0
  }
6404
923k
  if (control->length == 0) {
6405
0
    if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE)) &&
6406
0
        (filling_sinfo)) {
6407
      /* find a more suitable one then this */
6408
0
      ctl = TAILQ_NEXT(control, next);
6409
0
      while (ctl) {
6410
0
        if ((ctl->stcb != control->stcb) && (ctl->length) &&
6411
0
            (ctl->some_taken ||
6412
0
             (ctl->spec_flags & M_NOTIFICATION) ||
6413
0
             ((ctl->do_not_ref_stcb == 0) &&
6414
0
              (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))
6415
0
          ) {
6416
          /*-
6417
           * If we have a different TCB next, and there is data
6418
           * present. If we have already taken some (pdapi), OR we can
6419
           * ref the tcb and no delivery as started on this stream, we
6420
           * take it. Note we allow a notification on a different
6421
           * assoc to be delivered..
6422
           */
6423
0
          control = ctl;
6424
0
          goto found_one;
6425
0
        } else if ((sctp_is_feature_on(inp, SCTP_PCB_FLAGS_INTERLEAVE_STRMS)) &&
6426
0
             (ctl->length) &&
6427
0
             ((ctl->some_taken) ||
6428
0
              ((ctl->do_not_ref_stcb == 0) &&
6429
0
               ((ctl->spec_flags & M_NOTIFICATION) == 0) &&
6430
0
               (ctl->stcb->asoc.strmin[ctl->sinfo_stream].delivery_started == 0)))) {
6431
          /*-
6432
           * If we have the same tcb, and there is data present, and we
6433
           * have the strm interleave feature present. Then if we have
6434
           * taken some (pdapi) or we can refer to tht tcb AND we have
6435
           * not started a delivery for this stream, we can take it.
6436
           * Note we do NOT allow a notification on the same assoc to
6437
           * be delivered.
6438
           */
6439
0
          control = ctl;
6440
0
          goto found_one;
6441
0
        }
6442
0
        ctl = TAILQ_NEXT(ctl, next);
6443
0
      }
6444
0
    }
6445
    /*
6446
     * if we reach here, not suitable replacement is available
6447
     * <or> fragment interleave is NOT on. So stuff the sb_cc
6448
     * into the our held count, and its time to sleep again.
6449
     */
6450
0
    held_length = SCTP_SBAVAIL(&so->so_rcv);
6451
0
    control->held_length = SCTP_SBAVAIL(&so->so_rcv);
6452
0
    goto restart;
6453
0
  }
6454
  /* Clear the held length since there is something to read */
6455
923k
  control->held_length = 0;
6456
923k
 found_one:
6457
  /*
6458
   * If we reach here, control has a some data for us to read off.
6459
   * Note that stcb COULD be NULL.
6460
   */
6461
923k
  if (hold_rlock == 0) {
6462
923k
    hold_rlock = 1;
6463
923k
    SCTP_INP_READ_LOCK(inp);
6464
923k
  }
6465
923k
  control->some_taken++;
6466
923k
  stcb = control->stcb;
6467
923k
  if (stcb) {
6468
923k
    if ((control->do_not_ref_stcb == 0) &&
6469
923k
        (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
6470
0
      if (freecnt_applied == 0)
6471
0
        stcb = NULL;
6472
923k
    } else if (control->do_not_ref_stcb == 0) {
6473
      /* you can't free it on me please */
6474
      /*
6475
       * The lock on the socket buffer protects us so the
6476
       * free code will stop. But since we used the socketbuf
6477
       * lock and the sender uses the tcb_lock to increment,
6478
       * we need to use the atomic add to the refcnt
6479
       */
6480
592k
      if (freecnt_applied) {
6481
0
#ifdef INVARIANTS
6482
0
        panic("refcnt already incremented");
6483
#else
6484
        SCTP_PRINTF("refcnt already incremented?\n");
6485
#endif
6486
592k
      } else {
6487
592k
        atomic_add_int(&stcb->asoc.refcnt, 1);
6488
592k
        freecnt_applied = 1;
6489
592k
      }
6490
      /*
6491
       * Setup to remember how much we have not yet told
6492
       * the peer our rwnd has opened up. Note we grab
6493
       * the value from the tcb from last time.
6494
       * Note too that sack sending clears this when a sack
6495
       * is sent, which is fine. Once we hit the rwnd_req,
6496
       * we then will go to the sctp_user_rcvd() that will
6497
       * not lock until it KNOWs it MUST send a WUP-SACK.
6498
       */
6499
592k
      freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
6500
592k
      stcb->freed_by_sorcv_sincelast = 0;
6501
592k
    }
6502
923k
  }
6503
923k
  if (stcb &&
6504
923k
      ((control->spec_flags & M_NOTIFICATION) == 0) &&
6505
923k
      control->do_not_ref_stcb == 0) {
6506
1.00k
    stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
6507
1.00k
  }
6508
6509
  /* First lets get off the sinfo and sockaddr info */
6510
923k
  if ((sinfo != NULL) && (filling_sinfo != 0)) {
6511
923k
    sinfo->sinfo_stream = control->sinfo_stream;
6512
923k
    sinfo->sinfo_ssn = (uint16_t)control->mid;
6513
923k
    sinfo->sinfo_flags = control->sinfo_flags;
6514
923k
    sinfo->sinfo_ppid = control->sinfo_ppid;
6515
923k
    sinfo->sinfo_context =control->sinfo_context;
6516
923k
    sinfo->sinfo_timetolive = control->sinfo_timetolive;
6517
923k
    sinfo->sinfo_tsn = control->sinfo_tsn;
6518
923k
    sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
6519
923k
    sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
6520
923k
    nxt = TAILQ_NEXT(control, next);
6521
923k
    if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6522
923k
        sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
6523
923k
      struct sctp_extrcvinfo *s_extra;
6524
923k
      s_extra = (struct sctp_extrcvinfo *)sinfo;
6525
923k
      if ((nxt) &&
6526
923k
          (nxt->length)) {
6527
914k
        s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
6528
914k
        if (nxt->sinfo_flags & SCTP_UNORDERED) {
6529
515
          s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
6530
515
        }
6531
914k
        if (nxt->spec_flags & M_NOTIFICATION) {
6532
913k
          s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
6533
913k
        }
6534
914k
        s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
6535
914k
        s_extra->serinfo_next_length = nxt->length;
6536
914k
        s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
6537
914k
        s_extra->serinfo_next_stream = nxt->sinfo_stream;
6538
914k
        if (nxt->tail_mbuf != NULL) {
6539
914k
          if (nxt->end_added) {
6540
914k
            s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
6541
914k
          }
6542
914k
        }
6543
914k
      } else {
6544
        /* we explicitly 0 this, since the memcpy got
6545
         * some other things beyond the older sinfo_
6546
         * that is on the control's structure :-D
6547
         */
6548
8.81k
        nxt = NULL;
6549
8.81k
        s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6550
8.81k
        s_extra->serinfo_next_aid = 0;
6551
8.81k
        s_extra->serinfo_next_length = 0;
6552
8.81k
        s_extra->serinfo_next_ppid = 0;
6553
8.81k
        s_extra->serinfo_next_stream = 0;
6554
8.81k
      }
6555
923k
    }
6556
    /*
6557
     * update off the real current cum-ack, if we have an stcb.
6558
     */
6559
923k
    if ((control->do_not_ref_stcb == 0) && stcb)
6560
592k
      sinfo->sinfo_cumtsn = stcb->asoc.cumulative_tsn;
6561
    /*
6562
     * mask off the high bits, we keep the actual chunk bits in
6563
     * there.
6564
     */
6565
923k
    sinfo->sinfo_flags &= 0x00ff;
6566
923k
    if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
6567
897
      sinfo->sinfo_flags |= SCTP_UNORDERED;
6568
897
    }
6569
923k
  }
6570
#ifdef SCTP_ASOCLOG_OF_TSNS
6571
  {
6572
    int index, newindex;
6573
    struct sctp_pcbtsn_rlog *entry;
6574
    do {
6575
      index = inp->readlog_index;
6576
      newindex = index + 1;
6577
      if (newindex >= SCTP_READ_LOG_SIZE) {
6578
        newindex = 0;
6579
      }
6580
    } while (atomic_cmpset_int(&inp->readlog_index, index, newindex) == 0);
6581
    entry = &inp->readlog[index];
6582
    entry->vtag = control->sinfo_assoc_id;
6583
    entry->strm = control->sinfo_stream;
6584
    entry->seq = (uint16_t)control->mid;
6585
    entry->sz = control->length;
6586
    entry->flgs = control->sinfo_flags;
6587
  }
6588
#endif
6589
923k
  if ((fromlen > 0) && (from != NULL)) {
6590
923k
    union sctp_sockstore store;
6591
923k
    size_t len;
6592
6593
923k
    switch (control->whoFrom->ro._l_addr.sa.sa_family) {
6594
0
#ifdef INET6
6595
17.3k
      case AF_INET6:
6596
17.3k
        len = sizeof(struct sockaddr_in6);
6597
17.3k
        store.sin6 = control->whoFrom->ro._l_addr.sin6;
6598
17.3k
        store.sin6.sin6_port = control->port_from;
6599
17.3k
        break;
6600
0
#endif
6601
0
#ifdef INET
6602
382
      case AF_INET:
6603
382
#ifdef INET6
6604
382
        if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NEEDS_MAPPED_V4)) {
6605
0
          len = sizeof(struct sockaddr_in6);
6606
0
          in6_sin_2_v4mapsin6(&control->whoFrom->ro._l_addr.sin,
6607
0
                  &store.sin6);
6608
0
          store.sin6.sin6_port = control->port_from;
6609
382
        } else {
6610
382
          len = sizeof(struct sockaddr_in);
6611
382
          store.sin = control->whoFrom->ro._l_addr.sin;
6612
382
          store.sin.sin_port = control->port_from;
6613
382
        }
6614
#else
6615
        len = sizeof(struct sockaddr_in);
6616
        store.sin = control->whoFrom->ro._l_addr.sin;
6617
        store.sin.sin_port = control->port_from;
6618
#endif
6619
382
        break;
6620
0
#endif
6621
0
#if defined(__Userspace__)
6622
905k
      case AF_CONN:
6623
905k
        len = sizeof(struct sockaddr_conn);
6624
905k
        store.sconn = control->whoFrom->ro._l_addr.sconn;
6625
905k
        store.sconn.sconn_port = control->port_from;
6626
905k
        break;
6627
0
#endif
6628
0
      default:
6629
0
        len = 0;
6630
0
        break;
6631
923k
    }
6632
923k
    memcpy(from, &store, min((size_t)fromlen, len));
6633
#if defined(SCTP_EMBEDDED_V6_SCOPE)
6634
#ifdef INET6
6635
    {
6636
      struct sockaddr_in6 lsa6, *from6;
6637
6638
      from6 = (struct sockaddr_in6 *)from;
6639
      sctp_recover_scope_mac(from6, (&lsa6));
6640
    }
6641
#endif
6642
#endif
6643
923k
  }
6644
923k
  if (hold_rlock) {
6645
923k
    SCTP_INP_READ_UNLOCK(inp);
6646
923k
    hold_rlock = 0;
6647
923k
  }
6648
923k
  if (hold_sblock) {
6649
0
    SOCKBUF_UNLOCK(&so->so_rcv);
6650
0
    hold_sblock = 0;
6651
0
  }
6652
  /* now copy out what data we can */
6653
923k
  if (mp == NULL) {
6654
    /* copy out each mbuf in the chain up to length */
6655
923k
  get_more_data:
6656
923k
    m = control->data;
6657
925k
    while (m) {
6658
      /* Move out all we can */
6659
#if defined(__APPLE__) && !defined(__Userspace__)
6660
#if defined(APPLE_LEOPARD)
6661
      cp_len = uio->uio_resid;
6662
#else
6663
      cp_len = uio_resid(uio);
6664
#endif
6665
#else
6666
925k
      cp_len = uio->uio_resid;
6667
925k
#endif
6668
925k
      my_len = SCTP_BUF_LEN(m);
6669
925k
      if (cp_len > my_len) {
6670
        /* not enough in this buf */
6671
924k
        cp_len = my_len;
6672
924k
      }
6673
925k
      if (hold_rlock) {
6674
1.76k
        SCTP_INP_READ_UNLOCK(inp);
6675
1.76k
        hold_rlock = 0;
6676
1.76k
      }
6677
#if defined(__APPLE__) && !defined(__Userspace__)
6678
      SCTP_SOCKET_UNLOCK(so, 0);
6679
#endif
6680
925k
      if (cp_len > 0)
6681
925k
        error = uiomove(mtod(m, char *), (int)cp_len, uio);
6682
#if defined(__APPLE__) && !defined(__Userspace__)
6683
      SCTP_SOCKET_LOCK(so, 0);
6684
#endif
6685
      /* re-read */
6686
925k
      if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
6687
0
        goto release;
6688
0
      }
6689
6690
925k
      if ((control->do_not_ref_stcb == 0) && stcb &&
6691
925k
          stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6692
0
        no_rcv_needed = 1;
6693
0
      }
6694
925k
      if (error) {
6695
        /* error we are out of here */
6696
0
        goto release;
6697
0
      }
6698
925k
      SCTP_INP_READ_LOCK(inp);
6699
925k
      hold_rlock = 1;
6700
925k
      if (cp_len == SCTP_BUF_LEN(m)) {
6701
924k
        if ((SCTP_BUF_NEXT(m)== NULL) &&
6702
924k
            (control->end_added)) {
6703
922k
          out_flags |= MSG_EOR;
6704
922k
          if ((control->do_not_ref_stcb == 0) &&
6705
922k
              (control->stcb != NULL) &&
6706
922k
              ((control->spec_flags & M_NOTIFICATION) == 0))
6707
859
            control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6708
922k
        }
6709
924k
        if (control->spec_flags & M_NOTIFICATION) {
6710
922k
          out_flags |= MSG_NOTIFICATION;
6711
922k
        }
6712
        /* we ate up the mbuf */
6713
924k
        if (in_flags & MSG_PEEK) {
6714
          /* just looking */
6715
0
          m = SCTP_BUF_NEXT(m);
6716
0
          copied_so_far += cp_len;
6717
924k
        } else {
6718
          /* dispose of the mbuf */
6719
924k
          if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6720
0
            sctp_sblog(&so->so_rcv,
6721
0
               control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
6722
0
          }
6723
1.84M
          sctp_sbfree(control, stcb, &so->so_rcv, m);
6724
924k
          if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6725
0
            sctp_sblog(&so->so_rcv,
6726
0
               control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
6727
0
          }
6728
924k
          copied_so_far += cp_len;
6729
924k
          freed_so_far += (uint32_t)cp_len;
6730
924k
          freed_so_far += MSIZE;
6731
924k
          atomic_subtract_int(&control->length, (int)cp_len);
6732
924k
          control->data = sctp_m_free(m);
6733
924k
          m = control->data;
6734
          /* been through it all, must hold sb lock ok to null tail */
6735
924k
          if (control->data == NULL) {
6736
922k
#ifdef INVARIANTS
6737
#if defined(__FreeBSD__) && !defined(__Userspace__)
6738
            if ((control->end_added == 0) ||
6739
                (TAILQ_NEXT(control, next) == NULL)) {
6740
              /* If the end is not added, OR the
6741
               * next is NOT null we MUST have the lock.
6742
               */
6743
              if (mtx_owned(&inp->inp_rdata_mtx) == 0) {
6744
                panic("Hmm we don't own the lock?");
6745
              }
6746
            }
6747
#endif
6748
922k
#endif
6749
922k
            control->tail_mbuf = NULL;
6750
922k
#ifdef INVARIANTS
6751
922k
            if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
6752
0
              panic("end_added, nothing left and no MSG_EOR");
6753
0
            }
6754
922k
#endif
6755
922k
          }
6756
924k
        }
6757
924k
      } else {
6758
        /* Do we need to trim the mbuf? */
6759
243
        if (control->spec_flags & M_NOTIFICATION) {
6760
0
          out_flags |= MSG_NOTIFICATION;
6761
0
        }
6762
243
        if ((in_flags & MSG_PEEK) == 0) {
6763
243
          SCTP_BUF_RESV_UF(m, cp_len);
6764
243
          SCTP_BUF_LEN(m) -= (int)cp_len;
6765
243
          if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6766
0
            sctp_sblog(&so->so_rcv, control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, (int)cp_len);
6767
0
          }
6768
243
          SCTP_SB_DECR(&so->so_rcv, cp_len);
6769
243
          if ((control->do_not_ref_stcb == 0) &&
6770
243
              stcb) {
6771
149
            atomic_subtract_int(&stcb->asoc.sb_cc, (int)cp_len);
6772
149
          }
6773
243
          copied_so_far += cp_len;
6774
243
          freed_so_far += (uint32_t)cp_len;
6775
243
          freed_so_far += MSIZE;
6776
243
          if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
6777
0
            sctp_sblog(&so->so_rcv, control->do_not_ref_stcb?NULL:stcb,
6778
0
                 SCTP_LOG_SBRESULT, 0);
6779
0
          }
6780
243
          atomic_subtract_int(&control->length, (int)cp_len);
6781
243
        } else {
6782
0
          copied_so_far += cp_len;
6783
0
        }
6784
243
      }
6785
#if defined(__APPLE__) && !defined(__Userspace__)
6786
#if defined(APPLE_LEOPARD)
6787
      if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
6788
#else
6789
      if ((out_flags & MSG_EOR) || (uio_resid(uio) == 0)) {
6790
#endif
6791
#else
6792
925k
      if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
6793
923k
#endif
6794
923k
        break;
6795
923k
      }
6796
1.76k
      if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6797
1.76k
          (control->do_not_ref_stcb == 0) &&
6798
1.76k
          (freed_so_far >= rwnd_req)) {
6799
43
        sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6800
43
      }
6801
1.76k
    } /* end while(m) */
6802
    /*
6803
     * At this point we have looked at it all and we either have
6804
     * a MSG_EOR/or read all the user wants... <OR>
6805
     * control->length == 0.
6806
     */
6807
923k
    if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
6808
      /* we are done with this control */
6809
922k
      if (control->length == 0) {
6810
922k
        if (control->data) {
6811
0
#ifdef INVARIANTS
6812
0
          panic("control->data not null at read eor?");
6813
#else
6814
          SCTP_PRINTF("Strange, data left in the control buffer .. invariants would panic?\n");
6815
          sctp_m_freem(control->data);
6816
          control->data = NULL;
6817
#endif
6818
0
        }
6819
922k
      done_with_control:
6820
922k
        if (hold_rlock == 0) {
6821
0
          SCTP_INP_READ_LOCK(inp);
6822
0
          hold_rlock = 1;
6823
0
        }
6824
922k
        TAILQ_REMOVE(&inp->read_queue, control, next);
6825
        /* Add back any hidden data */
6826
922k
        if (control->held_length) {
6827
0
          held_length = 0;
6828
0
          control->held_length = 0;
6829
0
          wakeup_read_socket = 1;
6830
0
        }
6831
922k
        if (control->aux_data) {
6832
0
          sctp_m_free (control->aux_data);
6833
0
          control->aux_data = NULL;
6834
0
        }
6835
922k
        no_rcv_needed = control->do_not_ref_stcb;
6836
922k
        sctp_free_remote_addr(control->whoFrom);
6837
922k
        control->data = NULL;
6838
922k
#ifdef INVARIANTS
6839
922k
        if (control->on_strm_q) {
6840
0
          panic("About to free ctl:%p so:%p and its in %d",
6841
0
                control, so, control->on_strm_q);
6842
0
        }
6843
922k
#endif
6844
1.84M
        sctp_free_a_readq(stcb, control);
6845
1.84M
        control = NULL;
6846
1.84M
        if ((freed_so_far >= rwnd_req) &&
6847
922k
            (no_rcv_needed == 0))
6848
16.7k
          sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6849
6850
1.84M
      } else {
6851
        /*
6852
         * The user did not read all of this
6853
         * message, turn off the returned MSG_EOR
6854
         * since we are leaving more behind on the
6855
         * control to read.
6856
         */
6857
0
#ifdef INVARIANTS
6858
0
        if (control->end_added &&
6859
0
            (control->data == NULL) &&
6860
0
            (control->tail_mbuf == NULL)) {
6861
0
          panic("Gak, control->length is corrupt?");
6862
0
        }
6863
0
#endif
6864
0
        no_rcv_needed = control->do_not_ref_stcb;
6865
0
        out_flags &= ~MSG_EOR;
6866
0
      }
6867
922k
    }
6868
923k
    if (out_flags & MSG_EOR) {
6869
922k
      goto release;
6870
922k
    }
6871
#if defined(__APPLE__) && !defined(__Userspace__)
6872
#if defined(APPLE_LEOPARD)
6873
    if ((uio->uio_resid == 0) ||
6874
#else
6875
    if ((uio_resid(uio) == 0) ||
6876
#endif
6877
#else
6878
244
    if ((uio->uio_resid == 0) ||
6879
244
#endif
6880
244
        ((in_eeor_mode) &&
6881
244
         (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))) {
6882
244
      goto release;
6883
244
    }
6884
    /*
6885
     * If I hit here the receiver wants more and this message is
6886
     * NOT done (pd-api). So two questions. Can we block? if not
6887
     * we are done. Did the user NOT set MSG_WAITALL?
6888
     */
6889
0
    if (block_allowed == 0) {
6890
0
      goto release;
6891
0
    }
6892
    /*
6893
     * We need to wait for more data a few things:
6894
     * - We don't release the I/O lock so we don't get someone else
6895
     *   reading.
6896
     * - We must be sure to account for the case where what is added
6897
     *   is NOT to our control when we wakeup.
6898
     */
6899
6900
    /* Do we need to tell the transport a rwnd update might be
6901
     * needed before we go to sleep?
6902
     */
6903
0
    if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6904
0
        ((freed_so_far >= rwnd_req) &&
6905
0
         (control->do_not_ref_stcb == 0) &&
6906
0
         (no_rcv_needed == 0))) {
6907
0
      sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6908
0
    }
6909
0
  wait_some_more:
6910
#if (defined(__FreeBSD__) || defined(_WIN32)) && !defined(__Userspace__)
6911
    if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
6912
      goto release;
6913
    }
6914
#else
6915
0
    if (so->so_state & SS_CANTRCVMORE) {
6916
0
      goto release;
6917
0
    }
6918
0
#endif
6919
6920
0
    if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE)
6921
0
      goto release;
6922
6923
0
    if (hold_rlock == 1) {
6924
0
      SCTP_INP_READ_UNLOCK(inp);
6925
0
      hold_rlock = 0;
6926
0
    }
6927
0
    if (hold_sblock == 0) {
6928
0
      SOCKBUF_LOCK(&so->so_rcv);
6929
0
      hold_sblock = 1;
6930
0
    }
6931
0
    if ((copied_so_far) && (control->length == 0) &&
6932
0
        (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_FRAG_INTERLEAVE))) {
6933
0
      goto release;
6934
0
    }
6935
#if defined(__APPLE__) && !defined(__Userspace__)
6936
    sbunlock(&so->so_rcv, 1);
6937
#endif
6938
0
    if (SCTP_SBAVAIL(&so->so_rcv) <= control->held_length) {
6939
#if defined(__FreeBSD__) && !defined(__Userspace__)
6940
      error = sbwait(so, SO_RCV);
6941
#else
6942
0
      error = sbwait(&so->so_rcv);
6943
0
#endif
6944
0
      if (error) {
6945
#if defined(__APPLE__) && !defined(__Userspace__)
6946
        goto release_unlocked;
6947
#else
6948
0
        goto release;
6949
0
#endif
6950
0
      }
6951
0
      control->held_length = 0;
6952
0
    }
6953
#if defined(__APPLE__) && !defined(__Userspace__)
6954
    error = sblock(&so->so_rcv, SBLOCKWAIT(in_flags));
6955
#endif
6956
0
    if (hold_sblock) {
6957
0
      SOCKBUF_UNLOCK(&so->so_rcv);
6958
0
      hold_sblock = 0;
6959
0
    }
6960
0
    if (control->length == 0) {
6961
      /* still nothing here */
6962
0
      if (control->end_added == 1) {
6963
        /* he aborted, or is done i.e.did a shutdown */
6964
0
        out_flags |= MSG_EOR;
6965
0
        if (control->pdapi_aborted) {
6966
0
          if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6967
0
            control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6968
6969
0
          out_flags |= MSG_TRUNC;
6970
0
        } else {
6971
0
          if ((control->do_not_ref_stcb == 0) && ((control->spec_flags & M_NOTIFICATION) == 0))
6972
0
            control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6973
0
        }
6974
0
        goto done_with_control;
6975
0
      }
6976
0
      if (SCTP_SBAVAIL(&so->so_rcv) > held_length) {
6977
0
        control->held_length = SCTP_SBAVAIL(&so->so_rcv);
6978
0
        held_length = 0;
6979
0
      }
6980
0
      goto wait_some_more;
6981
0
    } else if (control->data == NULL) {
6982
      /* we must re-sync since data
6983
       * is probably being added
6984
       */
6985
0
      SCTP_INP_READ_LOCK(inp);
6986
0
      if ((control->length > 0) && (control->data == NULL)) {
6987
        /* big trouble.. we have the lock and its corrupt? */
6988
0
#ifdef INVARIANTS
6989
0
        panic ("Impossible data==NULL length !=0");
6990
0
#endif
6991
0
        out_flags |= MSG_EOR;
6992
0
        out_flags |= MSG_TRUNC;
6993
0
        control->length = 0;
6994
0
        SCTP_INP_READ_UNLOCK(inp);
6995
0
        goto done_with_control;
6996
0
      }
6997
0
      SCTP_INP_READ_UNLOCK(inp);
6998
      /* We will fall around to get more data */
6999
0
    }
7000
0
    goto get_more_data;
7001
0
  } else {
7002
    /*-
7003
     * Give caller back the mbuf chain,
7004
     * store in uio_resid the length
7005
     */
7006
0
    wakeup_read_socket = 0;
7007
0
    if ((control->end_added == 0) ||
7008
0
        (TAILQ_NEXT(control, next) == NULL)) {
7009
      /* Need to get rlock */
7010
0
      if (hold_rlock == 0) {
7011
0
        SCTP_INP_READ_LOCK(inp);
7012
0
        hold_rlock = 1;
7013
0
      }
7014
0
    }
7015
0
    if (control->end_added) {
7016
0
      out_flags |= MSG_EOR;
7017
0
      if ((control->do_not_ref_stcb == 0) &&
7018
0
          (control->stcb != NULL) &&
7019
0
          ((control->spec_flags & M_NOTIFICATION) == 0))
7020
0
        control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
7021
0
    }
7022
0
    if (control->spec_flags & M_NOTIFICATION) {
7023
0
      out_flags |= MSG_NOTIFICATION;
7024
0
    }
7025
#if defined(__APPLE__) && !defined(__Userspace__)
7026
#if defined(APPLE_LEOPARD)
7027
    uio->uio_resid = control->length;
7028
#else
7029
    uio_setresid(uio, control->length);
7030
#endif
7031
#else
7032
0
    uio->uio_resid = control->length;
7033
0
#endif
7034
0
    *mp = control->data;
7035
0
    m = control->data;
7036
0
    while (m) {
7037
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
7038
0
        sctp_sblog(&so->so_rcv,
7039
0
           control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBFREE, SCTP_BUF_LEN(m));
7040
0
      }
7041
0
      sctp_sbfree(control, stcb, &so->so_rcv, m);
7042
0
      freed_so_far += (uint32_t)SCTP_BUF_LEN(m);
7043
0
      freed_so_far += MSIZE;
7044
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_SB_LOGGING_ENABLE) {
7045
0
        sctp_sblog(&so->so_rcv,
7046
0
           control->do_not_ref_stcb?NULL:stcb, SCTP_LOG_SBRESULT, 0);
7047
0
      }
7048
0
      m = SCTP_BUF_NEXT(m);
7049
0
    }
7050
0
    control->data = control->tail_mbuf = NULL;
7051
0
    control->length = 0;
7052
0
    if (out_flags & MSG_EOR) {
7053
      /* Done with this control */
7054
0
      goto done_with_control;
7055
0
    }
7056
0
  }
7057
923k
 release:
7058
923k
  if (hold_rlock == 1) {
7059
923k
    SCTP_INP_READ_UNLOCK(inp);
7060
923k
    hold_rlock = 0;
7061
923k
  }
7062
923k
#if defined(__Userspace__)
7063
923k
  if (hold_sblock == 0) {
7064
923k
    SOCKBUF_LOCK(&so->so_rcv);
7065
923k
    hold_sblock = 1;
7066
923k
  }
7067
#else
7068
  if (hold_sblock == 1) {
7069
    SOCKBUF_UNLOCK(&so->so_rcv);
7070
    hold_sblock = 0;
7071
  }
7072
#endif
7073
#if defined(__APPLE__) && !defined(__Userspace__)
7074
  sbunlock(&so->so_rcv, 1);
7075
#endif
7076
7077
#if defined(__FreeBSD__) && !defined(__Userspace__)
7078
  SOCK_IO_RECV_UNLOCK(so);
7079
  sockbuf_lock = 0;
7080
#endif
7081
7082
923k
 release_unlocked:
7083
923k
  if (hold_sblock) {
7084
923k
    SOCKBUF_UNLOCK(&so->so_rcv);
7085
923k
    hold_sblock = 0;
7086
923k
  }
7087
923k
  if ((stcb) && (in_flags & MSG_PEEK) == 0) {
7088
923k
    if ((freed_so_far >= rwnd_req) &&
7089
923k
        (control && (control->do_not_ref_stcb == 0)) &&
7090
923k
        (no_rcv_needed == 0))
7091
4
      sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
7092
923k
  }
7093
924k
 out:
7094
924k
  if (msg_flags) {
7095
924k
    *msg_flags = out_flags;
7096
924k
  }
7097
924k
  if (((out_flags & MSG_EOR) == 0) &&
7098
924k
      ((in_flags & MSG_PEEK) == 0) &&
7099
924k
      (sinfo) &&
7100
924k
      (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
7101
1.81k
       sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
7102
1.81k
    struct sctp_extrcvinfo *s_extra;
7103
1.81k
    s_extra = (struct sctp_extrcvinfo *)sinfo;
7104
1.81k
    s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
7105
1.81k
  }
7106
924k
  if (hold_rlock == 1) {
7107
0
    SCTP_INP_READ_UNLOCK(inp);
7108
0
  }
7109
924k
  if (hold_sblock) {
7110
1.56k
    SOCKBUF_UNLOCK(&so->so_rcv);
7111
1.56k
  }
7112
#if defined(__FreeBSD__) && !defined(__Userspace__)
7113
  if (sockbuf_lock) {
7114
    SOCK_IO_RECV_UNLOCK(so);
7115
  }
7116
#endif
7117
7118
924k
  if (freecnt_applied) {
7119
    /*
7120
     * The lock on the socket buffer protects us so the free
7121
     * code will stop. But since we used the socketbuf lock and
7122
     * the sender uses the tcb_lock to increment, we need to use
7123
     * the atomic add to the refcnt.
7124
     */
7125
592k
    if (stcb == NULL) {
7126
0
#ifdef INVARIANTS
7127
0
      panic("stcb for refcnt has gone NULL?");
7128
0
      goto stage_left;
7129
#else
7130
      goto stage_left;
7131
#endif
7132
0
    }
7133
    /* Save the value back for next time */
7134
592k
    stcb->freed_by_sorcv_sincelast = freed_so_far;
7135
592k
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
7136
592k
  }
7137
924k
  if (SCTP_BASE_SYSCTL(sctp_logging_level) &SCTP_RECV_RWND_LOGGING_ENABLE) {
7138
0
    if (stcb) {
7139
0
      sctp_misc_ints(SCTP_SORECV_DONE,
7140
0
                     freed_so_far,
7141
#if defined(__APPLE__) && !defined(__Userspace__)
7142
#if defined(APPLE_LEOPARD)
7143
                     ((uio) ? (slen - uio->uio_resid) : slen),
7144
#else
7145
                     ((uio) ? (slen - uio_resid(uio)) : slen),
7146
#endif
7147
#else
7148
0
                     (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
7149
0
#endif
7150
0
                     stcb->asoc.my_rwnd,
7151
0
                     SCTP_SBAVAIL(&so->so_rcv));
7152
0
    } else {
7153
0
      sctp_misc_ints(SCTP_SORECV_DONE,
7154
0
                     freed_so_far,
7155
#if defined(__APPLE__) && !defined(__Userspace__)
7156
#if defined(APPLE_LEOPARD)
7157
                     ((uio) ? (slen - uio->uio_resid) : slen),
7158
#else
7159
                     ((uio) ? (slen - uio_resid(uio)) : slen),
7160
#endif
7161
#else
7162
0
                     (uint32_t)((uio) ? (slen - uio->uio_resid) : slen),
7163
0
#endif
7164
0
                     0,
7165
0
                     SCTP_SBAVAIL(&so->so_rcv));
7166
0
    }
7167
0
  }
7168
924k
 stage_left:
7169
924k
  if (wakeup_read_socket) {
7170
0
    sctp_sorwakeup(inp, so);
7171
0
  }
7172
924k
  return (error);
7173
924k
}
7174
7175
#ifdef SCTP_MBUF_LOGGING
7176
struct mbuf *
7177
sctp_m_free(struct mbuf *m)
7178
{
7179
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7180
    sctp_log_mb(m, SCTP_MBUF_IFREE);
7181
  }
7182
  return (m_free(m));
7183
}
7184
7185
void
7186
sctp_m_freem(struct mbuf *mb)
7187
{
7188
  while (mb != NULL)
7189
    mb = sctp_m_free(mb);
7190
}
7191
7192
#endif
7193
7194
int
7195
sctp_dynamic_set_primary(struct sockaddr *sa, uint32_t vrf_id)
7196
0
{
7197
  /* Given a local address. For all associations
7198
   * that holds the address, request a peer-set-primary.
7199
   */
7200
0
  struct sctp_ifa *ifa;
7201
0
  struct sctp_laddr *wi;
7202
7203
0
  ifa = sctp_find_ifa_by_addr(sa, vrf_id, SCTP_ADDR_NOT_LOCKED);
7204
0
  if (ifa == NULL) {
7205
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EADDRNOTAVAIL);
7206
0
    return (EADDRNOTAVAIL);
7207
0
  }
7208
  /* Now that we have the ifa we must awaken the
7209
   * iterator with this message.
7210
   */
7211
0
  wi = SCTP_ZONE_GET(SCTP_BASE_INFO(ipi_zone_laddr), struct sctp_laddr);
7212
0
  if (wi == NULL) {
7213
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, ENOMEM);
7214
0
    return (ENOMEM);
7215
0
  }
7216
  /* Now incr the count and int wi structure */
7217
0
  SCTP_INCR_LADDR_COUNT();
7218
0
  memset(wi, 0, sizeof(*wi));
7219
0
  (void)SCTP_GETTIME_TIMEVAL(&wi->start_time);
7220
0
  wi->ifa = ifa;
7221
0
  wi->action = SCTP_SET_PRIM_ADDR;
7222
0
  atomic_add_int(&ifa->refcount, 1);
7223
7224
  /* Now add it to the work queue */
7225
0
  SCTP_WQ_ADDR_LOCK();
7226
  /*
7227
   * Should this really be a tailq? As it is we will process the
7228
   * newest first :-0
7229
   */
7230
0
  LIST_INSERT_HEAD(&SCTP_BASE_INFO(addr_wq), wi, sctp_nxt_addr);
7231
0
  sctp_timer_start(SCTP_TIMER_TYPE_ADDR_WQ,
7232
0
       (struct sctp_inpcb *)NULL,
7233
0
       (struct sctp_tcb *)NULL,
7234
0
       (struct sctp_nets *)NULL);
7235
0
  SCTP_WQ_ADDR_UNLOCK();
7236
0
  return (0);
7237
0
}
7238
7239
#if defined(__Userspace__)
7240
/* no sctp_soreceive for __Userspace__ now */
7241
#endif
7242
#if !defined(__Userspace__)
7243
int
7244
sctp_soreceive( struct socket *so,
7245
    struct sockaddr **psa,
7246
    struct uio *uio,
7247
    struct mbuf **mp0,
7248
    struct mbuf **controlp,
7249
    int *flagsp)
7250
{
7251
  int error, fromlen;
7252
  uint8_t sockbuf[256];
7253
  struct sockaddr *from;
7254
  struct sctp_extrcvinfo sinfo;
7255
  int filling_sinfo = 1;
7256
  int flags;
7257
  struct sctp_inpcb *inp;
7258
7259
  inp = (struct sctp_inpcb *)so->so_pcb;
7260
  /* pickup the assoc we are reading from */
7261
  if (inp == NULL) {
7262
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7263
    return (EINVAL);
7264
  }
7265
  if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVDATAIOEVNT) &&
7266
       sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVRCVINFO) &&
7267
       sctp_is_feature_off(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) ||
7268
      (controlp == NULL)) {
7269
    /* user does not want the sndrcv ctl */
7270
    filling_sinfo = 0;
7271
  }
7272
  if (psa) {
7273
    from = (struct sockaddr *)sockbuf;
7274
    fromlen = sizeof(sockbuf);
7275
#ifdef HAVE_SA_LEN
7276
    from->sa_len = 0;
7277
#endif
7278
  } else {
7279
    from = NULL;
7280
    fromlen = 0;
7281
  }
7282
7283
#if defined(__APPLE__) && !defined(__Userspace__)
7284
  SCTP_SOCKET_LOCK(so, 1);
7285
#endif
7286
  if (filling_sinfo) {
7287
    memset(&sinfo, 0, sizeof(struct sctp_extrcvinfo));
7288
  }
7289
  if (flagsp != NULL) {
7290
    flags = *flagsp;
7291
  } else {
7292
    flags = 0;
7293
  }
7294
  error = sctp_sorecvmsg(so, uio, mp0, from, fromlen, &flags,
7295
      (struct sctp_sndrcvinfo *)&sinfo, filling_sinfo);
7296
  if (flagsp != NULL) {
7297
    *flagsp = flags;
7298
  }
7299
  if (controlp != NULL) {
7300
    /* copy back the sinfo in a CMSG format */
7301
    if (filling_sinfo && ((flags & MSG_NOTIFICATION) == 0)) {
7302
      *controlp = sctp_build_ctl_nchunk(inp,
7303
                                        (struct sctp_sndrcvinfo *)&sinfo);
7304
    } else {
7305
      *controlp = NULL;
7306
    }
7307
  }
7308
  if (psa) {
7309
    /* copy back the address info */
7310
#ifdef HAVE_SA_LEN
7311
    if (from && from->sa_len) {
7312
#else
7313
    if (from) {
7314
#endif
7315
#if (defined(__FreeBSD__) || defined(_WIN32)) && !defined(__Userspace__)
7316
      *psa = sodupsockaddr(from, M_NOWAIT);
7317
#else
7318
      *psa = dup_sockaddr(from, mp0 == 0);
7319
#endif
7320
    } else {
7321
      *psa = NULL;
7322
    }
7323
  }
7324
#if defined(__APPLE__) && !defined(__Userspace__)
7325
  SCTP_SOCKET_UNLOCK(so, 1);
7326
#endif
7327
  return (error);
7328
}
7329
7330
#if defined(_WIN32) && !defined(__Userspace__)
7331
/*
7332
 * General routine to allocate a hash table with control of memory flags.
7333
 * is in 7.0 and beyond for sure :-)
7334
 */
7335
void *
7336
sctp_hashinit_flags(int elements, struct malloc_type *type,
7337
                    u_long *hashmask, int flags)
7338
{
7339
  long hashsize;
7340
  LIST_HEAD(generic, generic) *hashtbl;
7341
  int i;
7342
7343
7344
  if (elements <= 0) {
7345
#ifdef INVARIANTS
7346
    panic("hashinit: bad elements");
7347
#else
7348
    SCTP_PRINTF("hashinit: bad elements?");
7349
    elements = 1;
7350
#endif
7351
  }
7352
  for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
7353
    continue;
7354
  hashsize >>= 1;
7355
  if (flags & HASH_WAITOK)
7356
    hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_WAITOK);
7357
  else if (flags & HASH_NOWAIT)
7358
    hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl), type, M_NOWAIT);
7359
  else {
7360
#ifdef INVARIANTS
7361
    panic("flag incorrect in hashinit_flags");
7362
#else
7363
    return (NULL);
7364
#endif
7365
  }
7366
7367
  /* no memory? */
7368
  if (hashtbl == NULL)
7369
    return (NULL);
7370
7371
  for (i = 0; i < hashsize; i++)
7372
    LIST_INIT(&hashtbl[i]);
7373
  *hashmask = hashsize - 1;
7374
  return (hashtbl);
7375
}
7376
#endif
7377
#else /*  __Userspace__ ifdef above sctp_soreceive */
7378
/*
7379
 * __Userspace__ Defining sctp_hashinit_flags() and sctp_hashdestroy() for userland.
7380
 * NOTE: We don't want multiple definitions here. So sctp_hashinit_flags() above for
7381
 *__FreeBSD__ must be excluded.
7382
 *
7383
 */
7384
7385
void *
7386
sctp_hashinit_flags(int elements, struct malloc_type *type,
7387
                    u_long *hashmask, int flags)
7388
21.0k
{
7389
21.0k
  long hashsize;
7390
21.0k
  LIST_HEAD(generic, generic) *hashtbl;
7391
21.0k
  int i;
7392
7393
21.0k
  if (elements <= 0) {
7394
0
    SCTP_PRINTF("hashinit: bad elements?");
7395
0
#ifdef INVARIANTS
7396
0
    return (NULL);
7397
#else
7398
    elements = 1;
7399
#endif
7400
0
  }
7401
178k
  for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
7402
157k
    continue;
7403
21.0k
  hashsize >>= 1;
7404
  /*cannot use MALLOC here because it has to be declared or defined
7405
    using MALLOC_DECLARE or MALLOC_DEFINE first. */
7406
21.0k
  if (flags & HASH_WAITOK)
7407
0
    hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl));
7408
21.0k
  else if (flags & HASH_NOWAIT)
7409
21.0k
    hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl));
7410
0
  else {
7411
0
#ifdef INVARIANTS
7412
0
    SCTP_PRINTF("flag incorrect in hashinit_flags.\n");
7413
0
#endif
7414
0
    return (NULL);
7415
0
  }
7416
7417
  /* no memory? */
7418
21.0k
  if (hashtbl == NULL)
7419
0
    return (NULL);
7420
7421
3.06M
  for (i = 0; i < hashsize; i++)
7422
3.04M
    LIST_INIT(&hashtbl[i]);
7423
21.0k
  *hashmask = hashsize - 1;
7424
21.0k
  return (hashtbl);
7425
21.0k
}
7426
7427
void
7428
sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
7429
21.0k
{
7430
21.0k
  LIST_HEAD(generic, generic) *hashtbl, *hp;
7431
7432
21.0k
  hashtbl = vhashtbl;
7433
3.04M
  for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
7434
3.02M
    if (!LIST_EMPTY(hp)) {
7435
0
      SCTP_PRINTF("hashdestroy: hash not empty.\n");
7436
0
      return;
7437
0
    }
7438
21.0k
  FREE(hashtbl, type);
7439
21.0k
}
7440
7441
void
7442
sctp_hashfreedestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
7443
0
{
7444
0
  LIST_HEAD(generic, generic) *hashtbl/*, *hp*/;
7445
  /*
7446
  LIST_ENTRY(type) *start, *temp;
7447
   */
7448
0
  hashtbl = vhashtbl;
7449
  /* Apparently temp is not dynamically allocated, so attempts to
7450
     free it results in error.
7451
  for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
7452
    if (!LIST_EMPTY(hp)) {
7453
      start = LIST_FIRST(hp);
7454
      while (start != NULL) {
7455
        temp = start;
7456
        start = start->le_next;
7457
        SCTP_PRINTF("%s: %p \n", __func__, (void *)temp);
7458
        FREE(temp, type);
7459
      }
7460
    }
7461
   */
7462
0
  FREE(hashtbl, type);
7463
0
}
7464
7465
#endif
7466
int
7467
sctp_connectx_helper_add(struct sctp_tcb *stcb, struct sockaddr *addr,
7468
       int totaddr, int *error)
7469
0
{
7470
0
  int added = 0;
7471
0
  int i;
7472
0
  struct sctp_inpcb *inp;
7473
0
  struct sockaddr *sa;
7474
0
  size_t incr = 0;
7475
0
#ifdef INET
7476
0
  struct sockaddr_in *sin;
7477
0
#endif
7478
0
#ifdef INET6
7479
0
  struct sockaddr_in6 *sin6;
7480
0
#endif
7481
7482
0
  sa = addr;
7483
0
  inp = stcb->sctp_ep;
7484
0
  *error = 0;
7485
0
  for (i = 0; i < totaddr; i++) {
7486
0
    switch (sa->sa_family) {
7487
0
#ifdef INET
7488
0
    case AF_INET:
7489
0
      incr = sizeof(struct sockaddr_in);
7490
0
      sin = (struct sockaddr_in *)sa;
7491
0
      if (in_broadcast(sin->sin_addr) ||
7492
0
          IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
7493
0
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7494
0
        (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7495
0
                              SCTP_FROM_SCTPUTIL + SCTP_LOC_7);
7496
0
        *error = EINVAL;
7497
0
        goto out_now;
7498
0
      }
7499
0
      if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
7500
0
                               SCTP_DONOT_SETSCOPE,
7501
0
                               SCTP_ADDR_IS_CONFIRMED)) {
7502
        /* assoc gone no un-lock */
7503
0
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
7504
0
        (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7505
0
                              SCTP_FROM_SCTPUTIL + SCTP_LOC_8);
7506
0
        *error = ENOBUFS;
7507
0
        goto out_now;
7508
0
      }
7509
0
      added++;
7510
0
      break;
7511
0
#endif
7512
0
#ifdef INET6
7513
0
    case AF_INET6:
7514
0
      incr = sizeof(struct sockaddr_in6);
7515
0
      sin6 = (struct sockaddr_in6 *)sa;
7516
0
      if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
7517
0
          IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
7518
0
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7519
0
        (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7520
0
                              SCTP_FROM_SCTPUTIL + SCTP_LOC_9);
7521
0
        *error = EINVAL;
7522
0
        goto out_now;
7523
0
      }
7524
0
      if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
7525
0
                               SCTP_DONOT_SETSCOPE,
7526
0
                               SCTP_ADDR_IS_CONFIRMED)) {
7527
        /* assoc gone no un-lock */
7528
0
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
7529
0
        (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7530
0
                              SCTP_FROM_SCTPUTIL + SCTP_LOC_10);
7531
0
        *error = ENOBUFS;
7532
0
        goto out_now;
7533
0
      }
7534
0
      added++;
7535
0
      break;
7536
0
#endif
7537
0
#if defined(__Userspace__)
7538
0
    case AF_CONN:
7539
0
      incr = sizeof(struct sockaddr_conn);
7540
0
      if (sctp_add_remote_addr(stcb, sa, NULL, stcb->asoc.port,
7541
0
                               SCTP_DONOT_SETSCOPE,
7542
0
                               SCTP_ADDR_IS_CONFIRMED)) {
7543
        /* assoc gone no un-lock */
7544
0
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ENOBUFS);
7545
0
        (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
7546
0
                              SCTP_FROM_SCTPUTIL + SCTP_LOC_11);
7547
0
        *error = ENOBUFS;
7548
0
        goto out_now;
7549
0
      }
7550
0
      added++;
7551
0
      break;
7552
0
#endif
7553
0
    default:
7554
0
      break;
7555
0
    }
7556
0
    sa = (struct sockaddr *)((caddr_t)sa + incr);
7557
0
  }
7558
0
 out_now:
7559
0
  return (added);
7560
0
}
7561
7562
int
7563
sctp_connectx_helper_find(struct sctp_inpcb *inp, struct sockaddr *addr,
7564
        unsigned int totaddr,
7565
        unsigned int *num_v4, unsigned int *num_v6,
7566
        unsigned int limit)
7567
0
{
7568
0
  struct sockaddr *sa;
7569
0
  struct sctp_tcb *stcb;
7570
0
  unsigned int incr, at, i;
7571
7572
0
  at = 0;
7573
0
  sa = addr;
7574
0
  *num_v6 = *num_v4 = 0;
7575
  /* account and validate addresses */
7576
0
  if (totaddr == 0) {
7577
0
    return (EINVAL);
7578
0
  }
7579
0
  for (i = 0; i < totaddr; i++) {
7580
0
    if (at + sizeof(struct sockaddr) > limit) {
7581
0
      return (EINVAL);
7582
0
    }
7583
0
    switch (sa->sa_family) {
7584
0
#ifdef INET
7585
0
    case AF_INET:
7586
0
      incr = (unsigned int)sizeof(struct sockaddr_in);
7587
#ifdef HAVE_SA_LEN
7588
      if (sa->sa_len != incr) {
7589
        return (EINVAL);
7590
      }
7591
#endif
7592
0
      (*num_v4) += 1;
7593
0
      break;
7594
0
#endif
7595
0
#ifdef INET6
7596
0
    case AF_INET6:
7597
0
    {
7598
0
      struct sockaddr_in6 *sin6;
7599
7600
0
      incr = (unsigned int)sizeof(struct sockaddr_in6);
7601
#ifdef HAVE_SA_LEN
7602
      if (sa->sa_len != incr) {
7603
        return (EINVAL);
7604
      }
7605
#endif
7606
0
      sin6 = (struct sockaddr_in6 *)sa;
7607
0
      if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7608
        /* Must be non-mapped for connectx */
7609
0
        return (EINVAL);
7610
0
      }
7611
0
      (*num_v6) += 1;
7612
0
      break;
7613
0
    }
7614
0
#endif
7615
0
    default:
7616
0
      return (EINVAL);
7617
0
    }
7618
0
    if ((at + incr) > limit) {
7619
0
      return (EINVAL);
7620
0
    }
7621
0
    SCTP_INP_INCR_REF(inp);
7622
0
    stcb = sctp_findassociation_ep_addr(&inp, sa, NULL, NULL, NULL);
7623
0
    if (stcb != NULL) {
7624
0
      SCTP_TCB_UNLOCK(stcb);
7625
0
      return (EALREADY);
7626
0
    } else {
7627
0
      SCTP_INP_DECR_REF(inp);
7628
0
    }
7629
0
    at += incr;
7630
0
    sa = (struct sockaddr *)((caddr_t)sa + incr);
7631
0
  }
7632
0
  return (0);
7633
0
}
7634
7635
/*
7636
 * sctp_bindx(ADD) for one address.
7637
 * assumes all arguments are valid/checked by caller.
7638
 */
7639
void
7640
sctp_bindx_add_address(struct socket *so, struct sctp_inpcb *inp,
7641
                       struct sockaddr *sa, uint32_t vrf_id, int *error,
7642
                       void *p)
7643
0
{
7644
0
#if defined(INET) && defined(INET6)
7645
0
  struct sockaddr_in sin;
7646
0
#endif
7647
0
#ifdef INET6
7648
0
  struct sockaddr_in6 *sin6;
7649
0
#endif
7650
0
#ifdef INET
7651
0
  struct sockaddr_in *sinp;
7652
0
#endif
7653
0
  struct sockaddr *addr_to_use;
7654
0
  struct sctp_inpcb *lep;
7655
#ifdef SCTP_MVRF
7656
  int i;
7657
#endif
7658
0
  uint16_t port;
7659
7660
  /* see if we're bound all already! */
7661
0
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7662
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7663
0
    *error = EINVAL;
7664
0
    return;
7665
0
  }
7666
#ifdef SCTP_MVRF
7667
  /* Is the VRF one we have */
7668
  for (i = 0; i < inp->num_vrfs; i++) {
7669
    if (vrf_id == inp->m_vrf_ids[i]) {
7670
      break;
7671
    }
7672
  }
7673
  if (i == inp->num_vrfs) {
7674
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7675
    *error = EINVAL;
7676
    return;
7677
  }
7678
#endif
7679
0
  switch (sa->sa_family) {
7680
0
#ifdef INET6
7681
0
  case AF_INET6:
7682
#ifdef HAVE_SA_LEN
7683
    if (sa->sa_len != sizeof(struct sockaddr_in6)) {
7684
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7685
      *error = EINVAL;
7686
      return;
7687
    }
7688
#endif
7689
0
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7690
      /* can only bind v6 on PF_INET6 sockets */
7691
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7692
0
      *error = EINVAL;
7693
0
      return;
7694
0
    }
7695
0
    sin6 = (struct sockaddr_in6 *)sa;
7696
0
    port = sin6->sin6_port;
7697
0
#ifdef INET
7698
0
    if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7699
0
      if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7700
0
          SCTP_IPV6_V6ONLY(inp)) {
7701
        /* can't bind v4-mapped on PF_INET sockets */
7702
0
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7703
0
        *error = EINVAL;
7704
0
        return;
7705
0
      }
7706
0
      in6_sin6_2_sin(&sin, sin6);
7707
0
      addr_to_use = (struct sockaddr *)&sin;
7708
0
    } else {
7709
0
      addr_to_use = sa;
7710
0
    }
7711
#else
7712
    addr_to_use = sa;
7713
#endif
7714
0
    break;
7715
0
#endif
7716
0
#ifdef INET
7717
0
  case AF_INET:
7718
#ifdef HAVE_SA_LEN
7719
    if (sa->sa_len != sizeof(struct sockaddr_in)) {
7720
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7721
      *error = EINVAL;
7722
      return;
7723
    }
7724
#endif
7725
0
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7726
0
        SCTP_IPV6_V6ONLY(inp)) {
7727
      /* can't bind v4 on PF_INET sockets */
7728
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7729
0
      *error = EINVAL;
7730
0
      return;
7731
0
    }
7732
0
    sinp = (struct sockaddr_in *)sa;
7733
0
    port = sinp->sin_port;
7734
0
    addr_to_use = sa;
7735
0
    break;
7736
0
#endif
7737
0
  default:
7738
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7739
0
    *error = EINVAL;
7740
0
    return;
7741
0
  }
7742
0
  if (inp->sctp_flags & SCTP_PCB_FLAGS_UNBOUND) {
7743
#if !(defined(_WIN32) || defined(__Userspace__))
7744
    if (p == NULL) {
7745
      /* Can't get proc for Net/Open BSD */
7746
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7747
      *error = EINVAL;
7748
      return;
7749
    }
7750
#endif
7751
0
    *error = sctp_inpcb_bind(so, addr_to_use, NULL, p);
7752
0
    return;
7753
0
  }
7754
  /* Validate the incoming port. */
7755
0
  if ((port != 0) && (port != inp->sctp_lport)) {
7756
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7757
0
    *error = EINVAL;
7758
0
    return;
7759
0
  }
7760
0
  lep = sctp_pcb_findep(addr_to_use, 1, 0, vrf_id);
7761
0
  if (lep == NULL) {
7762
    /* add the address */
7763
0
    *error = sctp_addr_mgmt_ep_sa(inp, addr_to_use,
7764
0
                                  SCTP_ADD_IP_ADDRESS, vrf_id);
7765
0
  } else {
7766
0
    if (lep != inp) {
7767
0
      *error = EADDRINUSE;
7768
0
    }
7769
0
    SCTP_INP_DECR_REF(lep);
7770
0
  }
7771
0
}
7772
7773
/*
7774
 * sctp_bindx(DELETE) for one address.
7775
 * assumes all arguments are valid/checked by caller.
7776
 */
7777
void
7778
sctp_bindx_delete_address(struct sctp_inpcb *inp,
7779
                          struct sockaddr *sa, uint32_t vrf_id, int *error)
7780
0
{
7781
0
  struct sockaddr *addr_to_use;
7782
0
#if defined(INET) && defined(INET6)
7783
0
  struct sockaddr_in6 *sin6;
7784
0
  struct sockaddr_in sin;
7785
0
#endif
7786
#ifdef SCTP_MVRF
7787
  int i;
7788
#endif
7789
7790
  /* see if we're bound all already! */
7791
0
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7792
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7793
0
    *error = EINVAL;
7794
0
    return;
7795
0
  }
7796
#ifdef SCTP_MVRF
7797
  /* Is the VRF one we have */
7798
  for (i = 0; i < inp->num_vrfs; i++) {
7799
    if (vrf_id == inp->m_vrf_ids[i]) {
7800
      break;
7801
    }
7802
  }
7803
  if (i == inp->num_vrfs) {
7804
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7805
    *error = EINVAL;
7806
    return;
7807
  }
7808
#endif
7809
0
  switch (sa->sa_family) {
7810
0
#ifdef INET6
7811
0
  case AF_INET6:
7812
#ifdef HAVE_SA_LEN
7813
    if (sa->sa_len != sizeof(struct sockaddr_in6)) {
7814
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7815
      *error = EINVAL;
7816
      return;
7817
    }
7818
#endif
7819
0
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) {
7820
      /* can only bind v6 on PF_INET6 sockets */
7821
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7822
0
      *error = EINVAL;
7823
0
      return;
7824
0
    }
7825
0
#ifdef INET
7826
0
    sin6 = (struct sockaddr_in6 *)sa;
7827
0
    if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
7828
0
      if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7829
0
          SCTP_IPV6_V6ONLY(inp)) {
7830
        /* can't bind mapped-v4 on PF_INET sockets */
7831
0
        SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7832
0
        *error = EINVAL;
7833
0
        return;
7834
0
      }
7835
0
      in6_sin6_2_sin(&sin, sin6);
7836
0
      addr_to_use = (struct sockaddr *)&sin;
7837
0
    } else {
7838
0
      addr_to_use = sa;
7839
0
    }
7840
#else
7841
    addr_to_use = sa;
7842
#endif
7843
0
    break;
7844
0
#endif
7845
0
#ifdef INET
7846
0
  case AF_INET:
7847
#ifdef HAVE_SA_LEN
7848
    if (sa->sa_len != sizeof(struct sockaddr_in)) {
7849
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7850
      *error = EINVAL;
7851
      return;
7852
    }
7853
#endif
7854
0
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) &&
7855
0
        SCTP_IPV6_V6ONLY(inp)) {
7856
      /* can't bind v4 on PF_INET sockets */
7857
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7858
0
      *error = EINVAL;
7859
0
      return;
7860
0
    }
7861
0
    addr_to_use = sa;
7862
0
    break;
7863
0
#endif
7864
0
  default:
7865
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
7866
0
    *error = EINVAL;
7867
0
    return;
7868
0
  }
7869
  /* No lock required mgmt_ep_sa does its own locking. */
7870
0
  *error = sctp_addr_mgmt_ep_sa(inp, addr_to_use, SCTP_DEL_IP_ADDRESS,
7871
0
                                vrf_id);
7872
0
}
7873
7874
/*
7875
 * returns the valid local address count for an assoc, taking into account
7876
 * all scoping rules
7877
 */
7878
int
7879
sctp_local_addr_count(struct sctp_tcb *stcb)
7880
0
{
7881
0
  int loopback_scope;
7882
0
#if defined(INET)
7883
0
  int ipv4_local_scope, ipv4_addr_legal;
7884
0
#endif
7885
0
#if defined(INET6)
7886
0
  int local_scope, site_scope, ipv6_addr_legal;
7887
0
#endif
7888
0
#if defined(__Userspace__)
7889
0
  int conn_addr_legal;
7890
0
#endif
7891
0
  struct sctp_vrf *vrf;
7892
0
  struct sctp_ifn *sctp_ifn;
7893
0
  struct sctp_ifa *sctp_ifa;
7894
0
  int count = 0;
7895
7896
  /* Turn on all the appropriate scopes */
7897
0
  loopback_scope = stcb->asoc.scope.loopback_scope;
7898
0
#if defined(INET)
7899
0
  ipv4_local_scope = stcb->asoc.scope.ipv4_local_scope;
7900
0
  ipv4_addr_legal = stcb->asoc.scope.ipv4_addr_legal;
7901
0
#endif
7902
0
#if defined(INET6)
7903
0
  local_scope = stcb->asoc.scope.local_scope;
7904
0
  site_scope = stcb->asoc.scope.site_scope;
7905
0
  ipv6_addr_legal = stcb->asoc.scope.ipv6_addr_legal;
7906
0
#endif
7907
0
#if defined(__Userspace__)
7908
0
  conn_addr_legal = stcb->asoc.scope.conn_addr_legal;
7909
0
#endif
7910
0
  SCTP_IPI_ADDR_RLOCK();
7911
0
  vrf = sctp_find_vrf(stcb->asoc.vrf_id);
7912
0
  if (vrf == NULL) {
7913
    /* no vrf, no addresses */
7914
0
    SCTP_IPI_ADDR_RUNLOCK();
7915
0
    return (0);
7916
0
  }
7917
7918
0
  if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
7919
    /*
7920
     * bound all case: go through all ifns on the vrf
7921
     */
7922
0
    LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
7923
0
      if ((loopback_scope == 0) &&
7924
0
          SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
7925
0
        continue;
7926
0
      }
7927
0
      LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
7928
0
        if (sctp_is_addr_restricted(stcb, sctp_ifa))
7929
0
          continue;
7930
0
        switch (sctp_ifa->address.sa.sa_family) {
7931
0
#ifdef INET
7932
0
        case AF_INET:
7933
0
          if (ipv4_addr_legal) {
7934
0
            struct sockaddr_in *sin;
7935
7936
0
            sin = &sctp_ifa->address.sin;
7937
0
            if (sin->sin_addr.s_addr == 0) {
7938
              /* skip unspecified addrs */
7939
0
              continue;
7940
0
            }
7941
#if defined(__FreeBSD__) && !defined(__Userspace__)
7942
            if (prison_check_ip4(stcb->sctp_ep->ip_inp.inp.inp_cred,
7943
                                 &sin->sin_addr) != 0) {
7944
              continue;
7945
            }
7946
#endif
7947
0
            if ((ipv4_local_scope == 0) &&
7948
0
                (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
7949
0
              continue;
7950
0
            }
7951
            /* count this one */
7952
0
            count++;
7953
0
          } else {
7954
0
            continue;
7955
0
          }
7956
0
          break;
7957
0
#endif
7958
0
#ifdef INET6
7959
0
        case AF_INET6:
7960
0
          if (ipv6_addr_legal) {
7961
0
            struct sockaddr_in6 *sin6;
7962
7963
#if defined(SCTP_EMBEDDED_V6_SCOPE) && !defined(SCTP_KAME)
7964
            struct sockaddr_in6 lsa6;
7965
#endif
7966
0
            sin6 = &sctp_ifa->address.sin6;
7967
0
            if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
7968
0
              continue;
7969
0
            }
7970
#if defined(__FreeBSD__) && !defined(__Userspace__)
7971
            if (prison_check_ip6(stcb->sctp_ep->ip_inp.inp.inp_cred,
7972
                                 &sin6->sin6_addr) != 0) {
7973
              continue;
7974
            }
7975
#endif
7976
0
            if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
7977
0
              if (local_scope == 0)
7978
0
                continue;
7979
#if defined(SCTP_EMBEDDED_V6_SCOPE)
7980
              if (sin6->sin6_scope_id == 0) {
7981
#ifdef SCTP_KAME
7982
                if (sa6_recoverscope(sin6) != 0)
7983
                  /*
7984
                   * bad link
7985
                   * local
7986
                   * address
7987
                   */
7988
                  continue;
7989
#else
7990
                lsa6 = *sin6;
7991
                if (in6_recoverscope(&lsa6,
7992
                                     &lsa6.sin6_addr,
7993
                                     NULL))
7994
                  /*
7995
                   * bad link
7996
                   * local
7997
                   * address
7998
                   */
7999
                  continue;
8000
                sin6 = &lsa6;
8001
#endif /* SCTP_KAME */
8002
              }
8003
#endif /* SCTP_EMBEDDED_V6_SCOPE */
8004
0
            }
8005
0
            if ((site_scope == 0) &&
8006
0
                (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
8007
0
              continue;
8008
0
            }
8009
            /* count this one */
8010
0
            count++;
8011
0
          }
8012
0
          break;
8013
0
#endif
8014
0
#if defined(__Userspace__)
8015
0
        case AF_CONN:
8016
0
          if (conn_addr_legal) {
8017
0
            count++;
8018
0
          }
8019
0
          break;
8020
0
#endif
8021
0
        default:
8022
          /* TSNH */
8023
0
          break;
8024
0
        }
8025
0
      }
8026
0
    }
8027
0
  } else {
8028
    /*
8029
     * subset bound case
8030
     */
8031
0
    struct sctp_laddr *laddr;
8032
0
    LIST_FOREACH(laddr, &stcb->sctp_ep->sctp_addr_list,
8033
0
           sctp_nxt_addr) {
8034
0
      if (sctp_is_addr_restricted(stcb, laddr->ifa)) {
8035
0
        continue;
8036
0
      }
8037
      /* count this one */
8038
0
      count++;
8039
0
    }
8040
0
  }
8041
0
  SCTP_IPI_ADDR_RUNLOCK();
8042
0
  return (count);
8043
0
}
8044
8045
#if defined(SCTP_LOCAL_TRACE_BUF)
8046
8047
void
8048
sctp_log_trace(uint32_t subsys, const char *str SCTP_UNUSED, uint32_t a, uint32_t b, uint32_t c, uint32_t d, uint32_t e, uint32_t f)
8049
{
8050
  uint32_t saveindex, newindex;
8051
8052
#if defined(_WIN32) && !defined(__Userspace__)
8053
  if (SCTP_BASE_SYSCTL(sctp_log) == NULL) {
8054
    return;
8055
  }
8056
  do {
8057
    saveindex = SCTP_BASE_SYSCTL(sctp_log)->index;
8058
    if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
8059
      newindex = 1;
8060
    } else {
8061
      newindex = saveindex + 1;
8062
    }
8063
  } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log)->index, saveindex, newindex) == 0);
8064
  if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
8065
    saveindex = 0;
8066
  }
8067
  SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
8068
  SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].subsys = subsys;
8069
  SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[0] = a;
8070
  SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[1] = b;
8071
  SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[2] = c;
8072
  SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[3] = d;
8073
  SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[4] = e;
8074
  SCTP_BASE_SYSCTL(sctp_log)->entry[saveindex].params[5] = f;
8075
#else
8076
  do {
8077
    saveindex = SCTP_BASE_SYSCTL(sctp_log).index;
8078
    if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
8079
      newindex = 1;
8080
    } else {
8081
      newindex = saveindex + 1;
8082
    }
8083
  } while (atomic_cmpset_int(&SCTP_BASE_SYSCTL(sctp_log).index, saveindex, newindex) == 0);
8084
  if (saveindex >= SCTP_MAX_LOGGING_SIZE) {
8085
    saveindex = 0;
8086
  }
8087
  SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].timestamp = SCTP_GET_CYCLECOUNT;
8088
  SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].subsys = subsys;
8089
  SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[0] = a;
8090
  SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[1] = b;
8091
  SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[2] = c;
8092
  SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[3] = d;
8093
  SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[4] = e;
8094
  SCTP_BASE_SYSCTL(sctp_log).entry[saveindex].params[5] = f;
8095
#endif
8096
}
8097
8098
#endif
8099
#if defined(__FreeBSD__) && !defined(__Userspace__)
8100
static bool
8101
sctp_recv_udp_tunneled_packet(struct mbuf *m, int off, struct inpcb *inp,
8102
    const struct sockaddr *sa SCTP_UNUSED, void *ctx SCTP_UNUSED)
8103
{
8104
  struct ip *iph;
8105
#ifdef INET6
8106
  struct ip6_hdr *ip6;
8107
#endif
8108
  struct mbuf *sp, *last;
8109
  struct udphdr *uhdr;
8110
  uint16_t port;
8111
8112
  if ((m->m_flags & M_PKTHDR) == 0) {
8113
    /* Can't handle one that is not a pkt hdr */
8114
    goto out;
8115
  }
8116
  /* Pull the src port */
8117
  iph = mtod(m, struct ip *);
8118
  uhdr = (struct udphdr *)((caddr_t)iph + off);
8119
  port = uhdr->uh_sport;
8120
  /* Split out the mbuf chain. Leave the
8121
   * IP header in m, place the
8122
   * rest in the sp.
8123
   */
8124
  sp = m_split(m, off, M_NOWAIT);
8125
  if (sp == NULL) {
8126
    /* Gak, drop packet, we can't do a split */
8127
    goto out;
8128
  }
8129
  if (sp->m_pkthdr.len < sizeof(struct udphdr) + sizeof(struct sctphdr)) {
8130
    /* Gak, packet can't have an SCTP header in it - too small */
8131
    m_freem(sp);
8132
    goto out;
8133
  }
8134
  /* Now pull up the UDP header and SCTP header together */
8135
  sp = m_pullup(sp, sizeof(struct udphdr) + sizeof(struct sctphdr));
8136
  if (sp == NULL) {
8137
    /* Gak pullup failed */
8138
    goto out;
8139
  }
8140
  /* Trim out the UDP header */
8141
  m_adj(sp, sizeof(struct udphdr));
8142
8143
  /* Now reconstruct the mbuf chain */
8144
  for (last = m; last->m_next; last = last->m_next);
8145
  last->m_next = sp;
8146
  m->m_pkthdr.len += sp->m_pkthdr.len;
8147
  /*
8148
   * The CSUM_DATA_VALID flags indicates that the HW checked the
8149
   * UDP checksum and it was valid.
8150
   * Since CSUM_DATA_VALID == CSUM_SCTP_VALID this would imply that
8151
   * the HW also verified the SCTP checksum. Therefore, clear the bit.
8152
   */
8153
  SCTPDBG(SCTP_DEBUG_CRCOFFLOAD,
8154
          "sctp_recv_udp_tunneled_packet(): Packet of length %d received on %s with csum_flags 0x%b.\n",
8155
          m->m_pkthdr.len,
8156
          if_name(m->m_pkthdr.rcvif),
8157
          (int)m->m_pkthdr.csum_flags, CSUM_BITS);
8158
  m->m_pkthdr.csum_flags &= ~CSUM_DATA_VALID;
8159
  iph = mtod(m, struct ip *);
8160
  switch (iph->ip_v) {
8161
#ifdef INET
8162
  case IPVERSION:
8163
    iph->ip_len = htons(ntohs(iph->ip_len) - sizeof(struct udphdr));
8164
    sctp_input_with_port(m, off, port);
8165
    break;
8166
#endif
8167
#ifdef INET6
8168
  case IPV6_VERSION >> 4:
8169
    ip6 = mtod(m, struct ip6_hdr *);
8170
    ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - sizeof(struct udphdr));
8171
    sctp6_input_with_port(&m, &off, port);
8172
    break;
8173
#endif
8174
  default:
8175
    goto out;
8176
    break;
8177
  }
8178
  return (true);
8179
 out:
8180
  m_freem(m);
8181
8182
  return (true);
8183
}
8184
8185
#ifdef INET
8186
static void
8187
sctp_recv_icmp_tunneled_packet(udp_tun_icmp_param_t param)
8188
{
8189
  struct icmp *icmp = param.icmp;
8190
  struct ip *outer_ip, *inner_ip;
8191
  struct sctphdr *sh;
8192
  struct udphdr *udp;
8193
  struct sctp_inpcb *inp;
8194
  struct sctp_tcb *stcb;
8195
  struct sctp_nets *net;
8196
  struct sctp_init_chunk *ch;
8197
  struct sockaddr_in src, dst;
8198
  uint8_t type, code;
8199
8200
  inner_ip = &icmp->icmp_ip;
8201
  outer_ip = (struct ip *)((caddr_t)icmp - sizeof(struct ip));
8202
  if (ntohs(outer_ip->ip_len) <
8203
      sizeof(struct ip) + 8 + (inner_ip->ip_hl << 2) + sizeof(struct udphdr) + 8) {
8204
    return;
8205
  }
8206
  udp = (struct udphdr *)((caddr_t)inner_ip + (inner_ip->ip_hl << 2));
8207
  sh = (struct sctphdr *)(udp + 1);
8208
  memset(&src, 0, sizeof(struct sockaddr_in));
8209
  src.sin_family = AF_INET;
8210
#ifdef HAVE_SIN_LEN
8211
  src.sin_len = sizeof(struct sockaddr_in);
8212
#endif
8213
  src.sin_port = sh->src_port;
8214
  src.sin_addr = inner_ip->ip_src;
8215
  memset(&dst, 0, sizeof(struct sockaddr_in));
8216
  dst.sin_family = AF_INET;
8217
#ifdef HAVE_SIN_LEN
8218
  dst.sin_len = sizeof(struct sockaddr_in);
8219
#endif
8220
  dst.sin_port = sh->dest_port;
8221
  dst.sin_addr = inner_ip->ip_dst;
8222
  /*
8223
   * 'dst' holds the dest of the packet that failed to be sent.
8224
   * 'src' holds our local endpoint address. Thus we reverse
8225
   * the dst and the src in the lookup.
8226
   */
8227
  inp = NULL;
8228
  net = NULL;
8229
  stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
8230
                                      (struct sockaddr *)&src,
8231
                                      &inp, &net, 1,
8232
                                      SCTP_DEFAULT_VRFID);
8233
  if ((stcb != NULL) &&
8234
      (net != NULL) &&
8235
      (inp != NULL)) {
8236
    /* Check the UDP port numbers */
8237
    if ((udp->uh_dport != net->port) ||
8238
        (udp->uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
8239
      SCTP_TCB_UNLOCK(stcb);
8240
      return;
8241
    }
8242
    /* Check the verification tag */
8243
    if (ntohl(sh->v_tag) != 0) {
8244
      /*
8245
       * This must be the verification tag used
8246
       * for sending out packets. We don't
8247
       * consider packets reflecting the
8248
       * verification tag.
8249
       */
8250
      if (ntohl(sh->v_tag) != stcb->asoc.peer_vtag) {
8251
        SCTP_TCB_UNLOCK(stcb);
8252
        return;
8253
      }
8254
    } else {
8255
      if (ntohs(outer_ip->ip_len) >=
8256
          sizeof(struct ip) +
8257
          8 + (inner_ip->ip_hl << 2) + 8 + 20) {
8258
        /*
8259
         * In this case we can check if we
8260
         * got an INIT chunk and if the
8261
         * initiate tag matches.
8262
         */
8263
        ch = (struct sctp_init_chunk *)(sh + 1);
8264
        if ((ch->ch.chunk_type != SCTP_INITIATION) ||
8265
            (ntohl(ch->init.initiate_tag) != stcb->asoc.my_vtag)) {
8266
          SCTP_TCB_UNLOCK(stcb);
8267
          return;
8268
        }
8269
      } else {
8270
        SCTP_TCB_UNLOCK(stcb);
8271
        return;
8272
      }
8273
    }
8274
    type = icmp->icmp_type;
8275
    code = icmp->icmp_code;
8276
    if ((type == ICMP_UNREACH) &&
8277
        (code == ICMP_UNREACH_PORT)) {
8278
      code = ICMP_UNREACH_PROTOCOL;
8279
    }
8280
    sctp_notify(inp, stcb, net, type, code,
8281
                ntohs(inner_ip->ip_len),
8282
                (uint32_t)ntohs(icmp->icmp_nextmtu));
8283
#if defined(__Userspace__)
8284
    if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
8285
        (stcb->sctp_socket != NULL)) {
8286
      struct socket *upcall_socket;
8287
8288
      upcall_socket = stcb->sctp_socket;
8289
      SOCK_LOCK(upcall_socket);
8290
      soref(upcall_socket);
8291
      SOCK_UNLOCK(upcall_socket);
8292
      if ((upcall_socket->so_upcall != NULL) &&
8293
          (upcall_socket->so_error != 0)) {
8294
        (*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
8295
      }
8296
      ACCEPT_LOCK();
8297
      SOCK_LOCK(upcall_socket);
8298
      sorele(upcall_socket);
8299
    }
8300
#endif
8301
  } else {
8302
    if ((stcb == NULL) && (inp != NULL)) {
8303
      /* reduce ref-count */
8304
      SCTP_INP_WLOCK(inp);
8305
      SCTP_INP_DECR_REF(inp);
8306
      SCTP_INP_WUNLOCK(inp);
8307
    }
8308
    if (stcb) {
8309
      SCTP_TCB_UNLOCK(stcb);
8310
    }
8311
  }
8312
  return;
8313
}
8314
#endif
8315
8316
#ifdef INET6
8317
static void
8318
sctp_recv_icmp6_tunneled_packet(udp_tun_icmp_param_t param)
8319
{
8320
  struct ip6ctlparam *ip6cp = param.ip6cp;
8321
  struct sctp_inpcb *inp;
8322
  struct sctp_tcb *stcb;
8323
  struct sctp_nets *net;
8324
  struct sctphdr sh;
8325
  struct udphdr udp;
8326
  struct sockaddr_in6 src, dst;
8327
  uint8_t type, code;
8328
8329
  /*
8330
   * XXX: We assume that when IPV6 is non NULL, M and OFF are
8331
   * valid.
8332
   */
8333
  if (ip6cp->ip6c_m == NULL) {
8334
    return;
8335
  }
8336
  /* Check if we can safely examine the ports and the
8337
   * verification tag of the SCTP common header.
8338
   */
8339
  if (ip6cp->ip6c_m->m_pkthdr.len <
8340
      ip6cp->ip6c_off + sizeof(struct udphdr)+ offsetof(struct sctphdr, checksum)) {
8341
    return;
8342
  }
8343
  /* Copy out the UDP header. */
8344
  memset(&udp, 0, sizeof(struct udphdr));
8345
  m_copydata(ip6cp->ip6c_m,
8346
       ip6cp->ip6c_off,
8347
       sizeof(struct udphdr),
8348
       (caddr_t)&udp);
8349
  /* Copy out the port numbers and the verification tag. */
8350
  memset(&sh, 0, sizeof(struct sctphdr));
8351
  m_copydata(ip6cp->ip6c_m,
8352
       ip6cp->ip6c_off + sizeof(struct udphdr),
8353
       sizeof(uint16_t) + sizeof(uint16_t) + sizeof(uint32_t),
8354
       (caddr_t)&sh);
8355
  memset(&src, 0, sizeof(struct sockaddr_in6));
8356
  src.sin6_family = AF_INET6;
8357
#ifdef HAVE_SIN6_LEN
8358
  src.sin6_len = sizeof(struct sockaddr_in6);
8359
#endif
8360
  src.sin6_port = sh.src_port;
8361
  src.sin6_addr = ip6cp->ip6c_ip6->ip6_src;
8362
#if defined(__FreeBSD__) && !defined(__Userspace__)
8363
  if (in6_setscope(&src.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
8364
    return;
8365
  }
8366
#endif
8367
  memset(&dst, 0, sizeof(struct sockaddr_in6));
8368
  dst.sin6_family = AF_INET6;
8369
#ifdef HAVE_SIN6_LEN
8370
  dst.sin6_len = sizeof(struct sockaddr_in6);
8371
#endif
8372
  dst.sin6_port = sh.dest_port;
8373
  dst.sin6_addr = ip6cp->ip6c_ip6->ip6_dst;
8374
#if defined(__FreeBSD__) && !defined(__Userspace__)
8375
  if (in6_setscope(&dst.sin6_addr, ip6cp->ip6c_m->m_pkthdr.rcvif, NULL) != 0) {
8376
    return;
8377
  }
8378
#endif
8379
  inp = NULL;
8380
  net = NULL;
8381
  stcb = sctp_findassociation_addr_sa((struct sockaddr *)&dst,
8382
                                      (struct sockaddr *)&src,
8383
                                      &inp, &net, 1, SCTP_DEFAULT_VRFID);
8384
  if ((stcb != NULL) &&
8385
      (net != NULL) &&
8386
      (inp != NULL)) {
8387
    /* Check the UDP port numbers */
8388
    if ((udp.uh_dport != net->port) ||
8389
        (udp.uh_sport != htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)))) {
8390
      SCTP_TCB_UNLOCK(stcb);
8391
      return;
8392
    }
8393
    /* Check the verification tag */
8394
    if (ntohl(sh.v_tag) != 0) {
8395
      /*
8396
       * This must be the verification tag used for
8397
       * sending out packets. We don't consider
8398
       * packets reflecting the verification tag.
8399
       */
8400
      if (ntohl(sh.v_tag) != stcb->asoc.peer_vtag) {
8401
        SCTP_TCB_UNLOCK(stcb);
8402
        return;
8403
      }
8404
    } else {
8405
#if defined(__FreeBSD__) && !defined(__Userspace__)
8406
      if (ip6cp->ip6c_m->m_pkthdr.len >=
8407
          ip6cp->ip6c_off + sizeof(struct udphdr) +
8408
                            sizeof(struct sctphdr) +
8409
                            sizeof(struct sctp_chunkhdr) +
8410
                            offsetof(struct sctp_init, a_rwnd)) {
8411
        /*
8412
         * In this case we can check if we
8413
         * got an INIT chunk and if the
8414
         * initiate tag matches.
8415
         */
8416
        uint32_t initiate_tag;
8417
        uint8_t chunk_type;
8418
8419
        m_copydata(ip6cp->ip6c_m,
8420
             ip6cp->ip6c_off +
8421
             sizeof(struct udphdr) +
8422
             sizeof(struct sctphdr),
8423
             sizeof(uint8_t),
8424
             (caddr_t)&chunk_type);
8425
        m_copydata(ip6cp->ip6c_m,
8426
             ip6cp->ip6c_off +
8427
             sizeof(struct udphdr) +
8428
             sizeof(struct sctphdr) +
8429
             sizeof(struct sctp_chunkhdr),
8430
             sizeof(uint32_t),
8431
             (caddr_t)&initiate_tag);
8432
        if ((chunk_type != SCTP_INITIATION) ||
8433
            (ntohl(initiate_tag) != stcb->asoc.my_vtag)) {
8434
          SCTP_TCB_UNLOCK(stcb);
8435
          return;
8436
        }
8437
      } else {
8438
        SCTP_TCB_UNLOCK(stcb);
8439
        return;
8440
      }
8441
#else
8442
      SCTP_TCB_UNLOCK(stcb);
8443
      return;
8444
#endif
8445
    }
8446
    type = ip6cp->ip6c_icmp6->icmp6_type;
8447
    code = ip6cp->ip6c_icmp6->icmp6_code;
8448
    if ((type == ICMP6_DST_UNREACH) &&
8449
        (code == ICMP6_DST_UNREACH_NOPORT)) {
8450
      type = ICMP6_PARAM_PROB;
8451
      code = ICMP6_PARAMPROB_NEXTHEADER;
8452
    }
8453
    sctp6_notify(inp, stcb, net, type, code,
8454
           ntohl(ip6cp->ip6c_icmp6->icmp6_mtu));
8455
#if defined(__Userspace__)
8456
    if (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
8457
        (stcb->sctp_socket != NULL)) {
8458
      struct socket *upcall_socket;
8459
8460
      upcall_socket = stcb->sctp_socket;
8461
      SOCK_LOCK(upcall_socket);
8462
      soref(upcall_socket);
8463
      SOCK_UNLOCK(upcall_socket);
8464
      if ((upcall_socket->so_upcall != NULL) &&
8465
          (upcall_socket->so_error != 0)) {
8466
        (*upcall_socket->so_upcall)(upcall_socket, upcall_socket->so_upcallarg, M_NOWAIT);
8467
      }
8468
      ACCEPT_LOCK();
8469
      SOCK_LOCK(upcall_socket);
8470
      sorele(upcall_socket);
8471
    }
8472
#endif
8473
  } else {
8474
    if ((stcb == NULL) && (inp != NULL)) {
8475
      /* reduce inp's ref-count */
8476
      SCTP_INP_WLOCK(inp);
8477
      SCTP_INP_DECR_REF(inp);
8478
      SCTP_INP_WUNLOCK(inp);
8479
    }
8480
    if (stcb) {
8481
      SCTP_TCB_UNLOCK(stcb);
8482
    }
8483
  }
8484
}
8485
#endif
8486
8487
void
8488
sctp_over_udp_stop(void)
8489
{
8490
  /*
8491
   * This function assumes sysctl caller holds sctp_sysctl_info_lock() for writing!
8492
   */
8493
#ifdef INET
8494
  if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
8495
    soclose(SCTP_BASE_INFO(udp4_tun_socket));
8496
    SCTP_BASE_INFO(udp4_tun_socket) = NULL;
8497
  }
8498
#endif
8499
#ifdef INET6
8500
  if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
8501
    soclose(SCTP_BASE_INFO(udp6_tun_socket));
8502
    SCTP_BASE_INFO(udp6_tun_socket) = NULL;
8503
  }
8504
#endif
8505
}
8506
8507
int
8508
sctp_over_udp_start(void)
8509
{
8510
  uint16_t port;
8511
  int ret;
8512
#ifdef INET
8513
  struct sockaddr_in sin;
8514
#endif
8515
#ifdef INET6
8516
  struct sockaddr_in6 sin6;
8517
#endif
8518
  /*
8519
   * This function assumes sysctl caller holds sctp_sysctl_info_lock() for writing!
8520
   */
8521
  port = SCTP_BASE_SYSCTL(sctp_udp_tunneling_port);
8522
  if (ntohs(port) == 0) {
8523
    /* Must have a port set */
8524
    return (EINVAL);
8525
  }
8526
#ifdef INET
8527
  if (SCTP_BASE_INFO(udp4_tun_socket) != NULL) {
8528
    /* Already running -- must stop first */
8529
    return (EALREADY);
8530
  }
8531
#endif
8532
#ifdef INET6
8533
  if (SCTP_BASE_INFO(udp6_tun_socket) != NULL) {
8534
    /* Already running -- must stop first */
8535
    return (EALREADY);
8536
  }
8537
#endif
8538
#ifdef INET
8539
  if ((ret = socreate(PF_INET, &SCTP_BASE_INFO(udp4_tun_socket),
8540
                      SOCK_DGRAM, IPPROTO_UDP,
8541
                      curthread->td_ucred, curthread))) {
8542
    sctp_over_udp_stop();
8543
    return (ret);
8544
  }
8545
  /* Call the special UDP hook. */
8546
  if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp4_tun_socket),
8547
                                      sctp_recv_udp_tunneled_packet,
8548
                                      sctp_recv_icmp_tunneled_packet,
8549
                                      NULL))) {
8550
    sctp_over_udp_stop();
8551
    return (ret);
8552
  }
8553
  /* Ok, we have a socket, bind it to the port. */
8554
  memset(&sin, 0, sizeof(struct sockaddr_in));
8555
  sin.sin_len = sizeof(struct sockaddr_in);
8556
  sin.sin_family = AF_INET;
8557
  sin.sin_port = htons(port);
8558
  if ((ret = sobind(SCTP_BASE_INFO(udp4_tun_socket),
8559
                    (struct sockaddr *)&sin, curthread))) {
8560
    sctp_over_udp_stop();
8561
    return (ret);
8562
  }
8563
#endif
8564
#ifdef INET6
8565
  if ((ret = socreate(PF_INET6, &SCTP_BASE_INFO(udp6_tun_socket),
8566
                      SOCK_DGRAM, IPPROTO_UDP,
8567
                      curthread->td_ucred, curthread))) {
8568
    sctp_over_udp_stop();
8569
    return (ret);
8570
  }
8571
  /* Call the special UDP hook. */
8572
  if ((ret = udp_set_kernel_tunneling(SCTP_BASE_INFO(udp6_tun_socket),
8573
                                      sctp_recv_udp_tunneled_packet,
8574
                                      sctp_recv_icmp6_tunneled_packet,
8575
                                      NULL))) {
8576
    sctp_over_udp_stop();
8577
    return (ret);
8578
  }
8579
  /* Ok, we have a socket, bind it to the port. */
8580
  memset(&sin6, 0, sizeof(struct sockaddr_in6));
8581
  sin6.sin6_len = sizeof(struct sockaddr_in6);
8582
  sin6.sin6_family = AF_INET6;
8583
  sin6.sin6_port = htons(port);
8584
  if ((ret = sobind(SCTP_BASE_INFO(udp6_tun_socket),
8585
                    (struct sockaddr *)&sin6, curthread))) {
8586
    sctp_over_udp_stop();
8587
    return (ret);
8588
  }
8589
#endif
8590
  return (0);
8591
}
8592
#endif
8593
8594
/*
8595
 * sctp_min_mtu ()returns the minimum of all non-zero arguments.
8596
 * If all arguments are zero, zero is returned.
8597
 */
8598
uint32_t
8599
sctp_min_mtu(uint32_t mtu1, uint32_t mtu2, uint32_t mtu3)
8600
0
{
8601
0
  if (mtu1 > 0) {
8602
0
    if (mtu2 > 0) {
8603
0
      if (mtu3 > 0) {
8604
0
        return (min(mtu1, min(mtu2, mtu3)));
8605
0
      } else {
8606
0
        return (min(mtu1, mtu2));
8607
0
      }
8608
0
    } else {
8609
0
      if (mtu3 > 0) {
8610
0
        return (min(mtu1, mtu3));
8611
0
      } else {
8612
0
        return (mtu1);
8613
0
      }
8614
0
    }
8615
0
  } else {
8616
0
    if (mtu2 > 0) {
8617
0
      if (mtu3 > 0) {
8618
0
        return (min(mtu2, mtu3));
8619
0
      } else {
8620
0
        return (mtu2);
8621
0
      }
8622
0
    } else {
8623
0
      return (mtu3);
8624
0
    }
8625
0
  }
8626
0
}
8627
8628
#if defined(__FreeBSD__) && !defined(__Userspace__)
8629
void
8630
sctp_hc_set_mtu(union sctp_sockstore *addr, uint16_t fibnum, uint32_t mtu)
8631
{
8632
  struct in_conninfo inc;
8633
8634
  memset(&inc, 0, sizeof(struct in_conninfo));
8635
  inc.inc_fibnum = fibnum;
8636
  switch (addr->sa.sa_family) {
8637
#ifdef INET
8638
  case AF_INET:
8639
    inc.inc_faddr = addr->sin.sin_addr;
8640
    break;
8641
#endif
8642
#ifdef INET6
8643
  case AF_INET6:
8644
    inc.inc_flags |= INC_ISIPV6;
8645
    inc.inc6_faddr = addr->sin6.sin6_addr;
8646
    break;
8647
#endif
8648
  default:
8649
    return;
8650
  }
8651
  tcp_hc_updatemtu(&inc, (u_long)mtu);
8652
}
8653
8654
uint32_t
8655
sctp_hc_get_mtu(union sctp_sockstore *addr, uint16_t fibnum)
8656
{
8657
  struct in_conninfo inc;
8658
8659
  memset(&inc, 0, sizeof(struct in_conninfo));
8660
  inc.inc_fibnum = fibnum;
8661
  switch (addr->sa.sa_family) {
8662
#ifdef INET
8663
  case AF_INET:
8664
    inc.inc_faddr = addr->sin.sin_addr;
8665
    break;
8666
#endif
8667
#ifdef INET6
8668
  case AF_INET6:
8669
    inc.inc_flags |= INC_ISIPV6;
8670
    inc.inc6_faddr = addr->sin6.sin6_addr;
8671
    break;
8672
#endif
8673
  default:
8674
    return (0);
8675
  }
8676
  return ((uint32_t)tcp_hc_getmtu(&inc));
8677
}
8678
#endif
8679
8680
void
8681
sctp_set_state(struct sctp_tcb *stcb, int new_state)
8682
34.3k
{
8683
#if defined(KDTRACE_HOOKS)
8684
  int old_state = stcb->asoc.state;
8685
#endif
8686
8687
34.3k
  KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
8688
34.3k
          ("sctp_set_state: Can't set substate (new_state = %x)",
8689
34.3k
          new_state));
8690
34.3k
  stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
8691
34.3k
  if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
8692
34.3k
      (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
8693
34.3k
      (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8694
121
    SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
8695
121
  }
8696
#if defined(KDTRACE_HOOKS)
8697
  if (((old_state & SCTP_STATE_MASK) != new_state) &&
8698
      !(((old_state & SCTP_STATE_MASK) == SCTP_STATE_EMPTY) &&
8699
        (new_state == SCTP_STATE_INUSE))) {
8700
    SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
8701
  }
8702
#endif
8703
34.3k
}
8704
8705
void
8706
sctp_add_substate(struct sctp_tcb *stcb, int substate)
8707
21.0k
{
8708
#if defined(KDTRACE_HOOKS)
8709
  int old_state = stcb->asoc.state;
8710
#endif
8711
8712
21.0k
  KASSERT((substate & SCTP_STATE_MASK) == 0,
8713
21.0k
          ("sctp_add_substate: Can't set state (substate = %x)",
8714
21.0k
          substate));
8715
21.0k
  stcb->asoc.state |= substate;
8716
#if defined(KDTRACE_HOOKS)
8717
  if (((substate & SCTP_STATE_ABOUT_TO_BE_FREED) &&
8718
       ((old_state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0)) ||
8719
      ((substate & SCTP_STATE_SHUTDOWN_PENDING) &&
8720
       ((old_state & SCTP_STATE_SHUTDOWN_PENDING) == 0))) {
8721
    SCTP_PROBE6(state__change, NULL, stcb, NULL, stcb, NULL, old_state);
8722
  }
8723
#endif
8724
21.0k
}
8725