Coverage Report

Created: 2025-08-29 06:07

/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
144k
{
801
144k
  uint64_t temp;
802
144k
  uint32_t ticks;
803
804
144k
  if (hz == 1000) {
805
144k
    ticks = msecs;
806
144k
  } else {
807
2
    temp = (((uint64_t)msecs * hz) + 999) / 1000;
808
2
    if (temp > UINT32_MAX) {
809
0
      ticks = UINT32_MAX;
810
2
    } else {
811
2
      ticks = (uint32_t)temp;
812
2
    }
813
2
  }
814
144k
  return (ticks);
815
144k
}
816
817
uint32_t
818
sctp_ticks_to_msecs(uint32_t ticks)
819
21.2k
{
820
21.2k
  uint64_t temp;
821
21.2k
  uint32_t msecs;
822
823
21.2k
  if (hz == 1000) {
824
21.2k
    msecs = ticks;
825
21.2k
  } 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.2k
  return (msecs);
834
21.2k
}
835
836
uint32_t
837
sctp_secs_to_ticks(uint32_t secs)
838
53.1k
{
839
53.1k
  uint64_t temp;
840
53.1k
  uint32_t ticks;
841
842
53.1k
  temp = (uint64_t)secs * hz;
843
53.1k
  if (temp > UINT32_MAX) {
844
0
    ticks = UINT32_MAX;
845
53.1k
  } else {
846
53.1k
    ticks = (uint32_t)temp;
847
53.1k
  }
848
53.1k
  return (ticks);
849
53.1k
}
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
39
{
874
39
  struct sctp_inpcb *inp;
875
39
  struct sctp_nets *net;
876
877
39
  inp = stcb->sctp_ep;
878
879
39
  sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
880
39
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_12);
881
39
  sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
882
39
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_13);
883
39
  sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
884
39
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_14);
885
39
  sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
886
39
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_15);
887
112
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
888
112
    sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
889
112
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_16);
890
112
    sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
891
112
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_17);
892
112
  }
893
39
}
894
895
void
896
sctp_stop_association_timers(struct sctp_tcb *stcb, bool stop_assoc_kill_timer)
897
21.2k
{
898
21.2k
  struct sctp_inpcb *inp;
899
21.2k
  struct sctp_nets *net;
900
901
21.2k
  inp = stcb->sctp_ep;
902
21.2k
  sctp_timer_stop(SCTP_TIMER_TYPE_RECV, inp, stcb, NULL,
903
21.2k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_18);
904
21.2k
  sctp_timer_stop(SCTP_TIMER_TYPE_STRRESET, inp, stcb, NULL,
905
21.2k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_19);
906
21.2k
  if (stop_assoc_kill_timer) {
907
10.6k
    sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
908
10.6k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_20);
909
10.6k
  }
910
21.2k
  sctp_timer_stop(SCTP_TIMER_TYPE_ASCONF, inp, stcb, NULL,
911
21.2k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_21);
912
21.2k
  sctp_timer_stop(SCTP_TIMER_TYPE_AUTOCLOSE, inp, stcb, NULL,
913
21.2k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_22);
914
21.2k
  sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNGUARD, inp, stcb, NULL,
915
21.2k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_23);
916
  /* Mobility adaptation */
917
21.2k
  sctp_timer_stop(SCTP_TIMER_TYPE_PRIM_DELETED, inp, stcb, NULL,
918
21.2k
                  SCTP_FROM_SCTPUTIL + SCTP_LOC_24);
919
30.5k
  TAILQ_FOREACH(net, &stcb->asoc.nets, sctp_next) {
920
30.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
921
30.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_25);
922
30.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_INIT, inp, stcb, net,
923
30.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_26);
924
30.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWN, inp, stcb, net,
925
30.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_27);
926
30.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net,
927
30.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_28);
928
30.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_SHUTDOWNACK, inp, stcb, net,
929
30.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_29);
930
30.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_PATHMTURAISE, inp, stcb, net,
931
30.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_30);
932
30.5k
    sctp_timer_stop(SCTP_TIMER_TYPE_HEARTBEAT, inp, stcb, net,
933
30.5k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_31);
934
30.5k
  }
935
21.2k
}
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
37.6k
{
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
37.6k
  m->store_at = 0;
1023
37.6k
#if defined(__Userspace__) && defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
1024
790k
  for (int i = 0; i < (int) (sizeof(m->random_store) / sizeof(m->random_store[0])); i++) {
1025
752k
    m->random_store[i] = (uint8_t) rand();
1026
752k
  }
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
37.6k
  m->random_counter++;
1033
37.6k
}
1034
1035
uint32_t
1036
sctp_select_initial_TSN(struct sctp_pcb *inp)
1037
166k
{
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
166k
  uint32_t x, *xp;
1044
166k
  uint8_t *p;
1045
166k
  int store_at, new_store;
1046
1047
166k
  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
166k
 retry:
1055
166k
  store_at = inp->store_at;
1056
166k
  new_store = store_at + sizeof(uint32_t);
1057
166k
  if (new_store >= (SCTP_SIGNATURE_SIZE-3)) {
1058
26.9k
    new_store = 0;
1059
26.9k
  }
1060
166k
  if (!atomic_cmpset_int(&inp->store_at, store_at, new_store)) {
1061
0
    goto retry;
1062
0
  }
1063
166k
  if (new_store == 0) {
1064
    /* Refill the random store */
1065
26.9k
    sctp_fill_random_store(inp);
1066
26.9k
  }
1067
166k
  p = &inp->random_store[store_at];
1068
166k
  xp = (uint32_t *)p;
1069
166k
  x = *xp;
1070
166k
  return (x);
1071
166k
}
1072
1073
uint32_t
1074
sctp_select_a_tag(struct sctp_inpcb *inp, uint16_t lport, uint16_t rport, int check)
1075
51.6k
{
1076
51.6k
  uint32_t x;
1077
51.6k
  struct timeval now;
1078
1079
51.6k
  if (check) {
1080
30.4k
    (void)SCTP_GETTIME_TIMEVAL(&now);
1081
30.4k
  }
1082
51.6k
  for (;;) {
1083
51.6k
    x = sctp_select_initial_TSN(&inp->sctp_ep);
1084
51.6k
    if (x == 0) {
1085
      /* we never use 0 */
1086
0
      continue;
1087
0
    }
1088
51.6k
    if (!check || sctp_is_vtag_good(x, lport, rport, &now)) {
1089
51.6k
      break;
1090
51.6k
    }
1091
51.6k
  }
1092
51.6k
  return (x);
1093
51.6k
}
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.6k
{
1143
10.6k
  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.6k
  int i;
1156
#if defined(SCTP_DETAILED_STR_STATS)
1157
  int j;
1158
#endif
1159
1160
10.6k
  asoc = &stcb->asoc;
1161
  /* init all variables to a known value. */
1162
10.6k
  SCTP_SET_STATE(stcb, SCTP_STATE_INUSE);
1163
10.6k
  asoc->max_burst = inp->sctp_ep.max_burst;
1164
10.6k
  asoc->fr_max_burst = inp->sctp_ep.fr_max_burst;
1165
10.6k
  asoc->heart_beat_delay = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_HEARTBEAT]);
1166
10.6k
  asoc->cookie_life = inp->sctp_ep.def_cookie_life;
1167
10.6k
  asoc->sctp_cmt_on_off = inp->sctp_cmt_on_off;
1168
10.6k
  asoc->ecn_supported = inp->ecn_supported;
1169
10.6k
  asoc->prsctp_supported = inp->prsctp_supported;
1170
10.6k
  asoc->auth_supported = inp->auth_supported;
1171
10.6k
  asoc->asconf_supported = inp->asconf_supported;
1172
10.6k
  asoc->reconfig_supported = inp->reconfig_supported;
1173
10.6k
  asoc->nrsack_supported = inp->nrsack_supported;
1174
10.6k
  asoc->pktdrop_supported = inp->pktdrop_supported;
1175
10.6k
  asoc->idata_supported = inp->idata_supported;
1176
10.6k
  asoc->rcv_edmid = inp->rcv_edmid;
1177
10.6k
  asoc->snd_edmid = SCTP_EDMID_NONE;
1178
10.6k
  asoc->sctp_cmt_pf = (uint8_t)0;
1179
10.6k
  asoc->sctp_frag_point = inp->sctp_frag_point;
1180
10.6k
  asoc->sctp_features = inp->sctp_features;
1181
10.6k
  asoc->default_dscp = inp->sctp_ep.default_dscp;
1182
10.6k
  asoc->max_cwnd = inp->max_cwnd;
1183
10.6k
#ifdef INET6
1184
10.6k
  if (inp->sctp_ep.default_flowlabel) {
1185
0
    asoc->default_flowlabel = inp->sctp_ep.default_flowlabel;
1186
10.6k
  } else {
1187
10.6k
    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.6k
    } else {
1192
10.6k
      asoc->default_flowlabel = 0;
1193
10.6k
    }
1194
10.6k
  }
1195
10.6k
#endif
1196
10.6k
  asoc->sb_send_resv = 0;
1197
10.6k
  if (override_tag) {
1198
0
    asoc->my_vtag = override_tag;
1199
10.6k
  } else {
1200
10.6k
    asoc->my_vtag = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport,  1);
1201
10.6k
  }
1202
  /* Get the nonce tags */
1203
10.6k
  asoc->my_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1204
10.6k
  asoc->peer_vtag_nonce = sctp_select_a_tag(inp, stcb->sctp_ep->sctp_lport, stcb->rport, 0);
1205
10.6k
  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.6k
  asoc->refcnt = 0;
1219
10.6k
  asoc->assoc_up_sent = 0;
1220
10.6k
  if (override_tag) {
1221
0
    asoc->init_seq_number = initial_tsn;
1222
10.6k
  } else {
1223
10.6k
    asoc->init_seq_number = sctp_select_initial_TSN(&inp->sctp_ep);
1224
10.6k
  }
1225
10.6k
  asoc->asconf_seq_out = asoc->init_seq_number;
1226
10.6k
  asoc->str_reset_seq_out = asoc->init_seq_number;
1227
10.6k
  asoc->sending_seq = asoc->init_seq_number;
1228
10.6k
  asoc->asconf_seq_out_acked = asoc->init_seq_number - 1;
1229
  /* we are optimistic here */
1230
10.6k
  asoc->peer_supports_nat = 0;
1231
10.6k
  asoc->sent_queue_retran_cnt = 0;
1232
1233
  /* for CMT */
1234
10.6k
  asoc->last_net_cmt_send_started = NULL;
1235
1236
10.6k
  asoc->last_acked_seq = asoc->init_seq_number - 1;
1237
10.6k
  asoc->advanced_peer_ack_point = asoc->init_seq_number - 1;
1238
10.6k
  asoc->asconf_seq_in = asoc->init_seq_number - 1;
1239
1240
  /* here we are different, we hold the next one we expect */
1241
10.6k
  asoc->str_reset_seq_in = asoc->init_seq_number;
1242
1243
10.6k
  asoc->initial_init_rto_max = inp->sctp_ep.initial_init_rto_max;
1244
10.6k
  asoc->initial_rto = inp->sctp_ep.initial_rto;
1245
1246
10.6k
  asoc->default_mtu = inp->sctp_ep.default_mtu;
1247
10.6k
  asoc->max_init_times = inp->sctp_ep.max_init_times;
1248
10.6k
  asoc->max_send_times = inp->sctp_ep.max_send_times;
1249
10.6k
  asoc->def_net_failure = inp->sctp_ep.def_net_failure;
1250
10.6k
  asoc->def_net_pf_threshold = inp->sctp_ep.def_net_pf_threshold;
1251
10.6k
  asoc->free_chunk_cnt = 0;
1252
1253
10.6k
  asoc->iam_blocking = 0;
1254
10.6k
  asoc->context = inp->sctp_context;
1255
10.6k
  asoc->local_strreset_support = inp->local_strreset_support;
1256
10.6k
  asoc->def_send = inp->def_send;
1257
10.6k
  asoc->delayed_ack = sctp_ticks_to_msecs(inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_RECV]);
1258
10.6k
  asoc->sack_freq = inp->sctp_ep.sctp_sack_freq;
1259
10.6k
  asoc->pr_sctp_cnt = 0;
1260
10.6k
  asoc->total_output_queue_size = 0;
1261
1262
10.6k
  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.6k
  } else {
1273
10.6k
    asoc->scope.ipv6_addr_legal = 0;
1274
10.6k
#if defined(__Userspace__)
1275
10.6k
    if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
1276
10.6k
      asoc->scope.conn_addr_legal = 1;
1277
10.6k
      asoc->scope.ipv4_addr_legal = 0;
1278
10.6k
    } 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.6k
  }
1286
1287
10.6k
  asoc->my_rwnd = max(SCTP_SB_LIMIT_RCV(inp->sctp_socket), SCTP_MINIMAL_RWND);
1288
10.6k
  asoc->peers_rwnd = SCTP_SB_LIMIT_RCV(inp->sctp_socket);
1289
1290
10.6k
  asoc->smallest_mtu = 0;
1291
10.6k
  asoc->minrto = inp->sctp_ep.sctp_minrto;
1292
10.6k
  asoc->maxrto = inp->sctp_ep.sctp_maxrto;
1293
1294
10.6k
  asoc->stream_locked_on = 0;
1295
10.6k
  asoc->ecn_echo_cnt_onq = 0;
1296
10.6k
  asoc->stream_locked = 0;
1297
1298
10.6k
  asoc->send_sack = 1;
1299
1300
10.6k
  LIST_INIT(&asoc->sctp_restricted_addrs);
1301
1302
10.6k
  TAILQ_INIT(&asoc->nets);
1303
10.6k
  TAILQ_INIT(&asoc->pending_reply_queue);
1304
10.6k
  TAILQ_INIT(&asoc->asconf_ack_sent);
1305
  /* Setup to fill the hb random cache at first HB */
1306
10.6k
  asoc->hb_random_idx = 4;
1307
1308
10.6k
  asoc->sctp_autoclose_ticks = inp->sctp_ep.auto_close_time;
1309
1310
10.6k
  stcb->asoc.congestion_control_module = inp->sctp_ep.sctp_default_cc_module;
1311
10.6k
  stcb->asoc.cc_functions = sctp_cc_functions[inp->sctp_ep.sctp_default_cc_module];
1312
1313
10.6k
  stcb->asoc.stream_scheduling_module = inp->sctp_ep.sctp_default_ss_module;
1314
10.6k
  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.6k
  asoc->strm_realoutsize = asoc->streamoutcnt = asoc->pre_open_streams =
1321
10.6k
      o_strms;
1322
10.6k
  SCTP_MALLOC(asoc->strmout, struct sctp_stream_out *,
1323
10.6k
        asoc->streamoutcnt * sizeof(struct sctp_stream_out),
1324
10.6k
        SCTP_M_STRMO);
1325
10.6k
  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.6k
  SCTP_TCB_LOCK(stcb);
1331
2.74M
  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.73M
    TAILQ_INIT(&asoc->strmout[i].outqueue);
1341
2.73M
    asoc->ss_functions.sctp_ss_init_stream(stcb, &asoc->strmout[i], NULL);
1342
2.73M
    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.73M
    asoc->strmout[i].abandoned_sent[0] = 0;
1350
2.73M
    asoc->strmout[i].abandoned_unsent[0] = 0;
1351
2.73M
#endif
1352
2.73M
    asoc->strmout[i].next_mid_ordered = 0;
1353
2.73M
    asoc->strmout[i].next_mid_unordered = 0;
1354
2.73M
    asoc->strmout[i].sid = i;
1355
2.73M
    asoc->strmout[i].last_msg_incomplete = 0;
1356
2.73M
    asoc->strmout[i].state = SCTP_STREAM_OPENING;
1357
2.73M
  }
1358
10.6k
  asoc->ss_functions.sctp_ss_init(stcb, asoc);
1359
10.6k
  SCTP_TCB_UNLOCK(stcb);
1360
1361
  /* Now the mapping array */
1362
10.6k
  asoc->mapping_array_size = SCTP_INITIAL_MAPPING_ARRAY;
1363
10.6k
  SCTP_MALLOC(asoc->mapping_array, uint8_t *, asoc->mapping_array_size,
1364
10.6k
        SCTP_M_MAP);
1365
10.6k
  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.6k
  memset(asoc->mapping_array, 0, asoc->mapping_array_size);
1371
10.6k
  SCTP_MALLOC(asoc->nr_mapping_array, uint8_t *, asoc->mapping_array_size,
1372
10.6k
      SCTP_M_MAP);
1373
10.6k
  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.6k
  memset(asoc->nr_mapping_array, 0, asoc->mapping_array_size);
1380
1381
  /* Now the init of the other outqueues */
1382
10.6k
  TAILQ_INIT(&asoc->free_chunks);
1383
10.6k
  TAILQ_INIT(&asoc->control_send_queue);
1384
10.6k
  TAILQ_INIT(&asoc->asconf_send_queue);
1385
10.6k
  TAILQ_INIT(&asoc->send_queue);
1386
10.6k
  TAILQ_INIT(&asoc->sent_queue);
1387
10.6k
  TAILQ_INIT(&asoc->resetHead);
1388
10.6k
  asoc->max_inbound_streams = inp->sctp_ep.max_open_streams_intome;
1389
10.6k
  TAILQ_INIT(&asoc->asconf_queue);
1390
  /* authentication fields */
1391
10.6k
  asoc->authinfo.random = NULL;
1392
10.6k
  asoc->authinfo.active_keyid = 0;
1393
10.6k
  asoc->authinfo.assoc_key = NULL;
1394
10.6k
  asoc->authinfo.assoc_keyid = 0;
1395
10.6k
  asoc->authinfo.recv_key = NULL;
1396
10.6k
  asoc->authinfo.recv_keyid = 0;
1397
10.6k
  LIST_INIT(&asoc->shared_keys);
1398
10.6k
  asoc->marked_retrans = 0;
1399
10.6k
  asoc->port = inp->sctp_ep.port;
1400
10.6k
  asoc->timoinit = 0;
1401
10.6k
  asoc->timodata = 0;
1402
10.6k
  asoc->timosack = 0;
1403
10.6k
  asoc->timoshutdown = 0;
1404
10.6k
  asoc->timoheartbeat = 0;
1405
10.6k
  asoc->timocookie = 0;
1406
10.6k
  asoc->timoshutdownack = 0;
1407
10.6k
  (void)SCTP_GETTIME_TIMEVAL(&asoc->start_time);
1408
10.6k
  asoc->discontinuity_time = asoc->start_time;
1409
53.1k
  for (i = 0; i < SCTP_PR_SCTP_MAX + 1; i++) {
1410
42.5k
    asoc->abandoned_unsent[i] = 0;
1411
42.5k
    asoc->abandoned_sent[i] = 0;
1412
42.5k
  }
1413
  /* sa_ignore MEMLEAK {memory is put in the assoc mapping array and freed later when
1414
   * the association is freed.
1415
   */
1416
10.6k
  return (0);
1417
10.6k
}
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.36k
{
1457
  /* mapping array needs to grow */
1458
2.36k
  uint8_t *new_array1, *new_array2;
1459
2.36k
  uint32_t new_size;
1460
1461
2.36k
  new_size = asoc->mapping_array_size + ((needed+7)/8 + SCTP_MAPPING_ARRAY_INCR);
1462
2.36k
  SCTP_MALLOC(new_array1, uint8_t *, new_size, SCTP_M_MAP);
1463
2.36k
  SCTP_MALLOC(new_array2, uint8_t *, new_size, SCTP_M_MAP);
1464
2.36k
  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.36k
  memset(new_array1, 0, new_size);
1476
2.36k
  memset(new_array2, 0, new_size);
1477
2.36k
  memcpy(new_array1, asoc->mapping_array, asoc->mapping_array_size);
1478
2.36k
  memcpy(new_array2, asoc->nr_mapping_array, asoc->mapping_array_size);
1479
2.36k
  SCTP_FREE(asoc->mapping_array, SCTP_M_MAP);
1480
2.36k
  SCTP_FREE(asoc->nr_mapping_array, SCTP_M_MAP);
1481
2.36k
  asoc->mapping_array = new_array1;
1482
2.36k
  asoc->nr_mapping_array = new_array2;
1483
2.36k
  asoc->mapping_array_size = new_size;
1484
2.36k
  return (0);
1485
2.36k
}
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
2.14k
{
1777
#if defined(__FreeBSD__) && !defined(__Userspace__)
1778
  struct epoch_tracker et;
1779
#endif
1780
2.14k
  struct timeval tv;
1781
2.14k
  struct sctp_inpcb *inp;
1782
2.14k
  struct sctp_tcb *stcb;
1783
2.14k
  struct sctp_nets *net;
1784
2.14k
  struct sctp_timer *tmr;
1785
2.14k
  struct mbuf *op_err;
1786
#if defined(__APPLE__) && !defined(__Userspace__)
1787
  struct socket *so;
1788
#endif
1789
2.14k
#if defined(__Userspace__)
1790
2.14k
  struct socket *upcall_socket = NULL;
1791
2.14k
#endif
1792
2.14k
  int type;
1793
2.14k
  int i, secret;
1794
2.14k
  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
2.14k
  tmr = (struct sctp_timer *)t;
1802
2.14k
  inp = (struct sctp_inpcb *)tmr->ep;
1803
2.14k
  stcb = (struct sctp_tcb *)tmr->tcb;
1804
2.14k
  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
2.14k
  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
2.14k
  KASSERT(tmr->self == NULL || tmr->self == tmr,
1818
2.14k
          ("sctp_timeout_handler: tmr->self corrupted"));
1819
2.14k
  KASSERT(SCTP_IS_TIMER_TYPE_VALID(tmr->type),
1820
2.14k
          ("sctp_timeout_handler: invalid timer type %d", tmr->type));
1821
2.14k
  type = tmr->type;
1822
2.14k
  KASSERT(stcb == NULL || stcb->sctp_ep == inp,
1823
2.14k
          ("sctp_timeout_handler of type %d: inp = %p, stcb->sctp_ep %p",
1824
2.14k
           type, stcb, stcb->sctp_ep));
1825
2.14k
  tmr->stopped_from = 0xa001;
1826
2.14k
  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
2.14k
  tmr->stopped_from = 0xa002;
1833
2.14k
  SCTPDBG(SCTP_DEBUG_TIMER2, "Timer type %d goes off.\n", type);
1834
2.14k
  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
2.14k
  tmr->stopped_from = 0xa003;
1842
2.14k
  if (stcb) {
1843
2.12k
    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
2.12k
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
1850
2.12k
    released_asoc_reference = true;
1851
2.12k
    if ((type != SCTP_TIMER_TYPE_ASOCKILL) &&
1852
2.12k
        ((stcb->asoc.state == SCTP_STATE_EMPTY) ||
1853
21
         (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED))) {
1854
21
      SCTPDBG(SCTP_DEBUG_TIMER2,
1855
21
              "Timer type %d handler exiting due to CLOSED association.\n",
1856
21
              type);
1857
21
      goto out;
1858
21
    }
1859
2.12k
  } else if (inp != NULL) {
1860
21
    SCTP_INP_WLOCK(inp);
1861
21
  } else {
1862
0
    SCTP_WQ_ADDR_LOCK();
1863
0
  }
1864
1865
  /* Record in stopped_from which timeout occurred. */
1866
2.12k
  tmr->stopped_from = type;
1867
  /* mark as being serviced now */
1868
2.12k
  if (SCTP_OS_TIMER_PENDING(&tmr->timer)) {
1869
    /*
1870
     * Callout has been rescheduled.
1871
     */
1872
0
    goto out;
1873
0
  }
1874
2.12k
  if (!SCTP_OS_TIMER_ACTIVE(&tmr->timer)) {
1875
    /*
1876
     * Not active, so no action.
1877
     */
1878
0
    goto out;
1879
0
  }
1880
2.12k
  SCTP_OS_TIMER_DEACTIVATE(&tmr->timer);
1881
1882
2.12k
#if defined(__Userspace__)
1883
2.12k
  if ((stcb != NULL) &&
1884
2.12k
      ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) &&
1885
2.12k
      (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
2.12k
#endif
1892
  /* call the handler for the appropriate timer type */
1893
2.12k
  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
21
  case SCTP_TIMER_TYPE_INPKILL:
2111
21
    KASSERT(inp != NULL && stcb == NULL && net == NULL,
2112
21
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2113
21
             type, inp, stcb, net));
2114
21
    SCTP_STAT_INCR(sctps_timoinpkill);
2115
    /*
2116
     * special case, take away our increment since WE are the
2117
     * killer
2118
     */
2119
21
    sctp_timer_stop(SCTP_TIMER_TYPE_INPKILL, inp, NULL, NULL,
2120
21
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_3);
2121
#if defined(__APPLE__) && !defined(__Userspace__)
2122
    SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 1);
2123
#endif
2124
21
    SCTP_INP_DECR_REF(inp);
2125
21
    SCTP_INP_WUNLOCK(inp);
2126
21
    sctp_inpcb_free(inp, SCTP_FREE_SHOULD_USE_ABORT,
2127
21
                    SCTP_CALLED_FROM_INPKILL_TIMER);
2128
#if defined(__APPLE__) && !defined(__Userspace__)
2129
    SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 1);
2130
#endif
2131
21
    inp = NULL;
2132
21
    goto out_decr;
2133
2.10k
  case SCTP_TIMER_TYPE_ASOCKILL:
2134
2.10k
    KASSERT(inp != NULL && stcb != NULL && net == NULL,
2135
2.10k
            ("timeout of type %d: inp = %p, stcb = %p, net = %p",
2136
2.10k
             type, inp, stcb, net));
2137
2.10k
    SCTP_STAT_INCR(sctps_timoassockill);
2138
    /* Can we free it yet? */
2139
2.10k
    sctp_timer_stop(SCTP_TIMER_TYPE_ASOCKILL, inp, stcb, NULL,
2140
2.10k
                    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
2.10k
    (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
2150
2.10k
                          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
2.10k
    stcb = NULL;
2159
2.10k
    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
2.12k
  }
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
21
out:
2198
21
  if (stcb != NULL) {
2199
21
    SCTP_TCB_UNLOCK(stcb);
2200
21
  } else if (inp != NULL) {
2201
0
    SCTP_INP_WUNLOCK(inp);
2202
0
  } else {
2203
0
    SCTP_WQ_ADDR_UNLOCK();
2204
0
  }
2205
2206
2.14k
out_decr:
2207
2.14k
#if defined(__Userspace__)
2208
2.14k
  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
2.14k
#endif
2218
  /* These reference counts were incremented in sctp_timer_start(). */
2219
2.14k
  if (inp != NULL) {
2220
2.12k
    SCTP_INP_DECR_REF(inp);
2221
2.12k
  }
2222
2.14k
  if ((stcb != NULL) && !released_asoc_reference) {
2223
0
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
2224
0
  }
2225
2.14k
  if (net != NULL) {
2226
21
    sctp_free_remote_addr(net);
2227
21
  }
2228
2.14k
  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
2.14k
}
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
172k
{
2266
172k
  struct sctp_timer *tmr;
2267
172k
  uint32_t to_ticks;
2268
172k
  uint32_t rndval, jitter;
2269
2270
172k
  KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2271
172k
          ("sctp_timer_start of type %d: inp = %p, stcb->sctp_ep %p",
2272
172k
           t_type, stcb, stcb->sctp_ep));
2273
172k
  tmr = NULL;
2274
172k
  if (stcb != NULL) {
2275
161k
    SCTP_TCB_LOCK_ASSERT(stcb);
2276
161k
  } else if (inp != NULL) {
2277
10.6k
    SCTP_INP_WLOCK_ASSERT(inp);
2278
10.6k
  } else {
2279
0
    SCTP_WQ_ADDR_LOCK_ASSERT();
2280
0
  }
2281
172k
  if (stcb != NULL) {
2282
    /* Don't restart timer on association that's about to be killed. */
2283
161k
    if ((stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) &&
2284
161k
        (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
161k
    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
161k
  }
2298
172k
  switch (t_type) {
2299
8.55k
  case SCTP_TIMER_TYPE_SEND:
2300
    /* Here we use the RTO timer. */
2301
8.55k
    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
8.55k
    tmr = &net->rxt_timer;
2310
8.55k
    if (net->RTO == 0) {
2311
139
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2312
8.41k
    } else {
2313
8.41k
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2314
8.41k
    }
2315
8.55k
    break;
2316
26.1k
  case SCTP_TIMER_TYPE_INIT:
2317
    /*
2318
     * Here we use the INIT timer default usually about 1
2319
     * second.
2320
     */
2321
26.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
26.1k
    tmr = &net->rxt_timer;
2330
26.1k
    if (net->RTO == 0) {
2331
19.7k
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2332
19.7k
    } else {
2333
6.42k
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2334
6.42k
    }
2335
26.1k
    break;
2336
132
  case SCTP_TIMER_TYPE_RECV:
2337
    /*
2338
     * Here we use the Delayed-Ack timer value from the inp,
2339
     * usually about 200ms.
2340
     */
2341
132
    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
132
    tmr = &stcb->asoc.dack_timer;
2350
132
    to_ticks = sctp_msecs_to_ticks(stcb->asoc.delayed_ack);
2351
132
    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
55.4k
  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
55.4k
    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
55.4k
    if ((net->dest_state & SCTP_ADDR_NOHB) &&
2384
55.4k
        ((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
55.4k
    tmr = &net->hb_timer;
2391
55.4k
    if (net->RTO == 0) {
2392
44.4k
      to_ticks = stcb->asoc.initial_rto;
2393
44.4k
    } else {
2394
11.0k
      to_ticks = net->RTO;
2395
11.0k
    }
2396
55.4k
    if (!((net->dest_state & SCTP_ADDR_UNCONFIRMED) &&
2397
55.4k
          (net->dest_state & SCTP_ADDR_REACHABLE)) &&
2398
55.4k
        ((net->dest_state & SCTP_ADDR_PF) == 0)) {
2399
13.2k
      if (to_ticks > 1) {
2400
13.2k
        rndval = sctp_select_initial_TSN(&inp->sctp_ep);
2401
13.2k
        jitter = rndval % to_ticks;
2402
13.2k
        to_ticks >>= 1;
2403
13.2k
        if (jitter < (UINT32_MAX - to_ticks)) {
2404
13.2k
          to_ticks += jitter;
2405
13.2k
        } else {
2406
0
          to_ticks = UINT32_MAX;
2407
0
        }
2408
13.2k
      }
2409
13.2k
      if (net->heart_beat_delay < (UINT32_MAX - to_ticks)) {
2410
13.2k
        to_ticks += net->heart_beat_delay;
2411
13.2k
      } else {
2412
0
        to_ticks = UINT32_MAX;
2413
0
      }
2414
13.2k
    }
2415
    /*
2416
     * Now we must convert the to_ticks that are now in
2417
     * ms to ticks.
2418
     */
2419
55.4k
    to_ticks = sctp_msecs_to_ticks(to_ticks);
2420
55.4k
    break;
2421
16.4k
  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
16.4k
    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
16.4k
    tmr = &net->rxt_timer;
2436
16.4k
    if (net->RTO == 0) {
2437
0
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2438
16.4k
    } else {
2439
16.4k
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2440
16.4k
    }
2441
16.4k
    break;
2442
10.6k
  case SCTP_TIMER_TYPE_NEWCOOKIE:
2443
    /*
2444
     * Nothing needed but the endpoint here usually about 60
2445
     * minutes.
2446
     */
2447
10.6k
    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.6k
    tmr = &inp->sctp_ep.signature_change;
2456
10.6k
    to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_SIGNATURE];
2457
10.6k
    break;
2458
49.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
49.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
49.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
49.9k
    tmr = &net->pmtu_timer;
2478
49.9k
    to_ticks = inp->sctp_ep.sctp_timeoutticks[SCTP_TIMER_PMTU];
2479
49.9k
    break;
2480
39
  case SCTP_TIMER_TYPE_SHUTDOWNACK:
2481
    /* Here we use the RTO of the destination. */
2482
39
    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
39
    tmr = &net->rxt_timer;
2491
39
    if (net->RTO == 0) {
2492
0
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2493
39
    } else {
2494
39
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2495
39
    }
2496
39
    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.62k
  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.62k
    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.62k
    tmr = &stcb->asoc.strreset_timer;
2567
2.62k
    if (net->RTO == 0) {
2568
8
      to_ticks = sctp_msecs_to_ticks(stcb->asoc.initial_rto);
2569
2.61k
    } else {
2570
2.61k
      to_ticks = sctp_msecs_to_ticks(net->RTO);
2571
2.61k
    }
2572
2.62k
    break;
2573
21
  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
21
    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
21
    tmr = &inp->sctp_ep.signature_change;
2588
21
    to_ticks = sctp_msecs_to_ticks(SCTP_INP_KILL_TIMEOUT);
2589
21
    break;
2590
2.10k
  case SCTP_TIMER_TYPE_ASOCKILL:
2591
2.10k
    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
2.10k
    tmr = &stcb->asoc.strreset_timer;
2600
2.10k
    to_ticks = sctp_msecs_to_ticks(SCTP_ASOC_KILL_TIMEOUT);
2601
2.10k
    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
172k
  }
2634
172k
  KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2635
172k
  KASSERT(to_ticks > 0, ("to_ticks == 0 for timer type %d", t_type));
2636
172k
  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
23.0k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2642
23.0k
            "Timer type %d already running: inp=%p, stcb=%p, net=%p.\n",
2643
23.0k
             t_type, inp, stcb, net);
2644
23.0k
    return;
2645
23.0k
  }
2646
  /* At this point we can proceed. */
2647
149k
  if (t_type == SCTP_TIMER_TYPE_SEND) {
2648
2.93k
    stcb->asoc.num_send_timers_up++;
2649
2.93k
  }
2650
149k
  tmr->stopped_from = 0;
2651
149k
  tmr->type = t_type;
2652
149k
  tmr->ep = (void *)inp;
2653
149k
  tmr->tcb = (void *)stcb;
2654
149k
  if (t_type == SCTP_TIMER_TYPE_STRRESET) {
2655
2.59k
    tmr->net = NULL;
2656
146k
  } else {
2657
146k
    tmr->net = (void *)net;
2658
146k
  }
2659
149k
  tmr->self = (void *)tmr;
2660
#if defined(__FreeBSD__) && !defined(__Userspace__)
2661
  tmr->vnet = (void *)curvnet;
2662
#endif
2663
149k
  tmr->ticks = sctp_get_tick_count();
2664
149k
  if (SCTP_OS_TIMER_START(&tmr->timer, to_ticks, sctp_timeout_handler, tmr) == 0) {
2665
149k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2666
149k
            "Timer type %d started: ticks=%u, inp=%p, stcb=%p, net=%p.\n",
2667
149k
             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
149k
    if (tmr->ep != NULL) {
2673
149k
      SCTP_INP_INCR_REF(inp);
2674
149k
    }
2675
149k
    if (tmr->tcb != NULL) {
2676
138k
      atomic_add_int(&stcb->asoc.refcnt, 1);
2677
138k
    }
2678
149k
    if (tmr->net != NULL) {
2679
133k
      atomic_add_int(&net->ref_count, 1);
2680
133k
    }
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
149k
  return;
2691
172k
}
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
487k
{
2724
487k
  struct sctp_timer *tmr;
2725
2726
487k
  KASSERT(stcb == NULL || stcb->sctp_ep == inp,
2727
487k
          ("sctp_timer_stop of type %d: inp = %p, stcb->sctp_ep %p",
2728
487k
           t_type, stcb, stcb->sctp_ep));
2729
487k
  if (stcb != NULL) {
2730
477k
    SCTP_TCB_LOCK_ASSERT(stcb);
2731
477k
  } else if (inp != NULL) {
2732
10.6k
    SCTP_INP_WLOCK_ASSERT(inp);
2733
18.4E
  } else {
2734
18.4E
    SCTP_WQ_ADDR_LOCK_ASSERT();
2735
18.4E
  }
2736
487k
  tmr = NULL;
2737
487k
  switch (t_type) {
2738
30.5k
  case SCTP_TIMER_TYPE_SEND:
2739
30.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
30.5k
    tmr = &net->rxt_timer;
2748
30.5k
    break;
2749
59.9k
  case SCTP_TIMER_TYPE_INIT:
2750
59.9k
    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
59.9k
    tmr = &net->rxt_timer;
2759
59.9k
    break;
2760
27.7k
  case SCTP_TIMER_TYPE_RECV:
2761
27.7k
    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
27.7k
    tmr = &stcb->asoc.dack_timer;
2770
27.7k
    break;
2771
30.5k
  case SCTP_TIMER_TYPE_SHUTDOWN:
2772
30.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
30.5k
    tmr = &net->rxt_timer;
2781
30.5k
    break;
2782
68.6k
  case SCTP_TIMER_TYPE_HEARTBEAT:
2783
68.6k
    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
68.6k
    tmr = &net->hb_timer;
2792
68.6k
    break;
2793
41.0k
  case SCTP_TIMER_TYPE_COOKIE:
2794
41.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
41.0k
    tmr = &net->rxt_timer;
2803
41.0k
    break;
2804
10.6k
  case SCTP_TIMER_TYPE_NEWCOOKIE:
2805
10.6k
    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.6k
    tmr = &inp->sctp_ep.signature_change;
2814
10.6k
    break;
2815
68.6k
  case SCTP_TIMER_TYPE_PATHMTURAISE:
2816
68.6k
    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
68.6k
    tmr = &net->pmtu_timer;
2825
68.6k
    break;
2826
30.5k
  case SCTP_TIMER_TYPE_SHUTDOWNACK:
2827
30.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
30.5k
    tmr = &net->rxt_timer;
2836
30.5k
    break;
2837
21.5k
  case SCTP_TIMER_TYPE_ASCONF:
2838
21.5k
    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.5k
    tmr = &stcb->asoc.asconf_timer;
2847
21.5k
    break;
2848
21.2k
  case SCTP_TIMER_TYPE_SHUTDOWNGUARD:
2849
21.2k
    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.2k
    tmr = &stcb->asoc.shut_guard_timer;
2858
21.2k
    break;
2859
21.3k
  case SCTP_TIMER_TYPE_AUTOCLOSE:
2860
21.3k
    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.3k
    tmr = &stcb->asoc.autoclose_timer;
2869
21.3k
    break;
2870
21.3k
  case SCTP_TIMER_TYPE_STRRESET:
2871
21.3k
    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.3k
    tmr = &stcb->asoc.strreset_timer;
2880
21.3k
    break;
2881
21
  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
21
    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
21
    tmr = &inp->sctp_ep.signature_change;
2896
21
    break;
2897
12.7k
  case SCTP_TIMER_TYPE_ASOCKILL:
2898
12.7k
    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.7k
    tmr = &stcb->asoc.strreset_timer;
2907
12.7k
    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.2k
  case SCTP_TIMER_TYPE_PRIM_DELETED:
2920
21.2k
    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.2k
    tmr = &stcb->asoc.delete_prim_timer;
2929
21.2k
    break;
2930
0
  default:
2931
0
#ifdef INVARIANTS
2932
0
    panic("Unknown timer type %d", t_type);
2933
#else
2934
    return;
2935
#endif
2936
487k
  }
2937
487k
  KASSERT(tmr != NULL, ("tmr is NULL for timer type %d", t_type));
2938
487k
  if ((tmr->type != SCTP_TIMER_TYPE_NONE) &&
2939
487k
      (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
89.3k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2947
89.3k
            "Shared timer type %d not running: inp=%p, stcb=%p, net=%p.\n",
2948
89.3k
             t_type, inp, stcb, net);
2949
89.3k
    return;
2950
89.3k
  }
2951
398k
  if ((t_type == SCTP_TIMER_TYPE_SEND) && (stcb != NULL)) {
2952
14.7k
    stcb->asoc.num_send_timers_up--;
2953
14.7k
    if (stcb->asoc.num_send_timers_up < 0) {
2954
11.8k
      stcb->asoc.num_send_timers_up = 0;
2955
11.8k
    }
2956
14.7k
  }
2957
398k
  tmr->self = NULL;
2958
398k
  tmr->stopped_from = from;
2959
398k
  if (SCTP_OS_TIMER_STOP(&tmr->timer) == 1) {
2960
146k
    KASSERT(tmr->ep == inp,
2961
146k
            ("sctp_timer_stop of type %d: inp = %p, tmr->inp = %p",
2962
146k
             t_type, inp, tmr->ep));
2963
146k
    KASSERT(tmr->tcb == stcb,
2964
146k
            ("sctp_timer_stop of type %d: stcb = %p, tmr->stcb = %p",
2965
146k
             t_type, stcb, tmr->tcb));
2966
146k
    KASSERT(((t_type == SCTP_TIMER_TYPE_ASCONF) && (tmr->net != NULL)) ||
2967
146k
            ((t_type != SCTP_TIMER_TYPE_ASCONF) && (tmr->net == net)),
2968
146k
            ("sctp_timer_stop of type %d: net = %p, tmr->net = %p",
2969
146k
             t_type, net, tmr->net));
2970
146k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2971
146k
            "Timer type %d stopped: inp=%p, stcb=%p, net=%p.\n",
2972
146k
            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
146k
    if (tmr->ep != NULL) {
2978
146k
      tmr->ep = NULL;
2979
146k
      SCTP_INP_DECR_REF(inp);
2980
146k
    }
2981
146k
    if (tmr->tcb != NULL) {
2982
136k
      tmr->tcb = NULL;
2983
136k
      atomic_subtract_int(&stcb->asoc.refcnt, 1);
2984
136k
    }
2985
146k
    if (tmr->net != NULL) {
2986
133k
      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
133k
      tmr_net = tmr->net;
2993
133k
      tmr->net = NULL;
2994
133k
      sctp_free_remote_addr(tmr_net);
2995
133k
    }
2996
251k
  } else {
2997
251k
    SCTPDBG(SCTP_DEBUG_TIMER2,
2998
251k
            "Timer type %d not stopped: inp=%p, stcb=%p, net=%p.\n",
2999
251k
             t_type, inp, stcb, net);
3000
251k
  }
3001
398k
  return;
3002
398k
}
3003
3004
uint32_t
3005
sctp_calculate_len(struct mbuf *m)
3006
58.6k
{
3007
58.6k
  struct mbuf *at;
3008
58.6k
  uint32_t tlen;
3009
3010
58.6k
  tlen = 0;
3011
255k
  for (at = m; at != NULL; at = SCTP_BUF_NEXT(at)) {
3012
196k
    tlen += SCTP_BUF_LEN(at);
3013
196k
  }
3014
58.6k
  return (tlen);
3015
58.6k
}
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
16.5k
{
3031
16.5k
  struct timeval now;
3032
16.5k
  uint64_t rtt_us;  /* RTT in us */
3033
16.5k
  int32_t rtt;    /* RTT in ms */
3034
16.5k
  uint32_t new_rto;
3035
16.5k
  int first_measure = 0;
3036
3037
  /************************/
3038
  /* 1. calculate new RTT */
3039
  /************************/
3040
  /* get the current time */
3041
16.5k
  if (stcb->asoc.use_precise_time) {
3042
0
    (void)SCTP_GETPTIME_TIMEVAL(&now);
3043
16.5k
  } else {
3044
16.5k
    (void)SCTP_GETTIME_TIMEVAL(&now);
3045
16.5k
  }
3046
16.5k
  if ((old->tv_sec > now.tv_sec) ||
3047
16.5k
      ((old->tv_sec == now.tv_sec) && (old->tv_usec > now.tv_usec))) {
3048
    /* The starting point is in the future. */
3049
398
    return (0);
3050
398
  }
3051
16.1k
  timevalsub(&now, old);
3052
16.1k
  rtt_us = (uint64_t)1000000 * (uint64_t)now.tv_sec + (uint64_t)now.tv_usec;
3053
16.1k
  if (rtt_us > SCTP_RTO_UPPER_BOUND * 1000) {
3054
    /* The RTT is larger than a sane value. */
3055
2.31k
    return (0);
3056
2.31k
  }
3057
  /* store the current RTT in us */
3058
13.8k
  net->rtt = rtt_us;
3059
  /* compute rtt in ms */
3060
13.8k
  rtt = (int32_t)(net->rtt / 1000);
3061
13.8k
  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.8k
  if ((rtt_from_sack == SCTP_RTT_FROM_DATA) &&
3070
13.8k
      (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.8k
  if (net->RTO_measured) {
3090
5.58k
    rtt -= (net->lastsa >> SCTP_RTT_SHIFT);
3091
5.58k
    net->lastsa += rtt;
3092
5.58k
    if (rtt < 0) {
3093
0
      rtt = -rtt;
3094
0
    }
3095
5.58k
    rtt -= (net->lastsv >> SCTP_RTT_VAR_SHIFT);
3096
5.58k
    net->lastsv += rtt;
3097
5.58k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
3098
0
      rto_logging(net, SCTP_LOG_RTTVAR);
3099
0
    }
3100
8.24k
  } else {
3101
    /* First RTO measurement */
3102
8.24k
    net->RTO_measured = 1;
3103
8.24k
    first_measure = 1;
3104
8.24k
    net->lastsa = rtt << SCTP_RTT_SHIFT;
3105
8.24k
    net->lastsv = (rtt / 2) << SCTP_RTT_VAR_SHIFT;
3106
8.24k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_RTTVAR_LOGGING_ENABLE) {
3107
0
      rto_logging(net, SCTP_LOG_INITIAL_RTT);
3108
0
    }
3109
8.24k
  }
3110
13.8k
  if (net->lastsv == 0) {
3111
8.24k
    net->lastsv = SCTP_CLOCK_GRANULARITY;
3112
8.24k
  }
3113
13.8k
  new_rto = (net->lastsa >> SCTP_RTT_SHIFT) + net->lastsv;
3114
13.8k
  if ((new_rto > SCTP_SAT_NETWORK_MIN) &&
3115
13.8k
      (stcb->asoc.sat_network_lockout == 0)) {
3116
0
    stcb->asoc.sat_network = 1;
3117
13.8k
  } 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.8k
  if (new_rto < stcb->asoc.minrto) {
3123
13.8k
    new_rto = stcb->asoc.minrto;
3124
13.8k
  }
3125
13.8k
  if (new_rto > stcb->asoc.maxrto) {
3126
0
    new_rto = stcb->asoc.maxrto;
3127
0
  }
3128
13.8k
  net->RTO = new_rto;
3129
13.8k
  return (1);
3130
16.1k
}
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.28M
{
3141
2.28M
  uint32_t count;
3142
2.28M
  uint8_t *ptr;
3143
3144
2.28M
  ptr = in_ptr;
3145
2.28M
  if ((off < 0) || (len <= 0))
3146
0
    return (NULL);
3147
3148
  /* find the desired start location */
3149
13.9M
  while ((m != NULL) && (off > 0)) {
3150
13.8M
    if (off < SCTP_BUF_LEN(m))
3151
2.27M
      break;
3152
11.6M
    off -= SCTP_BUF_LEN(m);
3153
11.6M
    m = SCTP_BUF_NEXT(m);
3154
11.6M
  }
3155
2.28M
  if (m == NULL)
3156
9.31k
    return (NULL);
3157
3158
  /* is the current mbuf large enough (eg. contiguous)? */
3159
2.27M
  if ((SCTP_BUF_LEN(m) - off) >= len) {
3160
2.27M
    return (mtod(m, caddr_t) + off);
3161
2.27M
  } else {
3162
    /* else, it spans more than one mbuf, so save a temp copy... */
3163
16.8k
    while ((m != NULL) && (len > 0)) {
3164
10.7k
      count = min(SCTP_BUF_LEN(m) - off, len);
3165
10.7k
      memcpy(ptr, mtod(m, caddr_t) + off, count);
3166
10.7k
      len -= count;
3167
10.7k
      ptr += count;
3168
10.7k
      off = 0;
3169
10.7k
      m = SCTP_BUF_NEXT(m);
3170
10.7k
    }
3171
6.08k
    if ((m == NULL) && (len > 0))
3172
1.68k
      return (NULL);
3173
4.39k
    else
3174
4.39k
      return ((caddr_t)in_ptr);
3175
6.08k
  }
3176
2.27M
}
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
377k
{
3184
  /* This just provides a typed signature to Peter's Pull routine */
3185
377k
  return ((struct sctp_paramhdr *)sctp_m_getptr(m, offset, pull_limit,
3186
377k
      (uint8_t *) pull));
3187
377k
}
3188
3189
struct mbuf *
3190
sctp_add_pad_tombuf(struct mbuf *m, int padlen)
3191
37.9k
{
3192
37.9k
  struct mbuf *m_last;
3193
37.9k
  caddr_t dp;
3194
3195
37.9k
  if (padlen > 3) {
3196
0
    return (NULL);
3197
0
  }
3198
37.9k
  if (padlen <= M_TRAILINGSPACE(m)) {
3199
    /*
3200
     * The easy way. We hope the majority of the time we hit
3201
     * here :)
3202
     */
3203
32.9k
    m_last = m;
3204
32.9k
  } else {
3205
    /* Hard way we must grow the mbuf chain */
3206
5.01k
    m_last = sctp_get_mbuf_for_msg(padlen, 0, M_NOWAIT, 1, MT_DATA);
3207
5.01k
    if (m_last == NULL) {
3208
0
      return (NULL);
3209
0
    }
3210
5.01k
    SCTP_BUF_LEN(m_last) = 0;
3211
5.01k
    SCTP_BUF_NEXT(m_last) = NULL;
3212
5.01k
    SCTP_BUF_NEXT(m) = m_last;
3213
5.01k
  }
3214
37.9k
  dp = mtod(m_last, caddr_t) + SCTP_BUF_LEN(m_last);
3215
37.9k
  SCTP_BUF_LEN(m_last) += padlen;
3216
37.9k
  memset(dp, 0, padlen);
3217
37.9k
  return (m_last);
3218
37.9k
}
3219
3220
struct mbuf *
3221
sctp_pad_lastmbuf(struct mbuf *m, int padval, struct mbuf *last_mbuf)
3222
6.98k
{
3223
  /* find the last mbuf in chain and pad it */
3224
6.98k
  struct mbuf *m_at;
3225
3226
6.98k
  if (last_mbuf != NULL) {
3227
5.09k
    return (sctp_add_pad_tombuf(last_mbuf, padval));
3228
5.09k
  } else {
3229
2.01k
    for (m_at = m; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3230
2.01k
      if (SCTP_BUF_NEXT(m_at) == NULL) {
3231
1.88k
        return (sctp_add_pad_tombuf(m_at, padval));
3232
1.88k
      }
3233
2.01k
    }
3234
1.88k
  }
3235
0
  return (NULL);
3236
6.98k
}
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
7.22k
{
3243
7.22k
  struct mbuf *m_notify;
3244
7.22k
  struct sctp_assoc_change *sac;
3245
7.22k
  struct sctp_queued_to_read *control;
3246
7.22k
  struct sctp_inpcb *inp;
3247
7.22k
  unsigned int notif_len;
3248
7.22k
  unsigned int i;
3249
7.22k
  uint16_t abort_len;
3250
#if defined(__APPLE__) && !defined(__Userspace__)
3251
  struct socket *so;
3252
#endif
3253
3254
7.22k
  KASSERT(abort == NULL || from_peer,
3255
7.22k
          ("sctp_notify_assoc_change: ABORT chunk provided for local termination"));
3256
7.22k
  KASSERT(!from_peer || !timedout,
3257
7.22k
          ("sctp_notify_assoc_change: timeouts can only be local"));
3258
7.22k
  KASSERT(stcb != NULL, ("stcb == NULL"));
3259
7.22k
  SCTP_TCB_LOCK_ASSERT(stcb);
3260
7.22k
  inp = stcb->sctp_ep;
3261
7.22k
  SCTP_INP_READ_LOCK_ASSERT(inp);
3262
3263
7.22k
  if (sctp_stcb_is_feature_on(inp, stcb, SCTP_PCB_FLAGS_RECVASSOCEVNT)) {
3264
7.22k
    notif_len = (unsigned int)sizeof(struct sctp_assoc_change);
3265
7.22k
    if (abort != NULL) {
3266
59
      abort_len = ntohs(abort->ch.chunk_length);
3267
      /*
3268
       * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
3269
       * contiguous.
3270
       */
3271
59
      if (abort_len > SCTP_CHUNK_BUFFER_SIZE) {
3272
1
        abort_len = SCTP_CHUNK_BUFFER_SIZE;
3273
1
      }
3274
7.16k
    } else {
3275
7.16k
      abort_len = 0;
3276
7.16k
    }
3277
7.22k
    if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3278
5.52k
      notif_len += SCTP_ASSOC_SUPPORTS_MAX;
3279
5.52k
    } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3280
1.69k
      notif_len += abort_len;
3281
1.69k
    }
3282
7.22k
    m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
3283
7.22k
    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
7.22k
    SCTP_BUF_NEXT(m_notify) = NULL;
3292
7.22k
    sac = mtod(m_notify, struct sctp_assoc_change *);
3293
7.22k
    memset(sac, 0, notif_len);
3294
7.22k
    sac->sac_type = SCTP_ASSOC_CHANGE;
3295
7.22k
    sac->sac_flags = 0;
3296
7.22k
    sac->sac_length = sizeof(struct sctp_assoc_change);
3297
7.22k
    sac->sac_state = state;
3298
7.22k
    sac->sac_error = error;
3299
7.22k
    if (state == SCTP_CANT_STR_ASSOC) {
3300
666
      sac->sac_outbound_streams = 0;
3301
666
      sac->sac_inbound_streams = 0;
3302
6.56k
    } else {
3303
6.56k
      sac->sac_outbound_streams = stcb->asoc.streamoutcnt;
3304
6.56k
      sac->sac_inbound_streams = stcb->asoc.streamincnt;
3305
6.56k
    }
3306
7.22k
    sac->sac_assoc_id = sctp_get_associd(stcb);
3307
7.22k
    if (notif_len > sizeof(struct sctp_assoc_change)) {
3308
5.58k
      if ((state == SCTP_COMM_UP) || (state == SCTP_RESTART)) {
3309
5.52k
        i = 0;
3310
5.52k
        if (stcb->asoc.prsctp_supported == 1) {
3311
5.52k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_PR;
3312
5.52k
        }
3313
5.52k
        if (stcb->asoc.auth_supported == 1) {
3314
5.52k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_AUTH;
3315
5.52k
        }
3316
5.52k
        if (stcb->asoc.asconf_supported == 1) {
3317
5.51k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_ASCONF;
3318
5.51k
        }
3319
5.52k
        if (stcb->asoc.idata_supported == 1) {
3320
2.86k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_INTERLEAVING;
3321
2.86k
        }
3322
5.52k
        sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_MULTIBUF;
3323
5.52k
        if (stcb->asoc.reconfig_supported == 1) {
3324
5.52k
          sac->sac_info[i++] = SCTP_ASSOC_SUPPORTS_RE_CONFIG;
3325
5.52k
        }
3326
5.52k
        sac->sac_length += i;
3327
5.52k
      } else if ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC)) {
3328
59
        memcpy(sac->sac_info, abort, abort_len);
3329
59
        sac->sac_length += abort_len;
3330
59
      }
3331
5.58k
    }
3332
7.22k
    SCTP_BUF_LEN(m_notify) = sac->sac_length;
3333
7.22k
    control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3334
7.22k
                                     0, 0, stcb->asoc.context, 0, 0, 0,
3335
7.22k
                                     m_notify);
3336
7.22k
    if (control != NULL) {
3337
7.22k
      control->length = SCTP_BUF_LEN(m_notify);
3338
7.22k
      control->spec_flags = M_NOTIFICATION;
3339
      /* not that we need this */
3340
7.22k
      control->tail_mbuf = m_notify;
3341
7.22k
      sctp_add_to_readq(inp, stcb, control,
3342
7.22k
                        &stcb->sctp_socket->so_rcv, 1,
3343
7.22k
                        SCTP_READ_LOCK_HELD, so_locked);
3344
7.22k
    } else {
3345
0
      sctp_m_freem(m_notify);
3346
0
    }
3347
7.22k
  }
3348
  /*
3349
   * For 1-to-1 style sockets, we send up and error when an ABORT
3350
   * comes in.
3351
   */
3352
7.22k
set_error:
3353
7.22k
  if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3354
7.22k
       (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
3355
7.22k
      ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
3356
1.69k
    SOCK_LOCK(stcb->sctp_socket);
3357
1.69k
    if (from_peer) {
3358
59
      if (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) {
3359
27
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNREFUSED);
3360
27
        stcb->sctp_socket->so_error = ECONNREFUSED;
3361
32
      } else {
3362
32
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNRESET);
3363
32
        stcb->sctp_socket->so_error = ECONNRESET;
3364
32
      }
3365
1.64k
    } else {
3366
1.64k
      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.64k
      } else {
3370
1.64k
        SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTPUTIL, ECONNABORTED);
3371
1.64k
        stcb->sctp_socket->so_error = ECONNABORTED;
3372
1.64k
      }
3373
1.64k
    }
3374
1.69k
    SOCK_UNLOCK(stcb->sctp_socket);
3375
1.69k
  }
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
7.22k
  if (((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3392
7.22k
       (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) &&
3393
7.22k
      ((state == SCTP_COMM_LOST) || (state == SCTP_CANT_STR_ASSOC))) {
3394
1.69k
    socantrcvmore(stcb->sctp_socket);
3395
1.69k
  }
3396
7.22k
  sorwakeup(stcb->sctp_socket);
3397
7.22k
  sowwakeup(stcb->sctp_socket);
3398
#if defined(__APPLE__) && !defined(__Userspace__)
3399
  if (!so_locked) {
3400
    SCTP_SOCKET_UNLOCK(so, 1);
3401
  }
3402
#endif
3403
7.22k
}
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
89.1k
{
3409
89.1k
  struct mbuf *m_notify;
3410
89.1k
  struct sctp_paddr_change *spc;
3411
89.1k
  struct sctp_queued_to_read *control;
3412
3413
89.1k
  KASSERT(stcb != NULL, ("stcb == NULL"));
3414
89.1k
  SCTP_TCB_LOCK_ASSERT(stcb);
3415
89.1k
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3416
3417
89.1k
  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
89.1k
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_paddr_change), 0, M_NOWAIT, 1, MT_DATA);
3423
89.1k
  if (m_notify == NULL)
3424
0
    return;
3425
89.1k
  SCTP_BUF_LEN(m_notify) = 0;
3426
89.1k
  spc = mtod(m_notify, struct sctp_paddr_change *);
3427
89.1k
  memset(spc, 0, sizeof(struct sctp_paddr_change));
3428
89.1k
  spc->spc_type = SCTP_PEER_ADDR_CHANGE;
3429
89.1k
  spc->spc_flags = 0;
3430
89.1k
  spc->spc_length = sizeof(struct sctp_paddr_change);
3431
89.1k
  switch (sa->sa_family) {
3432
0
#ifdef INET
3433
81.7k
  case AF_INET:
3434
81.7k
#ifdef INET6
3435
81.7k
    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
81.7k
    } else {
3439
81.7k
      memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3440
81.7k
    }
3441
#else
3442
    memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_in));
3443
#endif
3444
81.7k
    break;
3445
0
#endif
3446
0
#ifdef INET6
3447
6.83k
  case AF_INET6:
3448
6.83k
  {
3449
#ifdef SCTP_EMBEDDED_V6_SCOPE
3450
    struct sockaddr_in6 *sin6;
3451
#endif /* SCTP_EMBEDDED_V6_SCOPE */
3452
6.83k
    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
6.83k
    break;
3472
0
  }
3473
0
#endif
3474
0
#if defined(__Userspace__)
3475
626
  case AF_CONN:
3476
626
    memcpy(&spc->spc_aaddr, sa, sizeof(struct sockaddr_conn));
3477
626
    break;
3478
0
#endif
3479
0
  default:
3480
    /* TSNH */
3481
0
    break;
3482
89.1k
  }
3483
89.1k
  spc->spc_state = state;
3484
89.1k
  spc->spc_error = error;
3485
89.1k
  spc->spc_assoc_id = sctp_get_associd(stcb);
3486
3487
89.1k
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_paddr_change);
3488
89.1k
  SCTP_BUF_NEXT(m_notify) = NULL;
3489
3490
  /* append to socket */
3491
89.1k
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3492
89.1k
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3493
89.1k
                                   m_notify);
3494
89.1k
  if (control == NULL) {
3495
    /* no memory */
3496
0
    sctp_m_freem(m_notify);
3497
0
    return;
3498
0
  }
3499
89.1k
  control->length = SCTP_BUF_LEN(m_notify);
3500
89.1k
  control->spec_flags = M_NOTIFICATION;
3501
  /* not that we need this */
3502
89.1k
  control->tail_mbuf = m_notify;
3503
89.1k
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3504
89.1k
                    &stcb->sctp_socket->so_rcv, 1,
3505
89.1k
                    SCTP_READ_LOCK_HELD, so_locked);
3506
89.1k
}
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
542
{
3512
542
  struct mbuf *m_notify;
3513
542
  struct sctp_send_failed *ssf;
3514
542
  struct sctp_send_failed_event *ssfe;
3515
542
  struct sctp_queued_to_read *control;
3516
542
  struct sctp_chunkhdr *chkhdr;
3517
542
  int notifhdr_len, chk_len, chkhdr_len, padding_len, payload_len;
3518
3519
542
  KASSERT(stcb != NULL, ("stcb == NULL"));
3520
542
  SCTP_TCB_LOCK_ASSERT(stcb);
3521
542
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3522
3523
542
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3524
542
      sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3525
    /* event not enabled */
3526
0
    return;
3527
0
  }
3528
3529
542
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3530
542
    notifhdr_len = sizeof(struct sctp_send_failed_event);
3531
542
  } else {
3532
0
    notifhdr_len = sizeof(struct sctp_send_failed);
3533
0
  }
3534
542
  m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3535
542
  if (m_notify == NULL)
3536
    /* no space left */
3537
0
    return;
3538
542
  SCTP_BUF_LEN(m_notify) = notifhdr_len;
3539
542
  if (stcb->asoc.idata_supported) {
3540
426
    chkhdr_len = sizeof(struct sctp_idata_chunk);
3541
426
  } else {
3542
116
    chkhdr_len = sizeof(struct sctp_data_chunk);
3543
116
  }
3544
  /* Use some defaults in case we can't access the chunk header */
3545
542
  if (chk->send_size >= chkhdr_len) {
3546
542
    payload_len = chk->send_size - chkhdr_len;
3547
542
  } else {
3548
0
    payload_len = 0;
3549
0
  }
3550
542
  padding_len = 0;
3551
542
  if (chk->data != NULL) {
3552
542
    chkhdr = mtod(chk->data, struct sctp_chunkhdr *);
3553
542
    if (chkhdr != NULL) {
3554
542
      chk_len = ntohs(chkhdr->chunk_length);
3555
542
      if ((chk_len >= chkhdr_len) &&
3556
542
          (chk->send_size >= chk_len) &&
3557
542
          (chk->send_size - chk_len < 4)) {
3558
542
        padding_len = chk->send_size - chk_len;
3559
542
        payload_len = chk->send_size - chkhdr_len - padding_len;
3560
542
      }
3561
542
    }
3562
542
  }
3563
542
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3564
542
    ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3565
542
    memset(ssfe, 0, notifhdr_len);
3566
542
    ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3567
542
    if (sent) {
3568
513
      ssfe->ssfe_flags = SCTP_DATA_SENT;
3569
513
    } else {
3570
29
      ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3571
29
    }
3572
542
    ssfe->ssfe_length = (uint32_t)(notifhdr_len + payload_len);
3573
542
    ssfe->ssfe_error = error;
3574
    /* not exactly what the user sent in, but should be close :) */
3575
542
    ssfe->ssfe_info.snd_sid = chk->rec.data.sid;
3576
542
    ssfe->ssfe_info.snd_flags = chk->rec.data.rcv_flags;
3577
542
    ssfe->ssfe_info.snd_ppid = chk->rec.data.ppid;
3578
542
    ssfe->ssfe_info.snd_context = chk->rec.data.context;
3579
542
    ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3580
542
    ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3581
542
  } 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
542
  if (chk->data != NULL) {
3602
    /* Trim off the sctp chunk header (it should be there) */
3603
542
    if (chk->send_size == chkhdr_len + payload_len + padding_len) {
3604
542
      m_adj(chk->data, chkhdr_len);
3605
542
      m_adj(chk->data, -padding_len);
3606
542
      sctp_mbuf_crush(chk->data);
3607
542
      chk->send_size -= (chkhdr_len + padding_len);
3608
542
    }
3609
542
  }
3610
542
  SCTP_BUF_NEXT(m_notify) = chk->data;
3611
  /* Steal off the mbuf */
3612
542
  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
542
  if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3619
8
    sctp_m_freem(m_notify);
3620
8
    return;
3621
8
  }
3622
  /* append to socket */
3623
534
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3624
534
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3625
534
                                   m_notify);
3626
534
  if (control == NULL) {
3627
    /* no memory */
3628
0
    sctp_m_freem(m_notify);
3629
0
    return;
3630
0
  }
3631
534
  control->length = SCTP_BUF_LEN(m_notify);
3632
534
  control->spec_flags = M_NOTIFICATION;
3633
  /* not that we need this */
3634
534
  control->tail_mbuf = m_notify;
3635
534
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3636
534
                    &stcb->sctp_socket->so_rcv, 1,
3637
534
                    SCTP_READ_LOCK_HELD, so_locked);
3638
534
}
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
534
{
3644
534
  struct mbuf *m_notify;
3645
534
  struct sctp_send_failed *ssf;
3646
534
  struct sctp_send_failed_event *ssfe;
3647
534
  struct sctp_queued_to_read *control;
3648
534
  int notifhdr_len;
3649
3650
534
  KASSERT(stcb != NULL, ("stcb == NULL"));
3651
534
  SCTP_TCB_LOCK_ASSERT(stcb);
3652
534
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3653
3654
534
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVSENDFAILEVNT) &&
3655
534
      sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3656
    /* event not enabled */
3657
0
    return;
3658
0
  }
3659
3660
534
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3661
534
    notifhdr_len = sizeof(struct sctp_send_failed_event);
3662
534
  } else {
3663
0
    notifhdr_len = sizeof(struct sctp_send_failed);
3664
0
  }
3665
534
  m_notify = sctp_get_mbuf_for_msg(notifhdr_len, 0, M_NOWAIT, 1, MT_DATA);
3666
534
  if (m_notify == NULL) {
3667
    /* no space left */
3668
0
    return;
3669
0
  }
3670
534
  SCTP_BUF_LEN(m_notify) = notifhdr_len;
3671
534
  if (sctp_stcb_is_feature_on(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVNSENDFAILEVNT)) {
3672
534
    ssfe = mtod(m_notify, struct sctp_send_failed_event *);
3673
534
    memset(ssfe, 0, notifhdr_len);
3674
534
    ssfe->ssfe_type = SCTP_SEND_FAILED_EVENT;
3675
534
    ssfe->ssfe_flags = SCTP_DATA_UNSENT;
3676
534
    ssfe->ssfe_length = (uint32_t)(notifhdr_len + sp->length);
3677
534
    ssfe->ssfe_error = error;
3678
    /* not exactly what the user sent in, but should be close :) */
3679
534
    ssfe->ssfe_info.snd_sid = sp->sid;
3680
534
    if (sp->some_taken) {
3681
0
      ssfe->ssfe_info.snd_flags = SCTP_DATA_LAST_FRAG;
3682
534
    } else {
3683
534
      ssfe->ssfe_info.snd_flags = SCTP_DATA_NOT_FRAG;
3684
534
    }
3685
534
    ssfe->ssfe_info.snd_ppid = sp->ppid;
3686
534
    ssfe->ssfe_info.snd_context = sp->context;
3687
534
    ssfe->ssfe_info.snd_assoc_id = sctp_get_associd(stcb);
3688
534
    ssfe->ssfe_assoc_id = sctp_get_associd(stcb);
3689
534
  } 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
534
  SCTP_BUF_NEXT(m_notify) = sp->data;
3710
3711
  /* Steal off the mbuf */
3712
534
  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
534
  if (sctp_sbspace_failedmsgs(&stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
3719
8
    sctp_m_freem(m_notify);
3720
8
    return;
3721
8
  }
3722
  /* append to socket */
3723
526
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3724
526
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3725
526
                                   m_notify);
3726
526
  if (control == NULL) {
3727
    /* no memory */
3728
0
    sctp_m_freem(m_notify);
3729
0
    return;
3730
0
  }
3731
526
  control->length = SCTP_BUF_LEN(m_notify);
3732
526
  control->spec_flags = M_NOTIFICATION;
3733
  /* not that we need this */
3734
526
  control->tail_mbuf = m_notify;
3735
526
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3736
526
                    &stcb->sctp_socket->so_rcv, 1,
3737
526
                    SCTP_READ_LOCK_HELD, so_locked);
3738
526
}
3739
3740
static void
3741
sctp_notify_adaptation_layer(struct sctp_tcb *stcb, int so_locked)
3742
5.52k
{
3743
5.52k
  struct mbuf *m_notify;
3744
5.52k
  struct sctp_adaptation_event *sai;
3745
5.52k
  struct sctp_queued_to_read *control;
3746
3747
5.52k
  KASSERT(stcb != NULL, ("stcb == NULL"));
3748
5.52k
  SCTP_TCB_LOCK_ASSERT(stcb);
3749
5.52k
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3750
3751
5.52k
  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.52k
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_adaption_event), 0, M_NOWAIT, 1, MT_DATA);
3757
5.52k
  if (m_notify == NULL)
3758
    /* no space left */
3759
0
    return;
3760
5.52k
  SCTP_BUF_LEN(m_notify) = 0;
3761
5.52k
  sai = mtod(m_notify, struct sctp_adaptation_event *);
3762
5.52k
  memset(sai, 0, sizeof(struct sctp_adaptation_event));
3763
5.52k
  sai->sai_type = SCTP_ADAPTATION_INDICATION;
3764
5.52k
  sai->sai_flags = 0;
3765
5.52k
  sai->sai_length = sizeof(struct sctp_adaptation_event);
3766
5.52k
  sai->sai_adaptation_ind = stcb->asoc.peers_adaptation;
3767
5.52k
  sai->sai_assoc_id = sctp_get_associd(stcb);
3768
3769
5.52k
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_adaptation_event);
3770
5.52k
  SCTP_BUF_NEXT(m_notify) = NULL;
3771
3772
  /* append to socket */
3773
5.52k
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3774
5.52k
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3775
5.52k
                                   m_notify);
3776
5.52k
  if (control == NULL) {
3777
    /* no memory */
3778
0
    sctp_m_freem(m_notify);
3779
0
    return;
3780
0
  }
3781
5.52k
  control->length = SCTP_BUF_LEN(m_notify);
3782
5.52k
  control->spec_flags = M_NOTIFICATION;
3783
  /* not that we need this */
3784
5.52k
  control->tail_mbuf = m_notify;
3785
5.52k
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3786
5.52k
                    &stcb->sctp_socket->so_rcv, 1,
3787
5.52k
                    SCTP_READ_LOCK_HELD, so_locked);
3788
5.52k
}
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
1.26k
{
3795
1.26k
  struct mbuf *m_notify;
3796
1.26k
  struct sctp_pdapi_event *pdapi;
3797
1.26k
  struct sctp_queued_to_read *control;
3798
1.26k
  struct sockbuf *sb;
3799
3800
1.26k
  KASSERT(aborted_control != NULL, ("aborted_control is NULL"));
3801
1.26k
  KASSERT(stcb != NULL, ("stcb == NULL"));
3802
1.26k
  SCTP_TCB_LOCK_ASSERT(stcb);
3803
1.26k
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3804
3805
1.26k
  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
1.26k
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_pdapi_event), 0, M_NOWAIT, 1, MT_DATA);
3811
1.26k
  if (m_notify == NULL)
3812
    /* no space left */
3813
0
    return;
3814
1.26k
  SCTP_BUF_LEN(m_notify) = 0;
3815
1.26k
  pdapi = mtod(m_notify, struct sctp_pdapi_event *);
3816
1.26k
  memset(pdapi, 0, sizeof(struct sctp_pdapi_event));
3817
1.26k
  pdapi->pdapi_type = SCTP_PARTIAL_DELIVERY_EVENT;
3818
1.26k
  pdapi->pdapi_flags = 0;
3819
1.26k
  pdapi->pdapi_length = sizeof(struct sctp_pdapi_event);
3820
1.26k
  pdapi->pdapi_indication = error;
3821
1.26k
  pdapi->pdapi_stream = aborted_control->sinfo_stream;
3822
1.26k
  pdapi->pdapi_seq = (uint16_t)aborted_control->mid;
3823
1.26k
  pdapi->pdapi_assoc_id = sctp_get_associd(stcb);
3824
3825
1.26k
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_pdapi_event);
3826
1.26k
  SCTP_BUF_NEXT(m_notify) = NULL;
3827
1.26k
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3828
1.26k
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3829
1.26k
                                   m_notify);
3830
1.26k
  if (control == NULL) {
3831
    /* no memory */
3832
0
    sctp_m_freem(m_notify);
3833
0
    return;
3834
0
  }
3835
1.26k
  control->length = SCTP_BUF_LEN(m_notify);
3836
1.26k
  control->spec_flags = M_NOTIFICATION;
3837
  /* not that we need this */
3838
1.26k
  control->tail_mbuf = m_notify;
3839
1.26k
  sb = &stcb->sctp_socket->so_rcv;
3840
1.26k
  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
1.26k
  sctp_sballoc(stcb, sb, m_notify);
3844
1.26k
  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
1.26k
  control->end_added = 1;
3848
1.26k
  TAILQ_INSERT_AFTER(&stcb->sctp_ep->read_queue, aborted_control, control, next);
3849
1.26k
  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
1.26k
    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
1.26k
  }
3874
1.26k
}
3875
3876
static void
3877
sctp_notify_shutdown_event(struct sctp_tcb *stcb, int so_locked)
3878
92
{
3879
92
  struct mbuf *m_notify;
3880
92
  struct sctp_shutdown_event *sse;
3881
92
  struct sctp_queued_to_read *control;
3882
3883
92
  KASSERT(stcb != NULL, ("stcb == NULL"));
3884
92
  SCTP_TCB_LOCK_ASSERT(stcb);
3885
92
  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
92
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
3892
92
      (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
92
    socantsendmore(stcb->sctp_socket);
3909
#if defined(__APPLE__) && !defined(__Userspace__)
3910
    SCTP_SOCKET_UNLOCK(so, 1);
3911
#endif
3912
92
  }
3913
3914
92
  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
92
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_event), 0, M_NOWAIT, 1, MT_DATA);
3920
92
  if (m_notify == NULL)
3921
    /* no space left */
3922
0
    return;
3923
92
  sse = mtod(m_notify, struct sctp_shutdown_event *);
3924
92
  memset(sse, 0, sizeof(struct sctp_shutdown_event));
3925
92
  sse->sse_type = SCTP_SHUTDOWN_EVENT;
3926
92
  sse->sse_flags = 0;
3927
92
  sse->sse_length = sizeof(struct sctp_shutdown_event);
3928
92
  sse->sse_assoc_id = sctp_get_associd(stcb);
3929
3930
92
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_shutdown_event);
3931
92
  SCTP_BUF_NEXT(m_notify) = NULL;
3932
3933
  /* append to socket */
3934
92
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3935
92
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3936
92
                                   m_notify);
3937
92
  if (control == NULL) {
3938
    /* no memory */
3939
0
    sctp_m_freem(m_notify);
3940
0
    return;
3941
0
  }
3942
92
  control->length = SCTP_BUF_LEN(m_notify);
3943
92
  control->spec_flags = M_NOTIFICATION;
3944
  /* not that we need this */
3945
92
  control->tail_mbuf = m_notify;
3946
92
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3947
92
                    &stcb->sctp_socket->so_rcv, 1,
3948
92
                    SCTP_READ_LOCK_HELD, so_locked);
3949
92
}
3950
3951
static void
3952
sctp_notify_sender_dry_event(struct sctp_tcb *stcb, int so_locked)
3953
470
{
3954
470
  struct mbuf *m_notify;
3955
470
  struct sctp_sender_dry_event *event;
3956
470
  struct sctp_queued_to_read *control;
3957
3958
470
  KASSERT(stcb != NULL, ("stcb == NULL"));
3959
470
  SCTP_TCB_LOCK_ASSERT(stcb);
3960
470
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
3961
3962
470
  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
470
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_sender_dry_event), 0, M_NOWAIT, 1, MT_DATA);
3968
470
  if (m_notify == NULL) {
3969
    /* no space left */
3970
0
    return;
3971
0
  }
3972
470
  SCTP_BUF_LEN(m_notify) = 0;
3973
470
  event = mtod(m_notify, struct sctp_sender_dry_event *);
3974
470
  memset(event, 0, sizeof(struct sctp_sender_dry_event));
3975
470
  event->sender_dry_type = SCTP_SENDER_DRY_EVENT;
3976
470
  event->sender_dry_flags = 0;
3977
470
  event->sender_dry_length = sizeof(struct sctp_sender_dry_event);
3978
470
  event->sender_dry_assoc_id = sctp_get_associd(stcb);
3979
3980
470
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_sender_dry_event);
3981
470
  SCTP_BUF_NEXT(m_notify) = NULL;
3982
3983
  /* append to socket */
3984
470
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
3985
470
                                   0, 0, stcb->asoc.context, 0, 0, 0,
3986
470
                                   m_notify);
3987
470
  if (control == NULL) {
3988
    /* no memory */
3989
0
    sctp_m_freem(m_notify);
3990
0
    return;
3991
0
  }
3992
470
  control->length = SCTP_BUF_LEN(m_notify);
3993
470
  control->spec_flags = M_NOTIFICATION;
3994
  /* not that we need this */
3995
470
  control->tail_mbuf = m_notify;
3996
470
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
3997
470
                    &stcb->sctp_socket->so_rcv, 1,
3998
470
                    SCTP_READ_LOCK_HELD, so_locked);
3999
470
}
4000
4001
static void
4002
sctp_notify_stream_reset_add(struct sctp_tcb *stcb, int flag, int so_locked)
4003
27
{
4004
27
  struct mbuf *m_notify;
4005
27
  struct sctp_queued_to_read *control;
4006
27
  struct sctp_stream_change_event *stradd;
4007
4008
27
  KASSERT(stcb != NULL, ("stcb == NULL"));
4009
27
  SCTP_TCB_LOCK_ASSERT(stcb);
4010
27
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
4011
4012
27
  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
27
  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
27
  stcb->asoc.peer_req_out = 0;
4023
27
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_stream_change_event), 0, M_NOWAIT, 1, MT_DATA);
4024
27
  if (m_notify == NULL)
4025
    /* no space left */
4026
0
    return;
4027
27
  SCTP_BUF_LEN(m_notify) = 0;
4028
27
  stradd = mtod(m_notify, struct sctp_stream_change_event *);
4029
27
  memset(stradd, 0, sizeof(struct sctp_stream_change_event));
4030
27
  stradd->strchange_type = SCTP_STREAM_CHANGE_EVENT;
4031
27
  stradd->strchange_flags = flag;
4032
27
  stradd->strchange_length = sizeof(struct sctp_stream_change_event);
4033
27
  stradd->strchange_assoc_id = sctp_get_associd(stcb);
4034
27
  stradd->strchange_instrms = stcb->asoc.streamincnt;
4035
27
  stradd->strchange_outstrms = stcb->asoc.streamoutcnt;
4036
27
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_stream_change_event);
4037
27
  SCTP_BUF_NEXT(m_notify) = NULL;
4038
27
  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
22
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4045
22
                                   0, 0, stcb->asoc.context, 0, 0, 0,
4046
22
                                   m_notify);
4047
22
  if (control == NULL) {
4048
    /* no memory */
4049
0
    sctp_m_freem(m_notify);
4050
0
    return;
4051
0
  }
4052
22
  control->length = SCTP_BUF_LEN(m_notify);
4053
22
  control->spec_flags = M_NOTIFICATION;
4054
  /* not that we need this */
4055
22
  control->tail_mbuf = m_notify;
4056
22
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
4057
22
                    &stcb->sctp_socket->so_rcv, 1,
4058
22
                    SCTP_READ_LOCK_HELD, so_locked);
4059
22
}
4060
4061
static void
4062
sctp_notify_stream_reset_tsn(struct sctp_tcb *stcb, int flag, int so_locked)
4063
58
{
4064
58
  struct mbuf *m_notify;
4065
58
  struct sctp_queued_to_read *control;
4066
58
  struct sctp_assoc_reset_event *strasoc;
4067
4068
58
  KASSERT(stcb != NULL, ("stcb == NULL"));
4069
58
  SCTP_TCB_LOCK_ASSERT(stcb);
4070
58
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
4071
4072
58
  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
58
  m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_assoc_reset_event), 0, M_NOWAIT, 1, MT_DATA);
4078
58
  if (m_notify == NULL)
4079
    /* no space left */
4080
0
    return;
4081
58
  SCTP_BUF_LEN(m_notify) = 0;
4082
58
  strasoc = mtod(m_notify, struct sctp_assoc_reset_event  *);
4083
58
  memset(strasoc, 0, sizeof(struct sctp_assoc_reset_event));
4084
58
  strasoc->assocreset_type = SCTP_ASSOC_RESET_EVENT;
4085
58
  strasoc->assocreset_flags = flag;
4086
58
  strasoc->assocreset_length = sizeof(struct sctp_assoc_reset_event);
4087
58
  strasoc->assocreset_assoc_id= sctp_get_associd(stcb);
4088
58
  strasoc->assocreset_local_tsn = stcb->asoc.sending_seq;
4089
58
  strasoc->assocreset_remote_tsn = stcb->asoc.mapping_array_base_tsn + 1;
4090
58
  SCTP_BUF_LEN(m_notify) = sizeof(struct sctp_assoc_reset_event);
4091
58
  SCTP_BUF_NEXT(m_notify) = NULL;
4092
58
  if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
4093
    /* no space */
4094
20
    sctp_m_freem(m_notify);
4095
20
    return;
4096
20
  }
4097
  /* append to socket */
4098
38
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4099
38
                                   0, 0, stcb->asoc.context, 0, 0, 0,
4100
38
                                   m_notify);
4101
38
  if (control == NULL) {
4102
    /* no memory */
4103
0
    sctp_m_freem(m_notify);
4104
0
    return;
4105
0
  }
4106
38
  control->length = SCTP_BUF_LEN(m_notify);
4107
38
  control->spec_flags = M_NOTIFICATION;
4108
  /* not that we need this */
4109
38
  control->tail_mbuf = m_notify;
4110
38
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
4111
38
                    &stcb->sctp_socket->so_rcv, 1,
4112
38
                    SCTP_READ_LOCK_HELD, so_locked);
4113
38
}
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
615
{
4119
615
  struct mbuf *m_notify;
4120
615
  struct sctp_queued_to_read *control;
4121
615
  struct sctp_stream_reset_event *strreset;
4122
615
  int len;
4123
4124
615
  KASSERT(stcb != NULL, ("stcb == NULL"));
4125
615
  SCTP_TCB_LOCK_ASSERT(stcb);
4126
615
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
4127
4128
615
  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
615
  m_notify = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
4134
615
  if (m_notify == NULL)
4135
    /* no space left */
4136
0
    return;
4137
615
  SCTP_BUF_LEN(m_notify) = 0;
4138
615
  len = sizeof(struct sctp_stream_reset_event) + (number_entries * sizeof(uint16_t));
4139
615
  if (len > M_TRAILINGSPACE(m_notify)) {
4140
    /* never enough room */
4141
0
    sctp_m_freem(m_notify);
4142
0
    return;
4143
0
  }
4144
615
  strreset = mtod(m_notify, struct sctp_stream_reset_event *);
4145
615
  memset(strreset, 0, len);
4146
615
  strreset->strreset_type = SCTP_STREAM_RESET_EVENT;
4147
615
  strreset->strreset_flags = flag;
4148
615
  strreset->strreset_length = len;
4149
615
  strreset->strreset_assoc_id = sctp_get_associd(stcb);
4150
615
  if (number_entries) {
4151
260
    int i;
4152
4153
5.67k
    for (i = 0; i < number_entries; i++) {
4154
5.41k
      strreset->strreset_stream_list[i] = ntohs(list[i]);
4155
5.41k
    }
4156
260
  }
4157
615
  SCTP_BUF_LEN(m_notify) = len;
4158
615
  SCTP_BUF_NEXT(m_notify) = NULL;
4159
615
  if (sctp_sbspace(&stcb->asoc, &stcb->sctp_socket->so_rcv) < SCTP_BUF_LEN(m_notify)) {
4160
    /* no space */
4161
35
    sctp_m_freem(m_notify);
4162
35
    return;
4163
35
  }
4164
  /* append to socket */
4165
580
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4166
580
                                   0, 0, stcb->asoc.context, 0, 0, 0,
4167
580
                                   m_notify);
4168
580
  if (control == NULL) {
4169
    /* no memory */
4170
0
    sctp_m_freem(m_notify);
4171
0
    return;
4172
0
  }
4173
580
  control->length = SCTP_BUF_LEN(m_notify);
4174
580
  control->spec_flags = M_NOTIFICATION;
4175
  /* not that we need this */
4176
580
  control->tail_mbuf = m_notify;
4177
580
  sctp_add_to_readq(stcb->sctp_ep, stcb, control,
4178
580
                    &stcb->sctp_socket->so_rcv, 1,
4179
580
                    SCTP_READ_LOCK_HELD, so_locked);
4180
580
}
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
426k
{
4186
426k
  struct mbuf *m_notify;
4187
426k
  struct sctp_remote_error *sre;
4188
426k
  struct sctp_queued_to_read *control;
4189
426k
  unsigned int notif_len;
4190
426k
  uint16_t chunk_len;
4191
4192
426k
  KASSERT(stcb != NULL, ("stcb == NULL"));
4193
426k
  SCTP_TCB_LOCK_ASSERT(stcb);
4194
426k
  SCTP_INP_READ_LOCK_ASSERT(stcb->sctp_ep);
4195
4196
426k
  if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_RECVPEERERR)) {
4197
0
    return;
4198
0
  }
4199
4200
426k
  if (chunk != NULL) {
4201
426k
    chunk_len = ntohs(chunk->ch.chunk_length);
4202
    /*
4203
     * Only SCTP_CHUNK_BUFFER_SIZE are guaranteed to be
4204
     * contiguous.
4205
     */
4206
426k
    if (chunk_len > SCTP_CHUNK_BUFFER_SIZE) {
4207
118
      chunk_len = SCTP_CHUNK_BUFFER_SIZE;
4208
118
    }
4209
426k
  } else {
4210
0
    chunk_len = 0;
4211
0
  }
4212
426k
  notif_len = (unsigned int)(sizeof(struct sctp_remote_error) + chunk_len);
4213
426k
  m_notify = sctp_get_mbuf_for_msg(notif_len, 0, M_NOWAIT, 1, MT_DATA);
4214
426k
  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
426k
  SCTP_BUF_NEXT(m_notify) = NULL;
4223
426k
  sre = mtod(m_notify, struct sctp_remote_error *);
4224
426k
  memset(sre, 0, notif_len);
4225
426k
  sre->sre_type = SCTP_REMOTE_ERROR;
4226
426k
  sre->sre_flags = 0;
4227
426k
  sre->sre_length = sizeof(struct sctp_remote_error);
4228
426k
  sre->sre_error = error;
4229
426k
  sre->sre_assoc_id = sctp_get_associd(stcb);
4230
426k
  if (notif_len > sizeof(struct sctp_remote_error)) {
4231
426k
    memcpy(sre->sre_data, chunk, chunk_len);
4232
426k
    sre->sre_length += chunk_len;
4233
426k
  }
4234
426k
  SCTP_BUF_LEN(m_notify) = sre->sre_length;
4235
426k
  control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
4236
426k
                                   0, 0, stcb->asoc.context, 0, 0, 0,
4237
426k
                                   m_notify);
4238
426k
  if (control != NULL) {
4239
426k
    control->length = SCTP_BUF_LEN(m_notify);
4240
426k
    control->spec_flags = M_NOTIFICATION;
4241
    /* not that we need this */
4242
426k
    control->tail_mbuf = m_notify;
4243
426k
    sctp_add_to_readq(stcb->sctp_ep, stcb, control,
4244
426k
                      &stcb->sctp_socket->so_rcv, 1,
4245
426k
                      SCTP_READ_LOCK_HELD, so_locked);
4246
426k
  } else {
4247
0
    sctp_m_freem(m_notify);
4248
0
  }
4249
426k
}
4250
4251
void
4252
sctp_ulp_notify(uint32_t notification, struct sctp_tcb *stcb,
4253
                uint32_t error, void *data, int so_locked)
4254
526k
{
4255
526k
  struct sctp_inpcb *inp;
4256
526k
  struct sctp_nets *net;
4257
4258
526k
  KASSERT(stcb != NULL, ("stcb == NULL"));
4259
526k
  SCTP_TCB_LOCK_ASSERT(stcb);
4260
4261
526k
  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
526k
  if (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) {
4270
0
    return;
4271
0
  }
4272
526k
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4273
526k
      (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4274
224k
    if ((notification == SCTP_NOTIFY_INTERFACE_DOWN) ||
4275
224k
        (notification == SCTP_NOTIFY_INTERFACE_UP) ||
4276
224k
        (notification == SCTP_NOTIFY_INTERFACE_CONFIRMED)) {
4277
      /* Don't report these in front states */
4278
42
      return;
4279
42
    }
4280
224k
  }
4281
526k
  if (notification != SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION) {
4282
525k
    SCTP_INP_READ_LOCK(inp);
4283
525k
  }
4284
526k
  SCTP_INP_READ_LOCK_ASSERT(inp);
4285
4286
526k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4287
526k
      (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4288
526k
      (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_CANT_READ)) {
4289
0
    SCTP_INP_READ_UNLOCK(inp);
4290
0
    return;
4291
0
  }
4292
4293
526k
  switch (notification) {
4294
5.52k
  case SCTP_NOTIFY_ASSOC_UP:
4295
5.52k
    if (stcb->asoc.assoc_up_sent == 0) {
4296
5.52k
      sctp_notify_assoc_change(SCTP_COMM_UP, stcb, error, NULL, false, false, so_locked);
4297
5.52k
      stcb->asoc.assoc_up_sent = 1;
4298
5.52k
    }
4299
5.52k
    if (stcb->asoc.adaptation_needed && (stcb->asoc.adaptation_sent == 0)) {
4300
5.52k
      sctp_notify_adaptation_layer(stcb, so_locked);
4301
5.52k
    }
4302
5.52k
    if (stcb->asoc.auth_supported == 0) {
4303
0
      sctp_notify_authentication(stcb, SCTP_AUTH_NO_AUTH, 0, so_locked);
4304
0
    }
4305
5.52k
    break;
4306
1
  case SCTP_NOTIFY_ASSOC_DOWN:
4307
1
    sctp_notify_assoc_change(SCTP_SHUTDOWN_COMP, stcb, error, NULL, false, false, so_locked);
4308
1
#if defined(__Userspace__)
4309
1
    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
1
#endif
4324
1
    break;
4325
1
  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
534
  case SCTP_NOTIFY_SPECIAL_SP_FAIL:
4341
534
    sctp_notify_send_failed2(stcb, error,
4342
534
                             (struct sctp_stream_queue_pending *)data, so_locked);
4343
534
    break;
4344
513
  case SCTP_NOTIFY_SENT_DG_FAIL:
4345
513
    sctp_notify_send_failed(stcb, 1, error,
4346
513
        (struct sctp_tmit_chunk *)data, so_locked);
4347
513
    break;
4348
29
  case SCTP_NOTIFY_UNSENT_DG_FAIL:
4349
29
    sctp_notify_send_failed(stcb, 0, error,
4350
29
                            (struct sctp_tmit_chunk *)data, so_locked);
4351
29
    break;
4352
1.26k
  case SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION:
4353
1.26k
    sctp_notify_partial_delivery_indication(stcb, error,
4354
1.26k
                                            (struct sctp_queued_to_read *)data,
4355
1.26k
                                            so_locked);
4356
1.26k
    break;
4357
1.64k
  case SCTP_NOTIFY_ASSOC_LOC_ABORTED:
4358
1.64k
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4359
1.64k
        (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4360
627
      sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, false, false, so_locked);
4361
1.01k
    } else {
4362
1.01k
      sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, false, false, so_locked);
4363
1.01k
    }
4364
1.64k
    break;
4365
59
  case SCTP_NOTIFY_ASSOC_REM_ABORTED:
4366
59
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
4367
59
        (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
4368
39
      sctp_notify_assoc_change(SCTP_CANT_STR_ASSOC, stcb, error, data, true, false, so_locked);
4369
39
    } else {
4370
20
      sctp_notify_assoc_change(SCTP_COMM_LOST, stcb, error, data, true, false, so_locked);
4371
20
    }
4372
59
    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
58
  case SCTP_NOTIFY_STR_RESET_SEND:
4388
58
    sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_OUTGOING_SSN, so_locked);
4389
58
    break;
4390
557
  case SCTP_NOTIFY_STR_RESET_RECV:
4391
557
    sctp_notify_stream_reset(stcb, error, ((uint16_t *) data), SCTP_STREAM_RESET_INCOMING, so_locked);
4392
557
    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
27
  case SCTP_NOTIFY_STR_RESET_ADD:
4410
27
    sctp_notify_stream_reset_add(stcb, error, so_locked);
4411
27
    break;
4412
58
  case SCTP_NOTIFY_STR_RESET_TSN:
4413
58
    sctp_notify_stream_reset_tsn(stcb, error, so_locked);
4414
58
    break;
4415
42.5k
  case SCTP_NOTIFY_ASCONF_ADD_IP:
4416
42.5k
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_ADDED, data,
4417
42.5k
                                 error, so_locked);
4418
42.5k
    break;
4419
44.8k
  case SCTP_NOTIFY_ASCONF_DELETE_IP:
4420
44.8k
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_REMOVED, data,
4421
44.8k
                                 error, so_locked);
4422
44.8k
    break;
4423
1.70k
  case SCTP_NOTIFY_ASCONF_SET_PRIMARY:
4424
1.70k
    sctp_notify_peer_addr_change(stcb, SCTP_ADDR_MADE_PRIM, data,
4425
1.70k
                                 error, so_locked);
4426
1.70k
    break;
4427
92
  case SCTP_NOTIFY_PEER_SHUTDOWN:
4428
92
    sctp_notify_shutdown_event(stcb, so_locked);
4429
92
    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
470
  case SCTP_NOTIFY_SENDER_DRY:
4443
470
    sctp_notify_sender_dry_event(stcb, so_locked);
4444
470
    break;
4445
426k
  case SCTP_NOTIFY_REMOTE_ERROR:
4446
426k
    sctp_notify_remote_error(stcb, error, data, so_locked);
4447
426k
    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
526k
  }
4453
526k
  if (notification != SCTP_NOTIFY_PARTIAL_DELVIERY_INDICATION) {
4454
525k
    SCTP_INP_READ_UNLOCK(inp);
4455
525k
  }
4456
526k
}
4457
4458
void
4459
sctp_report_all_outbound(struct sctp_tcb *stcb, uint16_t error, int so_locked)
4460
1.69k
{
4461
1.69k
  struct sctp_association *asoc;
4462
1.69k
  struct sctp_stream_out *outs;
4463
1.69k
  struct sctp_tmit_chunk *chk, *nchk;
4464
1.69k
  struct sctp_stream_queue_pending *sp, *nsp;
4465
1.69k
  int i;
4466
4467
1.69k
  if (stcb == NULL) {
4468
0
    return;
4469
0
  }
4470
1.69k
  asoc = &stcb->asoc;
4471
1.69k
  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.69k
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4483
1.69k
      (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4484
1.69k
      (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.69k
  TAILQ_FOREACH_SAFE(chk, &asoc->sent_queue, sctp_next, nchk) {
4490
513
    TAILQ_REMOVE(&asoc->sent_queue, chk, sctp_next);
4491
513
    asoc->sent_queue_cnt--;
4492
513
    if (chk->sent != SCTP_DATAGRAM_NR_ACKED) {
4493
513
      if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
4494
513
        asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4495
513
#ifdef INVARIANTS
4496
513
      } else {
4497
0
        panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4498
0
#endif
4499
0
      }
4500
513
    }
4501
513
    if (chk->data != NULL) {
4502
513
      sctp_free_bufspace(stcb, asoc, chk, 1);
4503
513
      sctp_ulp_notify(SCTP_NOTIFY_SENT_DG_FAIL, stcb,
4504
513
                      error, chk, so_locked);
4505
513
      if (chk->data) {
4506
0
        sctp_m_freem(chk->data);
4507
0
        chk->data = NULL;
4508
0
      }
4509
513
    }
4510
513
    sctp_free_a_chunk(stcb, chk, so_locked);
4511
    /*sa_ignore FREED_MEMORY*/
4512
513
  }
4513
  /* pending send queue SHOULD be empty */
4514
1.69k
  TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
4515
29
    TAILQ_REMOVE(&asoc->send_queue, chk, sctp_next);
4516
29
    asoc->send_queue_cnt--;
4517
29
    if (asoc->strmout[chk->rec.data.sid].chunks_on_queues > 0) {
4518
29
      asoc->strmout[chk->rec.data.sid].chunks_on_queues--;
4519
29
#ifdef INVARIANTS
4520
29
    } else {
4521
0
      panic("No chunks on the queues for sid %u.", chk->rec.data.sid);
4522
0
#endif
4523
0
    }
4524
29
    if (chk->data != NULL) {
4525
29
      sctp_free_bufspace(stcb, asoc, chk, 1);
4526
29
      sctp_ulp_notify(SCTP_NOTIFY_UNSENT_DG_FAIL, stcb,
4527
29
                      error, chk, so_locked);
4528
29
      if (chk->data) {
4529
0
        sctp_m_freem(chk->data);
4530
0
        chk->data = NULL;
4531
0
      }
4532
29
    }
4533
29
    sctp_free_a_chunk(stcb, chk, so_locked);
4534
    /*sa_ignore FREED_MEMORY*/
4535
29
  }
4536
410k
  for (i = 0; i < asoc->streamoutcnt; i++) {
4537
    /* For each stream */
4538
408k
    outs = &asoc->strmout[i];
4539
    /* clean up any sends there */
4540
408k
    TAILQ_FOREACH_SAFE(sp, &outs->outqueue, next, nsp) {
4541
534
      atomic_subtract_int(&asoc->stream_queue_cnt, 1);
4542
534
      TAILQ_REMOVE(&outs->outqueue, sp, next);
4543
534
      stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, outs, sp);
4544
534
      sctp_free_spbufspace(stcb, asoc, sp);
4545
534
      if (sp->data) {
4546
534
        sctp_ulp_notify(SCTP_NOTIFY_SPECIAL_SP_FAIL, stcb,
4547
534
            error, (void *)sp, so_locked);
4548
534
        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
534
      }
4555
534
      if (sp->net) {
4556
0
        sctp_free_remote_addr(sp->net);
4557
0
        sp->net = NULL;
4558
0
      }
4559
      /* Free the chunk */
4560
534
      sctp_free_a_strmoq(stcb, sp, so_locked);
4561
      /*sa_ignore FREED_MEMORY*/
4562
534
    }
4563
408k
  }
4564
1.69k
}
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.69k
{
4571
1.69k
  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.69k
  SCTP_TCB_LOCK_ASSERT(stcb);
4582
4583
1.69k
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL) ||
4584
1.69k
      ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
4585
1.69k
       (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_CONNECTED))) {
4586
1.69k
    sctp_pcb_add_flags(stcb->sctp_ep, SCTP_PCB_FLAGS_WAS_ABORTED);
4587
1.69k
  }
4588
1.69k
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
4589
1.69k
      (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
4590
1.69k
      (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)) {
4591
0
    return;
4592
0
  }
4593
1.69k
  SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_WAS_ABORTED);
4594
  /* Tell them we lost the asoc */
4595
1.69k
  sctp_report_all_outbound(stcb, error, so_locked);
4596
1.69k
  if (from_peer) {
4597
59
    sctp_ulp_notify(SCTP_NOTIFY_ASSOC_REM_ABORTED, stcb, error, abort, so_locked);
4598
1.64k
  } else {
4599
1.64k
    if (timeout) {
4600
0
      sctp_ulp_notify(SCTP_NOTIFY_ASSOC_TIMEDOUT, stcb, error, abort, so_locked);
4601
1.64k
    } else {
4602
1.64k
      sctp_ulp_notify(SCTP_NOTIFY_ASSOC_LOC_ABORTED, stcb, error, abort, so_locked);
4603
1.64k
    }
4604
1.64k
  }
4605
1.69k
}
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
482
{
4617
#if defined(__APPLE__) && !defined(__Userspace__)
4618
  struct socket *so;
4619
#endif
4620
482
  struct sctp_gen_error_cause* cause;
4621
482
  uint32_t vtag;
4622
482
  uint16_t cause_code;
4623
4624
482
  if (stcb != NULL) {
4625
482
    vtag = stcb->asoc.peer_vtag;
4626
482
    vrf_id = stcb->asoc.vrf_id;
4627
482
    if (op_err != NULL) {
4628
      /* Read the cause code from the error cause. */
4629
270
      cause = mtod(op_err, struct sctp_gen_error_cause *);
4630
270
      cause_code = ntohs(cause->code);
4631
270
    } else {
4632
212
      cause_code = 0;
4633
212
    }
4634
482
  } else {
4635
0
    vtag = 0;
4636
0
  }
4637
482
  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
482
                  vrf_id, port);
4642
482
  if (stcb != NULL) {
4643
    /* We have a TCB to abort, send notification too */
4644
482
    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
482
    SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4655
482
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4656
482
        (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4657
0
      SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4658
0
    }
4659
482
    (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4660
482
                          SCTP_FROM_SCTPUTIL + SCTP_LOC_4);
4661
#if defined(__APPLE__) && !defined(__Userspace__)
4662
    SCTP_SOCKET_UNLOCK(so, 1);
4663
#endif
4664
482
  }
4665
482
}
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.15k
{
4732
#if defined(__APPLE__) && !defined(__Userspace__)
4733
  struct socket *so;
4734
#endif
4735
1.15k
  struct sctp_gen_error_cause* cause;
4736
1.15k
  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.15k
  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.15k
  if (op_err != NULL) {
4769
    /* Read the cause code from the error cause. */
4770
1.15k
    cause = mtod(op_err, struct sctp_gen_error_cause *);
4771
1.15k
    cause_code = ntohs(cause->code);
4772
1.15k
  } else {
4773
0
    cause_code = 0;
4774
0
  }
4775
  /* notify the peer */
4776
1.15k
  sctp_send_abort_tcb(stcb, op_err, so_locked);
4777
1.15k
  SCTP_STAT_INCR_COUNTER32(sctps_aborted);
4778
1.15k
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
4779
1.15k
      (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) {
4780
1.01k
    SCTP_STAT_DECR_GAUGE32(sctps_currestab);
4781
1.01k
  }
4782
  /* notify the ulp */
4783
1.15k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) == 0) {
4784
1.15k
    sctp_abort_notification(stcb, false, timedout, cause_code, NULL, so_locked);
4785
1.15k
  }
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.15k
  (void)sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
4800
1.15k
                        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.15k
}
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
348
{
4818
348
  struct sctp_chunkhdr *ch, chunk_buf;
4819
348
  unsigned int chk_length;
4820
348
  int contains_init_chunk;
4821
4822
348
  SCTP_STAT_INCR_COUNTER32(sctps_outoftheblue);
4823
  /* Generate a TO address for future reference */
4824
348
  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
348
  contains_init_chunk = 0;
4837
348
  ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4838
348
      sizeof(*ch), (uint8_t *) & chunk_buf);
4839
33.4k
  while (ch != NULL) {
4840
33.1k
    chk_length = ntohs(ch->chunk_length);
4841
33.1k
    if (chk_length < sizeof(*ch)) {
4842
      /* break to abort land */
4843
38
      break;
4844
38
    }
4845
33.1k
    switch (ch->chunk_type) {
4846
30.2k
    case SCTP_INIT:
4847
30.2k
      contains_init_chunk = 1;
4848
30.2k
      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
2.92k
    default:
4869
2.92k
      break;
4870
33.1k
    }
4871
33.1k
    offset += SCTP_SIZE32(chk_length);
4872
33.1k
    ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4873
33.1k
        sizeof(*ch), (uint8_t *) & chunk_buf);
4874
33.1k
  }
4875
341
  if ((SCTP_BASE_SYSCTL(sctp_blackhole) == 0) ||
4876
341
      ((SCTP_BASE_SYSCTL(sctp_blackhole) == 1) &&
4877
341
       (contains_init_chunk == 0))) {
4878
341
    sctp_send_abort(m, iphlen, src, dst, sh, 0, cause,
4879
#if defined(__FreeBSD__) && !defined(__Userspace__)
4880
                    mflowtype, mflowid, fibnum,
4881
#endif
4882
341
                    vrf_id, port);
4883
341
  }
4884
341
}
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
823
{
4893
823
  struct sctp_chunkhdr *ch;
4894
823
  struct sctp_init_chunk *init_chk, chunk_buf;
4895
823
  int offset;
4896
823
  unsigned int chk_length;
4897
4898
823
  offset = iphlen + sizeof(struct sctphdr);
4899
823
  ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset, sizeof(*ch),
4900
823
      (uint8_t *) & chunk_buf);
4901
70.7k
  while (ch != NULL) {
4902
69.9k
    chk_length = ntohs(ch->chunk_length);
4903
69.9k
    if (chk_length < sizeof(*ch)) {
4904
      /* packet is probably corrupt */
4905
38
      break;
4906
38
    }
4907
    /* we seem to be ok, is it an abort? */
4908
69.9k
    if (ch->chunk_type == SCTP_ABORT_ASSOCIATION) {
4909
      /* yep, tell them */
4910
0
      return (1);
4911
0
    }
4912
69.9k
    if ((ch->chunk_type == SCTP_INITIATION) ||
4913
69.9k
        (ch->chunk_type == SCTP_INITIATION_ACK)) {
4914
      /* need to update the Vtag */
4915
31.7k
      init_chk = (struct sctp_init_chunk *)sctp_m_getptr(m,
4916
31.7k
          offset, sizeof(struct sctp_init_chunk), (uint8_t *) & chunk_buf);
4917
31.7k
      if (init_chk != NULL) {
4918
31.5k
        *vtag = ntohl(init_chk->init.initiate_tag);
4919
31.5k
      }
4920
31.7k
    }
4921
    /* Nope, move to the next chunk */
4922
69.9k
    offset += SCTP_SIZE32(chk_length);
4923
69.9k
    ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
4924
69.9k
        sizeof(*ch), (uint8_t *) & chunk_buf);
4925
69.9k
  }
4926
823
  return (0);
4927
823
}
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
355k
{
5014
5015
  /* must be valid */
5016
355k
  if (sa1 == NULL || sa2 == NULL)
5017
0
    return (0);
5018
5019
  /* must be the same family */
5020
355k
  if (sa1->sa_family != sa2->sa_family)
5021
80.3k
    return (0);
5022
5023
274k
  switch (sa1->sa_family) {
5024
0
#ifdef INET6
5025
30.3k
  case AF_INET6:
5026
30.3k
  {
5027
    /* IPv6 addresses */
5028
30.3k
    struct sockaddr_in6 *sin6_1, *sin6_2;
5029
5030
30.3k
    sin6_1 = (struct sockaddr_in6 *)sa1;
5031
30.3k
    sin6_2 = (struct sockaddr_in6 *)sa2;
5032
30.3k
    return (SCTP6_ARE_ADDR_EQUAL(sin6_1,
5033
30.3k
        sin6_2));
5034
0
  }
5035
0
#endif
5036
0
#ifdef INET
5037
208k
  case AF_INET:
5038
208k
  {
5039
    /* IPv4 addresses */
5040
208k
    struct sockaddr_in *sin_1, *sin_2;
5041
5042
208k
    sin_1 = (struct sockaddr_in *)sa1;
5043
208k
    sin_2 = (struct sockaddr_in *)sa2;
5044
208k
    return (sin_1->sin_addr.s_addr == sin_2->sin_addr.s_addr);
5045
0
  }
5046
0
#endif
5047
0
#if defined(__Userspace__)
5048
35.7k
  case AF_CONN:
5049
35.7k
  {
5050
35.7k
    struct sockaddr_conn *sconn_1, *sconn_2;
5051
5052
35.7k
    sconn_1 = (struct sockaddr_conn *)sa1;
5053
35.7k
    sconn_2 = (struct sockaddr_conn *)sa2;
5054
35.7k
    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
274k
  }
5061
274k
}
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
531k
{
5234
531k
  if ((inp != NULL) &&
5235
531k
      (inp->sctp_socket != NULL) &&
5236
531k
      (((inp->sctp_flags & (SCTP_PCB_FLAGS_TCPTYPE | SCTP_PCB_FLAGS_IN_TCPPOOL)) == 0) ||
5237
531k
       !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
531k
    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
531k
  }
5265
531k
}
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
531k
{
5274
531k
  uint32_t pd_point, length;
5275
5276
531k
  if ((inp->recv_callback == NULL) ||
5277
531k
      (stcb == NULL) ||
5278
531k
      (stcb->sctp_socket == NULL)) {
5279
531k
    return;
5280
531k
  }
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
531k
{
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
531k
  struct mbuf *m, *prev = NULL;
5381
5382
531k
  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
531k
  if (inp_read_lock_held == SCTP_READ_LOCK_NOT_HELD) {
5397
883
    SCTP_INP_READ_LOCK(inp);
5398
883
  }
5399
531k
  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
531k
  if ((control->spec_flags & M_NOTIFICATION) == 0) {
5414
883
    atomic_add_int(&inp->total_recvs, 1);
5415
883
    if (!control->do_not_ref_stcb) {
5416
883
      atomic_add_int(&stcb->total_recvs, 1);
5417
883
    }
5418
883
  }
5419
531k
  m = control->data;
5420
531k
  control->held_length = 0;
5421
531k
  control->length = 0;
5422
1.06M
  while (m != NULL) {
5423
533k
    if (SCTP_BUF_LEN(m) == 0) {
5424
      /* Skip mbufs with NO length */
5425
119
      if (prev == NULL) {
5426
        /* First one */
5427
119
        control->data = sctp_m_free(m);
5428
119
        m = control->data;
5429
119
      } else {
5430
0
        SCTP_BUF_NEXT(prev) = sctp_m_free(m);
5431
0
        m = SCTP_BUF_NEXT(prev);
5432
0
      }
5433
119
      if (m == NULL) {
5434
0
        control->tail_mbuf = prev;
5435
0
      }
5436
119
      continue;
5437
119
    }
5438
533k
    prev = m;
5439
533k
    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
533k
    sctp_sballoc(stcb, sb, m);
5443
533k
    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
533k
    atomic_add_int(&control->length, SCTP_BUF_LEN(m));
5447
533k
    m = SCTP_BUF_NEXT(m);
5448
533k
  }
5449
531k
  if (prev != NULL) {
5450
531k
    control->tail_mbuf = prev;
5451
531k
  } 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
531k
  if (end) {
5462
531k
    control->end_added = 1;
5463
531k
  }
5464
531k
  TAILQ_INSERT_TAIL(&inp->read_queue, control, next);
5465
531k
  control->on_read_q = 1;
5466
531k
#if defined(__Userspace__)
5467
531k
  sctp_invoke_recv_callback(inp, stcb, control, SCTP_READ_LOCK_HELD);
5468
531k
#endif
5469
531k
  if ((inp != NULL) && (inp->sctp_socket != NULL)) {
5470
531k
    sctp_wakeup_the_read_socket(inp, stcb, so_locked);
5471
531k
  }
5472
531k
  if (inp_read_lock_held == SCTP_READ_LOCK_NOT_HELD) {
5473
883
    SCTP_INP_READ_UNLOCK(inp);
5474
883
  }
5475
531k
}
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.65k
{
5488
8.65k
  struct mbuf *m;
5489
8.65k
  struct sctp_gen_error_cause *cause;
5490
8.65k
  size_t info_len;
5491
8.65k
  uint16_t len;
5492
5493
8.65k
  if ((code == 0) || (info == NULL)) {
5494
560
    return (NULL);
5495
560
  }
5496
8.09k
  info_len = strlen(info);
5497
8.09k
  if (info_len > (SCTP_MAX_CAUSE_LENGTH - sizeof(struct sctp_paramhdr))) {
5498
0
    return (NULL);
5499
0
  }
5500
8.09k
  len = (uint16_t)(sizeof(struct sctp_paramhdr) + info_len);
5501
8.09k
  m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
5502
8.09k
  if (m != NULL) {
5503
8.09k
    SCTP_BUF_LEN(m) = len;
5504
8.09k
    cause = mtod(m, struct sctp_gen_error_cause *);
5505
8.09k
    cause->code = htons(code);
5506
8.09k
    cause->length = htons(len);
5507
8.09k
    memcpy(cause->info, info, info_len);
5508
8.09k
  }
5509
8.09k
  return (m);
5510
8.09k
}
5511
5512
struct mbuf *
5513
sctp_generate_no_user_data_cause(uint32_t tsn)
5514
5
{
5515
5
  struct mbuf *m;
5516
5
  struct sctp_error_no_user_data *no_user_data_cause;
5517
5
  uint16_t len;
5518
5519
5
  len = (uint16_t)sizeof(struct sctp_error_no_user_data);
5520
5
  m = sctp_get_mbuf_for_msg(len, 0, M_NOWAIT, 1, MT_DATA);
5521
5
  if (m != NULL) {
5522
5
    SCTP_BUF_LEN(m) = len;
5523
5
    no_user_data_cause = mtod(m, struct sctp_error_no_user_data *);
5524
5
    no_user_data_cause->cause.code = htons(SCTP_CAUSE_NO_USER_DATA);
5525
5
    no_user_data_cause->cause.length = htons(len);
5526
5
    no_user_data_cause->tsn = htonl(tsn);
5527
5
  }
5528
5
  return (m);
5529
5
}
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
542
{
5535
542
  if (tp1->data == NULL) {
5536
0
    return;
5537
0
  }
5538
542
  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
542
  if (asoc->total_output_queue_size >= tp1->book_size) {
5549
542
    atomic_subtract_int(&asoc->total_output_queue_size, tp1->book_size);
5550
542
  } else {
5551
0
    asoc->total_output_queue_size = 0;
5552
0
  }
5553
542
  if ((stcb->sctp_socket != NULL) &&
5554
542
      (((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) ||
5555
542
       ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE)))) {
5556
542
    SCTP_SB_DECR(&stcb->sctp_socket->so_snd, tp1->book_size);
5557
542
  }
5558
542
}
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
63.8k
{
5863
63.8k
  switch (addr->sa_family) {
5864
0
#ifdef INET
5865
38.5k
  case AF_INET:
5866
38.5k
  {
5867
38.5k
    struct sockaddr_in *sin;
5868
5869
38.5k
    sin = (struct sockaddr_in *)addr;
5870
38.5k
    return (sin->sin_addr.s_addr ^ (sin->sin_addr.s_addr >> 16));
5871
0
  }
5872
0
#endif
5873
0
#ifdef INET6
5874
4.06k
  case AF_INET6:
5875
4.06k
  {
5876
4.06k
    struct sockaddr_in6 *sin6;
5877
4.06k
    uint32_t hash_of_addr;
5878
5879
4.06k
    sin6 = (struct sockaddr_in6 *)addr;
5880
4.06k
#if !defined(_WIN32) && !(defined(__FreeBSD__) && defined(__Userspace__)) && !defined(__APPLE__)
5881
4.06k
    hash_of_addr = (sin6->sin6_addr.s6_addr32[0] +
5882
4.06k
        sin6->sin6_addr.s6_addr32[1] +
5883
4.06k
        sin6->sin6_addr.s6_addr32[2] +
5884
4.06k
        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
4.06k
    hash_of_addr = (hash_of_addr ^ (hash_of_addr >> 16));
5892
4.06k
    return (hash_of_addr);
5893
0
  }
5894
0
#endif
5895
0
#if defined(__Userspace__)
5896
21.2k
  case AF_CONN:
5897
21.2k
  {
5898
21.2k
    struct sockaddr_conn *sconn;
5899
21.2k
    uintptr_t temp;
5900
5901
21.2k
    sconn = (struct sockaddr_conn *)addr;
5902
21.2k
    temp = (uintptr_t)sconn->sconn_addr;
5903
21.2k
    return ((uint32_t)(temp ^ (temp >> 16)));
5904
0
  }
5905
0
#endif
5906
0
  default:
5907
0
    break;
5908
63.8k
  }
5909
0
  return (0);
5910
63.8k
}
5911
5912
struct sctp_ifa *
5913
sctp_find_ifa_by_addr(struct sockaddr *addr, uint32_t vrf_id, int holds_lock)
5914
63.8k
{
5915
63.8k
  struct sctp_ifa *sctp_ifap;
5916
63.8k
  struct sctp_vrf *vrf;
5917
63.8k
  struct sctp_ifalist *hash_head;
5918
63.8k
  uint32_t hash_of_addr;
5919
5920
63.8k
  if (holds_lock == 0) {
5921
63.8k
    SCTP_IPI_ADDR_RLOCK();
5922
63.8k
  } else {
5923
3
    SCTP_IPI_ADDR_LOCK_ASSERT();
5924
3
  }
5925
5926
63.8k
  vrf = sctp_find_vrf(vrf_id);
5927
63.8k
  if (vrf == NULL) {
5928
0
    if (holds_lock == 0)
5929
0
      SCTP_IPI_ADDR_RUNLOCK();
5930
0
    return (NULL);
5931
0
  }
5932
5933
63.8k
  hash_of_addr = sctp_get_ifa_hash_val(addr);
5934
5935
63.8k
  hash_head = &vrf->vrf_addr_hash[(hash_of_addr & vrf->vrf_addr_hashmark)];
5936
63.8k
  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
63.8k
  LIST_FOREACH(sctp_ifap, hash_head, next_bucket) {
5948
34.9k
    if (addr->sa_family != sctp_ifap->address.sa.sa_family)
5949
7.43k
      continue;
5950
27.5k
#ifdef INET
5951
27.5k
    if (addr->sa_family == AF_INET) {
5952
6.28k
      if (((struct sockaddr_in *)addr)->sin_addr.s_addr ==
5953
6.28k
          sctp_ifap->address.sin.sin_addr.s_addr) {
5954
        /* found him. */
5955
0
        break;
5956
0
      }
5957
6.28k
    }
5958
27.5k
#endif
5959
27.5k
#ifdef INET6
5960
27.5k
    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
27.5k
#endif
5968
27.5k
#if defined(__Userspace__)
5969
27.5k
    if (addr->sa_family == AF_CONN) {
5970
21.2k
      if (((struct sockaddr_conn *)addr)->sconn_addr == sctp_ifap->address.sconn.sconn_addr) {
5971
        /* found him. */
5972
21.2k
        break;
5973
21.2k
      }
5974
21.2k
    }
5975
27.5k
#endif
5976
27.5k
  }
5977
63.8k
  if (holds_lock == 0)
5978
63.8k
    SCTP_IPI_ADDR_RUNLOCK();
5979
63.8k
  return (sctp_ifap);
5980
63.8k
}
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
17.6k
{
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
17.6k
  int r_unlocked = 0;
5991
17.6k
  uint32_t dif, rwnd;
5992
17.6k
  struct socket *so = NULL;
5993
5994
17.6k
  if (stcb == NULL)
5995
0
    return;
5996
5997
17.6k
  atomic_add_int(&stcb->asoc.refcnt, 1);
5998
5999
17.6k
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6000
17.6k
      (stcb->asoc.state & (SCTP_STATE_ABOUT_TO_BE_FREED | SCTP_STATE_SHUTDOWN_RECEIVED))) {
6001
    /* Pre-check If we are freeing no update */
6002
11.5k
    goto no_lock;
6003
11.5k
  }
6004
6.14k
  SCTP_INP_INCR_REF(stcb->sctp_ep);
6005
6.14k
  if ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
6006
6.14k
      (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
6007
0
    goto out;
6008
0
  }
6009
6.14k
  so = stcb->sctp_socket;
6010
6.14k
  if (so == NULL) {
6011
0
    goto out;
6012
0
  }
6013
6.14k
  atomic_add_int(&stcb->freed_by_sorcv_sincelast, *freed_so_far);
6014
  /* Have you have freed enough to look */
6015
6.14k
  *freed_so_far = 0;
6016
  /* Yep, its worth a look and the lock overhead */
6017
6018
  /* Figure out what the rwnd would be */
6019
6.14k
  rwnd = sctp_calc_rwnd(stcb, &stcb->asoc);
6020
6.14k
  if (rwnd >= stcb->asoc.my_last_reported_rwnd) {
6021
5.57k
    dif = rwnd - stcb->asoc.my_last_reported_rwnd;
6022
5.57k
  } else {
6023
568
    dif = 0;
6024
568
  }
6025
6.14k
  if (dif >= rwnd_req) {
6026
1.25k
    if (hold_rlock) {
6027
1.25k
      SCTP_INP_READ_UNLOCK(stcb->sctp_ep);
6028
1.25k
      r_unlocked = 1;
6029
1.25k
    }
6030
1.25k
    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.25k
    SCTP_TCB_LOCK(stcb);
6039
1.25k
    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.25k
    SCTP_STAT_INCR(sctps_wu_sacks_sent);
6045
#if defined(__FreeBSD__) && !defined(__Userspace__)
6046
    NET_EPOCH_ENTER(et);
6047
#endif
6048
1.25k
    sctp_send_sack(stcb, SCTP_SO_LOCKED);
6049
6050
1.25k
    sctp_chunk_output(stcb->sctp_ep, stcb,
6051
1.25k
          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.25k
    sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
6057
1.25k
                    SCTP_FROM_SCTPUTIL + SCTP_LOC_6);
6058
1.25k
    SCTP_TCB_UNLOCK(stcb);
6059
4.88k
  } else {
6060
    /* Update how much we have pending */
6061
4.88k
    stcb->freed_by_sorcv_sincelast = dif;
6062
4.88k
  }
6063
6.14k
 out:
6064
6.14k
  if (so && r_unlocked && hold_rlock) {
6065
1.25k
    SCTP_INP_READ_LOCK(stcb->sctp_ep);
6066
1.25k
  }
6067
6068
6.14k
  SCTP_INP_DECR_REF(stcb->sctp_ep);
6069
17.6k
 no_lock:
6070
17.6k
  atomic_subtract_int(&stcb->asoc.refcnt, 1);
6071
17.6k
  return;
6072
6.14k
}
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
534k
{
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
534k
  struct sctp_inpcb *inp = NULL;
6093
534k
  ssize_t my_len = 0;
6094
534k
  ssize_t cp_len = 0;
6095
534k
  int error = 0;
6096
534k
  struct sctp_queued_to_read *control = NULL, *ctl = NULL, *nxt = NULL;
6097
534k
  struct mbuf *m = NULL;
6098
534k
  struct sctp_tcb *stcb = NULL;
6099
534k
  int wakeup_read_socket = 0;
6100
534k
  int freecnt_applied = 0;
6101
534k
  int out_flags = 0, in_flags = 0;
6102
534k
  int block_allowed = 1;
6103
534k
  uint32_t freed_so_far = 0;
6104
534k
  ssize_t copied_so_far = 0;
6105
534k
  int in_eeor_mode = 0;
6106
534k
  int no_rcv_needed = 0;
6107
534k
  uint32_t rwnd_req = 0;
6108
534k
  int hold_sblock = 0;
6109
534k
  int hold_rlock = 0;
6110
534k
  ssize_t slen = 0;
6111
534k
  uint32_t held_length = 0;
6112
#if defined(__FreeBSD__) && !defined(__Userspace__)
6113
  int sockbuf_lock = 0;
6114
#endif
6115
6116
534k
  if (uio == NULL) {
6117
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTPUTIL, EINVAL);
6118
0
    return (EINVAL);
6119
0
  }
6120
6121
534k
  if (msg_flags) {
6122
534k
    in_flags = *msg_flags;
6123
534k
    if (in_flags & MSG_PEEK)
6124
534k
      SCTP_STAT_INCR(sctps_read_peeks);
6125
534k
  } 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
534k
  slen = uio->uio_resid;
6136
534k
#endif
6137
6138
  /* Pull in and set up our int flags */
6139
534k
  if (in_flags & MSG_OOB) {
6140
    /* Out of band's NOT supported */
6141
0
    return (EOPNOTSUPP);
6142
0
  }
6143
534k
  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
534k
  if ((in_flags & (MSG_DONTWAIT
6148
#if defined(__FreeBSD__) && !defined(__Userspace__)
6149
       | MSG_NBIO
6150
#endif
6151
534k
         )) ||
6152
534k
      SCTP_SO_IS_NBIO(so)) {
6153
534k
    block_allowed = 0;
6154
534k
  }
6155
  /* setup the endpoint */
6156
534k
  inp = (struct sctp_inpcb *)so->so_pcb;
6157
534k
  if (inp == NULL) {
6158
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTPUTIL, EFAULT);
6159
0
    return (EFAULT);
6160
0
  }
6161
534k
  rwnd_req = (SCTP_SB_LIMIT_RCV(so) >> SCTP_RWND_HIWAT_SHIFT);
6162
  /* Must be at least a MTU's worth */
6163
534k
  if (rwnd_req < SCTP_MIN_RWND)
6164
0
    rwnd_req = SCTP_MIN_RWND;
6165
534k
  in_eeor_mode = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
6166
534k
  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
534k
#if defined(__Userspace__)
6181
534k
  SOCKBUF_LOCK(&so->so_rcv);
6182
534k
  hold_sblock = 1;
6183
534k
#endif
6184
534k
  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
534k
  if (error) {
6206
0
    goto release_unlocked;
6207
0
  }
6208
#if defined(__FreeBSD__) && !defined(__Userspace__)
6209
  sockbuf_lock = 1;
6210
#endif
6211
534k
 restart:
6212
534k
#if defined(__Userspace__)
6213
534k
  if (hold_sblock == 0) {
6214
0
    SOCKBUF_LOCK(&so->so_rcv);
6215
0
    hold_sblock = 1;
6216
0
  }
6217
534k
#endif
6218
#if defined(__APPLE__) && !defined(__Userspace__)
6219
  sbunlock(&so->so_rcv, 1);
6220
#endif
6221
6222
534k
 restart_nosblocks:
6223
534k
  if (hold_sblock == 0) {
6224
0
    SOCKBUF_LOCK(&so->so_rcv);
6225
0
    hold_sblock = 1;
6226
0
  }
6227
534k
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
6228
534k
      (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
534k
  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
534k
  if (SCTP_SBAVAIL(&so->so_rcv) <= held_length) {
6250
1.70k
    if (so->so_error) {
6251
1.69k
      error = so->so_error;
6252
1.69k
      if ((in_flags & MSG_PEEK) == 0) {
6253
1.69k
        so->so_error = 0;
6254
1.69k
      }
6255
1.69k
      goto out;
6256
1.69k
    }
6257
1
    if ((SCTP_SBAVAIL(&so->so_rcv) == 0) &&
6258
1
        ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
6259
1
         (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
6260
1
      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
1
        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
1
        so->so_state &= ~(SS_ISCONNECTING |
6270
1
              SS_ISDISCONNECTING |
6271
1
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
6272
1
              SS_ISCONFIRMING |
6273
1
#endif
6274
1
              SS_ISCONNECTED);
6275
1
        if (error == 0) {
6276
1
          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
1
        }
6281
1
        goto out;
6282
1
      }
6283
1
    }
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
532k
  if (hold_sblock == 1) {
6302
532k
    SOCKBUF_UNLOCK(&so->so_rcv);
6303
532k
    hold_sblock = 0;
6304
532k
  }
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
532k
  control = TAILQ_FIRST(&inp->read_queue);
6311
532k
  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
532k
  if ((control->length == 0) &&
6332
532k
      (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
532k
  if ((control->length == 0) &&
6370
532k
      (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
532k
  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
532k
  control->held_length = 0;
6456
532k
 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
532k
  if (hold_rlock == 0) {
6462
532k
    hold_rlock = 1;
6463
532k
    SCTP_INP_READ_LOCK(inp);
6464
532k
  }
6465
532k
  control->some_taken++;
6466
532k
  stcb = control->stcb;
6467
532k
  if (stcb) {
6468
532k
    if ((control->do_not_ref_stcb == 0) &&
6469
532k
        (stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED)) {
6470
0
      if (freecnt_applied == 0)
6471
0
        stcb = NULL;
6472
532k
    } 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
390k
      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
390k
      } else {
6487
390k
        atomic_add_int(&stcb->asoc.refcnt, 1);
6488
390k
        freecnt_applied = 1;
6489
390k
      }
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
390k
      freed_so_far = (uint32_t)stcb->freed_by_sorcv_sincelast;
6500
390k
      stcb->freed_by_sorcv_sincelast = 0;
6501
390k
    }
6502
532k
  }
6503
532k
  if (stcb &&
6504
532k
      ((control->spec_flags & M_NOTIFICATION) == 0) &&
6505
532k
      control->do_not_ref_stcb == 0) {
6506
827
    stcb->asoc.strmin[control->sinfo_stream].delivery_started = 1;
6507
827
  }
6508
6509
  /* First lets get off the sinfo and sockaddr info */
6510
532k
  if ((sinfo != NULL) && (filling_sinfo != 0)) {
6511
532k
    sinfo->sinfo_stream = control->sinfo_stream;
6512
532k
    sinfo->sinfo_ssn = (uint16_t)control->mid;
6513
532k
    sinfo->sinfo_flags = control->sinfo_flags;
6514
532k
    sinfo->sinfo_ppid = control->sinfo_ppid;
6515
532k
    sinfo->sinfo_context =control->sinfo_context;
6516
532k
    sinfo->sinfo_timetolive = control->sinfo_timetolive;
6517
532k
    sinfo->sinfo_tsn = control->sinfo_tsn;
6518
532k
    sinfo->sinfo_cumtsn = control->sinfo_cumtsn;
6519
532k
    sinfo->sinfo_assoc_id = control->sinfo_assoc_id;
6520
532k
    nxt = TAILQ_NEXT(control, next);
6521
532k
    if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
6522
532k
        sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO)) {
6523
532k
      struct sctp_extrcvinfo *s_extra;
6524
532k
      s_extra = (struct sctp_extrcvinfo *)sinfo;
6525
532k
      if ((nxt) &&
6526
532k
          (nxt->length)) {
6527
523k
        s_extra->serinfo_next_flags = SCTP_NEXT_MSG_AVAIL;
6528
523k
        if (nxt->sinfo_flags & SCTP_UNORDERED) {
6529
472
          s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_UNORDERED;
6530
472
        }
6531
523k
        if (nxt->spec_flags & M_NOTIFICATION) {
6532
522k
          s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_IS_NOTIFICATION;
6533
522k
        }
6534
523k
        s_extra->serinfo_next_aid = nxt->sinfo_assoc_id;
6535
523k
        s_extra->serinfo_next_length = nxt->length;
6536
523k
        s_extra->serinfo_next_ppid = nxt->sinfo_ppid;
6537
523k
        s_extra->serinfo_next_stream = nxt->sinfo_stream;
6538
523k
        if (nxt->tail_mbuf != NULL) {
6539
523k
          if (nxt->end_added) {
6540
523k
            s_extra->serinfo_next_flags |= SCTP_NEXT_MSG_ISCOMPLETE;
6541
523k
          }
6542
523k
        }
6543
523k
      } 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
9.33k
        nxt = NULL;
6549
9.33k
        s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
6550
9.33k
        s_extra->serinfo_next_aid = 0;
6551
9.33k
        s_extra->serinfo_next_length = 0;
6552
9.33k
        s_extra->serinfo_next_ppid = 0;
6553
9.33k
        s_extra->serinfo_next_stream = 0;
6554
9.33k
      }
6555
532k
    }
6556
    /*
6557
     * update off the real current cum-ack, if we have an stcb.
6558
     */
6559
532k
    if ((control->do_not_ref_stcb == 0) && stcb)
6560
390k
      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
532k
    sinfo->sinfo_flags &= 0x00ff;
6566
532k
    if ((control->sinfo_flags >> 8) & SCTP_DATA_UNORDERED) {
6567
784
      sinfo->sinfo_flags |= SCTP_UNORDERED;
6568
784
    }
6569
532k
  }
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
532k
  if ((fromlen > 0) && (from != NULL)) {
6590
532k
    union sctp_sockstore store;
6591
532k
    size_t len;
6592
6593
532k
    switch (control->whoFrom->ro._l_addr.sa.sa_family) {
6594
0
#ifdef INET6
6595
8.52k
      case AF_INET6:
6596
8.52k
        len = sizeof(struct sockaddr_in6);
6597
8.52k
        store.sin6 = control->whoFrom->ro._l_addr.sin6;
6598
8.52k
        store.sin6.sin6_port = control->port_from;
6599
8.52k
        break;
6600
0
#endif
6601
0
#ifdef INET
6602
297
      case AF_INET:
6603
297
#ifdef INET6
6604
297
        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
297
        } else {
6610
297
          len = sizeof(struct sockaddr_in);
6611
297
          store.sin = control->whoFrom->ro._l_addr.sin;
6612
297
          store.sin.sin_port = control->port_from;
6613
297
        }
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
297
        break;
6620
0
#endif
6621
0
#if defined(__Userspace__)
6622
524k
      case AF_CONN:
6623
524k
        len = sizeof(struct sockaddr_conn);
6624
524k
        store.sconn = control->whoFrom->ro._l_addr.sconn;
6625
524k
        store.sconn.sconn_port = control->port_from;
6626
524k
        break;
6627
0
#endif
6628
0
      default:
6629
0
        len = 0;
6630
0
        break;
6631
532k
    }
6632
532k
    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
532k
  }
6644
532k
  if (hold_rlock) {
6645
532k
    SCTP_INP_READ_UNLOCK(inp);
6646
532k
    hold_rlock = 0;
6647
532k
  }
6648
532k
  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
532k
  if (mp == NULL) {
6654
    /* copy out each mbuf in the chain up to length */
6655
532k
  get_more_data:
6656
532k
    m = control->data;
6657
534k
    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
534k
      cp_len = uio->uio_resid;
6667
534k
#endif
6668
534k
      my_len = SCTP_BUF_LEN(m);
6669
534k
      if (cp_len > my_len) {
6670
        /* not enough in this buf */
6671
534k
        cp_len = my_len;
6672
534k
      }
6673
534k
      if (hold_rlock) {
6674
1.85k
        SCTP_INP_READ_UNLOCK(inp);
6675
1.85k
        hold_rlock = 0;
6676
1.85k
      }
6677
#if defined(__APPLE__) && !defined(__Userspace__)
6678
      SCTP_SOCKET_UNLOCK(so, 0);
6679
#endif
6680
534k
      if (cp_len > 0)
6681
534k
        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
534k
      if (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) {
6687
0
        goto release;
6688
0
      }
6689
6690
534k
      if ((control->do_not_ref_stcb == 0) && stcb &&
6691
534k
          stcb->asoc.state & SCTP_STATE_ABOUT_TO_BE_FREED) {
6692
0
        no_rcv_needed = 1;
6693
0
      }
6694
534k
      if (error) {
6695
        /* error we are out of here */
6696
0
        goto release;
6697
0
      }
6698
534k
      SCTP_INP_READ_LOCK(inp);
6699
534k
      hold_rlock = 1;
6700
534k
      if (cp_len == SCTP_BUF_LEN(m)) {
6701
534k
        if ((SCTP_BUF_NEXT(m)== NULL) &&
6702
534k
            (control->end_added)) {
6703
532k
          out_flags |= MSG_EOR;
6704
532k
          if ((control->do_not_ref_stcb == 0) &&
6705
532k
              (control->stcb != NULL) &&
6706
532k
              ((control->spec_flags & M_NOTIFICATION) == 0))
6707
716
            control->stcb->asoc.strmin[control->sinfo_stream].delivery_started = 0;
6708
532k
        }
6709
534k
        if (control->spec_flags & M_NOTIFICATION) {
6710
532k
          out_flags |= MSG_NOTIFICATION;
6711
532k
        }
6712
        /* we ate up the mbuf */
6713
534k
        if (in_flags & MSG_PEEK) {
6714
          /* just looking */
6715
0
          m = SCTP_BUF_NEXT(m);
6716
0
          copied_so_far += cp_len;
6717
534k
        } else {
6718
          /* dispose of the mbuf */
6719
534k
          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.06M
          sctp_sbfree(control, stcb, &so->so_rcv, m);
6724
534k
          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
534k
          copied_so_far += cp_len;
6729
534k
          freed_so_far += (uint32_t)cp_len;
6730
534k
          freed_so_far += MSIZE;
6731
534k
          atomic_subtract_int(&control->length, (int)cp_len);
6732
534k
          control->data = sctp_m_free(m);
6733
534k
          m = control->data;
6734
          /* been through it all, must hold sb lock ok to null tail */
6735
534k
          if (control->data == NULL) {
6736
532k
#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
532k
#endif
6749
532k
            control->tail_mbuf = NULL;
6750
532k
#ifdef INVARIANTS
6751
532k
            if ((control->end_added) && ((out_flags & MSG_EOR) == 0)) {
6752
0
              panic("end_added, nothing left and no MSG_EOR");
6753
0
            }
6754
532k
#endif
6755
532k
          }
6756
534k
        }
6757
534k
      } else {
6758
        /* Do we need to trim the mbuf? */
6759
204
        if (control->spec_flags & M_NOTIFICATION) {
6760
0
          out_flags |= MSG_NOTIFICATION;
6761
0
        }
6762
204
        if ((in_flags & MSG_PEEK) == 0) {
6763
204
          SCTP_BUF_RESV_UF(m, cp_len);
6764
204
          SCTP_BUF_LEN(m) -= (int)cp_len;
6765
204
          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
204
          SCTP_SB_DECR(&so->so_rcv, cp_len);
6769
204
          if ((control->do_not_ref_stcb == 0) &&
6770
204
              stcb) {
6771
111
            atomic_subtract_int(&stcb->asoc.sb_cc, (int)cp_len);
6772
111
          }
6773
204
          copied_so_far += cp_len;
6774
204
          freed_so_far += (uint32_t)cp_len;
6775
204
          freed_so_far += MSIZE;
6776
204
          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
204
          atomic_subtract_int(&control->length, (int)cp_len);
6781
204
        } else {
6782
0
          copied_so_far += cp_len;
6783
0
        }
6784
204
      }
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
534k
      if ((out_flags & MSG_EOR) || (uio->uio_resid == 0)) {
6793
532k
#endif
6794
532k
        break;
6795
532k
      }
6796
1.85k
      if (((stcb) && (in_flags & MSG_PEEK) == 0) &&
6797
1.85k
          (control->do_not_ref_stcb == 0) &&
6798
1.85k
          (freed_so_far >= rwnd_req)) {
6799
27
        sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6800
27
      }
6801
1.85k
    } /* 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
532k
    if ((out_flags & MSG_EOR) && ((in_flags & MSG_PEEK) == 0)) {
6808
      /* we are done with this control */
6809
532k
      if (control->length == 0) {
6810
532k
        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
532k
      done_with_control:
6820
532k
        if (hold_rlock == 0) {
6821
0
          SCTP_INP_READ_LOCK(inp);
6822
0
          hold_rlock = 1;
6823
0
        }
6824
532k
        TAILQ_REMOVE(&inp->read_queue, control, next);
6825
        /* Add back any hidden data */
6826
532k
        if (control->held_length) {
6827
0
          held_length = 0;
6828
0
          control->held_length = 0;
6829
0
          wakeup_read_socket = 1;
6830
0
        }
6831
532k
        if (control->aux_data) {
6832
0
          sctp_m_free (control->aux_data);
6833
0
          control->aux_data = NULL;
6834
0
        }
6835
532k
        no_rcv_needed = control->do_not_ref_stcb;
6836
532k
        sctp_free_remote_addr(control->whoFrom);
6837
532k
        control->data = NULL;
6838
532k
#ifdef INVARIANTS
6839
532k
        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
532k
#endif
6844
1.06M
        sctp_free_a_readq(stcb, control);
6845
1.06M
        control = NULL;
6846
1.06M
        if ((freed_so_far >= rwnd_req) &&
6847
532k
            (no_rcv_needed == 0))
6848
17.6k
          sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
6849
6850
1.06M
      } 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
532k
    }
6868
532k
    if (out_flags & MSG_EOR) {
6869
532k
      goto release;
6870
532k
    }
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
204
    if ((uio->uio_resid == 0) ||
6879
204
#endif
6880
204
        ((in_eeor_mode) &&
6881
204
         (copied_so_far >= max(so->so_rcv.sb_lowat, 1)))) {
6882
204
      goto release;
6883
204
    }
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
532k
 release:
7058
532k
  if (hold_rlock == 1) {
7059
532k
    SCTP_INP_READ_UNLOCK(inp);
7060
532k
    hold_rlock = 0;
7061
532k
  }
7062
532k
#if defined(__Userspace__)
7063
532k
  if (hold_sblock == 0) {
7064
532k
    SOCKBUF_LOCK(&so->so_rcv);
7065
532k
    hold_sblock = 1;
7066
532k
  }
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
532k
 release_unlocked:
7083
532k
  if (hold_sblock) {
7084
532k
    SOCKBUF_UNLOCK(&so->so_rcv);
7085
532k
    hold_sblock = 0;
7086
532k
  }
7087
532k
  if ((stcb) && (in_flags & MSG_PEEK) == 0) {
7088
532k
    if ((freed_so_far >= rwnd_req) &&
7089
532k
        (control && (control->do_not_ref_stcb == 0)) &&
7090
532k
        (no_rcv_needed == 0))
7091
7
      sctp_user_rcvd(stcb, &freed_so_far, hold_rlock, rwnd_req);
7092
532k
  }
7093
534k
 out:
7094
534k
  if (msg_flags) {
7095
534k
    *msg_flags = out_flags;
7096
534k
  }
7097
534k
  if (((out_flags & MSG_EOR) == 0) &&
7098
534k
      ((in_flags & MSG_PEEK) == 0) &&
7099
534k
      (sinfo) &&
7100
534k
      (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXT_RCVINFO) ||
7101
1.90k
       sctp_is_feature_on(inp, SCTP_PCB_FLAGS_RECVNXTINFO))) {
7102
1.90k
    struct sctp_extrcvinfo *s_extra;
7103
1.90k
    s_extra = (struct sctp_extrcvinfo *)sinfo;
7104
1.90k
    s_extra->serinfo_next_flags = SCTP_NO_NEXT_MSG;
7105
1.90k
  }
7106
534k
  if (hold_rlock == 1) {
7107
0
    SCTP_INP_READ_UNLOCK(inp);
7108
0
  }
7109
534k
  if (hold_sblock) {
7110
1.70k
    SOCKBUF_UNLOCK(&so->so_rcv);
7111
1.70k
  }
7112
#if defined(__FreeBSD__) && !defined(__Userspace__)
7113
  if (sockbuf_lock) {
7114
    SOCK_IO_RECV_UNLOCK(so);
7115
  }
7116
#endif
7117
7118
534k
  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
390k
    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
390k
    stcb->freed_by_sorcv_sincelast = freed_so_far;
7135
390k
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
7136
390k
  }
7137
534k
  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
534k
 stage_left:
7169
534k
  if (wakeup_read_socket) {
7170
0
    sctp_sorwakeup(inp, so);
7171
0
  }
7172
534k
  return (error);
7173
534k
}
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.2k
{
7389
21.2k
  long hashsize;
7390
21.2k
  LIST_HEAD(generic, generic) *hashtbl;
7391
21.2k
  int i;
7392
7393
21.2k
  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
180k
  for (hashsize = 1; hashsize <= elements; hashsize <<= 1)
7402
159k
    continue;
7403
21.2k
  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.2k
  if (flags & HASH_WAITOK)
7407
0
    hashtbl = malloc((u_long)hashsize * sizeof(*hashtbl));
7408
21.2k
  else if (flags & HASH_NOWAIT)
7409
21.2k
    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.2k
  if (hashtbl == NULL)
7419
0
    return (NULL);
7420
7421
3.10M
  for (i = 0; i < hashsize; i++)
7422
3.08M
    LIST_INIT(&hashtbl[i]);
7423
21.2k
  *hashmask = hashsize - 1;
7424
21.2k
  return (hashtbl);
7425
21.2k
}
7426
7427
void
7428
sctp_hashdestroy(void *vhashtbl, struct malloc_type *type, u_long hashmask)
7429
21.2k
{
7430
21.2k
  LIST_HEAD(generic, generic) *hashtbl, *hp;
7431
7432
21.2k
  hashtbl = vhashtbl;
7433
3.08M
  for (hp = hashtbl; hp <= &hashtbl[hashmask]; hp++)
7434
3.06M
    if (!LIST_EMPTY(hp)) {
7435
0
      SCTP_PRINTF("hashdestroy: hash not empty.\n");
7436
0
      return;
7437
0
    }
7438
21.2k
  FREE(hashtbl, type);
7439
21.2k
}
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
35.3k
{
8683
#if defined(KDTRACE_HOOKS)
8684
  int old_state = stcb->asoc.state;
8685
#endif
8686
8687
35.3k
  KASSERT((new_state & ~SCTP_STATE_MASK) == 0,
8688
35.3k
          ("sctp_set_state: Can't set substate (new_state = %x)",
8689
35.3k
          new_state));
8690
35.3k
  stcb->asoc.state = (stcb->asoc.state & ~SCTP_STATE_MASK) | new_state;
8691
35.3k
  if ((new_state == SCTP_STATE_SHUTDOWN_RECEIVED) ||
8692
35.3k
      (new_state == SCTP_STATE_SHUTDOWN_SENT) ||
8693
35.3k
      (new_state == SCTP_STATE_SHUTDOWN_ACK_SENT)) {
8694
131
    SCTP_CLEAR_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
8695
131
  }
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
35.3k
}
8704
8705
void
8706
sctp_add_substate(struct sctp_tcb *stcb, int substate)
8707
21.2k
{
8708
#if defined(KDTRACE_HOOKS)
8709
  int old_state = stcb->asoc.state;
8710
#endif
8711
8712
21.2k
  KASSERT((substate & SCTP_STATE_MASK) == 0,
8713
21.2k
          ("sctp_add_substate: Can't set state (substate = %x)",
8714
21.2k
          substate));
8715
21.2k
  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.2k
}
8725