Coverage Report

Created: 2026-02-14 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/usrsctp/usrsctplib/netinet/sctp_output.c
Line
Count
Source
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
#if defined(__FreeBSD__) && !defined(__Userspace__)
37
#include <sys/proc.h>
38
#endif
39
#include <netinet/sctp_var.h>
40
#include <netinet/sctp_sysctl.h>
41
#include <netinet/sctp_header.h>
42
#include <netinet/sctp_pcb.h>
43
#include <netinet/sctputil.h>
44
#include <netinet/sctp_output.h>
45
#include <netinet/sctp_uio.h>
46
#include <netinet/sctputil.h>
47
#include <netinet/sctp_auth.h>
48
#include <netinet/sctp_timer.h>
49
#include <netinet/sctp_asconf.h>
50
#include <netinet/sctp_indata.h>
51
#include <netinet/sctp_bsd_addr.h>
52
#include <netinet/sctp_input.h>
53
#include <netinet/sctp_crc32.h>
54
#if defined(__FreeBSD__) && !defined(__Userspace__)
55
#include <netinet/sctp_kdtrace.h>
56
#endif
57
#if defined(__linux__)
58
#define __FAVOR_BSD    /* (on Ubuntu at least) enables UDP header field names like BSD in RFC 768 */
59
#endif
60
#if defined(INET) || defined(INET6)
61
#if !defined(_WIN32)
62
#include <netinet/udp.h>
63
#endif
64
#endif
65
#if !defined(__Userspace__)
66
#if defined(__APPLE__)
67
#include <netinet/in.h>
68
#endif
69
#if defined(__FreeBSD__) && !defined(__Userspace__)
70
#include <netinet/udp_var.h>
71
#include <machine/in_cksum.h>
72
#endif
73
#endif
74
#if defined(__Userspace__) && defined(INET6)
75
#include <netinet6/sctp6_var.h>
76
#endif
77
#if defined(_WIN32) && defined(__MINGW32__)
78
#include <minmax.h>
79
#endif
80
#if defined(__APPLE__) && !defined(__Userspace__)
81
#if !(defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD))
82
#define SCTP_MAX_LINKHDR 16
83
#endif
84
#endif
85
86
#define SCTP_MAX_GAPS_INARRAY 4
87
struct sack_track {
88
  uint8_t right_edge; /* mergable on the right edge */
89
  uint8_t left_edge;  /* mergable on the left edge */
90
  uint8_t num_entries;
91
  uint8_t spare;
92
  struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
93
};
94
95
const struct sack_track sack_array[256] = {
96
  {0, 0, 0, 0,    /* 0x00 */
97
    {{0, 0},
98
    {0, 0},
99
    {0, 0},
100
    {0, 0}
101
    }
102
  },
103
  {1, 0, 1, 0,    /* 0x01 */
104
    {{0, 0},
105
    {0, 0},
106
    {0, 0},
107
    {0, 0}
108
    }
109
  },
110
  {0, 0, 1, 0,    /* 0x02 */
111
    {{1, 1},
112
    {0, 0},
113
    {0, 0},
114
    {0, 0}
115
    }
116
  },
117
  {1, 0, 1, 0,    /* 0x03 */
118
    {{0, 1},
119
    {0, 0},
120
    {0, 0},
121
    {0, 0}
122
    }
123
  },
124
  {0, 0, 1, 0,    /* 0x04 */
125
    {{2, 2},
126
    {0, 0},
127
    {0, 0},
128
    {0, 0}
129
    }
130
  },
131
  {1, 0, 2, 0,    /* 0x05 */
132
    {{0, 0},
133
    {2, 2},
134
    {0, 0},
135
    {0, 0}
136
    }
137
  },
138
  {0, 0, 1, 0,    /* 0x06 */
139
    {{1, 2},
140
    {0, 0},
141
    {0, 0},
142
    {0, 0}
143
    }
144
  },
145
  {1, 0, 1, 0,    /* 0x07 */
146
    {{0, 2},
147
    {0, 0},
148
    {0, 0},
149
    {0, 0}
150
    }
151
  },
152
  {0, 0, 1, 0,    /* 0x08 */
153
    {{3, 3},
154
    {0, 0},
155
    {0, 0},
156
    {0, 0}
157
    }
158
  },
159
  {1, 0, 2, 0,    /* 0x09 */
160
    {{0, 0},
161
    {3, 3},
162
    {0, 0},
163
    {0, 0}
164
    }
165
  },
166
  {0, 0, 2, 0,    /* 0x0a */
167
    {{1, 1},
168
    {3, 3},
169
    {0, 0},
170
    {0, 0}
171
    }
172
  },
173
  {1, 0, 2, 0,    /* 0x0b */
174
    {{0, 1},
175
    {3, 3},
176
    {0, 0},
177
    {0, 0}
178
    }
179
  },
180
  {0, 0, 1, 0,    /* 0x0c */
181
    {{2, 3},
182
    {0, 0},
183
    {0, 0},
184
    {0, 0}
185
    }
186
  },
187
  {1, 0, 2, 0,    /* 0x0d */
188
    {{0, 0},
189
    {2, 3},
190
    {0, 0},
191
    {0, 0}
192
    }
193
  },
194
  {0, 0, 1, 0,    /* 0x0e */
195
    {{1, 3},
196
    {0, 0},
197
    {0, 0},
198
    {0, 0}
199
    }
200
  },
201
  {1, 0, 1, 0,    /* 0x0f */
202
    {{0, 3},
203
    {0, 0},
204
    {0, 0},
205
    {0, 0}
206
    }
207
  },
208
  {0, 0, 1, 0,    /* 0x10 */
209
    {{4, 4},
210
    {0, 0},
211
    {0, 0},
212
    {0, 0}
213
    }
214
  },
215
  {1, 0, 2, 0,    /* 0x11 */
216
    {{0, 0},
217
    {4, 4},
218
    {0, 0},
219
    {0, 0}
220
    }
221
  },
222
  {0, 0, 2, 0,    /* 0x12 */
223
    {{1, 1},
224
    {4, 4},
225
    {0, 0},
226
    {0, 0}
227
    }
228
  },
229
  {1, 0, 2, 0,    /* 0x13 */
230
    {{0, 1},
231
    {4, 4},
232
    {0, 0},
233
    {0, 0}
234
    }
235
  },
236
  {0, 0, 2, 0,    /* 0x14 */
237
    {{2, 2},
238
    {4, 4},
239
    {0, 0},
240
    {0, 0}
241
    }
242
  },
243
  {1, 0, 3, 0,    /* 0x15 */
244
    {{0, 0},
245
    {2, 2},
246
    {4, 4},
247
    {0, 0}
248
    }
249
  },
250
  {0, 0, 2, 0,    /* 0x16 */
251
    {{1, 2},
252
    {4, 4},
253
    {0, 0},
254
    {0, 0}
255
    }
256
  },
257
  {1, 0, 2, 0,    /* 0x17 */
258
    {{0, 2},
259
    {4, 4},
260
    {0, 0},
261
    {0, 0}
262
    }
263
  },
264
  {0, 0, 1, 0,    /* 0x18 */
265
    {{3, 4},
266
    {0, 0},
267
    {0, 0},
268
    {0, 0}
269
    }
270
  },
271
  {1, 0, 2, 0,    /* 0x19 */
272
    {{0, 0},
273
    {3, 4},
274
    {0, 0},
275
    {0, 0}
276
    }
277
  },
278
  {0, 0, 2, 0,    /* 0x1a */
279
    {{1, 1},
280
    {3, 4},
281
    {0, 0},
282
    {0, 0}
283
    }
284
  },
285
  {1, 0, 2, 0,    /* 0x1b */
286
    {{0, 1},
287
    {3, 4},
288
    {0, 0},
289
    {0, 0}
290
    }
291
  },
292
  {0, 0, 1, 0,    /* 0x1c */
293
    {{2, 4},
294
    {0, 0},
295
    {0, 0},
296
    {0, 0}
297
    }
298
  },
299
  {1, 0, 2, 0,    /* 0x1d */
300
    {{0, 0},
301
    {2, 4},
302
    {0, 0},
303
    {0, 0}
304
    }
305
  },
306
  {0, 0, 1, 0,    /* 0x1e */
307
    {{1, 4},
308
    {0, 0},
309
    {0, 0},
310
    {0, 0}
311
    }
312
  },
313
  {1, 0, 1, 0,    /* 0x1f */
314
    {{0, 4},
315
    {0, 0},
316
    {0, 0},
317
    {0, 0}
318
    }
319
  },
320
  {0, 0, 1, 0,    /* 0x20 */
321
    {{5, 5},
322
    {0, 0},
323
    {0, 0},
324
    {0, 0}
325
    }
326
  },
327
  {1, 0, 2, 0,    /* 0x21 */
328
    {{0, 0},
329
    {5, 5},
330
    {0, 0},
331
    {0, 0}
332
    }
333
  },
334
  {0, 0, 2, 0,    /* 0x22 */
335
    {{1, 1},
336
    {5, 5},
337
    {0, 0},
338
    {0, 0}
339
    }
340
  },
341
  {1, 0, 2, 0,    /* 0x23 */
342
    {{0, 1},
343
    {5, 5},
344
    {0, 0},
345
    {0, 0}
346
    }
347
  },
348
  {0, 0, 2, 0,    /* 0x24 */
349
    {{2, 2},
350
    {5, 5},
351
    {0, 0},
352
    {0, 0}
353
    }
354
  },
355
  {1, 0, 3, 0,    /* 0x25 */
356
    {{0, 0},
357
    {2, 2},
358
    {5, 5},
359
    {0, 0}
360
    }
361
  },
362
  {0, 0, 2, 0,    /* 0x26 */
363
    {{1, 2},
364
    {5, 5},
365
    {0, 0},
366
    {0, 0}
367
    }
368
  },
369
  {1, 0, 2, 0,    /* 0x27 */
370
    {{0, 2},
371
    {5, 5},
372
    {0, 0},
373
    {0, 0}
374
    }
375
  },
376
  {0, 0, 2, 0,    /* 0x28 */
377
    {{3, 3},
378
    {5, 5},
379
    {0, 0},
380
    {0, 0}
381
    }
382
  },
383
  {1, 0, 3, 0,    /* 0x29 */
384
    {{0, 0},
385
    {3, 3},
386
    {5, 5},
387
    {0, 0}
388
    }
389
  },
390
  {0, 0, 3, 0,    /* 0x2a */
391
    {{1, 1},
392
    {3, 3},
393
    {5, 5},
394
    {0, 0}
395
    }
396
  },
397
  {1, 0, 3, 0,    /* 0x2b */
398
    {{0, 1},
399
    {3, 3},
400
    {5, 5},
401
    {0, 0}
402
    }
403
  },
404
  {0, 0, 2, 0,    /* 0x2c */
405
    {{2, 3},
406
    {5, 5},
407
    {0, 0},
408
    {0, 0}
409
    }
410
  },
411
  {1, 0, 3, 0,    /* 0x2d */
412
    {{0, 0},
413
    {2, 3},
414
    {5, 5},
415
    {0, 0}
416
    }
417
  },
418
  {0, 0, 2, 0,    /* 0x2e */
419
    {{1, 3},
420
    {5, 5},
421
    {0, 0},
422
    {0, 0}
423
    }
424
  },
425
  {1, 0, 2, 0,    /* 0x2f */
426
    {{0, 3},
427
    {5, 5},
428
    {0, 0},
429
    {0, 0}
430
    }
431
  },
432
  {0, 0, 1, 0,    /* 0x30 */
433
    {{4, 5},
434
    {0, 0},
435
    {0, 0},
436
    {0, 0}
437
    }
438
  },
439
  {1, 0, 2, 0,    /* 0x31 */
440
    {{0, 0},
441
    {4, 5},
442
    {0, 0},
443
    {0, 0}
444
    }
445
  },
446
  {0, 0, 2, 0,    /* 0x32 */
447
    {{1, 1},
448
    {4, 5},
449
    {0, 0},
450
    {0, 0}
451
    }
452
  },
453
  {1, 0, 2, 0,    /* 0x33 */
454
    {{0, 1},
455
    {4, 5},
456
    {0, 0},
457
    {0, 0}
458
    }
459
  },
460
  {0, 0, 2, 0,    /* 0x34 */
461
    {{2, 2},
462
    {4, 5},
463
    {0, 0},
464
    {0, 0}
465
    }
466
  },
467
  {1, 0, 3, 0,    /* 0x35 */
468
    {{0, 0},
469
    {2, 2},
470
    {4, 5},
471
    {0, 0}
472
    }
473
  },
474
  {0, 0, 2, 0,    /* 0x36 */
475
    {{1, 2},
476
    {4, 5},
477
    {0, 0},
478
    {0, 0}
479
    }
480
  },
481
  {1, 0, 2, 0,    /* 0x37 */
482
    {{0, 2},
483
    {4, 5},
484
    {0, 0},
485
    {0, 0}
486
    }
487
  },
488
  {0, 0, 1, 0,    /* 0x38 */
489
    {{3, 5},
490
    {0, 0},
491
    {0, 0},
492
    {0, 0}
493
    }
494
  },
495
  {1, 0, 2, 0,    /* 0x39 */
496
    {{0, 0},
497
    {3, 5},
498
    {0, 0},
499
    {0, 0}
500
    }
501
  },
502
  {0, 0, 2, 0,    /* 0x3a */
503
    {{1, 1},
504
    {3, 5},
505
    {0, 0},
506
    {0, 0}
507
    }
508
  },
509
  {1, 0, 2, 0,    /* 0x3b */
510
    {{0, 1},
511
    {3, 5},
512
    {0, 0},
513
    {0, 0}
514
    }
515
  },
516
  {0, 0, 1, 0,    /* 0x3c */
517
    {{2, 5},
518
    {0, 0},
519
    {0, 0},
520
    {0, 0}
521
    }
522
  },
523
  {1, 0, 2, 0,    /* 0x3d */
524
    {{0, 0},
525
    {2, 5},
526
    {0, 0},
527
    {0, 0}
528
    }
529
  },
530
  {0, 0, 1, 0,    /* 0x3e */
531
    {{1, 5},
532
    {0, 0},
533
    {0, 0},
534
    {0, 0}
535
    }
536
  },
537
  {1, 0, 1, 0,    /* 0x3f */
538
    {{0, 5},
539
    {0, 0},
540
    {0, 0},
541
    {0, 0}
542
    }
543
  },
544
  {0, 0, 1, 0,    /* 0x40 */
545
    {{6, 6},
546
    {0, 0},
547
    {0, 0},
548
    {0, 0}
549
    }
550
  },
551
  {1, 0, 2, 0,    /* 0x41 */
552
    {{0, 0},
553
    {6, 6},
554
    {0, 0},
555
    {0, 0}
556
    }
557
  },
558
  {0, 0, 2, 0,    /* 0x42 */
559
    {{1, 1},
560
    {6, 6},
561
    {0, 0},
562
    {0, 0}
563
    }
564
  },
565
  {1, 0, 2, 0,    /* 0x43 */
566
    {{0, 1},
567
    {6, 6},
568
    {0, 0},
569
    {0, 0}
570
    }
571
  },
572
  {0, 0, 2, 0,    /* 0x44 */
573
    {{2, 2},
574
    {6, 6},
575
    {0, 0},
576
    {0, 0}
577
    }
578
  },
579
  {1, 0, 3, 0,    /* 0x45 */
580
    {{0, 0},
581
    {2, 2},
582
    {6, 6},
583
    {0, 0}
584
    }
585
  },
586
  {0, 0, 2, 0,    /* 0x46 */
587
    {{1, 2},
588
    {6, 6},
589
    {0, 0},
590
    {0, 0}
591
    }
592
  },
593
  {1, 0, 2, 0,    /* 0x47 */
594
    {{0, 2},
595
    {6, 6},
596
    {0, 0},
597
    {0, 0}
598
    }
599
  },
600
  {0, 0, 2, 0,    /* 0x48 */
601
    {{3, 3},
602
    {6, 6},
603
    {0, 0},
604
    {0, 0}
605
    }
606
  },
607
  {1, 0, 3, 0,    /* 0x49 */
608
    {{0, 0},
609
    {3, 3},
610
    {6, 6},
611
    {0, 0}
612
    }
613
  },
614
  {0, 0, 3, 0,    /* 0x4a */
615
    {{1, 1},
616
    {3, 3},
617
    {6, 6},
618
    {0, 0}
619
    }
620
  },
621
  {1, 0, 3, 0,    /* 0x4b */
622
    {{0, 1},
623
    {3, 3},
624
    {6, 6},
625
    {0, 0}
626
    }
627
  },
628
  {0, 0, 2, 0,    /* 0x4c */
629
    {{2, 3},
630
    {6, 6},
631
    {0, 0},
632
    {0, 0}
633
    }
634
  },
635
  {1, 0, 3, 0,    /* 0x4d */
636
    {{0, 0},
637
    {2, 3},
638
    {6, 6},
639
    {0, 0}
640
    }
641
  },
642
  {0, 0, 2, 0,    /* 0x4e */
643
    {{1, 3},
644
    {6, 6},
645
    {0, 0},
646
    {0, 0}
647
    }
648
  },
649
  {1, 0, 2, 0,    /* 0x4f */
650
    {{0, 3},
651
    {6, 6},
652
    {0, 0},
653
    {0, 0}
654
    }
655
  },
656
  {0, 0, 2, 0,    /* 0x50 */
657
    {{4, 4},
658
    {6, 6},
659
    {0, 0},
660
    {0, 0}
661
    }
662
  },
663
  {1, 0, 3, 0,    /* 0x51 */
664
    {{0, 0},
665
    {4, 4},
666
    {6, 6},
667
    {0, 0}
668
    }
669
  },
670
  {0, 0, 3, 0,    /* 0x52 */
671
    {{1, 1},
672
    {4, 4},
673
    {6, 6},
674
    {0, 0}
675
    }
676
  },
677
  {1, 0, 3, 0,    /* 0x53 */
678
    {{0, 1},
679
    {4, 4},
680
    {6, 6},
681
    {0, 0}
682
    }
683
  },
684
  {0, 0, 3, 0,    /* 0x54 */
685
    {{2, 2},
686
    {4, 4},
687
    {6, 6},
688
    {0, 0}
689
    }
690
  },
691
  {1, 0, 4, 0,    /* 0x55 */
692
    {{0, 0},
693
    {2, 2},
694
    {4, 4},
695
    {6, 6}
696
    }
697
  },
698
  {0, 0, 3, 0,    /* 0x56 */
699
    {{1, 2},
700
    {4, 4},
701
    {6, 6},
702
    {0, 0}
703
    }
704
  },
705
  {1, 0, 3, 0,    /* 0x57 */
706
    {{0, 2},
707
    {4, 4},
708
    {6, 6},
709
    {0, 0}
710
    }
711
  },
712
  {0, 0, 2, 0,    /* 0x58 */
713
    {{3, 4},
714
    {6, 6},
715
    {0, 0},
716
    {0, 0}
717
    }
718
  },
719
  {1, 0, 3, 0,    /* 0x59 */
720
    {{0, 0},
721
    {3, 4},
722
    {6, 6},
723
    {0, 0}
724
    }
725
  },
726
  {0, 0, 3, 0,    /* 0x5a */
727
    {{1, 1},
728
    {3, 4},
729
    {6, 6},
730
    {0, 0}
731
    }
732
  },
733
  {1, 0, 3, 0,    /* 0x5b */
734
    {{0, 1},
735
    {3, 4},
736
    {6, 6},
737
    {0, 0}
738
    }
739
  },
740
  {0, 0, 2, 0,    /* 0x5c */
741
    {{2, 4},
742
    {6, 6},
743
    {0, 0},
744
    {0, 0}
745
    }
746
  },
747
  {1, 0, 3, 0,    /* 0x5d */
748
    {{0, 0},
749
    {2, 4},
750
    {6, 6},
751
    {0, 0}
752
    }
753
  },
754
  {0, 0, 2, 0,    /* 0x5e */
755
    {{1, 4},
756
    {6, 6},
757
    {0, 0},
758
    {0, 0}
759
    }
760
  },
761
  {1, 0, 2, 0,    /* 0x5f */
762
    {{0, 4},
763
    {6, 6},
764
    {0, 0},
765
    {0, 0}
766
    }
767
  },
768
  {0, 0, 1, 0,    /* 0x60 */
769
    {{5, 6},
770
    {0, 0},
771
    {0, 0},
772
    {0, 0}
773
    }
774
  },
775
  {1, 0, 2, 0,    /* 0x61 */
776
    {{0, 0},
777
    {5, 6},
778
    {0, 0},
779
    {0, 0}
780
    }
781
  },
782
  {0, 0, 2, 0,    /* 0x62 */
783
    {{1, 1},
784
    {5, 6},
785
    {0, 0},
786
    {0, 0}
787
    }
788
  },
789
  {1, 0, 2, 0,    /* 0x63 */
790
    {{0, 1},
791
    {5, 6},
792
    {0, 0},
793
    {0, 0}
794
    }
795
  },
796
  {0, 0, 2, 0,    /* 0x64 */
797
    {{2, 2},
798
    {5, 6},
799
    {0, 0},
800
    {0, 0}
801
    }
802
  },
803
  {1, 0, 3, 0,    /* 0x65 */
804
    {{0, 0},
805
    {2, 2},
806
    {5, 6},
807
    {0, 0}
808
    }
809
  },
810
  {0, 0, 2, 0,    /* 0x66 */
811
    {{1, 2},
812
    {5, 6},
813
    {0, 0},
814
    {0, 0}
815
    }
816
  },
817
  {1, 0, 2, 0,    /* 0x67 */
818
    {{0, 2},
819
    {5, 6},
820
    {0, 0},
821
    {0, 0}
822
    }
823
  },
824
  {0, 0, 2, 0,    /* 0x68 */
825
    {{3, 3},
826
    {5, 6},
827
    {0, 0},
828
    {0, 0}
829
    }
830
  },
831
  {1, 0, 3, 0,    /* 0x69 */
832
    {{0, 0},
833
    {3, 3},
834
    {5, 6},
835
    {0, 0}
836
    }
837
  },
838
  {0, 0, 3, 0,    /* 0x6a */
839
    {{1, 1},
840
    {3, 3},
841
    {5, 6},
842
    {0, 0}
843
    }
844
  },
845
  {1, 0, 3, 0,    /* 0x6b */
846
    {{0, 1},
847
    {3, 3},
848
    {5, 6},
849
    {0, 0}
850
    }
851
  },
852
  {0, 0, 2, 0,    /* 0x6c */
853
    {{2, 3},
854
    {5, 6},
855
    {0, 0},
856
    {0, 0}
857
    }
858
  },
859
  {1, 0, 3, 0,    /* 0x6d */
860
    {{0, 0},
861
    {2, 3},
862
    {5, 6},
863
    {0, 0}
864
    }
865
  },
866
  {0, 0, 2, 0,    /* 0x6e */
867
    {{1, 3},
868
    {5, 6},
869
    {0, 0},
870
    {0, 0}
871
    }
872
  },
873
  {1, 0, 2, 0,    /* 0x6f */
874
    {{0, 3},
875
    {5, 6},
876
    {0, 0},
877
    {0, 0}
878
    }
879
  },
880
  {0, 0, 1, 0,    /* 0x70 */
881
    {{4, 6},
882
    {0, 0},
883
    {0, 0},
884
    {0, 0}
885
    }
886
  },
887
  {1, 0, 2, 0,    /* 0x71 */
888
    {{0, 0},
889
    {4, 6},
890
    {0, 0},
891
    {0, 0}
892
    }
893
  },
894
  {0, 0, 2, 0,    /* 0x72 */
895
    {{1, 1},
896
    {4, 6},
897
    {0, 0},
898
    {0, 0}
899
    }
900
  },
901
  {1, 0, 2, 0,    /* 0x73 */
902
    {{0, 1},
903
    {4, 6},
904
    {0, 0},
905
    {0, 0}
906
    }
907
  },
908
  {0, 0, 2, 0,    /* 0x74 */
909
    {{2, 2},
910
    {4, 6},
911
    {0, 0},
912
    {0, 0}
913
    }
914
  },
915
  {1, 0, 3, 0,    /* 0x75 */
916
    {{0, 0},
917
    {2, 2},
918
    {4, 6},
919
    {0, 0}
920
    }
921
  },
922
  {0, 0, 2, 0,    /* 0x76 */
923
    {{1, 2},
924
    {4, 6},
925
    {0, 0},
926
    {0, 0}
927
    }
928
  },
929
  {1, 0, 2, 0,    /* 0x77 */
930
    {{0, 2},
931
    {4, 6},
932
    {0, 0},
933
    {0, 0}
934
    }
935
  },
936
  {0, 0, 1, 0,    /* 0x78 */
937
    {{3, 6},
938
    {0, 0},
939
    {0, 0},
940
    {0, 0}
941
    }
942
  },
943
  {1, 0, 2, 0,    /* 0x79 */
944
    {{0, 0},
945
    {3, 6},
946
    {0, 0},
947
    {0, 0}
948
    }
949
  },
950
  {0, 0, 2, 0,    /* 0x7a */
951
    {{1, 1},
952
    {3, 6},
953
    {0, 0},
954
    {0, 0}
955
    }
956
  },
957
  {1, 0, 2, 0,    /* 0x7b */
958
    {{0, 1},
959
    {3, 6},
960
    {0, 0},
961
    {0, 0}
962
    }
963
  },
964
  {0, 0, 1, 0,    /* 0x7c */
965
    {{2, 6},
966
    {0, 0},
967
    {0, 0},
968
    {0, 0}
969
    }
970
  },
971
  {1, 0, 2, 0,    /* 0x7d */
972
    {{0, 0},
973
    {2, 6},
974
    {0, 0},
975
    {0, 0}
976
    }
977
  },
978
  {0, 0, 1, 0,    /* 0x7e */
979
    {{1, 6},
980
    {0, 0},
981
    {0, 0},
982
    {0, 0}
983
    }
984
  },
985
  {1, 0, 1, 0,    /* 0x7f */
986
    {{0, 6},
987
    {0, 0},
988
    {0, 0},
989
    {0, 0}
990
    }
991
  },
992
  {0, 1, 1, 0,    /* 0x80 */
993
    {{7, 7},
994
    {0, 0},
995
    {0, 0},
996
    {0, 0}
997
    }
998
  },
999
  {1, 1, 2, 0,    /* 0x81 */
1000
    {{0, 0},
1001
    {7, 7},
1002
    {0, 0},
1003
    {0, 0}
1004
    }
1005
  },
1006
  {0, 1, 2, 0,    /* 0x82 */
1007
    {{1, 1},
1008
    {7, 7},
1009
    {0, 0},
1010
    {0, 0}
1011
    }
1012
  },
1013
  {1, 1, 2, 0,    /* 0x83 */
1014
    {{0, 1},
1015
    {7, 7},
1016
    {0, 0},
1017
    {0, 0}
1018
    }
1019
  },
1020
  {0, 1, 2, 0,    /* 0x84 */
1021
    {{2, 2},
1022
    {7, 7},
1023
    {0, 0},
1024
    {0, 0}
1025
    }
1026
  },
1027
  {1, 1, 3, 0,    /* 0x85 */
1028
    {{0, 0},
1029
    {2, 2},
1030
    {7, 7},
1031
    {0, 0}
1032
    }
1033
  },
1034
  {0, 1, 2, 0,    /* 0x86 */
1035
    {{1, 2},
1036
    {7, 7},
1037
    {0, 0},
1038
    {0, 0}
1039
    }
1040
  },
1041
  {1, 1, 2, 0,    /* 0x87 */
1042
    {{0, 2},
1043
    {7, 7},
1044
    {0, 0},
1045
    {0, 0}
1046
    }
1047
  },
1048
  {0, 1, 2, 0,    /* 0x88 */
1049
    {{3, 3},
1050
    {7, 7},
1051
    {0, 0},
1052
    {0, 0}
1053
    }
1054
  },
1055
  {1, 1, 3, 0,    /* 0x89 */
1056
    {{0, 0},
1057
    {3, 3},
1058
    {7, 7},
1059
    {0, 0}
1060
    }
1061
  },
1062
  {0, 1, 3, 0,    /* 0x8a */
1063
    {{1, 1},
1064
    {3, 3},
1065
    {7, 7},
1066
    {0, 0}
1067
    }
1068
  },
1069
  {1, 1, 3, 0,    /* 0x8b */
1070
    {{0, 1},
1071
    {3, 3},
1072
    {7, 7},
1073
    {0, 0}
1074
    }
1075
  },
1076
  {0, 1, 2, 0,    /* 0x8c */
1077
    {{2, 3},
1078
    {7, 7},
1079
    {0, 0},
1080
    {0, 0}
1081
    }
1082
  },
1083
  {1, 1, 3, 0,    /* 0x8d */
1084
    {{0, 0},
1085
    {2, 3},
1086
    {7, 7},
1087
    {0, 0}
1088
    }
1089
  },
1090
  {0, 1, 2, 0,    /* 0x8e */
1091
    {{1, 3},
1092
    {7, 7},
1093
    {0, 0},
1094
    {0, 0}
1095
    }
1096
  },
1097
  {1, 1, 2, 0,    /* 0x8f */
1098
    {{0, 3},
1099
    {7, 7},
1100
    {0, 0},
1101
    {0, 0}
1102
    }
1103
  },
1104
  {0, 1, 2, 0,    /* 0x90 */
1105
    {{4, 4},
1106
    {7, 7},
1107
    {0, 0},
1108
    {0, 0}
1109
    }
1110
  },
1111
  {1, 1, 3, 0,    /* 0x91 */
1112
    {{0, 0},
1113
    {4, 4},
1114
    {7, 7},
1115
    {0, 0}
1116
    }
1117
  },
1118
  {0, 1, 3, 0,    /* 0x92 */
1119
    {{1, 1},
1120
    {4, 4},
1121
    {7, 7},
1122
    {0, 0}
1123
    }
1124
  },
1125
  {1, 1, 3, 0,    /* 0x93 */
1126
    {{0, 1},
1127
    {4, 4},
1128
    {7, 7},
1129
    {0, 0}
1130
    }
1131
  },
1132
  {0, 1, 3, 0,    /* 0x94 */
1133
    {{2, 2},
1134
    {4, 4},
1135
    {7, 7},
1136
    {0, 0}
1137
    }
1138
  },
1139
  {1, 1, 4, 0,    /* 0x95 */
1140
    {{0, 0},
1141
    {2, 2},
1142
    {4, 4},
1143
    {7, 7}
1144
    }
1145
  },
1146
  {0, 1, 3, 0,    /* 0x96 */
1147
    {{1, 2},
1148
    {4, 4},
1149
    {7, 7},
1150
    {0, 0}
1151
    }
1152
  },
1153
  {1, 1, 3, 0,    /* 0x97 */
1154
    {{0, 2},
1155
    {4, 4},
1156
    {7, 7},
1157
    {0, 0}
1158
    }
1159
  },
1160
  {0, 1, 2, 0,    /* 0x98 */
1161
    {{3, 4},
1162
    {7, 7},
1163
    {0, 0},
1164
    {0, 0}
1165
    }
1166
  },
1167
  {1, 1, 3, 0,    /* 0x99 */
1168
    {{0, 0},
1169
    {3, 4},
1170
    {7, 7},
1171
    {0, 0}
1172
    }
1173
  },
1174
  {0, 1, 3, 0,    /* 0x9a */
1175
    {{1, 1},
1176
    {3, 4},
1177
    {7, 7},
1178
    {0, 0}
1179
    }
1180
  },
1181
  {1, 1, 3, 0,    /* 0x9b */
1182
    {{0, 1},
1183
    {3, 4},
1184
    {7, 7},
1185
    {0, 0}
1186
    }
1187
  },
1188
  {0, 1, 2, 0,    /* 0x9c */
1189
    {{2, 4},
1190
    {7, 7},
1191
    {0, 0},
1192
    {0, 0}
1193
    }
1194
  },
1195
  {1, 1, 3, 0,    /* 0x9d */
1196
    {{0, 0},
1197
    {2, 4},
1198
    {7, 7},
1199
    {0, 0}
1200
    }
1201
  },
1202
  {0, 1, 2, 0,    /* 0x9e */
1203
    {{1, 4},
1204
    {7, 7},
1205
    {0, 0},
1206
    {0, 0}
1207
    }
1208
  },
1209
  {1, 1, 2, 0,    /* 0x9f */
1210
    {{0, 4},
1211
    {7, 7},
1212
    {0, 0},
1213
    {0, 0}
1214
    }
1215
  },
1216
  {0, 1, 2, 0,    /* 0xa0 */
1217
    {{5, 5},
1218
    {7, 7},
1219
    {0, 0},
1220
    {0, 0}
1221
    }
1222
  },
1223
  {1, 1, 3, 0,    /* 0xa1 */
1224
    {{0, 0},
1225
    {5, 5},
1226
    {7, 7},
1227
    {0, 0}
1228
    }
1229
  },
1230
  {0, 1, 3, 0,    /* 0xa2 */
1231
    {{1, 1},
1232
    {5, 5},
1233
    {7, 7},
1234
    {0, 0}
1235
    }
1236
  },
1237
  {1, 1, 3, 0,    /* 0xa3 */
1238
    {{0, 1},
1239
    {5, 5},
1240
    {7, 7},
1241
    {0, 0}
1242
    }
1243
  },
1244
  {0, 1, 3, 0,    /* 0xa4 */
1245
    {{2, 2},
1246
    {5, 5},
1247
    {7, 7},
1248
    {0, 0}
1249
    }
1250
  },
1251
  {1, 1, 4, 0,    /* 0xa5 */
1252
    {{0, 0},
1253
    {2, 2},
1254
    {5, 5},
1255
    {7, 7}
1256
    }
1257
  },
1258
  {0, 1, 3, 0,    /* 0xa6 */
1259
    {{1, 2},
1260
    {5, 5},
1261
    {7, 7},
1262
    {0, 0}
1263
    }
1264
  },
1265
  {1, 1, 3, 0,    /* 0xa7 */
1266
    {{0, 2},
1267
    {5, 5},
1268
    {7, 7},
1269
    {0, 0}
1270
    }
1271
  },
1272
  {0, 1, 3, 0,    /* 0xa8 */
1273
    {{3, 3},
1274
    {5, 5},
1275
    {7, 7},
1276
    {0, 0}
1277
    }
1278
  },
1279
  {1, 1, 4, 0,    /* 0xa9 */
1280
    {{0, 0},
1281
    {3, 3},
1282
    {5, 5},
1283
    {7, 7}
1284
    }
1285
  },
1286
  {0, 1, 4, 0,    /* 0xaa */
1287
    {{1, 1},
1288
    {3, 3},
1289
    {5, 5},
1290
    {7, 7}
1291
    }
1292
  },
1293
  {1, 1, 4, 0,    /* 0xab */
1294
    {{0, 1},
1295
    {3, 3},
1296
    {5, 5},
1297
    {7, 7}
1298
    }
1299
  },
1300
  {0, 1, 3, 0,    /* 0xac */
1301
    {{2, 3},
1302
    {5, 5},
1303
    {7, 7},
1304
    {0, 0}
1305
    }
1306
  },
1307
  {1, 1, 4, 0,    /* 0xad */
1308
    {{0, 0},
1309
    {2, 3},
1310
    {5, 5},
1311
    {7, 7}
1312
    }
1313
  },
1314
  {0, 1, 3, 0,    /* 0xae */
1315
    {{1, 3},
1316
    {5, 5},
1317
    {7, 7},
1318
    {0, 0}
1319
    }
1320
  },
1321
  {1, 1, 3, 0,    /* 0xaf */
1322
    {{0, 3},
1323
    {5, 5},
1324
    {7, 7},
1325
    {0, 0}
1326
    }
1327
  },
1328
  {0, 1, 2, 0,    /* 0xb0 */
1329
    {{4, 5},
1330
    {7, 7},
1331
    {0, 0},
1332
    {0, 0}
1333
    }
1334
  },
1335
  {1, 1, 3, 0,    /* 0xb1 */
1336
    {{0, 0},
1337
    {4, 5},
1338
    {7, 7},
1339
    {0, 0}
1340
    }
1341
  },
1342
  {0, 1, 3, 0,    /* 0xb2 */
1343
    {{1, 1},
1344
    {4, 5},
1345
    {7, 7},
1346
    {0, 0}
1347
    }
1348
  },
1349
  {1, 1, 3, 0,    /* 0xb3 */
1350
    {{0, 1},
1351
    {4, 5},
1352
    {7, 7},
1353
    {0, 0}
1354
    }
1355
  },
1356
  {0, 1, 3, 0,    /* 0xb4 */
1357
    {{2, 2},
1358
    {4, 5},
1359
    {7, 7},
1360
    {0, 0}
1361
    }
1362
  },
1363
  {1, 1, 4, 0,    /* 0xb5 */
1364
    {{0, 0},
1365
    {2, 2},
1366
    {4, 5},
1367
    {7, 7}
1368
    }
1369
  },
1370
  {0, 1, 3, 0,    /* 0xb6 */
1371
    {{1, 2},
1372
    {4, 5},
1373
    {7, 7},
1374
    {0, 0}
1375
    }
1376
  },
1377
  {1, 1, 3, 0,    /* 0xb7 */
1378
    {{0, 2},
1379
    {4, 5},
1380
    {7, 7},
1381
    {0, 0}
1382
    }
1383
  },
1384
  {0, 1, 2, 0,    /* 0xb8 */
1385
    {{3, 5},
1386
    {7, 7},
1387
    {0, 0},
1388
    {0, 0}
1389
    }
1390
  },
1391
  {1, 1, 3, 0,    /* 0xb9 */
1392
    {{0, 0},
1393
    {3, 5},
1394
    {7, 7},
1395
    {0, 0}
1396
    }
1397
  },
1398
  {0, 1, 3, 0,    /* 0xba */
1399
    {{1, 1},
1400
    {3, 5},
1401
    {7, 7},
1402
    {0, 0}
1403
    }
1404
  },
1405
  {1, 1, 3, 0,    /* 0xbb */
1406
    {{0, 1},
1407
    {3, 5},
1408
    {7, 7},
1409
    {0, 0}
1410
    }
1411
  },
1412
  {0, 1, 2, 0,    /* 0xbc */
1413
    {{2, 5},
1414
    {7, 7},
1415
    {0, 0},
1416
    {0, 0}
1417
    }
1418
  },
1419
  {1, 1, 3, 0,    /* 0xbd */
1420
    {{0, 0},
1421
    {2, 5},
1422
    {7, 7},
1423
    {0, 0}
1424
    }
1425
  },
1426
  {0, 1, 2, 0,    /* 0xbe */
1427
    {{1, 5},
1428
    {7, 7},
1429
    {0, 0},
1430
    {0, 0}
1431
    }
1432
  },
1433
  {1, 1, 2, 0,    /* 0xbf */
1434
    {{0, 5},
1435
    {7, 7},
1436
    {0, 0},
1437
    {0, 0}
1438
    }
1439
  },
1440
  {0, 1, 1, 0,    /* 0xc0 */
1441
    {{6, 7},
1442
    {0, 0},
1443
    {0, 0},
1444
    {0, 0}
1445
    }
1446
  },
1447
  {1, 1, 2, 0,    /* 0xc1 */
1448
    {{0, 0},
1449
    {6, 7},
1450
    {0, 0},
1451
    {0, 0}
1452
    }
1453
  },
1454
  {0, 1, 2, 0,    /* 0xc2 */
1455
    {{1, 1},
1456
    {6, 7},
1457
    {0, 0},
1458
    {0, 0}
1459
    }
1460
  },
1461
  {1, 1, 2, 0,    /* 0xc3 */
1462
    {{0, 1},
1463
    {6, 7},
1464
    {0, 0},
1465
    {0, 0}
1466
    }
1467
  },
1468
  {0, 1, 2, 0,    /* 0xc4 */
1469
    {{2, 2},
1470
    {6, 7},
1471
    {0, 0},
1472
    {0, 0}
1473
    }
1474
  },
1475
  {1, 1, 3, 0,    /* 0xc5 */
1476
    {{0, 0},
1477
    {2, 2},
1478
    {6, 7},
1479
    {0, 0}
1480
    }
1481
  },
1482
  {0, 1, 2, 0,    /* 0xc6 */
1483
    {{1, 2},
1484
    {6, 7},
1485
    {0, 0},
1486
    {0, 0}
1487
    }
1488
  },
1489
  {1, 1, 2, 0,    /* 0xc7 */
1490
    {{0, 2},
1491
    {6, 7},
1492
    {0, 0},
1493
    {0, 0}
1494
    }
1495
  },
1496
  {0, 1, 2, 0,    /* 0xc8 */
1497
    {{3, 3},
1498
    {6, 7},
1499
    {0, 0},
1500
    {0, 0}
1501
    }
1502
  },
1503
  {1, 1, 3, 0,    /* 0xc9 */
1504
    {{0, 0},
1505
    {3, 3},
1506
    {6, 7},
1507
    {0, 0}
1508
    }
1509
  },
1510
  {0, 1, 3, 0,    /* 0xca */
1511
    {{1, 1},
1512
    {3, 3},
1513
    {6, 7},
1514
    {0, 0}
1515
    }
1516
  },
1517
  {1, 1, 3, 0,    /* 0xcb */
1518
    {{0, 1},
1519
    {3, 3},
1520
    {6, 7},
1521
    {0, 0}
1522
    }
1523
  },
1524
  {0, 1, 2, 0,    /* 0xcc */
1525
    {{2, 3},
1526
    {6, 7},
1527
    {0, 0},
1528
    {0, 0}
1529
    }
1530
  },
1531
  {1, 1, 3, 0,    /* 0xcd */
1532
    {{0, 0},
1533
    {2, 3},
1534
    {6, 7},
1535
    {0, 0}
1536
    }
1537
  },
1538
  {0, 1, 2, 0,    /* 0xce */
1539
    {{1, 3},
1540
    {6, 7},
1541
    {0, 0},
1542
    {0, 0}
1543
    }
1544
  },
1545
  {1, 1, 2, 0,    /* 0xcf */
1546
    {{0, 3},
1547
    {6, 7},
1548
    {0, 0},
1549
    {0, 0}
1550
    }
1551
  },
1552
  {0, 1, 2, 0,    /* 0xd0 */
1553
    {{4, 4},
1554
    {6, 7},
1555
    {0, 0},
1556
    {0, 0}
1557
    }
1558
  },
1559
  {1, 1, 3, 0,    /* 0xd1 */
1560
    {{0, 0},
1561
    {4, 4},
1562
    {6, 7},
1563
    {0, 0}
1564
    }
1565
  },
1566
  {0, 1, 3, 0,    /* 0xd2 */
1567
    {{1, 1},
1568
    {4, 4},
1569
    {6, 7},
1570
    {0, 0}
1571
    }
1572
  },
1573
  {1, 1, 3, 0,    /* 0xd3 */
1574
    {{0, 1},
1575
    {4, 4},
1576
    {6, 7},
1577
    {0, 0}
1578
    }
1579
  },
1580
  {0, 1, 3, 0,    /* 0xd4 */
1581
    {{2, 2},
1582
    {4, 4},
1583
    {6, 7},
1584
    {0, 0}
1585
    }
1586
  },
1587
  {1, 1, 4, 0,    /* 0xd5 */
1588
    {{0, 0},
1589
    {2, 2},
1590
    {4, 4},
1591
    {6, 7}
1592
    }
1593
  },
1594
  {0, 1, 3, 0,    /* 0xd6 */
1595
    {{1, 2},
1596
    {4, 4},
1597
    {6, 7},
1598
    {0, 0}
1599
    }
1600
  },
1601
  {1, 1, 3, 0,    /* 0xd7 */
1602
    {{0, 2},
1603
    {4, 4},
1604
    {6, 7},
1605
    {0, 0}
1606
    }
1607
  },
1608
  {0, 1, 2, 0,    /* 0xd8 */
1609
    {{3, 4},
1610
    {6, 7},
1611
    {0, 0},
1612
    {0, 0}
1613
    }
1614
  },
1615
  {1, 1, 3, 0,    /* 0xd9 */
1616
    {{0, 0},
1617
    {3, 4},
1618
    {6, 7},
1619
    {0, 0}
1620
    }
1621
  },
1622
  {0, 1, 3, 0,    /* 0xda */
1623
    {{1, 1},
1624
    {3, 4},
1625
    {6, 7},
1626
    {0, 0}
1627
    }
1628
  },
1629
  {1, 1, 3, 0,    /* 0xdb */
1630
    {{0, 1},
1631
    {3, 4},
1632
    {6, 7},
1633
    {0, 0}
1634
    }
1635
  },
1636
  {0, 1, 2, 0,    /* 0xdc */
1637
    {{2, 4},
1638
    {6, 7},
1639
    {0, 0},
1640
    {0, 0}
1641
    }
1642
  },
1643
  {1, 1, 3, 0,    /* 0xdd */
1644
    {{0, 0},
1645
    {2, 4},
1646
    {6, 7},
1647
    {0, 0}
1648
    }
1649
  },
1650
  {0, 1, 2, 0,    /* 0xde */
1651
    {{1, 4},
1652
    {6, 7},
1653
    {0, 0},
1654
    {0, 0}
1655
    }
1656
  },
1657
  {1, 1, 2, 0,    /* 0xdf */
1658
    {{0, 4},
1659
    {6, 7},
1660
    {0, 0},
1661
    {0, 0}
1662
    }
1663
  },
1664
  {0, 1, 1, 0,    /* 0xe0 */
1665
    {{5, 7},
1666
    {0, 0},
1667
    {0, 0},
1668
    {0, 0}
1669
    }
1670
  },
1671
  {1, 1, 2, 0,    /* 0xe1 */
1672
    {{0, 0},
1673
    {5, 7},
1674
    {0, 0},
1675
    {0, 0}
1676
    }
1677
  },
1678
  {0, 1, 2, 0,    /* 0xe2 */
1679
    {{1, 1},
1680
    {5, 7},
1681
    {0, 0},
1682
    {0, 0}
1683
    }
1684
  },
1685
  {1, 1, 2, 0,    /* 0xe3 */
1686
    {{0, 1},
1687
    {5, 7},
1688
    {0, 0},
1689
    {0, 0}
1690
    }
1691
  },
1692
  {0, 1, 2, 0,    /* 0xe4 */
1693
    {{2, 2},
1694
    {5, 7},
1695
    {0, 0},
1696
    {0, 0}
1697
    }
1698
  },
1699
  {1, 1, 3, 0,    /* 0xe5 */
1700
    {{0, 0},
1701
    {2, 2},
1702
    {5, 7},
1703
    {0, 0}
1704
    }
1705
  },
1706
  {0, 1, 2, 0,    /* 0xe6 */
1707
    {{1, 2},
1708
    {5, 7},
1709
    {0, 0},
1710
    {0, 0}
1711
    }
1712
  },
1713
  {1, 1, 2, 0,    /* 0xe7 */
1714
    {{0, 2},
1715
    {5, 7},
1716
    {0, 0},
1717
    {0, 0}
1718
    }
1719
  },
1720
  {0, 1, 2, 0,    /* 0xe8 */
1721
    {{3, 3},
1722
    {5, 7},
1723
    {0, 0},
1724
    {0, 0}
1725
    }
1726
  },
1727
  {1, 1, 3, 0,    /* 0xe9 */
1728
    {{0, 0},
1729
    {3, 3},
1730
    {5, 7},
1731
    {0, 0}
1732
    }
1733
  },
1734
  {0, 1, 3, 0,    /* 0xea */
1735
    {{1, 1},
1736
    {3, 3},
1737
    {5, 7},
1738
    {0, 0}
1739
    }
1740
  },
1741
  {1, 1, 3, 0,    /* 0xeb */
1742
    {{0, 1},
1743
    {3, 3},
1744
    {5, 7},
1745
    {0, 0}
1746
    }
1747
  },
1748
  {0, 1, 2, 0,    /* 0xec */
1749
    {{2, 3},
1750
    {5, 7},
1751
    {0, 0},
1752
    {0, 0}
1753
    }
1754
  },
1755
  {1, 1, 3, 0,    /* 0xed */
1756
    {{0, 0},
1757
    {2, 3},
1758
    {5, 7},
1759
    {0, 0}
1760
    }
1761
  },
1762
  {0, 1, 2, 0,    /* 0xee */
1763
    {{1, 3},
1764
    {5, 7},
1765
    {0, 0},
1766
    {0, 0}
1767
    }
1768
  },
1769
  {1, 1, 2, 0,    /* 0xef */
1770
    {{0, 3},
1771
    {5, 7},
1772
    {0, 0},
1773
    {0, 0}
1774
    }
1775
  },
1776
  {0, 1, 1, 0,    /* 0xf0 */
1777
    {{4, 7},
1778
    {0, 0},
1779
    {0, 0},
1780
    {0, 0}
1781
    }
1782
  },
1783
  {1, 1, 2, 0,    /* 0xf1 */
1784
    {{0, 0},
1785
    {4, 7},
1786
    {0, 0},
1787
    {0, 0}
1788
    }
1789
  },
1790
  {0, 1, 2, 0,    /* 0xf2 */
1791
    {{1, 1},
1792
    {4, 7},
1793
    {0, 0},
1794
    {0, 0}
1795
    }
1796
  },
1797
  {1, 1, 2, 0,    /* 0xf3 */
1798
    {{0, 1},
1799
    {4, 7},
1800
    {0, 0},
1801
    {0, 0}
1802
    }
1803
  },
1804
  {0, 1, 2, 0,    /* 0xf4 */
1805
    {{2, 2},
1806
    {4, 7},
1807
    {0, 0},
1808
    {0, 0}
1809
    }
1810
  },
1811
  {1, 1, 3, 0,    /* 0xf5 */
1812
    {{0, 0},
1813
    {2, 2},
1814
    {4, 7},
1815
    {0, 0}
1816
    }
1817
  },
1818
  {0, 1, 2, 0,    /* 0xf6 */
1819
    {{1, 2},
1820
    {4, 7},
1821
    {0, 0},
1822
    {0, 0}
1823
    }
1824
  },
1825
  {1, 1, 2, 0,    /* 0xf7 */
1826
    {{0, 2},
1827
    {4, 7},
1828
    {0, 0},
1829
    {0, 0}
1830
    }
1831
  },
1832
  {0, 1, 1, 0,    /* 0xf8 */
1833
    {{3, 7},
1834
    {0, 0},
1835
    {0, 0},
1836
    {0, 0}
1837
    }
1838
  },
1839
  {1, 1, 2, 0,    /* 0xf9 */
1840
    {{0, 0},
1841
    {3, 7},
1842
    {0, 0},
1843
    {0, 0}
1844
    }
1845
  },
1846
  {0, 1, 2, 0,    /* 0xfa */
1847
    {{1, 1},
1848
    {3, 7},
1849
    {0, 0},
1850
    {0, 0}
1851
    }
1852
  },
1853
  {1, 1, 2, 0,    /* 0xfb */
1854
    {{0, 1},
1855
    {3, 7},
1856
    {0, 0},
1857
    {0, 0}
1858
    }
1859
  },
1860
  {0, 1, 1, 0,    /* 0xfc */
1861
    {{2, 7},
1862
    {0, 0},
1863
    {0, 0},
1864
    {0, 0}
1865
    }
1866
  },
1867
  {1, 1, 2, 0,    /* 0xfd */
1868
    {{0, 0},
1869
    {2, 7},
1870
    {0, 0},
1871
    {0, 0}
1872
    }
1873
  },
1874
  {0, 1, 1, 0,    /* 0xfe */
1875
    {{1, 7},
1876
    {0, 0},
1877
    {0, 0},
1878
    {0, 0}
1879
    }
1880
  },
1881
  {1, 1, 1, 0,    /* 0xff */
1882
    {{0, 7},
1883
    {0, 0},
1884
    {0, 0},
1885
    {0, 0}
1886
    }
1887
  }
1888
};
1889
1890
int
1891
sctp_is_address_in_scope(struct sctp_ifa *ifa,
1892
                         struct sctp_scoping *scope,
1893
                         int do_update)
1894
0
{
1895
0
  if ((scope->loopback_scope == 0) &&
1896
0
      (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1897
    /*
1898
     * skip loopback if not in scope *
1899
     */
1900
0
    return (0);
1901
0
  }
1902
0
  switch (ifa->address.sa.sa_family) {
1903
0
#ifdef INET
1904
0
  case AF_INET:
1905
0
    if (scope->ipv4_addr_legal) {
1906
0
      struct sockaddr_in *sin;
1907
1908
0
      sin = &ifa->address.sin;
1909
0
      if (sin->sin_addr.s_addr == 0) {
1910
        /* not in scope , unspecified */
1911
0
        return (0);
1912
0
      }
1913
0
      if ((scope->ipv4_local_scope == 0) &&
1914
0
          (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1915
        /* private address not in scope */
1916
0
        return (0);
1917
0
      }
1918
0
    } else {
1919
0
      return (0);
1920
0
    }
1921
0
    break;
1922
0
#endif
1923
0
#ifdef INET6
1924
0
  case AF_INET6:
1925
0
    if (scope->ipv6_addr_legal) {
1926
0
      struct sockaddr_in6 *sin6;
1927
1928
      /* Must update the flags,  bummer, which
1929
       * means any IFA locks must now be applied HERE <->
1930
       */
1931
0
      if (do_update) {
1932
0
        sctp_gather_internal_ifa_flags(ifa);
1933
0
      }
1934
0
      if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1935
0
        return (0);
1936
0
      }
1937
      /* ok to use deprecated addresses? */
1938
0
      sin6 = &ifa->address.sin6;
1939
0
      if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1940
        /* skip unspecified addresses */
1941
0
        return (0);
1942
0
      }
1943
0
      if (    /* (local_scope == 0) && */
1944
0
          (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1945
0
        return (0);
1946
0
      }
1947
0
      if ((scope->site_scope == 0) &&
1948
0
          (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1949
0
        return (0);
1950
0
      }
1951
0
    } else {
1952
0
      return (0);
1953
0
    }
1954
0
    break;
1955
0
#endif
1956
0
#if defined(__Userspace__)
1957
0
  case AF_CONN:
1958
0
    if (!scope->conn_addr_legal) {
1959
0
      return (0);
1960
0
    }
1961
0
    break;
1962
0
#endif
1963
0
  default:
1964
0
    return (0);
1965
0
  }
1966
0
  return (1);
1967
0
}
1968
1969
static struct mbuf *
1970
sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len)
1971
0
{
1972
0
#if defined(INET) || defined(INET6)
1973
0
  struct sctp_paramhdr *paramh;
1974
0
  struct mbuf *mret;
1975
0
  uint16_t plen;
1976
0
#endif
1977
1978
0
  switch (ifa->address.sa.sa_family) {
1979
0
#ifdef INET
1980
0
  case AF_INET:
1981
0
    plen = (uint16_t)sizeof(struct sctp_ipv4addr_param);
1982
0
    break;
1983
0
#endif
1984
0
#ifdef INET6
1985
0
  case AF_INET6:
1986
0
    plen = (uint16_t)sizeof(struct sctp_ipv6addr_param);
1987
0
    break;
1988
0
#endif
1989
0
  default:
1990
0
    return (m);
1991
0
  }
1992
0
#if defined(INET) || defined(INET6)
1993
0
  if (M_TRAILINGSPACE(m) >= plen) {
1994
    /* easy side we just drop it on the end */
1995
0
    paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1996
0
    mret = m;
1997
0
  } else {
1998
    /* Need more space */
1999
0
    mret = m;
2000
0
    while (SCTP_BUF_NEXT(mret) != NULL) {
2001
0
      mret = SCTP_BUF_NEXT(mret);
2002
0
    }
2003
0
    SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA);
2004
0
    if (SCTP_BUF_NEXT(mret) == NULL) {
2005
      /* We are hosed, can't add more addresses */
2006
0
      return (m);
2007
0
    }
2008
0
    mret = SCTP_BUF_NEXT(mret);
2009
0
    paramh = mtod(mret, struct sctp_paramhdr *);
2010
0
  }
2011
  /* now add the parameter */
2012
0
  switch (ifa->address.sa.sa_family) {
2013
0
#ifdef INET
2014
0
  case AF_INET:
2015
0
  {
2016
0
    struct sctp_ipv4addr_param *ipv4p;
2017
0
    struct sockaddr_in *sin;
2018
2019
0
    sin = &ifa->address.sin;
2020
0
    ipv4p = (struct sctp_ipv4addr_param *)paramh;
2021
0
    paramh->param_type = htons(SCTP_IPV4_ADDRESS);
2022
0
    paramh->param_length = htons(plen);
2023
0
    ipv4p->addr = sin->sin_addr.s_addr;
2024
0
    SCTP_BUF_LEN(mret) += plen;
2025
0
    break;
2026
0
  }
2027
0
#endif
2028
0
#ifdef INET6
2029
0
  case AF_INET6:
2030
0
  {
2031
0
    struct sctp_ipv6addr_param *ipv6p;
2032
0
    struct sockaddr_in6 *sin6;
2033
2034
0
    sin6 = &ifa->address.sin6;
2035
0
    ipv6p = (struct sctp_ipv6addr_param *)paramh;
2036
0
    paramh->param_type = htons(SCTP_IPV6_ADDRESS);
2037
0
    paramh->param_length = htons(plen);
2038
0
    memcpy(ipv6p->addr, &sin6->sin6_addr,
2039
0
        sizeof(ipv6p->addr));
2040
#if defined(SCTP_EMBEDDED_V6_SCOPE)
2041
    /* clear embedded scope in the address */
2042
    in6_clearscope((struct in6_addr *)ipv6p->addr);
2043
#endif
2044
0
    SCTP_BUF_LEN(mret) += plen;
2045
0
    break;
2046
0
  }
2047
0
#endif
2048
0
  default:
2049
0
    return (m);
2050
0
  }
2051
0
  if (len != NULL) {
2052
0
    *len += plen;
2053
0
  }
2054
0
  return (mret);
2055
0
#endif
2056
0
}
2057
2058
struct mbuf *
2059
sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2060
                           struct sctp_scoping *scope,
2061
         struct mbuf *m_at, int cnt_inits_to,
2062
         uint16_t *padding_len, uint16_t *chunk_len)
2063
22.6k
{
2064
22.6k
  struct sctp_vrf *vrf = NULL;
2065
22.6k
  int cnt, limit_out = 0, total_count;
2066
22.6k
  uint32_t vrf_id;
2067
2068
22.6k
  vrf_id = inp->def_vrf_id;
2069
22.6k
  SCTP_IPI_ADDR_RLOCK();
2070
22.6k
  vrf = sctp_find_vrf(vrf_id);
2071
22.6k
  if (vrf == NULL) {
2072
0
    SCTP_IPI_ADDR_RUNLOCK();
2073
0
    return (m_at);
2074
0
  }
2075
22.6k
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2076
0
    struct sctp_ifa *sctp_ifap;
2077
0
    struct sctp_ifn *sctp_ifnp;
2078
2079
0
    cnt = cnt_inits_to;
2080
0
    if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2081
0
      limit_out = 1;
2082
0
      cnt = SCTP_ADDRESS_LIMIT;
2083
0
      goto skip_count;
2084
0
    }
2085
0
    LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2086
0
      if ((scope->loopback_scope == 0) &&
2087
0
          SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2088
        /*
2089
         * Skip loopback devices if loopback_scope
2090
         * not set
2091
         */
2092
0
        continue;
2093
0
      }
2094
0
      LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2095
#if defined(__FreeBSD__) && !defined(__Userspace__)
2096
#ifdef INET
2097
        if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2098
            (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2099
                              &sctp_ifap->address.sin.sin_addr) != 0)) {
2100
          continue;
2101
        }
2102
#endif
2103
#ifdef INET6
2104
        if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2105
            (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2106
                              &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2107
          continue;
2108
        }
2109
#endif
2110
#endif
2111
0
        if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2112
0
          continue;
2113
0
        }
2114
0
#if defined(__Userspace__)
2115
0
        if (sctp_ifap->address.sa.sa_family == AF_CONN) {
2116
0
          continue;
2117
0
        }
2118
0
#endif
2119
0
        if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) {
2120
0
          continue;
2121
0
        }
2122
0
        cnt++;
2123
0
        if (cnt > SCTP_ADDRESS_LIMIT) {
2124
0
          break;
2125
0
        }
2126
0
      }
2127
0
      if (cnt > SCTP_ADDRESS_LIMIT) {
2128
0
        break;
2129
0
      }
2130
0
    }
2131
0
  skip_count:
2132
0
    if (cnt > 1) {
2133
0
      total_count = 0;
2134
0
      LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2135
0
        cnt = 0;
2136
0
        if ((scope->loopback_scope == 0) &&
2137
0
            SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2138
          /*
2139
           * Skip loopback devices if
2140
           * loopback_scope not set
2141
           */
2142
0
          continue;
2143
0
        }
2144
0
        LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2145
#if defined(__FreeBSD__) && !defined(__Userspace__)
2146
#ifdef INET
2147
          if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2148
              (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2149
                                &sctp_ifap->address.sin.sin_addr) != 0)) {
2150
            continue;
2151
          }
2152
#endif
2153
#ifdef INET6
2154
          if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2155
              (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2156
                                &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2157
            continue;
2158
          }
2159
#endif
2160
#endif
2161
0
          if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2162
0
            continue;
2163
0
          }
2164
0
#if defined(__Userspace__)
2165
0
          if (sctp_ifap->address.sa.sa_family == AF_CONN) {
2166
0
            continue;
2167
0
          }
2168
0
#endif
2169
0
          if (sctp_is_address_in_scope(sctp_ifap,
2170
0
                     scope, 0) == 0) {
2171
0
            continue;
2172
0
          }
2173
0
          if ((chunk_len != NULL) &&
2174
0
              (padding_len != NULL) &&
2175
0
              (*padding_len > 0)) {
2176
0
            memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len);
2177
0
            SCTP_BUF_LEN(m_at) += *padding_len;
2178
0
            *chunk_len += *padding_len;
2179
0
            *padding_len = 0;
2180
0
          }
2181
0
          m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len);
2182
0
          if (limit_out) {
2183
0
            cnt++;
2184
0
            total_count++;
2185
0
            if (cnt >= 2) {
2186
              /* two from each address */
2187
0
              break;
2188
0
            }
2189
0
            if (total_count > SCTP_ADDRESS_LIMIT) {
2190
              /* No more addresses */
2191
0
              break;
2192
0
            }
2193
0
          }
2194
0
        }
2195
0
      }
2196
0
    }
2197
22.6k
  } else {
2198
22.6k
    struct sctp_laddr *laddr;
2199
2200
22.6k
    cnt = cnt_inits_to;
2201
    /* First, how many ? */
2202
22.6k
    LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2203
22.6k
      if (laddr->ifa == NULL) {
2204
0
        continue;
2205
0
      }
2206
22.6k
      if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2207
        /* Address being deleted by the system, dont
2208
         * list.
2209
         */
2210
0
        continue;
2211
22.6k
      if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2212
        /* Address being deleted on this ep
2213
         * don't list.
2214
         */
2215
0
        continue;
2216
0
      }
2217
22.6k
#if defined(__Userspace__)
2218
22.6k
      if (laddr->ifa->address.sa.sa_family == AF_CONN) {
2219
22.6k
        continue;
2220
22.6k
      }
2221
0
#endif
2222
0
      if (sctp_is_address_in_scope(laddr->ifa,
2223
0
                 scope, 1) == 0) {
2224
0
        continue;
2225
0
      }
2226
0
      cnt++;
2227
0
    }
2228
    /*
2229
     * To get through a NAT we only list addresses if we have
2230
     * more than one. That way if you just bind a single address
2231
     * we let the source of the init dictate our address.
2232
     */
2233
22.6k
    if (cnt > 1) {
2234
0
      cnt = cnt_inits_to;
2235
0
      LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2236
0
        if (laddr->ifa == NULL) {
2237
0
          continue;
2238
0
        }
2239
0
        if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2240
0
          continue;
2241
0
        }
2242
0
#if defined(__Userspace__)
2243
0
        if (laddr->ifa->address.sa.sa_family == AF_CONN) {
2244
0
          continue;
2245
0
        }
2246
0
#endif
2247
0
        if (sctp_is_address_in_scope(laddr->ifa,
2248
0
                   scope, 0) == 0) {
2249
0
          continue;
2250
0
        }
2251
0
        if ((chunk_len != NULL) &&
2252
0
            (padding_len != NULL) &&
2253
0
            (*padding_len > 0)) {
2254
0
          memset(mtod(m_at, caddr_t) + *chunk_len, 0, *padding_len);
2255
0
          SCTP_BUF_LEN(m_at) += *padding_len;
2256
0
          *chunk_len += *padding_len;
2257
0
          *padding_len = 0;
2258
0
        }
2259
0
        m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len);
2260
0
        cnt++;
2261
0
        if (cnt >= SCTP_ADDRESS_LIMIT) {
2262
0
          break;
2263
0
        }
2264
0
      }
2265
0
    }
2266
22.6k
  }
2267
22.6k
  SCTP_IPI_ADDR_RUNLOCK();
2268
22.6k
  return (m_at);
2269
22.6k
}
2270
2271
static struct sctp_ifa *
2272
sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2273
         uint8_t dest_is_loop,
2274
         uint8_t dest_is_priv,
2275
         sa_family_t fam)
2276
4.37k
{
2277
4.37k
  uint8_t dest_is_global = 0;
2278
  /* dest_is_priv is true if destination is a private address */
2279
  /* dest_is_loop is true if destination is a loopback addresses */
2280
2281
  /**
2282
   * Here we determine if its a preferred address. A preferred address
2283
   * means it is the same scope or higher scope then the destination.
2284
   * L = loopback, P = private, G = global
2285
   * -----------------------------------------
2286
   *    src    |  dest | result
2287
   *  ----------------------------------------
2288
   *     L     |    L  |    yes
2289
   *  -----------------------------------------
2290
   *     P     |    L  |    yes-v4 no-v6
2291
   *  -----------------------------------------
2292
   *     G     |    L  |    yes-v4 no-v6
2293
   *  -----------------------------------------
2294
   *     L     |    P  |    no
2295
   *  -----------------------------------------
2296
   *     P     |    P  |    yes
2297
   *  -----------------------------------------
2298
   *     G     |    P  |    no
2299
   *   -----------------------------------------
2300
   *     L     |    G  |    no
2301
   *   -----------------------------------------
2302
   *     P     |    G  |    no
2303
   *    -----------------------------------------
2304
   *     G     |    G  |    yes
2305
   *    -----------------------------------------
2306
   */
2307
2308
4.37k
  if (ifa->address.sa.sa_family != fam) {
2309
    /* forget mis-matched family */
2310
4.37k
    return (NULL);
2311
4.37k
  }
2312
0
  if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2313
0
    dest_is_global = 1;
2314
0
  }
2315
0
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2316
0
  SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2317
  /* Ok the address may be ok */
2318
0
#ifdef INET6
2319
0
  if (fam == AF_INET6) {
2320
    /* ok to use deprecated addresses? no lets not! */
2321
0
    if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2322
0
      SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2323
0
      return (NULL);
2324
0
    }
2325
0
    if (ifa->src_is_priv && !ifa->src_is_loop) {
2326
0
      if (dest_is_loop) {
2327
0
        SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2328
0
        return (NULL);
2329
0
      }
2330
0
    }
2331
0
    if (ifa->src_is_glob) {
2332
0
      if (dest_is_loop) {
2333
0
        SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2334
0
        return (NULL);
2335
0
      }
2336
0
    }
2337
0
  }
2338
0
#endif
2339
  /* Now that we know what is what, implement or table
2340
   * this could in theory be done slicker (it used to be), but this
2341
   * is straightforward and easier to validate :-)
2342
   */
2343
0
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2344
0
    ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2345
0
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2346
0
    dest_is_loop, dest_is_priv, dest_is_global);
2347
2348
0
  if ((ifa->src_is_loop) && (dest_is_priv)) {
2349
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2350
0
    return (NULL);
2351
0
  }
2352
0
  if ((ifa->src_is_glob) && (dest_is_priv)) {
2353
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2354
0
    return (NULL);
2355
0
  }
2356
0
  if ((ifa->src_is_loop) && (dest_is_global)) {
2357
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2358
0
    return (NULL);
2359
0
  }
2360
0
  if ((ifa->src_is_priv) && (dest_is_global)) {
2361
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2362
0
    return (NULL);
2363
0
  }
2364
0
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2365
  /* its a preferred address */
2366
0
  return (ifa);
2367
0
}
2368
2369
static struct sctp_ifa *
2370
sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2371
          uint8_t dest_is_loop,
2372
          uint8_t dest_is_priv,
2373
          sa_family_t fam)
2374
4.37k
{
2375
4.37k
  uint8_t dest_is_global = 0;
2376
2377
  /**
2378
   * Here we determine if its a acceptable address. A acceptable
2379
   * address means it is the same scope or higher scope but we can
2380
   * allow for NAT which means its ok to have a global dest and a
2381
   * private src.
2382
   *
2383
   * L = loopback, P = private, G = global
2384
   * -----------------------------------------
2385
   *  src    |  dest | result
2386
   * -----------------------------------------
2387
   *   L     |   L   |    yes
2388
   *  -----------------------------------------
2389
   *   P     |   L   |    yes-v4 no-v6
2390
   *  -----------------------------------------
2391
   *   G     |   L   |    yes
2392
   * -----------------------------------------
2393
   *   L     |   P   |    no
2394
   * -----------------------------------------
2395
   *   P     |   P   |    yes
2396
   * -----------------------------------------
2397
   *   G     |   P   |    yes - May not work
2398
   * -----------------------------------------
2399
   *   L     |   G   |    no
2400
   * -----------------------------------------
2401
   *   P     |   G   |    yes - May not work
2402
   * -----------------------------------------
2403
   *   G     |   G   |    yes
2404
   * -----------------------------------------
2405
   */
2406
2407
4.37k
  if (ifa->address.sa.sa_family != fam) {
2408
    /* forget non matching family */
2409
4.37k
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2410
4.37k
      ifa->address.sa.sa_family, fam);
2411
4.37k
    return (NULL);
2412
4.37k
  }
2413
  /* Ok the address may be ok */
2414
0
  SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2415
0
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2416
0
    dest_is_loop, dest_is_priv);
2417
0
  if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2418
0
    dest_is_global = 1;
2419
0
  }
2420
0
#ifdef INET6
2421
0
  if (fam == AF_INET6) {
2422
    /* ok to use deprecated addresses? */
2423
0
    if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2424
0
      return (NULL);
2425
0
    }
2426
0
    if (ifa->src_is_priv) {
2427
      /* Special case, linklocal to loop */
2428
0
      if (dest_is_loop)
2429
0
        return (NULL);
2430
0
    }
2431
0
  }
2432
0
#endif
2433
  /*
2434
   * Now that we know what is what, implement our table.
2435
   * This could in theory be done slicker (it used to be), but this
2436
   * is straightforward and easier to validate :-)
2437
   */
2438
0
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2439
0
    ifa->src_is_loop,
2440
0
    dest_is_priv);
2441
0
  if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2442
0
    return (NULL);
2443
0
  }
2444
0
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2445
0
    ifa->src_is_loop,
2446
0
    dest_is_global);
2447
0
  if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2448
0
    return (NULL);
2449
0
  }
2450
0
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2451
  /* its an acceptable address */
2452
0
  return (ifa);
2453
0
}
2454
2455
int
2456
sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2457
440k
{
2458
440k
  struct sctp_laddr *laddr;
2459
2460
440k
  if (stcb == NULL) {
2461
    /* There are no restrictions, no TCB :-) */
2462
0
    return (0);
2463
0
  }
2464
440k
  LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2465
0
    if (laddr->ifa == NULL) {
2466
0
      SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2467
0
        __func__);
2468
0
      continue;
2469
0
    }
2470
0
    if (laddr->ifa == ifa) {
2471
      /* Yes it is on the list */
2472
0
      return (1);
2473
0
    }
2474
0
  }
2475
440k
  return (0);
2476
440k
}
2477
2478
int
2479
sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2480
5.06k
{
2481
5.06k
  struct sctp_laddr *laddr;
2482
2483
5.06k
  if (ifa == NULL)
2484
0
    return (0);
2485
5.06k
  LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2486
5.06k
    if (laddr->ifa == NULL) {
2487
0
      SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2488
0
        __func__);
2489
0
      continue;
2490
0
    }
2491
5.06k
    if ((laddr->ifa == ifa) && laddr->action == 0)
2492
      /* same pointer */
2493
0
      return (1);
2494
5.06k
  }
2495
5.06k
  return (0);
2496
5.06k
}
2497
2498
static struct sctp_ifa *
2499
sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2500
            sctp_route_t *ro,
2501
            uint32_t vrf_id,
2502
            int non_asoc_addr_ok,
2503
            uint8_t dest_is_priv,
2504
            uint8_t dest_is_loop,
2505
            sa_family_t fam)
2506
0
{
2507
0
  struct sctp_laddr *laddr, *starting_point;
2508
0
  void *ifn;
2509
0
  int resettotop = 0;
2510
0
  struct sctp_ifn *sctp_ifn;
2511
0
  struct sctp_ifa *sctp_ifa, *sifa;
2512
0
  struct sctp_vrf *vrf;
2513
0
  uint32_t ifn_index;
2514
2515
0
  vrf = sctp_find_vrf(vrf_id);
2516
0
  if (vrf == NULL)
2517
0
    return (NULL);
2518
2519
0
  ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2520
0
  ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2521
0
  sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2522
  /*
2523
   * first question, is the ifn we will emit on in our list, if so, we
2524
   * want such an address. Note that we first looked for a
2525
   * preferred address.
2526
   */
2527
0
  if (sctp_ifn) {
2528
    /* is a preferred one on the interface we route out? */
2529
0
    LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2530
#if defined(__FreeBSD__) && !defined(__Userspace__)
2531
#ifdef INET
2532
      if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2533
          (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2534
                            &sctp_ifa->address.sin.sin_addr) != 0)) {
2535
        continue;
2536
      }
2537
#endif
2538
#ifdef INET6
2539
      if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2540
          (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2541
                            &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2542
        continue;
2543
      }
2544
#endif
2545
#endif
2546
0
      if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2547
0
          (non_asoc_addr_ok == 0))
2548
0
        continue;
2549
0
      sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2550
0
                dest_is_loop,
2551
0
                dest_is_priv, fam);
2552
0
      if (sifa == NULL)
2553
0
        continue;
2554
0
      if (sctp_is_addr_in_ep(inp, sifa)) {
2555
0
        atomic_add_int(&sifa->refcount, 1);
2556
0
        return (sifa);
2557
0
      }
2558
0
    }
2559
0
  }
2560
  /*
2561
   * ok, now we now need to find one on the list of the addresses.
2562
   * We can't get one on the emitting interface so let's find first
2563
   * a preferred one. If not that an acceptable one otherwise...
2564
   * we return NULL.
2565
   */
2566
0
  starting_point = inp->next_addr_touse;
2567
0
 once_again:
2568
0
  if (inp->next_addr_touse == NULL) {
2569
0
    inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2570
0
    resettotop = 1;
2571
0
  }
2572
0
  for (laddr = inp->next_addr_touse; laddr;
2573
0
       laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2574
0
    if (laddr->ifa == NULL) {
2575
      /* address has been removed */
2576
0
      continue;
2577
0
    }
2578
0
    if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2579
      /* address is being deleted */
2580
0
      continue;
2581
0
    }
2582
0
    sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2583
0
              dest_is_priv, fam);
2584
0
    if (sifa == NULL)
2585
0
      continue;
2586
0
    atomic_add_int(&sifa->refcount, 1);
2587
0
    return (sifa);
2588
0
  }
2589
0
  if (resettotop == 0) {
2590
0
    inp->next_addr_touse = NULL;
2591
0
    goto once_again;
2592
0
  }
2593
2594
0
  inp->next_addr_touse = starting_point;
2595
0
  resettotop = 0;
2596
0
 once_again_too:
2597
0
  if (inp->next_addr_touse == NULL) {
2598
0
    inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2599
0
    resettotop = 1;
2600
0
  }
2601
2602
  /* ok, what about an acceptable address in the inp */
2603
0
  for (laddr = inp->next_addr_touse; laddr;
2604
0
       laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2605
0
    if (laddr->ifa == NULL) {
2606
      /* address has been removed */
2607
0
      continue;
2608
0
    }
2609
0
    if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2610
      /* address is being deleted */
2611
0
      continue;
2612
0
    }
2613
0
    sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2614
0
               dest_is_priv, fam);
2615
0
    if (sifa == NULL)
2616
0
      continue;
2617
0
    atomic_add_int(&sifa->refcount, 1);
2618
0
    return (sifa);
2619
0
  }
2620
0
  if (resettotop == 0) {
2621
0
    inp->next_addr_touse = NULL;
2622
0
    goto once_again_too;
2623
0
  }
2624
2625
  /*
2626
   * no address bound can be a source for the destination we are in
2627
   * trouble
2628
   */
2629
0
  return (NULL);
2630
0
}
2631
2632
static struct sctp_ifa *
2633
sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2634
             struct sctp_tcb *stcb,
2635
             sctp_route_t *ro,
2636
             uint32_t vrf_id,
2637
             uint8_t dest_is_priv,
2638
             uint8_t dest_is_loop,
2639
             int non_asoc_addr_ok,
2640
             sa_family_t fam)
2641
2.53k
{
2642
2.53k
  struct sctp_laddr *laddr, *starting_point;
2643
2.53k
  void *ifn;
2644
2.53k
  struct sctp_ifn *sctp_ifn;
2645
2.53k
  struct sctp_ifa *sctp_ifa, *sifa;
2646
2.53k
  uint8_t start_at_beginning = 0;
2647
2.53k
  struct sctp_vrf *vrf;
2648
2.53k
  uint32_t ifn_index;
2649
2650
  /*
2651
   * first question, is the ifn we will emit on in our list, if so, we
2652
   * want that one.
2653
   */
2654
2.53k
  vrf = sctp_find_vrf(vrf_id);
2655
2.53k
  if (vrf == NULL)
2656
0
    return (NULL);
2657
2658
2.53k
  ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2659
2.53k
  ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2660
2.53k
  sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2661
2662
  /*
2663
   * first question, is the ifn we will emit on in our list?  If so,
2664
   * we want that one. First we look for a preferred. Second, we go
2665
   * for an acceptable.
2666
   */
2667
2.53k
  if (sctp_ifn) {
2668
    /* first try for a preferred address on the ep */
2669
2.53k
    LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2670
#if defined(__FreeBSD__) && !defined(__Userspace__)
2671
#ifdef INET
2672
      if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2673
          (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2674
                            &sctp_ifa->address.sin.sin_addr) != 0)) {
2675
        continue;
2676
      }
2677
#endif
2678
#ifdef INET6
2679
      if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2680
          (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2681
                            &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2682
        continue;
2683
      }
2684
#endif
2685
#endif
2686
2.53k
      if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2687
0
        continue;
2688
2.53k
      if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2689
0
        sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2690
0
        if (sifa == NULL)
2691
0
          continue;
2692
0
        if (((non_asoc_addr_ok == 0) &&
2693
0
             (sctp_is_addr_restricted(stcb, sifa))) ||
2694
0
            (non_asoc_addr_ok &&
2695
0
             (sctp_is_addr_restricted(stcb, sifa)) &&
2696
0
             (!sctp_is_addr_pending(stcb, sifa)))) {
2697
          /* on the no-no list */
2698
0
          continue;
2699
0
        }
2700
0
        atomic_add_int(&sifa->refcount, 1);
2701
0
        return (sifa);
2702
0
      }
2703
2.53k
    }
2704
    /* next try for an acceptable address on the ep */
2705
2.53k
    LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2706
#if defined(__FreeBSD__) && !defined(__Userspace__)
2707
#ifdef INET
2708
      if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2709
          (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2710
                            &sctp_ifa->address.sin.sin_addr) != 0)) {
2711
        continue;
2712
      }
2713
#endif
2714
#ifdef INET6
2715
      if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2716
          (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2717
                            &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2718
        continue;
2719
      }
2720
#endif
2721
#endif
2722
2.53k
      if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2723
0
        continue;
2724
2.53k
      if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2725
0
        sifa= sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv,fam);
2726
0
        if (sifa == NULL)
2727
0
          continue;
2728
0
        if (((non_asoc_addr_ok == 0) &&
2729
0
             (sctp_is_addr_restricted(stcb, sifa))) ||
2730
0
            (non_asoc_addr_ok &&
2731
0
             (sctp_is_addr_restricted(stcb, sifa)) &&
2732
0
             (!sctp_is_addr_pending(stcb, sifa)))) {
2733
          /* on the no-no list */
2734
0
          continue;
2735
0
        }
2736
0
        atomic_add_int(&sifa->refcount, 1);
2737
0
        return (sifa);
2738
0
      }
2739
2.53k
    }
2740
2.53k
  }
2741
  /*
2742
   * if we can't find one like that then we must look at all
2743
   * addresses bound to pick one at first preferable then
2744
   * secondly acceptable.
2745
   */
2746
2.53k
  starting_point = stcb->asoc.last_used_address;
2747
4.37k
 sctp_from_the_top:
2748
4.37k
  if (stcb->asoc.last_used_address == NULL) {
2749
2.53k
    start_at_beginning = 1;
2750
2.53k
    stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2751
2.53k
  }
2752
  /* search beginning with the last used address */
2753
8.75k
  for (laddr = stcb->asoc.last_used_address; laddr;
2754
4.37k
       laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2755
4.37k
    if (laddr->ifa == NULL) {
2756
      /* address has been removed */
2757
0
      continue;
2758
0
    }
2759
4.37k
    if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2760
      /* address is being deleted */
2761
0
      continue;
2762
0
    }
2763
4.37k
    sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2764
4.37k
    if (sifa == NULL)
2765
4.37k
      continue;
2766
0
    if (((non_asoc_addr_ok == 0) &&
2767
0
         (sctp_is_addr_restricted(stcb, sifa))) ||
2768
0
        (non_asoc_addr_ok &&
2769
0
         (sctp_is_addr_restricted(stcb, sifa)) &&
2770
0
         (!sctp_is_addr_pending(stcb, sifa)))) {
2771
      /* on the no-no list */
2772
0
      continue;
2773
0
    }
2774
0
    stcb->asoc.last_used_address = laddr;
2775
0
    atomic_add_int(&sifa->refcount, 1);
2776
0
    return (sifa);
2777
0
  }
2778
4.37k
  if (start_at_beginning == 0) {
2779
1.84k
    stcb->asoc.last_used_address = NULL;
2780
1.84k
    goto sctp_from_the_top;
2781
1.84k
  }
2782
  /* now try for any higher scope than the destination */
2783
2.53k
  stcb->asoc.last_used_address = starting_point;
2784
2.53k
  start_at_beginning = 0;
2785
4.37k
 sctp_from_the_top2:
2786
4.37k
  if (stcb->asoc.last_used_address == NULL) {
2787
2.53k
    start_at_beginning = 1;
2788
2.53k
    stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2789
2.53k
  }
2790
  /* search beginning with the last used address */
2791
8.75k
  for (laddr = stcb->asoc.last_used_address; laddr;
2792
4.37k
       laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2793
4.37k
    if (laddr->ifa == NULL) {
2794
      /* address has been removed */
2795
0
      continue;
2796
0
    }
2797
4.37k
    if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2798
      /* address is being deleted */
2799
0
      continue;
2800
0
    }
2801
4.37k
    sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2802
4.37k
               dest_is_priv, fam);
2803
4.37k
    if (sifa == NULL)
2804
4.37k
      continue;
2805
0
    if (((non_asoc_addr_ok == 0) &&
2806
0
         (sctp_is_addr_restricted(stcb, sifa))) ||
2807
0
        (non_asoc_addr_ok &&
2808
0
         (sctp_is_addr_restricted(stcb, sifa)) &&
2809
0
         (!sctp_is_addr_pending(stcb, sifa)))) {
2810
      /* on the no-no list */
2811
0
      continue;
2812
0
    }
2813
0
    stcb->asoc.last_used_address = laddr;
2814
0
    atomic_add_int(&sifa->refcount, 1);
2815
0
    return (sifa);
2816
0
  }
2817
4.37k
  if (start_at_beginning == 0) {
2818
1.84k
    stcb->asoc.last_used_address = NULL;
2819
1.84k
    goto sctp_from_the_top2;
2820
1.84k
  }
2821
2.53k
  return (NULL);
2822
4.37k
}
2823
2824
static struct sctp_ifa *
2825
sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2826
#if defined(__FreeBSD__) && !defined(__Userspace__)
2827
                                                 struct sctp_inpcb *inp,
2828
#else
2829
                                                 struct sctp_inpcb *inp SCTP_UNUSED,
2830
#endif
2831
                                                 struct sctp_tcb *stcb,
2832
                                                 int non_asoc_addr_ok,
2833
                                                 uint8_t dest_is_loop,
2834
                                                 uint8_t dest_is_priv,
2835
                                                 int addr_wanted,
2836
                                                 sa_family_t fam,
2837
                                                 sctp_route_t *ro)
2838
0
{
2839
0
  struct sctp_ifa *ifa, *sifa;
2840
0
  int num_eligible_addr = 0;
2841
0
#ifdef INET6
2842
#ifdef SCTP_EMBEDDED_V6_SCOPE
2843
  struct sockaddr_in6 sin6, lsa6;
2844
2845
  if (fam == AF_INET6) {
2846
    memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2847
#ifdef SCTP_KAME
2848
    (void)sa6_recoverscope(&sin6);
2849
#else
2850
    (void)in6_recoverscope(&sin6, &sin6.sin6_addr, NULL);
2851
#endif  /* SCTP_KAME */
2852
  }
2853
#endif  /* SCTP_EMBEDDED_V6_SCOPE */
2854
0
#endif  /* INET6 */
2855
0
  LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2856
#if defined(__FreeBSD__) && !defined(__Userspace__)
2857
#ifdef INET
2858
    if ((ifa->address.sa.sa_family == AF_INET) &&
2859
        (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2860
                          &ifa->address.sin.sin_addr) != 0)) {
2861
      continue;
2862
    }
2863
#endif
2864
#ifdef INET6
2865
    if ((ifa->address.sa.sa_family == AF_INET6) &&
2866
        (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2867
                          &ifa->address.sin6.sin6_addr) != 0)) {
2868
      continue;
2869
    }
2870
#endif
2871
#endif
2872
0
    if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2873
0
        (non_asoc_addr_ok == 0))
2874
0
      continue;
2875
0
    sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2876
0
              dest_is_priv, fam);
2877
0
    if (sifa == NULL)
2878
0
      continue;
2879
0
#ifdef INET6
2880
0
    if (fam == AF_INET6 &&
2881
0
        dest_is_loop &&
2882
0
        sifa->src_is_loop && sifa->src_is_priv) {
2883
      /* don't allow fe80::1 to be a src on loop ::1, we don't list it
2884
       * to the peer so we will get an abort.
2885
       */
2886
0
      continue;
2887
0
    }
2888
#ifdef SCTP_EMBEDDED_V6_SCOPE
2889
    if (fam == AF_INET6 &&
2890
        IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2891
        IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2892
      /* link-local <-> link-local must belong to the same scope. */
2893
      memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2894
#ifdef SCTP_KAME
2895
      (void)sa6_recoverscope(&lsa6);
2896
#else
2897
      (void)in6_recoverscope(&lsa6, &lsa6.sin6_addr, NULL);
2898
#endif  /* SCTP_KAME */
2899
      if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2900
        continue;
2901
      }
2902
    }
2903
#endif  /* SCTP_EMBEDDED_V6_SCOPE */
2904
0
#endif  /* INET6 */
2905
2906
0
#if defined(__FreeBSD__) || defined(__APPLE__) || defined(__Userspace__)
2907
    /* Check if the IPv6 address matches to next-hop.
2908
       In the mobile case, old IPv6 address may be not deleted
2909
       from the interface. Then, the interface has previous and
2910
       new addresses.  We should use one corresponding to the
2911
       next-hop.  (by micchie)
2912
     */
2913
0
#ifdef INET6
2914
0
    if (stcb && fam == AF_INET6 &&
2915
0
        sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2916
0
      if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) == 0) {
2917
0
        continue;
2918
0
      }
2919
0
    }
2920
0
#endif
2921
0
#ifdef INET
2922
    /* Avoid topologically incorrect IPv4 address */
2923
0
    if (stcb && fam == AF_INET &&
2924
0
        sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2925
0
      if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2926
0
        continue;
2927
0
      }
2928
0
    }
2929
0
#endif
2930
0
#endif
2931
0
    if (stcb) {
2932
0
      if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2933
0
        continue;
2934
0
      }
2935
0
      if (((non_asoc_addr_ok == 0) &&
2936
0
           (sctp_is_addr_restricted(stcb, sifa))) ||
2937
0
          (non_asoc_addr_ok &&
2938
0
           (sctp_is_addr_restricted(stcb, sifa)) &&
2939
0
           (!sctp_is_addr_pending(stcb, sifa)))) {
2940
        /*
2941
         * It is restricted for some reason..
2942
         * probably not yet added.
2943
         */
2944
0
        continue;
2945
0
      }
2946
0
    }
2947
0
    if (num_eligible_addr >= addr_wanted) {
2948
0
      return (sifa);
2949
0
    }
2950
0
    num_eligible_addr++;
2951
0
  }
2952
0
  return (NULL);
2953
0
}
2954
2955
static int
2956
sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2957
#if defined(__FreeBSD__) && !defined(__Userspace__)
2958
                                  struct sctp_inpcb *inp,
2959
#else
2960
                                  struct sctp_inpcb *inp SCTP_UNUSED,
2961
#endif
2962
          struct sctp_tcb *stcb,
2963
          int non_asoc_addr_ok,
2964
          uint8_t dest_is_loop,
2965
          uint8_t dest_is_priv,
2966
          sa_family_t fam)
2967
0
{
2968
0
  struct sctp_ifa *ifa, *sifa;
2969
0
  int num_eligible_addr = 0;
2970
2971
0
  LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2972
#if defined(__FreeBSD__) && !defined(__Userspace__)
2973
#ifdef INET
2974
    if ((ifa->address.sa.sa_family == AF_INET) &&
2975
        (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2976
                          &ifa->address.sin.sin_addr) != 0)) {
2977
      continue;
2978
    }
2979
#endif
2980
#ifdef INET6
2981
    if ((ifa->address.sa.sa_family == AF_INET6) &&
2982
        (stcb != NULL) &&
2983
        (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2984
                          &ifa->address.sin6.sin6_addr) != 0)) {
2985
      continue;
2986
    }
2987
#endif
2988
#endif
2989
0
    if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2990
0
        (non_asoc_addr_ok == 0)) {
2991
0
      continue;
2992
0
    }
2993
0
    sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2994
0
              dest_is_priv, fam);
2995
0
    if (sifa == NULL) {
2996
0
      continue;
2997
0
    }
2998
0
    if (stcb) {
2999
0
      if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
3000
0
        continue;
3001
0
      }
3002
0
      if (((non_asoc_addr_ok == 0) &&
3003
0
           (sctp_is_addr_restricted(stcb, sifa))) ||
3004
0
          (non_asoc_addr_ok &&
3005
0
           (sctp_is_addr_restricted(stcb, sifa)) &&
3006
0
           (!sctp_is_addr_pending(stcb, sifa)))) {
3007
        /*
3008
         * It is restricted for some reason..
3009
         * probably not yet added.
3010
         */
3011
0
        continue;
3012
0
      }
3013
0
    }
3014
0
    num_eligible_addr++;
3015
0
  }
3016
0
  return (num_eligible_addr);
3017
0
}
3018
3019
static struct sctp_ifa *
3020
sctp_choose_boundall(struct sctp_inpcb *inp,
3021
                     struct sctp_tcb *stcb,
3022
         struct sctp_nets *net,
3023
         sctp_route_t *ro,
3024
         uint32_t vrf_id,
3025
         uint8_t dest_is_priv,
3026
         uint8_t dest_is_loop,
3027
         int non_asoc_addr_ok,
3028
         sa_family_t fam)
3029
0
{
3030
0
  int cur_addr_num = 0, num_preferred = 0;
3031
0
  void *ifn;
3032
0
  struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
3033
0
  struct sctp_ifa *sctp_ifa, *sifa;
3034
0
  uint32_t ifn_index;
3035
0
  struct sctp_vrf *vrf;
3036
0
#ifdef INET
3037
0
  int retried = 0;
3038
0
#endif
3039
3040
  /*-
3041
   * For boundall we can use any address in the association.
3042
   * If non_asoc_addr_ok is set we can use any address (at least in
3043
   * theory). So we look for preferred addresses first. If we find one,
3044
   * we use it. Otherwise we next try to get an address on the
3045
   * interface, which we should be able to do (unless non_asoc_addr_ok
3046
   * is false and we are routed out that way). In these cases where we
3047
   * can't use the address of the interface we go through all the
3048
   * ifn's looking for an address we can use and fill that in. Punting
3049
   * means we send back address 0, which will probably cause problems
3050
   * actually since then IP will fill in the address of the route ifn,
3051
   * which means we probably already rejected it.. i.e. here comes an
3052
   * abort :-<.
3053
   */
3054
0
  vrf = sctp_find_vrf(vrf_id);
3055
0
  if (vrf == NULL)
3056
0
    return (NULL);
3057
3058
0
  ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
3059
0
  ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
3060
0
  SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
3061
0
  emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
3062
0
  if (sctp_ifn == NULL) {
3063
    /* ?? We don't have this guy ?? */
3064
0
    SCTPDBG(SCTP_DEBUG_OUTPUT2,"No ifn emit interface?\n");
3065
0
    goto bound_all_plan_b;
3066
0
  }
3067
0
  SCTPDBG(SCTP_DEBUG_OUTPUT2,"ifn_index:%d name:%s is emit interface\n",
3068
0
    ifn_index, sctp_ifn->ifn_name);
3069
3070
0
  if (net) {
3071
0
    cur_addr_num = net->indx_of_eligible_next_to_use;
3072
0
  }
3073
0
  num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
3074
0
                inp, stcb,
3075
0
                non_asoc_addr_ok,
3076
0
                dest_is_loop,
3077
0
                dest_is_priv, fam);
3078
0
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
3079
0
    num_preferred, sctp_ifn->ifn_name);
3080
0
  if (num_preferred == 0) {
3081
    /*
3082
     * no eligible addresses, we must use some other interface
3083
     * address if we can find one.
3084
     */
3085
0
    goto bound_all_plan_b;
3086
0
  }
3087
  /*
3088
   * Ok we have num_eligible_addr set with how many we can use, this
3089
   * may vary from call to call due to addresses being deprecated
3090
   * etc..
3091
   */
3092
0
  if (cur_addr_num >= num_preferred) {
3093
0
    cur_addr_num = 0;
3094
0
  }
3095
  /*
3096
   * select the nth address from the list (where cur_addr_num is the
3097
   * nth) and 0 is the first one, 1 is the second one etc...
3098
   */
3099
0
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
3100
3101
0
  sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3102
0
                                                                    dest_is_priv, cur_addr_num, fam, ro);
3103
3104
  /* if sctp_ifa is NULL something changed??, fall to plan b. */
3105
0
  if (sctp_ifa) {
3106
0
    atomic_add_int(&sctp_ifa->refcount, 1);
3107
0
    if (net) {
3108
      /* save off where the next one we will want */
3109
0
      net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3110
0
    }
3111
0
    return (sctp_ifa);
3112
0
  }
3113
  /*
3114
   * plan_b: Look at all interfaces and find a preferred address. If
3115
   * no preferred fall through to plan_c.
3116
   */
3117
0
 bound_all_plan_b:
3118
0
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
3119
0
  LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3120
0
    SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
3121
0
      sctp_ifn->ifn_name);
3122
0
    if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3123
      /* wrong base scope */
3124
0
      SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
3125
0
      continue;
3126
0
    }
3127
0
    if ((sctp_ifn == looked_at) && looked_at) {
3128
      /* already looked at this guy */
3129
0
      SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
3130
0
      continue;
3131
0
    }
3132
0
    num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok,
3133
0
                                                                  dest_is_loop, dest_is_priv, fam);
3134
0
    SCTPDBG(SCTP_DEBUG_OUTPUT2,
3135
0
      "Found ifn:%p %d preferred source addresses\n",
3136
0
      ifn, num_preferred);
3137
0
    if (num_preferred == 0) {
3138
      /* None on this interface. */
3139
0
      SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n");
3140
0
      continue;
3141
0
    }
3142
0
    SCTPDBG(SCTP_DEBUG_OUTPUT2,
3143
0
      "num preferred:%d on interface:%p cur_addr_num:%d\n",
3144
0
      num_preferred, (void *)sctp_ifn, cur_addr_num);
3145
3146
    /*
3147
     * Ok we have num_eligible_addr set with how many we can
3148
     * use, this may vary from call to call due to addresses
3149
     * being deprecated etc..
3150
     */
3151
0
    if (cur_addr_num >= num_preferred) {
3152
0
      cur_addr_num = 0;
3153
0
    }
3154
0
    sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3155
0
                                                                        dest_is_priv, cur_addr_num, fam, ro);
3156
0
    if (sifa == NULL)
3157
0
      continue;
3158
0
    if (net) {
3159
0
      net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3160
0
      SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3161
0
        cur_addr_num);
3162
0
      SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3163
0
      SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3164
0
      SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3165
0
      SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3166
0
    }
3167
0
    atomic_add_int(&sifa->refcount, 1);
3168
0
    return (sifa);
3169
0
  }
3170
0
#ifdef INET
3171
0
again_with_private_addresses_allowed:
3172
0
#endif
3173
  /* plan_c: do we have an acceptable address on the emit interface */
3174
0
  sifa = NULL;
3175
0
  SCTPDBG(SCTP_DEBUG_OUTPUT2,"Trying Plan C: find acceptable on interface\n");
3176
0
  if (emit_ifn == NULL) {
3177
0
    SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jump to Plan D - no emit_ifn\n");
3178
0
    goto plan_d;
3179
0
  }
3180
0
  LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3181
0
    SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa);
3182
#if defined(__FreeBSD__) && !defined(__Userspace__)
3183
#ifdef INET
3184
    if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3185
        (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3186
                          &sctp_ifa->address.sin.sin_addr) != 0)) {
3187
      SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jailed\n");
3188
      continue;
3189
    }
3190
#endif
3191
#ifdef INET6
3192
    if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3193
        (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3194
                          &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3195
      SCTPDBG(SCTP_DEBUG_OUTPUT2,"Jailed\n");
3196
      continue;
3197
    }
3198
#endif
3199
#endif
3200
0
    if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3201
0
        (non_asoc_addr_ok == 0)) {
3202
0
      SCTPDBG(SCTP_DEBUG_OUTPUT2,"Defer\n");
3203
0
      continue;
3204
0
    }
3205
0
    sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3206
0
               dest_is_priv, fam);
3207
0
    if (sifa == NULL) {
3208
0
      SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3209
0
      continue;
3210
0
    }
3211
0
    if (stcb) {
3212
0
      if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3213
0
        SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3214
0
        sifa = NULL;
3215
0
        continue;
3216
0
      }
3217
0
      if (((non_asoc_addr_ok == 0) &&
3218
0
           (sctp_is_addr_restricted(stcb, sifa))) ||
3219
0
          (non_asoc_addr_ok &&
3220
0
           (sctp_is_addr_restricted(stcb, sifa)) &&
3221
0
           (!sctp_is_addr_pending(stcb, sifa)))) {
3222
        /*
3223
         * It is restricted for some
3224
         * reason.. probably not yet added.
3225
         */
3226
0
        SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n");
3227
0
        sifa = NULL;
3228
0
        continue;
3229
0
      }
3230
0
    }
3231
0
    atomic_add_int(&sifa->refcount, 1);
3232
0
    goto out;
3233
0
  }
3234
0
 plan_d:
3235
  /*
3236
   * plan_d: We are in trouble. No preferred address on the emit
3237
   * interface. And not even a preferred address on all interfaces.
3238
   * Go out and see if we can find an acceptable address somewhere
3239
   * amongst all interfaces.
3240
   */
3241
0
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at);
3242
0
  LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3243
0
    if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3244
      /* wrong base scope */
3245
0
      continue;
3246
0
    }
3247
0
    LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3248
#if defined(__FreeBSD__) && !defined(__Userspace__)
3249
#ifdef INET
3250
      if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3251
          (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3252
                            &sctp_ifa->address.sin.sin_addr) != 0)) {
3253
        continue;
3254
      }
3255
#endif
3256
#ifdef INET6
3257
      if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3258
          (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3259
                            &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3260
        continue;
3261
      }
3262
#endif
3263
#endif
3264
0
      if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3265
0
          (non_asoc_addr_ok == 0))
3266
0
        continue;
3267
0
      sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3268
0
                 dest_is_loop,
3269
0
                 dest_is_priv, fam);
3270
0
      if (sifa == NULL)
3271
0
        continue;
3272
0
      if (stcb) {
3273
0
        if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3274
0
          sifa = NULL;
3275
0
          continue;
3276
0
        }
3277
0
        if (((non_asoc_addr_ok == 0) &&
3278
0
             (sctp_is_addr_restricted(stcb, sifa))) ||
3279
0
            (non_asoc_addr_ok &&
3280
0
             (sctp_is_addr_restricted(stcb, sifa)) &&
3281
0
             (!sctp_is_addr_pending(stcb, sifa)))) {
3282
          /*
3283
           * It is restricted for some
3284
           * reason.. probably not yet added.
3285
           */
3286
0
          sifa = NULL;
3287
0
          continue;
3288
0
        }
3289
0
      }
3290
0
      goto out;
3291
0
    }
3292
0
  }
3293
0
#ifdef INET
3294
0
  if (stcb) {
3295
0
    if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) {
3296
0
      stcb->asoc.scope.ipv4_local_scope = 1;
3297
0
      retried = 1;
3298
0
      goto again_with_private_addresses_allowed;
3299
0
    } else if (retried == 1) {
3300
0
      stcb->asoc.scope.ipv4_local_scope = 0;
3301
0
    }
3302
0
  }
3303
0
#endif
3304
0
out:
3305
0
#ifdef INET
3306
0
  if (sifa) {
3307
0
    if (retried == 1) {
3308
0
      LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3309
0
        if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3310
          /* wrong base scope */
3311
0
          continue;
3312
0
        }
3313
0
        LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3314
0
          struct sctp_ifa *tmp_sifa;
3315
3316
#if defined(__FreeBSD__) && !defined(__Userspace__)
3317
#ifdef INET
3318
          if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3319
              (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3320
                                &sctp_ifa->address.sin.sin_addr) != 0)) {
3321
            continue;
3322
          }
3323
#endif
3324
#ifdef INET6
3325
          if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3326
              (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3327
                                &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3328
            continue;
3329
          }
3330
#endif
3331
#endif
3332
0
          if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3333
0
              (non_asoc_addr_ok == 0))
3334
0
            continue;
3335
0
          tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3336
0
                                                 dest_is_loop,
3337
0
                                                 dest_is_priv, fam);
3338
0
          if (tmp_sifa == NULL) {
3339
0
            continue;
3340
0
          }
3341
0
          if (tmp_sifa == sifa) {
3342
0
            continue;
3343
0
          }
3344
0
          if (stcb) {
3345
0
            if (sctp_is_address_in_scope(tmp_sifa,
3346
0
                                         &stcb->asoc.scope, 0) == 0) {
3347
0
              continue;
3348
0
            }
3349
0
            if (((non_asoc_addr_ok == 0) &&
3350
0
                 (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3351
0
                (non_asoc_addr_ok &&
3352
0
                 (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3353
0
                 (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3354
              /*
3355
               * It is restricted for some
3356
               * reason.. probably not yet added.
3357
               */
3358
0
              continue;
3359
0
            }
3360
0
          }
3361
0
          if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3362
0
              (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3363
0
            sctp_add_local_addr_restricted(stcb, tmp_sifa);
3364
0
          }
3365
0
        }
3366
0
      }
3367
0
    }
3368
0
    atomic_add_int(&sifa->refcount, 1);
3369
0
  }
3370
0
#endif
3371
0
  return (sifa);
3372
0
}
3373
3374
/* tcb may be NULL */
3375
struct sctp_ifa *
3376
sctp_source_address_selection(struct sctp_inpcb *inp,
3377
            struct sctp_tcb *stcb,
3378
            sctp_route_t *ro,
3379
            struct sctp_nets *net,
3380
            int non_asoc_addr_ok, uint32_t vrf_id)
3381
2.53k
{
3382
2.53k
  struct sctp_ifa *answer;
3383
2.53k
  uint8_t dest_is_priv, dest_is_loop;
3384
2.53k
  sa_family_t fam;
3385
2.53k
#ifdef INET
3386
2.53k
  struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3387
2.53k
#endif
3388
2.53k
#ifdef INET6
3389
2.53k
  struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3390
2.53k
#endif
3391
3392
  /**
3393
   * Rules:
3394
   * - Find the route if needed, cache if I can.
3395
   * - Look at interface address in route, Is it in the bound list. If so we
3396
   *   have the best source.
3397
   * - If not we must rotate amongst the addresses.
3398
   *
3399
   * Caveats and issues
3400
   *
3401
   * Do we need to pay attention to scope. We can have a private address
3402
   * or a global address we are sourcing or sending to. So if we draw
3403
   * it out
3404
   * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3405
   * For V4
3406
   * ------------------------------------------
3407
   *      source     *      dest  *  result
3408
   * -----------------------------------------
3409
   * <a>  Private    *    Global  *  NAT
3410
   * -----------------------------------------
3411
   * <b>  Private    *    Private *  No problem
3412
   * -----------------------------------------
3413
   * <c>  Global     *    Private *  Huh, How will this work?
3414
   * -----------------------------------------
3415
   * <d>  Global     *    Global  *  No Problem
3416
   *------------------------------------------
3417
   * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3418
   * For V6
3419
   *------------------------------------------
3420
   *      source     *      dest  *  result
3421
   * -----------------------------------------
3422
   * <a>  Linklocal  *    Global  *
3423
   * -----------------------------------------
3424
   * <b>  Linklocal  * Linklocal  *  No problem
3425
   * -----------------------------------------
3426
   * <c>  Global     * Linklocal  *  Huh, How will this work?
3427
   * -----------------------------------------
3428
   * <d>  Global     *    Global  *  No Problem
3429
   *------------------------------------------
3430
   * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3431
   *
3432
   * And then we add to that what happens if there are multiple addresses
3433
   * assigned to an interface. Remember the ifa on a ifn is a linked
3434
   * list of addresses. So one interface can have more than one IP
3435
   * address. What happens if we have both a private and a global
3436
   * address? Do we then use context of destination to sort out which
3437
   * one is best? And what about NAT's sending P->G may get you a NAT
3438
   * translation, or should you select the G thats on the interface in
3439
   * preference.
3440
   *
3441
   * Decisions:
3442
   *
3443
   * - count the number of addresses on the interface.
3444
   * - if it is one, no problem except case <c>.
3445
   *   For <a> we will assume a NAT out there.
3446
   * - if there are more than one, then we need to worry about scope P
3447
   *   or G. We should prefer G -> G and P -> P if possible.
3448
   *   Then as a secondary fall back to mixed types G->P being a last
3449
   *   ditch one.
3450
   * - The above all works for bound all, but bound specific we need to
3451
   *   use the same concept but instead only consider the bound
3452
   *   addresses. If the bound set is NOT assigned to the interface then
3453
   *   we must use rotation amongst the bound addresses..
3454
   */
3455
#if defined(__FreeBSD__) && !defined(__Userspace__)
3456
  if (ro->ro_nh == NULL) {
3457
#else
3458
2.53k
  if (ro->ro_rt == NULL) {
3459
0
#endif
3460
    /*
3461
     * Need a route to cache.
3462
     */
3463
0
    SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
3464
0
  }
3465
#if defined(__FreeBSD__) && !defined(__Userspace__)
3466
  if (ro->ro_nh == NULL) {
3467
#else
3468
2.53k
  if (ro->ro_rt == NULL) {
3469
0
#endif
3470
0
    return (NULL);
3471
0
  }
3472
#if defined(_WIN32)
3473
  /* On Windows the sa_family is U_SHORT or ADDRESS_FAMILY */
3474
  fam = (sa_family_t)ro->ro_dst.sa_family;
3475
#else
3476
2.53k
  fam = ro->ro_dst.sa_family;
3477
2.53k
#endif
3478
2.53k
  dest_is_priv = dest_is_loop = 0;
3479
  /* Setup our scopes for the destination */
3480
2.53k
  switch (fam) {
3481
0
#ifdef INET
3482
1.24k
  case AF_INET:
3483
    /* Scope based on outbound address */
3484
1.24k
    if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3485
670
      dest_is_loop = 1;
3486
670
      if (net != NULL) {
3487
        /* mark it as local */
3488
670
        net->addr_is_local = 1;
3489
670
      }
3490
670
    } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3491
49
      dest_is_priv = 1;
3492
49
    }
3493
1.24k
    break;
3494
0
#endif
3495
0
#ifdef INET6
3496
1.28k
  case AF_INET6:
3497
    /* Scope based on outbound address */
3498
#if defined(_WIN32)
3499
    if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr)) {
3500
#else
3501
1.28k
    if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3502
1.28k
        SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3503
2
#endif
3504
      /*
3505
       * If the address is a loopback address, which
3506
       * consists of "::1" OR "fe80::1%lo0", we are loopback
3507
       * scope. But we don't use dest_is_priv (link local
3508
       * addresses).
3509
       */
3510
2
      dest_is_loop = 1;
3511
2
      if (net != NULL) {
3512
        /* mark it as local */
3513
2
        net->addr_is_local = 1;
3514
2
      }
3515
1.28k
    } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3516
619
      dest_is_priv = 1;
3517
619
    }
3518
1.28k
    break;
3519
2.53k
#endif
3520
2.53k
  }
3521
2.53k
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3522
2.53k
  SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3523
2.53k
  SCTP_IPI_ADDR_RLOCK();
3524
2.53k
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3525
    /*
3526
     * Bound all case
3527
     */
3528
0
    answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3529
0
                dest_is_priv, dest_is_loop,
3530
0
                non_asoc_addr_ok, fam);
3531
0
    SCTP_IPI_ADDR_RUNLOCK();
3532
0
    return (answer);
3533
0
  }
3534
  /*
3535
   * Subset bound case
3536
   */
3537
2.53k
  if (stcb) {
3538
2.53k
    answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3539
2.53k
              vrf_id, dest_is_priv,
3540
2.53k
              dest_is_loop,
3541
2.53k
              non_asoc_addr_ok, fam);
3542
2.53k
  } else {
3543
0
    answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3544
0
                   non_asoc_addr_ok,
3545
0
                   dest_is_priv,
3546
0
                   dest_is_loop, fam);
3547
0
  }
3548
2.53k
  SCTP_IPI_ADDR_RUNLOCK();
3549
2.53k
  return (answer);
3550
2.53k
}
3551
3552
static bool
3553
sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3554
0
{
3555
#if defined(_WIN32)
3556
  WSACMSGHDR cmh;
3557
#else
3558
0
  struct cmsghdr cmh;
3559
0
#endif
3560
0
  struct sctp_sndinfo sndinfo;
3561
0
  struct sctp_prinfo prinfo;
3562
0
  struct sctp_authinfo authinfo;
3563
0
  int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3564
0
  bool found;
3565
3566
  /*
3567
   * Independent of how many mbufs, find the c_type inside the control
3568
   * structure and copy out the data.
3569
   */
3570
0
  found = false;
3571
0
  tot_len = SCTP_BUF_LEN(control);
3572
0
  for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3573
0
    rem_len = tot_len - off;
3574
0
    if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3575
      /* There is not enough room for one more. */
3576
0
      return (found);
3577
0
    }
3578
0
    m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3579
0
    if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3580
      /* We dont't have a complete CMSG header. */
3581
0
      return (found);
3582
0
    }
3583
0
    if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3584
      /* We don't have the complete CMSG. */
3585
0
      return (found);
3586
0
    }
3587
0
    cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3588
0
    cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3589
0
    if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3590
0
        ((c_type == cmh.cmsg_type) ||
3591
0
         ((c_type == SCTP_SNDRCV) &&
3592
0
          ((cmh.cmsg_type == SCTP_SNDINFO) ||
3593
0
           (cmh.cmsg_type == SCTP_PRINFO) ||
3594
0
           (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3595
0
      if (c_type == cmh.cmsg_type) {
3596
0
        if (cpsize > INT_MAX) {
3597
0
          return (found);
3598
0
        }
3599
0
        if (cmsg_data_len < (int)cpsize) {
3600
0
          return (found);
3601
0
        }
3602
        /* It is exactly what we want. Copy it out. */
3603
0
        m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data);
3604
0
        return (1);
3605
0
      } else {
3606
0
        struct sctp_sndrcvinfo *sndrcvinfo;
3607
3608
0
        sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3609
0
        if (!found) {
3610
0
          if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3611
0
            return (found);
3612
0
          }
3613
0
          memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3614
0
        }
3615
0
        switch (cmh.cmsg_type) {
3616
0
        case SCTP_SNDINFO:
3617
0
          if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) {
3618
0
            return (found);
3619
0
          }
3620
0
          m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3621
0
          sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3622
0
          sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3623
0
          sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3624
0
          sndrcvinfo->sinfo_context = sndinfo.snd_context;
3625
0
          sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3626
0
          break;
3627
0
        case SCTP_PRINFO:
3628
0
          if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) {
3629
0
            return (found);
3630
0
          }
3631
0
          m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3632
0
          if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) {
3633
0
            sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3634
0
          } else {
3635
0
            sndrcvinfo->sinfo_timetolive = 0;
3636
0
          }
3637
0
          sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3638
0
          break;
3639
0
        case SCTP_AUTHINFO:
3640
0
          if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) {
3641
0
            return (found);
3642
0
          }
3643
0
          m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3644
0
          sndrcvinfo->sinfo_keynumber_valid = 1;
3645
0
          sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber;
3646
0
          break;
3647
0
        default:
3648
0
          return (found);
3649
0
        }
3650
0
        found = true;
3651
0
      }
3652
0
    }
3653
0
  }
3654
0
  return (found);
3655
0
}
3656
3657
static int
3658
sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3659
0
{
3660
#if defined(_WIN32)
3661
  WSACMSGHDR cmh;
3662
#else
3663
0
  struct cmsghdr cmh;
3664
0
#endif
3665
0
  struct sctp_initmsg initmsg;
3666
0
#ifdef INET
3667
0
  struct sockaddr_in sin;
3668
0
#endif
3669
0
#ifdef INET6
3670
0
  struct sockaddr_in6 sin6;
3671
0
#endif
3672
0
  int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3673
3674
0
  tot_len = SCTP_BUF_LEN(control);
3675
0
  for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3676
0
    rem_len = tot_len - off;
3677
0
    if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3678
      /* There is not enough room for one more. */
3679
0
      *error = EINVAL;
3680
0
      return (1);
3681
0
    }
3682
0
    m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3683
0
    if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3684
      /* We dont't have a complete CMSG header. */
3685
0
      *error = EINVAL;
3686
0
      return (1);
3687
0
    }
3688
0
    if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3689
      /* We don't have the complete CMSG. */
3690
0
      *error = EINVAL;
3691
0
      return (1);
3692
0
    }
3693
0
    cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3694
0
    cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3695
0
    if (cmh.cmsg_level == IPPROTO_SCTP) {
3696
0
      switch (cmh.cmsg_type) {
3697
0
      case SCTP_INIT:
3698
0
        if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) {
3699
0
          *error = EINVAL;
3700
0
          return (1);
3701
0
        }
3702
0
        m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3703
0
        if (initmsg.sinit_max_attempts)
3704
0
          stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3705
0
        if (initmsg.sinit_num_ostreams)
3706
0
          stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3707
0
        if (initmsg.sinit_max_instreams)
3708
0
          stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3709
0
        if (initmsg.sinit_max_init_timeo)
3710
0
          stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3711
0
        if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3712
0
          struct sctp_stream_out *tmp_str;
3713
0
          unsigned int i;
3714
#if defined(SCTP_DETAILED_STR_STATS)
3715
          int j;
3716
#endif
3717
3718
          /* Default is NOT correct */
3719
0
          SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3720
0
            stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3721
0
          SCTP_TCB_UNLOCK(stcb);
3722
0
          SCTP_MALLOC(tmp_str,
3723
0
                      struct sctp_stream_out *,
3724
0
                      (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3725
0
                      SCTP_M_STRMO);
3726
0
          SCTP_TCB_LOCK(stcb);
3727
0
          if (tmp_str != NULL) {
3728
0
            SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3729
0
            stcb->asoc.strmout = tmp_str;
3730
0
            stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3731
0
          } else {
3732
0
            stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3733
0
          }
3734
0
          for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3735
0
            TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3736
0
            stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
3737
0
            stcb->asoc.strmout[i].chunks_on_queues = 0;
3738
#if defined(SCTP_DETAILED_STR_STATS)
3739
            for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
3740
              stcb->asoc.strmout[i].abandoned_sent[j] = 0;
3741
              stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
3742
            }
3743
#else
3744
0
            stcb->asoc.strmout[i].abandoned_sent[0] = 0;
3745
0
            stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
3746
0
#endif
3747
0
            stcb->asoc.strmout[i].next_mid_ordered = 0;
3748
0
            stcb->asoc.strmout[i].next_mid_unordered = 0;
3749
0
            stcb->asoc.strmout[i].sid = i;
3750
0
            stcb->asoc.strmout[i].last_msg_incomplete = 0;
3751
0
            stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING;
3752
0
          }
3753
0
        }
3754
0
        break;
3755
0
#ifdef INET
3756
0
      case SCTP_DSTADDRV4:
3757
0
        if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3758
0
          *error = EINVAL;
3759
0
          return (1);
3760
0
        }
3761
0
        memset(&sin, 0, sizeof(struct sockaddr_in));
3762
0
        sin.sin_family = AF_INET;
3763
#ifdef HAVE_SIN_LEN
3764
        sin.sin_len = sizeof(struct sockaddr_in);
3765
#endif
3766
0
        sin.sin_port = stcb->rport;
3767
0
        m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3768
0
        if (in_broadcast(sin.sin_addr) ||
3769
0
            IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3770
0
          *error = EINVAL;
3771
0
          return (1);
3772
0
        }
3773
0
        if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3774
0
                                 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3775
0
          *error = ENOBUFS;
3776
0
          return (1);
3777
0
        }
3778
0
        break;
3779
0
#endif
3780
0
#ifdef INET6
3781
0
      case SCTP_DSTADDRV6:
3782
0
        if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3783
0
          *error = EINVAL;
3784
0
          return (1);
3785
0
        }
3786
0
        memset(&sin6, 0, sizeof(struct sockaddr_in6));
3787
0
        sin6.sin6_family = AF_INET6;
3788
#ifdef HAVE_SIN6_LEN
3789
        sin6.sin6_len = sizeof(struct sockaddr_in6);
3790
#endif
3791
0
        sin6.sin6_port = stcb->rport;
3792
0
        m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3793
0
        if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3794
0
            IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3795
0
          *error = EINVAL;
3796
0
          return (1);
3797
0
        }
3798
0
#ifdef INET
3799
0
        if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3800
0
          in6_sin6_2_sin(&sin, &sin6);
3801
0
          if (in_broadcast(sin.sin_addr) ||
3802
0
              IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3803
0
            *error = EINVAL;
3804
0
            return (1);
3805
0
          }
3806
0
          if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3807
0
                                   SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3808
0
            *error = ENOBUFS;
3809
0
            return (1);
3810
0
          }
3811
0
        } else
3812
0
#endif
3813
0
          if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port,
3814
0
                                   SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3815
0
            *error = ENOBUFS;
3816
0
            return (1);
3817
0
          }
3818
0
        break;
3819
0
#endif
3820
0
      default:
3821
0
        break;
3822
0
      }
3823
0
    }
3824
0
  }
3825
0
  return (0);
3826
0
}
3827
3828
#if defined(INET) || defined(INET6)
3829
static struct sctp_tcb *
3830
sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3831
                           uint16_t port,
3832
                           struct mbuf *control,
3833
                           struct sctp_nets **net_p,
3834
                           int *error)
3835
0
{
3836
#if defined(_WIN32)
3837
  WSACMSGHDR cmh;
3838
#else
3839
0
  struct cmsghdr cmh;
3840
0
#endif
3841
0
  struct sctp_tcb *stcb;
3842
0
  struct sockaddr *addr;
3843
0
#ifdef INET
3844
0
  struct sockaddr_in sin;
3845
0
#endif
3846
0
#ifdef INET6
3847
0
  struct sockaddr_in6 sin6;
3848
0
#endif
3849
0
  int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3850
3851
0
  tot_len = SCTP_BUF_LEN(control);
3852
0
  for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3853
0
    rem_len = tot_len - off;
3854
0
    if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3855
      /* There is not enough room for one more. */
3856
0
      *error = EINVAL;
3857
0
      return (NULL);
3858
0
    }
3859
0
    m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3860
0
    if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3861
      /* We dont't have a complete CMSG header. */
3862
0
      *error = EINVAL;
3863
0
      return (NULL);
3864
0
    }
3865
0
    if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3866
      /* We don't have the complete CMSG. */
3867
0
      *error = EINVAL;
3868
0
      return (NULL);
3869
0
    }
3870
0
    cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3871
0
    cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3872
0
    if (cmh.cmsg_level == IPPROTO_SCTP) {
3873
0
      switch (cmh.cmsg_type) {
3874
0
#ifdef INET
3875
0
      case SCTP_DSTADDRV4:
3876
0
        if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3877
0
          *error = EINVAL;
3878
0
          return (NULL);
3879
0
        }
3880
0
        memset(&sin, 0, sizeof(struct sockaddr_in));
3881
0
        sin.sin_family = AF_INET;
3882
#ifdef HAVE_SIN_LEN
3883
        sin.sin_len = sizeof(struct sockaddr_in);
3884
#endif
3885
0
        sin.sin_port = port;
3886
0
        m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3887
0
        addr = (struct sockaddr *)&sin;
3888
0
        break;
3889
0
#endif
3890
0
#ifdef INET6
3891
0
      case SCTP_DSTADDRV6:
3892
0
        if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3893
0
          *error = EINVAL;
3894
0
          return (NULL);
3895
0
        }
3896
0
        memset(&sin6, 0, sizeof(struct sockaddr_in6));
3897
0
        sin6.sin6_family = AF_INET6;
3898
#ifdef HAVE_SIN6_LEN
3899
        sin6.sin6_len = sizeof(struct sockaddr_in6);
3900
#endif
3901
0
        sin6.sin6_port = port;
3902
0
        m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3903
0
#ifdef INET
3904
0
        if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3905
0
          in6_sin6_2_sin(&sin, &sin6);
3906
0
          addr = (struct sockaddr *)&sin;
3907
0
        } else
3908
0
#endif
3909
0
          addr = (struct sockaddr *)&sin6;
3910
0
        break;
3911
0
#endif
3912
0
      default:
3913
0
        addr = NULL;
3914
0
        break;
3915
0
      }
3916
0
      if (addr) {
3917
0
        stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3918
0
        if (stcb != NULL) {
3919
0
          return (stcb);
3920
0
        }
3921
0
      }
3922
0
    }
3923
0
  }
3924
0
  return (NULL);
3925
0
}
3926
#endif
3927
3928
static struct mbuf *
3929
sctp_add_cookie(struct mbuf *init, int init_offset,
3930
    struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature)
3931
758
{
3932
758
  struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3933
758
  struct sctp_state_cookie *stc;
3934
758
  struct sctp_paramhdr *ph;
3935
758
  uint16_t cookie_sz;
3936
3937
758
  mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3938
758
              sizeof(struct sctp_paramhdr)), 0,
3939
758
             M_NOWAIT, 1, MT_DATA);
3940
758
  if (mret == NULL) {
3941
0
    return (NULL);
3942
0
  }
3943
758
  copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT);
3944
758
  if (copy_init == NULL) {
3945
0
    sctp_m_freem(mret);
3946
0
    return (NULL);
3947
0
  }
3948
#ifdef SCTP_MBUF_LOGGING
3949
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3950
    sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY);
3951
  }
3952
#endif
3953
758
  copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3954
758
      M_NOWAIT);
3955
758
  if (copy_initack == NULL) {
3956
0
    sctp_m_freem(mret);
3957
0
    sctp_m_freem(copy_init);
3958
0
    return (NULL);
3959
0
  }
3960
#ifdef SCTP_MBUF_LOGGING
3961
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3962
    sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY);
3963
  }
3964
#endif
3965
  /* easy side we just drop it on the end */
3966
758
  ph = mtod(mret, struct sctp_paramhdr *);
3967
758
  SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3968
758
      sizeof(struct sctp_paramhdr);
3969
758
  stc = (struct sctp_state_cookie *)((caddr_t)ph +
3970
758
      sizeof(struct sctp_paramhdr));
3971
758
  ph->param_type = htons(SCTP_STATE_COOKIE);
3972
758
  ph->param_length = 0; /* fill in at the end */
3973
  /* Fill in the stc cookie data */
3974
758
  memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3975
3976
  /* tack the INIT and then the INIT-ACK onto the chain */
3977
758
  cookie_sz = 0;
3978
758
  for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3979
758
    cookie_sz += SCTP_BUF_LEN(m_at);
3980
758
    if (SCTP_BUF_NEXT(m_at) == NULL) {
3981
758
      SCTP_BUF_NEXT(m_at) = copy_init;
3982
758
      break;
3983
758
    }
3984
758
  }
3985
2.36k
  for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3986
2.36k
    cookie_sz += SCTP_BUF_LEN(m_at);
3987
2.36k
    if (SCTP_BUF_NEXT(m_at) == NULL) {
3988
758
      SCTP_BUF_NEXT(m_at) = copy_initack;
3989
758
      break;
3990
758
    }
3991
2.36k
  }
3992
265k
  for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3993
265k
    cookie_sz += SCTP_BUF_LEN(m_at);
3994
265k
    if (SCTP_BUF_NEXT(m_at) == NULL) {
3995
758
      break;
3996
758
    }
3997
265k
  }
3998
758
  sig = sctp_get_mbuf_for_msg(SCTP_SIGNATURE_SIZE, 0, M_NOWAIT, 1, MT_DATA);
3999
758
  if (sig == NULL) {
4000
    /* no space, so free the entire chain */
4001
0
    sctp_m_freem(mret);
4002
0
    return (NULL);
4003
0
  }
4004
758
  SCTP_BUF_NEXT(m_at) = sig;
4005
758
  SCTP_BUF_LEN(sig) = SCTP_SIGNATURE_SIZE;
4006
758
  cookie_sz += SCTP_SIGNATURE_SIZE;
4007
758
  ph->param_length = htons(cookie_sz);
4008
758
  *signature = (uint8_t *)mtod(sig, caddr_t);
4009
758
  memset(*signature, 0, SCTP_SIGNATURE_SIZE);
4010
758
  return (mret);
4011
758
}
4012
4013
static uint8_t
4014
sctp_get_ect(struct sctp_tcb *stcb)
4015
7.18k
{
4016
7.18k
  if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) {
4017
6.87k
    return (SCTP_ECT0_BIT);
4018
6.87k
  } else {
4019
313
    return (0);
4020
313
  }
4021
7.18k
}
4022
4023
#if defined(INET) || defined(INET6)
4024
static void
4025
sctp_handle_no_route(struct sctp_tcb *stcb,
4026
                     struct sctp_nets *net,
4027
                     int so_locked)
4028
2.53k
{
4029
2.53k
  SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
4030
4031
2.53k
  if (net) {
4032
2.53k
    SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
4033
2.53k
    SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
4034
2.53k
    if (net->dest_state & SCTP_ADDR_CONFIRMED) {
4035
0
      if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
4036
0
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net);
4037
0
        sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
4038
0
                              stcb, 0,
4039
0
                              (void *)net,
4040
0
                              so_locked);
4041
0
        net->dest_state &= ~SCTP_ADDR_REACHABLE;
4042
0
        net->dest_state &= ~SCTP_ADDR_PF;
4043
0
      }
4044
0
    }
4045
2.53k
    if (stcb) {
4046
2.53k
      if (net == stcb->asoc.primary_destination) {
4047
        /* need a new primary */
4048
1.17k
        struct sctp_nets *alt;
4049
4050
1.17k
        alt = sctp_find_alternate_net(stcb, net, 0);
4051
1.17k
        if (alt != net) {
4052
1.17k
          if (stcb->asoc.alternate) {
4053
852
            sctp_free_remote_addr(stcb->asoc.alternate);
4054
852
          }
4055
1.17k
          stcb->asoc.alternate = alt;
4056
1.17k
          atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
4057
1.17k
          if (net->ro._s_addr) {
4058
0
            sctp_free_ifa(net->ro._s_addr);
4059
0
            net->ro._s_addr = NULL;
4060
0
          }
4061
1.17k
          net->src_addr_selected = 0;
4062
1.17k
        }
4063
1.17k
      }
4064
2.53k
    }
4065
2.53k
  }
4066
2.53k
}
4067
#endif
4068
4069
static int
4070
sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
4071
    struct sctp_tcb *stcb,  /* may be NULL */
4072
    struct sctp_nets *net,
4073
    struct sockaddr *to,
4074
    struct mbuf *m,
4075
    uint32_t auth_offset,
4076
    struct sctp_auth_chunk *auth,
4077
    uint16_t auth_keyid,
4078
    int nofragment_flag,
4079
    int ecn_ok,
4080
    int out_of_asoc_ok,
4081
    uint16_t src_port,
4082
    uint16_t dest_port,
4083
    uint32_t v_tag,
4084
    uint16_t port,
4085
    union sctp_sockstore *over_addr,
4086
#if defined(__FreeBSD__) && !defined(__Userspace__)
4087
    uint8_t mflowtype, uint32_t mflowid,
4088
#endif
4089
    bool use_zero_crc,
4090
    int so_locked)
4091
/* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
4092
54.9k
{
4093
  /**
4094
   * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
4095
   * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
4096
   * - fill in the HMAC digest of any AUTH chunk in the packet.
4097
   * - calculate and fill in the SCTP checksum.
4098
   * - prepend an IP address header.
4099
   * - if boundall use INADDR_ANY.
4100
   * - if boundspecific do source address selection.
4101
   * - set fragmentation option for ipV4.
4102
   * - On return from IP output, check/adjust mtu size of output
4103
   *   interface and smallest_mtu size as well.
4104
   */
4105
  /* Will need ifdefs around this */
4106
54.9k
  struct mbuf *newm;
4107
54.9k
  struct sctphdr *sctphdr;
4108
54.9k
  int packet_length;
4109
54.9k
  int ret;
4110
54.9k
#if defined(INET) || defined(INET6)
4111
54.9k
  uint32_t vrf_id;
4112
54.9k
#endif
4113
54.9k
#if defined(INET) || defined(INET6)
4114
54.9k
  struct mbuf *o_pak;
4115
54.9k
  sctp_route_t *ro = NULL;
4116
54.9k
  struct udphdr *udp = NULL;
4117
54.9k
#endif
4118
54.9k
  uint8_t tos_value;
4119
#if defined(__APPLE__) && !defined(__Userspace__)
4120
  struct socket *so = NULL;
4121
#endif
4122
4123
#if defined(__APPLE__) && !defined(__Userspace__)
4124
  if (so_locked) {
4125
    sctp_lock_assert(SCTP_INP_SO(inp));
4126
    SCTP_TCB_LOCK_ASSERT(stcb);
4127
  } else {
4128
    sctp_unlock_assert(SCTP_INP_SO(inp));
4129
  }
4130
#endif
4131
54.9k
  if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
4132
691
    SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4133
691
    sctp_m_freem(m);
4134
691
    return (EFAULT);
4135
691
  }
4136
54.2k
#if defined(INET) || defined(INET6)
4137
54.2k
  if (stcb) {
4138
53.4k
    vrf_id = stcb->asoc.vrf_id;
4139
53.4k
  } else {
4140
758
    vrf_id = inp->def_vrf_id;
4141
758
  }
4142
54.2k
#endif
4143
  /* fill in the HMAC digest for any AUTH chunk in the packet */
4144
54.2k
  if ((auth != NULL) && (stcb != NULL)) {
4145
554
    sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
4146
554
  }
4147
4148
54.2k
  if (net) {
4149
53.4k
    tos_value = net->dscp;
4150
53.4k
  } else if (stcb) {
4151
0
    tos_value = stcb->asoc.default_dscp;
4152
758
  } else {
4153
758
    tos_value = inp->sctp_ep.default_dscp;
4154
758
  }
4155
4156
54.2k
  switch (to->sa_family) {
4157
0
#ifdef INET
4158
1.24k
  case AF_INET:
4159
1.24k
  {
4160
1.24k
    struct ip *ip = NULL;
4161
1.24k
    sctp_route_t iproute;
4162
1.24k
    int len;
4163
4164
1.24k
    len = SCTP_MIN_V4_OVERHEAD;
4165
1.24k
    if (port) {
4166
0
      len += sizeof(struct udphdr);
4167
0
    }
4168
1.24k
    newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4169
1.24k
    if (newm == NULL) {
4170
0
      sctp_m_freem(m);
4171
0
      SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4172
0
      return (ENOMEM);
4173
0
    }
4174
1.24k
    SCTP_ALIGN_TO_END(newm, len);
4175
1.24k
    SCTP_BUF_LEN(newm) = len;
4176
1.24k
    SCTP_BUF_NEXT(newm) = m;
4177
1.24k
    m = newm;
4178
#if defined(__FreeBSD__) && !defined(__Userspace__)
4179
    if (net != NULL) {
4180
      m->m_pkthdr.flowid = net->flowid;
4181
      M_HASHTYPE_SET(m, net->flowtype);
4182
    } else {
4183
      m->m_pkthdr.flowid = mflowid;
4184
      M_HASHTYPE_SET(m, mflowtype);
4185
    }
4186
#endif
4187
1.24k
    packet_length = sctp_calculate_len(m);
4188
1.24k
    ip = mtod(m, struct ip *);
4189
1.24k
    ip->ip_v = IPVERSION;
4190
1.24k
    ip->ip_hl = (sizeof(struct ip) >> 2);
4191
1.24k
    if (tos_value == 0) {
4192
      /*
4193
       * This means especially, that it is not set at the
4194
       * SCTP layer. So use the value from the IP layer.
4195
       */
4196
1.24k
      tos_value = inp->ip_inp.inp.inp_ip_tos;
4197
1.24k
    }
4198
1.24k
    tos_value &= 0xfc;
4199
1.24k
    if (ecn_ok) {
4200
2
      tos_value |= sctp_get_ect(stcb);
4201
2
    }
4202
1.24k
    if ((nofragment_flag) && (port == 0)) {
4203
#if defined(__FreeBSD__) && !defined(__Userspace__)
4204
      ip->ip_off = htons(IP_DF);
4205
#elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__)
4206
      ip->ip_off = IP_DF;
4207
#else
4208
611
      ip->ip_off = htons(IP_DF);
4209
611
#endif
4210
630
    } else {
4211
#if defined(__FreeBSD__) && !defined(__Userspace__)
4212
      ip->ip_off = htons(0);
4213
#else
4214
630
      ip->ip_off = 0;
4215
630
#endif
4216
630
    }
4217
1.24k
#if defined(__Userspace__)
4218
1.24k
    ip->ip_id = htons(SCTP_IP_ID(inp)++);
4219
#elif defined(__FreeBSD__)
4220
    /* FreeBSD has a function for ip_id's */
4221
    ip_fillid(ip, V_ip_random_id);
4222
#elif defined(__APPLE__)
4223
#if RANDOM_IP_ID
4224
    ip->ip_id = ip_randomid();
4225
#else
4226
    ip->ip_id = htons(ip_id++);
4227
#endif
4228
#else
4229
    ip->ip_id = SCTP_IP_ID(inp)++;
4230
#endif
4231
4232
1.24k
    ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
4233
#if defined(__FreeBSD__) && !defined(__Userspace__)
4234
    ip->ip_len = htons(packet_length);
4235
#else
4236
1.24k
    ip->ip_len = packet_length;
4237
1.24k
#endif
4238
1.24k
    ip->ip_tos = tos_value;
4239
1.24k
    if (port) {
4240
0
      ip->ip_p = IPPROTO_UDP;
4241
1.24k
    } else {
4242
1.24k
      ip->ip_p = IPPROTO_SCTP;
4243
1.24k
    }
4244
1.24k
    ip->ip_sum = 0;
4245
1.24k
    if (net == NULL) {
4246
0
      ro = &iproute;
4247
0
      memset(&iproute, 0, sizeof(iproute));
4248
#ifdef HAVE_SA_LEN
4249
      memcpy(&ro->ro_dst, to, to->sa_len);
4250
#else
4251
0
      memcpy(&ro->ro_dst, to, sizeof(struct sockaddr_in));
4252
0
#endif
4253
1.24k
    } else {
4254
1.24k
      ro = (sctp_route_t *)&net->ro;
4255
1.24k
    }
4256
    /* Now the address selection part */
4257
1.24k
    ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4258
4259
    /* call the routine to select the src address */
4260
1.24k
    if (net && out_of_asoc_ok == 0) {
4261
1.24k
      if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) {
4262
0
        sctp_free_ifa(net->ro._s_addr);
4263
0
        net->ro._s_addr = NULL;
4264
0
        net->src_addr_selected = 0;
4265
#if defined(__FreeBSD__) && !defined(__Userspace__)
4266
        RO_NHFREE(ro);
4267
#else
4268
0
        if (ro->ro_rt) {
4269
0
          RTFREE(ro->ro_rt);
4270
0
          ro->ro_rt = NULL;
4271
0
        }
4272
0
#endif
4273
0
      }
4274
1.24k
      if (net->src_addr_selected == 0) {
4275
        /* Cache the source address */
4276
1.24k
        net->ro._s_addr = sctp_source_address_selection(inp,stcb,
4277
1.24k
                    ro, net, 0,
4278
1.24k
                    vrf_id);
4279
1.24k
        net->src_addr_selected = 1;
4280
1.24k
      }
4281
1.24k
      if (net->ro._s_addr == NULL) {
4282
        /* No route to host */
4283
1.24k
        net->src_addr_selected = 0;
4284
1.24k
        sctp_handle_no_route(stcb, net, so_locked);
4285
1.24k
        SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4286
1.24k
        sctp_m_freem(m);
4287
1.24k
        return (EHOSTUNREACH);
4288
1.24k
      }
4289
0
      ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4290
0
    } else {
4291
0
      if (over_addr == NULL) {
4292
0
        struct sctp_ifa *_lsrc;
4293
4294
0
        _lsrc = sctp_source_address_selection(inp, stcb, ro,
4295
0
                                              net,
4296
0
                                              out_of_asoc_ok,
4297
0
                                              vrf_id);
4298
0
        if (_lsrc == NULL) {
4299
0
          sctp_handle_no_route(stcb, net, so_locked);
4300
0
          SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4301
0
          sctp_m_freem(m);
4302
0
          return (EHOSTUNREACH);
4303
0
        }
4304
0
        ip->ip_src = _lsrc->address.sin.sin_addr;
4305
0
        sctp_free_ifa(_lsrc);
4306
0
      } else {
4307
0
        ip->ip_src = over_addr->sin.sin_addr;
4308
0
        SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4309
0
      }
4310
0
    }
4311
0
    if (port) {
4312
0
      if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4313
0
        sctp_handle_no_route(stcb, net, so_locked);
4314
0
        SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4315
0
        sctp_m_freem(m);
4316
0
        return (EHOSTUNREACH);
4317
0
      }
4318
0
      udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4319
0
      udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4320
0
      udp->uh_dport = port;
4321
0
      udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip)));
4322
#if !defined(__Userspace__)
4323
#if defined(__FreeBSD__)
4324
      if (V_udp_cksum) {
4325
        udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4326
      } else {
4327
        udp->uh_sum = 0;
4328
      }
4329
#else
4330
      udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4331
#endif
4332
#else
4333
0
      udp->uh_sum = 0;
4334
0
#endif
4335
0
      sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4336
0
    } else {
4337
0
      sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4338
0
    }
4339
4340
0
    sctphdr->src_port = src_port;
4341
0
    sctphdr->dest_port = dest_port;
4342
0
    sctphdr->v_tag = v_tag;
4343
0
    sctphdr->checksum = 0;
4344
4345
    /*
4346
     * If source address selection fails and we find no route
4347
     * then the ip_output should fail as well with a
4348
     * NO_ROUTE_TO_HOST type error. We probably should catch
4349
     * that somewhere and abort the association right away
4350
     * (assuming this is an INIT being sent).
4351
     */
4352
#if defined(__FreeBSD__) && !defined(__Userspace__)
4353
    if (ro->ro_nh == NULL) {
4354
#else
4355
0
    if (ro->ro_rt == NULL) {
4356
0
#endif
4357
      /*
4358
       * src addr selection failed to find a route (or
4359
       * valid source addr), so we can't get there from
4360
       * here (yet)!
4361
       */
4362
0
      sctp_handle_no_route(stcb, net, so_locked);
4363
0
      SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4364
0
      sctp_m_freem(m);
4365
0
      return (EHOSTUNREACH);
4366
0
    }
4367
0
    if (ro != &iproute) {
4368
0
      memcpy(&iproute, ro, sizeof(*ro));
4369
0
    }
4370
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4371
0
      (uint32_t) (ntohl(ip->ip_src.s_addr)));
4372
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4373
0
      (uint32_t)(ntohl(ip->ip_dst.s_addr)));
4374
#if defined(__FreeBSD__) && !defined(__Userspace__)
4375
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4376
      (void *)ro->ro_nh);
4377
#else
4378
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4379
0
      (void *)ro->ro_rt);
4380
0
#endif
4381
4382
0
    if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4383
      /* failed to prepend data, give up */
4384
0
      SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4385
0
      sctp_m_freem(m);
4386
0
      return (ENOMEM);
4387
0
    }
4388
0
    SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4389
0
    if (port) {
4390
0
      if (use_zero_crc) {
4391
0
        SCTP_STAT_INCR(sctps_sendzerocrc);
4392
0
      } else {
4393
0
        sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4394
0
        SCTP_STAT_INCR(sctps_sendswcrc);
4395
0
      }
4396
#if !defined(__Userspace__)
4397
#if defined(__FreeBSD__)
4398
      if (V_udp_cksum) {
4399
        SCTP_ENABLE_UDP_CSUM(o_pak);
4400
      }
4401
#else
4402
      SCTP_ENABLE_UDP_CSUM(o_pak);
4403
#endif
4404
#endif
4405
0
    } else {
4406
0
      if (use_zero_crc) {
4407
0
        SCTP_STAT_INCR(sctps_sendzerocrc);
4408
0
      } else {
4409
#if defined(__FreeBSD__) && !defined(__Userspace__)
4410
        m->m_pkthdr.csum_flags = CSUM_SCTP;
4411
        m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4412
        SCTP_STAT_INCR(sctps_sendhwcrc);
4413
#else
4414
0
        if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4415
0
              (stcb) && (stcb->asoc.scope.loopback_scope))) {
4416
0
          sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip));
4417
0
          SCTP_STAT_INCR(sctps_sendswcrc);
4418
0
        } else {
4419
0
          SCTP_STAT_INCR(sctps_sendhwcrc);
4420
0
        }
4421
0
#endif
4422
0
      }
4423
0
    }
4424
#ifdef SCTP_PACKET_LOGGING
4425
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4426
      sctp_packet_log(o_pak);
4427
#endif
4428
    /* send it out.  table id is taken from stcb */
4429
#if defined(__APPLE__) && !defined(__Userspace__)
4430
    if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4431
      so = SCTP_INP_SO(inp);
4432
      SCTP_SOCKET_UNLOCK(so, 0);
4433
    }
4434
#endif
4435
#if defined(__FreeBSD__) && !defined(__Userspace__)
4436
    SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr);
4437
#endif
4438
0
    SCTP_IP_OUTPUT(ret, o_pak, ro, inp, vrf_id);
4439
#if defined(__APPLE__) && !defined(__Userspace__)
4440
    if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4441
      atomic_add_int(&stcb->asoc.refcnt, 1);
4442
      SCTP_TCB_UNLOCK(stcb);
4443
      SCTP_SOCKET_LOCK(so, 0);
4444
      SCTP_TCB_LOCK(stcb);
4445
      atomic_subtract_int(&stcb->asoc.refcnt, 1);
4446
    }
4447
#endif
4448
#if defined(__FreeBSD__) && !defined(__Userspace__)
4449
    if (port) {
4450
      UDPSTAT_INC(udps_opackets);
4451
    }
4452
#endif
4453
0
    SCTP_STAT_INCR(sctps_sendpackets);
4454
0
    SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4455
0
    if (ret)
4456
0
      SCTP_STAT_INCR(sctps_senderrors);
4457
4458
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4459
0
    if (net == NULL) {
4460
      /* free tempy routes */
4461
#if defined(__FreeBSD__) && !defined(__Userspace__)
4462
      RO_NHFREE(ro);
4463
#else
4464
0
      if (ro->ro_rt) {
4465
0
        RTFREE(ro->ro_rt);
4466
0
        ro->ro_rt = NULL;
4467
0
      }
4468
0
#endif
4469
0
    } else {
4470
#if defined(__FreeBSD__) && !defined(__Userspace__)
4471
      if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4472
#else
4473
0
      if ((ro->ro_rt != NULL) && (net->ro._s_addr) &&
4474
0
#endif
4475
0
          ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4476
0
        uint32_t mtu;
4477
4478
#if defined(__FreeBSD__) && !defined(__Userspace__)
4479
        mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4480
#else
4481
0
        mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4482
0
#endif
4483
0
        if (mtu > 0) {
4484
0
          if (net->port) {
4485
0
            mtu -= sizeof(struct udphdr);
4486
0
          }
4487
0
          if (mtu < net->mtu) {
4488
0
            net->mtu = mtu;
4489
0
            if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4490
0
              sctp_pathmtu_adjustment(stcb, mtu, true);
4491
0
            }
4492
0
          }
4493
0
        }
4494
#if defined(__FreeBSD__) && !defined(__Userspace__)
4495
      } else if (ro->ro_nh == NULL) {
4496
#else
4497
0
      } else if (ro->ro_rt == NULL) {
4498
0
#endif
4499
        /* route was freed */
4500
0
        if (net->ro._s_addr &&
4501
0
            net->src_addr_selected) {
4502
0
          sctp_free_ifa(net->ro._s_addr);
4503
0
          net->ro._s_addr = NULL;
4504
0
        }
4505
0
        net->src_addr_selected = 0;
4506
0
      }
4507
0
    }
4508
0
    return (ret);
4509
0
  }
4510
0
#endif
4511
0
#ifdef INET6
4512
1.28k
  case AF_INET6:
4513
1.28k
  {
4514
1.28k
    uint32_t flowlabel, flowinfo;
4515
1.28k
    struct ip6_hdr *ip6h;
4516
1.28k
    struct route_in6 ip6route;
4517
#if !defined(__Userspace__)
4518
    struct ifnet *ifp;
4519
#endif
4520
1.28k
    struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4521
1.28k
    int prev_scope = 0;
4522
#ifdef SCTP_EMBEDDED_V6_SCOPE
4523
    struct sockaddr_in6 lsa6_storage;
4524
    int error;
4525
#endif
4526
1.28k
    u_short prev_port = 0;
4527
1.28k
    int len;
4528
4529
1.28k
    if (net) {
4530
1.28k
      flowlabel = net->flowlabel;
4531
1.28k
    } else if (stcb) {
4532
0
      flowlabel = stcb->asoc.default_flowlabel;
4533
0
    } else {
4534
0
      flowlabel = inp->sctp_ep.default_flowlabel;
4535
0
    }
4536
1.28k
    if (flowlabel == 0) {
4537
      /*
4538
       * This means especially, that it is not set at the
4539
       * SCTP layer. So use the value from the IP layer.
4540
       */
4541
#if defined(__APPLE__) && !defined(__Userspace__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION))
4542
      flowlabel = ntohl(inp->ip_inp.inp.inp_flow);
4543
#else
4544
1.28k
      flowlabel = ntohl(((struct inpcb *)inp)->inp_flow);
4545
1.28k
#endif
4546
1.28k
    }
4547
1.28k
    flowlabel &= 0x000fffff;
4548
1.28k
    len = SCTP_MIN_OVERHEAD;
4549
1.28k
    if (port) {
4550
0
      len += sizeof(struct udphdr);
4551
0
    }
4552
1.28k
    newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4553
1.28k
    if (newm == NULL) {
4554
0
      sctp_m_freem(m);
4555
0
      SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4556
0
      return (ENOMEM);
4557
0
    }
4558
1.28k
    SCTP_ALIGN_TO_END(newm, len);
4559
1.28k
    SCTP_BUF_LEN(newm) = len;
4560
1.28k
    SCTP_BUF_NEXT(newm) = m;
4561
1.28k
    m = newm;
4562
#if defined(__FreeBSD__) && !defined(__Userspace__)
4563
    if (net != NULL) {
4564
      m->m_pkthdr.flowid = net->flowid;
4565
      M_HASHTYPE_SET(m, net->flowtype);
4566
    } else {
4567
      m->m_pkthdr.flowid = mflowid;
4568
      M_HASHTYPE_SET(m, mflowtype);
4569
    }
4570
#endif
4571
1.28k
    packet_length = sctp_calculate_len(m);
4572
4573
1.28k
    ip6h = mtod(m, struct ip6_hdr *);
4574
    /* protect *sin6 from overwrite */
4575
1.28k
    sin6 = (struct sockaddr_in6 *)to;
4576
1.28k
    tmp = *sin6;
4577
1.28k
    sin6 = &tmp;
4578
4579
#ifdef SCTP_EMBEDDED_V6_SCOPE
4580
    /* KAME hack: embed scopeid */
4581
#if defined(__APPLE__) && !defined(__Userspace__)
4582
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4583
    if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
4584
#else
4585
    if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0)
4586
#endif
4587
#elif defined(SCTP_KAME)
4588
    if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0)
4589
#else
4590
    if (in6_embedscope(&sin6->sin6_addr, sin6) != 0)
4591
#endif
4592
    {
4593
      SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4594
      sctp_m_freem(m);
4595
      return (EINVAL);
4596
    }
4597
#endif /* SCTP_EMBEDDED_V6_SCOPE */
4598
1.28k
    if (net == NULL) {
4599
0
      memset(&ip6route, 0, sizeof(ip6route));
4600
0
      ro = (sctp_route_t *)&ip6route;
4601
#ifdef HAVE_SIN6_LEN
4602
      memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4603
#else
4604
0
      memcpy(&ro->ro_dst, sin6, sizeof(struct sockaddr_in6));
4605
0
#endif
4606
1.28k
    } else {
4607
1.28k
      ro = (sctp_route_t *)&net->ro;
4608
1.28k
    }
4609
    /*
4610
     * We assume here that inp_flow is in host byte order within
4611
     * the TCB!
4612
     */
4613
1.28k
    if (tos_value == 0) {
4614
      /*
4615
       * This means especially, that it is not set at the
4616
       * SCTP layer. So use the value from the IP layer.
4617
       */
4618
#if defined(__APPLE__) && !defined(__Userspace__) && (!defined(APPLE_LEOPARD) && !defined(APPLE_SNOWLEOPARD) && !defined(APPLE_LION) && !defined(APPLE_MOUNTAINLION))
4619
      tos_value = (ntohl(inp->ip_inp.inp.inp_flow) >> 20) & 0xff;
4620
#else
4621
1.28k
      tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff;
4622
1.28k
#endif
4623
1.28k
    }
4624
1.28k
    tos_value &= 0xfc;
4625
1.28k
    if (ecn_ok) {
4626
86
      tos_value |= sctp_get_ect(stcb);
4627
86
    }
4628
1.28k
    flowinfo = 0x06;
4629
1.28k
    flowinfo <<= 8;
4630
1.28k
    flowinfo |= tos_value;
4631
1.28k
    flowinfo <<= 20;
4632
1.28k
    flowinfo |= flowlabel;
4633
1.28k
    ip6h->ip6_flow = htonl(flowinfo);
4634
1.28k
    if (port) {
4635
0
      ip6h->ip6_nxt = IPPROTO_UDP;
4636
1.28k
    } else {
4637
1.28k
      ip6h->ip6_nxt = IPPROTO_SCTP;
4638
1.28k
    }
4639
1.28k
    ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4640
1.28k
    ip6h->ip6_dst = sin6->sin6_addr;
4641
4642
    /*
4643
     * Add SRC address selection here: we can only reuse to a
4644
     * limited degree the kame src-addr-sel, since we can try
4645
     * their selection but it may not be bound.
4646
     */
4647
1.28k
    memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
4648
1.28k
    lsa6_tmp.sin6_family = AF_INET6;
4649
#ifdef HAVE_SIN6_LEN
4650
    lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4651
#endif
4652
1.28k
    lsa6 = &lsa6_tmp;
4653
1.28k
    if (net && out_of_asoc_ok == 0) {
4654
1.28k
      if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED|SCTP_ADDR_IFA_UNUSEABLE))) {
4655
0
        sctp_free_ifa(net->ro._s_addr);
4656
0
        net->ro._s_addr = NULL;
4657
0
        net->src_addr_selected = 0;
4658
#if defined(__FreeBSD__) && !defined(__Userspace__)
4659
        RO_NHFREE(ro);
4660
#else
4661
0
        if (ro->ro_rt) {
4662
0
          RTFREE(ro->ro_rt);
4663
0
          ro->ro_rt = NULL;
4664
0
        }
4665
0
#endif
4666
0
      }
4667
1.28k
      if (net->src_addr_selected == 0) {
4668
#ifdef SCTP_EMBEDDED_V6_SCOPE
4669
        sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4670
        /* KAME hack: embed scopeid */
4671
#if defined(__APPLE__) && !defined(__Userspace__)
4672
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4673
        if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
4674
#else
4675
        if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0)
4676
#endif
4677
#elif defined(SCTP_KAME)
4678
        if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0)
4679
#else
4680
        if (in6_embedscope(&sin6->sin6_addr, sin6) != 0)
4681
#endif
4682
        {
4683
          SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4684
          sctp_m_freem(m);
4685
          return (EINVAL);
4686
        }
4687
#endif /* SCTP_EMBEDDED_V6_SCOPE */
4688
        /* Cache the source address */
4689
1.28k
        net->ro._s_addr = sctp_source_address_selection(inp,
4690
1.28k
                    stcb,
4691
1.28k
                    ro,
4692
1.28k
                    net,
4693
1.28k
                    0,
4694
1.28k
                    vrf_id);
4695
#ifdef SCTP_EMBEDDED_V6_SCOPE
4696
#ifdef SCTP_KAME
4697
        (void)sa6_recoverscope(sin6);
4698
#else
4699
        (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4700
#endif  /* SCTP_KAME */
4701
#endif  /* SCTP_EMBEDDED_V6_SCOPE */
4702
1.28k
        net->src_addr_selected = 1;
4703
1.28k
      }
4704
1.28k
      if (net->ro._s_addr == NULL) {
4705
1.28k
        SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4706
1.28k
        net->src_addr_selected = 0;
4707
1.28k
        sctp_handle_no_route(stcb, net, so_locked);
4708
1.28k
        SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4709
1.28k
        sctp_m_freem(m);
4710
1.28k
        return (EHOSTUNREACH);
4711
1.28k
      }
4712
0
      lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4713
0
    } else {
4714
#ifdef SCTP_EMBEDDED_V6_SCOPE
4715
      sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4716
      /* KAME hack: embed scopeid */
4717
#if defined(__APPLE__) && !defined(__Userspace__)
4718
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
4719
      if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL) != 0)
4720
#else
4721
      if (in6_embedscope(&sin6->sin6_addr, sin6, NULL, NULL, NULL) != 0)
4722
#endif
4723
#elif defined(SCTP_KAME)
4724
      if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0)
4725
#else
4726
      if (in6_embedscope(&sin6->sin6_addr, sin6) != 0)
4727
#endif
4728
        {
4729
        SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4730
        sctp_m_freem(m);
4731
        return (EINVAL);
4732
        }
4733
#endif /* SCTP_EMBEDDED_V6_SCOPE */
4734
0
      if (over_addr == NULL) {
4735
0
        struct sctp_ifa *_lsrc;
4736
4737
0
        _lsrc = sctp_source_address_selection(inp, stcb, ro,
4738
0
                                              net,
4739
0
                                              out_of_asoc_ok,
4740
0
                                              vrf_id);
4741
0
        if (_lsrc == NULL) {
4742
0
          sctp_handle_no_route(stcb, net, so_locked);
4743
0
          SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4744
0
          sctp_m_freem(m);
4745
0
          return (EHOSTUNREACH);
4746
0
        }
4747
0
        lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4748
0
        sctp_free_ifa(_lsrc);
4749
0
      } else {
4750
0
        lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4751
0
        SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4752
0
      }
4753
#ifdef SCTP_EMBEDDED_V6_SCOPE
4754
#ifdef SCTP_KAME
4755
      (void)sa6_recoverscope(sin6);
4756
#else
4757
      (void)in6_recoverscope(sin6, &sin6->sin6_addr, NULL);
4758
#endif  /* SCTP_KAME */
4759
#endif  /* SCTP_EMBEDDED_V6_SCOPE */
4760
0
    }
4761
0
    lsa6->sin6_port = inp->sctp_lport;
4762
4763
#if defined(__FreeBSD__) && !defined(__Userspace__)
4764
    if (ro->ro_nh == NULL) {
4765
#else
4766
0
    if (ro->ro_rt == NULL) {
4767
0
#endif
4768
      /*
4769
       * src addr selection failed to find a route (or
4770
       * valid source addr), so we can't get there from
4771
       * here!
4772
       */
4773
0
      sctp_handle_no_route(stcb, net, so_locked);
4774
0
      SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4775
0
      sctp_m_freem(m);
4776
0
      return (EHOSTUNREACH);
4777
0
    }
4778
0
#ifndef SCOPEDROUTING
4779
#ifdef SCTP_EMBEDDED_V6_SCOPE
4780
    /*
4781
     * XXX: sa6 may not have a valid sin6_scope_id in the
4782
     * non-SCOPEDROUTING case.
4783
     */
4784
    memset(&lsa6_storage, 0, sizeof(lsa6_storage));
4785
    lsa6_storage.sin6_family = AF_INET6;
4786
#ifdef HAVE_SIN6_LEN
4787
    lsa6_storage.sin6_len = sizeof(lsa6_storage);
4788
#endif
4789
#ifdef SCTP_KAME
4790
    lsa6_storage.sin6_addr = lsa6->sin6_addr;
4791
    if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4792
#else
4793
    if ((error = in6_recoverscope(&lsa6_storage, &lsa6->sin6_addr,
4794
        NULL)) != 0) {
4795
#endif        /* SCTP_KAME */
4796
      SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4797
      sctp_m_freem(m);
4798
      return (error);
4799
    }
4800
    /* XXX */
4801
    lsa6_storage.sin6_addr = lsa6->sin6_addr;
4802
    lsa6_storage.sin6_port = inp->sctp_lport;
4803
    lsa6 = &lsa6_storage;
4804
#endif /* SCTP_EMBEDDED_V6_SCOPE */
4805
0
#endif /* SCOPEDROUTING */
4806
0
    ip6h->ip6_src = lsa6->sin6_addr;
4807
4808
0
    if (port) {
4809
0
      if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4810
0
        sctp_handle_no_route(stcb, net, so_locked);
4811
0
        SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4812
0
        sctp_m_freem(m);
4813
0
        return (EHOSTUNREACH);
4814
0
      }
4815
0
      udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4816
0
      udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4817
0
      udp->uh_dport = port;
4818
0
      udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4819
0
      udp->uh_sum = 0;
4820
0
      sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4821
0
    } else {
4822
0
      sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4823
0
    }
4824
4825
0
    sctphdr->src_port = src_port;
4826
0
    sctphdr->dest_port = dest_port;
4827
0
    sctphdr->v_tag = v_tag;
4828
0
    sctphdr->checksum = 0;
4829
4830
    /*
4831
     * We set the hop limit now since there is a good chance
4832
     * that our ro pointer is now filled
4833
     */
4834
0
    ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4835
#if !defined(__Userspace__)
4836
    ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4837
#endif
4838
4839
#ifdef SCTP_DEBUG
4840
    /* Copy to be sure something bad is not happening */
4841
    sin6->sin6_addr = ip6h->ip6_dst;
4842
    lsa6->sin6_addr = ip6h->ip6_src;
4843
#endif
4844
4845
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4846
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4847
0
    SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4848
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4849
0
    SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4850
0
    if (net) {
4851
0
      sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4852
      /* preserve the port and scope for link local send */
4853
0
      prev_scope = sin6->sin6_scope_id;
4854
0
      prev_port = sin6->sin6_port;
4855
0
    }
4856
4857
0
    if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4858
      /* failed to prepend data, give up */
4859
0
      sctp_m_freem(m);
4860
0
      SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4861
0
      return (ENOMEM);
4862
0
    }
4863
0
    SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4864
0
    if (port) {
4865
0
      sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4866
0
      SCTP_STAT_INCR(sctps_sendswcrc);
4867
#if !defined(__Userspace__)
4868
#if defined(_WIN32)
4869
      udp->uh_sum = 0;
4870
#else
4871
      if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4872
        udp->uh_sum = 0xffff;
4873
      }
4874
#endif
4875
#endif
4876
0
    } else {
4877
#if defined(__FreeBSD__) && !defined(__Userspace__)
4878
      m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
4879
      m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4880
      SCTP_STAT_INCR(sctps_sendhwcrc);
4881
#else
4882
0
      if (!(SCTP_BASE_SYSCTL(sctp_no_csum_on_loopback) &&
4883
0
            (stcb) && (stcb->asoc.scope.loopback_scope))) {
4884
0
        sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr));
4885
0
        SCTP_STAT_INCR(sctps_sendswcrc);
4886
0
      } else {
4887
0
        SCTP_STAT_INCR(sctps_sendhwcrc);
4888
0
      }
4889
0
#endif
4890
0
    }
4891
    /* send it out. table id is taken from stcb */
4892
#if defined(__APPLE__) && !defined(__Userspace__)
4893
    if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4894
      so = SCTP_INP_SO(inp);
4895
      SCTP_SOCKET_UNLOCK(so, 0);
4896
    }
4897
#endif
4898
#ifdef SCTP_PACKET_LOGGING
4899
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4900
      sctp_packet_log(o_pak);
4901
#endif
4902
#if !defined(__Userspace__)
4903
#if defined(__FreeBSD__)
4904
    SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr);
4905
#endif
4906
    SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, inp, vrf_id);
4907
#else
4908
0
    SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, NULL, inp, vrf_id);
4909
0
#endif
4910
#if defined(__APPLE__) && !defined(__Userspace__)
4911
    if ((SCTP_BASE_SYSCTL(sctp_output_unlocked)) && (so_locked)) {
4912
      atomic_add_int(&stcb->asoc.refcnt, 1);
4913
      SCTP_TCB_UNLOCK(stcb);
4914
      SCTP_SOCKET_LOCK(so, 0);
4915
      SCTP_TCB_LOCK(stcb);
4916
      atomic_subtract_int(&stcb->asoc.refcnt, 1);
4917
    }
4918
#endif
4919
0
    if (net) {
4920
      /* for link local this must be done */
4921
0
      sin6->sin6_scope_id = prev_scope;
4922
0
      sin6->sin6_port = prev_port;
4923
0
    }
4924
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4925
#if defined(__FreeBSD__) && !defined(__Userspace__)
4926
    if (port) {
4927
      UDPSTAT_INC(udps_opackets);
4928
    }
4929
#endif
4930
0
    SCTP_STAT_INCR(sctps_sendpackets);
4931
0
    SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4932
0
    if (ret) {
4933
0
      SCTP_STAT_INCR(sctps_senderrors);
4934
0
    }
4935
0
    if (net == NULL) {
4936
      /* Now if we had a temp route free it */
4937
#if defined(__FreeBSD__) && !defined(__Userspace__)
4938
      RO_NHFREE(ro);
4939
#else
4940
0
      if (ro->ro_rt) {
4941
0
        RTFREE(ro->ro_rt);
4942
0
        ro->ro_rt = NULL;
4943
0
      }
4944
0
#endif
4945
0
    } else {
4946
      /* PMTU check versus smallest asoc MTU goes here */
4947
#if defined(__FreeBSD__) && !defined(__Userspace__)
4948
      if (ro->ro_nh == NULL) {
4949
#else
4950
0
      if (ro->ro_rt == NULL) {
4951
0
#endif
4952
        /* Route was freed */
4953
0
        if (net->ro._s_addr &&
4954
0
            net->src_addr_selected) {
4955
0
          sctp_free_ifa(net->ro._s_addr);
4956
0
          net->ro._s_addr = NULL;
4957
0
        }
4958
0
        net->src_addr_selected = 0;
4959
0
      }
4960
#if defined(__FreeBSD__) && !defined(__Userspace__)
4961
      if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4962
#else
4963
0
      if ((ro->ro_rt != NULL) && (net->ro._s_addr) &&
4964
0
#endif
4965
0
          ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4966
0
        uint32_t mtu;
4967
4968
#if defined(__FreeBSD__) && !defined(__Userspace__)
4969
        mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4970
#else
4971
0
        mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_rt);
4972
0
#endif
4973
0
        if (mtu > 0) {
4974
0
          if (net->port) {
4975
0
            mtu -= sizeof(struct udphdr);
4976
0
          }
4977
0
          if (mtu < net->mtu) {
4978
0
            net->mtu = mtu;
4979
0
            if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4980
0
              sctp_pathmtu_adjustment(stcb, mtu, false);
4981
0
            }
4982
0
          }
4983
0
        }
4984
0
      }
4985
#if !defined(__Userspace__)
4986
      else if (ifp != NULL) {
4987
#if defined(_WIN32)
4988
#define ND_IFINFO(ifp)  (ifp)
4989
#define linkmtu   if_mtu
4990
#endif
4991
        if ((ND_IFINFO(ifp)->linkmtu > 0) &&
4992
            (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4993
          sctp_pathmtu_adjustment(stcb, ND_IFINFO(ifp)->linkmtu, false);
4994
        }
4995
      }
4996
#endif
4997
0
    }
4998
0
    return (ret);
4999
0
  }
5000
0
#endif
5001
0
#if defined(__Userspace__)
5002
51.7k
  case AF_CONN:
5003
51.7k
  {
5004
51.7k
    char *buffer;
5005
51.7k
    struct sockaddr_conn *sconn;
5006
51.7k
    int len;
5007
5008
51.7k
    sconn = (struct sockaddr_conn *)to;
5009
51.7k
    len = sizeof(struct sctphdr);
5010
51.7k
    newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
5011
51.7k
    if (newm == NULL) {
5012
0
      sctp_m_freem(m);
5013
0
      SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
5014
0
      return (ENOMEM);
5015
0
    }
5016
51.7k
    SCTP_ALIGN_TO_END(newm, len);
5017
51.7k
    SCTP_BUF_LEN(newm) = len;
5018
51.7k
    SCTP_BUF_NEXT(newm) = m;
5019
51.7k
    m = newm;
5020
51.7k
    packet_length = sctp_calculate_len(m);
5021
51.7k
    m->m_pkthdr.len = packet_length;
5022
51.7k
    sctphdr = mtod(m, struct sctphdr *);
5023
51.7k
    sctphdr->src_port = src_port;
5024
51.7k
    sctphdr->dest_port = dest_port;
5025
51.7k
    sctphdr->v_tag = v_tag;
5026
51.7k
    sctphdr->checksum = 0;
5027
51.7k
    if (use_zero_crc) {
5028
0
      SCTP_STAT_INCR(sctps_sendzerocrc);
5029
51.7k
    } else if (SCTP_BASE_VAR(crc32c_offloaded) == 0) {
5030
0
      sctphdr->checksum = sctp_calculate_cksum(m, 0);
5031
0
      SCTP_STAT_INCR(sctps_sendswcrc);
5032
51.7k
    } else {
5033
51.7k
      SCTP_STAT_INCR(sctps_sendhwcrc);
5034
51.7k
    }
5035
51.7k
    if (tos_value == 0) {
5036
51.7k
      tos_value = inp->ip_inp.inp.inp_ip_tos;
5037
51.7k
    }
5038
51.7k
    tos_value &= 0xfc;
5039
51.7k
    if (ecn_ok) {
5040
7.09k
      tos_value |= sctp_get_ect(stcb);
5041
7.09k
    }
5042
    /* Don't alloc/free for each packet */
5043
51.7k
    if ((buffer = malloc(packet_length)) != NULL) {
5044
51.7k
      m_copydata(m, 0, packet_length, buffer);
5045
51.7k
      ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, packet_length, tos_value, nofragment_flag);
5046
51.7k
      free(buffer);
5047
51.7k
    } else {
5048
0
      ret = ENOMEM;
5049
0
    }
5050
51.7k
    sctp_m_freem(m);
5051
51.7k
    return (ret);
5052
51.7k
  }
5053
0
#endif
5054
0
  default:
5055
0
    SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
5056
0
            ((struct sockaddr *)to)->sa_family);
5057
0
    sctp_m_freem(m);
5058
0
    SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
5059
0
    return (EFAULT);
5060
54.2k
  }
5061
54.2k
}
5062
5063
void
5064
sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
5065
23.9k
{
5066
23.9k
  struct mbuf *m, *m_last;
5067
23.9k
  struct sctp_nets *net;
5068
23.9k
  struct sctp_init_chunk *init;
5069
23.9k
  struct sctp_supported_addr_param *sup_addr;
5070
23.9k
  struct sctp_adaptation_layer_indication *ali;
5071
23.9k
  struct sctp_zero_checksum_acceptable *zero_chksum;
5072
23.9k
  struct sctp_supported_chunk_types_param *pr_supported;
5073
23.9k
  struct sctp_paramhdr *ph;
5074
23.9k
  int cnt_inits_to = 0;
5075
23.9k
  int error;
5076
23.9k
  uint16_t num_ext, chunk_len, padding_len, parameter_len;
5077
5078
#if defined(__APPLE__) && !defined(__Userspace__)
5079
  if (so_locked) {
5080
    sctp_lock_assert(SCTP_INP_SO(inp));
5081
  } else {
5082
    sctp_unlock_assert(SCTP_INP_SO(inp));
5083
  }
5084
#endif
5085
  /* INIT's always go to the primary (and usually ONLY address) */
5086
23.9k
  net = stcb->asoc.primary_destination;
5087
23.9k
  if (net == NULL) {
5088
0
    net = TAILQ_FIRST(&stcb->asoc.nets);
5089
0
    if (net == NULL) {
5090
      /* TSNH */
5091
0
      return;
5092
0
    }
5093
    /* we confirm any address we send an INIT to */
5094
0
    net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
5095
0
    (void)sctp_set_primary_addr(stcb, NULL, net);
5096
23.9k
  } else {
5097
    /* we confirm any address we send an INIT to */
5098
23.9k
    net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
5099
23.9k
  }
5100
23.9k
  SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
5101
23.9k
#ifdef INET6
5102
23.9k
  if (net->ro._l_addr.sa.sa_family == AF_INET6) {
5103
    /*
5104
     * special hook, if we are sending to link local it will not
5105
     * show up in our private address count.
5106
     */
5107
691
    if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr))
5108
691
      cnt_inits_to = 1;
5109
691
  }
5110
23.9k
#endif
5111
23.9k
  if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
5112
    /* This case should not happen */
5113
2.05k
    SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
5114
2.05k
    return;
5115
2.05k
  }
5116
  /* start the INIT timer */
5117
21.9k
  sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
5118
5119
21.9k
  m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA);
5120
21.9k
  if (m == NULL) {
5121
    /* No memory, INIT timer will re-attempt. */
5122
0
    SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
5123
0
    return;
5124
0
  }
5125
21.9k
  chunk_len = (uint16_t)sizeof(struct sctp_init_chunk);
5126
21.9k
  padding_len = 0;
5127
  /* Now lets put the chunk header in place */
5128
21.9k
  init = mtod(m, struct sctp_init_chunk *);
5129
  /* now the chunk header */
5130
21.9k
  init->ch.chunk_type = SCTP_INITIATION;
5131
21.9k
  init->ch.chunk_flags = 0;
5132
  /* fill in later from mbuf we build */
5133
21.9k
  init->ch.chunk_length = 0;
5134
  /* place in my tag */
5135
21.9k
  init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
5136
  /* set up some of the credits. */
5137
21.9k
  init->init.a_rwnd = htonl(max(inp->sctp_socket?SCTP_SB_LIMIT_RCV(inp->sctp_socket):0,
5138
21.9k
                                SCTP_MINIMAL_RWND));
5139
21.9k
  init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
5140
21.9k
  init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
5141
21.9k
  init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
5142
5143
  /* Adaptation layer indication parameter */
5144
21.9k
  if (inp->sctp_ep.adaptation_layer_indicator_provided) {
5145
0
    parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
5146
0
    ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len);
5147
0
    ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5148
0
    ali->ph.param_length = htons(parameter_len);
5149
0
    ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
5150
0
    chunk_len += parameter_len;
5151
0
  }
5152
5153
  /* ECN parameter */
5154
21.9k
  if (stcb->asoc.ecn_supported == 1) {
5155
21.9k
    parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5156
21.9k
    ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
5157
21.9k
    ph->param_type = htons(SCTP_ECN_CAPABLE);
5158
21.9k
    ph->param_length = htons(parameter_len);
5159
21.9k
    chunk_len += parameter_len;
5160
21.9k
  }
5161
5162
  /* PR-SCTP supported parameter */
5163
21.9k
  if (stcb->asoc.prsctp_supported == 1) {
5164
19.5k
    parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5165
19.5k
    ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
5166
19.5k
    ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
5167
19.5k
    ph->param_length = htons(parameter_len);
5168
19.5k
    chunk_len += parameter_len;
5169
19.5k
  }
5170
5171
  /* Zero checksum acceptable parameter */
5172
21.9k
  if (stcb->asoc.rcv_edmid != SCTP_EDMID_NONE) {
5173
0
    parameter_len = (uint16_t)sizeof(struct sctp_zero_checksum_acceptable);
5174
0
    zero_chksum = (struct sctp_zero_checksum_acceptable *)(mtod(m, caddr_t) + chunk_len);
5175
0
    zero_chksum->ph.param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
5176
0
    zero_chksum->ph.param_length = htons(parameter_len);
5177
0
    zero_chksum->edmid = htonl(stcb->asoc.rcv_edmid);
5178
0
    chunk_len += parameter_len;
5179
0
  }
5180
5181
  /* Add NAT friendly parameter. */
5182
21.9k
  if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
5183
0
    parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5184
0
    ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
5185
0
    ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5186
0
    ph->param_length = htons(parameter_len);
5187
0
    chunk_len += parameter_len;
5188
0
  }
5189
5190
  /* And now tell the peer which extensions we support */
5191
21.9k
  num_ext = 0;
5192
21.9k
  pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len);
5193
21.9k
  if (stcb->asoc.prsctp_supported == 1) {
5194
19.5k
    pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5195
19.5k
    if (stcb->asoc.idata_supported) {
5196
12.1k
      pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
5197
12.1k
    }
5198
19.5k
  }
5199
21.9k
  if (stcb->asoc.auth_supported == 1) {
5200
21.9k
    pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5201
21.9k
  }
5202
21.9k
  if (stcb->asoc.asconf_supported == 1) {
5203
19.8k
    pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5204
19.8k
    pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5205
19.8k
  }
5206
21.9k
  if (stcb->asoc.reconfig_supported == 1) {
5207
21.9k
    pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5208
21.9k
  }
5209
21.9k
  if (stcb->asoc.idata_supported) {
5210
13.9k
    pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
5211
13.9k
  }
5212
21.9k
  if (stcb->asoc.nrsack_supported == 1) {
5213
16.0k
    pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5214
16.0k
  }
5215
21.9k
  if (stcb->asoc.pktdrop_supported == 1) {
5216
16.0k
    pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5217
16.0k
  }
5218
21.9k
  if (num_ext > 0) {
5219
21.9k
    parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
5220
21.9k
    pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5221
21.9k
    pr_supported->ph.param_length = htons(parameter_len);
5222
21.9k
    padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5223
21.9k
    chunk_len += parameter_len;
5224
21.9k
  }
5225
  /* add authentication parameters */
5226
21.9k
  if (stcb->asoc.auth_supported) {
5227
    /* attach RANDOM parameter, if available */
5228
21.9k
    if (stcb->asoc.authinfo.random != NULL) {
5229
21.9k
      struct sctp_auth_random *randp;
5230
5231
21.9k
      if (padding_len > 0) {
5232
21.3k
        memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
5233
21.3k
        chunk_len += padding_len;
5234
21.3k
        padding_len = 0;
5235
21.3k
      }
5236
21.9k
      randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len);
5237
21.9k
      parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len;
5238
      /* random key already contains the header */
5239
21.9k
      memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len);
5240
21.9k
      padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5241
21.9k
      chunk_len += parameter_len;
5242
21.9k
    }
5243
    /* add HMAC_ALGO parameter */
5244
21.9k
    if (stcb->asoc.local_hmacs != NULL) {
5245
21.9k
      struct sctp_auth_hmac_algo *hmacs;
5246
5247
21.9k
      if (padding_len > 0) {
5248
0
        memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
5249
0
        chunk_len += padding_len;
5250
0
        padding_len = 0;
5251
0
      }
5252
21.9k
      hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len);
5253
21.9k
      parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) +
5254
21.9k
                                 stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t));
5255
21.9k
      hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
5256
21.9k
      hmacs->ph.param_length = htons(parameter_len);
5257
21.9k
      sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids);
5258
21.9k
      padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5259
21.9k
      chunk_len += parameter_len;
5260
21.9k
    }
5261
    /* add CHUNKS parameter */
5262
21.9k
    if (stcb->asoc.local_auth_chunks != NULL) {
5263
21.9k
      struct sctp_auth_chunk_list *chunks;
5264
5265
21.9k
      if (padding_len > 0) {
5266
21.9k
        memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
5267
21.9k
        chunk_len += padding_len;
5268
21.9k
        padding_len = 0;
5269
21.9k
      }
5270
21.9k
      chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len);
5271
21.9k
      parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) +
5272
21.9k
                                 sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks));
5273
21.9k
      chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
5274
21.9k
      chunks->ph.param_length = htons(parameter_len);
5275
21.9k
      sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types);
5276
21.9k
      padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5277
21.9k
      chunk_len += parameter_len;
5278
21.9k
    }
5279
21.9k
  }
5280
5281
  /* now any cookie time extensions */
5282
21.9k
  if (stcb->asoc.cookie_preserve_req > 0) {
5283
48
    struct sctp_cookie_perserve_param *cookie_preserve;
5284
5285
48
    if (padding_len > 0) {
5286
48
      memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
5287
48
      chunk_len += padding_len;
5288
48
      padding_len = 0;
5289
48
    }
5290
48
    parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param);
5291
48
    cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t) + chunk_len);
5292
48
    cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
5293
48
    cookie_preserve->ph.param_length = htons(parameter_len);
5294
48
    cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
5295
48
    stcb->asoc.cookie_preserve_req = 0;
5296
48
    chunk_len += parameter_len;
5297
48
  }
5298
5299
21.9k
  if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) {
5300
0
    uint8_t i;
5301
5302
0
    if (padding_len > 0) {
5303
0
      memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
5304
0
      chunk_len += padding_len;
5305
0
      padding_len = 0;
5306
0
    }
5307
0
    parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5308
0
    if (stcb->asoc.scope.ipv4_addr_legal) {
5309
0
      parameter_len += (uint16_t)sizeof(uint16_t);
5310
0
    }
5311
0
    if (stcb->asoc.scope.ipv6_addr_legal) {
5312
0
      parameter_len += (uint16_t)sizeof(uint16_t);
5313
0
    }
5314
0
    sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t) + chunk_len);
5315
0
    sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
5316
0
    sup_addr->ph.param_length = htons(parameter_len);
5317
0
    i = 0;
5318
0
    if (stcb->asoc.scope.ipv4_addr_legal) {
5319
0
      sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS);
5320
0
    }
5321
0
    if (stcb->asoc.scope.ipv6_addr_legal) {
5322
0
      sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS);
5323
0
    }
5324
0
    padding_len = 4 - 2 * i;
5325
0
    chunk_len += parameter_len;
5326
0
  }
5327
5328
21.9k
  SCTP_BUF_LEN(m) = chunk_len;
5329
  /* now the addresses */
5330
  /* To optimize this we could put the scoping stuff
5331
   * into a structure and remove the individual uint8's from
5332
   * the assoc structure. Then we could just sifa in the
5333
   * address within the stcb. But for now this is a quick
5334
   * hack to get the address stuff teased apart.
5335
   */
5336
21.9k
  m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope,
5337
21.9k
                                      m, cnt_inits_to,
5338
21.9k
                                      &padding_len, &chunk_len);
5339
5340
21.9k
  init->ch.chunk_length = htons(chunk_len);
5341
21.9k
  if (padding_len > 0) {
5342
21.8k
    if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
5343
0
      sctp_m_freem(m);
5344
0
      return;
5345
0
    }
5346
21.8k
  }
5347
21.9k
  SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
5348
21.9k
  if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
5349
21.9k
                                          (struct sockaddr *)&net->ro._l_addr,
5350
21.9k
                                          m, 0, NULL, 0, 0, 0, 0,
5351
21.9k
                                          inp->sctp_lport, stcb->rport, htonl(0),
5352
21.9k
                                          net->port, NULL,
5353
#if defined(__FreeBSD__) && !defined(__Userspace__)
5354
                                          0, 0,
5355
#endif
5356
21.9k
                                          false, so_locked))) {
5357
1.27k
    SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
5358
1.27k
    if (error == ENOBUFS) {
5359
0
      stcb->asoc.ifp_had_enobuf = 1;
5360
0
      SCTP_STAT_INCR(sctps_lowlevelerr);
5361
0
    }
5362
20.6k
  } else {
5363
20.6k
    stcb->asoc.ifp_had_enobuf = 0;
5364
20.6k
  }
5365
21.9k
  SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
5366
21.9k
  (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
5367
21.9k
}
5368
5369
struct mbuf *
5370
sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
5371
                                      int param_offset, int *abort_processing,
5372
                                      struct sctp_chunkhdr *cp,
5373
                                      int *nat_friendly,
5374
                                      int *cookie_found,
5375
                                      uint32_t *edmid)
5376
8.79k
{
5377
  /*
5378
   * Given a mbuf containing an INIT or INIT-ACK with the param_offset
5379
   * being equal to the beginning of the params i.e. (iphlen +
5380
   * sizeof(struct sctp_init_msg) parse through the parameters to the
5381
   * end of the mbuf verifying that all parameters are known.
5382
   *
5383
   * For unknown parameters build and return a mbuf with
5384
   * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
5385
   * processing this chunk stop, and set *abort_processing to 1.
5386
   *
5387
   * By having param_offset be pre-set to where parameters begin it is
5388
   * hoped that this routine may be reused in the future by new
5389
   * features.
5390
   */
5391
8.79k
  struct sctp_zero_checksum_acceptable zero_chksum, *zero_chksum_p;
5392
8.79k
  struct sctp_paramhdr *phdr, params;
5393
8.79k
  struct mbuf *mat, *m_tmp, *op_err, *op_err_last;
5394
8.79k
  int at, limit, pad_needed;
5395
8.79k
  uint16_t ptype, plen, padded_size;
5396
5397
8.79k
  *abort_processing = 0;
5398
8.79k
  if (cookie_found != NULL) {
5399
7.82k
    *cookie_found = 0;
5400
7.82k
  }
5401
8.79k
  if (edmid != NULL) {
5402
969
    *edmid = SCTP_EDMID_NONE;
5403
969
  }
5404
8.79k
  mat = in_initpkt;
5405
8.79k
  limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
5406
8.79k
  at = param_offset;
5407
8.79k
  op_err = NULL;
5408
8.79k
  op_err_last = NULL;
5409
8.79k
  pad_needed = 0;
5410
8.79k
  SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
5411
8.79k
  phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
5412
299k
  while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
5413
291k
    ptype = ntohs(phdr->param_type);
5414
291k
    plen = ntohs(phdr->param_length);
5415
291k
    if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
5416
      /* wacked parameter */
5417
119
      SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
5418
119
      goto invalid_size;
5419
119
    }
5420
291k
    limit -= SCTP_SIZE32(plen);
5421
    /*-
5422
     * All parameters for all chunks that we know/understand are
5423
     * listed here. We process them other places and make
5424
     * appropriate stop actions per the upper bits. However this
5425
     * is the generic routine processor's can call to get back
5426
     * an operr.. to either incorporate (init-ack) or send.
5427
     */
5428
291k
    padded_size = SCTP_SIZE32(plen);
5429
291k
    switch (ptype) {
5430
      /* Param's with variable size */
5431
1.04k
    case SCTP_HEARTBEAT_INFO:
5432
3.02k
    case SCTP_UNRECOG_PARAM:
5433
5.57k
    case SCTP_ERROR_CAUSE_IND:
5434
      /* ok skip fwd */
5435
5.57k
      at += padded_size;
5436
5.57k
      break;
5437
10.6k
    case SCTP_STATE_COOKIE:
5438
10.6k
      if (cookie_found != NULL) {
5439
9.66k
        *cookie_found = 1;
5440
9.66k
      }
5441
10.6k
      at += padded_size;
5442
10.6k
      break;
5443
      /* Param's with variable size within a range */
5444
11.8k
    case SCTP_CHUNK_LIST:
5445
35.2k
    case SCTP_SUPPORTED_CHUNK_EXT:
5446
35.2k
      if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
5447
14
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
5448
14
        goto invalid_size;
5449
14
      }
5450
35.2k
      at += padded_size;
5451
35.2k
      break;
5452
2.88k
    case SCTP_SUPPORTED_ADDRTYPE:
5453
2.88k
      if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
5454
11
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
5455
11
        goto invalid_size;
5456
11
      }
5457
2.87k
      at += padded_size;
5458
2.87k
      break;
5459
3.10k
    case SCTP_ZERO_CHECKSUM_ACCEPTABLE:
5460
3.10k
      if (padded_size != sizeof(struct sctp_zero_checksum_acceptable)) {
5461
3
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error checksum acceptable %d\n", plen);
5462
3
        goto invalid_size;
5463
3
      }
5464
3.10k
      if (edmid != NULL) {
5465
2.25k
        phdr = sctp_get_next_param(mat, at,
5466
2.25k
                                   (struct sctp_paramhdr *)&zero_chksum,
5467
2.25k
                                   sizeof(struct sctp_zero_checksum_acceptable));
5468
2.25k
        if (phdr != NULL) {
5469
2.24k
          zero_chksum_p = (struct sctp_zero_checksum_acceptable *)phdr;
5470
2.24k
          *edmid = ntohl(zero_chksum_p->edmid);
5471
2.24k
        }
5472
2.25k
      }
5473
3.10k
      at += padded_size;
5474
3.10k
      break;
5475
7.51k
    case SCTP_RANDOM:
5476
7.51k
      if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
5477
7
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
5478
7
        goto invalid_size;
5479
7
      }
5480
7.51k
      at += padded_size;
5481
7.51k
      break;
5482
3.00k
    case SCTP_SET_PRIM_ADDR:
5483
4.04k
    case SCTP_DEL_IP_ADDRESS:
5484
5.21k
    case SCTP_ADD_IP_ADDRESS:
5485
5.21k
      if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
5486
997
          (padded_size != sizeof(struct sctp_asconf_addr_param))) {
5487
28
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
5488
28
        goto invalid_size;
5489
28
      }
5490
5.18k
      at += padded_size;
5491
5.18k
      break;
5492
      /* Param's with a fixed size */
5493
731
    case SCTP_IPV4_ADDRESS:
5494
731
      if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
5495
14
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
5496
14
        goto invalid_size;
5497
14
      }
5498
717
      at += padded_size;
5499
717
      break;
5500
678
    case SCTP_IPV6_ADDRESS:
5501
678
      if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
5502
5
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
5503
5
        goto invalid_size;
5504
5
      }
5505
673
      at += padded_size;
5506
673
      break;
5507
1.14k
    case SCTP_COOKIE_PRESERVE:
5508
1.14k
      if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
5509
11
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
5510
11
        goto invalid_size;
5511
11
      }
5512
1.13k
      at += padded_size;
5513
1.13k
      break;
5514
4.28k
    case SCTP_HAS_NAT_SUPPORT:
5515
4.28k
      if (padded_size != sizeof(struct sctp_paramhdr)) {
5516
4
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error nat support %d\n", plen);
5517
4
        goto invalid_size;
5518
4
      }
5519
4.28k
      *nat_friendly = 1;
5520
4.28k
      at += padded_size;
5521
4.28k
      break;
5522
11.4k
    case SCTP_PRSCTP_SUPPORTED:
5523
11.4k
      if (padded_size != sizeof(struct sctp_paramhdr)) {
5524
13
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp %d\n", plen);
5525
13
        goto invalid_size;
5526
13
      }
5527
11.4k
      at += padded_size;
5528
11.4k
      break;
5529
12.8k
    case SCTP_ECN_CAPABLE:
5530
12.8k
      if (padded_size != sizeof(struct sctp_paramhdr)) {
5531
12
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
5532
12
        goto invalid_size;
5533
12
      }
5534
12.8k
      at += padded_size;
5535
12.8k
      break;
5536
8.36k
    case SCTP_ULP_ADAPTATION:
5537
8.36k
      if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
5538
8
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5539
8
        goto invalid_size;
5540
8
      }
5541
8.35k
      at += padded_size;
5542
8.35k
      break;
5543
912
    case SCTP_SUCCESS_REPORT:
5544
912
      if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5545
6
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5546
6
        goto invalid_size;
5547
6
      }
5548
906
      at += padded_size;
5549
906
      break;
5550
15
    case SCTP_HOSTNAME_ADDRESS:
5551
15
    {
5552
      /* Hostname parameters are deprecated. */
5553
15
      struct sctp_gen_error_cause *cause;
5554
15
      int l_len;
5555
5556
15
      SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5557
15
      *abort_processing = 1;
5558
15
      sctp_m_freem(op_err);
5559
15
      op_err = NULL;
5560
15
      op_err_last = NULL;
5561
15
#ifdef INET6
5562
15
      l_len = SCTP_MIN_OVERHEAD;
5563
#else
5564
      l_len = SCTP_MIN_V4_OVERHEAD;
5565
#endif
5566
15
      l_len += sizeof(struct sctp_chunkhdr);
5567
15
      l_len += sizeof(struct sctp_gen_error_cause);
5568
15
      op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5569
15
      if (op_err != NULL) {
5570
        /*
5571
         * Pre-reserve space for IP, SCTP, and
5572
         * chunk header.
5573
         */
5574
15
#ifdef INET6
5575
15
        SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5576
#else
5577
        SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5578
#endif
5579
15
        SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5580
15
        SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5581
15
        SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5582
15
        cause = mtod(op_err, struct sctp_gen_error_cause *);
5583
15
        cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5584
15
        cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen));
5585
15
        SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5586
15
        if (SCTP_BUF_NEXT(op_err) == NULL) {
5587
0
          sctp_m_freem(op_err);
5588
0
          op_err = NULL;
5589
0
          op_err_last = NULL;
5590
0
        }
5591
15
      }
5592
15
      return (op_err);
5593
912
    }
5594
180k
    default:
5595
      /*
5596
       * we do not recognize the parameter figure out what
5597
       * we do.
5598
       */
5599
180k
      SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5600
180k
      if ((ptype & 0x4000) == 0x4000) {
5601
        /* Report bit is set?? */
5602
160k
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5603
160k
        if (op_err == NULL) {
5604
781
          int l_len;
5605
          /* Ok need to try to get an mbuf */
5606
781
#ifdef INET6
5607
781
          l_len = SCTP_MIN_OVERHEAD;
5608
#else
5609
          l_len = SCTP_MIN_V4_OVERHEAD;
5610
#endif
5611
781
          l_len += sizeof(struct sctp_chunkhdr);
5612
781
          l_len += sizeof(struct sctp_paramhdr);
5613
781
          op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5614
781
          if (op_err) {
5615
781
            SCTP_BUF_LEN(op_err) = 0;
5616
781
#ifdef INET6
5617
781
            SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5618
#else
5619
            SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5620
#endif
5621
781
            SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5622
781
            SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5623
781
            op_err_last = op_err;
5624
781
          }
5625
781
        }
5626
160k
        if (op_err != NULL) {
5627
          /* If we have space */
5628
160k
          struct sctp_paramhdr *param;
5629
5630
160k
          if (pad_needed > 0) {
5631
8.09k
            op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed);
5632
8.09k
          }
5633
160k
          if (op_err_last == NULL) {
5634
0
            sctp_m_freem(op_err);
5635
0
            op_err = NULL;
5636
0
            op_err_last = NULL;
5637
0
            goto more_processing;
5638
0
          }
5639
160k
          if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) {
5640
143k
            m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
5641
143k
            if (m_tmp == NULL) {
5642
0
              sctp_m_freem(op_err);
5643
0
              op_err = NULL;
5644
0
              op_err_last = NULL;
5645
0
              goto more_processing;
5646
0
            }
5647
143k
            SCTP_BUF_LEN(m_tmp) = 0;
5648
143k
            SCTP_BUF_NEXT(m_tmp) = NULL;
5649
143k
            SCTP_BUF_NEXT(op_err_last) = m_tmp;
5650
143k
            op_err_last = m_tmp;
5651
143k
          }
5652
160k
          param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t) + SCTP_BUF_LEN(op_err_last));
5653
160k
          param->param_type = htons(SCTP_UNRECOG_PARAM);
5654
160k
          param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen);
5655
160k
          SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr);
5656
160k
          SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5657
160k
          if (SCTP_BUF_NEXT(op_err_last) == NULL) {
5658
0
            sctp_m_freem(op_err);
5659
0
            op_err = NULL;
5660
0
            op_err_last = NULL;
5661
0
            goto more_processing;
5662
160k
          } else {
5663
322k
            while (SCTP_BUF_NEXT(op_err_last) != NULL) {
5664
161k
              op_err_last = SCTP_BUF_NEXT(op_err_last);
5665
161k
            }
5666
160k
          }
5667
160k
          if (plen % 4 != 0) {
5668
8.31k
            pad_needed = 4 - (plen % 4);
5669
152k
          } else {
5670
152k
            pad_needed = 0;
5671
152k
          }
5672
160k
        }
5673
160k
      }
5674
180k
    more_processing:
5675
180k
      if ((ptype & 0x8000) == 0x0000) {
5676
407
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5677
407
        return (op_err);
5678
180k
      } else {
5679
        /* skip this chunk and continue processing */
5680
180k
        SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5681
180k
        at += SCTP_SIZE32(plen);
5682
180k
      }
5683
180k
      break;
5684
291k
    }
5685
290k
    phdr = sctp_get_next_param(mat, at, &params, sizeof(params));
5686
290k
  }
5687
8.11k
  return (op_err);
5688
255
 invalid_size:
5689
255
  SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5690
255
  *abort_processing = 1;
5691
255
  sctp_m_freem(op_err);
5692
255
  op_err = NULL;
5693
255
  op_err_last = NULL;
5694
255
  if (phdr != NULL) {
5695
255
    struct sctp_paramhdr *param;
5696
255
    int l_len;
5697
255
#ifdef INET6
5698
255
    l_len = SCTP_MIN_OVERHEAD;
5699
#else
5700
    l_len = SCTP_MIN_V4_OVERHEAD;
5701
#endif
5702
255
    l_len += sizeof(struct sctp_chunkhdr);
5703
255
    l_len += (2 * sizeof(struct sctp_paramhdr));
5704
255
    op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5705
255
    if (op_err) {
5706
255
      SCTP_BUF_LEN(op_err) = 0;
5707
255
#ifdef INET6
5708
255
      SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5709
#else
5710
      SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5711
#endif
5712
255
      SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5713
255
      SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5714
255
      SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr);
5715
255
      param = mtod(op_err, struct sctp_paramhdr *);
5716
255
      param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5717
255
      param->param_length = htons(2 * sizeof(struct sctp_paramhdr));
5718
255
      param++;
5719
255
      param->param_type = htons(ptype);
5720
255
      param->param_length = htons(plen);
5721
255
    }
5722
255
  }
5723
255
  return (op_err);
5724
8.79k
}
5725
5726
/*
5727
 * Given a INIT chunk, look through the parameters to verify that there
5728
 * are no new addresses.
5729
 * Return true, if there is a new address or there is a problem parsing
5730
   the parameters. Provide an optional error cause used when sending an ABORT.
5731
 * Return false, if there are no new addresses and there is no problem in
5732
   parameter processing.
5733
 */
5734
static bool
5735
sctp_are_there_new_addresses(struct sctp_association *asoc,
5736
    struct mbuf *in_initpkt, int offset, int limit, struct sockaddr *src,
5737
    struct mbuf **op_err)
5738
0
{
5739
0
  struct sockaddr *sa_touse;
5740
0
  struct sockaddr *sa;
5741
0
  struct sctp_paramhdr *phdr, params;
5742
0
  struct sctp_nets *net;
5743
0
#ifdef INET
5744
0
  struct sockaddr_in sin4, *sa4;
5745
0
#endif
5746
0
#ifdef INET6
5747
0
  struct sockaddr_in6 sin6, *sa6;
5748
0
#endif
5749
0
#if defined(__Userspace__)
5750
0
  struct sockaddr_conn *sac;
5751
0
#endif
5752
0
  uint16_t ptype, plen;
5753
0
  bool fnd, check_src;
5754
5755
0
  *op_err = NULL;
5756
0
#ifdef INET
5757
0
  memset(&sin4, 0, sizeof(sin4));
5758
0
  sin4.sin_family = AF_INET;
5759
#ifdef HAVE_SIN_LEN
5760
  sin4.sin_len = sizeof(sin4);
5761
#endif
5762
0
#endif
5763
0
#ifdef INET6
5764
0
  memset(&sin6, 0, sizeof(sin6));
5765
0
  sin6.sin6_family = AF_INET6;
5766
#ifdef HAVE_SIN6_LEN
5767
  sin6.sin6_len = sizeof(sin6);
5768
#endif
5769
0
#endif
5770
  /* First what about the src address of the pkt ? */
5771
0
  check_src = false;
5772
0
  switch (src->sa_family) {
5773
0
#ifdef INET
5774
0
  case AF_INET:
5775
0
    if (asoc->scope.ipv4_addr_legal) {
5776
0
      check_src = true;
5777
0
    }
5778
0
    break;
5779
0
#endif
5780
0
#ifdef INET6
5781
0
  case AF_INET6:
5782
0
    if (asoc->scope.ipv6_addr_legal) {
5783
0
      check_src = true;
5784
0
    }
5785
0
    break;
5786
0
#endif
5787
0
#if defined(__Userspace__)
5788
0
  case AF_CONN:
5789
0
    if (asoc->scope.conn_addr_legal) {
5790
0
      check_src = true;
5791
0
    }
5792
0
    break;
5793
0
#endif
5794
0
  default:
5795
    /* TSNH */
5796
0
    break;
5797
0
  }
5798
0
  if (check_src) {
5799
0
    fnd = false;
5800
0
    TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5801
0
      sa = (struct sockaddr *)&net->ro._l_addr;
5802
0
      if (sa->sa_family == src->sa_family) {
5803
0
#ifdef INET
5804
0
        if (sa->sa_family == AF_INET) {
5805
0
          struct sockaddr_in *src4;
5806
5807
0
          sa4 = (struct sockaddr_in *)sa;
5808
0
          src4 = (struct sockaddr_in *)src;
5809
0
          if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) {
5810
0
            fnd = true;
5811
0
            break;
5812
0
          }
5813
0
        }
5814
0
#endif
5815
0
#ifdef INET6
5816
0
        if (sa->sa_family == AF_INET6) {
5817
0
          struct sockaddr_in6 *src6;
5818
5819
0
          sa6 = (struct sockaddr_in6 *)sa;
5820
0
          src6 = (struct sockaddr_in6 *)src;
5821
0
          if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) {
5822
0
            fnd = true;
5823
0
            break;
5824
0
          }
5825
0
        }
5826
0
#endif
5827
0
#if defined(__Userspace__)
5828
0
        if (sa->sa_family == AF_CONN) {
5829
0
          struct sockaddr_conn *srcc;
5830
5831
0
          sac = (struct sockaddr_conn *)sa;
5832
0
          srcc = (struct sockaddr_conn *)src;
5833
0
          if (sac->sconn_addr == srcc->sconn_addr) {
5834
0
            fnd = true;
5835
0
            break;
5836
0
          }
5837
0
        }
5838
0
#endif
5839
0
      }
5840
0
    }
5841
0
    if (!fnd) {
5842
      /*
5843
       * If sending an ABORT in case of an additional address,
5844
       * don't use the new address error cause.
5845
       * This looks no different than if no listener was
5846
       * present.
5847
       */
5848
0
      *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5849
0
      return (true);
5850
0
    }
5851
0
  }
5852
  /* Ok so far lets munge through the rest of the packet */
5853
0
  offset += sizeof(struct sctp_init_chunk);
5854
0
  phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5855
0
  while (phdr) {
5856
0
    sa_touse = NULL;
5857
0
    ptype = ntohs(phdr->param_type);
5858
0
    plen = ntohs(phdr->param_length);
5859
0
    if (offset + plen > limit) {
5860
0
      *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Partial parameter");
5861
0
      return (true);
5862
0
    }
5863
0
    if (plen < sizeof(struct sctp_paramhdr)) {
5864
0
      *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length too small");
5865
0
      return (true);
5866
0
    }
5867
0
    switch (ptype) {
5868
0
#ifdef INET
5869
0
    case SCTP_IPV4_ADDRESS:
5870
0
    {
5871
0
      struct sctp_ipv4addr_param *p4, p4_buf;
5872
5873
0
      if (plen != sizeof(struct sctp_ipv4addr_param)) {
5874
0
        *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5875
0
        return (true);
5876
0
      }
5877
0
      phdr = sctp_get_next_param(in_initpkt, offset,
5878
0
          (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5879
0
      if (phdr == NULL) {
5880
0
        *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5881
0
        return (true);
5882
0
      }
5883
0
      if (asoc->scope.ipv4_addr_legal) {
5884
0
        p4 = (struct sctp_ipv4addr_param *)phdr;
5885
0
        sin4.sin_addr.s_addr = p4->addr;
5886
0
        sa_touse = (struct sockaddr *)&sin4;
5887
0
      }
5888
0
      break;
5889
0
    }
5890
0
#endif
5891
0
#ifdef INET6
5892
0
    case SCTP_IPV6_ADDRESS:
5893
0
    {
5894
0
      struct sctp_ipv6addr_param *p6, p6_buf;
5895
5896
0
      if (plen != sizeof(struct sctp_ipv6addr_param)) {
5897
0
        *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5898
0
        return (true);
5899
0
      }
5900
0
      phdr = sctp_get_next_param(in_initpkt, offset,
5901
0
          (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5902
0
      if (phdr == NULL) {
5903
0
        *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5904
0
        return (true);
5905
0
      }
5906
0
      if (asoc->scope.ipv6_addr_legal) {
5907
0
        p6 = (struct sctp_ipv6addr_param *)phdr;
5908
0
        memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5909
0
               sizeof(p6->addr));
5910
0
        sa_touse = (struct sockaddr *)&sin6;
5911
0
      }
5912
0
      break;
5913
0
    }
5914
0
#endif
5915
0
    default:
5916
0
      sa_touse = NULL;
5917
0
      break;
5918
0
    }
5919
0
    if (sa_touse) {
5920
      /* ok, sa_touse points to one to check */
5921
0
      fnd = false;
5922
0
      TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5923
0
        sa = (struct sockaddr *)&net->ro._l_addr;
5924
0
        if (sa->sa_family != sa_touse->sa_family) {
5925
0
          continue;
5926
0
        }
5927
0
#ifdef INET
5928
0
        if (sa->sa_family == AF_INET) {
5929
0
          sa4 = (struct sockaddr_in *)sa;
5930
0
          if (sa4->sin_addr.s_addr ==
5931
0
              sin4.sin_addr.s_addr) {
5932
0
            fnd = true;
5933
0
            break;
5934
0
          }
5935
0
        }
5936
0
#endif
5937
0
#ifdef INET6
5938
0
        if (sa->sa_family == AF_INET6) {
5939
0
          sa6 = (struct sockaddr_in6 *)sa;
5940
0
          if (SCTP6_ARE_ADDR_EQUAL(
5941
0
              sa6, &sin6)) {
5942
0
            fnd = true;
5943
0
            break;
5944
0
          }
5945
0
        }
5946
0
#endif
5947
0
      }
5948
0
      if (!fnd) {
5949
        /*
5950
         * If sending an ABORT in case of an additional
5951
         * address, don't use the new address error
5952
         * cause.
5953
         * This looks no different than if no listener
5954
         * was present.
5955
         */
5956
0
        *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5957
0
        return (true);
5958
0
      }
5959
0
    }
5960
0
    offset += SCTP_SIZE32(plen);
5961
0
    if (offset >= limit) {
5962
0
      break;
5963
0
    }
5964
0
    phdr = sctp_get_next_param(in_initpkt, offset, &params, sizeof(params));
5965
0
  }
5966
0
  return (false);
5967
0
}
5968
5969
/*
5970
 * Given a MBUF chain that was sent into us containing an INIT. Build a
5971
 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5972
 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5973
 * message (i.e. the struct sctp_init_msg).
5974
 */
5975
void
5976
sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5977
                       struct sctp_nets *src_net, struct mbuf *init_pkt,
5978
                       int iphlen, int offset,
5979
                       struct sockaddr *src, struct sockaddr *dst,
5980
                       struct sctphdr *sh, struct sctp_init_chunk *init_chk,
5981
#if defined(__FreeBSD__) && !defined(__Userspace__)
5982
                       uint8_t mflowtype, uint32_t mflowid,
5983
#endif
5984
                       uint32_t vrf_id, uint16_t port)
5985
969
{
5986
969
  struct sctp_association *asoc;
5987
969
  struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err;
5988
969
  struct sctp_init_ack_chunk *initack;
5989
969
  struct sctp_adaptation_layer_indication *ali;
5990
969
  struct sctp_zero_checksum_acceptable *zero_chksum;
5991
969
  struct sctp_supported_chunk_types_param *pr_supported;
5992
969
  struct sctp_paramhdr *ph;
5993
969
  union sctp_sockstore *over_addr;
5994
969
  struct sctp_scoping scp;
5995
969
  struct timeval now;
5996
969
#ifdef INET
5997
969
  struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
5998
969
  struct sockaddr_in *src4 = (struct sockaddr_in *)src;
5999
969
  struct sockaddr_in *sin;
6000
969
#endif
6001
969
#ifdef INET6
6002
969
  struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
6003
969
  struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src;
6004
969
  struct sockaddr_in6 *sin6;
6005
969
#endif
6006
969
#if defined(__Userspace__)
6007
969
  struct sockaddr_conn *dstconn = (struct sockaddr_conn *)dst;
6008
969
  struct sockaddr_conn *srcconn = (struct sockaddr_conn *)src;
6009
969
  struct sockaddr_conn *sconn;
6010
969
#endif
6011
969
  struct sockaddr *to;
6012
969
  struct sctp_state_cookie stc;
6013
969
  struct sctp_nets *net = NULL;
6014
969
  uint8_t *signature = NULL;
6015
969
  int cnt_inits_to = 0;
6016
969
  uint16_t his_limit, i_want;
6017
969
  int abort_flag;
6018
969
  int nat_friendly = 0;
6019
969
  int error;
6020
969
  struct socket *so;
6021
969
  uint32_t edmid;
6022
969
  uint16_t num_ext, chunk_len, padding_len, parameter_len;
6023
969
  bool use_zero_crc;
6024
6025
969
  if (stcb) {
6026
0
    asoc = &stcb->asoc;
6027
969
  } else {
6028
969
    asoc = NULL;
6029
969
  }
6030
969
  if ((asoc != NULL) &&
6031
0
      (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
6032
0
    if (sctp_are_there_new_addresses(asoc, init_pkt, offset, offset + ntohs(init_chk->ch.chunk_length), src, &op_err)) {
6033
      /*
6034
       * new addresses, out of here in non-cookie-wait states
6035
       */
6036
0
      sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
6037
#if defined(__FreeBSD__) && !defined(__Userspace__)
6038
                      mflowtype, mflowid, inp->fibnum,
6039
#endif
6040
0
                      vrf_id, port);
6041
0
      return;
6042
0
    }
6043
0
    if (src_net != NULL && (src_net->port != port)) {
6044
      /*
6045
       * change of remote encapsulation port, out of here in
6046
       * non-cookie-wait states
6047
       *
6048
       * Send an ABORT, without an specific error cause.
6049
       * This looks no different than if no listener
6050
       * was present.
6051
       */
6052
0
      op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6053
0
                                   "Remote encapsulation port changed");
6054
0
      sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
6055
#if defined(__FreeBSD__) && !defined(__Userspace__)
6056
                      mflowtype, mflowid, inp->fibnum,
6057
#endif
6058
0
                      vrf_id, port);
6059
0
      return;
6060
0
    }
6061
0
  }
6062
969
  abort_flag = 0;
6063
969
  op_err = sctp_arethere_unrecognized_parameters(init_pkt,
6064
969
                                                 (offset + sizeof(struct sctp_init_chunk)),
6065
969
                                                 &abort_flag,
6066
969
                                                 (struct sctp_chunkhdr *)init_chk,
6067
969
                                                 &nat_friendly, NULL, &edmid);
6068
969
  if (abort_flag) {
6069
211
  do_a_abort:
6070
211
    if (op_err == NULL) {
6071
0
      char msg[SCTP_DIAG_INFO_LEN];
6072
6073
0
      SCTP_SNPRINTF(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__);
6074
0
      op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6075
0
                                   msg);
6076
0
    }
6077
211
    sctp_send_abort(init_pkt, iphlen, src, dst, sh,
6078
211
        init_chk->init.initiate_tag, op_err,
6079
#if defined(__FreeBSD__) && !defined(__Userspace__)
6080
                    mflowtype, mflowid, inp->fibnum,
6081
#endif
6082
211
                    vrf_id, port);
6083
211
    return;
6084
211
  }
6085
758
  m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
6086
758
  if (m == NULL) {
6087
    /* No memory, INIT timer will re-attempt. */
6088
0
    sctp_m_freem(op_err);
6089
0
    return;
6090
0
  }
6091
758
  chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk);
6092
758
  padding_len = 0;
6093
6094
  /*
6095
   * We might not overwrite the identification[] completely and on
6096
   * some platforms time_entered will contain some padding.
6097
   * Therefore zero out the cookie to avoid putting
6098
   * uninitialized memory on the wire.
6099
   */
6100
758
  memset(&stc, 0, sizeof(struct sctp_state_cookie));
6101
6102
  /* the time I built cookie */
6103
758
  (void)SCTP_GETTIME_TIMEVAL(&now);
6104
758
  stc.time_entered.tv_sec = now.tv_sec;
6105
758
  stc.time_entered.tv_usec = now.tv_usec;
6106
6107
  /* populate any tie tags */
6108
758
  if (asoc != NULL) {
6109
    /* unlock before tag selections */
6110
0
    stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
6111
0
    stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
6112
0
    stc.cookie_life = asoc->cookie_life;
6113
0
    net = asoc->primary_destination;
6114
758
  } else {
6115
758
    stc.tie_tag_my_vtag = 0;
6116
758
    stc.tie_tag_peer_vtag = 0;
6117
    /* life I will award this cookie */
6118
758
    stc.cookie_life = inp->sctp_ep.def_cookie_life;
6119
758
  }
6120
6121
  /* copy in the ports for later check */
6122
758
  stc.myport = sh->dest_port;
6123
758
  stc.peerport = sh->src_port;
6124
6125
  /*
6126
   * If we wanted to honor cookie life extensions, we would add to
6127
   * stc.cookie_life. For now we should NOT honor any extension
6128
   */
6129
758
  stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
6130
758
  if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6131
0
    stc.ipv6_addr_legal = 1;
6132
0
    if (SCTP_IPV6_V6ONLY(inp)) {
6133
0
      stc.ipv4_addr_legal = 0;
6134
0
    } else {
6135
0
      stc.ipv4_addr_legal = 1;
6136
0
    }
6137
0
#if defined(__Userspace__)
6138
0
    stc.conn_addr_legal = 0;
6139
0
#endif
6140
758
  } else {
6141
758
    stc.ipv6_addr_legal = 0;
6142
758
#if defined(__Userspace__)
6143
758
    if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
6144
758
      stc.conn_addr_legal = 1;
6145
758
      stc.ipv4_addr_legal = 0;
6146
758
    } else {
6147
0
      stc.conn_addr_legal = 0;
6148
0
      stc.ipv4_addr_legal = 1;
6149
0
    }
6150
#else
6151
    stc.ipv4_addr_legal = 1;
6152
#endif
6153
758
  }
6154
758
  stc.ipv4_scope = 0;
6155
758
  if (net == NULL) {
6156
758
    to = src;
6157
758
    switch (dst->sa_family) {
6158
0
#ifdef INET
6159
0
    case AF_INET:
6160
0
    {
6161
      /* lookup address */
6162
0
      stc.address[0] = src4->sin_addr.s_addr;
6163
0
      stc.address[1] = 0;
6164
0
      stc.address[2] = 0;
6165
0
      stc.address[3] = 0;
6166
0
      stc.addr_type = SCTP_IPV4_ADDRESS;
6167
      /* local from address */
6168
0
      stc.laddress[0] = dst4->sin_addr.s_addr;
6169
0
      stc.laddress[1] = 0;
6170
0
      stc.laddress[2] = 0;
6171
0
      stc.laddress[3] = 0;
6172
0
      stc.laddr_type = SCTP_IPV4_ADDRESS;
6173
      /* scope_id is only for v6 */
6174
0
      stc.scope_id = 0;
6175
0
      if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) ||
6176
0
          (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))) {
6177
0
        stc.ipv4_scope = 1;
6178
0
      }
6179
      /* Must use the address in this case */
6180
0
      if (sctp_is_address_on_local_host(src, vrf_id)) {
6181
0
        stc.loopback_scope = 1;
6182
0
        stc.ipv4_scope = 1;
6183
0
        stc.site_scope = 1;
6184
0
        stc.local_scope = 0;
6185
0
      }
6186
0
      break;
6187
0
    }
6188
0
#endif
6189
0
#ifdef INET6
6190
0
    case AF_INET6:
6191
0
    {
6192
0
      stc.addr_type = SCTP_IPV6_ADDRESS;
6193
0
      memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr));
6194
#if defined(__FreeBSD__) && !defined(__Userspace__)
6195
      stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr));
6196
#else
6197
0
      stc.scope_id = 0;
6198
0
#endif
6199
0
      if (sctp_is_address_on_local_host(src, vrf_id)) {
6200
0
        stc.loopback_scope = 1;
6201
0
        stc.local_scope = 0;
6202
0
        stc.site_scope = 1;
6203
0
        stc.ipv4_scope = 1;
6204
0
      } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) ||
6205
0
                 IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) {
6206
        /*
6207
         * If the new destination or source is a
6208
         * LINK_LOCAL we must have common both site and
6209
         * local scope. Don't set local scope though
6210
         * since we must depend on the source to be
6211
         * added implicitly. We cannot assure just
6212
         * because we share one link that all links are
6213
         * common.
6214
         */
6215
#if defined(__APPLE__) && !defined(__Userspace__)
6216
        /* Mac OS X currently doesn't have in6_getscope() */
6217
        stc.scope_id = src6->sin6_addr.s6_addr16[1];
6218
#endif
6219
0
        stc.local_scope = 0;
6220
0
        stc.site_scope = 1;
6221
0
        stc.ipv4_scope = 1;
6222
        /*
6223
         * we start counting for the private address
6224
         * stuff at 1. since the link local we
6225
         * source from won't show up in our scoped
6226
         * count.
6227
         */
6228
0
        cnt_inits_to = 1;
6229
        /* pull out the scope_id from incoming pkt */
6230
0
      } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) ||
6231
0
                 IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) {
6232
        /*
6233
         * If the new destination or source is
6234
         * SITE_LOCAL then we must have site scope in
6235
         * common.
6236
         */
6237
0
        stc.site_scope = 1;
6238
0
      }
6239
0
      memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr));
6240
0
      stc.laddr_type = SCTP_IPV6_ADDRESS;
6241
0
      break;
6242
0
    }
6243
0
#endif
6244
0
#if defined(__Userspace__)
6245
758
    case AF_CONN:
6246
758
    {
6247
      /* lookup address */
6248
758
      stc.address[0] = 0;
6249
758
      stc.address[1] = 0;
6250
758
      stc.address[2] = 0;
6251
758
      stc.address[3] = 0;
6252
758
      memcpy(&stc.address, &srcconn->sconn_addr, sizeof(void *));
6253
758
      stc.addr_type = SCTP_CONN_ADDRESS;
6254
      /* local from address */
6255
758
      stc.laddress[0] = 0;
6256
758
      stc.laddress[1] = 0;
6257
758
      stc.laddress[2] = 0;
6258
758
      stc.laddress[3] = 0;
6259
758
      memcpy(&stc.laddress, &dstconn->sconn_addr, sizeof(void *));
6260
758
      stc.laddr_type = SCTP_CONN_ADDRESS;
6261
      /* scope_id is only for v6 */
6262
758
      stc.scope_id = 0;
6263
758
      break;
6264
0
    }
6265
0
#endif
6266
0
    default:
6267
      /* TSNH */
6268
0
      goto do_a_abort;
6269
0
      break;
6270
758
    }
6271
758
  } else {
6272
    /* set the scope per the existing tcb */
6273
6274
0
#ifdef INET6
6275
0
    struct sctp_nets *lnet;
6276
0
#endif
6277
6278
0
    stc.loopback_scope = asoc->scope.loopback_scope;
6279
0
    stc.ipv4_scope = asoc->scope.ipv4_local_scope;
6280
0
    stc.site_scope = asoc->scope.site_scope;
6281
0
    stc.local_scope = asoc->scope.local_scope;
6282
0
#ifdef INET6
6283
    /* Why do we not consider IPv4 LL addresses? */
6284
0
    TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
6285
0
      if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
6286
0
        if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
6287
          /*
6288
           * if we have a LL address, start
6289
           * counting at 1.
6290
           */
6291
0
          cnt_inits_to = 1;
6292
0
        }
6293
0
      }
6294
0
    }
6295
0
#endif
6296
    /* use the net pointer */
6297
0
    to = (struct sockaddr *)&net->ro._l_addr;
6298
0
    switch (to->sa_family) {
6299
0
#ifdef INET
6300
0
    case AF_INET:
6301
0
      sin = (struct sockaddr_in *)to;
6302
0
      stc.address[0] = sin->sin_addr.s_addr;
6303
0
      stc.address[1] = 0;
6304
0
      stc.address[2] = 0;
6305
0
      stc.address[3] = 0;
6306
0
      stc.addr_type = SCTP_IPV4_ADDRESS;
6307
0
      if (net->src_addr_selected == 0) {
6308
        /*
6309
         * strange case here, the INIT should have
6310
         * did the selection.
6311
         */
6312
0
        net->ro._s_addr = sctp_source_address_selection(inp,
6313
0
                    stcb, (sctp_route_t *)&net->ro,
6314
0
                    net, 0, vrf_id);
6315
0
        if (net->ro._s_addr == NULL) {
6316
0
          sctp_m_freem(op_err);
6317
0
          sctp_m_freem(m);
6318
0
          return;
6319
0
        }
6320
6321
0
        net->src_addr_selected = 1;
6322
0
      }
6323
0
      stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
6324
0
      stc.laddress[1] = 0;
6325
0
      stc.laddress[2] = 0;
6326
0
      stc.laddress[3] = 0;
6327
0
      stc.laddr_type = SCTP_IPV4_ADDRESS;
6328
      /* scope_id is only for v6 */
6329
0
      stc.scope_id = 0;
6330
0
      break;
6331
0
#endif
6332
0
#ifdef INET6
6333
0
    case AF_INET6:
6334
0
      sin6 = (struct sockaddr_in6 *)to;
6335
0
      memcpy(&stc.address, &sin6->sin6_addr,
6336
0
             sizeof(struct in6_addr));
6337
0
      stc.addr_type = SCTP_IPV6_ADDRESS;
6338
0
      stc.scope_id = sin6->sin6_scope_id;
6339
0
      if (net->src_addr_selected == 0) {
6340
        /*
6341
         * strange case here, the INIT should have
6342
         * done the selection.
6343
         */
6344
0
        net->ro._s_addr = sctp_source_address_selection(inp,
6345
0
                    stcb, (sctp_route_t *)&net->ro,
6346
0
                    net, 0, vrf_id);
6347
0
        if (net->ro._s_addr == NULL) {
6348
0
          sctp_m_freem(op_err);
6349
0
          sctp_m_freem(m);
6350
0
          return;
6351
0
        }
6352
6353
0
        net->src_addr_selected = 1;
6354
0
      }
6355
0
      memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
6356
0
             sizeof(struct in6_addr));
6357
0
      stc.laddr_type = SCTP_IPV6_ADDRESS;
6358
0
      break;
6359
0
#endif
6360
0
#if defined(__Userspace__)
6361
0
    case AF_CONN:
6362
0
      sconn = (struct sockaddr_conn *)to;
6363
0
      stc.address[0] = 0;
6364
0
      stc.address[1] = 0;
6365
0
      stc.address[2] = 0;
6366
0
      stc.address[3] = 0;
6367
0
      memcpy(&stc.address, &sconn->sconn_addr, sizeof(void *));
6368
0
      stc.addr_type = SCTP_CONN_ADDRESS;
6369
0
      stc.laddress[0] = 0;
6370
0
      stc.laddress[1] = 0;
6371
0
      stc.laddress[2] = 0;
6372
0
      stc.laddress[3] = 0;
6373
0
      memcpy(&stc.laddress, &sconn->sconn_addr, sizeof(void *));
6374
0
      stc.laddr_type = SCTP_CONN_ADDRESS;
6375
0
      stc.scope_id = 0;
6376
0
      break;
6377
0
#endif
6378
0
    }
6379
0
  }
6380
758
  if (asoc != NULL) {
6381
0
    stc.rcv_edmid = asoc->rcv_edmid;
6382
758
  } else {
6383
758
    stc.rcv_edmid = inp->rcv_edmid;
6384
758
  }
6385
  /* Now lets put the SCTP header in place */
6386
758
  initack = mtod(m, struct sctp_init_ack_chunk *);
6387
  /* Save it off for quick ref */
6388
758
  stc.peers_vtag = ntohl(init_chk->init.initiate_tag);
6389
  /* who are we */
6390
758
  memcpy(stc.identification, SCTP_VERSION_STRING,
6391
758
         min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
6392
758
  memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
6393
  /* now the chunk header */
6394
758
  initack->ch.chunk_type = SCTP_INITIATION_ACK;
6395
758
  initack->ch.chunk_flags = 0;
6396
  /* fill in later from mbuf we build */
6397
758
  initack->ch.chunk_length = 0;
6398
  /* place in my tag */
6399
758
  if ((asoc != NULL) &&
6400
0
      ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
6401
0
       (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) ||
6402
0
       (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) {
6403
    /* re-use the v-tags and init-seq here */
6404
0
    initack->init.initiate_tag = htonl(asoc->my_vtag);
6405
0
    initack->init.initial_tsn = htonl(asoc->init_seq_number);
6406
758
  } else {
6407
758
    uint32_t vtag, itsn;
6408
6409
758
    if (asoc) {
6410
0
      atomic_add_int(&asoc->refcnt, 1);
6411
0
      SCTP_TCB_UNLOCK(stcb);
6412
0
    new_tag:
6413
0
      SCTP_INP_INFO_RLOCK();
6414
0
      vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
6415
0
      SCTP_INP_INFO_RUNLOCK();
6416
0
      if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
6417
        /* Got a duplicate vtag on some guy behind a nat
6418
         * make sure we don't use it.
6419
         */
6420
0
        goto new_tag;
6421
0
      }
6422
0
      initack->init.initiate_tag = htonl(vtag);
6423
      /* get a TSN to use too */
6424
0
      itsn = sctp_select_initial_TSN(&inp->sctp_ep);
6425
0
      initack->init.initial_tsn = htonl(itsn);
6426
0
      SCTP_TCB_LOCK(stcb);
6427
0
      atomic_subtract_int(&asoc->refcnt, 1);
6428
758
    } else {
6429
758
      SCTP_INP_INCR_REF(inp);
6430
758
      SCTP_INP_RUNLOCK(inp);
6431
758
      SCTP_INP_INFO_RLOCK();
6432
758
      vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
6433
758
      SCTP_INP_INFO_RUNLOCK();
6434
758
      initack->init.initiate_tag = htonl(vtag);
6435
      /* get a TSN to use too */
6436
758
      initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
6437
758
      SCTP_INP_RLOCK(inp);
6438
758
      SCTP_INP_DECR_REF(inp);
6439
758
    }
6440
758
  }
6441
  /* save away my tag to */
6442
758
  stc.my_vtag = initack->init.initiate_tag;
6443
6444
  /* set up some of the credits. */
6445
758
  so = inp->sctp_socket;
6446
758
  if (so == NULL) {
6447
    /* memory problem */
6448
0
    sctp_m_freem(op_err);
6449
0
    sctp_m_freem(m);
6450
0
    return;
6451
758
  } else {
6452
758
    initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
6453
758
  }
6454
  /* set what I want */
6455
758
  his_limit = ntohs(init_chk->init.num_inbound_streams);
6456
  /* choose what I want */
6457
758
  if (asoc != NULL) {
6458
0
    if (asoc->streamoutcnt > asoc->pre_open_streams) {
6459
0
      i_want = asoc->streamoutcnt;
6460
0
    } else {
6461
0
      i_want = asoc->pre_open_streams;
6462
0
    }
6463
758
  } else {
6464
758
    i_want = inp->sctp_ep.pre_open_stream_count;
6465
758
  }
6466
758
  if (his_limit < i_want) {
6467
    /* I Want more :< */
6468
111
    initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
6469
647
  } else {
6470
    /* I can have what I want :> */
6471
647
    initack->init.num_outbound_streams = htons(i_want);
6472
647
  }
6473
  /* tell him his limit. */
6474
758
  initack->init.num_inbound_streams =
6475
758
    htons(inp->sctp_ep.max_open_streams_intome);
6476
6477
  /* adaptation layer indication parameter */
6478
758
  if (inp->sctp_ep.adaptation_layer_indicator_provided) {
6479
0
    parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
6480
0
    ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t) + chunk_len);
6481
0
    ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
6482
0
    ali->ph.param_length = htons(parameter_len);
6483
0
    ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
6484
0
    chunk_len += parameter_len;
6485
0
  }
6486
6487
  /* ECN parameter */
6488
758
  if (((asoc != NULL) && (asoc->ecn_supported == 1)) ||
6489
758
      ((asoc == NULL) && (inp->ecn_supported == 1))) {
6490
758
    parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
6491
758
    ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
6492
758
    ph->param_type = htons(SCTP_ECN_CAPABLE);
6493
758
    ph->param_length = htons(parameter_len);
6494
758
    chunk_len += parameter_len;
6495
758
  }
6496
6497
  /* PR-SCTP supported parameter */
6498
758
  if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
6499
758
      ((asoc == NULL) && (inp->prsctp_supported == 1))) {
6500
758
    parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
6501
758
    ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
6502
758
    ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
6503
758
    ph->param_length = htons(parameter_len);
6504
758
    chunk_len += parameter_len;
6505
758
  }
6506
6507
  /* Zero checksum acceptable parameter */
6508
758
  if (((asoc != NULL) && (asoc->rcv_edmid != SCTP_EDMID_NONE)) ||
6509
758
      ((asoc == NULL) && (inp->rcv_edmid != SCTP_EDMID_NONE))) {
6510
0
    parameter_len = (uint16_t)sizeof(struct sctp_zero_checksum_acceptable);
6511
0
    zero_chksum = (struct sctp_zero_checksum_acceptable *)(mtod(m, caddr_t) + chunk_len);
6512
0
    zero_chksum->ph.param_type = htons(SCTP_ZERO_CHECKSUM_ACCEPTABLE);
6513
0
    zero_chksum->ph.param_length = htons(parameter_len);
6514
0
    if (asoc != NULL) {
6515
0
      zero_chksum->edmid = htonl(asoc->rcv_edmid);
6516
0
    } else {
6517
0
      zero_chksum->edmid = htonl(inp->rcv_edmid);
6518
0
    }
6519
0
    chunk_len += parameter_len;
6520
0
  }
6521
6522
  /* Add NAT friendly parameter */
6523
758
  if (nat_friendly) {
6524
53
    parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
6525
53
    ph = (struct sctp_paramhdr *)(mtod(m, caddr_t) + chunk_len);
6526
53
    ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
6527
53
    ph->param_length = htons(parameter_len);
6528
53
    chunk_len += parameter_len;
6529
53
  }
6530
6531
  /* And now tell the peer which extensions we support */
6532
758
  num_ext = 0;
6533
758
  pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t) + chunk_len);
6534
758
  if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
6535
758
      ((asoc == NULL) && (inp->prsctp_supported == 1))) {
6536
758
    pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
6537
758
    if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
6538
758
        ((asoc == NULL) && (inp->idata_supported == 1))) {
6539
0
      pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
6540
0
    }
6541
758
  }
6542
758
  if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
6543
758
      ((asoc == NULL) && (inp->auth_supported == 1))) {
6544
758
    pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
6545
758
  }
6546
758
  if (((asoc != NULL) && (asoc->asconf_supported == 1)) ||
6547
758
      ((asoc == NULL) && (inp->asconf_supported == 1))) {
6548
758
    pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
6549
758
    pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
6550
758
  }
6551
758
  if (((asoc != NULL) && (asoc->reconfig_supported == 1)) ||
6552
758
      ((asoc == NULL) && (inp->reconfig_supported == 1))) {
6553
758
    pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
6554
758
  }
6555
758
  if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
6556
758
      ((asoc == NULL) && (inp->idata_supported == 1))) {
6557
0
    pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
6558
0
  }
6559
758
  if (((asoc != NULL) && (asoc->nrsack_supported == 1)) ||
6560
758
      ((asoc == NULL) && (inp->nrsack_supported == 1))) {
6561
0
    pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
6562
0
  }
6563
758
  if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) ||
6564
758
      ((asoc == NULL) && (inp->pktdrop_supported == 1))) {
6565
0
    pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
6566
0
  }
6567
758
  if (num_ext > 0) {
6568
758
    parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
6569
758
    pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
6570
758
    pr_supported->ph.param_length = htons(parameter_len);
6571
758
    padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6572
758
    chunk_len += parameter_len;
6573
758
  }
6574
6575
  /* add authentication parameters */
6576
758
  if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
6577
758
      ((asoc == NULL) && (inp->auth_supported == 1))) {
6578
758
    struct sctp_auth_random *randp;
6579
758
    struct sctp_auth_hmac_algo *hmacs;
6580
758
    struct sctp_auth_chunk_list *chunks;
6581
6582
758
    if (padding_len > 0) {
6583
758
      memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
6584
758
      chunk_len += padding_len;
6585
758
      padding_len = 0;
6586
758
    }
6587
    /* generate and add RANDOM parameter */
6588
758
    randp = (struct sctp_auth_random *)(mtod(m, caddr_t) + chunk_len);
6589
758
    parameter_len = (uint16_t)sizeof(struct sctp_auth_random) +
6590
758
                    SCTP_AUTH_RANDOM_SIZE_DEFAULT;
6591
758
    randp->ph.param_type = htons(SCTP_RANDOM);
6592
758
    randp->ph.param_length = htons(parameter_len);
6593
758
    SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT);
6594
758
    padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6595
758
    chunk_len += parameter_len;
6596
6597
758
    if (padding_len > 0) {
6598
0
      memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
6599
0
      chunk_len += padding_len;
6600
0
      padding_len = 0;
6601
0
    }
6602
    /* add HMAC_ALGO parameter */
6603
758
    hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t) + chunk_len);
6604
758
    parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) +
6605
758
                    sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
6606
758
                                            (uint8_t *)hmacs->hmac_ids);
6607
758
    hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
6608
758
    hmacs->ph.param_length = htons(parameter_len);
6609
758
    padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6610
758
    chunk_len += parameter_len;
6611
6612
758
    if (padding_len > 0) {
6613
758
      memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
6614
758
      chunk_len += padding_len;
6615
758
      padding_len = 0;
6616
758
    }
6617
    /* add CHUNKS parameter */
6618
758
    chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t) + chunk_len);
6619
758
    parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) +
6620
758
                    sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
6621
758
                                               chunks->chunk_types);
6622
758
    chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6623
758
    chunks->ph.param_length = htons(parameter_len);
6624
758
    padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6625
758
    chunk_len += parameter_len;
6626
758
  }
6627
758
  SCTP_BUF_LEN(m) = chunk_len;
6628
758
  m_last = m;
6629
  /* now the addresses */
6630
  /* To optimize this we could put the scoping stuff
6631
   * into a structure and remove the individual uint8's from
6632
   * the stc structure. Then we could just sifa in the
6633
   * address within the stc.. but for now this is a quick
6634
   * hack to get the address stuff teased apart.
6635
   */
6636
758
  scp.ipv4_addr_legal = stc.ipv4_addr_legal;
6637
758
  scp.ipv6_addr_legal = stc.ipv6_addr_legal;
6638
758
#if defined(__Userspace__)
6639
758
  scp.conn_addr_legal = stc.conn_addr_legal;
6640
758
#endif
6641
758
  scp.loopback_scope = stc.loopback_scope;
6642
758
  scp.ipv4_local_scope = stc.ipv4_scope;
6643
758
  scp.local_scope = stc.local_scope;
6644
758
  scp.site_scope = stc.site_scope;
6645
758
  m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last,
6646
758
                                      cnt_inits_to,
6647
758
                                      &padding_len, &chunk_len);
6648
  /* padding_len can only be positive, if no addresses have been added */
6649
758
  if (padding_len > 0) {
6650
758
    memset(mtod(m, caddr_t) + chunk_len, 0, padding_len);
6651
758
    chunk_len += padding_len;
6652
758
    SCTP_BUF_LEN(m) += padding_len;
6653
758
    padding_len = 0;
6654
758
  }
6655
6656
  /* tack on the operational error if present */
6657
758
  if (op_err) {
6658
418
    parameter_len = 0;
6659
264k
    for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6660
264k
      parameter_len += SCTP_BUF_LEN(m_tmp);
6661
264k
    }
6662
418
    padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6663
418
    SCTP_BUF_NEXT(m_last) = op_err;
6664
264k
    while (SCTP_BUF_NEXT(m_last) != NULL) {
6665
264k
      m_last = SCTP_BUF_NEXT(m_last);
6666
264k
    }
6667
418
    chunk_len += parameter_len;
6668
418
  }
6669
758
  if (padding_len > 0) {
6670
125
    m_last = sctp_add_pad_tombuf(m_last, padding_len);
6671
125
    if (m_last == NULL) {
6672
      /* Houston we have a problem, no space */
6673
0
      sctp_m_freem(m);
6674
0
      return;
6675
0
    }
6676
125
    chunk_len += padding_len;
6677
125
    padding_len = 0;
6678
125
  }
6679
  /* Now we must build a cookie */
6680
758
  m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
6681
758
  if (m_cookie == NULL) {
6682
    /* memory problem */
6683
0
    sctp_m_freem(m);
6684
0
    return;
6685
0
  }
6686
  /* Now append the cookie to the end and update the space/size */
6687
758
  SCTP_BUF_NEXT(m_last) = m_cookie;
6688
758
  parameter_len = 0;
6689
269k
  for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6690
269k
    parameter_len += SCTP_BUF_LEN(m_tmp);
6691
269k
    if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6692
758
      m_last = m_tmp;
6693
758
    }
6694
269k
  }
6695
758
  padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6696
758
  chunk_len += parameter_len;
6697
6698
  /* Place in the size, but we don't include
6699
   * the last pad (if any) in the INIT-ACK.
6700
   */
6701
758
  initack->ch.chunk_length = htons(chunk_len);
6702
6703
  /* Time to sign the cookie, we don't sign over the cookie
6704
   * signature though thus we set trailer.
6705
   */
6706
758
  (void)sctp_hmac_m(SCTP_HMAC,
6707
758
        (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6708
758
        SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6709
758
        (uint8_t *)signature, SCTP_SIGNATURE_SIZE);
6710
758
#if defined(__Userspace__)
6711
  /*
6712
   * Don't put AF_CONN addresses on the wire, in case this is critical
6713
   * for the application. However, they are protected by the HMAC and
6714
   * need to be reconstructed before checking the HMAC.
6715
   * Clearing is only done in the mbuf chain, since the local stc is
6716
   * not used anymore.
6717
   */
6718
758
  if (stc.addr_type == SCTP_CONN_ADDRESS) {
6719
758
    const void *p = NULL;
6720
6721
758
    m_copyback(m_cookie, sizeof(struct sctp_paramhdr) + offsetof(struct sctp_state_cookie, address),
6722
758
               (int)sizeof(void *), (caddr_t)&p);
6723
758
  }
6724
758
  if (stc.laddr_type == SCTP_CONN_ADDRESS) {
6725
758
    const void *p = NULL;
6726
6727
758
    m_copyback(m_cookie, sizeof(struct sctp_paramhdr) + offsetof(struct sctp_state_cookie, laddress),
6728
758
               (int)sizeof(void *), (caddr_t)&p);
6729
758
  }
6730
758
#endif
6731
  /*
6732
   * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6733
   * here since the timer will drive a retranmission.
6734
   */
6735
758
  if (padding_len > 0) {
6736
380
    if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
6737
0
      sctp_m_freem(m);
6738
0
      return;
6739
0
    }
6740
380
  }
6741
758
  if (stc.loopback_scope) {
6742
0
    over_addr = (union sctp_sockstore *)dst;
6743
758
  } else {
6744
758
    over_addr = NULL;
6745
758
  }
6746
6747
758
  if (asoc != NULL) {
6748
0
    use_zero_crc = (asoc->rcv_edmid != SCTP_EDMID_NONE) && (asoc->rcv_edmid == edmid);
6749
758
  } else {
6750
758
    use_zero_crc = (inp->rcv_edmid != SCTP_EDMID_NONE) && (inp->rcv_edmid == edmid);
6751
758
  }
6752
6753
758
  if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6754
758
                                          0, 0,
6755
758
                                          inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6756
758
                                          port, over_addr,
6757
#if defined(__FreeBSD__) && !defined(__Userspace__)
6758
                                          mflowtype, mflowid,
6759
#endif
6760
758
                                          use_zero_crc,
6761
758
                                          SCTP_SO_NOT_LOCKED))) {
6762
0
    SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
6763
0
    if (error == ENOBUFS) {
6764
0
      if (asoc != NULL) {
6765
0
        asoc->ifp_had_enobuf = 1;
6766
0
      }
6767
0
      SCTP_STAT_INCR(sctps_lowlevelerr);
6768
0
    }
6769
758
  } else {
6770
758
    if (asoc != NULL) {
6771
0
      asoc->ifp_had_enobuf = 0;
6772
0
    }
6773
758
  }
6774
758
  SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6775
758
}
6776
6777
static void
6778
sctp_prune_prsctp(struct sctp_tcb *stcb,
6779
    struct sctp_association *asoc,
6780
    struct sctp_nonpad_sndrcvinfo *srcv,
6781
    int dataout)
6782
0
{
6783
0
  int freed_spc = 0;
6784
0
  struct sctp_tmit_chunk *chk, *nchk;
6785
6786
0
  SCTP_TCB_LOCK_ASSERT(stcb);
6787
0
  if ((asoc->prsctp_supported) &&
6788
0
      (asoc->sent_queue_cnt_removeable > 0)) {
6789
0
    TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6790
      /*
6791
       * Look for chunks marked with the PR_SCTP flag AND
6792
       * the buffer space flag. If the one being sent is
6793
       * equal or greater priority then purge the old one
6794
       * and free some space.
6795
       */
6796
0
      if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6797
        /*
6798
         * This one is PR-SCTP AND buffer space
6799
         * limited type
6800
         */
6801
0
        if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6802
          /*
6803
           * Lower numbers equates to higher
6804
           * priority. So if the one we are
6805
           * looking at has a larger priority,
6806
           * we want to drop the data and NOT
6807
           * retransmit it.
6808
           */
6809
0
          if (chk->data) {
6810
            /*
6811
             * We release the book_size
6812
             * if the mbuf is here
6813
             */
6814
0
            int ret_spc;
6815
0
            uint8_t sent;
6816
6817
0
            if (chk->sent > SCTP_DATAGRAM_UNSENT)
6818
0
              sent = 1;
6819
0
            else
6820
0
              sent = 0;
6821
0
            ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6822
0
                sent,
6823
0
                SCTP_SO_LOCKED);
6824
0
            freed_spc += ret_spc;
6825
0
            if (freed_spc >= dataout) {
6826
0
              return;
6827
0
            }
6828
0
          } /* if chunk was present */
6829
0
        } /* if of sufficient priority */
6830
0
      } /* if chunk has enabled */
6831
0
    }   /* tailqforeach */
6832
6833
0
    TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6834
      /* Here we must move to the sent queue and mark */
6835
0
      if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6836
0
        if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6837
0
          if (chk->data) {
6838
            /*
6839
             * We release the book_size
6840
             * if the mbuf is here
6841
             */
6842
0
            int ret_spc;
6843
6844
0
            ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6845
0
                0, SCTP_SO_LOCKED);
6846
6847
0
            freed_spc += ret_spc;
6848
0
            if (freed_spc >= dataout) {
6849
0
              return;
6850
0
            }
6851
0
          } /* end if chk->data */
6852
0
        } /* end if right class */
6853
0
      } /* end if chk pr-sctp */
6854
0
    }   /* tailqforeachsafe (chk) */
6855
0
  }     /* if enabled in asoc */
6856
0
}
6857
6858
uint32_t
6859
sctp_get_frag_point(struct sctp_tcb *stcb)
6860
48.0k
{
6861
48.0k
  struct sctp_association *asoc;
6862
48.0k
  uint32_t frag_point, overhead;
6863
6864
48.0k
  asoc = &stcb->asoc;
6865
  /* Consider IP header and SCTP common header. */
6866
48.0k
  if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6867
0
    overhead = SCTP_MIN_OVERHEAD;
6868
48.0k
  } else {
6869
48.0k
#if defined(__Userspace__)
6870
48.0k
    if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_CONN) {
6871
48.0k
      overhead = sizeof(struct sctphdr);
6872
48.0k
    } else {
6873
0
      overhead = SCTP_MIN_V4_OVERHEAD;
6874
0
    }
6875
#else
6876
    overhead = SCTP_MIN_V4_OVERHEAD;
6877
#endif
6878
48.0k
  }
6879
  /* Consider DATA/IDATA chunk header and AUTH header, if needed. */
6880
48.0k
  if (asoc->idata_supported) {
6881
15.3k
    overhead += sizeof(struct sctp_idata_chunk);
6882
15.3k
    if (sctp_auth_is_required_chunk(SCTP_IDATA, asoc->peer_auth_chunks)) {
6883
14
      overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6884
14
    }
6885
32.6k
  } else {
6886
32.6k
    overhead += sizeof(struct sctp_data_chunk);
6887
32.6k
    if (sctp_auth_is_required_chunk(SCTP_DATA, asoc->peer_auth_chunks)) {
6888
42
      overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6889
42
    }
6890
32.6k
  }
6891
48.0k
  KASSERT(overhead % 4 == 0,
6892
48.0k
          ("overhead (%u) not a multiple of 4", overhead));
6893
  /* Consider padding. */
6894
48.0k
  if (asoc->smallest_mtu % 4 > 0) {
6895
0
    overhead += (asoc->smallest_mtu % 4);
6896
0
  }
6897
48.0k
  KASSERT(asoc->smallest_mtu > overhead,
6898
48.0k
          ("Association MTU (%u) too small for overhead (%u)",
6899
48.0k
           asoc->smallest_mtu, overhead));
6900
48.0k
  frag_point = asoc->smallest_mtu - overhead;
6901
48.0k
  KASSERT(frag_point % 4 == 0,
6902
48.0k
          ("frag_point (%u) not a multiple of 4", frag_point));
6903
  /* Honor MAXSEG socket option. */
6904
48.0k
  if ((asoc->sctp_frag_point > 0) &&
6905
0
      (asoc->sctp_frag_point < frag_point)) {
6906
0
    frag_point = asoc->sctp_frag_point;
6907
0
  }
6908
48.0k
  return (frag_point);
6909
48.0k
}
6910
6911
static void
6912
sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6913
24.7k
{
6914
  /*
6915
   * We assume that the user wants PR_SCTP_TTL if the user
6916
   * provides a positive lifetime but does not specify any
6917
   * PR_SCTP policy.
6918
   */
6919
24.7k
  if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6920
0
    sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6921
24.7k
  } else if (sp->timetolive > 0) {
6922
0
    sp->sinfo_flags |= SCTP_PR_SCTP_TTL;
6923
0
    sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6924
24.7k
  } else {
6925
24.7k
    return;
6926
24.7k
  }
6927
0
  switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6928
0
  case CHUNK_FLAGS_PR_SCTP_BUF:
6929
    /*
6930
     * Time to live is a priority stored in tv_sec when
6931
     * doing the buffer drop thing.
6932
     */
6933
0
    sp->ts.tv_sec = sp->timetolive;
6934
0
    sp->ts.tv_usec = 0;
6935
0
    break;
6936
0
  case CHUNK_FLAGS_PR_SCTP_TTL:
6937
0
  {
6938
0
    struct timeval tv;
6939
0
    (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6940
0
    tv.tv_sec = sp->timetolive / 1000;
6941
0
    tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6942
    /* TODO sctp_constants.h needs alternative time macros when
6943
     *  _KERNEL is undefined.
6944
     */
6945
0
#if !(defined(__FreeBSD__) && !defined(__Userspace__))
6946
0
    timeradd(&sp->ts, &tv, &sp->ts);
6947
#else
6948
    timevaladd(&sp->ts, &tv);
6949
#endif
6950
0
  }
6951
0
    break;
6952
0
  case CHUNK_FLAGS_PR_SCTP_RTX:
6953
    /*
6954
     * Time to live is a the number or retransmissions
6955
     * stored in tv_sec.
6956
     */
6957
0
    sp->ts.tv_sec = sp->timetolive;
6958
0
    sp->ts.tv_usec = 0;
6959
0
    break;
6960
0
  default:
6961
0
    SCTPDBG(SCTP_DEBUG_USRREQ1,
6962
0
      "Unknown PR_SCTP policy %u.\n",
6963
0
      PR_SCTP_POLICY(sp->sinfo_flags));
6964
0
    break;
6965
0
  }
6966
0
}
6967
6968
static int
6969
sctp_msg_append(struct sctp_tcb *stcb,
6970
    struct sctp_nets *net,
6971
    struct mbuf *m,
6972
    struct sctp_nonpad_sndrcvinfo *srcv)
6973
0
{
6974
0
  int error = 0;
6975
0
  struct mbuf *at;
6976
0
  struct sctp_stream_queue_pending *sp = NULL;
6977
0
  struct sctp_stream_out *strm;
6978
6979
0
  SCTP_TCB_LOCK_ASSERT(stcb);
6980
6981
  /* Given an mbuf chain, put it
6982
   * into the association send queue and
6983
   * place it on the wheel
6984
   */
6985
0
  if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6986
    /* Invalid stream number */
6987
0
    SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6988
0
    error = EINVAL;
6989
0
    goto out_now;
6990
0
  }
6991
0
  if ((stcb->asoc.stream_locked) &&
6992
0
      (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6993
0
    SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6994
0
    error = EINVAL;
6995
0
    goto out_now;
6996
0
  }
6997
0
  if ((stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) &&
6998
0
      (stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) {
6999
    /*
7000
     * Can't queue any data while stream reset is underway.
7001
     */
7002
0
    if (stcb->asoc.strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) {
7003
0
      error = EAGAIN;
7004
0
    } else {
7005
0
      error = EINVAL;
7006
0
    }
7007
0
    goto out_now;
7008
0
  }
7009
  /* Now can we send this? */
7010
0
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
7011
0
      (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
7012
0
      (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7013
0
      (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
7014
    /* got data while shutting down */
7015
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EPIPE);
7016
0
    error = EPIPE;
7017
0
    goto out_now;
7018
0
  }
7019
0
  sctp_alloc_a_strmoq(stcb, sp);
7020
0
  if (sp == NULL) {
7021
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7022
0
    error = ENOMEM;
7023
0
    goto out_now;
7024
0
  }
7025
0
  sp->sinfo_flags = srcv->sinfo_flags;
7026
0
  sp->timetolive = srcv->sinfo_timetolive;
7027
0
  sp->ppid = srcv->sinfo_ppid;
7028
0
  sp->context = srcv->sinfo_context;
7029
0
  sp->fsn = 0;
7030
0
  if (sp->sinfo_flags & SCTP_ADDR_OVER) {
7031
0
    sp->net = net;
7032
0
    atomic_add_int(&sp->net->ref_count, 1);
7033
0
  } else {
7034
0
    sp->net = NULL;
7035
0
  }
7036
0
  (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
7037
0
  sp->sid = srcv->sinfo_stream;
7038
0
  sp->msg_is_complete = 1;
7039
0
  sp->sender_all_done = 1;
7040
0
  sp->some_taken = 0;
7041
0
  sp->data = m;
7042
0
  sp->tail_mbuf = NULL;
7043
0
  sctp_set_prsctp_policy(sp);
7044
  /* We could in theory (for sendall) sifa the length
7045
   * in, but we would still have to hunt through the
7046
   * chain since we need to setup the tail_mbuf
7047
   */
7048
0
  sp->length = 0;
7049
0
  for (at = m; at; at = SCTP_BUF_NEXT(at)) {
7050
0
    if (SCTP_BUF_NEXT(at) == NULL)
7051
0
      sp->tail_mbuf = at;
7052
0
    sp->length += SCTP_BUF_LEN(at);
7053
0
  }
7054
0
  if (srcv->sinfo_keynumber_valid) {
7055
0
    sp->auth_keyid = srcv->sinfo_keynumber;
7056
0
  } else {
7057
0
    sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
7058
0
  }
7059
0
  if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
7060
0
    sctp_auth_key_acquire(stcb, sp->auth_keyid);
7061
0
    sp->holds_key_ref = 1;
7062
0
  }
7063
0
  strm = &stcb->asoc.strmout[srcv->sinfo_stream];
7064
0
  sctp_snd_sb_alloc(stcb, sp->length);
7065
0
  atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
7066
0
  TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
7067
0
  stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp);
7068
0
  m = NULL;
7069
0
out_now:
7070
0
  if (m) {
7071
0
    sctp_m_freem(m);
7072
0
  }
7073
0
  return (error);
7074
0
}
7075
7076
static struct mbuf *
7077
sctp_copy_mbufchain(struct mbuf *clonechain,
7078
        struct mbuf *outchain,
7079
        struct mbuf **endofchain,
7080
        int can_take_mbuf,
7081
        int sizeofcpy,
7082
        uint8_t copy_by_ref)
7083
152k
{
7084
152k
  struct mbuf *m;
7085
152k
  struct mbuf *appendchain;
7086
152k
  caddr_t cp;
7087
152k
  int len;
7088
7089
152k
  if (endofchain == NULL) {
7090
    /* error */
7091
0
  error_out:
7092
0
    if (outchain)
7093
0
      sctp_m_freem(outchain);
7094
0
    return (NULL);
7095
0
  }
7096
152k
  if (can_take_mbuf) {
7097
45.4k
    appendchain = clonechain;
7098
107k
  } else {
7099
107k
    if (!copy_by_ref &&
7100
102k
        (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))) {
7101
      /* Its not in a cluster */
7102
102k
      if (*endofchain == NULL) {
7103
        /* lets get a mbuf cluster */
7104
13.5k
        if (outchain == NULL) {
7105
          /* This is the general case */
7106
13.5k
        new_mbuf:
7107
13.5k
          outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
7108
13.5k
          if (outchain == NULL) {
7109
0
            goto error_out;
7110
0
          }
7111
13.5k
          SCTP_BUF_LEN(outchain) = 0;
7112
13.5k
          *endofchain = outchain;
7113
          /* get the prepend space */
7114
13.5k
          SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV+4));
7115
13.5k
        } else {
7116
          /* We really should not get a NULL in endofchain */
7117
          /* find end */
7118
0
          m = outchain;
7119
0
          while (m) {
7120
0
            if (SCTP_BUF_NEXT(m) == NULL) {
7121
0
              *endofchain = m;
7122
0
              break;
7123
0
            }
7124
0
            m = SCTP_BUF_NEXT(m);
7125
0
          }
7126
          /* sanity */
7127
0
          if (*endofchain == NULL) {
7128
            /* huh, TSNH XXX maybe we should panic */
7129
0
            sctp_m_freem(outchain);
7130
0
            goto new_mbuf;
7131
0
          }
7132
0
        }
7133
        /* get the new end of length */
7134
13.5k
        len = (int)M_TRAILINGSPACE(*endofchain);
7135
89.3k
      } else {
7136
        /* how much is left at the end? */
7137
89.3k
        len = (int)M_TRAILINGSPACE(*endofchain);
7138
89.3k
      }
7139
      /* Find the end of the data, for appending */
7140
102k
      cp = (mtod((*endofchain), caddr_t) + SCTP_BUF_LEN((*endofchain)));
7141
7142
      /* Now lets copy it out */
7143
102k
      if (len >= sizeofcpy) {
7144
        /* It all fits, copy it in */
7145
100k
        m_copydata(clonechain, 0, sizeofcpy, cp);
7146
100k
        SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7147
100k
      } else {
7148
        /* fill up the end of the chain */
7149
2.73k
        if (len > 0) {
7150
597
          m_copydata(clonechain, 0, len, cp);
7151
597
          SCTP_BUF_LEN((*endofchain)) += len;
7152
          /* now we need another one */
7153
597
          sizeofcpy -= len;
7154
597
        }
7155
2.73k
        m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
7156
2.73k
        if (m == NULL) {
7157
          /* We failed */
7158
0
          goto error_out;
7159
0
        }
7160
2.73k
        SCTP_BUF_NEXT((*endofchain)) = m;
7161
2.73k
        *endofchain = m;
7162
2.73k
        cp = mtod((*endofchain), caddr_t);
7163
2.73k
        m_copydata(clonechain, len, sizeofcpy, cp);
7164
2.73k
        SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
7165
2.73k
      }
7166
102k
      return (outchain);
7167
102k
    } else {
7168
      /* copy the old fashion way */
7169
4.59k
      appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT);
7170
#ifdef SCTP_MBUF_LOGGING
7171
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7172
        sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY);
7173
      }
7174
#endif
7175
4.59k
    }
7176
107k
  }
7177
50.0k
  if (appendchain == NULL) {
7178
    /* error */
7179
0
    if (outchain)
7180
0
      sctp_m_freem(outchain);
7181
0
    return (NULL);
7182
0
  }
7183
50.0k
  if (outchain) {
7184
    /* tack on to the end */
7185
38.5k
    if (*endofchain != NULL) {
7186
38.5k
      SCTP_BUF_NEXT(((*endofchain))) = appendchain;
7187
38.5k
    } else {
7188
0
      m = outchain;
7189
0
      while (m) {
7190
0
        if (SCTP_BUF_NEXT(m) == NULL) {
7191
0
          SCTP_BUF_NEXT(m) = appendchain;
7192
0
          break;
7193
0
        }
7194
0
        m = SCTP_BUF_NEXT(m);
7195
0
      }
7196
0
    }
7197
    /*
7198
     * save off the end and update the end-chain
7199
     * position
7200
     */
7201
38.5k
    m = appendchain;
7202
52.2k
    while (m) {
7203
52.2k
      if (SCTP_BUF_NEXT(m) == NULL) {
7204
38.5k
        *endofchain = m;
7205
38.5k
        break;
7206
38.5k
      }
7207
13.7k
      m = SCTP_BUF_NEXT(m);
7208
13.7k
    }
7209
38.5k
    return (outchain);
7210
38.5k
  } else {
7211
    /* save off the end and update the end-chain position */
7212
11.4k
    m = appendchain;
7213
18.1k
    while (m) {
7214
18.1k
      if (SCTP_BUF_NEXT(m) == NULL) {
7215
11.4k
        *endofchain = m;
7216
11.4k
        break;
7217
11.4k
      }
7218
6.66k
      m = SCTP_BUF_NEXT(m);
7219
6.66k
    }
7220
11.4k
    return (appendchain);
7221
11.4k
  }
7222
50.0k
}
7223
7224
static int
7225
sctp_med_chunk_output(struct sctp_inpcb *inp,
7226
          struct sctp_tcb *stcb,
7227
          struct sctp_association *asoc,
7228
          int *num_out,
7229
          int *reason_code,
7230
          int control_only, int from_where,
7231
          struct timeval *now, int *now_filled,
7232
          uint32_t frag_point, int so_locked);
7233
7234
static void
7235
sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
7236
    uint32_t val SCTP_UNUSED)
7237
0
{
7238
0
  struct sctp_copy_all *ca;
7239
0
  struct mbuf *m;
7240
0
  int ret = 0;
7241
0
  int added_control = 0;
7242
0
  int un_sent, do_chunk_output = 1;
7243
0
  struct sctp_association *asoc;
7244
0
  struct sctp_nets *net;
7245
7246
0
  ca = (struct sctp_copy_all *)ptr;
7247
0
  if (ca->m == NULL) {
7248
0
    return;
7249
0
  }
7250
0
  if (ca->inp != inp) {
7251
    /* TSNH */
7252
0
    return;
7253
0
  }
7254
0
  if (ca->sndlen > 0) {
7255
0
    m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT);
7256
0
    if (m == NULL) {
7257
      /* can't copy so we are done */
7258
0
      ca->cnt_failed++;
7259
0
      return;
7260
0
    }
7261
#ifdef SCTP_MBUF_LOGGING
7262
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7263
      sctp_log_mbc(m, SCTP_MBUF_ICOPY);
7264
    }
7265
#endif
7266
0
  } else {
7267
0
    m = NULL;
7268
0
  }
7269
0
  SCTP_TCB_LOCK_ASSERT(stcb);
7270
0
  if (stcb->asoc.alternate) {
7271
0
    net = stcb->asoc.alternate;
7272
0
  } else {
7273
0
    net = stcb->asoc.primary_destination;
7274
0
  }
7275
0
  if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
7276
    /* Abort this assoc with m as the user defined reason */
7277
0
    if (m != NULL) {
7278
0
      SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT);
7279
0
    } else {
7280
0
      m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
7281
0
                                0, M_NOWAIT, 1, MT_DATA);
7282
0
      if (m != NULL) {
7283
0
        SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
7284
0
      }
7285
0
    }
7286
0
    if (m != NULL) {
7287
0
      struct sctp_paramhdr *ph;
7288
7289
0
      ph = mtod(m, struct sctp_paramhdr *);
7290
0
      ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
7291
0
      ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen));
7292
0
    }
7293
    /* We add one here to keep the assoc from
7294
     * dis-appearing on us.
7295
     */
7296
0
    atomic_add_int(&stcb->asoc.refcnt, 1);
7297
0
    sctp_abort_an_association(inp, stcb, m, false, SCTP_SO_NOT_LOCKED);
7298
    /* sctp_abort_an_association calls sctp_free_asoc()
7299
     * free association will NOT free it since we
7300
     * incremented the refcnt .. we do this to prevent
7301
     * it being freed and things getting tricky since
7302
     * we could end up (from free_asoc) calling inpcb_free
7303
     * which would get a recursive lock call to the
7304
     * iterator lock.. But as a consequence of that the
7305
     * stcb will return to us un-locked.. since free_asoc
7306
     * returns with either no TCB or the TCB unlocked, we
7307
     * must relock.. to unlock in the iterator timer :-0
7308
     */
7309
0
    SCTP_TCB_LOCK(stcb);
7310
0
    atomic_subtract_int(&stcb->asoc.refcnt, 1);
7311
0
    goto no_chunk_output;
7312
0
  } else {
7313
0
    if (m != NULL) {
7314
0
      ret = sctp_msg_append(stcb, net, m, &ca->sndrcv);
7315
0
    }
7316
0
    asoc = &stcb->asoc;
7317
0
    if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
7318
      /* shutdown this assoc */
7319
0
      if (TAILQ_EMPTY(&asoc->send_queue) &&
7320
0
          TAILQ_EMPTY(&asoc->sent_queue) &&
7321
0
          sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) {
7322
0
        if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
7323
0
          goto abort_anyway;
7324
0
        }
7325
        /* there is nothing queued to send, so I'm done... */
7326
0
        if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
7327
0
            (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
7328
0
            (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
7329
          /* only send SHUTDOWN the first time through */
7330
0
          if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
7331
0
            SCTP_STAT_DECR_GAUGE32(sctps_currestab);
7332
0
          }
7333
0
          SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
7334
0
          sctp_stop_timers_for_shutdown(stcb);
7335
0
          sctp_send_shutdown(stcb, net);
7336
0
          sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
7337
0
                           net);
7338
0
          sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
7339
0
                           NULL);
7340
0
          added_control = 1;
7341
0
          do_chunk_output = 0;
7342
0
        }
7343
0
      } else {
7344
        /*
7345
         * we still got (or just got) data to send, so set
7346
         * SHUTDOWN_PENDING
7347
         */
7348
        /*
7349
         * XXX sockets draft says that SCTP_EOF should be
7350
         * sent with no data.  currently, we will allow user
7351
         * data to be sent first and move to
7352
         * SHUTDOWN-PENDING
7353
         */
7354
0
        if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
7355
0
            (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
7356
0
            (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
7357
0
          if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
7358
0
            SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
7359
0
          }
7360
0
          SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
7361
0
          if (TAILQ_EMPTY(&asoc->send_queue) &&
7362
0
              TAILQ_EMPTY(&asoc->sent_queue) &&
7363
0
              (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
7364
0
            struct mbuf *op_err;
7365
0
            char msg[SCTP_DIAG_INFO_LEN];
7366
7367
0
          abort_anyway:
7368
0
            SCTP_SNPRINTF(msg, sizeof(msg),
7369
0
                          "%s:%d at %s", __FILE__, __LINE__, __func__);
7370
0
            op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
7371
0
                                         msg);
7372
0
            atomic_add_int(&stcb->asoc.refcnt, 1);
7373
0
            sctp_abort_an_association(stcb->sctp_ep, stcb,
7374
0
                                      op_err, false, SCTP_SO_NOT_LOCKED);
7375
0
            atomic_subtract_int(&stcb->asoc.refcnt, 1);
7376
0
            goto no_chunk_output;
7377
0
          }
7378
0
        }
7379
0
      }
7380
0
    }
7381
0
  }
7382
0
  un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
7383
0
       (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb)));
7384
7385
0
  if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
7386
0
      (stcb->asoc.total_flight > 0) &&
7387
0
      (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
7388
0
    do_chunk_output = 0;
7389
0
  }
7390
0
  if (do_chunk_output)
7391
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
7392
0
  else if (added_control) {
7393
0
    struct timeval now;
7394
0
    int num_out, reason, now_filled = 0;
7395
7396
0
    (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
7397
0
                                &reason, 1, 1, &now, &now_filled,
7398
0
                                sctp_get_frag_point(stcb),
7399
0
                                SCTP_SO_NOT_LOCKED);
7400
0
  }
7401
0
 no_chunk_output:
7402
0
  if (ret) {
7403
0
    ca->cnt_failed++;
7404
0
  } else {
7405
0
    ca->cnt_sent++;
7406
0
  }
7407
0
}
7408
7409
static void
7410
sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
7411
0
{
7412
0
  struct sctp_copy_all *ca;
7413
7414
0
  ca = (struct sctp_copy_all *)ptr;
7415
  /*
7416
   * Do a notify here? Kacheong suggests that the notify be done at
7417
   * the send time.. so you would push up a notification if any send
7418
   * failed. Don't know if this is feasible since the only failures we
7419
   * have is "memory" related and if you cannot get an mbuf to send
7420
   * the data you surely can't get an mbuf to send up to notify the
7421
   * user you can't send the data :->
7422
   */
7423
7424
  /* now free everything */
7425
0
  if (ca->inp) {
7426
    /* Lets clear the flag to allow others to run. */
7427
0
    SCTP_INP_WLOCK(ca->inp);
7428
0
    ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
7429
0
    SCTP_INP_WUNLOCK(ca->inp);
7430
0
  }
7431
0
  sctp_m_freem(ca->m);
7432
0
  SCTP_FREE(ca, SCTP_M_COPYAL);
7433
0
}
7434
7435
static struct mbuf *
7436
sctp_copy_out_all(struct uio *uio, ssize_t len)
7437
0
{
7438
0
  struct mbuf *ret, *at;
7439
0
  ssize_t left, willcpy, cancpy, error;
7440
7441
0
  ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA);
7442
0
  if (ret == NULL) {
7443
    /* TSNH */
7444
0
    return (NULL);
7445
0
  }
7446
0
  left = len;
7447
0
  SCTP_BUF_LEN(ret) = 0;
7448
  /* save space for the data chunk header */
7449
0
  cancpy = (int)M_TRAILINGSPACE(ret);
7450
0
  willcpy = min(cancpy, left);
7451
0
  at = ret;
7452
0
  while (left > 0) {
7453
    /* Align data to the end */
7454
0
    error = uiomove(mtod(at, caddr_t), (int)willcpy, uio);
7455
0
    if (error) {
7456
0
  err_out_now:
7457
0
      sctp_m_freem(at);
7458
0
      return (NULL);
7459
0
    }
7460
0
    SCTP_BUF_LEN(at) = (int)willcpy;
7461
0
    SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
7462
0
    left -= willcpy;
7463
0
    if (left > 0) {
7464
0
      SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA);
7465
0
      if (SCTP_BUF_NEXT(at) == NULL) {
7466
0
        goto err_out_now;
7467
0
      }
7468
0
      at = SCTP_BUF_NEXT(at);
7469
0
      SCTP_BUF_LEN(at) = 0;
7470
0
      cancpy = (int)M_TRAILINGSPACE(at);
7471
0
      willcpy = min(cancpy, left);
7472
0
    }
7473
0
  }
7474
0
  return (ret);
7475
0
}
7476
7477
static int
7478
sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
7479
    struct sctp_nonpad_sndrcvinfo *srcv)
7480
0
{
7481
0
  struct sctp_copy_all *ca;
7482
0
  struct mbuf *mat;
7483
0
  ssize_t sndlen;
7484
0
  int ret;
7485
7486
0
  if (uio != NULL) {
7487
#if defined(__APPLE__) && !defined(__Userspace__)
7488
#if defined(APPLE_LEOPARD)
7489
    sndlen = uio->uio_resid;
7490
#else
7491
    sndlen = uio_resid(uio);
7492
#endif
7493
#else
7494
0
    sndlen = uio->uio_resid;
7495
0
#endif
7496
0
  } else {
7497
0
    sndlen = 0;
7498
0
    for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
7499
0
      sndlen += SCTP_BUF_LEN(mat);
7500
0
    }
7501
0
  }
7502
0
  if (sndlen > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
7503
    /* You must not be larger than the limit! */
7504
0
    return (EMSGSIZE);
7505
0
  }
7506
0
  SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
7507
0
        SCTP_M_COPYAL);
7508
0
  if (ca == NULL) {
7509
0
    sctp_m_freem(m);
7510
0
    SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7511
0
    return (ENOMEM);
7512
0
  }
7513
0
  memset(ca, 0, sizeof(struct sctp_copy_all));
7514
0
  ca->inp = inp;
7515
0
  if (srcv != NULL) {
7516
0
    memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
7517
0
  }
7518
  /* Serialize. */
7519
0
  SCTP_INP_WLOCK(inp);
7520
0
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) != 0) {
7521
0
    SCTP_INP_WUNLOCK(inp);
7522
0
    sctp_m_freem(m);
7523
0
    SCTP_FREE(ca, SCTP_M_COPYAL);
7524
0
    return (EBUSY);
7525
0
  }
7526
0
  inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP;
7527
0
  SCTP_INP_WUNLOCK(inp);
7528
  /*
7529
   * take off the sendall flag, it would be bad if we failed to do
7530
   * this :-0
7531
   */
7532
0
  ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
7533
  /* get length and mbuf chain */
7534
0
  ca->sndlen = sndlen;
7535
0
  if (uio != NULL) {
7536
#if defined(__APPLE__) && !defined(__Userspace__)
7537
    SCTP_SOCKET_UNLOCK(SCTP_INP_SO(inp), 0);
7538
#endif
7539
0
    ca->m = sctp_copy_out_all(uio, ca->sndlen);
7540
#if defined(__APPLE__) && !defined(__Userspace__)
7541
    SCTP_SOCKET_LOCK(SCTP_INP_SO(inp), 0);
7542
#endif
7543
0
    if (ca->m == NULL) {
7544
0
      SCTP_FREE(ca, SCTP_M_COPYAL);
7545
0
      sctp_m_freem(m);
7546
0
      SCTP_INP_WLOCK(inp);
7547
0
      inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
7548
0
      SCTP_INP_WUNLOCK(inp);
7549
0
      SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
7550
0
      return (ENOMEM);
7551
0
    }
7552
0
  } else {
7553
0
    ca->m = m;
7554
0
  }
7555
0
  ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
7556
0
                               SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
7557
0
                               SCTP_ASOC_ANY_STATE,
7558
0
                               (void *)ca, 0,
7559
0
                               sctp_sendall_completes, inp, 1);
7560
0
  if (ret != 0) {
7561
0
    SCTP_INP_WLOCK(inp);
7562
0
    inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
7563
0
    SCTP_INP_WUNLOCK(inp);
7564
0
    SCTP_FREE(ca, SCTP_M_COPYAL);
7565
0
    SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
7566
0
    return (EFAULT);
7567
0
  }
7568
0
  return (0);
7569
0
}
7570
7571
void
7572
sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
7573
29.1k
{
7574
29.1k
  struct sctp_tmit_chunk *chk, *nchk;
7575
7576
2.63M
  TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7577
2.63M
    if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
7578
5.71k
      TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7579
5.71k
      asoc->ctrl_queue_cnt--;
7580
5.71k
      if (chk->data) {
7581
5.71k
        sctp_m_freem(chk->data);
7582
5.71k
        chk->data = NULL;
7583
5.71k
      }
7584
5.71k
      sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7585
5.71k
    }
7586
2.63M
  }
7587
29.1k
}
7588
7589
void
7590
sctp_toss_old_asconf(struct sctp_tcb *stcb)
7591
399
{
7592
399
  struct sctp_association *asoc;
7593
399
  struct sctp_tmit_chunk *chk, *nchk;
7594
399
  struct sctp_asconf_chunk *acp;
7595
7596
399
  asoc = &stcb->asoc;
7597
399
  TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
7598
    /* find SCTP_ASCONF chunk in queue */
7599
0
    if (chk->rec.chunk_id.id == SCTP_ASCONF) {
7600
0
      if (chk->data) {
7601
0
        acp = mtod(chk->data, struct sctp_asconf_chunk *);
7602
0
        if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
7603
          /* Not Acked yet */
7604
0
          break;
7605
0
        }
7606
0
      }
7607
0
      TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
7608
0
      asoc->ctrl_queue_cnt--;
7609
0
      if (chk->data) {
7610
0
        sctp_m_freem(chk->data);
7611
0
        chk->data = NULL;
7612
0
      }
7613
0
      sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
7614
0
    }
7615
0
  }
7616
399
}
7617
7618
static void
7619
sctp_clean_up_datalist(struct sctp_tcb *stcb,
7620
    struct sctp_association *asoc,
7621
    struct sctp_tmit_chunk **data_list,
7622
    int bundle_at,
7623
    struct sctp_nets *net)
7624
7.09k
{
7625
7.09k
  int i;
7626
7.09k
  struct sctp_tmit_chunk *tp1;
7627
7628
14.1k
  for (i = 0; i < bundle_at; i++) {
7629
    /* off of the send queue */
7630
7.09k
    TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
7631
7.09k
    asoc->send_queue_cnt--;
7632
7.09k
    if (i > 0) {
7633
      /*
7634
       * Any chunk NOT 0 you zap the time chunk 0 gets
7635
       * zapped or set based on if a RTO measurement is
7636
       * needed.
7637
       */
7638
0
      data_list[i]->do_rtt = 0;
7639
0
    }
7640
    /* record time */
7641
7.09k
    data_list[i]->sent_rcv_time = net->last_sent_time;
7642
7.09k
    data_list[i]->rec.data.cwnd_at_send = net->cwnd;
7643
7.09k
    data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn;
7644
7.09k
    if (data_list[i]->whoTo == NULL) {
7645
7.09k
      data_list[i]->whoTo = net;
7646
7.09k
      atomic_add_int(&net->ref_count, 1);
7647
7.09k
    }
7648
    /* on to the sent queue */
7649
7.09k
    tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
7650
7.09k
    if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7651
0
      struct sctp_tmit_chunk *tpp;
7652
7653
      /* need to move back */
7654
0
    back_up_more:
7655
0
      tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
7656
0
      if (tpp == NULL) {
7657
0
        TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
7658
0
        goto all_done;
7659
0
      }
7660
0
      tp1 = tpp;
7661
0
      if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7662
0
        goto back_up_more;
7663
0
      }
7664
0
      TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
7665
7.09k
    } else {
7666
7.09k
      TAILQ_INSERT_TAIL(&asoc->sent_queue,
7667
7.09k
            data_list[i],
7668
7.09k
            sctp_next);
7669
7.09k
    }
7670
7.09k
  all_done:
7671
    /* This does not lower until the cum-ack passes it */
7672
7.09k
    asoc->sent_queue_cnt++;
7673
7.09k
    if ((asoc->peers_rwnd <= 0) &&
7674
0
        (asoc->total_flight == 0) &&
7675
0
        (bundle_at == 1)) {
7676
      /* Mark the chunk as being a window probe */
7677
0
      SCTP_STAT_INCR(sctps_windowprobed);
7678
0
    }
7679
#ifdef SCTP_AUDITING_ENABLED
7680
    sctp_audit_log(0xC2, 3);
7681
#endif
7682
7.09k
    data_list[i]->sent = SCTP_DATAGRAM_SENT;
7683
7.09k
    data_list[i]->snd_count = 1;
7684
7.09k
    data_list[i]->rec.data.chunk_was_revoked = 0;
7685
7.09k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7686
0
      sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
7687
0
               data_list[i]->whoTo->flight_size,
7688
0
               data_list[i]->book_size,
7689
0
               (uint32_t)(uintptr_t)data_list[i]->whoTo,
7690
0
               data_list[i]->rec.data.tsn);
7691
0
    }
7692
7.09k
    sctp_flight_size_increase(data_list[i]);
7693
7.09k
    sctp_total_flight_increase(stcb, data_list[i]);
7694
7.09k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7695
0
      sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
7696
0
            asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
7697
0
    }
7698
7.09k
    asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
7699
7.09k
                (uint32_t) (data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
7700
7.09k
    if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7701
      /* SWS sender side engages */
7702
4
      asoc->peers_rwnd = 0;
7703
4
    }
7704
7.09k
  }
7705
7.09k
  if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
7706
0
    (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted)(stcb, net);
7707
0
  }
7708
7.09k
}
7709
7710
static void
7711
sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked)
7712
32.1k
{
7713
32.1k
  struct sctp_tmit_chunk *chk, *nchk;
7714
7715
182k
  TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7716
182k
    if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7717
178k
        (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7718
178k
        (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7719
152k
        (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7720
133k
        (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
7721
133k
        (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7722
133k
        (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7723
132k
        (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7724
32.2k
        (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7725
32.2k
        (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7726
31.2k
        (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7727
151k
        (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7728
      /* Stray chunks must be cleaned up */
7729
160k
  clean_up_anyway:
7730
160k
      TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7731
160k
      asoc->ctrl_queue_cnt--;
7732
160k
      if (chk->data) {
7733
115k
        sctp_m_freem(chk->data);
7734
115k
        chk->data = NULL;
7735
115k
      }
7736
160k
      if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
7737
56
        asoc->fwd_tsn_cnt--;
7738
56
      }
7739
160k
      sctp_free_a_chunk(stcb, chk, so_locked);
7740
160k
    } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7741
      /* special handling, we must look into the param */
7742
12.6k
      if (chk != asoc->str_reset) {
7743
8.43k
        goto clean_up_anyway;
7744
8.43k
      }
7745
12.6k
    }
7746
182k
  }
7747
32.1k
}
7748
7749
static uint32_t
7750
sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length,
7751
                       uint32_t space_left, uint32_t frag_point, int eeor_on)
7752
0
{
7753
  /* Make a decision on if I should split a
7754
   * msg into multiple parts. This is only asked of
7755
   * incomplete messages.
7756
   */
7757
0
  if (eeor_on) {
7758
    /* If we are doing EEOR we need to always send
7759
     * it if its the entire thing, since it might
7760
     * be all the guy is putting in the hopper.
7761
     */
7762
0
    if (space_left >= length) {
7763
      /*-
7764
       * If we have data outstanding,
7765
       * we get another chance when the sack
7766
       * arrives to transmit - wait for more data
7767
       */
7768
0
      if (stcb->asoc.total_flight == 0) {
7769
        /* If nothing is in flight, we zero
7770
         * the packet counter.
7771
         */
7772
0
        return (length);
7773
0
      }
7774
0
      return (0);
7775
7776
0
    } else {
7777
      /* You can fill the rest */
7778
0
      return (space_left);
7779
0
    }
7780
0
  }
7781
  /*-
7782
   * For those strange folk that make the send buffer
7783
   * smaller than our fragmentation point, we can't
7784
   * get a full msg in so we have to allow splitting.
7785
   */
7786
0
  if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7787
0
    return (length);
7788
0
  }
7789
0
  if ((length <= space_left) ||
7790
0
      ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7791
    /* Sub-optimal residual don't split in non-eeor mode. */
7792
0
    return (0);
7793
0
  }
7794
  /* If we reach here length is larger
7795
   * than the space_left. Do we wish to split
7796
   * it for the sake of packet putting together?
7797
   */
7798
0
  if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7799
    /* Its ok to split it */
7800
0
    return (min(space_left, frag_point));
7801
0
  }
7802
  /* Nope, can't split */
7803
0
  return (0);
7804
0
}
7805
7806
static uint32_t
7807
sctp_move_to_outqueue(struct sctp_tcb *stcb,
7808
                      struct sctp_nets *net,
7809
                      struct sctp_stream_out *strq,
7810
                      uint32_t space_left,
7811
                      uint32_t frag_point,
7812
                      int *giveup,
7813
                      int eeor_mode,
7814
                      int *bail,
7815
                      int so_locked)
7816
8.09k
{
7817
  /* Move from the stream to the send_queue keeping track of the total */
7818
8.09k
  struct sctp_association *asoc;
7819
8.09k
  struct sctp_stream_queue_pending *sp;
7820
8.09k
  struct sctp_tmit_chunk *chk;
7821
8.09k
  struct sctp_data_chunk *dchkh=NULL;
7822
8.09k
  struct sctp_idata_chunk *ndchkh=NULL;
7823
8.09k
  uint32_t to_move, length;
7824
8.09k
  int leading;
7825
8.09k
  uint8_t rcv_flags = 0;
7826
8.09k
  uint8_t some_taken;
7827
7828
8.09k
  SCTP_TCB_LOCK_ASSERT(stcb);
7829
8.09k
  asoc = &stcb->asoc;
7830
8.09k
one_more_time:
7831
  /*sa_ignore FREED_MEMORY*/
7832
8.09k
  sp = TAILQ_FIRST(&strq->outqueue);
7833
8.09k
  if (sp == NULL) {
7834
0
    sp = TAILQ_FIRST(&strq->outqueue);
7835
0
    if (sp) {
7836
0
      goto one_more_time;
7837
0
    }
7838
0
    if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) &&
7839
0
        (stcb->asoc.idata_supported == 0) &&
7840
0
        (strq->last_msg_incomplete)) {
7841
0
      SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7842
0
                  strq->sid,
7843
0
                  strq->last_msg_incomplete);
7844
0
      strq->last_msg_incomplete = 0;
7845
0
    }
7846
0
    to_move = 0;
7847
0
    goto out_of;
7848
0
  }
7849
8.09k
  if ((sp->msg_is_complete) && (sp->length == 0)) {
7850
0
    if (sp->sender_all_done) {
7851
      /* We are doing deferred cleanup. Last
7852
       * time through when we took all the data
7853
       * the sender_all_done was not set.
7854
       */
7855
0
      if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7856
0
        SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7857
0
        SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7858
0
                    sp->sender_all_done,
7859
0
                    sp->length,
7860
0
                    sp->msg_is_complete,
7861
0
                    sp->put_last_out);
7862
0
      }
7863
0
      atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7864
0
      TAILQ_REMOVE(&strq->outqueue, sp, next);
7865
0
      stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7866
0
      if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7867
0
          (strq->chunks_on_queues == 0) &&
7868
0
          TAILQ_EMPTY(&strq->outqueue)) {
7869
0
        stcb->asoc.trigger_reset = 1;
7870
0
      }
7871
0
      if (sp->net) {
7872
0
        sctp_free_remote_addr(sp->net);
7873
0
        sp->net = NULL;
7874
0
      }
7875
0
      if (sp->data) {
7876
0
        sctp_m_freem(sp->data);
7877
0
        sp->data = NULL;
7878
0
      }
7879
0
      sctp_free_a_strmoq(stcb, sp, so_locked);
7880
      /* back to get the next msg */
7881
0
      goto one_more_time;
7882
0
    } else {
7883
      /* sender just finished this but
7884
       * still holds a reference
7885
       */
7886
0
      *giveup = 1;
7887
0
      to_move = 0;
7888
0
      goto out_of;
7889
0
    }
7890
8.09k
  } else {
7891
    /* is there some to get */
7892
8.09k
    if (sp->length == 0) {
7893
      /* no */
7894
0
      *giveup = 1;
7895
0
      to_move = 0;
7896
0
      goto out_of;
7897
8.09k
    } else if (sp->discard_rest) {
7898
      /* Whack down the size */
7899
0
      atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7900
0
      if ((stcb->sctp_socket != NULL) &&
7901
0
          ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7902
0
           (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7903
0
        SCTP_SB_DECR(&stcb->sctp_socket->so_snd, sp->length);
7904
0
      }
7905
0
      if (sp->data) {
7906
0
        sctp_m_freem(sp->data);
7907
0
        sp->data = NULL;
7908
0
        sp->tail_mbuf = NULL;
7909
0
      }
7910
0
      sp->length = 0;
7911
0
      sp->some_taken = 1;
7912
0
      *giveup = 1;
7913
0
      to_move = 0;
7914
0
      goto out_of;
7915
0
    }
7916
8.09k
  }
7917
8.09k
  some_taken = sp->some_taken;
7918
8.09k
  length = sp->length;
7919
8.09k
  if (sp->msg_is_complete) {
7920
    /* The message is complete */
7921
8.09k
    to_move = min(length, frag_point);
7922
8.09k
    if (to_move == length) {
7923
      /* All of it fits in the MTU */
7924
3.55k
      if (sp->some_taken) {
7925
0
        rcv_flags |= SCTP_DATA_LAST_FRAG;
7926
3.55k
      } else {
7927
3.55k
        rcv_flags |= SCTP_DATA_NOT_FRAG;
7928
3.55k
      }
7929
3.55k
      sp->put_last_out = 1;
7930
3.55k
      if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) {
7931
0
        rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7932
0
      }
7933
4.54k
    } else {
7934
      /* Not all of it fits, we fragment */
7935
4.54k
      if (sp->some_taken == 0) {
7936
1.51k
        rcv_flags |= SCTP_DATA_FIRST_FRAG;
7937
1.51k
      }
7938
4.54k
      sp->some_taken = 1;
7939
4.54k
    }
7940
8.09k
  } else {
7941
0
    to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode);
7942
0
    if (to_move > 0) {
7943
0
      if (to_move >= length) {
7944
0
        to_move = length;
7945
0
      }
7946
0
      if (sp->some_taken == 0) {
7947
0
        rcv_flags |= SCTP_DATA_FIRST_FRAG;
7948
0
        sp->some_taken = 1;
7949
0
      }
7950
0
    } else {
7951
      /* Nothing to take. */
7952
0
      *giveup = 1;
7953
0
      to_move = 0;
7954
0
      goto out_of;
7955
0
    }
7956
0
  }
7957
7958
  /* If we reach here, we can copy out a chunk */
7959
8.09k
  sctp_alloc_a_chunk(stcb, chk);
7960
8.09k
  if (chk == NULL) {
7961
    /* No chunk memory */
7962
0
    *giveup = 1;
7963
0
    to_move = 0;
7964
0
    goto out_of;
7965
0
  }
7966
  /* Setup for unordered if needed by looking
7967
   * at the user sent info flags.
7968
   */
7969
8.09k
  if (sp->sinfo_flags & SCTP_UNORDERED) {
7970
0
    rcv_flags |= SCTP_DATA_UNORDERED;
7971
0
  }
7972
8.09k
  if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
7973
8.09k
      (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) {
7974
0
    rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7975
0
  }
7976
  /* clear out the chunk before setting up */
7977
8.09k
  memset(chk, 0, sizeof(*chk));
7978
8.09k
  chk->rec.data.rcv_flags = rcv_flags;
7979
7980
8.09k
  if (to_move >= length) {
7981
    /* we think we can steal the whole thing */
7982
3.55k
    if (to_move < sp->length) {
7983
      /* bail, it changed */
7984
0
      goto dont_do_it;
7985
0
    }
7986
3.55k
    chk->data = sp->data;
7987
3.55k
    chk->last_mbuf = sp->tail_mbuf;
7988
    /* register the stealing */
7989
3.55k
    sp->data = sp->tail_mbuf = NULL;
7990
4.54k
  } else {
7991
4.54k
    struct mbuf *m;
7992
4.54k
  dont_do_it:
7993
4.54k
    chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT);
7994
4.54k
    chk->last_mbuf = NULL;
7995
4.54k
    if (chk->data == NULL) {
7996
0
      sp->some_taken = some_taken;
7997
0
      sctp_free_a_chunk(stcb, chk, so_locked);
7998
0
      *bail = 1;
7999
0
      to_move = 0;
8000
0
      goto out_of;
8001
0
    }
8002
#ifdef SCTP_MBUF_LOGGING
8003
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8004
      sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY);
8005
    }
8006
#endif
8007
    /* Pull off the data */
8008
4.54k
    m_adj(sp->data, to_move);
8009
    /* Now lets work our way down and compact it */
8010
4.54k
    m = sp->data;
8011
6.05k
    while (m && (SCTP_BUF_LEN(m) == 0)) {
8012
1.51k
      sp->data = SCTP_BUF_NEXT(m);
8013
1.51k
      SCTP_BUF_NEXT(m) = NULL;
8014
1.51k
      if (sp->tail_mbuf == m) {
8015
        /*-
8016
         * Freeing tail? TSNH since
8017
         * we supposedly were taking less
8018
         * than the sp->length.
8019
         */
8020
0
#ifdef INVARIANTS
8021
0
        panic("Huh, freeing tail? - TSNH");
8022
#else
8023
        SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
8024
        sp->tail_mbuf = sp->data = NULL;
8025
        sp->length = 0;
8026
#endif
8027
0
      }
8028
1.51k
      sctp_m_free(m);
8029
1.51k
      m = sp->data;
8030
1.51k
    }
8031
4.54k
  }
8032
8.09k
  if (SCTP_BUF_IS_EXTENDED(chk->data)) {
8033
4.54k
    chk->copy_by_ref = 1;
8034
4.54k
  } else {
8035
3.55k
    chk->copy_by_ref = 0;
8036
3.55k
  }
8037
  /* get last_mbuf and counts of mb usage
8038
   * This is ugly but hopefully its only one mbuf.
8039
   */
8040
8.09k
  if (chk->last_mbuf == NULL) {
8041
4.54k
    chk->last_mbuf = chk->data;
8042
6.05k
    while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
8043
1.51k
      chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
8044
1.51k
    }
8045
4.54k
  }
8046
8047
8.09k
  if (to_move > length) {
8048
    /*- This should not happen either
8049
     * since we always lower to_move to the size
8050
     * of sp->length if its larger.
8051
     */
8052
0
#ifdef INVARIANTS
8053
0
    panic("Huh, how can to_move be larger?");
8054
#else
8055
    SCTP_PRINTF("Huh, how can to_move be larger?\n");
8056
    sp->length = 0;
8057
#endif
8058
8.09k
  } else {
8059
8.09k
    atomic_subtract_int(&sp->length, to_move);
8060
8.09k
  }
8061
8.09k
  leading = SCTP_DATA_CHUNK_OVERHEAD(stcb);
8062
8.09k
  if (M_LEADINGSPACE(chk->data) < leading) {
8063
    /* Not enough room for a chunk header, get some */
8064
4.54k
    struct mbuf *m;
8065
8066
4.54k
    m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA);
8067
4.54k
    if (m == NULL) {
8068
      /*
8069
       * we're in trouble here. _PREPEND below will free
8070
       * all the data if there is no leading space, so we
8071
       * must put the data back and restore.
8072
       */
8073
0
      if (sp->data == NULL) {
8074
        /* unsteal the data */
8075
0
        sp->data = chk->data;
8076
0
        sp->tail_mbuf = chk->last_mbuf;
8077
0
      } else {
8078
0
        struct mbuf *m_tmp;
8079
        /* reassemble the data */
8080
0
        m_tmp = sp->data;
8081
0
        sp->data = chk->data;
8082
0
        SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
8083
0
      }
8084
0
      sp->some_taken = some_taken;
8085
0
      atomic_add_int(&sp->length, to_move);
8086
0
      chk->data = NULL;
8087
0
      *bail = 1;
8088
0
      sctp_free_a_chunk(stcb, chk, so_locked);
8089
0
      to_move = 0;
8090
0
      goto out_of;
8091
4.54k
    } else {
8092
4.54k
      SCTP_BUF_LEN(m) = 0;
8093
4.54k
      SCTP_BUF_NEXT(m) = chk->data;
8094
4.54k
      chk->data = m;
8095
4.54k
      M_ALIGN(chk->data, 4);
8096
4.54k
    }
8097
4.54k
  }
8098
8.09k
  SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT);
8099
8.09k
  if (chk->data == NULL) {
8100
    /* HELP, TSNH since we assured it would not above? */
8101
0
#ifdef INVARIANTS
8102
0
    panic("prepend fails HELP?");
8103
#else
8104
    SCTP_PRINTF("prepend fails HELP?\n");
8105
    sctp_free_a_chunk(stcb, chk, so_locked);
8106
#endif
8107
0
    *bail = 1;
8108
0
    to_move = 0;
8109
0
    goto out_of;
8110
0
  }
8111
8.09k
  sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb));
8112
8.09k
  chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb));
8113
8.09k
  chk->book_size_scale = 0;
8114
8.09k
  chk->sent = SCTP_DATAGRAM_UNSENT;
8115
8116
8.09k
  chk->flags = 0;
8117
8.09k
  chk->asoc = &stcb->asoc;
8118
8.09k
  chk->pad_inplace = 0;
8119
8.09k
  chk->no_fr_allowed = 0;
8120
8.09k
  if (stcb->asoc.idata_supported == 0) {
8121
5.11k
    if (rcv_flags & SCTP_DATA_UNORDERED) {
8122
      /* Just use 0. The receiver ignores the values. */
8123
0
      chk->rec.data.mid = 0;
8124
5.11k
    } else {
8125
5.11k
      chk->rec.data.mid = strq->next_mid_ordered;
8126
5.11k
      if (rcv_flags & SCTP_DATA_LAST_FRAG) {
8127
942
        strq->next_mid_ordered++;
8128
942
      }
8129
5.11k
    }
8130
5.11k
  } else {
8131
2.97k
    if (rcv_flags & SCTP_DATA_UNORDERED) {
8132
0
      chk->rec.data.mid = strq->next_mid_unordered;
8133
0
      if (rcv_flags & SCTP_DATA_LAST_FRAG) {
8134
0
        strq->next_mid_unordered++;
8135
0
      }
8136
2.97k
    } else {
8137
2.97k
      chk->rec.data.mid = strq->next_mid_ordered;
8138
2.97k
      if (rcv_flags & SCTP_DATA_LAST_FRAG) {
8139
2.61k
        strq->next_mid_ordered++;
8140
2.61k
      }
8141
2.97k
    }
8142
2.97k
  }
8143
8.09k
  chk->rec.data.sid = sp->sid;
8144
8.09k
  chk->rec.data.ppid = sp->ppid;
8145
8.09k
  chk->rec.data.context = sp->context;
8146
8.09k
  chk->rec.data.doing_fast_retransmit = 0;
8147
8148
8.09k
  chk->rec.data.timetodrop = sp->ts;
8149
8.09k
  chk->flags = sp->act_flags;
8150
8151
8.09k
  if (sp->net) {
8152
0
    chk->whoTo = sp->net;
8153
0
    atomic_add_int(&chk->whoTo->ref_count, 1);
8154
0
  } else
8155
8.09k
    chk->whoTo = NULL;
8156
8157
8.09k
  if (sp->holds_key_ref) {
8158
0
    chk->auth_keyid = sp->auth_keyid;
8159
0
    sctp_auth_key_acquire(stcb, chk->auth_keyid);
8160
0
    chk->holds_key_ref = 1;
8161
0
  }
8162
8.09k
  stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, to_move);
8163
#if defined(__FreeBSD__) && !defined(__Userspace__)
8164
  chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1);
8165
#else
8166
8.09k
  chk->rec.data.tsn = asoc->sending_seq++;
8167
8.09k
#endif
8168
8.09k
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
8169
0
    sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
8170
0
                   (uint32_t)(uintptr_t)stcb, sp->length,
8171
0
                   (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)),
8172
0
                   chk->rec.data.tsn);
8173
0
  }
8174
8.09k
  if (stcb->asoc.idata_supported == 0) {
8175
5.11k
    dchkh = mtod(chk->data, struct sctp_data_chunk *);
8176
5.11k
  } else {
8177
2.97k
    ndchkh = mtod(chk->data, struct sctp_idata_chunk *);
8178
2.97k
  }
8179
  /*
8180
   * Put the rest of the things in place now. Size was done
8181
   * earlier in previous loop prior to padding.
8182
   */
8183
8184
8.09k
  SCTP_TCB_LOCK_ASSERT(stcb);
8185
#ifdef SCTP_ASOCLOG_OF_TSNS
8186
  if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
8187
    asoc->tsn_out_at = 0;
8188
    asoc->tsn_out_wrapped = 1;
8189
  }
8190
  asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn;
8191
  asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid;
8192
  asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid;
8193
  asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
8194
  asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
8195
  asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
8196
  asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
8197
  asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
8198
  asoc->tsn_out_at++;
8199
#endif
8200
8.09k
  if (stcb->asoc.idata_supported == 0) {
8201
5.11k
    dchkh->ch.chunk_type = SCTP_DATA;
8202
5.11k
    dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
8203
5.11k
    dchkh->dp.tsn = htonl(chk->rec.data.tsn);
8204
5.11k
    dchkh->dp.sid = htons(strq->sid);
8205
5.11k
    dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid);
8206
5.11k
    dchkh->dp.ppid = chk->rec.data.ppid;
8207
5.11k
    dchkh->ch.chunk_length = htons(chk->send_size);
8208
5.11k
  } else {
8209
2.97k
    ndchkh->ch.chunk_type = SCTP_IDATA;
8210
2.97k
    ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
8211
2.97k
    ndchkh->dp.tsn = htonl(chk->rec.data.tsn);
8212
2.97k
    ndchkh->dp.sid = htons(strq->sid);
8213
2.97k
    ndchkh->dp.reserved = htons(0);
8214
2.97k
    ndchkh->dp.mid = htonl(chk->rec.data.mid);
8215
2.97k
    if (sp->fsn == 0)
8216
2.73k
      ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid;
8217
244
    else
8218
244
      ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn);
8219
2.97k
    sp->fsn++;
8220
2.97k
    ndchkh->ch.chunk_length = htons(chk->send_size);
8221
2.97k
  }
8222
  /* Now advance the chk->send_size by the actual pad needed. */
8223
8.09k
  if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
8224
    /* need a pad */
8225
3.55k
    struct mbuf *lm;
8226
3.55k
    int pads;
8227
8228
3.55k
    pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
8229
3.55k
    lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf);
8230
3.55k
    if (lm != NULL) {
8231
3.55k
      chk->last_mbuf = lm;
8232
3.55k
      chk->pad_inplace = 1;
8233
3.55k
    }
8234
3.55k
    chk->send_size += pads;
8235
3.55k
  }
8236
8.09k
  if (PR_SCTP_ENABLED(chk->flags)) {
8237
0
    asoc->pr_sctp_cnt++;
8238
0
  }
8239
8.09k
  if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
8240
    /* All done pull and kill the message */
8241
3.55k
    if (sp->put_last_out == 0) {
8242
0
      SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
8243
0
      SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
8244
0
                  sp->sender_all_done,
8245
0
                  sp->length,
8246
0
                  sp->msg_is_complete,
8247
0
                  sp->put_last_out);
8248
0
    }
8249
3.55k
    atomic_subtract_int(&asoc->stream_queue_cnt, 1);
8250
3.55k
    TAILQ_REMOVE(&strq->outqueue, sp, next);
8251
3.55k
    stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
8252
3.55k
    if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
8253
25
        (strq->chunks_on_queues == 0) &&
8254
3.55k
        TAILQ_EMPTY(&strq->outqueue)) {
8255
25
      stcb->asoc.trigger_reset = 1;
8256
25
    }
8257
3.55k
    if (sp->net) {
8258
0
      sctp_free_remote_addr(sp->net);
8259
0
      sp->net = NULL;
8260
0
    }
8261
3.55k
    if (sp->data) {
8262
0
      sctp_m_freem(sp->data);
8263
0
      sp->data = NULL;
8264
0
    }
8265
3.55k
    sctp_free_a_strmoq(stcb, sp, so_locked);
8266
3.55k
  }
8267
8.09k
  asoc->chunks_on_out_queue++;
8268
8.09k
  strq->chunks_on_queues++;
8269
8.09k
  TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
8270
8.09k
  asoc->send_queue_cnt++;
8271
8.09k
out_of:
8272
8.09k
  return (to_move);
8273
8.09k
}
8274
8275
static void
8276
sctp_fill_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
8277
                   uint32_t frag_point, int eeor_mode, int *quit_now,
8278
                   int so_locked)
8279
8.87k
{
8280
8.87k
  struct sctp_association *asoc;
8281
8.87k
  struct sctp_stream_out *strq;
8282
8.87k
  uint32_t space_left, moved, total_moved;
8283
8.87k
  int bail, giveup;
8284
8285
8.87k
  SCTP_TCB_LOCK_ASSERT(stcb);
8286
8.87k
  asoc = &stcb->asoc;
8287
8.87k
  total_moved = 0;
8288
8.87k
  switch (net->ro._l_addr.sa.sa_family) {
8289
0
#ifdef INET
8290
3
    case AF_INET:
8291
3
      space_left = net->mtu - SCTP_MIN_V4_OVERHEAD;
8292
3
      break;
8293
0
#endif
8294
0
#ifdef INET6
8295
778
    case AF_INET6:
8296
778
      space_left = net->mtu - SCTP_MIN_OVERHEAD;
8297
778
      break;
8298
0
#endif
8299
0
#if defined(__Userspace__)
8300
8.09k
    case AF_CONN:
8301
8.09k
      space_left = net->mtu - sizeof(struct sctphdr);
8302
8.09k
      break;
8303
0
#endif
8304
0
    default:
8305
      /* TSNH */
8306
0
      space_left = net->mtu;
8307
0
      break;
8308
8.87k
  }
8309
  /* Need an allowance for the data chunk header too */
8310
8.87k
  space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
8311
8312
  /* must make even word boundary */
8313
8.87k
  space_left &= 0xfffffffc;
8314
8.87k
  strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
8315
8.87k
  giveup = 0;
8316
8.87k
  bail = 0;
8317
16.9k
  while ((space_left > 0) && (strq != NULL)) {
8318
8.09k
    moved = sctp_move_to_outqueue(stcb, net, strq, space_left,
8319
8.09k
                                  frag_point, &giveup, eeor_mode,
8320
8.09k
                                  &bail, so_locked);
8321
8.09k
    if ((giveup != 0) || (bail != 0)) {
8322
0
      break;
8323
0
    }
8324
8.09k
    strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
8325
8.09k
    total_moved += moved;
8326
8.09k
    if (space_left >= moved) {
8327
8.09k
      space_left -= moved;
8328
8.09k
    } else {
8329
0
      space_left = 0;
8330
0
    }
8331
8.09k
    if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) {
8332
3.55k
      space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
8333
4.54k
    } else {
8334
4.54k
      space_left = 0;
8335
4.54k
    }
8336
8.09k
    space_left &= 0xfffffffc;
8337
8.09k
  }
8338
8.87k
  if (bail != 0)
8339
0
    *quit_now = 1;
8340
8341
8.87k
  stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
8342
8343
8.87k
  if (total_moved == 0) {
8344
781
    if ((stcb->asoc.sctp_cmt_on_off == 0) &&
8345
781
        (net == stcb->asoc.primary_destination)) {
8346
      /* ran dry for primary network net */
8347
13
      SCTP_STAT_INCR(sctps_primary_randry);
8348
768
    } else if (stcb->asoc.sctp_cmt_on_off > 0) {
8349
      /* ran dry with CMT on */
8350
0
      SCTP_STAT_INCR(sctps_cmt_randry);
8351
0
    }
8352
781
  }
8353
8.87k
}
8354
8355
void
8356
sctp_fix_ecn_echo(struct sctp_association *asoc)
8357
0
{
8358
0
  struct sctp_tmit_chunk *chk;
8359
8360
0
  TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8361
0
    if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8362
0
      chk->sent = SCTP_DATAGRAM_UNSENT;
8363
0
    }
8364
0
  }
8365
0
}
8366
8367
void
8368
sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
8369
1.41k
{
8370
1.41k
  struct sctp_association *asoc;
8371
1.41k
  struct sctp_tmit_chunk *chk;
8372
1.41k
  struct sctp_stream_queue_pending *sp;
8373
1.41k
  unsigned int i;
8374
8375
1.41k
  if (net == NULL) {
8376
0
    return;
8377
0
  }
8378
1.41k
  asoc = &stcb->asoc;
8379
364k
  for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
8380
363k
    TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
8381
0
      if (sp->net == net) {
8382
0
        sctp_free_remote_addr(sp->net);
8383
0
        sp->net = NULL;
8384
0
      }
8385
0
    }
8386
363k
  }
8387
1.41k
  TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
8388
590
    if (chk->whoTo == net) {
8389
0
      sctp_free_remote_addr(chk->whoTo);
8390
0
      chk->whoTo = NULL;
8391
0
    }
8392
590
  }
8393
1.41k
}
8394
8395
int
8396
sctp_med_chunk_output(struct sctp_inpcb *inp,
8397
                      struct sctp_tcb *stcb,
8398
                      struct sctp_association *asoc,
8399
                      int *num_out,
8400
                      int *reason_code,
8401
                      int control_only, int from_where,
8402
                      struct timeval *now, int *now_filled,
8403
                      uint32_t frag_point, int so_locked)
8404
53.0k
{
8405
  /**
8406
   * Ok this is the generic chunk service queue. we must do the
8407
   * following:
8408
   * - Service the stream queue that is next, moving any
8409
   *   message (note I must get a complete message i.e. FIRST/MIDDLE and
8410
   *   LAST to the out queue in one pass) and assigning TSN's. This
8411
   *   only applies though if the peer does not support NDATA. For NDATA
8412
   *   chunks its ok to not send the entire message ;-)
8413
   * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and
8414
   *   formulate and send the low level chunks. Making sure to combine
8415
   *   any control in the control chunk queue also.
8416
   */
8417
53.0k
  struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
8418
53.0k
  struct mbuf *outchain, *endoutchain;
8419
53.0k
  struct sctp_tmit_chunk *chk, *nchk;
8420
8421
  /* temp arrays for unlinking */
8422
53.0k
  struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
8423
53.0k
  int no_fragmentflg, error;
8424
53.0k
  unsigned int max_rwnd_per_dest, max_send_per_dest;
8425
53.0k
  int one_chunk, hbflag, skip_data_for_this_net;
8426
53.0k
  int asconf, cookie, no_out_cnt;
8427
53.0k
  int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
8428
53.0k
  unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
8429
53.0k
  int tsns_sent = 0;
8430
53.0k
  uint32_t auth_offset;
8431
53.0k
  struct sctp_auth_chunk *auth;
8432
53.0k
  uint16_t auth_keyid;
8433
53.0k
  int override_ok = 1;
8434
53.0k
  int skip_fill_up = 0;
8435
53.0k
  int data_auth_reqd = 0;
8436
  /* JRS 5/14/07 - Add flag for whether a heartbeat is sent to
8437
     the destination. */
8438
53.0k
  int quit_now = 0;
8439
53.0k
  bool use_zero_crc;
8440
8441
#if defined(__APPLE__) && !defined(__Userspace__)
8442
  if (so_locked) {
8443
    sctp_lock_assert(SCTP_INP_SO(inp));
8444
  } else {
8445
    sctp_unlock_assert(SCTP_INP_SO(inp));
8446
  }
8447
#endif
8448
53.0k
  *num_out = 0;
8449
53.0k
  *reason_code = 0;
8450
53.0k
  auth_keyid = stcb->asoc.authinfo.active_keyid;
8451
53.0k
  if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
8452
53.0k
      (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
8453
53.0k
      (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
8454
34
    eeor_mode = 1;
8455
53.0k
  } else {
8456
53.0k
    eeor_mode = 0;
8457
53.0k
  }
8458
53.0k
  ctl_cnt = no_out_cnt = asconf = cookie = 0;
8459
  /*
8460
   * First lets prime the pump. For each destination, if there is room
8461
   * in the flight size, attempt to pull an MTU's worth out of the
8462
   * stream queues into the general send_queue
8463
   */
8464
#ifdef SCTP_AUDITING_ENABLED
8465
  sctp_audit_log(0xC2, 2);
8466
#endif
8467
53.0k
  SCTP_TCB_LOCK_ASSERT(stcb);
8468
53.0k
  hbflag = 0;
8469
53.0k
  if (control_only)
8470
1.06k
    no_data_chunks = 1;
8471
51.9k
  else
8472
51.9k
    no_data_chunks = 0;
8473
8474
  /* Nothing to possible to send? */
8475
53.0k
  if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
8476
25.1k
       (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
8477
53.0k
      TAILQ_EMPTY(&asoc->asconf_send_queue) &&
8478
53.0k
      TAILQ_EMPTY(&asoc->send_queue) &&
8479
27.5k
      sctp_is_there_unsent_data(stcb, so_locked) == 0) {
8480
0
  nothing_to_send:
8481
0
    *reason_code = 9;
8482
0
    return (0);
8483
0
  }
8484
53.0k
  if (asoc->peers_rwnd == 0) {
8485
    /* No room in peers rwnd */
8486
168
    *reason_code = 1;
8487
168
    if (asoc->total_flight > 0) {
8488
      /* we are allowed one chunk in flight */
8489
4
      no_data_chunks = 1;
8490
4
    }
8491
168
  }
8492
53.0k
  if (stcb->asoc.ecn_echo_cnt_onq) {
8493
    /* Record where a sack goes, if any */
8494
0
    if (no_data_chunks &&
8495
0
        (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
8496
      /* Nothing but ECNe to send - we don't do that */
8497
0
      goto nothing_to_send;
8498
0
    }
8499
0
    TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8500
0
      if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8501
0
          (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8502
0
        sack_goes_to = chk->whoTo;
8503
0
        break;
8504
0
      }
8505
0
    }
8506
0
  }
8507
53.0k
  max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
8508
53.0k
  if (stcb->sctp_socket)
8509
53.0k
    max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
8510
0
  else
8511
0
    max_send_per_dest = 0;
8512
53.0k
  if (no_data_chunks == 0) {
8513
    /* How many non-directed chunks are there? */
8514
51.9k
    TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
8515
965
      if (chk->whoTo == NULL) {
8516
        /* We already have non-directed
8517
         * chunks on the queue, no need
8518
         * to do a fill-up.
8519
         */
8520
965
        skip_fill_up = 1;
8521
965
        break;
8522
965
      }
8523
965
    }
8524
51.9k
  }
8525
53.0k
  if ((no_data_chunks == 0) &&
8526
51.9k
      (skip_fill_up == 0) &&
8527
51.0k
      (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
8528
31.8k
    TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
8529
      /*
8530
       * This for loop we are in takes in
8531
       * each net, if its's got space in cwnd and
8532
       * has data sent to it (when CMT is off) then it
8533
       * calls sctp_fill_outqueue for the net. This gets
8534
       * data on the send queue for that network.
8535
       *
8536
       * In sctp_fill_outqueue TSN's are assigned and
8537
       * data is copied out of the stream buffers. Note
8538
       * mostly copy by reference (we hope).
8539
       */
8540
31.8k
      net->window_probe = 0;
8541
31.8k
      if ((net != stcb->asoc.alternate) &&
8542
31.7k
          ((net->dest_state & SCTP_ADDR_PF) ||
8543
31.7k
           ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
8544
31.7k
           (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
8545
2.03k
        if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8546
0
          sctp_log_cwnd(stcb, net, 1,
8547
0
                        SCTP_CWND_LOG_FILL_OUTQ_CALLED);
8548
0
        }
8549
2.03k
        continue;
8550
2.03k
      }
8551
29.7k
      if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
8552
0
          (net->flight_size == 0)) {
8553
0
        (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins)(stcb, net);
8554
0
      }
8555
29.7k
      if (net->flight_size >= net->cwnd) {
8556
        /* skip this network, no room - can't fill */
8557
20.8k
        if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8558
0
          sctp_log_cwnd(stcb, net, 3,
8559
0
                        SCTP_CWND_LOG_FILL_OUTQ_CALLED);
8560
0
        }
8561
20.8k
        continue;
8562
20.8k
      }
8563
8.87k
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8564
0
        sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
8565
0
      }
8566
8.87k
      sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
8567
8.87k
      if (quit_now) {
8568
        /* memory alloc failure */
8569
0
        no_data_chunks = 1;
8570
0
        break;
8571
0
      }
8572
8.87k
    }
8573
28.9k
  }
8574
  /* now service each destination and send out what we can for it */
8575
  /* Nothing to send? */
8576
53.0k
  if (TAILQ_EMPTY(&asoc->control_send_queue) &&
8577
53.0k
      TAILQ_EMPTY(&asoc->asconf_send_queue) &&
8578
53.0k
      TAILQ_EMPTY(&asoc->send_queue)) {
8579
20.8k
    *reason_code = 8;
8580
20.8k
    return (0);
8581
20.8k
  }
8582
8583
32.1k
  if (asoc->sctp_cmt_on_off > 0) {
8584
    /* get the last start point */
8585
0
    start_at = asoc->last_net_cmt_send_started;
8586
0
    if (start_at == NULL) {
8587
      /* null so to beginning */
8588
0
      start_at = TAILQ_FIRST(&asoc->nets);
8589
0
    } else {
8590
0
      start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
8591
0
      if (start_at == NULL) {
8592
0
        start_at = TAILQ_FIRST(&asoc->nets);
8593
0
      }
8594
0
    }
8595
0
    asoc->last_net_cmt_send_started = start_at;
8596
32.1k
  } else {
8597
32.1k
    start_at = TAILQ_FIRST(&asoc->nets);
8598
32.1k
  }
8599
182k
  TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
8600
182k
    if (chk->whoTo == NULL) {
8601
108k
      if (asoc->alternate) {
8602
604
        chk->whoTo = asoc->alternate;
8603
108k
      } else {
8604
108k
        chk->whoTo = asoc->primary_destination;
8605
108k
      }
8606
108k
      atomic_add_int(&chk->whoTo->ref_count, 1);
8607
108k
    }
8608
182k
  }
8609
32.1k
  old_start_at = NULL;
8610
64.3k
again_one_more_time:
8611
105k
  for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
8612
    /* how much can we send? */
8613
    /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
8614
73.2k
    if (old_start_at && (old_start_at == net)) {
8615
      /* through list completely. */
8616
32.1k
      break;
8617
32.1k
    }
8618
41.1k
    tsns_sent = 0xa;
8619
41.1k
    if (TAILQ_EMPTY(&asoc->control_send_queue) &&
8620
41.1k
        TAILQ_EMPTY(&asoc->asconf_send_queue) &&
8621
8.46k
        (net->flight_size >= net->cwnd)) {
8622
      /* Nothing on control or asconf and flight is full, we can skip
8623
       * even in the CMT case.
8624
       */
8625
0
      continue;
8626
0
    }
8627
41.1k
    bundle_at = 0;
8628
41.1k
    endoutchain = outchain = NULL;
8629
41.1k
    auth = NULL;
8630
41.1k
    auth_offset = 0;
8631
41.1k
    no_fragmentflg = 1;
8632
41.1k
    one_chunk = 0;
8633
41.1k
    if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
8634
6.44k
      skip_data_for_this_net = 1;
8635
34.6k
    } else {
8636
34.6k
      skip_data_for_this_net = 0;
8637
34.6k
    }
8638
41.1k
    switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8639
0
#ifdef INET
8640
3.80k
    case AF_INET:
8641
3.80k
      mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8642
3.80k
      break;
8643
0
#endif
8644
0
#ifdef INET6
8645
5.15k
    case AF_INET6:
8646
5.15k
      mtu = net->mtu - SCTP_MIN_OVERHEAD;
8647
5.15k
      break;
8648
0
#endif
8649
0
#if defined(__Userspace__)
8650
32.1k
    case AF_CONN:
8651
32.1k
      mtu = net->mtu - sizeof(struct sctphdr);
8652
32.1k
      break;
8653
0
#endif
8654
0
    default:
8655
      /* TSNH */
8656
0
      mtu = net->mtu;
8657
0
      break;
8658
41.1k
    }
8659
41.1k
    mx_mtu = mtu;
8660
41.1k
    to_out = 0;
8661
41.1k
    if (mtu > asoc->peers_rwnd) {
8662
168
      if (asoc->total_flight > 0) {
8663
        /* We have a packet in flight somewhere */
8664
4
        r_mtu = asoc->peers_rwnd;
8665
164
      } else {
8666
        /* We are always allowed to send one MTU out */
8667
164
        one_chunk = 1;
8668
164
        r_mtu = mtu;
8669
164
      }
8670
40.9k
    } else {
8671
40.9k
      r_mtu = mtu;
8672
40.9k
    }
8673
41.1k
    error = 0;
8674
    /************************/
8675
    /* ASCONF transmission */
8676
    /************************/
8677
    /* Now first lets go through the asconf queue */
8678
41.1k
    TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8679
0
      if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8680
0
        continue;
8681
0
      }
8682
0
      if (chk->whoTo == NULL) {
8683
0
        if (asoc->alternate == NULL) {
8684
0
          if (asoc->primary_destination != net) {
8685
0
            break;
8686
0
          }
8687
0
        } else {
8688
0
          if (asoc->alternate != net) {
8689
0
            break;
8690
0
          }
8691
0
        }
8692
0
      } else {
8693
0
        if (chk->whoTo != net) {
8694
0
          break;
8695
0
        }
8696
0
      }
8697
0
      if (chk->data == NULL) {
8698
0
        break;
8699
0
      }
8700
0
      if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8701
0
          chk->sent != SCTP_DATAGRAM_RESEND) {
8702
0
        break;
8703
0
      }
8704
      /*
8705
       * if no AUTH is yet included and this chunk
8706
       * requires it, make sure to account for it.  We
8707
       * don't apply the size until the AUTH chunk is
8708
       * actually added below in case there is no room for
8709
       * this chunk. NOTE: we overload the use of "omtu"
8710
       * here
8711
       */
8712
0
      if ((auth == NULL) &&
8713
0
          sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8714
0
              stcb->asoc.peer_auth_chunks)) {
8715
0
        omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8716
0
      } else
8717
0
        omtu = 0;
8718
      /* Here we do NOT factor the r_mtu */
8719
0
      if ((chk->send_size < (int)(mtu - omtu)) ||
8720
0
          (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8721
        /*
8722
         * We probably should glom the mbuf chain
8723
         * from the chk->data for control but the
8724
         * problem is it becomes yet one more level
8725
         * of tracking to do if for some reason
8726
         * output fails. Then I have got to
8727
         * reconstruct the merged control chain.. el
8728
         * yucko.. for now we take the easy way and
8729
         * do the copy
8730
         */
8731
        /*
8732
         * Add an AUTH chunk, if chunk requires it
8733
         * save the offset into the chain for AUTH
8734
         */
8735
0
        if ((auth == NULL) &&
8736
0
            (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8737
0
                 stcb->asoc.peer_auth_chunks))) {
8738
0
          outchain = sctp_add_auth_chunk(outchain,
8739
0
                       &endoutchain,
8740
0
                       &auth,
8741
0
                       &auth_offset,
8742
0
                       stcb,
8743
0
                       chk->rec.chunk_id.id);
8744
0
          SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8745
0
        }
8746
0
        outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8747
0
                     (int)chk->rec.chunk_id.can_take_data,
8748
0
                     chk->send_size, chk->copy_by_ref);
8749
0
        if (outchain == NULL) {
8750
0
          *reason_code = 8;
8751
0
          SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8752
0
          return (ENOMEM);
8753
0
        }
8754
0
        SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8755
        /* update our MTU size */
8756
0
        if (mtu > (chk->send_size + omtu))
8757
0
          mtu -= (chk->send_size + omtu);
8758
0
        else
8759
0
          mtu = 0;
8760
0
        to_out += (chk->send_size + omtu);
8761
        /* Do clear IP_DF ? */
8762
0
        if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8763
0
          no_fragmentflg = 0;
8764
0
        }
8765
0
        if (chk->rec.chunk_id.can_take_data)
8766
0
          chk->data = NULL;
8767
        /*
8768
         * set hb flag since we can
8769
         * use these for RTO
8770
         */
8771
0
        hbflag = 1;
8772
0
        asconf = 1;
8773
        /*
8774
         * should sysctl this: don't
8775
         * bundle data with ASCONF
8776
         * since it requires AUTH
8777
         */
8778
0
        no_data_chunks = 1;
8779
0
        chk->sent = SCTP_DATAGRAM_SENT;
8780
0
        if (chk->whoTo == NULL) {
8781
0
          chk->whoTo = net;
8782
0
          atomic_add_int(&net->ref_count, 1);
8783
0
        }
8784
0
        chk->snd_count++;
8785
0
        if (mtu == 0) {
8786
          /*
8787
           * Ok we are out of room but we can
8788
           * output without effecting the
8789
           * flight size since this little guy
8790
           * is a control only packet.
8791
           */
8792
0
          sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8793
          /*
8794
           * do NOT clear the asconf
8795
           * flag as it is used to do
8796
           * appropriate source address
8797
           * selection.
8798
           */
8799
0
          if (*now_filled == 0) {
8800
0
            (void)SCTP_GETTIME_TIMEVAL(now);
8801
0
            *now_filled = 1;
8802
0
          }
8803
0
          net->last_sent_time = *now;
8804
0
          hbflag = 0;
8805
0
          if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8806
0
                                                  (struct sockaddr *)&net->ro._l_addr,
8807
0
                                                  outchain, auth_offset, auth,
8808
0
                                                  stcb->asoc.authinfo.active_keyid,
8809
0
                                                  no_fragmentflg, 0, asconf,
8810
0
                                                  inp->sctp_lport, stcb->rport,
8811
0
                                                  htonl(stcb->asoc.peer_vtag),
8812
0
                                                  net->port, NULL,
8813
#if defined(__FreeBSD__) && !defined(__Userspace__)
8814
                                                  0, 0,
8815
#endif
8816
0
                                                  false, so_locked))) {
8817
            /* error, we could not output */
8818
0
            SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8819
0
            if (from_where == 0) {
8820
0
              SCTP_STAT_INCR(sctps_lowlevelerrusr);
8821
0
            }
8822
0
            if (error == ENOBUFS) {
8823
0
              asoc->ifp_had_enobuf = 1;
8824
0
              SCTP_STAT_INCR(sctps_lowlevelerr);
8825
0
            }
8826
            /* error, could not output */
8827
0
            if (error == EHOSTUNREACH) {
8828
              /*
8829
               * Destination went
8830
               * unreachable
8831
               * during this send
8832
               */
8833
0
              sctp_move_chunks_from_net(stcb, net);
8834
0
            }
8835
0
            asconf = 0;
8836
0
            *reason_code = 7;
8837
0
            break;
8838
0
          } else {
8839
0
            asoc->ifp_had_enobuf = 0;
8840
0
          }
8841
          /*
8842
           * increase the number we sent, if a
8843
           * cookie is sent we don't tell them
8844
           * any was sent out.
8845
           */
8846
0
          outchain = endoutchain = NULL;
8847
0
          auth = NULL;
8848
0
          auth_offset = 0;
8849
0
          asconf = 0;
8850
0
          if (!no_out_cnt)
8851
0
            *num_out += ctl_cnt;
8852
          /* recalc a clean slate and setup */
8853
0
          switch (net->ro._l_addr.sa.sa_family) {
8854
0
#ifdef INET
8855
0
            case AF_INET:
8856
0
              mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8857
0
              break;
8858
0
#endif
8859
0
#ifdef INET6
8860
0
            case AF_INET6:
8861
0
              mtu = net->mtu - SCTP_MIN_OVERHEAD;
8862
0
              break;
8863
0
#endif
8864
0
#if defined(__Userspace__)
8865
0
            case AF_CONN:
8866
0
              mtu = net->mtu - sizeof(struct sctphdr);
8867
0
              break;
8868
0
#endif
8869
0
            default:
8870
              /* TSNH */
8871
0
              mtu = net->mtu;
8872
0
              break;
8873
0
          }
8874
0
          to_out = 0;
8875
0
          no_fragmentflg = 1;
8876
0
        }
8877
0
      }
8878
0
    }
8879
41.1k
    if (error != 0) {
8880
      /* try next net */
8881
0
      continue;
8882
0
    }
8883
    /************************/
8884
    /* Control transmission */
8885
    /************************/
8886
    /* Now first lets go through the control queue */
8887
223k
    TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8888
223k
      if ((sack_goes_to) &&
8889
0
          (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8890
0
          (chk->whoTo != sack_goes_to)) {
8891
        /*
8892
         * if we have a sack in queue, and we are looking at an
8893
         * ecn echo that is NOT queued to where the sack is going..
8894
         */
8895
0
        if (chk->whoTo == net) {
8896
          /* Don't transmit it to where its going (current net) */
8897
0
          continue;
8898
0
        } else if (sack_goes_to == net) {
8899
          /* But do transmit it to this address */
8900
0
          goto skip_net_check;
8901
0
        }
8902
0
      }
8903
223k
      if (chk->whoTo == NULL) {
8904
0
        if (asoc->alternate == NULL) {
8905
0
          if (asoc->primary_destination != net) {
8906
0
            continue;
8907
0
          }
8908
0
        } else {
8909
0
          if (asoc->alternate != net) {
8910
0
            continue;
8911
0
          }
8912
0
        }
8913
223k
      } else {
8914
223k
        if (chk->whoTo != net) {
8915
41.1k
          continue;
8916
41.1k
        }
8917
223k
      }
8918
181k
    skip_net_check:
8919
181k
      if (chk->data == NULL) {
8920
0
        continue;
8921
0
      }
8922
181k
      if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8923
        /*
8924
         * It must be unsent. Cookies and ASCONF's
8925
         * hang around but there timers will force
8926
         * when marked for resend.
8927
         */
8928
12.7k
        continue;
8929
12.7k
      }
8930
      /*
8931
       * if no AUTH is yet included and this chunk
8932
       * requires it, make sure to account for it.  We
8933
       * don't apply the size until the AUTH chunk is
8934
       * actually added below in case there is no room for
8935
       * this chunk. NOTE: we overload the use of "omtu"
8936
       * here
8937
       */
8938
169k
      if ((auth == NULL) &&
8939
167k
          sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8940
169k
              stcb->asoc.peer_auth_chunks)) {
8941
575
        omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8942
575
      } else
8943
168k
        omtu = 0;
8944
      /* Here we do NOT factor the r_mtu */
8945
169k
      if ((chk->send_size <= (int)(mtu - omtu)) ||
8946
144k
          (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8947
        /*
8948
         * We probably should glom the mbuf chain
8949
         * from the chk->data for control but the
8950
         * problem is it becomes yet one more level
8951
         * of tracking to do if for some reason
8952
         * output fails. Then I have got to
8953
         * reconstruct the merged control chain.. el
8954
         * yucko.. for now we take the easy way and
8955
         * do the copy
8956
         */
8957
        /*
8958
         * Add an AUTH chunk, if chunk requires it
8959
         * save the offset into the chain for AUTH
8960
         */
8961
144k
        if ((auth == NULL) &&
8962
143k
            (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8963
143k
                 stcb->asoc.peer_auth_chunks))) {
8964
574
          outchain = sctp_add_auth_chunk(outchain,
8965
574
                       &endoutchain,
8966
574
                       &auth,
8967
574
                       &auth_offset,
8968
574
                       stcb,
8969
574
                       chk->rec.chunk_id.id);
8970
574
          SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8971
574
        }
8972
144k
        outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8973
144k
                     (int)chk->rec.chunk_id.can_take_data,
8974
144k
                     chk->send_size, chk->copy_by_ref);
8975
144k
        if (outchain == NULL) {
8976
0
          *reason_code = 8;
8977
0
          SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8978
0
          return (ENOMEM);
8979
0
        }
8980
144k
        SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8981
        /* update our MTU size */
8982
144k
        if (mtu > (chk->send_size + omtu))
8983
143k
          mtu -= (chk->send_size + omtu);
8984
1.55k
        else
8985
1.55k
          mtu = 0;
8986
144k
        to_out += (chk->send_size + omtu);
8987
        /* Do clear IP_DF ? */
8988
144k
        if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8989
8.03k
          no_fragmentflg = 0;
8990
8.03k
        }
8991
144k
        if (chk->rec.chunk_id.can_take_data)
8992
44.8k
          chk->data = NULL;
8993
        /* Mark things to be removed, if needed */
8994
144k
        if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8995
141k
            (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
8996
140k
            (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8997
120k
            (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8998
102k
            (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8999
102k
            (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
9000
101k
            (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
9001
18.2k
            (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
9002
17.2k
            (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
9003
16.9k
            (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
9004
128k
            (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
9005
128k
          if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
9006
20.1k
            hbflag = 1;
9007
20.1k
          }
9008
          /* remove these chunks at the end */
9009
128k
          if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
9010
124k
              (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
9011
            /* turn off the timer */
9012
4.12k
            if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9013
0
              sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
9014
0
                              inp, stcb, NULL,
9015
0
                              SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
9016
0
            }
9017
4.12k
          }
9018
128k
          ctl_cnt++;
9019
128k
        } else {
9020
          /*
9021
           * Other chunks, since they have
9022
           * timers running (i.e. COOKIE)
9023
           * we just "trust" that it
9024
           * gets sent or retransmitted.
9025
           */
9026
16.4k
          ctl_cnt++;
9027
16.4k
          if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9028
7.48k
            cookie = 1;
9029
7.48k
            no_out_cnt = 1;
9030
8.92k
          } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
9031
            /*
9032
             * Increment ecne send count here
9033
             * this means we may be over-zealous in
9034
             * our counting if the send fails, but its
9035
             * the best place to do it (we used to do
9036
             * it in the queue of the chunk, but that did
9037
             * not tell how many times it was sent.
9038
             */
9039
0
            SCTP_STAT_INCR(sctps_sendecne);
9040
0
          }
9041
16.4k
          chk->sent = SCTP_DATAGRAM_SENT;
9042
16.4k
          if (chk->whoTo == NULL) {
9043
0
            chk->whoTo = net;
9044
0
            atomic_add_int(&net->ref_count, 1);
9045
0
          }
9046
16.4k
          chk->snd_count++;
9047
16.4k
        }
9048
144k
        if (mtu == 0) {
9049
          /*
9050
           * Ok we are out of room but we can
9051
           * output without effecting the
9052
           * flight size since this little guy
9053
           * is a control only packet.
9054
           */
9055
1.55k
          switch (asoc->snd_edmid) {
9056
0
          case SCTP_EDMID_LOWER_LAYER_DTLS:
9057
0
            use_zero_crc = true;
9058
0
            break;
9059
1.55k
          default:
9060
1.55k
            use_zero_crc = false;
9061
1.55k
            break;
9062
1.55k
          }
9063
1.55k
          if (asconf) {
9064
0
            sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
9065
0
            use_zero_crc = false;
9066
            /*
9067
             * do NOT clear the asconf
9068
             * flag as it is used to do
9069
             * appropriate source address
9070
             * selection.
9071
             */
9072
0
          }
9073
1.55k
          if (cookie) {
9074
10
            sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
9075
10
            use_zero_crc = false;
9076
10
            cookie = 0;
9077
10
          }
9078
          /* Only HB or ASCONF advances time */
9079
1.55k
          if (hbflag) {
9080
700
            if (*now_filled == 0) {
9081
34
              (void)SCTP_GETTIME_TIMEVAL(now);
9082
34
              *now_filled = 1;
9083
34
            }
9084
700
            net->last_sent_time = *now;
9085
700
            hbflag = 0;
9086
700
          }
9087
1.55k
          if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9088
1.55k
                                                  (struct sockaddr *)&net->ro._l_addr,
9089
1.55k
                                                  outchain,
9090
1.55k
                                                  auth_offset, auth,
9091
1.55k
                                                  stcb->asoc.authinfo.active_keyid,
9092
1.55k
                                                  no_fragmentflg, 0, asconf,
9093
1.55k
                                                  inp->sctp_lport, stcb->rport,
9094
1.55k
                                                  htonl(stcb->asoc.peer_vtag),
9095
1.55k
                                                  net->port, NULL,
9096
#if defined(__FreeBSD__) && !defined(__Userspace__)
9097
                                                  0, 0,
9098
#endif
9099
1.55k
                                                  use_zero_crc, so_locked))) {
9100
            /* error, we could not output */
9101
3
            SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9102
3
            if (from_where == 0) {
9103
0
              SCTP_STAT_INCR(sctps_lowlevelerrusr);
9104
0
            }
9105
3
            if (error == ENOBUFS) {
9106
0
              asoc->ifp_had_enobuf = 1;
9107
0
              SCTP_STAT_INCR(sctps_lowlevelerr);
9108
0
            }
9109
3
            if (error == EHOSTUNREACH) {
9110
              /*
9111
               * Destination went
9112
               * unreachable
9113
               * during this send
9114
               */
9115
2
              sctp_move_chunks_from_net(stcb, net);
9116
2
            }
9117
3
            asconf = 0;
9118
3
            *reason_code = 7;
9119
3
            break;
9120
1.55k
          } else {
9121
1.55k
            asoc->ifp_had_enobuf = 0;
9122
1.55k
          }
9123
          /*
9124
           * increase the number we sent, if a
9125
           * cookie is sent we don't tell them
9126
           * any was sent out.
9127
           */
9128
1.55k
          outchain = endoutchain = NULL;
9129
1.55k
          auth = NULL;
9130
1.55k
          auth_offset = 0;
9131
1.55k
          asconf = 0;
9132
1.55k
          if (!no_out_cnt)
9133
1.54k
            *num_out += ctl_cnt;
9134
          /* recalc a clean slate and setup */
9135
1.55k
          switch (net->ro._l_addr.sa.sa_family) {
9136
0
#ifdef INET
9137
0
            case AF_INET:
9138
0
              mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9139
0
              break;
9140
0
#endif
9141
0
#ifdef INET6
9142
0
            case AF_INET6:
9143
0
              mtu = net->mtu - SCTP_MIN_OVERHEAD;
9144
0
              break;
9145
0
#endif
9146
0
#if defined(__Userspace__)
9147
1.55k
            case AF_CONN:
9148
1.55k
              mtu = net->mtu - sizeof(struct sctphdr);
9149
1.55k
              break;
9150
0
#endif
9151
0
            default:
9152
              /* TSNH */
9153
0
              mtu = net->mtu;
9154
0
              break;
9155
1.55k
          }
9156
1.55k
          to_out = 0;
9157
1.55k
          no_fragmentflg = 1;
9158
1.55k
        }
9159
144k
      }
9160
169k
    }
9161
41.1k
    if (error != 0) {
9162
      /* try next net */
9163
3
      continue;
9164
3
    }
9165
    /* JRI: if dest is in PF state, do not send data to it */
9166
41.1k
    if ((asoc->sctp_cmt_on_off > 0) &&
9167
0
        (net != stcb->asoc.alternate) &&
9168
0
        (net->dest_state & SCTP_ADDR_PF)) {
9169
0
      goto no_data_fill;
9170
0
    }
9171
41.1k
    if (net->flight_size >= net->cwnd) {
9172
0
      goto no_data_fill;
9173
0
    }
9174
41.1k
    if ((asoc->sctp_cmt_on_off > 0) &&
9175
0
        (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
9176
0
        (net->flight_size > max_rwnd_per_dest)) {
9177
0
      goto no_data_fill;
9178
0
    }
9179
    /*
9180
     * We need a specific accounting for the usage of the
9181
     * send buffer. We also need to check the number of messages
9182
     * per net. For now, this is better than nothing and it
9183
     * disabled by default...
9184
     */
9185
41.1k
    if ((asoc->sctp_cmt_on_off > 0) &&
9186
0
        (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
9187
0
        (max_send_per_dest > 0) &&
9188
0
        (net->flight_size > max_send_per_dest)) {
9189
0
      goto no_data_fill;
9190
0
    }
9191
    /*********************/
9192
    /* Data transmission */
9193
    /*********************/
9194
    /*
9195
     * if AUTH for DATA is required and no AUTH has been added
9196
     * yet, account for this in the mtu now... if no data can be
9197
     * bundled, this adjustment won't matter anyways since the
9198
     * packet will be going out...
9199
     */
9200
41.1k
    data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
9201
41.1k
                   stcb->asoc.peer_auth_chunks);
9202
41.1k
    if (data_auth_reqd && (auth == NULL)) {
9203
71
      mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9204
71
    }
9205
    /* now lets add any data within the MTU constraints */
9206
41.1k
    switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
9207
0
#ifdef INET
9208
3.80k
    case AF_INET:
9209
3.80k
      if (net->mtu > SCTP_MIN_V4_OVERHEAD)
9210
3.80k
        omtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9211
0
      else
9212
0
        omtu = 0;
9213
3.80k
      break;
9214
0
#endif
9215
0
#ifdef INET6
9216
5.15k
    case AF_INET6:
9217
5.15k
      if (net->mtu > SCTP_MIN_OVERHEAD)
9218
5.15k
        omtu = net->mtu - SCTP_MIN_OVERHEAD;
9219
0
      else
9220
0
        omtu = 0;
9221
5.15k
      break;
9222
0
#endif
9223
0
#if defined(__Userspace__)
9224
32.1k
    case AF_CONN:
9225
32.1k
      if (net->mtu > sizeof(struct sctphdr)) {
9226
32.1k
        omtu = net->mtu - sizeof(struct sctphdr);
9227
32.1k
      } else {
9228
0
        omtu = 0;
9229
0
      }
9230
32.1k
      break;
9231
0
#endif
9232
0
    default:
9233
      /* TSNH */
9234
0
      omtu = 0;
9235
0
      break;
9236
41.1k
    }
9237
41.1k
    if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
9238
22.4k
          (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
9239
18.7k
         (skip_data_for_this_net == 0)) ||
9240
25.9k
        (cookie)) {
9241
22.6k
      TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
9242
8.76k
        if (no_data_chunks) {
9243
          /* let only control go out */
9244
0
          *reason_code = 1;
9245
0
          break;
9246
0
        }
9247
8.76k
        if (net->flight_size >= net->cwnd) {
9248
          /* skip this net, no room for data */
9249
0
          *reason_code = 2;
9250
0
          break;
9251
0
        }
9252
8.76k
        if ((chk->whoTo != NULL) &&
9253
0
            (chk->whoTo != net)) {
9254
          /* Don't send the chunk on this net */
9255
0
          continue;
9256
0
        }
9257
9258
8.76k
        if (asoc->sctp_cmt_on_off == 0) {
9259
8.76k
          if ((asoc->alternate) &&
9260
983
              (asoc->alternate != net) &&
9261
892
              (chk->whoTo == NULL)) {
9262
892
            continue;
9263
7.87k
          } else if ((net != asoc->primary_destination) &&
9264
601
               (asoc->alternate == NULL) &&
9265
510
               (chk->whoTo == NULL)) {
9266
510
            continue;
9267
510
          }
9268
8.76k
        }
9269
7.36k
        if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
9270
          /*-
9271
           * strange, we have a chunk that is
9272
           * to big for its destination and
9273
           * yet no fragment ok flag.
9274
           * Something went wrong when the
9275
           * PMTU changed...we did not mark
9276
           * this chunk for some reason?? I
9277
           * will fix it here by letting IP
9278
           * fragment it for now and printing
9279
           * a warning. This really should not
9280
           * happen ...
9281
           */
9282
0
          SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
9283
0
                chk->send_size, mtu);
9284
0
          chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
9285
0
        }
9286
7.36k
        if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
9287
7.36k
            (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
9288
0
          struct sctp_data_chunk *dchkh;
9289
9290
0
          dchkh = mtod(chk->data, struct sctp_data_chunk *);
9291
0
          dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
9292
0
        }
9293
7.36k
        if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
9294
7.34k
            ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
9295
          /* ok we will add this one */
9296
9297
          /*
9298
           * Add an AUTH chunk, if chunk
9299
           * requires it, save the offset into
9300
           * the chain for AUTH
9301
           */
9302
7.34k
          if (data_auth_reqd) {
9303
23
            if (auth == NULL) {
9304
20
              outchain = sctp_add_auth_chunk(outchain,
9305
20
                           &endoutchain,
9306
20
                           &auth,
9307
20
                           &auth_offset,
9308
20
                           stcb,
9309
20
                           SCTP_DATA);
9310
20
              auth_keyid = chk->auth_keyid;
9311
20
              override_ok = 0;
9312
20
              SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9313
20
            } else if (override_ok) {
9314
              /* use this data's keyid */
9315
3
              auth_keyid = chk->auth_keyid;
9316
3
              override_ok = 0;
9317
3
            } else if (auth_keyid != chk->auth_keyid) {
9318
              /* different keyid, so done bundling */
9319
0
              break;
9320
0
            }
9321
23
          }
9322
7.34k
          outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
9323
7.34k
                       chk->send_size, chk->copy_by_ref);
9324
7.34k
          if (outchain == NULL) {
9325
0
            SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
9326
0
            if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9327
0
              sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9328
0
            }
9329
0
            *reason_code = 3;
9330
0
            SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9331
0
            return (ENOMEM);
9332
0
          }
9333
          /* update our MTU size */
9334
          /* Do clear IP_DF ? */
9335
7.34k
          if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9336
0
            no_fragmentflg = 0;
9337
0
          }
9338
          /* unsigned subtraction of mtu */
9339
7.34k
          if (mtu > chk->send_size)
9340
2.80k
            mtu -= chk->send_size;
9341
4.54k
          else
9342
4.54k
            mtu = 0;
9343
          /* unsigned subtraction of r_mtu */
9344
7.34k
          if (r_mtu > chk->send_size)
9345
2.80k
            r_mtu -= chk->send_size;
9346
4.54k
          else
9347
4.54k
            r_mtu = 0;
9348
9349
7.34k
          to_out += chk->send_size;
9350
7.34k
          if ((to_out > mx_mtu) && no_fragmentflg) {
9351
0
#ifdef INVARIANTS
9352
0
            panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
9353
#else
9354
            SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
9355
                  mx_mtu, to_out);
9356
#endif
9357
0
          }
9358
7.34k
          chk->window_probe = 0;
9359
7.34k
          data_list[bundle_at++] = chk;
9360
7.34k
          if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9361
0
            break;
9362
0
          }
9363
7.34k
          if (chk->sent == SCTP_DATAGRAM_UNSENT) {
9364
7.34k
            if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
9365
7.34k
              SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
9366
7.34k
            } else {
9367
0
              SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
9368
0
            }
9369
7.34k
            if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
9370
2.80k
                ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
9371
              /* Count number of user msg's that were fragmented
9372
               * we do this by counting when we see a LAST fragment
9373
               * only.
9374
               */
9375
7.34k
              SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
9376
7.34k
          }
9377
7.34k
          if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
9378
4.54k
            if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
9379
0
              data_list[0]->window_probe = 1;
9380
0
              net->window_probe = 1;
9381
0
            }
9382
4.54k
            break;
9383
4.54k
          }
9384
7.34k
        } else {
9385
          /*
9386
           * Must be sent in order of the
9387
           * TSN's (on a network)
9388
           */
9389
15
          break;
9390
15
        }
9391
7.36k
      }  /* for (chunk gather loop for this net) */
9392
22.6k
    }    /* if asoc.state OPEN */
9393
41.1k
  no_data_fill:
9394
    /* Is there something to send for this destination? */
9395
41.1k
    if (outchain) {
9396
23.3k
      switch (asoc->snd_edmid) {
9397
0
      case SCTP_EDMID_LOWER_LAYER_DTLS:
9398
0
        use_zero_crc = true;
9399
0
        break;
9400
23.3k
      default:
9401
23.3k
        use_zero_crc = false;
9402
23.3k
        break;
9403
23.3k
      }
9404
      /* We may need to start a control timer or two */
9405
23.3k
      if (asconf) {
9406
0
        sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
9407
0
             stcb, net);
9408
0
        use_zero_crc = false;
9409
        /*
9410
         * do NOT clear the asconf flag as it is used
9411
         * to do appropriate source address selection.
9412
         */
9413
0
      }
9414
23.3k
      if (cookie) {
9415
7.47k
        sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
9416
7.47k
        use_zero_crc = false;
9417
7.47k
        cookie = 0;
9418
7.47k
      }
9419
      /* must start a send timer if data is being sent */
9420
23.3k
      if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
9421
        /*
9422
         * no timer running on this destination
9423
         * restart it.
9424
         */
9425
3.59k
        sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9426
3.59k
      }
9427
23.3k
      if (bundle_at || hbflag) {
9428
        /* For data/asconf and hb set time */
9429
9.04k
        if (*now_filled == 0) {
9430
4.76k
          (void)SCTP_GETTIME_TIMEVAL(now);
9431
4.76k
          *now_filled = 1;
9432
4.76k
        }
9433
9.04k
        net->last_sent_time = *now;
9434
9.04k
      }
9435
      /* Now send it, if there is anything to send :> */
9436
23.3k
      if ((error = sctp_lowlevel_chunk_output(inp,
9437
23.3k
                                              stcb,
9438
23.3k
                                              net,
9439
23.3k
                                              (struct sockaddr *)&net->ro._l_addr,
9440
23.3k
                                              outchain,
9441
23.3k
                                              auth_offset,
9442
23.3k
                                              auth,
9443
23.3k
                                              auth_keyid,
9444
23.3k
                                              no_fragmentflg,
9445
23.3k
                                              bundle_at,
9446
23.3k
                                              asconf,
9447
23.3k
                                              inp->sctp_lport, stcb->rport,
9448
23.3k
                                              htonl(stcb->asoc.peer_vtag),
9449
23.3k
                                              net->port, NULL,
9450
#if defined(__FreeBSD__) && !defined(__Userspace__)
9451
                                              0, 0,
9452
#endif
9453
23.3k
                                              use_zero_crc,
9454
23.3k
                                              so_locked))) {
9455
        /* error, we could not output */
9456
1.83k
        SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9457
1.83k
        if (from_where == 0) {
9458
0
          SCTP_STAT_INCR(sctps_lowlevelerrusr);
9459
0
        }
9460
1.83k
        if (error == ENOBUFS) {
9461
0
          asoc->ifp_had_enobuf = 1;
9462
0
          SCTP_STAT_INCR(sctps_lowlevelerr);
9463
0
        }
9464
1.83k
        if (error == EHOSTUNREACH) {
9465
          /*
9466
           * Destination went unreachable
9467
           * during this send
9468
           */
9469
1.41k
          sctp_move_chunks_from_net(stcb, net);
9470
1.41k
        }
9471
1.83k
        asconf = 0;
9472
1.83k
        *reason_code = 6;
9473
        /*-
9474
         * I add this line to be paranoid. As far as
9475
         * I can tell the continue, takes us back to
9476
         * the top of the for, but just to make sure
9477
         * I will reset these again here.
9478
         */
9479
1.83k
        ctl_cnt = 0;
9480
1.83k
        continue; /* This takes us back to the for() for the nets. */
9481
21.5k
      } else {
9482
21.5k
        asoc->ifp_had_enobuf = 0;
9483
21.5k
      }
9484
21.5k
      endoutchain = NULL;
9485
21.5k
      auth = NULL;
9486
21.5k
      auth_offset = 0;
9487
21.5k
      asconf = 0;
9488
21.5k
      if (!no_out_cnt) {
9489
14.0k
        *num_out += (ctl_cnt + bundle_at);
9490
14.0k
      }
9491
21.5k
      if (bundle_at) {
9492
        /* setup for a RTO measurement */
9493
7.09k
        tsns_sent = data_list[0]->rec.data.tsn;
9494
        /* fill time if not already filled */
9495
7.09k
        if (*now_filled == 0) {
9496
0
          (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9497
0
          *now_filled = 1;
9498
0
          *now = asoc->time_last_sent;
9499
7.09k
        } else {
9500
7.09k
          asoc->time_last_sent = *now;
9501
7.09k
        }
9502
7.09k
        if (net->rto_needed) {
9503
3.83k
          data_list[0]->do_rtt = 1;
9504
3.83k
          net->rto_needed = 0;
9505
3.83k
        }
9506
7.09k
        SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
9507
7.09k
        sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
9508
7.09k
      }
9509
21.5k
      if (one_chunk) {
9510
78
        break;
9511
78
      }
9512
21.5k
    }
9513
39.2k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9514
0
      sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
9515
0
    }
9516
39.2k
  }
9517
64.3k
  if (old_start_at == NULL) {
9518
32.1k
    old_start_at = start_at;
9519
32.1k
    start_at = TAILQ_FIRST(&asoc->nets);
9520
32.1k
    if (old_start_at)
9521
32.1k
      goto again_one_more_time;
9522
32.1k
  }
9523
9524
  /*
9525
   * At the end there should be no NON timed chunks hanging on this
9526
   * queue.
9527
   */
9528
32.1k
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9529
0
    sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
9530
0
  }
9531
32.1k
  if ((*num_out == 0) && (*reason_code == 0)) {
9532
17.6k
    *reason_code = 4;
9533
17.6k
  } else {
9534
14.5k
    *reason_code = 5;
9535
14.5k
  }
9536
32.1k
  sctp_clean_up_ctl(stcb, asoc, so_locked);
9537
32.1k
  return (0);
9538
64.3k
}
9539
9540
void
9541
sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
9542
123k
{
9543
  /*-
9544
   * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
9545
   * the control chunk queue.
9546
   */
9547
123k
  struct sctp_chunkhdr *hdr;
9548
123k
  struct sctp_tmit_chunk *chk;
9549
123k
  struct mbuf *mat, *last_mbuf;
9550
123k
  uint32_t chunk_length;
9551
123k
  uint16_t padding_length;
9552
9553
123k
  SCTP_TCB_LOCK_ASSERT(stcb);
9554
123k
  SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT);
9555
123k
  if (op_err == NULL) {
9556
0
    return;
9557
0
  }
9558
123k
  last_mbuf = NULL;
9559
123k
  chunk_length = 0;
9560
499k
  for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) {
9561
376k
    chunk_length += SCTP_BUF_LEN(mat);
9562
376k
    if (SCTP_BUF_NEXT(mat) == NULL) {
9563
123k
      last_mbuf = mat;
9564
123k
    }
9565
376k
  }
9566
123k
  if (chunk_length > SCTP_MAX_CHUNK_LENGTH) {
9567
0
    sctp_m_freem(op_err);
9568
0
    return;
9569
0
  }
9570
123k
  padding_length = chunk_length % 4;
9571
123k
  if (padding_length != 0) {
9572
523
    padding_length = 4 - padding_length;
9573
523
  }
9574
123k
  if (padding_length != 0) {
9575
523
    if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) {
9576
0
      sctp_m_freem(op_err);
9577
0
      return;
9578
0
    }
9579
523
  }
9580
123k
  sctp_alloc_a_chunk(stcb, chk);
9581
123k
  if (chk == NULL) {
9582
    /* no memory */
9583
0
    sctp_m_freem(op_err);
9584
0
    return;
9585
0
  }
9586
123k
  chk->copy_by_ref = 0;
9587
123k
  chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
9588
123k
  chk->rec.chunk_id.can_take_data = 0;
9589
123k
  chk->flags = 0;
9590
123k
  chk->send_size = (uint16_t)chunk_length;
9591
123k
  chk->sent = SCTP_DATAGRAM_UNSENT;
9592
123k
  chk->snd_count = 0;
9593
123k
  chk->asoc = &stcb->asoc;
9594
123k
  chk->data = op_err;
9595
123k
  chk->whoTo = NULL;
9596
123k
  hdr = mtod(op_err, struct sctp_chunkhdr *);
9597
123k
  hdr->chunk_type = SCTP_OPERATION_ERROR;
9598
123k
  hdr->chunk_flags = 0;
9599
123k
  hdr->chunk_length = htons(chk->send_size);
9600
123k
  TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9601
123k
  chk->asoc->ctrl_queue_cnt++;
9602
123k
}
9603
9604
int
9605
sctp_send_cookie_echo(struct mbuf *m,
9606
    int offset, int limit,
9607
    struct sctp_tcb *stcb,
9608
    struct sctp_nets *net)
9609
7.48k
{
9610
  /*-
9611
   * pull out the cookie and put it at the front of the control chunk
9612
   * queue.
9613
   */
9614
7.48k
  int at;
9615
7.48k
  struct mbuf *cookie;
9616
7.48k
  struct sctp_paramhdr param, *phdr;
9617
7.48k
  struct sctp_chunkhdr *hdr;
9618
7.48k
  struct sctp_tmit_chunk *chk;
9619
7.48k
  uint16_t ptype, plen;
9620
9621
7.48k
  SCTP_TCB_LOCK_ASSERT(stcb);
9622
  /* First find the cookie in the param area */
9623
7.48k
  cookie = NULL;
9624
7.48k
  at = offset + sizeof(struct sctp_init_chunk);
9625
59.8k
  for (;;) {
9626
59.8k
    phdr = sctp_get_next_param(m, at, &param, sizeof(param));
9627
59.8k
    if (phdr == NULL) {
9628
0
      return (-3);
9629
0
    }
9630
59.8k
    ptype = ntohs(phdr->param_type);
9631
59.8k
    plen = ntohs(phdr->param_length);
9632
59.8k
    if (plen < sizeof(struct sctp_paramhdr)) {
9633
0
      return (-6);
9634
0
    }
9635
59.8k
    if (ptype == SCTP_STATE_COOKIE) {
9636
7.48k
      int pad;
9637
9638
      /* found the cookie */
9639
7.48k
      if (at + plen > limit) {
9640
0
        return (-7);
9641
0
      }
9642
7.48k
      cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT);
9643
7.48k
      if (cookie == NULL) {
9644
        /* No memory */
9645
0
        return (-2);
9646
0
      }
9647
7.48k
      if ((pad = (plen % 4)) > 0) {
9648
57
        pad = 4 - pad;
9649
57
      }
9650
7.48k
      if (pad > 0) {
9651
57
        if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) {
9652
0
          return (-8);
9653
0
        }
9654
57
      }
9655
#ifdef SCTP_MBUF_LOGGING
9656
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9657
        sctp_log_mbc(cookie, SCTP_MBUF_ICOPY);
9658
      }
9659
#endif
9660
7.48k
      break;
9661
7.48k
    }
9662
52.3k
    at += SCTP_SIZE32(plen);
9663
52.3k
  }
9664
  /* ok, we got the cookie lets change it into a cookie echo chunk */
9665
  /* first the change from param to cookie */
9666
7.48k
  hdr = mtod(cookie, struct sctp_chunkhdr *);
9667
7.48k
  hdr->chunk_type = SCTP_COOKIE_ECHO;
9668
7.48k
  hdr->chunk_flags = 0;
9669
  /* get the chunk stuff now and place it in the FRONT of the queue */
9670
7.48k
  sctp_alloc_a_chunk(stcb, chk);
9671
7.48k
  if (chk == NULL) {
9672
    /* no memory */
9673
0
    sctp_m_freem(cookie);
9674
0
    return (-5);
9675
0
  }
9676
7.48k
  chk->copy_by_ref = 0;
9677
7.48k
  chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
9678
7.48k
  chk->rec.chunk_id.can_take_data = 0;
9679
7.48k
  chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9680
7.48k
  chk->send_size = SCTP_SIZE32(plen);
9681
7.48k
  chk->sent = SCTP_DATAGRAM_UNSENT;
9682
7.48k
  chk->snd_count = 0;
9683
7.48k
  chk->asoc = &stcb->asoc;
9684
7.48k
  chk->data = cookie;
9685
7.48k
  chk->whoTo = net;
9686
7.48k
  atomic_add_int(&chk->whoTo->ref_count, 1);
9687
7.48k
  TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
9688
7.48k
  chk->asoc->ctrl_queue_cnt++;
9689
7.48k
  return (0);
9690
7.48k
}
9691
9692
void
9693
sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
9694
    struct mbuf *m,
9695
    int offset,
9696
    int chk_length,
9697
    struct sctp_nets *net)
9698
20.3k
{
9699
  /*
9700
   * take a HB request and make it into a HB ack and send it.
9701
   */
9702
20.3k
  struct mbuf *outchain;
9703
20.3k
  struct sctp_chunkhdr *chdr;
9704
20.3k
  struct sctp_tmit_chunk *chk;
9705
9706
20.3k
  if (net == NULL)
9707
    /* must have a net pointer */
9708
0
    return;
9709
9710
20.3k
  outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT);
9711
20.3k
  if (outchain == NULL) {
9712
    /* gak out of memory */
9713
0
    return;
9714
0
  }
9715
#ifdef SCTP_MBUF_LOGGING
9716
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9717
    sctp_log_mbc(outchain, SCTP_MBUF_ICOPY);
9718
  }
9719
#endif
9720
20.3k
  chdr = mtod(outchain, struct sctp_chunkhdr *);
9721
20.3k
  chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9722
20.3k
  chdr->chunk_flags = 0;
9723
20.3k
  if (chk_length % 4 != 0) {
9724
1.86k
    sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL);
9725
1.86k
  }
9726
20.3k
  sctp_alloc_a_chunk(stcb, chk);
9727
20.3k
  if (chk == NULL) {
9728
    /* no memory */
9729
0
    sctp_m_freem(outchain);
9730
0
    return;
9731
0
  }
9732
20.3k
  chk->copy_by_ref = 0;
9733
20.3k
  chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9734
20.3k
  chk->rec.chunk_id.can_take_data = 1;
9735
20.3k
  chk->flags = 0;
9736
20.3k
  chk->send_size = chk_length;
9737
20.3k
  chk->sent = SCTP_DATAGRAM_UNSENT;
9738
20.3k
  chk->snd_count = 0;
9739
20.3k
  chk->asoc = &stcb->asoc;
9740
20.3k
  chk->data = outchain;
9741
20.3k
  chk->whoTo = net;
9742
20.3k
  atomic_add_int(&chk->whoTo->ref_count, 1);
9743
20.3k
  TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9744
20.3k
  chk->asoc->ctrl_queue_cnt++;
9745
20.3k
}
9746
9747
void
9748
sctp_send_cookie_ack(struct sctp_tcb *stcb)
9749
1.02k
{
9750
  /* formulate and queue a cookie-ack back to sender */
9751
1.02k
  struct mbuf *cookie_ack;
9752
1.02k
  struct sctp_chunkhdr *hdr;
9753
1.02k
  struct sctp_tmit_chunk *chk;
9754
9755
1.02k
  SCTP_TCB_LOCK_ASSERT(stcb);
9756
9757
1.02k
  cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
9758
1.02k
  if (cookie_ack == NULL) {
9759
    /* no mbuf's */
9760
0
    return;
9761
0
  }
9762
1.02k
  SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9763
1.02k
  sctp_alloc_a_chunk(stcb, chk);
9764
1.02k
  if (chk == NULL) {
9765
    /* no memory */
9766
0
    sctp_m_freem(cookie_ack);
9767
0
    return;
9768
0
  }
9769
1.02k
  chk->copy_by_ref = 0;
9770
1.02k
  chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9771
1.02k
  chk->rec.chunk_id.can_take_data = 1;
9772
1.02k
  chk->flags = 0;
9773
1.02k
  chk->send_size = sizeof(struct sctp_chunkhdr);
9774
1.02k
  chk->sent = SCTP_DATAGRAM_UNSENT;
9775
1.02k
  chk->snd_count = 0;
9776
1.02k
  chk->asoc = &stcb->asoc;
9777
1.02k
  chk->data = cookie_ack;
9778
1.02k
  if (chk->asoc->last_control_chunk_from != NULL) {
9779
1.02k
    chk->whoTo = chk->asoc->last_control_chunk_from;
9780
1.02k
    atomic_add_int(&chk->whoTo->ref_count, 1);
9781
1.02k
  } else {
9782
0
    chk->whoTo = NULL;
9783
0
  }
9784
1.02k
  hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9785
1.02k
  hdr->chunk_type = SCTP_COOKIE_ACK;
9786
1.02k
  hdr->chunk_flags = 0;
9787
1.02k
  hdr->chunk_length = htons(chk->send_size);
9788
1.02k
  SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9789
1.02k
  TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9790
1.02k
  chk->asoc->ctrl_queue_cnt++;
9791
1.02k
  return;
9792
1.02k
}
9793
9794
void
9795
sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9796
1.45k
{
9797
  /* formulate and queue a SHUTDOWN-ACK back to the sender */
9798
1.45k
  struct mbuf *m_shutdown_ack;
9799
1.45k
  struct sctp_shutdown_ack_chunk *ack_cp;
9800
1.45k
  struct sctp_tmit_chunk *chk;
9801
9802
1.45k
  m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9803
1.45k
  if (m_shutdown_ack == NULL) {
9804
    /* no mbuf's */
9805
0
    return;
9806
0
  }
9807
1.45k
  SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9808
1.45k
  sctp_alloc_a_chunk(stcb, chk);
9809
1.45k
  if (chk == NULL) {
9810
    /* no memory */
9811
0
    sctp_m_freem(m_shutdown_ack);
9812
0
    return;
9813
0
  }
9814
1.45k
  chk->copy_by_ref = 0;
9815
1.45k
  chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9816
1.45k
  chk->rec.chunk_id.can_take_data = 1;
9817
1.45k
  chk->flags = 0;
9818
1.45k
  chk->send_size = sizeof(struct sctp_chunkhdr);
9819
1.45k
  chk->sent = SCTP_DATAGRAM_UNSENT;
9820
1.45k
  chk->snd_count = 0;
9821
1.45k
  chk->asoc = &stcb->asoc;
9822
1.45k
  chk->data = m_shutdown_ack;
9823
1.45k
  chk->whoTo = net;
9824
1.45k
  if (chk->whoTo) {
9825
1.45k
    atomic_add_int(&chk->whoTo->ref_count, 1);
9826
1.45k
  }
9827
1.45k
  ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9828
1.45k
  ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9829
1.45k
  ack_cp->ch.chunk_flags = 0;
9830
1.45k
  ack_cp->ch.chunk_length = htons(chk->send_size);
9831
1.45k
  SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9832
1.45k
  TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9833
1.45k
  chk->asoc->ctrl_queue_cnt++;
9834
1.45k
  return;
9835
1.45k
}
9836
9837
void
9838
sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9839
2.64k
{
9840
  /* formulate and queue a SHUTDOWN to the sender */
9841
2.64k
  struct mbuf *m_shutdown;
9842
2.64k
  struct sctp_shutdown_chunk *shutdown_cp;
9843
2.64k
  struct sctp_tmit_chunk *chk;
9844
9845
715k
  TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
9846
715k
    if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) {
9847
      /* We already have a SHUTDOWN queued. Reuse it. */
9848
2.58k
      if (chk->whoTo) {
9849
2.58k
        sctp_free_remote_addr(chk->whoTo);
9850
2.58k
        chk->whoTo = NULL;
9851
2.58k
      }
9852
2.58k
      break;
9853
2.58k
    }
9854
715k
  }
9855
2.64k
  if (chk == NULL) {
9856
61
    m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9857
61
    if (m_shutdown == NULL) {
9858
      /* no mbuf's */
9859
0
      return;
9860
0
    }
9861
61
    SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9862
61
    sctp_alloc_a_chunk(stcb, chk);
9863
61
    if (chk == NULL) {
9864
      /* no memory */
9865
0
      sctp_m_freem(m_shutdown);
9866
0
      return;
9867
0
    }
9868
61
    chk->copy_by_ref = 0;
9869
61
    chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9870
61
    chk->rec.chunk_id.can_take_data = 1;
9871
61
    chk->flags = 0;
9872
61
    chk->send_size = sizeof(struct sctp_shutdown_chunk);
9873
61
    chk->sent = SCTP_DATAGRAM_UNSENT;
9874
61
    chk->snd_count = 0;
9875
61
    chk->asoc = &stcb->asoc;
9876
61
    chk->data = m_shutdown;
9877
61
    chk->whoTo = net;
9878
61
    if (chk->whoTo) {
9879
61
      atomic_add_int(&chk->whoTo->ref_count, 1);
9880
61
    }
9881
61
    shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9882
61
    shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9883
61
    shutdown_cp->ch.chunk_flags = 0;
9884
61
    shutdown_cp->ch.chunk_length = htons(chk->send_size);
9885
61
    shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9886
61
    SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9887
61
    TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9888
61
    chk->asoc->ctrl_queue_cnt++;
9889
2.58k
  } else {
9890
2.58k
    TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next);
9891
2.58k
    chk->whoTo = net;
9892
2.58k
    if (chk->whoTo) {
9893
2.58k
      atomic_add_int(&chk->whoTo->ref_count, 1);
9894
2.58k
    }
9895
2.58k
    shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *);
9896
2.58k
    shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9897
2.58k
    TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9898
2.58k
  }
9899
2.64k
  return;
9900
2.64k
}
9901
9902
void
9903
sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9904
0
{
9905
  /*
9906
   * formulate and queue an ASCONF to the peer.
9907
   * ASCONF parameters should be queued on the assoc queue.
9908
   */
9909
0
  struct sctp_tmit_chunk *chk;
9910
0
  struct mbuf *m_asconf;
9911
0
  int len;
9912
9913
0
  SCTP_TCB_LOCK_ASSERT(stcb);
9914
9915
0
  if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9916
0
      (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9917
    /* can't send a new one if there is one in flight already */
9918
0
    return;
9919
0
  }
9920
9921
  /* compose an ASCONF chunk, maximum length is PMTU */
9922
0
  m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9923
0
  if (m_asconf == NULL) {
9924
0
    return;
9925
0
  }
9926
9927
0
  sctp_alloc_a_chunk(stcb, chk);
9928
0
  if (chk == NULL) {
9929
    /* no memory */
9930
0
    sctp_m_freem(m_asconf);
9931
0
    return;
9932
0
  }
9933
9934
0
  chk->copy_by_ref = 0;
9935
0
  chk->rec.chunk_id.id = SCTP_ASCONF;
9936
0
  chk->rec.chunk_id.can_take_data = 0;
9937
0
  chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9938
0
  chk->data = m_asconf;
9939
0
  chk->send_size = len;
9940
0
  chk->sent = SCTP_DATAGRAM_UNSENT;
9941
0
  chk->snd_count = 0;
9942
0
  chk->asoc = &stcb->asoc;
9943
0
  chk->whoTo = net;
9944
0
  if (chk->whoTo) {
9945
0
    atomic_add_int(&chk->whoTo->ref_count, 1);
9946
0
  }
9947
0
  TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9948
0
  chk->asoc->ctrl_queue_cnt++;
9949
0
  return;
9950
0
}
9951
9952
void
9953
sctp_send_asconf_ack(struct sctp_tcb *stcb)
9954
1.37k
{
9955
  /*
9956
   * formulate and queue a asconf-ack back to sender.
9957
   * the asconf-ack must be stored in the tcb.
9958
   */
9959
1.37k
  struct sctp_tmit_chunk *chk;
9960
1.37k
  struct sctp_asconf_ack *ack, *latest_ack;
9961
1.37k
  struct mbuf *m_ack;
9962
1.37k
  struct sctp_nets *net = NULL;
9963
9964
1.37k
  SCTP_TCB_LOCK_ASSERT(stcb);
9965
  /* Get the latest ASCONF-ACK */
9966
1.37k
  latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9967
1.37k
  if (latest_ack == NULL) {
9968
812
    return;
9969
812
  }
9970
558
  if (latest_ack->last_sent_to != NULL &&
9971
0
      latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9972
    /* we're doing a retransmission */
9973
0
    net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9974
0
    if (net == NULL) {
9975
      /* no alternate */
9976
0
      if (stcb->asoc.last_control_chunk_from == NULL) {
9977
0
        if (stcb->asoc.alternate) {
9978
0
          net = stcb->asoc.alternate;
9979
0
        } else {
9980
0
          net = stcb->asoc.primary_destination;
9981
0
        }
9982
0
      } else {
9983
0
        net = stcb->asoc.last_control_chunk_from;
9984
0
      }
9985
0
    }
9986
558
  } else {
9987
    /* normal case */
9988
558
    if (stcb->asoc.last_control_chunk_from == NULL) {
9989
0
      if (stcb->asoc.alternate) {
9990
0
        net = stcb->asoc.alternate;
9991
0
      } else {
9992
0
        net = stcb->asoc.primary_destination;
9993
0
      }
9994
558
    } else {
9995
558
      net = stcb->asoc.last_control_chunk_from;
9996
558
    }
9997
558
  }
9998
558
  latest_ack->last_sent_to = net;
9999
10000
578
  TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
10001
578
    if (ack->data == NULL) {
10002
0
      continue;
10003
0
    }
10004
10005
    /* copy the asconf_ack */
10006
578
    m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT);
10007
578
    if (m_ack == NULL) {
10008
      /* couldn't copy it */
10009
0
      return;
10010
0
    }
10011
#ifdef SCTP_MBUF_LOGGING
10012
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
10013
      sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY);
10014
    }
10015
#endif
10016
10017
578
    sctp_alloc_a_chunk(stcb, chk);
10018
578
    if (chk == NULL) {
10019
      /* no memory */
10020
0
      if (m_ack)
10021
0
        sctp_m_freem(m_ack);
10022
0
      return;
10023
0
    }
10024
578
    chk->copy_by_ref = 0;
10025
578
    chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
10026
578
    chk->rec.chunk_id.can_take_data = 1;
10027
578
    chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
10028
578
    chk->whoTo = net;
10029
578
    if (chk->whoTo) {
10030
578
      atomic_add_int(&chk->whoTo->ref_count, 1);
10031
578
    }
10032
578
    chk->data = m_ack;
10033
578
    chk->send_size = ack->len;
10034
578
    chk->sent = SCTP_DATAGRAM_UNSENT;
10035
578
    chk->snd_count = 0;
10036
578
    chk->asoc = &stcb->asoc;
10037
10038
578
    TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
10039
578
    chk->asoc->ctrl_queue_cnt++;
10040
578
  }
10041
558
  return;
10042
558
}
10043
10044
static int
10045
sctp_chunk_retransmission(struct sctp_inpcb *inp,
10046
    struct sctp_tcb *stcb,
10047
    struct sctp_association *asoc,
10048
    int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked)
10049
0
{
10050
  /*-
10051
   * send out one MTU of retransmission. If fast_retransmit is
10052
   * happening we ignore the cwnd. Otherwise we obey the cwnd and
10053
   * rwnd. For a Cookie or Asconf in the control chunk queue we
10054
   * retransmit them by themselves.
10055
   *
10056
   * For data chunks we will pick out the lowest TSN's in the sent_queue
10057
   * marked for resend and bundle them all together (up to a MTU of
10058
   * destination). The address to send to should have been
10059
   * selected/changed where the retransmission was marked (i.e. in FR
10060
   * or t3-timeout routines).
10061
   */
10062
0
  struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
10063
0
  struct sctp_tmit_chunk *chk, *fwd;
10064
0
  struct mbuf *m, *endofchain;
10065
0
  struct sctp_nets *net = NULL;
10066
0
  uint32_t tsns_sent = 0;
10067
0
  int no_fragmentflg, bundle_at;
10068
0
  unsigned int mtu;
10069
0
  int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
10070
0
  struct sctp_auth_chunk *auth = NULL;
10071
0
  uint32_t auth_offset = 0;
10072
0
  uint16_t auth_keyid;
10073
0
  int override_ok = 1;
10074
0
  int data_auth_reqd = 0;
10075
0
  uint32_t dmtu = 0;
10076
0
  bool use_zero_crc;
10077
10078
#if defined(__APPLE__) && !defined(__Userspace__)
10079
  if (so_locked) {
10080
    sctp_lock_assert(SCTP_INP_SO(inp));
10081
  } else {
10082
    sctp_unlock_assert(SCTP_INP_SO(inp));
10083
  }
10084
#endif
10085
0
  SCTP_TCB_LOCK_ASSERT(stcb);
10086
0
  tmr_started = ctl_cnt = 0;
10087
0
  no_fragmentflg = 1;
10088
0
  fwd_tsn = 0;
10089
0
  *cnt_out = 0;
10090
0
  fwd = NULL;
10091
0
  endofchain = m = NULL;
10092
0
  auth_keyid = stcb->asoc.authinfo.active_keyid;
10093
#ifdef SCTP_AUDITING_ENABLED
10094
  sctp_audit_log(0xC3, 1);
10095
#endif
10096
0
  if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
10097
0
      (TAILQ_EMPTY(&asoc->control_send_queue))) {
10098
0
    SCTPDBG(SCTP_DEBUG_OUTPUT1,"SCTP hits empty queue with cnt set to %d?\n",
10099
0
      asoc->sent_queue_retran_cnt);
10100
0
    asoc->sent_queue_cnt = 0;
10101
0
    asoc->sent_queue_cnt_removeable = 0;
10102
    /* send back 0/0 so we enter normal transmission */
10103
0
    *cnt_out = 0;
10104
0
    return (0);
10105
0
  }
10106
0
  TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10107
0
    if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
10108
0
        (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
10109
0
        (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
10110
0
      if (chk->sent != SCTP_DATAGRAM_RESEND) {
10111
0
        continue;
10112
0
      }
10113
0
      if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
10114
0
        if (chk != asoc->str_reset) {
10115
          /*
10116
           * not eligible for retran if its
10117
           * not ours
10118
           */
10119
0
          continue;
10120
0
        }
10121
0
      }
10122
0
      ctl_cnt++;
10123
0
      if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10124
0
        fwd_tsn = 1;
10125
0
      }
10126
      /*
10127
       * Add an AUTH chunk, if chunk requires it save the
10128
       * offset into the chain for AUTH
10129
       */
10130
0
      if ((auth == NULL) &&
10131
0
          (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
10132
0
               stcb->asoc.peer_auth_chunks))) {
10133
0
        m = sctp_add_auth_chunk(m, &endofchain,
10134
0
              &auth, &auth_offset,
10135
0
              stcb,
10136
0
              chk->rec.chunk_id.id);
10137
0
        SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10138
0
      }
10139
0
      m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10140
0
      break;
10141
0
    }
10142
0
  }
10143
0
  one_chunk = 0;
10144
  /* do we have control chunks to retransmit? */
10145
0
  if (m != NULL) {
10146
    /* Start a timer no matter if we succeed or fail */
10147
0
    switch (asoc->snd_edmid) {
10148
0
    case SCTP_EDMID_LOWER_LAYER_DTLS:
10149
0
      use_zero_crc = true;
10150
0
      break;
10151
0
    default:
10152
0
      use_zero_crc = false;
10153
0
      break;
10154
0
    }
10155
0
    if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
10156
0
      sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
10157
0
      use_zero_crc = false;
10158
0
    } else if (chk->rec.chunk_id.id == SCTP_ASCONF) {
10159
      /* XXXMT: Can this happen? */
10160
0
      sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
10161
0
      use_zero_crc = false;
10162
0
    }
10163
0
    chk->snd_count++; /* update our count */
10164
0
    if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
10165
0
                                            (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
10166
0
                                            auth_offset, auth, stcb->asoc.authinfo.active_keyid,
10167
0
                                            no_fragmentflg, 0, 0,
10168
0
                                            inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10169
0
                                            chk->whoTo->port, NULL,
10170
#if defined(__FreeBSD__) && !defined(__Userspace__)
10171
                                            0, 0,
10172
#endif
10173
0
                                            use_zero_crc,
10174
0
                                            so_locked))) {
10175
0
      SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10176
0
      if (error == ENOBUFS) {
10177
0
        asoc->ifp_had_enobuf = 1;
10178
0
        SCTP_STAT_INCR(sctps_lowlevelerr);
10179
0
      }
10180
0
      return (error);
10181
0
    } else {
10182
0
      asoc->ifp_had_enobuf = 0;
10183
0
    }
10184
0
    endofchain = NULL;
10185
0
    auth = NULL;
10186
0
    auth_offset = 0;
10187
    /*
10188
     * We don't want to mark the net->sent time here since this
10189
     * we use this for HB and retrans cannot measure RTT
10190
     */
10191
    /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
10192
0
    *cnt_out += 1;
10193
0
    chk->sent = SCTP_DATAGRAM_SENT;
10194
0
    sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
10195
0
    if (fwd_tsn == 0) {
10196
0
      return (0);
10197
0
    } else {
10198
      /* Clean up the fwd-tsn list */
10199
0
      sctp_clean_up_ctl(stcb, asoc, so_locked);
10200
0
      return (0);
10201
0
    }
10202
0
  }
10203
  /*
10204
   * Ok, it is just data retransmission we need to do or that and a
10205
   * fwd-tsn with it all.
10206
   */
10207
0
  if (TAILQ_EMPTY(&asoc->sent_queue)) {
10208
0
    return (SCTP_RETRAN_DONE);
10209
0
  }
10210
0
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) ||
10211
0
      (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) {
10212
    /* not yet open, resend the cookie and that is it */
10213
0
    return (1);
10214
0
  }
10215
#ifdef SCTP_AUDITING_ENABLED
10216
  sctp_auditing(20, inp, stcb, NULL);
10217
#endif
10218
0
  data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
10219
0
  TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
10220
0
    if (chk->sent != SCTP_DATAGRAM_RESEND) {
10221
      /* No, not sent to this net or not ready for rtx */
10222
0
      continue;
10223
0
    }
10224
0
    if (chk->data == NULL) {
10225
0
      SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
10226
0
                  chk->rec.data.tsn, chk->snd_count, chk->sent);
10227
0
      continue;
10228
0
    }
10229
0
    if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
10230
0
        (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
10231
0
      struct mbuf *op_err;
10232
0
      char msg[SCTP_DIAG_INFO_LEN];
10233
10234
0
      SCTP_SNPRINTF(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up",
10235
0
                    chk->rec.data.tsn, chk->snd_count);
10236
0
      op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
10237
0
                                   msg);
10238
0
      atomic_add_int(&stcb->asoc.refcnt, 1);
10239
0
      sctp_abort_an_association(stcb->sctp_ep, stcb, op_err,
10240
0
                                false, so_locked);
10241
0
      SCTP_TCB_LOCK(stcb);
10242
0
      atomic_subtract_int(&stcb->asoc.refcnt, 1);
10243
0
      return (SCTP_RETRAN_EXIT);
10244
0
    }
10245
    /* pick up the net */
10246
0
    net = chk->whoTo;
10247
0
    switch (net->ro._l_addr.sa.sa_family) {
10248
0
#ifdef INET
10249
0
      case AF_INET:
10250
0
        mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
10251
0
        break;
10252
0
#endif
10253
0
#ifdef INET6
10254
0
      case AF_INET6:
10255
0
        mtu = net->mtu - SCTP_MIN_OVERHEAD;
10256
0
        break;
10257
0
#endif
10258
0
#if defined(__Userspace__)
10259
0
      case AF_CONN:
10260
0
        mtu = net->mtu - sizeof(struct sctphdr);
10261
0
        break;
10262
0
#endif
10263
0
      default:
10264
        /* TSNH */
10265
0
        mtu = net->mtu;
10266
0
        break;
10267
0
    }
10268
10269
0
    if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
10270
      /* No room in peers rwnd */
10271
0
      uint32_t tsn;
10272
10273
0
      tsn = asoc->last_acked_seq + 1;
10274
0
      if (tsn == chk->rec.data.tsn) {
10275
        /*
10276
         * we make a special exception for this
10277
         * case. The peer has no rwnd but is missing
10278
         * the lowest chunk.. which is probably what
10279
         * is holding up the rwnd.
10280
         */
10281
0
        goto one_chunk_around;
10282
0
      }
10283
0
      return (1);
10284
0
    }
10285
0
  one_chunk_around:
10286
0
    if (asoc->peers_rwnd < mtu) {
10287
0
      one_chunk = 1;
10288
0
      if ((asoc->peers_rwnd == 0) &&
10289
0
          (asoc->total_flight == 0)) {
10290
0
        chk->window_probe = 1;
10291
0
        chk->whoTo->window_probe = 1;
10292
0
      }
10293
0
    }
10294
#ifdef SCTP_AUDITING_ENABLED
10295
    sctp_audit_log(0xC3, 2);
10296
#endif
10297
0
    bundle_at = 0;
10298
0
    m = NULL;
10299
0
    net->fast_retran_ip = 0;
10300
0
    if (chk->rec.data.doing_fast_retransmit == 0) {
10301
      /*
10302
       * if no FR in progress skip destination that have
10303
       * flight_size > cwnd.
10304
       */
10305
0
      if (net->flight_size >= net->cwnd) {
10306
0
        continue;
10307
0
      }
10308
0
    } else {
10309
      /*
10310
       * Mark the destination net to have FR recovery
10311
       * limits put on it.
10312
       */
10313
0
      *fr_done = 1;
10314
0
      net->fast_retran_ip = 1;
10315
0
    }
10316
10317
    /*
10318
     * if no AUTH is yet included and this chunk requires it,
10319
     * make sure to account for it.  We don't apply the size
10320
     * until the AUTH chunk is actually added below in case
10321
     * there is no room for this chunk.
10322
     */
10323
0
    if (data_auth_reqd && (auth == NULL)) {
10324
0
      dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
10325
0
    } else
10326
0
      dmtu = 0;
10327
10328
0
    if ((chk->send_size <= (mtu - dmtu)) ||
10329
0
        (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
10330
      /* ok we will add this one */
10331
0
      if (data_auth_reqd) {
10332
0
        if (auth == NULL) {
10333
0
          m = sctp_add_auth_chunk(m,
10334
0
                &endofchain,
10335
0
                &auth,
10336
0
                &auth_offset,
10337
0
                stcb,
10338
0
                SCTP_DATA);
10339
0
          auth_keyid = chk->auth_keyid;
10340
0
          override_ok = 0;
10341
0
          SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10342
0
        } else if (override_ok) {
10343
0
          auth_keyid = chk->auth_keyid;
10344
0
          override_ok = 0;
10345
0
        } else if (chk->auth_keyid != auth_keyid) {
10346
          /* different keyid, so done bundling */
10347
0
          break;
10348
0
        }
10349
0
      }
10350
0
      m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
10351
0
      if (m == NULL) {
10352
0
        SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10353
0
        return (ENOMEM);
10354
0
      }
10355
      /* Do clear IP_DF ? */
10356
0
      if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
10357
0
        no_fragmentflg = 0;
10358
0
      }
10359
      /* update our MTU size */
10360
0
      if (mtu > (chk->send_size + dmtu))
10361
0
        mtu -= (chk->send_size + dmtu);
10362
0
      else
10363
0
        mtu = 0;
10364
0
      data_list[bundle_at++] = chk;
10365
0
      if (one_chunk && (asoc->total_flight <= 0)) {
10366
0
        SCTP_STAT_INCR(sctps_windowprobed);
10367
0
      }
10368
0
    }
10369
0
    if (one_chunk == 0) {
10370
      /*
10371
       * now are there anymore forward from chk to pick
10372
       * up?
10373
       */
10374
0
      for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
10375
0
        if (fwd->sent != SCTP_DATAGRAM_RESEND) {
10376
          /* Nope, not for retran */
10377
0
          continue;
10378
0
        }
10379
0
        if (fwd->whoTo != net) {
10380
          /* Nope, not the net in question */
10381
0
          continue;
10382
0
        }
10383
0
        if (data_auth_reqd && (auth == NULL)) {
10384
0
          dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
10385
0
        } else
10386
0
          dmtu = 0;
10387
0
        if (fwd->send_size <= (mtu - dmtu)) {
10388
0
          if (data_auth_reqd) {
10389
0
            if (auth == NULL) {
10390
0
              m = sctp_add_auth_chunk(m,
10391
0
                    &endofchain,
10392
0
                    &auth,
10393
0
                    &auth_offset,
10394
0
                    stcb,
10395
0
                    SCTP_DATA);
10396
0
              auth_keyid = fwd->auth_keyid;
10397
0
              override_ok = 0;
10398
0
              SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10399
0
            } else if (override_ok) {
10400
0
              auth_keyid = fwd->auth_keyid;
10401
0
              override_ok = 0;
10402
0
            } else if (fwd->auth_keyid != auth_keyid) {
10403
              /* different keyid, so done bundling */
10404
0
              break;
10405
0
            }
10406
0
          }
10407
0
          m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
10408
0
          if (m == NULL) {
10409
0
            SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
10410
0
            return (ENOMEM);
10411
0
          }
10412
          /* Do clear IP_DF ? */
10413
0
          if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
10414
0
            no_fragmentflg = 0;
10415
0
          }
10416
          /* update our MTU size */
10417
0
          if (mtu > (fwd->send_size + dmtu))
10418
0
            mtu -= (fwd->send_size + dmtu);
10419
0
          else
10420
0
            mtu = 0;
10421
0
          data_list[bundle_at++] = fwd;
10422
0
          if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
10423
0
            break;
10424
0
          }
10425
0
        } else {
10426
          /* can't fit so we are done */
10427
0
          break;
10428
0
        }
10429
0
      }
10430
0
    }
10431
    /* Is there something to send for this destination? */
10432
0
    if (m) {
10433
      /*
10434
       * No matter if we fail/or succeed we should start a
10435
       * timer. A failure is like a lost IP packet :-)
10436
       */
10437
0
      if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
10438
        /*
10439
         * no timer running on this destination
10440
         * restart it.
10441
         */
10442
0
        sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10443
0
        tmr_started = 1;
10444
0
      }
10445
0
      switch (asoc->snd_edmid) {
10446
0
      case SCTP_EDMID_LOWER_LAYER_DTLS:
10447
0
        use_zero_crc = true;
10448
0
        break;
10449
0
      default:
10450
0
        use_zero_crc = false;
10451
0
        break;
10452
0
      }
10453
      /* Now lets send it, if there is anything to send :> */
10454
0
      if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
10455
0
                                              (struct sockaddr *)&net->ro._l_addr, m,
10456
0
                                              auth_offset, auth, auth_keyid,
10457
0
                                              no_fragmentflg, 0, 0,
10458
0
                                              inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
10459
0
                                              net->port, NULL,
10460
#if defined(__FreeBSD__) && !defined(__Userspace__)
10461
                                              0, 0,
10462
#endif
10463
0
                                              use_zero_crc,
10464
0
                                              so_locked))) {
10465
        /* error, we could not output */
10466
0
        SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10467
0
        if (error == ENOBUFS) {
10468
0
          asoc->ifp_had_enobuf = 1;
10469
0
          SCTP_STAT_INCR(sctps_lowlevelerr);
10470
0
        }
10471
0
        return (error);
10472
0
      } else {
10473
0
        asoc->ifp_had_enobuf = 0;
10474
0
      }
10475
0
      endofchain = NULL;
10476
0
      auth = NULL;
10477
0
      auth_offset = 0;
10478
      /* For HB's */
10479
      /*
10480
       * We don't want to mark the net->sent time here
10481
       * since this we use this for HB and retrans cannot
10482
       * measure RTT
10483
       */
10484
      /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
10485
10486
      /* For auto-close */
10487
0
      if (*now_filled == 0) {
10488
0
        (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
10489
0
        *now = asoc->time_last_sent;
10490
0
        *now_filled = 1;
10491
0
      } else {
10492
0
        asoc->time_last_sent = *now;
10493
0
      }
10494
0
      *cnt_out += bundle_at;
10495
#ifdef SCTP_AUDITING_ENABLED
10496
      sctp_audit_log(0xC4, bundle_at);
10497
#endif
10498
0
      if (bundle_at) {
10499
0
        tsns_sent = data_list[0]->rec.data.tsn;
10500
0
      }
10501
0
      for (i = 0; i < bundle_at; i++) {
10502
0
        SCTP_STAT_INCR(sctps_sendretransdata);
10503
0
        data_list[i]->sent = SCTP_DATAGRAM_SENT;
10504
        /*
10505
         * When we have a revoked data, and we
10506
         * retransmit it, then we clear the revoked
10507
         * flag since this flag dictates if we
10508
         * subtracted from the fs
10509
         */
10510
0
        if (data_list[i]->rec.data.chunk_was_revoked) {
10511
          /* Deflate the cwnd */
10512
0
          data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
10513
0
          data_list[i]->rec.data.chunk_was_revoked = 0;
10514
0
        }
10515
0
        data_list[i]->snd_count++;
10516
0
        sctp_ucount_decr(asoc->sent_queue_retran_cnt);
10517
        /* record the time */
10518
0
        data_list[i]->sent_rcv_time = asoc->time_last_sent;
10519
0
        if (data_list[i]->book_size_scale) {
10520
          /*
10521
           * need to double the book size on
10522
           * this one
10523
           */
10524
0
          data_list[i]->book_size_scale = 0;
10525
          /* Since we double the booksize, we must
10526
           * also double the output queue size, since this
10527
           * get shrunk when we free by this amount.
10528
           */
10529
0
          atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
10530
0
          data_list[i]->book_size *= 2;
10531
0
        } else {
10532
0
          if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
10533
0
            sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
10534
0
                  asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
10535
0
          }
10536
0
          asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
10537
0
                      (uint32_t) (data_list[i]->send_size +
10538
0
                      SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
10539
0
        }
10540
0
        if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
10541
0
          sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
10542
0
                   data_list[i]->whoTo->flight_size,
10543
0
                   data_list[i]->book_size,
10544
0
                   (uint32_t)(uintptr_t)data_list[i]->whoTo,
10545
0
                   data_list[i]->rec.data.tsn);
10546
0
        }
10547
0
        sctp_flight_size_increase(data_list[i]);
10548
0
        sctp_total_flight_increase(stcb, data_list[i]);
10549
0
        if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
10550
          /* SWS sender side engages */
10551
0
          asoc->peers_rwnd = 0;
10552
0
        }
10553
0
        if ((i == 0) &&
10554
0
            (data_list[i]->rec.data.doing_fast_retransmit)) {
10555
0
          SCTP_STAT_INCR(sctps_sendfastretrans);
10556
0
          if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
10557
0
              (tmr_started == 0)) {
10558
            /*-
10559
             * ok we just fast-retrans'd
10560
             * the lowest TSN, i.e the
10561
             * first on the list. In
10562
             * this case we want to give
10563
             * some more time to get a
10564
             * SACK back without a
10565
             * t3-expiring.
10566
             */
10567
0
            sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
10568
0
                            SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
10569
0
            sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
10570
0
          }
10571
0
        }
10572
0
      }
10573
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10574
0
        sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
10575
0
      }
10576
#ifdef SCTP_AUDITING_ENABLED
10577
      sctp_auditing(21, inp, stcb, NULL);
10578
#endif
10579
0
    } else {
10580
      /* None will fit */
10581
0
      return (1);
10582
0
    }
10583
0
    if (asoc->sent_queue_retran_cnt <= 0) {
10584
      /* all done we have no more to retran */
10585
0
      asoc->sent_queue_retran_cnt = 0;
10586
0
      break;
10587
0
    }
10588
0
    if (one_chunk) {
10589
      /* No more room in rwnd */
10590
0
      return (1);
10591
0
    }
10592
    /* stop the for loop here. we sent out a packet */
10593
0
    break;
10594
0
  }
10595
0
  return (0);
10596
0
}
10597
10598
static void
10599
sctp_timer_validation(struct sctp_inpcb *inp,
10600
    struct sctp_tcb *stcb,
10601
    struct sctp_association *asoc)
10602
0
{
10603
0
  struct sctp_nets *net;
10604
10605
  /* Validate that a timer is running somewhere */
10606
0
  TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10607
0
    if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
10608
      /* Here is a timer */
10609
0
      return;
10610
0
    }
10611
0
  }
10612
0
  SCTP_TCB_LOCK_ASSERT(stcb);
10613
  /* Gak, we did not have a timer somewhere */
10614
0
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
10615
0
  if (asoc->alternate) {
10616
0
    sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
10617
0
  } else {
10618
0
    sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
10619
0
  }
10620
0
  return;
10621
0
}
10622
10623
void
10624
sctp_chunk_output(struct sctp_inpcb *inp,
10625
    struct sctp_tcb *stcb,
10626
    int from_where,
10627
    int so_locked)
10628
46.9k
{
10629
  /*-
10630
   * Ok this is the generic chunk service queue. we must do the
10631
   * following:
10632
   * - See if there are retransmits pending, if so we must
10633
   *   do these first.
10634
   * - Service the stream queue that is next, moving any
10635
   *   message (note I must get a complete message i.e.
10636
   *   FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
10637
   *   TSN's
10638
   * - Check to see if the cwnd/rwnd allows any output, if so we
10639
   *   go ahead and formulate and send the low level chunks. Making sure
10640
   *   to combine any control in the control chunk queue also.
10641
   */
10642
46.9k
  struct sctp_association *asoc;
10643
46.9k
  struct sctp_nets *net;
10644
46.9k
  int error = 0, num_out, tot_out = 0, ret = 0, reason_code;
10645
46.9k
  unsigned int burst_cnt = 0;
10646
46.9k
  struct timeval now;
10647
46.9k
  int now_filled = 0;
10648
46.9k
  int nagle_on;
10649
46.9k
  uint32_t frag_point = sctp_get_frag_point(stcb);
10650
46.9k
  int un_sent = 0;
10651
46.9k
  int fr_done;
10652
46.9k
  unsigned int tot_frs = 0;
10653
10654
#if defined(__APPLE__) && !defined(__Userspace__)
10655
  if (so_locked) {
10656
    sctp_lock_assert(SCTP_INP_SO(inp));
10657
  } else {
10658
    sctp_unlock_assert(SCTP_INP_SO(inp));
10659
  }
10660
#endif
10661
46.9k
  asoc = &stcb->asoc;
10662
46.9k
do_it_again:
10663
  /* The Nagle algorithm is only applied when handling a send call. */
10664
46.9k
  if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
10665
22.6k
    if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
10666
22.6k
      nagle_on = 0;
10667
22.6k
    } else {
10668
0
      nagle_on = 1;
10669
0
    }
10670
24.3k
  } else {
10671
24.3k
    nagle_on = 0;
10672
24.3k
  }
10673
46.9k
  SCTP_TCB_LOCK_ASSERT(stcb);
10674
10675
46.9k
  un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
10676
10677
46.9k
  if ((un_sent <= 0) &&
10678
22.1k
      (TAILQ_EMPTY(&asoc->control_send_queue)) &&
10679
121
      (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
10680
121
      (asoc->sent_queue_retran_cnt == 0) &&
10681
121
      (asoc->trigger_reset == 0)) {
10682
    /* Nothing to do unless there is something to be sent left */
10683
121
    return;
10684
121
  }
10685
  /* Do we have something to send, data or control AND
10686
   * a sack timer running, if so piggy-back the sack.
10687
   */
10688
46.8k
  if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
10689
58
    sctp_send_sack(stcb, so_locked);
10690
58
    sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
10691
58
                    SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
10692
58
  }
10693
46.8k
  while (asoc->sent_queue_retran_cnt) {
10694
    /*-
10695
     * Ok, it is retransmission time only, we send out only ONE
10696
     * packet with a single call off to the retran code.
10697
     */
10698
0
    if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
10699
      /*-
10700
       * Special hook for handling cookies discarded
10701
       * by peer that carried data. Send cookie-ack only
10702
       * and then the next call with get the retran's.
10703
       */
10704
0
      (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
10705
0
                                  from_where,
10706
0
                                  &now, &now_filled, frag_point, so_locked);
10707
0
      return;
10708
0
    } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
10709
      /* if its not from a HB then do it */
10710
0
      fr_done = 0;
10711
0
      ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
10712
0
      if (fr_done) {
10713
0
        tot_frs++;
10714
0
      }
10715
0
    } else {
10716
      /*
10717
       * its from any other place, we don't allow retran
10718
       * output (only control)
10719
       */
10720
0
      ret = 1;
10721
0
    }
10722
0
    if (ret > 0) {
10723
      /* Can't send anymore */
10724
      /*-
10725
       * now lets push out control by calling med-level
10726
       * output once. this assures that we WILL send HB's
10727
       * if queued too.
10728
       */
10729
0
      (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
10730
0
                                  from_where,
10731
0
                                  &now, &now_filled, frag_point, so_locked);
10732
#ifdef SCTP_AUDITING_ENABLED
10733
      sctp_auditing(8, inp, stcb, NULL);
10734
#endif
10735
0
      sctp_timer_validation(inp, stcb, asoc);
10736
0
      return;
10737
0
    }
10738
0
    if (ret < 0) {
10739
      /*-
10740
       * The count was off.. retran is not happening so do
10741
       * the normal retransmission.
10742
       */
10743
#ifdef SCTP_AUDITING_ENABLED
10744
      sctp_auditing(9, inp, stcb, NULL);
10745
#endif
10746
0
      if (ret == SCTP_RETRAN_EXIT) {
10747
0
        return;
10748
0
      }
10749
0
      break;
10750
0
    }
10751
0
    if (from_where == SCTP_OUTPUT_FROM_T3) {
10752
      /* Only one transmission allowed out of a timeout */
10753
#ifdef SCTP_AUDITING_ENABLED
10754
      sctp_auditing(10, inp, stcb, NULL);
10755
#endif
10756
      /* Push out any control */
10757
0
      (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10758
0
                                  &now, &now_filled, frag_point, so_locked);
10759
0
      return;
10760
0
    }
10761
0
    if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10762
      /* Hit FR burst limit */
10763
0
      return;
10764
0
    }
10765
0
    if ((num_out == 0) && (ret == 0)) {
10766
      /* No more retrans to send */
10767
0
      break;
10768
0
    }
10769
0
  }
10770
#ifdef SCTP_AUDITING_ENABLED
10771
  sctp_auditing(12, inp, stcb, NULL);
10772
#endif
10773
  /* Check for bad destinations, if they exist move chunks around. */
10774
54.0k
  TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10775
54.0k
    if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10776
      /*-
10777
       * if possible move things off of this address we
10778
       * still may send below due to the dormant state but
10779
       * we try to find an alternate address to send to
10780
       * and if we have one we move all queued data on the
10781
       * out wheel to this alternate address.
10782
       */
10783
0
      if (net->ref_count > 1)
10784
0
        sctp_move_chunks_from_net(stcb, net);
10785
54.0k
    } else {
10786
      /*-
10787
       * if ((asoc->sat_network) || (net->addr_is_local))
10788
       * { burst_limit = asoc->max_burst *
10789
       * SCTP_SAT_NETWORK_BURST_INCR; }
10790
       */
10791
54.0k
      if (asoc->max_burst > 0) {
10792
54.0k
        if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10793
54.0k
          if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10794
            /* JRS - Use the congestion control given in the congestion control module */
10795
0
            asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10796
0
            if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10797
0
              sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10798
0
            }
10799
0
            SCTP_STAT_INCR(sctps_maxburstqueued);
10800
0
          }
10801
54.0k
          net->fast_retran_ip = 0;
10802
54.0k
        } else {
10803
0
          if (net->flight_size == 0) {
10804
            /* Should be decaying the cwnd here */
10805
0
            ;
10806
0
          }
10807
0
        }
10808
54.0k
      }
10809
54.0k
    }
10810
54.0k
  }
10811
46.8k
  burst_cnt = 0;
10812
51.9k
  do {
10813
51.9k
    error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10814
51.9k
                                  &reason_code, 0, from_where,
10815
51.9k
                                  &now, &now_filled, frag_point, so_locked);
10816
51.9k
    if (error) {
10817
0
      SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10818
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10819
0
        sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10820
0
      }
10821
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10822
0
        sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10823
0
        sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10824
0
      }
10825
0
      break;
10826
0
    }
10827
51.9k
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10828
10829
51.9k
    tot_out += num_out;
10830
51.9k
    burst_cnt++;
10831
51.9k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10832
0
      sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10833
0
      if (num_out == 0) {
10834
0
        sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10835
0
      }
10836
0
    }
10837
51.9k
    if (nagle_on) {
10838
      /*
10839
       * When the Nagle algorithm is used, look at how much
10840
       * is unsent, then if its smaller than an MTU and we
10841
       * have data in flight we stop, except if we are
10842
       * handling a fragmented user message.
10843
       */
10844
0
      un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight;
10845
0
      if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10846
0
          (stcb->asoc.total_flight > 0)) {
10847
/*  &&         sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/
10848
0
        break;
10849
0
      }
10850
0
    }
10851
51.9k
    if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10852
51.9k
        TAILQ_EMPTY(&asoc->send_queue) &&
10853
30.1k
        sctp_is_there_unsent_data(stcb, so_locked) == 0) {
10854
      /* Nothing left to send */
10855
4.66k
      break;
10856
4.66k
    }
10857
47.3k
    if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10858
      /* Nothing left to send */
10859
19.9k
      break;
10860
19.9k
    }
10861
47.3k
  } while (num_out &&
10862
5.14k
           ((asoc->max_burst == 0) ||
10863
5.14k
      SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10864
0
      (burst_cnt < asoc->max_burst)));
10865
10866
46.8k
  if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10867
0
    if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10868
0
      SCTP_STAT_INCR(sctps_maxburstqueued);
10869
0
      asoc->burst_limit_applied = 1;
10870
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10871
0
        sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10872
0
      }
10873
0
    } else {
10874
0
      asoc->burst_limit_applied = 0;
10875
0
    }
10876
0
  }
10877
46.8k
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10878
0
    sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10879
0
  }
10880
46.8k
  SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10881
46.8k
    tot_out);
10882
10883
  /*-
10884
   * Now we need to clean up the control chunk chain if a ECNE is on
10885
   * it. It must be marked as UNSENT again so next call will continue
10886
   * to send it until such time that we get a CWR, to remove it.
10887
   */
10888
46.8k
  if (stcb->asoc.ecn_echo_cnt_onq)
10889
0
    sctp_fix_ecn_echo(asoc);
10890
10891
46.8k
  if (stcb->asoc.trigger_reset) {
10892
25
    if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) {
10893
0
      goto do_it_again;
10894
0
    }
10895
25
  }
10896
46.8k
  return;
10897
46.8k
}
10898
10899
int
10900
sctp_output(
10901
  struct sctp_inpcb *inp,
10902
  struct mbuf *m,
10903
  struct sockaddr *addr,
10904
  struct mbuf *control,
10905
#if defined(__FreeBSD__) && !defined(__Userspace__)
10906
  struct thread *p,
10907
#elif defined(_WIN32) && !defined(__Userspace__)
10908
  PKTHREAD p,
10909
#else
10910
#if defined(__APPLE__) && !defined(__Userspace__)
10911
  struct proc *p SCTP_UNUSED,
10912
#else
10913
  struct proc *p,
10914
#endif
10915
#endif
10916
  int flags)
10917
0
{
10918
0
  if (inp == NULL) {
10919
0
    SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10920
0
    return (EINVAL);
10921
0
  }
10922
10923
0
  if (inp->sctp_socket == NULL) {
10924
0
    SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10925
0
    return (EINVAL);
10926
0
  }
10927
0
  return (sctp_sosend(inp->sctp_socket,
10928
0
          addr,
10929
0
          (struct uio *)NULL,
10930
0
          m,
10931
0
          control,
10932
#if defined(__APPLE__) && !defined(__Userspace__)
10933
          flags
10934
#else
10935
0
          flags, p
10936
0
#endif
10937
0
      ));
10938
0
}
10939
10940
void
10941
send_forward_tsn(struct sctp_tcb *stcb,
10942
     struct sctp_association *asoc)
10943
1.07k
{
10944
1.07k
  struct sctp_tmit_chunk *chk, *at, *tp1, *last;
10945
1.07k
  struct sctp_forward_tsn_chunk *fwdtsn;
10946
1.07k
  struct sctp_strseq *strseq;
10947
1.07k
  struct sctp_strseq_mid *strseq_m;
10948
1.07k
  uint32_t advance_peer_ack_point;
10949
1.07k
  unsigned int cnt_of_space, i, ovh;
10950
1.07k
  unsigned int space_needed;
10951
1.07k
  unsigned int cnt_of_skipped = 0;
10952
10953
1.07k
  SCTP_TCB_LOCK_ASSERT(stcb);
10954
3.21k
  TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10955
3.21k
    if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10956
      /* mark it to unsent */
10957
1.00k
      chk->sent = SCTP_DATAGRAM_UNSENT;
10958
1.00k
      chk->snd_count = 0;
10959
      /* Do we correct its output location? */
10960
1.00k
      if (chk->whoTo) {
10961
0
        sctp_free_remote_addr(chk->whoTo);
10962
0
        chk->whoTo = NULL;
10963
0
      }
10964
1.00k
      goto sctp_fill_in_rest;
10965
1.00k
    }
10966
3.21k
  }
10967
  /* Ok if we reach here we must build one */
10968
72
  sctp_alloc_a_chunk(stcb, chk);
10969
72
  if (chk == NULL) {
10970
0
    return;
10971
0
  }
10972
72
  asoc->fwd_tsn_cnt++;
10973
72
  chk->copy_by_ref = 0;
10974
  /*
10975
   * We don't do the old thing here since
10976
   * this is used not for on-wire but to
10977
   * tell if we are sending a fwd-tsn by
10978
   * the stack during output. And if its
10979
   * a IFORWARD or a FORWARD it is a fwd-tsn.
10980
   */
10981
72
  chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10982
72
  chk->rec.chunk_id.can_take_data = 0;
10983
72
  chk->flags = 0;
10984
72
  chk->asoc = asoc;
10985
72
  chk->whoTo = NULL;
10986
72
  chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
10987
72
  if (chk->data == NULL) {
10988
0
    sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10989
0
    return;
10990
0
  }
10991
72
  SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10992
72
  chk->sent = SCTP_DATAGRAM_UNSENT;
10993
72
  chk->snd_count = 0;
10994
72
  TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10995
72
  asoc->ctrl_queue_cnt++;
10996
1.07k
sctp_fill_in_rest:
10997
  /*-
10998
   * Here we go through and fill out the part that deals with
10999
   * stream/seq of the ones we skip.
11000
   */
11001
1.07k
  SCTP_BUF_LEN(chk->data) = 0;
11002
1.07k
  TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
11003
0
    if ((at->sent != SCTP_FORWARD_TSN_SKIP) &&
11004
0
        (at->sent != SCTP_DATAGRAM_NR_ACKED)) {
11005
      /* no more to look at */
11006
0
      break;
11007
0
    }
11008
0
    if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
11009
      /* We don't report these */
11010
0
      continue;
11011
0
    }
11012
0
    cnt_of_skipped++;
11013
0
  }
11014
1.07k
  if (asoc->idata_supported) {
11015
521
    space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
11016
521
                    (cnt_of_skipped * sizeof(struct sctp_strseq_mid)));
11017
554
  } else {
11018
554
    space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
11019
554
                    (cnt_of_skipped * sizeof(struct sctp_strseq)));
11020
554
  }
11021
1.07k
  cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data);
11022
11023
1.07k
  if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
11024
0
    ovh = SCTP_MIN_OVERHEAD;
11025
1.07k
  } else {
11026
1.07k
    ovh = SCTP_MIN_V4_OVERHEAD;
11027
1.07k
  }
11028
1.07k
  if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
11029
    /* trim to a mtu size */
11030
1.07k
    cnt_of_space = asoc->smallest_mtu - ovh;
11031
1.07k
  }
11032
1.07k
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
11033
0
    sctp_misc_ints(SCTP_FWD_TSN_CHECK,
11034
0
             0xff, 0, cnt_of_skipped,
11035
0
             asoc->advanced_peer_ack_point);
11036
0
  }
11037
1.07k
  advance_peer_ack_point = asoc->advanced_peer_ack_point;
11038
1.07k
  if (cnt_of_space < space_needed) {
11039
    /*-
11040
     * ok we must trim down the chunk by lowering the
11041
     * advance peer ack point.
11042
     */
11043
0
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
11044
0
      sctp_misc_ints(SCTP_FWD_TSN_CHECK,
11045
0
               0xff, 0xff, cnt_of_space,
11046
0
               space_needed);
11047
0
    }
11048
0
    cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
11049
0
    if (asoc->idata_supported) {
11050
0
      cnt_of_skipped /= sizeof(struct sctp_strseq_mid);
11051
0
    } else {
11052
0
      cnt_of_skipped /= sizeof(struct sctp_strseq);
11053
0
    }
11054
    /*-
11055
     * Go through and find the TSN that will be the one
11056
     * we report.
11057
     */
11058
0
    at = TAILQ_FIRST(&asoc->sent_queue);
11059
0
    if (at != NULL) {
11060
0
      for (i = 0; i < cnt_of_skipped; i++) {
11061
0
        tp1 = TAILQ_NEXT(at, sctp_next);
11062
0
        if (tp1 == NULL) {
11063
0
          break;
11064
0
        }
11065
0
        at = tp1;
11066
0
      }
11067
0
    }
11068
0
    if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
11069
0
      sctp_misc_ints(SCTP_FWD_TSN_CHECK,
11070
0
               0xff, cnt_of_skipped, at->rec.data.tsn,
11071
0
               asoc->advanced_peer_ack_point);
11072
0
    }
11073
0
    last = at;
11074
    /*-
11075
     * last now points to last one I can report, update
11076
     * peer ack point
11077
     */
11078
0
    if (last) {
11079
0
      advance_peer_ack_point = last->rec.data.tsn;
11080
0
    }
11081
0
    if (asoc->idata_supported) {
11082
0
      space_needed = sizeof(struct sctp_forward_tsn_chunk) +
11083
0
                     cnt_of_skipped * sizeof(struct sctp_strseq_mid);
11084
0
    } else {
11085
0
      space_needed = sizeof(struct sctp_forward_tsn_chunk) +
11086
0
                     cnt_of_skipped * sizeof(struct sctp_strseq);
11087
0
    }
11088
0
  }
11089
1.07k
  chk->send_size = space_needed;
11090
  /* Setup the chunk */
11091
1.07k
  fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
11092
1.07k
  fwdtsn->ch.chunk_length = htons(chk->send_size);
11093
1.07k
  fwdtsn->ch.chunk_flags = 0;
11094
1.07k
  if (asoc->idata_supported) {
11095
521
    fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN;
11096
554
  } else {
11097
554
    fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
11098
554
  }
11099
1.07k
  fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
11100
1.07k
  SCTP_BUF_LEN(chk->data) = chk->send_size;
11101
1.07k
  fwdtsn++;
11102
  /*-
11103
   * Move pointer to after the fwdtsn and transfer to the
11104
   * strseq pointer.
11105
   */
11106
1.07k
  if (asoc->idata_supported) {
11107
521
    strseq_m = (struct sctp_strseq_mid *)fwdtsn;
11108
521
    strseq = NULL;
11109
554
  } else {
11110
554
    strseq = (struct sctp_strseq *)fwdtsn;
11111
554
    strseq_m = NULL;
11112
554
  }
11113
  /*-
11114
   * Now populate the strseq list. This is done blindly
11115
   * without pulling out duplicate stream info. This is
11116
   * inefficient but won't harm the process since the peer will
11117
   * look at these in sequence and will thus release anything.
11118
   * It could mean we exceed the PMTU and chop off some that
11119
   * we could have included.. but this is unlikely (aka 1432/4
11120
   * would mean 300+ stream seq's would have to be reported in
11121
   * one FWD-TSN. With a bit of work we can later FIX this to
11122
   * optimize and pull out duplicates.. but it does add more
11123
   * overhead. So for now... not!
11124
   */
11125
1.07k
  i = 0;
11126
1.07k
  TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
11127
0
    if (i >= cnt_of_skipped) {
11128
0
      break;
11129
0
    }
11130
0
    if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
11131
      /* We don't report these */
11132
0
      continue;
11133
0
    }
11134
0
    if (at->rec.data.tsn == advance_peer_ack_point) {
11135
0
      at->rec.data.fwd_tsn_cnt = 0;
11136
0
    }
11137
0
    if (asoc->idata_supported) {
11138
0
      strseq_m->sid = htons(at->rec.data.sid);
11139
0
      if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
11140
0
        strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG);
11141
0
      } else {
11142
0
        strseq_m->flags = 0;
11143
0
      }
11144
0
      strseq_m->mid = htonl(at->rec.data.mid);
11145
0
      strseq_m++;
11146
0
    } else {
11147
0
      strseq->sid = htons(at->rec.data.sid);
11148
0
      strseq->ssn = htons((uint16_t)at->rec.data.mid);
11149
0
      strseq++;
11150
0
    }
11151
0
    i++;
11152
0
  }
11153
1.07k
  return;
11154
72
}
11155
11156
void
11157
sctp_send_sack(struct sctp_tcb *stcb, int so_locked)
11158
11.8k
{
11159
  /*-
11160
   * Queue up a SACK or NR-SACK in the control queue.
11161
   * We must first check to see if a SACK or NR-SACK is
11162
   * somehow on the control queue.
11163
   * If so, we will take and and remove the old one.
11164
   */
11165
11.8k
  struct sctp_association *asoc;
11166
11.8k
  struct sctp_tmit_chunk *chk, *a_chk;
11167
11.8k
  struct sctp_sack_chunk *sack;
11168
11.8k
  struct sctp_nr_sack_chunk *nr_sack;
11169
11.8k
  struct sctp_gap_ack_block *gap_descriptor;
11170
11.8k
  const struct sack_track *selector;
11171
11.8k
  int mergeable = 0;
11172
11.8k
  int offset;
11173
11.8k
  caddr_t limit;
11174
11.8k
  uint32_t *dup;
11175
11.8k
  int limit_reached = 0;
11176
11.8k
  unsigned int i, siz, j;
11177
11.8k
  unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
11178
11.8k
  int num_dups = 0;
11179
11.8k
  int space_req;
11180
11.8k
  uint32_t highest_tsn;
11181
11.8k
  uint8_t flags;
11182
11.8k
  uint8_t type;
11183
11.8k
  uint8_t tsn_map;
11184
11185
11.8k
  if (stcb->asoc.nrsack_supported == 1) {
11186
5.95k
    type = SCTP_NR_SELECTIVE_ACK;
11187
5.95k
  } else {
11188
5.93k
    type = SCTP_SELECTIVE_ACK;
11189
5.93k
  }
11190
11.8k
  a_chk = NULL;
11191
11.8k
  asoc = &stcb->asoc;
11192
11.8k
  SCTP_TCB_LOCK_ASSERT(stcb);
11193
11.8k
  if (asoc->last_data_chunk_from == NULL) {
11194
    /* Hmm we never received anything */
11195
2.52k
    return;
11196
2.52k
  }
11197
9.36k
  sctp_slide_mapping_arrays(stcb);
11198
9.36k
  sctp_set_rwnd(stcb, asoc);
11199
5.14M
  TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11200
5.14M
    if (chk->rec.chunk_id.id == type) {
11201
      /* Hmm, found a sack already on queue, remove it */
11202
5.20k
      TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
11203
5.20k
      asoc->ctrl_queue_cnt--;
11204
5.20k
      a_chk = chk;
11205
5.20k
      if (a_chk->data) {
11206
5.20k
        sctp_m_freem(a_chk->data);
11207
5.20k
        a_chk->data = NULL;
11208
5.20k
      }
11209
5.20k
      if (a_chk->whoTo) {
11210
5.20k
        sctp_free_remote_addr(a_chk->whoTo);
11211
5.20k
        a_chk->whoTo = NULL;
11212
5.20k
      }
11213
5.20k
      break;
11214
5.20k
    }
11215
5.14M
  }
11216
9.36k
  if (a_chk == NULL) {
11217
4.16k
    sctp_alloc_a_chunk(stcb, a_chk);
11218
4.16k
    if (a_chk == NULL) {
11219
      /* No memory so we drop the idea, and set a timer */
11220
0
      if (stcb->asoc.delayed_ack) {
11221
0
        sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11222
0
                        stcb->sctp_ep, stcb, NULL,
11223
0
                        SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
11224
0
        sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11225
0
            stcb->sctp_ep, stcb, NULL);
11226
0
      } else {
11227
0
        stcb->asoc.send_sack = 1;
11228
0
      }
11229
0
      return;
11230
0
    }
11231
4.16k
    a_chk->copy_by_ref = 0;
11232
4.16k
    a_chk->rec.chunk_id.id = type;
11233
4.16k
    a_chk->rec.chunk_id.can_take_data = 1;
11234
4.16k
  }
11235
  /* Clear our pkt counts */
11236
9.36k
  asoc->data_pkts_seen = 0;
11237
11238
9.36k
  a_chk->flags = 0;
11239
9.36k
  a_chk->asoc = asoc;
11240
9.36k
  a_chk->snd_count = 0;
11241
9.36k
  a_chk->send_size = 0; /* fill in later */
11242
9.36k
  a_chk->sent = SCTP_DATAGRAM_UNSENT;
11243
9.36k
  a_chk->whoTo = NULL;
11244
11245
9.36k
  if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) == 0) {
11246
    /*-
11247
     * Ok, the destination for the SACK is unreachable, lets see if
11248
     * we can select an alternate to asoc->last_data_chunk_from
11249
     */
11250
0
    a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
11251
0
    if (a_chk->whoTo == NULL) {
11252
      /* Nope, no alternate */
11253
0
      a_chk->whoTo = asoc->last_data_chunk_from;
11254
0
    }
11255
9.36k
  } else {
11256
9.36k
    a_chk->whoTo = asoc->last_data_chunk_from;
11257
9.36k
  }
11258
9.36k
  if (a_chk->whoTo) {
11259
9.36k
    atomic_add_int(&a_chk->whoTo->ref_count, 1);
11260
9.36k
  }
11261
9.36k
  if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
11262
764
    highest_tsn = asoc->highest_tsn_inside_map;
11263
8.60k
  } else {
11264
8.60k
    highest_tsn = asoc->highest_tsn_inside_nr_map;
11265
8.60k
  }
11266
9.36k
  if (highest_tsn == asoc->cumulative_tsn) {
11267
    /* no gaps */
11268
6.43k
    if (type == SCTP_SELECTIVE_ACK) {
11269
2.40k
      space_req = sizeof(struct sctp_sack_chunk);
11270
4.03k
    } else {
11271
4.03k
      space_req = sizeof(struct sctp_nr_sack_chunk);
11272
4.03k
    }
11273
6.43k
  } else {
11274
    /* gaps get a cluster */
11275
2.93k
    space_req = MCLBYTES;
11276
2.93k
  }
11277
  /* Ok now lets formulate a MBUF with our sack */
11278
9.36k
  a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA);
11279
9.36k
  if ((a_chk->data == NULL) ||
11280
9.36k
      (a_chk->whoTo == NULL)) {
11281
    /* rats, no mbuf memory */
11282
0
    if (a_chk->data) {
11283
      /* was a problem with the destination */
11284
0
      sctp_m_freem(a_chk->data);
11285
0
      a_chk->data = NULL;
11286
0
    }
11287
0
    sctp_free_a_chunk(stcb, a_chk, so_locked);
11288
    /* sa_ignore NO_NULL_CHK */
11289
0
    if (stcb->asoc.delayed_ack) {
11290
0
      sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
11291
0
                      stcb->sctp_ep, stcb, NULL,
11292
0
                      SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
11293
0
      sctp_timer_start(SCTP_TIMER_TYPE_RECV,
11294
0
          stcb->sctp_ep, stcb, NULL);
11295
0
    } else {
11296
0
      stcb->asoc.send_sack = 1;
11297
0
    }
11298
0
    return;
11299
0
  }
11300
  /* ok, lets go through and fill it in */
11301
9.36k
  SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
11302
9.36k
  space = (unsigned int)M_TRAILINGSPACE(a_chk->data);
11303
9.36k
  if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
11304
2.93k
    space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
11305
2.93k
  }
11306
9.36k
  limit = mtod(a_chk->data, caddr_t);
11307
9.36k
  limit += space;
11308
11309
9.36k
  flags = 0;
11310
11311
9.36k
  if ((asoc->sctp_cmt_on_off > 0) &&
11312
0
      SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
11313
    /*-
11314
     * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
11315
     * received, then set high bit to 1, else 0. Reset
11316
     * pkts_rcvd.
11317
     */
11318
0
    flags |= (asoc->cmt_dac_pkts_rcvd << 6);
11319
0
    asoc->cmt_dac_pkts_rcvd = 0;
11320
0
  }
11321
#ifdef SCTP_ASOCLOG_OF_TSNS
11322
  stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
11323
  stcb->asoc.cumack_log_atsnt++;
11324
  if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
11325
    stcb->asoc.cumack_log_atsnt = 0;
11326
  }
11327
#endif
11328
  /* reset the readers interpretation */
11329
9.36k
  stcb->freed_by_sorcv_sincelast = 0;
11330
11331
9.36k
  if (type == SCTP_SELECTIVE_ACK) {
11332
3.85k
    sack = mtod(a_chk->data, struct sctp_sack_chunk *);
11333
3.85k
    nr_sack = NULL;
11334
3.85k
    gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
11335
3.85k
    if (highest_tsn > asoc->mapping_array_base_tsn) {
11336
1.51k
      siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
11337
2.34k
    } else {
11338
2.34k
      siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + highest_tsn + 7) / 8;
11339
2.34k
    }
11340
5.51k
  } else {
11341
5.51k
    sack = NULL;
11342
5.51k
    nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
11343
5.51k
    gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
11344
5.51k
    if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
11345
0
      siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
11346
5.51k
    } else {
11347
5.51k
      siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
11348
5.51k
    }
11349
5.51k
  }
11350
11351
9.36k
  if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
11352
6.78k
    offset = 1;
11353
6.78k
  } else {
11354
2.58k
    offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
11355
2.58k
  }
11356
9.36k
  if (((type == SCTP_SELECTIVE_ACK) &&
11357
9.36k
       SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
11358
7.92k
      ((type == SCTP_NR_SELECTIVE_ACK) &&
11359
7.92k
       SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
11360
    /* we have a gap .. maybe */
11361
363k
    for (i = 0; i < siz; i++) {
11362
360k
      tsn_map = asoc->mapping_array[i];
11363
360k
      if (type == SCTP_SELECTIVE_ACK) {
11364
360k
        tsn_map |= asoc->nr_mapping_array[i];
11365
360k
      }
11366
360k
      if (i == 0) {
11367
        /*
11368
         * Clear all bits corresponding to TSNs
11369
         * smaller or equal to the cumulative TSN.
11370
         */
11371
1.44k
        tsn_map &= (~0U << (1 - offset));
11372
1.44k
      }
11373
360k
      selector = &sack_array[tsn_map];
11374
360k
      if (mergeable && selector->right_edge) {
11375
        /*
11376
         * Backup, left and right edges were ok to
11377
         * merge.
11378
         */
11379
103
        num_gap_blocks--;
11380
103
        gap_descriptor--;
11381
103
      }
11382
360k
      if (selector->num_entries == 0)
11383
355k
        mergeable = 0;
11384
4.34k
      else {
11385
8.92k
        for (j = 0; j < selector->num_entries; j++) {
11386
4.58k
          if (mergeable && selector->right_edge) {
11387
            /*
11388
             * do a merge by NOT setting
11389
             * the left side
11390
             */
11391
103
            mergeable = 0;
11392
4.47k
          } else {
11393
            /*
11394
             * no merge, set the left
11395
             * side
11396
             */
11397
4.47k
            mergeable = 0;
11398
4.47k
            gap_descriptor->start = htons((selector->gaps[j].start + offset));
11399
4.47k
          }
11400
4.58k
          gap_descriptor->end = htons((selector->gaps[j].end + offset));
11401
4.58k
          num_gap_blocks++;
11402
4.58k
          gap_descriptor++;
11403
4.58k
          if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
11404
            /* no more room */
11405
0
            limit_reached = 1;
11406
0
            break;
11407
0
          }
11408
4.58k
        }
11409
4.34k
        if (selector->left_edge) {
11410
858
          mergeable = 1;
11411
858
        }
11412
4.34k
      }
11413
360k
      if (limit_reached) {
11414
        /* Reached the limit stop */
11415
0
        break;
11416
0
      }
11417
360k
      offset += 8;
11418
360k
    }
11419
2.93k
  }
11420
9.36k
  if ((type == SCTP_NR_SELECTIVE_ACK) &&
11421
5.51k
      (limit_reached == 0)) {
11422
5.51k
    mergeable = 0;
11423
11424
5.51k
    if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
11425
2.37k
      siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
11426
3.13k
    } else {
11427
3.13k
      siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
11428
3.13k
    }
11429
11430
5.51k
    if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
11431
3.11k
      offset = 1;
11432
3.11k
    } else {
11433
2.39k
      offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
11434
2.39k
    }
11435
5.51k
    if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
11436
      /* we have a gap .. maybe */
11437
1.48k
      for (i = 0; i < siz; i++) {
11438
0
        tsn_map = asoc->nr_mapping_array[i];
11439
0
        if (i == 0) {
11440
          /*
11441
           * Clear all bits corresponding to TSNs
11442
           * smaller or equal to the cumulative TSN.
11443
           */
11444
0
          tsn_map &= (~0U << (1 - offset));
11445
0
        }
11446
0
        selector = &sack_array[tsn_map];
11447
0
        if (mergeable && selector->right_edge) {
11448
          /*
11449
          * Backup, left and right edges were ok to
11450
          * merge.
11451
          */
11452
0
          num_nr_gap_blocks--;
11453
0
          gap_descriptor--;
11454
0
        }
11455
0
        if (selector->num_entries == 0)
11456
0
          mergeable = 0;
11457
0
        else {
11458
0
          for (j = 0; j < selector->num_entries; j++) {
11459
0
            if (mergeable && selector->right_edge) {
11460
              /*
11461
              * do a merge by NOT setting
11462
              * the left side
11463
              */
11464
0
              mergeable = 0;
11465
0
            } else {
11466
              /*
11467
              * no merge, set the left
11468
              * side
11469
              */
11470
0
              mergeable = 0;
11471
0
              gap_descriptor->start = htons((selector->gaps[j].start + offset));
11472
0
            }
11473
0
            gap_descriptor->end = htons((selector->gaps[j].end + offset));
11474
0
            num_nr_gap_blocks++;
11475
0
            gap_descriptor++;
11476
0
            if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
11477
              /* no more room */
11478
0
              limit_reached = 1;
11479
0
              break;
11480
0
            }
11481
0
          }
11482
0
          if (selector->left_edge) {
11483
0
            mergeable = 1;
11484
0
          }
11485
0
        }
11486
0
        if (limit_reached) {
11487
          /* Reached the limit stop */
11488
0
          break;
11489
0
        }
11490
0
        offset += 8;
11491
0
      }
11492
1.48k
    }
11493
5.51k
  }
11494
  /* now we must add any dups we are going to report. */
11495
9.36k
  if ((limit_reached == 0) && (asoc->numduptsns)) {
11496
914
    dup = (uint32_t *) gap_descriptor;
11497
2.62k
    for (i = 0; i < asoc->numduptsns; i++) {
11498
1.70k
      *dup = htonl(asoc->dup_tsns[i]);
11499
1.70k
      dup++;
11500
1.70k
      num_dups++;
11501
1.70k
      if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
11502
        /* no more room */
11503
0
        break;
11504
0
      }
11505
1.70k
    }
11506
914
    asoc->numduptsns = 0;
11507
914
  }
11508
  /*
11509
   * now that the chunk is prepared queue it to the control chunk
11510
   * queue.
11511
   */
11512
9.36k
  if (type == SCTP_SELECTIVE_ACK) {
11513
3.85k
    a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) +
11514
3.85k
                                  (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
11515
3.85k
                                  num_dups * sizeof(int32_t));
11516
3.85k
    SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
11517
3.85k
    sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11518
3.85k
    sack->sack.a_rwnd = htonl(asoc->my_rwnd);
11519
3.85k
    sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
11520
3.85k
    sack->sack.num_dup_tsns = htons(num_dups);
11521
3.85k
    sack->ch.chunk_type = type;
11522
3.85k
    sack->ch.chunk_flags = flags;
11523
3.85k
    sack->ch.chunk_length = htons(a_chk->send_size);
11524
5.51k
  } else {
11525
5.51k
    a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) +
11526
5.51k
                                  (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
11527
5.51k
                                  num_dups * sizeof(int32_t));
11528
5.51k
    SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
11529
5.51k
    nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
11530
5.51k
    nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
11531
5.51k
    nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
11532
5.51k
    nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
11533
5.51k
    nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
11534
5.51k
    nr_sack->nr_sack.reserved = 0;
11535
5.51k
    nr_sack->ch.chunk_type = type;
11536
5.51k
    nr_sack->ch.chunk_flags = flags;
11537
5.51k
    nr_sack->ch.chunk_length = htons(a_chk->send_size);
11538
5.51k
  }
11539
9.36k
  TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
11540
9.36k
  asoc->my_last_reported_rwnd = asoc->my_rwnd;
11541
9.36k
  asoc->ctrl_queue_cnt++;
11542
9.36k
  asoc->send_sack = 0;
11543
9.36k
  SCTP_STAT_INCR(sctps_sendsacks);
11544
9.36k
  return;
11545
9.36k
}
11546
11547
void
11548
sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
11549
7.30k
{
11550
7.30k
  struct mbuf *m_abort, *m, *m_last;
11551
7.30k
  struct mbuf *m_out, *m_end = NULL;
11552
7.30k
  struct sctp_abort_chunk *abort;
11553
7.30k
  struct sctp_auth_chunk *auth = NULL;
11554
7.30k
  struct sctp_nets *net;
11555
7.30k
  uint32_t vtag;
11556
7.30k
  uint32_t auth_offset = 0;
11557
7.30k
  int error;
11558
7.30k
  uint16_t cause_len, chunk_len, padding_len;
11559
7.30k
  bool use_zero_crc;
11560
11561
#if defined(__APPLE__) && !defined(__Userspace__)
11562
  if (so_locked) {
11563
    sctp_lock_assert(SCTP_INP_SO(stcb->sctp_ep));
11564
  } else {
11565
    sctp_unlock_assert(SCTP_INP_SO(stcb->sctp_ep));
11566
  }
11567
#endif
11568
7.30k
  SCTP_TCB_LOCK_ASSERT(stcb);
11569
  /*-
11570
   * Add an AUTH chunk, if chunk requires it and save the offset into
11571
   * the chain for AUTH
11572
   */
11573
7.30k
  if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
11574
7.30k
                                  stcb->asoc.peer_auth_chunks)) {
11575
20
    m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset,
11576
20
              stcb, SCTP_ABORT_ASSOCIATION);
11577
20
    SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11578
7.28k
  } else {
11579
7.28k
    m_out = NULL;
11580
7.28k
  }
11581
7.30k
  switch (stcb->asoc.snd_edmid) {
11582
0
  case SCTP_EDMID_LOWER_LAYER_DTLS:
11583
0
    use_zero_crc = true;
11584
0
    break;
11585
7.30k
  default:
11586
7.30k
    use_zero_crc = false;
11587
7.30k
    break;
11588
7.30k
  }
11589
7.30k
  m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER);
11590
7.30k
  if (m_abort == NULL) {
11591
0
    if (m_out) {
11592
0
      sctp_m_freem(m_out);
11593
0
    }
11594
0
    if (operr) {
11595
0
      sctp_m_freem(operr);
11596
0
    }
11597
0
    return;
11598
0
  }
11599
  /* link in any error */
11600
7.30k
  SCTP_BUF_NEXT(m_abort) = operr;
11601
7.30k
  cause_len = 0;
11602
7.30k
  m_last = NULL;
11603
14.6k
  for (m = operr; m; m = SCTP_BUF_NEXT(m)) {
11604
7.30k
    cause_len += (uint16_t)SCTP_BUF_LEN(m);
11605
7.30k
    if (SCTP_BUF_NEXT(m) == NULL) {
11606
7.30k
      m_last = m;
11607
7.30k
    }
11608
7.30k
  }
11609
7.30k
  SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk);
11610
7.30k
  chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len;
11611
7.30k
  padding_len = SCTP_SIZE32(chunk_len) - chunk_len;
11612
7.30k
  if (m_out == NULL) {
11613
    /* NO Auth chunk prepended, so reserve space in front */
11614
7.30k
    SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
11615
7.30k
    m_out = m_abort;
11616
7.30k
  } else {
11617
    /* Put AUTH chunk at the front of the chain */
11618
6
    SCTP_BUF_NEXT(m_end) = m_abort;
11619
6
  }
11620
7.30k
  if (stcb->asoc.alternate) {
11621
30
    net = stcb->asoc.alternate;
11622
7.27k
  } else {
11623
7.27k
    net = stcb->asoc.primary_destination;
11624
7.27k
  }
11625
  /* Fill in the ABORT chunk header. */
11626
7.30k
  abort = mtod(m_abort, struct sctp_abort_chunk *);
11627
7.30k
  abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
11628
7.30k
  if (stcb->asoc.peer_vtag == 0) {
11629
    /* This happens iff the assoc is in COOKIE-WAIT state. */
11630
59
    vtag = stcb->asoc.my_vtag;
11631
59
    abort->ch.chunk_flags = SCTP_HAD_NO_TCB;
11632
7.24k
  } else {
11633
7.24k
    vtag = stcb->asoc.peer_vtag;
11634
7.24k
    abort->ch.chunk_flags = 0;
11635
7.24k
  }
11636
7.30k
  abort->ch.chunk_length = htons(chunk_len);
11637
  /* Add padding, if necessary. */
11638
7.30k
  if (padding_len > 0) {
11639
688
    if ((m_last == NULL) ||
11640
688
        (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) {
11641
0
      sctp_m_freem(m_out);
11642
0
      return;
11643
0
    }
11644
688
  }
11645
7.30k
  if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
11646
7.30k
                                          (struct sockaddr *)&net->ro._l_addr,
11647
7.30k
                                          m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
11648
7.30k
                                          stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag),
11649
7.30k
                                          stcb->asoc.primary_destination->port, NULL,
11650
#if defined(__FreeBSD__) && !defined(__Userspace__)
11651
                                          0, 0,
11652
#endif
11653
7.30k
                                          use_zero_crc,
11654
7.30k
                                          so_locked))) {
11655
114
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
11656
114
    if (error == ENOBUFS) {
11657
0
      stcb->asoc.ifp_had_enobuf = 1;
11658
0
      SCTP_STAT_INCR(sctps_lowlevelerr);
11659
0
    }
11660
7.19k
  } else {
11661
7.19k
    stcb->asoc.ifp_had_enobuf = 0;
11662
7.19k
  }
11663
7.30k
  SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11664
7.30k
}
11665
11666
void
11667
sctp_send_shutdown_complete(struct sctp_tcb *stcb,
11668
                            struct sctp_nets *net,
11669
                            int reflect_vtag)
11670
10
{
11671
  /* formulate and SEND a SHUTDOWN-COMPLETE */
11672
10
  struct mbuf *m_shutdown_comp;
11673
10
  struct sctp_shutdown_complete_chunk *shutdown_complete;
11674
10
  uint32_t vtag;
11675
10
  int error;
11676
10
  uint8_t flags;
11677
10
  bool use_zero_crc;
11678
11679
10
  m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
11680
10
  if (m_shutdown_comp == NULL) {
11681
    /* no mbuf's */
11682
0
    return;
11683
0
  }
11684
10
  if (reflect_vtag) {
11685
9
    flags = SCTP_HAD_NO_TCB;
11686
9
    vtag = stcb->asoc.my_vtag;
11687
9
  } else {
11688
1
    flags = 0;
11689
1
    vtag = stcb->asoc.peer_vtag;
11690
1
  }
11691
10
  switch (stcb->asoc.snd_edmid) {
11692
0
  case SCTP_EDMID_LOWER_LAYER_DTLS:
11693
0
    use_zero_crc = true;
11694
0
    break;
11695
10
  default:
11696
10
    use_zero_crc = false;
11697
10
    break;
11698
10
  }
11699
10
  shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
11700
10
  shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
11701
10
  shutdown_complete->ch.chunk_flags = flags;
11702
10
  shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
11703
10
  SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
11704
10
  if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
11705
10
                                          (struct sockaddr *)&net->ro._l_addr,
11706
10
                                          m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
11707
10
                                          stcb->sctp_ep->sctp_lport, stcb->rport,
11708
10
                                          htonl(vtag),
11709
10
                                          net->port, NULL,
11710
#if defined(__FreeBSD__) && !defined(__Userspace__)
11711
                                          0, 0,
11712
#endif
11713
10
                                          use_zero_crc,
11714
10
                                          SCTP_SO_NOT_LOCKED))) {
11715
0
    SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
11716
0
    if (error == ENOBUFS) {
11717
0
      stcb->asoc.ifp_had_enobuf = 1;
11718
0
      SCTP_STAT_INCR(sctps_lowlevelerr);
11719
0
    }
11720
10
  } else {
11721
10
    stcb->asoc.ifp_had_enobuf = 0;
11722
10
  }
11723
10
  SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11724
10
  return;
11725
10
}
11726
11727
#if defined(__FreeBSD__) && !defined(__Userspace__)
11728
static void
11729
sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
11730
                   struct sctphdr *sh, uint32_t vtag,
11731
                   uint8_t type, struct mbuf *cause,
11732
                   uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
11733
                   uint32_t vrf_id, uint16_t port)
11734
#else
11735
static void
11736
sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
11737
                   struct sctphdr *sh, uint32_t vtag,
11738
                   uint8_t type, struct mbuf *cause,
11739
                   uint32_t vrf_id SCTP_UNUSED, uint16_t port)
11740
#endif
11741
8.94k
{
11742
8.94k
  struct mbuf *o_pak;
11743
8.94k
  struct mbuf *mout;
11744
8.94k
  struct sctphdr *shout;
11745
8.94k
  struct sctp_chunkhdr *ch;
11746
8.94k
#if defined(INET) || defined(INET6)
11747
8.94k
  struct udphdr *udp;
11748
8.94k
#endif
11749
8.94k
  int ret, len, cause_len, padding_len;
11750
8.94k
#ifdef INET
11751
#if defined(__APPLE__) && !defined(__Userspace__)
11752
  sctp_route_t ro;
11753
#endif
11754
8.94k
  struct sockaddr_in *src_sin, *dst_sin;
11755
8.94k
  struct ip *ip;
11756
8.94k
#endif
11757
8.94k
#ifdef INET6
11758
8.94k
  struct sockaddr_in6 *src_sin6, *dst_sin6;
11759
8.94k
  struct ip6_hdr *ip6;
11760
8.94k
#endif
11761
11762
  /* Compute the length of the cause and add final padding. */
11763
8.94k
  cause_len = 0;
11764
8.94k
  if (cause != NULL) {
11765
7.43k
    struct mbuf *m_at, *m_last = NULL;
11766
11767
14.9k
    for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
11768
7.53k
      if (SCTP_BUF_NEXT(m_at) == NULL)
11769
7.43k
        m_last = m_at;
11770
7.53k
      cause_len += SCTP_BUF_LEN(m_at);
11771
7.53k
    }
11772
7.43k
    padding_len = cause_len % 4;
11773
7.43k
    if (padding_len != 0) {
11774
137
      padding_len = 4 - padding_len;
11775
137
    }
11776
7.43k
    if (padding_len != 0) {
11777
137
      if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
11778
0
        sctp_m_freem(cause);
11779
0
        return;
11780
0
      }
11781
137
    }
11782
7.43k
  } else {
11783
1.51k
    padding_len = 0;
11784
1.51k
  }
11785
  /* Get an mbuf for the header. */
11786
8.94k
  len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
11787
8.94k
  switch (dst->sa_family) {
11788
0
#ifdef INET
11789
0
  case AF_INET:
11790
0
    len += sizeof(struct ip);
11791
0
    break;
11792
0
#endif
11793
0
#ifdef INET6
11794
0
  case AF_INET6:
11795
0
    len += sizeof(struct ip6_hdr);
11796
0
    break;
11797
0
#endif
11798
8.94k
  default:
11799
8.94k
    break;
11800
8.94k
  }
11801
8.94k
#if defined(INET) || defined(INET6)
11802
8.94k
  if (port) {
11803
0
    len += sizeof(struct udphdr);
11804
0
  }
11805
8.94k
#endif
11806
#if defined(__APPLE__) && !defined(__Userspace__)
11807
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
11808
  mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
11809
#else
11810
  mout = sctp_get_mbuf_for_msg(len + SCTP_MAX_LINKHDR, 1, M_NOWAIT, 1, MT_DATA);
11811
#endif
11812
#else
11813
8.94k
  mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
11814
8.94k
#endif
11815
8.94k
  if (mout == NULL) {
11816
0
    if (cause) {
11817
0
      sctp_m_freem(cause);
11818
0
    }
11819
0
    return;
11820
0
  }
11821
#if defined(__APPLE__) && !defined(__Userspace__)
11822
#if defined(APPLE_LEOPARD) || defined(APPLE_SNOWLEOPARD)
11823
  SCTP_BUF_RESV_UF(mout, max_linkhdr);
11824
#else
11825
  SCTP_BUF_RESV_UF(mout, SCTP_MAX_LINKHDR);
11826
#endif
11827
#else
11828
8.94k
  SCTP_BUF_RESV_UF(mout, max_linkhdr);
11829
8.94k
#endif
11830
8.94k
  SCTP_BUF_LEN(mout) = len;
11831
8.94k
  SCTP_BUF_NEXT(mout) = cause;
11832
#if defined(__FreeBSD__) && !defined(__Userspace__)
11833
  M_SETFIB(mout, fibnum);
11834
  mout->m_pkthdr.flowid = mflowid;
11835
  M_HASHTYPE_SET(mout, mflowtype);
11836
#endif
11837
8.94k
#ifdef INET
11838
8.94k
  ip = NULL;
11839
8.94k
#endif
11840
8.94k
#ifdef INET6
11841
8.94k
  ip6 = NULL;
11842
8.94k
#endif
11843
8.94k
  switch (dst->sa_family) {
11844
0
#ifdef INET
11845
0
  case AF_INET:
11846
0
    src_sin = (struct sockaddr_in *)src;
11847
0
    dst_sin = (struct sockaddr_in *)dst;
11848
0
    ip = mtod(mout, struct ip *);
11849
0
    ip->ip_v = IPVERSION;
11850
0
    ip->ip_hl = (sizeof(struct ip) >> 2);
11851
0
    ip->ip_tos = 0;
11852
#if defined(__FreeBSD__) && !defined(__Userspace__)
11853
    ip->ip_off = htons(IP_DF);
11854
#elif defined(WITH_CONVERT_IP_OFF) || defined(__APPLE__)
11855
    ip->ip_off = IP_DF;
11856
#else
11857
0
    ip->ip_off = htons(IP_DF);
11858
0
#endif
11859
0
#if defined(__Userspace__)
11860
0
    ip->ip_id = htons(ip_id++);
11861
#elif defined(__FreeBSD__)
11862
    ip_fillid(ip, V_ip_random_id);
11863
#elif defined(__APPLE__)
11864
#if RANDOM_IP_ID
11865
    ip->ip_id = ip_randomid();
11866
#else
11867
    ip->ip_id = htons(ip_id++);
11868
#endif
11869
#else
11870
    ip->ip_id = ip_id++;
11871
#endif
11872
0
    ip->ip_ttl = MODULE_GLOBAL(ip_defttl);
11873
0
    if (port) {
11874
0
      ip->ip_p = IPPROTO_UDP;
11875
0
    } else {
11876
0
      ip->ip_p = IPPROTO_SCTP;
11877
0
    }
11878
0
    ip->ip_src.s_addr = dst_sin->sin_addr.s_addr;
11879
0
    ip->ip_dst.s_addr = src_sin->sin_addr.s_addr;
11880
0
    ip->ip_sum = 0;
11881
0
    len = sizeof(struct ip);
11882
0
    shout = (struct sctphdr *)((caddr_t)ip + len);
11883
0
    break;
11884
0
#endif
11885
0
#ifdef INET6
11886
0
  case AF_INET6:
11887
0
    src_sin6 = (struct sockaddr_in6 *)src;
11888
0
    dst_sin6 = (struct sockaddr_in6 *)dst;
11889
0
    ip6 = mtod(mout, struct ip6_hdr *);
11890
0
    ip6->ip6_flow = htonl(0x60000000);
11891
#if defined(__FreeBSD__) && !defined(__Userspace__)
11892
    if (V_ip6_auto_flowlabel) {
11893
      ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
11894
    }
11895
#endif
11896
0
#if defined(__Userspace__)
11897
0
    ip6->ip6_hlim = IPv6_HOP_LIMIT;
11898
#else
11899
    ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11900
#endif
11901
0
    if (port) {
11902
0
      ip6->ip6_nxt = IPPROTO_UDP;
11903
0
    } else {
11904
0
      ip6->ip6_nxt = IPPROTO_SCTP;
11905
0
    }
11906
0
    ip6->ip6_src = dst_sin6->sin6_addr;
11907
0
    ip6->ip6_dst = src_sin6->sin6_addr;
11908
0
    len = sizeof(struct ip6_hdr);
11909
0
    shout = (struct sctphdr *)((caddr_t)ip6 + len);
11910
0
    break;
11911
0
#endif
11912
8.94k
  default:
11913
8.94k
    len = 0;
11914
8.94k
    shout = mtod(mout, struct sctphdr *);
11915
8.94k
    break;
11916
8.94k
  }
11917
8.94k
#if defined(INET) || defined(INET6)
11918
8.94k
  if (port) {
11919
0
    if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11920
0
      sctp_m_freem(mout);
11921
0
      return;
11922
0
    }
11923
0
    udp = (struct udphdr *)shout;
11924
0
    udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11925
0
    udp->uh_dport = port;
11926
0
    udp->uh_sum = 0;
11927
0
    udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) +
11928
0
                                    sizeof(struct sctphdr) +
11929
0
                                    sizeof(struct sctp_chunkhdr) +
11930
0
                                    cause_len + padding_len));
11931
0
    len += sizeof(struct udphdr);
11932
0
    shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr));
11933
8.94k
  } else {
11934
8.94k
    udp = NULL;
11935
8.94k
  }
11936
8.94k
#endif
11937
8.94k
  shout->src_port = sh->dest_port;
11938
8.94k
  shout->dest_port = sh->src_port;
11939
8.94k
  shout->checksum = 0;
11940
8.94k
  if (vtag) {
11941
3.82k
    shout->v_tag = htonl(vtag);
11942
5.12k
  } else {
11943
5.12k
    shout->v_tag = sh->v_tag;
11944
5.12k
  }
11945
8.94k
  len += sizeof(struct sctphdr);
11946
8.94k
  ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr));
11947
8.94k
  ch->chunk_type = type;
11948
8.94k
  if (vtag) {
11949
3.82k
    ch->chunk_flags = 0;
11950
5.12k
  } else {
11951
5.12k
    ch->chunk_flags = SCTP_HAD_NO_TCB;
11952
5.12k
  }
11953
8.94k
  ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len));
11954
8.94k
  len += sizeof(struct sctp_chunkhdr);
11955
8.94k
  len += cause_len + padding_len;
11956
11957
8.94k
  if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11958
0
    sctp_m_freem(mout);
11959
0
    return;
11960
0
  }
11961
8.94k
  SCTP_ATTACH_CHAIN(o_pak, mout, len);
11962
8.94k
  switch (dst->sa_family) {
11963
0
#ifdef INET
11964
0
  case AF_INET:
11965
#if defined(__APPLE__) && !defined(__Userspace__)
11966
    /* zap the stack pointer to the route */
11967
    memset(&ro, 0, sizeof(sctp_route_t));
11968
#endif
11969
0
    if (port) {
11970
#if !defined(_WIN32) && !defined(__Userspace__)
11971
#if defined(__FreeBSD__)
11972
      if (V_udp_cksum) {
11973
        udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11974
      } else {
11975
        udp->uh_sum = 0;
11976
      }
11977
#else
11978
      udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11979
#endif
11980
#else
11981
0
      udp->uh_sum = 0;
11982
0
#endif
11983
0
    }
11984
#if defined(__FreeBSD__) && !defined(__Userspace__)
11985
    ip->ip_len = htons(len);
11986
#elif defined(__APPLE__) || defined(__Userspace__)
11987
    ip->ip_len = len;
11988
#else
11989
    ip->ip_len = htons(len);
11990
#endif
11991
0
    if (port) {
11992
0
      shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr));
11993
0
      SCTP_STAT_INCR(sctps_sendswcrc);
11994
#if !defined(_WIN32) && !defined(__Userspace__)
11995
#if defined(__FreeBSD__)
11996
      if (V_udp_cksum) {
11997
        SCTP_ENABLE_UDP_CSUM(o_pak);
11998
      }
11999
#else
12000
      SCTP_ENABLE_UDP_CSUM(o_pak);
12001
#endif
12002
#endif
12003
0
    } else {
12004
#if defined(__FreeBSD__) && !defined(__Userspace__)
12005
      mout->m_pkthdr.csum_flags = CSUM_SCTP;
12006
      mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
12007
      SCTP_STAT_INCR(sctps_sendhwcrc);
12008
#else
12009
0
      shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip));
12010
0
      SCTP_STAT_INCR(sctps_sendswcrc);
12011
0
#endif
12012
0
    }
12013
#ifdef SCTP_PACKET_LOGGING
12014
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
12015
      sctp_packet_log(o_pak);
12016
    }
12017
#endif
12018
#if defined(__APPLE__) && !defined(__Userspace__)
12019
    SCTP_IP_OUTPUT(ret, o_pak, &ro, NULL, vrf_id);
12020
    /* Free the route if we got one back */
12021
    if (ro.ro_rt) {
12022
      RTFREE(ro.ro_rt);
12023
      ro.ro_rt = NULL;
12024
    }
12025
#else
12026
#if defined(__FreeBSD__) && !defined(__Userspace__)
12027
    SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout);
12028
#endif
12029
0
    SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id);
12030
0
#endif
12031
0
    break;
12032
0
#endif
12033
0
#ifdef INET6
12034
0
  case AF_INET6:
12035
0
    ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr)));
12036
0
    if (port) {
12037
0
      shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
12038
0
      SCTP_STAT_INCR(sctps_sendswcrc);
12039
#if !defined(__Userspace__)
12040
#if defined(_WIN32)
12041
      udp->uh_sum = 0;
12042
#else
12043
      if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
12044
        udp->uh_sum = 0xffff;
12045
      }
12046
#endif
12047
#endif
12048
0
    } else {
12049
#if defined(__FreeBSD__) && !defined(__Userspace__)
12050
      mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
12051
      mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
12052
      SCTP_STAT_INCR(sctps_sendhwcrc);
12053
#else
12054
0
      shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr));
12055
0
      SCTP_STAT_INCR(sctps_sendswcrc);
12056
0
#endif
12057
0
    }
12058
#ifdef SCTP_PACKET_LOGGING
12059
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
12060
      sctp_packet_log(o_pak);
12061
    }
12062
#endif
12063
#if defined(__FreeBSD__) && !defined(__Userspace__)
12064
    SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout);
12065
#endif
12066
0
    SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id);
12067
0
    break;
12068
0
#endif
12069
0
#if defined(__Userspace__)
12070
8.94k
  case AF_CONN:
12071
8.94k
  {
12072
8.94k
    char *buffer;
12073
8.94k
    struct sockaddr_conn *sconn;
12074
12075
8.94k
    sconn = (struct sockaddr_conn *)src;
12076
8.94k
    if (SCTP_BASE_VAR(crc32c_offloaded) == 0) {
12077
0
      shout->checksum = sctp_calculate_cksum(o_pak, 0);
12078
0
      SCTP_STAT_INCR(sctps_sendswcrc);
12079
8.94k
    } else {
12080
8.94k
      SCTP_STAT_INCR(sctps_sendhwcrc);
12081
8.94k
    }
12082
#ifdef SCTP_PACKET_LOGGING
12083
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
12084
      sctp_packet_log(mout);
12085
    }
12086
#endif
12087
    /* Don't alloc/free for each packet */
12088
8.94k
    if ((buffer = malloc(len)) != NULL) {
12089
8.94k
      m_copydata(o_pak, 0, len, buffer);
12090
8.94k
      ret = SCTP_BASE_VAR(conn_output)(sconn->sconn_addr, buffer, len, 0, 0);
12091
8.94k
      free(buffer);
12092
8.94k
    } else {
12093
0
      ret = ENOMEM;
12094
0
    }
12095
8.94k
    sctp_m_freem(o_pak);
12096
8.94k
    break;
12097
0
  }
12098
0
#endif
12099
0
  default:
12100
0
    SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
12101
0
            dst->sa_family);
12102
0
    sctp_m_freem(mout);
12103
0
    SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12104
0
    return;
12105
8.94k
  }
12106
8.94k
  SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
12107
#if defined(__FreeBSD__) && !defined(__Userspace__)
12108
  if (port) {
12109
    UDPSTAT_INC(udps_opackets);
12110
  }
12111
#endif
12112
8.94k
  SCTP_STAT_INCR(sctps_sendpackets);
12113
8.94k
  SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
12114
8.94k
  SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
12115
8.94k
  if (ret) {
12116
0
    SCTP_STAT_INCR(sctps_senderrors);
12117
0
  }
12118
8.94k
  return;
12119
8.94k
}
12120
12121
void
12122
sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst,
12123
                             struct sctphdr *sh,
12124
#if defined(__FreeBSD__) && !defined(__Userspace__)
12125
                             uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12126
#endif
12127
                             uint32_t vrf_id, uint16_t port)
12128
3
{
12129
3
  sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL,
12130
#if defined(__FreeBSD__) && !defined(__Userspace__)
12131
                     mflowtype, mflowid, fibnum,
12132
#endif
12133
3
                     vrf_id, port);
12134
3
}
12135
12136
void
12137
sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net,int so_locked)
12138
26.2k
{
12139
26.2k
  struct sctp_tmit_chunk *chk;
12140
26.2k
  struct sctp_heartbeat_chunk *hb;
12141
26.2k
  struct timeval now;
12142
12143
26.2k
  SCTP_TCB_LOCK_ASSERT(stcb);
12144
26.2k
  if (net == NULL) {
12145
0
    return;
12146
0
  }
12147
26.2k
  (void)SCTP_GETTIME_TIMEVAL(&now);
12148
26.2k
  switch (net->ro._l_addr.sa.sa_family) {
12149
0
#ifdef INET
12150
1.11k
  case AF_INET:
12151
1.11k
    break;
12152
0
#endif
12153
0
#ifdef INET6
12154
1.45k
  case AF_INET6:
12155
1.45k
    break;
12156
0
#endif
12157
0
#if defined(__Userspace__)
12158
23.7k
  case AF_CONN:
12159
23.7k
    break;
12160
0
#endif
12161
0
  default:
12162
0
    return;
12163
26.2k
  }
12164
26.2k
  sctp_alloc_a_chunk(stcb, chk);
12165
26.2k
  if (chk == NULL) {
12166
0
    SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
12167
0
    return;
12168
0
  }
12169
12170
26.2k
  chk->copy_by_ref = 0;
12171
26.2k
  chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
12172
26.2k
  chk->rec.chunk_id.can_take_data = 1;
12173
26.2k
  chk->flags = 0;
12174
26.2k
  chk->asoc = &stcb->asoc;
12175
26.2k
  chk->send_size = sizeof(struct sctp_heartbeat_chunk);
12176
12177
26.2k
  chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
12178
26.2k
  if (chk->data == NULL) {
12179
0
    sctp_free_a_chunk(stcb, chk, so_locked);
12180
0
    return;
12181
0
  }
12182
26.2k
  SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12183
26.2k
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12184
26.2k
  chk->sent = SCTP_DATAGRAM_UNSENT;
12185
26.2k
  chk->snd_count = 0;
12186
26.2k
  chk->whoTo = net;
12187
26.2k
  atomic_add_int(&chk->whoTo->ref_count, 1);
12188
  /* Now we have a mbuf that we can fill in with the details */
12189
26.2k
  hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
12190
26.2k
  memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
12191
  /* fill out chunk header */
12192
26.2k
  hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
12193
26.2k
  hb->ch.chunk_flags = 0;
12194
26.2k
  hb->ch.chunk_length = htons(chk->send_size);
12195
  /* Fill out hb parameter */
12196
26.2k
  hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
12197
26.2k
  hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
12198
26.2k
  hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
12199
26.2k
  hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
12200
  /* Did our user request this one, put it in */
12201
26.2k
  hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family;
12202
#ifdef HAVE_SA_LEN
12203
  hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
12204
#else
12205
26.2k
  switch (net->ro._l_addr.sa.sa_family) {
12206
0
#ifdef INET
12207
1.11k
  case AF_INET:
12208
1.11k
    hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in);
12209
1.11k
    break;
12210
0
#endif
12211
0
#ifdef INET6
12212
1.45k
  case AF_INET6:
12213
1.45k
    hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_in6);
12214
1.45k
    break;
12215
0
#endif
12216
0
#if defined(__Userspace__)
12217
23.7k
  case AF_CONN:
12218
23.7k
    hb->heartbeat.hb_info.addr_len = sizeof(struct sockaddr_conn);
12219
23.7k
    break;
12220
0
#endif
12221
0
  default:
12222
0
    hb->heartbeat.hb_info.addr_len = 0;
12223
0
    break;
12224
26.2k
  }
12225
26.2k
#endif
12226
26.2k
  if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
12227
    /*
12228
     * we only take from the entropy pool if the address is not
12229
     * confirmed.
12230
     */
12231
2.03k
    net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12232
2.03k
    net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
12233
24.2k
  } else {
12234
24.2k
    net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
12235
24.2k
    net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
12236
24.2k
  }
12237
26.2k
  switch (net->ro._l_addr.sa.sa_family) {
12238
0
#ifdef INET
12239
1.11k
  case AF_INET:
12240
1.11k
    memcpy(hb->heartbeat.hb_info.address,
12241
1.11k
           &net->ro._l_addr.sin.sin_addr,
12242
1.11k
           sizeof(net->ro._l_addr.sin.sin_addr));
12243
1.11k
    break;
12244
0
#endif
12245
0
#ifdef INET6
12246
1.45k
  case AF_INET6:
12247
1.45k
    memcpy(hb->heartbeat.hb_info.address,
12248
1.45k
           &net->ro._l_addr.sin6.sin6_addr,
12249
1.45k
           sizeof(net->ro._l_addr.sin6.sin6_addr));
12250
1.45k
    break;
12251
0
#endif
12252
0
#if defined(__Userspace__)
12253
23.7k
  case AF_CONN:
12254
23.7k
    memcpy(hb->heartbeat.hb_info.address,
12255
23.7k
           &net->ro._l_addr.sconn.sconn_addr,
12256
23.7k
           sizeof(net->ro._l_addr.sconn.sconn_addr));
12257
23.7k
    break;
12258
0
#endif
12259
0
  default:
12260
0
    if (chk->data) {
12261
0
      sctp_m_freem(chk->data);
12262
0
      chk->data = NULL;
12263
0
    }
12264
0
    sctp_free_a_chunk(stcb, chk, so_locked);
12265
0
    return;
12266
0
    break;
12267
26.2k
  }
12268
26.2k
  net->hb_responded = 0;
12269
26.2k
  TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12270
26.2k
  stcb->asoc.ctrl_queue_cnt++;
12271
26.2k
  SCTP_STAT_INCR(sctps_sendheartbeat);
12272
26.2k
  return;
12273
26.2k
}
12274
12275
void
12276
sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
12277
       uint32_t high_tsn)
12278
0
{
12279
0
  struct sctp_association *asoc;
12280
0
  struct sctp_ecne_chunk *ecne;
12281
0
  struct sctp_tmit_chunk *chk;
12282
12283
0
  if (net == NULL) {
12284
0
    return;
12285
0
  }
12286
0
  asoc = &stcb->asoc;
12287
0
  SCTP_TCB_LOCK_ASSERT(stcb);
12288
0
  TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12289
0
    if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
12290
      /* found a previous ECN_ECHO update it if needed */
12291
0
      uint32_t cnt, ctsn;
12292
0
      ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12293
0
      ctsn = ntohl(ecne->tsn);
12294
0
      if (SCTP_TSN_GT(high_tsn, ctsn)) {
12295
0
        ecne->tsn = htonl(high_tsn);
12296
0
        SCTP_STAT_INCR(sctps_queue_upd_ecne);
12297
0
      }
12298
0
      cnt = ntohl(ecne->num_pkts_since_cwr);
12299
0
      cnt++;
12300
0
      ecne->num_pkts_since_cwr = htonl(cnt);
12301
0
      return;
12302
0
    }
12303
0
  }
12304
  /* nope could not find one to update so we must build one */
12305
0
  sctp_alloc_a_chunk(stcb, chk);
12306
0
  if (chk == NULL) {
12307
0
    return;
12308
0
  }
12309
0
  SCTP_STAT_INCR(sctps_queue_upd_ecne);
12310
0
  chk->copy_by_ref = 0;
12311
0
  chk->rec.chunk_id.id = SCTP_ECN_ECHO;
12312
0
  chk->rec.chunk_id.can_take_data = 0;
12313
0
  chk->flags = 0;
12314
0
  chk->asoc = &stcb->asoc;
12315
0
  chk->send_size = sizeof(struct sctp_ecne_chunk);
12316
0
  chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
12317
0
  if (chk->data == NULL) {
12318
0
    sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
12319
0
    return;
12320
0
  }
12321
0
  SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12322
0
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12323
0
  chk->sent = SCTP_DATAGRAM_UNSENT;
12324
0
  chk->snd_count = 0;
12325
0
  chk->whoTo = net;
12326
0
  atomic_add_int(&chk->whoTo->ref_count, 1);
12327
12328
0
  stcb->asoc.ecn_echo_cnt_onq++;
12329
0
  ecne = mtod(chk->data, struct sctp_ecne_chunk *);
12330
0
  ecne->ch.chunk_type = SCTP_ECN_ECHO;
12331
0
  ecne->ch.chunk_flags = 0;
12332
0
  ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
12333
0
  ecne->tsn = htonl(high_tsn);
12334
0
  ecne->num_pkts_since_cwr = htonl(1);
12335
0
  TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
12336
0
  asoc->ctrl_queue_cnt++;
12337
0
}
12338
12339
void
12340
sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
12341
    struct mbuf *m, int len, int iphlen, int bad_crc)
12342
10
{
12343
10
  struct sctp_association *asoc;
12344
10
  struct sctp_pktdrop_chunk *drp;
12345
10
  struct sctp_tmit_chunk *chk;
12346
10
  uint8_t *datap;
12347
10
  int was_trunc = 0;
12348
10
  int fullsz = 0;
12349
10
  long spc;
12350
10
  int offset;
12351
10
  struct sctp_chunkhdr *ch, chunk_buf;
12352
10
  unsigned int chk_length;
12353
12354
10
        if (!stcb) {
12355
0
            return;
12356
0
        }
12357
10
  asoc = &stcb->asoc;
12358
10
  SCTP_TCB_LOCK_ASSERT(stcb);
12359
10
  if (asoc->pktdrop_supported == 0) {
12360
    /*-
12361
     * peer must declare support before I send one.
12362
     */
12363
10
    return;
12364
10
  }
12365
0
  if (stcb->sctp_socket == NULL) {
12366
0
    return;
12367
0
  }
12368
0
  sctp_alloc_a_chunk(stcb, chk);
12369
0
  if (chk == NULL) {
12370
0
    return;
12371
0
  }
12372
0
  chk->copy_by_ref = 0;
12373
0
  chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
12374
0
  chk->rec.chunk_id.can_take_data = 1;
12375
0
  chk->flags = 0;
12376
0
  len -= iphlen;
12377
0
  chk->send_size = len;
12378
  /* Validate that we do not have an ABORT in here. */
12379
0
  offset = iphlen + sizeof(struct sctphdr);
12380
0
  ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
12381
0
               sizeof(*ch), (uint8_t *) & chunk_buf);
12382
0
  while (ch != NULL) {
12383
0
    chk_length = ntohs(ch->chunk_length);
12384
0
    if (chk_length < sizeof(*ch)) {
12385
      /* break to abort land */
12386
0
      break;
12387
0
    }
12388
0
    switch (ch->chunk_type) {
12389
0
    case SCTP_PACKET_DROPPED:
12390
0
    case SCTP_ABORT_ASSOCIATION:
12391
0
    case SCTP_INITIATION_ACK:
12392
      /**
12393
       * We don't respond with an PKT-DROP to an ABORT
12394
       * or PKT-DROP. We also do not respond to an
12395
       * INIT-ACK, because we can't know if the initiation
12396
       * tag is correct or not.
12397
       */
12398
0
      sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
12399
0
      return;
12400
0
    default:
12401
0
      break;
12402
0
    }
12403
0
    offset += SCTP_SIZE32(chk_length);
12404
0
    ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
12405
0
        sizeof(*ch), (uint8_t *) & chunk_buf);
12406
0
  }
12407
12408
0
  if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
12409
0
      min(stcb->asoc.smallest_mtu, MCLBYTES)) {
12410
    /* only send 1 mtu worth, trim off the
12411
     * excess on the end.
12412
     */
12413
0
    fullsz = len;
12414
0
    len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
12415
0
    was_trunc = 1;
12416
0
  }
12417
0
  chk->asoc = &stcb->asoc;
12418
0
  chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12419
0
  if (chk->data == NULL) {
12420
0
jump_out:
12421
0
    sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
12422
0
    return;
12423
0
  }
12424
0
  SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12425
0
  drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
12426
0
  if (drp == NULL) {
12427
0
    sctp_m_freem(chk->data);
12428
0
    chk->data = NULL;
12429
0
    goto jump_out;
12430
0
  }
12431
0
  chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
12432
0
      sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
12433
0
  chk->book_size_scale = 0;
12434
0
  if (was_trunc) {
12435
0
    drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
12436
0
    drp->trunc_len = htons(fullsz);
12437
    /* Len is already adjusted to size minus overhead above
12438
     * take out the pkt_drop chunk itself from it.
12439
     */
12440
0
    chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk));
12441
0
    len = chk->send_size;
12442
0
  } else {
12443
    /* no truncation needed */
12444
0
    drp->ch.chunk_flags = 0;
12445
0
    drp->trunc_len = htons(0);
12446
0
  }
12447
0
  if (bad_crc) {
12448
0
    drp->ch.chunk_flags |= SCTP_BADCRC;
12449
0
  }
12450
0
  chk->send_size += sizeof(struct sctp_pktdrop_chunk);
12451
0
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12452
0
  chk->sent = SCTP_DATAGRAM_UNSENT;
12453
0
  chk->snd_count = 0;
12454
0
  if (net) {
12455
    /* we should hit here */
12456
0
    chk->whoTo = net;
12457
0
    atomic_add_int(&chk->whoTo->ref_count, 1);
12458
0
  } else {
12459
0
    chk->whoTo = NULL;
12460
0
  }
12461
0
  drp->ch.chunk_type = SCTP_PACKET_DROPPED;
12462
0
  drp->ch.chunk_length = htons(chk->send_size);
12463
0
  spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
12464
0
  if (spc < 0) {
12465
0
    spc = 0;
12466
0
  }
12467
0
  drp->bottle_bw = htonl(spc);
12468
0
  if (asoc->my_rwnd) {
12469
0
    drp->current_onq = htonl(asoc->size_on_reasm_queue +
12470
0
        asoc->size_on_all_streams +
12471
0
        asoc->my_rwnd_control_len +
12472
0
        SCTP_SBAVAIL(&stcb->sctp_socket->so_rcv));
12473
0
  } else {
12474
    /*-
12475
     * If my rwnd is 0, possibly from mbuf depletion as well as
12476
     * space used, tell the peer there is NO space aka onq == bw
12477
     */
12478
0
    drp->current_onq = htonl(spc);
12479
0
  }
12480
0
  drp->reserved = 0;
12481
0
  datap = drp->data;
12482
0
  m_copydata(m, iphlen, len, (caddr_t)datap);
12483
0
  TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
12484
0
  asoc->ctrl_queue_cnt++;
12485
0
}
12486
12487
void
12488
sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
12489
4.38k
{
12490
4.38k
  struct sctp_association *asoc;
12491
4.38k
  struct sctp_cwr_chunk *cwr;
12492
4.38k
  struct sctp_tmit_chunk *chk;
12493
12494
4.38k
  SCTP_TCB_LOCK_ASSERT(stcb);
12495
4.38k
  if (net == NULL) {
12496
0
    return;
12497
0
  }
12498
4.38k
  asoc = &stcb->asoc;
12499
12.2k
  TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
12500
12.2k
    if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
12501
      /* found a previous CWR queued to same destination update it if needed */
12502
4.01k
      uint32_t ctsn;
12503
4.01k
      cwr = mtod(chk->data, struct sctp_cwr_chunk *);
12504
4.01k
      ctsn = ntohl(cwr->tsn);
12505
4.01k
      if (SCTP_TSN_GT(high_tsn, ctsn)) {
12506
0
        cwr->tsn = htonl(high_tsn);
12507
0
      }
12508
4.01k
      if (override & SCTP_CWR_REDUCE_OVERRIDE) {
12509
        /* Make sure override is carried */
12510
4.01k
        cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
12511
4.01k
      }
12512
4.01k
      return;
12513
4.01k
    }
12514
12.2k
  }
12515
363
  sctp_alloc_a_chunk(stcb, chk);
12516
363
  if (chk == NULL) {
12517
0
    return;
12518
0
  }
12519
363
  chk->copy_by_ref = 0;
12520
363
  chk->rec.chunk_id.id = SCTP_ECN_CWR;
12521
363
  chk->rec.chunk_id.can_take_data = 1;
12522
363
  chk->flags = 0;
12523
363
  chk->asoc = asoc;
12524
363
  chk->send_size = sizeof(struct sctp_cwr_chunk);
12525
363
  chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
12526
363
  if (chk->data == NULL) {
12527
0
    sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
12528
0
    return;
12529
0
  }
12530
363
  SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12531
363
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12532
363
  chk->sent = SCTP_DATAGRAM_UNSENT;
12533
363
  chk->snd_count = 0;
12534
363
  chk->whoTo = net;
12535
363
  atomic_add_int(&chk->whoTo->ref_count, 1);
12536
363
  cwr = mtod(chk->data, struct sctp_cwr_chunk *);
12537
363
  cwr->ch.chunk_type = SCTP_ECN_CWR;
12538
363
  cwr->ch.chunk_flags = override;
12539
363
  cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
12540
363
  cwr->tsn = htonl(high_tsn);
12541
363
  TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
12542
363
  asoc->ctrl_queue_cnt++;
12543
363
}
12544
12545
static int
12546
sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
12547
                          uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
12548
7.01k
{
12549
7.01k
  uint16_t len, old_len, i;
12550
7.01k
  struct sctp_stream_reset_out_request *req_out;
12551
7.01k
  struct sctp_chunkhdr *ch;
12552
7.01k
  int at;
12553
7.01k
  int number_entries=0;
12554
12555
7.01k
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12556
7.01k
  old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12557
  /* get to new offset for the param. */
12558
7.01k
  req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
12559
  /* now how long will this param be? */
12560
1.80M
  for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
12561
1.80M
    if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
12562
427k
        (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
12563
1.80M
        TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
12564
425k
      number_entries++;
12565
425k
    }
12566
1.80M
  }
12567
7.01k
  if (number_entries == 0) {
12568
5.28k
    return (0);
12569
5.28k
  }
12570
1.72k
  if (number_entries == stcb->asoc.streamoutcnt) {
12571
688
    number_entries = 0;
12572
688
  }
12573
1.72k
  if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) {
12574
968
    number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET;
12575
968
  }
12576
1.72k
  len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
12577
1.72k
  req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
12578
1.72k
  req_out->ph.param_length = htons(len);
12579
1.72k
  req_out->request_seq = htonl(seq);
12580
1.72k
  req_out->response_seq = htonl(resp_seq);
12581
1.72k
  req_out->send_reset_at_tsn = htonl(last_sent);
12582
1.72k
  at = 0;
12583
1.72k
  if (number_entries) {
12584
205k
    for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
12585
205k
      if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
12586
194k
          (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
12587
205k
          TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
12588
193k
        req_out->list_of_streams[at] = htons(i);
12589
193k
        at++;
12590
193k
        stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
12591
193k
        if (at >= number_entries) {
12592
1.03k
          break;
12593
1.03k
        }
12594
193k
      }
12595
205k
    }
12596
1.03k
  } else {
12597
177k
    for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
12598
176k
      stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
12599
176k
    }
12600
688
  }
12601
1.72k
  if (SCTP_SIZE32(len) > len) {
12602
    /*-
12603
     * Need to worry about the pad we may end up adding to the
12604
     * end. This is easy since the struct is either aligned to 4
12605
     * bytes or 2 bytes off.
12606
     */
12607
21
    req_out->list_of_streams[number_entries] = 0;
12608
21
  }
12609
  /* now fix the chunk length */
12610
1.72k
  ch->chunk_length = htons(len + old_len);
12611
1.72k
  chk->book_size = len + old_len;
12612
1.72k
  chk->book_size_scale = 0;
12613
1.72k
  chk->send_size = SCTP_SIZE32(chk->book_size);
12614
1.72k
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12615
1.72k
  return (1);
12616
7.01k
}
12617
12618
static void
12619
sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
12620
                         int number_entries, uint16_t *list,
12621
                         uint32_t seq)
12622
1.64k
{
12623
1.64k
  uint16_t len, old_len, i;
12624
1.64k
  struct sctp_stream_reset_in_request *req_in;
12625
1.64k
  struct sctp_chunkhdr *ch;
12626
12627
1.64k
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12628
1.64k
  old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12629
12630
  /* get to new offset for the param. */
12631
1.64k
  req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
12632
  /* now how long will this param be? */
12633
1.64k
  len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
12634
1.64k
  req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
12635
1.64k
  req_in->ph.param_length = htons(len);
12636
1.64k
  req_in->request_seq = htonl(seq);
12637
1.64k
  if (number_entries) {
12638
0
    for (i = 0; i < number_entries; i++) {
12639
0
      req_in->list_of_streams[i] = htons(list[i]);
12640
0
    }
12641
0
  }
12642
1.64k
  if (SCTP_SIZE32(len) > len) {
12643
    /*-
12644
     * Need to worry about the pad we may end up adding to the
12645
     * end. This is easy since the struct is either aligned to 4
12646
     * bytes or 2 bytes off.
12647
     */
12648
0
    req_in->list_of_streams[number_entries] = 0;
12649
0
  }
12650
  /* now fix the chunk length */
12651
1.64k
  ch->chunk_length = htons(len + old_len);
12652
1.64k
  chk->book_size = len + old_len;
12653
1.64k
  chk->book_size_scale = 0;
12654
1.64k
  chk->send_size = SCTP_SIZE32(chk->book_size);
12655
1.64k
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12656
1.64k
  return;
12657
1.64k
}
12658
12659
static void
12660
sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
12661
                          uint32_t seq)
12662
0
{
12663
0
  uint16_t len, old_len;
12664
0
  struct sctp_stream_reset_tsn_request *req_tsn;
12665
0
  struct sctp_chunkhdr *ch;
12666
12667
0
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12668
0
  old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12669
12670
  /* get to new offset for the param. */
12671
0
  req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
12672
  /* now how long will this param be? */
12673
0
  len = sizeof(struct sctp_stream_reset_tsn_request);
12674
0
  req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
12675
0
  req_tsn->ph.param_length = htons(len);
12676
0
  req_tsn->request_seq = htonl(seq);
12677
12678
  /* now fix the chunk length */
12679
0
  ch->chunk_length = htons(len + old_len);
12680
0
  chk->send_size = len + old_len;
12681
0
  chk->book_size = SCTP_SIZE32(chk->send_size);
12682
0
  chk->book_size_scale = 0;
12683
0
  SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
12684
0
  return;
12685
0
}
12686
12687
void
12688
sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
12689
                             uint32_t resp_seq, uint32_t result)
12690
10.9k
{
12691
10.9k
  uint16_t len, old_len;
12692
10.9k
  struct sctp_stream_reset_response *resp;
12693
10.9k
  struct sctp_chunkhdr *ch;
12694
12695
10.9k
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12696
10.9k
  old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12697
12698
  /* get to new offset for the param. */
12699
10.9k
  resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
12700
  /* now how long will this param be? */
12701
10.9k
  len = sizeof(struct sctp_stream_reset_response);
12702
10.9k
  resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
12703
10.9k
  resp->ph.param_length = htons(len);
12704
10.9k
  resp->response_seq = htonl(resp_seq);
12705
10.9k
  resp->result = ntohl(result);
12706
12707
  /* now fix the chunk length */
12708
10.9k
  ch->chunk_length = htons(len + old_len);
12709
10.9k
  chk->book_size = len + old_len;
12710
10.9k
  chk->book_size_scale = 0;
12711
10.9k
  chk->send_size = SCTP_SIZE32(chk->book_size);
12712
10.9k
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12713
10.9k
  return;
12714
10.9k
}
12715
12716
void
12717
sctp_send_deferred_reset_response(struct sctp_tcb *stcb,
12718
         struct sctp_stream_reset_list *ent,
12719
         int response)
12720
156
{
12721
156
  struct sctp_association *asoc;
12722
156
  struct sctp_tmit_chunk *chk;
12723
156
  struct sctp_chunkhdr *ch;
12724
12725
156
  asoc = &stcb->asoc;
12726
12727
  /*
12728
   * Reset our last reset action to the new one IP -> response
12729
   * (PERFORMED probably). This assures that if we fail to send, a
12730
   * retran from the peer will get the new response.
12731
   */
12732
156
  asoc->last_reset_action[0] = response;
12733
156
  if (asoc->stream_reset_outstanding) {
12734
41
    return;
12735
41
  }
12736
115
  sctp_alloc_a_chunk(stcb, chk);
12737
115
  if (chk == NULL) {
12738
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12739
0
    return;
12740
0
  }
12741
115
  chk->copy_by_ref = 0;
12742
115
  chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12743
115
  chk->rec.chunk_id.can_take_data = 0;
12744
115
  chk->flags = 0;
12745
115
  chk->asoc = &stcb->asoc;
12746
115
  chk->book_size = sizeof(struct sctp_chunkhdr);
12747
115
  chk->send_size = SCTP_SIZE32(chk->book_size);
12748
115
  chk->book_size_scale = 0;
12749
115
  chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12750
115
  if (chk->data == NULL) {
12751
0
    sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
12752
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12753
0
    return;
12754
0
  }
12755
115
  SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12756
  /* setup chunk parameters */
12757
115
  chk->sent = SCTP_DATAGRAM_UNSENT;
12758
115
  chk->snd_count = 0;
12759
115
  if (stcb->asoc.alternate) {
12760
2
    chk->whoTo = stcb->asoc.alternate;
12761
113
  } else {
12762
113
    chk->whoTo = stcb->asoc.primary_destination;
12763
113
  }
12764
115
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12765
115
  ch->chunk_type = SCTP_STREAM_RESET;
12766
115
  ch->chunk_flags = 0;
12767
115
  ch->chunk_length = htons(chk->book_size);
12768
115
  atomic_add_int(&chk->whoTo->ref_count, 1);
12769
115
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12770
115
  sctp_add_stream_reset_result(chk, ent->seq, response);
12771
  /* insert the chunk for sending */
12772
115
  TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12773
115
        chk,
12774
115
        sctp_next);
12775
115
  asoc->ctrl_queue_cnt++;
12776
115
}
12777
12778
void
12779
sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
12780
                                 uint32_t resp_seq, uint32_t result,
12781
                                 uint32_t send_una, uint32_t recv_next)
12782
518
{
12783
518
  uint16_t len, old_len;
12784
518
  struct sctp_stream_reset_response_tsn *resp;
12785
518
  struct sctp_chunkhdr *ch;
12786
12787
518
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12788
518
  old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12789
12790
  /* get to new offset for the param. */
12791
518
  resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
12792
  /* now how long will this param be? */
12793
518
  len = sizeof(struct sctp_stream_reset_response_tsn);
12794
518
  resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
12795
518
  resp->ph.param_length = htons(len);
12796
518
  resp->response_seq = htonl(resp_seq);
12797
518
  resp->result = htonl(result);
12798
518
  resp->senders_next_tsn = htonl(send_una);
12799
518
  resp->receivers_next_tsn = htonl(recv_next);
12800
12801
  /* now fix the chunk length */
12802
518
  ch->chunk_length = htons(len + old_len);
12803
518
  chk->book_size = len + old_len;
12804
518
  chk->send_size = SCTP_SIZE32(chk->book_size);
12805
518
  chk->book_size_scale = 0;
12806
518
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12807
518
  return;
12808
518
}
12809
12810
static void
12811
sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
12812
           uint32_t seq,
12813
           uint16_t adding)
12814
39
{
12815
39
  uint16_t len, old_len;
12816
39
  struct sctp_chunkhdr *ch;
12817
39
  struct sctp_stream_reset_add_strm *addstr;
12818
12819
39
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12820
39
  old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12821
12822
  /* get to new offset for the param. */
12823
39
  addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
12824
  /* now how long will this param be? */
12825
39
  len = sizeof(struct sctp_stream_reset_add_strm);
12826
12827
  /* Fill it out. */
12828
39
  addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
12829
39
  addstr->ph.param_length = htons(len);
12830
39
  addstr->request_seq = htonl(seq);
12831
39
  addstr->number_of_streams = htons(adding);
12832
39
  addstr->reserved = 0;
12833
12834
  /* now fix the chunk length */
12835
39
  ch->chunk_length = htons(len + old_len);
12836
39
  chk->send_size = len + old_len;
12837
39
  chk->book_size = SCTP_SIZE32(chk->send_size);
12838
39
  chk->book_size_scale = 0;
12839
39
  SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
12840
39
  return;
12841
39
}
12842
12843
static void
12844
sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
12845
                      uint32_t seq,
12846
                      uint16_t adding)
12847
0
{
12848
0
  uint16_t len, old_len;
12849
0
  struct sctp_chunkhdr *ch;
12850
0
  struct sctp_stream_reset_add_strm *addstr;
12851
12852
0
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12853
0
  old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
12854
12855
  /* get to new offset for the param. */
12856
0
  addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
12857
  /* now how long will this param be? */
12858
0
  len = sizeof(struct sctp_stream_reset_add_strm);
12859
  /* Fill it out. */
12860
0
  addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
12861
0
  addstr->ph.param_length = htons(len);
12862
0
  addstr->request_seq = htonl(seq);
12863
0
  addstr->number_of_streams = htons(adding);
12864
0
  addstr->reserved = 0;
12865
12866
  /* now fix the chunk length */
12867
0
  ch->chunk_length = htons(len + old_len);
12868
0
  chk->send_size = len + old_len;
12869
0
  chk->book_size = SCTP_SIZE32(chk->send_size);
12870
0
  chk->book_size_scale = 0;
12871
0
  SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
12872
0
  return;
12873
0
}
12874
12875
int
12876
sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked)
12877
5.78k
{
12878
5.78k
  struct sctp_association *asoc;
12879
5.78k
  struct sctp_tmit_chunk *chk;
12880
5.78k
  struct sctp_chunkhdr *ch;
12881
5.78k
  uint32_t seq;
12882
12883
5.78k
  asoc = &stcb->asoc;
12884
5.78k
  asoc->trigger_reset = 0;
12885
5.78k
  if (asoc->stream_reset_outstanding) {
12886
415
    return (EALREADY);
12887
415
  }
12888
5.37k
  sctp_alloc_a_chunk(stcb, chk);
12889
5.37k
  if (chk == NULL) {
12890
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12891
0
    return (ENOMEM);
12892
0
  }
12893
5.37k
  chk->copy_by_ref = 0;
12894
5.37k
  chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12895
5.37k
  chk->rec.chunk_id.can_take_data = 0;
12896
5.37k
  chk->flags = 0;
12897
5.37k
  chk->asoc = &stcb->asoc;
12898
5.37k
  chk->book_size = sizeof(struct sctp_chunkhdr);
12899
5.37k
  chk->send_size = SCTP_SIZE32(chk->book_size);
12900
5.37k
  chk->book_size_scale = 0;
12901
5.37k
  chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12902
5.37k
  if (chk->data == NULL) {
12903
0
    sctp_free_a_chunk(stcb, chk, so_locked);
12904
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12905
0
    return (ENOMEM);
12906
0
  }
12907
5.37k
  SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12908
12909
  /* setup chunk parameters */
12910
5.37k
  chk->sent = SCTP_DATAGRAM_UNSENT;
12911
5.37k
  chk->snd_count = 0;
12912
5.37k
  if (stcb->asoc.alternate) {
12913
654
    chk->whoTo = stcb->asoc.alternate;
12914
4.71k
  } else {
12915
4.71k
    chk->whoTo = stcb->asoc.primary_destination;
12916
4.71k
  }
12917
5.37k
  ch = mtod(chk->data, struct sctp_chunkhdr *);
12918
5.37k
  ch->chunk_type = SCTP_STREAM_RESET;
12919
5.37k
  ch->chunk_flags = 0;
12920
5.37k
  ch->chunk_length = htons(chk->book_size);
12921
5.37k
  atomic_add_int(&chk->whoTo->ref_count, 1);
12922
5.37k
  SCTP_BUF_LEN(chk->data) = chk->send_size;
12923
5.37k
  seq = stcb->asoc.str_reset_seq_out;
12924
5.37k
  if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) {
12925
83
    seq++;
12926
83
    asoc->stream_reset_outstanding++;
12927
5.28k
  } else {
12928
5.28k
    m_freem(chk->data);
12929
5.28k
    chk->data = NULL;
12930
5.28k
    sctp_free_a_chunk(stcb, chk, so_locked);
12931
5.28k
    return (ENOENT);
12932
5.28k
  }
12933
83
  asoc->str_reset = chk;
12934
  /* insert the chunk for sending */
12935
83
  TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12936
83
        chk,
12937
83
        sctp_next);
12938
83
  asoc->ctrl_queue_cnt++;
12939
12940
83
  if (stcb->asoc.send_sack) {
12941
58
    sctp_send_sack(stcb, so_locked);
12942
58
  }
12943
83
  sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12944
83
  return (0);
12945
5.37k
}
12946
12947
int
12948
sctp_send_str_reset_req(struct sctp_tcb *stcb,
12949
                        uint16_t number_entries, uint16_t *list,
12950
                        uint8_t send_in_req,
12951
                        uint8_t send_tsn_req,
12952
                        uint8_t add_stream,
12953
                        uint16_t adding_o,
12954
                        uint16_t adding_i, uint8_t peer_asked)
12955
1.71k
{
12956
1.71k
  struct sctp_association *asoc;
12957
1.71k
  struct sctp_tmit_chunk *chk;
12958
1.71k
  struct sctp_chunkhdr *ch;
12959
1.71k
  int can_send_out_req=0;
12960
1.71k
  uint32_t seq;
12961
12962
1.71k
  SCTP_TCB_LOCK_ASSERT(stcb);
12963
12964
1.71k
  asoc = &stcb->asoc;
12965
1.71k
  if (asoc->stream_reset_outstanding) {
12966
    /*-
12967
     * Already one pending, must get ACK back to clear the flag.
12968
     */
12969
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
12970
0
    return (EBUSY);
12971
0
  }
12972
1.71k
  if ((send_in_req == 0) && (send_tsn_req == 0) &&
12973
71
      (add_stream == 0)) {
12974
    /* nothing to do */
12975
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12976
0
    return (EINVAL);
12977
0
  }
12978
1.71k
  if (send_tsn_req && send_in_req) {
12979
    /* error, can't do that */
12980
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12981
0
    return (EINVAL);
12982
1.71k
  } else if (send_in_req) {
12983
1.64k
    can_send_out_req = 1;
12984
1.64k
  }
12985
1.71k
  if (number_entries > (MCLBYTES -
12986
1.71k
                        SCTP_MIN_OVERHEAD -
12987
1.71k
                        sizeof(struct sctp_chunkhdr) -
12988
1.71k
                        sizeof(struct sctp_stream_reset_out_request)) /
12989
1.71k
                       sizeof(uint16_t)) {
12990
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12991
0
    return (ENOMEM);
12992
0
  }
12993
1.71k
  sctp_alloc_a_chunk(stcb, chk);
12994
1.71k
  if (chk == NULL) {
12995
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12996
0
    return (ENOMEM);
12997
0
  }
12998
1.71k
  chk->copy_by_ref = 0;
12999
1.71k
  chk->rec.chunk_id.id = SCTP_STREAM_RESET;
13000
1.71k
  chk->rec.chunk_id.can_take_data = 0;
13001
1.71k
  chk->flags = 0;
13002
1.71k
  chk->asoc = &stcb->asoc;
13003
1.71k
  chk->book_size = sizeof(struct sctp_chunkhdr);
13004
1.71k
  chk->send_size = SCTP_SIZE32(chk->book_size);
13005
1.71k
  chk->book_size_scale = 0;
13006
1.71k
  chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
13007
1.71k
  if (chk->data == NULL) {
13008
0
    sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
13009
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13010
0
    return (ENOMEM);
13011
0
  }
13012
1.71k
  SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
13013
13014
  /* setup chunk parameters */
13015
1.71k
  chk->sent = SCTP_DATAGRAM_UNSENT;
13016
1.71k
  chk->snd_count = 0;
13017
1.71k
  if (stcb->asoc.alternate) {
13018
8
    chk->whoTo = stcb->asoc.alternate;
13019
1.70k
  } else {
13020
1.70k
    chk->whoTo = stcb->asoc.primary_destination;
13021
1.70k
  }
13022
1.71k
  atomic_add_int(&chk->whoTo->ref_count, 1);
13023
1.71k
  ch = mtod(chk->data, struct sctp_chunkhdr *);
13024
1.71k
  ch->chunk_type = SCTP_STREAM_RESET;
13025
1.71k
  ch->chunk_flags = 0;
13026
1.71k
  ch->chunk_length = htons(chk->book_size);
13027
1.71k
  SCTP_BUF_LEN(chk->data) = chk->send_size;
13028
13029
1.71k
  seq = stcb->asoc.str_reset_seq_out;
13030
1.71k
  if (can_send_out_req) {
13031
1.64k
    int ret;
13032
13033
1.64k
    ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
13034
1.64k
    if (ret) {
13035
1.64k
      seq++;
13036
1.64k
      asoc->stream_reset_outstanding++;
13037
1.64k
    }
13038
1.64k
  }
13039
1.71k
  if ((add_stream & 1) &&
13040
71
      ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
13041
    /* Need to allocate more */
13042
39
    struct sctp_stream_out *oldstream;
13043
39
    struct sctp_stream_queue_pending *sp, *nsp;
13044
39
    int i;
13045
#if defined(SCTP_DETAILED_STR_STATS)
13046
    int j;
13047
#endif
13048
13049
39
    oldstream = stcb->asoc.strmout;
13050
    /* get some more */
13051
39
    SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
13052
39
          (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out),
13053
39
          SCTP_M_STRMO);
13054
39
    if (stcb->asoc.strmout == NULL) {
13055
0
      uint8_t x;
13056
0
      stcb->asoc.strmout = oldstream;
13057
      /* Turn off the bit */
13058
0
      x = add_stream & 0xfe;
13059
0
      add_stream = x;
13060
0
      goto skip_stuff;
13061
0
    }
13062
    /* Ok now we proceed with copying the old out stuff and
13063
     * initializing the new stuff.
13064
     */
13065
39
    stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, false);
13066
10.0k
    for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
13067
10.0k
      TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
13068
      /* FIX ME FIX ME */
13069
      /* This should be a SS_COPY operation FIX ME STREAM SCHEDULER EXPERT */
13070
10.0k
      stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]);
13071
10.0k
      stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues;
13072
#if defined(SCTP_DETAILED_STR_STATS)
13073
      for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
13074
        stcb->asoc.strmout[i].abandoned_sent[j] = oldstream[i].abandoned_sent[j];
13075
        stcb->asoc.strmout[i].abandoned_unsent[j] = oldstream[i].abandoned_unsent[j];
13076
      }
13077
#else
13078
10.0k
      stcb->asoc.strmout[i].abandoned_sent[0] = oldstream[i].abandoned_sent[0];
13079
10.0k
      stcb->asoc.strmout[i].abandoned_unsent[0] = oldstream[i].abandoned_unsent[0];
13080
10.0k
#endif
13081
10.0k
      stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered;
13082
10.0k
      stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered;
13083
10.0k
      stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
13084
10.0k
      stcb->asoc.strmout[i].sid = i;
13085
10.0k
      stcb->asoc.strmout[i].state = oldstream[i].state;
13086
      /* now anything on those queues? */
13087
10.0k
      TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
13088
4
        TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
13089
4
        TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
13090
4
      }
13091
10.0k
    }
13092
    /* now the new streams */
13093
39
    stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
13094
1.49M
    for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
13095
1.49M
      TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
13096
1.49M
      stcb->asoc.strmout[i].chunks_on_queues = 0;
13097
#if defined(SCTP_DETAILED_STR_STATS)
13098
      for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
13099
        stcb->asoc.strmout[i].abandoned_sent[j] = 0;
13100
        stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
13101
      }
13102
#else
13103
1.49M
      stcb->asoc.strmout[i].abandoned_sent[0] = 0;
13104
1.49M
      stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
13105
1.49M
#endif
13106
1.49M
      stcb->asoc.strmout[i].next_mid_ordered = 0;
13107
1.49M
      stcb->asoc.strmout[i].next_mid_unordered = 0;
13108
1.49M
      stcb->asoc.strmout[i].sid = i;
13109
1.49M
      stcb->asoc.strmout[i].last_msg_incomplete = 0;
13110
1.49M
      stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
13111
1.49M
      stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED;
13112
1.49M
    }
13113
39
    stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
13114
39
    SCTP_FREE(oldstream, SCTP_M_STRMO);
13115
39
  }
13116
1.71k
skip_stuff:
13117
1.71k
  if ((add_stream & 1) && (adding_o > 0)) {
13118
39
    asoc->strm_pending_add_size = adding_o;
13119
39
    asoc->peer_req_out = peer_asked;
13120
39
    sctp_add_an_out_stream(chk, seq, adding_o);
13121
39
    seq++;
13122
39
    asoc->stream_reset_outstanding++;
13123
39
  }
13124
1.71k
  if ((add_stream & 2) && (adding_i > 0)) {
13125
0
    sctp_add_an_in_stream(chk, seq, adding_i);
13126
0
    seq++;
13127
0
    asoc->stream_reset_outstanding++;
13128
0
  }
13129
1.71k
  if (send_in_req) {
13130
1.64k
    sctp_add_stream_reset_in(chk, number_entries, list, seq);
13131
1.64k
    seq++;
13132
1.64k
    asoc->stream_reset_outstanding++;
13133
1.64k
  }
13134
1.71k
  if (send_tsn_req) {
13135
0
    sctp_add_stream_reset_tsn(chk, seq);
13136
0
    asoc->stream_reset_outstanding++;
13137
0
  }
13138
1.71k
  asoc->str_reset = chk;
13139
  /* insert the chunk for sending */
13140
1.71k
  TAILQ_INSERT_TAIL(&asoc->control_send_queue,
13141
1.71k
        chk,
13142
1.71k
        sctp_next);
13143
1.71k
  asoc->ctrl_queue_cnt++;
13144
1.71k
  if (stcb->asoc.send_sack) {
13145
1.70k
    sctp_send_sack(stcb, SCTP_SO_LOCKED);
13146
1.70k
  }
13147
1.71k
  sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
13148
1.71k
  return (0);
13149
1.71k
}
13150
13151
void
13152
sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst,
13153
                struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
13154
#if defined(__FreeBSD__) && !defined(__Userspace__)
13155
                uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
13156
#endif
13157
                uint32_t vrf_id, uint16_t port)
13158
1.94k
{
13159
  /* Don't respond to an ABORT with an ABORT. */
13160
1.94k
  if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
13161
1
    if (cause)
13162
0
      sctp_m_freem(cause);
13163
1
    return;
13164
1
  }
13165
1.94k
  sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause,
13166
#if defined(__FreeBSD__) && !defined(__Userspace__)
13167
                     mflowtype, mflowid, fibnum,
13168
#endif
13169
1.94k
                     vrf_id, port);
13170
1.94k
  return;
13171
1.94k
}
13172
13173
void
13174
sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst,
13175
                   struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
13176
#if defined(__FreeBSD__) && !defined(__Userspace__)
13177
                   uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
13178
#endif
13179
                   uint32_t vrf_id, uint16_t port)
13180
6.99k
{
13181
6.99k
  sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause,
13182
#if defined(__FreeBSD__) && !defined(__Userspace__)
13183
                     mflowtype, mflowid, fibnum,
13184
#endif
13185
6.99k
                     vrf_id, port);
13186
6.99k
  return;
13187
6.99k
}
13188
13189
static struct mbuf *
13190
sctp_copy_resume(struct uio *uio,
13191
     int max_send_len,
13192
#if defined(__FreeBSD__) || defined(__Userspace__)
13193
     int user_marks_eor,
13194
#endif
13195
     int *error,
13196
     uint32_t *sndout,
13197
     struct mbuf **new_tail)
13198
0
{
13199
0
#if defined(__FreeBSD__) || defined(__Userspace__)
13200
0
  struct mbuf *m;
13201
13202
0
  m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
13203
0
    (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
13204
0
  if (m == NULL) {
13205
    /* The only possible error is EFAULT. */
13206
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
13207
0
    *error = EFAULT;
13208
0
  } else {
13209
0
    *sndout = m_length(m, NULL);
13210
0
    *new_tail = m_last(m);
13211
0
  }
13212
0
  return (m);
13213
#else
13214
  int left, cancpy, willcpy;
13215
  struct mbuf *m, *head;
13216
13217
#if defined(__APPLE__) && !defined(__Userspace__)
13218
#if defined(APPLE_LEOPARD)
13219
  left = (int)min(uio->uio_resid, max_send_len);
13220
#else
13221
  left = (int)min(uio_resid(uio), max_send_len);
13222
#endif
13223
#else
13224
  left = (int)min(uio->uio_resid, max_send_len);
13225
#endif
13226
  /* Always get a header just in case */
13227
  head = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA);
13228
  if (head == NULL) {
13229
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
13230
    *error = ENOBUFS;
13231
    return (NULL);
13232
  }
13233
  cancpy = (int)M_TRAILINGSPACE(head);
13234
  willcpy = min(cancpy, left);
13235
  *error = uiomove(mtod(head, caddr_t), willcpy, uio);
13236
  if (*error != 0) {
13237
    sctp_m_freem(head);
13238
    return (NULL);
13239
  }
13240
  *sndout += willcpy;
13241
  left -= willcpy;
13242
  SCTP_BUF_LEN(head) = willcpy;
13243
  m = head;
13244
  *new_tail = head;
13245
  while (left > 0) {
13246
    /* move in user data */
13247
    SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA);
13248
    if (SCTP_BUF_NEXT(m) == NULL) {
13249
      sctp_m_freem(head);
13250
      *new_tail = NULL;
13251
      SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
13252
      *error = ENOBUFS;
13253
      return (NULL);
13254
    }
13255
    m = SCTP_BUF_NEXT(m);
13256
    cancpy = (int)M_TRAILINGSPACE(m);
13257
    willcpy = min(cancpy, left);
13258
    *error = uiomove(mtod(m, caddr_t), willcpy, uio);
13259
    if (*error != 0) {
13260
      sctp_m_freem(head);
13261
      *new_tail = NULL;
13262
      SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, *error);
13263
      return (NULL);
13264
    }
13265
    SCTP_BUF_LEN(m) = willcpy;
13266
    left -= willcpy;
13267
    *sndout += willcpy;
13268
    *new_tail = m;
13269
    if (left == 0) {
13270
      SCTP_BUF_NEXT(m) = NULL;
13271
    }
13272
  }
13273
  return (head);
13274
#endif
13275
0
}
13276
13277
static int
13278
sctp_copy_one(struct sctp_stream_queue_pending *sp,
13279
              struct uio *uio,
13280
              int resv_upfront)
13281
24.7k
{
13282
24.7k
#if defined(__FreeBSD__) || defined(__Userspace__)
13283
24.7k
  sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, resv_upfront, 0);
13284
24.7k
  if (sp->data == NULL) {
13285
    /* The only possible error is EFAULT. */
13286
0
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
13287
0
    return (EFAULT);
13288
0
  }
13289
24.7k
  sp->tail_mbuf = m_last(sp->data);
13290
24.7k
  return (0);
13291
#else
13292
  int left;
13293
  int cancpy, willcpy, error;
13294
  struct mbuf *m, *head;
13295
  int cpsz = 0;
13296
13297
  /* First one gets a header */
13298
  left = sp->length;
13299
  head = m = sctp_get_mbuf_for_msg((left + resv_upfront), 0, M_WAITOK, 0, MT_DATA);
13300
  if (m == NULL) {
13301
    SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
13302
    return (ENOBUFS);
13303
  }
13304
  /*-
13305
   * Add this one for m in now, that way if the alloc fails we won't
13306
   * have a bad cnt.
13307
   */
13308
  SCTP_BUF_RESV_UF(m, resv_upfront);
13309
  cancpy = (int)M_TRAILINGSPACE(m);
13310
  willcpy = min(cancpy, left);
13311
  while (left > 0) {
13312
    /* move in user data */
13313
    error = uiomove(mtod(m, caddr_t), willcpy, uio);
13314
    if (error) {
13315
      sctp_m_freem(head);
13316
      return (error);
13317
    }
13318
    SCTP_BUF_LEN(m) = willcpy;
13319
    left -= willcpy;
13320
    cpsz += willcpy;
13321
    if (left > 0) {
13322
      SCTP_BUF_NEXT(m) = sctp_get_mbuf_for_msg(left, 0, M_WAITOK, 0, MT_DATA);
13323
      if (SCTP_BUF_NEXT(m) == NULL) {
13324
        /*
13325
         * the head goes back to caller, he can free
13326
         * the rest
13327
         */
13328
        sctp_m_freem(head);
13329
        SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOBUFS);
13330
        return (ENOBUFS);
13331
      }
13332
      m = SCTP_BUF_NEXT(m);
13333
      cancpy = (int)M_TRAILINGSPACE(m);
13334
      willcpy = min(cancpy, left);
13335
    } else {
13336
      sp->tail_mbuf = m;
13337
      SCTP_BUF_NEXT(m) = NULL;
13338
    }
13339
  }
13340
  sp->data = head;
13341
  sp->length = cpsz;
13342
  return (0);
13343
#endif
13344
24.7k
}
13345
13346
static struct sctp_stream_queue_pending *
13347
sctp_copy_it_in(struct sctp_tcb *stcb,
13348
    struct sctp_association *asoc,
13349
    struct sctp_nonpad_sndrcvinfo *srcv,
13350
    struct uio *uio,
13351
    struct sctp_nets *net,
13352
    ssize_t max_send_len,
13353
    int user_marks_eor,
13354
    int *error)
13355
13356
24.7k
{
13357
  /*-
13358
   * This routine must be very careful in its work. Protocol
13359
   * processing is up and running so care must be taken to spl...()
13360
   * when you need to do something that may effect the stcb/asoc. The
13361
   * sb is locked however. When data is copied the protocol processing
13362
   * should be enabled since this is a slower operation...
13363
   */
13364
24.7k
  struct sctp_stream_queue_pending *sp;
13365
24.7k
  int resv_in_first;
13366
13367
24.7k
  *error = 0;
13368
24.7k
  sctp_alloc_a_strmoq(stcb, sp);
13369
24.7k
  if (sp == NULL) {
13370
0
    SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
13371
0
    *error = ENOMEM;
13372
0
    goto out_now;
13373
0
  }
13374
24.7k
  sp->act_flags = 0;
13375
24.7k
  sp->sender_all_done = 0;
13376
24.7k
  sp->sinfo_flags = srcv->sinfo_flags;
13377
24.7k
  sp->timetolive = srcv->sinfo_timetolive;
13378
24.7k
  sp->ppid = srcv->sinfo_ppid;
13379
24.7k
  sp->context = srcv->sinfo_context;
13380
24.7k
  sp->fsn = 0;
13381
24.7k
  (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
13382
24.7k
  sp->sid = srcv->sinfo_stream;
13383
#if defined(__APPLE__) && !defined(__Userspace__)
13384
#if defined(APPLE_LEOPARD)
13385
  sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
13386
#else
13387
  sp->length = (uint32_t)min(uio_resid(uio), max_send_len);
13388
#endif
13389
#else
13390
24.7k
  sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
13391
24.7k
#endif
13392
#if defined(__APPLE__) && !defined(__Userspace__)
13393
#if defined(APPLE_LEOPARD)
13394
  if ((sp->length == (uint32_t)uio->uio_resid) &&
13395
#else
13396
  if ((sp->length == (uint32_t)uio_resid(uio)) &&
13397
#endif
13398
#else
13399
24.7k
  if ((sp->length == (uint32_t)uio->uio_resid) &&
13400
24.7k
#endif
13401
24.7k
      ((user_marks_eor == 0) ||
13402
0
       (srcv->sinfo_flags & SCTP_EOF) ||
13403
24.7k
       (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
13404
24.7k
    sp->msg_is_complete = 1;
13405
24.7k
  } else {
13406
0
    sp->msg_is_complete = 0;
13407
0
  }
13408
24.7k
  sp->sender_all_done = 0;
13409
24.7k
  sp->some_taken = 0;
13410
24.7k
  sp->put_last_out = 0;
13411
24.7k
  resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb);
13412
24.7k
  sp->data = sp->tail_mbuf = NULL;
13413
24.7k
  if (sp->length == 0) {
13414
0
    goto skip_copy;
13415
0
  }
13416
24.7k
  if (srcv->sinfo_keynumber_valid) {
13417
0
    sp->auth_keyid = srcv->sinfo_keynumber;
13418
24.7k
  } else {
13419
24.7k
    sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
13420
24.7k
  }
13421
24.7k
  if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
13422
0
    sctp_auth_key_acquire(stcb, sp->auth_keyid);
13423
0
    sp->holds_key_ref = 1;
13424
0
  }
13425
#if defined(__APPLE__) && !defined(__Userspace__)
13426
  SCTP_SOCKET_UNLOCK(SCTP_INP_SO(stcb->sctp_ep), 0);
13427
#endif
13428
24.7k
  *error = sctp_copy_one(sp, uio, resv_in_first);
13429
#if defined(__APPLE__) && !defined(__Userspace__)
13430
  SCTP_SOCKET_LOCK(SCTP_INP_SO(stcb->sctp_ep), 0);
13431
#endif
13432
24.7k
 skip_copy:
13433
24.7k
  if (*error) {
13434
0
#if defined(__Userspace__)
13435
0
    SCTP_TCB_LOCK(stcb);
13436
0
#endif
13437
0
    sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
13438
0
#if defined(__Userspace__)
13439
0
    SCTP_TCB_UNLOCK(stcb);
13440
0
#endif
13441
0
    sp = NULL;
13442
24.7k
  } else {
13443
24.7k
    if (sp->sinfo_flags & SCTP_ADDR_OVER) {
13444
0
      sp->net = net;
13445
0
      atomic_add_int(&sp->net->ref_count, 1);
13446
24.7k
    } else {
13447
24.7k
      sp->net = NULL;
13448
24.7k
    }
13449
24.7k
    sctp_set_prsctp_policy(sp);
13450
24.7k
  }
13451
24.7k
out_now:
13452
24.7k
  return (sp);
13453
24.7k
}
13454
13455
int
13456
sctp_sosend(struct socket *so,
13457
            struct sockaddr *addr,
13458
            struct uio *uio,
13459
            struct mbuf *top,
13460
            struct mbuf *control,
13461
#if defined(__APPLE__) && !defined(__Userspace__)
13462
            int flags)
13463
#else
13464
            int flags,
13465
#if defined(__FreeBSD__) && !defined(__Userspace__)
13466
            struct thread *p)
13467
#elif defined(_WIN32) && !defined(__Userspace__)
13468
            PKTHREAD p)
13469
#else
13470
#if defined(__Userspace__)
13471
            /*
13472
       * proc is a dummy in __Userspace__ and will not be passed
13473
       * to sctp_lower_sosend
13474
       */
13475
#endif
13476
            struct proc *p)
13477
#endif
13478
#endif
13479
0
{
13480
0
  struct sctp_sndrcvinfo sndrcvninfo;
13481
0
#if defined(INET) && defined(INET6)
13482
0
  struct sockaddr_in sin;
13483
0
#endif
13484
0
  struct sockaddr *addr_to_use;
13485
#if defined(__APPLE__) && !defined(__Userspace__)
13486
  struct proc *p = current_proc();
13487
#endif
13488
0
  int error;
13489
0
  bool use_sndinfo;
13490
13491
0
  if (control != NULL) {
13492
    /* process cmsg snd/rcv info (maybe a assoc-id) */
13493
0
    use_sndinfo = sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, sizeof(sndrcvninfo));
13494
0
  } else {
13495
0
    use_sndinfo = false;
13496
0
  }
13497
0
#if defined(INET) && defined(INET6)
13498
0
  if ((addr != NULL) && (addr->sa_family == AF_INET6)) {
13499
0
    struct sockaddr_in6 *sin6;
13500
13501
#ifdef HAVE_SA_LEN
13502
    if (addr->sa_len != sizeof(struct sockaddr_in6)) {
13503
      SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
13504
      return (EINVAL);
13505
    }
13506
#endif
13507
0
    sin6 = (struct sockaddr_in6 *)addr;
13508
0
    if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
13509
0
      in6_sin6_2_sin(&sin, sin6);
13510
0
      addr_to_use = (struct sockaddr *)&sin;
13511
0
    } else {
13512
0
      addr_to_use = addr;
13513
0
    }
13514
0
  } else {
13515
0
    addr_to_use = addr;
13516
0
  }
13517
#else
13518
  addr_to_use = addr;
13519
#endif
13520
#if defined(__APPLE__) && !defined(__Userspace__)
13521
  SCTP_SOCKET_LOCK(so, 1);
13522
#endif
13523
0
  error = sctp_lower_sosend(so, addr_to_use, uio, top, control, flags,
13524
0
#if defined(__Userspace__)
13525
0
                            use_sndinfo ? &sndrcvninfo : NULL);
13526
#else
13527
                            use_sndinfo ? &sndrcvninfo : NULL, p);
13528
#endif
13529
#if defined(__APPLE__) && !defined(__Userspace__)
13530
  SCTP_SOCKET_UNLOCK(so, 1);
13531
#endif
13532
0
  return (error);
13533
0
}
13534
13535
int
13536
sctp_lower_sosend(struct socket *so,
13537
                  struct sockaddr *addr,
13538
                  struct uio *uio,
13539
                  struct mbuf *top,
13540
                  struct mbuf *control,
13541
                  int flags,
13542
#if defined(__Userspace__)
13543
                  struct sctp_sndrcvinfo *srcv)
13544
#else
13545
                  struct sctp_sndrcvinfo *srcv,
13546
#if defined(__FreeBSD__)
13547
                  struct thread *p)
13548
#elif defined(_WIN32)
13549
                  PKTHREAD p)
13550
#else
13551
                  struct proc *p)
13552
#endif
13553
#endif
13554
10.8M
{
13555
10.8M
  struct sctp_nonpad_sndrcvinfo sndrcvninfo_buf;
13556
#if defined(__FreeBSD__) && !defined(__Userspace__)
13557
  struct epoch_tracker et;
13558
#endif
13559
10.8M
  struct timeval now;
13560
10.8M
  struct sctp_block_entry be;
13561
10.8M
  struct sctp_inpcb *inp;
13562
10.8M
  struct sctp_tcb *stcb = NULL;
13563
10.8M
  struct sctp_nets *net;
13564
10.8M
  struct sctp_association *asoc;
13565
10.8M
  struct sctp_inpcb *t_inp;
13566
10.8M
  struct sctp_nonpad_sndrcvinfo *sndrcvninfo;
13567
10.8M
  ssize_t sndlen = 0, max_len, local_add_more;
13568
10.8M
  ssize_t local_soresv = 0;
13569
10.8M
  sctp_assoc_t sinfo_assoc_id;
13570
10.8M
  int user_marks_eor;
13571
10.8M
  int nagle_applies = 0;
13572
10.8M
  int error;
13573
10.8M
  int queue_only = 0, queue_only_for_init = 0;
13574
10.8M
  int un_sent;
13575
10.8M
  int now_filled = 0;
13576
10.8M
  unsigned int inqueue_bytes = 0;
13577
10.8M
  uint16_t port;
13578
10.8M
  uint16_t sinfo_flags;
13579
10.8M
  uint16_t sinfo_stream;
13580
10.8M
  bool create_lock_applied = false;
13581
10.8M
  bool free_cnt_applied = false;
13582
10.8M
  bool some_on_control;
13583
10.8M
  bool got_all_of_the_send = false;
13584
10.8M
  bool non_blocking = false;
13585
13586
10.8M
  error = 0;
13587
10.8M
  net = NULL;
13588
10.8M
  stcb = NULL;
13589
13590
#if defined(__APPLE__) && !defined(__Userspace__)
13591
  sctp_lock_assert(so);
13592
#endif
13593
10.8M
  if ((uio == NULL) && (top == NULL)) {
13594
0
    error = EINVAL;
13595
0
    goto out_unlocked;
13596
0
  }
13597
10.8M
  if (addr != NULL) {
13598
0
    union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
13599
13600
0
    switch (raddr->sa.sa_family) {
13601
0
#ifdef INET
13602
0
    case AF_INET:
13603
#ifdef HAVE_SIN_LEN
13604
      if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
13605
        error = EINVAL;
13606
        goto out_unlocked;
13607
      }
13608
#endif
13609
0
      port = raddr->sin.sin_port;
13610
0
      break;
13611
0
#endif
13612
0
#ifdef INET6
13613
0
    case AF_INET6:
13614
#ifdef HAVE_SIN6_LEN
13615
      if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
13616
        error = EINVAL;
13617
        goto out_unlocked;
13618
      }
13619
#endif
13620
0
      port = raddr->sin6.sin6_port;
13621
0
      break;
13622
0
#endif
13623
0
#if defined(__Userspace__)
13624
0
    case AF_CONN:
13625
#ifdef HAVE_SCONN_LEN
13626
      if (raddr->sconn.sconn_len != sizeof(struct sockaddr_conn)) {
13627
        error = EINVAL;
13628
        goto out_unlocked;
13629
      }
13630
#endif
13631
0
      port = raddr->sconn.sconn_port;
13632
0
      break;
13633
0
#endif
13634
0
    default:
13635
0
      error = EAFNOSUPPORT;
13636
0
      goto out_unlocked;
13637
0
    }
13638
10.8M
  } else {
13639
10.8M
    port = 0;
13640
10.8M
  }
13641
10.8M
  if (uio != NULL) {
13642
#if defined(__APPLE__) && !defined(__Userspace__)
13643
#if defined(APPLE_LEOPARD)
13644
    if (uio->uio_resid < 0) {
13645
#else
13646
    if (uio_resid(uio) < 0) {
13647
#endif
13648
#else
13649
10.8M
    if (uio->uio_resid < 0) {
13650
0
#endif
13651
0
      error = EINVAL;
13652
0
      goto out_unlocked;
13653
0
    }
13654
#if defined(__APPLE__) && !defined(__Userspace__)
13655
#if defined(APPLE_LEOPARD)
13656
    sndlen = uio->uio_resid;
13657
#else
13658
    sndlen = uio_resid(uio);
13659
#endif
13660
#else
13661
10.8M
    sndlen = uio->uio_resid;
13662
10.8M
#endif
13663
10.8M
  } else {
13664
0
    sndlen = SCTP_HEADER_LEN(top);
13665
0
  }
13666
10.8M
  SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zd\n",
13667
10.8M
          (void *)addr, sndlen);
13668
13669
10.8M
  t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
13670
10.8M
  if (inp == NULL) {
13671
0
    error = EINVAL;
13672
0
    goto out_unlocked;
13673
0
  }
13674
10.8M
  user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
13675
10.8M
  if ((uio == NULL) && (user_marks_eor != 0)) {
13676
    /*-
13677
     * We do not support eeor mode for
13678
     * sending with mbuf chains (like sendfile).
13679
     */
13680
0
    error = EINVAL;
13681
0
    goto out_unlocked;
13682
0
  }
13683
10.8M
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
13684
10.8M
      SCTP_IS_LISTENING(inp)) {
13685
    /* The listener can NOT send. */
13686
0
    error = EINVAL;
13687
0
    goto out_unlocked;
13688
0
  }
13689
10.8M
  atomic_add_int(&inp->total_sends, 1);
13690
13691
10.8M
  if (srcv != NULL) {
13692
0
    sndrcvninfo = (struct sctp_nonpad_sndrcvinfo *)srcv;
13693
0
    sinfo_assoc_id = sndrcvninfo->sinfo_assoc_id;
13694
0
    sinfo_flags = sndrcvninfo->sinfo_flags;
13695
0
    if (INVALID_SINFO_FLAG(sinfo_flags) ||
13696
0
        PR_SCTP_INVALID_POLICY(sinfo_flags)) {
13697
0
      error = EINVAL;
13698
0
      goto out_unlocked;
13699
0
    }
13700
0
    if (sinfo_flags != 0) {
13701
0
      SCTP_STAT_INCR(sctps_sends_with_flags);
13702
0
    }
13703
10.8M
  } else {
13704
10.8M
    sndrcvninfo = NULL;
13705
10.8M
    sinfo_flags = inp->def_send.sinfo_flags;
13706
10.8M
    sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
13707
10.8M
  }
13708
#if defined(__FreeBSD__) && !defined(__Userspace__)
13709
  if (flags & MSG_EOR) {
13710
    sinfo_flags |= SCTP_EOR;
13711
  }
13712
  if (flags & MSG_EOF) {
13713
    sinfo_flags |= SCTP_EOF;
13714
  }
13715
#endif
13716
10.8M
  if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
13717
0
    error = EINVAL;
13718
0
    goto out_unlocked;
13719
0
  }
13720
10.8M
  SCTP_INP_RLOCK(inp);
13721
10.8M
  if ((sinfo_flags & SCTP_SENDALL) &&
13722
0
      (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
13723
0
    SCTP_INP_RUNLOCK(inp);
13724
0
    error = sctp_sendall(inp, uio, top, sndrcvninfo);
13725
0
    top = NULL;
13726
0
    goto out_unlocked;
13727
0
  }
13728
  /* Now we must find the association. */
13729
10.8M
  if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
13730
10.8M
      (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
13731
10.8M
    stcb = LIST_FIRST(&inp->sctp_asoc_list);
13732
10.8M
    if (stcb != NULL) {
13733
10.8M
      SCTP_TCB_LOCK(stcb);
13734
10.8M
    }
13735
10.8M
    SCTP_INP_RUNLOCK(inp);
13736
10.8M
  } else if (sinfo_assoc_id > SCTP_ALL_ASSOC) {
13737
0
    stcb = sctp_findasoc_ep_asocid_locked(inp, sinfo_assoc_id, 1);
13738
0
    SCTP_INP_RUNLOCK(inp);
13739
0
    if (stcb != NULL) {
13740
0
      SCTP_TCB_LOCK_ASSERT(stcb);
13741
0
    }
13742
0
  } else if (addr != NULL) {
13743
    /*-
13744
     * Since we did not use findep we must
13745
     * increment it, and if we don't find a tcb
13746
     * decrement it.
13747
     */
13748
0
    SCTP_INP_INCR_REF(inp);
13749
0
    SCTP_INP_RUNLOCK(inp);
13750
0
    stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
13751
0
    if (stcb == NULL) {
13752
0
      SCTP_INP_WLOCK(inp);
13753
0
      SCTP_INP_DECR_REF(inp);
13754
0
      SCTP_INP_WUNLOCK(inp);
13755
0
    } else {
13756
0
      SCTP_TCB_LOCK_ASSERT(stcb);
13757
0
    }
13758
0
  } else {
13759
0
    SCTP_INP_RUNLOCK(inp);
13760
0
  }
13761
13762
10.8M
#ifdef INVARIANTS
13763
10.8M
  if (stcb != NULL) {
13764
10.8M
    SCTP_TCB_LOCK_ASSERT(stcb);
13765
10.8M
  }
13766
10.8M
#endif
13767
13768
10.8M
  if ((stcb == NULL) && (addr != NULL)) {
13769
    /* Possible implicit send? */
13770
0
    SCTP_ASOC_CREATE_LOCK(inp);
13771
0
    create_lock_applied = true;
13772
0
    if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
13773
0
        (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
13774
0
      error = EINVAL;
13775
0
      goto out_unlocked;
13776
0
    }
13777
0
    if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
13778
0
        (addr->sa_family == AF_INET6)) {
13779
0
      error = EINVAL;
13780
0
      goto out_unlocked;
13781
0
    }
13782
0
    SCTP_INP_WLOCK(inp);
13783
0
    SCTP_INP_INCR_REF(inp);
13784
0
    SCTP_INP_WUNLOCK(inp);
13785
    /* With the lock applied look again */
13786
0
    stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
13787
0
#if defined(INET) || defined(INET6)
13788
0
    if ((stcb == NULL) && (control != NULL) && (port > 0)) {
13789
0
      stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
13790
0
    }
13791
0
#endif
13792
0
    if (stcb == NULL) {
13793
0
      SCTP_INP_WLOCK(inp);
13794
0
      SCTP_INP_DECR_REF(inp);
13795
0
      SCTP_INP_WUNLOCK(inp);
13796
0
    } else {
13797
0
      SCTP_TCB_LOCK_ASSERT(stcb);
13798
0
      SCTP_ASOC_CREATE_UNLOCK(inp);
13799
0
      create_lock_applied = false;
13800
0
    }
13801
0
    if (error != 0) {
13802
0
      goto out_unlocked;
13803
0
    }
13804
0
    if (t_inp != inp) {
13805
0
      error = ENOTCONN;
13806
0
      goto out_unlocked;
13807
0
    }
13808
0
  }
13809
10.8M
  if (stcb == NULL) {
13810
0
    if (addr == NULL) {
13811
0
      error = ENOENT;
13812
0
      goto out_unlocked;
13813
0
    } else {
13814
      /* We must go ahead and start the INIT process */
13815
0
      uint32_t vrf_id;
13816
13817
0
      if ((sinfo_flags & SCTP_ABORT) ||
13818
0
          ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
13819
        /*-
13820
         * User asks to abort a non-existent assoc,
13821
         * or EOF a non-existent assoc with no data
13822
         */
13823
0
        error = ENOENT;
13824
0
        goto out_unlocked;
13825
0
      }
13826
      /* get an asoc/stcb struct */
13827
0
      vrf_id = inp->def_vrf_id;
13828
0
      KASSERT(create_lock_applied, ("create_lock_applied is false"));
13829
0
      stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
13830
0
                                       inp->sctp_ep.pre_open_stream_count,
13831
0
                                       inp->sctp_ep.port,
13832
#if !defined(__Userspace__)
13833
                                       p,
13834
#else
13835
0
                                       (struct proc *)NULL,
13836
0
#endif
13837
0
                                       SCTP_INITIALIZE_AUTH_PARAMS);
13838
0
      if (stcb == NULL) {
13839
        /* error is setup for us in the call. */
13840
0
        KASSERT(error != 0, ("error is 0 although stcb is NULL"));
13841
0
        goto out_unlocked;
13842
0
      }
13843
0
      SCTP_TCB_LOCK_ASSERT(stcb);
13844
0
      SCTP_ASOC_CREATE_UNLOCK(inp);
13845
0
      create_lock_applied = false;
13846
      /* Turn on queue only flag to prevent data from being sent */
13847
0
      queue_only = 1;
13848
0
      SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13849
0
      (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
13850
0
      if (control != NULL) {
13851
0
        if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
13852
0
          sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
13853
0
                          SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
13854
0
          stcb = NULL;
13855
0
          KASSERT(error != 0,
13856
0
              ("error is 0 although sctp_process_cmsgs_for_init() indicated an error"));
13857
0
          goto out_unlocked;
13858
0
        }
13859
0
      }
13860
      /* out with the INIT */
13861
0
      queue_only_for_init = 1;
13862
      /*-
13863
       * we may want to dig in after this call and adjust the MTU
13864
       * value. It defaulted to 1500 (constant) but the ro
13865
       * structure may now have an update and thus we may need to
13866
       * change it BEFORE we append the message.
13867
       */
13868
0
    }
13869
0
  }
13870
13871
10.8M
  KASSERT(!create_lock_applied, ("create_lock_applied is true"));
13872
10.8M
  KASSERT(stcb != NULL, ("stcb is NULL"));
13873
10.8M
  SCTP_TCB_LOCK_ASSERT(stcb);
13874
13875
10.8M
  asoc = &stcb->asoc;
13876
10.8M
  if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13877
10.8M
      (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13878
0
    if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13879
      /* XXX: Could also be ECONNABORTED, not enough info. */
13880
0
      error = ECONNRESET;
13881
0
    } else {
13882
0
      error = ENOTCONN;
13883
0
    }
13884
0
    goto out_unlocked;
13885
0
  }
13886
10.8M
  if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
13887
10.8M
      (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
13888
2.11k
    queue_only = 1;
13889
2.11k
  }
13890
  /* Keep the stcb from being freed under our feet. */
13891
10.8M
  atomic_add_int(&asoc->refcnt, 1);
13892
10.8M
  free_cnt_applied = true;
13893
10.8M
  if (sndrcvninfo == NULL) {
13894
    /* Use a local copy to have a consistent view. */
13895
10.8M
    sndrcvninfo_buf = asoc->def_send;
13896
10.8M
    sndrcvninfo = &sndrcvninfo_buf;
13897
10.8M
    sinfo_flags = sndrcvninfo->sinfo_flags;
13898
#if defined(__FreeBSD__) && !defined(__Userspace__)
13899
    if (flags & MSG_EOR) {
13900
      sinfo_flags |= SCTP_EOR;
13901
    }
13902
    if (flags & MSG_EOF) {
13903
      sinfo_flags |= SCTP_EOF;
13904
    }
13905
#endif
13906
10.8M
  }
13907
  /* Are we aborting? */
13908
10.8M
  if (sinfo_flags & SCTP_ABORT) {
13909
0
    struct mbuf *mm;
13910
0
    struct sctp_paramhdr *ph;
13911
0
    ssize_t tot_demand, tot_out = 0, max_out;
13912
13913
0
    SCTP_STAT_INCR(sctps_sends_with_abort);
13914
0
    if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
13915
0
        (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
13916
      /* It has to be up before we abort. */
13917
0
      error = EINVAL;
13918
0
      goto out_unlocked;
13919
0
    }
13920
    /* How big is the user initiated abort? */
13921
0
    if (top != NULL) {
13922
0
      struct mbuf *cntm;
13923
13924
0
      if (sndlen != 0) {
13925
0
        for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
13926
0
          tot_out += SCTP_BUF_LEN(cntm);
13927
0
        }
13928
0
      }
13929
0
      mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
13930
0
    } else {
13931
      /* Must fit in a MTU */
13932
0
      tot_out = sndlen;
13933
0
      tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
13934
0
      if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
13935
0
        error = EMSGSIZE;
13936
0
        goto out_unlocked;
13937
0
      }
13938
0
      mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_NOWAIT, 1, MT_DATA);
13939
0
    }
13940
0
    if (mm == NULL) {
13941
0
      error = ENOMEM;
13942
0
      goto out_unlocked;
13943
0
    }
13944
0
    max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
13945
0
    max_out -= sizeof(struct sctp_abort_msg);
13946
0
    if (tot_out > max_out) {
13947
0
      tot_out = max_out;
13948
0
    }
13949
0
    ph = mtod(mm, struct sctp_paramhdr *);
13950
0
    ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
13951
0
    ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out));
13952
0
    ph++;
13953
0
    SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr));
13954
0
    if (top == NULL) {
13955
0
      SCTP_TCB_UNLOCK(stcb);
13956
#if defined(__APPLE__) && !defined(__Userspace__)
13957
      SCTP_SOCKET_UNLOCK(so, 0);
13958
#endif
13959
0
      error = uiomove((caddr_t)ph, (int)tot_out, uio);
13960
#if defined(__APPLE__) && !defined(__Userspace__)
13961
      SCTP_SOCKET_LOCK(so, 0);
13962
#endif
13963
0
      SCTP_TCB_LOCK(stcb);
13964
0
      if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13965
0
          (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13966
0
        sctp_m_freem(mm);
13967
0
        if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13968
          /* XXX: Could also be ECONNABORTED, not enough info. */
13969
0
          error = ECONNRESET;
13970
0
        } else {
13971
0
          error = ENOTCONN;
13972
0
        }
13973
0
        goto out_unlocked;
13974
0
      }
13975
0
      if (error != 0) {
13976
        /*-
13977
         * Here if we can't get his data we
13978
         * still abort we just don't get to
13979
         * send the users note :-0
13980
         */
13981
0
        sctp_m_freem(mm);
13982
0
        mm = NULL;
13983
0
        error = 0;
13984
0
      }
13985
0
    } else {
13986
0
      if (sndlen != 0) {
13987
0
        SCTP_BUF_NEXT(mm) = top;
13988
0
      }
13989
0
    }
13990
0
    atomic_subtract_int(&asoc->refcnt, 1);
13991
0
    free_cnt_applied = false;
13992
    /* release this lock, otherwise we hang on ourselves */
13993
#if defined(__FreeBSD__) && !defined(__Userspace__)
13994
    NET_EPOCH_ENTER(et);
13995
#endif
13996
0
    sctp_abort_an_association(stcb->sctp_ep, stcb, mm, false, SCTP_SO_LOCKED);
13997
#if defined(__FreeBSD__) && !defined(__Userspace__)
13998
    NET_EPOCH_EXIT(et);
13999
#endif
14000
0
    stcb = NULL;
14001
    /* In this case top is already chained to mm
14002
     * avoid double free, since we free it below if
14003
     * top != NULL and driver would free it after sending
14004
     * the packet out
14005
     */
14006
0
    if (sndlen != 0) {
14007
0
      top = NULL;
14008
0
    }
14009
0
    goto out_unlocked;
14010
0
  }
14011
14012
10.8M
  KASSERT(stcb != NULL, ("stcb is NULL"));
14013
10.8M
  SCTP_TCB_LOCK_ASSERT(stcb);
14014
10.8M
  KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14015
10.8M
          ("Association about to be freed"));
14016
10.8M
  KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14017
10.8M
          ("Association was aborted"));
14018
14019
10.8M
  if (sinfo_flags & SCTP_ADDR_OVER) {
14020
0
    if (addr != NULL) {
14021
0
      net = sctp_findnet(stcb, addr);
14022
0
    } else {
14023
0
      net = NULL;
14024
0
    }
14025
0
    if ((net == NULL) ||
14026
0
        ((port != 0) && (port != stcb->rport))) {
14027
0
      error = EINVAL;
14028
0
      goto out_unlocked;
14029
0
    }
14030
10.8M
  } else {
14031
10.8M
    if (asoc->alternate != NULL) {
14032
0
      net = asoc->alternate;
14033
10.8M
    } else {
14034
10.8M
      net = asoc->primary_destination;
14035
10.8M
    }
14036
10.8M
  }
14037
10.8M
  if (sndlen == 0) {
14038
0
    if (sinfo_flags & SCTP_EOF) {
14039
0
      got_all_of_the_send = true;
14040
0
      goto dataless_eof;
14041
0
    } else {
14042
0
      error = EINVAL;
14043
0
      goto out_unlocked;
14044
0
    }
14045
0
  }
14046
10.8M
  if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
14047
0
    if (sndlen > (ssize_t)asoc->smallest_mtu) {
14048
0
      error = EMSGSIZE;
14049
0
      goto out_unlocked;
14050
0
    }
14051
0
  }
14052
10.8M
  sinfo_stream = sndrcvninfo->sinfo_stream;
14053
  /* Is the stream no. valid? */
14054
10.8M
  if (sinfo_stream >= asoc->streamoutcnt) {
14055
    /* Invalid stream number */
14056
0
    error = EINVAL;
14057
0
    goto out_unlocked;
14058
0
  }
14059
10.8M
  if ((asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPEN) &&
14060
1.66k
      (asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPENING)) {
14061
    /*
14062
     * Can't queue any data while stream reset is underway.
14063
     */
14064
614
    if (asoc->strmout[sinfo_stream].state > SCTP_STREAM_OPEN) {
14065
614
      error = EAGAIN;
14066
614
    } else {
14067
0
      error = EINVAL;
14068
0
    }
14069
614
    goto out_unlocked;
14070
614
  }
14071
10.8M
  atomic_add_int(&stcb->total_sends, 1);
14072
10.8M
#if defined(__Userspace__)
14073
10.8M
  if (inp->recv_callback != NULL) {
14074
0
    non_blocking = true;
14075
0
  }
14076
10.8M
#endif
14077
#if defined(__FreeBSD__) && !defined(__Userspace__)
14078
  if (SCTP_SO_IS_NBIO(so) || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
14079
#else
14080
10.8M
  if (SCTP_SO_IS_NBIO(so)) {
14081
10.8M
#endif
14082
10.8M
    non_blocking = true;
14083
10.8M
  }
14084
10.8M
  if (non_blocking) {
14085
10.8M
    ssize_t amount;
14086
14087
10.8M
    inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
14088
10.8M
    if (user_marks_eor == 0) {
14089
10.8M
      amount = sndlen;
14090
10.8M
    } else {
14091
0
      amount = 1;
14092
0
    }
14093
10.8M
    if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + asoc->sb_send_resv)) ||
14094
10.7M
        (asoc->chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14095
10.7M
      if ((sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) &&
14096
0
          (user_marks_eor == 0)) {
14097
0
        error = EMSGSIZE;
14098
10.7M
      } else {
14099
10.7M
        error = EWOULDBLOCK;
14100
10.7M
      }
14101
10.7M
      goto out_unlocked;
14102
10.7M
    }
14103
10.8M
  }
14104
24.7k
  atomic_add_int(&asoc->sb_send_resv, (int)sndlen);
14105
24.7k
  local_soresv = sndlen;
14106
14107
24.7k
  KASSERT(stcb != NULL, ("stcb is NULL"));
14108
24.7k
  SCTP_TCB_LOCK_ASSERT(stcb);
14109
24.7k
  KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14110
24.7k
          ("Association about to be freed"));
14111
24.7k
  KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14112
24.7k
          ("Association was aborted"));
14113
14114
  /* Ok, we will attempt a msgsnd :> */
14115
#if !(defined(_WIN32) || defined(__Userspace__))
14116
  if (p != NULL) {
14117
#if defined(__FreeBSD__)
14118
    p->td_ru.ru_msgsnd++;
14119
#else
14120
    p->p_stats->p_ru.ru_msgsnd++;
14121
#endif
14122
  }
14123
#endif
14124
  /* Calculate the maximum we can send */
14125
24.7k
  inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
14126
24.7k
  if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14127
24.7k
    max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14128
24.7k
  } else {
14129
0
    max_len = 0;
14130
0
  }
14131
  /* Unless E_EOR mode is on, we must make a send FIT in one call. */
14132
24.7k
  if ((user_marks_eor == 0) &&
14133
24.7k
      (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
14134
    /* It will NEVER fit. */
14135
0
    error = EMSGSIZE;
14136
0
    goto out_unlocked;
14137
0
  }
14138
24.7k
  if (user_marks_eor != 0) {
14139
0
    local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
14140
24.7k
  } else {
14141
    /*-
14142
     * For non-eeor the whole message must fit in
14143
     * the socket send buffer.
14144
     */
14145
24.7k
    local_add_more = sndlen;
14146
24.7k
  }
14147
24.7k
  if (non_blocking) {
14148
24.7k
    goto skip_preblock;
14149
24.7k
  }
14150
0
  if (((max_len <= local_add_more) && ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
14151
0
      (max_len == 0) ||
14152
0
      ((asoc->chunks_on_out_queue + asoc->stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14153
    /* No room right now! */
14154
0
    inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
14155
0
    SOCKBUF_LOCK(&so->so_snd);
14156
0
    while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
14157
0
           ((asoc->stream_queue_cnt + asoc->chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
14158
0
      SCTPDBG(SCTP_DEBUG_OUTPUT1,"pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n",
14159
0
              (unsigned int)SCTP_SB_LIMIT_SND(so),
14160
0
              inqueue_bytes,
14161
0
              local_add_more,
14162
0
              asoc->stream_queue_cnt,
14163
0
              asoc->chunks_on_out_queue,
14164
0
              SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
14165
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14166
0
        sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
14167
0
      }
14168
0
      be.error = 0;
14169
0
#if !(defined(_WIN32) && !defined(__Userspace__))
14170
0
      stcb->block_entry = &be;
14171
0
#endif
14172
0
      SCTP_TCB_UNLOCK(stcb);
14173
#if defined(__FreeBSD__) && !defined(__Userspace__)
14174
      error = sbwait(so, SO_SND);
14175
#else
14176
0
      error = sbwait(&so->so_snd);
14177
0
#endif
14178
0
      if (error == 0) {
14179
0
        if (so->so_error != 0) {
14180
0
          error = so->so_error;
14181
0
        }
14182
0
        if (be.error != 0) {
14183
0
          error = be.error;
14184
0
        }
14185
0
      }
14186
0
      SOCKBUF_UNLOCK(&so->so_snd);
14187
0
      SCTP_TCB_LOCK(stcb);
14188
0
      stcb->block_entry = NULL;
14189
0
      if (error != 0) {
14190
0
        goto out_unlocked;
14191
0
      }
14192
0
      if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
14193
0
          (asoc->state & SCTP_STATE_WAS_ABORTED)) {
14194
0
        if (asoc->state & SCTP_STATE_WAS_ABORTED) {
14195
          /* XXX: Could also be ECONNABORTED, not enough info. */
14196
0
          error = ECONNRESET;
14197
0
        } else {
14198
0
          error = ENOTCONN;
14199
0
        }
14200
0
        goto out_unlocked;
14201
0
      }
14202
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14203
0
        sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
14204
0
                       asoc, asoc->total_output_queue_size);
14205
0
      }
14206
0
      inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
14207
0
      SOCKBUF_LOCK(&so->so_snd);
14208
0
    }
14209
0
    if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14210
0
      max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14211
0
    } else {
14212
0
      max_len = 0;
14213
0
    }
14214
0
    SOCKBUF_UNLOCK(&so->so_snd);
14215
0
  }
14216
14217
24.7k
skip_preblock:
14218
24.7k
  KASSERT(stcb != NULL, ("stcb is NULL"));
14219
24.7k
  SCTP_TCB_LOCK_ASSERT(stcb);
14220
24.7k
  KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14221
24.7k
          ("Association about to be freed"));
14222
24.7k
  KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14223
24.7k
          ("Association was aborted"));
14224
14225
#if defined(__APPLE__) && !defined(__Userspace__)
14226
  error = sblock(&so->so_snd, SBLOCKWAIT(flags));
14227
  if (error != 0) {
14228
    goto out_unlocked;
14229
  }
14230
#endif
14231
  /* sndlen covers for mbuf case
14232
   * uio_resid covers for the non-mbuf case
14233
   * NOTE: uio will be null when top/mbuf is passed
14234
   */
14235
24.7k
  if (top == NULL) {
14236
24.7k
    struct sctp_stream_queue_pending *sp;
14237
24.7k
    struct sctp_stream_out *strm;
14238
24.7k
    uint32_t sndout;
14239
14240
24.7k
    if ((asoc->stream_locked) &&
14241
0
        (asoc->stream_locked_on != sinfo_stream)) {
14242
0
      error = EINVAL;
14243
0
      goto out;
14244
0
    }
14245
24.7k
    strm = &asoc->strmout[sinfo_stream];
14246
24.7k
    if (strm->last_msg_incomplete == 0) {
14247
24.7k
    do_a_copy_in:
14248
24.7k
      SCTP_TCB_UNLOCK(stcb);
14249
24.7k
      sp = sctp_copy_it_in(stcb, asoc, sndrcvninfo, uio, net, max_len, user_marks_eor, &error);
14250
24.7k
      SCTP_TCB_LOCK(stcb);
14251
24.7k
      if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
14252
24.7k
          (asoc->state & SCTP_STATE_WAS_ABORTED)) {
14253
0
        if (asoc->state & SCTP_STATE_WAS_ABORTED) {
14254
          /* XXX: Could also be ECONNABORTED, not enough info. */
14255
0
          error = ECONNRESET;
14256
0
        } else {
14257
0
          error = ENOTCONN;
14258
0
        }
14259
0
        goto out;
14260
0
      }
14261
24.7k
      if (error != 0) {
14262
0
        goto out;
14263
0
      }
14264
      /*
14265
       * Reject the sending of a new user message, if the
14266
       * association is about to be shut down.
14267
       */
14268
24.7k
      if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
14269
24.7k
          (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
14270
24.7k
          (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
14271
24.7k
          (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
14272
0
        if (sp->data != 0) {
14273
0
          sctp_m_freem(sp->data);
14274
0
          sp->data = NULL;
14275
0
          sp->tail_mbuf = NULL;
14276
0
          sp->length = 0;
14277
0
        }
14278
0
        if (sp->net != NULL) {
14279
0
          sctp_free_remote_addr(sp->net);
14280
0
          sp->net = NULL;
14281
0
        }
14282
0
        sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
14283
0
        error = EPIPE;
14284
0
        goto out_unlocked;
14285
0
      }
14286
      /* The out streams might be reallocated. */
14287
24.7k
      strm = &asoc->strmout[sinfo_stream];
14288
24.7k
      if (sp->msg_is_complete) {
14289
24.7k
        strm->last_msg_incomplete = 0;
14290
24.7k
        asoc->stream_locked = 0;
14291
24.7k
      } else {
14292
        /* Just got locked to this guy in
14293
         * case of an interrupt.
14294
         */
14295
0
        strm->last_msg_incomplete = 1;
14296
0
        if (asoc->idata_supported == 0) {
14297
0
          asoc->stream_locked = 1;
14298
0
          asoc->stream_locked_on = sinfo_stream;
14299
0
        }
14300
0
        sp->sender_all_done = 0;
14301
0
      }
14302
24.7k
      sctp_snd_sb_alloc(stcb, sp->length);
14303
24.7k
      atomic_add_int(&asoc->stream_queue_cnt, 1);
14304
24.7k
      if (sinfo_flags & SCTP_UNORDERED) {
14305
0
        SCTP_STAT_INCR(sctps_sends_with_unord);
14306
0
      }
14307
24.7k
      sp->processing = 1;
14308
24.7k
      TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
14309
24.7k
      asoc->ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp);
14310
24.7k
    } else {
14311
0
      sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
14312
0
      if (sp == NULL) {
14313
        /* ???? Huh ??? last msg is gone */
14314
0
#ifdef INVARIANTS
14315
0
        panic("Warning: Last msg marked incomplete, yet nothing left?");
14316
#else
14317
        SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
14318
        strm->last_msg_incomplete = 0;
14319
#endif
14320
0
        goto do_a_copy_in;
14321
0
      }
14322
0
      if (sp->processing != 0) {
14323
0
        error = EINVAL;
14324
0
        goto out;
14325
0
      } else {
14326
0
        sp->processing = 1;
14327
0
      }
14328
0
    }
14329
14330
24.7k
    KASSERT(stcb != NULL, ("stcb is NULL"));
14331
24.7k
    SCTP_TCB_LOCK_ASSERT(stcb);
14332
24.7k
    KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14333
24.7k
            ("Association about to be freed"));
14334
24.7k
    KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14335
24.7k
            ("Association was aborted"));
14336
14337
#if defined(__APPLE__) && !defined(__Userspace__)
14338
#if defined(APPLE_LEOPARD)
14339
    while (uio->uio_resid > 0) {
14340
#else
14341
    while (uio_resid(uio) > 0) {
14342
#endif
14343
#else
14344
24.7k
    while (uio->uio_resid > 0) {
14345
0
#endif
14346
      /* How much room do we have? */
14347
0
      struct mbuf *new_tail, *mm;
14348
14349
0
      inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
14350
0
      if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
14351
0
        max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14352
0
      } else {
14353
0
        max_len = 0;
14354
0
      }
14355
0
      if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
14356
0
          ((max_len > 0 ) && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
14357
#if defined(__APPLE__) && !defined(__Userspace__)
14358
#if defined(APPLE_LEOPARD)
14359
          (uio->uio_resid <= max_len)) {
14360
#else
14361
          (uio_resid(uio) <= max_len)) {
14362
#endif
14363
#else
14364
0
          (uio->uio_resid <= max_len)) {
14365
0
#endif
14366
0
        SCTP_TCB_UNLOCK(stcb);
14367
#if defined(__APPLE__) && !defined(__Userspace__)
14368
        SCTP_SOCKET_UNLOCK(so, 0);
14369
#endif
14370
0
        sndout = 0;
14371
0
        new_tail = NULL;
14372
0
#if defined(__FreeBSD__) || defined(__Userspace__)
14373
0
        mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail);
14374
#else
14375
        mm = sctp_copy_resume(uio, (int)max_len, &error, &sndout, &new_tail);
14376
#endif
14377
#if defined(__APPLE__) && !defined(__Userspace__)
14378
        SCTP_SOCKET_LOCK(so, 0);
14379
#endif
14380
0
        SCTP_TCB_LOCK(stcb);
14381
0
        if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
14382
0
            (asoc->state & SCTP_STATE_WAS_ABORTED)) {
14383
          /* We need to get out.
14384
           * Peer probably aborted.
14385
           */
14386
0
          sctp_m_freem(mm);
14387
0
          if (asoc->state & SCTP_STATE_WAS_ABORTED) {
14388
            /* XXX: Could also be ECONNABORTED, not enough info. */
14389
0
            error = ECONNRESET;
14390
0
          } else {
14391
0
            error = ENOTCONN;
14392
0
          }
14393
0
          goto out;
14394
0
        }
14395
0
        if ((mm == NULL) || (error != 0)) {
14396
0
          if (mm != NULL) {
14397
0
            sctp_m_freem(mm);
14398
0
          }
14399
0
          if (sp != NULL) {
14400
0
            sp->processing = 0;
14401
0
          }
14402
0
          goto out;
14403
0
        }
14404
        /* Update the mbuf and count */
14405
0
        if (sp->tail_mbuf != NULL) {
14406
          /* Tack it to the end. */
14407
0
          SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
14408
0
        } else {
14409
          /* A stolen mbuf. */
14410
0
          sp->data = mm;
14411
0
        }
14412
0
        sp->tail_mbuf = new_tail;
14413
0
        sctp_snd_sb_alloc(stcb, sndout);
14414
0
        atomic_add_int(&sp->length, sndout);
14415
0
        if (sinfo_flags & SCTP_SACK_IMMEDIATELY) {
14416
0
          sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY;
14417
0
        }
14418
14419
        /* Did we reach EOR? */
14420
#if defined(__APPLE__) && !defined(__Userspace__)
14421
#if defined(APPLE_LEOPARD)
14422
        if ((uio->uio_resid == 0) &&
14423
#else
14424
        if ((uio_resid(uio) == 0) &&
14425
#endif
14426
#else
14427
0
        if ((uio->uio_resid == 0) &&
14428
0
#endif
14429
0
            ((user_marks_eor == 0) ||
14430
0
             (sinfo_flags & SCTP_EOF) ||
14431
0
             (user_marks_eor && (sinfo_flags & SCTP_EOR)))) {
14432
0
          sp->msg_is_complete = 1;
14433
0
        } else {
14434
0
          sp->msg_is_complete = 0;
14435
0
        }
14436
0
      }
14437
14438
0
      KASSERT(stcb != NULL, ("stcb is NULL"));
14439
0
      SCTP_TCB_LOCK_ASSERT(stcb);
14440
0
      KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14441
0
              ("Association about to be freed"));
14442
0
      KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14443
0
              ("Association was aborted"));
14444
14445
#if defined(__APPLE__) && !defined(__Userspace__)
14446
#if defined(APPLE_LEOPARD)
14447
      if (uio->uio_resid == 0) {
14448
#else
14449
      if (uio_resid(uio) == 0) {
14450
#endif
14451
#else
14452
0
      if (uio->uio_resid == 0) {
14453
0
#endif
14454
        /* got it all? */
14455
0
        continue;
14456
0
      }
14457
      /* PR-SCTP? */
14458
0
      if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) {
14459
        /* This is ugly but we must assure locking order */
14460
0
        sctp_prune_prsctp(stcb, asoc, sndrcvninfo, (int)sndlen);
14461
0
        inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
14462
0
        if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes)
14463
0
          max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
14464
0
        else
14465
0
          max_len = 0;
14466
0
        if (max_len > 0) {
14467
0
          continue;
14468
0
        }
14469
0
      }
14470
      /* wait for space now */
14471
0
      if (non_blocking) {
14472
        /* Non-blocking io in place out */
14473
0
        if (sp != NULL) {
14474
0
          sp->processing = 0;
14475
0
        }
14476
0
        goto skip_out_eof;
14477
0
      }
14478
      /* What about the INIT, send it maybe */
14479
0
      if (queue_only_for_init) {
14480
0
        if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
14481
          /* a collision took us forward? */
14482
0
          queue_only = 0;
14483
0
        } else {
14484
#if defined(__FreeBSD__) && !defined(__Userspace__)
14485
          NET_EPOCH_ENTER(et);
14486
#endif
14487
0
          sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
14488
#if defined(__FreeBSD__) && !defined(__Userspace__)
14489
          NET_EPOCH_EXIT(et);
14490
#endif
14491
0
          SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
14492
0
          queue_only = 1;
14493
0
        }
14494
0
      }
14495
0
      if ((net->flight_size > net->cwnd) &&
14496
0
          (asoc->sctp_cmt_on_off == 0)) {
14497
0
        SCTP_STAT_INCR(sctps_send_cwnd_avoid);
14498
0
        queue_only = 1;
14499
0
      } else if (asoc->ifp_had_enobuf) {
14500
0
        SCTP_STAT_INCR(sctps_ifnomemqueued);
14501
0
        if (net->flight_size > (2 * net->mtu)) {
14502
0
          queue_only = 1;
14503
0
        }
14504
0
        asoc->ifp_had_enobuf = 0;
14505
0
      }
14506
0
      un_sent = asoc->total_output_queue_size - asoc->total_flight;
14507
0
      if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
14508
0
          (asoc->total_flight > 0) &&
14509
0
          (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
14510
0
          (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
14511
        /*-
14512
         * Ok, Nagle is set on and we have data outstanding.
14513
         * Don't send anything and let SACKs drive out the
14514
         * data unless we have a "full" segment to send.
14515
         */
14516
0
        if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
14517
0
          sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
14518
0
        }
14519
0
        SCTP_STAT_INCR(sctps_naglequeued);
14520
0
        nagle_applies = 1;
14521
0
      } else {
14522
0
        if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
14523
0
          if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
14524
0
            sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
14525
0
        }
14526
0
        SCTP_STAT_INCR(sctps_naglesent);
14527
0
        nagle_applies = 0;
14528
0
      }
14529
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14530
0
        sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
14531
0
                       nagle_applies, un_sent);
14532
0
        sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
14533
0
                       asoc->total_flight,
14534
0
                       asoc->chunks_on_out_queue, asoc->total_flight_count);
14535
0
      }
14536
0
      if (queue_only_for_init) {
14537
0
        queue_only_for_init = 0;
14538
0
      }
14539
0
      if ((queue_only == 0) && (nagle_applies == 0)) {
14540
        /*-
14541
         * need to start chunk output
14542
         * before blocking.. note that if
14543
         * a lock is already applied, then
14544
         * the input via the net is happening
14545
         * and I don't need to start output :-D
14546
         */
14547
#if defined(__FreeBSD__) && !defined(__Userspace__)
14548
        NET_EPOCH_ENTER(et);
14549
#endif
14550
0
        sctp_chunk_output(inp, stcb,
14551
0
                          SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
14552
#if defined(__FreeBSD__) && !defined(__Userspace__)
14553
        NET_EPOCH_EXIT(et);
14554
#endif
14555
0
      }
14556
      /*-
14557
       * This is a bit strange, but I think it will
14558
       * work. The total_output_queue_size is locked and
14559
       * protected by the TCB_LOCK, which we just released.
14560
       * There is a race that can occur between releasing it
14561
       * above, and me getting the socket lock, where sacks
14562
       * come in but we have not put the SB_WAIT on the
14563
       * so_snd buffer to get the wakeup. After the LOCK
14564
       * is applied the sack_processing will also need to
14565
       * LOCK the so->so_snd to do the actual sowwakeup(). So
14566
       * once we have the socket buffer lock if we recheck the
14567
       * size we KNOW we will get to sleep safely with the
14568
       * wakeup flag in place.
14569
       */
14570
0
      inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
14571
0
      SOCKBUF_LOCK(&so->so_snd);
14572
0
      if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes +
14573
0
                                    min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
14574
0
        if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14575
#if defined(__APPLE__) && !defined(__Userspace__)
14576
#if defined(APPLE_LEOPARD)
14577
          sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
14578
                         asoc, uio->uio_resid);
14579
#else
14580
          sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
14581
                         asoc, uio_resid(uio));
14582
#endif
14583
#else
14584
0
          sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
14585
0
                         asoc, uio->uio_resid);
14586
0
#endif
14587
0
        }
14588
0
        be.error = 0;
14589
0
#if !(defined(_WIN32) && !defined(__Userspace__))
14590
0
        stcb->block_entry = &be;
14591
0
#endif
14592
0
        SCTP_TCB_UNLOCK(stcb);
14593
#if defined(__APPLE__) && !defined(__Userspace__)
14594
        sbunlock(&so->so_snd, 1);
14595
#endif
14596
#if defined(__FreeBSD__) && !defined(__Userspace__)
14597
        error = sbwait(so, SO_SND);
14598
#else
14599
0
        error = sbwait(&so->so_snd);
14600
0
#endif
14601
0
        if (error == 0) {
14602
0
          if (so->so_error != 0)
14603
0
            error = so->so_error;
14604
0
          if (be.error != 0) {
14605
0
            error = be.error;
14606
0
          }
14607
0
        }
14608
0
        SOCKBUF_UNLOCK(&so->so_snd);
14609
0
        SCTP_TCB_LOCK(stcb);
14610
0
        stcb->block_entry = NULL;
14611
0
        if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
14612
0
            (asoc->state & SCTP_STATE_WAS_ABORTED)) {
14613
0
          if (asoc->state & SCTP_STATE_WAS_ABORTED) {
14614
            /* XXX: Could also be ECONNABORTED, not enough info. */
14615
0
            error = ECONNRESET;
14616
0
          } else {
14617
0
            error = ENOTCONN;
14618
0
          }
14619
0
          goto out_unlocked;
14620
0
        }
14621
0
        if (error != 0) {
14622
0
          if (sp != NULL) {
14623
0
            sp->processing = 0;
14624
0
          }
14625
0
          goto out_unlocked;
14626
0
        }
14627
#if defined(__APPLE__) && !defined(__Userspace__)
14628
        error = sblock(&so->so_snd, SBLOCKWAIT(flags));
14629
        if (error != 0) {
14630
          goto out_unlocked;
14631
        }
14632
#endif
14633
0
      } else {
14634
0
        SOCKBUF_UNLOCK(&so->so_snd);
14635
0
      }
14636
0
      if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14637
0
        sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
14638
0
                       asoc, asoc->total_output_queue_size);
14639
0
      }
14640
0
    }
14641
14642
24.7k
    KASSERT(stcb != NULL, ("stcb is NULL"));
14643
24.7k
    SCTP_TCB_LOCK_ASSERT(stcb);
14644
24.7k
    KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14645
24.7k
            ("Association about to be freed"));
14646
24.7k
    KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14647
24.7k
            ("Association was aborted"));
14648
14649
    /* The out streams might be reallocated. */
14650
24.7k
    strm = &asoc->strmout[sinfo_stream];
14651
24.7k
    if (sp != NULL) {
14652
24.7k
      if (sp->msg_is_complete == 0) {
14653
0
        strm->last_msg_incomplete = 1;
14654
0
        if (asoc->idata_supported == 0) {
14655
0
          asoc->stream_locked = 1;
14656
0
          asoc->stream_locked_on = sinfo_stream;
14657
0
        }
14658
24.7k
      } else {
14659
24.7k
        sp->sender_all_done = 1;
14660
24.7k
        strm->last_msg_incomplete = 0;
14661
24.7k
        asoc->stream_locked = 0;
14662
24.7k
      }
14663
24.7k
      sp->processing = 0;
14664
24.7k
    } else {
14665
0
      SCTP_PRINTF("Huh no sp TSNH?\n");
14666
0
      strm->last_msg_incomplete = 0;
14667
0
      asoc->stream_locked = 0;
14668
0
    }
14669
#if defined(__APPLE__) && !defined(__Userspace__)
14670
#if defined(APPLE_LEOPARD)
14671
    if (uio->uio_resid == 0) {
14672
#else
14673
    if (uio_resid(uio) == 0) {
14674
#endif
14675
#else
14676
24.7k
    if (uio->uio_resid == 0) {
14677
24.7k
#endif
14678
24.7k
      got_all_of_the_send = true;
14679
24.7k
    }
14680
24.7k
  } else {
14681
0
    error = sctp_msg_append(stcb, net, top, sndrcvninfo);
14682
0
    top = NULL;
14683
0
    if ((sinfo_flags & SCTP_EOF) != 0) {
14684
0
      got_all_of_the_send = true;
14685
0
    }
14686
0
  }
14687
24.7k
  if (error != 0) {
14688
0
    goto out;
14689
0
  }
14690
14691
24.7k
dataless_eof:
14692
24.7k
  KASSERT(stcb != NULL, ("stcb is NULL"));
14693
24.7k
  SCTP_TCB_LOCK_ASSERT(stcb);
14694
24.7k
  KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14695
24.7k
          ("Association about to be freed"));
14696
24.7k
  KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14697
24.7k
          ("Association was aborted"));
14698
14699
  /* EOF thing ? */
14700
24.7k
  if ((sinfo_flags & SCTP_EOF) && got_all_of_the_send) {
14701
0
    SCTP_STAT_INCR(sctps_sends_with_eof);
14702
0
    error = 0;
14703
0
    if (TAILQ_EMPTY(&asoc->send_queue) &&
14704
0
        TAILQ_EMPTY(&asoc->sent_queue) &&
14705
0
        sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) {
14706
0
      if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
14707
0
        goto abort_anyway;
14708
0
      }
14709
      /* there is nothing queued to send, so I'm done... */
14710
0
      if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
14711
0
          (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
14712
0
          (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
14713
0
        struct sctp_nets *netp;
14714
14715
        /* only send SHUTDOWN the first time through */
14716
0
        if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
14717
0
          SCTP_STAT_DECR_GAUGE32(sctps_currestab);
14718
0
        }
14719
0
        SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
14720
0
        sctp_stop_timers_for_shutdown(stcb);
14721
0
        if (asoc->alternate != NULL) {
14722
0
          netp = asoc->alternate;
14723
0
        } else {
14724
0
          netp = asoc->primary_destination;
14725
0
        }
14726
0
        sctp_send_shutdown(stcb, netp);
14727
0
        sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
14728
0
                         netp);
14729
0
        sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
14730
0
                         NULL);
14731
0
      }
14732
0
    } else {
14733
      /*-
14734
       * we still got (or just got) data to send, so set
14735
       * SHUTDOWN_PENDING
14736
       */
14737
      /*-
14738
       * XXX sockets draft says that SCTP_EOF should be
14739
       * sent with no data.  currently, we will allow user
14740
       * data to be sent first and move to
14741
       * SHUTDOWN-PENDING
14742
       */
14743
0
      if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
14744
0
          (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
14745
0
          (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
14746
0
        if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete)(stcb, asoc)) {
14747
0
          SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
14748
0
        }
14749
0
        SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
14750
0
        if (TAILQ_EMPTY(&asoc->send_queue) &&
14751
0
            TAILQ_EMPTY(&asoc->sent_queue) &&
14752
0
            (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
14753
0
          struct mbuf *op_err;
14754
0
          char msg[SCTP_DIAG_INFO_LEN];
14755
14756
0
        abort_anyway:
14757
0
          if (free_cnt_applied) {
14758
0
            atomic_subtract_int(&asoc->refcnt, 1);
14759
0
            free_cnt_applied = false;
14760
0
          }
14761
0
          SCTP_SNPRINTF(msg, sizeof(msg),
14762
0
                        "%s:%d at %s", __FILE__, __LINE__, __func__);
14763
0
          op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
14764
0
                                       msg);
14765
#if defined(__FreeBSD__) && !defined(__Userspace__)
14766
          NET_EPOCH_ENTER(et);
14767
#endif
14768
0
          sctp_abort_an_association(stcb->sctp_ep, stcb,
14769
0
                                    op_err, false, SCTP_SO_LOCKED);
14770
#if defined(__FreeBSD__) && !defined(__Userspace__)
14771
          NET_EPOCH_EXIT(et);
14772
#endif
14773
0
          stcb = NULL;
14774
0
          error = ECONNABORTED;
14775
0
          goto out;
14776
0
        }
14777
0
        sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
14778
0
      }
14779
0
    }
14780
0
  }
14781
14782
24.7k
skip_out_eof:
14783
24.7k
  KASSERT(stcb != NULL, ("stcb is NULL"));
14784
24.7k
  SCTP_TCB_LOCK_ASSERT(stcb);
14785
24.7k
  KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14786
24.7k
          ("Association about to be freed"));
14787
24.7k
  KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14788
24.7k
          ("Association was aborted"));
14789
14790
24.7k
  some_on_control = !TAILQ_EMPTY(&asoc->control_send_queue);
14791
24.7k
  if (queue_only_for_init) {
14792
0
    if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
14793
      /* a collision took us forward? */
14794
0
      queue_only = 0;
14795
0
    } else {
14796
#if defined(__FreeBSD__) && !defined(__Userspace__)
14797
      NET_EPOCH_ENTER(et);
14798
#endif
14799
0
      sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
14800
#if defined(__FreeBSD__) && !defined(__Userspace__)
14801
      NET_EPOCH_EXIT(et);
14802
#endif
14803
0
      SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
14804
0
      queue_only = 1;
14805
0
    }
14806
0
  }
14807
14808
24.7k
  KASSERT(stcb != NULL, ("stcb is NULL"));
14809
24.7k
  SCTP_TCB_LOCK_ASSERT(stcb);
14810
24.7k
  KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14811
24.7k
          ("Association about to be freed"));
14812
24.7k
  KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14813
24.7k
          ("Association was aborted"));
14814
14815
24.7k
  if ((net->flight_size > net->cwnd) &&
14816
0
      (asoc->sctp_cmt_on_off == 0)) {
14817
0
    SCTP_STAT_INCR(sctps_send_cwnd_avoid);
14818
0
    queue_only = 1;
14819
24.7k
  } else if (asoc->ifp_had_enobuf) {
14820
0
    SCTP_STAT_INCR(sctps_ifnomemqueued);
14821
0
    if (net->flight_size > (2 * net->mtu)) {
14822
0
      queue_only = 1;
14823
0
    }
14824
0
    asoc->ifp_had_enobuf = 0;
14825
0
  }
14826
24.7k
  un_sent = asoc->total_output_queue_size - asoc->total_flight;
14827
24.7k
  if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
14828
0
      (asoc->total_flight > 0) &&
14829
0
      (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
14830
0
      (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
14831
    /*-
14832
     * Ok, Nagle is set on and we have data outstanding.
14833
     * Don't send anything and let SACKs drive out the
14834
     * data unless wen have a "full" segment to send.
14835
     */
14836
0
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
14837
0
      sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
14838
0
    }
14839
0
    SCTP_STAT_INCR(sctps_naglequeued);
14840
0
    nagle_applies = 1;
14841
24.7k
  } else {
14842
24.7k
    if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
14843
0
      if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
14844
0
        sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
14845
0
    }
14846
24.7k
    SCTP_STAT_INCR(sctps_naglesent);
14847
24.7k
    nagle_applies = 0;
14848
24.7k
  }
14849
24.7k
  if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
14850
0
    sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
14851
0
                   nagle_applies, un_sent);
14852
0
    sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
14853
0
                   asoc->total_flight,
14854
0
                   asoc->chunks_on_out_queue, asoc->total_flight_count);
14855
0
  }
14856
14857
24.7k
  KASSERT(stcb != NULL, ("stcb is NULL"));
14858
24.7k
  SCTP_TCB_LOCK_ASSERT(stcb);
14859
24.7k
  KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14860
24.7k
          ("Association about to be freed"));
14861
24.7k
  KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14862
24.7k
          ("Association was aborted"));
14863
14864
#if defined(__FreeBSD__) && !defined(__Userspace__)
14865
  NET_EPOCH_ENTER(et);
14866
#endif
14867
24.7k
  if ((queue_only == 0) && (nagle_applies == 0) && (asoc->peers_rwnd && un_sent)) {
14868
22.6k
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
14869
22.6k
  } else if ((queue_only == 0) &&
14870
0
             (asoc->peers_rwnd == 0) &&
14871
0
             (asoc->total_flight == 0)) {
14872
    /* We get to have a probe outstanding */
14873
0
    sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
14874
2.11k
  } else if (some_on_control) {
14875
1.06k
    int num_out, reason;
14876
14877
    /* Here we do control only */
14878
1.06k
    (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out,
14879
1.06k
                                &reason, 1, 1, &now, &now_filled,
14880
1.06k
                                sctp_get_frag_point(stcb),
14881
1.06k
                                SCTP_SO_LOCKED);
14882
1.06k
  }
14883
#if defined(__FreeBSD__) && !defined(__Userspace__)
14884
  NET_EPOCH_EXIT(et);
14885
#endif
14886
24.7k
  SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
14887
24.7k
          queue_only, asoc->peers_rwnd, un_sent,
14888
24.7k
          asoc->total_flight, asoc->chunks_on_out_queue,
14889
24.7k
          asoc->total_output_queue_size, error);
14890
14891
24.7k
  KASSERT(stcb != NULL, ("stcb is NULL"));
14892
24.7k
  SCTP_TCB_LOCK_ASSERT(stcb);
14893
24.7k
  KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
14894
24.7k
          ("Association about to be freed"));
14895
24.7k
  KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
14896
24.7k
          ("Association was aborted"));
14897
14898
24.7k
out:
14899
#if defined(__APPLE__) && !defined(__Userspace__)
14900
  sbunlock(&so->so_snd, 1);
14901
#endif
14902
10.8M
out_unlocked:
14903
10.8M
  if (create_lock_applied) {
14904
0
    SCTP_ASOC_CREATE_UNLOCK(inp);
14905
0
  }
14906
10.8M
  if (stcb != NULL) {
14907
10.8M
    if (local_soresv) {
14908
24.7k
      atomic_subtract_int(&asoc->sb_send_resv, (int)sndlen);
14909
24.7k
    }
14910
10.8M
    if (free_cnt_applied) {
14911
10.8M
      atomic_subtract_int(&asoc->refcnt, 1);
14912
10.8M
    }
14913
10.8M
    SCTP_TCB_UNLOCK(stcb);
14914
10.8M
  }
14915
10.8M
  if (top != NULL) {
14916
0
    sctp_m_freem(top);
14917
0
  }
14918
10.8M
  if (control != NULL) {
14919
0
    sctp_m_freem(control);
14920
0
  }
14921
10.8M
  SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error);
14922
10.8M
  return (error);
14923
10.8M
}
14924
14925
/*
14926
 * generate an AUTHentication chunk, if required
14927
 */
14928
struct mbuf *
14929
sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
14930
    struct sctp_auth_chunk **auth_ret, uint32_t * offset,
14931
    struct sctp_tcb *stcb, uint8_t chunk)
14932
614
{
14933
614
  struct mbuf *m_auth;
14934
614
  struct sctp_auth_chunk *auth;
14935
614
  int chunk_len;
14936
614
  struct mbuf *cn;
14937
14938
614
  if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
14939
614
      (stcb == NULL))
14940
0
    return (m);
14941
14942
614
  if (stcb->asoc.auth_supported == 0) {
14943
60
    return (m);
14944
60
  }
14945
  /* does the requested chunk require auth? */
14946
554
  if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
14947
0
    return (m);
14948
0
  }
14949
554
  m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER);
14950
554
  if (m_auth == NULL) {
14951
    /* no mbuf's */
14952
0
    return (m);
14953
0
  }
14954
  /* reserve some space if this will be the first mbuf */
14955
554
  if (m == NULL)
14956
509
    SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
14957
  /* fill in the AUTH chunk details */
14958
554
  auth = mtod(m_auth, struct sctp_auth_chunk *);
14959
554
  memset(auth, 0, sizeof(*auth));
14960
554
  auth->ch.chunk_type = SCTP_AUTHENTICATION;
14961
554
  auth->ch.chunk_flags = 0;
14962
554
  chunk_len = sizeof(*auth) +
14963
554
      sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
14964
554
  auth->ch.chunk_length = htons(chunk_len);
14965
554
  auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
14966
  /* key id and hmac digest will be computed and filled in upon send */
14967
14968
  /* save the offset where the auth was inserted into the chain */
14969
554
  *offset = 0;
14970
804
  for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
14971
250
    *offset += SCTP_BUF_LEN(cn);
14972
250
  }
14973
14974
  /* update length and return pointer to the auth chunk */
14975
554
  SCTP_BUF_LEN(m_auth) = chunk_len;
14976
554
  m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
14977
554
  if (auth_ret != NULL)
14978
554
    *auth_ret = auth;
14979
14980
554
  return (m);
14981
554
}
14982
14983
#if (defined(__FreeBSD__) || defined(__APPLE__)) && !defined(__Userspace__)
14984
#ifdef INET6
14985
int
14986
sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
14987
{
14988
  struct nd_prefix *pfx = NULL;
14989
  struct nd_pfxrouter *pfxrtr = NULL;
14990
  struct sockaddr_in6 gw6;
14991
14992
#if defined(__FreeBSD__)
14993
  if (ro == NULL || ro->ro_nh == NULL || src6->sin6_family != AF_INET6)
14994
#else
14995
  if (ro == NULL || ro->ro_rt == NULL || src6->sin6_family != AF_INET6)
14996
#endif
14997
    return (0);
14998
14999
  /* get prefix entry of address */
15000
#if defined(__FreeBSD__)
15001
  ND6_RLOCK();
15002
#endif
15003
  LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
15004
    if (pfx->ndpr_stateflags & NDPRF_DETACHED)
15005
      continue;
15006
    if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
15007
        &src6->sin6_addr, &pfx->ndpr_mask))
15008
      break;
15009
  }
15010
  /* no prefix entry in the prefix list */
15011
  if (pfx == NULL) {
15012
#if defined(__FreeBSD__)
15013
    ND6_RUNLOCK();
15014
#endif
15015
    SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
15016
    SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15017
    return (0);
15018
  }
15019
15020
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
15021
  SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
15022
15023
  /* search installed gateway from prefix entry */
15024
  LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
15025
    memset(&gw6, 0, sizeof(struct sockaddr_in6));
15026
    gw6.sin6_family = AF_INET6;
15027
#ifdef HAVE_SIN6_LEN
15028
    gw6.sin6_len = sizeof(struct sockaddr_in6);
15029
#endif
15030
    memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
15031
        sizeof(struct in6_addr));
15032
    SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
15033
    SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
15034
    SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
15035
#if defined(__FreeBSD__)
15036
    SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
15037
#else
15038
    SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15039
#endif
15040
#if defined(__FreeBSD__)
15041
    if (sctp_cmpaddr((struct sockaddr *)&gw6, &ro->ro_nh->gw_sa)) {
15042
      ND6_RUNLOCK();
15043
#else
15044
    if (sctp_cmpaddr((struct sockaddr *)&gw6, ro->ro_rt->rt_gateway)) {
15045
#endif
15046
      SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
15047
      return (1);
15048
    }
15049
  }
15050
#if defined(__FreeBSD__)
15051
  ND6_RUNLOCK();
15052
#endif
15053
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
15054
  return (0);
15055
}
15056
#endif
15057
15058
int
15059
sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
15060
{
15061
#ifdef INET
15062
  struct sockaddr_in *sin, *mask;
15063
  struct ifaddr *ifa;
15064
  struct in_addr srcnetaddr, gwnetaddr;
15065
15066
#if defined(__FreeBSD__)
15067
  if (ro == NULL || ro->ro_nh == NULL ||
15068
#else
15069
  if (ro == NULL || ro->ro_rt == NULL ||
15070
#endif
15071
      sifa->address.sa.sa_family != AF_INET) {
15072
    return (0);
15073
  }
15074
  ifa = (struct ifaddr *)sifa->ifa;
15075
  mask = (struct sockaddr_in *)(ifa->ifa_netmask);
15076
  sin = &sifa->address.sin;
15077
  srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15078
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "match_nexthop4: src address is ");
15079
  SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
15080
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "network address is %x\n", srcnetaddr.s_addr);
15081
15082
#if defined(__FreeBSD__)
15083
  sin = &ro->ro_nh->gw4_sa;
15084
#else
15085
  sin = (struct sockaddr_in *)ro->ro_rt->rt_gateway;
15086
#endif
15087
  gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
15088
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "match_nexthop4: nexthop is ");
15089
#if defined(__FreeBSD__)
15090
  SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
15091
#else
15092
  SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, ro->ro_rt->rt_gateway);
15093
#endif
15094
  SCTPDBG(SCTP_DEBUG_OUTPUT2, "network address is %x\n", gwnetaddr.s_addr);
15095
  if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
15096
    return (1);
15097
  }
15098
#endif
15099
  return (0);
15100
}
15101
#elif defined(__Userspace__)
15102
/* TODO __Userspace__ versions of sctp_vXsrc_match_nexthop(). */
15103
int
15104
sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
15105
0
{
15106
0
    return (0);
15107
0
}
15108
int
15109
sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
15110
0
{
15111
0
    return (0);
15112
0
}
15113
15114
#endif