1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 *
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
17 *
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <netinet/sctp_os.h>
36 #include <sys/proc.h>
37 #include <netinet/sctp_var.h>
38 #include <netinet/sctp_sysctl.h>
39 #include <netinet/sctp_header.h>
40 #include <netinet/sctp_pcb.h>
41 #include <netinet/sctputil.h>
42 #include <netinet/sctp_output.h>
43 #include <netinet/sctp_uio.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_auth.h>
46 #include <netinet/sctp_timer.h>
47 #include <netinet/sctp_asconf.h>
48 #include <netinet/sctp_indata.h>
49 #include <netinet/sctp_bsd_addr.h>
50 #include <netinet/sctp_input.h>
51 #include <netinet/sctp_crc32.h>
52 #include <netinet/sctp_kdtrace.h>
53 #if defined(INET) || defined(INET6)
54 #include <netinet/udp.h>
55 #endif
56 #include <netinet/udp_var.h>
57 #include <machine/in_cksum.h>
58
59 #define SCTP_MAX_GAPS_INARRAY 4
60 struct sack_track {
61 uint8_t right_edge; /* mergable on the right edge */
62 uint8_t left_edge; /* mergable on the left edge */
63 uint8_t num_entries;
64 uint8_t spare;
65 struct sctp_gap_ack_block gaps[SCTP_MAX_GAPS_INARRAY];
66 };
67
68 const struct sack_track sack_array[256] = {
69 {0, 0, 0, 0, /* 0x00 */
70 {{0, 0},
71 {0, 0},
72 {0, 0},
73 {0, 0}
74 }
75 },
76 {1, 0, 1, 0, /* 0x01 */
77 {{0, 0},
78 {0, 0},
79 {0, 0},
80 {0, 0}
81 }
82 },
83 {0, 0, 1, 0, /* 0x02 */
84 {{1, 1},
85 {0, 0},
86 {0, 0},
87 {0, 0}
88 }
89 },
90 {1, 0, 1, 0, /* 0x03 */
91 {{0, 1},
92 {0, 0},
93 {0, 0},
94 {0, 0}
95 }
96 },
97 {0, 0, 1, 0, /* 0x04 */
98 {{2, 2},
99 {0, 0},
100 {0, 0},
101 {0, 0}
102 }
103 },
104 {1, 0, 2, 0, /* 0x05 */
105 {{0, 0},
106 {2, 2},
107 {0, 0},
108 {0, 0}
109 }
110 },
111 {0, 0, 1, 0, /* 0x06 */
112 {{1, 2},
113 {0, 0},
114 {0, 0},
115 {0, 0}
116 }
117 },
118 {1, 0, 1, 0, /* 0x07 */
119 {{0, 2},
120 {0, 0},
121 {0, 0},
122 {0, 0}
123 }
124 },
125 {0, 0, 1, 0, /* 0x08 */
126 {{3, 3},
127 {0, 0},
128 {0, 0},
129 {0, 0}
130 }
131 },
132 {1, 0, 2, 0, /* 0x09 */
133 {{0, 0},
134 {3, 3},
135 {0, 0},
136 {0, 0}
137 }
138 },
139 {0, 0, 2, 0, /* 0x0a */
140 {{1, 1},
141 {3, 3},
142 {0, 0},
143 {0, 0}
144 }
145 },
146 {1, 0, 2, 0, /* 0x0b */
147 {{0, 1},
148 {3, 3},
149 {0, 0},
150 {0, 0}
151 }
152 },
153 {0, 0, 1, 0, /* 0x0c */
154 {{2, 3},
155 {0, 0},
156 {0, 0},
157 {0, 0}
158 }
159 },
160 {1, 0, 2, 0, /* 0x0d */
161 {{0, 0},
162 {2, 3},
163 {0, 0},
164 {0, 0}
165 }
166 },
167 {0, 0, 1, 0, /* 0x0e */
168 {{1, 3},
169 {0, 0},
170 {0, 0},
171 {0, 0}
172 }
173 },
174 {1, 0, 1, 0, /* 0x0f */
175 {{0, 3},
176 {0, 0},
177 {0, 0},
178 {0, 0}
179 }
180 },
181 {0, 0, 1, 0, /* 0x10 */
182 {{4, 4},
183 {0, 0},
184 {0, 0},
185 {0, 0}
186 }
187 },
188 {1, 0, 2, 0, /* 0x11 */
189 {{0, 0},
190 {4, 4},
191 {0, 0},
192 {0, 0}
193 }
194 },
195 {0, 0, 2, 0, /* 0x12 */
196 {{1, 1},
197 {4, 4},
198 {0, 0},
199 {0, 0}
200 }
201 },
202 {1, 0, 2, 0, /* 0x13 */
203 {{0, 1},
204 {4, 4},
205 {0, 0},
206 {0, 0}
207 }
208 },
209 {0, 0, 2, 0, /* 0x14 */
210 {{2, 2},
211 {4, 4},
212 {0, 0},
213 {0, 0}
214 }
215 },
216 {1, 0, 3, 0, /* 0x15 */
217 {{0, 0},
218 {2, 2},
219 {4, 4},
220 {0, 0}
221 }
222 },
223 {0, 0, 2, 0, /* 0x16 */
224 {{1, 2},
225 {4, 4},
226 {0, 0},
227 {0, 0}
228 }
229 },
230 {1, 0, 2, 0, /* 0x17 */
231 {{0, 2},
232 {4, 4},
233 {0, 0},
234 {0, 0}
235 }
236 },
237 {0, 0, 1, 0, /* 0x18 */
238 {{3, 4},
239 {0, 0},
240 {0, 0},
241 {0, 0}
242 }
243 },
244 {1, 0, 2, 0, /* 0x19 */
245 {{0, 0},
246 {3, 4},
247 {0, 0},
248 {0, 0}
249 }
250 },
251 {0, 0, 2, 0, /* 0x1a */
252 {{1, 1},
253 {3, 4},
254 {0, 0},
255 {0, 0}
256 }
257 },
258 {1, 0, 2, 0, /* 0x1b */
259 {{0, 1},
260 {3, 4},
261 {0, 0},
262 {0, 0}
263 }
264 },
265 {0, 0, 1, 0, /* 0x1c */
266 {{2, 4},
267 {0, 0},
268 {0, 0},
269 {0, 0}
270 }
271 },
272 {1, 0, 2, 0, /* 0x1d */
273 {{0, 0},
274 {2, 4},
275 {0, 0},
276 {0, 0}
277 }
278 },
279 {0, 0, 1, 0, /* 0x1e */
280 {{1, 4},
281 {0, 0},
282 {0, 0},
283 {0, 0}
284 }
285 },
286 {1, 0, 1, 0, /* 0x1f */
287 {{0, 4},
288 {0, 0},
289 {0, 0},
290 {0, 0}
291 }
292 },
293 {0, 0, 1, 0, /* 0x20 */
294 {{5, 5},
295 {0, 0},
296 {0, 0},
297 {0, 0}
298 }
299 },
300 {1, 0, 2, 0, /* 0x21 */
301 {{0, 0},
302 {5, 5},
303 {0, 0},
304 {0, 0}
305 }
306 },
307 {0, 0, 2, 0, /* 0x22 */
308 {{1, 1},
309 {5, 5},
310 {0, 0},
311 {0, 0}
312 }
313 },
314 {1, 0, 2, 0, /* 0x23 */
315 {{0, 1},
316 {5, 5},
317 {0, 0},
318 {0, 0}
319 }
320 },
321 {0, 0, 2, 0, /* 0x24 */
322 {{2, 2},
323 {5, 5},
324 {0, 0},
325 {0, 0}
326 }
327 },
328 {1, 0, 3, 0, /* 0x25 */
329 {{0, 0},
330 {2, 2},
331 {5, 5},
332 {0, 0}
333 }
334 },
335 {0, 0, 2, 0, /* 0x26 */
336 {{1, 2},
337 {5, 5},
338 {0, 0},
339 {0, 0}
340 }
341 },
342 {1, 0, 2, 0, /* 0x27 */
343 {{0, 2},
344 {5, 5},
345 {0, 0},
346 {0, 0}
347 }
348 },
349 {0, 0, 2, 0, /* 0x28 */
350 {{3, 3},
351 {5, 5},
352 {0, 0},
353 {0, 0}
354 }
355 },
356 {1, 0, 3, 0, /* 0x29 */
357 {{0, 0},
358 {3, 3},
359 {5, 5},
360 {0, 0}
361 }
362 },
363 {0, 0, 3, 0, /* 0x2a */
364 {{1, 1},
365 {3, 3},
366 {5, 5},
367 {0, 0}
368 }
369 },
370 {1, 0, 3, 0, /* 0x2b */
371 {{0, 1},
372 {3, 3},
373 {5, 5},
374 {0, 0}
375 }
376 },
377 {0, 0, 2, 0, /* 0x2c */
378 {{2, 3},
379 {5, 5},
380 {0, 0},
381 {0, 0}
382 }
383 },
384 {1, 0, 3, 0, /* 0x2d */
385 {{0, 0},
386 {2, 3},
387 {5, 5},
388 {0, 0}
389 }
390 },
391 {0, 0, 2, 0, /* 0x2e */
392 {{1, 3},
393 {5, 5},
394 {0, 0},
395 {0, 0}
396 }
397 },
398 {1, 0, 2, 0, /* 0x2f */
399 {{0, 3},
400 {5, 5},
401 {0, 0},
402 {0, 0}
403 }
404 },
405 {0, 0, 1, 0, /* 0x30 */
406 {{4, 5},
407 {0, 0},
408 {0, 0},
409 {0, 0}
410 }
411 },
412 {1, 0, 2, 0, /* 0x31 */
413 {{0, 0},
414 {4, 5},
415 {0, 0},
416 {0, 0}
417 }
418 },
419 {0, 0, 2, 0, /* 0x32 */
420 {{1, 1},
421 {4, 5},
422 {0, 0},
423 {0, 0}
424 }
425 },
426 {1, 0, 2, 0, /* 0x33 */
427 {{0, 1},
428 {4, 5},
429 {0, 0},
430 {0, 0}
431 }
432 },
433 {0, 0, 2, 0, /* 0x34 */
434 {{2, 2},
435 {4, 5},
436 {0, 0},
437 {0, 0}
438 }
439 },
440 {1, 0, 3, 0, /* 0x35 */
441 {{0, 0},
442 {2, 2},
443 {4, 5},
444 {0, 0}
445 }
446 },
447 {0, 0, 2, 0, /* 0x36 */
448 {{1, 2},
449 {4, 5},
450 {0, 0},
451 {0, 0}
452 }
453 },
454 {1, 0, 2, 0, /* 0x37 */
455 {{0, 2},
456 {4, 5},
457 {0, 0},
458 {0, 0}
459 }
460 },
461 {0, 0, 1, 0, /* 0x38 */
462 {{3, 5},
463 {0, 0},
464 {0, 0},
465 {0, 0}
466 }
467 },
468 {1, 0, 2, 0, /* 0x39 */
469 {{0, 0},
470 {3, 5},
471 {0, 0},
472 {0, 0}
473 }
474 },
475 {0, 0, 2, 0, /* 0x3a */
476 {{1, 1},
477 {3, 5},
478 {0, 0},
479 {0, 0}
480 }
481 },
482 {1, 0, 2, 0, /* 0x3b */
483 {{0, 1},
484 {3, 5},
485 {0, 0},
486 {0, 0}
487 }
488 },
489 {0, 0, 1, 0, /* 0x3c */
490 {{2, 5},
491 {0, 0},
492 {0, 0},
493 {0, 0}
494 }
495 },
496 {1, 0, 2, 0, /* 0x3d */
497 {{0, 0},
498 {2, 5},
499 {0, 0},
500 {0, 0}
501 }
502 },
503 {0, 0, 1, 0, /* 0x3e */
504 {{1, 5},
505 {0, 0},
506 {0, 0},
507 {0, 0}
508 }
509 },
510 {1, 0, 1, 0, /* 0x3f */
511 {{0, 5},
512 {0, 0},
513 {0, 0},
514 {0, 0}
515 }
516 },
517 {0, 0, 1, 0, /* 0x40 */
518 {{6, 6},
519 {0, 0},
520 {0, 0},
521 {0, 0}
522 }
523 },
524 {1, 0, 2, 0, /* 0x41 */
525 {{0, 0},
526 {6, 6},
527 {0, 0},
528 {0, 0}
529 }
530 },
531 {0, 0, 2, 0, /* 0x42 */
532 {{1, 1},
533 {6, 6},
534 {0, 0},
535 {0, 0}
536 }
537 },
538 {1, 0, 2, 0, /* 0x43 */
539 {{0, 1},
540 {6, 6},
541 {0, 0},
542 {0, 0}
543 }
544 },
545 {0, 0, 2, 0, /* 0x44 */
546 {{2, 2},
547 {6, 6},
548 {0, 0},
549 {0, 0}
550 }
551 },
552 {1, 0, 3, 0, /* 0x45 */
553 {{0, 0},
554 {2, 2},
555 {6, 6},
556 {0, 0}
557 }
558 },
559 {0, 0, 2, 0, /* 0x46 */
560 {{1, 2},
561 {6, 6},
562 {0, 0},
563 {0, 0}
564 }
565 },
566 {1, 0, 2, 0, /* 0x47 */
567 {{0, 2},
568 {6, 6},
569 {0, 0},
570 {0, 0}
571 }
572 },
573 {0, 0, 2, 0, /* 0x48 */
574 {{3, 3},
575 {6, 6},
576 {0, 0},
577 {0, 0}
578 }
579 },
580 {1, 0, 3, 0, /* 0x49 */
581 {{0, 0},
582 {3, 3},
583 {6, 6},
584 {0, 0}
585 }
586 },
587 {0, 0, 3, 0, /* 0x4a */
588 {{1, 1},
589 {3, 3},
590 {6, 6},
591 {0, 0}
592 }
593 },
594 {1, 0, 3, 0, /* 0x4b */
595 {{0, 1},
596 {3, 3},
597 {6, 6},
598 {0, 0}
599 }
600 },
601 {0, 0, 2, 0, /* 0x4c */
602 {{2, 3},
603 {6, 6},
604 {0, 0},
605 {0, 0}
606 }
607 },
608 {1, 0, 3, 0, /* 0x4d */
609 {{0, 0},
610 {2, 3},
611 {6, 6},
612 {0, 0}
613 }
614 },
615 {0, 0, 2, 0, /* 0x4e */
616 {{1, 3},
617 {6, 6},
618 {0, 0},
619 {0, 0}
620 }
621 },
622 {1, 0, 2, 0, /* 0x4f */
623 {{0, 3},
624 {6, 6},
625 {0, 0},
626 {0, 0}
627 }
628 },
629 {0, 0, 2, 0, /* 0x50 */
630 {{4, 4},
631 {6, 6},
632 {0, 0},
633 {0, 0}
634 }
635 },
636 {1, 0, 3, 0, /* 0x51 */
637 {{0, 0},
638 {4, 4},
639 {6, 6},
640 {0, 0}
641 }
642 },
643 {0, 0, 3, 0, /* 0x52 */
644 {{1, 1},
645 {4, 4},
646 {6, 6},
647 {0, 0}
648 }
649 },
650 {1, 0, 3, 0, /* 0x53 */
651 {{0, 1},
652 {4, 4},
653 {6, 6},
654 {0, 0}
655 }
656 },
657 {0, 0, 3, 0, /* 0x54 */
658 {{2, 2},
659 {4, 4},
660 {6, 6},
661 {0, 0}
662 }
663 },
664 {1, 0, 4, 0, /* 0x55 */
665 {{0, 0},
666 {2, 2},
667 {4, 4},
668 {6, 6}
669 }
670 },
671 {0, 0, 3, 0, /* 0x56 */
672 {{1, 2},
673 {4, 4},
674 {6, 6},
675 {0, 0}
676 }
677 },
678 {1, 0, 3, 0, /* 0x57 */
679 {{0, 2},
680 {4, 4},
681 {6, 6},
682 {0, 0}
683 }
684 },
685 {0, 0, 2, 0, /* 0x58 */
686 {{3, 4},
687 {6, 6},
688 {0, 0},
689 {0, 0}
690 }
691 },
692 {1, 0, 3, 0, /* 0x59 */
693 {{0, 0},
694 {3, 4},
695 {6, 6},
696 {0, 0}
697 }
698 },
699 {0, 0, 3, 0, /* 0x5a */
700 {{1, 1},
701 {3, 4},
702 {6, 6},
703 {0, 0}
704 }
705 },
706 {1, 0, 3, 0, /* 0x5b */
707 {{0, 1},
708 {3, 4},
709 {6, 6},
710 {0, 0}
711 }
712 },
713 {0, 0, 2, 0, /* 0x5c */
714 {{2, 4},
715 {6, 6},
716 {0, 0},
717 {0, 0}
718 }
719 },
720 {1, 0, 3, 0, /* 0x5d */
721 {{0, 0},
722 {2, 4},
723 {6, 6},
724 {0, 0}
725 }
726 },
727 {0, 0, 2, 0, /* 0x5e */
728 {{1, 4},
729 {6, 6},
730 {0, 0},
731 {0, 0}
732 }
733 },
734 {1, 0, 2, 0, /* 0x5f */
735 {{0, 4},
736 {6, 6},
737 {0, 0},
738 {0, 0}
739 }
740 },
741 {0, 0, 1, 0, /* 0x60 */
742 {{5, 6},
743 {0, 0},
744 {0, 0},
745 {0, 0}
746 }
747 },
748 {1, 0, 2, 0, /* 0x61 */
749 {{0, 0},
750 {5, 6},
751 {0, 0},
752 {0, 0}
753 }
754 },
755 {0, 0, 2, 0, /* 0x62 */
756 {{1, 1},
757 {5, 6},
758 {0, 0},
759 {0, 0}
760 }
761 },
762 {1, 0, 2, 0, /* 0x63 */
763 {{0, 1},
764 {5, 6},
765 {0, 0},
766 {0, 0}
767 }
768 },
769 {0, 0, 2, 0, /* 0x64 */
770 {{2, 2},
771 {5, 6},
772 {0, 0},
773 {0, 0}
774 }
775 },
776 {1, 0, 3, 0, /* 0x65 */
777 {{0, 0},
778 {2, 2},
779 {5, 6},
780 {0, 0}
781 }
782 },
783 {0, 0, 2, 0, /* 0x66 */
784 {{1, 2},
785 {5, 6},
786 {0, 0},
787 {0, 0}
788 }
789 },
790 {1, 0, 2, 0, /* 0x67 */
791 {{0, 2},
792 {5, 6},
793 {0, 0},
794 {0, 0}
795 }
796 },
797 {0, 0, 2, 0, /* 0x68 */
798 {{3, 3},
799 {5, 6},
800 {0, 0},
801 {0, 0}
802 }
803 },
804 {1, 0, 3, 0, /* 0x69 */
805 {{0, 0},
806 {3, 3},
807 {5, 6},
808 {0, 0}
809 }
810 },
811 {0, 0, 3, 0, /* 0x6a */
812 {{1, 1},
813 {3, 3},
814 {5, 6},
815 {0, 0}
816 }
817 },
818 {1, 0, 3, 0, /* 0x6b */
819 {{0, 1},
820 {3, 3},
821 {5, 6},
822 {0, 0}
823 }
824 },
825 {0, 0, 2, 0, /* 0x6c */
826 {{2, 3},
827 {5, 6},
828 {0, 0},
829 {0, 0}
830 }
831 },
832 {1, 0, 3, 0, /* 0x6d */
833 {{0, 0},
834 {2, 3},
835 {5, 6},
836 {0, 0}
837 }
838 },
839 {0, 0, 2, 0, /* 0x6e */
840 {{1, 3},
841 {5, 6},
842 {0, 0},
843 {0, 0}
844 }
845 },
846 {1, 0, 2, 0, /* 0x6f */
847 {{0, 3},
848 {5, 6},
849 {0, 0},
850 {0, 0}
851 }
852 },
853 {0, 0, 1, 0, /* 0x70 */
854 {{4, 6},
855 {0, 0},
856 {0, 0},
857 {0, 0}
858 }
859 },
860 {1, 0, 2, 0, /* 0x71 */
861 {{0, 0},
862 {4, 6},
863 {0, 0},
864 {0, 0}
865 }
866 },
867 {0, 0, 2, 0, /* 0x72 */
868 {{1, 1},
869 {4, 6},
870 {0, 0},
871 {0, 0}
872 }
873 },
874 {1, 0, 2, 0, /* 0x73 */
875 {{0, 1},
876 {4, 6},
877 {0, 0},
878 {0, 0}
879 }
880 },
881 {0, 0, 2, 0, /* 0x74 */
882 {{2, 2},
883 {4, 6},
884 {0, 0},
885 {0, 0}
886 }
887 },
888 {1, 0, 3, 0, /* 0x75 */
889 {{0, 0},
890 {2, 2},
891 {4, 6},
892 {0, 0}
893 }
894 },
895 {0, 0, 2, 0, /* 0x76 */
896 {{1, 2},
897 {4, 6},
898 {0, 0},
899 {0, 0}
900 }
901 },
902 {1, 0, 2, 0, /* 0x77 */
903 {{0, 2},
904 {4, 6},
905 {0, 0},
906 {0, 0}
907 }
908 },
909 {0, 0, 1, 0, /* 0x78 */
910 {{3, 6},
911 {0, 0},
912 {0, 0},
913 {0, 0}
914 }
915 },
916 {1, 0, 2, 0, /* 0x79 */
917 {{0, 0},
918 {3, 6},
919 {0, 0},
920 {0, 0}
921 }
922 },
923 {0, 0, 2, 0, /* 0x7a */
924 {{1, 1},
925 {3, 6},
926 {0, 0},
927 {0, 0}
928 }
929 },
930 {1, 0, 2, 0, /* 0x7b */
931 {{0, 1},
932 {3, 6},
933 {0, 0},
934 {0, 0}
935 }
936 },
937 {0, 0, 1, 0, /* 0x7c */
938 {{2, 6},
939 {0, 0},
940 {0, 0},
941 {0, 0}
942 }
943 },
944 {1, 0, 2, 0, /* 0x7d */
945 {{0, 0},
946 {2, 6},
947 {0, 0},
948 {0, 0}
949 }
950 },
951 {0, 0, 1, 0, /* 0x7e */
952 {{1, 6},
953 {0, 0},
954 {0, 0},
955 {0, 0}
956 }
957 },
958 {1, 0, 1, 0, /* 0x7f */
959 {{0, 6},
960 {0, 0},
961 {0, 0},
962 {0, 0}
963 }
964 },
965 {0, 1, 1, 0, /* 0x80 */
966 {{7, 7},
967 {0, 0},
968 {0, 0},
969 {0, 0}
970 }
971 },
972 {1, 1, 2, 0, /* 0x81 */
973 {{0, 0},
974 {7, 7},
975 {0, 0},
976 {0, 0}
977 }
978 },
979 {0, 1, 2, 0, /* 0x82 */
980 {{1, 1},
981 {7, 7},
982 {0, 0},
983 {0, 0}
984 }
985 },
986 {1, 1, 2, 0, /* 0x83 */
987 {{0, 1},
988 {7, 7},
989 {0, 0},
990 {0, 0}
991 }
992 },
993 {0, 1, 2, 0, /* 0x84 */
994 {{2, 2},
995 {7, 7},
996 {0, 0},
997 {0, 0}
998 }
999 },
1000 {1, 1, 3, 0, /* 0x85 */
1001 {{0, 0},
1002 {2, 2},
1003 {7, 7},
1004 {0, 0}
1005 }
1006 },
1007 {0, 1, 2, 0, /* 0x86 */
1008 {{1, 2},
1009 {7, 7},
1010 {0, 0},
1011 {0, 0}
1012 }
1013 },
1014 {1, 1, 2, 0, /* 0x87 */
1015 {{0, 2},
1016 {7, 7},
1017 {0, 0},
1018 {0, 0}
1019 }
1020 },
1021 {0, 1, 2, 0, /* 0x88 */
1022 {{3, 3},
1023 {7, 7},
1024 {0, 0},
1025 {0, 0}
1026 }
1027 },
1028 {1, 1, 3, 0, /* 0x89 */
1029 {{0, 0},
1030 {3, 3},
1031 {7, 7},
1032 {0, 0}
1033 }
1034 },
1035 {0, 1, 3, 0, /* 0x8a */
1036 {{1, 1},
1037 {3, 3},
1038 {7, 7},
1039 {0, 0}
1040 }
1041 },
1042 {1, 1, 3, 0, /* 0x8b */
1043 {{0, 1},
1044 {3, 3},
1045 {7, 7},
1046 {0, 0}
1047 }
1048 },
1049 {0, 1, 2, 0, /* 0x8c */
1050 {{2, 3},
1051 {7, 7},
1052 {0, 0},
1053 {0, 0}
1054 }
1055 },
1056 {1, 1, 3, 0, /* 0x8d */
1057 {{0, 0},
1058 {2, 3},
1059 {7, 7},
1060 {0, 0}
1061 }
1062 },
1063 {0, 1, 2, 0, /* 0x8e */
1064 {{1, 3},
1065 {7, 7},
1066 {0, 0},
1067 {0, 0}
1068 }
1069 },
1070 {1, 1, 2, 0, /* 0x8f */
1071 {{0, 3},
1072 {7, 7},
1073 {0, 0},
1074 {0, 0}
1075 }
1076 },
1077 {0, 1, 2, 0, /* 0x90 */
1078 {{4, 4},
1079 {7, 7},
1080 {0, 0},
1081 {0, 0}
1082 }
1083 },
1084 {1, 1, 3, 0, /* 0x91 */
1085 {{0, 0},
1086 {4, 4},
1087 {7, 7},
1088 {0, 0}
1089 }
1090 },
1091 {0, 1, 3, 0, /* 0x92 */
1092 {{1, 1},
1093 {4, 4},
1094 {7, 7},
1095 {0, 0}
1096 }
1097 },
1098 {1, 1, 3, 0, /* 0x93 */
1099 {{0, 1},
1100 {4, 4},
1101 {7, 7},
1102 {0, 0}
1103 }
1104 },
1105 {0, 1, 3, 0, /* 0x94 */
1106 {{2, 2},
1107 {4, 4},
1108 {7, 7},
1109 {0, 0}
1110 }
1111 },
1112 {1, 1, 4, 0, /* 0x95 */
1113 {{0, 0},
1114 {2, 2},
1115 {4, 4},
1116 {7, 7}
1117 }
1118 },
1119 {0, 1, 3, 0, /* 0x96 */
1120 {{1, 2},
1121 {4, 4},
1122 {7, 7},
1123 {0, 0}
1124 }
1125 },
1126 {1, 1, 3, 0, /* 0x97 */
1127 {{0, 2},
1128 {4, 4},
1129 {7, 7},
1130 {0, 0}
1131 }
1132 },
1133 {0, 1, 2, 0, /* 0x98 */
1134 {{3, 4},
1135 {7, 7},
1136 {0, 0},
1137 {0, 0}
1138 }
1139 },
1140 {1, 1, 3, 0, /* 0x99 */
1141 {{0, 0},
1142 {3, 4},
1143 {7, 7},
1144 {0, 0}
1145 }
1146 },
1147 {0, 1, 3, 0, /* 0x9a */
1148 {{1, 1},
1149 {3, 4},
1150 {7, 7},
1151 {0, 0}
1152 }
1153 },
1154 {1, 1, 3, 0, /* 0x9b */
1155 {{0, 1},
1156 {3, 4},
1157 {7, 7},
1158 {0, 0}
1159 }
1160 },
1161 {0, 1, 2, 0, /* 0x9c */
1162 {{2, 4},
1163 {7, 7},
1164 {0, 0},
1165 {0, 0}
1166 }
1167 },
1168 {1, 1, 3, 0, /* 0x9d */
1169 {{0, 0},
1170 {2, 4},
1171 {7, 7},
1172 {0, 0}
1173 }
1174 },
1175 {0, 1, 2, 0, /* 0x9e */
1176 {{1, 4},
1177 {7, 7},
1178 {0, 0},
1179 {0, 0}
1180 }
1181 },
1182 {1, 1, 2, 0, /* 0x9f */
1183 {{0, 4},
1184 {7, 7},
1185 {0, 0},
1186 {0, 0}
1187 }
1188 },
1189 {0, 1, 2, 0, /* 0xa0 */
1190 {{5, 5},
1191 {7, 7},
1192 {0, 0},
1193 {0, 0}
1194 }
1195 },
1196 {1, 1, 3, 0, /* 0xa1 */
1197 {{0, 0},
1198 {5, 5},
1199 {7, 7},
1200 {0, 0}
1201 }
1202 },
1203 {0, 1, 3, 0, /* 0xa2 */
1204 {{1, 1},
1205 {5, 5},
1206 {7, 7},
1207 {0, 0}
1208 }
1209 },
1210 {1, 1, 3, 0, /* 0xa3 */
1211 {{0, 1},
1212 {5, 5},
1213 {7, 7},
1214 {0, 0}
1215 }
1216 },
1217 {0, 1, 3, 0, /* 0xa4 */
1218 {{2, 2},
1219 {5, 5},
1220 {7, 7},
1221 {0, 0}
1222 }
1223 },
1224 {1, 1, 4, 0, /* 0xa5 */
1225 {{0, 0},
1226 {2, 2},
1227 {5, 5},
1228 {7, 7}
1229 }
1230 },
1231 {0, 1, 3, 0, /* 0xa6 */
1232 {{1, 2},
1233 {5, 5},
1234 {7, 7},
1235 {0, 0}
1236 }
1237 },
1238 {1, 1, 3, 0, /* 0xa7 */
1239 {{0, 2},
1240 {5, 5},
1241 {7, 7},
1242 {0, 0}
1243 }
1244 },
1245 {0, 1, 3, 0, /* 0xa8 */
1246 {{3, 3},
1247 {5, 5},
1248 {7, 7},
1249 {0, 0}
1250 }
1251 },
1252 {1, 1, 4, 0, /* 0xa9 */
1253 {{0, 0},
1254 {3, 3},
1255 {5, 5},
1256 {7, 7}
1257 }
1258 },
1259 {0, 1, 4, 0, /* 0xaa */
1260 {{1, 1},
1261 {3, 3},
1262 {5, 5},
1263 {7, 7}
1264 }
1265 },
1266 {1, 1, 4, 0, /* 0xab */
1267 {{0, 1},
1268 {3, 3},
1269 {5, 5},
1270 {7, 7}
1271 }
1272 },
1273 {0, 1, 3, 0, /* 0xac */
1274 {{2, 3},
1275 {5, 5},
1276 {7, 7},
1277 {0, 0}
1278 }
1279 },
1280 {1, 1, 4, 0, /* 0xad */
1281 {{0, 0},
1282 {2, 3},
1283 {5, 5},
1284 {7, 7}
1285 }
1286 },
1287 {0, 1, 3, 0, /* 0xae */
1288 {{1, 3},
1289 {5, 5},
1290 {7, 7},
1291 {0, 0}
1292 }
1293 },
1294 {1, 1, 3, 0, /* 0xaf */
1295 {{0, 3},
1296 {5, 5},
1297 {7, 7},
1298 {0, 0}
1299 }
1300 },
1301 {0, 1, 2, 0, /* 0xb0 */
1302 {{4, 5},
1303 {7, 7},
1304 {0, 0},
1305 {0, 0}
1306 }
1307 },
1308 {1, 1, 3, 0, /* 0xb1 */
1309 {{0, 0},
1310 {4, 5},
1311 {7, 7},
1312 {0, 0}
1313 }
1314 },
1315 {0, 1, 3, 0, /* 0xb2 */
1316 {{1, 1},
1317 {4, 5},
1318 {7, 7},
1319 {0, 0}
1320 }
1321 },
1322 {1, 1, 3, 0, /* 0xb3 */
1323 {{0, 1},
1324 {4, 5},
1325 {7, 7},
1326 {0, 0}
1327 }
1328 },
1329 {0, 1, 3, 0, /* 0xb4 */
1330 {{2, 2},
1331 {4, 5},
1332 {7, 7},
1333 {0, 0}
1334 }
1335 },
1336 {1, 1, 4, 0, /* 0xb5 */
1337 {{0, 0},
1338 {2, 2},
1339 {4, 5},
1340 {7, 7}
1341 }
1342 },
1343 {0, 1, 3, 0, /* 0xb6 */
1344 {{1, 2},
1345 {4, 5},
1346 {7, 7},
1347 {0, 0}
1348 }
1349 },
1350 {1, 1, 3, 0, /* 0xb7 */
1351 {{0, 2},
1352 {4, 5},
1353 {7, 7},
1354 {0, 0}
1355 }
1356 },
1357 {0, 1, 2, 0, /* 0xb8 */
1358 {{3, 5},
1359 {7, 7},
1360 {0, 0},
1361 {0, 0}
1362 }
1363 },
1364 {1, 1, 3, 0, /* 0xb9 */
1365 {{0, 0},
1366 {3, 5},
1367 {7, 7},
1368 {0, 0}
1369 }
1370 },
1371 {0, 1, 3, 0, /* 0xba */
1372 {{1, 1},
1373 {3, 5},
1374 {7, 7},
1375 {0, 0}
1376 }
1377 },
1378 {1, 1, 3, 0, /* 0xbb */
1379 {{0, 1},
1380 {3, 5},
1381 {7, 7},
1382 {0, 0}
1383 }
1384 },
1385 {0, 1, 2, 0, /* 0xbc */
1386 {{2, 5},
1387 {7, 7},
1388 {0, 0},
1389 {0, 0}
1390 }
1391 },
1392 {1, 1, 3, 0, /* 0xbd */
1393 {{0, 0},
1394 {2, 5},
1395 {7, 7},
1396 {0, 0}
1397 }
1398 },
1399 {0, 1, 2, 0, /* 0xbe */
1400 {{1, 5},
1401 {7, 7},
1402 {0, 0},
1403 {0, 0}
1404 }
1405 },
1406 {1, 1, 2, 0, /* 0xbf */
1407 {{0, 5},
1408 {7, 7},
1409 {0, 0},
1410 {0, 0}
1411 }
1412 },
1413 {0, 1, 1, 0, /* 0xc0 */
1414 {{6, 7},
1415 {0, 0},
1416 {0, 0},
1417 {0, 0}
1418 }
1419 },
1420 {1, 1, 2, 0, /* 0xc1 */
1421 {{0, 0},
1422 {6, 7},
1423 {0, 0},
1424 {0, 0}
1425 }
1426 },
1427 {0, 1, 2, 0, /* 0xc2 */
1428 {{1, 1},
1429 {6, 7},
1430 {0, 0},
1431 {0, 0}
1432 }
1433 },
1434 {1, 1, 2, 0, /* 0xc3 */
1435 {{0, 1},
1436 {6, 7},
1437 {0, 0},
1438 {0, 0}
1439 }
1440 },
1441 {0, 1, 2, 0, /* 0xc4 */
1442 {{2, 2},
1443 {6, 7},
1444 {0, 0},
1445 {0, 0}
1446 }
1447 },
1448 {1, 1, 3, 0, /* 0xc5 */
1449 {{0, 0},
1450 {2, 2},
1451 {6, 7},
1452 {0, 0}
1453 }
1454 },
1455 {0, 1, 2, 0, /* 0xc6 */
1456 {{1, 2},
1457 {6, 7},
1458 {0, 0},
1459 {0, 0}
1460 }
1461 },
1462 {1, 1, 2, 0, /* 0xc7 */
1463 {{0, 2},
1464 {6, 7},
1465 {0, 0},
1466 {0, 0}
1467 }
1468 },
1469 {0, 1, 2, 0, /* 0xc8 */
1470 {{3, 3},
1471 {6, 7},
1472 {0, 0},
1473 {0, 0}
1474 }
1475 },
1476 {1, 1, 3, 0, /* 0xc9 */
1477 {{0, 0},
1478 {3, 3},
1479 {6, 7},
1480 {0, 0}
1481 }
1482 },
1483 {0, 1, 3, 0, /* 0xca */
1484 {{1, 1},
1485 {3, 3},
1486 {6, 7},
1487 {0, 0}
1488 }
1489 },
1490 {1, 1, 3, 0, /* 0xcb */
1491 {{0, 1},
1492 {3, 3},
1493 {6, 7},
1494 {0, 0}
1495 }
1496 },
1497 {0, 1, 2, 0, /* 0xcc */
1498 {{2, 3},
1499 {6, 7},
1500 {0, 0},
1501 {0, 0}
1502 }
1503 },
1504 {1, 1, 3, 0, /* 0xcd */
1505 {{0, 0},
1506 {2, 3},
1507 {6, 7},
1508 {0, 0}
1509 }
1510 },
1511 {0, 1, 2, 0, /* 0xce */
1512 {{1, 3},
1513 {6, 7},
1514 {0, 0},
1515 {0, 0}
1516 }
1517 },
1518 {1, 1, 2, 0, /* 0xcf */
1519 {{0, 3},
1520 {6, 7},
1521 {0, 0},
1522 {0, 0}
1523 }
1524 },
1525 {0, 1, 2, 0, /* 0xd0 */
1526 {{4, 4},
1527 {6, 7},
1528 {0, 0},
1529 {0, 0}
1530 }
1531 },
1532 {1, 1, 3, 0, /* 0xd1 */
1533 {{0, 0},
1534 {4, 4},
1535 {6, 7},
1536 {0, 0}
1537 }
1538 },
1539 {0, 1, 3, 0, /* 0xd2 */
1540 {{1, 1},
1541 {4, 4},
1542 {6, 7},
1543 {0, 0}
1544 }
1545 },
1546 {1, 1, 3, 0, /* 0xd3 */
1547 {{0, 1},
1548 {4, 4},
1549 {6, 7},
1550 {0, 0}
1551 }
1552 },
1553 {0, 1, 3, 0, /* 0xd4 */
1554 {{2, 2},
1555 {4, 4},
1556 {6, 7},
1557 {0, 0}
1558 }
1559 },
1560 {1, 1, 4, 0, /* 0xd5 */
1561 {{0, 0},
1562 {2, 2},
1563 {4, 4},
1564 {6, 7}
1565 }
1566 },
1567 {0, 1, 3, 0, /* 0xd6 */
1568 {{1, 2},
1569 {4, 4},
1570 {6, 7},
1571 {0, 0}
1572 }
1573 },
1574 {1, 1, 3, 0, /* 0xd7 */
1575 {{0, 2},
1576 {4, 4},
1577 {6, 7},
1578 {0, 0}
1579 }
1580 },
1581 {0, 1, 2, 0, /* 0xd8 */
1582 {{3, 4},
1583 {6, 7},
1584 {0, 0},
1585 {0, 0}
1586 }
1587 },
1588 {1, 1, 3, 0, /* 0xd9 */
1589 {{0, 0},
1590 {3, 4},
1591 {6, 7},
1592 {0, 0}
1593 }
1594 },
1595 {0, 1, 3, 0, /* 0xda */
1596 {{1, 1},
1597 {3, 4},
1598 {6, 7},
1599 {0, 0}
1600 }
1601 },
1602 {1, 1, 3, 0, /* 0xdb */
1603 {{0, 1},
1604 {3, 4},
1605 {6, 7},
1606 {0, 0}
1607 }
1608 },
1609 {0, 1, 2, 0, /* 0xdc */
1610 {{2, 4},
1611 {6, 7},
1612 {0, 0},
1613 {0, 0}
1614 }
1615 },
1616 {1, 1, 3, 0, /* 0xdd */
1617 {{0, 0},
1618 {2, 4},
1619 {6, 7},
1620 {0, 0}
1621 }
1622 },
1623 {0, 1, 2, 0, /* 0xde */
1624 {{1, 4},
1625 {6, 7},
1626 {0, 0},
1627 {0, 0}
1628 }
1629 },
1630 {1, 1, 2, 0, /* 0xdf */
1631 {{0, 4},
1632 {6, 7},
1633 {0, 0},
1634 {0, 0}
1635 }
1636 },
1637 {0, 1, 1, 0, /* 0xe0 */
1638 {{5, 7},
1639 {0, 0},
1640 {0, 0},
1641 {0, 0}
1642 }
1643 },
1644 {1, 1, 2, 0, /* 0xe1 */
1645 {{0, 0},
1646 {5, 7},
1647 {0, 0},
1648 {0, 0}
1649 }
1650 },
1651 {0, 1, 2, 0, /* 0xe2 */
1652 {{1, 1},
1653 {5, 7},
1654 {0, 0},
1655 {0, 0}
1656 }
1657 },
1658 {1, 1, 2, 0, /* 0xe3 */
1659 {{0, 1},
1660 {5, 7},
1661 {0, 0},
1662 {0, 0}
1663 }
1664 },
1665 {0, 1, 2, 0, /* 0xe4 */
1666 {{2, 2},
1667 {5, 7},
1668 {0, 0},
1669 {0, 0}
1670 }
1671 },
1672 {1, 1, 3, 0, /* 0xe5 */
1673 {{0, 0},
1674 {2, 2},
1675 {5, 7},
1676 {0, 0}
1677 }
1678 },
1679 {0, 1, 2, 0, /* 0xe6 */
1680 {{1, 2},
1681 {5, 7},
1682 {0, 0},
1683 {0, 0}
1684 }
1685 },
1686 {1, 1, 2, 0, /* 0xe7 */
1687 {{0, 2},
1688 {5, 7},
1689 {0, 0},
1690 {0, 0}
1691 }
1692 },
1693 {0, 1, 2, 0, /* 0xe8 */
1694 {{3, 3},
1695 {5, 7},
1696 {0, 0},
1697 {0, 0}
1698 }
1699 },
1700 {1, 1, 3, 0, /* 0xe9 */
1701 {{0, 0},
1702 {3, 3},
1703 {5, 7},
1704 {0, 0}
1705 }
1706 },
1707 {0, 1, 3, 0, /* 0xea */
1708 {{1, 1},
1709 {3, 3},
1710 {5, 7},
1711 {0, 0}
1712 }
1713 },
1714 {1, 1, 3, 0, /* 0xeb */
1715 {{0, 1},
1716 {3, 3},
1717 {5, 7},
1718 {0, 0}
1719 }
1720 },
1721 {0, 1, 2, 0, /* 0xec */
1722 {{2, 3},
1723 {5, 7},
1724 {0, 0},
1725 {0, 0}
1726 }
1727 },
1728 {1, 1, 3, 0, /* 0xed */
1729 {{0, 0},
1730 {2, 3},
1731 {5, 7},
1732 {0, 0}
1733 }
1734 },
1735 {0, 1, 2, 0, /* 0xee */
1736 {{1, 3},
1737 {5, 7},
1738 {0, 0},
1739 {0, 0}
1740 }
1741 },
1742 {1, 1, 2, 0, /* 0xef */
1743 {{0, 3},
1744 {5, 7},
1745 {0, 0},
1746 {0, 0}
1747 }
1748 },
1749 {0, 1, 1, 0, /* 0xf0 */
1750 {{4, 7},
1751 {0, 0},
1752 {0, 0},
1753 {0, 0}
1754 }
1755 },
1756 {1, 1, 2, 0, /* 0xf1 */
1757 {{0, 0},
1758 {4, 7},
1759 {0, 0},
1760 {0, 0}
1761 }
1762 },
1763 {0, 1, 2, 0, /* 0xf2 */
1764 {{1, 1},
1765 {4, 7},
1766 {0, 0},
1767 {0, 0}
1768 }
1769 },
1770 {1, 1, 2, 0, /* 0xf3 */
1771 {{0, 1},
1772 {4, 7},
1773 {0, 0},
1774 {0, 0}
1775 }
1776 },
1777 {0, 1, 2, 0, /* 0xf4 */
1778 {{2, 2},
1779 {4, 7},
1780 {0, 0},
1781 {0, 0}
1782 }
1783 },
1784 {1, 1, 3, 0, /* 0xf5 */
1785 {{0, 0},
1786 {2, 2},
1787 {4, 7},
1788 {0, 0}
1789 }
1790 },
1791 {0, 1, 2, 0, /* 0xf6 */
1792 {{1, 2},
1793 {4, 7},
1794 {0, 0},
1795 {0, 0}
1796 }
1797 },
1798 {1, 1, 2, 0, /* 0xf7 */
1799 {{0, 2},
1800 {4, 7},
1801 {0, 0},
1802 {0, 0}
1803 }
1804 },
1805 {0, 1, 1, 0, /* 0xf8 */
1806 {{3, 7},
1807 {0, 0},
1808 {0, 0},
1809 {0, 0}
1810 }
1811 },
1812 {1, 1, 2, 0, /* 0xf9 */
1813 {{0, 0},
1814 {3, 7},
1815 {0, 0},
1816 {0, 0}
1817 }
1818 },
1819 {0, 1, 2, 0, /* 0xfa */
1820 {{1, 1},
1821 {3, 7},
1822 {0, 0},
1823 {0, 0}
1824 }
1825 },
1826 {1, 1, 2, 0, /* 0xfb */
1827 {{0, 1},
1828 {3, 7},
1829 {0, 0},
1830 {0, 0}
1831 }
1832 },
1833 {0, 1, 1, 0, /* 0xfc */
1834 {{2, 7},
1835 {0, 0},
1836 {0, 0},
1837 {0, 0}
1838 }
1839 },
1840 {1, 1, 2, 0, /* 0xfd */
1841 {{0, 0},
1842 {2, 7},
1843 {0, 0},
1844 {0, 0}
1845 }
1846 },
1847 {0, 1, 1, 0, /* 0xfe */
1848 {{1, 7},
1849 {0, 0},
1850 {0, 0},
1851 {0, 0}
1852 }
1853 },
1854 {1, 1, 1, 0, /* 0xff */
1855 {{0, 7},
1856 {0, 0},
1857 {0, 0},
1858 {0, 0}
1859 }
1860 }
1861 };
1862
1863 int
sctp_is_address_in_scope(struct sctp_ifa * ifa,struct sctp_scoping * scope,int do_update)1864 sctp_is_address_in_scope(struct sctp_ifa *ifa,
1865 struct sctp_scoping *scope,
1866 int do_update)
1867 {
1868 if ((scope->loopback_scope == 0) &&
1869 (ifa->ifn_p) && SCTP_IFN_IS_IFT_LOOP(ifa->ifn_p)) {
1870 /*
1871 * skip loopback if not in scope *
1872 */
1873 return (0);
1874 }
1875 switch (ifa->address.sa.sa_family) {
1876 #ifdef INET
1877 case AF_INET:
1878 if (scope->ipv4_addr_legal) {
1879 struct sockaddr_in *sin;
1880
1881 sin = &ifa->address.sin;
1882 if (sin->sin_addr.s_addr == 0) {
1883 /* not in scope , unspecified */
1884 return (0);
1885 }
1886 if ((scope->ipv4_local_scope == 0) &&
1887 (IN4_ISPRIVATE_ADDRESS(&sin->sin_addr))) {
1888 /* private address not in scope */
1889 return (0);
1890 }
1891 } else {
1892 return (0);
1893 }
1894 break;
1895 #endif
1896 #ifdef INET6
1897 case AF_INET6:
1898 if (scope->ipv6_addr_legal) {
1899 struct sockaddr_in6 *sin6;
1900
1901 /*
1902 * Must update the flags, bummer, which means any
1903 * IFA locks must now be applied HERE <->
1904 */
1905 if (do_update) {
1906 sctp_gather_internal_ifa_flags(ifa);
1907 }
1908 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
1909 return (0);
1910 }
1911 /* ok to use deprecated addresses? */
1912 sin6 = &ifa->address.sin6;
1913 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
1914 /* skip unspecified addresses */
1915 return (0);
1916 }
1917 if ( /* (local_scope == 0) && */
1918 (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))) {
1919 return (0);
1920 }
1921 if ((scope->site_scope == 0) &&
1922 (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr))) {
1923 return (0);
1924 }
1925 } else {
1926 return (0);
1927 }
1928 break;
1929 #endif
1930 default:
1931 return (0);
1932 }
1933 return (1);
1934 }
1935
1936 static struct mbuf *
sctp_add_addr_to_mbuf(struct mbuf * m,struct sctp_ifa * ifa,uint16_t * len)1937 sctp_add_addr_to_mbuf(struct mbuf *m, struct sctp_ifa *ifa, uint16_t *len)
1938 {
1939 #if defined(INET) || defined(INET6)
1940 struct sctp_paramhdr *paramh;
1941 struct mbuf *mret;
1942 uint16_t plen;
1943 #endif
1944
1945 switch (ifa->address.sa.sa_family) {
1946 #ifdef INET
1947 case AF_INET:
1948 plen = (uint16_t)sizeof(struct sctp_ipv4addr_param);
1949 break;
1950 #endif
1951 #ifdef INET6
1952 case AF_INET6:
1953 plen = (uint16_t)sizeof(struct sctp_ipv6addr_param);
1954 break;
1955 #endif
1956 default:
1957 return (m);
1958 }
1959 #if defined(INET) || defined(INET6)
1960 if (M_TRAILINGSPACE(m) >= plen) {
1961 /* easy side we just drop it on the end */
1962 paramh = (struct sctp_paramhdr *)(SCTP_BUF_AT(m, SCTP_BUF_LEN(m)));
1963 mret = m;
1964 } else {
1965 /* Need more space */
1966 mret = m;
1967 while (SCTP_BUF_NEXT(mret) != NULL) {
1968 mret = SCTP_BUF_NEXT(mret);
1969 }
1970 SCTP_BUF_NEXT(mret) = sctp_get_mbuf_for_msg(plen, 0, M_NOWAIT, 1, MT_DATA);
1971 if (SCTP_BUF_NEXT(mret) == NULL) {
1972 /* We are hosed, can't add more addresses */
1973 return (m);
1974 }
1975 mret = SCTP_BUF_NEXT(mret);
1976 paramh = mtod(mret, struct sctp_paramhdr *);
1977 }
1978 /* now add the parameter */
1979 switch (ifa->address.sa.sa_family) {
1980 #ifdef INET
1981 case AF_INET:
1982 {
1983 struct sctp_ipv4addr_param *ipv4p;
1984 struct sockaddr_in *sin;
1985
1986 sin = &ifa->address.sin;
1987 ipv4p = (struct sctp_ipv4addr_param *)paramh;
1988 paramh->param_type = htons(SCTP_IPV4_ADDRESS);
1989 paramh->param_length = htons(plen);
1990 ipv4p->addr = sin->sin_addr.s_addr;
1991 SCTP_BUF_LEN(mret) += plen;
1992 break;
1993 }
1994 #endif
1995 #ifdef INET6
1996 case AF_INET6:
1997 {
1998 struct sctp_ipv6addr_param *ipv6p;
1999 struct sockaddr_in6 *sin6;
2000
2001 sin6 = &ifa->address.sin6;
2002 ipv6p = (struct sctp_ipv6addr_param *)paramh;
2003 paramh->param_type = htons(SCTP_IPV6_ADDRESS);
2004 paramh->param_length = htons(plen);
2005 memcpy(ipv6p->addr, &sin6->sin6_addr,
2006 sizeof(ipv6p->addr));
2007 /* clear embedded scope in the address */
2008 in6_clearscope((struct in6_addr *)ipv6p->addr);
2009 SCTP_BUF_LEN(mret) += plen;
2010 break;
2011 }
2012 #endif
2013 default:
2014 return (m);
2015 }
2016 if (len != NULL) {
2017 *len += plen;
2018 }
2019 return (mret);
2020 #endif
2021 }
2022
2023 struct mbuf *
sctp_add_addresses_to_i_ia(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_scoping * scope,struct mbuf * m_at,int cnt_inits_to,uint16_t * padding_len,uint16_t * chunk_len)2024 sctp_add_addresses_to_i_ia(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
2025 struct sctp_scoping *scope,
2026 struct mbuf *m_at, int cnt_inits_to,
2027 uint16_t *padding_len, uint16_t *chunk_len)
2028 {
2029 struct sctp_vrf *vrf = NULL;
2030 int cnt, limit_out = 0, total_count;
2031 uint32_t vrf_id;
2032
2033 vrf_id = inp->def_vrf_id;
2034 SCTP_IPI_ADDR_RLOCK();
2035 vrf = sctp_find_vrf(vrf_id);
2036 if (vrf == NULL) {
2037 SCTP_IPI_ADDR_RUNLOCK();
2038 return (m_at);
2039 }
2040 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
2041 struct sctp_ifa *sctp_ifap;
2042 struct sctp_ifn *sctp_ifnp;
2043
2044 cnt = cnt_inits_to;
2045 if (vrf->total_ifa_count > SCTP_COUNT_LIMIT) {
2046 limit_out = 1;
2047 cnt = SCTP_ADDRESS_LIMIT;
2048 goto skip_count;
2049 }
2050 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2051 if ((scope->loopback_scope == 0) &&
2052 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2053 /*
2054 * Skip loopback devices if loopback_scope
2055 * not set
2056 */
2057 continue;
2058 }
2059 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2060 #ifdef INET
2061 if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2062 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2063 &sctp_ifap->address.sin.sin_addr) != 0)) {
2064 continue;
2065 }
2066 #endif
2067 #ifdef INET6
2068 if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2069 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2070 &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2071 continue;
2072 }
2073 #endif
2074 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2075 continue;
2076 }
2077 if (sctp_is_address_in_scope(sctp_ifap, scope, 1) == 0) {
2078 continue;
2079 }
2080 cnt++;
2081 if (cnt > SCTP_ADDRESS_LIMIT) {
2082 break;
2083 }
2084 }
2085 if (cnt > SCTP_ADDRESS_LIMIT) {
2086 break;
2087 }
2088 }
2089 skip_count:
2090 if (cnt > 1) {
2091 total_count = 0;
2092 LIST_FOREACH(sctp_ifnp, &vrf->ifnlist, next_ifn) {
2093 cnt = 0;
2094 if ((scope->loopback_scope == 0) &&
2095 SCTP_IFN_IS_IFT_LOOP(sctp_ifnp)) {
2096 /*
2097 * Skip loopback devices if
2098 * loopback_scope not set
2099 */
2100 continue;
2101 }
2102 LIST_FOREACH(sctp_ifap, &sctp_ifnp->ifalist, next_ifa) {
2103 #ifdef INET
2104 if ((sctp_ifap->address.sa.sa_family == AF_INET) &&
2105 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2106 &sctp_ifap->address.sin.sin_addr) != 0)) {
2107 continue;
2108 }
2109 #endif
2110 #ifdef INET6
2111 if ((sctp_ifap->address.sa.sa_family == AF_INET6) &&
2112 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2113 &sctp_ifap->address.sin6.sin6_addr) != 0)) {
2114 continue;
2115 }
2116 #endif
2117 if (sctp_is_addr_restricted(stcb, sctp_ifap)) {
2118 continue;
2119 }
2120 if (sctp_is_address_in_scope(sctp_ifap,
2121 scope, 0) == 0) {
2122 continue;
2123 }
2124 if ((chunk_len != NULL) &&
2125 (padding_len != NULL) &&
2126 (*padding_len > 0)) {
2127 memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2128 SCTP_BUF_LEN(m_at) += *padding_len;
2129 *chunk_len += *padding_len;
2130 *padding_len = 0;
2131 }
2132 m_at = sctp_add_addr_to_mbuf(m_at, sctp_ifap, chunk_len);
2133 if (limit_out) {
2134 cnt++;
2135 total_count++;
2136 if (cnt >= 2) {
2137 /*
2138 * two from each
2139 * address
2140 */
2141 break;
2142 }
2143 if (total_count > SCTP_ADDRESS_LIMIT) {
2144 /* No more addresses */
2145 break;
2146 }
2147 }
2148 }
2149 }
2150 }
2151 } else {
2152 struct sctp_laddr *laddr;
2153
2154 cnt = cnt_inits_to;
2155 /* First, how many ? */
2156 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2157 if (laddr->ifa == NULL) {
2158 continue;
2159 }
2160 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED)
2161 /*
2162 * Address being deleted by the system, dont
2163 * list.
2164 */
2165 continue;
2166 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2167 /*
2168 * Address being deleted on this ep don't
2169 * list.
2170 */
2171 continue;
2172 }
2173 if (sctp_is_address_in_scope(laddr->ifa,
2174 scope, 1) == 0) {
2175 continue;
2176 }
2177 cnt++;
2178 }
2179 /*
2180 * To get through a NAT we only list addresses if we have
2181 * more than one. That way if you just bind a single address
2182 * we let the source of the init dictate our address.
2183 */
2184 if (cnt > 1) {
2185 cnt = cnt_inits_to;
2186 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2187 if (laddr->ifa == NULL) {
2188 continue;
2189 }
2190 if (laddr->ifa->localifa_flags & SCTP_BEING_DELETED) {
2191 continue;
2192 }
2193 if (sctp_is_address_in_scope(laddr->ifa,
2194 scope, 0) == 0) {
2195 continue;
2196 }
2197 if ((chunk_len != NULL) &&
2198 (padding_len != NULL) &&
2199 (*padding_len > 0)) {
2200 memset(mtod(m_at, caddr_t)+*chunk_len, 0, *padding_len);
2201 SCTP_BUF_LEN(m_at) += *padding_len;
2202 *chunk_len += *padding_len;
2203 *padding_len = 0;
2204 }
2205 m_at = sctp_add_addr_to_mbuf(m_at, laddr->ifa, chunk_len);
2206 cnt++;
2207 if (cnt >= SCTP_ADDRESS_LIMIT) {
2208 break;
2209 }
2210 }
2211 }
2212 }
2213 SCTP_IPI_ADDR_RUNLOCK();
2214 return (m_at);
2215 }
2216
2217 static struct sctp_ifa *
sctp_is_ifa_addr_preferred(struct sctp_ifa * ifa,uint8_t dest_is_loop,uint8_t dest_is_priv,sa_family_t fam)2218 sctp_is_ifa_addr_preferred(struct sctp_ifa *ifa,
2219 uint8_t dest_is_loop,
2220 uint8_t dest_is_priv,
2221 sa_family_t fam)
2222 {
2223 uint8_t dest_is_global = 0;
2224
2225 /* dest_is_priv is true if destination is a private address */
2226 /* dest_is_loop is true if destination is a loopback addresses */
2227
2228 /**
2229 * Here we determine if its a preferred address. A preferred address
2230 * means it is the same scope or higher scope then the destination.
2231 * L = loopback, P = private, G = global
2232 * -----------------------------------------
2233 * src | dest | result
2234 * ----------------------------------------
2235 * L | L | yes
2236 * -----------------------------------------
2237 * P | L | yes-v4 no-v6
2238 * -----------------------------------------
2239 * G | L | yes-v4 no-v6
2240 * -----------------------------------------
2241 * L | P | no
2242 * -----------------------------------------
2243 * P | P | yes
2244 * -----------------------------------------
2245 * G | P | no
2246 * -----------------------------------------
2247 * L | G | no
2248 * -----------------------------------------
2249 * P | G | no
2250 * -----------------------------------------
2251 * G | G | yes
2252 * -----------------------------------------
2253 */
2254
2255 if (ifa->address.sa.sa_family != fam) {
2256 /* forget mis-matched family */
2257 return (NULL);
2258 }
2259 if ((dest_is_priv == 0) && (dest_is_loop == 0)) {
2260 dest_is_global = 1;
2261 }
2262 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Is destination preferred:");
2263 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ifa->address.sa);
2264 /* Ok the address may be ok */
2265 #ifdef INET6
2266 if (fam == AF_INET6) {
2267 /* ok to use deprecated addresses? no lets not! */
2268 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2269 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:1\n");
2270 return (NULL);
2271 }
2272 if (ifa->src_is_priv && !ifa->src_is_loop) {
2273 if (dest_is_loop) {
2274 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:2\n");
2275 return (NULL);
2276 }
2277 }
2278 if (ifa->src_is_glob) {
2279 if (dest_is_loop) {
2280 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:3\n");
2281 return (NULL);
2282 }
2283 }
2284 }
2285 #endif
2286 /*
2287 * Now that we know what is what, implement or table this could in
2288 * theory be done slicker (it used to be), but this is
2289 * straightforward and easier to validate :-)
2290 */
2291 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src_loop:%d src_priv:%d src_glob:%d\n",
2292 ifa->src_is_loop, ifa->src_is_priv, ifa->src_is_glob);
2293 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dest_loop:%d dest_priv:%d dest_glob:%d\n",
2294 dest_is_loop, dest_is_priv, dest_is_global);
2295
2296 if ((ifa->src_is_loop) && (dest_is_priv)) {
2297 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:4\n");
2298 return (NULL);
2299 }
2300 if ((ifa->src_is_glob) && (dest_is_priv)) {
2301 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:5\n");
2302 return (NULL);
2303 }
2304 if ((ifa->src_is_loop) && (dest_is_global)) {
2305 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:6\n");
2306 return (NULL);
2307 }
2308 if ((ifa->src_is_priv) && (dest_is_global)) {
2309 SCTPDBG(SCTP_DEBUG_OUTPUT3, "NO:7\n");
2310 return (NULL);
2311 }
2312 SCTPDBG(SCTP_DEBUG_OUTPUT3, "YES\n");
2313 /* its a preferred address */
2314 return (ifa);
2315 }
2316
2317 static struct sctp_ifa *
sctp_is_ifa_addr_acceptable(struct sctp_ifa * ifa,uint8_t dest_is_loop,uint8_t dest_is_priv,sa_family_t fam)2318 sctp_is_ifa_addr_acceptable(struct sctp_ifa *ifa,
2319 uint8_t dest_is_loop,
2320 uint8_t dest_is_priv,
2321 sa_family_t fam)
2322 {
2323 uint8_t dest_is_global = 0;
2324
2325 /**
2326 * Here we determine if its a acceptable address. A acceptable
2327 * address means it is the same scope or higher scope but we can
2328 * allow for NAT which means its ok to have a global dest and a
2329 * private src.
2330 *
2331 * L = loopback, P = private, G = global
2332 * -----------------------------------------
2333 * src | dest | result
2334 * -----------------------------------------
2335 * L | L | yes
2336 * -----------------------------------------
2337 * P | L | yes-v4 no-v6
2338 * -----------------------------------------
2339 * G | L | yes
2340 * -----------------------------------------
2341 * L | P | no
2342 * -----------------------------------------
2343 * P | P | yes
2344 * -----------------------------------------
2345 * G | P | yes - May not work
2346 * -----------------------------------------
2347 * L | G | no
2348 * -----------------------------------------
2349 * P | G | yes - May not work
2350 * -----------------------------------------
2351 * G | G | yes
2352 * -----------------------------------------
2353 */
2354
2355 if (ifa->address.sa.sa_family != fam) {
2356 /* forget non matching family */
2357 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa_fam:%d fam:%d\n",
2358 ifa->address.sa.sa_family, fam);
2359 return (NULL);
2360 }
2361 /* Ok the address may be ok */
2362 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, &ifa->address.sa);
2363 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst_is_loop:%d dest_is_priv:%d\n",
2364 dest_is_loop, dest_is_priv);
2365 if ((dest_is_loop == 0) && (dest_is_priv == 0)) {
2366 dest_is_global = 1;
2367 }
2368 #ifdef INET6
2369 if (fam == AF_INET6) {
2370 /* ok to use deprecated addresses? */
2371 if (ifa->localifa_flags & SCTP_ADDR_IFA_UNUSEABLE) {
2372 return (NULL);
2373 }
2374 if (ifa->src_is_priv) {
2375 /* Special case, linklocal to loop */
2376 if (dest_is_loop)
2377 return (NULL);
2378 }
2379 }
2380 #endif
2381 /*
2382 * Now that we know what is what, implement our table. This could in
2383 * theory be done slicker (it used to be), but this is
2384 * straightforward and easier to validate :-)
2385 */
2386 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_priv:%d\n",
2387 ifa->src_is_loop,
2388 dest_is_priv);
2389 if ((ifa->src_is_loop == 1) && (dest_is_priv)) {
2390 return (NULL);
2391 }
2392 SCTPDBG(SCTP_DEBUG_OUTPUT3, "ifa->src_is_loop:%d dest_is_glob:%d\n",
2393 ifa->src_is_loop,
2394 dest_is_global);
2395 if ((ifa->src_is_loop == 1) && (dest_is_global)) {
2396 return (NULL);
2397 }
2398 SCTPDBG(SCTP_DEBUG_OUTPUT3, "address is acceptable\n");
2399 /* its an acceptable address */
2400 return (ifa);
2401 }
2402
2403 int
sctp_is_addr_restricted(struct sctp_tcb * stcb,struct sctp_ifa * ifa)2404 sctp_is_addr_restricted(struct sctp_tcb *stcb, struct sctp_ifa *ifa)
2405 {
2406 struct sctp_laddr *laddr;
2407
2408 if (stcb == NULL) {
2409 /* There are no restrictions, no TCB :-) */
2410 return (0);
2411 }
2412 LIST_FOREACH(laddr, &stcb->asoc.sctp_restricted_addrs, sctp_nxt_addr) {
2413 if (laddr->ifa == NULL) {
2414 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2415 __func__);
2416 continue;
2417 }
2418 if (laddr->ifa == ifa) {
2419 /* Yes it is on the list */
2420 return (1);
2421 }
2422 }
2423 return (0);
2424 }
2425
2426 int
sctp_is_addr_in_ep(struct sctp_inpcb * inp,struct sctp_ifa * ifa)2427 sctp_is_addr_in_ep(struct sctp_inpcb *inp, struct sctp_ifa *ifa)
2428 {
2429 struct sctp_laddr *laddr;
2430
2431 if (ifa == NULL)
2432 return (0);
2433 LIST_FOREACH(laddr, &inp->sctp_addr_list, sctp_nxt_addr) {
2434 if (laddr->ifa == NULL) {
2435 SCTPDBG(SCTP_DEBUG_OUTPUT1, "%s: NULL ifa\n",
2436 __func__);
2437 continue;
2438 }
2439 if ((laddr->ifa == ifa) && laddr->action == 0)
2440 /* same pointer */
2441 return (1);
2442 }
2443 return (0);
2444 }
2445
2446 static struct sctp_ifa *
sctp_choose_boundspecific_inp(struct sctp_inpcb * inp,sctp_route_t * ro,uint32_t vrf_id,int non_asoc_addr_ok,uint8_t dest_is_priv,uint8_t dest_is_loop,sa_family_t fam)2447 sctp_choose_boundspecific_inp(struct sctp_inpcb *inp,
2448 sctp_route_t *ro,
2449 uint32_t vrf_id,
2450 int non_asoc_addr_ok,
2451 uint8_t dest_is_priv,
2452 uint8_t dest_is_loop,
2453 sa_family_t fam)
2454 {
2455 struct sctp_laddr *laddr, *starting_point;
2456 void *ifn;
2457 int resettotop = 0;
2458 struct sctp_ifn *sctp_ifn;
2459 struct sctp_ifa *sctp_ifa, *sifa;
2460 struct sctp_vrf *vrf;
2461 uint32_t ifn_index;
2462
2463 vrf = sctp_find_vrf(vrf_id);
2464 if (vrf == NULL)
2465 return (NULL);
2466
2467 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2468 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2469 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2470 /*
2471 * first question, is the ifn we will emit on in our list, if so, we
2472 * want such an address. Note that we first looked for a preferred
2473 * address.
2474 */
2475 if (sctp_ifn) {
2476 /* is a preferred one on the interface we route out? */
2477 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2478 #ifdef INET
2479 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2480 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2481 &sctp_ifa->address.sin.sin_addr) != 0)) {
2482 continue;
2483 }
2484 #endif
2485 #ifdef INET6
2486 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2487 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2488 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2489 continue;
2490 }
2491 #endif
2492 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2493 (non_asoc_addr_ok == 0))
2494 continue;
2495 sifa = sctp_is_ifa_addr_preferred(sctp_ifa,
2496 dest_is_loop,
2497 dest_is_priv, fam);
2498 if (sifa == NULL)
2499 continue;
2500 if (sctp_is_addr_in_ep(inp, sifa)) {
2501 atomic_add_int(&sifa->refcount, 1);
2502 return (sifa);
2503 }
2504 }
2505 }
2506 /*
2507 * ok, now we now need to find one on the list of the addresses. We
2508 * can't get one on the emitting interface so let's find first a
2509 * preferred one. If not that an acceptable one otherwise... we
2510 * return NULL.
2511 */
2512 starting_point = inp->next_addr_touse;
2513 once_again:
2514 if (inp->next_addr_touse == NULL) {
2515 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2516 resettotop = 1;
2517 }
2518 for (laddr = inp->next_addr_touse; laddr;
2519 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2520 if (laddr->ifa == NULL) {
2521 /* address has been removed */
2522 continue;
2523 }
2524 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2525 /* address is being deleted */
2526 continue;
2527 }
2528 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop,
2529 dest_is_priv, fam);
2530 if (sifa == NULL)
2531 continue;
2532 atomic_add_int(&sifa->refcount, 1);
2533 return (sifa);
2534 }
2535 if (resettotop == 0) {
2536 inp->next_addr_touse = NULL;
2537 goto once_again;
2538 }
2539
2540 inp->next_addr_touse = starting_point;
2541 resettotop = 0;
2542 once_again_too:
2543 if (inp->next_addr_touse == NULL) {
2544 inp->next_addr_touse = LIST_FIRST(&inp->sctp_addr_list);
2545 resettotop = 1;
2546 }
2547
2548 /* ok, what about an acceptable address in the inp */
2549 for (laddr = inp->next_addr_touse; laddr;
2550 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2551 if (laddr->ifa == NULL) {
2552 /* address has been removed */
2553 continue;
2554 }
2555 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2556 /* address is being deleted */
2557 continue;
2558 }
2559 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2560 dest_is_priv, fam);
2561 if (sifa == NULL)
2562 continue;
2563 atomic_add_int(&sifa->refcount, 1);
2564 return (sifa);
2565 }
2566 if (resettotop == 0) {
2567 inp->next_addr_touse = NULL;
2568 goto once_again_too;
2569 }
2570
2571 /*
2572 * no address bound can be a source for the destination we are in
2573 * trouble
2574 */
2575 return (NULL);
2576 }
2577
2578 static struct sctp_ifa *
sctp_choose_boundspecific_stcb(struct sctp_inpcb * inp,struct sctp_tcb * stcb,sctp_route_t * ro,uint32_t vrf_id,uint8_t dest_is_priv,uint8_t dest_is_loop,int non_asoc_addr_ok,sa_family_t fam)2579 sctp_choose_boundspecific_stcb(struct sctp_inpcb *inp,
2580 struct sctp_tcb *stcb,
2581 sctp_route_t *ro,
2582 uint32_t vrf_id,
2583 uint8_t dest_is_priv,
2584 uint8_t dest_is_loop,
2585 int non_asoc_addr_ok,
2586 sa_family_t fam)
2587 {
2588 struct sctp_laddr *laddr, *starting_point;
2589 void *ifn;
2590 struct sctp_ifn *sctp_ifn;
2591 struct sctp_ifa *sctp_ifa, *sifa;
2592 uint8_t start_at_beginning = 0;
2593 struct sctp_vrf *vrf;
2594 uint32_t ifn_index;
2595
2596 /*
2597 * first question, is the ifn we will emit on in our list, if so, we
2598 * want that one.
2599 */
2600 vrf = sctp_find_vrf(vrf_id);
2601 if (vrf == NULL)
2602 return (NULL);
2603
2604 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2605 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2606 sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2607
2608 /*
2609 * first question, is the ifn we will emit on in our list? If so,
2610 * we want that one. First we look for a preferred. Second, we go
2611 * for an acceptable.
2612 */
2613 if (sctp_ifn) {
2614 /* first try for a preferred address on the ep */
2615 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2616 #ifdef INET
2617 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2618 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2619 &sctp_ifa->address.sin.sin_addr) != 0)) {
2620 continue;
2621 }
2622 #endif
2623 #ifdef INET6
2624 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2625 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2626 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2627 continue;
2628 }
2629 #endif
2630 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2631 continue;
2632 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2633 sifa = sctp_is_ifa_addr_preferred(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2634 if (sifa == NULL)
2635 continue;
2636 if (((non_asoc_addr_ok == 0) &&
2637 (sctp_is_addr_restricted(stcb, sifa))) ||
2638 (non_asoc_addr_ok &&
2639 (sctp_is_addr_restricted(stcb, sifa)) &&
2640 (!sctp_is_addr_pending(stcb, sifa)))) {
2641 /* on the no-no list */
2642 continue;
2643 }
2644 atomic_add_int(&sifa->refcount, 1);
2645 return (sifa);
2646 }
2647 }
2648 /* next try for an acceptable address on the ep */
2649 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
2650 #ifdef INET
2651 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
2652 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2653 &sctp_ifa->address.sin.sin_addr) != 0)) {
2654 continue;
2655 }
2656 #endif
2657 #ifdef INET6
2658 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
2659 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2660 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
2661 continue;
2662 }
2663 #endif
2664 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) && (non_asoc_addr_ok == 0))
2665 continue;
2666 if (sctp_is_addr_in_ep(inp, sctp_ifa)) {
2667 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop, dest_is_priv, fam);
2668 if (sifa == NULL)
2669 continue;
2670 if (((non_asoc_addr_ok == 0) &&
2671 (sctp_is_addr_restricted(stcb, sifa))) ||
2672 (non_asoc_addr_ok &&
2673 (sctp_is_addr_restricted(stcb, sifa)) &&
2674 (!sctp_is_addr_pending(stcb, sifa)))) {
2675 /* on the no-no list */
2676 continue;
2677 }
2678 atomic_add_int(&sifa->refcount, 1);
2679 return (sifa);
2680 }
2681 }
2682 }
2683 /*
2684 * if we can't find one like that then we must look at all addresses
2685 * bound to pick one at first preferable then secondly acceptable.
2686 */
2687 starting_point = stcb->asoc.last_used_address;
2688 sctp_from_the_top:
2689 if (stcb->asoc.last_used_address == NULL) {
2690 start_at_beginning = 1;
2691 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2692 }
2693 /* search beginning with the last used address */
2694 for (laddr = stcb->asoc.last_used_address; laddr;
2695 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2696 if (laddr->ifa == NULL) {
2697 /* address has been removed */
2698 continue;
2699 }
2700 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2701 /* address is being deleted */
2702 continue;
2703 }
2704 sifa = sctp_is_ifa_addr_preferred(laddr->ifa, dest_is_loop, dest_is_priv, fam);
2705 if (sifa == NULL)
2706 continue;
2707 if (((non_asoc_addr_ok == 0) &&
2708 (sctp_is_addr_restricted(stcb, sifa))) ||
2709 (non_asoc_addr_ok &&
2710 (sctp_is_addr_restricted(stcb, sifa)) &&
2711 (!sctp_is_addr_pending(stcb, sifa)))) {
2712 /* on the no-no list */
2713 continue;
2714 }
2715 stcb->asoc.last_used_address = laddr;
2716 atomic_add_int(&sifa->refcount, 1);
2717 return (sifa);
2718 }
2719 if (start_at_beginning == 0) {
2720 stcb->asoc.last_used_address = NULL;
2721 goto sctp_from_the_top;
2722 }
2723 /* now try for any higher scope than the destination */
2724 stcb->asoc.last_used_address = starting_point;
2725 start_at_beginning = 0;
2726 sctp_from_the_top2:
2727 if (stcb->asoc.last_used_address == NULL) {
2728 start_at_beginning = 1;
2729 stcb->asoc.last_used_address = LIST_FIRST(&inp->sctp_addr_list);
2730 }
2731 /* search beginning with the last used address */
2732 for (laddr = stcb->asoc.last_used_address; laddr;
2733 laddr = LIST_NEXT(laddr, sctp_nxt_addr)) {
2734 if (laddr->ifa == NULL) {
2735 /* address has been removed */
2736 continue;
2737 }
2738 if (laddr->action == SCTP_DEL_IP_ADDRESS) {
2739 /* address is being deleted */
2740 continue;
2741 }
2742 sifa = sctp_is_ifa_addr_acceptable(laddr->ifa, dest_is_loop,
2743 dest_is_priv, fam);
2744 if (sifa == NULL)
2745 continue;
2746 if (((non_asoc_addr_ok == 0) &&
2747 (sctp_is_addr_restricted(stcb, sifa))) ||
2748 (non_asoc_addr_ok &&
2749 (sctp_is_addr_restricted(stcb, sifa)) &&
2750 (!sctp_is_addr_pending(stcb, sifa)))) {
2751 /* on the no-no list */
2752 continue;
2753 }
2754 stcb->asoc.last_used_address = laddr;
2755 atomic_add_int(&sifa->refcount, 1);
2756 return (sifa);
2757 }
2758 if (start_at_beginning == 0) {
2759 stcb->asoc.last_used_address = NULL;
2760 goto sctp_from_the_top2;
2761 }
2762 return (NULL);
2763 }
2764
2765 static struct sctp_ifa *
sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn * ifn,struct sctp_inpcb * inp,struct sctp_tcb * stcb,int non_asoc_addr_ok,uint8_t dest_is_loop,uint8_t dest_is_priv,int addr_wanted,sa_family_t fam,sctp_route_t * ro)2766 sctp_select_nth_preferred_addr_from_ifn_boundall(struct sctp_ifn *ifn,
2767 struct sctp_inpcb *inp,
2768 struct sctp_tcb *stcb,
2769 int non_asoc_addr_ok,
2770 uint8_t dest_is_loop,
2771 uint8_t dest_is_priv,
2772 int addr_wanted,
2773 sa_family_t fam,
2774 sctp_route_t *ro)
2775 {
2776 struct sctp_ifa *ifa, *sifa;
2777 int num_eligible_addr = 0;
2778 #ifdef INET6
2779 struct sockaddr_in6 sin6, lsa6;
2780
2781 if (fam == AF_INET6) {
2782 memcpy(&sin6, &ro->ro_dst, sizeof(struct sockaddr_in6));
2783 (void)sa6_recoverscope(&sin6);
2784 }
2785 #endif /* INET6 */
2786 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2787 #ifdef INET
2788 if ((ifa->address.sa.sa_family == AF_INET) &&
2789 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2790 &ifa->address.sin.sin_addr) != 0)) {
2791 continue;
2792 }
2793 #endif
2794 #ifdef INET6
2795 if ((ifa->address.sa.sa_family == AF_INET6) &&
2796 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2797 &ifa->address.sin6.sin6_addr) != 0)) {
2798 continue;
2799 }
2800 #endif
2801 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2802 (non_asoc_addr_ok == 0))
2803 continue;
2804 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2805 dest_is_priv, fam);
2806 if (sifa == NULL)
2807 continue;
2808 #ifdef INET6
2809 if (fam == AF_INET6 &&
2810 dest_is_loop &&
2811 sifa->src_is_loop && sifa->src_is_priv) {
2812 /*
2813 * don't allow fe80::1 to be a src on loop ::1, we
2814 * don't list it to the peer so we will get an
2815 * abort.
2816 */
2817 continue;
2818 }
2819 if (fam == AF_INET6 &&
2820 IN6_IS_ADDR_LINKLOCAL(&sifa->address.sin6.sin6_addr) &&
2821 IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr)) {
2822 /*
2823 * link-local <-> link-local must belong to the same
2824 * scope.
2825 */
2826 memcpy(&lsa6, &sifa->address.sin6, sizeof(struct sockaddr_in6));
2827 (void)sa6_recoverscope(&lsa6);
2828 if (sin6.sin6_scope_id != lsa6.sin6_scope_id) {
2829 continue;
2830 }
2831 }
2832 #endif /* INET6 */
2833
2834 /*
2835 * Check if the IPv6 address matches to next-hop. In the
2836 * mobile case, old IPv6 address may be not deleted from the
2837 * interface. Then, the interface has previous and new
2838 * addresses. We should use one corresponding to the
2839 * next-hop. (by micchie)
2840 */
2841 #ifdef INET6
2842 if (stcb && fam == AF_INET6 &&
2843 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2844 if (sctp_v6src_match_nexthop(&sifa->address.sin6, ro) == 0) {
2845 continue;
2846 }
2847 }
2848 #endif
2849 #ifdef INET
2850 /* Avoid topologically incorrect IPv4 address */
2851 if (stcb && fam == AF_INET &&
2852 sctp_is_mobility_feature_on(stcb->sctp_ep, SCTP_MOBILITY_BASE)) {
2853 if (sctp_v4src_match_nexthop(sifa, ro) == 0) {
2854 continue;
2855 }
2856 }
2857 #endif
2858 if (stcb) {
2859 if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2860 continue;
2861 }
2862 if (((non_asoc_addr_ok == 0) &&
2863 (sctp_is_addr_restricted(stcb, sifa))) ||
2864 (non_asoc_addr_ok &&
2865 (sctp_is_addr_restricted(stcb, sifa)) &&
2866 (!sctp_is_addr_pending(stcb, sifa)))) {
2867 /*
2868 * It is restricted for some reason..
2869 * probably not yet added.
2870 */
2871 continue;
2872 }
2873 }
2874 if (num_eligible_addr >= addr_wanted) {
2875 return (sifa);
2876 }
2877 num_eligible_addr++;
2878 }
2879 return (NULL);
2880 }
2881
2882 static int
sctp_count_num_preferred_boundall(struct sctp_ifn * ifn,struct sctp_inpcb * inp,struct sctp_tcb * stcb,int non_asoc_addr_ok,uint8_t dest_is_loop,uint8_t dest_is_priv,sa_family_t fam)2883 sctp_count_num_preferred_boundall(struct sctp_ifn *ifn,
2884 struct sctp_inpcb *inp,
2885 struct sctp_tcb *stcb,
2886 int non_asoc_addr_ok,
2887 uint8_t dest_is_loop,
2888 uint8_t dest_is_priv,
2889 sa_family_t fam)
2890 {
2891 struct sctp_ifa *ifa, *sifa;
2892 int num_eligible_addr = 0;
2893
2894 LIST_FOREACH(ifa, &ifn->ifalist, next_ifa) {
2895 #ifdef INET
2896 if ((ifa->address.sa.sa_family == AF_INET) &&
2897 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
2898 &ifa->address.sin.sin_addr) != 0)) {
2899 continue;
2900 }
2901 #endif
2902 #ifdef INET6
2903 if ((ifa->address.sa.sa_family == AF_INET6) &&
2904 (stcb != NULL) &&
2905 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
2906 &ifa->address.sin6.sin6_addr) != 0)) {
2907 continue;
2908 }
2909 #endif
2910 if ((ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
2911 (non_asoc_addr_ok == 0)) {
2912 continue;
2913 }
2914 sifa = sctp_is_ifa_addr_preferred(ifa, dest_is_loop,
2915 dest_is_priv, fam);
2916 if (sifa == NULL) {
2917 continue;
2918 }
2919 if (stcb) {
2920 if (sctp_is_address_in_scope(ifa, &stcb->asoc.scope, 0) == 0) {
2921 continue;
2922 }
2923 if (((non_asoc_addr_ok == 0) &&
2924 (sctp_is_addr_restricted(stcb, sifa))) ||
2925 (non_asoc_addr_ok &&
2926 (sctp_is_addr_restricted(stcb, sifa)) &&
2927 (!sctp_is_addr_pending(stcb, sifa)))) {
2928 /*
2929 * It is restricted for some reason..
2930 * probably not yet added.
2931 */
2932 continue;
2933 }
2934 }
2935 num_eligible_addr++;
2936 }
2937 return (num_eligible_addr);
2938 }
2939
2940 static struct sctp_ifa *
sctp_choose_boundall(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net,sctp_route_t * ro,uint32_t vrf_id,uint8_t dest_is_priv,uint8_t dest_is_loop,int non_asoc_addr_ok,sa_family_t fam)2941 sctp_choose_boundall(struct sctp_inpcb *inp,
2942 struct sctp_tcb *stcb,
2943 struct sctp_nets *net,
2944 sctp_route_t *ro,
2945 uint32_t vrf_id,
2946 uint8_t dest_is_priv,
2947 uint8_t dest_is_loop,
2948 int non_asoc_addr_ok,
2949 sa_family_t fam)
2950 {
2951 int cur_addr_num = 0, num_preferred = 0;
2952 void *ifn;
2953 struct sctp_ifn *sctp_ifn, *looked_at = NULL, *emit_ifn;
2954 struct sctp_ifa *sctp_ifa, *sifa;
2955 uint32_t ifn_index;
2956 struct sctp_vrf *vrf;
2957 #ifdef INET
2958 int retried = 0;
2959 #endif
2960
2961 /*-
2962 * For boundall we can use any address in the association.
2963 * If non_asoc_addr_ok is set we can use any address (at least in
2964 * theory). So we look for preferred addresses first. If we find one,
2965 * we use it. Otherwise we next try to get an address on the
2966 * interface, which we should be able to do (unless non_asoc_addr_ok
2967 * is false and we are routed out that way). In these cases where we
2968 * can't use the address of the interface we go through all the
2969 * ifn's looking for an address we can use and fill that in. Punting
2970 * means we send back address 0, which will probably cause problems
2971 * actually since then IP will fill in the address of the route ifn,
2972 * which means we probably already rejected it.. i.e. here comes an
2973 * abort :-<.
2974 */
2975 vrf = sctp_find_vrf(vrf_id);
2976 if (vrf == NULL)
2977 return (NULL);
2978
2979 ifn = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
2980 ifn_index = SCTP_GET_IF_INDEX_FROM_ROUTE(ro);
2981 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn from route:%p ifn_index:%d\n", ifn, ifn_index);
2982 emit_ifn = looked_at = sctp_ifn = sctp_find_ifn(ifn, ifn_index);
2983 if (sctp_ifn == NULL) {
2984 /* ?? We don't have this guy ?? */
2985 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No ifn emit interface?\n");
2986 goto bound_all_plan_b;
2987 }
2988 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifn_index:%d name:%s is emit interface\n",
2989 ifn_index, sctp_ifn->ifn_name);
2990
2991 if (net) {
2992 cur_addr_num = net->indx_of_eligible_next_to_use;
2993 }
2994 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn,
2995 inp, stcb,
2996 non_asoc_addr_ok,
2997 dest_is_loop,
2998 dest_is_priv, fam);
2999 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Found %d preferred source addresses for intf:%s\n",
3000 num_preferred, sctp_ifn->ifn_name);
3001 if (num_preferred == 0) {
3002 /*
3003 * no eligible addresses, we must use some other interface
3004 * address if we can find one.
3005 */
3006 goto bound_all_plan_b;
3007 }
3008 /*
3009 * Ok we have num_eligible_addr set with how many we can use, this
3010 * may vary from call to call due to addresses being deprecated
3011 * etc..
3012 */
3013 if (cur_addr_num >= num_preferred) {
3014 cur_addr_num = 0;
3015 }
3016 /*
3017 * select the nth address from the list (where cur_addr_num is the
3018 * nth) and 0 is the first one, 1 is the second one etc...
3019 */
3020 SCTPDBG(SCTP_DEBUG_OUTPUT2, "cur_addr_num:%d\n", cur_addr_num);
3021
3022 sctp_ifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3023 dest_is_priv, cur_addr_num, fam, ro);
3024
3025 /* if sctp_ifa is NULL something changed??, fall to plan b. */
3026 if (sctp_ifa) {
3027 atomic_add_int(&sctp_ifa->refcount, 1);
3028 if (net) {
3029 /* save off where the next one we will want */
3030 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3031 }
3032 return (sctp_ifa);
3033 }
3034 /*
3035 * plan_b: Look at all interfaces and find a preferred address. If
3036 * no preferred fall through to plan_c.
3037 */
3038 bound_all_plan_b:
3039 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan B\n");
3040 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3041 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Examine interface %s\n",
3042 sctp_ifn->ifn_name);
3043 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3044 /* wrong base scope */
3045 SCTPDBG(SCTP_DEBUG_OUTPUT2, "skip\n");
3046 continue;
3047 }
3048 if ((sctp_ifn == looked_at) && looked_at) {
3049 /* already looked at this guy */
3050 SCTPDBG(SCTP_DEBUG_OUTPUT2, "already seen\n");
3051 continue;
3052 }
3053 num_preferred = sctp_count_num_preferred_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok,
3054 dest_is_loop, dest_is_priv, fam);
3055 SCTPDBG(SCTP_DEBUG_OUTPUT2,
3056 "Found ifn:%p %d preferred source addresses\n",
3057 ifn, num_preferred);
3058 if (num_preferred == 0) {
3059 /* None on this interface. */
3060 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No preferred -- skipping to next\n");
3061 continue;
3062 }
3063 SCTPDBG(SCTP_DEBUG_OUTPUT2,
3064 "num preferred:%d on interface:%p cur_addr_num:%d\n",
3065 num_preferred, (void *)sctp_ifn, cur_addr_num);
3066
3067 /*
3068 * Ok we have num_eligible_addr set with how many we can
3069 * use, this may vary from call to call due to addresses
3070 * being deprecated etc..
3071 */
3072 if (cur_addr_num >= num_preferred) {
3073 cur_addr_num = 0;
3074 }
3075 sifa = sctp_select_nth_preferred_addr_from_ifn_boundall(sctp_ifn, inp, stcb, non_asoc_addr_ok, dest_is_loop,
3076 dest_is_priv, cur_addr_num, fam, ro);
3077 if (sifa == NULL)
3078 continue;
3079 if (net) {
3080 net->indx_of_eligible_next_to_use = cur_addr_num + 1;
3081 SCTPDBG(SCTP_DEBUG_OUTPUT2, "we selected %d\n",
3082 cur_addr_num);
3083 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Source:");
3084 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
3085 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Dest:");
3086 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &net->ro._l_addr.sa);
3087 }
3088 atomic_add_int(&sifa->refcount, 1);
3089 return (sifa);
3090 }
3091 #ifdef INET
3092 again_with_private_addresses_allowed:
3093 #endif
3094 /* plan_c: do we have an acceptable address on the emit interface */
3095 sifa = NULL;
3096 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan C: find acceptable on interface\n");
3097 if (emit_ifn == NULL) {
3098 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jump to Plan D - no emit_ifn\n");
3099 goto plan_d;
3100 }
3101 LIST_FOREACH(sctp_ifa, &emit_ifn->ifalist, next_ifa) {
3102 SCTPDBG(SCTP_DEBUG_OUTPUT2, "ifa:%p\n", (void *)sctp_ifa);
3103 #ifdef INET
3104 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3105 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3106 &sctp_ifa->address.sin.sin_addr) != 0)) {
3107 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3108 continue;
3109 }
3110 #endif
3111 #ifdef INET6
3112 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3113 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3114 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3115 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Jailed\n");
3116 continue;
3117 }
3118 #endif
3119 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3120 (non_asoc_addr_ok == 0)) {
3121 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Defer\n");
3122 continue;
3123 }
3124 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa, dest_is_loop,
3125 dest_is_priv, fam);
3126 if (sifa == NULL) {
3127 SCTPDBG(SCTP_DEBUG_OUTPUT2, "IFA not acceptable\n");
3128 continue;
3129 }
3130 if (stcb) {
3131 if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3132 SCTPDBG(SCTP_DEBUG_OUTPUT2, "NOT in scope\n");
3133 sifa = NULL;
3134 continue;
3135 }
3136 if (((non_asoc_addr_ok == 0) &&
3137 (sctp_is_addr_restricted(stcb, sifa))) ||
3138 (non_asoc_addr_ok &&
3139 (sctp_is_addr_restricted(stcb, sifa)) &&
3140 (!sctp_is_addr_pending(stcb, sifa)))) {
3141 /*
3142 * It is restricted for some reason..
3143 * probably not yet added.
3144 */
3145 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Its restricted\n");
3146 sifa = NULL;
3147 continue;
3148 }
3149 }
3150 atomic_add_int(&sifa->refcount, 1);
3151 goto out;
3152 }
3153 plan_d:
3154 /*
3155 * plan_d: We are in trouble. No preferred address on the emit
3156 * interface. And not even a preferred address on all interfaces. Go
3157 * out and see if we can find an acceptable address somewhere
3158 * amongst all interfaces.
3159 */
3160 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Trying Plan D looked_at is %p\n", (void *)looked_at);
3161 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3162 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3163 /* wrong base scope */
3164 continue;
3165 }
3166 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3167 #ifdef INET
3168 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3169 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3170 &sctp_ifa->address.sin.sin_addr) != 0)) {
3171 continue;
3172 }
3173 #endif
3174 #ifdef INET6
3175 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3176 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3177 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3178 continue;
3179 }
3180 #endif
3181 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3182 (non_asoc_addr_ok == 0))
3183 continue;
3184 sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3185 dest_is_loop,
3186 dest_is_priv, fam);
3187 if (sifa == NULL)
3188 continue;
3189 if (stcb) {
3190 if (sctp_is_address_in_scope(sifa, &stcb->asoc.scope, 0) == 0) {
3191 sifa = NULL;
3192 continue;
3193 }
3194 if (((non_asoc_addr_ok == 0) &&
3195 (sctp_is_addr_restricted(stcb, sifa))) ||
3196 (non_asoc_addr_ok &&
3197 (sctp_is_addr_restricted(stcb, sifa)) &&
3198 (!sctp_is_addr_pending(stcb, sifa)))) {
3199 /*
3200 * It is restricted for some
3201 * reason.. probably not yet added.
3202 */
3203 sifa = NULL;
3204 continue;
3205 }
3206 }
3207 goto out;
3208 }
3209 }
3210 #ifdef INET
3211 if (stcb) {
3212 if ((retried == 0) && (stcb->asoc.scope.ipv4_local_scope == 0)) {
3213 stcb->asoc.scope.ipv4_local_scope = 1;
3214 retried = 1;
3215 goto again_with_private_addresses_allowed;
3216 } else if (retried == 1) {
3217 stcb->asoc.scope.ipv4_local_scope = 0;
3218 }
3219 }
3220 #endif
3221 out:
3222 #ifdef INET
3223 if (sifa) {
3224 if (retried == 1) {
3225 LIST_FOREACH(sctp_ifn, &vrf->ifnlist, next_ifn) {
3226 if (dest_is_loop == 0 && SCTP_IFN_IS_IFT_LOOP(sctp_ifn)) {
3227 /* wrong base scope */
3228 continue;
3229 }
3230 LIST_FOREACH(sctp_ifa, &sctp_ifn->ifalist, next_ifa) {
3231 struct sctp_ifa *tmp_sifa;
3232
3233 #ifdef INET
3234 if ((sctp_ifa->address.sa.sa_family == AF_INET) &&
3235 (prison_check_ip4(inp->ip_inp.inp.inp_cred,
3236 &sctp_ifa->address.sin.sin_addr) != 0)) {
3237 continue;
3238 }
3239 #endif
3240 #ifdef INET6
3241 if ((sctp_ifa->address.sa.sa_family == AF_INET6) &&
3242 (prison_check_ip6(inp->ip_inp.inp.inp_cred,
3243 &sctp_ifa->address.sin6.sin6_addr) != 0)) {
3244 continue;
3245 }
3246 #endif
3247 if ((sctp_ifa->localifa_flags & SCTP_ADDR_DEFER_USE) &&
3248 (non_asoc_addr_ok == 0))
3249 continue;
3250 tmp_sifa = sctp_is_ifa_addr_acceptable(sctp_ifa,
3251 dest_is_loop,
3252 dest_is_priv, fam);
3253 if (tmp_sifa == NULL) {
3254 continue;
3255 }
3256 if (tmp_sifa == sifa) {
3257 continue;
3258 }
3259 if (stcb) {
3260 if (sctp_is_address_in_scope(tmp_sifa,
3261 &stcb->asoc.scope, 0) == 0) {
3262 continue;
3263 }
3264 if (((non_asoc_addr_ok == 0) &&
3265 (sctp_is_addr_restricted(stcb, tmp_sifa))) ||
3266 (non_asoc_addr_ok &&
3267 (sctp_is_addr_restricted(stcb, tmp_sifa)) &&
3268 (!sctp_is_addr_pending(stcb, tmp_sifa)))) {
3269 /*
3270 * It is restricted
3271 * for some reason..
3272 * probably not yet
3273 * added.
3274 */
3275 continue;
3276 }
3277 }
3278 if ((tmp_sifa->address.sin.sin_family == AF_INET) &&
3279 (IN4_ISPRIVATE_ADDRESS(&(tmp_sifa->address.sin.sin_addr)))) {
3280 sctp_add_local_addr_restricted(stcb, tmp_sifa);
3281 }
3282 }
3283 }
3284 }
3285 atomic_add_int(&sifa->refcount, 1);
3286 }
3287 #endif
3288 return (sifa);
3289 }
3290
3291 /* tcb may be NULL */
3292 struct sctp_ifa *
sctp_source_address_selection(struct sctp_inpcb * inp,struct sctp_tcb * stcb,sctp_route_t * ro,struct sctp_nets * net,int non_asoc_addr_ok,uint32_t vrf_id)3293 sctp_source_address_selection(struct sctp_inpcb *inp,
3294 struct sctp_tcb *stcb,
3295 sctp_route_t *ro,
3296 struct sctp_nets *net,
3297 int non_asoc_addr_ok, uint32_t vrf_id)
3298 {
3299 struct sctp_ifa *answer;
3300 uint8_t dest_is_priv, dest_is_loop;
3301 sa_family_t fam;
3302 #ifdef INET
3303 struct sockaddr_in *to = (struct sockaddr_in *)&ro->ro_dst;
3304 #endif
3305 #ifdef INET6
3306 struct sockaddr_in6 *to6 = (struct sockaddr_in6 *)&ro->ro_dst;
3307 #endif
3308
3309 /**
3310 * Rules:
3311 * - Find the route if needed, cache if I can.
3312 * - Look at interface address in route, Is it in the bound list. If so we
3313 * have the best source.
3314 * - If not we must rotate amongst the addresses.
3315 *
3316 * Caveats and issues
3317 *
3318 * Do we need to pay attention to scope. We can have a private address
3319 * or a global address we are sourcing or sending to. So if we draw
3320 * it out
3321 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3322 * For V4
3323 * ------------------------------------------
3324 * source * dest * result
3325 * -----------------------------------------
3326 * <a> Private * Global * NAT
3327 * -----------------------------------------
3328 * <b> Private * Private * No problem
3329 * -----------------------------------------
3330 * <c> Global * Private * Huh, How will this work?
3331 * -----------------------------------------
3332 * <d> Global * Global * No Problem
3333 *------------------------------------------
3334 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3335 * For V6
3336 *------------------------------------------
3337 * source * dest * result
3338 * -----------------------------------------
3339 * <a> Linklocal * Global *
3340 * -----------------------------------------
3341 * <b> Linklocal * Linklocal * No problem
3342 * -----------------------------------------
3343 * <c> Global * Linklocal * Huh, How will this work?
3344 * -----------------------------------------
3345 * <d> Global * Global * No Problem
3346 *------------------------------------------
3347 * zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
3348 *
3349 * And then we add to that what happens if there are multiple addresses
3350 * assigned to an interface. Remember the ifa on a ifn is a linked
3351 * list of addresses. So one interface can have more than one IP
3352 * address. What happens if we have both a private and a global
3353 * address? Do we then use context of destination to sort out which
3354 * one is best? And what about NAT's sending P->G may get you a NAT
3355 * translation, or should you select the G thats on the interface in
3356 * preference.
3357 *
3358 * Decisions:
3359 *
3360 * - count the number of addresses on the interface.
3361 * - if it is one, no problem except case <c>.
3362 * For <a> we will assume a NAT out there.
3363 * - if there are more than one, then we need to worry about scope P
3364 * or G. We should prefer G -> G and P -> P if possible.
3365 * Then as a secondary fall back to mixed types G->P being a last
3366 * ditch one.
3367 * - The above all works for bound all, but bound specific we need to
3368 * use the same concept but instead only consider the bound
3369 * addresses. If the bound set is NOT assigned to the interface then
3370 * we must use rotation amongst the bound addresses..
3371 */
3372 if (ro->ro_nh == NULL) {
3373 /*
3374 * Need a route to cache.
3375 */
3376 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
3377 }
3378 if (ro->ro_nh == NULL) {
3379 return (NULL);
3380 }
3381 fam = ro->ro_dst.sa_family;
3382 dest_is_priv = dest_is_loop = 0;
3383 /* Setup our scopes for the destination */
3384 switch (fam) {
3385 #ifdef INET
3386 case AF_INET:
3387 /* Scope based on outbound address */
3388 if (IN4_ISLOOPBACK_ADDRESS(&to->sin_addr)) {
3389 dest_is_loop = 1;
3390 if (net != NULL) {
3391 /* mark it as local */
3392 net->addr_is_local = 1;
3393 }
3394 } else if ((IN4_ISPRIVATE_ADDRESS(&to->sin_addr))) {
3395 dest_is_priv = 1;
3396 }
3397 break;
3398 #endif
3399 #ifdef INET6
3400 case AF_INET6:
3401 /* Scope based on outbound address */
3402 if (IN6_IS_ADDR_LOOPBACK(&to6->sin6_addr) ||
3403 SCTP_ROUTE_IS_REAL_LOOP(ro)) {
3404 /*
3405 * If the address is a loopback address, which
3406 * consists of "::1" OR "fe80::1%lo0", we are
3407 * loopback scope. But we don't use dest_is_priv
3408 * (link local addresses).
3409 */
3410 dest_is_loop = 1;
3411 if (net != NULL) {
3412 /* mark it as local */
3413 net->addr_is_local = 1;
3414 }
3415 } else if (IN6_IS_ADDR_LINKLOCAL(&to6->sin6_addr)) {
3416 dest_is_priv = 1;
3417 }
3418 break;
3419 #endif
3420 }
3421 SCTPDBG(SCTP_DEBUG_OUTPUT2, "Select source addr for:");
3422 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&ro->ro_dst);
3423 SCTP_IPI_ADDR_RLOCK();
3424 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUNDALL) {
3425 /*
3426 * Bound all case
3427 */
3428 answer = sctp_choose_boundall(inp, stcb, net, ro, vrf_id,
3429 dest_is_priv, dest_is_loop,
3430 non_asoc_addr_ok, fam);
3431 SCTP_IPI_ADDR_RUNLOCK();
3432 return (answer);
3433 }
3434 /*
3435 * Subset bound case
3436 */
3437 if (stcb) {
3438 answer = sctp_choose_boundspecific_stcb(inp, stcb, ro,
3439 vrf_id, dest_is_priv,
3440 dest_is_loop,
3441 non_asoc_addr_ok, fam);
3442 } else {
3443 answer = sctp_choose_boundspecific_inp(inp, ro, vrf_id,
3444 non_asoc_addr_ok,
3445 dest_is_priv,
3446 dest_is_loop, fam);
3447 }
3448 SCTP_IPI_ADDR_RUNLOCK();
3449 return (answer);
3450 }
3451
3452 static bool
sctp_find_cmsg(int c_type,void * data,struct mbuf * control,size_t cpsize)3453 sctp_find_cmsg(int c_type, void *data, struct mbuf *control, size_t cpsize)
3454 {
3455 struct cmsghdr cmh;
3456 struct sctp_sndinfo sndinfo;
3457 struct sctp_prinfo prinfo;
3458 struct sctp_authinfo authinfo;
3459 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3460 bool found;
3461
3462 /*
3463 * Independent of how many mbufs, find the c_type inside the control
3464 * structure and copy out the data.
3465 */
3466 found = false;
3467 tot_len = SCTP_BUF_LEN(control);
3468 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3469 rem_len = tot_len - off;
3470 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3471 /* There is not enough room for one more. */
3472 return (found);
3473 }
3474 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3475 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3476 /* We dont't have a complete CMSG header. */
3477 return (found);
3478 }
3479 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3480 /* We don't have the complete CMSG. */
3481 return (found);
3482 }
3483 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3484 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3485 if ((cmh.cmsg_level == IPPROTO_SCTP) &&
3486 ((c_type == cmh.cmsg_type) ||
3487 ((c_type == SCTP_SNDRCV) &&
3488 ((cmh.cmsg_type == SCTP_SNDINFO) ||
3489 (cmh.cmsg_type == SCTP_PRINFO) ||
3490 (cmh.cmsg_type == SCTP_AUTHINFO))))) {
3491 if (c_type == cmh.cmsg_type) {
3492 if (cpsize > INT_MAX) {
3493 return (found);
3494 }
3495 if (cmsg_data_len < (int)cpsize) {
3496 return (found);
3497 }
3498 /* It is exactly what we want. Copy it out. */
3499 m_copydata(control, cmsg_data_off, (int)cpsize, (caddr_t)data);
3500 return (1);
3501 } else {
3502 struct sctp_sndrcvinfo *sndrcvinfo;
3503
3504 sndrcvinfo = (struct sctp_sndrcvinfo *)data;
3505 if (!found) {
3506 if (cpsize < sizeof(struct sctp_sndrcvinfo)) {
3507 return (found);
3508 }
3509 memset(sndrcvinfo, 0, sizeof(struct sctp_sndrcvinfo));
3510 }
3511 switch (cmh.cmsg_type) {
3512 case SCTP_SNDINFO:
3513 if (cmsg_data_len < (int)sizeof(struct sctp_sndinfo)) {
3514 return (found);
3515 }
3516 m_copydata(control, cmsg_data_off, sizeof(struct sctp_sndinfo), (caddr_t)&sndinfo);
3517 sndrcvinfo->sinfo_stream = sndinfo.snd_sid;
3518 sndrcvinfo->sinfo_flags = sndinfo.snd_flags;
3519 sndrcvinfo->sinfo_ppid = sndinfo.snd_ppid;
3520 sndrcvinfo->sinfo_context = sndinfo.snd_context;
3521 sndrcvinfo->sinfo_assoc_id = sndinfo.snd_assoc_id;
3522 break;
3523 case SCTP_PRINFO:
3524 if (cmsg_data_len < (int)sizeof(struct sctp_prinfo)) {
3525 return (found);
3526 }
3527 m_copydata(control, cmsg_data_off, sizeof(struct sctp_prinfo), (caddr_t)&prinfo);
3528 if (prinfo.pr_policy != SCTP_PR_SCTP_NONE) {
3529 sndrcvinfo->sinfo_timetolive = prinfo.pr_value;
3530 } else {
3531 sndrcvinfo->sinfo_timetolive = 0;
3532 }
3533 sndrcvinfo->sinfo_flags |= prinfo.pr_policy;
3534 break;
3535 case SCTP_AUTHINFO:
3536 if (cmsg_data_len < (int)sizeof(struct sctp_authinfo)) {
3537 return (found);
3538 }
3539 m_copydata(control, cmsg_data_off, sizeof(struct sctp_authinfo), (caddr_t)&authinfo);
3540 sndrcvinfo->sinfo_keynumber_valid = 1;
3541 sndrcvinfo->sinfo_keynumber = authinfo.auth_keynumber;
3542 break;
3543 default:
3544 return (found);
3545 }
3546 found = true;
3547 }
3548 }
3549 }
3550 return (found);
3551 }
3552
3553 static int
sctp_process_cmsgs_for_init(struct sctp_tcb * stcb,struct mbuf * control,int * error)3554 sctp_process_cmsgs_for_init(struct sctp_tcb *stcb, struct mbuf *control, int *error)
3555 {
3556 struct cmsghdr cmh;
3557 struct sctp_initmsg initmsg;
3558 #ifdef INET
3559 struct sockaddr_in sin;
3560 #endif
3561 #ifdef INET6
3562 struct sockaddr_in6 sin6;
3563 #endif
3564 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3565
3566 tot_len = SCTP_BUF_LEN(control);
3567 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3568 rem_len = tot_len - off;
3569 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3570 /* There is not enough room for one more. */
3571 *error = EINVAL;
3572 return (1);
3573 }
3574 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3575 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3576 /* We dont't have a complete CMSG header. */
3577 *error = EINVAL;
3578 return (1);
3579 }
3580 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3581 /* We don't have the complete CMSG. */
3582 *error = EINVAL;
3583 return (1);
3584 }
3585 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3586 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3587 if (cmh.cmsg_level == IPPROTO_SCTP) {
3588 switch (cmh.cmsg_type) {
3589 case SCTP_INIT:
3590 if (cmsg_data_len < (int)sizeof(struct sctp_initmsg)) {
3591 *error = EINVAL;
3592 return (1);
3593 }
3594 m_copydata(control, cmsg_data_off, sizeof(struct sctp_initmsg), (caddr_t)&initmsg);
3595 if (initmsg.sinit_max_attempts)
3596 stcb->asoc.max_init_times = initmsg.sinit_max_attempts;
3597 if (initmsg.sinit_num_ostreams)
3598 stcb->asoc.pre_open_streams = initmsg.sinit_num_ostreams;
3599 if (initmsg.sinit_max_instreams)
3600 stcb->asoc.max_inbound_streams = initmsg.sinit_max_instreams;
3601 if (initmsg.sinit_max_init_timeo)
3602 stcb->asoc.initial_init_rto_max = initmsg.sinit_max_init_timeo;
3603 if (stcb->asoc.streamoutcnt < stcb->asoc.pre_open_streams) {
3604 struct sctp_stream_out *tmp_str;
3605 unsigned int i;
3606 #if defined(SCTP_DETAILED_STR_STATS)
3607 int j;
3608 #endif
3609
3610 /* Default is NOT correct */
3611 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, default:%d pre_open:%d\n",
3612 stcb->asoc.streamoutcnt, stcb->asoc.pre_open_streams);
3613 SCTP_TCB_UNLOCK(stcb);
3614 SCTP_MALLOC(tmp_str,
3615 struct sctp_stream_out *,
3616 (stcb->asoc.pre_open_streams * sizeof(struct sctp_stream_out)),
3617 SCTP_M_STRMO);
3618 SCTP_TCB_LOCK(stcb);
3619 if (tmp_str != NULL) {
3620 SCTP_FREE(stcb->asoc.strmout, SCTP_M_STRMO);
3621 stcb->asoc.strmout = tmp_str;
3622 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt = stcb->asoc.pre_open_streams;
3623 } else {
3624 stcb->asoc.pre_open_streams = stcb->asoc.streamoutcnt;
3625 }
3626 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
3627 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
3628 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
3629 stcb->asoc.strmout[i].chunks_on_queues = 0;
3630 #if defined(SCTP_DETAILED_STR_STATS)
3631 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
3632 stcb->asoc.strmout[i].abandoned_sent[j] = 0;
3633 stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
3634 }
3635 #else
3636 stcb->asoc.strmout[i].abandoned_sent[0] = 0;
3637 stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
3638 #endif
3639 stcb->asoc.strmout[i].next_mid_ordered = 0;
3640 stcb->asoc.strmout[i].next_mid_unordered = 0;
3641 stcb->asoc.strmout[i].sid = i;
3642 stcb->asoc.strmout[i].last_msg_incomplete = 0;
3643 stcb->asoc.strmout[i].state = SCTP_STREAM_OPENING;
3644 }
3645 }
3646 break;
3647 #ifdef INET
3648 case SCTP_DSTADDRV4:
3649 if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3650 *error = EINVAL;
3651 return (1);
3652 }
3653 memset(&sin, 0, sizeof(struct sockaddr_in));
3654 sin.sin_family = AF_INET;
3655 sin.sin_len = sizeof(struct sockaddr_in);
3656 sin.sin_port = stcb->rport;
3657 m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3658 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3659 (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3660 IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3661 *error = EINVAL;
3662 return (1);
3663 }
3664 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3665 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3666 *error = ENOBUFS;
3667 return (1);
3668 }
3669 break;
3670 #endif
3671 #ifdef INET6
3672 case SCTP_DSTADDRV6:
3673 if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3674 *error = EINVAL;
3675 return (1);
3676 }
3677 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3678 sin6.sin6_family = AF_INET6;
3679 sin6.sin6_len = sizeof(struct sockaddr_in6);
3680 sin6.sin6_port = stcb->rport;
3681 m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3682 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) ||
3683 IN6_IS_ADDR_MULTICAST(&sin6.sin6_addr)) {
3684 *error = EINVAL;
3685 return (1);
3686 }
3687 #ifdef INET
3688 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3689 in6_sin6_2_sin(&sin, &sin6);
3690 if ((sin.sin_addr.s_addr == INADDR_ANY) ||
3691 (sin.sin_addr.s_addr == INADDR_BROADCAST) ||
3692 IN_MULTICAST(ntohl(sin.sin_addr.s_addr))) {
3693 *error = EINVAL;
3694 return (1);
3695 }
3696 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin, NULL, stcb->asoc.port,
3697 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3698 *error = ENOBUFS;
3699 return (1);
3700 }
3701 } else
3702 #endif
3703 if (sctp_add_remote_addr(stcb, (struct sockaddr *)&sin6, NULL, stcb->asoc.port,
3704 SCTP_DONOT_SETSCOPE, SCTP_ADDR_IS_CONFIRMED)) {
3705 *error = ENOBUFS;
3706 return (1);
3707 }
3708 break;
3709 #endif
3710 default:
3711 break;
3712 }
3713 }
3714 }
3715 return (0);
3716 }
3717
3718 #if defined(INET) || defined(INET6)
3719 static struct sctp_tcb *
sctp_findassociation_cmsgs(struct sctp_inpcb ** inp_p,uint16_t port,struct mbuf * control,struct sctp_nets ** net_p,int * error)3720 sctp_findassociation_cmsgs(struct sctp_inpcb **inp_p,
3721 uint16_t port,
3722 struct mbuf *control,
3723 struct sctp_nets **net_p,
3724 int *error)
3725 {
3726 struct cmsghdr cmh;
3727 struct sctp_tcb *stcb;
3728 struct sockaddr *addr;
3729 #ifdef INET
3730 struct sockaddr_in sin;
3731 #endif
3732 #ifdef INET6
3733 struct sockaddr_in6 sin6;
3734 #endif
3735 int tot_len, rem_len, cmsg_data_len, cmsg_data_off, off;
3736
3737 tot_len = SCTP_BUF_LEN(control);
3738 for (off = 0; off < tot_len; off += CMSG_ALIGN(cmh.cmsg_len)) {
3739 rem_len = tot_len - off;
3740 if (rem_len < (int)CMSG_ALIGN(sizeof(cmh))) {
3741 /* There is not enough room for one more. */
3742 *error = EINVAL;
3743 return (NULL);
3744 }
3745 m_copydata(control, off, sizeof(cmh), (caddr_t)&cmh);
3746 if (cmh.cmsg_len < CMSG_ALIGN(sizeof(cmh))) {
3747 /* We dont't have a complete CMSG header. */
3748 *error = EINVAL;
3749 return (NULL);
3750 }
3751 if ((cmh.cmsg_len > INT_MAX) || ((int)cmh.cmsg_len > rem_len)) {
3752 /* We don't have the complete CMSG. */
3753 *error = EINVAL;
3754 return (NULL);
3755 }
3756 cmsg_data_len = (int)cmh.cmsg_len - CMSG_ALIGN(sizeof(cmh));
3757 cmsg_data_off = off + CMSG_ALIGN(sizeof(cmh));
3758 if (cmh.cmsg_level == IPPROTO_SCTP) {
3759 switch (cmh.cmsg_type) {
3760 #ifdef INET
3761 case SCTP_DSTADDRV4:
3762 if (cmsg_data_len < (int)sizeof(struct in_addr)) {
3763 *error = EINVAL;
3764 return (NULL);
3765 }
3766 memset(&sin, 0, sizeof(struct sockaddr_in));
3767 sin.sin_family = AF_INET;
3768 sin.sin_len = sizeof(struct sockaddr_in);
3769 sin.sin_port = port;
3770 m_copydata(control, cmsg_data_off, sizeof(struct in_addr), (caddr_t)&sin.sin_addr);
3771 addr = (struct sockaddr *)&sin;
3772 break;
3773 #endif
3774 #ifdef INET6
3775 case SCTP_DSTADDRV6:
3776 if (cmsg_data_len < (int)sizeof(struct in6_addr)) {
3777 *error = EINVAL;
3778 return (NULL);
3779 }
3780 memset(&sin6, 0, sizeof(struct sockaddr_in6));
3781 sin6.sin6_family = AF_INET6;
3782 sin6.sin6_len = sizeof(struct sockaddr_in6);
3783 sin6.sin6_port = port;
3784 m_copydata(control, cmsg_data_off, sizeof(struct in6_addr), (caddr_t)&sin6.sin6_addr);
3785 #ifdef INET
3786 if (IN6_IS_ADDR_V4MAPPED(&sin6.sin6_addr)) {
3787 in6_sin6_2_sin(&sin, &sin6);
3788 addr = (struct sockaddr *)&sin;
3789 } else
3790 #endif
3791 addr = (struct sockaddr *)&sin6;
3792 break;
3793 #endif
3794 default:
3795 addr = NULL;
3796 break;
3797 }
3798 if (addr) {
3799 stcb = sctp_findassociation_ep_addr(inp_p, addr, net_p, NULL, NULL);
3800 if (stcb != NULL) {
3801 return (stcb);
3802 }
3803 }
3804 }
3805 }
3806 return (NULL);
3807 }
3808 #endif
3809
3810 static struct mbuf *
sctp_add_cookie(struct mbuf * init,int init_offset,struct mbuf * initack,int initack_offset,struct sctp_state_cookie * stc_in,uint8_t ** signature)3811 sctp_add_cookie(struct mbuf *init, int init_offset,
3812 struct mbuf *initack, int initack_offset, struct sctp_state_cookie *stc_in, uint8_t **signature)
3813 {
3814 struct mbuf *copy_init, *copy_initack, *m_at, *sig, *mret;
3815 struct sctp_state_cookie *stc;
3816 struct sctp_paramhdr *ph;
3817 uint16_t cookie_sz;
3818
3819 mret = sctp_get_mbuf_for_msg((sizeof(struct sctp_state_cookie) +
3820 sizeof(struct sctp_paramhdr)), 0,
3821 M_NOWAIT, 1, MT_DATA);
3822 if (mret == NULL) {
3823 return (NULL);
3824 }
3825 copy_init = SCTP_M_COPYM(init, init_offset, M_COPYALL, M_NOWAIT);
3826 if (copy_init == NULL) {
3827 sctp_m_freem(mret);
3828 return (NULL);
3829 }
3830 #ifdef SCTP_MBUF_LOGGING
3831 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3832 sctp_log_mbc(copy_init, SCTP_MBUF_ICOPY);
3833 }
3834 #endif
3835 copy_initack = SCTP_M_COPYM(initack, initack_offset, M_COPYALL,
3836 M_NOWAIT);
3837 if (copy_initack == NULL) {
3838 sctp_m_freem(mret);
3839 sctp_m_freem(copy_init);
3840 return (NULL);
3841 }
3842 #ifdef SCTP_MBUF_LOGGING
3843 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
3844 sctp_log_mbc(copy_initack, SCTP_MBUF_ICOPY);
3845 }
3846 #endif
3847 /* easy side we just drop it on the end */
3848 ph = mtod(mret, struct sctp_paramhdr *);
3849 SCTP_BUF_LEN(mret) = sizeof(struct sctp_state_cookie) +
3850 sizeof(struct sctp_paramhdr);
3851 stc = (struct sctp_state_cookie *)((caddr_t)ph +
3852 sizeof(struct sctp_paramhdr));
3853 ph->param_type = htons(SCTP_STATE_COOKIE);
3854 ph->param_length = 0; /* fill in at the end */
3855 /* Fill in the stc cookie data */
3856 memcpy(stc, stc_in, sizeof(struct sctp_state_cookie));
3857
3858 /* tack the INIT and then the INIT-ACK onto the chain */
3859 cookie_sz = 0;
3860 for (m_at = mret; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3861 cookie_sz += SCTP_BUF_LEN(m_at);
3862 if (SCTP_BUF_NEXT(m_at) == NULL) {
3863 SCTP_BUF_NEXT(m_at) = copy_init;
3864 break;
3865 }
3866 }
3867 for (m_at = copy_init; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3868 cookie_sz += SCTP_BUF_LEN(m_at);
3869 if (SCTP_BUF_NEXT(m_at) == NULL) {
3870 SCTP_BUF_NEXT(m_at) = copy_initack;
3871 break;
3872 }
3873 }
3874 for (m_at = copy_initack; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
3875 cookie_sz += SCTP_BUF_LEN(m_at);
3876 if (SCTP_BUF_NEXT(m_at) == NULL) {
3877 break;
3878 }
3879 }
3880 sig = sctp_get_mbuf_for_msg(SCTP_SIGNATURE_SIZE, 0, M_NOWAIT, 1, MT_DATA);
3881 if (sig == NULL) {
3882 /* no space, so free the entire chain */
3883 sctp_m_freem(mret);
3884 return (NULL);
3885 }
3886 SCTP_BUF_NEXT(m_at) = sig;
3887 SCTP_BUF_LEN(sig) = SCTP_SIGNATURE_SIZE;
3888 cookie_sz += SCTP_SIGNATURE_SIZE;
3889 ph->param_length = htons(cookie_sz);
3890 *signature = (uint8_t *)mtod(sig, caddr_t);
3891 memset(*signature, 0, SCTP_SIGNATURE_SIZE);
3892 return (mret);
3893 }
3894
3895 static uint8_t
sctp_get_ect(struct sctp_tcb * stcb)3896 sctp_get_ect(struct sctp_tcb *stcb)
3897 {
3898 if ((stcb != NULL) && (stcb->asoc.ecn_supported == 1)) {
3899 return (SCTP_ECT0_BIT);
3900 } else {
3901 return (0);
3902 }
3903 }
3904
3905 #if defined(INET) || defined(INET6)
3906 static void
sctp_handle_no_route(struct sctp_tcb * stcb,struct sctp_nets * net,int so_locked)3907 sctp_handle_no_route(struct sctp_tcb *stcb,
3908 struct sctp_nets *net,
3909 int so_locked)
3910 {
3911 SCTPDBG(SCTP_DEBUG_OUTPUT1, "dropped packet - no valid source addr\n");
3912
3913 if (net) {
3914 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Destination was ");
3915 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT1, &net->ro._l_addr.sa);
3916 if (net->dest_state & SCTP_ADDR_CONFIRMED) {
3917 if ((net->dest_state & SCTP_ADDR_REACHABLE) && stcb) {
3918 SCTPDBG(SCTP_DEBUG_OUTPUT1, "no route takes interface %p down\n", (void *)net);
3919 sctp_ulp_notify(SCTP_NOTIFY_INTERFACE_DOWN,
3920 stcb, 0,
3921 (void *)net,
3922 so_locked);
3923 net->dest_state &= ~SCTP_ADDR_REACHABLE;
3924 net->dest_state &= ~SCTP_ADDR_PF;
3925 }
3926 }
3927 if (stcb) {
3928 if (net == stcb->asoc.primary_destination) {
3929 /* need a new primary */
3930 struct sctp_nets *alt;
3931
3932 alt = sctp_find_alternate_net(stcb, net, 0);
3933 if (alt != net) {
3934 if (stcb->asoc.alternate) {
3935 sctp_free_remote_addr(stcb->asoc.alternate);
3936 }
3937 stcb->asoc.alternate = alt;
3938 atomic_add_int(&stcb->asoc.alternate->ref_count, 1);
3939 if (net->ro._s_addr) {
3940 sctp_free_ifa(net->ro._s_addr);
3941 net->ro._s_addr = NULL;
3942 }
3943 net->src_addr_selected = 0;
3944 }
3945 }
3946 }
3947 }
3948 }
3949 #endif
3950
3951 static int
sctp_lowlevel_chunk_output(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * net,struct sockaddr * to,struct mbuf * m,uint32_t auth_offset,struct sctp_auth_chunk * auth,uint16_t auth_keyid,int nofragment_flag,int ecn_ok,int out_of_asoc_ok,uint16_t src_port,uint16_t dest_port,uint32_t v_tag,uint16_t port,union sctp_sockstore * over_addr,uint8_t mflowtype,uint32_t mflowid,int so_locked)3952 sctp_lowlevel_chunk_output(struct sctp_inpcb *inp,
3953 struct sctp_tcb *stcb, /* may be NULL */
3954 struct sctp_nets *net,
3955 struct sockaddr *to,
3956 struct mbuf *m,
3957 uint32_t auth_offset,
3958 struct sctp_auth_chunk *auth,
3959 uint16_t auth_keyid,
3960 int nofragment_flag,
3961 int ecn_ok,
3962 int out_of_asoc_ok,
3963 uint16_t src_port,
3964 uint16_t dest_port,
3965 uint32_t v_tag,
3966 uint16_t port,
3967 union sctp_sockstore *over_addr,
3968 uint8_t mflowtype, uint32_t mflowid,
3969 int so_locked)
3970 {
3971 /* nofragment_flag to tell if IP_DF should be set (IPv4 only) */
3972 /**
3973 * Given a mbuf chain (via SCTP_BUF_NEXT()) that holds a packet header
3974 * WITH an SCTPHDR but no IP header, endpoint inp and sa structure:
3975 * - fill in the HMAC digest of any AUTH chunk in the packet.
3976 * - calculate and fill in the SCTP checksum.
3977 * - prepend an IP address header.
3978 * - if boundall use INADDR_ANY.
3979 * - if boundspecific do source address selection.
3980 * - set fragmentation option for ipV4.
3981 * - On return from IP output, check/adjust mtu size of output
3982 * interface and smallest_mtu size as well.
3983 */
3984 /* Will need ifdefs around this */
3985 struct mbuf *newm;
3986 struct sctphdr *sctphdr;
3987 int packet_length;
3988 int ret;
3989 #if defined(INET) || defined(INET6)
3990 uint32_t vrf_id;
3991 #endif
3992 #if defined(INET) || defined(INET6)
3993 struct mbuf *o_pak;
3994 sctp_route_t *ro = NULL;
3995 struct udphdr *udp = NULL;
3996 #endif
3997 uint8_t tos_value;
3998
3999 if ((net) && (net->dest_state & SCTP_ADDR_OUT_OF_SCOPE)) {
4000 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4001 sctp_m_freem(m);
4002 return (EFAULT);
4003 }
4004 #if defined(INET) || defined(INET6)
4005 if (stcb) {
4006 vrf_id = stcb->asoc.vrf_id;
4007 } else {
4008 vrf_id = inp->def_vrf_id;
4009 }
4010 #endif
4011 /* fill in the HMAC digest for any AUTH chunk in the packet */
4012 if ((auth != NULL) && (stcb != NULL)) {
4013 sctp_fill_hmac_digest_m(m, auth_offset, auth, stcb, auth_keyid);
4014 }
4015
4016 if (net) {
4017 tos_value = net->dscp;
4018 } else if (stcb) {
4019 tos_value = stcb->asoc.default_dscp;
4020 } else {
4021 tos_value = inp->sctp_ep.default_dscp;
4022 }
4023
4024 switch (to->sa_family) {
4025 #ifdef INET
4026 case AF_INET:
4027 {
4028 struct ip *ip = NULL;
4029 sctp_route_t iproute;
4030 int len;
4031
4032 len = SCTP_MIN_V4_OVERHEAD;
4033 if (port) {
4034 len += sizeof(struct udphdr);
4035 }
4036 newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4037 if (newm == NULL) {
4038 sctp_m_freem(m);
4039 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4040 return (ENOMEM);
4041 }
4042 SCTP_ALIGN_TO_END(newm, len);
4043 SCTP_BUF_LEN(newm) = len;
4044 SCTP_BUF_NEXT(newm) = m;
4045 m = newm;
4046 if (net != NULL) {
4047 m->m_pkthdr.flowid = net->flowid;
4048 M_HASHTYPE_SET(m, net->flowtype);
4049 } else {
4050 m->m_pkthdr.flowid = mflowid;
4051 M_HASHTYPE_SET(m, mflowtype);
4052 }
4053 packet_length = sctp_calculate_len(m);
4054 ip = mtod(m, struct ip *);
4055 ip->ip_v = IPVERSION;
4056 ip->ip_hl = (sizeof(struct ip) >> 2);
4057 if (tos_value == 0) {
4058 /*
4059 * This means especially, that it is not set
4060 * at the SCTP layer. So use the value from
4061 * the IP layer.
4062 */
4063 tos_value = inp->ip_inp.inp.inp_ip_tos;
4064 }
4065 tos_value &= 0xfc;
4066 if (ecn_ok) {
4067 tos_value |= sctp_get_ect(stcb);
4068 }
4069 if ((nofragment_flag) && (port == 0)) {
4070 ip->ip_off = htons(IP_DF);
4071 } else {
4072 ip->ip_off = htons(0);
4073 }
4074 /* FreeBSD has a function for ip_id's */
4075 ip_fillid(ip);
4076
4077 ip->ip_ttl = inp->ip_inp.inp.inp_ip_ttl;
4078 ip->ip_len = htons(packet_length);
4079 ip->ip_tos = tos_value;
4080 if (port) {
4081 ip->ip_p = IPPROTO_UDP;
4082 } else {
4083 ip->ip_p = IPPROTO_SCTP;
4084 }
4085 ip->ip_sum = 0;
4086 if (net == NULL) {
4087 ro = &iproute;
4088 memset(&iproute, 0, sizeof(iproute));
4089 memcpy(&ro->ro_dst, to, to->sa_len);
4090 } else {
4091 ro = (sctp_route_t *)&net->ro;
4092 }
4093 /* Now the address selection part */
4094 ip->ip_dst.s_addr = ((struct sockaddr_in *)to)->sin_addr.s_addr;
4095
4096 /* call the routine to select the src address */
4097 if (net && out_of_asoc_ok == 0) {
4098 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4099 sctp_free_ifa(net->ro._s_addr);
4100 net->ro._s_addr = NULL;
4101 net->src_addr_selected = 0;
4102 RO_NHFREE(ro);
4103 }
4104 if (net->src_addr_selected == 0) {
4105 /* Cache the source address */
4106 net->ro._s_addr = sctp_source_address_selection(inp, stcb,
4107 ro, net, 0,
4108 vrf_id);
4109 net->src_addr_selected = 1;
4110 }
4111 if (net->ro._s_addr == NULL) {
4112 /* No route to host */
4113 net->src_addr_selected = 0;
4114 sctp_handle_no_route(stcb, net, so_locked);
4115 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4116 sctp_m_freem(m);
4117 return (EHOSTUNREACH);
4118 }
4119 ip->ip_src = net->ro._s_addr->address.sin.sin_addr;
4120 } else {
4121 if (over_addr == NULL) {
4122 struct sctp_ifa *_lsrc;
4123
4124 _lsrc = sctp_source_address_selection(inp, stcb, ro,
4125 net,
4126 out_of_asoc_ok,
4127 vrf_id);
4128 if (_lsrc == NULL) {
4129 sctp_handle_no_route(stcb, net, so_locked);
4130 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4131 sctp_m_freem(m);
4132 return (EHOSTUNREACH);
4133 }
4134 ip->ip_src = _lsrc->address.sin.sin_addr;
4135 sctp_free_ifa(_lsrc);
4136 } else {
4137 ip->ip_src = over_addr->sin.sin_addr;
4138 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4139 }
4140 }
4141 if (port) {
4142 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4143 sctp_handle_no_route(stcb, net, so_locked);
4144 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4145 sctp_m_freem(m);
4146 return (EHOSTUNREACH);
4147 }
4148 udp = (struct udphdr *)((caddr_t)ip + sizeof(struct ip));
4149 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4150 udp->uh_dport = port;
4151 udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip)));
4152 if (V_udp_cksum) {
4153 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
4154 } else {
4155 udp->uh_sum = 0;
4156 }
4157 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4158 } else {
4159 sctphdr = (struct sctphdr *)((caddr_t)ip + sizeof(struct ip));
4160 }
4161
4162 sctphdr->src_port = src_port;
4163 sctphdr->dest_port = dest_port;
4164 sctphdr->v_tag = v_tag;
4165 sctphdr->checksum = 0;
4166
4167 /*
4168 * If source address selection fails and we find no
4169 * route then the ip_output should fail as well with
4170 * a NO_ROUTE_TO_HOST type error. We probably should
4171 * catch that somewhere and abort the association
4172 * right away (assuming this is an INIT being sent).
4173 */
4174 if (ro->ro_nh == NULL) {
4175 /*
4176 * src addr selection failed to find a route
4177 * (or valid source addr), so we can't get
4178 * there from here (yet)!
4179 */
4180 sctp_handle_no_route(stcb, net, so_locked);
4181 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4182 sctp_m_freem(m);
4183 return (EHOSTUNREACH);
4184 }
4185 if (ro != &iproute) {
4186 memcpy(&iproute, ro, sizeof(*ro));
4187 }
4188 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv4 output routine from low level src addr:%x\n",
4189 (uint32_t)(ntohl(ip->ip_src.s_addr)));
4190 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Destination is %x\n",
4191 (uint32_t)(ntohl(ip->ip_dst.s_addr)));
4192 SCTPDBG(SCTP_DEBUG_OUTPUT3, "RTP route is %p through\n",
4193 (void *)ro->ro_nh);
4194
4195 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4196 /* failed to prepend data, give up */
4197 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4198 sctp_m_freem(m);
4199 return (ENOMEM);
4200 }
4201 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4202 if (port) {
4203 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip) + sizeof(struct udphdr));
4204 SCTP_STAT_INCR(sctps_sendswcrc);
4205 if (V_udp_cksum) {
4206 SCTP_ENABLE_UDP_CSUM(o_pak);
4207 }
4208 } else {
4209 m->m_pkthdr.csum_flags = CSUM_SCTP;
4210 m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4211 SCTP_STAT_INCR(sctps_sendhwcrc);
4212 }
4213 #ifdef SCTP_PACKET_LOGGING
4214 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4215 sctp_packet_log(o_pak);
4216 #endif
4217 /* send it out. table id is taken from stcb */
4218 SCTP_PROBE5(send, NULL, stcb, ip, stcb, sctphdr);
4219 SCTP_IP_OUTPUT(ret, o_pak, ro, inp, vrf_id);
4220 if (port) {
4221 UDPSTAT_INC(udps_opackets);
4222 }
4223 SCTP_STAT_INCR(sctps_sendpackets);
4224 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4225 if (ret)
4226 SCTP_STAT_INCR(sctps_senderrors);
4227
4228 SCTPDBG(SCTP_DEBUG_OUTPUT3, "IP output returns %d\n", ret);
4229 if (net == NULL) {
4230 /* free tempy routes */
4231 RO_NHFREE(ro);
4232 } else {
4233 if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4234 ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4235 uint32_t mtu;
4236
4237 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4238 if (mtu > 0) {
4239 if (net->port) {
4240 mtu -= sizeof(struct udphdr);
4241 }
4242 if (mtu < net->mtu) {
4243 net->mtu = mtu;
4244 if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4245 sctp_pathmtu_adjustment(stcb, mtu, true);
4246 }
4247 }
4248 }
4249 } else if (ro->ro_nh == NULL) {
4250 /* route was freed */
4251 if (net->ro._s_addr &&
4252 net->src_addr_selected) {
4253 sctp_free_ifa(net->ro._s_addr);
4254 net->ro._s_addr = NULL;
4255 }
4256 net->src_addr_selected = 0;
4257 }
4258 }
4259 return (ret);
4260 }
4261 #endif
4262 #ifdef INET6
4263 case AF_INET6:
4264 {
4265 uint32_t flowlabel, flowinfo;
4266 struct ip6_hdr *ip6h;
4267 struct route_in6 ip6route;
4268 struct ifnet *ifp;
4269 struct sockaddr_in6 *sin6, tmp, *lsa6, lsa6_tmp;
4270 int prev_scope = 0;
4271 struct sockaddr_in6 lsa6_storage;
4272 int error;
4273 u_short prev_port = 0;
4274 int len;
4275
4276 if (net) {
4277 flowlabel = net->flowlabel;
4278 } else if (stcb) {
4279 flowlabel = stcb->asoc.default_flowlabel;
4280 } else {
4281 flowlabel = inp->sctp_ep.default_flowlabel;
4282 }
4283 if (flowlabel == 0) {
4284 /*
4285 * This means especially, that it is not set
4286 * at the SCTP layer. So use the value from
4287 * the IP layer.
4288 */
4289 flowlabel = ntohl(((struct inpcb *)inp)->inp_flow);
4290 }
4291 flowlabel &= 0x000fffff;
4292 len = SCTP_MIN_OVERHEAD;
4293 if (port) {
4294 len += sizeof(struct udphdr);
4295 }
4296 newm = sctp_get_mbuf_for_msg(len, 1, M_NOWAIT, 1, MT_DATA);
4297 if (newm == NULL) {
4298 sctp_m_freem(m);
4299 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4300 return (ENOMEM);
4301 }
4302 SCTP_ALIGN_TO_END(newm, len);
4303 SCTP_BUF_LEN(newm) = len;
4304 SCTP_BUF_NEXT(newm) = m;
4305 m = newm;
4306 if (net != NULL) {
4307 m->m_pkthdr.flowid = net->flowid;
4308 M_HASHTYPE_SET(m, net->flowtype);
4309 } else {
4310 m->m_pkthdr.flowid = mflowid;
4311 M_HASHTYPE_SET(m, mflowtype);
4312 }
4313 packet_length = sctp_calculate_len(m);
4314
4315 ip6h = mtod(m, struct ip6_hdr *);
4316 /* protect *sin6 from overwrite */
4317 sin6 = (struct sockaddr_in6 *)to;
4318 tmp = *sin6;
4319 sin6 = &tmp;
4320
4321 /* KAME hack: embed scopeid */
4322 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4323 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4324 sctp_m_freem(m);
4325 return (EINVAL);
4326 }
4327 if (net == NULL) {
4328 memset(&ip6route, 0, sizeof(ip6route));
4329 ro = (sctp_route_t *)&ip6route;
4330 memcpy(&ro->ro_dst, sin6, sin6->sin6_len);
4331 } else {
4332 ro = (sctp_route_t *)&net->ro;
4333 }
4334 /*
4335 * We assume here that inp_flow is in host byte
4336 * order within the TCB!
4337 */
4338 if (tos_value == 0) {
4339 /*
4340 * This means especially, that it is not set
4341 * at the SCTP layer. So use the value from
4342 * the IP layer.
4343 */
4344 tos_value = (ntohl(((struct inpcb *)inp)->inp_flow) >> 20) & 0xff;
4345 }
4346 tos_value &= 0xfc;
4347 if (ecn_ok) {
4348 tos_value |= sctp_get_ect(stcb);
4349 }
4350 flowinfo = 0x06;
4351 flowinfo <<= 8;
4352 flowinfo |= tos_value;
4353 flowinfo <<= 20;
4354 flowinfo |= flowlabel;
4355 ip6h->ip6_flow = htonl(flowinfo);
4356 if (port) {
4357 ip6h->ip6_nxt = IPPROTO_UDP;
4358 } else {
4359 ip6h->ip6_nxt = IPPROTO_SCTP;
4360 }
4361 ip6h->ip6_plen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4362 ip6h->ip6_dst = sin6->sin6_addr;
4363
4364 /*
4365 * Add SRC address selection here: we can only reuse
4366 * to a limited degree the kame src-addr-sel, since
4367 * we can try their selection but it may not be
4368 * bound.
4369 */
4370 memset(&lsa6_tmp, 0, sizeof(lsa6_tmp));
4371 lsa6_tmp.sin6_family = AF_INET6;
4372 lsa6_tmp.sin6_len = sizeof(lsa6_tmp);
4373 lsa6 = &lsa6_tmp;
4374 if (net && out_of_asoc_ok == 0) {
4375 if (net->ro._s_addr && (net->ro._s_addr->localifa_flags & (SCTP_BEING_DELETED | SCTP_ADDR_IFA_UNUSEABLE))) {
4376 sctp_free_ifa(net->ro._s_addr);
4377 net->ro._s_addr = NULL;
4378 net->src_addr_selected = 0;
4379 RO_NHFREE(ro);
4380 }
4381 if (net->src_addr_selected == 0) {
4382 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4383 /* KAME hack: embed scopeid */
4384 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4385 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4386 sctp_m_freem(m);
4387 return (EINVAL);
4388 }
4389 /* Cache the source address */
4390 net->ro._s_addr = sctp_source_address_selection(inp,
4391 stcb,
4392 ro,
4393 net,
4394 0,
4395 vrf_id);
4396 (void)sa6_recoverscope(sin6);
4397 net->src_addr_selected = 1;
4398 }
4399 if (net->ro._s_addr == NULL) {
4400 SCTPDBG(SCTP_DEBUG_OUTPUT3, "V6:No route to host\n");
4401 net->src_addr_selected = 0;
4402 sctp_handle_no_route(stcb, net, so_locked);
4403 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4404 sctp_m_freem(m);
4405 return (EHOSTUNREACH);
4406 }
4407 lsa6->sin6_addr = net->ro._s_addr->address.sin6.sin6_addr;
4408 } else {
4409 sin6 = (struct sockaddr_in6 *)&ro->ro_dst;
4410 /* KAME hack: embed scopeid */
4411 if (sa6_embedscope(sin6, MODULE_GLOBAL(ip6_use_defzone)) != 0) {
4412 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
4413 sctp_m_freem(m);
4414 return (EINVAL);
4415 }
4416 if (over_addr == NULL) {
4417 struct sctp_ifa *_lsrc;
4418
4419 _lsrc = sctp_source_address_selection(inp, stcb, ro,
4420 net,
4421 out_of_asoc_ok,
4422 vrf_id);
4423 if (_lsrc == NULL) {
4424 sctp_handle_no_route(stcb, net, so_locked);
4425 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4426 sctp_m_freem(m);
4427 return (EHOSTUNREACH);
4428 }
4429 lsa6->sin6_addr = _lsrc->address.sin6.sin6_addr;
4430 sctp_free_ifa(_lsrc);
4431 } else {
4432 lsa6->sin6_addr = over_addr->sin6.sin6_addr;
4433 SCTP_RTALLOC(ro, vrf_id, inp->fibnum);
4434 }
4435 (void)sa6_recoverscope(sin6);
4436 }
4437 lsa6->sin6_port = inp->sctp_lport;
4438
4439 if (ro->ro_nh == NULL) {
4440 /*
4441 * src addr selection failed to find a route
4442 * (or valid source addr), so we can't get
4443 * there from here!
4444 */
4445 sctp_handle_no_route(stcb, net, so_locked);
4446 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4447 sctp_m_freem(m);
4448 return (EHOSTUNREACH);
4449 }
4450 /*
4451 * XXX: sa6 may not have a valid sin6_scope_id in
4452 * the non-SCOPEDROUTING case.
4453 */
4454 memset(&lsa6_storage, 0, sizeof(lsa6_storage));
4455 lsa6_storage.sin6_family = AF_INET6;
4456 lsa6_storage.sin6_len = sizeof(lsa6_storage);
4457 lsa6_storage.sin6_addr = lsa6->sin6_addr;
4458 if ((error = sa6_recoverscope(&lsa6_storage)) != 0) {
4459 SCTPDBG(SCTP_DEBUG_OUTPUT3, "recover scope fails error %d\n", error);
4460 sctp_m_freem(m);
4461 return (error);
4462 }
4463 /* XXX */
4464 lsa6_storage.sin6_addr = lsa6->sin6_addr;
4465 lsa6_storage.sin6_port = inp->sctp_lport;
4466 lsa6 = &lsa6_storage;
4467 ip6h->ip6_src = lsa6->sin6_addr;
4468
4469 if (port) {
4470 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
4471 sctp_handle_no_route(stcb, net, so_locked);
4472 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EHOSTUNREACH);
4473 sctp_m_freem(m);
4474 return (EHOSTUNREACH);
4475 }
4476 udp = (struct udphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4477 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
4478 udp->uh_dport = port;
4479 udp->uh_ulen = htons((uint16_t)(packet_length - sizeof(struct ip6_hdr)));
4480 udp->uh_sum = 0;
4481 sctphdr = (struct sctphdr *)((caddr_t)udp + sizeof(struct udphdr));
4482 } else {
4483 sctphdr = (struct sctphdr *)((caddr_t)ip6h + sizeof(struct ip6_hdr));
4484 }
4485
4486 sctphdr->src_port = src_port;
4487 sctphdr->dest_port = dest_port;
4488 sctphdr->v_tag = v_tag;
4489 sctphdr->checksum = 0;
4490
4491 /*
4492 * We set the hop limit now since there is a good
4493 * chance that our ro pointer is now filled
4494 */
4495 ip6h->ip6_hlim = SCTP_GET_HLIM(inp, ro);
4496 ifp = SCTP_GET_IFN_VOID_FROM_ROUTE(ro);
4497
4498 #ifdef SCTP_DEBUG
4499 /* Copy to be sure something bad is not happening */
4500 sin6->sin6_addr = ip6h->ip6_dst;
4501 lsa6->sin6_addr = ip6h->ip6_src;
4502 #endif
4503
4504 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Calling ipv6 output routine from low level\n");
4505 SCTPDBG(SCTP_DEBUG_OUTPUT3, "src: ");
4506 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)lsa6);
4507 SCTPDBG(SCTP_DEBUG_OUTPUT3, "dst: ");
4508 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT3, (struct sockaddr *)sin6);
4509 if (net) {
4510 sin6 = (struct sockaddr_in6 *)&net->ro._l_addr;
4511 /*
4512 * preserve the port and scope for link
4513 * local send
4514 */
4515 prev_scope = sin6->sin6_scope_id;
4516 prev_port = sin6->sin6_port;
4517 }
4518
4519 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
4520 /* failed to prepend data, give up */
4521 sctp_m_freem(m);
4522 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
4523 return (ENOMEM);
4524 }
4525 SCTP_ATTACH_CHAIN(o_pak, m, packet_length);
4526 if (port) {
4527 sctphdr->checksum = sctp_calculate_cksum(m, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
4528 SCTP_STAT_INCR(sctps_sendswcrc);
4529 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), packet_length - sizeof(struct ip6_hdr))) == 0) {
4530 udp->uh_sum = 0xffff;
4531 }
4532 } else {
4533 m->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
4534 m->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
4535 SCTP_STAT_INCR(sctps_sendhwcrc);
4536 }
4537 /* send it out. table id is taken from stcb */
4538 #ifdef SCTP_PACKET_LOGGING
4539 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING)
4540 sctp_packet_log(o_pak);
4541 #endif
4542 SCTP_PROBE5(send, NULL, stcb, ip6h, stcb, sctphdr);
4543 SCTP_IP6_OUTPUT(ret, o_pak, (struct route_in6 *)ro, &ifp, inp, vrf_id);
4544 if (net) {
4545 /* for link local this must be done */
4546 sin6->sin6_scope_id = prev_scope;
4547 sin6->sin6_port = prev_port;
4548 }
4549 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
4550 if (port) {
4551 UDPSTAT_INC(udps_opackets);
4552 }
4553 SCTP_STAT_INCR(sctps_sendpackets);
4554 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
4555 if (ret) {
4556 SCTP_STAT_INCR(sctps_senderrors);
4557 }
4558 if (net == NULL) {
4559 /* Now if we had a temp route free it */
4560 RO_NHFREE(ro);
4561 } else {
4562 /*
4563 * PMTU check versus smallest asoc MTU goes
4564 * here
4565 */
4566 if (ro->ro_nh == NULL) {
4567 /* Route was freed */
4568 if (net->ro._s_addr &&
4569 net->src_addr_selected) {
4570 sctp_free_ifa(net->ro._s_addr);
4571 net->ro._s_addr = NULL;
4572 }
4573 net->src_addr_selected = 0;
4574 }
4575 if ((ro->ro_nh != NULL) && (net->ro._s_addr) &&
4576 ((net->dest_state & SCTP_ADDR_NO_PMTUD) == 0)) {
4577 uint32_t mtu;
4578
4579 mtu = SCTP_GATHER_MTU_FROM_ROUTE(net->ro._s_addr, &net->ro._l_addr.sa, ro->ro_nh);
4580 if (mtu > 0) {
4581 if (net->port) {
4582 mtu -= sizeof(struct udphdr);
4583 }
4584 if (mtu < net->mtu) {
4585 net->mtu = mtu;
4586 if ((stcb != NULL) && (stcb->asoc.smallest_mtu > mtu)) {
4587 sctp_pathmtu_adjustment(stcb, mtu, false);
4588 }
4589 }
4590 }
4591 } else if (ifp != NULL) {
4592 if ((ND_IFINFO(ifp)->linkmtu > 0) &&
4593 (stcb->asoc.smallest_mtu > ND_IFINFO(ifp)->linkmtu)) {
4594 sctp_pathmtu_adjustment(stcb, ND_IFINFO(ifp)->linkmtu, false);
4595 }
4596 }
4597 }
4598 return (ret);
4599 }
4600 #endif
4601 default:
4602 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
4603 ((struct sockaddr *)to)->sa_family);
4604 sctp_m_freem(m);
4605 SCTP_LTRACE_ERR_RET_PKT(m, inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, EFAULT);
4606 return (EFAULT);
4607 }
4608 }
4609
4610 void
sctp_send_initiate(struct sctp_inpcb * inp,struct sctp_tcb * stcb,int so_locked)4611 sctp_send_initiate(struct sctp_inpcb *inp, struct sctp_tcb *stcb, int so_locked)
4612 {
4613 struct mbuf *m, *m_last;
4614 struct sctp_nets *net;
4615 struct sctp_init_chunk *init;
4616 struct sctp_supported_addr_param *sup_addr;
4617 struct sctp_adaptation_layer_indication *ali;
4618 struct sctp_supported_chunk_types_param *pr_supported;
4619 struct sctp_paramhdr *ph;
4620 int cnt_inits_to = 0;
4621 int error;
4622 uint16_t num_ext, chunk_len, padding_len, parameter_len;
4623
4624 /* INIT's always go to the primary (and usually ONLY address) */
4625 net = stcb->asoc.primary_destination;
4626 if (net == NULL) {
4627 net = TAILQ_FIRST(&stcb->asoc.nets);
4628 if (net == NULL) {
4629 /* TSNH */
4630 return;
4631 }
4632 /* we confirm any address we send an INIT to */
4633 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4634 (void)sctp_set_primary_addr(stcb, NULL, net);
4635 } else {
4636 /* we confirm any address we send an INIT to */
4637 net->dest_state &= ~SCTP_ADDR_UNCONFIRMED;
4638 }
4639 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT\n");
4640 #ifdef INET6
4641 if (net->ro._l_addr.sa.sa_family == AF_INET6) {
4642 /*
4643 * special hook, if we are sending to link local it will not
4644 * show up in our private address count.
4645 */
4646 if (IN6_IS_ADDR_LINKLOCAL(&net->ro._l_addr.sin6.sin6_addr))
4647 cnt_inits_to = 1;
4648 }
4649 #endif
4650 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
4651 /* This case should not happen */
4652 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - failed timer?\n");
4653 return;
4654 }
4655 /* start the INIT timer */
4656 sctp_timer_start(SCTP_TIMER_TYPE_INIT, inp, stcb, net);
4657
4658 m = sctp_get_mbuf_for_msg(MCLBYTES, 1, M_NOWAIT, 1, MT_DATA);
4659 if (m == NULL) {
4660 /* No memory, INIT timer will re-attempt. */
4661 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - mbuf?\n");
4662 return;
4663 }
4664 chunk_len = (uint16_t)sizeof(struct sctp_init_chunk);
4665 padding_len = 0;
4666 /* Now lets put the chunk header in place */
4667 init = mtod(m, struct sctp_init_chunk *);
4668 /* now the chunk header */
4669 init->ch.chunk_type = SCTP_INITIATION;
4670 init->ch.chunk_flags = 0;
4671 /* fill in later from mbuf we build */
4672 init->ch.chunk_length = 0;
4673 /* place in my tag */
4674 init->init.initiate_tag = htonl(stcb->asoc.my_vtag);
4675 /* set up some of the credits. */
4676 init->init.a_rwnd = htonl(max(inp->sctp_socket ? SCTP_SB_LIMIT_RCV(inp->sctp_socket) : 0,
4677 SCTP_MINIMAL_RWND));
4678 init->init.num_outbound_streams = htons(stcb->asoc.pre_open_streams);
4679 init->init.num_inbound_streams = htons(stcb->asoc.max_inbound_streams);
4680 init->init.initial_tsn = htonl(stcb->asoc.init_seq_number);
4681
4682 /* Adaptation layer indication parameter */
4683 if (inp->sctp_ep.adaptation_layer_indicator_provided) {
4684 parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
4685 ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
4686 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
4687 ali->ph.param_length = htons(parameter_len);
4688 ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
4689 chunk_len += parameter_len;
4690 }
4691
4692 /* ECN parameter */
4693 if (stcb->asoc.ecn_supported == 1) {
4694 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4695 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4696 ph->param_type = htons(SCTP_ECN_CAPABLE);
4697 ph->param_length = htons(parameter_len);
4698 chunk_len += parameter_len;
4699 }
4700
4701 /* PR-SCTP supported parameter */
4702 if (stcb->asoc.prsctp_supported == 1) {
4703 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4704 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4705 ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
4706 ph->param_length = htons(parameter_len);
4707 chunk_len += parameter_len;
4708 }
4709
4710 /* Add NAT friendly parameter. */
4711 if (SCTP_BASE_SYSCTL(sctp_inits_include_nat_friendly)) {
4712 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4713 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
4714 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
4715 ph->param_length = htons(parameter_len);
4716 chunk_len += parameter_len;
4717 }
4718
4719 /* And now tell the peer which extensions we support */
4720 num_ext = 0;
4721 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
4722 if (stcb->asoc.prsctp_supported == 1) {
4723 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
4724 if (stcb->asoc.idata_supported) {
4725 pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
4726 }
4727 }
4728 if (stcb->asoc.auth_supported == 1) {
4729 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
4730 }
4731 if (stcb->asoc.asconf_supported == 1) {
4732 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
4733 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
4734 }
4735 if (stcb->asoc.reconfig_supported == 1) {
4736 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
4737 }
4738 if (stcb->asoc.idata_supported) {
4739 pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
4740 }
4741 if (stcb->asoc.nrsack_supported == 1) {
4742 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
4743 }
4744 if (stcb->asoc.pktdrop_supported == 1) {
4745 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
4746 }
4747 if (num_ext > 0) {
4748 parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
4749 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
4750 pr_supported->ph.param_length = htons(parameter_len);
4751 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4752 chunk_len += parameter_len;
4753 }
4754 /* add authentication parameters */
4755 if (stcb->asoc.auth_supported) {
4756 /* attach RANDOM parameter, if available */
4757 if (stcb->asoc.authinfo.random != NULL) {
4758 struct sctp_auth_random *randp;
4759
4760 if (padding_len > 0) {
4761 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4762 chunk_len += padding_len;
4763 padding_len = 0;
4764 }
4765 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
4766 parameter_len = (uint16_t)sizeof(struct sctp_auth_random) + stcb->asoc.authinfo.random_len;
4767 /* random key already contains the header */
4768 memcpy(randp, stcb->asoc.authinfo.random->key, parameter_len);
4769 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4770 chunk_len += parameter_len;
4771 }
4772 /* add HMAC_ALGO parameter */
4773 if (stcb->asoc.local_hmacs != NULL) {
4774 struct sctp_auth_hmac_algo *hmacs;
4775
4776 if (padding_len > 0) {
4777 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4778 chunk_len += padding_len;
4779 padding_len = 0;
4780 }
4781 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
4782 parameter_len = (uint16_t)(sizeof(struct sctp_auth_hmac_algo) +
4783 stcb->asoc.local_hmacs->num_algo * sizeof(uint16_t));
4784 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
4785 hmacs->ph.param_length = htons(parameter_len);
4786 sctp_serialize_hmaclist(stcb->asoc.local_hmacs, (uint8_t *)hmacs->hmac_ids);
4787 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4788 chunk_len += parameter_len;
4789 }
4790 /* add CHUNKS parameter */
4791 if (stcb->asoc.local_auth_chunks != NULL) {
4792 struct sctp_auth_chunk_list *chunks;
4793
4794 if (padding_len > 0) {
4795 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4796 chunk_len += padding_len;
4797 padding_len = 0;
4798 }
4799 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
4800 parameter_len = (uint16_t)(sizeof(struct sctp_auth_chunk_list) +
4801 sctp_auth_get_chklist_size(stcb->asoc.local_auth_chunks));
4802 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
4803 chunks->ph.param_length = htons(parameter_len);
4804 sctp_serialize_auth_chunks(stcb->asoc.local_auth_chunks, chunks->chunk_types);
4805 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
4806 chunk_len += parameter_len;
4807 }
4808 }
4809
4810 /* now any cookie time extensions */
4811 if (stcb->asoc.cookie_preserve_req > 0) {
4812 struct sctp_cookie_perserve_param *cookie_preserve;
4813
4814 if (padding_len > 0) {
4815 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4816 chunk_len += padding_len;
4817 padding_len = 0;
4818 }
4819 parameter_len = (uint16_t)sizeof(struct sctp_cookie_perserve_param);
4820 cookie_preserve = (struct sctp_cookie_perserve_param *)(mtod(m, caddr_t)+chunk_len);
4821 cookie_preserve->ph.param_type = htons(SCTP_COOKIE_PRESERVE);
4822 cookie_preserve->ph.param_length = htons(parameter_len);
4823 cookie_preserve->time = htonl(stcb->asoc.cookie_preserve_req);
4824 stcb->asoc.cookie_preserve_req = 0;
4825 chunk_len += parameter_len;
4826 }
4827
4828 if (stcb->asoc.scope.ipv4_addr_legal || stcb->asoc.scope.ipv6_addr_legal) {
4829 uint8_t i;
4830
4831 if (padding_len > 0) {
4832 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
4833 chunk_len += padding_len;
4834 padding_len = 0;
4835 }
4836 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
4837 if (stcb->asoc.scope.ipv4_addr_legal) {
4838 parameter_len += (uint16_t)sizeof(uint16_t);
4839 }
4840 if (stcb->asoc.scope.ipv6_addr_legal) {
4841 parameter_len += (uint16_t)sizeof(uint16_t);
4842 }
4843 sup_addr = (struct sctp_supported_addr_param *)(mtod(m, caddr_t)+chunk_len);
4844 sup_addr->ph.param_type = htons(SCTP_SUPPORTED_ADDRTYPE);
4845 sup_addr->ph.param_length = htons(parameter_len);
4846 i = 0;
4847 if (stcb->asoc.scope.ipv4_addr_legal) {
4848 sup_addr->addr_type[i++] = htons(SCTP_IPV4_ADDRESS);
4849 }
4850 if (stcb->asoc.scope.ipv6_addr_legal) {
4851 sup_addr->addr_type[i++] = htons(SCTP_IPV6_ADDRESS);
4852 }
4853 padding_len = 4 - 2 * i;
4854 chunk_len += parameter_len;
4855 }
4856
4857 SCTP_BUF_LEN(m) = chunk_len;
4858 /* now the addresses */
4859 /*
4860 * To optimize this we could put the scoping stuff into a structure
4861 * and remove the individual uint8's from the assoc structure. Then
4862 * we could just sifa in the address within the stcb. But for now
4863 * this is a quick hack to get the address stuff teased apart.
4864 */
4865 m_last = sctp_add_addresses_to_i_ia(inp, stcb, &stcb->asoc.scope,
4866 m, cnt_inits_to,
4867 &padding_len, &chunk_len);
4868
4869 init->ch.chunk_length = htons(chunk_len);
4870 if (padding_len > 0) {
4871 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
4872 sctp_m_freem(m);
4873 return;
4874 }
4875 }
4876 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Sending INIT - calls lowlevel_output\n");
4877 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
4878 (struct sockaddr *)&net->ro._l_addr,
4879 m, 0, NULL, 0, 0, 0, 0,
4880 inp->sctp_lport, stcb->rport, htonl(0),
4881 net->port, NULL,
4882 0, 0,
4883 so_locked))) {
4884 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
4885 if (error == ENOBUFS) {
4886 stcb->asoc.ifp_had_enobuf = 1;
4887 SCTP_STAT_INCR(sctps_lowlevelerr);
4888 }
4889 } else {
4890 stcb->asoc.ifp_had_enobuf = 0;
4891 }
4892 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
4893 (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time);
4894 }
4895
4896 struct mbuf *
sctp_arethere_unrecognized_parameters(struct mbuf * in_initpkt,int param_offset,int * abort_processing,struct sctp_chunkhdr * cp,int * nat_friendly,int * cookie_found)4897 sctp_arethere_unrecognized_parameters(struct mbuf *in_initpkt,
4898 int param_offset, int *abort_processing,
4899 struct sctp_chunkhdr *cp,
4900 int *nat_friendly,
4901 int *cookie_found)
4902 {
4903 /*
4904 * Given a mbuf containing an INIT or INIT-ACK with the param_offset
4905 * being equal to the beginning of the params i.e. (iphlen +
4906 * sizeof(struct sctp_init_msg) parse through the parameters to the
4907 * end of the mbuf verifying that all parameters are known.
4908 *
4909 * For unknown parameters build and return a mbuf with
4910 * UNRECOGNIZED_PARAMETER errors. If the flags indicate to stop
4911 * processing this chunk stop, and set *abort_processing to 1.
4912 *
4913 * By having param_offset be pre-set to where parameters begin it is
4914 * hoped that this routine may be reused in the future by new
4915 * features.
4916 */
4917 struct sctp_paramhdr *phdr, params;
4918
4919 struct mbuf *mat, *m_tmp, *op_err, *op_err_last;
4920 int at, limit, pad_needed;
4921 uint16_t ptype, plen, padded_size;
4922
4923 *abort_processing = 0;
4924 if (cookie_found != NULL) {
4925 *cookie_found = 0;
4926 }
4927 mat = in_initpkt;
4928 limit = ntohs(cp->chunk_length) - sizeof(struct sctp_init_chunk);
4929 at = param_offset;
4930 op_err = NULL;
4931 op_err_last = NULL;
4932 pad_needed = 0;
4933 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Check for unrecognized param's\n");
4934 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
4935 while ((phdr != NULL) && ((size_t)limit >= sizeof(struct sctp_paramhdr))) {
4936 ptype = ntohs(phdr->param_type);
4937 plen = ntohs(phdr->param_length);
4938 if ((plen > limit) || (plen < sizeof(struct sctp_paramhdr))) {
4939 /* wacked parameter */
4940 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error %d\n", plen);
4941 goto invalid_size;
4942 }
4943 limit -= SCTP_SIZE32(plen);
4944 /*-
4945 * All parameters for all chunks that we know/understand are
4946 * listed here. We process them other places and make
4947 * appropriate stop actions per the upper bits. However this
4948 * is the generic routine processor's can call to get back
4949 * an operr.. to either incorporate (init-ack) or send.
4950 */
4951 padded_size = SCTP_SIZE32(plen);
4952 switch (ptype) {
4953 /* Param's with variable size */
4954 case SCTP_HEARTBEAT_INFO:
4955 case SCTP_UNRECOG_PARAM:
4956 case SCTP_ERROR_CAUSE_IND:
4957 /* ok skip fwd */
4958 at += padded_size;
4959 break;
4960 case SCTP_STATE_COOKIE:
4961 if (cookie_found != NULL) {
4962 *cookie_found = 1;
4963 }
4964 at += padded_size;
4965 break;
4966 /* Param's with variable size within a range */
4967 case SCTP_CHUNK_LIST:
4968 case SCTP_SUPPORTED_CHUNK_EXT:
4969 if (padded_size > (sizeof(struct sctp_supported_chunk_types_param) + (sizeof(uint8_t) * SCTP_MAX_SUPPORTED_EXT))) {
4970 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error chklist %d\n", plen);
4971 goto invalid_size;
4972 }
4973 at += padded_size;
4974 break;
4975 case SCTP_SUPPORTED_ADDRTYPE:
4976 if (padded_size > SCTP_MAX_ADDR_PARAMS_SIZE) {
4977 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error supaddrtype %d\n", plen);
4978 goto invalid_size;
4979 }
4980 at += padded_size;
4981 break;
4982 case SCTP_RANDOM:
4983 if (padded_size > (sizeof(struct sctp_auth_random) + SCTP_RANDOM_MAX_SIZE)) {
4984 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error random %d\n", plen);
4985 goto invalid_size;
4986 }
4987 at += padded_size;
4988 break;
4989 case SCTP_SET_PRIM_ADDR:
4990 case SCTP_DEL_IP_ADDRESS:
4991 case SCTP_ADD_IP_ADDRESS:
4992 if ((padded_size != sizeof(struct sctp_asconf_addrv4_param)) &&
4993 (padded_size != sizeof(struct sctp_asconf_addr_param))) {
4994 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error setprim %d\n", plen);
4995 goto invalid_size;
4996 }
4997 at += padded_size;
4998 break;
4999 /* Param's with a fixed size */
5000 case SCTP_IPV4_ADDRESS:
5001 if (padded_size != sizeof(struct sctp_ipv4addr_param)) {
5002 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv4 addr %d\n", plen);
5003 goto invalid_size;
5004 }
5005 at += padded_size;
5006 break;
5007 case SCTP_IPV6_ADDRESS:
5008 if (padded_size != sizeof(struct sctp_ipv6addr_param)) {
5009 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ipv6 addr %d\n", plen);
5010 goto invalid_size;
5011 }
5012 at += padded_size;
5013 break;
5014 case SCTP_COOKIE_PRESERVE:
5015 if (padded_size != sizeof(struct sctp_cookie_perserve_param)) {
5016 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error cookie-preserve %d\n", plen);
5017 goto invalid_size;
5018 }
5019 at += padded_size;
5020 break;
5021 case SCTP_HAS_NAT_SUPPORT:
5022 if (padded_size != sizeof(struct sctp_paramhdr)) {
5023 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error nat support %d\n", plen);
5024 goto invalid_size;
5025 }
5026 *nat_friendly = 1;
5027 at += padded_size;
5028 break;
5029 case SCTP_PRSCTP_SUPPORTED:
5030 if (padded_size != sizeof(struct sctp_paramhdr)) {
5031 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error prsctp %d\n", plen);
5032 goto invalid_size;
5033 }
5034 at += padded_size;
5035 break;
5036 case SCTP_ECN_CAPABLE:
5037 if (padded_size != sizeof(struct sctp_paramhdr)) {
5038 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error ecn %d\n", plen);
5039 goto invalid_size;
5040 }
5041 at += padded_size;
5042 break;
5043 case SCTP_ULP_ADAPTATION:
5044 if (padded_size != sizeof(struct sctp_adaptation_layer_indication)) {
5045 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error adapatation %d\n", plen);
5046 goto invalid_size;
5047 }
5048 at += padded_size;
5049 break;
5050 case SCTP_SUCCESS_REPORT:
5051 if (padded_size != sizeof(struct sctp_asconf_paramhdr)) {
5052 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Invalid size - error success %d\n", plen);
5053 goto invalid_size;
5054 }
5055 at += padded_size;
5056 break;
5057 case SCTP_HOSTNAME_ADDRESS:
5058 {
5059 /* Hostname parameters are deprecated. */
5060 struct sctp_gen_error_cause *cause;
5061 int l_len;
5062
5063 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Can't handle hostname addresses.. abort processing\n");
5064 *abort_processing = 1;
5065 sctp_m_freem(op_err);
5066 op_err = NULL;
5067 op_err_last = NULL;
5068 #ifdef INET6
5069 l_len = SCTP_MIN_OVERHEAD;
5070 #else
5071 l_len = SCTP_MIN_V4_OVERHEAD;
5072 #endif
5073 l_len += sizeof(struct sctp_chunkhdr);
5074 l_len += sizeof(struct sctp_gen_error_cause);
5075 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5076 if (op_err != NULL) {
5077 /*
5078 * Pre-reserve space for IP, SCTP,
5079 * and chunk header.
5080 */
5081 #ifdef INET6
5082 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5083 #else
5084 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5085 #endif
5086 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5087 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5088 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_gen_error_cause);
5089 cause = mtod(op_err, struct sctp_gen_error_cause *);
5090 cause->code = htons(SCTP_CAUSE_UNRESOLVABLE_ADDR);
5091 cause->length = htons((uint16_t)(sizeof(struct sctp_gen_error_cause) + plen));
5092 SCTP_BUF_NEXT(op_err) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5093 if (SCTP_BUF_NEXT(op_err) == NULL) {
5094 sctp_m_freem(op_err);
5095 op_err = NULL;
5096 op_err_last = NULL;
5097 }
5098 }
5099 return (op_err);
5100 }
5101 default:
5102 /*
5103 * we do not recognize the parameter figure out what
5104 * we do.
5105 */
5106 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Hit default param %x\n", ptype);
5107 if ((ptype & 0x4000) == 0x4000) {
5108 /* Report bit is set?? */
5109 SCTPDBG(SCTP_DEBUG_OUTPUT1, "report op err\n");
5110 if (op_err == NULL) {
5111 int l_len;
5112
5113 /* Ok need to try to get an mbuf */
5114 #ifdef INET6
5115 l_len = SCTP_MIN_OVERHEAD;
5116 #else
5117 l_len = SCTP_MIN_V4_OVERHEAD;
5118 #endif
5119 l_len += sizeof(struct sctp_chunkhdr);
5120 l_len += sizeof(struct sctp_paramhdr);
5121 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5122 if (op_err) {
5123 SCTP_BUF_LEN(op_err) = 0;
5124 #ifdef INET6
5125 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5126 #else
5127 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5128 #endif
5129 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5130 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5131 op_err_last = op_err;
5132 }
5133 }
5134 if (op_err != NULL) {
5135 /* If we have space */
5136 struct sctp_paramhdr *param;
5137
5138 if (pad_needed > 0) {
5139 op_err_last = sctp_add_pad_tombuf(op_err_last, pad_needed);
5140 }
5141 if (op_err_last == NULL) {
5142 sctp_m_freem(op_err);
5143 op_err = NULL;
5144 op_err_last = NULL;
5145 goto more_processing;
5146 }
5147 if (M_TRAILINGSPACE(op_err_last) < (int)sizeof(struct sctp_paramhdr)) {
5148 m_tmp = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
5149 if (m_tmp == NULL) {
5150 sctp_m_freem(op_err);
5151 op_err = NULL;
5152 op_err_last = NULL;
5153 goto more_processing;
5154 }
5155 SCTP_BUF_LEN(m_tmp) = 0;
5156 SCTP_BUF_NEXT(m_tmp) = NULL;
5157 SCTP_BUF_NEXT(op_err_last) = m_tmp;
5158 op_err_last = m_tmp;
5159 }
5160 param = (struct sctp_paramhdr *)(mtod(op_err_last, caddr_t)+SCTP_BUF_LEN(op_err_last));
5161 param->param_type = htons(SCTP_UNRECOG_PARAM);
5162 param->param_length = htons((uint16_t)sizeof(struct sctp_paramhdr) + plen);
5163 SCTP_BUF_LEN(op_err_last) += sizeof(struct sctp_paramhdr);
5164 SCTP_BUF_NEXT(op_err_last) = SCTP_M_COPYM(mat, at, plen, M_NOWAIT);
5165 if (SCTP_BUF_NEXT(op_err_last) == NULL) {
5166 sctp_m_freem(op_err);
5167 op_err = NULL;
5168 op_err_last = NULL;
5169 goto more_processing;
5170 } else {
5171 while (SCTP_BUF_NEXT(op_err_last) != NULL) {
5172 op_err_last = SCTP_BUF_NEXT(op_err_last);
5173 }
5174 }
5175 if (plen % 4 != 0) {
5176 pad_needed = 4 - (plen % 4);
5177 } else {
5178 pad_needed = 0;
5179 }
5180 }
5181 }
5182 more_processing:
5183 if ((ptype & 0x8000) == 0x0000) {
5184 SCTPDBG(SCTP_DEBUG_OUTPUT1, "stop proc\n");
5185 return (op_err);
5186 } else {
5187 /* skip this chunk and continue processing */
5188 SCTPDBG(SCTP_DEBUG_OUTPUT1, "move on\n");
5189 at += SCTP_SIZE32(plen);
5190 }
5191 break;
5192 }
5193 phdr = sctp_get_next_param(mat, at, ¶ms, sizeof(params));
5194 }
5195 return (op_err);
5196 invalid_size:
5197 SCTPDBG(SCTP_DEBUG_OUTPUT1, "abort flag set\n");
5198 *abort_processing = 1;
5199 sctp_m_freem(op_err);
5200 op_err = NULL;
5201 op_err_last = NULL;
5202 if (phdr != NULL) {
5203 struct sctp_paramhdr *param;
5204 int l_len;
5205 #ifdef INET6
5206 l_len = SCTP_MIN_OVERHEAD;
5207 #else
5208 l_len = SCTP_MIN_V4_OVERHEAD;
5209 #endif
5210 l_len += sizeof(struct sctp_chunkhdr);
5211 l_len += (2 * sizeof(struct sctp_paramhdr));
5212 op_err = sctp_get_mbuf_for_msg(l_len, 0, M_NOWAIT, 1, MT_DATA);
5213 if (op_err) {
5214 SCTP_BUF_LEN(op_err) = 0;
5215 #ifdef INET6
5216 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip6_hdr));
5217 #else
5218 SCTP_BUF_RESV_UF(op_err, sizeof(struct ip));
5219 #endif
5220 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctphdr));
5221 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
5222 SCTP_BUF_LEN(op_err) = 2 * sizeof(struct sctp_paramhdr);
5223 param = mtod(op_err, struct sctp_paramhdr *);
5224 param->param_type = htons(SCTP_CAUSE_PROTOCOL_VIOLATION);
5225 param->param_length = htons(2 * sizeof(struct sctp_paramhdr));
5226 param++;
5227 param->param_type = htons(ptype);
5228 param->param_length = htons(plen);
5229 }
5230 }
5231 return (op_err);
5232 }
5233
5234 /*
5235 * Given a INIT chunk, look through the parameters to verify that there
5236 * are no new addresses.
5237 * Return true, if there is a new address or there is a problem parsing
5238 the parameters. Provide an optional error cause used when sending an ABORT.
5239 * Return false, if there are no new addresses and there is no problem in
5240 parameter processing.
5241 */
5242 static bool
sctp_are_there_new_addresses(struct sctp_association * asoc,struct mbuf * in_initpkt,int offset,int limit,struct sockaddr * src,struct mbuf ** op_err)5243 sctp_are_there_new_addresses(struct sctp_association *asoc,
5244 struct mbuf *in_initpkt, int offset, int limit, struct sockaddr *src,
5245 struct mbuf **op_err)
5246 {
5247 struct sockaddr *sa_touse;
5248 struct sockaddr *sa;
5249 struct sctp_paramhdr *phdr, params;
5250 struct sctp_nets *net;
5251 #ifdef INET
5252 struct sockaddr_in sin4, *sa4;
5253 #endif
5254 #ifdef INET6
5255 struct sockaddr_in6 sin6, *sa6;
5256 #endif
5257 uint16_t ptype, plen;
5258 bool fnd, check_src;
5259
5260 *op_err = NULL;
5261 #ifdef INET
5262 memset(&sin4, 0, sizeof(sin4));
5263 sin4.sin_family = AF_INET;
5264 sin4.sin_len = sizeof(sin4);
5265 #endif
5266 #ifdef INET6
5267 memset(&sin6, 0, sizeof(sin6));
5268 sin6.sin6_family = AF_INET6;
5269 sin6.sin6_len = sizeof(sin6);
5270 #endif
5271 /* First what about the src address of the pkt ? */
5272 check_src = false;
5273 switch (src->sa_family) {
5274 #ifdef INET
5275 case AF_INET:
5276 if (asoc->scope.ipv4_addr_legal) {
5277 check_src = true;
5278 }
5279 break;
5280 #endif
5281 #ifdef INET6
5282 case AF_INET6:
5283 if (asoc->scope.ipv6_addr_legal) {
5284 check_src = true;
5285 }
5286 break;
5287 #endif
5288 default:
5289 /* TSNH */
5290 break;
5291 }
5292 if (check_src) {
5293 fnd = false;
5294 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5295 sa = (struct sockaddr *)&net->ro._l_addr;
5296 if (sa->sa_family == src->sa_family) {
5297 #ifdef INET
5298 if (sa->sa_family == AF_INET) {
5299 struct sockaddr_in *src4;
5300
5301 sa4 = (struct sockaddr_in *)sa;
5302 src4 = (struct sockaddr_in *)src;
5303 if (sa4->sin_addr.s_addr == src4->sin_addr.s_addr) {
5304 fnd = true;
5305 break;
5306 }
5307 }
5308 #endif
5309 #ifdef INET6
5310 if (sa->sa_family == AF_INET6) {
5311 struct sockaddr_in6 *src6;
5312
5313 sa6 = (struct sockaddr_in6 *)sa;
5314 src6 = (struct sockaddr_in6 *)src;
5315 if (SCTP6_ARE_ADDR_EQUAL(sa6, src6)) {
5316 fnd = true;
5317 break;
5318 }
5319 }
5320 #endif
5321 }
5322 }
5323 if (!fnd) {
5324 /*
5325 * If sending an ABORT in case of an additional
5326 * address, don't use the new address error cause.
5327 * This looks no different than if no listener was
5328 * present.
5329 */
5330 *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5331 return (true);
5332 }
5333 }
5334 /* Ok so far lets munge through the rest of the packet */
5335 offset += sizeof(struct sctp_init_chunk);
5336 phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params));
5337 while (phdr) {
5338 sa_touse = NULL;
5339 ptype = ntohs(phdr->param_type);
5340 plen = ntohs(phdr->param_length);
5341 if (offset + plen > limit) {
5342 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Partial parameter");
5343 return (true);
5344 }
5345 if (plen < sizeof(struct sctp_paramhdr)) {
5346 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length too small");
5347 return (true);
5348 }
5349 switch (ptype) {
5350 #ifdef INET
5351 case SCTP_IPV4_ADDRESS:
5352 {
5353 struct sctp_ipv4addr_param *p4, p4_buf;
5354
5355 if (plen != sizeof(struct sctp_ipv4addr_param)) {
5356 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5357 return (true);
5358 }
5359 phdr = sctp_get_next_param(in_initpkt, offset,
5360 (struct sctp_paramhdr *)&p4_buf, sizeof(p4_buf));
5361 if (phdr == NULL) {
5362 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5363 return (true);
5364 }
5365 if (asoc->scope.ipv4_addr_legal) {
5366 p4 = (struct sctp_ipv4addr_param *)phdr;
5367 sin4.sin_addr.s_addr = p4->addr;
5368 sa_touse = (struct sockaddr *)&sin4;
5369 }
5370 break;
5371 }
5372 #endif
5373 #ifdef INET6
5374 case SCTP_IPV6_ADDRESS:
5375 {
5376 struct sctp_ipv6addr_param *p6, p6_buf;
5377
5378 if (plen != sizeof(struct sctp_ipv6addr_param)) {
5379 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "Parameter length illegal");
5380 return (true);
5381 }
5382 phdr = sctp_get_next_param(in_initpkt, offset,
5383 (struct sctp_paramhdr *)&p6_buf, sizeof(p6_buf));
5384 if (phdr == NULL) {
5385 *op_err = sctp_generate_cause(SCTP_CAUSE_PROTOCOL_VIOLATION, "");
5386 return (true);
5387 }
5388 if (asoc->scope.ipv6_addr_legal) {
5389 p6 = (struct sctp_ipv6addr_param *)phdr;
5390 memcpy((caddr_t)&sin6.sin6_addr, p6->addr,
5391 sizeof(p6->addr));
5392 sa_touse = (struct sockaddr *)&sin6;
5393 }
5394 break;
5395 }
5396 #endif
5397 default:
5398 sa_touse = NULL;
5399 break;
5400 }
5401 if (sa_touse) {
5402 /* ok, sa_touse points to one to check */
5403 fnd = false;
5404 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
5405 sa = (struct sockaddr *)&net->ro._l_addr;
5406 if (sa->sa_family != sa_touse->sa_family) {
5407 continue;
5408 }
5409 #ifdef INET
5410 if (sa->sa_family == AF_INET) {
5411 sa4 = (struct sockaddr_in *)sa;
5412 if (sa4->sin_addr.s_addr ==
5413 sin4.sin_addr.s_addr) {
5414 fnd = true;
5415 break;
5416 }
5417 }
5418 #endif
5419 #ifdef INET6
5420 if (sa->sa_family == AF_INET6) {
5421 sa6 = (struct sockaddr_in6 *)sa;
5422 if (SCTP6_ARE_ADDR_EQUAL(
5423 sa6, &sin6)) {
5424 fnd = true;
5425 break;
5426 }
5427 }
5428 #endif
5429 }
5430 if (!fnd) {
5431 /*
5432 * If sending an ABORT in case of an
5433 * additional address, don't use the new
5434 * address error cause. This looks no
5435 * different than if no listener was
5436 * present.
5437 */
5438 *op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code), "Address added");
5439 return (true);
5440 }
5441 }
5442 offset += SCTP_SIZE32(plen);
5443 if (offset >= limit) {
5444 break;
5445 }
5446 phdr = sctp_get_next_param(in_initpkt, offset, ¶ms, sizeof(params));
5447 }
5448 return (false);
5449 }
5450
5451 /*
5452 * Given a MBUF chain that was sent into us containing an INIT. Build a
5453 * INIT-ACK with COOKIE and send back. We assume that the in_initpkt has done
5454 * a pullup to include IPv6/4header, SCTP header and initial part of INIT
5455 * message (i.e. the struct sctp_init_msg).
5456 */
5457 void
sctp_send_initiate_ack(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_nets * src_net,struct mbuf * init_pkt,int iphlen,int offset,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,struct sctp_init_chunk * init_chk,uint8_t mflowtype,uint32_t mflowid,uint32_t vrf_id,uint16_t port)5458 sctp_send_initiate_ack(struct sctp_inpcb *inp, struct sctp_tcb *stcb,
5459 struct sctp_nets *src_net, struct mbuf *init_pkt,
5460 int iphlen, int offset,
5461 struct sockaddr *src, struct sockaddr *dst,
5462 struct sctphdr *sh, struct sctp_init_chunk *init_chk,
5463 uint8_t mflowtype, uint32_t mflowid,
5464 uint32_t vrf_id, uint16_t port)
5465 {
5466 struct sctp_association *asoc;
5467 struct mbuf *m, *m_tmp, *m_last, *m_cookie, *op_err;
5468 struct sctp_init_ack_chunk *initack;
5469 struct sctp_adaptation_layer_indication *ali;
5470 struct sctp_supported_chunk_types_param *pr_supported;
5471 struct sctp_paramhdr *ph;
5472 union sctp_sockstore *over_addr;
5473 struct sctp_scoping scp;
5474 struct timeval now;
5475 #ifdef INET
5476 struct sockaddr_in *dst4 = (struct sockaddr_in *)dst;
5477 struct sockaddr_in *src4 = (struct sockaddr_in *)src;
5478 struct sockaddr_in *sin;
5479 #endif
5480 #ifdef INET6
5481 struct sockaddr_in6 *dst6 = (struct sockaddr_in6 *)dst;
5482 struct sockaddr_in6 *src6 = (struct sockaddr_in6 *)src;
5483 struct sockaddr_in6 *sin6;
5484 #endif
5485 struct sockaddr *to;
5486 struct sctp_state_cookie stc;
5487 struct sctp_nets *net = NULL;
5488 uint8_t *signature = NULL;
5489 int cnt_inits_to = 0;
5490 uint16_t his_limit, i_want;
5491 int abort_flag;
5492 int nat_friendly = 0;
5493 int error;
5494 struct socket *so;
5495 uint16_t num_ext, chunk_len, padding_len, parameter_len;
5496
5497 if (stcb) {
5498 asoc = &stcb->asoc;
5499 } else {
5500 asoc = NULL;
5501 }
5502 if ((asoc != NULL) &&
5503 (SCTP_GET_STATE(stcb) != SCTP_STATE_COOKIE_WAIT)) {
5504 if (sctp_are_there_new_addresses(asoc, init_pkt, offset, offset + ntohs(init_chk->ch.chunk_length), src, &op_err)) {
5505 /*
5506 * new addresses, out of here in non-cookie-wait
5507 * states
5508 */
5509 sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5510 mflowtype, mflowid, inp->fibnum,
5511 vrf_id, port);
5512 return;
5513 }
5514 if (src_net != NULL && (src_net->port != port)) {
5515 /*
5516 * change of remote encapsulation port, out of here
5517 * in non-cookie-wait states
5518 *
5519 * Send an ABORT, without an specific error cause.
5520 * This looks no different than if no listener was
5521 * present.
5522 */
5523 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5524 "Remote encapsulation port changed");
5525 sctp_send_abort(init_pkt, iphlen, src, dst, sh, 0, op_err,
5526 mflowtype, mflowid, inp->fibnum,
5527 vrf_id, port);
5528 return;
5529 }
5530 }
5531 abort_flag = 0;
5532 op_err = sctp_arethere_unrecognized_parameters(init_pkt,
5533 (offset + sizeof(struct sctp_init_chunk)),
5534 &abort_flag,
5535 (struct sctp_chunkhdr *)init_chk,
5536 &nat_friendly, NULL);
5537 if (abort_flag) {
5538 do_a_abort:
5539 if (op_err == NULL) {
5540 char msg[SCTP_DIAG_INFO_LEN];
5541
5542 SCTP_SNPRINTF(msg, sizeof(msg), "%s:%d at %s", __FILE__, __LINE__, __func__);
5543 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
5544 msg);
5545 }
5546 sctp_send_abort(init_pkt, iphlen, src, dst, sh,
5547 init_chk->init.initiate_tag, op_err,
5548 mflowtype, mflowid, inp->fibnum,
5549 vrf_id, port);
5550 return;
5551 }
5552 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
5553 if (m == NULL) {
5554 /* No memory, INIT timer will re-attempt. */
5555 sctp_m_freem(op_err);
5556 return;
5557 }
5558 chunk_len = (uint16_t)sizeof(struct sctp_init_ack_chunk);
5559 padding_len = 0;
5560
5561 /*
5562 * We might not overwrite the identification[] completely and on
5563 * some platforms time_entered will contain some padding. Therefore
5564 * zero out the cookie to avoid putting uninitialized memory on the
5565 * wire.
5566 */
5567 memset(&stc, 0, sizeof(struct sctp_state_cookie));
5568
5569 /* the time I built cookie */
5570 (void)SCTP_GETTIME_TIMEVAL(&now);
5571 stc.time_entered.tv_sec = now.tv_sec;
5572 stc.time_entered.tv_usec = now.tv_usec;
5573
5574 /* populate any tie tags */
5575 if (asoc != NULL) {
5576 /* unlock before tag selections */
5577 stc.tie_tag_my_vtag = asoc->my_vtag_nonce;
5578 stc.tie_tag_peer_vtag = asoc->peer_vtag_nonce;
5579 stc.cookie_life = asoc->cookie_life;
5580 net = asoc->primary_destination;
5581 } else {
5582 stc.tie_tag_my_vtag = 0;
5583 stc.tie_tag_peer_vtag = 0;
5584 /* life I will award this cookie */
5585 stc.cookie_life = inp->sctp_ep.def_cookie_life;
5586 }
5587
5588 /* copy in the ports for later check */
5589 stc.myport = sh->dest_port;
5590 stc.peerport = sh->src_port;
5591
5592 /*
5593 * If we wanted to honor cookie life extensions, we would add to
5594 * stc.cookie_life. For now we should NOT honor any extension
5595 */
5596 stc.site_scope = stc.local_scope = stc.loopback_scope = 0;
5597 if (inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
5598 stc.ipv6_addr_legal = 1;
5599 if (SCTP_IPV6_V6ONLY(inp)) {
5600 stc.ipv4_addr_legal = 0;
5601 } else {
5602 stc.ipv4_addr_legal = 1;
5603 }
5604 } else {
5605 stc.ipv6_addr_legal = 0;
5606 stc.ipv4_addr_legal = 1;
5607 }
5608 stc.ipv4_scope = 0;
5609 if (net == NULL) {
5610 to = src;
5611 switch (dst->sa_family) {
5612 #ifdef INET
5613 case AF_INET:
5614 {
5615 /* lookup address */
5616 stc.address[0] = src4->sin_addr.s_addr;
5617 stc.address[1] = 0;
5618 stc.address[2] = 0;
5619 stc.address[3] = 0;
5620 stc.addr_type = SCTP_IPV4_ADDRESS;
5621 /* local from address */
5622 stc.laddress[0] = dst4->sin_addr.s_addr;
5623 stc.laddress[1] = 0;
5624 stc.laddress[2] = 0;
5625 stc.laddress[3] = 0;
5626 stc.laddr_type = SCTP_IPV4_ADDRESS;
5627 /* scope_id is only for v6 */
5628 stc.scope_id = 0;
5629 if ((IN4_ISPRIVATE_ADDRESS(&src4->sin_addr)) ||
5630 (IN4_ISPRIVATE_ADDRESS(&dst4->sin_addr))) {
5631 stc.ipv4_scope = 1;
5632 }
5633 /* Must use the address in this case */
5634 if (sctp_is_address_on_local_host(src, vrf_id)) {
5635 stc.loopback_scope = 1;
5636 stc.ipv4_scope = 1;
5637 stc.site_scope = 1;
5638 stc.local_scope = 0;
5639 }
5640 break;
5641 }
5642 #endif
5643 #ifdef INET6
5644 case AF_INET6:
5645 {
5646 stc.addr_type = SCTP_IPV6_ADDRESS;
5647 memcpy(&stc.address, &src6->sin6_addr, sizeof(struct in6_addr));
5648 stc.scope_id = ntohs(in6_getscope(&src6->sin6_addr));
5649 if (sctp_is_address_on_local_host(src, vrf_id)) {
5650 stc.loopback_scope = 1;
5651 stc.local_scope = 0;
5652 stc.site_scope = 1;
5653 stc.ipv4_scope = 1;
5654 } else if (IN6_IS_ADDR_LINKLOCAL(&src6->sin6_addr) ||
5655 IN6_IS_ADDR_LINKLOCAL(&dst6->sin6_addr)) {
5656 /*
5657 * If the new destination or source
5658 * is a LINK_LOCAL we must have
5659 * common both site and local scope.
5660 * Don't set local scope though
5661 * since we must depend on the
5662 * source to be added implicitly. We
5663 * cannot assure just because we
5664 * share one link that all links are
5665 * common.
5666 */
5667 stc.local_scope = 0;
5668 stc.site_scope = 1;
5669 stc.ipv4_scope = 1;
5670 /*
5671 * we start counting for the private
5672 * address stuff at 1. since the
5673 * link local we source from won't
5674 * show up in our scoped count.
5675 */
5676 cnt_inits_to = 1;
5677 /*
5678 * pull out the scope_id from
5679 * incoming pkt
5680 */
5681 } else if (IN6_IS_ADDR_SITELOCAL(&src6->sin6_addr) ||
5682 IN6_IS_ADDR_SITELOCAL(&dst6->sin6_addr)) {
5683 /*
5684 * If the new destination or source
5685 * is SITE_LOCAL then we must have
5686 * site scope in common.
5687 */
5688 stc.site_scope = 1;
5689 }
5690 memcpy(&stc.laddress, &dst6->sin6_addr, sizeof(struct in6_addr));
5691 stc.laddr_type = SCTP_IPV6_ADDRESS;
5692 break;
5693 }
5694 #endif
5695 default:
5696 /* TSNH */
5697 goto do_a_abort;
5698 break;
5699 }
5700 } else {
5701 /* set the scope per the existing tcb */
5702
5703 #ifdef INET6
5704 struct sctp_nets *lnet;
5705 #endif
5706
5707 stc.loopback_scope = asoc->scope.loopback_scope;
5708 stc.ipv4_scope = asoc->scope.ipv4_local_scope;
5709 stc.site_scope = asoc->scope.site_scope;
5710 stc.local_scope = asoc->scope.local_scope;
5711 #ifdef INET6
5712 /* Why do we not consider IPv4 LL addresses? */
5713 TAILQ_FOREACH(lnet, &asoc->nets, sctp_next) {
5714 if (lnet->ro._l_addr.sin6.sin6_family == AF_INET6) {
5715 if (IN6_IS_ADDR_LINKLOCAL(&lnet->ro._l_addr.sin6.sin6_addr)) {
5716 /*
5717 * if we have a LL address, start
5718 * counting at 1.
5719 */
5720 cnt_inits_to = 1;
5721 }
5722 }
5723 }
5724 #endif
5725 /* use the net pointer */
5726 to = (struct sockaddr *)&net->ro._l_addr;
5727 switch (to->sa_family) {
5728 #ifdef INET
5729 case AF_INET:
5730 sin = (struct sockaddr_in *)to;
5731 stc.address[0] = sin->sin_addr.s_addr;
5732 stc.address[1] = 0;
5733 stc.address[2] = 0;
5734 stc.address[3] = 0;
5735 stc.addr_type = SCTP_IPV4_ADDRESS;
5736 if (net->src_addr_selected == 0) {
5737 /*
5738 * strange case here, the INIT should have
5739 * did the selection.
5740 */
5741 net->ro._s_addr = sctp_source_address_selection(inp,
5742 stcb, (sctp_route_t *)&net->ro,
5743 net, 0, vrf_id);
5744 if (net->ro._s_addr == NULL) {
5745 sctp_m_freem(op_err);
5746 sctp_m_freem(m);
5747 return;
5748 }
5749
5750 net->src_addr_selected = 1;
5751 }
5752 stc.laddress[0] = net->ro._s_addr->address.sin.sin_addr.s_addr;
5753 stc.laddress[1] = 0;
5754 stc.laddress[2] = 0;
5755 stc.laddress[3] = 0;
5756 stc.laddr_type = SCTP_IPV4_ADDRESS;
5757 /* scope_id is only for v6 */
5758 stc.scope_id = 0;
5759 break;
5760 #endif
5761 #ifdef INET6
5762 case AF_INET6:
5763 sin6 = (struct sockaddr_in6 *)to;
5764 memcpy(&stc.address, &sin6->sin6_addr,
5765 sizeof(struct in6_addr));
5766 stc.addr_type = SCTP_IPV6_ADDRESS;
5767 stc.scope_id = sin6->sin6_scope_id;
5768 if (net->src_addr_selected == 0) {
5769 /*
5770 * strange case here, the INIT should have
5771 * done the selection.
5772 */
5773 net->ro._s_addr = sctp_source_address_selection(inp,
5774 stcb, (sctp_route_t *)&net->ro,
5775 net, 0, vrf_id);
5776 if (net->ro._s_addr == NULL) {
5777 sctp_m_freem(op_err);
5778 sctp_m_freem(m);
5779 return;
5780 }
5781
5782 net->src_addr_selected = 1;
5783 }
5784 memcpy(&stc.laddress, &net->ro._s_addr->address.sin6.sin6_addr,
5785 sizeof(struct in6_addr));
5786 stc.laddr_type = SCTP_IPV6_ADDRESS;
5787 break;
5788 #endif
5789 }
5790 }
5791 /* Now lets put the SCTP header in place */
5792 initack = mtod(m, struct sctp_init_ack_chunk *);
5793 /* Save it off for quick ref */
5794 stc.peers_vtag = ntohl(init_chk->init.initiate_tag);
5795 /* who are we */
5796 memcpy(stc.identification, SCTP_VERSION_STRING,
5797 min(strlen(SCTP_VERSION_STRING), sizeof(stc.identification)));
5798 memset(stc.reserved, 0, SCTP_RESERVE_SPACE);
5799 /* now the chunk header */
5800 initack->ch.chunk_type = SCTP_INITIATION_ACK;
5801 initack->ch.chunk_flags = 0;
5802 /* fill in later from mbuf we build */
5803 initack->ch.chunk_length = 0;
5804 /* place in my tag */
5805 if ((asoc != NULL) &&
5806 ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
5807 (SCTP_GET_STATE(stcb) == SCTP_STATE_INUSE) ||
5808 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED))) {
5809 /* re-use the v-tags and init-seq here */
5810 initack->init.initiate_tag = htonl(asoc->my_vtag);
5811 initack->init.initial_tsn = htonl(asoc->init_seq_number);
5812 } else {
5813 uint32_t vtag, itsn;
5814
5815 if (asoc) {
5816 atomic_add_int(&asoc->refcnt, 1);
5817 SCTP_TCB_UNLOCK(stcb);
5818 new_tag:
5819 SCTP_INP_INFO_RLOCK();
5820 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5821 SCTP_INP_INFO_RUNLOCK();
5822 if ((asoc->peer_supports_nat) && (vtag == asoc->my_vtag)) {
5823 /*
5824 * Got a duplicate vtag on some guy behind a
5825 * nat make sure we don't use it.
5826 */
5827 goto new_tag;
5828 }
5829 initack->init.initiate_tag = htonl(vtag);
5830 /* get a TSN to use too */
5831 itsn = sctp_select_initial_TSN(&inp->sctp_ep);
5832 initack->init.initial_tsn = htonl(itsn);
5833 SCTP_TCB_LOCK(stcb);
5834 atomic_subtract_int(&asoc->refcnt, 1);
5835 } else {
5836 SCTP_INP_INCR_REF(inp);
5837 SCTP_INP_RUNLOCK(inp);
5838 SCTP_INP_INFO_RLOCK();
5839 vtag = sctp_select_a_tag(inp, inp->sctp_lport, sh->src_port, 1);
5840 SCTP_INP_INFO_RUNLOCK();
5841 initack->init.initiate_tag = htonl(vtag);
5842 /* get a TSN to use too */
5843 initack->init.initial_tsn = htonl(sctp_select_initial_TSN(&inp->sctp_ep));
5844 SCTP_INP_RLOCK(inp);
5845 SCTP_INP_DECR_REF(inp);
5846 }
5847 }
5848 /* save away my tag to */
5849 stc.my_vtag = initack->init.initiate_tag;
5850
5851 /* set up some of the credits. */
5852 so = inp->sctp_socket;
5853 if (so == NULL) {
5854 /* memory problem */
5855 sctp_m_freem(op_err);
5856 sctp_m_freem(m);
5857 return;
5858 } else {
5859 initack->init.a_rwnd = htonl(max(SCTP_SB_LIMIT_RCV(so), SCTP_MINIMAL_RWND));
5860 }
5861 /* set what I want */
5862 his_limit = ntohs(init_chk->init.num_inbound_streams);
5863 /* choose what I want */
5864 if (asoc != NULL) {
5865 if (asoc->streamoutcnt > asoc->pre_open_streams) {
5866 i_want = asoc->streamoutcnt;
5867 } else {
5868 i_want = asoc->pre_open_streams;
5869 }
5870 } else {
5871 i_want = inp->sctp_ep.pre_open_stream_count;
5872 }
5873 if (his_limit < i_want) {
5874 /* I Want more :< */
5875 initack->init.num_outbound_streams = init_chk->init.num_inbound_streams;
5876 } else {
5877 /* I can have what I want :> */
5878 initack->init.num_outbound_streams = htons(i_want);
5879 }
5880 /* tell him his limit. */
5881 initack->init.num_inbound_streams =
5882 htons(inp->sctp_ep.max_open_streams_intome);
5883
5884 /* adaptation layer indication parameter */
5885 if (inp->sctp_ep.adaptation_layer_indicator_provided) {
5886 parameter_len = (uint16_t)sizeof(struct sctp_adaptation_layer_indication);
5887 ali = (struct sctp_adaptation_layer_indication *)(mtod(m, caddr_t)+chunk_len);
5888 ali->ph.param_type = htons(SCTP_ULP_ADAPTATION);
5889 ali->ph.param_length = htons(parameter_len);
5890 ali->indication = htonl(inp->sctp_ep.adaptation_layer_indicator);
5891 chunk_len += parameter_len;
5892 }
5893
5894 /* ECN parameter */
5895 if (((asoc != NULL) && (asoc->ecn_supported == 1)) ||
5896 ((asoc == NULL) && (inp->ecn_supported == 1))) {
5897 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5898 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5899 ph->param_type = htons(SCTP_ECN_CAPABLE);
5900 ph->param_length = htons(parameter_len);
5901 chunk_len += parameter_len;
5902 }
5903
5904 /* PR-SCTP supported parameter */
5905 if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5906 ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5907 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5908 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5909 ph->param_type = htons(SCTP_PRSCTP_SUPPORTED);
5910 ph->param_length = htons(parameter_len);
5911 chunk_len += parameter_len;
5912 }
5913
5914 /* Add NAT friendly parameter */
5915 if (nat_friendly) {
5916 parameter_len = (uint16_t)sizeof(struct sctp_paramhdr);
5917 ph = (struct sctp_paramhdr *)(mtod(m, caddr_t)+chunk_len);
5918 ph->param_type = htons(SCTP_HAS_NAT_SUPPORT);
5919 ph->param_length = htons(parameter_len);
5920 chunk_len += parameter_len;
5921 }
5922
5923 /* And now tell the peer which extensions we support */
5924 num_ext = 0;
5925 pr_supported = (struct sctp_supported_chunk_types_param *)(mtod(m, caddr_t)+chunk_len);
5926 if (((asoc != NULL) && (asoc->prsctp_supported == 1)) ||
5927 ((asoc == NULL) && (inp->prsctp_supported == 1))) {
5928 pr_supported->chunk_types[num_ext++] = SCTP_FORWARD_CUM_TSN;
5929 if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5930 ((asoc == NULL) && (inp->idata_supported == 1))) {
5931 pr_supported->chunk_types[num_ext++] = SCTP_IFORWARD_CUM_TSN;
5932 }
5933 }
5934 if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
5935 ((asoc == NULL) && (inp->auth_supported == 1))) {
5936 pr_supported->chunk_types[num_ext++] = SCTP_AUTHENTICATION;
5937 }
5938 if (((asoc != NULL) && (asoc->asconf_supported == 1)) ||
5939 ((asoc == NULL) && (inp->asconf_supported == 1))) {
5940 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF;
5941 pr_supported->chunk_types[num_ext++] = SCTP_ASCONF_ACK;
5942 }
5943 if (((asoc != NULL) && (asoc->reconfig_supported == 1)) ||
5944 ((asoc == NULL) && (inp->reconfig_supported == 1))) {
5945 pr_supported->chunk_types[num_ext++] = SCTP_STREAM_RESET;
5946 }
5947 if (((asoc != NULL) && (asoc->idata_supported == 1)) ||
5948 ((asoc == NULL) && (inp->idata_supported == 1))) {
5949 pr_supported->chunk_types[num_ext++] = SCTP_IDATA;
5950 }
5951 if (((asoc != NULL) && (asoc->nrsack_supported == 1)) ||
5952 ((asoc == NULL) && (inp->nrsack_supported == 1))) {
5953 pr_supported->chunk_types[num_ext++] = SCTP_NR_SELECTIVE_ACK;
5954 }
5955 if (((asoc != NULL) && (asoc->pktdrop_supported == 1)) ||
5956 ((asoc == NULL) && (inp->pktdrop_supported == 1))) {
5957 pr_supported->chunk_types[num_ext++] = SCTP_PACKET_DROPPED;
5958 }
5959 if (num_ext > 0) {
5960 parameter_len = (uint16_t)sizeof(struct sctp_supported_chunk_types_param) + num_ext;
5961 pr_supported->ph.param_type = htons(SCTP_SUPPORTED_CHUNK_EXT);
5962 pr_supported->ph.param_length = htons(parameter_len);
5963 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5964 chunk_len += parameter_len;
5965 }
5966
5967 /* add authentication parameters */
5968 if (((asoc != NULL) && (asoc->auth_supported == 1)) ||
5969 ((asoc == NULL) && (inp->auth_supported == 1))) {
5970 struct sctp_auth_random *randp;
5971 struct sctp_auth_hmac_algo *hmacs;
5972 struct sctp_auth_chunk_list *chunks;
5973
5974 if (padding_len > 0) {
5975 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
5976 chunk_len += padding_len;
5977 padding_len = 0;
5978 }
5979 /* generate and add RANDOM parameter */
5980 randp = (struct sctp_auth_random *)(mtod(m, caddr_t)+chunk_len);
5981 parameter_len = (uint16_t)sizeof(struct sctp_auth_random) +
5982 SCTP_AUTH_RANDOM_SIZE_DEFAULT;
5983 randp->ph.param_type = htons(SCTP_RANDOM);
5984 randp->ph.param_length = htons(parameter_len);
5985 SCTP_READ_RANDOM(randp->random_data, SCTP_AUTH_RANDOM_SIZE_DEFAULT);
5986 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
5987 chunk_len += parameter_len;
5988
5989 if (padding_len > 0) {
5990 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
5991 chunk_len += padding_len;
5992 padding_len = 0;
5993 }
5994 /* add HMAC_ALGO parameter */
5995 hmacs = (struct sctp_auth_hmac_algo *)(mtod(m, caddr_t)+chunk_len);
5996 parameter_len = (uint16_t)sizeof(struct sctp_auth_hmac_algo) +
5997 sctp_serialize_hmaclist(inp->sctp_ep.local_hmacs,
5998 (uint8_t *)hmacs->hmac_ids);
5999 hmacs->ph.param_type = htons(SCTP_HMAC_LIST);
6000 hmacs->ph.param_length = htons(parameter_len);
6001 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6002 chunk_len += parameter_len;
6003
6004 if (padding_len > 0) {
6005 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6006 chunk_len += padding_len;
6007 padding_len = 0;
6008 }
6009 /* add CHUNKS parameter */
6010 chunks = (struct sctp_auth_chunk_list *)(mtod(m, caddr_t)+chunk_len);
6011 parameter_len = (uint16_t)sizeof(struct sctp_auth_chunk_list) +
6012 sctp_serialize_auth_chunks(inp->sctp_ep.local_auth_chunks,
6013 chunks->chunk_types);
6014 chunks->ph.param_type = htons(SCTP_CHUNK_LIST);
6015 chunks->ph.param_length = htons(parameter_len);
6016 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6017 chunk_len += parameter_len;
6018 }
6019 SCTP_BUF_LEN(m) = chunk_len;
6020 m_last = m;
6021 /* now the addresses */
6022 /*
6023 * To optimize this we could put the scoping stuff into a structure
6024 * and remove the individual uint8's from the stc structure. Then we
6025 * could just sifa in the address within the stc.. but for now this
6026 * is a quick hack to get the address stuff teased apart.
6027 */
6028 scp.ipv4_addr_legal = stc.ipv4_addr_legal;
6029 scp.ipv6_addr_legal = stc.ipv6_addr_legal;
6030 scp.loopback_scope = stc.loopback_scope;
6031 scp.ipv4_local_scope = stc.ipv4_scope;
6032 scp.local_scope = stc.local_scope;
6033 scp.site_scope = stc.site_scope;
6034 m_last = sctp_add_addresses_to_i_ia(inp, stcb, &scp, m_last,
6035 cnt_inits_to,
6036 &padding_len, &chunk_len);
6037 /* padding_len can only be positive, if no addresses have been added */
6038 if (padding_len > 0) {
6039 memset(mtod(m, caddr_t)+chunk_len, 0, padding_len);
6040 chunk_len += padding_len;
6041 SCTP_BUF_LEN(m) += padding_len;
6042 padding_len = 0;
6043 }
6044
6045 /* tack on the operational error if present */
6046 if (op_err) {
6047 parameter_len = 0;
6048 for (m_tmp = op_err; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6049 parameter_len += SCTP_BUF_LEN(m_tmp);
6050 }
6051 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6052 SCTP_BUF_NEXT(m_last) = op_err;
6053 while (SCTP_BUF_NEXT(m_last) != NULL) {
6054 m_last = SCTP_BUF_NEXT(m_last);
6055 }
6056 chunk_len += parameter_len;
6057 }
6058 if (padding_len > 0) {
6059 m_last = sctp_add_pad_tombuf(m_last, padding_len);
6060 if (m_last == NULL) {
6061 /* Houston we have a problem, no space */
6062 sctp_m_freem(m);
6063 return;
6064 }
6065 chunk_len += padding_len;
6066 padding_len = 0;
6067 }
6068 /* Now we must build a cookie */
6069 m_cookie = sctp_add_cookie(init_pkt, offset, m, 0, &stc, &signature);
6070 if (m_cookie == NULL) {
6071 /* memory problem */
6072 sctp_m_freem(m);
6073 return;
6074 }
6075 /* Now append the cookie to the end and update the space/size */
6076 SCTP_BUF_NEXT(m_last) = m_cookie;
6077 parameter_len = 0;
6078 for (m_tmp = m_cookie; m_tmp != NULL; m_tmp = SCTP_BUF_NEXT(m_tmp)) {
6079 parameter_len += SCTP_BUF_LEN(m_tmp);
6080 if (SCTP_BUF_NEXT(m_tmp) == NULL) {
6081 m_last = m_tmp;
6082 }
6083 }
6084 padding_len = SCTP_SIZE32(parameter_len) - parameter_len;
6085 chunk_len += parameter_len;
6086
6087 /*
6088 * Place in the size, but we don't include the last pad (if any) in
6089 * the INIT-ACK.
6090 */
6091 initack->ch.chunk_length = htons(chunk_len);
6092
6093 /*
6094 * Time to sign the cookie, we don't sign over the cookie signature
6095 * though thus we set trailer.
6096 */
6097 (void)sctp_hmac_m(SCTP_HMAC,
6098 (uint8_t *)inp->sctp_ep.secret_key[(int)(inp->sctp_ep.current_secret_number)],
6099 SCTP_SECRET_SIZE, m_cookie, sizeof(struct sctp_paramhdr),
6100 (uint8_t *)signature, SCTP_SIGNATURE_SIZE);
6101 /*
6102 * We sifa 0 here to NOT set IP_DF if its IPv4, we ignore the return
6103 * here since the timer will drive a retranmission.
6104 */
6105 if (padding_len > 0) {
6106 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
6107 sctp_m_freem(m);
6108 return;
6109 }
6110 }
6111 if (stc.loopback_scope) {
6112 over_addr = (union sctp_sockstore *)dst;
6113 } else {
6114 over_addr = NULL;
6115 }
6116
6117 if ((error = sctp_lowlevel_chunk_output(inp, NULL, NULL, to, m, 0, NULL, 0, 0,
6118 0, 0,
6119 inp->sctp_lport, sh->src_port, init_chk->init.initiate_tag,
6120 port, over_addr,
6121 mflowtype, mflowid,
6122 SCTP_SO_NOT_LOCKED))) {
6123 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak send error %d\n", error);
6124 if (error == ENOBUFS) {
6125 if (asoc != NULL) {
6126 asoc->ifp_had_enobuf = 1;
6127 }
6128 SCTP_STAT_INCR(sctps_lowlevelerr);
6129 }
6130 } else {
6131 if (asoc != NULL) {
6132 asoc->ifp_had_enobuf = 0;
6133 }
6134 }
6135 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
6136 }
6137
6138 static void
sctp_prune_prsctp(struct sctp_tcb * stcb,struct sctp_association * asoc,struct sctp_nonpad_sndrcvinfo * srcv,int dataout)6139 sctp_prune_prsctp(struct sctp_tcb *stcb,
6140 struct sctp_association *asoc,
6141 struct sctp_nonpad_sndrcvinfo *srcv,
6142 int dataout)
6143 {
6144 int freed_spc = 0;
6145 struct sctp_tmit_chunk *chk, *nchk;
6146
6147 SCTP_TCB_LOCK_ASSERT(stcb);
6148 if ((asoc->prsctp_supported) &&
6149 (asoc->sent_queue_cnt_removeable > 0)) {
6150 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
6151 /*
6152 * Look for chunks marked with the PR_SCTP flag AND
6153 * the buffer space flag. If the one being sent is
6154 * equal or greater priority then purge the old one
6155 * and free some space.
6156 */
6157 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6158 /*
6159 * This one is PR-SCTP AND buffer space
6160 * limited type
6161 */
6162 if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6163 /*
6164 * Lower numbers equates to higher
6165 * priority. So if the one we are
6166 * looking at has a larger priority,
6167 * we want to drop the data and NOT
6168 * retransmit it.
6169 */
6170 if (chk->data) {
6171 /*
6172 * We release the book_size
6173 * if the mbuf is here
6174 */
6175 int ret_spc;
6176 uint8_t sent;
6177
6178 if (chk->sent > SCTP_DATAGRAM_UNSENT)
6179 sent = 1;
6180 else
6181 sent = 0;
6182 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6183 sent,
6184 SCTP_SO_LOCKED);
6185 freed_spc += ret_spc;
6186 if (freed_spc >= dataout) {
6187 return;
6188 }
6189 } /* if chunk was present */
6190 } /* if of sufficient priority */
6191 } /* if chunk has enabled */
6192 } /* tailqforeach */
6193
6194 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
6195 /* Here we must move to the sent queue and mark */
6196 if (PR_SCTP_BUF_ENABLED(chk->flags)) {
6197 if (chk->rec.data.timetodrop.tv_sec > (long)srcv->sinfo_timetolive) {
6198 if (chk->data) {
6199 /*
6200 * We release the book_size
6201 * if the mbuf is here
6202 */
6203 int ret_spc;
6204
6205 ret_spc = sctp_release_pr_sctp_chunk(stcb, chk,
6206 0, SCTP_SO_LOCKED);
6207
6208 freed_spc += ret_spc;
6209 if (freed_spc >= dataout) {
6210 return;
6211 }
6212 } /* end if chk->data */
6213 } /* end if right class */
6214 } /* end if chk pr-sctp */
6215 } /* tailqforeachsafe (chk) */
6216 } /* if enabled in asoc */
6217 }
6218
6219 uint32_t
sctp_get_frag_point(struct sctp_tcb * stcb)6220 sctp_get_frag_point(struct sctp_tcb *stcb)
6221 {
6222 struct sctp_association *asoc;
6223 uint32_t frag_point, overhead;
6224
6225 asoc = &stcb->asoc;
6226 /* Consider IP header and SCTP common header. */
6227 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
6228 overhead = SCTP_MIN_OVERHEAD;
6229 } else {
6230 overhead = SCTP_MIN_V4_OVERHEAD;
6231 }
6232 /* Consider DATA/IDATA chunk header and AUTH header, if needed. */
6233 if (asoc->idata_supported) {
6234 overhead += sizeof(struct sctp_idata_chunk);
6235 if (sctp_auth_is_required_chunk(SCTP_IDATA, asoc->peer_auth_chunks)) {
6236 overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6237 }
6238 } else {
6239 overhead += sizeof(struct sctp_data_chunk);
6240 if (sctp_auth_is_required_chunk(SCTP_DATA, asoc->peer_auth_chunks)) {
6241 overhead += sctp_get_auth_chunk_len(asoc->peer_hmac_id);
6242 }
6243 }
6244 KASSERT(overhead % 4 == 0,
6245 ("overhead (%u) not a multiple of 4", overhead));
6246 /* Consider padding. */
6247 if (asoc->smallest_mtu % 4 > 0) {
6248 overhead += (asoc->smallest_mtu % 4);
6249 }
6250 KASSERT(asoc->smallest_mtu > overhead,
6251 ("Association MTU (%u) too small for overhead (%u)",
6252 asoc->smallest_mtu, overhead));
6253 frag_point = asoc->smallest_mtu - overhead;
6254 KASSERT(frag_point % 4 == 0,
6255 ("frag_point (%u) not a multiple of 4", frag_point));
6256 /* Honor MAXSEG socket option. */
6257 if ((asoc->sctp_frag_point > 0) &&
6258 (asoc->sctp_frag_point < frag_point)) {
6259 frag_point = asoc->sctp_frag_point;
6260 }
6261 return (frag_point);
6262 }
6263
6264 static void
sctp_set_prsctp_policy(struct sctp_stream_queue_pending * sp)6265 sctp_set_prsctp_policy(struct sctp_stream_queue_pending *sp)
6266 {
6267 /*
6268 * We assume that the user wants PR_SCTP_TTL if the user provides a
6269 * positive lifetime but does not specify any PR_SCTP policy.
6270 */
6271 if (PR_SCTP_ENABLED(sp->sinfo_flags)) {
6272 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6273 } else if (sp->timetolive > 0) {
6274 sp->sinfo_flags |= SCTP_PR_SCTP_TTL;
6275 sp->act_flags |= PR_SCTP_POLICY(sp->sinfo_flags);
6276 } else {
6277 return;
6278 }
6279 switch (PR_SCTP_POLICY(sp->sinfo_flags)) {
6280 case CHUNK_FLAGS_PR_SCTP_BUF:
6281 /*
6282 * Time to live is a priority stored in tv_sec when doing
6283 * the buffer drop thing.
6284 */
6285 sp->ts.tv_sec = sp->timetolive;
6286 sp->ts.tv_usec = 0;
6287 break;
6288 case CHUNK_FLAGS_PR_SCTP_TTL:
6289 {
6290 struct timeval tv;
6291
6292 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6293 tv.tv_sec = sp->timetolive / 1000;
6294 tv.tv_usec = (sp->timetolive * 1000) % 1000000;
6295 /*
6296 * TODO sctp_constants.h needs alternative time
6297 * macros when _KERNEL is undefined.
6298 */
6299 timevaladd(&sp->ts, &tv);
6300 }
6301 break;
6302 case CHUNK_FLAGS_PR_SCTP_RTX:
6303 /*
6304 * Time to live is a the number or retransmissions stored in
6305 * tv_sec.
6306 */
6307 sp->ts.tv_sec = sp->timetolive;
6308 sp->ts.tv_usec = 0;
6309 break;
6310 default:
6311 SCTPDBG(SCTP_DEBUG_USRREQ1,
6312 "Unknown PR_SCTP policy %u.\n",
6313 PR_SCTP_POLICY(sp->sinfo_flags));
6314 break;
6315 }
6316 }
6317
6318 static int
sctp_msg_append(struct sctp_tcb * stcb,struct sctp_nets * net,struct mbuf * m,struct sctp_nonpad_sndrcvinfo * srcv)6319 sctp_msg_append(struct sctp_tcb *stcb,
6320 struct sctp_nets *net,
6321 struct mbuf *m,
6322 struct sctp_nonpad_sndrcvinfo *srcv)
6323 {
6324 int error = 0;
6325 struct mbuf *at;
6326 struct sctp_stream_queue_pending *sp = NULL;
6327 struct sctp_stream_out *strm;
6328
6329 SCTP_TCB_LOCK_ASSERT(stcb);
6330
6331 /*
6332 * Given an mbuf chain, put it into the association send queue and
6333 * place it on the wheel
6334 */
6335 if (srcv->sinfo_stream >= stcb->asoc.streamoutcnt) {
6336 /* Invalid stream number */
6337 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6338 error = EINVAL;
6339 goto out_now;
6340 }
6341 if ((stcb->asoc.stream_locked) &&
6342 (stcb->asoc.stream_locked_on != srcv->sinfo_stream)) {
6343 SCTP_LTRACE_ERR_RET_PKT(m, NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, EINVAL);
6344 error = EINVAL;
6345 goto out_now;
6346 }
6347 if ((stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPEN) &&
6348 (stcb->asoc.strmout[srcv->sinfo_stream].state != SCTP_STREAM_OPENING)) {
6349 /*
6350 * Can't queue any data while stream reset is underway.
6351 */
6352 if (stcb->asoc.strmout[srcv->sinfo_stream].state > SCTP_STREAM_OPEN) {
6353 error = EAGAIN;
6354 } else {
6355 error = EINVAL;
6356 }
6357 goto out_now;
6358 }
6359 /* Now can we send this? */
6360 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
6361 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
6362 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
6363 (stcb->asoc.state & SCTP_STATE_SHUTDOWN_PENDING)) {
6364 /* got data while shutting down */
6365 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EPIPE);
6366 error = EPIPE;
6367 goto out_now;
6368 }
6369 sctp_alloc_a_strmoq(stcb, sp);
6370 if (sp == NULL) {
6371 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6372 error = ENOMEM;
6373 goto out_now;
6374 }
6375 sp->sinfo_flags = srcv->sinfo_flags;
6376 sp->timetolive = srcv->sinfo_timetolive;
6377 sp->ppid = srcv->sinfo_ppid;
6378 sp->context = srcv->sinfo_context;
6379 sp->fsn = 0;
6380 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
6381 sp->net = net;
6382 atomic_add_int(&sp->net->ref_count, 1);
6383 } else {
6384 sp->net = NULL;
6385 }
6386 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
6387 sp->sid = srcv->sinfo_stream;
6388 sp->msg_is_complete = 1;
6389 sp->sender_all_done = 1;
6390 sp->some_taken = 0;
6391 sp->data = m;
6392 sp->tail_mbuf = NULL;
6393 sctp_set_prsctp_policy(sp);
6394 /*
6395 * We could in theory (for sendall) sifa the length in, but we would
6396 * still have to hunt through the chain since we need to setup the
6397 * tail_mbuf
6398 */
6399 sp->length = 0;
6400 for (at = m; at; at = SCTP_BUF_NEXT(at)) {
6401 if (SCTP_BUF_NEXT(at) == NULL)
6402 sp->tail_mbuf = at;
6403 sp->length += SCTP_BUF_LEN(at);
6404 }
6405 if (srcv->sinfo_keynumber_valid) {
6406 sp->auth_keyid = srcv->sinfo_keynumber;
6407 } else {
6408 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
6409 }
6410 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
6411 sctp_auth_key_acquire(stcb, sp->auth_keyid);
6412 sp->holds_key_ref = 1;
6413 }
6414 strm = &stcb->asoc.strmout[srcv->sinfo_stream];
6415 sctp_snd_sb_alloc(stcb, sp->length);
6416 atomic_add_int(&stcb->asoc.stream_queue_cnt, 1);
6417 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
6418 stcb->asoc.ss_functions.sctp_ss_add_to_stream(stcb, &stcb->asoc, strm, sp);
6419 m = NULL;
6420 out_now:
6421 if (m) {
6422 sctp_m_freem(m);
6423 }
6424 return (error);
6425 }
6426
6427 static struct mbuf *
sctp_copy_mbufchain(struct mbuf * clonechain,struct mbuf * outchain,struct mbuf ** endofchain,int can_take_mbuf,int sizeofcpy,uint8_t copy_by_ref)6428 sctp_copy_mbufchain(struct mbuf *clonechain,
6429 struct mbuf *outchain,
6430 struct mbuf **endofchain,
6431 int can_take_mbuf,
6432 int sizeofcpy,
6433 uint8_t copy_by_ref)
6434 {
6435 struct mbuf *m;
6436 struct mbuf *appendchain;
6437 caddr_t cp;
6438 int len;
6439
6440 if (endofchain == NULL) {
6441 /* error */
6442 error_out:
6443 if (outchain)
6444 sctp_m_freem(outchain);
6445 return (NULL);
6446 }
6447 if (can_take_mbuf) {
6448 appendchain = clonechain;
6449 } else {
6450 if (!copy_by_ref &&
6451 (sizeofcpy <= (int)((((SCTP_BASE_SYSCTL(sctp_mbuf_threshold_count) - 1) * MLEN) + MHLEN)))) {
6452 /* Its not in a cluster */
6453 if (*endofchain == NULL) {
6454 /* lets get a mbuf cluster */
6455 if (outchain == NULL) {
6456 /* This is the general case */
6457 new_mbuf:
6458 outchain = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6459 if (outchain == NULL) {
6460 goto error_out;
6461 }
6462 SCTP_BUF_LEN(outchain) = 0;
6463 *endofchain = outchain;
6464 /* get the prepend space */
6465 SCTP_BUF_RESV_UF(outchain, (SCTP_FIRST_MBUF_RESV + 4));
6466 } else {
6467 /*
6468 * We really should not get a NULL
6469 * in endofchain
6470 */
6471 /* find end */
6472 m = outchain;
6473 while (m) {
6474 if (SCTP_BUF_NEXT(m) == NULL) {
6475 *endofchain = m;
6476 break;
6477 }
6478 m = SCTP_BUF_NEXT(m);
6479 }
6480 /* sanity */
6481 if (*endofchain == NULL) {
6482 /*
6483 * huh, TSNH XXX maybe we
6484 * should panic
6485 */
6486 sctp_m_freem(outchain);
6487 goto new_mbuf;
6488 }
6489 }
6490 /* get the new end of length */
6491 len = (int)M_TRAILINGSPACE(*endofchain);
6492 } else {
6493 /* how much is left at the end? */
6494 len = (int)M_TRAILINGSPACE(*endofchain);
6495 }
6496 /* Find the end of the data, for appending */
6497 cp = (mtod((*endofchain), caddr_t)+SCTP_BUF_LEN((*endofchain)));
6498
6499 /* Now lets copy it out */
6500 if (len >= sizeofcpy) {
6501 /* It all fits, copy it in */
6502 m_copydata(clonechain, 0, sizeofcpy, cp);
6503 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6504 } else {
6505 /* fill up the end of the chain */
6506 if (len > 0) {
6507 m_copydata(clonechain, 0, len, cp);
6508 SCTP_BUF_LEN((*endofchain)) += len;
6509 /* now we need another one */
6510 sizeofcpy -= len;
6511 }
6512 m = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_HEADER);
6513 if (m == NULL) {
6514 /* We failed */
6515 goto error_out;
6516 }
6517 SCTP_BUF_NEXT((*endofchain)) = m;
6518 *endofchain = m;
6519 cp = mtod((*endofchain), caddr_t);
6520 m_copydata(clonechain, len, sizeofcpy, cp);
6521 SCTP_BUF_LEN((*endofchain)) += sizeofcpy;
6522 }
6523 return (outchain);
6524 } else {
6525 /* copy the old fashion way */
6526 appendchain = SCTP_M_COPYM(clonechain, 0, M_COPYALL, M_NOWAIT);
6527 #ifdef SCTP_MBUF_LOGGING
6528 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6529 sctp_log_mbc(appendchain, SCTP_MBUF_ICOPY);
6530 }
6531 #endif
6532 }
6533 }
6534 if (appendchain == NULL) {
6535 /* error */
6536 if (outchain)
6537 sctp_m_freem(outchain);
6538 return (NULL);
6539 }
6540 if (outchain) {
6541 /* tack on to the end */
6542 if (*endofchain != NULL) {
6543 SCTP_BUF_NEXT(((*endofchain))) = appendchain;
6544 } else {
6545 m = outchain;
6546 while (m) {
6547 if (SCTP_BUF_NEXT(m) == NULL) {
6548 SCTP_BUF_NEXT(m) = appendchain;
6549 break;
6550 }
6551 m = SCTP_BUF_NEXT(m);
6552 }
6553 }
6554 /*
6555 * save off the end and update the end-chain position
6556 */
6557 m = appendchain;
6558 while (m) {
6559 if (SCTP_BUF_NEXT(m) == NULL) {
6560 *endofchain = m;
6561 break;
6562 }
6563 m = SCTP_BUF_NEXT(m);
6564 }
6565 return (outchain);
6566 } else {
6567 /* save off the end and update the end-chain position */
6568 m = appendchain;
6569 while (m) {
6570 if (SCTP_BUF_NEXT(m) == NULL) {
6571 *endofchain = m;
6572 break;
6573 }
6574 m = SCTP_BUF_NEXT(m);
6575 }
6576 return (appendchain);
6577 }
6578 }
6579
6580 static int
6581 sctp_med_chunk_output(struct sctp_inpcb *inp,
6582 struct sctp_tcb *stcb,
6583 struct sctp_association *asoc,
6584 int *num_out,
6585 int *reason_code,
6586 int control_only, int from_where,
6587 struct timeval *now, int *now_filled,
6588 uint32_t frag_point, int so_locked);
6589
6590 static void
sctp_sendall_iterator(struct sctp_inpcb * inp,struct sctp_tcb * stcb,void * ptr,uint32_t val SCTP_UNUSED)6591 sctp_sendall_iterator(struct sctp_inpcb *inp, struct sctp_tcb *stcb, void *ptr,
6592 uint32_t val SCTP_UNUSED)
6593 {
6594 struct sctp_copy_all *ca;
6595 struct mbuf *m;
6596 int ret = 0;
6597 int added_control = 0;
6598 int un_sent, do_chunk_output = 1;
6599 struct sctp_association *asoc;
6600 struct sctp_nets *net;
6601
6602 ca = (struct sctp_copy_all *)ptr;
6603 if (ca->m == NULL) {
6604 return;
6605 }
6606 if (ca->inp != inp) {
6607 /* TSNH */
6608 return;
6609 }
6610 if (ca->sndlen > 0) {
6611 m = SCTP_M_COPYM(ca->m, 0, M_COPYALL, M_NOWAIT);
6612 if (m == NULL) {
6613 /* can't copy so we are done */
6614 ca->cnt_failed++;
6615 return;
6616 }
6617 #ifdef SCTP_MBUF_LOGGING
6618 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
6619 sctp_log_mbc(m, SCTP_MBUF_ICOPY);
6620 }
6621 #endif
6622 } else {
6623 m = NULL;
6624 }
6625 SCTP_TCB_LOCK_ASSERT(stcb);
6626 if (stcb->asoc.alternate) {
6627 net = stcb->asoc.alternate;
6628 } else {
6629 net = stcb->asoc.primary_destination;
6630 }
6631 if (ca->sndrcv.sinfo_flags & SCTP_ABORT) {
6632 /* Abort this assoc with m as the user defined reason */
6633 if (m != NULL) {
6634 SCTP_BUF_PREPEND(m, sizeof(struct sctp_paramhdr), M_NOWAIT);
6635 } else {
6636 m = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr),
6637 0, M_NOWAIT, 1, MT_DATA);
6638 if (m != NULL) {
6639 SCTP_BUF_LEN(m) = sizeof(struct sctp_paramhdr);
6640 }
6641 }
6642 if (m != NULL) {
6643 struct sctp_paramhdr *ph;
6644
6645 ph = mtod(m, struct sctp_paramhdr *);
6646 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
6647 ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + ca->sndlen));
6648 }
6649 /*
6650 * We add one here to keep the assoc from dis-appearing on
6651 * us.
6652 */
6653 atomic_add_int(&stcb->asoc.refcnt, 1);
6654 sctp_abort_an_association(inp, stcb, m, false, SCTP_SO_NOT_LOCKED);
6655 /*
6656 * sctp_abort_an_association calls sctp_free_asoc() free
6657 * association will NOT free it since we incremented the
6658 * refcnt .. we do this to prevent it being freed and things
6659 * getting tricky since we could end up (from free_asoc)
6660 * calling inpcb_free which would get a recursive lock call
6661 * to the iterator lock.. But as a consequence of that the
6662 * stcb will return to us un-locked.. since free_asoc
6663 * returns with either no TCB or the TCB unlocked, we must
6664 * relock.. to unlock in the iterator timer :-0
6665 */
6666 SCTP_TCB_LOCK(stcb);
6667 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6668 goto no_chunk_output;
6669 } else {
6670 if (m != NULL) {
6671 ret = sctp_msg_append(stcb, net, m, &ca->sndrcv);
6672 }
6673 asoc = &stcb->asoc;
6674 if (ca->sndrcv.sinfo_flags & SCTP_EOF) {
6675 /* shutdown this assoc */
6676 if (TAILQ_EMPTY(&asoc->send_queue) &&
6677 TAILQ_EMPTY(&asoc->sent_queue) &&
6678 sctp_is_there_unsent_data(stcb, SCTP_SO_NOT_LOCKED) == 0) {
6679 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6680 goto abort_anyway;
6681 }
6682 /*
6683 * there is nothing queued to send, so I'm
6684 * done...
6685 */
6686 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6687 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6688 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6689 /*
6690 * only send SHUTDOWN the first time
6691 * through
6692 */
6693 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
6694 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
6695 }
6696 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
6697 sctp_stop_timers_for_shutdown(stcb);
6698 sctp_send_shutdown(stcb, net);
6699 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
6700 net);
6701 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
6702 NULL);
6703 added_control = 1;
6704 do_chunk_output = 0;
6705 }
6706 } else {
6707 /*
6708 * we still got (or just got) data to send,
6709 * so set SHUTDOWN_PENDING
6710 */
6711 /*
6712 * XXX sockets draft says that SCTP_EOF
6713 * should be sent with no data. currently,
6714 * we will allow user data to be sent first
6715 * and move to SHUTDOWN-PENDING
6716 */
6717 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
6718 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
6719 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
6720 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
6721 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
6722 }
6723 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
6724 if (TAILQ_EMPTY(&asoc->send_queue) &&
6725 TAILQ_EMPTY(&asoc->sent_queue) &&
6726 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
6727 struct mbuf *op_err;
6728 char msg[SCTP_DIAG_INFO_LEN];
6729
6730 abort_anyway:
6731 SCTP_SNPRINTF(msg, sizeof(msg),
6732 "%s:%d at %s", __FILE__, __LINE__, __func__);
6733 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
6734 msg);
6735 atomic_add_int(&stcb->asoc.refcnt, 1);
6736 sctp_abort_an_association(stcb->sctp_ep, stcb,
6737 op_err, false, SCTP_SO_NOT_LOCKED);
6738 atomic_subtract_int(&stcb->asoc.refcnt, 1);
6739 goto no_chunk_output;
6740 }
6741 }
6742 }
6743 }
6744 }
6745 un_sent = ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) +
6746 (stcb->asoc.stream_queue_cnt * SCTP_DATA_CHUNK_OVERHEAD(stcb)));
6747
6748 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
6749 (stcb->asoc.total_flight > 0) &&
6750 (un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD))) {
6751 do_chunk_output = 0;
6752 }
6753 if (do_chunk_output)
6754 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_NOT_LOCKED);
6755 else if (added_control) {
6756 struct timeval now;
6757 int num_out, reason, now_filled = 0;
6758
6759 (void)sctp_med_chunk_output(inp, stcb, &stcb->asoc, &num_out,
6760 &reason, 1, 1, &now, &now_filled,
6761 sctp_get_frag_point(stcb),
6762 SCTP_SO_NOT_LOCKED);
6763 }
6764 no_chunk_output:
6765 if (ret) {
6766 ca->cnt_failed++;
6767 } else {
6768 ca->cnt_sent++;
6769 }
6770 }
6771
6772 static void
sctp_sendall_completes(void * ptr,uint32_t val SCTP_UNUSED)6773 sctp_sendall_completes(void *ptr, uint32_t val SCTP_UNUSED)
6774 {
6775 struct sctp_copy_all *ca;
6776
6777 ca = (struct sctp_copy_all *)ptr;
6778 /*
6779 * Do a notify here? Kacheong suggests that the notify be done at
6780 * the send time.. so you would push up a notification if any send
6781 * failed. Don't know if this is feasible since the only failures we
6782 * have is "memory" related and if you cannot get an mbuf to send
6783 * the data you surely can't get an mbuf to send up to notify the
6784 * user you can't send the data :->
6785 */
6786
6787 /* now free everything */
6788 if (ca->inp) {
6789 /* Lets clear the flag to allow others to run. */
6790 SCTP_INP_WLOCK(ca->inp);
6791 ca->inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6792 SCTP_INP_WUNLOCK(ca->inp);
6793 }
6794 sctp_m_freem(ca->m);
6795 SCTP_FREE(ca, SCTP_M_COPYAL);
6796 }
6797
6798 static struct mbuf *
sctp_copy_out_all(struct uio * uio,ssize_t len)6799 sctp_copy_out_all(struct uio *uio, ssize_t len)
6800 {
6801 struct mbuf *ret, *at;
6802 ssize_t left, willcpy, cancpy, error;
6803
6804 ret = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_WAITOK, 1, MT_DATA);
6805 if (ret == NULL) {
6806 /* TSNH */
6807 return (NULL);
6808 }
6809 left = len;
6810 SCTP_BUF_LEN(ret) = 0;
6811 /* save space for the data chunk header */
6812 cancpy = (int)M_TRAILINGSPACE(ret);
6813 willcpy = min(cancpy, left);
6814 at = ret;
6815 while (left > 0) {
6816 /* Align data to the end */
6817 error = uiomove(mtod(at, caddr_t), (int)willcpy, uio);
6818 if (error) {
6819 err_out_now:
6820 sctp_m_freem(at);
6821 return (NULL);
6822 }
6823 SCTP_BUF_LEN(at) = (int)willcpy;
6824 SCTP_BUF_NEXT_PKT(at) = SCTP_BUF_NEXT(at) = 0;
6825 left -= willcpy;
6826 if (left > 0) {
6827 SCTP_BUF_NEXT(at) = sctp_get_mbuf_for_msg((unsigned int)left, 0, M_WAITOK, 1, MT_DATA);
6828 if (SCTP_BUF_NEXT(at) == NULL) {
6829 goto err_out_now;
6830 }
6831 at = SCTP_BUF_NEXT(at);
6832 SCTP_BUF_LEN(at) = 0;
6833 cancpy = (int)M_TRAILINGSPACE(at);
6834 willcpy = min(cancpy, left);
6835 }
6836 }
6837 return (ret);
6838 }
6839
6840 static int
sctp_sendall(struct sctp_inpcb * inp,struct uio * uio,struct mbuf * m,struct sctp_nonpad_sndrcvinfo * srcv)6841 sctp_sendall(struct sctp_inpcb *inp, struct uio *uio, struct mbuf *m,
6842 struct sctp_nonpad_sndrcvinfo *srcv)
6843 {
6844 struct sctp_copy_all *ca;
6845 struct mbuf *mat;
6846 ssize_t sndlen;
6847 int ret;
6848
6849 if (uio != NULL) {
6850 sndlen = uio->uio_resid;
6851 } else {
6852 sndlen = 0;
6853 for (mat = m; mat; mat = SCTP_BUF_NEXT(mat)) {
6854 sndlen += SCTP_BUF_LEN(mat);
6855 }
6856 }
6857 if (sndlen > (ssize_t)SCTP_BASE_SYSCTL(sctp_sendall_limit)) {
6858 /* You must not be larger than the limit! */
6859 return (EMSGSIZE);
6860 }
6861 SCTP_MALLOC(ca, struct sctp_copy_all *, sizeof(struct sctp_copy_all),
6862 SCTP_M_COPYAL);
6863 if (ca == NULL) {
6864 sctp_m_freem(m);
6865 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6866 return (ENOMEM);
6867 }
6868 memset(ca, 0, sizeof(struct sctp_copy_all));
6869 ca->inp = inp;
6870 if (srcv != NULL) {
6871 memcpy(&ca->sndrcv, srcv, sizeof(struct sctp_nonpad_sndrcvinfo));
6872 }
6873 /* Serialize. */
6874 SCTP_INP_WLOCK(inp);
6875 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SND_ITERATOR_UP) != 0) {
6876 SCTP_INP_WUNLOCK(inp);
6877 sctp_m_freem(m);
6878 SCTP_FREE(ca, SCTP_M_COPYAL);
6879 return (EBUSY);
6880 }
6881 inp->sctp_flags |= SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6882 SCTP_INP_WUNLOCK(inp);
6883 /*
6884 * take off the sendall flag, it would be bad if we failed to do
6885 * this :-0
6886 */
6887 ca->sndrcv.sinfo_flags &= ~SCTP_SENDALL;
6888 /* get length and mbuf chain */
6889 ca->sndlen = sndlen;
6890 if (uio != NULL) {
6891 ca->m = sctp_copy_out_all(uio, ca->sndlen);
6892 if (ca->m == NULL) {
6893 SCTP_FREE(ca, SCTP_M_COPYAL);
6894 sctp_m_freem(m);
6895 SCTP_INP_WLOCK(inp);
6896 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6897 SCTP_INP_WUNLOCK(inp);
6898 SCTP_LTRACE_ERR_RET(inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
6899 return (ENOMEM);
6900 }
6901 } else {
6902 ca->m = m;
6903 }
6904 ret = sctp_initiate_iterator(NULL, sctp_sendall_iterator, NULL,
6905 SCTP_PCB_ANY_FLAGS, SCTP_PCB_ANY_FEATURES,
6906 SCTP_ASOC_ANY_STATE,
6907 (void *)ca, 0,
6908 sctp_sendall_completes, inp, 1);
6909 if (ret != 0) {
6910 SCTP_INP_WLOCK(inp);
6911 inp->sctp_flags &= ~SCTP_PCB_FLAGS_SND_ITERATOR_UP;
6912 SCTP_INP_WUNLOCK(inp);
6913 SCTP_FREE(ca, SCTP_M_COPYAL);
6914 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
6915 return (EFAULT);
6916 }
6917 return (0);
6918 }
6919
6920 void
sctp_toss_old_cookies(struct sctp_tcb * stcb,struct sctp_association * asoc)6921 sctp_toss_old_cookies(struct sctp_tcb *stcb, struct sctp_association *asoc)
6922 {
6923 struct sctp_tmit_chunk *chk, *nchk;
6924
6925 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
6926 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
6927 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
6928 asoc->ctrl_queue_cnt--;
6929 if (chk->data) {
6930 sctp_m_freem(chk->data);
6931 chk->data = NULL;
6932 }
6933 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6934 }
6935 }
6936 }
6937
6938 void
sctp_toss_old_asconf(struct sctp_tcb * stcb)6939 sctp_toss_old_asconf(struct sctp_tcb *stcb)
6940 {
6941 struct sctp_association *asoc;
6942 struct sctp_tmit_chunk *chk, *nchk;
6943 struct sctp_asconf_chunk *acp;
6944
6945 asoc = &stcb->asoc;
6946 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
6947 /* find SCTP_ASCONF chunk in queue */
6948 if (chk->rec.chunk_id.id == SCTP_ASCONF) {
6949 if (chk->data) {
6950 acp = mtod(chk->data, struct sctp_asconf_chunk *);
6951 if (SCTP_TSN_GT(ntohl(acp->serial_number), asoc->asconf_seq_out_acked)) {
6952 /* Not Acked yet */
6953 break;
6954 }
6955 }
6956 TAILQ_REMOVE(&asoc->asconf_send_queue, chk, sctp_next);
6957 asoc->ctrl_queue_cnt--;
6958 if (chk->data) {
6959 sctp_m_freem(chk->data);
6960 chk->data = NULL;
6961 }
6962 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
6963 }
6964 }
6965 }
6966
6967 static void
sctp_clean_up_datalist(struct sctp_tcb * stcb,struct sctp_association * asoc,struct sctp_tmit_chunk ** data_list,int bundle_at,struct sctp_nets * net)6968 sctp_clean_up_datalist(struct sctp_tcb *stcb,
6969 struct sctp_association *asoc,
6970 struct sctp_tmit_chunk **data_list,
6971 int bundle_at,
6972 struct sctp_nets *net)
6973 {
6974 int i;
6975 struct sctp_tmit_chunk *tp1;
6976
6977 for (i = 0; i < bundle_at; i++) {
6978 /* off of the send queue */
6979 TAILQ_REMOVE(&asoc->send_queue, data_list[i], sctp_next);
6980 asoc->send_queue_cnt--;
6981 if (i > 0) {
6982 /*
6983 * Any chunk NOT 0 you zap the time chunk 0 gets
6984 * zapped or set based on if a RTO measurement is
6985 * needed.
6986 */
6987 data_list[i]->do_rtt = 0;
6988 }
6989 /* record time */
6990 data_list[i]->sent_rcv_time = net->last_sent_time;
6991 data_list[i]->rec.data.cwnd_at_send = net->cwnd;
6992 data_list[i]->rec.data.fast_retran_tsn = data_list[i]->rec.data.tsn;
6993 if (data_list[i]->whoTo == NULL) {
6994 data_list[i]->whoTo = net;
6995 atomic_add_int(&net->ref_count, 1);
6996 }
6997 /* on to the sent queue */
6998 tp1 = TAILQ_LAST(&asoc->sent_queue, sctpchunk_listhead);
6999 if ((tp1) && SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7000 struct sctp_tmit_chunk *tpp;
7001
7002 /* need to move back */
7003 back_up_more:
7004 tpp = TAILQ_PREV(tp1, sctpchunk_listhead, sctp_next);
7005 if (tpp == NULL) {
7006 TAILQ_INSERT_BEFORE(tp1, data_list[i], sctp_next);
7007 goto all_done;
7008 }
7009 tp1 = tpp;
7010 if (SCTP_TSN_GT(tp1->rec.data.tsn, data_list[i]->rec.data.tsn)) {
7011 goto back_up_more;
7012 }
7013 TAILQ_INSERT_AFTER(&asoc->sent_queue, tp1, data_list[i], sctp_next);
7014 } else {
7015 TAILQ_INSERT_TAIL(&asoc->sent_queue,
7016 data_list[i],
7017 sctp_next);
7018 }
7019 all_done:
7020 /* This does not lower until the cum-ack passes it */
7021 asoc->sent_queue_cnt++;
7022 if ((asoc->peers_rwnd <= 0) &&
7023 (asoc->total_flight == 0) &&
7024 (bundle_at == 1)) {
7025 /* Mark the chunk as being a window probe */
7026 SCTP_STAT_INCR(sctps_windowprobed);
7027 }
7028 #ifdef SCTP_AUDITING_ENABLED
7029 sctp_audit_log(0xC2, 3);
7030 #endif
7031 data_list[i]->sent = SCTP_DATAGRAM_SENT;
7032 data_list[i]->snd_count = 1;
7033 data_list[i]->rec.data.chunk_was_revoked = 0;
7034 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
7035 sctp_misc_ints(SCTP_FLIGHT_LOG_UP,
7036 data_list[i]->whoTo->flight_size,
7037 data_list[i]->book_size,
7038 (uint32_t)(uintptr_t)data_list[i]->whoTo,
7039 data_list[i]->rec.data.tsn);
7040 }
7041 sctp_flight_size_increase(data_list[i]);
7042 sctp_total_flight_increase(stcb, data_list[i]);
7043 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
7044 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
7045 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
7046 }
7047 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
7048 (uint32_t)(data_list[i]->send_size + SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
7049 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
7050 /* SWS sender side engages */
7051 asoc->peers_rwnd = 0;
7052 }
7053 }
7054 if (asoc->cc_functions.sctp_cwnd_update_packet_transmitted) {
7055 (*asoc->cc_functions.sctp_cwnd_update_packet_transmitted) (stcb, net);
7056 }
7057 }
7058
7059 static void
sctp_clean_up_ctl(struct sctp_tcb * stcb,struct sctp_association * asoc,int so_locked)7060 sctp_clean_up_ctl(struct sctp_tcb *stcb, struct sctp_association *asoc, int so_locked)
7061 {
7062 struct sctp_tmit_chunk *chk, *nchk;
7063
7064 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
7065 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7066 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
7067 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
7068 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
7069 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) ||
7070 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
7071 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
7072 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
7073 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
7074 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
7075 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
7076 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
7077 /* Stray chunks must be cleaned up */
7078 clean_up_anyway:
7079 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
7080 asoc->ctrl_queue_cnt--;
7081 if (chk->data) {
7082 sctp_m_freem(chk->data);
7083 chk->data = NULL;
7084 }
7085 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
7086 asoc->fwd_tsn_cnt--;
7087 }
7088 sctp_free_a_chunk(stcb, chk, so_locked);
7089 } else if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
7090 /* special handling, we must look into the param */
7091 if (chk != asoc->str_reset) {
7092 goto clean_up_anyway;
7093 }
7094 }
7095 }
7096 }
7097
7098 static uint32_t
sctp_can_we_split_this(struct sctp_tcb * stcb,uint32_t length,uint32_t space_left,uint32_t frag_point,int eeor_on)7099 sctp_can_we_split_this(struct sctp_tcb *stcb, uint32_t length,
7100 uint32_t space_left, uint32_t frag_point, int eeor_on)
7101 {
7102 /*
7103 * Make a decision on if I should split a msg into multiple parts.
7104 * This is only asked of incomplete messages.
7105 */
7106 if (eeor_on) {
7107 /*
7108 * If we are doing EEOR we need to always send it if its the
7109 * entire thing, since it might be all the guy is putting in
7110 * the hopper.
7111 */
7112 if (space_left >= length) {
7113 /*-
7114 * If we have data outstanding,
7115 * we get another chance when the sack
7116 * arrives to transmit - wait for more data
7117 */
7118 if (stcb->asoc.total_flight == 0) {
7119 /*
7120 * If nothing is in flight, we zero the
7121 * packet counter.
7122 */
7123 return (length);
7124 }
7125 return (0);
7126
7127 } else {
7128 /* You can fill the rest */
7129 return (space_left);
7130 }
7131 }
7132 /*-
7133 * For those strange folk that make the send buffer
7134 * smaller than our fragmentation point, we can't
7135 * get a full msg in so we have to allow splitting.
7136 */
7137 if (SCTP_SB_LIMIT_SND(stcb->sctp_socket) < frag_point) {
7138 return (length);
7139 }
7140 if ((length <= space_left) ||
7141 ((length - space_left) < SCTP_BASE_SYSCTL(sctp_min_residual))) {
7142 /* Sub-optimal residual don't split in non-eeor mode. */
7143 return (0);
7144 }
7145 /*
7146 * If we reach here length is larger than the space_left. Do we wish
7147 * to split it for the sake of packet putting together?
7148 */
7149 if (space_left >= min(SCTP_BASE_SYSCTL(sctp_min_split_point), frag_point)) {
7150 /* Its ok to split it */
7151 return (min(space_left, frag_point));
7152 }
7153 /* Nope, can't split */
7154 return (0);
7155 }
7156
7157 static uint32_t
sctp_move_to_outqueue(struct sctp_tcb * stcb,struct sctp_nets * net,struct sctp_stream_out * strq,uint32_t space_left,uint32_t frag_point,int * giveup,int eeor_mode,int * bail,int so_locked)7158 sctp_move_to_outqueue(struct sctp_tcb *stcb,
7159 struct sctp_nets *net,
7160 struct sctp_stream_out *strq,
7161 uint32_t space_left,
7162 uint32_t frag_point,
7163 int *giveup,
7164 int eeor_mode,
7165 int *bail,
7166 int so_locked)
7167 {
7168 /* Move from the stream to the send_queue keeping track of the total */
7169 struct sctp_association *asoc;
7170 struct sctp_stream_queue_pending *sp;
7171 struct sctp_tmit_chunk *chk;
7172 struct sctp_data_chunk *dchkh = NULL;
7173 struct sctp_idata_chunk *ndchkh = NULL;
7174 uint32_t to_move, length;
7175 int leading;
7176 uint8_t rcv_flags = 0;
7177 uint8_t some_taken;
7178
7179 SCTP_TCB_LOCK_ASSERT(stcb);
7180 asoc = &stcb->asoc;
7181 one_more_time:
7182 /* sa_ignore FREED_MEMORY */
7183 sp = TAILQ_FIRST(&strq->outqueue);
7184 if (sp == NULL) {
7185 sp = TAILQ_FIRST(&strq->outqueue);
7186 if (sp) {
7187 goto one_more_time;
7188 }
7189 if ((sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_EXPLICIT_EOR) == 0) &&
7190 (stcb->asoc.idata_supported == 0) &&
7191 (strq->last_msg_incomplete)) {
7192 SCTP_PRINTF("Huh? Stream:%d lm_in_c=%d but queue is NULL\n",
7193 strq->sid,
7194 strq->last_msg_incomplete);
7195 strq->last_msg_incomplete = 0;
7196 }
7197 to_move = 0;
7198 goto out_of;
7199 }
7200 if ((sp->msg_is_complete) && (sp->length == 0)) {
7201 if (sp->sender_all_done) {
7202 /*
7203 * We are doing deferred cleanup. Last time through
7204 * when we took all the data the sender_all_done was
7205 * not set.
7206 */
7207 if ((sp->put_last_out == 0) && (sp->discard_rest == 0)) {
7208 SCTP_PRINTF("Gak, put out entire msg with NO end!-1\n");
7209 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7210 sp->sender_all_done,
7211 sp->length,
7212 sp->msg_is_complete,
7213 sp->put_last_out);
7214 }
7215 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7216 TAILQ_REMOVE(&strq->outqueue, sp, next);
7217 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7218 if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7219 (strq->chunks_on_queues == 0) &&
7220 TAILQ_EMPTY(&strq->outqueue)) {
7221 stcb->asoc.trigger_reset = 1;
7222 }
7223 if (sp->net) {
7224 sctp_free_remote_addr(sp->net);
7225 sp->net = NULL;
7226 }
7227 if (sp->data) {
7228 sctp_m_freem(sp->data);
7229 sp->data = NULL;
7230 }
7231 sctp_free_a_strmoq(stcb, sp, so_locked);
7232 /* back to get the next msg */
7233 goto one_more_time;
7234 } else {
7235 /*
7236 * sender just finished this but still holds a
7237 * reference
7238 */
7239 *giveup = 1;
7240 to_move = 0;
7241 goto out_of;
7242 }
7243 } else {
7244 /* is there some to get */
7245 if (sp->length == 0) {
7246 /* no */
7247 *giveup = 1;
7248 to_move = 0;
7249 goto out_of;
7250 } else if (sp->discard_rest) {
7251 /* Whack down the size */
7252 atomic_subtract_int(&stcb->asoc.total_output_queue_size, sp->length);
7253 if ((stcb->sctp_socket != NULL) &&
7254 ((stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) ||
7255 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL))) {
7256 SCTP_SB_DECR(&stcb->sctp_socket->so_snd, sp->length);
7257 }
7258 if (sp->data) {
7259 sctp_m_freem(sp->data);
7260 sp->data = NULL;
7261 sp->tail_mbuf = NULL;
7262 }
7263 sp->length = 0;
7264 sp->some_taken = 1;
7265 *giveup = 1;
7266 to_move = 0;
7267 goto out_of;
7268 }
7269 }
7270 some_taken = sp->some_taken;
7271 length = sp->length;
7272 if (sp->msg_is_complete) {
7273 /* The message is complete */
7274 to_move = min(length, frag_point);
7275 if (to_move == length) {
7276 /* All of it fits in the MTU */
7277 if (sp->some_taken) {
7278 rcv_flags |= SCTP_DATA_LAST_FRAG;
7279 } else {
7280 rcv_flags |= SCTP_DATA_NOT_FRAG;
7281 }
7282 sp->put_last_out = 1;
7283 if (sp->sinfo_flags & SCTP_SACK_IMMEDIATELY) {
7284 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7285 }
7286 } else {
7287 /* Not all of it fits, we fragment */
7288 if (sp->some_taken == 0) {
7289 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7290 }
7291 sp->some_taken = 1;
7292 }
7293 } else {
7294 to_move = sctp_can_we_split_this(stcb, length, space_left, frag_point, eeor_mode);
7295 if (to_move > 0) {
7296 if (to_move >= length) {
7297 to_move = length;
7298 }
7299 if (sp->some_taken == 0) {
7300 rcv_flags |= SCTP_DATA_FIRST_FRAG;
7301 sp->some_taken = 1;
7302 }
7303 } else {
7304 /* Nothing to take. */
7305 *giveup = 1;
7306 to_move = 0;
7307 goto out_of;
7308 }
7309 }
7310
7311 /* If we reach here, we can copy out a chunk */
7312 sctp_alloc_a_chunk(stcb, chk);
7313 if (chk == NULL) {
7314 /* No chunk memory */
7315 *giveup = 1;
7316 to_move = 0;
7317 goto out_of;
7318 }
7319 /*
7320 * Setup for unordered if needed by looking at the user sent info
7321 * flags.
7322 */
7323 if (sp->sinfo_flags & SCTP_UNORDERED) {
7324 rcv_flags |= SCTP_DATA_UNORDERED;
7325 }
7326 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
7327 (sp->sinfo_flags & SCTP_EOF) == SCTP_EOF) {
7328 rcv_flags |= SCTP_DATA_SACK_IMMEDIATELY;
7329 }
7330 /* clear out the chunk before setting up */
7331 memset(chk, 0, sizeof(*chk));
7332 chk->rec.data.rcv_flags = rcv_flags;
7333
7334 if (to_move >= length) {
7335 /* we think we can steal the whole thing */
7336 if (to_move < sp->length) {
7337 /* bail, it changed */
7338 goto dont_do_it;
7339 }
7340 chk->data = sp->data;
7341 chk->last_mbuf = sp->tail_mbuf;
7342 /* register the stealing */
7343 sp->data = sp->tail_mbuf = NULL;
7344 } else {
7345 struct mbuf *m;
7346
7347 dont_do_it:
7348 chk->data = SCTP_M_COPYM(sp->data, 0, to_move, M_NOWAIT);
7349 chk->last_mbuf = NULL;
7350 if (chk->data == NULL) {
7351 sp->some_taken = some_taken;
7352 sctp_free_a_chunk(stcb, chk, so_locked);
7353 *bail = 1;
7354 to_move = 0;
7355 goto out_of;
7356 }
7357 #ifdef SCTP_MBUF_LOGGING
7358 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
7359 sctp_log_mbc(chk->data, SCTP_MBUF_ICOPY);
7360 }
7361 #endif
7362 /* Pull off the data */
7363 m_adj(sp->data, to_move);
7364 /* Now lets work our way down and compact it */
7365 m = sp->data;
7366 while (m && (SCTP_BUF_LEN(m) == 0)) {
7367 sp->data = SCTP_BUF_NEXT(m);
7368 SCTP_BUF_NEXT(m) = NULL;
7369 if (sp->tail_mbuf == m) {
7370 /*-
7371 * Freeing tail? TSNH since
7372 * we supposedly were taking less
7373 * than the sp->length.
7374 */
7375 #ifdef INVARIANTS
7376 panic("Huh, freeing tail? - TSNH");
7377 #else
7378 SCTP_PRINTF("Huh, freeing tail? - TSNH\n");
7379 sp->tail_mbuf = sp->data = NULL;
7380 sp->length = 0;
7381 #endif
7382 }
7383 sctp_m_free(m);
7384 m = sp->data;
7385 }
7386 }
7387 if (SCTP_BUF_IS_EXTENDED(chk->data)) {
7388 chk->copy_by_ref = 1;
7389 } else {
7390 chk->copy_by_ref = 0;
7391 }
7392 /*
7393 * get last_mbuf and counts of mb usage This is ugly but hopefully
7394 * its only one mbuf.
7395 */
7396 if (chk->last_mbuf == NULL) {
7397 chk->last_mbuf = chk->data;
7398 while (SCTP_BUF_NEXT(chk->last_mbuf) != NULL) {
7399 chk->last_mbuf = SCTP_BUF_NEXT(chk->last_mbuf);
7400 }
7401 }
7402
7403 if (to_move > length) {
7404 /*- This should not happen either
7405 * since we always lower to_move to the size
7406 * of sp->length if its larger.
7407 */
7408 #ifdef INVARIANTS
7409 panic("Huh, how can to_move be larger?");
7410 #else
7411 SCTP_PRINTF("Huh, how can to_move be larger?\n");
7412 sp->length = 0;
7413 #endif
7414 } else {
7415 atomic_subtract_int(&sp->length, to_move);
7416 }
7417 leading = SCTP_DATA_CHUNK_OVERHEAD(stcb);
7418 if (M_LEADINGSPACE(chk->data) < leading) {
7419 /* Not enough room for a chunk header, get some */
7420 struct mbuf *m;
7421
7422 m = sctp_get_mbuf_for_msg(1, 0, M_NOWAIT, 1, MT_DATA);
7423 if (m == NULL) {
7424 /*
7425 * we're in trouble here. _PREPEND below will free
7426 * all the data if there is no leading space, so we
7427 * must put the data back and restore.
7428 */
7429 if (sp->data == NULL) {
7430 /* unsteal the data */
7431 sp->data = chk->data;
7432 sp->tail_mbuf = chk->last_mbuf;
7433 } else {
7434 struct mbuf *m_tmp;
7435
7436 /* reassemble the data */
7437 m_tmp = sp->data;
7438 sp->data = chk->data;
7439 SCTP_BUF_NEXT(chk->last_mbuf) = m_tmp;
7440 }
7441 sp->some_taken = some_taken;
7442 atomic_add_int(&sp->length, to_move);
7443 chk->data = NULL;
7444 *bail = 1;
7445 sctp_free_a_chunk(stcb, chk, so_locked);
7446 to_move = 0;
7447 goto out_of;
7448 } else {
7449 SCTP_BUF_LEN(m) = 0;
7450 SCTP_BUF_NEXT(m) = chk->data;
7451 chk->data = m;
7452 M_ALIGN(chk->data, 4);
7453 }
7454 }
7455 SCTP_BUF_PREPEND(chk->data, SCTP_DATA_CHUNK_OVERHEAD(stcb), M_NOWAIT);
7456 if (chk->data == NULL) {
7457 /* HELP, TSNH since we assured it would not above? */
7458 #ifdef INVARIANTS
7459 panic("prepend fails HELP?");
7460 #else
7461 SCTP_PRINTF("prepend fails HELP?\n");
7462 sctp_free_a_chunk(stcb, chk, so_locked);
7463 #endif
7464 *bail = 1;
7465 to_move = 0;
7466 goto out_of;
7467 }
7468 sctp_snd_sb_alloc(stcb, SCTP_DATA_CHUNK_OVERHEAD(stcb));
7469 chk->book_size = chk->send_size = (uint16_t)(to_move + SCTP_DATA_CHUNK_OVERHEAD(stcb));
7470 chk->book_size_scale = 0;
7471 chk->sent = SCTP_DATAGRAM_UNSENT;
7472
7473 chk->flags = 0;
7474 chk->asoc = &stcb->asoc;
7475 chk->pad_inplace = 0;
7476 chk->no_fr_allowed = 0;
7477 if (stcb->asoc.idata_supported == 0) {
7478 if (rcv_flags & SCTP_DATA_UNORDERED) {
7479 /* Just use 0. The receiver ignores the values. */
7480 chk->rec.data.mid = 0;
7481 } else {
7482 chk->rec.data.mid = strq->next_mid_ordered;
7483 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7484 strq->next_mid_ordered++;
7485 }
7486 }
7487 } else {
7488 if (rcv_flags & SCTP_DATA_UNORDERED) {
7489 chk->rec.data.mid = strq->next_mid_unordered;
7490 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7491 strq->next_mid_unordered++;
7492 }
7493 } else {
7494 chk->rec.data.mid = strq->next_mid_ordered;
7495 if (rcv_flags & SCTP_DATA_LAST_FRAG) {
7496 strq->next_mid_ordered++;
7497 }
7498 }
7499 }
7500 chk->rec.data.sid = sp->sid;
7501 chk->rec.data.ppid = sp->ppid;
7502 chk->rec.data.context = sp->context;
7503 chk->rec.data.doing_fast_retransmit = 0;
7504
7505 chk->rec.data.timetodrop = sp->ts;
7506 chk->flags = sp->act_flags;
7507
7508 if (sp->net) {
7509 chk->whoTo = sp->net;
7510 atomic_add_int(&chk->whoTo->ref_count, 1);
7511 } else
7512 chk->whoTo = NULL;
7513
7514 if (sp->holds_key_ref) {
7515 chk->auth_keyid = sp->auth_keyid;
7516 sctp_auth_key_acquire(stcb, chk->auth_keyid);
7517 chk->holds_key_ref = 1;
7518 }
7519 stcb->asoc.ss_functions.sctp_ss_scheduled(stcb, net, asoc, strq, to_move);
7520 chk->rec.data.tsn = atomic_fetchadd_int(&asoc->sending_seq, 1);
7521 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_AT_SEND_2_OUTQ) {
7522 sctp_misc_ints(SCTP_STRMOUT_LOG_SEND,
7523 (uint32_t)(uintptr_t)stcb, sp->length,
7524 (uint32_t)((chk->rec.data.sid << 16) | (0x0000ffff & chk->rec.data.mid)),
7525 chk->rec.data.tsn);
7526 }
7527 if (stcb->asoc.idata_supported == 0) {
7528 dchkh = mtod(chk->data, struct sctp_data_chunk *);
7529 } else {
7530 ndchkh = mtod(chk->data, struct sctp_idata_chunk *);
7531 }
7532 /*
7533 * Put the rest of the things in place now. Size was done earlier in
7534 * previous loop prior to padding.
7535 */
7536
7537 SCTP_TCB_LOCK_ASSERT(stcb);
7538 #ifdef SCTP_ASOCLOG_OF_TSNS
7539 if (asoc->tsn_out_at >= SCTP_TSN_LOG_SIZE) {
7540 asoc->tsn_out_at = 0;
7541 asoc->tsn_out_wrapped = 1;
7542 }
7543 asoc->out_tsnlog[asoc->tsn_out_at].tsn = chk->rec.data.tsn;
7544 asoc->out_tsnlog[asoc->tsn_out_at].strm = chk->rec.data.sid;
7545 asoc->out_tsnlog[asoc->tsn_out_at].seq = chk->rec.data.mid;
7546 asoc->out_tsnlog[asoc->tsn_out_at].sz = chk->send_size;
7547 asoc->out_tsnlog[asoc->tsn_out_at].flgs = chk->rec.data.rcv_flags;
7548 asoc->out_tsnlog[asoc->tsn_out_at].stcb = (void *)stcb;
7549 asoc->out_tsnlog[asoc->tsn_out_at].in_pos = asoc->tsn_out_at;
7550 asoc->out_tsnlog[asoc->tsn_out_at].in_out = 2;
7551 asoc->tsn_out_at++;
7552 #endif
7553 if (stcb->asoc.idata_supported == 0) {
7554 dchkh->ch.chunk_type = SCTP_DATA;
7555 dchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7556 dchkh->dp.tsn = htonl(chk->rec.data.tsn);
7557 dchkh->dp.sid = htons(strq->sid);
7558 dchkh->dp.ssn = htons((uint16_t)chk->rec.data.mid);
7559 dchkh->dp.ppid = chk->rec.data.ppid;
7560 dchkh->ch.chunk_length = htons(chk->send_size);
7561 } else {
7562 ndchkh->ch.chunk_type = SCTP_IDATA;
7563 ndchkh->ch.chunk_flags = chk->rec.data.rcv_flags;
7564 ndchkh->dp.tsn = htonl(chk->rec.data.tsn);
7565 ndchkh->dp.sid = htons(strq->sid);
7566 ndchkh->dp.reserved = htons(0);
7567 ndchkh->dp.mid = htonl(chk->rec.data.mid);
7568 if (sp->fsn == 0)
7569 ndchkh->dp.ppid_fsn.ppid = chk->rec.data.ppid;
7570 else
7571 ndchkh->dp.ppid_fsn.fsn = htonl(sp->fsn);
7572 sp->fsn++;
7573 ndchkh->ch.chunk_length = htons(chk->send_size);
7574 }
7575 /* Now advance the chk->send_size by the actual pad needed. */
7576 if (chk->send_size < SCTP_SIZE32(chk->book_size)) {
7577 /* need a pad */
7578 struct mbuf *lm;
7579 int pads;
7580
7581 pads = SCTP_SIZE32(chk->book_size) - chk->send_size;
7582 lm = sctp_pad_lastmbuf(chk->data, pads, chk->last_mbuf);
7583 if (lm != NULL) {
7584 chk->last_mbuf = lm;
7585 chk->pad_inplace = 1;
7586 }
7587 chk->send_size += pads;
7588 }
7589 if (PR_SCTP_ENABLED(chk->flags)) {
7590 asoc->pr_sctp_cnt++;
7591 }
7592 if (sp->msg_is_complete && (sp->length == 0) && (sp->sender_all_done)) {
7593 /* All done pull and kill the message */
7594 if (sp->put_last_out == 0) {
7595 SCTP_PRINTF("Gak, put out entire msg with NO end!-2\n");
7596 SCTP_PRINTF("sender_done:%d len:%d msg_comp:%d put_last_out:%d\n",
7597 sp->sender_all_done,
7598 sp->length,
7599 sp->msg_is_complete,
7600 sp->put_last_out);
7601 }
7602 atomic_subtract_int(&asoc->stream_queue_cnt, 1);
7603 TAILQ_REMOVE(&strq->outqueue, sp, next);
7604 stcb->asoc.ss_functions.sctp_ss_remove_from_stream(stcb, asoc, strq, sp);
7605 if ((strq->state == SCTP_STREAM_RESET_PENDING) &&
7606 (strq->chunks_on_queues == 0) &&
7607 TAILQ_EMPTY(&strq->outqueue)) {
7608 stcb->asoc.trigger_reset = 1;
7609 }
7610 if (sp->net) {
7611 sctp_free_remote_addr(sp->net);
7612 sp->net = NULL;
7613 }
7614 if (sp->data) {
7615 sctp_m_freem(sp->data);
7616 sp->data = NULL;
7617 }
7618 sctp_free_a_strmoq(stcb, sp, so_locked);
7619 }
7620 asoc->chunks_on_out_queue++;
7621 strq->chunks_on_queues++;
7622 TAILQ_INSERT_TAIL(&asoc->send_queue, chk, sctp_next);
7623 asoc->send_queue_cnt++;
7624 out_of:
7625 return (to_move);
7626 }
7627
7628 static void
sctp_fill_outqueue(struct sctp_tcb * stcb,struct sctp_nets * net,uint32_t frag_point,int eeor_mode,int * quit_now,int so_locked)7629 sctp_fill_outqueue(struct sctp_tcb *stcb, struct sctp_nets *net,
7630 uint32_t frag_point, int eeor_mode, int *quit_now,
7631 int so_locked)
7632 {
7633 struct sctp_association *asoc;
7634 struct sctp_stream_out *strq;
7635 uint32_t space_left, moved, total_moved;
7636 int bail, giveup;
7637
7638 SCTP_TCB_LOCK_ASSERT(stcb);
7639 asoc = &stcb->asoc;
7640 total_moved = 0;
7641 switch (net->ro._l_addr.sa.sa_family) {
7642 #ifdef INET
7643 case AF_INET:
7644 space_left = net->mtu - SCTP_MIN_V4_OVERHEAD;
7645 break;
7646 #endif
7647 #ifdef INET6
7648 case AF_INET6:
7649 space_left = net->mtu - SCTP_MIN_OVERHEAD;
7650 break;
7651 #endif
7652 default:
7653 /* TSNH */
7654 space_left = net->mtu;
7655 break;
7656 }
7657 /* Need an allowance for the data chunk header too */
7658 space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7659
7660 /* must make even word boundary */
7661 space_left &= 0xfffffffc;
7662 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7663 giveup = 0;
7664 bail = 0;
7665 while ((space_left > 0) && (strq != NULL)) {
7666 moved = sctp_move_to_outqueue(stcb, net, strq, space_left,
7667 frag_point, &giveup, eeor_mode,
7668 &bail, so_locked);
7669 if ((giveup != 0) || (bail != 0)) {
7670 break;
7671 }
7672 strq = stcb->asoc.ss_functions.sctp_ss_select_stream(stcb, net, asoc);
7673 total_moved += moved;
7674 if (space_left >= moved) {
7675 space_left -= moved;
7676 } else {
7677 space_left = 0;
7678 }
7679 if (space_left >= SCTP_DATA_CHUNK_OVERHEAD(stcb)) {
7680 space_left -= SCTP_DATA_CHUNK_OVERHEAD(stcb);
7681 } else {
7682 space_left = 0;
7683 }
7684 space_left &= 0xfffffffc;
7685 }
7686 if (bail != 0)
7687 *quit_now = 1;
7688
7689 stcb->asoc.ss_functions.sctp_ss_packet_done(stcb, net, asoc);
7690
7691 if (total_moved == 0) {
7692 if ((stcb->asoc.sctp_cmt_on_off == 0) &&
7693 (net == stcb->asoc.primary_destination)) {
7694 /* ran dry for primary network net */
7695 SCTP_STAT_INCR(sctps_primary_randry);
7696 } else if (stcb->asoc.sctp_cmt_on_off > 0) {
7697 /* ran dry with CMT on */
7698 SCTP_STAT_INCR(sctps_cmt_randry);
7699 }
7700 }
7701 }
7702
7703 void
sctp_fix_ecn_echo(struct sctp_association * asoc)7704 sctp_fix_ecn_echo(struct sctp_association *asoc)
7705 {
7706 struct sctp_tmit_chunk *chk;
7707
7708 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7709 if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
7710 chk->sent = SCTP_DATAGRAM_UNSENT;
7711 }
7712 }
7713 }
7714
7715 void
sctp_move_chunks_from_net(struct sctp_tcb * stcb,struct sctp_nets * net)7716 sctp_move_chunks_from_net(struct sctp_tcb *stcb, struct sctp_nets *net)
7717 {
7718 struct sctp_association *asoc;
7719 struct sctp_tmit_chunk *chk;
7720 struct sctp_stream_queue_pending *sp;
7721 unsigned int i;
7722
7723 if (net == NULL) {
7724 return;
7725 }
7726 asoc = &stcb->asoc;
7727 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
7728 TAILQ_FOREACH(sp, &stcb->asoc.strmout[i].outqueue, next) {
7729 if (sp->net == net) {
7730 sctp_free_remote_addr(sp->net);
7731 sp->net = NULL;
7732 }
7733 }
7734 }
7735 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7736 if (chk->whoTo == net) {
7737 sctp_free_remote_addr(chk->whoTo);
7738 chk->whoTo = NULL;
7739 }
7740 }
7741 }
7742
7743 int
sctp_med_chunk_output(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_association * asoc,int * num_out,int * reason_code,int control_only,int from_where,struct timeval * now,int * now_filled,uint32_t frag_point,int so_locked)7744 sctp_med_chunk_output(struct sctp_inpcb *inp,
7745 struct sctp_tcb *stcb,
7746 struct sctp_association *asoc,
7747 int *num_out,
7748 int *reason_code,
7749 int control_only, int from_where,
7750 struct timeval *now, int *now_filled,
7751 uint32_t frag_point, int so_locked)
7752 {
7753 /**
7754 * Ok this is the generic chunk service queue. we must do the
7755 * following:
7756 * - Service the stream queue that is next, moving any
7757 * message (note I must get a complete message i.e. FIRST/MIDDLE and
7758 * LAST to the out queue in one pass) and assigning TSN's. This
7759 * only applies though if the peer does not support NDATA. For NDATA
7760 * chunks its ok to not send the entire message ;-)
7761 * - Check to see if the cwnd/rwnd allows any output, if so we go ahead and
7762 * formulate and send the low level chunks. Making sure to combine
7763 * any control in the control chunk queue also.
7764 */
7765 struct sctp_nets *net, *start_at, *sack_goes_to = NULL, *old_start_at = NULL;
7766 struct mbuf *outchain, *endoutchain;
7767 struct sctp_tmit_chunk *chk, *nchk;
7768
7769 /* temp arrays for unlinking */
7770 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
7771 int no_fragmentflg, error;
7772 unsigned int max_rwnd_per_dest, max_send_per_dest;
7773 int one_chunk, hbflag, skip_data_for_this_net;
7774 int asconf, cookie, no_out_cnt;
7775 int bundle_at, ctl_cnt, no_data_chunks, eeor_mode;
7776 unsigned int mtu, r_mtu, omtu, mx_mtu, to_out;
7777 int tsns_sent = 0;
7778 uint32_t auth_offset;
7779 struct sctp_auth_chunk *auth;
7780 uint16_t auth_keyid;
7781 int override_ok = 1;
7782 int skip_fill_up = 0;
7783 int data_auth_reqd = 0;
7784
7785 /*
7786 * JRS 5/14/07 - Add flag for whether a heartbeat is sent to the
7787 * destination.
7788 */
7789 int quit_now = 0;
7790
7791 *num_out = 0;
7792 *reason_code = 0;
7793 auth_keyid = stcb->asoc.authinfo.active_keyid;
7794 if ((asoc->state & SCTP_STATE_SHUTDOWN_PENDING) ||
7795 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
7796 (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {
7797 eeor_mode = 1;
7798 } else {
7799 eeor_mode = 0;
7800 }
7801 ctl_cnt = no_out_cnt = asconf = cookie = 0;
7802 /*
7803 * First lets prime the pump. For each destination, if there is room
7804 * in the flight size, attempt to pull an MTU's worth out of the
7805 * stream queues into the general send_queue
7806 */
7807 #ifdef SCTP_AUDITING_ENABLED
7808 sctp_audit_log(0xC2, 2);
7809 #endif
7810 SCTP_TCB_LOCK_ASSERT(stcb);
7811 hbflag = 0;
7812 if (control_only)
7813 no_data_chunks = 1;
7814 else
7815 no_data_chunks = 0;
7816
7817 /* Nothing to possible to send? */
7818 if ((TAILQ_EMPTY(&asoc->control_send_queue) ||
7819 (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) &&
7820 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7821 TAILQ_EMPTY(&asoc->send_queue) &&
7822 sctp_is_there_unsent_data(stcb, so_locked) == 0) {
7823 nothing_to_send:
7824 *reason_code = 9;
7825 return (0);
7826 }
7827 if (asoc->peers_rwnd == 0) {
7828 /* No room in peers rwnd */
7829 *reason_code = 1;
7830 if (asoc->total_flight > 0) {
7831 /* we are allowed one chunk in flight */
7832 no_data_chunks = 1;
7833 }
7834 }
7835 if (stcb->asoc.ecn_echo_cnt_onq) {
7836 /* Record where a sack goes, if any */
7837 if (no_data_chunks &&
7838 (asoc->ctrl_queue_cnt == stcb->asoc.ecn_echo_cnt_onq)) {
7839 /* Nothing but ECNe to send - we don't do that */
7840 goto nothing_to_send;
7841 }
7842 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7843 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
7844 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
7845 sack_goes_to = chk->whoTo;
7846 break;
7847 }
7848 }
7849 }
7850 max_rwnd_per_dest = ((asoc->peers_rwnd + asoc->total_flight) / asoc->numnets);
7851 if (stcb->sctp_socket)
7852 max_send_per_dest = SCTP_SB_LIMIT_SND(stcb->sctp_socket) / asoc->numnets;
7853 else
7854 max_send_per_dest = 0;
7855 if (no_data_chunks == 0) {
7856 /* How many non-directed chunks are there? */
7857 TAILQ_FOREACH(chk, &asoc->send_queue, sctp_next) {
7858 if (chk->whoTo == NULL) {
7859 /*
7860 * We already have non-directed chunks on
7861 * the queue, no need to do a fill-up.
7862 */
7863 skip_fill_up = 1;
7864 break;
7865 }
7866 }
7867 }
7868 if ((no_data_chunks == 0) &&
7869 (skip_fill_up == 0) &&
7870 (!stcb->asoc.ss_functions.sctp_ss_is_empty(stcb, asoc))) {
7871 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
7872 /*
7873 * This for loop we are in takes in each net, if
7874 * its's got space in cwnd and has data sent to it
7875 * (when CMT is off) then it calls
7876 * sctp_fill_outqueue for the net. This gets data on
7877 * the send queue for that network.
7878 *
7879 * In sctp_fill_outqueue TSN's are assigned and data
7880 * is copied out of the stream buffers. Note mostly
7881 * copy by reference (we hope).
7882 */
7883 net->window_probe = 0;
7884 if ((net != stcb->asoc.alternate) &&
7885 ((net->dest_state & SCTP_ADDR_PF) ||
7886 ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) ||
7887 (net->dest_state & SCTP_ADDR_UNCONFIRMED))) {
7888 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7889 sctp_log_cwnd(stcb, net, 1,
7890 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7891 }
7892 continue;
7893 }
7894 if ((stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) &&
7895 (net->flight_size == 0)) {
7896 (*stcb->asoc.cc_functions.sctp_cwnd_new_transmission_begins) (stcb, net);
7897 }
7898 if (net->flight_size >= net->cwnd) {
7899 /* skip this network, no room - can't fill */
7900 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7901 sctp_log_cwnd(stcb, net, 3,
7902 SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7903 }
7904 continue;
7905 }
7906 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
7907 sctp_log_cwnd(stcb, net, 4, SCTP_CWND_LOG_FILL_OUTQ_CALLED);
7908 }
7909 sctp_fill_outqueue(stcb, net, frag_point, eeor_mode, &quit_now, so_locked);
7910 if (quit_now) {
7911 /* memory alloc failure */
7912 no_data_chunks = 1;
7913 break;
7914 }
7915 }
7916 }
7917 /* now service each destination and send out what we can for it */
7918 /* Nothing to send? */
7919 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7920 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7921 TAILQ_EMPTY(&asoc->send_queue)) {
7922 *reason_code = 8;
7923 return (0);
7924 }
7925
7926 if (asoc->sctp_cmt_on_off > 0) {
7927 /* get the last start point */
7928 start_at = asoc->last_net_cmt_send_started;
7929 if (start_at == NULL) {
7930 /* null so to beginning */
7931 start_at = TAILQ_FIRST(&asoc->nets);
7932 } else {
7933 start_at = TAILQ_NEXT(asoc->last_net_cmt_send_started, sctp_next);
7934 if (start_at == NULL) {
7935 start_at = TAILQ_FIRST(&asoc->nets);
7936 }
7937 }
7938 asoc->last_net_cmt_send_started = start_at;
7939 } else {
7940 start_at = TAILQ_FIRST(&asoc->nets);
7941 }
7942 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
7943 if (chk->whoTo == NULL) {
7944 if (asoc->alternate) {
7945 chk->whoTo = asoc->alternate;
7946 } else {
7947 chk->whoTo = asoc->primary_destination;
7948 }
7949 atomic_add_int(&chk->whoTo->ref_count, 1);
7950 }
7951 }
7952 old_start_at = NULL;
7953 again_one_more_time:
7954 for (net = start_at; net != NULL; net = TAILQ_NEXT(net, sctp_next)) {
7955 /* how much can we send? */
7956 /* SCTPDBG("Examine for sending net:%x\n", (uint32_t)net); */
7957 if (old_start_at && (old_start_at == net)) {
7958 /* through list completely. */
7959 break;
7960 }
7961 tsns_sent = 0xa;
7962 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
7963 TAILQ_EMPTY(&asoc->asconf_send_queue) &&
7964 (net->flight_size >= net->cwnd)) {
7965 /*
7966 * Nothing on control or asconf and flight is full,
7967 * we can skip even in the CMT case.
7968 */
7969 continue;
7970 }
7971 bundle_at = 0;
7972 endoutchain = outchain = NULL;
7973 auth = NULL;
7974 auth_offset = 0;
7975 no_fragmentflg = 1;
7976 one_chunk = 0;
7977 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
7978 skip_data_for_this_net = 1;
7979 } else {
7980 skip_data_for_this_net = 0;
7981 }
7982 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
7983 #ifdef INET
7984 case AF_INET:
7985 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
7986 break;
7987 #endif
7988 #ifdef INET6
7989 case AF_INET6:
7990 mtu = net->mtu - SCTP_MIN_OVERHEAD;
7991 break;
7992 #endif
7993 default:
7994 /* TSNH */
7995 mtu = net->mtu;
7996 break;
7997 }
7998 mx_mtu = mtu;
7999 to_out = 0;
8000 if (mtu > asoc->peers_rwnd) {
8001 if (asoc->total_flight > 0) {
8002 /* We have a packet in flight somewhere */
8003 r_mtu = asoc->peers_rwnd;
8004 } else {
8005 /* We are always allowed to send one MTU out */
8006 one_chunk = 1;
8007 r_mtu = mtu;
8008 }
8009 } else {
8010 r_mtu = mtu;
8011 }
8012 error = 0;
8013 /************************/
8014 /* ASCONF transmission */
8015 /************************/
8016 /* Now first lets go through the asconf queue */
8017 TAILQ_FOREACH_SAFE(chk, &asoc->asconf_send_queue, sctp_next, nchk) {
8018 if (chk->rec.chunk_id.id != SCTP_ASCONF) {
8019 continue;
8020 }
8021 if (chk->whoTo == NULL) {
8022 if (asoc->alternate == NULL) {
8023 if (asoc->primary_destination != net) {
8024 break;
8025 }
8026 } else {
8027 if (asoc->alternate != net) {
8028 break;
8029 }
8030 }
8031 } else {
8032 if (chk->whoTo != net) {
8033 break;
8034 }
8035 }
8036 if (chk->data == NULL) {
8037 break;
8038 }
8039 if (chk->sent != SCTP_DATAGRAM_UNSENT &&
8040 chk->sent != SCTP_DATAGRAM_RESEND) {
8041 break;
8042 }
8043 /*
8044 * if no AUTH is yet included and this chunk
8045 * requires it, make sure to account for it. We
8046 * don't apply the size until the AUTH chunk is
8047 * actually added below in case there is no room for
8048 * this chunk. NOTE: we overload the use of "omtu"
8049 * here
8050 */
8051 if ((auth == NULL) &&
8052 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8053 stcb->asoc.peer_auth_chunks)) {
8054 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8055 } else
8056 omtu = 0;
8057 /* Here we do NOT factor the r_mtu */
8058 if ((chk->send_size < (int)(mtu - omtu)) ||
8059 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8060 /*
8061 * We probably should glom the mbuf chain
8062 * from the chk->data for control but the
8063 * problem is it becomes yet one more level
8064 * of tracking to do if for some reason
8065 * output fails. Then I have got to
8066 * reconstruct the merged control chain.. el
8067 * yucko.. for now we take the easy way and
8068 * do the copy
8069 */
8070 /*
8071 * Add an AUTH chunk, if chunk requires it
8072 * save the offset into the chain for AUTH
8073 */
8074 if ((auth == NULL) &&
8075 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8076 stcb->asoc.peer_auth_chunks))) {
8077 outchain = sctp_add_auth_chunk(outchain,
8078 &endoutchain,
8079 &auth,
8080 &auth_offset,
8081 stcb,
8082 chk->rec.chunk_id.id);
8083 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8084 }
8085 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8086 (int)chk->rec.chunk_id.can_take_data,
8087 chk->send_size, chk->copy_by_ref);
8088 if (outchain == NULL) {
8089 *reason_code = 8;
8090 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8091 return (ENOMEM);
8092 }
8093 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8094 /* update our MTU size */
8095 if (mtu > (chk->send_size + omtu))
8096 mtu -= (chk->send_size + omtu);
8097 else
8098 mtu = 0;
8099 to_out += (chk->send_size + omtu);
8100 /* Do clear IP_DF ? */
8101 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8102 no_fragmentflg = 0;
8103 }
8104 if (chk->rec.chunk_id.can_take_data)
8105 chk->data = NULL;
8106 /*
8107 * set hb flag since we can use these for
8108 * RTO
8109 */
8110 hbflag = 1;
8111 asconf = 1;
8112 /*
8113 * should sysctl this: don't bundle data
8114 * with ASCONF since it requires AUTH
8115 */
8116 no_data_chunks = 1;
8117 chk->sent = SCTP_DATAGRAM_SENT;
8118 if (chk->whoTo == NULL) {
8119 chk->whoTo = net;
8120 atomic_add_int(&net->ref_count, 1);
8121 }
8122 chk->snd_count++;
8123 if (mtu == 0) {
8124 /*
8125 * Ok we are out of room but we can
8126 * output without effecting the
8127 * flight size since this little guy
8128 * is a control only packet.
8129 */
8130 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8131 /*
8132 * do NOT clear the asconf flag as
8133 * it is used to do appropriate
8134 * source address selection.
8135 */
8136 if (*now_filled == 0) {
8137 (void)SCTP_GETTIME_TIMEVAL(now);
8138 *now_filled = 1;
8139 }
8140 net->last_sent_time = *now;
8141 hbflag = 0;
8142 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8143 (struct sockaddr *)&net->ro._l_addr,
8144 outchain, auth_offset, auth,
8145 stcb->asoc.authinfo.active_keyid,
8146 no_fragmentflg, 0, asconf,
8147 inp->sctp_lport, stcb->rport,
8148 htonl(stcb->asoc.peer_vtag),
8149 net->port, NULL,
8150 0, 0,
8151 so_locked))) {
8152 /*
8153 * error, we could not
8154 * output
8155 */
8156 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8157 if (from_where == 0) {
8158 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8159 }
8160 if (error == ENOBUFS) {
8161 asoc->ifp_had_enobuf = 1;
8162 SCTP_STAT_INCR(sctps_lowlevelerr);
8163 }
8164 /* error, could not output */
8165 if (error == EHOSTUNREACH) {
8166 /*
8167 * Destination went
8168 * unreachable
8169 * during this send
8170 */
8171 sctp_move_chunks_from_net(stcb, net);
8172 }
8173 *reason_code = 7;
8174 break;
8175 } else {
8176 asoc->ifp_had_enobuf = 0;
8177 }
8178 /*
8179 * increase the number we sent, if a
8180 * cookie is sent we don't tell them
8181 * any was sent out.
8182 */
8183 outchain = endoutchain = NULL;
8184 auth = NULL;
8185 auth_offset = 0;
8186 if (!no_out_cnt)
8187 *num_out += ctl_cnt;
8188 /* recalc a clean slate and setup */
8189 switch (net->ro._l_addr.sa.sa_family) {
8190 #ifdef INET
8191 case AF_INET:
8192 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8193 break;
8194 #endif
8195 #ifdef INET6
8196 case AF_INET6:
8197 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8198 break;
8199 #endif
8200 default:
8201 /* TSNH */
8202 mtu = net->mtu;
8203 break;
8204 }
8205 to_out = 0;
8206 no_fragmentflg = 1;
8207 }
8208 }
8209 }
8210 if (error != 0) {
8211 /* try next net */
8212 continue;
8213 }
8214 /************************/
8215 /* Control transmission */
8216 /************************/
8217 /* Now first lets go through the control queue */
8218 TAILQ_FOREACH_SAFE(chk, &asoc->control_send_queue, sctp_next, nchk) {
8219 if ((sack_goes_to) &&
8220 (chk->rec.chunk_id.id == SCTP_ECN_ECHO) &&
8221 (chk->whoTo != sack_goes_to)) {
8222 /*
8223 * if we have a sack in queue, and we are
8224 * looking at an ecn echo that is NOT queued
8225 * to where the sack is going..
8226 */
8227 if (chk->whoTo == net) {
8228 /*
8229 * Don't transmit it to where its
8230 * going (current net)
8231 */
8232 continue;
8233 } else if (sack_goes_to == net) {
8234 /*
8235 * But do transmit it to this
8236 * address
8237 */
8238 goto skip_net_check;
8239 }
8240 }
8241 if (chk->whoTo == NULL) {
8242 if (asoc->alternate == NULL) {
8243 if (asoc->primary_destination != net) {
8244 continue;
8245 }
8246 } else {
8247 if (asoc->alternate != net) {
8248 continue;
8249 }
8250 }
8251 } else {
8252 if (chk->whoTo != net) {
8253 continue;
8254 }
8255 }
8256 skip_net_check:
8257 if (chk->data == NULL) {
8258 continue;
8259 }
8260 if (chk->sent != SCTP_DATAGRAM_UNSENT) {
8261 /*
8262 * It must be unsent. Cookies and ASCONF's
8263 * hang around but there timers will force
8264 * when marked for resend.
8265 */
8266 continue;
8267 }
8268 /*
8269 * if no AUTH is yet included and this chunk
8270 * requires it, make sure to account for it. We
8271 * don't apply the size until the AUTH chunk is
8272 * actually added below in case there is no room for
8273 * this chunk. NOTE: we overload the use of "omtu"
8274 * here
8275 */
8276 if ((auth == NULL) &&
8277 sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8278 stcb->asoc.peer_auth_chunks)) {
8279 omtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8280 } else
8281 omtu = 0;
8282 /* Here we do NOT factor the r_mtu */
8283 if ((chk->send_size <= (int)(mtu - omtu)) ||
8284 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
8285 /*
8286 * We probably should glom the mbuf chain
8287 * from the chk->data for control but the
8288 * problem is it becomes yet one more level
8289 * of tracking to do if for some reason
8290 * output fails. Then I have got to
8291 * reconstruct the merged control chain.. el
8292 * yucko.. for now we take the easy way and
8293 * do the copy
8294 */
8295 /*
8296 * Add an AUTH chunk, if chunk requires it
8297 * save the offset into the chain for AUTH
8298 */
8299 if ((auth == NULL) &&
8300 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
8301 stcb->asoc.peer_auth_chunks))) {
8302 outchain = sctp_add_auth_chunk(outchain,
8303 &endoutchain,
8304 &auth,
8305 &auth_offset,
8306 stcb,
8307 chk->rec.chunk_id.id);
8308 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8309 }
8310 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain,
8311 (int)chk->rec.chunk_id.can_take_data,
8312 chk->send_size, chk->copy_by_ref);
8313 if (outchain == NULL) {
8314 *reason_code = 8;
8315 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8316 return (ENOMEM);
8317 }
8318 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8319 /* update our MTU size */
8320 if (mtu > (chk->send_size + omtu))
8321 mtu -= (chk->send_size + omtu);
8322 else
8323 mtu = 0;
8324 to_out += (chk->send_size + omtu);
8325 /* Do clear IP_DF ? */
8326 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8327 no_fragmentflg = 0;
8328 }
8329 if (chk->rec.chunk_id.can_take_data)
8330 chk->data = NULL;
8331 /* Mark things to be removed, if needed */
8332 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8333 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK) || /* EY */
8334 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) ||
8335 (chk->rec.chunk_id.id == SCTP_HEARTBEAT_ACK) ||
8336 (chk->rec.chunk_id.id == SCTP_SHUTDOWN) ||
8337 (chk->rec.chunk_id.id == SCTP_SHUTDOWN_ACK) ||
8338 (chk->rec.chunk_id.id == SCTP_OPERATION_ERROR) ||
8339 (chk->rec.chunk_id.id == SCTP_COOKIE_ACK) ||
8340 (chk->rec.chunk_id.id == SCTP_ECN_CWR) ||
8341 (chk->rec.chunk_id.id == SCTP_PACKET_DROPPED) ||
8342 (chk->rec.chunk_id.id == SCTP_ASCONF_ACK)) {
8343 if (chk->rec.chunk_id.id == SCTP_HEARTBEAT_REQUEST) {
8344 hbflag = 1;
8345 }
8346 /* remove these chunks at the end */
8347 if ((chk->rec.chunk_id.id == SCTP_SELECTIVE_ACK) ||
8348 (chk->rec.chunk_id.id == SCTP_NR_SELECTIVE_ACK)) {
8349 /* turn off the timer */
8350 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
8351 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
8352 inp, stcb, NULL,
8353 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_1);
8354 }
8355 }
8356 ctl_cnt++;
8357 } else {
8358 /*
8359 * Other chunks, since they have
8360 * timers running (i.e. COOKIE) we
8361 * just "trust" that it gets sent or
8362 * retransmitted.
8363 */
8364 ctl_cnt++;
8365 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
8366 cookie = 1;
8367 no_out_cnt = 1;
8368 } else if (chk->rec.chunk_id.id == SCTP_ECN_ECHO) {
8369 /*
8370 * Increment ecne send count
8371 * here this means we may be
8372 * over-zealous in our
8373 * counting if the send
8374 * fails, but its the best
8375 * place to do it (we used
8376 * to do it in the queue of
8377 * the chunk, but that did
8378 * not tell how many times
8379 * it was sent.
8380 */
8381 SCTP_STAT_INCR(sctps_sendecne);
8382 }
8383 chk->sent = SCTP_DATAGRAM_SENT;
8384 if (chk->whoTo == NULL) {
8385 chk->whoTo = net;
8386 atomic_add_int(&net->ref_count, 1);
8387 }
8388 chk->snd_count++;
8389 }
8390 if (mtu == 0) {
8391 /*
8392 * Ok we are out of room but we can
8393 * output without effecting the
8394 * flight size since this little guy
8395 * is a control only packet.
8396 */
8397 if (asconf) {
8398 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, net);
8399 /*
8400 * do NOT clear the asconf
8401 * flag as it is used to do
8402 * appropriate source
8403 * address selection.
8404 */
8405 }
8406 if (cookie) {
8407 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8408 cookie = 0;
8409 }
8410 /* Only HB or ASCONF advances time */
8411 if (hbflag) {
8412 if (*now_filled == 0) {
8413 (void)SCTP_GETTIME_TIMEVAL(now);
8414 *now_filled = 1;
8415 }
8416 net->last_sent_time = *now;
8417 hbflag = 0;
8418 }
8419 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
8420 (struct sockaddr *)&net->ro._l_addr,
8421 outchain,
8422 auth_offset, auth,
8423 stcb->asoc.authinfo.active_keyid,
8424 no_fragmentflg, 0, asconf,
8425 inp->sctp_lport, stcb->rport,
8426 htonl(stcb->asoc.peer_vtag),
8427 net->port, NULL,
8428 0, 0,
8429 so_locked))) {
8430 /*
8431 * error, we could not
8432 * output
8433 */
8434 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8435 if (from_where == 0) {
8436 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8437 }
8438 if (error == ENOBUFS) {
8439 asoc->ifp_had_enobuf = 1;
8440 SCTP_STAT_INCR(sctps_lowlevelerr);
8441 }
8442 if (error == EHOSTUNREACH) {
8443 /*
8444 * Destination went
8445 * unreachable
8446 * during this send
8447 */
8448 sctp_move_chunks_from_net(stcb, net);
8449 }
8450 *reason_code = 7;
8451 break;
8452 } else {
8453 asoc->ifp_had_enobuf = 0;
8454 }
8455 /*
8456 * increase the number we sent, if a
8457 * cookie is sent we don't tell them
8458 * any was sent out.
8459 */
8460 outchain = endoutchain = NULL;
8461 auth = NULL;
8462 auth_offset = 0;
8463 if (!no_out_cnt)
8464 *num_out += ctl_cnt;
8465 /* recalc a clean slate and setup */
8466 switch (net->ro._l_addr.sa.sa_family) {
8467 #ifdef INET
8468 case AF_INET:
8469 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8470 break;
8471 #endif
8472 #ifdef INET6
8473 case AF_INET6:
8474 mtu = net->mtu - SCTP_MIN_OVERHEAD;
8475 break;
8476 #endif
8477 default:
8478 /* TSNH */
8479 mtu = net->mtu;
8480 break;
8481 }
8482 to_out = 0;
8483 no_fragmentflg = 1;
8484 }
8485 }
8486 }
8487 if (error != 0) {
8488 /* try next net */
8489 continue;
8490 }
8491 /* JRI: if dest is in PF state, do not send data to it */
8492 if ((asoc->sctp_cmt_on_off > 0) &&
8493 (net != stcb->asoc.alternate) &&
8494 (net->dest_state & SCTP_ADDR_PF)) {
8495 goto no_data_fill;
8496 }
8497 if (net->flight_size >= net->cwnd) {
8498 goto no_data_fill;
8499 }
8500 if ((asoc->sctp_cmt_on_off > 0) &&
8501 (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_RECV_BUFFER_SPLITTING) &&
8502 (net->flight_size > max_rwnd_per_dest)) {
8503 goto no_data_fill;
8504 }
8505 /*
8506 * We need a specific accounting for the usage of the send
8507 * buffer. We also need to check the number of messages per
8508 * net. For now, this is better than nothing and it disabled
8509 * by default...
8510 */
8511 if ((asoc->sctp_cmt_on_off > 0) &&
8512 (SCTP_BASE_SYSCTL(sctp_buffer_splitting) & SCTP_SEND_BUFFER_SPLITTING) &&
8513 (max_send_per_dest > 0) &&
8514 (net->flight_size > max_send_per_dest)) {
8515 goto no_data_fill;
8516 }
8517 /*********************/
8518 /* Data transmission */
8519 /*********************/
8520 /*
8521 * if AUTH for DATA is required and no AUTH has been added
8522 * yet, account for this in the mtu now... if no data can be
8523 * bundled, this adjustment won't matter anyways since the
8524 * packet will be going out...
8525 */
8526 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA,
8527 stcb->asoc.peer_auth_chunks);
8528 if (data_auth_reqd && (auth == NULL)) {
8529 mtu -= sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
8530 }
8531 /* now lets add any data within the MTU constraints */
8532 switch (((struct sockaddr *)&net->ro._l_addr)->sa_family) {
8533 #ifdef INET
8534 case AF_INET:
8535 if (net->mtu > SCTP_MIN_V4_OVERHEAD)
8536 omtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
8537 else
8538 omtu = 0;
8539 break;
8540 #endif
8541 #ifdef INET6
8542 case AF_INET6:
8543 if (net->mtu > SCTP_MIN_OVERHEAD)
8544 omtu = net->mtu - SCTP_MIN_OVERHEAD;
8545 else
8546 omtu = 0;
8547 break;
8548 #endif
8549 default:
8550 /* TSNH */
8551 omtu = 0;
8552 break;
8553 }
8554 if ((((SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) ||
8555 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED)) &&
8556 (skip_data_for_this_net == 0)) ||
8557 (cookie)) {
8558 TAILQ_FOREACH_SAFE(chk, &asoc->send_queue, sctp_next, nchk) {
8559 if (no_data_chunks) {
8560 /* let only control go out */
8561 *reason_code = 1;
8562 break;
8563 }
8564 if (net->flight_size >= net->cwnd) {
8565 /* skip this net, no room for data */
8566 *reason_code = 2;
8567 break;
8568 }
8569 if ((chk->whoTo != NULL) &&
8570 (chk->whoTo != net)) {
8571 /* Don't send the chunk on this net */
8572 continue;
8573 }
8574
8575 if (asoc->sctp_cmt_on_off == 0) {
8576 if ((asoc->alternate) &&
8577 (asoc->alternate != net) &&
8578 (chk->whoTo == NULL)) {
8579 continue;
8580 } else if ((net != asoc->primary_destination) &&
8581 (asoc->alternate == NULL) &&
8582 (chk->whoTo == NULL)) {
8583 continue;
8584 }
8585 }
8586 if ((chk->send_size > omtu) && ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) == 0)) {
8587 /*-
8588 * strange, we have a chunk that is
8589 * to big for its destination and
8590 * yet no fragment ok flag.
8591 * Something went wrong when the
8592 * PMTU changed...we did not mark
8593 * this chunk for some reason?? I
8594 * will fix it here by letting IP
8595 * fragment it for now and printing
8596 * a warning. This really should not
8597 * happen ...
8598 */
8599 SCTP_PRINTF("Warning chunk of %d bytes > mtu:%d and yet PMTU disc missed\n",
8600 chk->send_size, mtu);
8601 chk->flags |= CHUNK_FLAGS_FRAGMENT_OK;
8602 }
8603 if (SCTP_BASE_SYSCTL(sctp_enable_sack_immediately) &&
8604 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
8605 struct sctp_data_chunk *dchkh;
8606
8607 dchkh = mtod(chk->data, struct sctp_data_chunk *);
8608 dchkh->ch.chunk_flags |= SCTP_DATA_SACK_IMMEDIATELY;
8609 }
8610 if (((chk->send_size <= mtu) && (chk->send_size <= r_mtu)) ||
8611 ((chk->flags & CHUNK_FLAGS_FRAGMENT_OK) && (chk->send_size <= asoc->peers_rwnd))) {
8612 /* ok we will add this one */
8613
8614 /*
8615 * Add an AUTH chunk, if chunk
8616 * requires it, save the offset into
8617 * the chain for AUTH
8618 */
8619 if (data_auth_reqd) {
8620 if (auth == NULL) {
8621 outchain = sctp_add_auth_chunk(outchain,
8622 &endoutchain,
8623 &auth,
8624 &auth_offset,
8625 stcb,
8626 SCTP_DATA);
8627 auth_keyid = chk->auth_keyid;
8628 override_ok = 0;
8629 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
8630 } else if (override_ok) {
8631 /*
8632 * use this data's
8633 * keyid
8634 */
8635 auth_keyid = chk->auth_keyid;
8636 override_ok = 0;
8637 } else if (auth_keyid != chk->auth_keyid) {
8638 /*
8639 * different keyid,
8640 * so done bundling
8641 */
8642 break;
8643 }
8644 }
8645 outchain = sctp_copy_mbufchain(chk->data, outchain, &endoutchain, 0,
8646 chk->send_size, chk->copy_by_ref);
8647 if (outchain == NULL) {
8648 SCTPDBG(SCTP_DEBUG_OUTPUT3, "No memory?\n");
8649 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
8650 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8651 }
8652 *reason_code = 3;
8653 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
8654 return (ENOMEM);
8655 }
8656 /* update our MTU size */
8657 /* Do clear IP_DF ? */
8658 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
8659 no_fragmentflg = 0;
8660 }
8661 /* unsigned subtraction of mtu */
8662 if (mtu > chk->send_size)
8663 mtu -= chk->send_size;
8664 else
8665 mtu = 0;
8666 /* unsigned subtraction of r_mtu */
8667 if (r_mtu > chk->send_size)
8668 r_mtu -= chk->send_size;
8669 else
8670 r_mtu = 0;
8671
8672 to_out += chk->send_size;
8673 if ((to_out > mx_mtu) && no_fragmentflg) {
8674 #ifdef INVARIANTS
8675 panic("Exceeding mtu of %d out size is %d", mx_mtu, to_out);
8676 #else
8677 SCTP_PRINTF("Exceeding mtu of %d out size is %d\n",
8678 mx_mtu, to_out);
8679 #endif
8680 }
8681 chk->window_probe = 0;
8682 data_list[bundle_at++] = chk;
8683 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
8684 break;
8685 }
8686 if (chk->sent == SCTP_DATAGRAM_UNSENT) {
8687 if ((chk->rec.data.rcv_flags & SCTP_DATA_UNORDERED) == 0) {
8688 SCTP_STAT_INCR_COUNTER64(sctps_outorderchunks);
8689 } else {
8690 SCTP_STAT_INCR_COUNTER64(sctps_outunorderchunks);
8691 }
8692 if (((chk->rec.data.rcv_flags & SCTP_DATA_LAST_FRAG) == SCTP_DATA_LAST_FRAG) &&
8693 ((chk->rec.data.rcv_flags & SCTP_DATA_FIRST_FRAG) == 0))
8694 /*
8695 * Count number of
8696 * user msg's that
8697 * were fragmented
8698 * we do this by
8699 * counting when we
8700 * see a LAST
8701 * fragment only.
8702 */
8703 SCTP_STAT_INCR_COUNTER64(sctps_fragusrmsgs);
8704 }
8705 if ((mtu == 0) || (r_mtu == 0) || (one_chunk)) {
8706 if ((one_chunk) && (stcb->asoc.total_flight == 0)) {
8707 data_list[0]->window_probe = 1;
8708 net->window_probe = 1;
8709 }
8710 break;
8711 }
8712 } else {
8713 /*
8714 * Must be sent in order of the
8715 * TSN's (on a network)
8716 */
8717 break;
8718 }
8719 } /* for (chunk gather loop for this net) */
8720 } /* if asoc.state OPEN */
8721 no_data_fill:
8722 /* Is there something to send for this destination? */
8723 if (outchain) {
8724 /* We may need to start a control timer or two */
8725 if (asconf) {
8726 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp,
8727 stcb, net);
8728 /*
8729 * do NOT clear the asconf flag as it is
8730 * used to do appropriate source address
8731 * selection.
8732 */
8733 }
8734 if (cookie) {
8735 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, net);
8736 cookie = 0;
8737 }
8738 /* must start a send timer if data is being sent */
8739 if (bundle_at && (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer))) {
8740 /*
8741 * no timer running on this destination
8742 * restart it.
8743 */
8744 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
8745 }
8746 if (bundle_at || hbflag) {
8747 /* For data/asconf and hb set time */
8748 if (*now_filled == 0) {
8749 (void)SCTP_GETTIME_TIMEVAL(now);
8750 *now_filled = 1;
8751 }
8752 net->last_sent_time = *now;
8753 }
8754 /* Now send it, if there is anything to send :> */
8755 if ((error = sctp_lowlevel_chunk_output(inp,
8756 stcb,
8757 net,
8758 (struct sockaddr *)&net->ro._l_addr,
8759 outchain,
8760 auth_offset,
8761 auth,
8762 auth_keyid,
8763 no_fragmentflg,
8764 bundle_at,
8765 asconf,
8766 inp->sctp_lport, stcb->rport,
8767 htonl(stcb->asoc.peer_vtag),
8768 net->port, NULL,
8769 0, 0,
8770 so_locked))) {
8771 /* error, we could not output */
8772 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
8773 if (from_where == 0) {
8774 SCTP_STAT_INCR(sctps_lowlevelerrusr);
8775 }
8776 if (error == ENOBUFS) {
8777 asoc->ifp_had_enobuf = 1;
8778 SCTP_STAT_INCR(sctps_lowlevelerr);
8779 }
8780 if (error == EHOSTUNREACH) {
8781 /*
8782 * Destination went unreachable
8783 * during this send
8784 */
8785 sctp_move_chunks_from_net(stcb, net);
8786 }
8787 *reason_code = 6;
8788 /*-
8789 * I add this line to be paranoid. As far as
8790 * I can tell the continue, takes us back to
8791 * the top of the for, but just to make sure
8792 * I will reset these again here.
8793 */
8794 ctl_cnt = 0;
8795 continue; /* This takes us back to the
8796 * for() for the nets. */
8797 } else {
8798 asoc->ifp_had_enobuf = 0;
8799 }
8800 endoutchain = NULL;
8801 auth = NULL;
8802 auth_offset = 0;
8803 if (!no_out_cnt) {
8804 *num_out += (ctl_cnt + bundle_at);
8805 }
8806 if (bundle_at) {
8807 /* setup for a RTO measurement */
8808 tsns_sent = data_list[0]->rec.data.tsn;
8809 /* fill time if not already filled */
8810 if (*now_filled == 0) {
8811 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
8812 *now_filled = 1;
8813 *now = asoc->time_last_sent;
8814 } else {
8815 asoc->time_last_sent = *now;
8816 }
8817 if (net->rto_needed) {
8818 data_list[0]->do_rtt = 1;
8819 net->rto_needed = 0;
8820 }
8821 SCTP_STAT_INCR_BY(sctps_senddata, bundle_at);
8822 sctp_clean_up_datalist(stcb, asoc, data_list, bundle_at, net);
8823 }
8824 if (one_chunk) {
8825 break;
8826 }
8827 }
8828 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8829 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_SEND);
8830 }
8831 }
8832 if (old_start_at == NULL) {
8833 old_start_at = start_at;
8834 start_at = TAILQ_FIRST(&asoc->nets);
8835 if (old_start_at)
8836 goto again_one_more_time;
8837 }
8838
8839 /*
8840 * At the end there should be no NON timed chunks hanging on this
8841 * queue.
8842 */
8843 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
8844 sctp_log_cwnd(stcb, net, *num_out, SCTP_CWND_LOG_FROM_SEND);
8845 }
8846 if ((*num_out == 0) && (*reason_code == 0)) {
8847 *reason_code = 4;
8848 } else {
8849 *reason_code = 5;
8850 }
8851 sctp_clean_up_ctl(stcb, asoc, so_locked);
8852 return (0);
8853 }
8854
8855 void
sctp_queue_op_err(struct sctp_tcb * stcb,struct mbuf * op_err)8856 sctp_queue_op_err(struct sctp_tcb *stcb, struct mbuf *op_err)
8857 {
8858 /*-
8859 * Prepend a OPERATIONAL_ERROR chunk header and put on the end of
8860 * the control chunk queue.
8861 */
8862 struct sctp_chunkhdr *hdr;
8863 struct sctp_tmit_chunk *chk;
8864 struct mbuf *mat, *last_mbuf;
8865 uint32_t chunk_length;
8866 uint16_t padding_length;
8867
8868 SCTP_TCB_LOCK_ASSERT(stcb);
8869 SCTP_BUF_PREPEND(op_err, sizeof(struct sctp_chunkhdr), M_NOWAIT);
8870 if (op_err == NULL) {
8871 return;
8872 }
8873 last_mbuf = NULL;
8874 chunk_length = 0;
8875 for (mat = op_err; mat != NULL; mat = SCTP_BUF_NEXT(mat)) {
8876 chunk_length += SCTP_BUF_LEN(mat);
8877 if (SCTP_BUF_NEXT(mat) == NULL) {
8878 last_mbuf = mat;
8879 }
8880 }
8881 if (chunk_length > SCTP_MAX_CHUNK_LENGTH) {
8882 sctp_m_freem(op_err);
8883 return;
8884 }
8885 padding_length = chunk_length % 4;
8886 if (padding_length != 0) {
8887 padding_length = 4 - padding_length;
8888 }
8889 if (padding_length != 0) {
8890 if (sctp_add_pad_tombuf(last_mbuf, padding_length) == NULL) {
8891 sctp_m_freem(op_err);
8892 return;
8893 }
8894 }
8895 sctp_alloc_a_chunk(stcb, chk);
8896 if (chk == NULL) {
8897 /* no memory */
8898 sctp_m_freem(op_err);
8899 return;
8900 }
8901 chk->copy_by_ref = 0;
8902 chk->rec.chunk_id.id = SCTP_OPERATION_ERROR;
8903 chk->rec.chunk_id.can_take_data = 0;
8904 chk->flags = 0;
8905 chk->send_size = (uint16_t)chunk_length;
8906 chk->sent = SCTP_DATAGRAM_UNSENT;
8907 chk->snd_count = 0;
8908 chk->asoc = &stcb->asoc;
8909 chk->data = op_err;
8910 chk->whoTo = NULL;
8911 hdr = mtod(op_err, struct sctp_chunkhdr *);
8912 hdr->chunk_type = SCTP_OPERATION_ERROR;
8913 hdr->chunk_flags = 0;
8914 hdr->chunk_length = htons(chk->send_size);
8915 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
8916 chk->asoc->ctrl_queue_cnt++;
8917 }
8918
8919 int
sctp_send_cookie_echo(struct mbuf * m,int offset,int limit,struct sctp_tcb * stcb,struct sctp_nets * net)8920 sctp_send_cookie_echo(struct mbuf *m,
8921 int offset, int limit,
8922 struct sctp_tcb *stcb,
8923 struct sctp_nets *net)
8924 {
8925 /*-
8926 * pull out the cookie and put it at the front of the control chunk
8927 * queue.
8928 */
8929 int at;
8930 struct mbuf *cookie;
8931 struct sctp_paramhdr param, *phdr;
8932 struct sctp_chunkhdr *hdr;
8933 struct sctp_tmit_chunk *chk;
8934 uint16_t ptype, plen;
8935
8936 SCTP_TCB_LOCK_ASSERT(stcb);
8937 /* First find the cookie in the param area */
8938 cookie = NULL;
8939 at = offset + sizeof(struct sctp_init_chunk);
8940 for (;;) {
8941 phdr = sctp_get_next_param(m, at, ¶m, sizeof(param));
8942 if (phdr == NULL) {
8943 return (-3);
8944 }
8945 ptype = ntohs(phdr->param_type);
8946 plen = ntohs(phdr->param_length);
8947 if (plen < sizeof(struct sctp_paramhdr)) {
8948 return (-6);
8949 }
8950 if (ptype == SCTP_STATE_COOKIE) {
8951 int pad;
8952
8953 /* found the cookie */
8954 if (at + plen > limit) {
8955 return (-7);
8956 }
8957 cookie = SCTP_M_COPYM(m, at, plen, M_NOWAIT);
8958 if (cookie == NULL) {
8959 /* No memory */
8960 return (-2);
8961 }
8962 if ((pad = (plen % 4)) > 0) {
8963 pad = 4 - pad;
8964 }
8965 if (pad > 0) {
8966 if (sctp_pad_lastmbuf(cookie, pad, NULL) == NULL) {
8967 return (-8);
8968 }
8969 }
8970 #ifdef SCTP_MBUF_LOGGING
8971 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
8972 sctp_log_mbc(cookie, SCTP_MBUF_ICOPY);
8973 }
8974 #endif
8975 break;
8976 }
8977 at += SCTP_SIZE32(plen);
8978 }
8979 /* ok, we got the cookie lets change it into a cookie echo chunk */
8980 /* first the change from param to cookie */
8981 hdr = mtod(cookie, struct sctp_chunkhdr *);
8982 hdr->chunk_type = SCTP_COOKIE_ECHO;
8983 hdr->chunk_flags = 0;
8984 /* get the chunk stuff now and place it in the FRONT of the queue */
8985 sctp_alloc_a_chunk(stcb, chk);
8986 if (chk == NULL) {
8987 /* no memory */
8988 sctp_m_freem(cookie);
8989 return (-5);
8990 }
8991 chk->copy_by_ref = 0;
8992 chk->rec.chunk_id.id = SCTP_COOKIE_ECHO;
8993 chk->rec.chunk_id.can_take_data = 0;
8994 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
8995 chk->send_size = SCTP_SIZE32(plen);
8996 chk->sent = SCTP_DATAGRAM_UNSENT;
8997 chk->snd_count = 0;
8998 chk->asoc = &stcb->asoc;
8999 chk->data = cookie;
9000 chk->whoTo = net;
9001 atomic_add_int(&chk->whoTo->ref_count, 1);
9002 TAILQ_INSERT_HEAD(&chk->asoc->control_send_queue, chk, sctp_next);
9003 chk->asoc->ctrl_queue_cnt++;
9004 return (0);
9005 }
9006
9007 void
sctp_send_heartbeat_ack(struct sctp_tcb * stcb,struct mbuf * m,int offset,int chk_length,struct sctp_nets * net)9008 sctp_send_heartbeat_ack(struct sctp_tcb *stcb,
9009 struct mbuf *m,
9010 int offset,
9011 int chk_length,
9012 struct sctp_nets *net)
9013 {
9014 /*
9015 * take a HB request and make it into a HB ack and send it.
9016 */
9017 struct mbuf *outchain;
9018 struct sctp_chunkhdr *chdr;
9019 struct sctp_tmit_chunk *chk;
9020
9021 if (net == NULL)
9022 /* must have a net pointer */
9023 return;
9024
9025 outchain = SCTP_M_COPYM(m, offset, chk_length, M_NOWAIT);
9026 if (outchain == NULL) {
9027 /* gak out of memory */
9028 return;
9029 }
9030 #ifdef SCTP_MBUF_LOGGING
9031 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9032 sctp_log_mbc(outchain, SCTP_MBUF_ICOPY);
9033 }
9034 #endif
9035 chdr = mtod(outchain, struct sctp_chunkhdr *);
9036 chdr->chunk_type = SCTP_HEARTBEAT_ACK;
9037 chdr->chunk_flags = 0;
9038 if (chk_length % 4 != 0) {
9039 sctp_pad_lastmbuf(outchain, 4 - (chk_length % 4), NULL);
9040 }
9041 sctp_alloc_a_chunk(stcb, chk);
9042 if (chk == NULL) {
9043 /* no memory */
9044 sctp_m_freem(outchain);
9045 return;
9046 }
9047 chk->copy_by_ref = 0;
9048 chk->rec.chunk_id.id = SCTP_HEARTBEAT_ACK;
9049 chk->rec.chunk_id.can_take_data = 1;
9050 chk->flags = 0;
9051 chk->send_size = chk_length;
9052 chk->sent = SCTP_DATAGRAM_UNSENT;
9053 chk->snd_count = 0;
9054 chk->asoc = &stcb->asoc;
9055 chk->data = outchain;
9056 chk->whoTo = net;
9057 atomic_add_int(&chk->whoTo->ref_count, 1);
9058 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9059 chk->asoc->ctrl_queue_cnt++;
9060 }
9061
9062 void
sctp_send_cookie_ack(struct sctp_tcb * stcb)9063 sctp_send_cookie_ack(struct sctp_tcb *stcb)
9064 {
9065 /* formulate and queue a cookie-ack back to sender */
9066 struct mbuf *cookie_ack;
9067 struct sctp_chunkhdr *hdr;
9068 struct sctp_tmit_chunk *chk;
9069
9070 SCTP_TCB_LOCK_ASSERT(stcb);
9071
9072 cookie_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
9073 if (cookie_ack == NULL) {
9074 /* no mbuf's */
9075 return;
9076 }
9077 SCTP_BUF_RESV_UF(cookie_ack, SCTP_MIN_OVERHEAD);
9078 sctp_alloc_a_chunk(stcb, chk);
9079 if (chk == NULL) {
9080 /* no memory */
9081 sctp_m_freem(cookie_ack);
9082 return;
9083 }
9084 chk->copy_by_ref = 0;
9085 chk->rec.chunk_id.id = SCTP_COOKIE_ACK;
9086 chk->rec.chunk_id.can_take_data = 1;
9087 chk->flags = 0;
9088 chk->send_size = sizeof(struct sctp_chunkhdr);
9089 chk->sent = SCTP_DATAGRAM_UNSENT;
9090 chk->snd_count = 0;
9091 chk->asoc = &stcb->asoc;
9092 chk->data = cookie_ack;
9093 if (chk->asoc->last_control_chunk_from != NULL) {
9094 chk->whoTo = chk->asoc->last_control_chunk_from;
9095 atomic_add_int(&chk->whoTo->ref_count, 1);
9096 } else {
9097 chk->whoTo = NULL;
9098 }
9099 hdr = mtod(cookie_ack, struct sctp_chunkhdr *);
9100 hdr->chunk_type = SCTP_COOKIE_ACK;
9101 hdr->chunk_flags = 0;
9102 hdr->chunk_length = htons(chk->send_size);
9103 SCTP_BUF_LEN(cookie_ack) = chk->send_size;
9104 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9105 chk->asoc->ctrl_queue_cnt++;
9106 return;
9107 }
9108
9109 void
sctp_send_shutdown_ack(struct sctp_tcb * stcb,struct sctp_nets * net)9110 sctp_send_shutdown_ack(struct sctp_tcb *stcb, struct sctp_nets *net)
9111 {
9112 /* formulate and queue a SHUTDOWN-ACK back to the sender */
9113 struct mbuf *m_shutdown_ack;
9114 struct sctp_shutdown_ack_chunk *ack_cp;
9115 struct sctp_tmit_chunk *chk;
9116
9117 m_shutdown_ack = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_ack_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9118 if (m_shutdown_ack == NULL) {
9119 /* no mbuf's */
9120 return;
9121 }
9122 SCTP_BUF_RESV_UF(m_shutdown_ack, SCTP_MIN_OVERHEAD);
9123 sctp_alloc_a_chunk(stcb, chk);
9124 if (chk == NULL) {
9125 /* no memory */
9126 sctp_m_freem(m_shutdown_ack);
9127 return;
9128 }
9129 chk->copy_by_ref = 0;
9130 chk->rec.chunk_id.id = SCTP_SHUTDOWN_ACK;
9131 chk->rec.chunk_id.can_take_data = 1;
9132 chk->flags = 0;
9133 chk->send_size = sizeof(struct sctp_chunkhdr);
9134 chk->sent = SCTP_DATAGRAM_UNSENT;
9135 chk->snd_count = 0;
9136 chk->asoc = &stcb->asoc;
9137 chk->data = m_shutdown_ack;
9138 chk->whoTo = net;
9139 if (chk->whoTo) {
9140 atomic_add_int(&chk->whoTo->ref_count, 1);
9141 }
9142 ack_cp = mtod(m_shutdown_ack, struct sctp_shutdown_ack_chunk *);
9143 ack_cp->ch.chunk_type = SCTP_SHUTDOWN_ACK;
9144 ack_cp->ch.chunk_flags = 0;
9145 ack_cp->ch.chunk_length = htons(chk->send_size);
9146 SCTP_BUF_LEN(m_shutdown_ack) = chk->send_size;
9147 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9148 chk->asoc->ctrl_queue_cnt++;
9149 return;
9150 }
9151
9152 void
sctp_send_shutdown(struct sctp_tcb * stcb,struct sctp_nets * net)9153 sctp_send_shutdown(struct sctp_tcb *stcb, struct sctp_nets *net)
9154 {
9155 /* formulate and queue a SHUTDOWN to the sender */
9156 struct mbuf *m_shutdown;
9157 struct sctp_shutdown_chunk *shutdown_cp;
9158 struct sctp_tmit_chunk *chk;
9159
9160 TAILQ_FOREACH(chk, &stcb->asoc.control_send_queue, sctp_next) {
9161 if (chk->rec.chunk_id.id == SCTP_SHUTDOWN) {
9162 /* We already have a SHUTDOWN queued. Reuse it. */
9163 if (chk->whoTo) {
9164 sctp_free_remote_addr(chk->whoTo);
9165 chk->whoTo = NULL;
9166 }
9167 break;
9168 }
9169 }
9170 if (chk == NULL) {
9171 m_shutdown = sctp_get_mbuf_for_msg(sizeof(struct sctp_shutdown_chunk), 0, M_NOWAIT, 1, MT_HEADER);
9172 if (m_shutdown == NULL) {
9173 /* no mbuf's */
9174 return;
9175 }
9176 SCTP_BUF_RESV_UF(m_shutdown, SCTP_MIN_OVERHEAD);
9177 sctp_alloc_a_chunk(stcb, chk);
9178 if (chk == NULL) {
9179 /* no memory */
9180 sctp_m_freem(m_shutdown);
9181 return;
9182 }
9183 chk->copy_by_ref = 0;
9184 chk->rec.chunk_id.id = SCTP_SHUTDOWN;
9185 chk->rec.chunk_id.can_take_data = 1;
9186 chk->flags = 0;
9187 chk->send_size = sizeof(struct sctp_shutdown_chunk);
9188 chk->sent = SCTP_DATAGRAM_UNSENT;
9189 chk->snd_count = 0;
9190 chk->asoc = &stcb->asoc;
9191 chk->data = m_shutdown;
9192 chk->whoTo = net;
9193 if (chk->whoTo) {
9194 atomic_add_int(&chk->whoTo->ref_count, 1);
9195 }
9196 shutdown_cp = mtod(m_shutdown, struct sctp_shutdown_chunk *);
9197 shutdown_cp->ch.chunk_type = SCTP_SHUTDOWN;
9198 shutdown_cp->ch.chunk_flags = 0;
9199 shutdown_cp->ch.chunk_length = htons(chk->send_size);
9200 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9201 SCTP_BUF_LEN(m_shutdown) = chk->send_size;
9202 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9203 chk->asoc->ctrl_queue_cnt++;
9204 } else {
9205 TAILQ_REMOVE(&stcb->asoc.control_send_queue, chk, sctp_next);
9206 chk->whoTo = net;
9207 if (chk->whoTo) {
9208 atomic_add_int(&chk->whoTo->ref_count, 1);
9209 }
9210 shutdown_cp = mtod(chk->data, struct sctp_shutdown_chunk *);
9211 shutdown_cp->cumulative_tsn_ack = htonl(stcb->asoc.cumulative_tsn);
9212 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
9213 }
9214 return;
9215 }
9216
9217 void
sctp_send_asconf(struct sctp_tcb * stcb,struct sctp_nets * net,int addr_locked)9218 sctp_send_asconf(struct sctp_tcb *stcb, struct sctp_nets *net, int addr_locked)
9219 {
9220 /*
9221 * formulate and queue an ASCONF to the peer. ASCONF parameters
9222 * should be queued on the assoc queue.
9223 */
9224 struct sctp_tmit_chunk *chk;
9225 struct mbuf *m_asconf;
9226 int len;
9227
9228 SCTP_TCB_LOCK_ASSERT(stcb);
9229
9230 if ((!TAILQ_EMPTY(&stcb->asoc.asconf_send_queue)) &&
9231 (!sctp_is_feature_on(stcb->sctp_ep, SCTP_PCB_FLAGS_MULTIPLE_ASCONFS))) {
9232 /* can't send a new one if there is one in flight already */
9233 return;
9234 }
9235
9236 /* compose an ASCONF chunk, maximum length is PMTU */
9237 m_asconf = sctp_compose_asconf(stcb, &len, addr_locked);
9238 if (m_asconf == NULL) {
9239 return;
9240 }
9241
9242 sctp_alloc_a_chunk(stcb, chk);
9243 if (chk == NULL) {
9244 /* no memory */
9245 sctp_m_freem(m_asconf);
9246 return;
9247 }
9248
9249 chk->copy_by_ref = 0;
9250 chk->rec.chunk_id.id = SCTP_ASCONF;
9251 chk->rec.chunk_id.can_take_data = 0;
9252 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9253 chk->data = m_asconf;
9254 chk->send_size = len;
9255 chk->sent = SCTP_DATAGRAM_UNSENT;
9256 chk->snd_count = 0;
9257 chk->asoc = &stcb->asoc;
9258 chk->whoTo = net;
9259 if (chk->whoTo) {
9260 atomic_add_int(&chk->whoTo->ref_count, 1);
9261 }
9262 TAILQ_INSERT_TAIL(&chk->asoc->asconf_send_queue, chk, sctp_next);
9263 chk->asoc->ctrl_queue_cnt++;
9264 return;
9265 }
9266
9267 void
sctp_send_asconf_ack(struct sctp_tcb * stcb)9268 sctp_send_asconf_ack(struct sctp_tcb *stcb)
9269 {
9270 /*
9271 * formulate and queue a asconf-ack back to sender. the asconf-ack
9272 * must be stored in the tcb.
9273 */
9274 struct sctp_tmit_chunk *chk;
9275 struct sctp_asconf_ack *ack, *latest_ack;
9276 struct mbuf *m_ack;
9277 struct sctp_nets *net = NULL;
9278
9279 SCTP_TCB_LOCK_ASSERT(stcb);
9280 /* Get the latest ASCONF-ACK */
9281 latest_ack = TAILQ_LAST(&stcb->asoc.asconf_ack_sent, sctp_asconf_ackhead);
9282 if (latest_ack == NULL) {
9283 return;
9284 }
9285 if (latest_ack->last_sent_to != NULL &&
9286 latest_ack->last_sent_to == stcb->asoc.last_control_chunk_from) {
9287 /* we're doing a retransmission */
9288 net = sctp_find_alternate_net(stcb, stcb->asoc.last_control_chunk_from, 0);
9289 if (net == NULL) {
9290 /* no alternate */
9291 if (stcb->asoc.last_control_chunk_from == NULL) {
9292 if (stcb->asoc.alternate) {
9293 net = stcb->asoc.alternate;
9294 } else {
9295 net = stcb->asoc.primary_destination;
9296 }
9297 } else {
9298 net = stcb->asoc.last_control_chunk_from;
9299 }
9300 }
9301 } else {
9302 /* normal case */
9303 if (stcb->asoc.last_control_chunk_from == NULL) {
9304 if (stcb->asoc.alternate) {
9305 net = stcb->asoc.alternate;
9306 } else {
9307 net = stcb->asoc.primary_destination;
9308 }
9309 } else {
9310 net = stcb->asoc.last_control_chunk_from;
9311 }
9312 }
9313 latest_ack->last_sent_to = net;
9314
9315 TAILQ_FOREACH(ack, &stcb->asoc.asconf_ack_sent, next) {
9316 if (ack->data == NULL) {
9317 continue;
9318 }
9319
9320 /* copy the asconf_ack */
9321 m_ack = SCTP_M_COPYM(ack->data, 0, M_COPYALL, M_NOWAIT);
9322 if (m_ack == NULL) {
9323 /* couldn't copy it */
9324 return;
9325 }
9326 #ifdef SCTP_MBUF_LOGGING
9327 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_MBUF_LOGGING_ENABLE) {
9328 sctp_log_mbc(m_ack, SCTP_MBUF_ICOPY);
9329 }
9330 #endif
9331
9332 sctp_alloc_a_chunk(stcb, chk);
9333 if (chk == NULL) {
9334 /* no memory */
9335 if (m_ack)
9336 sctp_m_freem(m_ack);
9337 return;
9338 }
9339 chk->copy_by_ref = 0;
9340 chk->rec.chunk_id.id = SCTP_ASCONF_ACK;
9341 chk->rec.chunk_id.can_take_data = 1;
9342 chk->flags = CHUNK_FLAGS_FRAGMENT_OK;
9343 chk->whoTo = net;
9344 if (chk->whoTo) {
9345 atomic_add_int(&chk->whoTo->ref_count, 1);
9346 }
9347 chk->data = m_ack;
9348 chk->send_size = ack->len;
9349 chk->sent = SCTP_DATAGRAM_UNSENT;
9350 chk->snd_count = 0;
9351 chk->asoc = &stcb->asoc;
9352
9353 TAILQ_INSERT_TAIL(&chk->asoc->control_send_queue, chk, sctp_next);
9354 chk->asoc->ctrl_queue_cnt++;
9355 }
9356 return;
9357 }
9358
9359 static int
sctp_chunk_retransmission(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_association * asoc,int * cnt_out,struct timeval * now,int * now_filled,int * fr_done,int so_locked)9360 sctp_chunk_retransmission(struct sctp_inpcb *inp,
9361 struct sctp_tcb *stcb,
9362 struct sctp_association *asoc,
9363 int *cnt_out, struct timeval *now, int *now_filled, int *fr_done, int so_locked)
9364 {
9365 /*-
9366 * send out one MTU of retransmission. If fast_retransmit is
9367 * happening we ignore the cwnd. Otherwise we obey the cwnd and
9368 * rwnd. For a Cookie or Asconf in the control chunk queue we
9369 * retransmit them by themselves.
9370 *
9371 * For data chunks we will pick out the lowest TSN's in the sent_queue
9372 * marked for resend and bundle them all together (up to a MTU of
9373 * destination). The address to send to should have been
9374 * selected/changed where the retransmission was marked (i.e. in FR
9375 * or t3-timeout routines).
9376 */
9377 struct sctp_tmit_chunk *data_list[SCTP_MAX_DATA_BUNDLING];
9378 struct sctp_tmit_chunk *chk, *fwd;
9379 struct mbuf *m, *endofchain;
9380 struct sctp_nets *net = NULL;
9381 uint32_t tsns_sent = 0;
9382 int no_fragmentflg, bundle_at;
9383 unsigned int mtu;
9384 int error, i, one_chunk, fwd_tsn, ctl_cnt, tmr_started;
9385 struct sctp_auth_chunk *auth = NULL;
9386 uint32_t auth_offset = 0;
9387 uint16_t auth_keyid;
9388 int override_ok = 1;
9389 int data_auth_reqd = 0;
9390 uint32_t dmtu = 0;
9391
9392 SCTP_TCB_LOCK_ASSERT(stcb);
9393 tmr_started = ctl_cnt = 0;
9394 no_fragmentflg = 1;
9395 fwd_tsn = 0;
9396 *cnt_out = 0;
9397 fwd = NULL;
9398 endofchain = m = NULL;
9399 auth_keyid = stcb->asoc.authinfo.active_keyid;
9400 #ifdef SCTP_AUDITING_ENABLED
9401 sctp_audit_log(0xC3, 1);
9402 #endif
9403 if ((TAILQ_EMPTY(&asoc->sent_queue)) &&
9404 (TAILQ_EMPTY(&asoc->control_send_queue))) {
9405 SCTPDBG(SCTP_DEBUG_OUTPUT1, "SCTP hits empty queue with cnt set to %d?\n",
9406 asoc->sent_queue_retran_cnt);
9407 asoc->sent_queue_cnt = 0;
9408 asoc->sent_queue_cnt_removeable = 0;
9409 /* send back 0/0 so we enter normal transmission */
9410 *cnt_out = 0;
9411 return (0);
9412 }
9413 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
9414 if ((chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) ||
9415 (chk->rec.chunk_id.id == SCTP_STREAM_RESET) ||
9416 (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN)) {
9417 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9418 continue;
9419 }
9420 if (chk->rec.chunk_id.id == SCTP_STREAM_RESET) {
9421 if (chk != asoc->str_reset) {
9422 /*
9423 * not eligible for retran if its
9424 * not ours
9425 */
9426 continue;
9427 }
9428 }
9429 ctl_cnt++;
9430 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
9431 fwd_tsn = 1;
9432 }
9433 /*
9434 * Add an AUTH chunk, if chunk requires it save the
9435 * offset into the chain for AUTH
9436 */
9437 if ((auth == NULL) &&
9438 (sctp_auth_is_required_chunk(chk->rec.chunk_id.id,
9439 stcb->asoc.peer_auth_chunks))) {
9440 m = sctp_add_auth_chunk(m, &endofchain,
9441 &auth, &auth_offset,
9442 stcb,
9443 chk->rec.chunk_id.id);
9444 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9445 }
9446 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9447 break;
9448 }
9449 }
9450 one_chunk = 0;
9451 /* do we have control chunks to retransmit? */
9452 if (m != NULL) {
9453 /* Start a timer no matter if we succeed or fail */
9454 if (chk->rec.chunk_id.id == SCTP_COOKIE_ECHO) {
9455 sctp_timer_start(SCTP_TIMER_TYPE_COOKIE, inp, stcb, chk->whoTo);
9456 } else if (chk->rec.chunk_id.id == SCTP_ASCONF)
9457 sctp_timer_start(SCTP_TIMER_TYPE_ASCONF, inp, stcb, chk->whoTo);
9458 chk->snd_count++; /* update our count */
9459 if ((error = sctp_lowlevel_chunk_output(inp, stcb, chk->whoTo,
9460 (struct sockaddr *)&chk->whoTo->ro._l_addr, m,
9461 auth_offset, auth, stcb->asoc.authinfo.active_keyid,
9462 no_fragmentflg, 0, 0,
9463 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9464 chk->whoTo->port, NULL,
9465 0, 0,
9466 so_locked))) {
9467 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9468 if (error == ENOBUFS) {
9469 asoc->ifp_had_enobuf = 1;
9470 SCTP_STAT_INCR(sctps_lowlevelerr);
9471 }
9472 return (error);
9473 } else {
9474 asoc->ifp_had_enobuf = 0;
9475 }
9476 endofchain = NULL;
9477 auth = NULL;
9478 auth_offset = 0;
9479 /*
9480 * We don't want to mark the net->sent time here since this
9481 * we use this for HB and retrans cannot measure RTT
9482 */
9483 /* (void)SCTP_GETTIME_TIMEVAL(&chk->whoTo->last_sent_time); */
9484 *cnt_out += 1;
9485 chk->sent = SCTP_DATAGRAM_SENT;
9486 sctp_ucount_decr(stcb->asoc.sent_queue_retran_cnt);
9487 if (fwd_tsn == 0) {
9488 return (0);
9489 } else {
9490 /* Clean up the fwd-tsn list */
9491 sctp_clean_up_ctl(stcb, asoc, so_locked);
9492 return (0);
9493 }
9494 }
9495 /*
9496 * Ok, it is just data retransmission we need to do or that and a
9497 * fwd-tsn with it all.
9498 */
9499 if (TAILQ_EMPTY(&asoc->sent_queue)) {
9500 return (SCTP_RETRAN_DONE);
9501 }
9502 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED) ||
9503 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT)) {
9504 /* not yet open, resend the cookie and that is it */
9505 return (1);
9506 }
9507 #ifdef SCTP_AUDITING_ENABLED
9508 sctp_auditing(20, inp, stcb, NULL);
9509 #endif
9510 data_auth_reqd = sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks);
9511 TAILQ_FOREACH(chk, &asoc->sent_queue, sctp_next) {
9512 if (chk->sent != SCTP_DATAGRAM_RESEND) {
9513 /* No, not sent to this net or not ready for rtx */
9514 continue;
9515 }
9516 if (chk->data == NULL) {
9517 SCTP_PRINTF("TSN:%x chk->snd_count:%d chk->sent:%d can't retran - no data\n",
9518 chk->rec.data.tsn, chk->snd_count, chk->sent);
9519 continue;
9520 }
9521 if ((SCTP_BASE_SYSCTL(sctp_max_retran_chunk)) &&
9522 (chk->snd_count >= SCTP_BASE_SYSCTL(sctp_max_retran_chunk))) {
9523 struct mbuf *op_err;
9524 char msg[SCTP_DIAG_INFO_LEN];
9525
9526 SCTP_SNPRINTF(msg, sizeof(msg), "TSN %8.8x retransmitted %d times, giving up",
9527 chk->rec.data.tsn, chk->snd_count);
9528 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
9529 msg);
9530 atomic_add_int(&stcb->asoc.refcnt, 1);
9531 sctp_abort_an_association(stcb->sctp_ep, stcb, op_err,
9532 false, so_locked);
9533 SCTP_TCB_LOCK(stcb);
9534 atomic_subtract_int(&stcb->asoc.refcnt, 1);
9535 return (SCTP_RETRAN_EXIT);
9536 }
9537 /* pick up the net */
9538 net = chk->whoTo;
9539 switch (net->ro._l_addr.sa.sa_family) {
9540 #ifdef INET
9541 case AF_INET:
9542 mtu = net->mtu - SCTP_MIN_V4_OVERHEAD;
9543 break;
9544 #endif
9545 #ifdef INET6
9546 case AF_INET6:
9547 mtu = net->mtu - SCTP_MIN_OVERHEAD;
9548 break;
9549 #endif
9550 default:
9551 /* TSNH */
9552 mtu = net->mtu;
9553 break;
9554 }
9555
9556 if ((asoc->peers_rwnd < mtu) && (asoc->total_flight > 0)) {
9557 /* No room in peers rwnd */
9558 uint32_t tsn;
9559
9560 tsn = asoc->last_acked_seq + 1;
9561 if (tsn == chk->rec.data.tsn) {
9562 /*
9563 * we make a special exception for this
9564 * case. The peer has no rwnd but is missing
9565 * the lowest chunk.. which is probably what
9566 * is holding up the rwnd.
9567 */
9568 goto one_chunk_around;
9569 }
9570 return (1);
9571 }
9572 one_chunk_around:
9573 if (asoc->peers_rwnd < mtu) {
9574 one_chunk = 1;
9575 if ((asoc->peers_rwnd == 0) &&
9576 (asoc->total_flight == 0)) {
9577 chk->window_probe = 1;
9578 chk->whoTo->window_probe = 1;
9579 }
9580 }
9581 #ifdef SCTP_AUDITING_ENABLED
9582 sctp_audit_log(0xC3, 2);
9583 #endif
9584 bundle_at = 0;
9585 m = NULL;
9586 net->fast_retran_ip = 0;
9587 if (chk->rec.data.doing_fast_retransmit == 0) {
9588 /*
9589 * if no FR in progress skip destination that have
9590 * flight_size > cwnd.
9591 */
9592 if (net->flight_size >= net->cwnd) {
9593 continue;
9594 }
9595 } else {
9596 /*
9597 * Mark the destination net to have FR recovery
9598 * limits put on it.
9599 */
9600 *fr_done = 1;
9601 net->fast_retran_ip = 1;
9602 }
9603
9604 /*
9605 * if no AUTH is yet included and this chunk requires it,
9606 * make sure to account for it. We don't apply the size
9607 * until the AUTH chunk is actually added below in case
9608 * there is no room for this chunk.
9609 */
9610 if (data_auth_reqd && (auth == NULL)) {
9611 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9612 } else
9613 dmtu = 0;
9614
9615 if ((chk->send_size <= (mtu - dmtu)) ||
9616 (chk->flags & CHUNK_FLAGS_FRAGMENT_OK)) {
9617 /* ok we will add this one */
9618 if (data_auth_reqd) {
9619 if (auth == NULL) {
9620 m = sctp_add_auth_chunk(m,
9621 &endofchain,
9622 &auth,
9623 &auth_offset,
9624 stcb,
9625 SCTP_DATA);
9626 auth_keyid = chk->auth_keyid;
9627 override_ok = 0;
9628 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9629 } else if (override_ok) {
9630 auth_keyid = chk->auth_keyid;
9631 override_ok = 0;
9632 } else if (chk->auth_keyid != auth_keyid) {
9633 /* different keyid, so done bundling */
9634 break;
9635 }
9636 }
9637 m = sctp_copy_mbufchain(chk->data, m, &endofchain, 0, chk->send_size, chk->copy_by_ref);
9638 if (m == NULL) {
9639 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9640 return (ENOMEM);
9641 }
9642 /* Do clear IP_DF ? */
9643 if (chk->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9644 no_fragmentflg = 0;
9645 }
9646 /* update our MTU size */
9647 if (mtu > (chk->send_size + dmtu))
9648 mtu -= (chk->send_size + dmtu);
9649 else
9650 mtu = 0;
9651 data_list[bundle_at++] = chk;
9652 if (one_chunk && (asoc->total_flight <= 0)) {
9653 SCTP_STAT_INCR(sctps_windowprobed);
9654 }
9655 }
9656 if (one_chunk == 0) {
9657 /*
9658 * now are there anymore forward from chk to pick
9659 * up?
9660 */
9661 for (fwd = TAILQ_NEXT(chk, sctp_next); fwd != NULL; fwd = TAILQ_NEXT(fwd, sctp_next)) {
9662 if (fwd->sent != SCTP_DATAGRAM_RESEND) {
9663 /* Nope, not for retran */
9664 continue;
9665 }
9666 if (fwd->whoTo != net) {
9667 /* Nope, not the net in question */
9668 continue;
9669 }
9670 if (data_auth_reqd && (auth == NULL)) {
9671 dmtu = sctp_get_auth_chunk_len(stcb->asoc.peer_hmac_id);
9672 } else
9673 dmtu = 0;
9674 if (fwd->send_size <= (mtu - dmtu)) {
9675 if (data_auth_reqd) {
9676 if (auth == NULL) {
9677 m = sctp_add_auth_chunk(m,
9678 &endofchain,
9679 &auth,
9680 &auth_offset,
9681 stcb,
9682 SCTP_DATA);
9683 auth_keyid = fwd->auth_keyid;
9684 override_ok = 0;
9685 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
9686 } else if (override_ok) {
9687 auth_keyid = fwd->auth_keyid;
9688 override_ok = 0;
9689 } else if (fwd->auth_keyid != auth_keyid) {
9690 /*
9691 * different keyid,
9692 * so done bundling
9693 */
9694 break;
9695 }
9696 }
9697 m = sctp_copy_mbufchain(fwd->data, m, &endofchain, 0, fwd->send_size, fwd->copy_by_ref);
9698 if (m == NULL) {
9699 SCTP_LTRACE_ERR_RET(inp, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
9700 return (ENOMEM);
9701 }
9702 /* Do clear IP_DF ? */
9703 if (fwd->flags & CHUNK_FLAGS_FRAGMENT_OK) {
9704 no_fragmentflg = 0;
9705 }
9706 /* update our MTU size */
9707 if (mtu > (fwd->send_size + dmtu))
9708 mtu -= (fwd->send_size + dmtu);
9709 else
9710 mtu = 0;
9711 data_list[bundle_at++] = fwd;
9712 if (bundle_at >= SCTP_MAX_DATA_BUNDLING) {
9713 break;
9714 }
9715 } else {
9716 /* can't fit so we are done */
9717 break;
9718 }
9719 }
9720 }
9721 /* Is there something to send for this destination? */
9722 if (m) {
9723 /*
9724 * No matter if we fail/or succeed we should start a
9725 * timer. A failure is like a lost IP packet :-)
9726 */
9727 if (!SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9728 /*
9729 * no timer running on this destination
9730 * restart it.
9731 */
9732 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9733 tmr_started = 1;
9734 }
9735 /* Now lets send it, if there is anything to send :> */
9736 if ((error = sctp_lowlevel_chunk_output(inp, stcb, net,
9737 (struct sockaddr *)&net->ro._l_addr, m,
9738 auth_offset, auth, auth_keyid,
9739 no_fragmentflg, 0, 0,
9740 inp->sctp_lport, stcb->rport, htonl(stcb->asoc.peer_vtag),
9741 net->port, NULL,
9742 0, 0,
9743 so_locked))) {
9744 /* error, we could not output */
9745 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
9746 if (error == ENOBUFS) {
9747 asoc->ifp_had_enobuf = 1;
9748 SCTP_STAT_INCR(sctps_lowlevelerr);
9749 }
9750 return (error);
9751 } else {
9752 asoc->ifp_had_enobuf = 0;
9753 }
9754 endofchain = NULL;
9755 auth = NULL;
9756 auth_offset = 0;
9757 /* For HB's */
9758 /*
9759 * We don't want to mark the net->sent time here
9760 * since this we use this for HB and retrans cannot
9761 * measure RTT
9762 */
9763 /* (void)SCTP_GETTIME_TIMEVAL(&net->last_sent_time); */
9764
9765 /* For auto-close */
9766 if (*now_filled == 0) {
9767 (void)SCTP_GETTIME_TIMEVAL(&asoc->time_last_sent);
9768 *now = asoc->time_last_sent;
9769 *now_filled = 1;
9770 } else {
9771 asoc->time_last_sent = *now;
9772 }
9773 *cnt_out += bundle_at;
9774 #ifdef SCTP_AUDITING_ENABLED
9775 sctp_audit_log(0xC4, bundle_at);
9776 #endif
9777 if (bundle_at) {
9778 tsns_sent = data_list[0]->rec.data.tsn;
9779 }
9780 for (i = 0; i < bundle_at; i++) {
9781 SCTP_STAT_INCR(sctps_sendretransdata);
9782 data_list[i]->sent = SCTP_DATAGRAM_SENT;
9783 /*
9784 * When we have a revoked data, and we
9785 * retransmit it, then we clear the revoked
9786 * flag since this flag dictates if we
9787 * subtracted from the fs
9788 */
9789 if (data_list[i]->rec.data.chunk_was_revoked) {
9790 /* Deflate the cwnd */
9791 data_list[i]->whoTo->cwnd -= data_list[i]->book_size;
9792 data_list[i]->rec.data.chunk_was_revoked = 0;
9793 }
9794 data_list[i]->snd_count++;
9795 sctp_ucount_decr(asoc->sent_queue_retran_cnt);
9796 /* record the time */
9797 data_list[i]->sent_rcv_time = asoc->time_last_sent;
9798 if (data_list[i]->book_size_scale) {
9799 /*
9800 * need to double the book size on
9801 * this one
9802 */
9803 data_list[i]->book_size_scale = 0;
9804 /*
9805 * Since we double the booksize, we
9806 * must also double the output queue
9807 * size, since this get shrunk when
9808 * we free by this amount.
9809 */
9810 atomic_add_int(&((asoc)->total_output_queue_size), data_list[i]->book_size);
9811 data_list[i]->book_size *= 2;
9812 } else {
9813 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_RWND_ENABLE) {
9814 sctp_log_rwnd(SCTP_DECREASE_PEER_RWND,
9815 asoc->peers_rwnd, data_list[i]->send_size, SCTP_BASE_SYSCTL(sctp_peer_chunk_oh));
9816 }
9817 asoc->peers_rwnd = sctp_sbspace_sub(asoc->peers_rwnd,
9818 (uint32_t)(data_list[i]->send_size +
9819 SCTP_BASE_SYSCTL(sctp_peer_chunk_oh)));
9820 }
9821 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_FLIGHT_LOGGING_ENABLE) {
9822 sctp_misc_ints(SCTP_FLIGHT_LOG_UP_RSND,
9823 data_list[i]->whoTo->flight_size,
9824 data_list[i]->book_size,
9825 (uint32_t)(uintptr_t)data_list[i]->whoTo,
9826 data_list[i]->rec.data.tsn);
9827 }
9828 sctp_flight_size_increase(data_list[i]);
9829 sctp_total_flight_increase(stcb, data_list[i]);
9830 if (asoc->peers_rwnd < stcb->sctp_ep->sctp_ep.sctp_sws_sender) {
9831 /* SWS sender side engages */
9832 asoc->peers_rwnd = 0;
9833 }
9834 if ((i == 0) &&
9835 (data_list[i]->rec.data.doing_fast_retransmit)) {
9836 SCTP_STAT_INCR(sctps_sendfastretrans);
9837 if ((data_list[i] == TAILQ_FIRST(&asoc->sent_queue)) &&
9838 (tmr_started == 0)) {
9839 /*-
9840 * ok we just fast-retrans'd
9841 * the lowest TSN, i.e the
9842 * first on the list. In
9843 * this case we want to give
9844 * some more time to get a
9845 * SACK back without a
9846 * t3-expiring.
9847 */
9848 sctp_timer_stop(SCTP_TIMER_TYPE_SEND, inp, stcb, net,
9849 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_2);
9850 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, net);
9851 }
9852 }
9853 }
9854 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
9855 sctp_log_cwnd(stcb, net, tsns_sent, SCTP_CWND_LOG_FROM_RESEND);
9856 }
9857 #ifdef SCTP_AUDITING_ENABLED
9858 sctp_auditing(21, inp, stcb, NULL);
9859 #endif
9860 } else {
9861 /* None will fit */
9862 return (1);
9863 }
9864 if (asoc->sent_queue_retran_cnt <= 0) {
9865 /* all done we have no more to retran */
9866 asoc->sent_queue_retran_cnt = 0;
9867 break;
9868 }
9869 if (one_chunk) {
9870 /* No more room in rwnd */
9871 return (1);
9872 }
9873 /* stop the for loop here. we sent out a packet */
9874 break;
9875 }
9876 return (0);
9877 }
9878
9879 static void
sctp_timer_validation(struct sctp_inpcb * inp,struct sctp_tcb * stcb,struct sctp_association * asoc)9880 sctp_timer_validation(struct sctp_inpcb *inp,
9881 struct sctp_tcb *stcb,
9882 struct sctp_association *asoc)
9883 {
9884 struct sctp_nets *net;
9885
9886 /* Validate that a timer is running somewhere */
9887 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
9888 if (SCTP_OS_TIMER_PENDING(&net->rxt_timer.timer)) {
9889 /* Here is a timer */
9890 return;
9891 }
9892 }
9893 SCTP_TCB_LOCK_ASSERT(stcb);
9894 /* Gak, we did not have a timer somewhere */
9895 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Deadlock avoided starting timer on a dest at retran\n");
9896 if (asoc->alternate) {
9897 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->alternate);
9898 } else {
9899 sctp_timer_start(SCTP_TIMER_TYPE_SEND, inp, stcb, asoc->primary_destination);
9900 }
9901 return;
9902 }
9903
9904 void
sctp_chunk_output(struct sctp_inpcb * inp,struct sctp_tcb * stcb,int from_where,int so_locked)9905 sctp_chunk_output(struct sctp_inpcb *inp,
9906 struct sctp_tcb *stcb,
9907 int from_where,
9908 int so_locked)
9909 {
9910 /*-
9911 * Ok this is the generic chunk service queue. we must do the
9912 * following:
9913 * - See if there are retransmits pending, if so we must
9914 * do these first.
9915 * - Service the stream queue that is next, moving any
9916 * message (note I must get a complete message i.e.
9917 * FIRST/MIDDLE and LAST to the out queue in one pass) and assigning
9918 * TSN's
9919 * - Check to see if the cwnd/rwnd allows any output, if so we
9920 * go ahead and formulate and send the low level chunks. Making sure
9921 * to combine any control in the control chunk queue also.
9922 */
9923 struct sctp_association *asoc;
9924 struct sctp_nets *net;
9925 int error = 0, num_out, tot_out = 0, ret = 0, reason_code;
9926 unsigned int burst_cnt = 0;
9927 struct timeval now;
9928 int now_filled = 0;
9929 int nagle_on;
9930 uint32_t frag_point = sctp_get_frag_point(stcb);
9931 int un_sent = 0;
9932 int fr_done;
9933 unsigned int tot_frs = 0;
9934
9935 asoc = &stcb->asoc;
9936 do_it_again:
9937 /* The Nagle algorithm is only applied when handling a send call. */
9938 if (from_where == SCTP_OUTPUT_FROM_USR_SEND) {
9939 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NODELAY)) {
9940 nagle_on = 0;
9941 } else {
9942 nagle_on = 1;
9943 }
9944 } else {
9945 nagle_on = 0;
9946 }
9947 SCTP_TCB_LOCK_ASSERT(stcb);
9948
9949 un_sent = (stcb->asoc.total_output_queue_size - stcb->asoc.total_flight);
9950
9951 if ((un_sent <= 0) &&
9952 (TAILQ_EMPTY(&asoc->control_send_queue)) &&
9953 (TAILQ_EMPTY(&asoc->asconf_send_queue)) &&
9954 (asoc->sent_queue_retran_cnt == 0) &&
9955 (asoc->trigger_reset == 0)) {
9956 /* Nothing to do unless there is something to be sent left */
9957 return;
9958 }
9959 /*
9960 * Do we have something to send, data or control AND a sack timer
9961 * running, if so piggy-back the sack.
9962 */
9963 if (SCTP_OS_TIMER_PENDING(&stcb->asoc.dack_timer.timer)) {
9964 sctp_send_sack(stcb, so_locked);
9965 sctp_timer_stop(SCTP_TIMER_TYPE_RECV, stcb->sctp_ep, stcb, NULL,
9966 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_3);
9967 }
9968 while (asoc->sent_queue_retran_cnt) {
9969 /*-
9970 * Ok, it is retransmission time only, we send out only ONE
9971 * packet with a single call off to the retran code.
9972 */
9973 if (from_where == SCTP_OUTPUT_FROM_COOKIE_ACK) {
9974 /*-
9975 * Special hook for handling cookies discarded
9976 * by peer that carried data. Send cookie-ack only
9977 * and then the next call with get the retran's.
9978 */
9979 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
9980 from_where,
9981 &now, &now_filled, frag_point, so_locked);
9982 return;
9983 } else if (from_where != SCTP_OUTPUT_FROM_HB_TMR) {
9984 /* if its not from a HB then do it */
9985 fr_done = 0;
9986 ret = sctp_chunk_retransmission(inp, stcb, asoc, &num_out, &now, &now_filled, &fr_done, so_locked);
9987 if (fr_done) {
9988 tot_frs++;
9989 }
9990 } else {
9991 /*
9992 * its from any other place, we don't allow retran
9993 * output (only control)
9994 */
9995 ret = 1;
9996 }
9997 if (ret > 0) {
9998 /* Can't send anymore */
9999 /*-
10000 * now lets push out control by calling med-level
10001 * output once. this assures that we WILL send HB's
10002 * if queued too.
10003 */
10004 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1,
10005 from_where,
10006 &now, &now_filled, frag_point, so_locked);
10007 #ifdef SCTP_AUDITING_ENABLED
10008 sctp_auditing(8, inp, stcb, NULL);
10009 #endif
10010 sctp_timer_validation(inp, stcb, asoc);
10011 return;
10012 }
10013 if (ret < 0) {
10014 /*-
10015 * The count was off.. retran is not happening so do
10016 * the normal retransmission.
10017 */
10018 #ifdef SCTP_AUDITING_ENABLED
10019 sctp_auditing(9, inp, stcb, NULL);
10020 #endif
10021 if (ret == SCTP_RETRAN_EXIT) {
10022 return;
10023 }
10024 break;
10025 }
10026 if (from_where == SCTP_OUTPUT_FROM_T3) {
10027 /* Only one transmission allowed out of a timeout */
10028 #ifdef SCTP_AUDITING_ENABLED
10029 sctp_auditing(10, inp, stcb, NULL);
10030 #endif
10031 /* Push out any control */
10032 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out, &reason_code, 1, from_where,
10033 &now, &now_filled, frag_point, so_locked);
10034 return;
10035 }
10036 if ((asoc->fr_max_burst > 0) && (tot_frs >= asoc->fr_max_burst)) {
10037 /* Hit FR burst limit */
10038 return;
10039 }
10040 if ((num_out == 0) && (ret == 0)) {
10041 /* No more retrans to send */
10042 break;
10043 }
10044 }
10045 #ifdef SCTP_AUDITING_ENABLED
10046 sctp_auditing(12, inp, stcb, NULL);
10047 #endif
10048 /* Check for bad destinations, if they exist move chunks around. */
10049 TAILQ_FOREACH(net, &asoc->nets, sctp_next) {
10050 if ((net->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10051 /*-
10052 * if possible move things off of this address we
10053 * still may send below due to the dormant state but
10054 * we try to find an alternate address to send to
10055 * and if we have one we move all queued data on the
10056 * out wheel to this alternate address.
10057 */
10058 if (net->ref_count > 1)
10059 sctp_move_chunks_from_net(stcb, net);
10060 } else {
10061 /*-
10062 * if ((asoc->sat_network) || (net->addr_is_local))
10063 * { burst_limit = asoc->max_burst *
10064 * SCTP_SAT_NETWORK_BURST_INCR; }
10065 */
10066 if (asoc->max_burst > 0) {
10067 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst)) {
10068 if ((net->flight_size + (asoc->max_burst * net->mtu)) < net->cwnd) {
10069 /*
10070 * JRS - Use the congestion
10071 * control given in the
10072 * congestion control module
10073 */
10074 asoc->cc_functions.sctp_cwnd_update_after_output(stcb, net, asoc->max_burst);
10075 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10076 sctp_log_maxburst(stcb, net, 0, asoc->max_burst, SCTP_MAX_BURST_APPLIED);
10077 }
10078 SCTP_STAT_INCR(sctps_maxburstqueued);
10079 }
10080 net->fast_retran_ip = 0;
10081 } else {
10082 if (net->flight_size == 0) {
10083 /*
10084 * Should be decaying the
10085 * cwnd here
10086 */
10087 ;
10088 }
10089 }
10090 }
10091 }
10092 }
10093 burst_cnt = 0;
10094 do {
10095 error = sctp_med_chunk_output(inp, stcb, asoc, &num_out,
10096 &reason_code, 0, from_where,
10097 &now, &now_filled, frag_point, so_locked);
10098 if (error) {
10099 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Error %d was returned from med-c-op\n", error);
10100 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10101 sctp_log_maxburst(stcb, asoc->primary_destination, error, burst_cnt, SCTP_MAX_BURST_ERROR_STOP);
10102 }
10103 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10104 sctp_log_cwnd(stcb, NULL, error, SCTP_SEND_NOW_COMPLETES);
10105 sctp_log_cwnd(stcb, NULL, 0xdeadbeef, SCTP_SEND_NOW_COMPLETES);
10106 }
10107 break;
10108 }
10109 SCTPDBG(SCTP_DEBUG_OUTPUT3, "m-c-o put out %d\n", num_out);
10110
10111 tot_out += num_out;
10112 burst_cnt++;
10113 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10114 sctp_log_cwnd(stcb, NULL, num_out, SCTP_SEND_NOW_COMPLETES);
10115 if (num_out == 0) {
10116 sctp_log_cwnd(stcb, NULL, reason_code, SCTP_SEND_NOW_COMPLETES);
10117 }
10118 }
10119 if (nagle_on) {
10120 /*
10121 * When the Nagle algorithm is used, look at how
10122 * much is unsent, then if its smaller than an MTU
10123 * and we have data in flight we stop, except if we
10124 * are handling a fragmented user message.
10125 */
10126 un_sent = stcb->asoc.total_output_queue_size - stcb->asoc.total_flight;
10127 if ((un_sent < (int)(stcb->asoc.smallest_mtu - SCTP_MIN_OVERHEAD)) &&
10128 (stcb->asoc.total_flight > 0)) {
10129 /* && sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR))) {*/
10130 break;
10131 }
10132 }
10133 if (TAILQ_EMPTY(&asoc->control_send_queue) &&
10134 TAILQ_EMPTY(&asoc->send_queue) &&
10135 sctp_is_there_unsent_data(stcb, so_locked) == 0) {
10136 /* Nothing left to send */
10137 break;
10138 }
10139 if ((stcb->asoc.total_output_queue_size - stcb->asoc.total_flight) <= 0) {
10140 /* Nothing left to send */
10141 break;
10142 }
10143 } while (num_out &&
10144 ((asoc->max_burst == 0) ||
10145 SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) ||
10146 (burst_cnt < asoc->max_burst)));
10147
10148 if (SCTP_BASE_SYSCTL(sctp_use_cwnd_based_maxburst) == 0) {
10149 if ((asoc->max_burst > 0) && (burst_cnt >= asoc->max_burst)) {
10150 SCTP_STAT_INCR(sctps_maxburstqueued);
10151 asoc->burst_limit_applied = 1;
10152 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_MAXBURST_ENABLE) {
10153 sctp_log_maxburst(stcb, asoc->primary_destination, 0, burst_cnt, SCTP_MAX_BURST_APPLIED);
10154 }
10155 } else {
10156 asoc->burst_limit_applied = 0;
10157 }
10158 }
10159 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_CWND_LOGGING_ENABLE) {
10160 sctp_log_cwnd(stcb, NULL, tot_out, SCTP_SEND_NOW_COMPLETES);
10161 }
10162 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Ok, we have put out %d chunks\n",
10163 tot_out);
10164
10165 /*-
10166 * Now we need to clean up the control chunk chain if a ECNE is on
10167 * it. It must be marked as UNSENT again so next call will continue
10168 * to send it until such time that we get a CWR, to remove it.
10169 */
10170 if (stcb->asoc.ecn_echo_cnt_onq)
10171 sctp_fix_ecn_echo(asoc);
10172
10173 if (stcb->asoc.trigger_reset) {
10174 if (sctp_send_stream_reset_out_if_possible(stcb, so_locked) == 0) {
10175 goto do_it_again;
10176 }
10177 }
10178 return;
10179 }
10180
10181 int
sctp_output(struct sctp_inpcb * inp,struct mbuf * m,struct sockaddr * addr,struct mbuf * control,struct thread * p,int flags)10182 sctp_output(
10183 struct sctp_inpcb *inp,
10184 struct mbuf *m,
10185 struct sockaddr *addr,
10186 struct mbuf *control,
10187 struct thread *p,
10188 int flags)
10189 {
10190 if (inp == NULL) {
10191 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10192 return (EINVAL);
10193 }
10194
10195 if (inp->sctp_socket == NULL) {
10196 SCTP_LTRACE_ERR_RET_PKT(m, inp, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
10197 return (EINVAL);
10198 }
10199 return (sctp_sosend(inp->sctp_socket,
10200 addr,
10201 (struct uio *)NULL,
10202 m,
10203 control,
10204 flags, p
10205 ));
10206 }
10207
10208 void
send_forward_tsn(struct sctp_tcb * stcb,struct sctp_association * asoc)10209 send_forward_tsn(struct sctp_tcb *stcb,
10210 struct sctp_association *asoc)
10211 {
10212 struct sctp_tmit_chunk *chk, *at, *tp1, *last;
10213 struct sctp_forward_tsn_chunk *fwdtsn;
10214 struct sctp_strseq *strseq;
10215 struct sctp_strseq_mid *strseq_m;
10216 uint32_t advance_peer_ack_point;
10217 unsigned int cnt_of_space, i, ovh;
10218 unsigned int space_needed;
10219 unsigned int cnt_of_skipped = 0;
10220
10221 SCTP_TCB_LOCK_ASSERT(stcb);
10222 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10223 if (chk->rec.chunk_id.id == SCTP_FORWARD_CUM_TSN) {
10224 /* mark it to unsent */
10225 chk->sent = SCTP_DATAGRAM_UNSENT;
10226 chk->snd_count = 0;
10227 /* Do we correct its output location? */
10228 if (chk->whoTo) {
10229 sctp_free_remote_addr(chk->whoTo);
10230 chk->whoTo = NULL;
10231 }
10232 goto sctp_fill_in_rest;
10233 }
10234 }
10235 /* Ok if we reach here we must build one */
10236 sctp_alloc_a_chunk(stcb, chk);
10237 if (chk == NULL) {
10238 return;
10239 }
10240 asoc->fwd_tsn_cnt++;
10241 chk->copy_by_ref = 0;
10242 /*
10243 * We don't do the old thing here since this is used not for on-wire
10244 * but to tell if we are sending a fwd-tsn by the stack during
10245 * output. And if its a IFORWARD or a FORWARD it is a fwd-tsn.
10246 */
10247 chk->rec.chunk_id.id = SCTP_FORWARD_CUM_TSN;
10248 chk->rec.chunk_id.can_take_data = 0;
10249 chk->flags = 0;
10250 chk->asoc = asoc;
10251 chk->whoTo = NULL;
10252 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
10253 if (chk->data == NULL) {
10254 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
10255 return;
10256 }
10257 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
10258 chk->sent = SCTP_DATAGRAM_UNSENT;
10259 chk->snd_count = 0;
10260 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
10261 asoc->ctrl_queue_cnt++;
10262 sctp_fill_in_rest:
10263 /*-
10264 * Here we go through and fill out the part that deals with
10265 * stream/seq of the ones we skip.
10266 */
10267 SCTP_BUF_LEN(chk->data) = 0;
10268 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10269 if ((at->sent != SCTP_FORWARD_TSN_SKIP) &&
10270 (at->sent != SCTP_DATAGRAM_NR_ACKED)) {
10271 /* no more to look at */
10272 break;
10273 }
10274 if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10275 /* We don't report these */
10276 continue;
10277 }
10278 cnt_of_skipped++;
10279 }
10280 if (asoc->idata_supported) {
10281 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10282 (cnt_of_skipped * sizeof(struct sctp_strseq_mid)));
10283 } else {
10284 space_needed = (sizeof(struct sctp_forward_tsn_chunk) +
10285 (cnt_of_skipped * sizeof(struct sctp_strseq)));
10286 }
10287 cnt_of_space = (unsigned int)M_TRAILINGSPACE(chk->data);
10288
10289 if (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) {
10290 ovh = SCTP_MIN_OVERHEAD;
10291 } else {
10292 ovh = SCTP_MIN_V4_OVERHEAD;
10293 }
10294 if (cnt_of_space > (asoc->smallest_mtu - ovh)) {
10295 /* trim to a mtu size */
10296 cnt_of_space = asoc->smallest_mtu - ovh;
10297 }
10298 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10299 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10300 0xff, 0, cnt_of_skipped,
10301 asoc->advanced_peer_ack_point);
10302 }
10303 advance_peer_ack_point = asoc->advanced_peer_ack_point;
10304 if (cnt_of_space < space_needed) {
10305 /*-
10306 * ok we must trim down the chunk by lowering the
10307 * advance peer ack point.
10308 */
10309 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10310 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10311 0xff, 0xff, cnt_of_space,
10312 space_needed);
10313 }
10314 cnt_of_skipped = cnt_of_space - sizeof(struct sctp_forward_tsn_chunk);
10315 if (asoc->idata_supported) {
10316 cnt_of_skipped /= sizeof(struct sctp_strseq_mid);
10317 } else {
10318 cnt_of_skipped /= sizeof(struct sctp_strseq);
10319 }
10320 /*-
10321 * Go through and find the TSN that will be the one
10322 * we report.
10323 */
10324 at = TAILQ_FIRST(&asoc->sent_queue);
10325 if (at != NULL) {
10326 for (i = 0; i < cnt_of_skipped; i++) {
10327 tp1 = TAILQ_NEXT(at, sctp_next);
10328 if (tp1 == NULL) {
10329 break;
10330 }
10331 at = tp1;
10332 }
10333 }
10334 if (at && SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LOG_TRY_ADVANCE) {
10335 sctp_misc_ints(SCTP_FWD_TSN_CHECK,
10336 0xff, cnt_of_skipped, at->rec.data.tsn,
10337 asoc->advanced_peer_ack_point);
10338 }
10339 last = at;
10340 /*-
10341 * last now points to last one I can report, update
10342 * peer ack point
10343 */
10344 if (last) {
10345 advance_peer_ack_point = last->rec.data.tsn;
10346 }
10347 if (asoc->idata_supported) {
10348 space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10349 cnt_of_skipped * sizeof(struct sctp_strseq_mid);
10350 } else {
10351 space_needed = sizeof(struct sctp_forward_tsn_chunk) +
10352 cnt_of_skipped * sizeof(struct sctp_strseq);
10353 }
10354 }
10355 chk->send_size = space_needed;
10356 /* Setup the chunk */
10357 fwdtsn = mtod(chk->data, struct sctp_forward_tsn_chunk *);
10358 fwdtsn->ch.chunk_length = htons(chk->send_size);
10359 fwdtsn->ch.chunk_flags = 0;
10360 if (asoc->idata_supported) {
10361 fwdtsn->ch.chunk_type = SCTP_IFORWARD_CUM_TSN;
10362 } else {
10363 fwdtsn->ch.chunk_type = SCTP_FORWARD_CUM_TSN;
10364 }
10365 fwdtsn->new_cumulative_tsn = htonl(advance_peer_ack_point);
10366 SCTP_BUF_LEN(chk->data) = chk->send_size;
10367 fwdtsn++;
10368 /*-
10369 * Move pointer to after the fwdtsn and transfer to the
10370 * strseq pointer.
10371 */
10372 if (asoc->idata_supported) {
10373 strseq_m = (struct sctp_strseq_mid *)fwdtsn;
10374 strseq = NULL;
10375 } else {
10376 strseq = (struct sctp_strseq *)fwdtsn;
10377 strseq_m = NULL;
10378 }
10379 /*-
10380 * Now populate the strseq list. This is done blindly
10381 * without pulling out duplicate stream info. This is
10382 * inefficient but won't harm the process since the peer will
10383 * look at these in sequence and will thus release anything.
10384 * It could mean we exceed the PMTU and chop off some that
10385 * we could have included.. but this is unlikely (aka 1432/4
10386 * would mean 300+ stream seq's would have to be reported in
10387 * one FWD-TSN. With a bit of work we can later FIX this to
10388 * optimize and pull out duplicates.. but it does add more
10389 * overhead. So for now... not!
10390 */
10391 i = 0;
10392 TAILQ_FOREACH(at, &asoc->sent_queue, sctp_next) {
10393 if (i >= cnt_of_skipped) {
10394 break;
10395 }
10396 if (!asoc->idata_supported && (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED)) {
10397 /* We don't report these */
10398 continue;
10399 }
10400 if (at->rec.data.tsn == advance_peer_ack_point) {
10401 at->rec.data.fwd_tsn_cnt = 0;
10402 }
10403 if (asoc->idata_supported) {
10404 strseq_m->sid = htons(at->rec.data.sid);
10405 if (at->rec.data.rcv_flags & SCTP_DATA_UNORDERED) {
10406 strseq_m->flags = htons(PR_SCTP_UNORDERED_FLAG);
10407 } else {
10408 strseq_m->flags = 0;
10409 }
10410 strseq_m->mid = htonl(at->rec.data.mid);
10411 strseq_m++;
10412 } else {
10413 strseq->sid = htons(at->rec.data.sid);
10414 strseq->ssn = htons((uint16_t)at->rec.data.mid);
10415 strseq++;
10416 }
10417 i++;
10418 }
10419 return;
10420 }
10421
10422 void
sctp_send_sack(struct sctp_tcb * stcb,int so_locked)10423 sctp_send_sack(struct sctp_tcb *stcb, int so_locked)
10424 {
10425 /*-
10426 * Queue up a SACK or NR-SACK in the control queue.
10427 * We must first check to see if a SACK or NR-SACK is
10428 * somehow on the control queue.
10429 * If so, we will take and and remove the old one.
10430 */
10431 struct sctp_association *asoc;
10432 struct sctp_tmit_chunk *chk, *a_chk;
10433 struct sctp_sack_chunk *sack;
10434 struct sctp_nr_sack_chunk *nr_sack;
10435 struct sctp_gap_ack_block *gap_descriptor;
10436 const struct sack_track *selector;
10437 int mergeable = 0;
10438 int offset;
10439 caddr_t limit;
10440 uint32_t *dup;
10441 int limit_reached = 0;
10442 unsigned int i, siz, j;
10443 unsigned int num_gap_blocks = 0, num_nr_gap_blocks = 0, space;
10444 int num_dups = 0;
10445 int space_req;
10446 uint32_t highest_tsn;
10447 uint8_t flags;
10448 uint8_t type;
10449 uint8_t tsn_map;
10450
10451 if (stcb->asoc.nrsack_supported == 1) {
10452 type = SCTP_NR_SELECTIVE_ACK;
10453 } else {
10454 type = SCTP_SELECTIVE_ACK;
10455 }
10456 a_chk = NULL;
10457 asoc = &stcb->asoc;
10458 SCTP_TCB_LOCK_ASSERT(stcb);
10459 if (asoc->last_data_chunk_from == NULL) {
10460 /* Hmm we never received anything */
10461 return;
10462 }
10463 sctp_slide_mapping_arrays(stcb);
10464 sctp_set_rwnd(stcb, asoc);
10465 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
10466 if (chk->rec.chunk_id.id == type) {
10467 /* Hmm, found a sack already on queue, remove it */
10468 TAILQ_REMOVE(&asoc->control_send_queue, chk, sctp_next);
10469 asoc->ctrl_queue_cnt--;
10470 a_chk = chk;
10471 if (a_chk->data) {
10472 sctp_m_freem(a_chk->data);
10473 a_chk->data = NULL;
10474 }
10475 if (a_chk->whoTo) {
10476 sctp_free_remote_addr(a_chk->whoTo);
10477 a_chk->whoTo = NULL;
10478 }
10479 break;
10480 }
10481 }
10482 if (a_chk == NULL) {
10483 sctp_alloc_a_chunk(stcb, a_chk);
10484 if (a_chk == NULL) {
10485 /* No memory so we drop the idea, and set a timer */
10486 if (stcb->asoc.delayed_ack) {
10487 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10488 stcb->sctp_ep, stcb, NULL,
10489 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_4);
10490 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10491 stcb->sctp_ep, stcb, NULL);
10492 } else {
10493 stcb->asoc.send_sack = 1;
10494 }
10495 return;
10496 }
10497 a_chk->copy_by_ref = 0;
10498 a_chk->rec.chunk_id.id = type;
10499 a_chk->rec.chunk_id.can_take_data = 1;
10500 }
10501 /* Clear our pkt counts */
10502 asoc->data_pkts_seen = 0;
10503
10504 a_chk->flags = 0;
10505 a_chk->asoc = asoc;
10506 a_chk->snd_count = 0;
10507 a_chk->send_size = 0; /* fill in later */
10508 a_chk->sent = SCTP_DATAGRAM_UNSENT;
10509 a_chk->whoTo = NULL;
10510
10511 if ((asoc->last_data_chunk_from->dest_state & SCTP_ADDR_REACHABLE) == 0) {
10512 /*-
10513 * Ok, the destination for the SACK is unreachable, lets see if
10514 * we can select an alternate to asoc->last_data_chunk_from
10515 */
10516 a_chk->whoTo = sctp_find_alternate_net(stcb, asoc->last_data_chunk_from, 0);
10517 if (a_chk->whoTo == NULL) {
10518 /* Nope, no alternate */
10519 a_chk->whoTo = asoc->last_data_chunk_from;
10520 }
10521 } else {
10522 a_chk->whoTo = asoc->last_data_chunk_from;
10523 }
10524 if (a_chk->whoTo) {
10525 atomic_add_int(&a_chk->whoTo->ref_count, 1);
10526 }
10527 if (SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->highest_tsn_inside_nr_map)) {
10528 highest_tsn = asoc->highest_tsn_inside_map;
10529 } else {
10530 highest_tsn = asoc->highest_tsn_inside_nr_map;
10531 }
10532 if (highest_tsn == asoc->cumulative_tsn) {
10533 /* no gaps */
10534 if (type == SCTP_SELECTIVE_ACK) {
10535 space_req = sizeof(struct sctp_sack_chunk);
10536 } else {
10537 space_req = sizeof(struct sctp_nr_sack_chunk);
10538 }
10539 } else {
10540 /* gaps get a cluster */
10541 space_req = MCLBYTES;
10542 }
10543 /* Ok now lets formulate a MBUF with our sack */
10544 a_chk->data = sctp_get_mbuf_for_msg(space_req, 0, M_NOWAIT, 1, MT_DATA);
10545 if ((a_chk->data == NULL) ||
10546 (a_chk->whoTo == NULL)) {
10547 /* rats, no mbuf memory */
10548 if (a_chk->data) {
10549 /* was a problem with the destination */
10550 sctp_m_freem(a_chk->data);
10551 a_chk->data = NULL;
10552 }
10553 sctp_free_a_chunk(stcb, a_chk, so_locked);
10554 /* sa_ignore NO_NULL_CHK */
10555 if (stcb->asoc.delayed_ack) {
10556 sctp_timer_stop(SCTP_TIMER_TYPE_RECV,
10557 stcb->sctp_ep, stcb, NULL,
10558 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_5);
10559 sctp_timer_start(SCTP_TIMER_TYPE_RECV,
10560 stcb->sctp_ep, stcb, NULL);
10561 } else {
10562 stcb->asoc.send_sack = 1;
10563 }
10564 return;
10565 }
10566 /* ok, lets go through and fill it in */
10567 SCTP_BUF_RESV_UF(a_chk->data, SCTP_MIN_OVERHEAD);
10568 space = (unsigned int)M_TRAILINGSPACE(a_chk->data);
10569 if (space > (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD)) {
10570 space = (a_chk->whoTo->mtu - SCTP_MIN_OVERHEAD);
10571 }
10572 limit = mtod(a_chk->data, caddr_t);
10573 limit += space;
10574
10575 flags = 0;
10576
10577 if ((asoc->sctp_cmt_on_off > 0) &&
10578 SCTP_BASE_SYSCTL(sctp_cmt_use_dac)) {
10579 /*-
10580 * CMT DAC algorithm: If 2 (i.e., 0x10) packets have been
10581 * received, then set high bit to 1, else 0. Reset
10582 * pkts_rcvd.
10583 */
10584 flags |= (asoc->cmt_dac_pkts_rcvd << 6);
10585 asoc->cmt_dac_pkts_rcvd = 0;
10586 }
10587 #ifdef SCTP_ASOCLOG_OF_TSNS
10588 stcb->asoc.cumack_logsnt[stcb->asoc.cumack_log_atsnt] = asoc->cumulative_tsn;
10589 stcb->asoc.cumack_log_atsnt++;
10590 if (stcb->asoc.cumack_log_atsnt >= SCTP_TSN_LOG_SIZE) {
10591 stcb->asoc.cumack_log_atsnt = 0;
10592 }
10593 #endif
10594 /* reset the readers interpretation */
10595 stcb->freed_by_sorcv_sincelast = 0;
10596
10597 if (type == SCTP_SELECTIVE_ACK) {
10598 sack = mtod(a_chk->data, struct sctp_sack_chunk *);
10599 nr_sack = NULL;
10600 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)sack + sizeof(struct sctp_sack_chunk));
10601 if (highest_tsn > asoc->mapping_array_base_tsn) {
10602 siz = (((highest_tsn - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10603 } else {
10604 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + highest_tsn + 7) / 8;
10605 }
10606 } else {
10607 sack = NULL;
10608 nr_sack = mtod(a_chk->data, struct sctp_nr_sack_chunk *);
10609 gap_descriptor = (struct sctp_gap_ack_block *)((caddr_t)nr_sack + sizeof(struct sctp_nr_sack_chunk));
10610 if (asoc->highest_tsn_inside_map > asoc->mapping_array_base_tsn) {
10611 siz = (((asoc->highest_tsn_inside_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10612 } else {
10613 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_map + 7) / 8;
10614 }
10615 }
10616
10617 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10618 offset = 1;
10619 } else {
10620 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10621 }
10622 if (((type == SCTP_SELECTIVE_ACK) &&
10623 SCTP_TSN_GT(highest_tsn, asoc->cumulative_tsn)) ||
10624 ((type == SCTP_NR_SELECTIVE_ACK) &&
10625 SCTP_TSN_GT(asoc->highest_tsn_inside_map, asoc->cumulative_tsn))) {
10626 /* we have a gap .. maybe */
10627 for (i = 0; i < siz; i++) {
10628 tsn_map = asoc->mapping_array[i];
10629 if (type == SCTP_SELECTIVE_ACK) {
10630 tsn_map |= asoc->nr_mapping_array[i];
10631 }
10632 if (i == 0) {
10633 /*
10634 * Clear all bits corresponding to TSNs
10635 * smaller or equal to the cumulative TSN.
10636 */
10637 tsn_map &= (~0U << (1 - offset));
10638 }
10639 selector = &sack_array[tsn_map];
10640 if (mergeable && selector->right_edge) {
10641 /*
10642 * Backup, left and right edges were ok to
10643 * merge.
10644 */
10645 num_gap_blocks--;
10646 gap_descriptor--;
10647 }
10648 if (selector->num_entries == 0)
10649 mergeable = 0;
10650 else {
10651 for (j = 0; j < selector->num_entries; j++) {
10652 if (mergeable && selector->right_edge) {
10653 /*
10654 * do a merge by NOT setting
10655 * the left side
10656 */
10657 mergeable = 0;
10658 } else {
10659 /*
10660 * no merge, set the left
10661 * side
10662 */
10663 mergeable = 0;
10664 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10665 }
10666 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10667 num_gap_blocks++;
10668 gap_descriptor++;
10669 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10670 /* no more room */
10671 limit_reached = 1;
10672 break;
10673 }
10674 }
10675 if (selector->left_edge) {
10676 mergeable = 1;
10677 }
10678 }
10679 if (limit_reached) {
10680 /* Reached the limit stop */
10681 break;
10682 }
10683 offset += 8;
10684 }
10685 }
10686 if ((type == SCTP_NR_SELECTIVE_ACK) &&
10687 (limit_reached == 0)) {
10688 mergeable = 0;
10689
10690 if (asoc->highest_tsn_inside_nr_map > asoc->mapping_array_base_tsn) {
10691 siz = (((asoc->highest_tsn_inside_nr_map - asoc->mapping_array_base_tsn) + 1) + 7) / 8;
10692 } else {
10693 siz = (((MAX_TSN - asoc->mapping_array_base_tsn) + 1) + asoc->highest_tsn_inside_nr_map + 7) / 8;
10694 }
10695
10696 if (SCTP_TSN_GT(asoc->mapping_array_base_tsn, asoc->cumulative_tsn)) {
10697 offset = 1;
10698 } else {
10699 offset = asoc->mapping_array_base_tsn - asoc->cumulative_tsn;
10700 }
10701 if (SCTP_TSN_GT(asoc->highest_tsn_inside_nr_map, asoc->cumulative_tsn)) {
10702 /* we have a gap .. maybe */
10703 for (i = 0; i < siz; i++) {
10704 tsn_map = asoc->nr_mapping_array[i];
10705 if (i == 0) {
10706 /*
10707 * Clear all bits corresponding to
10708 * TSNs smaller or equal to the
10709 * cumulative TSN.
10710 */
10711 tsn_map &= (~0U << (1 - offset));
10712 }
10713 selector = &sack_array[tsn_map];
10714 if (mergeable && selector->right_edge) {
10715 /*
10716 * Backup, left and right edges were
10717 * ok to merge.
10718 */
10719 num_nr_gap_blocks--;
10720 gap_descriptor--;
10721 }
10722 if (selector->num_entries == 0)
10723 mergeable = 0;
10724 else {
10725 for (j = 0; j < selector->num_entries; j++) {
10726 if (mergeable && selector->right_edge) {
10727 /*
10728 * do a merge by NOT
10729 * setting the left
10730 * side
10731 */
10732 mergeable = 0;
10733 } else {
10734 /*
10735 * no merge, set the
10736 * left side
10737 */
10738 mergeable = 0;
10739 gap_descriptor->start = htons((selector->gaps[j].start + offset));
10740 }
10741 gap_descriptor->end = htons((selector->gaps[j].end + offset));
10742 num_nr_gap_blocks++;
10743 gap_descriptor++;
10744 if (((caddr_t)gap_descriptor + sizeof(struct sctp_gap_ack_block)) > limit) {
10745 /* no more room */
10746 limit_reached = 1;
10747 break;
10748 }
10749 }
10750 if (selector->left_edge) {
10751 mergeable = 1;
10752 }
10753 }
10754 if (limit_reached) {
10755 /* Reached the limit stop */
10756 break;
10757 }
10758 offset += 8;
10759 }
10760 }
10761 }
10762 /* now we must add any dups we are going to report. */
10763 if ((limit_reached == 0) && (asoc->numduptsns)) {
10764 dup = (uint32_t *)gap_descriptor;
10765 for (i = 0; i < asoc->numduptsns; i++) {
10766 *dup = htonl(asoc->dup_tsns[i]);
10767 dup++;
10768 num_dups++;
10769 if (((caddr_t)dup + sizeof(uint32_t)) > limit) {
10770 /* no more room */
10771 break;
10772 }
10773 }
10774 asoc->numduptsns = 0;
10775 }
10776 /*
10777 * now that the chunk is prepared queue it to the control chunk
10778 * queue.
10779 */
10780 if (type == SCTP_SELECTIVE_ACK) {
10781 a_chk->send_size = (uint16_t)(sizeof(struct sctp_sack_chunk) +
10782 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10783 num_dups * sizeof(int32_t));
10784 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10785 sack->sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10786 sack->sack.a_rwnd = htonl(asoc->my_rwnd);
10787 sack->sack.num_gap_ack_blks = htons(num_gap_blocks);
10788 sack->sack.num_dup_tsns = htons(num_dups);
10789 sack->ch.chunk_type = type;
10790 sack->ch.chunk_flags = flags;
10791 sack->ch.chunk_length = htons(a_chk->send_size);
10792 } else {
10793 a_chk->send_size = (uint16_t)(sizeof(struct sctp_nr_sack_chunk) +
10794 (num_gap_blocks + num_nr_gap_blocks) * sizeof(struct sctp_gap_ack_block) +
10795 num_dups * sizeof(int32_t));
10796 SCTP_BUF_LEN(a_chk->data) = a_chk->send_size;
10797 nr_sack->nr_sack.cum_tsn_ack = htonl(asoc->cumulative_tsn);
10798 nr_sack->nr_sack.a_rwnd = htonl(asoc->my_rwnd);
10799 nr_sack->nr_sack.num_gap_ack_blks = htons(num_gap_blocks);
10800 nr_sack->nr_sack.num_nr_gap_ack_blks = htons(num_nr_gap_blocks);
10801 nr_sack->nr_sack.num_dup_tsns = htons(num_dups);
10802 nr_sack->nr_sack.reserved = 0;
10803 nr_sack->ch.chunk_type = type;
10804 nr_sack->ch.chunk_flags = flags;
10805 nr_sack->ch.chunk_length = htons(a_chk->send_size);
10806 }
10807 TAILQ_INSERT_TAIL(&asoc->control_send_queue, a_chk, sctp_next);
10808 asoc->my_last_reported_rwnd = asoc->my_rwnd;
10809 asoc->ctrl_queue_cnt++;
10810 asoc->send_sack = 0;
10811 SCTP_STAT_INCR(sctps_sendsacks);
10812 return;
10813 }
10814
10815 void
sctp_send_abort_tcb(struct sctp_tcb * stcb,struct mbuf * operr,int so_locked)10816 sctp_send_abort_tcb(struct sctp_tcb *stcb, struct mbuf *operr, int so_locked)
10817 {
10818 struct mbuf *m_abort, *m, *m_last;
10819 struct mbuf *m_out, *m_end = NULL;
10820 struct sctp_abort_chunk *abort;
10821 struct sctp_auth_chunk *auth = NULL;
10822 struct sctp_nets *net;
10823 uint32_t vtag;
10824 uint32_t auth_offset = 0;
10825 int error;
10826 uint16_t cause_len, chunk_len, padding_len;
10827
10828 SCTP_TCB_LOCK_ASSERT(stcb);
10829 /*-
10830 * Add an AUTH chunk, if chunk requires it and save the offset into
10831 * the chain for AUTH
10832 */
10833 if (sctp_auth_is_required_chunk(SCTP_ABORT_ASSOCIATION,
10834 stcb->asoc.peer_auth_chunks)) {
10835 m_out = sctp_add_auth_chunk(NULL, &m_end, &auth, &auth_offset,
10836 stcb, SCTP_ABORT_ASSOCIATION);
10837 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10838 } else {
10839 m_out = NULL;
10840 }
10841 m_abort = sctp_get_mbuf_for_msg(sizeof(struct sctp_abort_chunk), 0, M_NOWAIT, 1, MT_HEADER);
10842 if (m_abort == NULL) {
10843 if (m_out) {
10844 sctp_m_freem(m_out);
10845 }
10846 if (operr) {
10847 sctp_m_freem(operr);
10848 }
10849 return;
10850 }
10851 /* link in any error */
10852 SCTP_BUF_NEXT(m_abort) = operr;
10853 cause_len = 0;
10854 m_last = NULL;
10855 for (m = operr; m; m = SCTP_BUF_NEXT(m)) {
10856 cause_len += (uint16_t)SCTP_BUF_LEN(m);
10857 if (SCTP_BUF_NEXT(m) == NULL) {
10858 m_last = m;
10859 }
10860 }
10861 SCTP_BUF_LEN(m_abort) = sizeof(struct sctp_abort_chunk);
10862 chunk_len = (uint16_t)sizeof(struct sctp_abort_chunk) + cause_len;
10863 padding_len = SCTP_SIZE32(chunk_len) - chunk_len;
10864 if (m_out == NULL) {
10865 /* NO Auth chunk prepended, so reserve space in front */
10866 SCTP_BUF_RESV_UF(m_abort, SCTP_MIN_OVERHEAD);
10867 m_out = m_abort;
10868 } else {
10869 /* Put AUTH chunk at the front of the chain */
10870 SCTP_BUF_NEXT(m_end) = m_abort;
10871 }
10872 if (stcb->asoc.alternate) {
10873 net = stcb->asoc.alternate;
10874 } else {
10875 net = stcb->asoc.primary_destination;
10876 }
10877 /* Fill in the ABORT chunk header. */
10878 abort = mtod(m_abort, struct sctp_abort_chunk *);
10879 abort->ch.chunk_type = SCTP_ABORT_ASSOCIATION;
10880 if (stcb->asoc.peer_vtag == 0) {
10881 /* This happens iff the assoc is in COOKIE-WAIT state. */
10882 vtag = stcb->asoc.my_vtag;
10883 abort->ch.chunk_flags = SCTP_HAD_NO_TCB;
10884 } else {
10885 vtag = stcb->asoc.peer_vtag;
10886 abort->ch.chunk_flags = 0;
10887 }
10888 abort->ch.chunk_length = htons(chunk_len);
10889 /* Add padding, if necessary. */
10890 if (padding_len > 0) {
10891 if ((m_last == NULL) ||
10892 (sctp_add_pad_tombuf(m_last, padding_len) == NULL)) {
10893 sctp_m_freem(m_out);
10894 return;
10895 }
10896 }
10897 if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10898 (struct sockaddr *)&net->ro._l_addr,
10899 m_out, auth_offset, auth, stcb->asoc.authinfo.active_keyid, 1, 0, 0,
10900 stcb->sctp_ep->sctp_lport, stcb->rport, htonl(vtag),
10901 stcb->asoc.primary_destination->port, NULL,
10902 0, 0,
10903 so_locked))) {
10904 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10905 if (error == ENOBUFS) {
10906 stcb->asoc.ifp_had_enobuf = 1;
10907 SCTP_STAT_INCR(sctps_lowlevelerr);
10908 }
10909 } else {
10910 stcb->asoc.ifp_had_enobuf = 0;
10911 }
10912 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10913 }
10914
10915 void
sctp_send_shutdown_complete(struct sctp_tcb * stcb,struct sctp_nets * net,int reflect_vtag)10916 sctp_send_shutdown_complete(struct sctp_tcb *stcb,
10917 struct sctp_nets *net,
10918 int reflect_vtag)
10919 {
10920 /* formulate and SEND a SHUTDOWN-COMPLETE */
10921 struct mbuf *m_shutdown_comp;
10922 struct sctp_shutdown_complete_chunk *shutdown_complete;
10923 uint32_t vtag;
10924 int error;
10925 uint8_t flags;
10926
10927 m_shutdown_comp = sctp_get_mbuf_for_msg(sizeof(struct sctp_chunkhdr), 0, M_NOWAIT, 1, MT_HEADER);
10928 if (m_shutdown_comp == NULL) {
10929 /* no mbuf's */
10930 return;
10931 }
10932 if (reflect_vtag) {
10933 flags = SCTP_HAD_NO_TCB;
10934 vtag = stcb->asoc.my_vtag;
10935 } else {
10936 flags = 0;
10937 vtag = stcb->asoc.peer_vtag;
10938 }
10939 shutdown_complete = mtod(m_shutdown_comp, struct sctp_shutdown_complete_chunk *);
10940 shutdown_complete->ch.chunk_type = SCTP_SHUTDOWN_COMPLETE;
10941 shutdown_complete->ch.chunk_flags = flags;
10942 shutdown_complete->ch.chunk_length = htons(sizeof(struct sctp_shutdown_complete_chunk));
10943 SCTP_BUF_LEN(m_shutdown_comp) = sizeof(struct sctp_shutdown_complete_chunk);
10944 if ((error = sctp_lowlevel_chunk_output(stcb->sctp_ep, stcb, net,
10945 (struct sockaddr *)&net->ro._l_addr,
10946 m_shutdown_comp, 0, NULL, 0, 1, 0, 0,
10947 stcb->sctp_ep->sctp_lport, stcb->rport,
10948 htonl(vtag),
10949 net->port, NULL,
10950 0, 0,
10951 SCTP_SO_NOT_LOCKED))) {
10952 SCTPDBG(SCTP_DEBUG_OUTPUT3, "Gak send error %d\n", error);
10953 if (error == ENOBUFS) {
10954 stcb->asoc.ifp_had_enobuf = 1;
10955 SCTP_STAT_INCR(sctps_lowlevelerr);
10956 }
10957 } else {
10958 stcb->asoc.ifp_had_enobuf = 0;
10959 }
10960 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
10961 return;
10962 }
10963
10964 static void
sctp_send_resp_msg(struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,uint32_t vtag,uint8_t type,struct mbuf * cause,uint8_t mflowtype,uint32_t mflowid,uint16_t fibnum,uint32_t vrf_id,uint16_t port)10965 sctp_send_resp_msg(struct sockaddr *src, struct sockaddr *dst,
10966 struct sctphdr *sh, uint32_t vtag,
10967 uint8_t type, struct mbuf *cause,
10968 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
10969 uint32_t vrf_id, uint16_t port)
10970 {
10971 struct mbuf *o_pak;
10972 struct mbuf *mout;
10973 struct sctphdr *shout;
10974 struct sctp_chunkhdr *ch;
10975 #if defined(INET) || defined(INET6)
10976 struct udphdr *udp;
10977 #endif
10978 int ret, len, cause_len, padding_len;
10979 #ifdef INET
10980 struct sockaddr_in *src_sin, *dst_sin;
10981 struct ip *ip;
10982 #endif
10983 #ifdef INET6
10984 struct sockaddr_in6 *src_sin6, *dst_sin6;
10985 struct ip6_hdr *ip6;
10986 #endif
10987
10988 /* Compute the length of the cause and add final padding. */
10989 cause_len = 0;
10990 if (cause != NULL) {
10991 struct mbuf *m_at, *m_last = NULL;
10992
10993 for (m_at = cause; m_at; m_at = SCTP_BUF_NEXT(m_at)) {
10994 if (SCTP_BUF_NEXT(m_at) == NULL)
10995 m_last = m_at;
10996 cause_len += SCTP_BUF_LEN(m_at);
10997 }
10998 padding_len = cause_len % 4;
10999 if (padding_len != 0) {
11000 padding_len = 4 - padding_len;
11001 }
11002 if (padding_len != 0) {
11003 if (sctp_add_pad_tombuf(m_last, padding_len) == NULL) {
11004 sctp_m_freem(cause);
11005 return;
11006 }
11007 }
11008 } else {
11009 padding_len = 0;
11010 }
11011 /* Get an mbuf for the header. */
11012 len = sizeof(struct sctphdr) + sizeof(struct sctp_chunkhdr);
11013 switch (dst->sa_family) {
11014 #ifdef INET
11015 case AF_INET:
11016 len += sizeof(struct ip);
11017 break;
11018 #endif
11019 #ifdef INET6
11020 case AF_INET6:
11021 len += sizeof(struct ip6_hdr);
11022 break;
11023 #endif
11024 default:
11025 break;
11026 }
11027 #if defined(INET) || defined(INET6)
11028 if (port) {
11029 len += sizeof(struct udphdr);
11030 }
11031 #endif
11032 mout = sctp_get_mbuf_for_msg(len + max_linkhdr, 1, M_NOWAIT, 1, MT_DATA);
11033 if (mout == NULL) {
11034 if (cause) {
11035 sctp_m_freem(cause);
11036 }
11037 return;
11038 }
11039 SCTP_BUF_RESV_UF(mout, max_linkhdr);
11040 SCTP_BUF_LEN(mout) = len;
11041 SCTP_BUF_NEXT(mout) = cause;
11042 M_SETFIB(mout, fibnum);
11043 mout->m_pkthdr.flowid = mflowid;
11044 M_HASHTYPE_SET(mout, mflowtype);
11045 #ifdef INET
11046 ip = NULL;
11047 #endif
11048 #ifdef INET6
11049 ip6 = NULL;
11050 #endif
11051 switch (dst->sa_family) {
11052 #ifdef INET
11053 case AF_INET:
11054 src_sin = (struct sockaddr_in *)src;
11055 dst_sin = (struct sockaddr_in *)dst;
11056 ip = mtod(mout, struct ip *);
11057 ip->ip_v = IPVERSION;
11058 ip->ip_hl = (sizeof(struct ip) >> 2);
11059 ip->ip_tos = 0;
11060 ip->ip_off = htons(IP_DF);
11061 ip_fillid(ip);
11062 ip->ip_ttl = MODULE_GLOBAL(ip_defttl);
11063 if (port) {
11064 ip->ip_p = IPPROTO_UDP;
11065 } else {
11066 ip->ip_p = IPPROTO_SCTP;
11067 }
11068 ip->ip_src.s_addr = dst_sin->sin_addr.s_addr;
11069 ip->ip_dst.s_addr = src_sin->sin_addr.s_addr;
11070 ip->ip_sum = 0;
11071 len = sizeof(struct ip);
11072 shout = (struct sctphdr *)((caddr_t)ip + len);
11073 break;
11074 #endif
11075 #ifdef INET6
11076 case AF_INET6:
11077 src_sin6 = (struct sockaddr_in6 *)src;
11078 dst_sin6 = (struct sockaddr_in6 *)dst;
11079 ip6 = mtod(mout, struct ip6_hdr *);
11080 ip6->ip6_flow = htonl(0x60000000);
11081 if (V_ip6_auto_flowlabel) {
11082 ip6->ip6_flow |= (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
11083 }
11084 ip6->ip6_hlim = MODULE_GLOBAL(ip6_defhlim);
11085 if (port) {
11086 ip6->ip6_nxt = IPPROTO_UDP;
11087 } else {
11088 ip6->ip6_nxt = IPPROTO_SCTP;
11089 }
11090 ip6->ip6_src = dst_sin6->sin6_addr;
11091 ip6->ip6_dst = src_sin6->sin6_addr;
11092 len = sizeof(struct ip6_hdr);
11093 shout = (struct sctphdr *)((caddr_t)ip6 + len);
11094 break;
11095 #endif
11096 default:
11097 len = 0;
11098 shout = mtod(mout, struct sctphdr *);
11099 break;
11100 }
11101 #if defined(INET) || defined(INET6)
11102 if (port) {
11103 if (htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port)) == 0) {
11104 sctp_m_freem(mout);
11105 return;
11106 }
11107 udp = (struct udphdr *)shout;
11108 udp->uh_sport = htons(SCTP_BASE_SYSCTL(sctp_udp_tunneling_port));
11109 udp->uh_dport = port;
11110 udp->uh_sum = 0;
11111 udp->uh_ulen = htons((uint16_t)(sizeof(struct udphdr) +
11112 sizeof(struct sctphdr) +
11113 sizeof(struct sctp_chunkhdr) +
11114 cause_len + padding_len));
11115 len += sizeof(struct udphdr);
11116 shout = (struct sctphdr *)((caddr_t)shout + sizeof(struct udphdr));
11117 } else {
11118 udp = NULL;
11119 }
11120 #endif
11121 shout->src_port = sh->dest_port;
11122 shout->dest_port = sh->src_port;
11123 shout->checksum = 0;
11124 if (vtag) {
11125 shout->v_tag = htonl(vtag);
11126 } else {
11127 shout->v_tag = sh->v_tag;
11128 }
11129 len += sizeof(struct sctphdr);
11130 ch = (struct sctp_chunkhdr *)((caddr_t)shout + sizeof(struct sctphdr));
11131 ch->chunk_type = type;
11132 if (vtag) {
11133 ch->chunk_flags = 0;
11134 } else {
11135 ch->chunk_flags = SCTP_HAD_NO_TCB;
11136 }
11137 ch->chunk_length = htons((uint16_t)(sizeof(struct sctp_chunkhdr) + cause_len));
11138 len += sizeof(struct sctp_chunkhdr);
11139 len += cause_len + padding_len;
11140
11141 if (SCTP_GET_HEADER_FOR_OUTPUT(o_pak)) {
11142 sctp_m_freem(mout);
11143 return;
11144 }
11145 SCTP_ATTACH_CHAIN(o_pak, mout, len);
11146 switch (dst->sa_family) {
11147 #ifdef INET
11148 case AF_INET:
11149 if (port) {
11150 if (V_udp_cksum) {
11151 udp->uh_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, udp->uh_ulen + htons(IPPROTO_UDP));
11152 } else {
11153 udp->uh_sum = 0;
11154 }
11155 }
11156 ip->ip_len = htons(len);
11157 if (port) {
11158 shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip) + sizeof(struct udphdr));
11159 SCTP_STAT_INCR(sctps_sendswcrc);
11160 if (V_udp_cksum) {
11161 SCTP_ENABLE_UDP_CSUM(o_pak);
11162 }
11163 } else {
11164 mout->m_pkthdr.csum_flags = CSUM_SCTP;
11165 mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11166 SCTP_STAT_INCR(sctps_sendhwcrc);
11167 }
11168 #ifdef SCTP_PACKET_LOGGING
11169 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11170 sctp_packet_log(o_pak);
11171 }
11172 #endif
11173 SCTP_PROBE5(send, NULL, NULL, ip, NULL, shout);
11174 SCTP_IP_OUTPUT(ret, o_pak, NULL, NULL, vrf_id);
11175 break;
11176 #endif
11177 #ifdef INET6
11178 case AF_INET6:
11179 ip6->ip6_plen = htons((uint16_t)(len - sizeof(struct ip6_hdr)));
11180 if (port) {
11181 shout->checksum = sctp_calculate_cksum(mout, sizeof(struct ip6_hdr) + sizeof(struct udphdr));
11182 SCTP_STAT_INCR(sctps_sendswcrc);
11183 if ((udp->uh_sum = in6_cksum(o_pak, IPPROTO_UDP, sizeof(struct ip6_hdr), len - sizeof(struct ip6_hdr))) == 0) {
11184 udp->uh_sum = 0xffff;
11185 }
11186 } else {
11187 mout->m_pkthdr.csum_flags = CSUM_SCTP_IPV6;
11188 mout->m_pkthdr.csum_data = offsetof(struct sctphdr, checksum);
11189 SCTP_STAT_INCR(sctps_sendhwcrc);
11190 }
11191 #ifdef SCTP_PACKET_LOGGING
11192 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_LAST_PACKET_TRACING) {
11193 sctp_packet_log(o_pak);
11194 }
11195 #endif
11196 SCTP_PROBE5(send, NULL, NULL, ip6, NULL, shout);
11197 SCTP_IP6_OUTPUT(ret, o_pak, NULL, NULL, NULL, vrf_id);
11198 break;
11199 #endif
11200 default:
11201 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Unknown protocol (TSNH) type %d\n",
11202 dst->sa_family);
11203 sctp_m_freem(mout);
11204 SCTP_LTRACE_ERR_RET_PKT(mout, NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
11205 return;
11206 }
11207 SCTPDBG(SCTP_DEBUG_OUTPUT3, "return from send is %d\n", ret);
11208 if (port) {
11209 UDPSTAT_INC(udps_opackets);
11210 }
11211 SCTP_STAT_INCR(sctps_sendpackets);
11212 SCTP_STAT_INCR_COUNTER64(sctps_outpackets);
11213 SCTP_STAT_INCR_COUNTER64(sctps_outcontrolchunks);
11214 if (ret) {
11215 SCTP_STAT_INCR(sctps_senderrors);
11216 }
11217 return;
11218 }
11219
11220 void
sctp_send_shutdown_complete2(struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,uint8_t mflowtype,uint32_t mflowid,uint16_t fibnum,uint32_t vrf_id,uint16_t port)11221 sctp_send_shutdown_complete2(struct sockaddr *src, struct sockaddr *dst,
11222 struct sctphdr *sh,
11223 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
11224 uint32_t vrf_id, uint16_t port)
11225 {
11226 sctp_send_resp_msg(src, dst, sh, 0, SCTP_SHUTDOWN_COMPLETE, NULL,
11227 mflowtype, mflowid, fibnum,
11228 vrf_id, port);
11229 }
11230
11231 void
sctp_send_hb(struct sctp_tcb * stcb,struct sctp_nets * net,int so_locked)11232 sctp_send_hb(struct sctp_tcb *stcb, struct sctp_nets *net, int so_locked)
11233 {
11234 struct sctp_tmit_chunk *chk;
11235 struct sctp_heartbeat_chunk *hb;
11236 struct timeval now;
11237
11238 SCTP_TCB_LOCK_ASSERT(stcb);
11239 if (net == NULL) {
11240 return;
11241 }
11242 (void)SCTP_GETTIME_TIMEVAL(&now);
11243 switch (net->ro._l_addr.sa.sa_family) {
11244 #ifdef INET
11245 case AF_INET:
11246 break;
11247 #endif
11248 #ifdef INET6
11249 case AF_INET6:
11250 break;
11251 #endif
11252 default:
11253 return;
11254 }
11255 sctp_alloc_a_chunk(stcb, chk);
11256 if (chk == NULL) {
11257 SCTPDBG(SCTP_DEBUG_OUTPUT4, "Gak, can't get a chunk for hb\n");
11258 return;
11259 }
11260
11261 chk->copy_by_ref = 0;
11262 chk->rec.chunk_id.id = SCTP_HEARTBEAT_REQUEST;
11263 chk->rec.chunk_id.can_take_data = 1;
11264 chk->flags = 0;
11265 chk->asoc = &stcb->asoc;
11266 chk->send_size = sizeof(struct sctp_heartbeat_chunk);
11267
11268 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11269 if (chk->data == NULL) {
11270 sctp_free_a_chunk(stcb, chk, so_locked);
11271 return;
11272 }
11273 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11274 SCTP_BUF_LEN(chk->data) = chk->send_size;
11275 chk->sent = SCTP_DATAGRAM_UNSENT;
11276 chk->snd_count = 0;
11277 chk->whoTo = net;
11278 atomic_add_int(&chk->whoTo->ref_count, 1);
11279 /* Now we have a mbuf that we can fill in with the details */
11280 hb = mtod(chk->data, struct sctp_heartbeat_chunk *);
11281 memset(hb, 0, sizeof(struct sctp_heartbeat_chunk));
11282 /* fill out chunk header */
11283 hb->ch.chunk_type = SCTP_HEARTBEAT_REQUEST;
11284 hb->ch.chunk_flags = 0;
11285 hb->ch.chunk_length = htons(chk->send_size);
11286 /* Fill out hb parameter */
11287 hb->heartbeat.hb_info.ph.param_type = htons(SCTP_HEARTBEAT_INFO);
11288 hb->heartbeat.hb_info.ph.param_length = htons(sizeof(struct sctp_heartbeat_info_param));
11289 hb->heartbeat.hb_info.time_value_1 = now.tv_sec;
11290 hb->heartbeat.hb_info.time_value_2 = now.tv_usec;
11291 /* Did our user request this one, put it in */
11292 hb->heartbeat.hb_info.addr_family = (uint8_t)net->ro._l_addr.sa.sa_family;
11293 hb->heartbeat.hb_info.addr_len = net->ro._l_addr.sa.sa_len;
11294 if (net->dest_state & SCTP_ADDR_UNCONFIRMED) {
11295 /*
11296 * we only take from the entropy pool if the address is not
11297 * confirmed.
11298 */
11299 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11300 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = sctp_select_initial_TSN(&stcb->sctp_ep->sctp_ep);
11301 } else {
11302 net->heartbeat_random1 = hb->heartbeat.hb_info.random_value1 = 0;
11303 net->heartbeat_random2 = hb->heartbeat.hb_info.random_value2 = 0;
11304 }
11305 switch (net->ro._l_addr.sa.sa_family) {
11306 #ifdef INET
11307 case AF_INET:
11308 memcpy(hb->heartbeat.hb_info.address,
11309 &net->ro._l_addr.sin.sin_addr,
11310 sizeof(net->ro._l_addr.sin.sin_addr));
11311 break;
11312 #endif
11313 #ifdef INET6
11314 case AF_INET6:
11315 memcpy(hb->heartbeat.hb_info.address,
11316 &net->ro._l_addr.sin6.sin6_addr,
11317 sizeof(net->ro._l_addr.sin6.sin6_addr));
11318 break;
11319 #endif
11320 default:
11321 if (chk->data) {
11322 sctp_m_freem(chk->data);
11323 chk->data = NULL;
11324 }
11325 sctp_free_a_chunk(stcb, chk, so_locked);
11326 return;
11327 break;
11328 }
11329 net->hb_responded = 0;
11330 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11331 stcb->asoc.ctrl_queue_cnt++;
11332 SCTP_STAT_INCR(sctps_sendheartbeat);
11333 return;
11334 }
11335
11336 void
sctp_send_ecn_echo(struct sctp_tcb * stcb,struct sctp_nets * net,uint32_t high_tsn)11337 sctp_send_ecn_echo(struct sctp_tcb *stcb, struct sctp_nets *net,
11338 uint32_t high_tsn)
11339 {
11340 struct sctp_association *asoc;
11341 struct sctp_ecne_chunk *ecne;
11342 struct sctp_tmit_chunk *chk;
11343
11344 if (net == NULL) {
11345 return;
11346 }
11347 asoc = &stcb->asoc;
11348 SCTP_TCB_LOCK_ASSERT(stcb);
11349 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11350 if ((chk->rec.chunk_id.id == SCTP_ECN_ECHO) && (net == chk->whoTo)) {
11351 /* found a previous ECN_ECHO update it if needed */
11352 uint32_t cnt, ctsn;
11353
11354 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11355 ctsn = ntohl(ecne->tsn);
11356 if (SCTP_TSN_GT(high_tsn, ctsn)) {
11357 ecne->tsn = htonl(high_tsn);
11358 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11359 }
11360 cnt = ntohl(ecne->num_pkts_since_cwr);
11361 cnt++;
11362 ecne->num_pkts_since_cwr = htonl(cnt);
11363 return;
11364 }
11365 }
11366 /* nope could not find one to update so we must build one */
11367 sctp_alloc_a_chunk(stcb, chk);
11368 if (chk == NULL) {
11369 return;
11370 }
11371 SCTP_STAT_INCR(sctps_queue_upd_ecne);
11372 chk->copy_by_ref = 0;
11373 chk->rec.chunk_id.id = SCTP_ECN_ECHO;
11374 chk->rec.chunk_id.can_take_data = 0;
11375 chk->flags = 0;
11376 chk->asoc = &stcb->asoc;
11377 chk->send_size = sizeof(struct sctp_ecne_chunk);
11378 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11379 if (chk->data == NULL) {
11380 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11381 return;
11382 }
11383 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11384 SCTP_BUF_LEN(chk->data) = chk->send_size;
11385 chk->sent = SCTP_DATAGRAM_UNSENT;
11386 chk->snd_count = 0;
11387 chk->whoTo = net;
11388 atomic_add_int(&chk->whoTo->ref_count, 1);
11389
11390 stcb->asoc.ecn_echo_cnt_onq++;
11391 ecne = mtod(chk->data, struct sctp_ecne_chunk *);
11392 ecne->ch.chunk_type = SCTP_ECN_ECHO;
11393 ecne->ch.chunk_flags = 0;
11394 ecne->ch.chunk_length = htons(sizeof(struct sctp_ecne_chunk));
11395 ecne->tsn = htonl(high_tsn);
11396 ecne->num_pkts_since_cwr = htonl(1);
11397 TAILQ_INSERT_HEAD(&stcb->asoc.control_send_queue, chk, sctp_next);
11398 asoc->ctrl_queue_cnt++;
11399 }
11400
11401 void
sctp_send_packet_dropped(struct sctp_tcb * stcb,struct sctp_nets * net,struct mbuf * m,int len,int iphlen,int bad_crc)11402 sctp_send_packet_dropped(struct sctp_tcb *stcb, struct sctp_nets *net,
11403 struct mbuf *m, int len, int iphlen, int bad_crc)
11404 {
11405 struct sctp_association *asoc;
11406 struct sctp_pktdrop_chunk *drp;
11407 struct sctp_tmit_chunk *chk;
11408 uint8_t *datap;
11409 int was_trunc = 0;
11410 int fullsz = 0;
11411 long spc;
11412 int offset;
11413 struct sctp_chunkhdr *ch, chunk_buf;
11414 unsigned int chk_length;
11415
11416 if (!stcb) {
11417 return;
11418 }
11419 asoc = &stcb->asoc;
11420 SCTP_TCB_LOCK_ASSERT(stcb);
11421 if (asoc->pktdrop_supported == 0) {
11422 /*-
11423 * peer must declare support before I send one.
11424 */
11425 return;
11426 }
11427 if (stcb->sctp_socket == NULL) {
11428 return;
11429 }
11430 sctp_alloc_a_chunk(stcb, chk);
11431 if (chk == NULL) {
11432 return;
11433 }
11434 chk->copy_by_ref = 0;
11435 chk->rec.chunk_id.id = SCTP_PACKET_DROPPED;
11436 chk->rec.chunk_id.can_take_data = 1;
11437 chk->flags = 0;
11438 len -= iphlen;
11439 chk->send_size = len;
11440 /* Validate that we do not have an ABORT in here. */
11441 offset = iphlen + sizeof(struct sctphdr);
11442 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11443 sizeof(*ch), (uint8_t *)&chunk_buf);
11444 while (ch != NULL) {
11445 chk_length = ntohs(ch->chunk_length);
11446 if (chk_length < sizeof(*ch)) {
11447 /* break to abort land */
11448 break;
11449 }
11450 switch (ch->chunk_type) {
11451 case SCTP_PACKET_DROPPED:
11452 case SCTP_ABORT_ASSOCIATION:
11453 case SCTP_INITIATION_ACK:
11454 /**
11455 * We don't respond with an PKT-DROP to an ABORT
11456 * or PKT-DROP. We also do not respond to an
11457 * INIT-ACK, because we can't know if the initiation
11458 * tag is correct or not.
11459 */
11460 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11461 return;
11462 default:
11463 break;
11464 }
11465 offset += SCTP_SIZE32(chk_length);
11466 ch = (struct sctp_chunkhdr *)sctp_m_getptr(m, offset,
11467 sizeof(*ch), (uint8_t *)&chunk_buf);
11468 }
11469
11470 if ((len + SCTP_MAX_OVERHEAD + sizeof(struct sctp_pktdrop_chunk)) >
11471 min(stcb->asoc.smallest_mtu, MCLBYTES)) {
11472 /*
11473 * only send 1 mtu worth, trim off the excess on the end.
11474 */
11475 fullsz = len;
11476 len = min(stcb->asoc.smallest_mtu, MCLBYTES) - SCTP_MAX_OVERHEAD;
11477 was_trunc = 1;
11478 }
11479 chk->asoc = &stcb->asoc;
11480 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11481 if (chk->data == NULL) {
11482 jump_out:
11483 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11484 return;
11485 }
11486 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11487 drp = mtod(chk->data, struct sctp_pktdrop_chunk *);
11488 if (drp == NULL) {
11489 sctp_m_freem(chk->data);
11490 chk->data = NULL;
11491 goto jump_out;
11492 }
11493 chk->book_size = SCTP_SIZE32((chk->send_size + sizeof(struct sctp_pktdrop_chunk) +
11494 sizeof(struct sctphdr) + SCTP_MED_OVERHEAD));
11495 chk->book_size_scale = 0;
11496 if (was_trunc) {
11497 drp->ch.chunk_flags = SCTP_PACKET_TRUNCATED;
11498 drp->trunc_len = htons(fullsz);
11499 /*
11500 * Len is already adjusted to size minus overhead above take
11501 * out the pkt_drop chunk itself from it.
11502 */
11503 chk->send_size = (uint16_t)(len - sizeof(struct sctp_pktdrop_chunk));
11504 len = chk->send_size;
11505 } else {
11506 /* no truncation needed */
11507 drp->ch.chunk_flags = 0;
11508 drp->trunc_len = htons(0);
11509 }
11510 if (bad_crc) {
11511 drp->ch.chunk_flags |= SCTP_BADCRC;
11512 }
11513 chk->send_size += sizeof(struct sctp_pktdrop_chunk);
11514 SCTP_BUF_LEN(chk->data) = chk->send_size;
11515 chk->sent = SCTP_DATAGRAM_UNSENT;
11516 chk->snd_count = 0;
11517 if (net) {
11518 /* we should hit here */
11519 chk->whoTo = net;
11520 atomic_add_int(&chk->whoTo->ref_count, 1);
11521 } else {
11522 chk->whoTo = NULL;
11523 }
11524 drp->ch.chunk_type = SCTP_PACKET_DROPPED;
11525 drp->ch.chunk_length = htons(chk->send_size);
11526 spc = SCTP_SB_LIMIT_RCV(stcb->sctp_socket);
11527 if (spc < 0) {
11528 spc = 0;
11529 }
11530 drp->bottle_bw = htonl(spc);
11531 if (asoc->my_rwnd) {
11532 drp->current_onq = htonl(asoc->size_on_reasm_queue +
11533 asoc->size_on_all_streams +
11534 asoc->my_rwnd_control_len +
11535 SCTP_SBAVAIL(&stcb->sctp_socket->so_rcv));
11536 } else {
11537 /*-
11538 * If my rwnd is 0, possibly from mbuf depletion as well as
11539 * space used, tell the peer there is NO space aka onq == bw
11540 */
11541 drp->current_onq = htonl(spc);
11542 }
11543 drp->reserved = 0;
11544 datap = drp->data;
11545 m_copydata(m, iphlen, len, (caddr_t)datap);
11546 TAILQ_INSERT_TAIL(&stcb->asoc.control_send_queue, chk, sctp_next);
11547 asoc->ctrl_queue_cnt++;
11548 }
11549
11550 void
sctp_send_cwr(struct sctp_tcb * stcb,struct sctp_nets * net,uint32_t high_tsn,uint8_t override)11551 sctp_send_cwr(struct sctp_tcb *stcb, struct sctp_nets *net, uint32_t high_tsn, uint8_t override)
11552 {
11553 struct sctp_association *asoc;
11554 struct sctp_cwr_chunk *cwr;
11555 struct sctp_tmit_chunk *chk;
11556
11557 SCTP_TCB_LOCK_ASSERT(stcb);
11558 if (net == NULL) {
11559 return;
11560 }
11561 asoc = &stcb->asoc;
11562 TAILQ_FOREACH(chk, &asoc->control_send_queue, sctp_next) {
11563 if ((chk->rec.chunk_id.id == SCTP_ECN_CWR) && (net == chk->whoTo)) {
11564 /*
11565 * found a previous CWR queued to same destination
11566 * update it if needed
11567 */
11568 uint32_t ctsn;
11569
11570 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11571 ctsn = ntohl(cwr->tsn);
11572 if (SCTP_TSN_GT(high_tsn, ctsn)) {
11573 cwr->tsn = htonl(high_tsn);
11574 }
11575 if (override & SCTP_CWR_REDUCE_OVERRIDE) {
11576 /* Make sure override is carried */
11577 cwr->ch.chunk_flags |= SCTP_CWR_REDUCE_OVERRIDE;
11578 }
11579 return;
11580 }
11581 }
11582 sctp_alloc_a_chunk(stcb, chk);
11583 if (chk == NULL) {
11584 return;
11585 }
11586 chk->copy_by_ref = 0;
11587 chk->rec.chunk_id.id = SCTP_ECN_CWR;
11588 chk->rec.chunk_id.can_take_data = 1;
11589 chk->flags = 0;
11590 chk->asoc = asoc;
11591 chk->send_size = sizeof(struct sctp_cwr_chunk);
11592 chk->data = sctp_get_mbuf_for_msg(chk->send_size, 0, M_NOWAIT, 1, MT_HEADER);
11593 if (chk->data == NULL) {
11594 sctp_free_a_chunk(stcb, chk, SCTP_SO_NOT_LOCKED);
11595 return;
11596 }
11597 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11598 SCTP_BUF_LEN(chk->data) = chk->send_size;
11599 chk->sent = SCTP_DATAGRAM_UNSENT;
11600 chk->snd_count = 0;
11601 chk->whoTo = net;
11602 atomic_add_int(&chk->whoTo->ref_count, 1);
11603 cwr = mtod(chk->data, struct sctp_cwr_chunk *);
11604 cwr->ch.chunk_type = SCTP_ECN_CWR;
11605 cwr->ch.chunk_flags = override;
11606 cwr->ch.chunk_length = htons(sizeof(struct sctp_cwr_chunk));
11607 cwr->tsn = htonl(high_tsn);
11608 TAILQ_INSERT_TAIL(&asoc->control_send_queue, chk, sctp_next);
11609 asoc->ctrl_queue_cnt++;
11610 }
11611
11612 static int
sctp_add_stream_reset_out(struct sctp_tcb * stcb,struct sctp_tmit_chunk * chk,uint32_t seq,uint32_t resp_seq,uint32_t last_sent)11613 sctp_add_stream_reset_out(struct sctp_tcb *stcb, struct sctp_tmit_chunk *chk,
11614 uint32_t seq, uint32_t resp_seq, uint32_t last_sent)
11615 {
11616 uint16_t len, old_len, i;
11617 struct sctp_stream_reset_out_request *req_out;
11618 struct sctp_chunkhdr *ch;
11619 int at;
11620 int number_entries = 0;
11621
11622 ch = mtod(chk->data, struct sctp_chunkhdr *);
11623 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11624 /* get to new offset for the param. */
11625 req_out = (struct sctp_stream_reset_out_request *)((caddr_t)ch + len);
11626 /* now how long will this param be? */
11627 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11628 if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11629 (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11630 TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11631 number_entries++;
11632 }
11633 }
11634 if (number_entries == 0) {
11635 return (0);
11636 }
11637 if (number_entries == stcb->asoc.streamoutcnt) {
11638 number_entries = 0;
11639 }
11640 if (number_entries > SCTP_MAX_STREAMS_AT_ONCE_RESET) {
11641 number_entries = SCTP_MAX_STREAMS_AT_ONCE_RESET;
11642 }
11643 len = (uint16_t)(sizeof(struct sctp_stream_reset_out_request) + (sizeof(uint16_t) * number_entries));
11644 req_out->ph.param_type = htons(SCTP_STR_RESET_OUT_REQUEST);
11645 req_out->ph.param_length = htons(len);
11646 req_out->request_seq = htonl(seq);
11647 req_out->response_seq = htonl(resp_seq);
11648 req_out->send_reset_at_tsn = htonl(last_sent);
11649 at = 0;
11650 if (number_entries) {
11651 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11652 if ((stcb->asoc.strmout[i].state == SCTP_STREAM_RESET_PENDING) &&
11653 (stcb->asoc.strmout[i].chunks_on_queues == 0) &&
11654 TAILQ_EMPTY(&stcb->asoc.strmout[i].outqueue)) {
11655 req_out->list_of_streams[at] = htons(i);
11656 at++;
11657 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11658 if (at >= number_entries) {
11659 break;
11660 }
11661 }
11662 }
11663 } else {
11664 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
11665 stcb->asoc.strmout[i].state = SCTP_STREAM_RESET_IN_FLIGHT;
11666 }
11667 }
11668 if (SCTP_SIZE32(len) > len) {
11669 /*-
11670 * Need to worry about the pad we may end up adding to the
11671 * end. This is easy since the struct is either aligned to 4
11672 * bytes or 2 bytes off.
11673 */
11674 req_out->list_of_streams[number_entries] = 0;
11675 }
11676 /* now fix the chunk length */
11677 ch->chunk_length = htons(len + old_len);
11678 chk->book_size = len + old_len;
11679 chk->book_size_scale = 0;
11680 chk->send_size = SCTP_SIZE32(chk->book_size);
11681 SCTP_BUF_LEN(chk->data) = chk->send_size;
11682 return (1);
11683 }
11684
11685 static void
sctp_add_stream_reset_in(struct sctp_tmit_chunk * chk,int number_entries,uint16_t * list,uint32_t seq)11686 sctp_add_stream_reset_in(struct sctp_tmit_chunk *chk,
11687 int number_entries, uint16_t *list,
11688 uint32_t seq)
11689 {
11690 uint16_t len, old_len, i;
11691 struct sctp_stream_reset_in_request *req_in;
11692 struct sctp_chunkhdr *ch;
11693
11694 ch = mtod(chk->data, struct sctp_chunkhdr *);
11695 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11696
11697 /* get to new offset for the param. */
11698 req_in = (struct sctp_stream_reset_in_request *)((caddr_t)ch + len);
11699 /* now how long will this param be? */
11700 len = (uint16_t)(sizeof(struct sctp_stream_reset_in_request) + (sizeof(uint16_t) * number_entries));
11701 req_in->ph.param_type = htons(SCTP_STR_RESET_IN_REQUEST);
11702 req_in->ph.param_length = htons(len);
11703 req_in->request_seq = htonl(seq);
11704 if (number_entries) {
11705 for (i = 0; i < number_entries; i++) {
11706 req_in->list_of_streams[i] = htons(list[i]);
11707 }
11708 }
11709 if (SCTP_SIZE32(len) > len) {
11710 /*-
11711 * Need to worry about the pad we may end up adding to the
11712 * end. This is easy since the struct is either aligned to 4
11713 * bytes or 2 bytes off.
11714 */
11715 req_in->list_of_streams[number_entries] = 0;
11716 }
11717 /* now fix the chunk length */
11718 ch->chunk_length = htons(len + old_len);
11719 chk->book_size = len + old_len;
11720 chk->book_size_scale = 0;
11721 chk->send_size = SCTP_SIZE32(chk->book_size);
11722 SCTP_BUF_LEN(chk->data) = chk->send_size;
11723 return;
11724 }
11725
11726 static void
sctp_add_stream_reset_tsn(struct sctp_tmit_chunk * chk,uint32_t seq)11727 sctp_add_stream_reset_tsn(struct sctp_tmit_chunk *chk,
11728 uint32_t seq)
11729 {
11730 uint16_t len, old_len;
11731 struct sctp_stream_reset_tsn_request *req_tsn;
11732 struct sctp_chunkhdr *ch;
11733
11734 ch = mtod(chk->data, struct sctp_chunkhdr *);
11735 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11736
11737 /* get to new offset for the param. */
11738 req_tsn = (struct sctp_stream_reset_tsn_request *)((caddr_t)ch + len);
11739 /* now how long will this param be? */
11740 len = sizeof(struct sctp_stream_reset_tsn_request);
11741 req_tsn->ph.param_type = htons(SCTP_STR_RESET_TSN_REQUEST);
11742 req_tsn->ph.param_length = htons(len);
11743 req_tsn->request_seq = htonl(seq);
11744
11745 /* now fix the chunk length */
11746 ch->chunk_length = htons(len + old_len);
11747 chk->send_size = len + old_len;
11748 chk->book_size = SCTP_SIZE32(chk->send_size);
11749 chk->book_size_scale = 0;
11750 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11751 return;
11752 }
11753
11754 void
sctp_add_stream_reset_result(struct sctp_tmit_chunk * chk,uint32_t resp_seq,uint32_t result)11755 sctp_add_stream_reset_result(struct sctp_tmit_chunk *chk,
11756 uint32_t resp_seq, uint32_t result)
11757 {
11758 uint16_t len, old_len;
11759 struct sctp_stream_reset_response *resp;
11760 struct sctp_chunkhdr *ch;
11761
11762 ch = mtod(chk->data, struct sctp_chunkhdr *);
11763 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11764
11765 /* get to new offset for the param. */
11766 resp = (struct sctp_stream_reset_response *)((caddr_t)ch + len);
11767 /* now how long will this param be? */
11768 len = sizeof(struct sctp_stream_reset_response);
11769 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11770 resp->ph.param_length = htons(len);
11771 resp->response_seq = htonl(resp_seq);
11772 resp->result = ntohl(result);
11773
11774 /* now fix the chunk length */
11775 ch->chunk_length = htons(len + old_len);
11776 chk->book_size = len + old_len;
11777 chk->book_size_scale = 0;
11778 chk->send_size = SCTP_SIZE32(chk->book_size);
11779 SCTP_BUF_LEN(chk->data) = chk->send_size;
11780 return;
11781 }
11782
11783 void
sctp_send_deferred_reset_response(struct sctp_tcb * stcb,struct sctp_stream_reset_list * ent,int response)11784 sctp_send_deferred_reset_response(struct sctp_tcb *stcb,
11785 struct sctp_stream_reset_list *ent,
11786 int response)
11787 {
11788 struct sctp_association *asoc;
11789 struct sctp_tmit_chunk *chk;
11790 struct sctp_chunkhdr *ch;
11791
11792 asoc = &stcb->asoc;
11793
11794 /*
11795 * Reset our last reset action to the new one IP -> response
11796 * (PERFORMED probably). This assures that if we fail to send, a
11797 * retran from the peer will get the new response.
11798 */
11799 asoc->last_reset_action[0] = response;
11800 if (asoc->stream_reset_outstanding) {
11801 return;
11802 }
11803 sctp_alloc_a_chunk(stcb, chk);
11804 if (chk == NULL) {
11805 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11806 return;
11807 }
11808 chk->copy_by_ref = 0;
11809 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11810 chk->rec.chunk_id.can_take_data = 0;
11811 chk->flags = 0;
11812 chk->asoc = &stcb->asoc;
11813 chk->book_size = sizeof(struct sctp_chunkhdr);
11814 chk->send_size = SCTP_SIZE32(chk->book_size);
11815 chk->book_size_scale = 0;
11816 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11817 if (chk->data == NULL) {
11818 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
11819 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11820 return;
11821 }
11822 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11823 /* setup chunk parameters */
11824 chk->sent = SCTP_DATAGRAM_UNSENT;
11825 chk->snd_count = 0;
11826 if (stcb->asoc.alternate) {
11827 chk->whoTo = stcb->asoc.alternate;
11828 } else {
11829 chk->whoTo = stcb->asoc.primary_destination;
11830 }
11831 ch = mtod(chk->data, struct sctp_chunkhdr *);
11832 ch->chunk_type = SCTP_STREAM_RESET;
11833 ch->chunk_flags = 0;
11834 ch->chunk_length = htons(chk->book_size);
11835 atomic_add_int(&chk->whoTo->ref_count, 1);
11836 SCTP_BUF_LEN(chk->data) = chk->send_size;
11837 sctp_add_stream_reset_result(chk, ent->seq, response);
11838 /* insert the chunk for sending */
11839 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
11840 chk,
11841 sctp_next);
11842 asoc->ctrl_queue_cnt++;
11843 }
11844
11845 void
sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk * chk,uint32_t resp_seq,uint32_t result,uint32_t send_una,uint32_t recv_next)11846 sctp_add_stream_reset_result_tsn(struct sctp_tmit_chunk *chk,
11847 uint32_t resp_seq, uint32_t result,
11848 uint32_t send_una, uint32_t recv_next)
11849 {
11850 uint16_t len, old_len;
11851 struct sctp_stream_reset_response_tsn *resp;
11852 struct sctp_chunkhdr *ch;
11853
11854 ch = mtod(chk->data, struct sctp_chunkhdr *);
11855 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11856
11857 /* get to new offset for the param. */
11858 resp = (struct sctp_stream_reset_response_tsn *)((caddr_t)ch + len);
11859 /* now how long will this param be? */
11860 len = sizeof(struct sctp_stream_reset_response_tsn);
11861 resp->ph.param_type = htons(SCTP_STR_RESET_RESPONSE);
11862 resp->ph.param_length = htons(len);
11863 resp->response_seq = htonl(resp_seq);
11864 resp->result = htonl(result);
11865 resp->senders_next_tsn = htonl(send_una);
11866 resp->receivers_next_tsn = htonl(recv_next);
11867
11868 /* now fix the chunk length */
11869 ch->chunk_length = htons(len + old_len);
11870 chk->book_size = len + old_len;
11871 chk->send_size = SCTP_SIZE32(chk->book_size);
11872 chk->book_size_scale = 0;
11873 SCTP_BUF_LEN(chk->data) = chk->send_size;
11874 return;
11875 }
11876
11877 static void
sctp_add_an_out_stream(struct sctp_tmit_chunk * chk,uint32_t seq,uint16_t adding)11878 sctp_add_an_out_stream(struct sctp_tmit_chunk *chk,
11879 uint32_t seq,
11880 uint16_t adding)
11881 {
11882 uint16_t len, old_len;
11883 struct sctp_chunkhdr *ch;
11884 struct sctp_stream_reset_add_strm *addstr;
11885
11886 ch = mtod(chk->data, struct sctp_chunkhdr *);
11887 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11888
11889 /* get to new offset for the param. */
11890 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11891 /* now how long will this param be? */
11892 len = sizeof(struct sctp_stream_reset_add_strm);
11893
11894 /* Fill it out. */
11895 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_OUT_STREAMS);
11896 addstr->ph.param_length = htons(len);
11897 addstr->request_seq = htonl(seq);
11898 addstr->number_of_streams = htons(adding);
11899 addstr->reserved = 0;
11900
11901 /* now fix the chunk length */
11902 ch->chunk_length = htons(len + old_len);
11903 chk->send_size = len + old_len;
11904 chk->book_size = SCTP_SIZE32(chk->send_size);
11905 chk->book_size_scale = 0;
11906 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11907 return;
11908 }
11909
11910 static void
sctp_add_an_in_stream(struct sctp_tmit_chunk * chk,uint32_t seq,uint16_t adding)11911 sctp_add_an_in_stream(struct sctp_tmit_chunk *chk,
11912 uint32_t seq,
11913 uint16_t adding)
11914 {
11915 uint16_t len, old_len;
11916 struct sctp_chunkhdr *ch;
11917 struct sctp_stream_reset_add_strm *addstr;
11918
11919 ch = mtod(chk->data, struct sctp_chunkhdr *);
11920 old_len = len = SCTP_SIZE32(ntohs(ch->chunk_length));
11921
11922 /* get to new offset for the param. */
11923 addstr = (struct sctp_stream_reset_add_strm *)((caddr_t)ch + len);
11924 /* now how long will this param be? */
11925 len = sizeof(struct sctp_stream_reset_add_strm);
11926 /* Fill it out. */
11927 addstr->ph.param_type = htons(SCTP_STR_RESET_ADD_IN_STREAMS);
11928 addstr->ph.param_length = htons(len);
11929 addstr->request_seq = htonl(seq);
11930 addstr->number_of_streams = htons(adding);
11931 addstr->reserved = 0;
11932
11933 /* now fix the chunk length */
11934 ch->chunk_length = htons(len + old_len);
11935 chk->send_size = len + old_len;
11936 chk->book_size = SCTP_SIZE32(chk->send_size);
11937 chk->book_size_scale = 0;
11938 SCTP_BUF_LEN(chk->data) = SCTP_SIZE32(chk->send_size);
11939 return;
11940 }
11941
11942 int
sctp_send_stream_reset_out_if_possible(struct sctp_tcb * stcb,int so_locked)11943 sctp_send_stream_reset_out_if_possible(struct sctp_tcb *stcb, int so_locked)
11944 {
11945 struct sctp_association *asoc;
11946 struct sctp_tmit_chunk *chk;
11947 struct sctp_chunkhdr *ch;
11948 uint32_t seq;
11949
11950 asoc = &stcb->asoc;
11951 asoc->trigger_reset = 0;
11952 if (asoc->stream_reset_outstanding) {
11953 return (EALREADY);
11954 }
11955 sctp_alloc_a_chunk(stcb, chk);
11956 if (chk == NULL) {
11957 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11958 return (ENOMEM);
11959 }
11960 chk->copy_by_ref = 0;
11961 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
11962 chk->rec.chunk_id.can_take_data = 0;
11963 chk->flags = 0;
11964 chk->asoc = &stcb->asoc;
11965 chk->book_size = sizeof(struct sctp_chunkhdr);
11966 chk->send_size = SCTP_SIZE32(chk->book_size);
11967 chk->book_size_scale = 0;
11968 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
11969 if (chk->data == NULL) {
11970 sctp_free_a_chunk(stcb, chk, so_locked);
11971 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
11972 return (ENOMEM);
11973 }
11974 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
11975
11976 /* setup chunk parameters */
11977 chk->sent = SCTP_DATAGRAM_UNSENT;
11978 chk->snd_count = 0;
11979 if (stcb->asoc.alternate) {
11980 chk->whoTo = stcb->asoc.alternate;
11981 } else {
11982 chk->whoTo = stcb->asoc.primary_destination;
11983 }
11984 ch = mtod(chk->data, struct sctp_chunkhdr *);
11985 ch->chunk_type = SCTP_STREAM_RESET;
11986 ch->chunk_flags = 0;
11987 ch->chunk_length = htons(chk->book_size);
11988 atomic_add_int(&chk->whoTo->ref_count, 1);
11989 SCTP_BUF_LEN(chk->data) = chk->send_size;
11990 seq = stcb->asoc.str_reset_seq_out;
11991 if (sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1))) {
11992 seq++;
11993 asoc->stream_reset_outstanding++;
11994 } else {
11995 m_freem(chk->data);
11996 chk->data = NULL;
11997 sctp_free_a_chunk(stcb, chk, so_locked);
11998 return (ENOENT);
11999 }
12000 asoc->str_reset = chk;
12001 /* insert the chunk for sending */
12002 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12003 chk,
12004 sctp_next);
12005 asoc->ctrl_queue_cnt++;
12006
12007 if (stcb->asoc.send_sack) {
12008 sctp_send_sack(stcb, so_locked);
12009 }
12010 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12011 return (0);
12012 }
12013
12014 int
sctp_send_str_reset_req(struct sctp_tcb * stcb,uint16_t number_entries,uint16_t * list,uint8_t send_in_req,uint8_t send_tsn_req,uint8_t add_stream,uint16_t adding_o,uint16_t adding_i,uint8_t peer_asked)12015 sctp_send_str_reset_req(struct sctp_tcb *stcb,
12016 uint16_t number_entries, uint16_t *list,
12017 uint8_t send_in_req,
12018 uint8_t send_tsn_req,
12019 uint8_t add_stream,
12020 uint16_t adding_o,
12021 uint16_t adding_i, uint8_t peer_asked)
12022 {
12023 struct sctp_association *asoc;
12024 struct sctp_tmit_chunk *chk;
12025 struct sctp_chunkhdr *ch;
12026 int can_send_out_req = 0;
12027 uint32_t seq;
12028
12029 SCTP_TCB_LOCK_ASSERT(stcb);
12030
12031 asoc = &stcb->asoc;
12032 if (asoc->stream_reset_outstanding) {
12033 /*-
12034 * Already one pending, must get ACK back to clear the flag.
12035 */
12036 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EBUSY);
12037 return (EBUSY);
12038 }
12039 if ((send_in_req == 0) && (send_tsn_req == 0) &&
12040 (add_stream == 0)) {
12041 /* nothing to do */
12042 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12043 return (EINVAL);
12044 }
12045 if (send_tsn_req && send_in_req) {
12046 /* error, can't do that */
12047 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12048 return (EINVAL);
12049 } else if (send_in_req) {
12050 can_send_out_req = 1;
12051 }
12052 if (number_entries > (MCLBYTES -
12053 SCTP_MIN_OVERHEAD -
12054 sizeof(struct sctp_chunkhdr) -
12055 sizeof(struct sctp_stream_reset_out_request)) /
12056 sizeof(uint16_t)) {
12057 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12058 return (ENOMEM);
12059 }
12060 sctp_alloc_a_chunk(stcb, chk);
12061 if (chk == NULL) {
12062 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12063 return (ENOMEM);
12064 }
12065 chk->copy_by_ref = 0;
12066 chk->rec.chunk_id.id = SCTP_STREAM_RESET;
12067 chk->rec.chunk_id.can_take_data = 0;
12068 chk->flags = 0;
12069 chk->asoc = &stcb->asoc;
12070 chk->book_size = sizeof(struct sctp_chunkhdr);
12071 chk->send_size = SCTP_SIZE32(chk->book_size);
12072 chk->book_size_scale = 0;
12073 chk->data = sctp_get_mbuf_for_msg(MCLBYTES, 0, M_NOWAIT, 1, MT_DATA);
12074 if (chk->data == NULL) {
12075 sctp_free_a_chunk(stcb, chk, SCTP_SO_LOCKED);
12076 SCTP_LTRACE_ERR_RET(NULL, stcb, NULL, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12077 return (ENOMEM);
12078 }
12079 SCTP_BUF_RESV_UF(chk->data, SCTP_MIN_OVERHEAD);
12080
12081 /* setup chunk parameters */
12082 chk->sent = SCTP_DATAGRAM_UNSENT;
12083 chk->snd_count = 0;
12084 if (stcb->asoc.alternate) {
12085 chk->whoTo = stcb->asoc.alternate;
12086 } else {
12087 chk->whoTo = stcb->asoc.primary_destination;
12088 }
12089 atomic_add_int(&chk->whoTo->ref_count, 1);
12090 ch = mtod(chk->data, struct sctp_chunkhdr *);
12091 ch->chunk_type = SCTP_STREAM_RESET;
12092 ch->chunk_flags = 0;
12093 ch->chunk_length = htons(chk->book_size);
12094 SCTP_BUF_LEN(chk->data) = chk->send_size;
12095
12096 seq = stcb->asoc.str_reset_seq_out;
12097 if (can_send_out_req) {
12098 int ret;
12099
12100 ret = sctp_add_stream_reset_out(stcb, chk, seq, (stcb->asoc.str_reset_seq_in - 1), (stcb->asoc.sending_seq - 1));
12101 if (ret) {
12102 seq++;
12103 asoc->stream_reset_outstanding++;
12104 }
12105 }
12106 if ((add_stream & 1) &&
12107 ((stcb->asoc.strm_realoutsize - stcb->asoc.streamoutcnt) < adding_o)) {
12108 /* Need to allocate more */
12109 struct sctp_stream_out *oldstream;
12110 struct sctp_stream_queue_pending *sp, *nsp;
12111 int i;
12112 #if defined(SCTP_DETAILED_STR_STATS)
12113 int j;
12114 #endif
12115
12116 oldstream = stcb->asoc.strmout;
12117 /* get some more */
12118 SCTP_MALLOC(stcb->asoc.strmout, struct sctp_stream_out *,
12119 (stcb->asoc.streamoutcnt + adding_o) * sizeof(struct sctp_stream_out),
12120 SCTP_M_STRMO);
12121 if (stcb->asoc.strmout == NULL) {
12122 uint8_t x;
12123
12124 stcb->asoc.strmout = oldstream;
12125 /* Turn off the bit */
12126 x = add_stream & 0xfe;
12127 add_stream = x;
12128 goto skip_stuff;
12129 }
12130 /*
12131 * Ok now we proceed with copying the old out stuff and
12132 * initializing the new stuff.
12133 */
12134 stcb->asoc.ss_functions.sctp_ss_clear(stcb, &stcb->asoc, false);
12135 for (i = 0; i < stcb->asoc.streamoutcnt; i++) {
12136 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12137 /* FIX ME FIX ME */
12138 /*
12139 * This should be a SS_COPY operation FIX ME STREAM
12140 * SCHEDULER EXPERT
12141 */
12142 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], &oldstream[i]);
12143 stcb->asoc.strmout[i].chunks_on_queues = oldstream[i].chunks_on_queues;
12144 #if defined(SCTP_DETAILED_STR_STATS)
12145 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12146 stcb->asoc.strmout[i].abandoned_sent[j] = oldstream[i].abandoned_sent[j];
12147 stcb->asoc.strmout[i].abandoned_unsent[j] = oldstream[i].abandoned_unsent[j];
12148 }
12149 #else
12150 stcb->asoc.strmout[i].abandoned_sent[0] = oldstream[i].abandoned_sent[0];
12151 stcb->asoc.strmout[i].abandoned_unsent[0] = oldstream[i].abandoned_unsent[0];
12152 #endif
12153 stcb->asoc.strmout[i].next_mid_ordered = oldstream[i].next_mid_ordered;
12154 stcb->asoc.strmout[i].next_mid_unordered = oldstream[i].next_mid_unordered;
12155 stcb->asoc.strmout[i].last_msg_incomplete = oldstream[i].last_msg_incomplete;
12156 stcb->asoc.strmout[i].sid = i;
12157 stcb->asoc.strmout[i].state = oldstream[i].state;
12158 /* now anything on those queues? */
12159 TAILQ_FOREACH_SAFE(sp, &oldstream[i].outqueue, next, nsp) {
12160 TAILQ_REMOVE(&oldstream[i].outqueue, sp, next);
12161 TAILQ_INSERT_TAIL(&stcb->asoc.strmout[i].outqueue, sp, next);
12162 }
12163 }
12164 /* now the new streams */
12165 stcb->asoc.ss_functions.sctp_ss_init(stcb, &stcb->asoc);
12166 for (i = stcb->asoc.streamoutcnt; i < (stcb->asoc.streamoutcnt + adding_o); i++) {
12167 TAILQ_INIT(&stcb->asoc.strmout[i].outqueue);
12168 stcb->asoc.strmout[i].chunks_on_queues = 0;
12169 #if defined(SCTP_DETAILED_STR_STATS)
12170 for (j = 0; j < SCTP_PR_SCTP_MAX + 1; j++) {
12171 stcb->asoc.strmout[i].abandoned_sent[j] = 0;
12172 stcb->asoc.strmout[i].abandoned_unsent[j] = 0;
12173 }
12174 #else
12175 stcb->asoc.strmout[i].abandoned_sent[0] = 0;
12176 stcb->asoc.strmout[i].abandoned_unsent[0] = 0;
12177 #endif
12178 stcb->asoc.strmout[i].next_mid_ordered = 0;
12179 stcb->asoc.strmout[i].next_mid_unordered = 0;
12180 stcb->asoc.strmout[i].sid = i;
12181 stcb->asoc.strmout[i].last_msg_incomplete = 0;
12182 stcb->asoc.ss_functions.sctp_ss_init_stream(stcb, &stcb->asoc.strmout[i], NULL);
12183 stcb->asoc.strmout[i].state = SCTP_STREAM_CLOSED;
12184 }
12185 stcb->asoc.strm_realoutsize = stcb->asoc.streamoutcnt + adding_o;
12186 SCTP_FREE(oldstream, SCTP_M_STRMO);
12187 }
12188 skip_stuff:
12189 if ((add_stream & 1) && (adding_o > 0)) {
12190 asoc->strm_pending_add_size = adding_o;
12191 asoc->peer_req_out = peer_asked;
12192 sctp_add_an_out_stream(chk, seq, adding_o);
12193 seq++;
12194 asoc->stream_reset_outstanding++;
12195 }
12196 if ((add_stream & 2) && (adding_i > 0)) {
12197 sctp_add_an_in_stream(chk, seq, adding_i);
12198 seq++;
12199 asoc->stream_reset_outstanding++;
12200 }
12201 if (send_in_req) {
12202 sctp_add_stream_reset_in(chk, number_entries, list, seq);
12203 seq++;
12204 asoc->stream_reset_outstanding++;
12205 }
12206 if (send_tsn_req) {
12207 sctp_add_stream_reset_tsn(chk, seq);
12208 asoc->stream_reset_outstanding++;
12209 }
12210 asoc->str_reset = chk;
12211 /* insert the chunk for sending */
12212 TAILQ_INSERT_TAIL(&asoc->control_send_queue,
12213 chk,
12214 sctp_next);
12215 asoc->ctrl_queue_cnt++;
12216 if (stcb->asoc.send_sack) {
12217 sctp_send_sack(stcb, SCTP_SO_LOCKED);
12218 }
12219 sctp_timer_start(SCTP_TIMER_TYPE_STRRESET, stcb->sctp_ep, stcb, chk->whoTo);
12220 return (0);
12221 }
12222
12223 void
sctp_send_abort(struct mbuf * m,int iphlen,struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,uint32_t vtag,struct mbuf * cause,uint8_t mflowtype,uint32_t mflowid,uint16_t fibnum,uint32_t vrf_id,uint16_t port)12224 sctp_send_abort(struct mbuf *m, int iphlen, struct sockaddr *src, struct sockaddr *dst,
12225 struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12226 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12227 uint32_t vrf_id, uint16_t port)
12228 {
12229 /* Don't respond to an ABORT with an ABORT. */
12230 if (sctp_is_there_an_abort_here(m, iphlen, &vtag)) {
12231 if (cause)
12232 sctp_m_freem(cause);
12233 return;
12234 }
12235 sctp_send_resp_msg(src, dst, sh, vtag, SCTP_ABORT_ASSOCIATION, cause,
12236 mflowtype, mflowid, fibnum,
12237 vrf_id, port);
12238 return;
12239 }
12240
12241 void
sctp_send_operr_to(struct sockaddr * src,struct sockaddr * dst,struct sctphdr * sh,uint32_t vtag,struct mbuf * cause,uint8_t mflowtype,uint32_t mflowid,uint16_t fibnum,uint32_t vrf_id,uint16_t port)12242 sctp_send_operr_to(struct sockaddr *src, struct sockaddr *dst,
12243 struct sctphdr *sh, uint32_t vtag, struct mbuf *cause,
12244 uint8_t mflowtype, uint32_t mflowid, uint16_t fibnum,
12245 uint32_t vrf_id, uint16_t port)
12246 {
12247 sctp_send_resp_msg(src, dst, sh, vtag, SCTP_OPERATION_ERROR, cause,
12248 mflowtype, mflowid, fibnum,
12249 vrf_id, port);
12250 return;
12251 }
12252
12253 static struct mbuf *
sctp_copy_resume(struct uio * uio,int max_send_len,int user_marks_eor,int * error,uint32_t * sndout,struct mbuf ** new_tail)12254 sctp_copy_resume(struct uio *uio,
12255 int max_send_len,
12256 int user_marks_eor,
12257 int *error,
12258 uint32_t *sndout,
12259 struct mbuf **new_tail)
12260 {
12261 struct mbuf *m;
12262
12263 m = m_uiotombuf(uio, M_WAITOK, max_send_len, 0,
12264 (M_PKTHDR | (user_marks_eor ? M_EOR : 0)));
12265 if (m == NULL) {
12266 /* The only possible error is EFAULT. */
12267 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12268 *error = EFAULT;
12269 } else {
12270 *sndout = m_length(m, NULL);
12271 *new_tail = m_last(m);
12272 }
12273 return (m);
12274 }
12275
12276 static int
sctp_copy_one(struct sctp_stream_queue_pending * sp,struct uio * uio,int resv_upfront)12277 sctp_copy_one(struct sctp_stream_queue_pending *sp,
12278 struct uio *uio,
12279 int resv_upfront)
12280 {
12281 sp->data = m_uiotombuf(uio, M_WAITOK, sp->length, resv_upfront, 0);
12282 if (sp->data == NULL) {
12283 /* The only possible error is EFAULT. */
12284 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EFAULT);
12285 return (EFAULT);
12286 }
12287 sp->tail_mbuf = m_last(sp->data);
12288 return (0);
12289 }
12290
12291 static struct sctp_stream_queue_pending *
sctp_copy_it_in(struct sctp_tcb * stcb,struct sctp_association * asoc,struct sctp_nonpad_sndrcvinfo * srcv,struct uio * uio,struct sctp_nets * net,ssize_t max_send_len,int user_marks_eor,int * error)12292 sctp_copy_it_in(struct sctp_tcb *stcb,
12293 struct sctp_association *asoc,
12294 struct sctp_nonpad_sndrcvinfo *srcv,
12295 struct uio *uio,
12296 struct sctp_nets *net,
12297 ssize_t max_send_len,
12298 int user_marks_eor,
12299 int *error)
12300 {
12301
12302 /*-
12303 * This routine must be very careful in its work. Protocol
12304 * processing is up and running so care must be taken to spl...()
12305 * when you need to do something that may effect the stcb/asoc. The
12306 * sb is locked however. When data is copied the protocol processing
12307 * should be enabled since this is a slower operation...
12308 */
12309 struct sctp_stream_queue_pending *sp;
12310 int resv_in_first;
12311
12312 *error = 0;
12313 sctp_alloc_a_strmoq(stcb, sp);
12314 if (sp == NULL) {
12315 SCTP_LTRACE_ERR_RET(NULL, stcb, net, SCTP_FROM_SCTP_OUTPUT, ENOMEM);
12316 *error = ENOMEM;
12317 goto out_now;
12318 }
12319 sp->act_flags = 0;
12320 sp->sender_all_done = 0;
12321 sp->sinfo_flags = srcv->sinfo_flags;
12322 sp->timetolive = srcv->sinfo_timetolive;
12323 sp->ppid = srcv->sinfo_ppid;
12324 sp->context = srcv->sinfo_context;
12325 sp->fsn = 0;
12326 (void)SCTP_GETTIME_TIMEVAL(&sp->ts);
12327 sp->sid = srcv->sinfo_stream;
12328 sp->length = (uint32_t)min(uio->uio_resid, max_send_len);
12329 if ((sp->length == (uint32_t)uio->uio_resid) &&
12330 ((user_marks_eor == 0) ||
12331 (srcv->sinfo_flags & SCTP_EOF) ||
12332 (user_marks_eor && (srcv->sinfo_flags & SCTP_EOR)))) {
12333 sp->msg_is_complete = 1;
12334 } else {
12335 sp->msg_is_complete = 0;
12336 }
12337 sp->sender_all_done = 0;
12338 sp->some_taken = 0;
12339 sp->put_last_out = 0;
12340 resv_in_first = SCTP_DATA_CHUNK_OVERHEAD(stcb);
12341 sp->data = sp->tail_mbuf = NULL;
12342 if (sp->length == 0) {
12343 goto skip_copy;
12344 }
12345 if (srcv->sinfo_keynumber_valid) {
12346 sp->auth_keyid = srcv->sinfo_keynumber;
12347 } else {
12348 sp->auth_keyid = stcb->asoc.authinfo.active_keyid;
12349 }
12350 if (sctp_auth_is_required_chunk(SCTP_DATA, stcb->asoc.peer_auth_chunks)) {
12351 sctp_auth_key_acquire(stcb, sp->auth_keyid);
12352 sp->holds_key_ref = 1;
12353 }
12354 *error = sctp_copy_one(sp, uio, resv_in_first);
12355 skip_copy:
12356 if (*error) {
12357 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
12358 sp = NULL;
12359 } else {
12360 if (sp->sinfo_flags & SCTP_ADDR_OVER) {
12361 sp->net = net;
12362 atomic_add_int(&sp->net->ref_count, 1);
12363 } else {
12364 sp->net = NULL;
12365 }
12366 sctp_set_prsctp_policy(sp);
12367 }
12368 out_now:
12369 return (sp);
12370 }
12371
12372 int
sctp_sosend(struct socket * so,struct sockaddr * addr,struct uio * uio,struct mbuf * top,struct mbuf * control,int flags,struct thread * p)12373 sctp_sosend(struct socket *so,
12374 struct sockaddr *addr,
12375 struct uio *uio,
12376 struct mbuf *top,
12377 struct mbuf *control,
12378 int flags,
12379 struct thread *p)
12380 {
12381 struct sctp_sndrcvinfo sndrcvninfo;
12382 #if defined(INET) && defined(INET6)
12383 struct sockaddr_in sin;
12384 #endif
12385 struct sockaddr *addr_to_use;
12386 int error;
12387 bool use_sndinfo;
12388
12389 if (control != NULL) {
12390 /* process cmsg snd/rcv info (maybe a assoc-id) */
12391 use_sndinfo = sctp_find_cmsg(SCTP_SNDRCV, (void *)&sndrcvninfo, control, sizeof(sndrcvninfo));
12392 } else {
12393 use_sndinfo = false;
12394 }
12395 #if defined(INET) && defined(INET6)
12396 if ((addr != NULL) && (addr->sa_family == AF_INET6)) {
12397 struct sockaddr_in6 *sin6;
12398
12399 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
12400 SCTP_LTRACE_ERR_RET(NULL, NULL, NULL, SCTP_FROM_SCTP_OUTPUT, EINVAL);
12401 return (EINVAL);
12402 }
12403 sin6 = (struct sockaddr_in6 *)addr;
12404 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
12405 in6_sin6_2_sin(&sin, sin6);
12406 addr_to_use = (struct sockaddr *)&sin;
12407 } else {
12408 addr_to_use = addr;
12409 }
12410 } else {
12411 addr_to_use = addr;
12412 }
12413 #else
12414 addr_to_use = addr;
12415 #endif
12416 error = sctp_lower_sosend(so, addr_to_use, uio, top, control, flags,
12417 use_sndinfo ? &sndrcvninfo : NULL, p);
12418 return (error);
12419 }
12420
12421 int
sctp_lower_sosend(struct socket * so,struct sockaddr * addr,struct uio * uio,struct mbuf * top,struct mbuf * control,int flags,struct sctp_sndrcvinfo * srcv,struct thread * p)12422 sctp_lower_sosend(struct socket *so,
12423 struct sockaddr *addr,
12424 struct uio *uio,
12425 struct mbuf *top,
12426 struct mbuf *control,
12427 int flags,
12428 struct sctp_sndrcvinfo *srcv,
12429 struct thread *p)
12430 {
12431 struct sctp_nonpad_sndrcvinfo sndrcvninfo_buf;
12432 struct epoch_tracker et;
12433 struct timeval now;
12434 struct sctp_block_entry be;
12435 struct sctp_inpcb *inp;
12436 struct sctp_tcb *stcb = NULL;
12437 struct sctp_nets *net;
12438 struct sctp_association *asoc;
12439 struct sctp_inpcb *t_inp;
12440 struct sctp_nonpad_sndrcvinfo *sndrcvninfo;
12441 ssize_t sndlen = 0, max_len, local_add_more;
12442 ssize_t local_soresv = 0;
12443 sctp_assoc_t sinfo_assoc_id;
12444 int user_marks_eor;
12445 int nagle_applies = 0;
12446 int error;
12447 int queue_only = 0, queue_only_for_init = 0;
12448 int un_sent;
12449 int now_filled = 0;
12450 unsigned int inqueue_bytes = 0;
12451 uint16_t port;
12452 uint16_t sinfo_flags;
12453 uint16_t sinfo_stream;
12454 bool create_lock_applied = false;
12455 bool free_cnt_applied = false;
12456 bool some_on_control;
12457 bool got_all_of_the_send = false;
12458 bool non_blocking = false;
12459
12460 error = 0;
12461 net = NULL;
12462 stcb = NULL;
12463
12464 if ((uio == NULL) && (top == NULL)) {
12465 error = EINVAL;
12466 goto out_unlocked;
12467 }
12468 if (addr != NULL) {
12469 union sctp_sockstore *raddr = (union sctp_sockstore *)addr;
12470
12471 switch (raddr->sa.sa_family) {
12472 #ifdef INET
12473 case AF_INET:
12474 if (raddr->sin.sin_len != sizeof(struct sockaddr_in)) {
12475 error = EINVAL;
12476 goto out_unlocked;
12477 }
12478 port = raddr->sin.sin_port;
12479 break;
12480 #endif
12481 #ifdef INET6
12482 case AF_INET6:
12483 if (raddr->sin6.sin6_len != sizeof(struct sockaddr_in6)) {
12484 error = EINVAL;
12485 goto out_unlocked;
12486 }
12487 port = raddr->sin6.sin6_port;
12488 break;
12489 #endif
12490 default:
12491 error = EAFNOSUPPORT;
12492 goto out_unlocked;
12493 }
12494 } else {
12495 port = 0;
12496 }
12497 if (uio != NULL) {
12498 if (uio->uio_resid < 0) {
12499 error = EINVAL;
12500 goto out_unlocked;
12501 }
12502 sndlen = uio->uio_resid;
12503 } else {
12504 sndlen = SCTP_HEADER_LEN(top);
12505 }
12506 SCTPDBG(SCTP_DEBUG_OUTPUT1, "Send called addr:%p send length %zd\n",
12507 (void *)addr, sndlen);
12508
12509 t_inp = inp = (struct sctp_inpcb *)so->so_pcb;
12510 if (inp == NULL) {
12511 error = EINVAL;
12512 goto out_unlocked;
12513 }
12514 user_marks_eor = sctp_is_feature_on(inp, SCTP_PCB_FLAGS_EXPLICIT_EOR);
12515 if ((uio == NULL) && (user_marks_eor != 0)) {
12516 /*-
12517 * We do not support eeor mode for
12518 * sending with mbuf chains (like sendfile).
12519 */
12520 error = EINVAL;
12521 goto out_unlocked;
12522 }
12523 if ((inp->sctp_flags & SCTP_PCB_FLAGS_TCPTYPE) &&
12524 SCTP_IS_LISTENING(inp)) {
12525 /* The listener can NOT send. */
12526 error = EINVAL;
12527 goto out_unlocked;
12528 }
12529 atomic_add_int(&inp->total_sends, 1);
12530
12531 if (srcv != NULL) {
12532 sndrcvninfo = (struct sctp_nonpad_sndrcvinfo *)srcv;
12533 sinfo_assoc_id = sndrcvninfo->sinfo_assoc_id;
12534 sinfo_flags = sndrcvninfo->sinfo_flags;
12535 if (INVALID_SINFO_FLAG(sinfo_flags) ||
12536 PR_SCTP_INVALID_POLICY(sinfo_flags)) {
12537 error = EINVAL;
12538 goto out_unlocked;
12539 }
12540 if (sinfo_flags != 0) {
12541 SCTP_STAT_INCR(sctps_sends_with_flags);
12542 }
12543 } else {
12544 sndrcvninfo = NULL;
12545 sinfo_flags = inp->def_send.sinfo_flags;
12546 sinfo_assoc_id = inp->def_send.sinfo_assoc_id;
12547 }
12548 if (flags & MSG_EOR) {
12549 sinfo_flags |= SCTP_EOR;
12550 }
12551 if (flags & MSG_EOF) {
12552 sinfo_flags |= SCTP_EOF;
12553 }
12554 if ((sinfo_flags & SCTP_ADDR_OVER) && (addr == NULL)) {
12555 error = EINVAL;
12556 goto out_unlocked;
12557 }
12558 SCTP_INP_RLOCK(inp);
12559 if ((sinfo_flags & SCTP_SENDALL) &&
12560 (inp->sctp_flags & SCTP_PCB_FLAGS_UDPTYPE)) {
12561 SCTP_INP_RUNLOCK(inp);
12562 error = sctp_sendall(inp, uio, top, sndrcvninfo);
12563 top = NULL;
12564 goto out_unlocked;
12565 }
12566 /* Now we must find the association. */
12567 if ((inp->sctp_flags & SCTP_PCB_FLAGS_CONNECTED) ||
12568 (inp->sctp_flags & SCTP_PCB_FLAGS_IN_TCPPOOL)) {
12569 stcb = LIST_FIRST(&inp->sctp_asoc_list);
12570 if (stcb != NULL) {
12571 SCTP_TCB_LOCK(stcb);
12572 }
12573 SCTP_INP_RUNLOCK(inp);
12574 } else if (sinfo_assoc_id > SCTP_ALL_ASSOC) {
12575 stcb = sctp_findasoc_ep_asocid_locked(inp, sinfo_assoc_id, 1);
12576 SCTP_INP_RUNLOCK(inp);
12577 if (stcb != NULL) {
12578 SCTP_TCB_LOCK_ASSERT(stcb);
12579 }
12580 } else if (addr != NULL) {
12581 /*-
12582 * Since we did not use findep we must
12583 * increment it, and if we don't find a tcb
12584 * decrement it.
12585 */
12586 SCTP_INP_INCR_REF(inp);
12587 SCTP_INP_RUNLOCK(inp);
12588 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12589 if (stcb == NULL) {
12590 SCTP_INP_WLOCK(inp);
12591 SCTP_INP_DECR_REF(inp);
12592 SCTP_INP_WUNLOCK(inp);
12593 } else {
12594 SCTP_TCB_LOCK_ASSERT(stcb);
12595 }
12596 } else {
12597 SCTP_INP_RUNLOCK(inp);
12598 }
12599
12600 #ifdef INVARIANTS
12601 if (stcb != NULL) {
12602 SCTP_TCB_LOCK_ASSERT(stcb);
12603 }
12604 #endif
12605
12606 if ((stcb == NULL) && (addr != NULL)) {
12607 /* Possible implicit send? */
12608 SCTP_ASOC_CREATE_LOCK(inp);
12609 create_lock_applied = true;
12610 if ((inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
12611 (inp->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE)) {
12612 error = EINVAL;
12613 goto out_unlocked;
12614 }
12615 if (((inp->sctp_flags & SCTP_PCB_FLAGS_BOUND_V6) == 0) &&
12616 (addr->sa_family == AF_INET6)) {
12617 error = EINVAL;
12618 goto out_unlocked;
12619 }
12620 SCTP_INP_WLOCK(inp);
12621 SCTP_INP_INCR_REF(inp);
12622 SCTP_INP_WUNLOCK(inp);
12623 /* With the lock applied look again */
12624 stcb = sctp_findassociation_ep_addr(&t_inp, addr, &net, NULL, NULL);
12625 #if defined(INET) || defined(INET6)
12626 if ((stcb == NULL) && (control != NULL) && (port > 0)) {
12627 stcb = sctp_findassociation_cmsgs(&t_inp, port, control, &net, &error);
12628 }
12629 #endif
12630 if (stcb == NULL) {
12631 SCTP_INP_WLOCK(inp);
12632 SCTP_INP_DECR_REF(inp);
12633 SCTP_INP_WUNLOCK(inp);
12634 } else {
12635 SCTP_TCB_LOCK_ASSERT(stcb);
12636 SCTP_ASOC_CREATE_UNLOCK(inp);
12637 create_lock_applied = false;
12638 }
12639 if (error != 0) {
12640 goto out_unlocked;
12641 }
12642 if (t_inp != inp) {
12643 error = ENOTCONN;
12644 goto out_unlocked;
12645 }
12646 }
12647 if (stcb == NULL) {
12648 if (addr == NULL) {
12649 error = ENOENT;
12650 goto out_unlocked;
12651 } else {
12652 /* We must go ahead and start the INIT process */
12653 uint32_t vrf_id;
12654
12655 if ((sinfo_flags & SCTP_ABORT) ||
12656 ((sinfo_flags & SCTP_EOF) && (sndlen == 0))) {
12657 /*-
12658 * User asks to abort a non-existent assoc,
12659 * or EOF a non-existent assoc with no data
12660 */
12661 error = ENOENT;
12662 goto out_unlocked;
12663 }
12664 /* get an asoc/stcb struct */
12665 vrf_id = inp->def_vrf_id;
12666 KASSERT(create_lock_applied, ("create_lock_applied is false"));
12667 stcb = sctp_aloc_assoc_connected(inp, addr, &error, 0, 0, vrf_id,
12668 inp->sctp_ep.pre_open_stream_count,
12669 inp->sctp_ep.port,
12670 p,
12671 SCTP_INITIALIZE_AUTH_PARAMS);
12672 if (stcb == NULL) {
12673 /* error is setup for us in the call. */
12674 KASSERT(error != 0, ("error is 0 although stcb is NULL"));
12675 goto out_unlocked;
12676 }
12677 SCTP_TCB_LOCK_ASSERT(stcb);
12678 SCTP_ASOC_CREATE_UNLOCK(inp);
12679 create_lock_applied = false;
12680 /*
12681 * Turn on queue only flag to prevent data from
12682 * being sent
12683 */
12684 queue_only = 1;
12685 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
12686 (void)SCTP_GETTIME_TIMEVAL(&stcb->asoc.time_entered);
12687 if (control != NULL) {
12688 if (sctp_process_cmsgs_for_init(stcb, control, &error)) {
12689 sctp_free_assoc(inp, stcb, SCTP_NORMAL_PROC,
12690 SCTP_FROM_SCTP_OUTPUT + SCTP_LOC_6);
12691 stcb = NULL;
12692 KASSERT(error != 0,
12693 ("error is 0 although sctp_process_cmsgs_for_init() indicated an error"));
12694 goto out_unlocked;
12695 }
12696 }
12697 /* out with the INIT */
12698 queue_only_for_init = 1;
12699 /*-
12700 * we may want to dig in after this call and adjust the MTU
12701 * value. It defaulted to 1500 (constant) but the ro
12702 * structure may now have an update and thus we may need to
12703 * change it BEFORE we append the message.
12704 */
12705 }
12706 }
12707
12708 KASSERT(!create_lock_applied, ("create_lock_applied is true"));
12709 KASSERT(stcb != NULL, ("stcb is NULL"));
12710 SCTP_TCB_LOCK_ASSERT(stcb);
12711
12712 asoc = &stcb->asoc;
12713 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12714 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12715 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12716 /* XXX: Could also be ECONNABORTED, not enough info. */
12717 error = ECONNRESET;
12718 } else {
12719 error = ENOTCONN;
12720 }
12721 goto out_unlocked;
12722 }
12723 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12724 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12725 queue_only = 1;
12726 }
12727 /* Keep the stcb from being freed under our feet. */
12728 atomic_add_int(&asoc->refcnt, 1);
12729 free_cnt_applied = true;
12730 if (sndrcvninfo == NULL) {
12731 /* Use a local copy to have a consistent view. */
12732 sndrcvninfo_buf = asoc->def_send;
12733 sndrcvninfo = &sndrcvninfo_buf;
12734 sinfo_flags = sndrcvninfo->sinfo_flags;
12735 if (flags & MSG_EOR) {
12736 sinfo_flags |= SCTP_EOR;
12737 }
12738 if (flags & MSG_EOF) {
12739 sinfo_flags |= SCTP_EOF;
12740 }
12741 }
12742 /* Are we aborting? */
12743 if (sinfo_flags & SCTP_ABORT) {
12744 struct mbuf *mm;
12745 struct sctp_paramhdr *ph;
12746 ssize_t tot_demand, tot_out = 0, max_out;
12747
12748 SCTP_STAT_INCR(sctps_sends_with_abort);
12749 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_WAIT) ||
12750 (SCTP_GET_STATE(stcb) == SCTP_STATE_COOKIE_ECHOED)) {
12751 /* It has to be up before we abort. */
12752 error = EINVAL;
12753 goto out_unlocked;
12754 }
12755 /* How big is the user initiated abort? */
12756 if (top != NULL) {
12757 struct mbuf *cntm;
12758
12759 if (sndlen != 0) {
12760 for (cntm = top; cntm; cntm = SCTP_BUF_NEXT(cntm)) {
12761 tot_out += SCTP_BUF_LEN(cntm);
12762 }
12763 }
12764 mm = sctp_get_mbuf_for_msg(sizeof(struct sctp_paramhdr), 0, M_NOWAIT, 1, MT_DATA);
12765 } else {
12766 /* Must fit in a MTU */
12767 tot_out = sndlen;
12768 tot_demand = (tot_out + sizeof(struct sctp_paramhdr));
12769 if (tot_demand > SCTP_DEFAULT_ADD_MORE) {
12770 error = EMSGSIZE;
12771 goto out_unlocked;
12772 }
12773 mm = sctp_get_mbuf_for_msg((unsigned int)tot_demand, 0, M_NOWAIT, 1, MT_DATA);
12774 }
12775 if (mm == NULL) {
12776 error = ENOMEM;
12777 goto out_unlocked;
12778 }
12779 max_out = asoc->smallest_mtu - sizeof(struct sctp_paramhdr);
12780 max_out -= sizeof(struct sctp_abort_msg);
12781 if (tot_out > max_out) {
12782 tot_out = max_out;
12783 }
12784 ph = mtod(mm, struct sctp_paramhdr *);
12785 ph->param_type = htons(SCTP_CAUSE_USER_INITIATED_ABT);
12786 ph->param_length = htons((uint16_t)(sizeof(struct sctp_paramhdr) + tot_out));
12787 ph++;
12788 SCTP_BUF_LEN(mm) = (int)(tot_out + sizeof(struct sctp_paramhdr));
12789 if (top == NULL) {
12790 SCTP_TCB_UNLOCK(stcb);
12791 error = uiomove((caddr_t)ph, (int)tot_out, uio);
12792 SCTP_TCB_LOCK(stcb);
12793 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
12794 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
12795 sctp_m_freem(mm);
12796 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
12797 /*
12798 * XXX: Could also be ECONNABORTED,
12799 * not enough info.
12800 */
12801 error = ECONNRESET;
12802 } else {
12803 error = ENOTCONN;
12804 }
12805 goto out_unlocked;
12806 }
12807 if (error != 0) {
12808 /*-
12809 * Here if we can't get his data we
12810 * still abort we just don't get to
12811 * send the users note :-0
12812 */
12813 sctp_m_freem(mm);
12814 mm = NULL;
12815 error = 0;
12816 }
12817 } else {
12818 if (sndlen != 0) {
12819 SCTP_BUF_NEXT(mm) = top;
12820 }
12821 }
12822 atomic_subtract_int(&asoc->refcnt, 1);
12823 free_cnt_applied = false;
12824 /* release this lock, otherwise we hang on ourselves */
12825 NET_EPOCH_ENTER(et);
12826 sctp_abort_an_association(stcb->sctp_ep, stcb, mm, false, SCTP_SO_LOCKED);
12827 NET_EPOCH_EXIT(et);
12828 stcb = NULL;
12829 /*
12830 * In this case top is already chained to mm avoid double
12831 * free, since we free it below if top != NULL and driver
12832 * would free it after sending the packet out
12833 */
12834 if (sndlen != 0) {
12835 top = NULL;
12836 }
12837 goto out_unlocked;
12838 }
12839
12840 KASSERT(stcb != NULL, ("stcb is NULL"));
12841 SCTP_TCB_LOCK_ASSERT(stcb);
12842 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
12843 ("Association about to be freed"));
12844 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
12845 ("Association was aborted"));
12846
12847 if (sinfo_flags & SCTP_ADDR_OVER) {
12848 if (addr != NULL) {
12849 net = sctp_findnet(stcb, addr);
12850 } else {
12851 net = NULL;
12852 }
12853 if ((net == NULL) ||
12854 ((port != 0) && (port != stcb->rport))) {
12855 error = EINVAL;
12856 goto out_unlocked;
12857 }
12858 } else {
12859 if (asoc->alternate != NULL) {
12860 net = asoc->alternate;
12861 } else {
12862 net = asoc->primary_destination;
12863 }
12864 }
12865 if (sndlen == 0) {
12866 if (sinfo_flags & SCTP_EOF) {
12867 got_all_of_the_send = true;
12868 goto dataless_eof;
12869 } else {
12870 error = EINVAL;
12871 goto out_unlocked;
12872 }
12873 }
12874 if (sctp_is_feature_on(inp, SCTP_PCB_FLAGS_NO_FRAGMENT)) {
12875 if (sndlen > (ssize_t)asoc->smallest_mtu) {
12876 error = EMSGSIZE;
12877 goto out_unlocked;
12878 }
12879 }
12880 sinfo_stream = sndrcvninfo->sinfo_stream;
12881 /* Is the stream no. valid? */
12882 if (sinfo_stream >= asoc->streamoutcnt) {
12883 /* Invalid stream number */
12884 error = EINVAL;
12885 goto out_unlocked;
12886 }
12887 if ((asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPEN) &&
12888 (asoc->strmout[sinfo_stream].state != SCTP_STREAM_OPENING)) {
12889 /*
12890 * Can't queue any data while stream reset is underway.
12891 */
12892 if (asoc->strmout[sinfo_stream].state > SCTP_STREAM_OPEN) {
12893 error = EAGAIN;
12894 } else {
12895 error = EINVAL;
12896 }
12897 goto out_unlocked;
12898 }
12899 atomic_add_int(&stcb->total_sends, 1);
12900 if (SCTP_SO_IS_NBIO(so) || (flags & (MSG_NBIO | MSG_DONTWAIT)) != 0) {
12901 non_blocking = true;
12902 }
12903 if (non_blocking) {
12904 ssize_t amount;
12905
12906 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12907 if (user_marks_eor == 0) {
12908 amount = sndlen;
12909 } else {
12910 amount = 1;
12911 }
12912 if ((SCTP_SB_LIMIT_SND(so) < (amount + inqueue_bytes + asoc->sb_send_resv)) ||
12913 (asoc->chunks_on_out_queue >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12914 if ((sndlen > (ssize_t)SCTP_SB_LIMIT_SND(so)) &&
12915 (user_marks_eor == 0)) {
12916 error = EMSGSIZE;
12917 } else {
12918 error = EWOULDBLOCK;
12919 }
12920 goto out_unlocked;
12921 }
12922 }
12923 atomic_add_int(&asoc->sb_send_resv, (int)sndlen);
12924 local_soresv = sndlen;
12925
12926 KASSERT(stcb != NULL, ("stcb is NULL"));
12927 SCTP_TCB_LOCK_ASSERT(stcb);
12928 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
12929 ("Association about to be freed"));
12930 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
12931 ("Association was aborted"));
12932
12933 /* Ok, we will attempt a msgsnd :> */
12934 if (p != NULL) {
12935 p->td_ru.ru_msgsnd++;
12936 }
12937 /* Calculate the maximum we can send */
12938 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12939 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
12940 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
12941 } else {
12942 max_len = 0;
12943 }
12944 /* Unless E_EOR mode is on, we must make a send FIT in one call. */
12945 if ((user_marks_eor == 0) &&
12946 (sndlen > (ssize_t)SCTP_SB_LIMIT_SND(stcb->sctp_socket))) {
12947 /* It will NEVER fit. */
12948 error = EMSGSIZE;
12949 goto out_unlocked;
12950 }
12951 if (user_marks_eor != 0) {
12952 local_add_more = (ssize_t)min(SCTP_SB_LIMIT_SND(so), SCTP_BASE_SYSCTL(sctp_add_more_threshold));
12953 } else {
12954 /*-
12955 * For non-eeor the whole message must fit in
12956 * the socket send buffer.
12957 */
12958 local_add_more = sndlen;
12959 }
12960 if (non_blocking) {
12961 goto skip_preblock;
12962 }
12963 if (((max_len <= local_add_more) && ((ssize_t)SCTP_SB_LIMIT_SND(so) >= local_add_more)) ||
12964 (max_len == 0) ||
12965 ((asoc->chunks_on_out_queue + asoc->stream_queue_cnt) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12966 /* No room right now! */
12967 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
12968 SOCKBUF_LOCK(&so->so_snd);
12969 while ((SCTP_SB_LIMIT_SND(so) < (inqueue_bytes + local_add_more)) ||
12970 ((asoc->stream_queue_cnt + asoc->chunks_on_out_queue) >= SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue))) {
12971 SCTPDBG(SCTP_DEBUG_OUTPUT1, "pre_block limit:%u <(inq:%d + %zd) || (%d+%d > %d)\n",
12972 (unsigned int)SCTP_SB_LIMIT_SND(so),
12973 inqueue_bytes,
12974 local_add_more,
12975 asoc->stream_queue_cnt,
12976 asoc->chunks_on_out_queue,
12977 SCTP_BASE_SYSCTL(sctp_max_chunks_on_queue));
12978 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
12979 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLKA, asoc, sndlen);
12980 }
12981 be.error = 0;
12982 stcb->block_entry = &be;
12983 SCTP_TCB_UNLOCK(stcb);
12984 error = sbwait(&so->so_snd);
12985 if (error == 0) {
12986 if (so->so_error != 0) {
12987 error = so->so_error;
12988 }
12989 if (be.error != 0) {
12990 error = be.error;
12991 }
12992 }
12993 SOCKBUF_UNLOCK(&so->so_snd);
12994 SCTP_TCB_LOCK(stcb);
12995 stcb->block_entry = NULL;
12996 if (error != 0) {
12997 goto out_unlocked;
12998 }
12999 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13000 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13001 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13002 /*
13003 * XXX: Could also be ECONNABORTED,
13004 * not enough info.
13005 */
13006 error = ECONNRESET;
13007 } else {
13008 error = ENOTCONN;
13009 }
13010 goto out_unlocked;
13011 }
13012 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13013 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13014 asoc, asoc->total_output_queue_size);
13015 }
13016 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13017 SOCKBUF_LOCK(&so->so_snd);
13018 }
13019 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13020 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13021 } else {
13022 max_len = 0;
13023 }
13024 SOCKBUF_UNLOCK(&so->so_snd);
13025 }
13026
13027 skip_preblock:
13028 KASSERT(stcb != NULL, ("stcb is NULL"));
13029 SCTP_TCB_LOCK_ASSERT(stcb);
13030 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13031 ("Association about to be freed"));
13032 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13033 ("Association was aborted"));
13034
13035 /*
13036 * sndlen covers for mbuf case uio_resid covers for the non-mbuf
13037 * case NOTE: uio will be null when top/mbuf is passed
13038 */
13039 if (top == NULL) {
13040 struct sctp_stream_queue_pending *sp;
13041 struct sctp_stream_out *strm;
13042 uint32_t sndout;
13043
13044 if ((asoc->stream_locked) &&
13045 (asoc->stream_locked_on != sinfo_stream)) {
13046 error = EINVAL;
13047 goto out;
13048 }
13049 strm = &asoc->strmout[sinfo_stream];
13050 if (strm->last_msg_incomplete == 0) {
13051 do_a_copy_in:
13052 SCTP_TCB_UNLOCK(stcb);
13053 sp = sctp_copy_it_in(stcb, asoc, sndrcvninfo, uio, net, max_len, user_marks_eor, &error);
13054 SCTP_TCB_LOCK(stcb);
13055 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13056 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13057 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13058 /*
13059 * XXX: Could also be ECONNABORTED,
13060 * not enough info.
13061 */
13062 error = ECONNRESET;
13063 } else {
13064 error = ENOTCONN;
13065 }
13066 goto out;
13067 }
13068 if (error != 0) {
13069 goto out;
13070 }
13071 /*
13072 * Reject the sending of a new user message, if the
13073 * association is about to be shut down.
13074 */
13075 if ((SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_SENT) ||
13076 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_RECEIVED) ||
13077 (SCTP_GET_STATE(stcb) == SCTP_STATE_SHUTDOWN_ACK_SENT) ||
13078 (asoc->state & SCTP_STATE_SHUTDOWN_PENDING)) {
13079 if (sp->data != 0) {
13080 sctp_m_freem(sp->data);
13081 sp->data = NULL;
13082 sp->tail_mbuf = NULL;
13083 sp->length = 0;
13084 }
13085 if (sp->net != NULL) {
13086 sctp_free_remote_addr(sp->net);
13087 sp->net = NULL;
13088 }
13089 sctp_free_a_strmoq(stcb, sp, SCTP_SO_LOCKED);
13090 error = EPIPE;
13091 goto out_unlocked;
13092 }
13093 /* The out streams might be reallocated. */
13094 strm = &asoc->strmout[sinfo_stream];
13095 if (sp->msg_is_complete) {
13096 strm->last_msg_incomplete = 0;
13097 asoc->stream_locked = 0;
13098 } else {
13099 /*
13100 * Just got locked to this guy in case of an
13101 * interrupt.
13102 */
13103 strm->last_msg_incomplete = 1;
13104 if (asoc->idata_supported == 0) {
13105 asoc->stream_locked = 1;
13106 asoc->stream_locked_on = sinfo_stream;
13107 }
13108 sp->sender_all_done = 0;
13109 }
13110 sctp_snd_sb_alloc(stcb, sp->length);
13111 atomic_add_int(&asoc->stream_queue_cnt, 1);
13112 if (sinfo_flags & SCTP_UNORDERED) {
13113 SCTP_STAT_INCR(sctps_sends_with_unord);
13114 }
13115 sp->processing = 1;
13116 TAILQ_INSERT_TAIL(&strm->outqueue, sp, next);
13117 asoc->ss_functions.sctp_ss_add_to_stream(stcb, asoc, strm, sp);
13118 } else {
13119 sp = TAILQ_LAST(&strm->outqueue, sctp_streamhead);
13120 if (sp == NULL) {
13121 /* ???? Huh ??? last msg is gone */
13122 #ifdef INVARIANTS
13123 panic("Warning: Last msg marked incomplete, yet nothing left?");
13124 #else
13125 SCTP_PRINTF("Warning: Last msg marked incomplete, yet nothing left?\n");
13126 strm->last_msg_incomplete = 0;
13127 #endif
13128 goto do_a_copy_in;
13129 }
13130 if (sp->processing != 0) {
13131 error = EINVAL;
13132 goto out;
13133 } else {
13134 sp->processing = 1;
13135 }
13136 }
13137
13138 KASSERT(stcb != NULL, ("stcb is NULL"));
13139 SCTP_TCB_LOCK_ASSERT(stcb);
13140 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13141 ("Association about to be freed"));
13142 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13143 ("Association was aborted"));
13144
13145 while (uio->uio_resid > 0) {
13146 /* How much room do we have? */
13147 struct mbuf *new_tail, *mm;
13148
13149 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13150 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes) {
13151 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13152 } else {
13153 max_len = 0;
13154 }
13155 if ((max_len > (ssize_t)SCTP_BASE_SYSCTL(sctp_add_more_threshold)) ||
13156 ((max_len > 0) && (SCTP_SB_LIMIT_SND(so) < SCTP_BASE_SYSCTL(sctp_add_more_threshold))) ||
13157 (uio->uio_resid <= max_len)) {
13158 SCTP_TCB_UNLOCK(stcb);
13159 sndout = 0;
13160 new_tail = NULL;
13161 mm = sctp_copy_resume(uio, (int)max_len, user_marks_eor, &error, &sndout, &new_tail);
13162 SCTP_TCB_LOCK(stcb);
13163 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13164 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13165 /*
13166 * We need to get out. Peer probably
13167 * aborted.
13168 */
13169 sctp_m_freem(mm);
13170 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13171 /*
13172 * XXX: Could also be
13173 * ECONNABORTED, not enough
13174 * info.
13175 */
13176 error = ECONNRESET;
13177 } else {
13178 error = ENOTCONN;
13179 }
13180 goto out;
13181 }
13182 if ((mm == NULL) || (error != 0)) {
13183 if (mm != NULL) {
13184 sctp_m_freem(mm);
13185 }
13186 if (sp != NULL) {
13187 sp->processing = 0;
13188 }
13189 goto out;
13190 }
13191 /* Update the mbuf and count */
13192 if (sp->tail_mbuf != NULL) {
13193 /* Tack it to the end. */
13194 SCTP_BUF_NEXT(sp->tail_mbuf) = mm;
13195 } else {
13196 /* A stolen mbuf. */
13197 sp->data = mm;
13198 }
13199 sp->tail_mbuf = new_tail;
13200 sctp_snd_sb_alloc(stcb, sndout);
13201 atomic_add_int(&sp->length, sndout);
13202 if (sinfo_flags & SCTP_SACK_IMMEDIATELY) {
13203 sp->sinfo_flags |= SCTP_SACK_IMMEDIATELY;
13204 }
13205
13206 /* Did we reach EOR? */
13207 if ((uio->uio_resid == 0) &&
13208 ((user_marks_eor == 0) ||
13209 (sinfo_flags & SCTP_EOF) ||
13210 (user_marks_eor && (sinfo_flags & SCTP_EOR)))) {
13211 sp->msg_is_complete = 1;
13212 } else {
13213 sp->msg_is_complete = 0;
13214 }
13215 }
13216
13217 KASSERT(stcb != NULL, ("stcb is NULL"));
13218 SCTP_TCB_LOCK_ASSERT(stcb);
13219 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13220 ("Association about to be freed"));
13221 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13222 ("Association was aborted"));
13223
13224 if (uio->uio_resid == 0) {
13225 /* got it all? */
13226 continue;
13227 }
13228 /* PR-SCTP? */
13229 if ((asoc->prsctp_supported) && (asoc->sent_queue_cnt_removeable > 0)) {
13230 /*
13231 * This is ugly but we must assure locking
13232 * order
13233 */
13234 sctp_prune_prsctp(stcb, asoc, sndrcvninfo, (int)sndlen);
13235 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13236 if (SCTP_SB_LIMIT_SND(so) > inqueue_bytes)
13237 max_len = SCTP_SB_LIMIT_SND(so) - inqueue_bytes;
13238 else
13239 max_len = 0;
13240 if (max_len > 0) {
13241 continue;
13242 }
13243 }
13244 /* wait for space now */
13245 if (non_blocking) {
13246 /* Non-blocking io in place out */
13247 if (sp != NULL) {
13248 sp->processing = 0;
13249 }
13250 goto skip_out_eof;
13251 }
13252 /* What about the INIT, send it maybe */
13253 if (queue_only_for_init) {
13254 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13255 /* a collision took us forward? */
13256 queue_only = 0;
13257 } else {
13258 NET_EPOCH_ENTER(et);
13259 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13260 NET_EPOCH_EXIT(et);
13261 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13262 queue_only = 1;
13263 }
13264 }
13265 if ((net->flight_size > net->cwnd) &&
13266 (asoc->sctp_cmt_on_off == 0)) {
13267 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13268 queue_only = 1;
13269 } else if (asoc->ifp_had_enobuf) {
13270 SCTP_STAT_INCR(sctps_ifnomemqueued);
13271 if (net->flight_size > (2 * net->mtu)) {
13272 queue_only = 1;
13273 }
13274 asoc->ifp_had_enobuf = 0;
13275 }
13276 un_sent = asoc->total_output_queue_size - asoc->total_flight;
13277 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13278 (asoc->total_flight > 0) &&
13279 (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13280 (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13281 /*-
13282 * Ok, Nagle is set on and we have data outstanding.
13283 * Don't send anything and let SACKs drive out the
13284 * data unless we have a "full" segment to send.
13285 */
13286 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13287 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13288 }
13289 SCTP_STAT_INCR(sctps_naglequeued);
13290 nagle_applies = 1;
13291 } else {
13292 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13293 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13294 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13295 }
13296 SCTP_STAT_INCR(sctps_naglesent);
13297 nagle_applies = 0;
13298 }
13299 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13300 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13301 nagle_applies, un_sent);
13302 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13303 asoc->total_flight,
13304 asoc->chunks_on_out_queue, asoc->total_flight_count);
13305 }
13306 if (queue_only_for_init) {
13307 queue_only_for_init = 0;
13308 }
13309 if ((queue_only == 0) && (nagle_applies == 0)) {
13310 /*-
13311 * need to start chunk output
13312 * before blocking.. note that if
13313 * a lock is already applied, then
13314 * the input via the net is happening
13315 * and I don't need to start output :-D
13316 */
13317 NET_EPOCH_ENTER(et);
13318 sctp_chunk_output(inp, stcb,
13319 SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13320 NET_EPOCH_EXIT(et);
13321 }
13322 /*-
13323 * This is a bit strange, but I think it will
13324 * work. The total_output_queue_size is locked and
13325 * protected by the TCB_LOCK, which we just released.
13326 * There is a race that can occur between releasing it
13327 * above, and me getting the socket lock, where sacks
13328 * come in but we have not put the SB_WAIT on the
13329 * so_snd buffer to get the wakeup. After the LOCK
13330 * is applied the sack_processing will also need to
13331 * LOCK the so->so_snd to do the actual sowwakeup(). So
13332 * once we have the socket buffer lock if we recheck the
13333 * size we KNOW we will get to sleep safely with the
13334 * wakeup flag in place.
13335 */
13336 inqueue_bytes = asoc->total_output_queue_size - (asoc->chunks_on_out_queue * SCTP_DATA_CHUNK_OVERHEAD(stcb));
13337 SOCKBUF_LOCK(&so->so_snd);
13338 if (SCTP_SB_LIMIT_SND(so) <= (inqueue_bytes +
13339 min(SCTP_BASE_SYSCTL(sctp_add_more_threshold), SCTP_SB_LIMIT_SND(so)))) {
13340 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13341 sctp_log_block(SCTP_BLOCK_LOG_INTO_BLK,
13342 asoc, uio->uio_resid);
13343 }
13344 be.error = 0;
13345 stcb->block_entry = &be;
13346 SCTP_TCB_UNLOCK(stcb);
13347 error = sbwait(&so->so_snd);
13348 if (error == 0) {
13349 if (so->so_error != 0)
13350 error = so->so_error;
13351 if (be.error != 0) {
13352 error = be.error;
13353 }
13354 }
13355 SOCKBUF_UNLOCK(&so->so_snd);
13356 SCTP_TCB_LOCK(stcb);
13357 stcb->block_entry = NULL;
13358 if ((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) ||
13359 (asoc->state & SCTP_STATE_WAS_ABORTED)) {
13360 if (asoc->state & SCTP_STATE_WAS_ABORTED) {
13361 /*
13362 * XXX: Could also be
13363 * ECONNABORTED, not enough
13364 * info.
13365 */
13366 error = ECONNRESET;
13367 } else {
13368 error = ENOTCONN;
13369 }
13370 goto out_unlocked;
13371 }
13372 if (error != 0) {
13373 if (sp != NULL) {
13374 sp->processing = 0;
13375 }
13376 goto out_unlocked;
13377 }
13378 } else {
13379 SOCKBUF_UNLOCK(&so->so_snd);
13380 }
13381 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13382 sctp_log_block(SCTP_BLOCK_LOG_OUTOF_BLK,
13383 asoc, asoc->total_output_queue_size);
13384 }
13385 }
13386
13387 KASSERT(stcb != NULL, ("stcb is NULL"));
13388 SCTP_TCB_LOCK_ASSERT(stcb);
13389 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13390 ("Association about to be freed"));
13391 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13392 ("Association was aborted"));
13393
13394 /* The out streams might be reallocated. */
13395 strm = &asoc->strmout[sinfo_stream];
13396 if (sp != NULL) {
13397 if (sp->msg_is_complete == 0) {
13398 strm->last_msg_incomplete = 1;
13399 if (asoc->idata_supported == 0) {
13400 asoc->stream_locked = 1;
13401 asoc->stream_locked_on = sinfo_stream;
13402 }
13403 } else {
13404 sp->sender_all_done = 1;
13405 strm->last_msg_incomplete = 0;
13406 asoc->stream_locked = 0;
13407 }
13408 sp->processing = 0;
13409 } else {
13410 SCTP_PRINTF("Huh no sp TSNH?\n");
13411 strm->last_msg_incomplete = 0;
13412 asoc->stream_locked = 0;
13413 }
13414 if (uio->uio_resid == 0) {
13415 got_all_of_the_send = true;
13416 }
13417 } else {
13418 error = sctp_msg_append(stcb, net, top, sndrcvninfo);
13419 top = NULL;
13420 if ((sinfo_flags & SCTP_EOF) != 0) {
13421 got_all_of_the_send = true;
13422 }
13423 }
13424 if (error != 0) {
13425 goto out;
13426 }
13427
13428 dataless_eof:
13429 KASSERT(stcb != NULL, ("stcb is NULL"));
13430 SCTP_TCB_LOCK_ASSERT(stcb);
13431 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13432 ("Association about to be freed"));
13433 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13434 ("Association was aborted"));
13435
13436 /* EOF thing ? */
13437 if ((sinfo_flags & SCTP_EOF) && got_all_of_the_send) {
13438 SCTP_STAT_INCR(sctps_sends_with_eof);
13439 error = 0;
13440 if (TAILQ_EMPTY(&asoc->send_queue) &&
13441 TAILQ_EMPTY(&asoc->sent_queue) &&
13442 sctp_is_there_unsent_data(stcb, SCTP_SO_LOCKED) == 0) {
13443 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13444 goto abort_anyway;
13445 }
13446 /* there is nothing queued to send, so I'm done... */
13447 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13448 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13449 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13450 struct sctp_nets *netp;
13451
13452 /* only send SHUTDOWN the first time through */
13453 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13454 SCTP_STAT_DECR_GAUGE32(sctps_currestab);
13455 }
13456 SCTP_SET_STATE(stcb, SCTP_STATE_SHUTDOWN_SENT);
13457 sctp_stop_timers_for_shutdown(stcb);
13458 if (asoc->alternate != NULL) {
13459 netp = asoc->alternate;
13460 } else {
13461 netp = asoc->primary_destination;
13462 }
13463 sctp_send_shutdown(stcb, netp);
13464 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWN, stcb->sctp_ep, stcb,
13465 netp);
13466 sctp_timer_start(SCTP_TIMER_TYPE_SHUTDOWNGUARD, stcb->sctp_ep, stcb,
13467 NULL);
13468 }
13469 } else {
13470 /*-
13471 * we still got (or just got) data to send, so set
13472 * SHUTDOWN_PENDING
13473 */
13474 /*-
13475 * XXX sockets draft says that SCTP_EOF should be
13476 * sent with no data. currently, we will allow user
13477 * data to be sent first and move to
13478 * SHUTDOWN-PENDING
13479 */
13480 if ((SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_SENT) &&
13481 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_RECEIVED) &&
13482 (SCTP_GET_STATE(stcb) != SCTP_STATE_SHUTDOWN_ACK_SENT)) {
13483 if ((*asoc->ss_functions.sctp_ss_is_user_msgs_incomplete) (stcb, asoc)) {
13484 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_PARTIAL_MSG_LEFT);
13485 }
13486 SCTP_ADD_SUBSTATE(stcb, SCTP_STATE_SHUTDOWN_PENDING);
13487 if (TAILQ_EMPTY(&asoc->send_queue) &&
13488 TAILQ_EMPTY(&asoc->sent_queue) &&
13489 (asoc->state & SCTP_STATE_PARTIAL_MSG_LEFT)) {
13490 struct mbuf *op_err;
13491 char msg[SCTP_DIAG_INFO_LEN];
13492
13493 abort_anyway:
13494 if (free_cnt_applied) {
13495 atomic_subtract_int(&asoc->refcnt, 1);
13496 free_cnt_applied = false;
13497 }
13498 SCTP_SNPRINTF(msg, sizeof(msg),
13499 "%s:%d at %s", __FILE__, __LINE__, __func__);
13500 op_err = sctp_generate_cause(SCTP_BASE_SYSCTL(sctp_diag_info_code),
13501 msg);
13502 NET_EPOCH_ENTER(et);
13503 sctp_abort_an_association(stcb->sctp_ep, stcb,
13504 op_err, false, SCTP_SO_LOCKED);
13505 NET_EPOCH_EXIT(et);
13506 stcb = NULL;
13507 error = ECONNABORTED;
13508 goto out;
13509 }
13510 sctp_feature_off(inp, SCTP_PCB_FLAGS_NODELAY);
13511 }
13512 }
13513 }
13514
13515 skip_out_eof:
13516 KASSERT(stcb != NULL, ("stcb is NULL"));
13517 SCTP_TCB_LOCK_ASSERT(stcb);
13518 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13519 ("Association about to be freed"));
13520 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13521 ("Association was aborted"));
13522
13523 some_on_control = !TAILQ_EMPTY(&asoc->control_send_queue);
13524 if (queue_only_for_init) {
13525 if (SCTP_GET_STATE(stcb) == SCTP_STATE_OPEN) {
13526 /* a collision took us forward? */
13527 queue_only = 0;
13528 } else {
13529 NET_EPOCH_ENTER(et);
13530 sctp_send_initiate(inp, stcb, SCTP_SO_LOCKED);
13531 NET_EPOCH_EXIT(et);
13532 SCTP_SET_STATE(stcb, SCTP_STATE_COOKIE_WAIT);
13533 queue_only = 1;
13534 }
13535 }
13536
13537 KASSERT(stcb != NULL, ("stcb is NULL"));
13538 SCTP_TCB_LOCK_ASSERT(stcb);
13539 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13540 ("Association about to be freed"));
13541 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13542 ("Association was aborted"));
13543
13544 if ((net->flight_size > net->cwnd) &&
13545 (asoc->sctp_cmt_on_off == 0)) {
13546 SCTP_STAT_INCR(sctps_send_cwnd_avoid);
13547 queue_only = 1;
13548 } else if (asoc->ifp_had_enobuf) {
13549 SCTP_STAT_INCR(sctps_ifnomemqueued);
13550 if (net->flight_size > (2 * net->mtu)) {
13551 queue_only = 1;
13552 }
13553 asoc->ifp_had_enobuf = 0;
13554 }
13555 un_sent = asoc->total_output_queue_size - asoc->total_flight;
13556 if ((sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY)) &&
13557 (asoc->total_flight > 0) &&
13558 (asoc->stream_queue_cnt < SCTP_MAX_DATA_BUNDLING) &&
13559 (un_sent < (int)(asoc->smallest_mtu - SCTP_MIN_OVERHEAD))) {
13560 /*-
13561 * Ok, Nagle is set on and we have data outstanding.
13562 * Don't send anything and let SACKs drive out the
13563 * data unless wen have a "full" segment to send.
13564 */
13565 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13566 sctp_log_nagle_event(stcb, SCTP_NAGLE_APPLIED);
13567 }
13568 SCTP_STAT_INCR(sctps_naglequeued);
13569 nagle_applies = 1;
13570 } else {
13571 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_NAGLE_LOGGING_ENABLE) {
13572 if (sctp_is_feature_off(inp, SCTP_PCB_FLAGS_NODELAY))
13573 sctp_log_nagle_event(stcb, SCTP_NAGLE_SKIPPED);
13574 }
13575 SCTP_STAT_INCR(sctps_naglesent);
13576 nagle_applies = 0;
13577 }
13578 if (SCTP_BASE_SYSCTL(sctp_logging_level) & SCTP_BLK_LOGGING_ENABLE) {
13579 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, queue_only_for_init, queue_only,
13580 nagle_applies, un_sent);
13581 sctp_misc_ints(SCTP_CWNDLOG_PRESEND, asoc->total_output_queue_size,
13582 asoc->total_flight,
13583 asoc->chunks_on_out_queue, asoc->total_flight_count);
13584 }
13585
13586 KASSERT(stcb != NULL, ("stcb is NULL"));
13587 SCTP_TCB_LOCK_ASSERT(stcb);
13588 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13589 ("Association about to be freed"));
13590 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13591 ("Association was aborted"));
13592
13593 NET_EPOCH_ENTER(et);
13594 if ((queue_only == 0) && (nagle_applies == 0) && (asoc->peers_rwnd && un_sent)) {
13595 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13596 } else if ((queue_only == 0) &&
13597 (asoc->peers_rwnd == 0) &&
13598 (asoc->total_flight == 0)) {
13599 /* We get to have a probe outstanding */
13600 sctp_chunk_output(inp, stcb, SCTP_OUTPUT_FROM_USR_SEND, SCTP_SO_LOCKED);
13601 } else if (some_on_control) {
13602 int num_out, reason;
13603
13604 /* Here we do control only */
13605 (void)sctp_med_chunk_output(inp, stcb, asoc, &num_out,
13606 &reason, 1, 1, &now, &now_filled,
13607 sctp_get_frag_point(stcb),
13608 SCTP_SO_LOCKED);
13609 }
13610 NET_EPOCH_EXIT(et);
13611 SCTPDBG(SCTP_DEBUG_OUTPUT1, "USR Send complete qo:%d prw:%d unsent:%d tf:%d cooq:%d toqs:%d err:%d\n",
13612 queue_only, asoc->peers_rwnd, un_sent,
13613 asoc->total_flight, asoc->chunks_on_out_queue,
13614 asoc->total_output_queue_size, error);
13615
13616 KASSERT(stcb != NULL, ("stcb is NULL"));
13617 SCTP_TCB_LOCK_ASSERT(stcb);
13618 KASSERT((asoc->state & SCTP_STATE_ABOUT_TO_BE_FREED) == 0,
13619 ("Association about to be freed"));
13620 KASSERT((asoc->state & SCTP_STATE_WAS_ABORTED) == 0,
13621 ("Association was aborted"));
13622
13623 out:
13624 out_unlocked:
13625 if (create_lock_applied) {
13626 SCTP_ASOC_CREATE_UNLOCK(inp);
13627 }
13628 if (stcb != NULL) {
13629 if (local_soresv) {
13630 atomic_subtract_int(&asoc->sb_send_resv, (int)sndlen);
13631 }
13632 if (free_cnt_applied) {
13633 atomic_subtract_int(&asoc->refcnt, 1);
13634 }
13635 SCTP_TCB_UNLOCK(stcb);
13636 }
13637 if (top != NULL) {
13638 sctp_m_freem(top);
13639 }
13640 if (control != NULL) {
13641 sctp_m_freem(control);
13642 }
13643 SCTP_LTRACE_ERR_RET(inp, stcb, net, SCTP_FROM_SCTP_OUTPUT, error);
13644 return (error);
13645 }
13646
13647 /*
13648 * generate an AUTHentication chunk, if required
13649 */
13650 struct mbuf *
sctp_add_auth_chunk(struct mbuf * m,struct mbuf ** m_end,struct sctp_auth_chunk ** auth_ret,uint32_t * offset,struct sctp_tcb * stcb,uint8_t chunk)13651 sctp_add_auth_chunk(struct mbuf *m, struct mbuf **m_end,
13652 struct sctp_auth_chunk **auth_ret, uint32_t *offset,
13653 struct sctp_tcb *stcb, uint8_t chunk)
13654 {
13655 struct mbuf *m_auth;
13656 struct sctp_auth_chunk *auth;
13657 int chunk_len;
13658 struct mbuf *cn;
13659
13660 if ((m_end == NULL) || (auth_ret == NULL) || (offset == NULL) ||
13661 (stcb == NULL))
13662 return (m);
13663
13664 if (stcb->asoc.auth_supported == 0) {
13665 return (m);
13666 }
13667 /* does the requested chunk require auth? */
13668 if (!sctp_auth_is_required_chunk(chunk, stcb->asoc.peer_auth_chunks)) {
13669 return (m);
13670 }
13671 m_auth = sctp_get_mbuf_for_msg(sizeof(*auth), 0, M_NOWAIT, 1, MT_HEADER);
13672 if (m_auth == NULL) {
13673 /* no mbuf's */
13674 return (m);
13675 }
13676 /* reserve some space if this will be the first mbuf */
13677 if (m == NULL)
13678 SCTP_BUF_RESV_UF(m_auth, SCTP_MIN_OVERHEAD);
13679 /* fill in the AUTH chunk details */
13680 auth = mtod(m_auth, struct sctp_auth_chunk *);
13681 memset(auth, 0, sizeof(*auth));
13682 auth->ch.chunk_type = SCTP_AUTHENTICATION;
13683 auth->ch.chunk_flags = 0;
13684 chunk_len = sizeof(*auth) +
13685 sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
13686 auth->ch.chunk_length = htons(chunk_len);
13687 auth->hmac_id = htons(stcb->asoc.peer_hmac_id);
13688 /* key id and hmac digest will be computed and filled in upon send */
13689
13690 /* save the offset where the auth was inserted into the chain */
13691 *offset = 0;
13692 for (cn = m; cn; cn = SCTP_BUF_NEXT(cn)) {
13693 *offset += SCTP_BUF_LEN(cn);
13694 }
13695
13696 /* update length and return pointer to the auth chunk */
13697 SCTP_BUF_LEN(m_auth) = chunk_len;
13698 m = sctp_copy_mbufchain(m_auth, m, m_end, 1, chunk_len, 0);
13699 if (auth_ret != NULL)
13700 *auth_ret = auth;
13701
13702 return (m);
13703 }
13704
13705 #ifdef INET6
13706 int
sctp_v6src_match_nexthop(struct sockaddr_in6 * src6,sctp_route_t * ro)13707 sctp_v6src_match_nexthop(struct sockaddr_in6 *src6, sctp_route_t *ro)
13708 {
13709 struct nd_prefix *pfx = NULL;
13710 struct nd_pfxrouter *pfxrtr = NULL;
13711 struct sockaddr_in6 gw6;
13712
13713 if (ro == NULL || ro->ro_nh == NULL || src6->sin6_family != AF_INET6)
13714 return (0);
13715
13716 /* get prefix entry of address */
13717 ND6_RLOCK();
13718 LIST_FOREACH(pfx, &MODULE_GLOBAL(nd_prefix), ndpr_entry) {
13719 if (pfx->ndpr_stateflags & NDPRF_DETACHED)
13720 continue;
13721 if (IN6_ARE_MASKED_ADDR_EQUAL(&pfx->ndpr_prefix.sin6_addr,
13722 &src6->sin6_addr, &pfx->ndpr_mask))
13723 break;
13724 }
13725 /* no prefix entry in the prefix list */
13726 if (pfx == NULL) {
13727 ND6_RUNLOCK();
13728 SCTPDBG(SCTP_DEBUG_OUTPUT2, "No prefix entry for ");
13729 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13730 return (0);
13731 }
13732
13733 SCTPDBG(SCTP_DEBUG_OUTPUT2, "v6src_match_nexthop(), Prefix entry is ");
13734 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)src6);
13735
13736 /* search installed gateway from prefix entry */
13737 LIST_FOREACH(pfxrtr, &pfx->ndpr_advrtrs, pfr_entry) {
13738 memset(&gw6, 0, sizeof(struct sockaddr_in6));
13739 gw6.sin6_family = AF_INET6;
13740 gw6.sin6_len = sizeof(struct sockaddr_in6);
13741 memcpy(&gw6.sin6_addr, &pfxrtr->router->rtaddr,
13742 sizeof(struct in6_addr));
13743 SCTPDBG(SCTP_DEBUG_OUTPUT2, "prefix router is ");
13744 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, (struct sockaddr *)&gw6);
13745 SCTPDBG(SCTP_DEBUG_OUTPUT2, "installed router is ");
13746 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13747 if (sctp_cmpaddr((struct sockaddr *)&gw6, &ro->ro_nh->gw_sa)) {
13748 ND6_RUNLOCK();
13749 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is installed\n");
13750 return (1);
13751 }
13752 }
13753 ND6_RUNLOCK();
13754 SCTPDBG(SCTP_DEBUG_OUTPUT2, "pfxrouter is not installed\n");
13755 return (0);
13756 }
13757 #endif
13758
13759 int
sctp_v4src_match_nexthop(struct sctp_ifa * sifa,sctp_route_t * ro)13760 sctp_v4src_match_nexthop(struct sctp_ifa *sifa, sctp_route_t *ro)
13761 {
13762 #ifdef INET
13763 struct sockaddr_in *sin, *mask;
13764 struct ifaddr *ifa;
13765 struct in_addr srcnetaddr, gwnetaddr;
13766
13767 if (ro == NULL || ro->ro_nh == NULL ||
13768 sifa->address.sa.sa_family != AF_INET) {
13769 return (0);
13770 }
13771 ifa = (struct ifaddr *)sifa->ifa;
13772 mask = (struct sockaddr_in *)(ifa->ifa_netmask);
13773 sin = &sifa->address.sin;
13774 srcnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13775 SCTPDBG(SCTP_DEBUG_OUTPUT2, "match_nexthop4: src address is ");
13776 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &sifa->address.sa);
13777 SCTPDBG(SCTP_DEBUG_OUTPUT2, "network address is %x\n", srcnetaddr.s_addr);
13778
13779 sin = &ro->ro_nh->gw4_sa;
13780 gwnetaddr.s_addr = (sin->sin_addr.s_addr & mask->sin_addr.s_addr);
13781 SCTPDBG(SCTP_DEBUG_OUTPUT2, "match_nexthop4: nexthop is ");
13782 SCTPDBG_ADDR(SCTP_DEBUG_OUTPUT2, &ro->ro_nh->gw_sa);
13783 SCTPDBG(SCTP_DEBUG_OUTPUT2, "network address is %x\n", gwnetaddr.s_addr);
13784 if (srcnetaddr.s_addr == gwnetaddr.s_addr) {
13785 return (1);
13786 }
13787 #endif
13788 return (0);
13789 }
13790